Skip to contentSkip to conversation

mm: do not free shared swap slots

PATCH Applied Vinayak Menon 3 participants 5 messages Oct 22, 2019 – Oct 31, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: do not free shared swap slots

The following race is observed due to which a processes faulting on a swap entry, finds the page neither in swapcache nor swap. This causes zram to give a zero filled page that gets mapped to the process, resulting in a user space crash later.

Consider parent and child processes Pa and Pb sharing the same swap slot with swap_count 2. Swap is on zram with SWP_SYNCHRONOUS_IO set. Virtual address 'VA' of Pa and Pb points to the shared swap entry.

Pa Pb

fault on VA                              fault on VA
do_swap_page                             do_swap_page
lookup_swap_cache fails                  lookup_swap_cache fails

Pb scheduled out
swapin_readahead (deletes zram entry)
swap_free (makes swap_count 1)
Pb scheduled in swap_readpage (swap_count == 1) Takes SWP_SYNCHRONOUS_IO path zram enrty absent zram gives a zero filled page

Fix this by making sure that swap slot is freed only when swap count drops down to one.

---
 mm/page_io.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/page_io.c +3 -3
index 83db25a..3a198de 100644
@@ -74,6 +74,7 @@ static void swap_slot_free_notify(struct page *page)
 {
 	struct swap_info_struct *sis;
 	struct gendisk *disk;
+	swp_entry_t entry;
 
 	/*
 	 * There is no guarantee that the page is in swap cache - the software
@@ -105,11 +106,10 @@ static void swap_slot_free_notify(struct page *page)
 	 * we again wish to reclaim it.
 	 */
 	disk = sis->bdev->bd_disk;
-	if (disk->fops->swap_slot_free_notify) {
-		swp_entry_t entry;
+	entry.val = page_private(page);
+	if (disk->fops->swap_slot_free_notify && __swap_count(entry) == 1) {
 		unsigned long offset;
 
-		entry.val = page_private(page);
 		offset = swp_offset(entry);
 
 		SetPageDirty(page);
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a
member of the Code Aurora Forum, hosted by The Linux Foundation
Acked-by on v1 1/1
#1 lore raw
Quoted 23 lines · Vinayak Menon

The following race is observed due to which a processes faulting on a swap entry, finds the page neither in swapcache nor swap. This causes zram to give a zero filled page that gets mapped to the process, resulting in a user space crash later.

Consider parent and child processes Pa and Pb sharing the same swap slot with swap_count 2. Swap is on zram with SWP_SYNCHRONOUS_IO set. Virtual address 'VA' of Pa and Pb points to the shared swap entry.

Pa Pb

fault on VA                              fault on VA
do_swap_page                             do_swap_page
lookup_swap_cache fails                  lookup_swap_cache fails

Pb scheduled out
swapin_readahead (deletes zram entry)
swap_free (makes swap_count 1)
Pb scheduled in swap_readpage (swap_count == 1) Takes SWP_SYNCHRONOUS_IO path zram enrty absent zram gives a zero filled page

Quoted 6 lines

Fix this by making sure that swap slot is freed only when swap count drops down to one.

-stable material from v4.15.

Thanks.

#2 lore raw
Quoted 27 lines · Minchan Kim
Quoted 26 lines · Vinayak Menon

The following race is observed due to which a processes faulting on a swap entry, finds the page neither in swapcache nor swap. This causes zram to give a zero filled page that gets mapped to the process, resulting in a user space crash later.

Consider parent and child processes Pa and Pb sharing the same swap slot with swap_count 2. Swap is on zram with SWP_SYNCHRONOUS_IO set. Virtual address 'VA' of Pa and Pb points to the shared swap entry.

Pa Pb

fault on VA                              fault on VA
do_swap_page                             do_swap_page
lookup_swap_cache fails                  lookup_swap_cache fails

Pb scheduled out
swapin_readahead (deletes zram entry)
swap_free (makes swap_count 1)
Pb scheduled in swap_readpage (swap_count == 1) Takes SWP_SYNCHRONOUS_IO path zram enrty absent zram gives a zero filled page

Fix this by making sure that swap slot is freed only when swap count drops down to one.

Quoted 6 lines
Quoted 3 lines

-stable material from v4.15.

Which commit does this fix? Fixes: tag would be highly appreciated.

Signature · 2 lines
Michal Hocko
SUSE Labs
#3 lore raw
Quoted 28 lines · Michal Hocko
Quoted 27 lines · Minchan Kim
Quoted 26 lines · Vinayak Menon

The following race is observed due to which a processes faulting on a swap entry, finds the page neither in swapcache nor swap. This causes zram to give a zero filled page that gets mapped to the process, resulting in a user space crash later.

Consider parent and child processes Pa and Pb sharing the same swap slot with swap_count 2. Swap is on zram with SWP_SYNCHRONOUS_IO set. Virtual address 'VA' of Pa and Pb points to the shared swap entry.

Pa Pb

fault on VA                              fault on VA
do_swap_page                             do_swap_page
lookup_swap_cache fails                  lookup_swap_cache fails

Pb scheduled out
swapin_readahead (deletes zram entry)
swap_free (makes swap_count 1)
Pb scheduled in swap_readpage (swap_count == 1) Takes SWP_SYNCHRONOUS_IO path zram enrty absent zram gives a zero filled page

Fix this by making sure that swap slot is freed only when swap count drops down to one.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

-stable material from v4.15.

Which commit does this fix? Fixes: tag would be highly appreciated.

Oops, I missed to mention it.

Fixes aa8d22a11da9 (mm: swap: SWP_SYNCHRONOUS_IO: skip swapcache only if swapped page has no other reference)
Quoted 4 lines
Signature · 3 lines
-- 
Michal Hocko
SUSE Labs
#4 lore raw
Quoted 32 lines · Minchan Kim
Quoted 31 lines · Michal Hocko
Quoted 30 lines · Minchan Kim
Quoted 29 lines · Vinayak Menon

The following race is observed due to which a processes faulting on a swap entry, finds the page neither in swapcache nor swap. This causes zram to give a zero filled page that gets mapped to the process, resulting in a user space crash later.

Consider parent and child processes Pa and Pb sharing the same swap slot with swap_count 2. Swap is on zram with SWP_SYNCHRONOUS_IO set. Virtual address 'VA' of Pa and Pb points to the shared swap entry.

Pa Pb

fault on VA                              fault on VA
do_swap_page                             do_swap_page
lookup_swap_cache fails                  lookup_swap_cache fails

Pb scheduled out
swapin_readahead (deletes zram entry)
swap_free (makes swap_count 1)
Pb scheduled in swap_readpage (swap_count == 1) Takes SWP_SYNCHRONOUS_IO path zram enrty absent zram gives a zero filled page

Fix this by making sure that swap slot is freed only when swap count drops down to one.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

-stable material from v4.15.

Which commit does this fix? Fixes: tag would be highly appreciated.

Oops, I missed to mention it.

Fixes aa8d22a11da9 (mm: swap: SWP_SYNCHRONOUS_IO: skip swapcache only if swapped page has no other reference)

Thanks!

Signature · 2 lines
Michal Hocko
SUSE Labs