[PATCH] i2c: fix reference leak in MP2 PCI device

Ma Ke posted 1 patch 4 days, 9 hours ago
There is a newer version of this series
drivers/i2c/busses/i2c-amd-mp2-pci.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] i2c: fix reference leak in MP2 PCI device
Posted by Ma Ke 4 days, 9 hours ago
In i2c_amd_probe(), amd_mp2_find_device() utilizes
driver_find_next_device() which internally calls driver_find_device()
to locate the matching device. driver_find_device() increments the
reference count of the found device by calling get_device(), but
amd_mp2_find_device() fails to call put_device() to decrement the
reference count before returning. This results in a reference count
leak of the PCI device each time i2c_amd_probe() is executed, which
may prevent the device from being properly released and cause a memory
leak.

Found by code review.

Cc: stable@vger.kernel.org
Fixes: 529766e0a011 ("i2c: Add drivers for the AMD PCIe MP2 I2C controller")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
 drivers/i2c/busses/i2c-amd-mp2-pci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/i2c/busses/i2c-amd-mp2-pci.c b/drivers/i2c/busses/i2c-amd-mp2-pci.c
index ef7370d3dbea..1f01d3f121ba 100644
--- a/drivers/i2c/busses/i2c-amd-mp2-pci.c
+++ b/drivers/i2c/busses/i2c-amd-mp2-pci.c
@@ -464,7 +464,9 @@ struct amd_mp2_dev *amd_mp2_find_device(void)
 		return NULL;
 
 	pci_dev = to_pci_dev(dev);
-	return (struct amd_mp2_dev *)pci_get_drvdata(pci_dev);
+	mp2_dev = (struct amd_mp2_dev *)pci_get_drvdata(pci_dev);
+	put_device(dev);
+	return mp2_dev;
 }
 EXPORT_SYMBOL_GPL(amd_mp2_find_device);
 
-- 
2.17.1
Re: [PATCH] i2c: fix reference leak in MP2 PCI device
Posted by kernel test robot 3 days, 14 hours ago
Hi Ma,

kernel test robot noticed the following build errors:

[auto build test ERROR on andi-shyti/i2c/i2c-host]
[also build test ERROR on linus/master v6.17-rc7 next-20250926]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Ma-Ke/i2c-fix-reference-leak-in-MP2-PCI-device/20250927-190047
base:   https://git.kernel.org/pub/scm/linux/kernel/git/andi.shyti/linux.git i2c/i2c-host
patch link:    https://lore.kernel.org/r/20250927105729.19164-1-make24%40iscas.ac.cn
patch subject: [PATCH] i2c: fix reference leak in MP2 PCI device
config: i386-randconfig-011-20250928 (https://download.01.org/0day-ci/archive/20250928/202509281317.3OLDoUnB-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250928/202509281317.3OLDoUnB-lkp@intel.com/reproduce)

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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202509281317.3OLDoUnB-lkp@intel.com/

All errors (new ones prefixed by >>):

>> drivers/i2c/busses/i2c-amd-mp2-pci.c:467:2: error: use of undeclared identifier 'mp2_dev'
     467 |         mp2_dev = (struct amd_mp2_dev *)pci_get_drvdata(pci_dev);
         |         ^
   drivers/i2c/busses/i2c-amd-mp2-pci.c:469:9: error: use of undeclared identifier 'mp2_dev'; did you mean 'pci_dev'?
     469 |         return mp2_dev;
         |                ^~~~~~~
         |                pci_dev
   drivers/i2c/busses/i2c-amd-mp2-pci.c:460:18: note: 'pci_dev' declared here
     460 |         struct pci_dev *pci_dev;
         |                         ^
>> drivers/i2c/busses/i2c-amd-mp2-pci.c:469:9: error: incompatible pointer types returning 'struct pci_dev *' from a function with result type 'struct amd_mp2_dev *' [-Werror,-Wincompatible-pointer-types]
     469 |         return mp2_dev;
         |                ^~~~~~~
   3 errors generated.


vim +/mp2_dev +467 drivers/i2c/busses/i2c-amd-mp2-pci.c

   456	
   457	struct amd_mp2_dev *amd_mp2_find_device(void)
   458	{
   459		struct device *dev;
   460		struct pci_dev *pci_dev;
   461	
   462		dev = driver_find_next_device(&amd_mp2_pci_driver.driver, NULL);
   463		if (!dev)
   464			return NULL;
   465	
   466		pci_dev = to_pci_dev(dev);
 > 467		mp2_dev = (struct amd_mp2_dev *)pci_get_drvdata(pci_dev);
   468		put_device(dev);
 > 469		return mp2_dev;
   470	}
   471	EXPORT_SYMBOL_GPL(amd_mp2_find_device);
   472	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] i2c: fix reference leak in MP2 PCI device
Posted by kernel test robot 3 days, 16 hours ago
Hi Ma,

kernel test robot noticed the following build warnings:

[auto build test WARNING on andi-shyti/i2c/i2c-host]
[also build test WARNING on linus/master v6.17-rc7 next-20250926]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Ma-Ke/i2c-fix-reference-leak-in-MP2-PCI-device/20250927-190047
base:   https://git.kernel.org/pub/scm/linux/kernel/git/andi.shyti/linux.git i2c/i2c-host
patch link:    https://lore.kernel.org/r/20250927105729.19164-1-make24%40iscas.ac.cn
patch subject: [PATCH] i2c: fix reference leak in MP2 PCI device
config: i386-randconfig-013-20250928 (https://download.01.org/0day-ci/archive/20250928/202509281110.ACsS1CDz-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250928/202509281110.ACsS1CDz-lkp@intel.com/reproduce)

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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202509281110.ACsS1CDz-lkp@intel.com/

All warnings (new ones prefixed by >>):

   drivers/i2c/busses/i2c-amd-mp2-pci.c: In function 'amd_mp2_find_device':
   drivers/i2c/busses/i2c-amd-mp2-pci.c:467:9: error: 'mp2_dev' undeclared (first use in this function)
     467 |         mp2_dev = (struct amd_mp2_dev *)pci_get_drvdata(pci_dev);
         |         ^~~~~~~
   drivers/i2c/busses/i2c-amd-mp2-pci.c:467:9: note: each undeclared identifier is reported only once for each function it appears in
>> drivers/i2c/busses/i2c-amd-mp2-pci.c:470:1: warning: control reaches end of non-void function [-Wreturn-type]
     470 | }
         | ^


vim +470 drivers/i2c/busses/i2c-amd-mp2-pci.c

529766e0a01144 Elie Morisse     2019-03-05  456  
529766e0a01144 Elie Morisse     2019-03-05  457  struct amd_mp2_dev *amd_mp2_find_device(void)
529766e0a01144 Elie Morisse     2019-03-05  458  {
529766e0a01144 Elie Morisse     2019-03-05  459  	struct device *dev;
529766e0a01144 Elie Morisse     2019-03-05  460  	struct pci_dev *pci_dev;
529766e0a01144 Elie Morisse     2019-03-05  461  
6bf85ba9e55f65 Suzuki K Poulose 2019-07-23  462  	dev = driver_find_next_device(&amd_mp2_pci_driver.driver, NULL);
529766e0a01144 Elie Morisse     2019-03-05  463  	if (!dev)
529766e0a01144 Elie Morisse     2019-03-05  464  		return NULL;
529766e0a01144 Elie Morisse     2019-03-05  465  
529766e0a01144 Elie Morisse     2019-03-05  466  	pci_dev = to_pci_dev(dev);
10a231ddf6f43e Ma Ke            2025-09-27  467  	mp2_dev = (struct amd_mp2_dev *)pci_get_drvdata(pci_dev);
10a231ddf6f43e Ma Ke            2025-09-27  468  	put_device(dev);
10a231ddf6f43e Ma Ke            2025-09-27  469  	return mp2_dev;
529766e0a01144 Elie Morisse     2019-03-05 @470  }
529766e0a01144 Elie Morisse     2019-03-05  471  EXPORT_SYMBOL_GPL(amd_mp2_find_device);
529766e0a01144 Elie Morisse     2019-03-05  472  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] i2c: fix reference leak in MP2 PCI device
Posted by Markus Elfring 4 days, 7 hours ago
> In i2c_amd_probe(), amd_mp2_find_device() utilizes
> driver_find_next_device() which internally calls driver_find_device()
> to locate the matching device. driver_find_device() increments the
> reference count of the found device by calling get_device(), but
> amd_mp2_find_device() fails to call put_device() to decrement the
> reference count before returning. This results in a reference count
> leak of the PCI device each time i2c_amd_probe() is executed, which
> may prevent the device from being properly released and cause a memory
> leak.

How do you think about to increase the application of scope-based resource management?
https://elixir.bootlin.com/linux/v6.17-rc7/source/include/linux/device.h#L1180

Regards,
Markus
Re: [PATCH] i2c: fix reference leak in MP2 PCI device
Posted by Greg KH 4 days, 2 hours ago
On Sat, Sep 27, 2025 at 02:56:49PM +0200, Markus Elfring wrote:
> > In i2c_amd_probe(), amd_mp2_find_device() utilizes
> > driver_find_next_device() which internally calls driver_find_device()
> > to locate the matching device. driver_find_device() increments the
> > reference count of the found device by calling get_device(), but
> > amd_mp2_find_device() fails to call put_device() to decrement the
> > reference count before returning. This results in a reference count
> > leak of the PCI device each time i2c_amd_probe() is executed, which
> > may prevent the device from being properly released and cause a memory
> > leak.
> 
> How do you think about to increase the application of scope-based resource management?
> https://elixir.bootlin.com/linux/v6.17-rc7/source/include/linux/device.h#L1180
> 
> Regards,
> Markus
> 

Hi,

This is the semi-friendly patch-bot of Greg Kroah-Hartman.

Markus, you seem to have sent a nonsensical or otherwise pointless
review comment to a patch submission on a Linux kernel developer mailing
list.  I strongly suggest that you not do this anymore.  Please do not
bother developers who are actively working to produce patches and
features with comments that, in the end, are a waste of time.

Patch submitter, please ignore Markus's suggestion; you do not need to
follow it at all.  The person/bot/AI that sent it is being ignored by
almost all Linux kernel maintainers for having a persistent pattern of
behavior of producing distracting and pointless commentary, and
inability to adapt to feedback.  Please feel free to also ignore emails
from them.

thanks,

greg k-h's patch email bot