From mboxrd@z Thu Jan 1 00:00:00 1970 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 3E997C74A5B for ; Fri, 17 Mar 2023 11:49:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BBEAB6B0074; Fri, 17 Mar 2023 07:49:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B6E666B0075; Fri, 17 Mar 2023 07:49:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A38426B0078; Fri, 17 Mar 2023 07:49:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 92B836B0074 for ; Fri, 17 Mar 2023 07:49:00 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4BF62ABF05 for ; Fri, 17 Mar 2023 11:49:00 +0000 (UTC) X-FDA: 80578218840.13.ABB3FA6 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf22.hostedemail.com (Postfix) with ESMTP id 75AA2C0013 for ; Fri, 17 Mar 2023 11:48:58 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=G9saBPfd; spf=pass (imf22.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679053738; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ibJh3VT/c2O2rMXBIBdyl9tA9DMwYnhyp8aDA0PNYmA=; b=mg043DIslu7DwSIvJZ1euTJD5M1VH7/O3cXFLVCNFtSbZyf80j1GfTTyI/FAN0pR6T6gtv R/sZqxzeT5n19jSpw+UbGztpzC/ia5/YZRSM1EA6Xd/0mV43Fc+FBGaEgVIUpNqVKYCqoR ZMwwWmsjGdqE0LEsMnYihxCihZbqzis= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=G9saBPfd; spf=pass (imf22.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679053738; a=rsa-sha256; cv=none; b=QmOyuFxyxjubw79pwdq6KPZCBDTwkU0WnN7spCLqFuTpht92o3599YyWTDY2JQ4MU73vnl Zh81y2EJpaVaq/FH4pw1awAjl5zGd/9Pg6vFLIkZGyfdQAkIcOfb0MFZp53TVMc7COvCvY OCnSAEZl2IWbnTOKlKQ4HwbvPu7dcnw= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 51AFE62290; Fri, 17 Mar 2023 11:48:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B7B9C433D2; Fri, 17 Mar 2023 11:48:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679053736; bh=Ep7xq0TiU+qHaUMEFrkyCD2+t3tCkcfkRWs301HgOUc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=G9saBPfdCtC+vAwmfznGdZB2UTqdSQzF263AJzR5mcpKX2ygzQA9bJ0Od2Q9KrrFG 0vWoPa2+wsEwNvcGMSYYOmV4zKj0uapfmT7udrufWWgmjAH1SgneGQuO3s+yzqqeCb 7HWVNseINxfc9xAm/DlR+pa3mSuP6DU6rDdOgkwf2rhNiM5AwL24XItAK5yhBdIWX1 lD3q/+yLOxnjrUatpIrRYGMTlVvujX34ejxxo6K/lOf/wpCQVnf5s03xfC8DfCBGJl cFaZnZ5zillNOYl9Vau9q17yVFSvIzhG/r50v/UJbAsA4R7OqQpKtmhi0pLVq+RwtC 6Itb2xlX4MURg== Date: Fri, 17 Mar 2023 13:48:38 +0200 From: Mike Rapoport To: Vlastimil Babka Cc: Christoph Lameter , David Rientjes , Joonsoo Kim , Pekka Enberg , 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, linux-doc@vger.kernel.org, Jonathan Corbet , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Josh Triplett , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Joel Fernandes Subject: Re: [PATCH v2 6/6] mm/slab: document kfree() as allowed for kmem_cache_alloc() objects Message-ID: References: <20230317104307.29328-1-vbabka@suse.cz> <20230317104307.29328-7-vbabka@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230317104307.29328-7-vbabka@suse.cz> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: xe6ggihg6cisjd65baajehgj937fbj8g X-Rspamd-Queue-Id: 75AA2C0013 X-HE-Tag: 1679053738-226189 X-HE-Meta: U2FsdGVkX1+id+ybK1xaICQn3sHIeeradtrSEavKHalcKx+7z1PHvroIk28Vi5UlG1VDffTTwaw4E3YwFe+Ru0mfy7bQrRErdwV8Yx44BUbmaHz3SawRgiEMaqkKzm/Rn2/GyfJyWiKBV3KmiVRfOegyG7VNf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PdN4Z13MdzlfEaxF8dWxM2XW01+/vYZT1kpZ80SOqLp4K2q9viz/yUK2wOsFckd6Sgh6hkqW6CJ5UliMAzTIujj+MXjNusyYhZ9j4hxYeBo56tFLjiO8ODzmeDGEXxplSu0rj1U6MvplsDpJoYDKL5z+Zjksp2HfclOA/5/0pAAtTq3CoKDq3Yz2n35hQE+J2Z3vh/Fj5eOLDgoiqvwAsiFkM10FMNady6IkR8aG8Nrxcjv1hgo+jeF4b+JDhIMy+uMYQb0NhvtlrebQD+V5BHthZEGVbaRfsYRjlqOJ2Gcq38s9f3mAQGJudQ+aZ56QVXR39m3jGveUGimD2WWqsizgiVkD/WpWpCtwLLDhUduOP1BRgJlyzBRNzTuRqycNPa0JWHeY5rxy+QhzOnPjzsiPRfjYP43hKA8T0+jOLw9iTK/rdf/hIxfCj7v9q5Vdp+41RqJdukxofitbz5/7V6PiVEsUiu3pZD6kngLp4wsrX8oG+PpMBXNtWwOZqIOp/na51laF9FXvUr49lIbkwkwGXud9sS5O1eROgC/rS5eL9k7u0YukDn1+nF181svWnYDftlJK1W7vIRax94Dah32A+7bLoDtWs8NGLzfI9jXqGv/AiMyUv/rkM= 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: On Fri, Mar 17, 2023 at 11:43:07AM +0100, Vlastimil Babka wrote: > 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 Reviewed-by: Mike Rapoport (IBM) > --- > Documentation/core-api/memory-allocation.rst | 17 +++++++++++++---- > include/linux/rcupdate.h | 6 ++++-- > mm/slab_common.c | 5 +---- > 3 files changed, 18 insertions(+), 10 deletions(-) > > diff --git a/Documentation/core-api/memory-allocation.rst b/Documentation/core-api/memory-allocation.rst > index 5954ddf6ee13..1c58d883b273 100644 > --- a/Documentation/core-api/memory-allocation.rst > +++ b/Documentation/core-api/memory-allocation.rst > @@ -170,7 +170,16 @@ 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`, `kfree` > +or `kvfree`, where the latter two might be more convenient thanks to not > +needing the kmem_cache pointer. > + > +The same rules apply to _bulk and _rcu flavors of freeing functions. > + > +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) > { > -- > 2.39.2 > -- Sincerely yours, Mike.