From patchwork Wed Jun 23 14:59:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12340025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E8932C4743C for ; Wed, 23 Jun 2021 15:00:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BE13B608FE for ; Wed, 23 Jun 2021 15:00:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230243AbhFWPDG (ORCPT ); Wed, 23 Jun 2021 11:03:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbhFWPDG (ORCPT ); Wed, 23 Jun 2021 11:03:06 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9D28C061574 for ; Wed, 23 Jun 2021 08:00:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=VvZg4PxfaiU69t4gVmY3F0mZQB37mtA+JzKjV1OmVl0=; b=P75Fu46Zv9ZX51BSTENaVj7TcQ oM5RT1a4q7x7v2kIFDWVqkjfG0OXZcpZiKWAiwzBuzmip9m1rDPhXaHUz8rqBl4SJARLWxX+0pAQU cHAwHaYJyZIRWaQIoeSdKp+FuegrOrYXdlWHq7WzRvHNeiDJBSDrwlA/GJ40c2DXZdkAi4wvl+Di/ BFoCpIP4JxqsPj7Wu/+hrQjTi858xjNOKDhr6zwcigdC+KOWGiOv/xQTvPaXYiEjdC7UmZh8Zmv3o HerXXKtlKKVwmSHxcDA8KPAHR24FlimQ884d9+XmKaSh/weveXuoGYFE1kiNvacQzHzzIPW4SD4Xl kHfZ8oMw==; Received: from [2001:4bb8:188:3e21:6594:49:139:2b3f] (helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1lw4Lx-00FXrQ-O1; Wed, 23 Jun 2021 14:59:59 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Tetsuo Handa , linux-block@vger.kernel.org, Chaitanya Kulkarni Subject: [PATCH 1/9] loop: reorder loop_exit Date: Wed, 23 Jun 2021 16:59:00 +0200 Message-Id: <20210623145908.92973-2-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623145908.92973-1-hch@lst.de> References: <20210623145908.92973-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Unregister the misc and blockdevice first to prevent further access, and only then iterate to remove the devices. Signed-off-by: Christoph Hellwig Reviewed-by: Chaitanya Kulkarni --- drivers/block/loop.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 081eb9aaeba8..44fa27c54ac2 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -2381,14 +2381,11 @@ static int loop_exit_cb(int id, void *ptr, void *data) static void __exit loop_exit(void) { mutex_lock(&loop_ctl_mutex); - - idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); - idr_destroy(&loop_index_idr); - unregister_blkdev(LOOP_MAJOR, "loop"); - misc_deregister(&loop_misc); + idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); + idr_destroy(&loop_index_idr); mutex_unlock(&loop_ctl_mutex); } From patchwork Wed Jun 23 14:59:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12340027 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00D2EC4743C for ; Wed, 23 Jun 2021 15:01:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D3F1C608FE for ; Wed, 23 Jun 2021 15:01:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230339AbhFWPDh (ORCPT ); Wed, 23 Jun 2021 11:03:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbhFWPDh (ORCPT ); Wed, 23 Jun 2021 11:03:37 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3D00C061574 for ; Wed, 23 Jun 2021 08:01:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=miz2SHtXr83p5l2VfoB34cIcc7wi7YYh2t9n5METgEM=; b=Eom5tOHeYarLJZGbbgMNDR7TzF JxHX6zAczc6lTHVooXcaMt2WjAF2qS7C+DwmZ3NySLr4yKGt2nmZ/+2LXPQRjy/oo85DwRR/6t5hP y0WyJ3Ciz0nW5wb7ingIrmLWvI59b3JNtgl9Rukzv+cAzcftWhUT+pwXnnr9rjpFF6tff/YwmVZ+r vVENsBl3DIfB6zMsL3G9H0+XV3fIViERtqvhziwmxNlju+SJYMY9MEHVTdUXsjRcHw9+Jc+d7nkk4 /dUrCDSlQ6HFENTxgrqOviwnNoA0WD18ofLsuA4xNeOmKlHoIBgfesFak+fiujE8JAapR9OsIE90v K6wwWmyA==; Received: from [2001:4bb8:188:3e21:6594:49:139:2b3f] (helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1lw4MT-00FXtO-OB; Wed, 23 Jun 2021 15:00:47 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Tetsuo Handa , linux-block@vger.kernel.org, Chaitanya Kulkarni Subject: [PATCH 2/9] loop: reduce loop_ctl_mutex coverage in loop_exit Date: Wed, 23 Jun 2021 16:59:01 +0200 Message-Id: <20210623145908.92973-3-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623145908.92973-1-hch@lst.de> References: <20210623145908.92973-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org loop_ctl_mutex is only needed to iterate the IDR for removing the loop devices, so reduce the coverage. Signed-off-by: Christoph Hellwig Reviewed-by: Chaitanya Kulkarni --- drivers/block/loop.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 44fa27c54ac2..9df9fb490f87 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -2380,13 +2380,14 @@ static int loop_exit_cb(int id, void *ptr, void *data) static void __exit loop_exit(void) { - mutex_lock(&loop_ctl_mutex); unregister_blkdev(LOOP_MAJOR, "loop"); misc_deregister(&loop_misc); + mutex_lock(&loop_ctl_mutex); idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); - idr_destroy(&loop_index_idr); mutex_unlock(&loop_ctl_mutex); + + idr_destroy(&loop_index_idr); } module_init(loop_init); From patchwork Wed Jun 23 14:59:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12340029 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 450B4C48BE5 for ; Wed, 23 Jun 2021 15:01:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 28B92608FE for ; Wed, 23 Jun 2021 15:01:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230392AbhFWPEA (ORCPT ); Wed, 23 Jun 2021 11:04:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbhFWPEA (ORCPT ); Wed, 23 Jun 2021 11:04:00 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18BC5C061574 for ; Wed, 23 Jun 2021 08:01:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=XoP6Zb5BQuNW2F1akvMlxvEeo0gegatxAw2D0HU3vAg=; b=CjCFpFElncU+IxxVpKk51fbn7y tx/ktsuxRuSSQK2Jnc2qywCPXiiJKGrAWf2JCYWf1D6fpxsApwmLSSaJ7xAtQsKbbMzpWC3ArxQ1L D6Km+n1m1Y/C5/SVwtXZ14aIebunh0osM+2plHCSf05uFmMYSTG6ZZEnlpfV0JqEIQCNfzLtHloXe SAjKwcKus+Xh/IR47eiTf9VSjrSMY8kDWJolM22a/h4sG+EI8jK2ta8D3LxlNheg7T0P0HjQswWci Lt478vkv44CFQitNrxzl3xp1SyVB9RjFs5jWiFrMbQo4TD72AepoaNvEu4/3/pakgYFd5H/u3zrax VdaLKYDw==; Received: from [2001:4bb8:188:3e21:6594:49:139:2b3f] (helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1lw4NL-00FXwY-38; Wed, 23 Jun 2021 15:01:18 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Tetsuo Handa , linux-block@vger.kernel.org, Chaitanya Kulkarni Subject: [PATCH 3/9] loop: remove the l argument to loop_add Date: Wed, 23 Jun 2021 16:59:02 +0200 Message-Id: <20210623145908.92973-4-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623145908.92973-1-hch@lst.de> References: <20210623145908.92973-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org None of the callers cares about the allocated struct loop_device. Signed-off-by: Christoph Hellwig Reviewed-by: Chaitanya Kulkarni --- drivers/block/loop.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 9df9fb490f87..8392722d0e12 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -2069,7 +2069,7 @@ static const struct blk_mq_ops loop_mq_ops = { .complete = lo_complete_rq, }; -static int loop_add(struct loop_device **l, int i) +static int loop_add(int i) { struct loop_device *lo; struct gendisk *disk; @@ -2157,7 +2157,6 @@ static int loop_add(struct loop_device **l, int i) disk->queue = lo->lo_queue; sprintf(disk->disk_name, "loop%d", i); add_disk(disk); - *l = lo; return lo->lo_number; out_cleanup_tags: @@ -2227,7 +2226,7 @@ static void loop_probe(dev_t dev) mutex_lock(&loop_ctl_mutex); if (loop_lookup(&lo, idx) < 0) - loop_add(&lo, idx); + loop_add(idx); mutex_unlock(&loop_ctl_mutex); } @@ -2249,7 +2248,7 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd, ret = -EEXIST; break; } - ret = loop_add(&lo, parm); + ret = loop_add(parm); break; case LOOP_CTL_REMOVE: ret = loop_lookup(&lo, parm); @@ -2277,7 +2276,7 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd, ret = loop_lookup(&lo, -1); if (ret >= 0) break; - ret = loop_add(&lo, -1); + ret = loop_add(-1); } mutex_unlock(&loop_ctl_mutex); @@ -2304,7 +2303,6 @@ MODULE_ALIAS("devname:loop-control"); static int __init loop_init(void) { int i, nr; - struct loop_device *lo; int err; part_shift = 0; @@ -2358,7 +2356,7 @@ static int __init loop_init(void) /* pre-create number of devices given by config or max_loop */ mutex_lock(&loop_ctl_mutex); for (i = 0; i < nr; i++) - loop_add(&lo, i); + loop_add(i); mutex_unlock(&loop_ctl_mutex); printk(KERN_INFO "loop: module loaded\n"); From patchwork Wed Jun 23 14:59:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12340031 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D784C4743C for ; Wed, 23 Jun 2021 15:02:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4DF166101D for ; Wed, 23 Jun 2021 15:02:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230243AbhFWPEX (ORCPT ); Wed, 23 Jun 2021 11:04:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbhFWPEX (ORCPT ); Wed, 23 Jun 2021 11:04:23 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FC72C061574 for ; Wed, 23 Jun 2021 08:02:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=ZYTWT1x1aj8+1YwAzNumqNw6wJ39vs/Vr6slb7e5rnE=; b=TNkhv4My3FGtmxZcN+TPpbczq9 cuSOtwijBcn2yL0d/x0Dcvnt06wMjwzf9PoZ6XDt6RC+EvsOCGfpXc7KsxOC34OwZpgfGlQSetOFL 3qbJe4OWShf+HUWLEcMg8O2csupVoaM3u908zqREnaBX8+uDBOzROeygBl9tQJWHHA3IDjhlEasLR 6geQtisRwyk7i+OzSA18li1EuN49sx1NuT8jKxs2hwnCYEMydd/8MxB24hHesCw+QmG/8o0Z3jQeh TrcJ3/ck0V2NP1qvhnoZTxkoVNYJt3GLtmw6JJDMgOUhK6eMPEyWQUUQQkxsxqQ8Qk7aqX49Bss7N z9Sy7B1A==; Received: from [2001:4bb8:188:3e21:6594:49:139:2b3f] (helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1lw4Nk-00FXy3-Fy; Wed, 23 Jun 2021 15:01:41 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Tetsuo Handa , linux-block@vger.kernel.org Subject: [PATCH 4/9] loop: don't call loop_lookup before adding a loop device Date: Wed, 23 Jun 2021 16:59:03 +0200 Message-Id: <20210623145908.92973-5-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623145908.92973-1-hch@lst.de> References: <20210623145908.92973-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org loop_add returns the right error if the slot wasn't available. Signed-off-by: Christoph Hellwig --- drivers/block/loop.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 8392722d0e12..b1e7e420563b 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -2219,14 +2219,12 @@ static int loop_lookup(struct loop_device **l, int i) static void loop_probe(dev_t dev) { int idx = MINOR(dev) >> part_shift; - struct loop_device *lo; if (max_loop && idx >= max_loop) return; mutex_lock(&loop_ctl_mutex); - if (loop_lookup(&lo, idx) < 0) - loop_add(idx); + loop_add(idx); mutex_unlock(&loop_ctl_mutex); } @@ -2243,11 +2241,6 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd, ret = -ENOSYS; switch (cmd) { case LOOP_CTL_ADD: - ret = loop_lookup(&lo, parm); - if (ret >= 0) { - ret = -EEXIST; - break; - } ret = loop_add(parm); break; case LOOP_CTL_REMOVE: From patchwork Wed Jun 23 14:59:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12340033 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3776C48BC2 for ; Wed, 23 Jun 2021 15:02:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B8F156113B for ; Wed, 23 Jun 2021 15:02:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231282AbhFWPEw (ORCPT ); Wed, 23 Jun 2021 11:04:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbhFWPEv (ORCPT ); Wed, 23 Jun 2021 11:04:51 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EA35C061574 for ; Wed, 23 Jun 2021 08:02:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=gFVft+5X3dh0iXZ86Dq4ihNbVemSouJ+kgmT8VfIASI=; b=WT9ate+ectny58wjaOYNFd0CRQ z5dlE6wHuqsWzujXDdqjN59q+OzBobRumjOBJUOEKZX0m5Mm+PQIx1LtZ9TMcv+szWBcrjqIR3HEP y/k+ZiwWQaPjFU6tFNZyajhL+Li6Fsc3qV7NBVk3bEWToFvyuYTS2HC8tssuO4yqcTFF5Iz66+Xqo lQuDmWC/l6VceSOfHNuVS9LKHoBPDe6eKNF+R0KLYAHrtXlDIyzgRyVfXbKcfYmE/oImA64NfDIYw xkBF4tVOSUpbo5v2i8OQTpkhCGdxByBJ8efNda2C751SD2PYUIE3+FHo/tvoWYMOutvgvWH9iqNXy RKgqEuig==; Received: from [2001:4bb8:188:3e21:6594:49:139:2b3f] (helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1lw4O7-00FXz2-8O; Wed, 23 Jun 2021 15:02:04 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Tetsuo Handa , linux-block@vger.kernel.org, Chaitanya Kulkarni Subject: [PATCH 5/9] loop: split loop_control_ioctl Date: Wed, 23 Jun 2021 16:59:04 +0200 Message-Id: <20210623145908.92973-6-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623145908.92973-1-hch@lst.de> References: <20210623145908.92973-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Split loop_control_ioctl into a helper for each command. This keeps the code nicely separated for the upcoming locking changes. Signed-off-by: Christoph Hellwig Reviewed-by: Chaitanya Kulkarni --- drivers/block/loop.c | 93 ++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 33 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index b1e7e420563b..d55526c355e1 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -2228,8 +2228,51 @@ static void loop_probe(dev_t dev) mutex_unlock(&loop_ctl_mutex); } -static long loop_control_ioctl(struct file *file, unsigned int cmd, - unsigned long parm) +static int loop_control_add(int idx) +{ + int ret; + + ret = mutex_lock_killable(&loop_ctl_mutex); + if (ret) + return ret; + ret = loop_add(idx); + mutex_unlock(&loop_ctl_mutex); + return ret; +} + +static int loop_control_remove(int idx) +{ + struct loop_device *lo; + int ret; + + ret = mutex_lock_killable(&loop_ctl_mutex); + if (ret) + return ret; + + ret = loop_lookup(&lo, idx); + if (ret < 0) + goto out_unlock_ctrl; + + ret = mutex_lock_killable(&lo->lo_mutex); + if (ret) + goto out_unlock_ctrl; + if (lo->lo_state != Lo_unbound || + atomic_read(&lo->lo_refcnt) > 0) { + mutex_unlock(&lo->lo_mutex); + ret = -EBUSY; + goto out_unlock_ctrl; + } + lo->lo_state = Lo_deleting; + mutex_unlock(&lo->lo_mutex); + + idr_remove(&loop_index_idr, lo->lo_number); + loop_remove(lo); +out_unlock_ctrl: + mutex_unlock(&loop_ctl_mutex); + return ret; +} + +static int loop_control_get_free(int idx) { struct loop_device *lo; int ret; @@ -2237,43 +2280,27 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd, ret = mutex_lock_killable(&loop_ctl_mutex); if (ret) return ret; + ret = loop_lookup(&lo, -1); + if (ret < 0) + ret = loop_add(-1); + mutex_unlock(&loop_ctl_mutex); + + return ret; +} - ret = -ENOSYS; +static long loop_control_ioctl(struct file *file, unsigned int cmd, + unsigned long parm) +{ switch (cmd) { case LOOP_CTL_ADD: - ret = loop_add(parm); - break; + return loop_control_add(parm); case LOOP_CTL_REMOVE: - ret = loop_lookup(&lo, parm); - if (ret < 0) - break; - ret = mutex_lock_killable(&lo->lo_mutex); - if (ret) - break; - if (lo->lo_state != Lo_unbound) { - ret = -EBUSY; - mutex_unlock(&lo->lo_mutex); - break; - } - if (atomic_read(&lo->lo_refcnt) > 0) { - ret = -EBUSY; - mutex_unlock(&lo->lo_mutex); - break; - } - lo->lo_state = Lo_deleting; - mutex_unlock(&lo->lo_mutex); - idr_remove(&loop_index_idr, lo->lo_number); - loop_remove(lo); - break; + return loop_control_remove(parm); case LOOP_CTL_GET_FREE: - ret = loop_lookup(&lo, -1); - if (ret >= 0) - break; - ret = loop_add(-1); + return loop_control_get_free(parm); + default: + return -ENOSYS; } - mutex_unlock(&loop_ctl_mutex); - - return ret; } static const struct file_operations loop_ctl_fops = { From patchwork Wed Jun 23 14:59:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12340053 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D501AC4743C for ; Wed, 23 Jun 2021 15:03:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B9C936115A for ; Wed, 23 Jun 2021 15:03:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230061AbhFWPFT (ORCPT ); Wed, 23 Jun 2021 11:05:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231310AbhFWPFQ (ORCPT ); Wed, 23 Jun 2021 11:05:16 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C669DC061574 for ; Wed, 23 Jun 2021 08:02:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=zwVbBuoYI/AtLYOSj3B/Doj6bcQK/Y9wePlmaXjhV1o=; b=kByS3SwR3w/xqoDbLlBimC0/Il 1wg+eFtVgEWZf0zWK6VjXU0FWVXpu2MymG7AChEBpdBJNDhbmpFvsP7vUzu0SKGPOi9Wkqe04uU/n 3u0nueT0vbSJgnZwmsk6l+IXrrfLucmHFuBB73myAxOI34l96ndGuthnCTDqrflDrrJBAVXD5twz5 SrPBC+evxbfrjwV99iCG0c258mH/WFzKqQ4sAu8ME/Jo1QAEMLWqGehd0eUkqVkgraXp6vUoOluqk IUnmCiOCWqWIYl1qiyi1/iTT86lwVQwfD4NxnXZMFW8ZqePkB5LI9R1zQ0OKvr29HAav83Br/vuHk XzNV8gAw==; Received: from [2001:4bb8:188:3e21:6594:49:139:2b3f] (helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1lw4OS-00FY09-Gg; Wed, 23 Jun 2021 15:02:32 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Tetsuo Handa , linux-block@vger.kernel.org, Chaitanya Kulkarni Subject: [PATCH 6/9] loop: move loop_ctl_mutex locking into loop_add Date: Wed, 23 Jun 2021 16:59:05 +0200 Message-Id: <20210623145908.92973-7-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623145908.92973-1-hch@lst.de> References: <20210623145908.92973-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Move acquiring and releasing loop_ctl_mutex from the callers into loop_add. Signed-off-by: Christoph Hellwig Reviewed-by: Chaitanya Kulkarni --- drivers/block/loop.c | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index d55526c355e1..01d393a60b37 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -2079,9 +2079,12 @@ static int loop_add(int i) lo = kzalloc(sizeof(*lo), GFP_KERNEL); if (!lo) goto out; - lo->lo_state = Lo_unbound; + err = mutex_lock_killable(&loop_ctl_mutex); + if (err) + goto out_free_dev; + /* allocate id, if @id >= 0, we're requesting that specific id */ if (i >= 0) { err = idr_alloc(&loop_index_idr, lo, i, i + 1, GFP_KERNEL); @@ -2091,7 +2094,7 @@ static int loop_add(int i) err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL); } if (err < 0) - goto out_free_dev; + goto out_unlock; i = err; err = -ENOMEM; @@ -2157,12 +2160,15 @@ static int loop_add(int i) disk->queue = lo->lo_queue; sprintf(disk->disk_name, "loop%d", i); add_disk(disk); - return lo->lo_number; + mutex_unlock(&loop_ctl_mutex); + return i; out_cleanup_tags: blk_mq_free_tag_set(&lo->tag_set); out_free_idr: idr_remove(&loop_index_idr, i); +out_unlock: + mutex_unlock(&loop_ctl_mutex); out_free_dev: kfree(lo); out: @@ -2222,22 +2228,7 @@ static void loop_probe(dev_t dev) if (max_loop && idx >= max_loop) return; - - mutex_lock(&loop_ctl_mutex); loop_add(idx); - mutex_unlock(&loop_ctl_mutex); -} - -static int loop_control_add(int idx) -{ - int ret; - - ret = mutex_lock_killable(&loop_ctl_mutex); - if (ret) - return ret; - ret = loop_add(idx); - mutex_unlock(&loop_ctl_mutex); - return ret; } static int loop_control_remove(int idx) @@ -2281,11 +2272,11 @@ static int loop_control_get_free(int idx) if (ret) return ret; ret = loop_lookup(&lo, -1); - if (ret < 0) - ret = loop_add(-1); mutex_unlock(&loop_ctl_mutex); - return ret; + if (ret >= 0) + return ret; + return loop_add(-1); } static long loop_control_ioctl(struct file *file, unsigned int cmd, @@ -2293,7 +2284,7 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd, { switch (cmd) { case LOOP_CTL_ADD: - return loop_control_add(parm); + return loop_add(parm); case LOOP_CTL_REMOVE: return loop_control_remove(parm); case LOOP_CTL_GET_FREE: @@ -2374,10 +2365,8 @@ static int __init loop_init(void) } /* pre-create number of devices given by config or max_loop */ - mutex_lock(&loop_ctl_mutex); for (i = 0; i < nr; i++) loop_add(i); - mutex_unlock(&loop_ctl_mutex); printk(KERN_INFO "loop: module loaded\n"); return 0; From patchwork Wed Jun 23 14:59:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12340055 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13A6DC48BC2 for ; Wed, 23 Jun 2021 15:03:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ED2556113B for ; Wed, 23 Jun 2021 15:03:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231335AbhFWPF7 (ORCPT ); Wed, 23 Jun 2021 11:05:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231326AbhFWPF7 (ORCPT ); Wed, 23 Jun 2021 11:05:59 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9AA4FC061574 for ; Wed, 23 Jun 2021 08:03:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=WKxDoeRIkCXSSsjsUYE8A9fYbi97yUT4NAod0S2S7ps=; b=JlbVlNGKXqiXxWuEnlcDab7w9s Tan0Ssb5ovTomygAvPz4X96Hbcubkc7hpFXvAbAU2F4TNGyEvjJDUzHEznEe14JOvNxYancriewpc 1hjfXLJbk9IOlcoosrp3IUY4nqjEAyanq+Y78xhBxisUPOEPBQ0p7z+R2aip5JhoigFdorJRVsOdK 9ivGwbyGGq3ZFtvzJ67vGEWbm845Y4fn5X0cUUJqFsKC47y4gTQ9sLIk+yI4vFiEM1wrs+SfdbMwN kySYdNRjJTsMnMiP5s5Kz9j80ruH1iyu2YU9pRdBGP0N/MLrKUj3Iuftl07AF+2lRCr4dEgrJjWb3 AD15hsSg==; Received: from [2001:4bb8:188:3e21:6594:49:139:2b3f] (helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1lw4Oz-00FY2b-Fi; Wed, 23 Jun 2021 15:02:57 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Tetsuo Handa , linux-block@vger.kernel.org Subject: [PATCH 7/9] loop: don't allow deleting an unspecified loop device Date: Wed, 23 Jun 2021 16:59:06 +0200 Message-Id: <20210623145908.92973-8-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623145908.92973-1-hch@lst.de> References: <20210623145908.92973-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Passing a negative index to loop_lookup while return any unbound device. Doing that for a delete does not make much sense, so add check to explicitly reject that case. Signed-off-by: Christoph Hellwig --- drivers/block/loop.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 01d393a60b37..58fc29f1b1ae 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -2235,6 +2235,11 @@ static int loop_control_remove(int idx) { struct loop_device *lo; int ret; + + if (idx < 0) { + pr_warn("deleting an unspecified loop device is not supported.\n"); + return -EINVAL; + } ret = mutex_lock_killable(&loop_ctl_mutex); if (ret) From patchwork Wed Jun 23 14:59:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12340057 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 169A1C4743C for ; Wed, 23 Jun 2021 15:04:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EA9206102A for ; Wed, 23 Jun 2021 15:04:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230430AbhFWPGm (ORCPT ); Wed, 23 Jun 2021 11:06:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbhFWPGm (ORCPT ); Wed, 23 Jun 2021 11:06:42 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A752C061574 for ; Wed, 23 Jun 2021 08:04:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=gGDhKtCkRuBgc6lhWraLlq6rPw7pg46tuQY6HEvcUyw=; b=JoYbJgoQWkVoJchVE+AutrobC7 J9VY+zGkFSZjDFHvQWp9aXDwqGMDqghiSYPXD/idFSK3DAnzEycqUXz2rvnYtsCVCTipTUIdZhCkB YQVUkbiOOX9x0AjbnZyVa3qyKm8eyCzsvBH7xthIMwMVcORpBLtZ3Z9KDDLZLPm0H6xWuF8+Ax73h l5e8a/m7wMqEkzE6djY6e7ID/Ml0YToa037bTm6lhjgZ7/tFGzuxN0DQEFpMM9PXSWPsY/JK4yR2q XvBShinuHR4xpOvu41triFftLoI0ATWawGwtQ3sZ6V4T65jNixLAWr28kt3n9Fg3HLPkTn+cZSkyS fWeWwUfQ==; Received: from [2001:4bb8:188:3e21:6594:49:139:2b3f] (helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1lw4Pb-00FY7E-Tu; Wed, 23 Jun 2021 15:03:40 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Tetsuo Handa , linux-block@vger.kernel.org Subject: [PATCH 8/9] loop: split loop_lookup Date: Wed, 23 Jun 2021 16:59:07 +0200 Message-Id: <20210623145908.92973-9-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623145908.92973-1-hch@lst.de> References: <20210623145908.92973-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org loop_lookup has two callers - one wants to do the a find by index and the other wants any unbound loop device. Open code the respective functionality in each caller. Signed-off-by: Christoph Hellwig --- drivers/block/loop.c | 57 ++++++++++---------------------------------- 1 file changed, 12 insertions(+), 45 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 58fc29f1b1ae..93abf6d8b88c 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -2184,44 +2184,6 @@ static void loop_remove(struct loop_device *lo) kfree(lo); } -static int find_free_cb(int id, void *ptr, void *data) -{ - struct loop_device *lo = ptr; - struct loop_device **l = data; - - if (lo->lo_state == Lo_unbound) { - *l = lo; - return 1; - } - return 0; -} - -static int loop_lookup(struct loop_device **l, int i) -{ - struct loop_device *lo; - int ret = -ENODEV; - - if (i < 0) { - int err; - - err = idr_for_each(&loop_index_idr, &find_free_cb, &lo); - if (err == 1) { - *l = lo; - ret = lo->lo_number; - } - goto out; - } - - /* lookup and return a specific i */ - lo = idr_find(&loop_index_idr, i); - if (lo) { - *l = lo; - ret = lo->lo_number; - } -out: - return ret; -} - static void loop_probe(dev_t dev) { int idx = MINOR(dev) >> part_shift; @@ -2245,9 +2207,11 @@ static int loop_control_remove(int idx) if (ret) return ret; - ret = loop_lookup(&lo, idx); - if (ret < 0) + lo = idr_find(&loop_index_idr, idx); + if (!lo) { + ret = -ENODEV; goto out_unlock_ctrl; + } ret = mutex_lock_killable(&lo->lo_mutex); if (ret) @@ -2271,17 +2235,20 @@ static int loop_control_remove(int idx) static int loop_control_get_free(int idx) { struct loop_device *lo; - int ret; + int id, ret; ret = mutex_lock_killable(&loop_ctl_mutex); if (ret) return ret; - ret = loop_lookup(&lo, -1); + idr_for_each_entry(&loop_index_idr, lo, id) { + if (lo->lo_state == Lo_unbound) + goto found; + } mutex_unlock(&loop_ctl_mutex); - - if (ret >= 0) - return ret; return loop_add(-1); +found: + mutex_unlock(&loop_ctl_mutex); + return id; } static long loop_control_ioctl(struct file *file, unsigned int cmd, From patchwork Wed Jun 23 14:59:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12340059 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17C3AC4743C for ; Wed, 23 Jun 2021 15:04:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EE8176102A for ; Wed, 23 Jun 2021 15:04:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230243AbhFWPHO (ORCPT ); Wed, 23 Jun 2021 11:07:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbhFWPHO (ORCPT ); Wed, 23 Jun 2021 11:07:14 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9ACA0C061574 for ; Wed, 23 Jun 2021 08:04:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=UCYBBM8y+++myQaSPaE2GtTh8JIwBF+yqfSzegrr3d8=; b=qyzoJDZTQNBuiZswQEuIoMiPtz yV5ECC6LHiBm6OQ/F4zP9cVsSxYpvUBuaAFbAyb1KOIXoGrRA1vVoy0CFywRt6w1IYKvHu9EBkuD0 7QyxBVNk3AYWbU3QemL+9sZGXe9ystWK7dhuCIibYl75ThMXfNXIhKvOi800eSuTnEuFW4CaJWPwT BXtxt5Z3LWPWGdZnVb/kwIJ0gLvR2zt1rDy9ccfHefBG9rYyaFD52hSjG3bJV3qlGDOpn+xGq1bAi OUfAhW9QzpphbR2Rf1utlsgrh1q54dcykbnWsQDfHt1X22XBjhDUmLB3F4wYq5XP/gtm4qulwz/Br OH6r2ZOg==; Received: from [2001:4bb8:188:3e21:6594:49:139:2b3f] (helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1lw4QK-00FYA8-GG; Wed, 23 Jun 2021 15:04:23 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Tetsuo Handa , linux-block@vger.kernel.org, Chaitanya Kulkarni Subject: [PATCH 9/9] loop: rewrite loop_exit using idr_for_each_entry Date: Wed, 23 Jun 2021 16:59:08 +0200 Message-Id: <20210623145908.92973-10-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623145908.92973-1-hch@lst.de> References: <20210623145908.92973-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Use idr_for_each_entry to simplify removing all devices. Signed-off-by: Christoph Hellwig Reviewed-by: Chaitanya Kulkarni --- drivers/block/loop.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 93abf6d8b88c..40b7c6c470f2 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -2349,21 +2349,17 @@ static int __init loop_init(void) return err; } -static int loop_exit_cb(int id, void *ptr, void *data) -{ - struct loop_device *lo = ptr; - - loop_remove(lo); - return 0; -} - static void __exit loop_exit(void) { + struct loop_device *lo; + int id; + unregister_blkdev(LOOP_MAJOR, "loop"); misc_deregister(&loop_misc); mutex_lock(&loop_ctl_mutex); - idr_for_each(&loop_index_idr, &loop_exit_cb, NULL); + idr_for_each_entry(&loop_index_idr, lo, id) + loop_remove(lo); mutex_unlock(&loop_ctl_mutex); idr_destroy(&loop_index_idr);