Skip to contentSkip to conversation

arm: cpu: Add optional CMOs by VA

PATCH Superseded Ying-Chun Liu (PaulLiu) 3 participants 9 messages Feb 7, 2023 – Feb 8, 2023 Arm (32-bit)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover arm: cpu: Add optional CMOs by VA

Exposing set/way cache maintenance to a virtual machine is unsafe, not least because the instructions are not permission-checked but also because they are not broadcast between CPUs. Consequently, KVM traps and emulates such maintenance in the host kernel using by-VA operations and looping over the stage-2 page-tables. However, when running under protected KVM, these instructions are not able to be emulated and will instead result in an exception being delivered to the guest.

Introduce CONFIG_CMO_BY_VA_ONLY so that virtual platforms can select this option and perform by-VA cache maintenance instead of using the set/way instructions.

Ying-Chun Liu (PaulLiu) (2):
arm: cpu: Add optional CMOs by VA
arm64: Initialize TLB memory if CMO_BY_VA_ONLY

 arch/arm/cpu/armv8/Kconfig    |  4 ++
 arch/arm/cpu/armv8/cache.S    | 50 +++++++++++++-----
 arch/arm/cpu/armv8/cache_v8.c | 97 ++++++++++++++++++++++++++++++++++-
 arch/arm/cpu/armv8/cpu.c      | 30 +++++++----
 arch/arm/lib/cache.c          |  9 ++++
 5 files changed, 164 insertions(+), 26 deletions(-)
Signature · 1 line
2.39.1
#1 lore raw

PATCH 1/2 arm: cpu: Add optional CMOs by VA

Exposing set/way cache maintenance to a virtual machine is unsafe, not least because the instructions are not permission-checked but also because they are not broadcast between CPUs. Consequently, KVM traps and emulates such maintenance in the host kernel using by-VA operations and looping over the stage-2 page-tables. However, when running under protected KVM, these instructions are not able to be emulated and will instead result in an exception being delivered to the guest.

Introduce CONFIG_CMO_BY_VA_ONLY so that virtual platforms can select this option and perform by-VA cache maintenance instead of using the set/way instructions.

---
 arch/arm/cpu/armv8/Kconfig    |  4 ++
 arch/arm/cpu/armv8/cache.S    | 50 +++++++++++++-----
 arch/arm/cpu/armv8/cache_v8.c | 97 ++++++++++++++++++++++++++++++++++-
 arch/arm/cpu/armv8/cpu.c      | 30 +++++++----
 4 files changed, 155 insertions(+), 26 deletions(-)
arch/arm/cpu/armv8/Kconfig +4 -0
index 1305238c9d..7d5cf1594d 100644
@@ -1,5 +1,9 @@
 if ARM64
 
+config CMO_BY_VA_ONLY
+	bool "Force cache maintenance to be exclusively by VA"
+	depends on !SYS_DISABLE_DCACHE_OPS
+
 config ARMV8_SPL_EXCEPTION_VECTORS
 	bool "Install crash dump exception vectors"
 	depends on SPL
arch/arm/cpu/armv8/cache.S +36 -14

68 lines not on this page · raw message

arch/arm/cpu/armv8/cache_v8.c +96 -1

131 lines not on this page · raw message

arch/arm/cpu/armv8/cpu.c +19 -11
index db5d460eb4..3c7f36ad8d 100644
@@ -48,18 +48,26 @@ int cleanup_before_linux(void)
 
 	disable_interrupts();
 
-	/*
-	 * Turn off I-cache and invalidate it
-	 */
-	icache_disable();
-	invalidate_icache_all();
+	if (IS_ENABLED(CONFIG_CMO_BY_VA_ONLY)) {
+		/*
+		 * Disable D-cache.
+		 */
+		dcache_disable();
+	} else {
+		/*
+		 * Turn off I-cache and invalidate it
+		 */
+		icache_disable();
+		invalidate_icache_all();
 
-	/*
-	 * turn off D-cache
-	 * dcache_disable() in turn flushes the d-cache and disables MMU
-	 */
-	dcache_disable();
-	invalidate_dcache_all();
+		/*
+		 * turn off D-cache
+		 * dcache_disable() in turn flushes the d-cache and disables
+		 * MMU
+		 */
+		dcache_disable();
+		invalidate_dcache_all();
+	}
 
 	return 0;
 }
Signature · 1 line
2.39.1
#2 lore raw
Quoted 8 lines · Ying-Chun Liu (PaulLiu)

Exposing set/way cache maintenance to a virtual machine is unsafe, not least because the instructions are not permission-checked but also because they are not broadcast between CPUs. Consequently, KVM traps and emulates such maintenance in the host kernel using by-VA operations and looping over the stage-2 page-tables. However, when running under protected KVM, these instructions are not able to be emulated and will instead result in an exception being delivered to the guest.

Quoted 8 lines

Introduce CONFIG_CMO_BY_VA_ONLY so that virtual platforms can select this option and perform by-VA cache maintenance instead of using the set/way instructions.

The sign-off chain looks pretty odd. Either you are the author of this patch, and I have nothing to do on the sign-off list, or I'm the author and the authorship is wrong. Similar things would apply for Will.

So which one is it?

        M.
Signature · 1 line
Jazz is not dead. It just smells funny...
#3 lore raw
Quoted 18 lines · Marc Zyngier
Quoted 8 lines · Ying-Chun Liu (PaulLiu)

Exposing set/way cache maintenance to a virtual machine is unsafe, not least because the instructions are not permission-checked but also because they are not broadcast between CPUs. Consequently, KVM traps and emulates such maintenance in the host kernel using by-VA operations and looping over the stage-2 page-tables. However, when running under protected KVM, these instructions are not able to be emulated and will instead result in an exception being delivered to the guest.

Quoted 8 lines

Introduce CONFIG_CMO_BY_VA_ONLY so that virtual platforms can select this option and perform by-VA cache maintenance instead of using the set/way instructions.

Quoted 6 lines

The sign-off chain looks pretty odd. Either you are the author of this patch, and I have nothing to do on the sign-off list, or I'm the author and the authorship is wrong. Similar things would apply for Will.

So which one is it?

As my first guess here is copy and adopting code from Linux, this is not following the documented procedure here:
https://u-boot.readthedocs.io/en/latest/develop/sending_patches.html#attributing-code-copyrights-signing

Which if not sufficiently clear, please ask / suggest changes to. I see right now it isn't specific about cc'ing the original authors (who may, or may not, be interested, so blanket policy doesn't apply) but I would hope is clear enough that what's done in this example isn't right.

Signature · 1 line
Tom
#4 lore raw
Quoted 27 lines · Tom Rini
Quoted 18 lines · Marc Zyngier
Quoted 8 lines · Ying-Chun Liu (PaulLiu)

Exposing set/way cache maintenance to a virtual machine is unsafe, not least because the instructions are not permission-checked but also because they are not broadcast between CPUs. Consequently, KVM traps and emulates such maintenance in the host kernel using by-VA operations and looping over the stage-2 page-tables. However, when running under protected KVM, these instructions are not able to be emulated and will instead result in an exception being delivered to the guest.

Quoted 8 lines

Introduce CONFIG_CMO_BY_VA_ONLY so that virtual platforms can select this option and perform by-VA cache maintenance instead of using the set/way instructions.

Quoted 6 lines

The sign-off chain looks pretty odd. Either you are the author of this patch, and I have nothing to do on the sign-off list, or I'm the author and the authorship is wrong. Similar things would apply for Will.

So which one is it?

Quoted 8 lines

As my first guess here is copy and adopting code from Linux, this is not following the documented procedure here:
https://u-boot.readthedocs.io/en/latest/develop/sending_patches.html#attributing-code-copyrights-signing

Which if not sufficiently clear, please ask / suggest changes to. I see right now it isn't specific about cc'ing the original authors (who may, or may not, be interested, so blanket policy doesn't apply) but I would hope is clear enough that what's done in this example isn't right.

No, this really is u-boot code written as part of Android, from where the patch has been directly lifted[1].

Same goes for Pierre-Clement's patch that is part of the same series.

I'm not overly attached to this code (I have bad memories from it), but I think the OP may be unaware of these rules. In any case, I'm supportive of this code making it in upstream u-boot. I just want it to be done correctly.

Thanks,

	M.

[1] https://android.googlesource.com/platform/external/u-boot/+/db5507f47f4f57f766d52f753ff2cc761afc213b

Signature · 1 line
Without deviation from the norm, progress is not possible.
#5 lore raw
Quoted 48 lines · Marc Zyngier
Quoted 27 lines · Tom Rini
Quoted 18 lines · Marc Zyngier
Quoted 8 lines · Ying-Chun Liu (PaulLiu)

Exposing set/way cache maintenance to a virtual machine is unsafe, not least because the instructions are not permission-checked but also because they are not broadcast between CPUs. Consequently, KVM traps and emulates such maintenance in the host kernel using by-VA operations and looping over the stage-2 page-tables. However, when running under protected KVM, these instructions are not able to be emulated and will instead result in an exception being delivered to the guest.

Quoted 8 lines

Introduce CONFIG_CMO_BY_VA_ONLY so that virtual platforms can select this option and perform by-VA cache maintenance instead of using the set/way instructions.

Quoted 6 lines

The sign-off chain looks pretty odd. Either you are the author of this patch, and I have nothing to do on the sign-off list, or I'm the author and the authorship is wrong. Similar things would apply for Will.

So which one is it?

Quoted 8 lines

As my first guess here is copy and adopting code from Linux, this is not following the documented procedure here:
https://u-boot.readthedocs.io/en/latest/develop/sending_patches.html#attributing-code-copyrights-signing

Which if not sufficiently clear, please ask / suggest changes to. I see right now it isn't specific about cc'ing the original authors (who may, or may not, be interested, so blanket policy doesn't apply) but I would hope is clear enough that what's done in this example isn't right.

No, this really is u-boot code written as part of Android, from where the patch has been directly lifted[1].

Same goes for Pierre-Clement's patch that is part of the same series.

I'm not overly attached to this code (I have bad memories from it), but I think the OP may be unaware of these rules. In any case, I'm supportive of this code making it in upstream u-boot. I just want it to be done correctly.

Quoted 5 lines

Thanks,

	M.

[1] https://android.googlesource.com/platform/external/u-boot/+/db5507f47f4f57f766d52f753ff2cc761afc213b

Oh, hummm. I guess whatever the normal policy for upstreaming patches from an Android kernel tree, to mainline, would be the starting point here? Referencing the Android tree would be good too.

Signature · 1 line
Tom
author
#6 lore raw
In reply to Marc Zyngier

Hi Marc,

I think you are the author. I'm just making some minor fixes and then upstreaming to the mailing list.
What is the correct way to make the Signed-off-by list?

Yours,
Paul

Quoted 30 lines · Marc Zyngier
Quoted 9 lines · Ying-Chun Liu (PaulLiu)

Exposing set/way cache maintenance to a virtual machine is unsafe, not least because the instructions are not permission-checked but also because they are not broadcast between CPUs. Consequently, KVM traps and
emulates such maintenance in the host kernel using by-VA operations and looping over the stage-2 page-tables. However, when running under protected KVM, these instructions are not able to be emulated and will instead result in an exception being delivered to the guest.

Quoted 8 lines

Introduce CONFIG_CMO_BY_VA_ONLY so that virtual platforms can select this option and perform by-VA cache maintenance instead of using the set/way instructions.

The sign-off chain looks pretty odd. Either you are the author of this patch, and I have nothing to do on the sign-off list, or I'm the author and the authorship is wrong. Similar things would apply for Will.

So which one is it?

         M.

--
Jazz is not dead. It just smells funny...

#7 lore raw
Quoted 6 lines · Paul Liu

Hi Marc,

I think you are the author. I'm just making some minor fixes and then upstreaming to the mailing list. What is the correct way to make the Signed-off-by list?

In general, and unless you have completely rewritten the patch (it doesn't seem so in this particular case), you should keep the original authorship and sign-off chain, and add your own Signed-off-by: tag *after* the previous ones.

You should also document what changes you have made, if any, when picking up the patch.

When posting it, it should read something like:

<quote>
From: Random Developer <[email protected]>

Fix foo handling in bar().

[Paul: picked from the FooBar tree, fixed the return
value for bar() and rebased to upstream]

</quote>

where "Random Developer" is the original author, and "Random Committer" is someone who picked up the patch the first place. The important thing here is that, just by looking at the sign-off chain, you can tell how the patch has been handled.

The additional information (enclosed in square bracket) is optional but much appreciated by the reviewers, and so is the link to the original patch, which helps seeing it in context.

If the commits have lost the original authorship (which sometimes happen as you rebase patches and resolve conflicts), you can fix it with:

  git commit --amend --author="Random Developer <[email protected]>"

on each individual commit.

Tom's email also has a good pointer to what is expected from contributors (most of which is applicable to a large number of open-source projects).

Hope this helps,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#8 lore raw

PATCH 2/2 arm64: Initialize TLB memory if CMO_BY_VA_ONLY

In reply to Ying-Chun Liu (PaulLiu)

Memory used to hold the page tables is allocated from the top of RAM with no prior initialization and could therefore hold invalid data. As invalidate_dcache_all() will be called before the MMU has been initialized and as that function relies indirectly on the page tables when using CMO_BY_VA_ONLY, these must be in a valid state from their allocation.

---
 arch/arm/lib/cache.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/arm/lib/cache.c +9 -0
index 1a589c7e2a..7a16015867 100644
@@ -159,6 +159,15 @@ __weak int arm_reserve_mmu(void)
 	 */
 	gd->arch.tlb_allocated = gd->arch.tlb_addr;
 #endif
+
+	if (IS_ENABLED(CONFIG_CMO_BY_VA_ONLY)) {
+		/*
+		 * As invalidate_dcache_all() will be called before
+		 * mmu_setup(), we should make sure that the PTs are
+		 * already in a valid state.
+		 */
+		memset((void *)gd->arch.tlb_addr, 0, gd->arch.tlb_size);
+	}
 #endif
 
 	return 0;
Signature · 1 line
2.39.1