From patchwork Mon Mar 17 13:29:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 14019299 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6BE622759C; Mon, 17 Mar 2025 13:29:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218167; cv=none; b=Q/4w7KjC3vsXBxv+RZAsGynqIW5QLkmpIl41ooksLmjKcStecYK+N76vjLUHN2SAEviTIHp0rpG1u97Iv9VmDH5Uy0JN1M+vTUJiSaGFjtHu2O6tOB05ppWZalKImDGwzkN65sCjH6uU+kkSE27lDHTYfQfjOcuanBU9MAyJrj4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218167; c=relaxed/simple; bh=ho8Pzt9rnRzxEycPk61Hi2KRQSUXlk2Hs9Tw9uulgDA=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=H7d/Eev2HxluH/KiTO7bnDu+kpAwdYDpdD/EUi8h6TruQLdVrBy/JmIpAQqQsipTboMMHDi5SpESWvPrUZjstvKc7pt0LRffIWvmGHVLKD1omYkhYZyCvC4oMiJHXNJ9FYpp9/G4eND/XVgWHYY628DU9AU6/RqnPCX56ztBO2Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ikfuYWB2; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=9YalfP8x; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ikfuYWB2"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="9YalfP8x" Message-ID: <20250317092945.764490535@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742218163; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=dp0eYeKfrNlzvU3/WMfnQjPzD6qavekPLwUv4OZ2OOs=; b=ikfuYWB22wmHGAZ1ZCYxuHRBkfaFcMntu8Y2Xs5ZKHP20JMm5tpDEfW6mSqnqvIO7/Ajcd T6v6qi7q6R4ILWkgkDOA9Euq/2Zb29uV0VXgMO3S6EOiTvIiTE+3fMRc2bW6TgCzStokN0 2qNbE69vg0xjvKiRyG8sm76ROcQAiH7DcvK+2xFh2EUbUaH3RmjIZEbbqrki7c8bXpVgqd r29lTkocOA2rivUwQDjCvDUL23d+Y5cLsrbZNqF9pkBjMX/+mz0WWQ4SK1qFQ2nxUnM/Od +UNYZUNZd4i2O78LP2ilCXUXc58YfEFyuX8tC6NzyGzyPdar813GtKp6JAeFXA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742218163; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=dp0eYeKfrNlzvU3/WMfnQjPzD6qavekPLwUv4OZ2OOs=; b=9YalfP8xBbKHQiBQ+hYehlm/nMIPvpPeNa78JCueUr9eWpmBcupHLF1l2sIP1/dDDyvv38 onqG0szZCKhD09Cg== From: Thomas Gleixner To: LKML Cc: Marc Zyngier , Peter Zijlstra , Jonathan Cameron , Nishanth Menon , Dhruva Gole , Tero Kristo , Santosh Shilimkar , Logan Gunthorpe , Dave Jiang , Jon Mason , Allen Hubbe , ntb@lists.linux.dev, Bjorn Helgaas , linux-pci@vger.kernel.org, Michael Kelley , Wei Liu , Haiyang Zhang , linux-hyperv@vger.kernel.org, Wei Huang , Manivannan Sadhasivam , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, Jonathan Cameron Subject: [patch V3 01/10] cleanup: Provide retain_ptr() References: <20250317092919.008573387@linutronix.de> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 17 Mar 2025 14:29:22 +0100 (CET) In cases where an allocation is consumed by another function, the allocation needs to be retained on success or freed on failure. The code pattern is usually: struct foo *f = kzalloc(sizeof(*f), GFP_KERNEL); struct bar *b; ,,, // Initialize f ... if (ret) goto free; ... bar = bar_create(f); if (!bar) { ret = -ENOMEM; goto free; } ... return 0; free: kfree(f); return ret; This prevents using __free(kfree) on @f because there is no canonical way to tell the cleanup code that the allocation should not be freed. Abusing no_free_ptr() by force ignoring the return value is not really a sensible option either. Provide an explicit macro retain_ptr(), which NULLs the cleanup pointer. That makes it easy to analyze and reason about. Signed-off-by: Thomas Gleixner Reviewed-by: Jonathan Cameron Cc: Peter Zijlstra --- include/linux/cleanup.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -216,6 +216,23 @@ const volatile void * __must_check_fn(co #define return_ptr(p) return no_free_ptr(p) +/* + * Only for situations where an allocation is handed in to another function + * and consumed by that function on success. + * + * struct foo *f __free(kfree) = kzalloc(sizeof(*f), GFP_KERNEL); + * + * setup(f); + * if (some_condition) + * return -EINVAL; + * .... + * ret = bar(f); + * if (!ret) + * retain_ptr(f); + * return ret; + */ +#define retain_ptr(p) \ + __get_and_null(p, NULL) /* * DEFINE_CLASS(name, type, exit, init, init_args...):