Skip to contentSkip to conversation

NFSv4: fix blocking lock stalls under a delegation

PATCH New Olga Kornievskaia 1 participant 3 messages Nov 7

Subsystems (2)
lore
Share & tools
lore

Olga Kornievskaia

author reviewer
#0 lore raw

cover NFSv4: fix blocking lock stalls under a delegation

A test on a single client runs two processes that repeatedly acquire and release a POSIX lock on the same file on an NFSv4.1 mount, so that they compete for it, and measures how long each acquisition takes. When the open is granted a delegation, some acquisitions take unexpectedly long - almost exactly 1, 3 or 7 seconds - although the holder releases the lock within microseconds.

Over 20s and 2.4M acquisitions the slowest five were 3119404us, 3119248us, 1040679us, 1040288us and 1040212us. When the server grants no delegation, nothing exceeds 12ms.

The delegation case breaks the notification of waiters on the unlock path, in two independent places:

- NFS_STATE_MAY_NOTIFY_LOCK can only be learned from an OPEN reply, and an open satisfied from a delegation produces none. The waiter is therefore not on clp->cl_lock_waitq at all, and polls instead with a doubling 1s/2s/4s backoff - which is where the 1, 3 and 7 second acquisitions come from.

- Nothing wakes cl_lock_waitq when such a lock is released, since the server never saw it and cannot send a CB_NOTIFY_LOCK. A waiter that does reach that queue sleeps out the full 30s NFS4_LOCK_MAXTIMEOUT.

Both need fixing: one puts the waiter somewhere reachable, the other reaches it. They are ordered so that no halfway point regresses - the wake comes first, since adding it alone is harmless, whereas routing waiters onto the queue first would turn the 1-7s stalls into 30s ones.

With both applied the same test shows no acquisition over 2476us, total throughput up 23%, and the two processes splitting the work evenly (1464283/1464448, against 1029148/1355029 before).

Olga Kornievskaia (2):
NFSv4: wake blocking lock waiters when a delegated lock is released
NFSv4: remember that the server may send CB_NOTIFY_LOCK

 fs/nfs/nfs4proc.c         | 21 ++++++++++++++++++---
 include/linux/nfs_fs_sb.h |  1 +
 2 files changed, 19 insertions(+), 3 deletions(-)
Signature · 1 line
2.52.0
author reviewer
#1 lore raw

PATCH 1/2 NFSv4: wake blocking lock waiters when a delegated lock is released

When the client holds a delegation, _nfs4_proc_setlk() caches POSIX locks locally and never sends a LOCK to the server. It has to strip FL_SLEEP to do so, since that branch runs holding sp->so_delegreturn_mutex and nfsi->rwsem and F_SETLKW can block indefinitely. A contending waiter is therefore never placed on the VFS blocked-lock list, and locks_wake_up_blocks() does not reach it when the holder releases.

The waiter blocks in nfs4_retry_setlk() on clp->cl_lock_waitq instead, whose only waker is nfs4_callback_notify_lock(). The server never saw a LOCK for a cached lock, so it does not know anyone is waiting and will never send a CB_NOTIFY_LOCK; nfs4_proc_unlck() returns early without sending LOCKU for the same reason. The waiter sleeps out the full NFS4_LOCK_MAXTIMEOUT of 30s before retrying a lock that is long free.

Wake cl_lock_waitq when releasing a cached lock. The releasing thread cannot tell which waiter wants the lock, so the wakeup names only the inode; make the owner comparison in nfs4_wake_lock_waiter() conditional on cbnl_valid so that a CB_NOTIFY_LOCK still wakes exactly the owner it names. The inode restriction matters: without it every blocked locker for the server is woken, and those on non-delegated state each retry with a LOCK RPC.

Fixes a1d617d8f134 ("nfs: allow blocking locks to be awoken by lock callbacks")
Assisted-by Claude Opus 5
---
 fs/nfs/nfs4proc.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
fs/nfs/nfs4proc.c +16 -3
index e1018b03c1cb..50d36dc58fc6 100644
@@ -7287,8 +7287,15 @@ static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *
 	if (status != 0)
 		goto out;
 	/* Is this a delegated lock? */
-	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
+	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0) {
+		struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
+		struct cb_notify_lock_args cbnl = { .cbnl_valid = false };
+
+		/* The server never saw it, so it will not notify waiters */
+		nfs_copy_fh(&cbnl.cbnl_fh, NFS_FH(inode));
+		__wake_up(&clp->cl_lock_waitq, TASK_NORMAL, 0, &cbnl);
 		goto out;
+	}
 	alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
 	seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
 	status = -ENOMEM;
@@ -7708,8 +7715,14 @@ nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, vo
 		struct nfs_lowner		*lowner = &cbnl->cbnl_owner,
 						*wowner = &waiter->owner;
 
-		/* Only wake if the callback was for the same owner. */
-		if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
+		/*
+		 * Only wake the owner a CB_NOTIFY_LOCK names. A lock cached
+		 * under a delegation is released locally, and the releasing
+		 * thread cannot tell which waiter wants it, so those wakeups
+		 * (cbnl_valid false) name only the inode and wake them all.
+		 */
+		if (cbnl->cbnl_valid &&
+		    (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev))
 			return 0;
 
 		/* Make sure it's for the right inode */
Signature · 1 line
2.52.0
author reviewer
#2 lore raw

PATCH 2/2 NFSv4: remember that the server may send CB_NOTIFY_LOCK

In reply to Olga Kornievskaia

Two processes contending for a POSIX lock on one file see occasional acquisitions of almost exactly 1, 3 or 7 seconds when the client holds a delegation, although the holder releases within microseconds.

nfs4_retry_setlk() only parks a waiter on clp->cl_lock_waitq when NFS_STATE_MAY_NOTIFY_LOCK is set. Otherwise it uses nfs4_retry_setlk_simple(), which has no waitqueue at all and sleeps on a bare timeout starting at NFS4_LOCK_MINTIMEOUT and doubling, so a waiter that loses one poll waits a second, then two, then four.

NFS_STATE_MAY_NOTIFY_LOCK is set in _nfs4_open_and_get_state() from the OPEN reply rflags. It describes the server, but is recorded per nfs4_state and can only be learned from a reply. Once a delegation is held, further opens are satisfied from it with no OPEN on the wire, so o_res.rflags is zero and the new state starts out believing no callback can arrive, even though the open that obtained the delegation was told otherwise.

Record the capability on the nfs_client the first time an OPEN reply carries it, and apply it to every state built for that client. nfs_client is the right scope: the flag describes one server, CB_NOTIFY_LOCK arrives on that client's backchannel, and cl_lock_waitq is per nfs_client.

This also covers a case the per-state flag misses. After the delegation is recalled the state has neither NFS_DELEGATED_STATE nor NFS_STATE_MAY_NOTIFY_LOCK, so locks go to the server and the server does send CB_NOTIFY_LOCK, but the client is in the backoff loop and ignores it.

Fixes a8ce377a5db8 ("nfs: track whether server sets MAY_NOTIFY_LOCK flag")
Assisted-by Claude Opus 5
---
 fs/nfs/nfs4proc.c         | 2 ++
 include/linux/nfs_fs_sb.h | 1 +
 2 files changed, 3 insertions(+)
fs/nfs/nfs4proc.c +2 -0
index 50d36dc58fc6..2b01421edee7 100644
@@ -3093,6 +3093,8 @@ static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
 	if (server->caps & NFS_CAP_POSIX_LOCK)
 		set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
 	if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
+		server->nfs_client->cl_may_notify_lock = true;
+	if (server->nfs_client->cl_may_notify_lock)
 		set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
 	if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
 		set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
include/linux/nfs_fs_sb.h +1 -0
index 416c6f39f31d..6e1b0332f2de 100644
@@ -120,6 +120,7 @@ struct nfs_client {
 #define NFS_SP4_MACH_CRED_COMMIT   6	/* COMMIT */
 #define NFS_SP4_MACH_CRED_PNFS_CLEANUP  7 /* LAYOUTRETURN */
 	wait_queue_head_t	cl_lock_waitq;
+	bool			cl_may_notify_lock;
 #endif /* CONFIG_NFS_V4 */
 
 	/* Our own IP address, as a null-terminated string.
Signature · 1 line
2.52.0