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 034EDC3600C for ; Thu, 3 Apr 2025 16:59:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED12C280006; Thu, 3 Apr 2025 12:59:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E5827280005; Thu, 3 Apr 2025 12:59:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF8B0280006; Thu, 3 Apr 2025 12:59:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AD360280005 for ; Thu, 3 Apr 2025 12:59:45 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id AF17458C97 for ; Thu, 3 Apr 2025 16:59:46 +0000 (UTC) X-FDA: 83293344372.16.CAC5827 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf10.hostedemail.com (Postfix) with ESMTP id 0DDAEC0014 for ; Thu, 3 Apr 2025 16:59:44 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=OisHarl2; spf=pass (imf10.hostedemail.com: domain of kees@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743699585; 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=dPE3HowAQ2m2pe/etS+D/Mo6P/IUw+CaRhgB5DF5m3s=; b=1CCyyniSl6w24PvBJC6a585RSHcgSS6dbVgqAyjUh/DffUU3Z7jJUPH8qiBQfrWqN2rFhP doCHgReFDFk0f+NPl7eW9j6jsttyShtaxQnKXJOIubZkHqpexBDeGZ/+f4+3vbS9KFvIHF NQidKNxIBNRQApnLAj4fNWE0ksE9/k8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743699585; a=rsa-sha256; cv=none; b=Uq/LdlLHkIIHHeaAEPMAz7WEMIKpFIdGyL3Khzung3PJVOyx9wsBAGEliFMFTpInfa08WT I9o/ehgqMUV0wgypU7uZXZQKhyhDcGBuSQJRHMft4LQXcUAzMr6RoC6+hIbdOUrHePozo1 mm4moEpg0WKd4hw6M8b0OVUTbkf5qnk= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=OisHarl2; spf=pass (imf10.hostedemail.com: domain of kees@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 8B43BA44B90; Thu, 3 Apr 2025 16:54:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0BA35C4CEE3; Thu, 3 Apr 2025 16:59:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1743699584; bh=lwCLOrKjqeBhvraMsNIzlF4vAoIOPP0pmmDtxveRJqg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=OisHarl2Us9CgvsWT11VflIIAJ0QwjMDmVoWz4CDXVhOGuxfeXWJp60H92QVPkP7S Z/Ar2eg8aLxbxF5Xl5bniVmOvfrhJ80xpgphbJNPU4Xo63HromqJ3baeudaEJ7C7MV tsVmk6clyNFwj4K3OKSqIxzy19yhVTLB4XDboAtVEDcY3TWh/FI95zpVpSTGbPPmbB eB7h1JRHJE276B5g0fGUtu7Vpe61y6hL4NDyTEJDx3cpJ6t3v+QAz7UnTdzli+U5bL 6rktoftwaMyE5uOIOIH/sGow2JtP41V/obKNb5K4NvGesWVB37ukxmSVYIHgGDKC61 FYKQCpMYwUmgg== Date: Thu, 3 Apr 2025 09:59:41 -0700 From: Kees Cook To: Vlastimil Babka Cc: Przemek Kitszel , linux-kernel@vger.kernel.org, linux-mm@kvack.org, torvalds@linux-foundation.org, peterz@infradead.org, Jann Horn , andriy.shevchenko@linux.intel.com, intel-wired-lan@lists.osuosl.org, netdev@vger.kernel.org, Harry Yoo , Matthew Wilcox , Christoph Lameter Subject: Re: [RFC] slab: introduce auto_kfree macro Message-ID: <202504030955.5C4B7D82@keescook> References: <20250401134408.37312-1-przemyslaw.kitszel@intel.com> <3f387b13-5482-46ed-9f52-4a9ed7001e67@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3f387b13-5482-46ed-9f52-4a9ed7001e67@suse.cz> X-Stat-Signature: p36bbapzowt8begbcq9zicgse41hraks X-Rspam-User: X-Rspamd-Queue-Id: 0DDAEC0014 X-Rspamd-Server: rspam08 X-HE-Tag: 1743699584-692833 X-HE-Meta: U2FsdGVkX1+BuuJh7B5OV7uF3nUyQALuEFfwYsRlo52qbEEuQkNGTOZmNR2g6xEGFpv+j7huMzpmj3sm397J7d+gR++zCZryac7IDo+0v09rMPhZFV6WJKUrmkCsJBWmUv+U916fgtjFHRYYQp3g1GJxtGlg6fQPF3zz+mkF9PU/EeLCroqa1NOTmZyTmwRuF2ZADYnbzF8D7VA6PN4427YnJQcTrjQLeP0YHAx6qgRMu6a9AfeQsAXUCzX/kH5HGSk/PYQq7frSog6/zeXyADA4nklMZbEjb4DY1Z5/NyVTzakSwzD6+lTkDoSB/1kDhDkUG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@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, Apr 02, 2025 at 12:44:50PM +0200, Vlastimil Babka wrote: > Cc Kees and others from his related efforts: > > https://lore.kernel.org/all/20250321202620.work.175-kees@kernel.org/ I think, unfortunately, the consensus is that "invisible side-effects" are not going to be tolerated. After I finish with kmalloc_obj(), I'd like to take another run at this for basically providing something like: static inline __must_check void *kfree(void *p) { __kfree(p); return NULL; } And then switch all: kfree(s->ptr); to s->ptr = kfree(s->ptr); Where s->ptr isn't used again. -Kees > > On 4/1/25 15:44, Przemek Kitszel wrote: > > Add auto_kfree macro that acts as a higher level wrapper for manual > > __free(kfree) invocation, and sets the pointer to NULL - to have both > > well defined behavior also for the case code would lack other assignement. > > > > Consider the following code: > > int my_foo(int arg) > > { > > struct my_dev_foo *foo __free(kfree); /* no assignement */ > > > > foo = kzalloc(sizeof(*foo), GFP_KERNEL); > > /* ... */ > > } > > > > So far it is fine and even optimal in terms of not assigning when > > not needed. But it is typical to don't touch (and sadly to don't > > think about) code that is not related to the change, so let's consider > > an extension to the above, namely an "early return" style to check > > arg prior to allocation: > > int my_foo(int arg) > > { > > struct my_dev_foo *foo __free(kfree); /* no assignement */ > > + > > + if (!arg) > > + return -EINVAL; > > foo = kzalloc(sizeof(*foo), GFP_KERNEL); > > /* ... */ > > } > > Now we have uninitialized foo passed to kfree, what likely will crash. > > One could argue that `= NULL` should be added to this patch, but it is > > easy to forgot, especially when the foo declaration is outside of the > > default git context. > > > > With new auto_kfree, we simply will start with > > struct my_dev_foo *foo auto_kfree; > > and be safe against future extensions. > > > > I believe this will open up way for broader adoption of Scope Based > > Resource Management, say in networking. > > I also believe that my proposed name is special enough that it will > > be easy to know/spot that the assignement is hidden. > > > > Signed-off-by: Przemek Kitszel > > --- > > include/linux/slab.h | 1 + > > 1 file changed, 1 insertion(+) > > > > diff --git a/include/linux/slab.h b/include/linux/slab.h > > index 98e07e9e9e58..b943be0ce626 100644 > > --- a/include/linux/slab.h > > +++ b/include/linux/slab.h > > @@ -471,6 +471,7 @@ void kfree_sensitive(const void *objp); > > size_t __ksize(const void *objp); > > > > DEFINE_FREE(kfree, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T)) > > +#define auto_kfree __free(kfree) = NULL > > DEFINE_FREE(kfree_sensitive, void *, if (_T) kfree_sensitive(_T)) > > > > /** > -- Kees Cook