linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vincent Mailhol <mailhol@kernel.org>
To: Marc Kleine-Budde <mkl@pengutronix.de>,
	 Oliver Hartkopp <socketcan@hartkopp.net>
Cc: "Vincent Mailhol" <mailhol@kernel.org>,
	"Stéphane Grosjean" <stephane.grosjean@hms-networks.com>,
	"Robert Nawrath" <mbro1689@gmail.com>,
	"Minh Le" <minh.le.aj@renesas.com>,
	"Duy Nguyen" <duy.nguyen.rh@renesas.com>,
	linux-can@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v4 04/20] can: netlink: refactor can_validate_bittiming()
Date: Tue, 23 Sep 2025 15:58:29 +0900	[thread overview]
Message-ID: <20250923-canxl-netlink-prep-v4-4-e720d28f66fe@kernel.org> (raw)
In-Reply-To: <20250923-canxl-netlink-prep-v4-0-e720d28f66fe@kernel.org>

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>
---
Changelog:

v2 -> v3:

  - add a static_assert() to prove that the alignment is correct.
---
 drivers/net/can/dev/netlink.c | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)

diff --git a/drivers/net/can/dev/netlink.c b/drivers/net/can/dev/netlink.c
index 248f607e3864ffbda6f0b8daf4e2484179cf9cd5..13555253e789ec6ca9c8c30571c990ad6bfde770 100644
--- a/drivers/net/can/dev/netlink.c
+++ b/drivers/net/can/dev/netlink.c
@@ -36,13 +36,21 @@ static const struct nla_policy can_tdc_policy[IFLA_CAN_TDC_MAX + 1] = {
 	[IFLA_CAN_TDC_TDCF] = { .type = NLA_U32 },
 };
 
-static int can_validate_bittiming(const struct can_bittiming *bt,
-				  struct netlink_ext_ack *extack)
+static int can_validate_bittiming(struct nlattr *data[],
+				  struct netlink_ext_ack *extack,
+				  int ifla_can_bittiming)
 {
+	struct can_bittiming *bt;
+
+	if (!data[ifla_can_bittiming])
+		return 0;
+
+	static_assert(__alignof__(*bt) <= NLA_ALIGNTO);
+	bt = nla_data(data[ifla_can_bittiming]);
+
 	/* sample point is in one-tenth of a percent */
 	if (bt->sample_point >= 1000) {
 		NL_SET_ERR_MSG(extack, "sample point must be between 0 and 100%");
-
 		return -EINVAL;
 	}
 
@@ -105,14 +113,9 @@ static int can_validate(struct nlattr *tb[], struct nlattr *data[],
 		}
 	}
 
-	if (data[IFLA_CAN_BITTIMING]) {
-		struct can_bittiming bt;
-
-		memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
-		err = can_validate_bittiming(&bt, extack);
-		if (err)
-			return err;
-	}
+	err = can_validate_bittiming(data, extack, IFLA_CAN_BITTIMING);
+	if (err)
+		return err;
 
 	if (is_can_fd) {
 		if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
@@ -124,14 +127,9 @@ static int can_validate(struct nlattr *tb[], struct nlattr *data[],
 			return -EOPNOTSUPP;
 	}
 
-	if (data[IFLA_CAN_DATA_BITTIMING]) {
-		struct can_bittiming bt;
-
-		memcpy(&bt, nla_data(data[IFLA_CAN_DATA_BITTIMING]), sizeof(bt));
-		err = can_validate_bittiming(&bt, extack);
-		if (err)
-			return err;
-	}
+	err = can_validate_bittiming(data, extack, IFLA_CAN_DATA_BITTIMING);
+	if (err)
+		return err;
 
 	return 0;
 }

-- 
2.49.1


  parent reply	other threads:[~2025-09-23  6:59 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-23  6:58 [PATCH v4 00/20] can: netlink: preparation before introduction of CAN XL step 3/3 Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 01/20] can: dev: move struct data_bittiming_params to linux/can/bittiming.h Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 02/20] can: dev: make can_get_relative_tdco() FD agnostic and move it to bittiming.h Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 03/20] can: netlink: document which symbols are FD specific Vincent Mailhol
2025-09-23  6:58 ` Vincent Mailhol [this message]
2025-09-23  6:58 ` [PATCH v4 05/20] can: netlink: add can_validate_tdc() Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 06/20] can: netlink: add can_validate_databittiming() Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 07/20] can: netlink: refactor CAN_CTRLMODE_TDC_{AUTO,MANUAL} flag reset logic Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 08/20] can: netlink: remove useless check in can_tdc_changelink() Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 09/20] can: netlink: make can_tdc_changelink() FD agnostic Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 10/20] can: netlink: add can_dtb_changelink() Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 11/20] can: netlink: add can_ctrlmode_changelink() Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 12/20] can: netlink: make can_tdc_get_size() FD agnostic Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 13/20] can: netlink: add can_data_bittiming_get_size() Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 14/20] can: netlink: add can_bittiming_fill_info() Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 15/20] can: netlink: add can_bittiming_const_fill_info() Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 16/20] can: netlink: add can_bitrate_const_fill_info() Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 17/20] can: netlink: make can_tdc_fill_info() FD agnostic Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 18/20] can: calc_bittiming: make can_calc_tdco() " Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 19/20] can: dev: add can_get_ctrlmode_str() Vincent Mailhol
2025-09-23  6:58 ` [PATCH v4 20/20] can: netlink: add userland error messages Vincent Mailhol
2025-09-23  9:09 ` [PATCH v4 00/20] can: netlink: preparation before introduction of CAN XL step 3/3 Marc Kleine-Budde

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=20250923-canxl-netlink-prep-v4-4-e720d28f66fe@kernel.org \
    --to=mailhol@kernel.org \
    --cc=duy.nguyen.rh@renesas.com \
    --cc=linux-can@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mbro1689@gmail.com \
    --cc=minh.le.aj@renesas.com \
    --cc=mkl@pengutronix.de \
    --cc=socketcan@hartkopp.net \
    --cc=stephane.grosjean@hms-networks.com \
    /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;
as well as URLs for NNTP newsgroup(s).