locks: Add lm_would_deadlock callback to prevent NFSD hangs
Subsystems (6)
Chuck Lever
maintainerRFC PATCH 1/1 locks: Add lm_would_deadlock callback to prevent NFSD hangs
From: Chuck Lever <[email protected]>
When multiple pNFS layout conflicts occur on an NFS server, the NFSD thread pool can become exhausted while threads are waiting in __break_lease for clients to return their layouts. If all NFSD threads are blocked, none are available to process incoming LAYOUTRETURNs, creating a deadlock.
The approach proposed here, although somewhat expedient, avoids fencing responsive clients.
--- Documentation/filesystems/locking.rst | 2 ++ fs/locks.c | 12 ++++++++++ fs/nfsd/nfs4layouts.c | 33 +++++++++++++++++++++++++++ include/linux/filelock.h | 1 + 4 files changed, 48 insertions(+)
This is 100% untested and falls squarely in the "crazy ideas" category. I'm posting to provide an alternative and encourage some creative thinking about this sticky problem.
Documentation/filesystems/locking.rst +2 -0
@@ -403,6 +403,7 @@ prototypes:: bool (*lm_breaker_owns_lease)(struct file_lock *); bool (*lm_lock_expirable)(struct file_lock *); void (*lm_expire_lock)(void); + bool (*lm_would_deadlock)(struct file_lock *); locking rules:
@@ -416,6 +417,7 @@ lm_change yes no no lm_breaker_owns_lease: yes no no lm_lock_expirable yes no no lm_expire_lock no no yes +lm_would_deadlock yes no no ====================== ============= ================= ========= buffer_head
fs/locks.c +12 -0
@@ -1615,6 +1615,18 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)percpu_up_read (&file_rwsem);locks_dispose_list (&dispose); + +/* Check if lease manager predicts a deadlock situation */ +if (fl->fl_lmops && fl->fl_lmops->lm_would_deadlock && + fl->fl_lmops->lm_would_deadlock (fl)) { +trace_break_lease_noblock (inode, new_fl); + error = -EWOULDBLOCK ; +percpu_down_read (&file_rwsem); +spin_lock (&ctx->flc_lock); +__locks_delete_block (&new_fl->c); + goto out; + } + error =wait_event_interruptible_timeout (new_fl->c.flc_wait,list_empty (&new_fl->c.flc_blocked_member), break_time);
fs/nfsd/nfs4layouts.c +33 -0
@@ -764,9 +764,42 @@ nfsd4_layout_lm_change(struct file_lease *onlist, int arg,return lease_modify (onlist, arg, dispose); } +static bool +nfsd4_layout_lm_would_deadlock (struct file_lease *fl) +{ +struct svc_rqst *rqstp; +struct svc_pool *pool; +struct llist_node *idle; + +/* +* Check if we're running in an NFSD thread context. +* If not, we can't cause an NFSD deadlock. +*/ + rqstp =nfsd_current_rqst (); +if (!rqstp) +return false; + + pool = rqstp->rq_pool; + +/* +* Check the number of idle threads in the pool. We use +* READ_ONCE as sp_idle_threads is a lockless list. +* If we have 0 or 1 idle threads remaining and the current +* thread is about to block, we risk deadlock as there may +* not be enough threads available to process the LAYOUTRETURN +* RPCs needed to unblock. +*/ + idle =READ_ONCE (pool->sp_idle_threads.first); +if (!idle || !READ_ONCE (idle->next)) +return true; + +return false; +} +static const struct lease_manager_operations nfsd4_layouts_lm_ops = { .lm_break = nfsd4_layout_lm_break, .lm_change = nfsd4_layout_lm_change, + .lm_would_deadlock = nfsd4_layout_lm_would_deadlock, };int
include/linux/filelock.h +1 -0
@@ -49,6 +49,7 @@ struct lease_manager_operations {int (*lm_change)(struct file_lease *,int ,struct list_head *);void (*lm_setup)(struct file_lease *,void **);bool (*lm_breaker_owns_lease)(struct file_lease *); +bool (*lm_would_deadlock)(struct file_lease *); };struct lock_manager {
Signature · 1 line
2.51.0
Jeff Layton
maintainerQuoted 91 lines · Chuck Lever
From: Chuck Lever <[email protected]>
When multiple pNFS layout conflicts occur on an NFS server, the NFSD thread pool can become exhausted while threads are waiting in
__break_leasefor clients to return their layouts. If all NFSD threads are blocked, none are available to process incoming LAYOUTRETURNs, creating a deadlock.The approach proposed here, although somewhat expedient, avoids fencing responsive clients.
Signed-off-by Chuck Lever <[email protected]>--- Documentation/filesystems/locking.rst | 2 ++ fs/locks.c | 12 ++++++++++ fs/nfsd/nfs4layouts.c | 33 +++++++++++++++++++++++++++ include/linux/filelock.h | 1 + 4 files changed, 48 insertions(+)This is 100% untested and falls squarely in the "crazy ideas" category. I'm posting to provide an alternative and encourage some creative thinking about this sticky problem.
Documentation/filesystems/locking.rst +2
-0@@ -403,6 +403,7 @@ prototypes:: bool (*lm_breaker_owns_lease)(struct file_lock *); bool (*lm_lock_expirable)(struct file_lock *); void (*lm_expire_lock)(void); + bool (*lm_would_deadlock)(struct file_lock *); locking rules:@@ -416,6 +417,7 @@ lm_change yes no no lm_breaker_owns_lease: yes no no lm_lock_expirable yes no no lm_expire_lock no no yes +lm_would_deadlock yes no no ====================== ============= ================= ========= buffer_headfs/locks.c +12
-0@@ -1615,6 +1615,18 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)percpu_up_read (&file_rwsem);locks_dispose_list (&dispose); + +/* Check if lease manager predicts a deadlock situation */ +if (fl->fl_lmops && fl->fl_lmops->lm_would_deadlock && + fl->fl_lmops->lm_would_deadlock (fl)) { +trace_break_lease_noblock (inode, new_fl); + error = -EWOULDBLOCK ; +percpu_down_read (&file_rwsem); +spin_lock (&ctx->flc_lock); +__locks_delete_block (&new_fl->c); + goto out; + } + error =wait_event_interruptible_timeout (new_fl->c.flc_wait,list_empty (&new_fl->c.flc_blocked_member), break_time);fs/nfsd/nfs4layouts.c +17
-0@@ -764,9 +764,42 @@ nfsd4_layout_lm_change(struct file_lease *onlist, int arg,return lease_modify (onlist, arg, dispose); } +static bool +nfsd4_layout_lm_would_deadlock (struct file_lease *fl) +{ +struct svc_rqst *rqstp; +struct svc_pool *pool; +struct llist_node *idle; + +/* +* Check if we're running in an NFSD thread context. +* If not, we can't cause an NFSD deadlock. +*/ + rqstp =nfsd_current_rqst (); +if (!rqstp) +return false; + + pool = rqstp->rq_pool; +
fs/nfsd/nfs4layouts.c nfsd4_layout_lm_change(struct file_lease *onlist, int arg, · line 793
Quoted 10 lines
fs/nfsd/nfs4layouts.c +10
-0+/* +* Check the number of idle threads in the pool. We use +* READ_ONCE as sp_idle_threads is a lockless list. +* If we have 0 or 1 idle threads remaining and the current +* thread is about to block, we risk deadlock as there may +* not be enough threads available to process the LAYOUTRETURN +* RPCs needed to unblock. +*/ + idle =READ_ONCE (pool->sp_idle_threads.first); +if (!idle || !READ_ONCE (idle->next))
I think you might need the rcu_read_lock() around the above. I think it's possible for idle to be freed before you can dereference idle->next.
Quoted 24 lines
fs/nfsd/nfs4layouts.c +6
-0+return true; + +return false; +} +static const struct lease_manager_operations nfsd4_layouts_lm_ops = { .lm_break = nfsd4_layout_lm_break, .lm_change = nfsd4_layout_lm_change, + .lm_would_deadlock = nfsd4_layout_lm_would_deadlock, };int include/linux/filelock.h +1
-0@@ -49,6 +49,7 @@ struct lease_manager_operations {int (*lm_change)(struct file_lease *,int ,struct list_head *);void (*lm_setup)(struct file_lease *,void **);bool (*lm_breaker_owns_lease)(struct file_lease *); +bool (*lm_would_deadlock)(struct file_lease *); };struct lock_manager {
Signature · 1 line
Jeff Layton <[email protected]>
Dai Ngo
reviewerQuoted 78 lines · Chuck Lever
From: Chuck Lever <[email protected]>
When multiple pNFS layout conflicts occur on an NFS server, the NFSD thread pool can become exhausted while threads are waiting in
__break_leasefor clients to return their layouts. If all NFSD threads are blocked, none are available to process incoming LAYOUTRETURNs, creating a deadlock.The approach proposed here, although somewhat expedient, avoids fencing responsive clients.
Signed-off-by Chuck Lever <[email protected]>--- Documentation/filesystems/locking.rst | 2 ++ fs/locks.c | 12 ++++++++++ fs/nfsd/nfs4layouts.c | 33 +++++++++++++++++++++++++++ include/linux/filelock.h | 1 + 4 files changed, 48 insertions(+)This is 100% untested and falls squarely in the "crazy ideas" category. I'm posting to provide an alternative and encourage some creative thinking about this sticky problem.
Documentation/filesystems/locking.rst +2
-0@@ -403,6 +403,7 @@ prototypes:: bool (*lm_breaker_owns_lease)(struct file_lock *); bool (*lm_lock_expirable)(struct file_lock *); void (*lm_expire_lock)(void); + bool (*lm_would_deadlock)(struct file_lock *); locking rules:@@ -416,6 +417,7 @@ lm_change yes no no lm_breaker_owns_lease: yes no no lm_lock_expirable yes no no lm_expire_lock no no yes +lm_would_deadlock yes no no ====================== ============= ================= ========= buffer_headfs/locks.c +12
-0@@ -1615,6 +1615,18 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)percpu_up_read (&file_rwsem);locks_dispose_list (&dispose); + +/* Check if lease manager predicts a deadlock situation */ +if (fl->fl_lmops && fl->fl_lmops->lm_would_deadlock && + fl->fl_lmops->lm_would_deadlock (fl)) { +trace_break_lease_noblock (inode, new_fl); + error = -EWOULDBLOCK ; +percpu_down_read (&file_rwsem); +spin_lock (&ctx->flc_lock); +__locks_delete_block (&new_fl->c); + goto out; + } + error =wait_event_interruptible_timeout (new_fl->c.flc_wait,list_empty (&new_fl->c.flc_blocked_member), break_time);fs/nfsd/nfs4layouts.c +4
-0@@ -764,9 +764,42 @@ nfsd4_layout_lm_change(struct file_lease *onlist, int arg,return lease_modify (onlist, arg, dispose); } +static bool +nfsd4_layout_lm_would_deadlock (struct file_lease *fl) +{ +struct svc_rqst *rqstp;
fs/nfsd/nfs4layouts.c nfsd4_layout_lm_change(struct file_lease *onlist, int arg, · line 780
Quoted 10 lines
fs/nfsd/nfs4layouts.c +10
-0+struct svc_pool *pool; +struct llist_node *idle; + +/* +* Check if we're running in an NFSD thread context. +* If not, we can't cause an NFSD deadlock. +*/ + rqstp =nfsd_current_rqst (); +if (!rqstp) +return false;
If this is intended for layout lease only then I think we should check for 4.1 or newer.
-Dai
Quoted 37 lines
fs/nfsd/nfs4layouts.c +19
-0+ + pool = rqstp->rq_pool; + +/* +* Check the number of idle threads in the pool. We use +* READ_ONCE as sp_idle_threads is a lockless list. +* If we have 0 or 1 idle threads remaining and the current +* thread is about to block, we risk deadlock as there may +* not be enough threads available to process the LAYOUTRETURN +* RPCs needed to unblock. +*/ + idle =READ_ONCE (pool->sp_idle_threads.first); +if (!idle || !READ_ONCE (idle->next)) +return true; + +return false; +} +static const struct lease_manager_operations nfsd4_layouts_lm_ops = { .lm_break = nfsd4_layout_lm_break, .lm_change = nfsd4_layout_lm_change, + .lm_would_deadlock = nfsd4_layout_lm_would_deadlock, };int include/linux/filelock.h +1
-0@@ -49,6 +49,7 @@ struct lease_manager_operations {int (*lm_change)(struct file_lease *,int ,struct list_head *);void (*lm_setup)(struct file_lease *,void **);bool (*lm_breaker_owns_lease)(struct file_lease *); +bool (*lm_would_deadlock)(struct file_lease *); };struct lock_manager {