Re: [PATCH v6 0/9] variable-order, large folios for anonymous memory

From: David Hildenbrand
Date: Fri Oct 13 2023 - 16:15:45 EST




Yes, I think there are various ways forward regarding that. Or to enable "auto"
mode only once all are "auto", and as soon as one is not "auto", just disable
it. A simple

echo "auto" > /sys/kernel/mm/transparent_hugepage/hugepages-*/enabled

I'm not really a fan, because this implies that you have a period where "auto"
is reported for a size, but its not really in "auto" mode yet.

I think there are various alternatives that are feasible.

For most systems later, you'd want to just "auto" via compile-time CONFIG option as default or via some cmdline option like "transparent_hugepage=auto".



Would do to enable it. Or, have them all be "global" and have a global "auto"
mode as you raised.

echo "global" > /sys/kernel/mm/transparent_hugepage/hugepages-*/enabled
echo "auto" > /sys/kernel/mm/transparent_hugepage/enabled


Again, this isn't atomic either. I tend to prefer my proposal because it
switches atomically - there are no weird intermediate states. Anyway, I guess
the important point is we have demonstrated that your proposed interface could
be extended to support "auto" in future, should we need it.

I don't think the atomic switch is really relevant. But that's probably a separate discussion.

[...]


Just because a small-sized THP is PTE-mapped doesn't tell you anything, really.
What you want to know is if it is "completely" and "consecutively" mapped such
that the HW can actually benefit from it -- if HW even supports it. So
"PTE-mapped THP" is just part of the story. And that's where it gets tricky I
think.

I agree that it's good for debugging, but then maybe it should a) live somewhere
else (debugfs, bucketing below) and b) be consistent with other THPs, meaning we
also want similar stats somewhere.

One idea would be to expose such stats in a R/O fashion like "nr_allocated" or
"nr_hugepages" in /sys/kernel/mm/transparent_hugepage/hugepages-64kB/ and
friends. Of course, maybe tagging them with "anon" prefix.

I see your point, but I don't completely agree with it all. That said, given
your conclusion at the bottom, perhaps we should park the discussion about the
accounting for a separate series in future? Then we can focus on the ABI?

Yes!




I would actually argue for adding similar counters for file-backed memory too
for the same reasons. (I actually posted an independent patch a while back that
did this for file- and anon- memory, bucketted by size. But I think the idea of
the bucketting was NAKed.
For debugging, I *think* it might be valuable to see how many THP of each size
are allocated. Tracking exactly "how is it mapped" is not easy to achieve as we
learned. PMD-mapped was easy, but also requires us to keep doing that tracking
for all eternity ...

Do you have a pointer to the patch set? Did it try to squeeze it into
/proc/meminfo?

I was actually only working on smaps/smaps_rollup, which has been my main tool
for debugging. patches at [1].

[1] https://lore.kernel.org/linux-mm/20230613160950.3554675-1-ryan.roberts@xxxxxxx/


Thanks for the pointer!

[...]


I'll need some help with clasifying them, so showing my working. Final list that
I would propose as strict requirements at bottom.

This is my list with status, as per response to Yu in other thread:

   - David is working on "shared vs exclusive mappings"

Probably "COW reuse support" is a separate item, although my approach would
cover that.

Yeah that's the in the original thread as (2), but I thought we were all agreed
that is not a prerequisite so didn't bring it over here.

Agreed. Having a full list of todo items might be reasonable.



The question is, if the estimate we're using in most code for now would at least
be sufficient to merge it. The estimate is easily wrong, but we do have that
issue with PTE-mapped THP already.

Well as I understand it, at least the NUMA balancing code and khugepaged are
ignoring all folios > order-0. That's probably ok for the occasional PTE-mapped
THP, but I assume it becomes untenable if large folios are the norm. Perhaps we
can modify those paths to work with the current estimators in order to remove
your work from the critical path - is that what you are getting at?

IMHO most of the code that now uses the estimate-logic is really suboptimal, but it's all we have. It's probably interesting to see where the false negative/positives are tolerable for now ... I hate to be at the critical path ;) But I'm getting somewhere slowly but steadily (slowly, because I'm constantly distracted -- and apparently sick).

[...]



Although, since sending that, I've determined that when running kernel
compilation across high number of cores on arm64, the cost of splitting the
folios gets large due to needing to broadcast the extra TLBIs. So I think the
last point on that list may be a prerequisite after all. (I've been able to fix
this by adding support for allocating large folios in the swap file, and
avoiding the split - planning to send RFC this week).

There is also this set of things that you mentioned against "shared vs exclusive
mappings", which I'm not sure if you are planning to cover as part of your work
or if they are follow on things that will need to be done:

(1) Detecting shared folios, to not mess with them while they are shared.
     MADV_PAGEOUT, user-triggered page migration, NUMA hinting, khugepaged ...
     replace cases where folio_estimated_sharers() == 1 would currently be the
     best we can do (and in some cases, page_mapcount() == 1).

And I recently discovered that khugepaged doesn't collapse file-backed pages to
a PMD-size THP if they belong to a large folio, so I'm guessing it may also
suffer the same behaviour for anon memory. I'm not sure if that's what your
"khugepaged ..." comment refers to?

Yes. But I did not look into all the details yet.

"kuhepaged" collapse support to small-sized THP is probably also a very imporant
item, although it might be less relevant than for PMD -- and I consider it
future work. See below.

Yes I agree that it's definitely future work. Nothing regresses from today's
performance if you don't have that.



So taking all that and trying to put together a complete outstanding list for
strict requirements:

   - Shared vs Exclusive Mappings (DavidH)
       - user-triggered page migration
       - NUMA hinting/balancing
       - Enhance khugepaged to collapse to PMD-size from PTE-mapped large folios
   - Compaction of Large Folios (Zi Yan)
   - Swap out small-size THP without Split (Ryan Roberts)

^ that's going to be tough, I can promise. And the only way to live without that
would be khugepaged support. (because that's how it's all working for PMD-sized
THP after all!)

Are you referring specifically to the "swap out" line there? If so, it wasn't my
plan that we would *swap in* large folios - only swap them *out* as large folios

Ah!

to avoid the cost of splitting. Then when they come back in, the come in as
single pages, just like PMD-sized THP, if I've understood things correctly. I
have a patch working and showing the perf improvement as a result. I'm planning
to post an RFC today, hopefully.

I don't see the swap-in side as a problem for the initial patch set. OK, they
come in as single pages, so you lost the potential TLB benefits. But that's no
worse than today's performance so not a regression. And the ratio of SW savings
on THP allocation to HW savings from the TLB is very different for small-sized
THP; much more of the benefit comes from the SW and that's still there.


Once a PMD-sized THP was swapped out and evicted, it will always come back in
order-0 folios. khugeged will re-collapse into PMD-sized chunks. If we could do
that for PTE-sized THP as well ...

Yes, sure, but that's a future improvement, not a requirement to prevent
regression vs today, right?

Yes. You can't just currently assume: as soon as you swap, the whole benefit is gone because you end up will all order-0 pages.

These are certainly not limiting "merge" factors IMHO, but it's certainly one of the things users of distributions will heavily complain about ;)

PMD-sized THP are mostly self-healing in that sense.







Now, these are just my thoughts, and I'm happy about other thoughts.

As always, thanks for taking the time - I really appreciate it.

Sure. Hoping others can comment.

My gut feeling is that it's best to focus on getting the sysfs interface
right+future proof and handling the stats independently. While being a good
debug mechanism, I wouldn't consider these stats a requirement: we don't have
them for file/shmem small-sized thp so far as well.

So maybe really better to handle the stats in meminfo and friends separately.


I'd be very happy with that approach if others are bought in.

Yeah. I'm expecting there still to be discussions, but then we shall also here other proposals. memcg controls are IMHO certainly future work.

--
Cheers,

David / dhildenb