Skip to contentSkip to conversation

firmware: imx: Align imx SC msg structs to 4

PATCH Applied Leonard Crestez 7 participants 23 messages Feb 20, 2020 – Mar 17, 2020 1 message not in the archive

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v2 cover firmware: imx: Align imx SC msg structs to 4

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have sizeof "6" and "7".

This produces many oopses with CONFIG_KASAN=y:

	BUG: KASAN: stack-out-of-bounds in imx_mu_send_data+0x108/0x1f0

It shouldn't cause an issues in normal use because these structs are always allocated on the stack but tools like KASAN are very useful on stable kernels.

Chnages since v1:
* Split into many patches with individual fixes: tags Link to v1: https://patchwork.kernel.org/patch/11376909/

Leonard Crestez (8):
clk: imx: Align imx sc clock msg structs to 4
clk: imx: Align imx sc clock parent msg structs to 4
firmware: imx: misc: Align imx sc msg structs to 4
firmware: imx: scu-pd: Align imx sc msg structs to 4
firmware: imx: Align imx_sc_msg_req_cpu_start to 4
pinctrl: imx: scu: Align imx sc msg structs to 4
rtc: imx-sc: Align imx sc msg structs to 4
soc: imx-scu: Align imx sc msg structs to 4

 drivers/clk/imx/clk-scu.c               | 8 ++++----
 drivers/firmware/imx/misc.c             | 8 ++++----
 drivers/firmware/imx/scu-pd.c           | 2 +-
 drivers/pinctrl/freescale/pinctrl-scu.c | 4 ++--
 drivers/rtc/rtc-imx-sc.c                | 2 +-
 drivers/soc/imx/soc-imx-scu.c           | 2 +-
 6 files changed, 13 insertions(+), 13 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/8 clk: imx: Align imx sc clock msg structs to 4

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes fe37b4820417 ("clk: imx: add scu clock common part")
---
 drivers/clk/imx/clk-scu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/clk/imx/clk-scu.c +3 -3
index fbef740704d0..3c5c42d8833e 100644
@@ -41,16 +41,16 @@ struct clk_scu {
 struct imx_sc_msg_req_set_clock_rate {
 	struct imx_sc_rpc_msg hdr;
 	__le32 rate;
 	__le16 resource;
 	u8 clk;
-} __packed;
+} __packed __aligned(4);
 
 struct req_get_clock_rate {
 	__le16 resource;
 	u8 clk;
-} __packed;
+} __packed __aligned(4);
 
 struct resp_get_clock_rate {
 	__le32 rate;
 };
 
@@ -119,11 +119,11 @@ struct imx_sc_msg_req_clock_enable {
 	struct imx_sc_rpc_msg hdr;
 	__le16 resource;
 	u8 clk;
 	u8 enable;
 	u8 autog;
-} __packed;
+} __packed __aligned(4);
 
 static inline struct clk_scu *to_clk_scu(struct clk_hw *hw)
 {
 	return container_of(hw, struct clk_scu, hw);
 }
Signature · 1 line
2.17.1
Applied
#2 lore raw

Re: [PATCH v2 1/8] clk: imx: Align imx sc clock msg structs to 4

Quoted 9 lines · Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes fe37b4820417 ("clk: imx: add scu clock common part")

Applied, thanks.

#3 lore raw

Re: [PATCH v2 1/8] clk: imx: Align imx sc clock msg structs to 4

Quoted 4 lines · Shawn Guo
Quoted 3 lines · Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

Quoted 8 lines
Quoted 6 lines

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes fe37b4820417 ("clk: imx: add scu clock common part")

Applied, thanks.

Patch #1 and #2 were dropped from my clk queue, as Stephen hasn't been convinced by this change.

Shawn

maintainer
#4 lore raw

Re: [PATCH v2 1/8] clk: imx: Align imx sc clock msg structs to 4

In reply to Leonard Crestez

Quoting Leonard Crestez (2020-02-20 08:29:32)

Quoted 18 lines

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes fe37b4820417 ("clk: imx: add scu clock common part")
---
 drivers/clk/imx/clk-scu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/clk/imx/clk-scu.c +0 -0
index fbef740704d0..3c5c42d8833e 100644

drivers/clk/imx/clk-scu.c struct clk_scu { · line 46

Quoted 7 lines
drivers/clk/imx/clk-scu.c +1 -1
@@ -41,16 +41,16 @@ struct clk_scu {
 struct imx_sc_msg_req_set_clock_rate {
        struct imx_sc_rpc_msg hdr;
        __le32 rate;
        __le16 resource;
        u8 clk;
-} __packed;
+} __packed __aligned(4);

Sorry, this still doesn't make sense to me. Having __aligned(4) means that the struct is placed on the stack at some alignment, great, but it still has __packed so the sizeof this struct is some odd number like 11. If this struct is the last element on the stack it will end at some unaligned address and the mailbox code will read a few bytes beyond the end of the stack.

I see that the calling code puts 3 as the 'size' for this struct in clk_scu_set_rate().

	hdr->size = 3;

That seems to say that the struct is 3 words long, or 12 bytes. Then we call imx_scu_call_rpc(), passing the pointer to this struct on the stack and that eventually gets into imx_scu_ipc_write() calling mbox_send_message() with u32 pointers.

	for (i = 0; i < hdr->size; i++) {
		sc_chan = &sc_ipc->chans[i % 4];
		ret = mbox_send_message(sc_chan->ch, &data[i]);

So we've taken the 11 byte struct (data in this case) and casted it to a u32 array with 3 elements, which is bad. This is what kasan is warning about. Adding aligned sometimes fixes it because the compiler will place the next stack variable at the naturally aligned location and thus we get the one byte padding but I don't see how that works when it's the last stack element. The stack will end at some unaligned address.

The better solution would be to drop __aligned(4) and make a union of the struct with whatever size number of words the message is or do a copy of the struct into a u32 array that is passed to imx_scu_call_rpc().

For example:

	struct imx_sc_msg_req_set_clock_rate {
		union {
			struct packed_message {
				struct imx_sc_rpc_msg hdr;
				__le32 rate;
				__le16 resource;
				u8 clk;
			} __packed;
			u32 data[3];
		};
	};

If the union approach was used then each time imx_scu_call_rpc() is called we can simply pass the 'data' member and make the second argument 'msg' strongly typed to be a u32 pointer. kasan should be happy too.

#5 lore raw

Re: [PATCH v2 1/8] clk: imx: Align imx sc clock msg structs to 4

Quoted 25 lines · Stephen Boyd

Quoting Leonard Crestez (2020-02-20 08:29:32)

Quoted 24 lines

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes fe37b4820417 ("clk: imx: add scu clock common part")
---
  drivers/clk/imx/clk-scu.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/clk/imx/clk-scu.c +0 -1
index fbef740704d0..3c5c42d8833e 100644
@@ -41,16 +41,16 @@ struct clk_scu {
  struct imx_sc_msg_req_set_clock_rate {
         struct imx_sc_rpc_msg hdr;
         __le32 rate;
         __le16 resource;
         u8 clk;
-} __packed;
Quoted 8 lines

drivers/clk/imx/clk-scu.c struct clk_scu { · line 46

Quoted 1 line
drivers/clk/imx/clk-scu.c +1 -0
+} __packed __aligned(4);

Sorry, this still doesn't make sense to me. Having __aligned(4) means that the struct is placed on the stack at some alignment, great, but it still has __packed so the sizeof this struct is some odd number like 11. If this struct is the last element on the stack it will end at some unaligned address and the mailbox code will read a few bytes beyond the end of the stack.

I checked again and marking the struct with __aligned(4) makes it have sizeof == 12 as intended. It was 11 before.

     static_assert(sizeof(struct imx_sc_msg_req_set_clock_rate) == 12);

After reading through your email and gcc docs again I'm not sure if this portable/reliable this is but as far as I understand "sizeof" needs to account for alignment. Or is this just an accident with my compiler?

Marking a structure both __packed and __aligned(4) means that __packed only affects internal struct member layout but sizeof is still rounded up to a multiple of 4:

struct test {
	u8	a;
	u16	b;
} __packed __aligned(4);

static_assert(sizeof(struct test) == 4);
static_assert(offsetof(struct test, a) == 0);
static_assert(offsetof(struct test, b) == 1);

This test is not realistic because I don't think SCU messages have any such oddly-aligned members.

Quoted 45 lines

I see that the calling code puts 3 as the 'size' for this struct in clk_scu_set_rate().

	hdr->size = 3;

That seems to say that the struct is 3 words long, or 12 bytes. Then we call imx_scu_call_rpc(), passing the pointer to this struct on the stack and that eventually gets into imx_scu_ipc_write() calling mbox_send_message() with u32 pointers.

	for (i = 0; i < hdr->size; i++) {
		sc_chan = &sc_ipc->chans[i % 4];
		ret = mbox_send_message(sc_chan->ch, &data[i]);

So we've taken the 11 byte struct (data in this case) and casted it to a u32 array with 3 elements, which is bad. This is what kasan is warning about. Adding aligned sometimes fixes it because the compiler will place the next stack variable at the naturally aligned location and thus we get the one byte padding but I don't see how that works when it's the last stack element. The stack will end at some unaligned address.

The better solution would be to drop __aligned(4) and make a union of the struct with whatever size number of words the message is or do a copy of the struct into a u32 array that is passed to imx_scu_call_rpc().

For example:

	struct imx_sc_msg_req_set_clock_rate {
		union {
			struct packed_message {
				struct imx_sc_rpc_msg hdr;
				__le32 rate;
				__le16 resource;
				u8 clk;
			} __packed;
			u32 data[3];
		};
	};

If the union approach was used then each time imx_scu_call_rpc() is called we can simply pass the 'data' member and make the second argument 'msg' strongly typed to be a u32 pointer. kasan should be happy too.

A message that is not in the archive (sent off-list or not yet received): 158276809953.177367.6095692240077023796@swboyd.mtv.corp.google.com

#7 lore raw

Re: [PATCH v2 1/8] clk: imx: Align imx sc clock msg structs to 4

Quoted 62 lines · Stephen Boyd

Quoting Leonard Crestez (2020-02-25 11:52:11)

Quoted 53 lines · Stephen Boyd
Quoted 25 lines

Quoting Leonard Crestez (2020-02-20 08:29:32)

Quoted 24 lines

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes fe37b4820417 ("clk: imx: add scu clock common part")
---
   drivers/clk/imx/clk-scu.c | 6 +++---
   1 file changed, 3 insertions(+), 3 deletions(-)
drivers/clk/imx/clk-scu.c +0 -1
index fbef740704d0..3c5c42d8833e 100644
@@ -41,16 +41,16 @@ struct clk_scu {
   struct imx_sc_msg_req_set_clock_rate {
          struct imx_sc_rpc_msg hdr;
          __le32 rate;
          __le16 resource;
          u8 clk;
-} __packed;
Quoted 8 lines

drivers/clk/imx/clk-scu.c struct clk_scu { · line 46

Quoted 1 line
drivers/clk/imx/clk-scu.c +1 -0
+} __packed __aligned(4);

Sorry, this still doesn't make sense to me. Having __aligned(4) means that the struct is placed on the stack at some alignment, great, but it still has __packed so the sizeof this struct is some odd number like 11. If this struct is the last element on the stack it will end at some unaligned address and the mailbox code will read a few bytes beyond the end of the stack.

I checked again and marking the struct with __aligned(4) makes it have sizeof == 12 as intended. It was 11 before.

      static_assert(sizeof(struct imx_sc_msg_req_set_clock_rate) == 12);

After reading through your email and gcc docs again I'm not sure if this portable/reliable this is but as far as I understand "sizeof" needs to account for alignment. Or is this just an accident with my compiler?

Marking a structure both __packed and __aligned(4) means that __packed only affects internal struct member layout but sizeof is still rounded up to a multiple of 4:

struct test {
         u8      a;
         u16     b;
} __packed __aligned(4);
Quoted 7 lines

static_assert(sizeof(struct test) == 4);
static_assert(offsetof(struct test, a) == 0);
static_assert(offsetof(struct test, b) == 1);

This test is not realistic because I don't think SCU messages have any such oddly-aligned members.

Quoted 10 lines

I'm not really sure as I'm not a linker expert. I'm just especially wary of using __packed or __aligned attributes because they silently generate code that is usually inefficient. This is why we typically do lots of shifting and masking in the kernel, so that we can easily see how complicated it is to pack bits into place. Maybe it makes sense to get rid of the structs entirely and pack the bits into __le32 arrays of varying length. Then we don't have to worry about packed or aligned or what the compiler will do and we can easily be confident that we've put the bits in the right place in each u32 that is eventually written to the mailbox register space.

These message structs are not as complicated as hardware register, for example everything is always on a byte border.

In older versions of the imx internal tree SC messaging is done by packing into arrays through a layer of generated code which looks like this:

          RPC_VER(&msg) = SC_RPC_VERSION;
          RPC_SVC(&msg) = U8(SC_RPC_SVC_MISC);
          RPC_FUNC(&msg) = U8(MISC_FUNC_SET_CONTROL);
          RPC_U32(&msg, 0U) = U32(ctrl);
          RPC_U32(&msg, 4U) = U32(val);
          RPC_U16(&msg, 8U) = U16(resource);
          RPC_SIZE(&msg) = 4U;

The RPC_U32/U16 macros look like this:

#define RPC_I32(MESG, IDX)      ((MESG)->DATA.i32[(IDX) / 4U])
#define RPC_I16(MESG, IDX)      ((MESG)->DATA.i16[(IDX) / 2U])
#define RPC_I8(MESG, IDX)       ((MESG)->DATA.i8[(IDX)])
#define RPC_U32(MESG, IDX)      ((MESG)->DATA.u32[(IDX) / 4U])
#define RPC_U16(MESG, IDX)      ((MESG)->DATA.u16[(IDX) / 2U])
#define RPC_U8(MESG, IDX)       ((MESG)->DATA.u8[(IDX)])

and the message struct itself has a big union for the data:

typedef struct {
          uint8_t version;
          uint8_t size;
          uint8_t svc;
          uint8_t func;
          union {
                  int32_t i32[(SC_RPC_MAX_MSG - 1U)];
                  int16_t i16[(SC_RPC_MAX_MSG - 1U) * 2U];
                  int8_t i8[(SC_RPC_MAX_MSG - 1U) * 4U];
                  uint32_t u32[(SC_RPC_MAX_MSG - 1U)];
                  uint16_t u16[(SC_RPC_MAX_MSG - 1U) * 2U];
                  uint8_t u8[(SC_RPC_MAX_MSG - 1U) * 4U];
          } DATA;
} sc_rpc_msg_t;

This approach is very verbose to the point of being unreadable I think it's much to message structs instead. Compiler struct layout rules are not really all that complicated and casting binary data as structs is very common in areas such as networking. This approach is also used by other firmware interfaces like TI sci and nvidia bpmp.

imx8 currently has manually written message structs, it's unfortunate that a bug was found and fixing required a scattering patches in multiple subsystems. Perhaps a better solution would be to centralize all structs in a single header similar to drivers/firmware/ti_sci.h?

In order to ensrue that there are no issues specific to the compile version perhaps a bunch of static_assert statements could be added to check that sizeof and offset are as expected?

---------------------------------

As far as I can tell the issue KASAN warns about can be simplified to this:

struct __packed badpack {
     u32     a;
     u16     b;
     u8      c;
};

static_assert(sizeof(struct badpack) == 7);

static void func(void *x)

{
     u32* arr = (u32*)x;
     arr[0] = 0x11111111;
     arr[1] = 0x22222222;
}

static int hello(void)

{
     struct badpack s;
     u8 x = 0x33;

     printk("&s=%px &x=%px\n", &s, &x);
     func(&s);
     // x could be overwritten here, depending on stack layout.
     BUG_ON(x != 0x33);

     return 0;
}

Adding __aligned(4) bumps struct size to 8 and avoids the issue

Added KASAN maintainers to check if this is a valid fix.

--
Regards,
Leonard

#8 lore raw

Re: [PATCH v2 1/8] clk: imx: Align imx sc clock msg structs to 4

Quoted 162 lines · Leonard Crestez
Quoted 62 lines · Stephen Boyd

Quoting Leonard Crestez (2020-02-25 11:52:11)

Quoted 53 lines · Stephen Boyd
Quoted 25 lines

Quoting Leonard Crestez (2020-02-20 08:29:32)

Quoted 24 lines

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes fe37b4820417 ("clk: imx: add scu clock common part")
---
   drivers/clk/imx/clk-scu.c | 6 +++---
   1 file changed, 3 insertions(+), 3 deletions(-)
drivers/clk/imx/clk-scu.c +0 -1
index fbef740704d0..3c5c42d8833e 100644
@@ -41,16 +41,16 @@ struct clk_scu {
   struct imx_sc_msg_req_set_clock_rate {
          struct imx_sc_rpc_msg hdr;
          __le32 rate;
          __le16 resource;
          u8 clk;
-} __packed;
Quoted 8 lines

drivers/clk/imx/clk-scu.c struct clk_scu { · line 46

Quoted 1 line
drivers/clk/imx/clk-scu.c +1 -0
+} __packed __aligned(4);

Sorry, this still doesn't make sense to me. Having __aligned(4) means that the struct is placed on the stack at some alignment, great, but it still has __packed so the sizeof this struct is some odd number like 11. If this struct is the last element on the stack it will end at some unaligned address and the mailbox code will read a few bytes beyond the end of the stack.

I checked again and marking the struct with __aligned(4) makes it have sizeof == 12 as intended. It was 11 before.

      static_assert(sizeof(struct imx_sc_msg_req_set_clock_rate) == 12);

After reading through your email and gcc docs again I'm not sure if this portable/reliable this is but as far as I understand "sizeof" needs to account for alignment. Or is this just an accident with my compiler?

Marking a structure both __packed and __aligned(4) means that __packed only affects internal struct member layout but sizeof is still rounded up to a multiple of 4:

struct test {
         u8      a;
         u16     b;
} __packed __aligned(4);
Quoted 7 lines

static_assert(sizeof(struct test) == 4);
static_assert(offsetof(struct test, a) == 0);
static_assert(offsetof(struct test, b) == 1);

This test is not realistic because I don't think SCU messages have any such oddly-aligned members.

Quoted 10 lines

I'm not really sure as I'm not a linker expert. I'm just especially wary of using __packed or __aligned attributes because they silently generate code that is usually inefficient. This is why we typically do lots of shifting and masking in the kernel, so that we can easily see how complicated it is to pack bits into place. Maybe it makes sense to get rid of the structs entirely and pack the bits into __le32 arrays of varying length. Then we don't have to worry about packed or aligned or what the compiler will do and we can easily be confident that we've put the bits in the right place in each u32 that is eventually written to the mailbox register space.

These message structs are not as complicated as hardware register, for example everything is always on a byte border.

In older versions of the imx internal tree SC messaging is done by packing into arrays through a layer of generated code which looks like this:

          RPC_VER(&msg) = SC_RPC_VERSION;
          RPC_SVC(&msg) = U8(SC_RPC_SVC_MISC);
          RPC_FUNC(&msg) = U8(MISC_FUNC_SET_CONTROL);
          RPC_U32(&msg, 0U) = U32(ctrl);
          RPC_U32(&msg, 4U) = U32(val);
          RPC_U16(&msg, 8U) = U16(resource);
          RPC_SIZE(&msg) = 4U;

The RPC_U32/U16 macros look like this:

#define RPC_I32(MESG, IDX)      ((MESG)->DATA.i32[(IDX) / 4U])
#define RPC_I16(MESG, IDX)      ((MESG)->DATA.i16[(IDX) / 2U])
#define RPC_I8(MESG, IDX)       ((MESG)->DATA.i8[(IDX)])
#define RPC_U32(MESG, IDX)      ((MESG)->DATA.u32[(IDX) / 4U])
#define RPC_U16(MESG, IDX)      ((MESG)->DATA.u16[(IDX) / 2U])
#define RPC_U8(MESG, IDX)       ((MESG)->DATA.u8[(IDX)])

and the message struct itself has a big union for the data:

typedef struct {
          uint8_t version;
          uint8_t size;
          uint8_t svc;
          uint8_t func;
          union {
                  int32_t i32[(SC_RPC_MAX_MSG - 1U)];
                  int16_t i16[(SC_RPC_MAX_MSG - 1U) * 2U];
                  int8_t i8[(SC_RPC_MAX_MSG - 1U) * 4U];
                  uint32_t u32[(SC_RPC_MAX_MSG - 1U)];
                  uint16_t u16[(SC_RPC_MAX_MSG - 1U) * 2U];
                  uint8_t u8[(SC_RPC_MAX_MSG - 1U) * 4U];
          } DATA;
} sc_rpc_msg_t;

This approach is very verbose to the point of being unreadable I think it's much to message structs instead. Compiler struct layout rules are not really all that complicated and casting binary data as structs is very common in areas such as networking. This approach is also used by other firmware interfaces like TI sci and nvidia bpmp.

imx8 currently has manually written message structs, it's unfortunate that a bug was found and fixing required a scattering patches in multiple subsystems. Perhaps a better solution would be to centralize all structs in a single header similar to drivers/firmware/ti_sci.h?

In order to ensrue that there are no issues specific to the compile version perhaps a bunch of static_assert statements could be added to check that sizeof and offset are as expected?

---------------------------------

As far as I can tell the issue KASAN warns about can be simplified to this:

struct __packed badpack {
     u32     a;
     u16     b;
     u8      c;
};

static_assert(sizeof(struct badpack) == 7);

static void func(void *x)

{
     u32* arr = (u32*)x;
     arr[0] = 0x11111111;
     arr[1] = 0x22222222;
}

static int hello(void)

{
     struct badpack s;
     u8 x = 0x33;

     printk("&s=%px &x=%px\n", &s, &x);
     func(&s);
     // x could be overwritten here, depending on stack layout.
     BUG_ON(x != 0x33);

     return 0;
}
Quoted 3 lines

Adding __aligned(4) bumps struct size to 8 and avoids the issue

Added KASAN maintainers to check if this is a valid fix.

Hi Leonard,

I think it should fix the bug.
It's not so much about KASAN, more about the validity of the C program.

maintainer
#9 lore raw

RE: [PATCH v2 1/8] clk: imx: Align imx sc clock msg structs to 4

In reply to Stephen Boyd
Quoted 33 lines

From: Stephen Boyd <[email protected]>
Sent: Wednesday, February 26, 2020 12:52 AM
Quoting Leonard Crestez (2020-02-20 08:29:32)

Quoted 18 lines

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes fe37b4820417 ("clk: imx: add scu clock common part")
---
 drivers/clk/imx/clk-scu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/clk/imx/clk-scu.c +0 -0
index fbef740704d0..3c5c42d8833e 100644

drivers/clk/imx/clk-scu.c struct clk_scu { · line 46

Quoted 7 lines
drivers/clk/imx/clk-scu.c +1 -1
@@ -41,16 +41,16 @@ struct clk_scu {
 struct imx_sc_msg_req_set_clock_rate {
        struct imx_sc_rpc_msg hdr;
        __le32 rate;
        __le16 resource;
        u8 clk;
-} __packed;
+} __packed __aligned(4);

Sorry, this still doesn't make sense to me. Having __aligned(4) means that the struct is placed on the stack at some alignment, great, but it still has __packed so the sizeof this struct is some odd number like 11

Quoted 2 lines

If this struct is the last element on the stack it will end at some unaligned address and the mailbox code will read a few bytes beyond the end of the stack.

Hi Leonard,

Can you construct this case to see if we can reproduce the issue as pointed by Stephen?

Regards
Aisheng

Quoted 42 lines

I see that the calling code puts 3 as the 'size' for this struct in clk_scu_set_rate().

	hdr->size = 3;

That seems to say that the struct is 3 words long, or 12 bytes. Then we call imx_scu_call_rpc(), passing the pointer to this struct on the stack and that eventually gets into imx_scu_ipc_write() calling mbox_send_message() with u32 pointers.

	for (i = 0; i < hdr->size; i++) {
		sc_chan = &sc_ipc->chans[i % 4];
		ret = mbox_send_message(sc_chan->ch, &data[i]);

So we've taken the 11 byte struct (data in this case) and casted it to a u32 array with 3 elements, which is bad. This is what kasan is warning about. Adding aligned sometimes fixes it because the compiler will place the next stack variable at the naturally aligned location and thus we get the one byte padding but I don't see how that works when it's the last stack element. The stack will end at some unaligned address.

The better solution would be to drop __aligned(4) and make a union of the struct with whatever size number of words the message is or do a copy of the struct into a u32 array that is passed to imx_scu_call_rpc().

For example:

	struct imx_sc_msg_req_set_clock_rate {
		union {
			struct packed_message {
				struct imx_sc_rpc_msg hdr;
				__le32 rate;
				__le16 resource;
				u8 clk;
			} __packed;
			u32 data[3];
		};
	};

If the union approach was used then each time imx_scu_call_rpc() is called we can simply pass the 'data' member and make the second argument 'msg' strongly typed to be a u32 pointer. kasan should be happy too.

#10 lore raw

PATCH v2 2/8 clk: imx: Align imx sc clock parent msg structs to 4

In reply to Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes 666aed2d13ee ("clk: imx: scu: add set parent support")
---
 drivers/clk/imx/clk-scu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/imx/clk-scu.c +1 -1
index 3c5c42d8833e..b8b2072742a5 100644
@@ -82,11 +82,11 @@ struct imx_sc_msg_get_clock_parent {
 	struct imx_sc_rpc_msg hdr;
 	union {
 		struct req_get_clock_parent {
 			__le16 resource;
 			u8 clk;
-		} __packed req;
+		} __packed __aligned(4) req;
 		struct resp_get_clock_parent {
 			u8 parent;
 		} resp;
 	} data;
 };
Signature · 1 line
2.17.1
Applied
#11 lore raw

Re: [PATCH v2 2/8] clk: imx: Align imx sc clock parent msg structs to 4

Quoted 9 lines · Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes 666aed2d13ee ("clk: imx: scu: add set parent support")

Applied, thanks.

#12 lore raw

PATCH v2 3/8 firmware: imx: misc: Align imx sc msg structs to 4

In reply to Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y:

    BUG: KASAN: stack-out-of-bounds in imx_mu_send_data+0x108/0x1f0

It shouldn't cause an issues in normal use because these structs are always allocated on the stack.

Fixes 15e1f2bc8b3b ("firmware: imx: add misc svc support")
---
 drivers/firmware/imx/misc.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/firmware/imx/misc.c +3 -3
index 4b56a587dacd..6a21ff942f82 100644
@@ -14,11 +14,11 @@
 struct imx_sc_msg_req_misc_set_ctrl {
 	struct imx_sc_rpc_msg hdr;
 	u32 ctrl;
 	u32 val;
 	u16 resource;
-} __packed;
+} __packed __aligned(4);
 
 struct imx_sc_msg_req_cpu_start {
 	struct imx_sc_rpc_msg hdr;
 	u32 address_hi;
 	u32 address_lo;
@@ -28,16 +28,16 @@ struct imx_sc_msg_req_cpu_start {
 
 struct imx_sc_msg_req_misc_get_ctrl {
 	struct imx_sc_rpc_msg hdr;
 	u32 ctrl;
 	u16 resource;
-} __packed;
+} __packed __aligned(4);
 
 struct imx_sc_msg_resp_misc_get_ctrl {
 	struct imx_sc_rpc_msg hdr;
 	u32 val;
-} __packed;
+} __packed __aligned(4);
 
 /*
  * This function sets a miscellaneous control value.
  *
  * @param[in]     ipc         IPC handle
Signature · 1 line
2.17.1
Applied
#13 lore raw

Re: [PATCH v2 3/8] firmware: imx: misc: Align imx sc msg structs to 4

Quoted 5 lines · Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y:

Quoted 7 lines
    BUG: KASAN: stack-out-of-bounds in imx_mu_send_data+0x108/0x1f0

It shouldn't cause an issues in normal use because these structs are always allocated on the stack.

Fixes 15e1f2bc8b3b ("firmware: imx: add misc svc support")

Applied, thanks.

#14 lore raw

PATCH v2 4/8 firmware: imx: scu-pd: Align imx sc msg structs to 4

In reply to Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes c800cd7824bd ("firmware: imx: add SCU power domain driver")
---
 drivers/firmware/imx/scu-pd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/imx/scu-pd.c +1 -1
index 09cfa268c6bd..cbc9a47ff44f 100644
@@ -59,11 +59,11 @@
 /* SCU Power Mode Protocol definition */
 struct imx_sc_msg_req_set_resource_power_mode {
 	struct imx_sc_rpc_msg hdr;
 	u16 resource;
 	u8 mode;
-} __packed;
+} __packed __aligned(4);
 
 #define IMX_SCU_PD_NAME_SIZE 20
 struct imx_sc_pm_domain {
 	struct generic_pm_domain pd;
 	char name[IMX_SCU_PD_NAME_SIZE];
Signature · 1 line
2.17.1
Applied
#15 lore raw

Re: [PATCH v2 4/8] firmware: imx: scu-pd: Align imx sc msg structs to 4

Quoted 9 lines · Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes c800cd7824bd ("firmware: imx: add SCU power domain driver")

Applied, thanks.

#16 lore raw

PATCH v2 5/8 firmware: imx: Align imx_sc_msg_req_cpu_start to 4

In reply to Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes d90bf296ae18 ("firmware: imx: Add support to start/stop a CPU")
---
 drivers/firmware/imx/misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/imx/misc.c +1 -1
index 6a21ff942f82..d073cb3ce699 100644
@@ -22,11 +22,11 @@ struct imx_sc_msg_req_cpu_start {
 	struct imx_sc_rpc_msg hdr;
 	u32 address_hi;
 	u32 address_lo;
 	u16 resource;
 	u8 enable;
-} __packed;
+} __packed __aligned(4);
 
 struct imx_sc_msg_req_misc_get_ctrl {
 	struct imx_sc_rpc_msg hdr;
 	u32 ctrl;
 	u16 resource;
Signature · 1 line
2.17.1
Applied
#17 lore raw

Re: [PATCH v2 5/8] firmware: imx: Align imx_sc_msg_req_cpu_start to 4

Quoted 9 lines · Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes d90bf296ae18 ("firmware: imx: Add support to start/stop a CPU")

Applied, thanks.

#18 lore raw

PATCH v2 6/8 pinctrl: imx: scu: Align imx sc msg structs to 4

In reply to Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes b96eea718bf6 ("pinctrl: fsl: add scu based pinctrl support")
---
 drivers/pinctrl/freescale/pinctrl-scu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pinctrl/freescale/pinctrl-scu.c +2 -2
index 73bf1d9f9cc6..23cf04bdfc55 100644
@@ -21,16 +21,16 @@ enum pad_func_e {
 
 struct imx_sc_msg_req_pad_set {
 	struct imx_sc_rpc_msg hdr;
 	u32 val;
 	u16 pad;
-} __packed;
+} __packed __aligned(4);
 
 struct imx_sc_msg_req_pad_get {
 	struct imx_sc_rpc_msg hdr;
 	u16 pad;
-} __packed;
+} __packed __aligned(4);
 
 struct imx_sc_msg_resp_pad_get {
 	struct imx_sc_rpc_msg hdr;
 	u32 val;
 } __packed;
Signature · 1 line
2.17.1
#19 lore raw

Re: [PATCH v2 6/8] pinctrl: imx: scu: Align imx sc msg structs to 4

Quoted 9 lines · Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes b96eea718bf6 ("pinctrl: fsl: add scu based pinctrl support")

Patch applied for fixes. KASan needs to work.
Thanks for fixing this!

Yours,
Linus Walleij

#20 lore raw

PATCH v2 7/8 rtc: imx-sc: Align imx sc msg structs to 4

In reply to Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes a3094fc1a15e ("rtc: imx-sc: add rtc alarm support")
---
 drivers/rtc/rtc-imx-sc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rtc/rtc-imx-sc.c +1 -1
index cf2c12107f2b..a5f59e6f862e 100644
@@ -35,11 +35,11 @@ struct imx_sc_msg_timer_rtc_set_alarm {
 	u8 mon;
 	u8 day;
 	u8 hour;
 	u8 min;
 	u8 sec;
-} __packed;
+} __packed __aligned(4);
 
 static int imx_sc_rtc_read_time(struct device *dev, struct rtc_time *tm)
 {
 	struct imx_sc_msg_timer_get_rtc_time msg;
 	struct imx_sc_rpc_msg *hdr = &msg.hdr;
Signature · 1 line
2.17.1
maintainer Applied
#21 lore raw

Re: [PATCH v2 7/8] rtc: imx-sc: Align imx sc msg structs to 4

Quoted 5 lines · Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Quoted 9 lines

Fix by marking with __aligned(4).

Fixes a3094fc1a15e ("rtc: imx-sc: add rtc alarm support")
---
 drivers/rtc/rtc-imx-sc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Applied, thanks.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#22 lore raw

PATCH v2 8/8 soc: imx-scu: Align imx sc msg structs to 4

In reply to Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes 73feb4d0f8f1 ("soc: imx-scu: Add SoC UID(unique identifier) support")
---
 drivers/soc/imx/soc-imx-scu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/soc/imx/soc-imx-scu.c +1 -1
index fb70b8a3f7c5..20d37eaeb5f2 100644
@@ -23,11 +23,11 @@ struct imx_sc_msg_misc_get_soc_id {
 		} __packed req;
 		struct {
 			u32 id;
 		} resp;
 	} data;
-} __packed;
+} __packed __aligned(4);
 
 struct imx_sc_msg_misc_get_soc_uid {
 	struct imx_sc_rpc_msg hdr;
 	u32 uid_low;
 	u32 uid_high;
Signature · 1 line
2.17.1
Applied
#23 lore raw

Re: [PATCH v2 8/8] soc: imx-scu: Align imx sc msg structs to 4

Quoted 9 lines · Leonard Crestez

The imx SC api strongly assumes that messages are composed out of 4-bytes words but some of our message structs have odd sizeofs.

This produces many oopses with CONFIG_KASAN=y.

Fix by marking with __aligned(4).

Fixes 73feb4d0f8f1 ("soc: imx-scu: Add SoC UID(unique identifier) support")

Applied, thanks.