* [PATCH v2 1/3] fs/locks: Use allocation rather than the stack in fcntl_getlk()
2017-05-30 16:31 [PATCH v2 0/3] Fixups for l_pid Benjamin Coddington
@ 2017-05-30 16:31 ` Benjamin Coddington
2017-05-30 16:31 ` [PATCH v2 2/3] fs/locks: Set fl_nspid at file_lock allocation Benjamin Coddington
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Benjamin Coddington @ 2017-05-30 16:31 UTC (permalink / raw)
To: Alexander Viro, Jeff Layton, bfields
Cc: Christoph Hellwig, linux-fsdevel, linux-nfs
Struct file_lock is fairly large, so let's save some space on the stack by
using an allocation for struct file_lock in fcntl_getlk(), just as we do
for fcntl_setlk().
Signed-off-by: Benjamin Coddington <bcodding@redhat.com>
---
fs/locks.c | 46 ++++++++++++++++++++++++++--------------------
1 file changed, 26 insertions(+), 20 deletions(-)
diff --git a/fs/locks.c b/fs/locks.c
index afefeb4ad6de..d7daa6c8932f 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -2086,14 +2086,17 @@ static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
*/
int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
{
- struct file_lock file_lock;
+ struct file_lock *fl;
int error;
+ fl = locks_alloc_lock();
+ if (fl == NULL)
+ return -ENOMEM;
error = -EINVAL;
if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK)
goto out;
- error = flock_to_posix_lock(filp, &file_lock, flock);
+ error = flock_to_posix_lock(filp, fl, flock);
if (error)
goto out;
@@ -2103,23 +2106,22 @@ int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
goto out;
cmd = F_GETLK;
- file_lock.fl_flags |= FL_OFDLCK;
- file_lock.fl_owner = filp;
+ fl->fl_flags |= FL_OFDLCK;
+ fl->fl_owner = filp;
}
- error = vfs_test_lock(filp, &file_lock);
+ error = vfs_test_lock(filp, fl);
if (error)
goto out;
- flock->l_type = file_lock.fl_type;
- if (file_lock.fl_type != F_UNLCK) {
- error = posix_lock_to_flock(flock, &file_lock);
+ flock->l_type = fl->fl_type;
+ if (fl->fl_type != F_UNLCK) {
+ error = posix_lock_to_flock(flock, fl);
if (error)
- goto rel_priv;
+ goto out;
}
-rel_priv:
- locks_release_private(&file_lock);
out:
+ locks_free_lock(fl);
return error;
}
@@ -2298,14 +2300,18 @@ int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
*/
int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock)
{
- struct file_lock file_lock;
+ struct file_lock *fl;
int error;
+ fl = locks_alloc_lock();
+ if (fl == NULL)
+ return -ENOMEM;
+
error = -EINVAL;
if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK)
goto out;
- error = flock64_to_posix_lock(filp, &file_lock, flock);
+ error = flock64_to_posix_lock(filp, fl, flock);
if (error)
goto out;
@@ -2315,20 +2321,20 @@ int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock)
goto out;
cmd = F_GETLK64;
- file_lock.fl_flags |= FL_OFDLCK;
- file_lock.fl_owner = filp;
+ fl->fl_flags |= FL_OFDLCK;
+ fl->fl_owner = filp;
}
- error = vfs_test_lock(filp, &file_lock);
+ error = vfs_test_lock(filp, fl);
if (error)
goto out;
- flock->l_type = file_lock.fl_type;
- if (file_lock.fl_type != F_UNLCK)
- posix_lock_to_flock64(flock, &file_lock);
+ flock->l_type = fl->fl_type;
+ if (fl->fl_type != F_UNLCK)
+ posix_lock_to_flock64(flock, fl);
- locks_release_private(&file_lock);
out:
+ locks_free_lock(fl);
return error;
}
--
2.9.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 2/3] fs/locks: Set fl_nspid at file_lock allocation
2017-05-30 16:31 [PATCH v2 0/3] Fixups for l_pid Benjamin Coddington
2017-05-30 16:31 ` [PATCH v2 1/3] fs/locks: Use allocation rather than the stack in fcntl_getlk() Benjamin Coddington
@ 2017-05-30 16:31 ` Benjamin Coddington
2017-05-30 16:31 ` [PATCH v2 3/3] fs/locks: Use fs-specific l_pid for remote locks Benjamin Coddington
2017-05-31 17:16 ` [PATCH v2 0/3] Fixups for l_pid Jeff Layton
3 siblings, 0 replies; 5+ messages in thread
From: Benjamin Coddington @ 2017-05-30 16:31 UTC (permalink / raw)
To: Alexander Viro, Jeff Layton, bfields
Cc: Christoph Hellwig, linux-fsdevel, linux-nfs
Since commit c69899a17ca4 "NFSv4: Update of VFS byte range lock must be
atomic with the stateid update", NFSv4 has been inserting locks in rpciod
worker context. The result is that the file_lock's fl_nspid is the
kworker's pid instead of the original userspace pid. We can fix that up by
setting fl_nspid in locks_allocate_lock, and tranfer it to the file_lock
that's eventually recorded.
Signed-off-by: Benjamin Coddington <bcodding@redhat.com>
---
fs/locks.c | 29 ++++++++++++++++++++---------
1 file changed, 20 insertions(+), 9 deletions(-)
diff --git a/fs/locks.c b/fs/locks.c
index d7daa6c8932f..0f5a461b8da6 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -249,7 +249,9 @@ locks_dump_ctx_list(struct list_head *list, char *list_type)
struct file_lock *fl;
list_for_each_entry(fl, list, fl_list) {
- pr_warn("%s: fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", list_type, fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid);
+ pr_warn("%s: fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u fl_nspid=%u\n",
+ list_type, fl->fl_owner, fl->fl_flags, fl->fl_type,
+ fl->fl_pid, pid_vnr(fl->fl_nspid));
}
}
@@ -294,8 +296,10 @@ struct file_lock *locks_alloc_lock(void)
{
struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
- if (fl)
+ if (fl) {
locks_init_lock_heads(fl);
+ fl->fl_nspid = get_pid(task_tgid(current));
+ }
return fl;
}
@@ -328,6 +332,8 @@ void locks_free_lock(struct file_lock *fl)
BUG_ON(!hlist_unhashed(&fl->fl_link));
locks_release_private(fl);
+ if (fl->fl_nspid)
+ put_pid(fl->fl_nspid);
kmem_cache_free(filelock_cache, fl);
}
EXPORT_SYMBOL(locks_free_lock);
@@ -357,8 +363,15 @@ EXPORT_SYMBOL(locks_init_lock);
*/
void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
{
+ struct pid *replace_pid = new->fl_nspid;
+
new->fl_owner = fl->fl_owner;
new->fl_pid = fl->fl_pid;
+ if (fl->fl_nspid) {
+ new->fl_nspid = get_pid(fl->fl_nspid);
+ if (replace_pid)
+ put_pid(replace_pid);
+ }
new->fl_file = NULL;
new->fl_flags = fl->fl_flags;
new->fl_type = fl->fl_type;
@@ -733,7 +746,6 @@ static void locks_wake_up_blocks(struct file_lock *blocker)
static void
locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before)
{
- fl->fl_nspid = get_pid(task_tgid(current));
list_add_tail(&fl->fl_list, before);
locks_insert_global_locks(fl);
}
@@ -743,10 +755,6 @@ locks_unlink_lock_ctx(struct file_lock *fl)
{
locks_delete_global_locks(fl);
list_del_init(&fl->fl_list);
- if (fl->fl_nspid) {
- put_pid(fl->fl_nspid);
- fl->fl_nspid = NULL;
- }
locks_wake_up_blocks(fl);
}
@@ -823,8 +831,6 @@ posix_test_lock(struct file *filp, struct file_lock *fl)
list_for_each_entry(cfl, &ctx->flc_posix, fl_list) {
if (posix_locks_conflict(fl, cfl)) {
locks_copy_conflock(fl, cfl);
- if (cfl->fl_nspid)
- fl->fl_pid = pid_vnr(cfl->fl_nspid);
goto out;
}
}
@@ -2452,6 +2458,7 @@ void locks_remove_posix(struct file *filp, fl_owner_t owner)
lock.fl_end = OFFSET_MAX;
lock.fl_owner = owner;
lock.fl_pid = current->tgid;
+ lock.fl_nspid = get_pid(task_tgid(current));
lock.fl_file = filp;
lock.fl_ops = NULL;
lock.fl_lmops = NULL;
@@ -2460,6 +2467,7 @@ void locks_remove_posix(struct file *filp, fl_owner_t owner)
if (lock.fl_ops && lock.fl_ops->fl_release_private)
lock.fl_ops->fl_release_private(&lock);
+ put_pid(lock.fl_nspid);
trace_locks_remove_posix(inode, &lock, error);
}
@@ -2482,6 +2490,8 @@ locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
if (list_empty(&flctx->flc_flock))
return;
+ fl.fl_nspid = get_pid(task_tgid(current));
+
if (filp->f_op->flock && is_remote_lock(filp))
filp->f_op->flock(filp, F_SETLKW, &fl);
else
@@ -2489,6 +2499,7 @@ locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
if (fl.fl_ops && fl.fl_ops->fl_release_private)
fl.fl_ops->fl_release_private(&fl);
+ put_pid(fl.fl_nspid);
}
/* The i_flctx must be valid when calling into here */
--
2.9.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 3/3] fs/locks: Use fs-specific l_pid for remote locks
2017-05-30 16:31 [PATCH v2 0/3] Fixups for l_pid Benjamin Coddington
2017-05-30 16:31 ` [PATCH v2 1/3] fs/locks: Use allocation rather than the stack in fcntl_getlk() Benjamin Coddington
2017-05-30 16:31 ` [PATCH v2 2/3] fs/locks: Set fl_nspid at file_lock allocation Benjamin Coddington
@ 2017-05-30 16:31 ` Benjamin Coddington
2017-05-31 17:16 ` [PATCH v2 0/3] Fixups for l_pid Jeff Layton
3 siblings, 0 replies; 5+ messages in thread
From: Benjamin Coddington @ 2017-05-30 16:31 UTC (permalink / raw)
To: Alexander Viro, Jeff Layton, bfields
Cc: Christoph Hellwig, linux-fsdevel, linux-nfs
Now that we're setting fl_nspid at file_lock allocation, we need to handle
the case where a remote filesystem directly sets fl_pid. If the filesystem
implements the lock operation, set a flag to return the lock's fl_pid,
rather than fl_nspid.
Signed-off-by: Benjamin Coddington <bcodding@redhat.com>
---
fs/locks.c | 25 +++++++++++++++++++------
include/linux/fs.h | 7 +++++++
2 files changed, 26 insertions(+), 6 deletions(-)
diff --git a/fs/locks.c b/fs/locks.c
index 0f5a461b8da6..c7a222a8eaa7 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -2047,16 +2047,28 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
*/
int vfs_test_lock(struct file *filp, struct file_lock *fl)
{
- if (filp->f_op->lock && is_remote_lock(filp))
+ if (filp->f_op->lock && is_remote_lock(filp)) {
+ fl->fl_flags |= FL_PID_PRIV;
return filp->f_op->lock(filp, F_GETLK, fl);
+ }
posix_test_lock(filp, fl);
return 0;
}
EXPORT_SYMBOL_GPL(vfs_test_lock);
+static inline int flock_translate_pid(struct file_lock *fl)
+{
+ if (IS_OFDLCK(fl))
+ return -1;
+ if (fl->fl_flags & FL_PID_PRIV)
+ return fl->fl_pid;
+ return pid_vnr(fl->fl_nspid);
+}
+
+
static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
{
- flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
+ flock->l_pid = flock_translate_pid(fl);
#if BITS_PER_LONG == 32
/*
* Make sure we can represent the posix lock via
@@ -2078,7 +2090,7 @@ static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
#if BITS_PER_LONG == 32
static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
{
- flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
+ flock->l_pid = flock_translate_pid(fl);
flock->l_start = fl->fl_start;
flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
fl->fl_end - fl->fl_start + 1;
@@ -2596,7 +2608,9 @@ static void lock_get_status(struct seq_file *f, struct file_lock *fl,
struct inode *inode = NULL;
unsigned int fl_pid;
- if (fl->fl_nspid) {
+ if (fl->fl_flags & FL_PID_PRIV) {
+ fl_pid = fl->fl_pid;
+ } else {
struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
/* Don't let fl_pid change based on who is reading the file */
@@ -2609,8 +2623,7 @@ static void lock_get_status(struct seq_file *f, struct file_lock *fl,
*/
if (fl_pid == 0)
return;
- } else
- fl_pid = fl->fl_pid;
+ }
if (fl->fl_file != NULL)
inode = locks_inode(fl->fl_file);
diff --git a/include/linux/fs.h b/include/linux/fs.h
index aa4affb38c39..11794cbaaed0 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -908,6 +908,7 @@ static inline struct file *get_file(struct file *f)
#define FL_UNLOCK_PENDING 512 /* Lease is being broken */
#define FL_OFDLCK 1024 /* lock is "owned" by struct file */
#define FL_LAYOUT 2048 /* outstanding pNFS layout */
+#define FL_PID_PRIV 4096 /* F_GETLK should report fl_pid */
#define FL_CLOSE_POSIX (FL_POSIX | FL_CLOSE)
@@ -973,6 +974,12 @@ int opens_in_grace(struct net *);
* 3) lock range end
*
* Obviously, the last two criteria only matter for POSIX locks.
+ *
+ * fl_pid and fl_nspid appear redundant, but fl_pid is used to handle cases
+ * where a local F_GETLK returns a lock that was granted to a remote owner,
+ * as might exist with a remotely available filesystem like NFS. For that
+ * case, the lock manager should set fl_pid, and that value is returned to a
+ * local F_GETLK.
*/
struct file_lock {
struct file_lock *fl_next; /* singly linked list for this inode */
--
2.9.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 0/3] Fixups for l_pid
2017-05-30 16:31 [PATCH v2 0/3] Fixups for l_pid Benjamin Coddington
` (2 preceding siblings ...)
2017-05-30 16:31 ` [PATCH v2 3/3] fs/locks: Use fs-specific l_pid for remote locks Benjamin Coddington
@ 2017-05-31 17:16 ` Jeff Layton
3 siblings, 0 replies; 5+ messages in thread
From: Jeff Layton @ 2017-05-31 17:16 UTC (permalink / raw)
To: Benjamin Coddington, Alexander Viro, bfields
Cc: Christoph Hellwig, linux-fsdevel, linux-nfs
On Tue, 2017-05-30 at 12:31 -0400, Benjamin Coddington wrote:
> LTP fcntl tests (fcntl11 fcntl14 fcntl17 fcntl19 fcntl20 fcntl21) have been
> failing for NFSv4 mounts due to an unexpected l_pid. What follows are some
> fixups:
>
> on v2:
> - Rebase onto linux-next
> - Revert back to using the stack in locks_mandatory_area(), and fixup
> patch description for 1/3
> - Add a comment to 3/3 explaining the seemingly-redundant fl_pid and
> fl_nspid
>
> These three patches can be pulled from the branch named "fixups_for_l_pid"
> here: git://bcodding.com/~bcodding/linux
>
> Benjamin Coddington (3):
> fs/locks: Use allocation rather than the stack in fcntl_getlk()
> fs/locks: Set fl_nspid at file_lock allocation
> fs/locks: Use fs-specific l_pid for remote locks
>
> fs/locks.c | 100 ++++++++++++++++++++++++++++++++++-------------------
> include/linux/fs.h | 7 ++++
> 2 files changed, 72 insertions(+), 35 deletions(-)
>
This all looks good to me. Nice work!
I've gone ahead and merged it into my linux-next branch, and it should
make v4.13 (barring any problems).
Thanks!
--
Jeff Layton <jlayton@redhat.com>
^ permalink raw reply [flat|nested] 5+ messages in thread