From 6722e57e02a74546f61612552d8605c23554c195 Mon Sep 17 00:00:00 2001 From: Konrad Sztyber Date: Tue, 26 Nov 2019 08:34:49 +0100 Subject: [PATCH] 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 Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/475832 Tested-by: SPDK CI Jenkins Reviewed-by: Wojciech Malikowski Reviewed-by: Jim Harris Reviewed-by: Shuhei Matsumoto --- lib/vmd/vmd.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/vmd/vmd.c b/lib/vmd/vmd.c index a60e64079..f400b6c05 100644 --- a/lib/vmd/vmd.c +++ b/lib/vmd/vmd.c @@ -113,7 +113,7 @@ vmd_device_is_root_port(const struct vmd_pci_device *vmd_device) static uint64_t 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; 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 */ if (vmd->physical_addr & (size - 1)) { - uint32_t pad = size - (vmd->physical_addr & (size - 1)); - vmd->physical_addr += pad; - vmd->current_addr_size -= pad; + padding = size - (vmd->physical_addr & (size - 1)); } /* Allocate from membar if enough memory is left */ - if (vmd->current_addr_size >= size) { - base_address = vmd->physical_addr; - vmd->physical_addr += size; - vmd->current_addr_size -= size; + if (vmd->current_addr_size >= size + padding) { + base_address = vmd->physical_addr + padding; + vmd->physical_addr += size + padding; + vmd->current_addr_size -= size + padding; } SPDK_DEBUGLOG(SPDK_LOG_VMD, "allocated(size) %lx (%x)\n", base_address, size);