Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp800565pxb; Thu, 21 Oct 2021 09:42:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy8yb4yUr4cavv2RR2RCKJjoDUcgbNJ2l/0iOnPumSvYoy5kJxluLSanFIMJrpl1USF7x7H X-Received: by 2002:a05:6402:42c8:: with SMTP id i8mr9406597edc.349.1634834542446; Thu, 21 Oct 2021 09:42:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634834542; cv=none; d=google.com; s=arc-20160816; b=Q8REukAhcfT1xMLuKGDAVGYetDdYfMLFlNhhn8j2fJF06KY2G2DdzTt300Uae4i5FX 2/AxzzSmdv2YHYrflnMkuaJ44sZt79miU2+5wb9GltA9rXWlFeyPkwm+YfFh9FNmXFNN 6ROOjmlNT0jDI2K4ASrfIoP1FiEuwyYLOoju5pc4TDgPBNoiSW6upYmPT4MvwJg6UEjf zUnBLbNKrRIkEyYfK1qZTr/twExdZUSYbtGthnjbsjzVpL+/8vsC4ttwCIjtT0UJ5WAB 4qDLSJ5C08PLCeLvwXWuPA27G7CO+sSdO2GEkPwrgz89c7HkpS/F/CXjmIOj4LKda1ZK LOSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=lGkULHYlBjDIPGiB/BE49QsU3PYu09QZP7ud3a4qHGY=; b=eIepe+MO3qAK6TZlEZhbPQI8f/WA61wTqTC6v8mjh5tJ0cswmYamXzovC7t2mzwQuo YtFp18NwN2Lsar/tcduOqLDvmnbGuksEYNDbjhUTJ3RFu7I/IAbZjADY0GxDsfgPHaJH J9SiR1GXdAe2faydS+TwbbsTJPHgg02/7oPkxe14I7VHiSBzRwEcrLl1Ff3g0IO/uH1o hKeNvGFsBk42t+W2eum498RUrQ6bvfGID+uCICqkXUCfbahL1Zvcv/rWHsOX7vqMYE1Z 36em3+08UY1IBEQe8pzn5pwkHjCp/4+i2QHTUDi7AWJNtcJg0kolXKFhl5MO+eIgeZWp b0Pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=e+di7685; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gm1si9686194ejc.106.2021.10.21.09.41.58; Thu, 21 Oct 2021 09:42:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=e+di7685; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232126AbhJUQl2 (ORCPT + 99 others); Thu, 21 Oct 2021 12:41:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231248AbhJUQlW (ORCPT ); Thu, 21 Oct 2021 12:41:22 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95A08C061764; Thu, 21 Oct 2021 09:39:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=lGkULHYlBjDIPGiB/BE49QsU3PYu09QZP7ud3a4qHGY=; b=e+di7685rK0Ua2QBhEel7x35Px 3s9d5JRAOPeveFkDA/VOmRmn9bcR4GTZGXpjO9ktTY3II/UvdclAeQ67CM6rbgnx0w+CCHMBO4i8V 8/tDbERibVZ6ZlTNPzN8Aq76Ia7jChl6tJuZxW9j4ftW5W8Kr30QWFPEPbR/ByTkaRozPqAFiuZhq 3Fs+OgC0I7oXorZrTRLykSvVJrShdqoO+/njEZ+LdVpITTPF15ynxc697N8JyVnfAoBRwR9QsLkjQ YEwOd74lmXfDOV3cIoNRUcjAVQMfCxr0p+2i+zru5oqreCfjvOMRB7yV0704XRlasxTz7VDw/wTZD Ru2964cA==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdb5l-008OYw-VX; Thu, 21 Oct 2021 16:38:57 +0000 From: Luis Chamberlain To: axboe@kernel.dk, hch@lst.de, penguin-kernel@i-love.sakura.ne.jp, schmitzmic@gmail.com, efremov@linux.com, song@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, viro@zeniv.linux.org.uk, hare@suse.de, jack@suse.cz, ming.lei@redhat.com, tj@kernel.org Cc: linux-raid@vger.kernel.org, linux-scsi@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Tetsuo Handa , Luis Chamberlain Subject: [PATCH v3 1/3] ataflop: remove ataflop_probe_lock mutex Date: Thu, 21 Oct 2021 09:38:54 -0700 Message-Id: <20211021163856.2000993-2-mcgrof@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211021163856.2000993-1-mcgrof@kernel.org> References: <20211021163856.2000993-1-mcgrof@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: Luis Chamberlain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tetsuo Handa Commit bf9c0538e485b591 ("ataflop: use a separate gendisk for each media format") introduced ataflop_probe_lock mutex, but forgot to unlock the mutex when atari_floppy_init() (i.e. module loading) succeeded. This will result in double lock deadlock if ataflop_probe() is called. Also, unregister_blkdev() must not be called from atari_floppy_init() with ataflop_probe_lock held when atari_floppy_init() failed, for ataflop_probe() waits for ataflop_probe_lock with major_names_lock held (i.e. AB-BA deadlock). __register_blkdev() needs to be called last in order to avoid calling ataflop_probe() when atari_floppy_init() is about to fail, for memory for completing already-started ataflop_probe() safely will be released as soon as atari_floppy_init() released ataflop_probe_lock mutex. As with commit 8b52d8be86d72308 ("loop: reorder loop_exit"), unregister_blkdev() needs to be called first in order to avoid calling ataflop_alloc_disk() from ataflop_probe() after del_gendisk() from atari_floppy_exit(). By relocating __register_blkdev() / unregister_blkdev() as explained above, we can remove ataflop_probe_lock mutex, for probe function and __exit function are serialized by major_names_lock mutex. Signed-off-by: Tetsuo Handa Fixes: bf9c0538e485b591 ("ataflop: use a separate gendisk for each media format") Reviewed-by: Luis Chamberlain Tested-by: Michael Schmitz --- drivers/block/ataflop.c | 47 +++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/drivers/block/ataflop.c b/drivers/block/ataflop.c index 3d217f1d7440..9aecd37ea8a5 100644 --- a/drivers/block/ataflop.c +++ b/drivers/block/ataflop.c @@ -1976,8 +1976,6 @@ static int ataflop_alloc_disk(unsigned int drive, unsigned int type) return 0; } -static DEFINE_MUTEX(ataflop_probe_lock); - static void ataflop_probe(dev_t dev) { int drive = MINOR(dev) & 3; @@ -1988,14 +1986,32 @@ static void ataflop_probe(dev_t dev) if (drive >= FD_MAX_UNITS || type >= NUM_DISK_MINORS) return; - mutex_lock(&ataflop_probe_lock); if (!unit[drive].disk[type]) { if (ataflop_alloc_disk(drive, type) == 0) { add_disk(unit[drive].disk[type]); unit[drive].registered[type] = true; } } - mutex_unlock(&ataflop_probe_lock); +} + +static void atari_floppy_cleanup(void) +{ + int i; + int type; + + for (i = 0; i < FD_MAX_UNITS; i++) { + for (type = 0; type < NUM_DISK_MINORS; type++) { + if (!unit[i].disk[type]) + continue; + del_gendisk(unit[i].disk[type]); + blk_cleanup_queue(unit[i].disk[type]->queue); + put_disk(unit[i].disk[type]); + } + blk_mq_free_tag_set(&unit[i].tag_set); + } + + del_timer_sync(&fd_timer); + atari_stram_free(DMABuffer); } static void atari_cleanup_floppy_disk(struct atari_floppy_struct *fs) @@ -2021,11 +2037,6 @@ static int __init atari_floppy_init (void) /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */ return -ENODEV; - mutex_lock(&ataflop_probe_lock); - ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe); - if (ret) - goto out_unlock; - for (i = 0; i < FD_MAX_UNITS; i++) { memset(&unit[i].tag_set, 0, sizeof(unit[i].tag_set)); unit[i].tag_set.ops = &ataflop_mq_ops; @@ -2081,7 +2092,12 @@ static int __init atari_floppy_init (void) UseTrackbuffer ? "" : "no "); config_types(); - return 0; + ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe); + if (ret) { + printk(KERN_ERR "atari_floppy_init: cannot register block device\n"); + atari_floppy_cleanup(); + } + return ret; err_out_dma: atari_stram_free(DMABuffer); @@ -2089,9 +2105,6 @@ static int __init atari_floppy_init (void) while (--i >= 0) atari_cleanup_floppy_disk(&unit[i]); - unregister_blkdev(FLOPPY_MAJOR, "fd"); -out_unlock: - mutex_unlock(&ataflop_probe_lock); return ret; } @@ -2136,14 +2149,8 @@ __setup("floppy=", atari_floppy_setup); static void __exit atari_floppy_exit(void) { - int i; - - for (i = 0; i < FD_MAX_UNITS; i++) - atari_cleanup_floppy_disk(&unit[i]); unregister_blkdev(FLOPPY_MAJOR, "fd"); - - del_timer_sync(&fd_timer); - atari_stram_free( DMABuffer ); + atari_floppy_cleanup(); } module_init(atari_floppy_init) -- 2.30.2