Re: [PATCH] mm: madvise: fix uneven accounting of psi

From: Suren Baghdasaryan
Date: Tue Jun 06 2023 - 15:49:01 EST


Hi Charan,

On Tue, Jun 6, 2023 at 7:54 AM Charan Teja Kalla
<quic_charante@xxxxxxxxxxx> wrote:
>
> Thanks Johannes for the detailed review comments...
>
> On 6/5/2023 11:30 PM, Johannes Weiner wrote:
> >> Agree that we shouldn't be really silence the thrashing. My point is we
> >> shouldn't be considering the folios as thrashing If those were getting
> >> reclaim by the user him self through MADV_PAGEOUT under the assumption
> >> that __user knows they are not real working set__. Please let me know
> >> if I am not making sense here.
> > I'm not sure I agree with this. I think it misses the point of what
> > the madvise is actually for.
> >
> > The workingset is defined based on access frequency and available
> > memory. Thrashing is defined as having to read pages back shortly
> > after their eviction.
> >
> > MADV_PAGEOUT is for the application to inform the kernel that it's
> > done accessing the pages, so that the kernel can accelerate their
> > eviction over other pages that may still be in use. This is ultimately
> > meant to REDUCE reclaim and paging.
> >
> > However, in this case, the MADVISE_PAGEOUT evicts pages that are
> > reused after and then refault. It INCREASED reclaim and paging.
> >
> I agree here...
> > Surely that's a problem? And the system would have behaved better
> > without the madvise() in the first place?
> >
> Yes, the system behavior could be much better without this PAGEOUT
> operation...
> > In fact, I would argue that the pressure spike is a great signal for
> > detecting overzealous madvising. If you're redefining the workingset
> > from access frequency to "whatever the user is saying", that will take
> > away an important mechanism to detect advise bugs and unnecessary IO.
> currently wanted to do the PAGEOUT operation but what information lacks
> is if I am really operating on the workingset pages. Had the client
> knows that he is operating on the workingset pages, he could have backed
> off from madvising.
>
> I now note that I shouldn't be defining the workingset from "whatever
> user is saying". But then, IMO, there should be a way from the kernel to
> the user that his madvise operation is being performed on the workingset
> pages.
>
> One way the user can do is monitoring the PSI events while PAGEOUT is
> being performed and he may exclude those VMA's from next time.
>
> Alternatively kernel itself can support it may be through like
> MADV_PAGEOUT_INACTIVE which doesn't pageout the Workingset pages.
>
> Please let me know your opinion about this interface.
>
> This has the usecase on android where it just assumes that 2nd
> background app will most likely to be not used in the future thus
> reclaim those app pages. It works well for most of the times but such
> assumption will go wrong with the usecase I had mentioned.

Hi Folks. Sorry for being late to the party.
Yeah, userspace does not have a crystal ball to predict future user
behavior, so there will always be pathological cases when usual
assumptions and resulting madvise() would make things worse.

I think this discussion can be split into several questions/issues:
1. Inconsistency in how madvise(MADV_PAGEOUT) would affect PSI
calculation when the page is refaulted, based on the path it took
before being evicted by madvise(). In your initial description case
(a) is inconsistent with (b) and (c) and it's probably worth fixing.
IMHO (a) should be made consistent with others, not the other way
around. My reasoning is that page was expelled from the active list,
so it was part of the active workingset.

2. Whether refaults caused by incorrect madvise(MADV_PAGEOUT) should
be counted as workingset refault and affect PSI.
This one I think is trickier. IMHO it should be counted as workingset
refault simply because it was refaulted and it was part of the
workingset. Whether it should affect PSI, which is supposed to be an
indicator of "pressure" is, I think, debatable. With madvise() in the
mix, refault might happen without any real memory pressure... So, the
answer is not obvious to me.

3. Should refaults caused by incorrect madvise(MADV_PAGEOUT) be
distinguished from the ones which were evicted by kernel reclaim
mechanisms.
I can see use for that from userspace to detect incorrect madvise()
and adjust its aggressiveness. I think the API might get a bit complex
because of the need to associate refaults with specific madvise()/VMAs
to understand which hint was incorrect and adjust the behavior.

Hope my feedback is useful and if we can improve Android's userspace
behavior, I'm happy to help make that happen.
Thanks,
Suren.

>
> --Thanks.