fs: fix inode->i_flctx accesses
Subsystems (7)
Jeff Layton
maintainercover fs: fix inode->i_flctx accesses
The inode->i_flctx field is set via a cmpxchg operation, which is a release op. This means that most callers need to use an acquire to access it.
This series adds a new helper function for that, and replaces the existing accesses of i_flctx with that. The later patches then convert the various subsystems that access i_flctx directly to use the new helper.
Assuming there are no objectsions, I'll plan to merge this for v6.2 via my tree. If any maintainers want to take the subsystem patches in via their trees, let me know and we'll work it out.
Jeff Layton (7):
filelock: add a new locks_inode_context accessor function
ceph: use locks_inode_context helper
cifs: use locks_inode_context helper
ksmbd: use locks_inode_context helper
lockd: use locks_inode_context helper
nfs: use locks_inode_context helper
nfsd: use locks_inode_context helper
fs/ceph/locks.c | 4 ++-- fs/cifs/file.c | 2 +- fs/ksmbd/vfs.c | 2 +- fs/lockd/svcsubs.c | 4 ++-- fs/locks.c | 20 ++++++++++---------- fs/nfs/delegation.c | 2 +- fs/nfs/nfs4state.c | 2 +- fs/nfs/pagelist.c | 2 +- fs/nfs/write.c | 4 ++-- fs/nfsd/nfs4state.c | 6 +++--- include/linux/fs.h | 14 ++++++++++++++ 11 files changed, 38 insertions(+), 24 deletions(-)
Signature · 1 line
2.38.1
Jeff Layton
maintainerPATCH 1/7 filelock: add a new locks_inode_context accessor function
There are a number of places in the kernel that are accessing the inode->i_flctx field without smp_load_acquire. This is required to ensure that the caller doesn't see a partially-initialized structure.
Add a new accessor function for it to make this clear.
--- fs/locks.c | 20 ++++++++++---------- include/linux/fs.h | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 10 deletions(-)
fs/locks.c +10 -10
@@ -175,7 +175,7 @@ locks_get_lock_context(struct inode *inode, int type)struct file_lock_context *ctx;/* paired with cmpxchg() below */ - ctx =smp_load_acquire (&inode->i_flctx); + ctx =locks_inode_context (inode);if (likely (ctx) || type ==F_UNLCK ) goto out;
@@ -194,7 +194,7 @@ locks_get_lock_context(struct inode *inode, int type)*/ if (cmpxchg (&inode->i_flctx,NULL , ctx)) {kmem_cache_free (flctx_cache, ctx); - ctx =smp_load_acquire (&inode->i_flctx); + ctx =locks_inode_context (inode); } out:trace_locks_get_lock_context (inode, type, ctx);
@@ -891,7 +891,7 @@ posix_test_lock(struct file *filp, struct file_lock *fl)void *owner;void (*func)(void ); - ctx =smp_load_acquire (&inode->i_flctx); + ctx =locks_inode_context (inode);if (!ctx ||list_empty_careful (&ctx->flc_posix)) { fl->fl_type =F_UNLCK ;return ;
@@ -1483,7 +1483,7 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type) new_fl->fl_flags = type;/* typically we will check that ctx is non-NULL before calling */ - ctx =smp_load_acquire (&inode->i_flctx); + ctx =locks_inode_context (inode);if (!ctx) {WARN_ON_ONCE (1 ); goto free_lock;
@@ -1588,7 +1588,7 @@ void lease_get_mtime(struct inode *inode, struct timespec64 *time)struct file_lock_context *ctx;struct file_lock *fl; - ctx =smp_load_acquire (&inode->i_flctx); + ctx =locks_inode_context (inode);if (ctx && !list_empty_careful (&ctx->flc_lease)) {spin_lock (&ctx->flc_lock); fl = list_first_entry_or_null(&ctx->flc_lease,
@@ -1634,7 +1634,7 @@ int fcntl_getlease(struct file *filp)int type =F_UNLCK ;LIST_HEAD (dispose); - ctx =smp_load_acquire (&inode->i_flctx); + ctx =locks_inode_context (inode);if (ctx && !list_empty_careful (&ctx->flc_lease)) {percpu_down_read (&file_rwsem);spin_lock (&ctx->flc_lock);
@@ -1823,7 +1823,7 @@ static int generic_delete_lease(struct file *filp, void *owner)struct file_lock_context *ctx;LIST_HEAD (dispose); - ctx =smp_load_acquire (&inode->i_flctx); + ctx =locks_inode_context (inode);if (!ctx) {trace_generic_delete_lease (inode,NULL );return error;
@@ -2563,7 +2563,7 @@ void locks_remove_posix(struct file *filp, fl_owner_t owner)* posix_lock_file(). Another process could be setting a lock on this * file at the same time, but we wouldn't remove that lock anyway. */ - ctx =smp_load_acquire (&inode->i_flctx); + ctx =locks_inode_context (inode);if (!ctx ||list_empty (&ctx->flc_posix))return ;
@@ -2684,7 +2684,7 @@ bool vfs_inode_has_locks(struct inode *inode)struct file_lock_context *ctx;bool ret; - ctx =smp_load_acquire (&inode->i_flctx); + ctx =locks_inode_context (inode);if (!ctx)return false;
@@ -2865,7 +2865,7 @@ void show_fd_locks(struct seq_file *f,struct file_lock_context *ctx;int id =0 ; - ctx =smp_load_acquire (&inode->i_flctx); + ctx =locks_inode_context (inode);if (!ctx)return ;
include/linux/fs.h +14 -0
@@ -1187,6 +1187,13 @@ extern void show_fd_locks(struct seq_file *f, struct file *filp, struct files_struct *files);extern bool locks_owner_has_blockers (struct file_lock_context *flctx,fl_owner_t owner); + +static inline struct file_lock_context * +locks_inode_context (const struct inode *inode) +{ +return smp_load_acquire (&inode->i_flctx); +} +#else /* !CONFIG_FILE_LOCKING */ static inline int fcntl_getlk (struct file *file,unsigned int cmd,struct flock __user *user)
@@ -1327,6 +1334,13 @@ static inline bool locks_owner_has_blockers(struct file_lock_context *flctx, {return false; } + +static inline struct file_lock_context * +locks_inode_context (const struct inode *inode) +{ +return NULL ; +} +#endif /* !CONFIG_FILE_LOCKING */ static inline struct inode *file_inode (const struct file *f)
Signature · 1 line
2.38.1
Re: [PATCH 1/7] filelock: add a new locks_inode_context accessor function
Review
Quoted 2 lines · Jeff Layton
(quoted diff) +1
-1- ctx = smp_load_acquire(&inode->i_flctx); + ctx = locks_inode_context(inode);
Nit: this might be a good time to drop the weird double space here.
Otherwise looks good:
Jeff Layton
maintainerRe: [PATCH 1/7] filelock: add a new locks_inode_context accessor function
Quoted 9 lines · Christoph Hellwig
Review
Quoted 2 lines · Jeff Layton
(quoted diff) +1
-1- ctx = smp_load_acquire(&inode->i_flctx); + ctx = locks_inode_context(inode);Nit: this might be a good time to drop the weird double space here.
Otherwise looks good:
Reviewed-by Christoph Hellwig <[email protected]>
Fixed the nit in my tree.
After sending this, I converted locks_remove_file to use the helper too. I also think we probably need to use this helper in locks_free_lock_context.
Folded all 3 changes into this patch, and pushed to my linux-next feeder branch.
Thanks Christoph!
Signature · 1 line
Jeff Layton <[email protected]>
Jeff Layton
maintainerPATCH 2/7 ceph: use locks_inode_context helper
In reply to Jeff Laytonceph currently doesn't access i_flctx safely. This requires a smp_load_acquire, as the pointer is set via cmpxchg (a release operation).
--- fs/ceph/locks.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/ceph/locks.c +2 -2
@@ -364,7 +364,7 @@ void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count) *fcntl_count =0 ; *flock_count =0 ; - ctx = inode->i_flctx; + ctx =locks_inode_context (inode);if (ctx) {spin_lock (&ctx->flc_lock);list_for_each_entry (lock, &ctx->flc_posix, fl_list)
@@ -418,7 +418,7 @@ int ceph_encode_locks_to_buffer(struct inode *inode, int num_fcntl_locks, int num_flock_locks) {struct file_lock *lock; -struct file_lock_context *ctx = inode->i_flctx; +struct file_lock_context *ctx =locks_inode_context (inode);int err =0 ;int seen_fcntl =0 ;int seen_flock =0 ;
Signature · 1 line
2.38.1
Re: [PATCH 2/7] ceph: use locks_inode_context helper
Looks good:
Re: [PATCH 2/7] ceph: use locks_inode_context helper
In reply to Jeff LaytonQuoted 24 lines · Jeff Layton
ceph currently doesn't access
i_flctxsafely. This requires asmp_load_acquire, as the pointer is set via cmpxchg (a release operation).Cc Xiubo Li <[email protected]>Signed-off-by Jeff Layton <[email protected]>--- fs/ceph/locks.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)fs/ceph/locks.c +1
-1@@ -364,7 +364,7 @@ void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count) *fcntl_count =0 ; *flock_count =0 ; - ctx = inode->i_flctx; + ctx =locks_inode_context (inode);if (ctx) {spin_lock (&ctx->flc_lock);list_for_each_entry (lock, &ctx->flc_posix, fl_list)
fs/ceph/locks.c int ceph_encode_locks_to_buffer(struct inode *inode, · line 424
Quoted 8 lines
fs/ceph/locks.c +1
-1@@ -418,7 +418,7 @@ int ceph_encode_locks_to_buffer(struct inode *inode, int num_fcntl_locks, int num_flock_locks) {struct file_lock *lock; -struct file_lock_context *ctx = inode->i_flctx; +struct file_lock_context *ctx =locks_inode_context (inode);int err =0 ;int seen_fcntl =0 ;int seen_flock =0 ;
Thanks Jeff!
Jeff Layton
maintainerPATCH 3/7 cifs: use locks_inode_context helper
In reply to Jeff Laytoncifs currently doesn't access i_flctx safely. This requires a smp_load_acquire, as the pointer is set via cmpxchg (a release operation).
--- fs/cifs/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/cifs/file.c +1 -1
@@ -1413,7 +1413,7 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)struct inode *inode =d_inode (cfile->dentry);struct cifs_tcon *tcon =tlink_tcon (cfile->tlink);struct file_lock *flock; -struct file_lock_context *flctx = inode->i_flctx; +struct file_lock_context *flctx =locks_inode_context (inode);unsigned int count =0 , i;int rc =0 , xid, type;struct list_head locks_to_send, *el;
Signature · 1 line
2.38.1
Re: [PATCH 3/7] cifs: use locks_inode_context helper
Looks good:
Jeff Layton
maintainerPATCH 4/7 ksmbd: use locks_inode_context helper
In reply to Jeff Laytonksmbd currently doesn't access i_flctx safely. This requires a smp_load_acquire, as the pointer is set via cmpxchg (a release operation).
--- fs/ksmbd/vfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ksmbd/vfs.c +1 -1
@@ -321,7 +321,7 @@ static int check_lock_range(struct file *filp, loff_t start, loff_t end, unsigned char type) {struct file_lock *flock; -struct file_lock_context *ctx =file_inode (filp)->i_flctx; +struct file_lock_context *ctx =locks_inode_context (file_inode (filp));int error =0 ;if (!ctx ||list_empty_careful (&ctx->flc_posix))
Signature · 1 line
2.38.1
Re: [PATCH 4/7] ksmbd: use locks_inode_context helper
Looks good:
Re: [PATCH 4/7] ksmbd: use locks_inode_context helper
In reply to Jeff Layton2022-11-17 0:17 GMT+09:00, Jeff Layton <[email protected]>:
Quoted 7 lines
ksmbd currently doesn't access
i_flctxsafely. This requires asmp_load_acquire, as the pointer is set via cmpxchg (a release operation).Cc Namjae Jeon <[email protected]>Cc Steve French <[email protected]>Signed-off-by Jeff Layton <[email protected]>
Thanks for your patch!
Jeff Layton
maintainerPATCH 5/7 lockd: use locks_inode_context helper
In reply to Jeff Laytonlockd currently doesn't access i_flctx safely. This requires a smp_load_acquire, as the pointer is set via cmpxchg (a release operation).
--- fs/lockd/svcsubs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/lockd/svcsubs.c +2 -2
@@ -207,7 +207,7 @@ nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file, {struct inode *inode =nlmsvc_file_inode (file);struct file_lock *fl; -struct file_lock_context *flctx = inode->i_flctx; +struct file_lock_context *flctx =locks_inode_context (inode);struct nlm_host *lockhost;if (!flctx ||list_empty_careful (&flctx->flc_posix))
@@ -262,7 +262,7 @@ nlm_file_inuse(struct nlm_file *file) {struct inode *inode =nlmsvc_file_inode (file);struct file_lock *fl; -struct file_lock_context *flctx = inode->i_flctx; +struct file_lock_context *flctx =locks_inode_context (inode);if (file->f_count || !list_empty (&file->f_blocks) || file->f_shares)return 1 ;
Signature · 1 line
2.38.1
Re: [PATCH 5/7] lockd: use locks_inode_context helper
Looks good:
Jeff Layton
maintainerPATCH 6/7 nfs: use locks_inode_context helper
In reply to Jeff Laytonnfs currently doesn't access i_flctx safely. This requires a smp_load_acquire, as the pointer is set via cmpxchg (a release operation).
--- fs/nfs/delegation.c | 2 +- fs/nfs/nfs4state.c | 2 +- fs/nfs/pagelist.c | 2 +- fs/nfs/write.c | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-)
fs/nfs/delegation.c +1 -1
@@ -146,7 +146,7 @@ static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_state {struct inode *inode = state->inode;struct file_lock *fl; -struct file_lock_context *flctx = inode->i_flctx; +struct file_lock_context *flctx =locks_inode_context (inode);struct list_head *list;int status =0 ;
fs/nfs/nfs4state.c +1 -1
@@ -1501,7 +1501,7 @@ static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_struct file_lock *fl;struct nfs4_lock_state *lsp;int status =0 ; -struct file_lock_context *flctx = inode->i_flctx; +struct file_lock_context *flctx =locks_inode_context (inode);struct list_head *list;if (flctx ==NULL )
fs/nfs/pagelist.c +1 -1
@@ -1055,7 +1055,7 @@ static unsigned int nfs_coalesce_size(struct nfs_page *prev,if (prev) {if (!nfs_match_open_context (nfs_req_openctx (req),nfs_req_openctx (prev)))return 0 ; - flctx =d_inode (nfs_req_openctx (req)->dentry)->i_flctx; + flctx =locks_inode_context (d_inode (nfs_req_openctx (req)->dentry)); if (flctx != NULL && !(list_empty_careful(&flctx->flc_posix) && list_empty_careful(&flctx->flc_flock)) &&
fs/nfs/write.c +2 -2
@@ -1185,7 +1185,7 @@ int nfs_flush_incompatible(struct file *file, struct page *page) {struct nfs_open_context *ctx =nfs_file_open_context (file);struct nfs_lock_context *l_ctx; -struct file_lock_context *flctx =file_inode (file)->i_flctx; +struct file_lock_context *flctx =locks_inode_context (file_inode (file));struct nfs_page *req;int do_flush, status;/*
@@ -1321,7 +1321,7 @@ static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode, unsigned int pagelen) { int ret; -struct file_lock_context *flctx = inode->i_flctx; +struct file_lock_context *flctx =locks_inode_context (inode);struct file_lock *fl;if (file->f_flags &O_DSYNC )
Signature · 1 line
2.38.1
Re: [PATCH 6/7] nfs: use locks_inode_context helper
Quoted 3 lines · Jeff Layton
nfs currently doesn't access
i_flctxsafely. This requires asmp_load_acquire, as the pointer is set via cmpxchg (a release operation).
Looks good:
Jeff Layton
maintainerPATCH 7/7 nfsd: use locks_inode_context helper
In reply to Jeff Laytonnfsd currently doesn't access i_flctx safely everywhere. This requires a smp_load_acquire, as the pointer is set via cmpxchg (a release operation).
--- fs/nfsd/nfs4state.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +3 -3
@@ -4758,7 +4758,7 @@ nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)static bool nfsd4_deleg_present (const struct inode *inode) { -struct file_lock_context *ctx =smp_load_acquire (&inode->i_flctx); +struct file_lock_context *ctx =locks_inode_context (inode);return ctx && !list_empty_careful (&ctx->flc_lease); }
@@ -5897,7 +5897,7 @@ nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)list_for_each_entry (stp, &lo->lo_owner.so_stateids, st_perstateowner) { nf = stp->st_stid.sc_file; - ctx = nf->fi_inode->i_flctx; + ctx =locks_inode_context (nf->fi_inode);if (!ctx)continue ;if (locks_owner_has_blockers (ctx, lo))
@@ -7713,7 +7713,7 @@ check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) } inode =locks_inode (nf->nf_file); - flctx = inode->i_flctx; + flctx =locks_inode_context (inode);if (flctx && !list_empty_careful (&flctx->flc_posix)) {spin_lock (&flctx->flc_lock);
Signature · 1 line
2.38.1
Re: [PATCH 7/7] nfsd: use locks_inode_context helper
Quoted 10 lines · Jeff Layton
nfsd currently doesn't access
i_flctxsafely everywhere. This requires asmp_load_acquire, as the pointer is set via cmpxchg (a release operation).Cc Chuck Lever <[email protected]>Signed-off-by Jeff Layton <[email protected]>
Quoted 38 lines
---
fs/nfsd/nfs4state.c| 6 +++---1 file changed, 3 insertions(+), 3 deletions(-)fs/nfsd/nfs4state.c +3
-3@@ -4758,7 +4758,7 @@ nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)static bool nfsd4_deleg_present (const struct inode *inode) { -struct file_lock_context *ctx =smp_load_acquire (&inode->i_flctx); +struct file_lock_context *ctx =locks_inode_context (inode);return ctx && !list_empty_careful (&ctx->flc_lease); }@@ -5897,7 +5897,7 @@ nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)list_for_each_entry (stp, &lo->lo_owner.so_stateids, st_perstateowner) { nf = stp->st_stid.sc_file; - ctx = nf->fi_inode->i_flctx; + ctx =locks_inode_context (nf->fi_inode);if (!ctx)continue ;if (locks_owner_has_blockers (ctx, lo))@@ -7713,7 +7713,7 @@ check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) } inode =locks_inode (nf->nf_file); - flctx = inode->i_flctx; + flctx =locks_inode_context (inode);if (flctx && !list_empty_careful (&flctx->flc_posix)) {spin_lock (&flctx->flc_lock);Signature · 1 line
2.38.1
--
Chuck Lever
Jeff Layton
maintainerRe: [PATCH 7/7] nfsd: use locks_inode_context helper
Quoted 28 lines · Chuck Lever III
Quoted 9 lines · Jeff Layton
nfsd currently doesn't access
i_flctxsafely everywhere. This requires asmp_load_acquire, as the pointer is set via cmpxchg (a release operation).Cc Chuck Lever <[email protected]>Signed-off-by Jeff Layton <[email protected]>Acked-by Chuck Lever <[email protected]>Quoted 15 lines
---
fs/nfsd/nfs4state.c| 6 +++---1 file changed, 3 insertions(+), 3 deletions(-)fs/nfsd/nfs4state.c +1
-1@@ -4758,7 +4758,7 @@ nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)static bool nfsd4_deleg_present (const struct inode *inode) { -struct file_lock_context *ctx =smp_load_acquire (&inode->i_flctx); +struct file_lock_context *ctx =locks_inode_context (inode);
Quoted 8 lines
fs/nfsd/nfs4state.c nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo) · line 5900
Quoted 8 lines
fs/nfsd/nfs4state.c +1
-1return ctx && !list_empty_careful (&ctx->flc_lease); }@@ -5897,7 +5897,7 @@ nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)list_for_each_entry (stp, &lo->lo_owner.so_stateids, st_perstateowner) { nf = stp->st_stid.sc_file; - ctx = nf->fi_inode->i_flctx; + ctx =locks_inode_context (nf->fi_inode);
Thanks Chuck.
To be clear: I think the above access is probably OK. We wouldn't have a lock stateid unless we had a valid lock context in the inode. That said, doing it this way keeps everything consistent, so I'm inclined to leave the patch as-is.
check_for_locks definitely needs this though.
Quoted 21 lines
Quoted 15 lines
fs/nfsd/nfs4state.c +1
-1if (!ctx)continue ;if (locks_owner_has_blockers (ctx, lo))@@ -7713,7 +7713,7 @@ check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) } inode =locks_inode (nf->nf_file); - flctx = inode->i_flctx; + flctx =locks_inode_context (inode);if (flctx && !list_empty_careful (&flctx->flc_posix)) {spin_lock (&flctx->flc_lock);Signature · 1 line
2.38.1--
Chuck Lever
Signature · 1 line
Jeff Layton <[email protected]>
Re: [PATCH 7/7] nfsd: use locks_inode_context helper
In reply to Jeff LaytonQuoted 3 lines · Jeff Layton
nfsd currently doesn't access
i_flctxsafely everywhere. This requires asmp_load_acquire, as the pointer is set via cmpxchg (a release operation).
Looks good: