* [PATCH] kobject: Reorder fields in 'struct kobject'
@ 2023-07-08 10:13 Christophe JAILLET
2023-07-08 11:17 ` Greg Kroah-Hartman
0 siblings, 1 reply; 2+ messages in thread
From: Christophe JAILLET @ 2023-07-08 10:13 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki
Cc: linux-kernel, kernel-janitors, Christophe JAILLET
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@wanadoo.fr>
---
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
};
__printf(2, 3) int kobject_set_name(struct kobject *kobj, const char *name, ...);
--
2.34.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] kobject: Reorder fields in 'struct kobject'
2023-07-08 10:13 [PATCH] kobject: Reorder fields in 'struct kobject' Christophe JAILLET
@ 2023-07-08 11:17 ` Greg Kroah-Hartman
0 siblings, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2023-07-08 11:17 UTC (permalink / raw)
To: Christophe JAILLET; +Cc: Rafael J. Wysocki, linux-kernel, kernel-janitors
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@wanadoo.fr>
> ---
> 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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-07-08 11:17 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-08 10:13 [PATCH] kobject: Reorder fields in 'struct kobject' Christophe JAILLET
2023-07-08 11:17 ` Greg Kroah-Hartman
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox