Skip to contentSkip to conversation

drivers: fix a memory leak

PATCH In review [email protected] 3 participants 3 messages Nov 7, 2019 – Nov 9, 2019 Agpgart driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers: fix a memory leak

From: Qiushi Wu <[email protected]>

In intel_gtt_setup_scratch_page(), "page" is not released if pci_dma_mapping_error() return errors. This will lead to memory leak. Fix this issue by freeing "page" before return.

---
 drivers/char/agp/intel-gtt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/char/agp/intel-gtt.c +3 -1
index c6271ce250b3..bd38b179157c 100644
@@ -304,8 +304,10 @@ static int intel_gtt_setup_scratch_page(void)
 	if (intel_private.needs_dmar) {
 		dma_addr = pci_map_page(intel_private.pcidev, page, 0,
 				    PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
-		if (pci_dma_mapping_error(intel_private.pcidev, dma_addr))
+		if (pci_dma_mapping_error(intel_private.pcidev, dma_addr)) {
+			__free_pages(page);
 			return -EINVAL;
+		}
 
 		intel_private.scratch_page_dma = dma_addr;
 	} else
Signature · 1 line
2.17.1
#2 lore raw
In reply to [email protected]
Thank you for the patch! Yet something to improve:

Hi,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on char-misc/char-misc-testing]
[also build test ERROR on v5.4-rc6 next-20191108]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/wu000273-umn-edu/drivers-fix-a-memory-leak/20191109-191819
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc.git bce92136c28f3ae9ddbbac0ee7c0df2eb0ae0bef
config: x86_64-rhel (attached as .config)
compiler: gcc-7 (Debian 7.4.0-14) 7.4.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

drivers/char/agp/intel-gtt.c: In function 'intel_gtt_setup_scratch_page':

>> drivers/char/agp/intel-gtt.c:308:4: error: too few arguments to function '__free_pages'
       __free_pages(page);
       ^~~~~~~~~~~~
   In file included from include/linux/umh.h:4:0,
                    from include/linux/kmod.h:9,
                    from include/linux/module.h:13,
                    from drivers/char/agp/intel-gtt.c:18:
   include/linux/gfp.h:543:13: note: declared here
    extern void __free_pages(struct page *page, unsigned int order);
                ^~~~~~~~~~~~

vim +/__free_pages +308 drivers/char/agp/intel-gtt.c

293
294 static int intel_gtt_setup_scratch_page(void)
295 {
296 struct page *page;
297 dma_addr_t dma_addr;
298
299 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
300 if (page == NULL)
301 return -ENOMEM;
302 set_pages_uc(page, 1);
303
304 if (intel_private.needs_dmar) {
305 dma_addr = pci_map_page(intel_private.pcidev, page, 0,
306 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
307 if (pci_dma_mapping_error(intel_private.pcidev, dma_addr)) {

 > 308				__free_pages(page);

309 return -EINVAL;
310 }
311
312 intel_private.scratch_page_dma = dma_addr;
313 } else
314 intel_private.scratch_page_dma = page_to_phys(page);
315
316 intel_private.scratch_page = page;
317
318 return 0;
319 }
320

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation