Skip to content

Commit 12ba1d3

Browse files
minchankgregkh
authored andcommitted
mm: fix unexpected zeroed page mapping with zram swap
commit e914d8f upstream. Two processes under CLONE_VM cloning, user process can be corrupted by seeing zeroed page unexpectedly. CPU A CPU B do_swap_page do_swap_page SWP_SYNCHRONOUS_IO path SWP_SYNCHRONOUS_IO path swap_readpage valid data swap_slot_free_notify delete zram entry swap_readpage zeroed(invalid) data pte_lock map the *zero data* to userspace pte_unlock pte_lock if (!pte_same) goto out_nomap; pte_unlock return and next refault will read zeroed data The swap_slot_free_notify is bogus for CLONE_VM case since it doesn't increase the refcount of swap slot at copy_mm so it couldn't catch up whether it's safe or not to discard data from backing device. In the case, only the lock it could rely on to synchronize swap slot freeing is page table lock. Thus, this patch gets rid of the swap_slot_free_notify function. With this patch, CPU A will see correct data. CPU A CPU B do_swap_page do_swap_page SWP_SYNCHRONOUS_IO path SWP_SYNCHRONOUS_IO path swap_readpage original data pte_lock map the original data swap_free swap_range_free bd_disk->fops->swap_slot_free_notify swap_readpage read zeroed data pte_unlock pte_lock if (!pte_same) goto out_nomap; pte_unlock return on next refault will see mapped data by CPU B The concern of the patch would increase memory consumption since it could keep wasted memory with compressed form in zram as well as uncompressed form in address space. However, most of cases of zram uses no readahead and do_swap_page is followed by swap_free so it will free the compressed form from in zram quickly. Link: https://lkml.kernel.org/r/[email protected] Fixes: 0bcac06 ("mm, swap: skip swapcache for swapin of synchronous device") Reported-by: Ivan Babrou <[email protected]> Tested-by: Ivan Babrou <[email protected]> Signed-off-by: Minchan Kim <[email protected]> Cc: Nitin Gupta <[email protected]> Cc: Sergey Senozhatsky <[email protected]> Cc: Jens Axboe <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: <[email protected]> [4.14+] Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent bb7645c commit 12ba1d3

File tree

1 file changed

+0
-54
lines changed

1 file changed

+0
-54
lines changed

mm/page_io.c

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -50,54 +50,6 @@ void end_swap_bio_write(struct bio *bio)
5050
bio_put(bio);
5151
}
5252

53-
static void swap_slot_free_notify(struct page *page)
54-
{
55-
struct swap_info_struct *sis;
56-
struct gendisk *disk;
57-
swp_entry_t entry;
58-
59-
/*
60-
* There is no guarantee that the page is in swap cache - the software
61-
* suspend code (at least) uses end_swap_bio_read() against a non-
62-
* swapcache page. So we must check PG_swapcache before proceeding with
63-
* this optimization.
64-
*/
65-
if (unlikely(!PageSwapCache(page)))
66-
return;
67-
68-
sis = page_swap_info(page);
69-
if (data_race(!(sis->flags & SWP_BLKDEV)))
70-
return;
71-
72-
/*
73-
* The swap subsystem performs lazy swap slot freeing,
74-
* expecting that the page will be swapped out again.
75-
* So we can avoid an unnecessary write if the page
76-
* isn't redirtied.
77-
* This is good for real swap storage because we can
78-
* reduce unnecessary I/O and enhance wear-leveling
79-
* if an SSD is used as the as swap device.
80-
* But if in-memory swap device (eg zram) is used,
81-
* this causes a duplicated copy between uncompressed
82-
* data in VM-owned memory and compressed data in
83-
* zram-owned memory. So let's free zram-owned memory
84-
* and make the VM-owned decompressed page *dirty*,
85-
* so the page should be swapped out somewhere again if
86-
* we again wish to reclaim it.
87-
*/
88-
disk = sis->bdev->bd_disk;
89-
entry.val = page_private(page);
90-
if (disk->fops->swap_slot_free_notify && __swap_count(entry) == 1) {
91-
unsigned long offset;
92-
93-
offset = swp_offset(entry);
94-
95-
SetPageDirty(page);
96-
disk->fops->swap_slot_free_notify(sis->bdev,
97-
offset);
98-
}
99-
}
100-
10153
static void end_swap_bio_read(struct bio *bio)
10254
{
10355
struct page *page = bio_first_page_all(bio);
@@ -113,7 +65,6 @@ static void end_swap_bio_read(struct bio *bio)
11365
}
11466

11567
SetPageUptodate(page);
116-
swap_slot_free_notify(page);
11768
out:
11869
unlock_page(page);
11970
WRITE_ONCE(bio->bi_private, NULL);
@@ -392,11 +343,6 @@ int swap_readpage(struct page *page, bool synchronous)
392343
if (sis->flags & SWP_SYNCHRONOUS_IO) {
393344
ret = bdev_read_page(sis->bdev, swap_page_sector(page), page);
394345
if (!ret) {
395-
if (trylock_page(page)) {
396-
swap_slot_free_notify(page);
397-
unlock_page(page);
398-
}
399-
400346
count_vm_event(PSWPIN);
401347
goto out;
402348
}

0 commit comments

Comments
 (0)