Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1323204 > unrolled thread

[PATCH 0/4] introduce new put_getdisk() call

Started byRoman Pen <roman.penyaev@profitbricks.com>
First post2016-02-01 16:00 +0100
Last post2016-02-01 18:20 +0100
Articles 3 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH 0/4] introduce new put_getdisk() call Roman Pen <roman.penyaev@profitbricks.com> - 2016-02-01 16:00 +0100
    [PATCH 2/4] block: introduce new call put_gendisk() in genhd.c Roman Pen <roman.penyaev@profitbricks.com> - 2016-02-01 16:00 +0100
    Re: [PATCH 0/4] introduce new put_getdisk() call Tejun Heo <tj@kernel.org> - 2016-02-01 18:20 +0100

#1323204 — [PATCH 0/4] introduce new put_getdisk() call

FromRoman Pen <roman.penyaev@profitbricks.com>
Date2016-02-01 16:00 +0100
Subject[PATCH 0/4] introduce new put_getdisk() call
Message-ID<qXo7o-4jK-3@gated-at.bofh.it>
Hello.

In this patchset in the first patch I fixed module reference leak inside
blk-cgroup.c.  In other patches I switched to a new put_getdisk() call,
which should be used if the disk was received by get_disk() or get_gendisk()
functions, which internally increase module reference.

The idea is to avoid confusion in the future and to have symmetric calls:

	alloc_disk() -> put_disk() [as it is done in all the block drivers]

and

	get_gendisk() -> put_gendisk() [if you need to find a disk by minor,major]

The second sequence internally increases disk owner module reference on
get and decreases it on put.

Roman Pen (4):
  block: fix module reference leak on put_disk() call for cgroups
    throttle
  block: introduce new call put_gendisk() in genhd.c
  block,fs: switch to a new put_gendisk() call
  hibernate: fix disk and module leak on successfull resume

 block/blk-cgroup.c       |  6 ++---
 block/genhd.c            | 59 +++++++++++++++++++++++++++++++++++++++++++++---
 fs/block_dev.c           | 24 ++++++--------------
 include/linux/genhd.h    |  1 +
 kernel/power/hibernate.c |  5 +++-
 5 files changed, 71 insertions(+), 24 deletions(-)

Cc: "Martin K. Petersen" <martin.petersen@oracle.com>
Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Gi-Oh Kim <gi-oh.kim@profitbricks.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Len Brown <len.brown@intel.com>
Cc: Ming Lei <tom.leiming@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: Sagi Grimberg <sagig@mellanox.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vishal Verma <vishal.l.verma@intel.com>
Cc: linux-block@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-pm@vger.kernel.org

-- 
2.6.2

[toc] | [next] | [standalone]


#1323209 — [PATCH 2/4] block: introduce new call put_gendisk() in genhd.c

FromRoman Pen <roman.penyaev@profitbricks.com>
Date2016-02-01 16:00 +0100
Subject[PATCH 2/4] block: introduce new call put_gendisk() in genhd.c
Message-ID<qXo7q-4jK-47@gated-at.bofh.it>
In reply to#1323204
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(-)

diff --git a/block/genhd.c b/block/genhd.c
index 0f566a5..66c8278 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -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";
diff --git a/include/linux/genhd.h b/include/linux/genhd.h
index 9e4e547..f84efbf 100644
--- a/include/linux/genhd.h
+++ b/include/linux/genhd.h
@@ -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 *),
-- 
2.6.2

[toc] | [prev] | [next] | [standalone]


#1323327

FromTejun Heo <tj@kernel.org>
Date2016-02-01 18:20 +0100
Message-ID<qXqiS-66P-1@gated-at.bofh.it>
In reply to#1323204
On Mon, Feb 01, 2016 at 03:51:51PM +0100, Roman Pen wrote:
> Hello.
> 
> In this patchset in the first patch I fixed module reference leak inside
> blk-cgroup.c.  In other patches I switched to a new put_getdisk() call,
> which should be used if the disk was received by get_disk() or get_gendisk()
> functions, which internally increase module reference.
> 
> The idea is to avoid confusion in the future and to have symmetric calls:
> 
> 	alloc_disk() -> put_disk() [as it is done in all the block drivers]
> 
> and
> 
> 	get_gendisk() -> put_gendisk() [if you need to find a disk by minor,major]
> 
> The second sequence internally increases disk owner module reference on
> get and decreases it on put.

For the entire series,

 Acked-by: Tejun Heo <tj@kernel.org>

Nice catch, thanks!

-- 
tejun

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web