[PATCH RFC] nvme: fix NS head cdev lifetime

John Garry john.g.garry at oracle.com
Thu Jul 9 00:33:19 PDT 2026


On 08/07/2026 17:18, Christoph Hellwig wrote:
> On Wed, Jul 08, 2026 at 10:57:02AM +0100, John Garry wrote:
>> If we put the nvne_ns_head (vs nvme_ns) in the driver data, then I can't
>> see a check to know is the driver data points to a nvne_ns_head or nvme_ns.
>> Unless we keep NULL for one, but that is not so nice.
> I think NULL just for one is fine, and I'm pretty sure we already do
> something like this in the nvme code (or at least used to in the
> past).

I mentioned in a follow up mail that we have this same issue for the NS 
cdev, so we need to do similar reference taking there.

So using a NULL check would look like this:

static void nvme_cdev_rel(struct device *dev)
{
	ida_free(&nvme_ns_chr_minor_ida, MINOR(dev->devt));
	if (dev->driver_data)
		nvme_put_ns_head(container_of(dev, struct nvme_ns_head,
			cdev_device));
	else
		nvme_put_ns(container_of(dev, struct nvme_ns, cdev_device));
}


Another idea is:

static void nvme_cdev_rel(struct device *dev)
{
	ida_free(&nvme_ns_chr_minor_ida, MINOR(dev->devt));
	if (dev->parent->class == &nvme_class)
		nvme_put_ns(container_of(dev, struct nvme_ns, cdev_device));
	else
		nvme_put_ns_head(container_of(dev, struct nvme_ns_head,
cdev_device));
}

any preference?

A nicer check would be cdev->fops == nvme_ns_chr_fops, but the cdev 
pointer cannot be looked up from the dev release handler and it's too 
late to even ref the cdev at this point.



More information about the Linux-nvme mailing list