Skip to content

Commit 2314c6f

Browse files
damien-lemoalMani-Sadhasivam
authored andcommitted
PCI: endpoint: Improve pci_epc_mem_alloc_addr()
There is no point in attempting to allocate memory from an endpoint controller memory window if the requested size is larger than the memory window size. Add a check to skip bitmap_find_free_region() calls for such case. This check can be done without the mem->lock mutex held as memory window sizes are constant and never modified at runtime. Also change the final return to return NULL to simplify the code. Signed-off-by: Damien Le Moal <dlemoal@kernel.org> Reviewed-by: Niklas Cassel <cassel@kernel.org> Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> Link: https://lore.kernel.org/r/20241012113246.95634-3-dlemoal@kernel.org Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
1 parent ca3c342 commit 2314c6f

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

drivers/pci/endpoint/pci-epc-mem.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ EXPORT_SYMBOL_GPL(pci_epc_mem_exit);
178178
void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc,
179179
phys_addr_t *phys_addr, size_t size)
180180
{
181-
void __iomem *virt_addr = NULL;
181+
void __iomem *virt_addr;
182182
struct pci_epc_mem *mem;
183183
unsigned int page_shift;
184184
size_t align_size;
@@ -188,10 +188,13 @@ void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc,
188188

189189
for (i = 0; i < epc->num_windows; i++) {
190190
mem = epc->windows[i];
191-
mutex_lock(&mem->lock);
191+
if (size > mem->window.size)
192+
continue;
193+
192194
align_size = ALIGN(size, mem->window.page_size);
193195
order = pci_epc_mem_get_order(mem, align_size);
194196

197+
mutex_lock(&mem->lock);
195198
pageno = bitmap_find_free_region(mem->bitmap, mem->pages,
196199
order);
197200
if (pageno >= 0) {
@@ -211,7 +214,7 @@ void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc,
211214
mutex_unlock(&mem->lock);
212215
}
213216

214-
return virt_addr;
217+
return NULL;
215218
}
216219
EXPORT_SYMBOL_GPL(pci_epc_mem_alloc_addr);
217220

0 commit comments

Comments
 (0)