Skip to contentSkip to conversation

arch/x86/events/zhaoxin/uncore.c:1608 uncore_pci_probe() warn: possible memory leak of 'boxes'

PATCH New Dan Carpenter 1 participant 1 message Jan 29, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 27108/30000 arch/x86/events/zhaoxin/uncore.c:1608 uncore_pci_probe() warn: possible memory leak of 'boxes'

tree:   https://gitee.com/openeuler/kernel.git OLK-5.10
head:   bb2503a5fcdf7f51d403ab34440c497acef3b4e7
commit: 2bb7fd043b4123c569fbbde1f18e98d7565a4997 [27108/30000] x86/perf: Update PMU support for more Zhaoxin CPU
config: x86_64-randconfig-161-20240124 (https://download.01.org/0day-ci/archive/20240127/[email protected]/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

New smatch warnings:
arch/x86/events/zhaoxin/uncore.c:1608 uncore_pci_probe() warn: possible memory leak of 'boxes'

vim +/boxes +1608 arch/x86/events/zhaoxin/uncore.c

2bb7fd043b4123 leoliu-oc 2023-10-26  1571  static int uncore_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2bb7fd043b4123 leoliu-oc 2023-10-26  1572  {
2bb7fd043b4123 leoliu-oc 2023-10-26  1573  	struct zhaoxin_uncore_type *type;
2bb7fd043b4123 leoliu-oc 2023-10-26  1574  	struct zhaoxin_uncore_pmu *pmu;
2bb7fd043b4123 leoliu-oc 2023-10-26  1575  	struct zhaoxin_uncore_box *box;
2bb7fd043b4123 leoliu-oc 2023-10-26  1576  	struct zhaoxin_uncore_box **boxes;
2bb7fd043b4123 leoliu-oc 2023-10-26  1577  
2bb7fd043b4123 leoliu-oc 2023-10-26  1578  	int loop, i, j = 0;
2bb7fd043b4123 leoliu-oc 2023-10-26  1579  
2bb7fd043b4123 leoliu-oc 2023-10-26  1580  	int subnode_id, ret = 0;
2bb7fd043b4123 leoliu-oc 2023-10-26  1581  
2bb7fd043b4123 leoliu-oc 2023-10-26  1582  	subnode_id = uncore_pcibus_to_subnodeid(pdev->bus);
2bb7fd043b4123 leoliu-oc 2023-10-26  1583  	if (subnode_id < 0)
2bb7fd043b4123 leoliu-oc 2023-10-26  1584  		return -EINVAL;
2bb7fd043b4123 leoliu-oc 2023-10-26  1585  
2bb7fd043b4123 leoliu-oc 2023-10-26  1586  	type = uncore_pci_uncores[UNCORE_PCI_DEV_TYPE(id->driver_data)];
2bb7fd043b4123 leoliu-oc 2023-10-26  1587  
2bb7fd043b4123 leoliu-oc 2023-10-26  1588  	if (!strcmp(type->name, "mc0"))
2bb7fd043b4123 leoliu-oc 2023-10-26  1589  		loop = 2;
2bb7fd043b4123 leoliu-oc 2023-10-26  1590  	else
2bb7fd043b4123 leoliu-oc 2023-10-26  1591  		loop = 1;
2bb7fd043b4123 leoliu-oc 2023-10-26  1592  
2bb7fd043b4123 leoliu-oc 2023-10-26  1593  	boxes = kzalloc(loop * sizeof(struct zhaoxin_uncore_box *), GFP_KERNEL);
2bb7fd043b4123 leoliu-oc 2023-10-26  1594  
2bb7fd043b4123 leoliu-oc 2023-10-26  1595  	for (i = 0; i < loop; i++) {
2bb7fd043b4123 leoliu-oc 2023-10-26  1596  		type = uncore_pci_uncores[UNCORE_PCI_DEV_TYPE(id->driver_data) + j];
2bb7fd043b4123 leoliu-oc 2023-10-26  1597  
2bb7fd043b4123 leoliu-oc 2023-10-26  1598  		if (!type)
2bb7fd043b4123 leoliu-oc 2023-10-26  1599  			continue;
2bb7fd043b4123 leoliu-oc 2023-10-26  1600  		/*
2bb7fd043b4123 leoliu-oc 2023-10-26  1601  		 * for performance monitoring unit with multiple boxes,
2bb7fd043b4123 leoliu-oc 2023-10-26  1602  		 * each box has a different function id.
2bb7fd043b4123 leoliu-oc 2023-10-26  1603  		 */
2bb7fd043b4123 leoliu-oc 2023-10-26  1604  
2bb7fd043b4123 leoliu-oc 2023-10-26  1605  		pmu = &type->pmus[UNCORE_PCI_DEV_IDX(id->driver_data)];
2bb7fd043b4123 leoliu-oc 2023-10-26  1606  
2bb7fd043b4123 leoliu-oc 2023-10-26  1607  		if (WARN_ON_ONCE(pmu->boxes[subnode_id] != NULL))

2bb7fd043b4123 leoliu-oc 2023-10-26 @1608 return -EINVAL;

Static checker wants some cleanup before returning.

2bb7fd043b4123 leoliu-oc 2023-10-26  1609  		box = uncore_alloc_box(type, NUMA_NO_NODE);
2bb7fd043b4123 leoliu-oc 2023-10-26  1610  		if (!box)
2bb7fd043b4123 leoliu-oc 2023-10-26  1611  			return -ENOMEM;
2bb7fd043b4123 leoliu-oc 2023-10-26  1612  
2bb7fd043b4123 leoliu-oc 2023-10-26  1613  		if (pmu->func_id < 0)
2bb7fd043b4123 leoliu-oc 2023-10-26  1614  			pmu->func_id = pdev->devfn;
2bb7fd043b4123 leoliu-oc 2023-10-26  1615  		else
2bb7fd043b4123 leoliu-oc 2023-10-26  1616  			WARN_ON_ONCE(pmu->func_id != pdev->devfn);
2bb7fd043b4123 leoliu-oc 2023-10-26  1617  
2bb7fd043b4123 leoliu-oc 2023-10-26  1618  		atomic_inc(&box->refcnt);
2bb7fd043b4123 leoliu-oc 2023-10-26  1619  		box->subnode_id = subnode_id;
2bb7fd043b4123 leoliu-oc 2023-10-26  1620  		box->pci_dev = pdev;
2bb7fd043b4123 leoliu-oc 2023-10-26  1621  		box->pmu = pmu;
2bb7fd043b4123 leoliu-oc 2023-10-26  1622  		uncore_box_init(box);
2bb7fd043b4123 leoliu-oc 2023-10-26  1623  		boxes[i] = box;
2bb7fd043b4123 leoliu-oc 2023-10-26  1624  
2bb7fd043b4123 leoliu-oc 2023-10-26  1625  		pci_set_drvdata(pdev, boxes);
2bb7fd043b4123 leoliu-oc 2023-10-26  1626  		pmu->boxes[subnode_id] = box;
2bb7fd043b4123 leoliu-oc 2023-10-26  1627  		if (atomic_inc_return(&pmu->activeboxes) > 1) {
2bb7fd043b4123 leoliu-oc 2023-10-26  1628  			if (!strcmp(type->name, "mc0"))
2bb7fd043b4123 leoliu-oc 2023-10-26  1629  				goto next_loop;
2bb7fd043b4123 leoliu-oc 2023-10-26  1630  			else
2bb7fd043b4123 leoliu-oc 2023-10-26  1631  				return 0;
2bb7fd043b4123 leoliu-oc 2023-10-26  1632  		}
2bb7fd043b4123 leoliu-oc 2023-10-26  1633  		/* First active box registers the pmu */
2bb7fd043b4123 leoliu-oc 2023-10-26  1634  		ret = uncore_pmu_register(pmu);
2bb7fd043b4123 leoliu-oc 2023-10-26  1635  		if (ret) {
2bb7fd043b4123 leoliu-oc 2023-10-26  1636  			pci_set_drvdata(pdev, NULL);
2bb7fd043b4123 leoliu-oc 2023-10-26  1637  			pmu->boxes[subnode_id] = NULL;
2bb7fd043b4123 leoliu-oc 2023-10-26  1638  			uncore_box_exit(box);
2bb7fd043b4123 leoliu-oc 2023-10-26  1639  			kfree(box);

The cleanup here looks nice.

2bb7fd043b4123 leoliu-oc 2023-10-26  1640  		}
2bb7fd043b4123 leoliu-oc 2023-10-26  1641  next_loop:
2bb7fd043b4123 leoliu-oc 2023-10-26  1642  		j++;
2bb7fd043b4123 leoliu-oc 2023-10-26  1643  	}
2bb7fd043b4123 leoliu-oc 2023-10-26  1644  	return ret;
2bb7fd043b4123 leoliu-oc 2023-10-26  1645  }
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki