Re: [PATCH v2] mm: swap: async free swap slot cache entries

From: Chris Li
Date: Mon Feb 05 2024 - 16:13:06 EST


On Mon, Feb 5, 2024 at 10:15 AM Tim Chen <tim.c.chen@xxxxxxxxxxxxxxx> wrote:
>
> On Sat, 2024-02-03 at 10:12 -0800, Chris Li wrote:
> >
> > > > > {
> > > > > struct swap_slots_cache *cache;
> > > > > @@ -282,17 +298,14 @@ void free_swap_slot(swp_entry_t entry)
> > > > > goto direct_free;
> > > > > }
> > > > > if (cache->n_ret >= SWAP_SLOTS_CACHE_SIZE) {
> > > > > - /*
> > > > > - * Return slots to global pool.
> > > > > - * The current swap_map value is SWAP_HAS_CACHE.
> > > > > - * Set it to 0 to indicate it is available for
> > > > > - * allocation in global pool
> > > > > - */
> > > > > - swapcache_free_entries(cache->slots_ret, cache->n_ret);
> > > > > - cache->n_ret = 0;
> > > > > + spin_unlock_irq(&cache->free_lock);
> > > > > + schedule_work(&cache->async_free);
> > > > > + goto direct_free;
> > > > > }
> > > > > cache->slots_ret[cache->n_ret++] = entry;
> > > > > spin_unlock_irq(&cache->free_lock);
> > > > > + if (cache->n_ret >= SWAP_SLOTS_CACHE_SIZE)
> > > > > + schedule_work(&cache->async_free);
> > >
> > >
> > > I have some concerns about the current patch with the change above.
> > > We could hit the direct_free path very often.
> > >
> > > By delaying the freeing of entries in the return
> > > cache, we have to do more freeing of swap entry one at a time. When
> > > we try to free an entry, we can find the return cache still full, waiting to be freed.
> >
> > You are describing the async free is not working. In that case it will always
> > hit the direct free path one by one.
> >
> > >
> > > So we have fewer batch free of swap entries, resulting in an increase in
> > > number of sis->lock acquisitions overall. This could have the
> > > effect of reducing swap throughput overall when swap is under heavy
> > > operations and sis->lock is contended.
> >
> > I can change the direct free path to free all entries. If the async
> > free hasn't freed up the batch by the time the next swap fault comes in.
> > The new swap fault will take the hit, just free the whole batch. It will behave
> > closer to the original batch free behavior in this path.
> >
> Will that negate the benefit you are looking for?

It should not. In our deployment, the rate of swap faults isn't that
high. It is one of the matrices that gets monitored and controlled. If
the swap fault gets that busy, the app's performance most likely
already starts to suffer from it. We should back off from swapping out
that much.
I expect the normal case, the async free already free up the 64
entries before the next swap fault on the same CPU hits.

> A hack is to double the SWAP_SLOTS_CACHE_SIZE to 128, and trigger the
> background reclaim when entries reach 64. This will allow you to avoid
> the one by one relcaim direct path and hopefully the delayed job
> would have done its job when slots accumulate between 64 and 128.

I would have some concern on that due to higher per CPU memory usage.
We have machines with high CPU count. That would mean more memory
reserved. I actually have a variant of the patch that starts the async
free before it reaches the 64 entries limit. e.g. 60 entries. It will
give some head room to avoid the direct free path for another 4
entries. I did not include that in the patch because it makes things
more complicated, also this code path isn't taking much at all. If it
helps I can resurrect that in the patch as V3.
>
> However, I am unsure how well this hack is under really heavy
> swap load. It means that the background reclaim will need to

In our system, a really heavy swap load is rare and it means something
is already wrong. At that point the app's SLO is likely at risk,
regardless of long tail swap latency. It is already too late to
address it at the swap fault end. We need to address the source of the
problem which is swapping out too much.

> work through a larger backlog and
> hold the sis->lock longer. So if you hit the direct path while
> the background reclaim is underway, you may have longer tail latency
> to acquire the sis->lock.

Chris