Shrinking DT memory usage
Subsystems (2)
Rob Herring
maintainercover Shrinking DT memory usage
On kernels with a minimal config and a RAM target in the 100s of KB, DT is quite a hog of runtime memory usage. How much is dependent on how many nodes and properties in the DT which have a corresponding struct device_node and struct property in the kernel. Just skipping disabled nodes saves a lot by not creating the device_nodes in the first place[1], but there's more low hanging fruit by making some of the fields in struct property and struct device_node optional. With the changes here, the memory usage goes from 17KB to under 8KB on QEMU's ARM virt machine which is a relatively small DT.
The majority of the diffstat here is just moving all the kobject/sysfs related code to its own file so we can avoid adding a bunch of ifdefs.
There's more drastic approaches we could take such as doing the unflattening at build time and storing the bulk of the unflattened tree as const data. Grant also has some ideas on storing properties as ids instead. He's explained it to me, but I still don't understand it.
Rob
[1] https://patchwork.kernel.org/patch/9983203/
Rob Herring (6):
of: fix missing kobject init for !SYSFS && OF_DYNAMIC config
of: remove struct property.unique_id for FDT
of: make struct property _flags field configurable
of: move kobj_to_device_node() into dynamic.c
of: wrap accesses to device_node kobject
of: make kobject and bin_attribute support configurable
drivers/base/core.c | 2 +- drivers/of/Kconfig | 4 ++ drivers/of/Makefile | 1 + drivers/of/base.c | 133 --------------------------------------- drivers/of/dynamic.c | 27 ++------ drivers/of/kobj.c | 164 ++++++++++++++++++++++++++++++++++++++++++++++++ drivers/of/of_private.h | 29 +++++++-- include/linux/of.h | 28 +++++---- 8 files changed, 215 insertions(+), 173 deletions(-) create mode 100644 drivers/of/kobj.c
Signature · 6 lines
2.11.0 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Rob Herring
maintainerPATCH 1/6 of: fix missing kobject init for !SYSFS && OF_DYNAMIC config
The ref counting is broken for OF_DYNAMIC when sysfs is disabled because the kobject initialization is skipped. Only the properties add/remove/update should be skipped for !SYSFS config.
--- drivers/of/base.c | 3 --- 1 file changed, 3 deletions(-)
drivers/of/base.c +0 -3
@@ -169,9 +169,6 @@ int __of_attach_node_sysfs(struct device_node *np)struct property *pp;int rc; -if (!IS_ENABLED (CONFIG_SYSFS )) -return 0 ; -if (!of_kset)return 0 ;
Signature · 6 lines
2.11.0 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Rob Herring
maintainerPATCH 2/6 of: remove struct property.unique_id for FDT
In reply to Rob HerringOnly Sparc uses unique_id, so remove it for FDT builds and shrink struct property a bit making the unflattened DT less of a memory hog.
--- include/linux/of.h | 2 ++ 1 file changed, 2 insertions(+)
include/linux/of.h +2 -0
@@ -38,7 +38,9 @@ struct property {void *value;struct property *next;unsigned long _flags; +#if defined(CONFIG_OF_PROMTREE )unsigned int unique_id; +#endif struct bin_attribute attr; };
Signature · 1 line
2.11.0
Re: [PATCH 2/6] of: remove struct property.unique_id for FDT
Quoted 2 lines · Rob Herring
Only Sparc uses
unique_id, so remove it for FDT builds and shrink struct property a bit making the unflattened DT less of a memory hog.
It can be even more constrained than that. I think the only user is openpromfs so you could make it depend on CONFIG_SUN_OPENPROMFS, and adjust the needed code in pdt.c.
Ideally, I'd like to be rid of unique_id entirely, but that's a much more invasive patch set. Regardless:
(That goes for the entire series)
Quoted 25 lines
Cc Nicolas Pitre <[email protected]>Cc Frank Rowand <[email protected]>Signed-off-by Rob Herring <[email protected]>--- include/linux/of.h | 2 ++ 1 file changed, 2 insertions(+)include/linux/of.h +2
-1@@ -38,7 +38,9 @@ struct property {void *value;struct property *next;unsigned long _flags; +#if defined(CONFIG_OF_PROMTREE )unsigned int unique_id; +#endif struct bin_attribute attr; }; --2.11.0
Rob Herring
maintainerPATCH 3/6 of: make struct property _flags field configurable
In reply to Rob HerringOnly Sparc and CONFIG_OF_DYNAMIC use the struct property._flags field, so make it conditional shrinking struct property a bit.
--- include/linux/of.h | 4 ++++ 1 file changed, 4 insertions(+)
include/linux/of.h +4 -0
@@ -37,7 +37,9 @@ struct property {int length;void *value;struct property *next; +#if defined(CONFIG_OF_DYNAMIC ) || defined(CONFIG_SPARC )unsigned long _flags; +#endif #if defined(CONFIG_OF_PROMTREE )unsigned int unique_id;#endif
@@ -205,6 +207,7 @@ static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)clear_bit (flag, &n->_flags); } +#if defined(CONFIG_OF_DYNAMIC ) || defined(CONFIG_SPARC )static inline int of_property_check_flag (struct property *p,unsigned long flag) {return test_bit (flag, &p->_flags);
@@ -219,6 +222,7 @@ static inline void of_property_clear_flag(struct property *p, unsigned long flag {clear_bit (flag, &p->_flags); } +#endif extern struct device_node *__of_find_all_nodes (struct device_node *prev);extern struct device_node *of_find_all_nodes (struct device_node *prev);
Signature · 1 line
2.11.0
Rob Herring
maintainerPATCH 4/6 of: move kobj_to_device_node() into dynamic.c
In reply to Rob HerringThe only user of kobj_to_device_node() is in dynamic.c, so move it there. This avoids having to make it conditional once kobject is configurable.
--- drivers/of/dynamic.c | 5 +++++ drivers/of/of_private.h | 6 ------ 2 files changed, 5 insertions(+), 6 deletions(-)
drivers/of/dynamic.c +5 -0
@@ -16,6 +16,11 @@#include "of_private.h" +static struct device_node *kobj_to_device_node (struct kobject *kobj) +{ + return container_of(kobj, struct device_node, kobj); +} +/** * of_node_get() - Increment refcount of a node * @node: Node to inc refcount, NULL is supported to simplify writing of
drivers/of/of_private.h +0 -6
@@ -35,12 +35,6 @@ extern struct mutex of_mutex;extern struct list_head aliases_lookup;extern struct kset *of_kset; - -static inline struct device_node *kobj_to_device_node (struct kobject *kobj) -{ - return container_of(kobj, struct device_node, kobj); -} -#if defined(CONFIG_OF_DYNAMIC )extern int of_property_notify (int action,struct device_node *np,struct property *prop,struct property *old_prop);
Signature · 1 line
2.11.0
Rob Herring
maintainerPATCH 5/6 of: wrap accesses to device_node kobject
In reply to Rob HerringIn preparation to make kobject element in struct device_node optional, provide and use a macro to return the kobject pointer. The only user outside the DT core is the driver core.
--- drivers/base/core.c | 2 +- include/linux/of.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/base/core.c +1 -1
@@ -1571,7 +1571,7 @@ static int device_add_class_symlinks(struct device *dev)int error;if (of_node) { - error =sysfs_create_link (&dev->kobj, &of_node->kobj,"of_node" ); + error =sysfs_create_link (&dev->kobj,of_node_kobj (of_node),"of_node" );if (error)dev_warn (dev,"Error %d creating of_node link\n" ,error);/* An error here doesn't warrant bringing down the device */
include/linux/of.h +2 -0
@@ -111,6 +111,8 @@ static inline void of_node_init(struct device_node *node) node->fwnode.ops = &of_fwnode_ops; } +#define of_node_kobj (n) (&(n)->kobj) +/* true when node is initialized */ static inline int of_node_is_initialized (struct device_node *node) {
Signature · 1 line
2.11.0
Re: [PATCH 5/6] of: wrap accesses to device_node kobject
Quoted 4 lines · Rob Herring
In preparation to make kobject element in struct
device_nodeoptional, provide and use a macro to return the kobject pointer. The only user outside the DT core is the driver core.
Quoted 8 lines
Cc Greg Kroah-Hartman <[email protected]>Cc Nicolas Pitre <[email protected]>Cc Frank Rowand <[email protected]>Signed-off-by Rob Herring <[email protected]>--- drivers/base/core.c | 2 +- include/linux/of.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)
Rob Herring
maintainerPATCH 6/6 of: make kobject and bin_attribute support configurable
In reply to Rob HerringHaving device_nodes be kobjects is only needed if sysfs or OF_DYNAMIC is enabled. Otherwise, having a kobject in struct device_node is unnecessary bloat in minimal kernel configurations.
Likewise, bin_attribute is only needed in struct property when sysfs is enabled, so we can make it configurable too.
--- drivers/of/Kconfig | 4 ++ drivers/of/Makefile | 1 + drivers/of/base.c | 130 -------------------------------------- drivers/of/dynamic.c | 22 ------- drivers/of/kobj.c | 164 ++++++++++++++++++++++++++++++++++++++++++++++++ drivers/of/of_private.h | 23 +++++++ include/linux/of.h | 22 +++---- 7 files changed, 202 insertions(+), 164 deletions(-) create mode 100644 drivers/of/kobj.c
drivers/of/Kconfig +4 -0
@@ -46,10 +46,14 @@ config OF_EARLY_FLATTREE config OF_PROMTREE bool +config OF_KOBJ + def_bool SYSFS + # Hardly any platforms need this. It is safe to select, but only do so if you # need it. config OF_DYNAMIC bool "Support for dynamic device trees" if OF_UNITTEST + select OF_KOBJ help On some platforms, the device tree can be manipulated at runtime. While this option is selected automatically on such platforms, you
drivers/of/Makefile +1 -0
@@ -1,4 +1,5 @@ obj-y = base.o device.o platform.o property.o +obj-$(CONFIG_OF_KOBJ) += kobj.o obj-$(CONFIG_OF_DYNAMIC) += dynamic.o obj-$(CONFIG_OF_FLATTREE) += fdt.o obj-$(CONFIG_OF_EARLY_FLATTREE) += fdt_address.o
drivers/of/base.c +0 -130
148 lines not on this page · raw message
drivers/of/dynamic.c +0 -22
@@ -48,28 +48,6 @@ void of_node_put(struct device_node *node) }EXPORT_SYMBOL (of_node_put); -void __of_detach_node_sysfs (struct device_node *np) -{ -struct property *pp; - -if (!IS_ENABLED (CONFIG_SYSFS )) -return ; - -BUG_ON (!of_node_is_initialized (np)); -if (!of_kset) -return ; - -/* only remove properties if on sysfs */ -if (of_node_is_attached (np)) { -for_each_property_of_node (np, pp) -__of_sysfs_remove_bin_file (np, pp); -kobject_del (&np->kobj); - } - -/* finally remove the kobj_init ref */ -of_node_put (np); -} -static BLOCKING_NOTIFIER_HEAD (of_reconfig_chain);int of_reconfig_notifier_register (struct notifier_block *nb)
drivers/of/kobj.c new file +164 -0
164 lines not on this page · raw message
drivers/of/of_private.h +23 -0
@@ -49,6 +49,29 @@ static inline int of_property_notify(int action, struct device_node *np, }#endif /* CONFIG_OF_DYNAMIC */ +#if defined(CONFIG_OF_KOBJ ) +int of_node_is_attached (struct device_node *node); +int __of_add_property_sysfs (struct device_node *np,struct property *pp); +void __of_remove_property_sysfs (struct device_node *np,struct property *prop); +void __of_update_property_sysfs (struct device_node *np,struct property *newprop, +struct property *oldprop); +int __of_attach_node_sysfs (struct device_node *np); +void __of_detach_node_sysfs (struct device_node *np); +#else +static inline int __of_add_property_sysfs (struct device_node *np,struct property *pp) +{ +return 0 ; +} +static inline void __of_remove_property_sysfs (struct device_node *np,struct property *prop) {} +static inline void __of_update_property_sysfs (struct device_node *np, +struct property *newprop,struct property *oldprop) {} +static inline int __of_attach_node_sysfs (struct device_node *np) +{ +return 0 ; +} +static inline void __of_detach_node_sysfs (struct device_node *np) {} +#endif +#if defined(CONFIG_OF_UNITTEST ) && defined(CONFIG_OF_OVERLAY )extern void __init unittest_unflatten_overlay_base (void );#else
include/linux/of.h +10 -12
@@ -43,7 +43,9 @@ struct property {#if defined(CONFIG_OF_PROMTREE )unsigned int unique_id;#endif +#if defined(CONFIG_OF_KOBJ )struct bin_attribute attr; +#endif };#if defined(CONFIG_SPARC )
@@ -62,7 +64,9 @@ struct device_node {struct device_node *parent;struct device_node *child;struct device_node *sibling; +#if defined(CONFIG_OF_KOBJ )struct kobject kobj; +#endif unsigned long _flags;void *data;#if defined(CONFIG_SPARC )
@@ -107,23 +111,17 @@ extern struct kobj_type of_node_ktype;extern const struct fwnode_operations of_fwnode_ops;static inline void of_node_init (struct device_node *node) { +#if defined(CONFIG_OF_KOBJ )kobject_init (&node->kobj, &of_node_ktype); +#endif node->fwnode.ops = &of_fwnode_ops; } +#if defined(CONFIG_OF_KOBJ )#define of_node_kobj (n) (&(n)->kobj) - -/* true when node is initialized */ -static inline int of_node_is_initialized (struct device_node *node) -{ -return node && node->kobj.state_initialized; -} - -/* true when node is attached (i.e. present on sysfs) */ -static inline int of_node_is_attached (struct device_node *node) -{ -return node && node->kobj.state_in_sysfs; -} +#else +#define of_node_kobj (n) NULL +#endif #ifdef CONFIG_OF_DYNAMIC extern struct device_node *of_node_get (struct device_node *node);
Signature · 1 line
2.11.0
Quoted 10 lines · Rob Herring
On kernels with a minimal config and a RAM target in the 100s of KB, DT is quite a hog of runtime memory usage. How much is dependent on how many nodes and properties in the DT which have a corresponding struct
device_nodeand struct property in the kernel. Just skipping disabled nodes saves a lot by not creating thedevice_nodesin the first place[1], but there's more low hanging fruit by making some of the fields in struct property and structdevice_nodeoptional. With the changes here, the memory usage goes from 17KB to under 8KB on QEMU's ARM virt machine which is a relatively small DT.
Quoted 7 lines
The majority of the diffstat here is just moving all the kobject/sysfs related code to its own file so we can avoid adding a bunch of ifdefs.
There's more drastic approaches we could take such as doing the unflattening at build time and storing the bulk of the unflattened tree as const data. Grant also has some ideas on storing properties as ids instead. He's explained it to me, but I still don't understand it.
It was a two part idea. The first part is to start using numerical IDs for property names instead of strings. The DTB format kind of already does this because all the property names are stored in the string table at the end of the dtb. The actual properties just reference an offset into the string table. If we predefined ids for some property names (reg, compatible, status, etc.), then we could have a variant of the of_property_* API to find properties by numerical ID instead of by strcmp().
The second idea is to eliminate struct property entirely and always store a node's properties as a single DTB tagged list blob. It would reduce the space required at the expense of processing time when parsing the blob. The upshot is the property list could be left in a flash-resident copy of the DTB.
Details need to be investigated, and it would be much more invasive than what Rob has been able to do here. It may not be worth the effort.
g.
Quoted 9 lines · Rob Herring
On kernels with a minimal config and a RAM target in the 100s of KB, DT is quite a hog of runtime memory usage. How much is dependent on how many nodes and properties in the DT which have a corresponding struct
device_nodeand struct property in the kernel. Just skipping disabled nodes saves a lot by not creating thedevice_nodesin the first place[1], but there's more low hanging fruit by making some of the fields in struct property and structdevice_nodeoptional. With the changes here, the memory usage goes from 17KB to under 8KB on QEMU's ARM virt machine which is a relatively small DT.
My test case went from 118072 bytes ddown to 21548 bytes with this series.
Nicolas
Rob Herring
maintainerQuoted 12 lines · Nicolas Pitre
Quoted 9 lines · Rob Herring
On kernels with a minimal config and a RAM target in the 100s of KB, DT is quite a hog of runtime memory usage. How much is dependent on how many nodes and properties in the DT which have a corresponding struct
device_nodeand struct property in the kernel. Just skipping disabled nodes saves a lot by not creating thedevice_nodesin the first place[1], but there's more low hanging fruit by making some of the fields in struct property and structdevice_nodeoptional. With the changes here, the memory usage goes from 17KB to under 8KB on QEMU's ARM virt machine which is a relatively small DT.
Quoted 2 lines
My test case went from 118072 bytes ddown to 21548 bytes with this series.
Did that include the changes for FDT skipping status=disabled and to stop storing the full path of every node? With those 2 alone you said it dropped to 25K.
Quoted 1 line
Tested-by Nicolas Pitre <[email protected]>
Thanks.
Rob
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 13 lines · Rob Herring
Quoted 12 lines · Nicolas Pitre
Quoted 9 lines · Rob Herring
On kernels with a minimal config and a RAM target in the 100s of KB, DT is quite a hog of runtime memory usage. How much is dependent on how many nodes and properties in the DT which have a corresponding struct
device_nodeand struct property in the kernel. Just skipping disabled nodes saves a lot by not creating thedevice_nodesin the first place[1], but there's more low hanging fruit by making some of the fields in struct property and structdevice_nodeoptional. With the changes here, the memory usage goes from 17KB to under 8KB on QEMU's ARM virt machine which is a relatively small DT.
Quoted 6 lines
Quoted 2 lines
My test case went from 118072 bytes ddown to 21548 bytes with this series.
Did that include the changes for FDT skipping status=disabled and to stop storing the full path of every node? With those 2 alone you said it dropped to 25K.
No -- that's with those 6 patches only.
Adding the other 2 patches on top and it is down to 11732 bytes.
Nicolas
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 30 lines · Rob Herring
On kernels with a minimal config and a RAM target in the 100s of KB, DT is quite a hog of runtime memory usage. How much is dependent on how many nodes and properties in the DT which have a corresponding struct
device_nodeand struct property in the kernel. Just skipping disabled nodes saves a lot by not creating thedevice_nodesin the first place[1], but there's more low hanging fruit by making some of the fields in struct property and structdevice_nodeoptional. With the changes here, the memory usage goes from 17KB to under 8KB on QEMU's ARM virt machine which is a relatively small DT.The majority of the diffstat here is just moving all the kobject/sysfs related code to its own file so we can avoid adding a bunch of ifdefs.
There's more drastic approaches we could take such as doing the unflattening at build time and storing the bulk of the unflattened tree as const data. Grant also has some ideas on storing properties as ids instead. He's explained it to me, but I still don't understand it.
Rob
[1] https://patchwork.kernel.org/patch/9983203/
Rob Herring (6):
of: fix missing kobject init for !SYSFS &&OF_DYNAMICconfig
of: remove struct property.unique_id for FDT
of: make struct property_flagsfield configurable
of: movekobj_to_device_node()intodynamic.c
of: wrap accesses todevice_nodekobject
of: make kobject andbin_attributesupport configurable
Quoted 11 lines
drivers/base/core.c | 2 +- drivers/of/Kconfig | 4 ++ drivers/of/Makefile | 1 + drivers/of/base.c | 133 --------------------------------------- drivers/of/dynamic.c | 27 ++------ drivers/of/kobj.c | 164 ++++++++++++++++++++++++++++++++++++++++++++++++ drivers/of/of_private.h | 29 +++++++-- include/linux/of.h | 28 +++++---- 8 files changed, 215 insertions(+), 173 deletions(-) create mode 100644 drivers/of/kobj.c
For the entire series:
I don't know if there will be any conflicts between my series "[PATCH 00/12] of: overlay: clean up device tree overlay code" and this series.
If you choose to apply this series first, I will make any needed changes to my series.
-Frank
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html