iommu/amd: Restructure code for freeing page table
authorSuravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Tue, 15 Dec 2020 07:36:59 +0000 (01:36 -0600)
committerJoerg Roedel <jroedel@suse.de>
Thu, 28 Jan 2021 15:51:17 +0000 (16:51 +0100)
By consolidate logic into v1_free_pgtable helper function,
which is called from IO page table framework.

Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Link: https://lore.kernel.org/r/20201215073705.123786-8-suravee.suthikulpanit@amd.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/iommu/amd/amd_iommu.h
drivers/iommu/amd/io_pgtable.c
drivers/iommu/amd/iommu.c

index 1bad42a3c73ca72cc280dacd916e6cc9a32773bc..91d098003f122325b88c14c4e8d2df9763075561 100644 (file)
@@ -132,7 +132,6 @@ static inline void amd_iommu_apply_ivrs_quirks(void) { }
 #endif
 
 /* TODO: These are temporary and will be removed once fully transition */
-extern void free_pagetable(struct domain_pgtable *pgtable);
 extern int iommu_map_page(struct protection_domain *dom,
                          unsigned long bus_addr,
                          unsigned long phys_addr,
index dbc0ad775d5e84a9765c75622322f9bd300d97b1..a8fea06efb4fc475314f255b53325b7553b35958 100644 (file)
@@ -157,23 +157,6 @@ static struct page *free_sub_pt(unsigned long root, int mode,
        return freelist;
 }
 
-void free_pagetable(struct domain_pgtable *pgtable)
-{
-       struct page *freelist = NULL;
-       unsigned long root;
-
-       if (pgtable->mode == PAGE_MODE_NONE)
-               return;
-
-       BUG_ON(pgtable->mode < PAGE_MODE_NONE ||
-              pgtable->mode > PAGE_MODE_6_LEVEL);
-
-       root = (unsigned long)pgtable->root;
-       freelist = free_sub_pt(root, pgtable->mode, freelist);
-
-       free_page_list(freelist);
-}
-
 void amd_iommu_domain_set_pgtable(struct protection_domain *domain,
                                  u64 *root, int mode)
 {
@@ -522,6 +505,30 @@ unsigned long iommu_unmap_page(struct protection_domain *dom,
  */
 static void v1_free_pgtable(struct io_pgtable *iop)
 {
+       struct amd_io_pgtable *pgtable = container_of(iop, struct amd_io_pgtable, iop);
+       struct protection_domain *dom;
+       struct page *freelist = NULL;
+       unsigned long root;
+
+       if (pgtable->mode == PAGE_MODE_NONE)
+               return;
+
+       dom = container_of(pgtable, struct protection_domain, iop);
+
+       /* Update data structure */
+       amd_iommu_domain_clr_pt_root(dom);
+
+       /* Make changes visible to IOMMUs */
+       amd_iommu_domain_update(dom);
+
+       /* Page-table is not visible to IOMMU anymore, so free it */
+       BUG_ON(pgtable->mode < PAGE_MODE_NONE ||
+              pgtable->mode > PAGE_MODE_6_LEVEL);
+
+       root = (unsigned long)pgtable->root;
+       freelist = free_sub_pt(root, pgtable->mode, freelist);
+
+       free_page_list(freelist);
 }
 
 static struct io_pgtable *v1_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
index eda8e196fb1456ed6991b1df04d18dff5436716d..d02aeb17fd8164cb57c273139cc30ad986b4b73b 100644 (file)
@@ -1902,17 +1902,14 @@ static void cleanup_domain(struct protection_domain *domain)
 
 static void protection_domain_free(struct protection_domain *domain)
 {
-       struct domain_pgtable pgtable;
-
        if (!domain)
                return;
 
        if (domain->id)
                domain_id_free(domain->id);
 
-       amd_iommu_domain_get_pgtable(domain, &pgtable);
-       amd_iommu_domain_clr_pt_root(domain);
-       free_pagetable(&pgtable);
+       if (domain->iop.pgtbl_cfg.tlb)
+               free_io_pgtable_ops(&domain->iop.iop.ops);
 
        kfree(domain);
 }
@@ -2301,22 +2298,12 @@ EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
 void amd_iommu_domain_direct_map(struct iommu_domain *dom)
 {
        struct protection_domain *domain = to_pdomain(dom);
-       struct domain_pgtable pgtable;
        unsigned long flags;
 
        spin_lock_irqsave(&domain->lock, flags);
 
-       /* First save pgtable configuration*/
-       amd_iommu_domain_get_pgtable(domain, &pgtable);
-
-       /* Remove page-table from domain */
-       amd_iommu_domain_clr_pt_root(domain);
-
-       /* Make changes visible to IOMMUs */
-       amd_iommu_domain_update(domain);
-
-       /* Page-table is not visible to IOMMU anymore, so free it */
-       free_pagetable(&pgtable);
+       if (domain->iop.pgtbl_cfg.tlb)
+               free_io_pgtable_ops(&domain->iop.iop.ops);
 
        spin_unlock_irqrestore(&domain->lock, flags);
 }