Skip to contentSkip to conversation

mailbox: ti-msgmgr: Fixes for polled rx and trailing bytes

PATCH Superseded Beleswar Padhi 3 participants 9 messages Sep 29 – Oct 2 Mailbox API

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover mailbox: ti-msgmgr: Fixes for polled rx and trailing bytes

This series fixes two issues in ti_msgmgr_send_data(), found while refactoring the ti-msgmgr mbox client (TI-SCI).

Patch 1 fixes a duplicate message transmission in polled rx mode. Patch 2 stops reading past the end of the message buffer when the message length is not a multiple of 4. This is a preparatory patch for when the mbox client can send data which is not a multiple of 4.

This series is independent and can be applied directly.

Testing Done:
- Boot tested on TI K3 J784S4 EVM.
- Tested that these patches do not introduce any new warning or error.

Thanks,
Beleswar

Beleswar Padhi (2):
mailbox: ti-msgmgr: Do not report rx poll timeout as a send failure
mailbox: ti-msgmgr: Read exact number of trailing message bytes

 drivers/mailbox/ti-msgmgr.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 mailbox: ti-msgmgr: Do not report rx poll timeout as a send failure

In polled rx mode, used from system suspend until resume, ti_msgmgr_send_data() writes the message to the tx queue and then waits for the response. If no response arrives in time it returns the poll error.

The mailbox core treats a ->send_data() error as a send failure. So, it leaves the message queued without making it the active request, and mbox_send_message() still returns success. The message is then submitted again on the next tx_tick() or mbox_send_message(), after the client has probably timed out and moved on. So, the firmware receives the same message twice, and later requests are sent late and can have responses matched to the wrong request.

The message has already been transmitted at this point, so return 0 and only log the missing response. The client detects this case with its own response timeout anyways.

---
 drivers/mailbox/ti-msgmgr.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/mailbox/ti-msgmgr.c +15 -3
index 8eb8df8d95a4c..425d5f9d9d0e3 100644
@@ -445,12 +445,24 @@ static int ti_msgmgr_send_data(struct mbox_chan *chan, void *data)
 		data_reg += sizeof(u32);
 	}
 
-	/* If we are in polled mode, wait for a response before proceeding */
-	if (ti_msgmgr_chan_has_polled_queue_rx(message->chan_rx))
+	/*
+	 * If we are in polled mode, wait for a response before proceeding.
+	 *
+	 * The message has already been transmitted at this point, so do not
+	 * report a missing response as a send failure. Doing so would make
+	 * the mailbox core keep the message queued and submit it again later,
+	 * after the client has possibly given up on it. The client detects the
+	 * missing response by itself timing out.
+	 */
+	if (ti_msgmgr_chan_has_polled_queue_rx(message->chan_rx)) {
 		ret = ti_msgmgr_queue_rx_poll_timeout(message->chan_rx,
 						      message->timeout_rx_ms * 1000);
+		if (ret)
+			dev_err(dev, "Queue %s timed out waiting for response: %d\n",
+				qinst->name, ret);
+	}
 
-	return ret;
+	return 0;
 }
 
 /**
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/2 mailbox: ti-msgmgr: Read exact number of trailing message bytes

In reply to Beleswar Padhi

ti_msgmgr_send_data() copies the message to the data registers in u32 units. This has been harmless so far because the clients (TI-SCI) always passed its preallocated message buffer, which is sized to the maximum message size (60 or 64 bytes, a multiple of 4), so the extra bytes read were still within the buffer. But, with a later TI-SCI update, the message size can be varying now and not guaranteed to be a multiple of 4, which would trigger KASAN out-of-bounds reports.

Therefore, Build the trailing word by reading one byte at a time, only up to the message length. The value written to the register is unchanged.

---
 drivers/mailbox/ti-msgmgr.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/mailbox/ti-msgmgr.c +12 -4
index 425d5f9d9d0e3..44457a896510f 100644
@@ -425,10 +425,18 @@ static int ti_msgmgr_send_data(struct mbox_chan *chan, void *data)
 
 	trail_bytes = message->len % sizeof(u32);
 	if (trail_bytes) {
-		u32 data_trail = *word_data;
-
-		/* Ensure all unused data is 0 */
-		data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
+		/*
+		 * Read the trailing bytes one at a time instead of as a full
+		 * u32, as the message buffer may end right after them and a
+		 * u32 read would go past the end of it. This also leaves all
+		 * unused data as 0.
+		 */
+		u8 *byte_data = (u8 *)word_data;
+		u32 data_trail = 0;
+		int i;
+
+		for (i = 0; i < trail_bytes; i++)
+			data_trail |= byte_data[i] << (8 * i);
 		writel(data_trail, data_reg);
 		data_reg += sizeof(u32);
 	}
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH 2/2] mailbox: ti-msgmgr: Read exact number of trailing message bytes

Quoted 8 lines · Beleswar Padhi

ti_msgmgr_send_data() copies the message to the data registers in u32 units. This has been harmless so far because the clients (TI-SCI) always passed its preallocated message buffer, which is sized to the maximum message size (60 or 64 bytes, a multiple of 4), so the extra bytes read were still within the buffer. But, with a later TI-SCI update, the message size can be varying now and not guaranteed to be a multiple of 4, which would trigger KASAN out-of-bounds reports.

Quoted 5 lines

Therefore, Build the trailing word by reading one byte at a time, only up to the message length. The value written to the register is unchanged.

This looks like a bug fixes and needs a Fixes: tag?

Quoted 31 lines
---
 drivers/mailbox/ti-msgmgr.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/mailbox/ti-msgmgr.c +12 -4
index 425d5f9d9d0e3..44457a896510f 100644
@@ -425,10 +425,18 @@ static int ti_msgmgr_send_data(struct mbox_chan *chan, void *data)
 
 	trail_bytes = message->len % sizeof(u32);
 	if (trail_bytes) {
-		u32 data_trail = *word_data;
-
-		/* Ensure all unused data is 0 */
-		data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
+		/*
+		 * Read the trailing bytes one at a time instead of as a full
+		 * u32, as the message buffer may end right after them and a
+		 * u32 read would go past the end of it. This also leaves all
+		 * unused data as 0.
+		 */
+		u8 *byte_data = (u8 *)word_data;
+		u32 data_trail = 0;
+		int i;
+
+		for (i = 0; i < trail_bytes; i++)
+			data_trail |= byte_data[i] << (8 * i);
 		writel(data_trail, data_reg);
 		data_reg += sizeof(u32);
 	}
Signature · 3 lines
Regards
Vignesh
https://ti.com/opensource
#4 lore raw

Re: [PATCH 2/2] mailbox: ti-msgmgr: Read exact number of trailing message bytes

Quoted 10 lines · Vignesh Raghavendra
Quoted 8 lines · Beleswar Padhi

ti_msgmgr_send_data() copies the message to the data registers in u32 units. This has been harmless so far because the clients (TI-SCI) always passed its preallocated message buffer, which is sized to the maximum message size (60 or 64 bytes, a multiple of 4), so the extra bytes read were still within the buffer. But, with a later TI-SCI update, the message size can be varying now and not guaranteed to be a multiple of 4, which would trigger KASAN out-of-bounds reports.

Quoted 6 lines
Quoted 5 lines

Therefore, Build the trailing word by reading one byte at a time, only up to the message length. The value written to the register is unchanged.

This looks like a bug fixes and needs a Fixes: tag?

All mbox clients of ti-msgmgr currently send 60/64 byte sized messages. So this is not a bug today. It is only a preparatory patch for future mbox clients which can send message sizes like 19 bytes etc.

Thanks,
Beleswar

Quoted 32 lines
Quoted 32 lines
---
  drivers/mailbox/ti-msgmgr.c | 16 ++++++++++++----
  1 file changed, 12 insertions(+), 4 deletions(-)
drivers/mailbox/ti-msgmgr.c +12 -4
index 425d5f9d9d0e3..44457a896510f 100644
@@ -425,10 +425,18 @@ static int ti_msgmgr_send_data(struct mbox_chan *chan, void *data)
 
  	trail_bytes = message->len % sizeof(u32);
  	if (trail_bytes) {
-		u32 data_trail = *word_data;
-
-		/* Ensure all unused data is 0 */
-		data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
+		/*
+		 * Read the trailing bytes one at a time instead of as a full
+		 * u32, as the message buffer may end right after them and a
+		 * u32 read would go past the end of it. This also leaves all
+		 * unused data as 0.
+		 */
+		u8 *byte_data = (u8 *)word_data;
+		u32 data_trail = 0;
+		int i;
+
+		for (i = 0; i < trail_bytes; i++)
+			data_trail |= byte_data[i] << (8 * i);
  		writel(data_trail, data_reg);
  		data_reg += sizeof(u32);
  	}
#5 lore raw

Re: [PATCH 2/2] mailbox: ti-msgmgr: Read exact number of trailing message bytes

Quoted 16 lines · Beleswar
Quoted 14 lines · Vignesh Raghavendra
Quoted 12 lines · Beleswar Padhi

ti_msgmgr_send_data() copies the message to the data registers in u32 units. This has been harmless so far because the clients (TI-SCI) always passed its preallocated message buffer, which is sized to the maximum message size (60 or 64 bytes, a multiple of 4), so the extra bytes read were still within the buffer. But, with a later TI-SCI update, the message size can be varying now and not guaranteed to be a multiple of 4, which would trigger KASAN out-of-bounds reports.

Therefore, Build the trailing word by reading one byte at a time, only up to the message length. The value written to the register is unchanged.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

This looks like a bug fixes and needs a Fixes: tag?

All mbox clients of ti-msgmgr currently send 60/64 byte sized messages. So this is not a bug today. It is only a preparatory patch for future mbox clients which can send message sizes like 19 bytes etc.

It's still a bug, even if no one happened to run into it yet. Probably good to add the Fixes tag anyway, just in case something else every gets backported also that needs the non-4-byte aligned reads.

Andrew

Quoted 34 lines

Thanks,
Beleswar

Quoted 31 lines
Quoted 31 lines

---
  drivers/mailbox/ti-msgmgr.c | 16 ++++++++++++----

  1 file changed, 12 insertions(+), 4 deletions(-)
drivers/mailbox/ti-msgmgr.c +12 -4
index 425d5f9d9d0e3..44457a896510f 100644
@@ -425,10 +425,18 @@ static int ti_msgmgr_send_data(struct mbox_chan *chan, void *data)
       trail_bytes = message->len % sizeof(u32);
       if (trail_bytes) {
-        u32 data_trail = *word_data;
-
-        /* Ensure all unused data is 0 */
-        data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
+        /*
+         * Read the trailing bytes one at a time instead of as a full
+         * u32, as the message buffer may end right after them and a
+         * u32 read would go past the end of it. This also leaves all
+         * unused data as 0.
+         */
+        u8 *byte_data = (u8 *)word_data;
+        u32 data_trail = 0;
+        int i;
+
+        for (i = 0; i < trail_bytes; i++)
+            data_trail |= byte_data[i] << (8 * i);
           writel(data_trail, data_reg);
           data_reg += sizeof(u32);
       }
#6 lore raw

Re: [PATCH 2/2] mailbox: ti-msgmgr: Read exact number of trailing message bytes

Quoted 21 lines · Andrew Davis
Quoted 20 lines · Beleswar
Quoted 16 lines · Vignesh Raghavendra
Quoted 8 lines · Beleswar Padhi

ti_msgmgr_send_data() copies the message to the data registers in u32 units. This has been harmless so far because the clients (TI-SCI) always passed its preallocated message buffer, which is sized to the maximum message size (60 or 64 bytes, a multiple of 4), so the extra bytes read were still within the buffer. But, with a later TI-SCI update, the message size can be varying now and not guaranteed to be a multiple of 4, which would trigger KASAN out-of-bounds reports.

Quoted 5 lines

Therefore, Build the trailing word by reading one byte at a time, only up to the message length. The value written to the register is unchanged.

This looks like a bug fixes and needs a Fixes: tag?

Quoted 6 lines
Quoted 4 lines

All mbox clients of ti-msgmgr currently send 60/64 byte sized messages. So this is not a bug today. It is only a preparatory patch for future mbox clients which can send message sizes like 19 bytes etc.

It's still a bug, even if no one happened to run into it yet.

So should every pointer be checked against NULL before dereferencing in all drivers by that logic then?

Quoted 3 lines

Probably good to add the Fixes tag anyway, just in case something else every gets backported also that needs the non-4-byte aligned reads.

Fair, makes sense. Will add Fixes tag in v3.

Thanks,
Beleswar

Quoted 40 lines

Andrew

Quoted 36 lines

Thanks,
Beleswar

Quoted 33 lines
Quoted 33 lines

---
  drivers/mailbox/ti-msgmgr.c | 16 ++++++++++++----

  1 file changed, 12 insertions(+), 4 deletions(-)
drivers/mailbox/ti-msgmgr.c +12 -4
index 425d5f9d9d0e3..44457a896510f 100644
@@ -425,10 +425,18 @@ static int ti_msgmgr_send_data(struct 
 mbox_chan *chan, void *data)
       trail_bytes = message->len % sizeof(u32);
       if (trail_bytes) {
-        u32 data_trail = *word_data;
-
-        /* Ensure all unused data is 0 */
-        data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - 
 trail_bytes));
+        /*
+         * Read the trailing bytes one at a time instead of as a full
+         * u32, as the message buffer may end right after them and a
+         * u32 read would go past the end of it. This also leaves all
+         * unused data as 0.
+         */
+        u8 *byte_data = (u8 *)word_data;
+        u32 data_trail = 0;
+        int i;
+
+        for (i = 0; i < trail_bytes; i++)
+            data_trail |= byte_data[i] << (8 * i);
           writel(data_trail, data_reg);
           data_reg += sizeof(u32);

      }

#7 lore raw

Re: [PATCH 2/2] mailbox: ti-msgmgr: Read exact number of trailing message bytes

Quoted 28 lines · Beleswar
Quoted 26 lines · Andrew Davis
Quoted 24 lines · Beleswar
Quoted 10 lines · Vignesh Raghavendra
Quoted 8 lines · Beleswar Padhi

ti_msgmgr_send_data() copies the message to the data registers in u32 units. This has been harmless so far because the clients (TI-SCI) always passed its preallocated message buffer, which is sized to the maximum message size (60 or 64 bytes, a multiple of 4), so the extra bytes read were still within the buffer. But, with a later TI-SCI update, the message size can be varying now and not guaranteed to be a multiple of 4, which would trigger KASAN out-of-bounds reports.

Quoted 6 lines
Quoted 5 lines

Therefore, Build the trailing word by reading one byte at a time, only up to the message length. The value written to the register is unchanged.

This looks like a bug fixes and needs a Fixes: tag?

All mbox clients of ti-msgmgr currently send 60/64 byte sized messages. So this is not a bug today. It is only a preparatory patch for future mbox clients which can send message sizes like 19 bytes etc.

Quoted 6 lines
Quoted 1 line

It's still a bug, even if no one happened to run into it yet.

So should every pointer be checked against NULL before dereferencing in all drivers by that logic then?

I get what you are saying, but in many cases if passing NULL is allowed or not is an implied contract between caller and callee. In this case the caller explicitly states the message length to read, and the function doesn't respect that value. Which is a more clear case of broken contract, and therefor a bug IMHO.

Andrew

Quoted 48 lines
Quoted 3 lines

Probably good to add the Fixes tag anyway, just in case something else every gets backported also that needs the non-4-byte aligned reads.

Fair, makes sense. Will add Fixes tag in v3.

Thanks,
Beleswar

Quoted 38 lines

Andrew

Quoted 34 lines

Thanks,
Beleswar

Quoted 31 lines
Quoted 31 lines

---
  drivers/mailbox/ti-msgmgr.c | 16 ++++++++++++----

  1 file changed, 12 insertions(+), 4 deletions(-)
drivers/mailbox/ti-msgmgr.c +12 -4
index 425d5f9d9d0e3..44457a896510f 100644
@@ -425,10 +425,18 @@ static int ti_msgmgr_send_data(struct mbox_chan *chan, void *data)
       trail_bytes = message->len % sizeof(u32);
       if (trail_bytes) {
-        u32 data_trail = *word_data;
-
-        /* Ensure all unused data is 0 */
-        data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
+        /*
+         * Read the trailing bytes one at a time instead of as a full
+         * u32, as the message buffer may end right after them and a
+         * u32 read would go past the end of it. This also leaves all
+         * unused data as 0.
+         */
+        u8 *byte_data = (u8 *)word_data;
+        u32 data_trail = 0;
+        int i;
+
+        for (i = 0; i < trail_bytes; i++)
+            data_trail |= byte_data[i] << (8 * i);
           writel(data_trail, data_reg);
           data_reg += sizeof(u32);
       }
#8 lore raw

Re: [PATCH 2/2] mailbox: ti-msgmgr: Read exact number of trailing message bytes

Quoted 36 lines · Andrew Davis
Quoted 28 lines · Beleswar
Quoted 26 lines · Andrew Davis
Quoted 24 lines · Beleswar
Quoted 10 lines · Vignesh Raghavendra
Quoted 8 lines · Beleswar Padhi

ti_msgmgr_send_data() copies the message to the data registers in u32 units. This has been harmless so far because the clients (TI-SCI) always passed its preallocated message buffer, which is sized to the maximum message size (60 or 64 bytes, a multiple of 4), so the extra bytes read were still within the buffer. But, with a later TI-SCI update, the message size can be varying now and not guaranteed to be a multiple of 4, which would trigger KASAN out-of-bounds reports.

Quoted 6 lines
Quoted 5 lines

Therefore, Build the trailing word by reading one byte at a time, only up to the message length. The value written to the register is unchanged.

This looks like a bug fixes and needs a Fixes: tag?

All mbox clients of ti-msgmgr currently send 60/64 byte sized messages. So this is not a bug today. It is only a preparatory patch for future mbox clients which can send message sizes like 19 bytes etc.

Quoted 6 lines
Quoted 1 line

It's still a bug, even if no one happened to run into it yet.

So should every pointer be checked against NULL before dereferencing in all drivers by that logic then?

Quoted 5 lines

I get what you are saying, but in many cases if passing NULL is allowed or not is an implied contract between caller and callee. In this case the caller explicitly states the message length to read, and the function doesn't respect that value. Which is a more clear case of broken contract, and therefor a bug IMHO.

Ah, the beauty. Thank you for providing this reasoning. Being able to back your arguments with something so concrete and correct is something I really respect about you.

Thanks,
Beleswar

Quoted 57 lines

Andrew

Quoted 53 lines
Quoted 3 lines

Probably good to add the Fixes tag anyway, just in case something else every gets backported also that needs the non-4-byte aligned reads.

Fair, makes sense. Will add Fixes tag in v3.

Thanks,
Beleswar

Quoted 43 lines

Andrew

Quoted 39 lines

Thanks,
Beleswar

Quoted 36 lines
Quoted 36 lines

---
  drivers/mailbox/ti-msgmgr.c | 16 ++++++++++++----

  1 file changed, 12 insertions(+), 4 deletions(-)
drivers/mailbox/ti-msgmgr.c +2 -4
index 425d5f9d9d0e3..44457a896510f 100644
@@ -425,10 +425,18 @@ static int ti_msgmgr_send_data(struct 
 mbox_chan *chan, void *data)
       trail_bytes = message->len % sizeof(u32);
       if (trail_bytes) {
-        u32 data_trail = *word_data;
-
-        /* Ensure all unused data is 0 */
-        data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - 
 trail_bytes));
+        /*
+         * Read the trailing bytes one at a time instead of as a 

full
+         * u32, as the message buffer may end right after them and a
+         * u32 read would go past the end of it. This also leaves all
+         * unused data as 0.
+         */
+        u8 *byte_data = (u8 *)word_data;
+        u32 data_trail = 0;
+        int i;
+
+        for (i = 0; i < trail_bytes; i++)
+            data_trail |= byte_data[i] << (8 * i); writel(data_trail, data_reg);
          data_reg += sizeof(u32);
      }