Skip to contentSkip to conversation

nfsd: don't put blocked locks on LRU until after vfs_lock_file returns

PATCH In review Jeff Layton 4 participants 7 messages Nov 23, 2021

Subsystems (2)
lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

PATCH 1/1 nfsd: don't put blocked locks on LRU until after vfs_lock_file returns

Vasily reported a case where vfs_lock_file took a very long time to return (longer than a lease period). The laundromat eventually ran and reaped the thing and when the vfs_lock_file returned, it ended up accessing freed memory.

Don't put entries onto the LRU until vfs_lock_file returns.

---
 fs/nfsd/nfs4state.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/nfsd/nfs4state.c +4 -2
index bfad94c70b84..8cfef84b9355 100644
@@ -6966,10 +6966,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	}
 
 	if (fl_flags & FL_SLEEP) {
-		nbl->nbl_time = ktime_get_boottime_seconds();
 		spin_lock(&nn->blocked_locks_lock);
 		list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
-		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
 		spin_unlock(&nn->blocked_locks_lock);
 	}
 
@@ -6982,6 +6980,10 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 			nn->somebody_reclaimed = true;
 		break;
 	case FILE_LOCK_DEFERRED:
+		nbl->nbl_time = ktime_get_boottime_seconds();
+		spin_lock(&nn->blocked_locks_lock);
+		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
+		spin_unlock(&nn->blocked_locks_lock);
 		nbl = NULL;
 		fallthrough;
 	case -EAGAIN:		/* conflock holds conflicting lock */
Signature · 1 line
2.33.1
Tested-by on v1 1/1
#1 lore raw
Quoted 6 lines · Jeff Layton

Vasily reported a case where vfs_lock_file took a very long time to return (longer than a lease period). The laundromat eventually ran and reaped the thing and when the vfs_lock_file returned, it ended up accessing freed memory.

Don't put entries onto the LRU until vfs_lock_file returns.

Fixes 7919d0a27f1e "nfsd: add a LRU list for blocked locks"
Quoted 2 lines
Quoted 31 lines
---
 fs/nfsd/nfs4state.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/nfsd/nfs4state.c +4 -2
index bfad94c70b84..8cfef84b9355 100644
@@ -6966,10 +6966,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	}
 
 	if (fl_flags & FL_SLEEP) {
-		nbl->nbl_time = ktime_get_boottime_seconds();
 		spin_lock(&nn->blocked_locks_lock);
 		list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
-		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
 		spin_unlock(&nn->blocked_locks_lock);
 	}
 
@@ -6982,6 +6980,10 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 			nn->somebody_reclaimed = true;
 		break;
 	case FILE_LOCK_DEFERRED:
+		nbl->nbl_time = ktime_get_boottime_seconds();
+		spin_lock(&nn->blocked_locks_lock);
+		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
+		spin_unlock(&nn->blocked_locks_lock);
 		nbl = NULL;
 		fallthrough;
 	case -EAGAIN:		/* conflock holds conflicting lock */
#2 lore raw
Quoted 10 lines · Vasily Averin
Quoted 5 lines · Jeff Layton

Vasily reported a case where vfs_lock_file took a very long time to return (longer than a lease period). The laundromat eventually ran and reaped the thing and when the vfs_lock_file returned, it ended up accessing freed memory.

Quoted 8 lines
Quoted 1 line

Don't put entries onto the LRU until vfs_lock_file returns.

Fixes 7919d0a27f1e "nfsd: add a LRU list for blocked locks"
Quoted 2 lines

I assume Bruce is grabbing this for v5.16-rc.

Quoted 19 lines
Quoted 19 lines

---
fs/nfsd/nfs4state.c | 6 ++++--

1 file changed, 4 insertions(+), 2 deletions(-)
fs/nfsd/nfs4state.c +0 -2
index bfad94c70b84..8cfef84b9355 100644
@@ -6966,10 +6966,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	}
 
 	if (fl_flags & FL_SLEEP) {
-		nbl->nbl_time = ktime_get_boottime_seconds();
 		spin_lock(&nn->blocked_locks_lock);
 		list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
-		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
 		spin_unlock(&nn->blocked_locks_lock);
 	}
 
Quoted 13 lines

fs/nfsd/nfs4state.c nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, · line 6989

Quoted 12 lines
fs/nfsd/nfs4state.c +4 -0
@@ -6982,6 +6980,10 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 			nn->somebody_reclaimed = true;
 		break;
 	case FILE_LOCK_DEFERRED:
+		nbl->nbl_time = ktime_get_boottime_seconds();
+		spin_lock(&nn->blocked_locks_lock);
+		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
+		spin_unlock(&nn->blocked_locks_lock);
 		nbl = NULL;
 		fallthrough;
 	case -EAGAIN:		/* conflock holds conflicting lock */

--
Chuck Lever

#3 lore raw
In reply to Jeff Layton
Quoted 4 lines · Jeff Layton

Vasily reported a case where vfs_lock_file took a very long time to return (longer than a lease period). The laundromat eventually ran and reaped the thing and when the vfs_lock_file returned, it ended up accessing freed memory.

By the way, once we've called vfs_lock_file(), is there anything preventing nfsd4_cb_notify_lock_release() from freeing nbl before we get here?

Quoted 37 lines

Don't put entries onto the LRU until vfs_lock_file returns.

---
 fs/nfsd/nfs4state.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/nfsd/nfs4state.c +4 -2
index bfad94c70b84..8cfef84b9355 100644
@@ -6966,10 +6966,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	}
 
 	if (fl_flags & FL_SLEEP) {
-		nbl->nbl_time = ktime_get_boottime_seconds();
 		spin_lock(&nn->blocked_locks_lock);
 		list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
-		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
 		spin_unlock(&nn->blocked_locks_lock);
 	}
 
@@ -6982,6 +6980,10 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 			nn->somebody_reclaimed = true;
 		break;
 	case FILE_LOCK_DEFERRED:
+		nbl->nbl_time = ktime_get_boottime_seconds();
+		spin_lock(&nn->blocked_locks_lock);
+		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
+		spin_unlock(&nn->blocked_locks_lock);
 		nbl = NULL;
 		fallthrough;
 	case -EAGAIN:		/* conflock holds conflicting lock */
Signature · 1 line
2.33.1
author maintainer
#4 lore raw
Quoted 10 lines · J. Bruce Fields
Quoted 4 lines · Jeff Layton

Vasily reported a case where vfs_lock_file took a very long time to return (longer than a lease period). The laundromat eventually ran and reaped the thing and when the vfs_lock_file returned, it ended up accessing freed memory.

By the way, once we've called vfs_lock_file(), is there anything preventing nfsd4_cb_notify_lock_release() from freeing nbl before we get here?

No, I don't think there is. Good catch.

Hmm...the only way I can see to fix that would be to add a refcount to these things, in which case we probably don't need this patch since it would prevent the original issue as well...

Quoted 37 lines
Quoted 37 lines

Don't put entries onto the LRU until vfs_lock_file returns.

---
 fs/nfsd/nfs4state.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/nfsd/nfs4state.c +4 -2
index bfad94c70b84..8cfef84b9355 100644
@@ -6966,10 +6966,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	}
 
 	if (fl_flags & FL_SLEEP) {
-		nbl->nbl_time = ktime_get_boottime_seconds();
 		spin_lock(&nn->blocked_locks_lock);
 		list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
-		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
 		spin_unlock(&nn->blocked_locks_lock);
 	}
 
@@ -6982,6 +6980,10 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 			nn->somebody_reclaimed = true;
 		break;
 	case FILE_LOCK_DEFERRED:
+		nbl->nbl_time = ktime_get_boottime_seconds();
+		spin_lock(&nn->blocked_locks_lock);
+		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
+		spin_unlock(&nn->blocked_locks_lock);
 		nbl = NULL;
 		fallthrough;
 	case -EAGAIN:		/* conflock holds conflicting lock */
Signature · 1 line
2.33.1
Signature · 1 line
Jeff Layton <[email protected]>
#5 lore raw
Quoted 7 lines · Jeff Layton
Quoted 6 lines · J. Bruce Fields
Quoted 4 lines · Jeff Layton

Vasily reported a case where vfs_lock_file took a very long time to return (longer than a lease period). The laundromat eventually ran and reaped the thing and when the vfs_lock_file returned, it ended up accessing freed memory.

Quoted 6 lines
Quoted 4 lines

By the way, once we've called vfs_lock_file(), is there anything preventing nfsd4_cb_notify_lock_release() from freeing nbl before we get here?

No, I don't think there is. Good catch.

It may be a rare race (an rpc's not normally going to reply in that time), but I wouldn't be surprised if there's some error condition where it's possible.

Quoted 3 lines

Hmm...the only way I can see to fix that would be to add a refcount to these things, in which case we probably don't need this patch since it would prevent the original issue as well...

Depending on how long that might take, I'd be OK with applying this as a stopgap.

--b.

Quoted 41 lines
Quoted 38 lines
Quoted 38 lines

Don't put entries onto the LRU until vfs_lock_file returns.

---
 fs/nfsd/nfs4state.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/nfsd/nfs4state.c +4 -2
index bfad94c70b84..8cfef84b9355 100644
@@ -6966,10 +6966,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	}
 
 	if (fl_flags & FL_SLEEP) {
-		nbl->nbl_time = ktime_get_boottime_seconds();
 		spin_lock(&nn->blocked_locks_lock);
 		list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
-		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
 		spin_unlock(&nn->blocked_locks_lock);
 	}
 
@@ -6982,6 +6980,10 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 			nn->somebody_reclaimed = true;
 		break;
 	case FILE_LOCK_DEFERRED:
+		nbl->nbl_time = ktime_get_boottime_seconds();
+		spin_lock(&nn->blocked_locks_lock);
+		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
+		spin_unlock(&nn->blocked_locks_lock);
 		nbl = NULL;
 		fallthrough;
 	case -EAGAIN:		/* conflock holds conflicting lock */
Signature · 1 line
2.33.1
Signature · 1 line
Jeff Layton <[email protected]>
author maintainer
#6 lore raw
Quoted 19 lines · J. Bruce Fields
Quoted 7 lines · Jeff Layton
Quoted 6 lines · J. Bruce Fields
Quoted 4 lines · Jeff Layton

Vasily reported a case where vfs_lock_file took a very long time to return (longer than a lease period). The laundromat eventually ran and reaped the thing and when the vfs_lock_file returned, it ended up accessing freed memory.

Quoted 6 lines
Quoted 4 lines

By the way, once we've called vfs_lock_file(), is there anything preventing nfsd4_cb_notify_lock_release() from freeing nbl before we get here?

No, I don't think there is. Good catch.

It may be a rare race (an rpc's not normally going to reply in that time), but I wouldn't be surprised if there's some error condition where it's possible.

Quoted 8 lines
Quoted 3 lines

Hmm...the only way I can see to fix that would be to add a refcount to these things, in which case we probably don't need this patch since it would prevent the original issue as well...

Depending on how long that might take, I'd be OK with applying this as a stopgap.

I won't have the cycles to do that anytime soon, unfortunately, so you may want to.

Quoted 41 lines
Quoted 41 lines
Quoted 38 lines
Quoted 38 lines

Don't put entries onto the LRU until vfs_lock_file returns.

---
 fs/nfsd/nfs4state.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/nfsd/nfs4state.c +4 -2
index bfad94c70b84..8cfef84b9355 100644
@@ -6966,10 +6966,8 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	}
 
 	if (fl_flags & FL_SLEEP) {
-		nbl->nbl_time = ktime_get_boottime_seconds();
 		spin_lock(&nn->blocked_locks_lock);
 		list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
-		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
 		spin_unlock(&nn->blocked_locks_lock);
 	}
 
@@ -6982,6 +6980,10 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 			nn->somebody_reclaimed = true;
 		break;
 	case FILE_LOCK_DEFERRED:
+		nbl->nbl_time = ktime_get_boottime_seconds();
+		spin_lock(&nn->blocked_locks_lock);
+		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
+		spin_unlock(&nn->blocked_locks_lock);
 		nbl = NULL;
 		fallthrough;
 	case -EAGAIN:		/* conflock holds conflicting lock */
Signature · 1 line
2.33.1
Signature · 1 line
Jeff Layton <[email protected]>
Signature · 1 line
Jeff Layton <[email protected]>