Re: Idea for reducing sysfs memory usage

From: David Ahern
Date: Tue Feb 16 2016 - 19:54:06 EST


On 2/16/16 5:47 PM, Greg Kroah-Hartman wrote:
How many sysfs entries are you creating for that 20kb? And how did you
measure it? If you don't access the files, the backing store is not
allocated, saving you a lot of memory. If you do access it, it will be
freed later on afterward, so it's sometimes really hard to measure this
accurately.

Using the kmem tracepoints:

perf record -g \
-e kmem:kmalloc,kmem:kmem_cache_alloc,kmem:mm_page_alloc,kmem:kfree,kmem:kmem_cache_free,kmem:mm_page_free -- ip link add dummy1 type dummy

First go round I painstakenly examined each allocation and location.
After that I modified perf-kmem to do the summary for me for other use cases.


I can forward the full notes if you want them. Here's the first few and last few:

################
# sysfs -- start

# directory

# device_add -> kobject_add -- register with generic layer

ip 1732 [005] 27128.556548: kmem:kmalloc: ptr=0xffff8800ba9a5d60 bytes_req=7 bytes_alloc=32 gfp_flags=37748928
ffffffff8113e5ce __kmalloc_track_caller ([kernel.kallsyms])
ffffffff81116643 kstrdup ([kernel.kallsyms])
ffffffff8111667c kstrdup_const ([kernel.kallsyms])
ffffffff811a6bf5 __kernfs_new_node ([kernel.kallsyms])
ffffffff811a7397 kernfs_new_node ([kernel.kallsyms])
ffffffff811a7813 kernfs_create_dir_ns ([kernel.kallsyms])
ffffffff811a95f0 sysfs_create_dir_ns ([kernel.kallsyms])
ffffffff8126f76f kobject_add_internal ([kernel.kallsyms])
ffffffff8126fa62 kobject_add ([kernel.kallsyms])
ffffffff81319317 device_add ([kernel.kallsyms])
ffffffff81408528 netdev_register_kobject ([kernel.kallsyms])
ffffffff813f2cbb register_netdevice ([kernel.kallsyms])
ffffffff81401314 rtnl_newlink ([kernel.kallsyms])

# directory

ip 1732 [005] 27128.556555: kmem:kmem_cache_alloc: ptr=0xffff8801399d6018 bytes_req=168 bytes_alloc=168 gfp_flags=37781696
ffffffff8113ee09 kmem_cache_alloc ([kernel.kallsyms])
ffffffff811a6c0e __kernfs_new_node ([kernel.kallsyms])
ffffffff811a7397 kernfs_new_node ([kernel.kallsyms])
ffffffff811a7813 kernfs_create_dir_ns ([kernel.kallsyms])
ffffffff811a95f0 sysfs_create_dir_ns ([kernel.kallsyms])
ffffffff8126f76f kobject_add_internal ([kernel.kallsyms])
ffffffff8126fa62 kobject_add ([kernel.kallsyms])
ffffffff81319317 device_add ([kernel.kallsyms])
ffffffff81408528 netdev_register_kobject ([kernel.kallsyms])
ffffffff813f2cbb register_netdevice ([kernel.kallsyms])
ffffffff81401314 rtnl_newlink ([kernel.kallsyms])

# device_add -> device_create_file(dev, &dev_attr_uevent) --> sysfs_create_file(&dev->kobj, &attr->attr)

ip 1732 [005] 27128.556583: kmem:kmem_cache_alloc: ptr=0xffff8801399d60c0 bytes_req=168 bytes_alloc=168 gfp_flags=37781696
ffffffff8113ee09 kmem_cache_alloc ([kernel.kallsyms])
ffffffff811a6c0e __kernfs_new_node ([kernel.kallsyms])
ffffffff811a7397 kernfs_new_node ([kernel.kallsyms])
ffffffff811a8ac7 __kernfs_create_file ([kernel.kallsyms])
ffffffff811a9310 sysfs_add_file_mode_ns ([kernel.kallsyms])
ffffffff811a9370 sysfs_create_file_ns ([kernel.kallsyms])
ffffffff81317f3b device_create_file ([kernel.kallsyms])
ffffffff81319342 device_add ([kernel.kallsyms])
ffffffff81408528 netdev_register_kobject ([kernel.kallsyms])
ffffffff813f2cbb register_netdevice ([kernel.kallsyms])
ffffffff81401314 rtnl_newlink ([kernel.kallsyms])

...

ip 1732 [005] 27128.560734: kmem:kmem_cache_alloc: ptr=0xffff8800b94758a0 bytes_req=168 bytes_alloc=168 gfp_flags=37781696
ffffffff8113ee09 kmem_cache_alloc ([kernel.kallsyms])
ffffffff811a6c0e __kernfs_new_node ([kernel.kallsyms])
ffffffff811a7397 kernfs_new_node ([kernel.kallsyms])
ffffffff811a8ac7 __kernfs_create_file ([kernel.kallsyms])
ffffffff811a9310 sysfs_add_file_mode_ns ([kernel.kallsyms])
ffffffff811a9cd2 internal_create_group ([kernel.kallsyms])
ffffffff811a9e20 sysfs_create_group ([kernel.kallsyms])
ffffffff814083ca netdev_queue_update_kobjects ([kernel.kallsyms])
ffffffff81408583 netdev_register_kobject ([kernel.kallsyms])
ffffffff813f2cbb register_netdevice ([kernel.kallsyms])
ffffffff81401314 rtnl_newlink ([kernel.kallsyms])

ip 1732 [005] 27128.560789: kmem:kmem_cache_alloc: ptr=0xffff880139cd0400 bytes_req=256 bytes_alloc=256 gfp_flags=37748928
ffffffff8113ee09 kmem_cache_alloc ([kernel.kallsyms])
ffffffff813df48a __alloc_skb ([kernel.kallsyms])
ffffffff81270485 kobject_uevent_env ([kernel.kallsyms])
ffffffff81270703 kobject_uevent ([kernel.kallsyms])
ffffffff814083da netdev_queue_update_kobjects ([kernel.kallsyms])
ffffffff81408583 netdev_register_kobject ([kernel.kallsyms])
ffffffff813f2cbb register_netdevice ([kernel.kallsyms])
ffffffff81401314 rtnl_newlink ([kernel.kallsyms])

ip 1732 [005] 27128.560798: kmem:kmalloc: ptr=0xffff8800bab97c00 bytes_req=512 bytes_alloc=512 gfp_flags=37814976
ffffffff8113e5ce __kmalloc_track_caller ([kernel.kallsyms])
ffffffff813def36 __kmalloc_reserve.isra.50 ([kernel.kallsyms])
ffffffff813df4bc __alloc_skb ([kernel.kallsyms])
ffffffff81270485 kobject_uevent_env ([kernel.kallsyms])
ffffffff81270703 kobject_uevent ([kernel.kallsyms])
ffffffff814083da netdev_queue_update_kobjects ([kernel.kallsyms])
ffffffff81408583 netdev_register_kobject ([kernel.kallsyms])
ffffffff813f2cbb register_netdevice ([kernel.kallsyms])
ffffffff81401314 rtnl_newlink ([kernel.kallsyms])

# end netdev_register_kobject
###############################