netfs, 9p: Fix race between umount and async request completion
Subsystems (6)
David Howells
maintainerPATCH v3 1/1 netfs, 9p: Fix race between umount and async request completion
netfs, 9p: Fix race between umount and async request completion
There's a problem in 9p's interaction with netfslib whereby a crash occurs because the 9p_fid structs get forcibly destroyed during client teardown (without paying attention to their refcounts) before netfslib has finished with them. However, it's not a simple case of deferring the clunking that p9_fid_put() does as that requires the p9_client record to still be present.
The problem is that netfslib has to unlock pages and clear the IN_PROGRESS flag before destroying the objects involved - including the fid - and, in any case, nothing checks to see if writeback completed barring looking at the page flags.
Fix this by keeping a count of outstanding I/O requests (of any type) and waiting for it to quiesce during inode eviction.
---
Notes:
Changes
=======
ver #3)
- Adjust commit message; no change to the diff.
ver #2)
- Wait for outstanding I/O before clobbering the pagecache.fs/9p/vfs_inode.c | 1 + fs/afs/inode.c | 1 + fs/netfs/objects.c | 5 +++++ fs/smb/client/cifsfs.c | 1 + include/linux/netfs.h | 18 ++++++++++++++++++ 5 files changed, 26 insertions(+)
fs/9p/vfs_inode.c +1 -0
@@ -349,6 +349,7 @@ void v9fs_evict_inode(struct inode *inode)__le32 __maybe_unused version;if (!is_bad_inode (inode)) { +netfs_wait_for_outstanding_io (inode);truncate_inode_pages_final (&inode->i_data); version =cpu_to_le32 (v9inode->qid.version);
fs/afs/inode.c +1 -0
@@ -648,6 +648,7 @@ void afs_evict_inode(struct inode *inode) ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode); +netfs_wait_for_outstanding_io (inode);truncate_inode_pages_final (&inode->i_data);afs_set_cache_aux (vnode, &aux);
fs/netfs/objects.c +5 -0
@@ -72,6 +72,7 @@ struct netfs_io_request *netfs_alloc_request(struct address_space *mapping, } } +atomic_inc (&ctx->io_count);trace_netfs_rreq_ref (rreq->debug_id,1 , netfs_rreq_trace_new);netfs_proc_add_rreq (rreq);netfs_stat (&netfs_n_rh_rreq);
@@ -124,6 +125,7 @@ static void netfs_free_request(struct work_struct *work) {struct netfs_io_request *rreq = container_of(work, struct netfs_io_request, work); +struct netfs_inode *ictx =netfs_inode (rreq->inode);unsigned int i;trace_netfs_rreq (rreq, netfs_rreq_trace_free);
@@ -142,6 +144,9 @@ static void netfs_free_request(struct work_struct *work) }kvfree (rreq->direct_bv); } + +if (atomic_dec_and_test (&ictx->io_count)) +wake_up_var (&ictx->io_count);call_rcu (&rreq->rcu, netfs_free_request_rcu); }
fs/smb/client/cifsfs.c +1 -0
@@ -431,6 +431,7 @@ cifs_free_inode(struct inode *inode)static void cifs_evict_inode (struct inode *inode) { +netfs_wait_for_outstanding_io (inode);truncate_inode_pages_final (&inode->i_data);if (inode->i_state &I_PINNING_NETFS_WB )cifs_fscache_unuse_inode_cookie (inode, true);
include/linux/netfs.h +18 -0
@@ -68,6 +68,7 @@ struct netfs_inode {loff_t remote_i_size;/* Size of the remote file */ loff_t zero_point;/* Size after which we assume there's no data * on the server */ +atomic_t io_count;/* Number of outstanding reqs */ unsigned long flags;#define NETFS_ICTX_ODIRECT 0/* The file has DIO in progress */ #define NETFS_ICTX_UNBUFFERED 1/* I/O should not use the pagecache */
@@ -474,6 +475,7 @@ static inline void netfs_inode_init(struct netfs_inode *ctx, ctx->remote_i_size =i_size_read (&ctx->inode); ctx->zero_point =LLONG_MAX ; ctx->flags =0 ; +atomic_set (&ctx->io_count,0 );#if IS_ENABLED (CONFIG_FSCACHE ) ctx->cache =NULL ;#endif
@@ -517,4 +519,20 @@ static inline struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx) #endif } +/** +* netfs_wait_for_outstanding_io - Wait for outstanding I/O to complete +* @ctx: The netfs inode to wait on +* +* Wait for outstanding I/O requests of any type to complete. This is intended +* to be called from inode eviction routines. This makes sure that any +* resources held by those requests are cleaned up before we let the inode get +* cleaned up. +*/ +static inline void netfs_wait_for_outstanding_io (struct inode *inode) +{ +struct netfs_inode *ictx =netfs_inode (inode); + +wait_var_event (&ictx->io_count,atomic_read (&ictx->io_count) ==0 ); +} +#endif /* _LINUX_NETFS_H */
Quoted 10 lines · David Howells
netfs, 9p: Fix race between umount and async request completion
There's a problem in 9p's interaction with netfslib whereby a crash occurs because the 9p_fid structs get forcibly destroyed during client teardown (without paying attention to their refcounts) before netfslib has finished with them. However, it's not a simple case of deferring the clunking that
p9_fid_put()does as that requires thep9_clientrecord to still be present.[...]
Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.fixes
[1/1] netfs, 9p: Fix race between umount and async request completion
https://git.kernel.org/vfs/vfs/c/e20fe12bfb0b