[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC PATCH kernel 06/17] x86/io/tsm: Allow mixed ioremap for shared+private BARs



A TDISP device advertises private MMIO ranges via a TDI report.
Upon transitioning to RUN, accesses to those must be made with
the encrypted bit set in a PTE.

A device can allow private access to a part of a BAR, for example,
MSIX BAR (when MSIX config is not locked or MSIX is emulated by the HV).

The existing __ioremap_caller() fails if not every single page has
the same protection in PTE and this breaks device drivers (usually
TDISP-unaware) calling pci_iomap() and such.

Adjust __ioremap_caller() to map such MMIO BAR in chunks.

Signed-off-by: Alexey Kardashevskiy <aik@xxxxxxx>
---
 include/linux/io.h     |  8 +++
 include/linux/ioport.h |  3 +
 arch/x86/mm/ioremap.c  |  2 +-
 kernel/resource.c      | 63 ++++++++++++++++++++
 mm/ioremap.c           |  2 +-
 5 files changed, 76 insertions(+), 2 deletions(-)

diff --git a/include/linux/io.h b/include/linux/io.h
index 0642c7ee41db..9f5d5928f30c 100644
--- a/include/linux/io.h
+++ b/include/linux/io.h
@@ -27,6 +27,8 @@ void __iowrite64_copy(void __iomem *to, const void *from, 
size_t count);
 #ifdef CONFIG_MMU
 int ioremap_page_range(unsigned long addr, unsigned long end,
                       phys_addr_t phys_addr, pgprot_t prot);
+int ioremap_map_page_range(unsigned long vaddr, phys_addr_t phys_addr,
+                          unsigned long size, pgprot_t prot);
 int vmap_page_range(unsigned long addr, unsigned long end,
                    phys_addr_t phys_addr, pgprot_t prot);
 #else
@@ -35,6 +37,12 @@ static inline int ioremap_page_range(unsigned long addr, 
unsigned long end,
 {
        return 0;
 }
+static inline int ioremap_map_page_range(unsigned long vaddr,
+                                        phys_addr_t phys_addr,
+                                        unsigned long size, pgprot_t prot)
+{
+       return 0;
+}
 static inline int vmap_page_range(unsigned long addr, unsigned long end,
                                  phys_addr_t phys_addr, pgprot_t prot)
 {
diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index 122f1eefb4b9..49d35b31f22d 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -444,8 +444,11 @@ walk_iomem_res_desc(unsigned long desc, unsigned long 
flags, u64 start, u64 end,
                    void *arg, int (*func)(struct resource *, void *));
 extern int walk_soft_reserve_res(u64 start, u64 end, void *arg,
                                 int (*func)(struct resource *, void *));
+extern int walk_encrypted_mem_res(u64 start, u64 end, void *arg,
+                                 int (*func)(struct resource *, void *));
 extern int
 region_intersects_soft_reserve(resource_size_t start, size_t size);
+extern int region_intersects_encrypted(resource_size_t start, size_t size);
 
 struct resource *devm_request_free_mem_region(struct device *dev,
                struct resource *base, unsigned long size);
diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c
index 12c8180ca1ba..976ce6d5cf70 100644
--- a/arch/x86/mm/ioremap.c
+++ b/arch/x86/mm/ioremap.c
@@ -298,7 +298,7 @@ __ioremap_caller(resource_size_t phys_addr, unsigned long 
size,
        if (memtype_kernel_map_sync(phys_addr, size, pcm))
                goto err_free_area;
 
-       if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot))
+       if (ioremap_map_page_range(vaddr, phys_addr, size, prot))
                goto err_free_area;
 
        ret_addr = (void __iomem *) (vaddr + offset);
diff --git a/kernel/resource.c b/kernel/resource.c
index 1b9cf2244b3f..c575ed87d67a 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -26,6 +26,8 @@
 #include <linux/mm.h>
 #include <linux/mount.h>
 #include <linux/resource_ext.h>
+#include <linux/cc_platform.h>
+#include <linux/io.h>
 #include <uapi/linux/magic.h>
 #include <linux/string.h>
 #include <linux/vmalloc.h>
@@ -713,6 +715,67 @@ int region_intersects_soft_reserve(resource_size_t start, 
size_t size)
 }
 EXPORT_SYMBOL_GPL(region_intersects_soft_reserve);
 
+/*
+ * Walk encrypted MMIO ranges registered for TDISP/TSM private device
+ * regions (see encrypted_iomem_resource).
+ */
+int walk_encrypted_mem_res(u64 start, u64 end, void *arg,
+                          int (*func)(struct resource *, void *))
+{
+       return walk_res_desc(&encrypted_iomem_resource, start, end,
+                            IORESOURCE_MEM, IORES_DESC_ENCRYPTED, arg, func);
+}
+EXPORT_SYMBOL_GPL(walk_encrypted_mem_res);
+
+int region_intersects_encrypted(resource_size_t start, size_t size)
+{
+       guard(read_lock)(&resource_lock);
+       return __region_intersects(&encrypted_iomem_resource, start, size,
+                                  IORESOURCE_MEM, IORES_DESC_ENCRYPTED);
+}
+EXPORT_SYMBOL_GPL(region_intersects_encrypted);
+
+#ifdef CONFIG_MMU
+/*
+ * Map @size bytes at @vaddr to @phys_addr using @prot, splitting the mapping
+ * when TDISP/TSM has registered only part of the range in
+ * encrypted_iomem_resource and per-page encryption attributes are required.
+ */
+int ioremap_map_page_range(unsigned long vaddr, phys_addr_t phys_addr,
+                          unsigned long size, pgprot_t prot)
+{
+       if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) ||
+           region_intersects_encrypted(phys_addr, size) == REGION_DISJOINT)
+               return ioremap_page_range(vaddr, vaddr + size, phys_addr, prot);
+
+       while (size) {
+               bool enc = region_intersects_encrypted(phys_addr, PAGE_SIZE) ==
+                          REGION_INTERSECTS;
+               unsigned long run = PAGE_SIZE;
+               pgprot_t run_prot = enc ? pgprot_encrypted(prot) : prot;
+
+               phys_addr += PAGE_SIZE;
+               vaddr += PAGE_SIZE;
+               size -= PAGE_SIZE;
+
+               while (size &&
+                      (region_intersects_encrypted(phys_addr, PAGE_SIZE) ==
+                       REGION_INTERSECTS) == enc) {
+                       run += PAGE_SIZE;
+                       phys_addr += PAGE_SIZE;
+                       vaddr += PAGE_SIZE;
+                       size -= PAGE_SIZE;
+               }
+
+               if (vmap_page_range(vaddr - run, vaddr, phys_addr - run, 
run_prot))
+                       return -EINVAL;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL(ioremap_map_page_range);
+#endif /* CONFIG_MMU */
+
 void __weak arch_remove_reservations(struct resource *avail)
 {
 }
diff --git a/mm/ioremap.c b/mm/ioremap.c
index c36dd9f62fd5..ccf938628e55 100644
--- a/mm/ioremap.c
+++ b/mm/ioremap.c
@@ -40,7 +40,7 @@ void __iomem *generic_ioremap_prot(phys_addr_t phys_addr, 
size_t size,
        vaddr = (unsigned long)area->addr;
        area->phys_addr = phys_addr;
 
-       if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot)) {
+       if (ioremap_map_page_range(vaddr, phys_addr, size, prot)) {
                free_vm_area(area);
                return NULL;
        }
-- 
2.55.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.