[PATCH v2 04/20] can: netlink: refactor can_validate_bittiming()

Vincent Mailhol posted 20 patches 3 weeks, 1 day ago
Re: [PATCH v2 04/20] can: netlink: refactor can_validate_bittiming()
Posted by Marc Kleine-Budde 3 weeks, 1 day ago
On 10.09.2025 15:03:29, Vincent Mailhol wrote:
> 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. Just use the pointer returned by
> nla_data() as-is.

The memcpy()'ed struct is guaranteed to be properly aligned, is this
also the case for the casted nla_data() pointer?

regards,
Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
Re: [PATCH v2 04/20] can: netlink: refactor can_validate_bittiming()
Posted by Vincent Mailhol 3 weeks, 1 day ago
On 10/09/2025 at 15:13, Marc Kleine-Budde wrote:
> On 10.09.2025 15:03:29, Vincent Mailhol wrote:
>> 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. Just use the pointer returned by
>> nla_data() as-is.
> 
> The memcpy()'ed struct is guaranteed to be properly aligned, is this
> also the case for the casted nla_data() pointer?

The NLA attributes are aligned on 4 bytes, c.f. NLA_ALIGNTO:

https://elixir.bootlin.com/linux/v6.16.5/source/include/uapi/linux/netlink.h#L248

Which is sufficient for struct can_bittiming which also requires just 4 bytes of
alignment as proven by the fact the the code would still compile if I add this
static assert:

  static_assert(_Alignof(typeof(*bt)) <= NLA_ALIGNTO);

But I have to admit that you caught me off guard. I did not think of that. Maybe
I should add above static assertions to the code to document that what we are
doing is correct?


Yours sincerely,
Vincent Mailhol
Re: [PATCH v2 04/20] can: netlink: refactor can_validate_bittiming()
Posted by Marc Kleine-Budde 3 weeks, 1 day ago
On 10.09.2025 15:43:00, Vincent Mailhol wrote:
> On 10/09/2025 at 15:13, Marc Kleine-Budde wrote:
> > On 10.09.2025 15:03:29, Vincent Mailhol wrote:
> >> 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. Just use the pointer returned by
> >> nla_data() as-is.
> > 
> > The memcpy()'ed struct is guaranteed to be properly aligned, is this
> > also the case for the casted nla_data() pointer?
> 
> The NLA attributes are aligned on 4 bytes, c.f. NLA_ALIGNTO:
> 
> https://elixir.bootlin.com/linux/v6.16.5/source/include/uapi/linux/netlink.h#L248
> 
> Which is sufficient for struct can_bittiming which also requires just 4 bytes of
> alignment as proven by the fact the the code would still compile if I add this
> static assert:
> 
>   static_assert(_Alignof(typeof(*bt)) <= NLA_ALIGNTO);
> 
> But I have to admit that you caught me off guard. I did not think of that. Maybe
> I should add above static assertions to the code to document that what we are
> doing is correct?

Yes, make it so!

regards,
Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
Re: [PATCH v2 04/20] can: netlink: refactor can_validate_bittiming()
Posted by Vincent Mailhol 3 weeks, 1 day ago
On 10/09/2025 at 19:55, Marc Kleine-Budde wrote:
> On 10.09.2025 15:43:00, Vincent Mailhol wrote:
>> On 10/09/2025 at 15:13, Marc Kleine-Budde wrote:
>>> On 10.09.2025 15:03:29, Vincent Mailhol wrote:
>>>> 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. Just use the pointer returned by
>>>> nla_data() as-is.
>>>
>>> The memcpy()'ed struct is guaranteed to be properly aligned, is this
>>> also the case for the casted nla_data() pointer?
>>
>> The NLA attributes are aligned on 4 bytes, c.f. NLA_ALIGNTO:
>>
>> https://elixir.bootlin.com/linux/v6.16.5/source/include/uapi/linux/netlink.h#L248
>>
>> Which is sufficient for struct can_bittiming which also requires just 4 bytes of
>> alignment as proven by the fact the the code would still compile if I add this
>> static assert:
>>
>>   static_assert(_Alignof(typeof(*bt)) <= NLA_ALIGNTO);
>>
>> But I have to admit that you caught me off guard. I did not think of that. Maybe
>> I should add above static assertions to the code to document that what we are
>> doing is correct?
> 
> Yes, make it so!

I applied the changes locally.

Let me know when you are done with the review of the other patches. I will wait
for your other comments (if any) before sending v3.


Yours sincerely,
Vincent Mailhol