Skip to contentSkip to conversation

drivers/pci/endpoint/functions/pci-epf-test.c:237 pci_epf_test_read() warn: possible memory leak of 'buf'

PATCH New Dan Carpenter 1 participant 1 message Jul 13, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 3376/7334 drivers/pci/endpoint/functions/pci-epf-test.c:237 pci_epf_test_read() warn: possible memory leak of 'buf'

CC: kbuild-all(a)lists.01.org
TO: Peter Ujfalusi <[email protected]>

tree:   https://github.com/omap-audio/linux-audio peter/ti-linux-5.4.y/wip
head:   00adf26991b8e9e47c6dd6dbc7938a73428c2e5c
commit: d18c9a1ac4bbfff051665c64a8a068036f7e2b56 [3376/7334] PCI: endpoint: Add support to transfer data using DMA
config: i386-randconfig-m021-20200710 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-14) 9.3.0

If you fix the issue, kindly add following tag as appropriate

smatch warnings:
drivers/pci/endpoint/functions/pci-epf-test.c:237 pci_epf_test_read() warn: possible memory leak of 'buf'

# https://github.com/omap-audio/linux-audio/commit/d18c9a1ac4bbfff051665c64a8a068036f7e2b56
git remote add omap-audio https://github.com/omap-audio/linux-audio
git remote update omap-audio
git checkout d18c9a1ac4bbfff051665c64a8a068036f7e2b56
vim +/buf +237 drivers/pci/endpoint/functions/pci-epf-test.c

349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  158  static int pci_epf_test_read(struct pci_epf_test *epf_test)
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  159  {
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  160  	int ret;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  161  	void __iomem *src_addr;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  162  	void *buf;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  163  	u32 crc32;
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  164  	bool use_dma;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  165  	phys_addr_t phys_addr;
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  166  	phys_addr_t dst_phys_addr;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  167  	struct pci_epf *epf = epf_test->epf;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  168  	struct device *dev = &epf->dev;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  169  	struct pci_epc *epc = epf->epc;
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  170  	struct device *dma_dev = epf->epc->dev.parent;
3235b994950d84 Kishon Vijay Abraham I 2017-08-18  171  	enum pci_barno test_reg_bar = epf_test->test_reg_bar;
3235b994950d84 Kishon Vijay Abraham I 2017-08-18  172  	struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar];
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  173  
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  174  	src_addr = pci_epc_mem_alloc_addr(epc, &phys_addr, reg->size);
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  175  	if (!src_addr) {
798c0441bec8c4 Gustavo Pimentel       2018-05-14  176  		dev_err(dev, "Failed to allocate address\n");
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  177  		reg->status = STATUS_SRC_ADDR_INVALID;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  178  		ret = -ENOMEM;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  179  		goto err;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  180  	}
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  181  
fd0d7332e64978 Kishon Vijay Abraham I 2020-01-23  182  	ret = pci_epc_map_addr(epc, epf->func_no, epf->vfunc_no, phys_addr,
fd0d7332e64978 Kishon Vijay Abraham I 2020-01-23  183  			       reg->src_addr, reg->size);
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  184  	if (ret) {
798c0441bec8c4 Gustavo Pimentel       2018-05-14  185  		dev_err(dev, "Failed to map address\n");
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  186  		reg->status = STATUS_SRC_ADDR_INVALID;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  187  		goto err_addr;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  188  	}
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  189  
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  190  	buf = kzalloc(reg->size, GFP_KERNEL);
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  191  	if (!buf) {
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  192  		ret = -ENOMEM;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  193  		goto err_map_addr;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  194  	}
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  195  
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  196  	use_dma = !!(reg->flags & FLAG_USE_DMA);
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  197  	if (use_dma) {
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  198  		if (!epf_test->dma_supported) {
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  199  			dev_err(dev, "Cannot transfer data using DMA\n");
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  200  			ret = -EINVAL;
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  201  			goto err_map_addr;

This should be "err_dma_map". The label name is sort of confusing. It's better to choose a label name based on what the goto does "free_buf" so the gotos are readable. (Just like how function names say what the functions do instead of where they are called from).

d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  202  		}
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  203  
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  204  		dst_phys_addr = dma_map_single(dma_dev, buf, reg->size,
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  205  					       DMA_FROM_DEVICE);
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  206  		if (dma_mapping_error(dma_dev, dst_phys_addr)) {
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  207  			dev_err(dev, "Failed to map destination buffer addr\n");
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  208  			ret = -ENOMEM;
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  209  			goto err_dma_map;
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  210  		}
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  211  
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  212  		ret = pci_epf_data_transfer(epf, dst_phys_addr, phys_addr,
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  213  					    reg->size);
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  214  		if (ret)
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  215  			dev_err(dev, "Data transfer failed\n");
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  216  
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  217  		dma_unmap_single(dma_dev, dst_phys_addr, reg->size,
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  218  				 DMA_FROM_DEVICE);
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  219  	} else {
726dabfde6aa35 Wen Yang               2019-02-11  220  		memcpy_fromio(buf, src_addr, reg->size);
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  221  	}
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  222  
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  223  	crc32 = crc32_le(~0, buf, reg->size);
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  224  	if (crc32 != reg->checksum)
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  225  		ret = -EIO;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  226  
d18c9a1ac4bbff Kishon Vijay Abraham I 2020-02-04  227  err_dma_map:
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  228  	kfree(buf);
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  229  
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  230  err_map_addr:
fd0d7332e64978 Kishon Vijay Abraham I 2020-01-23  231  	pci_epc_unmap_addr(epc, epf->func_no, epf->vfunc_no, phys_addr);
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  232  
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  233  err_addr:
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  234  	pci_epc_mem_free_addr(epc, phys_addr, src_addr, reg->size);
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  235  
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27  236  err:

349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27 @237 return ret;
349e7a85b25fa6 Kishon Vijay Abraham I 2017-03-27 238 }

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org