All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] can: uapi: can.h: mark union inside struct can_frame packed
@ 2021-03-22 10:28 Marc Kleine-Budde
  2021-03-22 16:27 ` Oliver Hartkopp
  0 siblings, 1 reply; 4+ messages in thread
From: Marc Kleine-Budde @ 2021-03-22 10:28 UTC (permalink / raw)
  To: linux-can; +Cc: kernel, Marc Kleine-Budde, Rong Chen, kernel test robot

In commit ea7800565a12 ("can: add optional DLC element to Classical
CAN frame structure") the struct can_frame::can_dlc was put into an
anonymous union with another u8 variable.

For various reasons some members in struct can_frame and canfd_frame
including the first 8 byes of data are expected to have the same
memory layout. This is enforced by a BUILD_BUG_ON check in af_can.c.

Since the above mentioned commit this check fails on at least one compiler
(arm-linux-gnueabi-gcc (GCC) 9.3.0). Rong Chen analyzed the problem
and found that the union in struct can_frame takes 4 bytes instead of
the expected 1:

| struct can_frame {
|          canid_t                    can_id;               /* 0     4 */
|          union {
|                  __u8               len;                  /* 4     1 */
|                  __u8               can_dlc;              /* 4     1 */
|          };                                               /* 4     4 */
|          __u8                       __pad;                /* 8     1 */
|          __u8                       __res0;               /* 9     1 */
|          __u8                       len8_dlc;             /* 10     1 */
|
|          /* XXX 5 bytes hole, try to pack */
|
|          __u8                       data[8]
| __attribute__((__aligned__(8))); /*    16     8 */
|
|          /* size: 24, cachelines: 1, members: 6 */
|          /* sum members: 19, holes: 1, sum holes: 5 */
|          /* forced alignments: 1, forced holes: 1, sum forced holes: 5 */
|          /* last cacheline: 24 bytes */
| } __attribute__((__aligned__(8)));

Marking the union as packed fixes the problem.

Fixes: ea7800565a12 ("can: add optional DLC element to Classical CAN frame structure")
Suggested-by: Rong Chen <rong.a.chen@intel.com>
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 include/uapi/linux/can.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/uapi/linux/can.h b/include/uapi/linux/can.h
index f75238ac6dce..9842bb55ffd9 100644
--- a/include/uapi/linux/can.h
+++ b/include/uapi/linux/can.h
@@ -113,7 +113,7 @@ struct can_frame {
 		 */
 		__u8 len;
 		__u8 can_dlc; /* deprecated */
-	};
+	} __attribute__((packed));
 	__u8 __pad; /* padding */
 	__u8 __res0; /* reserved / padding */
 	__u8 len8_dlc; /* optional DLC for 8 byte payload length (9 .. 15) */
-- 
2.30.2



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] can: uapi: can.h: mark union inside struct can_frame packed
  2021-03-22 10:28 [PATCH] can: uapi: can.h: mark union inside struct can_frame packed Marc Kleine-Budde
@ 2021-03-22 16:27 ` Oliver Hartkopp
  2021-03-23  8:24   ` Marc Kleine-Budde
  0 siblings, 1 reply; 4+ messages in thread
From: Oliver Hartkopp @ 2021-03-22 16:27 UTC (permalink / raw)
  To: Marc Kleine-Budde, linux-can; +Cc: kernel, Rong Chen, kernel test robot



On 22.03.21 11:28, Marc Kleine-Budde wrote:
> In commit ea7800565a12 ("can: add optional DLC element to Classical
> CAN frame structure") the struct can_frame::can_dlc was put into an
> anonymous union with another u8 variable.
> 
> For various reasons some members in struct can_frame and canfd_frame
> including the first 8 byes of data are expected to have the same
> memory layout. This is enforced by a BUILD_BUG_ON check in af_can.c.
> 
> Since the above mentioned commit this check fails on at least one compiler
> (arm-linux-gnueabi-gcc (GCC) 9.3.0). Rong Chen analyzed the problem
> and found that the union in struct can_frame takes 4 bytes instead of
> the expected 1:
> 
> | struct can_frame {
> |          canid_t                    can_id;               /* 0     4 */
> |          union {
> |                  __u8               len;                  /* 4     1 */
> |                  __u8               can_dlc;              /* 4     1 */
> |          };                                               /* 4     4 */
> |          __u8                       __pad;                /* 8     1 */
> |          __u8                       __res0;               /* 9     1 */
> |          __u8                       len8_dlc;             /* 10     1 */
> |
> |          /* XXX 5 bytes hole, try to pack */
> |
> |          __u8                       data[8]
> | __attribute__((__aligned__(8))); /*    16     8 */
> |
> |          /* size: 24, cachelines: 1, members: 6 */
> |          /* sum members: 19, holes: 1, sum holes: 5 */
> |          /* forced alignments: 1, forced holes: 1, sum forced holes: 5 */
> |          /* last cacheline: 24 bytes */
> | } __attribute__((__aligned__(8)));
> 
> Marking the union as packed fixes the problem.

Is this a proper answer to this issue?

Shouldn't this problem cause the developer to update the compiler?

https://lore.kernel.org/linux-can/f8075a19-10e1-abf9-6d59-1a46454b74b1@hartkopp.net/T/#u

Regards,
Oliver

> 
> Fixes: ea7800565a12 ("can: add optional DLC element to Classical CAN frame structure")
> Suggested-by: Rong Chen <rong.a.chen@intel.com>
> Reported-by: kernel test robot <lkp@intel.com>
> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
> ---
>   include/uapi/linux/can.h | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/uapi/linux/can.h b/include/uapi/linux/can.h
> index f75238ac6dce..9842bb55ffd9 100644
> --- a/include/uapi/linux/can.h
> +++ b/include/uapi/linux/can.h
> @@ -113,7 +113,7 @@ struct can_frame {
>   		 */
>   		__u8 len;
>   		__u8 can_dlc; /* deprecated */
> -	};
> +	} __attribute__((packed));
>   	__u8 __pad; /* padding */
>   	__u8 __res0; /* reserved / padding */
>   	__u8 len8_dlc; /* optional DLC for 8 byte payload length (9 .. 15) */
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] can: uapi: can.h: mark union inside struct can_frame packed
  2021-03-22 16:27 ` Oliver Hartkopp
@ 2021-03-23  8:24   ` Marc Kleine-Budde
  2021-03-23  9:37     ` Rong Chen
  0 siblings, 1 reply; 4+ messages in thread
From: Marc Kleine-Budde @ 2021-03-23  8:24 UTC (permalink / raw)
  To: Oliver Hartkopp; +Cc: linux-can, kernel, Rong Chen, kernel test robot

[-- Attachment #1: Type: text/plain, Size: 2847 bytes --]

On 22.03.2021 17:27:38, Oliver Hartkopp wrote:
> 
> 
> On 22.03.21 11:28, Marc Kleine-Budde wrote:
> > In commit ea7800565a12 ("can: add optional DLC element to Classical
> > CAN frame structure") the struct can_frame::can_dlc was put into an
> > anonymous union with another u8 variable.
> > 
> > For various reasons some members in struct can_frame and canfd_frame
> > including the first 8 byes of data are expected to have the same
> > memory layout. This is enforced by a BUILD_BUG_ON check in af_can.c.
> > 
> > Since the above mentioned commit this check fails on at least one compiler
> > (arm-linux-gnueabi-gcc (GCC) 9.3.0). Rong Chen analyzed the problem
> > and found that the union in struct can_frame takes 4 bytes instead of
> > the expected 1:
> > 
> > | struct can_frame {
> > |          canid_t                    can_id;               /* 0     4 */
> > |          union {
> > |                  __u8               len;                  /* 4     1 */
> > |                  __u8               can_dlc;              /* 4     1 */
> > |          };                                               /* 4     4 */
> > |          __u8                       __pad;                /* 8     1 */
> > |          __u8                       __res0;               /* 9     1 */
> > |          __u8                       len8_dlc;             /* 10     1 */
> > |
> > |          /* XXX 5 bytes hole, try to pack */
> > |
> > |          __u8                       data[8]
> > | __attribute__((__aligned__(8))); /*    16     8 */
> > |
> > |          /* size: 24, cachelines: 1, members: 6 */
> > |          /* sum members: 19, holes: 1, sum holes: 5 */
> > |          /* forced alignments: 1, forced holes: 1, sum forced holes: 5 */
> > |          /* last cacheline: 24 bytes */
> > | } __attribute__((__aligned__(8)));
> > 
> > Marking the union as packed fixes the problem.
> 
> Is this a proper answer to this issue?

With the affected compiler, yes. This patch makes the union 1 byte long,
as expected. This effectively fixes compiling.

I'm glad that the build bug triggered, which avoids having a broken
running kernel.

> Shouldn't this problem cause the developer to update the compiler?

The question is, are the other silent corruptions with a similar union
somewhere? Maybe we should escalate this problem to the linux-arm-kernel
ML.

Rong Chen, can I download the compiler you're using somewhere?

> https://lore.kernel.org/linux-can/f8075a19-10e1-abf9-6d59-1a46454b74b1@hartkopp.net/T/#u

regards,
Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] can: uapi: can.h: mark union inside struct can_frame packed
  2021-03-23  8:24   ` Marc Kleine-Budde
@ 2021-03-23  9:37     ` Rong Chen
  0 siblings, 0 replies; 4+ messages in thread
From: Rong Chen @ 2021-03-23  9:37 UTC (permalink / raw)
  To: Marc Kleine-Budde, Oliver Hartkopp; +Cc: linux-can, kernel, kernel test robot



On 3/23/21 4:24 PM, Marc Kleine-Budde wrote:
> On 22.03.2021 17:27:38, Oliver Hartkopp wrote:
>>
>> On 22.03.21 11:28, Marc Kleine-Budde wrote:
>>> In commit ea7800565a12 ("can: add optional DLC element to Classical
>>> CAN frame structure") the struct can_frame::can_dlc was put into an
>>> anonymous union with another u8 variable.
>>>
>>> For various reasons some members in struct can_frame and canfd_frame
>>> including the first 8 byes of data are expected to have the same
>>> memory layout. This is enforced by a BUILD_BUG_ON check in af_can.c.
>>>
>>> Since the above mentioned commit this check fails on at least one compiler
>>> (arm-linux-gnueabi-gcc (GCC) 9.3.0). Rong Chen analyzed the problem
>>> and found that the union in struct can_frame takes 4 bytes instead of
>>> the expected 1:
>>>
>>> | struct can_frame {
>>> |          canid_t                    can_id;               /* 0     4 */
>>> |          union {
>>> |                  __u8               len;                  /* 4     1 */
>>> |                  __u8               can_dlc;              /* 4     1 */
>>> |          };                                               /* 4     4 */
>>> |          __u8                       __pad;                /* 8     1 */
>>> |          __u8                       __res0;               /* 9     1 */
>>> |          __u8                       len8_dlc;             /* 10     1 */
>>> |
>>> |          /* XXX 5 bytes hole, try to pack */
>>> |
>>> |          __u8                       data[8]
>>> | __attribute__((__aligned__(8))); /*    16     8 */
>>> |
>>> |          /* size: 24, cachelines: 1, members: 6 */
>>> |          /* sum members: 19, holes: 1, sum holes: 5 */
>>> |          /* forced alignments: 1, forced holes: 1, sum forced holes: 5 */
>>> |          /* last cacheline: 24 bytes */
>>> | } __attribute__((__aligned__(8)));
>>>
>>> Marking the union as packed fixes the problem.
>> Is this a proper answer to this issue?
> With the affected compiler, yes. This patch makes the union 1 byte long,
> as expected. This effectively fixes compiling.
>
> I'm glad that the build bug triggered, which avoids having a broken
> running kernel.
>
>> Shouldn't this problem cause the developer to update the compiler?
> The question is, are the other silent corruptions with a similar union
> somewhere? Maybe we should escalate this problem to the linux-arm-kernel
> ML.
>
> Rong Chen, can I download the compiler you're using somewhere?

Hi Marc,

I tried the below two compilers , and detail is at 
https://lore.kernel.org/linux-can/f8075a19-10e1-abf9-6d59-1a46454b74b1@hartkopp.net/T/#m1eff42a8f6c24ebf066fa74a826a0ca068457ed8

https://download.01.org/0day-ci/cross-package/gcc-9.3.0-nolibc/x86_64-gcc-9.3.0-nolibc_arm-linux-gnueabi.tar.xz

http://cdn.kernel.org/pub/tools/crosstool/files/bin/x86_64/10.1.0/x86_64-gcc-10.1.0-nolibc-arm-linux-gnueabi.tar.xz

Best Regards,
Rong Chen

>
>> https://lore.kernel.org/linux-can/f8075a19-10e1-abf9-6d59-1a46454b74b1@hartkopp.net/T/#u
> regards,
> Marc
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-03-23  9:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-22 10:28 [PATCH] can: uapi: can.h: mark union inside struct can_frame packed Marc Kleine-Budde
2021-03-22 16:27 ` Oliver Hartkopp
2021-03-23  8:24   ` Marc Kleine-Budde
2021-03-23  9:37     ` Rong Chen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.