From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from n169-112.mail.139.com (n169-112.mail.139.com [120.232.169.112]) (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 67D3D363095; Tue, 24 Mar 2026 08:21:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=120.232.169.112 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774340524; cv=none; b=AEhZ7nQbyX4xbN/pVC76XPJEPEBjfP7LJH3t7u5ra+Xx+d+YexhU5UnGQjscuePOqvZM620O8d41vdZytsyz1VnaB3UjWpbNsPwSK8lo44iNmKVR+2E2a3CN1gd9ptc8a2+9O1cKCBc7NzxZIEraryaEtcD8zqrHx9bCNt18hkc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774340524; c=relaxed/simple; bh=RresJ9cJ7rbHNFImXUvswdjykY8j00URXG6gtoRG/Dw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Rnh0zRBwceDMHVqoDMA4JIWnwtrvGUc3d5gxZyYnmpq0aD0A0vc3Zoe8q3wD7X3tQJGolHcw8l+3eox5er6D5C8i3zvpmB7fUaW2N1YFa+ZYjMS1zwal6t7PE+YwJ6u1PrQPXDXGnLr13D4wzASRmeM8QALy1QXEUWFadCaeQOQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=139.com; spf=pass smtp.mailfrom=139.com; dkim=pass (1024-bit key) header.d=139.com header.i=@139.com header.b=YjcVDFN5; arc=none smtp.client-ip=120.232.169.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=139.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=139.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=139.com header.i=@139.com header.b="YjcVDFN5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=139.com; s=dkim; l=0; h=from:subject:message-id:to:cc:mime-version; bh=47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=; b=YjcVDFN5z5uzo/k8DE4g7Frzr5isbsDD8zkHT+hB70ld07OWo/9FvjVbMD4hytSDeQhy6ua/v2v3f bX3InMIs3vkcnyD5Hqp+pOhRsfEOIq3CqqK87ZvqDdgjprc96jDSFZLAbA+7HrsY0cI4P+WaEISUY7 /l54smaWbwV+Dfts= X-RM-TagInfo: emlType=0 X-RM-SPAM: X-RM-SPAM-FLAG:00000000 Received:from Ubuntu24 (unknown[2409:8A00:DD3:9760:10A3:2606:149F:AFFC]) by rmsmtp-lg-appmail-19-12022 (RichMail) with SMTP id 2ef669c249927e1-01800; Tue, 24 Mar 2026 16:21:45 +0800 (CST) X-RM-TRANSID:2ef669c249927e1-01800 From: Bin Lan To: gregkh@linuxfoundation.org, stable@vger.kernel.org Cc: linux-pm@vger.kernel.org, Jonathan Cameron , Rob Herring , Bin Lan Subject: [PATCH 5.15.y 1/2] of: Add cleanup.h based auto release via __free(device_node) markings Date: Tue, 24 Mar 2026 16:21:12 +0800 Message-ID: <20260324082113.6275-1-lanbincn@139.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Jonathan Cameron [ Upstream commit 9448e55d032d99af8e23487f51a542d51b2f1a48 ] 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 Reviewed-by: Rob Herring Link: https://lore.kernel.org/r/20240225142714.286440-2-jic23@kernel.org Signed-off-by: Rob Herring Signed-off-by: Bin Lan --- include/linux/of.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/linux/of.h b/include/linux/of.h index 29f657101f4f..3c840c487995 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -13,6 +13,7 @@ */ #include #include +#include #include #include #include @@ -128,6 +129,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.0