Skip to content

Commit 05bf86b

Browse files
Hugh Dickinstorvalds
authored andcommitted
tmpfs: fix race between swapoff and writepage
Shame on me! Commit b1dea80 "tmpfs: fix race between umount and writepage" fixed the advertized race, but introduced another: as even its comment makes clear, we cannot safely rely on a peek at list_empty() while holding no lock - until info->swapped is set, shmem_unuse_inode() may delete any formerly-swapped inode from the shmem_swaplist, which in this case would leave a swap area impossible to swapoff. Although I don't relish taking the mutex every time, I don't care much for the alternatives either; and at least the peek at list_empty() in shmem_evict_inode() (a hotter path since most inodes would never have been swapped) remains safe, because we already truncated the whole file. Signed-off-by: Hugh Dickins <hughd@google.com> Cc: stable@kernel.org Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent afa4979 commit 05bf86b

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

‎mm/shmem.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,6 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
10371037
struct address_space *mapping;
10381038
unsigned long index;
10391039
struct inode *inode;
1040-
bool unlock_mutex = false;
10411040

10421041
BUG_ON(!PageLocked(page));
10431042
mapping = page->mapping;
@@ -1072,15 +1071,14 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
10721071
* we've taken the spinlock, because shmem_unuse_inode() will
10731072
* prune a !swapped inode from the swaplist under both locks.
10741073
*/
1075-
if (swap.val && list_empty(&info->swaplist)) {
1074+
if (swap.val) {
10761075
mutex_lock(&shmem_swaplist_mutex);
1077-
/* move instead of add in case we're racing */
1078-
list_move_tail(&info->swaplist, &shmem_swaplist);
1079-
unlock_mutex = true;
1076+
if (list_empty(&info->swaplist))
1077+
list_add_tail(&info->swaplist, &shmem_swaplist);
10801078
}
10811079

10821080
spin_lock(&info->lock);
1083-
if (unlock_mutex)
1081+
if (swap.val)
10841082
mutex_unlock(&shmem_swaplist_mutex);
10851083

10861084
if (index >= info->next_index) {

0 commit comments

Comments
 (0)