From: Navid Emamdoost <navid.emamdoost@gmail.com>
To: Ian Abbott <abbotti@mev.co.uk>
Cc: H Hartley Sweeten <hsweeten@visionengravers.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
devel@driverdev.osuosl.org, LKML <linux-kernel@vger.kernel.org>,
Navid Emamdoost <emamd001@umn.edu>
Subject: Re: [PATCH] staging: comedi: drivers: Fix memory leak in gsc_hpdi_auto_attach
Date: Mon, 16 Dec 2019 14:18:35 -0600 [thread overview]
Message-ID: <CAEkB2ERefAPHerg=F2V_-OHDH4P8sq2QjiP8+W=0HVgcCQNscw@mail.gmail.com> (raw)
In-Reply-To: <6159c10a-2f5f-e6ef-7a64-4b613e422efa@mev.co.uk>
Ian, thanks for your feedback.
On Mon, Dec 16, 2019 at 4:36 AM Ian Abbott <abbotti@mev.co.uk> wrote:
>
> On 15/12/2019 01:33, Navid Emamdoost wrote:
> > In the implementation of gsc_hpdi_auto_attach(), the allocated dma
> > description is leaks in case of alignment error, or failure of
> > gsc_hpdi_setup_dma_descriptors() or comedi_alloc_subdevices(). Release
> > devpriv->dma_desc via dma_free_coherent().
> >
> > Signed-off-by: Navid Emamdoost <navid.emamdoost@gmail.com>
>
> Actually, there is no memory leak (although there is another problem
> that I'll mention below). If the "auto_attach" handler
> gsc_hpdi_auto_attach() returns an error, then the "detach" handler
> gsc_hpdi_detach() will be called automatically to clean up. (This is
> true for all comedi drivers). gsc_hpdi_detach() calls
> gsc_hpdi_free_dma() to free the DMA buffers and DMA descriptors.
>
I was aware that comedi_alloc_devpriv() is a resource managed
allocation, but was not sure how subsequent dma_desc allocation will
be handled when device detach.
> > ---
> > drivers/staging/comedi/drivers/gsc_hpdi.c | 16 +++++++++++++---
> > 1 file changed, 13 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/staging/comedi/drivers/gsc_hpdi.c b/drivers/staging/comedi/drivers/gsc_hpdi.c
> > index 4bdf44d82879..c0c7047a6d1b 100644
> > --- a/drivers/staging/comedi/drivers/gsc_hpdi.c
> > +++ b/drivers/staging/comedi/drivers/gsc_hpdi.c
> > @@ -633,16 +633,17 @@ static int gsc_hpdi_auto_attach(struct comedi_device *dev,
> > if (devpriv->dma_desc_phys_addr & 0xf) {
> > dev_warn(dev->class_dev,
> > " dma descriptors not quad-word aligned (bug)\n");
> > - return -EIO;
> > + retval = -EIO;
> > + goto release_dma_desc;
> > }
> >
> > retval = gsc_hpdi_setup_dma_descriptors(dev, 0x1000);
> > if (retval < 0)
> > - return retval;
> > + goto release_dma_desc;
> >
> > retval = comedi_alloc_subdevices(dev, 1);
> > if (retval)
> > - return retval;
> > + goto release_dma_desc;
> >
> > /* Digital I/O subdevice */
> > s = &dev->subdevices[0];
> > @@ -660,6 +661,15 @@ static int gsc_hpdi_auto_attach(struct comedi_device *dev,
> > s->cancel = gsc_hpdi_cancel;
> >
> > return gsc_hpdi_init(dev);
> > +
> > +release_dma_desc:
> > + if (devpriv->dma_desc)
> > + dma_free_coherent(&pcidev->dev,
> > + sizeof(struct plx_dma_desc) *
> > + NUM_DMA_DESCRIPTORS,
> > + devpriv->dma_desc,
> > + devpriv->dma_desc_phys_addr);
> > + return retval;
> > }
> >
> > static void gsc_hpdi_detach(struct comedi_device *dev)
> >
>
> This patch could actually result in devpriv->dma_desc being freed twice
> - once in the 'release_dma_desc:' code and again when gsc_hpdi_detach()
> is called externally as part of the clean-up.
>
> The real bug in the original code is that it does not check whether any
> of the calls to dma_alloc_coherent() returned NULL. If any of the calls
> to dma_alloc_coherent() returns NULL, gsc_hpdi_auto_attach() needs to
> return an error (-ENOMEM). The subsequent call to gsc_hpdi_detach()
> will then free whatever DMA coherent buffers where allocated.
>
Yes, this potential null deref is another type of bug, which I will
send a patch for separately.
> --
> -=( Ian Abbott <abbotti@mev.co.uk> || Web: www.mev.co.uk )=-
> -=( MEV Ltd. is a company registered in England & Wales. )=-
> -=( Registered number: 02862268. Registered address: )=-
> -=( 15 West Park Road, Bramhall, STOCKPORT, SK7 3JZ, UK. )=-
--
Navid.
prev parent reply other threads:[~2019-12-16 20:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-15 1:33 [PATCH] staging: comedi: drivers: Fix memory leak in gsc_hpdi_auto_attach Navid Emamdoost
2019-12-15 15:07 ` Markus Elfring
2019-12-16 10:36 ` Ian Abbott
2019-12-16 20:18 ` Navid Emamdoost [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='CAEkB2ERefAPHerg=F2V_-OHDH4P8sq2QjiP8+W=0HVgcCQNscw@mail.gmail.com' \
--to=navid.emamdoost@gmail.com \
--cc=abbotti@mev.co.uk \
--cc=devel@driverdev.osuosl.org \
--cc=emamd001@umn.edu \
--cc=gregkh@linuxfoundation.org \
--cc=hsweeten@visionengravers.com \
--cc=linux-kernel@vger.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;
as well as URLs for NNTP newsgroup(s).