kernfs: fix deadlock in epoll_ctl by waking pollfree
Subsystems (2)
PATCH v2 1/1 kernfs: fix deadlock in epoll_ctl by waking pollfree
Syzbot reported a task hung in do_epoll_ctl_file(). This is caused by an AB-BA lock inversion between epoll's ep->mtx and kernfs node teardown.
Thread A (epoll_ctl) acquires ep->mtx, then attempts to acquire the kernfs active reference during vfs_poll().
Thread B (kernfs_remove) deactivates the node and attempts to unhook active epoll monitors, which requires ep->mtx.
This patch fixes the deadlock by utilizing the existing epoll POLLFREE path. By calling wake_up_pollfree(&on->poll) before the waitqueue is freed, epoll users are notified to detach their wait entries without requiring kernfs teardown to acquire ep->mtx.
--- fs/kernfs/file.c | 8 ++++++++ 1 file changed, 8 insertions(+)
fs/kernfs/file.c +8 -0
@@ -600,6 +600,14 @@ static void kernfs_unlink_open_file(struct kernfs_node *kn, }if (list_empty (&on->files)) { +/* +* @on->poll is embedded in @on and is about to be freed. Tell +* epoll users to detach their wait entries through the POLLFREE +* callback path before the waitqueue disappears. This path is +* serialized by the waitqueue lock and doesn't require taking +* eventpoll's ep->mtx from kernfs teardown. +*/ +wake_up_pollfree (&on->poll);rcu_assign_pointer (kn->attr.open,NULL );kfree_rcu (on, rcu_head); }
Signature · 1 line
2.34.1
Greg KH
maintainerQuoted 13 lines · Sainath Manda
Syzbot reported a task hung in
do_epoll_ctl_file(). This is caused by an AB-BA lock inversion between epoll'sep->mtxand kernfs node teardown.Thread A (
epoll_ctl) acquiresep->mtx, then attempts to acquire the kernfs active reference duringvfs_poll().
Thread B (kernfs_remove) deactivates the node and attempts to unhook active epoll monitors, which requiresep->mtx.This patch fixes the deadlock by utilizing the existing epoll POLLFREE path. By calling
wake_up_pollfree(&on->poll)before the waitqueue is freed, epoll users are notified to detach their wait entries without requiring kernfs teardown to acquireep->mtx.
Quoted 5 lines
Reported-by [email protected]Signed-off-by Sainath Manda <[email protected]>--- fs/kernfs/file.c | 8 ++++++++ 1 file changed, 8 insertions(+)
What commit id does this fix? And why isn't it for stable kernels too?
thanks,
greg k-h