[PATCH 02/13] libmultipath: Add basic gendisk support

John Garry john.g.garry at oracle.com
Mon Mar 2 07:39:52 PST 2026


On 02/03/2026 12:31, Nilay Shroff wrote:
>>
>> +#define MPATH_HEAD_DISK_LIVE             0
>> +
>>   struct mpath_head {
>>       struct srcu_struct    srcu;
>>       struct list_head    dev_list;    /* list of all mpath_devs */
>> @@ -17,12 +34,36 @@ struct mpath_head {
>>       struct kref        ref;
>> +    unsigned long        flags;
>>       struct mpath_device __rcu         *current_path[MAX_NUMNODES];
>> +    const struct mpath_head_template    *mpdt;
>>       void            *drvdata;
>>   };
> Not sure why we don't have back reference to struct mpath_disk
> from struct mpath_head here. Does it make sense to have this?

We can get away without it.

Some more background info .. so the concept of separate mpath_head and 
mpath_disk is driven by SCSI, which has scsi_device and scsi_disk 
classes. The scsi_disk driver (sd.c) controls the per-path gendisk and 
the mpath_disk, and these internals are hidden from the scsi_core (which 
controls the scsi_device). SCSI having this layered approach makes 
things more complicated. This is unlike NVMe, where the core driver 
controls the NS gendisk also.

> 
> 
>> +static inline struct mpath_disk *mpath_bd_device_to_disk(struct 
>> device *dev)
>> +{
>> +    return dev_get_drvdata(dev);
>> +}
>> +
>> +static inline struct mpath_disk *mpath_gendisk_to_disk(struct gendisk 
>> *disk)
>> +{
>> +    return mpath_bd_device_to_disk(disk_to_dev(disk));
>> +}
>> +
>>   int mpath_get_head(struct mpath_head *mpath_head);
>>   void mpath_put_head(struct mpath_head *mpath_head);
>>   struct mpath_head *mpath_alloc_head(void);
>> +void mpath_put_disk(struct mpath_disk *mpath_disk);
>> +void mpath_remove_disk(struct mpath_disk *mpath_disk);
>> +void mpath_unregister_disk(struct mpath_disk *mpath_disk);
>> +struct mpath_disk *mpath_alloc_head_disk(struct queue_limits *lim,
>> +            int numa_node);
>> +void mpath_device_set_live(struct mpath_disk *mpath_disk,
>> +            struct mpath_device *mpath_device);
>> +void mpath_unregister_disk(struct mpath_disk *mpath_disk);
>> +static inline bool is_mpath_head(struct gendisk *disk)
>> +{
>> +    return disk->fops == &mpath_ops;
>> +}
>>   #endif // _LIBMULTIPATH_H
>> diff --git a/lib/multipath.c b/lib/multipath.c
>> index 15c495675d729..88efb0ae16acb 100644
>> --- a/lib/multipath.c
>> +++ b/lib/multipath.c
>> @@ -32,6 +32,135 @@ void mpath_put_head(struct mpath_head *mpath_head)
>>   }
>>   EXPORT_SYMBOL_GPL(mpath_put_head);
>> +static void mpath_free_disk(struct kref *ref)
>> +{
>> +    struct mpath_disk *mpath_disk =
>> +        container_of(ref, struct mpath_disk, ref);
>> +    struct mpath_head *mpath_head = mpath_disk->mpath_head;
>> +
>> +    put_disk(mpath_disk->disk);
>> +    mpath_put_head(mpath_head);
>> +    kfree(mpath_disk);
>> +}
>> +
> 
> The mpath_alloc_head_disk() doesn't get a reference to the
> mpath_head object but here while freeing mpath_disk we put
> the reference to mpath_head. Would that create a reference
> imbalance? 

I think that what I done can be improved. If you check 
nvme_mpath_alloc_disk(), when we alloc the head the ref is 1, and then 
we rely on the disk release to release that head reference.

> Yes we got a reference to mpath_head while
> allocating it but then these are two (alloc mpath_disk and
> alloc mpath_head) disjoint operations. In that case, can't
> we have both mpath_disk and mpath_head allocated under one
> libmultipath API?

I would like to have something simpler (like mainline NVMe code), but I 
have it this way because of SCSI, as above.

Thanks




More information about the Linux-nvme mailing list