Received: by 2002:a05:7208:3188:b0:7e:5202:c8b4 with SMTP id r8csp818613rbd; Fri, 23 Feb 2024 04:45:14 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW235kTHuysdY4FptEGSY67vGVPBDPxJZJ2utjYupYiBlTHZPnmfTecJJI0i5PnnT3RhyDRFVOKx+ZU4A9iYpN5IjsIft+tvW1s4Hlc7g== X-Google-Smtp-Source: AGHT+IFipqk/7FJTha18DGZhltJ+yddT0EUpo4e6Ps5WnV2Pkn0BgqdXsr0HLHxpeXVDJLRx9zCk X-Received: by 2002:a17:902:db0f:b0:1db:da3d:6fea with SMTP id m15-20020a170902db0f00b001dbda3d6feamr1908680plx.50.1708692314528; Fri, 23 Feb 2024 04:45:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708692314; cv=pass; d=google.com; s=arc-20160816; b=t5WQav/UqJhIiwUVGSFf+i0UnMDxuK0socFWB38ruN0DPO6rQL4HQUgXOrtlDjfG1C de7/sbDLgl/fwB4JGYZMWMUDmxluib0B3T7/0zUpWG3UU7rX3tSJTMZKGqUdt0h4n1BV Hj5BpSBPyq7IYDoNp0G7oRFWeT5z/r0W65buN18RkNppW3+4d8380z2gcQgrS2vaOxpc Y1QzfNWXtN6pzw2lHG7NgJIAZEbbidC7aQIzku1J+3UmvIEP7sOtmHOhLEsNh3C8q221 AA6GUga4Lxuvx4Ivilp8AHmeIKfV0eky/Os8mlyQAs/kX7t+LC5GsLuEQZLL/BUPjOQh uEZA== 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; bh=TZJ/mV3T8LMFsIR1+hANziShQqPprdLPOJmPvq5DV3k=; fh=Yi6ySJLnlzcQ1cHAdg2GPzYoHD8fqg7PDZYO61sUDKc=; b=fN7JlltsvYL2ANyUT/XlEIfU/tbXrmKEUOYxFfk7ZnHKUhTUJ0QzNCGF2BUuUPREhW 23pDTJsCkZf2W6REvT/ol/WHFL6IvmfCz3mffbjK3+6iRwdElKZr16wUzzRv3PgUJm4V es9dsH+4DHSa6XcN7avhP6R2XrTaG2+lRzV4IEtClS18iU+TdWdXR4rbIpRhYdK8E3Ld UwQZrv5uPbG0v8xtewJYPZI82MwVDwv4vL0PgmjZHOCL58ILUve+Kgcx519qn+xHOvNN q1pMe+t2Gxw4BkVHLybVFUceqj6RlwBiE/B+fqcLdX1vVUeLuCBAlAwhY7nzDghBVaBS BpIA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-78314-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-78314-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=QUARANTINE) header.from=huawei.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id n17-20020a170902e55100b001db729bd8d2si12089086plf.318.2024.02.23.04.45.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 04:45:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-78314-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-78314-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-78314-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=QUARANTINE) header.from=huawei.com 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 26341283B6D for ; Fri, 23 Feb 2024 12:45:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A0FA7C6E5; Fri, 23 Feb 2024 12:45:07 +0000 (UTC) Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) (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 6A70873F0A; Fri, 23 Feb 2024 12:45:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.176.79.56 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708692306; cv=none; b=jpSMV1pivRbgmm4AXGHxtpffhigaV/jUhbFXnwtGhG2cAEPxHDEzDw4lNhQ4qUFE8kLHrZ5aW/3j+8+ZCcYhBUGN194BnNMFTsWIHQuwlfgqpeH5VjRr5FBOlq+d6tqw5DP65QUYdXyASPHp3drKWaQrpAPqgrkJmW4RSNJcayQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708692306; c=relaxed/simple; bh=/P0d7MsaVNdBlLfUwWZoHt7wXeT0BkNvQZy0Rn68npo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=KNR/9z0L4Hramv53OHGv8xWvYfTs67M4+ax9sc2iir3TxslS1ci+9kstEHSCuVrTHQPURCAcADNvhossySIO2d3O3grnzsO2NTiJAlPsJ1XFPOfLqeHBNHKGztLjKQQjmRagZw520i1FPfzxOI6WSaXwF3c+lUlSHrsP3Ik97Hc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=185.176.79.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.18.186.216]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4Th8l56j03z6K8wg; Fri, 23 Feb 2024 20:41:21 +0800 (CST) Received: from lhrpeml500005.china.huawei.com (unknown [7.191.163.240]) by mail.maildlp.com (Postfix) with ESMTPS id E6723140119; Fri, 23 Feb 2024 20:45:00 +0800 (CST) Received: from SecurePC-101-06.china.huawei.com (10.122.247.231) by lhrpeml500005.china.huawei.com (7.191.163.240) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Fri, 23 Feb 2024 12:45:00 +0000 From: Jonathan Cameron To: , Rob Herring , Frank Rowand , , Julia Lawall CC: Peter Zijlstra , Andy Shevchenko , Greg Kroah-Hartman , Subject: [PATCH v2 1/4] of: Add cleanup.h based auto release via __free(device_node) markings. Date: Fri, 23 Feb 2024 12:44:29 +0000 Message-ID: <20240223124432.26443-2-Jonathan.Cameron@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240223124432.26443-1-Jonathan.Cameron@huawei.com> References: <20240223124432.26443-1-Jonathan.Cameron@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: lhrpeml100004.china.huawei.com (7.191.162.219) To lhrpeml500005.china.huawei.com (7.191.163.240) 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.39.2