of: fix bugs and improve codes
Subsystems (2)
v3 cover of: fix bugs and improve codes
This patch series is to fix bugs and improve codes for drivers/of/*.
---
Changes in v3:
- Drop 2 applied patches and pick up patch 4/7 again
- Fix build error for patch 6/7.
- Include of_private.h instead of function declaration for patch 2/7
- Correct tile and commit messages.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Drop applied/conflict/TBD patches.
- Correct based on Rob's comments.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Zijun Hu (7):
of: Correct child specifier used as input of the 2nd nexus node
of: Do not expose of_alias_scan() and correct its comments
of: Make of_property_present() applicable to all kinds of property
of: property: Use of_property_present() for of_fwnode_property_present()
of: Fix available buffer size calculating error in API of_device_uevent_modalias()
of: Fix potential wrong MODALIAS uevent value
of: Do not expose of_modalias()
drivers/of/base.c | 7 ++-- drivers/of/device.c | 33 +++++++-------- drivers/of/module.c | 109 +++++++++++++++++++++++++++++------------------- drivers/of/of_private.h | 4 ++ drivers/of/pdt.c | 2 + drivers/of/property.c | 2 +- include/linux/of.h | 31 ++++++-------- 7 files changed, 102 insertions(+), 86 deletions(-)
---
base-commit: 0f7ca6f69354e0c3923bbc28c92d0ecab4d50a3e
change-id: 20241206-of_core_fix-dc3021a06418
Best regards,
Signature · 1 line
Zijun Hu <[email protected]>
PATCH v3 1/7 of: Correct child specifier used as input of the 2nd nexus node
From: Zijun Hu <[email protected]>
API of_parse_phandle_with_args_map() will use wrong input for nexus node Nexus_2 as shown below:
Node_1 Nexus_1 Nexus_2
&Nexus_1,arg_1 -> arg_1,&Nexus_2,arg_2' -> &Nexus_2,arg_2 -> arg_2,...
map-pass-thru=<...>
Nexus_1's output arg_2 should be used as input of Nexus_2, but the API wrongly uses arg_2' instead which != arg_2 due to Nexus_1's map-pass-thru.
Fix by always making @match_array point to @initial_match_array into which to store nexus output.
--- drivers/of/base.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/of/base.c +1 -1
@@ -1536,7 +1536,6 @@ int of_parse_phandle_with_args_map(const struct device_node *np,* specifier into the out_args structure, keeping the * bits specified in <list>-map-pass-thru. */ - match_array = map - new_size;for (i =0 ; i < new_size; i++) {__be32 val = *(map - new_size + i);
@@ -1545,6 +1544,7 @@ int of_parse_phandle_with_args_map(const struct device_node *np, val |=cpu_to_be32 (out_args->args[i]) & pass[i]; } + initial_match_array[i] = val; out_args->args[i] =be32_to_cpu (val); } out_args->args_count = list_size = new_size;
Signature · 1 line
2.34.1
PATCH v3 2/7 of: Do not expose of_alias_scan() and correct its comments
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
For of_alias_scan():
- Do not expose it since it has no external callers.
- Correct its comments shown below:
1) Replace /* with /** to start comments since it is not a API.
2) Delete return value descriptions since it is a void function.
--- drivers/of/base.c | 5 ++--- drivers/of/of_private.h | 2 ++ drivers/of/pdt.c | 2 ++ include/linux/of.h | 1 - 4 files changed, 6 insertions(+), 4 deletions(-)
drivers/of/base.c +2 -3
@@ -1806,14 +1806,13 @@ static void of_alias_add(struct alias_prop *ap, struct device_node *np, ap->alias, ap->stem, ap->id, np); } -/** +/* * of_alias_scan - Scan all properties of the 'aliases' node * @dt_alloc: An allocator that provides a virtual address to memory * for storing the resulting tree * * The function scans all the properties of the 'aliases' node and populates -* the global lookup table with the properties. It returns the -* number of alias properties found, or an error code in case of failure. +* the global lookup table with the properties. */ void of_alias_scan (void * (*dt_alloc)(u64 size,u64 align)) {
drivers/of/of_private.h +2 -0
@@ -119,6 +119,8 @@ extern void *__unflatten_device_tree(const void *blob,void *(*dt_alloc)(u64 size,u64 align), bool detached); +void of_alias_scan (void * (*dt_alloc)(u64 size,u64 align)); +/** * General utilities for working with live trees. *
drivers/of/pdt.c +2 -0
@@ -19,6 +19,8 @@#include <linux/of.h> #include <linux/of_pdt.h> +#include "of_private.h" +static struct of_pdt_ops *of_pdt_prom_ops__initdata ;#if defined(CONFIG_SPARC )
include/linux/of.h +0 -1
@@ -397,7 +397,6 @@ extern int of_phandle_iterator_args(struct of_phandle_iterator *it,uint32_t *args, int size); -extern void of_alias_scan (void * (*dt_alloc)(u64 size,u64 align));extern int of_alias_get_id (const struct device_node *np,const char *stem);extern int of_alias_get_highest_id (const char *stem);
Signature · 1 line
2.34.1
PATCH v3 3/7 of: Make of_property_present() applicable to all kinds of property
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
API of_property_present() invokes of_property_read_bool() to check if a property is present or not, and that has 2 shortcomings shown below:
- That narrows down property scope applicable to of_property_present() from all kinds of property to only bool type.
- That is less logical since it says a property's presence is decided by its bool property value.
Fix by making of_property_read_bool() invoke of_property_present().
--- include/linux/of.h | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)
include/linux/of.h +12 -11
@@ -1242,17 +1242,16 @@ static inline int of_property_read_string_index(const struct device_node *np, }/** -* of_property_read_bool - Find a property -* @np: device node from which the property value is to be read. +* of_property_present - Test if a property is present in a node +* @np: device node to search for the property. * @propname: name of the property to be searched. * -* Search for a boolean property in a device node. Usage on non-boolean -* property types is deprecated. +* Test for a property present in a device node. * * Return: true if the property exists false otherwise. */ -static inline bool of_property_read_bool (const struct device_node *np, -const char *propname) +static inline bool of_property_present (const struct device_node *np, +const char *propname) {const struct property *prop =of_find_property (np, propname,NULL );
@@ -1260,17 +1259,19 @@ static inline bool of_property_read_bool(const struct device_node *np, }/** -* of_property_present - Test if a property is present in a node -* @np: device node to search for the property. +* of_property_read_bool - Find a property +* @np: device node from which the property value is to be read. * @propname: name of the property to be searched. * -* Test for a property present in a device node. +* Search for a boolean property in a device node. Usage on non-boolean +* property types is deprecated. * * Return: true if the property exists false otherwise. */ -static inline bool of_property_present (const struct device_node *np,const char *propname) +static inline bool of_property_read_bool (const struct device_node *np, +const char *propname) { -return of_property_read_bool (np, propname); +return of_property_present (np, propname); }/**
Signature · 1 line
2.34.1
PATCH v3 4/7 of: property: Use of_property_present() for of_fwnode_property_present()
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
Use of_property_present() instead of of_property_read_bool() for of_fwnode_property_present() since the former is more applicable obviously.
---
Hi Rob,
i pick up this change again after some considerations as below:
1) of_property_present() is more suitable than of_property_read_bool() here, deprecated API is not main reason.
2) it does not conflict with your job which warns when use of_property_read_bool() for non-bool property.
--- drivers/of/property.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/of/property.c +1 -1
@@ -966,7 +966,7 @@ of_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode)static bool of_fwnode_property_present (const struct fwnode_handle *fwnode,const char *propname) { -return of_property_read_bool (to_of_node (fwnode), propname); +return of_property_present (to_of_node (fwnode), propname); } static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
Signature · 1 line
2.34.1
PATCH v3 5/7 of: Fix available buffer size calculating error in API of_device_uevent_modalias()
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
of_device_uevent_modalias() saves MODALIAS value from offset (@env->buflen - 1), so the available buffer size should be (sizeof(@env->buf) - @env->buflen + 1), but it uses the wrong size (sizeof(@env->buf) - @env->buflen).
Fix by using size of space from char '\0' inclusive which ends "MODALIAS=".
--- drivers/of/device.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/of/device.c +10 -4
@@ -257,6 +257,7 @@ EXPORT_SYMBOL_GPL(of_device_uevent);int of_device_uevent_modalias (const struct device *dev,struct kobj_uevent_env *env) {int sl; +int pos;if ((!dev) || (!dev->of_node) || dev->of_node_reused)return -ENODEV ;
@@ -265,13 +266,18 @@ int of_device_uevent_modalias(const struct device *dev, struct kobj_uevent_env *if (add_uevent_var (env,"MODALIAS=" ))return -ENOMEM ; - sl =of_modalias (dev->of_node, &env->buf[env->buflen-1 ], -sizeof (env->buf) - env->buflen); +/* +* @env->buflen is pointing to the char after '\0' now +* override the '\0' to save MODALIAS value. +*/ + pos = env->buflen -1 ; + sl =of_modalias (dev->of_node, &env->buf[pos], +sizeof (env->buf) - pos);if (sl <0 )return sl; -if (sl >= (sizeof (env->buf) - env->buflen)) +if (sl >= (sizeof (env->buf) - pos))return -ENOMEM ; - env->buflen += sl; + env->buflen = pos + sl +1 ;return 0 ; }
Signature · 1 line
2.34.1
PATCH v3 6/7 of: Fix potential wrong MODALIAS uevent value
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
API of_device_uevent_modalias() makes uevent "MODALIAS=ITS_VALUE" in two steps, namely, produces "MODALIAS=" with add_uevent_var() fistly, then remainning "ITS_VALUE" with of_modalias() finally, and that may result in various wrong uevents as explained below:
"MODALIAS=\0" // @env->buf is full after the 1st step.
"MODALIAS=ITS_\0" // @env->buf is not enough during 2nd step.
"MODALIAS=ITS_VAR=VAR_VALUE\0" // another uevent "VAR=VAR_VALUE" comes.
The API depends on uevent internal design, so is not good practice as well.
Fix by:
1) Respin the callee of_modalias() with new prototype which is friendly with its callers.
2) Invoke add_uevent_var() to make the whole MODALIAS uevent.
3) Adapt new of_modalias() for its other callers.
BTW, there are no external callers of of_modalias() now.
--- drivers/of/device.c | 39 +++++++------------- drivers/of/module.c | 103 +++++++++++++++++++++++++++++++--------------------- include/linux/of.h | 7 ++-- 3 files changed, 79 insertions(+), 70 deletions(-)
drivers/of/device.c +14 -25
@@ -195,19 +195,18 @@ EXPORT_SYMBOL(of_device_get_match_data);ssize_t of_device_modalias (struct device *dev,char *str,ssize_t len) {ssize_t sl; + char *ptr __free(kfree) = NULL;if (!dev || !dev->of_node || dev->of_node_reused)return -ENODEV ; - sl =of_modalias (dev->of_node, str, len -2 ); -if (sl <0 ) -return sl; -if (sl > len -2 ) + ptr =of_modalias (dev->of_node, &sl); +if (IS_ERR (ptr)) +return PTR_ERR (no_free_ptr (ptr)); +if (sl +2 > len)return -ENOMEM ; - str[sl++] ='\n' ; - str[sl] =0 ; -return sl; +return snprintf (str, len,"%s\n" , ptr); }EXPORT_SYMBOL_GPL (of_device_modalias);
@@ -256,30 +255,20 @@ EXPORT_SYMBOL_GPL(of_device_uevent);int of_device_uevent_modalias (const struct device *dev,struct kobj_uevent_env *env) { -int sl; -int pos; +int ret; +char *ptr;if ((!dev) || (!dev->of_node) || dev->of_node_reused)return -ENODEV ; -/* Devicetree modalias is tricky, we add it in 2 steps */ -if (add_uevent_var (env,"MODALIAS=" )) -return -ENOMEM ; + ptr =of_modalias (dev->of_node,NULL ); +if (IS_ERR (ptr)) +return PTR_ERR (ptr); -/* -* @env->buflen is pointing to the char after '\0' now -* override the '\0' to save MODALIAS value. -*/ - pos = env->buflen -1 ; - sl =of_modalias (dev->of_node, &env->buf[pos], -sizeof (env->buf) - pos); -if (sl <0 ) -return sl; -if (sl >= (sizeof (env->buf) - pos)) -return -ENOMEM ; - env->buflen = pos + sl +1 ; + ret =add_uevent_var (env,"MODALIAS=%s" , ptr); -return 0 ; +kfree (ptr); +return ret; }EXPORT_SYMBOL_GPL (of_device_uevent_modalias);
drivers/of/module.c +62 -41
133 lines not on this page · raw message
include/linux/of.h +3 -4
@@ -382,7 +382,7 @@ extern int of_count_phandle_with_args(const struct device_node *np, const char *list_name, const char *cells_name);/* module functions */ -extern ssize_t of_modalias (const struct device_node *np,char *str,ssize_t len); +char *of_modalias (const struct device_node *np,ssize_t *lenp);extern int of_request_module (const struct device_node *np);/* phandle iterator functions */
@@ -761,10 +761,9 @@ static inline int of_count_phandle_with_args(const struct device_node *np,return -ENOSYS ; } -static inline ssize_t of_modalias (const struct device_node *np,char *str, -ssize_t len) +static inline char *of_modalias (const struct device_node *np,ssize_t *lenp) { -return -ENODEV ; +return ERR_PTR (-ENODEV ); }static inline int of_request_module (const struct device_node *np)
Signature · 1 line
2.34.1
PATCH v3 7/7 of: Do not expose of_modalias()
In reply to Zijun HuFrom: Zijun Hu <[email protected]>
Do not expose of_modalias() since it has no external callers.
--- drivers/of/module.c | 2 ++ drivers/of/of_private.h | 2 ++ include/linux/of.h | 6 ------ 3 files changed, 4 insertions(+), 6 deletions(-)
drivers/of/module.c +2 -0
@@ -8,6 +8,8 @@#include <linux/slab.h> #include <linux/string.h> +#include "of_private.h" +/* * of_modalias - get MODALIAS string value for a OF device node * @np: the OF device node
drivers/of/of_private.h +2 -0
@@ -150,6 +150,8 @@ extern void __of_detach_node(struct device_node *np);extern void __of_sysfs_remove_bin_file (struct device_node *np,const struct property *prop); +char *of_modalias (const struct device_node *np,ssize_t *lenp); +/* illegal phandle value (set when unresolved) */ #define OF_PHANDLE_ILLEGAL 0xdeadbeef
include/linux/of.h +0 -6
@@ -382,7 +382,6 @@ extern int of_count_phandle_with_args(const struct device_node *np, const char *list_name, const char *cells_name);/* module functions */ -char *of_modalias (const struct device_node *np,ssize_t *lenp);extern int of_request_module (const struct device_node *np);/* phandle iterator functions */
@@ -761,11 +760,6 @@ static inline int of_count_phandle_with_args(const struct device_node *np,return -ENOSYS ; } -static inline char *of_modalias (const struct device_node *np,ssize_t *lenp) -{ -return ERR_PTR (-ENODEV ); -} -static inline int of_request_module (const struct device_node *np) {return -ENODEV ;
Signature · 1 line
2.34.1