Skip to content

Commit 5ea9a7c

Browse files
neilbrownchucklever
authored andcommitted
nfsd: don't take fi_lock in nfsd_break_deleg_cb()
A recent change to check_for_locks() changed it to take ->flc_lock while holding ->fi_lock. This creates a lock inversion (reported by lockdep) because there is a case where ->fi_lock is taken while holding ->flc_lock. ->flc_lock is held across ->fl_lmops callbacks, and nfsd_break_deleg_cb() is one of those and does take ->fi_lock. However it doesn't need to. Prior to v4.17-rc1~110^2~22 ("nfsd: create a separate lease for each delegation") nfsd_break_deleg_cb() would walk the ->fi_delegations list and so needed the lock. Since then it doesn't walk the list and doesn't need the lock. Two actions are performed under the lock. One is to call nfsd_break_one_deleg which calls nfsd4_run_cb(). These doesn't act on the nfs4_file at all, so don't need the lock. The other is to set ->fi_had_conflict which is in the nfs4_file. This field is only ever set here (except when initialised to false) so there is no possible problem will multiple threads racing when setting it. The field is tested twice in nfs4_set_delegation(). The first test does not hold a lock and is documented as an opportunistic optimisation, so it doesn't impose any need to hold ->fi_lock while setting ->fi_had_conflict. The second test in nfs4_set_delegation() *is* make under ->fi_lock, so removing the locking when ->fi_had_conflict is set could make a change. The change could only be interesting if ->fi_had_conflict tested as false even though nfsd_break_one_deleg() ran before ->fi_lock was unlocked. i.e. while hash_delegation_locked() was running. As hash_delegation_lock() doesn't interact in any way with nfs4_run_cb() there can be no importance to this interaction. So this patch removes the locking from nfsd_break_one_deleg() and moves the final test on ->fi_had_conflict out of the locked region to make it clear that locking isn't important to the test. It is still tested *after* vfs_setlease() has succeeded. This might be significant and as vfs_setlease() takes ->flc_lock, and nfsd_break_one_deleg() is called under ->flc_lock this "after" is a true ordering provided by a spinlock. Fixes: edcf972 ("nfsd: fix RELEASE_LOCKOWNER") Signed-off-by: NeilBrown <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Chuck Lever <[email protected]>
1 parent edcf972 commit 5ea9a7c

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

fs/nfsd/nfs4state.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4945,10 +4945,8 @@ nfsd_break_deleg_cb(struct file_lock *fl)
49454945
*/
49464946
fl->fl_break_time = 0;
49474947

4948-
spin_lock(&fp->fi_lock);
49494948
fp->fi_had_conflict = true;
49504949
nfsd_break_one_deleg(dp);
4951-
spin_unlock(&fp->fi_lock);
49524950
return false;
49534951
}
49544952

@@ -5557,12 +5555,13 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
55575555
if (status)
55585556
goto out_unlock;
55595557

5558+
status = -EAGAIN;
5559+
if (fp->fi_had_conflict)
5560+
goto out_unlock;
5561+
55605562
spin_lock(&state_lock);
55615563
spin_lock(&fp->fi_lock);
5562-
if (fp->fi_had_conflict)
5563-
status = -EAGAIN;
5564-
else
5565-
status = hash_delegation_locked(dp, fp);
5564+
status = hash_delegation_locked(dp, fp);
55665565
spin_unlock(&fp->fi_lock);
55675566
spin_unlock(&state_lock);
55685567

0 commit comments

Comments
 (0)