mm: multi-gen LRU: Walk secondary MMU page tables while aging
Subsystems (21)
v4 cover mm: multi-gen LRU: Walk secondary MMU page tables while aging
This patchset makes it possible for MGLRU to consult secondary MMUs while doing aging, not just during eviction. This allows for more accurate reclaim decisions, which is especially important for proactive reclaim.
This series makes the necessary MMU notifier changes to MGLRU and then includes optimizations on top of that. This series also now includes changes to access_tracking_perf_test to verify that aging works properly for pages that are mainly used by KVM.
access_tracking_perf_test also has a mode (-p) to check performance of MGLRU aging while the VM is faulting memory in. Here are some results:
Testing MGLRU aging while vCPUs are faulting in memory on x86 with the TDP MMU. THPs disabled.
The test results varied a decent amount from run to run. I did my best to take representative averages, but nonetheless, the big picture is the important part.
Main takeaways:
- With the optimizations, the workload is much less impacted by the presence of aging.
- With the optimizations, MGLRU is able to do aging much more quickly, especially at 8+ vCPUs on my machine.
./access_tracking_perf_test -p -l -b 1G -v $N_VCPUS # 1G per vCPU
num_vcpus vcpu wall time aging avg pass time
1 (no aging) 0.878822016 n/a 1 (no opt) 0.938250568 0.008236007 1 (opt) 0.912270190 0.007314582
2 (no aging) 0.984959659 n/a 2 (no opt) 1.057880728 0.017989741 2 (opt) 1.037735641 0.013996319
4 (no aging) 1.264881581 n/a 4 (no opt) 1.318849182 0.056164918 4 (opt) 1.314653465 0.029311993
8 (no aging) 1.473883699 n/a 8 (no opt) 1.589441079 0.227419586s 8 (opt) 1.498439592 0.063857740s
16 (no aging) 2.048766096 n/a 16 (no opt) 2.399335597 1.247142841s 16 (opt) 2.000914001 0.121628689s
32 (no aging) 3.316256321 n/a 32 (no opt) 3.955417018 4.347290433 32 (opt) 3.355274507 0.250886289
64 (no aging) 6.498958516 n/a 64 (no opt) 7.127533884 9.815592054 64 (opt) 6.442582168 1.392907010
112 (no aging) 8.498029491 n/a 112 (no opt) 10.21372495 13.47381656 112 (opt) 8.896963554 2.292223850
Previous versions of this series included logic in MGLRU and KVM to support batching the updates to secondary page tables. This version removes this logic, as it was complex and not necessary to enable proactive reclaim. This optimization, as well as the additional optimizations for arm64 and powerpc, can be done in a later series.
Changes since v3[1]:
- Vastly simplified the series (thanks David). Removed mmu notifier batching logic entirely.
- Cleaned up how locking is done for mmu_notifier_test/clear_young (thanks David).
- Look-around is now only done when there are no secondary MMUs subscribed to MMU notifiers.
- CONFIG_LRU_GEN_WALKS_SECONDARY_MMU has been added.
- Fixed the lockless implementation of kvm_{test,}age_gfn for x86 (thanks David).
- Added MGLRU functional and performance tests to access_tracking_perf_test (thanks Axel).
- In v3, an mm would be completely ignored (for aging) if there was a secondary MMU but support for secondary MMU walking was missing. Now, missing secondary MMU walking support simply skips the notifier calls (except for eviction).
- Added a sanity check for that range->lockless and range->on_lock are never both provided for the memslot walk.
For the changes from v2[2] to v3, see v3[1].
This series applies cleanly to mm/mm-unstable and kvm/queue.
[1]: https://lore.kernel.org/linux-mm/20240401232946.1837665-1-jthoughton at google.com/ [2]: https://lore.kernel.org/kvmarm/20230526234435.662652-1-yuzhao at google.com/
James Houghton (7):
mm/Kconfig: Add LRU_GEN_WALKS_SECONDARY_MMU
mm: multi-gen LRU: Have secondary MMUs participate in aging
KVM: Add lockless memslot walk to KVM
KVM: Move MMU lock acquisition for test/clear_young to architecture
KVM: x86: Relax locking for kvm_test_age_gfn and kvm_age_gfn
KVM: arm64: Relax locking for kvm_test_age_gfn and kvm_age_gfn
KVM: selftests: Add multi-gen LRU aging to access_tracking_perf_test
Documentation/admin-guide/mm/multigen_lru.rst | 6 +- arch/arm64/kvm/hyp/pgtable.c | 9 +- arch/arm64/kvm/mmu.c | 30 +- arch/loongarch/kvm/mmu.c | 20 +- arch/mips/kvm/mmu.c | 21 +- arch/powerpc/kvm/book3s.c | 14 +- arch/riscv/kvm/mmu.c | 26 +- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 10 +- arch/x86/kvm/mmu/tdp_iter.h | 27 +- arch/x86/kvm/mmu/tdp_mmu.c | 67 ++- include/linux/kvm_host.h | 1 + include/linux/mmzone.h | 6 +- mm/Kconfig | 8 + mm/rmap.c | 9 +- mm/vmscan.c | 144 +++++-- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/access_tracking_perf_test.c | 365 ++++++++++++++-- .../selftests/kvm/include/lru_gen_util.h | 55 +++ .../testing/selftests/kvm/lib/lru_gen_util.c | 391 ++++++++++++++++++ virt/kvm/kvm_main.c | 38 +- 21 files changed, 1104 insertions(+), 145 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/lru_gen_util.h create mode 100644 tools/testing/selftests/kvm/lib/lru_gen_util.c
base-commit: e0cce98fe279b64f4a7d81b7f5c3a23d80b92fbc
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
PATCH v4 1/7 mm/Kconfig: Add LRU_GEN_WALKS_SECONDARY_MMU
Add this option so that one building the kernel can choose whether or not they want to support walking the secondary MMU.
We want users to be able to blindly enable all lru_gen features to have the best possible performance most of the time. Walking the secondary MMU is mainly useful for be able to do proactive reclaim, and it is possible that doing this can harm VM performance.
This option should be enabled by users who run VMs and also care to do proactive aging/reclaim with MGLRU.
With this config option enabled, a user can still disable the new functionality at runtime through sysfs.
--- mm/Kconfig | 8 ++++++++ 1 file changed, 8 insertions(+)
mm/Kconfig +8 -0
@@ -1222,6 +1222,14 @@ config LRU_GEN_STATS This option has a per-memcg and per-node memory overhead. +config LRU_GEN_WALKS_SECONDARY_MMU + bool "Walk secondary MMUs when aging" + depends on LRU_GEN && LRU_GEN_WALKS_MMU + help + This option allows multi-gen LRU to walk secondary MMU page tables + when aging. This allows for proactive reclaim, but this can reduce + overall performance (e.g. for a KVM VM). + config LRU_GEN_WALKS_MMU def_bool y depends on LRU_GEN && ARCH_HAS_HW_PTE_YOUNG
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
PATCH v4 2/7 mm: multi-gen LRU: Have secondary MMUs participate in aging
In reply to James HoughtonSecondary MMUs are currently consulted for access/age information at eviction time, but before then, we don't get accurate age information. That is, pages that are mostly accessed through a secondary MMU (like guest memory, used by KVM) will always just proceed down to the oldest generation, and then at eviction time, if KVM reports the page to be young, the page will be activated/promoted back to the youngest generation.
Do not do look around if there is a secondary MMU we have to interact with.
The added feature bit (0x8), if disabled, will make MGLRU behave as if there are no secondary MMUs subscribed to MMU notifiers except at eviction time.
--- Documentation/admin-guide/mm/multigen_lru.rst | 6 +- include/linux/mmzone.h | 6 +- mm/rmap.c | 9 +- mm/vmscan.c | 144 ++++++++++++++---- 4 files changed, 123 insertions(+), 42 deletions(-)
Documentation/admin-guide/mm/multigen_lru.rst +5 -1
@@ -48,6 +48,10 @@ Values Components verified on x86 varieties other than Intel and AMD. If it is disabled, the multi-gen LRU will suffer a negligible performance degradation. +0x0008 Continuously clear the accessed bit in secondary MMU page + tables instead of waiting until eviction time. This results in + accurate page age information for pages that are mainly used by + a secondary MMU. [yYnN] Apply to all the components above. ====== ===============================================================
@@ -56,7 +60,7 @@ E.g., echo y >/sys/kernel/mm/lru_gen/enabled cat /sys/kernel/mm/lru_gen/enabled - 0x0007 + 0x000f echo 5 >/sys/kernel/mm/lru_gen/enabled cat /sys/kernel/mm/lru_gen/enabled 0x0005
include/linux/mmzone.h +4 -2
@@ -400,6 +400,7 @@ enum {LRU_GEN_CORE ,LRU_GEN_MM_WALK ,LRU_GEN_NONLEAF_YOUNG , +LRU_GEN_SECONDARY_MMU_WALK , NR_LRU_GEN_CAPS };
@@ -557,7 +558,7 @@ struct lru_gen_memcg {void lru_gen_init_pgdat (struct pglist_data *pgdat);void lru_gen_init_lruvec (struct lruvec *lruvec); -void lru_gen_look_around (struct page_vma_mapped_walk *pvmw); +bool lru_gen_look_around (struct page_vma_mapped_walk *pvmw);void lru_gen_init_memcg (struct mem_cgroup *memcg);void lru_gen_exit_memcg (struct mem_cgroup *memcg);
@@ -576,8 +577,9 @@ static inline void lru_gen_init_lruvec(struct lruvec *lruvec) { } -static inline void lru_gen_look_around (struct page_vma_mapped_walk *pvmw) +static inline bool lru_gen_look_around (struct page_vma_mapped_walk *pvmw) { +return false; }static inline void lru_gen_init_memcg (struct mem_cgroup *memcg)
mm/rmap.c +3 -6
@@ -870,13 +870,10 @@ static bool folio_referenced_one(struct folio *folio,continue ; } -if (pvmw.pte) { -if (lru_gen_enabled () && -pte_young (ptep_get (pvmw.pte))) { -lru_gen_look_around (&pvmw); +if (lru_gen_enabled () && pvmw.pte) { +if (lru_gen_look_around (&pvmw)) referenced++; - } - + }else if (pvmw.pte) {if (ptep_clear_flush_young_notify (vma, address, pvmw.pte)) referenced++;
mm/vmscan.c +111 -33
310 lines not on this page · raw message
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
Yu Zhao
NAKRe: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 8 lines · On Wed, May 29, 2024 at 12:05?PM James Houghton <[email protected]> wrote:
Secondary MMUs are currently consulted for access/age information at eviction time, but before then, we don't get accurate age information. That is, pages that are mostly accessed through a secondary MMU (like guest memory, used by KVM) will always just proceed down to the oldest generation, and then at eviction time, if KVM reports the page to be young, the page will be activated/promoted back to the youngest generation.
Correct, and as I explained offline, this is the only reasonable behavior if we can't locklessly walk secondary MMUs.
Just for the record, the (crude) analogy I used was:
Imagine a large room with many bills ($1, $5, $10, ...) on the floor, but you are only allowed to pick up 10 of them (and put them in your pocket). A smart move would be to survey the room *first and then* pick up the largest ones. But if you are carrying a 500 lbs backpack, you would just want to pick up whichever that's in front of you rather than walk the entire room.
MGLRU should only scan (or lookaround) secondary MMUs if it can be done lockless. Otherwise, it should just fall back to the existing approach, which existed in previous versions but is removed in this version.
Quoted 9 lines
Do not do look around if there is a secondary MMU we have to interact with.
The added feature bit (
0x8), if disabled, will make MGLRU behave as if there are no secondary MMUs subscribed to MMU notifiers except at eviction time.Suggested-by Yu Zhao <[email protected]>Signed-off-by James Houghton <[email protected]>
This is not what I suggested, and it would have been done in the first place if it hadn't regressed the non-lockless case.
NAK.
Sean Christopherson
maintainerRe: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 21 lines · Yu Zhao
Quoted 8 lines · On Wed, May 29, 2024 at 12:05?PM James Houghton <[email protected]> wrote:
Secondary MMUs are currently consulted for access/age information at eviction time, but before then, we don't get accurate age information. That is, pages that are mostly accessed through a secondary MMU (like guest memory, used by KVM) will always just proceed down to the oldest generation, and then at eviction time, if KVM reports the page to be young, the page will be activated/promoted back to the youngest generation.
Correct, and as I explained offline, this is the only reasonable behavior if we can't locklessly walk secondary MMUs.
Just for the record, the (crude) analogy I used was:
Imagine a large room with many bills ($1, $5, $10, ...) on the floor, but you are only allowed to pick up 10 of them (and put them in your pocket). A smart move would be to survey the room *first and then* pick up the largest ones. But if you are carrying a 500 lbs backpack, you would just want to pick up whichever that's in front of you rather than walk the entire room.
Quoted 4 lines
MGLRU should only scan (or lookaround) secondary MMUs if it can be done lockless. Otherwise, it should just fall back to the existing approach, which existed in previous versions but is removed in this version.
IIUC, by "existing approach" you mean completely ignore secondary MMUs that don't implement a lockless walk?
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 23 lines · On Wed, May 29, 2024 at 3:59?PM Sean Christopherson <[email protected]> wrote:
Quoted 21 lines · Yu Zhao
Quoted 8 lines · On Wed, May 29, 2024 at 12:05?PM James Houghton <[email protected]> wrote:
Secondary MMUs are currently consulted for access/age information at eviction time, but before then, we don't get accurate age information. That is, pages that are mostly accessed through a secondary MMU (like guest memory, used by KVM) will always just proceed down to the oldest generation, and then at eviction time, if KVM reports the page to be young, the page will be activated/promoted back to the youngest generation.
Correct, and as I explained offline, this is the only reasonable behavior if we can't locklessly walk secondary MMUs.
Just for the record, the (crude) analogy I used was:
Imagine a large room with many bills ($1, $5, $10, ...) on the floor, but you are only allowed to pick up 10 of them (and put them in your pocket). A smart move would be to survey the room *first and then* pick up the largest ones. But if you are carrying a 500 lbs backpack, you would just want to pick up whichever that's in front of you rather than walk the entire room.
Quoted 7 lines
Quoted 4 lines
MGLRU should only scan (or lookaround) secondary MMUs if it can be done lockless. Otherwise, it should just fall back to the existing approach, which existed in previous versions but is removed in this version.
IIUC, by "existing approach" you mean completely ignore secondary MMUs that don't implement a lockless walk?
No, the existing approach only checks secondary MMUs for LRU folios, i.e., those at the end of the LRU list. It might not find the best candidates (the coldest ones) on the entire list, but it doesn't pay as much for the locking. MGLRU can *optionally* scan MMUs (secondary included) to find the best candidates, but it can only be a win if the scanning incurs a relatively low overhead, e.g., done locklessly for the secondary MMU. IOW, this is a balance between the cost of reclaiming not-so-cold (warm) folios and that of finding the coldest folios.
Scanning host MMUs is likely to be a win because 1) there is usually access locality 2) there is no coarsed locking. If neither holds, scanning secondary MMUs would likely be a loss. And 1) is generally weaker for secondary MMUs, since it's about (guest) physical address space.
Sean Christopherson
maintainerRe: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 32 lines · Yu Zhao
Quoted 23 lines · On Wed, May 29, 2024 at 3:59?PM Sean Christopherson <[email protected]> wrote:
Quoted 21 lines · Yu Zhao
Quoted 8 lines · On Wed, May 29, 2024 at 12:05?PM James Houghton <[email protected]> wrote:
Secondary MMUs are currently consulted for access/age information at eviction time, but before then, we don't get accurate age information. That is, pages that are mostly accessed through a secondary MMU (like guest memory, used by KVM) will always just proceed down to the oldest generation, and then at eviction time, if KVM reports the page to be young, the page will be activated/promoted back to the youngest generation.
Correct, and as I explained offline, this is the only reasonable behavior if we can't locklessly walk secondary MMUs.
Just for the record, the (crude) analogy I used was:
Imagine a large room with many bills ($1, $5, $10, ...) on the floor, but you are only allowed to pick up 10 of them (and put them in your pocket). A smart move would be to survey the room *first and then* pick up the largest ones. But if you are carrying a 500 lbs backpack, you would just want to pick up whichever that's in front of you rather than walk the entire room.Quoted 7 lines
Quoted 4 lines
MGLRU should only scan (or lookaround) secondary MMUs if it can be done lockless. Otherwise, it should just fall back to the existing approach, which existed in previous versions but is removed in this version.
IIUC, by "existing approach" you mean completely ignore secondary MMUs that don't implement a lockless walk?
Quoted 9 lines
No, the existing approach only checks secondary MMUs for LRU folios, i.e., those at the end of the LRU list. It might not find the best candidates (the coldest ones) on the entire list, but it doesn't pay as much for the locking. MGLRU can *optionally* scan MMUs (secondary included) to find the best candidates, but it can only be a win if the scanning incurs a relatively low overhead, e.g., done locklessly for the secondary MMU. IOW, this is a balance between the cost of reclaiming not-so-cold (warm) folios and that of finding the coldest folios.
Gotcha.
I tend to agree with Yu, driving the behavior via a Kconfig may generate simpler _code_, but I think it increases the overall system complexity. E.g. distros will likely enable the Kconfig, and in my experience people using KVM with a distro kernel usually aren't kernel experts, i.e. likely won't know that there's even a decision to be made, let alone be able to make an informed decision.
Having an mmu_notifier hook that is conditionally implemented doesn't seem overly complex, e.g. even if there's a runtime aspect at play, it'd be easy enough for KVM to nullify its mmu_notifier hook during initialization. The hardest part is likely going to be figuring out the threshold for how much overhead is too much.
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 52 lines · On Wed, May 29, 2024 at 3:58?PM Sean Christopherson <[email protected]> wrote:
Quoted 32 lines · Yu Zhao
Quoted 23 lines · On Wed, May 29, 2024 at 3:59?PM Sean Christopherson <[email protected]> wrote:
Quoted 21 lines · Yu Zhao
Quoted 8 lines · On Wed, May 29, 2024 at 12:05?PM James Houghton <[email protected]> wrote:
Secondary MMUs are currently consulted for access/age information at eviction time, but before then, we don't get accurate age information. That is, pages that are mostly accessed through a secondary MMU (like guest memory, used by KVM) will always just proceed down to the oldest generation, and then at eviction time, if KVM reports the page to be young, the page will be activated/promoted back to the youngest generation.
Correct, and as I explained offline, this is the only reasonable behavior if we can't locklessly walk secondary MMUs.
Just for the record, the (crude) analogy I used was:
Imagine a large room with many bills ($1, $5, $10, ...) on the floor, but you are only allowed to pick up 10 of them (and put them in your pocket). A smart move would be to survey the room *first and then* pick up the largest ones. But if you are carrying a 500 lbs backpack, you would just want to pick up whichever that's in front of you rather than walk the entire room.Quoted 7 lines
Quoted 4 lines
MGLRU should only scan (or lookaround) secondary MMUs if it can be done lockless. Otherwise, it should just fall back to the existing approach, which existed in previous versions but is removed in this version.
IIUC, by "existing approach" you mean completely ignore secondary MMUs that don't implement a lockless walk?
Quoted 9 lines
No, the existing approach only checks secondary MMUs for LRU folios, i.e., those at the end of the LRU list. It might not find the best candidates (the coldest ones) on the entire list, but it doesn't pay as much for the locking. MGLRU can *optionally* scan MMUs (secondary included) to find the best candidates, but it can only be a win if the scanning incurs a relatively low overhead, e.g., done locklessly for the secondary MMU. IOW, this is a balance between the cost of reclaiming not-so-cold (warm) folios and that of finding the coldest folios.
Gotcha.
I tend to agree with Yu, driving the behavior via a
Kconfigmay generate simpler _code_, but I think it increases the overall system complexity. E.g. distros will likely enable theKconfig, and in my experience people using KVM with a distro kernel usually aren't kernel experts, i.e. likely won't know that there's even a decision to be made, let alone be able to make an informed decision.
Quoted 4 lines
Having an
mmu_notifierhook that is conditionally implemented doesn't seem overly complex, e.g. even if there's a runtime aspect at play, it'd be easy enough for KVM to nullify itsmmu_notifierhook during initialization. The hardest part is likely going to be figuring out the threshold for how much overhead is too much.
Hi Yu, Sean,
Perhaps I "simplified" this bit of the series a little bit too much. Being able to opportunistically do aging with KVM (even without setting the Kconfig) is valuable.
IIUC, we have the following possibilities:
- v4: aging with KVM is done if the new Kconfig is set.
- v3: aging with KVM is always done.
- v2: aging with KVM is done when the architecture reports that it can probably be done locklessly, set at KVM MMU init time.
- Another possibility?: aging with KVM is only done exactly when it can be done locklessly (i.e., mmu_notifier_test/clear_young() called such that it will not grab any locks).
I like the v4 approach because:
1. We can choose whether or not to do aging with KVM no matter what architecture we're using (without requiring userspace be aware to disable the feature at runtime with sysfs to avoid regressing performance if they don't care about proactive reclaim).
2. If we check the new feature bit (0x8) in sysfs, we can know for sure if aging is meant to be working or not. The selftest changes I made won't work properly unless there is a way to be sure that aging is working with KVM.
For look-around at eviction time:
- v4: done if the main mm PTE was young and no MMU notifiers are subscribed.
- v2/v3: done if the main mm PTE was young or (the SPTE was young and the MMU notifier was lockless/fast).
I made this logic change as part of removing batching.
I'd really appreciate guidance on what the correct thing to do is.
In my mind, what would work great is: by default, do aging exactly when KVM can do it locklessly, and then have a Kconfig to always have MGLRU to do aging with KVM if a user really cares about proactive reclaim (when the feature bit is set). The selftest can check the Kconfig + feature bit to know for sure if aging will be done.
I'm not sure what the exact right thing to do for look-around is.
Thanks for the quick feedback.
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 61 lines · James Houghton
Quoted 52 lines · Sean Christopherson
Quoted 32 lines · Yu Zhao
Quoted 23 lines · Sean Christopherson
Quoted 21 lines · Yu Zhao
Quoted 8 lines · James Houghton
Secondary MMUs are currently consulted for access/age information at eviction time, but before then, we don't get accurate age information. That is, pages that are mostly accessed through a secondary MMU (like guest memory, used by KVM) will always just proceed down to the oldest generation, and then at eviction time, if KVM reports the page to be young, the page will be activated/promoted back to the youngest generation.
Correct, and as I explained offline, this is the only reasonable behavior if we can't locklessly walk secondary MMUs.
Just for the record, the (crude) analogy I used was:
Imagine a large room with many bills ($1, $5, $10, ...) on the floor, but you are only allowed to pick up 10 of them (and put them in your pocket). A smart move would be to survey the room *first and then* pick up the largest ones. But if you are carrying a 500 lbs backpack, you would just want to pick up whichever that's in front of you rather than walk the entire room.Quoted 7 lines
Quoted 4 lines
MGLRU should only scan (or lookaround) secondary MMUs if it can be done lockless. Otherwise, it should just fall back to the existing approach, which existed in previous versions but is removed in this version.
IIUC, by "existing approach" you mean completely ignore secondary MMUs that don't implement a lockless walk?
Quoted 9 lines
No, the existing approach only checks secondary MMUs for LRU folios, i.e., those at the end of the LRU list. It might not find the best candidates (the coldest ones) on the entire list, but it doesn't pay as much for the locking. MGLRU can *optionally* scan MMUs (secondary included) to find the best candidates, but it can only be a win if the scanning incurs a relatively low overhead, e.g., done locklessly for the secondary MMU. IOW, this is a balance between the cost of reclaiming not-so-cold (warm) folios and that of finding the coldest folios.
Gotcha.
I tend to agree with Yu, driving the behavior via a
Kconfigmay generate simpler _code_, but I think it increases the overall system complexity. E.g. distros will likely enable theKconfig, and in my experience people using KVM with a distro kernel usually aren't kernel experts, i.e. likely won't know that there's even a decision to be made, let alone be able to make an informed decision.Quoted 4 lines
Having an
mmu_notifierhook that is conditionally implemented doesn't seem overly complex, e.g. even if there's a runtime aspect at play, it'd be easy enough for KVM to nullify itsmmu_notifierhook during initialization. The hardest part is likely going to be figuring out the threshold for how much overhead is too much.Hi Yu, Sean,
Quoted 7 lines
Perhaps I "simplified" this bit of the series a little bit too much. Being able to opportunistically do aging with KVM (even without setting the
Kconfig) is valuable.IIUC, we have the following possibilities:
- v4: aging with KVM is done if the newKconfigis set.
- v3: aging with KVM is always done.
This is not true -- in v3, MGLRU only scans secondary MMUs if it can be done locklessly on x86. It uses a bitmap to imply this requirement.
Quoted 2 lines
- v2: aging with KVM is done when the architecture reports that it can probably be done locklessly, set at KVM MMU init time.
Not really -- it's only done if it can be done locklessly on both x86 and arm64.
Quoted 3 lines
- Another possibility?: aging with KVM is only done exactly when it can be done locklessly (i.e., mmu_notifier_test/clear_young() called such that it will not grab any locks).
This is exactly the case for v2.
Quoted 9 lines
I like the v4 approach because:
1. We can choose whether or not to do aging with KVM no matter what architecture we're using (without requiring userspace be aware to disable the feature at runtime with sysfs to avoid regressing performance if they don't care about proactive reclaim).
2. If we check the new feature bit (0x8) in sysfs, we can know for sure if aging is meant to be working or not. The selftest changes I made won't work properly unless there is a way to be sure that aging is working with KVM.
I'm not convinced, but it doesn't mean your point of view is invalid. If you fully understand the implications of your design choice and document them, I will not object.
All optimizations in v2 were measured step by step. Even that bitmap, which might be considered overengineered, brought a readily measuarable 4% improvement in memcached throughput on Altra Max swapping to Optane:
Using the bitmap (64 KVM PTEs for each call)
============================================================================================================================
Type Ops/sec Hits/sec Misses/sec Avg. Latency p50
Latency p99 Latency p99.9 Latency KB/sec
----------------------------------------------------------------------------------------------------------------------------
Sets 0.00 --- --- ---
--- --- --- 0.00
Gets 1012801.92 431436.92 14965.11 0.06246
0.04700 0.16700 4.31900 39635.83
Waits 0.00 --- --- ---
--- --- --- ---
Totals 1012801.92 431436.92 14965.11 0.06246
0.04700 0.16700 4.31900 39635.83
Not using the bitmap (1 KVM PTEs for each call)
============================================================================================================================
Type Ops/sec Hits/sec Misses/sec Avg. Latency p50
Latency p99 Latency p99.9 Latency KB/sec
----------------------------------------------------------------------------------------------------------------------------
Sets 0.00 --- --- ---
--- --- --- 0.00
Gets 968210.02 412443.85 14303.89 0.06517
0.04700 0.15900 7.42300 37890.74
Waits 0.00 --- --- ---
--- --- --- ---
Totals 968210.02 412443.85 14303.89 0.06517
0.04700 0.15900 7.42300 37890.74
FlameGraphs with bitmap (1.svg) and without bitmap (2.svg) attached.
What I don't think is acceptable is simplifying those optimizations out without documenting your justifications (I would even call it a design change, rather than simplification, from v3 to v4).
Quoted 4 lines
For look-around at eviction time:
- v4: done if the main mm PTE was young and no MMU notifiers are subscribed.
- v2/v3: done if the main mm PTE was young or (the SPTE was young and the MMU notifier was lockless/fast).
The host and secondary MMUs are two *independent* cases, IMO:
1. lookaround the host MMU if the PTE mapping the folio under reclaim is young.
2. lookaround the secondary MMU if it can be done locklessly.
So the v2/v3 behavior sounds a lot more reasonable to me.
Also a nit -- don't use 'else' in the following case (should_look_around()):
if (foo)
return bar;
else
do_something();Quoted 9 lines
I made this logic change as part of removing batching.
I'd really appreciate guidance on what the correct thing to do is.
In my mind, what would work great is: by default, do aging exactly when KVM can do it locklessly, and then have a
Kconfigto always have MGLRU to do aging with KVM if a user really cares about proactive reclaim (when the feature bit is set). The selftest can check theKconfig+ feature bit to know for sure if aging will be done.
I still don't see how that Kconfig helps. Or why the new static branch isn't enough?
Quoted 3 lines
I'm not sure what the exact right thing to do for look-around is.
Thanks for the quick feedback.
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
On Fri, May 31, 2024 at 12:05:48AM -0600, Yu Zhao wrote:
[...]
Quoted 4 lines
All optimizations in v2 were measured step by step. Even that bitmap, which might be considered overengineered, brought a readily measuarable 4% improvement in memcached throughput on Altra Max swapping to Optane:
That's great, but taking an iterative approach to the problem allows the reviewers and maintainers to come to their own conclusions about each optimization independently. Squashing all of that together and posting the result doesn't allow for this.
Even if we were to take the series as-is, the door is wide open to subsequent improvements.
Quoted 3 lines
What I don't think is acceptable is simplifying those optimizations out without documenting your justifications (I would even call it a design change, rather than simplification, from v3 to v4).
No, sorry, there's nothing wrong with James' approach here.
The discussion that led to the design of v4 happened on list; you were on CC. The general consensus on the KVM side was that the bitmap was complicated and lacked independent justification. There was ample opportunity to voice your concerns before he spent the time on v4.
You seriously cannot fault a contributor for respinning their work based on the provided feedback.
Signature · 2 lines
Thanks, Oliver
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 2 lines · On Fri, May 31, 2024 at 1:03?AM Oliver Upton <[email protected]> wrote:
On Fri, May 31, 2024 at 12:05:48AM -0600, Yu Zhao wrote:
Let me add back what I said earlier:
I'm not convinced, but it doesn't mean your point of view is invalid. If you fully understand the implications of your design choice and document them, I will not object.
Quoted 9 lines
Quoted 4 lines
All optimizations in v2 were measured step by step. Even that bitmap, which might be considered overengineered, brought a readily measuarable 4% improvement in memcached throughput on Altra Max swapping to Optane:
That's great, but taking an iterative approach to the problem allows the reviewers and maintainers to come to their own conclusions about each optimization independently. Squashing all of that together and posting the result doesn't allow for this.
That's your methodology, which I respect: as I said I won't stand in your way.
But mine is backed by data, please do respect that as well, by doing what I asked: document your justifications.
Quoted 8 lines
Even if we were to take the series as-is, the door is wide open to subsequent improvements.
Quoted 3 lines
What I don't think is acceptable is simplifying those optimizations out without documenting your justifications (I would even call it a design change, rather than simplification, from v3 to v4).
No, sorry, there's nothing wrong with James' approach here.
Sorry, are you saying "without documenting your justifications" is nothing wrong? If so, please elaborate.
Quoted 4 lines
The discussion that led to the design of v4 happened on list; you were on CC. The general consensus on the KVM side was that the bitmap was complicated and lacked independent justification. There was ample opportunity to voice your concerns before he spent the time on v4.
Please re-read my previous emails -- I never object to the removal of the bitmap or James' approach.
And please stop making assumptions -- I did voice my concerns with James privately.
Quoted 2 lines
You seriously cannot fault a contributor for respinning their work based on the provided feedback.
Are you saying I faulted James for taking others' feedback? If so, where? And I'll make sure I don't give such an impression in the future.
Also what do you think about the technical flaws and inaccurate understandings I pointed out? You seem to have a strong opinion on your iterate approach, but I hope you didn't choose to overlook the real meat of this discussion.
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 9 lines · Yu Zhao
Quoted 2 lines · On Fri, May 31, 2024 at 1:03?AM Oliver Upton <[email protected]> wrote:
On Fri, May 31, 2024 at 12:05:48AM -0600, Yu Zhao wrote:
Let me add back what I said earlier:
I'm not convinced, but it doesn't mean your point of view is invalid. If you fully understand the implications of your design choice and document them, I will not object.
Thanks, I appreciate the sentiment. Hopefully we can align here.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
All optimizations in v2 were measured step by step. Even that bitmap, which might be considered overengineered, brought a readily measuarable 4% improvement in memcached throughput on Altra Max swapping to Optane:
Quoted 8 lines
Quoted 4 lines
That's great, but taking an iterative approach to the problem allows the reviewers and maintainers to come to their own conclusions about each optimization independently. Squashing all of that together and posting the result doesn't allow for this.
That's your methodology, which I respect: as I said I won't stand in your way.
But mine is backed by data, please do respect that as well,
Data is useful and expected for changes that aim to improve the performance of a system in one way or another. That is, after all, the sole intention of the work, no?
What I'm also looking for is a controlled experiment, where a single independent variable (e.g. locking) can be evaluated against the baseline. All-or-nothing data has limited usefulness.
Quoted 1 line
by doing what I asked: document your justifications.
The justification for a series is against the upstream tree, not some out-of-tree stuff. The cover letter explicitly calls out the decision to simplify the patch series along with performance data I can reproduce on my own systems.
This is a perfect example of how to contribute changes upstream.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
What I don't think is acceptable is simplifying those optimizations out without documenting your justifications (I would even call it a design change, rather than simplification, from v3 to v4).
No, sorry, there's nothing wrong with James' approach here.
Sorry, are you saying "without documenting your justifications" is nothing wrong? If so, please elaborate.
As I mentioned above, the reasoning is adequately documented and the discussion that led to v4 is public. OTOH...
Quoted 10 lines
Quoted 4 lines
The discussion that led to the design of v4 happened on list; you were on CC. The general consensus on the KVM side was that the bitmap was complicated and lacked independent justification. There was ample opportunity to voice your concerns before he spent the time on v4.
Please re-read my previous emails -- I never object to the removal of the bitmap or James' approach.
And please stop making assumptions -- I did voice my concerns with James privately.
^~~~~~~~~
If it happened in private then its no better than having said nothing at all.
Please, keep the conversation on-list next time so we can iron out any disagreements there. Otherwise contributors are put in a *very* awkward situation of mediating the on- and off-list dialogue.
Quoted 4 lines
Quoted 2 lines
You seriously cannot fault a contributor for respinning their work based on the provided feedback.
Are you saying I faulted James for taking others' feedback?
No. Sufficient justification is captured in the public review feedback + series cover letter. Your statement that the approach was changed without justification is unsubstantiated.
Quoted 4 lines
Also what do you think about the technical flaws and inaccurate understandings I pointed out? You seem to have a strong opinion on your iterate approach, but I hope you didn't choose to overlook the real meat of this discussion.
Serious question: are you not receiving my mail or something?
I re-raised my question to you from ages ago about locking on the arm64 MMU. You didn't answer last time, I'd appreciate a reply this time around.
Otherwise I couldn't be bothered about the color of the Kconfig bikeshed and don't have anything meaningful to add there. I think the three of you are trending in the right direction.
Signature · 2 lines
Thanks, Oliver
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
In reply to Yu ZhaoQuoted 19 lines · On Thu, May 30, 2024 at 11:06?PM Yu Zhao <[email protected]> wrote:
Quoted 10 lines · On Wed, May 29, 2024 at 7:08?PM James Houghton <[email protected]> wrote:
Hi Yu, Sean,
Perhaps I "simplified" this bit of the series a little bit too much. Being able to opportunistically do aging with KVM (even without setting the
Kconfig) is valuable.IIUC, we have the following possibilities:
- v4: aging with KVM is done if the newKconfigis set.
- v3: aging with KVM is always done.This is not true -- in v3, MGLRU only scans secondary MMUs if it can be done locklessly on x86. It uses a bitmap to imply this requirement.
Quoted 2 lines
- v2: aging with KVM is done when the architecture reports that it can probably be done locklessly, set at KVM MMU init time.
Quoted 7 lines
Not really -- it's only done if it can be done locklessly on both x86 and arm64.
Quoted 3 lines
- Another possibility?: aging with KVM is only done exactly when it can be done locklessly (i.e., mmu_notifier_test/clear_young() called such that it will not grab any locks).
This is exactly the case for v2.
Thanks for clarifying; sorry for getting this wrong.
Quoted 50 lines
Quoted 10 lines
I like the v4 approach because:
1. We can choose whether or not to do aging with KVM no matter what architecture we're using (without requiring userspace be aware to disable the feature at runtime with sysfs to avoid regressing performance if they don't care about proactive reclaim).
2. If we check the new feature bit (0x8) in sysfs, we can know for sure if aging is meant to be working or not. The selftest changes I made won't work properly unless there is a way to be sure that aging is working with KVM.I'm not convinced, but it doesn't mean your point of view is invalid. If you fully understand the implications of your design choice and document them, I will not object.
All optimizations in v2 were measured step by step. Even that bitmap, which might be considered overengineered, brought a readily measuarable 4% improvement in memcached throughput on Altra Max swapping to Optane:
Using the bitmap (64 KVM PTEs for each call)
============================================================================================================================
Type Ops/sec Hits/sec Misses/sec Avg. Latency p50
Latency p99 Latency p99.9 Latency KB/sec
----------------------------------------------------------------------------------------------------------------------------
Sets 0.00 --- --- ------ --- --- 0.00Gets 1012801.92 431436.92 14965.11 0.06246
0.04700 0.16700 4.31900 39635.83
Waits 0.00 --- --- ---
--- --- --- ---
Totals 1012801.92 431436.92 14965.11 0.06246
0.04700 0.16700 4.31900 39635.83Not using the bitmap (1 KVM PTEs for each call)
============================================================================================================================
Type Ops/sec Hits/sec Misses/sec Avg. Latency p50
Latency p99 Latency p99.9 Latency KB/sec
----------------------------------------------------------------------------------------------------------------------------
Sets 0.00 --- --- ------ --- --- 0.00Gets 968210.02 412443.85 14303.89 0.06517
0.04700 0.15900 7.42300 37890.74
Waits 0.00 --- --- ---
--- --- --- ---
Totals 968210.02 412443.85 14303.89 0.06517
0.04700 0.15900 7.42300 37890.74
Quoted 5 lines
FlameGraphs with bitmap (1.svg) and without bitmap (2.svg) attached.
What I don't think is acceptable is simplifying those optimizations out without documenting your justifications (I would even call it a design change, rather than simplification, from v3 to v4).
I'll put back something similar to what you had before (like a test_clear_young() with a "fast" parameter instead of "bitmap"). I like the idea of having a new mmu notifier, like fast_test_clear_young(), while leaving test_young() and clear_young() unchanged (where "fast" means "prioritize speed over accuracy"). It seems a little more straightforward that way.
Quoted 11 lines
Quoted 5 lines
For look-around at eviction time:
- v4: done if the main mm PTE was young and no MMU notifiers are subscribed.
- v2/v3: done if the main mm PTE was young or (the SPTE was young and the MMU notifier was lockless/fast).The host and secondary MMUs are two *independent* cases, IMO:
1. lookaround the host MMU if the PTE mapping the folio under reclaim is young.
2. lookaround the secondary MMU if it can be done locklessly.So the v2/v3 behavior sounds a lot more reasonable to me.
I'll restore the v2/v3 behavior. I initially removed it because, without batching, we (mostly) lose the spatial locality that, IIUC, look-around is designed to exploit.
Quoted 7 lines
Also a nit -- don't use 'else' in the following case (
should_look_around()):if (foo) return bar; else do_something();
Oh, yes, sorry. I wrote and rewrote should_look_around() quite a few times while trying to figure out what made sense in a no-batching series. I'll fix this.
Quoted 5 lines
Quoted 5 lines
I made this logic change as part of removing batching.
I'd really appreciate guidance on what the correct thing to do is.
Quoted 8 lines
Quoted 5 lines
In my mind, what would work great is: by default, do aging exactly when KVM can do it locklessly, and then have a
Kconfigto always have MGLRU to do aging with KVM if a user really cares about proactive reclaim (when the feature bit is set). The selftest can check theKconfig+ feature bit to know for sure if aging will be done.I still don't see how that
Kconfighelps. Or why the new static branch isn't enough?
Without a special Kconfig, the feature bit just tells us that aging with KVM is possible, not that it will necessarily be done. For the self-test, it'd be good to know exactly when aging is being done or not, so having a Kconfig like LRU_GEN_ALWAYS_WALK_SECONDARY_MMU would help make the self-test set the right expectations for aging.
The Kconfig would also allow a user to know that, no matter what, we're going to get correct age data for VMs, even if, say, we're using the shadow MMU. This is somewhat important for me/Google Cloud. Is that reasonable? Maybe there's a better solution.
Sean Christopherson
maintainerRe: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 10 lines · James Houghton
Quoted 3 lines · On Thu, May 30, 2024 at 11:06?PM Yu Zhao <[email protected]> wrote:
What I don't think is acceptable is simplifying those optimizations out without documenting your justifications (I would even call it a design change, rather than simplification, from v3 to v4).
I'll put back something similar to what you had before (like a
test_clear_young()with a "fast" parameter instead of "bitmap"). I like the idea of having a new mmu notifier, likefast_test_clear_young(), while leavingtest_young()andclear_young()unchanged (where "fast" means "prioritize speed over accuracy").
Those two statements are contradicting each other, aren't they? Anyways, I vote for a "fast only" variant, e.g. test_clear_young_fast_only() or so. gup() has already established that terminology in mm/, so hopefully it would be familiar to readers. We could pass a param, but then the MGLRU code would likely end up doing a bunch of useless indirect calls into secondary MMUs, whereas a dedicated hook allows implementations to nullify the pointer if the API isn't supported for whatever reason.
And pulling in Oliver's comments about locking, I think it's important that the mmu_notifier API express it's requirement that the operation be "fast", not that it be lockless. E.g. if a secondary MMU can guarantee that a lock will be contented only in rare, slow cases, then taking a lock is a-ok. Or a secondary MMU could do try-lock and bail if the lock is contended.
That way KVM can honor the intent of the API with an implementation that works best for KVM _and_ for MGRLU. I'm sure there will be future adjustments and fixes, but that's just more motivation for using something like "fast only" instead of "lockless".
Quoted 19 lines
Quoted 4 lines
Quoted 4 lines
I made this logic change as part of removing batching.
I'd really appreciate guidance on what the correct thing to do is.
Quoted 8 lines
Quoted 5 lines
In my mind, what would work great is: by default, do aging exactly when KVM can do it locklessly, and then have a
Kconfigto always have MGLRU to do aging with KVM if a user really cares about proactive reclaim (when the feature bit is set). The selftest can check theKconfig+ feature bit to know for sure if aging will be done.I still don't see how that
Kconfighelps. Or why the new static branch isn't enough?Without a special
Kconfig, the feature bit just tells us that aging with KVM is possible, not that it will necessarily be done. For the self-test, it'd be good to know exactly when aging is being done or not, so having aKconfiglikeLRU_GEN_ALWAYS_WALK_SECONDARY_MMUwould help make the self-test set the right expectations for aging.
Quoted 3 lines
The
Kconfigwould also allow a user to know that, no matter what, we're going to get correct age data for VMs, even if, say, we're using the shadow MMU.
Heh, unless KVM flushes, you won't get "correct" age data.
Quoted 2 lines
This is somewhat important for me/Google Cloud. Is that reasonable? Maybe there's a better solution.
Hmm, no? There's no reason to use a Kconfig, e.g. if we _really_ want to prioritize accuracy over speed, then a KVM (x86?) module param to have KVM walk nested TDP page tables would give us what we want.
But before we do that, I think we need to perform due dilegence (or provide data) showing that having KVM take mmu_lock for write in the "fast only" API provides better total behavior. I.e. that the additional accuracy is indeed worth the cost.
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 7 lines · On Mon, Jun 3, 2024 at 4:03?PM Sean Christopherson <[email protected]> wrote:
Quoted 5 lines · James Houghton
Quoted 3 lines · On Thu, May 30, 2024 at 11:06?PM Yu Zhao <[email protected]> wrote:
What I don't think is acceptable is simplifying those optimizations out without documenting your justifications (I would even call it a design change, rather than simplification, from v3 to v4).
Quoted 7 lines
Quoted 5 lines
I'll put back something similar to what you had before (like a
test_clear_young()with a "fast" parameter instead of "bitmap"). I like the idea of having a new mmu notifier, likefast_test_clear_young(), while leavingtest_young()andclear_young()unchanged (where "fast" means "prioritize speed over accuracy").Those two statements are contradicting each other, aren't they?
I guess it depends on how you define "similar". :)
Quoted 14 lines
Anyways, I vote
for a "fast only" variant, e.g.test_clear_young_fast_only()or so.gup()has already established that terminology inmm/, so hopefully it would be familiar to readers. We could pass a param, but then the MGLRU code would likely end up doing a bunch of useless indirect calls into secondary MMUs, whereas a dedicated hook allows implementations to nullify the pointer if the API isn't supported for whatever reason.And pulling in Oliver's comments about locking, I think it's important that the
mmu_notifierAPI express it's requirement that the operation be "fast", not that it be lockless. E.g. if a secondary MMU can guarantee that a lock will be contented only in rare, slow cases, then taking a lock is a-ok. Or a secondary MMU could do try-lock and bail if the lock is contended.
Quoted 4 lines
That way KVM can honor the intent of the API with an implementation that works best for KVM _and_ for MGRLU. I'm sure there will be future adjustments and fixes, but that's just more motivation for using something like "fast only" instead of "lockless".
Yes, thanks, this is exactly what I meant. I really should have "only" in the name to signify that it is a requirement that it be fast. Thanks for wording it so clearly.
Quoted 29 lines
Quoted 20 lines
Quoted 5 lines
Quoted 5 lines
I made this logic change as part of removing batching.
I'd really appreciate guidance on what the correct thing to do is.
Quoted 8 lines
Quoted 5 lines
In my mind, what would work great is: by default, do aging exactly when KVM can do it locklessly, and then have a
Kconfigto always have MGLRU to do aging with KVM if a user really cares about proactive reclaim (when the feature bit is set). The selftest can check theKconfig+ feature bit to know for sure if aging will be done.I still don't see how that
Kconfighelps. Or why the new static branch isn't enough?Without a special
Kconfig, the feature bit just tells us that aging with KVM is possible, not that it will necessarily be done. For the self-test, it'd be good to know exactly when aging is being done or not, so having aKconfiglikeLRU_GEN_ALWAYS_WALK_SECONDARY_MMUwould help make the self-test set the right expectations for aging.Quoted 3 lines
The
Kconfigwould also allow a user to know that, no matter what, we're going to get correct age data for VMs, even if, say, we're using the shadow MMU.Heh, unless KVM flushes, you won't get "correct" age data.
Quoted 2 lines
This is somewhat important for me/Google Cloud. Is that reasonable? Maybe there's a better solution.
Quoted 7 lines
Hmm, no? There's no reason to use a
Kconfig, e.g. if we _really_ want to prioritize accuracy over speed, then a KVM (x86?) module param to have KVM walk nested TDP page tables would give us what we want.But before we do that, I think we need to perform due dilegence (or provide data) showing that having KVM take
mmu_lockfor write in the "fast only" API provides better total behavior. I.e. that the additional accuracy is indeed worth the cost.
That sounds good to me. I'll drop the Kconfig. I'm not really sure what to do about the self-test, but that's not really all that important.
Sean Christopherson
maintainerRe: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 8 lines · James Houghton
Quoted 3 lines · On Mon, Jun 3, 2024 at 4:03?PM Sean Christopherson <[email protected]> wrote:
But before we do that, I think we need to perform due dilegence (or provide data) showing that having KVM take
mmu_lockfor write in the "fast only" API provides better total behavior. I.e. that the additional accuracy is indeed worth the cost.That sounds good to me. I'll drop the
Kconfig. I'm not really sure what to do about the self-test, but that's not really all that important.
Enable it only on architectures+setups that are guaranteed to implement the fast-only API? E.g. on x86, it darn well better be active if the TDP MMU is enabled. If the test fails because that doesn't hold true, then we _want_ the failure.
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
In reply to Yu ZhaoQuoted 21 lines · Yu Zhao
Quoted 8 lines · On Wed, May 29, 2024 at 12:05?PM James Houghton <[email protected]> wrote:
Secondary MMUs are currently consulted for access/age information at eviction time, but before then, we don't get accurate age information. That is, pages that are mostly accessed through a secondary MMU (like guest memory, used by KVM) will always just proceed down to the oldest generation, and then at eviction time, if KVM reports the page to be young, the page will be activated/promoted back to the youngest generation.
Correct, and as I explained offline, this is the only reasonable behavior if we can't locklessly walk secondary MMUs.
Just for the record, the (crude) analogy I used was:
Imagine a large room with many bills ($1, $5, $10, ...) on the floor, but you are only allowed to pick up 10 of them (and put them in your pocket). A smart move would be to survey the room *first and then* pick up the largest ones. But if you are carrying a 500 lbs backpack, you would just want to pick up whichever that's in front of you rather than walk the entire room.
Quoted 4 lines
MGLRU should only scan (or lookaround) secondary MMUs if it can be done lockless. Otherwise, it should just fall back to the existing approach, which existed in previous versions but is removed in this version.
Grabbing the MMU lock for write to scan sucks, no argument there. But can you please be specific about the impact of read lock v. RCU in the case of arm64? I had asked about this before and you never replied.
My concern remains that adding support for software table walkers outside of the MMU lock entirely requires more work than just deferring the deallocation to an RCU callback. Walkers that previously assumed 'exclusive' access while holding the MMU lock for write must now cope with volatile PTEs.
Yes, this problem already exists when hardware sets the AF, but the lock-free walker implementation needs to be generic so it can be applied for other PTE bits.
Signature · 2 lines
Thanks, Oliver
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 38 lines · On Fri, May 31, 2024 at 1:24?AM Oliver Upton <[email protected]> wrote:
Quoted 21 lines · Yu Zhao
Quoted 8 lines · On Wed, May 29, 2024 at 12:05?PM James Houghton <[email protected]> wrote:
Secondary MMUs are currently consulted for access/age information at eviction time, but before then, we don't get accurate age information. That is, pages that are mostly accessed through a secondary MMU (like guest memory, used by KVM) will always just proceed down to the oldest generation, and then at eviction time, if KVM reports the page to be young, the page will be activated/promoted back to the youngest generation.
Correct, and as I explained offline, this is the only reasonable behavior if we can't locklessly walk secondary MMUs.
Just for the record, the (crude) analogy I used was:
Imagine a large room with many bills ($1, $5, $10, ...) on the floor, but you are only allowed to pick up 10 of them (and put them in your pocket). A smart move would be to survey the room *first and then* pick up the largest ones. But if you are carrying a 500 lbs backpack, you would just want to pick up whichever that's in front of you rather than walk the entire room.Quoted 4 lines
MGLRU should only scan (or lookaround) secondary MMUs if it can be done lockless. Otherwise, it should just fall back to the existing approach, which existed in previous versions but is removed in this version.
Grabbing the MMU lock for write to scan sucks, no argument there. But can you please be specific about the impact of read lock v. RCU in the case of arm64? I had asked about this before and you never replied.
My concern remains that adding support for software table walkers outside of the MMU lock entirely requires more work than just deferring the deallocation to an RCU callback. Walkers that previously assumed 'exclusive' access while holding the MMU lock for write must now cope with volatile PTEs.
Quoted 3 lines
Yes, this problem already exists when hardware sets the AF, but the lock-free walker implementation needs to be generic so it can be applied for other PTE bits.
Direct reclaim is multi-threaded and each reclaimer can take the mmu lock for read (testing the A-bit) or write (unmapping before paging out) on arm64. The fundamental problem of using the readers-writer lock in this case is priority inversion: the readers have lower priority than the writers, so ideally, we don't want the readers to block the writers at all.
Using my previous (crude) analogy: puting the bill right in front of you (the writers) profits immediately whereas searching for the largest bill (the readers) can be futile.
As I said earlier, I prefer we drop the arm64 support for now, but I will not object to taking the mmu lock for read when clearing the A-bit, as long as we fully understand the problem here and document it clearly.
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 51 lines · On Fri, May 31, 2024 at 1:31?PM Yu Zhao <[email protected]> wrote:
Quoted 38 lines · On Fri, May 31, 2024 at 1:24?AM Oliver Upton <[email protected]> wrote:
Quoted 21 lines · Yu Zhao
Quoted 8 lines · On Wed, May 29, 2024 at 12:05?PM James Houghton <[email protected]> wrote:
Secondary MMUs are currently consulted for access/age information at eviction time, but before then, we don't get accurate age information. That is, pages that are mostly accessed through a secondary MMU (like guest memory, used by KVM) will always just proceed down to the oldest generation, and then at eviction time, if KVM reports the page to be young, the page will be activated/promoted back to the youngest generation.
Correct, and as I explained offline, this is the only reasonable behavior if we can't locklessly walk secondary MMUs.
Just for the record, the (crude) analogy I used was:
Imagine a large room with many bills ($1, $5, $10, ...) on the floor, but you are only allowed to pick up 10 of them (and put them in your pocket). A smart move would be to survey the room *first and then* pick up the largest ones. But if you are carrying a 500 lbs backpack, you would just want to pick up whichever that's in front of you rather than walk the entire room.Quoted 4 lines
MGLRU should only scan (or lookaround) secondary MMUs if it can be done lockless. Otherwise, it should just fall back to the existing approach, which existed in previous versions but is removed in this version.
Grabbing the MMU lock for write to scan sucks, no argument there. But can you please be specific about the impact of read lock v. RCU in the case of arm64? I had asked about this before and you never replied.
My concern remains that adding support for software table walkers outside of the MMU lock entirely requires more work than just deferring the deallocation to an RCU callback. Walkers that previously assumed 'exclusive' access while holding the MMU lock for write must now cope with volatile PTEs.
Quoted 3 lines
Yes, this problem already exists when hardware sets the AF, but the lock-free walker implementation needs to be generic so it can be applied for other PTE bits.
Direct reclaim is multi-threaded and each reclaimer can take the mmu lock for read (testing the A-bit) or write (unmapping before paging out) on arm64. The fundamental problem of using the readers-writer lock in this case is priority inversion: the readers have lower priority than the writers, so ideally, we don't want the readers to block the writers at all.
Quoted 8 lines
Using my previous (crude) analogy: puting the bill right in front of you (the writers) profits immediately whereas searching for the largest bill (the readers) can be futile.
As I said earlier, I prefer we drop the arm64 support for now, but I will not object to taking the mmu lock for read when clearing the A-bit, as long as we fully understand the problem here and document it clearly.
FWIW, Google Cloud has been doing proactive reclaim and kstaled-based aging (a Google-internal page aging daemon, for those outside of Google) for many years on x86 VMs with the A-bit harvesting under the write-lock. So I'm skeptical that making ARM64 lockless is necessary to allow Secondary MMUs to participate in MGLRU aging with acceptable performance for Cloud usecases. I don't even think it's necessary on x86 but it's a simple enough change that we might as well just do it.
I suspect under pathological conditions (host under intense memory pressure and high rate of reclaim occurring) making A-bit harvesting lockless will perform better. But under such conditions VM performance is likely going to suffer regardless. In a Cloud environment we deal with that through other mechanisms to reduce the rate of reclaim and make the host healthy.
For these reasons, I think there's value in giving users the option to enable Secondary MMUs participation MGLRU aging even when A-bit test/clearing is not done locklessly. I believe this was James' intent with the Kconfig. Perhaps a default-off writable module parameter would be better to avoid distros accidentally turning it on?
If and when there is a usecase for optimizing VM performance under pathological reclaim conditions on ARM, we can make it lockless then.
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
Quoted 62 lines · On Fri, May 31, 2024 at 2:06?PM David Matlack <[email protected]> wrote:
Quoted 51 lines · On Fri, May 31, 2024 at 1:31?PM Yu Zhao <[email protected]> wrote:
Quoted 38 lines · On Fri, May 31, 2024 at 1:24?AM Oliver Upton <[email protected]> wrote:
Quoted 21 lines · Yu Zhao
Quoted 8 lines · On Wed, May 29, 2024 at 12:05?PM James Houghton <[email protected]> wrote:
Secondary MMUs are currently consulted for access/age information at eviction time, but before then, we don't get accurate age information. That is, pages that are mostly accessed through a secondary MMU (like guest memory, used by KVM) will always just proceed down to the oldest generation, and then at eviction time, if KVM reports the page to be young, the page will be activated/promoted back to the youngest generation.
Correct, and as I explained offline, this is the only reasonable behavior if we can't locklessly walk secondary MMUs.
Just for the record, the (crude) analogy I used was:
Imagine a large room with many bills ($1, $5, $10, ...) on the floor, but you are only allowed to pick up 10 of them (and put them in your pocket). A smart move would be to survey the room *first and then* pick up the largest ones. But if you are carrying a 500 lbs backpack, you would just want to pick up whichever that's in front of you rather than walk the entire room.Quoted 4 lines
MGLRU should only scan (or lookaround) secondary MMUs if it can be done lockless. Otherwise, it should just fall back to the existing approach, which existed in previous versions but is removed in this version.
Grabbing the MMU lock for write to scan sucks, no argument there. But can you please be specific about the impact of read lock v. RCU in the case of arm64? I had asked about this before and you never replied.
My concern remains that adding support for software table walkers outside of the MMU lock entirely requires more work than just deferring the deallocation to an RCU callback. Walkers that previously assumed 'exclusive' access while holding the MMU lock for write must now cope with volatile PTEs.
Quoted 3 lines
Yes, this problem already exists when hardware sets the AF, but the lock-free walker implementation needs to be generic so it can be applied for other PTE bits.
Direct reclaim is multi-threaded and each reclaimer can take the mmu lock for read (testing the A-bit) or write (unmapping before paging out) on arm64. The fundamental problem of using the readers-writer lock in this case is priority inversion: the readers have lower priority than the writers, so ideally, we don't want the readers to block the writers at all.
Quoted 8 lines
Using my previous (crude) analogy: puting the bill right in front of you (the writers) profits immediately whereas searching for the largest bill (the readers) can be futile.
As I said earlier, I prefer we drop the arm64 support for now, but I will not object to taking the mmu lock for read when clearing the A-bit, as long as we fully understand the problem here and document it clearly.
Quoted 8 lines
FWIW, Google Cloud has been doing proactive reclaim and kstaled-based aging (a Google-internal page aging daemon, for those outside of Google) for many years on x86 VMs with the A-bit harvesting under the write-lock. So I'm skeptical that making ARM64 lockless is necessary to allow Secondary MMUs to participate in MGLRU aging with acceptable performance for Cloud usecases. I don't even think it's necessary on x86 but it's a simple enough change that we might as well just do it.
The obvious caveat here: If MGLRU aging and kstaled aging are substantially different in how frequently they trigger mmu_notifiers, then my analysis may not be correct. I'm hoping Yu you can shed some light on that. I'm also operating under the assumption that Secondary MMUs are only participating in aging, and not look-around (i.e. what is implemented in v4).
Quoted 16 lines
I suspect under pathological conditions (host under intense memory pressure and high rate of reclaim occurring) making A-bit harvesting lockless will perform better. But under such conditions VM performance is likely going to suffer regardless. In a Cloud environment we deal with that through other mechanisms to reduce the rate of reclaim and make the host healthy.
For these reasons, I think there's value in giving users the option to enable Secondary MMUs participation MGLRU aging even when A-bit test/clearing is not done locklessly. I believe this was James' intent with the
Kconfig. Perhaps a default-off writable module parameter would be better to avoid distros accidentally turning it on?If and when there is a usecase for optimizing VM performance under pathological reclaim conditions on ARM, we can make it lockless then.
Re: [PATCH v4 2/7] mm: multi-gen LRU: Have secondary MMUs participate in aging
In reply to Yu ZhaoQuoted 1 line · Yu Zhao
On Fri, May 31, 2024 at 1:24?AM Oliver Upton <[email protected]> wrote:
[...]
Quoted 14 lines
Quoted 10 lines
Grabbing the MMU lock for write to scan sucks, no argument there. But can you please be specific about the impact of read lock v. RCU in the case of arm64? I had asked about this before and you never replied.
My concern remains that adding support for software table walkers outside of the MMU lock entirely requires more work than just deferring the deallocation to an RCU callback. Walkers that previously assumed 'exclusive' access while holding the MMU lock for write must now cope with volatile PTEs.
Quoted 3 lines
Yes, this problem already exists when hardware sets the AF, but the lock-free walker implementation needs to be generic so it can be applied for other PTE bits.
Quoted 6 lines
Direct reclaim is multi-threaded and each reclaimer can take the mmu lock for read (testing the A-bit) or write (unmapping before paging out) on arm64. The fundamental problem of using the readers-writer lock in this case is priority inversion: the readers have lower priority than the writers, so ideally, we don't want the readers to block the writers at all.
So we already have this sort of problem of stage-2 fault handling v. secondary MMU invalidations, which is why I've been doubtful of the perceived issue. In fact, I'd argue that needing to wait for faults is worse than aging participation since those can be trivially influenced by userspace/guest.
In any case, we shouldn't ever be starved since younger readers cannot enter the critical section with a pending writer.
Quoted 4 lines
As I said earlier, I prefer we drop the arm64 support for now, but I will not object to taking the mmu lock for read when clearing the A-bit, as long as we fully understand the problem here and document it clearly.
I'd be convinced of this if there's data that shows read lock acquisition is in fact consequential. Otherwise, I'm not sure the added complexity of RCU table walkers (per my statement above) is worth the effort / maintenance burden.
Signature · 2 lines
Thanks, Oliver
PATCH v4 3/7 KVM: Add lockless memslot walk to KVM
In reply to James HoughtonProvide flexibility to the architecture to synchronize as optimally as they can instead of always taking the MMU lock for writing.
The immediate application is to allow architectures to implement the test/clear_young MMU notifiers more cheaply.
--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 38 +++++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 13 deletions(-)
include/linux/kvm_host.h +1 -0
@@ -266,6 +266,7 @@ struct kvm_gfn_range {gfn_t end;union kvm_mmu_notifier_arg arg;bool may_block; +bool lockless; };bool kvm_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range);bool kvm_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range);
virt/kvm/kvm_main.c +25 -13
@@ -558,6 +558,7 @@ struct kvm_mmu_notifier_range {on_lock_fn_t on_lock;bool flush_on_ret;bool may_block; +bool lockless; };/*
@@ -612,6 +613,10 @@ static __always_inline kvm_mn_ret_t __kvm_handle_hva_range(struct kvm *kvm, IS_KVM_NULL_FN(range->handler)))return r; +/* on_lock will never be called for lockless walks */ +if (WARN_ON_ONCE (range->lockless && !IS_KVM_NULL_FN (range->on_lock))) +return r; + idx =srcu_read_lock (&kvm->srcu);for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) {
@@ -643,15 +648,18 @@ static __always_inline kvm_mn_ret_t __kvm_handle_hva_range(struct kvm *kvm, gfn_range.start =hva_to_gfn_memslot (hva_start, slot); gfn_range.end =hva_to_gfn_memslot (hva_end +PAGE_SIZE -1 , slot); gfn_range.slot = slot; + gfn_range.lockless = range->lockless;if (!r.found_memslot) { r.found_memslot = true; -KVM_MMU_LOCK (kvm); -if (!IS_KVM_NULL_FN (range->on_lock)) - range->on_lock (kvm); - -if (IS_KVM_NULL_FN (range->handler)) -break ; +if (!range->lockless) { +KVM_MMU_LOCK (kvm); +if (!IS_KVM_NULL_FN (range->on_lock)) + range->on_lock (kvm); + +if (IS_KVM_NULL_FN (range->handler)) +break ; + } } r.ret |= range->handler (kvm, &gfn_range); }
@@ -660,7 +668,7 @@ static __always_inline kvm_mn_ret_t __kvm_handle_hva_range(struct kvm *kvm,if (range->flush_on_ret && r.ret)kvm_flush_remote_tlbs (kvm); -if (r.found_memslot) +if (r.found_memslot && !range->lockless)KVM_MMU_UNLOCK (kvm);srcu_read_unlock (&kvm->srcu, idx);
@@ -686,10 +694,12 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn,return __kvm_handle_hva_range (kvm, &range).ret; } -static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn, -unsigned long start, -unsigned long end, -gfn_handler_t handler) +static __always_inline int kvm_handle_hva_range_no_flush( +struct mmu_notifier *mn, +unsigned long start, +unsigned long end, +gfn_handler_t handler, +bool lockless) {struct kvm *kvm =mmu_notifier_to_kvm (mn);const struct kvm_mmu_notifier_range range = {
@@ -699,6 +709,7 @@ static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn .on_lock = (void *)kvm_null_fn, .flush_on_ret = false, .may_block = false, + .lockless = lockless, };return __kvm_handle_hva_range (kvm, &range).ret;
@@ -889,7 +900,8 @@ static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn,* cadence. If we find this inaccurate, we might come up with a * more sophisticated heuristic later. */ -return kvm_handle_hva_range_no_flush (mn, start, end, kvm_age_gfn); +return kvm_handle_hva_range_no_flush (mn, start, end, + kvm_age_gfn, false); } static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
@@ -899,7 +911,7 @@ static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,trace_kvm_test_age_hva (address);return kvm_handle_hva_range_no_flush (mn, address, address +1 , - kvm_test_age_gfn); + kvm_test_age_gfn, false); } static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
Sean Christopherson
maintainerRe: [PATCH v4 3/7] KVM: Add lockless memslot walk to KVM
Quoted 4 lines · James Houghton
virt/kvm/kvm_main.c +0
-0@@ -686,10 +694,12 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn,return __kvm_handle_hva_range (kvm, &range).ret; }
virt/kvm/kvm_main.c static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, · line 702
Quoted 10 lines
virt/kvm/kvm_main.c +6
-4-static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn, -unsigned long start, -unsigned long end, -gfn_handler_t handler) +static __always_inline int kvm_handle_hva_range_no_flush( +struct mmu_notifier *mn, +unsigned long start, +unsigned long end, +gfn_handler_t handler, +bool lockless)
Unnecessary and unwanted style change.
virt/kvm/kvm_main.c static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn · line 712
Quoted 8 lines
virt/kvm/kvm_main.c +1
-0{struct kvm *kvm =mmu_notifier_to_kvm (mn);const struct kvm_mmu_notifier_range range = {@@ -699,6 +709,7 @@ static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn .on_lock = (void *)kvm_null_fn, .flush_on_ret = false, .may_block = false, + .lockless = lockless,
Why add @lockess to kvm_handle_hva_range_no_flush()? Both callers immediately pass %false, and conceptually, locking is always optional for a "no flush" variant.
Quoted 25 lines
virt/kvm/kvm_main.c +3
-2};return __kvm_handle_hva_range (kvm, &range).ret;@@ -889,7 +900,8 @@ static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn,* cadence. If we find this inaccurate, we might come up with a * more sophisticated heuristic later. */ -return kvm_handle_hva_range_no_flush (mn, start, end, kvm_age_gfn); +return kvm_handle_hva_range_no_flush (mn, start, end, + kvm_age_gfn, false); } static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,@@ -899,7 +911,7 @@ static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,trace_kvm_test_age_hva (address);return kvm_handle_hva_range_no_flush (mn, address, address +1 , - kvm_test_age_gfn); + kvm_test_age_gfn, false); } static void kvm_mmu_notifier_release(struct mmu_notifier *mn,Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
Re: [PATCH v4 3/7] KVM: Add lockless memslot walk to KVM
Quoted 6 lines · On Wed, May 29, 2024 at 2:51?PM Sean Christopherson <[email protected]> wrote:
Quoted 4 lines · James Houghton
virt/kvm/kvm_main.c +0
-0@@ -686,10 +694,12 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn,return __kvm_handle_hva_range (kvm, &range).ret; }
Quoted 12 lines
virt/kvm/kvm_main.c static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, · line 702
Quoted 10 lines
virt/kvm/kvm_main.c +6
-4-static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn, -unsigned long start, -unsigned long end, -gfn_handler_t handler) +static __always_inline int kvm_handle_hva_range_no_flush( +struct mmu_notifier *mn, +unsigned long start, +unsigned long end, +gfn_handler_t handler, +bool lockless)Unnecessary and unwanted style change.
Sorry -- this will be fixed.
Quoted 12 lines
virt/kvm/kvm_main.c static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn · line 712
Quoted 9 lines
virt/kvm/kvm_main.c +1
-0{struct kvm *kvm =mmu_notifier_to_kvm (mn);const struct kvm_mmu_notifier_range range = {@@ -699,6 +709,7 @@ static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn .on_lock = (void *)kvm_null_fn, .flush_on_ret = false, .may_block = false, + .lockless = lockless,Why add @lockess to
kvm_handle_hva_range_no_flush()? Both callers immediately pass %false, and conceptually, locking is always optional for a "no flush" variant.
Right, this isn't needed in this patch. But I think I need it eventually (like, in the next patch), so I'll move it where it is really needed.
Quoted 26 lines
Quoted 26 lines
virt/kvm/kvm_main.c +3
-3};return __kvm_handle_hva_range (kvm, &range).ret;@@ -889,7 +900,8 @@ static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn,* cadence. If we find this inaccurate, we might come up with a * more sophisticated heuristic later. */ -return kvm_handle_hva_range_no_flush (mn, start, end, kvm_age_gfn); +return kvm_handle_hva_range_no_flush (mn, start, end, + kvm_age_gfn, false); } static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,@@ -899,7 +911,7 @@ static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,trace_kvm_test_age_hva (address);return kvm_handle_hva_range_no_flush (mn, address, address +1 , - kvm_test_age_gfn); + kvm_test_age_gfn, false); } static void kvm_mmu_notifier_release(struct mmu_notifier *mn, --2.45.1.288.g0e0cd299f1-goog
PATCH v4 4/7 KVM: Move MMU lock acquisition for test/clear_young to architecture
In reply to James HoughtonFor implementation mmu_notifier_{test,clear}_young, the KVM memslot walker used to take the MMU lock for us. Now make the architectures take it themselves.
Don't relax locking for any architecture except powerpc e500; its implementations of kvm_age_gfn and kvm_test_age_gfn simply return false, so there is no need to grab the KVM MMU lock.
--- arch/arm64/kvm/mmu.c | 30 ++++++++++++++++++++++-------- arch/loongarch/kvm/mmu.c | 20 +++++++++++++++----- arch/mips/kvm/mmu.c | 21 ++++++++++++++++----- arch/powerpc/kvm/book3s.c | 14 ++++++++++++-- arch/riscv/kvm/mmu.c | 26 ++++++++++++++++++++------ arch/x86/kvm/mmu/mmu.c | 8 ++++++++ virt/kvm/kvm_main.c | 4 ++-- 7 files changed, 95 insertions(+), 28 deletions(-)
arch/arm64/kvm/mmu.c +22 -8
@@ -1773,25 +1773,39 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)bool kvm_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) {u64 size = (range->end - range->start) <<PAGE_SHIFT ; +bool young = false; + +write_lock (&kvm->mmu_lock);if (!kvm->arch.mmu.pgt) -return false; + goto out; -return kvm_pgtable_stage2_test_clear_young (kvm->arch.mmu.pgt, - range->start <<PAGE_SHIFT , - size, true); + young =kvm_pgtable_stage2_test_clear_young (kvm->arch.mmu.pgt, + range->start <<PAGE_SHIFT , + size, true); + +out: +write_unlock (&kvm->mmu_lock); +return young; }bool kvm_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) {u64 size = (range->end - range->start) <<PAGE_SHIFT ; +bool young = false; + +write_lock (&kvm->mmu_lock);if (!kvm->arch.mmu.pgt) -return false; + goto out; -return kvm_pgtable_stage2_test_clear_young (kvm->arch.mmu.pgt, - range->start <<PAGE_SHIFT , - size, false); + young =kvm_pgtable_stage2_test_clear_young (kvm->arch.mmu.pgt, + range->start <<PAGE_SHIFT , + size, false); + +out: +write_unlock (&kvm->mmu_lock); +return young; }phys_addr_t kvm_mmu_get_httbr (void )
arch/loongarch/kvm/mmu.c +15 -5
@@ -497,24 +497,34 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)bool kvm_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) {kvm_ptw_ctx ctx; +bool young; + +spin_lock (&kvm->mmu_lock); ctx.flag =0 ; ctx.ops = kvm_mkold_pte;kvm_ptw_prepare (kvm, &ctx); -return kvm_ptw_top (kvm->arch.pgd, range->start <<PAGE_SHIFT , + young =kvm_ptw_top (kvm->arch.pgd, range->start <<PAGE_SHIFT , range->end <<PAGE_SHIFT , &ctx); + +spin_unlock (&kvm->mmu_lock); +return young; }bool kvm_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) {gpa_t gpa = range->start <<PAGE_SHIFT ; -kvm_pte_t *ptep =kvm_populate_gpa (kvm,NULL , gpa,0 ); +kvm_pte_t *ptep; +bool young; -if (ptep &&kvm_pte_present (NULL , ptep) &&kvm_pte_young (*ptep)) -return true; +spin_lock (&kvm->mmu_lock); + ptep =kvm_populate_gpa (kvm,NULL , gpa,0 ); -return false; + young = ptep &&kvm_pte_present (NULL , ptep) &&kvm_pte_young (*ptep); + +spin_unlock (&kvm->mmu_lock); +return young; }/*
arch/mips/kvm/mmu.c +16 -5
@@ -446,17 +446,28 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)bool kvm_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) { -return kvm_mips_mkold_gpa_pt (kvm, range->start, range->end); +bool young; + +spin_lock (&kvm->mmu_lock); + young =kvm_mips_mkold_gpa_pt (kvm, range->start, range->end); +spin_unlock (&kvm->mmu_lock); +return young; }bool kvm_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) {gpa_t gpa = range->start <<PAGE_SHIFT ; -pte_t *gpa_pte =kvm_mips_pte_for_gpa (kvm,NULL , gpa); +pte_t *gpa_pte; +bool young = false; -if (!gpa_pte) -return false; -return pte_young (*gpa_pte); +spin_lock (&kvm->mmu_lock); + gpa_pte =kvm_mips_pte_for_gpa (kvm,NULL , gpa); + +if (gpa_pte) + young =pte_young (*gpa_pte); + +spin_unlock (&kvm->mmu_lock); +return young; }/**
arch/powerpc/kvm/book3s.c +12 -2
@@ -887,12 +887,22 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)bool kvm_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) { -return kvm->arch.kvm_ops->age_gfn (kvm, range); +bool young; + +spin_lock (&kvm->mmu_lock); + young = kvm->arch.kvm_ops->age_gfn (kvm, range); +spin_unlock (&kvm->mmu_lock); +return young; }bool kvm_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) { -return kvm->arch.kvm_ops->test_age_gfn (kvm, range); +bool young; + +spin_lock (&kvm->mmu_lock); + young = kvm->arch.kvm_ops->test_age_gfn (kvm, range); +spin_unlock (&kvm->mmu_lock); +return young; }int kvmppc_core_init_vm (struct kvm *kvm)
arch/riscv/kvm/mmu.c +20 -6
@@ -555,17 +555,24 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)pte_t *ptep;u32 ptep_level =0 ;u64 size = (range->end - range->start) <<PAGE_SHIFT ; +bool young = false; + +spin_lock (&kvm->mmu_lock);if (!kvm->arch.pgd) -return false; + goto out;WARN_ON (size !=PAGE_SIZE && size !=PMD_SIZE && size !=PUD_SIZE );if (!gstage_get_leaf_entry (kvm, range->start <<PAGE_SHIFT , &ptep, &ptep_level)) -return false; + goto out; + + young =ptep_test_and_clear_young (NULL ,0 , ptep); -return ptep_test_and_clear_young (NULL ,0 , ptep); +out: +spin_unlock (&kvm->mmu_lock); +return young; }bool kvm_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range)
@@ -573,17 +580,24 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)pte_t *ptep;u32 ptep_level =0 ;u64 size = (range->end - range->start) <<PAGE_SHIFT ; +bool young = false; + +spin_lock (&kvm->mmu_lock);if (!kvm->arch.pgd) -return false; + goto out;WARN_ON (size !=PAGE_SIZE && size !=PMD_SIZE && size !=PUD_SIZE );if (!gstage_get_leaf_entry (kvm, range->start <<PAGE_SHIFT , &ptep, &ptep_level)) -return false; + goto out; + + young =pte_young (ptep_get (ptep)); -return pte_young (ptep_get (ptep)); +out: +spin_unlock (&kvm->mmu_lock); +return young; } int kvm_riscv_gstage_map(struct kvm_vcpu *vcpu,
arch/x86/kvm/mmu/mmu.c +8 -0
@@ -1630,12 +1630,16 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) {bool young = false; +write_lock (&kvm->mmu_lock); +if (kvm_memslots_have_rmaps (kvm)) young =kvm_handle_gfn_range (kvm, range, kvm_age_rmap);if (tdp_mmu_enabled) young |=kvm_tdp_mmu_age_gfn_range (kvm, range); +write_unlock (&kvm->mmu_lock); +return young; }
@@ -1643,12 +1647,16 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) {bool young = false; +write_lock (&kvm->mmu_lock); +if (kvm_memslots_have_rmaps (kvm)) young =kvm_handle_gfn_range (kvm, range, kvm_test_age_rmap);if (tdp_mmu_enabled) young |=kvm_tdp_mmu_test_age_gfn (kvm, range); +write_unlock (&kvm->mmu_lock); +return young; }
virt/kvm/kvm_main.c +2 -2
@@ -901,7 +901,7 @@ static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn,* more sophisticated heuristic later. */ return kvm_handle_hva_range_no_flush (mn, start, end, - kvm_age_gfn, false); + kvm_age_gfn, true); } static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
@@ -911,7 +911,7 @@ static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,trace_kvm_test_age_hva (address);return kvm_handle_hva_range_no_flush (mn, address, address +1 , - kvm_test_age_gfn, false); + kvm_test_age_gfn, true); } static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
Sean Christopherson
maintainerRe: [PATCH v4 4/7] KVM: Move MMU lock acquisition for test/clear_young to architecture
Quoted 3 lines · James Houghton
For implementation mmu_notifier_{test,clear}_young, the KVM memslot walker used to take the MMU lock for us. Now make the architectures take it themselves.
Hmm, *forcing* architectures to take mmu_lock is a step backwards. Rather than add all of this churn, what about adding CONFIG_KVM_MMU_NOTIFIER_LOCKLESS, e.g.
static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn,
unsigned long start, unsigned long end, gfn_handler_t handler)
{
struct kvm *kvm = mmu_notifier_to_kvm(mn);
const struct kvm_mmu_notifier_range range = {
.start = start,
.end = end,
.handler = handler,
.on_lock = (void *)kvm_null_fn,
.flush_on_ret = false,
.may_block = false,
.lockless = IS_ENABLED(CONFIG_KVM_MMU_NOTIFIER_LOCKLESS),
};
return __kvm_handle_hva_range(kvm, &range).ret;
}Re: [PATCH v4 4/7] KVM: Move MMU lock acquisition for test/clear_young to architecture
Quoted 25 lines · On Wed, May 29, 2024 at 2:55?PM Sean Christopherson <[email protected]> wrote:
Quoted 3 lines · James Houghton
For implementation mmu_notifier_{test,clear}_young, the KVM memslot walker used to take the MMU lock for us. Now make the architectures take it themselves.
Hmm, *forcing* architectures to take
mmu_lockis a step backwards. Rather than add all of this churn, what about addingCONFIG_KVM_MMU_NOTIFIER_LOCKLESS, e.g.static
__always_inlineint kvm_handle_hva_range_no_flush(structmmu_notifier*mn,
unsigned long start, unsigned long end,gfn_handler_thandler)
{
struct kvm *kvm =mmu_notifier_to_kvm(mn);
const structkvm_mmu_notifier_rangerange = {
.start = start,
.end = end,
.handler = handler,
.on_lock = (void *)kvm_null_fn,
.flush_on_ret = false,
.may_block = false,
.lockless =IS_ENABLED(CONFIG_KVM_MMU_NOTIFIER_LOCKLESS),
};
Quoted 2 lines
return
__kvm_handle_hva_range(kvm, &range).ret;
}
Thanks Sean, yes this is a lot better. I will do this for v5.
PATCH v4 5/7 KVM: x86: Relax locking for kvm_test_age_gfn and kvm_age_gfn
In reply to James HoughtonWalk the TDP MMU in an RCU read-side critical section. This requires a way to do RCU-safe walking of the tdp_mmu_roots; do this with a new macro. The PTE modifications are now done atomically, and kvm_tdp_mmu_spte_need_atomic_write() has been updated to account for the fact that kvm_age_gfn can now lockless update the accessed bit and the R/X bits).
If the cmpxchg for marking the spte for access tracking fails, we simply retry if the spte is still a leaf PTE. If it isn't, we return false to continue the walk.
Harvesting age information from the shadow MMU is still done while holding the MMU write lock.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 18 ++++----- arch/x86/kvm/mmu/tdp_iter.h | 27 +++++++------ arch/x86/kvm/mmu/tdp_mmu.c | 67 +++++++++++++++++++++++++-------- 4 files changed, 76 insertions(+), 37 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1438,6 +1438,7 @@ struct kvm_arch {* tdp_mmu_page set. * * For reads, this list is protected by: +* RCU alone or * the MMU lock in read mode + RCU or * the MMU lock in write mode *
arch/x86/kvm/mmu/mmu.c +8 -10
@@ -1630,16 +1630,15 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) {bool young = false; -write_lock (&kvm->mmu_lock); - -if (kvm_memslots_have_rmaps (kvm)) +if (kvm_memslots_have_rmaps (kvm)) { +write_lock (&kvm->mmu_lock); young =kvm_handle_gfn_range (kvm, range, kvm_age_rmap); +write_unlock (&kvm->mmu_lock); + }if (tdp_mmu_enabled) young |=kvm_tdp_mmu_age_gfn_range (kvm, range); -write_unlock (&kvm->mmu_lock); -return young; }
@@ -1647,16 +1646,15 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) {bool young = false; -write_lock (&kvm->mmu_lock); - -if (kvm_memslots_have_rmaps (kvm)) +if (kvm_memslots_have_rmaps (kvm)) { +write_lock (&kvm->mmu_lock); young =kvm_handle_gfn_range (kvm, range, kvm_test_age_rmap); +write_unlock (&kvm->mmu_lock); + }if (tdp_mmu_enabled) young |=kvm_tdp_mmu_test_age_gfn (kvm, range); -write_unlock (&kvm->mmu_lock); -return young; }
arch/x86/kvm/mmu/tdp_iter.h +15 -12
@@ -24,16 +24,24 @@ static inline u64 kvm_tdp_mmu_write_spte_atomic(tdp_ptep_t sptep, u64 new_spte)return xchg (rcu_dereference (sptep), new_spte); } +static inline u64 tdp_mmu_clear_spte_bits_atomic (tdp_ptep_t sptep,u64 mask) +{ +atomic64_t *sptep_atomic = (atomic64_t *)rcu_dereference (sptep); + +return (u64 )atomic64_fetch_and (~mask, sptep_atomic); +} +static inline void __kvm_tdp_mmu_write_spte (tdp_ptep_t sptep,u64 new_spte) {WRITE_ONCE (*rcu_dereference (sptep), new_spte); }/* -* SPTEs must be modified atomically if they are shadow-present, leaf -* SPTEs, and have volatile bits, i.e. has bits that can be set outside -* of mmu_lock. The Writable bit can be set by KVM's fast page fault -* handler, and Accessed and Dirty bits can be set by the CPU. +* SPTEs must be modified atomically if they have bits that can be set outside +* of the mmu_lock. This can happen for any shadow-present leaf SPTEs, as the +* Writable bit can be set by KVM's fast page fault handler, the Accessed and +* Dirty bits can be set by the CPU, and the Accessed and R/X bits can be +* cleared by age_gfn_range. * * Note, non-leaf SPTEs do have Accessed bits and those bits are * technically volatile, but KVM doesn't consume the Accessed bit of
@@ -44,8 +52,7 @@ static inline void __kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 new_spte)static inline bool kvm_tdp_mmu_spte_need_atomic_write (u64 old_spte,int level) {return is_shadow_present_pte (old_spte) && -is_last_spte (old_spte, level) && -spte_has_volatile_bits (old_spte); +is_last_spte (old_spte, level); } static inline u64 kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 old_spte,
@@ -61,12 +68,8 @@ static inline u64 kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 old_spte,static inline u64 tdp_mmu_clear_spte_bits (tdp_ptep_t sptep,u64 old_spte,u64 mask,int level) { -atomic64_t *sptep_atomic; - -if (kvm_tdp_mmu_spte_need_atomic_write (old_spte, level)) { - sptep_atomic = (atomic64_t *)rcu_dereference (sptep); -return (u64 )atomic64_fetch_and (~mask, sptep_atomic); - } +if (kvm_tdp_mmu_spte_need_atomic_write (old_spte, level)) +return tdp_mmu_clear_spte_bits_atomic (sptep, mask);__kvm_tdp_mmu_write_spte (sptep, old_spte & ~mask);return old_spte;
arch/x86/kvm/mmu/tdp_mmu.c +52 -15
112 lines not on this page · raw message
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
PATCH v4 6/7 KVM: arm64: Relax locking for kvm_test_age_gfn and kvm_age_gfn
In reply to James HoughtonReplace the MMU write locks for read locks.
Grabbing the read lock instead of the write lock is safe because the only requirement we have is that the stage-2 page tables do not get deallocated while we are walking them. The stage2_age_walker() callback is safe to race with itself; update the comment to reflect the synchronization change.
--- arch/arm64/kvm/hyp/pgtable.c | 9 ++++----- arch/arm64/kvm/mmu.c | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-)
arch/arm64/kvm/hyp/pgtable.c +4 -5
@@ -1319,10 +1319,8 @@ static int stage2_age_walker(const struct kvm_pgtable_visit_ctx *ctx, data->young = true;/* -* stage2_age_walker() is always called while holding the MMU lock for -* write, so this will always succeed. Nonetheless, this deliberately -* follows the race detection pattern of the other stage-2 walkers in -* case the locking mechanics of the MMU notifiers is ever changed. +* This walk may not be exclusive; the PTE is permitted to change +* from under us. */ if (data->mkold && !stage2_try_set_pte (ctx, new))return -EAGAIN ;
@@ -1345,7 +1343,8 @@ bool kvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr,struct kvm_pgtable_walker walker = { .cb = stage2_age_walker, .arg = &data, - .flags =KVM_PGTABLE_WALK_LEAF , + .flags =KVM_PGTABLE_WALK_LEAF | +KVM_PGTABLE_WALK_SHARED , };WARN_ON (kvm_pgtable_walk (pgt, addr, size, &walker));
arch/arm64/kvm/mmu.c +4 -4
@@ -1775,7 +1775,7 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)u64 size = (range->end - range->start) <<PAGE_SHIFT ;bool young = false; -write_lock (&kvm->mmu_lock); +read_lock (&kvm->mmu_lock);if (!kvm->arch.mmu.pgt) goto out;
@@ -1785,7 +1785,7 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) size, true); out: -write_unlock (&kvm->mmu_lock); +read_unlock (&kvm->mmu_lock);return young; }
@@ -1794,7 +1794,7 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)u64 size = (range->end - range->start) <<PAGE_SHIFT ;bool young = false; -write_lock (&kvm->mmu_lock); +read_lock (&kvm->mmu_lock);if (!kvm->arch.mmu.pgt) goto out;
@@ -1804,7 +1804,7 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) size, false); out: -write_unlock (&kvm->mmu_lock); +read_unlock (&kvm->mmu_lock);return young; }
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
Re: [PATCH v4 6/7] KVM: arm64: Relax locking for kvm_test_age_gfn and kvm_age_gfn
On Wed, May 29, 2024 at 06:05:09PM +0000, James Houghton wrote:
[...]
Quoted 7 lines
arch/arm64/kvm/hyp/pgtable.c +0
-0@@ -1319,10 +1319,8 @@ static int stage2_age_walker(const struct kvm_pgtable_visit_ctx *ctx, data->young = true;
arch/arm64/kvm/hyp/pgtable.c static int stage2_age_walker(const struct kvm_pgtable_visit_ctx *ctx, · line 1326
Quoted 10 lines
arch/arm64/kvm/hyp/pgtable.c +2
-4/* -* stage2_age_walker() is always called while holding the MMU lock for -* write, so this will always succeed. Nonetheless, this deliberately -* follows the race detection pattern of the other stage-2 walkers in -* case the locking mechanics of the MMU notifiers is ever changed. +* This walk may not be exclusive; the PTE is permitted to change +* from under us. */ if (data->mkold && !stage2_try_set_pte (ctx, new))return -EAGAIN ;
It is probably worth mentioning that if there was a race to update the PTE then the GFN is most likely young, so failing to clear AF probably isn't even consequential.
Signature · 2 lines
Thanks, Oliver
Re: [PATCH v4 6/7] KVM: arm64: Relax locking for kvm_test_age_gfn and kvm_age_gfn
Quoted 11 lines · Oliver Upton
On Wed, May 29, 2024 at 06:05:09PM +0000, James Houghton wrote:
[...]
Quoted 7 lines
arch/arm64/kvm/hyp/pgtable.c +0
-0@@ -1319,10 +1319,8 @@ static int stage2_age_walker(const struct kvm_pgtable_visit_ctx *ctx, data->young = true;
Quoted 14 lines
arch/arm64/kvm/hyp/pgtable.c static int stage2_age_walker(const struct kvm_pgtable_visit_ctx *ctx, · line 1326
Quoted 10 lines
arch/arm64/kvm/hyp/pgtable.c +2
-4/* -* stage2_age_walker() is always called while holding the MMU lock for -* write, so this will always succeed. Nonetheless, this deliberately -* follows the race detection pattern of the other stage-2 walkers in -* case the locking mechanics of the MMU notifiers is ever changed. +* This walk may not be exclusive; the PTE is permitted to change +* from under us. */ if (data->mkold && !stage2_try_set_pte (ctx, new))return -EAGAIN ;It is probably worth mentioning that if there was a race to update the PTE then the GFN is most likely young, so failing to clear AF probably isn't even consequential.
Oh, and the WARN_ON() in kvm_pgtable_stage2_test_clear_young() is bogus now. Maybe demote it to:
r = kvm_pgtable_walk(...); WARN_ON_ONCE(r && r != -EAGAIN);
Signature · 2 lines
Thanks, Oliver
Re: [PATCH v4 6/7] KVM: arm64: Relax locking for kvm_test_age_gfn and kvm_age_gfn
Quoted 13 lines · On Fri, May 31, 2024 at 12:18?PM Oliver Upton <[email protected]> wrote:
Quoted 11 lines · Oliver Upton
On Wed, May 29, 2024 at 06:05:09PM +0000, James Houghton wrote:
[...]
Quoted 7 lines
arch/arm64/kvm/hyp/pgtable.c +0
-0@@ -1319,10 +1319,8 @@ static int stage2_age_walker(const struct kvm_pgtable_visit_ctx *ctx, data->young = true;
Quoted 14 lines
Quoted 11 lines
arch/arm64/kvm/hyp/pgtable.c static int stage2_age_walker(const struct kvm_pgtable_visit_ctx *ctx, · line 1326
Quoted 10 lines
arch/arm64/kvm/hyp/pgtable.c +2
-4/* -* stage2_age_walker() is always called while holding the MMU lock for -* write, so this will always succeed. Nonetheless, this deliberately -* follows the race detection pattern of the other stage-2 walkers in -* case the locking mechanics of the MMU notifiers is ever changed. +* This walk may not be exclusive; the PTE is permitted to change +* from under us. */ if (data->mkold && !stage2_try_set_pte (ctx, new))return -EAGAIN ;Quoted 3 lines
It is probably worth mentioning that if there was a race to update the PTE then the GFN is most likely young, so failing to clear AF probably isn't even consequential.
Thanks Oliver.
Quoted 6 lines
Oh, and the
WARN_ON()inkvm_pgtable_stage2_test_clear_young()is bogus now. Maybe demote it to:r = kvm_pgtable_walk(...); WARN_ON_ONCE(r && r != -EAGAIN);
Oh, indeed, thank you. Just to make sure -- does it make sense to retry the cmpxchg if it fails? For example, the way I have it now for x86[1], we retry the cmpxchg if the spte is still a leaf, otherwise we move on to the next one having done nothing. Does something like that make sense for arm64?
[1]: https://lore.kernel.org/linux-mm/20240529180510.2295118-6-jthoughton at google.com/
Re: [PATCH v4 6/7] KVM: arm64: Relax locking for kvm_test_age_gfn and kvm_age_gfn
Quoted 35 lines · James Houghton
Quoted 13 lines · On Fri, May 31, 2024 at 12:18?PM Oliver Upton <[email protected]> wrote:
Quoted 11 lines · Oliver Upton
On Wed, May 29, 2024 at 06:05:09PM +0000, James Houghton wrote:
[...]
Quoted 7 lines
arch/arm64/kvm/hyp/pgtable.c +0
-0@@ -1319,10 +1319,8 @@ static int stage2_age_walker(const struct kvm_pgtable_visit_ctx *ctx, data->young = true;Quoted 14 lines
Quoted 11 lines
arch/arm64/kvm/hyp/pgtable.c static int stage2_age_walker(const struct kvm_pgtable_visit_ctx *ctx, · line 1326
Quoted 10 lines
arch/arm64/kvm/hyp/pgtable.c +2
-4/* -* stage2_age_walker() is always called while holding the MMU lock for -* write, so this will always succeed. Nonetheless, this deliberately -* follows the race detection pattern of the other stage-2 walkers in -* case the locking mechanics of the MMU notifiers is ever changed. +* This walk may not be exclusive; the PTE is permitted to change +* from under us. */ if (data->mkold && !stage2_try_set_pte (ctx, new))return -EAGAIN ;Quoted 3 lines
It is probably worth mentioning that if there was a race to update the PTE then the GFN is most likely young, so failing to clear AF probably isn't even consequential.
Thanks Oliver.
Quoted 4 lines
Oh, and the
WARN_ON()inkvm_pgtable_stage2_test_clear_young()is bogus now. Maybe demote it to:
Quoted 8 lines
Quoted 2 lines
r = kvm_pgtable_walk(...); WARN_ON_ONCE(r && r != -EAGAIN);Oh, indeed, thank you. Just to make sure -- does it make sense to retry the cmpxchg if it fails? For example, the way I have it now for x86[1], we retry the cmpxchg if the spte is still a leaf, otherwise we move on to the next one having done nothing. Does something like that make sense for arm64?
At least for arm64 I do not see a need for retry. The only possible races are:
- A stage-2 fault handler establishing / adjusting the mapping for the GFN. If the guest is directly accessing the GFN in question, what's the point of wiping out AF?
Even when returning -EAGAIN we've already primed stage2_age_data::young, so we report the correct state back to the primary MMU.
- Another kvm_age_gfn() trying to age the same GFN. I haven't even looked to see if this is possible from the primary MMU POV, but in theory one of the calls will win the race and clear AF.
Given Yu's concerns about making pending writers wait, we should take every opportunity to bail on the walk.
Signature · 2 lines
Thanks, Oliver
Sean Christopherson
maintainerRe: [PATCH v4 6/7] KVM: arm64: Relax locking for kvm_test_age_gfn and kvm_age_gfn
Quoted 27 lines · Oliver Upton
Quoted 7 lines · James Houghton
Quoted 6 lines · On Fri, May 31, 2024 at 12:18?PM Oliver Upton <[email protected]> wrote:
Quoted 1 line · Oliver Upton
On Wed, May 29, 2024 at 06:05:09PM +0000, James Houghton wrote:
Oh, and the
WARN_ON()inkvm_pgtable_stage2_test_clear_young()is bogus now. Maybe demote it to:Quoted 8 lines
Quoted 2 lines
r = kvm_pgtable_walk(...); WARN_ON_ONCE(r && r != -EAGAIN);Oh, indeed, thank you. Just to make sure -- does it make sense to retry the cmpxchg if it fails? For example, the way I have it now for x86[1], we retry the cmpxchg if the spte is still a leaf, otherwise we move on to the next one having done nothing. Does something like that make sense for arm64?
At least for arm64 I do not see a need for retry. The only possible races are:
- A stage-2 fault handler establishing / adjusting the mapping for the GFN. If the guest is directly accessing the GFN in question, what's the point of wiping out AF?
Even when returning
-EAGAINwe've already primedstage2_age_data::young, so we report the correct state back to the primary MMU.
Quoted 6 lines
- Another
kvm_age_gfn()trying to age the same GFN. I haven't even looked to see if this is possible from the primary MMU POV, but in theory one of the calls will win the race and clear AF.Given Yu's concerns about making pending writers wait, we should take every opportunity to bail on the walk.
+1. The x86 path that retries is, for all intents and purposes, limited to Intel CPUs that don't support EPT A/D bits, i.e. to pre-HSW CPUs. I wouldn't make any decisions based on that code.
PATCH v4 7/7 KVM: selftests: Add multi-gen LRU aging to access_tracking_perf_test
In reply to James HoughtonThis test now has two modes of operation:
1. (default) To check how much vCPU performance was affected by access tracking (previously existed, now supports MGLRU aging).
2. (-p) To also benchmark how fast MGLRU can do aging while vCPUs are faulting in memory.
Mode (1) also serves as a way to verify that aging is working properly for pages only accessed by KVM. It will fail if one does not have the 0x8 lru_gen feature bit.
To support MGLRU, the test creates a memory cgroup, moves itself into it, then uses the lru_gen debugfs output to track memory in that cgroup. The logic to parse the lru_gen debugfs output has been put into selftests/kvm/lib/lru_gen_util.c.
--- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/access_tracking_perf_test.c | 365 ++++++++++++++-- .../selftests/kvm/include/lru_gen_util.h | 55 +++ .../testing/selftests/kvm/lib/lru_gen_util.c | 391 ++++++++++++++++++ 4 files changed, 782 insertions(+), 30 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/lru_gen_util.h create mode 100644 tools/testing/selftests/kvm/lib/lru_gen_util.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -22,6 +22,7 @@ LIBKVM += lib/elf.c LIBKVM += lib/guest_modes.c LIBKVM += lib/io.c LIBKVM += lib/kvm_util.c +LIBKVM += lib/lru_gen_util.c LIBKVM += lib/memstress.c LIBKVM += lib/guest_sprintf.c LIBKVM += lib/rbtree.c
tools/testing/selftests/kvm/access_tracking_perf_test.c +335 -30
484 lines not on this page · raw message
tools/testing/selftests/kvm/include/lru_gen_util.h new file +55 -0
55 lines not on this page · raw message
tools/testing/selftests/kvm/lib/lru_gen_util.c new file +391 -0
391 lines not on this page · raw message
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog