From patchwork Fri Mar 10 10:32:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13169126 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 881BAC7618A for ; Fri, 10 Mar 2023 10:32:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 717858E0001; Fri, 10 Mar 2023 05:32:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 66E3E8E0006; Fri, 10 Mar 2023 05:32:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E4E28E0001; Fri, 10 Mar 2023 05:32:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 096978E0002 for ; Fri, 10 Mar 2023 05:32:21 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E110514155A for ; Fri, 10 Mar 2023 10:32:20 +0000 (UTC) X-FDA: 80552624040.25.9E06181 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf01.hostedemail.com (Postfix) with ESMTP id EA0BC40002 for ; Fri, 10 Mar 2023 10:32:18 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=ny6yHHgx; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=c9nJ0Fcc; spf=pass (imf01.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.29 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678444339; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=F0XRtKZ/CBnBdFwvCLJ4UaehJA7uc5hjJpqNCel0wLs=; b=GxPxrlHP0b/hn4QmTkG4fH45te0yP+gjkiMrzggXWHJ9FreJK+3WHMylAYKkG3NZbdFmp+ UHLQ5XATfr2EjO1gDCbqFuHaHYe6kaxbCrpMDnmdOCjltXJTmsrVTbvNEwoEN6PSoeUWY+ J3dY1GJp0i77HhPuBtdPtf7xn36Gfj8= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=ny6yHHgx; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=c9nJ0Fcc; spf=pass (imf01.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.29 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678444339; a=rsa-sha256; cv=none; b=o9TAniaEgXrDPlPSGMVgNcmZvIgIOBxYmZkHC3iMrN9uXLW/pQ8CX6yKOneZFoL4K9bhH7 J5hcUs1oHH34Y+wKMHnSXvePJxt8Z0rdzhPDqHPe28fwaahr6d2n8tQRk2PXGF4Vj2QMFx aDe72eVcGMFoNeOR1JnC+qCS6curgDA= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 0A9B920658; Fri, 10 Mar 2023 10:32:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1678444336; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F0XRtKZ/CBnBdFwvCLJ4UaehJA7uc5hjJpqNCel0wLs=; b=ny6yHHgxBFiJbE3G6TRbuILZouc1FfvSrZIoIEn8Em8pxQKwBpTC/0Qle6cFe4FXhysYz0 NMbFaFWjtUXlnAK/ccshrkhJGiBFdHDuievEiUw86Gbdf2AFa3ZsMybWtSlZHH8oy6Sp9i ZpGBiiSbCNgZ8F9agBUQAkhYLWCxENQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1678444336; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F0XRtKZ/CBnBdFwvCLJ4UaehJA7uc5hjJpqNCel0wLs=; b=c9nJ0FccGPUvt1DKTqxoLPfWREATBKJgPjNehuttmFBezZCtQpnvtyFJ63HgnPkQ7+m1up 9RsCY1fVGVJsrCAQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id B11A6139F9; Fri, 10 Mar 2023 10:32:15 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id aCmUKi8HC2SsXQAAMHmgww (envelope-from ); Fri, 10 Mar 2023 10:32:15 +0000 From: Vlastimil Babka To: Christoph Lameter , David Rientjes , Joonsoo Kim , Pekka Enberg Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>, Roman Gushchin , Andrew Morton , linux-mm@kvack.org, rcu@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, netdev@vger.kernel.org, linux-doc@vger.kernel.org, Vlastimil Babka , Mike Rapoport , Jonathan Corbet , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Josh Triplett , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Joel Fernandes Subject: [PATCH 7/7] mm/slab: document kfree() as allowed for kmem_cache_alloc() objects Date: Fri, 10 Mar 2023 11:32:09 +0100 Message-Id: <20230310103210.22372-8-vbabka@suse.cz> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230310103210.22372-1-vbabka@suse.cz> References: <20230310103210.22372-1-vbabka@suse.cz> MIME-Version: 1.0 X-Rspamd-Queue-Id: EA0BC40002 X-Stat-Signature: c4qd4z7mc4kmt1pb1re8mbarok35pcws X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1678444338-604588 X-HE-Meta: U2FsdGVkX1/if0920MFaQA8I35H+vsk93L/060WwSgn6bOG8DFjdDmBZE+M1HKGXfoIf2jKfA4qnRU8JiTtfa6vwwE6v+h/qnY7RleuGXQbNd/ELJm1ayxl+XPWxhBOCnSiM7N0R4liNBYhLLBcdeVfB+XgtnTXM+uzL3LT/55PDK5IKDuQ6Go2jKwE7++uirFuE/Hog3bRFoYmpCVQhhIXOCXR7LEnLrW4AbS9oQx4GghdD6PN793O9isncIUnTYzeDwpKE9oYDs+ftZzZEuV97CfHVSgZ371E/gg13fuZJvHoXeEnHSZ9JnT8CEdEAyKbZrQ51aj6LXn9iXo4PEkvXtOe00yoxTPFvaHSIEY17D1kdJYIQl8jZfSciFL7j8Xihc3hly0bODZmAwS0SWxl8tkezndJ8zbT8lrco7kwvcPpQEUtAx6ixwLfFZ5el7ziVlPPQQWgd4sX+CU6nBuuWr/tBkqhG7IvTYdU9Rg0Y9VHIiHCKyOCfjY9AzqqKMVyj+ZFmCNkCXTtswicgdNyFbLUGmF3jvadELm0g/C3mC+sELW+VGaeh3u0Z3L31/OfwaHH99MsNUfzc9fWmkwW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daqqcFnfCYt6Hgbfbr4TPTNoZoL1N+lBkCGSX//JRwSXfCN1Ros1NgDJg/0y3Mu/R1g2Tiy0z/sxrz9R217 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This will make it easier to free objects in situations when they can come from either kmalloc() or kmem_cache_alloc(), and also allow kfree_rcu() for freeing objects from kmem_cache_alloc(). For the SLAB and SLUB allocators this was always possible so with SLOB gone, we can document it as supported. Signed-off-by: Vlastimil Babka Cc: Mike Rapoport Cc: Jonathan Corbet Cc: "Paul E. McKenney" Cc: Frederic Weisbecker Cc: Neeraj Upadhyay Cc: Josh Triplett Cc: Steven Rostedt Cc: Mathieu Desnoyers Cc: Lai Jiangshan Cc: Joel Fernandes --- Documentation/core-api/memory-allocation.rst | 15 +++++++++++---- include/linux/rcupdate.h | 6 ++++-- mm/slab_common.c | 5 +---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Documentation/core-api/memory-allocation.rst b/Documentation/core-api/memory-allocation.rst index 5954ddf6ee13..f9e8d352ed67 100644 --- a/Documentation/core-api/memory-allocation.rst +++ b/Documentation/core-api/memory-allocation.rst @@ -170,7 +170,14 @@ should be used if a part of the cache might be copied to the userspace. After the cache is created kmem_cache_alloc() and its convenience wrappers can allocate memory from that cache. -When the allocated memory is no longer needed it must be freed. You can -use kvfree() for the memory allocated with `kmalloc`, `vmalloc` and -`kvmalloc`. The slab caches should be freed with kmem_cache_free(). And -don't forget to destroy the cache with kmem_cache_destroy(). +When the allocated memory is no longer needed it must be freed. Objects +allocated by `kmalloc` can be freed by `kfree` or `kvfree`. +Objects allocated by `kmem_cache_alloc` can be freed with `kmem_cache_free` +or also by `kfree` or `kvfree`, which can be more convenient as it does +not require the kmem_cache pointed. +The rules for _bulk and _rcu flavors of freeing functions are analogical. + +Memory allocated by `vmalloc` can be freed with `vfree` or `kvfree`. +Memory allocated by `kvmalloc` can be freed with `kvfree`. +Caches created by `kmem_cache_create` should be freed with +`kmem_cache_destroy` only after freeing all the allocated objects first. diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h index 094321c17e48..dcd2cf1e8326 100644 --- a/include/linux/rcupdate.h +++ b/include/linux/rcupdate.h @@ -976,8 +976,10 @@ static inline notrace void rcu_read_unlock_sched_notrace(void) * either fall back to use of call_rcu() or rearrange the structure to * position the rcu_head structure into the first 4096 bytes. * - * Note that the allowable offset might decrease in the future, for example, - * to allow something like kmem_cache_free_rcu(). + * The object to be freed can be allocated either by kmalloc() or + * kmem_cache_alloc(). + * + * Note that the allowable offset might decrease in the future. * * The BUILD_BUG_ON check must not involve any function calls, hence the * checks are done in macros here. diff --git a/mm/slab_common.c b/mm/slab_common.c index 1522693295f5..607249785c07 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -989,12 +989,9 @@ EXPORT_SYMBOL(__kmalloc_node_track_caller); /** * kfree - free previously allocated memory - * @object: pointer returned by kmalloc. + * @object: pointer returned by kmalloc() or kmem_cache_alloc() * * If @object is NULL, no operation is performed. - * - * Don't free memory not originally allocated by kmalloc() - * or you will run into trouble. */ void kfree(const void *object) {