From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753744AbdCIJV7 (ORCPT ); Thu, 9 Mar 2017 04:21:59 -0500 Received: from pandora.armlinux.org.uk ([78.32.30.218]:44912 "EHLO pandora.armlinux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753053AbdCIJVu (ORCPT ); Thu, 9 Mar 2017 04:21:50 -0500 Date: Thu, 9 Mar 2017 09:20:42 +0000 From: Russell King - ARM Linux To: Masahiro Yamada Cc: Lars-Peter Clausen , Robin Murphy , dmaengine@vger.kernel.org, linux-arm-kernel , Arnd Bergmann , Linux Kernel Mailing List , "James E.J. Bottomley" , Tejun Heo , "David S. Miller" Subject: Re: [Question] devm_kmalloc() for DMA ? Message-ID: <20170309092042.GH21222@n2100.armlinux.org.uk> References: <20170308195901.GD21222@n2100.armlinux.org.uk> <456e0515-b9d7-e1b6-fc25-a2ec5244d9db@metafoo.de> <20170308211900.GE21222@n2100.armlinux.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.