Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

can: netlink: refactor can_validate_bittiming()

Whenever can_validate_bittiming() is called, it is always preceded by
some boilerplate code which was copy pasted all over the place. Move
that repeated code directly inside can_validate_bittiming().

Finally, the mempcy() is not needed: the nla attributes are four bytes
aligned which is just enough for struct can_bittiming. Add a
static_assert() to document that the alignment is correct and just use
the pointer returned by nla_data() as-is.

Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
Link: https://patch.msgid.link/20250923-canxl-netlink-prep-v4-4-e720d28f66fe@kernel.org
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

authored by

Vincent Mailhol and committed by
Marc Kleine-Budde
f5ae5a75 94040a8f

+17 -19
+17 -19
drivers/net/can/dev/netlink.c
··· 36 36 [IFLA_CAN_TDC_TDCF] = { .type = NLA_U32 }, 37 37 }; 38 38 39 - static int can_validate_bittiming(const struct can_bittiming *bt, 40 - struct netlink_ext_ack *extack) 39 + static int can_validate_bittiming(struct nlattr *data[], 40 + struct netlink_ext_ack *extack, 41 + int ifla_can_bittiming) 41 42 { 43 + struct can_bittiming *bt; 44 + 45 + if (!data[ifla_can_bittiming]) 46 + return 0; 47 + 48 + static_assert(__alignof__(*bt) <= NLA_ALIGNTO); 49 + bt = nla_data(data[ifla_can_bittiming]); 50 + 42 51 /* sample point is in one-tenth of a percent */ 43 52 if (bt->sample_point >= 1000) { 44 53 NL_SET_ERR_MSG(extack, "sample point must be between 0 and 100%"); 45 - 46 54 return -EINVAL; 47 55 } 48 56 ··· 113 105 } 114 106 } 115 107 116 - if (data[IFLA_CAN_BITTIMING]) { 117 - struct can_bittiming bt; 118 - 119 - memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt)); 120 - err = can_validate_bittiming(&bt, extack); 121 - if (err) 122 - return err; 123 - } 108 + err = can_validate_bittiming(data, extack, IFLA_CAN_BITTIMING); 109 + if (err) 110 + return err; 124 111 125 112 if (is_can_fd) { 126 113 if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING]) ··· 127 124 return -EOPNOTSUPP; 128 125 } 129 126 130 - if (data[IFLA_CAN_DATA_BITTIMING]) { 131 - struct can_bittiming bt; 132 - 133 - memcpy(&bt, nla_data(data[IFLA_CAN_DATA_BITTIMING]), sizeof(bt)); 134 - err = can_validate_bittiming(&bt, extack); 135 - if (err) 136 - return err; 137 - } 127 + err = can_validate_bittiming(data, extack, IFLA_CAN_DATA_BITTIMING); 128 + if (err) 129 + return err; 138 130 139 131 return 0; 140 132 }