Re: [PATCH 04/24] mm/swap: avoid setting page lock bit and doing extra unlock check

From: Kairui Song
Date: Mon Nov 20 2023 - 06:16:29 EST


Chris Li <chrisl@xxxxxxxxxx> 于2023年11月20日周一 12:18写道:
>
> On Sun, Nov 19, 2023 at 11:48 AM Kairui Song <ryncsn@xxxxxxxxx> wrote:
> >
> > From: Kairui Song <kasong@xxxxxxxxxxx>
> >
> > When swapping in a page, mem_cgroup_swapin_charge_folio is called for new
> > allocated folio, nothing else is referencing the folio so no need to set
> > the lock bit. This avoided doing unlock check on error path.
> >
> > Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx>
> > ---
> > mm/swap_state.c | 20 +++++++++-----------
> > 1 file changed, 9 insertions(+), 11 deletions(-)
> >
> > diff --git a/mm/swap_state.c b/mm/swap_state.c
> > index ac4fa404eaa7..45dd8b7c195d 100644
> > --- a/mm/swap_state.c
> > +++ b/mm/swap_state.c
> > @@ -458,6 +458,8 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
>
> You move the mem_cgroup_swapin_charge_folio() inside the for loop:
>
>
> for (;;) {
> int err;
> /*
> * First check the swap cache. Since this is normally
> * called after swap_cache_get_folio() failed, re-calling
> * that would confuse statistics.
> */
> folio = filemap_get_folio(swap_address_space(entry),
> swp_offset(entry));
>
>
> > mpol, ilx, numa_node_id());
> > if (!folio)
> > goto fail_put_swap;
> > + if (mem_cgroup_swapin_charge_folio(folio, NULL, gfp_mask, entry))
> > + goto fail_put_folio;
>
> Wouldn't it cause repeat charging of the folio when it is racing
> against others in the for loop?

The race loser will call folio_put and discharge it?

>
> >
> > /*
> > * Swap entry may have been freed since our caller observed it.
> > @@ -483,13 +485,9 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
> > /*
> > * The swap entry is ours to swap in. Prepare the new page.
> > */
> > -
> > __folio_set_locked(folio);
> > __folio_set_swapbacked(folio);
> >
> > - if (mem_cgroup_swapin_charge_folio(folio, NULL, gfp_mask, entry))
> > - goto fail_unlock;
> > -
>
> The original code makes the charge outside of the for loop. Only the
> winner can charge once.

Right, this patch may make the charge/dis-charge path more complex for
race swapin, I'll re-check this part.

>
> Chris