From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933268AbcBAOxf (ORCPT ); Mon, 1 Feb 2016 09:53:35 -0500 Received: from mail-wm0-f44.google.com ([74.125.82.44]:33803 "EHLO mail-wm0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933008AbcBAOwT (ORCPT ); Mon, 1 Feb 2016 09:52:19 -0500 From: Roman Pen Cc: Roman Pen , Gi-Oh Kim , Jens Axboe , Dan Williams , Sagi Grimberg , "Martin K. Petersen" , Ming Lei , Vishal Verma , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/4] block: introduce new call put_gendisk() in genhd.c Date: Mon, 1 Feb 2016 15:51:53 +0100 Message-Id: <1454338315-13465-3-git-send-email-roman.penyaev@profitbricks.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1454338315-13465-1-git-send-email-roman.penyaev@profitbricks.com> References: <1454338315-13465-1-git-send-email-roman.penyaev@profitbricks.com> To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Cc: Gi-Oh Kim Cc: Jens Axboe Cc: Dan Williams Cc: Sagi Grimberg Cc: "Martin K. Petersen" Cc: Ming Lei Cc: Vishal Verma 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