Skip to contentSkip to conversation

ioremap() et al

PATCH Superseded Jan Beulich 5 participants 13 messages Feb 19 – Apr 2 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ioremap() et al

I found an old todo item, carrying out of which then made me notice another anomaly.

1: make ioremap_attr() common
2: make ioremap_wc() x86 only (for the time being)

Jan

#1 lore raw

Re: [PATCH 1/2] make ioremap_attr() common

This core backing function is uniform; what varies across architectures are the attributes passed and hence the wrappers around it. Yet of course extra checking or special handling may be needed per arch, so introduce a suitable hook. Permit such a hook to return both adjusted and un-adjusted (for the page offset) pointers.

---
Declarations (or inline counterparts) aren't being moved around, to avoid the need to touch source files using the functions. Quite possibly they want to consistently go into xen/io.h and asm/io.h.

Of course ioremap.c could also go into lib/.

For RISC-V the wrappers likely should become inline functions?

PPC doesn't reference any of the functions just yet, so gets only a declaration.

For Arm, a TODO item is deliberately retained, yet seeing the use of ioremap_wc() in domain building (which by itself is questionable, see next patch) I wonder if that's even feasible as long as we don't have memremap() or alike.

xen/arch/arm/include/asm/io.h +12 -0
@@ -1,6 +1,8 @@
 #ifndef _ASM_IO_H
 #define _ASM_IO_H
 
+#include <xen/mm-types.h>
+
 #if defined(CONFIG_ARM_32)
 # include <asm/arm32/io.h>
 #elif defined(CONFIG_ARM_64)
@@ -9,6 +11,16 @@
 # error "unknown ARM variant"
 #endif
 
+#ifdef CONFIG_MPU
+void __iomem *mpu_ioremap_attr(paddr_t start, size_t len, pte_attr_t flags);
+#define arch_ioremap_attr mpu_ioremap_attr
+#else
+/*
+ * ioremap_attr() should only be used to remap device address ranges.
+ * TODO: Add an arch hook to verify this assumption.
+ */
+#endif
+
 #endif
 /*
  * Local variables:
xen/arch/arm/include/asm/mm.h +2 -2
@@ -5,6 +5,7 @@
 #include <asm/page.h>
 #include <public/xen.h>
 #include <xen/pdx.h>
+#include <xen/vmap.h>
 
 #if defined(CONFIG_ARM_32)
 # include <asm/arm32/mm.h>
@@ -200,13 +201,12 @@ extern int prepare_secondary_mm(int cpu)
 extern void setup_frametable_mappings(paddr_t ps, paddr_t pe);
 /* Helper function to setup memory management */
 void setup_mm_helper(void);
-/* map a physical range in virtual memory */
-void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int attributes);
 
 static inline void __iomem *ioremap_nocache(paddr_t start, size_t len)
 {
     return ioremap_attr(start, len, PAGE_HYPERVISOR_NOCACHE);
 }
+#define ioremap ioremap_nocache
 
 static inline void __iomem *ioremap_cache(paddr_t start, size_t len)
 {
xen/arch/arm/mm.c +0 -5
@@ -455,11 +455,6 @@ unsigned long get_upper_mfn_bound(void)
     return max_page - 1;
 }
 
-void *ioremap(paddr_t pa, size_t len)
-{
-    return ioremap_attr(pa, len, PAGE_HYPERVISOR_NOCACHE);
-}
-
 /*
  * Local variables:
  * mode: C
xen/arch/arm/mmu/pt.c +0 -17
@@ -206,23 +206,6 @@ void clear_fixmap(unsigned int map)
     BUG_ON(res != 0);
 }
 
-/*
- * This function should only be used to remap device address ranges
- * TODO: add a check to verify this assumption
- */
-void *ioremap_attr(paddr_t start, size_t len, unsigned int attributes)
-{
-    mfn_t mfn = _mfn(PFN_DOWN(start));
-    unsigned int offs = start & (PAGE_SIZE - 1);
-    unsigned int nr = PFN_UP(offs + len);
-    void *ptr = __vmap(&mfn, nr, 1, 1, attributes, VMAP_DEFAULT);
-
-    if ( ptr == NULL )
-        return NULL;
-
-    return ptr + offs;
-}
-
 static int create_xen_table(lpae_t *entry)
 {
     mfn_t mfn;
xen/arch/arm/mpu/mm.c +3 -1
@@ -9,6 +9,8 @@
 #include <xen/sizes.h>
 #include <xen/spinlock.h>
 #include <xen/types.h>
+
+#include <asm/io.h>
 #include <asm/mpu.h>
 #include <asm/mpu/mm.h>
 #include <asm/page.h>
@@ -593,7 +595,7 @@ void free_init_memory(void)
     spin_unlock(&xen_mpumap_lock);
 }
 
-void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int flags)
+void __iomem *mpu_ioremap_attr(paddr_t start, size_t len, pte_attr_t flags)
 {
     paddr_t start_pg = round_pgdown(start);
     paddr_t end_pg = round_pgup(start_pg + len);
xen/arch/ppc/include/asm/io.h +2 -0
@@ -13,4 +13,6 @@
 #define writew(v,c)     ({ (void)(v); (void)(c); BUG_ON("unimplemented"); })
 #define writel(v,c)     ({ (void)(v); (void)(c); BUG_ON("unimplemented"); })
 
+void __iomem *ioremap(paddr_t pa, size_t len);
+
 #endif /* __ASM_PPC_IO_H__ */
xen/arch/riscv/include/asm/io.h +1 -0
@@ -41,6 +41,7 @@
 #include <xen/macros.h>
 #include <xen/types.h>
 
+void __iomem *ioremap(paddr_t pa, size_t len);
 void __iomem *ioremap_cache(paddr_t pa, size_t len);
 void __iomem *ioremap_wc(paddr_t pa, size_t len);
 
xen/arch/riscv/mm.c +0 -14
@@ -587,20 +587,6 @@ void *__init arch_vmap_virt_end(void)
     return (void *)(VMAP_VIRT_START + VMAP_VIRT_SIZE);
 }
 
-static void __iomem *ioremap_attr(paddr_t pa, size_t len,
-                                  pte_attr_t attributes)
-{
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    unsigned int offs = pa & (PAGE_SIZE - 1);
-    unsigned int nr = PFN_UP(offs + len);
-    void *ptr = __vmap(&mfn, nr, 1, 1, attributes, VMAP_DEFAULT);
-
-    if ( ptr == NULL )
-        return NULL;
-
-    return ptr + offs;
-}
-
 void __iomem *ioremap_cache(paddr_t pa, size_t len)
 {
     return ioremap_attr(pa, len, PAGE_HYPERVISOR);
xen/arch/x86/include/asm/io.h +3 -0
@@ -47,6 +47,9 @@ __OUT(b,"b",char)
 __OUT(w,"w",short)
 __OUT(l,,int)
 
+void __iomem *x86_ioremap_attr(paddr_t pa, size_t len, pte_attr_t attr);
+#define arch_ioremap_attr x86_ioremap_attr
+
 /*
  * Boolean indicator and function used to handle platform specific I/O port
  * emulation.
xen/arch/x86/include/asm/mm.h +9 -1
@@ -611,7 +611,15 @@ void destroy_perdomain_mapping(struct do
                                unsigned int nr);
 void free_perdomain_mappings(struct domain *d);
 
-void __iomem *ioremap_wc(paddr_t pa, size_t len);
+static inline void __iomem *ioremap(paddr_t pa, size_t len)
+{
+    return ioremap_attr(pa, len, PAGE_HYPERVISOR_UCMINUS);
+}
+
+static inline void __iomem *ioremap_wc(paddr_t pa, size_t len)
+{
+    return ioremap_attr(pa, len, PAGE_HYPERVISOR_WC);
+}
 
 extern int memory_add(unsigned long spfn, unsigned long epfn, unsigned int pxm);
 
xen/arch/x86/mm.c +5 -31
@@ -6166,41 +6166,15 @@ void *__init arch_vmap_virt_end(void)
     return fix_to_virt(__end_of_fixed_addresses);
 }
 
-void __iomem *ioremap(paddr_t pa, size_t len)
+void __iomem *x86_ioremap_attr(paddr_t pa, size_t len, pte_attr_t attr)
 {
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    void *va;
-
-    WARN_ON(page_is_ram_type(mfn_x(mfn), RAM_TYPE_CONVENTIONAL));
+    WARN_ON(page_is_ram_type(PFN_DOWN(pa), RAM_TYPE_CONVENTIONAL));
 
     /* The low first Mb is always mapped. */
-    if ( !((pa + len - 1) >> 20) )
-        va = __va(pa);
-    else
-    {
-        unsigned int offs = pa & (PAGE_SIZE - 1);
-        unsigned int nr = PFN_UP(offs + len);
-
-        va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_UCMINUS, VMAP_DEFAULT);
-        if ( va )
-            va += offs;
-    }
-
-    return (void __force __iomem *)va;
-}
-
-void __iomem *__init ioremap_wc(paddr_t pa, size_t len)
-{
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    unsigned int offs = pa & (PAGE_SIZE - 1);
-    unsigned int nr = PFN_UP(offs + len);
-    void *va;
-
-    WARN_ON(page_is_ram_type(mfn_x(mfn), RAM_TYPE_CONVENTIONAL));
-
-    va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_WC, VMAP_DEFAULT);
+    if ( !((pa + len - 1) >> 20) && attr == PAGE_HYPERVISOR_UCMINUS )
+        return (void __force __iomem *)__va(pa);
 
-    return (void __force __iomem *)(va ? va + offs : NULL);
+    return NULL;
 }
 
 int create_perdomain_mapping(struct domain *d, unsigned long va,
xen/common/Makefile +1 -0
@@ -19,6 +19,7 @@ obj-$(CONFIG_GRANT_TABLE) += grant_table
 obj-y += guestcopy.o
 obj-y += gzip/
 obj-$(CONFIG_HYPFS) += hypfs.o
+obj-y += ioremap.o
 obj-$(CONFIG_IOREQ_SERVER) += ioreq.o
 obj-y += irq.o
 obj-y += kernel.o
xen/common/ioremap.c new file +42 -0

42 lines not on this page · raw message

xen/include/xen/vmap.h +2 -1
@@ -121,9 +121,10 @@ void vfree(void *va);
  *
  * @param pa  Physical base address of the MMIO region.
  * @param len Length of the MMIO region in octets.
+ * @param attr Attributes for the mapping.
  * @return Pointer to the mapped area on success; NULL otherwise.
  */
-void __iomem *ioremap(paddr_t pa, size_t len);
+void __iomem *ioremap_attr(paddr_t pa, size_t len, pte_attr_t attr);
 
 /* Return the number of pages in the mapping starting at address 'va' */
 unsigned int vmap_size(const void *va);
Reviewed-by
#2 lore raw

Re: [PATCH 1/2] make ioremap_attr() common

Quoted 7 lines · Jan Beulich

This core backing function is uniform; what varies across architectures are the attributes passed and hence the wrappers around it. Yet of course extra checking or special handling may be needed per arch, so introduce a suitable hook. Permit such a hook to return both adjusted and un-adjusted (for the page offset) pointers.

Quoted 8 lines

---
Declarations (or inline counterparts) aren't being moved around, to avoid the need to touch source files using the functions. Quite possibly they want to consistently go into xen/io.h and asm/io.h.

Of course ioremap.c could also go into lib/.

For RISC-V the wrappers likely should become inline functions?

These wrappers are implemented not as static inline function or macros as it will require to include asm/page.h into asm/io.h what will lead to compilation issue.

I am okay to leave them in the way they are implemented now.

For other part of the changes, everything looks okay to me:

Thanks.

~ Oleksii

Reviewed-by
#3 lore raw

Re: [PATCH 1/2] make ioremap_attr() common

In reply to Jan Beulich
Quoted 277 lines · Jan Beulich

This core backing function is uniform; what varies across architectures are the attributes passed and hence the wrappers around it. Yet of course extra checking or special handling may be needed per arch, so introduce a suitable hook. Permit such a hook to return both adjusted and un-adjusted (for the page offset) pointers.

---
Declarations (or inline counterparts) aren't being moved around, to avoid the need to touch source files using the functions. Quite possibly they want to consistently go into xen/io.h and asm/io.h.

Of course ioremap.c could also go into lib/.

For RISC-V the wrappers likely should become inline functions?

PPC doesn't reference any of the functions just yet, so gets only a declaration.

For Arm, a TODO item is deliberately retained, yet seeing the use of ioremap_wc() in domain building (which by itself is questionable, see next patch) I wonder if that's even feasible as long as we don't have memremap() or alike.

xen/arch/arm/include/asm/io.h +12 -0
@@ -1,6 +1,8 @@
 #ifndef _ASM_IO_H
 #define _ASM_IO_H
 
+#include <xen/mm-types.h>
+
 #if defined(CONFIG_ARM_32)
 # include <asm/arm32/io.h>
 #elif defined(CONFIG_ARM_64)
@@ -9,6 +11,16 @@
 # error "unknown ARM variant"
 #endif
 
+#ifdef CONFIG_MPU
+void __iomem *mpu_ioremap_attr(paddr_t start, size_t len, pte_attr_t flags);
+#define arch_ioremap_attr mpu_ioremap_attr
+#else
+/*
+ * ioremap_attr() should only be used to remap device address ranges.
+ * TODO: Add an arch hook to verify this assumption.
+ */
+#endif
+
 #endif
 /*
  * Local variables:
xen/arch/arm/include/asm/mm.h +2 -2
@@ -5,6 +5,7 @@
 #include <asm/page.h>
 #include <public/xen.h>
 #include <xen/pdx.h>
+#include <xen/vmap.h>
 
 #if defined(CONFIG_ARM_32)
 # include <asm/arm32/mm.h>
@@ -200,13 +201,12 @@ extern int prepare_secondary_mm(int cpu)
 extern void setup_frametable_mappings(paddr_t ps, paddr_t pe);
 /* Helper function to setup memory management */
 void setup_mm_helper(void);
-/* map a physical range in virtual memory */
-void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int attributes);
 
 static inline void __iomem *ioremap_nocache(paddr_t start, size_t len)
 {
     return ioremap_attr(start, len, PAGE_HYPERVISOR_NOCACHE);
 }
+#define ioremap ioremap_nocache
 
 static inline void __iomem *ioremap_cache(paddr_t start, size_t len)
 {
xen/arch/arm/mm.c +0 -5
@@ -455,11 +455,6 @@ unsigned long get_upper_mfn_bound(void)
     return max_page - 1;
 }
 
-void *ioremap(paddr_t pa, size_t len)
-{
-    return ioremap_attr(pa, len, PAGE_HYPERVISOR_NOCACHE);
-}
-
 /*
  * Local variables:
  * mode: C
xen/arch/arm/mmu/pt.c +0 -17
@@ -206,23 +206,6 @@ void clear_fixmap(unsigned int map)
     BUG_ON(res != 0);
 }
 
-/*
- * This function should only be used to remap device address ranges
- * TODO: add a check to verify this assumption
- */
-void *ioremap_attr(paddr_t start, size_t len, unsigned int attributes)
-{
-    mfn_t mfn = _mfn(PFN_DOWN(start));
-    unsigned int offs = start & (PAGE_SIZE - 1);
-    unsigned int nr = PFN_UP(offs + len);
-    void *ptr = __vmap(&mfn, nr, 1, 1, attributes, VMAP_DEFAULT);
-
-    if ( ptr == NULL )
-        return NULL;
-
-    return ptr + offs;
-}
-
 static int create_xen_table(lpae_t *entry)
 {
     mfn_t mfn;
xen/arch/arm/mpu/mm.c +3 -1
@@ -9,6 +9,8 @@
 #include <xen/sizes.h>
 #include <xen/spinlock.h>
 #include <xen/types.h>
+
+#include <asm/io.h>
 #include <asm/mpu.h>
 #include <asm/mpu/mm.h>
 #include <asm/page.h>
@@ -593,7 +595,7 @@ void free_init_memory(void)
     spin_unlock(&xen_mpumap_lock);
 }
 
-void __iomem *ioremap_attr(paddr_t start, size_t len, unsigned int flags)
+void __iomem *mpu_ioremap_attr(paddr_t start, size_t len, pte_attr_t flags)
 {
     paddr_t start_pg = round_pgdown(start);
     paddr_t end_pg = round_pgup(start_pg + len);
xen/arch/ppc/include/asm/io.h +2 -0
@@ -13,4 +13,6 @@
 #define writew(v,c)     ({ (void)(v); (void)(c); BUG_ON("unimplemented"); })
 #define writel(v,c)     ({ (void)(v); (void)(c); BUG_ON("unimplemented"); })
 
+void __iomem *ioremap(paddr_t pa, size_t len);
+
 #endif /* __ASM_PPC_IO_H__ */
xen/arch/riscv/include/asm/io.h +1 -0
@@ -41,6 +41,7 @@
 #include <xen/macros.h>
 #include <xen/types.h>
 
+void __iomem *ioremap(paddr_t pa, size_t len);
 void __iomem *ioremap_cache(paddr_t pa, size_t len);
 void __iomem *ioremap_wc(paddr_t pa, size_t len);
 
xen/arch/riscv/mm.c +0 -14
@@ -587,20 +587,6 @@ void *__init arch_vmap_virt_end(void)
     return (void *)(VMAP_VIRT_START + VMAP_VIRT_SIZE);
 }
 
-static void __iomem *ioremap_attr(paddr_t pa, size_t len,
-                                  pte_attr_t attributes)
-{
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    unsigned int offs = pa & (PAGE_SIZE - 1);
-    unsigned int nr = PFN_UP(offs + len);
-    void *ptr = __vmap(&mfn, nr, 1, 1, attributes, VMAP_DEFAULT);
-
-    if ( ptr == NULL )
-        return NULL;
-
-    return ptr + offs;
-}
-
 void __iomem *ioremap_cache(paddr_t pa, size_t len)
 {
     return ioremap_attr(pa, len, PAGE_HYPERVISOR);
xen/arch/x86/include/asm/io.h +3 -0
@@ -47,6 +47,9 @@ __OUT(b,"b",char)
 __OUT(w,"w",short)
 __OUT(l,,int)
 
+void __iomem *x86_ioremap_attr(paddr_t pa, size_t len, pte_attr_t attr);
+#define arch_ioremap_attr x86_ioremap_attr
+
 /*
  * Boolean indicator and function used to handle platform specific I/O port
  * emulation.
xen/arch/x86/include/asm/mm.h +9 -1
@@ -611,7 +611,15 @@ void destroy_perdomain_mapping(struct do
                                unsigned int nr);
 void free_perdomain_mappings(struct domain *d);
 
-void __iomem *ioremap_wc(paddr_t pa, size_t len);
+static inline void __iomem *ioremap(paddr_t pa, size_t len)
+{
+    return ioremap_attr(pa, len, PAGE_HYPERVISOR_UCMINUS);
+}
+
+static inline void __iomem *ioremap_wc(paddr_t pa, size_t len)
+{
+    return ioremap_attr(pa, len, PAGE_HYPERVISOR_WC);
+}
 
 extern int memory_add(unsigned long spfn, unsigned long epfn, unsigned int pxm);
 
xen/arch/x86/mm.c +5 -31
@@ -6166,41 +6166,15 @@ void *__init arch_vmap_virt_end(void)
     return fix_to_virt(__end_of_fixed_addresses);
 }
 
-void __iomem *ioremap(paddr_t pa, size_t len)
+void __iomem *x86_ioremap_attr(paddr_t pa, size_t len, pte_attr_t attr)
 {
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    void *va;
-
-    WARN_ON(page_is_ram_type(mfn_x(mfn), RAM_TYPE_CONVENTIONAL));
+    WARN_ON(page_is_ram_type(PFN_DOWN(pa), RAM_TYPE_CONVENTIONAL));
 
     /* The low first Mb is always mapped. */
-    if ( !((pa + len - 1) >> 20) )
-        va = __va(pa);
-    else
-    {
-        unsigned int offs = pa & (PAGE_SIZE - 1);
-        unsigned int nr = PFN_UP(offs + len);
-
-        va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_UCMINUS, VMAP_DEFAULT);
-        if ( va )
-            va += offs;
-    }
-
-    return (void __force __iomem *)va;
-}
-
-void __iomem *__init ioremap_wc(paddr_t pa, size_t len)
-{
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    unsigned int offs = pa & (PAGE_SIZE - 1);
-    unsigned int nr = PFN_UP(offs + len);
-    void *va;
-
-    WARN_ON(page_is_ram_type(mfn_x(mfn), RAM_TYPE_CONVENTIONAL));
-
-    va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_WC, VMAP_DEFAULT);
+    if ( !((pa + len - 1) >> 20) && attr == PAGE_HYPERVISOR_UCMINUS )
+        return (void __force __iomem *)__va(pa);
 
-    return (void __force __iomem *)(va ? va + offs : NULL);
+    return NULL;
 }
 
 int create_perdomain_mapping(struct domain *d, unsigned long va,
xen/common/Makefile +1 -0
@@ -19,6 +19,7 @@ obj-$(CONFIG_GRANT_TABLE) += grant_table
 obj-y += guestcopy.o
 obj-y += gzip/
 obj-$(CONFIG_HYPFS) += hypfs.o
+obj-y += ioremap.o
 obj-$(CONFIG_IOREQ_SERVER) += ioreq.o
 obj-y += irq.o
 obj-y += kernel.o
xen/common/ioremap.c new file +5 -0
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/mm.h>
+#include <xen/pfn.h>
+#include <xen/vmap.h>

xen/common/ioremap.c line 15

Quoted 10 lines
xen/common/ioremap.c new file +10 -0
+
+#include <asm/io.h>
+
+void __iomem *ioremap_attr(paddr_t pa, size_t len, pte_attr_t attr)
+{
+    void __iomem *ptr = NULL;
+    unsigned int offs = PAGE_OFFSET(pa);
+
+#ifdef arch_ioremap_attr
+    ptr = arch_ioremap_attr(pa, len, attr);

I made an observation reviewing this patch.

TL;DR: Nothing wrong with this patch

For Arm MPU, callers now receive a sub-page-offset-adjusted pointer instead of a page-aligned one. mpu_ioremap_attr computes end_pg = round_pgup(start_pg + len) instead of round_pgup(start + len). Before, this was masked because the returned pointer was page-aligned, so callers effectively started from the page base. Now that the sub-page offset is correctly applied to the returned pointer, a caller using a non-page-aligned start with a len that crosses a page boundary would access memory from a different region.

I need to send a fix for MPU.

~Michal

#4 lore raw

Re: [PATCH 1/2] make ioremap_attr() common

In reply to Jan Beulich
Quoted 33 lines · Jan Beulich
xen/arch/x86/mm.c +2 -22
@@ -6166,41 +6166,15 @@ void *__init arch_vmap_virt_end(void)
     return fix_to_virt(__end_of_fixed_addresses);
 }
 
-void __iomem *ioremap(paddr_t pa, size_t len)
+void __iomem *x86_ioremap_attr(paddr_t pa, size_t len, pte_attr_t attr)
 {
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    void *va;
-
-    WARN_ON(page_is_ram_type(mfn_x(mfn), RAM_TYPE_CONVENTIONAL));
+    WARN_ON(page_is_ram_type(PFN_DOWN(pa), RAM_TYPE_CONVENTIONAL));
 
     /* The low first Mb is always mapped. */
-    if ( !((pa + len - 1) >> 20) )
-        va = __va(pa);
-    else
-    {
-        unsigned int offs = pa & (PAGE_SIZE - 1);
-        unsigned int nr = PFN_UP(offs + len);
-
-        va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_UCMINUS, VMAP_DEFAULT);
-        if ( va )
-            va += offs;
-    }
-
-    return (void __force __iomem *)va;
-}
-
-void __iomem *__init ioremap_wc(paddr_t pa, size_t len)
-{

xen/arch/x86/mm.c void *__init arch_vmap_virt_end(void) · line 6175

Quoted 10 lines
xen/arch/x86/mm.c +2 -8
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    unsigned int offs = pa & (PAGE_SIZE - 1);
-    unsigned int nr = PFN_UP(offs + len);
-    void *va;
-
-    WARN_ON(page_is_ram_type(mfn_x(mfn), RAM_TYPE_CONVENTIONAL));
-
-    va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_WC, VMAP_DEFAULT);
+    if ( !((pa + len - 1) >> 20) && attr == PAGE_HYPERVISOR_UCMINUS )
+        return (void __force __iomem *)__va(pa);

Sorry, I'm possibly a bit lost: I see that on the bootstrap page-tables we map the VGA hole (0xa0000-0xc0000) as UC-, but I'm not seeing this being done to the directmap?

Quoted 26 lines
xen/arch/x86/mm.c +1 -1
-    return (void __force __iomem *)(va ? va + offs : NULL);
+    return NULL;
 }
 
 int create_perdomain_mapping(struct domain *d, unsigned long va,
xen/common/Makefile +1 -0
@@ -19,6 +19,7 @@ obj-$(CONFIG_GRANT_TABLE) += grant_table
 obj-y += guestcopy.o
 obj-y += gzip/
 obj-$(CONFIG_HYPFS) += hypfs.o
+obj-y += ioremap.o
 obj-$(CONFIG_IOREQ_SERVER) += ioreq.o
 obj-y += irq.o
 obj-y += kernel.o
xen/common/ioremap.c new file +7 -0
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/mm.h>
+#include <xen/pfn.h>
+#include <xen/vmap.h>
+
+#include <asm/io.h>

xen/common/ioremap.c line 17

Quoted 10 lines
xen/common/ioremap.c new file +10 -0
+
+void __iomem *ioremap_attr(paddr_t pa, size_t len, pte_attr_t attr)
+{
+    void __iomem *ptr = NULL;
+    unsigned int offs = PAGE_OFFSET(pa);
+
+#ifdef arch_ioremap_attr
+    ptr = arch_ioremap_attr(pa, len, attr);
+    if ( PAGE_OFFSET(ptr) )
+        return ptr;

IMO it's a bit confusing to deal with the offset addition detached form here, I would rather do:

if ( ptr != NULL )

{
    ASSERT(PAGE_OFFSET(ptr) == offs);
    return PAGE_OFFSET(ptr) ? ptr : ptr + off;
}

And get rid of the !ptr check in the if below. So that when arch_ioremap_attr() succeeds it's all dealt with in the #ifdef protected block.

Quoted 4 lines
xen/common/ioremap.c new file +4 -0
+#endif
+
+    if ( IS_ENABLED(CONFIG_HAS_VMAP) && !ptr )
+    {

xen/common/ioremap.c line 31

Quoted 10 lines
xen/common/ioremap.c new file +10 -0
+        mfn_t mfn = _mfn(PFN_DOWN(pa));
+
+        ptr = (void __force __iomem *)__vmap(&mfn, PFN_UP(offs + len), 1, 1,
+                                             attr, VMAP_DEFAULT);
+    }
+
+    if ( !ptr )
+        return NULL;
+
+    return ptr + offs;

I would also join the NULL checking using a ternary operator:

return ptr ? ptr + off : NULL;

Thanks, Roger.

#5 lore raw

Re: [PATCH 1/2] make ioremap_attr() common

Quoted 40 lines · Roger Pau Monné
Quoted 39 lines · Jan Beulich
xen/arch/x86/mm.c +2 -28
@@ -6166,41 +6166,15 @@ void *__init arch_vmap_virt_end(void)
     return fix_to_virt(__end_of_fixed_addresses);
 }
 
-void __iomem *ioremap(paddr_t pa, size_t len)
+void __iomem *x86_ioremap_attr(paddr_t pa, size_t len, pte_attr_t attr)
 {
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    void *va;
-
-    WARN_ON(page_is_ram_type(mfn_x(mfn), RAM_TYPE_CONVENTIONAL));
+    WARN_ON(page_is_ram_type(PFN_DOWN(pa), RAM_TYPE_CONVENTIONAL));
 
     /* The low first Mb is always mapped. */
-    if ( !((pa + len - 1) >> 20) )
-        va = __va(pa);
-    else
-    {
-        unsigned int offs = pa & (PAGE_SIZE - 1);
-        unsigned int nr = PFN_UP(offs + len);
-
-        va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_UCMINUS, VMAP_DEFAULT);
-        if ( va )
-            va += offs;
-    }
-
-    return (void __force __iomem *)va;
-}
-
-void __iomem *__init ioremap_wc(paddr_t pa, size_t len)
-{
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    unsigned int offs = pa & (PAGE_SIZE - 1);
-    unsigned int nr = PFN_UP(offs + len);
-    void *va;
-
-    WARN_ON(page_is_ram_type(mfn_x(mfn), RAM_TYPE_CONVENTIONAL));
Quoted 8 lines

xen/arch/x86/mm.c void *__init arch_vmap_virt_end(void) · line 6175

Quoted 4 lines
xen/arch/x86/mm.c +2 -2
-
-    va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_WC, VMAP_DEFAULT);
+    if ( !((pa + len - 1) >> 20) && attr == PAGE_HYPERVISOR_UCMINUS )
+        return (void __force __iomem *)__va(pa);

Sorry, I'm possibly a bit lost: I see that on the bootstrap page-tables we map the VGA hole (0xa0000-0xc0000) as UC-, but I'm not seeing this being done to the directmap?

I'm not changing that aspect here, do I? ioremap() used the direct map (apparently assuming the cache attribute is suitable), and so do I now.

Yet then, to address the point you make anyway, I certainly see use of __PAGE_HYPERVISOR_UCMINUS in l1_directmap[].

Quoted 25 lines
Quoted 10 lines
xen/common/ioremap.c new file +7 -0
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/mm.h>
+#include <xen/pfn.h>
+#include <xen/vmap.h>
+
+#include <asm/io.h>

xen/common/ioremap.c line 17

Quoted 10 lines
xen/common/ioremap.c new file +10 -0
+
+void __iomem *ioremap_attr(paddr_t pa, size_t len, pte_attr_t attr)
+{
+    void __iomem *ptr = NULL;
+    unsigned int offs = PAGE_OFFSET(pa);
+
+#ifdef arch_ioremap_attr
+    ptr = arch_ioremap_attr(pa, len, attr);
+    if ( PAGE_OFFSET(ptr) )
+        return ptr;

IMO it's a bit confusing to deal with the offset addition detached form here, I would rather do:

if ( ptr != NULL )

Quoted 8 lines
{
    ASSERT(PAGE_OFFSET(ptr) == offs);
    return PAGE_OFFSET(ptr) ? ptr : ptr + off;
}

And get rid of the !ptr check in the if below. So that when arch_ioremap_attr() succeeds it's all dealt with in the #ifdef protected block.

Possible, yet I deemed the other variant better.

The assertion you suggest isn't valid anyway (see the last sentence of the description and xen/arch/arm/mpu/mm.c:ioremap_attr() prior to Michal's "xen/arm: mpu: Fix ioremap_attr region size and return value"). Now that that's changing, things may indeed be possible to simplify here.

Quoted 17 lines
Quoted 4 lines
xen/common/ioremap.c new file +4 -0
+#endif
+
+    if ( IS_ENABLED(CONFIG_HAS_VMAP) && !ptr )
+    {

xen/common/ioremap.c line 31

Quoted 10 lines
xen/common/ioremap.c new file +10 -0
+        mfn_t mfn = _mfn(PFN_DOWN(pa));
+
+        ptr = (void __force __iomem *)__vmap(&mfn, PFN_UP(offs + len), 1, 1,
+                                             attr, VMAP_DEFAULT);
+    }
+
+    if ( !ptr )
+        return NULL;
+
+    return ptr + offs;

I would also join the NULL checking using a ternary operator:

Quoted 1 line

return ptr ? ptr + off : NULL;

Can do, sure.

Jan

#6 lore raw

Re: [PATCH 1/2] make ioremap_attr() common

Quoted 50 lines · Jan Beulich
Quoted 40 lines · Roger Pau Monné
Quoted 39 lines · Jan Beulich
xen/arch/x86/mm.c +2 -28
@@ -6166,41 +6166,15 @@ void *__init arch_vmap_virt_end(void)
     return fix_to_virt(__end_of_fixed_addresses);
 }
 
-void __iomem *ioremap(paddr_t pa, size_t len)
+void __iomem *x86_ioremap_attr(paddr_t pa, size_t len, pte_attr_t attr)
 {
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    void *va;
-
-    WARN_ON(page_is_ram_type(mfn_x(mfn), RAM_TYPE_CONVENTIONAL));
+    WARN_ON(page_is_ram_type(PFN_DOWN(pa), RAM_TYPE_CONVENTIONAL));
 
     /* The low first Mb is always mapped. */
-    if ( !((pa + len - 1) >> 20) )
-        va = __va(pa);
-    else
-    {
-        unsigned int offs = pa & (PAGE_SIZE - 1);
-        unsigned int nr = PFN_UP(offs + len);
-
-        va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_UCMINUS, VMAP_DEFAULT);
-        if ( va )
-            va += offs;
-    }
-
-    return (void __force __iomem *)va;
-}
-
-void __iomem *__init ioremap_wc(paddr_t pa, size_t len)
-{
-    mfn_t mfn = _mfn(PFN_DOWN(pa));
-    unsigned int offs = pa & (PAGE_SIZE - 1);
-    unsigned int nr = PFN_UP(offs + len);
-    void *va;
-
-    WARN_ON(page_is_ram_type(mfn_x(mfn), RAM_TYPE_CONVENTIONAL));
Quoted 8 lines

xen/arch/x86/mm.c void *__init arch_vmap_virt_end(void) · line 6175

Quoted 4 lines
xen/arch/x86/mm.c +2 -2
-
-    va = __vmap(&mfn, nr, 1, 1, PAGE_HYPERVISOR_WC, VMAP_DEFAULT);
+    if ( !((pa + len - 1) >> 20) && attr == PAGE_HYPERVISOR_UCMINUS )
+        return (void __force __iomem *)__va(pa);

Sorry, I'm possibly a bit lost: I see that on the bootstrap page-tables we map the VGA hole (0xa0000-0xc0000) as UC-, but I'm not seeing this being done to the directmap?

Quoted 5 lines

I'm not changing that aspect here, do I? ioremap() used the direct map (apparently assuming the cache attribute is suitable), and so do I now.

Yet then, to address the point you make anyway, I certainly see use of __PAGE_HYPERVISOR_UCMINUS in l1_directmap[].

Yes, sorry, I know you are not adding this here. Just took a look at the code, and I couldn't find where the directmap entries for the low 1M get adjusted to use UC-. I should have made the comment clearer.

Quoted 30 lines
Quoted 30 lines
Quoted 10 lines
xen/common/ioremap.c new file +7 -0
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/mm.h>
+#include <xen/pfn.h>
+#include <xen/vmap.h>
+
+#include <asm/io.h>

xen/common/ioremap.c line 17

Quoted 10 lines
xen/common/ioremap.c new file +10 -0
+
+void __iomem *ioremap_attr(paddr_t pa, size_t len, pte_attr_t attr)
+{
+    void __iomem *ptr = NULL;
+    unsigned int offs = PAGE_OFFSET(pa);
+
+#ifdef arch_ioremap_attr
+    ptr = arch_ioremap_attr(pa, len, attr);
+    if ( PAGE_OFFSET(ptr) )
+        return ptr;

IMO it's a bit confusing to deal with the offset addition detached form here, I would rather do:

if ( ptr != NULL )

{
    ASSERT(PAGE_OFFSET(ptr) == offs);
    return PAGE_OFFSET(ptr) ? ptr : ptr + off;
}
Quoted 5 lines
Quoted 3 lines

And get rid of the !ptr check in the if below. So that when arch_ioremap_attr() succeeds it's all dealt with in the #ifdef protected block.

Possible, yet I deemed the other variant better.

OK, I'm not going to insist then. It just took me a bit to notice the extra !ptr check in the if condition below.

Quoted 4 lines

The assertion you suggest isn't valid anyway (see the last sentence of the description and xen/arch/arm/mpu/mm.c:ioremap_attr() prior to Michal's "xen/arm: mpu: Fix ioremap_attr region size and return value"). Now that that's changing, things may indeed be possible to simplify here.

Oh, I see, right you are. It should be:

ASSERT(!PAGE_OFFSET(ptr) || PAGE_OFFSET(ptr) == offs);

Thanks, Roger.

#7 lore raw

PATCH 2/2 make ioremap_wc() x86 only (for the time being)

In reply to Jan Beulich

Its use in domain building is questionable: Already at the point both uses were introduced, ioremap_cache() existed. I can't see why kernel and initrd would need mapping WC, when at the same time other similar mappings (in common/device-tree/) are done WB.

With those uses replaced, neither Arm nor RISC-V have a need for the function anymore.

Amends: d8972aa9645f ("xen/arm: kernel: Rework kernel_zimage_load to use the generic copy helper")
Amends: bb7e6d565d92 ("xen/arm: domain_build: Rework initrd_load to use the generic copy helper")

xen/arch/arm/include/asm/mm.h +0 -5
@@ -213,11 +213,6 @@ static inline void __iomem *ioremap_cach
     return ioremap_attr(start, len, PAGE_HYPERVISOR);
 }
 
-static inline void __iomem *ioremap_wc(paddr_t start, size_t len)
-{
-    return ioremap_attr(start, len, PAGE_HYPERVISOR_WC);
-}
-
 /* XXX -- account for base */
 #define mfn_valid(mfn)        ({                                              \
     unsigned long __m_f_n = mfn_x(mfn);                                       \
xen/arch/arm/kernel.c +1 -1
@@ -148,7 +148,7 @@ static void __init kernel_zimage_load(st
     printk("Loading zImage from %"PRIpaddr" to %"PRIpaddr"-%"PRIpaddr"\n",
            paddr, load_addr, load_addr + len);
 
-    kernel = ioremap_wc(paddr, len);
+    kernel = ioremap_cache(paddr, len);
     if ( !kernel )
         panic("Unable to map the %pd kernel\n", info->bd.d);
 
xen/arch/riscv/include/asm/io.h +0 -1
@@ -43,7 +43,6 @@
 
 void __iomem *ioremap(paddr_t pa, size_t len);
 void __iomem *ioremap_cache(paddr_t pa, size_t len);
-void __iomem *ioremap_wc(paddr_t pa, size_t len);
 
 /* Generic IO read/write.  These perform native-endian accesses. */
 static inline void __raw_writeb(uint8_t val, volatile void __iomem *addr)
xen/arch/riscv/mm.c +0 -5
@@ -592,11 +592,6 @@ void __iomem *ioremap_cache(paddr_t pa,
     return ioremap_attr(pa, len, PAGE_HYPERVISOR);
 }
 
-void __iomem *ioremap_wc(paddr_t pa, size_t len)
-{
-    return ioremap_attr(pa, len, PAGE_HYPERVISOR_WC);
-}
-
 void __iomem *ioremap(paddr_t pa, size_t len)
 {
     return ioremap_attr(pa, len, PAGE_HYPERVISOR_NOCACHE);
xen/common/device-tree/domain-build.c +1 -1
@@ -395,7 +395,7 @@ void __init initrd_load(struct kernel_in
     if ( res )
         panic("Cannot fix up \"linux,initrd-end\" property\n");
 
-    initrd = ioremap_wc(paddr, len);
+    initrd = ioremap_cache(paddr, len);
     if ( !initrd )
         panic("Unable to map the %pd initrd\n", kinfo->bd.d);
 
Reviewed-by on v1 2/2
#8 lore raw

Re: [PATCH 2/2] make ioremap_wc() x86 only (for the time being)

Quoted 8 lines · Jan Beulich

Its use in domain building is questionable: Already at the point both uses were introduced, ioremap_cache() existed. I can't see why kernel and initrd would need mapping WC, when at the same time other similar mappings (in common/device-tree/) are done WB.

With those uses replaced, neither Arm nor RISC-V have a need for the function anymore.

It is okay for RISC-V, but for Arm, IIRC, likely it was done because guest might start with cache disabled and PAGE_HYPERVISOR_WC on Arm uses "Normal Non-cacheable" what could lead to that guest won't see some part of kernel and/or initrd as it could be in a cache and won't be flushed to RAM.

But probably I misremembered something and clean_and_invalidate_dcache_va_range() or something similar should be called before guest is laucned.

For RISC-V part,

Thanks.

~ Oleksii

#9 lore raw

Re: [PATCH 2/2] make ioremap_wc() x86 only (for the time being)

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

Its use in domain building is questionable: Already at the point both uses were introduced, ioremap_cache() existed. I can't see why kernel and initrd would need mapping WC, when at the same time other similar mappings (in common/device-tree/) are done WB.

Quoted 7 lines
Quoted 3 lines

With those uses replaced, neither Arm nor RISC-V have a need for the function anymore.

It is okay for RISC-V, but for Arm, IIRC, likely it was done because guest might start with cache disabled and PAGE_HYPERVISOR_WC on Arm uses "Normal Non-cacheable" what could lead to that guest won't see some part of kernel and/or initrd as it could be in a cache and won't be flushed to RAM.

But probably I misremembered something and clean_and_invalidate_dcache_va_range() or something similar should be called before guest is laucned.

okay, so for this case copy_to_guest_phys_flush_dcache() is called after initrd and kernel are loaded.

Then it looks okay to me for Arm too.

~ Oleksii

#10 lore raw

Re: [PATCH 2/2] make ioremap_wc() x86 only (for the time being)

Quoted 23 lines · Oleksii Kurochko
Quoted 20 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

Its use in domain building is questionable: Already at the point both uses
were introduced, ioremap_cache() existed. I can't see why kernel and initrd would need mapping WC, when at the same time other similar mappings
(in common/device-tree/) are done WB.

With those uses replaced, neither Arm nor RISC-V have a need for the function anymore.

It is okay for RISC-V, but for Arm, IIRC, likely it was done because guest might start with cache disabled and PAGE_HYPERVISOR_WC on Arm uses "Normal Non-cacheable" what could lead to that guest won't see some part of kernel and/or initrd as it could be in a cache and won't be flushed to RAM.

Quoted 8 lines
Quoted 3 lines

But probably I misremembered something and clean_and_invalidate_dcache_va_range() or something similar should be called before guest is laucned.

okay, so for this case copy_to_guest_phys_flush_dcache() is called after initrd and kernel are loaded.

Then it looks okay to me for Arm too.

May I ask whether that removes the RISC-V restriction on the R-b (thanks for that) you gave in the earlier reply?

Jan

#11 lore raw

Re: [PATCH 2/2] make ioremap_wc() x86 only (for the time being)

In reply to Oleksii Kurochko

Hi Oleksii,

Quoted 15 lines · Oleksii Kurochko
Quoted 12 lines · Oleksii Kurochko
Quoted 8 lines · Jan Beulich

Its use in domain building is questionable: Already at the point both uses were introduced, ioremap_cache() existed. I can't see why kernel and initrd would need mapping WC, when at the same time other similar mappings (in common/device-tree/) are done WB.

With those uses replaced, neither Arm nor RISC-V have a need for the function anymore.

Quoted 5 lines
Quoted 3 lines

It is okay for RISC-V, but for Arm, IIRC, likely it was done because guest might start with cache disabled and PAGE_HYPERVISOR_WC on Arm uses "Normal Non-cacheable" what could lead to that guest won't see some part of kernel and/or initrd as it could be in a cache and won't be flushed to RAM.

But probably I misremembered something and clean_and_invalidate_dcache_va_range() or something similar should be called before guest is laucned.

okay, so for this case copy_to_guest_phys_flush_dcache() is called after initrd and kernel are loaded.

Looking at the code, we are using ioremap_wc() on the source side rather than the destination. So the use of copy_to_guest_phys_flush_cache() would not matter as this only applies to the destination.

Regarding the source side, the cache only guarantees coherency when either:
* the memory attributes are similar between the component that loaded
it (most likely the firmware) and Xen
* The component that loaded it issued a cache flush

I saw Jan mentioned that other part of the device-tree code are using ioremap_cache(). They were adding afterwards and I can't remember why we consisder it was ok. For older mapping we would be switching from a non-cacheable mapping (HYPERVISOR_WC) to a cacheable one (HYPERVISOR).

While I agree we should have some consistency in the mappings, I am a bit unease to change the attributes without any written details on why there will not be any impact. Can you share more details?

I would also be ok if we decide to keep ioremap_wc() for arm as well.

Cheers,

Signature · 1 line
Julien Grall
#12 lore raw

Re: [PATCH 2/2] make ioremap_wc() x86 only (for the time being)

Quoted 37 lines · Julien Grall

Hi Oleksii,

Quoted 23 lines · Oleksii Kurochko
Quoted 20 lines · Oleksii Kurochko
Quoted 10 lines · Jan Beulich

Its use in domain building is questionable: Already at the point both uses
were introduced, ioremap_cache() existed. I can't see why kernel and initrd would need mapping WC, when at the same time other similar mappings
(in common/device-tree/) are done WB.

With those uses replaced, neither Arm nor RISC-V have a need for the function anymore.

It is okay for RISC-V, but for Arm, IIRC, likely it was done because guest might start with cache disabled and PAGE_HYPERVISOR_WC on Arm uses "Normal Non-cacheable" what could lead to that guest won't see some part of kernel and/or initrd as it could be in a cache and won't be flushed to RAM.

Quoted 6 lines
Quoted 3 lines

But probably I misremembered something and clean_and_invalidate_dcache_va_range() or something similar should be called before guest is laucned.

okay, so for this case copy_to_guest_phys_flush_dcache() is called after initrd and kernel are loaded.

Looking at the code, we are using ioremap_wc() on the source side rather than the destination. So the use of copy_to_guest_phys_flush_cache() would not matter as this only applies to the destination.

Quoted 8 lines

Regarding the source side, the cache only guarantees coherency when either:
* the memory attributes are similar between the component that loaded
it (most likely the firmware) and Xen
* The component that loaded it issued a cache flush

I saw Jan mentioned that other part of the device-tree code are using ioremap_cache(). They were adding afterwards and I can't remember why we consisder it was ok.

This sounds like you think it isn't okay.

Quoted 2 lines

For older mapping we would be switching from a non-cacheable mapping (HYPERVISOR_WC) to a cacheable one (HYPERVISOR).

Whereas this sounds like you think switching everything to WB (or whatever the correct term on Arm) would be okay. IOW - I'm confused, and hence I'm
...

Quoted 3 lines

While I agree we should have some consistency in the mappings, I am a bit unease to change the attributes without any written details on why there will not be any impact.

... also unclear as to whether this actually is an objection to the Arm part of the change (incl patch 1). Please clarify.

As to written details: Mappings of ordinary RAM shouldn't need any extra documentation when done WB. Any non-standard mappings (like WC) should instead have comments. The absence of such comments indicates to me that at the time the "best thing thought to be available" was used, i.e. WC preferred over UC (which plain ioremap() would have used). Additionally, the use of ioremap_wc() from initrd_load() (not Arm-specific) pretty clearly isn't appropriate for RISC-V, aiui.

That said, using ioremap*() for RAM feels bogus in the first place. At the time vmap() didn't exist yet, so it may have been appropriate. If there are reasons not to use it here, perhaps something like Linux'es memremap() should long have been introduced?

Jan