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

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

commit 2f72baa1a3f8855a14b5edc1e08802734eca3795
Author: Bowen Wang <[email protected]>
AuthorDate: Mon Nov 23 03:06:23 2026 +0800

    drivers/misc/rpmsgdev: drop pending blocking reads when a side goes away
    
    A blocking read leaves the server polling the device with
    dev->readpend.fd, and the client waiting for the reply on a cookie.
    When the client goes away, rpmsgdev_ept_release() tore down only the
    client's own poll (dev->fd) before closing the file and freeing the
    server, so the device kept calling back into freed memory and the read
    work could still run. When the server goes away, nothing completed the
    client's cookie, so the blocking read never returned.
    
    Peer stop during a pending read, without this change (sim, KASAN):
    
      [   12.200372] [server] kasan_report: kasan detected a read access error, 
address at 0xea2813c4,size is 4, return address: 0x40037e1d
      [   12.200743] [server] dump_assert_info: Assertion failed panic: at 
file: :0 task: rpmsgdev_server process: Kernel 0x4003805a
    
      0x40037e1d: work_dispatch at sched/wqueue/kwork_thread.c:189
    
    qemu-armv8a:
    
      [    8.483161] [ 1] [ ALERT] [server] dump_assert_info: Assertion failed 
panic: at file: /arch/arm64/src/common/arm64_fatal.c:696 task: rpmsgdev_server 
process: Kernel 0x402bb024
      [    8.489488] [ 1] [ ALERT] [server] up_dump_register: ELR:       
0x402db3c0
    
      0x402db3c0: rpmsgdev_read_work at drivers/misc/rpmsgdev_server.c:271
    
    Tear down the pending read poll and cancel its work before the slot is
    reused or freed, in rpmsgdev_ept_release() and rpmsgdev_close_handler().
    On the client, keep the requests waiting for a reply on a list and fail
    them with -ENOTCONN when the server endpoint is unbound or the rpmsg
    device is destroyed.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/misc/rpmsgdev.c        | 64 +++++++++++++++++++++++++++++++++++++++---
 drivers/misc/rpmsgdev_server.c | 28 ++++++++++++++++++
 2 files changed, 88 insertions(+), 4 deletions(-)

diff --git a/drivers/misc/rpmsgdev.c b/drivers/misc/rpmsgdev.c
index 2d04c9bc0b8..cdc351689c8 100644
--- a/drivers/misc/rpmsgdev.c
+++ b/drivers/misc/rpmsgdev.c
@@ -36,7 +36,9 @@
 #include <nuttx/debug.h>
 #include <net/if.h>
 
+#include <nuttx/list.h>
 #include <nuttx/kmalloc.h>
+#include <nuttx/spinlock.h>
 #include <nuttx/fs/fs.h>
 #include <nuttx/fs/ioctl.h>
 #include <nuttx/video/fb.h>
@@ -77,15 +79,18 @@ struct rpmsgdev_s
                                       * operation until the connection
                                       * between two cpu established.
                                       */
+  struct list_node      cookies;     /* Requests waiting for a reply */
+  spinlock_t            lock;        /* Protect the cookie list */
 };
 
 /* Rpmsg device cookie used to handle the response from the remote cpu */
 
 struct rpmsgdev_cookie_s
 {
-  sem_t     sem;     /* Semaphore used for rpmsg */
-  int       result;  /* The return value of the remote call */
-  FAR void *data;    /* The return data buffer of the remote call */
+  struct list_node node;    /* Node in rpmsgdev_s::cookies */
+  sem_t            sem;     /* Semaphore used for rpmsg */
+  int              result;  /* The return value of the remote call */
+  FAR void        *data;    /* The return data buffer of the remote call */
 };
 
 /****************************************************************************
@@ -144,6 +149,7 @@ static int     rpmsgdev_ept_cb(FAR struct rpmsg_endpoint 
*ept,
                                FAR void *data, size_t len, uint32_t src,
                                FAR void *priv);
 static void    rpmsgdev_ns_bound(struct rpmsg_endpoint *ept);
+static void    rpmsgdev_ns_unbind(FAR struct rpmsg_endpoint *ept);
 
 /****************************************************************************
  * Private Data
@@ -795,6 +801,7 @@ static int rpmsgdev_send_recv(FAR struct rpmsgdev_s *priv,
                               int len, FAR void *data)
 {
   struct rpmsgdev_cookie_s cookie;
+  irqstate_t flags;
   int ret;
 
   memset(&cookie, 0, sizeof(cookie));
@@ -813,6 +820,10 @@ static int rpmsgdev_send_recv(FAR struct rpmsgdev_s *priv,
   msg->result  = -ENXIO;
   msg->cookie  = (uintptr_t)&cookie;
 
+  flags = spin_lock_irqsave(&priv->lock);
+  list_add_tail(&priv->cookies, &cookie.node);
+  spin_unlock_irqrestore(&priv->lock, flags);
+
   if (copy)
     {
       ret = rpmsg_send(&priv->ept, msg, len);
@@ -839,6 +850,9 @@ static int rpmsgdev_send_recv(FAR struct rpmsgdev_s *priv,
     }
 
 fail:
+  flags = spin_lock_irqsave(&priv->lock);
+  list_delete(&cookie.node);
+  spin_unlock_irqrestore(&priv->lock, flags);
   nxsem_destroy(&cookie.sem);
   return ret;
 }
@@ -1010,6 +1024,45 @@ static void rpmsgdev_ns_bound(FAR struct rpmsg_endpoint 
*ept)
   rpmsg_post(&priv->ept, &priv->wait);
 }
 
+/****************************************************************************
+ * Name: rpmsgdev_fail_cookies
+ *
+ * Description:
+ *   Complete every request still waiting for a reply with -ENOTCONN.  The
+ *   server is gone and no reply will come, so without this a blocking
+ *   read would never return.
+ *
+ ****************************************************************************/
+
+static void rpmsgdev_fail_cookies(FAR struct rpmsgdev_s *priv)
+{
+  FAR struct rpmsgdev_cookie_s *cookie;
+  irqstate_t flags;
+
+  /* rpmsg_post() may switch to a waiter, which then takes priv->lock */
+
+  flags = spin_lock_irqsave_nopreempt(&priv->lock);
+  list_for_every_entry(&priv->cookies, cookie, struct rpmsgdev_cookie_s,
+                       node)
+    {
+      cookie->result = -ENOTCONN;
+      rpmsg_post(&priv->ept, &cookie->sem);
+    }
+
+  spin_unlock_irqrestore_nopreempt(&priv->lock, flags);
+}
+
+/****************************************************************************
+ * Name: rpmsgdev_ns_unbind
+ ****************************************************************************/
+
+static void rpmsgdev_ns_unbind(FAR struct rpmsg_endpoint *ept)
+{
+  FAR struct rpmsgdev_s *priv = ept->priv;
+
+  rpmsgdev_fail_cookies(priv);
+}
+
 /****************************************************************************
  * Name: rpmsgdev_device_created
  *
@@ -1040,7 +1093,7 @@ static void rpmsgdev_device_created(FAR struct 
rpmsg_device *rdev,
                priv->remotepath);
       rpmsg_create_ept(&priv->ept, rdev, buf,
                        RPMSG_ADDR_ANY, RPMSG_ADDR_ANY,
-                       rpmsgdev_ept_cb, NULL);
+                       rpmsgdev_ept_cb, rpmsgdev_ns_unbind);
     }
 }
 
@@ -1067,6 +1120,7 @@ static void rpmsgdev_device_destroy(FAR struct 
rpmsg_device *rdev,
 
   if (strcmp(priv->remotecpu, rpmsg_get_cpuname(rdev)) == 0)
     {
+      rpmsgdev_fail_cookies(priv);
       rpmsg_wait(&priv->ept, &priv->wait);
       rpmsg_destroy_ept(&priv->ept);
     }
@@ -1157,6 +1211,8 @@ int rpmsgdev_register(FAR const char *remotecpu, FAR 
const char *remotepath,
   dev->remotepath = remotepath;
 
   nxsem_init(&dev->wait, 0, 0);
+  spin_lock_init(&dev->lock);
+  list_initialize(&dev->cookies);
 
   /* Register the rpmsg callback */
 
diff --git a/drivers/misc/rpmsgdev_server.c b/drivers/misc/rpmsgdev_server.c
index 363de8e890e..9e463573596 100644
--- a/drivers/misc/rpmsgdev_server.c
+++ b/drivers/misc/rpmsgdev_server.c
@@ -98,6 +98,7 @@ static int  rpmsgdev_open_handler(FAR struct rpmsg_endpoint 
*ept,
 static int  rpmsgdev_close_handler(FAR struct rpmsg_endpoint *ept,
                                    FAR void *data, size_t len,
                                    uint32_t src, FAR void *priv);
+static void rpmsgdev_read_cancel(FAR struct rpmsgdev_device_s *dev);
 static int  rpmsgdev_read_nonblock_handler(FAR struct rpmsg_endpoint *ept,
                                            FAR void *data, size_t len,
                                            uint32_t src, FAR void *priv);
@@ -195,6 +196,25 @@ out:
   return rpmsg_send(ept, msg, sizeof(*msg));
 }
 
+/****************************************************************************
+ * Name: rpmsgdev_read_cancel
+ *
+ * Description:
+ *   Drop a blocking read that is still waiting for data, so that the
+ *   device no longer calls back into the slot.
+ *
+ ****************************************************************************/
+
+static void rpmsgdev_read_cancel(FAR struct rpmsgdev_device_s *dev)
+{
+  if (dev->readpend.fd.priv != NULL)
+    {
+      file_poll(&dev->file, &dev->readpend.fd, false);
+    }
+
+  work_cancel_sync_wq(g_rpmsgdev_wqueue, &dev->readpend.work);
+}
+
 /****************************************************************************
  * Name: rpmsgdev_close_handler
  ****************************************************************************/
@@ -208,6 +228,7 @@ static int rpmsgdev_close_handler(FAR struct rpmsg_endpoint 
*ept,
   FAR struct rpmsgdev_device_s *dev =
     (FAR struct rpmsgdev_device_s *)(uintptr_t)msg->filep;
 
+  rpmsgdev_read_cancel(dev);
   msg->header.result = file_close(&dev->file);
   if (msg->header.result == 0)
     {
@@ -541,6 +562,13 @@ static void rpmsgdev_ept_release(FAR struct rpmsg_endpoint 
*ept)
           file_poll(&dev->file, &dev->fd, false);
         }
 
+      /* dev is freed below: nothing may still point at it from the
+       * device poll list or the work queue.
+       */
+
+      rpmsgdev_read_cancel(dev);
+      work_cancel_sync_wq(g_rpmsgdev_wqueue, &dev->work);
+
       if (dev->file.f_inode != NULL)
         {
           file_close(&dev->file);

Reply via email to