powerpc/kvm/vfio: Enable in-kernel acceleration
Subsystems (6)
v5 cover powerpc/kvm/vfio: Enable in-kernel acceleration
This is my current queue of patches to add acceleration of TCE updates in KVM.
This is based on the v4.10 release.
Please comment. Thanks.
Changes:
v5:
* replaced "KVM: PPC: Separate TCE validation from update" with
"KVM: PPC: iommu: Unify TCE checking"
* changed already reviewed "powerpc/iommu/vfio_spapr_tce: Cleanup iommu_table disposal"
* reworked "KVM: PPC: VFIO: Add in-kernel acceleration for VFIO"
* more details in individual commit logs
v4:
* addressed comments from v3
* updated subject lines with correct component names
* regrouped the patchset in order:
- powerpc fixes;
- vfio_spapr_tce driver fixes;
- KVM/PPC fixes;
- KVM+PPC+VFIO;
* everything except last 2 patches have "Reviewed-By: David"
v3:
* there was no full repost, only last patch was posted
v2:
* 11/11 reworked to use new notifiers, it is rather RFC as it still has a issue;
* got 09/11, 10/11 to use notifiers in 11/11;
* added rb: David to most of patches and added a comment in 05/11.
Alexey Kardashevskiy (10):
powerpc/mmu: Add real mode support for IOMMU preregistered memory powerpc/powernv/iommu: Add real mode version of
iommu_table_ops::exchange()
powerpc/iommu/vfio_spapr_tce: Cleanup iommu_table disposal powerpc/vfio_spapr_tce: Add reference counting to iommu_table
KVM: PPC: Reserve KVM_CAP_SPAPR_TCE_VFIO capability number
KVM: PPC: Enable IOMMU_API for KVM_BOOK3S_64 permanently
KVM: PPC: Pass kvm* to kvmppc_find_table()
KVM: PPC: Use preregistered memory API to access TCE list
KVM: PPC: iommu: Unify TCE checking
KVM: PPC: VFIO: Add in-kernel acceleration for VFIO
Documentation/virtual/kvm/devices/vfio.txt | 22 +- arch/powerpc/include/asm/iommu.h | 32 ++- arch/powerpc/include/asm/kvm_host.h | 8 + arch/powerpc/include/asm/kvm_ppc.h | 12 +- arch/powerpc/include/asm/mmu_context.h | 4 + include/uapi/linux/kvm.h | 9 + arch/powerpc/kernel/iommu.c | 86 +++++--- arch/powerpc/kvm/book3s_64_vio.c | 314 ++++++++++++++++++++++++++++- arch/powerpc/kvm/book3s_64_vio_hv.c | 254 ++++++++++++++++++----- arch/powerpc/kvm/powerpc.c | 2 + arch/powerpc/mm/mmu_context_iommu.c | 39 ++++ arch/powerpc/platforms/powernv/pci-ioda.c | 46 +++-- arch/powerpc/platforms/powernv/pci.c | 1 + arch/powerpc/platforms/pseries/iommu.c | 3 +- arch/powerpc/platforms/pseries/vio.c | 2 +- drivers/vfio/vfio_iommu_spapr_tce.c | 2 +- virt/kvm/vfio.c | 60 ++++++ arch/powerpc/kvm/Kconfig | 1 + 18 files changed, 787 insertions(+), 110 deletions(-)
Signature · 1 line
2.11.0
PATCH v5 1/10 powerpc/mmu: Add real mode support for IOMMU preregistered memory
This makes mm_iommu_lookup() able to work in realmode by replacing list_for_each_entry_rcu() (which can do debug stuff which can fail in real mode) with list_for_each_entry_lockless().
This adds realmode version of mm_iommu_ua_to_hpa() which adds explicit vmalloc'd-to-linear address conversion. Unlike mm_iommu_ua_to_hpa(), mm_iommu_ua_to_hpa_rm() can fail.
This changes mm_iommu_preregistered() to receive @mm as in real mode @current does not always have a correct pointer.
This adds realmode version of mm_iommu_lookup() which receives @mm (for the same reason as for mm_iommu_preregistered()) and uses lockless version of list_for_each_entry_rcu().
--- arch/powerpc/include/asm/mmu_context.h | 4 ++++ arch/powerpc/mm/mmu_context_iommu.c | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+)
arch/powerpc/include/asm/mmu_context.h +4 -0
@@ -29,10 +29,14 @@ extern void mm_iommu_init(struct mm_struct *mm);extern void mm_iommu_cleanup (struct mm_struct *mm);extern struct mm_iommu_table_group_mem_t *mm_iommu_lookup (struct mm_struct *mm,unsigned long ua,unsigned long size); +extern struct mm_iommu_table_group_mem_t *mm_iommu_lookup_rm ( +struct mm_struct *mm,unsigned long ua,unsigned long size);extern struct mm_iommu_table_group_mem_t *mm_iommu_find (struct mm_struct *mm,unsigned long ua,unsigned long entries);extern long mm_iommu_ua_to_hpa (struct mm_iommu_table_group_mem_t *mem,unsigned long ua,unsigned long *hpa); +extern long mm_iommu_ua_to_hpa_rm (struct mm_iommu_table_group_mem_t *mem, +unsigned long ua,unsigned long *hpa);extern long mm_iommu_mapped_inc (struct mm_iommu_table_group_mem_t *mem);extern void mm_iommu_mapped_dec (struct mm_iommu_table_group_mem_t *mem);#endif
arch/powerpc/mm/mmu_context_iommu.c +39 -0
@@ -314,6 +314,25 @@ struct mm_iommu_table_group_mem_t *mm_iommu_lookup(struct mm_struct *mm, }EXPORT_SYMBOL_GPL (mm_iommu_lookup); +struct mm_iommu_table_group_mem_t *mm_iommu_lookup_rm (struct mm_struct *mm, +unsigned long ua,unsigned long size) +{ +struct mm_iommu_table_group_mem_t *mem, *ret =NULL ; + +list_for_each_entry_lockless (mem, &mm->context.iommu_group_mem_list, + next) { +if ((mem->ua <= ua) && + (ua + size <= mem->ua + + (mem->entries <<PAGE_SHIFT ))) { + ret = mem; +break ; + } + } + +return ret; +} +EXPORT_SYMBOL_GPL (mm_iommu_lookup_rm); +struct mm_iommu_table_group_mem_t *mm_iommu_find (struct mm_struct *mm,unsigned long ua,unsigned long entries) {
@@ -345,6 +364,26 @@ long mm_iommu_ua_to_hpa(struct mm_iommu_table_group_mem_t *mem, }EXPORT_SYMBOL_GPL (mm_iommu_ua_to_hpa); +long mm_iommu_ua_to_hpa_rm (struct mm_iommu_table_group_mem_t *mem, +unsigned long ua,unsigned long *hpa) +{ +const long entry = (ua - mem->ua) >>PAGE_SHIFT ; +void *va = &mem->hpas[entry]; +unsigned long *pa; + +if (entry >= mem->entries) +return -EFAULT ; + + pa = (void *)vmalloc_to_phys (va); +if (!pa) +return -EFAULT ; + + *hpa = *pa | (ua & ~PAGE_MASK ); + +return 0 ; +} +EXPORT_SYMBOL_GPL (mm_iommu_ua_to_hpa_rm); +long mm_iommu_mapped_inc (struct mm_iommu_table_group_mem_t *mem) {if (atomic64_inc_not_zero (&mem->mapped))
Signature · 1 line
2.11.0
PATCH v5 2/10 powerpc/powernv/iommu: Add real mode version of iommu_table_ops::exchange()
In reply to Alexey KardashevskiyIn real mode, TCE tables are invalidated using special cache-inhibited store instructions which are not available in virtual mode
This defines and implements exchange_rm() callback. This does not define set_rm/clear_rm/flush_rm callbacks as there is no user for those - exchange/exchange_rm are only to be used by KVM for VFIO.
The exchange_rm callback is defined for IODA1/IODA2 powernv platforms.
This replaces list_for_each_entry_rcu with its lockless version as from now on pnv_pci_ioda2_tce_invalidate() can be called in the real mode too.
--- arch/powerpc/include/asm/iommu.h | 7 +++++++ arch/powerpc/kernel/iommu.c | 23 +++++++++++++++++++++++ arch/powerpc/platforms/powernv/pci-ioda.c | 26 +++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/iommu.h +7 -0
@@ -64,6 +64,11 @@ struct iommu_table_ops {long index, unsigned long *hpa, enum dma_data_direction *direction); +/* Real mode */ +int (*exchange_rm)(struct iommu_table *tbl, +long index, +unsigned long *hpa, +enum dma_data_direction *direction);#endif void (*clear)(struct iommu_table *tbl,long index,long npages);
@@ -208,6 +213,8 @@ extern void iommu_del_device(struct device *dev);extern int __init tce_iommu_bus_notifier_init (void );extern long iommu_tce_xchg (struct iommu_table *tbl,unsigned long entry,unsigned long *hpa,enum dma_data_direction *direction); +extern long iommu_tce_xchg_rm (struct iommu_table *tbl,unsigned long entry, +unsigned long *hpa,enum dma_data_direction *direction);#else static inline void iommu_register_group(struct iommu_table_group *table_group, int pci_domain_number,
arch/powerpc/kernel/iommu.c +23 -0
@@ -1004,6 +1004,29 @@ long iommu_tce_xchg(struct iommu_table *tbl, unsigned long entry, }EXPORT_SYMBOL_GPL (iommu_tce_xchg); +long iommu_tce_xchg_rm (struct iommu_table *tbl,unsigned long entry, +unsigned long *hpa,enum dma_data_direction *direction) +{ +long ret; + + ret = tbl->it_ops->exchange_rm (tbl, entry, hpa, direction); + +if (!ret && ((*direction ==DMA_FROM_DEVICE ) || + (*direction ==DMA_BIDIRECTIONAL ))) { +struct page *pg =realmode_pfn_to_page (*hpa >>PAGE_SHIFT ); + +if (likely (pg)) { +SetPageDirty (pg); + }else { + tbl->it_ops->exchange_rm (tbl, entry, hpa, direction); + ret = -EFAULT ; + } + } + +return ret; +} +EXPORT_SYMBOL_GPL (iommu_tce_xchg_rm); +int iommu_take_ownership (struct iommu_table *tbl) {unsigned long flags, i, sz = (tbl->it_size +7 ) >>3 ;
arch/powerpc/platforms/powernv/pci-ioda.c +25 -1
@@ -1860,6 +1860,17 @@ static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,return ret; } + +static int pnv_ioda1_tce_xchg_rm (struct iommu_table *tbl,long index, +unsigned long *hpa,enum dma_data_direction *direction) +{ +long ret =pnv_tce_xchg (tbl, index, hpa, direction); + +if (!ret) +pnv_pci_p7ioc_tce_invalidate (tbl, index,1 , true); + +return ret; +}#endif static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
@@ -1874,6 +1885,7 @@ static struct iommu_table_ops pnv_ioda1_iommu_ops = { .set = pnv_ioda1_tce_build, #ifdef CONFIG_IOMMU_API .exchange = pnv_ioda1_tce_xchg, + .exchange_rm = pnv_ioda1_tce_xchg_rm, #endif .clear = pnv_ioda1_tce_free, .get = pnv_tce_get,
@@ -1948,7 +1960,7 @@ static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl, {struct iommu_table_group_link *tgl; -list_for_each_entry_rcu (tgl, &tbl->it_group_list, next) { +list_for_each_entry_lockless (tgl, &tbl->it_group_list, next) {struct pnv_ioda_pe *pe =container_of (tgl->table_group, struct pnv_ioda_pe, table_group);struct pnv_phb *phb = pe->phb;
@@ -2004,6 +2016,17 @@ static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,return ret; } + +static int pnv_ioda2_tce_xchg_rm (struct iommu_table *tbl,long index, +unsigned long *hpa,enum dma_data_direction *direction) +{ +long ret =pnv_tce_xchg (tbl, index, hpa, direction); + +if (!ret) +pnv_pci_ioda2_tce_invalidate (tbl, index,1 , true); + +return ret; +}#endif static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
@@ -2024,6 +2047,7 @@ static struct iommu_table_ops pnv_ioda2_iommu_ops = { .set = pnv_ioda2_tce_build, #ifdef CONFIG_IOMMU_API .exchange = pnv_ioda2_tce_xchg, + .exchange_rm = pnv_ioda2_tce_xchg_rm, #endif .clear = pnv_ioda2_tce_free, .get = pnv_tce_get,
Signature · 1 line
2.11.0
PATCH v5 3/10 powerpc/iommu/vfio_spapr_tce: Cleanup iommu_table disposal
In reply to Alexey KardashevskiyAt the moment iommu_table can be disposed by either calling iommu_table_free() directly or it_ops::free(); the only implementation of free() is in IODA2 - pnv_ioda2_table_free() - and it calls iommu_table_free() anyway.
As we are going to have reference counting on tables, we need an unified way of disposing tables.
This moves it_ops::free() call into iommu_free_table() and makes use of the latter. The free() callback now handles only platform-specific data.
As from now on the iommu_free_table() calls it_ops->free(), we need to have it_ops initialized before calling iommu_free_table() so this moves this initialization in pnv_pci_ioda2_create_table().
This should cause no behavioral change.
---
Changes:
v5:
* moved "tbl->it_ops = &pnv_ioda2_iommu_ops" earlier and updated the commit log
--- arch/powerpc/kernel/iommu.c | 4 ++++ arch/powerpc/platforms/powernv/pci-ioda.c | 10 ++++------ drivers/vfio/vfio_iommu_spapr_tce.c | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-)
arch/powerpc/kernel/iommu.c +4 -0
@@ -719,6 +719,9 @@ void iommu_free_table(struct iommu_table *tbl, const char *node_name)if (!tbl)return ; +if (tbl->it_ops->free) + tbl->it_ops->free (tbl); +if (!tbl->it_map) {kfree (tbl);return ;
@@ -745,6 +748,7 @@ void iommu_free_table(struct iommu_table *tbl, const char *node_name)/* free table */ kfree (tbl); } +EXPORT_SYMBOL_GPL (iommu_free_table);/* Creates TCEs for a user provided buffer. The user buffer must be * contiguous real kernel storage (not vmalloc). The address passed here
arch/powerpc/platforms/powernv/pci-ioda.c +4 -6
@@ -1423,7 +1423,6 @@ static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_peiommu_group_put (pe->table_group.group);BUG_ON (pe->table_group.group); } -pnv_pci_ioda2_table_free_pages (tbl);iommu_free_table (tbl,of_node_full_name (dev->dev.of_node)); }
@@ -2040,7 +2039,6 @@ static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,static void pnv_ioda2_table_free (struct iommu_table *tbl) {pnv_pci_ioda2_table_free_pages (tbl); -iommu_free_table (tbl,"pnv" ); }static struct iommu_table_ops pnv_ioda2_iommu_ops = {
@@ -2317,6 +2315,8 @@ static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,if (!tbl)return -ENOMEM ; + tbl->it_ops = &pnv_ioda2_iommu_ops; + ret =pnv_pci_ioda2_table_alloc_pages (nid, bus_offset, page_shift, window_size, levels, tbl);
@@ -2325,8 +2325,6 @@ static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,return ret; } - tbl->it_ops = &pnv_ioda2_iommu_ops; - *ptbl = tbl;return 0 ;
@@ -2367,7 +2365,7 @@ static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)if (rc) {pe_err (pe,"Failed to configure 32-bit TCE table, err %ld\n" , rc); -pnv_ioda2_table_free (tbl); +iommu_free_table (tbl,"" );return rc; }
@@ -2455,7 +2453,7 @@ static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)pnv_pci_ioda2_unset_window (&pe->table_group,0 );if (pe->pbus)pnv_ioda_setup_bus_dma (pe, pe->pbus, false); -pnv_ioda2_table_free (tbl); +iommu_free_table (tbl,"pnv" ); }static void pnv_ioda2_release_ownership (struct iommu_table_group *table_group)
drivers/vfio/vfio_iommu_spapr_tce.c +1 -1
@@ -677,7 +677,7 @@ static void tce_iommu_free_table(struct tce_container *container,unsigned long pages = tbl->it_allocated_size >>PAGE_SHIFT ;tce_iommu_userspace_view_free (tbl, container->mm); - tbl->it_ops->free (tbl); +iommu_free_table (tbl,"" );decrement_locked_vm (container->mm, pages); }
Signature · 1 line
2.11.0
Re: [PATCH kernel v5 03/10] powerpc/iommu/vfio_spapr_tce: Cleanup iommu_table disposal
Quoted 12 lines · Alexey Kardashevskiy
At the moment
iommu_tablecan be disposed by either callingiommu_table_free()directly orit_ops::free(); the only implementation offree()is in IODA2 -pnv_ioda2_table_free()- and it callsiommu_table_free()anyway.As we are going to have reference counting on tables, we need an unified way of disposing tables.
This moves
it_ops::free()call intoiommu_free_table()and makes use of the latter. Thefree()callback now handles only platform-specific data.
Quoted 7 lines
As from now on the
iommu_free_table()callsit_ops->free(), we need to haveit_opsinitialized before callingiommu_free_table()so this moves this initialization inpnv_pci_ioda2_create_table().This should cause no behavioral change.
Signed-off-by Alexey Kardashevskiy <[email protected]>
Quoted 102 lines
---
Changes:
v5:
* moved "tbl->it_ops=&pnv_ioda2_iommu_ops" earlier and updated the commit log--- arch/powerpc/kernel/iommu.c | 4 ++++ arch/powerpc/platforms/powernv/pci-ioda.c | 10 ++++------ drivers/vfio/vfio_iommu_spapr_tce.c | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-)arch/powerpc/kernel/iommu.c +4
-0@@ -719,6 +719,9 @@ void iommu_free_table(struct iommu_table *tbl, const char *node_name)if (!tbl)return ; +if (tbl->it_ops->free) + tbl->it_ops->free (tbl); +if (!tbl->it_map) {kfree (tbl);return ;@@ -745,6 +748,7 @@ void iommu_free_table(struct iommu_table *tbl, const char *node_name)/* free table */ kfree (tbl); } +EXPORT_SYMBOL_GPL (iommu_free_table);/* Creates TCEs for a user provided buffer. The user buffer must be * contiguous real kernel storage (not vmalloc). The address passed here arch/powerpc/platforms/powernv/pci-ioda.c +4
-6@@ -1423,7 +1423,6 @@ static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_peiommu_group_put (pe->table_group.group);BUG_ON (pe->table_group.group); } -pnv_pci_ioda2_table_free_pages (tbl);iommu_free_table (tbl,of_node_full_name (dev->dev.of_node)); }@@ -2040,7 +2039,6 @@ static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,static void pnv_ioda2_table_free (struct iommu_table *tbl) {pnv_pci_ioda2_table_free_pages (tbl); -iommu_free_table (tbl,"pnv" ); }static struct iommu_table_ops pnv_ioda2_iommu_ops = {@@ -2317,6 +2315,8 @@ static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,if (!tbl)return -ENOMEM ; + tbl->it_ops = &pnv_ioda2_iommu_ops; + ret =pnv_pci_ioda2_table_alloc_pages (nid, bus_offset, page_shift, window_size, levels, tbl);@@ -2325,8 +2325,6 @@ static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,return ret; } - tbl->it_ops = &pnv_ioda2_iommu_ops; - *ptbl = tbl;return 0 ;@@ -2367,7 +2365,7 @@ static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)if (rc) {pe_err (pe,"Failed to configure 32-bit TCE table, err %ld\n" , rc); -pnv_ioda2_table_free (tbl); +iommu_free_table (tbl,"" );return rc; }@@ -2455,7 +2453,7 @@ static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)pnv_pci_ioda2_unset_window (&pe->table_group,0 );if (pe->pbus)pnv_ioda_setup_bus_dma (pe, pe->pbus, false); -pnv_ioda2_table_free (tbl); +iommu_free_table (tbl,"pnv" ); }static void pnv_ioda2_release_ownership (struct iommu_table_group *table_group)drivers/vfio/vfio_iommu_spapr_tce.c +1
-1@@ -677,7 +677,7 @@ static void tce_iommu_free_table(struct tce_container *container,unsigned long pages = tbl->it_allocated_size >>PAGE_SHIFT ;tce_iommu_userspace_view_free (tbl, container->mm); - tbl->it_ops->free (tbl); +iommu_free_table (tbl,"" );decrement_locked_vm (container->mm, pages); }
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v5 4/10 powerpc/vfio_spapr_tce: Add reference counting to iommu_table
In reply to Alexey KardashevskiySo 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_table_put() and makes iommu_free_table() static. iommu_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.
--- arch/powerpc/include/asm/iommu.h | 5 +++-- arch/powerpc/kernel/iommu.c | 24 +++++++++++++++++++----- arch/powerpc/platforms/powernv/pci-ioda.c | 14 +++++++------- arch/powerpc/platforms/powernv/pci.c | 1 + arch/powerpc/platforms/pseries/iommu.c | 3 ++- arch/powerpc/platforms/pseries/vio.c | 2 +- drivers/vfio/vfio_iommu_spapr_tce.c | 2 +- 7 files changed, 34 insertions(+), 17 deletions(-)
arch/powerpc/include/asm/iommu.h +3 -2
@@ -119,6 +119,7 @@ struct iommu_table {struct list_head it_group_list;/* List of iommu_table_group_link */ unsigned long *it_userspace;/* userspace view of the table */ struct iommu_table_ops *it_ops; +struct kref it_kref; };#define IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry) \
@@ -151,8 +152,8 @@ static inline void *get_iommu_table_base(struct device *dev)extern int dma_iommu_dma_supported (struct device *dev,u64 mask); -/* Frees table for an individual device node */ -extern void iommu_free_table (struct iommu_table *tbl,const char *node_name); +extern void iommu_table_get (struct iommu_table *tbl); +extern void iommu_table_put (struct iommu_table *tbl);/* Initializes an iommu_table based in values set in the passed-in * structure
arch/powerpc/kernel/iommu.c +19 -5
@@ -711,13 +711,13 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)return tbl; } -void iommu_free_table (struct iommu_table *tbl,const char *node_name) +static void iommu_table_free (struct kref *kref) {unsigned long bitmap_sz;unsigned int order; +struct iommu_table *tbl; -if (!tbl) -return ; + tbl = container_of(kref, struct iommu_table, it_kref);if (tbl->it_ops->free) tbl->it_ops->free (tbl);
@@ -736,7 +736,7 @@ void iommu_free_table(struct iommu_table *tbl, const char *node_name)/* verify that table contains no entries */ if (!bitmap_empty (tbl->it_map, tbl->it_size)) -pr_warn ("%s: Unexpected TCEs for %s\n" , __func__, node_name); +pr_warn ("%s: Unexpected TCEs\n" , __func__);/* calculate bitmap size in bytes */ bitmap_sz =BITS_TO_LONGS (tbl->it_size) *sizeof (unsigned long );
@@ -748,7 +748,21 @@ void iommu_free_table(struct iommu_table *tbl, const char *node_name)/* free table */ kfree (tbl); } -EXPORT_SYMBOL_GPL (iommu_free_table); + +void iommu_table_get (struct iommu_table *tbl) +{ +kref_get (&tbl->it_kref); +} +EXPORT_SYMBOL_GPL (iommu_table_get); + +void iommu_table_put (struct iommu_table *tbl) +{ +if (!tbl) +return ; + +kref_put (&tbl->it_kref, iommu_table_free); +} +EXPORT_SYMBOL_GPL (iommu_table_put);/* Creates TCEs for a user provided buffer. The user buffer must be * contiguous real kernel storage (not vmalloc). The address passed here
arch/powerpc/platforms/powernv/pci-ioda.c +7 -7
@@ -1423,7 +1423,7 @@ static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_peiommu_group_put (pe->table_group.group);BUG_ON (pe->table_group.group); } -iommu_free_table (tbl,of_node_full_name (dev->dev.of_node)); +iommu_table_put (tbl); }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,__free_pages (tce_mem,get_order (tce32_segsz * segs));if (tbl) {pnv_pci_unlink_table_and_group (tbl, &pe->table_group); -iommu_free_table (tbl,"pnv" ); +iommu_table_put (tbl); } }
@@ -2321,7 +2321,7 @@ static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group, bus_offset, page_shift, window_size, levels, tbl);if (ret) { -iommu_free_table (tbl,"pnv" ); +iommu_table_put (tbl);return ret; }
@@ -2365,7 +2365,7 @@ static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)if (rc) {pe_err (pe,"Failed to configure 32-bit TCE table, err %ld\n" , rc); -iommu_free_table (tbl,"" ); +iommu_table_put (tbl);return rc; }
@@ -2453,7 +2453,7 @@ static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)pnv_pci_ioda2_unset_window (&pe->table_group,0 );if (pe->pbus)pnv_ioda_setup_bus_dma (pe, pe->pbus, false); -iommu_free_table (tbl,"pnv" ); +iommu_table_put (tbl); }static void pnv_ioda2_release_ownership (struct iommu_table_group *table_group)
@@ -3426,7 +3426,7 @@ static void pnv_pci_ioda1_release_pe_dma(struct pnv_ioda_pe *pe) }free_pages (tbl->it_base,get_order (tbl->it_size <<3 )); -iommu_free_table (tbl,"pnv" ); +iommu_table_put (tbl); }static void pnv_pci_ioda2_release_pe_dma (struct pnv_ioda_pe *pe)
@@ -3453,7 +3453,7 @@ static void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe) }pnv_pci_ioda2_table_free_pages (tbl); -iommu_free_table (tbl,"pnv" ); +iommu_table_put (tbl); } static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,
arch/powerpc/platforms/powernv/pci.c +1 -0
@@ -767,6 +767,7 @@ struct iommu_table *pnv_pci_table_alloc(int nid) tbl =kzalloc_node (sizeof (struct iommu_table ),GFP_KERNEL , nid);INIT_LIST_HEAD_RCU (&tbl->it_group_list); +kref_init (&tbl->it_kref);return tbl; }
arch/powerpc/platforms/pseries/iommu.c +2 -1
@@ -74,6 +74,7 @@ static struct iommu_table_group *iommu_pseries_alloc_group(int node) goto fail_exit;INIT_LIST_HEAD_RCU (&tbl->it_group_list); +kref_init (&tbl->it_kref); tgl->table_group = table_group;list_add_rcu (&tgl->next, &tbl->it_group_list);
@@ -115,7 +116,7 @@ static void iommu_pseries_free_group(struct iommu_table_group *table_group, BUG_ON(table_group->group); }#endif -iommu_free_table (tbl, node_name); +iommu_table_put (tbl);kfree (table_group); }
arch/powerpc/platforms/pseries/vio.c +1 -1
@@ -1318,7 +1318,7 @@ static void vio_dev_release(struct device *dev)struct iommu_table *tbl =get_iommu_table_base (dev);if (tbl) -iommu_free_table (tbl,of_node_full_name (dev->of_node)); +iommu_table_put (tbl);of_node_put (dev->of_node);kfree (to_vio_dev (dev)); }
drivers/vfio/vfio_iommu_spapr_tce.c +1 -1
@@ -677,7 +677,7 @@ static void tce_iommu_free_table(struct tce_container *container,unsigned long pages = tbl->it_allocated_size >>PAGE_SHIFT ;tce_iommu_userspace_view_free (tbl, container->mm); -iommu_free_table (tbl,"" ); +iommu_table_put (tbl);decrement_locked_vm (container->mm, pages); }
Signature · 1 line
2.11.0
PATCH v5 5/10 KVM: PPC: Reserve KVM_CAP_SPAPR_TCE_VFIO capability number
In reply to Alexey KardashevskiyThis adds a capability number for in-kernel support for VFIO on SPAPR platform.
The capability will tell the user space whether in-kernel handlers of H_PUT_TCE can handle VFIO-targeted requests or not. If not, the user space must not attempt allocating a TCE table in the host kernel via the KVM_CREATE_SPAPR_TCE KVM ioctl because in that case TCE requests will not be passed to the user space which is desired action in the situation like that.
--- include/uapi/linux/kvm.h | 1 + 1 file changed, 1 insertion(+)
include/uapi/linux/kvm.h +1 -0
@@ -871,6 +871,7 @@ struct kvm_ppc_smmu_info {#define KVM_CAP_S390_USER_INSTR0 130#define KVM_CAP_MSI_DEVID 131#define KVM_CAP_PPC_HTM 132 +#define KVM_CAP_SPAPR_TCE_VFIO 133#ifdef KVM_CAP_IRQ_ROUTING
Signature · 1 line
2.11.0
PATCH v5 6/10 KVM: PPC: Enable IOMMU_API for KVM_BOOK3S_64 permanently
In reply to Alexey KardashevskiyIt does not make much sense to have KVM in book3s-64 and not to have IOMMU bits for PCI pass through support as it costs little and allows VFIO to function on book3s KVM.
Having IOMMU_API always enabled makes it unnecessary to have a lot of "#ifdef IOMMU_API" in arch/powerpc/kvm/book3s_64_vio*. With those ifdef's we could have only user space emulated devices accelerated (but not VFIO) which do not seem to be very useful.
--- arch/powerpc/kvm/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/powerpc/kvm/Kconfig +1 -0
@@ -67,6 +67,7 @@ config KVM_BOOK3S_64 select KVM_BOOK3S_64_HANDLER select KVM select KVM_BOOK3S_PR_POSSIBLE if !KVM_BOOK3S_HV_POSSIBLE + select SPAPR_TCE_IOMMU if IOMMU_SUPPORT ---help--- Support running unmodified book3s_64 and book3s_32 guest kernels in virtual machines on book3s_64 host processors.
Signature · 1 line
2.11.0
PATCH v5 7/10 KVM: PPC: Pass kvm* to kvmppc_find_table()
In reply to Alexey KardashevskiyThe guest view TCE tables are per KVM anyway (not per VCPU) so pass kvm* there. This will be used in the following patches where we will be attaching VFIO containers to LIOBNs via ioctl() to KVM (rather than to VCPU).
--- arch/powerpc/include/asm/kvm_ppc.h | 2 +- arch/powerpc/kvm/book3s_64_vio.c | 7 ++++--- arch/powerpc/kvm/book3s_64_vio_hv.c | 13 +++++++------ 3 files changed, 12 insertions(+), 10 deletions(-)
arch/powerpc/include/asm/kvm_ppc.h +1 -1
@@ -167,7 +167,7 @@ extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);extern long kvm_vm_ioctl_create_spapr_tce (struct kvm *kvm,struct kvm_create_spapr_tce_64 *args);extern struct kvmppc_spapr_tce_table *kvmppc_find_table ( -struct kvm_vcpu *vcpu,unsigned long liobn); +struct kvm *kvm,unsigned long liobn);extern long kvmppc_ioba_validate (struct kvmppc_spapr_tce_table *stt,unsigned long ioba,unsigned long npages); extern long kvmppc_tce_validate(struct kvmppc_spapr_tce_table *tt,
arch/powerpc/kvm/book3s_64_vio.c +4 -3
@@ -212,12 +212,13 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,long kvmppc_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) { -struct kvmppc_spapr_tce_table *stt =kvmppc_find_table (vcpu, liobn); +struct kvmppc_spapr_tce_table *stt;long ret;/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ + stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)return H_TOO_HARD ;
@@ -245,7 +246,7 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,u64 __user *tces;u64 tce; - stt =kvmppc_find_table (vcpu, liobn); + stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)return H_TOO_HARD ;
@@ -299,7 +300,7 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,struct kvmppc_spapr_tce_table *stt;long i, ret; - stt =kvmppc_find_table (vcpu, liobn); + stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)return H_TOO_HARD ;
arch/powerpc/kvm/book3s_64_vio_hv.c +7 -6
@@ -48,10 +48,9 @@* WARNING: This will be called in real or virtual mode on HV KVM and virtual * mode on PR KVM */ -struct kvmppc_spapr_tce_table *kvmppc_find_table (struct kvm_vcpu *vcpu, +struct kvmppc_spapr_tce_table *kvmppc_find_table (struct kvm *kvm,unsigned long liobn) { -struct kvm *kvm = vcpu->kvm;struct kvmppc_spapr_tce_table *stt;list_for_each_entry_lockless (stt, &kvm->arch.spapr_tce_tables, list)
@@ -182,12 +181,13 @@ EXPORT_SYMBOL_GPL(kvmppc_gpa_to_ua);long kvmppc_rm_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) { -struct kvmppc_spapr_tce_table *stt =kvmppc_find_table (vcpu, liobn); +struct kvmppc_spapr_tce_table *stt;long ret;/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ + stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)return H_TOO_HARD ;
@@ -240,7 +240,7 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,unsigned long tces, entry, ua =0 ;unsigned long *rmap =NULL ; - stt =kvmppc_find_table (vcpu, liobn); + stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)return H_TOO_HARD ;
@@ -301,7 +301,7 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,struct kvmppc_spapr_tce_table *stt;long i, ret; - stt =kvmppc_find_table (vcpu, liobn); + stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)return H_TOO_HARD ;
@@ -322,12 +322,13 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,long kvmppc_h_get_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba) { -struct kvmppc_spapr_tce_table *stt =kvmppc_find_table (vcpu, liobn); +struct kvmppc_spapr_tce_table *stt;long ret;unsigned long idx;struct page *page;u64 *tbl; + stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)return H_TOO_HARD ;
Signature · 1 line
2.11.0
PATCH v5 8/10 KVM: PPC: Use preregistered memory API to access TCE list
In reply to Alexey KardashevskiyVFIO on sPAPR already implements guest memory pre-registration when the entire guest RAM gets pinned. This can be used to translate the physical address of a guest page containing the TCE list from H_PUT_TCE_INDIRECT.
This makes use of the pre-registrered memory API to access TCE list pages in order to avoid unnecessary locking on the KVM memory reverse map as we know that all of guest memory is pinned and we have a flat array mapping GPA to HPA which makes it simpler and quicker to index into that array (even with looking up the kernel page tables in vmalloc_to_phys) than it is to find the memslot, lock the rmap entry, look up the user page tables, and unlock the rmap entry. Note that the rmap pointer is initialized to NULL where declared (not in this patch).
If a requested chunk of memory has not been preregistered, this will fall back to non-preregistered case and lock rmap.
---
Changes:
v4:
* removed oneline inlines
* now falls back to locking rmap if TCE list is not in preregistered memory
v2:
* updated the commit log with David's comment
--- arch/powerpc/kvm/book3s_64_vio_hv.c | 58 +++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 16 deletions(-)
arch/powerpc/kvm/book3s_64_vio_hv.c +42 -16
@@ -239,6 +239,7 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,long i, ret =H_SUCCESS ;unsigned long tces, entry, ua =0 ;unsigned long *rmap =NULL ; +bool prereg = false; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)
@@ -259,23 +260,47 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,if (ret !=H_SUCCESS )return ret; -if (kvmppc_gpa_to_ua (vcpu->kvm, tce_list, &ua, &rmap)) -return H_TOO_HARD ; +if (mm_iommu_preregistered (vcpu->kvm->mm)) { +/* +* We get here if guest memory was pre-registered which +* is normally VFIO case and gpa->hpa translation does not +* depend on hpt. +*/ +struct mm_iommu_table_group_mem_t *mem; - rmap = (void *)vmalloc_to_phys (rmap); +if (kvmppc_gpa_to_ua (vcpu->kvm, tce_list, &ua,NULL )) +return H_TOO_HARD ; -/* -* Synchronize with the MMU notifier callbacks in -* book3s_64_mmu_hv.c (kvm_unmap_hva_hv etc.). -* While we have the rmap lock, code running on other CPUs -* cannot finish unmapping the host real page that backs -* this guest real page, so we are OK to access the host -* real page. -*/ -lock_rmap (rmap); -if (kvmppc_rm_ua_to_hpa (vcpu, ua, &tces)) { - ret =H_TOO_HARD ; - goto unlock_exit; + mem =mm_iommu_lookup_rm (vcpu->kvm->mm, ua,IOMMU_PAGE_SIZE_4K ); +if (mem) + prereg =mm_iommu_ua_to_hpa_rm (mem, ua, &tces) ==0 ; + } + +if (!prereg) { +/* +* This is usually a case of a guest with emulated devices only +* when TCE list is not in preregistered memory. +* We do not require memory to be preregistered in this case +* so lock rmap and do __find_linux_pte_or_hugepte(). +*/ +if (kvmppc_gpa_to_ua (vcpu->kvm, tce_list, &ua, &rmap)) +return H_TOO_HARD ; + + rmap = (void *)vmalloc_to_phys (rmap); + +/* +* Synchronize with the MMU notifier callbacks in +* book3s_64_mmu_hv.c (kvm_unmap_hva_hv etc.). +* While we have the rmap lock, code running on other CPUs +* cannot finish unmapping the host real page that backs +* this guest real page, so we are OK to access the host +* real page. +*/ +lock_rmap (rmap); +if (kvmppc_rm_ua_to_hpa (vcpu, ua, &tces)) { + ret =H_TOO_HARD ; + goto unlock_exit; + } }for (i =0 ; i < npages; ++i) {
@@ -289,7 +314,8 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, } unlock_exit: -unlock_rmap (rmap); +if (rmap) +unlock_rmap (rmap);return ret; }
Signature · 1 line
2.11.0
PATCH v5 9/10 KVM: PPC: iommu: Unify TCE checking
In reply to Alexey Kardashevskiy--- arch/powerpc/include/asm/iommu.h | 20 +++++++++++++++----- arch/powerpc/include/asm/kvm_ppc.h | 6 ++++-- arch/powerpc/kernel/iommu.c | 37 +++++++++++++------------------------ arch/powerpc/kvm/book3s_64_vio_hv.c | 31 +++++++------------------------ 4 files changed, 39 insertions(+), 55 deletions(-)
arch/powerpc/include/asm/iommu.h +15 -5
@@ -296,11 +296,21 @@ static inline void iommu_restore(void)#endif /* The API to support IOMMU operations for VFIO */ -extern int iommu_tce_clear_param_check (struct iommu_table *tbl, -unsigned long ioba,unsigned long tce_value, -unsigned long npages); -extern int iommu_tce_put_param_check (struct iommu_table *tbl, -unsigned long ioba,unsigned long tce); +extern int iommu_tce_check_ioba (unsigned long page_shift, +unsigned long offset,unsigned long size, +unsigned long ioba,unsigned long npages); +extern int iommu_tce_check_tce (unsigned long page_shift, +unsigned long tce); + +#define iommu_tce_clear_param_check (tbl, ioba, tce_value, npages) \ + (iommu_tce_check_ioba((tbl)->it_page_shift, \ + (tbl)->it_offset, (tbl)->it_size, \ + (ioba), (npages)) || (tce_value)) +#define iommu_tce_put_param_check (tbl, ioba, tce) \ + (iommu_tce_check_ioba((tbl)->it_page_shift, \ + (tbl)->it_offset, (tbl)->it_size, \ + (ioba), 1) || \ + iommu_tce_check_tce((tbl)->it_page_shift, (tce)))extern void iommu_flush_tce (struct iommu_table *tbl);extern int iommu_take_ownership (struct iommu_table *tbl);
arch/powerpc/include/asm/kvm_ppc.h +4 -2
@@ -168,8 +168,10 @@ extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, struct kvm_create_spapr_tce_64 *args);extern struct kvmppc_spapr_tce_table *kvmppc_find_table (struct kvm *kvm,unsigned long liobn); -extern long kvmppc_ioba_validate (struct kvmppc_spapr_tce_table *stt, -unsigned long ioba,unsigned long npages); +#define kvmppc_ioba_validate (stt, ioba, npages) \ + (iommu_tce_check_ioba((stt)->page_shift, (stt)->offset, \ + (stt)->size, (ioba), (npages)) ? \ + H_PARAMETER : H_SUCCESS)extern long kvmppc_tce_validate (struct kvmppc_spapr_tce_table *tt,unsigned long tce); extern long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,
arch/powerpc/kernel/iommu.c +13 -24
@@ -960,47 +960,36 @@ void iommu_flush_tce(struct iommu_table *tbl) }EXPORT_SYMBOL_GPL (iommu_flush_tce); -int iommu_tce_clear_param_check (struct iommu_table *tbl, -unsigned long ioba,unsigned long tce_value, -unsigned long npages) +int iommu_tce_check_ioba (unsigned long page_shift, +unsigned long offset,unsigned long size, +unsigned long ioba,unsigned long npages) { -/* tbl->it_ops->clear() does not support any value but 0 */ -if (tce_value) -return -EINVAL ; +unsigned long mask = (1 << page_shift) -1 ; -if (ioba & ~IOMMU_PAGE_MASK (tbl)) +if (ioba & mask)return -EINVAL ; - ioba >>= tbl->it_page_shift; -if (ioba < tbl->it_offset) + ioba >>= page_shift; +if (ioba < offset)return -EINVAL ; -if ((ioba + npages) > (tbl->it_offset + tbl->it_size)) +if ((ioba +1 ) > (offset + size))return -EINVAL ;return 0 ; } -EXPORT_SYMBOL_GPL (iommu_tce_clear_param_check); +EXPORT_SYMBOL_GPL (iommu_tce_check_ioba); -int iommu_tce_put_param_check (struct iommu_table *tbl, -unsigned long ioba,unsigned long tce) +int iommu_tce_check_tce (unsigned long page_shift,unsigned long tce) { -if (tce & ~IOMMU_PAGE_MASK (tbl)) -return -EINVAL ; - -if (ioba & ~IOMMU_PAGE_MASK (tbl)) -return -EINVAL ; - - ioba >>= tbl->it_page_shift; -if (ioba < tbl->it_offset) -return -EINVAL ; +unsigned long mask = (1 << page_shift) -1 ; -if ((ioba +1 ) > (tbl->it_offset + tbl->it_size)) +if (tce & mask)return -EINVAL ;return 0 ; } -EXPORT_SYMBOL_GPL (iommu_tce_put_param_check); +EXPORT_SYMBOL_GPL (iommu_tce_check_tce);long iommu_tce_xchg (struct iommu_table *tbl,unsigned long entry,unsigned long *hpa,enum dma_data_direction *direction)
arch/powerpc/kvm/book3s_64_vio_hv.c +7 -24
@@ -62,27 +62,6 @@ struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm *kvm,EXPORT_SYMBOL_GPL (kvmppc_find_table);/* -* Validates IO address. -* -* WARNING: This will be called in real-mode on HV KVM and virtual -* mode on PR KVM -*/ -long kvmppc_ioba_validate (struct kvmppc_spapr_tce_table *stt, -unsigned long ioba,unsigned long npages) -{ -unsigned long mask = (1ULL << stt->page_shift) -1 ; -unsigned long idx = ioba >> stt->page_shift; - -if ((ioba & mask) || (idx < stt->offset) || - (idx - stt->offset + npages > stt->size) || - (idx + npages < idx)) -return H_PARAMETER ; - -return H_SUCCESS ; -} -EXPORT_SYMBOL_GPL (kvmppc_ioba_validate); - -/* * Validates TCE address. * At the moment flags and page mask are validated. * As the host kernel does not access those addresses (just puts them
@@ -95,10 +74,14 @@ EXPORT_SYMBOL_GPL(kvmppc_ioba_validate);*/ long kvmppc_tce_validate (struct kvmppc_spapr_tce_table *stt,unsigned long tce) { -unsigned long page_mask = ~((1ULL << stt->page_shift) -1 ); -unsigned long mask = ~(page_mask |TCE_PCI_WRITE |TCE_PCI_READ ); +unsigned long gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); +enum dma_data_direction dir =iommu_tce_direction (tce); -if (tce & mask) +/* Allow userspace to poison TCE table */ +if (dir ==DMA_NONE ) +return H_SUCCESS ; + +if (iommu_tce_check_tce (stt->page_shift, gpa))return H_PARAMETER ;return H_SUCCESS ;
Signature · 1 line
2.11.0
Re: [PATCH kernel v5 09/10] KVM: PPC: iommu: Unify TCE checking
Quoted 1 line · Alexey Kardashevskiy
Signed-off-by Alexey Kardashevskiy <[email protected]>
I like this, except for one detail
Quoted 102 lines
--- arch/powerpc/include/asm/iommu.h | 20 +++++++++++++++----- arch/powerpc/include/asm/kvm_ppc.h | 6 ++++-- arch/powerpc/kernel/iommu.c | 37 +++++++++++++------------------------ arch/powerpc/kvm/book3s_64_vio_hv.c | 31 +++++++------------------------ 4 files changed, 39 insertions(+), 55 deletions(-)arch/powerpc/include/asm/iommu.h +15
-5@@ -296,11 +296,21 @@ static inline void iommu_restore(void)#endif /* The API to support IOMMU operations for VFIO */ -extern int iommu_tce_clear_param_check (struct iommu_table *tbl, -unsigned long ioba,unsigned long tce_value, -unsigned long npages); -extern int iommu_tce_put_param_check (struct iommu_table *tbl, -unsigned long ioba,unsigned long tce); +extern int iommu_tce_check_ioba (unsigned long page_shift, +unsigned long offset,unsigned long size, +unsigned long ioba,unsigned long npages); +extern int iommu_tce_check_tce (unsigned long page_shift, +unsigned long tce); + +#define iommu_tce_clear_param_check (tbl, ioba, tce_value, npages) \ + (iommu_tce_check_ioba((tbl)->it_page_shift, \ + (tbl)->it_offset, (tbl)->it_size, \ + (ioba), (npages)) || (tce_value)) +#define iommu_tce_put_param_check (tbl, ioba, tce) \ + (iommu_tce_check_ioba((tbl)->it_page_shift, \ + (tbl)->it_offset, (tbl)->it_size, \ + (ioba), 1) || \ + iommu_tce_check_tce((tbl)->it_page_shift, (tce)))extern void iommu_flush_tce (struct iommu_table *tbl);extern int iommu_take_ownership (struct iommu_table *tbl);arch/powerpc/include/asm/kvm_ppc.h +4
-2@@ -168,8 +168,10 @@ extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, struct kvm_create_spapr_tce_64 *args);extern struct kvmppc_spapr_tce_table *kvmppc_find_table (struct kvm *kvm,unsigned long liobn); -extern long kvmppc_ioba_validate (struct kvmppc_spapr_tce_table *stt, -unsigned long ioba,unsigned long npages); +#define kvmppc_ioba_validate (stt, ioba, npages) \ + (iommu_tce_check_ioba((stt)->page_shift, (stt)->offset, \ + (stt)->size, (ioba), (npages)) ? \ + H_PARAMETER : H_SUCCESS)extern long kvmppc_tce_validate (struct kvmppc_spapr_tce_table *tt,unsigned long tce); extern long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,arch/powerpc/kernel/iommu.c +10
-17@@ -960,47 +960,36 @@ void iommu_flush_tce(struct iommu_table *tbl) }EXPORT_SYMBOL_GPL (iommu_flush_tce); -int iommu_tce_clear_param_check (struct iommu_table *tbl, -unsigned long ioba,unsigned long tce_value, -unsigned long npages) +int iommu_tce_check_ioba (unsigned long page_shift, +unsigned long offset,unsigned long size, +unsigned long ioba,unsigned long npages) { -/* tbl->it_ops->clear() does not support any value but 0 */ -if (tce_value) -return -EINVAL ; +unsigned long mask = (1 << page_shift) -1 ; -if (ioba & ~IOMMU_PAGE_MASK (tbl)) +if (ioba & mask)return -EINVAL ; - ioba >>= tbl->it_page_shift; -if (ioba < tbl->it_offset) + ioba >>= page_shift; +if (ioba < offset)return -EINVAL ; -if ((ioba + npages) > (tbl->it_offset + tbl->it_size)) +if ((ioba +1 ) > (offset + size))return -EINVAL ;return 0 ; } -EXPORT_SYMBOL_GPL (iommu_tce_clear_param_check); +EXPORT_SYMBOL_GPL (iommu_tce_check_ioba); -int iommu_tce_put_param_check (struct iommu_table *tbl, -unsigned long ioba,unsigned long tce) +int iommu_tce_check_tce (unsigned long page_shift,unsigned long tce) { -if (tce & ~IOMMU_PAGE_MASK (tbl)) -return -EINVAL ; - -if (ioba & ~IOMMU_PAGE_MASK (tbl))
arch/powerpc/kernel/iommu.c void iommu_flush_tce(struct iommu_table *tbl) · line 988
Quoted 10 lines
arch/powerpc/kernel/iommu.c +2
-6-return -EINVAL ; - - ioba >>= tbl->it_page_shift; -if (ioba < tbl->it_offset) -return -EINVAL ; +unsigned long mask = (1 << page_shift) -1 ; -if ((ioba +1 ) > (tbl->it_offset + tbl->it_size)) +if (tce & mask)return -EINVAL ;
This doesn't seem to be quite correctly named, since it doesn't check a TCE (including permission bits), but rather a GPA.
Quoted 57 lines
arch/powerpc/kernel/iommu.c +1
-1return 0 ; } -EXPORT_SYMBOL_GPL (iommu_tce_put_param_check); +EXPORT_SYMBOL_GPL (iommu_tce_check_tce);long iommu_tce_xchg (struct iommu_table *tbl,unsigned long entry,unsigned long *hpa,enum dma_data_direction *direction)arch/powerpc/kvm/book3s_64_vio_hv.c +7
-24@@ -62,27 +62,6 @@ struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm *kvm,EXPORT_SYMBOL_GPL (kvmppc_find_table);/* -* Validates IO address. -* -* WARNING: This will be called in real-mode on HV KVM and virtual -* mode on PR KVM -*/ -long kvmppc_ioba_validate (struct kvmppc_spapr_tce_table *stt, -unsigned long ioba,unsigned long npages) -{ -unsigned long mask = (1ULL << stt->page_shift) -1 ; -unsigned long idx = ioba >> stt->page_shift; - -if ((ioba & mask) || (idx < stt->offset) || - (idx - stt->offset + npages > stt->size) || - (idx + npages < idx)) -return H_PARAMETER ; - -return H_SUCCESS ; -} -EXPORT_SYMBOL_GPL (kvmppc_ioba_validate); - -/* * Validates TCE address. * At the moment flags and page mask are validated. * As the host kernel does not access those addresses (just puts them @@ -95,10 +74,14 @@ EXPORT_SYMBOL_GPL(kvmppc_ioba_validate);*/ long kvmppc_tce_validate (struct kvmppc_spapr_tce_table *stt,unsigned long tce) { -unsigned long page_mask = ~((1ULL << stt->page_shift) -1 ); -unsigned long mask = ~(page_mask |TCE_PCI_WRITE |TCE_PCI_READ ); +unsigned long gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); +enum dma_data_direction dir =iommu_tce_direction (tce); -if (tce & mask) +/* Allow userspace to poison TCE table */ +if (dir ==DMA_NONE ) +return H_SUCCESS ; + +if (iommu_tce_check_tce (stt->page_shift, gpa))return H_PARAMETER ;return H_SUCCESS ;
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v5 10/10 KVM: PPC: VFIO: Add in-kernel acceleration for VFIO
In reply to Alexey KardashevskiyThis allows the host kernel to handle H_PUT_TCE, H_PUT_TCE_INDIRECT and H_STUFF_TCE requests targeted an IOMMU TCE table used for VFIO without passing them to user space which saves time on switching to user space and back.
This adds H_PUT_TCE/H_PUT_TCE_INDIRECT/H_STUFF_TCE handlers to KVM. KVM tries to handle a TCE request in the real mode, if failed it passes the request to the virtual mode to complete the operation. If it a virtual mode handler fails, the request is passed to the user space; this is not expected to happen though.
To avoid dealing with page use counters (which is tricky in real mode), this only accelerates SPAPR TCE IOMMU v2 clients which are required to pre-register the userspace memory. The very first TCE request will be handled in the VFIO SPAPR TCE driver anyway as the userspace view of the TCE table (iommu_table::it_userspace) is not allocated till the very first mapping happens and we cannot call vmalloc in real mode.
If we fail to update a hardware IOMMU table unexpected reason, we just clear it and move on as there is nothing really we can do about it - for example, if we hot plug a VFIO device to a guest, existing TCE tables will be mirrored automatically to the hardware and there is no interface to report to the guest about possible failures.
This adds new attribute - KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE - to the VFIO KVM device. It takes a VFIO group fd and SPAPR TCE table fd and associates a physical IOMMU table with the SPAPR TCE table (which is a guest view of the hardware IOMMU table). The iommu_table object is cached and referenced so we do not have to look up for it in real mode.
This does not implement the UNSET counterpart as there is no use for it - once the acceleration is enabled, the existing userspace won't disable it unless a VFIO container is destroyed; this adds necessary cleanup to the KVM_DEV_VFIO_GROUP_DEL handler.
As this creates a descriptor per IOMMU table-LIOBN couple (called kvmppc_spapr_tce_iommu_table), it is possible to have several descriptors with the same iommu_table (hardware IOMMU table) attached to the same LIOBN; we do not remove duplicates though as iommu_table_ops::exchange not just update a TCE entry (which is shared among IOMMU groups) but also invalidates the TCE cache (one per IOMMU group).
This advertises the new KVM_CAP_SPAPR_TCE_VFIO capability to the user space.
This finally makes use of vfio_external_user_iommu_id() which was introduced quite some time ago and was considered for removal.
Tests show that this patch increases transmission speed from 220MB/s to 750..1020MB/s on 10Gb network (Chelsea CXGB3 10Gb ethernet card).
---
Changes:
v5:
* changed error codes in multiple places
* added bunch of WARN_ON() in places which should not really happen
* adde a check that an iommu table is not attached already to LIOBN
* dropped explicit calls to iommu_tce_clear_param_check/ iommu_tce_put_param_check as kvmppc_tce_validate/kvmppc_ioba_validate call them anyway (since the previous patch)
* if we fail to update a hardware IOMMU table for unexpected reason, this just clears the entry
v4:
* added note to the commit log about allowing multiple updates of the same IOMMU table;
* instead of checking for if any memory was preregistered, this returns H_TOO_HARD if a specific page was not;
* fixed comments from v3 about error handling in many places;
* simplified TCE handlers and merged IOMMU parts inline - for example, there used to be kvmppc_h_put_tce_iommu(), now it is merged into kvmppc_h_put_tce(); this allows to check IOBA boundaries against the first attached table only (makes the code simpler);
v3:
* simplified not to use VFIO group notifiers
* reworked cleanup, should be cleaner/simpler now
v2:
* reworked to use new VFIO notifiers
* now same iommu_table may appear in the list several times, to be fixed later
--- Documentation/virtual/kvm/devices/vfio.txt | 22 ++- arch/powerpc/include/asm/kvm_host.h | 8 + arch/powerpc/include/asm/kvm_ppc.h | 4 + include/uapi/linux/kvm.h | 8 + arch/powerpc/kvm/book3s_64_vio.c | 307 ++++++++++++++++++++++++++++- arch/powerpc/kvm/book3s_64_vio_hv.c | 152 +++++++++++++- arch/powerpc/kvm/powerpc.c | 2 + virt/kvm/vfio.c | 60 ++++++ 8 files changed, 555 insertions(+), 8 deletions(-)
Documentation/virtual/kvm/devices/vfio.txt +20 -2
@@ -16,7 +16,25 @@ Groups: KVM_DEV_VFIO_GROUP attributes: KVM_DEV_VFIO_GROUP_ADD: Add a VFIO group to VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. KVM_DEV_VFIO_GROUP_DEL: Remove a VFIO group from VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. + KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: attaches a guest visible TCE table + allocated by sPAPR KVM. + kvm_device_attr.addr points to a struct: -For each, kvm_device_attr.addr points to an int32_t file descriptor -for the VFIO group. + struct kvm_vfio_spapr_tce { + __u32 argsz; + __u32 flags; + __s32 groupfd; + __s32 tablefd; + }; + + where + @argsz is the size of kvm_vfio_spapr_tce_liobn; + @flags are not supported now, must be zero; + @groupfd is a file descriptor for a VFIO group; + @tablefd is a file descriptor for a TCE table allocated via + KVM_CREATE_SPAPR_TCE.
arch/powerpc/include/asm/kvm_host.h +8 -0
@@ -191,6 +191,13 @@ struct kvmppc_pginfo {atomic_t refcnt; }; +struct kvmppc_spapr_tce_iommu_table { +struct rcu_head rcu; +struct list_head next; +struct vfio_group *group; +struct iommu_table *tbl; +}; +struct kvmppc_spapr_tce_table {struct list_head list;struct kvm *kvm;
@@ -199,6 +206,7 @@ struct kvmppc_spapr_tce_table {u32 page_shift;u64 offset;/* in pages */ u64 size;/* window size in pages */ +struct list_head iommu_tables;struct page *pages[0 ]; };
arch/powerpc/include/asm/kvm_ppc.h +4 -0
@@ -163,6 +163,10 @@ extern long kvmppc_prepare_vrma(struct kvm *kvm,extern void kvmppc_map_vrma (struct kvm_vcpu *vcpu,struct kvm_memory_slot *memslot,unsigned long porder);extern int kvmppc_pseries_do_hcall (struct kvm_vcpu *vcpu); +extern long kvm_spapr_tce_attach_iommu_group (struct kvm *kvm,int tablefd, +struct vfio_group *group); +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group);extern long kvm_vm_ioctl_create_spapr_tce (struct kvm *kvm,struct kvm_create_spapr_tce_64 *args);
include/uapi/linux/kvm.h +8 -0
@@ -1076,6 +1076,7 @@ struct kvm_device_attr {#define KVM_DEV_VFIO_GROUP 1#define KVM_DEV_VFIO_GROUP_ADD 1#define KVM_DEV_VFIO_GROUP_DEL 2 +#define KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE 3enum kvm_device_type {KVM_DEV_TYPE_FSL_MPIC_20 =1 ,
@@ -1097,6 +1098,13 @@ enum kvm_device_type { KVM_DEV_TYPE_MAX, }; +struct kvm_vfio_spapr_tce { +__u32 argsz; +__u32 flags; +__s32 groupfd; +__s32 tablefd; +}; +/* * ioctls for VM fds */
arch/powerpc/kvm/book3s_64_vio.c +304 -3
379 lines not on this page · raw message
arch/powerpc/kvm/book3s_64_vio_hv.c +149 -3
200 lines not on this page · raw message
arch/powerpc/kvm/powerpc.c +2 -0
@@ -536,6 +536,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)#ifdef CONFIG_PPC_BOOK3S_64 case KVM_CAP_SPAPR_TCE :case KVM_CAP_SPAPR_TCE_64 : +/* fallthrough */ +case KVM_CAP_SPAPR_TCE_VFIO :case KVM_CAP_PPC_RTAS :case KVM_CAP_PPC_FIXUP_HCALL :case KVM_CAP_PPC_ENABLE_HCALL :
virt/kvm/vfio.c +60 -0
90 lines not on this page · raw message
Signature · 1 line
2.11.0
Re: [PATCH kernel v5 10/10] KVM: PPC: VFIO: Add in-kernel acceleration for VFIO
Quoted 46 lines · Alexey Kardashevskiy
This allows the host kernel to handle
H_PUT_TCE,H_PUT_TCE_INDIRECTandH_STUFF_TCErequests targeted an IOMMU TCE table used for VFIO without passing them to user space which saves time on switching to user space and back.This adds H_PUT_TCE/H_PUT_TCE_INDIRECT/H_STUFF_TCE handlers to KVM. KVM tries to handle a TCE request in the real mode, if failed it passes the request to the virtual mode to complete the operation. If it a virtual mode handler fails, the request is passed to the user space; this is not expected to happen though.
To avoid dealing with page use counters (which is tricky in real mode), this only accelerates SPAPR TCE IOMMU v2 clients which are required to pre-register the userspace memory. The very first TCE request will be handled in the VFIO SPAPR TCE driver anyway as the userspace view of the TCE table (
iommu_table::it_userspace) is not allocated till the very first mapping happens and we cannot call vmalloc in real mode.If we fail to update a hardware IOMMU table unexpected reason, we just clear it and move on as there is nothing really we can do about it - for example, if we hot plug a VFIO device to a guest, existing TCE tables will be mirrored automatically to the hardware and there is no interface to report to the guest about possible failures.
This adds new attribute -
KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE- to the VFIO KVM device. It takes a VFIO group fd and SPAPR TCE table fd and associates a physical IOMMU table with the SPAPR TCE table (which is a guest view of the hardware IOMMU table). Theiommu_tableobject is cached and referenced so we do not have to look up for it in real mode.This does not implement the UNSET counterpart as there is no use for it - once the acceleration is enabled, the existing userspace won't disable it unless a VFIO container is destroyed; this adds necessary cleanup to the
KVM_DEV_VFIO_GROUP_DELhandler.As this creates a descriptor per IOMMU table-LIOBN couple (called
kvmppc_spapr_tce_iommu_table), it is possible to have several descriptors with the sameiommu_table(hardware IOMMU table) attached to the same LIOBN; we do not remove duplicates though asiommu_table_ops::exchangenot just update a TCE entry (which is shared among IOMMU groups) but also invalidates the TCE cache (one per IOMMU group).This advertises the new
KVM_CAP_SPAPR_TCE_VFIOcapability to the user space.
Quoted 7 lines
This finally makes use of
vfio_external_user_iommu_id()which was introduced quite some time ago and was considered for removal.Tests show that this patch increases transmission speed from 220MB/s to 750..1020MB/s on 10Gb network (Chelsea CXGB3 10Gb ethernet card).
Signed-off-by Alexey Kardashevskiy <[email protected]>
I have some comments on this patch, but all the definite ones are pretty minor and could be done as later cleanups.
I have some more serious queries, but they are just queries and requests for clarification. If there are satisfactory answers to them, I'll add my R-b.
---
Quoted 238 lines
Changes:
v5:
* changed error codes in multiple places
* added bunch ofWARN_ON()in places which should not really happen
* adde a check that an iommu table is not attached already to LIOBN
* dropped explicit calls to iommu_tce_clear_param_check/iommu_tce_put_param_checkas kvmppc_tce_validate/kvmppc_ioba_validate call them anyway (since the previous patch)
* if we fail to update a hardware IOMMU table for unexpected reason, this just clears the entryv4:
* added note to the commit log about allowing multiple updates of the same IOMMU table;
* instead of checking for if any memory was preregistered, this returnsH_TOO_HARDif a specific page was not;
* fixed comments from v3 about error handling in many places;
* simplified TCE handlers and merged IOMMU parts inline - for example, there used to bekvmppc_h_put_tce_iommu(), now it is merged intokvmppc_h_put_tce(); this allows to check IOBA boundaries against the first attached table only (makes the code simpler);v3:
* simplified not to use VFIO group notifiers
* reworked cleanup, should be cleaner/simpler nowv2:
* reworked to use new VFIO notifiers
* now sameiommu_tablemay appear in the list several times, to be fixed later--- Documentation/virtual/kvm/devices/vfio.txt | 22 ++- arch/powerpc/include/asm/kvm_host.h | 8 + arch/powerpc/include/asm/kvm_ppc.h | 4 + include/uapi/linux/kvm.h | 8 + arch/powerpc/kvm/book3s_64_vio.c | 307 ++++++++++++++++++++++++++++- arch/powerpc/kvm/book3s_64_vio_hv.c | 152 +++++++++++++- arch/powerpc/kvm/powerpc.c | 2 + virt/kvm/vfio.c | 60 ++++++ 8 files changed, 555 insertions(+), 8 deletions(-)Documentation/virtual/kvm/devices/vfio.txt +20
-2@@ -16,7 +16,25 @@ Groups: KVM_DEV_VFIO_GROUP attributes: KVM_DEV_VFIO_GROUP_ADD: Add a VFIO group to VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. KVM_DEV_VFIO_GROUP_DEL: Remove a VFIO group from VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. + KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: attaches a guest visible TCE table + allocated by sPAPR KVM. + kvm_device_attr.addr points to a struct: -For each, kvm_device_attr.addr points to an int32_t file descriptor -for the VFIO group. + struct kvm_vfio_spapr_tce { + __u32 argsz; + __u32 flags; + __s32 groupfd; + __s32 tablefd; + }; + + where + @argsz is the size of kvm_vfio_spapr_tce_liobn; + @flags are not supported now, must be zero; + @groupfd is a file descriptor for a VFIO group; + @tablefd is a file descriptor for a TCE table allocated via + KVM_CREATE_SPAPR_TCE.arch/powerpc/include/asm/kvm_host.h +8
-0@@ -191,6 +191,13 @@ struct kvmppc_pginfo {atomic_t refcnt; }; +struct kvmppc_spapr_tce_iommu_table { +struct rcu_head rcu; +struct list_head next; +struct vfio_group *group; +struct iommu_table *tbl; +}; +struct kvmppc_spapr_tce_table {struct list_head list;struct kvm *kvm;@@ -199,6 +206,7 @@ struct kvmppc_spapr_tce_table {u32 page_shift;u64 offset;/* in pages */ u64 size;/* window size in pages */ +struct list_head iommu_tables;struct page *pages[0 ]; };arch/powerpc/include/asm/kvm_ppc.h +4
-0@@ -163,6 +163,10 @@ extern long kvmppc_prepare_vrma(struct kvm *kvm,extern void kvmppc_map_vrma (struct kvm_vcpu *vcpu,struct kvm_memory_slot *memslot,unsigned long porder);extern int kvmppc_pseries_do_hcall (struct kvm_vcpu *vcpu); +extern long kvm_spapr_tce_attach_iommu_group (struct kvm *kvm,int tablefd, +struct vfio_group *group); +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group);extern long kvm_vm_ioctl_create_spapr_tce (struct kvm *kvm,struct kvm_create_spapr_tce_64 *args);include/uapi/linux/kvm.h +8
-0@@ -1076,6 +1076,7 @@ struct kvm_device_attr {#define KVM_DEV_VFIO_GROUP 1#define KVM_DEV_VFIO_GROUP_ADD 1#define KVM_DEV_VFIO_GROUP_DEL 2 +#define KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE 3enum kvm_device_type {KVM_DEV_TYPE_FSL_MPIC_20 =1 ,@@ -1097,6 +1098,13 @@ enum kvm_device_type { KVM_DEV_TYPE_MAX, }; +struct kvm_vfio_spapr_tce { +__u32 argsz; +__u32 flags; +__s32 groupfd; +__s32 tablefd; +}; +/* * ioctls for VM fds */ arch/powerpc/kvm/book3s_64_vio.c +77
-0@@ -27,6 +27,10 @@#include <linux/hugetlb.h> #include <linux/list.h> #include <linux/anon_inodes.h> +#include <linux/iommu.h> +#include <linux/file.h> +#include <linux/vfio.h> +#include <linux/module.h> #include <asm/tlbflush.h> #include <asm/kvm_ppc.h> @@ -39,6 +43,36 @@#include <asm/udbg.h> #include <asm/iommu.h> #include <asm/tce.h> +#include <asm/mmu_context.h> + +static void kvm_vfio_group_put_external_user (struct vfio_group *vfio_group) +{ +void (*fn)(struct vfio_group *); + + fn =symbol_get (vfio_group_put_external_user); +if (WARN_ON (!fn)) +return ; + +fn (vfio_group); + +symbol_put (vfio_group_put_external_user); +} + +static int kvm_vfio_external_user_iommu_id (struct vfio_group *vfio_group) +{ +int (*fn)(struct vfio_group *); +int ret =-1 ; + + fn =symbol_get (vfio_external_user_iommu_id); +if (!fn) +return ret; + + ret =fn (vfio_group); + +symbol_put (vfio_external_user_iommu_id); + +return ret; +}static unsigned long kvmppc_tce_pages (unsigned long iommu_pages) {@@ -90,6 +124,130 @@ static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)return ret; } +static void kvm_spapr_tce_iommu_table_free (struct rcu_head *head) +{ +struct kvmppc_spapr_tce_iommu_table *stit =container_of (head, + struct kvmppc_spapr_tce_iommu_table, rcu); + +iommu_table_put (stit->tbl); +kvm_vfio_group_put_external_user (stit->group); + +kfree (stit); +} + +static void kvm_spapr_tce_liobn_release_iommu_group ( +struct kvmppc_spapr_tce_table *stt, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_iommu_table *stit, *tmp; + +list_for_each_entry_safe (stit, tmp, &stt->iommu_tables, next) { +if (group && (stit->group != group)) +continue ; + +list_del_rcu (&stit->next); + +call_rcu (&stit->rcu, kvm_spapr_tce_iommu_table_free); + } +} + +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_table *stt; + +list_for_each_entry_rcu (stt, &kvm->arch.spapr_tce_tables, list) +kvm_spapr_tce_liobn_release_iommu_group (stt, group); +} + +extern long kvm_spapr_tce_attach_iommu_group (struct kvm *kvm,int tablefd, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_table *stt =NULL ; +bool found = false; +struct iommu_table *tbl =NULL ; +struct iommu_table_group *table_group;
arch/powerpc/kvm/book3s_64_vio.c static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc) · line 179
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio.c +10
-0+long i, ret =0 ; +struct kvmppc_spapr_tce_iommu_table *stit; +struct fd f; +int group_id; +struct iommu_group *grp; + + group_id =kvm_vfio_external_user_iommu_id (group); + grp =iommu_group_get_by_id (group_id); +if (WARN_ON (!grp)) +return -EIO ;
I think it would be nicer to have a function that goes directly from vfio_group to iommu_group rather than going via id. That can be a later cleanup, though.
Quoted 97 lines
arch/powerpc/kvm/book3s_64_vio.c +76
-0+ + f =fdget (tablefd); +if (!f.file) { + ret = -EBADF ; + goto put_exit; + } + +list_for_each_entry_rcu (stt, &kvm->arch.spapr_tce_tables, list) { +if (stt == f.file->private_data) { + found = true; +break ; + } + } + +fdput (f); + +if (!found) { + ret = -EINVAL ; + goto put_exit; + } + + table_group =iommu_group_get_iommudata (grp); +if (WARN_ON (!table_group)) { + ret = -EFAULT ; + goto put_exit; + } + +for (i =0 ; i <IOMMU_TABLE_GROUP_MAX_TABLES ; ++i) { +struct iommu_table *tbltmp = table_group->tables[i]; + +if (!tbltmp) +continue ; + +/* +* Make sure hardware table parameters are exactly the same; +* this is used in the TCE handlers where boundary checks +* use only the first attached table. +*/ +if ((tbltmp->it_page_shift == stt->page_shift) && + (tbltmp->it_offset == stt->offset) && + (tbltmp->it_size == stt->size)) { + tbl = tbltmp; +break ; + } + } +if (!tbl) { + ret = -EINVAL ; + goto put_exit; + } + +list_for_each_entry_rcu (stit, &stt->iommu_tables, next) { +if ((stit->tbl == tbl) && (stit->group == group)) { + ret = -EBUSY ; + goto put_exit; + } + } + +iommu_table_get (tbl); + + stit =kzalloc (sizeof (*stit),GFP_KERNEL ); + stit->tbl = tbl; + stit->group = group; + +list_add_rcu (&stit->next, &stt->iommu_tables); + +put_exit: +iommu_group_put (grp); + +return ret; +} +static void release_spapr_tce_table (struct rcu_head *head) { struct kvmppc_spapr_tce_table *stt = container_of(head,@@ -132,6 +290,8 @@ static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)list_del_rcu (&stt->list); +kvm_spapr_tce_liobn_release_iommu_group (stt,NULL /* release all */ ); +kvm_put_kvm (stt->kvm); kvmppc_account_memlimit(@@ -181,6 +341,7 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, stt->offset = args->offset; stt->size = size; stt->kvm = kvm; +INIT_LIST_HEAD_RCU (&stt->iommu_tables);for (i =0 ; i < npages; i++) { stt->pages[i] =alloc_page (GFP_KERNEL | __GFP_ZERO);@@ -209,11 +370,102 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,return ret; } +static void kvmppc_clear_tce (struct iommu_table *tbl,unsigned long entry) +{
arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · line 384
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio.c +10
-0+unsigned long hpa =0 ; +enum dma_data_direction dir =DMA_NONE ; + +iommu_tce_xchg (tbl, entry, &hpa, &dir); +} + +static long kvmppc_tce_iommu_mapped_dec (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +struct mm_iommu_table_group_mem_t *mem =NULL ;
Initialization is unnecessary for the variable above.
Quoted 48 lines
arch/powerpc/kvm/book3s_64_vio.c +48
-0+const unsigned long pgsize =1ULL << tbl->it_page_shift; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); + +if (WARN_ON (!pua)) +return H_HARDWARE ; + + mem =mm_iommu_lookup (kvm->mm, *pua, pgsize); +if (!mem) +return H_TOO_HARD ; + +mm_iommu_mapped_dec (mem); + + *pua =0 ; + +return H_SUCCESS ; +} + +static long kvmppc_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret; + +if (iommu_tce_xchg (tbl, entry, &hpa, &dir)) +return H_HARDWARE ; + +if (dir ==DMA_NONE ) +return H_SUCCESS ; + + ret =kvmppc_tce_iommu_mapped_dec (kvm, tbl, entry); +if (ret !=H_SUCCESS ) +iommu_tce_xchg (tbl, entry, &hpa, &dir); + +return ret; +} + +long kvmppc_tce_iommu_map (struct kvm *kvm,struct iommu_table *tbl, +unsigned long entry,unsigned long gpa, +enum dma_data_direction dir) +{ +long ret; +unsigned long hpa, ua, *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); +struct mm_iommu_table_group_mem_t *mem; + +if (!pua) +/* it_userspace allocation might be delayed */ +return H_TOO_HARD ;
arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · line 442
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio.c +10
-0+ +if (kvmppc_gpa_to_ua (kvm, gpa, &ua,NULL )) +return H_PARAMETER ; + + mem =mm_iommu_lookup (kvm->mm, ua,1ULL << tbl->it_page_shift); +if (!mem) +return H_TOO_HARD ; + +if (WARN_ON (mm_iommu_ua_to_hpa (mem, ua, &hpa))) +return H_HARDWARE ;
Remind me - could a failure here be triggered by userspace failing to preregister all the memory it should?
Quoted 159 lines
arch/powerpc/kvm/book3s_64_vio.c +72
-3+if (mm_iommu_mapped_inc (mem)) +return H_CLOSED ; + + ret =iommu_tce_xchg (tbl, entry, &hpa, &dir); +if (ret) { +mm_iommu_mapped_dec (mem); +return H_TOO_HARD ; + } + +if (dir !=DMA_NONE ) +kvmppc_tce_iommu_mapped_dec (kvm, tbl, entry); + + *pua = ua; + +return 0 ; +} +long kvmppc_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) {struct kvmppc_spapr_tce_table *stt; -long ret; +long ret, idx; +struct kvmppc_spapr_tce_iommu_table *stit; +unsigned long entry, gpa; +enum dma_data_direction dir;/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ @@ -230,7 +482,28 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,if (ret !=H_SUCCESS )return ret; -kvmppc_tce_put (stt, ioba >> stt->page_shift, tce); + entry = ioba >> stt->page_shift; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + dir =iommu_tce_direction (tce); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +if (dir ==DMA_NONE ) { + ret =kvmppc_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry); + }else { + idx =srcu_read_lock (&vcpu->kvm->srcu); + ret =kvmppc_tce_iommu_map (vcpu->kvm, stit->tbl, + entry, gpa, dir); +srcu_read_unlock (&vcpu->kvm->srcu, idx); + } + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + +kvmppc_tce_put (stt, entry, tce);return H_SUCCESS ; }@@ -242,9 +515,10 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS , idx; -unsigned long entry, ua =0 ; +unsigned long entry, ua =0 , gpa;u64 __user *tces;u64 tce; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -283,6 +557,18 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,if (ret !=H_SUCCESS ) goto unlock_exit; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) + goto unlock_exit; + } +kvmppc_tce_put (stt, entry + i, tce); }@@ -299,6 +585,7 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -312,6 +599,20 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,if (tce_value & (TCE_PCI_WRITE |TCE_PCI_READ ))return H_PARAMETER ; +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +unsigned long entry = ioba >> stit->tbl->it_page_shift; + +for (i =0 ; i < npages; ++i) { + ret =kvmppc_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry + i); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + } +for (i =0 ; i < npages; ++i, ioba += (1ULL << stt->page_shift))kvmppc_tce_put (stt, ioba >> stt->page_shift, tce_value);arch/powerpc/kvm/book3s_64_vio_hv.c +32
-0@@ -161,11 +161,111 @@ long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,EXPORT_SYMBOL_GPL (kvmppc_gpa_to_ua);#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE +static void kvmppc_rm_clear_tce (struct iommu_table *tbl,unsigned long entry) +{ +unsigned long hpa =0 ; +enum dma_data_direction dir =DMA_NONE ; + +iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); +} + +static long kvmppc_rm_tce_iommu_mapped_dec (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +struct mm_iommu_table_group_mem_t *mem =NULL ; +const unsigned long pgsize =1ULL << tbl->it_page_shift; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); + +if (WARN_ON (!pua)) +return H_HARDWARE ; + + pua = (void *)vmalloc_to_phys (pua); +if (!pua) +return H_TOO_HARD ; + + mem =mm_iommu_lookup_rm (kvm->mm, *pua, pgsize); +if (!mem) +return H_TOO_HARD ; + +mm_iommu_mapped_dec (mem); + + *pua =0 ; + +return H_SUCCESS ; +}
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, · line 205
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +10
-0+ +static long kvmppc_rm_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret; + +if (iommu_tce_xchg_rm (tbl, entry, &hpa, &dir)) +return H_HARDWARE ;
To avoid a double WARN() (and to make the warnings easier to understand) I'd suggest putting a WARN_ON() here, rather than in the callers when they receieve an H_HARDWARE. IIUC this really shouldn't ever happen, and it certainly can't be the guest's fault?
Quoted 113 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +80
-3+ +if (dir ==DMA_NONE ) +return H_SUCCESS ; + + ret =kvmppc_rm_tce_iommu_mapped_dec (kvm, tbl, entry); +if (ret) +iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); + +return ret; +} + +static long kvmppc_rm_tce_iommu_map (struct kvm *kvm,struct iommu_table *tbl, +unsigned long entry,unsigned long gpa, +enum dma_data_direction dir) +{ +long ret; +unsigned long hpa =0 , ua; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); +struct mm_iommu_table_group_mem_t *mem; + +if (!pua) +/* it_userspace allocation might be delayed */ +return H_TOO_HARD ; + +if (kvmppc_gpa_to_ua (kvm, gpa, &ua,NULL )) +return H_PARAMETER ; + + mem =mm_iommu_lookup_rm (kvm->mm, ua,1ULL << tbl->it_page_shift); +if (!mem) +return H_TOO_HARD ; + +if (WARN_ON (mm_iommu_ua_to_hpa_rm (mem, ua, &hpa))) +return H_HARDWARE ; + + pua = (void *)vmalloc_to_phys (pua); +if (WARN_ON (!pua)) +return H_HARDWARE ; + +if (WARN_ON (mm_iommu_mapped_inc (mem))) +return H_CLOSED ; + + ret =iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); +if (ret) { +mm_iommu_mapped_dec (mem); +return H_TOO_HARD ; + } + +if (dir !=DMA_NONE ) +kvmppc_rm_tce_iommu_mapped_dec (kvm, tbl, entry); + + *pua = ua; + +return 0 ; +} +long kvmppc_rm_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) {struct kvmppc_spapr_tce_table *stt;long ret; +struct kvmppc_spapr_tce_iommu_table *stit; +unsigned long entry, gpa; +enum dma_data_direction dir;/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ @@ -182,7 +282,25 @@ long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,if (ret !=H_SUCCESS )return ret; -kvmppc_tce_put (stt, ioba >> stt->page_shift, tce); + entry = ioba >> stt->page_shift; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + dir =iommu_tce_direction (tce); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +if (dir ==DMA_NONE ) + ret =kvmppc_rm_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry); +else + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry, gpa, dir); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_rm_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + +kvmppc_tce_put (stt, entry, tce);return H_SUCCESS ; }@@ -220,9 +338,10 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS ; -unsigned long tces, entry, ua =0 ; +unsigned long tces, entry, ua =0 , tce, gpa;unsigned long *rmap =NULL ;bool prereg = false; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -287,12 +406,24 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, }for (i =0 ; i < npages; ++i) { -unsigned long tce =be64_to_cpu (((u64 *)tces)[i]); + tce =be64_to_cpu (((u64 *)tces)[i]); ret =kvmppc_tce_validate (stt, tce);
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, · line 421
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +7
-0if (ret !=H_SUCCESS ) goto unlock_exit; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE ))
I don't think you need the WARN() here - the only H_HARDWARE failure path in iommu_map() already includes a WARN().
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, · line 424
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+kvmppc_rm_clear_tce (stit->tbl, entry); + else if (ret != H_SUCCESS) + goto unlock_exit;
It's also not clear to me why the H_HARDWARE error path clears the entry, but the other failure paths don't. Or why an H_HARDWARE will result in continuing to set the rest of the TCEs, but other failures won't.
Quoted 16 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+ } +kvmppc_tce_put (stt, entry + i, tce); }@@ -309,6 +440,7 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -322,6 +454,20 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,if (tce_value & (TCE_PCI_WRITE |TCE_PCI_READ ))return H_PARAMETER ;
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, · line 465
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +9
-0+list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +unsigned long entry = ioba >> stit->tbl->it_page_shift; + +for (i =0 ; i < npages; ++i) { + ret =kvmppc_rm_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry + i); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_rm_clear_tce (stit->tbl, entry);
As noted earlier, I think this WARN belongs within iommu_unmap() rather than out here.
Quoted 120 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +5
-0+else if (ret !=H_SUCCESS ) +return ret; + } + } +for (i =0 ; i < npages; ++i, ioba += (1ULL << stt->page_shift))kvmppc_tce_put (stt, ioba >> stt->page_shift, tce_value);arch/powerpc/kvm/powerpc.c +2
-0@@ -536,6 +536,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)#ifdef CONFIG_PPC_BOOK3S_64 case KVM_CAP_SPAPR_TCE :case KVM_CAP_SPAPR_TCE_64 : +/* fallthrough */ +case KVM_CAP_SPAPR_TCE_VFIO :case KVM_CAP_PPC_RTAS :case KVM_CAP_PPC_FIXUP_HCALL :case KVM_CAP_PPC_ENABLE_HCALL :virt/kvm/vfio.c +60
-0@@ -20,6 +20,10 @@#include <linux/vfio.h> #include "vfio.h" +#ifdef CONFIG_SPAPR_TCE_IOMMU +#include <asm/kvm_ppc.h> +#endif +struct kvm_vfio_group {struct list_head node;struct vfio_group *vfio_group;@@ -211,6 +215,9 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)mutex_unlock (&kv->lock); +#ifdef CONFIG_SPAPR_TCE_IOMMU +kvm_spapr_tce_release_iommu_group (dev->kvm, vfio_group); +#endif kvm_vfio_group_set_kvm (vfio_group,NULL );kvm_vfio_group_put_external_user (vfio_group);@@ -218,6 +225,53 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)kvm_vfio_update_coherency (dev);return ret; + +#ifdef CONFIG_SPAPR_TCE_IOMMU +case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE : { +struct kvm_vfio_spapr_tce param; +unsigned long minsz; +struct kvm_vfio *kv = dev->private; +struct vfio_group *vfio_group; +struct kvm_vfio_group *kvg; +struct fd f; + + minsz = offsetofend(struct kvm_vfio_spapr_tce, tablefd); + +if (copy_from_user (¶m, (void __user *)arg, minsz)) +return -EFAULT ; + +if (param.argsz < minsz || param.flags) +return -EINVAL ; + + f =fdget (param.groupfd); +if (!f.file) +return -EBADF ; + + vfio_group =kvm_vfio_group_get_external_user (f.file); +fdput (f); + +if (IS_ERR (vfio_group)) +return PTR_ERR (vfio_group); + + ret = -ENOENT ; + +mutex_lock (&kv->lock); + +list_for_each_entry (kvg, &kv->group_list, node) { +if (kvg->vfio_group != vfio_group) +continue ; + + ret =kvm_spapr_tce_attach_iommu_group (dev->kvm, + param.tablefd, vfio_group); + +break ; + } + +mutex_unlock (&kv->lock); + +return ret; + } +#endif /* CONFIG_SPAPR_TCE_IOMMU */ }return -ENXIO ;@@ -242,6 +296,9 @@ static int kvm_vfio_has_attr(struct kvm_device *dev,switch (attr->attr) {case KVM_DEV_VFIO_GROUP_ADD :case KVM_DEV_VFIO_GROUP_DEL : +#ifdef CONFIG_SPAPR_TCE_IOMMU +case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE : +#endif return 0 ; }@@ -257,6 +314,9 @@ static void kvm_vfio_destroy(struct kvm_device *dev)struct kvm_vfio_group *kvg, *tmp;list_for_each_entry_safe (kvg, tmp, &kv->group_list, node) { +#ifdef CONFIG_SPAPR_TCE_IOMMU +kvm_spapr_tce_release_iommu_group (dev->kvm, kvg->vfio_group); +#endif kvm_vfio_group_set_kvm (kvg->vfio_group,NULL );kvm_vfio_group_put_external_user (kvg->vfio_group);list_del (&kvg->node);
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH kernel v5 10/10] KVM: PPC: VFIO: Add in-kernel acceleration for VFIO
Quoted 480 lines · David Gibson
Quoted 46 lines · Alexey Kardashevskiy
This allows the host kernel to handle
H_PUT_TCE,H_PUT_TCE_INDIRECTandH_STUFF_TCErequests targeted an IOMMU TCE table used for VFIO without passing them to user space which saves time on switching to user space and back.This adds H_PUT_TCE/H_PUT_TCE_INDIRECT/H_STUFF_TCE handlers to KVM. KVM tries to handle a TCE request in the real mode, if failed it passes the request to the virtual mode to complete the operation. If it a virtual mode handler fails, the request is passed to the user space; this is not expected to happen though.
To avoid dealing with page use counters (which is tricky in real mode), this only accelerates SPAPR TCE IOMMU v2 clients which are required to pre-register the userspace memory. The very first TCE request will be handled in the VFIO SPAPR TCE driver anyway as the userspace view of the TCE table (
iommu_table::it_userspace) is not allocated till the very first mapping happens and we cannot call vmalloc in real mode.If we fail to update a hardware IOMMU table unexpected reason, we just clear it and move on as there is nothing really we can do about it - for example, if we hot plug a VFIO device to a guest, existing TCE tables will be mirrored automatically to the hardware and there is no interface to report to the guest about possible failures.
This adds new attribute -
KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE- to the VFIO KVM device. It takes a VFIO group fd and SPAPR TCE table fd and associates a physical IOMMU table with the SPAPR TCE table (which is a guest view of the hardware IOMMU table). Theiommu_tableobject is cached and referenced so we do not have to look up for it in real mode.This does not implement the UNSET counterpart as there is no use for it - once the acceleration is enabled, the existing userspace won't disable it unless a VFIO container is destroyed; this adds necessary cleanup to the
KVM_DEV_VFIO_GROUP_DELhandler.As this creates a descriptor per IOMMU table-LIOBN couple (called
kvmppc_spapr_tce_iommu_table), it is possible to have several descriptors with the sameiommu_table(hardware IOMMU table) attached to the same LIOBN; we do not remove duplicates though asiommu_table_ops::exchangenot just update a TCE entry (which is shared among IOMMU groups) but also invalidates the TCE cache (one per IOMMU group).This advertises the new
KVM_CAP_SPAPR_TCE_VFIOcapability to the user space.Quoted 7 lines
This finally makes use of
vfio_external_user_iommu_id()which was introduced quite some time ago and was considered for removal.Tests show that this patch increases transmission speed from 220MB/s to 750..1020MB/s on 10Gb network (Chelsea CXGB3 10Gb ethernet card).
Signed-off-by Alexey Kardashevskiy <[email protected]>I have some comments on this patch, but all the definite ones are pretty minor and could be done as later cleanups.
I have some more serious queries, but they are just queries and requests for clarification. If there are satisfactory answers to them, I'll add my R-b.
---
Quoted 238 lines
Changes:
v5:
* changed error codes in multiple places
* added bunch ofWARN_ON()in places which should not really happen
* adde a check that an iommu table is not attached already to LIOBN
* dropped explicit calls to iommu_tce_clear_param_check/iommu_tce_put_param_checkas kvmppc_tce_validate/kvmppc_ioba_validate call them anyway (since the previous patch)
* if we fail to update a hardware IOMMU table for unexpected reason, this just clears the entryv4:
* added note to the commit log about allowing multiple updates of the same IOMMU table;
* instead of checking for if any memory was preregistered, this returnsH_TOO_HARDif a specific page was not;
* fixed comments from v3 about error handling in many places;
* simplified TCE handlers and merged IOMMU parts inline - for example, there used to bekvmppc_h_put_tce_iommu(), now it is merged intokvmppc_h_put_tce(); this allows to check IOBA boundaries against the first attached table only (makes the code simpler);v3:
* simplified not to use VFIO group notifiers
* reworked cleanup, should be cleaner/simpler nowv2:
* reworked to use new VFIO notifiers
* now sameiommu_tablemay appear in the list several times, to be fixed later--- Documentation/virtual/kvm/devices/vfio.txt | 22 ++- arch/powerpc/include/asm/kvm_host.h | 8 + arch/powerpc/include/asm/kvm_ppc.h | 4 + include/uapi/linux/kvm.h | 8 + arch/powerpc/kvm/book3s_64_vio.c | 307 ++++++++++++++++++++++++++++- arch/powerpc/kvm/book3s_64_vio_hv.c | 152 +++++++++++++- arch/powerpc/kvm/powerpc.c | 2 + virt/kvm/vfio.c | 60 ++++++ 8 files changed, 555 insertions(+), 8 deletions(-)Documentation/virtual/kvm/devices/vfio.txt +20
-2@@ -16,7 +16,25 @@ Groups: KVM_DEV_VFIO_GROUP attributes: KVM_DEV_VFIO_GROUP_ADD: Add a VFIO group to VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. KVM_DEV_VFIO_GROUP_DEL: Remove a VFIO group from VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. + KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: attaches a guest visible TCE table + allocated by sPAPR KVM. + kvm_device_attr.addr points to a struct: -For each, kvm_device_attr.addr points to an int32_t file descriptor -for the VFIO group. + struct kvm_vfio_spapr_tce { + __u32 argsz; + __u32 flags; + __s32 groupfd; + __s32 tablefd; + }; + + where + @argsz is the size of kvm_vfio_spapr_tce_liobn; + @flags are not supported now, must be zero; + @groupfd is a file descriptor for a VFIO group; + @tablefd is a file descriptor for a TCE table allocated via + KVM_CREATE_SPAPR_TCE.arch/powerpc/include/asm/kvm_host.h +8
-0@@ -191,6 +191,13 @@ struct kvmppc_pginfo {atomic_t refcnt; }; +struct kvmppc_spapr_tce_iommu_table { +struct rcu_head rcu; +struct list_head next; +struct vfio_group *group; +struct iommu_table *tbl; +}; +struct kvmppc_spapr_tce_table {struct list_head list;struct kvm *kvm;@@ -199,6 +206,7 @@ struct kvmppc_spapr_tce_table {u32 page_shift;u64 offset;/* in pages */ u64 size;/* window size in pages */ +struct list_head iommu_tables;struct page *pages[0 ]; };arch/powerpc/include/asm/kvm_ppc.h +4
-0@@ -163,6 +163,10 @@ extern long kvmppc_prepare_vrma(struct kvm *kvm,extern void kvmppc_map_vrma (struct kvm_vcpu *vcpu,struct kvm_memory_slot *memslot,unsigned long porder);extern int kvmppc_pseries_do_hcall (struct kvm_vcpu *vcpu); +extern long kvm_spapr_tce_attach_iommu_group (struct kvm *kvm,int tablefd, +struct vfio_group *group); +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group);extern long kvm_vm_ioctl_create_spapr_tce (struct kvm *kvm,struct kvm_create_spapr_tce_64 *args);include/uapi/linux/kvm.h +8
-0@@ -1076,6 +1076,7 @@ struct kvm_device_attr {#define KVM_DEV_VFIO_GROUP 1#define KVM_DEV_VFIO_GROUP_ADD 1#define KVM_DEV_VFIO_GROUP_DEL 2 +#define KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE 3enum kvm_device_type {KVM_DEV_TYPE_FSL_MPIC_20 =1 ,@@ -1097,6 +1098,13 @@ enum kvm_device_type { KVM_DEV_TYPE_MAX, }; +struct kvm_vfio_spapr_tce { +__u32 argsz; +__u32 flags; +__s32 groupfd; +__s32 tablefd; +}; +/* * ioctls for VM fds */ arch/powerpc/kvm/book3s_64_vio.c +77
-0@@ -27,6 +27,10 @@#include <linux/hugetlb.h> #include <linux/list.h> #include <linux/anon_inodes.h> +#include <linux/iommu.h> +#include <linux/file.h> +#include <linux/vfio.h> +#include <linux/module.h> #include <asm/tlbflush.h> #include <asm/kvm_ppc.h> @@ -39,6 +43,36 @@#include <asm/udbg.h> #include <asm/iommu.h> #include <asm/tce.h> +#include <asm/mmu_context.h> + +static void kvm_vfio_group_put_external_user (struct vfio_group *vfio_group) +{ +void (*fn)(struct vfio_group *); + + fn =symbol_get (vfio_group_put_external_user); +if (WARN_ON (!fn)) +return ; + +fn (vfio_group); + +symbol_put (vfio_group_put_external_user); +} + +static int kvm_vfio_external_user_iommu_id (struct vfio_group *vfio_group) +{ +int (*fn)(struct vfio_group *); +int ret =-1 ; + + fn =symbol_get (vfio_external_user_iommu_id); +if (!fn) +return ret; + + ret =fn (vfio_group); + +symbol_put (vfio_external_user_iommu_id); + +return ret; +}static unsigned long kvmppc_tce_pages (unsigned long iommu_pages) {@@ -90,6 +124,130 @@ static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)return ret; } +static void kvm_spapr_tce_iommu_table_free (struct rcu_head *head) +{ +struct kvmppc_spapr_tce_iommu_table *stit =container_of (head, + struct kvmppc_spapr_tce_iommu_table, rcu); + +iommu_table_put (stit->tbl); +kvm_vfio_group_put_external_user (stit->group); + +kfree (stit); +} + +static void kvm_spapr_tce_liobn_release_iommu_group ( +struct kvmppc_spapr_tce_table *stt, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_iommu_table *stit, *tmp; + +list_for_each_entry_safe (stit, tmp, &stt->iommu_tables, next) { +if (group && (stit->group != group)) +continue ; + +list_del_rcu (&stit->next); + +call_rcu (&stit->rcu, kvm_spapr_tce_iommu_table_free); + } +} + +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_table *stt; + +list_for_each_entry_rcu (stt, &kvm->arch.spapr_tce_tables, list) +kvm_spapr_tce_liobn_release_iommu_group (stt, group); +} + +extern long kvm_spapr_tce_attach_iommu_group (struct kvm *kvm,int tablefd, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_table *stt =NULL ; +bool found = false; +struct iommu_table *tbl =NULL ; +struct iommu_table_group *table_group;arch/powerpc/kvm/book3s_64_vio.c static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc) · line 179
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio.c +10
-0+long i, ret =0 ; +struct kvmppc_spapr_tce_iommu_table *stit; +struct fd f; +int group_id; +struct iommu_group *grp; + + group_id =kvm_vfio_external_user_iommu_id (group); + grp =iommu_group_get_by_id (group_id); +if (WARN_ON (!grp)) +return -EIO ;I think it would be nicer to have a function that goes directly from
vfio_grouptoiommu_grouprather than going via id. That can be a later cleanup, though.Quoted 97 lines
arch/powerpc/kvm/book3s_64_vio.c +76
-0+ + f =fdget (tablefd); +if (!f.file) { + ret = -EBADF ; + goto put_exit; + } + +list_for_each_entry_rcu (stt, &kvm->arch.spapr_tce_tables, list) { +if (stt == f.file->private_data) { + found = true; +break ; + } + } + +fdput (f); + +if (!found) { + ret = -EINVAL ; + goto put_exit; + } + + table_group =iommu_group_get_iommudata (grp); +if (WARN_ON (!table_group)) { + ret = -EFAULT ; + goto put_exit; + } + +for (i =0 ; i <IOMMU_TABLE_GROUP_MAX_TABLES ; ++i) { +struct iommu_table *tbltmp = table_group->tables[i]; + +if (!tbltmp) +continue ; + +/* +* Make sure hardware table parameters are exactly the same; +* this is used in the TCE handlers where boundary checks +* use only the first attached table. +*/ +if ((tbltmp->it_page_shift == stt->page_shift) && + (tbltmp->it_offset == stt->offset) && + (tbltmp->it_size == stt->size)) { + tbl = tbltmp; +break ; + } + } +if (!tbl) { + ret = -EINVAL ; + goto put_exit; + } + +list_for_each_entry_rcu (stit, &stt->iommu_tables, next) { +if ((stit->tbl == tbl) && (stit->group == group)) { + ret = -EBUSY ; + goto put_exit; + } + } + +iommu_table_get (tbl); + + stit =kzalloc (sizeof (*stit),GFP_KERNEL ); + stit->tbl = tbl; + stit->group = group; + +list_add_rcu (&stit->next, &stt->iommu_tables); + +put_exit: +iommu_group_put (grp); + +return ret; +} +static void release_spapr_tce_table (struct rcu_head *head) { struct kvmppc_spapr_tce_table *stt = container_of(head,@@ -132,6 +290,8 @@ static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)list_del_rcu (&stt->list); +kvm_spapr_tce_liobn_release_iommu_group (stt,NULL /* release all */ ); +kvm_put_kvm (stt->kvm); kvmppc_account_memlimit(@@ -181,6 +341,7 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, stt->offset = args->offset; stt->size = size; stt->kvm = kvm; +INIT_LIST_HEAD_RCU (&stt->iommu_tables);for (i =0 ; i < npages; i++) { stt->pages[i] =alloc_page (GFP_KERNEL | __GFP_ZERO);@@ -209,11 +370,102 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,return ret; } +static void kvmppc_clear_tce (struct iommu_table *tbl,unsigned long entry) +{arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · line 384
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio.c +10
-0+unsigned long hpa =0 ; +enum dma_data_direction dir =DMA_NONE ; + +iommu_tce_xchg (tbl, entry, &hpa, &dir); +} + +static long kvmppc_tce_iommu_mapped_dec (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +struct mm_iommu_table_group_mem_t *mem =NULL ;Initialization is unnecessary for the variable above.
Quoted 53 lines
arch/powerpc/kvm/book3s_64_vio.c +53
-0+const unsigned long pgsize =1ULL << tbl->it_page_shift; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); + +if (WARN_ON (!pua)) +return H_HARDWARE ; + + mem =mm_iommu_lookup (kvm->mm, *pua, pgsize); +if (!mem) +return H_TOO_HARD ; + +mm_iommu_mapped_dec (mem); + + *pua =0 ; + +return H_SUCCESS ; +} + +static long kvmppc_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret; + +if (iommu_tce_xchg (tbl, entry, &hpa, &dir)) +return H_HARDWARE ; + +if (dir ==DMA_NONE ) +return H_SUCCESS ; + + ret =kvmppc_tce_iommu_mapped_dec (kvm, tbl, entry); +if (ret !=H_SUCCESS ) +iommu_tce_xchg (tbl, entry, &hpa, &dir); + +return ret; +} + +long kvmppc_tce_iommu_map (struct kvm *kvm,struct iommu_table *tbl, +unsigned long entry,unsigned long gpa, +enum dma_data_direction dir) +{ +long ret; +unsigned long hpa, ua, *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); +struct mm_iommu_table_group_mem_t *mem; + +if (!pua) +/* it_userspace allocation might be delayed */ +return H_TOO_HARD ; + +if (kvmppc_gpa_to_ua (kvm, gpa, &ua,NULL )) +return H_PARAMETER ; + + mem =mm_iommu_lookup (kvm->mm, ua,1ULL << tbl->it_page_shift);
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · line 442
Quoted 5 lines
arch/powerpc/kvm/book3s_64_vio.c +5
-0+if (!mem) +return H_TOO_HARD ; + +if (WARN_ON (mm_iommu_ua_to_hpa (mem, ua, &hpa))) +return H_HARDWARE ;Remind me - could a failure here be triggered by userspace failing to preregister all the memory it should?
mm_iommu_lookup() should fail first as the only reason for mm_iommu_ua_to_hpa() to fail is (entry >= mem->entries) but this is checked in mm_iommu_lookup().
Quoted 167 lines
Quoted 167 lines
arch/powerpc/kvm/book3s_64_vio.c +72
-3+if (mm_iommu_mapped_inc (mem)) +return H_CLOSED ; + + ret =iommu_tce_xchg (tbl, entry, &hpa, &dir); +if (ret) { +mm_iommu_mapped_dec (mem); +return H_TOO_HARD ; + } + +if (dir !=DMA_NONE ) +kvmppc_tce_iommu_mapped_dec (kvm, tbl, entry); + + *pua = ua; + +return 0 ; +} +long kvmppc_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) {struct kvmppc_spapr_tce_table *stt; -long ret; +long ret, idx; +struct kvmppc_spapr_tce_iommu_table *stit; +unsigned long entry, gpa; +enum dma_data_direction dir;/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ @@ -230,7 +482,28 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,if (ret !=H_SUCCESS )return ret; -kvmppc_tce_put (stt, ioba >> stt->page_shift, tce); + entry = ioba >> stt->page_shift; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + dir =iommu_tce_direction (tce); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +if (dir ==DMA_NONE ) { + ret =kvmppc_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry); + }else { + idx =srcu_read_lock (&vcpu->kvm->srcu); + ret =kvmppc_tce_iommu_map (vcpu->kvm, stit->tbl, + entry, gpa, dir); +srcu_read_unlock (&vcpu->kvm->srcu, idx); + } + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + +kvmppc_tce_put (stt, entry, tce);return H_SUCCESS ; }@@ -242,9 +515,10 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS , idx; -unsigned long entry, ua =0 ; +unsigned long entry, ua =0 , gpa;u64 __user *tces;u64 tce; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -283,6 +557,18 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,if (ret !=H_SUCCESS ) goto unlock_exit; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) + goto unlock_exit; + } +kvmppc_tce_put (stt, entry + i, tce); }@@ -299,6 +585,7 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -312,6 +599,20 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,if (tce_value & (TCE_PCI_WRITE |TCE_PCI_READ ))return H_PARAMETER ; +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +unsigned long entry = ioba >> stit->tbl->it_page_shift; + +for (i =0 ; i < npages; ++i) { + ret =kvmppc_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry + i); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + } +for (i =0 ; i < npages; ++i, ioba += (1ULL << stt->page_shift))kvmppc_tce_put (stt, ioba >> stt->page_shift, tce_value);arch/powerpc/kvm/book3s_64_vio_hv.c +39
-0@@ -161,11 +161,111 @@ long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,EXPORT_SYMBOL_GPL (kvmppc_gpa_to_ua);#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE +static void kvmppc_rm_clear_tce (struct iommu_table *tbl,unsigned long entry) +{ +unsigned long hpa =0 ; +enum dma_data_direction dir =DMA_NONE ; + +iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); +} + +static long kvmppc_rm_tce_iommu_mapped_dec (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +struct mm_iommu_table_group_mem_t *mem =NULL ; +const unsigned long pgsize =1ULL << tbl->it_page_shift; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); + +if (WARN_ON (!pua)) +return H_HARDWARE ; + + pua = (void *)vmalloc_to_phys (pua); +if (!pua) +return H_TOO_HARD ; + + mem =mm_iommu_lookup_rm (kvm->mm, *pua, pgsize); +if (!mem) +return H_TOO_HARD ; + +mm_iommu_mapped_dec (mem); + + *pua =0 ; + +return H_SUCCESS ; +} + +static long kvmppc_rm_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret;
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, · line 205
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+ +if (iommu_tce_xchg_rm (tbl, entry, &hpa, &dir)) +return H_HARDWARE ;To avoid a double
WARN()(and to make the warnings easier to understand) I'd suggest putting aWARN_ON()here, rather than in the callers when they receieve anH_HARDWARE. IIUC this really shouldn't ever happen, and it certainly can't be the guest's fault?
Makes sense.
Quoted 18 lines
Quoted 18 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +18
-0+ +if (dir ==DMA_NONE ) +return H_SUCCESS ; + + ret =kvmppc_rm_tce_iommu_mapped_dec (kvm, tbl, entry); +if (ret) +iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); + +return ret; +} + +static long kvmppc_rm_tce_iommu_map (struct kvm *kvm,struct iommu_table *tbl, +unsigned long entry,unsigned long gpa, +enum dma_data_direction dir) +{ +long ret; +unsigned long hpa =0 , ua; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry);
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, · line 231
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +8
-0+struct mm_iommu_table_group_mem_t *mem; + +if (!pua) +/* it_userspace allocation might be delayed */ +return H_TOO_HARD ; + +if (kvmppc_gpa_to_ua (kvm, gpa, &ua,NULL )) +return H_PARAMETER ;
Referred below as [1]
Quoted 90 lines
Quoted 90 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +54
-3+ + mem =mm_iommu_lookup_rm (kvm->mm, ua,1ULL << tbl->it_page_shift); +if (!mem) +return H_TOO_HARD ; + +if (WARN_ON (mm_iommu_ua_to_hpa_rm (mem, ua, &hpa))) +return H_HARDWARE ; + + pua = (void *)vmalloc_to_phys (pua); +if (WARN_ON (!pua)) +return H_HARDWARE ; + +if (WARN_ON (mm_iommu_mapped_inc (mem))) +return H_CLOSED ; + + ret =iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); +if (ret) { +mm_iommu_mapped_dec (mem); +return H_TOO_HARD ; + } + +if (dir !=DMA_NONE ) +kvmppc_rm_tce_iommu_mapped_dec (kvm, tbl, entry); + + *pua = ua; + +return 0 ; +} +long kvmppc_rm_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) {struct kvmppc_spapr_tce_table *stt;long ret; +struct kvmppc_spapr_tce_iommu_table *stit; +unsigned long entry, gpa; +enum dma_data_direction dir;/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ @@ -182,7 +282,25 @@ long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,if (ret !=H_SUCCESS )return ret; -kvmppc_tce_put (stt, ioba >> stt->page_shift, tce); + entry = ioba >> stt->page_shift; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + dir =iommu_tce_direction (tce); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +if (dir ==DMA_NONE ) + ret =kvmppc_rm_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry); +else + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry, gpa, dir); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_rm_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + +kvmppc_tce_put (stt, entry, tce);return H_SUCCESS ; }@@ -220,9 +338,10 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS ; -unsigned long tces, entry, ua =0 ; +unsigned long tces, entry, ua =0 , tce, gpa;unsigned long *rmap =NULL ;bool prereg = false; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -287,12 +406,24 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, }for (i =0 ; i < npages; ++i) { -unsigned long tce =be64_to_cpu (((u64 *)tces)[i]); + tce =be64_to_cpu (((u64 *)tces)[i]); ret =kvmppc_tce_validate (stt, tce);if (ret !=H_SUCCESS ) goto unlock_exit;
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
Quoted 7 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +7
-0+ gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE ))I don't think you need the
WARN()here - the onlyH_HARDWAREfailure path iniommu_map()already includes aWARN().
True, I can drop it here.
Quoted 9 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, · line 424
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+kvmppc_rm_clear_tce (stit->tbl, entry); + else if (ret != H_SUCCESS) + goto unlock_exit;It's also not clear to me why the
H_HARDWAREerror path clears the entry, but the other failure paths don't. Or why anH_HARDWAREwill result in continuing to set the rest of the TCEs, but other failures won't.
The idea was that other failures still have some chance that handling may succeed in virtual mode or via QEMU, H_HARDWARE is fatal.
I am just not sure if H_PARAMETER is what I want to return at [1], to make the calling code simplier, I could return H_HARDWARE there as well (instead of H_PARAMETER).
Quoted 152 lines
Quoted 17 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+ } +kvmppc_tce_put (stt, entry + i, tce); }@@ -309,6 +440,7 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -322,6 +454,20 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,if (tce_value & (TCE_PCI_WRITE |TCE_PCI_READ ))return H_PARAMETER ;arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, · line 465
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +9
-0+list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +unsigned long entry = ioba >> stit->tbl->it_page_shift; + +for (i =0 ; i < npages; ++i) { + ret =kvmppc_rm_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry + i); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_rm_clear_tce (stit->tbl, entry);As noted earlier, I think this WARN belongs within
iommu_unmap()rather than out here.Quoted 120 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +5
-0+else if (ret !=H_SUCCESS ) +return ret; + } + } +for (i =0 ; i < npages; ++i, ioba += (1ULL << stt->page_shift))kvmppc_tce_put (stt, ioba >> stt->page_shift, tce_value);arch/powerpc/kvm/powerpc.c +2
-0@@ -536,6 +536,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)#ifdef CONFIG_PPC_BOOK3S_64 case KVM_CAP_SPAPR_TCE :case KVM_CAP_SPAPR_TCE_64 : +/* fallthrough */ +case KVM_CAP_SPAPR_TCE_VFIO :case KVM_CAP_PPC_RTAS :case KVM_CAP_PPC_FIXUP_HCALL :case KVM_CAP_PPC_ENABLE_HCALL :virt/kvm/vfio.c +60
-0@@ -20,6 +20,10 @@#include <linux/vfio.h> #include "vfio.h" +#ifdef CONFIG_SPAPR_TCE_IOMMU +#include <asm/kvm_ppc.h> +#endif +struct kvm_vfio_group {struct list_head node;struct vfio_group *vfio_group;@@ -211,6 +215,9 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)mutex_unlock (&kv->lock); +#ifdef CONFIG_SPAPR_TCE_IOMMU +kvm_spapr_tce_release_iommu_group (dev->kvm, vfio_group); +#endif kvm_vfio_group_set_kvm (vfio_group,NULL );kvm_vfio_group_put_external_user (vfio_group);@@ -218,6 +225,53 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)kvm_vfio_update_coherency (dev);return ret; + +#ifdef CONFIG_SPAPR_TCE_IOMMU +case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE : { +struct kvm_vfio_spapr_tce param; +unsigned long minsz; +struct kvm_vfio *kv = dev->private; +struct vfio_group *vfio_group; +struct kvm_vfio_group *kvg; +struct fd f; + + minsz = offsetofend(struct kvm_vfio_spapr_tce, tablefd); + +if (copy_from_user (¶m, (void __user *)arg, minsz)) +return -EFAULT ; + +if (param.argsz < minsz || param.flags) +return -EINVAL ; + + f =fdget (param.groupfd); +if (!f.file) +return -EBADF ; + + vfio_group =kvm_vfio_group_get_external_user (f.file); +fdput (f); + +if (IS_ERR (vfio_group)) +return PTR_ERR (vfio_group); + + ret = -ENOENT ; + +mutex_lock (&kv->lock); + +list_for_each_entry (kvg, &kv->group_list, node) { +if (kvg->vfio_group != vfio_group) +continue ; + + ret =kvm_spapr_tce_attach_iommu_group (dev->kvm, + param.tablefd, vfio_group); + +break ; + } + +mutex_unlock (&kv->lock); + +return ret; + } +#endif /* CONFIG_SPAPR_TCE_IOMMU */ }return -ENXIO ;@@ -242,6 +296,9 @@ static int kvm_vfio_has_attr(struct kvm_device *dev,switch (attr->attr) {case KVM_DEV_VFIO_GROUP_ADD :case KVM_DEV_VFIO_GROUP_DEL : +#ifdef CONFIG_SPAPR_TCE_IOMMU +case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE : +#endif return 0 ; }@@ -257,6 +314,9 @@ static void kvm_vfio_destroy(struct kvm_device *dev)struct kvm_vfio_group *kvg, *tmp;list_for_each_entry_safe (kvg, tmp, &kv->group_list, node) { +#ifdef CONFIG_SPAPR_TCE_IOMMU +kvm_spapr_tce_release_iommu_group (dev->kvm, kvg->vfio_group); +#endif kvm_vfio_group_set_kvm (kvg->vfio_group,NULL );kvm_vfio_group_put_external_user (kvg->vfio_group);list_del (&kvg->node);
Signature · 1 line
Alexey
Re: [PATCH kernel v5 10/10] KVM: PPC: VFIO: Add in-kernel acceleration for VFIO
Quoted 491 lines · Alexey Kardashevskiy
Quoted 480 lines · David Gibson
Quoted 46 lines · Alexey Kardashevskiy
This allows the host kernel to handle
H_PUT_TCE,H_PUT_TCE_INDIRECTandH_STUFF_TCErequests targeted an IOMMU TCE table used for VFIO without passing them to user space which saves time on switching to user space and back.This adds H_PUT_TCE/H_PUT_TCE_INDIRECT/H_STUFF_TCE handlers to KVM. KVM tries to handle a TCE request in the real mode, if failed it passes the request to the virtual mode to complete the operation. If it a virtual mode handler fails, the request is passed to the user space; this is not expected to happen though.
To avoid dealing with page use counters (which is tricky in real mode), this only accelerates SPAPR TCE IOMMU v2 clients which are required to pre-register the userspace memory. The very first TCE request will be handled in the VFIO SPAPR TCE driver anyway as the userspace view of the TCE table (
iommu_table::it_userspace) is not allocated till the very first mapping happens and we cannot call vmalloc in real mode.If we fail to update a hardware IOMMU table unexpected reason, we just clear it and move on as there is nothing really we can do about it - for example, if we hot plug a VFIO device to a guest, existing TCE tables will be mirrored automatically to the hardware and there is no interface to report to the guest about possible failures.
This adds new attribute -
KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE- to the VFIO KVM device. It takes a VFIO group fd and SPAPR TCE table fd and associates a physical IOMMU table with the SPAPR TCE table (which is a guest view of the hardware IOMMU table). Theiommu_tableobject is cached and referenced so we do not have to look up for it in real mode.This does not implement the UNSET counterpart as there is no use for it - once the acceleration is enabled, the existing userspace won't disable it unless a VFIO container is destroyed; this adds necessary cleanup to the
KVM_DEV_VFIO_GROUP_DELhandler.As this creates a descriptor per IOMMU table-LIOBN couple (called
kvmppc_spapr_tce_iommu_table), it is possible to have several descriptors with the sameiommu_table(hardware IOMMU table) attached to the same LIOBN; we do not remove duplicates though asiommu_table_ops::exchangenot just update a TCE entry (which is shared among IOMMU groups) but also invalidates the TCE cache (one per IOMMU group).This advertises the new
KVM_CAP_SPAPR_TCE_VFIOcapability to the user space.Quoted 7 lines
This finally makes use of
vfio_external_user_iommu_id()which was introduced quite some time ago and was considered for removal.Tests show that this patch increases transmission speed from 220MB/s to 750..1020MB/s on 10Gb network (Chelsea CXGB3 10Gb ethernet card).
Signed-off-by Alexey Kardashevskiy <[email protected]>I have some comments on this patch, but all the definite ones are pretty minor and could be done as later cleanups.
I have some more serious queries, but they are just queries and requests for clarification. If there are satisfactory answers to them, I'll add my R-b.
---
Quoted 238 lines
Changes:
v5:
* changed error codes in multiple places
* added bunch ofWARN_ON()in places which should not really happen
* adde a check that an iommu table is not attached already to LIOBN
* dropped explicit calls to iommu_tce_clear_param_check/iommu_tce_put_param_checkas kvmppc_tce_validate/kvmppc_ioba_validate call them anyway (since the previous patch)
* if we fail to update a hardware IOMMU table for unexpected reason, this just clears the entryv4:
* added note to the commit log about allowing multiple updates of the same IOMMU table;
* instead of checking for if any memory was preregistered, this returnsH_TOO_HARDif a specific page was not;
* fixed comments from v3 about error handling in many places;
* simplified TCE handlers and merged IOMMU parts inline - for example, there used to bekvmppc_h_put_tce_iommu(), now it is merged intokvmppc_h_put_tce(); this allows to check IOBA boundaries against the first attached table only (makes the code simpler);v3:
* simplified not to use VFIO group notifiers
* reworked cleanup, should be cleaner/simpler nowv2:
* reworked to use new VFIO notifiers
* now sameiommu_tablemay appear in the list several times, to be fixed later--- Documentation/virtual/kvm/devices/vfio.txt | 22 ++- arch/powerpc/include/asm/kvm_host.h | 8 + arch/powerpc/include/asm/kvm_ppc.h | 4 + include/uapi/linux/kvm.h | 8 + arch/powerpc/kvm/book3s_64_vio.c | 307 ++++++++++++++++++++++++++++- arch/powerpc/kvm/book3s_64_vio_hv.c | 152 +++++++++++++- arch/powerpc/kvm/powerpc.c | 2 + virt/kvm/vfio.c | 60 ++++++ 8 files changed, 555 insertions(+), 8 deletions(-)Documentation/virtual/kvm/devices/vfio.txt +20
-2@@ -16,7 +16,25 @@ Groups: KVM_DEV_VFIO_GROUP attributes: KVM_DEV_VFIO_GROUP_ADD: Add a VFIO group to VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. KVM_DEV_VFIO_GROUP_DEL: Remove a VFIO group from VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. + KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: attaches a guest visible TCE table + allocated by sPAPR KVM. + kvm_device_attr.addr points to a struct: -For each, kvm_device_attr.addr points to an int32_t file descriptor -for the VFIO group. + struct kvm_vfio_spapr_tce { + __u32 argsz; + __u32 flags; + __s32 groupfd; + __s32 tablefd; + }; + + where + @argsz is the size of kvm_vfio_spapr_tce_liobn; + @flags are not supported now, must be zero; + @groupfd is a file descriptor for a VFIO group; + @tablefd is a file descriptor for a TCE table allocated via + KVM_CREATE_SPAPR_TCE.arch/powerpc/include/asm/kvm_host.h +8
-0@@ -191,6 +191,13 @@ struct kvmppc_pginfo {atomic_t refcnt; }; +struct kvmppc_spapr_tce_iommu_table { +struct rcu_head rcu; +struct list_head next; +struct vfio_group *group; +struct iommu_table *tbl; +}; +struct kvmppc_spapr_tce_table {struct list_head list;struct kvm *kvm;@@ -199,6 +206,7 @@ struct kvmppc_spapr_tce_table {u32 page_shift;u64 offset;/* in pages */ u64 size;/* window size in pages */ +struct list_head iommu_tables;struct page *pages[0 ]; };arch/powerpc/include/asm/kvm_ppc.h +4
-0@@ -163,6 +163,10 @@ extern long kvmppc_prepare_vrma(struct kvm *kvm,extern void kvmppc_map_vrma (struct kvm_vcpu *vcpu,struct kvm_memory_slot *memslot,unsigned long porder);extern int kvmppc_pseries_do_hcall (struct kvm_vcpu *vcpu); +extern long kvm_spapr_tce_attach_iommu_group (struct kvm *kvm,int tablefd, +struct vfio_group *group); +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group);extern long kvm_vm_ioctl_create_spapr_tce (struct kvm *kvm,struct kvm_create_spapr_tce_64 *args);include/uapi/linux/kvm.h +8
-0@@ -1076,6 +1076,7 @@ struct kvm_device_attr {#define KVM_DEV_VFIO_GROUP 1#define KVM_DEV_VFIO_GROUP_ADD 1#define KVM_DEV_VFIO_GROUP_DEL 2 +#define KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE 3enum kvm_device_type {KVM_DEV_TYPE_FSL_MPIC_20 =1 ,@@ -1097,6 +1098,13 @@ enum kvm_device_type { KVM_DEV_TYPE_MAX, }; +struct kvm_vfio_spapr_tce { +__u32 argsz; +__u32 flags; +__s32 groupfd; +__s32 tablefd; +}; +/* * ioctls for VM fds */ arch/powerpc/kvm/book3s_64_vio.c +77
-0@@ -27,6 +27,10 @@#include <linux/hugetlb.h> #include <linux/list.h> #include <linux/anon_inodes.h> +#include <linux/iommu.h> +#include <linux/file.h> +#include <linux/vfio.h> +#include <linux/module.h> #include <asm/tlbflush.h> #include <asm/kvm_ppc.h> @@ -39,6 +43,36 @@#include <asm/udbg.h> #include <asm/iommu.h> #include <asm/tce.h> +#include <asm/mmu_context.h> + +static void kvm_vfio_group_put_external_user (struct vfio_group *vfio_group) +{ +void (*fn)(struct vfio_group *); + + fn =symbol_get (vfio_group_put_external_user); +if (WARN_ON (!fn)) +return ; + +fn (vfio_group); + +symbol_put (vfio_group_put_external_user); +} + +static int kvm_vfio_external_user_iommu_id (struct vfio_group *vfio_group) +{ +int (*fn)(struct vfio_group *); +int ret =-1 ; + + fn =symbol_get (vfio_external_user_iommu_id); +if (!fn) +return ret; + + ret =fn (vfio_group); + +symbol_put (vfio_external_user_iommu_id); + +return ret; +}static unsigned long kvmppc_tce_pages (unsigned long iommu_pages) {@@ -90,6 +124,130 @@ static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)return ret; } +static void kvm_spapr_tce_iommu_table_free (struct rcu_head *head) +{ +struct kvmppc_spapr_tce_iommu_table *stit =container_of (head, + struct kvmppc_spapr_tce_iommu_table, rcu); + +iommu_table_put (stit->tbl); +kvm_vfio_group_put_external_user (stit->group); + +kfree (stit); +} + +static void kvm_spapr_tce_liobn_release_iommu_group ( +struct kvmppc_spapr_tce_table *stt, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_iommu_table *stit, *tmp; + +list_for_each_entry_safe (stit, tmp, &stt->iommu_tables, next) { +if (group && (stit->group != group)) +continue ; + +list_del_rcu (&stit->next); + +call_rcu (&stit->rcu, kvm_spapr_tce_iommu_table_free); + } +} + +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_table *stt; + +list_for_each_entry_rcu (stt, &kvm->arch.spapr_tce_tables, list) +kvm_spapr_tce_liobn_release_iommu_group (stt, group); +} + +extern long kvm_spapr_tce_attach_iommu_group (struct kvm *kvm,int tablefd, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_table *stt =NULL ; +bool found = false; +struct iommu_table *tbl =NULL ; +struct iommu_table_group *table_group;arch/powerpc/kvm/book3s_64_vio.c static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc) · line 179
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio.c +10
-0+long i, ret =0 ; +struct kvmppc_spapr_tce_iommu_table *stit; +struct fd f; +int group_id; +struct iommu_group *grp; + + group_id =kvm_vfio_external_user_iommu_id (group); + grp =iommu_group_get_by_id (group_id); +if (WARN_ON (!grp)) +return -EIO ;I think it would be nicer to have a function that goes directly from
vfio_grouptoiommu_grouprather than going via id. That can be a later cleanup, though.Quoted 97 lines
arch/powerpc/kvm/book3s_64_vio.c +76
-0+ + f =fdget (tablefd); +if (!f.file) { + ret = -EBADF ; + goto put_exit; + } + +list_for_each_entry_rcu (stt, &kvm->arch.spapr_tce_tables, list) { +if (stt == f.file->private_data) { + found = true; +break ; + } + } + +fdput (f); + +if (!found) { + ret = -EINVAL ; + goto put_exit; + } + + table_group =iommu_group_get_iommudata (grp); +if (WARN_ON (!table_group)) { + ret = -EFAULT ; + goto put_exit; + } + +for (i =0 ; i <IOMMU_TABLE_GROUP_MAX_TABLES ; ++i) { +struct iommu_table *tbltmp = table_group->tables[i]; + +if (!tbltmp) +continue ; + +/* +* Make sure hardware table parameters are exactly the same; +* this is used in the TCE handlers where boundary checks +* use only the first attached table. +*/ +if ((tbltmp->it_page_shift == stt->page_shift) && + (tbltmp->it_offset == stt->offset) && + (tbltmp->it_size == stt->size)) { + tbl = tbltmp; +break ; + } + } +if (!tbl) { + ret = -EINVAL ; + goto put_exit; + } + +list_for_each_entry_rcu (stit, &stt->iommu_tables, next) { +if ((stit->tbl == tbl) && (stit->group == group)) { + ret = -EBUSY ; + goto put_exit; + } + } + +iommu_table_get (tbl); + + stit =kzalloc (sizeof (*stit),GFP_KERNEL ); + stit->tbl = tbl; + stit->group = group; + +list_add_rcu (&stit->next, &stt->iommu_tables); + +put_exit: +iommu_group_put (grp); + +return ret; +} +static void release_spapr_tce_table (struct rcu_head *head) { struct kvmppc_spapr_tce_table *stt = container_of(head,@@ -132,6 +290,8 @@ static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)list_del_rcu (&stt->list); +kvm_spapr_tce_liobn_release_iommu_group (stt,NULL /* release all */ ); +kvm_put_kvm (stt->kvm); kvmppc_account_memlimit(@@ -181,6 +341,7 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, stt->offset = args->offset; stt->size = size; stt->kvm = kvm; +INIT_LIST_HEAD_RCU (&stt->iommu_tables);for (i =0 ; i < npages; i++) { stt->pages[i] =alloc_page (GFP_KERNEL | __GFP_ZERO);@@ -209,11 +370,102 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,return ret; } +static void kvmppc_clear_tce (struct iommu_table *tbl,unsigned long entry) +{arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · line 384
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio.c +10
-0+unsigned long hpa =0 ; +enum dma_data_direction dir =DMA_NONE ; + +iommu_tce_xchg (tbl, entry, &hpa, &dir); +} + +static long kvmppc_tce_iommu_mapped_dec (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +struct mm_iommu_table_group_mem_t *mem =NULL ;Initialization is unnecessary for the variable above.
Quoted 53 lines
arch/powerpc/kvm/book3s_64_vio.c +53
-0+const unsigned long pgsize =1ULL << tbl->it_page_shift; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); + +if (WARN_ON (!pua)) +return H_HARDWARE ; + + mem =mm_iommu_lookup (kvm->mm, *pua, pgsize); +if (!mem) +return H_TOO_HARD ; + +mm_iommu_mapped_dec (mem); + + *pua =0 ; + +return H_SUCCESS ; +} + +static long kvmppc_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret; + +if (iommu_tce_xchg (tbl, entry, &hpa, &dir)) +return H_HARDWARE ; + +if (dir ==DMA_NONE ) +return H_SUCCESS ; + + ret =kvmppc_tce_iommu_mapped_dec (kvm, tbl, entry); +if (ret !=H_SUCCESS ) +iommu_tce_xchg (tbl, entry, &hpa, &dir); + +return ret; +} + +long kvmppc_tce_iommu_map (struct kvm *kvm,struct iommu_table *tbl, +unsigned long entry,unsigned long gpa, +enum dma_data_direction dir) +{ +long ret; +unsigned long hpa, ua, *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); +struct mm_iommu_table_group_mem_t *mem; + +if (!pua) +/* it_userspace allocation might be delayed */ +return H_TOO_HARD ; + +if (kvmppc_gpa_to_ua (kvm, gpa, &ua,NULL )) +return H_PARAMETER ; + + mem =mm_iommu_lookup (kvm->mm, ua,1ULL << tbl->it_page_shift);Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · line 442
Quoted 5 lines
arch/powerpc/kvm/book3s_64_vio.c +5
-0+if (!mem) +return H_TOO_HARD ; + +if (WARN_ON (mm_iommu_ua_to_hpa (mem, ua, &hpa))) +return H_HARDWARE ;Remind me - could a failure here be triggered by userspace failing to preregister all the memory it should?
Quoted 3 lines
mm_iommu_lookup()should fail first as the only reason formm_iommu_ua_to_hpa()to fail is (entry >=mem->entries) but this is checked inmm_iommu_lookup().
Ah, yes of course, I forgot. Ok, that should be fine.
Quoted 177 lines
Quoted 167 lines
Quoted 167 lines
arch/powerpc/kvm/book3s_64_vio.c +72
-3+if (mm_iommu_mapped_inc (mem)) +return H_CLOSED ; + + ret =iommu_tce_xchg (tbl, entry, &hpa, &dir); +if (ret) { +mm_iommu_mapped_dec (mem); +return H_TOO_HARD ; + } + +if (dir !=DMA_NONE ) +kvmppc_tce_iommu_mapped_dec (kvm, tbl, entry); + + *pua = ua; + +return 0 ; +} +long kvmppc_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) {struct kvmppc_spapr_tce_table *stt; -long ret; +long ret, idx; +struct kvmppc_spapr_tce_iommu_table *stit; +unsigned long entry, gpa; +enum dma_data_direction dir;/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ @@ -230,7 +482,28 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,if (ret !=H_SUCCESS )return ret; -kvmppc_tce_put (stt, ioba >> stt->page_shift, tce); + entry = ioba >> stt->page_shift; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + dir =iommu_tce_direction (tce); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +if (dir ==DMA_NONE ) { + ret =kvmppc_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry); + }else { + idx =srcu_read_lock (&vcpu->kvm->srcu); + ret =kvmppc_tce_iommu_map (vcpu->kvm, stit->tbl, + entry, gpa, dir); +srcu_read_unlock (&vcpu->kvm->srcu, idx); + } + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + +kvmppc_tce_put (stt, entry, tce);return H_SUCCESS ; }@@ -242,9 +515,10 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS , idx; -unsigned long entry, ua =0 ; +unsigned long entry, ua =0 , gpa;u64 __user *tces;u64 tce; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -283,6 +557,18 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,if (ret !=H_SUCCESS ) goto unlock_exit; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) + goto unlock_exit; + } +kvmppc_tce_put (stt, entry + i, tce); }@@ -299,6 +585,7 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -312,6 +599,20 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,if (tce_value & (TCE_PCI_WRITE |TCE_PCI_READ ))return H_PARAMETER ; +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +unsigned long entry = ioba >> stit->tbl->it_page_shift; + +for (i =0 ; i < npages; ++i) { + ret =kvmppc_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry + i); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + } +for (i =0 ; i < npages; ++i, ioba += (1ULL << stt->page_shift))kvmppc_tce_put (stt, ioba >> stt->page_shift, tce_value);arch/powerpc/kvm/book3s_64_vio_hv.c +39
-0@@ -161,11 +161,111 @@ long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,EXPORT_SYMBOL_GPL (kvmppc_gpa_to_ua);#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE +static void kvmppc_rm_clear_tce (struct iommu_table *tbl,unsigned long entry) +{ +unsigned long hpa =0 ; +enum dma_data_direction dir =DMA_NONE ; + +iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); +} + +static long kvmppc_rm_tce_iommu_mapped_dec (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +struct mm_iommu_table_group_mem_t *mem =NULL ; +const unsigned long pgsize =1ULL << tbl->it_page_shift; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); + +if (WARN_ON (!pua)) +return H_HARDWARE ; + + pua = (void *)vmalloc_to_phys (pua); +if (!pua) +return H_TOO_HARD ; + + mem =mm_iommu_lookup_rm (kvm->mm, *pua, pgsize); +if (!mem) +return H_TOO_HARD ; + +mm_iommu_mapped_dec (mem); + + *pua =0 ; + +return H_SUCCESS ; +} + +static long kvmppc_rm_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret;Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, · line 205
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+ +if (iommu_tce_xchg_rm (tbl, entry, &hpa, &dir)) +return H_HARDWARE ;To avoid a double
WARN()(and to make the warnings easier to understand) I'd suggest putting aWARN_ON()here, rather than in the callers when they receieve anH_HARDWARE. IIUC this really shouldn't ever happen, and it certainly can't be the guest's fault?
Quoted 1 line
Makes sense.
I guess it might want WARN_ON_ONCE() to avoid spamming the user with errors for every TCE, though.
Quoted 147 lines
Quoted 18 lines
Quoted 18 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +18
-0+ +if (dir ==DMA_NONE ) +return H_SUCCESS ; + + ret =kvmppc_rm_tce_iommu_mapped_dec (kvm, tbl, entry); +if (ret) +iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); + +return ret; +} + +static long kvmppc_rm_tce_iommu_map (struct kvm *kvm,struct iommu_table *tbl, +unsigned long entry,unsigned long gpa, +enum dma_data_direction dir) +{ +long ret; +unsigned long hpa =0 , ua; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry);Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, · line 231
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +8
-0+struct mm_iommu_table_group_mem_t *mem; + +if (!pua) +/* it_userspace allocation might be delayed */ +return H_TOO_HARD ; + +if (kvmppc_gpa_to_ua (kvm, gpa, &ua,NULL )) +return H_PARAMETER ;Referred below as [1]
Quoted 90 lines
Quoted 90 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +54
-3+ + mem =mm_iommu_lookup_rm (kvm->mm, ua,1ULL << tbl->it_page_shift); +if (!mem) +return H_TOO_HARD ; + +if (WARN_ON (mm_iommu_ua_to_hpa_rm (mem, ua, &hpa))) +return H_HARDWARE ; + + pua = (void *)vmalloc_to_phys (pua); +if (WARN_ON (!pua)) +return H_HARDWARE ; + +if (WARN_ON (mm_iommu_mapped_inc (mem))) +return H_CLOSED ; + + ret =iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); +if (ret) { +mm_iommu_mapped_dec (mem); +return H_TOO_HARD ; + } + +if (dir !=DMA_NONE ) +kvmppc_rm_tce_iommu_mapped_dec (kvm, tbl, entry); + + *pua = ua; + +return 0 ; +} +long kvmppc_rm_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) {struct kvmppc_spapr_tce_table *stt;long ret; +struct kvmppc_spapr_tce_iommu_table *stit; +unsigned long entry, gpa; +enum dma_data_direction dir;/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ @@ -182,7 +282,25 @@ long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,if (ret !=H_SUCCESS )return ret; -kvmppc_tce_put (stt, ioba >> stt->page_shift, tce); + entry = ioba >> stt->page_shift; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + dir =iommu_tce_direction (tce); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +if (dir ==DMA_NONE ) + ret =kvmppc_rm_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry); +else + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry, gpa, dir); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_rm_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + +kvmppc_tce_put (stt, entry, tce);return H_SUCCESS ; }@@ -220,9 +338,10 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS ; -unsigned long tces, entry, ua =0 ; +unsigned long tces, entry, ua =0 , tce, gpa;unsigned long *rmap =NULL ;bool prereg = false; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -287,12 +406,24 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, }for (i =0 ; i < npages; ++i) { -unsigned long tce =be64_to_cpu (((u64 *)tces)[i]); + tce =be64_to_cpu (((u64 *)tces)[i]); ret =kvmppc_tce_validate (stt, tce);if (ret !=H_SUCCESS ) goto unlock_exit;Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
Quoted 7 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +7
-0+ gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE ))I don't think you need the
WARN()here - the onlyH_HARDWAREfailure path iniommu_map()already includes aWARN().True, I can drop it here.
Quoted 9 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, · line 424
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+kvmppc_rm_clear_tce (stit->tbl, entry); + else if (ret != H_SUCCESS) + goto unlock_exit;It's also not clear to me why the
H_HARDWAREerror path clears the entry, but the other failure paths don't. Or why anH_HARDWAREwill result in continuing to set the rest of the TCEs, but other failures won't.
Quoted 2 lines
The idea was that other failures still have some chance that handling may succeed in virtual mode or via QEMU,
H_HARDWAREis fatal.
Um... yes.. but the logic seems to be backwards for that: on H_HARDWARE you warn and keep going, on other errors you bail out entirely.
Quoted 3 lines
I am just not sure if
H_PARAMETERis what I want to return at [1], to make the calling code simplier, I could returnH_HARDWAREthere as well (instead ofH_PARAMETER).
That sounds right, IIUC the gpa to ua translation shouldn't ever fail because of something the guest did. So I'd expect either H_HARDWARE, or H_TOO_HARD (if there's some hope that virtual mode can make the translation when real mode couldn't).
Quoted 153 lines
Quoted 151 lines
Quoted 16 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+ } +kvmppc_tce_put (stt, entry + i, tce); }@@ -309,6 +440,7 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -322,6 +454,20 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,if (tce_value & (TCE_PCI_WRITE |TCE_PCI_READ ))return H_PARAMETER ;arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, · line 465
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +9
-0+list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +unsigned long entry = ioba >> stit->tbl->it_page_shift; + +for (i =0 ; i < npages; ++i) { + ret =kvmppc_rm_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry + i); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_rm_clear_tce (stit->tbl, entry);As noted earlier, I think this WARN belongs within
iommu_unmap()rather than out here.Quoted 120 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +5
-0+else if (ret !=H_SUCCESS ) +return ret; + } + } +for (i =0 ; i < npages; ++i, ioba += (1ULL << stt->page_shift))kvmppc_tce_put (stt, ioba >> stt->page_shift, tce_value);arch/powerpc/kvm/powerpc.c +2
-0@@ -536,6 +536,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)#ifdef CONFIG_PPC_BOOK3S_64 case KVM_CAP_SPAPR_TCE :case KVM_CAP_SPAPR_TCE_64 : +/* fallthrough */ +case KVM_CAP_SPAPR_TCE_VFIO :case KVM_CAP_PPC_RTAS :case KVM_CAP_PPC_FIXUP_HCALL :case KVM_CAP_PPC_ENABLE_HCALL :virt/kvm/vfio.c +60
-0@@ -20,6 +20,10 @@#include <linux/vfio.h> #include "vfio.h" +#ifdef CONFIG_SPAPR_TCE_IOMMU +#include <asm/kvm_ppc.h> +#endif +struct kvm_vfio_group {struct list_head node;struct vfio_group *vfio_group;@@ -211,6 +215,9 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)mutex_unlock (&kv->lock); +#ifdef CONFIG_SPAPR_TCE_IOMMU +kvm_spapr_tce_release_iommu_group (dev->kvm, vfio_group); +#endif kvm_vfio_group_set_kvm (vfio_group,NULL );kvm_vfio_group_put_external_user (vfio_group);@@ -218,6 +225,53 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)kvm_vfio_update_coherency (dev);return ret; + +#ifdef CONFIG_SPAPR_TCE_IOMMU +case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE : { +struct kvm_vfio_spapr_tce param; +unsigned long minsz; +struct kvm_vfio *kv = dev->private; +struct vfio_group *vfio_group; +struct kvm_vfio_group *kvg; +struct fd f; + + minsz = offsetofend(struct kvm_vfio_spapr_tce, tablefd); + +if (copy_from_user (¶m, (void __user *)arg, minsz)) +return -EFAULT ; + +if (param.argsz < minsz || param.flags) +return -EINVAL ; + + f =fdget (param.groupfd); +if (!f.file) +return -EBADF ; + + vfio_group =kvm_vfio_group_get_external_user (f.file); +fdput (f); + +if (IS_ERR (vfio_group)) +return PTR_ERR (vfio_group); + + ret = -ENOENT ; + +mutex_lock (&kv->lock); + +list_for_each_entry (kvg, &kv->group_list, node) { +if (kvg->vfio_group != vfio_group) +continue ; + + ret =kvm_spapr_tce_attach_iommu_group (dev->kvm, + param.tablefd, vfio_group); + +break ; + } + +mutex_unlock (&kv->lock); + +return ret; + } +#endif /* CONFIG_SPAPR_TCE_IOMMU */ }return -ENXIO ;@@ -242,6 +296,9 @@ static int kvm_vfio_has_attr(struct kvm_device *dev,switch (attr->attr) {case KVM_DEV_VFIO_GROUP_ADD :case KVM_DEV_VFIO_GROUP_DEL : +#ifdef CONFIG_SPAPR_TCE_IOMMU +case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE : +#endif return 0 ; }@@ -257,6 +314,9 @@ static void kvm_vfio_destroy(struct kvm_device *dev)struct kvm_vfio_group *kvg, *tmp;list_for_each_entry_safe (kvg, tmp, &kv->group_list, node) { +#ifdef CONFIG_SPAPR_TCE_IOMMU +kvm_spapr_tce_release_iommu_group (dev->kvm, kvg->vfio_group); +#endif kvm_vfio_group_set_kvm (kvg->vfio_group,NULL );kvm_vfio_group_put_external_user (kvg->vfio_group);list_del (&kvg->node);
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH kernel v5 10/10] KVM: PPC: VFIO: Add in-kernel acceleration for VFIO
Quoted 676 lines · David Gibson
Quoted 491 lines · Alexey Kardashevskiy
Quoted 480 lines · David Gibson
Quoted 46 lines · Alexey Kardashevskiy
This allows the host kernel to handle
H_PUT_TCE,H_PUT_TCE_INDIRECTandH_STUFF_TCErequests targeted an IOMMU TCE table used for VFIO without passing them to user space which saves time on switching to user space and back.This adds H_PUT_TCE/H_PUT_TCE_INDIRECT/H_STUFF_TCE handlers to KVM. KVM tries to handle a TCE request in the real mode, if failed it passes the request to the virtual mode to complete the operation. If it a virtual mode handler fails, the request is passed to the user space; this is not expected to happen though.
To avoid dealing with page use counters (which is tricky in real mode), this only accelerates SPAPR TCE IOMMU v2 clients which are required to pre-register the userspace memory. The very first TCE request will be handled in the VFIO SPAPR TCE driver anyway as the userspace view of the TCE table (
iommu_table::it_userspace) is not allocated till the very first mapping happens and we cannot call vmalloc in real mode.If we fail to update a hardware IOMMU table unexpected reason, we just clear it and move on as there is nothing really we can do about it - for example, if we hot plug a VFIO device to a guest, existing TCE tables will be mirrored automatically to the hardware and there is no interface to report to the guest about possible failures.
This adds new attribute -
KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE- to the VFIO KVM device. It takes a VFIO group fd and SPAPR TCE table fd and associates a physical IOMMU table with the SPAPR TCE table (which is a guest view of the hardware IOMMU table). Theiommu_tableobject is cached and referenced so we do not have to look up for it in real mode.This does not implement the UNSET counterpart as there is no use for it - once the acceleration is enabled, the existing userspace won't disable it unless a VFIO container is destroyed; this adds necessary cleanup to the
KVM_DEV_VFIO_GROUP_DELhandler.As this creates a descriptor per IOMMU table-LIOBN couple (called
kvmppc_spapr_tce_iommu_table), it is possible to have several descriptors with the sameiommu_table(hardware IOMMU table) attached to the same LIOBN; we do not remove duplicates though asiommu_table_ops::exchangenot just update a TCE entry (which is shared among IOMMU groups) but also invalidates the TCE cache (one per IOMMU group).This advertises the new
KVM_CAP_SPAPR_TCE_VFIOcapability to the user space.Quoted 7 lines
This finally makes use of
vfio_external_user_iommu_id()which was introduced quite some time ago and was considered for removal.Tests show that this patch increases transmission speed from 220MB/s to 750..1020MB/s on 10Gb network (Chelsea CXGB3 10Gb ethernet card).
Signed-off-by Alexey Kardashevskiy <[email protected]>I have some comments on this patch, but all the definite ones are pretty minor and could be done as later cleanups.
I have some more serious queries, but they are just queries and requests for clarification. If there are satisfactory answers to them, I'll add my R-b.
---
Quoted 238 lines
Changes:
v5:
* changed error codes in multiple places
* added bunch ofWARN_ON()in places which should not really happen
* adde a check that an iommu table is not attached already to LIOBN
* dropped explicit calls to iommu_tce_clear_param_check/iommu_tce_put_param_checkas kvmppc_tce_validate/kvmppc_ioba_validate call them anyway (since the previous patch)
* if we fail to update a hardware IOMMU table for unexpected reason, this just clears the entryv4:
* added note to the commit log about allowing multiple updates of the same IOMMU table;
* instead of checking for if any memory was preregistered, this returnsH_TOO_HARDif a specific page was not;
* fixed comments from v3 about error handling in many places;
* simplified TCE handlers and merged IOMMU parts inline - for example, there used to bekvmppc_h_put_tce_iommu(), now it is merged intokvmppc_h_put_tce(); this allows to check IOBA boundaries against the first attached table only (makes the code simpler);v3:
* simplified not to use VFIO group notifiers
* reworked cleanup, should be cleaner/simpler nowv2:
* reworked to use new VFIO notifiers
* now sameiommu_tablemay appear in the list several times, to be fixed later--- Documentation/virtual/kvm/devices/vfio.txt | 22 ++- arch/powerpc/include/asm/kvm_host.h | 8 + arch/powerpc/include/asm/kvm_ppc.h | 4 + include/uapi/linux/kvm.h | 8 + arch/powerpc/kvm/book3s_64_vio.c | 307 ++++++++++++++++++++++++++++- arch/powerpc/kvm/book3s_64_vio_hv.c | 152 +++++++++++++- arch/powerpc/kvm/powerpc.c | 2 + virt/kvm/vfio.c | 60 ++++++ 8 files changed, 555 insertions(+), 8 deletions(-)Documentation/virtual/kvm/devices/vfio.txt +20
-2@@ -16,7 +16,25 @@ Groups: KVM_DEV_VFIO_GROUP attributes: KVM_DEV_VFIO_GROUP_ADD: Add a VFIO group to VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. KVM_DEV_VFIO_GROUP_DEL: Remove a VFIO group from VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. + KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: attaches a guest visible TCE table + allocated by sPAPR KVM. + kvm_device_attr.addr points to a struct: -For each, kvm_device_attr.addr points to an int32_t file descriptor -for the VFIO group. + struct kvm_vfio_spapr_tce { + __u32 argsz; + __u32 flags; + __s32 groupfd; + __s32 tablefd; + }; + + where + @argsz is the size of kvm_vfio_spapr_tce_liobn; + @flags are not supported now, must be zero; + @groupfd is a file descriptor for a VFIO group; + @tablefd is a file descriptor for a TCE table allocated via + KVM_CREATE_SPAPR_TCE.arch/powerpc/include/asm/kvm_host.h +8
-0@@ -191,6 +191,13 @@ struct kvmppc_pginfo {atomic_t refcnt; }; +struct kvmppc_spapr_tce_iommu_table { +struct rcu_head rcu; +struct list_head next; +struct vfio_group *group; +struct iommu_table *tbl; +}; +struct kvmppc_spapr_tce_table {struct list_head list;struct kvm *kvm;@@ -199,6 +206,7 @@ struct kvmppc_spapr_tce_table {u32 page_shift;u64 offset;/* in pages */ u64 size;/* window size in pages */ +struct list_head iommu_tables;struct page *pages[0 ]; };arch/powerpc/include/asm/kvm_ppc.h +4
-0@@ -163,6 +163,10 @@ extern long kvmppc_prepare_vrma(struct kvm *kvm,extern void kvmppc_map_vrma (struct kvm_vcpu *vcpu,struct kvm_memory_slot *memslot,unsigned long porder);extern int kvmppc_pseries_do_hcall (struct kvm_vcpu *vcpu); +extern long kvm_spapr_tce_attach_iommu_group (struct kvm *kvm,int tablefd, +struct vfio_group *group); +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group);extern long kvm_vm_ioctl_create_spapr_tce (struct kvm *kvm,struct kvm_create_spapr_tce_64 *args);include/uapi/linux/kvm.h +8
-0@@ -1076,6 +1076,7 @@ struct kvm_device_attr {#define KVM_DEV_VFIO_GROUP 1#define KVM_DEV_VFIO_GROUP_ADD 1#define KVM_DEV_VFIO_GROUP_DEL 2 +#define KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE 3enum kvm_device_type {KVM_DEV_TYPE_FSL_MPIC_20 =1 ,@@ -1097,6 +1098,13 @@ enum kvm_device_type { KVM_DEV_TYPE_MAX, }; +struct kvm_vfio_spapr_tce { +__u32 argsz; +__u32 flags; +__s32 groupfd; +__s32 tablefd; +}; +/* * ioctls for VM fds */ arch/powerpc/kvm/book3s_64_vio.c +77
-0@@ -27,6 +27,10 @@#include <linux/hugetlb.h> #include <linux/list.h> #include <linux/anon_inodes.h> +#include <linux/iommu.h> +#include <linux/file.h> +#include <linux/vfio.h> +#include <linux/module.h> #include <asm/tlbflush.h> #include <asm/kvm_ppc.h> @@ -39,6 +43,36 @@#include <asm/udbg.h> #include <asm/iommu.h> #include <asm/tce.h> +#include <asm/mmu_context.h> + +static void kvm_vfio_group_put_external_user (struct vfio_group *vfio_group) +{ +void (*fn)(struct vfio_group *); + + fn =symbol_get (vfio_group_put_external_user); +if (WARN_ON (!fn)) +return ; + +fn (vfio_group); + +symbol_put (vfio_group_put_external_user); +} + +static int kvm_vfio_external_user_iommu_id (struct vfio_group *vfio_group) +{ +int (*fn)(struct vfio_group *); +int ret =-1 ; + + fn =symbol_get (vfio_external_user_iommu_id); +if (!fn) +return ret; + + ret =fn (vfio_group); + +symbol_put (vfio_external_user_iommu_id); + +return ret; +}static unsigned long kvmppc_tce_pages (unsigned long iommu_pages) {@@ -90,6 +124,130 @@ static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)return ret; } +static void kvm_spapr_tce_iommu_table_free (struct rcu_head *head) +{ +struct kvmppc_spapr_tce_iommu_table *stit =container_of (head, + struct kvmppc_spapr_tce_iommu_table, rcu); + +iommu_table_put (stit->tbl); +kvm_vfio_group_put_external_user (stit->group); + +kfree (stit); +} + +static void kvm_spapr_tce_liobn_release_iommu_group ( +struct kvmppc_spapr_tce_table *stt, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_iommu_table *stit, *tmp; + +list_for_each_entry_safe (stit, tmp, &stt->iommu_tables, next) { +if (group && (stit->group != group)) +continue ; + +list_del_rcu (&stit->next); + +call_rcu (&stit->rcu, kvm_spapr_tce_iommu_table_free); + } +} + +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_table *stt; + +list_for_each_entry_rcu (stt, &kvm->arch.spapr_tce_tables, list) +kvm_spapr_tce_liobn_release_iommu_group (stt, group); +} + +extern long kvm_spapr_tce_attach_iommu_group (struct kvm *kvm,int tablefd, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_table *stt =NULL ; +bool found = false; +struct iommu_table *tbl =NULL ; +struct iommu_table_group *table_group;arch/powerpc/kvm/book3s_64_vio.c static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc) · line 179
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio.c +10
-0+long i, ret =0 ; +struct kvmppc_spapr_tce_iommu_table *stit; +struct fd f; +int group_id; +struct iommu_group *grp; + + group_id =kvm_vfio_external_user_iommu_id (group); + grp =iommu_group_get_by_id (group_id); +if (WARN_ON (!grp)) +return -EIO ;I think it would be nicer to have a function that goes directly from
vfio_grouptoiommu_grouprather than going via id. That can be a later cleanup, though.Quoted 97 lines
arch/powerpc/kvm/book3s_64_vio.c +76
-0+ + f =fdget (tablefd); +if (!f.file) { + ret = -EBADF ; + goto put_exit; + } + +list_for_each_entry_rcu (stt, &kvm->arch.spapr_tce_tables, list) { +if (stt == f.file->private_data) { + found = true; +break ; + } + } + +fdput (f); + +if (!found) { + ret = -EINVAL ; + goto put_exit; + } + + table_group =iommu_group_get_iommudata (grp); +if (WARN_ON (!table_group)) { + ret = -EFAULT ; + goto put_exit; + } + +for (i =0 ; i <IOMMU_TABLE_GROUP_MAX_TABLES ; ++i) { +struct iommu_table *tbltmp = table_group->tables[i]; + +if (!tbltmp) +continue ; + +/* +* Make sure hardware table parameters are exactly the same; +* this is used in the TCE handlers where boundary checks +* use only the first attached table. +*/ +if ((tbltmp->it_page_shift == stt->page_shift) && + (tbltmp->it_offset == stt->offset) && + (tbltmp->it_size == stt->size)) { + tbl = tbltmp; +break ; + } + } +if (!tbl) { + ret = -EINVAL ; + goto put_exit; + } + +list_for_each_entry_rcu (stit, &stt->iommu_tables, next) { +if ((stit->tbl == tbl) && (stit->group == group)) { + ret = -EBUSY ; + goto put_exit; + } + } + +iommu_table_get (tbl); + + stit =kzalloc (sizeof (*stit),GFP_KERNEL ); + stit->tbl = tbl; + stit->group = group; + +list_add_rcu (&stit->next, &stt->iommu_tables); + +put_exit: +iommu_group_put (grp); + +return ret; +} +static void release_spapr_tce_table (struct rcu_head *head) { struct kvmppc_spapr_tce_table *stt = container_of(head,@@ -132,6 +290,8 @@ static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)list_del_rcu (&stt->list); +kvm_spapr_tce_liobn_release_iommu_group (stt,NULL /* release all */ ); +kvm_put_kvm (stt->kvm); kvmppc_account_memlimit(@@ -181,6 +341,7 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, stt->offset = args->offset; stt->size = size; stt->kvm = kvm; +INIT_LIST_HEAD_RCU (&stt->iommu_tables);for (i =0 ; i < npages; i++) { stt->pages[i] =alloc_page (GFP_KERNEL | __GFP_ZERO);@@ -209,11 +370,102 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,return ret; } +static void kvmppc_clear_tce (struct iommu_table *tbl,unsigned long entry) +{arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · line 384
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio.c +10
-0+unsigned long hpa =0 ; +enum dma_data_direction dir =DMA_NONE ; + +iommu_tce_xchg (tbl, entry, &hpa, &dir); +} + +static long kvmppc_tce_iommu_mapped_dec (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +struct mm_iommu_table_group_mem_t *mem =NULL ;Initialization is unnecessary for the variable above.
Quoted 53 lines
arch/powerpc/kvm/book3s_64_vio.c +53
-0+const unsigned long pgsize =1ULL << tbl->it_page_shift; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); + +if (WARN_ON (!pua)) +return H_HARDWARE ; + + mem =mm_iommu_lookup (kvm->mm, *pua, pgsize); +if (!mem) +return H_TOO_HARD ; + +mm_iommu_mapped_dec (mem); + + *pua =0 ; + +return H_SUCCESS ; +} + +static long kvmppc_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret; + +if (iommu_tce_xchg (tbl, entry, &hpa, &dir)) +return H_HARDWARE ; + +if (dir ==DMA_NONE ) +return H_SUCCESS ; + + ret =kvmppc_tce_iommu_mapped_dec (kvm, tbl, entry); +if (ret !=H_SUCCESS ) +iommu_tce_xchg (tbl, entry, &hpa, &dir); + +return ret; +} + +long kvmppc_tce_iommu_map (struct kvm *kvm,struct iommu_table *tbl, +unsigned long entry,unsigned long gpa, +enum dma_data_direction dir) +{ +long ret; +unsigned long hpa, ua, *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); +struct mm_iommu_table_group_mem_t *mem; + +if (!pua) +/* it_userspace allocation might be delayed */ +return H_TOO_HARD ; + +if (kvmppc_gpa_to_ua (kvm, gpa, &ua,NULL )) +return H_PARAMETER ; + + mem =mm_iommu_lookup (kvm->mm, ua,1ULL << tbl->it_page_shift);Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · line 442
Quoted 5 lines
arch/powerpc/kvm/book3s_64_vio.c +5
-0+if (!mem) +return H_TOO_HARD ; + +if (WARN_ON (mm_iommu_ua_to_hpa (mem, ua, &hpa))) +return H_HARDWARE ;Remind me - could a failure here be triggered by userspace failing to preregister all the memory it should?
Quoted 3 lines
mm_iommu_lookup()should fail first as the only reason formm_iommu_ua_to_hpa()to fail is (entry >=mem->entries) but this is checked inmm_iommu_lookup().Ah, yes of course, I forgot. Ok, that should be fine.
Quoted 177 lines
Quoted 175 lines
Quoted 160 lines
arch/powerpc/kvm/book3s_64_vio.c +72
-3+if (mm_iommu_mapped_inc (mem)) +return H_CLOSED ; + + ret =iommu_tce_xchg (tbl, entry, &hpa, &dir); +if (ret) { +mm_iommu_mapped_dec (mem); +return H_TOO_HARD ; + } + +if (dir !=DMA_NONE ) +kvmppc_tce_iommu_mapped_dec (kvm, tbl, entry); + + *pua = ua; + +return 0 ; +} +long kvmppc_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) {struct kvmppc_spapr_tce_table *stt; -long ret; +long ret, idx; +struct kvmppc_spapr_tce_iommu_table *stit; +unsigned long entry, gpa; +enum dma_data_direction dir;/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ @@ -230,7 +482,28 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,if (ret !=H_SUCCESS )return ret; -kvmppc_tce_put (stt, ioba >> stt->page_shift, tce); + entry = ioba >> stt->page_shift; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + dir =iommu_tce_direction (tce); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +if (dir ==DMA_NONE ) { + ret =kvmppc_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry); + }else { + idx =srcu_read_lock (&vcpu->kvm->srcu); + ret =kvmppc_tce_iommu_map (vcpu->kvm, stit->tbl, + entry, gpa, dir); +srcu_read_unlock (&vcpu->kvm->srcu, idx); + } + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + +kvmppc_tce_put (stt, entry, tce);return H_SUCCESS ; }@@ -242,9 +515,10 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS , idx; -unsigned long entry, ua =0 ; +unsigned long entry, ua =0 , gpa;u64 __user *tces;u64 tce; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -283,6 +557,18 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,if (ret !=H_SUCCESS ) goto unlock_exit; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) + goto unlock_exit; + } +kvmppc_tce_put (stt, entry + i, tce); }@@ -299,6 +585,7 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -312,6 +599,20 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,if (tce_value & (TCE_PCI_WRITE |TCE_PCI_READ ))return H_PARAMETER ; +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +unsigned long entry = ioba >> stit->tbl->it_page_shift; + +for (i =0 ; i < npages; ++i) { + ret =kvmppc_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry + i); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + } +for (i =0 ; i < npages; ++i, ioba += (1ULL << stt->page_shift))kvmppc_tce_put (stt, ioba >> stt->page_shift, tce_value);arch/powerpc/kvm/book3s_64_vio_hv.c +32
-0@@ -161,11 +161,111 @@ long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,EXPORT_SYMBOL_GPL (kvmppc_gpa_to_ua);#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE +static void kvmppc_rm_clear_tce (struct iommu_table *tbl,unsigned long entry) +{ +unsigned long hpa =0 ; +enum dma_data_direction dir =DMA_NONE ; + +iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); +} + +static long kvmppc_rm_tce_iommu_mapped_dec (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +struct mm_iommu_table_group_mem_t *mem =NULL ; +const unsigned long pgsize =1ULL << tbl->it_page_shift; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); + +if (WARN_ON (!pua)) +return H_HARDWARE ; + + pua = (void *)vmalloc_to_phys (pua); +if (!pua) +return H_TOO_HARD ; + + mem =mm_iommu_lookup_rm (kvm->mm, *pua, pgsize); +if (!mem) +return H_TOO_HARD ; + +mm_iommu_mapped_dec (mem); + + *pua =0 ; + +return H_SUCCESS ; +}arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, · line 205
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +10
-0+ +static long kvmppc_rm_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret; + +if (iommu_tce_xchg_rm (tbl, entry, &hpa, &dir)) +return H_HARDWARE ;To avoid a double
WARN()(and to make the warnings easier to understand) I'd suggest putting aWARN_ON()here, rather than in the callers when they receieve anH_HARDWARE. IIUC this really shouldn't ever happen, and it certainly can't be the guest's fault?
Quoted 4 lines
Quoted 1 line
Makes sense.
I guess it might want
WARN_ON_ONCE()to avoid spamming the user with errors for every TCE, though.
We do not expect this to happen at all :) I can convert all of them to _ONCE really as the purpose of WARN_ON is mostly to document what we do not expect.
Quoted 147 lines
Quoted 147 lines
Quoted 18 lines
Quoted 18 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +18
-0+ +if (dir ==DMA_NONE ) +return H_SUCCESS ; + + ret =kvmppc_rm_tce_iommu_mapped_dec (kvm, tbl, entry); +if (ret) +iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); + +return ret; +} + +static long kvmppc_rm_tce_iommu_map (struct kvm *kvm,struct iommu_table *tbl, +unsigned long entry,unsigned long gpa, +enum dma_data_direction dir) +{ +long ret; +unsigned long hpa =0 , ua; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry);Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, · line 231
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +8
-0+struct mm_iommu_table_group_mem_t *mem; + +if (!pua) +/* it_userspace allocation might be delayed */ +return H_TOO_HARD ; + +if (kvmppc_gpa_to_ua (kvm, gpa, &ua,NULL )) +return H_PARAMETER ;Referred below as [1]
Quoted 90 lines
Quoted 90 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +54
-3+ + mem =mm_iommu_lookup_rm (kvm->mm, ua,1ULL << tbl->it_page_shift); +if (!mem) +return H_TOO_HARD ; + +if (WARN_ON (mm_iommu_ua_to_hpa_rm (mem, ua, &hpa))) +return H_HARDWARE ; + + pua = (void *)vmalloc_to_phys (pua); +if (WARN_ON (!pua)) +return H_HARDWARE ; + +if (WARN_ON (mm_iommu_mapped_inc (mem))) +return H_CLOSED ; + + ret =iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); +if (ret) { +mm_iommu_mapped_dec (mem); +return H_TOO_HARD ; + } + +if (dir !=DMA_NONE ) +kvmppc_rm_tce_iommu_mapped_dec (kvm, tbl, entry); + + *pua = ua; + +return 0 ; +} +long kvmppc_rm_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) {struct kvmppc_spapr_tce_table *stt;long ret; +struct kvmppc_spapr_tce_iommu_table *stit; +unsigned long entry, gpa; +enum dma_data_direction dir;/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ @@ -182,7 +282,25 @@ long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,if (ret !=H_SUCCESS )return ret; -kvmppc_tce_put (stt, ioba >> stt->page_shift, tce); + entry = ioba >> stt->page_shift; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + dir =iommu_tce_direction (tce); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +if (dir ==DMA_NONE ) + ret =kvmppc_rm_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry); +else + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry, gpa, dir); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_rm_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + +kvmppc_tce_put (stt, entry, tce);return H_SUCCESS ; }@@ -220,9 +338,10 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS ; -unsigned long tces, entry, ua =0 ; +unsigned long tces, entry, ua =0 , tce, gpa;unsigned long *rmap =NULL ;bool prereg = false; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -287,12 +406,24 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, }for (i =0 ; i < npages; ++i) { -unsigned long tce =be64_to_cpu (((u64 *)tces)[i]); + tce =be64_to_cpu (((u64 *)tces)[i]); ret =kvmppc_tce_validate (stt, tce);if (ret !=H_SUCCESS ) goto unlock_exit;Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
Quoted 7 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +7
-0+ gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE ))I don't think you need the
WARN()here - the onlyH_HARDWAREfailure path iniommu_map()already includes aWARN().True, I can drop it here.
Quoted 9 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, · line 424
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+kvmppc_rm_clear_tce (stit->tbl, entry); + else if (ret != H_SUCCESS) + goto unlock_exit;It's also not clear to me why the
H_HARDWAREerror path clears the entry, but the other failure paths don't. Or why anH_HARDWAREwill result in continuing to set the rest of the TCEs, but other failures won't.
Quoted 6 lines
Quoted 2 lines
The idea was that other failures still have some chance that handling may succeed in virtual mode or via QEMU,
H_HARDWAREis fatal.Um... yes.. but the logic seems to be backwards for that: on
H_HARDWAREyou warn and keep going, on other errors you bail out entirely.
By "fatal" I means fatal for this particular hardware TCE(s), no hope in trying this particular TCE in virtual mode.
Quoted 7 lines
Quoted 4 lines
I am just not sure if
H_PARAMETERis what I want to return at [1], to make the calling code simplier, I could returnH_HARDWAREthere as well (instead ofH_PARAMETER).That sounds right, IIUC the gpa to ua translation shouldn't ever fail because of something the guest did.
The guest can easily pass bad TCE/GPA which is not in any registered slot. So it is rather H_PARAMETER.
Quoted 3 lines
So I'd expect either
H_HARDWARE, orH_TOO_HARD(if there's some hope that virtual mode can make the translation when real mode couldn't).
No, virtual mode uses the exact same helper.
Quoted 157 lines
Quoted 153 lines
Quoted 151 lines
Quoted 16 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+ } +kvmppc_tce_put (stt, entry + i, tce); }@@ -309,6 +440,7 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -322,6 +454,20 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,if (tce_value & (TCE_PCI_WRITE |TCE_PCI_READ ))return H_PARAMETER ;arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, · line 465
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +9
-0+list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +unsigned long entry = ioba >> stit->tbl->it_page_shift; + +for (i =0 ; i < npages; ++i) { + ret =kvmppc_rm_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry + i); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_rm_clear_tce (stit->tbl, entry);As noted earlier, I think this WARN belongs within
iommu_unmap()rather than out here.Quoted 120 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +5
-0+else if (ret !=H_SUCCESS ) +return ret; + } + } +for (i =0 ; i < npages; ++i, ioba += (1ULL << stt->page_shift))kvmppc_tce_put (stt, ioba >> stt->page_shift, tce_value);arch/powerpc/kvm/powerpc.c +2
-0@@ -536,6 +536,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)#ifdef CONFIG_PPC_BOOK3S_64 case KVM_CAP_SPAPR_TCE :case KVM_CAP_SPAPR_TCE_64 : +/* fallthrough */ +case KVM_CAP_SPAPR_TCE_VFIO :case KVM_CAP_PPC_RTAS :case KVM_CAP_PPC_FIXUP_HCALL :case KVM_CAP_PPC_ENABLE_HCALL :virt/kvm/vfio.c +60
-0@@ -20,6 +20,10 @@#include <linux/vfio.h> #include "vfio.h" +#ifdef CONFIG_SPAPR_TCE_IOMMU +#include <asm/kvm_ppc.h> +#endif +struct kvm_vfio_group {struct list_head node;struct vfio_group *vfio_group;@@ -211,6 +215,9 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)mutex_unlock (&kv->lock); +#ifdef CONFIG_SPAPR_TCE_IOMMU +kvm_spapr_tce_release_iommu_group (dev->kvm, vfio_group); +#endif kvm_vfio_group_set_kvm (vfio_group,NULL );kvm_vfio_group_put_external_user (vfio_group);@@ -218,6 +225,53 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)kvm_vfio_update_coherency (dev);return ret; + +#ifdef CONFIG_SPAPR_TCE_IOMMU +case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE : { +struct kvm_vfio_spapr_tce param; +unsigned long minsz; +struct kvm_vfio *kv = dev->private; +struct vfio_group *vfio_group; +struct kvm_vfio_group *kvg; +struct fd f; + + minsz = offsetofend(struct kvm_vfio_spapr_tce, tablefd); + +if (copy_from_user (¶m, (void __user *)arg, minsz)) +return -EFAULT ; + +if (param.argsz < minsz || param.flags) +return -EINVAL ; + + f =fdget (param.groupfd); +if (!f.file) +return -EBADF ; + + vfio_group =kvm_vfio_group_get_external_user (f.file); +fdput (f); + +if (IS_ERR (vfio_group)) +return PTR_ERR (vfio_group); + + ret = -ENOENT ; + +mutex_lock (&kv->lock); + +list_for_each_entry (kvg, &kv->group_list, node) { +if (kvg->vfio_group != vfio_group) +continue ; + + ret =kvm_spapr_tce_attach_iommu_group (dev->kvm, + param.tablefd, vfio_group); + +break ; + } + +mutex_unlock (&kv->lock); + +return ret; + } +#endif /* CONFIG_SPAPR_TCE_IOMMU */ }return -ENXIO ;@@ -242,6 +296,9 @@ static int kvm_vfio_has_attr(struct kvm_device *dev,switch (attr->attr) {case KVM_DEV_VFIO_GROUP_ADD :case KVM_DEV_VFIO_GROUP_DEL : +#ifdef CONFIG_SPAPR_TCE_IOMMU +case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE : +#endif return 0 ; }@@ -257,6 +314,9 @@ static void kvm_vfio_destroy(struct kvm_device *dev)struct kvm_vfio_group *kvg, *tmp;list_for_each_entry_safe (kvg, tmp, &kv->group_list, node) { +#ifdef CONFIG_SPAPR_TCE_IOMMU +kvm_spapr_tce_release_iommu_group (dev->kvm, kvg->vfio_group); +#endif kvm_vfio_group_set_kvm (kvg->vfio_group,NULL );kvm_vfio_group_put_external_user (kvg->vfio_group);list_del (&kvg->node);
Signature · 1 line
Alexey
Re: [PATCH kernel v5 10/10] KVM: PPC: VFIO: Add in-kernel acceleration for VFIO
Quoted 856 lines · Alexey Kardashevskiy
Quoted 676 lines · David Gibson
Quoted 491 lines · Alexey Kardashevskiy
Quoted 480 lines · David Gibson
Quoted 46 lines · Alexey Kardashevskiy
This allows the host kernel to handle
H_PUT_TCE,H_PUT_TCE_INDIRECTandH_STUFF_TCErequests targeted an IOMMU TCE table used for VFIO without passing them to user space which saves time on switching to user space and back.This adds H_PUT_TCE/H_PUT_TCE_INDIRECT/H_STUFF_TCE handlers to KVM. KVM tries to handle a TCE request in the real mode, if failed it passes the request to the virtual mode to complete the operation. If it a virtual mode handler fails, the request is passed to the user space; this is not expected to happen though.
To avoid dealing with page use counters (which is tricky in real mode), this only accelerates SPAPR TCE IOMMU v2 clients which are required to pre-register the userspace memory. The very first TCE request will be handled in the VFIO SPAPR TCE driver anyway as the userspace view of the TCE table (
iommu_table::it_userspace) is not allocated till the very first mapping happens and we cannot call vmalloc in real mode.If we fail to update a hardware IOMMU table unexpected reason, we just clear it and move on as there is nothing really we can do about it - for example, if we hot plug a VFIO device to a guest, existing TCE tables will be mirrored automatically to the hardware and there is no interface to report to the guest about possible failures.
This adds new attribute -
KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE- to the VFIO KVM device. It takes a VFIO group fd and SPAPR TCE table fd and associates a physical IOMMU table with the SPAPR TCE table (which is a guest view of the hardware IOMMU table). Theiommu_tableobject is cached and referenced so we do not have to look up for it in real mode.This does not implement the UNSET counterpart as there is no use for it - once the acceleration is enabled, the existing userspace won't disable it unless a VFIO container is destroyed; this adds necessary cleanup to the
KVM_DEV_VFIO_GROUP_DELhandler.As this creates a descriptor per IOMMU table-LIOBN couple (called
kvmppc_spapr_tce_iommu_table), it is possible to have several descriptors with the sameiommu_table(hardware IOMMU table) attached to the same LIOBN; we do not remove duplicates though asiommu_table_ops::exchangenot just update a TCE entry (which is shared among IOMMU groups) but also invalidates the TCE cache (one per IOMMU group).This advertises the new
KVM_CAP_SPAPR_TCE_VFIOcapability to the user space.Quoted 7 lines
This finally makes use of
vfio_external_user_iommu_id()which was introduced quite some time ago and was considered for removal.Tests show that this patch increases transmission speed from 220MB/s to 750..1020MB/s on 10Gb network (Chelsea CXGB3 10Gb ethernet card).
Signed-off-by Alexey Kardashevskiy <[email protected]>I have some comments on this patch, but all the definite ones are pretty minor and could be done as later cleanups.
I have some more serious queries, but they are just queries and requests for clarification. If there are satisfactory answers to them, I'll add my R-b.
---
Quoted 238 lines
Changes:
v5:
* changed error codes in multiple places
* added bunch ofWARN_ON()in places which should not really happen
* adde a check that an iommu table is not attached already to LIOBN
* dropped explicit calls to iommu_tce_clear_param_check/iommu_tce_put_param_checkas kvmppc_tce_validate/kvmppc_ioba_validate call them anyway (since the previous patch)
* if we fail to update a hardware IOMMU table for unexpected reason, this just clears the entryv4:
* added note to the commit log about allowing multiple updates of the same IOMMU table;
* instead of checking for if any memory was preregistered, this returnsH_TOO_HARDif a specific page was not;
* fixed comments from v3 about error handling in many places;
* simplified TCE handlers and merged IOMMU parts inline - for example, there used to bekvmppc_h_put_tce_iommu(), now it is merged intokvmppc_h_put_tce(); this allows to check IOBA boundaries against the first attached table only (makes the code simpler);v3:
* simplified not to use VFIO group notifiers
* reworked cleanup, should be cleaner/simpler nowv2:
* reworked to use new VFIO notifiers
* now sameiommu_tablemay appear in the list several times, to be fixed later--- Documentation/virtual/kvm/devices/vfio.txt | 22 ++- arch/powerpc/include/asm/kvm_host.h | 8 + arch/powerpc/include/asm/kvm_ppc.h | 4 + include/uapi/linux/kvm.h | 8 + arch/powerpc/kvm/book3s_64_vio.c | 307 ++++++++++++++++++++++++++++- arch/powerpc/kvm/book3s_64_vio_hv.c | 152 +++++++++++++- arch/powerpc/kvm/powerpc.c | 2 + virt/kvm/vfio.c | 60 ++++++ 8 files changed, 555 insertions(+), 8 deletions(-)Documentation/virtual/kvm/devices/vfio.txt +20
-2@@ -16,7 +16,25 @@ Groups: KVM_DEV_VFIO_GROUP attributes: KVM_DEV_VFIO_GROUP_ADD: Add a VFIO group to VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. KVM_DEV_VFIO_GROUP_DEL: Remove a VFIO group from VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. + KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: attaches a guest visible TCE table + allocated by sPAPR KVM. + kvm_device_attr.addr points to a struct: -For each, kvm_device_attr.addr points to an int32_t file descriptor -for the VFIO group. + struct kvm_vfio_spapr_tce { + __u32 argsz; + __u32 flags; + __s32 groupfd; + __s32 tablefd; + }; + + where + @argsz is the size of kvm_vfio_spapr_tce_liobn; + @flags are not supported now, must be zero; + @groupfd is a file descriptor for a VFIO group; + @tablefd is a file descriptor for a TCE table allocated via + KVM_CREATE_SPAPR_TCE.arch/powerpc/include/asm/kvm_host.h +8
-0@@ -191,6 +191,13 @@ struct kvmppc_pginfo {atomic_t refcnt; }; +struct kvmppc_spapr_tce_iommu_table { +struct rcu_head rcu; +struct list_head next; +struct vfio_group *group; +struct iommu_table *tbl; +}; +struct kvmppc_spapr_tce_table {struct list_head list;struct kvm *kvm;@@ -199,6 +206,7 @@ struct kvmppc_spapr_tce_table {u32 page_shift;u64 offset;/* in pages */ u64 size;/* window size in pages */ +struct list_head iommu_tables;struct page *pages[0 ]; };arch/powerpc/include/asm/kvm_ppc.h +4
-0@@ -163,6 +163,10 @@ extern long kvmppc_prepare_vrma(struct kvm *kvm,extern void kvmppc_map_vrma (struct kvm_vcpu *vcpu,struct kvm_memory_slot *memslot,unsigned long porder);extern int kvmppc_pseries_do_hcall (struct kvm_vcpu *vcpu); +extern long kvm_spapr_tce_attach_iommu_group (struct kvm *kvm,int tablefd, +struct vfio_group *group); +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group);extern long kvm_vm_ioctl_create_spapr_tce (struct kvm *kvm,struct kvm_create_spapr_tce_64 *args);include/uapi/linux/kvm.h +8
-0@@ -1076,6 +1076,7 @@ struct kvm_device_attr {#define KVM_DEV_VFIO_GROUP 1#define KVM_DEV_VFIO_GROUP_ADD 1#define KVM_DEV_VFIO_GROUP_DEL 2 +#define KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE 3enum kvm_device_type {KVM_DEV_TYPE_FSL_MPIC_20 =1 ,@@ -1097,6 +1098,13 @@ enum kvm_device_type { KVM_DEV_TYPE_MAX, }; +struct kvm_vfio_spapr_tce { +__u32 argsz; +__u32 flags; +__s32 groupfd; +__s32 tablefd; +}; +/* * ioctls for VM fds */ arch/powerpc/kvm/book3s_64_vio.c +77
-0@@ -27,6 +27,10 @@#include <linux/hugetlb.h> #include <linux/list.h> #include <linux/anon_inodes.h> +#include <linux/iommu.h> +#include <linux/file.h> +#include <linux/vfio.h> +#include <linux/module.h> #include <asm/tlbflush.h> #include <asm/kvm_ppc.h> @@ -39,6 +43,36 @@#include <asm/udbg.h> #include <asm/iommu.h> #include <asm/tce.h> +#include <asm/mmu_context.h> + +static void kvm_vfio_group_put_external_user (struct vfio_group *vfio_group) +{ +void (*fn)(struct vfio_group *); + + fn =symbol_get (vfio_group_put_external_user); +if (WARN_ON (!fn)) +return ; + +fn (vfio_group); + +symbol_put (vfio_group_put_external_user); +} + +static int kvm_vfio_external_user_iommu_id (struct vfio_group *vfio_group) +{ +int (*fn)(struct vfio_group *); +int ret =-1 ; + + fn =symbol_get (vfio_external_user_iommu_id); +if (!fn) +return ret; + + ret =fn (vfio_group); + +symbol_put (vfio_external_user_iommu_id); + +return ret; +}static unsigned long kvmppc_tce_pages (unsigned long iommu_pages) {@@ -90,6 +124,130 @@ static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)return ret; } +static void kvm_spapr_tce_iommu_table_free (struct rcu_head *head) +{ +struct kvmppc_spapr_tce_iommu_table *stit =container_of (head, + struct kvmppc_spapr_tce_iommu_table, rcu); + +iommu_table_put (stit->tbl); +kvm_vfio_group_put_external_user (stit->group); + +kfree (stit); +} + +static void kvm_spapr_tce_liobn_release_iommu_group ( +struct kvmppc_spapr_tce_table *stt, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_iommu_table *stit, *tmp; + +list_for_each_entry_safe (stit, tmp, &stt->iommu_tables, next) { +if (group && (stit->group != group)) +continue ; + +list_del_rcu (&stit->next); + +call_rcu (&stit->rcu, kvm_spapr_tce_iommu_table_free); + } +} + +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_table *stt; + +list_for_each_entry_rcu (stt, &kvm->arch.spapr_tce_tables, list) +kvm_spapr_tce_liobn_release_iommu_group (stt, group); +} + +extern long kvm_spapr_tce_attach_iommu_group (struct kvm *kvm,int tablefd, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_table *stt =NULL ; +bool found = false; +struct iommu_table *tbl =NULL ; +struct iommu_table_group *table_group;arch/powerpc/kvm/book3s_64_vio.c static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc) · line 179
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio.c +10
-0+long i, ret =0 ; +struct kvmppc_spapr_tce_iommu_table *stit; +struct fd f; +int group_id; +struct iommu_group *grp; + + group_id =kvm_vfio_external_user_iommu_id (group); + grp =iommu_group_get_by_id (group_id); +if (WARN_ON (!grp)) +return -EIO ;I think it would be nicer to have a function that goes directly from
vfio_grouptoiommu_grouprather than going via id. That can be a later cleanup, though.Quoted 97 lines
arch/powerpc/kvm/book3s_64_vio.c +76
-0+ + f =fdget (tablefd); +if (!f.file) { + ret = -EBADF ; + goto put_exit; + } + +list_for_each_entry_rcu (stt, &kvm->arch.spapr_tce_tables, list) { +if (stt == f.file->private_data) { + found = true; +break ; + } + } + +fdput (f); + +if (!found) { + ret = -EINVAL ; + goto put_exit; + } + + table_group =iommu_group_get_iommudata (grp); +if (WARN_ON (!table_group)) { + ret = -EFAULT ; + goto put_exit; + } + +for (i =0 ; i <IOMMU_TABLE_GROUP_MAX_TABLES ; ++i) { +struct iommu_table *tbltmp = table_group->tables[i]; + +if (!tbltmp) +continue ; + +/* +* Make sure hardware table parameters are exactly the same; +* this is used in the TCE handlers where boundary checks +* use only the first attached table. +*/ +if ((tbltmp->it_page_shift == stt->page_shift) && + (tbltmp->it_offset == stt->offset) && + (tbltmp->it_size == stt->size)) { + tbl = tbltmp; +break ; + } + } +if (!tbl) { + ret = -EINVAL ; + goto put_exit; + } + +list_for_each_entry_rcu (stit, &stt->iommu_tables, next) { +if ((stit->tbl == tbl) && (stit->group == group)) { + ret = -EBUSY ; + goto put_exit; + } + } + +iommu_table_get (tbl); + + stit =kzalloc (sizeof (*stit),GFP_KERNEL ); + stit->tbl = tbl; + stit->group = group; + +list_add_rcu (&stit->next, &stt->iommu_tables); + +put_exit: +iommu_group_put (grp); + +return ret; +} +static void release_spapr_tce_table (struct rcu_head *head) { struct kvmppc_spapr_tce_table *stt = container_of(head,@@ -132,6 +290,8 @@ static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)list_del_rcu (&stt->list); +kvm_spapr_tce_liobn_release_iommu_group (stt,NULL /* release all */ ); +kvm_put_kvm (stt->kvm); kvmppc_account_memlimit(@@ -181,6 +341,7 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, stt->offset = args->offset; stt->size = size; stt->kvm = kvm; +INIT_LIST_HEAD_RCU (&stt->iommu_tables);for (i =0 ; i < npages; i++) { stt->pages[i] =alloc_page (GFP_KERNEL | __GFP_ZERO);@@ -209,11 +370,102 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,return ret; } +static void kvmppc_clear_tce (struct iommu_table *tbl,unsigned long entry) +{arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · line 384
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio.c +10
-0+unsigned long hpa =0 ; +enum dma_data_direction dir =DMA_NONE ; + +iommu_tce_xchg (tbl, entry, &hpa, &dir); +} + +static long kvmppc_tce_iommu_mapped_dec (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +struct mm_iommu_table_group_mem_t *mem =NULL ;Initialization is unnecessary for the variable above.
Quoted 53 lines
arch/powerpc/kvm/book3s_64_vio.c +53
-0+const unsigned long pgsize =1ULL << tbl->it_page_shift; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); + +if (WARN_ON (!pua)) +return H_HARDWARE ; + + mem =mm_iommu_lookup (kvm->mm, *pua, pgsize); +if (!mem) +return H_TOO_HARD ; + +mm_iommu_mapped_dec (mem); + + *pua =0 ; + +return H_SUCCESS ; +} + +static long kvmppc_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret; + +if (iommu_tce_xchg (tbl, entry, &hpa, &dir)) +return H_HARDWARE ; + +if (dir ==DMA_NONE ) +return H_SUCCESS ; + + ret =kvmppc_tce_iommu_mapped_dec (kvm, tbl, entry); +if (ret !=H_SUCCESS ) +iommu_tce_xchg (tbl, entry, &hpa, &dir); + +return ret; +} + +long kvmppc_tce_iommu_map (struct kvm *kvm,struct iommu_table *tbl, +unsigned long entry,unsigned long gpa, +enum dma_data_direction dir) +{ +long ret; +unsigned long hpa, ua, *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); +struct mm_iommu_table_group_mem_t *mem; + +if (!pua) +/* it_userspace allocation might be delayed */ +return H_TOO_HARD ; + +if (kvmppc_gpa_to_ua (kvm, gpa, &ua,NULL )) +return H_PARAMETER ; + + mem =mm_iommu_lookup (kvm->mm, ua,1ULL << tbl->it_page_shift);Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · line 442
Quoted 5 lines
arch/powerpc/kvm/book3s_64_vio.c +5
-0+if (!mem) +return H_TOO_HARD ; + +if (WARN_ON (mm_iommu_ua_to_hpa (mem, ua, &hpa))) +return H_HARDWARE ;Remind me - could a failure here be triggered by userspace failing to preregister all the memory it should?
Quoted 3 lines
mm_iommu_lookup()should fail first as the only reason formm_iommu_ua_to_hpa()to fail is (entry >=mem->entries) but this is checked inmm_iommu_lookup().Ah, yes of course, I forgot. Ok, that should be fine.
Quoted 177 lines
Quoted 175 lines
Quoted 160 lines
arch/powerpc/kvm/book3s_64_vio.c +72
-3+if (mm_iommu_mapped_inc (mem)) +return H_CLOSED ; + + ret =iommu_tce_xchg (tbl, entry, &hpa, &dir); +if (ret) { +mm_iommu_mapped_dec (mem); +return H_TOO_HARD ; + } + +if (dir !=DMA_NONE ) +kvmppc_tce_iommu_mapped_dec (kvm, tbl, entry); + + *pua = ua; + +return 0 ; +} +long kvmppc_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) {struct kvmppc_spapr_tce_table *stt; -long ret; +long ret, idx; +struct kvmppc_spapr_tce_iommu_table *stit; +unsigned long entry, gpa; +enum dma_data_direction dir;/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ @@ -230,7 +482,28 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,if (ret !=H_SUCCESS )return ret; -kvmppc_tce_put (stt, ioba >> stt->page_shift, tce); + entry = ioba >> stt->page_shift; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + dir =iommu_tce_direction (tce); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +if (dir ==DMA_NONE ) { + ret =kvmppc_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry); + }else { + idx =srcu_read_lock (&vcpu->kvm->srcu); + ret =kvmppc_tce_iommu_map (vcpu->kvm, stit->tbl, + entry, gpa, dir); +srcu_read_unlock (&vcpu->kvm->srcu, idx); + } + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + +kvmppc_tce_put (stt, entry, tce);return H_SUCCESS ; }@@ -242,9 +515,10 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS , idx; -unsigned long entry, ua =0 ; +unsigned long entry, ua =0 , gpa;u64 __user *tces;u64 tce; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -283,6 +557,18 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,if (ret !=H_SUCCESS ) goto unlock_exit; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) + goto unlock_exit; + } +kvmppc_tce_put (stt, entry + i, tce); }@@ -299,6 +585,7 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -312,6 +599,20 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,if (tce_value & (TCE_PCI_WRITE |TCE_PCI_READ ))return H_PARAMETER ; +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +unsigned long entry = ioba >> stit->tbl->it_page_shift; + +for (i =0 ; i < npages; ++i) { + ret =kvmppc_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry + i); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + } +for (i =0 ; i < npages; ++i, ioba += (1ULL << stt->page_shift))kvmppc_tce_put (stt, ioba >> stt->page_shift, tce_value);arch/powerpc/kvm/book3s_64_vio_hv.c +32
-0@@ -161,11 +161,111 @@ long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,EXPORT_SYMBOL_GPL (kvmppc_gpa_to_ua);#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE +static void kvmppc_rm_clear_tce (struct iommu_table *tbl,unsigned long entry) +{ +unsigned long hpa =0 ; +enum dma_data_direction dir =DMA_NONE ; + +iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); +} + +static long kvmppc_rm_tce_iommu_mapped_dec (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +struct mm_iommu_table_group_mem_t *mem =NULL ; +const unsigned long pgsize =1ULL << tbl->it_page_shift; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); + +if (WARN_ON (!pua)) +return H_HARDWARE ; + + pua = (void *)vmalloc_to_phys (pua); +if (!pua) +return H_TOO_HARD ; + + mem =mm_iommu_lookup_rm (kvm->mm, *pua, pgsize); +if (!mem) +return H_TOO_HARD ; + +mm_iommu_mapped_dec (mem); + + *pua =0 ; + +return H_SUCCESS ; +}arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, · line 205
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +10
-0+ +static long kvmppc_rm_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret; + +if (iommu_tce_xchg_rm (tbl, entry, &hpa, &dir)) +return H_HARDWARE ;To avoid a double
WARN()(and to make the warnings easier to understand) I'd suggest putting aWARN_ON()here, rather than in the callers when they receieve anH_HARDWARE. IIUC this really shouldn't ever happen, and it certainly can't be the guest's fault?Quoted 4 lines
Quoted 1 line
Makes sense.
I guess it might want
WARN_ON_ONCE()to avoid spamming the user with errors for every TCE, though.We do not expect this to happen at all :) I can convert all of them to
_ONCEreally as the purpose ofWARN_ONis mostly to document what we do not expect.Quoted 147 lines
Quoted 147 lines
Quoted 18 lines
Quoted 18 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +18
-0+ +if (dir ==DMA_NONE ) +return H_SUCCESS ; + + ret =kvmppc_rm_tce_iommu_mapped_dec (kvm, tbl, entry); +if (ret) +iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); + +return ret; +} + +static long kvmppc_rm_tce_iommu_map (struct kvm *kvm,struct iommu_table *tbl, +unsigned long entry,unsigned long gpa, +enum dma_data_direction dir) +{ +long ret; +unsigned long hpa =0 , ua; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry);Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, · line 231
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +8
-0+struct mm_iommu_table_group_mem_t *mem; + +if (!pua) +/* it_userspace allocation might be delayed */ +return H_TOO_HARD ; + +if (kvmppc_gpa_to_ua (kvm, gpa, &ua,NULL )) +return H_PARAMETER ;Referred below as [1]
Quoted 90 lines
Quoted 90 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +54
-3+ + mem =mm_iommu_lookup_rm (kvm->mm, ua,1ULL << tbl->it_page_shift); +if (!mem) +return H_TOO_HARD ; + +if (WARN_ON (mm_iommu_ua_to_hpa_rm (mem, ua, &hpa))) +return H_HARDWARE ; + + pua = (void *)vmalloc_to_phys (pua); +if (WARN_ON (!pua)) +return H_HARDWARE ; + +if (WARN_ON (mm_iommu_mapped_inc (mem))) +return H_CLOSED ; + + ret =iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); +if (ret) { +mm_iommu_mapped_dec (mem); +return H_TOO_HARD ; + } + +if (dir !=DMA_NONE ) +kvmppc_rm_tce_iommu_mapped_dec (kvm, tbl, entry); + + *pua = ua; + +return 0 ; +} +long kvmppc_rm_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) {struct kvmppc_spapr_tce_table *stt;long ret; +struct kvmppc_spapr_tce_iommu_table *stit; +unsigned long entry, gpa; +enum dma_data_direction dir;/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ @@ -182,7 +282,25 @@ long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,if (ret !=H_SUCCESS )return ret; -kvmppc_tce_put (stt, ioba >> stt->page_shift, tce); + entry = ioba >> stt->page_shift; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + dir =iommu_tce_direction (tce); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +if (dir ==DMA_NONE ) + ret =kvmppc_rm_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry); +else + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry, gpa, dir); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_rm_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + +kvmppc_tce_put (stt, entry, tce);return H_SUCCESS ; }@@ -220,9 +338,10 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS ; -unsigned long tces, entry, ua =0 ; +unsigned long tces, entry, ua =0 , tce, gpa;unsigned long *rmap =NULL ;bool prereg = false; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -287,12 +406,24 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, }for (i =0 ; i < npages; ++i) { -unsigned long tce =be64_to_cpu (((u64 *)tces)[i]); + tce =be64_to_cpu (((u64 *)tces)[i]); ret =kvmppc_tce_validate (stt, tce);if (ret !=H_SUCCESS ) goto unlock_exit;Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
Quoted 7 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +7
-0+ gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE ))I don't think you need the
WARN()here - the onlyH_HARDWAREfailure path iniommu_map()already includes aWARN().True, I can drop it here.
Quoted 9 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, · line 424
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+kvmppc_rm_clear_tce (stit->tbl, entry); + else if (ret != H_SUCCESS) + goto unlock_exit;It's also not clear to me why the
H_HARDWAREerror path clears the entry, but the other failure paths don't. Or why anH_HARDWAREwill result in continuing to set the rest of the TCEs, but other failures won't.Quoted 6 lines
Quoted 2 lines
The idea was that other failures still have some chance that handling may succeed in virtual mode or via QEMU,
H_HARDWAREis fatal.Um... yes.. but the logic seems to be backwards for that: on
H_HARDWAREyou warn and keep going, on other errors you bail out entirely.By "fatal" I means fatal for this particular hardware TCE(s), no hope in trying this particular TCE in virtual mode.
Quoted 7 lines
Quoted 4 lines
I am just not sure if
H_PARAMETERis what I want to return at [1], to make the calling code simplier, I could returnH_HARDWAREthere as well (instead ofH_PARAMETER).That sounds right, IIUC the gpa to ua translation shouldn't ever fail because of something the guest did.
Quoted 8 lines
The guest can easily pass bad TCE/GPA which is not in any registered slot. So it is rather
H_PARAMETER.Quoted 3 lines
So I'd expect either
H_HARDWARE, orH_TOO_HARD(if there's some hope that virtual mode can make the translation when real mode couldn't).No, virtual mode uses the exact same helper.
I just realized that kvmppc_gpa_to_ua() could be a part of kvmppc_tce_validate() and kvmppc_rm_tce_iommu_map() could just receive the userspace address rather than guest physical.
Quoted 161 lines
Quoted 159 lines
Quoted 155 lines
Quoted 153 lines
Quoted 18 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+ } +kvmppc_tce_put (stt, entry + i, tce); }@@ -309,6 +440,7 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -322,6 +454,20 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,if (tce_value & (TCE_PCI_WRITE |TCE_PCI_READ ))return H_PARAMETER ;arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, · line 465
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +9
-0+list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +unsigned long entry = ioba >> stit->tbl->it_page_shift; + +for (i =0 ; i < npages; ++i) { + ret =kvmppc_rm_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry + i); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_rm_clear_tce (stit->tbl, entry);As noted earlier, I think this WARN belongs within
iommu_unmap()rather than out here.Quoted 120 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +5
-0+else if (ret !=H_SUCCESS ) +return ret; + } + } +for (i =0 ; i < npages; ++i, ioba += (1ULL << stt->page_shift))kvmppc_tce_put (stt, ioba >> stt->page_shift, tce_value);arch/powerpc/kvm/powerpc.c +2
-0@@ -536,6 +536,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)#ifdef CONFIG_PPC_BOOK3S_64 case KVM_CAP_SPAPR_TCE :case KVM_CAP_SPAPR_TCE_64 : +/* fallthrough */ +case KVM_CAP_SPAPR_TCE_VFIO :case KVM_CAP_PPC_RTAS :case KVM_CAP_PPC_FIXUP_HCALL :case KVM_CAP_PPC_ENABLE_HCALL :virt/kvm/vfio.c +60
-0@@ -20,6 +20,10 @@#include <linux/vfio.h> #include "vfio.h" +#ifdef CONFIG_SPAPR_TCE_IOMMU +#include <asm/kvm_ppc.h> +#endif +struct kvm_vfio_group {struct list_head node;struct vfio_group *vfio_group;@@ -211,6 +215,9 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)mutex_unlock (&kv->lock); +#ifdef CONFIG_SPAPR_TCE_IOMMU +kvm_spapr_tce_release_iommu_group (dev->kvm, vfio_group); +#endif kvm_vfio_group_set_kvm (vfio_group,NULL );kvm_vfio_group_put_external_user (vfio_group);@@ -218,6 +225,53 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)kvm_vfio_update_coherency (dev);return ret; + +#ifdef CONFIG_SPAPR_TCE_IOMMU +case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE : { +struct kvm_vfio_spapr_tce param; +unsigned long minsz; +struct kvm_vfio *kv = dev->private; +struct vfio_group *vfio_group; +struct kvm_vfio_group *kvg; +struct fd f; + + minsz = offsetofend(struct kvm_vfio_spapr_tce, tablefd); + +if (copy_from_user (¶m, (void __user *)arg, minsz)) +return -EFAULT ; + +if (param.argsz < minsz || param.flags) +return -EINVAL ; + + f =fdget (param.groupfd); +if (!f.file) +return -EBADF ; + + vfio_group =kvm_vfio_group_get_external_user (f.file); +fdput (f); + +if (IS_ERR (vfio_group)) +return PTR_ERR (vfio_group); + + ret = -ENOENT ; + +mutex_lock (&kv->lock); + +list_for_each_entry (kvg, &kv->group_list, node) { +if (kvg->vfio_group != vfio_group) +continue ; + + ret =kvm_spapr_tce_attach_iommu_group (dev->kvm, + param.tablefd, vfio_group); + +break ; + } + +mutex_unlock (&kv->lock); + +return ret; + } +#endif /* CONFIG_SPAPR_TCE_IOMMU */ }return -ENXIO ;@@ -242,6 +296,9 @@ static int kvm_vfio_has_attr(struct kvm_device *dev,switch (attr->attr) {case KVM_DEV_VFIO_GROUP_ADD :case KVM_DEV_VFIO_GROUP_DEL : +#ifdef CONFIG_SPAPR_TCE_IOMMU +case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE : +#endif return 0 ; }@@ -257,6 +314,9 @@ static void kvm_vfio_destroy(struct kvm_device *dev)struct kvm_vfio_group *kvg, *tmp;list_for_each_entry_safe (kvg, tmp, &kv->group_list, node) { +#ifdef CONFIG_SPAPR_TCE_IOMMU +kvm_spapr_tce_release_iommu_group (dev->kvm, kvg->vfio_group); +#endif kvm_vfio_group_set_kvm (kvg->vfio_group,NULL );kvm_vfio_group_put_external_user (kvg->vfio_group);list_del (&kvg->node);
Signature · 1 line
Alexey
Quoted 28 lines · Alexey Kardashevskiy
Quoted 27 lines · David Gibson
Quoted 26 lines · Alexey Kardashevskiy
Quoted 25 lines · David Gibson
Quoted 24 lines · Alexey Kardashevskiy
This allows the host kernel to handle
H_PUT_TCE,H_PUT_TCE_INDIRECTandH_STUFF_TCErequests targeted an IOMMU TCE table used for VFIO without passing them to user space which saves time on switching to user space and back.This adds H_PUT_TCE/H_PUT_TCE_INDIRECT/H_STUFF_TCE handlers to KVM. KVM tries to handle a TCE request in the real mode, if failed it passes the request to the virtual mode to complete the operation. If it a virtual mode handler fails, the request is passed to the user space; this is not expected to happen though.
To avoid dealing with page use counters (which is tricky in real mode), this only accelerates SPAPR TCE IOMMU v2 clients which are required to pre-register the userspace memory. The very first TCE request will be handled in the VFIO SPAPR TCE driver anyway as the userspace view of the TCE table (
iommu_table::it_userspace) is not allocated till the very first mapping happens and we cannot call vmalloc in real mode.If we fail to update a hardware IOMMU table unexpected reason, we just clear it and move on as there is nothing really we can do about it - for example, if we hot plug a VFIO device to a guest, existing TCE tables will be mirrored automatically to the hardware and there is no interface to report to the guest about possible failures.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
This adds new attribute -
KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE- to the VFIO KVM device. It takes a VFIO group fd and SPAPR TCE table fd and associates a physical IOMMU table with the SPAPR TCE table (which is a guest view of the hardware IOMMU table). Theiommu_tableobject is cached and referenced so we do not have to look up for it in real
mode.
Quoted 56 lines
Quoted 56 lines
Quoted 56 lines
Quoted 56 lines
Quoted 17 lines
This does not implement the UNSET counterpart as there is no use for it - once the acceleration is enabled, the existing userspace won't disable it unless a VFIO container is destroyed; this adds necessary cleanup to the
KVM_DEV_VFIO_GROUP_DELhandler.As this creates a descriptor per IOMMU table-LIOBN couple (called
kvmppc_spapr_tce_iommu_table), it is possible to have several descriptors with the sameiommu_table(hardware IOMMU table) attached to the same LIOBN; we do not remove duplicates though asiommu_table_ops::exchangenot just update a TCE entry (which is shared among IOMMU groups) but also invalidates the TCE cache (one per IOMMU group).This advertises the new
KVM_CAP_SPAPR_TCE_VFIOcapability to the user space.Quoted 7 lines
This finally makes use of
vfio_external_user_iommu_id()which was introduced quite some time ago and was considered for removal.Tests show that this patch increases transmission speed from 220MB/s to 750..1020MB/s on 10Gb network (Chelsea CXGB3 10Gb ethernet card).
Signed-off-by Alexey Kardashevskiy <[email protected]>I have some comments on this patch, but all the definite ones are pretty minor and could be done as later cleanups.
I have some more serious queries, but they are just queries and requests for clarification. If there are satisfactory answers to them, I'll add my R-b.
---
Quoted 22 lines
Changes:
v5:
* changed error codes in multiple places
* added bunch ofWARN_ON()in places which should not really happen
* adde a check that an iommu table is not attached already to LIOBN
* dropped explicit calls to iommu_tce_clear_param_check/iommu_tce_put_param_checkas kvmppc_tce_validate/kvmppc_ioba_validate call them anyway (since the previous patch)
* if we fail to update a hardware IOMMU table for unexpected reason, this just clears the entryv4:
* added note to the commit log about allowing multiple updates of the same IOMMU table;
* instead of checking for if any memory was preregistered, this returnsH_TOO_HARDif a specific page was not;
* fixed comments from v3 about error handling in many places;
* simplified TCE handlers and merged IOMMU parts inline - for example, there used to bekvmppc_h_put_tce_iommu(), now it is merged intokvmppc_h_put_tce(); this allows to check IOBA boundaries against the first attached table only (makes the code simpler);
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
v3:
* simplified not to use VFIO group notifiers
* reworked cleanup, should be cleaner/simpler nowv2:
* reworked to use new VFIO notifiers
* now sameiommu_tablemay appear in the list several times, to be
fixed later
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
--- Documentation/virtual/kvm/devices/vfio.txt | 22 ++- arch/powerpc/include/asm/kvm_host.h | 8 + arch/powerpc/include/asm/kvm_ppc.h | 4 + include/uapi/linux/kvm.h | 8 + arch/powerpc/kvm/book3s_64_vio.c | 307
++++++++++++++++++++++++++++-
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Review
Quoted 6 lines
arch/powerpc/kvm/book3s_64_vio_hv.c | 152 +++++++++++++- arch/powerpc/kvm/powerpc.c | 2 + virt/kvm/vfio.c | 60 ++++++ 8 files changed, 555 insertions(+), 8 deletions(-)(quoted diff) +0
-0
b/Documentation/virtual/kvm/devices/vfio.txt
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Documentation/virtual/kvm/devices/vfio.txt Groups: · line 21
Quoted 10 lines
index
ef51740c67ca..f95d867168ea100644Documentation/virtual/kvm/devices/vfio.txt +2
-0@@ -16,7 +16,25 @@ Groups: KVM_DEV_VFIO_GROUP attributes: KVM_DEV_VFIO_GROUP_ADD: Add a VFIO group to VFIO-KVM device tracking + kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. KVM_DEV_VFIO_GROUP_DEL: Remove a VFIO group from VFIO-KVM device
tracking
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Documentation/virtual/kvm/devices/vfio.txt +5
-0+ kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO group. + KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: attaches a guest visible TCE table + allocated by sPAPR KVM. + kvm_device_attr.addr points to a struct:
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Documentation/virtual/kvm/devices/vfio.txt +6
-2-For each, kvm_device_attr.addr points to an int32_t file descriptor -for the VFIO group. + struct kvm_vfio_spapr_tce { + __u32 argsz; + __u32 flags; + __s32 groupfd; + __s32 tablefd; + };Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
Documentation/virtual/kvm/devices/vfio.txt +7
-0+ + where + @argsz is the size of kvm_vfio_spapr_tce_liobn; + @flags are not supported now, must be zero; + @groupfd is a file descriptor for a VFIO group; + @tablefd is a file descriptor for a TCE table allocated via + KVM_CREATE_SPAPR_TCE.(quoted diff) +0
-0
b/arch/powerpc/include/asm/kvm_host.h
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
index
e59b172666cd..a827006941f8100644arch/powerpc/include/asm/kvm_host.h +8
-0@@ -191,6 +191,13 @@ struct kvmppc_pginfo {atomic_t refcnt; }; +struct kvmppc_spapr_tce_iommu_table { +struct rcu_head rcu; +struct list_head next; +struct vfio_group *group; +struct iommu_table *tbl; +}; +struct kvmppc_spapr_tce_table {struct list_head list;struct kvm *kvm;@@ -199,6 +206,7 @@ struct kvmppc_spapr_tce_table {u32 page_shift;u64 offset;/* in pages */ u64 size;/* window size in pages */ +struct list_head iommu_tables;struct page *pages[0 ]; };
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +0
-0
b/arch/powerpc/include/asm/kvm_ppc.h
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
arch/powerpc/include/asm/kvm_ppc.h extern long kvmppc_prepare_vrma(struct kvm *kvm, · line 166
Quoted 8 lines
index
e04b7fb8ccaa..b8a39dec92cf100644arch/powerpc/include/asm/kvm_ppc.h +1
-0@@ -163,6 +163,10 @@ extern long kvmppc_prepare_vrma(struct kvm *kvm,extern void kvmppc_map_vrma (struct kvm_vcpu *vcpu,struct kvm_memory_slot *memslot,unsigned long porder);extern int kvmppc_pseries_do_hcall (struct kvm_vcpu *vcpu); +extern long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int
tablefd,
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
arch/powerpc/include/asm/kvm_ppc.h +3
-0+ struct vfio_group *group); +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group);extern long kvm_vm_ioctl_create_spapr_tce (struct kvm *kvm,struct kvm_create_spapr_tce_64 *args);include/uapi/linux/kvm.h +1
-0@@ -1076,6 +1076,7 @@ struct kvm_device_attr {#define KVM_DEV_VFIO_GROUP 1#define KVM_DEV_VFIO_GROUP_ADD 1#define KVM_DEV_VFIO_GROUP_DEL 2 +#define KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE 3enum kvm_device_type {KVM_DEV_TYPE_FSL_MPIC_20 =1 ,@@ -1097,6 +1098,13 @@ enum kvm_device_type { KVM_DEV_TYPE_MAX, };
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Review
Quoted 11 lines
include/uapi/linux/kvm.h +7
-0+struct kvm_vfio_spapr_tce { +__u32 argsz; +__u32 flags; +__s32 groupfd; +__s32 tablefd; +}; +/* * ioctls for VM fds */ (quoted diff) +0
-0
b/arch/powerpc/kvm/book3s_64_vio.c
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
index
15df8ae627d9..062407af09ee100644arch/powerpc/kvm/book3s_64_vio.c +4
-0@@ -27,6 +27,10 @@#include <linux/hugetlb.h> #include <linux/list.h> #include <linux/anon_inodes.h> +#include <linux/iommu.h> +#include <linux/file.h> +#include <linux/vfio.h> +#include <linux/module.h>
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
arch/powerpc/kvm/book3s_64_vio.c line 48
Quoted 9 lines
arch/powerpc/kvm/book3s_64_vio.c +3
-0#include <asm/tlbflush.h> #include <asm/kvm_ppc.h> @@ -39,6 +43,36 @@#include <asm/udbg.h> #include <asm/iommu.h> #include <asm/tce.h> +#include <asm/mmu_context.h> + +static void kvm_vfio_group_put_external_user(struct vfio_group
*vfio_group)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
arch/powerpc/kvm/book3s_64_vio.c +5
-0+{ +void (*fn)(struct vfio_group *); + + fn =symbol_get (vfio_group_put_external_user); +if (WARN_ON (!fn))
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio.c
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio.c +8
-0+return ; + +fn (vfio_group); + +symbol_put (vfio_group_put_external_user); +} + +static int kvm_vfio_external_user_iommu_id(struct vfio_group
*vfio_group)
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
arch/powerpc/kvm/book3s_64_vio.c +14
-0+{ +int (*fn)(struct vfio_group *); +int ret =-1 ; + + fn =symbol_get (vfio_external_user_iommu_id); +if (!fn) +return ret; + + ret =fn (vfio_group); + +symbol_put (vfio_external_user_iommu_id); + +return ret; +}
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio.c static long kvmppc_account_memlimit(unsigned
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio.c +0
-0static unsigned long kvmppc_tce_pages (unsigned long iommu_pages) {@@ -90,6 +124,130 @@ static long kvmppc_account_memlimit(unsigned
long stt_pages, bool inc)
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
arch/powerpc/kvm/book3s_64_vio.c +29
-0return ret; } +static void kvm_spapr_tce_iommu_table_free (struct rcu_head *head) +{ +struct kvmppc_spapr_tce_iommu_table *stit =container_of (head, + struct kvmppc_spapr_tce_iommu_table, rcu); + +iommu_table_put (stit->tbl); +kvm_vfio_group_put_external_user (stit->group); + +kfree (stit); +} + +static void kvm_spapr_tce_liobn_release_iommu_group ( +struct kvmppc_spapr_tce_table *stt, +struct vfio_group *group) +{ +struct kvmppc_spapr_tce_iommu_table *stit, *tmp; + +list_for_each_entry_safe (stit, tmp, &stt->iommu_tables, next) { +if (group && (stit->group != group)) +continue ; + +list_del_rcu (&stit->next); + +call_rcu (&stit->rcu, kvm_spapr_tce_iommu_table_free); + } +} + +extern void kvm_spapr_tce_release_iommu_group (struct kvm *kvm, +struct vfio_group *group)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio.c static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio.c +8
-0+{ +struct kvmppc_spapr_tce_table *stt; + +list_for_each_entry_rcu (stt, &kvm->arch.spapr_tce_tables, list) +kvm_spapr_tce_liobn_release_iommu_group (stt, group); +} + +extern long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int
tablefd,
Quoted 88 lines
Quoted 88 lines
Quoted 88 lines
Quoted 88 lines
Quoted 6 lines
arch/powerpc/kvm/book3s_64_vio.c +6
-0+ struct vfio_group *group) +{ +struct kvmppc_spapr_tce_table *stt =NULL ; +bool found = false; +struct iommu_table *tbl =NULL ; +struct iommu_table_group *table_group;arch/powerpc/kvm/book3s_64_vio.c static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc) · line 179
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio.c +10
-0+long i, ret =0 ; +struct kvmppc_spapr_tce_iommu_table *stit; +struct fd f; +int group_id; +struct iommu_group *grp; + + group_id =kvm_vfio_external_user_iommu_id (group); + grp =iommu_group_get_by_id (group_id); +if (WARN_ON (!grp)) +return -EIO ;I think it would be nicer to have a function that goes directly from
vfio_grouptoiommu_grouprather than going via id. That can be a later cleanup, though.Quoted 67 lines
arch/powerpc/kvm/book3s_64_vio.c +67
-0+ + f =fdget (tablefd); +if (!f.file) { + ret = -EBADF ; + goto put_exit; + } + +list_for_each_entry_rcu (stt, &kvm->arch.spapr_tce_tables, list) { +if (stt == f.file->private_data) { + found = true; +break ; + } + } + +fdput (f); + +if (!found) { + ret = -EINVAL ; + goto put_exit; + } + + table_group =iommu_group_get_iommudata (grp); +if (WARN_ON (!table_group)) { + ret = -EFAULT ; + goto put_exit; + } + +for (i =0 ; i <IOMMU_TABLE_GROUP_MAX_TABLES ; ++i) { +struct iommu_table *tbltmp = table_group->tables[i]; + +if (!tbltmp) +continue ; + +/* +* Make sure hardware table parameters are exactly the same; +* this is used in the TCE handlers where boundary checks +* use only the first attached table. +*/ +if ((tbltmp->it_page_shift == stt->page_shift) && + (tbltmp->it_offset == stt->offset) && + (tbltmp->it_size == stt->size)) { + tbl = tbltmp; +break ; + } + } +if (!tbl) { + ret = -EINVAL ; + goto put_exit; + } + +list_for_each_entry_rcu (stit, &stt->iommu_tables, next) { +if ((stit->tbl == tbl) && (stit->group == group)) { + ret = -EBUSY ; + goto put_exit; + } + } + +iommu_table_get (tbl); + + stit =kzalloc (sizeof (*stit),GFP_KERNEL ); + stit->tbl = tbl; + stit->group = group; + +list_add_rcu (&stit->next, &stt->iommu_tables); + +put_exit: +iommu_group_put (grp);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio.c static int kvm_spapr_tce_release(struct inode
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio.c +4
-0+ +return ret; +} +static void release_spapr_tce_table (struct rcu_head *head) { struct kvmppc_spapr_tce_table *stt = container_of(head,@@ -132,6 +290,8 @@ static int kvm_spapr_tce_release(struct inode
*inode, struct file *filp)
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
arch/powerpc/kvm/book3s_64_vio.c +3
-0list_del_rcu (&stt->list); +kvm_spapr_tce_liobn_release_iommu_group (stt,NULL /* release all */ ); +kvm_put_kvm (stt->kvm); kvmppc_account_memlimit(@@ -181,6 +341,7 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, stt->offset = args->offset; stt->size = size; stt->kvm = kvm; +INIT_LIST_HEAD_RCU (&stt->iommu_tables);
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio.c +0
-0for (i =0 ; i < npages; i++) { stt->pages[i] =alloc_page (GFP_KERNEL | __GFP_ZERO);@@ -209,11 +370,102 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm
*kvm,
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio.c static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio.c +1
-0return ret; } +static void kvmppc_clear_tce(struct iommu_table *tbl, unsigned long
entry)
Quoted 112 lines
Quoted 112 lines
Quoted 77 lines
Quoted 67 lines
arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · line 384
Quoted 11 lines
arch/powerpc/kvm/book3s_64_vio.c +11
-0+{ +unsigned long hpa =0 ; +enum dma_data_direction dir =DMA_NONE ; + +iommu_tce_xchg (tbl, entry, &hpa, &dir); +} + +static long kvmppc_tce_iommu_mapped_dec (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +struct mm_iommu_table_group_mem_t *mem =NULL ;Initialization is unnecessary for the variable above.
Quoted 53 lines
arch/powerpc/kvm/book3s_64_vio.c +53
-0+const unsigned long pgsize =1ULL << tbl->it_page_shift; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); + +if (WARN_ON (!pua)) +return H_HARDWARE ; + + mem =mm_iommu_lookup (kvm->mm, *pua, pgsize); +if (!mem) +return H_TOO_HARD ; + +mm_iommu_mapped_dec (mem); + + *pua =0 ; + +return H_SUCCESS ; +} + +static long kvmppc_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret; + +if (iommu_tce_xchg (tbl, entry, &hpa, &dir)) +return H_HARDWARE ; + +if (dir ==DMA_NONE ) +return H_SUCCESS ; + + ret =kvmppc_tce_iommu_mapped_dec (kvm, tbl, entry); +if (ret !=H_SUCCESS ) +iommu_tce_xchg (tbl, entry, &hpa, &dir); + +return ret; +} + +long kvmppc_tce_iommu_map (struct kvm *kvm,struct iommu_table *tbl, +unsigned long entry,unsigned long gpa, +enum dma_data_direction dir) +{ +long ret; +unsigned long hpa, ua, *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); +struct mm_iommu_table_group_mem_t *mem; + +if (!pua) +/* it_userspace allocation might be delayed */ +return H_TOO_HARD ; + +if (kvmppc_gpa_to_ua (kvm, gpa, &ua,NULL )) +return H_PARAMETER ; + + mem =mm_iommu_lookup (kvm->mm, ua,1ULL << tbl->it_page_shift);Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio.c long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, · line 442
Quoted 5 lines
arch/powerpc/kvm/book3s_64_vio.c +5
-0+if (!mem) +return H_TOO_HARD ; + +if (WARN_ON (mm_iommu_ua_to_hpa (mem, ua, &hpa))) +return H_HARDWARE ;Remind me - could a failure here be triggered by userspace failing to preregister all the memory it should?
Quoted 3 lines
mm_iommu_lookup()should fail first as the only reason formm_iommu_ua_to_hpa()to fail is (entry >=mem->entries) but this is checked inmm_iommu_lookup().Ah, yes of course, I forgot. Ok, that should be fine.
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
arch/powerpc/kvm/book3s_64_vio.c +21
-1+if (mm_iommu_mapped_inc (mem)) +return H_CLOSED ; + + ret =iommu_tce_xchg (tbl, entry, &hpa, &dir); +if (ret) { +mm_iommu_mapped_dec (mem); +return H_TOO_HARD ; + } + +if (dir !=DMA_NONE ) +kvmppc_tce_iommu_mapped_dec (kvm, tbl, entry); + + *pua = ua; + +return 0 ; +} +long kvmppc_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) {struct kvmppc_spapr_tce_table *stt; -long ret; +long ret, idx; +struct kvmppc_spapr_tce_iommu_table *stit; +unsigned long entry, gpa; +enum dma_data_direction dir;
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio.c long kvmppc_h_put_tce(struct kvm_vcpu *vcpu,
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio.c +0
-0/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ @@ -230,7 +482,28 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu,
unsigned long liobn,
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
arch/powerpc/kvm/book3s_64_vio.c +22
-1if (ret !=H_SUCCESS )return ret; -kvmppc_tce_put (stt, ioba >> stt->page_shift, tce); + entry = ioba >> stt->page_shift; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + dir =iommu_tce_direction (tce); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +if (dir ==DMA_NONE ) { + ret =kvmppc_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry); + }else { + idx =srcu_read_lock (&vcpu->kvm->srcu); + ret =kvmppc_tce_iommu_map (vcpu->kvm, stit->tbl, + entry, gpa, dir); +srcu_read_unlock (&vcpu->kvm->srcu, idx); + } + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + +kvmppc_tce_put (stt, entry, tce);
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio.c long kvmppc_h_put_tce_indirect(struct kvm_vcpu
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio.c +0
-0return H_SUCCESS ; }@@ -242,9 +515,10 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu
*vcpu,
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
arch/powerpc/kvm/book3s_64_vio.c +2
-1{struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS , idx; -unsigned long entry, ua =0 ; +unsigned long entry, ua =0 , gpa;u64 __user *tces;u64 tce; +struct kvmppc_spapr_tce_iommu_table *stit;
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio.c long kvmppc_h_put_tce_indirect(struct kvm_vcpu
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio.c +0
-0stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -283,6 +557,18 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu
*vcpu,
Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
arch/powerpc/kvm/book3s_64_vio.c +27
-0if (ret !=H_SUCCESS ) goto unlock_exit; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) + goto unlock_exit; + } +kvmppc_tce_put (stt, entry + i, tce); }@@ -299,6 +585,7 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -312,6 +599,20 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,if (tce_value & (TCE_PCI_WRITE |TCE_PCI_READ ))return H_PARAMETER ; +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +unsigned long entry = ioba >> stit->tbl->it_page_shift; + +for (i =0 ; i < npages; ++i) { + ret =kvmppc_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry + i); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + } +for (i =0 ; i < npages; ++i, ioba += (1ULL << stt->page_shift))kvmppc_tce_put (stt, ioba >> stt->page_shift, tce_value);
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +0
-0
b/arch/powerpc/kvm/book3s_64_vio_hv.c
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm,
Quoted 4 lines
index
92d769f4eaea..4a1d978ebd98100644arch/powerpc/kvm/book3s_64_vio_hv.c +0
-0@@ -161,11 +161,111 @@ long kvmppc_gpa_to_ua(struct kvm *kvm,
unsigned long gpa,
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +1
-0EXPORT_SYMBOL_GPL (kvmppc_gpa_to_ua);#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE +static void kvmppc_rm_clear_tce(struct iommu_table *tbl, unsigned
long entry)
Quoted 60 lines
Quoted 48 lines
Quoted 48 lines
Quoted 46 lines
Quoted 31 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +31
-0+{ +unsigned long hpa =0 ; +enum dma_data_direction dir =DMA_NONE ; + +iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); +} + +static long kvmppc_rm_tce_iommu_mapped_dec (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +struct mm_iommu_table_group_mem_t *mem =NULL ; +const unsigned long pgsize =1ULL << tbl->it_page_shift; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry); + +if (WARN_ON (!pua)) +return H_HARDWARE ; + + pua = (void *)vmalloc_to_phys (pua); +if (!pua) +return H_TOO_HARD ; + + mem =mm_iommu_lookup_rm (kvm->mm, *pua, pgsize); +if (!mem) +return H_TOO_HARD ; + +mm_iommu_mapped_dec (mem); + + *pua =0 ; + +return H_SUCCESS ; +}arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, · line 205
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +10
-0+ +static long kvmppc_rm_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret; + +if (iommu_tce_xchg_rm (tbl, entry, &hpa, &dir)) +return H_HARDWARE ;To avoid a double
WARN()(and to make the warnings easier to understand) I'd suggest putting aWARN_ON()here, rather than in the callers when they receieve anH_HARDWARE. IIUC this really shouldn't ever happen, and it certainly can't be the guest's fault?Quoted 4 lines
Quoted 1 line
Makes sense.
I guess it might want
WARN_ON_ONCE()to avoid spamming the user with errors for every TCE, though.We do not expect this to happen at all :) I can convert all of them to
_ONCEreally as the purpose ofWARN_ONis mostly to document what we do not expect.
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +4
-0+ +if (dir ==DMA_NONE ) +return H_SUCCESS ; +Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +8
-0+ ret =kvmppc_rm_tce_iommu_mapped_dec (kvm, tbl, entry); +if (ret) +iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); + +return ret; +} + +static long kvmppc_rm_tce_iommu_map(struct kvm *kvm, struct
iommu_table *tbl,
Quoted 57 lines
Quoted 57 lines
Quoted 57 lines
Quoted 6 lines
Quoted 6 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +6
-0+ unsigned long entry, unsigned long gpa, + enum dma_data_direction dir) +{ + long ret; +unsigned long hpa =0 , ua; +unsigned long *pua =IOMMU_TABLE_USERSPACE_ENTRY (tbl, entry);Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, · line 231
Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +8
-0+struct mm_iommu_table_group_mem_t *mem; + +if (!pua) +/* it_userspace allocation might be delayed */ +return H_TOO_HARD ; + +if (kvmppc_gpa_to_ua (kvm, gpa, &ua,NULL )) +return H_PARAMETER ;Referred below as [1]
Quoted 38 lines
Quoted 38 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +32
-0+ + mem =mm_iommu_lookup_rm (kvm->mm, ua,1ULL << tbl->it_page_shift); +if (!mem) +return H_TOO_HARD ; + +if (WARN_ON (mm_iommu_ua_to_hpa_rm (mem, ua, &hpa))) +return H_HARDWARE ; + + pua = (void *)vmalloc_to_phys (pua); +if (WARN_ON (!pua)) +return H_HARDWARE ; + +if (WARN_ON (mm_iommu_mapped_inc (mem))) +return H_CLOSED ; + + ret =iommu_tce_xchg_rm (tbl, entry, &hpa, &dir); +if (ret) { +mm_iommu_mapped_dec (mem); +return H_TOO_HARD ; + } + +if (dir !=DMA_NONE ) +kvmppc_rm_tce_iommu_mapped_dec (kvm, tbl, entry); + + *pua = ua; + +return 0 ; +} +long kvmppc_rm_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce) {struct kvmppc_spapr_tce_table *stt;long ret; +struct kvmppc_spapr_tce_iommu_table *stit; +unsigned long entry, gpa; +enum dma_data_direction dir;
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu,
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +0
-0/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ /* liobn, ioba, tce); */ @@ -182,7 +282,25 @@ long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu,
unsigned long liobn,
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +19
-1if (ret !=H_SUCCESS )return ret; -kvmppc_tce_put (stt, ioba >> stt->page_shift, tce); + entry = ioba >> stt->page_shift; + gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + dir =iommu_tce_direction (tce); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +if (dir ==DMA_NONE ) + ret =kvmppc_rm_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry); +else + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry, gpa, dir); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_rm_clear_tce (stit->tbl, entry); +else if (ret !=H_SUCCESS ) +return ret; + } + +kvmppc_tce_put (stt, entry, tce);
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +0
-0return H_SUCCESS ; }@@ -220,9 +338,10 @@ long kvmppc_rm_h_put_tce_indirect(struct
kvm_vcpu *vcpu,
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +2
-1{struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS ; -unsigned long tces, entry, ua =0 ; +unsigned long tces, entry, ua =0 , tce, gpa;unsigned long *rmap =NULL ;bool prereg = false; +struct kvmppc_spapr_tce_iommu_table *stit;
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +0
-0stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -287,12 +406,24 @@ long kvmppc_rm_h_put_tce_indirect(struct
kvm_vcpu *vcpu,
Quoted 104 lines
Quoted 36 lines
Quoted 36 lines
Quoted 10 lines
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +1
-1}for (i =0 ; i < npages; ++i) { -unsigned long tce =be64_to_cpu (((u64 *)tces)[i]); + tce =be64_to_cpu (((u64 *)tces)[i]); ret =kvmppc_tce_validate (stt, tce);if (ret !=H_SUCCESS ) goto unlock_exit;Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
Quoted 7 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +7
-0+ gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE ))I don't think you need the
WARN()here - the onlyH_HARDWAREfailure path iniommu_map()already includes aWARN().True, I can drop it here.
Quoted 9 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, · line 424
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+kvmppc_rm_clear_tce (stit->tbl, entry); + else if (ret != H_SUCCESS) + goto unlock_exit;It's also not clear to me why the
H_HARDWAREerror path clears the entry, but the other failure paths don't. Or why anH_HARDWAREwill result in continuing to set the rest of the TCEs, but other failures won't.Quoted 6 lines
Quoted 2 lines
The idea was that other failures still have some chance that handling may succeed in virtual mode or via QEMU,
H_HARDWAREis fatal.Um... yes.. but the logic seems to be backwards for that: on
H_HARDWAREyou warn and keep going, on other errors you bail out entirely.By "fatal" I means fatal for this particular hardware TCE(s), no hope in trying this particular TCE in virtual mode.
Quoted 7 lines
Quoted 4 lines
I am just not sure if
H_PARAMETERis what I want to return at [1], to make the calling code simplier, I could returnH_HARDWAREthere as well (instead ofH_PARAMETER).That sounds right, IIUC the gpa to ua translation shouldn't ever fail because of something the guest did.
The guest can easily pass bad TCE/GPA which is not in any registered slot. So it is rather
H_PARAMETER.Quoted 3 lines
So I'd expect either
H_HARDWARE, orH_TOO_HARD(if there's some hope that virtual mode can make the translation when real mode couldn't).No, virtual mode uses the exact same helper.
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 16 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+ } +kvmppc_tce_put (stt, entry + i, tce); }@@ -309,6 +440,7 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -322,6 +454,20 @@ long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,if (tce_value & (TCE_PCI_WRITE |TCE_PCI_READ ))return H_PARAMETER ;arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu, · line 465
Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +9
-0+list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { +unsigned long entry = ioba >> stit->tbl->it_page_shift; + +for (i =0 ; i < npages; ++i) { + ret =kvmppc_rm_tce_iommu_unmap (vcpu->kvm, + stit->tbl, entry + i); + +if (WARN_ON_ONCE (ret ==H_HARDWARE )) +kvmppc_rm_clear_tce (stit->tbl, entry);As noted earlier, I think this WARN belongs within
iommu_unmap()rather than out here.Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +5
-0+else if (ret !=H_SUCCESS ) +return ret; + } + } +for (i =0 ; i < npages; ++i, ioba += (1ULL << stt->page_shift))kvmppc_tce_put (stt, ioba >> stt->page_shift, tce_value);
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
arch/powerpc/kvm/powerpc.c int kvm_vm_ioctl_check_extension(struct kvm *kvm,
Quoted 5 lines
arch/powerpc/kvm/powerpc.c +0
-0@@ -536,6 +536,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm,
long ext)
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
arch/powerpc/kvm/powerpc.c +2
-0#ifdef CONFIG_PPC_BOOK3S_64 case KVM_CAP_SPAPR_TCE :case KVM_CAP_SPAPR_TCE_64 : +/* fallthrough */ +case KVM_CAP_SPAPR_TCE_VFIO :case KVM_CAP_PPC_RTAS :case KVM_CAP_PPC_FIXUP_HCALL :case KVM_CAP_PPC_ENABLE_HCALL :virt/kvm/vfio.c +0
-0@@ -20,6 +20,10 @@#include <linux/vfio.h> #include "vfio.h"
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
virt/kvm/vfio.c static int kvm_vfio_set_group(struct kvm_device
Quoted 8 lines
virt/kvm/vfio.c +4
-0+#ifdef CONFIG_SPAPR_TCE_IOMMU +#include <asm/kvm_ppc.h> +#endif +struct kvm_vfio_group {struct list_head node;struct vfio_group *vfio_group;@@ -211,6 +215,9 @@ static int kvm_vfio_set_group(struct kvm_device
*dev, long attr, u64 arg)
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
virt/kvm/vfio.c static int kvm_vfio_set_group(struct kvm_device
Quoted 10 lines
virt/kvm/vfio.c +3
-0mutex_unlock (&kv->lock); +#ifdef CONFIG_SPAPR_TCE_IOMMU +kvm_spapr_tce_release_iommu_group (dev->kvm, vfio_group); +#endif kvm_vfio_group_set_kvm (vfio_group,NULL );kvm_vfio_group_put_external_user (vfio_group);@@ -218,6 +225,53 @@ static int kvm_vfio_set_group(struct kvm_device
*dev, long attr, u64 arg)
Quoted 82 lines
Quoted 80 lines
Quoted 76 lines
Quoted 74 lines
Quoted 73 lines
virt/kvm/vfio.c +53
-0kvm_vfio_update_coherency (dev);return ret; + +#ifdef CONFIG_SPAPR_TCE_IOMMU +case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE : { +struct kvm_vfio_spapr_tce param; +unsigned long minsz; +struct kvm_vfio *kv = dev->private; +struct vfio_group *vfio_group; +struct kvm_vfio_group *kvg; +struct fd f; + + minsz = offsetofend(struct kvm_vfio_spapr_tce, tablefd); + +if (copy_from_user (¶m, (void __user *)arg, minsz)) +return -EFAULT ; + +if (param.argsz < minsz || param.flags) +return -EINVAL ; + + f =fdget (param.groupfd); +if (!f.file) +return -EBADF ; + + vfio_group =kvm_vfio_group_get_external_user (f.file); +fdput (f); + +if (IS_ERR (vfio_group)) +return PTR_ERR (vfio_group); + + ret = -ENOENT ; + +mutex_lock (&kv->lock); + +list_for_each_entry (kvg, &kv->group_list, node) { +if (kvg->vfio_group != vfio_group) +continue ; + + ret =kvm_spapr_tce_attach_iommu_group (dev->kvm, + param.tablefd, vfio_group); + +break ; + } + +mutex_unlock (&kv->lock); + +return ret; + } +#endif /* CONFIG_SPAPR_TCE_IOMMU */ }return -ENXIO ;@@ -242,6 +296,9 @@ static int kvm_vfio_has_attr(struct kvm_device *dev,switch (attr->attr) {case KVM_DEV_VFIO_GROUP_ADD :case KVM_DEV_VFIO_GROUP_DEL : +#ifdef CONFIG_SPAPR_TCE_IOMMU +case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE : +#endif return 0 ; }@@ -257,6 +314,9 @@ static void kvm_vfio_destroy(struct kvm_device *dev)struct kvm_vfio_group *kvg, *tmp;list_for_each_entry_safe (kvg, tmp, &kv->group_list, node) { +#ifdef CONFIG_SPAPR_TCE_IOMMU +kvm_spapr_tce_release_iommu_group (dev->kvm, kvg->vfio_group); +#endif kvm_vfio_group_set_kvm (kvg->vfio_group,NULL );kvm_vfio_group_put_external_user (kvg->vfio_group);list_del (&kvg->node);
Signature · 1 line
Alexey
Re: [PATCH kernel v5 10/10] KVM: PPC: VFIO: Add in-kernel acceleration for VFIO
In reply to Alexey KardashevskiyQuoted 5 lines · Alexey Kardashevskiy
Quoted 3 lines · David Gibson
Quoted 2 lines · Alexey Kardashevskiy
Quoted 1 line · David Gibson
On Wed, Feb 22, 2017 at 07:21:33PM +1100, Alexey Kardashevskiy
wrote:
[snip]
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +6
-0+static long kvmppc_rm_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret;Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, · line 205
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+ +if (iommu_tce_xchg_rm (tbl, entry, &hpa, &dir)) +return H_HARDWARE ;To avoid a double
WARN()(and to make the warnings easier to understand) I'd suggest putting aWARN_ON()here, rather than in the callers when they receieve anH_HARDWARE. IIUC this really shouldn't ever happen, and it certainly can't be the guest's fault?Makes sense.
Quoted 7 lines
Quoted 2 lines
I guess it might want
WARN_ON_ONCE()to avoid spamming the user with errors for every TCE, though.We do not expect this to happen at all :) I can convert all of them to
_ONCEreally as the purpose ofWARN_ONis mostly to document what we do not expect.
Sure, seems reasonable.
[snip]
Quoted 52 lines
Quoted 52 lines
Quoted 51 lines
Quoted 23 lines
Quoted 23 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-2@@ -220,9 +338,10 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS ; -unsigned long tces, entry, ua =0 ; +unsigned long tces, entry, ua =0 , tce, gpa;unsigned long *rmap =NULL ;bool prereg = false; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -287,12 +406,24 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, }for (i =0 ; i < npages; ++i) { -unsigned long tce =be64_to_cpu (((u64 *)tces)[i]); + tce =be64_to_cpu (((u64 *)tces)[i]); ret =kvmppc_tce_validate (stt, tce);if (ret !=H_SUCCESS ) goto unlock_exit;Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
Quoted 7 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +7
-0+ gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE ))I don't think you need the
WARN()here - the onlyH_HARDWAREfailure path iniommu_map()already includes aWARN().True, I can drop it here.
Quoted 9 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, · line 424
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+kvmppc_rm_clear_tce (stit->tbl, entry); + else if (ret != H_SUCCESS) + goto unlock_exit;It's also not clear to me why the
H_HARDWAREerror path clears the entry, but the other failure paths don't. Or why anH_HARDWAREwill result in continuing to set the rest of the TCEs, but other failures won't.The idea was that other failures still have some chance that handling may succeed in virtual mode or via QEMU,
H_HARDWAREis fatal.
Quoted 6 lines
Quoted 3 lines
Um... yes.. but the logic seems to be backwards for that: on
H_HARDWAREyou warn and keep going, on other errors you bail out entirely.By "fatal" I means fatal for this particular hardware TCE(s), no hope in trying this particular TCE in virtual mode.
Ok... still not following why that means the "fatal" error results in continuing to attempt for the rest of the updated TCEs, whereas the "non fatal" one bails out. Especially since the bail out will only go to virtual mode if ret == H_TOO_HARD, which it isn't clear is the only possibility.
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
I am just not sure if
H_PARAMETERis what I want to return at [1], to make the calling code simplier, I could returnH_HARDWAREthere as well (instead ofH_PARAMETER).That sounds right, IIUC the gpa to ua translation shouldn't ever fail because of something the guest did.
The guest can easily pass bad TCE/GPA which is not in any registered slot. So it is rather
H_PARAMETER.
Ah, yes.
Quoted 5 lines
Quoted 3 lines
So I'd expect either
H_HARDWARE, orH_TOO_HARD(if there's some hope that virtual mode can make the translation when real mode couldn't).No, virtual mode uses the exact same helper.
Ok.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH kernel v5 10/10] KVM: PPC: VFIO: Add in-kernel acceleration for VFIO
Quoted 92 lines · David Gibson
Quoted 5 lines · Alexey Kardashevskiy
Quoted 3 lines · David Gibson
Quoted 2 lines · Alexey Kardashevskiy
Quoted 1 line · David Gibson
On Wed, Feb 22, 2017 at 07:21:33PM +1100, Alexey Kardashevskiy
wrote:
[snip]
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +6
-0+static long kvmppc_rm_tce_iommu_unmap (struct kvm *kvm, +struct iommu_table *tbl,unsigned long entry) +{ +enum dma_data_direction dir =DMA_NONE ; +unsigned long hpa =0 ; +long ret;Quoted 8 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, · line 205
Quoted 3 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+ +if (iommu_tce_xchg_rm (tbl, entry, &hpa, &dir)) +return H_HARDWARE ;To avoid a double
WARN()(and to make the warnings easier to understand) I'd suggest putting aWARN_ON()here, rather than in the callers when they receieve anH_HARDWARE. IIUC this really shouldn't ever happen, and it certainly can't be the guest's fault?Makes sense.
Quoted 7 lines
Quoted 2 lines
I guess it might want
WARN_ON_ONCE()to avoid spamming the user with errors for every TCE, though.We do not expect this to happen at all :) I can convert all of them to
_ONCEreally as the purpose ofWARN_ONis mostly to document what we do not expect.Sure, seems reasonable.
[snip]
Quoted 56 lines
Quoted 55 lines
Quoted 49 lines
Quoted 23 lines
Quoted 23 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-2@@ -220,9 +338,10 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, {struct kvmppc_spapr_tce_table *stt;long i, ret =H_SUCCESS ; -unsigned long tces, entry, ua =0 ; +unsigned long tces, entry, ua =0 , tce, gpa;unsigned long *rmap =NULL ;bool prereg = false; +struct kvmppc_spapr_tce_iommu_table *stit; stt =kvmppc_find_table (vcpu->kvm, liobn);if (!stt)@@ -287,12 +406,24 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, }for (i =0 ; i < npages; ++i) { -unsigned long tce =be64_to_cpu (((u64 *)tces)[i]); + tce =be64_to_cpu (((u64 *)tces)[i]); ret =kvmppc_tce_validate (stt, tce);if (ret !=H_SUCCESS ) goto unlock_exit;Quoted 10 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
Quoted 7 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +7
-0+ gpa = tce & ~(TCE_PCI_READ |TCE_PCI_WRITE ); + +list_for_each_entry_lockless (stit, &stt->iommu_tables, next) { + ret =kvmppc_rm_tce_iommu_map (vcpu->kvm, + stit->tbl, entry + i, gpa, +iommu_tce_direction (tce)); +if (WARN_ON_ONCE (ret ==H_HARDWARE ))I don't think you need the
WARN()here - the onlyH_HARDWAREfailure path iniommu_map()already includes aWARN().True, I can drop it here.
Quoted 9 lines
arch/powerpc/kvm/book3s_64_vio_hv.c long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, · line 424
Quoted 4 lines
arch/powerpc/kvm/book3s_64_vio_hv.c +3
-0+kvmppc_rm_clear_tce (stit->tbl, entry); + else if (ret != H_SUCCESS) + goto unlock_exit;It's also not clear to me why the
H_HARDWAREerror path clears the entry, but the other failure paths don't. Or why anH_HARDWAREwill result in continuing to set the rest of the TCEs, but other failures won't.Quoted 2 lines
The idea was that other failures still have some chance that handling may succeed in virtual mode or via QEMU,
H_HARDWAREis fatal.Um... yes.. but the logic seems to be backwards for that: on
H_HARDWAREyou warn and keep going, on other errors you bail out entirely.
Quoted 6 lines
Quoted 2 lines
By "fatal" I means fatal for this particular hardware TCE(s), no hope in trying this particular TCE in virtual mode.
Ok... still not following why that means the "fatal" error results in continuing to attempt for the rest of the updated TCEs, whereas the "non fatal" one bails out.
I was applying the principle that if after all checks done we still cannot update the hardware table, then just clear the TCE and move on. Or I misunderstood the idea?
Quoted 3 lines
Especially since the bail out will only go
to virtual mode if ret ==H_TOO_HARD, which it isn't clear is the only possibility.
H_TOO_HARD goes to virtual mode, H_TOO_HARD in virtual goes to the userspace (QEMU).
Will "if (WARN_ON_ONCE(ret != H_SUCCESS && ret != H_TOO_HARD))" make more sense?
Quoted 22 lines
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
I am just not sure if
H_PARAMETERis what I want to return at [1], to make the calling code simplier, I could returnH_HARDWAREthere as well (instead ofH_PARAMETER).That sounds right, IIUC the gpa to ua translation shouldn't ever fail because of something the guest did.
The guest can easily pass bad TCE/GPA which is not in any registered slot. So it is rather
H_PARAMETER.Ah, yes.
Quoted 5 lines
Quoted 3 lines
So I'd expect either
H_HARDWARE, orH_TOO_HARD(if there's some hope that virtual mode can make the translation when real mode couldn't).No, virtual mode uses the exact same helper.
Ok.
Signature · 1 line
Alexey
Re: [PATCH kernel v5 10/10] KVM: PPC: VFIO: Add in-kernel acceleration for VFIO
Quoted 96 lines · Alexey Kardashevskiy
Quoted 95 lines · David Gibson
Quoted 5 lines · Alexey Kardashevskiy
Quoted 3 lines · David Gibson
Quoted 2 lines · Alexey Kardashevskiy
Quoted 1 line · David Gibson
On Wed, Feb 22, 2017 at 07:21:33PM +1100, Alexey Kardashevskiy
wrote:
[snip]
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
(quoted diff) +6
-0+static long kvmppc_rm_tce_iommu_unmap(struct kvm *kvm, + struct iommu_table *tbl, unsigned long entry) +{ + enum dma_data_direction dir = DMA_NONE; + unsigned long hpa = 0; + long ret;Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ + if (iommu_tce_xchg_rm(tbl, entry, &hpa, &dir)) + return H_HARDWARE;To avoid a double
WARN()(and to make the warnings easier to understand) I'd suggest putting aWARN_ON()here, rather than in the callers when they receieve anH_HARDWARE. IIUC this really shouldn't ever happen, and it certainly can't be the guest's fault?Makes sense.
Quoted 7 lines
Quoted 2 lines
I guess it might want
WARN_ON_ONCE()to avoid spamming the user with errors for every TCE, though.We do not expect this to happen at all :) I can convert all of them to
_ONCEreally as the purpose ofWARN_ONis mostly to document what we do not expect.Sure, seems reasonable.
[snip]
Quoted 58 lines
Quoted 49 lines
Quoted 49 lines
Quoted 23 lines
Quoted 23 lines
(quoted diff) +3
-2@@ -220,9 +338,10 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, { struct kvmppc_spapr_tce_table *stt; long i, ret = H_SUCCESS; - unsigned long tces, entry, ua = 0; + unsigned long tces, entry, ua = 0, tce, gpa; unsigned long *rmap = NULL; bool prereg = false; + struct kvmppc_spapr_tce_iommu_table *stit; stt = kvmppc_find_table(vcpu->kvm, liobn); if (!stt)@@ -287,12 +406,24 @@ long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, } for (i = 0; i < npages; ++i) { - unsigned long tce = be64_to_cpu(((u64 *)tces)[i]); + tce = be64_to_cpu(((u64 *)tces)[i]); ret = kvmppc_tce_validate(stt, tce); if (ret != H_SUCCESS) goto unlock_exit;Quoted 10 lines
Review long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, · line 421
Quoted 7 lines
(quoted diff) +7
-0+ gpa = tce & ~(TCE_PCI_READ | TCE_PCI_WRITE); + + list_for_each_entry_lockless(stit, &stt->iommu_tables, next) { + ret = kvmppc_rm_tce_iommu_map(vcpu->kvm, + stit->tbl, entry + i, gpa, + iommu_tce_direction(tce)); + if (WARN_ON_ONCE(ret == H_HARDWARE))I don't think you need the
WARN()here - the onlyH_HARDWAREfailure path iniommu_map()already includes aWARN().True, I can drop it here.
Quoted 9 lines
Review long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu, · line 424
Quoted 4 lines
(quoted diff) +3
-0+ kvmppc_rm_clear_tce(stit->tbl, entry); + else if (ret != H_SUCCESS) + goto unlock_exit;It's also not clear to me why the
H_HARDWAREerror path clears the entry, but the other failure paths don't. Or why anH_HARDWAREwill result in continuing to set the rest of the TCEs, but other failures won't.Quoted 6 lines
Quoted 2 lines
The idea was that other failures still have some chance that handling may succeed in virtual mode or via QEMU,
H_HARDWAREis fatal.Um... yes.. but the logic seems to be backwards for that: on
H_HARDWAREyou warn and keep going, on other errors you bail out entirely.By "fatal" I means fatal for this particular hardware TCE(s), no hope in trying this particular TCE in virtual mode.
Quoted 7 lines
Quoted 3 lines
Ok... still not following why that means the "fatal" error results in continuing to attempt for the rest of the updated TCEs, whereas the "non fatal" one bails out.
I was applying the principle that if after all checks done we still cannot update the hardware table, then just clear the TCE and move on. Or I misunderstood the idea?
*Still* not seeing why if we cannot update the hardware table we keep trying with the rest of the entries, but on other failures we don't.
Quoted 10 lines
Quoted 3 lines
Especially since the bail out will only go
to virtual mode if ret ==H_TOO_HARD, which it isn't clear is the only possibility.
H_TOO_HARDgoes to virtual mode,H_TOO_HARDin virtual goes to the userspace (QEMU).Will "if (
WARN_ON_ONCE(ret != H_SUCCESS && ret != H_TOO_HARD))" make more sense?
Probably, but depends what's in the if.
Quoted 24 lines
Quoted 22 lines
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
I am just not sure if
H_PARAMETERis what I want to return at [1], to make the calling code simplier, I could returnH_HARDWAREthere as well (instead ofH_PARAMETER).That sounds right, IIUC the gpa to ua translation shouldn't ever fail because of something the guest did.
The guest can easily pass bad TCE/GPA which is not in any registered slot. So it is rather
H_PARAMETER.Ah, yes.
Quoted 5 lines
Quoted 3 lines
So I'd expect either
H_HARDWARE, orH_TOO_HARD(if there's some hope that virtual mode can make the translation when real mode couldn't).No, virtual mode uses the exact same helper.
Ok.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson