From: Ma Ke <make24@iscas.ac.cn>
To: andi.shyti@kernel.org
Cc: akpm@linux-foundation.org, linux-i2c@vger.kernel.org,
linux-kernel@vger.kernel.org, make24@iscas.ac.cn,
shyam-sundar.s-k@amd.com, stable@vger.kernel.org,
syniurge@gmail.com, wsa@kernel.org
Subject: Re: [PATCH v2] i2c: fix reference leak in MP2 PCI device
Date: Thu, 2 Oct 2025 20:45:47 +0800 [thread overview]
Message-ID: <20251002124547.1506-1-make24@iscas.ac.cn> (raw)
In-Reply-To: <7cu6uvwjki72rz6zoshfg3vonawikiasxheotmrsowqoalk2jb@7iu7cenha7p5>
On Thu, 2 Oct 2025 at 07:56, Andi Shyti <andi.shyti@kernel.org> wrote:
> Hi,
>
> > diff --git a/drivers/i2c/busses/i2c-amd-mp2-pci.c b/drivers/i2c/busses/i2c-amd-mp2-pci.c
> > index ef7370d3dbea..60edbabc2986 100644
> > --- a/drivers/i2c/busses/i2c-amd-mp2-pci.c
> > +++ b/drivers/i2c/busses/i2c-amd-mp2-pci.c
> > @@ -458,13 +458,16 @@ struct amd_mp2_dev *amd_mp2_find_device(void)
> > {
> > struct device *dev;
> > struct pci_dev *pci_dev;
> > + struct amd_mp2_dev *mp2_dev;
> >
> > dev = driver_find_next_device(&amd_mp2_pci_driver.driver, NULL);
> > if (!dev)
> > 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;
>
> the patch is good, but I don't think you need to declare mp2_dev
> because to_pci_dev(dev) should work even without hodling the
> reference of dev.
Thank you for your feedback. The declaration of the temporary variable
mp2_dev in the patch may be necessary because we need to save the
result of pci_get_drvdata(pci_dev) before calling put_device(dev). If
we do not do this and instead call put_device(dev) first before
returning pci_get_drvdata(pci_dev), it may lead to the deallocation of
dev, thereby invalidating pci_dev. Accessing its driver data at this
point could potentially trigger a use-after-free error. Therefore, the
temporary variable ensures that the driver data remains safely
accessible even after the reference is released. So perhaps we need to
retain the declaration of this temporary variable?
Best regards,
Ma Ke
>
> I also have to agree with Markus that something like:
>
> struct device *dev __free(put_device) = ...; /* it can also be NULL */
>
> would work nicer.
>
> Thanks,
> Andi
>
> > }
> > EXPORT_SYMBOL_GPL(amd_mp2_find_device);
> >
> > --
> > 2.17.1
> >
prev parent reply other threads:[~2025-10-02 12:46 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-28 7:19 [PATCH v2] i2c: fix reference leak in MP2 PCI device Ma Ke
2025-09-28 11:00 ` Markus Elfring
2025-09-28 15:50 ` Greg KH
2025-10-01 23:56 ` Andi Shyti
2025-10-02 0:04 ` Andi Shyti
2025-10-02 12:45 ` Ma Ke [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251002124547.1506-1-make24@iscas.ac.cn \
--to=make24@iscas.ac.cn \
--cc=akpm@linux-foundation.org \
--cc=andi.shyti@kernel.org \
--cc=linux-i2c@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=shyam-sundar.s-k@amd.com \
--cc=stable@vger.kernel.org \
--cc=syniurge@gmail.com \
--cc=wsa@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox