Re: [PATCH] kobject: Reorder fields in 'struct kobject'

From: Greg Kroah-Hartman
Date: Sat Jul 08 2023 - 07:17:34 EST


On Sat, Jul 08, 2023 at 12:13:45PM +0200, Christophe JAILLET wrote:
> Group some variables based on their sizes to reduce hole and avoid padding.
> On x86_64, this shrinks the size of 'struct kobject' from 256 to 244 bytes.
>
> This structure is often included in some other structures. So these other
> structures will also benefit from this 8 bytes saving.
>
> This is especially nice for structure like 'cma_kobject' or 'class_dir'
> that are now 256 bytes long. When they are kzalloc()'ed, 256 bytes are
> allocated, instead of 512.
>
> Signed-off-by: Christophe JAILLET <christophe.jaillet@xxxxxxxxxx>
> ---
> Try to see how often this is included in another struct. Certainly not
> perfect, but gives an idea.
> git grep -P \\tstruct\ kobj[^*]*$ | wc -l
> 163
>
>
> Using pahole
>
> Before:
> ======
> struct kobject {
> const char * name; /* 0 8 */
> struct list_head entry; /* 8 16 */
> struct kobject * parent; /* 24 8 */
> struct kset * kset; /* 32 8 */
> const struct kobj_type * ktype; /* 40 8 */
> struct kernfs_node * sd; /* 48 8 */
> struct kref kref; /* 56 4 */
>
> /* XXX 4 bytes hole, try to pack */
>
> /* --- cacheline 1 boundary (64 bytes) --- */
> struct delayed_work release; /* 64 184 */
>
> /* XXX last struct has 4 bytes of padding */
>
> /* --- cacheline 3 boundary (192 bytes) was 56 bytes ago --- */
> unsigned int state_initialized:1; /* 248: 0 4 */
> unsigned int state_in_sysfs:1; /* 248: 1 4 */
> unsigned int state_add_uevent_sent:1; /* 248: 2 4 */
> unsigned int state_remove_uevent_sent:1; /* 248: 3 4 */
> unsigned int uevent_suppress:1; /* 248: 4 4 */
>
> /* size: 256, cachelines: 4, members: 13 */
> /* sum members: 244, holes: 1, sum holes: 4 */
> /* sum bitfield members: 5 bits (0 bytes) */
> /* padding: 4 */
> /* paddings: 1, sum paddings: 4 */
> /* bit_padding: 27 bits */
> };
>
>
> After:
> =====
> struct kobject {
> const char * name; /* 0 8 */
> struct list_head entry; /* 8 16 */
> struct kobject * parent; /* 24 8 */
> struct kset * kset; /* 32 8 */
> const struct kobj_type * ktype; /* 40 8 */
> struct kernfs_node * sd; /* 48 8 */
> struct kref kref; /* 56 4 */
> unsigned int state_initialized:1; /* 60: 0 4 */
> unsigned int state_in_sysfs:1; /* 60: 1 4 */
> unsigned int state_add_uevent_sent:1; /* 60: 2 4 */
> unsigned int state_remove_uevent_sent:1; /* 60: 3 4 */
> unsigned int uevent_suppress:1; /* 60: 4 4 */
>
> /* XXX 27 bits hole, try to pack */
>
> /* --- cacheline 1 boundary (64 bytes) --- */
> struct delayed_work release; /* 64 184 */
>
> /* XXX last struct has 4 bytes of padding */
>
> /* size: 248, cachelines: 4, members: 13 */
> /* sum members: 244 */
> /* sum bitfield members: 5 bits, bit holes: 1, sum bit holes: 27 bits */
> /* paddings: 1, sum paddings: 4 */
> /* last cacheline: 56 bytes */
> };
> ---
> include/linux/kobject.h | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/include/linux/kobject.h b/include/linux/kobject.h
> index c392c811d9ad..c30affcc43b4 100644
> --- a/include/linux/kobject.h
> +++ b/include/linux/kobject.h
> @@ -69,14 +69,16 @@ struct kobject {
> const struct kobj_type *ktype;
> struct kernfs_node *sd; /* sysfs directory entry */
> struct kref kref;
> -#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
> - struct delayed_work release;
> -#endif
> +
> unsigned int state_initialized:1;
> unsigned int state_in_sysfs:1;
> unsigned int state_add_uevent_sent:1;
> unsigned int state_remove_uevent_sent:1;
> unsigned int uevent_suppress:1;
> +
> +#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
> + struct delayed_work release;
> +#endif
> };

Very nice, thanks for doing this!

For some reason I thought I did this already, but it turned out to be in
an old branch of mine that never saw the light of day as I went down
some other path trying to do something else. So many thanks for this,
I'll queue it up after 6.5-rc1 is out.

greg k-h