From patchwork Mon Feb 1 14:51:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Pen X-Patchwork-Id: 8180271 Return-Path: X-Original-To: patchwork-linux-block@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 1578CBEEE5 for ; Mon, 1 Feb 2016 14:53:36 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3BB5220434 for ; Mon, 1 Feb 2016 14:53:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2A117203C2 for ; Mon, 1 Feb 2016 14:53:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933127AbcBAOwT (ORCPT ); Mon, 1 Feb 2016 09:52:19 -0500 Received: from mail-wm0-f45.google.com ([74.125.82.45]:36468 "EHLO mail-wm0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932994AbcBAOwS (ORCPT ); Mon, 1 Feb 2016 09:52:18 -0500 Received: by mail-wm0-f45.google.com with SMTP id p63so74242276wmp.1 for ; Mon, 01 Feb 2016 06:52:17 -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=ufSf2GjTxzU1ZQVAlYkxArMo1AVKOz/qlQ8CaFhiU5o=; b=E8O1/oo2VjoP5wCHBPLc4cAIYg7C6fLLAIN1Q1qdJHNrqZdXBkyu46xFuY3xLNmFyf a7ol8c2C66rM6Em+mOkPARLB3HTJ4e3+7HF/7GlzO8BvtNLNPKrEADe6JAMR1ig4G610 Rm5wn2sfjf9cQMQmrjme+/Z8acR7Y1mlM4eCIbiY2S3WfGA+RhlRwwsWjuIIs3YD/AXb fEw8ziG96D7OX8HdWTWb13g5JziUUZk+TYSnZ7A3frRvVZlEXNbMtMEQhGpaG0w0n1Tq ePOGmwRFEwyGPFIEVxqDsrFkN+ZWxBBczqGmiuX6O715igzHMXPZfxGoX8ZEFinTlXvM B6DQ== 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=ufSf2GjTxzU1ZQVAlYkxArMo1AVKOz/qlQ8CaFhiU5o=; b=ZZf/DA4s2EB7qpcH2lUeqSwZU+C/JQZmYzk6RBcx0FB1M/XHd/bmJj5PEQwSYplpCb rXZDm68eK1htOhnAPPsNC07SPHyTamQgQmIqsIz5/UDT2vV8qwaLDZ7SKmzWCC2Tf5/+ 1GBHHt5GP8hlxEJUG48J3o5GyPtrPu7Oi1anFEValJB8S3bTT4K2FN/jPKq6rMzTw+gT VWau+BLNz3KoykUoYisWHflHZrGUsAmia8G2pq0LMe9f3I+aDwKnf5o33V9ww/DezonL cYBNSop79Pv9A2ftUd62Yo+sNNnC3XxaKV05IDxrRa7ZchK9cFtro6YaWgS1HXbe0DZa J0+A== X-Gm-Message-State: AG10YOSlxVPYQNWTnsJHBPDx6I+QKx1xnlIBCFcH4f+i9NKoPu3nXVEeUfFBNP9LcCkgrmmA X-Received: by 10.28.186.87 with SMTP id k84mr11951963wmf.13.1454338337090; Mon, 01 Feb 2016 06:52:17 -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.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 01 Feb 2016 06:52:16 -0800 (PST) From: Roman Pen Cc: Roman Pen , Gi-Oh Kim , Tejun Heo , Jens Axboe , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/4] block: fix module reference leak on put_disk() call for cgroups throttle Date: Mon, 1 Feb 2016 15:51:52 +0100 Message-Id: <1454338315-13465-2-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 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 Cc: Gi-Oh Kim Cc: Tejun Heo Cc: Jens Axboe Cc: linux-block@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- block/blk-cgroup.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index 5a37188..66e6f1a 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -788,6 +788,7 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, { struct gendisk *disk; struct blkcg_gq *blkg; + struct module *owner; unsigned int major, minor; int key_len, part, ret; char *body; @@ -804,7 +805,9 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, if (!disk) return -ENODEV; if (part) { + owner = disk->fops->owner; put_disk(disk); + module_put(owner); return -ENODEV; } @@ -820,7 +823,9 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, ret = PTR_ERR(blkg); rcu_read_unlock(); spin_unlock_irq(disk->queue->queue_lock); + owner = disk->fops->owner; put_disk(disk); + module_put(owner); /* * If queue was bypassing, we should retry. Do so after a * short msleep(). It isn't strictly necessary but queue @@ -851,9 +856,13 @@ EXPORT_SYMBOL_GPL(blkg_conf_prep); void blkg_conf_finish(struct blkg_conf_ctx *ctx) __releases(ctx->disk->queue->queue_lock) __releases(rcu) { + struct module *owner; + spin_unlock_irq(ctx->disk->queue->queue_lock); rcu_read_unlock(); + owner = ctx->disk->fops->owner; put_disk(ctx->disk); + module_put(owner); } EXPORT_SYMBOL_GPL(blkg_conf_finish);