qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

vfio/spapr: Fix page size calculation

Coverity detected an issue (CID 1421903) with potential call of clz64(0)
which returns 64 which make it do "<<" with a negative number.

This checks the mask and avoids undefined behaviour.

In practice pgsizes and memory_region_iommu_get_min_page_size() always
have some common page sizes and even if they did not, the resulting page
size would be 0x8000.0000.0000.0000 (gcc 9.2) and
ioctl(VFIO_IOMMU_SPAPR_TCE_CREATE) would fail anyway.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Message-Id: <20200324063912.25063-1-aik@ozlabs.ru>
Reviewed-by: Greg Kurz <groug@kaod.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>

authored by

Alexey Kardashevskiy and committed by
David Gibson
79178edd 3b4f50bd

+3 -3
+3 -3
hw/vfio/spapr.c
··· 147 147 { 148 148 int ret = 0; 149 149 IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); 150 - uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr); 150 + uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr), pgmask; 151 151 unsigned entries, bits_total, bits_per_level, max_levels; 152 152 struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) }; 153 153 long rampagesize = qemu_minrampagesize(); ··· 159 159 if (pagesize > rampagesize) { 160 160 pagesize = rampagesize; 161 161 } 162 - pagesize = 1ULL << (63 - clz64(container->pgsizes & 163 - (pagesize | (pagesize - 1)))); 162 + pgmask = container->pgsizes & (pagesize | (pagesize - 1)); 163 + pagesize = pgmask ? (1ULL << (63 - clz64(pgmask))) : 0; 164 164 if (!pagesize) { 165 165 error_report("Host doesn't support page size 0x%"PRIx64 166 166 ", the supported mask is 0x%lx",