Skip to content

Commit be5eb93

Browse files
nmtadamChristoph Hellwig
authored andcommitted
nvme: fix per-namespace chardev deletion
Decrease reference count of chardevice during char device deletion in order to fix a memory leak. Add a release callabck for the device associated chardev and move ida_simple_remove into the release function. Fixes: 2637bae ("nvme: introduce generic per-namespace chardev") Reported-by: Yi Zhang <[email protected]> Suggested-by: Sagi Grimberg <[email protected]> Signed-off-by: Adam Manzanares <[email protected]> Reviewed-by: Javier González <[email protected]> Tested-by: Yi Zhang <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]>
1 parent 85f74ac commit be5eb93

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

drivers/nvme/host/core.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3548,10 +3548,15 @@ static int __nvme_check_ids(struct nvme_subsystem *subsys,
35483548
return 0;
35493549
}
35503550

3551+
static void nvme_cdev_rel(struct device *dev)
3552+
{
3553+
ida_simple_remove(&nvme_ns_chr_minor_ida, MINOR(dev->devt));
3554+
}
3555+
35513556
void nvme_cdev_del(struct cdev *cdev, struct device *cdev_device)
35523557
{
35533558
cdev_device_del(cdev, cdev_device);
3554-
ida_simple_remove(&nvme_ns_chr_minor_ida, MINOR(cdev_device->devt));
3559+
put_device(cdev_device);
35553560
}
35563561

35573562
int nvme_cdev_add(struct cdev *cdev, struct device *cdev_device,
@@ -3564,14 +3569,14 @@ int nvme_cdev_add(struct cdev *cdev, struct device *cdev_device,
35643569
return minor;
35653570
cdev_device->devt = MKDEV(MAJOR(nvme_ns_chr_devt), minor);
35663571
cdev_device->class = nvme_ns_chr_class;
3572+
cdev_device->release = nvme_cdev_rel;
35673573
device_initialize(cdev_device);
35683574
cdev_init(cdev, fops);
35693575
cdev->owner = owner;
35703576
ret = cdev_device_add(cdev, cdev_device);
3571-
if (ret) {
3577+
if (ret)
35723578
put_device(cdev_device);
3573-
ida_simple_remove(&nvme_ns_chr_minor_ida, minor);
3574-
}
3579+
35753580
return ret;
35763581
}
35773582

@@ -3603,11 +3608,9 @@ static int nvme_add_ns_cdev(struct nvme_ns *ns)
36033608
ns->ctrl->instance, ns->head->instance);
36043609
if (ret)
36053610
return ret;
3606-
ret = nvme_cdev_add(&ns->cdev, &ns->cdev_device, &nvme_ns_chr_fops,
3607-
ns->ctrl->ops->module);
3608-
if (ret)
3609-
kfree_const(ns->cdev_device.kobj.name);
3610-
return ret;
3611+
3612+
return nvme_cdev_add(&ns->cdev, &ns->cdev_device, &nvme_ns_chr_fops,
3613+
ns->ctrl->ops->module);
36113614
}
36123615

36133616
static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl,

drivers/nvme/host/multipath.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,6 @@ static int nvme_add_ns_head_cdev(struct nvme_ns_head *head)
431431
return ret;
432432
ret = nvme_cdev_add(&head->cdev, &head->cdev_device,
433433
&nvme_ns_head_chr_fops, THIS_MODULE);
434-
if (ret)
435-
kfree_const(head->cdev_device.kobj.name);
436434
return ret;
437435
}
438436

0 commit comments

Comments
 (0)