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 EFE7EC36014 for ; Wed, 2 Apr 2025 10:33:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0CB89280003; Wed, 2 Apr 2025 06:33:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 07C6E280001; Wed, 2 Apr 2025 06:33:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA8A7280003; Wed, 2 Apr 2025 06:33:18 -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 CAB21280001 for ; Wed, 2 Apr 2025 06:33:18 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CEEB71A16D0 for ; Wed, 2 Apr 2025 10:33:19 +0000 (UTC) X-FDA: 83288741718.07.9D402AA Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by imf25.hostedemail.com (Postfix) with ESMTP id D6669A000C for ; Wed, 2 Apr 2025 10:33:16 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="ZNR0e/if"; spf=none (imf25.hostedemail.com: domain of andriy.shevchenko@linux.intel.com has no SPF policy when checking 198.175.65.10) 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=1743589997; 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=eUvCGED7xu+Is2PLjm8dDuOWzRFSd+JT+RSSDf85Q6c=; b=uG69AeAHgpYYZjlxfBPFpz9mpcgXFPoyIYSvB8s3HrcLVWmmqQHTOU3gsFFn2VIe1b7dvy bbSgUO8Ttd/2LbIOTb1u8kYPFZkdasVh3d8Yer/s4k+74B8WtTy+rv7ud80H32syxHEw7o 15Uksne4CZrp44TAJFSbRgnyZF4tIKw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743589997; a=rsa-sha256; cv=none; b=mwNvJel1Ttuex7gyKKJKGyzvOFyus637/Y4YVN5p/SnFrNbg/OOPIzJb1jy3/R6YceDD+c 11C+OgIMRsOzhTG0hFZHh2G4sSKUWsjTg3ltyJIruQ2+XEXFg/rtcqrYBK+ayH3XRCcC/L 5zt4+6ZFnDeLJivEE+yteOIQZwnfa2s= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="ZNR0e/if"; spf=none (imf25.hostedemail.com: domain of andriy.shevchenko@linux.intel.com has no SPF policy when checking 198.175.65.10) smtp.mailfrom=andriy.shevchenko@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1743589997; x=1775125997; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=uzgVRHkC0/6oVwMh4YG7sDY5ZqY1Wc1ZMefrtNkrfxw=; b=ZNR0e/ifFoCRm1YcC0C88pk/IH+Dcn6W0RkU9/gTTAMazpwH9jGK/Y13 wg6swhSoOW0cgtHgTKG/QcIIjBFpD2EWpoN020W1DrKMh1ghAWMeT+Wjb MMYK+CZta4aywY8/Fbizu8kDhAeryyaD4GxWzkOfWpew43RL7t5FxkYFo B0sTQGMTfx+1hEBvbJQCZmxKZK9D7JgoifMQAFb0MM/JHzPMxizko/dON bxwSa9KsmscELSLPtmSEBm4ie5PS706Fl+rmYBANnFDYdzWlPnRi6d8+m RzQcQRHDDIegGuPeh63sROJwWsIaCuEzhuR52dpPlKBlvVvp6K+l/YnWD w==; X-CSE-ConnectionGUID: KqMpeGruTneTp3wbz3l2Ow== X-CSE-MsgGUID: QSMRxkDyS7eVgasYaq2qSw== X-IronPort-AV: E=McAfee;i="6700,10204,11391"; a="62346684" X-IronPort-AV: E=Sophos;i="6.14,182,1736841600"; d="scan'208";a="62346684" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Apr 2025 03:32:58 -0700 X-CSE-ConnectionGUID: jnKqawtdRCC7ytoiJ+6FvA== X-CSE-MsgGUID: 9Y43AS5zQbiCJvtQWGGu+A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.14,182,1736841600"; d="scan'208";a="126417413" Received: from smile.fi.intel.com ([10.237.72.58]) by orviesa009.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Apr 2025 03:32:55 -0700 Received: from andy by smile.fi.intel.com with local (Exim 4.98.2) (envelope-from ) id 1tzvOu-00000008RE3-0xzR; Wed, 02 Apr 2025 13:32:52 +0300 Date: Wed, 2 Apr 2025 13:32:51 +0300 From: Andy Shevchenko To: Przemek Kitszel Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, vbabka@suse.cz, torvalds@linux-foundation.org, peterz@infradead.org, intel-wired-lan@lists.osuosl.org, netdev@vger.kernel.org Subject: Re: [RFC] slab: introduce auto_kfree macro Message-ID: References: <20250401134408.37312-1-przemyslaw.kitszel@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250401134408.37312-1-przemyslaw.kitszel@intel.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo X-Stat-Signature: cxtuif39gnn1atotmcxyjknh4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US6JlqXN2NCdbYWSvVhF6+SpV232CeL+6FSiHxIByZ1Rga7QqPVsdM3ngs0JfceALoOVNIcp3TiDV9hv7dkFicFmvPljkfB30aAH1lEoWqYhhEv2wtkFcy3eJ8dEoEDmVkyWjVxDaVG0eEuYc3uXH47S+YjhablJPQlYfBHmNWuj4rZmeRAMQ+GfuMffpmseiz2Ioz8WL2kHAn16nsT+onv5F8YXgqzYx7SSRcNXevC0vKOrjCtQNrSzX/CR5n7C6eg= 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 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. > > 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. I understand the issue and the problem it solves, but... > +#define auto_kfree __free(kfree) = NULL ...I do not like this syntax at all (note, you forgot to show the result in the code how it will look like). What would be better in my opinion is to have it something like DEFINE_*() type, which will look more naturally in the current kernel codebase (as we have tons of DEFINE_FOO(). DEFINE_AUTO_KFREE_VAR(name, struct foo); with equivalent to struct foo *name __free(kfree) = NULL -- With Best Regards, Andy Shevchenko