Skip to content

Commit 890e519

Browse files
vincent-mailholmarckleinebudde
authored andcommitted
can: raw: use bitfields to store flags in struct raw_sock
The bound, loopback, recv_own_msgs, fd_frames, xl_frames and join_filters fields of struct raw_sock just need to store one bit of information. Declare all those members as a bitfields of type unsigned int and width one bit. Add a temporary variable to raw_setsockopt() and raw_getsockopt() to make the conversion between the stored bits and the socket interface. This reduces the size of struct raw_sock by sixteen 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 */ int loopback; /* 808 4 */ int recv_own_msgs; /* 812 4 */ int fd_frames; /* 816 4 */ int xl_frames; /* 820 4 */ struct can_raw_vcid_options raw_vcid_opts; /* 824 4 */ canid_t tx_vcid_shifted; /* 828 4 */ /* --- cacheline 13 boundary (832 bytes) --- */ canid_t rx_vcid_shifted; /* 832 4 */ canid_t rx_vcid_mask_shifted; /* 836 4 */ int join_filters; /* 840 4 */ int count; /* 844 4 */ struct can_filter dfilter; /* 848 8 */ struct can_filter * filter; /* 856 8 */ can_err_mask_t err_mask; /* 864 4 */ /* XXX 4 bytes hole, try to pack */ struct uniqframe * uniq; /* 872 8 */ /* size: 880, cachelines: 14, members: 20 */ /* sum members: 876, holes: 1, sum holes: 4 */ /* member types with bit holes: 1, total: 1 */ /* forced alignments: 1 */ /* last cacheline: 48 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 ifindex; /* 776 4 */ /* XXX 4 bytes hole, try to pack */ struct net_device * dev; /* 784 8 */ netdevice_tracker dev_tracker; /* 792 0 */ struct list_head notifier; /* 792 16 */ unsigned int bound:1; /* 808: 0 4 */ unsigned int loopback:1; /* 808: 1 4 */ unsigned int recv_own_msgs:1; /* 808: 2 4 */ unsigned int fd_frames:1; /* 808: 3 4 */ unsigned int xl_frames:1; /* 808: 4 4 */ unsigned int join_filters:1; /* 808: 5 4 */ /* XXX 2 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 count; /* 828 4 */ /* --- cacheline 13 boundary (832 bytes) --- */ struct can_filter dfilter; /* 832 8 */ struct can_filter * filter; /* 840 8 */ can_err_mask_t err_mask; /* 848 4 */ /* XXX 4 bytes hole, try to pack */ struct uniqframe * uniq; /* 856 8 */ /* size: 864, cachelines: 14, members: 20 */ /* sum members: 852, holes: 3, sum holes: 11 */ /* sum bitfield members: 6 bits, bit holes: 1, sum bit holes: 2 bits */ /* member types with bit holes: 1, total: 1 */ /* forced alignments: 1 */ /* last cacheline: 32 bytes */ } __attribute__((__aligned__(8))); Acked-by: Oliver Hartkopp <socketcan@hartkopp.net> Signed-off-by: Vincent Mailhol <mailhol@kernel.org> Link: https://patch.msgid.link/20250917-can-raw-repack-v2-2-395e8b3a4437@kernel.org Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent fc8418e commit 890e519

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

net/can/raw.c

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,20 @@ struct uniqframe {
8282

8383
struct raw_sock {
8484
struct sock sk;
85-
int bound;
8685
int ifindex;
8786
struct net_device *dev;
8887
netdevice_tracker dev_tracker;
8988
struct list_head notifier;
90-
int loopback;
91-
int recv_own_msgs;
92-
int fd_frames;
93-
int xl_frames;
89+
unsigned int bound:1;
90+
unsigned int loopback:1;
91+
unsigned int recv_own_msgs:1;
92+
unsigned int fd_frames:1;
93+
unsigned int xl_frames:1;
94+
unsigned int join_filters:1;
9495
struct can_raw_vcid_options raw_vcid_opts;
9596
canid_t tx_vcid_shifted;
9697
canid_t rx_vcid_shifted;
9798
canid_t rx_vcid_mask_shifted;
98-
int join_filters;
9999
int count; /* number of active filters */
100100
struct can_filter dfilter; /* default/single filter */
101101
struct can_filter *filter; /* pointer to filter(s) */
@@ -560,8 +560,8 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
560560
struct can_filter sfilter; /* single filter */
561561
struct net_device *dev = NULL;
562562
can_err_mask_t err_mask = 0;
563-
int fd_frames;
564563
int count = 0;
564+
int flag;
565565
int err = 0;
566566

567567
if (level != SOL_CAN_RAW)
@@ -682,44 +682,48 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
682682
break;
683683

684684
case CAN_RAW_LOOPBACK:
685-
if (optlen != sizeof(ro->loopback))
685+
if (optlen != sizeof(flag))
686686
return -EINVAL;
687687

688-
if (copy_from_sockptr(&ro->loopback, optval, optlen))
688+
if (copy_from_sockptr(&flag, optval, optlen))
689689
return -EFAULT;
690690

691+
ro->loopback = !!flag;
691692
break;
692693

693694
case CAN_RAW_RECV_OWN_MSGS:
694-
if (optlen != sizeof(ro->recv_own_msgs))
695+
if (optlen != sizeof(flag))
695696
return -EINVAL;
696697

697-
if (copy_from_sockptr(&ro->recv_own_msgs, optval, optlen))
698+
if (copy_from_sockptr(&flag, optval, optlen))
698699
return -EFAULT;
699700

701+
ro->recv_own_msgs = !!flag;
700702
break;
701703

702704
case CAN_RAW_FD_FRAMES:
703-
if (optlen != sizeof(fd_frames))
705+
if (optlen != sizeof(flag))
704706
return -EINVAL;
705707

706-
if (copy_from_sockptr(&fd_frames, optval, optlen))
708+
if (copy_from_sockptr(&flag, optval, optlen))
707709
return -EFAULT;
708710

709711
/* Enabling CAN XL includes CAN FD */
710-
if (ro->xl_frames && !fd_frames)
712+
if (ro->xl_frames && !flag)
711713
return -EINVAL;
712714

713-
ro->fd_frames = fd_frames;
715+
ro->fd_frames = !!flag;
714716
break;
715717

716718
case CAN_RAW_XL_FRAMES:
717-
if (optlen != sizeof(ro->xl_frames))
719+
if (optlen != sizeof(flag))
718720
return -EINVAL;
719721

720-
if (copy_from_sockptr(&ro->xl_frames, optval, optlen))
722+
if (copy_from_sockptr(&flag, optval, optlen))
721723
return -EFAULT;
722724

725+
ro->xl_frames = !!flag;
726+
723727
/* Enabling CAN XL includes CAN FD */
724728
if (ro->xl_frames)
725729
ro->fd_frames = ro->xl_frames;
@@ -739,12 +743,13 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
739743
break;
740744

741745
case CAN_RAW_JOIN_FILTERS:
742-
if (optlen != sizeof(ro->join_filters))
746+
if (optlen != sizeof(flag))
743747
return -EINVAL;
744748

745-
if (copy_from_sockptr(&ro->join_filters, optval, optlen))
749+
if (copy_from_sockptr(&flag, optval, optlen))
746750
return -EFAULT;
747751

752+
ro->join_filters = !!flag;
748753
break;
749754

750755
default:
@@ -758,6 +763,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
758763
{
759764
struct sock *sk = sock->sk;
760765
struct raw_sock *ro = raw_sk(sk);
766+
int flag;
761767
int len;
762768
void *val;
763769

@@ -806,25 +812,29 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
806812
case CAN_RAW_LOOPBACK:
807813
if (len > sizeof(int))
808814
len = sizeof(int);
809-
val = &ro->loopback;
815+
flag = ro->loopback;
816+
val = &flag;
810817
break;
811818

812819
case CAN_RAW_RECV_OWN_MSGS:
813820
if (len > sizeof(int))
814821
len = sizeof(int);
815-
val = &ro->recv_own_msgs;
822+
flag = ro->recv_own_msgs;
823+
val = &flag;
816824
break;
817825

818826
case CAN_RAW_FD_FRAMES:
819827
if (len > sizeof(int))
820828
len = sizeof(int);
821-
val = &ro->fd_frames;
829+
flag = ro->fd_frames;
830+
val = &flag;
822831
break;
823832

824833
case CAN_RAW_XL_FRAMES:
825834
if (len > sizeof(int))
826835
len = sizeof(int);
827-
val = &ro->xl_frames;
836+
flag = ro->xl_frames;
837+
val = &flag;
828838
break;
829839

830840
case CAN_RAW_XL_VCID_OPTS: {
@@ -849,7 +859,8 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
849859
case CAN_RAW_JOIN_FILTERS:
850860
if (len > sizeof(int))
851861
len = sizeof(int);
852-
val = &ro->join_filters;
862+
flag = ro->join_filters;
863+
val = &flag;
853864
break;
854865

855866
default:

0 commit comments

Comments
 (0)