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 8e5cb3ce352a1eeb11766826d4d0cb6f6d79d77f Author: zhangshuai39 <[email protected]> AuthorDate: Thu Feb 26 20:51:51 2026 +0800 fs/epoll: use a recursive lock for the epoll head If a thread is killed by a signal while holding eph->lock, the TLS cleanup runs epoll_do_close() on the same thread, which takes the lock again and deadlocks. Make eph->lock a recursive mutex. Signed-off-by: zhangshuai39 <[email protected]> Signed-off-by: Bowen Wang <[email protected]> --- fs/vfs/fs_epoll.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/fs/vfs/fs_epoll.c b/fs/vfs/fs_epoll.c index 06768f5933f..3211fb09c00 100644 --- a/fs/vfs/fs_epoll.c +++ b/fs/vfs/fs_epoll.c @@ -77,7 +77,7 @@ struct epoll_head_s { int size; int crefs; - mutex_t lock; + rmutex_t lock; sem_t sem; struct list_node setup; /* The setup list, store all the setuped * epoll node. @@ -197,14 +197,14 @@ static int epoll_do_open(FAR struct file *filep) FAR epoll_head_t *eph = filep->f_priv; int ret; - ret = nxmutex_lock(&eph->lock); + ret = nxrmutex_lock(&eph->lock); if (ret < 0) { return ret; } eph->crefs++; - nxmutex_unlock(&eph->lock); + nxrmutex_unlock(&eph->lock); return ret; } @@ -215,7 +215,7 @@ static int epoll_do_close(FAR struct file *filep) FAR epoll_node_t *tmp; int ret; - ret = nxmutex_lock(&eph->lock); + ret = nxrmutex_lock(&eph->lock); if (ret < 0) { return ret; @@ -245,14 +245,14 @@ static int epoll_do_close(FAR struct file *filep) fs_heap_free(epn); } - nxmutex_unlock(&eph->lock); - nxmutex_destroy(&eph->lock); + nxrmutex_unlock(&eph->lock); + nxrmutex_destroy(&eph->lock); nxsem_destroy(&eph->sem); fs_heap_free(eph); } else { - nxmutex_unlock(&eph->lock); + nxrmutex_unlock(&eph->lock); } return ret; @@ -324,7 +324,7 @@ static int epoll_do_poll(FAR struct file *filep, spin_unlock_irqrestore(&eph->poll_lock, flags); - nxmutex_lock(&eph->lock); + nxrmutex_lock(&eph->lock); list_for_every_entry(&eph->setup, epn, epoll_node_t, node) { @@ -341,7 +341,7 @@ static int epoll_do_poll(FAR struct file *filep, } } - nxmutex_unlock(&eph->lock); + nxrmutex_unlock(&eph->lock); epoll_notify(eph, eventset); } @@ -398,7 +398,7 @@ static int epoll_do_create(int size, int flags) } eph->size = size; - nxmutex_init(&eph->lock); + nxrmutex_init(&eph->lock); nxsem_init(&eph->sem, 0, 0); spin_lock_init(&eph->poll_lock); @@ -423,7 +423,7 @@ static int epoll_do_create(int size, int flags) fd = file_allocate_from_inode(&g_epoll_inode, flags, 0, eph, 0); if (fd < 0) { - nxmutex_destroy(&eph->lock); + nxrmutex_destroy(&eph->lock); nxsem_destroy(&eph->sem); fs_heap_free(eph); set_errno(-fd); @@ -453,7 +453,7 @@ static int epoll_setup(FAR epoll_head_t *eph) FAR epoll_node_t *epn; int ret; - ret = nxmutex_lock(&eph->lock); + ret = nxrmutex_lock(&eph->lock); if (ret < 0) { return ret; @@ -479,7 +479,7 @@ static int epoll_setup(FAR epoll_head_t *eph) list_add_tail(&eph->setup, &epn->node); } - nxmutex_unlock(&eph->lock); + nxrmutex_unlock(&eph->lock); return ret; } @@ -508,7 +508,7 @@ static int epoll_teardown(FAR epoll_head_t *eph, FAR struct epoll_event *evs, FAR epoll_node_t *epn; int i = 0; - nxmutex_lock(&eph->lock); + nxrmutex_lock(&eph->lock); list_for_every_entry_safe(&eph->setup, epn, tepn, epoll_node_t, node) { @@ -543,7 +543,7 @@ static int epoll_teardown(FAR epoll_head_t *eph, FAR struct epoll_event *evs, } } - nxmutex_unlock(&eph->lock); + nxrmutex_unlock(&eph->lock); return i; } @@ -697,7 +697,7 @@ int epoll_ctl(int epfd, int op, int fd, FAR struct epoll_event *ev) return ERROR; } - ret = nxmutex_lock(&eph->lock); + ret = nxrmutex_lock(&eph->lock); if (ret < 0) { goto err_without_lock; @@ -913,11 +913,11 @@ int epoll_ctl(int epfd, int op, int fd, FAR struct epoll_event *ev) } out: - nxmutex_unlock(&eph->lock); + nxrmutex_unlock(&eph->lock); epoll_filep_close(filep); return OK; err: - nxmutex_unlock(&eph->lock); + nxrmutex_unlock(&eph->lock); err_without_lock: epoll_filep_close(filep); set_errno(-ret);
