NFS: keep final releases out of writeback
Subsystems (2)
v3 cover NFS: keep final releases out of writeback
v2: https://lore.kernel.org/linux-nfs/[email protected]/
v1: https://lore.kernel.org/linux-nfs/[email protected]/
Releasing an open context closes NFSv4 state and drops what may be the last reference to the dentry, the inode and the superblock. The flusher holds sb->s_umount and has set I_SYNC on the inode, so it must not do either final release. It would wait for itself, on s_umount in deactivate_super() or on I_SYNC in evict(). A write normally avoids this because the RPC release callback of a WRITE or COMMIT runs on nfsiod. This series fixes the four ways in which writeback can still run such a release itself:
Patch 1: nfs_write_error() completes a write that fails before the client sends it, and it does that in the submitter. A client hit this deadlock in the field when its server began to return AUTH_TOOWEAK.
Patch 2: nfs_initiate_pgio() and nfs_initiate_commit() drop their task reference with rpc_put_task(). If the task has already completed, that call runs the release callback in the submitter. This is the deadlock that NeilBrown reported in 2014:
https://lore.kernel.org/linux-nfs/[email protected]/
Patch 3: the asynchronous LAYOUTCOMMIT that ->write_inode() sends has the same problem as patch 2. It pins an inode and the superblock instead of an open context, and its task has no workqueue.
Patch 4: when LOCALIO cannot allocate its fsync context for a COMMIT, nfs_local_commit() runs the release of the COMMIT in the submitter.
All four move the release to nfsiod, where every other write already does it. None changes how or when a filesystem shuts down, and nfs_sb_deactive() does not change.
Why not take another reference instead? These paths take and drop every reference in balance:
- Each write request holds its open context. So a closed file keeps its context until the client writes or fails its requests.
- Since commit 3d4ff43d895c ("nfs_open_context doesn't need struct path either"), an open context holds the superblock active. After umount, the last open context can hold the last active reference.
- The flusher holds s_umount and I_SYNC, but no reference of its own. Commit 169ebd90131b ("writeback: Avoid iput() from flusher thread") removed its inode reference on purpose.
A missing reference would show as an early release: a use-after-free or a refcount warning. Here the opposite happens: a thread waits for locks that it holds itself, because it drops the last reference. Another reference only changes which put is the last one. The flusher cannot drop a reference after it releases s_umount and I_SYNC, so the last put must run in another thread. For writes that thread is already nfsiod, and commit bf294b41cefc ("SUNRPC: Close a race in __rpc_wait_for_completion_task()") added rpc_put_task_async() for this case. The series moves the four exceptions to that rule. It does not add a new way to defer a release.
Testing: NFSv3 over loopback, with a test-only knob that makes nfs_do_writepage() see -EACCES in the pageio descriptor. Dirty a file through a mapping after closing it, unmount, and let the periodic flusher write it back. A plain umount is enough to leave the write requests as the only thing that keeps the superblock alive.
plain file sillyrenamed file
v1 shuts down, from flusher hangs in evict(),
v1's work item waiting for I_SYNC this series shuts down, from shuts down, from the release
the context's of the sillyrename REMOVE
work item
Patch 2 has a test of its own, with a second test-only knob. The knob leaves the COMMIT to nfs_write_inode() in the flusher. There it waits for the COMMIT to complete before nfs_initiate_commit() puts the task. Without patch 2, the flusher hangs in deactivate_super(), with the stack in the commit message of patch 2. With patch 2, the release runs on nfsiod and the filesystem shuts down in 2 s.
Patches 3 and 4 close a race and a rare error path, and neither test exercises them. Each patch builds with W=1 on Anna's linux-next.
Changes since v2:
- New patch 4, for the LOCALIO COMMIT release on an allocation failure.
- The cover letter now explains why the release is deferred rather than another reference taken. Its subject also covers patch 3.
- Patch 1: put_nfs_open_context_async() is declared without extern.
- Patch 2 now has a test that reproduces its deadlock, see Testing.
- Rebased on Anna's linux-next. v2's base-commit was not public.
Mike Snitzer (4):
NFS: don't release the open context of a failed write from writeback
NFS: don't run the release of a WRITE or COMMIT in the submitter NFSv4/pnfs: don't run the release of a LAYOUTCOMMIT in the submitter NFS/localio: don't run the release of a failed COMMIT in the submitter
fs/nfs/inode.c | 37 ++++++++++++++++++++++++++++++++++--- fs/nfs/internal.h | 1 + fs/nfs/localio.c | 28 +++++++++++++++++++++++++++- fs/nfs/nfs4proc.c | 7 ++++++- fs/nfs/pagelist.c | 3 ++- fs/nfs/write.c | 8 +++++++- include/linux/nfs_fs.h | 2 ++ 7 files changed, 79 insertions(+), 7 deletions(-)
base-commit: ef30b3efb7e8bae94d790a6503a595e1051323e7
Signature · 1 line
2.52.0
PATCH v3 1/4 NFS: don't release the open context of a failed write from writeback
When a write fails with an error that nfs_error_is_fatal_on_server() accepts, nfs_do_writepage() and nfs_async_write_error() complete the request in the submitting thread: nfs_write_error() removes the request from the inode and drops the last reference to it. The request owns a reference to its open context, so if the file has already been closed this is also where the open context is released.
Releasing an open context closes NFSv4 state and drops what may be the last reference to the dentry, the inode and the superblock. The submitting thread is usually the flusher, which may do none of that:
- It holds sb->s_umount shared, or works on behalf of a sync(2) caller that does. An open context does not keep a filesystem mounted, so once it has been unmounted the open context can hold the last active reference to the superblock, and deactivate_super() then waits for s_umount forever:
wb_workfn
__writeback_inodes_wb
super_trylock_shared(sb) <- s_umount held shared
writeback_sb_inodes
nfs_writepages
nfs_do_writepage <- fatal error: out_launder
nfs_write_error
nfs_release_request
nfs_put_lock_context
__put_nfs_open_context
nfs_sb_deactive
deactivate_super
down_write(&sb->s_umount) <- deadlock
- Since commit 169ebd90131b ("writeback: Avoid iput() from flusher thread") it pins the inode with I_SYNC instead of a reference, so that it never does the final iput(). evict() waits for I_SYNC, so if dput() drops the last reference to a stale or sillyrenamed inode, the flusher waits for itself:
wb_workfn
__writeback_inodes_wb
writeback_sb_inodes <- sets I_SYNC
nfs_writepages
nfs_do_writepage
nfs_write_error
nfs_release_request
nfs_put_lock_context
__put_nfs_open_context
dput
nfs_dentry_iput
iput
evict
inode_wait_for_writeback <- deadlock
The first of these was hit on a 6.12-based client whose server began rejecting requests with AUTH_TOOWEAK (-EACCES). The flush worker hung in deactivate_super(), and every later automount of the same filesystem blocked in grab_super() behind it. The second was reproduced by failing the writeback of a sillyrenamed file that had been closed.
A write that was sent is normally released by the RPC release callback, which runs on nfsiod; the next patch closes the case where it is not. Do the same for a write that fails before it is sent. Have nfs_write_error() hold the open context across the release of the request, and drop that reference with put_nfs_open_context_async(), which hands the release of the context to nfsiod if the reference is the last one.
This does not change when or how a filesystem is shut down. Since commit 3d4ff43d895c ("nfs_open_context doesn't need struct path either") an open context pins the superblock instead of a mount, so an open context that outlives the last mount shuts the filesystem down when it is released. The completion of a write already does that from nfsiod.
--- fs/nfs/inode.c | 37 ++++++++++++++++++++++++++++++++++--- fs/nfs/internal.h | 1 + fs/nfs/write.c | 5 +++++ include/linux/nfs_fs.h | 2 ++ 4 files changed, 42 insertions(+), 3 deletions(-)
fs/nfs/inode.c +34 -3
@@ -1237,13 +1237,11 @@ struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx) }EXPORT_SYMBOL_GPL (get_nfs_open_context); -static void __put_nfs_open_context (struct nfs_open_context *ctx,int is_sync) +static void nfs_free_open_context (struct nfs_open_context *ctx,int is_sync) {struct inode *inode =d_inode (ctx->dentry);struct super_block *sb = ctx->dentry->d_sb; -if (!refcount_dec_and_test (&ctx->lock_context.count)) -return ;if (!list_empty (&ctx->list)) {spin_lock (&inode->i_lock);list_del_rcu (&ctx->list);
@@ -1260,12 +1258,45 @@ static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)kfree_rcu (ctx, rcu_head); } +static void __put_nfs_open_context (struct nfs_open_context *ctx,int is_sync) +{ +if (refcount_dec_and_test (&ctx->lock_context.count)) +nfs_free_open_context (ctx, is_sync); +} +void put_nfs_open_context (struct nfs_open_context *ctx) {__put_nfs_open_context (ctx,0 ); }EXPORT_SYMBOL_GPL (put_nfs_open_context); +static void nfs_free_open_context_work (struct work_struct *work) +{ +struct nfs_open_context *ctx = + container_of(work, struct nfs_open_context, free_work); + +nfs_free_open_context (ctx,0 ); +} + +/** +* put_nfs_open_context_async - drop a reference from a caller that must not +* release the open context itself +* @ctx: open context to drop +* +* Releasing an open context closes NFSv4 state and drops what may be the +* last reference to the dentry, the inode and the superblock. None of that +* is allowed in writeback, which holds sb->s_umount and has set I_SYNC on +* the inode. If this is the last reference, release the context from nfsiod, +* which is where the completion of a write releases it. +*/ +void put_nfs_open_context_async (struct nfs_open_context *ctx) +{ +if (!refcount_dec_and_test (&ctx->lock_context.count)) +return ; +INIT_WORK (&ctx->free_work, nfs_free_open_context_work); +queue_work (nfsiod_workqueue, &ctx->free_work); +} +static void put_nfs_open_context_sync (struct nfs_open_context *ctx) {__put_nfs_open_context (ctx,1 );
fs/nfs/internal.h +1 -0
@@ -527,6 +527,7 @@ extern int __init register_nfs_fs(void); extern void __exit unregister_nfs_fs(void);extern bool nfs_sb_active (struct super_block *sb);extern void nfs_sb_deactive (struct super_block *sb); +void put_nfs_open_context_async (struct nfs_open_context *ctx);extern int nfs_client_for_each_server (struct nfs_client *clp,int (*fn)(struct nfs_server *,void *),void *data);
fs/nfs/write.c +5 -0
@@ -568,11 +568,16 @@ static struct nfs_page *nfs_lock_and_join_requests(struct folio *folio)static void nfs_write_error (struct nfs_page *req,int error) { +struct nfs_open_context *ctx; + +/* The caller may be writeback, which must not release the context */ + ctx =get_nfs_open_context (nfs_req_openctx (req));trace_nfs_write_error (nfs_page_to_inode (req), req, error);nfs_mapping_set_error (nfs_page_to_folio (req), error);nfs_inode_remove_request (req);nfs_page_end_writeback (req);nfs_release_request (req); +put_nfs_open_context_async (ctx); }/*
include/linux/nfs_fs.h +2 -0
@@ -26,6 +26,7 @@#include <linux/refcount.h> #include <linux/rwsem.h> #include <linux/wait.h> +#include <linux/workqueue.h> #include <linux/sunrpc/debug.h> #include <linux/sunrpc/auth.h>
@@ -115,6 +116,7 @@ struct nfs_open_context {struct nfs4_threshold *mdsthreshold;struct list_head list;struct rcu_head rcu_head; +struct work_struct free_work;struct nfs_file_localio nfl; };
Signature · 1 line
2.52.0
PATCH v3 2/4 NFS: don't run the release of a WRITE or COMMIT in the submitter
In reply to Mike Snitzernfs_initiate_pgio() and nfs_initiate_commit() start an asynchronous RPC task and then drop their reference to it with rpc_put_task(). If the task has already completed, that reference is the last one and rpc_put_task() runs the rpc_release() callback in the submitter rather than on nfsiod, where task_setup_data.workqueue asks for it to run.
The release of a WRITE or COMMIT releases write requests and, with them, what may be the last reference to an open context. The submitter is usually the flusher, which holds sb->s_umount and has set I_SYNC on the inode, so it can release neither the superblock nor the inode:
wb_workfn
wb_writeback
writeback_sb_inodes
__writeback_single_inode
nfs_write_inode
__nfs_commit_inode
nfs_generic_commit_list
nfs_commit_list
nfs_initiate_commit
rpc_put_task
rpc_free_task
nfs_commit_release
nfs_commitdata_release
put_nfs_open_context
nfs_sb_deactive
deactivate_super <- blocks on s_umount
This is the deadlock that was reported in 2014, see the link below.
Before commit bf294b41cefc ("SUNRPC: Close a race in __rpc_wait_for_completion_task()") the final rpc_put_task() always handed the release to the workqueue of the task. That commit made rpc_put_task() run the release in the caller, and added rpc_put_task_async() for callers that must not. Use it.
nfs_initiate_pgio() also starts READs, whose release now always runs on nfsiod as well.
Nothing depends on the release of a COMMIT having run by the time nfs_initiate_commit() returns: no caller has passed it FLUSH_SYNC since commit 64a93dbf25d3 ("NFS: Fix deadlocks in nfs_scan_commit_list()"). Before that commit nfs_write_inode() did, from the flusher, and the wait then made the reference of the submitter the last one every time.
--- fs/nfs/pagelist.c | 3 ++- fs/nfs/write.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-)
fs/nfs/pagelist.c +2 -1
@@ -816,7 +816,8 @@ int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr, task =rpc_run_task (&task_setup_data);if (IS_ERR (task))return PTR_ERR (task); -rpc_put_task (task); +/* The caller may be writeback: don't run rpc_release() here */ +rpc_put_task_async (task);return 0 ; }EXPORT_SYMBOL_GPL (nfs_initiate_pgio);
fs/nfs/write.c +2 -1
@@ -1677,7 +1677,8 @@ int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,return PTR_ERR (task);if (how &FLUSH_SYNC )rpc_wait_for_completion_task (task); -rpc_put_task (task); +/* The caller may be writeback: don't run rpc_release() here */ +rpc_put_task_async (task);return 0 ; }EXPORT_SYMBOL_GPL (nfs_initiate_commit);
Signature · 1 line
2.52.0
PATCH v3 3/4 NFSv4/pnfs: don't run the release of a LAYOUTCOMMIT in the submitter
In reply to Mike SnitzerSince commit 472e25944981 ("NFSv4.1: Pin the inode and super block in asynchronous layoutcommit") nfs4_proc_layoutcommit() pins the inode and the superblock for an asynchronous LAYOUTCOMMIT with nfs_igrab_and_active(), and nfs4_layoutcommit_release() drops both with nfs_iput_and_deactive().
The task is put with rpc_put_task(), so if it has already completed, the release runs in the submitter. nfs4_write_inode() sends an asynchronous LAYOUTCOMMIT from writeback, where the submitter is the flusher. It holds sb->s_umount and has set I_SYNC on the inode, so it can do neither the final iput() nor the final deactivate_super(). If the task has not completed, the release runs on rpciod, because the task has no workqueue.
Give the asynchronous task nfsiod as its workqueue and put it with rpc_put_task_async(), as is done for WRITE and COMMIT. None of the callers that send an asynchronous LAYOUTCOMMIT waits for its release.
A synchronous LAYOUTCOMMIT is left as it is: it pins nothing, so its release has nothing of the kind to drop.
--- fs/nfs/nfs4proc.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
fs/nfs/nfs4proc.c +6 -1
@@ -10152,6 +10152,7 @@ nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)return -EAGAIN ; } task_setup_data.flags =RPC_TASK_ASYNC ; + task_setup_data.workqueue = nfsiod_workqueue; }nfs4_init_sequence (NFS_SERVER (data->args.inode)->nfs_client, &data->args.seq_args, &data->res.seq_res,1 ,0 );
@@ -10162,7 +10163,11 @@ nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync) status = task->tk_status;trace_nfs4_layoutcommit (data->args.inode, &data->args.stateid, status);dprintk ("%s: status %d\n" , __func__, status); -rpc_put_task (task); +/* An asynchronous caller may be writeback: don't run rpc_release() here */ +if (sync) +rpc_put_task (task); +else +rpc_put_task_async (task);return status; }
Signature · 1 line
2.52.0
PATCH v3 4/4 NFS/localio: don't run the release of a failed COMMIT in the submitter
In reply to Mike Snitzernfs_local_commit() allocates its fsync context with GFP_NOIO. If the allocation fails, it runs rpc_call_done() and rpc_release() for the COMMIT in the submitter. The release of a COMMIT releases its write requests and, with them, what may be the last reference to an open context. The submitter can be the flusher, through nfs_write_inode(). The flusher holds sb->s_umount and has set I_SYNC on the inode, so it can release neither the superblock nor the inode. This is the deadlock of the previous patches, through LOCALIO.
Run the release on nfsiod instead, as the release of an RPC COMMIT runs. The failure path cannot allocate a work item. The commit's task is not an RPC task for LOCALIO, so its work item is free. The nfsd_file is still put in the submitter, because it holds no open context.
--- fs/nfs/localio.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
fs/nfs/localio.c +27 -1
@@ -1094,6 +1094,32 @@ nfs_local_release_commit_data(struct nfsd_file *localio, call_ops->rpc_release (data); } +static void nfs_local_commit_release_work (struct work_struct *work) +{ +struct nfs_commit_data *data = + container_of(work, struct nfs_commit_data, task.u.tk_work); + + data->task.tk_ops->rpc_call_done (&data->task, data); + data->task.tk_ops->rpc_release (data); +} + +/* +* The release of a COMMIT releases its write requests and, with them, what +* may be the last reference to an open context. The caller may be +* writeback, which must not release the context itself, so release from +* nfsiod, as the release of an RPC COMMIT is. The commit's task is not an +* RPC task here, so its work item is free. +*/ +static void nfs_local_commit_release_async (struct nfsd_file *localio, +struct nfs_commit_data *data, +const struct rpc_call_ops *call_ops) +{ +nfs_local_file_put (localio); + data->task.tk_ops = call_ops; +INIT_WORK (&data->task.u.tk_work, nfs_local_commit_release_work); +queue_work (nfsiod_workqueue, &data->task.u.tk_work); +} +static void nfs_local_fsync_ctx_free (struct nfs_local_fsync_ctx *ctx) {
@@ -1144,7 +1170,7 @@ int nfs_local_commit(struct nfsd_file *localio, ctx =nfs_local_fsync_ctx_alloc (data, localio,GFP_NOIO );if (!ctx) {nfs_local_commit_done (data, -ENOMEM ); -nfs_local_release_commit_data (localio, data, call_ops); +nfs_local_commit_release_async (localio, data, call_ops);return -ENOMEM ; }
Signature · 1 line
2.52.0