lib/vmd: check requested size before padding VMD's base address

The address was padded before the size of requested area was checked,
which for areas bigger than available space resulted in unnecessarily
padding the remaining area.

Change-Id: Ic52a6e30c1cbe2526ac7f3effc20279cd91cadbc
Signed-off-by: Konrad Sztyber <konrad.sztyber@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/475832
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Wojciech Malikowski <wojciech.malikowski@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
This commit is contained in:
Konrad Sztyber 2019-11-26 08:34:49 +01:00 committed by Tomasz Zawadzki
parent 2951c1010e
commit 6722e57e02

View File

@ -113,7 +113,7 @@ vmd_device_is_root_port(const struct vmd_pci_device *vmd_device)
static uint64_t static uint64_t
vmd_allocate_base_addr(struct vmd_adapter *vmd, struct vmd_pci_device *dev, uint32_t size) vmd_allocate_base_addr(struct vmd_adapter *vmd, struct vmd_pci_device *dev, uint32_t size)
{ {
uint64_t base_address = 0; uint64_t base_address = 0, padding = 0;
struct vmd_pci_bus *hp_bus; struct vmd_pci_bus *hp_bus;
if (size && ((size & (~size + 1)) != size)) { if (size && ((size & (~size + 1)) != size)) {
@ -135,16 +135,14 @@ vmd_allocate_base_addr(struct vmd_adapter *vmd, struct vmd_pci_device *dev, uint
/* Ensure physical membar allocated is size aligned */ /* Ensure physical membar allocated is size aligned */
if (vmd->physical_addr & (size - 1)) { if (vmd->physical_addr & (size - 1)) {
uint32_t pad = size - (vmd->physical_addr & (size - 1)); padding = size - (vmd->physical_addr & (size - 1));
vmd->physical_addr += pad;
vmd->current_addr_size -= pad;
} }
/* Allocate from membar if enough memory is left */ /* Allocate from membar if enough memory is left */
if (vmd->current_addr_size >= size) { if (vmd->current_addr_size >= size + padding) {
base_address = vmd->physical_addr; base_address = vmd->physical_addr + padding;
vmd->physical_addr += size; vmd->physical_addr += size + padding;
vmd->current_addr_size -= size; vmd->current_addr_size -= size + padding;
} }
SPDK_DEBUGLOG(SPDK_LOG_VMD, "allocated(size) %lx (%x)\n", base_address, size); SPDK_DEBUGLOG(SPDK_LOG_VMD, "allocated(size) %lx (%x)\n", base_address, size);