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 9ABBDC28B20 for ; Wed, 2 Apr 2025 12:22:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4198E280004; Wed, 2 Apr 2025 08:22:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C884280001; Wed, 2 Apr 2025 08:22:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2B8D0280004; Wed, 2 Apr 2025 08:22:27 -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 0E74F280001 for ; Wed, 2 Apr 2025 08:22:27 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B6B4B1617E9 for ; Wed, 2 Apr 2025 12:22:28 +0000 (UTC) X-FDA: 83289016776.15.40D336E Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) by imf05.hostedemail.com (Postfix) with ESMTP id 18DDC100014 for ; Wed, 2 Apr 2025 12:22:26 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=infradead.org header.s=desiato.20200630 header.b="exMHggJ/"; spf=none (imf05.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.92.199) smtp.mailfrom=peterz@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743596547; 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=33b2t4HcrwoP+ai68jl46pM/QnlIuyA8Mc5B+grxlcA=; b=BtK5XZF1FpOLqgXO2WYZNhn+DisnUwRTEoAveM3ItABmPHDp4We5Qj4JFOoofchg3rtek1 dn+Iyl30FgrXp7HWpUhO5EuYUPp9pr//cnzYLg9vBIyrtoavU0EoKSq4Ui6IUvKyCFTZ4/ vqLMqgB8MWzsJ0RdS2d5qb/o+UYRfro= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=infradead.org header.s=desiato.20200630 header.b="exMHggJ/"; spf=none (imf05.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.92.199) smtp.mailfrom=peterz@infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743596547; a=rsa-sha256; cv=none; b=S1RIEwVXpI54UcP8o0Spvrl7UQg1IwaD9YwuaqegoZyZLUOMPY6y1NA5XMEdbex3Gd2OBI oaCvkIByk3R+f8dkX/Xd4s0poMSXezjIs2a79Rvry10tLgqBx81e7YPTzmCfqjUEHkLGS3 DiujFNwdsH/x5Wy2WEbyl7WAIA4Yf/s= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=33b2t4HcrwoP+ai68jl46pM/QnlIuyA8Mc5B+grxlcA=; b=exMHggJ//R3UNW/FQV6swFas17 utBymsHGwpsvS0cD/fUT65Wxa+tkWNbJzZkCbFpksuhWh/1sZMpX8oEu6+AH5qenYXWbphvvL625h BKg6nKjo73TsrmNcQGl+cphViRA8+Kh5u5BURHr1U8bktfgwRyJ1IfXtPl8xvX2tV2+y1mL63G2IE ZFjWYBq7Eo0Pa+EfSIyvcZAoiI3/8U1ghZjlOkofcF5lNVM2v8nf1YvjD7zvHS69C/2C5SsU5qYuN Ix1CCUS+GIn6rtfLrIhINo8TbHvG2DA2mObaoS7Oeoj/199PLFQxuiiPtgjE6nAMjm9GEqRtzyHsa btvoQhng==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.98.1 #2 (Red Hat Linux)) id 1tzx6v-00000007183-0Szs; Wed, 02 Apr 2025 12:22:25 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id A8C68300288; Wed, 2 Apr 2025 14:22:24 +0200 (CEST) Date: Wed, 2 Apr 2025 14:22:24 +0200 From: Peter Zijlstra To: Andy Shevchenko Cc: Przemek Kitszel , linux-kernel@vger.kernel.org, linux-mm@kvack.org, vbabka@suse.cz, torvalds@linux-foundation.org, intel-wired-lan@lists.osuosl.org, netdev@vger.kernel.org, linux-toolchains@vger.kernel.org Subject: Re: [RFC] slab: introduce auto_kfree macro Message-ID: <20250402122224.GB25719@noisy.programming.kicks-ass.net> References: <20250401134408.37312-1-przemyslaw.kitszel@intel.com> <20250402121935.GJ25239@noisy.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250402121935.GJ25239@noisy.programming.kicks-ass.net> X-Rspamd-Queue-Id: 18DDC100014 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 64etqha7pkji7ryzkj8ts95k31gwhkwb X-HE-Tag: 1743596546-239095 X-HE-Meta: U2FsdGVkX18uHA6H3vDYabH9C3Vrn63FGIitCqDDwrVjaDjzR22HiYLQvH2q2nXYUIW7et2B2iAQNFV7JrrF7qEJI7gQ96zWmCJHDQSGWrwRv/vepFZ9aY2L2KDaJaxuE2uyNLrz0KUwJpTiJHJkiDxT1lum5DSsRoRl2x7Iqp10IFx1XhotupssQ4JNtArH/VPqvU5L6BtO0VMhbRdNG0xxZNcVvI4C9SVbpyd//KXTJFJtWF4YN4VldbD5qx7/+9VYahMrnbK41xLqpCAqNDKkNr0eyvs74yQHQh5/BWNYiMGaGC86oHDtQvClGRSGq4loYrasZbftnayqiRQgIeSAyigtxDIqtYJUxZo3Nv2xh1HOYng/BDPu3NJlMqFsDj/o7eaVaQ3umDQ+fbXwEizuQ21LgXGK4RfcLA0Z9GlIOFQ8E9qLRsdzPbgz0SIVKD6DJ+9N4YFj31hhoeOZMkAQfkfZqYsiYixLN/UBObCf9XXK+mQPNkG2J1PRJhVzgQWlNX2tO1yrSRDlFir7W+/ekz4TzaE5s2xiK9tu7M4wZkSN1g8ZPUldDO7PLQzsJcdRbyrPM/5WHG+sSvsmA/aUYvdZiJZsc65qNJLfFGZgvzJVUq13XKr+YgMDqDxrU+faPr3wxRpDgwo4Z/C9dtJ/fjtxduOO2gecU+p/Qi1eVEMaTAaIuqt2zeaYknPwAZFvk2aRy8P84T+KcY1/EGhBXHsasKXAxrEQUb4wtpplw2zivNypN9ttzwLzYru8WLBTh2XVrG9ncM8GwF1eRJ4rV71MAB4X1RJ6mlFrcwUOzx9LHd7MptF3Kz8HTybWA6SALilBjJXinrnIC1w7YsmI3ZFt+1Ww+baQaMPlrJ5mcBjcLxoR8wnrUThrBE9XrNFIK6+maVO/JMRAqgPfJ7gRacw5574KIb3V7Wt3OygMeG/2VVnKG9VKCY8gSgFwGZGWywq9bOBnKFlCgNN nwSmpl0M nnk4Idzyn846UA53m+ZizWUDPAmRr4aNyc/Ebg4Axt8vr17jCxjQWWU5jfSons62h9XrDw4lb+qw1hpcTPmUc/U/cAzSrVg1/1ZoArt0cV3k6CTPvsDej9SAqs7KcirdhtWGrHllYsAk7wNXeDv26y7bWzApZJaAZT42VhVN+g5vaqqvRlo0liakyD70+hSUq8tWPUORzZVcFRAGTt04Gnr2PJPs9oNiaLoIDWI9GgzOYkaY= 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: List-Subscribe: List-Unsubscribe: On Wed, Apr 02, 2025 at 02:19:35PM +0200, Peter Zijlstra wrote: > On Wed, Apr 02, 2025 at 01:32:51PM +0300, Andy Shevchenko wrote: > > On Tue, Apr 01, 2025 at 03:44:08PM +0200, 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. > > The compiler *should* complain. But neither GCC nor clang actually > appear to warn in this case. > > I don't think we should be making dodgy macros like you propose to work > around this compiler deficiency. Instead I would argue we ought to get > both compilers fixed asap, and then none of this will be needed. Ah, I think the problem is that the cleanup function takes a pointer to the object, and pointers to uninitialized values are generally considered okay. The compilers would have to explicitly disallow this for the cleanup functions.