Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751858AbdINU7V (ORCPT ); Thu, 14 Sep 2017 16:59:21 -0400 Received: from mail-db5eur01on0060.outbound.protection.outlook.com ([104.47.2.60]:38944 "EHLO EUR01-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751330AbdINU7T (ORCPT ); Thu, 14 Sep 2017 16:59:19 -0400 From: Roy Pledge To: Arnd Bergmann , Christoph Hellwig , "Marek Szyprowski" , Greg Kroah-Hartman CC: Robin Murphy , Vladimir Murzin , Mark Craske , "George G. Davis" , Bastian Hecht , "iommu@lists.linux-foundation.org" , "linux-kernel@vger.kernel.org" Subject: Re: dma-coherent: fix dma_declare_coherent_memory() logic error Thread-Topic: dma-coherent: fix dma_declare_coherent_memory() logic error Thread-Index: AQHTLZxR9YYzCtVhcUawp57Wd1yR4Q== Date: Thu, 14 Sep 2017 20:59:16 +0000 Message-ID: References: <20170905081026.1198405-1-arnd@arndb.de> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [192.88.168.1] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DB6PR04MB3125;6:G2vQngUhbtg4w8CcQE59ulEccbJGdHVArVUI+rjl3K7ZeK3FWLBxcsTx3cdn/7kktUryGi2VJhUAU+zkOxAf6LfpV/5HQ0E/vtxlG2z5pqeBRhhDoi0q3dH0Cw082nTL+JeG+MFjcmF//lukzm+DNI97GfxrbXK4u8VEAqc/8MCyOmo7FdUWMrtgBjap635njjzbW5+FIkbZL0pUCJxN9UL2wj4b+xb0RLa2IGXNMKKpAw7Xuiyxa3Bo1XbMmESoOmK6cI362XuxjKlimSExUoWr1Ic37M9zPjUArXHbZadUIa4LluO7ifnlROUZVJwNcT4yTEweSn3dYdth2UmEVQ==;5:+drU1jyv4/dem8zeYuxafS2O4DISVqvu2r9yQb1GTjcEsQzDkosfM/EuXNZw7EkPWuzdYB30TqHQ9qz7nb3tgW1bLO+PQeS0EXTRoaQpo9RxTMtskNOjgkc14S3WjhTVwzgM6lyelwTxknYSCGH7Aw==;24:va4OppyEwnOzz6CglZvwjAoWu83jgvbGZ5vtOqZziYNF+dA0aL0oIWVUuptuCYSCk07OYcR+Fth/Scyf1VO/hwg52UXPMzq+liuI7rQ5KDc=;7:2yZaO+rC6NLPuoAa23KPHskqMvFtNRSEYd8ldn6z5B008NqC2djazTxT5s8ejdsKFvYcx/mhdXqnJevRa6oQ0MhpL416O5mISDS1PdWo8AfhIHSFL2b+ycfY5gYLxBZgJteKsyZ3FyaqiKIhtiYxsJC16OcLyRreHc7Xcnkw4BmrfrAWhX02C8OPaKkZgTroylZ6l/91MgyNym4Nl96bGfTcpEqjiidck10dR+NDi0U= x-ms-exchange-antispam-srfa-diagnostics: SSOS; x-ms-office365-filtering-correlation-id: a0e08712-bfac-4262-9cf4-08d4fbb3760b x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(48565401081)(300000503095)(300135400095)(2017052603199)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:DB6PR04MB3125; x-ms-traffictypediagnostic: DB6PR04MB3125: x-exchange-antispam-report-test: UriScan:; x-microsoft-antispam-prvs: x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(5005006)(8121501046)(3002001)(10201501046)(93006095)(93001095)(100000703101)(100105400095)(6055026)(6041248)(20161123564025)(20161123558100)(20161123562025)(20161123560025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:DB6PR04MB3125;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:DB6PR04MB3125; x-forefront-prvs: 0430FA5CB7 x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(6009001)(346002)(376002)(377454003)(199003)(189002)(24454002)(54906002)(25786009)(7416002)(97736004)(68736007)(316002)(6246003)(7696004)(5660300001)(53936002)(478600001)(101416001)(50986999)(66066001)(54356999)(76176999)(14454004)(106356001)(86362001)(74316002)(55016002)(229853002)(3660700001)(8936002)(3280700002)(53546010)(2906002)(8676002)(105586002)(81156014)(81166006)(102836003)(3846002)(6116002)(4326008)(9686003)(189998001)(33656002)(305945005)(7736002)(99286003)(6506006)(6436002)(5250100002)(2900100001);DIR:OUT;SFP:1101;SCL:1;SRVR:DB6PR04MB3125;H:DB6PR04MB2999.eurprd04.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; authentication-results: spf=none (sender IP is ) smtp.mailfrom=roy.pledge@nxp.com; spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-originalarrivaltime: 14 Sep 2017 20:59:16.3491 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6PR04MB3125 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by nfs id v8EKxPST002614 Content-Length: 3906 Lines: 116 On 9/5/2017 4:10 AM, Arnd Bergmann wrote: > A recent change interprets the return code of dma_init_coherent_memory > as an error value, but it is instead a boolean, where 'true' indicates > success. This leads causes the caller to always do the wrong thing, > and also triggers a compile-time warning about it: > > drivers/base/dma-coherent.c: In function 'dma_declare_coherent_memory': > drivers/base/dma-coherent.c:99:15: error: 'mem' may be used uninitialized in this function [-Werror=maybe-uninitialized] > > I ended up changing the code a little more, to give use the usual > error handling, as this seemed the best way to fix up the warning > and make the code look reasonable at the same time. > > Fixes: 2436bdcda53f ("dma-coherent: remove the DMA_MEMORY_MAP and DMA_MEMORY_IO flags") > Signed-off-by: Arnd Bergmann > --- > drivers/base/dma-coherent.c | 38 +++++++++++++++++++++++++------------- > 1 file changed, 25 insertions(+), 13 deletions(-) > > diff --git a/drivers/base/dma-coherent.c b/drivers/base/dma-coherent.c > index f82a504583d4..a39b2166b145 100644 > --- a/drivers/base/dma-coherent.c > +++ b/drivers/base/dma-coherent.c > @@ -37,7 +37,7 @@ static inline dma_addr_t dma_get_device_base(struct device *dev, > return mem->device_base; > } > > -static bool dma_init_coherent_memory( > +static int dma_init_coherent_memory( > phys_addr_t phys_addr, dma_addr_t device_addr, size_t size, int flags, > struct dma_coherent_mem **mem) > { > @@ -45,20 +45,28 @@ static bool dma_init_coherent_memory( > void __iomem *mem_base = NULL; > int pages = size >> PAGE_SHIFT; > int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); > + int ret; > > - if (!size) > + if (!size) { > + ret = -EINVAL; > goto out; > + } > > mem_base = memremap(phys_addr, size, MEMREMAP_WC); > - if (!mem_base) > + if (!mem_base) { > + ret = -EINVAL; > goto out; > - > + } > dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL); > - if (!dma_mem) > + if (!dma_mem) { > + ret = -ENOMEM; > goto out; > + } > dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL); > - if (!dma_mem->bitmap) > + if (!dma_mem->bitmap) { > + ret = -ENOMEM; > goto out; > + } > > dma_mem->virt_base = mem_base; > dma_mem->device_base = device_addr; > @@ -68,13 +76,13 @@ static bool dma_init_coherent_memory( > spin_lock_init(&dma_mem->spinlock); > > *mem = dma_mem; > - return true; > + return 0; > > out: > kfree(dma_mem); > if (mem_base) > memunmap(mem_base); > - return false; > + return ret; > } > > static void dma_release_coherent_memory(struct dma_coherent_mem *mem) > @@ -338,14 +346,18 @@ static struct reserved_mem *dma_reserved_default_memory __initdata; > static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev) > { > struct dma_coherent_mem *mem = rmem->priv; > + int ret; > + > + if (!mem) > + return -ENODEV; When I picked up this change my use of of_reserved_mem_device_init() broke. The only place rmem->priv is set is in this function (bottom of the patch) so the !mem check above will always fail. Am I missing something? It seems to me the intent here was to only call dma_init_coherent_memory() once and use rmem->priv as a cache for future calls but I'm just looking at the implemation of this for the first time. > > - if (!mem && > - !dma_init_coherent_memory(rmem->base, rmem->base, rmem->size, > - DMA_MEMORY_EXCLUSIVE, > - &mem)) { > + ret = dma_init_coherent_memory(rmem->base, rmem->base, rmem->size, > + DMA_MEMORY_EXCLUSIVE, &mem); > + > + if (ret) { > pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %ld MiB\n", > &rmem->base, (unsigned long)rmem->size / SZ_1M); > - return -ENODEV; > + return ret; > } > mem->use_dev_dma_pfn_offset = true; > rmem->priv = mem; >