Linux-mm Archive on
 help / color / Atom feed
From: Vinayak Menon <>
Cc:, Vinayak Menon <>,
	Michal Hocko <>
Subject: [PATCH] mm: do not free shared swap slots
Date: Tue, 22 Oct 2019 16:51:34 +0530
Message-ID: <> (raw)

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.

Signed-off-by: Vinayak Menon <>
Suggested-by: Minchan Kim <>
Cc: Michal Hocko <>
 mm/page_io.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mm/page_io.c b/mm/page_io.c
index 83db25a..3a198de 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -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);
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a
member of the Code Aurora Forum, hosted by The Linux Foundation

             reply index

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-22 11:21 Vinayak Menon [this message]
2019-10-30  0:24 ` Minchan Kim
2019-10-30 10:24   ` Michal Hocko
2019-10-31 14:46     ` Minchan Kim
2019-10-31 17:15       ` Michal Hocko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-mm Archive on

Archives are clonable:
	git clone --mirror linux-mm/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-mm linux-mm/ \
	public-inbox-index linux-mm

Example config snippet for mirrors

Newsgroup available over NNTP:

AGPL code for this site: git clone