Re: [PATCH] mm/swap: fix race condition in direct swapin path

From: Kairui Song
Date: Mon Feb 05 2024 - 22:13:32 EST


On Tue, Feb 6, 2024 at 10:24 AM SeongJae Park <sj@xxxxxxxxxx> wrote:
>
> Hi Kairui,
>
> On Mon, 5 Feb 2024 19:09:59 +0800 Kairui Song <ryncsn@xxxxxxxxx> wrote:
>
> [...]
> > mm/memory.c | 19 +++++++++++++++++++
> > mm/swap.h | 5 +++++
> > mm/swapfile.c | 16 ++++++++++++++++
> > 3 files changed, 40 insertions(+)
> >
> > diff --git a/mm/memory.c b/mm/memory.c
> > index 7e1f4849463a..fd7c55a292f1 100644
> > --- a/mm/memory.c
> > +++ b/mm/memory.c
> > @@ -3867,6 +3867,20 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> > if (!folio) {
> > if (data_race(si->flags & SWP_SYNCHRONOUS_IO) &&
> > __swap_count(entry) == 1) {
> > + /*
> > + * With swap count == 1, after we read the entry,
> > + * other threads could finish swapin first, free
> > + * the entry, then swapout the modified page using
> > + * the same entry. Now the content we just read is
> > + * stalled, and it's undetectable as pte_same()
> > + * returns true due to entry reuse.
> > + *
> > + * So pin the swap entry using the cache flag even
> > + * cache is not used.
> > + */
> > + if (swapcache_prepare(entry))
> > + goto out;
> > +
>
> I'm getting below build error after this patch. I guess maybe the code need to
> take care of CONFIG_SWAP unset case?
>
> .../mm/memory.c: In function 'do_swap_page':
> .../mm/memory.c:4004:8: error: implicit declaration of function 'swapcache_prepare'; did you mean 'swapcache_clear'? [-Werror=implicit-function-declaration]
> 4004 | if (swapcache_prepare(entry))
> | ^~~~~~~~~~~~~~~~~
> | swapcache_clear
>
>

Ah, right. Thanks for the feedback.

For the CONFIG_SWAP unset case I added an empty function for
swapcache_clear, but the original swapcache_prepare also needs an
empty placeholder now. Will add that in V2.