Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2067371rdb; Sun, 11 Feb 2024 09:43:36 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU7O2izgW1GvNN4JnFCrNEf+ZetzpI2XtoLJHPpgucjBjML11kIrDw5SmKj3hTpzVU1fhuZnwCPd9X0uWEApgAXppdEccGAgiQhO+BjDQ== X-Google-Smtp-Source: AGHT+IEwt93hea503UvBVEfGiX7EbZY96zDUWJ133jCO2umCTp1+ysndzGKc69l6WOd3MtIi2lGJ X-Received: by 2002:a0c:e38c:0:b0:684:b789:5508 with SMTP id a12-20020a0ce38c000000b00684b7895508mr4801125qvl.31.1707673416072; Sun, 11 Feb 2024 09:43:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707673416; cv=pass; d=google.com; s=arc-20160816; b=eL0RK+deehTO0S8v+2KqeDmuAZGt+eFwUbrf6DByVinhSWo5Ewa0yRLp8tOxK0Y3J8 wKwjIE/kgMsBFPA5Dzo4yizfrEpgpywpBRblRwRZeJ9/3qmxS636tYQaqXC2iXEfA0ZY 79xLTje6ocxdcvQUzIrH+RJkhZEogk73OohyZpi2rxdYrLxcFBvZDgjtWin0TeS8/7NM jbEHPizZWIap4fALPThACs8PF52Qc/LOky4MJeANH83WT18xliOGLYzP0ERg/eEyS+KM dCSMOi8dFCoWYkMysmKWosQZsiDAtc6kbWHaXdBWR5A+RnOD2DkWxa10EKi1LwuCkPjy gJAA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=nQsj22NPxmBMr5ip7C9eNpQVJYUc91r8FEatPtU1zrk=; fh=8Nld1MX/4hDrmb+wapADPEsdJbmBlAiO8AE20L3ESps=; b=oqCtL8dhiFd9BNfBx9WyWlpZ2ibPrndOG0ONAk8aGwwbx9RAtxPWCSNJ7N5Fl3XVd9 syoEW2TWTjV5zAKXR+GhBYTZr378mn4ccxNmCmu81XAIcC3VE0C9WGgVGF6yD5MNn+9N Iwm38gDxVB/KQRy5SfIoya7/6v5hZ4imMCHBz3zQstaCWnXOKhE5Kk0kN9cxMyUoP00m YPr0Z/hagKCz13oY/n7ZQK+cQwM6XoNCUgI2V28Ysz2+UnG2LpxgH/yLAMX8z0BvNGqj zTkNpwrj6bktXm0UQQweWyFeEo8GxvV55Zke6FI4vpw1cEcx1nqCLIfEB7s4lZvrXB69 seLQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qUqMQoWk; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-60886-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60886-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCWFGQHfBP8Z1EoDIeZw+fErK6j7bmaxhivBAbosOccGGH9a4Mh3uYnj1fLSfDGuCYk0tjeBxSbvYlHazi8tB24K7i3tNsNIVxIlQTmzwA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 8-20020a0562140d4800b00683699d4df8si6803875qvr.139.2024.02.11.09.43.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Feb 2024 09:43:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60886-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qUqMQoWk; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-60886-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60886-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BC7CF1C20FCF for ; Sun, 11 Feb 2024 17:43:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 09AD05D49A; Sun, 11 Feb 2024 17:43:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qUqMQoWk" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 2DD102837D; Sun, 11 Feb 2024 17:43:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707673393; cv=none; b=SjVrsHH/2FMap/JwV/aWOrxMuWsq5Xrpaib4kRpudmCFnZ2JD+ooM3Obnf21JfWI3zCHyI2FP7rPxonTjSh6lZSB9jZtHdKsAXS5TmTiLbm4sSyakl5QTlwbD68sMpJUNfrg9xFnVjfGd838I1plck1BKFkoVgaQ3isjVPUQSak= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707673393; c=relaxed/simple; bh=FL7OGytfdxTg1+Il2BlWOFoikr/LWTCTYjPks2DpfGI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=H1LDiu8ygANN3U2cDpShQbIlPZAAsL5UydtHChrN8icqC6NdIR1kxB6CRAvCjJMsmah734MhzPY+g56APKZt9fwMV7Y1hr3qgXGwW/ekuyk7G5eZC+teGlhh9ktJek0k4oXDVyb2w1BgtuDgIIubacltHKinz2O87Ad4Br1ECGs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qUqMQoWk; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7586BC43390; Sun, 11 Feb 2024 17:43:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707673392; bh=FL7OGytfdxTg1+Il2BlWOFoikr/LWTCTYjPks2DpfGI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qUqMQoWk1tXkagwZnH8JtWCfo+skthBMefbkiM+Vfu4g8PAYIoVqT7XFHAHWFPWuf OXFcvLIbfoxckJ3Bowqs/sx/kC5iM9Sds9nYopT+bQ4o6ueB3YpwsH4oV5aHi5S76h rA/cChOxg6BuRiDe/W0FE+PEcV9k2DpFtMXiRUwliV3jVuYu7g9QVoO07h/HrMDb8C mwPGc1bPXyG3sXjdpqEo3CjzH+15pwe2VDwHtPKOW7Xkqn0G7o2b7RhQCQiKFYRmgp H5F/XrokeFMMrDTzdBorgj280D9v+zZuyBFX0IY1gPMc/PE9r3cgf8DUXSoeMGsoaL 66JjV23yVCS+A== From: Jonathan Cameron To: linux-iio@vger.kernel.org, Rob Herring , Frank Rowand , linux-kernel@vger.kernel.org, Julia Lawall Cc: Peter Zijlstra , Nicolas Palix , Sumera Priyadarsini , "Rafael J . Wysocki" , Len Brown , linux-acpi@vger.kernel.org, Andy Shevchenko , Greg Kroah-Hartman , =?UTF-8?q?Nuno=20S=C3=A1?= , Jonathan Cameron Subject: [PATCH 1/8] of: Add cleanup.h based auto release via __free(device_node) markings. Date: Sun, 11 Feb 2024 17:42:29 +0000 Message-ID: <20240211174237.182947-2-jic23@kernel.org> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240211174237.182947-1-jic23@kernel.org> References: <20240211174237.182947-1-jic23@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Jonathan Cameron The recent addition of scope based cleanup support to the kernel provides a convenient tool to reduce the chances of leaking reference counts where of_node_put() should have been called in an error path. This enables struct device_node *child __free(device_node) = NULL; for_each_child_of_node(np, child) { if (test) return test; } with no need for a manual call of of_node_put(). A following patch will reduce the scope of the child variable to the for loop, to avoid an issues with ordering of autocleanup, and make it obvious when this assigned a non NULL value. In this simple example the gains are small but there are some very complex error handling cases buried in these loops that will be greatly simplified by enabling early returns with out the need for this manual of_node_put() call. Note that there are coccinelle checks in scripts/coccinelle/iterators/for_each_child.cocci to detect a failure to call of_node_put(). This new approach does not cause false positives. Longer term we may want to add scripting to check this new approach is done correctly with no double of_node_put() calls being introduced due to the auto cleanup. It may also be useful to script finding places this new approach is useful. Signed-off-by: Jonathan Cameron --- include/linux/of.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/linux/of.h b/include/linux/of.h index 6a9ddf20e79a..50e882ee91da 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -13,6 +13,7 @@ */ #include #include +#include #include #include #include @@ -134,6 +135,7 @@ static inline struct device_node *of_node_get(struct device_node *node) } static inline void of_node_put(struct device_node *node) { } #endif /* !CONFIG_OF_DYNAMIC */ +DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T)) /* Pointer for first entry in chain of all nodes. */ extern struct device_node *of_root; -- 2.43.1