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 1C9B3C28B20 for ; Wed, 2 Apr 2025 12:58:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 507DC280005; Wed, 2 Apr 2025 08:58:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 490DA280004; Wed, 2 Apr 2025 08:58:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 309EE280005; Wed, 2 Apr 2025 08:58:03 -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 0F7F1280004 for ; Wed, 2 Apr 2025 08:58:03 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 2580A12187A for ; Wed, 2 Apr 2025 12:58:03 +0000 (UTC) X-FDA: 83289106446.04.837D1C7 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) by imf13.hostedemail.com (Postfix) with ESMTP id B25BB20003 for ; Wed, 2 Apr 2025 12:58:00 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=RBWFI2Ia; spf=none (imf13.hostedemail.com: domain of andriy.shevchenko@linux.intel.com has no SPF policy when checking 198.175.65.14) smtp.mailfrom=andriy.shevchenko@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743598681; 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=OSm70u8tMLroCFAhMGXJydB5zmnt/ZsQN4exXcnN/Aw=; b=LWMU8xSeRTqMSgp/jK5zlVOdTLMvnapYxLqBmfKDFOyZClztEhzHwH8vOBCkQt5DbiAy/E Ii3XSOu7K3PZpK2AI4sx3A9Pcvcqv9Yu2FPXZ0Ff2gXo8afh7yaU6nxuFyvE0dYSC59C/Q NO+KXe5BaUL/8HAl995Ic2GEXjElhH0= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=RBWFI2Ia; spf=none (imf13.hostedemail.com: domain of andriy.shevchenko@linux.intel.com has no SPF policy when checking 198.175.65.14) smtp.mailfrom=andriy.shevchenko@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743598681; a=rsa-sha256; cv=none; b=ytCm2kzSGzmbnH+yIWxOHXMPNDqwU/qjz5HPAW0cfg0nUT2y+wQwYmHHnh8pOjBanS4u/o ZBtPySIpURVzSuteLMOrQAqB74dFpbzwITp5LLQUW3h5s3yKW3EfnCiTBn03rTuWcs9HwS vXCwcYk3aG8bPDD8rvuChgIMjTcaf2o= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1743598681; x=1775134681; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=9PaKlNBLrieA3abOkdoYAAKdlkNEcWYYrdczTHWO80Q=; b=RBWFI2IaEIoM1Ev2bjpwit5xv/YMMR4RE9deuAn52otu9xizI2faecSn RJes0cXddOfVQXrdlS2ADswaMvy/944NgzYFBGw1zokNzhsrycRhAoQFw lLeQFwLQf7fBqA7d9uv27jOe3m3PZ+/+TQb9nZKMTmP3LmHRzZ3UGbhLD L7B9EnFrQPW+6Toj1uLjeSZIChfYqjauBzWd728kQzcQ2euO/FImU09Km MCI60QJpfggeSBSvQCRfyqXxzMcJ249nUVW7ywR5DToVo6XF4RDP0bSB/ OkH1/vN6sbwfm1FXIGZDyoTCpNrLjKeFsBzFbIS7q7LnOcwus5CYqHPr6 A==; X-CSE-ConnectionGUID: HZCRzjJWSNqT+8PryqURng== X-CSE-MsgGUID: jabSED/6RCO7co3w28YdcA== X-IronPort-AV: E=McAfee;i="6700,10204,11392"; a="48750392" X-IronPort-AV: E=Sophos;i="6.15,182,1739865600"; d="scan'208";a="48750392" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Apr 2025 05:58:00 -0700 X-CSE-ConnectionGUID: /xJdOPYHT0W4RBku5oc+fw== X-CSE-MsgGUID: md2FkTnQT4etxJfUUnTQ6Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,182,1739865600"; d="scan'208";a="149880473" Received: from smile.fi.intel.com ([10.237.72.58]) by fmviesa002.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Apr 2025 05:57:57 -0700 Received: from andy by smile.fi.intel.com with local (Exim 4.98.2) (envelope-from ) id 1tzxfG-00000008Tqt-2q8U; Wed, 02 Apr 2025 15:57:54 +0300 Date: Wed, 2 Apr 2025 15:57:54 +0300 From: Andy Shevchenko To: Peter Zijlstra 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: References: <20250401134408.37312-1-przemyslaw.kitszel@intel.com> <20250402121935.GJ25239@noisy.programming.kicks-ass.net> <20250402122224.GB25719@noisy.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250402122224.GB25719@noisy.programming.kicks-ass.net> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: B25BB20003 X-Stat-Signature: fuz31jnfqtu67fddq5a9q1cq785aekfd X-HE-Tag: 1743598680-669533 X-HE-Meta: U2FsdGVkX1+atN5/9d/bZRp3dmnYtXS4ONEebmORUKD7OSPlhPH8xDBTc8jBOpOtJwXXq87ub3Jsw6iWM1Qy7om0GQB4732uSX1sH+Ky5Ywk7z1DDTuiFt69UM/eO8s/OwI2eW6EwQmsJm4mlyW9OdmE1JKtYLUN94hAZhh83JcbLAG+IxVkOrDYrIITKobRN2Dlm3xeeqzu+bFQhx4MHGnZEpAOuLRnwQm4tqc14dqjTqxZ4tHZu91Jmc5erHOg5GTCanaegsZFFUm8lKSPceoNLi+Wqrco0XyHbrm0GcJ06pshKuEX2k0k1Hr/K8PXBFApLgGlikH84g2oVodBskOLEdI+RQgYqrs5XANgEVZjhsKgkIyvEWRKsQKwCbhCbxkqiEmLnk6WEXO4O9cBZXmr6F4MPN7wyUToQLrUuDkMO5W4cRdFP/LBpawxv0CaqZF9wdF/htKz2h/Ion7CGdOjKTcHbSoBFosS8jXXqPwm8AVeQdHa0rSyBieFOUN2lV6xQRAmjeISdSHR8ZCRuxVGO6syMGFGrkDYQOMZq4Tl6ocn9YcOdo1gg1vUyTnMLYsYeYTNYE40yMGX7CMxZ/MfAe8s/79rMsYucjXWRE6PCQfGCilOWW7LFY0W0eQPL7qx13A8JIf79DE+r3VvH86GSBREguAu0mUksuP9gWfpJCuSDkETVuYk17FkgTRcLNFyn3JF67coBsiZbHG4rx4v7IvB8Gv3Wt2rZGaEEmKC0O+79I0DlAbnrMO5RtlWI8h9uxBd0sNQES1yfH69A0ZcmqvJ3x/W8UoMeRNmm25xNfQcKTY/CBPZkkzANOaxVrHp3DW3YSqZk/xLJd2sH5c9efZWhgOKGo6Ip0AduVvMEDgq4btzFHy6BCWOB/GLcD1lciWN76SS9xDq2u/1QO4JBcQVg1mZTawBlWUJtlFk+mj3ju80BYVOiBujWiBWe+Z+yX2/8fnDuo87ySB R5roFfHI okQaDfO74UNCpDvrvQIR8wcOzPJbgbRCWlo59yoHvlClDOD7s1Xfj8iBQouSexMhEJYmdFgI4QdZ7VrZ7tRrsy78tDQwNoJbvUSRryTzCrcjr+9EbD0feco1S2+UEWKpBrL/iOJyqoFabmLetMbeu0RaRdQOJBwB7KcxD3kqUBsD6U62g6Ox66XvRzHekslkqiA972ZkWikhD1VdNXerhbDCWMfl5Lamridhc5m73xIZnM40B/C0FY3K4y7cFx+p0EmtD6l3YBAJVTXQ= 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:22:24PM +0200, Peter Zijlstra wrote: > 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. Hmm... What I have heard is that the cleanup is basically a port of C++ destructor code to C, and it might be related to the virtual functions that are may be absent for the basic classes. But not an expert here, just speculating based on my poor knowledge of C++. -- With Best Regards, Andy Shevchenko