Optimize S2 page splitting
v3 cover Optimize S2 page splitting
While playing with dirty-bit tracking, I decided to take a look on how page splitting works. Found out all entries are walked, even though we can infer, for instance that:
- If a level-3 entry is walked, it means the parent level-2 entry is split
- If a split just succeeded in an table entry, it means all children nodes are already split
This patches' idea is to introduce new walking flags to skip pagetable levels 0-3.
The idea of skipping child nodes was also tested, but it was marginally slower than just skipping levels, so it was discarted.
Optimization measured on two scenarios involving eager-splitting on a VM with 32 memslot of 2GB (total 64GB), and vcpu per slot:
- Scenario 1: No manual protect, whole memslot split at dirty-track enable (KVM_SET_USER_MEMORY_REGION2 ioctl with KVM_MEM_LOG_DIRTY_PAGES)
- Split happens only once, whole region
- Evalutes improved batch performance of splitting
- Scenario 2: Manual protect, split happens during every dirty-bit clean (KVM_CLEAR_DIRTY_LOG ioctl), average for 2 iterations.
- Split called multiple times, for smaller 64-page sections.
- Evaluate improved performance for multiple calls
Scenario 1, improvement on dirty-track enable ioctl for the memslot:
- Memory was already split (4k pages): -43.18% runtime (stdev 3.87%) - THP backed memory: -25.77% runtime (stdev 1.01%) - 64x1GB hugetlb memory: -25.62% runtime (stdev 1.09%)
Scenario 2, improvement on dirty-log clean ioctl for the memslot:
- Memory was already split (4k pages): -39.88% runtime (stdev 2.71%) - THP backed memory: -25.18% runtime (stdev 0.32%) - 64x1GB hugetlb memory: -49.17% runtime (stdev 1.86%)
For collecting above numbers, the following script was ran in both vanilla and patched kernels, with kernel parameter 'default_hugepagesz=1G', on an TX2 with 128GB RAM.
--- dirty_test.sh
#!/bin/bash
filename=$(uname -r |cut -d'-' -f 4-)
run_test(){
base_test="./dirty_log_perf_test -b 2G -v 32 -m 6 -m 8"
# Manual cleaning disable
${base_test} -g
${base_test} -g -s anonymous_thp
echo 64 > /proc/sys/vm/nr_hugepages
${base_test} -g -s shared_hugetlb
echo 0 > /proc/sys/vm/nr_hugepages # Manual cleaning enable
${base_test}
${base_test} -s anonymous_thp
echo 64 > /proc/sys/vm/nr_hugepages
${base_test} -s shared_hugetlb
echo 0 > /proc/sys/vm/nr_hugepages
}
run_test 2>&1 | tee ${filename}
---
Above dirty_log_perf_test command is the standard kvm selftest found in the kernel tree. It tested the following guest modes:
Testing guest mode: PA-bits:40, VA-bits:48, 4K pages
Testing guest mode: PA-bits:40, VA-bits:48, 64K pages
(Modes with PA-bits:36 were discarted in this version, given the amount of RAM being used for testing, and the similarity of previous results)
Performance numbers from above modes were used to calculate average and stdev showed in the optimization results.
Changes since v2:
- Rebased on top of v7.2-rc1
- Improved testing, added more memory, re-tested
- Now: 32 vcpus @ total of 64G
- Before: 1cpu @ 16G
Changes since v1:
- Fixed inverted flag verification priority (Sashiko)
- Fixed incorrectly skipping POST call if level was skipped (Sashiko), and to that
- New pre-patch that changes goto-out -> return to avoid re-testing walk_continue v1 Link: https://lore.kernel.org/lkml/[email protected]/
Changes since RFC:
- Changed approach from return value to walk flags (Will Deacon)
- Discarted skip_child approach (Oliver Upton)
- Measured in real hardware, and from userspace perspective (Marc Zyngier)
- Better explanation of what and how numbers were collected
RFC Link: https://lore.kernel.org/all/[email protected]/
Thanks!
Leo
Leonardo Bras (3):
KVM: arm64: Avoid re-testing walk_continue
KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
KVM: arm64: Make stage2_split_walker() skip unnecessary walks
arch/arm64/include/asm/kvm_pgtable.h | 13 +++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 28 +++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-)
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
Signature · 1 line
2.55.0
PATCH v3 1/3 KVM: arm64: Avoid re-testing walk_continue
In __kvm_pgtable_visit(), a couple tests for kvm_pgtable_walk_continue() will 'goto out' if it should not continue. This means the same test will be ran again before returning ret, which is unnecessary.
Return ret directly instead.
This will simplify next patch.
--- arch/arm64/kvm/hyp/pgtable.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/hyp/pgtable.c +2 -2
@@ -183,32 +183,32 @@ static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data,* Reload the page table after invoking the walker callback for leaf * entries or after pre-order traversal, to allow the walker to descend * into a newly installed or replaced table. */ if (reload) { ctx.old =READ_ONCE (*ptep); table =kvm_pte_table (ctx.old, level); }if (!kvm_pgtable_walk_continue (data->walker, ret)) - goto out; +return ret;if (!table) { data->addr =ALIGN_DOWN (data->addr,kvm_granule_size (level)); data->addr +=kvm_granule_size (level); goto out; } childp = (kvm_pteref_t )kvm_pte_follow (ctx.old, mm_ops); ret =__kvm_pgtable_walk (data, mm_ops, childp, level +1 );if (!kvm_pgtable_walk_continue (data->walker, ret)) - goto out; +return ret;if (ctx.flags &KVM_PGTABLE_WALK_TABLE_POST ) ret =kvm_pgtable_visitor_cb (data, &ctx,KVM_PGTABLE_WALK_TABLE_POST ); out:if (kvm_pgtable_walk_continue (data->walker, ret))return 0 ;return ret; }
Signature · 1 line
2.55.0
Marc Zyngier
maintainerRe: [PATCH v3 1/3] KVM: arm64: Avoid re-testing walk_continue
Quoted 7 lines · Leonardo Bras
In
__kvm_pgtable_visit(), a couple tests forkvm_pgtable_walk_continue()will 'goto out' if it should not continue. This means the same test will be ran again before returning ret, which is unnecessary.Return ret directly instead.
This will simplify next patch.
This reads quite badly, and doesn't explain what you are trying to do. How about something like:
"__kvm_pgtable_visit() performs a bunch of calls to
kvm_pgtable_walk_continue() to find out whether the walk can continue further, and if not, falls back to an 'goto out' which retests the possibility of continuing the walk.
Given that there is no reason why the situation would have changed between the two checks, turn this goto into an early return, simplifying the code and paving the way for further rework."
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
Re: [PATCH v3 1/3] KVM: arm64: Avoid re-testing walk_continue
Quoted 13 lines · Marc Zyngier
Quoted 7 lines · Leonardo Bras
In
__kvm_pgtable_visit(), a couple tests forkvm_pgtable_walk_continue()will 'goto out' if it should not continue. This means the same test will be ran again before returning ret, which is unnecessary.Return ret directly instead.
This will simplify next patch.This reads quite badly, and doesn't explain what you are trying to do. How about something like:
Quoted 9 lines
"
__kvm_pgtable_visit()performs a bunch of calls to
kvm_pgtable_walk_continue()to find out whether the walk can continue further, and if not, falls back to an 'goto out' which retests the possibility of continuing the walk.Given that there is no reason why the situation would have changed between the two checks, turn this goto into an early return, simplifying the code and paving the way for further rework."
Agree, your version looks better.
Will try to be more clear, and reuse some of your version, in my next version.
Thanks!
Leo
PATCH v3 2/3 KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
In reply to Leonardo BrasAdd the new walking flags that tell kvm_pgtable_walk() to skip lower levels when walking the pagetables.
--- arch/arm64/include/asm/kvm_pgtable.h | 13 +++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 19 ++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +13 -0
@@ -311,31 +311,44 @@ typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end,* @KVM_PGTABLE_WALK_SHARED: Indicates the page-tables may be shared * with other software walkers. * @KVM_PGTABLE_WALK_IGNORE_EAGAIN: Don't terminate the walk early if * the walker returns -EAGAIN. * @KVM_PGTABLE_WALK_SKIP_BBM_TLBI: Visit and update table entries * without Break-before-make's * TLB invalidation. * @KVM_PGTABLE_WALK_SKIP_CMO: Visit and update table entries * without Cache maintenance * operations required. +* @KVM_PGTABLE_WALK_SKIP_LEVEL0: Skip visiting level-0+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL1: Skip visiting level-1+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL2: Skip visiting level-2+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL3: Skip visiting level-3 entries */ enum kvm_pgtable_walk_flags {KVM_PGTABLE_WALK_LEAF =BIT (0 ),KVM_PGTABLE_WALK_TABLE_PRE =BIT (1 ),KVM_PGTABLE_WALK_TABLE_POST =BIT (2 ),KVM_PGTABLE_WALK_SHARED =BIT (3 ),KVM_PGTABLE_WALK_IGNORE_EAGAIN =BIT (4 ),KVM_PGTABLE_WALK_SKIP_BBM_TLBI =BIT (5 ),KVM_PGTABLE_WALK_SKIP_CMO =BIT (6 ), +KVM_PGTABLE_WALK_SKIP_LEVEL0 =BIT (7 ), +KVM_PGTABLE_WALK_SKIP_LEVEL1 =BIT (8 ), +KVM_PGTABLE_WALK_SKIP_LEVEL2 =BIT (9 ), +KVM_PGTABLE_WALK_SKIP_LEVEL3 =BIT (10 ), }; +#define KVM_PGTABLE_WALK_SKIP_LEVELS (KVM_PGTABLE_WALK_SKIP_LEVEL0 | \ + KVM_PGTABLE_WALK_SKIP_LEVEL1 | \ + KVM_PGTABLE_WALK_SKIP_LEVEL2 | \ + KVM_PGTABLE_WALK_SKIP_LEVEL3 ) +struct kvm_pgtable_visit_ctx {kvm_pte_t *ptep;kvm_pte_t old;void *arg;struct kvm_pgtable_mm_ops *mm_ops;u64 start;u64 addr;u64 end;s8 level;enum kvm_pgtable_walk_flags flags;
arch/arm64/kvm/hyp/pgtable.c +16 -3
@@ -137,20 +137,33 @@ static bool kvm_pgtable_walk_continue(const struct kvm_pgtable_walker *walker,* Ignore the return code altogether for walkers outside a fault handler * (e.g. write protecting a range of memory) and chug along with the * page table walk. */ if (r == -EAGAIN )return walker->flags &KVM_PGTABLE_WALK_IGNORE_EAGAIN ;return !r; } +static __always_inline bool kvm_pgtable_skip_level(s8 level, enum kvm_pgtable_walk_flags flags) +{ + flags &=KVM_PGTABLE_WALK_SKIP_LEVELS ; + +if (likely (!flags)) +return false; + +if (level >= (ffs (flags) -ffs (KVM_PGTABLE_WALK_SKIP_LEVELS ))) +return true; + +return false; +} +static int __kvm_pgtable_walk (struct kvm_pgtable_walk_data *data,struct kvm_pgtable_mm_ops *mm_ops,kvm_pteref_t pgtable,s8 level);static inline int __kvm_pgtable_visit (struct kvm_pgtable_walk_data *data,struct kvm_pgtable_mm_ops *mm_ops,kvm_pteref_t pteref,s8 level) {enum kvm_pgtable_walk_flags flags = data->walker->flags;kvm_pte_t *ptep =kvm_dereference_pteref (data->walker, pteref);struct kvm_pgtable_visit_ctx ctx = {
@@ -185,35 +198,35 @@ static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data,* into a newly installed or replaced table. */ if (reload) { ctx.old =READ_ONCE (*ptep); table =kvm_pte_table (ctx.old, level); }if (!kvm_pgtable_walk_continue (data->walker, ret))return ret; -if (!table) { +if (!table ||kvm_pgtable_skip_level (level +1 , ctx.flags)) { data->addr =ALIGN_DOWN (data->addr,kvm_granule_size (level)); data->addr +=kvm_granule_size (level); goto out; } childp = (kvm_pteref_t )kvm_pte_follow (ctx.old, mm_ops); ret =__kvm_pgtable_walk (data, mm_ops, childp, level +1 );if (!kvm_pgtable_walk_continue (data->walker, ret))return ret; -if (ctx.flags &KVM_PGTABLE_WALK_TABLE_POST ) +out: +if (table && ctx.flags &KVM_PGTABLE_WALK_TABLE_POST ) ret =kvm_pgtable_visitor_cb (data, &ctx,KVM_PGTABLE_WALK_TABLE_POST ); -out:if (kvm_pgtable_walk_continue (data->walker, ret))return 0 ;return ret; }static int __kvm_pgtable_walk (struct kvm_pgtable_walk_data *data,struct kvm_pgtable_mm_ops *mm_ops,kvm_pteref_t pgtable,s8 level) {u32 idx;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v3 2/3] KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] KVM_PGTABLE_WALK_SKIP_LEVEL* flags fail to skip the starting root level of the page table walk. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] KVM_PGTABLE_WALK_SKIP_LEVEL* flags fail to skip the starting root level of the page table walk. --
--- Patch [2]: [PATCH v3 2/3] KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags --- commit 5ddf63ba492be8c2a84af0b81cf00790fc72767a
Author: Leonardo Bras <[email protected]>
KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
This commit adds new walking flags that tell kvm_pgtable_walk() to skip lower levels when traversing page tables.
Quoted 4 lines
arch/arm64/kvm/hyp/pgtable.c +0
-0
[ ... ]
arch/arm64/kvm/hyp/pgtable.c static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data, · line 208
Quoted 6 lines
arch/arm64/kvm/hyp/pgtable.c +1
-1@@ -192,7 +205,7 @@ static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data,if (!kvm_pgtable_walk_continue (data->walker, ret))return ret; -if (!table) { +if (!table ||kvm_pgtable_skip_level (level +1 , ctx.flags)) {
[Severity: Medium]
Does this fail to skip the starting level of the page table walk if that initial level matches the skip criteria?
Looking at __kvm_pgtable_visit() in arch/arm64/kvm/hyp/pgtable.c, the visitor callbacks are executed unconditionally for the current level before we reach this skip check:
if (table && (ctx.flags & KVM_PGTABLE_WALK_TABLE_PRE)) {
ret = kvm_pgtable_visitor_cb(data, &ctx, KVM_PGTABLE_WALK_TABLE_PRE);
reload = true;
}
if (!table && (ctx.flags & KVM_PGTABLE_WALK_LEAF)) {
ret = kvm_pgtable_visitor_cb(data, &ctx, KVM_PGTABLE_WALK_LEAF);
reload = true;
}Since the skip condition here only evaluates level + 1 to prevent descending further, if a caller begins a walk at a level that should be skipped (for example, starting at level 2 with KVM_PGTABLE_WALK_SKIP_LEVEL2), the callbacks for that root level will still execute.
Should the current level also be checked against the skip flags before executing the pre-table or leaf visitor callbacks?
Quoted 3 lines
arch/arm64/kvm/hyp/pgtable.c +0
-0data->addr =ALIGN_DOWN (data->addr,kvm_granule_size (level)); data->addr +=kvm_granule_size (level); goto out;
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v3 2/3] KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
Quoted 45 lines · On Wed, Jul 08, 2026 at 01:50:11PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] KVM_PGTABLE_WALK_SKIP_LEVEL* flags fail to skip the starting root level of the page table walk. ----- Patch [2]: [PATCH v3 2/3] KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags --- commit
5ddf63ba492be8c2a84af0b81cf00790fc72767a
Author: Leonardo Bras <[email protected]>KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
This commit adds new walking flags that tell
kvm_pgtable_walk()to skip lower levels when traversing page tables.Quoted 4 lines
arch/arm64/kvm/hyp/pgtable.c +0
-0[ ... ]
arch/arm64/kvm/hyp/pgtable.c static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data, · line 208
Quoted 6 lines
arch/arm64/kvm/hyp/pgtable.c +1
-1@@ -192,7 +205,7 @@ static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data,if (!kvm_pgtable_walk_continue (data->walker, ret))return ret; -if (!table) { +if (!table ||kvm_pgtable_skip_level (level +1 , ctx.flags)) {[Severity: Medium]
Does this fail to skip the starting level of the page table walk if that initial level matches the skip criteria?Looking at
__kvm_pgtable_visit()inarch/arm64/kvm/hyp/pgtable.c, the visitor callbacks are executed unconditionally for the current level before we reach this skip check:if (table && (ctx.flags & KVM_PGTABLE_WALK_TABLE_PRE)) { ret = kvm_pgtable_visitor_cb(data, &ctx, KVM_PGTABLE_WALK_TABLE_PRE); reload = true; } if (!table && (ctx.flags & KVM_PGTABLE_WALK_LEAF)) { ret = kvm_pgtable_visitor_cb(data, &ctx, KVM_PGTABLE_WALK_LEAF); reload = true; }
Quoted 7 lines
Since the skip condition here only evaluates level + 1 to prevent descending further, if a caller begins a walk at a level that should be skipped (for example, starting at level 2 with
KVM_PGTABLE_WALK_SKIP_LEVEL2), the callbacks for that root level will still execute.Should the current level also be checked against the skip flags before executing the pre-table or leaf visitor callbacks?
Yes, we do execute the callback at least in the root we are given, which can have bad results in some scenario. That being said, testing here is not the best place, as this function will be called a lot.
If we want to avoid that scenario, I suggest we do that test before starting the walk, which would be on _kvm_pgtable_walk(), except that both kvm_pgtable_stage2_create_unlinked(), and kvm_pgtable_stage2_free_unlinked()
will call _kvm_pgtable_walk() directly.
(although they don't use skip_level paramters, yet)
Will take a look on that.
Thanks!
Leo
Re: [PATCH v3 2/3] KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
Quoted 58 lines · Leonardo Bras
Quoted 45 lines · On Wed, Jul 08, 2026 at 01:50:11PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] KVM_PGTABLE_WALK_SKIP_LEVEL* flags fail to skip the starting root level of the page table walk. ----- Patch [2]: [PATCH v3 2/3] KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags --- commit
5ddf63ba492be8c2a84af0b81cf00790fc72767a
Author: Leonardo Bras <[email protected]>KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
This commit adds new walking flags that tell
kvm_pgtable_walk()to skip lower levels when traversing page tables.Quoted 4 lines
arch/arm64/kvm/hyp/pgtable.c +0
-0[ ... ]
arch/arm64/kvm/hyp/pgtable.c static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data, · line 208
Quoted 6 lines
arch/arm64/kvm/hyp/pgtable.c +1
-1@@ -192,7 +205,7 @@ static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data,if (!kvm_pgtable_walk_continue (data->walker, ret))return ret; -if (!table) { +if (!table ||kvm_pgtable_skip_level (level +1 , ctx.flags)) {[Severity: Medium]
Does this fail to skip the starting level of the page table walk if that initial level matches the skip criteria?Looking at
__kvm_pgtable_visit()inarch/arm64/kvm/hyp/pgtable.c, the visitor callbacks are executed unconditionally for the current level before we reach this skip check:if (table && (ctx.flags & KVM_PGTABLE_WALK_TABLE_PRE)) { ret = kvm_pgtable_visitor_cb(data, &ctx, KVM_PGTABLE_WALK_TABLE_PRE); reload = true; } if (!table && (ctx.flags & KVM_PGTABLE_WALK_LEAF)) { ret = kvm_pgtable_visitor_cb(data, &ctx, KVM_PGTABLE_WALK_LEAF); reload = true; }Quoted 7 lines
Since the skip condition here only evaluates level + 1 to prevent descending further, if a caller begins a walk at a level that should be skipped (for example, starting at level 2 with
KVM_PGTABLE_WALK_SKIP_LEVEL2), the callbacks for that root level will still execute.Should the current level also be checked against the skip flags before executing the pre-table or leaf visitor callbacks?
Yes, we do execute the callback at least in the root we are given, which can have bad results in some scenario. That being said, testing here is not the best place, as this function will be called a lot.
Quoted 5 lines
If we want to avoid that scenario, I suggest we do that test before starting the walk, which would be on
_kvm_pgtable_walk(), except that bothkvm_pgtable_stage2_create_unlinked(), andkvm_pgtable_stage2_free_unlinked()
will call_kvm_pgtable_walk()directly.
will call __kvm_pgtable_walk() directly.
Quoted 7 lines
(although they don't use
skip_levelparamters, yet)Will take a look on that.
Thanks!
Leo
Marc Zyngier
maintainerRe: [PATCH v3 2/3] KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
In reply to Leonardo BrasQuoted 3 lines · Leonardo Bras
Add the new walking flags that tell
kvm_pgtable_walk()to skip lower levels when walking the pagetables.
I don't understand what 'lower' means here. There is also no description of what this patch is trying to do, or why it is trying to do it. All I see is a wall of code with no rationale, no explanation.
Quoted 16 lines
Signed-off-by Leonardo Bras <[email protected]>--- arch/arm64/include/asm/kvm_pgtable.h | 13 +++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 19 ++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_pgtable.h +0
-0@@ -311,31 +311,44 @@ typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end,* @KVM_PGTABLE_WALK_SHARED: Indicates the page-tables may be shared * with other software walkers. * @KVM_PGTABLE_WALK_IGNORE_EAGAIN: Don't terminate the walk early if * the walker returns -EAGAIN.
arch/arm64/include/asm/kvm_pgtable.h typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end, · line 324
Quoted 10 lines
arch/arm64/include/asm/kvm_pgtable.h +4
-0* @KVM_PGTABLE_WALK_SKIP_BBM_TLBI: Visit and update table entries * without Break-before-make's * TLB invalidation. * @KVM_PGTABLE_WALK_SKIP_CMO: Visit and update table entries * without Cache maintenance * operations required. +* @KVM_PGTABLE_WALK_SKIP_LEVEL0: Skip visiting level-0+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL1: Skip visiting level-1+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL2: Skip visiting level-2+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL3: Skip visiting level-3 entries
Skip under which conditions? Always?
arch/arm64/include/asm/kvm_pgtable.h typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end, · line 337
Quoted 13 lines
arch/arm64/include/asm/kvm_pgtable.h +4
-0*/ enum kvm_pgtable_walk_flags {KVM_PGTABLE_WALK_LEAF =BIT (0 ),KVM_PGTABLE_WALK_TABLE_PRE =BIT (1 ),KVM_PGTABLE_WALK_TABLE_POST =BIT (2 ),KVM_PGTABLE_WALK_SHARED =BIT (3 ),KVM_PGTABLE_WALK_IGNORE_EAGAIN =BIT (4 ),KVM_PGTABLE_WALK_SKIP_BBM_TLBI =BIT (5 ),KVM_PGTABLE_WALK_SKIP_CMO =BIT (6 ), +KVM_PGTABLE_WALK_SKIP_LEVEL0 =BIT (7 ), +KVM_PGTABLE_WALK_SKIP_LEVEL1 =BIT (8 ), +KVM_PGTABLE_WALK_SKIP_LEVEL2 =BIT (9 ), +KVM_PGTABLE_WALK_SKIP_LEVEL3 =BIT (10 ),
There is a strong assumption that these bits must be contiguous. And yet that's not captured anywhere. Want to bet what is going to happen next?
Quoted 23 lines
arch/arm64/include/asm/kvm_pgtable.h +5
-0}; +#define KVM_PGTABLE_WALK_SKIP_LEVELS (KVM_PGTABLE_WALK_SKIP_LEVEL0 | \ + KVM_PGTABLE_WALK_SKIP_LEVEL1 | \ + KVM_PGTABLE_WALK_SKIP_LEVEL2 | \ + KVM_PGTABLE_WALK_SKIP_LEVEL3 ) +struct kvm_pgtable_visit_ctx {kvm_pte_t *ptep;kvm_pte_t old;void *arg;struct kvm_pgtable_mm_ops *mm_ops;u64 start;u64 addr;u64 end;s8 level;enum kvm_pgtable_walk_flags flags;arch/arm64/kvm/hyp/pgtable.c +0
-0@@ -137,20 +137,33 @@ static bool kvm_pgtable_walk_continue(const struct kvm_pgtable_walker *walker,* Ignore the return code altogether for walkers outside a fault handler
arch/arm64/kvm/hyp/pgtable.c static bool kvm_pgtable_walk_continue(const struct kvm_pgtable_walker *walker, · line 147
Quoted 10 lines
arch/arm64/kvm/hyp/pgtable.c +1
-0* (e.g. write protecting a range of memory) and chug along with the * page table walk. */ if (r == -EAGAIN )return walker->flags &KVM_PGTABLE_WALK_IGNORE_EAGAIN ;return !r; } +static __always_inline bool kvm_pgtable_skip_level(s8 level, enum kvm_pgtable_walk_flags flags)
Why __always_inline? I'm sure the compiler can decide for itself.
arch/arm64/kvm/hyp/pgtable.c static bool kvm_pgtable_walk_continue(const struct kvm_pgtable_walker *walker, · line 155
Quoted 8 lines
arch/arm64/kvm/hyp/pgtable.c +8
-0+{ + flags &=KVM_PGTABLE_WALK_SKIP_LEVELS ; + +if (likely (!flags)) +return false; + +if (level >= (ffs (flags) -ffs (KVM_PGTABLE_WALK_SKIP_LEVELS ))) +return true;
This looks awfully complex for something this trivial:
u32 skip = FIELD_GET(KVM_PGTABLE_WALK_SKIP_LEVELS, flags); return skip && level >= ffs(skip);
But also, you seem to assume that there cannot be more than one such flag set. I have no idea of the outcome in this situation, and the whole thing is completely undocumented anyway...
Quoted 30 lines
arch/arm64/kvm/hyp/pgtable.c +5
-1+ +return false; +} +static int __kvm_pgtable_walk (struct kvm_pgtable_walk_data *data,struct kvm_pgtable_mm_ops *mm_ops,kvm_pteref_t pgtable,s8 level);static inline int __kvm_pgtable_visit (struct kvm_pgtable_walk_data *data,struct kvm_pgtable_mm_ops *mm_ops,kvm_pteref_t pteref,s8 level) {enum kvm_pgtable_walk_flags flags = data->walker->flags;kvm_pte_t *ptep =kvm_dereference_pteref (data->walker, pteref);struct kvm_pgtable_visit_ctx ctx = {@@ -185,35 +198,35 @@ static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data,* into a newly installed or replaced table. */ if (reload) { ctx.old =READ_ONCE (*ptep); table =kvm_pte_table (ctx.old, level); }if (!kvm_pgtable_walk_continue (data->walker, ret))return ret; -if (!table) { +if (!table ||kvm_pgtable_skip_level (level +1 , ctx.flags)) { data->addr =ALIGN_DOWN (data->addr,kvm_granule_size (level)); data->addr +=kvm_granule_size (level); goto out;
arch/arm64/kvm/hyp/pgtable.c static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data, · line 220
Quoted 10 lines
arch/arm64/kvm/hyp/pgtable.c +2
-1} childp = (kvm_pteref_t )kvm_pte_follow (ctx.old, mm_ops); ret =__kvm_pgtable_walk (data, mm_ops, childp, level +1 );if (!kvm_pgtable_walk_continue (data->walker, ret))return ret; -if (ctx.flags &KVM_PGTABLE_WALK_TABLE_POST ) +out: +if (table && ctx.flags &KVM_PGTABLE_WALK_TABLE_POST )
I don't understand this change. Care to explain?
arch/arm64/kvm/hyp/pgtable.c static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data, · line 232
Quoted 13 lines
arch/arm64/kvm/hyp/pgtable.c +0
-1ret =kvm_pgtable_visitor_cb (data, &ctx,KVM_PGTABLE_WALK_TABLE_POST ); -out:if (kvm_pgtable_walk_continue (data->walker, ret))return 0 ;return ret; }static int __kvm_pgtable_walk (struct kvm_pgtable_walk_data *data,struct kvm_pgtable_mm_ops *mm_ops,kvm_pteref_t pgtable,s8 level) {u32 idx;
Thanks,
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
Re: [PATCH v3 2/3] KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
Quoted 9 lines · Marc Zyngier
Quoted 3 lines · Leonardo Bras
Add the new walking flags that tell
kvm_pgtable_walk()to skip lower levels when walking the pagetables.I don't understand what 'lower' means here. There is also no description of what this patch is trying to do, or why it is trying to do it. All I see is a wall of code with no rationale, no explanation.
Indeed, the description is very poor. Sorry about that.
For next version, what about something like that:
Add new walking flags KVM_PGTABLE_WALK_SKIP_LEVEL{0-3}.
When used, they make the kvm_pgtable_walk() skip the given level, as well as levels higher than that.
Quoted 21 lines
Quoted 21 lines
Signed-off-by Leonardo Bras <[email protected]>--- arch/arm64/include/asm/kvm_pgtable.h | 13 +++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 19 ++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_pgtable.h +0
-0@@ -311,31 +311,44 @@ typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end,* @KVM_PGTABLE_WALK_SHARED: Indicates the page-tables may be shared * with other software walkers. * @KVM_PGTABLE_WALK_IGNORE_EAGAIN: Don't terminate the walk early if * the walker returns -EAGAIN. * @KVM_PGTABLE_WALK_SKIP_BBM_TLBI: Visit and update table entries * without Break-before-make's * TLB invalidation. * @KVM_PGTABLE_WALK_SKIP_CMO: Visit and update table entries
Quoted 8 lines
arch/arm64/include/asm/kvm_pgtable.h typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end, · line 324
Quoted 6 lines
arch/arm64/include/asm/kvm_pgtable.h +4
-0* without Cache maintenance * operations required. +* @KVM_PGTABLE_WALK_SKIP_LEVEL0: Skip visiting level-0+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL1: Skip visiting level-1+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL2: Skip visiting level-2+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL3: Skip visiting level-3 entries Skip under which conditions? Always?
Yes, if passed they will skip visiting those levels.
The only current issue with this patchset is that the root of the subtree passed is always visited. But I should be able to fix that for next version.
Quoted 16 lines
Quoted 4 lines
arch/arm64/include/asm/kvm_pgtable.h +0
-0*/ enum kvm_pgtable_walk_flags {KVM_PGTABLE_WALK_LEAF =BIT (0 ),arch/arm64/include/asm/kvm_pgtable.h typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end, · line 337
Quoted 10 lines
arch/arm64/include/asm/kvm_pgtable.h +4
-0KVM_PGTABLE_WALK_TABLE_PRE =BIT (1 ),KVM_PGTABLE_WALK_TABLE_POST =BIT (2 ),KVM_PGTABLE_WALK_SHARED =BIT (3 ),KVM_PGTABLE_WALK_IGNORE_EAGAIN =BIT (4 ),KVM_PGTABLE_WALK_SKIP_BBM_TLBI =BIT (5 ),KVM_PGTABLE_WALK_SKIP_CMO =BIT (6 ), +KVM_PGTABLE_WALK_SKIP_LEVEL0 =BIT (7 ), +KVM_PGTABLE_WALK_SKIP_LEVEL1 =BIT (8 ), +KVM_PGTABLE_WALK_SKIP_LEVEL2 =BIT (9 ), + KVM_PGTABLE_WALK_SKIP_LEVEL3 = BIT(10),There is a strong assumption that these bits must be contiguous.
Correct. They can be shifted in the flags variable, but not reordered nor split.
Quoted 3 lines
And
yet that's not captured anywhere. Want to bet what is going to happen next?
I assumed that once the flags get defined, they are not usually changed or reordered, I only prepared for a day where we need skipping level -1 as well, as it can be added below.
But I think I get your point. Should have added a commend saying that the SKIP_LEVEL* flags should not be reordered nor split.
Is that it? or you suggest doing something else?
Quoted 30 lines
Quoted 30 lines
arch/arm64/include/asm/kvm_pgtable.h +5
-0}; +#define KVM_PGTABLE_WALK_SKIP_LEVELS (KVM_PGTABLE_WALK_SKIP_LEVEL0 | \ + KVM_PGTABLE_WALK_SKIP_LEVEL1 | \ + KVM_PGTABLE_WALK_SKIP_LEVEL2 | \ + KVM_PGTABLE_WALK_SKIP_LEVEL3 ) +struct kvm_pgtable_visit_ctx {kvm_pte_t *ptep;kvm_pte_t old;void *arg;struct kvm_pgtable_mm_ops *mm_ops;u64 start;u64 addr;u64 end;s8 level;enum kvm_pgtable_walk_flags flags;arch/arm64/kvm/hyp/pgtable.c +0
-0@@ -137,20 +137,33 @@ static bool kvm_pgtable_walk_continue(const struct kvm_pgtable_walker *walker,* Ignore the return code altogether for walkers outside a fault handler * (e.g. write protecting a range of memory) and chug along with the * page table walk. */ if (r == -EAGAIN )return walker->flags &KVM_PGTABLE_WALK_IGNORE_EAGAIN ;
Quoted 7 lines
arch/arm64/kvm/hyp/pgtable.c static bool kvm_pgtable_walk_continue(const struct kvm_pgtable_walker *walker, · line 147
Quoted 4 lines
arch/arm64/kvm/hyp/pgtable.c +1
-0return !r; } +static __always_inline bool kvm_pgtable_skip_level(s8 level, enum kvm_pgtable_walk_flags flags)Why
__always_inline? I'm sure the compiler can decide for itself.
Right, will change that in next version.
Quoted 11 lines
arch/arm64/kvm/hyp/pgtable.c static bool kvm_pgtable_walk_continue(const struct kvm_pgtable_walker *walker, · line 155
Quoted 8 lines
arch/arm64/kvm/hyp/pgtable.c +8
-0+{ + flags &=KVM_PGTABLE_WALK_SKIP_LEVELS ; + +if (likely (!flags)) +return false; + +if (level >= (ffs (flags) -ffs (KVM_PGTABLE_WALK_SKIP_LEVELS ))) +return true;This looks awfully complex for something this trivial:
Quoted 4 lines
u32 skip = FIELD_GET(KVM_PGTABLE_WALK_SKIP_LEVELS, flags); return skip && level >= ffs(skip);
My rationale was that in the regular case we would not have those flags set, so I tried to minimize any code that would be happening for that case. So I used only a 'bitwise and' to check the flags, then returned if not used.
If one of those flags were set, then find the lowest level set, and check against the current level for skipping.
But looking into the ARM instructions, my rationale would be pointless, as there are instructions that AND+SHIFT at once, and your version with FIELD_GET() should be better.
Quoted 4 lines
But also, you seem to assume that there cannot be more than one such flag set. I have no idea of the outcome in this situation, and the whole thing is completely undocumented anyway...
ffs(flags) is meant to find the first set bit in the flags, which is, after the masking, the lowest level to skip. If you are skipping a level, it's supposed to skip all levels higher than that, as per the flag documentation:
+ * @KVM_PGTABLE_WALK_SKIP_LEVEL0: Skip visiting level-0+ entries
+ * @KVM_PGTABLE_WALK_SKIP_LEVEL1: Skip visiting level-1+ entries
+ * @KVM_PGTABLE_WALK_SKIP_LEVEL2: Skip visiting level-2+ entries
+ * @KVM_PGTABLE_WALK_SKIP_LEVEL3: Skip visiting level-3 entries
*/
The + above means any level above that as well.
Quoted 37 lines
Quoted 37 lines
arch/arm64/kvm/hyp/pgtable.c +5
-1+ +return false; +} +static int __kvm_pgtable_walk (struct kvm_pgtable_walk_data *data,struct kvm_pgtable_mm_ops *mm_ops,kvm_pteref_t pgtable,s8 level);static inline int __kvm_pgtable_visit (struct kvm_pgtable_walk_data *data,struct kvm_pgtable_mm_ops *mm_ops,kvm_pteref_t pteref,s8 level) {enum kvm_pgtable_walk_flags flags = data->walker->flags;kvm_pte_t *ptep =kvm_dereference_pteref (data->walker, pteref);struct kvm_pgtable_visit_ctx ctx = {@@ -185,35 +198,35 @@ static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data,* into a newly installed or replaced table. */ if (reload) { ctx.old =READ_ONCE (*ptep); table =kvm_pte_table (ctx.old, level); }if (!kvm_pgtable_walk_continue (data->walker, ret))return ret; -if (!table) { +if (!table ||kvm_pgtable_skip_level (level +1 , ctx.flags)) { data->addr =ALIGN_DOWN (data->addr,kvm_granule_size (level)); data->addr +=kvm_granule_size (level); goto out; } childp = (kvm_pteref_t )kvm_pte_follow (ctx.old, mm_ops); ret =__kvm_pgtable_walk (data, mm_ops, childp, level +1 );if (!kvm_pgtable_walk_continue (data->walker, ret))return ret;
Quoted 5 lines
arch/arm64/kvm/hyp/pgtable.c static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data, · line 220
Quoted 3 lines
arch/arm64/kvm/hyp/pgtable.c +2
-1-if (ctx.flags &KVM_PGTABLE_WALK_TABLE_POST ) +out: +if (table && ctx.flags &KVM_PGTABLE_WALK_TABLE_POST )I don't understand this change. Care to explain?
Sure!
Before this change, there was no need to test if the entry was a table before visiting it on WALK_TABLE_POST, as that if clause would only be reached by table entries.
(Since the 'out:' label was below it, and above there was an if (!table) that would always goto it, it was always skipped by !table entries)
With the moving of 'out:' label to above this clause, we need to check if the entry is actually a table before visiting with a WALK_TABLE_POST flag.
Now, the reason why I moved that label up is that the if(!table) clause received the '|| skip_level' check, and now we can have this scenario:
- table entry visited in WALK_TABLE_PRE,
- table got its children skipped due to skip_level,
- need to visit it due to the WALK_TABLE_POST.
And that was the least intrusive way of doing it :)
Please let me know if I can make something else more clear!
Thanks!
Leo
Re: [PATCH v3 2/3] KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
In reply to Leonardo BrasHi Leonardo,
Quoted 18 lines · Leonardo Bras
Add the new walking flags that tell
kvm_pgtable_walk()to skip lower levels when walking the pagetables.Signed-off-by Leonardo Bras <[email protected]>--- arch/arm64/include/asm/kvm_pgtable.h | 13 +++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 19 ++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_pgtable.h +0
-0@@ -311,31 +311,44 @@ typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end,* @KVM_PGTABLE_WALK_SHARED: Indicates the page-tables may be shared * with other software walkers. * @KVM_PGTABLE_WALK_IGNORE_EAGAIN: Don't terminate the walk early if * the walker returns -EAGAIN.
arch/arm64/include/asm/kvm_pgtable.h typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end, · line 324
Quoted 10 lines
arch/arm64/include/asm/kvm_pgtable.h +4
-0* @KVM_PGTABLE_WALK_SKIP_BBM_TLBI: Visit and update table entries * without Break-before-make's * TLB invalidation. * @KVM_PGTABLE_WALK_SKIP_CMO: Visit and update table entries * without Cache maintenance * operations required. +* @KVM_PGTABLE_WALK_SKIP_LEVEL0: Skip visiting level-0+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL1: Skip visiting level-1+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL2: Skip visiting level-2+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL3: Skip visiting level-3 entries
Just a drive-by nit:
Maybe avoid the level-$LEVEL terminology? Since as you said there is level -1 in the architecture, in that case you'd have to write "level--1" which is awkward :)
Thanks,
Wei-Lin Chang
Quoted 110 lines
arch/arm64/include/asm/kvm_pgtable.h +9
-0*/ enum kvm_pgtable_walk_flags {KVM_PGTABLE_WALK_LEAF =BIT (0 ),KVM_PGTABLE_WALK_TABLE_PRE =BIT (1 ),KVM_PGTABLE_WALK_TABLE_POST =BIT (2 ),KVM_PGTABLE_WALK_SHARED =BIT (3 ),KVM_PGTABLE_WALK_IGNORE_EAGAIN =BIT (4 ),KVM_PGTABLE_WALK_SKIP_BBM_TLBI =BIT (5 ),KVM_PGTABLE_WALK_SKIP_CMO =BIT (6 ), +KVM_PGTABLE_WALK_SKIP_LEVEL0 =BIT (7 ), +KVM_PGTABLE_WALK_SKIP_LEVEL1 =BIT (8 ), +KVM_PGTABLE_WALK_SKIP_LEVEL2 =BIT (9 ), +KVM_PGTABLE_WALK_SKIP_LEVEL3 =BIT (10 ), }; +#define KVM_PGTABLE_WALK_SKIP_LEVELS (KVM_PGTABLE_WALK_SKIP_LEVEL0 | \ + KVM_PGTABLE_WALK_SKIP_LEVEL1 | \ + KVM_PGTABLE_WALK_SKIP_LEVEL2 | \ + KVM_PGTABLE_WALK_SKIP_LEVEL3 ) +struct kvm_pgtable_visit_ctx {kvm_pte_t *ptep;kvm_pte_t old;void *arg;struct kvm_pgtable_mm_ops *mm_ops;u64 start;u64 addr;u64 end;s8 level;enum kvm_pgtable_walk_flags flags;arch/arm64/kvm/hyp/pgtable.c +16
-3@@ -137,20 +137,33 @@ static bool kvm_pgtable_walk_continue(const struct kvm_pgtable_walker *walker,* Ignore the return code altogether for walkers outside a fault handler * (e.g. write protecting a range of memory) and chug along with the * page table walk. */ if (r == -EAGAIN )return walker->flags &KVM_PGTABLE_WALK_IGNORE_EAGAIN ;return !r; } +static __always_inline bool kvm_pgtable_skip_level(s8 level, enum kvm_pgtable_walk_flags flags) +{ + flags &=KVM_PGTABLE_WALK_SKIP_LEVELS ; + +if (likely (!flags)) +return false; + +if (level >= (ffs (flags) -ffs (KVM_PGTABLE_WALK_SKIP_LEVELS ))) +return true; + +return false; +} +static int __kvm_pgtable_walk (struct kvm_pgtable_walk_data *data,struct kvm_pgtable_mm_ops *mm_ops,kvm_pteref_t pgtable,s8 level);static inline int __kvm_pgtable_visit (struct kvm_pgtable_walk_data *data,struct kvm_pgtable_mm_ops *mm_ops,kvm_pteref_t pteref,s8 level) {enum kvm_pgtable_walk_flags flags = data->walker->flags;kvm_pte_t *ptep =kvm_dereference_pteref (data->walker, pteref);struct kvm_pgtable_visit_ctx ctx = {@@ -185,35 +198,35 @@ static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data,* into a newly installed or replaced table. */ if (reload) { ctx.old =READ_ONCE (*ptep); table =kvm_pte_table (ctx.old, level); }if (!kvm_pgtable_walk_continue (data->walker, ret))return ret; -if (!table) { +if (!table ||kvm_pgtable_skip_level (level +1 , ctx.flags)) { data->addr =ALIGN_DOWN (data->addr,kvm_granule_size (level)); data->addr +=kvm_granule_size (level); goto out; } childp = (kvm_pteref_t )kvm_pte_follow (ctx.old, mm_ops); ret =__kvm_pgtable_walk (data, mm_ops, childp, level +1 );if (!kvm_pgtable_walk_continue (data->walker, ret))return ret; -if (ctx.flags &KVM_PGTABLE_WALK_TABLE_POST ) +out: +if (table && ctx.flags &KVM_PGTABLE_WALK_TABLE_POST ) ret =kvm_pgtable_visitor_cb (data, &ctx,KVM_PGTABLE_WALK_TABLE_POST ); -out:if (kvm_pgtable_walk_continue (data->walker, ret))return 0 ;return ret; }static int __kvm_pgtable_walk (struct kvm_pgtable_walk_data *data,struct kvm_pgtable_mm_ops *mm_ops,kvm_pteref_t pgtable,s8 level) {u32 idx;Signature · 1 line
2.55.0
Re: [PATCH v3 2/3] KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
Quoted 1 line · Wei-Lin Chang
Hi Leonardo,
Hi Wei-Lin, thanks for reviewing!
Quoted 27 lines · Leonardo Bras
Quoted 25 lines
Add the new walking flags that tell
kvm_pgtable_walk()to skip lower levels when walking the pagetables.Signed-off-by Leonardo Bras <[email protected]>--- arch/arm64/include/asm/kvm_pgtable.h | 13 +++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 19 ++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_pgtable.h +1
-0@@ -311,31 +311,44 @@ typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end,* @KVM_PGTABLE_WALK_SHARED: Indicates the page-tables may be shared * with other software walkers. * @KVM_PGTABLE_WALK_IGNORE_EAGAIN: Don't terminate the walk early if * the walker returns -EAGAIN. * @KVM_PGTABLE_WALK_SKIP_BBM_TLBI: Visit and update table entries * without Break-before-make's * TLB invalidation. * @KVM_PGTABLE_WALK_SKIP_CMO: Visit and update table entries * without Cache maintenance * operations required. +* @KVM_PGTABLE_WALK_SKIP_LEVEL0: Skip visiting level-0+ entries
Quoted 8 lines
arch/arm64/include/asm/kvm_pgtable.h typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end, · line 324
Quoted 3 lines
arch/arm64/include/asm/kvm_pgtable.h +3
-0+* @KVM_PGTABLE_WALK_SKIP_LEVEL1: Skip visiting level-1+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL2: Skip visiting level-2+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL3: Skip visiting level-3 entries Just a drive-by nit:
Maybe avoid the level-$LEVEL terminology? Since as you said there is level -1 in the architecture, in that case you'd have to write "level--1" which is awkward :)
It would have to be something like level-(-1), or we could call it level+1, LOL.
Seriously though, yes it's bad.
I could call it level_1 entries, but level_-1 also seems weird :( level(1) & level(-1) could work, but looks like a function now
maybe just saying 'level 1' and 'level -1' can be better.
What do you think?
Thanks!
Leo
Re: [PATCH v3 2/3] KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
Quoted 41 lines · Leonardo Bras
Quoted 1 line · Wei-Lin Chang
Hi Leonardo,
Hi Wei-Lin, thanks for reviewing!
Quoted 27 lines · Leonardo Bras
Quoted 25 lines
Add the new walking flags that tell
kvm_pgtable_walk()to skip lower levels when walking the pagetables.Signed-off-by Leonardo Bras <[email protected]>--- arch/arm64/include/asm/kvm_pgtable.h | 13 +++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 19 ++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_pgtable.h +1
-0@@ -311,31 +311,44 @@ typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end,* @KVM_PGTABLE_WALK_SHARED: Indicates the page-tables may be shared * with other software walkers. * @KVM_PGTABLE_WALK_IGNORE_EAGAIN: Don't terminate the walk early if * the walker returns -EAGAIN. * @KVM_PGTABLE_WALK_SKIP_BBM_TLBI: Visit and update table entries * without Break-before-make's * TLB invalidation. * @KVM_PGTABLE_WALK_SKIP_CMO: Visit and update table entries * without Cache maintenance * operations required. +* @KVM_PGTABLE_WALK_SKIP_LEVEL0: Skip visiting level-0+ entries Quoted 8 lines
arch/arm64/include/asm/kvm_pgtable.h typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end, · line 324
Quoted 3 lines
arch/arm64/include/asm/kvm_pgtable.h +3
-0+* @KVM_PGTABLE_WALK_SKIP_LEVEL1: Skip visiting level-1+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL2: Skip visiting level-2+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL3: Skip visiting level-3 entries Just a drive-by nit:
Maybe avoid the level-$LEVEL terminology? Since as you said there is level -1 in the architecture, in that case you'd have to write "level--1" which is awkward :)
Quoted 2 lines
It would have to be something like level-(-1), or we could call it level+1, LOL.
Good one! :)
Quoted 6 lines
Seriously though, yes it's bad.
I could call itlevel_1entries, but level_-1 also seems weird :( level(1) & level(-1) could work, but looks like a function nowmaybe just saying 'level 1' and 'level -1' can be better.
Yeah, I generally also just go with the space when it's text. For code/variables, I guess a direct concatenation like what's done here for the enum is good.
Thanks,
Wei-Lin Chang
Quoted 7 lines
What do you think?
Thanks!
Leo
Re: [PATCH v3 2/3] KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
Quoted 52 lines · Wei-Lin Chang
Quoted 41 lines · Leonardo Bras
Quoted 1 line · Wei-Lin Chang
Hi Leonardo,
Hi Wei-Lin, thanks for reviewing!
Quoted 27 lines · Leonardo Bras
Quoted 25 lines
Add the new walking flags that tell
kvm_pgtable_walk()to skip lower levels when walking the pagetables.Signed-off-by Leonardo Bras <[email protected]>--- arch/arm64/include/asm/kvm_pgtable.h | 13 +++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 19 ++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_pgtable.h +1
-0@@ -311,31 +311,44 @@ typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end,* @KVM_PGTABLE_WALK_SHARED: Indicates the page-tables may be shared * with other software walkers. * @KVM_PGTABLE_WALK_IGNORE_EAGAIN: Don't terminate the walk early if * the walker returns -EAGAIN. * @KVM_PGTABLE_WALK_SKIP_BBM_TLBI: Visit and update table entries * without Break-before-make's * TLB invalidation. * @KVM_PGTABLE_WALK_SKIP_CMO: Visit and update table entries * without Cache maintenance * operations required. +* @KVM_PGTABLE_WALK_SKIP_LEVEL0: Skip visiting level-0+ entries Quoted 8 lines
arch/arm64/include/asm/kvm_pgtable.h typedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end, · line 324
Quoted 3 lines
arch/arm64/include/asm/kvm_pgtable.h +3
-0+* @KVM_PGTABLE_WALK_SKIP_LEVEL1: Skip visiting level-1+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL2: Skip visiting level-2+ entries +* @KVM_PGTABLE_WALK_SKIP_LEVEL3: Skip visiting level-3 entries Just a drive-by nit:
Maybe avoid the level-$LEVEL terminology? Since as you said there is level -1 in the architecture, in that case you'd have to write "level--1" which is awkward :)Quoted 2 lines
It would have to be something like level-(-1), or we could call it level+1, LOL.
Good one! :)
Quoted 5 lines
Seriously though, yes it's bad.
I could call itlevel_1entries, but level_-1 also seems weird :( level(1) & level(-1) could work, but looks like a function now
Quoted 6 lines
Quoted 1 line
maybe just saying 'level 1' and 'level -1' can be better.
Yeah, I generally also just go with the space when it's text. For code/variables, I guess a direct concatenation like what's done here for the enum is good.
Sure, noted for next version
Thanks!
Leo
PATCH v3 3/3 KVM: arm64: Make stage2_split_walker() skip unnecessary walks
In reply to Leonardo BrasCurrently, when splitting, all the child and sibling nodes will be walked, with the walker just returning earlier if there is nothing to do. This means all pagetable entries in the splitting range get a callback from the walker function, even if it was a level-3 entry.
Optimize splitting by skipping all level-3 entries, as they are already the smallest block size and can't be split any further.
(i.e. set flag KVM_PGTABLE_WALK_SKIP_LEVEL3)
Optimization measured on a 64GB VM, with eager splitting, no manual protect (splitting all memory at once on dirty-track enalble):
- Memory was already split (4k pages): -97.33% runtime (-172ms) - 20 runs - THP backed memory: -19.82% runtime (-153ms) - 10 runs - 64x1GB hugetlb memory: -20.65% runtime (-150ms) - 10 runs
kvm_mmu_split_huge_pages called in
kvm_clear_dirty_log_protect() - manual - 64-page granularity kvm_mmu_split_memory_region() - no manual - all memory
--- arch/arm64/kvm/hyp/pgtable.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/pgtable.c +2 -1
@@ -1565,21 +1565,22 @@ static int stage2_split_walker(const struct kvm_pgtable_visit_ctx *ctx, new =kvm_init_table_pte (childp, mm_ops);stage2_make_pte (ctx, new);return 0 ; }int kvm_pgtable_stage2_split (struct kvm_pgtable *pgt,u64 addr,u64 size,struct kvm_mmu_memory_cache *mc) {struct kvm_pgtable_walker walker = { .cb = stage2_split_walker, - .flags =KVM_PGTABLE_WALK_LEAF , + .flags =KVM_PGTABLE_WALK_LEAF | +KVM_PGTABLE_WALK_SKIP_LEVEL3 , .arg = mc, };int ret; ret =kvm_pgtable_walk (pgt, addr, size, &walker);dsb (ishst);return ret; } int __kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
Signature · 1 line
2.55.0
Quoted 70 lines · Leonardo Bras
While playing with dirty-bit tracking, I decided to take a look on how page splitting works. Found out all entries are walked, even though we can infer, for instance that:
- If a level-3 entry is walked, it means the parent level-2 entry is split
- If a split just succeeded in an table entry, it means all children nodes are already splitThis patches' idea is to introduce new walking flags to skip pagetable levels 0-3.
The idea of skipping child nodes was also tested, but it was marginally slower than just skipping levels, so it was discarted.
Optimization measured on two scenarios involving eager-splitting on a VM with 32 memslot of 2GB (total 64GB), and vcpu per slot:
- Scenario 1: No manual protect, whole memslot split at dirty-track enable (KVM_SET_USER_MEMORY_REGION2ioctl withKVM_MEM_LOG_DIRTY_PAGES)
- Split happens only once, whole region
- Evalutes improved batch performance of splitting
- Scenario 2: Manual protect, split happens during every dirty-bit clean (KVM_CLEAR_DIRTY_LOGioctl), average for 2 iterations.
- Split called multiple times, for smaller 64-page sections.
- Evaluate improved performance for multiple callsScenario 1, improvement on dirty-track enable ioctl for the memslot:
- Memory was already split (4k pages): -43.18% runtime (stdev 3.87%) - THP backed memory: -25.77% runtime (stdev 1.01%) - 64x1GB hugetlb memory: -25.62% runtime (stdev 1.09%)Scenario 2, improvement on dirty-log clean ioctl for the memslot:
- Memory was already split (4k pages): -39.88% runtime (stdev 2.71%) - THP backed memory: -25.18% runtime (stdev 0.32%) - 64x1GB hugetlb memory: -49.17% runtime (stdev 1.86%)For collecting above numbers, the following script was ran in both vanilla and patched kernels, with kernel parameter 'default_hugepagesz=1G', on an TX2 with 128GB RAM.
---
dirty_test.sh
#!/bin/bash
filename=$(uname -r |cut -d'-' -f 4-)run_test(){
base_test="./dirty_log_perf_test -b 2G -v 32 -m 6 -m 8"# Manual cleaning disable ${base_test} -g ${base_test} -g -s anonymous_thp echo 64 > /proc/sys/vm/nr_hugepages ${base_test} -g -s shared_hugetlb echo 0 > /proc/sys/vm/nr_hugepages# Manual cleaning enable
${base_test}
${base_test} -sanonymous_thp
echo 64 >/proc/sys/vm/nr_hugepages
${base_test} -sshared_hugetlb
echo 0 >/proc/sys/vm/nr_hugepages
}
run_test2>&1 | tee ${filename}
---Above
dirty_log_perf_testcommand is the standard kvm selftest found in the kernel tree. It tested the following guest modes:
Testing guest mode: PA-bits:40, VA-bits:48, 4K pages
Testing guest mode: PA-bits:40, VA-bits:48, 64K pages
(Modes with PA-bits:36 were discarted in this version, given the amount of RAM being used for testing, and the similarity of previous results)
Quoted 8 lines
Performance numbers from above modes were used to calculate average and stdev showed in the optimization results.
Changes since v2:
- Rebased on top of v7.2-rc1
- Improved testing, added more memory, re-tested
- Now: 32 vcpus @ total of 64G
- Before: 1cpu @ 16G
v2 Link: https://lore.kernel.org/all/[email protected]/
Quoted 32 lines
Changes since v1:
- Fixed inverted flag verification priority (Sashiko)
- Fixed incorrectly skipping POST call if level was skipped (Sashiko), and to that
- New pre-patch that changes goto-out -> return to avoid re-testingwalk_continuev1 Link: https://lore.kernel.org/lkml/[email protected]/Changes since RFC:
- Changed approach from return value to walk flags (Will Deacon)
- Discartedskip_childapproach (Oliver Upton)
- Measured in real hardware, and from userspace perspective (Marc Zyngier)
- Better explanation of what and how numbers were collected
RFC Link: https://lore.kernel.org/all/[email protected]/Thanks!
LeoLeonardo Bras (3):
KVM: arm64: Avoid re-testingwalk_continue
KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
KVM: arm64: Makestage2_split_walker()skip unnecessary walksarch/arm64/include/asm/kvm_pgtable.h | 13 +++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 28 +++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-)base-commit:
dc59e4fea9d83f03bad6bddf3fa2e52491777482Signature · 1 line
2.55.0
Quoted 6 lines · Leonardo Bras
While playing with dirty-bit tracking, I decided to take a look on how page splitting works. Found out all entries are walked, even though we can infer, for instance that:
- If a level-3 entry is walked, it means the parent level-2 entry is split
- If a split just succeeded in an table entry, it means all children nodes are already split
That does not happen for force_pte = false right? Or is it guaranteed that in your case that force_pte = true always?
Quoted 101 lines
This patches' idea is to introduce new walking flags to skip pagetable levels 0-3.
The idea of skipping child nodes was also tested, but it was marginally slower than just skipping levels, so it was discarted.
Optimization measured on two scenarios involving eager-splitting on a VM with 32 memslot of 2GB (total 64GB), and vcpu per slot:
- Scenario 1: No manual protect, whole memslot split at dirty-track enable (KVM_SET_USER_MEMORY_REGION2ioctl withKVM_MEM_LOG_DIRTY_PAGES)
- Split happens only once, whole region
- Evalutes improved batch performance of splitting
- Scenario 2: Manual protect, split happens during every dirty-bit clean (KVM_CLEAR_DIRTY_LOGioctl), average for 2 iterations.
- Split called multiple times, for smaller 64-page sections.
- Evaluate improved performance for multiple callsScenario 1, improvement on dirty-track enable ioctl for the memslot:
- Memory was already split (4k pages): -43.18% runtime (stdev 3.87%) - THP backed memory: -25.77% runtime (stdev 1.01%) - 64x1GB hugetlb memory: -25.62% runtime (stdev 1.09%)Scenario 2, improvement on dirty-log clean ioctl for the memslot:
- Memory was already split (4k pages): -39.88% runtime (stdev 2.71%) - THP backed memory: -25.18% runtime (stdev 0.32%) - 64x1GB hugetlb memory: -49.17% runtime (stdev 1.86%)For collecting above numbers, the following script was ran in both vanilla and patched kernels, with kernel parameter 'default_hugepagesz=1G', on an TX2 with 128GB RAM.
---
dirty_test.sh
#!/bin/bash
filename=$(uname -r |cut -d'-' -f 4-)run_test(){
base_test="./dirty_log_perf_test -b 2G -v 32 -m 6 -m 8"# Manual cleaning disable ${base_test} -g ${base_test} -g -s anonymous_thp echo 64 > /proc/sys/vm/nr_hugepages ${base_test} -g -s shared_hugetlb echo 0 > /proc/sys/vm/nr_hugepages# Manual cleaning enable
${base_test}
${base_test} -sanonymous_thp
echo 64 >/proc/sys/vm/nr_hugepages
${base_test} -sshared_hugetlb
echo 0 >/proc/sys/vm/nr_hugepages
}
run_test2>&1 | tee ${filename}
---Above
dirty_log_perf_testcommand is the standard kvm selftest found in the kernel tree. It tested the following guest modes:
Testing guest mode: PA-bits:40, VA-bits:48, 4K pages
Testing guest mode: PA-bits:40, VA-bits:48, 64K pages
(Modes with PA-bits:36 were discarted in this version, given the amount of RAM being used for testing, and the similarity of previous results)Performance numbers from above modes were used to calculate average and stdev showed in the optimization results.
Changes since v2:
- Rebased on top of v7.2-rc1
- Improved testing, added more memory, re-tested
- Now: 32 vcpus @ total of 64G
- Before: 1cpu @ 16GChanges since v1:
- Fixed inverted flag verification priority (Sashiko)
- Fixed incorrectly skipping POST call if level was skipped (Sashiko), and to that
- New pre-patch that changes goto-out -> return to avoid re-testingwalk_continuev1 Link: https://lore.kernel.org/lkml/[email protected]/Changes since RFC:
- Changed approach from return value to walk flags (Will Deacon)
- Discartedskip_childapproach (Oliver Upton)
- Measured in real hardware, and from userspace perspective (Marc Zyngier)
- Better explanation of what and how numbers were collected
RFC Link: https://lore.kernel.org/all/[email protected]/Thanks!
LeoLeonardo Bras (3):
KVM: arm64: Avoid re-testingwalk_continue
KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
KVM: arm64: Makestage2_split_walker()skip unnecessary walksarch/arm64/include/asm/kvm_pgtable.h | 13 +++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 28 +++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-)base-commit:
dc59e4fea9d83f03bad6bddf3fa2e52491777482
Quoted 9 lines · Dev Jain
Quoted 6 lines · Leonardo Bras
While playing with dirty-bit tracking, I decided to take a look on how page splitting works. Found out all entries are walked, even though we can infer, for instance that:
- If a level-3 entry is walked, it means the parent level-2 entry is split
- If a split just succeeded in an table entry, it means all children nodes are already split
Hi Dev, thanks for reviewing!
Quoted 3 lines
That does not happen for
force_pte= false right? Or is it guaranteed that in your case thatforce_pte= true always?
IIUC for kvm_pgtable_stage2_split(), force_pte will affect the creation of pagetables, and will only happen to be false if the cache used for splitting hugepages does not contain enough pages to split the whole subtree. In this case, it only maps one page of either table entries or ptes.
I am not sure how often does that happen for the memory cache not being able to split everythig, but I guess it's only when memory is tight in the system. (Haven't checked, I can be wrong here)
But in any case, the idea of optimizing this was discarted because it ended up being faster to just skip by level.
Quoted 6 lines
Quoted 6 lines
This patches' idea is to introduce new walking flags to skip pagetable levels 0-3.
The idea of skipping child nodes was also tested, but it was marginally slower than just skipping levels, so it was discarted.
^ Here
Thanks!
Leo
Quoted 96 lines
Quoted 95 lines
Optimization measured on two scenarios involving eager-splitting on a VM with 32 memslot of 2GB (total 64GB), and vcpu per slot:
- Scenario 1: No manual protect, whole memslot split at dirty-track enable (KVM_SET_USER_MEMORY_REGION2ioctl withKVM_MEM_LOG_DIRTY_PAGES)
- Split happens only once, whole region
- Evalutes improved batch performance of splitting
- Scenario 2: Manual protect, split happens during every dirty-bit clean (KVM_CLEAR_DIRTY_LOGioctl), average for 2 iterations.
- Split called multiple times, for smaller 64-page sections.
- Evaluate improved performance for multiple callsScenario 1, improvement on dirty-track enable ioctl for the memslot:
- Memory was already split (4k pages): -43.18% runtime (stdev 3.87%) - THP backed memory: -25.77% runtime (stdev 1.01%) - 64x1GB hugetlb memory: -25.62% runtime (stdev 1.09%)Scenario 2, improvement on dirty-log clean ioctl for the memslot:
- Memory was already split (4k pages): -39.88% runtime (stdev 2.71%) - THP backed memory: -25.18% runtime (stdev 0.32%) - 64x1GB hugetlb memory: -49.17% runtime (stdev 1.86%)For collecting above numbers, the following script was ran in both vanilla and patched kernels, with kernel parameter 'default_hugepagesz=1G', on an TX2 with 128GB RAM.
---
dirty_test.sh
#!/bin/bash
filename=$(uname -r |cut -d'-' -f 4-)run_test(){
base_test="./dirty_log_perf_test -b 2G -v 32 -m 6 -m 8"# Manual cleaning disable ${base_test} -g ${base_test} -g -s anonymous_thp echo 64 > /proc/sys/vm/nr_hugepages ${base_test} -g -s shared_hugetlb echo 0 > /proc/sys/vm/nr_hugepages# Manual cleaning enable
${base_test}
${base_test} -sanonymous_thp
echo 64 >/proc/sys/vm/nr_hugepages
${base_test} -sshared_hugetlb
echo 0 >/proc/sys/vm/nr_hugepages
}
run_test2>&1 | tee ${filename}
---Above
dirty_log_perf_testcommand is the standard kvm selftest found in the kernel tree. It tested the following guest modes:
Testing guest mode: PA-bits:40, VA-bits:48, 4K pages
Testing guest mode: PA-bits:40, VA-bits:48, 64K pages
(Modes with PA-bits:36 were discarted in this version, given the amount of RAM being used for testing, and the similarity of previous results)Performance numbers from above modes were used to calculate average and stdev showed in the optimization results.
Changes since v2:
- Rebased on top of v7.2-rc1
- Improved testing, added more memory, re-tested
- Now: 32 vcpus @ total of 64G
- Before: 1cpu @ 16GChanges since v1:
- Fixed inverted flag verification priority (Sashiko)
- Fixed incorrectly skipping POST call if level was skipped (Sashiko), and to that
- New pre-patch that changes goto-out -> return to avoid re-testingwalk_continuev1 Link: https://lore.kernel.org/lkml/[email protected]/Changes since RFC:
- Changed approach from return value to walk flags (Will Deacon)
- Discartedskip_childapproach (Oliver Upton)
- Measured in real hardware, and from userspace perspective (Marc Zyngier)
- Better explanation of what and how numbers were collected
RFC Link: https://lore.kernel.org/all/[email protected]/Thanks!
LeoLeonardo Bras (3):
KVM: arm64: Avoid re-testingwalk_continue
KVM: arm64: Introduce KVM_PGTABLE_WALK_SKIP_LEVEL* walk flags
KVM: arm64: Makestage2_split_walker()skip unnecessary walksarch/arm64/include/asm/kvm_pgtable.h | 13 +++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 28 +++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-)base-commit:
dc59e4fea9d83f03bad6bddf3fa2e52491777482