This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 8f9b79298ba085a60b98613f53c60ec6b06ddb15
Author: liaoao <[email protected]>
AuthorDate: Tue Jun 17 14:43:24 2025 +0800

    drivers/ioexpander: replace critical_section to spin_lock
    
    Protect the signal table and the poll waiters of the GPIO driver with a
    per-device spinlock instead of the global critical section, and make
    register_count an atomic_t. The procfs reader now reads register_count
    with atomic_read(), and the nxsig_event_valid() error path no longer
    leaves a critical section that is not entered. register_count is
    updated with atomic_add()/atomic_sub(), which return the old value.
    
    Signed-off-by: liaoao <[email protected]>
---
 drivers/ioexpander/gpio.c       | 36 ++++++++++++++++++++----------------
 include/nuttx/ioexpander/gpio.h |  8 +++++++-
 2 files changed, 27 insertions(+), 17 deletions(-)

diff --git a/drivers/ioexpander/gpio.c b/drivers/ioexpander/gpio.c
index f64e0f7967d..a114d1088e1 100644
--- a/drivers/ioexpander/gpio.c
+++ b/drivers/ioexpander/gpio.c
@@ -329,7 +329,8 @@ static ssize_t gpio_procfs_read(FAR struct file *filep, FAR 
char *buffer,
 
       n += snprintf(line + n, sizeof(line) - n,
                     " regs:%u ints:%" PRIuPTR,
-                    entry->dev->register_count, entry->dev->int_count);
+                    (unsigned int)atomic_read(&entry->dev->register_count),
+                    entry->dev->int_count);
 
       extra[0] = '\0';
       if (entry->dev->gp_ops->go_describe != NULL)
@@ -825,18 +826,17 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
       case GPIOC_REGISTER:
         if (dev->gp_pintype >= GPIO_INTERRUPT_PIN)
           {
-            flags = enter_critical_section();
 #if CONFIG_DEV_GPIO_NSIGNALS > 0
             if (arg)
               {
                 ret = nxsig_event_valid((FAR struct sigevent *)arg);
                 if (ret < 0)
                   {
-                    leave_critical_section(flags);
                     break;
                   }
 
                 pid = nxsched_getpid();
+                flags = spin_lock_irqsave(&dev->lock);
                 for (i = 0; i < CONFIG_DEV_GPIO_NSIGNALS; i++)
                   {
                     FAR struct gpio_signal_s *signal = &dev->gp_signals[i];
@@ -850,23 +850,20 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
                       }
                   }
 
+                spin_unlock_irqrestore(&dev->lock, flags);
                 if (i == CONFIG_DEV_GPIO_NSIGNALS)
                   {
-                    leave_critical_section(flags);
                     ret = -EBUSY;
                     break;
                   }
               }
 #endif
 
-            if (dev->register_count++ > 0)
+            if (atomic_add(&dev->register_count, 1) > 0)
               {
-                leave_critical_section(flags);
                 break;
               }
 
-            leave_critical_section(flags);
-
             /* Register our handler */
 
             DEBUGASSERT(dev->gp_ops->go_attach != NULL);
@@ -894,13 +891,15 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
       case GPIOC_UNREGISTER:
         if (dev->gp_pintype >= GPIO_INTERRUPT_PIN)
           {
-            flags = enter_critical_section();
 #if CONFIG_DEV_GPIO_NSIGNALS > 0
             pid = nxsched_getpid();
+            flags = spin_lock_irqsave(&dev->lock);
             for (i = 0; i < CONFIG_DEV_GPIO_NSIGNALS; i++)
               {
                 if (pid == dev->gp_signals[i].gp_pid)
                   {
+                    FAR struct sigwork_s *work;
+
                     for (j = i + 1; j < CONFIG_DEV_GPIO_NSIGNALS; j++)
                       {
                         if (dev->gp_signals[j].gp_pid == 0)
@@ -916,20 +915,22 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
                       }
 
                     dev->gp_signals[j].gp_pid = 0;
-                    nxsig_cancel_notification(&dev->gp_signals[j].gp_work);
+                    work = &dev->gp_signals[j].gp_work;
+                    spin_unlock_irqrestore(&dev->lock, flags);
+                    nxsig_cancel_notification(work);
+                    flags = spin_lock_irqsave(&dev->lock);
                     break;
                   }
               }
+
+            spin_unlock_irqrestore(&dev->lock, flags);
 #endif
 
-            if (--dev->register_count > 0)
+            if (atomic_sub(&dev->register_count, 1) > 1)
               {
-                leave_critical_section(flags);
                 break;
               }
 
-            leave_critical_section(flags);
-
             /* Make sure that the pin interrupt is disabled */
 
             DEBUGASSERT(dev->gp_ops->go_enable != NULL);
@@ -1077,7 +1078,7 @@ static int gpio_poll(FAR struct file *filep,
 
   /* Are we setting up the poll?  Or tearing it down? */
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&dev->lock);
   if (setup)
     {
 #if CONFIG_DEV_GPIO_NPOLLWAITERS > 0
@@ -1100,7 +1101,9 @@ static int gpio_poll(FAR struct file *filep,
 
               if (dev->int_count != (uintptr_t)(filep->f_priv))
                 {
+                  spin_unlock_irqrestore(&dev->lock, flags);
                   poll_notify(&fds, 1, POLLIN);
+                  return ret;
                 }
 
               break;
@@ -1126,7 +1129,7 @@ static int gpio_poll(FAR struct file *filep,
       fds->priv = NULL;
     }
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&dev->lock, flags);
   return ret;
 }
 
@@ -1193,6 +1196,7 @@ int gpio_pin_register_byname(FAR struct gpio_dev_s *dev,
 
   DEBUGASSERT(dev != NULL && dev->gp_ops != NULL && pinname != NULL);
 
+  spin_lock_init(&dev->lock);
   switch (dev->gp_pintype)
     {
       case GPIO_INPUT_PIN:
diff --git a/include/nuttx/ioexpander/gpio.h b/include/nuttx/ioexpander/gpio.h
index 32d883e12ea..de656e5a21f 100644
--- a/include/nuttx/ioexpander/gpio.h
+++ b/include/nuttx/ioexpander/gpio.h
@@ -33,7 +33,9 @@
 #include <stdint.h>
 #include <stdbool.h>
 
+#include <nuttx/atomic.h>
 #include <nuttx/signal.h>
+#include <nuttx/spinlock_type.h>
 #include <nuttx/fs/ioctl.h>
 
 #ifdef CONFIG_DEV_GPIO
@@ -196,7 +198,7 @@ struct gpio_dev_s
 
   /* Number of times the device has been registered by ioctl */
 
-  uint8_t register_count;
+  atomic_t register_count;
 
   /* Number of times interrupt occurred */
 
@@ -218,6 +220,10 @@ struct gpio_dev_s
   FAR struct pollfd_s *fds[CONFIG_DEV_GPIO_NPOLLWAITERS];
 #endif
 
+  /* Access lock for changing devices' private data */
+
+  spinlock_t lock;
+
   /* Device specific, lower-half information may follow. */
 };
 

Reply via email to