Skip to contentSkip to conversation

MTE support for KVM guest

PATCH Superseded Steven Price 3 participants 49 messages May 17, 2021 – May 27, 2021

Subsystems (4)

v12 of 17 Superseded Latest is v17 →

lore
Share & tools
lore
#0 lore raw

v12 cover MTE support for KVM guest

This series adds support for using the Arm Memory Tagging Extensions (MTE) in a KVM guest.

Changes since v11[1]:

* Series is prefixed with a bug fix for a potential race synchronising tags. This is basically race as was recently[2] fixed for PG_dcache_clean where the update of the page flag cannot be done atomically with the work that flag represents.

For the PG_dcache_clean case the problem is easier because extra cache maintenance isn't a problem, but here restoring the tags twice could cause data loss.

The current solution is a global spinlock for mte_sync_page_tags(). If we hit scalability problems that other solutions such as potentially using another page flag as a lock will need to be investigated.

* The second patch is from Catalin to mitigate the performance impact of the first - by handling the page zeroing case explicitly we can avoid entering mte_sync_page_tags() at all in most cases. Peter Collingbourne has a patch which similarly improves this case using the DC GZVA instruction. So this patch may be dropped in favour of Peter's, however Catalin's is likely easier to backport.

* Use pte_access_permitted() in set_pte_at() to identify pages which may be accessed by the user rather than open-coding a check for PTE_USER. Also add a comment documenting what's going on. There's also some short-cuts added in mte_sync_tags() compared to the previous post, to again mitigate the performance impact of the first patch.

* Move the code to sanitise tags out of user_mem_abort() into its own function. Also call this new function from kvm_set_spte_gfn() as that path was missing the sanitising.

Originally I was going to move the code all the way down to kvm_pgtable_stage2_map(). Sadly as that also part of the EL2 hypervisor this breaks nVHE as the code needs to perform actions in the host.

* Drop the union in struct kvm_vcpu_events - it served no purpose and was confusing.

* Update CAP number (again) and other minor conflict resolutions.

[1] https://lore.kernel.org/r/[email protected]/
[2] https://lore.kernel.org/r/[email protected]/
[3] https://lore.kernel.org/r/de812a02fd94a0dba07d43606bd893c564aa4528.1620849613.git.pcc@google.com/

Catalin Marinas (1):
arm64: Handle MTE tags zeroing in __alloc_zeroed_user_highpage()

Steven Price (7):
arm64: mte: Handle race when synchronising tags
arm64: mte: Sync tags for pages where PTE is untagged
arm64: kvm: Introduce MTE VM feature
arm64: kvm: Save/restore MTE registers
arm64: kvm: Expose KVM_ARM_CAP_MTE
KVM: arm64: ioctl to fetch/store tags in a guest
KVM: arm64: Document MTE capability and ioctl

 Documentation/virt/kvm/api.rst             | 53 +++++++++++++++
 arch/arm64/include/asm/kvm_emulate.h       |  3 +
 arch/arm64/include/asm/kvm_host.h          |  9 +++
 arch/arm64/include/asm/kvm_mte.h           | 66 ++++++++++++++++++
 arch/arm64/include/asm/page.h              |  6 +-
 arch/arm64/include/asm/pgtable.h           |  9 ++-
 arch/arm64/include/asm/sysreg.h            |  3 +-
 arch/arm64/include/uapi/asm/kvm.h          | 11 +++
 arch/arm64/kernel/asm-offsets.c            |  3 +
 arch/arm64/kernel/mte.c                    | 37 ++++++++--
 arch/arm64/kvm/arm.c                       | 78 ++++++++++++++++++++++
 arch/arm64/kvm/hyp/entry.S                 |  7 ++
 arch/arm64/kvm/hyp/exception.c             |  3 +-
 arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 21 ++++++
 arch/arm64/kvm/mmu.c                       | 37 +++++++++-
 arch/arm64/kvm/sys_regs.c                  | 28 ++++++--
 arch/arm64/mm/fault.c                      | 21 ++++++
 include/uapi/linux/kvm.h                   |  2 +
 18 files changed, 381 insertions(+), 16 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_mte.h
Signature · 1 line
2.20.1
#1 lore raw

PATCH v12 1/8 arm64: mte: Handle race when synchronising tags

mte_sync_tags() used test_and_set_bit() to set the PG_mte_tagged flag before restoring/zeroing the MTE tags. However if another thread were to race and attempt to sync the tags on the same page before the first thread had completed restoring/zeroing then it would see the flag is already set and continue without waiting. This would potentially expose the previous contents of the tags to user space, and cause any updates that user space makes before the restoring/zeroing has completed to potentially be lost.

Since this code is run from atomic contexts we can't just lock the page during the process. Instead implement a new (global) spinlock to protect the mte_sync_page_tags() function.

Fixes 34bfeea4a9e9 ("arm64: mte: Clear the tags when a page is mapped in user-space with PROT_MTE")

---

---
 arch/arm64/kernel/mte.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
arch/arm64/kernel/mte.c +18 -3
index 125a10e413e9..c88e778c2fa9 100644
@@ -25,6 +25,7 @@
 u64 gcr_kernel_excl __ro_after_init;
 
 static bool report_fault_once = true;
+static spinlock_t tag_sync_lock;
 
 #ifdef CONFIG_KASAN_HW_TAGS
 /* Whether the MTE asynchronous mode is enabled. */
@@ -34,13 +35,22 @@ EXPORT_SYMBOL_GPL(mte_async_mode);
 
 static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
 {
+	unsigned long flags;
 	pte_t old_pte = READ_ONCE(*ptep);
 
+	spin_lock_irqsave(&tag_sync_lock, flags);
+
+	/* Recheck with the lock held */
+	if (test_bit(PG_mte_tagged, &page->flags))
+		goto out;
+
 	if (check_swap && is_swap_pte(old_pte)) {
 		swp_entry_t entry = pte_to_swp_entry(old_pte);
 
-		if (!non_swap_entry(entry) && mte_restore_tags(entry, page))
-			return;
+		if (!non_swap_entry(entry) && mte_restore_tags(entry, page)) {
+			set_bit(PG_mte_tagged, &page->flags);
+			goto out;
+		}
 	}
 
 	page_kasan_tag_reset(page);
@@ -53,6 +63,10 @@ static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
 	 */
 	smp_wmb();
 	mte_clear_page_tags(page_address(page));
+	set_bit(PG_mte_tagged, &page->flags);
+
+out:
+	spin_unlock_irqrestore(&tag_sync_lock, flags);
 }
 
 void mte_sync_tags(pte_t *ptep, pte_t pte)
@@ -60,10 +74,11 @@ void mte_sync_tags(pte_t *ptep, pte_t pte)
 	struct page *page = pte_page(pte);
 	long i, nr_pages = compound_nr(page);
 	bool check_swap = nr_pages == 1;
+	bool pte_is_tagged = pte_tagged(pte);
 
 	/* if PG_mte_tagged is set, tags have already been initialised */
 	for (i = 0; i < nr_pages; i++, page++) {
-		if (!test_and_set_bit(PG_mte_tagged, &page->flags))
+		if (!test_bit(PG_mte_tagged, &page->flags))
 			mte_sync_page_tags(page, ptep, check_swap);
 	}
 }
Signature · 1 line
2.20.1
maintainer
#2 lore raw

Re: [PATCH v12 1/8] arm64: mte: Handle race when synchronising tags

Hi Steven,

Quoted 26 lines · Steven Price

mte_sync_tags() used test_and_set_bit() to set the PG_mte_tagged flag before restoring/zeroing the MTE tags. However if another thread were to race and attempt to sync the tags on the same page before the first thread had completed restoring/zeroing then it would see the flag is already set and continue without waiting. This would potentially expose the previous contents of the tags to user space, and cause any updates that user space makes before the restoring/zeroing has completed to potentially be lost.

Since this code is run from atomic contexts we can't just lock the page during the process. Instead implement a new (global) spinlock to protect the mte_sync_page_tags() function.

Fixes 34bfeea4a9e9 ("arm64: mte: Clear the tags when a page is mapped in user-space with PROT_MTE")

---

---
 arch/arm64/kernel/mte.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
arch/arm64/kernel/mte.c +0 -0
index 125a10e413e9..c88e778c2fa9 100644

arch/arm64/kernel/mte.c line 28

Quoted 4 lines
arch/arm64/kernel/mte.c +1 -0
@@ -25,6 +25,7 @@
 u64 gcr_kernel_excl __ro_after_init;
 
 static bool report_fault_once = true;
+static spinlock_t tag_sync_lock;

What initialises this spinlock? Have you tried this with lockdep? I'd expect it to be defined with DEFINE_SPINLOCK(), which always does the right thing.

Quoted 42 lines
arch/arm64/kernel/mte.c +15 -2
 #ifdef CONFIG_KASAN_HW_TAGS
 /* Whether the MTE asynchronous mode is enabled. */
@@ -34,13 +35,22 @@ EXPORT_SYMBOL_GPL(mte_async_mode);
 
 static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
 {
+	unsigned long flags;
 	pte_t old_pte = READ_ONCE(*ptep);
 
+	spin_lock_irqsave(&tag_sync_lock, flags);
+
+	/* Recheck with the lock held */
+	if (test_bit(PG_mte_tagged, &page->flags))
+		goto out;
+
 	if (check_swap && is_swap_pte(old_pte)) {
 		swp_entry_t entry = pte_to_swp_entry(old_pte);
 
-		if (!non_swap_entry(entry) && mte_restore_tags(entry, page))
-			return;
+		if (!non_swap_entry(entry) && mte_restore_tags(entry, page)) {
+			set_bit(PG_mte_tagged, &page->flags);
+			goto out;
+		}
 	}
 
 	page_kasan_tag_reset(page);
@@ -53,6 +63,10 @@ static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
 	 */
 	smp_wmb();
 	mte_clear_page_tags(page_address(page));
+	set_bit(PG_mte_tagged, &page->flags);
+
+out:
+	spin_unlock_irqrestore(&tag_sync_lock, flags);
 }
 
 void mte_sync_tags(pte_t *ptep, pte_t pte)
@@ -60,10 +74,11 @@ void mte_sync_tags(pte_t *ptep, pte_t pte)
 	struct page *page = pte_page(pte);
 	long i, nr_pages = compound_nr(page);

arch/arm64/kernel/mte.c void mte_sync_tags(pte_t *ptep, pte_t pte) · line 84

Quoted 10 lines
arch/arm64/kernel/mte.c +2 -1
 	bool check_swap = nr_pages == 1;
+	bool pte_is_tagged = pte_tagged(pte);
 
 	/* if PG_mte_tagged is set, tags have already been initialised */
 	for (i = 0; i < nr_pages; i++, page++) {
-		if (!test_and_set_bit(PG_mte_tagged, &page->flags))
+		if (!test_bit(PG_mte_tagged, &page->flags))
 			mte_sync_page_tags(page, ptep, check_swap);
 	}
 }

Thanks,

	M.
Signature · 5 lines
Without deviation from the norm, progress is not possible.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#3 lore raw

Re: [PATCH v12 1/8] arm64: mte: Handle race when synchronising tags

Quoted 1 line · Marc Zyngier

Hi Steven,

Hi Marc,

Quoted 30 lines
Quoted 28 lines · Steven Price

mte_sync_tags() used test_and_set_bit() to set the PG_mte_tagged flag before restoring/zeroing the MTE tags. However if another thread were to race and attempt to sync the tags on the same page before the first thread had completed restoring/zeroing then it would see the flag is already set and continue without waiting. This would potentially expose the previous contents of the tags to user space, and cause any updates that user space makes before the restoring/zeroing has completed to potentially be lost.

Since this code is run from atomic contexts we can't just lock the page during the process. Instead implement a new (global) spinlock to protect the mte_sync_page_tags() function.

Fixes 34bfeea4a9e9 ("arm64: mte: Clear the tags when a page is mapped in user-space with PROT_MTE")

---

---
 arch/arm64/kernel/mte.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
arch/arm64/kernel/mte.c +0 -0
index 125a10e413e9..c88e778c2fa9 100644
@@ -25,6 +25,7 @@
 u64 gcr_kernel_excl __ro_after_init;
 
Quoted 6 lines

arch/arm64/kernel/mte.c line 28

Quoted 2 lines
arch/arm64/kernel/mte.c +1 -0
 static bool report_fault_once = true;
+static spinlock_t tag_sync_lock;

What initialises this spinlock? Have you tried this with lockdep? I'd expect it to be defined with DEFINE_SPINLOCK(), which always does the right thing.

You of course are absolute right, and this will blow up with lockdep. Sorry about that. DEFINE_SPINLOCK() solves the problem.

Thanks,

Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer Reviewed-by on v12 1/8
#4 lore raw

Re: [PATCH v12 1/8] arm64: mte: Handle race when synchronising tags

In reply to Steven Price
Quoted 9 lines · Steven Price

mte_sync_tags() used test_and_set_bit() to set the PG_mte_tagged flag before restoring/zeroing the MTE tags. However if another thread were to race and attempt to sync the tags on the same page before the first thread had completed restoring/zeroing then it would see the flag is already set and continue without waiting. This would potentially expose the previous contents of the tags to user space, and cause any updates that user space makes before the restoring/zeroing has completed to potentially be lost.

Quoted 6 lines

Since this code is run from atomic contexts we can't just lock the page during the process. Instead implement a new (global) spinlock to protect the mte_sync_page_tags() function.

Fixes 34bfeea4a9e9 ("arm64: mte: Clear the tags when a page is mapped in user-space with PROT_MTE")

Other than the missing spinlock initialisation, the patch looks fine to me.

(though I'll probably queue it as a fix, waiting a couple of days for comments)

Signature · 5 lines
Catalin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#5 lore raw

PATCH v12 2/8 arm64: Handle MTE tags zeroing in __alloc_zeroed_user_highpage()

In reply to Steven Price

From: Catalin Marinas <[email protected]>

Currently, on an anonymous page fault, the kernel allocates a zeroed page and maps it in user space. If the mapping is tagged (PROT_MTE), set_pte_at() additionally clears the tags under a spinlock to avoid a race on the page->flags. In order to optimise the lock, clear the page tags on allocation in __alloc_zeroed_user_highpage() if the vma flags have VM_MTE set.

---
 arch/arm64/include/asm/page.h |  6 ++++--
 arch/arm64/mm/fault.c         | 21 +++++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)
arch/arm64/include/asm/page.h +4 -2
index 012cffc574e8..97853570d0f1 100644
@@ -13,6 +13,7 @@
 #ifndef __ASSEMBLY__
 
 #include <linux/personality.h> /* for READ_IMPLIES_EXEC */
+#include <linux/types.h>
 #include <asm/pgtable-types.h>
 
 struct page;
@@ -28,8 +29,9 @@ void copy_user_highpage(struct page *to, struct page *from,
 void copy_highpage(struct page *to, struct page *from);
 #define __HAVE_ARCH_COPY_HIGHPAGE
 
-#define __alloc_zeroed_user_highpage(movableflags, vma, vaddr) \
-	alloc_page_vma(GFP_HIGHUSER | __GFP_ZERO | movableflags, vma, vaddr)
+struct page *__alloc_zeroed_user_highpage(gfp_t movableflags,
+					  struct vm_area_struct *vma,
+					  unsigned long vaddr);
 #define __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE
 
 #define clear_user_page(page, vaddr, pg)	clear_page(page)
arch/arm64/mm/fault.c +21 -0
index 871c82ab0a30..5a03428e97f3 100644
@@ -921,3 +921,24 @@ void do_debug_exception(unsigned long addr_if_watchpoint, unsigned int esr,
 	debug_exception_exit(regs);
 }
 NOKPROBE_SYMBOL(do_debug_exception);
+
+/*
+ * Used during anonymous page fault handling.
+ */
+struct page *__alloc_zeroed_user_highpage(gfp_t movableflags,
+					  struct vm_area_struct *vma,
+					  unsigned long vaddr)
+{
+	struct page *page;
+	bool tagged = system_supports_mte() && (vma->vm_flags & VM_MTE);
+
+	page = alloc_page_vma(GFP_HIGHUSER | __GFP_ZERO | movableflags, vma,
+			      vaddr);
+	if (tagged && page) {
+		mte_clear_page_tags(page_address(page));
+		page_kasan_tag_reset(page);
+		set_bit(PG_mte_tagged, &page->flags);
+	}
+
+	return page;
+}
Signature · 1 line
2.20.1
#6 lore raw

PATCH v12 3/8 arm64: mte: Sync tags for pages where PTE is untagged

In reply to Steven Price

A KVM guest could store tags in a page even if the VMM hasn't mapped the page with PROT_MTE. So when restoring pages from swap we will need to check to see if there are any saved tags even if !pte_tagged().

However don't check pages for which pte_access_permitted() returns false as these will not have been swapped out.

---
 arch/arm64/include/asm/pgtable.h |  9 +++++++--
 arch/arm64/kernel/mte.c          | 16 ++++++++++++++--
 2 files changed, 21 insertions(+), 4 deletions(-)
arch/arm64/include/asm/pgtable.h +7 -2
index 0b10204e72fc..275178a810c1 100644
@@ -314,8 +314,13 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
 	if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte))
 		__sync_icache_dcache(pte);
 
-	if (system_supports_mte() &&
-	    pte_present(pte) && pte_tagged(pte) && !pte_special(pte))
+	/*
+	 * If the PTE would provide user space access to the tags associated
+	 * with it then ensure that the MTE tags are synchronised.  Exec-only
+	 * mappings don't expose tags (instruction fetches don't check tags).
+	 */
+	if (system_supports_mte() && pte_present(pte) &&
+	    pte_access_permitted(pte, false) && !pte_special(pte))
 		mte_sync_tags(ptep, pte);
 
 	__check_racy_pte_update(mm, ptep, pte);
arch/arm64/kernel/mte.c +14 -2
index c88e778c2fa9..a604818c52c1 100644
@@ -33,11 +33,15 @@ DEFINE_STATIC_KEY_FALSE(mte_async_mode);
 EXPORT_SYMBOL_GPL(mte_async_mode);
 #endif
 
-static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
+static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap,
+			       bool pte_is_tagged)
 {
 	unsigned long flags;
 	pte_t old_pte = READ_ONCE(*ptep);
 
+	if (!is_swap_pte(old_pte) && !pte_is_tagged)
+		return;
+
 	spin_lock_irqsave(&tag_sync_lock, flags);
 
 	/* Recheck with the lock held */
@@ -53,6 +57,9 @@ static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
 		}
 	}
 
+	if (!pte_is_tagged)
+		goto out;
+
 	page_kasan_tag_reset(page);
 	/*
 	 * We need smp_wmb() in between setting the flags and clearing the
@@ -76,10 +83,15 @@ void mte_sync_tags(pte_t *ptep, pte_t pte)
 	bool check_swap = nr_pages == 1;
 	bool pte_is_tagged = pte_tagged(pte);
 
+	/* Early out if there's nothing to do */
+	if (!check_swap && !pte_is_tagged)
+		return;
+
 	/* if PG_mte_tagged is set, tags have already been initialised */
 	for (i = 0; i < nr_pages; i++, page++) {
 		if (!test_bit(PG_mte_tagged, &page->flags))
-			mte_sync_page_tags(page, ptep, check_swap);
+			mte_sync_page_tags(page, ptep, check_swap,
+					   pte_is_tagged);
 	}
 }
 
Signature · 1 line
2.20.1
maintainer
#7 lore raw

Re: [PATCH v12 3/8] arm64: mte: Sync tags for pages where PTE is untagged

Quoted 19 lines · Steven Price

A KVM guest could store tags in a page even if the VMM hasn't mapped the page with PROT_MTE. So when restoring pages from swap we will need to check to see if there are any saved tags even if !pte_tagged().

However don't check pages for which pte_access_permitted() returns false as these will not have been swapped out.

---
 arch/arm64/include/asm/pgtable.h |  9 +++++++--
 arch/arm64/kernel/mte.c          | 16 ++++++++++++++--
 2 files changed, 21 insertions(+), 4 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -0
index 0b10204e72fc..275178a810c1 100644

arch/arm64/include/asm/pgtable.h static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, · line 320

Quoted 9 lines
arch/arm64/include/asm/pgtable.h +4 -2
@@ -314,8 +314,13 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
 	if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte))
 		__sync_icache_dcache(pte);
 
-	if (system_supports_mte() &&
-	    pte_present(pte) && pte_tagged(pte) && !pte_special(pte))
+	/*
+	 * If the PTE would provide user space access to the tags associated
+	 * with it then ensure that the MTE tags are synchronised.  Exec-only
+	 * mappings don't expose tags (instruction fetches don't check tags).

I'm not sure I understand this comment. Of course, execution doesn't match tags. But the memory could still have tags associated with it. Does this mean such a page would lose its tags is swapped out?

Thanks,

	M.
Quoted 58 lines
arch/arm64/include/asm/pgtable.h +3 -0
+	 */
+	if (system_supports_mte() && pte_present(pte) &&
+	    pte_access_permitted(pte, false) && !pte_special(pte))
 		mte_sync_tags(ptep, pte);
 
 	__check_racy_pte_update(mm, ptep, pte);
arch/arm64/kernel/mte.c +14 -2
index c88e778c2fa9..a604818c52c1 100644
@@ -33,11 +33,15 @@ DEFINE_STATIC_KEY_FALSE(mte_async_mode);
 EXPORT_SYMBOL_GPL(mte_async_mode);
 #endif
 
-static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
+static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap,
+			       bool pte_is_tagged)
 {
 	unsigned long flags;
 	pte_t old_pte = READ_ONCE(*ptep);
 
+	if (!is_swap_pte(old_pte) && !pte_is_tagged)
+		return;
+
 	spin_lock_irqsave(&tag_sync_lock, flags);
 
 	/* Recheck with the lock held */
@@ -53,6 +57,9 @@ static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
 		}
 	}
 
+	if (!pte_is_tagged)
+		goto out;
+
 	page_kasan_tag_reset(page);
 	/*
 	 * We need smp_wmb() in between setting the flags and clearing the
@@ -76,10 +83,15 @@ void mte_sync_tags(pte_t *ptep, pte_t pte)
 	bool check_swap = nr_pages == 1;
 	bool pte_is_tagged = pte_tagged(pte);
 
+	/* Early out if there's nothing to do */
+	if (!check_swap && !pte_is_tagged)
+		return;
+
 	/* if PG_mte_tagged is set, tags have already been initialised */
 	for (i = 0; i < nr_pages; i++, page++) {
 		if (!test_bit(PG_mte_tagged, &page->flags))
-			mte_sync_page_tags(page, ptep, check_swap);
+			mte_sync_page_tags(page, ptep, check_swap,
+					   pte_is_tagged);
 	}
 }
 
Signature · 1 line
2.20.1
Signature · 5 lines
Without deviation from the norm, progress is not possible.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#8 lore raw

Re: [PATCH v12 3/8] arm64: mte: Sync tags for pages where PTE is untagged

Quoted 24 lines · Marc Zyngier
Quoted 22 lines · Steven Price

A KVM guest could store tags in a page even if the VMM hasn't mapped the page with PROT_MTE. So when restoring pages from swap we will need to check to see if there are any saved tags even if !pte_tagged().

However don't check pages for which pte_access_permitted() returns false as these will not have been swapped out.

---
 arch/arm64/include/asm/pgtable.h |  9 +++++++--
 arch/arm64/kernel/mte.c          | 16 ++++++++++++++--
 2 files changed, 21 insertions(+), 4 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -0
index 0b10204e72fc..275178a810c1 100644
@@ -314,8 +314,13 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
 	if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte))
 		__sync_icache_dcache(pte);
 
Quoted 10 lines

arch/arm64/include/asm/pgtable.h static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, · line 320

Quoted 6 lines
arch/arm64/include/asm/pgtable.h +4 -2
-	if (system_supports_mte() &&
-	    pte_present(pte) && pte_tagged(pte) && !pte_special(pte))
+	/*
+	 * If the PTE would provide user space access to the tags associated
+	 * with it then ensure that the MTE tags are synchronised.  Exec-only
+	 * mappings don't expose tags (instruction fetches don't check tags).

I'm not sure I understand this comment. Of course, execution doesn't match tags. But the memory could still have tags associated with it. Does this mean such a page would lose its tags is swapped out?

Hmm, I probably should have reread that - the context of the comment is lost.

I added the comment when changing to pte_access_permitted(), and the comment on pte_access_permitted() explains a potential gotcha:

* p??_access_permitted() is true for valid user mappings (PTE_USER
* bit set, subject to the write permission check). For execute-only
* mappings, like PROT_EXEC with EPAN (both PTE_USER and PTE_UXN bits
* not set) must return false. PROT_NONE mappings do not have the
* PTE_VALID bit set.

So execute-only mappings return false even though that is effectively a type of user access. However, because MTE checks are not performed by the PE for instruction fetches this doesn't matter. I'll update the comment, how about:

/*
* If the PTE would provide user space access to the tags associated
* with it then ensure that the MTE tags are synchronised. Although
* pte_access_permitted() returns false for exec only mappings, they
* don't expose tags (instruction fetches don't check tags). */

Thanks,

Steve

Quoted 63 lines

Thanks,

	M.
Quoted 58 lines
arch/arm64/include/asm/pgtable.h +3 -0
+	 */
+	if (system_supports_mte() && pte_present(pte) &&
+	    pte_access_permitted(pte, false) && !pte_special(pte))
 		mte_sync_tags(ptep, pte);
 
 	__check_racy_pte_update(mm, ptep, pte);
arch/arm64/kernel/mte.c +14 -2
index c88e778c2fa9..a604818c52c1 100644
@@ -33,11 +33,15 @@ DEFINE_STATIC_KEY_FALSE(mte_async_mode);
 EXPORT_SYMBOL_GPL(mte_async_mode);
 #endif
 
-static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
+static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap,
+			       bool pte_is_tagged)
 {
 	unsigned long flags;
 	pte_t old_pte = READ_ONCE(*ptep);
 
+	if (!is_swap_pte(old_pte) && !pte_is_tagged)
+		return;
+
 	spin_lock_irqsave(&tag_sync_lock, flags);
 
 	/* Recheck with the lock held */
@@ -53,6 +57,9 @@ static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
 		}
 	}
 
+	if (!pte_is_tagged)
+		goto out;
+
 	page_kasan_tag_reset(page);
 	/*
 	 * We need smp_wmb() in between setting the flags and clearing the
@@ -76,10 +83,15 @@ void mte_sync_tags(pte_t *ptep, pte_t pte)
 	bool check_swap = nr_pages == 1;
 	bool pte_is_tagged = pte_tagged(pte);
 
+	/* Early out if there's nothing to do */
+	if (!check_swap && !pte_is_tagged)
+		return;
+
 	/* if PG_mte_tagged is set, tags have already been initialised */
 	for (i = 0; i < nr_pages; i++, page++) {
 		if (!test_bit(PG_mte_tagged, &page->flags))
-			mte_sync_page_tags(page, ptep, check_swap);
+			mte_sync_page_tags(page, ptep, check_swap,
+					   pte_is_tagged);
 	}
 }
 
Signature · 1 line
2.20.1
maintainer
#9 lore raw

Re: [PATCH v12 3/8] arm64: mte: Sync tags for pages where PTE is untagged

Quoted 53 lines · Steven Price
Quoted 24 lines · Marc Zyngier
Quoted 22 lines · Steven Price

A KVM guest could store tags in a page even if the VMM hasn't mapped the page with PROT_MTE. So when restoring pages from swap we will need to check to see if there are any saved tags even if !pte_tagged().

However don't check pages for which pte_access_permitted() returns false as these will not have been swapped out.

---
 arch/arm64/include/asm/pgtable.h |  9 +++++++--
 arch/arm64/kernel/mte.c          | 16 ++++++++++++++--
 2 files changed, 21 insertions(+), 4 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -0
index 0b10204e72fc..275178a810c1 100644
@@ -314,8 +314,13 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
 	if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte))
 		__sync_icache_dcache(pte);
 
Quoted 10 lines

arch/arm64/include/asm/pgtable.h static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, · line 320

Quoted 6 lines
arch/arm64/include/asm/pgtable.h +4 -2
-	if (system_supports_mte() &&
-	    pte_present(pte) && pte_tagged(pte) && !pte_special(pte))
+	/*
+	 * If the PTE would provide user space access to the tags associated
+	 * with it then ensure that the MTE tags are synchronised.  Exec-only
+	 * mappings don't expose tags (instruction fetches don't check tags).

I'm not sure I understand this comment. Of course, execution doesn't match tags. But the memory could still have tags associated with it. Does this mean such a page would lose its tags is swapped out?

Hmm, I probably should have reread that - the context of the comment is lost.

I added the comment when changing to pte_access_permitted(), and the comment on pte_access_permitted() explains a potential gotcha:

* p??_access_permitted() is true for valid user mappings (PTE_USER
* bit set, subject to the write permission check). For execute-only
* mappings, like PROT_EXEC with EPAN (both PTE_USER and PTE_UXN bits
* not set) must return false. PROT_NONE mappings do not have the
* PTE_VALID bit set.

So execute-only mappings return false even though that is effectively a type of user access. However, because MTE checks are not performed by the PE for instruction fetches this doesn't matter. I'll update the comment, how about:

Quoted 6 lines

/*
* If the PTE would provide user space access to the tags associated
* with it then ensure that the MTE tags are synchronised. Although
* pte_access_permitted() returns false for exec only mappings, they
* don't expose tags (instruction fetches don't check tags). */

This looks fine to me. We basically want to check the PTE_VALID and PTE_USER bits and pte_access_permitted() does this (we could come up with a new macro name like pte_valid_user() but since we don't care about execute-only, it gets unnecessarily complicated).

Signature · 1 line
Catalin
maintainer
#10 lore raw

Re: [PATCH v12 3/8] arm64: mte: Sync tags for pages where PTE is untagged

In reply to Steven Price
Quoted 21 lines · Steven Price

A KVM guest could store tags in a page even if the VMM hasn't mapped the page with PROT_MTE. So when restoring pages from swap we will need to check to see if there are any saved tags even if !pte_tagged().

However don't check pages for which pte_access_permitted() returns false as these will not have been swapped out.

---
 arch/arm64/include/asm/pgtable.h |  9 +++++++--
 arch/arm64/kernel/mte.c          | 16 ++++++++++++++--
 2 files changed, 21 insertions(+), 4 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -0
index 0b10204e72fc..275178a810c1 100644
@@ -314,8 +314,13 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
 	if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte))
 		__sync_icache_dcache(pte);
 

arch/arm64/include/asm/pgtable.h static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, · line 324

Quoted 10 lines
arch/arm64/include/asm/pgtable.h +7 -2
-	if (system_supports_mte() &&
-	    pte_present(pte) && pte_tagged(pte) && !pte_special(pte))
+	/*
+	 * If the PTE would provide user space access to the tags associated
+	 * with it then ensure that the MTE tags are synchronised.  Exec-only
+	 * mappings don't expose tags (instruction fetches don't check tags).
+	 */
+	if (system_supports_mte() && pte_present(pte) &&
+	    pte_access_permitted(pte, false) && !pte_special(pte))
 		mte_sync_tags(ptep, pte);

Looking at the mte_sync_page_tags() logic, we bail out early if it's the old pte is not a swap one and the new pte is not tagged. So we only need to call mte_sync_tags() if it's a tagged new pte or the old one is swap. What about changing the set_pte_at() test to:

	if (system_supports_mte() && pte_present(pte) && !pte_special(pte) &&
	    (pte_tagged(pte) || is_swap_pte(READ_ONCE(*ptep))))
		mte_sync_tags(ptep, pte);

We can even change mte_sync_tags() to take the old pte directly:

	if (system_supports_mte() && pte_present(pte) && !pte_special(pte)) {
		pte_t old_pte = READ_ONCE(*ptep);
		if (pte_tagged(pte) || is_swap_pte(old_pte))
			mte_sync_tags(old_pte, pte);
	}

It would save a function call in most cases where the page is not tagged.

Signature · 1 line
Catalin
#11 lore raw

Re: [PATCH v12 3/8] arm64: mte: Sync tags for pages where PTE is untagged

Quoted 44 lines · Catalin Marinas
Quoted 21 lines · Steven Price

A KVM guest could store tags in a page even if the VMM hasn't mapped the page with PROT_MTE. So when restoring pages from swap we will need to check to see if there are any saved tags even if !pte_tagged().

However don't check pages for which pte_access_permitted() returns false as these will not have been swapped out.

---
 arch/arm64/include/asm/pgtable.h |  9 +++++++--
 arch/arm64/kernel/mte.c          | 16 ++++++++++++++--
 2 files changed, 21 insertions(+), 4 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -0
index 0b10204e72fc..275178a810c1 100644
@@ -314,8 +314,13 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
 	if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte))
 		__sync_icache_dcache(pte);
 

arch/arm64/include/asm/pgtable.h static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, · line 324

Quoted 10 lines
arch/arm64/include/asm/pgtable.h +7 -2
-	if (system_supports_mte() &&
-	    pte_present(pte) && pte_tagged(pte) && !pte_special(pte))
+	/*
+	 * If the PTE would provide user space access to the tags associated
+	 * with it then ensure that the MTE tags are synchronised.  Exec-only
+	 * mappings don't expose tags (instruction fetches don't check tags).
+	 */
+	if (system_supports_mte() && pte_present(pte) &&
+	    pte_access_permitted(pte, false) && !pte_special(pte))
 		mte_sync_tags(ptep, pte);

Looking at the mte_sync_page_tags() logic, we bail out early if it's the old pte is not a swap one and the new pte is not tagged. So we only need to call mte_sync_tags() if it's a tagged new pte or the old one is swap. What about changing the set_pte_at() test to:

	if (system_supports_mte() && pte_present(pte) && !pte_special(pte) &&
	    (pte_tagged(pte) || is_swap_pte(READ_ONCE(*ptep))))
		mte_sync_tags(ptep, pte);

We can even change mte_sync_tags() to take the old pte directly:

Quoted 9 lines
	if (system_supports_mte() && pte_present(pte) && !pte_special(pte)) {
		pte_t old_pte = READ_ONCE(*ptep);
		if (pte_tagged(pte) || is_swap_pte(old_pte))
			mte_sync_tags(old_pte, pte);
	}

It would save a function call in most cases where the page is not tagged.

Yes that looks like a good optimisation - although you've missed the pte_access_permitted() part of the check ;) The problem I hit is one of include dependencies:

is_swap_pte() is defined (as a static inline) in include/linux/swapops.h. However the definition depends on pte_none()/pte_present() which are defined in pgtable.h - so there's a circular dependency.

Open coding is_swap_pte() in set_pte_at() works, but it's a bit ugly. Any ideas on how to improve on the below?

	if (system_supports_mte() && pte_present(pte) &&
	    pte_access_permitted(pte, false) && !pte_special(pte)) {
		pte_t old_pte = READ_ONCE(*ptep);
		/*
		 * We only need to synchronise if the new PTE has tags enabled
		 * or if swapping in (in which case another mapping may have
		 * set tags in the past even if this PTE isn't tagged).
		 * (!pte_none() && !pte_present()) is an open coded version of
		 * is_swap_pte()
		 */
		if (pte_tagged(pte) || (!pte_none(pte) && !pte_present(pte)))
			mte_sync_tags(old_pte, pte);
	}

Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer
#12 lore raw

Re: [PATCH v12 3/8] arm64: mte: Sync tags for pages where PTE is untagged

Quoted 51 lines · Steven Price
Quoted 50 lines · Catalin Marinas
Quoted 21 lines · Steven Price

A KVM guest could store tags in a page even if the VMM hasn't mapped the page with PROT_MTE. So when restoring pages from swap we will need to check to see if there are any saved tags even if !pte_tagged().

However don't check pages for which pte_access_permitted() returns false as these will not have been swapped out.

---
 arch/arm64/include/asm/pgtable.h |  9 +++++++--
 arch/arm64/kernel/mte.c          | 16 ++++++++++++++--
 2 files changed, 21 insertions(+), 4 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -0
index 0b10204e72fc..275178a810c1 100644
@@ -314,8 +314,13 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
 	if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte))
 		__sync_icache_dcache(pte);
 

arch/arm64/include/asm/pgtable.h static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, · line 324

Quoted 10 lines
arch/arm64/include/asm/pgtable.h +7 -2
-	if (system_supports_mte() &&
-	    pte_present(pte) && pte_tagged(pte) && !pte_special(pte))
+	/*
+	 * If the PTE would provide user space access to the tags associated
+	 * with it then ensure that the MTE tags are synchronised.  Exec-only
+	 * mappings don't expose tags (instruction fetches don't check tags).
+	 */
+	if (system_supports_mte() && pte_present(pte) &&
+	    pte_access_permitted(pte, false) && !pte_special(pte))
 		mte_sync_tags(ptep, pte);

Looking at the mte_sync_page_tags() logic, we bail out early if it's the old pte is not a swap one and the new pte is not tagged. So we only need to call mte_sync_tags() if it's a tagged new pte or the old one is swap. What about changing the set_pte_at() test to:

	if (system_supports_mte() && pte_present(pte) && !pte_special(pte) &&
	    (pte_tagged(pte) || is_swap_pte(READ_ONCE(*ptep))))
		mte_sync_tags(ptep, pte);

We can even change mte_sync_tags() to take the old pte directly:

	if (system_supports_mte() && pte_present(pte) && !pte_special(pte)) {
		pte_t old_pte = READ_ONCE(*ptep);
		if (pte_tagged(pte) || is_swap_pte(old_pte))
			mte_sync_tags(old_pte, pte);
	}
Quoted 5 lines
Quoted 2 lines

It would save a function call in most cases where the page is not tagged.

Yes that looks like a good optimisation - although you've missed the pte_access_permitted() part of the check ;)

I was actually wondering if we could remove it. I don't think it buys us much as we have a pte_present() check already, so we know it is pointing to a valid page. Currently we'd only get a tagged pte on user mappings, same with swap entries.

When vmalloc kasan_hw will be added, I think we have a set_pte_at() with a tagged pte but init_mm and high address (we might as well add a warning if addr > TASK_SIZE_64 on the mte_sync_tags path so that we don't forget).

Quoted 10 lines

The problem I hit is one of include dependencies:

is_swap_pte() is defined (as a static inline) in include/linux/swapops.h. However the definition depends on pte_none()/pte_present() which are defined in pgtable.h - so there's a circular dependency.

Open coding is_swap_pte() in set_pte_at() works, but it's a bit ugly. Any ideas on how to improve on the below?

Quoted 13 lines
	if (system_supports_mte() && pte_present(pte) &&
	    pte_access_permitted(pte, false) && !pte_special(pte)) {
		pte_t old_pte = READ_ONCE(*ptep);
		/*
		 * We only need to synchronise if the new PTE has tags enabled
		 * or if swapping in (in which case another mapping may have
		 * set tags in the past even if this PTE isn't tagged).
		 * (!pte_none() && !pte_present()) is an open coded version of
		 * is_swap_pte()
		 */
		if (pte_tagged(pte) || (!pte_none(pte) && !pte_present(pte)))
			mte_sync_tags(old_pte, pte);
	}

That's why I avoided testing my suggestion ;). I think we should just add !pte_none() in there with a comment that it may be a swap pte and use the is_swap_pte() again on the mte_sync_tags() path. We already have the pte_present() check.

Signature · 5 lines
Catalin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
maintainer
#13 lore raw

Re: [PATCH v12 3/8] arm64: mte: Sync tags for pages where PTE is untagged

Quoted 25 lines · Catalin Marinas
Quoted 10 lines · Steven Price

The problem I hit is one of include dependencies:

is_swap_pte() is defined (as a static inline) in include/linux/swapops.h. However the definition depends on pte_none()/pte_present() which are defined in pgtable.h - so there's a circular dependency.

Open coding is_swap_pte() in set_pte_at() works, but it's a bit ugly. Any ideas on how to improve on the below?

Quoted 13 lines
	if (system_supports_mte() && pte_present(pte) &&
	    pte_access_permitted(pte, false) && !pte_special(pte)) {
		pte_t old_pte = READ_ONCE(*ptep);
		/*
		 * We only need to synchronise if the new PTE has tags enabled
		 * or if swapping in (in which case another mapping may have
		 * set tags in the past even if this PTE isn't tagged).
		 * (!pte_none() && !pte_present()) is an open coded version of
		 * is_swap_pte()
		 */
		if (pte_tagged(pte) || (!pte_none(pte) && !pte_present(pte)))
			mte_sync_tags(old_pte, pte);
	}
Quoted 4 lines

That's why I avoided testing my suggestion ;). I think we should just add !pte_none() in there with a comment that it may be a swap pte and use the is_swap_pte() again on the mte_sync_tags() path. We already have the pte_present() check.

Correction - pte_present() checks the new pte only, we need another for the old pte. So it looks like we'll open-code is_swap_pte().

Signature · 5 lines
Catalin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#14 lore raw

Re: [PATCH v12 3/8] arm64: mte: Sync tags for pages where PTE is untagged

In reply to Catalin Marinas
Quoted 55 lines · Catalin Marinas
Quoted 54 lines · Steven Price
Quoted 52 lines · Catalin Marinas
Quoted 21 lines · Steven Price

A KVM guest could store tags in a page even if the VMM hasn't mapped the page with PROT_MTE. So when restoring pages from swap we will need to check to see if there are any saved tags even if !pte_tagged().

However don't check pages for which pte_access_permitted() returns false as these will not have been swapped out.

---
 arch/arm64/include/asm/pgtable.h |  9 +++++++--
 arch/arm64/kernel/mte.c          | 16 ++++++++++++++--
 2 files changed, 21 insertions(+), 4 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -0
index 0b10204e72fc..275178a810c1 100644
@@ -314,8 +314,13 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
 	if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte))
 		__sync_icache_dcache(pte);
 

arch/arm64/include/asm/pgtable.h static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, · line 324

Quoted 10 lines
arch/arm64/include/asm/pgtable.h +7 -2
-	if (system_supports_mte() &&
-	    pte_present(pte) && pte_tagged(pte) && !pte_special(pte))
+	/*
+	 * If the PTE would provide user space access to the tags associated
+	 * with it then ensure that the MTE tags are synchronised.  Exec-only
+	 * mappings don't expose tags (instruction fetches don't check tags).
+	 */
+	if (system_supports_mte() && pte_present(pte) &&
+	    pte_access_permitted(pte, false) && !pte_special(pte))
 		mte_sync_tags(ptep, pte);

Looking at the mte_sync_page_tags() logic, we bail out early if it's the old pte is not a swap one and the new pte is not tagged. So we only need to call mte_sync_tags() if it's a tagged new pte or the old one is swap. What about changing the set_pte_at() test to:

	if (system_supports_mte() && pte_present(pte) && !pte_special(pte) &&
	    (pte_tagged(pte) || is_swap_pte(READ_ONCE(*ptep))))
		mte_sync_tags(ptep, pte);

We can even change mte_sync_tags() to take the old pte directly:

	if (system_supports_mte() && pte_present(pte) && !pte_special(pte)) {
		pte_t old_pte = READ_ONCE(*ptep);
		if (pte_tagged(pte) || is_swap_pte(old_pte))
			mte_sync_tags(old_pte, pte);
	}

It would save a function call in most cases where the page is not tagged.

Quoted 7 lines
Quoted 2 lines

Yes that looks like a good optimisation - although you've missed the pte_access_permitted() part of the check ;)

I was actually wondering if we could remove it. I don't think it buys us much as we have a pte_present() check already, so we know it is pointing to a valid page. Currently we'd only get a tagged pte on user mappings, same with swap entries.

Actually the other way round makes more sense surely? pte_access_permitted() is true if both PTE_VALID & PTE_USER are set. pte_present() is true if *either* PTE_VALID or PTE_PROT_NONE are set. So the pte_present() is actually redundant.

Quoted 4 lines

When vmalloc kasan_hw will be added, I think we have a set_pte_at() with a tagged pte but init_mm and high address (we might as well add a warning if addr > TASK_SIZE_64 on the mte_sync_tags path so that we don't forget).

While we might not yet have tagged kernel pages - I'm not sure there's much point weakening the check to have to then check addr as well in the future.

Quoted 24 lines
Quoted 10 lines

The problem I hit is one of include dependencies:

is_swap_pte() is defined (as a static inline) in include/linux/swapops.h. However the definition depends on pte_none()/pte_present() which are defined in pgtable.h - so there's a circular dependency.

Open coding is_swap_pte() in set_pte_at() works, but it's a bit ugly. Any ideas on how to improve on the below?

Quoted 13 lines
	if (system_supports_mte() && pte_present(pte) &&
	    pte_access_permitted(pte, false) && !pte_special(pte)) {
		pte_t old_pte = READ_ONCE(*ptep);
		/*
		 * We only need to synchronise if the new PTE has tags enabled
		 * or if swapping in (in which case another mapping may have
		 * set tags in the past even if this PTE isn't tagged).
		 * (!pte_none() && !pte_present()) is an open coded version of
		 * is_swap_pte()
		 */
		if (pte_tagged(pte) || (!pte_none(pte) && !pte_present(pte)))
			mte_sync_tags(old_pte, pte);
	}
Quoted 4 lines

That's why I avoided testing my suggestion ;). I think we should just add !pte_none() in there with a comment that it may be a swap pte and use the is_swap_pte() again on the mte_sync_tags() path. We already have the pte_present() check.

Well of course I didn't test the above beyond building - and I've screwed up because the open coded is_swap_pte() should have been called on old_pte not pte!

So the pte_present() check above (which I've just removed...) is for the *new* PTE. So I think we need to keep both here.

Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#15 lore raw

PATCH v12 4/8 arm64: kvm: Introduce MTE VM feature

In reply to Steven Price

Add a new VM feature 'KVM_ARM_CAP_MTE' which enables memory tagging for a VM. This will expose the feature to the guest and automatically tag memory pages touched by the VM as PG_mte_tagged (and clear the tag storage) to ensure that the guest cannot see stale tags, and so that the tags are correctly saved/restored across swap.

Actually exposing the new capability to user space happens in a later patch.

---
 arch/arm64/include/asm/kvm_emulate.h |  3 +++
 arch/arm64/include/asm/kvm_host.h    |  3 +++
 arch/arm64/kvm/hyp/exception.c       |  3 ++-
 arch/arm64/kvm/mmu.c                 | 37 +++++++++++++++++++++++++++-
 arch/arm64/kvm/sys_regs.c            |  3 +++
 include/uapi/linux/kvm.h             |  1 +
 6 files changed, 48 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +3 -0
index f612c090f2e4..6bf776c2399c 100644
@@ -84,6 +84,9 @@ static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
 	if (cpus_have_const_cap(ARM64_MISMATCHED_CACHE_TYPE) ||
 	    vcpu_el1_is_32bit(vcpu))
 		vcpu->arch.hcr_el2 |= HCR_TID2;
+
+	if (kvm_has_mte(vcpu->kvm))
+		vcpu->arch.hcr_el2 |= HCR_ATA;
 }
 
 static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu)
arch/arm64/include/asm/kvm_host.h +3 -0
index 7cd7d5c8c4bc..afaa5333f0e4 100644
@@ -132,6 +132,8 @@ struct kvm_arch {
 
 	u8 pfr0_csv2;
 	u8 pfr0_csv3;
+	/* Memory Tagging Extension enabled for the guest */
+	bool mte_enabled;
 };
 
 struct kvm_vcpu_fault_info {
@@ -769,6 +771,7 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
 #define kvm_arm_vcpu_sve_finalized(vcpu) \
 	((vcpu)->arch.flags & KVM_ARM64_VCPU_SVE_FINALIZED)
 
+#define kvm_has_mte(kvm) (system_supports_mte() && (kvm)->arch.mte_enabled)
 #define kvm_vcpu_has_pmu(vcpu)					\
 	(test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features))
 
arch/arm64/kvm/hyp/exception.c +2 -1
index 73629094f903..56426565600c 100644
@@ -112,7 +112,8 @@ static void enter_exception64(struct kvm_vcpu *vcpu, unsigned long target_mode,
 	new |= (old & PSR_C_BIT);
 	new |= (old & PSR_V_BIT);
 
-	// TODO: TCO (if/when ARMv8.5-MemTag is exposed to guests)
+	if (kvm_has_mte(vcpu->kvm))
+		new |= PSR_TCO_BIT;
 
 	new |= (old & PSR_DIT_BIT);
 
arch/arm64/kvm/mmu.c +36 -1
index c5d1f3c87dbd..8660f6a03f51 100644
@@ -822,6 +822,31 @@ transparent_hugepage_adjust(struct kvm_memory_slot *memslot,
 	return PAGE_SIZE;
 }
 
+static int sanitise_mte_tags(struct kvm *kvm, unsigned long size,
+			     kvm_pfn_t pfn)
+{
+	if (kvm_has_mte(kvm)) {
+		/*
+		 * The page will be mapped in stage 2 as Normal Cacheable, so
+		 * the VM will be able to see the page's tags and therefore
+		 * they must be initialised first. If PG_mte_tagged is set,
+		 * tags have already been initialised.
+		 */
+		unsigned long i, nr_pages = size >> PAGE_SHIFT;
+		struct page *page = pfn_to_online_page(pfn);
+
+		if (!page)
+			return -EFAULT;
+
+		for (i = 0; i < nr_pages; i++, page++) {
+			if (!test_and_set_bit(PG_mte_tagged, &page->flags))
+				mte_clear_page_tags(page_address(page));
+		}
+	}
+
+	return 0;
+}
+
 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 			  struct kvm_memory_slot *memslot, unsigned long hva,
 			  unsigned long fault_status)
@@ -971,8 +996,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (writable)
 		prot |= KVM_PGTABLE_PROT_W;
 
-	if (fault_status != FSC_PERM && !device)
+	if (fault_status != FSC_PERM && !device) {
+		ret = sanitise_mte_tags(kvm, vma_pagesize, pfn);
+		if (ret)
+			goto out_unlock;
+
 		clean_dcache_guest_page(pfn, vma_pagesize);
+	}
 
 	if (exec_fault) {
 		prot |= KVM_PGTABLE_PROT_X;
@@ -1168,12 +1198,17 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
 {
 	kvm_pfn_t pfn = pte_pfn(range->pte);
+	int ret;
 
 	if (!kvm->arch.mmu.pgt)
 		return 0;
 
 	WARN_ON(range->end - range->start != 1);
 
+	ret = sanitise_mte_tags(kvm, PAGE_SIZE, pfn);
+	if (ret)
+		return ret;
+
 	/*
 	 * We've moved a page around, probably through CoW, so let's treat it
 	 * just like a translation fault and clean the cache to the PoC.
arch/arm64/kvm/sys_regs.c +3 -0
index 76ea2800c33e..24a844cb79ca 100644
@@ -1047,6 +1047,9 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu,
 		break;
 	case SYS_ID_AA64PFR1_EL1:
 		val &= ~FEATURE(ID_AA64PFR1_MTE);
+		if (kvm_has_mte(vcpu->kvm))
+			val |= FIELD_PREP(FEATURE(ID_AA64PFR1_MTE),
+					  ID_AA64PFR1_MTE);
 		break;
 	case SYS_ID_AA64ISAR1_EL1:
 		if (!vcpu_has_ptrauth(vcpu))
include/uapi/linux/kvm.h +1 -0
index 3fd9a7e9d90c..8c95ba0fadda 100644
@@ -1082,6 +1082,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_SGX_ATTRIBUTE 196
 #define KVM_CAP_VM_COPY_ENC_CONTEXT_FROM 197
 #define KVM_CAP_PTP_KVM 198
+#define KVM_CAP_ARM_MTE 199
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
Signature · 1 line
2.20.1
maintainer
#16 lore raw

Re: [PATCH v12 4/8] arm64: kvm: Introduce MTE VM feature

Quoted 9 lines · Steven Price

Add a new VM feature 'KVM_ARM_CAP_MTE' which enables memory tagging for a VM. This will expose the feature to the guest and automatically tag memory pages touched by the VM as PG_mte_tagged (and clear the tag storage) to ensure that the guest cannot see stale tags, and so that the tags are correctly saved/restored across swap.

Actually exposing the new capability to user space happens in a later patch.

uber nit in $SUBJECT: "KVM: arm64:" is the preferred prefix (just like patches 7 and 8).

Quoted 65 lines
---
 arch/arm64/include/asm/kvm_emulate.h |  3 +++
 arch/arm64/include/asm/kvm_host.h    |  3 +++
 arch/arm64/kvm/hyp/exception.c       |  3 ++-
 arch/arm64/kvm/mmu.c                 | 37 +++++++++++++++++++++++++++-
 arch/arm64/kvm/sys_regs.c            |  3 +++
 include/uapi/linux/kvm.h             |  1 +
 6 files changed, 48 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +3 -0
index f612c090f2e4..6bf776c2399c 100644
@@ -84,6 +84,9 @@ static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
 	if (cpus_have_const_cap(ARM64_MISMATCHED_CACHE_TYPE) ||
 	    vcpu_el1_is_32bit(vcpu))
 		vcpu->arch.hcr_el2 |= HCR_TID2;
+
+	if (kvm_has_mte(vcpu->kvm))
+		vcpu->arch.hcr_el2 |= HCR_ATA;
 }
 
 static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu)
arch/arm64/include/asm/kvm_host.h +3 -0
index 7cd7d5c8c4bc..afaa5333f0e4 100644
@@ -132,6 +132,8 @@ struct kvm_arch {
 
 	u8 pfr0_csv2;
 	u8 pfr0_csv3;
+	/* Memory Tagging Extension enabled for the guest */
+	bool mte_enabled;
 };
 
 struct kvm_vcpu_fault_info {
@@ -769,6 +771,7 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
 #define kvm_arm_vcpu_sve_finalized(vcpu) \
 	((vcpu)->arch.flags & KVM_ARM64_VCPU_SVE_FINALIZED)
 
+#define kvm_has_mte(kvm) (system_supports_mte() && (kvm)->arch.mte_enabled)
 #define kvm_vcpu_has_pmu(vcpu)					\
 	(test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features))
 
arch/arm64/kvm/hyp/exception.c +2 -1
index 73629094f903..56426565600c 100644
@@ -112,7 +112,8 @@ static void enter_exception64(struct kvm_vcpu *vcpu, unsigned long target_mode,
 	new |= (old & PSR_C_BIT);
 	new |= (old & PSR_V_BIT);
 
-	// TODO: TCO (if/when ARMv8.5-MemTag is exposed to guests)
+	if (kvm_has_mte(vcpu->kvm))
+		new |= PSR_TCO_BIT;
 
 	new |= (old & PSR_DIT_BIT);
 
arch/arm64/kvm/mmu.c +0 -0
index c5d1f3c87dbd..8660f6a03f51 100644

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 826

Quoted 5 lines
arch/arm64/kvm/mmu.c +2 -0
@@ -822,6 +822,31 @@ transparent_hugepage_adjust(struct kvm_memory_slot *memslot,
 	return PAGE_SIZE;
 }
 
+static int sanitise_mte_tags(struct kvm *kvm, unsigned long size,
+			     kvm_pfn_t pfn)

Nit: please order the parameters as address, then size.

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 839

Quoted 13 lines
arch/arm64/kvm/mmu.c +13 -0
+{
+	if (kvm_has_mte(kvm)) {
+		/*
+		 * The page will be mapped in stage 2 as Normal Cacheable, so
+		 * the VM will be able to see the page's tags and therefore
+		 * they must be initialised first. If PG_mte_tagged is set,
+		 * tags have already been initialised.
+		 */
+		unsigned long i, nr_pages = size >> PAGE_SHIFT;
+		struct page *page = pfn_to_online_page(pfn);
+
+		if (!page)
+			return -EFAULT;

Under which circumstances can this happen? We already have done a GUP on the page, so I really can't see how the page can vanish from under our feet.

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 843

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+
+		for (i = 0; i < nr_pages; i++, page++) {
+			if (!test_and_set_bit(PG_mte_tagged, &page->flags))
+				mte_clear_page_tags(page_address(page));

You seem to be doing this irrespective of the VMA being created with PROT_MTE. This is fine form a guest perspective (all its memory should be MTE capable). However, I can't see any guarantee that the VMM will actually allocate memslots with PROT_MTE.

Aren't we missing some sanity checks at memslot registration time?

Quoted 28 lines
arch/arm64/kvm/mmu.c +12 -1
+		}
+	}
+
+	return 0;
+}
+
 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 			  struct kvm_memory_slot *memslot, unsigned long hva,
 			  unsigned long fault_status)
@@ -971,8 +996,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (writable)
 		prot |= KVM_PGTABLE_PROT_W;
 
-	if (fault_status != FSC_PERM && !device)
+	if (fault_status != FSC_PERM && !device) {
+		ret = sanitise_mte_tags(kvm, vma_pagesize, pfn);
+		if (ret)
+			goto out_unlock;
+
 		clean_dcache_guest_page(pfn, vma_pagesize);
+	}
 
 	if (exec_fault) {
 		prot |= KVM_PGTABLE_PROT_X;
@@ -1168,12 +1198,17 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
 {
 	kvm_pfn_t pfn = pte_pfn(range->pte);

arch/arm64/kvm/mmu.c bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) · line 1210

Quoted 10 lines
arch/arm64/kvm/mmu.c +4 -0
+	int ret;
 
 	if (!kvm->arch.mmu.pgt)
 		return 0;
 
 	WARN_ON(range->end - range->start != 1);
 
+	ret = sanitise_mte_tags(kvm, PAGE_SIZE, pfn);
+	if (ret)
+		return ret;

Notice the change in return type?

Quoted 9 lines
arch/arm64/kvm/mmu.c +1 -0
+
 	/*
 	 * We've moved a page around, probably through CoW, so let's treat it
 	 * just like a translation fault and clean the cache to the PoC.
arch/arm64/kvm/sys_regs.c +0 -0
index 76ea2800c33e..24a844cb79ca 100644

arch/arm64/kvm/sys_regs.c static u64 read_id_reg(const struct kvm_vcpu *vcpu, · line 1052

Quoted 6 lines
arch/arm64/kvm/sys_regs.c +3 -0
@@ -1047,6 +1047,9 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu,
 		break;
 	case SYS_ID_AA64PFR1_EL1:
 		val &= ~FEATURE(ID_AA64PFR1_MTE);
+		if (kvm_has_mte(vcpu->kvm))
+			val |= FIELD_PREP(FEATURE(ID_AA64PFR1_MTE),
+					  ID_AA64PFR1_MTE);

Shouldn't this be consistent with what the HW is capable of (i.e. FEAT_MTE3 if available), and extracted from the sanitised view of the feature set?

Quoted 8 lines
arch/arm64/kvm/sys_regs.c +0 -0
 		break;
 	case SYS_ID_AA64ISAR1_EL1:
 		if (!vcpu_has_ptrauth(vcpu))
include/uapi/linux/kvm.h +0 -0
index 3fd9a7e9d90c..8c95ba0fadda 100644

include/uapi/linux/kvm.h struct kvm_ppc_resize_hpt { · line 1088

Quoted 7 lines
include/uapi/linux/kvm.h +1 -0
@@ -1082,6 +1082,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_SGX_ATTRIBUTE 196
 #define KVM_CAP_VM_COPY_ENC_CONTEXT_FROM 197
 #define KVM_CAP_PTP_KVM 198
+#define KVM_CAP_ARM_MTE 199
 
 #ifdef KVM_CAP_IRQ_ROUTING
 

Thanks,

	M.
Signature · 5 lines
Without deviation from the norm, progress is not possible.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#17 lore raw

Re: [PATCH v12 4/8] arm64: kvm: Introduce MTE VM feature

Quoted 9 lines · Marc Zyngier
Quoted 7 lines · Steven Price

Add a new VM feature 'KVM_ARM_CAP_MTE' which enables memory tagging for a VM. This will expose the feature to the guest and automatically tag memory pages touched by the VM as PG_mte_tagged (and clear the tag storage) to ensure that the guest cannot see stale tags, and so that the tags are correctly saved/restored across swap.

Quoted 5 lines
Quoted 2 lines

Actually exposing the new capability to user space happens in a later patch.

uber nit in $SUBJECT: "KVM: arm64:" is the preferred prefix (just like patches 7 and 8).

Good spot - I obviously got carried away with the "arm64:" prefix ;)

Quoted 68 lines
Quoted 68 lines
---
 arch/arm64/include/asm/kvm_emulate.h |  3 +++
 arch/arm64/include/asm/kvm_host.h    |  3 +++
 arch/arm64/kvm/hyp/exception.c       |  3 ++-
 arch/arm64/kvm/mmu.c                 | 37 +++++++++++++++++++++++++++-
 arch/arm64/kvm/sys_regs.c            |  3 +++
 include/uapi/linux/kvm.h             |  1 +
 6 files changed, 48 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +3 -0
index f612c090f2e4..6bf776c2399c 100644
@@ -84,6 +84,9 @@ static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
 	if (cpus_have_const_cap(ARM64_MISMATCHED_CACHE_TYPE) ||
 	    vcpu_el1_is_32bit(vcpu))
 		vcpu->arch.hcr_el2 |= HCR_TID2;
+
+	if (kvm_has_mte(vcpu->kvm))
+		vcpu->arch.hcr_el2 |= HCR_ATA;
 }
 
 static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu)
arch/arm64/include/asm/kvm_host.h +3 -0
index 7cd7d5c8c4bc..afaa5333f0e4 100644
@@ -132,6 +132,8 @@ struct kvm_arch {
 
 	u8 pfr0_csv2;
 	u8 pfr0_csv3;
+	/* Memory Tagging Extension enabled for the guest */
+	bool mte_enabled;
 };
 
 struct kvm_vcpu_fault_info {
@@ -769,6 +771,7 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
 #define kvm_arm_vcpu_sve_finalized(vcpu) \
 	((vcpu)->arch.flags & KVM_ARM64_VCPU_SVE_FINALIZED)
 
+#define kvm_has_mte(kvm) (system_supports_mte() && (kvm)->arch.mte_enabled)
 #define kvm_vcpu_has_pmu(vcpu)					\
 	(test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features))
 
arch/arm64/kvm/hyp/exception.c +2 -1
index 73629094f903..56426565600c 100644
@@ -112,7 +112,8 @@ static void enter_exception64(struct kvm_vcpu *vcpu, unsigned long target_mode,
 	new |= (old & PSR_C_BIT);
 	new |= (old & PSR_V_BIT);
 
-	// TODO: TCO (if/when ARMv8.5-MemTag is exposed to guests)
+	if (kvm_has_mte(vcpu->kvm))
+		new |= PSR_TCO_BIT;
 
 	new |= (old & PSR_DIT_BIT);
 
arch/arm64/kvm/mmu.c +0 -0
index c5d1f3c87dbd..8660f6a03f51 100644
@@ -822,6 +822,31 @@ transparent_hugepage_adjust(struct kvm_memory_slot *memslot,
 	return PAGE_SIZE;
 }
 
Quoted 4 lines

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 826

Quoted 2 lines
arch/arm64/kvm/mmu.c +2 -0
+static int sanitise_mte_tags(struct kvm *kvm, unsigned long size,
+			     kvm_pfn_t pfn)

Nit: please order the parameters as address, then size.

Sure

Quoted 9 lines
Quoted 9 lines
arch/arm64/kvm/mmu.c +9 -0
+{
+	if (kvm_has_mte(kvm)) {
+		/*
+		 * The page will be mapped in stage 2 as Normal Cacheable, so
+		 * the VM will be able to see the page's tags and therefore
+		 * they must be initialised first. If PG_mte_tagged is set,
+		 * tags have already been initialised.
+		 */
+		unsigned long i, nr_pages = size >> PAGE_SHIFT;
Quoted 8 lines

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 839

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+		struct page *page = pfn_to_online_page(pfn);
+
+		if (!page)
+			return -EFAULT;

Under which circumstances can this happen? We already have done a GUP on the page, so I really can't see how the page can vanish from under our feet.

It's less about the page vanishing and more that pfn_to_online_page() will reject some pages. Specifically in this case we want to reject any sort of device memory (e.g. graphics card memory or other memory on the end of a bus like PCIe) as it is unlikely to support MTE.

Quoted 11 lines

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 843

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+
+		for (i = 0; i < nr_pages; i++, page++) {
+			if (!test_and_set_bit(PG_mte_tagged, &page->flags))
+				mte_clear_page_tags(page_address(page));

You seem to be doing this irrespective of the VMA being created with PROT_MTE. This is fine form a guest perspective (all its memory should be MTE capable). However, I can't see any guarantee that the VMM will actually allocate memslots with PROT_MTE.

Aren't we missing some sanity checks at memslot registration time?

I've taken the decision not to require that the VMM allocates with PROT_MTE, there are two main reasons for this:

1. The VMM generally doesn't actually want a PROT_MTE mapping as the
tags from the guest are almost certainly wrong for most usages (e.g. device emulation). So a PROT_MTE mapping actively gets in the way of the VMM using MTE for it's own purposes. However this then leads to the requirement for the new ioctl in patch 7.

2. Because the VMM can change the pages in a memslot at any time and
KVM relies on mmu notifiers to spot the change it's hard and ugly to enforce that the memslot VMAs keep having the PROT_MTE flag. When I tried this it meant we've discover that a page doesn't have the MTE flag at fault time and have no other option that to kill the VM at that time.

So the model is that non-PROT_MTE memory can be supplied to the memslots and KVM will automatically upgrade it to PG_mte_tagged if you supply it to a VM with MTE enabled. This makes the VMM implementation easier for most cases, and the new ioctl helps for migration. I think the kernel code is tidier too.

Of course even better would be a stage 2 flag to control MTE availability on a page-by-page basis, but that doesn't exist in the architecture at the moment.

Quoted 33 lines
Quoted 33 lines
arch/arm64/kvm/mmu.c +13 -1
+		}
+	}
+
+	return 0;
+}
+
 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 			  struct kvm_memory_slot *memslot, unsigned long hva,
 			  unsigned long fault_status)
@@ -971,8 +996,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (writable)
 		prot |= KVM_PGTABLE_PROT_W;
 
-	if (fault_status != FSC_PERM && !device)
+	if (fault_status != FSC_PERM && !device) {
+		ret = sanitise_mte_tags(kvm, vma_pagesize, pfn);
+		if (ret)
+			goto out_unlock;
+
 		clean_dcache_guest_page(pfn, vma_pagesize);
+	}
 
 	if (exec_fault) {
 		prot |= KVM_PGTABLE_PROT_X;
@@ -1168,12 +1198,17 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
 {
 	kvm_pfn_t pfn = pte_pfn(range->pte);
+	int ret;
 
 	if (!kvm->arch.mmu.pgt)
 		return 0;
 
Quoted 7 lines

arch/arm64/kvm/mmu.c bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) · line 1210

Quoted 5 lines
arch/arm64/kvm/mmu.c +3 -0
 	WARN_ON(range->end - range->start != 1);
 
+	ret = sanitise_mte_tags(kvm, PAGE_SIZE, pfn);
+	if (ret)
+		return ret;

Notice the change in return type?

I do now - I was tricked by the use of '0' as false. Looks like false ('0') is actually the correct return here to avoid an unnecessary kvm_flush_remote_tlbs().

Quoted 11 lines
Quoted 11 lines
arch/arm64/kvm/mmu.c +1 -0
+
 	/*
 	 * We've moved a page around, probably through CoW, so let's treat it
 	 * just like a translation fault and clean the cache to the PoC.
arch/arm64/kvm/sys_regs.c +0 -0
index 76ea2800c33e..24a844cb79ca 100644
@@ -1047,6 +1047,9 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu,
 		break;
 	case SYS_ID_AA64PFR1_EL1:
Quoted 8 lines

arch/arm64/kvm/sys_regs.c static u64 read_id_reg(const struct kvm_vcpu *vcpu, · line 1052

Quoted 4 lines
arch/arm64/kvm/sys_regs.c +3 -0
 		val &= ~FEATURE(ID_AA64PFR1_MTE);
+		if (kvm_has_mte(vcpu->kvm))
+			val |= FIELD_PREP(FEATURE(ID_AA64PFR1_MTE),
+					  ID_AA64PFR1_MTE);

Shouldn't this be consistent with what the HW is capable of (i.e. FEAT_MTE3 if available), and extracted from the sanitised view of the feature set?

Yes - however at the moment our sanitised view is either FEAT_MTE2 or nothing:

{
.desc = "Memory Tagging Extension", .capability = ARM64_MTE,
.type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE, .matches = has_cpuid_feature, .sys_reg = SYS_ID_AA64PFR1_EL1, .field_pos = ID_AA64PFR1_MTE_SHIFT, .min_field_value = ID_AA64PFR1_MTE, .sign = FTR_UNSIGNED,
.cpu_enable = cpu_enable_mte,
},

When host support for FEAT_MTE3 is added then the KVM code will need revisiting to expose that down to the guest safely (AFAICS there's nothing extra to do here, but I haven't tested any of the MTE3 features). I don't think we want to expose newer versions to the guest than the host is aware of. (Or indeed expose FEAT_MTE if the host has MTE disabled because Linux requires at least FEAT_MTE2).

Thanks,

Steve

Quoted 20 lines
Quoted 8 lines
arch/arm64/kvm/sys_regs.c +0 -0
 		break;
 	case SYS_ID_AA64ISAR1_EL1:
 		if (!vcpu_has_ptrauth(vcpu))
include/uapi/linux/kvm.h +0 -0
index 3fd9a7e9d90c..8c95ba0fadda 100644

include/uapi/linux/kvm.h struct kvm_ppc_resize_hpt { · line 1088

Quoted 7 lines
include/uapi/linux/kvm.h +1 -0
@@ -1082,6 +1082,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_SGX_ATTRIBUTE 196
 #define KVM_CAP_VM_COPY_ENC_CONTEXT_FROM 197
 #define KVM_CAP_PTP_KVM 198
+#define KVM_CAP_ARM_MTE 199
 
 #ifdef KVM_CAP_IRQ_ROUTING
 

Thanks,

	M.
maintainer
#18 lore raw

Re: [PATCH v12 4/8] arm64: kvm: Introduce MTE VM feature

Quoted 112 lines · Steven Price
Quoted 9 lines · Marc Zyngier
Quoted 7 lines · Steven Price

Add a new VM feature 'KVM_ARM_CAP_MTE' which enables memory tagging for a VM. This will expose the feature to the guest and automatically tag memory pages touched by the VM as PG_mte_tagged (and clear the tag storage) to ensure that the guest cannot see stale tags, and so that the tags are correctly saved/restored across swap.

Quoted 5 lines
Quoted 2 lines

Actually exposing the new capability to user space happens in a later patch.

uber nit in $SUBJECT: "KVM: arm64:" is the preferred prefix (just like patches 7 and 8).

Good spot - I obviously got carried away with the "arm64:" prefix ;)

Quoted 68 lines
Quoted 68 lines
---
 arch/arm64/include/asm/kvm_emulate.h |  3 +++
 arch/arm64/include/asm/kvm_host.h    |  3 +++
 arch/arm64/kvm/hyp/exception.c       |  3 ++-
 arch/arm64/kvm/mmu.c                 | 37 +++++++++++++++++++++++++++-
 arch/arm64/kvm/sys_regs.c            |  3 +++
 include/uapi/linux/kvm.h             |  1 +
 6 files changed, 48 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +3 -0
index f612c090f2e4..6bf776c2399c 100644
@@ -84,6 +84,9 @@ static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
 	if (cpus_have_const_cap(ARM64_MISMATCHED_CACHE_TYPE) ||
 	    vcpu_el1_is_32bit(vcpu))
 		vcpu->arch.hcr_el2 |= HCR_TID2;
+
+	if (kvm_has_mte(vcpu->kvm))
+		vcpu->arch.hcr_el2 |= HCR_ATA;
 }
 
 static inline unsigned long *vcpu_hcr(struct kvm_vcpu *vcpu)
arch/arm64/include/asm/kvm_host.h +3 -0
index 7cd7d5c8c4bc..afaa5333f0e4 100644
@@ -132,6 +132,8 @@ struct kvm_arch {
 
 	u8 pfr0_csv2;
 	u8 pfr0_csv3;
+	/* Memory Tagging Extension enabled for the guest */
+	bool mte_enabled;
 };
 
 struct kvm_vcpu_fault_info {
@@ -769,6 +771,7 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
 #define kvm_arm_vcpu_sve_finalized(vcpu) \
 	((vcpu)->arch.flags & KVM_ARM64_VCPU_SVE_FINALIZED)
 
+#define kvm_has_mte(kvm) (system_supports_mte() && (kvm)->arch.mte_enabled)
 #define kvm_vcpu_has_pmu(vcpu)					\
 	(test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features))
 
arch/arm64/kvm/hyp/exception.c +2 -1
index 73629094f903..56426565600c 100644
@@ -112,7 +112,8 @@ static void enter_exception64(struct kvm_vcpu *vcpu, unsigned long target_mode,
 	new |= (old & PSR_C_BIT);
 	new |= (old & PSR_V_BIT);
 
-	// TODO: TCO (if/when ARMv8.5-MemTag is exposed to guests)
+	if (kvm_has_mte(vcpu->kvm))
+		new |= PSR_TCO_BIT;
 
 	new |= (old & PSR_DIT_BIT);
 
arch/arm64/kvm/mmu.c +0 -0
index c5d1f3c87dbd..8660f6a03f51 100644
@@ -822,6 +822,31 @@ transparent_hugepage_adjust(struct kvm_memory_slot *memslot,
 	return PAGE_SIZE;
 }
 
Quoted 4 lines

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 826

Quoted 2 lines
arch/arm64/kvm/mmu.c +2 -0
+static int sanitise_mte_tags(struct kvm *kvm, unsigned long size,
+			     kvm_pfn_t pfn)

Nit: please order the parameters as address, then size.

Sure

Quoted 9 lines
Quoted 9 lines
arch/arm64/kvm/mmu.c +9 -0
+{
+	if (kvm_has_mte(kvm)) {
+		/*
+		 * The page will be mapped in stage 2 as Normal Cacheable, so
+		 * the VM will be able to see the page's tags and therefore
+		 * they must be initialised first. If PG_mte_tagged is set,
+		 * tags have already been initialised.
+		 */
+		unsigned long i, nr_pages = size >> PAGE_SHIFT;
Quoted 8 lines

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 839

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+		struct page *page = pfn_to_online_page(pfn);
+
+		if (!page)
+			return -EFAULT;

Under which circumstances can this happen? We already have done a GUP on the page, so I really can't see how the page can vanish from under our feet.

Quoted 4 lines

It's less about the page vanishing and more that pfn_to_online_page() will reject some pages. Specifically in this case we want to reject any sort of device memory (e.g. graphics card memory or other memory on the end of a bus like PCIe) as it is unlikely to support MTE.

OK. We really never should see this error as we check for device mappings right before calling this, but I guess it doesn't hurt.

Quoted 28 lines
Quoted 11 lines

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 843

Quoted 5 lines
arch/arm64/kvm/mmu.c +4 -0
+
+		for (i = 0; i < nr_pages; i++, page++) {
+			if (!test_and_set_bit(PG_mte_tagged, &page->flags))
+				mte_clear_page_tags(page_address(page));

You seem to be doing this irrespective of the VMA being created with PROT_MTE. This is fine form a guest perspective (all its memory should be MTE capable). However, I can't see any guarantee that the VMM will actually allocate memslots with PROT_MTE.

Quoted 1 line

Aren't we missing some sanity checks at memslot registration time?

I've taken the decision not to require that the VMM allocates with PROT_MTE, there are two main reasons for this:

1. The VMM generally doesn't actually want a PROT_MTE mapping as the
tags from the guest are almost certainly wrong for most usages (e.g. device emulation). So a PROT_MTE mapping actively gets in the way of the VMM using MTE for it's own purposes. However this then leads to the requirement for the new ioctl in patch 7.

2. Because the VMM can change the pages in a memslot at any time and
KVM relies on mmu notifiers to spot the change it's hard and ugly to enforce that the memslot VMAs keep having the PROT_MTE flag. When I tried this it meant we've discover that a page doesn't have the MTE flag at fault time and have no other option that to kill the VM at that time.

Quoted 5 lines

So the model is that non-PROT_MTE memory can be supplied to the memslots and KVM will automatically upgrade it to PG_mte_tagged if you supply it to a VM with MTE enabled. This makes the VMM implementation easier for most cases, and the new ioctl helps for migration. I think the kernel code is tidier too.

OK, I see your point. I guess we rely on the implicit requirement that all the available memory is MTE-capable, although I'm willing to bet that someone will eventually break this requirement.

Quoted 3 lines

Of course even better would be a stage 2 flag to control MTE availability on a page-by-page basis, but that doesn't exist in the architecture at the moment.

Nah, that'd be way too good. Let's not do that.

Quoted 42 lines
Quoted 34 lines
Quoted 34 lines
arch/arm64/kvm/mmu.c +13 -1
+		}
+	}
+
+	return 0;
+}
+
 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 			  struct kvm_memory_slot *memslot, unsigned long hva,
 			  unsigned long fault_status)
@@ -971,8 +996,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (writable)
 		prot |= KVM_PGTABLE_PROT_W;
 
-	if (fault_status != FSC_PERM && !device)
+	if (fault_status != FSC_PERM && !device) {
+		ret = sanitise_mte_tags(kvm, vma_pagesize, pfn);
+		if (ret)
+			goto out_unlock;
+
 		clean_dcache_guest_page(pfn, vma_pagesize);
+	}
 
 	if (exec_fault) {
 		prot |= KVM_PGTABLE_PROT_X;
@@ -1168,12 +1198,17 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
 {
 	kvm_pfn_t pfn = pte_pfn(range->pte);
+	int ret;
 
 	if (!kvm->arch.mmu.pgt)
 		return 0;
 
Quoted 7 lines

arch/arm64/kvm/mmu.c bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) · line 1210

Quoted 5 lines
arch/arm64/kvm/mmu.c +3 -0
 	WARN_ON(range->end - range->start != 1);
 
+	ret = sanitise_mte_tags(kvm, PAGE_SIZE, pfn);
+	if (ret)
+		return ret;

Notice the change in return type?

Quoted 3 lines

I do now - I was tricked by the use of '0' as false. Looks like false ('0') is actually the correct return here to avoid an unnecessary kvm_flush_remote_tlbs().

Yup. BTW, the return values have been fixed to proper boolean types in the latest set of fixes.

Quoted 36 lines
Quoted 12 lines
Quoted 12 lines
arch/arm64/kvm/mmu.c +1 -0
+
 	/*
 	 * We've moved a page around, probably through CoW, so let's treat it
 	 * just like a translation fault and clean the cache to the PoC.
arch/arm64/kvm/sys_regs.c +0 -0
index 76ea2800c33e..24a844cb79ca 100644
@@ -1047,6 +1047,9 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu,
 		break;
 	case SYS_ID_AA64PFR1_EL1:
Quoted 8 lines

arch/arm64/kvm/sys_regs.c static u64 read_id_reg(const struct kvm_vcpu *vcpu, · line 1052

Quoted 4 lines
arch/arm64/kvm/sys_regs.c +3 -0
 		val &= ~FEATURE(ID_AA64PFR1_MTE);
+		if (kvm_has_mte(vcpu->kvm))
+			val |= FIELD_PREP(FEATURE(ID_AA64PFR1_MTE),
+					  ID_AA64PFR1_MTE);

Shouldn't this be consistent with what the HW is capable of (i.e. FEAT_MTE3 if available), and extracted from the sanitised view of the feature set?

Yes - however at the moment our sanitised view is either FEAT_MTE2 or nothing:

{
.desc = "Memory Tagging Extension", .capability = ARM64_MTE,
.type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE, .matches = has_cpuid_feature, .sys_reg = SYS_ID_AA64PFR1_EL1, .field_pos = ID_AA64PFR1_MTE_SHIFT, .min_field_value = ID_AA64PFR1_MTE, .sign = FTR_UNSIGNED,
.cpu_enable = cpu_enable_mte,
},

Quoted 6 lines

When host support for FEAT_MTE3 is added then the KVM code will need revisiting to expose that down to the guest safely (AFAICS there's nothing extra to do here, but I haven't tested any of the MTE3 features). I don't think we want to expose newer versions to the guest than the host is aware of. (Or indeed expose FEAT_MTE if the host has MTE disabled because Linux requires at least FEAT_MTE2).

What I was suggesting is to have something like this:

     pfr = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
     mte = cpuid_feature_extract_unsigned_field(pfr, ID_AA64PFR1_MTE_SHIFT);
     val |= FIELD_PREP(FEATURE(ID_AA64PFR1_MTE), mte);

which does the trick nicely, and doesn't expose more than the host supports.

Thanks,

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

Re: [PATCH v12 4/8] arm64: kvm: Introduce MTE VM feature

Quoted 6 lines · Marc Zyngier
Quoted 4 lines · Steven Price
Quoted 2 lines · Marc Zyngier

On Mon, 17 May 2021 13:32:35 +0100,
Steven Price <[email protected]> wrote:

[...]

Quoted 41 lines
Quoted 41 lines
Quoted 40 lines
Quoted 28 lines
arch/arm64/kvm/mmu.c +12 -1
+		}
+	}
+
+	return 0;
+}
+
 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 			  struct kvm_memory_slot *memslot, unsigned long hva,
 			  unsigned long fault_status)
@@ -971,8 +996,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (writable)
 		prot |= KVM_PGTABLE_PROT_W;
 
-	if (fault_status != FSC_PERM && !device)
+	if (fault_status != FSC_PERM && !device) {
+		ret = sanitise_mte_tags(kvm, vma_pagesize, pfn);
+		if (ret)
+			goto out_unlock;
+
 		clean_dcache_guest_page(pfn, vma_pagesize);
+	}
 
 	if (exec_fault) {
 		prot |= KVM_PGTABLE_PROT_X;
@@ -1168,12 +1198,17 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
 {
 	kvm_pfn_t pfn = pte_pfn(range->pte);

arch/arm64/kvm/mmu.c bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) · line 1210

Quoted 10 lines
arch/arm64/kvm/mmu.c +4 -0
+	int ret;
 
 	if (!kvm->arch.mmu.pgt)
 		return 0;
 
 	WARN_ON(range->end - range->start != 1);
 
+	ret = sanitise_mte_tags(kvm, PAGE_SIZE, pfn);
+	if (ret)
+		return ret;

Notice the change in return type?

Quoted 6 lines
Quoted 3 lines

I do now - I was tricked by the use of '0' as false. Looks like false ('0') is actually the correct return here to avoid an unnecessary kvm_flush_remote_tlbs().

Yup. BTW, the return values have been fixed to proper boolean types in the latest set of fixes.

Thanks for the heads up - I'll return 'false' to avoid regressing that.

Quoted 43 lines
Quoted 36 lines
Quoted 12 lines
Quoted 12 lines
arch/arm64/kvm/mmu.c +1 -0
+
 	/*
 	 * We've moved a page around, probably through CoW, so let's treat it
 	 * just like a translation fault and clean the cache to the PoC.
arch/arm64/kvm/sys_regs.c +0 -0
index 76ea2800c33e..24a844cb79ca 100644
@@ -1047,6 +1047,9 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu,
 		break;
 	case SYS_ID_AA64PFR1_EL1:
Quoted 8 lines

arch/arm64/kvm/sys_regs.c static u64 read_id_reg(const struct kvm_vcpu *vcpu, · line 1052

Quoted 4 lines
arch/arm64/kvm/sys_regs.c +3 -0
 		val &= ~FEATURE(ID_AA64PFR1_MTE);
+		if (kvm_has_mte(vcpu->kvm))
+			val |= FIELD_PREP(FEATURE(ID_AA64PFR1_MTE),
+					  ID_AA64PFR1_MTE);

Shouldn't this be consistent with what the HW is capable of (i.e. FEAT_MTE3 if available), and extracted from the sanitised view of the feature set?

Yes - however at the moment our sanitised view is either FEAT_MTE2 or nothing:

{
.desc = "Memory Tagging Extension", .capability = ARM64_MTE,
.type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE, .matches = has_cpuid_feature, .sys_reg = SYS_ID_AA64PFR1_EL1, .field_pos = ID_AA64PFR1_MTE_SHIFT, .min_field_value = ID_AA64PFR1_MTE, .sign = FTR_UNSIGNED,
.cpu_enable = cpu_enable_mte,
},

Quoted 6 lines

When host support for FEAT_MTE3 is added then the KVM code will need revisiting to expose that down to the guest safely (AFAICS there's nothing extra to do here, but I haven't tested any of the MTE3 features). I don't think we want to expose newer versions to the guest than the host is aware of. (Or indeed expose FEAT_MTE if the host has MTE disabled because Linux requires at least FEAT_MTE2).

Quoted 8 lines

What I was suggesting is to have something like this:

     pfr = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
     mte = cpuid_feature_extract_unsigned_field(pfr, ID_AA64PFR1_MTE_SHIFT);
     val |= FIELD_PREP(FEATURE(ID_AA64PFR1_MTE), mte);

which does the trick nicely, and doesn't expose more than the host supports.

Ok, I have to admit to not fully understanding the sanitised register code - but wouldn't this expose higher MTE values if all CPUs support it, even though the host doesn't know what a hypothetical 'MTE4' adds? Or is there some magic in the sanitising that caps the value to what the host knows about?

Thanks,

Steve

maintainer
#20 lore raw

Re: [PATCH v12 4/8] arm64: kvm: Introduce MTE VM feature

In reply to Steven Price
Quoted 13 lines · Steven Price
arch/arm64/kvm/mmu.c +5 -0
index c5d1f3c87dbd..8660f6a03f51 100644
@@ -822,6 +822,31 @@ transparent_hugepage_adjust(struct kvm_memory_slot *memslot,
 	return PAGE_SIZE;
 }
 
+static int sanitise_mte_tags(struct kvm *kvm, unsigned long size,
+			     kvm_pfn_t pfn)
+{
+	if (kvm_has_mte(kvm)) {
+		/*

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 839

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+		 * The page will be mapped in stage 2 as Normal Cacheable, so
+		 * the VM will be able to see the page's tags and therefore
+		 * they must be initialised first. If PG_mte_tagged is set,
+		 * tags have already been initialised.
+		 */
+		unsigned long i, nr_pages = size >> PAGE_SHIFT;
+		struct page *page = pfn_to_online_page(pfn);
+
+		if (!page)
+			return -EFAULT;

IIRC we ended up with pfn_to_online_page() to reject ZONE_DEVICE pages that may be mapped into a guest and we have no idea whether they support MTE. It may be worth adding a comment, otherwise, as Marc said, the page wouldn't disappear.

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 843

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+
+		for (i = 0; i < nr_pages; i++, page++) {
+			if (!test_and_set_bit(PG_mte_tagged, &page->flags))
+				mte_clear_page_tags(page_address(page));

We started the page->flags thread and ended up fixing it for the host set_pte_at() as per the first patch:

https://lore.kernel.org/r/[email protected]

Now, can we have a race between the stage 2 kvm_set_spte_gfn() and a stage 1 set_pte_at()? Only the latter takes a lock. Or between two kvm_set_spte_gfn() in different VMs? I think in the above thread we concluded that there's only a problem if the page is shared between multiple VMMs (MAP_SHARED). How realistic is this and what's the workaround?

Either way, I think it's worth adding a comment here on the race on page->flags as it looks strange that here it's just a test_and_set_bit() while set_pte_at() uses a spinlock.

Signature · 5 lines
Catalin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#21 lore raw

Re: [PATCH v12 4/8] arm64: kvm: Introduce MTE VM feature

Quoted 21 lines · Catalin Marinas
Quoted 20 lines · Steven Price
arch/arm64/kvm/mmu.c +12 -0
index c5d1f3c87dbd..8660f6a03f51 100644
@@ -822,6 +822,31 @@ transparent_hugepage_adjust(struct kvm_memory_slot *memslot,
 	return PAGE_SIZE;
 }
 
+static int sanitise_mte_tags(struct kvm *kvm, unsigned long size,
+			     kvm_pfn_t pfn)
+{
+	if (kvm_has_mte(kvm)) {
+		/*
+		 * The page will be mapped in stage 2 as Normal Cacheable, so
+		 * the VM will be able to see the page's tags and therefore
+		 * they must be initialised first. If PG_mte_tagged is set,
+		 * tags have already been initialised.
+		 */
+		unsigned long i, nr_pages = size >> PAGE_SHIFT;
+		struct page *page = pfn_to_online_page(pfn);
Quoted 8 lines

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 839

Quoted 3 lines
arch/arm64/kvm/mmu.c +3 -0
+
+		if (!page)
+			return -EFAULT;

IIRC we ended up with pfn_to_online_page() to reject ZONE_DEVICE pages that may be mapped into a guest and we have no idea whether they support MTE. It may be worth adding a comment, otherwise, as Marc said, the page wouldn't disappear.

I'll add a comment.

Quoted 17 lines

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 843

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+
+		for (i = 0; i < nr_pages; i++, page++) {
+			if (!test_and_set_bit(PG_mte_tagged, &page->flags))
+				mte_clear_page_tags(page_address(page));

We started the page->flags thread and ended up fixing it for the host set_pte_at() as per the first patch:

https://lore.kernel.org/r/[email protected]

Now, can we have a race between the stage 2 kvm_set_spte_gfn() and a stage 1 set_pte_at()? Only the latter takes a lock. Or between two kvm_set_spte_gfn() in different VMs? I think in the above thread we concluded that there's only a problem if the page is shared between multiple VMMs (MAP_SHARED). How realistic is this and what's the workaround?

Quoted 4 lines

Either way, I think it's worth adding a comment here on the race on page->flags as it looks strange that here it's just a test_and_set_bit() while set_pte_at() uses a spinlock.

Very good point! I should have thought about that. I think splitting the test_and_set_bit() in two (as with the cache flush) is sufficient. While there technically still is a race which could lead to user space tags being clobbered:

a) It's very odd for a VMM to be doing an mprotect() after the fact to add PROT_MTE, or to be sharing the memory with another process which sets PROT_MTE.

b) The window for the race is incredibly small and the VMM (generally) needs to be robust against the guest changing tags anyway.

But I'll add a comment here as well:

/*
* There is a potential race between sanitising the
* flags here and user space using mprotect() to add
* PROT_MTE to access the tags, however by splitting
* the test/set the only risk is user space tags
* being overwritten by the mte_clear_page_tags() call. */

Thanks,

Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer
#22 lore raw

Re: [PATCH v12 4/8] arm64: kvm: Introduce MTE VM feature

Quoted 69 lines · Steven Price
Quoted 21 lines · Catalin Marinas
Quoted 20 lines · Steven Price
arch/arm64/kvm/mmu.c +12 -0
index c5d1f3c87dbd..8660f6a03f51 100644
@@ -822,6 +822,31 @@ transparent_hugepage_adjust(struct kvm_memory_slot *memslot,
 	return PAGE_SIZE;
 }
 
+static int sanitise_mte_tags(struct kvm *kvm, unsigned long size,
+			     kvm_pfn_t pfn)
+{
+	if (kvm_has_mte(kvm)) {
+		/*
+		 * The page will be mapped in stage 2 as Normal Cacheable, so
+		 * the VM will be able to see the page's tags and therefore
+		 * they must be initialised first. If PG_mte_tagged is set,
+		 * tags have already been initialised.
+		 */
+		unsigned long i, nr_pages = size >> PAGE_SHIFT;
+		struct page *page = pfn_to_online_page(pfn);
Quoted 8 lines

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 839

Quoted 3 lines
arch/arm64/kvm/mmu.c +3 -0
+
+		if (!page)
+			return -EFAULT;

IIRC we ended up with pfn_to_online_page() to reject ZONE_DEVICE pages that may be mapped into a guest and we have no idea whether they support MTE. It may be worth adding a comment, otherwise, as Marc said, the page wouldn't disappear.

I'll add a comment.

Quoted 17 lines

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 843

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+
+		for (i = 0; i < nr_pages; i++, page++) {
+			if (!test_and_set_bit(PG_mte_tagged, &page->flags))
+				mte_clear_page_tags(page_address(page));

We started the page->flags thread and ended up fixing it for the host set_pte_at() as per the first patch:

https://lore.kernel.org/r/[email protected]

Now, can we have a race between the stage 2 kvm_set_spte_gfn() and a stage 1 set_pte_at()? Only the latter takes a lock. Or between two kvm_set_spte_gfn() in different VMs? I think in the above thread we concluded that there's only a problem if the page is shared between multiple VMMs (MAP_SHARED). How realistic is this and what's the workaround?

Quoted 4 lines

Either way, I think it's worth adding a comment here on the race on page->flags as it looks strange that here it's just a test_and_set_bit() while set_pte_at() uses a spinlock.

Very good point! I should have thought about that. I think splitting the test_and_set_bit() in two (as with the cache flush) is sufficient. While there technically still is a race which could lead to user space tags being clobbered:

a) It's very odd for a VMM to be doing an mprotect() after the fact to add PROT_MTE, or to be sharing the memory with another process which sets PROT_MTE.

b) The window for the race is incredibly small and the VMM (generally) needs to be robust against the guest changing tags anyway.

But I'll add a comment here as well:

Quoted 7 lines

/*
* There is a potential race between sanitising the
* flags here and user space using mprotect() to add
* PROT_MTE to access the tags, however by splitting
* the test/set the only risk is user space tags
* being overwritten by the mte_clear_page_tags() call. */

I think (well, I haven't re-checked), an mprotect() in the VMM ends up calling set_pte_at_notify() which would call kvm_set_spte_gfn() and that will map the page in the guest. So the problem only appears between different VMMs sharing the same page. In principle they can be MAP_PRIVATE but they'd be CoW so the race wouldn't matter. So it's left with MAP_SHARED between multiple VMMs.

I think we should just state that this is unsafe and they can delete each-others tags. If we are really worried, we can export that lock you added in mte.c.

Signature · 1 line
Catalin
#23 lore raw

Re: [PATCH v12 4/8] arm64: kvm: Introduce MTE VM feature

Quoted 78 lines · Catalin Marinas
Quoted 69 lines · Steven Price
Quoted 21 lines · Catalin Marinas
Quoted 20 lines · Steven Price
arch/arm64/kvm/mmu.c +12 -0
index c5d1f3c87dbd..8660f6a03f51 100644
@@ -822,6 +822,31 @@ transparent_hugepage_adjust(struct kvm_memory_slot *memslot,
 	return PAGE_SIZE;
 }
 
+static int sanitise_mte_tags(struct kvm *kvm, unsigned long size,
+			     kvm_pfn_t pfn)
+{
+	if (kvm_has_mte(kvm)) {
+		/*
+		 * The page will be mapped in stage 2 as Normal Cacheable, so
+		 * the VM will be able to see the page's tags and therefore
+		 * they must be initialised first. If PG_mte_tagged is set,
+		 * tags have already been initialised.
+		 */
+		unsigned long i, nr_pages = size >> PAGE_SHIFT;
+		struct page *page = pfn_to_online_page(pfn);
Quoted 8 lines

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 839

Quoted 3 lines
arch/arm64/kvm/mmu.c +3 -0
+
+		if (!page)
+			return -EFAULT;

IIRC we ended up with pfn_to_online_page() to reject ZONE_DEVICE pages that may be mapped into a guest and we have no idea whether they support MTE. It may be worth adding a comment, otherwise, as Marc said, the page wouldn't disappear.

I'll add a comment.

Quoted 17 lines

arch/arm64/kvm/mmu.c transparent_hugepage_adjust(struct kvm_memory_slot *memslot, · line 843

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+
+		for (i = 0; i < nr_pages; i++, page++) {
+			if (!test_and_set_bit(PG_mte_tagged, &page->flags))
+				mte_clear_page_tags(page_address(page));

We started the page->flags thread and ended up fixing it for the host set_pte_at() as per the first patch:

https://lore.kernel.org/r/[email protected]

Now, can we have a race between the stage 2 kvm_set_spte_gfn() and a stage 1 set_pte_at()? Only the latter takes a lock. Or between two kvm_set_spte_gfn() in different VMs? I think in the above thread we concluded that there's only a problem if the page is shared between multiple VMMs (MAP_SHARED). How realistic is this and what's the workaround?

Quoted 4 lines

Either way, I think it's worth adding a comment here on the race on page->flags as it looks strange that here it's just a test_and_set_bit() while set_pte_at() uses a spinlock.

Very good point! I should have thought about that. I think splitting the test_and_set_bit() in two (as with the cache flush) is sufficient. While there technically still is a race which could lead to user space tags being clobbered:

a) It's very odd for a VMM to be doing an mprotect() after the fact to add PROT_MTE, or to be sharing the memory with another process which sets PROT_MTE.

b) The window for the race is incredibly small and the VMM (generally) needs to be robust against the guest changing tags anyway.

But I'll add a comment here as well:

Quoted 7 lines

/*
* There is a potential race between sanitising the
* flags here and user space using mprotect() to add
* PROT_MTE to access the tags, however by splitting
* the test/set the only risk is user space tags
* being overwritten by the mte_clear_page_tags() call. */

Quoted 6 lines

I think (well, I haven't re-checked), an mprotect() in the VMM ends up calling set_pte_at_notify() which would call kvm_set_spte_gfn() and that will map the page in the guest. So the problem only appears between different VMMs sharing the same page. In principle they can be MAP_PRIVATE but they'd be CoW so the race wouldn't matter. So it's left with MAP_SHARED between multiple VMMs.

mprotect.c only has a call to set_pte_at() not set_pte_at_notify(). And AFAICT the MMU notifiers are called to invalidate only in change_pmd_range(). So the stage 2 mappings would be invalidated rather than populated. However I believe this should cause synchronisation because of the KVM mmu_lock. So from my reading you are right an mprotect() can't race.

MAP_SHARED between multiple VMs is then the only potential problem.

Quoted 4 lines

I think we should just state that this is unsafe and they can delete each-others tags. If we are really worried, we can export that lock you added in mte.c.

I'll just update the comment for now.

Thanks,

Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#24 lore raw

PATCH v12 5/8 arm64: kvm: Save/restore MTE registers

In reply to Steven Price

Define the new system registers that MTE introduces and context switch them. The MTE feature is still hidden from the ID register as it isn't supported in a VM yet.

---
 arch/arm64/include/asm/kvm_host.h          |  6 ++
 arch/arm64/include/asm/kvm_mte.h           | 66 ++++++++++++++++++++++
 arch/arm64/include/asm/sysreg.h            |  3 +-
 arch/arm64/kernel/asm-offsets.c            |  3 +
 arch/arm64/kvm/hyp/entry.S                 |  7 +++
 arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 21 +++++++
 arch/arm64/kvm/sys_regs.c                  | 22 ++++++--
 7 files changed, 123 insertions(+), 5 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_mte.h
arch/arm64/include/asm/kvm_host.h +6 -0
index afaa5333f0e4..309e36cc1b42 100644
@@ -208,6 +208,12 @@ enum vcpu_sysreg {
 	CNTP_CVAL_EL0,
 	CNTP_CTL_EL0,
 
+	/* Memory Tagging Extension registers */
+	RGSR_EL1,	/* Random Allocation Tag Seed Register */
+	GCR_EL1,	/* Tag Control Register */
+	TFSR_EL1,	/* Tag Fault Status Register (EL1) */
+	TFSRE0_EL1,	/* Tag Fault Status Register (EL0) */
+
 	/* 32bit specific registers. Keep them at the end of the range */
 	DACR32_EL2,	/* Domain Access Control Register */
 	IFSR32_EL2,	/* Instruction Fault Status Register */
arch/arm64/include/asm/kvm_mte.h new file +66 -0

66 lines not on this page · raw message

arch/arm64/include/asm/sysreg.h +2 -1
index 65d15700a168..347ccac2341e 100644
@@ -651,7 +651,8 @@
 
 #define INIT_SCTLR_EL2_MMU_ON						\
 	(SCTLR_ELx_M  | SCTLR_ELx_C | SCTLR_ELx_SA | SCTLR_ELx_I |	\
-	 SCTLR_ELx_IESB | SCTLR_ELx_WXN | ENDIAN_SET_EL2 | SCTLR_EL2_RES1)
+	 SCTLR_ELx_IESB | SCTLR_ELx_WXN | ENDIAN_SET_EL2 |		\
+	 SCTLR_ELx_ITFSB | SCTLR_EL2_RES1)
 
 #define INIT_SCTLR_EL2_MMU_OFF \
 	(SCTLR_EL2_RES1 | ENDIAN_SET_EL2)
arch/arm64/kernel/asm-offsets.c +3 -0
index 0cb34ccb6e73..6b489a8462f0 100644
@@ -111,6 +111,9 @@ int main(void)
   DEFINE(VCPU_WORKAROUND_FLAGS,	offsetof(struct kvm_vcpu, arch.workaround_flags));
   DEFINE(VCPU_HCR_EL2,		offsetof(struct kvm_vcpu, arch.hcr_el2));
   DEFINE(CPU_USER_PT_REGS,	offsetof(struct kvm_cpu_context, regs));
+  DEFINE(CPU_RGSR_EL1,		offsetof(struct kvm_cpu_context, sys_regs[RGSR_EL1]));
+  DEFINE(CPU_GCR_EL1,		offsetof(struct kvm_cpu_context, sys_regs[GCR_EL1]));
+  DEFINE(CPU_TFSRE0_EL1,	offsetof(struct kvm_cpu_context, sys_regs[TFSRE0_EL1]));
   DEFINE(CPU_APIAKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APIAKEYLO_EL1]));
   DEFINE(CPU_APIBKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APIBKEYLO_EL1]));
   DEFINE(CPU_APDAKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APDAKEYLO_EL1]));
arch/arm64/kvm/hyp/entry.S +7 -0
index e831d3dfd50d..435346ea1504 100644
@@ -13,6 +13,7 @@
 #include <asm/kvm_arm.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_mmu.h>
+#include <asm/kvm_mte.h>
 #include <asm/kvm_ptrauth.h>
 
 	.text
@@ -51,6 +52,9 @@ alternative_else_nop_endif
 
 	add	x29, x0, #VCPU_CONTEXT
 
+	// mte_switch_to_guest(g_ctxt, h_ctxt, tmp1)
+	mte_switch_to_guest x29, x1, x2
+
 	// Macro ptrauth_switch_to_guest format:
 	// 	ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3)
 	// The below macro to restore guest keys is not implemented in C code
@@ -142,6 +146,9 @@ SYM_INNER_LABEL(__guest_exit, SYM_L_GLOBAL)
 	// when this feature is enabled for kernel code.
 	ptrauth_switch_to_hyp x1, x2, x3, x4, x5
 
+	// mte_switch_to_hyp(g_ctxt, h_ctxt, reg1)
+	mte_switch_to_hyp x1, x2, x3
+
 	// Restore hyp's sp_el0
 	restore_sp_el0 x2, x3
 
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +21 -0
index cce43bfe158f..de7e14c862e6 100644
@@ -14,6 +14,7 @@
 #include <asm/kvm_asm.h>
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_hyp.h>
+#include <asm/kvm_mmu.h>
 
 static inline void __sysreg_save_common_state(struct kvm_cpu_context *ctxt)
 {
@@ -26,6 +27,16 @@ static inline void __sysreg_save_user_state(struct kvm_cpu_context *ctxt)
 	ctxt_sys_reg(ctxt, TPIDRRO_EL0)	= read_sysreg(tpidrro_el0);
 }
 
+static inline bool ctxt_has_mte(struct kvm_cpu_context *ctxt)
+{
+	struct kvm_vcpu *vcpu = ctxt->__hyp_running_vcpu;
+
+	if (!vcpu)
+		vcpu = container_of(ctxt, struct kvm_vcpu, arch.ctxt);
+
+	return kvm_has_mte(kern_hyp_va(vcpu->kvm));
+}
+
 static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
 {
 	ctxt_sys_reg(ctxt, CSSELR_EL1)	= read_sysreg(csselr_el1);
@@ -46,6 +57,11 @@ static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
 	ctxt_sys_reg(ctxt, PAR_EL1)	= read_sysreg_par();
 	ctxt_sys_reg(ctxt, TPIDR_EL1)	= read_sysreg(tpidr_el1);
 
+	if (ctxt_has_mte(ctxt)) {
+		ctxt_sys_reg(ctxt, TFSR_EL1) = read_sysreg_el1(SYS_TFSR);
+		ctxt_sys_reg(ctxt, TFSRE0_EL1) = read_sysreg_s(SYS_TFSRE0_EL1);
+	}
+
 	ctxt_sys_reg(ctxt, SP_EL1)	= read_sysreg(sp_el1);
 	ctxt_sys_reg(ctxt, ELR_EL1)	= read_sysreg_el1(SYS_ELR);
 	ctxt_sys_reg(ctxt, SPSR_EL1)	= read_sysreg_el1(SYS_SPSR);
@@ -107,6 +123,11 @@ static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt)
 	write_sysreg(ctxt_sys_reg(ctxt, PAR_EL1),	par_el1);
 	write_sysreg(ctxt_sys_reg(ctxt, TPIDR_EL1),	tpidr_el1);
 
+	if (ctxt_has_mte(ctxt)) {
+		write_sysreg_el1(ctxt_sys_reg(ctxt, TFSR_EL1), SYS_TFSR);
+		write_sysreg_s(ctxt_sys_reg(ctxt, TFSRE0_EL1), SYS_TFSRE0_EL1);
+	}
+
 	if (!has_vhe() &&
 	    cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT) &&
 	    ctxt->__hyp_running_vcpu) {
arch/arm64/kvm/sys_regs.c +18 -4
index 24a844cb79ca..88adbc2286f2 100644
@@ -1305,6 +1305,20 @@ static bool access_ccsidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 	return true;
 }
 
+static unsigned int mte_visibility(const struct kvm_vcpu *vcpu,
+				   const struct sys_reg_desc *rd)
+{
+	return REG_HIDDEN;
+}
+
+#define MTE_REG(name) {				\
+	SYS_DESC(SYS_##name),			\
+	.access = undef_access,			\
+	.reset = reset_unknown,			\
+	.reg = name,				\
+	.visibility = mte_visibility,		\
+}
+
 /* sys_reg_desc initialiser for known cpufeature ID registers */
 #define ID_SANITISED(name) {			\
 	SYS_DESC(SYS_##name),			\
@@ -1473,8 +1487,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ SYS_DESC(SYS_ACTLR_EL1), access_actlr, reset_actlr, ACTLR_EL1 },
 	{ SYS_DESC(SYS_CPACR_EL1), NULL, reset_val, CPACR_EL1, 0 },
 
-	{ SYS_DESC(SYS_RGSR_EL1), undef_access },
-	{ SYS_DESC(SYS_GCR_EL1), undef_access },
+	MTE_REG(RGSR_EL1),
+	MTE_REG(GCR_EL1),
 
 	{ SYS_DESC(SYS_ZCR_EL1), NULL, reset_val, ZCR_EL1, 0, .visibility = sve_visibility },
 	{ SYS_DESC(SYS_TRFCR_EL1), undef_access },
@@ -1501,8 +1515,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ SYS_DESC(SYS_ERXMISC0_EL1), trap_raz_wi },
 	{ SYS_DESC(SYS_ERXMISC1_EL1), trap_raz_wi },
 
-	{ SYS_DESC(SYS_TFSR_EL1), undef_access },
-	{ SYS_DESC(SYS_TFSRE0_EL1), undef_access },
+	MTE_REG(TFSR_EL1),
+	MTE_REG(TFSRE0_EL1),
 
 	{ SYS_DESC(SYS_FAR_EL1), access_vm_reg, reset_unknown, FAR_EL1 },
 	{ SYS_DESC(SYS_PAR_EL1), NULL, reset_unknown, PAR_EL1 },
Signature · 1 line
2.20.1
maintainer
#25 lore raw

Re: [PATCH v12 5/8] arm64: kvm: Save/restore MTE registers

Quoted 81 lines · Steven Price

Define the new system registers that MTE introduces and context switch them. The MTE feature is still hidden from the ID register as it isn't supported in a VM yet.

---
 arch/arm64/include/asm/kvm_host.h          |  6 ++
 arch/arm64/include/asm/kvm_mte.h           | 66 ++++++++++++++++++++++
 arch/arm64/include/asm/sysreg.h            |  3 +-
 arch/arm64/kernel/asm-offsets.c            |  3 +
 arch/arm64/kvm/hyp/entry.S                 |  7 +++
 arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 21 +++++++
 arch/arm64/kvm/sys_regs.c                  | 22 ++++++--
 7 files changed, 123 insertions(+), 5 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_mte.h
arch/arm64/include/asm/kvm_host.h +6 -0
index afaa5333f0e4..309e36cc1b42 100644
@@ -208,6 +208,12 @@ enum vcpu_sysreg {
 	CNTP_CVAL_EL0,
 	CNTP_CTL_EL0,
 
+	/* Memory Tagging Extension registers */
+	RGSR_EL1,	/* Random Allocation Tag Seed Register */
+	GCR_EL1,	/* Tag Control Register */
+	TFSR_EL1,	/* Tag Fault Status Register (EL1) */
+	TFSRE0_EL1,	/* Tag Fault Status Register (EL0) */
+
 	/* 32bit specific registers. Keep them at the end of the range */
 	DACR32_EL2,	/* Domain Access Control Register */
 	IFSR32_EL2,	/* Instruction Fault Status Register */
arch/arm64/include/asm/kvm_mte.h new file +41 -0
new file mode 100644
index 000000000000..6541c7d6ce06
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020 ARM Ltd.
+ */
+#ifndef __ASM_KVM_MTE_H
+#define __ASM_KVM_MTE_H
+
+#ifdef __ASSEMBLY__
+
+#include <asm/sysreg.h>
+
+#ifdef CONFIG_ARM64_MTE
+
+.macro mte_switch_to_guest g_ctxt, h_ctxt, reg1
+alternative_if_not ARM64_MTE
+	b	.L__skip_switch\@
+alternative_else_nop_endif
+	mrs	\reg1, hcr_el2
+	and	\reg1, \reg1, #(HCR_ATA)
+	cbz	\reg1, .L__skip_switch\@
+
+	mrs_s	\reg1, SYS_RGSR_EL1
+	str	\reg1, [\h_ctxt, #CPU_RGSR_EL1]
+	mrs_s	\reg1, SYS_GCR_EL1
+	str	\reg1, [\h_ctxt, #CPU_GCR_EL1]
+
+	ldr	\reg1, [\g_ctxt, #CPU_RGSR_EL1]
+	msr_s	SYS_RGSR_EL1, \reg1
+	ldr	\reg1, [\g_ctxt, #CPU_GCR_EL1]
+	msr_s	SYS_GCR_EL1, \reg1
+
+.L__skip_switch\@:
+.endm
+
+.macro mte_switch_to_hyp g_ctxt, h_ctxt, reg1
+alternative_if_not ARM64_MTE
+	b	.L__skip_switch\@
+alternative_else_nop_endif
+	mrs	\reg1, hcr_el2
+	and	\reg1, \reg1, #(HCR_ATA)
+	cbz	\reg1, .L__skip_switch\@

arch/arm64/include/asm/kvm_mte.h line 51

Quoted 10 lines
arch/arm64/include/asm/kvm_mte.h new file +10 -0
+
+	mrs_s	\reg1, SYS_RGSR_EL1
+	str	\reg1, [\g_ctxt, #CPU_RGSR_EL1]
+	mrs_s	\reg1, SYS_GCR_EL1
+	str	\reg1, [\g_ctxt, #CPU_GCR_EL1]
+
+	ldr	\reg1, [\h_ctxt, #CPU_RGSR_EL1]
+	msr_s	SYS_RGSR_EL1, \reg1
+	ldr	\reg1, [\h_ctxt, #CPU_GCR_EL1]
+	msr_s	SYS_GCR_EL1, \reg1

What is the rational for not having any synchronisation here? It is quite uncommon to allocate memory at EL2, but VHE can perform all kind of tricks.

Quoted 34 lines
arch/arm64/include/asm/kvm_mte.h new file +15 -0
+
+.L__skip_switch\@:
+.endm
+
+#else /* CONFIG_ARM64_MTE */
+
+.macro mte_switch_to_guest g_ctxt, h_ctxt, reg1
+.endm
+
+.macro mte_switch_to_hyp g_ctxt, h_ctxt, reg1
+.endm
+
+#endif /* CONFIG_ARM64_MTE */
+#endif /* __ASSEMBLY__ */
+#endif /* __ASM_KVM_MTE_H */
arch/arm64/include/asm/sysreg.h +2 -1
index 65d15700a168..347ccac2341e 100644
@@ -651,7 +651,8 @@
 
 #define INIT_SCTLR_EL2_MMU_ON						\
 	(SCTLR_ELx_M  | SCTLR_ELx_C | SCTLR_ELx_SA | SCTLR_ELx_I |	\
-	 SCTLR_ELx_IESB | SCTLR_ELx_WXN | ENDIAN_SET_EL2 | SCTLR_EL2_RES1)
+	 SCTLR_ELx_IESB | SCTLR_ELx_WXN | ENDIAN_SET_EL2 |		\
+	 SCTLR_ELx_ITFSB | SCTLR_EL2_RES1)
 
 #define INIT_SCTLR_EL2_MMU_OFF \
 	(SCTLR_EL2_RES1 | ENDIAN_SET_EL2)
arch/arm64/kernel/asm-offsets.c +0 -0
index 0cb34ccb6e73..6b489a8462f0 100644

arch/arm64/kernel/asm-offsets.c int main(void) · line 116

Quoted 6 lines
arch/arm64/kernel/asm-offsets.c +3 -0
@@ -111,6 +111,9 @@ int main(void)
   DEFINE(VCPU_WORKAROUND_FLAGS,	offsetof(struct kvm_vcpu, arch.workaround_flags));
   DEFINE(VCPU_HCR_EL2,		offsetof(struct kvm_vcpu, arch.hcr_el2));
   DEFINE(CPU_USER_PT_REGS,	offsetof(struct kvm_cpu_context, regs));
+  DEFINE(CPU_RGSR_EL1,		offsetof(struct kvm_cpu_context, sys_regs[RGSR_EL1]));
+  DEFINE(CPU_GCR_EL1,		offsetof(struct kvm_cpu_context, sys_regs[GCR_EL1]));
+  DEFINE(CPU_TFSRE0_EL1,	offsetof(struct kvm_cpu_context, sys_regs[TFSRE0_EL1]));

TFSRE0_EL1 is never accessed from assembly code. Leftover from a previous version?

Quoted 65 lines
arch/arm64/kernel/asm-offsets.c +0 -0
   DEFINE(CPU_APIAKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APIAKEYLO_EL1]));
   DEFINE(CPU_APIBKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APIBKEYLO_EL1]));
   DEFINE(CPU_APDAKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APDAKEYLO_EL1]));
arch/arm64/kvm/hyp/entry.S +7 -0
index e831d3dfd50d..435346ea1504 100644
@@ -13,6 +13,7 @@
 #include <asm/kvm_arm.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_mmu.h>
+#include <asm/kvm_mte.h>
 #include <asm/kvm_ptrauth.h>
 
 	.text
@@ -51,6 +52,9 @@ alternative_else_nop_endif
 
 	add	x29, x0, #VCPU_CONTEXT
 
+	// mte_switch_to_guest(g_ctxt, h_ctxt, tmp1)
+	mte_switch_to_guest x29, x1, x2
+
 	// Macro ptrauth_switch_to_guest format:
 	// 	ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3)
 	// The below macro to restore guest keys is not implemented in C code
@@ -142,6 +146,9 @@ SYM_INNER_LABEL(__guest_exit, SYM_L_GLOBAL)
 	// when this feature is enabled for kernel code.
 	ptrauth_switch_to_hyp x1, x2, x3, x4, x5
 
+	// mte_switch_to_hyp(g_ctxt, h_ctxt, reg1)
+	mte_switch_to_hyp x1, x2, x3
+
 	// Restore hyp's sp_el0
 	restore_sp_el0 x2, x3
 
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +11 -0
index cce43bfe158f..de7e14c862e6 100644
@@ -14,6 +14,7 @@
 #include <asm/kvm_asm.h>
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_hyp.h>
+#include <asm/kvm_mmu.h>
 
 static inline void __sysreg_save_common_state(struct kvm_cpu_context *ctxt)
 {
@@ -26,6 +27,16 @@ static inline void __sysreg_save_user_state(struct kvm_cpu_context *ctxt)
 	ctxt_sys_reg(ctxt, TPIDRRO_EL0)	= read_sysreg(tpidrro_el0);
 }
 
+static inline bool ctxt_has_mte(struct kvm_cpu_context *ctxt)
+{
+	struct kvm_vcpu *vcpu = ctxt->__hyp_running_vcpu;
+
+	if (!vcpu)
+		vcpu = container_of(ctxt, struct kvm_vcpu, arch.ctxt);
+
+	return kvm_has_mte(kern_hyp_va(vcpu->kvm));
+}
+
 static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
 {
 	ctxt_sys_reg(ctxt, CSSELR_EL1)	= read_sysreg(csselr_el1);

arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt) · line 63

Quoted 7 lines
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +4 -0
@@ -46,6 +57,11 @@ static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
 	ctxt_sys_reg(ctxt, PAR_EL1)	= read_sysreg_par();
 	ctxt_sys_reg(ctxt, TPIDR_EL1)	= read_sysreg(tpidr_el1);
 
+	if (ctxt_has_mte(ctxt)) {
+		ctxt_sys_reg(ctxt, TFSR_EL1) = read_sysreg_el1(SYS_TFSR);
+		ctxt_sys_reg(ctxt, TFSRE0_EL1) = read_sysreg_s(SYS_TFSRE0_EL1);
+	}

I remember suggesting that this is slightly heavier than necessary.

On nVHE, TFSRE0_EL1 could be moved to load/put, as we never run userspace with a vcpu loaded. The same holds of course for VHE, but we also can move TFSR_EL1 to load/put, as the host uses TFSR_EL2.

Do you see any issue with that?

Quoted 53 lines
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +6 -0
+
 	ctxt_sys_reg(ctxt, SP_EL1)	= read_sysreg(sp_el1);
 	ctxt_sys_reg(ctxt, ELR_EL1)	= read_sysreg_el1(SYS_ELR);
 	ctxt_sys_reg(ctxt, SPSR_EL1)	= read_sysreg_el1(SYS_SPSR);
@@ -107,6 +123,11 @@ static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt)
 	write_sysreg(ctxt_sys_reg(ctxt, PAR_EL1),	par_el1);
 	write_sysreg(ctxt_sys_reg(ctxt, TPIDR_EL1),	tpidr_el1);
 
+	if (ctxt_has_mte(ctxt)) {
+		write_sysreg_el1(ctxt_sys_reg(ctxt, TFSR_EL1), SYS_TFSR);
+		write_sysreg_s(ctxt_sys_reg(ctxt, TFSRE0_EL1), SYS_TFSRE0_EL1);
+	}
+
 	if (!has_vhe() &&
 	    cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT) &&
 	    ctxt->__hyp_running_vcpu) {
arch/arm64/kvm/sys_regs.c +16 -2
index 24a844cb79ca..88adbc2286f2 100644
@@ -1305,6 +1305,20 @@ static bool access_ccsidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 	return true;
 }
 
+static unsigned int mte_visibility(const struct kvm_vcpu *vcpu,
+				   const struct sys_reg_desc *rd)
+{
+	return REG_HIDDEN;
+}
+
+#define MTE_REG(name) {				\
+	SYS_DESC(SYS_##name),			\
+	.access = undef_access,			\
+	.reset = reset_unknown,			\
+	.reg = name,				\
+	.visibility = mte_visibility,		\
+}
+
 /* sys_reg_desc initialiser for known cpufeature ID registers */
 #define ID_SANITISED(name) {			\
 	SYS_DESC(SYS_##name),			\
@@ -1473,8 +1487,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ SYS_DESC(SYS_ACTLR_EL1), access_actlr, reset_actlr, ACTLR_EL1 },
 	{ SYS_DESC(SYS_CPACR_EL1), NULL, reset_val, CPACR_EL1, 0 },
 
-	{ SYS_DESC(SYS_RGSR_EL1), undef_access },
-	{ SYS_DESC(SYS_GCR_EL1), undef_access },
+	MTE_REG(RGSR_EL1),
+	MTE_REG(GCR_EL1),
 
 	{ SYS_DESC(SYS_ZCR_EL1), NULL, reset_val, ZCR_EL1, 0, .visibility = sve_visibility },
 	{ SYS_DESC(SYS_TRFCR_EL1), undef_access },

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 1522

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +2 -2
@@ -1501,8 +1515,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ SYS_DESC(SYS_ERXMISC0_EL1), trap_raz_wi },
 	{ SYS_DESC(SYS_ERXMISC1_EL1), trap_raz_wi },
 
-	{ SYS_DESC(SYS_TFSR_EL1), undef_access },
-	{ SYS_DESC(SYS_TFSRE0_EL1), undef_access },
+	MTE_REG(TFSR_EL1),
+	MTE_REG(TFSRE0_EL1),
 
 	{ SYS_DESC(SYS_FAR_EL1), access_vm_reg, reset_unknown, FAR_EL1 },
 	{ SYS_DESC(SYS_PAR_EL1), NULL, reset_unknown, PAR_EL1 },

Thanks,

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

Re: [PATCH v12 5/8] arm64: kvm: Save/restore MTE registers

Quoted 89 lines · Marc Zyngier
Quoted 87 lines · Steven Price

Define the new system registers that MTE introduces and context switch them. The MTE feature is still hidden from the ID register as it isn't supported in a VM yet.

---
 arch/arm64/include/asm/kvm_host.h          |  6 ++
 arch/arm64/include/asm/kvm_mte.h           | 66 ++++++++++++++++++++++
 arch/arm64/include/asm/sysreg.h            |  3 +-
 arch/arm64/kernel/asm-offsets.c            |  3 +
 arch/arm64/kvm/hyp/entry.S                 |  7 +++
 arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 21 +++++++
 arch/arm64/kvm/sys_regs.c                  | 22 ++++++--
 7 files changed, 123 insertions(+), 5 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_mte.h
arch/arm64/include/asm/kvm_host.h +6 -0
index afaa5333f0e4..309e36cc1b42 100644
@@ -208,6 +208,12 @@ enum vcpu_sysreg {
 	CNTP_CVAL_EL0,
 	CNTP_CTL_EL0,
 
+	/* Memory Tagging Extension registers */
+	RGSR_EL1,	/* Random Allocation Tag Seed Register */
+	GCR_EL1,	/* Tag Control Register */
+	TFSR_EL1,	/* Tag Fault Status Register (EL1) */
+	TFSRE0_EL1,	/* Tag Fault Status Register (EL0) */
+
 	/* 32bit specific registers. Keep them at the end of the range */
 	DACR32_EL2,	/* Domain Access Control Register */
 	IFSR32_EL2,	/* Instruction Fault Status Register */
arch/arm64/include/asm/kvm_mte.h new file +47 -0
new file mode 100644
index 000000000000..6541c7d6ce06
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020 ARM Ltd.
+ */
+#ifndef __ASM_KVM_MTE_H
+#define __ASM_KVM_MTE_H
+
+#ifdef __ASSEMBLY__
+
+#include <asm/sysreg.h>
+
+#ifdef CONFIG_ARM64_MTE
+
+.macro mte_switch_to_guest g_ctxt, h_ctxt, reg1
+alternative_if_not ARM64_MTE
+	b	.L__skip_switch\@
+alternative_else_nop_endif
+	mrs	\reg1, hcr_el2
+	and	\reg1, \reg1, #(HCR_ATA)
+	cbz	\reg1, .L__skip_switch\@
+
+	mrs_s	\reg1, SYS_RGSR_EL1
+	str	\reg1, [\h_ctxt, #CPU_RGSR_EL1]
+	mrs_s	\reg1, SYS_GCR_EL1
+	str	\reg1, [\h_ctxt, #CPU_GCR_EL1]
+
+	ldr	\reg1, [\g_ctxt, #CPU_RGSR_EL1]
+	msr_s	SYS_RGSR_EL1, \reg1
+	ldr	\reg1, [\g_ctxt, #CPU_GCR_EL1]
+	msr_s	SYS_GCR_EL1, \reg1
+
+.L__skip_switch\@:
+.endm
+
+.macro mte_switch_to_hyp g_ctxt, h_ctxt, reg1
+alternative_if_not ARM64_MTE
+	b	.L__skip_switch\@
+alternative_else_nop_endif
+	mrs	\reg1, hcr_el2
+	and	\reg1, \reg1, #(HCR_ATA)
+	cbz	\reg1, .L__skip_switch\@
+
+	mrs_s	\reg1, SYS_RGSR_EL1
+	str	\reg1, [\g_ctxt, #CPU_RGSR_EL1]
+	mrs_s	\reg1, SYS_GCR_EL1
+	str	\reg1, [\g_ctxt, #CPU_GCR_EL1]
+
Quoted 8 lines

arch/arm64/include/asm/kvm_mte.h line 51

Quoted 4 lines
arch/arm64/include/asm/kvm_mte.h new file +4 -0
+	ldr	\reg1, [\h_ctxt, #CPU_RGSR_EL1]
+	msr_s	SYS_RGSR_EL1, \reg1
+	ldr	\reg1, [\h_ctxt, #CPU_GCR_EL1]
+	msr_s	SYS_GCR_EL1, \reg1

What is the rational for not having any synchronisation here? It is quite uncommon to allocate memory at EL2, but VHE can perform all kind of tricks.

I don't follow. This is part of the __guest_exit path and there's an ISB at the end of that - is that not sufficient? I don't see any possibility for allocating memory before that. What am I missing?

Quoted 27 lines
Quoted 27 lines
arch/arm64/include/asm/kvm_mte.h new file +15 -0
+
+.L__skip_switch\@:
+.endm
+
+#else /* CONFIG_ARM64_MTE */
+
+.macro mte_switch_to_guest g_ctxt, h_ctxt, reg1
+.endm
+
+.macro mte_switch_to_hyp g_ctxt, h_ctxt, reg1
+.endm
+
+#endif /* CONFIG_ARM64_MTE */
+#endif /* __ASSEMBLY__ */
+#endif /* __ASM_KVM_MTE_H */
arch/arm64/include/asm/sysreg.h +2 -1
index 65d15700a168..347ccac2341e 100644
@@ -651,7 +651,8 @@
 
 #define INIT_SCTLR_EL2_MMU_ON						\
 	(SCTLR_ELx_M  | SCTLR_ELx_C | SCTLR_ELx_SA | SCTLR_ELx_I |	\
-	 SCTLR_ELx_IESB | SCTLR_ELx_WXN | ENDIAN_SET_EL2 | SCTLR_EL2_RES1)
+	 SCTLR_ELx_IESB | SCTLR_ELx_WXN | ENDIAN_SET_EL2 |		\
+	 SCTLR_ELx_ITFSB | SCTLR_EL2_RES1)
 
Quoted 16 lines

arch/arm64/kernel/asm-offsets.c int main(void) · line 116

Quoted 13 lines
arch/arm64/include/asm/sysreg.h +0 -0
 #define INIT_SCTLR_EL2_MMU_OFF \
 	(SCTLR_EL2_RES1 | ENDIAN_SET_EL2)
arch/arm64/kernel/asm-offsets.c +3 -0
index 0cb34ccb6e73..6b489a8462f0 100644
@@ -111,6 +111,9 @@ int main(void)
   DEFINE(VCPU_WORKAROUND_FLAGS,	offsetof(struct kvm_vcpu, arch.workaround_flags));
   DEFINE(VCPU_HCR_EL2,		offsetof(struct kvm_vcpu, arch.hcr_el2));
   DEFINE(CPU_USER_PT_REGS,	offsetof(struct kvm_cpu_context, regs));
+  DEFINE(CPU_RGSR_EL1,		offsetof(struct kvm_cpu_context, sys_regs[RGSR_EL1]));
+  DEFINE(CPU_GCR_EL1,		offsetof(struct kvm_cpu_context, sys_regs[GCR_EL1]));
+  DEFINE(CPU_TFSRE0_EL1,	offsetof(struct kvm_cpu_context, sys_regs[TFSRE0_EL1]));

TFSRE0_EL1 is never accessed from assembly code. Leftover from a previous version?

Indeed, I will drop it.

Quoted 75 lines
Quoted 65 lines
arch/arm64/kernel/asm-offsets.c +0 -0
   DEFINE(CPU_APIAKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APIAKEYLO_EL1]));
   DEFINE(CPU_APIBKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APIBKEYLO_EL1]));
   DEFINE(CPU_APDAKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APDAKEYLO_EL1]));
arch/arm64/kvm/hyp/entry.S +7 -0
index e831d3dfd50d..435346ea1504 100644
@@ -13,6 +13,7 @@
 #include <asm/kvm_arm.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_mmu.h>
+#include <asm/kvm_mte.h>
 #include <asm/kvm_ptrauth.h>
 
 	.text
@@ -51,6 +52,9 @@ alternative_else_nop_endif
 
 	add	x29, x0, #VCPU_CONTEXT
 
+	// mte_switch_to_guest(g_ctxt, h_ctxt, tmp1)
+	mte_switch_to_guest x29, x1, x2
+
 	// Macro ptrauth_switch_to_guest format:
 	// 	ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3)
 	// The below macro to restore guest keys is not implemented in C code
@@ -142,6 +146,9 @@ SYM_INNER_LABEL(__guest_exit, SYM_L_GLOBAL)
 	// when this feature is enabled for kernel code.
 	ptrauth_switch_to_hyp x1, x2, x3, x4, x5
 
+	// mte_switch_to_hyp(g_ctxt, h_ctxt, reg1)
+	mte_switch_to_hyp x1, x2, x3
+
 	// Restore hyp's sp_el0
 	restore_sp_el0 x2, x3
 
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +11 -0
index cce43bfe158f..de7e14c862e6 100644
@@ -14,6 +14,7 @@
 #include <asm/kvm_asm.h>
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_hyp.h>
+#include <asm/kvm_mmu.h>
 
 static inline void __sysreg_save_common_state(struct kvm_cpu_context *ctxt)
 {
@@ -26,6 +27,16 @@ static inline void __sysreg_save_user_state(struct kvm_cpu_context *ctxt)
 	ctxt_sys_reg(ctxt, TPIDRRO_EL0)	= read_sysreg(tpidrro_el0);
 }
 
+static inline bool ctxt_has_mte(struct kvm_cpu_context *ctxt)
+{
+	struct kvm_vcpu *vcpu = ctxt->__hyp_running_vcpu;
+
+	if (!vcpu)
+		vcpu = container_of(ctxt, struct kvm_vcpu, arch.ctxt);
+
+	return kvm_has_mte(kern_hyp_va(vcpu->kvm));
+}
+
 static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
 {
 	ctxt_sys_reg(ctxt, CSSELR_EL1)	= read_sysreg(csselr_el1);

arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt) · line 63

Quoted 7 lines
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +4 -0
@@ -46,6 +57,11 @@ static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
 	ctxt_sys_reg(ctxt, PAR_EL1)	= read_sysreg_par();
 	ctxt_sys_reg(ctxt, TPIDR_EL1)	= read_sysreg(tpidr_el1);
 
+	if (ctxt_has_mte(ctxt)) {
+		ctxt_sys_reg(ctxt, TFSR_EL1) = read_sysreg_el1(SYS_TFSR);
+		ctxt_sys_reg(ctxt, TFSRE0_EL1) = read_sysreg_s(SYS_TFSRE0_EL1);
+	}

I remember suggesting that this is slightly heavier than necessary.

Quoted 5 lines

On nVHE, TFSRE0_EL1 could be moved to load/put, as we never run userspace with a vcpu loaded. The same holds of course for VHE, but we also can move TFSR_EL1 to load/put, as the host uses TFSR_EL2.

Do you see any issue with that?

The comment[1] I made before was:

For TFSR_EL1 + VHE I believe it is synchronised only on vcpu_load/put - __sysreg_save_el1_state() is called from kvm_vcpu_load_sysregs_vhe().

TFSRE0_EL1 potentially could be improved. I have to admit I was unsure if it should be in __sysreg_save_user_state() instead. However AFAICT that is called at the same time as __sysreg_save_el1_state() and there's no optimisation for nVHE. And given it's an _EL1 register this seemed like the logic place.

Am I missing something here? Potentially there are other registers to be optimised (TPIDRRO_EL0 looks like a possiblity), but IMHO that doesn't belong in this series.

For VHE TFSR_EL1 is already only saved/restored on load/put (__sysreg_save_el1_state() is called from kvm_vcpu_put_sysregs_vhe()).

TFSRE0_EL1 could be moved, but I'm not sure where it should live as I mentioned above.

[1] https://lore.kernel.org/kvmarm/[email protected]/

Thanks,

Steve

Quoted 68 lines
Quoted 53 lines
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +6 -0
+
 	ctxt_sys_reg(ctxt, SP_EL1)	= read_sysreg(sp_el1);
 	ctxt_sys_reg(ctxt, ELR_EL1)	= read_sysreg_el1(SYS_ELR);
 	ctxt_sys_reg(ctxt, SPSR_EL1)	= read_sysreg_el1(SYS_SPSR);
@@ -107,6 +123,11 @@ static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt)
 	write_sysreg(ctxt_sys_reg(ctxt, PAR_EL1),	par_el1);
 	write_sysreg(ctxt_sys_reg(ctxt, TPIDR_EL1),	tpidr_el1);
 
+	if (ctxt_has_mte(ctxt)) {
+		write_sysreg_el1(ctxt_sys_reg(ctxt, TFSR_EL1), SYS_TFSR);
+		write_sysreg_s(ctxt_sys_reg(ctxt, TFSRE0_EL1), SYS_TFSRE0_EL1);
+	}
+
 	if (!has_vhe() &&
 	    cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT) &&
 	    ctxt->__hyp_running_vcpu) {
arch/arm64/kvm/sys_regs.c +16 -2
index 24a844cb79ca..88adbc2286f2 100644
@@ -1305,6 +1305,20 @@ static bool access_ccsidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 	return true;
 }
 
+static unsigned int mte_visibility(const struct kvm_vcpu *vcpu,
+				   const struct sys_reg_desc *rd)
+{
+	return REG_HIDDEN;
+}
+
+#define MTE_REG(name) {				\
+	SYS_DESC(SYS_##name),			\
+	.access = undef_access,			\
+	.reset = reset_unknown,			\
+	.reg = name,				\
+	.visibility = mte_visibility,		\
+}
+
 /* sys_reg_desc initialiser for known cpufeature ID registers */
 #define ID_SANITISED(name) {			\
 	SYS_DESC(SYS_##name),			\
@@ -1473,8 +1487,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ SYS_DESC(SYS_ACTLR_EL1), access_actlr, reset_actlr, ACTLR_EL1 },
 	{ SYS_DESC(SYS_CPACR_EL1), NULL, reset_val, CPACR_EL1, 0 },
 
-	{ SYS_DESC(SYS_RGSR_EL1), undef_access },
-	{ SYS_DESC(SYS_GCR_EL1), undef_access },
+	MTE_REG(RGSR_EL1),
+	MTE_REG(GCR_EL1),
 
 	{ SYS_DESC(SYS_ZCR_EL1), NULL, reset_val, ZCR_EL1, 0, .visibility = sve_visibility },
 	{ SYS_DESC(SYS_TRFCR_EL1), undef_access },

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 1522

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +2 -2
@@ -1501,8 +1515,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ SYS_DESC(SYS_ERXMISC0_EL1), trap_raz_wi },
 	{ SYS_DESC(SYS_ERXMISC1_EL1), trap_raz_wi },
 
-	{ SYS_DESC(SYS_TFSR_EL1), undef_access },
-	{ SYS_DESC(SYS_TFSRE0_EL1), undef_access },
+	MTE_REG(TFSR_EL1),
+	MTE_REG(TFSRE0_EL1),
 
 	{ SYS_DESC(SYS_FAR_EL1), access_vm_reg, reset_unknown, FAR_EL1 },
 	{ SYS_DESC(SYS_PAR_EL1), NULL, reset_unknown, PAR_EL1 },

Thanks,

	M.
maintainer
#27 lore raw

Re: [PATCH v12 5/8] arm64: kvm: Save/restore MTE registers

Quoted 100 lines · Steven Price
Quoted 89 lines · Marc Zyngier
Quoted 87 lines · Steven Price

Define the new system registers that MTE introduces and context switch them. The MTE feature is still hidden from the ID register as it isn't supported in a VM yet.

---
 arch/arm64/include/asm/kvm_host.h          |  6 ++
 arch/arm64/include/asm/kvm_mte.h           | 66 ++++++++++++++++++++++
 arch/arm64/include/asm/sysreg.h            |  3 +-
 arch/arm64/kernel/asm-offsets.c            |  3 +
 arch/arm64/kvm/hyp/entry.S                 |  7 +++
 arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 21 +++++++
 arch/arm64/kvm/sys_regs.c                  | 22 ++++++--
 7 files changed, 123 insertions(+), 5 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_mte.h
arch/arm64/include/asm/kvm_host.h +6 -0
index afaa5333f0e4..309e36cc1b42 100644
@@ -208,6 +208,12 @@ enum vcpu_sysreg {
 	CNTP_CVAL_EL0,
 	CNTP_CTL_EL0,
 
+	/* Memory Tagging Extension registers */
+	RGSR_EL1,	/* Random Allocation Tag Seed Register */
+	GCR_EL1,	/* Tag Control Register */
+	TFSR_EL1,	/* Tag Fault Status Register (EL1) */
+	TFSRE0_EL1,	/* Tag Fault Status Register (EL0) */
+
 	/* 32bit specific registers. Keep them at the end of the range */
 	DACR32_EL2,	/* Domain Access Control Register */
 	IFSR32_EL2,	/* Instruction Fault Status Register */
arch/arm64/include/asm/kvm_mte.h new file +47 -0
new file mode 100644
index 000000000000..6541c7d6ce06
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020 ARM Ltd.
+ */
+#ifndef __ASM_KVM_MTE_H
+#define __ASM_KVM_MTE_H
+
+#ifdef __ASSEMBLY__
+
+#include <asm/sysreg.h>
+
+#ifdef CONFIG_ARM64_MTE
+
+.macro mte_switch_to_guest g_ctxt, h_ctxt, reg1
+alternative_if_not ARM64_MTE
+	b	.L__skip_switch\@
+alternative_else_nop_endif
+	mrs	\reg1, hcr_el2
+	and	\reg1, \reg1, #(HCR_ATA)
+	cbz	\reg1, .L__skip_switch\@
+
+	mrs_s	\reg1, SYS_RGSR_EL1
+	str	\reg1, [\h_ctxt, #CPU_RGSR_EL1]
+	mrs_s	\reg1, SYS_GCR_EL1
+	str	\reg1, [\h_ctxt, #CPU_GCR_EL1]
+
+	ldr	\reg1, [\g_ctxt, #CPU_RGSR_EL1]
+	msr_s	SYS_RGSR_EL1, \reg1
+	ldr	\reg1, [\g_ctxt, #CPU_GCR_EL1]
+	msr_s	SYS_GCR_EL1, \reg1
+
+.L__skip_switch\@:
+.endm
+
+.macro mte_switch_to_hyp g_ctxt, h_ctxt, reg1
+alternative_if_not ARM64_MTE
+	b	.L__skip_switch\@
+alternative_else_nop_endif
+	mrs	\reg1, hcr_el2
+	and	\reg1, \reg1, #(HCR_ATA)
+	cbz	\reg1, .L__skip_switch\@
+
+	mrs_s	\reg1, SYS_RGSR_EL1
+	str	\reg1, [\g_ctxt, #CPU_RGSR_EL1]
+	mrs_s	\reg1, SYS_GCR_EL1
+	str	\reg1, [\g_ctxt, #CPU_GCR_EL1]
+
Quoted 8 lines

arch/arm64/include/asm/kvm_mte.h line 51

Quoted 4 lines
arch/arm64/include/asm/kvm_mte.h new file +4 -0
+	ldr	\reg1, [\h_ctxt, #CPU_RGSR_EL1]
+	msr_s	SYS_RGSR_EL1, \reg1
+	ldr	\reg1, [\h_ctxt, #CPU_GCR_EL1]
+	msr_s	SYS_GCR_EL1, \reg1

What is the rational for not having any synchronisation here? It is quite uncommon to allocate memory at EL2, but VHE can perform all kind of tricks.

Quoted 3 lines

I don't follow. This is part of the __guest_exit path and there's an ISB at the end of that - is that not sufficient? I don't see any possibility for allocating memory before that. What am I missing?

Which ISB? We have a few in the SError handling code, but that's conditioned on not having RAS. With any RAS-enabled CPU, we return to C code early, since we don't need any extra synchronisation (see the comment about the absence of ISB on this path).

I would really like to ensure that we return to C code in the exact state we left it.

Quoted 122 lines
Quoted 28 lines
Quoted 28 lines
arch/arm64/include/asm/kvm_mte.h new file +15 -0
+
+.L__skip_switch\@:
+.endm
+
+#else /* CONFIG_ARM64_MTE */
+
+.macro mte_switch_to_guest g_ctxt, h_ctxt, reg1
+.endm
+
+.macro mte_switch_to_hyp g_ctxt, h_ctxt, reg1
+.endm
+
+#endif /* CONFIG_ARM64_MTE */
+#endif /* __ASSEMBLY__ */
+#endif /* __ASM_KVM_MTE_H */
arch/arm64/include/asm/sysreg.h +2 -1
index 65d15700a168..347ccac2341e 100644
@@ -651,7 +651,8 @@
 
 #define INIT_SCTLR_EL2_MMU_ON						\
 	(SCTLR_ELx_M  | SCTLR_ELx_C | SCTLR_ELx_SA | SCTLR_ELx_I |	\
-	 SCTLR_ELx_IESB | SCTLR_ELx_WXN | ENDIAN_SET_EL2 | SCTLR_EL2_RES1)
+	 SCTLR_ELx_IESB | SCTLR_ELx_WXN | ENDIAN_SET_EL2 |		\
+	 SCTLR_ELx_ITFSB | SCTLR_EL2_RES1)
 
Quoted 16 lines

arch/arm64/kernel/asm-offsets.c int main(void) · line 116

Quoted 13 lines
arch/arm64/include/asm/sysreg.h +0 -0
 #define INIT_SCTLR_EL2_MMU_OFF \
 	(SCTLR_EL2_RES1 | ENDIAN_SET_EL2)
arch/arm64/kernel/asm-offsets.c +3 -0
index 0cb34ccb6e73..6b489a8462f0 100644
@@ -111,6 +111,9 @@ int main(void)
   DEFINE(VCPU_WORKAROUND_FLAGS,	offsetof(struct kvm_vcpu, arch.workaround_flags));
   DEFINE(VCPU_HCR_EL2,		offsetof(struct kvm_vcpu, arch.hcr_el2));
   DEFINE(CPU_USER_PT_REGS,	offsetof(struct kvm_cpu_context, regs));
+  DEFINE(CPU_RGSR_EL1,		offsetof(struct kvm_cpu_context, sys_regs[RGSR_EL1]));
+  DEFINE(CPU_GCR_EL1,		offsetof(struct kvm_cpu_context, sys_regs[GCR_EL1]));
+  DEFINE(CPU_TFSRE0_EL1,	offsetof(struct kvm_cpu_context, sys_regs[TFSRE0_EL1]));

TFSRE0_EL1 is never accessed from assembly code. Leftover from a previous version?

Indeed, I will drop it.

Quoted 75 lines
Quoted 65 lines
arch/arm64/kernel/asm-offsets.c +0 -0
   DEFINE(CPU_APIAKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APIAKEYLO_EL1]));
   DEFINE(CPU_APIBKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APIBKEYLO_EL1]));
   DEFINE(CPU_APDAKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APDAKEYLO_EL1]));
arch/arm64/kvm/hyp/entry.S +7 -0
index e831d3dfd50d..435346ea1504 100644
@@ -13,6 +13,7 @@
 #include <asm/kvm_arm.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_mmu.h>
+#include <asm/kvm_mte.h>
 #include <asm/kvm_ptrauth.h>
 
 	.text
@@ -51,6 +52,9 @@ alternative_else_nop_endif
 
 	add	x29, x0, #VCPU_CONTEXT
 
+	// mte_switch_to_guest(g_ctxt, h_ctxt, tmp1)
+	mte_switch_to_guest x29, x1, x2
+
 	// Macro ptrauth_switch_to_guest format:
 	// 	ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3)
 	// The below macro to restore guest keys is not implemented in C code
@@ -142,6 +146,9 @@ SYM_INNER_LABEL(__guest_exit, SYM_L_GLOBAL)
 	// when this feature is enabled for kernel code.
 	ptrauth_switch_to_hyp x1, x2, x3, x4, x5
 
+	// mte_switch_to_hyp(g_ctxt, h_ctxt, reg1)
+	mte_switch_to_hyp x1, x2, x3
+
 	// Restore hyp's sp_el0
 	restore_sp_el0 x2, x3
 
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +11 -0
index cce43bfe158f..de7e14c862e6 100644
@@ -14,6 +14,7 @@
 #include <asm/kvm_asm.h>
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_hyp.h>
+#include <asm/kvm_mmu.h>
 
 static inline void __sysreg_save_common_state(struct kvm_cpu_context *ctxt)
 {
@@ -26,6 +27,16 @@ static inline void __sysreg_save_user_state(struct kvm_cpu_context *ctxt)
 	ctxt_sys_reg(ctxt, TPIDRRO_EL0)	= read_sysreg(tpidrro_el0);
 }
 
+static inline bool ctxt_has_mte(struct kvm_cpu_context *ctxt)
+{
+	struct kvm_vcpu *vcpu = ctxt->__hyp_running_vcpu;
+
+	if (!vcpu)
+		vcpu = container_of(ctxt, struct kvm_vcpu, arch.ctxt);
+
+	return kvm_has_mte(kern_hyp_va(vcpu->kvm));
+}
+
 static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
 {
 	ctxt_sys_reg(ctxt, CSSELR_EL1)	= read_sysreg(csselr_el1);

arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt) · line 63

Quoted 7 lines
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +4 -0
@@ -46,6 +57,11 @@ static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
 	ctxt_sys_reg(ctxt, PAR_EL1)	= read_sysreg_par();
 	ctxt_sys_reg(ctxt, TPIDR_EL1)	= read_sysreg(tpidr_el1);
 
+	if (ctxt_has_mte(ctxt)) {
+		ctxt_sys_reg(ctxt, TFSR_EL1) = read_sysreg_el1(SYS_TFSR);
+		ctxt_sys_reg(ctxt, TFSRE0_EL1) = read_sysreg_s(SYS_TFSRE0_EL1);
+	}

I remember suggesting that this is slightly heavier than necessary.

Quoted 7 lines
Quoted 5 lines

On nVHE, TFSRE0_EL1 could be moved to load/put, as we never run userspace with a vcpu loaded. The same holds of course for VHE, but we also can move TFSR_EL1 to load/put, as the host uses TFSR_EL2.

Do you see any issue with that?

The comment[1] I made before was:

Ah, I totally missed this email (or can't remember reading it, which amounts to the same thing). Apologies for that.

Quoted 13 lines

For TFSR_EL1 + VHE I believe it is synchronised only on vcpu_load/put - __sysreg_save_el1_state() is called from kvm_vcpu_load_sysregs_vhe().

TFSRE0_EL1 potentially could be improved. I have to admit I was unsure if it should be in __sysreg_save_user_state() instead. However AFAICT that is called at the same time as __sysreg_save_el1_state() and there's no optimisation for nVHE. And given it's an _EL1 register this seemed like the logic place.

Am I missing something here? Potentially there are other registers to be optimised (TPIDRRO_EL0 looks like a possiblity), but IMHO that doesn't belong in this series.

Quoted 5 lines

For VHE TFSR_EL1 is already only saved/restored on load/put (__sysreg_save_el1_state() is called from kvm_vcpu_put_sysregs_vhe()).

TFSRE0_EL1 could be moved, but I'm not sure where it should live as I mentioned above.

Yeah, this looks fine, please ignore my rambling.

Thanks,

	M.
Signature · 5 lines
Without deviation from the norm, progress is not possible.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#28 lore raw

Re: [PATCH v12 5/8] arm64: kvm: Save/restore MTE registers

Quoted 102 lines · Marc Zyngier
Quoted 100 lines · Steven Price
Quoted 97 lines · Marc Zyngier
Quoted 81 lines · Steven Price

Define the new system registers that MTE introduces and context switch them. The MTE feature is still hidden from the ID register as it isn't supported in a VM yet.

---
 arch/arm64/include/asm/kvm_host.h          |  6 ++
 arch/arm64/include/asm/kvm_mte.h           | 66 ++++++++++++++++++++++
 arch/arm64/include/asm/sysreg.h            |  3 +-
 arch/arm64/kernel/asm-offsets.c            |  3 +
 arch/arm64/kvm/hyp/entry.S                 |  7 +++
 arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 21 +++++++
 arch/arm64/kvm/sys_regs.c                  | 22 ++++++--
 7 files changed, 123 insertions(+), 5 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_mte.h
arch/arm64/include/asm/kvm_host.h +6 -0
index afaa5333f0e4..309e36cc1b42 100644
@@ -208,6 +208,12 @@ enum vcpu_sysreg {
 	CNTP_CVAL_EL0,
 	CNTP_CTL_EL0,
 
+	/* Memory Tagging Extension registers */
+	RGSR_EL1,	/* Random Allocation Tag Seed Register */
+	GCR_EL1,	/* Tag Control Register */
+	TFSR_EL1,	/* Tag Fault Status Register (EL1) */
+	TFSRE0_EL1,	/* Tag Fault Status Register (EL0) */
+
 	/* 32bit specific registers. Keep them at the end of the range */
 	DACR32_EL2,	/* Domain Access Control Register */
 	IFSR32_EL2,	/* Instruction Fault Status Register */
arch/arm64/include/asm/kvm_mte.h new file +41 -0
new file mode 100644
index 000000000000..6541c7d6ce06
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020 ARM Ltd.
+ */
+#ifndef __ASM_KVM_MTE_H
+#define __ASM_KVM_MTE_H
+
+#ifdef __ASSEMBLY__
+
+#include <asm/sysreg.h>
+
+#ifdef CONFIG_ARM64_MTE
+
+.macro mte_switch_to_guest g_ctxt, h_ctxt, reg1
+alternative_if_not ARM64_MTE
+	b	.L__skip_switch\@
+alternative_else_nop_endif
+	mrs	\reg1, hcr_el2
+	and	\reg1, \reg1, #(HCR_ATA)
+	cbz	\reg1, .L__skip_switch\@
+
+	mrs_s	\reg1, SYS_RGSR_EL1
+	str	\reg1, [\h_ctxt, #CPU_RGSR_EL1]
+	mrs_s	\reg1, SYS_GCR_EL1
+	str	\reg1, [\h_ctxt, #CPU_GCR_EL1]
+
+	ldr	\reg1, [\g_ctxt, #CPU_RGSR_EL1]
+	msr_s	SYS_RGSR_EL1, \reg1
+	ldr	\reg1, [\g_ctxt, #CPU_GCR_EL1]
+	msr_s	SYS_GCR_EL1, \reg1
+
+.L__skip_switch\@:
+.endm
+
+.macro mte_switch_to_hyp g_ctxt, h_ctxt, reg1
+alternative_if_not ARM64_MTE
+	b	.L__skip_switch\@
+alternative_else_nop_endif
+	mrs	\reg1, hcr_el2
+	and	\reg1, \reg1, #(HCR_ATA)
+	cbz	\reg1, .L__skip_switch\@

arch/arm64/include/asm/kvm_mte.h line 51

Quoted 10 lines
arch/arm64/include/asm/kvm_mte.h new file +10 -0
+
+	mrs_s	\reg1, SYS_RGSR_EL1
+	str	\reg1, [\g_ctxt, #CPU_RGSR_EL1]
+	mrs_s	\reg1, SYS_GCR_EL1
+	str	\reg1, [\g_ctxt, #CPU_GCR_EL1]
+
+	ldr	\reg1, [\h_ctxt, #CPU_RGSR_EL1]
+	msr_s	SYS_RGSR_EL1, \reg1
+	ldr	\reg1, [\h_ctxt, #CPU_GCR_EL1]
+	msr_s	SYS_GCR_EL1, \reg1

What is the rational for not having any synchronisation here? It is quite uncommon to allocate memory at EL2, but VHE can perform all kind of tricks.

Quoted 8 lines
Quoted 3 lines

I don't follow. This is part of the __guest_exit path and there's an ISB at the end of that - is that not sufficient? I don't see any possibility for allocating memory before that. What am I missing?

Which ISB? We have a few in the SError handling code, but that's conditioned on not having RAS. With any RAS-enabled CPU, we return to C code early, since we don't need any extra synchronisation (see the comment about the absence of ISB on this path).

Ah, I clearly didn't read the code (or comment) carefully enough - indeed with RAS we're potentially skipping the ISB.

Quoted 2 lines

I would really like to ensure that we return to C code in the exact state we left it.

Agreed, I'll stick an ISB at the end of mte_switch_to_hyp. Although there's clearly room for optimisation here as ptrauth_switch_to_hyp has a similar ISB.

Quoted 146 lines
Quoted 122 lines
Quoted 28 lines
Quoted 28 lines
arch/arm64/include/asm/kvm_mte.h new file +15 -0
+
+.L__skip_switch\@:
+.endm
+
+#else /* CONFIG_ARM64_MTE */
+
+.macro mte_switch_to_guest g_ctxt, h_ctxt, reg1
+.endm
+
+.macro mte_switch_to_hyp g_ctxt, h_ctxt, reg1
+.endm
+
+#endif /* CONFIG_ARM64_MTE */
+#endif /* __ASSEMBLY__ */
+#endif /* __ASM_KVM_MTE_H */
arch/arm64/include/asm/sysreg.h +2 -1
index 65d15700a168..347ccac2341e 100644
@@ -651,7 +651,8 @@
 
 #define INIT_SCTLR_EL2_MMU_ON						\
 	(SCTLR_ELx_M  | SCTLR_ELx_C | SCTLR_ELx_SA | SCTLR_ELx_I |	\
-	 SCTLR_ELx_IESB | SCTLR_ELx_WXN | ENDIAN_SET_EL2 | SCTLR_EL2_RES1)
+	 SCTLR_ELx_IESB | SCTLR_ELx_WXN | ENDIAN_SET_EL2 |		\
+	 SCTLR_ELx_ITFSB | SCTLR_EL2_RES1)
 
Quoted 16 lines

arch/arm64/kernel/asm-offsets.c int main(void) · line 116

Quoted 13 lines
arch/arm64/include/asm/sysreg.h +0 -0
 #define INIT_SCTLR_EL2_MMU_OFF \
 	(SCTLR_EL2_RES1 | ENDIAN_SET_EL2)
arch/arm64/kernel/asm-offsets.c +3 -0
index 0cb34ccb6e73..6b489a8462f0 100644
@@ -111,6 +111,9 @@ int main(void)
   DEFINE(VCPU_WORKAROUND_FLAGS,	offsetof(struct kvm_vcpu, arch.workaround_flags));
   DEFINE(VCPU_HCR_EL2,		offsetof(struct kvm_vcpu, arch.hcr_el2));
   DEFINE(CPU_USER_PT_REGS,	offsetof(struct kvm_cpu_context, regs));
+  DEFINE(CPU_RGSR_EL1,		offsetof(struct kvm_cpu_context, sys_regs[RGSR_EL1]));
+  DEFINE(CPU_GCR_EL1,		offsetof(struct kvm_cpu_context, sys_regs[GCR_EL1]));
+  DEFINE(CPU_TFSRE0_EL1,	offsetof(struct kvm_cpu_context, sys_regs[TFSRE0_EL1]));

TFSRE0_EL1 is never accessed from assembly code. Leftover from a previous version?

Indeed, I will drop it.

Quoted 75 lines
Quoted 65 lines
arch/arm64/kernel/asm-offsets.c +0 -0
   DEFINE(CPU_APIAKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APIAKEYLO_EL1]));
   DEFINE(CPU_APIBKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APIBKEYLO_EL1]));
   DEFINE(CPU_APDAKEYLO_EL1,	offsetof(struct kvm_cpu_context, sys_regs[APDAKEYLO_EL1]));
arch/arm64/kvm/hyp/entry.S +7 -0
index e831d3dfd50d..435346ea1504 100644
@@ -13,6 +13,7 @@
 #include <asm/kvm_arm.h>
 #include <asm/kvm_asm.h>
 #include <asm/kvm_mmu.h>
+#include <asm/kvm_mte.h>
 #include <asm/kvm_ptrauth.h>
 
 	.text
@@ -51,6 +52,9 @@ alternative_else_nop_endif
 
 	add	x29, x0, #VCPU_CONTEXT
 
+	// mte_switch_to_guest(g_ctxt, h_ctxt, tmp1)
+	mte_switch_to_guest x29, x1, x2
+
 	// Macro ptrauth_switch_to_guest format:
 	// 	ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3)
 	// The below macro to restore guest keys is not implemented in C code
@@ -142,6 +146,9 @@ SYM_INNER_LABEL(__guest_exit, SYM_L_GLOBAL)
 	// when this feature is enabled for kernel code.
 	ptrauth_switch_to_hyp x1, x2, x3, x4, x5
 
+	// mte_switch_to_hyp(g_ctxt, h_ctxt, reg1)
+	mte_switch_to_hyp x1, x2, x3
+
 	// Restore hyp's sp_el0
 	restore_sp_el0 x2, x3
 
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +11 -0
index cce43bfe158f..de7e14c862e6 100644
@@ -14,6 +14,7 @@
 #include <asm/kvm_asm.h>
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_hyp.h>
+#include <asm/kvm_mmu.h>
 
 static inline void __sysreg_save_common_state(struct kvm_cpu_context *ctxt)
 {
@@ -26,6 +27,16 @@ static inline void __sysreg_save_user_state(struct kvm_cpu_context *ctxt)
 	ctxt_sys_reg(ctxt, TPIDRRO_EL0)	= read_sysreg(tpidrro_el0);
 }
 
+static inline bool ctxt_has_mte(struct kvm_cpu_context *ctxt)
+{
+	struct kvm_vcpu *vcpu = ctxt->__hyp_running_vcpu;
+
+	if (!vcpu)
+		vcpu = container_of(ctxt, struct kvm_vcpu, arch.ctxt);
+
+	return kvm_has_mte(kern_hyp_va(vcpu->kvm));
+}
+
 static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
 {
 	ctxt_sys_reg(ctxt, CSSELR_EL1)	= read_sysreg(csselr_el1);

arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt) · line 63

Quoted 7 lines
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +4 -0
@@ -46,6 +57,11 @@ static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
 	ctxt_sys_reg(ctxt, PAR_EL1)	= read_sysreg_par();
 	ctxt_sys_reg(ctxt, TPIDR_EL1)	= read_sysreg(tpidr_el1);
 
+	if (ctxt_has_mte(ctxt)) {
+		ctxt_sys_reg(ctxt, TFSR_EL1) = read_sysreg_el1(SYS_TFSR);
+		ctxt_sys_reg(ctxt, TFSRE0_EL1) = read_sysreg_s(SYS_TFSRE0_EL1);
+	}

I remember suggesting that this is slightly heavier than necessary.

Quoted 7 lines
Quoted 5 lines

On nVHE, TFSRE0_EL1 could be moved to load/put, as we never run userspace with a vcpu loaded. The same holds of course for VHE, but we also can move TFSR_EL1 to load/put, as the host uses TFSR_EL2.

Do you see any issue with that?

The comment[1] I made before was:

Ah, I totally missed this email (or can't remember reading it, which amounts to the same thing). Apologies for that.

Quoted 13 lines

For TFSR_EL1 + VHE I believe it is synchronised only on vcpu_load/put - __sysreg_save_el1_state() is called from kvm_vcpu_load_sysregs_vhe().

TFSRE0_EL1 potentially could be improved. I have to admit I was unsure if it should be in __sysreg_save_user_state() instead. However AFAICT that is called at the same time as __sysreg_save_el1_state() and there's no optimisation for nVHE. And given it's an _EL1 register this seemed like the logic place.

Am I missing something here? Potentially there are other registers to be optimised (TPIDRRO_EL0 looks like a possiblity), but IMHO that doesn't belong in this series.

Quoted 7 lines
Quoted 5 lines

For VHE TFSR_EL1 is already only saved/restored on load/put (__sysreg_save_el1_state() is called from kvm_vcpu_put_sysregs_vhe()).

TFSRE0_EL1 could be moved, but I'm not sure where it should live as I mentioned above.

Yeah, this looks fine, please ignore my rambling.

No problem!

Thanks,

Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#29 lore raw

PATCH v12 6/8 arm64: kvm: Expose KVM_ARM_CAP_MTE

In reply to Steven Price

It's now safe for the VMM to enable MTE in a guest, so expose the capability to user space.

---
 arch/arm64/kvm/arm.c      | 9 +++++++++
 arch/arm64/kvm/sys_regs.c | 3 +++
 2 files changed, 12 insertions(+)
arch/arm64/kvm/arm.c +9 -0
index 1cb39c0803a4..e89a5e275e25 100644
@@ -93,6 +93,12 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		r = 0;
 		kvm->arch.return_nisv_io_abort_to_user = true;
 		break;
+	case KVM_CAP_ARM_MTE:
+		if (!system_supports_mte() || kvm->created_vcpus)
+			return -EINVAL;
+		r = 0;
+		kvm->arch.mte_enabled = true;
+		break;
 	default:
 		r = -EINVAL;
 		break;
@@ -237,6 +243,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		 */
 		r = 1;
 		break;
+	case KVM_CAP_ARM_MTE:
+		r = system_supports_mte();
+		break;
 	case KVM_CAP_STEAL_TIME:
 		r = kvm_arm_pvtime_supported();
 		break;
arch/arm64/kvm/sys_regs.c +3 -0
index 88adbc2286f2..3a749fa0779b 100644
@@ -1308,6 +1308,9 @@ static bool access_ccsidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 static unsigned int mte_visibility(const struct kvm_vcpu *vcpu,
 				   const struct sys_reg_desc *rd)
 {
+	if (kvm_has_mte(vcpu->kvm))
+		return 0;
+
 	return REG_HIDDEN;
 }
 
Signature · 1 line
2.20.1
maintainer
#30 lore raw

Re: [PATCH v12 6/8] arm64: kvm: Expose KVM_ARM_CAP_MTE

Quoted 15 lines · Steven Price

It's now safe for the VMM to enable MTE in a guest, so expose the capability to user space.

---
 arch/arm64/kvm/arm.c      | 9 +++++++++
 arch/arm64/kvm/sys_regs.c | 3 +++
 2 files changed, 12 insertions(+)
arch/arm64/kvm/arm.c +0 -0
index 1cb39c0803a4..e89a5e275e25 100644

arch/arm64/kvm/arm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 100

Quoted 8 lines
arch/arm64/kvm/arm.c +5 -0
@@ -93,6 +93,12 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		r = 0;
 		kvm->arch.return_nisv_io_abort_to_user = true;
 		break;
+	case KVM_CAP_ARM_MTE:
+		if (!system_supports_mte() || kvm->created_vcpus)
+			return -EINVAL;
+		r = 0;
+		kvm->arch.mte_enabled = true;

As far as I can tell from the architecture, this isn't valid for a 32bit guest.

	M.
Signature · 5 lines
Without deviation from the norm, progress is not possible.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#31 lore raw

Re: [PATCH v12 6/8] arm64: kvm: Expose KVM_ARM_CAP_MTE

Quoted 12 lines · Marc Zyngier
Quoted 10 lines · Steven Price

It's now safe for the VMM to enable MTE in a guest, so expose the capability to user space.

---
 arch/arm64/kvm/arm.c      | 9 +++++++++
 arch/arm64/kvm/sys_regs.c | 3 +++
 2 files changed, 12 insertions(+)
Quoted 16 lines

arch/arm64/kvm/arm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 100

Quoted 13 lines
arch/arm64/kvm/arm.c +5 -0
index 1cb39c0803a4..e89a5e275e25 100644
@@ -93,6 +93,12 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		r = 0;
 		kvm->arch.return_nisv_io_abort_to_user = true;
 		break;
+	case KVM_CAP_ARM_MTE:
+		if (!system_supports_mte() || kvm->created_vcpus)
+			return -EINVAL;
+		r = 0;
+		kvm->arch.mte_enabled = true;

As far as I can tell from the architecture, this isn't valid for a 32bit guest.

Indeed, however the MTE flag is a property of the VM not of the vCPU. And, unless I'm mistaken, it's technically possible to create a VM where some CPUs are 32 bit and some 64 bit. Not that I can see much use of a configuration like that.

Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer
#32 lore raw

Re: [PATCH v12 6/8] arm64: kvm: Expose KVM_ARM_CAP_MTE

Quoted 31 lines · Steven Price
Quoted 12 lines · Marc Zyngier
Quoted 10 lines · Steven Price

It's now safe for the VMM to enable MTE in a guest, so expose the capability to user space.

---
 arch/arm64/kvm/arm.c      | 9 +++++++++
 arch/arm64/kvm/sys_regs.c | 3 +++
 2 files changed, 12 insertions(+)
Quoted 16 lines

arch/arm64/kvm/arm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 100

Quoted 13 lines
arch/arm64/kvm/arm.c +5 -0
index 1cb39c0803a4..e89a5e275e25 100644
@@ -93,6 +93,12 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		r = 0;
 		kvm->arch.return_nisv_io_abort_to_user = true;
 		break;
+	case KVM_CAP_ARM_MTE:
+		if (!system_supports_mte() || kvm->created_vcpus)
+			return -EINVAL;
+		r = 0;
+		kvm->arch.mte_enabled = true;

As far as I can tell from the architecture, this isn't valid for a 32bit guest.

Quoted 4 lines

Indeed, however the MTE flag is a property of the VM not of the vCPU. And, unless I'm mistaken, it's technically possible to create a VM where some CPUs are 32 bit and some 64 bit. Not that I can see much use of a configuration like that.

It looks that this is indeed a bug, and I'm on my way to squash it. Can't believe we allowed that for so long...

But the architecture clearly states:

<quote>
These features are supported in AArch64 state only. </quote>

So I'd expect something like:

arch/arm64/kvm/reset.c +2 -1
index 956cdc240148..50635eacfa43 100644
@@ -220,7 +220,8 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 	switch (vcpu->arch.target) {
 	default:
 		if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) {
-			if (!cpus_have_const_cap(ARM64_HAS_32BIT_EL1)) {
+			if (!cpus_have_const_cap(ARM64_HAS_32BIT_EL1) ||
+			    vcpu->kvm->arch.mte_enabled) {
 				ret = -EINVAL;
 				goto out;
 			}

that makes it completely impossible to create 32bit CPUs within a MTE-enabled guest.

Thanks,

	M.
Signature · 5 lines
Without deviation from the norm, progress is not possible.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#33 lore raw

Re: [PATCH v12 6/8] arm64: kvm: Expose KVM_ARM_CAP_MTE

Quoted 33 lines · Marc Zyngier
Quoted 31 lines · Steven Price
Quoted 28 lines · Marc Zyngier
Quoted 15 lines · Steven Price

It's now safe for the VMM to enable MTE in a guest, so expose the capability to user space.

---
 arch/arm64/kvm/arm.c      | 9 +++++++++
 arch/arm64/kvm/sys_regs.c | 3 +++
 2 files changed, 12 insertions(+)
arch/arm64/kvm/arm.c +0 -0
index 1cb39c0803a4..e89a5e275e25 100644

arch/arm64/kvm/arm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 100

Quoted 8 lines
arch/arm64/kvm/arm.c +5 -0
@@ -93,6 +93,12 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		r = 0;
 		kvm->arch.return_nisv_io_abort_to_user = true;
 		break;
+	case KVM_CAP_ARM_MTE:
+		if (!system_supports_mte() || kvm->created_vcpus)
+			return -EINVAL;
+		r = 0;
+		kvm->arch.mte_enabled = true;

As far as I can tell from the architecture, this isn't valid for a 32bit guest.

Quoted 7 lines
Quoted 4 lines

Indeed, however the MTE flag is a property of the VM not of the vCPU. And, unless I'm mistaken, it's technically possible to create a VM where some CPUs are 32 bit and some 64 bit. Not that I can see much use of a configuration like that.

It looks that this is indeed a bug, and I'm on my way to squash it. Can't believe we allowed that for so long...

Ah, well if you're going to kill off mixed 32bit/64bit VMs then...

Quoted 23 lines

But the architecture clearly states:

<quote>
These features are supported in AArch64 state only. </quote>

So I'd expect something like:

arch/arm64/kvm/reset.c +2 -1
index 956cdc240148..50635eacfa43 100644
@@ -220,7 +220,8 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 	switch (vcpu->arch.target) {
 	default:
 		if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) {
-			if (!cpus_have_const_cap(ARM64_HAS_32BIT_EL1)) {
+			if (!cpus_have_const_cap(ARM64_HAS_32BIT_EL1) ||
+			    vcpu->kvm->arch.mte_enabled) {
 				ret = -EINVAL;
 				goto out;
 			}
Quoted 2 lines

that makes it completely impossible to create 32bit CPUs within a MTE-enabled guest.

... that makes complete sense, and I'll include this hunk in my next posting.

Thanks,

Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#34 lore raw

PATCH v12 7/8 KVM: arm64: ioctl to fetch/store tags in a guest

In reply to Steven Price

The VMM may not wish to have it's own mapping of guest memory mapped with PROT_MTE because this causes problems if the VMM has tag checking enabled (the guest controls the tags in physical RAM and it's unlikely the tags are correct for the VMM).

Instead add a new ioctl which allows the VMM to easily read/write the tags from guest memory, allowing the VMM's mapping to be non-PROT_MTE while the VMM can still read/write the tags for the purpose of migration.

---
 arch/arm64/include/uapi/asm/kvm.h | 11 +++++
 arch/arm64/kvm/arm.c              | 69 +++++++++++++++++++++++++++++++
 include/uapi/linux/kvm.h          |  1 +
 3 files changed, 81 insertions(+)
arch/arm64/include/uapi/asm/kvm.h +11 -0
index 24223adae150..b3edde68bc3e 100644
@@ -184,6 +184,17 @@ struct kvm_vcpu_events {
 	__u32 reserved[12];
 };
 
+struct kvm_arm_copy_mte_tags {
+	__u64 guest_ipa;
+	__u64 length;
+	void __user *addr;
+	__u64 flags;
+	__u64 reserved[2];
+};
+
+#define KVM_ARM_TAGS_TO_GUEST		0
+#define KVM_ARM_TAGS_FROM_GUEST		1
+
 /* If you need to interpret the index values, here is the key: */
 #define KVM_REG_ARM_COPROC_MASK		0x000000000FFF0000
 #define KVM_REG_ARM_COPROC_SHIFT	16
arch/arm64/kvm/arm.c +69 -0

81 lines not on this page · raw message

include/uapi/linux/kvm.h +1 -0
index 8c95ba0fadda..4c011c60d468 100644
@@ -1428,6 +1428,7 @@ struct kvm_s390_ucas_mapping {
 /* Available with KVM_CAP_PMU_EVENT_FILTER */
 #define KVM_SET_PMU_EVENT_FILTER  _IOW(KVMIO,  0xb2, struct kvm_pmu_event_filter)
 #define KVM_PPC_SVM_OFF		  _IO(KVMIO,  0xb3)
+#define KVM_ARM_MTE_COPY_TAGS	  _IOR(KVMIO,  0xb4, struct kvm_arm_copy_mte_tags)
 
 /* ioctl for vm fd */
 #define KVM_CREATE_DEVICE	  _IOWR(KVMIO,  0xe0, struct kvm_create_device)
Signature · 1 line
2.20.1
maintainer
#35 lore raw

Re: [PATCH v12 7/8] KVM: arm64: ioctl to fetch/store tags in a guest

Quoted 65 lines · Steven Price

The VMM may not wish to have it's own mapping of guest memory mapped with PROT_MTE because this causes problems if the VMM has tag checking enabled (the guest controls the tags in physical RAM and it's unlikely the tags are correct for the VMM).

Instead add a new ioctl which allows the VMM to easily read/write the tags from guest memory, allowing the VMM's mapping to be non-PROT_MTE while the VMM can still read/write the tags for the purpose of migration.

---
 arch/arm64/include/uapi/asm/kvm.h | 11 +++++
 arch/arm64/kvm/arm.c              | 69 +++++++++++++++++++++++++++++++
 include/uapi/linux/kvm.h          |  1 +
 3 files changed, 81 insertions(+)
arch/arm64/include/uapi/asm/kvm.h +11 -0
index 24223adae150..b3edde68bc3e 100644
@@ -184,6 +184,17 @@ struct kvm_vcpu_events {
 	__u32 reserved[12];
 };
 
+struct kvm_arm_copy_mte_tags {
+	__u64 guest_ipa;
+	__u64 length;
+	void __user *addr;
+	__u64 flags;
+	__u64 reserved[2];
+};
+
+#define KVM_ARM_TAGS_TO_GUEST		0
+#define KVM_ARM_TAGS_FROM_GUEST		1
+
 /* If you need to interpret the index values, here is the key: */
 #define KVM_REG_ARM_COPROC_MASK		0x000000000FFF0000
 #define KVM_REG_ARM_COPROC_SHIFT	16
arch/arm64/kvm/arm.c +17 -0
index e89a5e275e25..4b6c83beb75d 100644
@@ -1309,6 +1309,65 @@ static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
 	}
 }
 
+static int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
+				      struct kvm_arm_copy_mte_tags *copy_tags)
+{
+	gpa_t guest_ipa = copy_tags->guest_ipa;
+	size_t length = copy_tags->length;
+	void __user *tags = copy_tags->addr;
+	gpa_t gfn;
+	bool write = !(copy_tags->flags & KVM_ARM_TAGS_FROM_GUEST);
+	int ret = 0;
+
+	if (copy_tags->reserved[0] || copy_tags->reserved[1])
+		return -EINVAL;
+
+	if (copy_tags->flags & ~KVM_ARM_TAGS_FROM_GUEST)
+		return -EINVAL;
+
+	if (length & ~PAGE_MASK || guest_ipa & ~PAGE_MASK)

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1338

Quoted 10 lines
arch/arm64/kvm/arm.c +10 -0
+		return -EINVAL;
+
+	gfn = gpa_to_gfn(guest_ipa);
+
+	mutex_lock(&kvm->slots_lock);
+
+	while (length > 0) {
+		kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL);
+		void *maddr;
+		unsigned long num_tags = PAGE_SIZE / MTE_GRANULE_SIZE;

nit: this is a compile time constant, make it a #define. This will avoid the confusing overloading of "num_tags" as both an input and an output for the mte_copy_tags-* functions.

Quoted 22 lines
arch/arm64/kvm/arm.c +22 -0
+
+		if (is_error_noslot_pfn(pfn)) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		maddr = page_address(pfn_to_page(pfn));
+
+		if (!write) {
+			num_tags = mte_copy_tags_to_user(tags, maddr, num_tags);
+			kvm_release_pfn_clean(pfn);
+		} else {
+			num_tags = mte_copy_tags_from_user(maddr, tags,
+							   num_tags);
+			kvm_release_pfn_dirty(pfn);
+		}
+
+		if (num_tags != PAGE_SIZE / MTE_GRANULE_SIZE) {
+			ret = -EFAULT;
+			goto out;
+		}
+

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1370

Quoted 10 lines
arch/arm64/kvm/arm.c +10 -0
+		gfn++;
+		tags += num_tags;
+		length -= PAGE_SIZE;
+	}
+
+out:
+	mutex_unlock(&kvm->slots_lock);
+	return ret;
+}
+

nit again: I'd really prefer it if you moved this to guest.c, where we already have a bunch of the save/restore stuff.

Quoted 25 lines
arch/arm64/kvm/arm.c +10 -0
 long kvm_arch_vm_ioctl(struct file *filp,
 		       unsigned int ioctl, unsigned long arg)
 {
@@ -1345,6 +1404,16 @@ long kvm_arch_vm_ioctl(struct file *filp,
 
 		return 0;
 	}
+	case KVM_ARM_MTE_COPY_TAGS: {
+		struct kvm_arm_copy_mte_tags copy_tags;
+
+		if (!kvm_has_mte(kvm))
+			return -EINVAL;
+
+		if (copy_from_user(&copy_tags, argp, sizeof(copy_tags)))
+			return -EFAULT;
+		return kvm_vm_ioctl_mte_copy_tags(kvm, &copy_tags);
+	}
 	default:
 		return -EINVAL;
 	}
include/uapi/linux/kvm.h +0 -0
index 8c95ba0fadda..4c011c60d468 100644

include/uapi/linux/kvm.h struct kvm_s390_ucas_mapping { · line 1434

Quoted 7 lines
include/uapi/linux/kvm.h +1 -0
@@ -1428,6 +1428,7 @@ struct kvm_s390_ucas_mapping {
 /* Available with KVM_CAP_PMU_EVENT_FILTER */
 #define KVM_SET_PMU_EVENT_FILTER  _IOW(KVMIO,  0xb2, struct kvm_pmu_event_filter)
 #define KVM_PPC_SVM_OFF		  _IO(KVMIO,  0xb3)
+#define KVM_ARM_MTE_COPY_TAGS	  _IOR(KVMIO,  0xb4, struct kvm_arm_copy_mte_tags)
 
 /* ioctl for vm fd */
 #define KVM_CREATE_DEVICE	  _IOWR(KVMIO,  0xe0, struct kvm_create_device)

Thanks,

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

Re: [PATCH v12 7/8] KVM: arm64: ioctl to fetch/store tags in a guest

Quoted 73 lines · Marc Zyngier
Quoted 71 lines · Steven Price

The VMM may not wish to have it's own mapping of guest memory mapped with PROT_MTE because this causes problems if the VMM has tag checking enabled (the guest controls the tags in physical RAM and it's unlikely the tags are correct for the VMM).

Instead add a new ioctl which allows the VMM to easily read/write the tags from guest memory, allowing the VMM's mapping to be non-PROT_MTE while the VMM can still read/write the tags for the purpose of migration.

---
 arch/arm64/include/uapi/asm/kvm.h | 11 +++++
 arch/arm64/kvm/arm.c              | 69 +++++++++++++++++++++++++++++++
 include/uapi/linux/kvm.h          |  1 +
 3 files changed, 81 insertions(+)
arch/arm64/include/uapi/asm/kvm.h +11 -0
index 24223adae150..b3edde68bc3e 100644
@@ -184,6 +184,17 @@ struct kvm_vcpu_events {
 	__u32 reserved[12];
 };
 
+struct kvm_arm_copy_mte_tags {
+	__u64 guest_ipa;
+	__u64 length;
+	void __user *addr;
+	__u64 flags;
+	__u64 reserved[2];
+};
+
+#define KVM_ARM_TAGS_TO_GUEST		0
+#define KVM_ARM_TAGS_FROM_GUEST		1
+
 /* If you need to interpret the index values, here is the key: */
 #define KVM_REG_ARM_COPROC_MASK		0x000000000FFF0000
 #define KVM_REG_ARM_COPROC_SHIFT	16
arch/arm64/kvm/arm.c +23 -0
index e89a5e275e25..4b6c83beb75d 100644
@@ -1309,6 +1309,65 @@ static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
 	}
 }
 
+static int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
+				      struct kvm_arm_copy_mte_tags *copy_tags)
+{
+	gpa_t guest_ipa = copy_tags->guest_ipa;
+	size_t length = copy_tags->length;
+	void __user *tags = copy_tags->addr;
+	gpa_t gfn;
+	bool write = !(copy_tags->flags & KVM_ARM_TAGS_FROM_GUEST);
+	int ret = 0;
+
+	if (copy_tags->reserved[0] || copy_tags->reserved[1])
+		return -EINVAL;
+
+	if (copy_tags->flags & ~KVM_ARM_TAGS_FROM_GUEST)
+		return -EINVAL;
+
+	if (length & ~PAGE_MASK || guest_ipa & ~PAGE_MASK)
+		return -EINVAL;
+
+	gfn = gpa_to_gfn(guest_ipa);
+
+	mutex_lock(&kvm->slots_lock);
+
Quoted 8 lines

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1338

Quoted 4 lines
arch/arm64/kvm/arm.c +4 -0
+	while (length > 0) {
+		kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL);
+		void *maddr;
+		unsigned long num_tags = PAGE_SIZE / MTE_GRANULE_SIZE;

nit: this is a compile time constant, make it a #define. This will avoid the confusing overloading of "num_tags" as both an input and an output for the mte_copy_tags-* functions.

No problem, I agree my usage of num_tags wasn't very clear.

Quoted 27 lines
Quoted 27 lines
arch/arm64/kvm/arm.c +27 -0
+
+		if (is_error_noslot_pfn(pfn)) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		maddr = page_address(pfn_to_page(pfn));
+
+		if (!write) {
+			num_tags = mte_copy_tags_to_user(tags, maddr, num_tags);
+			kvm_release_pfn_clean(pfn);
+		} else {
+			num_tags = mte_copy_tags_from_user(maddr, tags,
+							   num_tags);
+			kvm_release_pfn_dirty(pfn);
+		}
+
+		if (num_tags != PAGE_SIZE / MTE_GRANULE_SIZE) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		gfn++;
+		tags += num_tags;
+		length -= PAGE_SIZE;
+	}
+
Quoted 8 lines

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1370

Quoted 5 lines
arch/arm64/kvm/arm.c +5 -0
+out:
+	mutex_unlock(&kvm->slots_lock);
+	return ret;
+}
+

nit again: I'd really prefer it if you moved this to guest.c, where we already have a bunch of the save/restore stuff.

Sure - I'll move it across.

Thanks,

Steve

maintainer
#37 lore raw

Re: [PATCH v12 7/8] KVM: arm64: ioctl to fetch/store tags in a guest

In reply to Steven Price
Quoted 5 lines · Steven Price
arch/arm64/include/uapi/asm/kvm.h +0 -0
index 24223adae150..b3edde68bc3e 100644

arch/arm64/include/uapi/asm/kvm.h struct kvm_vcpu_events { · line 192

Quoted 9 lines
arch/arm64/include/uapi/asm/kvm.h +6 -0
@@ -184,6 +184,17 @@ struct kvm_vcpu_events {
 	__u32 reserved[12];
 };
 
+struct kvm_arm_copy_mte_tags {
+	__u64 guest_ipa;
+	__u64 length;
+	void __user *addr;
+	__u64 flags;
+	__u64 reserved[2];

I forgot the past discussions, what's the reserved for? Future expansion?

Quoted 36 lines
arch/arm64/kvm/arm.c +28 -0
index e89a5e275e25..4b6c83beb75d 100644
@@ -1309,6 +1309,65 @@ static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
 	}
 }
 
+static int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
+				      struct kvm_arm_copy_mte_tags *copy_tags)
+{
+	gpa_t guest_ipa = copy_tags->guest_ipa;
+	size_t length = copy_tags->length;
+	void __user *tags = copy_tags->addr;
+	gpa_t gfn;
+	bool write = !(copy_tags->flags & KVM_ARM_TAGS_FROM_GUEST);
+	int ret = 0;
+
+	if (copy_tags->reserved[0] || copy_tags->reserved[1])
+		return -EINVAL;
+
+	if (copy_tags->flags & ~KVM_ARM_TAGS_FROM_GUEST)
+		return -EINVAL;
+
+	if (length & ~PAGE_MASK || guest_ipa & ~PAGE_MASK)
+		return -EINVAL;
+
+	gfn = gpa_to_gfn(guest_ipa);
+
+	mutex_lock(&kvm->slots_lock);
+
+	while (length > 0) {
+		kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL);
+		void *maddr;
+		unsigned long num_tags = PAGE_SIZE / MTE_GRANULE_SIZE;
+

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1349

Quoted 10 lines
arch/arm64/kvm/arm.c +10 -0
+		if (is_error_noslot_pfn(pfn)) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		maddr = page_address(pfn_to_page(pfn));
+
+		if (!write) {
+			num_tags = mte_copy_tags_to_user(tags, maddr, num_tags);
+			kvm_release_pfn_clean(pfn);

Do we need to check if PG_mte_tagged is set? If the page was not faulted into the guest address space but the VMM has the page, does the gfn_to_pfn_prot() guarantee that a kvm_set_spte_gfn() was called? If not, this may read stale tags.

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1354

Quoted 5 lines
arch/arm64/kvm/arm.c +5 -0
+		} else {
+			num_tags = mte_copy_tags_from_user(maddr, tags,
+							   num_tags);
+			kvm_release_pfn_dirty(pfn);
+		}

Same question here, if the we can't guarantee the stage 2 pte being set, we'd need to set PG_mte_tagged.

Quoted 16 lines
arch/arm64/kvm/arm.c +16 -0
+
+		if (num_tags != PAGE_SIZE / MTE_GRANULE_SIZE) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		gfn++;
+		tags += num_tags;
+		length -= PAGE_SIZE;
+	}
+
+out:
+	mutex_unlock(&kvm->slots_lock);
+	return ret;
+}
+
Signature · 5 lines
Catalin
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#38 lore raw

Re: [PATCH v12 7/8] KVM: arm64: ioctl to fetch/store tags in a guest

Quoted 9 lines · Catalin Marinas
Quoted 8 lines · Steven Price
arch/arm64/include/uapi/asm/kvm.h +0 -0
index 24223adae150..b3edde68bc3e 100644
@@ -184,6 +184,17 @@ struct kvm_vcpu_events {
 	__u32 reserved[12];
 };
 
Quoted 9 lines

arch/arm64/include/uapi/asm/kvm.h struct kvm_vcpu_events { · line 192

Quoted 6 lines
arch/arm64/include/uapi/asm/kvm.h +6 -0
+struct kvm_arm_copy_mte_tags {
+	__u64 guest_ipa;
+	__u64 length;
+	void __user *addr;
+	__u64 flags;
+	__u64 reserved[2];

I forgot the past discussions, what's the reserved for? Future expansion?

Yes - for future expansion. Marc asked for them[1]:

Quoted 1 line

I'd be keen on a couple of reserved __64s. Just in case...

[1] https://lore.kernel.org/r/87ft14xl9e.wl-maz%40kernel.org

Quoted 43 lines
Quoted 43 lines
arch/arm64/kvm/arm.c +35 -0
index e89a5e275e25..4b6c83beb75d 100644
@@ -1309,6 +1309,65 @@ static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
 	}
 }
 
+static int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
+				      struct kvm_arm_copy_mte_tags *copy_tags)
+{
+	gpa_t guest_ipa = copy_tags->guest_ipa;
+	size_t length = copy_tags->length;
+	void __user *tags = copy_tags->addr;
+	gpa_t gfn;
+	bool write = !(copy_tags->flags & KVM_ARM_TAGS_FROM_GUEST);
+	int ret = 0;
+
+	if (copy_tags->reserved[0] || copy_tags->reserved[1])
+		return -EINVAL;
+
+	if (copy_tags->flags & ~KVM_ARM_TAGS_FROM_GUEST)
+		return -EINVAL;
+
+	if (length & ~PAGE_MASK || guest_ipa & ~PAGE_MASK)
+		return -EINVAL;
+
+	gfn = gpa_to_gfn(guest_ipa);
+
+	mutex_lock(&kvm->slots_lock);
+
+	while (length > 0) {
+		kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL);
+		void *maddr;
+		unsigned long num_tags = PAGE_SIZE / MTE_GRANULE_SIZE;
+
+		if (is_error_noslot_pfn(pfn)) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		maddr = page_address(pfn_to_page(pfn));
+
Quoted 8 lines

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1349

Quoted 3 lines
arch/arm64/kvm/arm.c +3 -0
+		if (!write) {
+			num_tags = mte_copy_tags_to_user(tags, maddr, num_tags);
+			kvm_release_pfn_clean(pfn);

Do we need to check if PG_mte_tagged is set? If the page was not faulted into the guest address space but the VMM has the page, does the gfn_to_pfn_prot() guarantee that a kvm_set_spte_gfn() was called? If not, this may read stale tags.

Ah, I hadn't thought about that... No I don't believe gfn_to_pfn_prot() will fault it into the guest.

Quoted 8 lines

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1354

Quoted 5 lines
arch/arm64/kvm/arm.c +5 -0
+		} else {
+			num_tags = mte_copy_tags_from_user(maddr, tags,
+							   num_tags);
+			kvm_release_pfn_dirty(pfn);
+		}

Same question here, if the we can't guarantee the stage 2 pte being set, we'd need to set PG_mte_tagged.

This is arguably worse as we'll be writing tags into the guest but without setting PG_mte_tagged - so they'll be lost when the guest then faults the pages in. Which sounds like it should break migration.

I think the below should be safe, and avoids the overhead of setting the flag just for reads.

Thanks,

Steve

----8<----

		page = pfn_to_page(pfn);
		maddr = page_address(page);

		if (!write) {
			if (test_bit(PG_mte_tagged, &page->flags))
				num_tags = mte_copy_tags_to_user(tags, maddr,
							MTE_GRANULES_PER_PAGE);
			else
				/* No tags in memory, so write zeros */
				num_tags = MTE_GRANULES_PER_PAGE -
					clear_user(tag, MTE_GRANULES_PER_PAGE);
			kvm_release_pfn_clean(pfn);
		} else {
			num_tags = mte_copy_tags_from_user(maddr, tags,
							MTE_GRANULES_PER_PAGE);
			kvm_release_pfn_dirty(pfn);
		}

		if (num_tags != MTE_GRANULES_PER_PAGE) {
			ret = -EFAULT;
			goto out;
		}

		if (write)
			test_and_set_bit(PG_mte_tagged, &page->flags);
maintainer
#39 lore raw

Re: [PATCH v12 7/8] KVM: arm64: ioctl to fetch/store tags in a guest

Quoted 54 lines · Steven Price
Quoted 44 lines · Catalin Marinas
Quoted 43 lines · Steven Price
arch/arm64/kvm/arm.c +35 -0
index e89a5e275e25..4b6c83beb75d 100644
@@ -1309,6 +1309,65 @@ static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
 	}
 }
 
+static int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
+				      struct kvm_arm_copy_mte_tags *copy_tags)
+{
+	gpa_t guest_ipa = copy_tags->guest_ipa;
+	size_t length = copy_tags->length;
+	void __user *tags = copy_tags->addr;
+	gpa_t gfn;
+	bool write = !(copy_tags->flags & KVM_ARM_TAGS_FROM_GUEST);
+	int ret = 0;
+
+	if (copy_tags->reserved[0] || copy_tags->reserved[1])
+		return -EINVAL;
+
+	if (copy_tags->flags & ~KVM_ARM_TAGS_FROM_GUEST)
+		return -EINVAL;
+
+	if (length & ~PAGE_MASK || guest_ipa & ~PAGE_MASK)
+		return -EINVAL;
+
+	gfn = gpa_to_gfn(guest_ipa);
+
+	mutex_lock(&kvm->slots_lock);
+
+	while (length > 0) {
+		kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL);
+		void *maddr;
+		unsigned long num_tags = PAGE_SIZE / MTE_GRANULE_SIZE;
+
+		if (is_error_noslot_pfn(pfn)) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		maddr = page_address(pfn_to_page(pfn));
+
Quoted 8 lines

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1349

Quoted 3 lines
arch/arm64/kvm/arm.c +3 -0
+		if (!write) {
+			num_tags = mte_copy_tags_to_user(tags, maddr, num_tags);
+			kvm_release_pfn_clean(pfn);

Do we need to check if PG_mte_tagged is set? If the page was not faulted into the guest address space but the VMM has the page, does the gfn_to_pfn_prot() guarantee that a kvm_set_spte_gfn() was called? If not, this may read stale tags.

Quoted 2 lines

Ah, I hadn't thought about that... No I don't believe gfn_to_pfn_prot() will fault it into the guest.

It doesn't indeed. What it does is a get_user_pages() but it's not of much help since the VMM pte wouldn't be tagged (we would have solved lots of problems if we required PROT_MTE in the VMM...)

Quoted 21 lines
Quoted 8 lines

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1354

Quoted 5 lines
arch/arm64/kvm/arm.c +5 -0
+		} else {
+			num_tags = mte_copy_tags_from_user(maddr, tags,
+							   num_tags);
+			kvm_release_pfn_dirty(pfn);
+		}

Same question here, if the we can't guarantee the stage 2 pte being set, we'd need to set PG_mte_tagged.

This is arguably worse as we'll be writing tags into the guest but without setting PG_mte_tagged - so they'll be lost when the guest then faults the pages in. Which sounds like it should break migration.

I think the below should be safe, and avoids the overhead of setting the flag just for reads.

Thanks,

Steve

----8<----

Quoted 12 lines
		page = pfn_to_page(pfn);
		maddr = page_address(page);

		if (!write) {
			if (test_bit(PG_mte_tagged, &page->flags))
				num_tags = mte_copy_tags_to_user(tags, maddr,
							MTE_GRANULES_PER_PAGE);
			else
				/* No tags in memory, so write zeros */
				num_tags = MTE_GRANULES_PER_PAGE -
					clear_user(tag, MTE_GRANULES_PER_PAGE);
			kvm_release_pfn_clean(pfn);

For ptrace we return a -EOPNOTSUPP if the address doesn't have VM_MTE but I don't think it makes sense here, so I'm fine with clearing the destination and assuming that the tags are zero (as they'd be on faulting into the guest.

Another thing I forgot to ask, what's guaranteeing that the page supports tags? Does this ioctl ensure that it would attempt the tag copying from some device mapping? Do we need some kvm_is_device_pfn() check? I guess ZONE_DEVICE memory we just refuse to map in an earlier patch.

Quoted 6 lines
		} else {
			num_tags = mte_copy_tags_from_user(maddr, tags,
							MTE_GRANULES_PER_PAGE);
			kvm_release_pfn_dirty(pfn);
		}
Quoted 7 lines
		if (num_tags != MTE_GRANULES_PER_PAGE) {
			ret = -EFAULT;
			goto out;
		}

		if (write)
			test_and_set_bit(PG_mte_tagged, &page->flags);

I think a set_bit() would do, I doubt it's any more efficient. But why not add it in the 'else' block above where we actually wrote the tags? The copy function may have failed part-way through. Maybe your logic is correct though, there are invalid tags in the page. Just add a comment.

Signature · 1 line
Catalin
#40 lore raw

Re: [PATCH v12 7/8] KVM: arm64: ioctl to fetch/store tags in a guest

Quoted 55 lines · Catalin Marinas
Quoted 54 lines · Steven Price
Quoted 52 lines · Catalin Marinas
Quoted 36 lines · Steven Price
arch/arm64/kvm/arm.c +28 -0
index e89a5e275e25..4b6c83beb75d 100644
@@ -1309,6 +1309,65 @@ static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
 	}
 }
 
+static int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
+				      struct kvm_arm_copy_mte_tags *copy_tags)
+{
+	gpa_t guest_ipa = copy_tags->guest_ipa;
+	size_t length = copy_tags->length;
+	void __user *tags = copy_tags->addr;
+	gpa_t gfn;
+	bool write = !(copy_tags->flags & KVM_ARM_TAGS_FROM_GUEST);
+	int ret = 0;
+
+	if (copy_tags->reserved[0] || copy_tags->reserved[1])
+		return -EINVAL;
+
+	if (copy_tags->flags & ~KVM_ARM_TAGS_FROM_GUEST)
+		return -EINVAL;
+
+	if (length & ~PAGE_MASK || guest_ipa & ~PAGE_MASK)
+		return -EINVAL;
+
+	gfn = gpa_to_gfn(guest_ipa);
+
+	mutex_lock(&kvm->slots_lock);
+
+	while (length > 0) {
+		kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL);
+		void *maddr;
+		unsigned long num_tags = PAGE_SIZE / MTE_GRANULE_SIZE;
+

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1349

Quoted 10 lines
arch/arm64/kvm/arm.c +10 -0
+		if (is_error_noslot_pfn(pfn)) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		maddr = page_address(pfn_to_page(pfn));
+
+		if (!write) {
+			num_tags = mte_copy_tags_to_user(tags, maddr, num_tags);
+			kvm_release_pfn_clean(pfn);

Do we need to check if PG_mte_tagged is set? If the page was not faulted into the guest address space but the VMM has the page, does the gfn_to_pfn_prot() guarantee that a kvm_set_spte_gfn() was called? If not, this may read stale tags.

Quoted 6 lines
Quoted 2 lines

Ah, I hadn't thought about that... No I don't believe gfn_to_pfn_prot() will fault it into the guest.

It doesn't indeed. What it does is a get_user_pages() but it's not of much help since the VMM pte wouldn't be tagged (we would have solved lots of problems if we required PROT_MTE in the VMM...)

Sadly it solves some problems and creates others :(

Quoted 34 lines
Quoted 21 lines
Quoted 8 lines

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1354

Quoted 5 lines
arch/arm64/kvm/arm.c +5 -0
+		} else {
+			num_tags = mte_copy_tags_from_user(maddr, tags,
+							   num_tags);
+			kvm_release_pfn_dirty(pfn);
+		}

Same question here, if the we can't guarantee the stage 2 pte being set, we'd need to set PG_mte_tagged.

This is arguably worse as we'll be writing tags into the guest but without setting PG_mte_tagged - so they'll be lost when the guest then faults the pages in. Which sounds like it should break migration.

I think the below should be safe, and avoids the overhead of setting the flag just for reads.

Thanks,

Steve

----8<----

Quoted 12 lines
		page = pfn_to_page(pfn);
		maddr = page_address(page);

		if (!write) {
			if (test_bit(PG_mte_tagged, &page->flags))
				num_tags = mte_copy_tags_to_user(tags, maddr,
							MTE_GRANULES_PER_PAGE);
			else
				/* No tags in memory, so write zeros */
				num_tags = MTE_GRANULES_PER_PAGE -
					clear_user(tag, MTE_GRANULES_PER_PAGE);
			kvm_release_pfn_clean(pfn);
Quoted 4 lines

For ptrace we return a -EOPNOTSUPP if the address doesn't have VM_MTE but I don't think it makes sense here, so I'm fine with clearing the destination and assuming that the tags are zero (as they'd be on faulting into the guest.

Yeah - conceptually all pages in an MTE-enabled guest have tags. It's just we don't actually populate the physical memory until the guest tries to touch them. So it makes sense to just return zeros here. Alternatively we could populate the physical tags but that seems unnecessary.

Quoted 5 lines

Another thing I forgot to ask, what's guaranteeing that the page supports tags? Does this ioctl ensure that it would attempt the tag copying from some device mapping? Do we need some kvm_is_device_pfn() check? I guess ZONE_DEVICE memory we just refuse to map in an earlier patch.

Hmm, nothing much. While reads are now fine (the memory won't have PG_mte_tagged), writes could potentially happen on ZONE_DEVICE memory.

The fix is to just replace pfn_to_page() with pfn_to_online_page() and handle the error.

Quoted 11 lines
Quoted 11 lines
		} else {
			num_tags = mte_copy_tags_from_user(maddr, tags,
							MTE_GRANULES_PER_PAGE);
			kvm_release_pfn_dirty(pfn);
		}

		if (num_tags != MTE_GRANULES_PER_PAGE) {
			ret = -EFAULT;
			goto out;
		}
Quoted 4 lines
Quoted 2 lines
		if (write)
			test_and_set_bit(PG_mte_tagged, &page->flags);

I think a set_bit() would do, I doubt it's any more efficient. But why

I'd seen test_and_set_bit() used elsewhere (I forget where now) as a slightly more efficient approach. It complies down to a READ_ONCE and a conditional atomic, vs a single non-conditional atomic. But I don't have any actual data on the performance and this isn't a hot path, so I'll switch to the more obvious set_bit().

Quoted 3 lines

not add it in the 'else' block above where we actually wrote the tags? The copy function may have failed part-way through. Maybe your logic is correct though, there are invalid tags in the page. Just add a comment.

Yeah it's in case the write fails part way through - we don't want to expose the tags which were not written. I'll add a comment to explain that.

Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

maintainer
#41 lore raw

Re: [PATCH v12 7/8] KVM: arm64: ioctl to fetch/store tags in a guest

Quoted 56 lines · Steven Price
Quoted 55 lines · Catalin Marinas
Quoted 54 lines · Steven Price
Quoted 52 lines · Catalin Marinas
Quoted 36 lines · Steven Price
arch/arm64/kvm/arm.c +28 -0
index e89a5e275e25..4b6c83beb75d 100644
@@ -1309,6 +1309,65 @@ static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
 	}
 }
 
+static int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
+				      struct kvm_arm_copy_mte_tags *copy_tags)
+{
+	gpa_t guest_ipa = copy_tags->guest_ipa;
+	size_t length = copy_tags->length;
+	void __user *tags = copy_tags->addr;
+	gpa_t gfn;
+	bool write = !(copy_tags->flags & KVM_ARM_TAGS_FROM_GUEST);
+	int ret = 0;
+
+	if (copy_tags->reserved[0] || copy_tags->reserved[1])
+		return -EINVAL;
+
+	if (copy_tags->flags & ~KVM_ARM_TAGS_FROM_GUEST)
+		return -EINVAL;
+
+	if (length & ~PAGE_MASK || guest_ipa & ~PAGE_MASK)
+		return -EINVAL;
+
+	gfn = gpa_to_gfn(guest_ipa);
+
+	mutex_lock(&kvm->slots_lock);
+
+	while (length > 0) {
+		kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL);
+		void *maddr;
+		unsigned long num_tags = PAGE_SIZE / MTE_GRANULE_SIZE;
+

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1349

Quoted 10 lines
arch/arm64/kvm/arm.c +10 -0
+		if (is_error_noslot_pfn(pfn)) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		maddr = page_address(pfn_to_page(pfn));
+
+		if (!write) {
+			num_tags = mte_copy_tags_to_user(tags, maddr, num_tags);
+			kvm_release_pfn_clean(pfn);

Do we need to check if PG_mte_tagged is set? If the page was not faulted into the guest address space but the VMM has the page, does the gfn_to_pfn_prot() guarantee that a kvm_set_spte_gfn() was called? If not, this may read stale tags.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Ah, I hadn't thought about that... No I don't believe gfn_to_pfn_prot() will fault it into the guest.

It doesn't indeed. What it does is a get_user_pages() but it's not of much help since the VMM pte wouldn't be tagged (we would have solved lots of problems if we required PROT_MTE in the VMM...)

Sadly it solves some problems and creates others :(

I had some (random) thoughts on how to make things simpler, maybe. I think most of these races would have been solved if we required PROT_MTE in the VMM but this has an impact on the VMM if it wants to use MTE itself. If such requirement was in place, all KVM needed to do is check PG_mte_tagged.

So what we actually need is a set_pte_at() in the VMM to clear the tags and set PG_mte_tagged. Currently, we only do this if the memory type is tagged (PROT_MTE) but it's not strictly necessary.

As an optimisation for normal programs, we don't want to do this all the time but the visible behaviour wouldn't change (well, maybe for ptrace slightly). However, it doesn't mean we couldn't for a VMM, with an opt-in via prctl(). This would add a MMCF_MTE_TAG_INIT bit (couldn't think of a better name) to mm_context_t.flags and set_pte_at() would behave as if the pte was tagged without actually mapping the memory in user space as tagged (protection flags not changed). Pages that don't support tagging are still safe, just some unnecessary ignored tag writes. This would need to be set before the mmap() for the guest memory.

If we want finer-grained control we'd have to store this information in the vma flags, in addition to VM_MTE (e.g. VM_MTE_TAG_INIT) but without affecting the actual memory type. The easiest would be another pte bit, though we are short on them. A more intrusive (not too bad) approach is to introduce a set_pte_at_vma() and read the flags directly in the arch code. In most places where set_pte_at() is called on a user mm, the vma is also available.

Anyway, I'm not saying we go this route, just thinking out loud, get some opinions.

Quoted 8 lines
Quoted 5 lines

Another thing I forgot to ask, what's guaranteeing that the page supports tags? Does this ioctl ensure that it would attempt the tag copying from some device mapping? Do we need some kvm_is_device_pfn() check? I guess ZONE_DEVICE memory we just refuse to map in an earlier patch.

Hmm, nothing much. While reads are now fine (the memory won't have PG_mte_tagged), writes could potentially happen on ZONE_DEVICE memory.

I don't think it's a problem for writes either as the host wouldn't map such memory as tagged. It's just that it returns zeros and writes are ignored, so we could instead return an error (I haven't checked your latest series yet).

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
		} else {
			num_tags = mte_copy_tags_from_user(maddr, tags,
							MTE_GRANULES_PER_PAGE);
			kvm_release_pfn_dirty(pfn);
		}

		if (num_tags != MTE_GRANULES_PER_PAGE) {
			ret = -EFAULT;
			goto out;
		}

		if (write)
			test_and_set_bit(PG_mte_tagged, &page->flags);
Quoted 7 lines
Quoted 1 line

I think a set_bit() would do, I doubt it's any more efficient. But why

I'd seen test_and_set_bit() used elsewhere (I forget where now) as a slightly more efficient approach. It complies down to a READ_ONCE and a conditional atomic, vs a single non-conditional atomic. But I don't have any actual data on the performance and this isn't a hot path, so I'll switch to the more obvious set_bit().

Yeah, I think I've seen this as well. Anyway, it's probably lost in the noise of tag writing here.

Signature · 1 line
Catalin
#42 lore raw

Re: [PATCH v12 7/8] KVM: arm64: ioctl to fetch/store tags in a guest

Quoted 95 lines · Catalin Marinas
Quoted 56 lines · Steven Price
Quoted 55 lines · Catalin Marinas
Quoted 54 lines · Steven Price
Quoted 52 lines · Catalin Marinas
Quoted 36 lines · Steven Price
arch/arm64/kvm/arm.c +28 -0
index e89a5e275e25..4b6c83beb75d 100644
@@ -1309,6 +1309,65 @@ static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
 	}
 }
 
+static int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
+				      struct kvm_arm_copy_mte_tags *copy_tags)
+{
+	gpa_t guest_ipa = copy_tags->guest_ipa;
+	size_t length = copy_tags->length;
+	void __user *tags = copy_tags->addr;
+	gpa_t gfn;
+	bool write = !(copy_tags->flags & KVM_ARM_TAGS_FROM_GUEST);
+	int ret = 0;
+
+	if (copy_tags->reserved[0] || copy_tags->reserved[1])
+		return -EINVAL;
+
+	if (copy_tags->flags & ~KVM_ARM_TAGS_FROM_GUEST)
+		return -EINVAL;
+
+	if (length & ~PAGE_MASK || guest_ipa & ~PAGE_MASK)
+		return -EINVAL;
+
+	gfn = gpa_to_gfn(guest_ipa);
+
+	mutex_lock(&kvm->slots_lock);
+
+	while (length > 0) {
+		kvm_pfn_t pfn = gfn_to_pfn_prot(kvm, gfn, write, NULL);
+		void *maddr;
+		unsigned long num_tags = PAGE_SIZE / MTE_GRANULE_SIZE;
+

arch/arm64/kvm/arm.c static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, · line 1349

Quoted 10 lines
arch/arm64/kvm/arm.c +10 -0
+		if (is_error_noslot_pfn(pfn)) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		maddr = page_address(pfn_to_page(pfn));
+
+		if (!write) {
+			num_tags = mte_copy_tags_to_user(tags, maddr, num_tags);
+			kvm_release_pfn_clean(pfn);

Do we need to check if PG_mte_tagged is set? If the page was not faulted into the guest address space but the VMM has the page, does the gfn_to_pfn_prot() guarantee that a kvm_set_spte_gfn() was called? If not, this may read stale tags.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Ah, I hadn't thought about that... No I don't believe gfn_to_pfn_prot() will fault it into the guest.

It doesn't indeed. What it does is a get_user_pages() but it's not of much help since the VMM pte wouldn't be tagged (we would have solved lots of problems if we required PROT_MTE in the VMM...)

Sadly it solves some problems and creates others :(

I had some (random) thoughts on how to make things simpler, maybe. I think most of these races would have been solved if we required PROT_MTE in the VMM but this has an impact on the VMM if it wants to use MTE itself. If such requirement was in place, all KVM needed to do is check PG_mte_tagged.

So what we actually need is a set_pte_at() in the VMM to clear the tags and set PG_mte_tagged. Currently, we only do this if the memory type is tagged (PROT_MTE) but it's not strictly necessary.

As an optimisation for normal programs, we don't want to do this all the time but the visible behaviour wouldn't change (well, maybe for ptrace slightly). However, it doesn't mean we couldn't for a VMM, with an opt-in via prctl(). This would add a MMCF_MTE_TAG_INIT bit (couldn't think of a better name) to mm_context_t.flags and set_pte_at() would behave as if the pte was tagged without actually mapping the memory in user space as tagged (protection flags not changed). Pages that don't support tagging are still safe, just some unnecessary ignored tag writes. This would need to be set before the mmap() for the guest memory.

If we want finer-grained control we'd have to store this information in the vma flags, in addition to VM_MTE (e.g. VM_MTE_TAG_INIT) but without affecting the actual memory type. The easiest would be another pte bit, though we are short on them. A more intrusive (not too bad) approach is to introduce a set_pte_at_vma() and read the flags directly in the arch code. In most places where set_pte_at() is called on a user mm, the vma is also available.

Quoted 2 lines

Anyway, I'm not saying we go this route, just thinking out loud, get some opinions.

Does get_user_pages() actually end up calling set_pte_at() normally? If not then on the normal user_mem_abort() route although we can easily check VM_MTE_TAG_INIT there's no obvious place to hook in to ensure that the pages actually allocated have the PG_mte_tagged flag.

I'm also not sure how well this would work with the MMU notifiers path in KVM. With MMU notifiers (i.e. the VMM replacing a page in the memslot) there's not even an obvious hook to enforce the VMA flag. So I think we'd end up with something like the sanitise_mte_tags() function to at least check that the PG_mte_tagged flag is set on the pages (assuming that the trigger for the MMU notifier has done the corresponding set_pte_at()). Admittedly this might close the current race documented there.

It also feels wrong to me to tie this to a process with prctl(), it seems much more normal to implement this as a new mprotect() flag as this is really a memory property not a process property. And I think we'll find some scary corner cases if we try to associate everything back to a process - although I can't instantly think of anything that will actually break.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Another thing I forgot to ask, what's guaranteeing that the page supports tags? Does this ioctl ensure that it would attempt the tag copying from some device mapping? Do we need some kvm_is_device_pfn() check? I guess ZONE_DEVICE memory we just refuse to map in an earlier patch.

Quoted 7 lines
Quoted 2 lines

Hmm, nothing much. While reads are now fine (the memory won't have PG_mte_tagged), writes could potentially happen on ZONE_DEVICE memory.

I don't think it's a problem for writes either as the host wouldn't map such memory as tagged. It's just that it returns zeros and writes are ignored, so we could instead return an error (I haven't checked your latest series yet).

The latest series uses pfn_to_online_page() to reject ZONE_DEVICE early.

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 6 lines
		} else {
			num_tags = mte_copy_tags_from_user(maddr, tags,
							MTE_GRANULES_PER_PAGE);
			kvm_release_pfn_dirty(pfn);
		}
Quoted 7 lines
		if (num_tags != MTE_GRANULES_PER_PAGE) {
			ret = -EFAULT;
			goto out;
		}

		if (write)
			test_and_set_bit(PG_mte_tagged, &page->flags);

I think a set_bit() would do, I doubt it's any more efficient. But why

Quoted 9 lines
Quoted 5 lines

I'd seen test_and_set_bit() used elsewhere (I forget where now) as a slightly more efficient approach. It complies down to a READ_ONCE and a conditional atomic, vs a single non-conditional atomic. But I don't have any actual data on the performance and this isn't a hot path, so I'll switch to the more obvious set_bit().

Yeah, I think I've seen this as well. Anyway, it's probably lost in the noise of tag writing here.

Agreed.

Thanks,

Steve

maintainer
#43 lore raw

Re: [PATCH v12 7/8] KVM: arm64: ioctl to fetch/store tags in a guest

Quoted 30 lines · Steven Price
Quoted 29 lines · Catalin Marinas

I had some (random) thoughts on how to make things simpler, maybe. I think most of these races would have been solved if we required PROT_MTE in the VMM but this has an impact on the VMM if it wants to use MTE itself. If such requirement was in place, all KVM needed to do is check PG_mte_tagged.

So what we actually need is a set_pte_at() in the VMM to clear the tags and set PG_mte_tagged. Currently, we only do this if the memory type is tagged (PROT_MTE) but it's not strictly necessary.

As an optimisation for normal programs, we don't want to do this all the time but the visible behaviour wouldn't change (well, maybe for ptrace slightly). However, it doesn't mean we couldn't for a VMM, with an opt-in via prctl(). This would add a MMCF_MTE_TAG_INIT bit (couldn't think of a better name) to mm_context_t.flags and set_pte_at() would behave as if the pte was tagged without actually mapping the memory in user space as tagged (protection flags not changed). Pages that don't support tagging are still safe, just some unnecessary ignored tag writes. This would need to be set before the mmap() for the guest memory.

If we want finer-grained control we'd have to store this information in the vma flags, in addition to VM_MTE (e.g. VM_MTE_TAG_INIT) but without affecting the actual memory type. The easiest would be another pte bit, though we are short on them. A more intrusive (not too bad) approach is to introduce a set_pte_at_vma() and read the flags directly in the arch code. In most places where set_pte_at() is called on a user mm, the vma is also available.

Quoted 4 lines
Quoted 2 lines

Anyway, I'm not saying we go this route, just thinking out loud, get some opinions.

Does get_user_pages() actually end up calling set_pte_at() normally?

Not always, at least as how it's called from hva_to_pfn(). My reading of the get_user_page_fast_only() is that it doesn't touch the pte, just walks the page tables and pins the page. Of course, it expects a valid pte to have been set in the VMM already, otherwise it doesn't pin any page and the caller falls back to the slow path.

The slow path, get_user_pages_unlocked(), passes FOLL_TOUCH and set_pte_at() will be called either in follow_pfn_pte() if it was valid or via faultin_page() -> handle_mm_fault().

Quoted 3 lines

If not then on the normal user_mem_abort() route although we can easily check VM_MTE_TAG_INIT there's no obvious place to hook in to ensure that the pages actually allocated have the PG_mte_tagged flag.

I don't think it helps if we checked such vma flag in user_mem_abort(), we'd still have the race with set_pte_at() on the page flags. What I was trying to avoid is touching the page flags in too many places, so deferring this always to set_pte_at() in the VMM.

Quoted 8 lines

I'm also not sure how well this would work with the MMU notifiers path in KVM. With MMU notifiers (i.e. the VMM replacing a page in the memslot) there's not even an obvious hook to enforce the VMA flag. So I think we'd end up with something like the sanitise_mte_tags() function to at least check that the PG_mte_tagged flag is set on the pages (assuming that the trigger for the MMU notifier has done the corresponding set_pte_at()). Admittedly this might close the current race documented there.

If we kept this check to the VMM set_pte_at(), I think we can ignore the notifiers.

Quoted 6 lines

It also feels wrong to me to tie this to a process with prctl(), it seems much more normal to implement this as a new mprotect() flag as this is really a memory property not a process property. And I think we'll find some scary corner cases if we try to associate everything back to a process - although I can't instantly think of anything that will actually break.

I agree, tying it to the process looks wrong, only that it's less intrusive. I don't think it would break anything, only potential performance regression. A process would still need to pass PROT_MTE to be able to get tag checking. That's basically what I had in an early MTE implementation with clear_user_page() always zeroing the tags.

I agree with you that a vma flag would be better but it's more complicated without an additional pte bit. We could also miss some updates as mprotect() for example checks for pte_same() before calling set_pte_at() (it would need to check the updated vma flags).

I'll review the latest series but I'm tempted to move the logic in santise_mte_tags() to mte.c and take the big lock in there if PG_mte_tagged is not already set. If we hit performance issues, we can optimise this later to have the page flag set already on creation (new PROT flag, prctl etc.).

Signature · 1 line
Catalin
#44 lore raw

PATCH v12 8/8 KVM: arm64: Document MTE capability and ioctl

In reply to Steven Price

A new capability (KVM_CAP_ARM_MTE) identifies that the kernel supports granting a guest access to the tags, and provides a mechanism for the VMM to enable it.

A new ioctl (KVM_ARM_MTE_COPY_TAGS) provides a simple way for a VMM to access the tags of a guest without having to maintain a PROT_MTE mapping in userspace. The above capability gates access to the ioctl.

---
 Documentation/virt/kvm/api.rst | 53 ++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
Documentation/virt/kvm/api.rst +53 -0
index 22d077562149..a31661b870ba 100644
@@ -5034,6 +5034,40 @@ see KVM_XEN_VCPU_SET_ATTR above.
 The KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST type may not be used
 with the KVM_XEN_VCPU_GET_ATTR ioctl.
 
+4.130 KVM_ARM_MTE_COPY_TAGS
+---------------------------
+
+:Capability: KVM_CAP_ARM_MTE
+:Architectures: arm64
+:Type: vm ioctl
+:Parameters: struct kvm_arm_copy_mte_tags
+:Returns: 0 on success, < 0 on error
+
+::
+
+  struct kvm_arm_copy_mte_tags {
+	__u64 guest_ipa;
+	__u64 length;
+	union {
+		void __user *addr;
+		__u64 padding;
+	};
+	__u64 flags;
+	__u64 reserved[2];
+  };
+
+Copies Memory Tagging Extension (MTE) tags to/from guest tag memory. The
+``guest_ipa`` and ``length`` fields must be ``PAGE_SIZE`` aligned. The ``addr``
+fieldmust point to a buffer which the tags will be copied to or from.
+
+``flags`` specifies the direction of copy, either ``KVM_ARM_TAGS_TO_GUEST`` or
+``KVM_ARM_TAGS_FROM_GUEST``.
+
+The size of the buffer to store the tags is ``(length / MTE_GRANULE_SIZE)``
+bytes (i.e. 1/16th of the corresponding size). Each byte contains a single tag
+value. This matches the format of ``PTRACE_PEEKMTETAGS`` and
+``PTRACE_POKEMTETAGS``.
+
 5. The kvm_run structure
 ========================
 
@@ -6362,6 +6396,25 @@ default.
 
 See Documentation/x86/sgx/2.Kernel-internals.rst for more details.
 
+7.26 KVM_CAP_ARM_MTE
+--------------------
+
+:Architectures: arm64
+:Parameters: none
+
+This capability indicates that KVM (and the hardware) supports exposing the
+Memory Tagging Extensions (MTE) to the guest. It must also be enabled by the
+VMM before the guest will be granted access.
+
+When enabled the guest is able to access tags associated with any memory given
+to the guest. KVM will ensure that the pages are flagged ``PG_mte_tagged`` so
+that the tags are maintained during swap or hibernation of the host; however
+the VMM needs to manually save/restore the tags as appropriate if the VM is
+migrated.
+
+When enabled the VMM may make use of the ``KVM_ARM_MTE_COPY_TAGS`` ioctl to
+perform a bulk copy of tags to/from the guest.
+
 8. Other capabilities.
 ======================
 
Signature · 1 line
2.20.1
maintainer
#45 lore raw

Re: [PATCH v12 8/8] KVM: arm64: Document MTE capability and ioctl

Quoted 33 lines · Steven Price

A new capability (KVM_CAP_ARM_MTE) identifies that the kernel supports granting a guest access to the tags, and provides a mechanism for the VMM to enable it.

A new ioctl (KVM_ARM_MTE_COPY_TAGS) provides a simple way for a VMM to access the tags of a guest without having to maintain a PROT_MTE mapping in userspace. The above capability gates access to the ioctl.

---
 Documentation/virt/kvm/api.rst | 53 ++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
Documentation/virt/kvm/api.rst +11 -0
index 22d077562149..a31661b870ba 100644
@@ -5034,6 +5034,40 @@ see KVM_XEN_VCPU_SET_ATTR above.
 The KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST type may not be used
 with the KVM_XEN_VCPU_GET_ATTR ioctl.
 
+4.130 KVM_ARM_MTE_COPY_TAGS
+---------------------------
+
+:Capability: KVM_CAP_ARM_MTE
+:Architectures: arm64
+:Type: vm ioctl
+:Parameters: struct kvm_arm_copy_mte_tags
+:Returns: 0 on success, < 0 on error
+
+::
+

Documentation/virt/kvm/api.rst see KVM_XEN_VCPU_SET_ATTR above. · line 5057

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+  struct kvm_arm_copy_mte_tags {
+	__u64 guest_ipa;
+	__u64 length;
+	union {
+		void __user *addr;
+		__u64 padding;
+	};
+	__u64 flags;
+	__u64 reserved[2];
+  };

This doesn't exactly match the structure in the previous patch :-(.

Documentation/virt/kvm/api.rst see KVM_XEN_VCPU_SET_ATTR above. · line 5066

Quoted 9 lines
Documentation/virt/kvm/api.rst +9 -0
+
+Copies Memory Tagging Extension (MTE) tags to/from guest tag memory. The
+``guest_ipa`` and ``length`` fields must be ``PAGE_SIZE`` aligned. The ``addr``
+fieldmust point to a buffer which the tags will be copied to or from.
+
+``flags`` specifies the direction of copy, either ``KVM_ARM_TAGS_TO_GUEST`` or
+``KVM_ARM_TAGS_FROM_GUEST``.
+
+The size of the buffer to store the tags is ``(length / MTE_GRANULE_SIZE)``

Should we add a UAPI definition for MTE_GRANULE_SIZE?

Quoted 20 lines
Documentation/virt/kvm/api.rst +13 -0
+bytes (i.e. 1/16th of the corresponding size). Each byte contains a single tag
+value. This matches the format of ``PTRACE_PEEKMTETAGS`` and
+``PTRACE_POKEMTETAGS``.
+
 5. The kvm_run structure
 ========================
 
@@ -6362,6 +6396,25 @@ default.
 
 See Documentation/x86/sgx/2.Kernel-internals.rst for more details.
 
+7.26 KVM_CAP_ARM_MTE
+--------------------
+
+:Architectures: arm64
+:Parameters: none
+
+This capability indicates that KVM (and the hardware) supports exposing the
+Memory Tagging Extensions (MTE) to the guest. It must also be enabled by the
+VMM before the guest will be granted access.

Documentation/virt/kvm/api.rst default. · line 6417

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+
+When enabled the guest is able to access tags associated with any memory given
+to the guest. KVM will ensure that the pages are flagged ``PG_mte_tagged`` so
+that the tags are maintained during swap or hibernation of the host; however
+the VMM needs to manually save/restore the tags as appropriate if the VM is
+migrated.
+
+When enabled the VMM may make use of the ``KVM_ARM_MTE_COPY_TAGS`` ioctl to
+perform a bulk copy of tags to/from the guest.
+

Missing limitation to AArch64 guests.

Thanks,

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

Re: [PATCH v12 8/8] KVM: arm64: Document MTE capability and ioctl

Quoted 39 lines · Marc Zyngier
Quoted 37 lines · Steven Price

A new capability (KVM_CAP_ARM_MTE) identifies that the kernel supports granting a guest access to the tags, and provides a mechanism for the VMM to enable it.

A new ioctl (KVM_ARM_MTE_COPY_TAGS) provides a simple way for a VMM to access the tags of a guest without having to maintain a PROT_MTE mapping in userspace. The above capability gates access to the ioctl.

---
 Documentation/virt/kvm/api.rst | 53 ++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
Documentation/virt/kvm/api.rst +15 -0
index 22d077562149..a31661b870ba 100644
@@ -5034,6 +5034,40 @@ see KVM_XEN_VCPU_SET_ATTR above.
 The KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST type may not be used
 with the KVM_XEN_VCPU_GET_ATTR ioctl.
 
+4.130 KVM_ARM_MTE_COPY_TAGS
+---------------------------
+
+:Capability: KVM_CAP_ARM_MTE
+:Architectures: arm64
+:Type: vm ioctl
+:Parameters: struct kvm_arm_copy_mte_tags
+:Returns: 0 on success, < 0 on error
+
+::
+
+  struct kvm_arm_copy_mte_tags {
+	__u64 guest_ipa;
+	__u64 length;
+	union {
Quoted 8 lines

Documentation/virt/kvm/api.rst see KVM_XEN_VCPU_SET_ATTR above. · line 5057

Quoted 6 lines
Documentation/virt/kvm/api.rst +6 -0
+		void __user *addr;
+		__u64 padding;
+	};
+	__u64 flags;
+	__u64 reserved[2];
+  };

This doesn't exactly match the structure in the previous patch :-(.

:( I knew there was a reason I didn't include it in the documentation for the first 9 versions... I'll fix this up, thanks for spotting it.

Quoted 11 lines

Documentation/virt/kvm/api.rst see KVM_XEN_VCPU_SET_ATTR above. · line 5066

Quoted 9 lines
Documentation/virt/kvm/api.rst +9 -0
+
+Copies Memory Tagging Extension (MTE) tags to/from guest tag memory. The
+``guest_ipa`` and ``length`` fields must be ``PAGE_SIZE`` aligned. The ``addr``
+fieldmust point to a buffer which the tags will be copied to or from.
+
+``flags`` specifies the direction of copy, either ``KVM_ARM_TAGS_TO_GUEST`` or
+``KVM_ARM_TAGS_FROM_GUEST``.
+
+The size of the buffer to store the tags is ``(length / MTE_GRANULE_SIZE)``

Should we add a UAPI definition for MTE_GRANULE_SIZE?

I wasn't sure whether to export this or not. The ioctl is based around the existing ptrace interface (PTRACE_{PEEK,POKE}MTETAGS) which doesn't expose a UAPI definition. Admittedly the documentation there also just says "16-byte granule" rather than MTE_GRANULE_SIZE.

So I'll just remove the reference to MTE_GRANULE_SIZE in the documentation unless you feel that we should have a UAPI definition.

Quoted 24 lines
Quoted 24 lines
Documentation/virt/kvm/api.rst +17 -0
+bytes (i.e. 1/16th of the corresponding size). Each byte contains a single tag
+value. This matches the format of ``PTRACE_PEEKMTETAGS`` and
+``PTRACE_POKEMTETAGS``.
+
 5. The kvm_run structure
 ========================
 
@@ -6362,6 +6396,25 @@ default.
 
 See Documentation/x86/sgx/2.Kernel-internals.rst for more details.
 
+7.26 KVM_CAP_ARM_MTE
+--------------------
+
+:Architectures: arm64
+:Parameters: none
+
+This capability indicates that KVM (and the hardware) supports exposing the
+Memory Tagging Extensions (MTE) to the guest. It must also be enabled by the
+VMM before the guest will be granted access.
+
+When enabled the guest is able to access tags associated with any memory given
+to the guest. KVM will ensure that the pages are flagged ``PG_mte_tagged`` so
+that the tags are maintained during swap or hibernation of the host; however
Quoted 8 lines

Documentation/virt/kvm/api.rst default. · line 6417

Quoted 6 lines
Documentation/virt/kvm/api.rst +6 -0
+the VMM needs to manually save/restore the tags as appropriate if the VM is
+migrated.
+
+When enabled the VMM may make use of the ``KVM_ARM_MTE_COPY_TAGS`` ioctl to
+perform a bulk copy of tags to/from the guest.
+

Missing limitation to AArch64 guests.

As mentioned previously it's not technically limited to AArch64, but I'll expand this to make it clear that MTE isn't usable from a AArch32 VCPU.

Thanks,

Steve

maintainer
#47 lore raw

Re: [PATCH v12 8/8] KVM: arm64: Document MTE capability and ioctl

Quoted 65 lines · Steven Price
Quoted 39 lines · Marc Zyngier
Quoted 37 lines · Steven Price

A new capability (KVM_CAP_ARM_MTE) identifies that the kernel supports granting a guest access to the tags, and provides a mechanism for the VMM to enable it.

A new ioctl (KVM_ARM_MTE_COPY_TAGS) provides a simple way for a VMM to access the tags of a guest without having to maintain a PROT_MTE mapping in userspace. The above capability gates access to the ioctl.

---
 Documentation/virt/kvm/api.rst | 53 ++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
Documentation/virt/kvm/api.rst +15 -0
index 22d077562149..a31661b870ba 100644
@@ -5034,6 +5034,40 @@ see KVM_XEN_VCPU_SET_ATTR above.
 The KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST type may not be used
 with the KVM_XEN_VCPU_GET_ATTR ioctl.
 
+4.130 KVM_ARM_MTE_COPY_TAGS
+---------------------------
+
+:Capability: KVM_CAP_ARM_MTE
+:Architectures: arm64
+:Type: vm ioctl
+:Parameters: struct kvm_arm_copy_mte_tags
+:Returns: 0 on success, < 0 on error
+
+::
+
+  struct kvm_arm_copy_mte_tags {
+	__u64 guest_ipa;
+	__u64 length;
+	union {
Quoted 8 lines

Documentation/virt/kvm/api.rst see KVM_XEN_VCPU_SET_ATTR above. · line 5057

Quoted 6 lines
Documentation/virt/kvm/api.rst +6 -0
+		void __user *addr;
+		__u64 padding;
+	};
+	__u64 flags;
+	__u64 reserved[2];
+  };

This doesn't exactly match the structure in the previous patch :-(.

:( I knew there was a reason I didn't include it in the documentation for the first 9 versions... I'll fix this up, thanks for spotting it.

Quoted 11 lines

Documentation/virt/kvm/api.rst see KVM_XEN_VCPU_SET_ATTR above. · line 5066

Quoted 9 lines
Documentation/virt/kvm/api.rst +9 -0
+
+Copies Memory Tagging Extension (MTE) tags to/from guest tag memory. The
+``guest_ipa`` and ``length`` fields must be ``PAGE_SIZE`` aligned. The ``addr``
+fieldmust point to a buffer which the tags will be copied to or from.
+
+``flags`` specifies the direction of copy, either ``KVM_ARM_TAGS_TO_GUEST`` or
+``KVM_ARM_TAGS_FROM_GUEST``.
+
+The size of the buffer to store the tags is ``(length / MTE_GRANULE_SIZE)``

Should we add a UAPI definition for MTE_GRANULE_SIZE?

Quoted 7 lines

I wasn't sure whether to export this or not. The ioctl is based around the existing ptrace interface (PTRACE_{PEEK,POKE}MTETAGS) which doesn't expose a UAPI definition. Admittedly the documentation there also just says "16-byte granule" rather than MTE_GRANULE_SIZE.

So I'll just remove the reference to MTE_GRANULE_SIZE in the documentation unless you feel that we should have a UAPI definition.

Dropping the mention of this symbol and replacing it by the value 16 matches the architecture and doesn't require any extra UAPI definition, so let's just do that.

Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
Documentation/virt/kvm/api.rst +17 -0
+bytes (i.e. 1/16th of the corresponding size). Each byte contains a single tag
+value. This matches the format of ``PTRACE_PEEKMTETAGS`` and
+``PTRACE_POKEMTETAGS``.
+
 5. The kvm_run structure
 ========================
 
@@ -6362,6 +6396,25 @@ default.
 
 See Documentation/x86/sgx/2.Kernel-internals.rst for more details.
 
+7.26 KVM_CAP_ARM_MTE
+--------------------
+
+:Architectures: arm64
+:Parameters: none
+
+This capability indicates that KVM (and the hardware) supports exposing the
+Memory Tagging Extensions (MTE) to the guest. It must also be enabled by the
+VMM before the guest will be granted access.
+
+When enabled the guest is able to access tags associated with any memory given
+to the guest. KVM will ensure that the pages are flagged ``PG_mte_tagged`` so
+that the tags are maintained during swap or hibernation of the host; however
Quoted 8 lines

Documentation/virt/kvm/api.rst default. · line 6417

Quoted 6 lines
Documentation/virt/kvm/api.rst +6 -0
+the VMM needs to manually save/restore the tags as appropriate if the VM is
+migrated.
+
+When enabled the VMM may make use of the ``KVM_ARM_MTE_COPY_TAGS`` ioctl to
+perform a bulk copy of tags to/from the guest.
+

Missing limitation to AArch64 guests.

Quoted 2 lines

As mentioned previously it's not technically limited to AArch64, but I'll expand this to make it clear that MTE isn't usable from a AArch32 VCPU.

I believe the architecture is quite clear that it *is* limited to AArch64. The clarification is welcome though.

	M.
Signature · 5 lines
Without deviation from the norm, progress is not possible.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#48 lore raw

Re: [PATCH v12 8/8] KVM: arm64: Document MTE capability and ioctl

Quoted 6 lines · Marc Zyngier
Quoted 4 lines · Steven Price
Quoted 2 lines · Marc Zyngier

On Mon, 17 May 2021 13:32:39 +0100,
Steven Price <[email protected]> wrote:

[...]>>>> +bytes (i.e. 1/16th of the corresponding size). Each byte contains a single tag

Quoted 32 lines
Quoted 32 lines
Quoted 31 lines
Quoted 19 lines
Documentation/virt/kvm/api.rst +12 -0
+value. This matches the format of ``PTRACE_PEEKMTETAGS`` and
+``PTRACE_POKEMTETAGS``.
+
 5. The kvm_run structure
 ========================
@@ -6362,6 +6396,25 @@ default.
 
 See Documentation/x86/sgx/2.Kernel-internals.rst for more details.
 
+7.26 KVM_CAP_ARM_MTE
+--------------------
+
+:Architectures: arm64
+:Parameters: none
+
+This capability indicates that KVM (and the hardware) supports exposing the
+Memory Tagging Extensions (MTE) to the guest. It must also be enabled by the
+VMM before the guest will be granted access.

Documentation/virt/kvm/api.rst default. · line 6417

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+
+When enabled the guest is able to access tags associated with any memory given
+to the guest. KVM will ensure that the pages are flagged ``PG_mte_tagged`` so
+that the tags are maintained during swap or hibernation of the host; however
+the VMM needs to manually save/restore the tags as appropriate if the VM is
+migrated.
+
+When enabled the VMM may make use of the ``KVM_ARM_MTE_COPY_TAGS`` ioctl to
+perform a bulk copy of tags to/from the guest.
+

Missing limitation to AArch64 guests.

Quoted 5 lines
Quoted 2 lines

As mentioned previously it's not technically limited to AArch64, but I'll expand this to make it clear that MTE isn't usable from a AArch32 VCPU.

I believe the architecture is quite clear that it *is* limited to AArch64. The clarification is welcome though.

I explained that badly. A system supporting MTE doesn't have to have all CPUs running AArch64 - fairly obviously you can boot a 32 bit OS on a system supporting AArch64.

Since the KVM capability is a VM capability it's not architecturally inconsistent to enable it even if all your CPUs are running AArch32 (at EL1 and lower) - just a bit pointless.

However, given your comment that a mixture of AArch32/AArch64 VCPUs is a bug - we can fail creation of AArch32 VCPUs and I'll explicitly document this is a AArch64 only feature.

Thanks,

Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm