Skip to content

Commit 1c87f73

Browse files
linuswstorulf
authored andcommitted
mmc: block: Fix bug when removing RPMB chardev
I forgot to account for the fact that the device core holds a reference to a device added with device_initialize() that need to be released with a corresponding put_device() to reach a 0 refcount at the end of the lifecycle. This led to a NULL pointer reference when freeing the device when e.g. unbidning the host device in sysfs. Fix this and use the device .release() callback to free the IDA and free:ing the memory used by the RPMB device. Before this patch: /sys/bus/amba/drivers/mmci-pl18x$ echo 80114000.sdi4_per2 > unbind [ 29.797332] mmc3: card 0001 removed [ 29.810791] Unable to handle kernel NULL pointer dereference at virtual address 00000050 [ 29.818878] pgd = de70c000 [ 29.821624] [00000050] *pgd=1e70a831, *pte=00000000, *ppte=00000000 [ 29.827911] Internal error: Oops: 17 [#1] PREEMPT SMP ARM [ 29.833282] Modules linked in: [ 29.836334] CPU: 1 PID: 154 Comm: sh Not tainted 4.14.0-rc3-00039-g83318e309566-dirty torvalds#736 [ 29.844604] Hardware name: ST-Ericsson Ux5x0 platform (Device Tree Support) [ 29.851562] task: de572700 task.stack: de742000 [ 29.856079] PC is at kernfs_find_ns+0x8/0x100 [ 29.860443] LR is at kernfs_find_and_get_ns+0x30/0x48 After this patch: /sys/bus/amba/drivers/mmci-pl18x$ echo 80005000.sdi4_per2 > unbind [ 20.623382] mmc3: card 0001 removed Fixes: 9754857 ("mmc: block: Convert RPMB to a character device") Reported-by: Adrian Hunter <adrian.hunter@intel.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Acked-by: Adrian Hunter <adrian.hunter@intel.com> Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
1 parent 14f4ca7 commit 1c87f73

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

drivers/mmc/core/block.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,9 +2277,7 @@ static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
22772277

22782278
get_device(&rpmb->dev);
22792279
filp->private_data = rpmb;
2280-
mutex_lock(&open_lock);
2281-
rpmb->md->usage++;
2282-
mutex_unlock(&open_lock);
2280+
mmc_blk_get(rpmb->md->disk);
22832281

22842282
return nonseekable_open(inode, filp);
22852283
}
@@ -2290,9 +2288,7 @@ static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
22902288
struct mmc_rpmb_data, chrdev);
22912289

22922290
put_device(&rpmb->dev);
2293-
mutex_lock(&open_lock);
2294-
rpmb->md->usage--;
2295-
mutex_unlock(&open_lock);
2291+
mmc_blk_put(rpmb->md);
22962292

22972293
return 0;
22982294
}
@@ -2308,6 +2304,13 @@ static const struct file_operations mmc_rpmb_fileops = {
23082304
#endif
23092305
};
23102306

2307+
static void mmc_blk_rpmb_device_release(struct device *dev)
2308+
{
2309+
struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2310+
2311+
ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2312+
kfree(rpmb);
2313+
}
23112314

23122315
static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
23132316
struct mmc_blk_data *md,
@@ -2326,8 +2329,10 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
23262329
return devidx;
23272330

23282331
rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2329-
if (!rpmb)
2332+
if (!rpmb) {
2333+
ida_simple_remove(&mmc_rpmb_ida, devidx);
23302334
return -ENOMEM;
2335+
}
23312336

23322337
snprintf(rpmb_name, sizeof(rpmb_name),
23332338
"mmcblk%u%s", card->host->index, subname ? subname : "");
@@ -2338,6 +2343,7 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
23382343
rpmb->dev.bus = &mmc_rpmb_bus_type;
23392344
rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
23402345
rpmb->dev.parent = &card->dev;
2346+
rpmb->dev.release = mmc_blk_rpmb_device_release;
23412347
device_initialize(&rpmb->dev);
23422348
dev_set_drvdata(&rpmb->dev, rpmb);
23432349
rpmb->md = md;
@@ -2347,7 +2353,7 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
23472353
ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
23482354
if (ret) {
23492355
pr_err("%s: could not add character device\n", rpmb_name);
2350-
goto out_remove_ida;
2356+
goto out_put_device;
23512357
}
23522358

23532359
list_add(&rpmb->node, &md->rpmbs);
@@ -2362,18 +2368,16 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
23622368

23632369
return 0;
23642370

2365-
out_remove_ida:
2366-
ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2367-
kfree(rpmb);
2371+
out_put_device:
2372+
put_device(&rpmb->dev);
23682373
return ret;
23692374
}
23702375

23712376
static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2377+
23722378
{
23732379
cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2374-
device_del(&rpmb->dev);
2375-
ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2376-
kfree(rpmb);
2380+
put_device(&rpmb->dev);
23772381
}
23782382

23792383
/* MMC Physical partitions consist of two boot partitions and

0 commit comments

Comments
 (0)