This adds of_reserved_mem_lookup_by_name() helper to get a
reserved-memory region based on the DT node name.
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
drivers/of/of_reserved_mem.c | 27 +++++++++++++++++++++++++++
include/linux/of_reserved_mem.h | 6 ++++++
2 files changed, 33 insertions(+)
diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index e0a86c3fa656..b94c3b1d14b6 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -758,6 +758,33 @@ struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
}
EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
+/**
+ * of_reserved_mem_lookup_by_name() - acquire reserved_mem from node name
+ * @name: node name
+ *
+ * This function allows drivers to acquire a reference to the reserved_mem
+ * struct based on a reserved-memory node name.
+ *
+ * Returns a reserved_mem reference, or NULL on error.
+ */
+struct reserved_mem *of_reserved_mem_lookup_by_name(const char *name)
+{
+ struct device_node *np __free(device_node) =
+ of_find_node_by_path("/reserved-memory");
+ struct device_node *child __free(device_node) = NULL;
+
+ if (!np)
+ return ERR_PTR(-ENODEV);
+
+ for_each_child_of_node(np, child) {
+ if (of_node_name_eq(child, name))
+ return of_reserved_mem_lookup(child);
+ }
+
+ return ERR_PTR(-ENODEV);
+}
+EXPORT_SYMBOL_GPL(of_reserved_mem_lookup_by_name);
+
/**
* of_reserved_mem_region_to_resource() - Get a reserved memory region as a resource
* @np: node containing 'memory-region' property
diff --git a/include/linux/of_reserved_mem.h b/include/linux/of_reserved_mem.h
index 3933f1d39e9a..d6d187597b7f 100644
--- a/include/linux/of_reserved_mem.h
+++ b/include/linux/of_reserved_mem.h
@@ -41,6 +41,7 @@ int reserved_mem_device_init(struct device *dev, struct reserved_mem *rmem);
void of_reserved_mem_device_release(struct device *dev);
struct reserved_mem *of_reserved_mem_lookup(struct device_node *np);
+struct reserved_mem *of_reserved_mem_lookup_by_name(const char *name);
int of_reserved_mem_region_to_resource(const struct device_node *np,
unsigned int idx, struct resource *res);
int of_reserved_mem_region_to_resource_byname(const struct device_node *np,
@@ -78,6 +79,11 @@ static inline struct reserved_mem *of_reserved_mem_lookup(struct device_node *np
return NULL;
}
+static inline struct reserved_mem *of_reserved_mem_lookup_by_name(const char *name)
+{
+ return NULL;
+}
+
static inline int of_reserved_mem_region_to_resource(const struct device_node *np,
unsigned int idx,
struct resource *res)
--
2.31.1.272.g89b43f80a514
On 30/07/2025 11:29, Viresh Kumar wrote: > This adds of_reserved_mem_lookup_by_name() helper to get a Add... > > +/** > + * of_reserved_mem_lookup_by_name() - acquire reserved_mem from node name > + * @name: node name > + * > + * This function allows drivers to acquire a reference to the reserved_mem > + * struct based on a reserved-memory node name. > + * > + * Returns a reserved_mem reference, or NULL on error. > + */ > +struct reserved_mem *of_reserved_mem_lookup_by_name(const char *name) > +{ > + struct device_node *np __free(device_node) = > + of_find_node_by_path("/reserved-memory"); > + struct device_node *child __free(device_node) = NULL; This should not be NULL or this should not be cleanup. Follow coding style for cleanup - constructor must be real here. You probably wanted scoped loop below. > + > + if (!np) > + return ERR_PTR(-ENODEV); > + > + for_each_child_of_node(np, child) { Best regards, Krzysztof
On 30-07-25, 11:46, Krzysztof Kozlowski wrote: > On 30/07/2025 11:29, Viresh Kumar wrote: > > + struct device_node *child __free(device_node) = NULL; > > This should not be NULL or this should not be cleanup. Follow coding > style for cleanup - constructor must be real here. I may have misunderstood how cleanup works, but this is what I thought: The cleanup is defined in of.h as: DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T)) Doesn't this mean that it handles the case where `child` is NULL, by not calling of_node_put() ? So it should either be a valid constructor or NULL and not some stale value. > You probably wanted scoped loop below. Ahh, didn't realize we have that available. -- viresh
On 30/07/2025 12:57, Viresh Kumar wrote: > On 30-07-25, 11:46, Krzysztof Kozlowski wrote: >> On 30/07/2025 11:29, Viresh Kumar wrote: >>> + struct device_node *child __free(device_node) = NULL; >> >> This should not be NULL or this should not be cleanup. Follow coding >> style for cleanup - constructor must be real here. > > I may have misunderstood how cleanup works, but this is what I > thought: > > The cleanup is defined in of.h as: > > DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T)) > > > Doesn't this mean that it handles the case where `child` is NULL, by > not calling of_node_put() ? So it should either be a valid constructor > or NULL and not some stale value. I am speaking about coding style. It's explicitly requested to use only the full constructor syntax (see long time Linus' remark or just read cleanup.h docs). NULL is allowed in certain cases, but the code here does not look like needing it in the first place. Best regards, Krzysztof
On 30-07-25, 13:24, Krzysztof Kozlowski wrote: > I am speaking about coding style. It's explicitly requested to use only > the full constructor syntax (see long time Linus' remark or just read > cleanup.h docs). Ahh, I wasn't aware of the interdependency problem. Thanks. -- viresh
© 2016 - 2025 Red Hat, Inc.