[PATCH 3/3] can: raw: reorder struct raw_sock's members to optimise packing

Vincent Mailhol posted 3 patches 2 weeks, 3 days ago
There is a newer version of this series
[PATCH 3/3] can: raw: reorder struct raw_sock's members to optimise packing
Posted by Vincent Mailhol 2 weeks, 3 days ago
struct raw_sock has several holes. Reorder the fields to save 8 bytes.

Statistics before:

  $ pahole --class_name=raw_sock net/can/raw.o
  struct raw_sock {
  	struct sock                sk __attribute__((__aligned__(8))); /*     0   776 */

  	/* XXX last struct has 1 bit hole */

  	/* --- cacheline 12 boundary (768 bytes) was 8 bytes ago --- */
  	int                        bound;                /*   776     4 */
  	int                        ifindex;              /*   780     4 */
  	struct net_device *        dev;                  /*   784     8 */
  	netdevice_tracker          dev_tracker;          /*   792     0 */
  	struct list_head           notifier;             /*   792    16 */
  	unsigned int               loopback:1;           /*   808: 0  4 */
  	unsigned int               recv_own_msgs:1;      /*   808: 1  4 */
  	unsigned int               fd_frames:1;          /*   808: 2  4 */
  	unsigned int               xl_frames:1;          /*   808: 3  4 */

  	/* XXX 4 bits hole, try to pack */
  	/* Bitfield combined with next fields */

  	struct can_raw_vcid_options raw_vcid_opts;       /*   809     4 */

  	/* XXX 3 bytes hole, try to pack */

  	canid_t                    tx_vcid_shifted;      /*   816     4 */
  	canid_t                    rx_vcid_shifted;      /*   820     4 */
  	canid_t                    rx_vcid_mask_shifted; /*   824     4 */
  	int                        join_filters;         /*   828     4 */
  	/* --- cacheline 13 boundary (832 bytes) --- */
  	int                        count;                /*   832     4 */
  	struct can_filter          dfilter;              /*   836     8 */

  	/* XXX 4 bytes hole, try to pack */

  	struct can_filter *        filter;               /*   848     8 */
  	can_err_mask_t             err_mask;             /*   856     4 */

  	/* XXX 4 bytes hole, try to pack */

  	struct uniqframe *         uniq;                 /*   864     8 */

  	/* size: 872, cachelines: 14, members: 20 */
  	/* sum members: 860, holes: 3, sum holes: 11 */
  	/* sum bitfield members: 4 bits, bit holes: 1, sum bit holes: 4 bits */
  	/* member types with bit holes: 1, total: 1 */
  	/* forced alignments: 1 */
  	/* last cacheline: 40 bytes */
  } __attribute__((__aligned__(8)));

...and after:

  $ pahole --class_name=raw_sock net/can/raw.o
  struct raw_sock {
  	struct sock                sk __attribute__((__aligned__(8))); /*     0   776 */

  	/* XXX last struct has 1 bit hole */

  	/* --- cacheline 12 boundary (768 bytes) was 8 bytes ago --- */
  	int                        bound;                /*   776     4 */
  	int                        ifindex;              /*   780     4 */
  	struct net_device *        dev;                  /*   784     8 */
  	netdevice_tracker          dev_tracker;          /*   792     0 */
  	struct list_head           notifier;             /*   792    16 */
  	struct can_raw_vcid_options raw_vcid_opts;       /*   808     4 */
  	unsigned int               loopback:1;           /*   812: 0  4 */
  	unsigned int               recv_own_msgs:1;      /*   812: 1  4 */
  	unsigned int               fd_frames:1;          /*   812: 2  4 */
  	unsigned int               xl_frames:1;          /*   812: 3  4 */

  	/* XXX 28 bits hole, try to pack */

  	canid_t                    tx_vcid_shifted;      /*   816     4 */
  	canid_t                    rx_vcid_shifted;      /*   820     4 */
  	canid_t                    rx_vcid_mask_shifted; /*   824     4 */
  	can_err_mask_t             err_mask;             /*   828     4 */
  	/* --- cacheline 13 boundary (832 bytes) --- */
  	int                        join_filters;         /*   832     4 */
  	int                        count;                /*   836     4 */
  	struct can_filter          dfilter;              /*   840     8 */
  	struct can_filter *        filter;               /*   848     8 */
  	struct uniqframe *         uniq;                 /*   856     8 */

  	/* size: 864, cachelines: 14, members: 20 */
  	/* sum members: 860 */
  	/* sum bitfield members: 4 bits, bit holes: 1, sum bit holes: 28 bits */
  	/* member types with bit holes: 1, total: 1 */
  	/* forced alignments: 1 */
  	/* last cacheline: 32 bytes */
  } __attribute__((__aligned__(8)));

Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
---
 net/can/raw.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/can/raw.c b/net/can/raw.c
index cec580ecd58e36931d1be05716e6beb9c93aa271..81f5de63bcfaacf3f51670159fb3d1d7d1fc6020 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -87,19 +87,19 @@ struct raw_sock {
 	struct net_device *dev;
 	netdevice_tracker dev_tracker;
 	struct list_head notifier;
+	struct can_raw_vcid_options raw_vcid_opts;
 	unsigned int loopback:1;
 	unsigned int recv_own_msgs:1;
 	unsigned int fd_frames:1;
 	unsigned int xl_frames:1;
-	struct can_raw_vcid_options raw_vcid_opts;
 	canid_t tx_vcid_shifted;
 	canid_t rx_vcid_shifted;
 	canid_t rx_vcid_mask_shifted;
+	can_err_mask_t err_mask;
 	int join_filters;
 	int count;                 /* number of active filters */
 	struct can_filter dfilter; /* default/single filter */
 	struct can_filter *filter; /* pointer to filter(s) */
-	can_err_mask_t err_mask;
 	struct uniqframe __percpu *uniq;
 };
 

-- 
2.49.1
Re: [PATCH 3/3] can: raw: reorder struct raw_sock's members to optimise packing
Posted by Oliver Hartkopp 2 weeks, 2 days ago

On 15.09.25 11:23, Vincent Mailhol wrote:
> struct raw_sock has several holes. Reorder the fields to save 8 bytes.
> 
> Statistics before:
> 
>    $ pahole --class_name=raw_sock net/can/raw.o
>    struct raw_sock {
>    	struct sock                sk __attribute__((__aligned__(8))); /*     0   776 */
> 
>    	/* XXX last struct has 1 bit hole */
> 
>    	/* --- cacheline 12 boundary (768 bytes) was 8 bytes ago --- */
>    	int                        bound;                /*   776     4 */
>    	int                        ifindex;              /*   780     4 */
>    	struct net_device *        dev;                  /*   784     8 */
>    	netdevice_tracker          dev_tracker;          /*   792     0 */
>    	struct list_head           notifier;             /*   792    16 */
>    	unsigned int               loopback:1;           /*   808: 0  4 */
>    	unsigned int               recv_own_msgs:1;      /*   808: 1  4 */
>    	unsigned int               fd_frames:1;          /*   808: 2  4 */
>    	unsigned int               xl_frames:1;          /*   808: 3  4 */
> 
>    	/* XXX 4 bits hole, try to pack */
>    	/* Bitfield combined with next fields */
> 
>    	struct can_raw_vcid_options raw_vcid_opts;       /*   809     4 */
> 
>    	/* XXX 3 bytes hole, try to pack */
> 
>    	canid_t                    tx_vcid_shifted;      /*   816     4 */
>    	canid_t                    rx_vcid_shifted;      /*   820     4 */
>    	canid_t                    rx_vcid_mask_shifted; /*   824     4 */
>    	int                        join_filters;         /*   828     4 */
>    	/* --- cacheline 13 boundary (832 bytes) --- */
>    	int                        count;                /*   832     4 */
>    	struct can_filter          dfilter;              /*   836     8 */
> 
>    	/* XXX 4 bytes hole, try to pack */
> 
>    	struct can_filter *        filter;               /*   848     8 */
>    	can_err_mask_t             err_mask;             /*   856     4 */
> 
>    	/* XXX 4 bytes hole, try to pack */
> 
>    	struct uniqframe *         uniq;                 /*   864     8 */
> 
>    	/* size: 872, cachelines: 14, members: 20 */
>    	/* sum members: 860, holes: 3, sum holes: 11 */
>    	/* sum bitfield members: 4 bits, bit holes: 1, sum bit holes: 4 bits */
>    	/* member types with bit holes: 1, total: 1 */
>    	/* forced alignments: 1 */
>    	/* last cacheline: 40 bytes */
>    } __attribute__((__aligned__(8)));
> 
> ...and after:
> 
>    $ pahole --class_name=raw_sock net/can/raw.o
>    struct raw_sock {
>    	struct sock                sk __attribute__((__aligned__(8))); /*     0   776 */
> 
>    	/* XXX last struct has 1 bit hole */
> 
>    	/* --- cacheline 12 boundary (768 bytes) was 8 bytes ago --- */
>    	int                        bound;                /*   776     4 */
>    	int                        ifindex;              /*   780     4 */
>    	struct net_device *        dev;                  /*   784     8 */
>    	netdevice_tracker          dev_tracker;          /*   792     0 */
>    	struct list_head           notifier;             /*   792    16 */
>    	struct can_raw_vcid_options raw_vcid_opts;       /*   808     4 */
>    	unsigned int               loopback:1;           /*   812: 0  4 */
>    	unsigned int               recv_own_msgs:1;      /*   812: 1  4 */
>    	unsigned int               fd_frames:1;          /*   812: 2  4 */
>    	unsigned int               xl_frames:1;          /*   812: 3  4 */
> 
>    	/* XXX 28 bits hole, try to pack */
> 
>    	canid_t                    tx_vcid_shifted;      /*   816     4 */
>    	canid_t                    rx_vcid_shifted;      /*   820     4 */
>    	canid_t                    rx_vcid_mask_shifted; /*   824     4 */
>    	can_err_mask_t             err_mask;             /*   828     4 */
>    	/* --- cacheline 13 boundary (832 bytes) --- */
>    	int                        join_filters;         /*   832     4 */
>    	int                        count;                /*   836     4 */
>    	struct can_filter          dfilter;              /*   840     8 */
>    	struct can_filter *        filter;               /*   848     8 */
>    	struct uniqframe *         uniq;                 /*   856     8 */
> 
>    	/* size: 864, cachelines: 14, members: 20 */
>    	/* sum members: 860 */
>    	/* sum bitfield members: 4 bits, bit holes: 1, sum bit holes: 28 bits */
>    	/* member types with bit holes: 1, total: 1 */
>    	/* forced alignments: 1 */
>    	/* last cacheline: 32 bytes */
>    } __attribute__((__aligned__(8)));
> 
> Signed-off-by: Vincent Mailhol <mailhol@kernel.org>

Acked-by: Oliver Hartkopp <socketcan@hartkopp.net>

> ---
>   net/can/raw.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/net/can/raw.c b/net/can/raw.c
> index cec580ecd58e36931d1be05716e6beb9c93aa271..81f5de63bcfaacf3f51670159fb3d1d7d1fc6020 100644
> --- a/net/can/raw.c
> +++ b/net/can/raw.c
> @@ -87,19 +87,19 @@ struct raw_sock {
>   	struct net_device *dev;
>   	netdevice_tracker dev_tracker;
>   	struct list_head notifier;
> +	struct can_raw_vcid_options raw_vcid_opts;
>   	unsigned int loopback:1;
>   	unsigned int recv_own_msgs:1;
>   	unsigned int fd_frames:1;
>   	unsigned int xl_frames:1;
> -	struct can_raw_vcid_options raw_vcid_opts;
>   	canid_t tx_vcid_shifted;
>   	canid_t rx_vcid_shifted;
>   	canid_t rx_vcid_mask_shifted;
> +	can_err_mask_t err_mask;
>   	int join_filters;
>   	int count;                 /* number of active filters */
>   	struct can_filter dfilter; /* default/single filter */
>   	struct can_filter *filter; /* pointer to filter(s) */
> -	can_err_mask_t err_mask;
>   	struct uniqframe __percpu *uniq;
>   };
>   
>