* [PATCH v2 1/4] KVM: arm64: Don't defer TLB invalidation when zapping table entries
2024-03-27 12:48 [PATCH v2 0/4] KVM: arm64: TLBI fixes for the pgtable code Will Deacon
@ 2024-03-27 12:48 ` Will Deacon
2024-03-27 12:48 ` [PATCH v2 2/4] KVM: arm64: Don't pass a TLBI level hint " Will Deacon
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Will Deacon @ 2024-03-27 12:48 UTC (permalink / raw)
To: kvmarm
Cc: linux-arm-kernel, Will Deacon, Catalin Marinas, Gavin Shan,
Marc Zyngier, Mostafa Saleh, Oliver Upton, Quentin Perret,
Raghavendra Rao Ananta, Ryan Roberts, Shaoqin Huang
Commit 7657ea920c54 ("KVM: arm64: Use TLBI range-based instructions for
unmap") introduced deferred TLB invalidation for the stage-2 page-table
so that range-based invalidation can be used for the accumulated
addresses. This works fine if the structure of the page-tables remains
unchanged, but if entire tables are zapped and subsequently freed then
we transiently leave the hardware page-table walker with a reference
to freed memory thanks to the translation walk caches. For example,
stage2_unmap_walker() will free page-table pages:
if (childp)
mm_ops->put_page(childp);
and issue the TLB invalidation later in kvm_pgtable_stage2_unmap():
if (stage2_unmap_defer_tlb_flush(pgt))
/* Perform the deferred TLB invalidations */
kvm_tlb_flush_vmid_range(pgt->mmu, addr, size);
For now, take the conservative approach and invalidate the TLB eagerly
when we clear a table entry. Note, however, that the existing level
hint passed to __kvm_tlb_flush_vmid_ipa() is incorrect and will be
fixed in a subsequent patch.
Cc: Raghavendra Rao Ananta <rananta@google.com>
Cc: Shaoqin Huang <shahuang@redhat.com>
Cc: Marc Zyngier <maz@kernel.org>
Cc: Oliver Upton <oliver.upton@linux.dev>
Signed-off-by: Will Deacon <will@kernel.org>
---
arch/arm64/kvm/hyp/pgtable.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c
index 3fae5830f8d2..de0b667ba296 100644
--- a/arch/arm64/kvm/hyp/pgtable.c
+++ b/arch/arm64/kvm/hyp/pgtable.c
@@ -896,9 +896,11 @@ static void stage2_unmap_put_pte(const struct kvm_pgtable_visit_ctx *ctx,
if (kvm_pte_valid(ctx->old)) {
kvm_clear_pte(ctx->ptep);
- if (!stage2_unmap_defer_tlb_flush(pgt))
+ if (!stage2_unmap_defer_tlb_flush(pgt) ||
+ kvm_pte_table(ctx->old, ctx->level)) {
kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu,
ctx->addr, ctx->level);
+ }
}
mm_ops->put_page(ctx->ptep);
--
2.44.0.396.g6e790dbe36-goog
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v2 2/4] KVM: arm64: Don't pass a TLBI level hint when zapping table entries
2024-03-27 12:48 [PATCH v2 0/4] KVM: arm64: TLBI fixes for the pgtable code Will Deacon
2024-03-27 12:48 ` [PATCH v2 1/4] KVM: arm64: Don't defer TLB invalidation when zapping table entries Will Deacon
@ 2024-03-27 12:48 ` Will Deacon
2024-03-27 12:48 ` [PATCH v2 3/4] KVM: arm64: Use TLBI_TTL_UNKNOWN in __kvm_tlb_flush_vmid_range() Will Deacon
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Will Deacon @ 2024-03-27 12:48 UTC (permalink / raw)
To: kvmarm
Cc: linux-arm-kernel, Will Deacon, Catalin Marinas, Gavin Shan,
Marc Zyngier, Mostafa Saleh, Oliver Upton, Quentin Perret,
Raghavendra Rao Ananta, Ryan Roberts, Shaoqin Huang
The TLBI level hints are for leaf entries only, so take care not to pass
them incorrectly after clearing a table entry.
Cc: Gavin Shan <gshan@redhat.com>
Cc: Marc Zyngier <maz@kernel.org>
Cc: Quentin Perret <qperret@google.com>
Fixes: 82bb02445de5 ("KVM: arm64: Implement kvm_pgtable_hyp_unmap() at EL2")
Fixes: 6d9d2115c480 ("KVM: arm64: Add support for stage-2 map()/unmap() in generic page-table")
Signed-off-by: Will Deacon <will@kernel.org>
---
arch/arm64/kvm/hyp/pgtable.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c
index de0b667ba296..a40dafc43bb6 100644
--- a/arch/arm64/kvm/hyp/pgtable.c
+++ b/arch/arm64/kvm/hyp/pgtable.c
@@ -528,7 +528,7 @@ static int hyp_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx,
kvm_clear_pte(ctx->ptep);
dsb(ishst);
- __tlbi_level(vae2is, __TLBI_VADDR(ctx->addr, 0), ctx->level);
+ __tlbi_level(vae2is, __TLBI_VADDR(ctx->addr, 0), TLBI_TTL_UNKNOWN);
} else {
if (ctx->end - ctx->addr < granule)
return -EINVAL;
@@ -896,10 +896,12 @@ static void stage2_unmap_put_pte(const struct kvm_pgtable_visit_ctx *ctx,
if (kvm_pte_valid(ctx->old)) {
kvm_clear_pte(ctx->ptep);
- if (!stage2_unmap_defer_tlb_flush(pgt) ||
- kvm_pte_table(ctx->old, ctx->level)) {
- kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu,
- ctx->addr, ctx->level);
+ if (kvm_pte_table(ctx->old, ctx->level)) {
+ kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, ctx->addr,
+ TLBI_TTL_UNKNOWN);
+ } else if (!stage2_unmap_defer_tlb_flush(pgt)) {
+ kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, ctx->addr,
+ ctx->level);
}
}
--
2.44.0.396.g6e790dbe36-goog
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v2 3/4] KVM: arm64: Use TLBI_TTL_UNKNOWN in __kvm_tlb_flush_vmid_range()
2024-03-27 12:48 [PATCH v2 0/4] KVM: arm64: TLBI fixes for the pgtable code Will Deacon
2024-03-27 12:48 ` [PATCH v2 1/4] KVM: arm64: Don't defer TLB invalidation when zapping table entries Will Deacon
2024-03-27 12:48 ` [PATCH v2 2/4] KVM: arm64: Don't pass a TLBI level hint " Will Deacon
@ 2024-03-27 12:48 ` Will Deacon
2024-03-27 12:48 ` [PATCH v2 4/4] KVM: arm64: Ensure target address is granule-aligned for range TLBI Will Deacon
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Will Deacon @ 2024-03-27 12:48 UTC (permalink / raw)
To: kvmarm
Cc: linux-arm-kernel, Will Deacon, Catalin Marinas, Gavin Shan,
Marc Zyngier, Mostafa Saleh, Oliver Upton, Quentin Perret,
Raghavendra Rao Ananta, Ryan Roberts, Shaoqin Huang
Commit c910f2b65518 ("arm64/mm: Update tlb invalidation routines for
FEAT_LPA2") updated the __tlbi_level() macro to take the target level
as an argument, with TLBI_TTL_UNKNOWN (rather than 0) indicating that
the caller cannot provide level information. Unfortunately, the two
implementations of __kvm_tlb_flush_vmid_range() were not updated and so
now ask for an level 0 invalidation if FEAT_LPA2 is implemented.
Fix the problem by passing TLBI_TTL_UNKNOWN instead of 0 as the level
argument to __flush_s2_tlb_range_op() in __kvm_tlb_flush_vmid_range().
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Oliver Upton <oliver.upton@linux.dev>
Cc: Marc Zyngier <maz@kernel.org>
Reviewed-by: Ryan Roberts <ryan.roberts@arm.com>
Fixes: c910f2b65518 ("arm64/mm: Update tlb invalidation routines for FEAT_LPA2")
Signed-off-by: Will Deacon <will@kernel.org>
---
arch/arm64/kvm/hyp/nvhe/tlb.c | 3 ++-
arch/arm64/kvm/hyp/vhe/tlb.c | 3 ++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/kvm/hyp/nvhe/tlb.c b/arch/arm64/kvm/hyp/nvhe/tlb.c
index a60fb13e2192..2fc68da4036d 100644
--- a/arch/arm64/kvm/hyp/nvhe/tlb.c
+++ b/arch/arm64/kvm/hyp/nvhe/tlb.c
@@ -154,7 +154,8 @@ void __kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu,
/* Switch to requested VMID */
__tlb_switch_to_guest(mmu, &cxt, false);
- __flush_s2_tlb_range_op(ipas2e1is, start, pages, stride, 0);
+ __flush_s2_tlb_range_op(ipas2e1is, start, pages, stride,
+ TLBI_TTL_UNKNOWN);
dsb(ish);
__tlbi(vmalle1is);
diff --git a/arch/arm64/kvm/hyp/vhe/tlb.c b/arch/arm64/kvm/hyp/vhe/tlb.c
index b32e2940df7d..1a60b95381e8 100644
--- a/arch/arm64/kvm/hyp/vhe/tlb.c
+++ b/arch/arm64/kvm/hyp/vhe/tlb.c
@@ -171,7 +171,8 @@ void __kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu,
/* Switch to requested VMID */
__tlb_switch_to_guest(mmu, &cxt);
- __flush_s2_tlb_range_op(ipas2e1is, start, pages, stride, 0);
+ __flush_s2_tlb_range_op(ipas2e1is, start, pages, stride,
+ TLBI_TTL_UNKNOWN);
dsb(ish);
__tlbi(vmalle1is);
--
2.44.0.396.g6e790dbe36-goog
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v2 4/4] KVM: arm64: Ensure target address is granule-aligned for range TLBI
2024-03-27 12:48 [PATCH v2 0/4] KVM: arm64: TLBI fixes for the pgtable code Will Deacon
` (2 preceding siblings ...)
2024-03-27 12:48 ` [PATCH v2 3/4] KVM: arm64: Use TLBI_TTL_UNKNOWN in __kvm_tlb_flush_vmid_range() Will Deacon
@ 2024-03-27 12:48 ` Will Deacon
2024-03-30 10:00 ` [PATCH v2 0/4] KVM: arm64: TLBI fixes for the pgtable code Marc Zyngier
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Will Deacon @ 2024-03-27 12:48 UTC (permalink / raw)
To: kvmarm
Cc: linux-arm-kernel, Will Deacon, Catalin Marinas, Gavin Shan,
Marc Zyngier, Mostafa Saleh, Oliver Upton, Quentin Perret,
Raghavendra Rao Ananta, Ryan Roberts, Shaoqin Huang
When zapping a table entry in stage2_try_break_pte(), we issue range
TLB invalidation for the region that was mapped by the table. However,
we neglect to align the base address down to the granule size and so
if we ended up reaching the table entry via a misaligned address then
we will accidentally skip invalidation for some prefix of the affected
address range.
Align 'ctx->addr' down to the granule size when performing TLB
invalidation for an unmapped table in stage2_try_break_pte().
Cc: Raghavendra Rao Ananta <rananta@google.com>
Cc: Gavin Shan <gshan@redhat.com>
Cc: Shaoqin Huang <shahuang@redhat.com>
Cc: Quentin Perret <qperret@google.com>
Fixes: defc8cc7abf0 ("KVM: arm64: Invalidate the table entries upon a range")
Signed-off-by: Will Deacon <will@kernel.org>
---
arch/arm64/kvm/hyp/pgtable.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c
index a40dafc43bb6..5a59ef88b646 100644
--- a/arch/arm64/kvm/hyp/pgtable.c
+++ b/arch/arm64/kvm/hyp/pgtable.c
@@ -843,12 +843,15 @@ static bool stage2_try_break_pte(const struct kvm_pgtable_visit_ctx *ctx,
* Perform the appropriate TLB invalidation based on the
* evicted pte value (if any).
*/
- if (kvm_pte_table(ctx->old, ctx->level))
- kvm_tlb_flush_vmid_range(mmu, ctx->addr,
- kvm_granule_size(ctx->level));
- else if (kvm_pte_valid(ctx->old))
+ if (kvm_pte_table(ctx->old, ctx->level)) {
+ u64 size = kvm_granule_size(ctx->level);
+ u64 addr = ALIGN_DOWN(ctx->addr, size);
+
+ kvm_tlb_flush_vmid_range(mmu, addr, size);
+ } else if (kvm_pte_valid(ctx->old)) {
kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu,
ctx->addr, ctx->level);
+ }
}
if (stage2_pte_is_counted(ctx->old))
--
2.44.0.396.g6e790dbe36-goog
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH v2 0/4] KVM: arm64: TLBI fixes for the pgtable code
2024-03-27 12:48 [PATCH v2 0/4] KVM: arm64: TLBI fixes for the pgtable code Will Deacon
` (3 preceding siblings ...)
2024-03-27 12:48 ` [PATCH v2 4/4] KVM: arm64: Ensure target address is granule-aligned for range TLBI Will Deacon
@ 2024-03-30 10:00 ` Marc Zyngier
2024-04-01 2:57 ` Shaoqin Huang
2024-04-01 8:46 ` Oliver Upton
6 siblings, 0 replies; 8+ messages in thread
From: Marc Zyngier @ 2024-03-30 10:00 UTC (permalink / raw)
To: Will Deacon
Cc: kvmarm, linux-arm-kernel, Catalin Marinas, Gavin Shan,
Mostafa Saleh, Oliver Upton, Quentin Perret,
Raghavendra Rao Ananta, Ryan Roberts, Shaoqin Huang
On Wed, 27 Mar 2024 12:48:49 +0000,
Will Deacon <will@kernel.org> wrote:
>
> Hi again,
>
> This is version two of the series I previously posted on Monday:
>
> https://lore.kernel.org/r/20240325185158.8565-1-will@kernel.org
>
> We've got a long weekend coming up in the UK, so I wanted to get this
> out before I chuck the laptop in the river.
>
> Changes since v1 include:
>
> * Add Ryan's Reviewed-by on the third patch
> * Add an extra patch to ensure correct alignment of range TLBI address
> argument
> * Tweak commit messages
For the series,
Reviewed-by: Marc Zyngier <maz@kernel.org>
M.
--
Without deviation from the norm, progress is not possible.
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH v2 0/4] KVM: arm64: TLBI fixes for the pgtable code
2024-03-27 12:48 [PATCH v2 0/4] KVM: arm64: TLBI fixes for the pgtable code Will Deacon
` (4 preceding siblings ...)
2024-03-30 10:00 ` [PATCH v2 0/4] KVM: arm64: TLBI fixes for the pgtable code Marc Zyngier
@ 2024-04-01 2:57 ` Shaoqin Huang
2024-04-01 8:46 ` Oliver Upton
6 siblings, 0 replies; 8+ messages in thread
From: Shaoqin Huang @ 2024-04-01 2:57 UTC (permalink / raw)
To: Will Deacon, kvmarm
Cc: linux-arm-kernel, Catalin Marinas, Gavin Shan, Marc Zyngier,
Mostafa Saleh, Oliver Upton, Quentin Perret,
Raghavendra Rao Ananta, Ryan Roberts
On 3/27/24 20:48, Will Deacon wrote:
> Hi again,
>
> This is version two of the series I previously posted on Monday:
>
> https://lore.kernel.org/r/20240325185158.8565-1-will@kernel.org
>
> We've got a long weekend coming up in the UK, so I wanted to get this
> out before I chuck the laptop in the river.
>
> Changes since v1 include:
>
> * Add Ryan's Reviewed-by on the third patch
> * Add an extra patch to ensure correct alignment of range TLBI address
> argument
> * Tweak commit messages
>
> Cheers,
>
> Will
Reviewed-by: Shaoqin Huang <shahuang@redhat.com>
>
> Cc: Catalin Marinas <catalin.marinas@arm.com>
> Cc: Gavin Shan <gshan@redhat.com>
> Cc: Marc Zyngier <maz@kernel.org>
> Cc: Mostafa Saleh <smostafa@google.com>
> Cc: Oliver Upton <oliver.upton@linux.dev>
> Cc: Quentin Perret <qperret@google.com>
> Cc: Raghavendra Rao Ananta <rananta@google.com>
> Cc: Ryan Roberts <ryan.roberts@arm.com>
> Cc: Shaoqin Huang <shahuang@redhat.com>
>
> --->8
>
> Will Deacon (4):
> KVM: arm64: Don't defer TLB invalidation when zapping table entries
> KVM: arm64: Don't pass a TLBI level hint when zapping table entries
> KVM: arm64: Use TLBI_TTL_UNKNOWN in __kvm_tlb_flush_vmid_range()
> KVM: arm64: Ensure target address is granule-aligned for range TLBI
>
> arch/arm64/kvm/hyp/nvhe/tlb.c | 3 ++-
> arch/arm64/kvm/hyp/pgtable.c | 23 +++++++++++++++--------
> arch/arm64/kvm/hyp/vhe/tlb.c | 3 ++-
> 3 files changed, 19 insertions(+), 10 deletions(-)
>
--
Shaoqin
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH v2 0/4] KVM: arm64: TLBI fixes for the pgtable code
2024-03-27 12:48 [PATCH v2 0/4] KVM: arm64: TLBI fixes for the pgtable code Will Deacon
` (5 preceding siblings ...)
2024-04-01 2:57 ` Shaoqin Huang
@ 2024-04-01 8:46 ` Oliver Upton
6 siblings, 0 replies; 8+ messages in thread
From: Oliver Upton @ 2024-04-01 8:46 UTC (permalink / raw)
To: kvmarm, Will Deacon
Cc: Oliver Upton, linux-arm-kernel, Catalin Marinas, Gavin Shan,
Marc Zyngier, Mostafa Saleh, Quentin Perret,
Raghavendra Rao Ananta, Ryan Roberts, Shaoqin Huang
On Wed, 27 Mar 2024 12:48:49 +0000, Will Deacon wrote:
> This is version two of the series I previously posted on Monday:
>
> https://lore.kernel.org/r/20240325185158.8565-1-will@kernel.org
>
> We've got a long weekend coming up in the UK, so I wanted to get this
> out before I chuck the laptop in the river.
>
> [...]
Applied to kvmarm/fixes, thanks!
[1/4] KVM: arm64: Don't defer TLB invalidation when zapping table entries
commit: f62d4c3eb687d87b616b4279acec7862553bda77
[2/4] KVM: arm64: Don't pass a TLBI level hint when zapping table entries
commit: 36e008323926036650299cfbb2dca704c7aba849
[3/4] KVM: arm64: Use TLBI_TTL_UNKNOWN in __kvm_tlb_flush_vmid_range()
commit: 0f0ff097bf77663b8d2692e33d56119947611bb0
[4/4] KVM: arm64: Ensure target address is granule-aligned for range TLBI
commit: 4c36a156738887c1edd78589fe192d757989bcde
--
Best,
Oliver
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 8+ messages in thread