From patchwork Tue Apr 1 13:44:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Przemek Kitszel X-Patchwork-Id: 14034863 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 B79A4C36010 for ; Tue, 1 Apr 2025 13:50:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 71495280002; Tue, 1 Apr 2025 09:50:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C079280001; Tue, 1 Apr 2025 09:50:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 589DC280002; Tue, 1 Apr 2025 09:50:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 3CFF6280001 for ; Tue, 1 Apr 2025 09:50:40 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 198BAA9355 for ; Tue, 1 Apr 2025 13:50:40 +0000 (UTC) X-FDA: 83285610240.24.F4FBB05 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) by imf07.hostedemail.com (Postfix) with ESMTP id 5198C4000F for ; Tue, 1 Apr 2025 13:50:37 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=P6B7Jt3V; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf07.hostedemail.com: domain of przemyslaw.kitszel@intel.com designates 192.198.163.19 as permitted sender) smtp.mailfrom=przemyslaw.kitszel@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743515438; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=w0d3ejIB8ykp3itxWnlZMeX96K6W51JjpPdg3diXZpU=; b=0mgzHTVDugpFxVGUaX16N0qW8ytILELBs8vqPnlINDl5RpqhHMrIcBJ+heYbiJCklV++Jf rtQEeGsm7hqy4e4Gvc8OxHr8ALO6Kj2fJqWF5QU0ucUYwTShMwl130+5RewMXS4ObEjYu9 UrH2kb/thL1Dv0coYJMHORNM0IVX3wQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743515438; a=rsa-sha256; cv=none; b=HkCj/INEyuSWrVq7nFC3tCFgg4UQpmHjBYQC7jZH0sQfn3gBlCxFA5nMfXWC0RDq3uBErG YqNl0sKk2ze5kA8sysGOOUxufg7Luug7PSZ2vkOCBmqU4ixYUV9Z2zTlaeLLuuQZfQO+XL xsaCkAXI0N2fSz+U606YQomHzU7mwtc= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=P6B7Jt3V; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf07.hostedemail.com: domain of przemyslaw.kitszel@intel.com designates 192.198.163.19 as permitted sender) smtp.mailfrom=przemyslaw.kitszel@intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1743515437; x=1775051437; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=39dw4BZHdCa1g3Uare5eNbAKiAqjVx6gWMtRg8KXg6g=; b=P6B7Jt3VvS9KPznolE1NCkpd9yN5tB3VkvvAsrHsDPgTQFT2sWlT9/fm KQt1Uf75TRjA+k1rBVNxm6OfJKwZIGwIbun7BzU1Y3pou4w+SZaB2tQR5 ixSTnd77RQpxLrKwKAZc4qqwfwnh1kso7WjslaxzQ7IcNRvQnCuBZyhvu C8mPZ92/j2kbRdZH0h/tkCuecMNuXpF+a+aSB0DuNTVAHfHlindOpZu0y ft8P4D/4oqn5qPX1BUfar98ECjaRnt1K/4xCZjRzM2tlSUFw/zlnZk//g v0WD1VL6rL5vdh0GchFZhFfwdhZwJEDPe5qHRUVvHpXpip1NgS0dr/fAb A==; X-CSE-ConnectionGUID: PvvLtkQNSZir8oLd0jc8Ag== X-CSE-MsgGUID: kN++7fzSR7mDiWpgoNTnwA== X-IronPort-AV: E=McAfee;i="6700,10204,11391"; a="43987249" X-IronPort-AV: E=Sophos;i="6.14,293,1736841600"; d="scan'208";a="43987249" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Apr 2025 06:50:36 -0700 X-CSE-ConnectionGUID: WfeZqm49QkmEdayZ399bsw== X-CSE-MsgGUID: c2bFjzdNQQ2mnWKppKHtTQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.14,293,1736841600"; d="scan'208";a="157328790" Received: from irvmail002.ir.intel.com ([10.43.11.120]) by orviesa002.jf.intel.com with ESMTP; 01 Apr 2025 06:50:33 -0700 Received: from vecna.igk.intel.com (vecna.igk.intel.com [10.123.220.17]) by irvmail002.ir.intel.com (Postfix) with ESMTP id 1391432EDE; Tue, 1 Apr 2025 14:50:32 +0100 (IST) From: Przemek Kitszel To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, vbabka@suse.cz, torvalds@linux-foundation.org, peterz@infradead.org Cc: andriy.shevchenko@linux.intel.com, intel-wired-lan@lists.osuosl.org, netdev@vger.kernel.org, Przemek Kitszel Subject: [RFC] slab: introduce auto_kfree macro Date: Tue, 1 Apr 2025 15:44:08 +0200 Message-Id: <20250401134408.37312-1-przemyslaw.kitszel@intel.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5198C4000F X-Stat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iSvyCgHA5FRWZymwlT4oIrCHXX/u60g6ZmodDZvYsTSIYOXVIcVdSGlFxR/OPIRbsmL20lp38iYQhO1IcPlltKlsD43ruwDNpQZyQrXfznHOkmfMrcxL4915SG6p/5axPlbffu79/aIUw+2/S6gYcDJfEMIBX6Al5gWbqi4UD9AH4f71caSU305GE4EAtF89ieXLlVNLI 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: 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)) /**