NFS: pin the superblock across migration recovery
Subsystems (3)
PATCH v2 1/1 NFS: pin the superblock across migration recovery
The NFSv4 state manager walks clp->cl_superblocks under RCU in nfs4_handle_migration() and nfs4_handle_lease_moved(), then drops the lock and calls nfs4_try_migration() without holding a reference on the nfs_server or its superblock. A concurrent umount can therefore run nfs_kill_super() -> nfs_free_server() while migration recovery is still inside nfs4_update_server(): the server is removed from cl_superblocks a second time (DEBUG_LIST BUG), and once freed by call_rcu() the migration path still writes server->mig_status and touches server->super->s_root.
Pin the superblock with nfs_sb_active() before dropping RCU, as nfs_client_for_each_server() already does, and drop it again with nfs_sb_deactive() on every path: umount then detaches the superblock but cannot reach nfs_kill_super()/nfs_free_server() until migration recovery has finished and restored the server's list membership. nfs4_handle_migration() releases the pin after nfs4_try_migration(); nfs4_handle_lease_moved() releases it on both restart paths and before its final return.
Umount may now wait for the migration attempt to finish, bounded by the mount's RPC timeouts, the same trade-off as the other nfs_sb_active() users.
---
v2: add Link, Fixes and stable tags, and remove local validation details from the commit message.
fs/nfs/nfs4state.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
fs/nfs/nfs4state.c +13 -2
@@ -2169,8 +2169,12 @@ restart: &server->mig_status))continue ; +if (!server->super || !nfs_sb_active (server->super)) +continue ; +rcu_read_unlock (); status =nfs4_try_migration (server, cred); +nfs_sb_deactive (server->super);if (status <0 ) {put_cred (cred);return status;
@@ -2212,13 +2216,20 @@ restart:continue ; server->mig_gen = clp->cl_mig_gen; +if (!server->super || !nfs_sb_active (server->super)) +continue ; +rcu_read_unlock (); inode =d_inode (server->super->s_root); status =nfs4_proc_fsid_present (inode, cred); -if (status != -NFS4ERR_MOVED ) +if (status != -NFS4ERR_MOVED ) { +nfs_sb_deactive (server->super); goto restart;/* wasn't this one */ -if (nfs4_try_migration (server, cred) == -NFS4ERR_LEASE_MOVED ) + } + status =nfs4_try_migration (server, cred); +nfs_sb_deactive (server->super); +if (status == -NFS4ERR_LEASE_MOVED ) goto restart;/* there are more */ goto out; }
Quoted 1 line · Cheng Yongkang
Umount may now wait for the migration attempt to finish, bounded by
the
Quoted 2 lines
mount's RPC timeouts, the same trade-off as the other
nfs_sb_active()users.
Is this description accurate? While the pin is held, umount(2) returns as soon as its deactivate_super() call has decremented s_active:
fs/super.c:deactivate_super() {
if (!atomic_add_unless(&s->s_active, -1, 1)) {
__super_lock_excl(s);
deactivate_locked_super(s);
}
}
The wait goes the other way. The last nfs_sb_deactive() in the state manager runs the unmount teardown. There are questions about that below.
[ ... ]
Quoted 1 line
Fixes c9fdeb280b8c ("NFS: Add basic migration support to state
manager thread")
This isn't a bug, but nfs4_handle_lease_moved() was added by commit b7f7a66e420a ("NFS: Support NFS4ERR_LEASE_MOVED recovery in state manager"). Should that commit get a Fixes: tag too?
[ ... ]
Quoted 5 lines
fs/nfs/nfs4state.c +0
-0
fs/nfs/nfs4state.c restart: · line 2177
Quoted 9 lines
fs/nfs/nfs4state.c +4
-0@@ -2169,8 +2169,12 @@ restart: &server->mig_status))continue ; +if (!server->super || !nfs_sb_active (server->super)) +continue ; +rcu_read_unlock (); status =nfs4_try_migration (server, cred); +nfs_sb_deactive (server->super);
When this drops the last reference, deactivate_super() runs nfs_kill_super() and generic_shutdown_super() here, on the state manager
thread.
NFS4CLNT_MANAGER_RUNNING is still set at that point. Once nfs4_try_migration() has called nfs4_begin_drain_session(), NFS4_SLOT_TBL_DRAINING is set too. Only the nfs4_end_drain_session() call
at the bottom of the nfs4_state_manager() loop clears it.
Can this deadlock against sync(2) on a pNFS mount? A pending LAYOUTCOMMIT
does not hold an active superblock reference, because pnfs_set_layoutcommit() takes only an lseg reference.
While a write delegation is held, nfs4_file_flush() may only start writeback. A close that sends no CLOSE, for example one for an open satisfied from the delegation, never reaches nfs4_do_close() and pnfs_roc(). If sync(2) reaches such an inode while the migration attempt
has the table drained:
sync(2)
iterate_supers()
super_lock(sb, false) <- s_umount held shared
sync_inodes_sb()
nfs4_write_inode() <- flusher, WB_SYNC_ALL
pnfs_layoutcommit_inode(inode, true)
nfs4_proc_layoutcommit(data, true) <- not privileged
nfs4_layoutcommit_prepare()
nfs4_setup_sequence()
rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL)
state manager
nfs4_handle_migration()
nfs4_try_migration()
nfs4_begin_drain_session()
nfs_sb_deactive() <- umount already returned
deactivate_super()
__super_lock_excl() <- waits for sync(2)
Only nfs4_end_drain_slot_table() can wake the LAYOUTCOMMIT, and the state
manager never gets back to it.
Without pNFS there does not seem to be a deadlock. Dirty and unstable pages keep an open context, and with it an nfs_sb_active() reference, until their WRITE or COMMIT completes. The DELEGRETURN and LAYOUTRETURN sent from nfs4_evict_inode() become privileged once the inode is being freed.
The teardown still runs inside the drain, though. evict_inodes() sends a
synchronous DELEGRETURN for each delegation. Until it finishes, every non-privileged RPC for every mount that shares this nfs_client waits on slot_tbl_waitq. Before this patch the umount thread did that work, and the state manager went on to lift the drain.
nfs_reap_expired_delegations() and nfs4_do_reclaim() already drop superblock references inside a drain in the same way, so this may need a
common fix. Could the reference be held until after nfs4_end_drain_session() and nfs4_clear_state_manager_bit()? That is where nfs_client_return_marked_delegations() already runs.
Quoted 4 lines
fs/nfs/nfs4state.c +0
-0if (status <0 ) {put_cred (cred);return status;@@ -2212,13 +2216,20 @@ restart:
[ ... ]
Quoted 11 lines
inode =
d_inode(server->super->s_root);
status =nfs4_proc_fsid_present(inode, cred);
- if (status != -NFS4ERR_MOVED)
+ if (status != -NFS4ERR_MOVED) {
+nfs_sb_deactive(server->super);
goto restart; /* wasn't this one */
- if (nfs4_try_migration(server, cred)== -NFS4ERR_LEASE_MOVED)
+ }
+ status =nfs4_try_migration(server, cred);
+nfs_sb_deactive(server->super);
+ if (status == -NFS4ERR_LEASE_MOVED)
The same question applies to both nfs_sb_deactive() calls added to nfs4_handle_lease_moved(). After a restart for -NFS4ERR_LEASE_MOVED, the
"wasn't this one" release also runs inside the drain left by the previous nfs4_try_migration().
[ ... ]
I've dropped this from my testing branch for now.
Quoted 26 lines · Cheng Yongkang
The NFSv4 state manager walks
clp->cl_superblocksunder RCU innfs4_handle_migration()andnfs4_handle_lease_moved(), then drops the lock and callsnfs4_try_migration()without holding a reference on thenfs_serveror its superblock. A concurrent umount can therefore runnfs_kill_super()->nfs_free_server()while migration recovery is still
insidenfs4_update_server(): the server is removed fromcl_superblocksa
second time (DEBUG_LISTBUG), and once freed bycall_rcu()the migration
path still writesserver->mig_statusand touches server->super-Quoted 1 line
s_root.Pin the superblock with
nfs_sb_active()before dropping RCU, asnfs_client_for_each_server()already does, and drop it again withnfs_sb_deactive()on every path: umount then detaches the superblock but
cannot reach nfs_kill_super()/nfs_free_server() until migration recovery
has finished and restored the server's list membership.nfs4_handle_migration()releases the pin afternfs4_try_migration();nfs4_handle_lease_moved()releases it on both restart paths and before
its final return.
Quoted 7 lines
Umount may now wait for the migration attempt to finish, bounded by the
mount's RPC timeouts, the same trade-off as the othernfs_sb_active()users.Link:
https://lore.kernel.org/all/[email protected]/
Quoted 54 lines
Fixes c9fdeb280b8c ("NFS: Add basic migration support to statemanager thread")
---
v2: add Link, Fixes and stable tags, and remove local validation details from the
commit message.fs/nfs/nfs4state.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)fs/nfs/nfs4state.c +9
-1@@ -2169,8 +2169,12 @@ restart: &server->mig_status)) continue; + if (!server->super || !nfs_sb_active(server->super)) + continue; + rcu_read_unlock(); status = nfs4_try_migration(server, cred); + nfs_sb_deactive(server->super); if (status < 0) { put_cred(cred); return status;@@ -2212,13 +2216,20 @@ restart: continue; server->mig_gen = clp->cl_mig_gen; + if (!server->super || !nfs_sb_active(server->super)) + continue; + rcu_read_unlock(); inode = d_inode(server->super->s_root); status = nfs4_proc_fsid_present(inode, cred); - if (status != -NFS4ERR_MOVED) + if (status != -NFS4ERR_MOVED) { + nfs_sb_deactive(server->super);goto restart; /* wasn't this one */ - if (
nfs4_try_migration(server, cred)== -NFS4ERR_LEASE_MOVED)
+ }
+ status =nfs4_try_migration(server, cred); + nfs_sb_deactive(server->super);
+ if (status == -NFS4ERR_LEASE_MOVED) goto restart; /* there are more */ goto out;
}