From patchwork Fri Feb 16 20:31:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 13560651 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 21260C48295 for ; Fri, 16 Feb 2024 20:34:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=QthKnlPtqM/IxkX2qXUvO7vxbTJVVNMilAWi4nnT+q8=; b=fZPw7d5wfAQB8W WDUyKDFVkxnW1VPElt47Lc/UroArWyHOwiLYO9pMB6KE0XmXzQIwvEljc7dWRsknn2SqBBAvoQVU7 KY1ligLRaXm6+7IbOqI7p0Tg794LIjBa1IEt4ivaYBjrl6aW9zJZWiRXV7csFESuOvJqaixf8nzd7 gMcimuEhPUGVYkmKD4SGe3KMzl78/xK9hS7QN7hAOz8xrLvjcCQVMcqHAnC4ZKMA+b44tjSiuGW1G yEqTIiGQgHf396YxWPq2+8sumWg0+c6gc9ViEWJlol1oNZQC6MgFoYyXYSeY8/xpSfzaLFdZwwomy wzPWg2d1E4ZEsc/TAMjg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rb4uC-00000003e13-1XpA; Fri, 16 Feb 2024 20:33:56 +0000 Received: from mail-lj1-x22d.google.com ([2a00:1450:4864:20::22d]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rb4ti-00000003dkc-3nAm for linux-arm-kernel@lists.infradead.org; Fri, 16 Feb 2024 20:33:32 +0000 Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2d11d17dddeso16105471fa.3 for ; Fri, 16 Feb 2024 12:33:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1708115592; x=1708720392; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Xzau/i70OXFn0nQv+xGSpdZtho+tWjzX+6Y/L0CiXOM=; b=iVH2tsTUMDme2EoSJpb+QONn+aANULTy0gMbstH2YgFktZHQdDe5f0r7bLvyUe/qA1 bbNR1BqIPNQefDwpJrMY0iNXYCGgfxEZkLGISma689VwdtyPInTDE7jxUBwW3ytbLAuG N6buYMCBSuRzAE8X0gx7uPhFkKag3OpnDoBMShKulbNffnKrhsepEZeQYMh24GLFrKJv XaFq/zbXe5BNJCMM12R94FNXvK3wax9ifVWt/K5MTQ0YLddtBZdOtBS+HrgntbapDupR OswcP6WuaJJziv1Zn5ticcAQtUx2mBci2OodTV42s8o5gz8W73ZhQufqovsVhGcSBHjx Aj9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708115592; x=1708720392; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xzau/i70OXFn0nQv+xGSpdZtho+tWjzX+6Y/L0CiXOM=; b=vrVO7RcHNIiPvlNDDwlMg7PcZ6xHlFCvkwmEDczlPxaqRFfjOBojzHiXe3QVBJDnF6 xd0GZnJpR3eYIBQLgLKQAM0HcKTm5NfUY/R69Hb27aOzQHVxB3GvTfTHwmCkcWvhzUV0 9W7CqxZvm1o7q22BODfeKp2YrUFuUEVLnhVSGFAk6LNA0m0ooU67/FEVcixHK+HQjF09 ecTDk5WbYeoNKslJOdluACCWQ/59u4IrMrlzo0BFJxgOeHJ3hlGNXGcXHRlmI3A7ZjLk 2365WGDU0D1DR473l1Ikq+wB/lWTMWUN+cANb0esLWeTVZ9GewkAygvfNzteuRNoTR25 ltiQ== X-Forwarded-Encrypted: i=1; AJvYcCXP8tEBzfFMhz9lYYPanYoKbFuKm4S4MTJLJcBTAs+P/oq+7jSjUCeajKsJI4ij5PAjdpSVD8fXhpt1mwK3O5Ku8aGcQ6hAllUf+P0d/aoSI54/ebg= X-Gm-Message-State: AOJu0YzWjzg+tebHbLg1shav1R/Ei2i01ayC+vqNR30YsD1ao62gb6EP dshqradl8tf4XTdZIFScjbTvJ3zUzkAIb/4lEKMkDq4zeDzMLG9qIFft2zOZ3Ps= X-Google-Smtp-Source: AGHT+IHDXA1CmYyMRhXqVjoGiSoJb0Qo9lCMPdos1FiMsILNtLiMwId05Qkpwu2DiA7OHYVJNW3yMg== X-Received: by 2002:a05:651c:b0f:b0:2d0:dfa7:fa1a with SMTP id b15-20020a05651c0b0f00b002d0dfa7fa1amr4944359ljr.0.1708115592326; Fri, 16 Feb 2024 12:33:12 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:7758:12d:16:5f19]) by smtp.gmail.com with ESMTPSA id m5-20020a05600c4f4500b0041253d0acd6sm1420528wmq.47.2024.02.16.12.33.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 12:33:11 -0800 (PST) From: Bartosz Golaszewski To: Marcel Holtmann , Luiz Augusto von Dentz , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Kalle Valo , Bjorn Andersson , Konrad Dybcio , Liam Girdwood , Mark Brown , Catalin Marinas , Will Deacon , Bjorn Helgaas , Bartosz Golaszewski , Saravana Kannan , Geert Uytterhoeven , Arnd Bergmann , Neil Armstrong , Marek Szyprowski , Alex Elder , Srini Kandagatla , Greg Kroah-Hartman , Abel Vesa , Manivannan Sadhasivam , Lukas Wunner , Dmitry Baryshkov Cc: linux-bluetooth@vger.kernel.org, netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-wireless@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-pci@vger.kernel.org, linux-pm@vger.kernel.org, Jonathan Cameron , Bartosz Golaszewski Subject: [PATCH v5 01/18] of: Add cleanup.h based auto release via __free(device_node) markings. Date: Fri, 16 Feb 2024 21:31:58 +0100 Message-Id: <20240216203215.40870-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240216203215.40870-1-brgl@bgdev.pl> References: <20240216203215.40870-1-brgl@bgdev.pl> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240216_123327_280382_170A6BE2 X-CRM114-Status: GOOD ( 18.16 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 Signed-off-by: Bartosz Golaszewski --- include/linux/of.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/linux/of.h b/include/linux/of.h index 331e05918f11..8df240214db1 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;