Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp841088rbb; Sun, 25 Feb 2024 06:28:00 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW1lGLo8H9Nm1YOPcZNj5CZne5toeW1WG10P9ps633kToRpDZWYOrRtWierIKa486pL1H8gKpewqtwGkcaoKcrG7eMT3fDTcWgOf1MwUw== X-Google-Smtp-Source: AGHT+IG6fceE5332txoOG8wR+1exZ7JdnKmWgehI1sH+irid9h6SelkM1jg5FaWMEEkkK6eGZPyF X-Received: by 2002:a17:903:25c9:b0:1db:cbff:f7d6 with SMTP id jc9-20020a17090325c900b001dbcbfff7d6mr3311865plb.44.1708871279915; Sun, 25 Feb 2024 06:27:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708871279; cv=pass; d=google.com; s=arc-20160816; b=WHgd/yMKI8/gIfqQnE/RzOK260qXY/bJzGfoHQ0DvZux7AxudEdRUR/RMgMRc0A6K9 pfNineZf887Vuv8feO7G68qGPsAKCfOAVePbu2W3eun7tTb+uRSMNzKocZByKEaDGGUS timGWCuW1tTi0K8YFxIUJZc83YAk22Fd2P7jnWmOyUxKGvCX/hz66JtjjH9tPiGoFDF4 U+KlmvXEdoVgwJEZDgdQ3+ckiIU1BrhhJYnAsz1BNFFuTzvxbGmWtG1M3Ib5xBLYpNjY YsodCKMk7oj7vBLATF9CSbA382QKHGzmFxQ9Cifv/IrFUUUOWjAnMGPLfEudXLcPLcs0 pCTw== 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=F73PukQcjePdt0NFnjd8avHPgvRLhun90mCfXwZ4nKo=; fh=PiZ5V/4sGLDov5cF6A5wY5iqIfZAXRefEy8l5uZe2FU=; b=IejqXyLNfaZVVBrjcwuqOmmpdRqmFGjDjTItOYXQDRlsS+aEiDyi2S9agNM+sPZJUQ xT10X5pV8JjVjeFt96mjrgO7rW8NGt33Ucv6+dT0QekcCjQmZUPYtnmR667uulkgmxsX A45xJMwAaJH02JNlw2pXjl6jTr3KmYxX82HeDoyXQ+PVWT9TrjEnKLtB8rW/YWhE268J ievj7OxbF7N5+rW9nN0VbX/B3vejcYFmdlilVy5BbgVsh6M0pc2l/FGTCeikwGFFOFOJ gugYn+pbOR1d1glZTE9bilJUZKivhv7LTc5sSZIJUM/gCtOL1z2fKOmPDCUtTjb2boob W5xw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EIbuUKf4; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-80098-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80098-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id x6-20020a170902ec8600b001dc4bba386esi2200969plg.229.2024.02.25.06.27.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 06:27:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80098-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EIbuUKf4; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-80098-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80098-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 9A4BA28175D for ; Sun, 25 Feb 2024 14:27:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 15D6814A83; Sun, 25 Feb 2024 14:27:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EIbuUKf4" 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 3576214280; Sun, 25 Feb 2024 14:27:37 +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=1708871258; cv=none; b=lozC34kN4ME3mu4ouBayGd626KnOKvMHV4Fa0TO6pt9J9KSiQpMF6LUK/6CZa+6usaDdbrE58kHngL04QJ5Lfc4YpSfW+YAVSQTE8Z54VXhMkbb5VuBoi46WRX99s3cJp1Mvyipi86e0FF7jOEqwcULPV19wdBcqjTG1RfgAu4A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708871258; c=relaxed/simple; bh=HAT98xnSwLDAnpvodaMTdqxT64M67j1g0X6shQNezLg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZF7olYEI7dZiRzbO+QRdpI38BN+HZAemE+yOgfgHpxXcLaC2P05xst2MD8ed4VTYC5sNuJSBoxF3bCv6A9V3ZpUxVsSk4AQdrjQYFUfSpDlLS6eNRkvmyuOZmIDsrG66wc61hubUO2vng1l0FcZZyuO9wmB3ewV7/7oiGjXQsuY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EIbuUKf4; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 53840C43390; Sun, 25 Feb 2024 14:27:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708871257; bh=HAT98xnSwLDAnpvodaMTdqxT64M67j1g0X6shQNezLg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EIbuUKf4y/pyYsjG9hm5YQtRuJ+jRGEDKz0LyJSGSgCYl7GMGtEKsNgSvlyhPE7VT gjg1AGMvcM9tkJTDSjtJAaRWLUDHJe7+eRIYtGe1/Ba3DzDztt0FUjxFuEnGDzHBRo ckofUhmRYZQY9angGKOPvspLMtrocVDHCl4KRa9sojUOn/oYbzZYRp4JoIvPzmhTR8 E2Z5Rwfu+oQi26eKAo2/Iw1g/jI/MBDZfnWpFBJ3nt1LExcck6xa3oTZGSBPuAgfgb UQsCsHrT1UysEFxyvUxOIZUtZo5t/vcli98lJY22HTRLk8CPVt2cv3++RInceSR4S2 jFYjQ75vN5zsQ== From: Jonathan Cameron To: devicetree@vger.kernel.org, linux-iio@vger.kernel.org, Rob Herring , Frank Rowand , linux-kernel@vger.kernel.org, Julia Lawall Cc: Peter Zijlstra , Andy Shevchenko , Greg Kroah-Hartman , marek.vasut@gmail.com, Jonathan Cameron Subject: [RESEND PATCH v2 1/4] of: Add cleanup.h based auto release via __free(device_node) markings. Date: Sun, 25 Feb 2024 14:27:11 +0000 Message-ID: <20240225142714.286440-2-jic23@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240225142714.286440-1-jic23@kernel.org> References: <20240225142714.286440-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.44.0