diff options
author | Krzysztof Kozlowski <k.kozlowski@samsung.com> | 2013-10-16 16:47:06 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-10-17 00:35:53 -0400 |
commit | 5b808a2300a5ac45f4798ebfac8b367e98a4b692 (patch) | |
tree | b08db2864c1380eeb1c41f29ebeea1a2c29dd473 | |
parent | fad1a86e25e0a1f85635ed06ef62ddadd5b8fa4c (diff) |
swap: fix set_blocksize race during swapon/swapoff
Fix race between swapoff and swapon. Swapoff used old_block_size from
swap_info outside of swapon_mutex so it could be overwritten by
concurrent swapon.
The race has visible effect only if more than one swap block device
exists with different block sizes (e.g. /dev/sda1 with block size 4096
and /dev/sdb1 with 512). In such case it leads to setting the blocksize
of swapped off device with wrong blocksize.
The bug can be triggered with multiple concurrent swapoff and swapon:
0. Swap for some device is on.
1. swapoff:
First the swapoff is called on this device and "struct swap_info_struct
*p" is assigned. This is done under swap_lock however this lock is
released for the call try_to_unuse().
2. swapon:
After the assignment above (and before acquiring swapon_mutex &
swap_lock by swapoff) the swapon is called on the same device.
The p->old_block_size is assigned to the value of block_size the device.
This block size should be the same as previous but sometimes it is not.
The swapon ends successfully.
3. swapoff:
Swapoff resumes, grabs the locks and mutex and continues to disable this
swap device. Now it sets the block size to value taken from swap_info
which was overwritten by swapon in 2.
Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
Reported-by: Weijie Yang <weijie.yang.kh@gmail.com>
Cc: Bob Liu <bob.liu@oracle.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Shaohua Li <shli@fusionio.com>
Cc: Minchan Kim <minchan@kernel.org>
Acked-by: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | mm/swapfile.c | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/mm/swapfile.c b/mm/swapfile.c index 3963fc24fcc1..de7c904e52e5 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c | |||
@@ -1824,6 +1824,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) | |||
1824 | struct filename *pathname; | 1824 | struct filename *pathname; |
1825 | int i, type, prev; | 1825 | int i, type, prev; |
1826 | int err; | 1826 | int err; |
1827 | unsigned int old_block_size; | ||
1827 | 1828 | ||
1828 | if (!capable(CAP_SYS_ADMIN)) | 1829 | if (!capable(CAP_SYS_ADMIN)) |
1829 | return -EPERM; | 1830 | return -EPERM; |
@@ -1914,6 +1915,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) | |||
1914 | } | 1915 | } |
1915 | 1916 | ||
1916 | swap_file = p->swap_file; | 1917 | swap_file = p->swap_file; |
1918 | old_block_size = p->old_block_size; | ||
1917 | p->swap_file = NULL; | 1919 | p->swap_file = NULL; |
1918 | p->max = 0; | 1920 | p->max = 0; |
1919 | swap_map = p->swap_map; | 1921 | swap_map = p->swap_map; |
@@ -1938,7 +1940,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) | |||
1938 | inode = mapping->host; | 1940 | inode = mapping->host; |
1939 | if (S_ISBLK(inode->i_mode)) { | 1941 | if (S_ISBLK(inode->i_mode)) { |
1940 | struct block_device *bdev = I_BDEV(inode); | 1942 | struct block_device *bdev = I_BDEV(inode); |
1941 | set_blocksize(bdev, p->old_block_size); | 1943 | set_blocksize(bdev, old_block_size); |
1942 | blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); | 1944 | blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); |
1943 | } else { | 1945 | } else { |
1944 | mutex_lock(&inode->i_mutex); | 1946 | mutex_lock(&inode->i_mutex); |