You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "jerpelea (via GitHub)" <gi...@apache.org> on 2025/06/02 07:38:13 UTC

[PR] drivers/serial: fix race conditions [nuttx]

jerpelea opened a new pull request, #16466:
URL: https://github.com/apache/nuttx/pull/16466

   ## Summary
   
   While going through the code in drivers/serial/serial.c, I noticed this 
   comment:
   
   The head and tail pointers are 16-bit values.  The only time that
   the following could be unsafe is if the CPU made two non-atomic
   8-bit accesses to obtain the 16-bit head index.
   
   This is what happens for (at least) AVR architecture. These are 8bit 
   microcontrollers and as such, they will fetch the 16-bit value in two 
   8-bit load instructions; interrupt routine may execute between those and 
   change the value being read. This will result in corrupted read (one 
   byte of the value will be from pre-interrupt state and the other from 
   post-interrupt state.)
   
   This patch introduces CONFIG_ARCH_LD_16BIT_NOT_ATOMIC configuration 
   option, which is automatically selected for architectures known to be 
   unable to load 16bit values in one instruction. (It is currently only 
   set for AVR. I presume it might be also useful for Z80 but I do not have 
   any experience with that architecture so I did no changes there.) When 
   this configuration option is set, reads of recv.head and xmit.tail are 
   enclosed with enter_critical_section and leave_critical_section calls to 
   prevent interrupt handlers from running, if needed. Not all reads need 
   to be protected this way - some are already in existing critical 
   sections and some happen with the UART-specific interrupt disabled.
   
   If the configuration option is not set, the code simply loads the value 
   into a local variable. Subsequent direct uses of the unprotected 
   volatile variable are replaced with the local variables for both cases.
   
   There is also a related change that only applies when 
   CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS is set. Aside from the protection 
   selected by CONFIG_ARCH_LD_16BIT_NOT_ATOMIC, this patch also fixes 
   calculation of the nbuffered value. This calculation is not running with 
   interrupts disabled and value of rxbuf->head can change between the 
   condition and actual computation. Even if the load itself is atomic, 
   this leads to TOCTOU error.
   
   ## Impact
   
   this change should not impact architectures that do not benefit 
   from this change at all unless CONFIG_SERIAL_IFLOWCONTROL is set. If 
   CONFIG_SERIAL_IFLOWCONTROL is set, the only change that remains in 
   effect is the fix of the TOCTOU error.
   
   ## Testing
   
    Patch was tested with rv-virt:nsh - disassembly of functions 
   from drivers/serial/serial.c was compared and did not change after the 
   patch was applied - with one exception. The exception was an address of 
   a global variable, I assume it was caused by change of g_version length 
   (which notes that the tree is dirty) and is therefore inconsequential. 
   CONFIG_SERIAL_IFLOWCONTROL was not set in this test.
   
   Configuration with CONFIG_SERIAL_IFLOWCONTROL set was tested by building 
   it for AVR DA/DB. Configuration with CONFIG_SERIAL_IFLOWCONTROL unset 
   was tested by custom echo application running on AVR128DA28 for a few 
   hours.
   
   I also ran CI test: 1044 passed, 10 skipped, 14 deselected, 3 warnings 
   in 2463.23s (0:41:03)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#issuecomment-2980368396

   > @acassis @cederom any remaining concerns?
   
   not fix my comment yet.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2135965309


##########
include/nuttx/serial/serial.h:
##########
@@ -123,14 +123,25 @@
 /* This structure defines one serial I/O buffer.
  * The serial infrastructure will initialize the 'sem' field but all other
  * fields must be initialized by the caller of uart_register().
+ *
+ * Maximum buffer size is reduced to 8 bits on architectures where 16bit
+ * load takes two instructions and is therefore not atomic. This prevents
+ * corrupted read if the value is changed in an interrupt handler while
+ * being loaded in non-interrupt code.
  */
 
 struct uart_buffer_s
 {
   mutex_t          lock;   /* Used to control exclusive access to the buffer */
+#ifndef CONFIG_ARCH_LD_16BIT_NOT_ATOMIC
   volatile int16_t head;   /* Index to the head [IN] index in the buffer */
   volatile int16_t tail;   /* Index to the tail [OUT] index in the buffer */
   int16_t          size;   /* The allocated size of the buffer */
+#else
+  volatile uint8_t head;   /* Index to the head [IN] index in the buffer */

Review Comment:
   could we extract a common typedef to avoid #ifdef/#else/#endif spread the code base?



##########
include/nuttx/serial/serial.h:
##########
@@ -123,14 +123,25 @@
 /* This structure defines one serial I/O buffer.
  * The serial infrastructure will initialize the 'sem' field but all other
  * fields must be initialized by the caller of uart_register().
+ *
+ * Maximum buffer size is reduced to 8 bits on architectures where 16bit

Review Comment:
   move to help of ARCH_LD_16BIT_NOT_ATOMIC



##########
drivers/serial/Kconfig-usart:
##########
@@ -130,13 +132,15 @@ menu "USART1 Configuration"
 
 config USART1_RXBUFSIZE
 	int "Receive buffer size"
+	default 128 if ARCH_LD_16BIT_NOT_ATOMIC
 	default 256

Review Comment:
   could we directly change the default value to 255?



##########
drivers/serial/Kconfig-usart:
##########
@@ -705,13 +723,15 @@ menu "USART9 Configuration"
 
 config USART9_RXBUFSIZE
 	int "Receive buffer size"
+	default 128 if ARCH_LD_16BIT_NOT_ATOMIC
 	default 256
 	---help---
 		Characters are buffered as they are received. This specifies
 		the size of the receive buffer.
 
 config USART9_TXBUFSIZE
 	int "Transmit buffer size"
+	default 128 if ARCH_LD_16BIT_NOT_ATOMIC

Review Comment:
   can we change to ARCH_HAVE_8BIT_BUFSIZE  and put into serial/Kconfig?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2140495844


##########
include/nuttx/serial/serial.h:
##########
@@ -123,14 +123,25 @@
 /* This structure defines one serial I/O buffer.
  * The serial infrastructure will initialize the 'sem' field but all other
  * fields must be initialized by the caller of uart_register().
+ *
+ * Maximum buffer size is reduced to 8 bits on architectures where 16bit

Review Comment:
   OK



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis merged PR #16466:
URL: https://github.com/apache/nuttx/pull/16466


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "jerpelea (via GitHub)" <gi...@apache.org>.
jerpelea commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2139918418


##########
drivers/serial/Kconfig-usart:
##########
@@ -705,13 +723,15 @@ menu "USART9 Configuration"
 
 config USART9_RXBUFSIZE
 	int "Receive buffer size"
+	default 128 if ARCH_LD_16BIT_NOT_ATOMIC
 	default 256
 	---help---
 		Characters are buffered as they are received. This specifies
 		the size of the receive buffer.
 
 config USART9_TXBUFSIZE
 	int "Transmit buffer size"
+	default 128 if ARCH_LD_16BIT_NOT_ATOMIC

Review Comment:
   I am not sure if that means to a) change the name from 
   ARCH_LD_16BIT_NOT_ATOMIC to ARCH_HAVE_8BIT_BUFSIZE or b) add 
   ARCH_HAVE_8BIT_BUFSIZE and have it selected by ARCH_LD_16BIT_NOT_ATOMIC? 
   Neither seems fully correct though.
   
   The first variant reduces the problem to buffers only but that is too 
   narrow. Essentially, any other value that is simultaneously written in 
   interrupt code and read in non-interrupt code needs the be treated in 
   the same manner as buffer head/tail here. The current name reflects 
   that. Also, adding ARCH_LD_32BIT_NOT_ATOMIC may be needed as well 
   (possibly not only for AVR but for 16 bit microcontrollers too - if I 
   understand it correctly, that applies to Freescale M9S12.)
   
   The second variant implies that all of the arch has 8 bit buffer size 
   for all buffers everywhere which is not true and may not be desirable.
   
   How about adding ARCH_HAVE_8BIT_SERIAL_BUFSIZE to serial/Kconfig and 
   have it selected by ARCH_LD_16BIT_NOT_ATOMIC?
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "jlaitine (via GitHub)" <gi...@apache.org>.
jlaitine commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2131598395


##########
drivers/serial/serial.c:
##########
@@ -248,9 +249,26 @@ static int uart_putxmitchar(FAR uart_dev_t *dev, int ch, bool oktoblock)
           nexthead = 0;
         }
 
-      /* Check if the TX buffer is full */
+      /* Check if the TX buffer is full
+       *
+       * This method is currently called from uart_readv and uart_writev.
+       * The latter calls it after a call to uart_disabletxint, meaning
+       * transmit interrupt cannot happen. The former, however, runs
+       * with interrupt handling enabled. Interrupt handler can therefore
+       * execute in the middle of loading xmit.tail. If the load isn't
+       * atomic (8bit MCUs), it must run with interrupts disabled because
+       * the interrupt handler changes xmit.tail.
+       */
+
+#ifdef CONFIG_ARCH_LD_16BIT_NOT_ATOMIC
+      flags = enter_critical_section();
+      tail = dev->xmit.tail;

Review Comment:
   I don't see a platform supporting SMP where 16-bit reads would not be atomic. The whole flag is relevant only for 8-bit microncontrollers



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2124313387


##########
drivers/serial/serial.c:
##########
@@ -248,9 +249,26 @@ static int uart_putxmitchar(FAR uart_dev_t *dev, int ch, bool oktoblock)
           nexthead = 0;
         }
 
-      /* Check if the TX buffer is full */
+      /* Check if the TX buffer is full
+       *
+       * This method is currently called from uart_readv and uart_writev.
+       * The latter calls it after a call to uart_disabletxint, meaning
+       * transmit interrupt cannot happen. The former, however, runs
+       * with interrupt handling enabled. Interrupt handler can therefore
+       * execute in the middle of loading xmit.tail. If the load isn't
+       * atomic (8bit MCUs), it must run with interrupts disabled because
+       * the interrupt handler changes xmit.tail.
+       */
+
+#ifdef CONFIG_ARCH_LD_16BIT_NOT_ATOMIC
+      flags = enter_critical_section();
+      tail = dev->xmit.tail;

Review Comment:
   let's use atomic_xxx api instead the critical section, or read twice to confirm there is no change. To improve the concurrency in SMP, the common code should avoid to use the critical section as much as possible



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2121299247


##########
drivers/serial/serial.c:
##########
@@ -1875,7 +1933,18 @@ static int uart_poll(FAR struct file *filep,
           ndx = 0;
         }
 
-      if (ndx != dev->xmit.tail)
+      /* See other occurences of this - transmit interrupt handler

Review Comment:
   @jerpelea please fix occurences -> to let it pass on CI



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2124055054


##########
drivers/serial/serial.c:
##########
@@ -248,9 +249,26 @@ static int uart_putxmitchar(FAR uart_dev_t *dev, int ch, bool oktoblock)
           nexthead = 0;
         }
 
-      /* Check if the TX buffer is full */
+      /* Check if the TX buffer is full
+       *
+       * This method is currently called from uart_readv and uart_writev.
+       * The latter calls it after a call to uart_disabletxint, meaning
+       * transmit interrupt cannot happen. The former, however, runs
+       * with interrupt handling enabled. Interrupt handler can therefore
+       * execute in the middle of loading xmit.tail. If the load isn't
+       * atomic (8bit MCUs), it must run with interrupts disabled because

Review Comment:
   This is his reply:
   
   "limiting the buffer size under 256 bytes would work too but there are a
   lot of default configurations that use 256 bytes or more so I think
   doing it like that may break things for people.
   
   Another option is sort of middle-ground - keep the
   CONFIG_ARCH_LD_16BIT_NOT_ATOMIC configuration option and determine
   maximum buffer size and type for head/tail variables based on if it is
   set. If set, the type changes to uint8_t and maximum buffer size will be
   restricted to (let's say) 128.
   
   This way, the risk of introducing some bug would be lower as well too.
   
   However, I expect this is not the only case where such a race condition
   exists for 8bit architectures. For example, the circbuf library uses
   size_t for head and tail variables so it's likely that a similar change
   is needed there as well."
   
   So, let's include it, since it could be used in other areas for 8-bit MCUs



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "jlaitine (via GitHub)" <gi...@apache.org>.
jlaitine commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2152247517


##########
drivers/serial/serial.c:
##########
@@ -901,12 +901,21 @@ static ssize_t uart_readv(FAR struct file *filep, FAR struct uio *uio)
 #ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
   unsigned int nbuffered;
   unsigned int watermark;
+#  ifndef CONFIG_ARCH_LD_16BIT_NOT_ATOMIC
+  int16_t head;

Review Comment:
   I find it a bit strange that the 16-bit indexes are signed, but th 8-bit versions are unsigned. But I guess a good reasoning is not to change the original 16-bit indexies unnecessarily.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2124313387


##########
drivers/serial/serial.c:
##########
@@ -248,9 +249,26 @@ static int uart_putxmitchar(FAR uart_dev_t *dev, int ch, bool oktoblock)
           nexthead = 0;
         }
 
-      /* Check if the TX buffer is full */
+      /* Check if the TX buffer is full
+       *
+       * This method is currently called from uart_readv and uart_writev.
+       * The latter calls it after a call to uart_disabletxint, meaning
+       * transmit interrupt cannot happen. The former, however, runs
+       * with interrupt handling enabled. Interrupt handler can therefore
+       * execute in the middle of loading xmit.tail. If the load isn't
+       * atomic (8bit MCUs), it must run with interrupts disabled because
+       * the interrupt handler changes xmit.tail.
+       */
+
+#ifdef CONFIG_ARCH_LD_16BIT_NOT_ATOMIC
+      flags = enter_critical_section();
+      tail = dev->xmit.tail;

Review Comment:
   let's use atomic_xxx api instead the critical section



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "jerpelea (via GitHub)" <gi...@apache.org>.
jerpelea commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2139917822


##########
include/nuttx/serial/serial.h:
##########
@@ -123,14 +123,25 @@
 /* This structure defines one serial I/O buffer.
  * The serial infrastructure will initialize the 'sem' field but all other
  * fields must be initialized by the caller of uart_register().
+ *
+ * Maximum buffer size is reduced to 8 bits on architectures where 16bit
+ * load takes two instructions and is therefore not atomic. This prevents
+ * corrupted read if the value is changed in an interrupt handler while
+ * being loaded in non-interrupt code.
  */
 
 struct uart_buffer_s
 {
   mutex_t          lock;   /* Used to control exclusive access to the buffer */
+#ifndef CONFIG_ARCH_LD_16BIT_NOT_ATOMIC
   volatile int16_t head;   /* Index to the head [IN] index in the buffer */
   volatile int16_t tail;   /* Index to the tail [OUT] index in the buffer */
   int16_t          size;   /* The allocated size of the buffer */
+#else
+  volatile uint8_t head;   /* Index to the head [IN] index in the buffer */

Review Comment:
   Can do - sbuf_size_t seems unused throughout the whole tree so I'll add 
   that to serial.h.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2140489324


##########
drivers/serial/Kconfig-usart:
##########
@@ -130,13 +132,15 @@ menu "USART1 Configuration"
 
 config USART1_RXBUFSIZE
 	int "Receive buffer size"
+	default 128 if ARCH_LD_16BIT_NOT_ATOMIC
 	default 256

Review Comment:
   yes, 252 is a better number, thanks.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2121473225


##########
drivers/serial/serial.c:
##########
@@ -248,9 +249,26 @@ static int uart_putxmitchar(FAR uart_dev_t *dev, int ch, bool oktoblock)
           nexthead = 0;
         }
 
-      /* Check if the TX buffer is full */
+      /* Check if the TX buffer is full
+       *
+       * This method is currently called from uart_readv and uart_writev.
+       * The latter calls it after a call to uart_disabletxint, meaning
+       * transmit interrupt cannot happen. The former, however, runs
+       * with interrupt handling enabled. Interrupt handler can therefore
+       * execute in the middle of loading xmit.tail. If the load isn't
+       * atomic (8bit MCUs), it must run with interrupts disabled because

Review Comment:
   should we simply limit the buffer size small than 256B and change tail/head to uint8_t in this case?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "jerpelea (via GitHub)" <gi...@apache.org>.
jerpelea commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2139921657


##########
include/nuttx/serial/serial.h:
##########
@@ -123,14 +123,25 @@
 /* This structure defines one serial I/O buffer.
  * The serial infrastructure will initialize the 'sem' field but all other
  * fields must be initialized by the caller of uart_register().
+ *
+ * Maximum buffer size is reduced to 8 bits on architectures where 16bit

Review Comment:
   I think it would be better to put something more generic in there. 
   Additionally, I'll copy the comment about the buffer size to help for 
   USARTn_RX/TXBUFSIZE.



##########
drivers/serial/Kconfig-usart:
##########
@@ -130,13 +132,15 @@ menu "USART1 Configuration"
 
 config USART1_RXBUFSIZE
 	int "Receive buffer size"
+	default 128 if ARCH_LD_16BIT_NOT_ATOMIC
 	default 256

Review Comment:
   Can be done but that would affect other architectures as well which is 
   something I wanted to avoid. Also, using 255 feels dangerous when the 
   underlying type is uint8_t. Might lead to bugs where expression like (x 
   + 1 > size) never evaluates to true. Not that I see any such code in 
   current sources, but if the default value should change for all 
   architectures, I would rather use 252 out of abundance of caution.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "jerpelea (via GitHub)" <gi...@apache.org>.
jerpelea commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2139921332


##########
drivers/serial/serial.c:
##########
@@ -901,6 +901,11 @@ static ssize_t uart_readv(FAR struct file *filep, FAR struct uio *uio)
 #ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
   unsigned int nbuffered;
   unsigned int watermark;
+#  ifndef CONFIG_ARCH_LD_16BIT_NOT_ATOMIC
+  int16_t head;

Review Comment:
   Because it fixes different bug (on all architectures.) I will reorder 
   the patches to make that more obvious and to have the less intrusive one 
   be the first in case it turns out that the second one needs to be 
   reverted.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2124309089


##########
drivers/serial/serial.c:
##########
@@ -248,9 +249,26 @@ static int uart_putxmitchar(FAR uart_dev_t *dev, int ch, bool oktoblock)
           nexthead = 0;
         }
 
-      /* Check if the TX buffer is full */
+      /* Check if the TX buffer is full
+       *
+       * This method is currently called from uart_readv and uart_writev.
+       * The latter calls it after a call to uart_disabletxint, meaning
+       * transmit interrupt cannot happen. The former, however, runs
+       * with interrupt handling enabled. Interrupt handler can therefore
+       * execute in the middle of loading xmit.tail. If the load isn't
+       * atomic (8bit MCUs), it must run with interrupts disabled because

Review Comment:
   only avr need limit to 256 byte, since avr is a 8/16bit arch, 256 is reasonable.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "jerpelea (via GitHub)" <gi...@apache.org>.
jerpelea commented on PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#issuecomment-2986823530

   > > @acassis @cederom any remaining concerns?
   > 
   > not fix my comment yet.
   
   updated


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "jerpelea (via GitHub)" <gi...@apache.org>.
jerpelea commented on PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#issuecomment-2980021727

   @acassis @cederom any remaining concerns?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2140510794


##########
drivers/serial/Kconfig-usart:
##########
@@ -705,13 +723,15 @@ menu "USART9 Configuration"
 
 config USART9_RXBUFSIZE
 	int "Receive buffer size"
+	default 128 if ARCH_LD_16BIT_NOT_ATOMIC
 	default 256
 	---help---
 		Characters are buffered as they are received. This specifies
 		the size of the receive buffer.
 
 config USART9_TXBUFSIZE
 	int "Transmit buffer size"
+	default 128 if ARCH_LD_16BIT_NOT_ATOMIC

Review Comment:
   > I am not sure if that means to a) change the name from ARCH_LD_16BIT_NOT_ATOMIC to ARCH_HAVE_8BIT_BUFSIZE or b) add ARCH_HAVE_8BIT_BUFSIZE and have it selected by ARCH_LD_16BIT_NOT_ATOMIC? Neither seems fully correct though.
   > 
   
   change to ARCH_HAVE_SERIAL_8BIT_BUFSIZE and put after:
   https://github.com/apache/nuttx/blob/master/drivers/serial/Kconfig#L6
   select it from AVR serial driver Kconfig option.
   
   > The first variant reduces the problem to buffers only but that is too narrow. Essentially, any other value that is simultaneously written in interrupt code and read in non-interrupt code needs the be treated in the same manner as buffer head/tail here. The current name reflects that. Also, adding ARCH_LD_32BIT_NOT_ATOMIC may be needed as well (possibly not only for AVR but for 16 bit microcontrollers too - if I understand it correctly, that applies to Freescale M9S12.)
   > 
   
   16bit arch normally support the atomic access to 16bit data, it's strange that AVR can't support it.
   
   > The second variant implies that all of the arch has 8 bit buffer size for all buffers everywhere which is not true and may not be desirable.
   > 
   > How about adding ARCH_HAVE_8BIT_SERIAL_BUFSIZE to serial/Kconfig and have it selected by 
   
   yes, it's better name.
   
   > ARCH_LD_16BIT_NOT_ATOMIC?
   
   if you are considering other modules may have the similar issue, ARCH_LD_16BIT_NOT_ATOMIC is better to keep, but should we remove LD_ from name since store shouldn't be atomic too?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2140510794


##########
drivers/serial/Kconfig-usart:
##########
@@ -705,13 +723,15 @@ menu "USART9 Configuration"
 
 config USART9_RXBUFSIZE
 	int "Receive buffer size"
+	default 128 if ARCH_LD_16BIT_NOT_ATOMIC
 	default 256
 	---help---
 		Characters are buffered as they are received. This specifies
 		the size of the receive buffer.
 
 config USART9_TXBUFSIZE
 	int "Transmit buffer size"
+	default 128 if ARCH_LD_16BIT_NOT_ATOMIC

Review Comment:
   > I am not sure if that means to a) change the name from ARCH_LD_16BIT_NOT_ATOMIC to ARCH_HAVE_8BIT_BUFSIZE or b) add ARCH_HAVE_8BIT_BUFSIZE and have it selected by ARCH_LD_16BIT_NOT_ATOMIC? Neither seems fully correct though.
   > 
   
   change to ARCH_HAVE_SERIAL_8BIT_BUFSIZE and put after:
   https://github.com/apache/nuttx/blob/master/drivers/serial/Kconfig#L6
   select it from AVR serial driver Kconfig option.
   
   > The first variant reduces the problem to buffers only but that is too narrow. Essentially, any other value that is simultaneously written in interrupt code and read in non-interrupt code needs the be treated in the same manner as buffer head/tail here. The current name reflects that. Also, adding ARCH_LD_32BIT_NOT_ATOMIC may be needed as well (possibly not only for AVR but for 16 bit microcontrollers too - if I understand it correctly, that applies to Freescale M9S12.)
   > 
   
   16bit arch normally support the atomic access to 16bit data, it's strange that AVR doesn't support it.
   
   > The second variant implies that all of the arch has 8 bit buffer size for all buffers everywhere which is not true and may not be desirable.
   > 
   > How about adding ARCH_HAVE_8BIT_SERIAL_BUFSIZE to serial/Kconfig and have it selected by 
   
   yes, it's better name.
   
   > ARCH_LD_16BIT_NOT_ATOMIC?
   
   if you consider to other module may have the similar issue, ARCH_LD_16BIT_NOT_ATOMIC is better to keep, but should we remove LD_ from name since store shouldn't be atomic too.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] drivers/serial: fix race conditions [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #16466:
URL: https://github.com/apache/nuttx/pull/16466#discussion_r2136262355


##########
drivers/serial/serial.c:
##########
@@ -901,6 +901,11 @@ static ssize_t uart_readv(FAR struct file *filep, FAR struct uio *uio)
 #ifdef CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS
   unsigned int nbuffered;
   unsigned int watermark;
+#  ifndef CONFIG_ARCH_LD_16BIT_NOT_ATOMIC
+  int16_t head;

Review Comment:
   why not merge to previous patch



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org