irqchip/gic-v3-its: Fixes to table allocation and the ITT pool
v3 cover irqchip/gic-v3-its: Fixes to table allocation and the ITT pool
Hi folks,
Changes since v2 [1]:
- Dropped __GFP_ZERO from the pages added to the ITT pool. (Marc)
- Added Cc: stable to patch 3. (Marc)
- Changed patch 2's prefix to irqchip/gic-v4.1. (Zenghui)
- Collected Reviewed-by from Zenghui. (thanks!)
Five fixes to how the ITS driver sets up the memory the GIC reads.
The first adds the barrier that was missing between zeroing a table page and the write that makes the page visible to the GIC. The second cleans the GICv4.1 vPE table to PoC, as the driver already does for the pending table. Without the clean, only a redistributor that accesses the table as non-shareable can read stale contents from it.
The other three fix the ITT pool. Its 1-byte granule keeps ITTs aligned only while every ITT's size is a multiple of 256, which holds for power-of-two ITE sizes but not for QEMU's 12-byte ones. The third gives the pool a 256-byte granule. The fourth zeroes an ITT reused from the pool, as MAPD requires. The fifth frees the page when gen_pool_add() fails.
With patch 4, a pKVM guest shares each new pool page with the host before anything zeroes it. The pkvm-guest patch [2] makes pkvm_set_memory_decrypted() zero what it shares, which closes that.
Based on Linux 7.4-rc3 (aebb727447640).
Cheers,
/fuad
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Fuad Tabba (5):
irqchip/gic-v3-its: Order table zeroing before the GIC can see it irqchip/gic-v4.1: Clean the vPE table to PoC irqchip/gic-v3-its: Align sub-page ITTs to 256 bytes irqchip/gic-v3-its: Zero ITTs reused from the ITT pool irqchip/gic-v3-its: Free the page when gen_pool_add() fails
drivers/irqchip/irq-gic-v3-its.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.47.3
PATCH v3 1/5 irqchip/gic-v3-its: Order table zeroing before the GIC can see it
The ITS driver allocates the ITS tables and the vPE tables as zeroed pages. It exposes each table with a single store: a relaxed GICR_VPROPBASER write with Valid set, the relaxed GITS_CTLR write that enables the ITS, or a valid level-1 entry for a level-2 page. However, on a shareable table the zeroing isn't ordered before that store, so the GIC can read the page's previous contents as table entries, which the spec makes UNPREDICTABLE (IHI 0069H.b 5.2.1, and GICR_VPROPBASER.Z in 12.11.39). A level-2 page is exposed at runtime, when the GIC may already be walking the level-1 table for other IDs.
Add a dsb(ishst) at the end of its_alloc_pages_node(), as its_flush_cmd() does for a command. The allocator doesn't take a shareability argument, so the barrier is unconditional. On a non-shareable table it doesn't hurt.
--- drivers/irqchip/irq-gic-v3-its.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/irqchip/irq-gic-v3-its.c +3 -0
@@ -233,6 +233,9 @@ static struct page *its_alloc_pages_node(int node, gfp_t gfp,if (ret)return NULL ; +/* Order the zeroing before any store that exposes the page to the GIC */ +dsb (ishst); +return page; }
Signature · 1 line
2.47.3
PATCH v3 2/5 irqchip/gic-v4.1: Clean the vPE table to PoC
In reply to Fuad TabbaOn a GICv4.1 system, allocate_vpe_l1_table() sets Z and Valid in GICR_VPROPBASER and points it at a freshly zeroed vPE table, but leaves the zeroes in the CPU's cache. A redistributor that accesses the table as non-shareable reads it from memory, where it might not be zero, and the spec makes that UNPREDICTABLE (GICR_VPROPBASER.Z, IHI 0069H.b 12.11.39).
Clean the table to PoC unconditionally, as the driver does for the pending table. Testing rdists_support_shareable() isn't enough, since GICR_VPROPBASER can read back as non-shareable even when the helper returns true. allocate_vpe_l2_table() already cleans the level-2 pages in that case. A vPE table inherited from an ITS is cleaned by its_setup_baser() when the ITS is non-shareable.
--- drivers/irqchip/irq-gic-v3-its.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/irqchip/irq-gic-v3-its.c +3 -0
@@ -3007,6 +3007,9 @@ static int allocate_vpe_l1_table(void) pa =virt_to_phys (page_address (page));WARN_ON (!IS_ALIGNED (pa, psz)); +/* Make sure the GIC will observe the zeroed table */ +gic_flush_dcache_to_poc (page_address (page), np *PAGE_SIZE ); + val |=FIELD_PREP (GICR_VPROPBASER_4_1_ADDR , pa >>12 );if (rdists_support_shareable ()) { val |= GICR_VPROPBASER_RaWb;
Signature · 1 line
2.47.3
PATCH v3 3/5 irqchip/gic-v3-its: Align sub-page ITTs to 256 bytes
In reply to Fuad TabbaCommit e36d4165f0796 ("irqchip/gic-v3-its: Rely on genpool alignment") dropped the ALIGN() of the ITT address in MAPD, on the premise that the ITT pool returns 256-byte aligned blocks. However, the pool's minimum allocation order is get_order(ITS_ITT_ALIGN), which is 0, so its granule is a single byte. An ITT whose size is not a multiple of 256 leaves the next device's ITT misaligned. MAPD drops the low bits of its address, so that device's ITEs land on the previous device's ITT. QEMU's emulated ITS has 12-byte ITEs, so two devices with 32 vectors each are enough to hit it.
Create the pool with a granule of ITS_ITT_ALIGN.
--- drivers/irqchip/irq-gic-v3-its.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/irqchip/irq-gic-v3-its.c +1 -1
@@ -5854,7 +5854,7 @@ int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,bool has_v4_1 = false;int err; - itt_pool =gen_pool_create (get_order (ITS_ITT_ALIGN ),-1 ); + itt_pool =gen_pool_create (ilog2 (ITS_ITT_ALIGN ),-1 );if (!itt_pool)return -ENOMEM ;
Signature · 1 line
2.47.3
PATCH v3 4/5 irqchip/gic-v3-its: Zero ITTs reused from the ITT pool
In reply to Fuad TabbaCommit b08e2f42e86b5 ("irqchip/gic-v3-its: Share ITS tables with a non-trusted hypervisor") moved sub-page ITTs from kzalloc_node() to a genpool. An ITT freed back to the pool goes to the next device as the previous one left it. MAPD is then UNPREDICTABLE: "When V == 1, the specified ITT does not contain all zeros" (IHI 0069H.b, 5.3.10).
Zero every ITT that comes from the pool. The ITS then no longer needs the pages added to the pool zeroed, so drop __GFP_ZERO from their allocation. A pKVM guest shares each new pool page with the host, though, and pkvm_set_memory_decrypted() doesn't zero what it shares, so the host now sees that page's old contents.
--- drivers/irqchip/irq-gic-v3-its.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/irqchip/irq-gic-v3-its.c +4 -2
@@ -270,10 +270,12 @@ static void *itt_alloc_pool(int node, int size) do { addr = gen_pool_alloc(itt_pool, size); - if (addr) + if (addr) { + memset((void *)addr, 0, size); break; + } - page = its_alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0); + page = its_alloc_pages_node(node, GFP_KERNEL, 0); if (!page) break;
Signature · 1 line
2.47.3
PATCH v3 5/5 irqchip/gic-v3-its: Free the page when gen_pool_add() fails
In reply to Fuad TabbaWhen the ITT pool has no room, itt_alloc_pool() grows it by a page and retries. gen_pool_add() fails when it cannot allocate its chunk descriptor, but its return is ignored, so the page is leaked and the retry allocates another page to leak, until an add succeeds or a page allocation fails.
Free the page and fail the allocation instead.
--- drivers/irqchip/irq-gic-v3-its.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/irqchip/irq-gic-v3-its.c +4 -1
@@ -279,7 +279,10 @@ static void *itt_alloc_pool(int node, int size)if (!page)break ; -gen_pool_add (itt_pool, (unsigned long )page_address (page),PAGE_SIZE , node); +if (gen_pool_add (itt_pool, (unsigned long )page_address (page),PAGE_SIZE , node)) { +its_free_pages (page_address (page),0 ); +break ; + } }while (!addr);return (void *)addr;
Signature · 1 line
2.47.3