From patchwork Sun Feb 25 14:27:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 206023 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1599694dyb; Sun, 25 Feb 2024 06:28:08 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX0VUilsmGvqS1L112HeaK+amWxUJYITnK07bi8nAVFcVOS8YsDuC31jt7w9qI25nxa4ZaLZJQ4kWX9yK9bUjDWmKEd3A== X-Google-Smtp-Source: AGHT+IHh0OHLzTSulshAljG5hi9gcw5owIzkbjLk4yVAevj833oIUliuUS/gf/p1X8xPLeublhGg X-Received: by 2002:a05:620a:570c:b0:787:7cf8:dcfa with SMTP id wi12-20020a05620a570c00b007877cf8dcfamr6096808qkn.76.1708871288415; Sun, 25 Feb 2024 06:28:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708871288; cv=pass; d=google.com; s=arc-20160816; b=HiXqbeApZfW31/x2/wJUnonLNvvpn0ZMdbkwzCZZceRUYFIdDgkUBuPl/7OAEt/Nnk WEDBzSUPIgzR5PdZl3CZ2bCjZujv1r/SGKy1HLF2Ed4nRAdidiHL5H4b+YDxXcy7SscH CFtMCjLWda6KwRbQR8jzqTvDXsbOBPqM05g7kEt182jJQSj8AufnwMMM5+9SdNRSYMMm m4IuTDVyLTU6L/AagEuMGGst9fNCtSwEuhFB+r+im9RUNQB6zE7ATRZcx4aYmqSZSa88 BU7jYf7jYaAkwa+R8dov0Cl8Qj6ZL/Zud52AUEa221xOq9ZCxUSGd6l3oZKQkctDOkyE 0H5g== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80098-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d17-20020a05620a137100b00787a0bed972si3027790qkl.145.2024.02.25.06.28.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 06:28:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80098-ouuuleilei=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=EIbuUKf4; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-80098-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80098-ouuuleilei=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 182071C20BD5 for ; Sun, 25 Feb 2024 14:28:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DDE6314AB7; Sun, 25 Feb 2024 14:27:40 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791881420240933517 X-GMAIL-MSGID: 1791881420240933517 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;