diff mbox series

mm: do not free shared swap slots

Message ID 1571743294-14285-1-git-send-email-vinmenon@codeaurora.org (mailing list archive)
State New, archived
Headers show
Series mm: do not free shared swap slots | expand

Commit Message

Vinayak Menon Oct. 22, 2019, 11:21 a.m. UTC
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 <vinmenon@codeaurora.org>
Suggested-by: Minchan Kim <minchan@google.com>
Cc: Michal Hocko <mhocko@suse.com>
---
 mm/page_io.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Minchan Kim Oct. 30, 2019, 12:24 a.m. UTC | #1
On Tue, Oct 22, 2019 at 04:51:34PM +0530, Vinayak Menon wrote:
> 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 <vinmenon@codeaurora.org>
> Suggested-by: Minchan Kim <minchan@google.com>
> Cc: Michal Hocko <mhocko@suse.com>
Acked-by: Minchan Kim <minchan@kernel.org>

-stable material from v4.15.

Thanks.
Michal Hocko Oct. 30, 2019, 10:24 a.m. UTC | #2
On Tue 29-10-19 17:24:47, Minchan Kim wrote:
> On Tue, Oct 22, 2019 at 04:51:34PM +0530, Vinayak Menon wrote:
> > 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 <vinmenon@codeaurora.org>
> > Suggested-by: Minchan Kim <minchan@google.com>
> > Cc: Michal Hocko <mhocko@suse.com>
> Acked-by: Minchan Kim <minchan@kernel.org>
> 
> -stable material from v4.15.

Which commit does this fix? Fixes: tag would be highly appreciated.
Minchan Kim Oct. 31, 2019, 2:46 p.m. UTC | #3
On Wed, Oct 30, 2019 at 11:24:09AM +0100, Michal Hocko wrote:
> On Tue 29-10-19 17:24:47, Minchan Kim wrote:
> > On Tue, Oct 22, 2019 at 04:51:34PM +0530, Vinayak Menon wrote:
> > > 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 <vinmenon@codeaurora.org>
> > > Suggested-by: Minchan Kim <minchan@google.com>
> > > Cc: Michal Hocko <mhocko@suse.com>
> > Acked-by: Minchan Kim <minchan@kernel.org>
> > 
> > -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)

> 
> -- 
> Michal Hocko
> SUSE Labs
Michal Hocko Oct. 31, 2019, 5:15 p.m. UTC | #4
On Thu 31-10-19 07:46:08, Minchan Kim wrote:
> On Wed, Oct 30, 2019 at 11:24:09AM +0100, Michal Hocko wrote:
> > On Tue 29-10-19 17:24:47, Minchan Kim wrote:
> > > On Tue, Oct 22, 2019 at 04:51:34PM +0530, Vinayak Menon wrote:
> > > > 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 <vinmenon@codeaurora.org>
> > > > Suggested-by: Minchan Kim <minchan@google.com>
> > > > Cc: Michal Hocko <mhocko@suse.com>
> > > Acked-by: Minchan Kim <minchan@kernel.org>
> > > 
> > > -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!
diff mbox series

Patch

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);
 
 		SetPageDirty(page);