public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>,
	linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org
Subject: Re: [PATCH] kobject: Reorder fields in 'struct kobject'
Date: Sat, 8 Jul 2023 13:17:25 +0200	[thread overview]
Message-ID: <2023070840-backyard-outgrow-e4e7@gregkh> (raw)
In-Reply-To: <6c7d1e3005dbec5483bdb9b7b60071175bf7bf70.1688811201.git.christophe.jaillet@wanadoo.fr>

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

      reply	other threads:[~2023-07-08 11:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-08 10:13 [PATCH] kobject: Reorder fields in 'struct kobject' Christophe JAILLET
2023-07-08 11:17 ` Greg Kroah-Hartman [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2023070840-backyard-outgrow-e4e7@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox