ITS command queues and tables are shared with the host in a confidential
computing guest.

Route allocations through alloc_cc_shared_node(), which rounds the byte
request internally and returns a naturally aligned linear-map address.
Release whole allocations with free_cc_shared(), without reconstructing
metadata.

Use addresses directly where possible and convert to struct page for the
existing property and pending table interfaces. Add the actual allocation
size returned by cc_shared_alloc_size() to the ITT pool.

Cc: Marc Zyngier <[email protected]>
Signed-off-by: Aneesh Kumar K.V (Arm) <[email protected]>
---
 drivers/irqchip/irq-gic-v3-its.c | 142 ++++++++++++-------------------
 1 file changed, 56 insertions(+), 86 deletions(-)

diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
index ada5c30af41e..cc64384109de 100644
--- a/drivers/irqchip/irq-gic-v3-its.c
+++ b/drivers/irqchip/irq-gic-v3-its.c
@@ -8,6 +8,7 @@
 #include <linux/acpi_iort.h>
 #include <linux/bitfield.h>
 #include <linux/bitmap.h>
+#include <linux/cc_shared.h>
 #include <linux/cpu.h>
 #include <linux/crash_dump.h>
 #include <linux/delay.h>
@@ -19,7 +20,6 @@
 #include <linux/irqdomain.h>
 #include <linux/list.h>
 #include <linux/log2.h>
-#include <linux/mem_encrypt.h>
 #include <linux/memblock.h>
 #include <linux/mm.h>
 #include <linux/msi.h>
@@ -29,7 +29,6 @@
 #include <linux/of_pci.h>
 #include <linux/of_platform.h>
 #include <linux/percpu.h>
-#include <linux/set_memory.h>
 #include <linux/slab.h>
 #include <linux/syscore_ops.h>
 #include <linux/topology.h>
@@ -213,47 +212,18 @@ static DEFINE_IDA(its_vpeid_ida);
 
 static gfp_t gfp_flags_quirk;
 
-static struct page *its_alloc_pages_node(int node, gfp_t gfp,
-                                        unsigned int order)
+static void *its_alloc_node(int node, gfp_t gfp, unsigned int order)
 {
-       struct page *page;
-       int ret = 0;
-
        if (node == NUMA_NO_NODE)
                node = numa_mem_id();
 
-       page = alloc_pages_node(node, gfp | gfp_flags_quirk, order);
-
-       if (!page)
-               return NULL;
-
-       ret = set_memory_decrypted((unsigned long)page_address(page),
-                                  1 << order);
-       /*
-        * If set_memory_decrypted() fails then we don't know what state the
-        * page is in, so we can't free it. Instead we leak it.
-        * set_memory_decrypted() will already have WARNed.
-        */
-       if (ret)
-               return NULL;
-
-       return page;
-}
-
-static struct page *its_alloc_pages(gfp_t gfp, unsigned int order)
-{
-       return its_alloc_pages_node(NUMA_NO_NODE, gfp, order);
+       return alloc_cc_shared_node(node, gfp | gfp_flags_quirk,
+                                   PAGE_SIZE << order);
 }
 
-static void its_free_pages(void *addr, unsigned int order)
+static void *its_alloc(gfp_t gfp, unsigned int order)
 {
-       /*
-        * If the memory cannot be encrypted again then we must leak the pages.
-        * set_memory_encrypted() will already have WARNed.
-        */
-       if (set_memory_encrypted((unsigned long)addr, 1 << order))
-               return;
-       free_pages((unsigned long)addr, order);
+       return its_alloc_node(NUMA_NO_NODE, gfp, order);
 }
 
 static struct gen_pool *itt_pool;
@@ -261,24 +231,27 @@ static struct gen_pool *itt_pool;
 static void *itt_alloc_pool(int node, int size)
 {
        unsigned long addr;
-       struct page *page;
+       void *table_base;
 
-       if (size >= PAGE_SIZE) {
-               page = its_alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 
get_order(size));
-
-               return page ? page_address(page) : NULL;
-       }
+       if (size >= PAGE_SIZE)
+               return its_alloc_node(node, GFP_KERNEL | __GFP_ZERO, 
get_order(size));
 
        do {
                addr = gen_pool_alloc(itt_pool, size);
                if (addr)
                        break;
 
-               page = its_alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
-               if (!page)
+               table_base = its_alloc_node(node, GFP_KERNEL | __GFP_ZERO, 0);
+               if (!table_base)
                        break;
 
-               gen_pool_add(itt_pool, (unsigned long)page_address(page), 
PAGE_SIZE, node);
+               /*
+                * Sharing-granule rounding may allocate more than PAGE_SIZE.
+                * Add the entire shared allocation to the pool so the extra
+                * space can be used for other ITTs.
+                */
+               gen_pool_add(itt_pool, (unsigned long)table_base,
+                            cc_shared_alloc_size(table_base), node);
        } while (!addr);
 
        return (void *)addr;
@@ -290,7 +263,7 @@ static void itt_free_pool(void *addr, int size)
                return;
 
        if (size >= PAGE_SIZE) {
-               its_free_pages(addr, get_order(size));
+               free_cc_shared(addr);
                return;
        }
 
@@ -2270,21 +2243,20 @@ static void gic_reset_prop_table(void *va)
 
 static struct page *its_allocate_prop_table(gfp_t gfp_flags)
 {
-       struct page *prop_page;
+       void *addr;
 
-       prop_page = its_alloc_pages(gfp_flags,
-                                   get_order(LPI_PROPBASE_SZ));
-       if (!prop_page)
+       addr = its_alloc(gfp_flags, get_order(LPI_PROPBASE_SZ));
+       if (!addr)
                return NULL;
 
-       gic_reset_prop_table(page_address(prop_page));
+       gic_reset_prop_table(addr);
 
-       return prop_page;
+       return virt_to_page(addr);
 }
 
 static void its_free_prop_table(struct page *prop_page)
 {
-       its_free_pages(page_address(prop_page), get_order(LPI_PROPBASE_SZ));
+       free_cc_shared(page_address(prop_page));
 }
 
 static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
@@ -2393,7 +2365,6 @@ static int its_setup_baser(struct its_node *its, struct 
its_baser *baser,
        u64 type = GITS_BASER_TYPE(val);
        u64 baser_phys, tmp;
        u32 alloc_pages, psz;
-       struct page *page;
        void *base;
 
        psz = baser->psz;
@@ -2406,11 +2377,10 @@ static int its_setup_baser(struct its_node *its, struct 
its_baser *baser,
                order = get_order(GITS_BASER_PAGES_MAX * psz);
        }
 
-       page = its_alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, 
order);
-       if (!page)
+       base = its_alloc_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order);
+       if (!base)
                return -ENOMEM;
 
-       base = (void *)page_address(page);
        baser_phys = virt_to_phys(base);
 
        /* Check if the physical address of the memory is above 48bits */
@@ -2419,7 +2389,7 @@ static int its_setup_baser(struct its_node *its, struct 
its_baser *baser,
                /* 52bit PA is supported only when PageSize=64K */
                if (psz != SZ_64K) {
                        pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
-                       its_free_pages(base, order);
+                       free_cc_shared(base);
                        return -ENXIO;
                }
 
@@ -2475,7 +2445,7 @@ static int its_setup_baser(struct its_node *its, struct 
its_baser *baser,
                pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
                       &its->phys_base, its_base_type_string[type],
                       val, tmp);
-               its_free_pages(base, order);
+               free_cc_shared(base);
                return -ENXIO;
        }
 
@@ -2612,7 +2582,7 @@ static void its_free_tables(struct its_node *its)
        for (i = 0; i < GITS_BASER_NR_REGS; i++) {
                if (its->tables[i].base) {
                        if (!its->tables[i].shared)
-                               its_free_pages(its->tables[i].base, 
its->tables[i].order);
+                               free_cc_shared(its->tables[i].base);
                        its->tables[i].base = NULL;
                }
        }
@@ -2837,7 +2807,7 @@ static bool allocate_vpe_l2_table(int cpu, u32 id)
        void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
        unsigned int psz, esz, idx, npg, gpsz;
        u64 val;
-       struct page *page;
+       void *table_base;
        __le64 *table;
 
        if (!gic_rdists->has_rvpeid)
@@ -2881,15 +2851,15 @@ static bool allocate_vpe_l2_table(int cpu, u32 id)
 
        /* Allocate memory for 2nd level table */
        if (!table[idx]) {
-               page = its_alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(psz));
-               if (!page)
+               table_base = its_alloc(GFP_KERNEL | __GFP_ZERO, get_order(psz));
+               if (!table_base)
                        return false;
 
                /* Flush Lvl2 table to PoC if hw doesn't support coherency */
                if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
-                       gic_flush_dcache_to_poc(page_address(page), psz);
+                       gic_flush_dcache_to_poc(table_base, psz);
 
-               table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
+               table[idx] = cpu_to_le64(virt_to_phys(table_base) | 
GITS_BASER_VALID);
 
                /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
                if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
@@ -2908,7 +2878,7 @@ static int allocate_vpe_l1_table(void)
        u64 val, gpsz, npg, pa;
        unsigned int psz = SZ_64K;
        unsigned int np, epp, esz;
-       struct page *page;
+       void *table_base;
 
        if (!gic_rdists->has_rvpeid)
                return 0;
@@ -3000,12 +2970,12 @@ static int allocate_vpe_l1_table(void)
 
        pr_debug("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n",
                 np, npg, psz, epp, esz);
-       page = its_alloc_pages(GFP_ATOMIC | __GFP_ZERO, get_order(np * 
PAGE_SIZE));
-       if (!page)
+       table_base = its_alloc(GFP_ATOMIC | __GFP_ZERO, get_order(np * 
PAGE_SIZE));
+       if (!table_base)
                return -ENOMEM;
 
-       gic_data_rdist()->vpe_l1_base = page_address(page);
-       pa = virt_to_phys(page_address(page));
+       gic_data_rdist()->vpe_l1_base = table_base;
+       pa = virt_to_phys(table_base);
        WARN_ON(!IS_ALIGNED(pa, psz));
 
        val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, pa >> 12);
@@ -3043,21 +3013,21 @@ static int its_alloc_collections(struct its_node *its)
 
 static struct page *its_allocate_pending_table(gfp_t gfp_flags)
 {
-       struct page *pend_page;
+       void *addr;
 
-       pend_page = its_alloc_pages(gfp_flags | __GFP_ZERO, 
get_order(LPI_PENDBASE_SZ));
-       if (!pend_page)
+       addr = its_alloc(gfp_flags | __GFP_ZERO, get_order(LPI_PENDBASE_SZ));
+       if (!addr)
                return NULL;
 
        /* Make sure the GIC will observe the zero-ed page */
-       gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
+       gic_flush_dcache_to_poc(addr, LPI_PENDBASE_SZ);
 
-       return pend_page;
+       return virt_to_page(addr);
 }
 
 static void its_free_pending_table(struct page *pt)
 {
-       its_free_pages(page_address(pt), get_order(LPI_PENDBASE_SZ));
+       free_cc_shared(page_address(pt));
 }
 
 /*
@@ -3372,7 +3342,7 @@ static struct its_baser *its_get_baser(struct its_node 
*its, u32 type)
 static bool its_alloc_table_entry(struct its_node *its,
                                  struct its_baser *baser, u32 id)
 {
-       struct page *page;
+       void *table_base;
        u32 esz, idx;
        __le64 *table;
 
@@ -3390,16 +3360,16 @@ static bool its_alloc_table_entry(struct its_node *its,
 
        /* Allocate memory for 2nd level table */
        if (!table[idx]) {
-               page = its_alloc_pages_node(its->numa_node, GFP_KERNEL | 
__GFP_ZERO,
+               table_base = its_alloc_node(its->numa_node, GFP_KERNEL | 
__GFP_ZERO,
                                            get_order(baser->psz));
-               if (!page)
+               if (!table_base)
                        return false;
 
                /* Flush Lvl2 table to PoC if hw doesn't support coherency */
                if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
-                       gic_flush_dcache_to_poc(page_address(page), baser->psz);
+                       gic_flush_dcache_to_poc(table_base, baser->psz);
 
-               table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
+               table[idx] = cpu_to_le64(virt_to_phys(table_base) | 
GITS_BASER_VALID);
 
                /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
                if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
@@ -5257,7 +5227,7 @@ static int __init its_compute_its_list_map(struct 
its_node *its)
 static int __init its_probe_one(struct its_node *its)
 {
        u64 baser, tmp;
-       struct page *page;
+       void *table_base;
        u32 ctlr;
        int err;
 
@@ -5293,14 +5263,14 @@ static int __init its_probe_one(struct its_node *its)
                }
        }
 
-       page = its_alloc_pages_node(its->numa_node,
+       table_base = its_alloc_node(its->numa_node,
                                    GFP_KERNEL | __GFP_ZERO,
                                    get_order(ITS_CMD_QUEUE_SZ));
-       if (!page) {
+       if (!table_base) {
                err = -ENOMEM;
                goto out_unmap_sgir;
        }
-       its->cmd_base = (void *)page_address(page);
+       its->cmd_base = table_base;
        its->cmd_write = its->cmd_base;
 
        err = its_alloc_tables(its);
@@ -5363,7 +5333,7 @@ static int __init its_probe_one(struct its_node *its)
 out_free_tables:
        its_free_tables(its);
 out_free_cmd:
-       its_free_pages(its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
+       free_cc_shared(its->cmd_base);
 out_unmap_sgir:
        if (its->sgir_base)
                iounmap(its->sgir_base);
-- 
2.43.0

Reply via email to