Commit 1c87f735 authored by Linus Walleij's avatar Linus Walleij Committed by Ulf Hansson

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 #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: 97548575 ("mmc: block: Convert RPMB to a character device")
Reported-by: default avatarAdrian Hunter <adrian.hunter@intel.com>
Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
Acked-by: default avatarAdrian Hunter <adrian.hunter@intel.com>
Signed-off-by: default avatarUlf Hansson <ulf.hansson@linaro.org>
parent 14f4ca7e
...@@ -2277,9 +2277,7 @@ static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp) ...@@ -2277,9 +2277,7 @@ static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
get_device(&rpmb->dev); get_device(&rpmb->dev);
filp->private_data = rpmb; filp->private_data = rpmb;
mutex_lock(&open_lock); mmc_blk_get(rpmb->md->disk);
rpmb->md->usage++;
mutex_unlock(&open_lock);
return nonseekable_open(inode, filp); return nonseekable_open(inode, filp);
} }
...@@ -2290,9 +2288,7 @@ static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp) ...@@ -2290,9 +2288,7 @@ static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
struct mmc_rpmb_data, chrdev); struct mmc_rpmb_data, chrdev);
put_device(&rpmb->dev); put_device(&rpmb->dev);
mutex_lock(&open_lock); mmc_blk_put(rpmb->md);
rpmb->md->usage--;
mutex_unlock(&open_lock);
return 0; return 0;
} }
...@@ -2308,6 +2304,13 @@ static const struct file_operations mmc_rpmb_fileops = { ...@@ -2308,6 +2304,13 @@ static const struct file_operations mmc_rpmb_fileops = {
#endif #endif
}; };
static void mmc_blk_rpmb_device_release(struct device *dev)
{
struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
kfree(rpmb);
}
static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
struct mmc_blk_data *md, struct mmc_blk_data *md,
...@@ -2326,8 +2329,10 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, ...@@ -2326,8 +2329,10 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
return devidx; return devidx;
rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL); rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
if (!rpmb) if (!rpmb) {
ida_simple_remove(&mmc_rpmb_ida, devidx);
return -ENOMEM; return -ENOMEM;
}
snprintf(rpmb_name, sizeof(rpmb_name), snprintf(rpmb_name, sizeof(rpmb_name),
"mmcblk%u%s", card->host->index, subname ? subname : ""); "mmcblk%u%s", card->host->index, subname ? subname : "");
...@@ -2338,6 +2343,7 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, ...@@ -2338,6 +2343,7 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
rpmb->dev.bus = &mmc_rpmb_bus_type; rpmb->dev.bus = &mmc_rpmb_bus_type;
rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id); rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
rpmb->dev.parent = &card->dev; rpmb->dev.parent = &card->dev;
rpmb->dev.release = mmc_blk_rpmb_device_release;
device_initialize(&rpmb->dev); device_initialize(&rpmb->dev);
dev_set_drvdata(&rpmb->dev, rpmb); dev_set_drvdata(&rpmb->dev, rpmb);
rpmb->md = md; rpmb->md = md;
...@@ -2347,7 +2353,7 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, ...@@ -2347,7 +2353,7 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev); ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
if (ret) { if (ret) {
pr_err("%s: could not add character device\n", rpmb_name); pr_err("%s: could not add character device\n", rpmb_name);
goto out_remove_ida; goto out_put_device;
} }
list_add(&rpmb->node, &md->rpmbs); list_add(&rpmb->node, &md->rpmbs);
...@@ -2362,18 +2368,16 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, ...@@ -2362,18 +2368,16 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
return 0; return 0;
out_remove_ida: out_put_device:
ida_simple_remove(&mmc_rpmb_ida, rpmb->id); put_device(&rpmb->dev);
kfree(rpmb);
return ret; return ret;
} }
static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb) static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
{ {
cdev_device_del(&rpmb->chrdev, &rpmb->dev); cdev_device_del(&rpmb->chrdev, &rpmb->dev);
device_del(&rpmb->dev); put_device(&rpmb->dev);
ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
kfree(rpmb);
} }
/* MMC Physical partitions consist of two boot partitions and /* MMC Physical partitions consist of two boot partitions and
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment