From patchwork Fri Mar 17 10:43:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 71235 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp263729wrt; Fri, 17 Mar 2023 03:46:43 -0700 (PDT) X-Google-Smtp-Source: AK7set9UUtUtVAUlCHgtsT5KJTgoAOf8pY8nZl724er42M4Z4w1fEy4KeQ8e1wWylFDYSqlbUfHR X-Received: by 2002:a17:90a:9306:b0:234:b170:1f27 with SMTP id p6-20020a17090a930600b00234b1701f27mr6094785pjo.0.1679050003125; Fri, 17 Mar 2023 03:46:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679050003; cv=none; d=google.com; s=arc-20160816; b=lGFdv6DA1066gTIV0fNzT8UwETpEOyu7WxuK4OHxM/zmdIsV/KURVI6w/GpOW5Y/NG TyrREEvohAQzyWArVSBuLf+7aH9lCCgM5WDuxHcnUeN1lEO9KfZxd41PrSZM7+/xpbe0 pbgWE2J7TBY1Ej+QodKtSwdxJ1YK921Q3esE/JXUmEw4rkn9QUUnQiN+fco7LVgYwefp arxAYxVcvOGdtfR8FwbksLmK52/ZLzjaVhcHFcZu20EvXluueNcuIan4AWZjZq0wdO3f hTxD7MoC/q7qORmjKIEQ4xGFCCAcxIDMnZJMUgTg4svnzYDY7fiaF9zdXeNxLLuPUIHd AGJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=QMYqXFz2IaKT3NUuszy4Agqdpp++dYT+bm01yseqepo=; b=YKzg9LsUSmi+RgyiCIkcZzWYdFoauutSfaOP3+LU+eJHitvvNNv7LDPMZ0bIvj8ChS n22g3mw2W0QEyil64uuOdbkjUWMOwrWmSuXwbcqMqv2pyZ7BRNDaBbHvwWaUW9V+M6WS HoBkwKhsCcVgOcgLK8W9kq1YKxxjrv+Hi9eeCqxW3aji/a7aadhyNFxBoGMQ2f8GEEYD x6gO6x/BbSvS2SdK7tupDDAc+nXVaQXnPtmRrzV1grSkVa3o+6WZR+y+zyagcnhHHtUs DFJE/+GHshP2y4lYoxp8P9xyYsd4XekLRi0IxDeiuHHc+3Z9Aq6MYA5KpnbevQLJJKXo DHSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=3RpsWYEB; dkim=neutral (no key) header.i=@suse.cz header.b=L+rlwaT8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id la8-20020a170902fa0800b0019f0e7990e8si370479plb.197.2023.03.17.03.46.27; Fri, 17 Mar 2023 03:46:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=3RpsWYEB; dkim=neutral (no key) header.i=@suse.cz header.b=L+rlwaT8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230373AbjCQKpC (ORCPT + 99 others); Fri, 17 Mar 2023 06:45:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230401AbjCQKo0 (ORCPT ); Fri, 17 Mar 2023 06:44:26 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A2832D70; Fri, 17 Mar 2023 03:43:52 -0700 (PDT) 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 6441E1FE12; Fri, 17 Mar 2023 10:43:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1679049804; 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=QMYqXFz2IaKT3NUuszy4Agqdpp++dYT+bm01yseqepo=; b=3RpsWYEBsUt1LFWxCtGa654D5WiAY6xlDzmguCq7ZIYVl2lubbeAt4r2RFSsxW0nlHA/eC c3wX6HGC3gUendkoyOfqOmUfJnYC2pOdF4XUXI+knSdJQj7iD7m2tiRkWNW32AwwO/Wf7W +qXw0+FixUwDXTRG+OSphmXADh28IKA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1679049804; 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=QMYqXFz2IaKT3NUuszy4Agqdpp++dYT+bm01yseqepo=; b=L+rlwaT8LS9vjdsg9XHVyHNS5hCWePW/zbB/fIp1ejwCCCjFDK4vtitN8q9Zka3g3gxPfG K86/MFNPW7+ZsxAQ== 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 CF23E1346F; Fri, 17 Mar 2023 10:43:23 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id yMbfMUtEFGRgdwAAMHmgww (envelope-from ); Fri, 17 Mar 2023 10:43:23 +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, 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 v2 6/6] mm/slab: document kfree() as allowed for kmem_cache_alloc() objects Date: Fri, 17 Mar 2023 11:43:07 +0100 Message-Id: <20230317104307.29328-7-vbabka@suse.cz> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230317104307.29328-1-vbabka@suse.cz> References: <20230317104307.29328-1-vbabka@suse.cz> MIME-Version: 1.0 X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_SOFTFAIL,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760611535946222881?= X-GMAIL-MSGID: =?utf-8?q?1760611535946222881?= 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) {