@@ -722,7 +722,13 @@ static ssize_t disk_badblocks_store(struct device *dev,
* @partno: returned partition index
*
* This function gets the structure containing partitioning
- * information for the given device @devt.
+ * information for the given device @devt and increases the kobj
+ * and disk owner module references.
+ *
+ * RETURNS:
+ * Resulting gendisk on success, NULL in case of failure. After usage
+ * disk must be put with put_gendisk() call, which also puts the module
+ * reference.
*/
struct gendisk *get_gendisk(dev_t devt, int *partno)
{
@@ -1305,6 +1311,17 @@ struct gendisk *alloc_disk(int minors)
}
EXPORT_SYMBOL(alloc_disk);
+/**
+ * alloc_disk_node - allocates disk for specified minors and node id.
+ *
+ * @minors: how many minors are expected for that disk before switching
+ * to extended block range
+ * @node_id: memory node from which to allocate
+ *
+ * RETURNS:
+ * Resulting disk on success, NULL in case of failure.
+ * alloc_disk() and alloc_disk_node() are paired with put_disk() call.
+ */
struct gendisk *alloc_disk_node(int minors, int node_id)
{
struct gendisk *disk;
@@ -1349,6 +1366,16 @@ struct gendisk *alloc_disk_node(int minors, int node_id)
}
EXPORT_SYMBOL(alloc_disk_node);
+/**
+ * get_disk - increases the kobj and module references.
+ *
+ * @disk: disk for which references should be increased
+ *
+ * RETURNS:
+ * Resulting kobj of the disk on success, NULL in case of failure.
+ * After usage disk must be put with put_gendisk() call, which also
+ * puts the module reference.
+ */
struct kobject *get_disk(struct gendisk *disk)
{
struct module *owner;
@@ -1367,17 +1394,43 @@ struct kobject *get_disk(struct gendisk *disk)
return kobj;
}
-
EXPORT_SYMBOL(get_disk);
+/**
+ * put_disk - puts only kobj reference.
+ *
+ * @disk: disk to put
+ *
+ * This put_disk() is paired with alloc_disk(). Never call this
+ * if you get a disk using get_gendisk() or get_disk() calls.
+ */
void put_disk(struct gendisk *disk)
{
if (disk)
kobject_put(&disk_to_dev(disk)->kobj);
}
-
EXPORT_SYMBOL(put_disk);
+/**
+ * put_gendisk - puts kobj and disk owner module references.
+ *
+ * @disk: disk to put
+ *
+ * This put_gendisk() is paired with get_gendisk() and get_disk() calls.
+ * Never use this call if you just have allocated a disk using alloc_disk(),
+ * use put_disk() instead.
+ */
+void put_gendisk(struct gendisk *disk)
+{
+ if (disk) {
+ struct module *owner = disk->fops->owner;
+
+ put_disk(disk);
+ module_put(owner);
+ }
+}
+EXPORT_SYMBOL(put_gendisk);
+
static void set_disk_ro_uevent(struct gendisk *gd, int ro)
{
char event[] = "DISK_RO=1";
@@ -635,6 +635,7 @@ extern struct gendisk *alloc_disk_node(int minors, int node_id);
extern struct gendisk *alloc_disk(int minors);
extern struct kobject *get_disk(struct gendisk *disk);
extern void put_disk(struct gendisk *disk);
+extern void put_gendisk(struct gendisk *disk);
extern void blk_register_region(dev_t devt, unsigned long range,
struct module *module,
struct kobject *(*probe)(dev_t, int *, void *),
The confusion comes from the fact, that if disk was get using get_disk() get_gendisk() calls, you also have to put a disk owner module reference, so the sequence is the following: disk = get_gendisk(...); /* use disk */ owner = disk->fops->owner; put_disk(disk); module_put(owner); The disk, which was allocated using alloc_disk() requires only the opposite put_disk() call, without puting the module reference. That's error prone. Here in this patch new call is introduced put_gendisk(), which is aimed to put a disk reference and also a disk owner reference. The expected usage for the block drivers has not been changed and is the following: disk = alloc_disk(...); /* use */ put_disk(disk); The expected usage for those who did get_gendisk(): disk = get_gendisk(); /* use */ put_gendisk(disk); That should help to get rid of modules references leak, which happens if disk was received by get_gendisk() call, but then was put by put_disk(), without corresponding module_put(). Also function description is updated. Signed-off-by: Roman Pen <roman.penyaev@profitbricks.com> Cc: Gi-Oh Kim <gi-oh.kim@profitbricks.com> Cc: Jens Axboe <axboe@kernel.dk> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Sagi Grimberg <sagig@mellanox.com> Cc: "Martin K. Petersen" <martin.petersen@oracle.com> Cc: Ming Lei <tom.leiming@gmail.com> Cc: Vishal Verma <vishal.l.verma@intel.com> Cc: linux-block@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- block/genhd.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++--- include/linux/genhd.h | 1 + 2 files changed, 57 insertions(+), 3 deletions(-)