Skip to content

Commit b1af23d

Browse files
aikpaulusmack
authored andcommitted
KVM: PPC: iommu: Unify TCE checking
This reworks helpers for checking TCE update parameters in way they can be used in KVM. This should cause no behavioral change. Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru> Reviewed-by: David Gibson <david@gibson.dropbear.id.au> Acked-by: Michael Ellerman <mpe@ellerman.id.au> Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
1 parent da6f59e commit b1af23d

File tree

4 files changed

+39
-55
lines changed

4 files changed

+39
-55
lines changed

arch/powerpc/include/asm/iommu.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,21 @@ static inline void iommu_restore(void)
296296
#endif
297297

298298
/* The API to support IOMMU operations for VFIO */
299-
extern int iommu_tce_clear_param_check(struct iommu_table *tbl,
300-
unsigned long ioba, unsigned long tce_value,
301-
unsigned long npages);
302-
extern int iommu_tce_put_param_check(struct iommu_table *tbl,
303-
unsigned long ioba, unsigned long tce);
299+
extern int iommu_tce_check_ioba(unsigned long page_shift,
300+
unsigned long offset, unsigned long size,
301+
unsigned long ioba, unsigned long npages);
302+
extern int iommu_tce_check_gpa(unsigned long page_shift,
303+
unsigned long gpa);
304+
305+
#define iommu_tce_clear_param_check(tbl, ioba, tce_value, npages) \
306+
(iommu_tce_check_ioba((tbl)->it_page_shift, \
307+
(tbl)->it_offset, (tbl)->it_size, \
308+
(ioba), (npages)) || (tce_value))
309+
#define iommu_tce_put_param_check(tbl, ioba, gpa) \
310+
(iommu_tce_check_ioba((tbl)->it_page_shift, \
311+
(tbl)->it_offset, (tbl)->it_size, \
312+
(ioba), 1) || \
313+
iommu_tce_check_gpa((tbl)->it_page_shift, (gpa)))
304314

305315
extern void iommu_flush_tce(struct iommu_table *tbl);
306316
extern int iommu_take_ownership(struct iommu_table *tbl);

arch/powerpc/include/asm/kvm_ppc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,10 @@ extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
178178
struct kvm_create_spapr_tce_64 *args);
179179
extern struct kvmppc_spapr_tce_table *kvmppc_find_table(
180180
struct kvm *kvm, unsigned long liobn);
181-
extern long kvmppc_ioba_validate(struct kvmppc_spapr_tce_table *stt,
182-
unsigned long ioba, unsigned long npages);
181+
#define kvmppc_ioba_validate(stt, ioba, npages) \
182+
(iommu_tce_check_ioba((stt)->page_shift, (stt)->offset, \
183+
(stt)->size, (ioba), (npages)) ? \
184+
H_PARAMETER : H_SUCCESS)
183185
extern long kvmppc_tce_validate(struct kvmppc_spapr_tce_table *tt,
184186
unsigned long tce);
185187
extern long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,

arch/powerpc/kernel/iommu.c

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -963,47 +963,36 @@ void iommu_flush_tce(struct iommu_table *tbl)
963963
}
964964
EXPORT_SYMBOL_GPL(iommu_flush_tce);
965965

966-
int iommu_tce_clear_param_check(struct iommu_table *tbl,
967-
unsigned long ioba, unsigned long tce_value,
968-
unsigned long npages)
966+
int iommu_tce_check_ioba(unsigned long page_shift,
967+
unsigned long offset, unsigned long size,
968+
unsigned long ioba, unsigned long npages)
969969
{
970-
/* tbl->it_ops->clear() does not support any value but 0 */
971-
if (tce_value)
972-
return -EINVAL;
970+
unsigned long mask = (1UL << page_shift) - 1;
973971

974-
if (ioba & ~IOMMU_PAGE_MASK(tbl))
972+
if (ioba & mask)
975973
return -EINVAL;
976974

977-
ioba >>= tbl->it_page_shift;
978-
if (ioba < tbl->it_offset)
975+
ioba >>= page_shift;
976+
if (ioba < offset)
979977
return -EINVAL;
980978

981-
if ((ioba + npages) > (tbl->it_offset + tbl->it_size))
979+
if ((ioba + 1) > (offset + size))
982980
return -EINVAL;
983981

984982
return 0;
985983
}
986-
EXPORT_SYMBOL_GPL(iommu_tce_clear_param_check);
984+
EXPORT_SYMBOL_GPL(iommu_tce_check_ioba);
987985

988-
int iommu_tce_put_param_check(struct iommu_table *tbl,
989-
unsigned long ioba, unsigned long tce)
986+
int iommu_tce_check_gpa(unsigned long page_shift, unsigned long gpa)
990987
{
991-
if (tce & ~IOMMU_PAGE_MASK(tbl))
992-
return -EINVAL;
993-
994-
if (ioba & ~IOMMU_PAGE_MASK(tbl))
995-
return -EINVAL;
996-
997-
ioba >>= tbl->it_page_shift;
998-
if (ioba < tbl->it_offset)
999-
return -EINVAL;
988+
unsigned long mask = (1UL << page_shift) - 1;
1000989

1001-
if ((ioba + 1) > (tbl->it_offset + tbl->it_size))
990+
if (gpa & mask)
1002991
return -EINVAL;
1003992

1004993
return 0;
1005994
}
1006-
EXPORT_SYMBOL_GPL(iommu_tce_put_param_check);
995+
EXPORT_SYMBOL_GPL(iommu_tce_check_gpa);
1007996

1008997
long iommu_tce_xchg(struct iommu_table *tbl, unsigned long entry,
1009998
unsigned long *hpa, enum dma_data_direction *direction)

arch/powerpc/kvm/book3s_64_vio_hv.c

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,6 @@ struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm *kvm,
6161
}
6262
EXPORT_SYMBOL_GPL(kvmppc_find_table);
6363

64-
/*
65-
* Validates IO address.
66-
*
67-
* WARNING: This will be called in real-mode on HV KVM and virtual
68-
* mode on PR KVM
69-
*/
70-
long kvmppc_ioba_validate(struct kvmppc_spapr_tce_table *stt,
71-
unsigned long ioba, unsigned long npages)
72-
{
73-
unsigned long mask = (1ULL << stt->page_shift) - 1;
74-
unsigned long idx = ioba >> stt->page_shift;
75-
76-
if ((ioba & mask) || (idx < stt->offset) ||
77-
(idx - stt->offset + npages > stt->size) ||
78-
(idx + npages < idx))
79-
return H_PARAMETER;
80-
81-
return H_SUCCESS;
82-
}
83-
EXPORT_SYMBOL_GPL(kvmppc_ioba_validate);
84-
8564
/*
8665
* Validates TCE address.
8766
* At the moment flags and page mask are validated.
@@ -95,10 +74,14 @@ EXPORT_SYMBOL_GPL(kvmppc_ioba_validate);
9574
*/
9675
long kvmppc_tce_validate(struct kvmppc_spapr_tce_table *stt, unsigned long tce)
9776
{
98-
unsigned long page_mask = ~((1ULL << stt->page_shift) - 1);
99-
unsigned long mask = ~(page_mask | TCE_PCI_WRITE | TCE_PCI_READ);
77+
unsigned long gpa = tce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
78+
enum dma_data_direction dir = iommu_tce_direction(tce);
79+
80+
/* Allow userspace to poison TCE table */
81+
if (dir == DMA_NONE)
82+
return H_SUCCESS;
10083

101-
if (tce & mask)
84+
if (iommu_tce_check_gpa(stt->page_shift, gpa))
10285
return H_PARAMETER;
10386

10487
return H_SUCCESS;

0 commit comments

Comments
 (0)