Skip to content

Commit 21fb440

Browse files
neilbrownAnna Schumaker
authored andcommitted
nfs_localio: protect race between nfs_uuid_put() and nfs_close_local_fh()
nfs_uuid_put() and nfs_close_local_fh() can race if a "struct nfs_file_localio" is released at the same time that nfsd calls nfs_localio_invalidate_clients(). It is important that neither of these functions completes after the other has started looking at a given nfs_file_localio and before it finishes. If nfs_uuid_put() exits while nfs_close_local_fh() is closing ro_file and rw_file it could return to __nfd_file_cache_purge() while some files are still referenced so the purge may not succeed. If nfs_close_local_fh() exits while nfsd_uuid_put() is still closing the files then the "struct nfs_file_localio" could be freed while nfsd_uuid_put() is still looking at it. This side is currently handled by copying the pointers out of ro_file and rw_file before deleting from the list in nfsd_uuid. We need to preserve this while ensuring that nfsd_uuid_put() does wait for nfs_close_local_fh(). This patch use nfl->uuid and nfl->list to provide the required interlock. nfs_uuid_put() removes the nfs_file_localio from the list, then drops locks and puts the two files, then reclaims the spinlock and sets ->nfs_uuid to NULL. nfs_close_local_fh() operates in the reverse order, setting ->nfs_uuid to NULL, then closing the files, then unlinking from the list. If nfs_uuid_put() finds that ->nfs_uuid is already NULL, it waits for the nfs_file_localio to be removed from the list. If nfs_close_local_fh() find that it has already been unlinked it waits for ->nfs_uuid to become NULL. This ensure that one of the two tries to close the files, but they each waits for the other. As nfs_uuid_put() is making the list empty, change from a list_for_each_safe loop to a while that always takes the first entry. This makes the intent more clear. Also don't move the list to a temporary local list as this would defeat the guarantees required for the interlock. Fixes: 86e0041 ("nfs: cache all open LOCALIO nfsd_file(s) in client") Signed-off-by: NeilBrown <neil@brown.name> Signed-off-by: Anna Schumaker <anna.schumaker@oracle.com>
1 parent 74fc55a commit 21fb440

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

fs/nfs_common/nfslocalio.c

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ EXPORT_SYMBOL_GPL(nfs_localio_enable_client);
151151
*/
152152
static bool nfs_uuid_put(nfs_uuid_t *nfs_uuid)
153153
{
154-
LIST_HEAD(local_files);
155-
struct nfs_file_localio *nfl, *tmp;
154+
struct nfs_file_localio *nfl;
156155

157156
spin_lock(&nfs_uuid->lock);
158157
if (unlikely(!rcu_access_pointer(nfs_uuid->net))) {
@@ -166,37 +165,49 @@ static bool nfs_uuid_put(nfs_uuid_t *nfs_uuid)
166165
nfs_uuid->dom = NULL;
167166
}
168167

169-
list_splice_init(&nfs_uuid->files, &local_files);
170-
spin_unlock(&nfs_uuid->lock);
171-
172168
/* Walk list of files and ensure their last references dropped */
173-
list_for_each_entry_safe(nfl, tmp, &local_files, list) {
169+
170+
while ((nfl = list_first_entry_or_null(&nfs_uuid->files,
171+
struct nfs_file_localio,
172+
list)) != NULL) {
174173
struct nfsd_file *ro_nf;
175174
struct nfsd_file *rw_nf;
176175

176+
/* If nfs_uuid is already NULL, nfs_close_local_fh is
177+
* closing and we must wait, else we unlink and close.
178+
*/
179+
if (rcu_access_pointer(nfl->nfs_uuid) == NULL) {
180+
/* nfs_close_local_fh() is doing the
181+
* close and we must wait. until it unlinks
182+
*/
183+
wait_var_event_spinlock(nfl,
184+
list_first_entry_or_null(
185+
&nfs_uuid->files,
186+
struct nfs_file_localio,
187+
list) != nfl,
188+
&nfs_uuid->lock);
189+
continue;
190+
}
191+
177192
ro_nf = unrcu_pointer(xchg(&nfl->ro_file, NULL));
178193
rw_nf = unrcu_pointer(xchg(&nfl->rw_file, NULL));
179194

180-
spin_lock(&nfs_uuid->lock);
181195
/* Remove nfl from nfs_uuid->files list */
182196
list_del_init(&nfl->list);
183197
spin_unlock(&nfs_uuid->lock);
184-
/* Now we can allow racing nfs_close_local_fh() to
185-
* skip the locking.
186-
*/
187-
RCU_INIT_POINTER(nfl->nfs_uuid, NULL);
188-
189198
if (ro_nf)
190199
nfs_to_nfsd_file_put_local(ro_nf);
191200
if (rw_nf)
192201
nfs_to_nfsd_file_put_local(rw_nf);
193-
194202
cond_resched();
203+
spin_lock(&nfs_uuid->lock);
204+
/* Now we can allow racing nfs_close_local_fh() to
205+
* skip the locking.
206+
*/
207+
RCU_INIT_POINTER(nfl->nfs_uuid, NULL);
208+
wake_up_var_locked(&nfl->nfs_uuid, &nfs_uuid->lock);
195209
}
196210

197-
spin_lock(&nfs_uuid->lock);
198-
BUG_ON(!list_empty(&nfs_uuid->files));
199-
200211
/* Remove client from nn->local_clients */
201212
if (nfs_uuid->list_lock) {
202213
spin_lock(nfs_uuid->list_lock);
@@ -304,23 +315,43 @@ void nfs_close_local_fh(struct nfs_file_localio *nfl)
304315
return;
305316
}
306317

307-
ro_nf = unrcu_pointer(xchg(&nfl->ro_file, NULL));
308-
rw_nf = unrcu_pointer(xchg(&nfl->rw_file, NULL));
309-
310318
spin_lock(&nfs_uuid->lock);
311-
/* Remove nfl from nfs_uuid->files list */
312-
list_del_init(&nfl->list);
319+
if (!rcu_access_pointer(nfl->nfs_uuid)) {
320+
/* nfs_uuid_put has finished here */
321+
spin_unlock(&nfs_uuid->lock);
322+
rcu_read_unlock();
323+
return;
324+
}
325+
if (list_empty(&nfs_uuid->files)) {
326+
/* nfs_uuid_put() has started closing files, wait for it
327+
* to finished
328+
*/
329+
spin_unlock(&nfs_uuid->lock);
330+
rcu_read_unlock();
331+
wait_var_event(&nfl->nfs_uuid,
332+
rcu_access_pointer(nfl->nfs_uuid) == NULL);
333+
return;
334+
}
335+
/* tell nfs_uuid_put() to wait for us */
336+
RCU_INIT_POINTER(nfl->nfs_uuid, NULL);
313337
spin_unlock(&nfs_uuid->lock);
314338
rcu_read_unlock();
315-
/* Now we can allow racing nfs_close_local_fh() to
316-
* skip the locking.
317-
*/
318-
RCU_INIT_POINTER(nfl->nfs_uuid, NULL);
319339

340+
ro_nf = unrcu_pointer(xchg(&nfl->ro_file, NULL));
341+
rw_nf = unrcu_pointer(xchg(&nfl->rw_file, NULL));
320342
if (ro_nf)
321343
nfs_to_nfsd_file_put_local(ro_nf);
322344
if (rw_nf)
323345
nfs_to_nfsd_file_put_local(rw_nf);
346+
347+
/* Remove nfl from nfs_uuid->files list and signal nfs_uuid_put()
348+
* that we are done. The moment we drop the spinlock the
349+
* nfs_uuid could be freed.
350+
*/
351+
spin_lock(&nfs_uuid->lock);
352+
list_del_init(&nfl->list);
353+
wake_up_var_locked(&nfl->nfs_uuid, &nfs_uuid->lock);
354+
spin_unlock(&nfs_uuid->lock);
324355
}
325356
EXPORT_SYMBOL_GPL(nfs_close_local_fh);
326357

0 commit comments

Comments
 (0)