Skip to content

Commit f893ab4

Browse files
Weijie Yangtorvalds
authored andcommitted
mm/swap: fix race on swap_info reuse between swapoff and swapon
swapoff clear swap_info's SWP_USED flag prematurely and free its resources after that. A concurrent swapon will reuse this swap_info while its previous resources are not cleared completely. These late freed resources are: - p->percpu_cluster - swap_cgroup_ctrl[type] - block_device setting - inode->i_flags &= ~S_SWAPFILE This patch clears the SWP_USED flag after all its resources are freed, so that swapon can reuse this swap_info by alloc_swap_info() safely. [akpm@linux-foundation.org: tidy up code comment] Signed-off-by: Weijie Yang <weijie.yang@samsung.com> Acked-by: Hugh Dickins <hughd@google.com> Cc: Krzysztof Kozlowski <k.kozlowski@samsung.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 579f829 commit f893ab4

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎mm/swapfile.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
19231923
p->swap_map = NULL;
19241924
cluster_info = p->cluster_info;
19251925
p->cluster_info = NULL;
1926-
p->flags = 0;
19271926
frontswap_map = frontswap_map_get(p);
19281927
spin_unlock(&p->lock);
19291928
spin_unlock(&swap_lock);
@@ -1949,6 +1948,16 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
19491948
mutex_unlock(&inode->i_mutex);
19501949
}
19511950
filp_close(swap_file, NULL);
1951+
1952+
/*
1953+
* Clear the SWP_USED flag after all resources are freed so that swapon
1954+
* can reuse this swap_info in alloc_swap_info() safely. It is ok to
1955+
* not hold p->lock after we cleared its SWP_WRITEOK.
1956+
*/
1957+
spin_lock(&swap_lock);
1958+
p->flags = 0;
1959+
spin_unlock(&swap_lock);
1960+
19521961
err = 0;
19531962
atomic_inc(&proc_poll_event);
19541963
wake_up_interruptible(&proc_poll_wait);

0 commit comments

Comments
 (0)