NFSD: restart ssc_expire_umount walk after dropping nfsd_ssc_lock
Subsystems (3)
PATCH v2 1/1 NFSD: restart ssc_expire_umount walk after dropping nfsd_ssc_lock
nfsd4_ssc_expire_umount() walks nn->nfsd_ssc_mount_list with list_for_each_entry_safe(ni, tmp, ...). For each expired entry it sets nsui_busy = true, drops nfsd_ssc_lock to run mntput() on the source vfsmount, then reacquires the lock to list_del + kfree the entry and continue iterating via the macro's saved tmp pointer.
The nsui_busy flag protects the current ni from concurrent nfsd4_ssc_setup_dul() finders during the lock-drop window, but it does not pin tmp. Another nfsd RPC thread that fails its source- server mount and reaches nfsd4_ssc_cancel_dul() will, during that same window, take nfsd_ssc_lock, list_del + kfree its own ssc_umount item, and release the lock. If that item is the saved tmp of the expire walk, the next iteration dereferences a freed nfsd4_ssc_umount_item.
Restart the walk from the head after the mntput() unlock window so no saved next pointer survives the lock-drop. The list is bounded by the number of active inter-server source mounts (typically small) and the expire delayed-work runs periodically rather than per-IO, so the restart is cheap.
--- fs/nfsd/nfs4state.c | 44 +++++++++++++++++++++++++-------------------
Changes since v1:
- Rewrote the in-code comment above goto-restart to describe the current code (concurrent cancel_dul can free any list item while the lock is dropped) rather than referencing the removed list_for_each_entry_safe() iterator. (Chuck Lever.)
1 file changed, 25 insertions(+), 19 deletions(-)
fs/nfsd/nfs4state.c +25 -19
@@ -6728,30 +6728,36 @@ static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)static void nfsd4_ssc_expire_umount (struct nfsd_net *nn) {bool do_wakeup = false; -struct nfsd4_ssc_umount_item *ni =NULL ; -struct nfsd4_ssc_umount_item *tmp; +struct nfsd4_ssc_umount_item *ni; +restart:spin_lock (&nn->nfsd_ssc_lock); -list_for_each_entry_safe (ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) { -if (time_after (jiffies, ni->nsui_expire)) { -if (refcount_read (&ni->nsui_refcnt) >1 ) -continue ; +list_for_each_entry (ni, &nn->nfsd_ssc_mount_list, nsui_list) { +if (!time_after (jiffies, ni->nsui_expire)) +break ; +if (refcount_read (&ni->nsui_refcnt) >1 ) +continue ; -/* mark being unmount */ - ni->nsui_busy = true; -spin_unlock (&nn->nfsd_ssc_lock); -mntput (ni->nsui_vfsmount); -spin_lock (&nn->nfsd_ssc_lock); +/* mark being unmount */ + ni->nsui_busy = true; +spin_unlock (&nn->nfsd_ssc_lock); +mntput (ni->nsui_vfsmount); +spin_lock (&nn->nfsd_ssc_lock); -/* waiters need to start from begin of list */ -list_del (&ni->nsui_list); -kfree (ni); +/* waiters need to start from begin of list */ +list_del (&ni->nsui_list); +kfree (ni); -/* wakeup ssc_connect waiters */ - do_wakeup = true; -continue ; - } -break ; +/* wakeup ssc_connect waiters */ + do_wakeup = true; +/* +* Concurrent nfsd4_ssc_cancel_dul() can free any item +* on the list under nfsd_ssc_lock while mntput() runs +* above. Restart from the head; the list is short and +* the expire worker is periodic, so this is cheap. +*/ +spin_unlock (&nn->nfsd_ssc_lock); + goto restart; }if (do_wakeup)wake_up_all (&nn->nfsd_ssc_waitq);
Signature · 1 line
2.53.0
Chuck Lever
maintainerFrom: Chuck Lever <[email protected]>
Quoted 15 lines · Michael Bommarito
nfsd4_ssc_expire_umount()walksnn->nfsd_ssc_mount_listwithlist_for_each_entry_safe(ni, tmp, ...). For each expired entry it setsnsui_busy= true, dropsnfsd_ssc_lockto runmntput()on the source vfsmount, then reacquires the lock tolist_del+ kfree the entry and continue iterating via the macro's saved tmp pointer.The
nsui_busyflag protects the current ni from concurrentnfsd4_ssc_setup_dul()finders during the lock-drop window, but it does not pin tmp. Another nfsd RPC thread that fails its source- server mount and reachesnfsd4_ssc_cancel_dul()will, during that same window, takenfsd_ssc_lock,list_del+ kfree its ownssc_umountitem, and release the lock. If that item is the saved tmp of the expire walk, the next iteration dereferences a freednfsd4_ssc_umount_item.
Quoted 1 line
[...]
Applied to nfsd-testing, thanks!
[1/1] NFSD: restart ssc_expire_umount walk after dropping nfsd_ssc_lock
commit: e6dc1fc7e8483f9b36dcfdbcd33665298b018ba9
--
Chuck Lever <[email protected]>