2019-11-22 19:35:28

by Navid Emamdoost

[permalink] [raw]
Subject: [PATCH] PCI/IOV: Fix memory leak in pci_iov_add_virtfn()

In the implementation of pci_iov_add_virtfn() the allocated virtfn is
leaked if pci_setup_device() fails. The error handling is not calling
pci_stop_and_remove_bus_device(). Change the goto label to failed2.

Fixes: 156c55325d30 ("PCI: Check for pci_setup_device() failure in pci_iov_add_virtfn()")
Signed-off-by: Navid Emamdoost <[email protected]>
---
drivers/pci/iov.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index b3f972e8cfed..713660482feb 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -164,7 +164,7 @@ int pci_iov_add_virtfn(struct pci_dev *dev, int id)

rc = pci_setup_device(virtfn);
if (rc)
- goto failed1;
+ goto failed2;

virtfn->dev.parent = dev->dev.parent;
virtfn->multifunction = 0;
--
2.17.1


2019-11-25 18:55:17

by Nathan Chancellor

[permalink] [raw]
Subject: Re: [PATCH] PCI/IOV: Fix memory leak in pci_iov_add_virtfn()

On Fri, Nov 22, 2019 at 01:31:36PM -0600, Navid Emamdoost wrote:
> In the implementation of pci_iov_add_virtfn() the allocated virtfn is
> leaked if pci_setup_device() fails. The error handling is not calling
> pci_stop_and_remove_bus_device(). Change the goto label to failed2.
>
> Fixes: 156c55325d30 ("PCI: Check for pci_setup_device() failure in pci_iov_add_virtfn()")
> Signed-off-by: Navid Emamdoost <[email protected]>
> ---
> drivers/pci/iov.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
> index b3f972e8cfed..713660482feb 100644
> --- a/drivers/pci/iov.c
> +++ b/drivers/pci/iov.c
> @@ -164,7 +164,7 @@ int pci_iov_add_virtfn(struct pci_dev *dev, int id)
>
> rc = pci_setup_device(virtfn);
> if (rc)
> - goto failed1;
> + goto failed2;
>
> virtfn->dev.parent = dev->dev.parent;
> virtfn->multifunction = 0;
> --
> 2.17.1
>

Hi Navid,

This patch causes a Clang warning about failed1 no longer being a used
label, as shown by this 0day build report. Would you please look into it
and address it in the same patch so there is not a warning regression?

Cheers,
Nathan

On Mon, Nov 25, 2019 at 07:20:46AM +0800, kbuild test robot wrote:
> CC: [email protected]
> In-Reply-To: <[email protected]>
> References: <[email protected]>
> TO: Navid Emamdoost <[email protected]>
> CC: Bjorn Helgaas <[email protected]>, [email protected], [email protected], [email protected], Navid Emamdoost <[email protected]>, [email protected], Navid Emamdoost <[email protected]>
> CC: [email protected], Navid Emamdoost <[email protected]>
>
> Hi Navid,
>
> Thank you for the patch! Perhaps something to improve:
>
> [auto build test WARNING on pci/next]
> [also build test WARNING on v5.4-rc8 next-20191122]
> [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/Navid-Emamdoost/PCI-IOV-Fix-memory-leak-in-pci_iov_add_virtfn/20191125-020946
> base: https://git.kernel.org/pub/scm/linux/kernel/git/helgaas/pci.git next
> config: arm64-defconfig (attached as .config)
> compiler: clang version 10.0.0 (git://gitmirror/llvm_project 844d97f650a2d716e63e3be903c32a82f2f817b1)
> reproduce:
> wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
> chmod +x ~/bin/make.cross
> # save the attached .config to linux build tree
> make.cross ARCH=arm64
>
> If you fix the issue, kindly add following tag
> Reported-by: kbuild test robot <[email protected]>
>
> All warnings (new ones prefixed by >>):
>
> >> drivers/pci/iov.c:204:1: warning: unused label 'failed1' [-Wunused-label]
> failed1:
> ^~~~~~~~
> 1 warning generated.
>
> vim +/failed1 +204 drivers/pci/iov.c
>
> cf0921bea66c556 KarimAllah Ahmed 2018-03-19 135
> 753f612471819d3 Jan H. Sch?nherr 2017-09-26 136 int pci_iov_add_virtfn(struct pci_dev *dev, int id)
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 137 {
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 138 int i;
> dc087f2f6a2925e Jiang Liu 2013-05-25 139 int rc = -ENOMEM;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 140 u64 size;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 141 char buf[VIRTFN_ID_LEN];
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 142 struct pci_dev *virtfn;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 143 struct resource *res;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 144 struct pci_sriov *iov = dev->sriov;
> 8b1fce04dc2a221 Gu Zheng 2013-05-25 145 struct pci_bus *bus;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 146
> b07579c0924eee1 Wei Yang 2015-03-25 147 bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
> dc087f2f6a2925e Jiang Liu 2013-05-25 148 if (!bus)
> dc087f2f6a2925e Jiang Liu 2013-05-25 149 goto failed;
> dc087f2f6a2925e Jiang Liu 2013-05-25 150
> dc087f2f6a2925e Jiang Liu 2013-05-25 151 virtfn = pci_alloc_dev(bus);
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 152 if (!virtfn)
> dc087f2f6a2925e Jiang Liu 2013-05-25 153 goto failed0;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 154
> b07579c0924eee1 Wei Yang 2015-03-25 155 virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 156 virtfn->vendor = dev->vendor;
> 3142d832af10d8c Filippo Sironi 2017-08-28 157 virtfn->device = iov->vf_device;
> cf0921bea66c556 KarimAllah Ahmed 2018-03-19 158 virtfn->is_virtfn = 1;
> cf0921bea66c556 KarimAllah Ahmed 2018-03-19 159 virtfn->physfn = pci_dev_get(dev);
> cf0921bea66c556 KarimAllah Ahmed 2018-03-19 160
> cf0921bea66c556 KarimAllah Ahmed 2018-03-19 161 if (id == 0)
> cf0921bea66c556 KarimAllah Ahmed 2018-03-19 162 pci_read_vf_config_common(virtfn);
> cf0921bea66c556 KarimAllah Ahmed 2018-03-19 163
> 156c55325d30261 Po Liu 2016-08-29 164 rc = pci_setup_device(virtfn);
> 156c55325d30261 Po Liu 2016-08-29 165 if (rc)
> 59fb9307eee20d6 Navid Emamdoost 2019-11-22 166 goto failed2;
> 156c55325d30261 Po Liu 2016-08-29 167
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 168 virtfn->dev.parent = dev->dev.parent;
> aa9319773619c9d Alex Williamson 2014-01-09 169 virtfn->multifunction = 0;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 170
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 171 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
> c1fe1f96e30d31c Bjorn Helgaas 2015-03-25 172 res = &dev->resource[i + PCI_IOV_RESOURCES];
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 173 if (!res->parent)
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 174 continue;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 175 virtfn->resource[i].name = pci_name(virtfn);
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 176 virtfn->resource[i].flags = res->flags;
> 0e6c9122a6ec96d Wei Yang 2015-03-25 177 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 178 virtfn->resource[i].start = res->start + size * id;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 179 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 180 rc = request_resource(res, &virtfn->resource[i]);
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 181 BUG_ON(rc);
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 182 }
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 183
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 184 pci_device_add(virtfn, virtfn->bus);
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 185
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 186 sprintf(buf, "virtfn%u", id);
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 187 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 188 if (rc)
> cf0921bea66c556 KarimAllah Ahmed 2018-03-19 189 goto failed2;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 190 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 191 if (rc)
> cf0921bea66c556 KarimAllah Ahmed 2018-03-19 192 goto failed3;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 193
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 194 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 195
> 27d6162944b9b34 Stuart Hayes 2017-10-04 196 pci_bus_add_device(virtfn);
> 27d6162944b9b34 Stuart Hayes 2017-10-04 197
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 198 return 0;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 199
> cf0921bea66c556 KarimAllah Ahmed 2018-03-19 200 failed3:
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 201 sysfs_remove_link(&dev->dev.kobj, buf);
> cf0921bea66c556 KarimAllah Ahmed 2018-03-19 202 failed2:
> cf0921bea66c556 KarimAllah Ahmed 2018-03-19 203 pci_stop_and_remove_bus_device(virtfn);
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 @204 failed1:
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 205 pci_dev_put(dev);
> dc087f2f6a2925e Jiang Liu 2013-05-25 206 failed0:
> dc087f2f6a2925e Jiang Liu 2013-05-25 207 virtfn_remove_bus(dev->bus, bus);
> dc087f2f6a2925e Jiang Liu 2013-05-25 208 failed:
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 209
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 210 return rc;
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 211 }
> dd7cc44d0bcec5e Yu Zhao 2009-03-20 212
>
> :::::: The code at line 204 was first introduced by commit
> :::::: dd7cc44d0bcec5e9c42fe52e88dc254ae62eac8d PCI: add SR-IOV API for Physical Function driver
>
> :::::: TO: Yu Zhao <[email protected]>
> :::::: CC: Jesse Barnes <[email protected]>
>
> ---
> 0-DAY kernel test infrastructure Open Source Technology Center
> https://lists.01.org/hyperkitty/list/[email protected] Intel Corporation

2019-11-25 19:34:57

by Navid Emamdoost

[permalink] [raw]
Subject: Re: [PATCH] PCI/IOV: Fix memory leak in pci_iov_add_virtfn()

Hi Nathan,

On Mon, Nov 25, 2019 at 12:04 PM Nathan Chancellor
<[email protected]> wrote:
>
> On Fri, Nov 22, 2019 at 01:31:36PM -0600, Navid Emamdoost wrote:
> > In the implementation of pci_iov_add_virtfn() the allocated virtfn is
> > leaked if pci_setup_device() fails. The error handling is not calling
> > pci_stop_and_remove_bus_device(). Change the goto label to failed2.
> >
> > Fixes: 156c55325d30 ("PCI: Check for pci_setup_device() failure in pci_iov_add_virtfn()")
> > Signed-off-by: Navid Emamdoost <[email protected]>
> > ---
> > drivers/pci/iov.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
> > index b3f972e8cfed..713660482feb 100644
> > --- a/drivers/pci/iov.c
> > +++ b/drivers/pci/iov.c
> > @@ -164,7 +164,7 @@ int pci_iov_add_virtfn(struct pci_dev *dev, int id)
> >
> > rc = pci_setup_device(virtfn);
> > if (rc)
> > - goto failed1;
> > + goto failed2;
> >
> > virtfn->dev.parent = dev->dev.parent;
> > virtfn->multifunction = 0;
> > --
> > 2.17.1
> >
>
> Hi Navid,
>
> This patch causes a Clang warning about failed1 no longer being a used
> label, as shown by this 0day build report. Would you please look into it
> and address it in the same patch so there is not a warning regression?
>

Sure I will prepare a v2.

> Cheers,
> Nathan
>
> On Mon, Nov 25, 2019 at 07:20:46AM +0800, kbuild test robot wrote:
> > CC: [email protected]
> > In-Reply-To: <[email protected]>
> > References: <[email protected]>
> > TO: Navid Emamdoost <[email protected]>
> > CC: Bjorn Helgaas <[email protected]>, [email protected], [email protected], [email protected], Navid Emamdoost <[email protected]>, [email protected], Navid Emamdoost <[email protected]>
> > CC: [email protected], Navid Emamdoost <[email protected]>
> >
> > Hi Navid,
> >
> > Thank you for the patch! Perhaps something to improve:
> >
> > [auto build test WARNING on pci/next]
> > [also build test WARNING on v5.4-rc8 next-20191122]
> > [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/Navid-Emamdoost/PCI-IOV-Fix-memory-leak-in-pci_iov_add_virtfn/20191125-020946
> > base: https://git.kernel.org/pub/scm/linux/kernel/git/helgaas/pci.git next
> > config: arm64-defconfig (attached as .config)
> > compiler: clang version 10.0.0 (git://gitmirror/llvm_project 844d97f650a2d716e63e3be903c32a82f2f817b1)
> > reproduce:
> > wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
> > chmod +x ~/bin/make.cross
> > # save the attached .config to linux build tree
> > make.cross ARCH=arm64
> >
> > If you fix the issue, kindly add following tag
> > Reported-by: kbuild test robot <[email protected]>
> >
> > All warnings (new ones prefixed by >>):
> >
> > >> drivers/pci/iov.c:204:1: warning: unused label 'failed1' [-Wunused-label]
> > failed1:
> > ^~~~~~~~
> > 1 warning generated.
> >
> > vim +/failed1 +204 drivers/pci/iov.c
> >
> > cf0921bea66c556 KarimAllah Ahmed 2018-03-19 135
> > 753f612471819d3 Jan H. Schönherr 2017-09-26 136 int pci_iov_add_virtfn(struct pci_dev *dev, int id)
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 137 {
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 138 int i;
> > dc087f2f6a2925e Jiang Liu 2013-05-25 139 int rc = -ENOMEM;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 140 u64 size;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 141 char buf[VIRTFN_ID_LEN];
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 142 struct pci_dev *virtfn;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 143 struct resource *res;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 144 struct pci_sriov *iov = dev->sriov;
> > 8b1fce04dc2a221 Gu Zheng 2013-05-25 145 struct pci_bus *bus;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 146
> > b07579c0924eee1 Wei Yang 2015-03-25 147 bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
> > dc087f2f6a2925e Jiang Liu 2013-05-25 148 if (!bus)
> > dc087f2f6a2925e Jiang Liu 2013-05-25 149 goto failed;
> > dc087f2f6a2925e Jiang Liu 2013-05-25 150
> > dc087f2f6a2925e Jiang Liu 2013-05-25 151 virtfn = pci_alloc_dev(bus);
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 152 if (!virtfn)
> > dc087f2f6a2925e Jiang Liu 2013-05-25 153 goto failed0;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 154
> > b07579c0924eee1 Wei Yang 2015-03-25 155 virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 156 virtfn->vendor = dev->vendor;
> > 3142d832af10d8c Filippo Sironi 2017-08-28 157 virtfn->device = iov->vf_device;
> > cf0921bea66c556 KarimAllah Ahmed 2018-03-19 158 virtfn->is_virtfn = 1;
> > cf0921bea66c556 KarimAllah Ahmed 2018-03-19 159 virtfn->physfn = pci_dev_get(dev);
> > cf0921bea66c556 KarimAllah Ahmed 2018-03-19 160
> > cf0921bea66c556 KarimAllah Ahmed 2018-03-19 161 if (id == 0)
> > cf0921bea66c556 KarimAllah Ahmed 2018-03-19 162 pci_read_vf_config_common(virtfn);
> > cf0921bea66c556 KarimAllah Ahmed 2018-03-19 163
> > 156c55325d30261 Po Liu 2016-08-29 164 rc = pci_setup_device(virtfn);
> > 156c55325d30261 Po Liu 2016-08-29 165 if (rc)
> > 59fb9307eee20d6 Navid Emamdoost 2019-11-22 166 goto failed2;
> > 156c55325d30261 Po Liu 2016-08-29 167
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 168 virtfn->dev.parent = dev->dev.parent;
> > aa9319773619c9d Alex Williamson 2014-01-09 169 virtfn->multifunction = 0;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 170
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 171 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
> > c1fe1f96e30d31c Bjorn Helgaas 2015-03-25 172 res = &dev->resource[i + PCI_IOV_RESOURCES];
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 173 if (!res->parent)
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 174 continue;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 175 virtfn->resource[i].name = pci_name(virtfn);
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 176 virtfn->resource[i].flags = res->flags;
> > 0e6c9122a6ec96d Wei Yang 2015-03-25 177 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 178 virtfn->resource[i].start = res->start + size * id;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 179 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 180 rc = request_resource(res, &virtfn->resource[i]);
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 181 BUG_ON(rc);
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 182 }
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 183
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 184 pci_device_add(virtfn, virtfn->bus);
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 185
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 186 sprintf(buf, "virtfn%u", id);
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 187 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 188 if (rc)
> > cf0921bea66c556 KarimAllah Ahmed 2018-03-19 189 goto failed2;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 190 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 191 if (rc)
> > cf0921bea66c556 KarimAllah Ahmed 2018-03-19 192 goto failed3;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 193
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 194 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 195
> > 27d6162944b9b34 Stuart Hayes 2017-10-04 196 pci_bus_add_device(virtfn);
> > 27d6162944b9b34 Stuart Hayes 2017-10-04 197
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 198 return 0;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 199
> > cf0921bea66c556 KarimAllah Ahmed 2018-03-19 200 failed3:
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 201 sysfs_remove_link(&dev->dev.kobj, buf);
> > cf0921bea66c556 KarimAllah Ahmed 2018-03-19 202 failed2:
> > cf0921bea66c556 KarimAllah Ahmed 2018-03-19 203 pci_stop_and_remove_bus_device(virtfn);
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 @204 failed1:
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 205 pci_dev_put(dev);
> > dc087f2f6a2925e Jiang Liu 2013-05-25 206 failed0:
> > dc087f2f6a2925e Jiang Liu 2013-05-25 207 virtfn_remove_bus(dev->bus, bus);
> > dc087f2f6a2925e Jiang Liu 2013-05-25 208 failed:
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 209
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 210 return rc;
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 211 }
> > dd7cc44d0bcec5e Yu Zhao 2009-03-20 212
> >
> > :::::: The code at line 204 was first introduced by commit
> > :::::: dd7cc44d0bcec5e9c42fe52e88dc254ae62eac8d PCI: add SR-IOV API for Physical Function driver
> >
> > :::::: TO: Yu Zhao <[email protected]>
> > :::::: CC: Jesse Barnes <[email protected]>
> >
> > ---
> > 0-DAY kernel test infrastructure Open Source Technology Center
> > https://lists.01.org/hyperkitty/list/[email protected] Intel Corporation



--
Navid.

2019-11-25 19:55:03

by Navid Emamdoost

[permalink] [raw]
Subject: [PATCH v2] PCI/IOV: Fix memory leak in pci_iov_add_virtfn()

In the implementation of pci_iov_add_virtfn() the allocated virtfn is
leaked if pci_setup_device() fails. The error handling is not calling
pci_stop_and_remove_bus_device(). Change the goto label to failed2.

Fixes: 156c55325d30 ("PCI: Check for pci_setup_device() failure in pci_iov_add_virtfn()")
Signed-off-by: Navid Emamdoost <[email protected]>
---
Changes in v2:
- rename the labels for error paths
---
drivers/pci/iov.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index b3f972e8cfed..deec9f9e0b61 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -187,10 +187,10 @@ int pci_iov_add_virtfn(struct pci_dev *dev, int id)
sprintf(buf, "virtfn%u", id);
rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
if (rc)
- goto failed2;
+ goto failed1;
rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
if (rc)
- goto failed3;
+ goto failed2;

kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);

@@ -198,11 +198,10 @@ int pci_iov_add_virtfn(struct pci_dev *dev, int id)

return 0;

-failed3:
- sysfs_remove_link(&dev->dev.kobj, buf);
failed2:
- pci_stop_and_remove_bus_device(virtfn);
+ sysfs_remove_link(&dev->dev.kobj, buf);
failed1:
+ pci_stop_and_remove_bus_device(virtfn);
pci_dev_put(dev);
failed0:
virtfn_remove_bus(dev->bus, bus);
--
2.17.1

2019-11-26 23:39:43

by Bjorn Helgaas

[permalink] [raw]
Subject: Re: [PATCH v2] PCI/IOV: Fix memory leak in pci_iov_add_virtfn()

On Mon, Nov 25, 2019 at 01:52:52PM -0600, Navid Emamdoost wrote:
> In the implementation of pci_iov_add_virtfn() the allocated virtfn is
> leaked if pci_setup_device() fails. The error handling is not calling
> pci_stop_and_remove_bus_device(). Change the goto label to failed2.
>
> Fixes: 156c55325d30 ("PCI: Check for pci_setup_device() failure in pci_iov_add_virtfn()")
> Signed-off-by: Navid Emamdoost <[email protected]>

Applied to pci/virtualization for v5.6, thanks!

> ---
> Changes in v2:
> - rename the labels for error paths
> ---
> drivers/pci/iov.c | 9 ++++-----
> 1 file changed, 4 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
> index b3f972e8cfed..deec9f9e0b61 100644
> --- a/drivers/pci/iov.c
> +++ b/drivers/pci/iov.c
> @@ -187,10 +187,10 @@ int pci_iov_add_virtfn(struct pci_dev *dev, int id)
> sprintf(buf, "virtfn%u", id);
> rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
> if (rc)
> - goto failed2;
> + goto failed1;
> rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
> if (rc)
> - goto failed3;
> + goto failed2;
>
> kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
>
> @@ -198,11 +198,10 @@ int pci_iov_add_virtfn(struct pci_dev *dev, int id)
>
> return 0;
>
> -failed3:
> - sysfs_remove_link(&dev->dev.kobj, buf);
> failed2:
> - pci_stop_and_remove_bus_device(virtfn);
> + sysfs_remove_link(&dev->dev.kobj, buf);
> failed1:
> + pci_stop_and_remove_bus_device(virtfn);
> pci_dev_put(dev);
> failed0:
> virtfn_remove_bus(dev->bus, bus);
> --
> 2.17.1
>

2019-11-28 07:13:06

by Markus Elfring

[permalink] [raw]
Subject: Re: [PATCH v2] PCI/IOV: Fix memory leak in pci_iov_add_virtfn()

> In the implementation of pci_iov_add_virtfn() the allocated virtfn is
> leaked if pci_setup_device() fails. The error handling is not calling
> pci_stop_and_remove_bus_device(). Change the goto label to failed2.

Would it be nicer to rename numbered labels also according to the
Linux coding style?

Regards,
Markus