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 e0a754b0bee56bd8803122f963ca4ebebed438df Author: p-szafonimateusz <[email protected]> AuthorDate: Tue Oct 13 12:15:21 2026 +0200 fs/rpmsgfs: block FIOC_SETLKW instead of polling The client downgraded FIOC_SETLKW to a non-blocking FIOC_SETLK and retried in a 20ms loop. Forward the real FIOC_SETLKW and defer the reply on the server: a request that cannot be taken is parked on a wait queue and answered once the lock is granted, so only the calling thread blocks. Waiters are retried on any lock release the server observes. Signed-off-by: p-szafonimateusz <[email protected]> Signed-off-by: jingfei <[email protected]> --- fs/rpmsgfs/rpmsgfs_client.c | 46 ++++++-------- fs/rpmsgfs/rpmsgfs_server.c | 149 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 27 deletions(-) diff --git a/fs/rpmsgfs/rpmsgfs_client.c b/fs/rpmsgfs/rpmsgfs_client.c index 01b181df611..87d22a1bb68 100644 --- a/fs/rpmsgfs/rpmsgfs_client.c +++ b/fs/rpmsgfs/rpmsgfs_client.c @@ -668,7 +668,6 @@ int rpmsgfs_client_ioctl(FAR void *handle, int fd, FAR struct rpmsgfs_ioctl_s *msg; uint32_t space; size_t len; - int ret; if (arglen < 0) { @@ -677,38 +676,31 @@ int rpmsgfs_client_ioctl(FAR void *handle, int fd, len = sizeof(*msg) + arglen; - while (1) + msg = rpmsgfs_get_tx_payload_buffer(priv, &space); + if (msg == NULL) { - msg = rpmsgfs_get_tx_payload_buffer(priv, &space); - if (msg == NULL) - { - return -ENOMEM; - } - - DEBUGASSERT(len <= space); - - msg->fd = fd; - msg->request = request == FIOC_SETLKW ? FIOC_SETLK : request; - msg->arg = arg; - msg->arglen = arglen; + return -ENOMEM; + } - if (arglen > 0) - { - memcpy(msg->buf, (FAR void *)(uintptr_t)arg, arglen); - } + DEBUGASSERT(len <= space); - ret = rpmsgfs_send_recv(handle, RPMSGFS_IOCTL, false, - (FAR struct rpmsgfs_header_s *)msg, len, - arglen > 0 ? (FAR void *)arg : NULL); - if (request != FIOC_SETLKW || ret != -EAGAIN) - { - break; - } + msg->fd = fd; + msg->request = request; + msg->arg = arg; + msg->arglen = arglen; - usleep(20000); + if (arglen > 0) + { + memcpy(msg->buf, (FAR void *)(uintptr_t)arg, arglen); } - return ret; + /* FIOC_SETLKW blocks here: the server defers its reply until the lock is + * granted, so only this caller waits (rpmsg_wait is uninterruptible). + */ + + return rpmsgfs_send_recv(handle, RPMSGFS_IOCTL, false, + (FAR struct rpmsgfs_header_s *)msg, len, + arglen > 0 ? (FAR void *)arg : NULL); } int rpmsgfs_client_sync(FAR void *handle, int fd) diff --git a/fs/rpmsgfs/rpmsgfs_server.c b/fs/rpmsgfs/rpmsgfs_server.c index b63287e8f79..a420cb40497 100644 --- a/fs/rpmsgfs/rpmsgfs_server.c +++ b/fs/rpmsgfs/rpmsgfs_server.c @@ -35,12 +35,15 @@ #include <errno.h> #include <nuttx/kmalloc.h> +#include <nuttx/list.h> #include <nuttx/mutex.h> +#include <nuttx/semaphore.h> #include <nuttx/fs/fs.h> #include <nuttx/rpmsg/rpmsg.h> #include "rpmsgfs.h" #include "fs_heap.h" +#include "vfs/vfs.h" /**************************************************************************** * Private Types @@ -54,8 +57,28 @@ struct rpmsgfs_server_s int file_rows; int dir_nums; mutex_t lock; +#if CONFIG_FS_LOCK_BUCKET_SIZE > 0 + struct list_node lock_waiters; /* Deferred FIOC_SETLKW requests */ + sem_t lock_drained; /* Posted when lock_waiters drains */ + bool lock_closing; /* ept_release waits on lock_drained */ +#endif }; +#if CONFIG_FS_LOCK_BUCKET_SIZE > 0 +/* One blocked FIOC_SETLKW: holds a copy of the request so its reply can be + * sent once file_setlk_async() grants the lock. + */ + +struct rpmsgfs_lockctx_s +{ + struct list_node node; + struct file_lock_waiter_s waiter; + FAR struct rpmsg_endpoint *ept; + size_t len; + char msg[1]; +}; +#endif + /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -149,6 +172,9 @@ static void rpmsgfs_ns_bind(FAR struct rpmsg_device *rdev, static int rpmsgfs_server_ept_cb(FAR struct rpmsg_endpoint *ept, FAR void *data, size_t len, uint32_t src, FAR void *priv); +#if CONFIG_FS_LOCK_BUCKET_SIZE > 0 +static void rpmsgfs_lock_done(FAR void *arg, int result); +#endif /**************************************************************************** * Private Data @@ -547,6 +573,80 @@ static int rpmsgfs_lseek_handler(FAR struct rpmsg_endpoint *ept, return rpmsg_send(ept, msg, sizeof(*msg)); } +#if CONFIG_FS_LOCK_BUCKET_SIZE > 0 +/* file_setlk_async() grant callback: reply to the client that has been + * blocked in FIOC_SETLKW. Runs on whichever thread released the lock, and + * ctx stays on lock_waiters until the reply is sent so that ept_release + * cannot free the endpoint underneath it. + */ + +static void rpmsgfs_lock_done(FAR void *arg, int result) +{ + FAR struct rpmsgfs_lockctx_s *ctx = arg; + FAR struct rpmsgfs_server_s *server = ctx->ept->priv; + FAR struct rpmsgfs_ioctl_s *msg = (FAR struct rpmsgfs_ioctl_s *)ctx->msg; + + msg->header.result = result; + rpmsg_send(ctx->ept, msg, ctx->len); + + nxmutex_lock(&server->lock); + list_delete(&ctx->node); + if (server->lock_closing && list_is_empty(&server->lock_waiters)) + { + nxsem_post(&server->lock_drained); + } + + nxmutex_unlock(&server->lock); + fs_heap_free(ctx); +} + +/* Wait for the lock without blocking the rx thread: file_setlk_async() + * grants it later and rpmsgfs_lock_done() sends the deferred reply. + */ + +static int rpmsgfs_lock_handler(FAR struct rpmsg_endpoint *ept, + FAR struct rpmsgfs_server_s *server, + FAR struct file *filep, + FAR void *data, size_t len) +{ + FAR struct rpmsgfs_ioctl_s *msg = data; + FAR struct rpmsgfs_lockctx_s *ctx; + int ret; + + ctx = fs_heap_malloc(sizeof(*ctx) + len - 1); + if (ctx == NULL) + { + ret = -ENOMEM; + goto reply; + } + + ctx->waiter.cb = rpmsgfs_lock_done; + ctx->waiter.arg = ctx; + ctx->ept = ept; + ctx->len = len; + memcpy(ctx->msg, data, len); + + nxmutex_lock(&server->lock); + list_add_tail(&server->lock_waiters, &ctx->node); + nxmutex_unlock(&server->lock); + + ret = file_setlk_async(filep, (FAR struct flock *)msg->buf, &ctx->waiter); + if (ret == -EINPROGRESS) + { + return 0; + } + + nxmutex_lock(&server->lock); + list_delete(&ctx->node); + nxmutex_unlock(&server->lock); + fs_heap_free(ctx); + +reply: + msg->header.result = ret; + return rpmsg_send(ept, msg, len); +} +#endif + static int rpmsgfs_ioctl_handler(FAR struct rpmsg_endpoint *ept, FAR void *data, size_t len, uint32_t src, FAR void *priv) @@ -556,6 +656,13 @@ static int rpmsgfs_ioctl_handler(FAR struct rpmsg_endpoint *ept, int ret = -ENOENT; filep = rpmsgfs_get_file(priv, msg->fd); +#if CONFIG_FS_LOCK_BUCKET_SIZE > 0 + if (filep != NULL && msg->request == FIOC_SETLKW) + { + return rpmsgfs_lock_handler(ept, priv, filep, data, len); + } +#endif + if (filep != NULL) { ret = file_ioctl(filep, msg->request, msg->arglen > 0 ? @@ -1028,9 +1135,44 @@ static bool rpmsgfs_ns_match(FAR struct rpmsg_device *rdev, static void rpmsgfs_ept_release(FAR struct rpmsg_endpoint *ept) { FAR struct rpmsgfs_server_s *priv = ept->priv; +#if CONFIG_FS_LOCK_BUCKET_SIZE > 0 + FAR struct rpmsgfs_lockctx_s *ctx; + FAR struct rpmsgfs_lockctx_s *tmp; + bool drain; +#endif int i; int j; +#if CONFIG_FS_LOCK_BUCKET_SIZE > 0 + /* The client is gone; cancel any deferred FIOC_SETLKW and drop its + * context without replying. Cancel before closing the backing files so + * the lock bucket still resolves the path. A request that can no longer + * be cancelled was already granted, so wait for its callback to finish. + */ + + nxmutex_lock(&priv->lock); + priv->lock_closing = true; + list_for_every_entry_safe(&priv->lock_waiters, ctx, tmp, + struct rpmsgfs_lockctx_s, node) + { + if (file_setlk_cancel(&ctx->waiter) == OK) + { + list_delete(&ctx->node); + fs_heap_free(ctx); + } + } + + drain = !list_is_empty(&priv->lock_waiters); + nxmutex_unlock(&priv->lock); + + if (drain) + { + nxsem_wait_uninterruptible(&priv->lock_drained); + } + + nxsem_destroy(&priv->lock_drained); +#endif + for (i = 0; i < priv->file_rows; i++) { for (j = 0; j < CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; j++) @@ -1075,12 +1217,19 @@ static void rpmsgfs_ns_bind(FAR struct rpmsg_device *rdev, priv->ept.priv = priv; priv->ept.release_cb = rpmsgfs_ept_release; nxmutex_init(&priv->lock); +#if CONFIG_FS_LOCK_BUCKET_SIZE > 0 + list_initialize(&priv->lock_waiters); + nxsem_init(&priv->lock_drained, 0, 0); +#endif ret = rpmsg_create_ept(&priv->ept, rdev, name, RPMSG_ADDR_ANY, dest, rpmsgfs_server_ept_cb, rpmsg_destroy_ept); if (ret) { +#if CONFIG_FS_LOCK_BUCKET_SIZE > 0 + nxsem_destroy(&priv->lock_drained); +#endif nxmutex_destroy(&priv->lock); fs_heap_free(priv); }
