Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751678AbbEHA06 (ORCPT ); Thu, 7 May 2015 20:26:58 -0400 Received: from mail-pd0-f182.google.com ([209.85.192.182]:35328 "EHLO mail-pd0-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750907AbbEHA0z (ORCPT ); Thu, 7 May 2015 20:26:55 -0400 Date: Thu, 7 May 2015 17:26:49 -0700 From: Brian Norris To: Giuseppe Cantavenera Cc: linux-mtd@lists.infradead.org, lorenzo.restelli.ext@nokia.com, dwmw2@infradead.org, stable@vger.kernel.org, alexander.sverdlin@nokia.com, zhangxingcai , fengfuqiu@huawei.com, tanhaijun@huawei.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mtd: fix: avoid race condition when accessing mtd->usecount Message-ID: <20150508002649.GX32500@ld-irv-0074> References: <1429611622-2652-1-git-send-email-giuseppe.cantavenera.ext@nokia.com> <20150508001012.GV32500@ld-irv-0074> <20150508001745.GW32500@ld-irv-0074> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20150508001745.GW32500@ld-irv-0074> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2968 Lines: 93 On Thu, May 07, 2015 at 05:17:45PM -0700, Brian Norris wrote: > On Thu, May 07, 2015 at 05:10:12PM -0700, Brian Norris wrote: > > On Tue, Apr 21, 2015 at 12:20:22PM +0200, Giuseppe Cantavenera wrote: > > > @@ -484,7 +486,7 @@ int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old) > > > if (old->open) { > > > if (old->tr->release) > > > old->tr->release(old); > > > - __put_mtd_device(old->mtd); > > > + put_mtd_device(old->mtd); > > > > This looks wrong. See: > [...] > > deregister_mtd_blktrans() > > |_ mutex_lock(&mtd_table_mutex) > > |_ tr->remove_dev() -> inftl_remove_dev() > > |_ del_mtd_blktrans_dev() > > |_ put_mtd_device() > > |_ mutex_lock(&mtd_table_mutex) <--- AA deadlock > > What's more, this code in del_mtd_blktrans_dev() makes it obvious that > this hunk is wrong: > > int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old) > { > unsigned long flags; > > if (mutex_trylock(&mtd_table_mutex)) { > mutex_unlock(&mtd_table_mutex); > BUG(); > } > ... > > So rather than a comment, the code is showing that it's a BUG() to not > be holding mtd_table_mutex already. As an alternative to your patch, how about the following? BTW, this does still leave a usecount race in drivers/mtd/maps/vmu-flash.c. But that driver should really be using mtd->_get_device(), if it actually wants its own refcount. Signed-off-by: Brian Norris --- diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c index 2b0c52870999..df7c6c70757a 100644 --- a/drivers/mtd/mtd_blkdevs.c +++ b/drivers/mtd/mtd_blkdevs.c @@ -197,6 +197,7 @@ static int blktrans_open(struct block_device *bdev, fmode_t mode) return -ERESTARTSYS; /* FIXME: busy loop! -arnd*/ mutex_lock(&dev->lock); + mutex_lock(&mtd_table_mutex); if (dev->open) goto unlock; @@ -220,6 +221,7 @@ static int blktrans_open(struct block_device *bdev, fmode_t mode) unlock: dev->open++; + mutex_unlock(&mtd_table_mutex); mutex_unlock(&dev->lock); blktrans_dev_put(dev); return ret; @@ -230,6 +232,7 @@ error_release: error_put: module_put(dev->tr->owner); kref_put(&dev->ref, blktrans_dev_release); + mutex_unlock(&mtd_table_mutex); mutex_unlock(&dev->lock); blktrans_dev_put(dev); return ret; @@ -243,6 +246,7 @@ static void blktrans_release(struct gendisk *disk, fmode_t mode) return; mutex_lock(&dev->lock); + mutex_lock(&mtd_table_mutex); if (--dev->open) goto unlock; @@ -256,6 +260,7 @@ static void blktrans_release(struct gendisk *disk, fmode_t mode) __put_mtd_device(dev->mtd); } unlock: + mutex_unlock(&mtd_table_mutex); mutex_unlock(&dev->lock); blktrans_dev_put(dev); } -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/