From: Yosry Ahmed <yosryahmed@google.com>
To: Nhat Pham <nphamcs@gmail.com>
Cc: Usama Arif <usamaarif642@gmail.com>,
akpm@linux-foundation.org, linux-mm@kvack.org,
hannes@cmpxchg.org, david@redhat.com, willy@infradead.org,
kanchana.p.sridhar@intel.com, chengming.zhou@linux.dev,
ryan.roberts@arm.com, ying.huang@intel.com, 21cnbao@gmail.com,
riel@surriel.com, shakeel.butt@linux.dev, kernel-team@meta.com,
linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
Kairui Song <kasong@tencent.com>, Kairui Song <ryncsn@gmail.com>
Subject: Re: [RFC 1/4] mm/zswap: skip swapcache for swapping in zswap pages
Date: Fri, 25 Oct 2024 12:10:37 -0700 [thread overview]
Message-ID: <CAJD7tkaMThYdgBNJnm913u0MYGnh73pzKd8wDbOX0T6nxxdvqA@mail.gmail.com> (raw)
In-Reply-To: <CAKEwX=PVbiEd9_4NAvLrHG9dr3k-25c4AB1iEagi4ZBcYRX24w@mail.gmail.com>
On Fri, Oct 25, 2024 at 11:19 AM Nhat Pham <nphamcs@gmail.com> wrote:
>
> On Tue, Oct 22, 2024 at 5:46 PM Yosry Ahmed <yosryahmed@google.com> wrote:
> >
> > [..]
> > > >> @@ -1576,6 +1576,52 @@ bool zswap_store(struct folio *folio)
> > > >> return ret;
> > > >> }
> > > >>
> > > >> +static bool swp_offset_in_zswap(unsigned int type, pgoff_t offset)
> > > >> +{
> > > >> + return (offset >> SWAP_ADDRESS_SPACE_SHIFT) < nr_zswap_trees[type];
> > > >
> > > > I am not sure I understand what we are looking for here. When does
> > > > this return false? Aren't the zswap trees always allocated during
> > > > swapon?
> > > >
> > >
> > > Hi Yosry,
> > >
> > > Thanks for the review!
> > >
> > > It becomes useful in patch 3 when trying to determine if a large folio can be allocated.
> > >
> > > For e.g. if the swap entry is the last entry of the last tree, and 1M folios are enabled
> > > (nr_pages = 256), then the while loop in zswap_present_test will try to access a tree
> > > that doesn't exist from the 2nd 4K page onwards if we dont have this check in
> > > zswap_present_test.
> >
> > Doesn't swap_pte_batch() make sure that the range of swap entries
> > passed here all corresponds to existing swap entries, and those
> > entries should always have a corresponding zswap tree? How can the
> > passed in range contain an entry that is not in any zswap tree?
> >
> > I feel like I am missing something.
> >
> > >
> > > >> +}
> > > >> +
> > > >> +/* Returns true if the entire folio is in zswap */
> > > >
> > > > There isn't really a folio at this point, maybe "Returns true if the
> > > > entire range is in zswap"?
> > >
> > > Will change, Thanks!
> > >
> > > >
> > > > Also, this is racy because an exclusive load, invalidation, or
> > > > writeback can cause an entry to be removed from zswap. Under what
> > > > conditions is this safe? The caller can probably guarantee we don't
> > > > race against invalidation, but can we guarantee that concurrent
> > > > exclusive loads or writebacks don't happen?
> > > >
> > > > If the answer is yes, this needs to be properly documented.
> > >
> > > swapcache_prepare should stop things from becoming racy.
> > >
> > > lets say trying to swapin a mTHP of size 32 pages:
> > > - T1 is doing do_swap_page, T2 is doing zswap_writeback.
> > > - T1 - Check if the entire 32 pages is in zswap, swapcache_prepare(entry, nr_pages) in do_swap_page is not yet called.
> > > - T2 - zswap_writeback_entry starts and lets say writes page 2 to swap. it calls __read_swap_cache_async -> swapcache_prepare increments swap_map count, writes page to swap.
> >
> > Can the folio be dropped from the swapcache at this point (e.g. by
> > reclaim)? If yes, it seems like swapcache_prepare() can succeed and
> > try to read it from zswap.
> >
>
> I think you're onto something.
>
> Can this happen: say T2 writebacks a couple of tail pages, but not all
> of them, then drops everything from swap cache. Then T1 can definitely
> proceed. It would then check again in zswap_load(), which returns
> false (thanks to zswap_present()) test.
>
> All fine and good so far, but then in swap_read_folio(), it would try
> to fall back to reading the entire large folio from swapfile, which
> will contain bogus data in pages that have not been written back by
> T2.
>
> I think the problem is swap_read_folio() assumes it always succeeds,
> and a precondition for successful reading is the large folio must have
> no mixed backing state for its subpages, which we fail to guarantee
> before entering swap_read_folio(). This can lead to memory corruption.
>
> Buuut, I think all we need to do is just check the backing state again
> after T1's swapcache_prepare() call. At this point, we are guaranteed
> to have a stable backing state. If it fails here, then we can just
> exit and fall back to individual page swapping in.
I think this should work, but we need to take a closer look for other
things that can go wrong along this path.
next prev parent reply other threads:[~2024-10-25 19:11 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-18 10:48 [RFC 0/4] mm: zswap: add support for zswapin of large folios Usama Arif
2024-10-18 10:48 ` [RFC 1/4] mm/zswap: skip swapcache for swapping in zswap pages Usama Arif
2024-10-21 21:09 ` Yosry Ahmed
2024-10-22 19:49 ` Usama Arif
2024-10-23 0:45 ` Yosry Ahmed
2024-10-25 18:19 ` Nhat Pham
2024-10-25 19:10 ` Yosry Ahmed [this message]
2024-10-21 21:11 ` Yosry Ahmed
2024-10-22 19:59 ` Usama Arif
2024-10-23 0:47 ` Yosry Ahmed
2024-10-18 10:48 ` [RFC 2/4] mm/zswap: modify zswap_decompress to accept page instead of folio Usama Arif
2024-10-18 10:48 ` [RFC 3/4] mm/zswap: add support for large folio zswapin Usama Arif
2024-10-21 5:49 ` Barry Song
2024-10-21 10:44 ` Usama Arif
2024-10-21 10:55 ` Barry Song
2024-10-21 12:21 ` Usama Arif
2024-10-21 20:28 ` Barry Song
2024-10-21 20:57 ` Usama Arif
2024-10-21 21:34 ` Yosry Ahmed
2024-10-18 10:48 ` [RFC 4/4] mm/zswap: count successful large folio zswap loads Usama Arif
2024-10-21 5:09 ` [RFC 0/4] mm: zswap: add support for zswapin of large folios Barry Song
2024-10-21 10:40 ` Usama Arif
2024-10-22 15:26 ` Usama Arif
2024-10-22 20:46 ` Barry Song
2024-10-22 21:17 ` Usama Arif
2024-10-22 22:07 ` Barry Song
2024-10-23 10:26 ` Barry Song
2024-10-23 10:48 ` Usama Arif
2024-10-23 13:08 ` Usama Arif
2024-10-23 18:02 ` Yosry Ahmed
2024-10-23 18:31 ` Usama Arif
2024-10-23 18:52 ` Barry Song
2024-10-23 19:47 ` Usama Arif
2024-10-23 20:36 ` Barry Song
2024-10-23 23:35 ` Barry Song
2024-10-24 14:29 ` Johannes Weiner
2024-10-24 17:48 ` Barry Song
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=CAJD7tkaMThYdgBNJnm913u0MYGnh73pzKd8wDbOX0T6nxxdvqA@mail.gmail.com \
--to=yosryahmed@google.com \
--cc=21cnbao@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=chengming.zhou@linux.dev \
--cc=david@redhat.com \
--cc=hannes@cmpxchg.org \
--cc=kanchana.p.sridhar@intel.com \
--cc=kasong@tencent.com \
--cc=kernel-team@meta.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=riel@surriel.com \
--cc=ryan.roberts@arm.com \
--cc=ryncsn@gmail.com \
--cc=shakeel.butt@linux.dev \
--cc=usamaarif642@gmail.com \
--cc=willy@infradead.org \
--cc=ying.huang@intel.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