Skip to contentSkip to conversation

xen/riscv: introduce p2m functionality

PATCH Superseded Oleksii Kurochko 5 participants 161 messages Jun 10, 2025 – Jul 29, 2025 RISC-V Page 1 of 2

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v2 cover xen/riscv: introduce p2m functionality

In this patch series are introduced necessary functions to build and manage RISC-V guest page tables and MMIO/RAM mappings.

CI tests:
https://gitlab.com/xen-project/people/olkur/xen/-/pipelines/1862284573

---
Changes in V2:
- Merged to staging:
- [PATCH v1 1/6] xen/riscv: add inclusion of xen/bitops.h to asm/cmpxchg.h
- New patches:
- xen/riscv: implement sbi_remote_hfence_gvma{_vmid}().
- Split patch "xen/riscv: implement p2m mapping functionality" into smaller one patches:
- xen/riscv: introduce page_set_xenheap_gfn()
- xen/riscv: implement guest_physmap_add_entry() for mapping GFNs to MFNs
- xen/riscv: implement p2m_set_entry() and __p2m_set_entry()
- xen/riscv: Implement p2m_free_entry() and related helpers
- xen/riscv: Implement superpage splitting for p2m mappings
- xen/riscv: implement p2m_next_level()
- xen/riscv: Implement p2m_entry_from_mfn() and support PBMT configuration
- Move root p2m table allocation to separate patch:
xen/riscv: add root page table allocation
- Drop dependency of this patch series from the patch witn an introduction of SvPBMT as it was merged.
- Patch "[PATCH v1 4/6] xen/riscv: define pt_t and pt_walk_t structures" was renamed to xen/riscv: introduce pte_{set,get}_mfn() as after dropping of bitfields for PTE structure, this patch introduce only pte_{set,get}_mfn().
- Rename "xen/riscv: define pt_t and pt_walk_t structures" to "xen/riscv: introduce pte_{set,get}_mfn()" as pt_t and pt_walk_t were dropped.
- Introduce guest domain's VMID allocation and manegement.
- Add patches necessary to implement p2m lookup:
- xen/riscv: implement mfn_valid() and page reference, ownership handling helpers
- xen/riscv: add support of page lookup by GFN
- Re-sort patch series.
- Add link to CI tests.
- All other changes are patch-specific. Please check them.
---

Oleksii Kurochko (17):
xen/riscv: implement sbi_remote_hfence_gvma()
xen/riscv: introduce sbi_remote_hfence_gvma_vmid() xen/riscv: introduce guest domain's VMID allocation and manegement xen/riscv: construct the P2M pages pool for guests xen/riscv: introduce things necessary for p2m initialization xen/riscv: add root page table allocation
xen/riscv: introduce pte_{set,get}_mfn()
xen/riscv: add new p2m types and helper macros for type classification xen/riscv: introduce page_set_xenheap_gfn()
xen/riscv: implement guest_physmap_add_entry() for mapping GFNs to
MFNs
xen/riscv: implement p2m_set_entry() and __p2m_set_entry() xen/riscv: Implement p2m_free_entry() and related helpers xen/riscv: Implement p2m_entry_from_mfn() and support PBMT
configuration
xen/riscv: implement p2m_next_level()
xen/riscv: Implement superpage splitting for p2m mappings xen/riscv: implement mfn_valid() and page reference, ownership
handling helpers
xen/riscv: add support of page lookup by GFN

 xen/arch/riscv/Makefile                     |    1 +
 xen/arch/riscv/include/asm/domain.h         |   16 +
 xen/arch/riscv/include/asm/mm.h             |   45 +-
 xen/arch/riscv/include/asm/p2m.h            |  125 +-
 xen/arch/riscv/include/asm/page.h           |   33 +
 xen/arch/riscv/include/asm/riscv_encoding.h |    4 +
 xen/arch/riscv/include/asm/sbi.h            |   38 +
 xen/arch/riscv/mm.c                         |   97 +-
 xen/arch/riscv/p2m.c                        | 1188 +++++++++++++++++++
 xen/arch/riscv/sbi.c                        |   18 +
 xen/arch/riscv/setup.c                      |    3 +
 11 files changed, 1550 insertions(+), 18 deletions(-)
 create mode 100644 xen/arch/riscv/p2m.c
Signature · 1 line
2.49.0
#1 lore raw

PATCH v2 1/17 xen/riscv: implement sbi_remote_hfence_gvma()

Instruct the remote harts to execute one or more HFENCE.GVMA instructions, covering the range of guest physical addresses between start_addr and start_addr + size for all the guests.

The remote fence operation applies to the entire address space if either:
- start_addr and size are both 0, or
- size is equal to 2^XLEN-1.

---
Changes in V2:
- New patch.

---
 xen/arch/riscv/include/asm/sbi.h | 21 +++++++++++++++++++++
 xen/arch/riscv/sbi.c             |  9 +++++++++
 2 files changed, 30 insertions(+)
xen/arch/riscv/include/asm/sbi.h +21 -0
index 527d773277..8e346347af 100644
@@ -89,6 +89,27 @@ bool sbi_has_rfence(void);
 int sbi_remote_sfence_vma(const cpumask_t *cpu_mask, vaddr_t start,
                           size_t size);
 
+/*
+ * Instructs the remote harts to execute one or more HFENCE.GVMA
+ * instructions, covering the range of guest physical addresses
+ * between start_addr and start_addr + size for all the guests.
+ * This function call is only valid for harts implementing
+ * hypervisor extension.
+ *
+ * Returns 0 if IPI was sent to all the targeted harts successfully
+ * or negative value if start_addr or size is not valid.
+ *
+ * The remote fence operation applies to the entire address space if either:
+ *  - start_addr and size are both 0, or
+ *  - size is equal to 2^XLEN-1.
+ *
+ * @cpu_mask a cpu mask containing all the target CPUs (in Xen space).
+ * @param start virtual address start
+ * @param size virtual address range size
+ */
+int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start,
+                           size_t size);
+
 /*
  * Initialize SBI library
  *
xen/arch/riscv/sbi.c +9 -0
index 4209520389..0613ad1cb0 100644
@@ -258,6 +258,15 @@ int sbi_remote_sfence_vma(const cpumask_t *cpu_mask, vaddr_t start,
                       cpu_mask, start, size, 0, 0);
 }
 
+int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start,
+                           size_t size)
+{
+    ASSERT(sbi_rfence);
+
+    return sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
+                      cpu_mask, start, size, 0, 0);
+}
+
 /* This function must always succeed. */
 #define sbi_get_spec_version()  \
     sbi_ext_base_func(SBI_EXT_BASE_GET_SPEC_VERSION)
Signature · 1 line
2.49.0
#8 lore raw

PATCH v2 2/17 xen/riscv: introduce sbi_remote_hfence_gvma_vmid()

In reply to Oleksii Kurochko

It instructs the remote harts to execute one or more HFENCE.GVMA instructions by making an SBI call, covering the range of guest physical addresses between start_addr and start_addr + size only for the given VMID.

This function call is only valid for harts implementing hypervisor extension.

The remote fence operation applies to the entire address space if either:
- start_addr and size are both 0, or
- size is equal to 2^XLEN-1.

---
Changes in V2:
- New patch.

---
 xen/arch/riscv/include/asm/sbi.h | 17 +++++++++++++++++
 xen/arch/riscv/sbi.c             |  9 +++++++++
 2 files changed, 26 insertions(+)
xen/arch/riscv/include/asm/sbi.h +17 -0
index 8e346347af..2644833eb4 100644
@@ -110,6 +110,23 @@ int sbi_remote_sfence_vma(const cpumask_t *cpu_mask, vaddr_t start,
 int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start,
                            size_t size);
 
+/*
+ * Instruct the remote harts to execute one or more HFENCE.GVMA instructions,
+ * covering the range of guest physical addresses between start_addr and
+ * start_addr + size only for the given VMID. This function call is only
+ * valid for harts implementing hypervisor extension.
+ * The remote fence operation applies to the entire address space if either:
+ *  - start_addr and size are both 0, or
+ *  - size is equal to 2^XLEN-1.
+ *
+ * @cpu_mask a cpu mask containing all the target CPUs (in Xen space).
+ * @param start virtual address start
+ * @param size virtual address range size
+ * @param vmid virtual machine id
+ */
+int sbi_remote_hfence_gvma_vmid(const cpumask_t *cpu_mask, vaddr_t start,
+                                size_t size, unsigned long vmid);
+
 /*
  * Initialize SBI library
  *
xen/arch/riscv/sbi.c +9 -0
index 0613ad1cb0..bfd1193509 100644
@@ -267,6 +267,15 @@ int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start,
                       cpu_mask, start, size, 0, 0);
 }
 
+int sbi_remote_hfence_gvma_vmid(const cpumask_t *cpu_mask, vaddr_t start,
+                           size_t size, unsigned long vmid)
+{
+    ASSERT(sbi_rfence);
+
+    return sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
+                      cpu_mask, start, size, vmid, 0);
+}
+
 /* This function must always succeed. */
 #define sbi_get_spec_version()  \
     sbi_ext_base_func(SBI_EXT_BASE_GET_SPEC_VERSION)
Signature · 1 line
2.49.0
#11 lore raw

PATCH v2 3/17 xen/riscv: introduce guest domain's VMID allocation and manegement

In reply to Oleksii Kurochko

Implementation is based on Arm code with some minor changes:
- Re-define INVALID_VMID.
- Re-define MAX_VMID.
- Add TLB flushing when VMID is re-used.

Also, as a part of this path structure p2m_domain is introduced with vmid member inside it. It is necessary for VMID management functions.

Add a bitmap-based allocator to manage VMID space, supporting up to 127 VMIDs on RV32 and 16,383 on RV64 platforms, in accordance with the architecture's hgatp VMID field (RV32 - 7 bit long, others - 14 bit long).

Reserve the highest VMID as INVALID_VMID to ensure it's not reused.

Implement p2m_alloc_vmid() and p2m_free_vmid() for dynamic allocation and release of VMIDs per domain.

Integrate VMID initialization into p2m_init() and ensured domain-specific TLB flushes on VMID release using sbi_remote_hfence_gvma_vmid().

---
Changes in V2:
- New patch.

---
 xen/arch/riscv/Makefile             |   1 +
 xen/arch/riscv/include/asm/domain.h |   4 +
 xen/arch/riscv/include/asm/p2m.h    |  14 ++++
 xen/arch/riscv/p2m.c                | 115 ++++++++++++++++++++++++++++
 xen/arch/riscv/setup.c              |   3 +
 5 files changed, 137 insertions(+)
 create mode 100644 xen/arch/riscv/p2m.c
xen/arch/riscv/Makefile +1 -0
index a1c145c506..1034f2c9cd 100644
@@ -6,6 +6,7 @@ obj-y += intc.o
 obj-y += irq.o
 obj-y += mm.o
 obj-y += pt.o
+obj-y += p2m.o
 obj-$(CONFIG_RISCV_64) += riscv64/
 obj-y += sbi.o
 obj-y += setup.o
xen/arch/riscv/include/asm/domain.h +4 -0
index c3d965a559..b9a03e91c5 100644
@@ -5,6 +5,8 @@
 #include <xen/xmalloc.h>
 #include <public/hvm/params.h>
 
+#include <asm/p2m.h>
+
 struct hvm_domain
 {
     uint64_t              params[HVM_NR_PARAMS];
@@ -18,6 +20,8 @@ struct arch_vcpu {
 
 struct arch_domain {
     struct hvm_domain hvm;
+
+    struct p2m_domain p2m;
 };
 
 #include <xen/sched.h>
xen/arch/riscv/include/asm/p2m.h +14 -0
index 28f57a74f2..359408e1be 100644
@@ -3,11 +3,21 @@
 #define ASM__RISCV__P2M_H
 
 #include <xen/errno.h>
+#include <xen/types.h>
 
 #include <asm/page-bits.h>
 
 #define paddr_bits PADDR_BITS
 
+/* Get host p2m table */
+#define p2m_get_hostp2m(d) (&(d)->arch.p2m)
+
+/* Per-p2m-table state */
+struct p2m_domain {
+    /* Current VMID in use */
+    uint16_t vmid;
+};
+
 /*
  * List of possible type for each page in the p2m entry.
  * The number of available bit per page in the pte for this purpose is 2 bits.
@@ -93,6 +103,10 @@ static inline void p2m_altp2m_check(struct vcpu *v, uint16_t idx)
     /* Not supported on RISCV. */
 }
 
+void p2m_vmid_allocator_init(void);
+
+int p2m_init(struct domain *d);
+
 #endif /* ASM__RISCV__P2M_H */
 
 /*
xen/arch/riscv/p2m.c new file +115 -0

115 lines not on this page · raw message

xen/arch/riscv/setup.c +3 -0
index 8bcd19218d..aa8f5646ea 100644
@@ -19,6 +19,7 @@
 #include <asm/early_printk.h>
 #include <asm/fixmap.h>
 #include <asm/intc.h>
+#include <asm/p2m.h>
 #include <asm/sbi.h>
 #include <asm/setup.h>
 #include <asm/traps.h>
@@ -134,6 +135,8 @@ void __init noreturn start_xen(unsigned long bootcpu_id,
 
     intc_preinit();
 
+    p2m_vmid_allocator_init();
+
     printk("All set up\n");
 
     machine_halt();
Signature · 1 line
2.49.0
#27 lore raw

PATCH v2 4/17 xen/riscv: construct the P2M pages pool for guests

In reply to Oleksii Kurochko

Implement p2m_set_allocation() to construct p2m pages pool for guests based on required number of pages.

This is implemented by:
- Adding a struct paging_domain which contains a freelist, a counter variable and a spinlock to struct arch_domain to indicate the free p2m pages and the number of p2m total pages in the p2m pages pool.
- Adding a helper p2m_set_allocation to set the p2m pages pool size. This helper should be called before allocating memory for a guest and is called from domain_p2m_set_allocation(), the latter is a part of common dom0less code.

---
Changes in v2:
- Drop the comment above inclusion of <xen/event.h> in riscv/p2m.c.
- Use ACCESS_ONCE() for lhs and rhs for the expressions in p2m_set_allocation().

---
 xen/arch/riscv/include/asm/domain.h | 12 ++++++
 xen/arch/riscv/p2m.c                | 59 +++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+)
xen/arch/riscv/include/asm/domain.h +12 -0
index b9a03e91c5..b818127f9f 100644
@@ -2,6 +2,8 @@
 #ifndef ASM__RISCV__DOMAIN_H
 #define ASM__RISCV__DOMAIN_H
 
+#include <xen/mm.h>
+#include <xen/spinlock.h>
 #include <xen/xmalloc.h>
 #include <public/hvm/params.h>
 
@@ -18,10 +20,20 @@ struct arch_vcpu_io {
 struct arch_vcpu {
 };
 
+struct paging_domain {
+    spinlock_t lock;
+    /* Free P2M pages from the pre-allocated P2M pool */
+    struct page_list_head p2m_freelist;
+    /* Number of pages from the pre-allocated P2M pool */
+    unsigned long p2m_total_pages;
+};
+
 struct arch_domain {
     struct hvm_domain hvm;
 
     struct p2m_domain p2m;
+
+    struct paging_domain paging;
 };
 
 #include <xen/sched.h>
xen/arch/riscv/p2m.c +59 -0

72 lines not on this page · raw message

Signature · 1 line
2.49.0
#39 lore raw

PATCH v2 5/17 xen/riscv: introduce things necessary for p2m initialization

In reply to Oleksii Kurochko

Introduce the following things:
- Update p2m_domain structure, which describe per p2m-table state, with:
- lock to protect updates to p2m.
- pool with pages used to construct p2m.
- clean_pte which indicate if it is requires to clean the cache when writing an entry.
- radix tree to store p2m type as PTE doesn't have enough free bits to store type.
- default_access to store p2m access type for each page in the domain.
- back pointer to domain structure.
- p2m_init() to initalize members introduced in p2m_domain structure.
- Introudce p2m_write_lock() and p2m_is_write_locked().
- Introduce p2m_force_tlb_flush_sync() to flush TLBs after p2m table update.

---
Changes in V2:
- Use introduced erlier sbi_remote_hfence_gvma_vmid() for proper implementation of p2m_force_tlb_flush_sync() as TLB flushing needs to happen for each pCPU which potentially has cached a mapping, what is tracked by d->dirty_cpumask.
- Drop unnecessary blanks.
- Fix code style for # of pre-processor directive.
- Drop max_mapped_gfn and lowest_mapped_gfn as they aren't used now.
- [p2m_init()] Set p2m->clean_pte=false if CONFIG_HAS_PASSTHROUGH=n.
- [p2m_init()] Update the comment above p2m->domain = d;
- Drop p2m->need_flush as it seems to be always true for RISC-V and as a consequence drop p2m_tlb_flush_sync().
- Move to separate patch an introduction of root page table allocation.

---
 xen/arch/riscv/include/asm/p2m.h | 39 +++++++++++++++++++++
 xen/arch/riscv/p2m.c             | 58 ++++++++++++++++++++++++++++++++
 2 files changed, 97 insertions(+)
xen/arch/riscv/include/asm/p2m.h +39 -0
index 359408e1be..9570eff014 100644
@@ -3,6 +3,10 @@
 #define ASM__RISCV__P2M_H
 
 #include <xen/errno.h>
+#include <xen/mem_access.h>
+#include <xen/mm.h>
+#include <xen/radix-tree.h>
+#include <xen/rwlock.h>
 #include <xen/types.h>
 
 #include <asm/page-bits.h>
@@ -14,6 +18,29 @@
 
 /* Per-p2m-table state */
 struct p2m_domain {
+    /*
+     * Lock that protects updates to the p2m.
+     */
+    rwlock_t lock;
+
+    /* Pages used to construct the p2m */
+    struct page_list_head pages;
+
+    /* Indicate if it is required to clean the cache when writing an entry */
+    bool clean_pte;
+
+    struct radix_tree_root p2m_type;
+
+    /*
+     * Default P2M access type for each page in the the domain: new pages,
+     * swapped in pages, cleared pages, and pages that are ambiguously
+     * retyped get this access type.  See definition of p2m_access_t.
+     */
+    p2m_access_t default_access;
+
+    /* Back pointer to domain */
+    struct domain *domain;
+
     /* Current VMID in use */
     uint16_t vmid;
 };
@@ -107,6 +134,18 @@ void p2m_vmid_allocator_init(void);
 
 int p2m_init(struct domain *d);
 
+static inline void p2m_write_lock(struct p2m_domain *p2m)
+{
+    write_lock(&p2m->lock);
+}
+
+void p2m_write_unlock(struct p2m_domain *p2m);
+
+static inline int p2m_is_write_locked(struct p2m_domain *p2m)
+{
+    return rw_is_write_locked(&p2m->lock);
+}
+
 #endif /* ASM__RISCV__P2M_H */
 
 /*
xen/arch/riscv/p2m.c +58 -0

79 lines not on this page · raw message

Signature · 1 line
2.49.0
#46 lore raw

PATCH v2 6/17 xen/riscv: add root page table allocation

In reply to Oleksii Kurochko

Introduce support for allocating and initializing the root page table required for RISC-V stage-2 address translation.

To implement root page table allocation the following is introduced:
- p2m_get_clean_page() and p2m_allocate_root() helpers to allocate and zero a 16 KiB root page table, as mandated by the RISC-V privileged specification for Sv39x4/Sv48x4 modes.
- Add hgatp_from_page() to construct the hgatp register value from the allocated root page.
- Update p2m_init() to allocate the root table and initialize p2m->root and p2m->hgatp.
- Add maddr_to_page() and page_to_maddr() macros for easier address manipulation.
- Allocate root p2m table after p2m pool is initialized.

---
Changes in v2:
- This patch was created from "xen/riscv: introduce things necessary for p2m initialization" with the following changes:
- [clear_and_clean_page()] Add missed call of clean_dcache_va_range().
- Drop p2m_get_clean_page() as it is going to be used only once to allocate root page table. Open-code it explicittly in p2m_allocate_root(). Also, it will help avoid duplication of the code connected to order and nr_pages of p2m root page table.
- Instead of using order 2 for alloc_domheap_pages(), use

     get_order_from_bytes(KB(16)).
   - Clear and clean a proper amount of allocated pages in p2m_allocate_root().
   - Drop _info from the function name hgatp_from_page_info() and its argument
     page_info.
   - Introduce HGATP_MODE_MASK and use MASK_INSR() instead of shift to calculate
     value of hgatp.
   - Drop unnecessary parentheses in definition of page_to_maddr().
   - Add support of VMID.
   - Drop TLB flushing in p2m_alloc_root_table() and do that once when VMID
     is re-used. [Look at p2m_alloc_vmid()]
   - Allocate p2m root table after p2m pool is fully initialized: first
     return pages to p2m pool them allocate p2m root table.
---
 xen/arch/riscv/include/asm/mm.h             |  4 +
 xen/arch/riscv/include/asm/p2m.h            |  6 ++
 xen/arch/riscv/include/asm/riscv_encoding.h |  4 +
 xen/arch/riscv/p2m.c                        | 94 +++++++++++++++++++++
 4 files changed, 108 insertions(+)
xen/arch/riscv/include/asm/mm.h +4 -0
index 01bbd92a06..912bc79e1b 100644
@@ -149,6 +149,10 @@ extern struct page_info *frametable_virt_start;
 #define mfn_to_page(mfn)    (frametable_virt_start + mfn_x(mfn))
 #define page_to_mfn(pg)     _mfn((pg) - frametable_virt_start)
 
+/* Convert between machine addresses and page-info structures. */
+#define maddr_to_page(ma) mfn_to_page(maddr_to_mfn(ma))
+#define page_to_maddr(pg) mfn_to_maddr(page_to_mfn(pg))
+
 static inline void *page_to_virt(const struct page_info *pg)
 {
     return mfn_to_virt(mfn_x(page_to_mfn(pg)));
xen/arch/riscv/include/asm/p2m.h +6 -0
index 9570eff014..a31b05bd50 100644
@@ -26,6 +26,12 @@ struct p2m_domain {
     /* Pages used to construct the p2m */
     struct page_list_head pages;
 
+    /* The root of the p2m tree. May be concatenated */
+    struct page_info *root;
+
+    /* Address Translation Table for the p2m */
+    paddr_t hgatp;
+
     /* Indicate if it is required to clean the cache when writing an entry */
     bool clean_pte;
 
xen/arch/riscv/include/asm/riscv_encoding.h +4 -0
index 6cc8f4eb45..a71b7546ef 100644
@@ -133,11 +133,13 @@
 #define HGATP_MODE_SV48X4		_UL(9)
 
 #define HGATP32_MODE_SHIFT		31
+#define HGATP32_MODE_MASK		_UL(0x80000000)
 #define HGATP32_VMID_SHIFT		22
 #define HGATP32_VMID_MASK		_UL(0x1FC00000)
 #define HGATP32_PPN			_UL(0x003FFFFF)
 
 #define HGATP64_MODE_SHIFT		60
+#define HGATP64_MODE_MASK		_ULL(0xF000000000000000)
 #define HGATP64_VMID_SHIFT		44
 #define HGATP64_VMID_MASK		_ULL(0x03FFF00000000000)
 #define HGATP64_PPN			_ULL(0x00000FFFFFFFFFFF)
@@ -170,6 +172,7 @@
 #define HGATP_VMID_SHIFT		HGATP64_VMID_SHIFT
 #define HGATP_VMID_MASK			HGATP64_VMID_MASK
 #define HGATP_MODE_SHIFT		HGATP64_MODE_SHIFT
+#define HGATP_MODE_MASK			HGATP64_MODE_MASK
 #else
 #define MSTATUS_SD			MSTATUS32_SD
 #define SSTATUS_SD			SSTATUS32_SD
@@ -181,6 +184,7 @@
 #define HGATP_VMID_SHIFT		HGATP32_VMID_SHIFT
 #define HGATP_VMID_MASK			HGATP32_VMID_MASK
 #define HGATP_MODE_SHIFT		HGATP32_MODE_SHIFT
+#define HGATP_MODE_MASK			HGATP32_MODE_MASK
 #endif
 
 #define TOPI_IID_SHIFT			16
xen/arch/riscv/p2m.c +94 -0

105 lines not on this page · raw message

Signature · 1 line
2.49.0
#54 lore raw

PATCH v2 7/17 xen/riscv: introduce pte_{set,get}_mfn()

In reply to Oleksii Kurochko

Introduce helpers pte_{set,get}_mfn() to simplify setting and getting of mfn.

Also, introduce PTE_PPN_MASK and add BUILD_BUG_ON() to be sure that PTE_PPN_MASK remains the same for all MMU modes except Sv32.

---
Changes in V2:
- Patch "[PATCH v1 4/6] xen/riscv: define pt_t and pt_walk_t structures" was renamed to xen/riscv: introduce pte_{set,get}_mfn() as after dropping of bitfields for PTE structure, this patch introduce only pte_{set,get}_mfn().
- As pt_t and pt_walk_t were dropped, update implementation of pte_{set,get}_mfn() to use bit operations and shifts instead of bitfields.
- Introduce PTE_PPN_MASK to be able to use MASK_INSR for setting/getting PPN.
- Add BUILD_BUG_ON(RV_STAGE1_MODE > SATP_MODE_SV57) to be sure that when new MMU mode will be added, someone checks that PPN is still bits 53:10.

---
 xen/arch/riscv/include/asm/page.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
xen/arch/riscv/include/asm/page.h +24 -0
index 4cb0179648..1b8b145663 100644
@@ -114,6 +114,30 @@ typedef struct {
 #endif
 } pte_t;
 
+#if RV_STAGE1_MODE != SATP_MODE_SV32
+#define PTE_PPN_MASK _UL(0x3FFFFFFFFFFC00)
+#else
+#define PTE_PPN_MASK _U(0xFFFFFC00)
+#endif
+
+static inline void pte_set_mfn(pte_t *p, mfn_t mfn)
+{
+    /*
+     * At the moment spec provides Sv32 - Sv57.
+     * If one day new MMU mode will be added it will be needed
+     * to check that PPN mask still continue to cover bits 53:10.
+     */
+    BUILD_BUG_ON(RV_STAGE1_MODE > SATP_MODE_SV57);
+
+    p->pte &= ~PTE_PPN_MASK;
+    p->pte |= MASK_INSR(mfn_x(mfn), PTE_PPN_MASK);
+}
+
+static inline mfn_t pte_get_mfn(pte_t p)
+{
+    return _mfn(MASK_EXTR(p.pte, PTE_PPN_MASK));
+}
+
 static inline pte_t paddr_to_pte(paddr_t paddr,
                                  unsigned int permissions)
 {
Signature · 1 line
2.49.0
#56 lore raw

PATCH v2 8/17 xen/riscv: add new p2m types and helper macros for type classification

In reply to Oleksii Kurochko

- Extended p2m_type_t with additional types: p2m_ram_ro, p2m_mmio_direct_dev, p2m_grant_map_{rw,ro}.
- Added macros to classify memory types: P2M_RAM_TYPES, P2M_GRANT_TYPES.
- Introduced helper predicates: p2m_is_ram(), p2m_is_any_ram().

---
Changes in V2:
- Drop stuff connected to foreign mapping as it isn't necessary for RISC-V right now.

---
 xen/arch/riscv/include/asm/p2m.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
xen/arch/riscv/include/asm/p2m.h +20 -0
index a31b05bd50..0c05b58992 100644
@@ -61,8 +61,28 @@ struct p2m_domain {
 typedef enum {
     p2m_invalid = 0,    /* Nothing mapped here */
     p2m_ram_rw,         /* Normal read/write domain RAM */
+    p2m_ram_ro,         /* Read-only; writes are silently dropped */
+    p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */
+    p2m_grant_map_rw,   /* Read/write grant mapping */
+    p2m_grant_map_ro,   /* Read-only grant mapping */
 } p2m_type_t;
 
+/* We use bitmaps and mask to handle groups of types */
+#define p2m_to_mask(t_) BIT(t_, UL)
+
+/* RAM types, which map to real machine frames */
+#define P2M_RAM_TYPES (p2m_to_mask(p2m_ram_rw) | \
+                       p2m_to_mask(p2m_ram_ro))
+
+/* Grant mapping types, which map to a real frame in another VM */
+#define P2M_GRANT_TYPES (p2m_to_mask(p2m_grant_map_rw) | \
+                         p2m_to_mask(p2m_grant_map_ro))
+
+/* Useful predicates */
+#define p2m_is_ram(t_) (p2m_to_mask(t_) & P2M_RAM_TYPES)
+#define p2m_is_any_ram(t_) (p2m_to_mask(t_) & \
+                            (P2M_RAM_TYPES | P2M_GRANT_TYPES))
+
 #include <xen/p2m-common.h>
 
 static inline int get_page_and_type(struct page_info *page,
Signature · 1 line
2.49.0
#72 lore raw

PATCH v2 10/17 xen/riscv: implement guest_physmap_add_entry() for mapping GFNs to MFNs

In reply to Oleksii Kurochko

Introduce an initial implementation of guest_physmap_add_entry() on RISC-V by adding a basic framework to insert guest physical memory mappings. This allows mapping a range of GFNs to MFNs using a placeholder p2m_set_entry() function, which currently returns -EOPNOTSUPP.

Changes included:
- Promoting guest_physmap_add_entry() from a stub to a functional interface calling a new p2m_insert_mapping() helper.
- Adding map_regions_p2mt() for generic mapping purposes.
- Introducing p2m_insert_mapping() and a skeleton for p2m_set_entry() to prepare for future support of actual page table manipulation.
- Enclosing the actual mapping logic within
p2m_write_lock() / p2m_write_unlock() to ensure safe concurrent updates to the P2M.

---
Changes in v2:
- This changes were part of "xen/riscv: implement p2m mapping functionality". No additional signigicant changes were done.

---
 xen/arch/riscv/include/asm/p2m.h | 12 ++++------
 xen/arch/riscv/p2m.c             | 41 ++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 8 deletions(-)
xen/arch/riscv/include/asm/p2m.h +4 -8
index 0c05b58992..af2025b9fd 100644
@@ -118,14 +118,10 @@ static inline int guest_physmap_mark_populate_on_demand(struct domain *d,
     return -EOPNOTSUPP;
 }
 
-static inline int guest_physmap_add_entry(struct domain *d,
-                                          gfn_t gfn, mfn_t mfn,
-                                          unsigned long page_order,
-                                          p2m_type_t t)
-{
-    BUG_ON("unimplemented");
-    return -EINVAL;
-}
+int guest_physmap_add_entry(struct domain *d,
+                            gfn_t gfn, mfn_t mfn,
+                            unsigned long page_order,
+                            p2m_type_t t);
 
 /* Untyped version for RAM only, for compatibility */
 static inline int __must_check
xen/arch/riscv/p2m.c +41 -0

44 lines not on this page · raw message

Signature · 1 line
2.49.0
#68 lore raw

PATCH v2 9/17 xen/riscv: introduce page_set_xenheap_gfn()

In reply to Oleksii Kurochko

Introduce page_set_xenheap_gfn() helper to encode the GFN associated with a Xen heap page directly into the type_info field of struct page_info.

Introduce a GFN field in the type_info of a Xen heap page by reserving 10 bits (sufficient for both Sv32 and Sv39+ modes), and define PGT_gfn_mask and PGT_gfn_width accordingly. This ensures a consistent bit layout across all RISC-V MMU modes, avoiding the need for mode-specific ifdefs.

---
Changes in v2:
- This changes were part of "xen/riscv: implement p2m mapping functionality". No additional changes were done.

---
 xen/arch/riscv/include/asm/mm.h | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)
xen/arch/riscv/include/asm/mm.h +29 -3
index 912bc79e1b..41bf9002d7 100644
@@ -12,6 +12,7 @@
 #include <xen/sections.h>
 #include <xen/types.h>
 
+#include <asm/cmpxchg.h>
 #include <asm/page-bits.h>
 
 extern vaddr_t directmap_virt_start;
@@ -229,9 +230,21 @@ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)
 #define PGT_writable_page PG_mask(1, 1)  /* has writable mappings?         */
 #define PGT_type_mask     PG_mask(1, 1)  /* Bits 31 or 63.                 */
 
-/* Count of uses of this frame as its current type. */
-#define PGT_count_width   PG_shift(2)
-#define PGT_count_mask    ((1UL << PGT_count_width) - 1)
+ /* 9-bit count of uses of this frame as its current type. */
+#define PGT_count_mask    PG_mask(0x3FF, 10)
+
+/*
+ * Sv32 has 22-bit GFN. Sv{39, 48, 57} have 44-bit GFN.
+ * Thereby we can use for `type_info` 10 bits for all modes, having the same
+ * amount of bits for `type_info` for all MMU modes let us avoid introducing
+ * an extra #ifdef to that header:
+ *   if we go with maximum possible bits for count on each configuration
+ *   we would need to have a set of PGT_count_* and PGT_gfn_*).
+ */
+#define PGT_gfn_width     PG_shift(10)
+#define PGT_gfn_mask      (BIT(PGT_gfn_width, UL) - 1)
+
+#define PGT_INVALID_XENHEAP_GFN   _gfn(PGT_gfn_mask)
 
 /*
  * Page needs to be scrubbed. Since this bit can only be set on a page that is
@@ -283,6 +296,19 @@ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)
 
 #define PFN_ORDER(pg) ((pg)->v.free.order)
 
+static inline void page_set_xenheap_gfn(struct page_info *p, gfn_t gfn)
+{
+    gfn_t gfn_ = gfn_eq(gfn, INVALID_GFN) ? PGT_INVALID_XENHEAP_GFN : gfn;
+    unsigned long x, nx, y = p->u.inuse.type_info;
+
+    ASSERT(is_xen_heap_page(p));
+
+    do {
+        x = y;
+        nx = (x & ~PGT_gfn_mask) | gfn_x(gfn_);
+    } while ( (y = cmpxchg(&p->u.inuse.type_info, x, nx)) != x );
+}
+
 extern unsigned char cpu0_boot_stack[];
 
 void setup_initial_pagetables(void);
Signature · 1 line
2.49.0
#80 lore raw

PATCH v2 11/17 xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

In reply to Oleksii Kurochko

This patch introduces p2m_set_entry() and its core helper __p2m_set_entry() for RISC-V, based loosely on the Arm implementation, with several RISC-V-specific modifications.

Key differences include:
- TLB Flushing: RISC-V allows caching of invalid PTEs and does not require break-before-make (BBM). As a result, the flushing logic is simplified. TLB invalidation can be deferred until p2m_write_unlock() is called. Consequently, the p2m->need_flush flag is always considered true and is removed.
- Page Table Traversal: The order of walking the page tables differs from Arm, and this implementation reflects that reversed traversal.
- Macro Adjustments: The macros P2M_ROOT_LEVEL, P2M_ROOT_ORDER, and P2M_ROOT_PAGES are updated to align with the new RISC-V implementation.

The main functionality is in __p2m_set_entry(), which handles mappings aligned to page table block entries (e.g., 1GB, 2MB, or 4KB with 4KB granularity).

p2m_set_entry() breaks a region down into block-aligned mappings and calls __p2m_set_entry() accordingly.

Stub implementations (to be completed later) include:
- p2m_free_entry()
- p2m_next_level()
- p2m_entry_from_mfn()
- p2me_is_valid()

Note: Support for shattering block entries is not implemented in this patch and will be added separately.

Additionally, some straightforward helper functions are now implemented:
- p2m_write_pte()
- p2m_remove_pte()
- p2m_get_root_pointer()

---
Changes in V2:
- New patch. It was a part of a big patch "xen/riscv: implement p2m mapping functionality" which was splitted to smaller.
- Update the way when p2m TLB is flushed:
- RISC-V does't require BBM so there is no need to remove PTE before making new so drop 'if /*pte_is_valid(orig_pte) */' and remove PTE only removing has been requested.
- Drop p2m->need_flush |= !!pte_is_valid(orig_pte); for the case when PTE's removing is happening as RISC-V could cache invalid PTE and thereby it requires to do a flush each time and it doesn't matter if PTE is valid or not at the moment when PTE removing is happening.
- Drop a check if PTE is valid in case of PTE is modified as it was mentioned above as BBM isn't required so TLB flushing could be defered and there is no need to do it before modifying of PTE.
- Drop p2m->need_flush as it seems like it will be always true.
- Drop foreign mapping things as it isn't necessary for RISC-V right now.
- s/p2m_is_valid/p2me_is_valid.
- Move definition and initalization of p2m->{max_mapped_gfn,lowest_mapped_gfn} to this patch.

---
 xen/arch/riscv/include/asm/p2m.h |  16 ++
 xen/arch/riscv/p2m.c             | 260 ++++++++++++++++++++++++++++++-
 2 files changed, 275 insertions(+), 1 deletion(-)
xen/arch/riscv/include/asm/p2m.h +16 -0
index af2025b9fd..fdebd18356 100644
@@ -9,8 +9,13 @@
 #include <xen/rwlock.h>
 #include <xen/types.h>
 
+#include <asm/page.h>
 #include <asm/page-bits.h>
 
+#define P2M_ROOT_LEVEL  HYP_PT_ROOT_LEVEL
+#define P2M_ROOT_ORDER  XEN_PT_LEVEL_ORDER(P2M_ROOT_LEVEL)
+#define P2M_ROOT_PAGES  BIT(P2M_ROOT_ORDER, U)
+
 #define paddr_bits PADDR_BITS
 
 /* Get host p2m table */
@@ -49,6 +54,17 @@ struct p2m_domain {
 
     /* Current VMID in use */
     uint16_t vmid;
+
+    /* Highest guest frame that's ever been mapped in the p2m */
+    gfn_t max_mapped_gfn;
+
+    /*
+     * Lowest mapped gfn in the p2m. When releasing mapped gfn's in a
+     * preemptible manner this is update to track recall where to
+     * resume the search. Apart from during teardown this can only
+     * decrease.
+     */
+    gfn_t lowest_mapped_gfn;
 };
 
 /*
xen/arch/riscv/p2m.c +259 -1

278 lines not on this page · raw message

Signature · 1 line
2.49.0
#103 lore raw

PATCH v2 13/17 xen/riscv: Implement p2m_entry_from_mfn() and support PBMT configuration

In reply to Oleksii Kurochko

This patch adds the initial logic for constructing PTEs from MFNs in the RISC-V p2m subsystem. It includes:
- Implementation of p2m_entry_from_mfn(): Generates a valid PTE using the given MFN, p2m_type_t, and p2m_access_t, including permission encoding and PBMT attribute setup.
- New helper p2m_set_permission(): Encodes access rights (r, w, x) into the PTE based on both p2m type and access permissions.
- p2m_type_radix_set(): Stores the p2m type in a radix tree keyed by the PTE for later retrieval.

PBMT type encoding support:
- Introduces an enum pbmt_type_t to represent the PBMT field values.
- Maps types like p2m_mmio_direct_dev to pbmt_io, others default to pbmt_pma.

---
Changes in V2:
- New patch. It was a part of a big patch "xen/riscv: implement p2m mapping functionality" which was splitted to smaller.

---
 xen/arch/riscv/include/asm/page.h |   8 +++
 xen/arch/riscv/p2m.c              | 103 ++++++++++++++++++++++++++++--
 2 files changed, 107 insertions(+), 4 deletions(-)
xen/arch/riscv/include/asm/page.h +8 -0
index c67b9578c9..1d1054fa5c 100644
@@ -76,6 +76,14 @@
 #define PTE_SMALL       BIT(10, UL)
 #define PTE_POPULATE    BIT(11, UL)
 
+enum pbmt_type_t {
+    pbmt_pma,
+    pbmt_nc,
+    pbmt_io,
+    pbmt_rsvd,
+    pbmt_max,
+};
+
 #define PTE_ACCESS_MASK (PTE_READABLE | PTE_WRITABLE | PTE_EXECUTABLE)
 
 #define PTE_PBMT_MASK   (PTE_PBMT_NOCACHE | PTE_PBMT_IO)
xen/arch/riscv/p2m.c +99 -4

117 lines not on this page · raw message

Signature · 1 line
2.49.0
#97 lore raw

PATCH v2 12/17 xen/riscv: Implement p2m_free_entry() and related helpers

In reply to Oleksii Kurochko

This patch introduces a working implementation of p2m_free_entry() for RISC-V based on ARM's implementation of p2m_free_entry(), enabling proper cleanup of page table entries in the P2M (physical-to-machine) mapping.

Only few things are changed:
- Use p2m_force_flush_sync() instead of p2m_tlb_flush_sync() as latter isn't implemented on RISC-V.
- Introduce and use p2m_type_radix_get() to get a type of p2m entry as RISC-V's PTE doesn't have enough space to store all necessary types so a type is stored in a radix tree.

Key additions include:
- p2m_free_entry(): Recursively frees page table entries at all levels. It handles both regular and superpage mappings and ensures that TLB entries are flushed before freeing intermediate tables.
- p2m_put_page() and helpers:
- p2m_put_4k_page(): Clears GFN from xenheap pages if applicable.
- p2m_put_2m_superpage(): Releases foreign page references in a 2MB superpage.
- p2m_type_radix_get(): Extracts the stored p2m_type from the radix tree using the PTE.
- p2m_free_page(): Returns a page either to the domain's freelist or to the domheap, depending on whether the domain is hardware-backed.

Defines XEN_PT_ENTRIES in asm/page.h to simplify loops over page table entries.

---
Changes in V2:
- New patch. It was a part of a big patch "xen/riscv: implement p2m mapping functionality" which was splitted to smaller.
- s/p2m_is_superpage/p2me_is_superpage.

---
 xen/arch/riscv/include/asm/page.h |   1 +
 xen/arch/riscv/p2m.c              | 144 +++++++++++++++++++++++++++++-
 2 files changed, 142 insertions(+), 3 deletions(-)
xen/arch/riscv/include/asm/page.h +1 -0
index 1b8b145663..c67b9578c9 100644
@@ -22,6 +22,7 @@
 #define XEN_PT_LEVEL_SIZE(lvl)      (_AT(paddr_t, 1) << XEN_PT_LEVEL_SHIFT(lvl))
 #define XEN_PT_LEVEL_MAP_MASK(lvl)  (~(XEN_PT_LEVEL_SIZE(lvl) - 1))
 #define XEN_PT_LEVEL_MASK(lvl)      (VPN_MASK << XEN_PT_LEVEL_SHIFT(lvl))
+#define XEN_PT_ENTRIES              (_AT(unsigned int, 1) << PAGETABLE_ORDER)
 
 /*
  * PTE format:
xen/arch/riscv/p2m.c +141 -3

163 lines not on this page · raw message

Signature · 1 line
2.49.0
#125 lore raw

PATCH v2 14/17 xen/riscv: implement p2m_next_level()

In reply to Oleksii Kurochko

Implement the p2m_next_level() function, which enables traversal and dynamic allocation of intermediate levels (if necessary) in the RISC-V p2m (physical-to-machine) page table hierarchy.

To support this, the following helpers are introduced:
- p2me_is_mapping(): Determines whether a PTE represents a valid mapping.
- page_to_p2m_table(): Constructs non-leaf PTEs pointing to next-level page tables with correct attributes.
- p2m_alloc_page(): Allocates page table pages, supporting both hardware and guest domains.
- p2m_create_table(): Allocates and initializes a new page table page and installs it into the hierarchy.

---
Changes in V2:
- New patch. It was a part of a big patch "xen/riscv: implement p2m mapping functionality" which was splitted to smaller.
- s/p2m_is_mapping/p2me_is_mapping.

---
 xen/arch/riscv/p2m.c | 103 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 101 insertions(+), 2 deletions(-)
xen/arch/riscv/p2m.c +101 -2
index cba04acf38..87dd636b80 100644
@@ -387,6 +387,17 @@ static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
     return p2m_type_radix_get(p2m, pte) != p2m_invalid;
 }
 
+/*
+ * pte_is_* helpers are checking the valid bit set in the
+ * PTE but we have to check p2m_type instead (look at the comment above
+ * p2me_is_valid())
+ * Provide our own overlay to check the valid bit.
+ */
+static inline bool p2me_is_mapping(struct p2m_domain *p2m, pte_t pte)
+{
+    return p2me_is_valid(p2m, pte) && (pte.pte & PTE_ACCESS_MASK);
+}
+
 static inline bool p2me_is_superpage(struct p2m_domain *p2m, pte_t pte,
                                     unsigned int level)
 {
@@ -492,6 +503,70 @@ static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn, p2m_type_t t,
     return e;
 }
 
+/* Generate table entry with correct attributes. */
+static pte_t page_to_p2m_table(struct p2m_domain *p2m, struct page_info *page)
+{
+    /*
+     * Since this function generates a table entry, according to "Encoding
+     * of PTE R/W/X fields," the entry's r, w, and x fields must be set to 0
+     * to point to the next level of the page table.
+     * Therefore, to ensure that an entry is a page table entry,
+     * `p2m_access_n2rwx` is passed to `mfn_to_p2m_entry()` as the access value,
+     * which overrides whatever was passed as `p2m_type_t` and guarantees that
+     * the entry is a page table entry by setting r = w = x = 0.
+     */
+    return p2m_entry_from_mfn(p2m, page_to_mfn(page), p2m_ram_rw, p2m_access_n2rwx);
+}
+
+static struct page_info *p2m_alloc_page(struct domain *d)
+{
+    struct page_info *pg;
+
+    /*
+     * For hardware domain, there should be no limit in the number of pages that
+     * can be allocated, so that the kernel may take advantage of the extended
+     * regions. Hence, allocate p2m pages for hardware domains from heap.
+     */
+    if ( is_hardware_domain(d) )
+    {
+        pg = alloc_domheap_page(d, MEMF_no_owner);
+        if ( pg == NULL )
+            printk(XENLOG_G_ERR "Failed to allocate P2M pages for hwdom.\n");
+    }
+    else
+    {
+        spin_lock(&d->arch.paging.lock);
+        pg = page_list_remove_head(&d->arch.paging.p2m_freelist);
+        spin_unlock(&d->arch.paging.lock);
+    }
+
+    return pg;
+}
+
+/* Allocate a new page table page and hook it in via the given entry. */
+static int p2m_create_table(struct p2m_domain *p2m, pte_t *entry)
+{
+    struct page_info *page;
+    pte_t *p;
+
+    ASSERT(!p2me_is_valid(p2m, *entry));
+
+    page = p2m_alloc_page(p2m->domain);
+    if ( page == NULL )
+        return -ENOMEM;
+
+    page_list_add(page, &p2m->pages);
+
+    p = __map_domain_page(page);
+    clear_page(p);
+
+    unmap_domain_page(p);
+
+    p2m_write_pte(entry, page_to_p2m_table(p2m, page), p2m->clean_pte);
+
+    return 0;
+}
+
 #define GUEST_TABLE_MAP_NONE 0
 #define GUEST_TABLE_MAP_NOMEM 1
 #define GUEST_TABLE_SUPER_PAGE 2
@@ -516,9 +591,33 @@ static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl,
                           unsigned int level, pte_t **table,
                           unsigned int offset)
 {
-    panic("%s: hasn't been implemented yet\n", __func__);
+    pte_t *entry;
+    int ret;
+    mfn_t mfn;
+
+    entry = *table + offset;
+
+    if ( !p2me_is_valid(p2m, *entry) )
+    {
+        if ( !alloc_tbl )
+            return GUEST_TABLE_MAP_NONE;
+
+        ret = p2m_create_table(p2m, entry);
+        if ( ret )
+            return GUEST_TABLE_MAP_NOMEM;
+    }
+
+    /* The function p2m_next_level() is never called at the last level */
+    ASSERT(level != 0);
+    if ( p2me_is_mapping(p2m, *entry) )
+        return GUEST_TABLE_SUPER_PAGE;
+
+    mfn = mfn_from_pte(*entry);
+
+    unmap_domain_page(*table);
+    *table = map_domain_page(mfn);
 
-    return GUEST_TABLE_MAP_NONE;
+    return GUEST_TABLE_NORMAL;
 }
 
 static void p2m_put_foreign_page(struct page_info *pg)
Signature · 1 line
2.49.0
#135 lore raw

PATCH v2 15/17 xen/riscv: Implement superpage splitting for p2m mappings

In reply to Oleksii Kurochko

Add support for down large memory mappings ("superpages") in the RISC-V p2m mapping so that smaller, more precise mappings ("finer-grained entries") can be inserted into lower levels of the page table hierarchy.

To implement that the following is done:
- Introduce p2m_split_superpage(): Recursively shatters a superpage into smaller page table entries down to the target level, preserving original permissions and attributes.
- __p2m_set_entry() updated to invoke superpage splitting when inserting entries at lower levels within a superpage-mapped region.

This implementation is based on the ARM code, with modifications to the part that follows the BBM (break-before-make) approach. Unlike ARM, RISC-V does not require BBM, so there is no need to invalidate the PTE and flush the TLB before updating it with the newly created, split page table. Additionally, the page table walk logic has been adjusted, as ARM uses the opposite walk order compared to RISC-V.

---
Changes in V2:
- New patch. It was a part of a big patch "xen/riscv: implement p2m mapping functionality" which was splitted to smaller.
- Update the commit above the cycle which creates new page table as RISC-V travserse page tables in an opposite to ARM order.
- RISC-V doesn't require BBM so there is no needed for invalidating and TLB flushing before updating PTE.

---
 xen/arch/riscv/p2m.c | 102 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 101 insertions(+), 1 deletion(-)
xen/arch/riscv/p2m.c +101 -1
index 87dd636b80..79c4473f1f 100644
@@ -743,6 +743,77 @@ static void p2m_free_entry(struct p2m_domain *p2m,
     p2m_free_page(p2m->domain, pg);
 }
 
+static bool p2m_split_superpage(struct p2m_domain *p2m, pte_t *entry,
+                                unsigned int level, unsigned int target,
+                                const unsigned int *offsets)
+{
+    struct page_info *page;
+    unsigned int i;
+    pte_t pte, *table;
+    bool rv = true;
+
+    /* Convenience aliases */
+    mfn_t mfn = pte_get_mfn(*entry);
+    unsigned int next_level = level - 1;
+    unsigned int level_order = XEN_PT_LEVEL_ORDER(next_level);
+
+    /*
+     * This should only be called with target != level and the entry is
+     * a superpage.
+     */
+    ASSERT(level > target);
+    ASSERT(p2me_is_superpage(p2m, *entry, level));
+
+    page = p2m_alloc_page(p2m->domain);
+    if ( !page )
+        return false;
+
+    page_list_add(page, &p2m->pages);
+    table = __map_domain_page(page);
+
+    /*
+     * We are either splitting a second level 1G page into 512 first level
+     * 2M pages, or a first level 2M page into 512 zero level 4K pages.
+     */
+    for ( i = 0; i < XEN_PT_ENTRIES; i++ )
+    {
+        pte_t *new_entry = table + i;
+
+        /*
+         * Use the content of the superpage entry and override
+         * the necessary fields. So the correct permission are kept.
+         */
+        pte = *entry;
+        pte_set_mfn(&pte, mfn_add(mfn, i << level_order));
+
+        write_pte(new_entry, pte);
+    }
+
+    /*
+     * Shatter superpage in the page to the level we want to make the
+     * changes.
+     * This is done outside the loop to avoid checking the offset to
+     * know whether the entry should be shattered for every entry.
+     */
+    if ( next_level != target )
+        rv = p2m_split_superpage(p2m, table + offsets[next_level],
+                                 level - 1, target, offsets);
+
+    /* TODO: why it is necessary to have clean here? Not somewhere in the caller */
+    if ( p2m->clean_pte )
+        clean_dcache_va_range(table, PAGE_SIZE);
+
+    unmap_domain_page(table);
+
+    /*
+     * Even if we failed, we should install the newly allocated PTE
+     * entry. The caller will be in charge to free the sub-tree.
+     */
+    p2m_write_pte(entry, page_to_p2m_table(p2m, page), p2m->clean_pte);
+
+    return rv;
+}
+
 /*
  * Insert an entry in the p2m. This should be called with a mapping
  * equal to a page/superpage.
@@ -806,7 +877,36 @@ static int __p2m_set_entry(struct p2m_domain *p2m,
      */
     if ( level > target )
     {
-        panic("Shattering isn't implemented\n");
+        /* We need to split the original page. */
+        pte_t split_pte = *entry;
+
+        ASSERT(p2me_is_superpage(p2m, *entry, level));
+
+        if ( !p2m_split_superpage(p2m, &split_pte, level, target, offsets) )
+        {
+            /* Free the allocated sub-tree */
+            p2m_free_entry(p2m, split_pte, level);
+
+            rc = -ENOMEM;
+            goto out;
+        }
+
+        p2m_write_pte(entry, split_pte, p2m->clean_pte);
+
+        /* Then move to the level we want to make real changes */
+        for ( ; level < target; level++ )
+        {
+            rc = p2m_next_level(p2m, true, level, &table, offsets[level]);
+
+            /*
+             * The entry should be found and either be a table
+             * or a superpage if level 0 is not targeted
+             */
+            ASSERT(rc == GUEST_TABLE_NORMAL ||
+                   (rc == GUEST_TABLE_SUPER_PAGE && target > 0));
+        }
+
+        entry = table + offsets[level];
     }
 
     /*
Signature · 1 line
2.49.0
#143 lore raw

PATCH v2 16/17 xen/riscv: implement mfn_valid() and page reference, ownership handling helpers

In reply to Oleksii Kurochko

Implement the mfn_valid() macro to verify whether a given MFN is valid by checking that it falls within the range [start_page, max_page). These bounds are initialized based on the start and end addresses of RAM.

As part of this patch, start_page is introduced and initialized with the PFN of the first RAM page.

Also, after providing a non-stub implementation of the mfn_valid() macro,
the following compilation errors started to occur:
riscv64-linux-gnu-ld: prelink.o: in function `__next_node':
/build/xen/./include/xen/nodemask.h:202: undefined reference to `page_is_ram_type'
riscv64-linux-gnu-ld: prelink.o: in function `get_free_buddy':
/build/xen/common/page_alloc.c:881: undefined reference to `page_is_ram_type'
riscv64-linux-gnu-ld: prelink.o: in function `alloc_heap_pages':
/build/xen/common/page_alloc.c:1043: undefined reference to `page_get_owner_and_reference'
riscv64-linux-gnu-ld: /build/xen/common/page_alloc.c:1098: undefined reference to `page_is_ram_type'
riscv64-linux-gnu-ld: prelink.o: in function `ns16550_interrupt':
/build/xen/drivers/char/ns16550.c:205: undefined reference to `get_page'
riscv64-linux-gnu-ld: ./.xen-syms.0: hidden symbol `page_get_owner_and_reference' isn't defined
riscv64-linux-gnu-ld: final link failed: bad value
make[2]: *** [arch/riscv/Makefile:35: xen-syms] Error 1
To resolve these errors, the following functions have also been introduced,
based on their Arm counterparts:
- page_get_owner_and_reference() and its variant to safely acquire a
reference to a page and retrieve its owner.
- put_page() and put_page_nr() to release page references and free the page
when the count drops to zero.
For put_page_nr(), code related to static memory configuration is wrapped
with CONFIG_STATIC_MEMORY, as this configuration has not yet been moved to
common code. Therefore, PGC_static and free_domstatic_page() are not
introduced for RISC-V. However, since this configuration could be useful
in the future, the relevant code is retained and conditionally compiled.
- A stub for page_is_ram_type() that currently always returns 0 and asserts unreachable, as RAM type checking is not yet implemented.

---
Changes in V2:
- New patch.

---
 xen/arch/riscv/include/asm/mm.h |  9 ++-
 xen/arch/riscv/mm.c             | 97 +++++++++++++++++++++++++++++++--
 2 files changed, 99 insertions(+), 7 deletions(-)
xen/arch/riscv/include/asm/mm.h +7 -2
index 41bf9002d7..bd8511e5f9 100644
@@ -5,6 +5,7 @@
 
 #include <public/xen.h>
 #include <xen/bug.h>
+#include <xen/compiler.h>
 #include <xen/const.h>
 #include <xen/mm-frame.h>
 #include <xen/pdx.h>
@@ -288,8 +289,12 @@ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)
 #define page_get_owner(p)    (p)->v.inuse.domain
 #define page_set_owner(p, d) ((p)->v.inuse.domain = (d))
 
-/* TODO: implement */
-#define mfn_valid(mfn) ({ (void)(mfn); 0; })
+extern unsigned long start_page;
+
+#define mfn_valid(mfn) ({                                   \
+    unsigned long mfn__ = mfn_x(mfn);                       \
+    likely((mfn__ >= start_page) && (mfn__ < max_page));    \
+})
 
 #define domain_set_alloc_bitsize(d) ((void)(d))
 #define domain_clamp_alloc_bitsize(d, b) ((void)(d), (b))
xen/arch/riscv/mm.c +92 -5

118 lines not on this page · raw message

Signature · 1 line
2.49.0
#157 lore raw

PATCH v2 17/17 xen/riscv: add support of page lookup by GFN

In reply to Oleksii Kurochko

Introduce helper functions for safely querying the P2M (physical-to-machine) mapping:
- add p2m_read_lock(), p2m_read_unlock(), and p2m_is_locked() for managing P2M lock state.
- Implement p2m_get_entry() to retrieve mapping details for a given GFN, including MFN, page order, and validity.
- Add p2m_lookup() to encapsulate read-locked MFN retrieval.
- Introduce p2m_get_page_from_gfn() to convert a GFN into a page_info pointer, acquiring a reference to the page if valid.

Implementations are based on Arm's functions with some minor modifications:
- p2m_get_entry():
- Reverse traversal of page tables, as RISC-V uses the opposite order compared to Arm.
- Removed the return of p2m_access_t from p2m_get_entry() since mem_access_settings is not introduced for RISC-V.
- Updated BUILD_BUG_ON() to check using the level 0 mask, which corresponds to Arm's THIRD_MASK.
- Replaced open-coded bit shifts with the BIT() macro.
- Other minor changes, such as using RISC-V-specific functions to validate P2M PTEs, and replacing Arm-specific GUEST_* macros with their RISC-V equivalents.
- p2m_get_page_from_gfn():
- Removed p2m_is_foreign() and related logic, as this functionality is not implemented for RISC-V.

---
Changes in V2:
- New patch.

---
 xen/arch/riscv/include/asm/p2m.h |  18 +++++
 xen/arch/riscv/p2m.c             | 131 +++++++++++++++++++++++++++++++
 2 files changed, 149 insertions(+)
xen/arch/riscv/include/asm/p2m.h +18 -0
index fdebd18356..96e0790dbc 100644
@@ -184,6 +184,24 @@ static inline int p2m_is_write_locked(struct p2m_domain *p2m)
     return rw_is_write_locked(&p2m->lock);
 }
 
+static inline void p2m_read_lock(struct p2m_domain *p2m)
+{
+    read_lock(&p2m->lock);
+}
+
+static inline void p2m_read_unlock(struct p2m_domain *p2m)
+{
+    read_unlock(&p2m->lock);
+}
+
+static inline int p2m_is_locked(struct p2m_domain *p2m)
+{
+    return rw_is_locked(&p2m->lock);
+}
+
+struct page_info *p2m_get_page_from_gfn(struct domain *d, gfn_t gfn,
+                                        p2m_type_t *t);
+
 #endif /* ASM__RISCV__P2M_H */
 
 /*
xen/arch/riscv/p2m.c +131 -0

134 lines not on this page · raw message

Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH v2 01/17] xen/riscv: implement sbi_remote_hfence_gvma()

In reply to Oleksii Kurochko
Quoted 3 lines · Oleksii Kurochko

Instruct the remote harts to execute one or more HFENCE.GVMA instructions, covering the range of guest physical addresses between start_addr and start_addr + size for all the guests.

Here and in the code comment: Why "for all the guests"? Under what conditions would you require such a broad (guest) TLB flush?

xen/arch/riscv/sbi.c int sbi_remote_sfence_vma(const cpumask_t *cpu_mask, vaddr_t start, · line 264

Quoted 10 lines
xen/arch/riscv/sbi.c +4 -0
@@ -258,6 +258,15 @@ int sbi_remote_sfence_vma(const cpumask_t *cpu_mask, vaddr_t start,
                       cpu_mask, start, size, 0, 0);
 }
 
+int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start,
+                           size_t size)
+{
+    ASSERT(sbi_rfence);

As previously indicated, I question the usefulness of such assertions. If the pointer is still NULL, ...

xen/arch/riscv/sbi.c int sbi_remote_sfence_vma(const cpumask_t *cpu_mask, vaddr_t start, · line 267

Quoted 2 lines
xen/arch/riscv/sbi.c +2 -0
+    return sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
+                      cpu_mask, start, size, 0, 0);

... you'll crash here anyway (much like you will in a release build).

Jan

#9 lore raw

Re: [PATCH v2 02/17] xen/riscv: introduce sbi_remote_hfence_gvma_vmid()

In reply to Oleksii Kurochko
Quoted 5 lines · Oleksii Kurochko

It instructs the remote harts to execute one or more HFENCE.GVMA instructions by making an SBI call, covering the range of guest physical addresses between start_addr and start_addr + size only for the given VMID.

This function call is only valid for harts implementing hypervisor extension.

We require H now, don't we? It's also odd to have this here, but not in patch 1.

Quoted 4 lines
xen/arch/riscv/sbi.c +0 -0
@@ -267,6 +267,15 @@ int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start,
                       cpu_mask, start, size, 0, 0);

xen/arch/riscv/sbi.c int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start, · line 277

Quoted 10 lines
xen/arch/riscv/sbi.c +8 -0
 }
 
+int sbi_remote_hfence_gvma_vmid(const cpumask_t *cpu_mask, vaddr_t start,
+                           size_t size, unsigned long vmid)
+{
+    ASSERT(sbi_rfence);
+
+    return sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
+                      cpu_mask, start, size, vmid, 0);
+}

sbi_remote_hfence_gvma() may want implementing in terms of this new function, requiring the patches to be swapped. Provided (see comment there) that helper is actually needed.

Jan

#12 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

In reply to Oleksii Kurochko
Quoted 5 lines · Oleksii Kurochko

Implementation is based on Arm code with some minor changes:
- Re-define INVALID_VMID.
- Re-define MAX_VMID.
- Add TLB flushing when VMID is re-used.

Quoted 8 lines

Also, as a part of this path structure p2m_domain is introduced with vmid member inside it. It is necessary for VMID management functions.

Add a bitmap-based allocator to manage VMID space, supporting up to 127 VMIDs on RV32 and 16,383 on RV64 platforms, in accordance with the architecture's hgatp VMID field (RV32 - 7 bit long, others - 14 bit long).

Reserve the highest VMID as INVALID_VMID to ensure it's not reused.

Why must that VMID not be (re)used? INVALID_VMID can be any value wider than the hgatp.VMID field.

xen/arch/riscv/Makefile obj-y += intc.o · line 9

Quoted 7 lines
xen/arch/riscv/Makefile +1 -0
@@ -6,6 +6,7 @@ obj-y += intc.o
 obj-y += irq.o
 obj-y += mm.o
 obj-y += pt.o
+obj-y += p2m.o

Nit: Numbers typically sort ahead of letters.

Quoted 8 lines
xen/arch/riscv/p2m.c new file +5 -0
@@ -0,0 +1,115 @@
+#include <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/xvmalloc.h>

xen/arch/riscv/p2m.c line 15

Quoted 10 lines
xen/arch/riscv/p2m.c new file +10 -0
+
+#include <asm/p2m.h>
+#include <asm/sbi.h>
+
+static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
+
+/*
+ * hgatp's VMID field is 7 or 14 bits. RV64 may support 14-bit VMID.
+ * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1)
+ * concurrent domains.

Which is pretty limiting especially in the RV32 case. Hence why we don't assign a permanent ID to VMs on x86, but rather manage IDs per-CPU (note:
not per-vCPU).

xen/arch/riscv/p2m.c line 23

Quoted 9 lines

The bitmap space will be allocated dynamically
+ * based on whether 7 or 14 bit VMIDs are supported.
+ */

xen/arch/riscv/p2m.c new file +6 -0
+static unsigned long *vmid_mask;
+static unsigned long *vmid_flushing_needed;
+
+/*
+ * -2 here because:
+ *    - -1 is needed to get the maximal possible VMID

I don't follow this part.

Quoted 1 line

+ * - -1 is reserved for beinng used as INVALID_VMID

Whereas for this part - see above.

xen/arch/riscv/p2m.c line 28

Quoted 4 lines

+ */

xen/arch/riscv/p2m.c new file +3 -0
+#ifdef CONFIG_RISCV_32
+#define MAX_VMID (BIT(7, U) - 2)
+#else

Better "#elif defined(CONFIG_RISCV_64)"?

xen/arch/riscv/p2m.c line 35

Quoted 7 lines
xen/arch/riscv/p2m.c new file +7 -0
+#define MAX_VMID (BIT(14, U) - 2)
+#endif
+
+/* Reserve the max possible VMID to be INVALID. */
+#define INVALID_VMID (MAX_VMID + 1)
+
+void p2m_vmid_allocator_init(void)

__init

xen/arch/riscv/p2m.c line 44

Quoted 9 lines
xen/arch/riscv/p2m.c new file +9 -0
+{
+    /*
+     * Allocate space for vmid_mask and vmid_flushing_needed
+     * based on INVALID_VMID as it is the max possible VMID which just
+     * was reserved to be INVALID_VMID.
+     */
+    vmid_mask = xvzalloc_array(unsigned long, BITS_TO_LONGS(INVALID_VMID));
+    vmid_flushing_needed =
+        xvzalloc_array(unsigned long, BITS_TO_LONGS(INVALID_VMID));

These both want to use MAX_VMID + 1; there's no logical connection here to INVALID_VMID.

Furthermore don't you first need to determine how many bits hgatp.VMID actually implements? The 7 and 14 bits respectively are maximum values only, after all.

VMIDLEN being permitted to be 0, how would you run more than one VM (e.g. Dom0) on such a system?

xen/arch/riscv/p2m.c line 49

Quoted 4 lines
xen/arch/riscv/p2m.c new file +4 -0
+    if ( !vmid_mask || !vmid_flushing_needed )
+        panic("Could not allocate VMID bitmap space or VMID flushing map\n");
+
+    set_bit(INVALID_VMID, vmid_mask);

If (see above) this is really needed, __set_bit() please.

xen/arch/riscv/p2m.c line 52

Quoted 3 lines
xen/arch/riscv/p2m.c new file +3 -0
+}
+
+int p2m_alloc_vmid(struct domain *d)

Looks like this can be static? (p2m_free_vmid() has no caller at all, so it's not clear what use it is going to be.)

xen/arch/riscv/p2m.c line 56

Quoted 4 lines
xen/arch/riscv/p2m.c new file +4 -0
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    int rc, nr;

No need for the blank line between the (few) declarations?

xen/arch/riscv/p2m.c line 60

Quoted 3 lines
xen/arch/riscv/p2m.c new file +3 -0
+    spin_lock(&vmid_alloc_lock);
+
+    nr = find_first_zero_bit(vmid_mask, MAX_VMID);

As per this nr wants to be unsigned int.

xen/arch/riscv/p2m.c line 67

Quoted 6 lines
xen/arch/riscv/p2m.c new file +6 -0
+    ASSERT(nr != INVALID_VMID);
+
+    if ( nr == MAX_VMID )
+    {
+        rc = -EBUSY;
+        printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n", d->domain_id);

Please use %pd.

xen/arch/riscv/p2m.c line 71

Quoted 4 lines
xen/arch/riscv/p2m.c new file +4 -0
+        goto out;
+    }
+
+    set_bit(nr, vmid_mask);

Since you do this under lock, even here __set_bit() ought to be sufficient.

xen/arch/riscv/p2m.c line 75

Quoted 3 lines
xen/arch/riscv/p2m.c new file +3 -0
+    if ( test_bit(p2m->vmid, vmid_flushing_needed) )
+    {
+        clear_bit(p2m->vmid, vmid_flushing_needed);

And __clear_bit() here, or yet better use __test_and_clear_bit() in the if().

Quoted 1 line

+ sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid);

You're creating d; it cannot possibly have run on any CPU yet. IOW d->dirty_cpumask will be reliably empty here. I think it would be hard to avoid issuing the flush to all CPUs here in this scheme.

xen/arch/riscv/p2m.c line 83

Quoted 7 lines
xen/arch/riscv/p2m.c new file +7 -0
+    }
+
+    p2m->vmid = nr;
+
+    rc = 0;
+
+out:

Nit: Style.

Quoted 4 lines
xen/arch/riscv/p2m.c new file +4 -0
+    spin_unlock(&vmid_alloc_lock);
+    return rc;
+}
+

xen/arch/riscv/p2m.c line 97

Quoted 10 lines
xen/arch/riscv/p2m.c new file +10 -0
+void p2m_free_vmid(struct domain *d)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    spin_lock(&vmid_alloc_lock);
+
+    if ( p2m->vmid != INVALID_VMID )
+    {
+        clear_bit(p2m->vmid, vmid_mask);
+        set_bit(p2m->vmid, vmid_flushing_needed);

Does this scheme really avoid any flushes (except near when the system is about to go down)?

As to choice of functions - see above.

xen/arch/riscv/p2m.c line 108

Quoted 11 lines
xen/arch/riscv/p2m.c new file +11 -0
+    }
+
+    spin_unlock(&vmid_alloc_lock);
+}
+
+int p2m_init(struct domain *d)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    int rc;
+
+    p2m->vmid = INVALID_VMID;

Given the absence of callers of p2m_free_vmid() it's also not clear what use this is.

Jan

#28 lore raw

Re: [PATCH v2 04/17] xen/riscv: construct the P2M pages pool for guests

In reply to Oleksii Kurochko
Quoted 8 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/domain.h +4 -0
@@ -18,10 +20,20 @@ struct arch_vcpu_io {
 struct arch_vcpu {
 };
 
+struct paging_domain {
+    spinlock_t lock;
+    /* Free P2M pages from the pre-allocated P2M pool */
+    struct page_list_head p2m_freelist;

xen/arch/riscv/include/asm/domain.h struct arch_vcpu_io { · line 36

Quoted 10 lines
xen/arch/riscv/include/asm/domain.h +6 -0
+    /* Number of pages from the pre-allocated P2M pool */
+    unsigned long p2m_total_pages;
+};
+
 struct arch_domain {
     struct hvm_domain hvm;
 
     struct p2m_domain p2m;
+
+    struct paging_domain paging;

With the separate structures, do you have plans to implement e.g. shadow paging? Or some other paging mode beyond the basic one based on the H extension? If the structures are to remain separate, may I suggest that you keep things properly separated (no matter how e.g. Arm may have it) in terms of naming? I.e. no single "p2m" inside struct paging_domain.

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 110

Quoted 6 lines
xen/arch/riscv/p2m.c +2 -0
@@ -105,6 +106,9 @@ int p2m_init(struct domain *d)
     struct p2m_domain *p2m = p2m_get_hostp2m(d);
     int rc;
 
+    spin_lock_init(&d->arch.paging.lock);
+    INIT_PAGE_LIST_HEAD(&d->arch.paging.p2m_freelist);

If you want p2m and paging to be separate, you will want to put these in a new paging_init().

Jan

#40 lore raw

Re: [PATCH v2 05/17] xen/riscv: introduce things necessary for p2m initialization

In reply to Oleksii Kurochko
Quoted 4 lines · Oleksii Kurochko

Introduce the following things:
- Update p2m_domain structure, which describe per p2m-table state, with:
- lock to protect updates to p2m.
- pool with pages used to construct p2m.

Quoted 8 lines

- clean_pte which indicate if it is requires to clean the cache when writing an entry.
- radix tree to store p2m type as PTE doesn't have enough free bits to store type.
- default_access to store p2m access type for each page in the domain.
- back pointer to domain structure.
- p2m_init() to initalize members introduced in p2m_domain structure.
- Introudce p2m_write_lock() and p2m_is_write_locked().

What about the reader variant? If you don't need that, why not use a simple spin lock?

Quoted 6 lines
xen/arch/riscv/include/asm/p2m.h +2 -0
@@ -14,6 +18,29 @@
 
 /* Per-p2m-table state */
 struct p2m_domain {
+    /*
+     * Lock that protects updates to the p2m.

xen/arch/riscv/include/asm/p2m.h line 32

Quoted 10 lines
xen/arch/riscv/include/asm/p2m.h +10 -0
+     */
+    rwlock_t lock;
+
+    /* Pages used to construct the p2m */
+    struct page_list_head pages;
+
+    /* Indicate if it is required to clean the cache when writing an entry */
+    bool clean_pte;
+
+    struct radix_tree_root p2m_type;

A field with a p2m_ prefix in a p2m struct? And is this tree really about just a single "type"?

xen/arch/riscv/include/asm/p2m.h line 42

Quoted 9 lines
xen/arch/riscv/include/asm/p2m.h +9 -0
+    /*
+     * Default P2M access type for each page in the the domain: new pages,
+     * swapped in pages, cleared pages, and pages that are ambiguously
+     * retyped get this access type.  See definition of p2m_access_t.
+     */
+    p2m_access_t default_access;
+
+    /* Back pointer to domain */
+    struct domain *domain;

This you may want to introduce earlier, to prefer passing around struct p2m_domain * in / to P2M functions (which would benefit earlier patches already, I think).

Quoted 32 lines
xen/arch/riscv/p2m.c +19 -0
@@ -1,13 +1,46 @@
 #include <xen/bitops.h>
+#include <xen/domain_page.h>
 #include <xen/event.h>
+#include <xen/iommu.h>
 #include <xen/lib.h>
+#include <xen/mm.h>
+#include <xen/pfn.h>
+#include <xen/rwlock.h>
 #include <xen/sched.h>
 #include <xen/spinlock.h>
 #include <xen/xvmalloc.h>
 
+#include <asm/page.h>
 #include <asm/p2m.h>
 #include <asm/sbi.h>
 
+/*
+ * Force a synchronous P2M TLB flush.
+ *
+ * Must be called with the p2m lock held.
+ */
+static void p2m_force_tlb_flush_sync(struct p2m_domain *p2m)
+{
+    struct domain *d = p2m->domain;
+
+    ASSERT(p2m_is_write_locked(p2m));
+
+    sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid);
+}

xen/arch/riscv/p2m.c line 39

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+
+/* Unlock the flush and do a P2M TLB flush if necessary */
+void p2m_write_unlock(struct p2m_domain *p2m)
+{
+    /*
+     * The final flush is done with the P2M write lock taken to avoid
+     * someone else modifying the P2M wbefore the TLB invalidation has
+     * completed.
+     */
+    p2m_force_tlb_flush_sync(p2m);

The comment ahead of the function says "if necessary". Yet there's no conditional here. I also question the need for a global flush in all cases.

Quoted 4 lines
xen/arch/riscv/p2m.c +0 -0
@@ -109,8 +142,33 @@ int p2m_init(struct domain *d)
     spin_lock_init(&d->arch.paging.lock);
     INIT_PAGE_LIST_HEAD(&d->arch.paging.p2m_freelist);
 

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 154

Quoted 10 lines
xen/arch/riscv/p2m.c +8 -0
+    rwlock_init(&p2m->lock);
+    INIT_PAGE_LIST_HEAD(&p2m->pages);
+
     p2m->vmid = INVALID_VMID;
 
+    p2m->default_access = p2m_access_rwx;
+
+    radix_tree_init(&p2m->p2m_type);
+
+#ifdef CONFIG_HAS_PASSTHROUGH

Do you expect this to be conditionally selected on RISC-V?

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 161

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+    /*
+     * Some IOMMUs don't support coherent PT walk. When the p2m is
+     * shared with the CPU, Xen has to make sure that the PT changes have
+     * reached the memory
+     */
+    p2m->clean_pte = is_iommu_enabled(d) &&
+        !iommu_has_feature(d, IOMMU_FEAT_COHERENT_WALK);

The comment talks about shared page tables, yet you don't check whether page table sharing is actually enabled for the domain.

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 163

Quoted 2 lines
xen/arch/riscv/p2m.c +2 -0
+#else
+    p2m->clean_pte = false;

I hope the struct starts out zero-filled, in which case you wouldn't need this.

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 170

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+#endif
+
+    /*
+     * "Trivial" initialisation is now complete.  Set the backpointer so the
+     * users of p2m could get an access to domain structure.
+     */
+    p2m->domain = d;

Better set this about the very first thing?

Jan

#3 lore raw

Re: [PATCH v2 01/17] xen/riscv: implement sbi_remote_hfence_gvma()

In reply to Jan Beulich
Quoted 6 lines · Jan Beulich
Quoted 3 lines · Oleksii Kurochko

Instruct the remote harts to execute one or more HFENCE.GVMA instructions, covering the range of guest physical addresses between start_addr and start_addr + size for all the guests.

Here and in the code comment: Why "for all the guests"? Under what conditions would you require such a broad (guest) TLB flush?

Originally, it came from Andrew reply:

```
TLB flushing needs to happen for each pCPU which potentially has cached
a mapping.

In other arches, this is tracked by d->dirty_cpumask which is the bitmap
of pCPUs where this domain is scheduled.

CPUs need to flush their TLBs before removing themselves from
d->dirty_cpumask, which is typically done during context switch, but it
means that to flush the P2M, you only need to IPI a subset of CPUs.
```

But specifically this function was introduced to work in case no VMID support as we can't distinguish which TLB entries belong to which domain. As a result, we have no choice but to flush the entire TLB to avoid incorrect translations.

However, this patch may no longer be necessary, as VMID support has been introduced and|sbi_remote_hfence_gvma_vmid()| will be used instead.

Quoted 14 lines

xen/arch/riscv/sbi.c int sbi_remote_sfence_vma(const cpumask_t *cpu_mask, vaddr_t start, · line 264

Quoted 11 lines
xen/arch/riscv/sbi.c +4 -0
@@ -258,6 +258,15 @@ int sbi_remote_sfence_vma(const cpumask_t *cpu_mask, vaddr_t start,
                        cpu_mask, start, size, 0, 0);
  }
 
+int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start,
+                           size_t size)
+{
+    ASSERT(sbi_rfence);

As previously indicated, I question the usefulness of such assertions. If the pointer is still NULL, ...

Quoted 3 lines

xen/arch/riscv/sbi.c int sbi_remote_sfence_vma(const cpumask_t *cpu_mask, vaddr_t start, · line 267

Quoted 2 lines
xen/arch/riscv/sbi.c +2 -0
+    return sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
+                      cpu_mask, start, size, 0, 0);

... you'll crash here anyway (much like you will in a release build).

I will drop ASSERT() for rfence functions.

Thanks.

~ Oleksii

#10 lore raw

Re: [PATCH v2 02/17] xen/riscv: introduce sbi_remote_hfence_gvma_vmid()

In reply to Jan Beulich
Quoted 7 lines · Jan Beulich
Quoted 5 lines · Oleksii Kurochko

It instructs the remote harts to execute one or more HFENCE.GVMA instructions by making an SBI call, covering the range of guest physical addresses between start_addr and start_addr + size only for the given VMID.

This function call is only valid for harts implementing hypervisor extension.

We require H now, don't we? It's also odd to have this here, but not in patch 1.

Yes, we required it. I will drop this part from the commit message and the comment above declaration of sbi_remote_hfence_gvma_vmid().

Quoted 7 lines
Quoted 7 lines
xen/arch/riscv/sbi.c +0 -0
@@ -267,6 +267,15 @@ int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start,
                        cpu_mask, start, size, 0, 0);
  }
 
Quoted 11 lines

xen/arch/riscv/sbi.c int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start, · line 277

Quoted 8 lines
xen/arch/riscv/sbi.c +8 -0
+int sbi_remote_hfence_gvma_vmid(const cpumask_t *cpu_mask, vaddr_t start,
+                           size_t size, unsigned long vmid)
+{
+    ASSERT(sbi_rfence);
+
+    return sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
+                      cpu_mask, start, size, vmid, 0);
+}

sbi_remote_hfence_gvma() may want implementing in terms of this new function, requiring the patches to be swapped. Provided (see comment there) that helper is actually needed.

It makes sense.
But it seems like there is no need for sbi_remote_hfence_gvma() as we have VMID introduced.

~ Oleksii

#4 lore raw

Re: [PATCH v2 01/17] xen/riscv: implement sbi_remote_hfence_gvma()

In reply to Oleksii Kurochko
Quoted 9 lines · Oleksii Kurochko
Quoted 6 lines · Jan Beulich
Quoted 3 lines · Oleksii Kurochko

Instruct the remote harts to execute one or more HFENCE.GVMA instructions, covering the range of guest physical addresses between start_addr and start_addr + size for all the guests.

Here and in the code comment: Why "for all the guests"? Under what conditions would you require such a broad (guest) TLB flush?

Originally, it came from Andrew reply:

Quoted 11 lines
```
TLB flushing needs to happen for each pCPU which potentially has cached
a mapping.

In other arches, this is tracked by d->dirty_cpumask which is the bitmap
of pCPUs where this domain is scheduled.

CPUs need to flush their TLBs before removing themselves from
d->dirty_cpumask, which is typically done during context switch, but it
means that to flush the P2M, you only need to IPI a subset of CPUs.
```

Hmm, but the word "guest" doesn't even appear there. "Each pCPU" isn't quite the same as "all guests".

Quoted 6 lines

But specifically this function was introduced to work in case no VMID support as we can't distinguish which TLB entries belong to which domain. As a result, we have no choice but to flush the entire TLB to avoid incorrect translations.

However, this patch may no longer be necessary, as VMID support has been introduced and|sbi_remote_hfence_gvma_vmid()| will be used instead.

Good.

Jan

#5 lore raw

Re: [PATCH v2 01/17] xen/riscv: implement sbi_remote_hfence_gvma()

Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
Quoted 6 lines · Jan Beulich
Quoted 3 lines · Oleksii Kurochko

Instruct the remote harts to execute one or more HFENCE.GVMA instructions, covering the range of guest physical addresses between start_addr and start_addr + size for all the guests.

Here and in the code comment: Why "for all the guests"? Under what conditions would you require such a broad (guest) TLB flush?

Originally, it came from Andrew reply:
```
TLB flushing needs to happen for each pCPU which potentially has cached a mapping.

In other arches, this is tracked by d->dirty_cpumask which is the bitmap of pCPUs where this domain is scheduled.

Quoted 6 lines
Quoted 4 lines

CPUs need to flush their TLBs before removing themselves from d->dirty_cpumask, which is typically done during context switch, but it means that to flush the P2M, you only need to IPI a subset of CPUs. ```

Hmm, but the word "guest" doesn't even appear there. "Each pCPU" isn't quite the same as "all guests".

Agree, it is just what SBI spec wording that...

pCPU here it is the first argument of sbi_remote_hfence_gvma(unsigned long hart_mask, ...)

It is even more confusing as based on explaantion if RISC-V priv spec., hfence.gvma it is used to flush G-stage (stage-2) TLB and hfence.vvma it is VS-stage (stage-1) TLB flush.

~ Oleksii

#13 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

In reply to Jan Beulich
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Oleksii Kurochko

Implementation is based on Arm code with some minor changes:
- Re-define INVALID_VMID.
- Re-define MAX_VMID.
- Add TLB flushing when VMID is re-used.

Also, as a part of this path structure p2m_domain is introduced with vmid member inside it. It is necessary for VMID management functions.

Quoted 7 lines
Quoted 5 lines

Add a bitmap-based allocator to manage VMID space, supporting up to 127 VMIDs on RV32 and 16,383 on RV64 platforms, in accordance with the architecture's hgatp VMID field (RV32 - 7 bit long, others - 14 bit long).

Reserve the highest VMID as INVALID_VMID to ensure it's not reused.

Why must that VMID not be (re)used? INVALID_VMID can be any value wider than the hgatp.VMID field.

Oh, agree it could be just any value wider tan hgatp.VMID filed. I forgot about that hgatp.VMID is only 14-bit long value. So we have two extra bits in uint16_t.

Quoted 13 lines
Quoted 13 lines
xen/arch/riscv/p2m.c new file +10 -0
@@ -0,0 +1,115 @@
+#include <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/xvmalloc.h>
+
+#include <asm/p2m.h>
+#include <asm/sbi.h>
+
+static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
Quoted 8 lines

xen/arch/riscv/p2m.c

Quoted 5 lines
xen/arch/riscv/p2m.c new file +5 -0
+
+/*
+ * hgatp's VMID field is 7 or 14 bits. RV64 may support 14-bit VMID.
+ * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1)
+ * concurrent domains.

Which is pretty limiting especially in the RV32 case. Hence why we don't assign a permanent ID to VMs on x86, but rather manage IDs per-CPU (note:
not per-vCPU).

Good point.

I don't believe anyone will use RV32.
For RV64, the available ID space seems sufficiently large.

However, if it turns out that the value isn't large enough even for RV64, I can rework it to manage IDs per physical CPU.
Wouldn't that approach result in more TLB entries being flushed compared to per-vCPU allocation, potentially leading to slightly worse performance?

What about then to allocate VMID per-domain?

Quoted 10 lines

xen/arch/riscv/p2m.c line 23

Quoted 9 lines

The bitmap space will be allocated dynamically
+ * based on whether 7 or 14 bit VMIDs are supported.
+ */

xen/arch/riscv/p2m.c new file +6 -0
+static unsigned long *vmid_mask;
+static unsigned long *vmid_flushing_needed;
+
+/*
+ * -2 here because:
+ *    - -1 is needed to get the maximal possible VMID

I don't follow this part.

Probably, I'm missing something.

hgat.vmid is 7 bit long. BIT(7,U) = 1 << 7 = 128 which is bigger then 7 bit can cover (0b1000_0000 and 0x111_1111). Thereby the MAX_VMID is:
BIT(7, U) - 1 (in case of RV32).

Quoted 5 lines

xen/arch/riscv/p2m.c line 28

Quoted 4 lines

+ */

xen/arch/riscv/p2m.c new file +3 -0
+#ifdef CONFIG_RISCV_32
+#define MAX_VMID (BIT(7, U) - 2)
+#else

Better "#elif defined(CONFIG_RISCV_64)"?

First, I read the spec as for other bitness except 32 it will be 14 bit long, but I re-read it and it is true only for HSXLEN=64, so RV128 will/can have different amount of bit for VMID. I will update to "#elif defined(CONFIG_RISCV_64)" + #error "Define MAX_VMID" if bitness isn't 32 or 64.

Quoted 9 lines

xen/arch/riscv/p2m.c line 44

Quoted 9 lines
xen/arch/riscv/p2m.c new file +9 -0
+{
+    /*
+     * Allocate space for vmid_mask and vmid_flushing_needed
+     * based on INVALID_VMID as it is the max possible VMID which just
+     * was reserved to be INVALID_VMID.
+     */
+    vmid_mask = xvzalloc_array(unsigned long, BITS_TO_LONGS(INVALID_VMID));
+    vmid_flushing_needed =
+        xvzalloc_array(unsigned long, BITS_TO_LONGS(INVALID_VMID));
Quoted 5 lines

These both want to use MAX_VMID + 1; there's no logical connection here to INVALID_VMID.

Furthermore don't you first need to determine how many bits hgatp.VMID actually implements? The 7 and 14 bits respectively are maximum values only, after all.

I missed that it depends on VMIDLEN:

```
The number of VMID bits is UNSPECIFIED and may be zero. The number of implemented VMID bits,
termed VMIDLEN, may be determined by writing one to every bit position in the VMID field, then
reading back the value in hgatp to see which bit positions in the VMID field hold a one. The least-
significant bits of VMID are implemented first: that is, if VMIDLEN > 0, VMID[VMIDLEN-1:0] is
writable. The maximal value of VMIDLEN, termed VMIDMAX, is 7 for Sv32x4 or 14 for Sv39x4,
Sv48x4, and Sv57x4.
```

So yes, I have to determine first how many bits are supported by an implementation.

Quoted 2 lines

VMIDLEN being permitted to be 0, how would you run more than one VM (e.g. Dom0) on such a system?

Hmm, good question.

Then it will be needed to flush TLB on each VM switch by using sbi_remote_hfence_gvma().

Quoted 11 lines

xen/arch/riscv/p2m.c line 49

Quoted 4 lines
xen/arch/riscv/p2m.c new file +4 -0
+    if ( !vmid_mask || !vmid_flushing_needed )
+        panic("Could not allocate VMID bitmap space or VMID flushing map\n");
+
+    set_bit(INVALID_VMID, vmid_mask);

If (see above) this is really needed, __set_bit() please.

xen/arch/riscv/p2m.c line 52

Quoted 3 lines
xen/arch/riscv/p2m.c new file +3 -0
+}
+
+int p2m_alloc_vmid(struct domain *d)

Looks like this can be static? (p2m_free_vmid() has no caller at all, so it's not clear what use it is going to be.)

It really can be static. And p2m_free_vmid() too, but as there is no caller of p2m_free_vmid() probably it makes sense to do in the following way:
/* Uncomment static when p2m_free_vmid() will be called. */ /* static */ void p2m_free_vmid(struct domain *d)
Or just drop for the moment when it will be really needed.

Quoted 9 lines

xen/arch/riscv/p2m.c line 71

Quoted 4 lines
xen/arch/riscv/p2m.c new file +4 -0
+        goto out;
+    }
+
+    set_bit(nr, vmid_mask);

Since you do this under lock, even here __set_bit() ought to be sufficient.

xen/arch/riscv/p2m.c line 75

Quoted 3 lines
xen/arch/riscv/p2m.c new file +3 -0
+    if ( test_bit(p2m->vmid, vmid_flushing_needed) )
+    {
+        clear_bit(p2m->vmid, vmid_flushing_needed);
Quoted 6 lines

And __clear_bit() here, or yet better use __test_and_clear_bit() in the if().

Quoted 1 line

+ sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid);

You're creating d; it cannot possibly have run on any CPU yet. IOW d->dirty_cpumask will be reliably empty here. I think it would be hard to avoid issuing the flush to all CPUs here in this scheme.

I didn't double check, but I was sure that in case d->dirty_cpumask is empty then rfence for all CPUs will be send. But I was wrong about that.

What about just update a code of sbi_rfence_v02()?

At the moment, we have check if a pointer to cpu_mask isn't NULL and if NULL then do rfence for all CPUs:

static int cf_check sbi_rfence_v02(unsigned long fid,
const cpumask_t *cpu_mask, vaddr_t start, size_t size, unsigned long arg4, unsigned long arg5)
{
...

/*
* hart_mask_base can be set to -1 to indicate that hart_mask can be
* ignored and all available harts must be considered. */
if ( !cpu_mask )
return sbi_rfence_v02_real(fid, 0UL, -1UL, start, size, arg4);
...

What about just to add here:

    if ( !cpu_mask || cpumask_empty(cpu_mask) )

Does it make sense?

Quoted 17 lines
Quoted 4 lines
xen/arch/riscv/p2m.c new file +4 -0
+    spin_unlock(&vmid_alloc_lock);
+    return rc;
+}
+

xen/arch/riscv/p2m.c line 97

Quoted 10 lines
xen/arch/riscv/p2m.c new file +10 -0
+void p2m_free_vmid(struct domain *d)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    spin_lock(&vmid_alloc_lock);
+
+    if ( p2m->vmid != INVALID_VMID )
+    {
+        clear_bit(p2m->vmid, vmid_mask);
+        set_bit(p2m->vmid, vmid_flushing_needed);

Does this scheme really avoid any flushes (except near when the system is about to go down)?

Quoted 1 line

As to choice of functions - see above.

I think yes, so my idea was that if vmid isn't freed then we have enough free VMID and in this case flush isn't needed as each vcpu has unique not-used yet VMID, and if there is no free VMID then and error will return in p2m_alloc_vmid():

    if ( nr == MAX_VMID )
    {
        rc = -EBUSY;
        printk(XENLOG_ERR "p2m.c: dom%pd: VMID pool exhausted\n", d->domain_id);
        goto out;
    }

On other hand, if VMID was freed and then re-used in p2m_alloc_vmid(), then it means that vmid_flushing_needed will have VMID bit set, what means that a TLB flush is needed.

Quoted 14 lines

xen/arch/riscv/p2m.c line 108

Quoted 12 lines
xen/arch/riscv/p2m.c new file +11 -0
+    }
+
+    spin_unlock(&vmid_alloc_lock);
+}
+
+int p2m_init(struct domain *d)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    int rc;
+
+    p2m->vmid = INVALID_VMID;

Given the absence of callers of p2m_free_vmid() it's also not clear what use this is.

Just mark that VMID for this domain wasn't yet allocated.

Anyway, it will be called from arch_domain_create() by arch_domain_destroy() so if the some error happens during arch_domain_create() and p2m->vmid wasn't allocated yet (so is equal to INVALID_VMID), it means that there is no sense to update vmid_mask or vmid_flushing_needed.

~ Oleksii

#6 lore raw

Re: [PATCH v2 01/17] xen/riscv: implement sbi_remote_hfence_gvma()

In reply to Jan Beulich
Quoted 6 lines · Jan Beulich
Quoted 3 lines · Oleksii Kurochko

Instruct the remote harts to execute one or more HFENCE.GVMA instructions, covering the range of guest physical addresses between start_addr and start_addr + size for all the guests.

Here and in the code comment: Why "for all the guests"? Under what conditions would you require such a broad (guest) TLB flush?

Hmm, it seems like KVM always do such a broad (guest) TLB flush during detection of VMIDLEN:

	void __init kvm_riscv_gstage_vmid_detect(void)
	{
		unsigned long old;
	
		/* Figure-out number of VMID bits in HW */
		old = csr_read(CSR_HGATP);
		csr_write(CSR_HGATP, old | HGATP_VMID);
		vmid_bits = csr_read(CSR_HGATP);
		vmid_bits = (vmid_bits & HGATP_VMID) >> HGATP_VMID_SHIFT;
		vmid_bits = fls_long(vmid_bits);
		csr_write(CSR_HGATP, old);
	
		/* We polluted local TLB so flush all guest TLB */
		kvm_riscv_local_hfence_gvma_all();
	
		/* We don't use VMID bits if they are not sufficient */
		if ((1UL << vmid_bits) < num_possible_cpus())
			vmid_bits = 0;
	}

It is not clear actually why so broad and why not hfence_gvma_vmid(vmid_bits).

And I am not really 100% sure that any hfence_gvma() is needed here as I don't see what could pollutes local guest TLB between csr_write() calls.

RISC-V spec. says that:
Note that writing hgatp does not imply any ordering constraints between page-table updates and subsequent G-stage address translations. If the new virtual machine’s guest physical page tables have been modified, or if a VMID is reused, it may be necessary to execute an HFENCE.GVMA instruction (see Section 18.3.2) before or after writing hgatp.

But we don't modify VM's guest physical page table. We could potentially reuse VMID between csr_write() calls, but it is returning back and we don't switch to a guest with this "new" VMID, so it isn't really used.

Do you have any thoughts about that?

~ Oleksii

#14 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

In reply to Oleksii Kurochko
Quoted 26 lines · Oleksii Kurochko
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c new file +10 -0
@@ -0,0 +1,115 @@
+#include <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/xvmalloc.h>
+
+#include <asm/p2m.h>
+#include <asm/sbi.h>
+
+static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
Quoted 8 lines

xen/arch/riscv/p2m.c

Quoted 5 lines
xen/arch/riscv/p2m.c new file +5 -0
+
+/*
+ * hgatp's VMID field is 7 or 14 bits. RV64 may support 14-bit VMID.
+ * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1)
+ * concurrent domains.

Which is pretty limiting especially in the RV32 case. Hence why we don't assign a permanent ID to VMs on x86, but rather manage IDs per-CPU (note:
not per-vCPU).

Good point.

Quoted 7 lines

I don't believe anyone will use RV32.
For RV64, the available ID space seems sufficiently large.

However, if it turns out that the value isn't large enough even for RV64, I can rework it to manage IDs per physical CPU.
Wouldn't that approach result in more TLB entries being flushed compared to per-vCPU allocation, potentially leading to slightly worse performance?

Depends on the condition for when to flush. Of course performance is unavoidably going to suffer if you have only very few VMIDs to use. Nevertheless, as indicated before, the model used on x86 may be a candidate to use here, too. See hvm_asid_handle_vmenter() for the core (and vendor-independent) part of it.

Quoted 1 line

What about then to allocate VMID per-domain?

That's what you're doing right now, isn't it? And that gets problematic when you have only very few bits in hgatp.VMID, as mentioned below.

Quoted 11 lines
Quoted 10 lines

xen/arch/riscv/p2m.c line 23

Quoted 9 lines

The bitmap space will be allocated dynamically
+ * based on whether 7 or 14 bit VMIDs are supported.
+ */

xen/arch/riscv/p2m.c new file +6 -0
+static unsigned long *vmid_mask;
+static unsigned long *vmid_flushing_needed;
+
+/*
+ * -2 here because:
+ *    - -1 is needed to get the maximal possible VMID

I don't follow this part.

Quoted 5 lines

Probably, I'm missing something.

hgat.vmid is 7 bit long. BIT(7,U) = 1 << 7 = 128 which is bigger then 7 bit can cover (0b1000_0000 and 0x111_1111). Thereby the MAX_VMID is:
BIT(7, U) - 1 (in case of RV32).

Right, but then why -2? (Maybe this is moot now that you agreed that INVALID_VMID can be defined differently.

Quoted 7 lines
Quoted 2 lines

VMIDLEN being permitted to be 0, how would you run more than one VM (e.g. Dom0) on such a system?

Hmm, good question.

Then it will be needed to flush TLB on each VM switch by using sbi_remote_hfence_gvma().

Right, but just to be clear: That flush should not be conditional upon VMIDLEN being 0. In whatever model you chose, the handling of this special case should come out "natural".

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

+ sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid);

You're creating d; it cannot possibly have run on any CPU yet. IOW d->dirty_cpumask will be reliably empty here. I think it would be hard to avoid issuing the flush to all CPUs here in this scheme.

I didn't double check, but I was sure that in case d->dirty_cpumask is empty then rfence for all CPUs will be send. But I was wrong about that.

What about just update a code of sbi_rfence_v02()?

I don't know, but dealing with the issue there feels wrong. However, before deciding where to do something, it needs to be clear what you actually want to achieve. To me at least, that's not clear at all.

Quoted 22 lines
Quoted 17 lines
Quoted 4 lines
xen/arch/riscv/p2m.c new file +4 -0
+    spin_unlock(&vmid_alloc_lock);
+    return rc;
+}
+

xen/arch/riscv/p2m.c line 97

Quoted 10 lines
xen/arch/riscv/p2m.c new file +10 -0
+void p2m_free_vmid(struct domain *d)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    spin_lock(&vmid_alloc_lock);
+
+    if ( p2m->vmid != INVALID_VMID )
+    {
+        clear_bit(p2m->vmid, vmid_mask);
+        set_bit(p2m->vmid, vmid_flushing_needed);

Does this scheme really avoid any flushes (except near when the system is about to go down)?

Quoted 1 line

As to choice of functions - see above.

I think yes, so my idea was that if vmid isn't freed then we have enough free VMID and in this case flush isn't needed as each vcpu has unique not-used yet VMID, and if there is no free VMID then and error will return in p2m_alloc_vmid():

Quoted 6 lines
     if ( nr == MAX_VMID )
     {
         rc = -EBUSY;
         printk(XENLOG_ERR "p2m.c: dom%pd: VMID pool exhausted\n", d->domain_id);
         goto out;
     }

Which, as said, is a problem when there are only very few VMIDs.

Quoted 2 lines

On other hand, if VMID was freed and then re-used in p2m_alloc_vmid(), then it means that vmid_flushing_needed will have VMID bit set, what means that a TLB flush is needed.

Let's assume over the uptime of a system you cycle through all VMIDs a thousand times. While you manage to delay some TLB flushes, the percentage of ones actually saved is going to be very low then.

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/riscv/p2m.c new file +5 -0
+    }
+
+    spin_unlock(&vmid_alloc_lock);
+}
+
Quoted 8 lines

xen/arch/riscv/p2m.c line 108

Quoted 6 lines
xen/arch/riscv/p2m.c new file +6 -0
+int p2m_init(struct domain *d)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    int rc;
+
+    p2m->vmid = INVALID_VMID;

Given the absence of callers of p2m_free_vmid() it's also not clear what use this is.

Quoted 5 lines

Just mark that VMID for this domain wasn't yet allocated.

Anyway, it will be called from arch_domain_create() by arch_domain_destroy() so if the some error happens during arch_domain_create() and p2m->vmid wasn't allocated yet (so is equal to INVALID_VMID), it means that there is no sense to update vmid_mask or vmid_flushing_needed.

But only if you actually came through p2m_init() prior to the error. My point
is: If you allocate a VMID here anyway, why first set the field like this? (Again, this is likely moot since the allocation scheme is likely to change altogether.)

Jan

#7 lore raw

Re: [PATCH v2 01/17] xen/riscv: implement sbi_remote_hfence_gvma()

In reply to Oleksii Kurochko
Quoted 35 lines · Oleksii Kurochko
Quoted 6 lines · Jan Beulich
Quoted 3 lines · Oleksii Kurochko

Instruct the remote harts to execute one or more HFENCE.GVMA instructions, covering the range of guest physical addresses between start_addr and start_addr + size for all the guests.

Here and in the code comment: Why "for all the guests"? Under what conditions would you require such a broad (guest) TLB flush?

Hmm, it seems like KVM always do such a broad (guest) TLB flush during detection of VMIDLEN:

	void __init kvm_riscv_gstage_vmid_detect(void)
	{
		unsigned long old;
	
		/* Figure-out number of VMID bits in HW */
		old = csr_read(CSR_HGATP);
		csr_write(CSR_HGATP, old | HGATP_VMID);
		vmid_bits = csr_read(CSR_HGATP);
		vmid_bits = (vmid_bits & HGATP_VMID) >> HGATP_VMID_SHIFT;
		vmid_bits = fls_long(vmid_bits);
		csr_write(CSR_HGATP, old);
	
		/* We polluted local TLB so flush all guest TLB */
		kvm_riscv_local_hfence_gvma_all();
	
		/* We don't use VMID bits if they are not sufficient */
		if ((1UL << vmid_bits) < num_possible_cpus())
			vmid_bits = 0;
	}

It is not clear actually why so broad and why not hfence_gvma_vmid(vmid_bits).

And I am not really 100% sure that any hfence_gvma() is needed here as I don't see what could pollutes local guest TLB between csr_write() calls.

Quoted 8 lines

RISC-V spec. says that:
Note that writing hgatp does not imply any ordering constraints between page-table updates and subsequent G-stage address translations. If the new virtual machine’s guest physical page tables have been modified, or if a VMID is reused, it may be necessary to execute an HFENCE.GVMA instruction (see Section 18.3.2) before or after writing hgatp.

But we don't modify VM's guest physical page table. We could potentially reuse VMID between csr_write() calls, but it is returning back and we don't switch to a guest with this "new" VMID, so it isn't really used.

That would be my expectation, too. Yet I don't know if RISC-V has any peculiarities there.

Jan

#15 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

In reply to Jan Beulich
Quoted 33 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c new file +10 -0
@@ -0,0 +1,115 @@
+#include <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/xvmalloc.h>
+
+#include <asm/p2m.h>
+#include <asm/sbi.h>
+
+static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
Quoted 8 lines

xen/arch/riscv/p2m.c

Quoted 5 lines
xen/arch/riscv/p2m.c new file +5 -0
+
+/*
+ * hgatp's VMID field is 7 or 14 bits. RV64 may support 14-bit VMID.
+ * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1)
+ * concurrent domains.

Which is pretty limiting especially in the RV32 case. Hence why we don't assign a permanent ID to VMs on x86, but rather manage IDs per-CPU (note:
not per-vCPU).

Good point.

Quoted 7 lines

I don't believe anyone will use RV32.
For RV64, the available ID space seems sufficiently large.

However, if it turns out that the value isn't large enough even for RV64, I can rework it to manage IDs per physical CPU.
Wouldn't that approach result in more TLB entries being flushed compared to per-vCPU allocation, potentially leading to slightly worse performance?

Quoted 5 lines

Depends on the condition for when to flush. Of course performance is unavoidably going to suffer if you have only very few VMIDs to use. Nevertheless, as indicated before, the model used on x86 may be a candidate to use here, too. See hvm_asid_handle_vmenter() for the core (and vendor-independent) part of it.

Thanks.

IIUC, so basically it is just a round-robin and when VMIDs are ran out
then just do full guest TLB flush and start to re-use VMIDs from the start.
It makes sense to me, I'll implement something similar. (as I'm not really
sure that we needdata->core_asid_generation, probably, I will understand it better when start to implement it)

Quoted 4 lines
Quoted 2 lines

What about then to allocate VMID per-domain?

That's what you're doing right now, isn't it? And that gets problematic when you have only very few bits in hgatp.VMID, as mentioned below.

Right, I just phrased my question poorly—sorry about that.

What I meant to ask is: does the approach described above actually depend on whether VMIDs are allocated per-domain or per-pCPU? It seems that the main advantage of allocating VMIDs per-pCPU is potentially reducing the number of TLB flushes, since it's more likely that a platform will have more than|VMID_MAX| domains than |VMID_MAX| physical CPUs—am I right?

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

xen/arch/riscv/p2m.c line 23

Quoted 10 lines

The bitmap space will be allocated dynamically
+ * based on whether 7 or 14 bit VMIDs are supported.
+ */

xen/arch/riscv/p2m.c new file +6 -0
+static unsigned long *vmid_mask;
+static unsigned long *vmid_flushing_needed;
+
+/*
+ * -2 here because:
+ *    - -1 is needed to get the maximal possible VMID

I don't follow this part.

Quoted 7 lines
Quoted 5 lines

Probably, I'm missing something.

hgat.vmid is 7 bit long. BIT(7,U) = 1 << 7 = 128 which is bigger then 7 bit can cover (0b1000_0000 and 0x111_1111). Thereby the MAX_VMID is:

   BIT(7, U) - 1 (in case of RV32).

Right, but then why -2? (Maybe this is moot now that you agreed that INVALID_VMID can be defined differently.

Yes, another one -1 was because how INVALID_VMID was defined.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

VMIDLEN being permitted to be 0, how would you run more than one VM (e.g. Dom0) on such a system?

Hmm, good question.

Then it will be needed to flush TLB on each VM switch by using sbi_remote_hfence_gvma().

Right, but just to be clear: That flush should not be conditional upon VMIDLEN being 0. In whatever model you chose, the handling of this special case should come out "natural".

Sure. I have some ideas how to do it natural.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

+ sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid);

You're creating d; it cannot possibly have run on any CPU yet. IOW d->dirty_cpumask will be reliably empty here. I think it would be hard to avoid issuing the flush to all CPUs here in this scheme.

Quoted 7 lines
Quoted 4 lines

I didn't double check, but I was sure that in case d->dirty_cpumask is empty then rfence for all CPUs will be send. But I was wrong about that.

What about just update a code of sbi_rfence_v02()?

I don't know, but dealing with the issue there feels wrong. However, before deciding where to do something, it needs to be clear what you actually want to achieve. To me at least, that's not clear at all.

I want to achieve the following behavior: if a mask is empty (specifically, in our case|d->dirty_cpumask|), then perform the flush on all CPUs.

If you think it's not a good idea to change the current implementation of|sbi_rfence_v02()|, then I’ll just check if|d->dirty_cpumask| is empty before calling|sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid)|.

If it is empty, I’ll call|sbi_remote_hfence_gvma()| instead:
|if( !cpumask_empty(d->dirty_cpumask) ) sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid); elsesbi_remote_hfence_gvma(NULL, 0, 0); |

A similar check will be needed in|p2m_force_tlb_flush_sync()|, which is implemented in one of the following patches in this series.

However, if we instead move the|if ( !cpumask_empty(d->dirty_cpumask) ) |check into||https://gitlab.com/xen-project/xen/-/blob/staging/xen/arch/riscv/sbi.c?ref_type=heads#L178, we could call only:

  |sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid); |and get the same effect, which might result in cleaner code overall
as we already have a similar check (cpumask == NULL)|sbi_rfence_v02|() and a result of which

is just to send rfence operation to all CPUs.

Quoted 28 lines
Quoted 22 lines
Quoted 18 lines
Quoted 5 lines
xen/arch/riscv/p2m.c new file +4 -0
+    spin_unlock(&vmid_alloc_lock);
+    return rc;
+}
+

xen/arch/riscv/p2m.c line 97

Quoted 10 lines
xen/arch/riscv/p2m.c new file +10 -0
+void p2m_free_vmid(struct domain *d)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    spin_lock(&vmid_alloc_lock);
+
+    if ( p2m->vmid != INVALID_VMID )
+    {
+        clear_bit(p2m->vmid, vmid_mask);
+        set_bit(p2m->vmid, vmid_flushing_needed);

Does this scheme really avoid any flushes (except near when the system is about to go down)?

Quoted 1 line

As to choice of functions - see above.

I think yes, so my idea was that if vmid isn't freed then we have enough free VMID and in this case flush isn't needed as each vcpu has unique not-used yet VMID, and if there is no free VMID then and error will return in p2m_alloc_vmid():

Quoted 6 lines
      if ( nr == MAX_VMID )
      {
          rc = -EBUSY;
          printk(XENLOG_ERR "p2m.c: dom%pd: VMID pool exhausted\n", d->domain_id);
          goto out;
      }
Quoted 7 lines

Which, as said, is a problem when there are only very few VMIDs.

Quoted 2 lines

On other hand, if VMID was freed and then re-used in p2m_alloc_vmid(), then it means that vmid_flushing_needed will have VMID bit set, what means that a TLB flush is needed.

Let's assume over the uptime of a system you cycle through all VMIDs a thousand times. While you manage to delay some TLB flushes, the percentage of ones actually saved is going to be very low then.

Then it is just better to update VMID allocation algo.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

xen/arch/riscv/p2m.c line 108

Quoted 12 lines
xen/arch/riscv/p2m.c new file +11 -0
+    }
+
+    spin_unlock(&vmid_alloc_lock);
+}
+
+int p2m_init(struct domain *d)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    int rc;
+
+    p2m->vmid = INVALID_VMID;

Given the absence of callers of p2m_free_vmid() it's also not clear what use this is.

Quoted 7 lines
Quoted 5 lines

Just mark that VMID for this domain wasn't yet allocated.

Anyway, it will be called from arch_domain_create() by arch_domain_destroy() so if the some error happens during arch_domain_create() and p2m->vmid wasn't allocated yet (so is equal to INVALID_VMID), it means that there is no sense to update vmid_mask or vmid_flushing_needed.

But only if you actually came through p2m_init() prior to the error. My point
is: If you allocate a VMID here anyway, why first set the field like this?

Oh, got your point. Indeed, there is no sense.

Quoted 2 lines

(Again, this is likely moot since the allocation scheme is likely to change altogether.)

Yes, it won't be really needed in the new allocation scheme.

Thanks.

~ Oleksii

#16 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

Quoted 40 lines · Oleksii Kurochko
Quoted 33 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c new file +10 -0
@@ -0,0 +1,115 @@
+#include <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/xvmalloc.h>
+
+#include <asm/p2m.h>
+#include <asm/sbi.h>
+
+static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
Quoted 8 lines

xen/arch/riscv/p2m.c

Quoted 5 lines
xen/arch/riscv/p2m.c new file +5 -0
+
+/*
+ * hgatp's VMID field is 7 or 14 bits. RV64 may support 14-bit VMID.
+ * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1)
+ * concurrent domains.

Which is pretty limiting especially in the RV32 case. Hence why we don't assign a permanent ID to VMs on x86, but rather manage IDs per-CPU (note:
not per-vCPU).

Good point.

Quoted 7 lines

I don't believe anyone will use RV32.
For RV64, the available ID space seems sufficiently large.

However, if it turns out that the value isn't large enough even for RV64, I can rework it to manage IDs per physical CPU.
Wouldn't that approach result in more TLB entries being flushed compared to per-vCPU allocation, potentially leading to slightly worse performance?

Quoted 5 lines

Depends on the condition for when to flush. Of course performance is unavoidably going to suffer if you have only very few VMIDs to use. Nevertheless, as indicated before, the model used on x86 may be a candidate to use here, too. See hvm_asid_handle_vmenter() for the core (and vendor-independent) part of it.

Quoted 5 lines

IIUC, so basically it is just a round-robin and when VMIDs are ran out then just do full guest TLB flush and start to re-use VMIDs from the start. It makes sense to me, I'll implement something similar. (as I'm not really sure that we needdata->core_asid_generation, probably, I will understand it better when start to implement it)

Well. The fewer VMID bits you have the more quickly you will need a new generation. And keep track of the generation you're at you also need to track the present number somewhere.

Quoted 11 lines
Quoted 3 lines
Quoted 1 line

What about then to allocate VMID per-domain?

That's what you're doing right now, isn't it? And that gets problematic when you have only very few bits in hgatp.VMID, as mentioned below.

Right, I just phrased my question poorly—sorry about that.

What I meant to ask is: does the approach described above actually depend on whether VMIDs are allocated per-domain or per-pCPU? It seems that the main advantage of allocating VMIDs per-pCPU is potentially reducing the number of TLB flushes, since it's more likely that a platform will have more than|VMID_MAX| domains than |VMID_MAX| physical CPUs—am I right?

Seeing that there can be systems with hundreds or even thousands of CPUs, I don't think I can agree here. Plus per-pCPU allocation would similarly get you in trouble when you have only very few VMID bits.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

xen/arch/riscv/p2m.c line 76

Quoted 1 line
xen/arch/riscv/p2m.c new file +1 -0
+        sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid);

You're creating d; it cannot possibly have run on any CPU yet. IOW d->dirty_cpumask will be reliably empty here. I think it would be hard to avoid issuing the flush to all CPUs here in this scheme.

I didn't double check, but I was sure that in case d->dirty_cpumask is empty then rfence for all CPUs will be send. But I was wrong about that.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

What about just update a code of sbi_rfence_v02()?

I don't know, but dealing with the issue there feels wrong. However, before deciding where to do something, it needs to be clear what you actually want to achieve. To me at least, that's not clear at all.

I want to achieve the following behavior: if a mask is empty (specifically, in our case|d->dirty_cpumask|), then perform the flush on all CPUs.

That's still too far into the "how". The "why" here is still unclear: Why do you need any flushing here at all? (With the scheme you now mean to implement I expect it'll become yet more clear that no flush is needed during domain construction.)

Jan

#17 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

+ sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid);

You're creating d; it cannot possibly have run on any CPU yet. IOW d->dirty_cpumask will be reliably empty here. I think it would be hard to avoid issuing the flush to all CPUs here in this scheme.

I didn't double check, but I was sure that in case d->dirty_cpumask is empty then rfence for all CPUs will be send. But I was wrong about that.

What about just update a code of sbi_rfence_v02()?

I don't know, but dealing with the issue there feels wrong. However, before deciding where to do something, it needs to be clear what you actually want to achieve. To me at least, that's not clear at all.

Quoted 7 lines
Quoted 3 lines

I want to achieve the following behavior: if a mask is empty (specifically, in our case|d->dirty_cpumask|), then perform the flush on all CPUs.

That's still too far into the "how". The "why" here is still unclear: Why do you need any flushing here at all? (With the scheme you now mean to implement I expect it'll become yet more clear that no flush is needed during domain construction.)

For the same reason x86 has flush:

    /* If there are no free ASIDs, need to go to a new generation */
    if ( unlikely(data->next_asid > data->max_asid) )
    {
        hvm_asid_flush_core();

But hvm_asid_flush_core() isn't doing a "real" flush what I missed to check on the first look at hvm_asid_handle_vmenter().

So I assume then a "real" flush will be called somewhere before entry to guest context.

I think now it is more or less clear.

Anyway, what then do for the cases if it is needed to have ASID which isn't expected to be changed?
With this cycling approach after a new generation will be needed, all ASIDs could/will be changed. It isn't a case for RISC-V (at least, at the moment) but AFAIK it is an issue for AMD SEV.

~ Oleksii

#29 lore raw

Re: [PATCH v2 04/17] xen/riscv: construct the P2M pages pool for guests

In reply to Jan Beulich
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/domain.h +8 -0
@@ -18,10 +20,20 @@ struct arch_vcpu_io {
  struct arch_vcpu {
  };
 
+struct paging_domain {
+    spinlock_t lock;
+    /* Free P2M pages from the pre-allocated P2M pool */
+    struct page_list_head p2m_freelist;
+    /* Number of pages from the pre-allocated P2M pool */
+    unsigned long p2m_total_pages;
+};
+
  struct arch_domain {
      struct hvm_domain hvm;
 
Quoted 5 lines

xen/arch/riscv/include/asm/domain.h struct arch_vcpu_io { · line 36

Quoted 3 lines
xen/arch/riscv/include/asm/domain.h +2 -0
      struct p2m_domain p2m;
+
+    struct paging_domain paging;

With the separate structures, do you have plans to implement e.g. shadow paging? Or some other paging mode beyond the basic one based on the H extension?

No, there is no such plans.

Quoted 4 lines

If the
structures are to remain separate, may I suggest that you keep things properly separated (no matter how e.g. Arm may have it) in terms of naming? I.e. no single "p2m" inside struct paging_domain.

Arm doesn't implement shadow paging too (AFAIK) and probably this approach was copied from x86, and then to RISC-V.
I thought that a reason for that was just to have two separate entities: one which covers page tables and which covers the full available guest memory. And if the only idea of that was to have shadow paging then I don't how it should be done better. As p2m code is based on Arm's, perhaps, it makes sense to have this stuff separated, so easier porting will be.

Quoted 9 lines

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 110

Quoted 7 lines
xen/arch/riscv/p2m.c +2 -0
@@ -105,6 +106,9 @@ int p2m_init(struct domain *d)
      struct p2m_domain *p2m = p2m_get_hostp2m(d);
      int rc;
 
+    spin_lock_init(&d->arch.paging.lock);
+    INIT_PAGE_LIST_HEAD(&d->arch.paging.p2m_freelist);

If you want p2m and paging to be separate, you will want to put these in a new paging_init().

I am not really understand what is wrong to have it here, but likely it is because I don't really get an initial purpose of having p2m and paging separately. It seems like p2m and paging are connected between each other, so it is fine to init them together.

~ Oleksii

#30 lore raw

Re: [PATCH v2 04/17] xen/riscv: construct the P2M pages pool for guests

Quoted 48 lines · Oleksii Kurochko
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/domain.h +8 -0
@@ -18,10 +20,20 @@ struct arch_vcpu_io {
  struct arch_vcpu {
  };
 
+struct paging_domain {
+    spinlock_t lock;
+    /* Free P2M pages from the pre-allocated P2M pool */
+    struct page_list_head p2m_freelist;
+    /* Number of pages from the pre-allocated P2M pool */
+    unsigned long p2m_total_pages;
+};
+
  struct arch_domain {
      struct hvm_domain hvm;
 
Quoted 5 lines

xen/arch/riscv/include/asm/domain.h struct arch_vcpu_io { · line 36

Quoted 3 lines
xen/arch/riscv/include/asm/domain.h +2 -0
      struct p2m_domain p2m;
+
+    struct paging_domain paging;

With the separate structures, do you have plans to implement e.g. shadow paging? Or some other paging mode beyond the basic one based on the H extension?

No, there is no such plans.

Quoted 4 lines

If the
structures are to remain separate, may I suggest that you keep things properly separated (no matter how e.g. Arm may have it) in terms of naming? I.e. no single "p2m" inside struct paging_domain.

Arm doesn't implement shadow paging too (AFAIK) and probably this approach was copied from x86, and then to RISC-V.
I thought that a reason for that was just to have two separate entities: one which covers page tables and which covers the full available guest memory. And if the only idea of that was to have shadow paging then I don't how it should be done better. As p2m code is based on Arm's, perhaps, it makes sense to have this stuff separated, so easier porting will be.

Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 110

Quoted 6 lines
xen/arch/riscv/p2m.c +2 -0
@@ -105,6 +106,9 @@ int p2m_init(struct domain *d)
      struct p2m_domain *p2m = p2m_get_hostp2m(d);
      int rc;
 
+    spin_lock_init(&d->arch.paging.lock);
+    INIT_PAGE_LIST_HEAD(&d->arch.paging.p2m_freelist);

If you want p2m and paging to be separate, you will want to put these in a new paging_init().

Quoted 4 lines

I am not really understand what is wrong to have it here, but likely it is because I don't really get an initial purpose of having p2m and paging separately. It seems like p2m and paging are connected between each other, so it is fine to init them together.

If you want to retain the separation, imo you want to follow what x86 has:
paging_domain_init() calling p2m_init(). And d->arch.paging.* would then be initialized in paging_domain_init(), like x86 has it.

Jan

#41 lore raw

Re: [PATCH v2 05/17] xen/riscv: introduce things necessary for p2m initialization

In reply to Jan Beulich
Quoted 13 lines · Jan Beulich
Quoted 4 lines · Oleksii Kurochko

Introduce the following things:
- Update p2m_domain structure, which describe per p2m-table state, with:
- lock to protect updates to p2m.
- pool with pages used to construct p2m.

Quoted 8 lines

- clean_pte which indicate if it is requires to clean the cache when writing an entry.
- radix tree to store p2m type as PTE doesn't have enough free bits to store type.
- default_access to store p2m access type for each page in the domain.
- back pointer to domain structure.
- p2m_init() to initalize members introduced in p2m_domain structure.
- Introudce p2m_write_lock() and p2m_is_write_locked().

Quoted 2 lines

What about the reader variant? If you don't need that, why not use a simple spin lock?

It will be introduced later in "xen/riscv: add support of page lookup by GFN" of this patch series where it is really used.

But I can move it here.

Quoted 18 lines
Quoted 7 lines
xen/arch/riscv/include/asm/p2m.h +2 -0
@@ -14,6 +18,29 @@
 
  /* Per-p2m-table state */
  struct p2m_domain {
+    /*
+     * Lock that protects updates to the p2m.

xen/arch/riscv/include/asm/p2m.h line 32

Quoted 10 lines
xen/arch/riscv/include/asm/p2m.h +10 -0
+     */
+    rwlock_t lock;
+
+    /* Pages used to construct the p2m */
+    struct page_list_head pages;
+
+    /* Indicate if it is required to clean the cache when writing an entry */
+    bool clean_pte;
+
+    struct radix_tree_root p2m_type;

A field with a p2m_ prefix in a p2m struct?

p2m_ prefix could be really dropped.

Quoted 2 lines

And is this tree really about
just a single "type"?

Yes, we don't have enough bits in PTE so we need some extra storage to store type.

Quoted 10 lines

xen/arch/riscv/include/asm/p2m.h line 42

Quoted 10 lines
xen/arch/riscv/include/asm/p2m.h +9 -0
+    /*
+     * Default P2M access type for each page in the the domain: new pages,
+     * swapped in pages, cleared pages, and pages that are ambiguously
+     * retyped get this access type.  See definition of p2m_access_t.
+     */
+    p2m_access_t default_access;
+
+    /* Back pointer to domain */
+    struct domain *domain;
Quoted 3 lines

This you may want to introduce earlier, to prefer passing around struct p2m_domain * in / to P2M functions (which would benefit earlier patches already, I think).

But nothing uses it earlier.

Quoted 38 lines
Quoted 38 lines
xen/arch/riscv/p2m.c +24 -0
@@ -1,13 +1,46 @@
  #include <xen/bitops.h>
+#include <xen/domain_page.h>
  #include <xen/event.h>
+#include <xen/iommu.h>
  #include <xen/lib.h>
+#include <xen/mm.h>
+#include <xen/pfn.h>
+#include <xen/rwlock.h>
  #include <xen/sched.h>
  #include <xen/spinlock.h>
  #include <xen/xvmalloc.h>
 
+#include <asm/page.h>
  #include <asm/p2m.h>
  #include <asm/sbi.h>
 
+/*
+ * Force a synchronous P2M TLB flush.
+ *
+ * Must be called with the p2m lock held.
+ */
+static void p2m_force_tlb_flush_sync(struct p2m_domain *p2m)
+{
+    struct domain *d = p2m->domain;
+
+    ASSERT(p2m_is_write_locked(p2m));
+
+    sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid);
+}
+
+/* Unlock the flush and do a P2M TLB flush if necessary */
+void p2m_write_unlock(struct p2m_domain *p2m)
+{
+    /*
Quoted 8 lines

xen/arch/riscv/p2m.c line 39

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+     * The final flush is done with the P2M write lock taken to avoid
+     * someone else modifying the P2M wbefore the TLB invalidation has
+     * completed.
+     */
+    p2m_force_tlb_flush_sync(p2m);

The comment ahead of the function says "if necessary". Yet there's no conditional here. I also question the need for a global flush in all cases.

Stale comment.

But if p2m page table was modified that it is needed to do a flush for CPUs in d->dirty_cpumask.

Quoted 10 lines
Quoted 10 lines
xen/arch/riscv/p2m.c +3 -0
@@ -109,8 +142,33 @@ int p2m_init(struct domain *d)
      spin_lock_init(&d->arch.paging.lock);
      INIT_PAGE_LIST_HEAD(&d->arch.paging.p2m_freelist);
 
+    rwlock_init(&p2m->lock);
+    INIT_PAGE_LIST_HEAD(&p2m->pages);
+
      p2m->vmid = INVALID_VMID;
 
Quoted 6 lines

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 154

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+    p2m->default_access = p2m_access_rwx;
+
+    radix_tree_init(&p2m->p2m_type);
+
+#ifdef CONFIG_HAS_PASSTHROUGH

Do you expect this to be conditionally selected on RISC-V?

No, once it will be implemented it will be just selected once by config RISC-V. And it was done so because iommu_has_feature() isn't implemented now as IOMMU isn't supported now and depends on CONFIG_HAS_PASSTHROUGH.

Quoted 10 lines

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 161

Quoted 8 lines
xen/arch/riscv/p2m.c +7 -0
+    /*
+     * Some IOMMUs don't support coherent PT walk. When the p2m is
+     * shared with the CPU, Xen has to make sure that the PT changes have
+     * reached the memory
+     */
+    p2m->clean_pte = is_iommu_enabled(d) &&
+        !iommu_has_feature(d, IOMMU_FEAT_COHERENT_WALK);

The comment talks about shared page tables, yet you don't check whether page table sharing is actually enabled for the domain.

Do we have such function/macros? It is shared by implementation now.

Quoted 6 lines

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 163

Quoted 3 lines
xen/arch/riscv/p2m.c +2 -0
+#else
+    p2m->clean_pte = false;

I hope the struct starts out zero-filled, in which case you wouldn't need this.

Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 170

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+#endif
+
+    /*
+     * "Trivial" initialisation is now complete.  Set the backpointer so the
+     * users of p2m could get an access to domain structure.
+     */
+    p2m->domain = d;

Better set this about the very first thing?

It makes sense. I will move it up.

Thanks.

~ Oleksii

#42 lore raw

Re: [PATCH v2 05/17] xen/riscv: introduce things necessary for p2m initialization

Quoted 20 lines · Oleksii Kurochko
Quoted 18 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +2 -0
@@ -14,6 +18,29 @@
 
  /* Per-p2m-table state */
  struct p2m_domain {
+    /*
+     * Lock that protects updates to the p2m.

xen/arch/riscv/include/asm/p2m.h line 32

Quoted 10 lines
xen/arch/riscv/include/asm/p2m.h +10 -0
+     */
+    rwlock_t lock;
+
+    /* Pages used to construct the p2m */
+    struct page_list_head pages;
+
+    /* Indicate if it is required to clean the cache when writing an entry */
+    bool clean_pte;
+
+    struct radix_tree_root p2m_type;

A field with a p2m_ prefix in a p2m struct?

Quoted 6 lines

p2m_ prefix could be really dropped.

Quoted 2 lines

And is this tree really about
just a single "type"?

Yes, we don't have enough bits in PTE so we need some extra storage to store type.

My question wasn't about that, though. My question was whether in the name "type" (singular) is appropriate. I didn't think you need a tree to store just a single type.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/riscv/include/asm/p2m.h +9 -0
+    /*
+     * Default P2M access type for each page in the the domain: new pages,
+     * swapped in pages, cleared pages, and pages that are ambiguously
+     * retyped get this access type.  See definition of p2m_access_t.
+     */
+    p2m_access_t default_access;
+
+    /* Back pointer to domain */
+    struct domain *domain;
Quoted 5 lines
Quoted 3 lines

This you may want to introduce earlier, to prefer passing around struct p2m_domain * in / to P2M functions (which would benefit earlier patches already, I think).

But nothing uses it earlier.

If you do as suggested and pass around struct p2m_domain * for p2m_*() functions, you'll quickly find it used, I think.

Quoted 46 lines
Quoted 37 lines
Quoted 37 lines
xen/arch/riscv/p2m.c +24 -0
@@ -1,13 +1,46 @@
  #include <xen/bitops.h>
+#include <xen/domain_page.h>
  #include <xen/event.h>
+#include <xen/iommu.h>
  #include <xen/lib.h>
+#include <xen/mm.h>
+#include <xen/pfn.h>
+#include <xen/rwlock.h>
  #include <xen/sched.h>
  #include <xen/spinlock.h>
  #include <xen/xvmalloc.h>
 
+#include <asm/page.h>
  #include <asm/p2m.h>
  #include <asm/sbi.h>
 
+/*
+ * Force a synchronous P2M TLB flush.
+ *
+ * Must be called with the p2m lock held.
+ */
+static void p2m_force_tlb_flush_sync(struct p2m_domain *p2m)
+{
+    struct domain *d = p2m->domain;
+
+    ASSERT(p2m_is_write_locked(p2m));
+
+    sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid);
+}
+
+/* Unlock the flush and do a P2M TLB flush if necessary */
+void p2m_write_unlock(struct p2m_domain *p2m)
+{
+    /*
Quoted 8 lines

xen/arch/riscv/p2m.c line 39

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+     * The final flush is done with the P2M write lock taken to avoid
+     * someone else modifying the P2M wbefore the TLB invalidation has
+     * completed.
+     */
+    p2m_force_tlb_flush_sync(p2m);

The comment ahead of the function says "if necessary". Yet there's no conditional here. I also question the need for a global flush in all cases.

Quoted 4 lines

Stale comment.

But if p2m page table was modified that it is needed to do a flush for CPUs in d->dirty_cpumask.

Right, but is that true for each and every case where you acquire the lock in write mode? There may e.g. be early-out path which end up doing nothing, yet you would then still flush the TLB.

Quoted 16 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/riscv/p2m.c +3 -0
@@ -109,8 +142,33 @@ int p2m_init(struct domain *d)
      spin_lock_init(&d->arch.paging.lock);
      INIT_PAGE_LIST_HEAD(&d->arch.paging.p2m_freelist);
 
+    rwlock_init(&p2m->lock);
+    INIT_PAGE_LIST_HEAD(&p2m->pages);
+
      p2m->vmid = INVALID_VMID;
 
Quoted 6 lines

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 154

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+    p2m->default_access = p2m_access_rwx;
+
+    radix_tree_init(&p2m->p2m_type);
+
+#ifdef CONFIG_HAS_PASSTHROUGH

Do you expect this to be conditionally selected on RISC-V?

Quoted 3 lines

No, once it will be implemented it will be just selected once by config RISC-V. And it was done so because iommu_has_feature() isn't implemented now as IOMMU isn't supported now and depends on CONFIG_HAS_PASSTHROUGH.

If the selection isn't going to be conditional, then I see no reason to have such conditionals in RISC-V-specific code. The piece of code presently inside that #ifdef may simply need adding later, once there's enough infrastructure to allow that code to compile. Or maybe it would even compile fine already now?

Quoted 11 lines
Quoted 9 lines

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 161

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+    /*
+     * Some IOMMUs don't support coherent PT walk. When the p2m is
+     * shared with the CPU, Xen has to make sure that the PT changes have
+     * reached the memory
+     */
+    p2m->clean_pte = is_iommu_enabled(d) &&
+        !iommu_has_feature(d, IOMMU_FEAT_COHERENT_WALK);

The comment talks about shared page tables, yet you don't check whether page table sharing is actually enabled for the domain.

Do we have such function/macros?

We have iommu_hap_pt_share, and we have the per-domain hap_pt_share flag.

Quoted 1 line

It is shared by implementation now.

I don't understand. There's no IOMMU support yet for RISC-V. Hence it's in neither state - not shared, but also not not shared.

Jan

#43 lore raw

Re: [PATCH v2 05/17] xen/riscv: introduce things necessary for p2m initialization

Quoted 20 lines · Jan Beulich
Quoted 19 lines · Oleksii Kurochko
Quoted 18 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +2 -0
@@ -14,6 +18,29 @@
 
   /* Per-p2m-table state */
   struct p2m_domain {
+    /*
+     * Lock that protects updates to the p2m.

xen/arch/riscv/include/asm/p2m.h line 32

Quoted 10 lines
xen/arch/riscv/include/asm/p2m.h +10 -0
+     */
+    rwlock_t lock;
+
+    /* Pages used to construct the p2m */
+    struct page_list_head pages;
+
+    /* Indicate if it is required to clean the cache when writing an entry */
+    bool clean_pte;
+
+    struct radix_tree_root p2m_type;

A field with a p2m_ prefix in a p2m struct?

Quoted 8 lines
Quoted 5 lines

p2m_ prefix could be really dropped.

Quoted 2 lines

And is this tree really about
just a single "type"?

Yes, we don't have enough bits in PTE so we need some extra storage to store type.

My question wasn't about that, though. My question was whether in the name "type" (singular) is appropriate. I didn't think you need a tree to store just a single type.

I need tree to store a pair of <gfn, p2m_type>, where gfn is an index. And it seems to me a tree is a good structure for fast insert/search.

Quoted 62 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
xen/arch/riscv/include/asm/p2m.h +9 -0
+    /*
+     * Default P2M access type for each page in the the domain: new pages,
+     * swapped in pages, cleared pages, and pages that are ambiguously
+     * retyped get this access type.  See definition of p2m_access_t.
+     */
+    p2m_access_t default_access;
+
+    /* Back pointer to domain */
+    struct domain *domain;
Quoted 4 lines
Quoted 3 lines

This you may want to introduce earlier, to prefer passing around struct p2m_domain * in / to P2M functions (which would benefit earlier patches already, I think).

But nothing uses it earlier.

If you do as suggested and pass around struct p2m_domain * for p2m_*() functions, you'll quickly find it used, I think.

Quoted 45 lines
Quoted 45 lines
Quoted 32 lines
xen/arch/riscv/p2m.c +19 -0
@@ -1,13 +1,46 @@
   #include <xen/bitops.h>
+#include <xen/domain_page.h>
   #include <xen/event.h>
+#include <xen/iommu.h>
   #include <xen/lib.h>
+#include <xen/mm.h>
+#include <xen/pfn.h>
+#include <xen/rwlock.h>
   #include <xen/sched.h>
   #include <xen/spinlock.h>
   #include <xen/xvmalloc.h>
 
+#include <asm/page.h>
   #include <asm/p2m.h>
   #include <asm/sbi.h>
 
+/*
+ * Force a synchronous P2M TLB flush.
+ *
+ * Must be called with the p2m lock held.
+ */
+static void p2m_force_tlb_flush_sync(struct p2m_domain *p2m)
+{
+    struct domain *d = p2m->domain;
+
+    ASSERT(p2m_is_write_locked(p2m));
+
+    sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid);
+}

xen/arch/riscv/p2m.c line 39

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+
+/* Unlock the flush and do a P2M TLB flush if necessary */
+void p2m_write_unlock(struct p2m_domain *p2m)
+{
+    /*
+     * The final flush is done with the P2M write lock taken to avoid
+     * someone else modifying the P2M wbefore the TLB invalidation has
+     * completed.
+     */
+    p2m_force_tlb_flush_sync(p2m);

The comment ahead of the function says "if necessary". Yet there's no conditional here. I also question the need for a global flush in all cases.

Quoted 7 lines
Quoted 4 lines

Stale comment.

But if p2m page table was modified that it is needed to do a flush for CPUs in d->dirty_cpumask.

Right, but is that true for each and every case where you acquire the lock in write mode? There may e.g. be early-out path which end up doing nothing, yet you would then still flush the TLB.

Initially, I assumed that early-out patch will happen mostly in the cases when some error happen, so it will be okay to flush the TLB each time.

But, yes, I missed some cases when it will be end up doing nothing. I will return back need_flush.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 5 lines
xen/arch/riscv/p2m.c +0 -0
@@ -109,8 +142,33 @@ int p2m_init(struct domain *d)
       spin_lock_init(&d->arch.paging.lock);
       INIT_PAGE_LIST_HEAD(&d->arch.paging.p2m_freelist);
 

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 154

Quoted 10 lines
xen/arch/riscv/p2m.c +8 -0
+    rwlock_init(&p2m->lock);
+    INIT_PAGE_LIST_HEAD(&p2m->pages);
+
       p2m->vmid = INVALID_VMID;
 
+    p2m->default_access = p2m_access_rwx;
+
+    radix_tree_init(&p2m->p2m_type);
+
+#ifdef CONFIG_HAS_PASSTHROUGH

Do you expect this to be conditionally selected on RISC-V?

Quoted 7 lines
Quoted 3 lines

No, once it will be implemented it will be just selected once by config RISC-V. And it was done so because iommu_has_feature() isn't implemented now as IOMMU isn't supported now and depends on CONFIG_HAS_PASSTHROUGH.

If the selection isn't going to be conditional, then I see no reason to have such conditionals in RISC-V-specific code. The piece of code presently inside that #ifdef may simply need adding later, once there's enough infrastructure to allow that code to compile. Or maybe it would even compile fine already now?

I haven't tried. Anyway, I get your point.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/riscv/p2m.c +7 -0
+    /*
+     * Some IOMMUs don't support coherent PT walk. When the p2m is
+     * shared with the CPU, Xen has to make sure that the PT changes have
+     * reached the memory
+     */
+    p2m->clean_pte = is_iommu_enabled(d) &&
+        !iommu_has_feature(d, IOMMU_FEAT_COHERENT_WALK);
Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

The comment talks about shared page tables, yet you don't check whether page table sharing is actually enabled for the domain.

Do we have such function/macros?

We have iommu_hap_pt_share, and we have the per-domain hap_pt_share flag.

Quoted 1 line

It is shared by implementation now.

I don't understand. There's no IOMMU support yet for RISC-V. Hence it's in neither state - not shared, but also not not shared.

In downstream there is a support of IOMMU for RISC-V.

~ Oleksii

#18 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

In reply to Jan Beulich
Quoted 54 lines · Jan Beulich
Quoted 39 lines · Oleksii Kurochko
Quoted 33 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c new file +10 -0
@@ -0,0 +1,115 @@
+#include <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/xvmalloc.h>
+
+#include <asm/p2m.h>
+#include <asm/sbi.h>
+
+static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
Quoted 8 lines

xen/arch/riscv/p2m.c

Quoted 5 lines
xen/arch/riscv/p2m.c new file +5 -0
+
+/*
+ * hgatp's VMID field is 7 or 14 bits. RV64 may support 14-bit VMID.
+ * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1)
+ * concurrent domains.

Which is pretty limiting especially in the RV32 case. Hence why we don't assign a permanent ID to VMs on x86, but rather manage IDs per-CPU (note:
not per-vCPU).

Good point.

Quoted 7 lines

I don't believe anyone will use RV32.
For RV64, the available ID space seems sufficiently large.

However, if it turns out that the value isn't large enough even for RV64, I can rework it to manage IDs per physical CPU.
Wouldn't that approach result in more TLB entries being flushed compared to per-vCPU allocation, potentially leading to slightly worse performance?

Quoted 5 lines

Depends on the condition for when to flush. Of course performance is unavoidably going to suffer if you have only very few VMIDs to use. Nevertheless, as indicated before, the model used on x86 may be a candidate to use here, too. See hvm_asid_handle_vmenter() for the core (and vendor-independent) part of it.

Quoted 5 lines

IIUC, so basically it is just a round-robin and when VMIDs are ran out then just do full guest TLB flush and start to re-use VMIDs from the start. It makes sense to me, I'll implement something similar. (as I'm not really sure that we needdata->core_asid_generation, probably, I will understand it better when start to implement it)

Well. The fewer VMID bits you have the more quickly you will need a new generation. And keep track of the generation you're at you also need to track the present number somewhere.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

What about then to allocate VMID per-domain?

That's what you're doing right now, isn't it? And that gets problematic when you have only very few bits in hgatp.VMID, as mentioned below.

Right, I just phrased my question poorly—sorry about that.

Quoted 8 lines
Quoted 5 lines

What I meant to ask is: does the approach described above actually depend on whether VMIDs are allocated per-domain or per-pCPU? It seems that the main advantage of allocating VMIDs per-pCPU is potentially reducing the number of TLB flushes, since it's more likely that a platform will have more than|VMID_MAX| domains than |VMID_MAX| physical CPUs—am I right?

Seeing that there can be systems with hundreds or even thousands of CPUs, I don't think I can agree here. Plus per-pCPU allocation would similarly get you in trouble when you have only very few VMID bits.

But not so fast as in case of per-domain allocation, right?

I mean that if we have only 4 bits, then in case of per-domain allocation we will need to do TLB flush + VMID re-assigning when we have more then 16 domains.

But in case of per-pCPU allocation we could run 16 domains on 1 pCPU and at the same time in multiprocessor systems we have more pCPUs, which will allow us to run more domains and avoid TLB flushes.
On other hand, it is needed to consider that it's unlikely that a domain will have only one vCPU. And it is likely that amount of vCPUs will be bigger then an amount of domains, so to have a round-robin approach (as x86) without permanent ID allocation for each domain will work better then per-pCPU allocation.
In other words, I'm not 100% sure that I get a point why x86 chose per-pCPU allocation instead of per-domain allocation with having the same VMID for all vCPUs of domains.

~ Oleksii

#19 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

Quoted 70 lines · Oleksii Kurochko
Quoted 54 lines · Jan Beulich
Quoted 39 lines · Oleksii Kurochko
Quoted 33 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c new file +10 -0
@@ -0,0 +1,115 @@
+#include <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/xvmalloc.h>
+
+#include <asm/p2m.h>
+#include <asm/sbi.h>
+
+static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
Quoted 8 lines

xen/arch/riscv/p2m.c line 15

Quoted 5 lines
xen/arch/riscv/p2m.c new file +5 -0
+
+/*
+ * hgatp's VMID field is 7 or 14 bits. RV64 may support 14-bit VMID.
+ * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1)
+ * concurrent domains.

Which is pretty limiting especially in the RV32 case. Hence why we don't assign a permanent ID to VMs on x86, but rather manage IDs per-CPU (note:
not per-vCPU).

Good point.

Quoted 7 lines

I don't believe anyone will use RV32.
For RV64, the available ID space seems sufficiently large.

However, if it turns out that the value isn't large enough even for RV64, I can rework it to manage IDs per physical CPU.
Wouldn't that approach result in more TLB entries being flushed compared to per-vCPU allocation, potentially leading to slightly worse performance?

Quoted 5 lines

Depends on the condition for when to flush. Of course performance is unavoidably going to suffer if you have only very few VMIDs to use. Nevertheless, as indicated before, the model used on x86 may be a candidate to use here, too. See hvm_asid_handle_vmenter() for the core (and vendor-independent) part of it.

Quoted 5 lines

IIUC, so basically it is just a round-robin and when VMIDs are ran out then just do full guest TLB flush and start to re-use VMIDs from the start. It makes sense to me, I'll implement something similar. (as I'm not really sure that we needdata->core_asid_generation, probably, I will understand it better when start to implement it)

Well. The fewer VMID bits you have the more quickly you will need a new generation. And keep track of the generation you're at you also need to track the present number somewhere.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

What about then to allocate VMID per-domain?

That's what you're doing right now, isn't it? And that gets problematic when you have only very few bits in hgatp.VMID, as mentioned below.

Right, I just phrased my question poorly—sorry about that.

Quoted 8 lines
Quoted 5 lines

What I meant to ask is: does the approach described above actually depend on whether VMIDs are allocated per-domain or per-pCPU? It seems that the main advantage of allocating VMIDs per-pCPU is potentially reducing the number of TLB flushes, since it's more likely that a platform will have more than|VMID_MAX| domains than |VMID_MAX| physical CPUs—am I right?

Seeing that there can be systems with hundreds or even thousands of CPUs, I don't think I can agree here. Plus per-pCPU allocation would similarly get you in trouble when you have only very few VMID bits.

But not so fast as in case of per-domain allocation, right?

I mean that if we have only 4 bits, then in case of per-domain allocation we will need to do TLB flush + VMID re-assigning when we have more then 16 domains.

Quoted 7 lines

But in case of per-pCPU allocation we could run 16 domains on 1 pCPU and at the same time in multiprocessor systems we have more pCPUs, which will allow us to run more domains and avoid TLB flushes.
On other hand, it is needed to consider that it's unlikely that a domain will have only one vCPU. And it is likely that amount of vCPUs will be bigger then an amount of domains, so to have a round-robin approach (as x86) without permanent ID allocation for each domain will work better then per-pCPU allocation.

Here you (appear to) say one thing, ...

Quoted 2 lines

In other words, I'm not 100% sure that I get a point why x86 chose per-pCPU allocation instead of per-domain allocation with having the same VMID for all vCPUs of domains.

... and then here the opposite. Overall I'm in severe trouble understanding this reply of yours as a whole, so I fear I can't really respond to it (or even just parts thereof).

Jan

#44 lore raw

Re: [PATCH v2 05/17] xen/riscv: introduce things necessary for p2m initialization

In reply to Oleksii Kurochko
Quoted 25 lines · Oleksii Kurochko
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Oleksii Kurochko
Quoted 18 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +2 -0
@@ -14,6 +18,29 @@
 
   /* Per-p2m-table state */
   struct p2m_domain {
+    /*
+     * Lock that protects updates to the p2m.

xen/arch/riscv/include/asm/p2m.h line 32

Quoted 10 lines
xen/arch/riscv/include/asm/p2m.h +10 -0
+     */
+    rwlock_t lock;
+
+    /* Pages used to construct the p2m */
+    struct page_list_head pages;
+
+    /* Indicate if it is required to clean the cache when writing an entry */
+    bool clean_pte;
+
+    struct radix_tree_root p2m_type;

A field with a p2m_ prefix in a p2m struct?

p2m_ prefix could be really dropped.

Quoted 2 lines

And is this tree really about
just a single "type"?

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Yes, we don't have enough bits in PTE so we need some extra storage to store type.

My question wasn't about that, though. My question was whether in the name "type" (singular) is appropriate. I didn't think you need a tree to store just a single type.

I need tree to store a pair of <gfn, p2m_type>, where gfn is an index. And it seems to me a tree is a good structure for fast insert/search.

Hmm, I'm increasingly puzzled. I tried to emphasize that my question was towards the singular "type" in the variable name. I can't see any relationship between that and your reply. (And yes, using a tree here may be appropriate. There is a concern towards memory consumption, but that's a separate topic.)

Having said that, aiui you don't use the two RSW bits in the PTE. Do you have any plans there? If not, can't they be used to at least represent the most commonly used types, such that the number of entries in that tree can be kept (relatively) low?

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 161

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+    /*
+     * Some IOMMUs don't support coherent PT walk. When the p2m is
+     * shared with the CPU, Xen has to make sure that the PT changes have
+     * reached the memory
+     */
+    p2m->clean_pte = is_iommu_enabled(d) &&
+        !iommu_has_feature(d, IOMMU_FEAT_COHERENT_WALK);

The comment talks about shared page tables, yet you don't check whether page table sharing is actually enabled for the domain.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Do we have such function/macros?

We have iommu_hap_pt_share, and we have the per-domain hap_pt_share flag.

Quoted 1 line

It is shared by implementation now.

I don't understand. There's no IOMMU support yet for RISC-V. Hence it's in neither state - not shared, but also not not shared.

In downstream there is a support of IOMMU for RISC-V.

And there page tables are unconditionally shared? I'll be surprised if no want/need for non-shared page tables would ever appear.

Jan

#20 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

In reply to Jan Beulich
Quoted 76 lines · Jan Beulich
Quoted 68 lines · Oleksii Kurochko
Quoted 54 lines · Jan Beulich
Quoted 39 lines · Oleksii Kurochko
Quoted 33 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c new file +10 -0
@@ -0,0 +1,115 @@
+#include <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/xvmalloc.h>
+
+#include <asm/p2m.h>
+#include <asm/sbi.h>
+
+static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
Quoted 8 lines

xen/arch/riscv/p2m.c line 15

Quoted 5 lines
xen/arch/riscv/p2m.c new file +5 -0
+
+/*
+ * hgatp's VMID field is 7 or 14 bits. RV64 may support 14-bit VMID.
+ * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1)
+ * concurrent domains.

Which is pretty limiting especially in the RV32 case. Hence why we don't assign a permanent ID to VMs on x86, but rather manage IDs per-CPU (note:
not per-vCPU).

Good point.

Quoted 7 lines

I don't believe anyone will use RV32.
For RV64, the available ID space seems sufficiently large.

However, if it turns out that the value isn't large enough even for RV64, I can rework it to manage IDs per physical CPU.
Wouldn't that approach result in more TLB entries being flushed compared to per-vCPU allocation, potentially leading to slightly worse performance?

Quoted 5 lines

Depends on the condition for when to flush. Of course performance is unavoidably going to suffer if you have only very few VMIDs to use. Nevertheless, as indicated before, the model used on x86 may be a candidate to use here, too. See hvm_asid_handle_vmenter() for the core (and vendor-independent) part of it.

Quoted 5 lines

IIUC, so basically it is just a round-robin and when VMIDs are ran out then just do full guest TLB flush and start to re-use VMIDs from the start. It makes sense to me, I'll implement something similar. (as I'm not really sure that we needdata->core_asid_generation, probably, I will understand it better when start to implement it)

Well. The fewer VMID bits you have the more quickly you will need a new generation. And keep track of the generation you're at you also need to track the present number somewhere.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

What about then to allocate VMID per-domain?

That's what you're doing right now, isn't it? And that gets problematic when you have only very few bits in hgatp.VMID, as mentioned below.

Right, I just phrased my question poorly—sorry about that.

Quoted 8 lines
Quoted 5 lines

What I meant to ask is: does the approach described above actually depend on whether VMIDs are allocated per-domain or per-pCPU? It seems that the main advantage of allocating VMIDs per-pCPU is potentially reducing the number of TLB flushes, since it's more likely that a platform will have more than|VMID_MAX| domains than |VMID_MAX| physical CPUs—am I right?

Seeing that there can be systems with hundreds or even thousands of CPUs, I don't think I can agree here. Plus per-pCPU allocation would similarly get you in trouble when you have only very few VMID bits.

But not so fast as in case of per-domain allocation, right?

I mean that if we have only 4 bits, then in case of per-domain allocation we will need to do TLB flush + VMID re-assigning when we have more then 16 domains.

Quoted 7 lines

But in case of per-pCPU allocation we could run 16 domains on 1 pCPU and at the same time in multiprocessor systems we have more pCPUs, which will allow us to run more domains and avoid TLB flushes.
On other hand, it is needed to consider that it's unlikely that a domain will have only one vCPU. And it is likely that amount of vCPUs will be bigger then an amount of domains, so to have a round-robin approach (as x86) without permanent ID allocation for each domain will work better then per-pCPU allocation.

Quoted 7 lines

Here you (appear to) say one thing, ...

Quoted 2 lines

In other words, I'm not 100% sure that I get a point why x86 chose per-pCPU allocation instead of per-domain allocation with having the same VMID for all vCPUs of domains.

... and then here the opposite. Overall I'm in severe trouble understanding this reply of yours as a whole, so I fear I can't really respond to it (or even just parts thereof).

IIUC, x86 allocates VMIDs per physical CPU (pCPU) "dynamically" — these are just sequential numbers, and once VMIDs run out on a given pCPU, there's no guarantee that a vCPU will receive the same VMID again.

On the other hand, RISC-V currently allocates a single VMID per domain, and that VMID is considered "permanent" until the domain is destroyed. This means we are limited to at most VMID_MAX domains. To avoid this limitation, I plan to implement a round-robin reuse approach: when no free VMIDs remain, we start a new generation and begin reusing old VMIDs.

The only remaining design question is whether we want RISC-V to follow a global VMID allocation policy (i.e., one VMID per domain, shared across all of its vCPUs), or adopt a policy similar to x86 with per-CPU VMID allocation (each vCPU gets its own VMID, local to the CPU it's running on).

Each policy has its own trade-offs. But in the case where the number of available VMIDs is small (i.e., low VMIDLEN), a global allocation policy may be more suitable, as it requires fewer VMIDs overall.

So my main question was:
What are the advantages of per-pCPU VMID allocation in scenarios with limited VMID space, and why did x86 choose that design?

From what I can tell, the benefits of per-pCPU VMID allocation include:
- Minimized inter-CPU TLB flushes — since VMIDs are local, TLB entries don’t need to be invalidated on other CPUs when reused.
- Better scalability — this approach works better on systems with a large number of CPUs.
- Frequent VM switches don’t require global TLB flushes — reducing the overhead of context switching.
However, the downside is that this model consumes more VMIDs. For example, if a single domain runs on 4 vCPUs across 4 CPUs, it will consume 4 VMIDs instead of just one.

~ Oleksii

#21 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

Quoted 110 lines · Oleksii Kurochko
Quoted 76 lines · Jan Beulich
Quoted 68 lines · Oleksii Kurochko
Quoted 54 lines · Jan Beulich
Quoted 39 lines · Oleksii Kurochko
Quoted 33 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c new file +10 -0
@@ -0,0 +1,115 @@
+#include <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/xvmalloc.h>
+
+#include <asm/p2m.h>
+#include <asm/sbi.h>
+
+static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
Quoted 8 lines

xen/arch/riscv/p2m.c line 15

Quoted 5 lines
xen/arch/riscv/p2m.c new file +5 -0
+
+/*
+ * hgatp's VMID field is 7 or 14 bits. RV64 may support 14-bit VMID.
+ * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1)
+ * concurrent domains.

Which is pretty limiting especially in the RV32 case. Hence why we don't assign a permanent ID to VMs on x86, but rather manage IDs per-CPU (note:
not per-vCPU).

Good point.

Quoted 7 lines

I don't believe anyone will use RV32.
For RV64, the available ID space seems sufficiently large.

However, if it turns out that the value isn't large enough even for RV64, I can rework it to manage IDs per physical CPU.
Wouldn't that approach result in more TLB entries being flushed compared to per-vCPU allocation, potentially leading to slightly worse performance?

Quoted 5 lines

Depends on the condition for when to flush. Of course performance is unavoidably going to suffer if you have only very few VMIDs to use. Nevertheless, as indicated before, the model used on x86 may be a candidate to use here, too. See hvm_asid_handle_vmenter() for the core (and vendor-independent) part of it.

Quoted 5 lines

IIUC, so basically it is just a round-robin and when VMIDs are ran out then just do full guest TLB flush and start to re-use VMIDs from the start. It makes sense to me, I'll implement something similar. (as I'm not really sure that we needdata->core_asid_generation, probably, I will understand it better when start to implement it)

Well. The fewer VMID bits you have the more quickly you will need a new generation. And keep track of the generation you're at you also need to track the present number somewhere.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

What about then to allocate VMID per-domain?

That's what you're doing right now, isn't it? And that gets problematic when you have only very few bits in hgatp.VMID, as mentioned below.

Right, I just phrased my question poorly—sorry about that.

Quoted 8 lines
Quoted 5 lines

What I meant to ask is: does the approach described above actually depend on whether VMIDs are allocated per-domain or per-pCPU? It seems that the main advantage of allocating VMIDs per-pCPU is potentially reducing the number of TLB flushes, since it's more likely that a platform will have more than|VMID_MAX| domains than |VMID_MAX| physical CPUs—am I right?

Seeing that there can be systems with hundreds or even thousands of CPUs, I don't think I can agree here. Plus per-pCPU allocation would similarly get you in trouble when you have only very few VMID bits.

But not so fast as in case of per-domain allocation, right?

I mean that if we have only 4 bits, then in case of per-domain allocation we will need to do TLB flush + VMID re-assigning when we have more then 16 domains.

Quoted 7 lines

But in case of per-pCPU allocation we could run 16 domains on 1 pCPU and at the same time in multiprocessor systems we have more pCPUs, which will allow us to run more domains and avoid TLB flushes.
On other hand, it is needed to consider that it's unlikely that a domain will have only one vCPU. And it is likely that amount of vCPUs will be bigger then an amount of domains, so to have a round-robin approach (as x86) without permanent ID allocation for each domain will work better then per-pCPU allocation.

Quoted 7 lines

Here you (appear to) say one thing, ...

Quoted 2 lines

In other words, I'm not 100% sure that I get a point why x86 chose per-pCPU allocation instead of per-domain allocation with having the same VMID for all vCPUs of domains.

... and then here the opposite. Overall I'm in severe trouble understanding this reply of yours as a whole, so I fear I can't really respond to it (or even just parts thereof).

IIUC, x86 allocates VMIDs per physical CPU (pCPU) "dynamically" — these are just sequential numbers, and once VMIDs run out on a given pCPU, there's no guarantee that a vCPU will receive the same VMID again.

On the other hand, RISC-V currently allocates a single VMID per domain, and that VMID is considered "permanent" until the domain is destroyed. This means we are limited to at most VMID_MAX domains. To avoid this limitation, I plan to implement a round-robin reuse approach: when no free VMIDs remain, we start a new generation and begin reusing old VMIDs.

The only remaining design question is whether we want RISC-V to follow a global VMID allocation policy (i.e., one VMID per domain, shared across all of its vCPUs), or adopt a policy similar to x86 with per-CPU VMID allocation (each vCPU gets its own VMID, local to the CPU it's running on).

Each policy has its own trade-offs. But in the case where the number of available VMIDs is small (i.e., low VMIDLEN), a global allocation policy may be more suitable, as it requires fewer VMIDs overall.

So my main question was:
What are the advantages of per-pCPU VMID allocation in scenarios with limited VMID space, and why did x86 choose that design?

Quoted 1 line

From what I can tell, the benefits of per-pCPU VMID allocation include:

Quoted 9 lines

- Minimized inter-CPU TLB flushes — since VMIDs are local, TLB entries don’t need to be invalidated on other CPUs when reused.
- Better scalability — this approach works better on systems with a large number of CPUs.
- Frequent VM switches don’t require global TLB flushes — reducing the overhead of context switching.
However, the downside is that this model consumes more VMIDs. For example, if a single domain runs on 4 vCPUs across 4 CPUs, it will consume 4 VMIDs instead of just one.

Consider you have 4 bits for VMIDs, resulting in 16 VMID values.

If you have a system with 32 physical CPUs and 32 domains with 1 vcpu each on that system, your scheme would NOT allow to keep each physical cpu busy by running a domain on it, as only 16 domains could be active at the same time.

Juergen

#45 lore raw

Re: [PATCH v2 05/17] xen/riscv: introduce things necessary for p2m initialization

In reply to Jan Beulich
Quoted 30 lines · Jan Beulich
Quoted 29 lines · Oleksii Kurochko
Quoted 28 lines · Jan Beulich
Quoted 19 lines · Oleksii Kurochko
Quoted 18 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +2 -0
@@ -14,6 +18,29 @@
 
    /* Per-p2m-table state */
    struct p2m_domain {
+    /*
+     * Lock that protects updates to the p2m.

xen/arch/riscv/include/asm/p2m.h line 32

Quoted 10 lines
xen/arch/riscv/include/asm/p2m.h +10 -0
+     */
+    rwlock_t lock;
+
+    /* Pages used to construct the p2m */
+    struct page_list_head pages;
+
+    /* Indicate if it is required to clean the cache when writing an entry */
+    bool clean_pte;
+
+    struct radix_tree_root p2m_type;

A field with a p2m_ prefix in a p2m struct?

Quoted 5 lines

p2m_ prefix could be really dropped.

Quoted 2 lines

And is this tree really about
just a single "type"?

Yes, we don't have enough bits in PTE so we need some extra storage to store type.

My question wasn't about that, though. My question was whether in the name "type" (singular) is appropriate. I didn't think you need a tree to store just a single type.

Quoted 6 lines
Quoted 2 lines

I need tree to store a pair of <gfn, p2m_type>, where gfn is an index. And it seems to me a tree is a good structure for fast insert/search.

Hmm, I'm increasingly puzzled. I tried to emphasize that my question was towards the singular "type" in the variable name. I can't see any relationship between that and your reply. (And yes, using a tree here may be appropriate. There is a concern towards memory consumption, but that's a separate topic.)

Oh, I got your initial intention. For sure, it should be "types".

Quoted 5 lines

Having said that, aiui you don't use the two RSW bits in the PTE. Do you have any plans there? If not, can't they be used to at least represent the most commonly used types, such that the number of entries in that tree can be kept (relatively) low?

It could be really an option for optimization.

In this case I have to p2m_type_t by adding a new type p2m_tree_type:
typedef enum {

    p2m_invalid = 0,    /* Nothing mapped here */
    p2m_ram_rw,         /* Normal read/write domain RAM */
    p2m_ram_ro,         /* Read-only */

+ p2m_tree_type, /* The types below p2m_free_type will be stored outside PTE's bits */

p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */
p2m_grant_map_rw, /* Read/write grant mapping */
p2m_grant_map_ro, /* Read-only grant mapping */
} p2m_type_t;

Probably, it make sense to switch p2m_ram_ro and p2m_mmio_direct_dev. I think device mapping is more often operations.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 161

Quoted 8 lines
xen/arch/riscv/p2m.c +7 -0
+    /*
+     * Some IOMMUs don't support coherent PT walk. When the p2m is
+     * shared with the CPU, Xen has to make sure that the PT changes have
+     * reached the memory
+     */
+    p2m->clean_pte = is_iommu_enabled(d) &&
+        !iommu_has_feature(d, IOMMU_FEAT_COHERENT_WALK);

The comment talks about shared page tables, yet you don't check whether page table sharing is actually enabled for the domain.

Do we have such function/macros?

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

We have iommu_hap_pt_share, and we have the per-domain hap_pt_share flag.

Quoted 1 line

It is shared by implementation now.

I don't understand. There's no IOMMU support yet for RISC-V. Hence it's in neither state - not shared, but also not not shared.

In downstream there is a support of IOMMU for RISC-V.

And there page tables are unconditionally shared? I'll be surprised if no want/need for non-shared page tables would ever appear.

At the moment, yes, but it isn't strict limitation. So yes, it should be page tables should be conditionally shared.

~ Oleksii

#22 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

In reply to Juergen Gross
Quoted 126 lines · Juergen Gross
Quoted 112 lines · Oleksii Kurochko
Quoted 78 lines · Jan Beulich
Quoted 70 lines · Oleksii Kurochko
Quoted 56 lines · Jan Beulich
Quoted 41 lines · Oleksii Kurochko
Quoted 35 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c new file +12 -0
@@ -0,0 +1,115 @@
+#include <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/xvmalloc.h>
+
+#include <asm/p2m.h>
+#include <asm/sbi.h>
+
+static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
+
+/*
Quoted 8 lines
Quoted 5 lines
xen/arch/riscv/p2m.c new file +1 -0
+ * hgatp's VMID field is 7 or 14 bits. RV64 may support 

14-bit VMID.
+ * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1)
+ * concurrent domains.

Which is pretty limiting especially in the RV32 case. Hence why we don't assign a permanent ID to VMs on x86, but rather manage IDs per-CPU (note:
not per-vCPU).

Good point.

Quoted 7 lines

I don't believe anyone will use RV32.
For RV64, the available ID space seems sufficiently large.

However, if it turns out that the value isn't large enough even for RV64, I can rework it to manage IDs per physical CPU.
Wouldn't that approach result in more TLB entries being flushed compared to per-vCPU allocation, potentially leading to slightly worse performance?

Quoted 5 lines

Depends on the condition for when to flush. Of course performance is unavoidably going to suffer if you have only very few VMIDs to use. Nevertheless, as indicated before, the model used on x86 may be a candidate to use here, too. See hvm_asid_handle_vmenter() for the core (and vendor-independent) part of it.

Quoted 5 lines

IIUC, so basically it is just a round-robin and when VMIDs are ran out then just do full guest TLB flush and start to re-use VMIDs from the start. It makes sense to me, I'll implement something similar. (as I'm not really sure that we needdata->core_asid_generation, probably, I will understand it better when start to implement it)

Well. The fewer VMID bits you have the more quickly you will need a new generation. And keep track of the generation you're at you also need to track the present number somewhere.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

What about then to allocate VMID per-domain?

That's what you're doing right now, isn't it? And that gets problematic when you have only very few bits in hgatp.VMID, as mentioned below.

Right, I just phrased my question poorly—sorry about that.

Quoted 8 lines
Quoted 5 lines

What I meant to ask is: does the approach described above actually depend on whether VMIDs are allocated per-domain or per-pCPU? It seems that the main advantage of allocating VMIDs per-pCPU is potentially reducing the number of TLB flushes, since it's more likely that a platform will have more than|VMID_MAX| domains than |VMID_MAX| physical CPUs—am I right?

Seeing that there can be systems with hundreds or even thousands of CPUs, I don't think I can agree here. Plus per-pCPU allocation would similarly get you in trouble when you have only very few VMID bits.

But not so fast as in case of per-domain allocation, right?

I mean that if we have only 4 bits, then in case of per-domain allocation we will need to do TLB flush + VMID re-assigning when we have more then 16 domains.

Quoted 7 lines

But in case of per-pCPU allocation we could run 16 domains on 1 pCPU and at the same time in multiprocessor systems we have more pCPUs, which will allow us to run more domains and avoid TLB flushes.
On other hand, it is needed to consider that it's unlikely that a domain will have only one vCPU. And it is likely that amount of vCPUs will be bigger then an amount of domains, so to have a round-robin approach (as x86) without permanent ID allocation for each domain will work better then per-pCPU allocation.

Quoted 7 lines

Here you (appear to) say one thing, ...

Quoted 2 lines

In other words, I'm not 100% sure that I get a point why x86 chose per-pCPU allocation instead of per-domain allocation with having the same VMID for all vCPUs of domains.

... and then here the opposite. Overall I'm in severe trouble understanding this reply of yours as a whole, so I fear I can't really respond to it (or even just parts thereof).

IIUC, x86 allocates VMIDs per physical CPU (pCPU) "dynamically" — these are just sequential numbers, and once VMIDs run out on a given pCPU, there's no guarantee that a vCPU will receive the same VMID again.

On the other hand, RISC-V currently allocates a single VMID per domain, and that VMID is considered "permanent" until the domain is destroyed. This means we are limited to at most VMID_MAX domains. To avoid this limitation, I plan to implement a round-robin reuse approach: when no free VMIDs remain, we start a new generation and begin reusing old VMIDs.

The only remaining design question is whether we want RISC-V to follow a global VMID allocation policy (i.e., one VMID per domain, shared across all of its vCPUs), or adopt a policy similar to x86 with per-CPU VMID allocation (each vCPU gets its own VMID, local to the CPU it's running on).

Each policy has its own trade-offs. But in the case where the number of available VMIDs is small (i.e., low VMIDLEN), a global allocation policy may be more suitable, as it requires fewer VMIDs overall.

So my main question was:
What are the advantages of per-pCPU VMID allocation in scenarios with limited VMID space, and why did x86 choose that design?

Quoted 1 line

From what I can tell, the benefits of per-pCPU VMID allocation include:

Quoted 12 lines

- Minimized inter-CPU TLB flushes — since VMIDs are local, TLB entries don’t need
   to be invalidated on other CPUs when reused.
- Better scalability — this approach works better on systems with a large number
   of CPUs.
- Frequent VM switches don’t require global TLB flushes — reducing the overhead
   of context switching.
However, the downside is that this model consumes more VMIDs. For example, if a single domain runs on 4 vCPUs across 4 CPUs, it will consume 4 VMIDs instead of just one.

Quoted 6 lines

Consider you have 4 bits for VMIDs, resulting in 16 VMID values.

If you have a system with 32 physical CPUs and 32 domains with 1 vcpu each on that system, your scheme would NOT allow to keep each physical cpu busy by running a domain on it, as only 16 domains could be active at the same time.

It makes sense to me.

Thanks.

~ Oleksii

#25 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

In reply to Oleksii Kurochko
Quoted 96 lines · Oleksii Kurochko
Quoted 76 lines · Jan Beulich
Quoted 68 lines · Oleksii Kurochko
Quoted 54 lines · Jan Beulich
Quoted 39 lines · Oleksii Kurochko
Quoted 33 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c new file +10 -0
@@ -0,0 +1,115 @@
+#include <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/xvmalloc.h>
+
+#include <asm/p2m.h>
+#include <asm/sbi.h>
+
+static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
Quoted 8 lines

xen/arch/riscv/p2m.c line 15

Quoted 5 lines
xen/arch/riscv/p2m.c new file +5 -0
+
+/*
+ * hgatp's VMID field is 7 or 14 bits. RV64 may support 14-bit VMID.
+ * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1)
+ * concurrent domains.

Which is pretty limiting especially in the RV32 case. Hence why we don't assign a permanent ID to VMs on x86, but rather manage IDs per-CPU (note:
not per-vCPU).

Good point.

Quoted 7 lines

I don't believe anyone will use RV32.
For RV64, the available ID space seems sufficiently large.

However, if it turns out that the value isn't large enough even for RV64, I can rework it to manage IDs per physical CPU.
Wouldn't that approach result in more TLB entries being flushed compared to per-vCPU allocation, potentially leading to slightly worse performance?

Quoted 5 lines

Depends on the condition for when to flush. Of course performance is unavoidably going to suffer if you have only very few VMIDs to use. Nevertheless, as indicated before, the model used on x86 may be a candidate to use here, too. See hvm_asid_handle_vmenter() for the core (and vendor-independent) part of it.

Quoted 5 lines

IIUC, so basically it is just a round-robin and when VMIDs are ran out then just do full guest TLB flush and start to re-use VMIDs from the start. It makes sense to me, I'll implement something similar. (as I'm not really sure that we needdata->core_asid_generation, probably, I will understand it better when start to implement it)

Well. The fewer VMID bits you have the more quickly you will need a new generation. And keep track of the generation you're at you also need to track the present number somewhere.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

What about then to allocate VMID per-domain?

That's what you're doing right now, isn't it? And that gets problematic when you have only very few bits in hgatp.VMID, as mentioned below.

Right, I just phrased my question poorly—sorry about that.

Quoted 8 lines
Quoted 5 lines

What I meant to ask is: does the approach described above actually depend on whether VMIDs are allocated per-domain or per-pCPU? It seems that the main advantage of allocating VMIDs per-pCPU is potentially reducing the number of TLB flushes, since it's more likely that a platform will have more than|VMID_MAX| domains than |VMID_MAX| physical CPUs—am I right?

Seeing that there can be systems with hundreds or even thousands of CPUs, I don't think I can agree here. Plus per-pCPU allocation would similarly get you in trouble when you have only very few VMID bits.

But not so fast as in case of per-domain allocation, right?

I mean that if we have only 4 bits, then in case of per-domain allocation we will need to do TLB flush + VMID re-assigning when we have more then 16 domains.

Quoted 7 lines

But in case of per-pCPU allocation we could run 16 domains on 1 pCPU and at the same time in multiprocessor systems we have more pCPUs, which will allow us to run more domains and avoid TLB flushes.
On other hand, it is needed to consider that it's unlikely that a domain will have only one vCPU. And it is likely that amount of vCPUs will be bigger then an amount of domains, so to have a round-robin approach (as x86) without permanent ID allocation for each domain will work better then per-pCPU allocation.

Quoted 7 lines

Here you (appear to) say one thing, ...

Quoted 2 lines

In other words, I'm not 100% sure that I get a point why x86 chose per-pCPU allocation instead of per-domain allocation with having the same VMID for all vCPUs of domains.

... and then here the opposite. Overall I'm in severe trouble understanding this reply of yours as a whole, so I fear I can't really respond to it (or even just parts thereof).

IIUC, x86 allocates VMIDs per physical CPU (pCPU) "dynamically" — these are just sequential numbers, and once VMIDs run out on a given pCPU, there's no guarantee that a vCPU will receive the same VMID again.

On the other hand, RISC-V currently allocates a single VMID per domain, and that VMID is considered "permanent" until the domain is destroyed. This means we are limited to at most VMID_MAX domains. To avoid this limitation, I plan to implement a round-robin reuse approach: when no free VMIDs remain, we start a new generation and begin reusing old VMIDs.

Quoted 4 lines

The only remaining design question is whether we want RISC-V to follow a global VMID allocation policy (i.e., one VMID per domain, shared across all of its vCPUs), or adopt a policy similar to x86 with per-CPU VMID allocation (each vCPU gets its own VMID, local to the CPU it's running on).

Besides what Jürgen has said, what would this mean if you have 16 VMIDs and a 17th domain appears? You can't "take away" the VMID from any domain, unless you fully suspended it first (that is, all of its vCPU-s).

Quoted 8 lines

Each policy has its own trade-offs. But in the case where the number of available VMIDs is small (i.e., low VMIDLEN), a global allocation policy may be more suitable, as it requires fewer VMIDs overall.

So my main question was:
What are the advantages of per-pCPU VMID allocation in scenarios with limited VMID space, and why did x86 choose that design?

Quoted 10 lines

From what I can tell, the benefits of per-pCPU VMID allocation include:
- Minimized inter-CPU TLB flushes — since VMIDs are local, TLB entries don’t need to be invalidated on other CPUs when reused.
- Better scalability — this approach works better on systems with a large number of CPUs.
- Frequent VM switches don’t require global TLB flushes — reducing the overhead of context switching.
However, the downside is that this model consumes more VMIDs. For example, if a single domain runs on 4 vCPUs across 4 CPUs, it will consume 4 VMIDs instead of just one.

I don't understand this, nor why it's a downside. Looking at a domain as a whole simply doesn't make sense in this model. Or if you do, then you need to consider the system-wide number of VMIDs you have available:
(1 << VMIDLEN) * num_online_cpus(). That is, in your calculation a domain with 4 vCPU-s may indeed use up to 4 VMIDs at a time, but out of a pool at least 4 times the size of that of an individual pCPU.

Jan

#23 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

In reply to Juergen Gross

Le 26/06/2025 à 13:46, Juergen Gross a écrit :

Quoted 18 lines · Oleksii Kurochko
Quoted 8 lines · Jan Beulich

- Minimized inter-CPU TLB flushes — since VMIDs are local, TLB entries don’t need
   to be invalidated on other CPUs when reused.
- Better scalability — this approach works better on systems with a large number
   of CPUs.

Quoted 8 lines

- Frequent VM switches don’t require global TLB flushes — reducing the overhead
   of context switching.
However, the downside is that this model consumes more VMIDs. For example,
if a single domain runs on 4 vCPUs across 4 CPUs, it will consume 4 VMIDs instead
of just one.

Quoted 7 lines

Consider you have 4 bits for VMIDs, resulting in 16 VMID values.

If you have a system with 32 physical CPUs and 32 domains with 1 vcpu each on that system, your scheme would NOT allow to keep each physical cpu busy by running a domain on it, as only 16 domains could be active at the same time.

Why not instead consider dropping use of VMID in case there is no one remaining ?
(i.e systematically flush the guest TLB before entering the vcpu and using a "blank" VMID)

I don't expect a lot of platforms to allow for 32 pCPU while not giving more than 16 VMID values. So it would just be less efficient in that case at worst.

Teddy

Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#26 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

In reply to Jan Beulich
Quoted 95 lines · Jan Beulich
Quoted 94 lines · Oleksii Kurochko
Quoted 76 lines · Jan Beulich
Quoted 68 lines · Oleksii Kurochko
Quoted 54 lines · Jan Beulich
Quoted 39 lines · Oleksii Kurochko
Quoted 33 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c new file +10 -0
@@ -0,0 +1,115 @@
+#include <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/spinlock.h>
+#include <xen/xvmalloc.h>
+
+#include <asm/p2m.h>
+#include <asm/sbi.h>
+
+static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
Quoted 8 lines

xen/arch/riscv/p2m.c line 15

Quoted 5 lines
xen/arch/riscv/p2m.c new file +5 -0
+
+/*
+ * hgatp's VMID field is 7 or 14 bits. RV64 may support 14-bit VMID.
+ * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1)
+ * concurrent domains.

Which is pretty limiting especially in the RV32 case. Hence why we don't assign a permanent ID to VMs on x86, but rather manage IDs per-CPU (note:
not per-vCPU).

Good point.

Quoted 7 lines

I don't believe anyone will use RV32.
For RV64, the available ID space seems sufficiently large.

However, if it turns out that the value isn't large enough even for RV64, I can rework it to manage IDs per physical CPU.
Wouldn't that approach result in more TLB entries being flushed compared to per-vCPU allocation, potentially leading to slightly worse performance?

Quoted 5 lines

Depends on the condition for when to flush. Of course performance is unavoidably going to suffer if you have only very few VMIDs to use. Nevertheless, as indicated before, the model used on x86 may be a candidate to use here, too. See hvm_asid_handle_vmenter() for the core (and vendor-independent) part of it.

Quoted 5 lines

IIUC, so basically it is just a round-robin and when VMIDs are ran out then just do full guest TLB flush and start to re-use VMIDs from the start. It makes sense to me, I'll implement something similar. (as I'm not really sure that we needdata->core_asid_generation, probably, I will understand it better when start to implement it)

Well. The fewer VMID bits you have the more quickly you will need a new generation. And keep track of the generation you're at you also need to track the present number somewhere.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

What about then to allocate VMID per-domain?

That's what you're doing right now, isn't it? And that gets problematic when you have only very few bits in hgatp.VMID, as mentioned below.

Right, I just phrased my question poorly—sorry about that.

Quoted 8 lines
Quoted 5 lines

What I meant to ask is: does the approach described above actually depend on whether VMIDs are allocated per-domain or per-pCPU? It seems that the main advantage of allocating VMIDs per-pCPU is potentially reducing the number of TLB flushes, since it's more likely that a platform will have more than|VMID_MAX| domains than |VMID_MAX| physical CPUs—am I right?

Seeing that there can be systems with hundreds or even thousands of CPUs, I don't think I can agree here. Plus per-pCPU allocation would similarly get you in trouble when you have only very few VMID bits.

But not so fast as in case of per-domain allocation, right?

I mean that if we have only 4 bits, then in case of per-domain allocation we will need to do TLB flush + VMID re-assigning when we have more then 16 domains.

Quoted 7 lines

But in case of per-pCPU allocation we could run 16 domains on 1 pCPU and at the same time in multiprocessor systems we have more pCPUs, which will allow us to run more domains and avoid TLB flushes.
On other hand, it is needed to consider that it's unlikely that a domain will have only one vCPU. And it is likely that amount of vCPUs will be bigger then an amount of domains, so to have a round-robin approach (as x86) without permanent ID allocation for each domain will work better then per-pCPU allocation.

Quoted 7 lines

Here you (appear to) say one thing, ...

Quoted 2 lines

In other words, I'm not 100% sure that I get a point why x86 chose per-pCPU allocation instead of per-domain allocation with having the same VMID for all vCPUs of domains.

... and then here the opposite. Overall I'm in severe trouble understanding this reply of yours as a whole, so I fear I can't really respond to it (or even just parts thereof).

IIUC, x86 allocates VMIDs per physical CPU (pCPU) "dynamically" — these are just sequential numbers, and once VMIDs run out on a given pCPU, there's no guarantee that a vCPU will receive the same VMID again.

On the other hand, RISC-V currently allocates a single VMID per domain, and that VMID is considered "permanent" until the domain is destroyed. This means we are limited to at most VMID_MAX domains. To avoid this limitation, I plan to implement a round-robin reuse approach: when no free VMIDs remain, we start a new generation and begin reusing old VMIDs.

Quoted 7 lines
Quoted 4 lines

The only remaining design question is whether we want RISC-V to follow a global VMID allocation policy (i.e., one VMID per domain, shared across all of its vCPUs), or adopt a policy similar to x86 with per-CPU VMID allocation (each vCPU gets its own VMID, local to the CPU it's running on).

Besides what Jürgen has said, what would this mean if you have 16 VMIDs and a 17th domain appears? You can't "take away" the VMID from any domain, unless you fully suspended it first (that is, all of its vCPU-s).

In this case, use of VMID could be dropped and just flush the guest TLB before entering domain. Not efficient, but still an option.

Quoted 19 lines
Quoted 9 lines

Each policy has its own trade-offs. But in the case where the number of available VMIDs is small (i.e., low VMIDLEN), a global allocation policy may be more suitable, as it requires fewer VMIDs overall.

So my main question was:
What are the advantages of per-pCPU VMID allocation in scenarios with limited VMID space, and why did x86 choose that design?

Quoted 10 lines

From what I can tell, the benefits of per-pCPU VMID allocation include:
- Minimized inter-CPU TLB flushes — since VMIDs are local, TLB entries don’t need to be invalidated on other CPUs when reused.
- Better scalability — this approach works better on systems with a large number of CPUs.
- Frequent VM switches don’t require global TLB flushes — reducing the overhead of context switching.
However, the downside is that this model consumes more VMIDs. For example, if a single domain runs on 4 vCPUs across 4 CPUs, it will consume 4 VMIDs instead of just one.

Quoted 6 lines

I don't understand this, nor why it's a downside. Looking at a domain as a whole simply doesn't make sense in this model. Or if you do, then you need to consider the system-wide number of VMIDs you have available:
(1 << VMIDLEN) * num_online_cpus(). That is, in your calculation a domain with 4 vCPU-s may indeed use up to 4 VMIDs at a time, but out of a pool at least 4 times the size of that of an individual pCPU.

Good point, I thought about that too.

~ Oleksii

#24 lore raw

Re: [PATCH v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement

In reply to Teddy Astie
Quoted 26 lines · Teddy Astie

Le 26/06/2025 à 13:46, Juergen Gross a écrit :

Quoted 18 lines · Oleksii Kurochko
Quoted 8 lines · Jan Beulich

- Minimized inter-CPU TLB flushes — since VMIDs are local, TLB entries don’t need
   to be invalidated on other CPUs when reused.
- Better scalability — this approach works better on systems with a large number
   of CPUs.

Quoted 8 lines

- Frequent VM switches don’t require global TLB flushes — reducing the overhead
   of context switching.
However, the downside is that this model consumes more VMIDs. For example,
if a single domain runs on 4 vCPUs across 4 CPUs, it will consume 4 VMIDs instead
of just one.

Quoted 6 lines

Consider you have 4 bits for VMIDs, resulting in 16 VMID values.

If you have a system with 32 physical CPUs and 32 domains with 1 vcpu each on that system, your scheme would NOT allow to keep each physical cpu busy by running a domain on it, as only 16 domains could be active at the same time.

Quoted 4 lines

Why not instead consider dropping use of VMID in case there is no one remaining ?
(i.e systematically flush the guest TLB before entering the vcpu and using a "blank" VMID)

Why would one want to do that, when there's a better scheme available? And how would you decide which VMs to penalize?

Quoted 3 lines

I don't expect a lot of platforms to allow for 32 pCPU while not giving more than 16 VMID values. So it would just be less efficient in that case at worst.

How would you know? How many CPUs (cores) to have in a system is entirely independent of the capabilities of the individual CPUs.

Jan

#57 lore raw

Re: [PATCH v2 08/17] xen/riscv: add new p2m types and helper macros for type classification

In reply to Oleksii Kurochko

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 64

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -61,8 +61,28 @@ struct p2m_domain {
 typedef enum {
     p2m_invalid = 0,    /* Nothing mapped here */
     p2m_ram_rw,         /* Normal read/write domain RAM */
+    p2m_ram_ro,         /* Read-only; writes are silently dropped */

As indicated before - this type should be added when the special handling that it requires is also introduced.

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 65

Quoted 1 line
xen/arch/riscv/include/asm/p2m.h +1 -0
+    p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */

What's the _dev suffix indicating here?

Jan

#58 lore raw

Re: [PATCH v2 08/17] xen/riscv: add new p2m types and helper macros for type classification

Quoted 10 lines · Jan Beulich

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 64

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -61,8 +61,28 @@ struct p2m_domain {
  typedef enum {
      p2m_invalid = 0,    /* Nothing mapped here */
      p2m_ram_rw,         /* Normal read/write domain RAM */
+    p2m_ram_ro,         /* Read-only; writes are silently dropped */

As indicated before - this type should be added when the special handling that it requires is also introduced.

Perhaps, I missed that. I will drop this type for now.

Quoted 3 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 65

Quoted 2 lines
xen/arch/riscv/include/asm/p2m.h +1 -0
+    p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */

What's the _dev suffix indicating here?

It indicates that it is device memory, probably, it isn't so necessary in case of RISC-V as spec doesn't use such terminology. In RISC-V there is only available IO, NC. And we are |using PTE_PBMT_IO for |p2m_mmio_direct_dev.

Maybe it would be better just to rename s/p2m_mmio_direct_dev/p2m_mmio_direct_io.

~ Oleksii

#59 lore raw

Re: [PATCH v2 08/17] xen/riscv: add new p2m types and helper macros for type classification

Quoted 15 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 64

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -61,8 +61,28 @@ struct p2m_domain {
  typedef enum {
      p2m_invalid = 0,    /* Nothing mapped here */
      p2m_ram_rw,         /* Normal read/write domain RAM */
+    p2m_ram_ro,         /* Read-only; writes are silently dropped */

As indicated before - this type should be added when the special handling that it requires is also introduced.

Perhaps, I missed that. I will drop this type for now.

Quoted 7 lines
Quoted 2 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 65

Quoted 1 line
xen/arch/riscv/include/asm/p2m.h +1 -0
+    p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */

What's the _dev suffix indicating here?

It indicates that it is device memory, probably, it isn't so necessary in case of RISC-V as spec doesn't use such terminology. In RISC-V there is only available IO, NC. And we are |using PTE_PBMT_IO for |p2m_mmio_direct_dev.

Maybe it would be better just to rename s/p2m_mmio_direct_dev/p2m_mmio_direct_io.

I forgot that p2m_mmio_direct_dev is used by common code for dom0less code (handle_passthrough_prop())

~ Oleksii

#65 lore raw

Re: [PATCH v2 08/17] xen/riscv: add new p2m types and helper macros for type classification

In reply to Oleksii Kurochko
Quoted 15 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 64

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -61,8 +61,28 @@ struct p2m_domain {
  typedef enum {
      p2m_invalid = 0,    /* Nothing mapped here */
      p2m_ram_rw,         /* Normal read/write domain RAM */
+    p2m_ram_ro,         /* Read-only; writes are silently dropped */

As indicated before - this type should be added when the special handling that it requires is also introduced.

Perhaps, I missed that. I will drop this type for now.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 65

Quoted 1 line
xen/arch/riscv/include/asm/p2m.h +1 -0
+    p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */

What's the _dev suffix indicating here?

It indicates that it is device memory, probably, it isn't so necessary in case of RISC-V as spec doesn't use such terminology. In RISC-V there is only available IO, NC. And we are |using PTE_PBMT_IO for |p2m_mmio_direct_dev.

Maybe it would be better just to rename s/p2m_mmio_direct_dev/p2m_mmio_direct_io.

And what would the _io suffix indicate, beyond what "mmio" already indicates?

Jan

#60 lore raw

Re: [PATCH v2 08/17] xen/riscv: add new p2m types and helper macros for type classification

In reply to Oleksii Kurochko
Quoted 15 lines · Oleksii Kurochko
Quoted 14 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 64

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -61,8 +61,28 @@ struct p2m_domain {
  typedef enum {
      p2m_invalid = 0,    /* Nothing mapped here */
      p2m_ram_rw,         /* Normal read/write domain RAM */
+    p2m_ram_ro,         /* Read-only; writes are silently dropped */

As indicated before - this type should be added when the special handling that it requires is also introduced.

Perhaps, I missed that. I will drop this type for now.

Quoted 1 line
Quoted 1 line
xen/arch/riscv/include/asm/p2m.h +1 -0
+    p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */
Quoted 8 lines
Quoted 6 lines
Quoted 1 line

What's the _dev suffix indicating here?

It indicates that it is device memory, probably, it isn't so necessary in case of RISC-V as spec doesn't use such terminology. In RISC-V there is only available IO, NC. And we are |using PTE_PBMT_IO for |p2m_mmio_direct_dev.

Maybe it would be better just to rename s/p2m_mmio_direct_dev/p2m_mmio_direct_io.

I forgot that p2m_mmio_direct_dev is used by common code for dom0less code (handle_passthrough_prop())

That'll want abstracting out, I think. I don't view it as helpful to clutter RISC-V (and later perhaps also PPC) with Arm-specific terminology.

Jan

#66 lore raw

Re: [PATCH v2 08/17] xen/riscv: add new p2m types and helper macros for type classification

In reply to Jan Beulich
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 64

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -61,8 +61,28 @@ struct p2m_domain {
   typedef enum {
       p2m_invalid = 0,    /* Nothing mapped here */
       p2m_ram_rw,         /* Normal read/write domain RAM */
+    p2m_ram_ro,         /* Read-only; writes are silently dropped */

As indicated before - this type should be added when the special handling that it requires is also introduced.

Perhaps, I missed that. I will drop this type for now.

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 65

Quoted 1 line
xen/arch/riscv/include/asm/p2m.h +1 -0
+    p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */

What's the _dev suffix indicating here?

It indicates that it is device memory, probably, it isn't so necessary in case of RISC-V as spec doesn't use such terminology. In RISC-V there is only available IO, NC. And we are |using PTE_PBMT_IO for |p2m_mmio_direct_dev.

Maybe it would be better just to rename s/p2m_mmio_direct_dev/p2m_mmio_direct_io.

And what would the _io suffix indicate, beyond what "mmio" already indicates?

Just that PBMT_IO will be used for device memory and not PBMT_NC.

~ Oleksii

#47 lore raw

Re: [PATCH v2 06/17] xen/riscv: add root page table allocation

In reply to Oleksii Kurochko

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 33

Quoted 11 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +5 -0
@@ -26,6 +26,12 @@ struct p2m_domain {
     /* Pages used to construct the p2m */
     struct page_list_head pages;
 
+    /* The root of the p2m tree. May be concatenated */
+    struct page_info *root;
+
+    /* Address Translation Table for the p2m */
+    paddr_t hgatp;

Does this really need holding in a struct field? Can't is be re-created at any time from "root" above? And such re-creation is apparently infrequent, if happening at all after initial allocation. (But of course I don't know what future patches of yours will bring.) This is even more so if ...

Quoted 5 lines
xen/arch/riscv/include/asm/riscv_encoding.h +0 -0
@@ -133,11 +133,13 @@
 #define HGATP_MODE_SV48X4		_UL(9)
 

xen/arch/riscv/include/asm/riscv_encoding.h line 144

Quoted 10 lines
xen/arch/riscv/include/asm/riscv_encoding.h +2 -0
 #define HGATP32_MODE_SHIFT		31
+#define HGATP32_MODE_MASK		_UL(0x80000000)
 #define HGATP32_VMID_SHIFT		22
 #define HGATP32_VMID_MASK		_UL(0x1FC00000)
 #define HGATP32_PPN			_UL(0x003FFFFF)
 
 #define HGATP64_MODE_SHIFT		60
+#define HGATP64_MODE_MASK		_ULL(0xF000000000000000)
 #define HGATP64_VMID_SHIFT		44
 #define HGATP64_VMID_MASK		_ULL(0x03FFF00000000000)

... VMID management is going to change as previously discussed, at which point the value to put in hgatp will need (partly) re-calculating at certain points anyway.

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 48

Quoted 11 lines
xen/arch/riscv/p2m.c +5 -0
@@ -41,6 +41,91 @@ void p2m_write_unlock(struct p2m_domain *p2m)
     write_unlock(&p2m->lock);
 }
 
+static void clear_and_clean_page(struct page_info *page)
+{
+    clean_dcache_va_range(page, PAGE_SIZE);
+    clear_domain_page(page_to_mfn(page));
+}

A function of this name can, imo, only clear and then clean. Question is why it's the other way around, and what the underlying requirement is for the cleaning part to be there in the first place. Maybe that's obvious for a RISC-V person, but it's entirely non-obvious to me (Arm being different in this regard because of running with caches disabled at certain points in time).

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 53

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+static struct page_info *p2m_allocate_root(struct domain *d)
+{
+    struct page_info *page;
+    unsigned int order = get_order_from_bytes(KB(16));

While better than a hard-coded order of 2, this still is lacking. Is there a reason there can't be a suitable manifest constant in the header?

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 54

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+    unsigned int nr_pages = _AC(1,U) << order;

Nit (style): Missing blank after comma.

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 59

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+    /* Return back nr_pages necessary for p2m root table. */
+
+    if ( ACCESS_ONCE(d->arch.paging.p2m_total_pages) < nr_pages )
+        panic("Specify more xen,domain-p2m-mem-mb\n");

You shouldn't panic() in anything involved in domain creation. You want to return NULL in this case.

Further, to me the use of "more" looks misleading here. Do you perhaps mean "larger" or "bigger"?

This also looks to be happening without any lock held. If that's intentional, I think the "why" wants clarifying in a code comment.

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+    for ( unsigned int i = 0; i < nr_pages; i++ )
+    {
+        /* Return memory to domheap. */
+        page = page_list_remove_head(&d->arch.paging.p2m_freelist);
+        if( page )
+        {

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 76

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+            free_domheap_page(page);
+        }
+        else
+        {
+            printk(XENLOG_ERR
+                   "Failed to free P2M pages, P2M freelist is empty.\n");
+            return NULL;
+        }
+    }

The reason for doing this may also want to be put in a comment.

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 82

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+    /* Allocate memory for p2m root table. */
+
+    /*
+     * As mentioned in the Priviliged Architecture Spec (version 20240411)
+     * As explained in Section 18.5.1, for the paged virtual-memory schemes

The first sentence didn't finish when the 2nd starts. Is there a piece missing? Do the two sentences want to be joined together?

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 96

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+static unsigned long hgatp_from_page(struct p2m_domain *p2m)

Function name and parameter type/name don't fit together.

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 81

Quoted 2 lines
xen/arch/riscv/p2m.c +2 -0
+{
+    struct page_info *p2m_root_page = p2m->root;

As always: pointer-to-const wherever possible, please. But: Is this local variable really useful to have?

Quoted 8 lines
xen/arch/riscv/p2m.c +8 -0
+    unsigned long ppn;
+    unsigned long hgatp_mode;
+
+    ppn = PFN_DOWN(page_to_maddr(p2m_root_page)) & HGATP_PPN;
+
+#if RV_STAGE1_MODE == SATP_MODE_SV39
+    hgatp_mode = HGATP_MODE_SV39X4;
+#elif RV_STAGE1_MODE == SATP_MODE_SV48

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 116

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+    hgatp_mode = HGATP_MODE_SV48X4;
+#else
+#   error "add HGATP_MODE"
+#endif
+
+    return ppn | MASK_INSR(p2m->vmid, HGATP_VMID_MASK) |
+           MASK_INSR(hgatp_mode, HGATP_MODE_MASK);
+}
+
+static int p2m_alloc_root_table(struct domain *d)

As indicated earlier, in a wider context - this is a good candidate where the caller rather wants to pass struct p2m_domain *. Once you get variations on P2Ms (like x86'es altp2m or nestedp2m, the domain won't be meaningful here to know which P2M to allocate the root for.

Quoted 16 lines
xen/arch/riscv/p2m.c +12 -0
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    p2m->root = p2m_allocate_root(d);
+    if ( !p2m->root )
+        return -ENOMEM;
+
+    p2m->hgatp = hgatp_from_page(p2m);
+
+    return 0;
+}
+
 static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
 
 /*

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 317

Quoted 5 lines
xen/arch/riscv/p2m.c +2 -0
@@ -228,5 +313,14 @@ int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
         }
     }
 
+    /*
+    * First, wait for the p2m pool to be initialized. Then allocate the root

Why "wait"? There's waiting here.

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 322

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+    * table so that the necessary pages can be returned from the p2m pool,
+    * since the root table must be allocated using alloc_domheap_pages(...)
+    * to meet its specific requirements.
+    */
+    if ( !d->arch.p2m.root )

Aren't you open-coding p2m_get_hostp2m() here?

Jan

Quoted 4 lines
xen/arch/riscv/p2m.c +2 -0
+        p2m_alloc_root_table(d);
+
     return 0;
 }
#67 lore raw

Re: [PATCH v2 08/17] xen/riscv: add new p2m types and helper macros for type classification

In reply to Oleksii Kurochko
Quoted 18 lines · Oleksii Kurochko
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 64

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -61,8 +61,28 @@ struct p2m_domain {
   typedef enum {
       p2m_invalid = 0,    /* Nothing mapped here */
       p2m_ram_rw,         /* Normal read/write domain RAM */
+    p2m_ram_ro,         /* Read-only; writes are silently dropped */

As indicated before - this type should be added when the special handling that it requires is also introduced.

Perhaps, I missed that. I will drop this type for now.

Quoted 2 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 65

Quoted 1 line
xen/arch/riscv/include/asm/p2m.h +1 -0
+    p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */

What's the _dev suffix indicating here?

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

It indicates that it is device memory, probably, it isn't so necessary in case of RISC-V as spec doesn't use such terminology. In RISC-V there is only available IO, NC. And we are |using PTE_PBMT_IO for |p2m_mmio_direct_dev.

Maybe it would be better just to rename s/p2m_mmio_direct_dev/p2m_mmio_direct_io.

And what would the _io suffix indicate, beyond what "mmio" already indicates?

Just that PBMT_IO will be used for device memory and not PBMT_NC.

And will there (later) also be a p2m_mmio_direct_nc type? If so, I can see the point of the suffix.

Jan

#61 lore raw

Re: [PATCH v2 08/17] xen/riscv: add new p2m types and helper macros for type classification

In reply to Jan Beulich
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Oleksii Kurochko
Quoted 15 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 64

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -61,8 +61,28 @@ struct p2m_domain {
   typedef enum {
       p2m_invalid = 0,    /* Nothing mapped here */
       p2m_ram_rw,         /* Normal read/write domain RAM */
+    p2m_ram_ro,         /* Read-only; writes are silently dropped */

As indicated before - this type should be added when the special handling that it requires is also introduced.

Perhaps, I missed that. I will drop this type for now.

Quoted 2 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 65

Quoted 1 line
xen/arch/riscv/include/asm/p2m.h +1 -0
+    p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */

What's the _dev suffix indicating here?

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

It indicates that it is device memory, probably, it isn't so necessary in case of RISC-V as spec doesn't use such terminology. In RISC-V there is only available IO, NC. And we are |using PTE_PBMT_IO for |p2m_mmio_direct_dev.

Maybe it would be better just to rename s/p2m_mmio_direct_dev/p2m_mmio_direct_io.

I forgot that p2m_mmio_direct_dev is used by common code for dom0less code (handle_passthrough_prop())

That'll want abstracting out, I think. I don't view it as helpful to clutter RISC-V (and later perhaps also PPC) with Arm-specific terminology.

Would it be better then just rename it to p2m_device? Then it won't clear for Arm which type of MMIO p2m's types is used as Arm has there MMIO types: *_dev, *_nc, *_c.

As an option (which I don't really like) it could be "#define p2m_mmio_direct_dev ARCH_specific_name" in asm/p2m.h to not touch common code.

~ Oleksii

#69 lore raw

Re: [PATCH v2 09/17] xen/riscv: introduce page_set_xenheap_gfn()

In reply to Oleksii Kurochko
Quoted 6 lines · Oleksii Kurochko

Introduce page_set_xenheap_gfn() helper to encode the GFN associated with a Xen heap page directly into the type_info field of struct page_info.

Introduce a GFN field in the type_info of a Xen heap page by reserving 10 bits (sufficient for both Sv32 and Sv39+ modes), and define PGT_gfn_mask and PGT_gfn_width accordingly.

This reads as if you wanted to encode the GFN in 10 bits.

What would also help is if you said why you actually need this. x86, after all, gets away without anything like this. (But I understand you're more Arm-like here.)

Quoted 12 lines
xen/arch/riscv/include/asm/mm.h +5 -3
@@ -229,9 +230,21 @@ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)
 #define PGT_writable_page PG_mask(1, 1)  /* has writable mappings?         */
 #define PGT_type_mask     PG_mask(1, 1)  /* Bits 31 or 63.                 */
 
-/* Count of uses of this frame as its current type. */
-#define PGT_count_width   PG_shift(2)
-#define PGT_count_mask    ((1UL << PGT_count_width) - 1)
+ /* 9-bit count of uses of this frame as its current type. */
+#define PGT_count_mask    PG_mask(0x3FF, 10)
+
+/*
+ * Sv32 has 22-bit GFN. Sv{39, 48, 57} have 44-bit GFN.

xen/arch/riscv/include/asm/mm.h static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr) · line 247

Quoted 10 lines
xen/arch/riscv/include/asm/mm.h +10 -0
+ * Thereby we can use for `type_info` 10 bits for all modes, having the same
+ * amount of bits for `type_info` for all MMU modes let us avoid introducing
+ * an extra #ifdef to that header:
+ *   if we go with maximum possible bits for count on each configuration
+ *   we would need to have a set of PGT_count_* and PGT_gfn_*).
+ */
+#define PGT_gfn_width     PG_shift(10)
+#define PGT_gfn_mask      (BIT(PGT_gfn_width, UL) - 1)
+
+#define PGT_INVALID_XENHEAP_GFN   _gfn(PGT_gfn_mask)

Commentary here would imo be preferable to be much closer to Arm's. I don't see the point of the extra verbosity (part of which may be fine to have in the description, except you already say something along these lines there). While in turn the comment talks of fewer bits than are actually being used in the RV64 case.

Quoted 10 lines
xen/arch/riscv/include/asm/mm.h +6 -0
@@ -283,6 +296,19 @@ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)
 
 #define PFN_ORDER(pg) ((pg)->v.free.order)
 
+static inline void page_set_xenheap_gfn(struct page_info *p, gfn_t gfn)
+{
+    gfn_t gfn_ = gfn_eq(gfn, INVALID_GFN) ? PGT_INVALID_XENHEAP_GFN : gfn;
+    unsigned long x, nx, y = p->u.inuse.type_info;
+
+    ASSERT(is_xen_heap_page(p));

xen/arch/riscv/include/asm/mm.h static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr) · line 314

Quoted 10 lines
xen/arch/riscv/include/asm/mm.h +7 -0
+
+    do {
+        x = y;
+        nx = (x & ~PGT_gfn_mask) | gfn_x(gfn_);
+    } while ( (y = cmpxchg(&p->u.inuse.type_info, x, nx)) != x );
+}
+
 extern unsigned char cpu0_boot_stack[];
 
 void setup_initial_pagetables(void);

What about the "get" counterpart?

Jan

#62 lore raw

Re: [PATCH v2 08/17] xen/riscv: add new p2m types and helper macros for type classification

In reply to Oleksii Kurochko
Quoted 23 lines · Oleksii Kurochko
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Oleksii Kurochko
Quoted 19 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 64

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -61,8 +61,28 @@ struct p2m_domain {
   typedef enum {
       p2m_invalid = 0,    /* Nothing mapped here */
       p2m_ram_rw,         /* Normal read/write domain RAM */
+    p2m_ram_ro,         /* Read-only; writes are silently dropped */

As indicated before - this type should be added when the special handling that it requires is also introduced.

Perhaps, I missed that. I will drop this type for now.

Quoted 2 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 65

Quoted 1 line
xen/arch/riscv/include/asm/p2m.h +1 -0
+    p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */

What's the _dev suffix indicating here?

It indicates that it is device memory, probably, it isn't so necessary in case of RISC-V as spec doesn't use such terminology. In RISC-V there is only available IO, NC. And we are |using PTE_PBMT_IO for |p2m_mmio_direct_dev.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

Maybe it would be better just to rename s/p2m_mmio_direct_dev/p2m_mmio_direct_io.

I forgot that p2m_mmio_direct_dev is used by common code for dom0less code (handle_passthrough_prop())

That'll want abstracting out, I think. I don't view it as helpful to clutter RISC-V (and later perhaps also PPC) with Arm-specific terminology.

Would it be better then just rename it to p2m_device? Then it won't clear for Arm which type of MMIO p2m's types is used as Arm has there MMIO types: *_dev, *_nc, *_c.

I don't understand why Arm matters here. P2M types want naming in a way that makes sense for RISC-V.

Quoted 2 lines

As an option (which I don't really like) it could be "#define p2m_mmio_direct_dev ARCH_specific_name" in asm/p2m.h to not touch common code.

A #define may be needed, but not one to _still_ introduce Arm naming into non-Arm code.

Jan

#73 lore raw

Re: [PATCH v2 10/17] xen/riscv: implement guest_physmap_add_entry() for mapping GFNs to MFNs

In reply to Oleksii Kurochko
Quoted 8 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +2 -0
@@ -324,3 +324,44 @@ int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
 
     return 0;
 }
+
+static int p2m_set_entry(struct p2m_domain *p2m,

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 338

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+                         gfn_t sgfn,
+                         unsigned long nr,
+                         mfn_t smfn,
+                         p2m_type_t t,
+                         p2m_access_t a)
+{
+    return -EOPNOTSUPP;
+}
+
+static int p2m_insert_mapping(struct domain *d, gfn_t start_gfn,

This likely again wants to be struct p2m_domain *.

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+                              unsigned long nr, mfn_t mfn, p2m_type_t t)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    int rc;
+
+    p2m_write_lock(p2m);
+    rc = p2m_set_entry(p2m, start_gfn, nr, mfn, t, p2m->default_access);
+    p2m_write_unlock(p2m);
+
+    return rc;

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 358

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+}
+
+int map_regions_p2mt(struct domain *d,
+                     gfn_t gfn,
+                     unsigned long nr,
+                     mfn_t mfn,
+                     p2m_type_t p2mt)
+{
+    return p2m_insert_mapping(d, gfn, nr, mfn, p2mt);
+}

What is this function doing here? The description says "for generic mapping purposes", which really may mean anything. Plus, if and when you need it, it wants to come with a name that fits with e.g. ...

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 364

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+int guest_physmap_add_entry(struct domain *d,
+                            gfn_t gfn,
+                            mfn_t mfn,
+                            unsigned long page_order,
+                            p2m_type_t t)

... this one, to understand their relationship / difference.

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 365

Quoted 2 lines
xen/arch/riscv/p2m.c +2 -0
+{
+    return p2m_insert_mapping(d, gfn, (1 << page_order), mfn, t);

1UL please, while at the same time the parentheses could be omitted.

Jan

#48 lore raw

Re: [PATCH v2 06/17] xen/riscv: add root page table allocation

In reply to Jan Beulich
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +0 -0
@@ -26,6 +26,12 @@ struct p2m_domain {
      /* Pages used to construct the p2m */
      struct page_list_head pages;
 
Quoted 7 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 33

Quoted 5 lines
xen/arch/riscv/include/asm/p2m.h +5 -0
+    /* The root of the p2m tree. May be concatenated */
+    struct page_info *root;
+
+    /* Address Translation Table for the p2m */
+    paddr_t hgatp;

Does this really need holding in a struct field? Can't is be re-created at any time from "root" above?

Yes, with the current one implementation, I agree it would be enough only root. But as you noticed below...

Quoted 15 lines

And such re-creation is apparently infrequent,
if happening at all after initial allocation. (But of course I don't know what future patches of yours will bring.) This is even more so if ...

Quoted 11 lines
xen/arch/riscv/include/asm/riscv_encoding.h +1 -0
@@ -133,11 +133,13 @@
  #define HGATP_MODE_SV48X4		_UL(9)
 
  #define HGATP32_MODE_SHIFT		31
+#define HGATP32_MODE_MASK		_UL(0x80000000)
  #define HGATP32_VMID_SHIFT		22
  #define HGATP32_VMID_MASK		_UL(0x1FC00000)
  #define HGATP32_PPN			_UL(0x003FFFFF)
 
Quoted 7 lines

xen/arch/riscv/include/asm/riscv_encoding.h line 144

Quoted 4 lines
xen/arch/riscv/include/asm/riscv_encoding.h +1 -0
  #define HGATP64_MODE_SHIFT		60
+#define HGATP64_MODE_MASK		_ULL(0xF000000000000000)
  #define HGATP64_VMID_SHIFT		44
  #define HGATP64_VMID_MASK		_ULL(0x03FFF00000000000)

... VMID management is going to change as previously discussed, at which point the value to put in hgatp will need (partly) re-calculating at certain points anyway.

... after VMID management will changed to per-CPU base then it will be needed to update re-calculate hgatp each time vCPU on pCPU is changed. In this case I prefer to have partially calculated 'hgatp'.

Quoted 6 lines
Quoted 6 lines
xen/arch/riscv/p2m.c +0 -0
@@ -41,6 +41,91 @@ void p2m_write_unlock(struct p2m_domain *p2m)
      write_unlock(&p2m->lock);
  }
 
Quoted 11 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 48

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+static void clear_and_clean_page(struct page_info *page)
+{
+    clean_dcache_va_range(page, PAGE_SIZE);
+    clear_domain_page(page_to_mfn(page));
+}

A function of this name can, imo, only clear and then clean. Question is why it's the other way around, and what the underlying requirement is for the cleaning part to be there in the first place. Maybe that's obvious for a RISC-V person, but it's entirely non-obvious to me (Arm being different in this regard because of running with caches disabled at certain points in time).

You're right, the current name|clear_and_clean_page()| implies that clearing should come before cleaning, which contradicts the current implementation. The intent here is to ensure that the page contents are consistent in RAM (not just in cache) before use by other entities (guests or devices).

The clean must follow the clear — so yes, the order needs to be reversed.

Quoted 7 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 53

Quoted 5 lines
xen/arch/riscv/p2m.c +4 -0
+static struct page_info *p2m_allocate_root(struct domain *d)
+{
+    struct page_info *page;
+    unsigned int order = get_order_from_bytes(KB(16));

While better than a hard-coded order of 2, this still is lacking. Is there a reason there can't be a suitable manifest constant in the header?

No any specific reason, I just decided not to introduce new definition as it is going to be used only inside this function.

I think it will make sense to have in p2m.c:
#define P2M_ROOT_PT_SIZE KB(16)
If it isn't the best one option, then what about to move this defintion to config.h or asm/p2m.h.

Quoted 3 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 54

Quoted 2 lines
xen/arch/riscv/p2m.c +1 -0
+    unsigned int nr_pages = _AC(1,U) << order;

Nit (style): Missing blank after comma.

I've changed that to BIT(order, U)

Quoted 7 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 59

Quoted 5 lines
xen/arch/riscv/p2m.c +4 -0
+    /* Return back nr_pages necessary for p2m root table. */
+
+    if ( ACCESS_ONCE(d->arch.paging.p2m_total_pages) < nr_pages )
+        panic("Specify more xen,domain-p2m-mem-mb\n");

You shouldn't panic() in anything involved in domain creation. You want to return NULL in this case.

It makes sense in this case just to return NULL.

Quoted 6 lines

Further, to me the use of "more" looks misleading here. Do you perhaps mean "larger" or "bigger"?

This also looks to be happening without any lock held. If that's intentional, I think the "why" wants clarifying in a code comment.

Agree, returning back pages necessary for p2m root table should be done under spin_lock(&d->arch.paging.lock).

Quoted 17 lines
Quoted 7 lines
xen/arch/riscv/p2m.c +6 -0
+    for ( unsigned int i = 0; i < nr_pages; i++ )
+    {
+        /* Return memory to domheap. */
+        page = page_list_remove_head(&d->arch.paging.p2m_freelist);
+        if( page )
+        {

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 76

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+            free_domheap_page(page);
+        }
+        else
+        {
+            printk(XENLOG_ERR
+                   "Failed to free P2M pages, P2M freelist is empty.\n");
+            return NULL;
+        }
+    }
Quoted 1 line

The reason for doing this may also want to be put in a comment.

I thought it would be enough the comment above: /* Return back nr_pages necessary for p2m root table. */

Quoted 8 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 82

Quoted 6 lines
xen/arch/riscv/p2m.c +5 -0
+    /* Allocate memory for p2m root table. */
+
+    /*
+     * As mentioned in the Priviliged Architecture Spec (version 20240411)
+     * As explained in Section 18.5.1, for the paged virtual-memory schemes

The first sentence didn't finish when the 2nd starts. Is there a piece missing? Do the two sentences want to be joined together?

Nothing is missed, just bad wording. I will update to:
As mentioned in the Priviliged Architecture Spec (version 20240411) in Section 18.5.1, ...

Quoted 3 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 96

Quoted 2 lines
xen/arch/riscv/p2m.c +1 -0
+static unsigned long hgatp_from_page(struct p2m_domain *p2m)

Function name and parameter type/name don't fit together.

I'll update an argument to struct page_info *root.

Quoted 5 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 81

Quoted 3 lines
xen/arch/riscv/p2m.c +2 -0
+{
+    struct page_info *p2m_root_page = p2m->root;

As always: pointer-to-const wherever possible, please. But: Is this local variable really useful to have?

No, it will be just passed as an argument.

Quoted 15 lines
Quoted 15 lines
xen/arch/riscv/p2m.c +14 -0
+    unsigned long ppn;
+    unsigned long hgatp_mode;
+
+    ppn = PFN_DOWN(page_to_maddr(p2m_root_page)) & HGATP_PPN;
+
+#if RV_STAGE1_MODE == SATP_MODE_SV39
+    hgatp_mode = HGATP_MODE_SV39X4;
+#elif RV_STAGE1_MODE == SATP_MODE_SV48
+    hgatp_mode = HGATP_MODE_SV48X4;
+#else
+#   error "add HGATP_MODE"
+#endif
+
+    return ppn | MASK_INSR(p2m->vmid, HGATP_VMID_MASK) |
Quoted 8 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 116

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+           MASK_INSR(hgatp_mode, HGATP_MODE_MASK);
+}
+
+static int p2m_alloc_root_table(struct domain *d)

As indicated earlier, in a wider context - this is a good candidate where the caller rather wants to pass struct p2m_domain *. Once you get variations on P2Ms (like x86'es altp2m or nestedp2m, the domain won't be meaningful here to know which P2M to allocate the root for.

Good point. I will re-work that.

Quoted 15 lines
Quoted 15 lines
xen/arch/riscv/p2m.c +12 -0
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    p2m->root = p2m_allocate_root(d);
+    if ( !p2m->root )
+        return -ENOMEM;
+
+    p2m->hgatp = hgatp_from_page(p2m);
+
+    return 0;
+}
+
  static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
 
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 317

Quoted 7 lines
xen/arch/riscv/p2m.c +2 -0
  /*
@@ -228,5 +313,14 @@ int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
          }
      }
 
+    /*
+    * First, wait for the p2m pool to be initialized. Then allocate the root

Why "wait"? There's waiting here.

I am not really get your question.

"wait" here is about the initialization of the pool which happens above this comment.

Quoted 7 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 322

Quoted 6 lines
xen/arch/riscv/p2m.c +5 -0
+    * table so that the necessary pages can be returned from the p2m pool,
+    * since the root table must be allocated using alloc_domheap_pages(...)
+    * to meet its specific requirements.
+    */
+    if ( !d->arch.p2m.root )

Aren't you open-coding p2m_get_hostp2m() here?

Yes, p2m_get_hostp2m() should be used here.

~ Oleksii

#49 lore raw

Re: [PATCH v2 06/17] xen/riscv: add root page table allocation

Quoted 42 lines · Oleksii Kurochko
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +0 -0
@@ -26,6 +26,12 @@ struct p2m_domain {
      /* Pages used to construct the p2m */
      struct page_list_head pages;
 
Quoted 7 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 33

Quoted 5 lines
xen/arch/riscv/include/asm/p2m.h +5 -0
+    /* The root of the p2m tree. May be concatenated */
+    struct page_info *root;
+
+    /* Address Translation Table for the p2m */
+    paddr_t hgatp;

Does this really need holding in a struct field? Can't is be re-created at any time from "root" above?

Yes, with the current one implementation, I agree it would be enough only root. But as you noticed below...

Quoted 15 lines

And such re-creation is apparently infrequent,
if happening at all after initial allocation. (But of course I don't know what future patches of yours will bring.) This is even more so if ...

Quoted 11 lines
xen/arch/riscv/include/asm/riscv_encoding.h +1 -0
@@ -133,11 +133,13 @@
  #define HGATP_MODE_SV48X4		_UL(9)
 
  #define HGATP32_MODE_SHIFT		31
+#define HGATP32_MODE_MASK		_UL(0x80000000)
  #define HGATP32_VMID_SHIFT		22
  #define HGATP32_VMID_MASK		_UL(0x1FC00000)
  #define HGATP32_PPN			_UL(0x003FFFFF)
 
Quoted 7 lines

xen/arch/riscv/include/asm/riscv_encoding.h line 144

Quoted 4 lines
xen/arch/riscv/include/asm/riscv_encoding.h +1 -0
  #define HGATP64_MODE_SHIFT		60
+#define HGATP64_MODE_MASK		_ULL(0xF000000000000000)
  #define HGATP64_VMID_SHIFT		44
  #define HGATP64_VMID_MASK		_ULL(0x03FFF00000000000)

... VMID management is going to change as previously discussed, at which point the value to put in hgatp will need (partly) re-calculating at certain points anyway.

Quoted 3 lines

... after VMID management will changed to per-CPU base then it will be needed to update re-calculate hgatp each time vCPU on pCPU is changed. In this case I prefer to have partially calculated 'hgatp'.

But why, when you need to do some recalculation anyway?

Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/riscv/p2m.c +0 -0
@@ -41,6 +41,91 @@ void p2m_write_unlock(struct p2m_domain *p2m)
      write_unlock(&p2m->lock);
  }
 
Quoted 11 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 48

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+static void clear_and_clean_page(struct page_info *page)
+{
+    clean_dcache_va_range(page, PAGE_SIZE);
+    clear_domain_page(page_to_mfn(page));
+}

A function of this name can, imo, only clear and then clean. Question is why it's the other way around, and what the underlying requirement is for the cleaning part to be there in the first place. Maybe that's obvious for a RISC-V person, but it's entirely non-obvious to me (Arm being different in this regard because of running with caches disabled at certain points in time).

Quoted 6 lines

You're right, the current name|clear_and_clean_page()| implies that clearing should come before cleaning, which contradicts the current implementation. The intent here is to ensure that the page contents are consistent in RAM (not just in cache) before use by other entities (guests or devices).

The clean must follow the clear — so yes, the order needs to be reversed.

What you don't address though - why's the cleaning needed in the first place?

Quoted 7 lines
Quoted 6 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 53

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+static struct page_info *p2m_allocate_root(struct domain *d)
+{
+    struct page_info *page;
+    unsigned int order = get_order_from_bytes(KB(16));

While better than a hard-coded order of 2, this still is lacking. Is there a reason there can't be a suitable manifest constant in the header?

Quoted 7 lines

No any specific reason, I just decided not to introduce new definition as it is going to be used only inside this function.

I think it will make sense to have in p2m.c:
#define P2M_ROOT_PT_SIZE KB(16)
If it isn't the best one option, then what about to move this defintion to config.h or asm/p2m.h.

It's defined by the hardware, so neither of the two headers looks to be a good fit. Nor is the P2M_ prefix really in line with this being hardware- defined. page.h has various paging-related hw definitions, and riscv_encoding.h may also be a suitable place. There may be other candidates that I'm presently overlooking.

Quoted 16 lines
Quoted 2 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 54

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+    unsigned int nr_pages = _AC(1,U) << order;

Nit (style): Missing blank after comma.

I've changed that to BIT(order, U)

Quoted 7 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 59

Quoted 5 lines
xen/arch/riscv/p2m.c +4 -0
+    /* Return back nr_pages necessary for p2m root table. */
+
+    if ( ACCESS_ONCE(d->arch.paging.p2m_total_pages) < nr_pages )
+        panic("Specify more xen,domain-p2m-mem-mb\n");

You shouldn't panic() in anything involved in domain creation. You want to return NULL in this case.

It makes sense in this case just to return NULL.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

Further, to me the use of "more" looks misleading here. Do you perhaps mean "larger" or "bigger"?

This also looks to be happening without any lock held. If that's intentional, I think the "why" wants clarifying in a code comment.

Agree, returning back pages necessary for p2m root table should be done under spin_lock(&d->arch.paging.lock).

Which should be acquired at the paging_*() layer then, not at the p2m_*() layer. (As long as you mean to have that separation, that is. See the earlier discussion on that matter.)

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
xen/arch/riscv/p2m.c +16 -0
+    for ( unsigned int i = 0; i < nr_pages; i++ )
+    {
+        /* Return memory to domheap. */
+        page = page_list_remove_head(&d->arch.paging.p2m_freelist);
+        if( page )
+        {
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+            free_domheap_page(page);
+        }
+        else
+        {
+            printk(XENLOG_ERR
+                   "Failed to free P2M pages, P2M freelist is empty.\n");
+            return NULL;
+        }
+    }
Quoted 3 lines
Quoted 1 line

The reason for doing this may also want to be put in a comment.

I thought it would be enough the comment above: /* Return back nr_pages necessary for p2m root table. */

That describes what the code does, but not why.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
xen/arch/riscv/p2m.c +12 -0
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    p2m->root = p2m_allocate_root(d);
+    if ( !p2m->root )
+        return -ENOMEM;
+
+    p2m->hgatp = hgatp_from_page(p2m);
+
+    return 0;
+}
+
  static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
 
  /*
@@ -228,5 +313,14 @@ int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
          }
      }
 
Quoted 7 lines
Quoted 3 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 317

Quoted 2 lines
xen/arch/riscv/p2m.c +2 -0
+    /*
+    * First, wait for the p2m pool to be initialized. Then allocate the root

Why "wait"? There's waiting here.

I am not really get your question.

"wait" here is about the initialization of the pool which happens above this comment.

But there's no "waiting" involved. What you talk about is one thing needing to happen after the other.

Jan

#50 lore raw

Re: [PATCH v2 06/17] xen/riscv: add root page table allocation

Quoted 41 lines · Jan Beulich
Quoted 40 lines · Oleksii Kurochko
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +0 -0
@@ -26,6 +26,12 @@ struct p2m_domain {
       /* Pages used to construct the p2m */
       struct page_list_head pages;
 
Quoted 7 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 33

Quoted 5 lines
xen/arch/riscv/include/asm/p2m.h +5 -0
+    /* The root of the p2m tree. May be concatenated */
+    struct page_info *root;
+
+    /* Address Translation Table for the p2m */
+    paddr_t hgatp;

Does this really need holding in a struct field? Can't is be re-created at any time from "root" above?

Yes, with the current one implementation, I agree it would be enough only root. But as you noticed below...

Quoted 22 lines

And such re-creation is apparently infrequent,
if happening at all after initial allocation. (But of course I don't know what future patches of yours will bring.) This is even more so if ...

Quoted 5 lines
xen/arch/riscv/include/asm/riscv_encoding.h +0 -0
@@ -133,11 +133,13 @@
   #define HGATP_MODE_SV48X4		_UL(9)
 

xen/arch/riscv/include/asm/riscv_encoding.h line 144

Quoted 10 lines
xen/arch/riscv/include/asm/riscv_encoding.h +2 -0
   #define HGATP32_MODE_SHIFT		31
+#define HGATP32_MODE_MASK		_UL(0x80000000)
   #define HGATP32_VMID_SHIFT		22
   #define HGATP32_VMID_MASK		_UL(0x1FC00000)
   #define HGATP32_PPN			_UL(0x003FFFFF)
 
   #define HGATP64_MODE_SHIFT		60
+#define HGATP64_MODE_MASK		_ULL(0xF000000000000000)
   #define HGATP64_VMID_SHIFT		44
   #define HGATP64_VMID_MASK		_ULL(0x03FFF00000000000)

... VMID management is going to change as previously discussed, at which point the value to put in hgatp will need (partly) re-calculating at certain points anyway.

Quoted 4 lines
Quoted 3 lines

... after VMID management will changed to per-CPU base then it will be needed to update re-calculate hgatp each time vCPU on pCPU is changed. In this case I prefer to have partially calculated 'hgatp'.

But why, when you need to do some recalculation anyway?

Less operations will be needed to do.
If we have partially prepared 'hgatp' then we have to only update VMID bits instead of getting ppn for page, then calculate hgatp_mode each time. But if you think it isn't really needed I can add vmid argument for hgatp_from_page() and just call this function when an update of hgatp is needed.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 48

Quoted 12 lines
xen/arch/riscv/p2m.c +5 -0
@@ -41,6 +41,91 @@ void p2m_write_unlock(struct p2m_domain *p2m)
       write_unlock(&p2m->lock);
   }
 
+static void clear_and_clean_page(struct page_info *page)
+{
+    clean_dcache_va_range(page, PAGE_SIZE);
+    clear_domain_page(page_to_mfn(page));
+}

A function of this name can, imo, only clear and then clean. Question is why it's the other way around, and what the underlying requirement is for the cleaning part to be there in the first place. Maybe that's obvious for a RISC-V person, but it's entirely non-obvious to me (Arm being different in this regard because of running with caches disabled at certain points in time).

Quoted 7 lines
Quoted 6 lines

You're right, the current name|clear_and_clean_page()| implies that clearing should come before cleaning, which contradicts the current implementation. The intent here is to ensure that the page contents are consistent in RAM (not just in cache) before use by other entities (guests or devices).

The clean must follow the clear — so yes, the order needs to be reversed.

What you don't address though - why's the cleaning needed in the first place?

If we clean the data cache first, we flush the d-cache and then use the page to perform the clear operation. As a result, the "cleared" value will be written into the d-cache. To avoid polluting the d-cache with the "cleared" value, the correct sequence is to clear the page first, then clean the data cache.

Quoted 15 lines
Quoted 15 lines
Quoted 2 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 54

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+    unsigned int nr_pages = _AC(1,U) << order;

Nit (style): Missing blank after comma.

I've changed that to BIT(order, U)

Quoted 6 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 59

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+    /* Return back nr_pages necessary for p2m root table. */
+
+    if ( ACCESS_ONCE(d->arch.paging.p2m_total_pages) < nr_pages )
+        panic("Specify more xen,domain-p2m-mem-mb\n");

You shouldn't panic() in anything involved in domain creation. You want to return NULL in this case.

It makes sense in this case just to return NULL.

Quoted 3 lines

Further, to me the use of "more" looks misleading here. Do you perhaps mean "larger" or "bigger"?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

This also looks to be happening without any lock held. If that's intentional, I think the "why" wants clarifying in a code comment.

Agree, returning back pages necessary for p2m root table should be done under spin_lock(&d->arch.paging.lock).

Which should be acquired at the paging_*() layer then, not at the p2m_*() layer. (As long as you mean to have that separation, that is. See the earlier discussion on that matter.)

Then partly p2m_set_allocation() should be moved to paging_*() too.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
xen/arch/riscv/p2m.c +16 -0
+    for ( unsigned int i = 0; i < nr_pages; i++ )
+    {
+        /* Return memory to domheap. */
+        page = page_list_remove_head(&d->arch.paging.p2m_freelist);
+        if( page )
+        {
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+            free_domheap_page(page);
+        }
+        else
+        {
+            printk(XENLOG_ERR
+                   "Failed to free P2M pages, P2M freelist is empty.\n");
+            return NULL;
+        }
+    }
Quoted 3 lines
Quoted 2 lines
Quoted 1 line

The reason for doing this may also want to be put in a comment.

I thought it would be enough the comment above: /* Return back nr_pages necessary for p2m root table. */

That describes what the code does, but not why.

I will add to the comment: "... to get the memory accounting right".

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
xen/arch/riscv/p2m.c +12 -0
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    p2m->root = p2m_allocate_root(d);
+    if ( !p2m->root )
+        return -ENOMEM;
+
+    p2m->hgatp = hgatp_from_page(p2m);
+
+    return 0;
+}
+
   static spinlock_t vmid_alloc_lock = SPIN_LOCK_UNLOCKED;
 
   /*
@@ -228,5 +313,14 @@ int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
           }
       }
 
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 317

Quoted 2 lines
xen/arch/riscv/p2m.c +2 -0
+    /*
+    * First, wait for the p2m pool to be initialized. Then allocate the root

Why "wait"? There's waiting here.

I am not really get your question.

"wait" here is about the initialization of the pool which happens above this comment.

But there's no "waiting" involved. What you talk about is one thing needing to happen after the other.

Okay, then I will just reword comment.

Thanks.

~ Oleksii

#51 lore raw

Re: [PATCH v2 06/17] xen/riscv: add root page table allocation

Quoted 42 lines · Oleksii Kurochko
Quoted 41 lines · Jan Beulich
Quoted 40 lines · Oleksii Kurochko
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +0 -0
@@ -26,6 +26,12 @@ struct p2m_domain {
       /* Pages used to construct the p2m */
       struct page_list_head pages;
 
Quoted 7 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 33

Quoted 5 lines
xen/arch/riscv/include/asm/p2m.h +5 -0
+    /* The root of the p2m tree. May be concatenated */
+    struct page_info *root;
+
+    /* Address Translation Table for the p2m */
+    paddr_t hgatp;

Does this really need holding in a struct field? Can't is be re-created at any time from "root" above?

Yes, with the current one implementation, I agree it would be enough only root. But as you noticed below...

Quoted 22 lines

And such re-creation is apparently infrequent,
if happening at all after initial allocation. (But of course I don't know what future patches of yours will bring.) This is even more so if ...

Quoted 5 lines
xen/arch/riscv/include/asm/riscv_encoding.h +0 -0
@@ -133,11 +133,13 @@
   #define HGATP_MODE_SV48X4		_UL(9)
 

xen/arch/riscv/include/asm/riscv_encoding.h line 144

Quoted 10 lines
xen/arch/riscv/include/asm/riscv_encoding.h +2 -0
   #define HGATP32_MODE_SHIFT		31
+#define HGATP32_MODE_MASK		_UL(0x80000000)
   #define HGATP32_VMID_SHIFT		22
   #define HGATP32_VMID_MASK		_UL(0x1FC00000)
   #define HGATP32_PPN			_UL(0x003FFFFF)
 
   #define HGATP64_MODE_SHIFT		60
+#define HGATP64_MODE_MASK		_ULL(0xF000000000000000)
   #define HGATP64_VMID_SHIFT		44
   #define HGATP64_VMID_MASK		_ULL(0x03FFF00000000000)

... VMID management is going to change as previously discussed, at which point the value to put in hgatp will need (partly) re-calculating at certain points anyway.

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

... after VMID management will changed to per-CPU base then it will be needed to update re-calculate hgatp each time vCPU on pCPU is changed. In this case I prefer to have partially calculated 'hgatp'.

But why, when you need to do some recalculation anyway?

Less operations will be needed to do.

Right; I wonder how big the savings would be.

Quoted 4 lines

If we have partially prepared 'hgatp' then we have to only update VMID bits instead of getting ppn for page, then calculate hgatp_mode each time. But if you think it isn't really needed I can add vmid argument for hgatp_from_page() and just call this function when an update of hgatp is needed.

I think it'll need to be struct p2m_domain * that you (also?) pass in. In the longer run I think you will want to support all three permitted modes, with smaller guests using fewer page table levels.

As to "also" - maybe it's better to change the name of the function, and pass in just (const if possible) struct p2m_domain *.

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/riscv/p2m.c +0 -0
@@ -41,6 +41,91 @@ void p2m_write_unlock(struct p2m_domain *p2m)
       write_unlock(&p2m->lock);
   }
 
Quoted 11 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 48

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+static void clear_and_clean_page(struct page_info *page)
+{
+    clean_dcache_va_range(page, PAGE_SIZE);
+    clear_domain_page(page_to_mfn(page));
+}

A function of this name can, imo, only clear and then clean. Question is why it's the other way around, and what the underlying requirement is for the cleaning part to be there in the first place. Maybe that's obvious for a RISC-V person, but it's entirely non-obvious to me (Arm being different in this regard because of running with caches disabled at certain points in time).

You're right, the current name|clear_and_clean_page()| implies that clearing should come before cleaning, which contradicts the current implementation. The intent here is to ensure that the page contents are consistent in RAM (not just in cache) before use by other entities (guests or devices).

Quoted 7 lines
Quoted 2 lines
Quoted 1 line

The clean must follow the clear — so yes, the order needs to be reversed.

What you don't address though - why's the cleaning needed in the first place?

If we clean the data cache first, we flush the d-cache and then use the page to perform the clear operation. As a result, the "cleared" value will be written into the d-cache. To avoid polluting the d-cache with the "cleared" value, the correct sequence is to clear the page first, then clean the data cache.

If you want to avoid cache pollution, I think you'd need to use a form of stores which simply bypass the cache. Yet then - why would this matter here, but not elsewhere? Wouldn't you better leave such to the hardware, unless you can prove a (meaningful) performance gain?

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 2 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 54

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+    unsigned int nr_pages = _AC(1,U) << order;

Nit (style): Missing blank after comma.

I've changed that to BIT(order, U)

Quoted 6 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 59

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+    /* Return back nr_pages necessary for p2m root table. */
+
+    if ( ACCESS_ONCE(d->arch.paging.p2m_total_pages) < nr_pages )
+        panic("Specify more xen,domain-p2m-mem-mb\n");

You shouldn't panic() in anything involved in domain creation. You want to return NULL in this case.

It makes sense in this case just to return NULL.

Quoted 5 lines

Further, to me the use of "more" looks misleading here. Do you perhaps mean "larger" or "bigger"?

This also looks to be happening without any lock held. If that's intentional, I think the "why" wants clarifying in a code comment.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Agree, returning back pages necessary for p2m root table should be done under spin_lock(&d->arch.paging.lock).

Which should be acquired at the paging_*() layer then, not at the p2m_*() layer. (As long as you mean to have that separation, that is. See the earlier discussion on that matter.)

Then partly p2m_set_allocation() should be moved to paging_*() too.

Not exactly sure what you mean. On x86 at least the paging layer part of the function is pretty slim.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
xen/arch/riscv/p2m.c +16 -0
+    for ( unsigned int i = 0; i < nr_pages; i++ )
+    {
+        /* Return memory to domheap. */
+        page = page_list_remove_head(&d->arch.paging.p2m_freelist);
+        if( page )
+        {
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+            free_domheap_page(page);
+        }
+        else
+        {
+            printk(XENLOG_ERR
+                   "Failed to free P2M pages, P2M freelist is empty.\n");
+            return NULL;
+        }
+    }
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines
Quoted 1 line

The reason for doing this may also want to be put in a comment.

I thought it would be enough the comment above: /* Return back nr_pages necessary for p2m root table. */

That describes what the code does, but not why.

I will add to the comment: "... to get the memory accounting right".

I'm sorry to be picky, but what is "right"? You want assure the root table memory is also accounted against the P2M pool of the domain. Can't you say exactly that?

Jan

#31 lore raw

Re: [PATCH v2 04/17] xen/riscv: construct the P2M pages pool for guests

In reply to Oleksii Kurochko
Quoted 49 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +45 -0
@@ -113,3 +117,58 @@ int p2m_init(struct domain *d)
 
     return 0;
 }
+
+/*
+ * Set the pool of pages to the required number of pages.
+ * Returns 0 for success, non-zero for failure.
+ * Call with d->arch.paging.lock held.
+ */
+int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
+{
+    struct page_info *pg;
+
+    ASSERT(spin_is_locked(&d->arch.paging.lock));
+
+    for ( ; ; )
+    {
+        if ( d->arch.paging.p2m_total_pages < pages )
+        {
+            /* Need to allocate more memory from domheap */
+            pg = alloc_domheap_page(d, MEMF_no_owner);
+            if ( pg == NULL )
+            {
+                printk(XENLOG_ERR "Failed to allocate P2M pages.\n");
+                return -ENOMEM;
+            }
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)++;
+            page_list_add_tail(pg, &d->arch.paging.p2m_freelist);
+        }
+        else if ( d->arch.paging.p2m_total_pages > pages )
+        {
+            /* Need to return memory to domheap */
+            pg = page_list_remove_head(&d->arch.paging.p2m_freelist);
+            if( pg )
+            {
+                ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+                free_domheap_page(pg);
+            }
+            else
+            {
+                printk(XENLOG_ERR
+                       "Failed to free P2M pages, P2M freelist is empty.\n");
+                return -ENOMEM;
+            }
+        }
+        else
+            break;
+

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 174

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        /* Check to see if we need to yield and try again */
+        if ( preempted && general_preempt_check() )
+        {
+            *preempted = true;
+            return -ERESTART;
+        }
+    }
+
+    return 0;
+}

Btw, with the order-2 requirement for the root page table, you may want to consider an alternative approach: Here you could allocate some order-2 pages (possibly up to as many as a domain might need, which right now would be exactly one), put them on a separate list, and consume the root table(s) from there. If you run out of pages on the order-0 list, you could shatter a page from the order-2 one (as long as that's still non- empty). The difficulty would be with freeing, where a previously shattered order-2 page would be nice to re-combine once all of its constituents are free again. The main benefit would be avoiding the back and forth in patch 6.

Jan

#81 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

In reply to Oleksii Kurochko
Quoted 15 lines · Oleksii Kurochko

This patch introduces p2m_set_entry() and its core helper __p2m_set_entry() for RISC-V, based loosely on the Arm implementation, with several RISC-V-specific modifications.

Key differences include:
- TLB Flushing: RISC-V allows caching of invalid PTEs and does not require break-before-make (BBM). As a result, the flushing logic is simplified. TLB invalidation can be deferred until p2m_write_unlock() is called. Consequently, the p2m->need_flush flag is always considered true and is removed.
- Page Table Traversal: The order of walking the page tables differs from Arm, and this implementation reflects that reversed traversal.
- Macro Adjustments: The macros P2M_ROOT_LEVEL, P2M_ROOT_ORDER, and P2M_ROOT_PAGES are updated to align with the new RISC-V implementation.

Quoted 8 lines

The main functionality is in __p2m_set_entry(), which handles mappings aligned to page table block entries (e.g., 1GB, 2MB, or 4KB with 4KB granularity).

p2m_set_entry() breaks a region down into block-aligned mappings and calls __p2m_set_entry() accordingly.

Stub implementations (to be completed later) include:
- p2m_free_entry()

What would a function of this name do? You can clear entries, but you can't free them, can you?

xen/arch/riscv/include/asm/p2m.h line 16

Quoted 11 lines
xen/arch/riscv/include/asm/p2m.h +3 -0
@@ -9,8 +9,13 @@
 #include <xen/rwlock.h>
 #include <xen/types.h>
 
+#include <asm/page.h>
 #include <asm/page-bits.h>
 
+#define P2M_ROOT_LEVEL  HYP_PT_ROOT_LEVEL
+#define P2M_ROOT_ORDER  XEN_PT_LEVEL_ORDER(P2M_ROOT_LEVEL)

This is confusing, as in patch 6 we see that p2m root table order is 2. Something needs doing about the naming, so the two sets of things can't be confused.

Quoted 5 lines
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -49,6 +54,17 @@ struct p2m_domain {
 
     /* Current VMID in use */
     uint16_t vmid;
+

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 67

Quoted 10 lines
xen/arch/riscv/include/asm/p2m.h +10 -0
+    /* Highest guest frame that's ever been mapped in the p2m */
+    gfn_t max_mapped_gfn;
+
+    /*
+     * Lowest mapped gfn in the p2m. When releasing mapped gfn's in a
+     * preemptible manner this is update to track recall where to
+     * resume the search. Apart from during teardown this can only
+     * decrease.
+     */
+    gfn_t lowest_mapped_gfn;

When you copied the comment, you surely read it. Yet you copied pretty obvious flaws as-is. That is s/update/updated/, and something wants doing about "track recall", which makes no sense to me.

Quoted 12 lines
xen/arch/riscv/p2m.c +2 -0
@@ -231,6 +231,8 @@ int p2m_init(struct domain *d)
     INIT_PAGE_LIST_HEAD(&p2m->pages);
 
     p2m->vmid = INVALID_VMID;
+    p2m->max_mapped_gfn = _gfn(0);
+    p2m->lowest_mapped_gfn = _gfn(ULONG_MAX);
 
     p2m->default_access = p2m_access_rwx;
 

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 335

Quoted 9 lines
xen/arch/riscv/p2m.c +6 -0
@@ -325,6 +327,214 @@ int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
     return 0;
 }
 
+/*
+ * Find and map the root page table. The caller is responsible for
+ * unmapping the table.
+ *
+ * The function will return NULL if the offset of the root table is
+ * invalid.

Don't you mean "offset into ..."?

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 348

Quoted 13 lines
xen/arch/riscv/p2m.c +13 -0
+ */
+static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn)
+{
+    unsigned long root_table_indx;
+
+    root_table_indx = gfn_x(gfn) >> XEN_PT_LEVEL_ORDER(P2M_ROOT_LEVEL);
+    if ( root_table_indx >= P2M_ROOT_PAGES )
+        return NULL;
+
+    return __map_domain_page(p2m->root + root_table_indx);
+}
+
+static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)

The rule of thumb is to have inline functions only in header files, leaving decisions to the compiler elsewhere.

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 353

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+{
+    panic("%s: isn't implemented for now\n", __func__);
+
+    return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+static inline void p2m_write_pte(pte_t *p, pte_t pte, bool clean_pte)
+{
+    write_pte(p, pte);
+    if ( clean_pte )

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 368

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        clean_dcache_va_range(p, sizeof(*p));
+}
+
+static inline void p2m_remove_pte(pte_t *p, bool clean_pte)
+{
+    pte_t pte;
+
+    memset(&pte, 0x00, sizeof(pte));
+    p2m_write_pte(p, pte, clean_pte);
+}

May I suggest "clear" instead of "remove" and plain 0 instead of 0x00 (or simply give the variable a trivial initializer)?

As to the earlier function that I commented on: Seeing the names here, wouldn't p2m_pte_is_valid() be a more consistent name there?

214 lines not shown · raw message

Part of this message is not shown here. View the raw message.

#52 lore raw

Re: [PATCH v2 06/17] xen/riscv: add root page table allocation

In reply to Jan Beulich
Quoted 43 lines · Jan Beulich
Quoted 42 lines · Oleksii Kurochko
Quoted 41 lines · Jan Beulich
Quoted 40 lines · Oleksii Kurochko
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +0 -0
@@ -26,6 +26,12 @@ struct p2m_domain {
        /* Pages used to construct the p2m */
        struct page_list_head pages;
 
Quoted 7 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 33

Quoted 5 lines
xen/arch/riscv/include/asm/p2m.h +5 -0
+    /* The root of the p2m tree. May be concatenated */
+    struct page_info *root;
+
+    /* Address Translation Table for the p2m */
+    paddr_t hgatp;

Does this really need holding in a struct field? Can't is be re-created at any time from "root" above?

Yes, with the current one implementation, I agree it would be enough only root. But as you noticed below...

Quoted 22 lines

And such re-creation is apparently infrequent,
if happening at all after initial allocation. (But of course I don't know what future patches of yours will bring.) This is even more so if ...

Quoted 5 lines
xen/arch/riscv/include/asm/riscv_encoding.h +0 -0
@@ -133,11 +133,13 @@
    #define HGATP_MODE_SV48X4		_UL(9)
 

xen/arch/riscv/include/asm/riscv_encoding.h line 144

Quoted 10 lines
xen/arch/riscv/include/asm/riscv_encoding.h +2 -0
    #define HGATP32_MODE_SHIFT		31
+#define HGATP32_MODE_MASK		_UL(0x80000000)
    #define HGATP32_VMID_SHIFT		22
    #define HGATP32_VMID_MASK		_UL(0x1FC00000)
    #define HGATP32_PPN			_UL(0x003FFFFF)
 
    #define HGATP64_MODE_SHIFT		60
+#define HGATP64_MODE_MASK		_ULL(0xF000000000000000)
    #define HGATP64_VMID_SHIFT		44
    #define HGATP64_VMID_MASK		_ULL(0x03FFF00000000000)

... VMID management is going to change as previously discussed, at which point the value to put in hgatp will need (partly) re-calculating at certain points anyway.

Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

... after VMID management will changed to per-CPU base then it will be needed to update re-calculate hgatp each time vCPU on pCPU is changed. In this case I prefer to have partially calculated 'hgatp'.

But why, when you need to do some recalculation anyway?

Less operations will be needed to do.

Right; I wonder how big the savings would be.

Probably not big.

Quoted 8 lines
Quoted 5 lines

If we have partially prepared 'hgatp' then we have to only update VMID bits instead of getting ppn for page, then calculate hgatp_mode each time. But if you think it isn't really needed I can add vmid argument for hgatp_from_page() and just call this function when an update of hgatp is needed.

I think it'll need to be struct p2m_domain * that you (also?) pass in. In the longer run I think you will want to support all three permitted modes, with smaller guests using fewer page table levels.

Yes, but these modes will be const for a domain, I guess. I mean that once a mode has been set, it isn't going to be changed. But VMID is going to be changed each time vCPU gives control to another vCPU.
Anyway, I am okay to make update of hgatp more generic and just update it fully each time it is needed.

Quoted 28 lines

As to "also" - maybe it's better to change the name of the function, and pass in just (const if possible) struct p2m_domain *.

Quoted 24 lines
Quoted 24 lines
Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/riscv/p2m.c +0 -0
@@ -41,6 +41,91 @@ void p2m_write_unlock(struct p2m_domain *p2m)
        write_unlock(&p2m->lock);
    }
 
Quoted 11 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 48

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+static void clear_and_clean_page(struct page_info *page)
+{
+    clean_dcache_va_range(page, PAGE_SIZE);
+    clear_domain_page(page_to_mfn(page));
+}

A function of this name can, imo, only clear and then clean. Question is why it's the other way around, and what the underlying requirement is for the cleaning part to be there in the first place. Maybe that's obvious for a RISC-V person, but it's entirely non-obvious to me (Arm being different in this regard because of running with caches disabled at certain points in time).

Quoted 6 lines

You're right, the current name|clear_and_clean_page()| implies that clearing should come before cleaning, which contradicts the current implementation. The intent here is to ensure that the page contents are consistent in RAM (not just in cache) before use by other entities (guests or devices).

The clean must follow the clear — so yes, the order needs to be reversed.

What you don't address though - why's the cleaning needed in the first place?

Quoted 8 lines
Quoted 4 lines

If we clean the data cache first, we flush the d-cache and then use the page to perform the clear operation. As a result, the "cleared" value will be written into the d-cache. To avoid polluting the d-cache with the "cleared" value, the correct sequence is to clear the page first, then clean the data cache.

If you want to avoid cache pollution, I think you'd need to use a form of stores which simply bypass the cache. Yet then - why would this matter here, but not elsewhere? Wouldn't you better leave such to the hardware, unless you can prove a (meaningful) performance gain?

I thought about a case when IOMMU doesn't support coherent walks and p2m tables are shared between CPU and IOMMU. Then my understanding is:
- clear_page(p) just zero-ing a page in a CPU's cache.
- But IOMMU can see old data or uninitialized, if they still in cache.
- So, it is need to do clean_cache() to writeback data from cache to RAM, before a page will be used as a part of page table for IOMMU.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 3 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 54

Quoted 2 lines
xen/arch/riscv/p2m.c +1 -0
+    unsigned int nr_pages = _AC(1,U) << order;

Nit (style): Missing blank after comma.

I've changed that to BIT(order, U)

Quoted 6 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 59

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+    /* Return back nr_pages necessary for p2m root table. */
+
+    if ( ACCESS_ONCE(d->arch.paging.p2m_total_pages) < nr_pages )
+        panic("Specify more xen,domain-p2m-mem-mb\n");

You shouldn't panic() in anything involved in domain creation. You want to return NULL in this case.

It makes sense in this case just to return NULL.

Quoted 5 lines

Further, to me the use of "more" looks misleading here. Do you perhaps mean "larger" or "bigger"?

This also looks to be happening without any lock held. If that's intentional, I think the "why" wants clarifying in a code comment.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Agree, returning back pages necessary for p2m root table should be done under spin_lock(&d->arch.paging.lock).

Which should be acquired at the paging_*() layer then, not at the p2m_*() layer. (As long as you mean to have that separation, that is. See the earlier discussion on that matter.)

Then partly p2m_set_allocation() should be moved to paging_*() too.

Not exactly sure what you mean. On x86 at least the paging layer part of the function is pretty slim.

I meant that part of code which is spin_lock(&d->arch.paging.lock); ... spin_unlock(&d->arch.paging.lock) in function p2m_set_allocation() should be moved somewhere to paging_*() layer for the same logic as you suggested to move part of p2m_allocate_root()'s code which is guarded by d->arch.paging.lock to paging_*() layer.

Or I just misunderstood your initial idea with this paging_*() layer and its necessity.

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
xen/arch/riscv/p2m.c +16 -0
+    for ( unsigned int i = 0; i < nr_pages; i++ )
+    {
+        /* Return memory to domheap. */
+        page = page_list_remove_head(&d->arch.paging.p2m_freelist);
+        if( page )
+        {
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+            free_domheap_page(page);
+        }
+        else
+        {
+            printk(XENLOG_ERR
+                   "Failed to free P2M pages, P2M freelist is empty.\n");
+            return NULL;
+        }
+    }
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
Quoted 2 lines
Quoted 1 line

The reason for doing this may also want to be put in a comment.

I thought it would be enough the comment above: /* Return back nr_pages necessary for p2m root table. */

That describes what the code does, but not why.

I will add to the comment: "... to get the memory accounting right".

I'm sorry to be picky, but what is "right"? You want assure the root table memory is also accounted against the P2M pool of the domain. Can't you say exactly that?

It can be said in this way.

Thanks.

~ Oleksii

#98 lore raw

Re: [PATCH v2 12/17] xen/riscv: Implement p2m_free_entry() and related helpers

In reply to Oleksii Kurochko
Quoted 11 lines · Oleksii Kurochko

This patch introduces a working implementation of p2m_free_entry() for RISC-V based on ARM's implementation of p2m_free_entry(), enabling proper cleanup of page table entries in the P2M (physical-to-machine) mapping.

Only few things are changed:
- Use p2m_force_flush_sync() instead of p2m_tlb_flush_sync() as latter isn't implemented on RISC-V.
- Introduce and use p2m_type_radix_get() to get a type of p2m entry as RISC-V's PTE doesn't have enough space to store all necessary types so a type is stored in a radix tree.

Quoted 12 lines

Key additions include:
- p2m_free_entry(): Recursively frees page table entries at all levels. It handles both regular and superpage mappings and ensures that TLB entries are flushed before freeing intermediate tables.
- p2m_put_page() and helpers:
- p2m_put_4k_page(): Clears GFN from xenheap pages if applicable.
- p2m_put_2m_superpage(): Releases foreign page references in a 2MB superpage.
- p2m_type_radix_get(): Extracts the stored p2m_type from the radix tree using the PTE.
- p2m_free_page(): Returns a page either to the domain's freelist or to the domheap, depending on whether the domain is hardware-backed.

What is "hardware-backed"?

Quoted 9 lines

Defines XEN_PT_ENTRIES in asm/page.h to simplify loops over page table entries.

---
Changes in V2:
- New patch. It was a part of a big patch "xen/riscv: implement p2m mapping functionality" which was splitted to smaller.
- s/p2m_is_superpage/p2me_is_superpage.

See my earlier comments regarding naming.

xen/arch/riscv/p2m.c static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) · line 348

Quoted 7 lines
xen/arch/riscv/p2m.c +1 -0
@@ -345,11 +345,33 @@ static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn)
     return __map_domain_page(p2m->root + root_table_indx);
 }
 
+static p2m_type_t p2m_type_radix_get(struct p2m_domain *p2m, pte_t pte)

Does it matter to callers that ...

xen/arch/riscv/p2m.c static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) · line 359

Quoted 11 lines
xen/arch/riscv/p2m.c +11 -0
+{
+    void *ptr;
+    gfn_t gfn = mfn_to_gfn(p2m->domain, mfn_from_pte(pte));
+
+    ptr = radix_tree_lookup(&p2m->p2m_type, gfn_x(gfn));
+
+    if ( !ptr )
+        return p2m_invalid;
+
+    return radix_tree_ptr_to_int(ptr);
+}

... this is a radix tree lookup? IOW does "radix" need to be part of the function name? Also "get" may want to move forward in the name, to better match the naming of other functions.

xen/arch/riscv/p2m.c static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) · line 368

Quoted 9 lines
xen/arch/riscv/p2m.c +6 -1
+/*
+ * In the case of the P2M, the valid bit is used for other purpose. Use
+ * the type to check whether an entry is valid.
+ */
 static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
 {
-    panic("%s: isn't implemented for now\n", __func__);
+    return p2m_type_radix_get(p2m, pte) != p2m_invalid;
+}

No checking of the valid bit?

xen/arch/riscv/p2m.c static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) · line 374

Quoted 6 lines
xen/arch/riscv/p2m.c +5 -1
-    return false;
+static inline bool p2me_is_superpage(struct p2m_domain *p2m, pte_t pte,
+                                    unsigned int level)
+{
+    return p2me_is_valid(p2m, pte) && (pte.pte & PTE_ACCESS_MASK) &&
+           (level > 0);

In such combinations of conditions it's usually helpful to put the cheapest check(s) first. IOW what point is there in doing a radix tree lookup when the other two conditions aren't satisfied? (FTAOD applies elsewhere as well, even within this same patch.)

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 436

Quoted 12 lines
xen/arch/riscv/p2m.c +8 -0
@@ -404,11 +426,127 @@ static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl,
     return GUEST_TABLE_MAP_NONE;
 }
 
+static void p2m_put_foreign_page(struct page_info *pg)
+{
+    /*
+     * It's safe to do the put_page here because page_alloc will
+     * flush the TLBs if the page is reallocated before the end of
+     * this loop.
+     */
+    put_page(pg);

Is the comment really true? The page allocator will flush the normal TLBs, but not the stage-2 ones. Yet those are what you care about here, aiui.

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 446

Quoted 8 lines
xen/arch/riscv/p2m.c +8 -0
+/* Put any references on the single 4K page referenced by mfn. */
+static void p2m_put_4k_page(mfn_t mfn, p2m_type_t type)
+{
+    /* TODO: Handle other p2m types */
+
+    /* Detect the xenheap page and mark the stored GFN as invalid. */
+    if ( p2m_is_ram(type) && is_xen_heap_mfn(mfn) )
+        page_set_xenheap_gfn(mfn_to_page(mfn), INVALID_GFN);

Is this a valid thing to do? How do you make sure the respective uses (in gnttab's shared and status page arrays) are / were also removed?

Quoted 12 lines
xen/arch/riscv/p2m.c +12 -0
+}
+
+/* Put any references on the superpage referenced by mfn. */
+static void p2m_put_2m_superpage(mfn_t mfn, p2m_type_t type)
+{
+    struct page_info *pg;
+    unsigned int i;
+
+    ASSERT(mfn_valid(mfn));
+
+    pg = mfn_to_page(mfn);
+

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 468

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+    for ( i = 0; i < XEN_PT_ENTRIES; i++, pg++ )
+        p2m_put_foreign_page(pg);
+}
+
+/* Put any references on the page referenced by pte. */
+static void p2m_put_page(struct p2m_domain *p2m, const pte_t pte,
+                         unsigned int level)
+{
+    mfn_t mfn = pte_get_mfn(pte);
+    p2m_type_t p2m_type = p2m_type_radix_get(p2m, pte);

This gives you the type of the 1st page. What guarantees that all other pages in a superpage are of the exact same type?

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 478

Quoted 9 lines
xen/arch/riscv/p2m.c +9 -0
+    ASSERT(p2me_is_valid(p2m, pte));
+
+    /*
+     * TODO: Currently we don't handle level 2 super-page, Xen is not
+     * preemptible and therefore some work is needed to handle such
+     * superpages, for which at some point Xen might end up freeing memory
+     * and therefore for such a big mapping it could end up in a very long
+     * operation.
+     */

This is pretty unsatisfactory. Imo, if you don't deal with that right away, you're setting yourself up for a significant re-write.

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 482

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+    if ( level == 1 )
+        return p2m_put_2m_superpage(mfn, p2m_type);
+    else if ( level == 0 )
+        return p2m_put_4k_page(mfn, p2m_type);

Use switch() right away?

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 488

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+}
+
+static void p2m_free_page(struct domain *d, struct page_info *pg)
+{
+    if ( is_hardware_domain(d) )
+        free_domheap_page(pg);

Why's the hardware domain different here? It should have a pool just like all other domains have.

Quoted 11 lines
xen/arch/riscv/p2m.c +8 -0
+    else
+    {
+        spin_lock(&d->arch.paging.lock);
+        page_list_add_tail(pg, &d->arch.paging.p2m_freelist);
+        spin_unlock(&d->arch.paging.lock);
+    }
+}
+
 /* Free pte sub-tree behind an entry */
 static void p2m_free_entry(struct p2m_domain *p2m,
                            pte_t entry, unsigned int level)

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 508

Quoted 10 lines
xen/arch/riscv/p2m.c +8 -1
 {
-    panic("%s: hasn't been implemented yet\n", __func__);
+    unsigned int i;
+    pte_t *table;
+    mfn_t mfn;
+    struct page_info *pg;
+
+    /* Nothing to do if the entry is invalid. */
+    if ( !p2me_is_valid(p2m, entry) )
+        return;

Does this actually apply to intermediate page tables (which you handle later in the function), when that's (only) a P2M type check?

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 519

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+    if ( p2me_is_superpage(p2m, entry, level) || (level == 0) )
+    {
+#ifdef CONFIG_IOREQ_SERVER
+        /*
+         * If this gets called then either the entry was replaced by an entry
+         * with a different base (valid case) or the shattering of a superpage
+         * has failed (error case).
+         * So, at worst, the spurious mapcache invalidation might be sent.
+         */
+        if ( p2m_is_ram( p2m_type_radix_get(p2m, entry)) &&

Nit: Style.

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 531

Quoted 12 lines
xen/arch/riscv/p2m.c +12 -0
+             domain_has_ioreq_server(p2m->domain) )
+            ioreq_request_mapcache_invalidate(p2m->domain);
+#endif
+
+        p2m_put_page(p2m, entry, level);
+
+        return;
+    }
+
+    table = map_domain_page(pte_get_mfn(entry));
+    for ( i = 0; i < XEN_PT_ENTRIES; i++ )
+        p2m_free_entry(p2m, *(table + i), level - 1);

Better table[i]?

Jan

#53 lore raw

Re: [PATCH v2 06/17] xen/riscv: add root page table allocation

In reply to Oleksii Kurochko
Quoted 39 lines · Oleksii Kurochko
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Oleksii Kurochko
Quoted 27 lines · Jan Beulich
Quoted 19 lines · Oleksii Kurochko
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +0 -0
@@ -41,6 +41,91 @@ void p2m_write_unlock(struct p2m_domain *p2m)
        write_unlock(&p2m->lock);
    }
 
Quoted 11 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 48

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+static void clear_and_clean_page(struct page_info *page)
+{
+    clean_dcache_va_range(page, PAGE_SIZE);
+    clear_domain_page(page_to_mfn(page));
+}

A function of this name can, imo, only clear and then clean. Question is why it's the other way around, and what the underlying requirement is for the cleaning part to be there in the first place. Maybe that's obvious for a RISC-V person, but it's entirely non-obvious to me (Arm being different in this regard because of running with caches disabled at certain points in time).

Quoted 6 lines

You're right, the current name|clear_and_clean_page()| implies that clearing should come before cleaning, which contradicts the current implementation. The intent here is to ensure that the page contents are consistent in RAM (not just in cache) before use by other entities (guests or devices).

The clean must follow the clear — so yes, the order needs to be reversed.

What you don't address though - why's the cleaning needed in the first place?

Quoted 8 lines
Quoted 4 lines

If we clean the data cache first, we flush the d-cache and then use the page to perform the clear operation. As a result, the "cleared" value will be written into the d-cache. To avoid polluting the d-cache with the "cleared" value, the correct sequence is to clear the page first, then clean the data cache.

If you want to avoid cache pollution, I think you'd need to use a form of stores which simply bypass the cache. Yet then - why would this matter here, but not elsewhere? Wouldn't you better leave such to the hardware, unless you can prove a (meaningful) performance gain?

Quoted 6 lines

I thought about a case when IOMMU doesn't support coherent walks and p2m tables are shared between CPU and IOMMU. Then my understanding is:
- clear_page(p) just zero-ing a page in a CPU's cache.
- But IOMMU can see old data or uninitialized, if they still in cache.
- So, it is need to do clean_cache() to writeback data from cache to RAM, before a page will be used as a part of page table for IOMMU.

Okay, so this is purely about something that doesn't matter at all for now (until IOMMU support is introduced). Fair enough then to play safe from the beginning.

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 12 lines
Quoted 2 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 49

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+    unsigned int nr_pages = _AC(1,U) << order;

Nit (style): Missing blank after comma.

I've changed that to BIT(order, U)

Quoted 6 lines

xen/arch/riscv/p2m.c void p2m_write_unlock(struct p2m_domain *p2m) · line 53

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+    /* Return back nr_pages necessary for p2m root table. */
+
+    if ( ACCESS_ONCE(d->arch.paging.p2m_total_pages) < nr_pages )
+        panic("Specify more xen,domain-p2m-mem-mb\n");

You shouldn't panic() in anything involved in domain creation. You want to return NULL in this case.

It makes sense in this case just to return NULL.

Quoted 7 lines
Quoted 5 lines

Further, to me the use of "more" looks misleading here. Do you perhaps mean "larger" or "bigger"?

This also looks to be happening without any lock held. If that's intentional, I think the "why" wants clarifying in a code comment.

Agree, returning back pages necessary for p2m root table should be done under spin_lock(&d->arch.paging.lock).

Which should be acquired at the paging_*() layer then, not at the p2m_*() layer. (As long as you mean to have that separation, that is. See the earlier discussion on that matter.)

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Then partly p2m_set_allocation() should be moved to paging_*() too.

Not exactly sure what you mean. On x86 at least the paging layer part of the function is pretty slim.

I meant that part of code which is spin_lock(&d->arch.paging.lock); ... spin_unlock(&d->arch.paging.lock) in function p2m_set_allocation() should be moved somewhere to paging_*() layer for the same logic as you suggested to move part of p2m_allocate_root()'s code which is guarded by d->arch.paging.lock to paging_*() layer.

Yes, of course.

Jan

#104 lore raw

Re: [PATCH v2 13/17] xen/riscv: Implement p2m_entry_from_mfn() and support PBMT configuration

In reply to Oleksii Kurochko

xen/arch/riscv/include/asm/page.h line 79

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/page.h +1 -0
@@ -76,6 +76,14 @@
 #define PTE_SMALL       BIT(10, UL)
 #define PTE_POPULATE    BIT(11, UL)
 
+enum pbmt_type_t {

Please can we stick to _t suffixes only being used on typedef-ed identifiers?

xen/arch/riscv/include/asm/page.h line 84

Quoted 5 lines
xen/arch/riscv/include/asm/page.h +5 -0
+    pbmt_pma,
+    pbmt_nc,
+    pbmt_io,
+    pbmt_rsvd,
+    pbmt_max,

It's a 2-bit field in the PTE, isn't it? In which case the maximum valid value to put there is 3. That's what an identifier named "max" should evaluate to. The value 4 here would want to be named "count", "num", "nr", or alike.

xen/arch/riscv/p2m.c static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) · line 348

Quoted 7 lines
xen/arch/riscv/p2m.c +1 -0
@@ -345,6 +345,26 @@ static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn)
     return __map_domain_page(p2m->root + root_table_indx);
 }
 
+static int p2m_type_radix_set(struct p2m_domain *p2m, pte_t pte, p2m_type_t t)

See comments on the earlier patch regarding naming.

xen/arch/riscv/p2m.c static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) · line 351

Quoted 3 lines
xen/arch/riscv/p2m.c +3 -0
+{
+    int rc;
+    gfn_t gfn = mfn_to_gfn(p2m->domain, mfn_from_pte(pte));

How does this work, when you record GFNs only for Xenheap pages? I don't think you can get around having the caller pass in the GFN. At which point the PTE probably doesn't need passing.

Quoted 27 lines
xen/arch/riscv/p2m.c +16 -3
+    rc = radix_tree_insert(&p2m->p2m_type, gfn_x(gfn),
+                           radix_tree_int_to_ptr(t));
+    if ( rc == -EEXIST )
+    {
+        /* If a setting already exists, change it to the new one */
+        radix_tree_replace_slot(
+            radix_tree_lookup_slot(
+                &p2m->p2m_type, gfn_x(gfn)),
+            radix_tree_int_to_ptr(t));
+        rc = 0;
+    }
+
+    return rc;
+}
+
 static p2m_type_t p2m_type_radix_get(struct p2m_domain *p2m, pte_t pte)
 {
     void *ptr;
@@ -389,12 +409,87 @@ static inline void p2m_remove_pte(pte_t *p, bool clean_pte)
     p2m_write_pte(p, pte, clean_pte);
 }
 
-static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn,
-                                p2m_type_t t, p2m_access_t a)
+static void p2m_set_permission(pte_t *e, p2m_type_t t, p2m_access_t a)
 {
-    panic("%s: hasn't been implemented yet\n", __func__);

xen/arch/riscv/p2m.c static inline void p2m_remove_pte(pte_t *p, bool clean_pte) · line 423

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+    /* First apply type permissions */
+    switch ( t )
+    {
+    case p2m_ram_rw:
+        e->pte |= PTE_ACCESS_MASK;
+        break;
+
+    case p2m_mmio_direct_dev:
+        e->pte |= (PTE_READABLE | PTE_WRITABLE);
+        e->pte &= ~PTE_EXECUTABLE;

What's wrong with code living in MMIO, e.g. in the ROM of a PCI device? Such code would want to be executable.

xen/arch/riscv/p2m.c static inline void p2m_remove_pte(pte_t *p, bool clean_pte) · line 433

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        break;
+
+    case p2m_invalid:
+        e->pte &= ~PTE_ACCESS_MASK;
+        break;
+
+    default:
+        BUG();
+        break;
+    }

I think you ought to handle all types that are defined right away. I also don't think you should BUG() in the default case (also in the other switch() below). ASSERT_UNEACHABLE() may be fine, along with clearing all permissions in the entry for release builds.

Quoted 23 lines
xen/arch/riscv/p2m.c +23 -0
+    /* Then restrict with access permissions */
+    switch ( a )
+    {
+    case p2m_access_rwx:
+        break;
+    case p2m_access_wx:
+        e->pte &= ~PTE_READABLE;
+        break;
+    case p2m_access_rw:
+        e->pte &= ~PTE_EXECUTABLE;
+        break;
+    case p2m_access_w:
+        e->pte &= ~(PTE_READABLE | PTE_EXECUTABLE);
+        e->pte &= ~PTE_EXECUTABLE;
+        break;
+    case p2m_access_rx:
+    case p2m_access_rx2rw:
+        e->pte &= ~PTE_WRITABLE;
+        break;
+    case p2m_access_x:
+        e->pte &= ~(PTE_READABLE | PTE_WRITABLE);
+        break;
+    case p2m_access_r:

xen/arch/riscv/p2m.c static inline void p2m_remove_pte(pte_t *p, bool clean_pte) · line 467

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        e->pte &= ~(PTE_WRITABLE | PTE_EXECUTABLE);
+        break;
+    case p2m_access_n:
+    case p2m_access_n2rwx:
+        e->pte &= ~PTE_ACCESS_MASK;
+        break;
+    default:
+        BUG();
+        break;
+    }

Nit: Blank lines between non-fall-through case blocks, please.

xen/arch/riscv/p2m.c static inline void p2m_remove_pte(pte_t *p, bool clean_pte) · line 472

Quoted 3 lines
xen/arch/riscv/p2m.c +3 -0
+static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn, p2m_type_t t, p2m_access_t a)
+{
+    pte_t e = (pte_t) { 1 };

What's the 1 doing here?

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+    switch ( t )
+    {
+    case p2m_mmio_direct_dev:
+        e.pte |= PTE_PBMT_IO;
+        break;

xen/arch/riscv/p2m.c static inline void p2m_remove_pte(pte_t *p, bool clean_pte) · line 488

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+
+    default:
+        break;
+    }
+
+    p2m_set_permission(&e, t, a);
+
+    ASSERT(!(mfn_to_maddr(mfn) & ~PADDR_MASK));
+
+    pte_set_mfn(&e, mfn);

Based on how things work on x86 (and how I would have expected them to also work on Arm), may I suggest that you set MFN ahead of permissions, so that the permissions setting function can use the MFN for e.g. a lookup in mmio_ro_ranges.

xen/arch/riscv/p2m.c static inline void p2m_remove_pte(pte_t *p, bool clean_pte) · line 490

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+    BUG_ON(p2m_type_radix_set(p2m, e, t));

I'm not convinced of this error handling here either. Radix tree insertion _can_ fail, e.g. when there's no memory left. This must not bring down Xen, or we'll have an XSA right away. You could zap the PTE, or if need be you could crash the offending domain.

In this context (not sure if I asked before): With this use of a radix tree, how do you intend to bound the amount of memory that a domain can use, by making Xen insert very many entries?

Jan

#126 lore raw

Re: [PATCH v2 14/17] xen/riscv: implement p2m_next_level()

In reply to Oleksii Kurochko
Quoted 6 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +0 -0
@@ -387,6 +387,17 @@ static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
     return p2m_type_radix_get(p2m, pte) != p2m_invalid;
 }
 

xen/arch/riscv/p2m.c static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte) · line 399

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+/*
+ * pte_is_* helpers are checking the valid bit set in the
+ * PTE but we have to check p2m_type instead (look at the comment above
+ * p2me_is_valid())
+ * Provide our own overlay to check the valid bit.
+ */
+static inline bool p2me_is_mapping(struct p2m_domain *p2m, pte_t pte)
+{
+    return p2me_is_valid(p2m, pte) && (pte.pte & PTE_ACCESS_MASK);
+}

Same question as on the earlier patch - does P2M type apply to intermediate page tables at all? (Conceptually it shouldn't.)

Quoted 7 lines
xen/arch/riscv/p2m.c +3 -0
@@ -492,6 +503,70 @@ static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn, p2m_type_t t,
     return e;
 }
 
+/* Generate table entry with correct attributes. */
+static pte_t page_to_p2m_table(struct p2m_domain *p2m, struct page_info *page)
+{

xen/arch/riscv/p2m.c static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn, p2m_type_t t, · line 518

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+    /*
+     * Since this function generates a table entry, according to "Encoding
+     * of PTE R/W/X fields," the entry's r, w, and x fields must be set to 0
+     * to point to the next level of the page table.
+     * Therefore, to ensure that an entry is a page table entry,
+     * `p2m_access_n2rwx` is passed to `mfn_to_p2m_entry()` as the access value,
+     * which overrides whatever was passed as `p2m_type_t` and guarantees that
+     * the entry is a page table entry by setting r = w = x = 0.
+     */
+    return p2m_entry_from_mfn(p2m, page_to_mfn(page), p2m_ram_rw, p2m_access_n2rwx);

Similarly P2M access shouldn't apply to intermediate page tables. (Moot with that, but (ab)using p2m_access_n2rwx would also look wrong: You did read what it means, didn't you?)

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+}
+
+static struct page_info *p2m_alloc_page(struct domain *d)
+{
+    struct page_info *pg;
+
+    /*

xen/arch/riscv/p2m.c static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn, p2m_type_t t, · line 535

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+     * For hardware domain, there should be no limit in the number of pages that
+     * can be allocated, so that the kernel may take advantage of the extended
+     * regions. Hence, allocate p2m pages for hardware domains from heap.
+     */
+    if ( is_hardware_domain(d) )
+    {
+        pg = alloc_domheap_page(d, MEMF_no_owner);
+        if ( pg == NULL )
+            printk(XENLOG_G_ERR "Failed to allocate P2M pages for hwdom.\n");
+    }

The comment looks to have been taken verbatim from Arm. Whatever "extended regions" are, does the same concept even exist on RISC-V?

Also, special casing Dom0 like this has benefits, but also comes with a
pitfall: If the system's out of memory, allocations will fail. A pre- populated pool would avoid that (until exhausted, of course). If special- casing of Dom0 is needed, I wonder whether ...

xen/arch/riscv/p2m.c static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn, p2m_type_t t, · line 541

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+    else
+    {
+        spin_lock(&d->arch.paging.lock);
+        pg = page_list_remove_head(&d->arch.paging.p2m_freelist);
+        spin_unlock(&d->arch.paging.lock);
+    }

... going this path but with a Dom0-only fallback to general allocation wouldn't be the better route.

Quoted 11 lines
xen/arch/riscv/p2m.c +11 -0
+    return pg;
+}
+
+/* Allocate a new page table page and hook it in via the given entry. */
+static int p2m_create_table(struct p2m_domain *p2m, pte_t *entry)
+{
+    struct page_info *page;
+    pte_t *p;
+
+    ASSERT(!p2me_is_valid(p2m, *entry));
+

xen/arch/riscv/p2m.c static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn, p2m_type_t t, · line 563

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+    page = p2m_alloc_page(p2m->domain);
+    if ( page == NULL )
+        return -ENOMEM;
+
+    page_list_add(page, &p2m->pages);
+
+    p = __map_domain_page(page);
+    clear_page(p);
+
+    unmap_domain_page(p);

clear_domain_page()? Or actually clear_and_clean_page()?

Quoted 13 lines
xen/arch/riscv/p2m.c +8 -1
@@ -516,9 +591,33 @@ static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl,
                           unsigned int level, pte_t **table,
                           unsigned int offset)
 {
-    panic("%s: hasn't been implemented yet\n", __func__);
+    pte_t *entry;
+    int ret;
+    mfn_t mfn;
+
+    entry = *table + offset;
+
+    if ( !p2me_is_valid(p2m, *entry) )
+    {

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 611

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        if ( !alloc_tbl )
+            return GUEST_TABLE_MAP_NONE;
+
+        ret = p2m_create_table(p2m, entry);
+        if ( ret )
+            return GUEST_TABLE_MAP_NOMEM;
+    }
+
+    /* The function p2m_next_level() is never called at the last level */
+    ASSERT(level != 0);

Logically you would perhaps better do this ahead of trying to allocate a page table. Calls here with level == 0 are invalid in all cases aiui, not just when you make it here.

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 618

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+    if ( p2me_is_mapping(p2m, *entry) )
+        return GUEST_TABLE_SUPER_PAGE;
+
+    mfn = mfn_from_pte(*entry);
+
+    unmap_domain_page(*table);
+    *table = map_domain_page(mfn);

Just to mention it (may not need taking care of right away), there's an inefficiency here: In p2m_create_table() you map the page to clear it. Then you tear down that mapping, just to re-establish it here.

Jan

#136 lore raw

Re: [PATCH v2 15/17] xen/riscv: Implement superpage splitting for p2m mappings

In reply to Oleksii Kurochko
Quoted 11 lines · Oleksii Kurochko

Add support for down large memory mappings ("superpages") in the RISC-V p2m mapping so that smaller, more precise mappings ("finer-grained entries") can be inserted into lower levels of the page table hierarchy.

To implement that the following is done:
- Introduce p2m_split_superpage(): Recursively shatters a superpage into smaller page table entries down to the target level, preserving original permissions and attributes.
- __p2m_set_entry() updated to invoke superpage splitting when inserting entries at lower levels within a superpage-mapped region.

Quoted 4 lines

This implementation is based on the ARM code, with modifications to the part that follows the BBM (break-before-make) approach. Unlike ARM, RISC-V does not require BBM, so there is no need to invalidate the PTE and flush the TLB before updating it with the newly created, split page table.

But some flushing is going to be necessary. As long as you only ever do global flushes, the one after the individual PTE modification (within the split table) will do (if BBM isn't required, see below), but once you move to more fine-grained flushing, that's not going to be enough anymore. Not sure it's a good idea to leave such a pitfall.

As to (no need for) BBM: I couldn't find anything to that effect in the privileged spec. Can you provide some pointer? What I found instead is e.g. this sentence: "To ensure that implicit reads observe writes to the same memory locations, an SFENCE.VMA instruction must be executed after the writes to flush the relevant cached translations." And this: "Accessing the same location using different cacheability attributes may cause loss of coherence." (This may not only occur when the same physical address is mapped twice at different VAs, but also after the shattering of a superpage when the new entry differs in cacheability.)

Quoted 2 lines

Additionally, the page table walk logic has been adjusted, as ARM uses the opposite walk order compared to RISC-V.

I think you used some similar wording already in an earlier patch. I find this confusing: Walk order is, aiui, the same. It's merely the numbering of levels that is the opposite way round, isn't it?

Quoted 37 lines

---
Changes in V2:
- New patch. It was a part of a big patch "xen/riscv: implement p2m mapping functionality" which was splitted to smaller.
- Update the commit above the cycle which creates new page table as RISC-V travserse page tables in an opposite to ARM order.
- RISC-V doesn't require BBM so there is no needed for invalidating and TLB flushing before updating PTE.

---
 xen/arch/riscv/p2m.c | 102 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 101 insertions(+), 1 deletion(-)
xen/arch/riscv/p2m.c +16 -0
index 87dd636b80..79c4473f1f 100644
@@ -743,6 +743,77 @@ static void p2m_free_entry(struct p2m_domain *p2m,
     p2m_free_page(p2m->domain, pg);
 }
 
+static bool p2m_split_superpage(struct p2m_domain *p2m, pte_t *entry,
+                                unsigned int level, unsigned int target,
+                                const unsigned int *offsets)
+{
+    struct page_info *page;
+    unsigned int i;
+    pte_t pte, *table;
+    bool rv = true;
+
+    /* Convenience aliases */
+    mfn_t mfn = pte_get_mfn(*entry);
+    unsigned int next_level = level - 1;
+    unsigned int level_order = XEN_PT_LEVEL_ORDER(next_level);
+
+    /*
+     * This should only be called with target != level and the entry is

xen/arch/riscv/p2m.c static void p2m_free_entry(struct p2m_domain *p2m, · line 771

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+     * a superpage.
+     */
+    ASSERT(level > target);
+    ASSERT(p2me_is_superpage(p2m, *entry, level));
+
+    page = p2m_alloc_page(p2m->domain);
+    if ( !page )
+        return false;
+
+    page_list_add(page, &p2m->pages);

Is there a reason this list maintenance isn't done in p2m_alloc_page()?

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+    table = __map_domain_page(page);
+
+    /*
+     * We are either splitting a second level 1G page into 512 first level
+     * 2M pages, or a first level 2M page into 512 zero level 4K pages.
+     */

xen/arch/riscv/p2m.c static void p2m_free_entry(struct p2m_domain *p2m, · line 787

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+    for ( i = 0; i < XEN_PT_ENTRIES; i++ )
+    {
+        pte_t *new_entry = table + i;
+
+        /*
+         * Use the content of the superpage entry and override
+         * the necessary fields. So the correct permission are kept.
+         */
+        pte = *entry;
+        pte_set_mfn(&pte, mfn_add(mfn, i << level_order));

While okay as long as you only permit superpages up to 1G, this is another trap for someone to fall into: Imo i would better be unsigned long right away, considering that RISC-V permits large pages at all levels.

xen/arch/riscv/p2m.c static void p2m_free_entry(struct p2m_domain *p2m, · line 800

Quoted 12 lines
xen/arch/riscv/p2m.c +12 -0
+        write_pte(new_entry, pte);
+    }
+
+    /*
+     * Shatter superpage in the page to the level we want to make the
+     * changes.
+     * This is done outside the loop to avoid checking the offset to
+     * know whether the entry should be shattered for every entry.
+     */
+    if ( next_level != target )
+        rv = p2m_split_superpage(p2m, table + offsets[next_level],
+                                 level - 1, target, offsets);

I don't understand the comment: Under what conditions would every entry need (further) shattering? And where's that happening? Or is this merely a word ordering issue in the sentence, and "for every entry" wants moving ahead? (In that case I'm unconvinced this is in need of commenting upon.)

xen/arch/riscv/p2m.c static void p2m_free_entry(struct p2m_domain *p2m, · line 806

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+    /* TODO: why it is necessary to have clean here? Not somewhere in the caller */
+    if ( p2m->clean_pte )
+        clean_dcache_va_range(table, PAGE_SIZE);
+
+    unmap_domain_page(table);

Again likely not something that wants taking care of right away, but there again is an inefficiency here: The caller almost certainly wants to map the same page again, to update the one entry that caused the request to shatter the page.

xen/arch/riscv/p2m.c static void p2m_free_entry(struct p2m_domain *p2m, · line 812

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+    /*
+     * Even if we failed, we should install the newly allocated PTE
+     * entry. The caller will be in charge to free the sub-tree.
+     */
+    p2m_write_pte(entry, page_to_p2m_table(p2m, page), p2m->clean_pte);

Why would it be wrong to free the page right here, vacating the entry at the same time (or leaving just that to the caller)? (IOW - if this is an implementation decision of yours, I think the word "should" would want dropping.) After all, the caller invoking p2m_free_entry() on the thus split PTE is less efficient (needs to iterate over all entries) than on the original one (where it's just a single superpage).

xen/arch/riscv/p2m.c static int __p2m_set_entry(struct p2m_domain *p2m, · line 878

Quoted 3 lines
xen/arch/riscv/p2m.c +0 -0
@@ -806,7 +877,36 @@ static int __p2m_set_entry(struct p2m_domain *p2m,
      */
     if ( level > target )

This condition is likely too strong, unless you actually mean to also split a superpage if it really wouldn't need splitting (new entry written still fitting with the superpage mapping, i.e. suitable MFN and same attributes).

Quoted 10 lines
xen/arch/riscv/p2m.c +8 -1
     {
-        panic("Shattering isn't implemented\n");
+        /* We need to split the original page. */
+        pte_t split_pte = *entry;
+
+        ASSERT(p2me_is_superpage(p2m, *entry, level));
+
+        if ( !p2m_split_superpage(p2m, &split_pte, level, target, offsets) )
+        {
+            /* Free the allocated sub-tree */

xen/arch/riscv/p2m.c static int __p2m_set_entry(struct p2m_domain *p2m, · line 897

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+            p2m_free_entry(p2m, split_pte, level);
+
+            rc = -ENOMEM;
+            goto out;
+        }
+
+        p2m_write_pte(entry, split_pte, p2m->clean_pte);
+
+        /* Then move to the level we want to make real changes */
+        for ( ; level < target; level++ )

Don't you mean to move downwards here? At which point I wonder: Did you test this code?

xen/arch/riscv/p2m.c static int __p2m_set_entry(struct p2m_domain *p2m, · line 907

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        {
+            rc = p2m_next_level(p2m, true, level, &table, offsets[level]);
+
+            /*
+             * The entry should be found and either be a table
+             * or a superpage if level 0 is not targeted
+             */
+            ASSERT(rc == GUEST_TABLE_NORMAL ||
+                   (rc == GUEST_TABLE_SUPER_PAGE && target > 0));
+        }

This, too, is inefficient (but likely good enough as a starting point): You walk tables twice - first when splitting, and then again when finding the target level.

Considering the enclosing if(), this also again is a do/while() candidate.

Jan

#144 lore raw

Re: [PATCH v2 16/17] xen/riscv: implement mfn_valid() and page reference, ownership handling helpers

In reply to Oleksii Kurochko
Quoted 26 lines · Oleksii Kurochko

Implement the mfn_valid() macro to verify whether a given MFN is valid by checking that it falls within the range [start_page, max_page). These bounds are initialized based on the start and end addresses of RAM.

As part of this patch, start_page is introduced and initialized with the PFN of the first RAM page.

Also, after providing a non-stub implementation of the mfn_valid() macro,
the following compilation errors started to occur:
riscv64-linux-gnu-ld: prelink.o: in function `__next_node':
/build/xen/./include/xen/nodemask.h:202: undefined reference to `page_is_ram_type'
riscv64-linux-gnu-ld: prelink.o: in function `get_free_buddy':
/build/xen/common/page_alloc.c:881: undefined reference to `page_is_ram_type'
riscv64-linux-gnu-ld: prelink.o: in function `alloc_heap_pages':
/build/xen/common/page_alloc.c:1043: undefined reference to `page_get_owner_and_reference'
riscv64-linux-gnu-ld: /build/xen/common/page_alloc.c:1098: undefined reference to `page_is_ram_type'
riscv64-linux-gnu-ld: prelink.o: in function `ns16550_interrupt':
/build/xen/drivers/char/ns16550.c:205: undefined reference to `get_page'
riscv64-linux-gnu-ld: ./.xen-syms.0: hidden symbol `page_get_owner_and_reference' isn't defined
riscv64-linux-gnu-ld: final link failed: bad value
make[2]: *** [arch/riscv/Makefile:35: xen-syms] Error 1
To resolve these errors, the following functions have also been introduced,
based on their Arm counterparts:
- page_get_owner_and_reference() and its variant to safely acquire a
reference to a page and retrieve its owner.
- put_page() and put_page_nr() to release page references and free the page

Quoted 8 lines

when the count drops to zero.
For put_page_nr(), code related to static memory configuration is wrapped with CONFIG_STATIC_MEMORY, as this configuration has not yet been moved to common code. Therefore, PGC_static and free_domstatic_page() are not introduced for RISC-V. However, since this configuration could be useful in the future, the relevant code is retained and conditionally compiled.
- A stub for page_is_ram_type() that currently always returns 0 and asserts unreachable, as RAM type checking is not yet implemented.

How does this end up working when common code references the function?

xen/arch/riscv/include/asm/mm.h static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr) · line 297

Quoted 12 lines
xen/arch/riscv/include/asm/mm.h +6 -2
@@ -288,8 +289,12 @@ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)
 #define page_get_owner(p)    (p)->v.inuse.domain
 #define page_set_owner(p, d) ((p)->v.inuse.domain = (d))
 
-/* TODO: implement */
-#define mfn_valid(mfn) ({ (void)(mfn); 0; })
+extern unsigned long start_page;
+
+#define mfn_valid(mfn) ({                                   \
+    unsigned long mfn__ = mfn_x(mfn);                       \
+    likely((mfn__ >= start_page) && (mfn__ < max_page));    \
+})

I don't think you should try to be clever and avoid using __mfn_valid() here, at least not without an easily identifiable TODO. Surely you've seen that both Arm and x86 use it.

Also, according to all I know, likely() doesn't work very well when used like this, except for architectures supporting conditionally executed insns (like Arm32 or IA-64, i.e. beyond conditional branches). I.e. if you want to use likely() here, I think you need two of them.

xen/arch/riscv/mm.c static void __init setup_directmap_mappings(unsigned long base_mfn, · line 523

Quoted 5 lines
xen/arch/riscv/mm.c +1 -0
@@ -525,6 +520,8 @@ static void __init setup_directmap_mappings(unsigned long base_mfn,
 #error setup_{directmap,frametable}_mapping() should be implemented for RV_32
 #endif
 
+unsigned long __read_mostly start_page;

Memory hotplug question again: __read_mostly or __ro_after_init?

Quoted 14 lines
xen/arch/riscv/mm.c +10 -0
@@ -613,3 +612,91 @@ void __iomem *ioremap(paddr_t pa, size_t len)
 {
     return ioremap_attr(pa, len, PAGE_HYPERVISOR_NOCACHE);
 }
+
+int page_is_ram_type(unsigned long mfn, unsigned long mem_type)
+{
+    ASSERT_UNREACHABLE();
+
+    return 0;
+}
+
+static struct domain *page_get_owner_and_nr_reference(struct page_info *page,
+                                                      unsigned long nr)

xen/arch/riscv/mm.c void __iomem *ioremap(paddr_t pa, size_t len) · line 634

Quoted 10 lines
xen/arch/riscv/mm.c +10 -0
+{
+    unsigned long x, y = page->count_info;
+    struct domain *owner;
+
+    /* Restrict nr to avoid "double" overflow */
+    if ( nr >= PGC_count_mask )
+    {
+        ASSERT_UNREACHABLE();
+        return NULL;
+    }

I question the validity of this, already in the Arm original: I can't spot how the caller guarantees to stay below that limit. Without such an (attempted) guarantee, ASSERT_UNREACHABLE() is wrong to use. All I can see is process_shm_node() incrementing shmem_extra[].nr_shm_borrowers, without any limit check.

xen/arch/riscv/mm.c void __iomem *ioremap(paddr_t pa, size_t len) · line 641

Quoted 6 lines
xen/arch/riscv/mm.c +6 -0
+    do {
+        x = y;
+        /*
+         * Count ==  0: Page is not allocated, so we cannot take a reference.
+         * Count == -1: Reference count would wrap, which is invalid.
+         */

May I once again ask that you look carefully at comments (as much as at code) you copy. Clearly this comment wasn't properly updated when the bumping by 1 was changed to bumping by nr.

Quoted 24 lines
xen/arch/riscv/mm.c +24 -0
+        if ( unlikely(((x + nr) & PGC_count_mask) <= nr) )
+            return NULL;
+    }
+    while ( (y = cmpxchg(&page->count_info, x, x + nr)) != x );
+
+    owner = page_get_owner(page);
+    ASSERT(owner);
+
+    return owner;
+}
+
+struct domain *page_get_owner_and_reference(struct page_info *page)
+{
+    return page_get_owner_and_nr_reference(page, 1);
+}
+
+void put_page_nr(struct page_info *page, unsigned long nr)
+{
+    unsigned long nx, x, y = page->count_info;
+
+    do {
+        ASSERT((y & PGC_count_mask) >= nr);
+        x  = y;
+        nx = x - nr;

xen/arch/riscv/mm.c void __iomem *ioremap(paddr_t pa, size_t len) · line 675

Quoted 10 lines
xen/arch/riscv/mm.c +10 -0
+    }
+    while ( unlikely((y = cmpxchg(&page->count_info, x, nx)) != x) );
+
+    if ( unlikely((nx & PGC_count_mask) == 0) )
+    {
+#ifdef CONFIG_STATIC_MEMORY
+        if ( unlikely(nx & PGC_static) )
+            free_domstatic_page(page);
+        else
+#endif

Such #ifdef-ed-out code is liable to go stale. Minimally use IS_ENABLED(). Even better would imo be if you introduced a "stub" PGC_static, resolving to 0 (i.e. for now unconditionally).

Jan

#63 lore raw

Re: [PATCH v2 08/17] xen/riscv: add new p2m types and helper macros for type classification

In reply to Jan Beulich
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Oleksii Kurochko
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Oleksii Kurochko
Quoted 19 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 64

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -61,8 +61,28 @@ struct p2m_domain {
    typedef enum {
        p2m_invalid = 0,    /* Nothing mapped here */
        p2m_ram_rw,         /* Normal read/write domain RAM */
+    p2m_ram_ro,         /* Read-only; writes are silently dropped */

As indicated before - this type should be added when the special handling that it requires is also introduced.

Perhaps, I missed that. I will drop this type for now.

Quoted 2 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 65

Quoted 1 line
xen/arch/riscv/include/asm/p2m.h +1 -0
+    p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */

What's the _dev suffix indicating here?

It indicates that it is device memory, probably, it isn't so necessary in case of RISC-V as spec doesn't use such terminology. In RISC-V there is only available IO, NC. And we are |using PTE_PBMT_IO for |p2m_mmio_direct_dev.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

Maybe it would be better just to rename s/p2m_mmio_direct_dev/p2m_mmio_direct_io.

I forgot that p2m_mmio_direct_dev is used by common code for dom0less code (handle_passthrough_prop())

That'll want abstracting out, I think. I don't view it as helpful to clutter RISC-V (and later perhaps also PPC) with Arm-specific terminology.

Would it be better then just rename it to p2m_device? Then it won't clear for Arm which type of MMIO p2m's types is used as Arm has there MMIO types: *_dev, *_nc, *_c.

I don't understand why Arm matters here. P2M types want naming in a way that makes sense for RISC-V.

It doesn't matter.
But if we want to change the type name from p2m_mmio_direct_dev to p2m_mmio_direct or p2m_device then it will affect Arm too as p2m_mmio_direct_dev is used in dom0less code which is also used by Arm.
I just re-used p2m_mmio_direct_dev as it looked for me pretty generic and clear for what this type is.

Quoted 4 lines
Quoted 2 lines

As an option (which I don't really like) it could be "#define p2m_mmio_direct_dev ARCH_specific_name" in asm/p2m.h to not touch common code.

A #define may be needed, but not one to _still_ introduce Arm naming into non-Arm code.

As I mentioned above that p2m_mmio_direct_dev sounds pretty generic to me and I am okay to use it for RISC-V. But if you have better suggestions I will be happy to consider it.

~ Oleksii

#145 lore raw

Re: [PATCH v2 16/17] xen/riscv: implement mfn_valid() and page reference, ownership handling helpers

In reply to Jan Beulich
Quoted 43 lines · Jan Beulich
Quoted 14 lines · Oleksii Kurochko
xen/arch/riscv/mm.c +10 -0
@@ -613,3 +612,91 @@ void __iomem *ioremap(paddr_t pa, size_t len)
 {
     return ioremap_attr(pa, len, PAGE_HYPERVISOR_NOCACHE);
 }
+
+int page_is_ram_type(unsigned long mfn, unsigned long mem_type)
+{
+    ASSERT_UNREACHABLE();
+
+    return 0;
+}
+
+static struct domain *page_get_owner_and_nr_reference(struct page_info *page,
+                                                      unsigned long nr)

xen/arch/riscv/mm.c void __iomem *ioremap(paddr_t pa, size_t len) · line 634

Quoted 10 lines
xen/arch/riscv/mm.c +10 -0
+{
+    unsigned long x, y = page->count_info;
+    struct domain *owner;
+
+    /* Restrict nr to avoid "double" overflow */
+    if ( nr >= PGC_count_mask )
+    {
+        ASSERT_UNREACHABLE();
+        return NULL;
+    }

I question the validity of this, already in the Arm original: I can't spot how the caller guarantees to stay below that limit. Without such an (attempted) guarantee, ASSERT_UNREACHABLE() is wrong to use. All I can see is process_shm_node() incrementing shmem_extra[].nr_shm_borrowers, without any limit check.

xen/arch/riscv/mm.c void __iomem *ioremap(paddr_t pa, size_t len) · line 641

Quoted 6 lines
xen/arch/riscv/mm.c +6 -0
+    do {
+        x = y;
+        /*
+         * Count ==  0: Page is not allocated, so we cannot take a reference.
+         * Count == -1: Reference count would wrap, which is invalid.
+         */

May I once again ask that you look carefully at comments (as much as at code) you copy. Clearly this comment wasn't properly updated when the bumping by 1 was changed to bumping by nr.

Quoted 10 lines

xen/arch/riscv/mm.c void __iomem *ioremap(paddr_t pa, size_t len) · line 651

Quoted 10 lines
xen/arch/riscv/mm.c +10 -0
+        if ( unlikely(((x + nr) & PGC_count_mask) <= nr) )
+            return NULL;
+    }
+    while ( (y = cmpxchg(&page->count_info, x, x + nr)) != x );
+
+    owner = page_get_owner(page);
+    ASSERT(owner);
+
+    return owner;
+}

There also looks to be a dead code concern here (towards the "nr" parameters here and elsewhere, when STATIC_SHM=n). Just that apparently we decided to leave out Misra rule 2.2 entirely.

Jan

#32 lore raw

Re: [PATCH v2 04/17] xen/riscv: construct the P2M pages pool for guests

In reply to Jan Beulich
Quoted 62 lines · Jan Beulich
Quoted 49 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +45 -0
@@ -113,3 +117,58 @@ int p2m_init(struct domain *d)
 
      return 0;
  }
+
+/*
+ * Set the pool of pages to the required number of pages.
+ * Returns 0 for success, non-zero for failure.
+ * Call with d->arch.paging.lock held.
+ */
+int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
+{
+    struct page_info *pg;
+
+    ASSERT(spin_is_locked(&d->arch.paging.lock));
+
+    for ( ; ; )
+    {
+        if ( d->arch.paging.p2m_total_pages < pages )
+        {
+            /* Need to allocate more memory from domheap */
+            pg = alloc_domheap_page(d, MEMF_no_owner);
+            if ( pg == NULL )
+            {
+                printk(XENLOG_ERR "Failed to allocate P2M pages.\n");
+                return -ENOMEM;
+            }
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)++;
+            page_list_add_tail(pg, &d->arch.paging.p2m_freelist);
+        }
+        else if ( d->arch.paging.p2m_total_pages > pages )
+        {
+            /* Need to return memory to domheap */
+            pg = page_list_remove_head(&d->arch.paging.p2m_freelist);
+            if( pg )
+            {
+                ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+                free_domheap_page(pg);
+            }
+            else
+            {
+                printk(XENLOG_ERR
+                       "Failed to free P2M pages, P2M freelist is empty.\n");
+                return -ENOMEM;
+            }
+        }
+        else
+            break;
+

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 174

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        /* Check to see if we need to yield and try again */
+        if ( preempted && general_preempt_check() )
+        {
+            *preempted = true;
+            return -ERESTART;
+        }
+    }
+
+    return 0;
+}

Btw, with the order-2 requirement for the root page table, you may want to consider an alternative approach: Here you could allocate some order-2

Quoted 8 lines

pages (possibly up to as many as a domain might need, which right now would be exactly one), put them on a separate list, and consume the root table(s) from there. If you run out of pages on the order-0 list, you could shatter a page from the order-2 one (as long as that's still non- empty). The difficulty would be with freeing, where a previously shattered order-2 page would be nice to re-combine once all of its constituents are free again. The main benefit would be avoiding the back and forth in patch 6.

It is an option.

But I'm still not 100% sure it's necessary to allocate the root page table from the freelist. We could simply allocate the root page table from the domheap (as is done for hardware domains) and reserve the freelist for other pages.
The freelist is specific to Dom0less guest domains and is primarily used to limit the amount of memory available for the guest—essentially for static configurations where you want a clear and fixed limit on p2m allocations.

~ Oleksii

#33 lore raw

Re: [PATCH v2 04/17] xen/riscv: construct the P2M pages pool for guests

Quoted 75 lines · Oleksii Kurochko
Quoted 62 lines · Jan Beulich
Quoted 49 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +45 -0
@@ -113,3 +117,58 @@ int p2m_init(struct domain *d)
 
      return 0;
  }
+
+/*
+ * Set the pool of pages to the required number of pages.
+ * Returns 0 for success, non-zero for failure.
+ * Call with d->arch.paging.lock held.
+ */
+int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
+{
+    struct page_info *pg;
+
+    ASSERT(spin_is_locked(&d->arch.paging.lock));
+
+    for ( ; ; )
+    {
+        if ( d->arch.paging.p2m_total_pages < pages )
+        {
+            /* Need to allocate more memory from domheap */
+            pg = alloc_domheap_page(d, MEMF_no_owner);
+            if ( pg == NULL )
+            {
+                printk(XENLOG_ERR "Failed to allocate P2M pages.\n");
+                return -ENOMEM;
+            }
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)++;
+            page_list_add_tail(pg, &d->arch.paging.p2m_freelist);
+        }
+        else if ( d->arch.paging.p2m_total_pages > pages )
+        {
+            /* Need to return memory to domheap */
+            pg = page_list_remove_head(&d->arch.paging.p2m_freelist);
+            if( pg )
+            {
+                ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+                free_domheap_page(pg);
+            }
+            else
+            {
+                printk(XENLOG_ERR
+                       "Failed to free P2M pages, P2M freelist is empty.\n");
+                return -ENOMEM;
+            }
+        }
+        else
+            break;
+

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 174

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        /* Check to see if we need to yield and try again */
+        if ( preempted && general_preempt_check() )
+        {
+            *preempted = true;
+            return -ERESTART;
+        }
+    }
+
+    return 0;
+}

Btw, with the order-2 requirement for the root page table, you may want to consider an alternative approach: Here you could allocate some order-2

Quoted 8 lines

pages (possibly up to as many as a domain might need, which right now would be exactly one), put them on a separate list, and consume the root table(s) from there. If you run out of pages on the order-0 list, you could shatter a page from the order-2 one (as long as that's still non- empty). The difficulty would be with freeing, where a previously shattered order-2 page would be nice to re-combine once all of its constituents are free again. The main benefit would be avoiding the back and forth in patch 6.

It is an option.

Quoted 7 lines

But I'm still not 100% sure it's necessary to allocate the root page table from the freelist. We could simply allocate the root page table from the domheap (as is done for hardware domains) and reserve the freelist for other pages.
The freelist is specific to Dom0less guest domains and is primarily used to limit the amount of memory available for the guest—essentially for static configurations where you want a clear and fixed limit on p2m allocations.

Is that true? My understanding is that this pre-populated pool is used by all DomU-s, whether or not under dom0less.

Plus we're meaning to move towards better accounting of memory used by a domain (besides its actual allocation). Allocating the root table from the domain heap would move us one small step farther away from there.

Jan

#64 lore raw

Re: [PATCH v2 08/17] xen/riscv: add new p2m types and helper macros for type classification

In reply to Oleksii Kurochko
Quoted 29 lines · Oleksii Kurochko
Quoted 27 lines · Jan Beulich
Quoted 26 lines · Oleksii Kurochko
Quoted 25 lines · Jan Beulich
Quoted 14 lines · Oleksii Kurochko
Quoted 13 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 64

Quoted 7 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/p2m.h +1 -0
@@ -61,8 +61,28 @@ struct p2m_domain {
    typedef enum {
        p2m_invalid = 0,    /* Nothing mapped here */
        p2m_ram_rw,         /* Normal read/write domain RAM */
+    p2m_ram_ro,         /* Read-only; writes are silently dropped */

As indicated before - this type should be added when the special handling that it requires is also introduced.

Perhaps, I missed that. I will drop this type for now.

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

xen/arch/riscv/include/asm/p2m.h struct p2m_domain { · line 65

Quoted 1 line
xen/arch/riscv/include/asm/p2m.h +1 -0
+    p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area */

What's the _dev suffix indicating here?

It indicates that it is device memory, probably, it isn't so necessary in case of RISC-V as spec doesn't use such terminology. In RISC-V there is only available IO, NC. And we are |using PTE_PBMT_IO for |p2m_mmio_direct_dev.

Maybe it would be better just to rename s/p2m_mmio_direct_dev/p2m_mmio_direct_io.

I forgot that p2m_mmio_direct_dev is used by common code for dom0less code (handle_passthrough_prop())

That'll want abstracting out, I think. I don't view it as helpful to clutter RISC-V (and later perhaps also PPC) with Arm-specific terminology.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Would it be better then just rename it to p2m_device? Then it won't clear for Arm which type of MMIO p2m's types is used as Arm has there MMIO types: *_dev, *_nc, *_c.

I don't understand why Arm matters here. P2M types want naming in a way that makes sense for RISC-V.

It doesn't matter.
But if we want to change the type name from p2m_mmio_direct_dev to p2m_mmio_direct or p2m_device then it will affect Arm too as p2m_mmio_direct_dev is used in dom0less code which is also used by Arm.

As said - imo this needs abstracting away.

Quoted 9 lines

I just re-used p2m_mmio_direct_dev as it looked for me pretty generic and clear for what this type is.

Quoted 4 lines
Quoted 2 lines

As an option (which I don't really like) it could be "#define p2m_mmio_direct_dev ARCH_specific_name" in asm/p2m.h to not touch common code.

A #define may be needed, but not one to _still_ introduce Arm naming into non-Arm code.

As I mentioned above that p2m_mmio_direct_dev sounds pretty generic to me and I am okay to use it for RISC-V. But if you have better suggestions I will be happy to consider it.

Well, the name we use on x86 (and I think this was quite obviously implied by earlier replies of mine): p2m_mmio_direct.

Jan

#34 lore raw

Re: [PATCH v2 04/17] xen/riscv: construct the P2M pages pool for guests

In reply to Jan Beulich
Quoted 81 lines · Jan Beulich
Quoted 73 lines · Oleksii Kurochko
Quoted 62 lines · Jan Beulich
Quoted 49 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +45 -0
@@ -113,3 +117,58 @@ int p2m_init(struct domain *d)
 
       return 0;
   }
+
+/*
+ * Set the pool of pages to the required number of pages.
+ * Returns 0 for success, non-zero for failure.
+ * Call with d->arch.paging.lock held.
+ */
+int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
+{
+    struct page_info *pg;
+
+    ASSERT(spin_is_locked(&d->arch.paging.lock));
+
+    for ( ; ; )
+    {
+        if ( d->arch.paging.p2m_total_pages < pages )
+        {
+            /* Need to allocate more memory from domheap */
+            pg = alloc_domheap_page(d, MEMF_no_owner);
+            if ( pg == NULL )
+            {
+                printk(XENLOG_ERR "Failed to allocate P2M pages.\n");
+                return -ENOMEM;
+            }
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)++;
+            page_list_add_tail(pg, &d->arch.paging.p2m_freelist);
+        }
+        else if ( d->arch.paging.p2m_total_pages > pages )
+        {
+            /* Need to return memory to domheap */
+            pg = page_list_remove_head(&d->arch.paging.p2m_freelist);
+            if( pg )
+            {
+                ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+                free_domheap_page(pg);
+            }
+            else
+            {
+                printk(XENLOG_ERR
+                       "Failed to free P2M pages, P2M freelist is empty.\n");
+                return -ENOMEM;
+            }
+        }
+        else
+            break;
+

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 174

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        /* Check to see if we need to yield and try again */
+        if ( preempted && general_preempt_check() )
+        {
+            *preempted = true;
+            return -ERESTART;
+        }
+    }
+
+    return 0;
+}

Btw, with the order-2 requirement for the root page table, you may want to consider an alternative approach: Here you could allocate some order-2

Quoted 8 lines

pages (possibly up to as many as a domain might need, which right now would be exactly one), put them on a separate list, and consume the root table(s) from there. If you run out of pages on the order-0 list, you could shatter a page from the order-2 one (as long as that's still non- empty). The difficulty would be with freeing, where a previously shattered order-2 page would be nice to re-combine once all of its constituents are free again. The main benefit would be avoiding the back and forth in patch 6.

It is an option.

Quoted 7 lines

But I'm still not 100% sure it's necessary to allocate the root page table from the freelist. We could simply allocate the root page table from the domheap (as is done for hardware domains) and reserve the freelist for other pages.
The freelist is specific to Dom0less guest domains and is primarily used to limit the amount of memory available for the guest—essentially for static configurations where you want a clear and fixed limit on p2m allocations.

Quoted 2 lines

Is that true? My understanding is that this pre-populated pool is used by all DomU-s, whether or not under dom0less.

I think you are right, I just automatically decided so as this pre-populated pool is set now only in dom0less.

~ Oleksii

Quoted 6 lines

Plus we're meaning to move towards better accounting of memory used by a domain (besides its actual allocation). Allocating the root table from the domain heap would move us one small step farther away from there.

Jan

#158 lore raw

Re: [PATCH v2 17/17] xen/riscv: add support of page lookup by GFN

In reply to Oleksii Kurochko
Quoted 10 lines · Oleksii Kurochko

Introduce helper functions for safely querying the P2M (physical-to-machine) mapping:
- add p2m_read_lock(), p2m_read_unlock(), and p2m_is_locked() for managing P2M lock state.
- Implement p2m_get_entry() to retrieve mapping details for a given GFN, including MFN, page order, and validity.
- Add p2m_lookup() to encapsulate read-locked MFN retrieval.
- Introduce p2m_get_page_from_gfn() to convert a GFN into a page_info pointer, acquiring a reference to the page if valid.

Quoted 6 lines

Implementations are based on Arm's functions with some minor modifications:
- p2m_get_entry():
- Reverse traversal of page tables, as RISC-V uses the opposite order compared to Arm.
- Removed the return of p2m_access_t from p2m_get_entry() since mem_access_settings is not introduced for RISC-V.

Didn't I see uses of p2m_access in earlier patches? If you don't mean to have that, then please consistently {every,no}where.

Quoted 9 lines

- Updated BUILD_BUG_ON() to check using the level 0 mask, which corresponds to Arm's THIRD_MASK.
- Replaced open-coded bit shifts with the BIT() macro.
- Other minor changes, such as using RISC-V-specific functions to validate P2M PTEs, and replacing Arm-specific GUEST_* macros with their RISC-V equivalents.
- p2m_get_page_from_gfn():
- Removed p2m_is_foreign() and related logic, as this functionality is not implemented for RISC-V.

Yet I expect you'll need this, sooner or later.

Quoted 13 lines
xen/arch/riscv/include/asm/p2m.h +7 -0
@@ -184,6 +184,24 @@ static inline int p2m_is_write_locked(struct p2m_domain *p2m)
     return rw_is_write_locked(&p2m->lock);
 }
 
+static inline void p2m_read_lock(struct p2m_domain *p2m)
+{
+    read_lock(&p2m->lock);
+}
+
+static inline void p2m_read_unlock(struct p2m_domain *p2m)
+{

xen/arch/riscv/include/asm/p2m.h static inline int p2m_is_write_locked(struct p2m_domain *p2m) · line 203

Quoted 10 lines
xen/arch/riscv/include/asm/p2m.h +10 -0
+    read_unlock(&p2m->lock);
+}
+
+static inline int p2m_is_locked(struct p2m_domain *p2m)
+{
+    return rw_is_locked(&p2m->lock);
+}
+
+struct page_info *p2m_get_page_from_gfn(struct domain *d, gfn_t gfn,
+                                        p2m_type_t *t);

Once again I don't think you can pass struct domain * here, when in the long run a domain can have multiple P2Ms.

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1063

Quoted 12 lines
xen/arch/riscv/p2m.c +6 -0
@@ -1055,3 +1055,134 @@ int guest_physmap_add_entry(struct domain *d,
 {
     return p2m_insert_mapping(d, gfn, (1 << page_order), mfn, t);
 }
+
+/*
+ * Get the details of a given gfn.
+ *
+ * If the entry is present, the associated MFN will be returned and the
+ * access and type filled up. The page_order will correspond to the

You removed p2m_access_t * from the parameters; you need to also update the comment then accordingly.

Quoted 9 lines
xen/arch/riscv/p2m.c +9 -0
+ * order of the mapping in the page table (i.e it could be a superpage).
+ *
+ * If the entry is not present, INVALID_MFN will be returned and the
+ * page_order will be set according to the order of the invalid range.
+ *
+ * valid will contain the value of bit[0] (e.g valid bit) of the
+ * entry.
+ */
+static mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn,

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1082

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+                           p2m_type_t *t,
+                           unsigned int *page_order,
+                           bool *valid)
+{
+    paddr_t addr = gfn_to_gaddr(gfn);
+    unsigned int level = 0;
+    pte_t entry, *table;
+    int rc;
+    mfn_t mfn = INVALID_MFN;
+    p2m_type_t _t;

Please no local variables with leading underscores. In x86 we commonly name such variables p2mt.

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1083

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+    DECLARE_OFFSETS(offsets, addr);

This is the sole use of "addr". Is such a local variable really worth having?

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+    ASSERT(p2m_is_locked(p2m));
+    BUILD_BUG_ON(XEN_PT_LEVEL_MAP_MASK(0) != PAGE_MASK);
+
+    /* Allow t to be NULL */
+    t = t ?: &_t;
+
+    *t = p2m_invalid;

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1101

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+
+    if ( valid )
+        *valid = false;
+
+    /* XXX: Check if the mapping is lower than the mapped gfn */
+
+    /* This gfn is higher than the highest the p2m map currently holds */
+    if ( gfn_x(gfn) > gfn_x(p2m->max_mapped_gfn) )
+    {
+        for ( level = P2M_ROOT_LEVEL; level ; level-- )

Nit: Stray blank before the 2nd semicolon. (Again at least once below.)

Quoted 14 lines
xen/arch/riscv/p2m.c +14 -0
+            if ( (gfn_x(gfn) & (XEN_PT_LEVEL_MASK(level) >> PAGE_SHIFT)) >
+                 gfn_x(p2m->max_mapped_gfn) )
+                break;
+
+        goto out;
+    }
+
+    table = p2m_get_root_pointer(p2m, gfn);
+
+    /*
+     * the table should always be non-NULL because the gfn is below
+     * p2m->max_mapped_gfn and the root table pages are always present.
+     */
+    if ( !table )

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1125

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+    {
+        ASSERT_UNREACHABLE();
+        level = P2M_ROOT_LEVEL;
+        goto out;
+    }
+
+    for ( level = P2M_ROOT_LEVEL; level ; level-- )
+    {
+        rc = p2m_next_level(p2m, true, level, &table, offsets[level]);
+        if ( (rc == GUEST_TABLE_MAP_NONE) && (rc != GUEST_TABLE_MAP_NOMEM) )

This condition looks odd. As written the rhs of the && is redundant.

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1127

Quoted 2 lines
xen/arch/riscv/p2m.c +2 -0
+            goto out_unmap;
+        else if ( rc != GUEST_TABLE_NORMAL )

As before, no real need for "else" in such cases.

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1135

Quoted 8 lines
xen/arch/riscv/p2m.c +8 -0
+            break;
+    }
+
+    entry = table[offsets[level]];
+
+    if ( p2me_is_valid(p2m, entry) )
+    {
+        *t = p2m_type_radix_get(p2m, entry);

If the incoming argument is NULL, the somewhat expensive radix tree lookup is unnecessary here.

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1146

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        mfn = pte_get_mfn(entry);
+        /*
+         * The entry may point to a superpage. Find the MFN associated
+         * to the GFN.
+         */
+        mfn = mfn_add(mfn,
+                      gfn_x(gfn) & (BIT(XEN_PT_LEVEL_ORDER(level), UL) - 1));
+
+        if ( valid )
+            *valid = pte_is_valid(entry);

Interesting. Why not the P2M counterpart of the function? Yes, the comment ahead of the function says so, but I don't see why the valid bit suddenly is relevant here (besides the P2M type).

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1152

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+    }
+
+out_unmap:
+    unmap_domain_page(table);
+
+out:

Nit: Style (bot labels).

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1159

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+    if ( page_order )
+        *page_order = XEN_PT_LEVEL_ORDER(level);
+
+    return mfn;
+}
+
+static mfn_t p2m_lookup(struct domain *d, gfn_t gfn, p2m_type_t *t)

pointer-to-const for the 1st arg? But again more likely struct p2m_domain * anyway?

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1171

Quoted 12 lines
xen/arch/riscv/p2m.c +12 -0
+{
+    mfn_t mfn;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    p2m_read_lock(p2m);
+    mfn = p2m_get_entry(p2m, gfn, t, NULL, NULL);
+    p2m_read_unlock(p2m);
+
+    return mfn;
+}
+
+struct page_info *p2m_get_page_from_gfn(struct domain *d, gfn_t gfn,

Same here - likely you mean struct p2m_domain * instead.

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1174

Quoted 3 lines
xen/arch/riscv/p2m.c +3 -0
+                                        p2m_type_t *t)
+{
+    p2m_type_t p2mt = {0};

Why a compound initializer for something that isn't a compound object? And why plain 0 for something that is an enumerated type?

xen/arch/riscv/p2m.c int guest_physmap_add_entry(struct domain *d, · line 1180

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+    struct page_info *page;
+
+    mfn_t mfn = p2m_lookup(d, gfn, &p2mt);
+
+    if ( t )
+        *t = p2mt;

What's wrong with passing t directly to p2m_lookup()?

Jan

#35 lore raw

Re: [PATCH v2 04/17] xen/riscv: construct the P2M pages pool for guests

In reply to Jan Beulich
Quoted 61 lines · Jan Beulich
Quoted 49 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +45 -0
@@ -113,3 +117,58 @@ int p2m_init(struct domain *d)
 
      return 0;
  }
+
+/*
+ * Set the pool of pages to the required number of pages.
+ * Returns 0 for success, non-zero for failure.
+ * Call with d->arch.paging.lock held.
+ */
+int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
+{
+    struct page_info *pg;
+
+    ASSERT(spin_is_locked(&d->arch.paging.lock));
+
+    for ( ; ; )
+    {
+        if ( d->arch.paging.p2m_total_pages < pages )
+        {
+            /* Need to allocate more memory from domheap */
+            pg = alloc_domheap_page(d, MEMF_no_owner);
+            if ( pg == NULL )
+            {
+                printk(XENLOG_ERR "Failed to allocate P2M pages.\n");
+                return -ENOMEM;
+            }
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)++;
+            page_list_add_tail(pg, &d->arch.paging.p2m_freelist);
+        }
+        else if ( d->arch.paging.p2m_total_pages > pages )
+        {
+            /* Need to return memory to domheap */
+            pg = page_list_remove_head(&d->arch.paging.p2m_freelist);
+            if( pg )
+            {
+                ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+                free_domheap_page(pg);
+            }
+            else
+            {
+                printk(XENLOG_ERR
+                       "Failed to free P2M pages, P2M freelist is empty.\n");
+                return -ENOMEM;
+            }
+        }
+        else
+            break;
+

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 174

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        /* Check to see if we need to yield and try again */
+        if ( preempted && general_preempt_check() )
+        {
+            *preempted = true;
+            return -ERESTART;
+        }
+    }
+
+    return 0;
+}

Btw, with the order-2 requirement for the root page table, you may want to

Quoted 8 lines

consider an alternative approach: Here you could allocate some order-2 pages (possibly up to as many as a domain might need, which right now would be exactly one), put them on a separate list, and consume the root table(s) from there. If you run out of pages on the order-0 list, you could shatter a page from the order-2 one (as long as that's still non- empty). The difficulty would be with freeing, where a previously shattered order-2 page would be nice to re-combine once all of its constituents are free again.

Do we really need to re-combine shattered order-2 pages?
It seems like the only usage for this order-2-list is to have 1 order-2 page for root page table. All other pages are 4k pages so even if we won't re-combine them, nothing serious will happen.

And if we aren't going to have more usages of order-2 pages list then do we really need a separate order-2 list just basically for root page table?

...

Quoted 2 lines

The main benefit would be avoiding the back and forth in patch
6.

...
Can’t we just avoid putting the pages (which will get back) for the root page table into the freelist at all? That way, there would be no need to return them later—something like:

Something like:
int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)

{
    struct page_info *pg;

    ASSERT(spin_is_locked(&d->arch.paging.lock));

    pages -= root_page_table_num;
    
    for ( ; ; )
    {
        if ( d->arch.paging.p2m_total_pages < pages )
        {

...
}

~ Oleksii

#36 lore raw

Re: [PATCH v2 04/17] xen/riscv: construct the P2M pages pool for guests

Quoted 71 lines · Oleksii Kurochko
Quoted 61 lines · Jan Beulich
Quoted 49 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +45 -0
@@ -113,3 +117,58 @@ int p2m_init(struct domain *d)
 
      return 0;
  }
+
+/*
+ * Set the pool of pages to the required number of pages.
+ * Returns 0 for success, non-zero for failure.
+ * Call with d->arch.paging.lock held.
+ */
+int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
+{
+    struct page_info *pg;
+
+    ASSERT(spin_is_locked(&d->arch.paging.lock));
+
+    for ( ; ; )
+    {
+        if ( d->arch.paging.p2m_total_pages < pages )
+        {
+            /* Need to allocate more memory from domheap */
+            pg = alloc_domheap_page(d, MEMF_no_owner);
+            if ( pg == NULL )
+            {
+                printk(XENLOG_ERR "Failed to allocate P2M pages.\n");
+                return -ENOMEM;
+            }
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)++;
+            page_list_add_tail(pg, &d->arch.paging.p2m_freelist);
+        }
+        else if ( d->arch.paging.p2m_total_pages > pages )
+        {
+            /* Need to return memory to domheap */
+            pg = page_list_remove_head(&d->arch.paging.p2m_freelist);
+            if( pg )
+            {
+                ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+                free_domheap_page(pg);
+            }
+            else
+            {
+                printk(XENLOG_ERR
+                       "Failed to free P2M pages, P2M freelist is empty.\n");
+                return -ENOMEM;
+            }
+        }
+        else
+            break;
+

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 174

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        /* Check to see if we need to yield and try again */
+        if ( preempted && general_preempt_check() )
+        {
+            *preempted = true;
+            return -ERESTART;
+        }
+    }
+
+    return 0;
+}

Btw, with the order-2 requirement for the root page table, you may want to

Quoted 8 lines

consider an alternative approach: Here you could allocate some order-2 pages (possibly up to as many as a domain might need, which right now would be exactly one), put them on a separate list, and consume the root table(s) from there. If you run out of pages on the order-0 list, you could shatter a page from the order-2 one (as long as that's still non- empty). The difficulty would be with freeing, where a previously shattered order-2 page would be nice to re-combine once all of its constituents are free again.

Quoted 4 lines

Do we really need to re-combine shattered order-2 pages?
It seems like the only usage for this order-2-list is to have 1 order-2 page for root page table. All other pages are 4k pages so even if we won't re-combine them, nothing serious will happen.

That's true as long as you have only the host-P2M for each domain. Once you have alternative or nested ones, things may change (unless they all have their roots also set up right during domain creation, which would seem wasteful to me).

Quoted 6 lines
Quoted 2 lines

The main benefit would be avoiding the back and forth in patch
6.

...
Can’t we just avoid putting the pages (which will get back) for the root page table into the freelist at all?

Again, this may be fine as long as there's only the host-P2M. That sole root won't ever be freed anyway during the lifetime of a domain.

Jan

#37 lore raw

Re: [PATCH v2 04/17] xen/riscv: construct the P2M pages pool for guests

Quoted 71 lines · Jan Beulich
Quoted 70 lines · Oleksii Kurochko
Quoted 69 lines · Jan Beulich
Quoted 49 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +45 -0
@@ -113,3 +117,58 @@ int p2m_init(struct domain *d)
 
       return 0;
   }
+
+/*
+ * Set the pool of pages to the required number of pages.
+ * Returns 0 for success, non-zero for failure.
+ * Call with d->arch.paging.lock held.
+ */
+int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
+{
+    struct page_info *pg;
+
+    ASSERT(spin_is_locked(&d->arch.paging.lock));
+
+    for ( ; ; )
+    {
+        if ( d->arch.paging.p2m_total_pages < pages )
+        {
+            /* Need to allocate more memory from domheap */
+            pg = alloc_domheap_page(d, MEMF_no_owner);
+            if ( pg == NULL )
+            {
+                printk(XENLOG_ERR "Failed to allocate P2M pages.\n");
+                return -ENOMEM;
+            }
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)++;
+            page_list_add_tail(pg, &d->arch.paging.p2m_freelist);
+        }
+        else if ( d->arch.paging.p2m_total_pages > pages )
+        {
+            /* Need to return memory to domheap */
+            pg = page_list_remove_head(&d->arch.paging.p2m_freelist);
+            if( pg )
+            {
+                ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+                free_domheap_page(pg);
+            }
+            else
+            {
+                printk(XENLOG_ERR
+                       "Failed to free P2M pages, P2M freelist is empty.\n");
+                return -ENOMEM;
+            }
+        }
+        else
+            break;
+

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 174

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        /* Check to see if we need to yield and try again */
+        if ( preempted && general_preempt_check() )
+        {
+            *preempted = true;
+            return -ERESTART;
+        }
+    }
+
+    return 0;
+}

Btw, with the order-2 requirement for the root page table, you may want to consider an alternative approach: Here you could allocate some order-2 pages (possibly up to as many as a domain might need, which right now would be exactly one), put them on a separate list, and consume the root table(s) from there. If you run out of pages on the order-0 list, you could shatter a page from the order-2 one (as long as that's still non- empty). The difficulty would be with freeing, where a previously shattered order-2 page would be nice to re-combine once all of its constituents are free again.

Quoted 8 lines
Quoted 4 lines

Do we really need to re-combine shattered order-2 pages?
It seems like the only usage for this order-2-list is to have 1 order-2 page for root page table. All other pages are 4k pages so even if we won't re-combine them, nothing serious will happen.

That's true as long as you have only the host-P2M for each domain. Once you have alternative or nested ones, things may change (unless they all have their roots also set up right during domain creation, which would seem wasteful to me).

I don't know how it is implemented on x86, but I thought that if it is needed alternative or nested P2Ms then it is needed to provide separated from host-P2M page tables (root page table including).

~ Oleksii

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

The main benefit would be avoiding the back and forth in patch
6.

...
Can’t we just avoid putting the pages (which will get back) for the root page table into the freelist at all?

Again, this may be fine as long as there's only the host-P2M. That sole root won't ever be freed anyway during the lifetime of a domain.

Jan

#38 lore raw

Re: [PATCH v2 04/17] xen/riscv: construct the P2M pages pool for guests

Quoted 77 lines · Oleksii Kurochko
Quoted 75 lines · Jan Beulich
Quoted 74 lines · Oleksii Kurochko
Quoted 61 lines · Jan Beulich
Quoted 49 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +45 -0
@@ -113,3 +117,58 @@ int p2m_init(struct domain *d)
 
       return 0;
   }
+
+/*
+ * Set the pool of pages to the required number of pages.
+ * Returns 0 for success, non-zero for failure.
+ * Call with d->arch.paging.lock held.
+ */
+int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
+{
+    struct page_info *pg;
+
+    ASSERT(spin_is_locked(&d->arch.paging.lock));
+
+    for ( ; ; )
+    {
+        if ( d->arch.paging.p2m_total_pages < pages )
+        {
+            /* Need to allocate more memory from domheap */
+            pg = alloc_domheap_page(d, MEMF_no_owner);
+            if ( pg == NULL )
+            {
+                printk(XENLOG_ERR "Failed to allocate P2M pages.\n");
+                return -ENOMEM;
+            }
+            ACCESS_ONCE(d->arch.paging.p2m_total_pages)++;
+            page_list_add_tail(pg, &d->arch.paging.p2m_freelist);
+        }
+        else if ( d->arch.paging.p2m_total_pages > pages )
+        {
+            /* Need to return memory to domheap */
+            pg = page_list_remove_head(&d->arch.paging.p2m_freelist);
+            if( pg )
+            {
+                ACCESS_ONCE(d->arch.paging.p2m_total_pages)--;
+                free_domheap_page(pg);
+            }
+            else
+            {
+                printk(XENLOG_ERR
+                       "Failed to free P2M pages, P2M freelist is empty.\n");
+                return -ENOMEM;
+            }
+        }
+        else
+            break;
+

xen/arch/riscv/p2m.c int p2m_init(struct domain *d) · line 174

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        /* Check to see if we need to yield and try again */
+        if ( preempted && general_preempt_check() )
+        {
+            *preempted = true;
+            return -ERESTART;
+        }
+    }
+
+    return 0;
+}

Btw, with the order-2 requirement for the root page table, you may want to

Quoted 8 lines

consider an alternative approach: Here you could allocate some order-2 pages (possibly up to as many as a domain might need, which right now would be exactly one), put them on a separate list, and consume the root table(s) from there. If you run out of pages on the order-0 list, you could shatter a page from the order-2 one (as long as that's still non- empty). The difficulty would be with freeing, where a previously shattered order-2 page would be nice to re-combine once all of its constituents are free again.

Do we really need to re-combine shattered order-2 pages?
It seems like the only usage for this order-2-list is to have 1 order-2 page for root page table. All other pages are 4k pages so even if we won't re-combine them, nothing serious will happen.

Quoted 8 lines
Quoted 4 lines

That's true as long as you have only the host-P2M for each domain. Once you have alternative or nested ones, things may change (unless they all have their roots also set up right during domain creation, which would seem wasteful to me).

I don't know how it is implemented on x86, but I thought that if it is needed alternative or nested P2Ms then it is needed to provide separated from host-P2M page tables (root page table including).

Correct, hence why you will then need to allocate multiple root tables. Those secondary page tables are nevertheless all allocated from the single pool that a domain has.

Jan

#152 lore raw

Re: [PATCH v2 16/17] xen/riscv: implement mfn_valid() and page reference, ownership handling helpers

In reply to Jan Beulich
Quoted 90 lines · Jan Beulich
Quoted 26 lines · Oleksii Kurochko

Implement the mfn_valid() macro to verify whether a given MFN is valid by checking that it falls within the range [start_page, max_page). These bounds are initialized based on the start and end addresses of RAM.

As part of this patch, start_page is introduced and initialized with the PFN of the first RAM page.

Also, after providing a non-stub implementation of the mfn_valid() macro,
the following compilation errors started to occur:
riscv64-linux-gnu-ld: prelink.o: in function `__next_node':
/build/xen/./include/xen/nodemask.h:202: undefined reference to `page_is_ram_type'
riscv64-linux-gnu-ld: prelink.o: in function `get_free_buddy':
/build/xen/common/page_alloc.c:881: undefined reference to `page_is_ram_type'
riscv64-linux-gnu-ld: prelink.o: in function `alloc_heap_pages':
/build/xen/common/page_alloc.c:1043: undefined reference to `page_get_owner_and_reference'
riscv64-linux-gnu-ld: /build/xen/common/page_alloc.c:1098: undefined reference to `page_is_ram_type'
riscv64-linux-gnu-ld: prelink.o: in function `ns16550_interrupt':
/build/xen/drivers/char/ns16550.c:205: undefined reference to `get_page'
riscv64-linux-gnu-ld: ./.xen-syms.0: hidden symbol `page_get_owner_and_reference' isn't defined
riscv64-linux-gnu-ld: final link failed: bad value
make[2]: *** [arch/riscv/Makefile:35: xen-syms] Error 1
To resolve these errors, the following functions have also been introduced,
based on their Arm counterparts:
- page_get_owner_and_reference() and its variant to safely acquire a
reference to a page and retrieve its owner.
- put_page() and put_page_nr() to release page references and free the page

Quoted 8 lines

when the count drops to zero.
For put_page_nr(), code related to static memory configuration is wrapped with CONFIG_STATIC_MEMORY, as this configuration has not yet been moved to common code. Therefore, PGC_static and free_domstatic_page() are not introduced for RISC-V. However, since this configuration could be useful in the future, the relevant code is retained and conditionally compiled.
- A stub for page_is_ram_type() that currently always returns 0 and asserts unreachable, as RAM type checking is not yet implemented.

How does this end up working when common code references the function?

xen/arch/riscv/include/asm/mm.h static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr) · line 297

Quoted 12 lines
xen/arch/riscv/include/asm/mm.h +6 -2
@@ -288,8 +289,12 @@ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)
 #define page_get_owner(p)    (p)->v.inuse.domain
 #define page_set_owner(p, d) ((p)->v.inuse.domain = (d))
 
-/* TODO: implement */
-#define mfn_valid(mfn) ({ (void)(mfn); 0; })
+extern unsigned long start_page;
+
+#define mfn_valid(mfn) ({                                   \
+    unsigned long mfn__ = mfn_x(mfn);                       \
+    likely((mfn__ >= start_page) && (mfn__ < max_page));    \
+})

I don't think you should try to be clever and avoid using __mfn_valid() here, at least not without an easily identifiable TODO. Surely you've seen that both Arm and x86 use it.

Also, according to all I know, likely() doesn't work very well when used like this, except for architectures supporting conditionally executed insns (like Arm32 or IA-64, i.e. beyond conditional branches). I.e. if you want to use likely() here, I think you need two of them.

xen/arch/riscv/mm.c static void __init setup_directmap_mappings(unsigned long base_mfn, · line 523

Quoted 5 lines
xen/arch/riscv/mm.c +1 -0
@@ -525,6 +520,8 @@ static void __init setup_directmap_mappings(unsigned long base_mfn,
 #error setup_{directmap,frametable}_mapping() should be implemented for RV_32
 #endif
 
+unsigned long __read_mostly start_page;

Memory hotplug question again: __read_mostly or __ro_after_init?

Quoted 22 lines
xen/arch/riscv/mm.c +18 -0
@@ -613,3 +612,91 @@ void __iomem *ioremap(paddr_t pa, size_t len)
 {
     return ioremap_attr(pa, len, PAGE_HYPERVISOR_NOCACHE);
 }
+
+int page_is_ram_type(unsigned long mfn, unsigned long mem_type)
+{
+    ASSERT_UNREACHABLE();
+
+    return 0;
+}
+
+static struct domain *page_get_owner_and_nr_reference(struct page_info *page,
+                                                      unsigned long nr)
+{
+    unsigned long x, y = page->count_info;
+    struct domain *owner;
+
+    /* Restrict nr to avoid "double" overflow */
+    if ( nr >= PGC_count_mask )
+    {
+        ASSERT_UNREACHABLE();
Quoted 8 lines

Review void __iomem *ioremap(paddr_t pa, size_t len) · line 634

Quoted 2 lines
(quoted diff) +2 -0
+        return NULL;
+    }

I question the validity of this, already in the Arm original: I can't spot how the caller guarantees to stay below that limit. Without such an (attempted) guarantee, ASSERT_UNREACHABLE() is wrong to use. All I can see is process_shm_node() incrementing shmem_extra[].nr_shm_borrowers, without any limit check.

Honestly I don't know why this assert was placed here. I checked the code and we don't limit nr_shm_borrowers in any place, so in theory it's possible to end up here.

~Michal

Quoted 52 lines

xen/arch/riscv/mm.c void __iomem *ioremap(paddr_t pa, size_t len) · line 641

Quoted 7 lines
xen/arch/riscv/mm.c +6 -0
+    do {
+        x = y;
+        /*
+         * Count ==  0: Page is not allocated, so we cannot take a reference.
+         * Count == -1: Reference count would wrap, which is invalid.
+         */

May I once again ask that you look carefully at comments (as much as at code) you copy. Clearly this comment wasn't properly updated when the bumping by 1 was changed to bumping by nr.

Quoted 24 lines
xen/arch/riscv/mm.c +24 -0
+        if ( unlikely(((x + nr) & PGC_count_mask) <= nr) )
+            return NULL;
+    }
+    while ( (y = cmpxchg(&page->count_info, x, x + nr)) != x );
+
+    owner = page_get_owner(page);
+    ASSERT(owner);
+
+    return owner;
+}
+
+struct domain *page_get_owner_and_reference(struct page_info *page)
+{
+    return page_get_owner_and_nr_reference(page, 1);
+}
+
+void put_page_nr(struct page_info *page, unsigned long nr)
+{
+    unsigned long nx, x, y = page->count_info;
+
+    do {
+        ASSERT((y & PGC_count_mask) >= nr);
+        x  = y;
+        nx = x - nr;

xen/arch/riscv/mm.c void __iomem *ioremap(paddr_t pa, size_t len) · line 675

Quoted 10 lines
xen/arch/riscv/mm.c +10 -0
+    }
+    while ( unlikely((y = cmpxchg(&page->count_info, x, nx)) != x) );
+
+    if ( unlikely((nx & PGC_count_mask) == 0) )
+    {
+#ifdef CONFIG_STATIC_MEMORY
+        if ( unlikely(nx & PGC_static) )
+            free_domstatic_page(page);
+        else
+#endif

Such #ifdef-ed-out code is liable to go stale. Minimally use IS_ENABLED(). Even better would imo be if you introduced a "stub" PGC_static, resolving to 0 (i.e. for now unconditionally).

Jan

#70 lore raw

Re: [PATCH v2 09/17] xen/riscv: introduce page_set_xenheap_gfn()

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko

Introduce page_set_xenheap_gfn() helper to encode the GFN associated with a Xen heap page directly into the type_info field of struct page_info.

Introduce a GFN field in the type_info of a Xen heap page by reserving 10 bits (sufficient for both Sv32 and Sv39+ modes), and define PGT_gfn_mask and PGT_gfn_width accordingly.

This reads as if you wanted to encode the GFN in 10 bits.

I will reword it to:
Reserve 10 MSB bits to store the usage counter and frame type;
use all remaining bits to store the grant table frame GFN.
It will be enough as Sv32 uses 22-bit GFNs and Sv{39, 47, 58} uses 44-bit GFNs.

Quoted 4 lines

What would also help is if you said why you actually need this. x86, after all, gets away without anything like this. (But I understand you're more Arm-like here.)

I think with the rewording mentioned above it will be clear that it is needed for grant tables. But I also can add the following:
The grant table frame GFN will be stored directly in|struct page_info| instead of being maintained in separate status/shared arrays. To avoid increasing the size of|struct page_info|, the necessary bits are borrowed from the
|||type_info of struct page_info.|

Quoted 19 lines
Quoted 19 lines
xen/arch/riscv/include/asm/mm.h +12 -3
@@ -229,9 +230,21 @@ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)
  #define PGT_writable_page PG_mask(1, 1)  /* has writable mappings?         */
  #define PGT_type_mask     PG_mask(1, 1)  /* Bits 31 or 63.                 */
 
-/* Count of uses of this frame as its current type. */
-#define PGT_count_width   PG_shift(2)
-#define PGT_count_mask    ((1UL << PGT_count_width) - 1)
+ /* 9-bit count of uses of this frame as its current type. */
+#define PGT_count_mask    PG_mask(0x3FF, 10)
+
+/*
+ * Sv32 has 22-bit GFN. Sv{39, 48, 57} have 44-bit GFN.
+ * Thereby we can use for `type_info` 10 bits for all modes, having the same
+ * amount of bits for `type_info` for all MMU modes let us avoid introducing
+ * an extra #ifdef to that header:
+ *   if we go with maximum possible bits for count on each configuration
+ *   we would need to have a set of PGT_count_* and PGT_gfn_*).
+ */
+#define PGT_gfn_width     PG_shift(10)
Quoted 8 lines

xen/arch/riscv/include/asm/mm.h static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr) · line 247

Quoted 3 lines
xen/arch/riscv/include/asm/mm.h +3 -0
+#define PGT_gfn_mask      (BIT(PGT_gfn_width, UL) - 1)
+
+#define PGT_INVALID_XENHEAP_GFN   _gfn(PGT_gfn_mask)

Commentary here would imo be preferable to be much closer to Arm's. I don't see the point of the extra verbosity (part of which may be fine to have in the description, except you already say something along these lines there). While in turn the comment talks of fewer bits than are actually being used in the RV64 case.

Sure, I will replace this comment with:
/*
* Stored in bits [22:0] (Sv32) or [44:0] (Sv39,48,57) GFN if page is xenheap page. */

Quoted 19 lines
Quoted 19 lines
xen/arch/riscv/include/asm/mm.h +13 -0
@@ -283,6 +296,19 @@ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)
 
  #define PFN_ORDER(pg) ((pg)->v.free.order)
 
+static inline void page_set_xenheap_gfn(struct page_info *p, gfn_t gfn)
+{
+    gfn_t gfn_ = gfn_eq(gfn, INVALID_GFN) ? PGT_INVALID_XENHEAP_GFN : gfn;
+    unsigned long x, nx, y = p->u.inuse.type_info;
+
+    ASSERT(is_xen_heap_page(p));
+
+    do {
+        x = y;
+        nx = (x & ~PGT_gfn_mask) | gfn_x(gfn_);
+    } while ( (y = cmpxchg(&p->u.inuse.type_info, x, nx)) != x );
+}
+
  extern unsigned char cpu0_boot_stack[];
 
Quoted 2 lines

xen/arch/riscv/include/asm/mm.h static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr) · line 314

Quoted 1 line
xen/arch/riscv/include/asm/mm.h +0 -0
  void setup_initial_pagetables(void);

What about the "get" counterpart?

I haven't added it as it isn't used now and it will lead to compilation error as it will be static inline (in a similar way as Arm introduces it).

As an option this patch could be dropped and introduced with an introduction of grant tables.

~ Oleksii

#71 lore raw

Re: [PATCH v2 09/17] xen/riscv: introduce page_set_xenheap_gfn()

Quoted 17 lines · Oleksii Kurochko
Quoted 8 lines · Jan Beulich
Quoted 6 lines · Oleksii Kurochko

Introduce page_set_xenheap_gfn() helper to encode the GFN associated with a Xen heap page directly into the type_info field of struct page_info.

Introduce a GFN field in the type_info of a Xen heap page by reserving 10 bits (sufficient for both Sv32 and Sv39+ modes), and define PGT_gfn_mask and PGT_gfn_width accordingly.

This reads as if you wanted to encode the GFN in 10 bits.

I will reword it to:
Reserve 10 MSB bits to store the usage counter and frame type;
use all remaining bits to store the grant table frame GFN.
It will be enough as Sv32 uses 22-bit GFNs and Sv{39, 47, 58} uses 44-bit GFNs.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

What would also help is if you said why you actually need this. x86, after all, gets away without anything like this. (But I understand you're more Arm-like here.)

I think with the rewording mentioned above it will be clear that it is needed for grant tables. But I also can add the following:

I agree it's fine with just the re-wording.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
xen/arch/riscv/include/asm/mm.h +13 -0
@@ -283,6 +296,19 @@ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr)
 
  #define PFN_ORDER(pg) ((pg)->v.free.order)
 
+static inline void page_set_xenheap_gfn(struct page_info *p, gfn_t gfn)
+{
+    gfn_t gfn_ = gfn_eq(gfn, INVALID_GFN) ? PGT_INVALID_XENHEAP_GFN : gfn;
+    unsigned long x, nx, y = p->u.inuse.type_info;
+
+    ASSERT(is_xen_heap_page(p));
+
+    do {
+        x = y;
+        nx = (x & ~PGT_gfn_mask) | gfn_x(gfn_);
+    } while ( (y = cmpxchg(&p->u.inuse.type_info, x, nx)) != x );
+}
+
  extern unsigned char cpu0_boot_stack[];
 
Quoted 5 lines
Quoted 2 lines

xen/arch/riscv/include/asm/mm.h static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long nr) · line 314

Quoted 1 line
xen/arch/riscv/include/asm/mm.h +0 -0
  void setup_initial_pagetables(void);

What about the "get" counterpart?

I haven't added it as it isn't used now and it will lead to compilation error as it will be static inline (in a similar way as Arm introduces it).

Why would a static inline (in a header) cause compilation errors?

Quoted 1 line

As an option this patch could be dropped and introduced with an introduction of grant tables.

That's up to you - you must have had a reason to include it here.

Jan

#74 lore raw

Re: [PATCH v2 10/17] xen/riscv: implement guest_physmap_add_entry() for mapping GFNs to MFNs

In reply to Jan Beulich
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +15 -0
+                              unsigned long nr, mfn_t mfn, p2m_type_t t)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    int rc;
+
+    p2m_write_lock(p2m);
+    rc = p2m_set_entry(p2m, start_gfn, nr, mfn, t, p2m->default_access);
+    p2m_write_unlock(p2m);
+
+    return rc;
+}
+
+int map_regions_p2mt(struct domain *d,
+                     gfn_t gfn,
+                     unsigned long nr,
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 358

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+                     mfn_t mfn,
+                     p2m_type_t p2mt)
+{
+    return p2m_insert_mapping(d, gfn, nr, mfn, p2mt);
+}

What is this function doing here? The description says "for generic mapping purposes", which really may mean anything. Plus, if and when you need it, it wants to come with a name that fits with e.g. ...

These names are used across the common code and various architectures. Not all architectures need to implement all of these functions.
I believe|guest_physmap_add_page()| (which internally calls|guest_physmap_add_entry()|) is needed to be implemented for all architectures, while|map_regions_p2mt()| is used by Arm and the common Dom0less-related code, and because of RISC-V is going to re-use common Dom0less code it is implementing this function too.

Quoted 6 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 364

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+int guest_physmap_add_entry(struct domain *d,
+                            gfn_t gfn,
+                            mfn_t mfn,
+                            unsigned long page_order,
+                            p2m_type_t t)

... this one, to understand their relationship / difference.

Basically, the difference is only in API and where they are expected to be used:
- guest_physmap_add_entry() to map and set a specific p2m type for a page.
- map_regions_p2mt() to map a region (mostly MMIO) in the guest p2m with a specific p2m type.

I added both of them here as they are implemented in a similar way. I will re-word commit subject and message:
xen/riscv: implement functions to map memory in guest p2m

Introduce guest_physmap_add_entry() to map a page and assign a specific p2m type, and map_regions_p2mt() to map a region (typically MMIO) in the guest p2m with a designated p2m type.

Currently, this functionality is not fully operational, as p2m_set_entry() still returns -EOPNOTSUPP.

Additionally, introduce p2m_write_(un)lock() to protect modifications to the p2m page tables, along with p2m TLB flush helpers to ensure proper TLB invalidation (if necessary) when the p2m lock is released.

~ Oleksii

#75 lore raw

Re: [PATCH v2 10/17] xen/riscv: implement guest_physmap_add_entry() for mapping GFNs to MFNs

Quoted 26 lines · Oleksii Kurochko
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +15 -0
+                              unsigned long nr, mfn_t mfn, p2m_type_t t)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    int rc;
+
+    p2m_write_lock(p2m);
+    rc = p2m_set_entry(p2m, start_gfn, nr, mfn, t, p2m->default_access);
+    p2m_write_unlock(p2m);
+
+    return rc;
+}
+
+int map_regions_p2mt(struct domain *d,
+                     gfn_t gfn,
+                     unsigned long nr,
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 358

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+                     mfn_t mfn,
+                     p2m_type_t p2mt)
+{
+    return p2m_insert_mapping(d, gfn, nr, mfn, p2mt);
+}

What is this function doing here? The description says "for generic mapping purposes", which really may mean anything. Plus, if and when you need it, it wants to come with a name that fits with e.g. ...

Quoted 6 lines

These names are used across the common code and various architectures. Not all architectures need to implement all of these functions.
I believe|guest_physmap_add_page()| (which internally calls|guest_physmap_add_entry()|) is needed to be implemented for all architectures, while|map_regions_p2mt()| is used by Arm and the common Dom0less-related code, and because of RISC-V is going to re-use common Dom0less code it is implementing this function too.

First, my comment was solely about this one function above. And then I didn't even know Arm had such a function. It's not used from common code (except again from dom0less code where it should have been better abstracted, imo). I'm also not surprised I wasn't aware of it since, as can be implied from the above, otherwise I would likely have complained about its name not fitting the general scheme (which isn't all that good either).

Quoted 11 lines
Quoted 6 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 364

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+int guest_physmap_add_entry(struct domain *d,
+                            gfn_t gfn,
+                            mfn_t mfn,
+                            unsigned long page_order,
+                            p2m_type_t t)

... this one, to understand their relationship / difference.

Basically, the difference is only in API and where they are expected to be used:
- guest_physmap_add_entry() to map and set a specific p2m type for a page.
- map_regions_p2mt() to map a region (mostly MMIO) in the guest p2m with a specific p2m type.

Sorry, from this description they still look basically identical to me. The visible difference being that one takes a "nr" argument and the other a "page_order" one. Which still makes them largely redundant, and which still suggests that the earlier one's name doesn't really fit.

Quoted 7 lines

I added both of them here as they are implemented in a similar way. I will re-word commit subject and message:
xen/riscv: implement functions to map memory in guest p2m

Introduce guest_physmap_add_entry() to map a page and assign a specific p2m type, and map_regions_p2mt() to map a region (typically MMIO) in the guest p2m with a designated p2m type.

I.e., as per above, two functions for basically the same purpose.

Jan

#76 lore raw

Re: [PATCH v2 10/17] xen/riscv: implement guest_physmap_add_entry() for mapping GFNs to MFNs

Quoted 32 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +15 -0
+                              unsigned long nr, mfn_t mfn, p2m_type_t t)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    int rc;
+
+    p2m_write_lock(p2m);
+    rc = p2m_set_entry(p2m, start_gfn, nr, mfn, t, p2m->default_access);
+    p2m_write_unlock(p2m);
+
+    return rc;
+}
+
+int map_regions_p2mt(struct domain *d,
+                     gfn_t gfn,
+                     unsigned long nr,
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 358

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+                     mfn_t mfn,
+                     p2m_type_t p2mt)
+{
+    return p2m_insert_mapping(d, gfn, nr, mfn, p2mt);
+}

What is this function doing here? The description says "for generic mapping purposes", which really may mean anything. Plus, if and when you need it, it wants to come with a name that fits with e.g. ...

Quoted 6 lines

These names are used across the common code and various architectures. Not all architectures need to implement all of these functions.
I believe|guest_physmap_add_page()| (which internally calls|guest_physmap_add_entry()|) is needed to be implemented for all architectures, while|map_regions_p2mt()| is used by Arm and the common Dom0less-related code, and because of RISC-V is going to re-use common Dom0less code it is implementing this function too.

Quoted 6 lines

First, my comment was solely about this one function above. And then I didn't even know Arm had such a function. It's not used from common code (except again from dom0less code where it should have been better abstracted, imo). I'm also not surprised I wasn't aware of it since, as can be implied from the above, otherwise I would likely have complained about its name not fitting the general scheme (which isn't all that good either).

If I'm right, there is nothing similar to|map_regions_p2mt()| in the common headers.

Anyway, I think we could follow up with a patch to rename this function to something more appropriate.

I was thinking about adding something like|map_regions_to_guest()|,|map_p2m_regions()|, or|map_p2m_memory()| to|xen/mm.h|, along with proper renaming in the Arm code.

Does that make sense?

Quoted 16 lines
Quoted 11 lines
Quoted 7 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 364

Quoted 6 lines
xen/arch/riscv/p2m.c +5 -0
+int guest_physmap_add_entry(struct domain *d,
+                            gfn_t gfn,
+                            mfn_t mfn,
+                            unsigned long page_order,
+                            p2m_type_t t)

... this one, to understand their relationship / difference.

Basically, the difference is only in API and where they are expected to be used:
- guest_physmap_add_entry() to map and set a specific p2m type for a page.
- map_regions_p2mt() to map a region (mostly MMIO) in the guest p2m with a specific p2m type.

Sorry, from this description they still look basically identical to me. The visible difference being that one takes a "nr" argument and the other a "page_order" one. Which still makes them largely redundant, and which still suggests that the earlier one's name doesn't really fit.

Quoted 8 lines
Quoted 7 lines

I added both of them here as they are implemented in a similar way. I will re-word commit subject and message:
xen/riscv: implement functions to map memory in guest p2m

Introduce guest_physmap_add_entry() to map a page and assign a specific p2m type, and map_regions_p2mt() to map a region (typically MMIO) in the guest p2m with a designated p2m type.

I.e., as per above, two functions for basically the same purpose.

Generally, I agree that the purpose is the same.

I will then just drop|guest_physmap_add_entry()| and use only |map_regions_p2mt()| (or whatever name we decide on).

~ Oleksii

#77 lore raw

Re: [PATCH v2 10/17] xen/riscv: implement guest_physmap_add_entry() for mapping GFNs to MFNs

Quoted 43 lines · Oleksii Kurochko
Quoted 32 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +15 -0
+                              unsigned long nr, mfn_t mfn, p2m_type_t t)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    int rc;
+
+    p2m_write_lock(p2m);
+    rc = p2m_set_entry(p2m, start_gfn, nr, mfn, t, p2m->default_access);
+    p2m_write_unlock(p2m);
+
+    return rc;
+}
+
+int map_regions_p2mt(struct domain *d,
+                     gfn_t gfn,
+                     unsigned long nr,
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 358

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+                     mfn_t mfn,
+                     p2m_type_t p2mt)
+{
+    return p2m_insert_mapping(d, gfn, nr, mfn, p2mt);
+}

What is this function doing here? The description says "for generic mapping purposes", which really may mean anything. Plus, if and when you need it, it wants to come with a name that fits with e.g. ...

Quoted 6 lines

These names are used across the common code and various architectures. Not all architectures need to implement all of these functions.
I believe|guest_physmap_add_page()| (which internally calls|guest_physmap_add_entry()|) is needed to be implemented for all architectures, while|map_regions_p2mt()| is used by Arm and the common Dom0less-related code, and because of RISC-V is going to re-use common Dom0less code it is implementing this function too.

Quoted 6 lines

First, my comment was solely about this one function above. And then I didn't even know Arm had such a function. It's not used from common code (except again from dom0less code where it should have been better abstracted, imo). I'm also not surprised I wasn't aware of it since, as can be implied from the above, otherwise I would likely have complained about its name not fitting the general scheme (which isn't all that good either).

If I'm right, there is nothing similar to|map_regions_p2mt()| in the common headers.

Quoted 7 lines

Anyway, I think we could follow up with a patch to rename this function to something more appropriate.

I was thinking about adding something like|map_regions_to_guest()|,|map_p2m_regions()|, or|map_p2m_memory()| to|xen/mm.h|, along with proper renaming in the Arm code.

Does that make sense?

Imo that seemingly redundant function (i.e. if it's really needed) would want to be named guest_physmap_<whatever>().

Jan

#78 lore raw

Re: [PATCH v2 10/17] xen/riscv: implement guest_physmap_add_entry() for mapping GFNs to MFNs

Quoted 45 lines · Jan Beulich
Quoted 44 lines · Oleksii Kurochko
Quoted 32 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +15 -0
+                              unsigned long nr, mfn_t mfn, p2m_type_t t)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    int rc;
+
+    p2m_write_lock(p2m);
+    rc = p2m_set_entry(p2m, start_gfn, nr, mfn, t, p2m->default_access);
+    p2m_write_unlock(p2m);
+
+    return rc;
+}
+
+int map_regions_p2mt(struct domain *d,
+                     gfn_t gfn,
+                     unsigned long nr,
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 358

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+                     mfn_t mfn,
+                     p2m_type_t p2mt)
+{
+    return p2m_insert_mapping(d, gfn, nr, mfn, p2mt);
+}

What is this function doing here? The description says "for generic mapping purposes", which really may mean anything. Plus, if and when you need it, it wants to come with a name that fits with e.g. ...

Quoted 6 lines

These names are used across the common code and various architectures. Not all architectures need to implement all of these functions.
I believe|guest_physmap_add_page()| (which internally calls|guest_physmap_add_entry()|) is needed to be implemented for all architectures, while|map_regions_p2mt()| is used by Arm and the common Dom0less-related code, and because of RISC-V is going to re-use common Dom0less code it is implementing this function too.

Quoted 6 lines

First, my comment was solely about this one function above. And then I didn't even know Arm had such a function. It's not used from common code (except again from dom0less code where it should have been better abstracted, imo). I'm also not surprised I wasn't aware of it since, as can be implied from the above, otherwise I would likely have complained about its name not fitting the general scheme (which isn't all that good either).

If I'm right, there is nothing similar to|map_regions_p2mt()| in the common headers.

Anyway, I think we could follow up with a patch to rename this function to something more appropriate.

Quoted 6 lines
Quoted 4 lines

I was thinking about adding something like|map_regions_to_guest()|,|map_p2m_regions()|, or|map_p2m_memory()| to|xen/mm.h|, along with proper renaming in the Arm code.

Does that make sense?

Imo that seemingly redundant function (i.e. if it's really needed) would want to be named guest_physmap_<whatever>().

If it is redundant what is expected to be used instead to map_regions_p2mt() to map MMIO, for example, to guest: guest_physmap_add_page()? Based on the comment above the definition of this function it is for RAM: /* Untyped version for RAM only, for compatibility */

~ Oleksii

#79 lore raw

Re: [PATCH v2 10/17] xen/riscv: implement guest_physmap_add_entry() for mapping GFNs to MFNs

Quoted 50 lines · Oleksii Kurochko
Quoted 48 lines · Jan Beulich
Quoted 47 lines · Oleksii Kurochko
Quoted 32 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Oleksii Kurochko
(quoted diff) +15 -0
+                              unsigned long nr, mfn_t mfn, p2m_type_t t)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    int rc;
+
+    p2m_write_lock(p2m);
+    rc = p2m_set_entry(p2m, start_gfn, nr, mfn, t, p2m->default_access);
+    p2m_write_unlock(p2m);
+
+    return rc;
+}
+
+int map_regions_p2mt(struct domain *d,
+                     gfn_t gfn,
+                     unsigned long nr,
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+                     mfn_t mfn,
+                     p2m_type_t p2mt)
+{
+    return p2m_insert_mapping(d, gfn, nr, mfn, p2mt);
+}

What is this function doing here? The description says "for generic mapping purposes", which really may mean anything. Plus, if and when you need it, it wants to come with a name that fits with e.g. ...

Quoted 6 lines

These names are used across the common code and various architectures. Not all architectures need to implement all of these functions.
I believe|guest_physmap_add_page()| (which internally calls|guest_physmap_add_entry()|) is needed to be implemented for all architectures, while|map_regions_p2mt()| is used by Arm and the common Dom0less-related code, and because of RISC-V is going to re-use common Dom0less code it is implementing this function too.

Quoted 6 lines

First, my comment was solely about this one function above. And then I didn't even know Arm had such a function. It's not used from common code (except again from dom0less code where it should have been better abstracted, imo). I'm also not surprised I wasn't aware of it since, as can be implied from the above, otherwise I would likely have complained about its name not fitting the general scheme (which isn't all that good either).

If I'm right, there is nothing similar to|map_regions_p2mt()| in the common headers.

Anyway, I think we could follow up with a patch to rename this function to something more appropriate.

I was thinking about adding something like|map_regions_to_guest()|,|map_p2m_regions()|, or|map_p2m_memory()| to|xen/mm.h|, along with proper renaming in the Arm code.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Does that make sense?

Imo that seemingly redundant function (i.e. if it's really needed) would want to be named guest_physmap_<whatever>().

If it is redundant what is expected to be used instead to map_regions_p2mt() to map MMIO, for example, to guest: guest_physmap_add_page()? Based on the comment above the definition of this function it is for RAM: /* Untyped version for RAM only, for compatibility */

But we're talking about guest_physmap_add_entry().

Jan

#82 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

In reply to Jan Beulich
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Oleksii Kurochko

This patch introduces p2m_set_entry() and its core helper __p2m_set_entry() for RISC-V, based loosely on the Arm implementation, with several RISC-V-specific modifications.

Key differences include:
- TLB Flushing: RISC-V allows caching of invalid PTEs and does not require break-before-make (BBM). As a result, the flushing logic is simplified. TLB invalidation can be deferred until p2m_write_unlock() is called. Consequently, the p2m->need_flush flag is always considered true and is removed.
- Page Table Traversal: The order of walking the page tables differs from Arm, and this implementation reflects that reversed traversal.
- Macro Adjustments: The macros P2M_ROOT_LEVEL, P2M_ROOT_ORDER, and P2M_ROOT_PAGES are updated to align with the new RISC-V implementation.

The main functionality is in __p2m_set_entry(), which handles mappings aligned to page table block entries (e.g., 1GB, 2MB, or 4KB with 4KB granularity).

Quoted 6 lines
Quoted 5 lines

p2m_set_entry() breaks a region down into block-aligned mappings and calls __p2m_set_entry() accordingly.

Stub implementations (to be completed later) include:
- p2m_free_entry()

What would a function of this name do?

Recursively visiting all leaf PTE's for sub-tree behind an entry, then calls put_page() (which will free if there is no any reference to this page), freeing intermediate page table (after all entries were freed) by removing it from d->arch.paging.freelist, and removes correspondent page of intermediate page table from p2m->pages list.

Quoted 2 lines

You can clear entries, but you can't
free them, can you?

Is is a question regarding terminology? I can't free entry itself, but a page table or a page (if it is a leaf entry) on which it points could free.

Quoted 6 lines
Quoted 6 lines
xen/arch/riscv/include/asm/p2m.h +0 -0
@@ -9,8 +9,13 @@
  #include <xen/rwlock.h>
  #include <xen/types.h>
 
Quoted 8 lines

xen/arch/riscv/include/asm/p2m.h line 16

Quoted 5 lines
xen/arch/riscv/include/asm/p2m.h +3 -0
+#include <asm/page.h>
  #include <asm/page-bits.h>
 
+#define P2M_ROOT_LEVEL  HYP_PT_ROOT_LEVEL
+#define P2M_ROOT_ORDER  XEN_PT_LEVEL_ORDER(P2M_ROOT_LEVEL)

This is confusing, as in patch 6 we see that p2m root table order is 2. Something needs doing about the naming, so the two sets of things can't be confused.

Agree, confusing enough.

I will define|P2M_ROOT_ORDER| as|get_order_from_bytes(GUEST_ROOT_PAGE_TABLE_SIZE)| (or declare a new variable to store this value).

Actually, the way it's currently defined was only needed for|p2m_get_root_pointer() |to find the root page table by GFN, but|XEN_PT_LEVEL_ORDER(P2M_ROOT_LEVEL) |is used explicitly there, so I just missed doing a proper cleanup.

Quoted 15 lines
Quoted 15 lines
xen/arch/riscv/p2m.c +2 -0
@@ -231,6 +231,8 @@ int p2m_init(struct domain *d)
      INIT_PAGE_LIST_HEAD(&p2m->pages);
 
      p2m->vmid = INVALID_VMID;
+    p2m->max_mapped_gfn = _gfn(0);
+    p2m->lowest_mapped_gfn = _gfn(ULONG_MAX);
 
      p2m->default_access = p2m_access_rwx;
 
@@ -325,6 +327,214 @@ int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted)
      return 0;
  }
 
Quoted 7 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 335

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+/*
+ * Find and map the root page table. The caller is responsible for
+ * unmapping the table.
+ *
+ * The function will return NULL if the offset of the root table is
+ * invalid.

Don't you mean "offset into ..."?

If you won't suggested that, I will think that the meaning of "of" and "into" is pretty close. But it seems like semantically "into" is more accurate and better conveys the intent of the code.

Quoted 7 lines
Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+ */
+static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn)
+{
+    unsigned long root_table_indx;
+
+    root_table_indx = gfn_x(gfn) >> XEN_PT_LEVEL_ORDER(P2M_ROOT_LEVEL);
+    if ( root_table_indx >= P2M_ROOT_PAGES )
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 348

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+        return NULL;
+
+    return __map_domain_page(p2m->root + root_table_indx);
+}
+
+static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)

The rule of thumb is to have inline functions only in header files, leaving decisions to the compiler elsewhere.

I am not sure what you mean in the second part (after coma) of your sentence.

Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 353

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+{
+    panic("%s: isn't implemented for now\n", __func__);
+
+    return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):
/*
* In the case of the P2M, the valid bit is used for other purpose. Use
* the type to check whether an entry is valid.
*/
static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)

  {
      return p2m_type_radix_get(p2m, pte) != p2m_invalid;

}

It is done to track which page was modified by a guest.

Quoted 8 lines
Quoted 8 lines
xen/arch/riscv/p2m.c +8 -0
+static inline void p2m_write_pte(pte_t *p, pte_t pte, bool clean_pte)
+{
+    write_pte(p, pte);
+    if ( clean_pte )
+        clean_dcache_va_range(p, sizeof(*p));
+}
+
+static inline void p2m_remove_pte(pte_t *p, bool clean_pte)
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 368

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+{
+    pte_t pte;
+
+    memset(&pte, 0x00, sizeof(pte));
+    p2m_write_pte(p, pte, clean_pte);
+}

May I suggest "clear" instead of "remove" and plain 0 instead of 0x00 (or simply give the variable a trivial initializer)?

Sure, I will rename and use plain 0.

Quoted 3 lines

As to the earlier function that I commented on: Seeing the names here, wouldn't p2m_pte_is_valid() be a more consistent name there?

Then all p2me_*() should be updated to p2m_pte_*().

But initial logic was that p2me = p2m entry = p2m page table entry.

Probably we can just return back to the prefix p2m_ as based on arguments it is clear that it is a function for working with P2M's PTE.

Quoted 10 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 376

Quoted 8 lines
xen/arch/riscv/p2m.c +7 -0
+static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn,
+                                p2m_type_t t, p2m_access_t a)
+{
+    panic("%s: hasn't been implemented yet\n", __func__);
+
+    return (pte_t) { .pte = 0 };
+}

And then perhaps p2m_pte_from_mfn() here?

Quoted 6 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 381

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+#define GUEST_TABLE_MAP_NONE 0
+#define GUEST_TABLE_MAP_NOMEM 1
+#define GUEST_TABLE_SUPER_PAGE 2
+#define GUEST_TABLE_NORMAL 3

Is GUEST_ a good prefix? The guest doesn't control these tables, and the word could also mean the guest's own page tables.

Then P2M_ prefix should be better.

Quoted 5 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 380

Quoted 3 lines
xen/arch/riscv/p2m.c +2 -0
+/*
+ * Take the currently mapped table, find the corresponding GFN entry,

That's not what you mean though, is it? It's more like "the entry corresponding to the GFN" (implying "at the given level").

It will be more clear, I'll update the comment.

198 lines not shown · raw message

Part of this message is not shown here. View the raw message.

#83 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 33 lines · Oleksii Kurochko
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Oleksii Kurochko

This patch introduces p2m_set_entry() and its core helper __p2m_set_entry() for RISC-V, based loosely on the Arm implementation, with several RISC-V-specific modifications.

Key differences include:
- TLB Flushing: RISC-V allows caching of invalid PTEs and does not require break-before-make (BBM). As a result, the flushing logic is simplified. TLB invalidation can be deferred until p2m_write_unlock() is called. Consequently, the p2m->need_flush flag is always considered true and is removed.
- Page Table Traversal: The order of walking the page tables differs from Arm, and this implementation reflects that reversed traversal.
- Macro Adjustments: The macros P2M_ROOT_LEVEL, P2M_ROOT_ORDER, and P2M_ROOT_PAGES are updated to align with the new RISC-V implementation.

The main functionality is in __p2m_set_entry(), which handles mappings aligned to page table block entries (e.g., 1GB, 2MB, or 4KB with 4KB granularity).

Quoted 6 lines
Quoted 5 lines

p2m_set_entry() breaks a region down into block-aligned mappings and calls __p2m_set_entry() accordingly.

Stub implementations (to be completed later) include:
- p2m_free_entry()

What would a function of this name do?

Recursively visiting all leaf PTE's for sub-tree behind an entry, then calls put_page() (which will free if there is no any reference to this page), freeing intermediate page table (after all entries were freed) by removing it from d->arch.paging.freelist, and removes correspondent page of intermediate page table from p2m->pages list.

Quoted 4 lines
Quoted 2 lines

You can clear entries, but you can't
free them, can you?

Is is a question regarding terminology?

Yes. If one sees a call to a function, it should be possible to at least roughly know what it does without needing to go look at the implementation.

Quoted 2 lines

I can't free entry itself, but a page table or
a page (if it is a leaf entry) on which it points could free.

Then e.g. pte_free_subtree() or some such?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)

The rule of thumb is to have inline functions only in header files, leaving decisions to the compiler elsewhere.

I am not sure what you mean in the second part (after coma) of your sentence.

The compiler does its own inlining decisions quite fine when it can see all call sites (as is the case for static functions). Hence in general you want to omit "inline" there. Except of course in header files, where non-inline static-s are a problem.

Quoted 20 lines
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 353

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+{
+    panic("%s: isn't implemented for now\n", __func__);
+
+    return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):

   /*
    * In the case of the P2M, the valid bit is used for other purpose. Use
    * the type to check whether an entry is valid.
    */
   static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
   {
       return p2m_type_radix_get(p2m, pte) != p2m_invalid;
   }
Quoted 1 line

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does. Plus can't a guest also arrange for an entry's type to move to p2m_invalid? That's then still an entry that was modified by the guest.

Overall I think I'm lacking clarity what you mean to use this predicate for.

Quoted 23 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/riscv/p2m.c +8 -0
+static inline void p2m_write_pte(pte_t *p, pte_t pte, bool clean_pte)
+{
+    write_pte(p, pte);
+    if ( clean_pte )
+        clean_dcache_va_range(p, sizeof(*p));
+}
+
+static inline void p2m_remove_pte(pte_t *p, bool clean_pte)
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 368

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+{
+    pte_t pte;
+
+    memset(&pte, 0x00, sizeof(pte));
+    p2m_write_pte(p, pte, clean_pte);
+}

May I suggest "clear" instead of "remove" and plain 0 instead of 0x00 (or simply give the variable a trivial initializer)?

Sure, I will rename and use plain 0.

Quoted 3 lines

As to the earlier function that I commented on: Seeing the names here, wouldn't p2m_pte_is_valid() be a more consistent name there?

Quoted 6 lines

Then all p2me_*() should be updated to p2m_pte_*().

But initial logic was that p2me = p2m entry = p2m page table entry.

Probably we can just return back to the prefix p2m_ as based on arguments it is clear that it is a function for working with P2M's PTE.

In the end it's up to you. Having thought about it some more, perhaps p2me_*() is still quite helpful to separate from functions dealing with P2Ms as a while, and to also avoid the verbosity of p2m_pte_*().

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+{
+    unsigned int level;
+    unsigned int target = page_order / PAGETABLE_ORDER;
+    pte_t *entry, *table, orig_pte;
+    int rc;
+    /* A mapping is removed if the MFN is invalid. */
+    bool removing_mapping = mfn_eq(smfn, INVALID_MFN);
+    DECLARE_OFFSETS(offsets, gfn_to_gaddr(sgfn));
+
+    ASSERT(p2m_is_write_locked(p2m));
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 439

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+
+    /*
+     * Check if the level target is valid: we only support
+     * 4K - 2M - 1G mapping.
+     */
+    ASSERT(target <= 2);

No provisions towards the division that produced the value having left a remainder?

Quoted 6 lines

The way the order is initialized will always result in division without a remainder.

If it makes sense, the|ASSERT()| could be updated to ensure that the order is always a multiple of|PAGETABLE_ORDER|:

   ASSERT((target <= 2) && !IS_ALIGNED(page_order, PAGETABLE_ORDER));

Except that the ! looks wrong here.

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/riscv/p2m.c +3 -1
@@ -332,7 +542,55 @@ static int p2m_set_entry(struct p2m_domain *p2m,
                           p2m_type_t t,
                           p2m_access_t a)
  {
-    return -EOPNOTSUPP;
+    int rc = 0;
+
+    /*
Quoted 8 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 554

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+     * Any reference taken by the P2M mappings (e.g. foreign mapping) will
+     * be dropped in relinquish_p2m_mapping(). As the P2M will still
+     * be accessible after, we need to prevent mapping to be added when the
+     * domain is dying.
+     */
+    if ( unlikely(p2m->domain->is_dying) )
+        return -ENOMEM;

Why ENOMEM?

Quoted 3 lines

I expect that when a domain is dying, it means there’s no point in using its memory—either because it's no longer available or it has already been freed. Basically, no memory.

That can end up odd for call sites. Please consider using e.g. EACCES.

Quoted 5 lines
Quoted 3 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 556

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+    while ( nr )

Why's there a loop here? The function name uses singular, i.e. means to create exactly one entry.

I will rename the function to p2m_set_entries().

Or maybe p2m_set_range()?

Quoted 11 lines
Quoted 8 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 569

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+        /*
+         * Don't take into account the MFN when removing mapping (i.e
+         * MFN_INVALID) to calculate the correct target order.
+         *
+         * XXX: Support superpage mappings if nr is not aligned to a
+         * superpage size.
+         */

Does this really need leaving as a to-do?

I think so, yes. It won’t break the current workflow if|nr| isn’t aligned, a smaller order will simply be chosen.

Well, my question was more like "Isn't it simple enough to cover the case right away?"

Quoted 29 lines
Quoted 29 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 578

Quoted 9 lines
xen/arch/riscv/p2m.c +9 -0
+        mask = !mfn_eq(smfn, INVALID_MFN) ? mfn_x(smfn) : 0;
+        mask |= gfn_x(sgfn) | nr;
+
+        for ( ; i != 0; i-- )
+        {
+            if ( !(mask & (BIT(XEN_PT_LEVEL_ORDER(i), UL) - 1)) )
+            {
+                    order = XEN_PT_LEVEL_ORDER(i);
+                    break;

Nit: Style.

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 590

Quoted 12 lines
xen/arch/riscv/p2m.c +12 -0
+            }
+        }
+
+        rc = __p2m_set_entry(p2m, sgfn, order, smfn, t, a);
+        if ( rc )
+            break;
+
+        sgfn = gfn_add(sgfn, (1 << order));
+        if ( !mfn_eq(smfn, INVALID_MFN) )
+           smfn = mfn_add(smfn, (1 << order));
+
+        nr -= (1 << order);

Throughout maybe better be safe right away and use 1UL?

Quoted 4 lines
xen/arch/riscv/p2m.c +3 -0
+    }
+
+    return rc;
  }
Quoted 5 lines
Quoted 2 lines

How's the caller going to know how much of the range was successfully mapped?

There is no such option. Do other arches do that? I mean returns somehow the number of successfully mapped (sgfn,smfn).

On x86 we had to introduce some not very nice code to cover for the absence of proper handling there. For a new port I think it wants at least seriously considering not to repeat such a potentially unhelpful pattern.

Quoted 5 lines
Quoted 2 lines

That part may need undoing (if not here, then in the caller), or a caller may want to retry.

So the caller in the case if rc != 0, can just undoing the full range (by using the same sgfn, nr, smfn).

Can it? How would it know what the original state was?

Jan

#84 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 35 lines · Jan Beulich
Quoted 34 lines · Oleksii Kurochko
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Oleksii Kurochko

This patch introduces p2m_set_entry() and its core helper __p2m_set_entry() for RISC-V, based loosely on the Arm implementation, with several RISC-V-specific modifications.

Key differences include:
- TLB Flushing: RISC-V allows caching of invalid PTEs and does not require break-before-make (BBM). As a result, the flushing logic is simplified. TLB invalidation can be deferred until p2m_write_unlock() is called. Consequently, the p2m->need_flush flag is always considered true and is removed.
- Page Table Traversal: The order of walking the page tables differs from Arm, and this implementation reflects that reversed traversal.
- Macro Adjustments: The macros P2M_ROOT_LEVEL, P2M_ROOT_ORDER, and P2M_ROOT_PAGES are updated to align with the new RISC-V implementation.

The main functionality is in __p2m_set_entry(), which handles mappings aligned to page table block entries (e.g., 1GB, 2MB, or 4KB with 4KB granularity).

Quoted 6 lines
Quoted 5 lines

p2m_set_entry() breaks a region down into block-aligned mappings and calls __p2m_set_entry() accordingly.

Stub implementations (to be completed later) include:
- p2m_free_entry()

What would a function of this name do?

Recursively visiting all leaf PTE's for sub-tree behind an entry, then calls put_page() (which will free if there is no any reference to this page), freeing intermediate page table (after all entries were freed) by removing it from d->arch.paging.freelist, and removes correspondent page of intermediate page table from p2m->pages list.

Quoted 2 lines

You can clear entries, but you can't
free them, can you?

Quoted 7 lines
Quoted 1 line

Is is a question regarding terminology?

Yes. If one sees a call to a function, it should be possible to at least roughly know what it does without needing to go look at the implementation.

Quoted 2 lines

I can't free entry itself, but a page table or
a page (if it is a leaf entry) on which it points could free.

Then e.g. pte_free_subtree() or some such?

It sounds fine to me. I'll use suggested name.

Just want to notice that other arches also have the same function for the same purpose with the same name.
Does it make sense then to change a name for all arches?

Quoted 9 lines
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

+static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)

The rule of thumb is to have inline functions only in header files, leaving decisions to the compiler elsewhere.

I am not sure what you mean in the second part (after coma) of your sentence.

The compiler does its own inlining decisions quite fine when it can see all call sites (as is the case for static functions). Hence in general you want to omit "inline" there. Except of course in header files, where non-inline static-s are a problem.

Thanks, now it is clear what you meant.

Quoted 20 lines
Quoted 20 lines
Quoted 9 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 353

Quoted 6 lines
xen/arch/riscv/p2m.c +5 -0
+{
+    panic("%s: isn't implemented for now\n", __func__);
+
+    return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):

    /*
     * In the case of the P2M, the valid bit is used for other purpose. Use
     * the type to check whether an entry is valid.
     */
    static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
    {
        return p2m_type_radix_get(p2m, pte) != p2m_invalid;
    }
Quoted 2 lines
Quoted 1 line

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does.

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

Quoted 3 lines

Plus
can't a guest also arrange for an entry's type to move to p2m_invalid? That's then still an entry that was modified by the guest.

I am not really sure that I fully understand the question.
Do you ask if a guest can do something which will lead to a call of p2m_set_entry() with p2m_invalid argument?
If yes, then it seems like it will be done only in case of p2m_remove_mapping() what will mean that alongside with p2m_invalid INVALID_MFN will be also passed, what means this entry isn't expected to be used anymore.

Quoted 2 lines

Overall I think I'm lacking clarity what you mean to use this predicate for.

By using of "p2me_" predicate I wanted to express that not PTE's valid bit will be checked, but the type saved in radix tree will be used.
As suggested above probably it will be better drop "e" too and just use p2m_type_is_valid().

Quoted 49 lines
Quoted 19 lines
Quoted 17 lines
Quoted 5 lines
xen/arch/riscv/p2m.c +4 -0
+static inline void p2m_write_pte(pte_t *p, pte_t pte, bool clean_pte)
+{
+    write_pte(p, pte);
+    if ( clean_pte )

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 368

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+        clean_dcache_va_range(p, sizeof(*p));
+}
+
+static inline void p2m_remove_pte(pte_t *p, bool clean_pte)
+{
+    pte_t pte;
+
+    memset(&pte, 0x00, sizeof(pte));
+    p2m_write_pte(p, pte, clean_pte);
+}

May I suggest "clear" instead of "remove" and plain 0 instead of 0x00 (or simply give the variable a trivial initializer)?

Sure, I will rename and use plain 0.

Quoted 8 lines
Quoted 2 lines

As to the earlier function that I commented on: Seeing the names here, wouldn't p2m_pte_is_valid() be a more consistent name there?

Then all p2me_*() should be updated to p2m_pte_*().

But initial logic was that p2me = p2m entry = p2m page table entry.

Probably we can just return back to the prefix p2m_ as based on arguments it is clear that it is a function for working with P2M's PTE.

In the end it's up to you. Having thought about it some more, perhaps p2me_*() is still quite helpful to separate from functions dealing with P2Ms as a while, and to also avoid the verbosity of p2m_pte_*().

Quoted 18 lines
Quoted 18 lines
Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+{
+    unsigned int level;
+    unsigned int target = page_order / PAGETABLE_ORDER;
+    pte_t *entry, *table, orig_pte;
+    int rc;
+    /* A mapping is removed if the MFN is invalid. */

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 439

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+    bool removing_mapping = mfn_eq(smfn, INVALID_MFN);
+    DECLARE_OFFSETS(offsets, gfn_to_gaddr(sgfn));
+
+    ASSERT(p2m_is_write_locked(p2m));
+
+    /*
+     * Check if the level target is valid: we only support
+     * 4K - 2M - 1G mapping.
+     */
+    ASSERT(target <= 2);

No provisions towards the division that produced the value having left a remainder?

Quoted 7 lines
Quoted 6 lines

The way the order is initialized will always result in division without a remainder.

If it makes sense, the|ASSERT()| could be updated to ensure that the order is always a multiple of|PAGETABLE_ORDER|:

    ASSERT((target <= 2) && !IS_ALIGNED(page_order, PAGETABLE_ORDER));

Except that the ! looks wrong here.

Agree, it shouldn't be here. Thanks.

Quoted 20 lines
Quoted 17 lines
Quoted 17 lines
Quoted 6 lines
xen/arch/riscv/p2m.c +0 -1
@@ -332,7 +542,55 @@ static int p2m_set_entry(struct p2m_domain *p2m,
                            p2m_type_t t,
                            p2m_access_t a)
   {
-    return -EOPNOTSUPP;

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 554

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+    int rc = 0;
+
+    /*
+     * Any reference taken by the P2M mappings (e.g. foreign mapping) will
+     * be dropped in relinquish_p2m_mapping(). As the P2M will still
+     * be accessible after, we need to prevent mapping to be added when the
+     * domain is dying.
+     */
+    if ( unlikely(p2m->domain->is_dying) )
+        return -ENOMEM;

Why ENOMEM?

Quoted 3 lines

I expect that when a domain is dying, it means there’s no point in using its memory—either because it's no longer available or it has already been freed. Basically, no memory.

Quoted 7 lines

That can end up odd for call sites. Please consider using e.g. EACCES.

Quoted 4 lines
Quoted 3 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 556

Quoted 1 line
xen/arch/riscv/p2m.c +1 -0
+    while ( nr )

Why's there a loop here? The function name uses singular, i.e. means to create exactly one entry.

I will rename the function to p2m_set_entries().

Or maybe p2m_set_range()?

It is much better.

Quoted 51 lines
Quoted 11 lines
Quoted 9 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 569

Quoted 8 lines
xen/arch/riscv/p2m.c +7 -0
+        /*
+         * Don't take into account the MFN when removing mapping (i.e
+         * MFN_INVALID) to calculate the correct target order.
+         *
+         * XXX: Support superpage mappings if nr is not aligned to a
+         * superpage size.
+         */

Does this really need leaving as a to-do?

I think so, yes. It won’t break the current workflow if|nr| isn’t aligned, a smaller order will simply be chosen.

Well, my question was more like "Isn't it simple enough to cover the case right away?"

Quoted 25 lines
Quoted 25 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 578

Quoted 9 lines
xen/arch/riscv/p2m.c +9 -0
+        mask = !mfn_eq(smfn, INVALID_MFN) ? mfn_x(smfn) : 0;
+        mask |= gfn_x(sgfn) | nr;
+
+        for ( ; i != 0; i-- )
+        {
+            if ( !(mask & (BIT(XEN_PT_LEVEL_ORDER(i), UL) - 1)) )
+            {
+                    order = XEN_PT_LEVEL_ORDER(i);
+                    break;

Nit: Style.

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 590

Quoted 12 lines
xen/arch/riscv/p2m.c +12 -0
+            }
+        }
+
+        rc = __p2m_set_entry(p2m, sgfn, order, smfn, t, a);
+        if ( rc )
+            break;
+
+        sgfn = gfn_add(sgfn, (1 << order));
+        if ( !mfn_eq(smfn, INVALID_MFN) )
+           smfn = mfn_add(smfn, (1 << order));
+
+        nr -= (1 << order);

Throughout maybe better be safe right away and use 1UL?

Quoted 8 lines
Quoted 6 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 594

Quoted 4 lines
xen/arch/riscv/p2m.c +3 -0
+    }
+
+    return rc;
   }

How's the caller going to know how much of the range was successfully mapped?

There is no such option. Do other arches do that? I mean returns somehow the number of successfully mapped (sgfn,smfn).

On x86 we had to introduce some not very nice code to cover for the absence of proper handling there. For a new port I think it wants at least seriously considering not to repeat such a potentially unhelpful pattern.

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

That part may need undoing (if not here, then in the caller), or a caller may want to retry.

So the caller in the case if rc != 0, can just undoing the full range (by using the same sgfn, nr, smfn).

Can it? How would it know what the original state was?

You're right — blindly unmapping the range assumes that no entries were valid beforehand and I missed that it could be that something valid was mapped before p2m_set_entry(sgfn,...,smfn) was called.
But then I am not really understand why it won't be an issue if will know how many GFNs were successfully mapped.

~ Oleksii

#85 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 40 lines · Oleksii Kurochko
Quoted 39 lines · Jan Beulich
Quoted 32 lines · Oleksii Kurochko
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Oleksii Kurochko

This patch introduces p2m_set_entry() and its core helper __p2m_set_entry() for RISC-V, based loosely on the Arm implementation, with several RISC-V-specific modifications.

Key differences include:
- TLB Flushing: RISC-V allows caching of invalid PTEs and does not require break-before-make (BBM). As a result, the flushing logic is simplified. TLB invalidation can be deferred until p2m_write_unlock() is called. Consequently, the p2m->need_flush flag is always considered true and is removed.
- Page Table Traversal: The order of walking the page tables differs from Arm, and this implementation reflects that reversed traversal.
- Macro Adjustments: The macros P2M_ROOT_LEVEL, P2M_ROOT_ORDER, and P2M_ROOT_PAGES are updated to align with the new RISC-V implementation.

The main functionality is in __p2m_set_entry(), which handles mappings aligned to page table block entries (e.g., 1GB, 2MB, or 4KB with 4KB granularity).

Quoted 6 lines
Quoted 5 lines

p2m_set_entry() breaks a region down into block-aligned mappings and calls __p2m_set_entry() accordingly.

Stub implementations (to be completed later) include:
- p2m_free_entry()

What would a function of this name do?

Recursively visiting all leaf PTE's for sub-tree behind an entry, then calls put_page() (which will free if there is no any reference to this page), freeing intermediate page table (after all entries were freed) by removing it from d->arch.paging.freelist, and removes correspondent page of intermediate page table from p2m->pages list.

Quoted 3 lines
Quoted 2 lines

You can clear entries, but you can't
free them, can you?

Is is a question regarding terminology?

Yes. If one sees a call to a function, it should be possible to at least roughly know what it does without needing to go look at the implementation.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

I can't free entry itself, but a page table or
a page (if it is a leaf entry) on which it points could free.

Then e.g. pte_free_subtree() or some such?

It sounds fine to me. I'll use suggested name.

Just want to notice that other arches also have the same function for the same purpose with the same name.

As to x86, it's not general P2M code which uses this odd (for the purpose) name, but only p2m-pt.c.

Quoted 1 line

Does it make sense then to change a name for all arches?

I think so.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 353

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+{
+    panic("%s: isn't implemented for now\n", __func__);
+
+    return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):

    /*
     * In the case of the P2M, the valid bit is used for other purpose. Use
     * the type to check whether an entry is valid.
     */
    static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
    {
        return p2m_type_radix_get(p2m, pte) != p2m_invalid;
    }
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does.

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

For P2M type checks please don't invent new naming, but use what both x86 and Arm are already using. Note how we already have p2m_is_valid() in that set. Just that it's not doing what you want here.

Quoted 7 lines
Quoted 3 lines

Plus
can't a guest also arrange for an entry's type to move to p2m_invalid? That's then still an entry that was modified by the guest.

I am not really sure that I fully understand the question.
Do you ask if a guest can do something which will lead to a call of p2m_set_entry() with p2m_invalid argument?

That I'm not asking, but rather stating. I.e. I expect such is possible.

Quoted 3 lines

If yes, then it seems like it will be done only in case of p2m_remove_mapping() what will mean that alongside with p2m_invalid INVALID_MFN will be also passed, what means this entry isn't expected to be used anymore.

Right. But such an entry would still have been "modified" by the guest.

Quoted 6 lines
Quoted 2 lines

Overall I think I'm lacking clarity what you mean to use this predicate for.

By using of "p2me_" predicate I wanted to express that not PTE's valid bit will be checked, but the type saved in radix tree will be used.
As suggested above probably it will be better drop "e" too and just use p2m_type_is_valid().

See above regarding that name.

Quoted 54 lines
Quoted 54 lines
Quoted 10 lines
Quoted 8 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 569

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+        /*
+         * Don't take into account the MFN when removing mapping (i.e
+         * MFN_INVALID) to calculate the correct target order.
+         *
+         * XXX: Support superpage mappings if nr is not aligned to a
+         * superpage size.
+         */

Does this really need leaving as a to-do?

I think so, yes. It won’t break the current workflow if|nr| isn’t aligned, a smaller order will simply be chosen.

Well, my question was more like "Isn't it simple enough to cover the case right away?"

Quoted 25 lines
Quoted 25 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 578

Quoted 9 lines
xen/arch/riscv/p2m.c +9 -0
+        mask = !mfn_eq(smfn, INVALID_MFN) ? mfn_x(smfn) : 0;
+        mask |= gfn_x(sgfn) | nr;
+
+        for ( ; i != 0; i-- )
+        {
+            if ( !(mask & (BIT(XEN_PT_LEVEL_ORDER(i), UL) - 1)) )
+            {
+                    order = XEN_PT_LEVEL_ORDER(i);
+                    break;

Nit: Style.

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 590

Quoted 12 lines
xen/arch/riscv/p2m.c +12 -0
+            }
+        }
+
+        rc = __p2m_set_entry(p2m, sgfn, order, smfn, t, a);
+        if ( rc )
+            break;
+
+        sgfn = gfn_add(sgfn, (1 << order));
+        if ( !mfn_eq(smfn, INVALID_MFN) )
+           smfn = mfn_add(smfn, (1 << order));
+
+        nr -= (1 << order);

Throughout maybe better be safe right away and use 1UL?

Quoted 8 lines
Quoted 6 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 594

Quoted 4 lines
xen/arch/riscv/p2m.c +3 -0
+    }
+
+    return rc;
   }

How's the caller going to know how much of the range was successfully mapped?

There is no such option. Do other arches do that? I mean returns somehow the number of successfully mapped (sgfn,smfn).

On x86 we had to introduce some not very nice code to cover for the absence of proper handling there. For a new port I think it wants at least seriously considering not to repeat such a potentially unhelpful pattern.

Quoted 4 lines
Quoted 2 lines

That part may need undoing (if not here, then in the caller), or a caller may want to retry.

So the caller in the case if rc != 0, can just undoing the full range (by using the same sgfn, nr, smfn).

Quoted 7 lines
Quoted 1 line

Can it? How would it know what the original state was?

You're right — blindly unmapping the range assumes that no entries were valid beforehand and I missed that it could be that something valid was mapped before p2m_set_entry(sgfn,...,smfn) was called.
But then I am not really understand why it won't be an issue if will know how many GFNs were successfully mapped.

The caller may know what that range's state was. But what I really wanted to convey is: Updating multiple entries in one go is complicated in some of the corner cases. You will want to think this through now, in order to avoid the need to re-write everything later again.

Jan

#86 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 73 lines · Jan Beulich
Quoted 40 lines · Oleksii Kurochko
Quoted 39 lines · Jan Beulich
Quoted 32 lines · Oleksii Kurochko
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Oleksii Kurochko

This patch introduces p2m_set_entry() and its core helper __p2m_set_entry() for RISC-V, based loosely on the Arm implementation, with several RISC-V-specific modifications.

Key differences include:
- TLB Flushing: RISC-V allows caching of invalid PTEs and does not require break-before-make (BBM). As a result, the flushing logic is simplified. TLB invalidation can be deferred until p2m_write_unlock() is called. Consequently, the p2m->need_flush flag is always considered true and is removed.
- Page Table Traversal: The order of walking the page tables differs from Arm, and this implementation reflects that reversed traversal.
- Macro Adjustments: The macros P2M_ROOT_LEVEL, P2M_ROOT_ORDER, and P2M_ROOT_PAGES are updated to align with the new RISC-V implementation.

The main functionality is in __p2m_set_entry(), which handles mappings aligned to page table block entries (e.g., 1GB, 2MB, or 4KB with 4KB granularity).

Quoted 6 lines
Quoted 5 lines

p2m_set_entry() breaks a region down into block-aligned mappings and calls __p2m_set_entry() accordingly.

Stub implementations (to be completed later) include:
- p2m_free_entry()

What would a function of this name do?

Recursively visiting all leaf PTE's for sub-tree behind an entry, then calls put_page() (which will free if there is no any reference to this page), freeing intermediate page table (after all entries were freed) by removing it from d->arch.paging.freelist, and removes correspondent page of intermediate page table from p2m->pages list.

Quoted 3 lines
Quoted 2 lines

You can clear entries, but you can't
free them, can you?

Is is a question regarding terminology?

Yes. If one sees a call to a function, it should be possible to at least roughly know what it does without needing to go look at the implementation.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

I can't free entry itself, but a page table or
a page (if it is a leaf entry) on which it points could free.

Then e.g. pte_free_subtree() or some such?

It sounds fine to me. I'll use suggested name.

Just want to notice that other arches also have the same function for the same purpose with the same name.

As to x86, it's not general P2M code which uses this odd (for the purpose) name, but only p2m-pt.c.

Quoted 1 line

Does it make sense then to change a name for all arches?

I think so.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 8 lines

xen/arch/riscv/p2m.c int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) · line 353

Quoted 5 lines
xen/arch/riscv/p2m.c +5 -0
+{
+    panic("%s: isn't implemented for now\n", __func__);
+
+    return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):

     /*
      * In the case of the P2M, the valid bit is used for other purpose. Use
      * the type to check whether an entry is valid.
      */
     static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
     {
         return p2m_type_radix_get(p2m, pte) != p2m_invalid;
     }
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines
Quoted 1 line

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does.

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

For P2M type checks please don't invent new naming, but use what both x86 and Arm are already using. Note how we already have p2m_is_valid() in that set. Just that it's not doing what you want here.

Hm, why not doing what I want? p2m_is_valid() verifies if P2M entry is valid. And in here it is checked if P2M pte is valid from P2M point of view by checking the type in radix tree and/or in reserved PTEs bits (just to remind we have only 2 free bits for type).

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

Plus
can't a guest also arrange for an entry's type to move to p2m_invalid? That's then still an entry that was modified by the guest.

I am not really sure that I fully understand the question.
Do you ask if a guest can do something which will lead to a call of p2m_set_entry() with p2m_invalid argument?

Quoted 6 lines

That I'm not asking, but rather stating. I.e. I expect such is possible.

Quoted 3 lines

If yes, then it seems like it will be done only in case of p2m_remove_mapping() what will mean that alongside with p2m_invalid INVALID_MFN will be also passed, what means this entry isn't expected to be used anymore.

Right. But such an entry would still have been "modified" by the guest.

Yes, but nothing then is needed to do with it. For example, if it is already invalid there is not any sense to flush page to RAM (as in this case PTE's bit will be checked), something like Arm does:
https://elixir.bootlin.com/xen/v4.20.0/source/xen/arch/arm/p2m.c#L375

Quoted 60 lines
Quoted 52 lines
Quoted 52 lines
Quoted 10 lines
Quoted 8 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 569

Quoted 7 lines
xen/arch/riscv/p2m.c +7 -0
+        /*
+         * Don't take into account the MFN when removing mapping (i.e
+         * MFN_INVALID) to calculate the correct target order.
+         *
+         * XXX: Support superpage mappings if nr is not aligned to a
+         * superpage size.
+         */

Does this really need leaving as a to-do?

I think so, yes. It won’t break the current workflow if|nr| isn’t aligned, a smaller order will simply be chosen.

Well, my question was more like "Isn't it simple enough to cover the case right away?"

Quoted 25 lines
Quoted 25 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 578

Quoted 9 lines
xen/arch/riscv/p2m.c +9 -0
+        mask = !mfn_eq(smfn, INVALID_MFN) ? mfn_x(smfn) : 0;
+        mask |= gfn_x(sgfn) | nr;
+
+        for ( ; i != 0; i-- )
+        {
+            if ( !(mask & (BIT(XEN_PT_LEVEL_ORDER(i), UL) - 1)) )
+            {
+                    order = XEN_PT_LEVEL_ORDER(i);
+                    break;

Nit: Style.

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 590

Quoted 12 lines
xen/arch/riscv/p2m.c +12 -0
+            }
+        }
+
+        rc = __p2m_set_entry(p2m, sgfn, order, smfn, t, a);
+        if ( rc )
+            break;
+
+        sgfn = gfn_add(sgfn, (1 << order));
+        if ( !mfn_eq(smfn, INVALID_MFN) )
+           smfn = mfn_add(smfn, (1 << order));
+
+        nr -= (1 << order);

Throughout maybe better be safe right away and use 1UL?

Quoted 8 lines
Quoted 6 lines

xen/arch/riscv/p2m.c static int p2m_set_entry(struct p2m_domain *p2m, · line 594

Quoted 4 lines
xen/arch/riscv/p2m.c +3 -0
+    }
+
+    return rc;
    }

How's the caller going to know how much of the range was successfully mapped?

There is no such option. Do other arches do that? I mean returns somehow the number of successfully mapped (sgfn,smfn).

On x86 we had to introduce some not very nice code to cover for the absence of proper handling there. For a new port I think it wants at least seriously considering not to repeat such a potentially unhelpful pattern.

Quoted 2 lines
Quoted 2 lines

That part may need undoing (if not here, then in the caller), or a caller may want to retry.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

So the caller in the case if rc != 0, can just undoing the full range (by using the same sgfn, nr, smfn).

Can it? How would it know what the original state was?

You're right — blindly unmapping the range assumes that no entries were valid beforehand and I missed that it could be that something valid was mapped before p2m_set_entry(sgfn,...,smfn) was called.
But then I am not really understand why it won't be an issue if will know how many GFNs were successfully mapped.

Quoted 4 lines

The caller may know what that range's state was. But what I really wanted to convey is: Updating multiple entries in one go is complicated in some of the corner cases. You will want to think this through now, in order to avoid the need to re-write everything later again.

I can add one more argument to return the number of successfully mapped GFNs. Fortunately, that's very easy to do.

The problem for me is that I don’t really understand what the caller is supposed to do with that information. The only use case I can think of is that the caller might try to map the remaining GFNs again. But that doesn’t seem very useful, if|p2m_set_entry()| wasn’t able to map the full range, it likely indicates a serious issue, and retrying would probably result in the same error.

The same applies to rolling back the state. It wouldn’t be difficult to add a local array to track all modified PTEs and then use it to revert the state if needed. But again, what would the caller do after the rollback? At this point, it still seems like the best option is simply to|panic(). |

Basically, I don’t see or understand the cases where knowing how many GFNs were successfully mapped, or whether a rollback was performed, would really help — because in most cases, I don’t have a better option than just calling|panic()| at the end.

For example, if I call|map_regions_p2mt()| for an MMIO region described in a device tree node, and the mapping fails partway through, I’m left with two options: either ignore the device (if it's not essential for Xen or guest functionality) and continue
booting; in which case I’d need to perform a rollback, and simply knowing the number
of successfully mapped GFNs may not be enough or, more likely, just panic.

Are there any realistic use cases where knowing the number of mapped GFNs or having rollback support would actually allow us to avoid a panic?

Even more so, how would that information be used in the current call chain? We have the following chain:
|map_regions_p2mt()| →|p2m_insert()| →|p2m_set_entry()|

If|p2m_set_entry()| returns the number of successfully mapped GFNs, what should

|p2m_insert()| do with it — process it further, or just pass it along to
|map_regions_p2mt()|?

Thanks in advance for clarifications.
|~ Oleksii |

#87 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 28 lines · Oleksii Kurochko
Quoted 27 lines · Jan Beulich
Quoted 26 lines · Oleksii Kurochko
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich

Review

Quoted 5 lines · Oleksii Kurochko
(quoted diff) +5 -0
+{
+    panic("%s: isn't implemented for now\n", __func__);
+
+    return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):

     /*
      * In the case of the P2M, the valid bit is used for other purpose. Use
      * the type to check whether an entry is valid.
      */
     static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
     {
         return p2m_type_radix_get(p2m, pte) != p2m_invalid;
     }
Quoted 2 lines
Quoted 1 line

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does.

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

Quoted 8 lines
Quoted 3 lines

For P2M type checks please don't invent new naming, but use what both x86 and Arm are already using. Note how we already have p2m_is_valid() in that set. Just that it's not doing what you want here.

Hm, why not doing what I want? p2m_is_valid() verifies if P2M entry is valid. And in here it is checked if P2M pte is valid from P2M point of view by checking the type in radix tree and/or in reserved PTEs bits (just to remind we have only 2 free bits for type).

Because this is how it's defined on x86:

#define p2m_is_valid(_t)    (p2m_to_mask(_t) & \
                             (P2M_RAM_TYPES | p2m_to_mask(p2m_mmio_direct)))

I.e. more strict that simply "!= p2m_invalid". And I think such predicates would better be uniform across architectures, such that in principle they might also be usable in common code (as we already do with p2m_is_foreign()).

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Plus
can't a guest also arrange for an entry's type to move to p2m_invalid? That's then still an entry that was modified by the guest.

I am not really sure that I fully understand the question.
Do you ask if a guest can do something which will lead to a call of p2m_set_entry() with p2m_invalid argument?

Quoted 8 lines
Quoted 6 lines

That I'm not asking, but rather stating. I.e. I expect such is possible.

Quoted 3 lines

If yes, then it seems like it will be done only in case of p2m_remove_mapping() what will mean that alongside with p2m_invalid INVALID_MFN will be also passed, what means this entry isn't expected to be used anymore.

Right. But such an entry would still have been "modified" by the guest.

Yes, but nothing then is needed to do with it.

I understand that. Maybe I'm overly picky, but all of the above was in response to you saying "It is done to track which page was modified by a guest." And I'm simply trying to get you to use precise wording, both in code comments and in discussions. In a case like the one here I simply can't judge whether you simply expressed yourself not clear enough, or whether you indeed meant what you said.

Quoted 65 lines
Quoted 60 lines
Quoted 52 lines
Quoted 52 lines
Quoted 10 lines
Quoted 8 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+        /*
+         * Don't take into account the MFN when removing mapping (i.e
+         * MFN_INVALID) to calculate the correct target order.
+         *
+         * XXX: Support superpage mappings if nr is not aligned to a
+         * superpage size.
+         */

Does this really need leaving as a to-do?

I think so, yes. It won’t break the current workflow if|nr| isn’t aligned, a smaller order will simply be chosen.

Well, my question was more like "Isn't it simple enough to cover the case right away?"

Quoted 25 lines
Quoted 25 lines

Review

Quoted 9 lines
(quoted diff) +9 -0
+        mask = !mfn_eq(smfn, INVALID_MFN) ? mfn_x(smfn) : 0;
+        mask |= gfn_x(sgfn) | nr;
+
+        for ( ; i != 0; i-- )
+        {
+            if ( !(mask & (BIT(XEN_PT_LEVEL_ORDER(i), UL) - 1)) )
+            {
+                    order = XEN_PT_LEVEL_ORDER(i);
+                    break;

Nit: Style.

Review

Quoted 12 lines
(quoted diff) +12 -0
+            }
+        }
+
+        rc = __p2m_set_entry(p2m, sgfn, order, smfn, t, a);
+        if ( rc )
+            break;
+
+        sgfn = gfn_add(sgfn, (1 << order));
+        if ( !mfn_eq(smfn, INVALID_MFN) )
+           smfn = mfn_add(smfn, (1 << order));
+
+        nr -= (1 << order);

Throughout maybe better be safe right away and use 1UL?

Quoted 8 lines
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +3 -0
+    }
+
+    return rc;
    }

How's the caller going to know how much of the range was successfully mapped?

There is no such option. Do other arches do that? I mean returns somehow the number of successfully mapped (sgfn,smfn).

On x86 we had to introduce some not very nice code to cover for the absence of proper handling there. For a new port I think it wants at least seriously considering not to repeat such a potentially unhelpful pattern.

Quoted 2 lines
Quoted 2 lines

That part may need undoing (if not here, then in the caller), or a caller may want to retry.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

So the caller in the case if rc != 0, can just undoing the full range (by using the same sgfn, nr, smfn).

Can it? How would it know what the original state was?

You're right — blindly unmapping the range assumes that no entries were valid beforehand and I missed that it could be that something valid was mapped before p2m_set_entry(sgfn,...,smfn) was called.
But then I am not really understand why it won't be an issue if will know how many GFNs were successfully mapped.

Quoted 4 lines

The caller may know what that range's state was. But what I really wanted to convey is: Updating multiple entries in one go is complicated in some of the corner cases. You will want to think this through now, in order to avoid the need to re-write everything later again.

Quoted 5 lines

I can add one more argument to return the number of successfully mapped GFNs. Fortunately, that's very easy to do.

The problem for me is that I don’t really understand what the caller is supposed to do with that information.

That's only the 2nd step to take. The first is: What behavior do you want, overall?

Quoted 5 lines

The only use case I can think of is that the caller
might try to map the remaining GFNs again. But that doesn’t seem very useful, if|p2m_set_entry()| wasn’t able to map the full range, it likely indicates a serious issue, and retrying would probably result in the same error.

Quoted 8 lines

The same applies to rolling back the state. It wouldn’t be difficult to add a local array to track all modified PTEs and then use it to revert the state if needed. But again, what would the caller do after the rollback? At this point, it still seems like the best option is simply to|panic(). |

Basically, I don’t see or understand the cases where knowing how many GFNs were successfully mapped, or whether a rollback was performed, would really help — because in most cases, I don’t have a better option than just calling|panic()| at the end.

panic()-ing is of course only a last resort. Anything related to domain handling would better crash only the domain in question. And even that only if suitable error handling isn't possible.

Quoted 5 lines

For example, if I call|map_regions_p2mt()| for an MMIO region described in a device tree node, and the mapping fails partway through, I’m left with two options: either ignore the device (if it's not essential for Xen or guest functionality) and continue
booting; in which case I’d need to perform a rollback, and simply knowing the number
of successfully mapped GFNs may not be enough or, more likely, just panic.

Well, no. For example, before even trying to map you could check that the range of P2M entries covered is all empty. _Then_ you know how to correctly roll back. And yes, doing so may not even require passing back information on how much of a region was successfully mapped.

Jan

#88 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 24 lines · Jan Beulich
Quoted 21 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich

Review

Quoted 5 lines · Oleksii Kurochko
(quoted diff) +5 -0
+{
+    panic("%s: isn't implemented for now\n", __func__);
+
+    return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):

      /*
       * In the case of the P2M, the valid bit is used for other purpose. Use
       * the type to check whether an entry is valid.
       */
      static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
      {
          return p2m_type_radix_get(p2m, pte) != p2m_invalid;
      }
Quoted 1 line

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

For P2M type checks please don't invent new naming, but use what both x86 and Arm are already using. Note how we already have p2m_is_valid() in that set. Just that it's not doing what you want here.

Hm, why not doing what I want? p2m_is_valid() verifies if P2M entry is valid. And in here it is checked if P2M pte is valid from P2M point of view by checking the type in radix tree and/or in reserved PTEs bits (just to remind we have only 2 free bits for type).

Quoted 8 lines

Because this is how it's defined on x86:

#define p2m_is_valid(_t)    (p2m_to_mask(_t) & \
                              (P2M_RAM_TYPES | p2m_to_mask(p2m_mmio_direct)))

I.e. more strict that simply "!= p2m_invalid". And I think such predicates would better be uniform across architectures, such that in principle they might also be usable in common code (as we already do with p2m_is_foreign()).

Yeah, Arm isn't so strict in definition of p2m_is_valid() and it seems like x86 and Arm have different understanding what is valid.

Except what mentioned in the comment that grant types aren't considered valid for x86 (and shouldn't be the same then for Arm?), it isn't clear why x86's p2m_is_valid() is stricter then Arm's one and if other arches should be also so strict.
It seems like from the point of view of mapping/unmapping it is enough just to verify a "copy" of PTE's valid bit (in terms of P2M it is p2m_invalid type).

Quoted 84 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

Plus
can't a guest also arrange for an entry's type to move to p2m_invalid? That's then still an entry that was modified by the guest.

I am not really sure that I fully understand the question.
Do you ask if a guest can do something which will lead to a call of p2m_set_entry() with p2m_invalid argument?

Quoted 7 lines
Quoted 6 lines

That I'm not asking, but rather stating. I.e. I expect such is possible.

Quoted 3 lines

If yes, then it seems like it will be done only in case of p2m_remove_mapping() what will mean that alongside with p2m_invalid INVALID_MFN will be also passed, what means this entry isn't expected to be used anymore.

Right. But such an entry would still have been "modified" by the guest.

Yes, but nothing then is needed to do with it.

I understand that. Maybe I'm overly picky, but all of the above was in response to you saying "It is done to track which page was modified by a guest." And I'm simply trying to get you to use precise wording, both in code comments and in discussions. In a case like the one here I simply can't judge whether you simply expressed yourself not clear enough, or whether you indeed meant what you said.

Quoted 64 lines
Quoted 64 lines
Quoted 52 lines
Quoted 52 lines
Quoted 10 lines
Quoted 8 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+        /*
+         * Don't take into account the MFN when removing mapping (i.e
+         * MFN_INVALID) to calculate the correct target order.
+         *
+         * XXX: Support superpage mappings if nr is not aligned to a
+         * superpage size.
+         */

Does this really need leaving as a to-do?

I think so, yes. It won’t break the current workflow if|nr| isn’t aligned, a smaller order will simply be chosen.

Well, my question was more like "Isn't it simple enough to cover the case right away?"

Quoted 25 lines
Quoted 25 lines

Review

Quoted 9 lines
(quoted diff) +9 -0
+        mask = !mfn_eq(smfn, INVALID_MFN) ? mfn_x(smfn) : 0;
+        mask |= gfn_x(sgfn) | nr;
+
+        for ( ; i != 0; i-- )
+        {
+            if ( !(mask & (BIT(XEN_PT_LEVEL_ORDER(i), UL) - 1)) )
+            {
+                    order = XEN_PT_LEVEL_ORDER(i);
+                    break;

Nit: Style.

Review

Quoted 12 lines
(quoted diff) +12 -0
+            }
+        }
+
+        rc = __p2m_set_entry(p2m, sgfn, order, smfn, t, a);
+        if ( rc )
+            break;
+
+        sgfn = gfn_add(sgfn, (1 << order));
+        if ( !mfn_eq(smfn, INVALID_MFN) )
+           smfn = mfn_add(smfn, (1 << order));
+
+        nr -= (1 << order);

Throughout maybe better be safe right away and use 1UL?

Quoted 8 lines
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +3 -0
+    }
+
+    return rc;
     }

How's the caller going to know how much of the range was successfully mapped?

There is no such option. Do other arches do that? I mean returns somehow the number of successfully mapped (sgfn,smfn).

On x86 we had to introduce some not very nice code to cover for the absence of proper handling there. For a new port I think it wants at least seriously considering not to repeat such a potentially unhelpful pattern.

Quoted 2 lines
Quoted 2 lines

That part may need undoing (if not here, then in the caller), or a caller may want to retry.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

So the caller in the case if rc != 0, can just undoing the full range (by using the same sgfn, nr, smfn).

Can it? How would it know what the original state was?

You're right — blindly unmapping the range assumes that no entries were valid beforehand and I missed that it could be that something valid was mapped before p2m_set_entry(sgfn,...,smfn) was called.
But then I am not really understand why it won't be an issue if will know how many GFNs were successfully mapped.

The caller may know what that range's state was. But what I really wanted to convey is: Updating multiple entries in one go is complicated in some of the corner cases. You will want to think this through now, in order to avoid the need to re-write everything later again.

Quoted 6 lines
Quoted 5 lines

I can add one more argument to return the number of successfully mapped GFNs. Fortunately, that's very easy to do.

The problem for me is that I don’t really understand what the caller is supposed to do with that information.

That's only the 2nd step to take. The first is: What behavior do you want, overall?

My initial idea was that if something went wrong ( rc != 0 ) then just panic(). But based on your questions it seems like it isn't the best one idea.

Quoted 11 lines
Quoted 11 lines

The only use case I can think of is that the caller
might try to map the remaining GFNs again. But that doesn’t seem very useful, if|p2m_set_entry()| wasn’t able to map the full range, it likely indicates a serious issue, and retrying would probably result in the same error.

The same applies to rolling back the state. It wouldn’t be difficult to add a local array to track all modified PTEs and then use it to revert the state if needed. But again, what would the caller do after the rollback? At this point, it still seems like the best option is simply to|panic(). |

Quoted 6 lines
Quoted 3 lines

Basically, I don’t see or understand the cases where knowing how many GFNs were successfully mapped, or whether a rollback was performed, would really help — because in most cases, I don’t have a better option than just calling|panic()| at the end.

panic()-ing is of course only a last resort. Anything related to domain handling would better crash only the domain in question. And even that only if suitable error handling isn't possible.

And if there is no still any runnable domain available, for example, we are creating domain and some p2m mapping is called? Will it be enough just ignore to boot this domain? If yes, then it is enough to return only error code without returning how many GFNs were mapped or rollbacking as domain won't be ran anyway.
(just to mention, I am not trying to convince you that rollback or returning of an amount of GFNs isn't necessary, I just trying to understand what is the best implementation of handling none-fully mapped mappings you mentioned)

Quoted 8 lines
Quoted 6 lines

For example, if I call|map_regions_p2mt()| for an MMIO region described in a device tree node, and the mapping fails partway through, I’m left with two options: either ignore the device (if it's not essential for Xen or guest functionality) and continue
booting; in which case I’d need to perform a rollback, and simply knowing the number
of successfully mapped GFNs may not be enough or, more likely, just panic.

Well, no. For example, before even trying to map you could check that the range of P2M entries covered is all empty.

Could it be that they aren't all empty? Then it seems like we have overlapping and we can't just do a mapping, right?
Won't be this procedure consume a lot of time as it is needed to go through each page tables for each entry.

Quoted 3 lines

_Then_ you know how to correctly roll back.
And yes, doing so may not even require passing back information on how much of a region was successfully mapped.

If P2M entries were empty before start of the mapping then it is enough to just go through the same range (sgfn,nr,smfn) and just clean them, right?

Thanks.

~ Oleksii

#89 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 46 lines · Oleksii Kurochko
Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 24 lines · Jan Beulich
Quoted 21 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich

Review

Quoted 5 lines · Oleksii Kurochko
(quoted diff) +5 -0
+{
+    panic("%s: isn't implemented for now\n", __func__);
+
+    return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):

      /*
       * In the case of the P2M, the valid bit is used for other purpose. Use
       * the type to check whether an entry is valid.
       */
      static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
      {
          return p2m_type_radix_get(p2m, pte) != p2m_invalid;
      }
Quoted 1 line

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

For P2M type checks please don't invent new naming, but use what both x86 and Arm are already using. Note how we already have p2m_is_valid() in that set. Just that it's not doing what you want here.

Hm, why not doing what I want? p2m_is_valid() verifies if P2M entry is valid. And in here it is checked if P2M pte is valid from P2M point of view by checking the type in radix tree and/or in reserved PTEs bits (just to remind we have only 2 free bits for type).

Quoted 8 lines

Because this is how it's defined on x86:

#define p2m_is_valid(_t)    (p2m_to_mask(_t) & \
                              (P2M_RAM_TYPES | p2m_to_mask(p2m_mmio_direct)))

I.e. more strict that simply "!= p2m_invalid". And I think such predicates would better be uniform across architectures, such that in principle they might also be usable in common code (as we already do with p2m_is_foreign()).

Quoted 7 lines

Yeah, Arm isn't so strict in definition of p2m_is_valid() and it seems like x86 and Arm have different understanding what is valid.

Except what mentioned in the comment that grant types aren't considered valid for x86 (and shouldn't be the same then for Arm?), it isn't clear why x86's p2m_is_valid() is stricter then Arm's one and if other arches should be also so strict.

Arm's p2m_is_valid() is entirely different (and imo misnamed, but arguably one could also consider x86'es to require a better name). It's a local helper, not a P2M type checking predicate. With that in mind, you may of course follow Arm's model, but in the longer run we may need to do something about the name collision then.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

The only use case I can think of is that the caller
might try to map the remaining GFNs again. But that doesn’t seem very useful, if|p2m_set_entry()| wasn’t able to map the full range, it likely indicates a serious issue, and retrying would probably result in the same error.

The same applies to rolling back the state. It wouldn’t be difficult to add a local array to track all modified PTEs and then use it to revert the state if needed. But again, what would the caller do after the rollback? At this point, it still seems like the best option is simply to|panic(). |

Basically, I don’t see or understand the cases where knowing how many GFNs were successfully mapped, or whether a rollback was performed, would really help — because in most cases, I don’t have a better option than just calling|panic()| at the end.

Quoted 8 lines
Quoted 3 lines

panic()-ing is of course only a last resort. Anything related to domain handling would better crash only the domain in question. And even that only if suitable error handling isn't possible.

And if there is no still any runnable domain available, for example, we are creating domain and some p2m mapping is called? Will it be enough just ignore to boot this domain? If yes, then it is enough to return only error code without returning how many GFNs were mapped or rollbacking as domain won't be ran anyway.

During domain creation all you need to do is return an error. But when you write a generic function that's also (going to be) used at domain runtime, you need to consider what to do there in case of partial success.

Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

For example, if I call|map_regions_p2mt()| for an MMIO region described in a device tree node, and the mapping fails partway through, I’m left with two options: either ignore the device (if it's not essential for Xen or guest functionality) and continue
booting; in which case I’d need to perform a rollback, and simply knowing the number
of successfully mapped GFNs may not be enough or, more likely, just panic.

Well, no. For example, before even trying to map you could check that the range of P2M entries covered is all empty.

Could it be that they aren't all empty? Then it seems like we have overlapping and we can't just do a mapping, right?

Possibly that would simply mean to return an error, yes.

Quoted 2 lines

Won't be this procedure consume a lot of time as it is needed to go through each page tables for each entry.

Well, you're free to suggest a clean alternative without doing so.

Quoted 6 lines
Quoted 3 lines

_Then_ you know how to correctly roll back.
And yes, doing so may not even require passing back information on how much of a region was successfully mapped.

If P2M entries were empty before start of the mapping then it is enough to just go through the same range (sgfn,nr,smfn) and just clean them, right?

Yes, what else would "roll back" mean in that case?

Jan

#90 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 90 lines · Jan Beulich
Quoted 45 lines · Oleksii Kurochko
Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 24 lines · Jan Beulich
Quoted 21 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich

Review

Quoted 5 lines · Oleksii Kurochko
(quoted diff) +5 -0
+{
+    panic("%s: isn't implemented for now\n", __func__);
+
+    return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):

       /*
        * In the case of the P2M, the valid bit is used for other purpose. Use
        * the type to check whether an entry is valid.
        */
       static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
       {
           return p2m_type_radix_get(p2m, pte) != p2m_invalid;
       }
Quoted 1 line

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

For P2M type checks please don't invent new naming, but use what both x86 and Arm are already using. Note how we already have p2m_is_valid() in that set. Just that it's not doing what you want here.

Hm, why not doing what I want? p2m_is_valid() verifies if P2M entry is valid. And in here it is checked if P2M pte is valid from P2M point of view by checking the type in radix tree and/or in reserved PTEs bits (just to remind we have only 2 free bits for type).

Quoted 8 lines

Because this is how it's defined on x86:

#define p2m_is_valid(_t)    (p2m_to_mask(_t) & \
                               (P2M_RAM_TYPES | p2m_to_mask(p2m_mmio_direct)))

I.e. more strict that simply "!= p2m_invalid". And I think such predicates would better be uniform across architectures, such that in principle they might also be usable in common code (as we already do with p2m_is_foreign()).

Quoted 7 lines

Yeah, Arm isn't so strict in definition of p2m_is_valid() and it seems like x86 and Arm have different understanding what is valid.

Except what mentioned in the comment that grant types aren't considered valid for x86 (and shouldn't be the same then for Arm?), it isn't clear why x86's p2m_is_valid() is stricter then Arm's one and if other arches should be also so strict.

Arm's p2m_is_valid() is entirely different (and imo misnamed, but arguably one could also consider x86'es to require a better name). It's a local helper, not a P2M type checking predicate. With that in mind, you may of course follow Arm's model, but in the longer run we may need to do something about the name collision then.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

The only use case I can think of is that the caller
might try to map the remaining GFNs again. But that doesn’t seem very useful, if|p2m_set_entry()| wasn’t able to map the full range, it likely indicates a serious issue, and retrying would probably result in the same error.

The same applies to rolling back the state. It wouldn’t be difficult to add a local array to track all modified PTEs and then use it to revert the state if needed. But again, what would the caller do after the rollback? At this point, it still seems like the best option is simply to|panic(). |

Basically, I don’t see or understand the cases where knowing how many GFNs were successfully mapped, or whether a rollback was performed, would really help — because in most cases, I don’t have a better option than just calling|panic()| at the end.

Quoted 7 lines
Quoted 3 lines

panic()-ing is of course only a last resort. Anything related to domain handling would better crash only the domain in question. And even that only if suitable error handling isn't possible.

And if there is no still any runnable domain available, for example, we are creating domain and some p2m mapping is called? Will it be enough just ignore to boot this domain? If yes, then it is enough to return only error code without returning how many GFNs were mapped or rollbacking as domain won't be ran anyway.

During domain creation all you need to do is return an error. But when you write a generic function that's also (going to be) used at domain runtime, you need to consider what to do there in case of partial success.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

For example, if I call|map_regions_p2mt()| for an MMIO region described in a device tree node, and the mapping fails partway through, I’m left with two options: either ignore the device (if it's not essential for Xen or guest functionality) and continue
booting; in which case I’d need to perform a rollback, and simply knowing the number
of successfully mapped GFNs may not be enough or, more likely, just panic.

Well, no. For example, before even trying to map you could check that the range of P2M entries covered is all empty.

Quoted 7 lines
Quoted 2 lines

Could it be that they aren't all empty? Then it seems like we have overlapping and we can't just do a mapping, right?

Possibly that would simply mean to return an error, yes.

Quoted 2 lines

Won't be this procedure consume a lot of time as it is needed to go through each page tables for each entry.

Well, you're free to suggest a clean alternative without doing so.

I thought about dynamically allocating an array in p2m_set_entry(), where to save all changed PTEs, and then use it to roll back if __p2m_set_entry() returns rc != 0 ...

Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

_Then_ you know how to correctly roll back.
And yes, doing so may not even require passing back information on how much of a region was successfully mapped.

If P2M entries were empty before start of the mapping then it is enough to just go through the same range (sgfn,nr,smfn) and just clean them, right?

Yes, what else would "roll back" mean in that case?

... If we know that the P2M entries were empty, then there's nothing else to be done, just clean PTE is needed to be done.
However, if the P2M entries weren’t empty (and I’m still not sure whether that’s a legal case), then rolling back would mean restoring their original state, the state they had before the P2M mapping procedure started.

~ Oleksii

#91 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 106 lines · Oleksii Kurochko
Quoted 90 lines · Jan Beulich
Quoted 45 lines · Oleksii Kurochko
Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 24 lines · Jan Beulich
Quoted 21 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich

Review

Quoted 5 lines · Oleksii Kurochko
(quoted diff) +5 -0
+{
+    panic("%s: isn't implemented for now\n", __func__);
+
+    return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):

       /*
        * In the case of the P2M, the valid bit is used for other purpose. Use
        * the type to check whether an entry is valid.
        */
       static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
       {
           return p2m_type_radix_get(p2m, pte) != p2m_invalid;
       }
Quoted 1 line

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

For P2M type checks please don't invent new naming, but use what both x86 and Arm are already using. Note how we already have p2m_is_valid() in that set. Just that it's not doing what you want here.

Hm, why not doing what I want? p2m_is_valid() verifies if P2M entry is valid. And in here it is checked if P2M pte is valid from P2M point of view by checking the type in radix tree and/or in reserved PTEs bits (just to remind we have only 2 free bits for type).

Quoted 8 lines

Because this is how it's defined on x86:

#define p2m_is_valid(_t)    (p2m_to_mask(_t) & \
                               (P2M_RAM_TYPES | p2m_to_mask(p2m_mmio_direct)))

I.e. more strict that simply "!= p2m_invalid". And I think such predicates would better be uniform across architectures, such that in principle they might also be usable in common code (as we already do with p2m_is_foreign()).

Quoted 7 lines

Yeah, Arm isn't so strict in definition of p2m_is_valid() and it seems like x86 and Arm have different understanding what is valid.

Except what mentioned in the comment that grant types aren't considered valid for x86 (and shouldn't be the same then for Arm?), it isn't clear why x86's p2m_is_valid() is stricter then Arm's one and if other arches should be also so strict.

Arm's p2m_is_valid() is entirely different (and imo misnamed, but arguably one could also consider x86'es to require a better name). It's a local helper, not a P2M type checking predicate. With that in mind, you may of course follow Arm's model, but in the longer run we may need to do something about the name collision then.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

The only use case I can think of is that the caller
might try to map the remaining GFNs again. But that doesn’t seem very useful, if|p2m_set_entry()| wasn’t able to map the full range, it likely indicates a serious issue, and retrying would probably result in the same error.

The same applies to rolling back the state. It wouldn’t be difficult to add a local array to track all modified PTEs and then use it to revert the state if needed. But again, what would the caller do after the rollback? At this point, it still seems like the best option is simply to|panic(). |

Basically, I don’t see or understand the cases where knowing how many GFNs were successfully mapped, or whether a rollback was performed, would really help — because in most cases, I don’t have a better option than just calling|panic()| at the end.

Quoted 7 lines
Quoted 3 lines

panic()-ing is of course only a last resort. Anything related to domain handling would better crash only the domain in question. And even that only if suitable error handling isn't possible.

And if there is no still any runnable domain available, for example, we are creating domain and some p2m mapping is called? Will it be enough just ignore to boot this domain? If yes, then it is enough to return only error code without returning how many GFNs were mapped or rollbacking as domain won't be ran anyway.

During domain creation all you need to do is return an error. But when you write a generic function that's also (going to be) used at domain runtime, you need to consider what to do there in case of partial success.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

For example, if I call|map_regions_p2mt()| for an MMIO region described in a device tree node, and the mapping fails partway through, I’m left with two options: either ignore the device (if it's not essential for Xen or guest functionality) and continue
booting; in which case I’d need to perform a rollback, and simply knowing the number
of successfully mapped GFNs may not be enough or, more likely, just panic.

Well, no. For example, before even trying to map you could check that the range of P2M entries covered is all empty.

Quoted 7 lines
Quoted 2 lines

Could it be that they aren't all empty? Then it seems like we have overlapping and we can't just do a mapping, right?

Possibly that would simply mean to return an error, yes.

Quoted 2 lines

Won't be this procedure consume a lot of time as it is needed to go through each page tables for each entry.

Well, you're free to suggest a clean alternative without doing so.

I thought about dynamically allocating an array in p2m_set_entry(), where to save all changed PTEs, and then use it to roll back if __p2m_set_entry() returns rc != 0 ...

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

_Then_ you know how to correctly roll back.
And yes, doing so may not even require passing back information on how much of a region was successfully mapped.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

If P2M entries were empty before start of the mapping then it is enough to just go through the same range (sgfn,nr,smfn) and just clean them, right?

Yes, what else would "roll back" mean in that case?

... If we know that the P2M entries were empty, then there's nothing else to be done, just clean PTE is needed to be done.
However, if the P2M entries weren’t empty (and I’m still not sure whether that’s a legal case), then rolling back would mean restoring their original state, the state they had before the P2M mapping procedure started.

Possible roll back is harder to implement as expected because there is a case where subtree could be freed:

    /*
     * Free the entry only if the original pte was valid and the base
     * is different (to avoid freeing when permission is changed).
     */
    if ( p2me_is_valid(p2m, orig_pte) &&
         !mfn_eq(pte_get_mfn(*entry), pte_get_mfn(orig_pte)) )
        p2m_free_subtree(p2m, orig_pte, level);

In this case then it will be needed to store the full subtree.

~ Oleksii

#92 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 97 lines · Oleksii Kurochko
Quoted 95 lines · Oleksii Kurochko
Quoted 92 lines · Jan Beulich
Quoted 45 lines · Oleksii Kurochko
Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 24 lines · Jan Beulich
Quoted 21 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich

Review

Quoted 5 lines · Oleksii Kurochko
(quoted diff) +5 -0
+{
+    panic("%s: isn't implemented for now\n", __func__);
+
+    return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):

       /*
        * In the case of the P2M, the valid bit is used for other purpose. Use
        * the type to check whether an entry is valid.
        */
       static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
       {
           return p2m_type_radix_get(p2m, pte) != p2m_invalid;
       }
Quoted 1 line

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

For P2M type checks please don't invent new naming, but use what both x86 and Arm are already using. Note how we already have p2m_is_valid() in that set. Just that it's not doing what you want here.

Hm, why not doing what I want? p2m_is_valid() verifies if P2M entry is valid. And in here it is checked if P2M pte is valid from P2M point of view by checking the type in radix tree and/or in reserved PTEs bits (just to remind we have only 2 free bits for type).

Quoted 8 lines

Because this is how it's defined on x86:

#define p2m_is_valid(_t)    (p2m_to_mask(_t) & \
                               (P2M_RAM_TYPES | p2m_to_mask(p2m_mmio_direct)))

I.e. more strict that simply "!= p2m_invalid". And I think such predicates would better be uniform across architectures, such that in principle they might also be usable in common code (as we already do with p2m_is_foreign()).

Quoted 7 lines

Yeah, Arm isn't so strict in definition of p2m_is_valid() and it seems like x86 and Arm have different understanding what is valid.

Except what mentioned in the comment that grant types aren't considered valid for x86 (and shouldn't be the same then for Arm?), it isn't clear why x86's p2m_is_valid() is stricter then Arm's one and if other arches should be also so strict.

Arm's p2m_is_valid() is entirely different (and imo misnamed, but arguably one could also consider x86'es to require a better name). It's a local helper, not a P2M type checking predicate. With that in mind, you may of course follow Arm's model, but in the longer run we may need to do something about the name collision then.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

The only use case I can think of is that the caller
might try to map the remaining GFNs again. But that doesn’t seem very useful, if|p2m_set_entry()| wasn’t able to map the full range, it likely indicates a serious issue, and retrying would probably result in the same error.

The same applies to rolling back the state. It wouldn’t be difficult to add a local array to track all modified PTEs and then use it to revert the state if needed. But again, what would the caller do after the rollback? At this point, it still seems like the best option is simply to|panic(). |

Basically, I don’t see or understand the cases where knowing how many GFNs were successfully mapped, or whether a rollback was performed, would really help — because in most cases, I don’t have a better option than just calling|panic()| at the end.

Quoted 7 lines
Quoted 3 lines

panic()-ing is of course only a last resort. Anything related to domain handling would better crash only the domain in question. And even that only if suitable error handling isn't possible.

And if there is no still any runnable domain available, for example, we are creating domain and some p2m mapping is called? Will it be enough just ignore to boot this domain? If yes, then it is enough to return only error code without returning how many GFNs were mapped or rollbacking as domain won't be ran anyway.

During domain creation all you need to do is return an error. But when you write a generic function that's also (going to be) used at domain runtime, you need to consider what to do there in case of partial success.

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

For example, if I call|map_regions_p2mt()| for an MMIO region described in a device tree node, and the mapping fails partway through, I’m left with two options: either ignore the device (if it's not essential for Xen or guest functionality) and continue
booting; in which case I’d need to perform a rollback, and simply knowing the number
of successfully mapped GFNs may not be enough or, more likely, just panic.

Well, no. For example, before even trying to map you could check that the range of P2M entries covered is all empty.

Could it be that they aren't all empty? Then it seems like we have overlapping and we can't just do a mapping, right?

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

Possibly that would simply mean to return an error, yes.

Quoted 2 lines

Won't be this procedure consume a lot of time as it is needed to go through each page tables for each entry.

Well, you're free to suggest a clean alternative without doing so.

I thought about dynamically allocating an array in p2m_set_entry(), where to save all changed PTEs, and then use it to roll back if __p2m_set_entry() returns rc != 0 ...

That's another possible source for failure, and such an allocation may end up being a rather big one.

Quoted 14 lines
Quoted 11 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

_Then_ you know how to correctly roll back.
And yes, doing so may not even require passing back information on how much of a region was successfully mapped.

If P2M entries were empty before start of the mapping then it is enough to just go through the same range (sgfn,nr,smfn) and just clean them, right?

Yes, what else would "roll back" mean in that case?

... If we know that the P2M entries were empty, then there's nothing else to be done, just clean PTE is needed to be done.
However, if the P2M entries weren’t empty (and I’m still not sure whether that’s a legal case), then rolling back would mean restoring their original state, the state they had before the P2M mapping procedure started.

Possible roll back is harder to implement as expected because there is a case where subtree could be freed:

Quoted 8 lines
     /*
      * Free the entry only if the original pte was valid and the base
      * is different (to avoid freeing when permission is changed).
      */
     if ( p2me_is_valid(p2m, orig_pte) &&
          !mfn_eq(pte_get_mfn(*entry), pte_get_mfn(orig_pte)) )
         p2m_free_subtree(p2m, orig_pte, level);

In this case then it will be needed to store the full subtree.

Right, which is why it may be desirable to limit the ability to update multiple entries in one go. Or work from certain assumptions, violation of which would cause the domain to be crashed.

Jan

#93 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 127 lines · Jan Beulich
Quoted 95 lines · Oleksii Kurochko
Quoted 94 lines · Oleksii Kurochko
Quoted 92 lines · Jan Beulich
Quoted 45 lines · Oleksii Kurochko
Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 24 lines · Jan Beulich
Quoted 21 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich
Quoted 5 lines · Oleksii Kurochko

+{
+ panic("%s: isn't implemented for now\n", __func__);
+
+ return false;
+}

For this function in particular, though: Besides the "p2me" in the name being somewhat odd (supposedly page table entries here are simply pte_t), how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):

        /*
         * In the case of the P2M, the valid bit is used for other purpose. Use
         * the type to check whether an entry is valid.
         */
        static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
        {
            return p2m_type_radix_get(p2m, pte) != p2m_invalid;
        }
Quoted 1 line

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

For P2M type checks please don't invent new naming, but use what both x86 and Arm are already using. Note how we already have p2m_is_valid() in that set. Just that it's not doing what you want here.

Hm, why not doing what I want? p2m_is_valid() verifies if P2M entry is valid. And in here it is checked if P2M pte is valid from P2M point of view by checking the type in radix tree and/or in reserved PTEs bits (just to remind we have only 2 free bits for type).

Quoted 8 lines

Because this is how it's defined on x86:

#define p2m_is_valid(_t)    (p2m_to_mask(_t) & \
                                (P2M_RAM_TYPES | p2m_to_mask(p2m_mmio_direct)))

I.e. more strict that simply "!= p2m_invalid". And I think such predicates would better be uniform across architectures, such that in principle they might also be usable in common code (as we already do with p2m_is_foreign()).

Quoted 7 lines

Yeah, Arm isn't so strict in definition of p2m_is_valid() and it seems like x86 and Arm have different understanding what is valid.

Except what mentioned in the comment that grant types aren't considered valid for x86 (and shouldn't be the same then for Arm?), it isn't clear why x86's p2m_is_valid() is stricter then Arm's one and if other arches should be also so strict.

Arm's p2m_is_valid() is entirely different (and imo misnamed, but arguably one could also consider x86'es to require a better name). It's a local helper, not a P2M type checking predicate. With that in mind, you may of course follow Arm's model, but in the longer run we may need to do something about the name collision then.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

The only use case I can think of is that the caller
might try to map the remaining GFNs again. But that doesn’t seem very useful, if|p2m_set_entry()| wasn’t able to map the full range, it likely indicates a serious issue, and retrying would probably result in the same error.

The same applies to rolling back the state. It wouldn’t be difficult to add a local array to track all modified PTEs and then use it to revert the state if needed. But again, what would the caller do after the rollback? At this point, it still seems like the best option is simply to|panic(). |

Basically, I don’t see or understand the cases where knowing how many GFNs were successfully mapped, or whether a rollback was performed, would really help — because in most cases, I don’t have a better option than just calling|panic()| at the end.

Quoted 7 lines
Quoted 3 lines

panic()-ing is of course only a last resort. Anything related to domain handling would better crash only the domain in question. And even that only if suitable error handling isn't possible.

And if there is no still any runnable domain available, for example, we are creating domain and some p2m mapping is called? Will it be enough just ignore to boot this domain? If yes, then it is enough to return only error code without returning how many GFNs were mapped or rollbacking as domain won't be ran anyway.

During domain creation all you need to do is return an error. But when you write a generic function that's also (going to be) used at domain runtime, you need to consider what to do there in case of partial success.

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

For example, if I call|map_regions_p2mt()| for an MMIO region described in a device tree node, and the mapping fails partway through, I’m left with two options: either ignore the device (if it's not essential for Xen or guest functionality) and continue
booting; in which case I’d need to perform a rollback, and simply knowing the number
of successfully mapped GFNs may not be enough or, more likely, just panic.

Well, no. For example, before even trying to map you could check that the range of P2M entries covered is all empty.

Could it be that they aren't all empty? Then it seems like we have overlapping and we can't just do a mapping, right?

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

Possibly that would simply mean to return an error, yes.

Quoted 2 lines

Won't be this procedure consume a lot of time as it is needed to go through each page tables for each entry.

Well, you're free to suggest a clean alternative without doing so.

I thought about dynamically allocating an array in p2m_set_entry(), where to save all changed PTEs, and then use it to roll back if __p2m_set_entry() returns rc != 0 ...

That's another possible source for failure, and such an allocation may end up being a rather big one.

Quoted 13 lines
Quoted 11 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

_Then_ you know how to correctly roll back.
And yes, doing so may not even require passing back information on how much of a region was successfully mapped.

If P2M entries were empty before start of the mapping then it is enough to just go through the same range (sgfn,nr,smfn) and just clean them, right?

Yes, what else would "roll back" mean in that case?

... If we know that the P2M entries were empty, then there's nothing else to be done, just clean PTE is needed to be done.
However, if the P2M entries weren’t empty (and I’m still not sure whether that’s a legal case), then rolling back would mean restoring their original state, the state they had before the P2M mapping procedure started.

Possible roll back is harder to implement as expected because there is a case where subtree could be freed:

Quoted 8 lines
      /*
       * Free the entry only if the original pte was valid and the base
       * is different (to avoid freeing when permission is changed).
       */
      if ( p2me_is_valid(p2m, orig_pte) &&
           !mfn_eq(pte_get_mfn(*entry), pte_get_mfn(orig_pte)) )
          p2m_free_subtree(p2m, orig_pte, level);

In this case then it will be needed to store the full subtree.

Quoted 3 lines

Right, which is why it may be desirable to limit the ability to update multiple entries in one go. Or work from certain assumptions, violation of which would cause the domain to be crashed.

It seems to me that the main issue with updating multiple entries in one go is the rollback mechanism in case of a partial mapping failure. (other issues? mapping could consume a lot of time so something should wait while allocation will end?) In my opinion, the rollback mechanism is quite complex to implement and could become a source of further failures. For example, most of the cases where p2m_set_entry() could fail are due to failure in mapping the page table (to allow Xen to walk through it) or failure in creating a new page table due to memory exhaustion. Then, during rollback, which might also require memory allocation, we could face the same memory shortage issue.
And what should be done in that case?

In my opinion, the best option is to simply return from p2m_set_entry() the number of successfully mapped GFNs (stored in rc which is returned by p2m_set_entry()) and let the caller decide how to handle the partial mapping:
1. If a partial mapping occurs during domain creation, we could just report that this domain can't be created and continue without it if there are other domains to start; otherwise, panic.
2. If a partial mapping occurs during the lifetime of a domain, for example, if the domain requests to map some memory, we return the number of successfully mapped GFNs and let the domain decide what to do: either remove the mappings or retry mapping the remaining part. However, I think there's not much value in retrying, since p2m_set_entry() is likely to fail again. So, perhaps the best course of action is to stop the domain altogether.
Does that make sense?

~ Oleksii

#94 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 143 lines · Oleksii Kurochko
Quoted 127 lines · Jan Beulich
Quoted 95 lines · Oleksii Kurochko
Quoted 94 lines · Oleksii Kurochko
Quoted 92 lines · Jan Beulich
Quoted 45 lines · Oleksii Kurochko
Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 24 lines · Jan Beulich
Quoted 21 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich

On 10.06.2025 15:05, Oleksii Kurochko wrote:
+{
+ panic("%s: isn't implemented for now\n", __func__);
+
+ return false;
+}
For this function in particular, though: Besides the "p2me" in the name
being somewhat odd (supposedly page table entries here are simply pte_t),
how is this going to be different from pte_is_valid()?

pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking what is a type stored in the radix tree (p2m->p2m_types):

        /*
         * In the case of the P2M, the valid bit is used for other purpose. Use
         * the type to check whether an entry is valid.
         */
        static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
        {
            return p2m_type_radix_get(p2m, pte) != p2m_invalid;
        }
Quoted 1 line

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

For P2M type checks please don't invent new naming, but use what both x86 and Arm are already using. Note how we already have p2m_is_valid() in that set. Just that it's not doing what you want here.

Hm, why not doing what I want? p2m_is_valid() verifies if P2M entry is valid. And in here it is checked if P2M pte is valid from P2M point of view by checking the type in radix tree and/or in reserved PTEs bits (just to remind we have only 2 free bits for type).

Quoted 8 lines

Because this is how it's defined on x86:

#define p2m_is_valid(_t)    (p2m_to_mask(_t) & \
                                (P2M_RAM_TYPES | p2m_to_mask(p2m_mmio_direct)))

I.e. more strict that simply "!= p2m_invalid". And I think such predicates would better be uniform across architectures, such that in principle they might also be usable in common code (as we already do with p2m_is_foreign()).

Quoted 7 lines

Yeah, Arm isn't so strict in definition of p2m_is_valid() and it seems like x86 and Arm have different understanding what is valid.

Except what mentioned in the comment that grant types aren't considered valid for x86 (and shouldn't be the same then for Arm?), it isn't clear why x86's p2m_is_valid() is stricter then Arm's one and if other arches should be also so strict.

Arm's p2m_is_valid() is entirely different (and imo misnamed, but arguably one could also consider x86'es to require a better name). It's a local helper, not a P2M type checking predicate. With that in mind, you may of course follow Arm's model, but in the longer run we may need to do something about the name collision then.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

The only use case I can think of is that the caller
might try to map the remaining GFNs again. But that doesn’t seem very useful, if|p2m_set_entry()| wasn’t able to map the full range, it likely indicates a serious issue, and retrying would probably result in the same error.

The same applies to rolling back the state. It wouldn’t be difficult to add a local array to track all modified PTEs and then use it to revert the state if needed. But again, what would the caller do after the rollback? At this point, it still seems like the best option is simply to|panic(). |

Basically, I don’t see or understand the cases where knowing how many GFNs were successfully mapped, or whether a rollback was performed, would really help — because in most cases, I don’t have a better option than just calling|panic()| at the end.

Quoted 7 lines
Quoted 3 lines

panic()-ing is of course only a last resort. Anything related to domain handling would better crash only the domain in question. And even that only if suitable error handling isn't possible.

And if there is no still any runnable domain available, for example, we are creating domain and some p2m mapping is called? Will it be enough just ignore to boot this domain? If yes, then it is enough to return only error code without returning how many GFNs were mapped or rollbacking as domain won't be ran anyway.

During domain creation all you need to do is return an error. But when you write a generic function that's also (going to be) used at domain runtime, you need to consider what to do there in case of partial success.

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

For example, if I call|map_regions_p2mt()| for an MMIO region described in a device tree node, and the mapping fails partway through, I’m left with two options: either ignore the device (if it's not essential for Xen or guest functionality) and continue
booting; in which case I’d need to perform a rollback, and simply knowing the number
of successfully mapped GFNs may not be enough or, more likely, just panic.

Well, no. For example, before even trying to map you could check that the range of P2M entries covered is all empty.

Could it be that they aren't all empty? Then it seems like we have overlapping and we can't just do a mapping, right?

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

Possibly that would simply mean to return an error, yes.

Quoted 2 lines

Won't be this procedure consume a lot of time as it is needed to go through each page tables for each entry.

Well, you're free to suggest a clean alternative without doing so.

I thought about dynamically allocating an array in p2m_set_entry(), where to save all changed PTEs, and then use it to roll back if __p2m_set_entry() returns rc != 0 ...

That's another possible source for failure, and such an allocation may end up being a rather big one.

Quoted 13 lines
Quoted 11 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

_Then_ you know how to correctly roll back.
And yes, doing so may not even require passing back information on how much of a region was successfully mapped.

If P2M entries were empty before start of the mapping then it is enough to just go through the same range (sgfn,nr,smfn) and just clean them, right?

Yes, what else would "roll back" mean in that case?

... If we know that the P2M entries were empty, then there's nothing else to be done, just clean PTE is needed to be done.
However, if the P2M entries weren’t empty (and I’m still not sure whether that’s a legal case), then rolling back would mean restoring their original state, the state they had before the P2M mapping procedure started.

Possible roll back is harder to implement as expected because there is a case where subtree could be freed:

Quoted 8 lines
      /*
       * Free the entry only if the original pte was valid and the base
       * is different (to avoid freeing when permission is changed).
       */
      if ( p2me_is_valid(p2m, orig_pte) &&
           !mfn_eq(pte_get_mfn(*entry), pte_get_mfn(orig_pte)) )
          p2m_free_subtree(p2m, orig_pte, level);

In this case then it will be needed to store the full subtree.

Quoted 3 lines

Right, which is why it may be desirable to limit the ability to update multiple entries in one go. Or work from certain assumptions, violation of which would cause the domain to be crashed.

It seems to me that the main issue with updating multiple entries in one go is the rollback mechanism in case of a partial mapping failure. (other issues? mapping could consume a lot of time so something should wait while allocation will end?) In my opinion, the rollback mechanism is quite complex to implement and could become a source of further failures. For example, most of the cases where p2m_set_entry() could fail are due to failure in mapping the page table (to allow Xen to walk through it) or failure in creating a new page table due to memory exhaustion. Then, during rollback, which might also require memory allocation, we could face the same memory shortage issue.
And what should be done in that case?

Quoted 6 lines

In my opinion, the best option is to simply return from p2m_set_entry() the number of successfully mapped GFNs (stored in rc which is returned by p2m_set_entry()) and let the caller decide how to handle the partial mapping:
1. If a partial mapping occurs during domain creation, we could just report that this

    domain can't be created and continue without it if there are other domains to start;
    otherwise, panic.

I don't see how panic()-ing is relevant here. That's to be decided (far) up the call stack.

Quoted 6 lines

2. If a partial mapping occurs during the lifetime of a domain, for example, if the domain requests to map some memory, we return the number of successfully mapped GFNs and let the domain decide what to do: either remove the mappings or retry mapping the remaining part. However, I think there's not much value in retrying, since p2m_set_entry() is likely to fail again. So, perhaps the best course of action is to stop the domain altogether.
Does that make sense?

Sure, why not. Provided you actually have a way to communicate back how much was mapped.

Jan

#95 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 141 lines · Jan Beulich
Quoted 140 lines · Oleksii Kurochko
Quoted 126 lines · Jan Beulich
Quoted 94 lines · Oleksii Kurochko
Quoted 93 lines · Oleksii Kurochko
Quoted 92 lines · Jan Beulich
Quoted 45 lines · Oleksii Kurochko
Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 24 lines · Jan Beulich
Quoted 22 lines · Oleksii Kurochko

On 7/1/25 3:49 PM, Jan Beulich wrote:
On 10.06.2025 15:05, Oleksii Kurochko wrote:
+{
+ panic("%s: isn't implemented for now\n", __func__);
+
+ return false;
+}
For this function in particular, though: Besides the "p2me" in the name
being somewhat odd (supposedly page table entries here are simply pte_t),
how is this going to be different from pte_is_valid()?
pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking
what is a type stored in the radix tree (p2m->p2m_types):
/*
* In the case of the P2M, the valid bit is used for other purpose. Use
* the type to check whether an entry is valid.
*/
static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
{
return p2m_type_radix_get(p2m, pte) != p2m_invalid;
}

It is done to track which page was modified by a guest.

But then (again) the name doesn't convey what the function does.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

For P2M type checks please don't invent new naming, but use what both x86 and Arm are already using. Note how we already have p2m_is_valid() in that set. Just that it's not doing what you want here.

Hm, why not doing what I want? p2m_is_valid() verifies if P2M entry is valid. And in here it is checked if P2M pte is valid from P2M point of view by checking the type in radix tree and/or in reserved PTEs bits (just to remind we have only 2 free bits for type).

Quoted 8 lines

Because this is how it's defined on x86:

#define p2m_is_valid(_t)    (p2m_to_mask(_t) & \
                                 (P2M_RAM_TYPES | p2m_to_mask(p2m_mmio_direct)))

I.e. more strict that simply "!= p2m_invalid". And I think such predicates would better be uniform across architectures, such that in principle they might also be usable in common code (as we already do with p2m_is_foreign()).

Quoted 7 lines

Yeah, Arm isn't so strict in definition of p2m_is_valid() and it seems like x86 and Arm have different understanding what is valid.

Except what mentioned in the comment that grant types aren't considered valid for x86 (and shouldn't be the same then for Arm?), it isn't clear why x86's p2m_is_valid() is stricter then Arm's one and if other arches should be also so strict.

Arm's p2m_is_valid() is entirely different (and imo misnamed, but arguably one could also consider x86'es to require a better name). It's a local helper, not a P2M type checking predicate. With that in mind, you may of course follow Arm's model, but in the longer run we may need to do something about the name collision then.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

The only use case I can think of is that the caller
might try to map the remaining GFNs again. But that doesn’t seem very useful, if|p2m_set_entry()| wasn’t able to map the full range, it likely indicates a serious issue, and retrying would probably result in the same error.

The same applies to rolling back the state. It wouldn’t be difficult to add a local array to track all modified PTEs and then use it to revert the state if needed. But again, what would the caller do after the rollback? At this point, it still seems like the best option is simply to|panic(). |

Basically, I don’t see or understand the cases where knowing how many GFNs were successfully mapped, or whether a rollback was performed, would really help — because in most cases, I don’t have a better option than just calling|panic()| at the end.

Quoted 7 lines
Quoted 3 lines

panic()-ing is of course only a last resort. Anything related to domain handling would better crash only the domain in question. And even that only if suitable error handling isn't possible.

And if there is no still any runnable domain available, for example, we are creating domain and some p2m mapping is called? Will it be enough just ignore to boot this domain? If yes, then it is enough to return only error code without returning how many GFNs were mapped or rollbacking as domain won't be ran anyway.

During domain creation all you need to do is return an error. But when you write a generic function that's also (going to be) used at domain runtime, you need to consider what to do there in case of partial success.

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

For example, if I call|map_regions_p2mt()| for an MMIO region described in a device tree node, and the mapping fails partway through, I’m left with two options: either ignore the device (if it's not essential for Xen or guest functionality) and continue
booting; in which case I’d need to perform a rollback, and simply knowing the number
of successfully mapped GFNs may not be enough or, more likely, just panic.

Well, no. For example, before even trying to map you could check that the range of P2M entries covered is all empty.

Could it be that they aren't all empty? Then it seems like we have overlapping and we can't just do a mapping, right?

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

Possibly that would simply mean to return an error, yes.

Quoted 2 lines

Won't be this procedure consume a lot of time as it is needed to go through each page tables for each entry.

Well, you're free to suggest a clean alternative without doing so.

I thought about dynamically allocating an array in p2m_set_entry(), where to save all changed PTEs, and then use it to roll back if __p2m_set_entry() returns rc != 0 ...

That's another possible source for failure, and such an allocation may end up being a rather big one.

Quoted 13 lines
Quoted 11 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

_Then_ you know how to correctly roll back.
And yes, doing so may not even require passing back information on how much of a region was successfully mapped.

If P2M entries were empty before start of the mapping then it is enough to just go through the same range (sgfn,nr,smfn) and just clean them, right?

Yes, what else would "roll back" mean in that case?

... If we know that the P2M entries were empty, then there's nothing else to be done, just clean PTE is needed to be done.
However, if the P2M entries weren’t empty (and I’m still not sure whether that’s a legal case), then rolling back would mean restoring their original state, the state they had before the P2M mapping procedure started.

Possible roll back is harder to implement as expected because there is a case where subtree could be freed:

Quoted 8 lines
       /*
        * Free the entry only if the original pte was valid and the base
        * is different (to avoid freeing when permission is changed).
        */
       if ( p2me_is_valid(p2m, orig_pte) &&
            !mfn_eq(pte_get_mfn(*entry), pte_get_mfn(orig_pte)) )
           p2m_free_subtree(p2m, orig_pte, level);

In this case then it will be needed to store the full subtree.

Quoted 3 lines

Right, which is why it may be desirable to limit the ability to update multiple entries in one go. Or work from certain assumptions, violation of which would cause the domain to be crashed.

It seems to me that the main issue with updating multiple entries in one go is the rollback mechanism in case of a partial mapping failure. (other issues? mapping could consume a lot of time so something should wait while allocation will end?) In my opinion, the rollback mechanism is quite complex to implement and could become a source of further failures. For example, most of the cases where p2m_set_entry() could fail are due to failure in mapping the page table (to allow Xen to walk through it) or failure in creating a new page table due to memory exhaustion. Then, during rollback, which might also require memory allocation, we could face the same memory shortage issue.
And what should be done in that case?

Quoted 8 lines
Quoted 6 lines

In my opinion, the best option is to simply return from p2m_set_entry() the number of successfully mapped GFNs (stored in rc which is returned by p2m_set_entry()) and let the caller decide how to handle the partial mapping:
1. If a partial mapping occurs during domain creation, we could just report that this

     domain can't be created and continue without it if there are other domains to start;
     otherwise, panic.

I don't see how panic()-ing is relevant here. That's to be decided (far) up the call stack.

So it's just a question of whether the caller should panic() or propagate the return value (error code) up the call stack.

For example, in case of domain construction return value is propogate almost to the top
of the stack:
p2m_set_entry(p2m_access_t a, p2m_type_t t, mfn_t smfn, unsigned long nr, gfn_t sgfn, struct p2m_domain * p2m) (/run/media/ok/blue_disk//xen/xen/arch/riscv/p2m.c:1005) p2m_insert_mapping(struct domain * d, gfn_t start_gfn, unsigned long nr, mfn_t mfn, p2m_type_t t) (/run/media/ok/blue_disk//xen/xen/arch/riscv/p2m.c:1055) guest_physmap_add_entry(struct domain * d, gfn_t gfn, mfn_t mfn, unsigned long page_order, p2m_type_t t) (/run/media/ok/blue_disk//xen/xen/arch/riscv/p2m.c:1076) guest_physmap_add_page(unsigned int page_order, struct domain * d) (/run/media/ok/blue_disk//xen/xen/arch/riscv/include/asm/p2m.h:152) guest_map_pages(struct domain * d, struct page_info * pg, unsigned int order, void * extra) (/run/media/ok/blue_disk//xen/xen/common/device-tree/domain-build.c:63) allocate_domheap_memory(struct domain * d, paddr_t tot_size, alloc_domheap_mem_cb cb, void * extra) (/run/media/ok/blue_disk//xen/xen/common/device-tree/domain-build.c:47) allocate_bank_memory(struct kernel_info * kinfo, gfn_t sgfn, paddr_t tot_size) (/run/media/ok/blue_disk//xen/xen/common/device-tree/domain-build.c:99) allocate_memory(struct domain * d, struct kernel_info * kinfo) (/run/media/ok/blue_disk//xen/xen/include/xen/mm-frame.h:43) construct_domU(struct domain * d, const struct dt_device_node * node) (/run/media/ok/blue_disk//xen/xen/common/device-tree/dom0less-build.c:835) create_domUs() (/run/media/ok/blue_disk//xen/xen/common/device-tree/dom0less-build.c:1019)
start_xen(unsigned long bootcpu_id, paddr_t dtb_addr) (/run/media/ok/blue_disk//xen/xen/arch/riscv/setup.c:296)
start() (/run/media/ok/blue_disk//xen/xen/arch/riscv/riscv64/head.S:61)

And panic() almost at the end:
rc = construct_domU(d, node);
if ( rc )
panic("Could not set up domain %s (rc = %d)\n",
dt_node_name(node), rc);

Quoted 9 lines
Quoted 7 lines

2. If a partial mapping occurs during the lifetime of a domain, for example, if the domain requests to map some memory, we return the number of successfully mapped GFNs and let the domain decide what to do: either remove the mappings or retry mapping the remaining part. However, I think there's not much value in retrying, since p2m_set_entry() is likely to fail again. So, perhaps the best course of action is to stop the domain altogether.
Does that make sense?

Sure, why not. Provided you actually have a way to communicate back how much was mapped.

I was thinking of simply returning it as the return value. This way, a return value of 0 would indicate that everything was mapped successfully, while a value greater than 0 would indicate how many GFNs were successfully mapped. And negative value if nothing was be mapped at all:
static int p2m_set_entry(struct p2m_domain *p2m,
gfn_t sgfn,
unsigned long nr,
mfn_t smfn,
p2m_type_t t,
p2m_access_t a)

   {
      int rc = 0;
      unsigned int i;
  
      ...
  
      for ( i = 1; i <= nr; i++ )
      {
          ...
          rc = __p2m_set_entry(p2m, sgfn, order, smfn, t, a);
          if ( rc )
              break;
          ...
      }
      
      return i == nr ? 0 : i ?: rc;

}

~ Oleksii

#96 lore raw

Re: [PATCH v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry()

Quoted 170 lines · Oleksii Kurochko
Quoted 141 lines · Jan Beulich
Quoted 140 lines · Oleksii Kurochko
Quoted 126 lines · Jan Beulich
Quoted 94 lines · Oleksii Kurochko
Quoted 93 lines · Oleksii Kurochko
Quoted 92 lines · Jan Beulich
Quoted 45 lines · Oleksii Kurochko
Quoted 36 lines · Jan Beulich
Quoted 27 lines · Oleksii Kurochko
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Oleksii Kurochko
Quoted 24 lines · Jan Beulich

On 04.07.2025 17:01, Oleksii Kurochko wrote:
On 7/1/25 3:49 PM, Jan Beulich wrote:
On 10.06.2025 15:05, Oleksii Kurochko wrote:
+{
+ panic("%s: isn't implemented for now\n", __func__);
+
+ return false;
+}
For this function in particular, though: Besides the "p2me" in the name
being somewhat odd (supposedly page table entries here are simply pte_t),
how is this going to be different from pte_is_valid()?
pte_is_valid() is checking a real bit of PTE, but p2me_is_valid() is checking
what is a type stored in the radix tree (p2m->p2m_types):
/*
* In the case of the P2M, the valid bit is used for other purpose. Use
* the type to check whether an entry is valid.
*/
static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
{
return p2m_type_radix_get(p2m, pte) != p2m_invalid;
}

It is done to track which page was modified by a guest.
But then (again) the name doesn't convey what the function does.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Then probably p2me_type_is_valid(struct p2m_domain *p2m, pte_t pte) would better.

For P2M type checks please don't invent new naming, but use what both x86 and Arm are already using. Note how we already have p2m_is_valid() in that set. Just that it's not doing what you want here.

Hm, why not doing what I want? p2m_is_valid() verifies if P2M entry is valid. And in here it is checked if P2M pte is valid from P2M point of view by checking the type in radix tree and/or in reserved PTEs bits (just to remind we have only 2 free bits for type).

Quoted 8 lines

Because this is how it's defined on x86:

#define p2m_is_valid(_t)    (p2m_to_mask(_t) & \
                                 (P2M_RAM_TYPES | p2m_to_mask(p2m_mmio_direct)))

I.e. more strict that simply "!= p2m_invalid". And I think such predicates would better be uniform across architectures, such that in principle they might also be usable in common code (as we already do with p2m_is_foreign()).

Quoted 7 lines

Yeah, Arm isn't so strict in definition of p2m_is_valid() and it seems like x86 and Arm have different understanding what is valid.

Except what mentioned in the comment that grant types aren't considered valid for x86 (and shouldn't be the same then for Arm?), it isn't clear why x86's p2m_is_valid() is stricter then Arm's one and if other arches should be also so strict.

Arm's p2m_is_valid() is entirely different (and imo misnamed, but arguably one could also consider x86'es to require a better name). It's a local helper, not a P2M type checking predicate. With that in mind, you may of course follow Arm's model, but in the longer run we may need to do something about the name collision then.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

The only use case I can think of is that the caller
might try to map the remaining GFNs again. But that doesn’t seem very useful, if|p2m_set_entry()| wasn’t able to map the full range, it likely indicates a serious issue, and retrying would probably result in the same error.

The same applies to rolling back the state. It wouldn’t be difficult to add a local array to track all modified PTEs and then use it to revert the state if needed. But again, what would the caller do after the rollback? At this point, it still seems like the best option is simply to|panic(). |

Basically, I don’t see or understand the cases where knowing how many GFNs were successfully mapped, or whether a rollback was performed, would really help — because in most cases, I don’t have a better option than just calling|panic()| at the end.

Quoted 7 lines
Quoted 3 lines

panic()-ing is of course only a last resort. Anything related to domain handling would better crash only the domain in question. And even that only if suitable error handling isn't possible.

And if there is no still any runnable domain available, for example, we are creating domain and some p2m mapping is called? Will it be enough just ignore to boot this domain? If yes, then it is enough to return only error code without returning how many GFNs were mapped or rollbacking as domain won't be ran anyway.

During domain creation all you need to do is return an error. But when you write a generic function that's also (going to be) used at domain runtime, you need to consider what to do there in case of partial success.

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines

For example, if I call|map_regions_p2mt()| for an MMIO region described in a device tree node, and the mapping fails partway through, I’m left with two options: either ignore the device (if it's not essential for Xen or guest functionality) and continue
booting; in which case I’d need to perform a rollback, and simply knowing the number
of successfully mapped GFNs may not be enough or, more likely, just panic.

Well, no. For example, before even trying to map you could check that the range of P2M entries covered is all empty.

Could it be that they aren't all empty? Then it seems like we have overlapping and we can't just do a mapping, right?

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

Possibly that would simply mean to return an error, yes.

Quoted 2 lines

Won't be this procedure consume a lot of time as it is needed to go through each page tables for each entry.

Well, you're free to suggest a clean alternative without doing so.

I thought about dynamically allocating an array in p2m_set_entry(), where to save all changed PTEs, and then use it to roll back if __p2m_set_entry() returns rc != 0 ...

That's another possible source for failure, and such an allocation may end up being a rather big one.

Quoted 13 lines
Quoted 11 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

_Then_ you know how to correctly roll back.
And yes, doing so may not even require passing back information on how much of a region was successfully mapped.

If P2M entries were empty before start of the mapping then it is enough to just go through the same range (sgfn,nr,smfn) and just clean them, right?

Yes, what else would "roll back" mean in that case?

... If we know that the P2M entries were empty, then there's nothing else to be done, just clean PTE is needed to be done.
However, if the P2M entries weren’t empty (and I’m still not sure whether that’s a legal case), then rolling back would mean restoring their original state, the state they had before the P2M mapping procedure started.

Possible roll back is harder to implement as expected because there is a case where subtree could be freed:

Quoted 8 lines
       /*
        * Free the entry only if the original pte was valid and the base
        * is different (to avoid freeing when permission is changed).
        */
       if ( p2me_is_valid(p2m, orig_pte) &&
            !mfn_eq(pte_get_mfn(*entry), pte_get_mfn(orig_pte)) )
           p2m_free_subtree(p2m, orig_pte, level);

In this case then it will be needed to store the full subtree.

Quoted 3 lines

Right, which is why it may be desirable to limit the ability to update multiple entries in one go. Or work from certain assumptions, violation of which would cause the domain to be crashed.

It seems to me that the main issue with updating multiple entries in one go is the rollback mechanism in case of a partial mapping failure. (other issues? mapping could consume a lot of time so something should wait while allocation will end?) In my opinion, the rollback mechanism is quite complex to implement and could become a source of further failures. For example, most of the cases where p2m_set_entry() could fail are due to failure in mapping the page table (to allow Xen to walk through it) or failure in creating a new page table due to memory exhaustion. Then, during rollback, which might also require memory allocation, we could face the same memory shortage issue.
And what should be done in that case?

Quoted 8 lines
Quoted 6 lines

In my opinion, the best option is to simply return from p2m_set_entry() the number of successfully mapped GFNs (stored in rc which is returned by p2m_set_entry()) and let the caller decide how to handle the partial mapping:
1. If a partial mapping occurs during domain creation, we could just report that this

     domain can't be created and continue without it if there are other domains to start;
     otherwise, panic.

I don't see how panic()-ing is relevant here. That's to be decided (far) up the call stack.

So it's just a question of whether the caller should panic() or propagate the return value (error code) up the call stack.

For example, in case of domain construction return value is propogate almost to the top
of the stack:
p2m_set_entry(p2m_access_t a, p2m_type_t t, mfn_t smfn, unsigned long nr, gfn_t sgfn, struct p2m_domain * p2m) (/run/media/ok/blue_disk//xen/xen/arch/riscv/p2m.c:1005) p2m_insert_mapping(struct domain * d, gfn_t start_gfn, unsigned long nr, mfn_t mfn, p2m_type_t t) (/run/media/ok/blue_disk//xen/xen/arch/riscv/p2m.c:1055) guest_physmap_add_entry(struct domain * d, gfn_t gfn, mfn_t mfn, unsigned long page_order, p2m_type_t t) (/run/media/ok/blue_disk//xen/xen/arch/riscv/p2m.c:1076) guest_physmap_add_page(unsigned int page_order, struct domain * d) (/run/media/ok/blue_disk//xen/xen/arch/riscv/include/asm/p2m.h:152) guest_map_pages(struct domain * d, struct page_info * pg, unsigned int order, void * extra) (/run/media/ok/blue_disk//xen/xen/common/device-tree/domain-build.c:63) allocate_domheap_memory(struct domain * d, paddr_t tot_size, alloc_domheap_mem_cb cb, void * extra) (/run/media/ok/blue_disk//xen/xen/common/device-tree/domain-build.c:47) allocate_bank_memory(struct kernel_info * kinfo, gfn_t sgfn, paddr_t tot_size) (/run/media/ok/blue_disk//xen/xen/common/device-tree/domain-build.c:99) allocate_memory(struct domain * d, struct kernel_info * kinfo) (/run/media/ok/blue_disk//xen/xen/include/xen/mm-frame.h:43) construct_domU(struct domain * d, const struct dt_device_node * node) (/run/media/ok/blue_disk//xen/xen/common/device-tree/dom0less-build.c:835) create_domUs() (/run/media/ok/blue_disk//xen/xen/common/device-tree/dom0less-build.c:1019)
start_xen(unsigned long bootcpu_id, paddr_t dtb_addr) (/run/media/ok/blue_disk//xen/xen/arch/riscv/setup.c:296)
start() (/run/media/ok/blue_disk//xen/xen/arch/riscv/riscv64/head.S:61)

Quoted 5 lines

And panic() almost at the end:
rc = construct_domU(d, node);
if ( rc )
panic("Could not set up domain %s (rc = %d)\n",
dt_node_name(node), rc);

Which is what is wanted, imo.

Jan

#99 lore raw

Re: [PATCH v2 12/17] xen/riscv: Implement p2m_free_entry() and related helpers

In reply to Jan Beulich
Quoted 24 lines · Jan Beulich
Quoted 11 lines · Oleksii Kurochko

This patch introduces a working implementation of p2m_free_entry() for RISC-V based on ARM's implementation of p2m_free_entry(), enabling proper cleanup of page table entries in the P2M (physical-to-machine) mapping.

Only few things are changed:
- Use p2m_force_flush_sync() instead of p2m_tlb_flush_sync() as latter isn't implemented on RISC-V.
- Introduce and use p2m_type_radix_get() to get a type of p2m entry as RISC-V's PTE doesn't have enough space to store all necessary types so a type is stored in a radix tree.

Quoted 12 lines

Key additions include:
- p2m_free_entry(): Recursively frees page table entries at all levels. It handles both regular and superpage mappings and ensures that TLB entries are flushed before freeing intermediate tables.
- p2m_put_page() and helpers:
- p2m_put_4k_page(): Clears GFN from xenheap pages if applicable.
- p2m_put_2m_superpage(): Releases foreign page references in a 2MB superpage.
- p2m_type_radix_get(): Extracts the stored p2m_type from the radix tree using the PTE.
- p2m_free_page(): Returns a page either to the domain's freelist or to the domheap, depending on whether the domain is hardware-backed.

Quoted 1 line

What is "hardware-backed"?

It means basically hardware domain, i.e. DOM0.

Quoted 9 lines

xen/arch/riscv/p2m.c static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) · line 348

Quoted 7 lines
xen/arch/riscv/p2m.c +1 -0
@@ -345,11 +345,33 @@ static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn)
      return __map_domain_page(p2m->root + root_table_indx);
  }
 
+static p2m_type_t p2m_type_radix_get(struct p2m_domain *p2m, pte_t pte)

Does it matter to callers that ...

Quoted 14 lines

xen/arch/riscv/p2m.c static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) · line 359

Quoted 11 lines
xen/arch/riscv/p2m.c +11 -0
+{
+    void *ptr;
+    gfn_t gfn = mfn_to_gfn(p2m->domain, mfn_from_pte(pte));
+
+    ptr = radix_tree_lookup(&p2m->p2m_type, gfn_x(gfn));
+
+    if ( !ptr )
+        return p2m_invalid;
+
+    return radix_tree_ptr_to_int(ptr);
+}

... this is a radix tree lookup? IOW does "radix" need to be part of the function name? Also "get" may want to move forward in the name, to better match the naming of other functions.

Agree, it doesn't really matter, so I will rename it.

Quoted 10 lines

xen/arch/riscv/p2m.c static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) · line 368

Quoted 9 lines
xen/arch/riscv/p2m.c +6 -1
+/*
+ * In the case of the P2M, the valid bit is used for other purpose. Use
+ * the type to check whether an entry is valid.
+ */
  static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
  {
-    panic("%s: isn't implemented for now\n", __func__);
+    return p2m_type_radix_get(p2m, pte) != p2m_invalid;
+}

No checking of the valid bit?

As mentioned in the comment, only the P2M type should be checked, since the valid bit is used for other purposes we discussed earlier, for example, to track whether pages were accessed by a guest domain, or to support certain table invalidation optimizations (1) and (2).
So, in this case, we only need to consider whether the entry is invalid from the P2M perspective.

(1)https://github.com/xen-project/xen/blob/19772b67/xen/arch/arm/mmu/p2m.c#L1245 (2)https://github.com/xen-project/xen/blob/19772b67/xen/arch/arm/mmu/p2m.c#L1386

Quoted 4 lines
Quoted 4 lines
xen/arch/riscv/p2m.c +0 -0
@@ -404,11 +426,127 @@ static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl,
      return GUEST_TABLE_MAP_NONE;
  }
 
Quoted 11 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 436

Quoted 8 lines
xen/arch/riscv/p2m.c +8 -0
+static void p2m_put_foreign_page(struct page_info *pg)
+{
+    /*
+     * It's safe to do the put_page here because page_alloc will
+     * flush the TLBs if the page is reallocated before the end of
+     * this loop.
+     */
+    put_page(pg);

Is the comment really true? The page allocator will flush the normal TLBs, but not the stage-2 ones. Yet those are what you care about here, aiui.

In alloc_heap_pages():
...
if ( need_tlbflush )
filtered_flush_tlb_mask(tlbflush_timestamp);
...

filtered_flush_tlb_mask() calls arch_flush_tlb_mask().

and arch_flush_tlb_mask(), at least, on Arm (I haven't checked x86) is implented as:
void arch_flush_tlb_mask(const cpumask_t *mask)

  {
      /* No need to IPI other processors on ARM, the processor takes care of it. */
      flush_all_guests_tlb();

}

So it flushes stage-2 TLB.

Quoted 11 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 446

Quoted 9 lines
xen/arch/riscv/p2m.c +8 -0
+/* Put any references on the single 4K page referenced by mfn. */
+static void p2m_put_4k_page(mfn_t mfn, p2m_type_t type)
+{
+    /* TODO: Handle other p2m types */
+
+    /* Detect the xenheap page and mark the stored GFN as invalid. */
+    if ( p2m_is_ram(type) && is_xen_heap_mfn(mfn) )
+        page_set_xenheap_gfn(mfn_to_page(mfn), INVALID_GFN);

Is this a valid thing to do? How do you make sure the respective uses (in gnttab's shared and status page arrays) are / were also removed?

As grant table frame GFN is stored directly in struct page_info instead of keeping it in standalone status/shared arrays, thereby there is no need for status/shared arrays.

Quoted 17 lines
Quoted 17 lines
xen/arch/riscv/p2m.c +16 -0
+}
+
+/* Put any references on the superpage referenced by mfn. */
+static void p2m_put_2m_superpage(mfn_t mfn, p2m_type_t type)
+{
+    struct page_info *pg;
+    unsigned int i;
+
+    ASSERT(mfn_valid(mfn));
+
+    pg = mfn_to_page(mfn);
+
+    for ( i = 0; i < XEN_PT_ENTRIES; i++, pg++ )
+        p2m_put_foreign_page(pg);
+}
+
Quoted 8 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 468

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+/* Put any references on the page referenced by pte. */
+static void p2m_put_page(struct p2m_domain *p2m, const pte_t pte,
+                         unsigned int level)
+{
+    mfn_t mfn = pte_get_mfn(pte);
+    p2m_type_t p2m_type = p2m_type_radix_get(p2m, pte);

This gives you the type of the 1st page. What guarantees that all other pages in a superpage are of the exact same type?

Doesn't superpage mean that all the 4KB pages within that superpage have the same type and contiguous in memory?

Quoted 10 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 478

Quoted 10 lines
xen/arch/riscv/p2m.c +9 -0
+    ASSERT(p2me_is_valid(p2m, pte));
+
+    /*
+     * TODO: Currently we don't handle level 2 super-page, Xen is not
+     * preemptible and therefore some work is needed to handle such
+     * superpages, for which at some point Xen might end up freeing memory
+     * and therefore for such a big mapping it could end up in a very long
+     * operation.
+     */
Quoted 2 lines

This is pretty unsatisfactory. Imo, if you don't deal with that right away, you're setting yourself up for a significant re-write.

ARM leaves with that for a long time and it seems like it isn't a big issue for it. And considering that frametable supports only 4Kb page granularity such big mappings could lead to long operations during memory freeing.
And 1gb mapping isn't used for

Quoted 6 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 482

Quoted 5 lines
xen/arch/riscv/p2m.c +4 -0
+    if ( level == 1 )
+        return p2m_put_2m_superpage(mfn, p2m_type);
+    else if ( level == 0 )
+        return p2m_put_4k_page(mfn, p2m_type);

Use switch() right away?

It could be, I think that no big difference at the moment, at least. But I am okay to rework it.

Quoted 9 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 488

Quoted 7 lines
xen/arch/riscv/p2m.c +6 -0
+}
+
+static void p2m_free_page(struct domain *d, struct page_info *pg)
+{
+    if ( is_hardware_domain(d) )
+        free_domheap_page(pg);

Why's the hardware domain different here? It should have a pool just like all other domains have.

Hardware domain (dom0) should be no limit in the number of pages that can be allocated, so allocate p2m pages for hardware domain is done from heap.

An idea of p2m pool is to provide a way how to put clear limit and amount to the p2m allocation.

Quoted 16 lines
Quoted 16 lines
xen/arch/riscv/p2m.c +10 -1
+    else
+    {
+        spin_lock(&d->arch.paging.lock);
+        page_list_add_tail(pg, &d->arch.paging.p2m_freelist);
+        spin_unlock(&d->arch.paging.lock);
+    }
+}
+
  /* Free pte sub-tree behind an entry */
  static void p2m_free_entry(struct p2m_domain *p2m,
                             pte_t entry, unsigned int level)
  {
-    panic("%s: hasn't been implemented yet\n", __func__);
+    unsigned int i;
+    pte_t *table;
Quoted 8 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 508

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+    mfn_t mfn;
+    struct page_info *pg;
+
+    /* Nothing to do if the entry is invalid. */
+    if ( !p2me_is_valid(p2m, entry) )
+        return;

Does this actually apply to intermediate page tables (which you handle later in the function), when that's (only) a P2M type check?

Yes, any PTE should have V bit set to 1, so from P2M perspective it also should be, at least, not equal to p2m_invalid.

~ Oleksii

#100 lore raw

Re: [PATCH v2 12/17] xen/riscv: Implement p2m_free_entry() and related helpers

Quoted 20 lines · Oleksii Kurochko
Quoted 11 lines · Jan Beulich

xen/arch/riscv/p2m.c static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) · line 368

Quoted 9 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +6 -1
+/*
+ * In the case of the P2M, the valid bit is used for other purpose. Use
+ * the type to check whether an entry is valid.
+ */
  static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
  {
-    panic("%s: isn't implemented for now\n", __func__);
+    return p2m_type_radix_get(p2m, pte) != p2m_invalid;
+}

No checking of the valid bit?

As mentioned in the comment, only the P2M type should be checked, since the valid bit is used for other purposes we discussed earlier, for example, to track whether pages were accessed by a guest domain, or to support certain table invalidation optimizations (1) and (2).
So, in this case, we only need to consider whether the entry is invalid from the P2M perspective.

Quoted 2 lines

(1)https://github.com/xen-project/xen/blob/19772b67/xen/arch/arm/mmu/p2m.c#L1245 (2)https://github.com/xen-project/xen/blob/19772b67/xen/arch/arm/mmu/p2m.c#L1386

And there can be e.g. entries with the valid bit set and the type being p2m_invalid? IOW there's no short-circuiting possible in any of the possible cases, avoiding the radix tree lookup in at least some of the cases?

Quoted 26 lines
Quoted 4 lines
Quoted 4 lines
xen/arch/riscv/p2m.c +0 -0
@@ -404,11 +426,127 @@ static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl,
      return GUEST_TABLE_MAP_NONE;
  }
 
Quoted 11 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 436

Quoted 8 lines
xen/arch/riscv/p2m.c +8 -0
+static void p2m_put_foreign_page(struct page_info *pg)
+{
+    /*
+     * It's safe to do the put_page here because page_alloc will
+     * flush the TLBs if the page is reallocated before the end of
+     * this loop.
+     */
+    put_page(pg);

Is the comment really true? The page allocator will flush the normal TLBs, but not the stage-2 ones. Yet those are what you care about here, aiui.

In alloc_heap_pages():
...
if ( need_tlbflush )
filtered_flush_tlb_mask(tlbflush_timestamp);
...

filtered_flush_tlb_mask() calls arch_flush_tlb_mask().

and arch_flush_tlb_mask(), at least, on Arm (I haven't checked x86) is implented as:

Quoted 7 lines
   void arch_flush_tlb_mask(const cpumask_t *mask)
   {
       /* No need to IPI other processors on ARM, the processor takes care of it. */
       flush_all_guests_tlb();
   }

So it flushes stage-2 TLB.

Hmm, okay. And I take it you have the same plan on RISC-V? What I'd like to ask for, though, is that the comment (also) mentions where that (guest) flushing actually happens. That's not in page_alloc.c, and it also wasn't originally intended for guest TLBs to also be flushed from there (as x86 is where the flush avoidance machinery originates, which Arm and now also RISC-V don't really use).

Quoted 11 lines
Quoted 10 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 446

Quoted 8 lines
xen/arch/riscv/p2m.c +8 -0
+/* Put any references on the single 4K page referenced by mfn. */
+static void p2m_put_4k_page(mfn_t mfn, p2m_type_t type)
+{
+    /* TODO: Handle other p2m types */
+
+    /* Detect the xenheap page and mark the stored GFN as invalid. */
+    if ( p2m_is_ram(type) && is_xen_heap_mfn(mfn) )
+        page_set_xenheap_gfn(mfn_to_page(mfn), INVALID_GFN);

Is this a valid thing to do? How do you make sure the respective uses (in gnttab's shared and status page arrays) are / were also removed?

Quoted 3 lines

As grant table frame GFN is stored directly in struct page_info instead of keeping it in standalone status/shared arrays, thereby there is no need for status/shared arrays.

I fear I don't follow. Looking at Arm's header (which I understand you derive from), I see

#define gnttab_shared_page(t, i) virt_to_page((t)->shared_raw[i])

#define gnttab_status_page(t, i) virt_to_page((t)->status[i])

Are you intending to do things differently?

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
xen/arch/riscv/p2m.c +14 -0
+/* Put any references on the superpage referenced by mfn. */
+static void p2m_put_2m_superpage(mfn_t mfn, p2m_type_t type)
+{
+    struct page_info *pg;
+    unsigned int i;
+
+    ASSERT(mfn_valid(mfn));
+
+    pg = mfn_to_page(mfn);
+
+    for ( i = 0; i < XEN_PT_ENTRIES; i++, pg++ )
+        p2m_put_foreign_page(pg);
+}
+
Quoted 8 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 468

Quoted 6 lines
xen/arch/riscv/p2m.c +6 -0
+/* Put any references on the page referenced by pte. */
+static void p2m_put_page(struct p2m_domain *p2m, const pte_t pte,
+                         unsigned int level)
+{
+    mfn_t mfn = pte_get_mfn(pte);
+    p2m_type_t p2m_type = p2m_type_radix_get(p2m, pte);

This gives you the type of the 1st page. What guarantees that all other pages in a superpage are of the exact same type?

Quoted 2 lines

Doesn't superpage mean that all the 4KB pages within that superpage have the same type and contiguous in memory?

If the mapping is a super-page one - yes. Yet I see nothing super-page-ish here.

Quoted 8 lines
Quoted 5 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 482

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+    if ( level == 1 )
+        return p2m_put_2m_superpage(mfn, p2m_type);
+    else if ( level == 0 )
+        return p2m_put_4k_page(mfn, p2m_type);

Use switch() right away?

It could be, I think that no big difference at the moment, at least. But I am okay to rework it.

If you don't want to use switch() here, then my other style nit would need giving: Please avoid "else" in situations like this.

Quoted 7 lines
Quoted 6 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 488

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+static void p2m_free_page(struct domain *d, struct page_info *pg)
+{
+    if ( is_hardware_domain(d) )
+        free_domheap_page(pg);

Why's the hardware domain different here? It should have a pool just like all other domains have.

Quoted 5 lines

Hardware domain (dom0) should be no limit in the number of pages that can be allocated, so allocate p2m pages for hardware domain is done from heap.

An idea of p2m pool is to provide a way how to put clear limit and amount to the p2m allocation.

Well, we had been there on another thread, and I outlined how I think Dom0 may want handling.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
xen/arch/riscv/p2m.c +8 -1
  /* Free pte sub-tree behind an entry */
  static void p2m_free_entry(struct p2m_domain *p2m,
                             pte_t entry, unsigned int level)
  {
-    panic("%s: hasn't been implemented yet\n", __func__);
+    unsigned int i;
+    pte_t *table;
+    mfn_t mfn;
+    struct page_info *pg;
+
+    /* Nothing to do if the entry is invalid. */
+    if ( !p2me_is_valid(p2m, entry) )
+        return;
Quoted 5 lines
Quoted 2 lines

Does this actually apply to intermediate page tables (which you handle later in the function), when that's (only) a P2M type check?

Yes, any PTE should have V bit set to 1, so from P2M perspective it also should be, at least, not equal to p2m_invalid.

I don't follow. Where would that type be set? The radix tree being GFN- indexed, you would need to "invent" a GFN for every intermediate page table, just to be able to (legitimately) invoke the type retrieval function. Maybe you mean to leverage that (now, i.e. post-v2) you encode some of the types directly in the PTE, and p2m_invalid may be one of them. But that wasn't the case in the v2 submission, and hence the code looked wrong to me. Which in turn suggests that at least some better commentary is going to be needed, maybe even some BUILD_BUG_ON().

Jan

#101 lore raw

Re: [PATCH v2 12/17] xen/riscv: Implement p2m_free_entry() and related helpers

Quoted 20 lines · Jan Beulich
Quoted 19 lines · Oleksii Kurochko
Quoted 11 lines · Jan Beulich

xen/arch/riscv/p2m.c static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) · line 368

Quoted 9 lines · Oleksii Kurochko
xen/arch/riscv/p2m.c +6 -1
+/*
+ * In the case of the P2M, the valid bit is used for other purpose. Use
+ * the type to check whether an entry is valid.
+ */
   static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte)
   {
-    panic("%s: isn't implemented for now\n", __func__);
+    return p2m_type_radix_get(p2m, pte) != p2m_invalid;
+}

No checking of the valid bit?

As mentioned in the comment, only the P2M type should be checked, since the valid bit is used for other purposes we discussed earlier, for example, to track whether pages were accessed by a guest domain, or to support certain table invalidation optimizations (1) and (2).
So, in this case, we only need to consider whether the entry is invalid from the P2M perspective.

Quoted 4 lines
Quoted 2 lines

(1)https://github.com/xen-project/xen/blob/19772b67/xen/arch/arm/mmu/p2m.c#L1245 (2)https://github.com/xen-project/xen/blob/19772b67/xen/arch/arm/mmu/p2m.c#L1386

And there can be e.g. entries with the valid bit set and the type being p2m_invalid?

It shouldn't be so, at least, at the moment, I don't know such cases.

Quoted 3 lines

IOW there's no short-circuiting possible in any of the possible cases, avoiding the radix tree lookup in at least some of the cases?

Yes, I’ve implemented such optimization. I started using two free bits in the PTE for some “popular” types:
static p2m_type_t p2m_get_type(struct p2m_domain *p2m, pte_t pte)

  {
      p2m_type_t type = MASK_EXTR(pte.pte, P2M_TYPE_PTE_BITS_MASK);
  
      if ( type == p2m_device_tree_type )
      {
	  ...
          ptr = radix_tree_lookup(&p2m->p2m_types, gfn_x(gfn));
          ...
          return radix_tree_ptr_to_int(ptr);
      }
  
      return type;

}

  /*
   * In the case of the P2M, the valid bit is used for other purpose. Use
   * the type to check whether an entry is valid.
   */
  static inline bool p2m_is_valid(struct p2m_domain *p2m, pte_t pte)
  {
      return p2m_get_type(p2m, pte) != p2m_invalid;
  }

But thanks to your reply, I realized that in the case of|p2m_is_valid()|, the implementation could be simplified further to:
/*
* In the case of the P2M, the valid bit is used for other purpose. Use
* the type to check whether an entry is valid.
*/
static inline bool p2m_is_valid(struct p2m_domain *p2m, pte_t pte)

  {
      return MASK_EXTR(pte.pte, P2M_TYPE_PTE_BITS_MASK) != p2m_invalid;

}

As we care here only about whether the type is|p2m_invalid| or not, and we don’t need the specific type here if it’s not|p2m_invalid|.

Quoted 32 lines
Quoted 32 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/riscv/p2m.c +0 -0
@@ -404,11 +426,127 @@ static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl,
       return GUEST_TABLE_MAP_NONE;
   }
 
Quoted 11 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 436

Quoted 8 lines
xen/arch/riscv/p2m.c +8 -0
+static void p2m_put_foreign_page(struct page_info *pg)
+{
+    /*
+     * It's safe to do the put_page here because page_alloc will
+     * flush the TLBs if the page is reallocated before the end of
+     * this loop.
+     */
+    put_page(pg);

Is the comment really true? The page allocator will flush the normal TLBs, but not the stage-2 ones. Yet those are what you care about here, aiui.

In alloc_heap_pages():

   ...
       if ( need_tlbflush )
          filtered_flush_tlb_mask(tlbflush_timestamp);
   ...

filtered_flush_tlb_mask() calls arch_flush_tlb_mask().

and arch_flush_tlb_mask(), at least, on Arm (I haven't checked x86) is implented as:

    void arch_flush_tlb_mask(const cpumask_t *mask)
    {
        /* No need to IPI other processors on ARM, the processor takes care of it. */
        flush_all_guests_tlb();
    }
Quoted 2 lines
Quoted 1 line

So it flushes stage-2 TLB.

Hmm, okay. And I take it you have the same plan on RISC-V?

Yes, there is such a plan.

Quoted 6 lines

What I'd like to
ask for, though, is that the comment (also) mentions where that (guest) flushing actually happens. That's not in page_alloc.c, and it also wasn't originally intended for guest TLBs to also be flushed from there (as x86 is where the flush avoidance machinery originates, which Arm and now also RISC-V don't really use).

Sure, it makes sense to update the comment.

Quoted 14 lines
Quoted 11 lines
Quoted 11 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 446

Quoted 9 lines
xen/arch/riscv/p2m.c +8 -0
+/* Put any references on the single 4K page referenced by mfn. */
+static void p2m_put_4k_page(mfn_t mfn, p2m_type_t type)
+{
+    /* TODO: Handle other p2m types */
+
+    /* Detect the xenheap page and mark the stored GFN as invalid. */
+    if ( p2m_is_ram(type) && is_xen_heap_mfn(mfn) )
+        page_set_xenheap_gfn(mfn_to_page(mfn), INVALID_GFN);

Is this a valid thing to do? How do you make sure the respective uses (in gnttab's shared and status page arrays) are / were also removed?

Quoted 3 lines

As grant table frame GFN is stored directly in struct page_info instead of keeping it in standalone status/shared arrays, thereby there is no need for status/shared arrays.

Quoted 8 lines

I fear I don't follow. Looking at Arm's header (which I understand you derive from), I see

#define gnttab_shared_page(t, i) virt_to_page((t)->shared_raw[i])

#define gnttab_status_page(t, i) virt_to_page((t)->status[i])

Are you intending to do things differently?

I missed these arrays... Arm had different arrays:

-    (gt)->arch.shared_gfn = xmalloc_array(gfn_t, ngf_);                  \
-    (gt)->arch.status_gfn = xmalloc_array(gfn_t, nsf_);                  \

I think I don't know the answer to your question, as I'm not deeply familiar with grant tables and would need to do some additional investigation.

And just to be sure I understand your question correctly: are you asking whether I marked a page as|INVALID_GFN| while a domain might still be using it for grant table purposes?

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines
xen/arch/riscv/p2m.c +10 -0
+/* Put any references on the superpage referenced by mfn. */
+static void p2m_put_2m_superpage(mfn_t mfn, p2m_type_t type)
+{
+    struct page_info *pg;
+    unsigned int i;
+
+    ASSERT(mfn_valid(mfn));
+
+    pg = mfn_to_page(mfn);
+

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 468

Quoted 10 lines
xen/arch/riscv/p2m.c +10 -0
+    for ( i = 0; i < XEN_PT_ENTRIES; i++, pg++ )
+        p2m_put_foreign_page(pg);
+}
+
+/* Put any references on the page referenced by pte. */
+static void p2m_put_page(struct p2m_domain *p2m, const pte_t pte,
+                         unsigned int level)
+{
+    mfn_t mfn = pte_get_mfn(pte);
+    p2m_type_t p2m_type = p2m_type_radix_get(p2m, pte);

This gives you the type of the 1st page. What guarantees that all other pages in a superpage are of the exact same type?

Quoted 4 lines
Quoted 2 lines

Doesn't superpage mean that all the 4KB pages within that superpage have the same type and contiguous in memory?

If the mapping is a super-page one - yes. Yet I see nothing super-page-ish here.

Probably, I just misunderstood your reply, but there is a check below:

    if ( level == 2 )
        return p2m_put_l2_superpage(mfn, pte.p2m.type);

And I expect that if|level == 2|, it means it is a superpage, which means that all the 4KB pages within that superpage share the same type and are contiguous in memory.

Quoted 17 lines
Quoted 8 lines
Quoted 6 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 482

Quoted 5 lines
xen/arch/riscv/p2m.c +4 -0
+    if ( level == 1 )
+        return p2m_put_2m_superpage(mfn, p2m_type);
+    else if ( level == 0 )
+        return p2m_put_4k_page(mfn, p2m_type);

Use switch() right away?

It could be, I think that no big difference at the moment, at least. But I am okay to rework it.

If you don't want to use switch() here, then my other style nit would need giving: Please avoid "else" in situations like this.

Quoted 6 lines
Quoted 6 lines

xen/arch/riscv/p2m.c static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, · line 488

Quoted 4 lines
xen/arch/riscv/p2m.c +4 -0
+static void p2m_free_page(struct domain *d, struct page_info *pg)
+{
+    if ( is_hardware_domain(d) )
+        free_domheap_page(pg);

Why's the hardware domain different here? It should have a pool just like all other domains have.

Quoted 7 lines
Quoted 5 lines

Hardware domain (dom0) should be no limit in the number of pages that can be allocated, so allocate p2m pages for hardware domain is done from heap.

An idea of p2m pool is to provide a way how to put clear limit and amount to the p2m allocation.

Well, we had been there on another thread, and I outlined how I think Dom0 may want handling.

I think that I don't remember. Could you please remind me what was that thread?
Probably, do you mean this reply:https://lore.kernel.org/xen-devel/[email protected]/T/#m4789842aaae1653b91d3368f66cadb0ef87fb17e ? But this is not really about Dom0 case.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
xen/arch/riscv/p2m.c +8 -1
   /* Free pte sub-tree behind an entry */
   static void p2m_free_entry(struct p2m_domain *p2m,
                              pte_t entry, unsigned int level)
   {
-    panic("%s: hasn't been implemented yet\n", __func__);
+    unsigned int i;
+    pte_t *table;
+    mfn_t mfn;
+    struct page_info *pg;
+
+    /* Nothing to do if the entry is invalid. */
+    if ( !p2me_is_valid(p2m, entry) )
+        return;
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Does this actually apply to intermediate page tables (which you handle later in the function), when that's (only) a P2M type check?

Yes, any PTE should have V bit set to 1, so from P2M perspective it also should be, at least, not equal to p2m_invalid.

I don't follow. Where would that type be set? The radix tree being GFN- indexed, you would need to "invent" a GFN for every intermediate page table, just to be able to (legitimately) invoke the type retrieval function.

Maybe, it is incorrect, but in this patch series the type is set when |page_to_p2m_table|() is called, which get as an argument a page correspondent
to a table. And then GFN is calculated based on this mfn:
staticpte_tpage_to_p2m_table(structp2m_domain *p2m, structpage_info *page)
{
/*
*Since this function generates a table entry, according to "Encoding
* of PTE R/W/X fields," the entry's r, w, and x fields must be set to 0
*to point to the next level of the page table.
* Therefore,to ensure that anentry is a page table entry,
* p2m_access_n2rwxis passed to mfn_to_p2m_entry() as the access value,
*which overrides whatever was passed as p2m_type_t and guarantees that
*the entry is apage table entry by setting r = w = x = 0.
*/
returnp2m_entry_from_mfn(p2m, page_to_mfn(page), p2m_ram_rw, p2m_access_n2rwx);
}
where:
static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn, p2m_type_t t, p2m_access_t a)

  {
      ...
  
      pte_set_mfn(&e, mfn);
  
      BUG_ON(p2m_type_radix_set(p2m, e, t));
  
      return e;

}

and where:
static int p2m_type_radix_set(struct p2m_domain *p2m, pte_t pte, p2m_type_t t)

  {
      int rc;
      gfn_t gfn = mfn_to_gfn(p2m->domain, mfn_from_pte(pte));
  
      rc = radix_tree_insert(&p2m->p2m_types, gfn_x(gfn),
                             radix_tree_int_to_ptr(t));
      ....

}

But as you mentioned below ...

Quoted 6 lines

Maybe
you mean to leverage that (now, i.e. post-v2) you encode some of the types directly in the PTE, and p2m_invalid may be one of them. But that wasn't the case in the v2 submission, and hence the code looked wrong to me. Which in turn suggests that at least some better commentary is going to be needed, maybe even some BUILD_BUG_ON().

... p2m_invalid type will be encoded directly in the PTE in the next patch version.

~ Oleksii