Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Damien Le Moal and committed by
Manivannan Sadhasivam
2314c6ff ca3c342f

+6 -3
+6 -3
drivers/pci/endpoint/pci-epc-mem.c
··· 178 178 void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc, 179 179 phys_addr_t *phys_addr, size_t size) 180 180 { 181 - void __iomem *virt_addr = NULL; 181 + void __iomem *virt_addr; 182 182 struct pci_epc_mem *mem; 183 183 unsigned int page_shift; 184 184 size_t align_size; ··· 188 188 189 189 for (i = 0; i < epc->num_windows; i++) { 190 190 mem = epc->windows[i]; 191 - mutex_lock(&mem->lock); 191 + if (size > mem->window.size) 192 + continue; 193 + 192 194 align_size = ALIGN(size, mem->window.page_size); 193 195 order = pci_epc_mem_get_order(mem, align_size); 194 196 197 + mutex_lock(&mem->lock); 195 198 pageno = bitmap_find_free_region(mem->bitmap, mem->pages, 196 199 order); 197 200 if (pageno >= 0) { ··· 214 211 mutex_unlock(&mem->lock); 215 212 } 216 213 217 - return virt_addr; 214 + return NULL; 218 215 } 219 216 EXPORT_SYMBOL_GPL(pci_epc_mem_alloc_addr); 220 217