Skip to content

Commit 39a169b

Browse files
Roman Penaxboe
Roman Pen
authored andcommitted
block: fix module reference leak on put_disk() call for cgroups throttle
get_disk(),get_gendisk() calls have non explicit side effect: they increase the reference on the disk owner module. The following is the correct sequence how to get a disk reference and to put it: disk = get_gendisk(...); /* use disk */ owner = disk->fops->owner; put_disk(disk); module_put(owner); fs/block_dev.c is aware of this required module_put() call, but f.e. blkg_conf_finish(), which is located in block/blk-cgroup.c, does not put a module reference. To see a leakage in action cgroups throttle config can be used. In the following script I'm removing throttle for /dev/ram0 (actually this is NOP, because throttle was never set for this device): # lsmod | grep brd brd 5175 0 # i=100; while [ $i -gt 0 ]; do echo "1:0 0" > \ /sys/fs/cgroup/blkio/blkio.throttle.read_bps_device; i=$(($i - 1)); \ done # lsmod | grep brd brd 5175 100 Now brd module has 100 references. The issue is fixed by calling module_put() just right away put_disk(). Signed-off-by: Roman Pen <[email protected]> Cc: Gi-Oh Kim <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Jens Axboe <[email protected]> Cc: [email protected] Cc: [email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 21d1478 commit 39a169b

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

block/blk-cgroup.c

+9
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
788788
{
789789
struct gendisk *disk;
790790
struct blkcg_gq *blkg;
791+
struct module *owner;
791792
unsigned int major, minor;
792793
int key_len, part, ret;
793794
char *body;
@@ -804,7 +805,9 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
804805
if (!disk)
805806
return -ENODEV;
806807
if (part) {
808+
owner = disk->fops->owner;
807809
put_disk(disk);
810+
module_put(owner);
808811
return -ENODEV;
809812
}
810813

@@ -820,7 +823,9 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
820823
ret = PTR_ERR(blkg);
821824
rcu_read_unlock();
822825
spin_unlock_irq(disk->queue->queue_lock);
826+
owner = disk->fops->owner;
823827
put_disk(disk);
828+
module_put(owner);
824829
/*
825830
* If queue was bypassing, we should retry. Do so after a
826831
* short msleep(). It isn't strictly necessary but queue
@@ -851,9 +856,13 @@ EXPORT_SYMBOL_GPL(blkg_conf_prep);
851856
void blkg_conf_finish(struct blkg_conf_ctx *ctx)
852857
__releases(ctx->disk->queue->queue_lock) __releases(rcu)
853858
{
859+
struct module *owner;
860+
854861
spin_unlock_irq(ctx->disk->queue->queue_lock);
855862
rcu_read_unlock();
863+
owner = ctx->disk->fops->owner;
856864
put_disk(ctx->disk);
865+
module_put(owner);
857866
}
858867
EXPORT_SYMBOL_GPL(blkg_conf_finish);
859868

0 commit comments

Comments
 (0)