Skip to content

Commit 8d00b77

Browse files
jgunthorpejoergroedel
authored andcommitted
iommu/amd: Move allocation of the top table into v1_alloc_pgtable
All the page table memory should be allocated/free within the io_pgtable struct. The v2 path is already doing this, make it consistent. It is hard to see but the free of the root in protection_domain_free() is a NOP on the success path because v1_free_pgtable() does amd_iommu_domain_clr_pt_root(). The root memory is already freed because free_sub_pt() put it on the freelist. The free path in protection_domain_free() is only used during error unwind of protection_domain_alloc(). Reviewed-by: Vasant Hegde <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent 89ffb2c commit 8d00b77

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

drivers/iommu/amd/io_pgtable.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,20 +573,24 @@ static void v1_free_pgtable(struct io_pgtable *iop)
573573
pgtable->mode > PAGE_MODE_6_LEVEL);
574574

575575
free_sub_pt(pgtable->root, pgtable->mode, &freelist);
576+
iommu_put_pages_list(&freelist);
576577

577578
/* Update data structure */
578579
amd_iommu_domain_clr_pt_root(dom);
579580

580581
/* Make changes visible to IOMMUs */
581582
amd_iommu_domain_update(dom);
582-
583-
iommu_put_pages_list(&freelist);
584583
}
585584

586585
static struct io_pgtable *v1_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
587586
{
588587
struct amd_io_pgtable *pgtable = io_pgtable_cfg_to_data(cfg);
589588

589+
pgtable->root = iommu_alloc_page(GFP_KERNEL);
590+
if (!pgtable->root)
591+
return NULL;
592+
pgtable->mode = PAGE_MODE_3_LEVEL;
593+
590594
cfg->pgsize_bitmap = AMD_IOMMU_PGSIZES;
591595
cfg->ias = IOMMU_IN_ADDR_BIT_SIZE;
592596
cfg->oas = IOMMU_OUT_ADDR_BIT_SIZE;

drivers/iommu/amd/iommu.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
#define HT_RANGE_START (0xfd00000000ULL)
5353
#define HT_RANGE_END (0xffffffffffULL)
5454

55-
#define DEFAULT_PGTABLE_LEVEL PAGE_MODE_3_LEVEL
56-
5755
static DEFINE_SPINLOCK(pd_bitmap_lock);
5856

5957
LIST_HEAD(ioapic_map);
@@ -2260,30 +2258,15 @@ void protection_domain_free(struct protection_domain *domain)
22602258
if (domain->iop.pgtbl_cfg.tlb)
22612259
free_io_pgtable_ops(&domain->iop.iop.ops);
22622260

2263-
if (domain->iop.root)
2264-
iommu_free_page(domain->iop.root);
2265-
22662261
if (domain->id)
22672262
domain_id_free(domain->id);
22682263

22692264
kfree(domain);
22702265
}
22712266

2272-
static int protection_domain_init_v1(struct protection_domain *domain, int mode)
2267+
static int protection_domain_init_v1(struct protection_domain *domain)
22732268
{
2274-
u64 *pt_root = NULL;
2275-
2276-
BUG_ON(mode < PAGE_MODE_NONE || mode > PAGE_MODE_6_LEVEL);
2277-
2278-
if (mode != PAGE_MODE_NONE) {
2279-
pt_root = iommu_alloc_page(GFP_KERNEL);
2280-
if (!pt_root)
2281-
return -ENOMEM;
2282-
}
2283-
22842269
domain->pd_mode = PD_MODE_V1;
2285-
amd_iommu_domain_set_pgtable(domain, pt_root, mode);
2286-
22872270
return 0;
22882271
}
22892272

@@ -2336,7 +2319,7 @@ struct protection_domain *protection_domain_alloc(unsigned int type)
23362319

23372320
switch (pgtable) {
23382321
case AMD_IOMMU_V1:
2339-
ret = protection_domain_init_v1(domain, DEFAULT_PGTABLE_LEVEL);
2322+
ret = protection_domain_init_v1(domain);
23402323
break;
23412324
case AMD_IOMMU_V2:
23422325
ret = protection_domain_init_v2(domain);

0 commit comments

Comments
 (0)