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 8adddb2dac0c4f6369b64a7b7f13a9602246aa95 Author: wangshaoxin <[email protected]> AuthorDate: Wed Jul 29 17:09:49 2026 +0800 !drivers/misc/rpmsgdev: preallocate the per-open state Every open() allocated struct rpmsgdev_priv_s on the client and struct rpmsgdev_device_s on the server, and close() freed them. Embed CONFIG_DEV_RPMSG_OPEN_MAX of them in the device and in the server endpoint and keep them on free/opened lists, so open and close no longer touch the heap. BREAKING CHANGE: a device opened through rpmsgdev can now be open at most CONFIG_DEV_RPMSG_OPEN_MAX times at once; a further open() fails with -ENOMEM. Quick fix: raise CONFIG_DEV_RPMSG_OPEN_MAX. Signed-off-by: wangshaoxin <[email protected]> Signed-off-by: Bowen Wang <[email protected]> --- drivers/misc/Kconfig | 12 ++++++++ drivers/misc/rpmsgdev.c | 59 ++++++++++++++++++++++++++++---------- drivers/misc/rpmsgdev_server.c | 65 +++++++++++++++++++++++++++++------------- 3 files changed, 101 insertions(+), 35 deletions(-) diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index 2a240269b6e..03274d36be8 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -60,6 +60,18 @@ config DEV_RPMSG_SERVER default n depends on RPMSG +config DEV_RPMSG_OPEN_MAX + int "Rpmsg Device max concurrent opens per device" + default 4 + depends on DEV_RPMSG || DEV_RPMSG_SERVER + ---help--- + Maximum number of concurrent open() instances supported per rpmsg + device. The per-open data blocks (client: struct rpmsgdev_priv_s, + server: struct rpmsgdev_device_s) are preallocated as a fixed-size + array inside each device/endpoint instance to avoid dynamic + allocation. open() is rejected with -ENOMEM once all slots are in + use. + if DEV_RPMSG_SERVER config DEV_RPMSG_SERVER_WORK_PRIORITY diff --git a/drivers/misc/rpmsgdev.c b/drivers/misc/rpmsgdev.c index 957bc8c8ee2..3d5a760e380 100644 --- a/drivers/misc/rpmsgdev.c +++ b/drivers/misc/rpmsgdev.c @@ -28,6 +28,7 @@ #include <stdio.h> #include <stdlib.h> +#include <stdint.h> #include <string.h> #include <assert.h> #include <fcntl.h> @@ -64,24 +65,34 @@ * Private Types ****************************************************************************/ +/* The private data slots are preallocated in the device instance and the + * unused ones are kept on the 'free' list. A slot is claimed by removing it + * from that list under the lock, which is atomic with respect to any other + * open(), so no in-use flag is needed. The opened slots are reachable from + * filep->f_priv and therefore do not need to be linked anywhere. + */ + struct rpmsgdev_priv_s { - uint64_t filep; /* store server file pointer */ - bool nonblock; /* true: open with O_NONBLOCK */ - mutex_t lock; /* Mutex used to protect the file operation */ + uint64_t filep; /* store server file pointer */ + bool nonblock; /* true: open with O_NONBLOCK */ + struct list_node node; /* Free list node, unlinked while in use */ + mutex_t lock; /* Mutex used to protect the file operation */ }; struct rpmsgdev_s { - struct rpmsg_endpoint ept; /* Rpmsg endpoint */ - FAR const char *remotecpu; /* The server cpu name */ - FAR const char *remotepath; /* The device path in the server cpu */ - sem_t wait; /* Wait sem, used for preventing any - * operation until the connection - * between two cpu established. - */ - struct list_node cookies; /* Requests waiting for a reply */ - spinlock_t lock; /* Protect the cookie list */ + struct rpmsg_endpoint ept; /* Rpmsg endpoint */ + FAR const char *remotecpu; /* The server cpu name */ + FAR const char *remotepath; /* The device path in the server cpu */ + sem_t wait; /* Wait sem, used for preventing any + * operation until the connection + * between two cpu established. + */ + struct list_node free; /* List head of the free slots */ + struct list_node cookies; /* Requests waiting for a reply */ + spinlock_t lock; /* Protect the free and cookie lists */ + struct rpmsgdev_priv_s priv[CONFIG_DEV_RPMSG_OPEN_MAX]; }; /* Rpmsg device cookie used to handle the response from the remote cpu */ @@ -209,6 +220,7 @@ static int rpmsgdev_open(FAR struct file *filep) FAR struct rpmsgdev_s *dev; FAR struct rpmsgdev_priv_s *priv; struct rpmsgdev_open_s msg; + irqstate_t flags; int ret; /* Get the mountpoint inode reference from the file structure and the @@ -218,7 +230,12 @@ static int rpmsgdev_open(FAR struct file *filep) dev = filep->f_inode->i_private; DEBUGASSERT(dev != NULL); - priv = kmm_zalloc(sizeof(*priv)); + /* Claim a free private data slot by removing it from the free list */ + + flags = spin_lock_irqsave(&dev->lock); + priv = list_remove_head_type(&dev->free, struct rpmsgdev_priv_s, node); + spin_unlock_irqrestore(&dev->lock, flags); + if (priv == NULL) { return -ENOMEM; @@ -235,7 +252,9 @@ static int rpmsgdev_open(FAR struct file *filep) if (ret < 0) { rpmsgdeverr("open failed, ret=%d\n", ret); - kmm_free(priv); + flags = spin_lock_irqsave(&dev->lock); + list_add_tail(&dev->free, &priv->node); + spin_unlock_irqrestore(&dev->lock, flags); return ret; } @@ -268,6 +287,7 @@ static int rpmsgdev_close(FAR struct file *filep) FAR struct rpmsgdev_s *dev; FAR struct rpmsgdev_priv_s *priv; struct rpmsgdev_close_s msg; + irqstate_t flags; int ret; /* Recover our private data from the struct file instance */ @@ -289,7 +309,9 @@ static int rpmsgdev_close(FAR struct file *filep) filep->f_priv = NULL; nxmutex_destroy(&priv->lock); - kmm_free(priv); + flags = spin_lock_irqsave(&dev->lock); + list_add_tail(&dev->free, &priv->node); + spin_unlock_irqrestore(&dev->lock, flags); return ret; } @@ -1286,6 +1308,7 @@ int rpmsgdev_register(FAR const char *remotecpu, FAR const char *remotepath, { FAR struct rpmsgdev_s *dev; int ret; + int i; /* Arguments check */ @@ -1310,7 +1333,13 @@ int rpmsgdev_register(FAR const char *remotecpu, FAR const char *remotepath, nxsem_init(&dev->wait, 0, 0); spin_lock_init(&dev->lock); + + list_initialize(&dev->free); list_initialize(&dev->cookies); + for (i = 0; i < CONFIG_DEV_RPMSG_OPEN_MAX; i++) + { + list_add_tail(&dev->free, &dev->priv[i].node); + } /* Register the rpmsg callback */ diff --git a/drivers/misc/rpmsgdev_server.c b/drivers/misc/rpmsgdev_server.c index 6390d0aafbf..724ad46ba74 100644 --- a/drivers/misc/rpmsgdev_server.c +++ b/drivers/misc/rpmsgdev_server.c @@ -59,12 +59,18 @@ struct rpmsgdev_read_pend_s bool cancel; }; +/* The device slots are preallocated in the endpoint instance and managed by + * two lists: 'free' holds the unused slots and 'head' the opened ones. A + * slot is claimed by removing it from the free list under the lock, which is + * atomic with respect to any other open, so no in-use flag is needed. + */ + struct rpmsgdev_device_s { struct file file; /* The open file */ struct pollfd_s fd; /* The poll fd */ uint64_t cfd; /* The client poll fd pointer */ - struct list_node node; /* The double-linked list node */ + struct list_node node; /* Free or opened list node */ struct work_s work; /* Poll notify work */ struct rpmsgdev_read_pend_s readpend; }; @@ -78,14 +84,14 @@ struct rpmsgdev_export_s struct rpmsgdev_server_s { - struct rpmsg_endpoint ept; /* Rpmsg end point */ - struct list_node head; /* The double-linked list head of opened - * devices - */ - mutex_t lock; /* The mutex used to protect the list - * operation - */ + struct rpmsg_endpoint ept; /* Rpmsg end point */ + struct list_node head; /* List head of the opened devices */ + struct list_node free; /* List head of the free device slots */ + mutex_t lock; /* Protect the device lists below */ FAR struct rpmsgdev_export_s *export; + struct rpmsgdev_device_s dev[CONFIG_DEV_RPMSG_OPEN_MAX]; + + /* Preallocated per-open device slots */ }; /**************************************************************************** @@ -171,27 +177,40 @@ static int rpmsgdev_open_handler(FAR struct rpmsg_endpoint *ept, FAR struct rpmsgdev_open_s *msg = data; FAR struct rpmsgdev_device_s *dev; - dev = kmm_zalloc(sizeof(*dev)); + /* Claim a free device slot by removing it from the free list */ + + nxmutex_lock(&server->lock); + dev = list_remove_head_type(&server->free, struct rpmsgdev_device_s, node); + nxmutex_unlock(&server->lock); + if (dev == NULL) { msg->header.result = -ENOMEM; goto out; } + /* Clear the stale poll/work state left by a previous user of this slot. + * The node is unlinked while the slot is claimed, so clearing it is safe. + */ + + memset(dev, 0, sizeof(*dev)); + msg->header.result = file_open(&dev->file, export ? export->localpath : strchr(ept->name, '/'), msg->flags, 0); + + nxmutex_lock(&server->lock); if (msg->header.result < 0) { - kmm_free(dev); - goto out; + list_add_tail(&server->free, &dev->node); + } + else + { + list_add_tail(&server->head, &dev->node); + msg->filep = (uint64_t)(uintptr_t)&dev->file; } - msg->filep = (uint64_t)(uintptr_t)&dev->file; - - nxmutex_lock(&server->lock); - list_add_tail(&server->head, &dev->node); nxmutex_unlock(&server->lock); out: @@ -245,9 +264,8 @@ static int rpmsgdev_close_handler(FAR struct rpmsg_endpoint *ept, list_delete(&dev->node); work_cancel_sync_wq(g_rpmsgdev_wqueue, &dev->work); work_cancel_sync_wq(g_rpmsgdev_wqueue, &dev->readpend.work); + list_add_tail(&server->free, &dev->node); nxmutex_unlock(&server->lock); - - kmm_free(dev); } return rpmsg_send(ept, msg, sizeof(*msg)); @@ -629,8 +647,8 @@ 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. + /* The slots live in server, freed below: nothing may still point + * at them from the device poll list or the work queue. */ rpmsgdev_read_cancel(dev); @@ -642,7 +660,7 @@ static void rpmsgdev_ept_release(FAR struct rpmsg_endpoint *ept) } list_delete(&dev->node); - kmm_free(dev); + list_add_tail(&server->free, &dev->node); } nxmutex_unlock(&server->lock); @@ -660,6 +678,7 @@ static void rpmsgdev_ns_bind(FAR struct rpmsg_device *rdev, { FAR struct rpmsgdev_server_s *server; int ret; + int i; server = kmm_zalloc(sizeof(*server)); if (server == NULL) @@ -668,6 +687,12 @@ static void rpmsgdev_ns_bind(FAR struct rpmsg_device *rdev, } list_initialize(&server->head); + list_initialize(&server->free); + for (i = 0; i < CONFIG_DEV_RPMSG_OPEN_MAX; i++) + { + list_add_tail(&server->free, &server->dev[i].node); + } + nxmutex_init(&server->lock); server->ept.priv = server; server->export = priv;
