From: Johannes Weiner <hannes@cmpxchg.org>
To: Chris Li <chrisl@kernel.org>
Cc: Domenico Cerasuolo <cerasuolodomenico@gmail.com>,
sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com,
minchan@kernel.org, ngupta@vflare.org, akpm@linux-foundation.org,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] mm: fix zswap writeback race condition
Date: Wed, 3 May 2023 22:29:04 -0400 [thread overview]
Message-ID: <20230504022904.GA202700@cmpxchg.org> (raw)
In-Reply-To: <ZFLZQ/hJarV+iNYd@google.com>
On Wed, May 03, 2023 at 02:59:31PM -0700, Chris Li wrote:
> Hi Domenico,
>
> On Wed, May 03, 2023 at 05:12:00PM +0200, Domenico Cerasuolo wrote:
> > 1. a page with data A and swap offset X is stored in zswap
> > 2. page A is removed off the LRU by zpool driver for writeback in
> > zswap-shrink work, data for A is mapped by zpool driver
> > 3. user space program faults and invalidates page entry A, offset X is
> > considered free
> > 4. kswapd stores page B at offset X in zswap (zswap could also be full,
> > if so, page B would then be IOed to X, then skip step 5.)
> > 5. entry A is replaced by B in tree->rbroot, this doesn't affect the
> > local reference held by zswap-shrink work
> > 6. zswap-shrink work writes back A at X, and frees zswap entry A
> > 7. swapin of slot X brings A in memory instead of B
>
> Thanks for the interesting discovery.
>
> > V2:
> > - updated comment with better explaination of the situation being
> > addressed in the check
> >
> > Signed-off-by: Domenico Cerasuolo <cerasuolodomenico@gmail.com>
> > Acked-by: Johannes Weiner <hannes@cmpxchg.org>
> > ---
> > mm/zswap.c | 16 ++++++++++++++++
> > 1 file changed, 16 insertions(+)
> >
> > diff --git a/mm/zswap.c b/mm/zswap.c
> > index f6c89049cf70..5d5977c9ea45 100644
> > --- a/mm/zswap.c
> > +++ b/mm/zswap.c
> > @@ -995,6 +995,22 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
> > goto fail;
> >
> > case ZSWAP_SWAPCACHE_NEW: /* page is locked */
^^^^^^^^^^^^^^^^^^^^
> > + /*
> > + * Having a local reference to the zswap entry doesn't exclude
> > + * swapping from invalidating and recycling the swap slot. Once
> > + * the swapcache is secured against concurrent swapping to and
> > + * from the slot, recheck that the entry is still current before
> > + * writing.
> > + */
> > + spin_lock(&tree->lock);
> > + if (zswap_rb_search(&tree->rbroot, entry->offset) != entry) {
> > + spin_unlock(&tree->lock);
> > + delete_from_swap_cache(page_folio(page));
> > + ret = -ENOMEM;
> > + goto fail;
> > + }
> > + spin_unlock(&tree->lock);
> > +
>
> The race condition is still there, just making it much harder to hit.
> What happens after you perform the rb tree search, release tree lock.
> Then the entry gets invalid and recycled right here before the decompress
> step?
Recyling can only happen up until we see ZSWAP_SWAPCACHE_NEW.
Once we see it, we're holding the page lock* on a new swapcache page
for a valid, in-use** swp_entry_t.
The lock of the swapcache page prevents swapin, which would be
required for the count to drop and the entry to be recycled.
__read_swap_cache_async() checked that the entry is valid, so the slot
cannot be allocated to someone else.
Now we just have to check if that entry is the right one, iow the slot
wasn't recycled.
If the slot wasn't recycled, we know we have the right data and we can
start the IO and unlock the page. (After that swapins can continue and
the data can change, but regular writeback vs redirtying rules apply.)
If the slot was indeed recycled before we get ZSWAP_SWAPCACHE_NEW, we
see the mismatch, delete the page from the swapcache and unlock it. A
racing do_swap_page() may have found and reffed the page in swapcache,
and acquire the page lock after us; but it'll see it's no longer in
the swapcache, drop the reference (free the page) and retry the fault.
next prev parent reply other threads:[~2023-05-04 2:29 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-03 15:12 Domenico Cerasuolo
2023-05-03 21:59 ` Chris Li
2023-05-04 2:29 ` Johannes Weiner [this message]
2023-05-04 7:22 ` Chris Li
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=20230504022904.GA202700@cmpxchg.org \
--to=hannes@cmpxchg.org \
--cc=akpm@linux-foundation.org \
--cc=cerasuolodomenico@gmail.com \
--cc=chrisl@kernel.org \
--cc=ddstreet@ieee.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=minchan@kernel.org \
--cc=ngupta@vflare.org \
--cc=sjenning@redhat.com \
--cc=vitaly.wool@konsulko.com \
/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