From patchwork Mon Feb 1 14:51:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Pen X-Patchwork-Id: 8180291 Return-Path: X-Original-To: patchwork-linux-block@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 261969F4DD for ; Mon, 1 Feb 2016 14:53:51 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 20FAD20459 for ; Mon, 1 Feb 2016 14:53:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 028EF20434 for ; Mon, 1 Feb 2016 14:53:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932955AbcBAOxe (ORCPT ); Mon, 1 Feb 2016 09:53:34 -0500 Received: from mail-wm0-f54.google.com ([74.125.82.54]:33547 "EHLO mail-wm0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933021AbcBAOwT (ORCPT ); Mon, 1 Feb 2016 09:52:19 -0500 Received: by mail-wm0-f54.google.com with SMTP id l66so73994922wml.0 for ; Mon, 01 Feb 2016 06:52:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=profitbricks-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Pu7fj6KPmbRwx1KyRRhVnZdl/I1DOp7SVyt7F8XpJQ8=; b=AA3lLRYt5gkwSeiq3A3SrTwQhB9M3YvPSaBWyTntSbFr7mM39y4qIDfrWzktp7gP0D DvTXo6FOS8IMKWIWD7Ja63VSzL5uebuDJMNpGKW+pJ3ZZh0wHPxZehszv527Qbo/uzF0 B1NpafBuhdVtP3pz+hIQvfqgSORztr7ioEItfizRWlqTuMzMjTBta/25Z1gtSoKwGNFS 4SR22aTznw9c+QfH/DaLgRXMteTxcHnhL+lV0Fh5RW0UwJ4rVZzErbvPauGrFWAfrIXG pbG9unJdLp8+tRHXCxgf2BZCRwY2WAM4NQUofGKoErjlm0Qnezz/CS0bbEErcCuP34SN gBHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Pu7fj6KPmbRwx1KyRRhVnZdl/I1DOp7SVyt7F8XpJQ8=; b=RKQhTXtePrvzg2ZAC6k+iFSInu0/6NyOk4p0xK3LMkoRT+njaURFPPgJDQ4nYg5LeG /CUxsvEIxJsivoJNz1zUsBZBVQcxjSD26tjkEoVS5pygHc9wQn7WdjFlAc+5PIX8Tiir JJfnb+WHEA3fctMEgYthaVTC5OuJ0wAdIwfBBNv7E4UndINcgQ07SaCEKPBAtTxkZvTY A3Iu/saqcXdjeqsU3g756rfuhUcGsclBeI3qSSFmilput5YdLMuYzaxnZIZwOtjyfiDo EJpjwqYdrOzfxTXPQeaeFWcv1PpuVTAB2sm0UbvkN2pJZrwOP1hc1N8UyAk1pKdje8g2 mhMg== X-Gm-Message-State: AG10YOQeLprxULTTT4ujmcUqI9OwuBF7iTKbIP1U0C1dGAU6EESaoN2LS7HR74feliNeA5xW X-Received: by 10.28.128.86 with SMTP id b83mr13212086wmd.27.1454338338149; Mon, 01 Feb 2016 06:52:18 -0800 (PST) Received: from pb.pb.local ([62.217.45.26]) by smtp.gmail.com with ESMTPSA id g3sm29496312wjw.31.2016.02.01.06.52.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 01 Feb 2016 06:52:17 -0800 (PST) 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-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 *),