summaryrefslogtreecommitdiff
path: root/include/linux
diff options
context:
space:
mode:
authorJonathan Cameron <Jonathan.Cameron@huawei.com>2024-02-17 19:42:38 +0300
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2024-12-14 21:54:28 +0300
commit6fedf3dc30e8584018987fedf4b74c19d82df807 (patch)
treed8c25738a18bdb2fd10042afabc5ee2c33ea661e /include/linux
parentda1097f8d9398b49b3c44cfab517a169266692e8 (diff)
downloadlinux-6fedf3dc30e8584018987fedf4b74c19d82df807.tar.xz
device property: Introduce device_for_each_child_node_scoped()
[ Upstream commit 365130fd47af6d4317aa16a407874b699ab8d8cb ] Similar to recently propose for_each_child_of_node_scoped() this new version of the loop macro instantiates a new local struct fwnode_handle * that uses the __free(fwnode_handle) auto cleanup handling so that if a reference to a node is held on early exit from the loop the reference will be released. If the loop runs to completion, the child pointer will be NULL and no action will be taken. The reason this is useful is that it removes the need for fwnode_handle_put() on early loop exits. If there is a need to retain the reference, then return_ptr(child) or no_free_ptr(child) may be used to safely disable the auto cleanup. Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com> Link: https://lore.kernel.org/r/20240217164249.921878-5-jic23@kernel.org Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Stable-dep-of: 73b03b27736e ("leds: flash: mt6360: Fix device_for_each_child_node() refcounting in error paths") Signed-off-by: Sasha Levin <sashal@kernel.org>
Diffstat (limited to 'include/linux')
-rw-r--r--include/linux/property.h5
1 files changed, 5 insertions, 0 deletions
diff --git a/include/linux/property.h b/include/linux/property.h
index f2e8590cefd8..fb685f0f702a 100644
--- a/include/linux/property.h
+++ b/include/linux/property.h
@@ -122,6 +122,11 @@ struct fwnode_handle *device_get_next_child_node(const struct device *dev,
for (child = device_get_next_child_node(dev, NULL); child; \
child = device_get_next_child_node(dev, child))
+#define device_for_each_child_node_scoped(dev, child) \
+ for (struct fwnode_handle *child __free(fwnode_handle) = \
+ device_get_next_child_node(dev, NULL); \
+ child; child = device_get_next_child_node(dev, child))
+
struct fwnode_handle *fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
const char *childname);
struct fwnode_handle *device_get_named_child_node(const struct device *dev,