[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <Z7aU7wEx9jKo3TaT@MiWiFi-R3L-srv>
Date: Thu, 20 Feb 2025 10:35:27 +0800
From: Baoquan He <bhe@...hat.com>
To: Kairui Song <ryncsn@...il.com>
Cc: linux-mm@...ck.org, Andrew Morton <akpm@...ux-foundation.org>,
Chris Li <chrisl@...nel.org>, Barry Song <v-songbaohua@...o.com>,
Hugh Dickins <hughd@...gle.com>,
Yosry Ahmed <yosryahmed@...gle.com>,
"Huang, Ying" <ying.huang@...ux.alibaba.com>,
Nhat Pham <nphamcs@...il.com>, Johannes Weiner <hannes@...xchg.org>,
Kalesh Singh <kaleshsingh@...gle.com>, linux-kernel@...r.kernel.org
Subject: Re: [PATCH 5/7] mm, swap: use percpu cluster as allocation fast path
On 02/19/25 at 07:12pm, Kairui Song wrote:
> On Wed, Feb 19, 2025 at 6:55 PM Baoquan He <bhe@...hat.com> wrote:
> >
> > On 02/19/25 at 04:34pm, Kairui Song wrote:
> > > On Wed, Feb 19, 2025 at 3:54 PM Baoquan He <bhe@...hat.com> wrote:
> > >
> > > Hi Baoquan,
> > >
> > > Thanks for the review!
> > >
> > > >
> > > > On 02/15/25 at 01:57am, Kairui Song wrote:
> > > > > From: Kairui Song <kasong@...cent.com>
> > > > >
> > > > > Current allocation workflow first traverses the plist with a global lock
> > > > > held, after choosing a device, it uses the percpu cluster on that swap
> > > > > device. This commit moves the percpu cluster variable out of being tied
> > > > > to individual swap devices, making it a global percpu variable, and will
> > > > > be used directly for allocation as a fast path.
> > > > >
> > > > > The global percpu cluster variable will never point to a HDD device, and
> > > > > allocation on HDD devices is still globally serialized.
> > > > >
> > > > > This improves the allocator performance and prepares for removal of the
> > > > > slot cache in later commits. There shouldn't be much observable behavior
> > > > > change, except one thing: this changes how swap device allocation
> > > > > rotation works.
> > > > >
> > > > > Currently, each allocation will rotate the plist, and because of the
> > > > > existence of slot cache (64 entries), swap devices of the same priority
> > > > > are rotated for every 64 entries consumed. And, high order allocations
> > > > > are different, they will bypass the slot cache, and so swap device is
> > > > > rotated for every 16K, 32K, or up to 2M allocation.
> > > > >
> > > > > The rotation rule was never clearly defined or documented, it was changed
> > > > > several times without mentioning too.
> > > > >
> > > > > After this commit, once slot cache is gone in later commits, swap device
> > > > > rotation will happen for every consumed cluster. Ideally non-HDD devices
> > > > > will be rotated if 2M space has been consumed for each order, this seems
> > > >
> > > > This breaks the rule where the high priority swap device is always taken
> > > > to allocate as long as there's free space in the device. After this patch,
> > > > it will try the percpu cluster firstly which is lower priority even though
> > > > the higher priority device has free space. However, this only happens when
> > > > the higher priority device is exhausted, not a generic case. If this is
> > > > expected, it may need be mentioned in log or doc somewhere at least.
> > >
> > > Hmm, actually this rule was already broken if you are very strict
> > > about it. The current percpu slot cache does a pre-allocation, so the
> > > high priority device will be removed from the plist while some CPU's
> > > slot cache holding usable entries.
> > >
> > > If the high priority device is exhausted, some CPU's percpu cluster
> > > will point to a low priority device indeed, and keep using it until
> > > the percpu cluster is drained. I think this should be
> > > OK. The high priority device is already full, so the amount of
> > > swapouts falls back to low priority device is only a performance
> > > issue, I think it's a tiny change for a rare case.
> > >
> > > >
> > > > > reasonable. HDD devices is rotated for every allocation regardless of the
> > > > > allocation order, which should be OK and trivial.
> > > > >
> > > > > Signed-off-by: Kairui Song <kasong@...cent.com>
> > > > > ---
> > > > > include/linux/swap.h | 11 ++--
> > > > > mm/swapfile.c | 120 +++++++++++++++++++++++++++----------------
> > > > > 2 files changed, 79 insertions(+), 52 deletions(-)
> > > > ......
> > > > > diff --git a/mm/swapfile.c b/mm/swapfile.c
> > > > > index ae3bd0a862fc..791cd7ed5bdf 100644
> > > > > --- a/mm/swapfile.c
> > > > > +++ b/mm/swapfile.c
> > > > > @@ -116,6 +116,18 @@ static atomic_t proc_poll_event = ATOMIC_INIT(0);
> > > > >
> > > > ......snip....
> > > > > int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order)
> > > > > {
> > > > > int order = swap_entry_order(entry_order);
> > > > > @@ -1211,19 +1251,28 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order)
> > > > > int n_ret = 0;
> > > > > int node;
> > > > >
> > > > > + /* Fast path using percpu cluster */
> > > > > + local_lock(&percpu_swap_cluster.lock);
> > > > > + n_ret = swap_alloc_fast(swp_entries,
> > > > > + SWAP_HAS_CACHE,
> > > > > + order, n_goal);
> > > > > + if (n_ret == n_goal)
> > > > > + goto out;
> > > > > +
> > > > > + n_goal = min_t(int, n_goal - n_ret, SWAP_BATCH);
> > > >
> > > > Here, the behaviour is changed too. In old allocation, partial
> > > > allocation will jump out to return. In this patch, you try the percpu
> > > > cluster firstly, then call scan_swap_map_slots() to try best and will
> > > > jump out even though partial allocation succeed. But the allocation from
> > > > scan_swap_map_slots() could happen on different si device, this looks
> > > > bizarre. Do you think we need reconsider the design?
> > >
> > > Right, that's a behavior change, but only temporarily affects slot cache.
> > > get_swap_pages will only be called with size > 1 when order == 0, and
> > > only by slot cache. (Large order allocation always use size == 1,
> > > other users only uses order == 0 && size == 1). So I didn't' notice
> > > it, as this series is removing slot cache.
> > >
> > > The partial side effect would be "returned slots will be from
> > > different devices" and "slot_cache may get drained faster as
> > > get_swap_pages may return less slots when percpu cluster is drained".
> > > Might be a performance issue but seems slight and trivial, slot cache
> > > can still work. And the next commit will just remove the slot cache,
> > > and the problem will be gone. I think I can add a comment about it
> > > here?
> >
> > By the way, another thing I suddenly think of is the percpu cluster
> > becoming glober over all devices. If one order on the stored si of
> > percpu cluster is used up, then the whole percpu cluster is drained and
> > rewritten. Won't this impact performance compared with the old embedding
> > percpu cluster in each si? In log you said "Ideally non-HDD devices
> > will be rotated if 2M space has been consumed for each order, this seems
> > reasonable.", while in reality it may be very difficult to achieve the
> > 'each 2M space has been consumed for each order', but more often happen
> > when very few of order's space has been consumed, then rewrite percpu.
> > Wonder what I have missed about this point.
>
> Hi Baoquan,
>
> > then the whole percpu cluster is drained and rewritten
>
> Not sure what you mean, SWAP IO doesn't happen in units of clusters,
> cluster is only a management unit for slots, so only allocated / freed
> slot will be written. Discard is a different thing, and this should
> have very little effect on that.
>
> Or you mean the percpu struct array getting updated? It should be even
> faster than before, updating a global percpu variable is easier to
> calculate the offset at runtime, using GS.
Yes, I mean the global percpu array updating.
>
> > n reality it may be very difficult to achieve the 'each 2M space has been consumed for each order',
>
> Very true, but notice for order >= 1, slot cache never worked before.
> And for order == 0, it's very likely that a cluster will have more
> than 64 slots usable. The test result I posted should be a good
> example, and device is very full during the test, and performance is
> basically identical to before. My only concern was about the device
My worry is the global percpu cluster may impact performance among
multiple swap devices. Before, per si percpu cluster will cache the
valid offset in one cluster for each order. For multiple swap devices,
this consumes a little bit more percpu memory. While the new global
percpu cluster could be updated to a different swap device easily only
of one order is available, then the whole array is invalid. That looks a
little drastic cmpared with before.
Yeah, the example you shown looks good. Wonder how many swap devices are
simulated in your example.
> rotating, as slot cache never worked for order >= 1, so the device
> rotates was very frequently. But still seems no one really cared about
> it, mthp swapout is a new thing and the previous rotation rule seems
> even more confusing than this new idea.
I never contact a real product environment with multiple tier and
many swap devices. In reality, with my shallow knowledge, usually only
one swap device is deployed. If that's true in most of time, the old
code or new code is fine, otherwise, seems we may need consider the
impact.
Powered by blists - more mailing lists