Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1595827

Re: [Question] devm_kmalloc() for DMA ?

From Russell King - ARM Linux <linux@armlinux.org.uk>
Newsgroups linux.kernel
Subject Re: [Question] devm_kmalloc() for DMA ?
Date 2017-03-09 10:30 +0100
Message-ID <tj2yt-3cS-7@gated-at.bofh.it> (permalink)
References (4 earlier) <tiQdY-35t-31@gated-at.bofh.it> <tiQH0-3gA-19@gated-at.bofh.it> <tiRjI-3Lx-17@gated-at.bofh.it> <tiSSt-4Wh-7@gated-at.bofh.it> <tiWW5-7Ck-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Thu, Mar 09, 2017 at 12:25:07PM +0900, Masahiro Yamada wrote:
> (c) Use kmalloc() and kfree().   (be careful for memory leak)

This is quite simple.  For the first one, it doesn't seem that it's
DMA'd into, so there's no need to use GFP_DMA.

-	/* allocate a temporary buffer for nand_scan_ident() */
-	denali->buf.buf = devm_kzalloc(denali->dev, PAGE_SIZE,
-					GFP_DMA | GFP_KERNEL);
-	if (!denali->buf.buf)
-		return -ENOMEM;

...

+	denali->buf.buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!denali->buf.buf)
+		return -ENOMEM;
+
	/*
	 * scan for NAND devices attached to the controller
	 * this is the first stage in a two step process to register
	 * with the nand subsystem
	 */
	ret = nand_scan_ident(mtd, denali->max_banks, NULL);
+	kfree(denali->buf.buf);
+
	if (ret)
		goto failed_req_irq;

-	/* allocate the right size buffer now */
-	devm_kfree(denali->dev, denali->buf.buf);

For the second one, I think the first thing to do is to move the
dma_set_mask() to the very beginning of the probe function - if that
fails, then we can't use DMA, and it's not something that requires
any cleanup.

With that gone, convert the other devm_kzalloc() there for buf.buf to
kzalloc(), and ensure that it's appropriately freed.  Note that this
driver is _already_ buggy in that if:

        } else if (mtd->oobsize < (denali->bbtskipbytes +
                        ECC_8BITS * (mtd->writesize /
                        ECC_SECTOR_SIZE))) {
                pr_err("Your NAND chip OOB is not large enough to contain 8bit ECC correction codes");
                goto failed_req_irq;

fails, or these:

        ret = nand_scan_tail(mtd);
        if (ret)
                goto failed_req_irq;

        ret = mtd_device_register(mtd, NULL, 0);
        if (ret) {
                dev_err(denali->dev, "Failed to register MTD: %d\n", ret);
                goto failed_req_irq;
        }

it doesn't unmap the buffer.  So, the driver is already broken.

-- 
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[Question] devm_kmalloc() for DMA ? Masahiro Yamada <yamada.masahiro@socionext.com> - 2017-03-08 12:10 +0100
  Re: [Question] devm_kmalloc() for DMA ? Robin Murphy <robin.murphy@arm.com> - 2017-03-08 12:20 +0100
    Re: [Question] devm_kmalloc() for DMA ? Masahiro Yamada <yamada.masahiro@socionext.com> - 2017-03-08 19:10 +0100
      Re: [Question] devm_kmalloc() for DMA ? Russell King - ARM Linux <linux@armlinux.org.uk> - 2017-03-08 21:20 +0100
        Re: [Question] devm_kmalloc() for DMA ? Lars-Peter Clausen <lars@metafoo.de> - 2017-03-08 21:50 +0100
          Re: [Question] devm_kmalloc() for DMA ? Russell King - ARM Linux <linux@armlinux.org.uk> - 2017-03-08 22:30 +0100
            Re: [Question] devm_kmalloc() for DMA ? Lars-Peter Clausen <lars@metafoo.de> - 2017-03-09 00:10 +0100
              Re: [Question] devm_kmalloc() for DMA ? Masahiro Yamada <yamada.masahiro@socionext.com> - 2017-03-09 04:30 +0100
                Re: [Question] devm_kmalloc() for DMA ? Russell King - ARM Linux <linux@armlinux.org.uk> - 2017-03-09 10:30 +0100
      Re: [Question] devm_kmalloc() for DMA ? Lars-Peter Clausen <lars@metafoo.de> - 2017-03-08 21:50 +0100
      Re: [Question] devm_kmalloc() for DMA ? Robin Murphy <robin.murphy@arm.com> - 2017-03-09 12:30 +0100

csiph-web