Skip to contentSkip to conversation

locks: Add lm_would_deadlock callback to prevent NFSD hangs

RFC PATCH In review Chuck Lever 3 participants 3 messages Nov 20, 2025 – Nov 21, 2025

Subsystems (6)
lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

RFC 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
index 77704fde9845..6b0cb5fd03fd 100644
@@ -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
index 04a3f0e20724..4ea473c885a8 100644
@@ -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
index 683bd1130afe..748a1b1b0626 100644
@@ -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
index c2ce8ba05d06..7c46444a3d50 100644
@@ -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
maintainer
#1 lore raw
Quoted 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_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
index 77704fde9845..6b0cb5fd03fd 100644
@@ -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
index 04a3f0e20724..4ea473c885a8 100644
@@ -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
index 683bd1130afe..748a1b1b0626 100644
@@ -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
index c2ce8ba05d06..7c46444a3d50 100644
@@ -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]>
reviewer
#2 lore raw
In reply to Chuck Lever
Quoted 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_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
index 77704fde9845..6b0cb5fd03fd 100644
@@ -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
index 04a3f0e20724..4ea473c885a8 100644
@@ -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
index 683bd1130afe..748a1b1b0626 100644
@@ -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
index c2ce8ba05d06..7c46444a3d50 100644
@@ -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 {