Skip to content

Commit e5afdf9

Browse files
aikmpe
authored andcommitted
powerpc/vfio_spapr_tce: Add reference counting to iommu_table
So far iommu_table obejcts were only used in virtual mode and had a single owner. We are going to change this by implementing in-kernel acceleration of DMA mapping requests. The proposed acceleration will handle requests in real mode and KVM will keep references to tables. This adds a kref to iommu_table and defines new helpers to update it. This replaces iommu_free_table() with iommu_tce_table_put() and makes iommu_free_table() static. iommu_tce_table_get() is not used in this patch but it will be in the following patch. Since this touches prototypes, this also removes @node_name parameter as it has never been really useful on powernv and carrying it for the pseries platform code to iommu_free_table() seems to be quite useless as well. This should cause no behavioral change. Signed-off-by: Alexey Kardashevskiy <[email protected]> Reviewed-by: David Gibson <[email protected]> Acked-by: Alex Williamson <[email protected]> Signed-off-by: Michael Ellerman <[email protected]>
1 parent 11edf11 commit e5afdf9

File tree

7 files changed

+37
-17
lines changed

7 files changed

+37
-17
lines changed

arch/powerpc/include/asm/iommu.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ struct iommu_table {
119119
struct list_head it_group_list;/* List of iommu_table_group_link */
120120
unsigned long *it_userspace; /* userspace view of the table */
121121
struct iommu_table_ops *it_ops;
122+
struct kref it_kref;
122123
};
123124

124125
#define IOMMU_TABLE_USERSPACE_ENTRY(tbl, entry) \
@@ -151,8 +152,8 @@ static inline void *get_iommu_table_base(struct device *dev)
151152

152153
extern int dma_iommu_dma_supported(struct device *dev, u64 mask);
153154

154-
/* Frees table for an individual device node */
155-
extern void iommu_free_table(struct iommu_table *tbl, const char *node_name);
155+
extern struct iommu_table *iommu_tce_table_get(struct iommu_table *tbl);
156+
extern int iommu_tce_table_put(struct iommu_table *tbl);
156157

157158
/* Initializes an iommu_table based in values set in the passed-in
158159
* structure

arch/powerpc/kernel/iommu.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -711,13 +711,13 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
711711
return tbl;
712712
}
713713

714-
void iommu_free_table(struct iommu_table *tbl, const char *node_name)
714+
static void iommu_table_free(struct kref *kref)
715715
{
716716
unsigned long bitmap_sz;
717717
unsigned int order;
718+
struct iommu_table *tbl;
718719

719-
if (!tbl)
720-
return;
720+
tbl = container_of(kref, struct iommu_table, it_kref);
721721

722722
if (tbl->it_ops->free)
723723
tbl->it_ops->free(tbl);
@@ -736,7 +736,7 @@ void iommu_free_table(struct iommu_table *tbl, const char *node_name)
736736

737737
/* verify that table contains no entries */
738738
if (!bitmap_empty(tbl->it_map, tbl->it_size))
739-
pr_warn("%s: Unexpected TCEs for %s\n", __func__, node_name);
739+
pr_warn("%s: Unexpected TCEs\n", __func__);
740740

741741
/* calculate bitmap size in bytes */
742742
bitmap_sz = BITS_TO_LONGS(tbl->it_size) * sizeof(unsigned long);
@@ -748,7 +748,24 @@ void iommu_free_table(struct iommu_table *tbl, const char *node_name)
748748
/* free table */
749749
kfree(tbl);
750750
}
751-
EXPORT_SYMBOL_GPL(iommu_free_table);
751+
752+
struct iommu_table *iommu_tce_table_get(struct iommu_table *tbl)
753+
{
754+
if (kref_get_unless_zero(&tbl->it_kref))
755+
return tbl;
756+
757+
return NULL;
758+
}
759+
EXPORT_SYMBOL_GPL(iommu_tce_table_get);
760+
761+
int iommu_tce_table_put(struct iommu_table *tbl)
762+
{
763+
if (WARN_ON(!tbl))
764+
return 0;
765+
766+
return kref_put(&tbl->it_kref, iommu_table_free);
767+
}
768+
EXPORT_SYMBOL_GPL(iommu_tce_table_put);
752769

753770
/* Creates TCEs for a user provided buffer. The user buffer must be
754771
* contiguous real kernel storage (not vmalloc). The address passed here

arch/powerpc/platforms/powernv/pci-ioda.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe
14241424
iommu_group_put(pe->table_group.group);
14251425
BUG_ON(pe->table_group.group);
14261426
}
1427-
iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
1427+
iommu_tce_table_put(tbl);
14281428
}
14291429

14301430
static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
@@ -2225,7 +2225,7 @@ static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
22252225
__free_pages(tce_mem, get_order(tce32_segsz * segs));
22262226
if (tbl) {
22272227
pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
2228-
iommu_free_table(tbl, "pnv");
2228+
iommu_tce_table_put(tbl);
22292229
}
22302230
}
22312231

@@ -2321,7 +2321,7 @@ static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
23212321
bus_offset, page_shift, window_size,
23222322
levels, tbl);
23232323
if (ret) {
2324-
iommu_free_table(tbl, "pnv");
2324+
iommu_tce_table_put(tbl);
23252325
return ret;
23262326
}
23272327

@@ -2365,7 +2365,7 @@ static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
23652365
if (rc) {
23662366
pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
23672367
rc);
2368-
iommu_free_table(tbl, "");
2368+
iommu_tce_table_put(tbl);
23692369
return rc;
23702370
}
23712371

@@ -2453,7 +2453,7 @@ static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
24532453
pnv_pci_ioda2_unset_window(&pe->table_group, 0);
24542454
if (pe->pbus)
24552455
pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
2456-
iommu_free_table(tbl, "pnv");
2456+
iommu_tce_table_put(tbl);
24572457
}
24582458

24592459
static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
@@ -3428,7 +3428,7 @@ static void pnv_pci_ioda1_release_pe_dma(struct pnv_ioda_pe *pe)
34283428
}
34293429

34303430
free_pages(tbl->it_base, get_order(tbl->it_size << 3));
3431-
iommu_free_table(tbl, "pnv");
3431+
iommu_tce_table_put(tbl);
34323432
}
34333433

34343434
static void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)
@@ -3455,7 +3455,7 @@ static void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)
34553455
}
34563456

34573457
pnv_pci_ioda2_table_free_pages(tbl);
3458-
iommu_free_table(tbl, "pnv");
3458+
iommu_tce_table_put(tbl);
34593459
}
34603460

34613461
static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,

arch/powerpc/platforms/powernv/pci.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ struct iommu_table *pnv_pci_table_alloc(int nid)
767767

768768
tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, nid);
769769
INIT_LIST_HEAD_RCU(&tbl->it_group_list);
770+
kref_init(&tbl->it_kref);
770771

771772
return tbl;
772773
}

arch/powerpc/platforms/pseries/iommu.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ static struct iommu_table_group *iommu_pseries_alloc_group(int node)
7474
goto fail_exit;
7575

7676
INIT_LIST_HEAD_RCU(&tbl->it_group_list);
77+
kref_init(&tbl->it_kref);
7778
tgl->table_group = table_group;
7879
list_add_rcu(&tgl->next, &tbl->it_group_list);
7980

@@ -115,7 +116,7 @@ static void iommu_pseries_free_group(struct iommu_table_group *table_group,
115116
BUG_ON(table_group->group);
116117
}
117118
#endif
118-
iommu_free_table(tbl, node_name);
119+
iommu_tce_table_put(tbl);
119120

120121
kfree(table_group);
121122
}

arch/powerpc/platforms/pseries/vio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ static void vio_dev_release(struct device *dev)
13181318
struct iommu_table *tbl = get_iommu_table_base(dev);
13191319

13201320
if (tbl)
1321-
iommu_free_table(tbl, of_node_full_name(dev->of_node));
1321+
iommu_tce_table_put(tbl);
13221322
of_node_put(dev->of_node);
13231323
kfree(to_vio_dev(dev));
13241324
}

drivers/vfio/vfio_iommu_spapr_tce.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ static void tce_iommu_free_table(struct tce_container *container,
680680
unsigned long pages = tbl->it_allocated_size >> PAGE_SHIFT;
681681

682682
tce_iommu_userspace_view_free(tbl, container->mm);
683-
iommu_free_table(tbl, "");
683+
iommu_tce_table_put(tbl);
684684
decrement_locked_vm(container->mm, pages);
685685
}
686686

0 commit comments

Comments
 (0)