Commit 57439f878afafefad8836ebf5c49da2a0a746105

Authored by npiggin@suse.de
Committed by Linus Torvalds
1 parent 5904b3b81d

fs: fix superblock iteration race

list_for_each_entry_safe is not suitable to protect against concurrent
modification of the list. 6754af6 introduced a race in sb walking.

list_for_each_entry can use the trick of pinning the current entry in
the list before we drop and retake the lock because it subsequently
follows cur->next. However list_for_each_entry_safe saves n=cur->next
for following before entering the loop body, so when the lock is
dropped, n may be deleted.

Signed-off-by: Nick Piggin <npiggin@suse.de>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: John Stultz <johnstul@us.ibm.com>
Cc: Frank Mayhar <fmayhar@google.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 3 changed files with 23 additions and 0 deletions Side-by-side Diff

... ... @@ -590,6 +590,8 @@
590 590 up_read(&sb->s_umount);
591 591 }
592 592 spin_lock(&sb_lock);
  593 + /* lock was dropped, must reset next */
  594 + list_safe_reset_next(sb, n, s_list);
593 595 count -= pruned;
594 596 __put_super(sb);
595 597 /* more work left to do? */
... ... @@ -374,6 +374,8 @@
374 374 up_read(&sb->s_umount);
375 375  
376 376 spin_lock(&sb_lock);
  377 + /* lock was dropped, must reset next */
  378 + list_safe_reset_next(sb, n, s_list);
377 379 __put_super(sb);
378 380 }
379 381 }
... ... @@ -405,6 +407,8 @@
405 407 up_read(&sb->s_umount);
406 408  
407 409 spin_lock(&sb_lock);
  410 + /* lock was dropped, must reset next */
  411 + list_safe_reset_next(sb, n, s_list);
408 412 __put_super(sb);
409 413 }
410 414 spin_unlock(&sb_lock);
... ... @@ -585,6 +589,8 @@
585 589 }
586 590 up_write(&sb->s_umount);
587 591 spin_lock(&sb_lock);
  592 + /* lock was dropped, must reset next */
  593 + list_safe_reset_next(sb, n, s_list);
588 594 __put_super(sb);
589 595 }
590 596 spin_unlock(&sb_lock);
include/linux/list.h
... ... @@ -544,6 +544,21 @@
544 544 &pos->member != (head); \
545 545 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
546 546  
  547 +/**
  548 + * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
  549 + * @pos: the loop cursor used in the list_for_each_entry_safe loop
  550 + * @n: temporary storage used in list_for_each_entry_safe
  551 + * @member: the name of the list_struct within the struct.
  552 + *
  553 + * list_safe_reset_next is not safe to use in general if the list may be
  554 + * modified concurrently (eg. the lock is dropped in the loop body). An
  555 + * exception to this is if the cursor element (pos) is pinned in the list,
  556 + * and list_safe_reset_next is called after re-taking the lock and before
  557 + * completing the current iteration of the loop body.
  558 + */
  559 +#define list_safe_reset_next(pos, n, member) \
  560 + n = list_entry(pos->member.next, typeof(*pos), member)
  561 +
547 562 /*
548 563 * Double linked lists with a single pointer list head.
549 564 * Mostly useful for hash tables where the two pointer list head is