Skip to content

Commit 94735ec

Browse files
Artem BityutskiyDavid Woodhouse
Artem Bityutskiy
authored and
David Woodhouse
committed
mtd: mtd_blkdevs: fix error path in blktrans_open
The 'blktrans_open()' does not handle possible '__get_mtd_device()' failures because it does not check the error code. Moreover, the 'dev->tr->open()' failures are not handled correctly because in this case the function just goes ahead and gets the mtd device, then returns an error. But Instead, it should _not_ try to get the mtd device, then it should put back the module and the kref. This patch fixes the issue. Note, I only compile-tested it. This patch was inspired by a bug report about a similar issue in 2.6.34 kernels sent by Mike Turner <[email protected]> to the MTD mailing list: http://lists.infradead.org/pipermail/linux-mtd/2011-April/034980.html Signed-off-by: Artem Bityutskiy <[email protected]> Signed-off-by: David Woodhouse <[email protected]>
1 parent 5c39c4c commit 94735ec

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

drivers/mtd/mtd_blkdevs.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,33 @@ static int blktrans_open(struct block_device *bdev, fmode_t mode)
221221
kref_get(&dev->ref);
222222
__module_get(dev->tr->owner);
223223

224-
if (dev->mtd) {
225-
ret = dev->tr->open ? dev->tr->open(dev) : 0;
226-
__get_mtd_device(dev->mtd);
224+
if (!dev->mtd)
225+
goto unlock;
226+
227+
if (dev->tr->open) {
228+
ret = dev->tr->open(dev);
229+
if (ret)
230+
goto error_put;
227231
}
228232

233+
ret = __get_mtd_device(dev->mtd);
234+
if (ret)
235+
goto error_release;
236+
229237
unlock:
230238
mutex_unlock(&dev->lock);
231239
blktrans_dev_put(dev);
232240
return ret;
241+
242+
error_release:
243+
if (dev->tr->release)
244+
dev->tr->release(dev);
245+
error_put:
246+
module_put(dev->tr->owner);
247+
kref_put(&dev->ref, blktrans_dev_release);
248+
mutex_unlock(&dev->lock);
249+
blktrans_dev_put(dev);
250+
return ret;
233251
}
234252

235253
static int blktrans_release(struct gendisk *disk, fmode_t mode)

0 commit comments

Comments
 (0)