of: Add debug aid to find unused device tree properties
RFC PATCH 1/1 of: Add debug aid to find unused device tree properties
This is a proof-of-concept patch that introduces a debug feature I find particularly useful. I frequently encounter situations where I'm uncertain if my device tree configuration is correct or being utilized by the kernel. This is especially common when porting device trees from vendor kernels, as some properties may have slightly different names in the upstream kernel, or upstream drivers may not use certain properties at all.
By writing 'y' to <debugfs>/of_mark_queried, every queried device tree property will gain S_IWUSR in sysfs. While abusing S_IWUSR is
admittedly a crude hack, it works for now. I'm open to better ideas,
perhaps using an xattr?
That way, dtc can easily add an annotation to unused device trees when reading from /proc/device-tree.
--- drivers/of/Kconfig | 9 +++++ drivers/of/Makefile | 1 + drivers/of/base.c | 2 + drivers/of/debug.c | 83 +++++++++++++++++++++++++++++++++++++++++ drivers/of/of_private.h | 6 +++ include/linux/of.h | 3 ++ 6 files changed, 104 insertions(+) create mode 100644 drivers/of/debug.c
drivers/of/Kconfig +9 -0
@@ -90,6 +90,15 @@ config OF_IRQ def_bool y depends on !SPARC && IRQ_DOMAIN +config OF_DEBUG + bool "Device Tree debug features" + select DEBUG_FS + help + This option enables device tree debug features. + Currently only <debugfs>/of_mark_queried, writing 'y' to this file + causes setting S_IWUSR on each device tree property in sysfs that + was queried by a device driver. This is useful to find dead properties. + config OF_RESERVED_MEM def_bool OF_EARLY_FLATTREE
drivers/of/Makefile +1 -0
@@ -25,3 +25,4 @@ obj-$(CONFIG_OF_OVERLAY_KUNIT_TEST) += overlay-test.o overlay-test-y := overlay_test.o kunit_overlay_test.dtbo.o obj-$(CONFIG_OF_UNITTEST) += unittest-data/ +obj-$(CONFIG_OF_DEBUG) += debug.o
drivers/of/base.c +2 -0
@@ -202,6 +202,8 @@ static struct property *__of_find_property(const struct device_node *np,if (of_prop_cmp (pp->name, name) ==0 ) {if (lenp) *lenp = pp->length; +of_debug_mark_queried (pp); +break ; } }
drivers/of/debug.c new file +83 -0
83 lines not on this page · raw message
drivers/of/of_private.h +6 -0
@@ -184,4 +184,10 @@ void fdt_init_reserved_mem(void);bool of_fdt_device_is_available (const void *blob,unsigned long node); +#if defined(CONFIG_OF_DEBUG ) +void of_debug_mark_queried (struct property *pp); +#else +static inline void of_debug_mark_queried (struct property *pp) { } +#endif +#endif /* _LINUX_OF_PRIVATE_H */
include/linux/of.h +3 -0
@@ -39,6 +39,9 @@ struct property {#if defined(CONFIG_OF_KOBJ )struct bin_attribute attr;#endif +#if defined(CONFIG_OF_DEBUG ) +bool queried; +#endif };#if defined(CONFIG_SPARC )
Signature · 1 line
2.35.3
Quoted 8 lines · Richard Weinberger
This is a proof-of-concept patch that introduces a debug feature I find particularly useful. I frequently encounter situations where I'm uncertain if my device tree configuration is correct or being utilized by the kernel. This is especially common when porting device trees from vendor kernels, as some properties may have slightly different names in the upstream kernel, or upstream drivers may not use certain properties at all.
Why not just add debug logs? You can print the full path of the properties being read and it should be easy to grep for the property you care about.
Quoted 1 line
By writing 'y' to <debugfs>/of_mark_queried, every queried device tree
A lot of querying is going to happen at boot time. So, I'm not sure if this method of enabling it is helpful. If we do this, make it a kernel command line.
Quoted 3 lines
property will gain
S_IWUSRin sysfs. While abusingS_IWUSRis
admittedly a crude hack, it works for now. I'm open to better ideas,
perhaps using an xattr?
This seems quite convoluted. Why not just add another file per node that lists all the queried properties?
Quoted 2 lines
That way, dtc can easily add an annotation to unused device trees when reading from
/proc/device-tree.
I'm not too familiar with this part. Can you elaborate more?
-Saravana
Quoted 175 lines
Signed-off-by Richard Weinberger <[email protected]>--- drivers/of/Kconfig | 9 +++++ drivers/of/Makefile | 1 + drivers/of/base.c | 2 + drivers/of/debug.c | 83 +++++++++++++++++++++++++++++++++++++++++ drivers/of/of_private.h | 6 +++ include/linux/of.h | 3 ++ 6 files changed, 104 insertions(+) create mode 100644 drivers/of/debug.cdrivers/of/Kconfig +9
-0@@ -90,6 +90,15 @@ config OF_IRQ def_bool y depends on !SPARC && IRQ_DOMAIN +config OF_DEBUG + bool "Device Tree debug features" + select DEBUG_FS + help + This option enables device tree debug features. + Currently only <debugfs>/of_mark_queried, writing 'y' to this file + causes setting S_IWUSR on each device tree property in sysfs that + was queried by a device driver. This is useful to find dead properties. + config OF_RESERVED_MEM def_bool OF_EARLY_FLATTREEdrivers/of/Makefile +1
-0@@ -25,3 +25,4 @@ obj-$(CONFIG_OF_OVERLAY_KUNIT_TEST) += overlay-test.o overlay-test-y := overlay_test.o kunit_overlay_test.dtbo.o obj-$(CONFIG_OF_UNITTEST) += unittest-data/ +obj-$(CONFIG_OF_DEBUG) += debug.odrivers/of/base.c +2
-0@@ -202,6 +202,8 @@ static struct property *__of_find_property(const struct device_node *np,if (of_prop_cmp (pp->name, name) ==0 ) {if (lenp) *lenp = pp->length; +of_debug_mark_queried (pp); +break ; } }drivers/of/debug.c new file +83
-0@@ -0,0 +1,83 @@ +// SPDX-License-Identifier: GPL-2.0+ +#include <linux/debugfs.h> +#include <linux/kstrtox.h> +#include <linux/of.h> + +#include "of_private.h" + +void of_debug_mark_queried (struct property *pp) +{ + pp->queried = true; +} + +static int dtmq_update_node_sysfs (struct device_node *np) +{ +struct property *pp; +int ret =0 ; + +if (!IS_ENABLED (CONFIG_SYSFS ) || !of_kset) + goto out; + +for_each_property_of_node (np, pp) { +if (pp->queried) { + ret =sysfs_chmod_file (&np->kobj, &pp->attr.attr, + pp->attr.attr.mode |S_IWUSR ); +if (ret) +break ; + } + } + +out: +return ret; +} + +static int dtmq_update_sysfs (void ) +{ +struct device_node *np; +int ret =0 ; + +mutex_lock (&of_mutex); +for_each_of_allnodes (np) { + ret =dtmq_update_node_sysfs (np); +if (ret) +break ; + } +mutex_unlock (&of_mutex); + +return ret; +} + +static ssize_t dtmq_file_write (struct file *file,const char __user *user_buf, +size_t count,loff_t *ppos) +{ +bool do_it; +int ret; + + ret =kstrtobool_from_user (user_buf, count, &do_it); +if (ret) + goto out; + +if (do_it) { + ret =dtmq_update_sysfs (); +if (!ret) + ret = count; + }else { + ret = -EINVAL ; + } + +out: +return ret; +} + +static const struct file_operations dtmq_fops = { + .write = dtmq_file_write, + .open = simple_open, + .owner =THIS_MODULE , +}; + +static int __init of_debug_init (void ) +{ +return PTR_ERR_OR_ZERO (debugfs_create_file ("of_mark_queried" ,0644 ,NULL ,NULL , + &dtmq_fops)); +} +late_initcall (of_debug_init);drivers/of/of_private.h +6
-0@@ -184,4 +184,10 @@ void fdt_init_reserved_mem(void);bool of_fdt_device_is_available (const void *blob,unsigned long node); +#if defined(CONFIG_OF_DEBUG ) +void of_debug_mark_queried (struct property *pp); +#else +static inline void of_debug_mark_queried (struct property *pp) { } +#endif +#endif /* _LINUX_OF_PRIVATE_H */ include/linux/of.h +3
-1@@ -39,6 +39,9 @@ struct property {#if defined(CONFIG_OF_KOBJ )struct bin_attribute attr;#endif +#if defined(CONFIG_OF_DEBUG ) +bool queried; +#endif };#if defined(CONFIG_SPARC ) --2.35.3
Saravana,
Am Montag, 14. Oktober 2024, 04:37:10 CEST schrieb 'Saravana Kannan' via upstream:
Quoted 10 lines · Richard Weinberger
Quoted 8 lines
This is a proof-of-concept patch that introduces a debug feature I find particularly useful. I frequently encounter situations where I'm uncertain if my device tree configuration is correct or being utilized by the kernel. This is especially common when porting device trees from vendor kernels, as some properties may have slightly different names in the upstream kernel, or upstream drivers may not use certain properties at all.
Quoted 3 lines
Why not just add debug logs? You can print the full path of the properties being read and it should be easy to grep for the property you care about.
This approach works only well when I know what property I care about. The problem I'm addressing is more like importing a device tree (usually something wonky from vendor tree) and want check whether all properties stated in the device tree get actually used.
If something is not being used, I need to investigate...
Quoted 6 lines
Quoted 2 lines
By writing 'y' to <debugfs>/of_mark_queried, every queried device tree
A lot of querying is going to happen at boot time. So, I'm not sure if this method of enabling it is helpful. If we do this, make it a kernel command line.
It works differently.
As soon CONFIG_OF_DEBUG is enabled, every queried property gets ->queried set to true.
Writing to of_mark_queried updates just the sysfs representation of the device tree.
That way one can analyze the tree also step by step.
E.g. learning what properties have been queried while bootup and later, what was queried after loading kernel modules.
Quoted 7 lines
Quoted 4 lines
property will gain
S_IWUSRin sysfs. While abusingS_IWUSRis
admittedly a crude hack, it works for now. I'm open to better ideas,
perhaps using an xattr?This seems quite convoluted. Why not just add another file per node that lists all the queried properties?
AFAICT, adding another file will confuse dtc since dtc treats every file in sysfs as property.
Quoted 5 lines
Quoted 3 lines
That way, dtc can easily add an annotation to unused device trees when reading from
/proc/device-tree.I'm not too familiar with this part. Can you elaborate more?
E.g.
$ ./dtc/dtc -I fs -O dts /proc/device-tree
...
intc@8000000 {
...
v2m@8020000 {
msi-controller;
compatible = "arm,gic-v2m-frame";
reg = <0x00 0x8020000 0x00 0x1000>;
lala = "lulu"; /* not queried */
};
};
...With my kernel patch and minimal changes to dtc, it is able to tell me that nobody has read the property "lala" in my device tree.
Thanks,
//richard
Signature · 2 lines
sigma star gmbh | Eduard-Bodem-Gasse 6, 6020 Innsbruck, AUT UID/VAT Nr: ATU 66964118 | FN: 374287y
Quoted 7 lines · Richard Weinberger
This is a proof-of-concept patch that introduces a debug feature I find particularly useful. I frequently encounter situations where I'm uncertain if my device tree configuration is correct or being utilized by the kernel. This is especially common when porting device trees from vendor kernels, as some properties may have slightly different names in the upstream kernel, or upstream drivers may not use certain properties at all.
In general I don't mind, but I have a comment about above rationale. It's just wrong. The point of DT is to describe hardware, not the one given, fixed in time implementation.
What's more, writing bindings mentions this explicit: make binding complete, even if it is not used.
Best regards,
Krzysztof
Krzysztof,
Am Montag, 14. Oktober 2024, 09:49:14 CEST schrieb 'Krzysztof Kozlowski' via upstream:
Quoted 9 lines · Richard Weinberger
Quoted 7 lines
This is a proof-of-concept patch that introduces a debug feature I find particularly useful. I frequently encounter situations where I'm uncertain if my device tree configuration is correct or being utilized by the kernel. This is especially common when porting device trees from vendor kernels, as some properties may have slightly different names in the upstream kernel, or upstream drivers may not use certain properties at all.
Quoted 3 lines
In general I don't mind, but I have a comment about above rationale. It's just wrong. The point of DT is to describe hardware, not the one given, fixed in time implementation.
I agree with you, sorry for being imprecise.
Quoted 2 lines
What's more, writing bindings mentions this explicit: make binding complete, even if it is not used.
Yes, with this aid, it is IMHO easier to find bindings that need attention. Just as an example, lately the device tree of a vendor used the property "timers", but in mainline it is "ti,timers". With this debug feature, it is easy to see that "timers" is not being used, and somebody has to decide whether the property is really not used by a driver, or if the binding needs more work.
Thanks,
//richard
Signature · 2 lines
sigma star gmbh | Eduard-Bodem-Gasse 6, 6020 Innsbruck, AUT UID/VAT Nr: ATU 66964118 | FN: 374287y
Rob Herring
maintainerQuoted 19 lines · Richard Weinberger
Krzysztof,
Am Montag, 14. Oktober 2024, 09:49:14 CEST schrieb 'Krzysztof Kozlowski' via upstream:
Quoted 9 lines · Richard Weinberger
Quoted 7 lines
This is a proof-of-concept patch that introduces a debug feature I find particularly useful. I frequently encounter situations where I'm uncertain if my device tree configuration is correct or being utilized by the kernel. This is especially common when porting device trees from vendor kernels, as some properties may have slightly different names in the upstream kernel, or upstream drivers may not use certain properties at all.
Quoted 3 lines
In general I don't mind, but I have a comment about above rationale. It's just wrong. The point of DT is to describe hardware, not the one given, fixed in time implementation.
I agree with you, sorry for being imprecise.
Quoted 8 lines
Quoted 2 lines
What's more, writing bindings mentions this explicit: make binding complete, even if it is not used.
Yes, with this aid, it is IMHO easier to find bindings that need attention. Just as an example, lately the device tree of a vendor used the property "timers", but in mainline it is "ti,timers". With this debug feature, it is easy to see that "timers" is not being used, and somebody has to decide whether the property is really not used by a driver, or if the binding needs more work.
Paying attention to the schema warnings would have found this issue. Assuming there is a schema for the node...
That's not to say this type of run-time check is not also useful.
Rob