stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sergey Senozhatsky <senozhatsky@chromium.org>
To: Minchan Kim <minchan@kernel.org>,
	Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	mm-commits@vger.kernel.org, stable@vger.kernel.org,
	ngupta@vflare.org, ivan@cloudflare.com, david@redhat.com,
	axboe@kernel.dk
Subject: Re: + mm-fix-unexpected-zeroed-page-mapping-with-zram-swap.patch added to -mm tree
Date: Wed, 30 Mar 2022 14:07:21 +0900	[thread overview]
Message-ID: <YkPliXgyWSEd+RvQ@google.com> (raw)
In-Reply-To: <YkPkYmn6E7DY7EPP@google.com>

On (22/03/30 14:02), Sergey Senozhatsky wrote:
> On (22/03/25 20:43), Andrew Morton wrote:
> > 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.
> 
> Minchan, a quick question, shouldn't this instead revert 3f2b1a04f4493?

Never mind! My bad.

The patch looks good to me.

      reply	other threads:[~2022-03-30  5:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-26  3:43 + mm-fix-unexpected-zeroed-page-mapping-with-zram-swap.patch added to -mm tree Andrew Morton
2022-03-30  5:02 ` Sergey Senozhatsky
2022-03-30  5:07   ` Sergey Senozhatsky [this message]

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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

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

  git send-email \
    --in-reply-to=YkPliXgyWSEd+RvQ@google.com \
    --to=senozhatsky@chromium.org \
    --cc=akpm@linux-foundation.org \
    --cc=axboe@kernel.dk \
    --cc=david@redhat.com \
    --cc=ivan@cloudflare.com \
    --cc=minchan@kernel.org \
    --cc=mm-commits@vger.kernel.org \
    --cc=ngupta@vflare.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).