Skip to content

Commit 3318f7b

Browse files
robclarkwilldeacon
authored andcommitted
iommu/io-pgtable-arm: Add quirk to quiet WARN_ON()
In situations where mapping/unmapping sequence can be controlled by userspace, attempting to map over a region that has not yet been unmapped is an error. But not something that should spam dmesg. Now that there is a quirk, we can also drop the selftest_running flag, and use the quirk instead for selftests. Acked-by: Robin Murphy <[email protected]> Signed-off-by: Rob Clark <[email protected]> Link: https://lore.kernel.org/r/[email protected] [will: Rename quirk to IO_PGTABLE_QUIRK_NO_WARN per Robin's suggestion] Signed-off-by: Will Deacon <[email protected]>
1 parent be5a2d3 commit 3318f7b

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

drivers/iommu/io-pgtable-arm.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,6 @@ static inline bool arm_lpae_concat_mandatory(struct io_pgtable_cfg *cfg,
251251
(data->start_level == 1) && (oas == 40);
252252
}
253253

254-
static bool selftest_running = false;
255-
256254
static dma_addr_t __arm_lpae_dma_addr(void *pages)
257255
{
258256
return (dma_addr_t)virt_to_phys(pages);
@@ -371,7 +369,7 @@ static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
371369
for (i = 0; i < num_entries; i++)
372370
if (iopte_leaf(ptep[i], lvl, data->iop.fmt)) {
373371
/* We require an unmap first */
374-
WARN_ON(!selftest_running);
372+
WARN_ON(!(data->iop.cfg.quirks & IO_PGTABLE_QUIRK_NO_WARN));
375373
return -EEXIST;
376374
} else if (iopte_type(ptep[i]) == ARM_LPAE_PTE_TYPE_TABLE) {
377375
/*
@@ -473,7 +471,7 @@ static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
473471
cptep = iopte_deref(pte, data);
474472
} else if (pte) {
475473
/* We require an unmap first */
476-
WARN_ON(!selftest_running);
474+
WARN_ON(!(cfg->quirks & IO_PGTABLE_QUIRK_NO_WARN));
477475
return -EEXIST;
478476
}
479477

@@ -641,8 +639,10 @@ static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
641639
unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
642640
ptep += unmap_idx_start;
643641
pte = READ_ONCE(*ptep);
644-
if (WARN_ON(!pte))
645-
return 0;
642+
if (!pte) {
643+
WARN_ON(!(data->iop.cfg.quirks & IO_PGTABLE_QUIRK_NO_WARN));
644+
return -ENOENT;
645+
}
646646

647647
/* If the size matches this level, we're in the right place */
648648
if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) {
@@ -652,8 +652,10 @@ static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
652652
/* Find and handle non-leaf entries */
653653
for (i = 0; i < num_entries; i++) {
654654
pte = READ_ONCE(ptep[i]);
655-
if (WARN_ON(!pte))
655+
if (!pte) {
656+
WARN_ON(!(data->iop.cfg.quirks & IO_PGTABLE_QUIRK_NO_WARN));
656657
break;
658+
}
657659

658660
if (!iopte_leaf(pte, lvl, iop->fmt)) {
659661
__arm_lpae_clear_pte(&ptep[i], &iop->cfg, 1);
@@ -968,7 +970,8 @@ arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
968970
if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
969971
IO_PGTABLE_QUIRK_ARM_TTBR1 |
970972
IO_PGTABLE_QUIRK_ARM_OUTER_WBWA |
971-
IO_PGTABLE_QUIRK_ARM_HD))
973+
IO_PGTABLE_QUIRK_ARM_HD |
974+
IO_PGTABLE_QUIRK_NO_WARN))
972975
return NULL;
973976

974977
data = arm_lpae_alloc_pgtable(cfg);
@@ -1069,7 +1072,8 @@ arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
10691072
struct arm_lpae_io_pgtable *data;
10701073
typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr;
10711074

1072-
if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_S2FWB))
1075+
if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_S2FWB |
1076+
IO_PGTABLE_QUIRK_NO_WARN))
10731077
return NULL;
10741078

10751079
data = arm_lpae_alloc_pgtable(cfg);
@@ -1310,7 +1314,6 @@ static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
13101314
#define __FAIL(ops, i) ({ \
13111315
WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
13121316
arm_lpae_dump_ops(ops); \
1313-
selftest_running = false; \
13141317
-EFAULT; \
13151318
})
13161319

@@ -1326,8 +1329,6 @@ static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
13261329
size_t size, mapped;
13271330
struct io_pgtable_ops *ops;
13281331

1329-
selftest_running = true;
1330-
13311332
for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
13321333
cfg_cookie = cfg;
13331334
ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
@@ -1416,7 +1417,6 @@ static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
14161417
free_io_pgtable_ops(ops);
14171418
}
14181419

1419-
selftest_running = false;
14201420
return 0;
14211421
}
14221422

@@ -1438,6 +1438,7 @@ static int __init arm_lpae_do_selftests(void)
14381438
.tlb = &dummy_tlb_ops,
14391439
.coherent_walk = true,
14401440
.iommu_dev = &dev,
1441+
.quirks = IO_PGTABLE_QUIRK_NO_WARN,
14411442
};
14421443

14431444
/* __arm_lpae_alloc_pages() merely needs dev_to_node() to work */

include/linux/io-pgtable.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ struct io_pgtable_cfg {
8888
*
8989
* IO_PGTABLE_QUIRK_ARM_HD: Enables dirty tracking in stage 1 pagetable.
9090
* IO_PGTABLE_QUIRK_ARM_S2FWB: Use the FWB format for the MemAttrs bits
91+
*
92+
* IO_PGTABLE_QUIRK_NO_WARN: Do not WARN_ON() on conflicting
93+
* mappings, but silently return -EEXISTS. Normally an attempt
94+
* to map over an existing mapping would indicate some sort of
95+
* kernel bug, which would justify the WARN_ON(). But for GPU
96+
* drivers, this could be under control of userspace. Which
97+
* deserves an error return, but not to spam dmesg.
9198
*/
9299
#define IO_PGTABLE_QUIRK_ARM_NS BIT(0)
93100
#define IO_PGTABLE_QUIRK_NO_PERMS BIT(1)
@@ -97,6 +104,7 @@ struct io_pgtable_cfg {
97104
#define IO_PGTABLE_QUIRK_ARM_OUTER_WBWA BIT(6)
98105
#define IO_PGTABLE_QUIRK_ARM_HD BIT(7)
99106
#define IO_PGTABLE_QUIRK_ARM_S2FWB BIT(8)
107+
#define IO_PGTABLE_QUIRK_NO_WARN BIT(9)
100108
unsigned long quirks;
101109
unsigned long pgsize_bitmap;
102110
unsigned int ias;

0 commit comments

Comments
 (0)