Skip to contentSkip to conversation

filelock: WARN when @filp and fl_file don't match

PATCH In review Jeff Layton 2 participants 10 messages Nov 11, 2022 – Nov 14, 2022

Subsystems (4)
lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

cover filelock: WARN when @filp and fl_file don't match

Eventually, I'd like to reduce the redundant arguments to the locking APIs, but to get there we need to ensure the callers all set their file locks sanely.

Adding the WARN_ON_ONCEs helped to find a couple of warts in lockd's file handling. The first 3 patches fix those.

Chuck, would you be willing to take these in for v6.2? I'd like to see the WARN_ONs added for that so we can try to clean up the file locking APIs for v6.3.

Thanks,

Jeff Layton (4):
lockd: set missing fl_flags field when retrieving args
lockd: ensure we use the correct file description when unlocking
lockd: fix file selection in nlmsvc_cancel_blocked
filelock: WARN_ON_ONCE when ->fl_file and filp don't match

 fs/lockd/svc4proc.c |  1 +
 fs/lockd/svclock.c  | 17 ++++++++++-------
 fs/lockd/svcproc.c  |  1 +
 fs/locks.c          |  3 +++
 4 files changed, 15 insertions(+), 7 deletions(-)
Signature · 1 line
2.38.1
author maintainer
#1 lore raw

PATCH 1/4 lockd: set missing fl_flags field when retrieving args

---
 fs/lockd/svc4proc.c | 1 +
 fs/lockd/svcproc.c  | 1 +
 2 files changed, 2 insertions(+)
fs/lockd/svc4proc.c +1 -0
index 284b019cb652..b72023a6b4c1 100644
@@ -52,6 +52,7 @@ nlm4svc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
 		*filp = file;
 
 		/* Set up the missing parts of the file_lock structure */
+		lock->fl.fl_flags = FL_POSIX;
 		lock->fl.fl_file  = file->f_file[mode];
 		lock->fl.fl_pid = current->tgid;
 		lock->fl.fl_start = (loff_t)lock->lock_start;
fs/lockd/svcproc.c +1 -0
index e35c05e27806..32784f508c81 100644
@@ -77,6 +77,7 @@ nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
 
 		/* Set up the missing parts of the file_lock structure */
 		mode = lock_to_openmode(&lock->fl);
+		lock->fl.fl_flags = FL_POSIX;
 		lock->fl.fl_file  = file->f_file[mode];
 		lock->fl.fl_pid = current->tgid;
 		lock->fl.fl_lmops = &nlmsvc_lock_operations;
Signature · 1 line
2.38.1
author maintainer
#2 lore raw

PATCH 2/4 lockd: ensure we use the correct file description when unlocking

In reply to Jeff Layton

Shared locks are set on O_RDONLY descriptors and exclusive locks are set on O_WRONLY ones. nlmsvc_unlock however calls vfs_lock_file twice, once for each descriptor, but it doesn't reset fl_file. Ensure that it does.

---
 fs/lockd/svclock.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/lockd/svclock.c +6 -4
index 9c1aa75441e1..9eae99e08e69 100644
@@ -659,11 +659,13 @@ nlmsvc_unlock(struct net *net, struct nlm_file *file, struct nlm_lock *lock)
 	nlmsvc_cancel_blocked(net, file, lock);
 
 	lock->fl.fl_type = F_UNLCK;
-	if (file->f_file[O_RDONLY])
-		error = vfs_lock_file(file->f_file[O_RDONLY], F_SETLK,
+	lock->fl.fl_file = file->f_file[O_RDONLY];
+	if (lock->fl.fl_file)
+		error = vfs_lock_file(lock->fl.fl_file, F_SETLK,
 					&lock->fl, NULL);
-	if (file->f_file[O_WRONLY])
-		error = vfs_lock_file(file->f_file[O_WRONLY], F_SETLK,
+	lock->fl.fl_file = file->f_file[O_WRONLY];
+	if (lock->fl.fl_file)
+		error |= vfs_lock_file(lock->fl.fl_file, F_SETLK,
 					&lock->fl, NULL);
 
 	return (error < 0)? nlm_lck_denied_nolocks : nlm_granted;
Signature · 1 line
2.38.1
author maintainer
#3 lore raw

PATCH 3/4 lockd: fix file selection in nlmsvc_cancel_blocked

In reply to Jeff Layton

We currently do a lock_to_openmode call based on the arguments from the NLM_UNLOCK call, but that will always set the fl_type of the lock to F_UNLCK, the the O_RDONLY descriptor is always chosen.

Fix it to use the file_lock from the block instead.

---
 fs/lockd/svclock.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/lockd/svclock.c +4 -3
index 9eae99e08e69..4e30f3c50970 100644
@@ -699,9 +699,10 @@ nlmsvc_cancel_blocked(struct net *net, struct nlm_file *file, struct nlm_lock *l
 	block = nlmsvc_lookup_block(file, lock);
 	mutex_unlock(&file->f_mutex);
 	if (block != NULL) {
-		mode = lock_to_openmode(&lock->fl);
-		vfs_cancel_lock(block->b_file->f_file[mode],
-				&block->b_call->a_args.lock.fl);
+		struct file_lock *fl = &block->b_call->a_args.lock.fl;
+
+		mode = lock_to_openmode(fl);
+		vfs_cancel_lock(block->b_file->f_file[mode], fl);
 		status = nlmsvc_unlink_block(block);
 		nlmsvc_release_block(block);
 	}
Signature · 1 line
2.38.1
#4 lore raw

Re: [PATCH 3/4] lockd: fix file selection in nlmsvc_cancel_blocked

Quoted 7 lines · Jeff Layton

We currently do a lock_to_openmode call based on the arguments from the NLM_UNLOCK call, but that will always set the fl_type of the lock to F_UNLCK, the the O_RDONLY descriptor is always chosen.

Except for the above sentence, these all look sane to me. I can apply them to nfsd's for-next once they've seen some review on fsdevel, as you mentioned in the other thread.

Quoted 28 lines

Fix it to use the file_lock from the block instead.

---
fs/lockd/svclock.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
fs/lockd/svclock.c +4 -3
index 9eae99e08e69..4e30f3c50970 100644
@@ -699,9 +699,10 @@ nlmsvc_cancel_blocked(struct net *net, struct nlm_file *file, struct nlm_lock *l
 	block = nlmsvc_lookup_block(file, lock);
 	mutex_unlock(&file->f_mutex);
 	if (block != NULL) {
-		mode = lock_to_openmode(&lock->fl);
-		vfs_cancel_lock(block->b_file->f_file[mode],
-				&block->b_call->a_args.lock.fl);
+		struct file_lock *fl = &block->b_call->a_args.lock.fl;
+
+		mode = lock_to_openmode(fl);
+		vfs_cancel_lock(block->b_file->f_file[mode], fl);
 		status = nlmsvc_unlink_block(block);
 		nlmsvc_release_block(block);
 	}
Signature · 1 line
2.38.1

--
Chuck Lever

author maintainer
#5 lore raw

Re: [PATCH 3/4] lockd: fix file selection in nlmsvc_cancel_blocked

Quoted 12 lines · Chuck Lever III
Quoted 6 lines · Jeff Layton

We currently do a lock_to_openmode call based on the arguments from the NLM_UNLOCK call, but that will always set the fl_type of the lock to F_UNLCK, the the O_RDONLY descriptor is always chosen.

Except for the above sentence, these all look sane to me. I can apply them to nfsd's for-next once they've seen some review on fsdevel, as you mentioned in the other thread.

Thanks. That should say "and the O_RDONLY...". Fixed in my tree.

I'll go ahead and resend with fsdevel included.

Quoted 34 lines
Quoted 28 lines

Fix it to use the file_lock from the block instead.

---
fs/lockd/svclock.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
fs/lockd/svclock.c +4 -3
index 9eae99e08e69..4e30f3c50970 100644
@@ -699,9 +699,10 @@ nlmsvc_cancel_blocked(struct net *net, struct nlm_file *file, struct nlm_lock *l
 	block = nlmsvc_lookup_block(file, lock);
 	mutex_unlock(&file->f_mutex);
 	if (block != NULL) {
-		mode = lock_to_openmode(&lock->fl);
-		vfs_cancel_lock(block->b_file->f_file[mode],
-				&block->b_call->a_args.lock.fl);
+		struct file_lock *fl = &block->b_call->a_args.lock.fl;
+
+		mode = lock_to_openmode(fl);
+		vfs_cancel_lock(block->b_file->f_file[mode], fl);
 		status = nlmsvc_unlink_block(block);
 		nlmsvc_release_block(block);
 	}
Signature · 1 line
2.38.1

--
Chuck Lever

Signature · 1 line
Jeff Layton <[email protected]>
author maintainer
#6 lore raw

Re: [PATCH 3/4] lockd: fix file selection in nlmsvc_cancel_blocked

Quoted 14 lines · Jeff Layton
Quoted 12 lines · Chuck Lever III
Quoted 6 lines · Jeff Layton

We currently do a lock_to_openmode call based on the arguments from the NLM_UNLOCK call, but that will always set the fl_type of the lock to F_UNLCK, the the O_RDONLY descriptor is always chosen.

Except for the above sentence, these all look sane to me. I can apply them to nfsd's for-next once they've seen some review on fsdevel, as you mentioned in the other thread.

Quoted 4 lines

Thanks. That should say "and the O_RDONLY...". Fixed in my tree.

I'll go ahead and resend with fsdevel included.

I reposted the series Friday afternoon.

What might be best is for you to carry the first 3 patches in the nfsd tree, and I'll take the filelock: patch into the locks-next branch, along with the other filelock API cleanups.

Sound OK?

Quoted 35 lines
Quoted 34 lines
Quoted 28 lines

Fix it to use the file_lock from the block instead.

---
fs/lockd/svclock.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
fs/lockd/svclock.c +4 -3
index 9eae99e08e69..4e30f3c50970 100644
@@ -699,9 +699,10 @@ nlmsvc_cancel_blocked(struct net *net, struct nlm_file *file, struct nlm_lock *l
 	block = nlmsvc_lookup_block(file, lock);
 	mutex_unlock(&file->f_mutex);
 	if (block != NULL) {
-		mode = lock_to_openmode(&lock->fl);
-		vfs_cancel_lock(block->b_file->f_file[mode],
-				&block->b_call->a_args.lock.fl);
+		struct file_lock *fl = &block->b_call->a_args.lock.fl;
+
+		mode = lock_to_openmode(fl);
+		vfs_cancel_lock(block->b_file->f_file[mode], fl);
 		status = nlmsvc_unlink_block(block);
 		nlmsvc_release_block(block);
 	}
Signature · 1 line
2.38.1

--
Chuck Lever

Signature · 1 line
Jeff Layton <[email protected]>
#7 lore raw

Re: [PATCH 3/4] lockd: fix file selection in nlmsvc_cancel_blocked

Quoted 24 lines · Jeff Layton
Quoted 14 lines · Jeff Layton
Quoted 12 lines · Chuck Lever III
Quoted 6 lines · Jeff Layton

We currently do a lock_to_openmode call based on the arguments from the NLM_UNLOCK call, but that will always set the fl_type of the lock to F_UNLCK, the the O_RDONLY descriptor is always chosen.

Except for the above sentence, these all look sane to me. I can apply them to nfsd's for-next once they've seen some review on fsdevel, as you mentioned in the other thread.

Quoted 4 lines

Thanks. That should say "and the O_RDONLY...". Fixed in my tree.

I'll go ahead and resend with fsdevel included.

Quoted 7 lines

I reposted the series Friday afternoon.

What might be best is for you to carry the first 3 patches in the nfsd tree, and I'll take the filelock: patch into the locks-next branch, along with the other filelock API cleanups.

Sound OK?

1/4 through 3/4 have been applied and pushed. Thanks!

--
Chuck Lever

author maintainer
#8 lore raw

PATCH 4/4 filelock: WARN_ON_ONCE when ->fl_file and filp don't match

In reply to Jeff Layton

vfs_lock_file, vfs_test_lock and vfs_cancel_lock all take both a struct file argument and a file_lock. The file_lock has a fl_file field in it howevever and it _must_ match the file passed in.

While most of the locks.c routines use the separately-passed file argument, some filesystems rely on fl_file being filled out correctly.

I'm working on a patch series to remove the redundant argument from these routines, but for now, let's ensure that the callers always set this properly by issuing a WARN_ON_ONCE if they ever don't match.

Cc Chuck Lever <[email protected]>
Cc Trond Myklebust <[email protected]>
---
 fs/locks.c | 3 +++
 1 file changed, 3 insertions(+)
fs/locks.c +3 -0
index 607f94a0e789..5876c8ff0edc 100644
@@ -2146,6 +2146,7 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
  */
 int vfs_test_lock(struct file *filp, struct file_lock *fl)
 {
+	WARN_ON_ONCE(filp != fl->fl_file);
 	if (filp->f_op->lock)
 		return filp->f_op->lock(filp, F_GETLK, fl);
 	posix_test_lock(filp, fl);
@@ -2295,6 +2296,7 @@ int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
  */
 int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
 {
+	WARN_ON_ONCE(filp != fl->fl_file);
 	if (filp->f_op->lock)
 		return filp->f_op->lock(filp, cmd, fl);
 	else
@@ -2663,6 +2665,7 @@ void locks_remove_file(struct file *filp)
  */
 int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
 {
+	WARN_ON_ONCE(filp != fl->fl_file);
 	if (filp->f_op->lock)
 		return filp->f_op->lock(filp, F_CANCELLK, fl);
 	return 0;
Signature · 1 line
2.38.1
author maintainer
#9 lore raw

Re: [PATCH 4/4] filelock: WARN_ON_ONCE when ->fl_file and filp don't match

Quoted 39 lines · Jeff Layton

vfs_lock_file, vfs_test_lock and vfs_cancel_lock all take both a struct file argument and a file_lock. The file_lock has a fl_file field in it howevever and it _must_ match the file passed in.

While most of the locks.c routines use the separately-passed file argument, some filesystems rely on fl_file being filled out correctly.

I'm working on a patch series to remove the redundant argument from these routines, but for now, let's ensure that the callers always set this properly by issuing a WARN_ON_ONCE if they ever don't match.

Cc Chuck Lever <[email protected]>
Cc Trond Myklebust <[email protected]>
---
 fs/locks.c | 3 +++
 1 file changed, 3 insertions(+)
fs/locks.c +2 -0
index 607f94a0e789..5876c8ff0edc 100644
@@ -2146,6 +2146,7 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
  */
 int vfs_test_lock(struct file *filp, struct file_lock *fl)
 {
+	WARN_ON_ONCE(filp != fl->fl_file);
 	if (filp->f_op->lock)
 		return filp->f_op->lock(filp, F_GETLK, fl);
 	posix_test_lock(filp, fl);
@@ -2295,6 +2296,7 @@ int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
  */
 int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
 {
+	WARN_ON_ONCE(filp != fl->fl_file);
 	if (filp->f_op->lock)
 		return filp->f_op->lock(filp, cmd, fl);
 	else

fs/locks.c void locks_remove_file(struct file *filp) · line 2671

Quoted 7 lines
fs/locks.c +1 -0
@@ -2663,6 +2665,7 @@ void locks_remove_file(struct file *filp)
  */
 int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
 {
+	WARN_ON_ONCE(filp != fl->fl_file);
 	if (filp->f_op->lock)
 		return filp->f_op->lock(filp, F_CANCELLK, fl);
 	return 0;

Oops, I meant to cc linux-fsdevel here too. I'll plan to do that on the next posting (assuming that I need to do one).

Cheers,

Signature · 1 line
Jeff Layton <[email protected]>