Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4215966pxj; Wed, 12 May 2021 00:04:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJylHIYVLsMJKGZG0Fclp5Aq58bH07UYJ0IH7mD5DtBkgaW4RATX5gheU6mJ01z1+uqQXf2y X-Received: by 2002:a50:bb27:: with SMTP id y36mr41533732ede.365.1620803050710; Wed, 12 May 2021 00:04:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620803050; cv=none; d=google.com; s=arc-20160816; b=SenCMInj61HB9Uzj+k37nJajyVTDKbJ0rLL/9GMsS36blfIjOiypCLYMb7Uv3HFhrK lTSH9sPhJjbBqg8epzh9CgVp8BxiHg1PBolpp6ZlcWxyk4D1G4OcMkqczOuklC8db+Rk mzYN1NEWnchWbm27yfKvw1u+emAj8NCF1OykLfnEfDKYweTBbuIwoyQCH1l/EASqzAO+ t2GOkNPigk4zp6ckepyuQfJs1m+4HgoVF3WYSI1klw8vTa23iQoM4HrD15rz2Lul+y8y QDbYxm02eB+PLlRKr2cRPi3Foqdiz8YqjWUlPOwHZOgtyQIUf1vp4Z5FyGgRQuXX7URU rZRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=4L1CM5TjzwngNWpSW5lfB0TalYgC1UQCXnGH/wxMcYc=; b=iph1bmLrp0DC1uhlFayVZAVASwH94OXxjV/DX7gkRkUtqyfO1BmK9rRjfS9x2fmlhP 6bndoMsPM/z6EkNcv+JQxYmd5aBBOdWkmDCWFgXU/Q1RNnW+QRuyt6qAuOjeaSgp6SfK 1B7CIyXYSEAeli2Cj7LKmbK0QsZrVdpAWXnkzVkmKkOZJnQHCISNTBObhQbkqtqA4vDn seU54ORJkVZ6xTGa5AAXqBQ7ahRFnrzHxhsQuQ6fSzUdAH6vDQzGVh6TrUEOJ9VsXQ2r C8myz6+HYLvOtS95vU4B9lVNNP83hL0L3G09P1Ao3PoFbRCWxZhnjyXU4Mq3zKsrid6Z nL0Q== ARC-Authentication-Results: i=1; mx.google.com; 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 br19si19186478ejb.498.2021.05.12.00.03.47; Wed, 12 May 2021 00:04:10 -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; 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 S230333AbhELHDY (ORCPT + 99 others); Wed, 12 May 2021 03:03:24 -0400 Received: from mail-pj1-f54.google.com ([209.85.216.54]:44650 "EHLO mail-pj1-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230292AbhELHDS (ORCPT ); Wed, 12 May 2021 03:03:18 -0400 Received: by mail-pj1-f54.google.com with SMTP id lj11-20020a17090b344bb029015bc3073608so2780136pjb.3; Wed, 12 May 2021 00:02:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4L1CM5TjzwngNWpSW5lfB0TalYgC1UQCXnGH/wxMcYc=; b=SvFqJ5h1Tcq3/m8UE6MEqsRovCIGJEBlhD2/nKQezdfj3e8U9yfUVFFksBz3Rw0ECR fEch/1wpCPtvOm20xWVyv0dtecFtqj7P47zqWXmXxdxS5eQ6mX59UYGgrlew4aKJzGRW 6YzjMl2pBKqhyEFbYLd8VR+tIXuiBypN7QdUx+daPTlc+t8ugInKr9qzB924yF7M4/Ar k8qb2XVxPQ+hnDzfy+3yis5+RQntxayG6i/JLteH3L1h5+it4MJnv4NfhMdM3E6X64xj AQR7Rs6Ph2/+qXz3evIjZSkyGcLzmq3U2EbPZ5PCHGjawu7qMk1fr4tf/DEv9nI73OUa qCfA== X-Gm-Message-State: AOAM531O9yKu3gqbXE7U0P/PrDmHa/gTmSBl/VCBd5J3bGj+aKqRbpeb wyaOQKbiz5T1pIfROdGlvyY= X-Received: by 2002:a17:90a:2844:: with SMTP id p4mr9728467pjf.89.1620802929782; Wed, 12 May 2021 00:02:09 -0700 (PDT) Received: from 42.do-not-panic.com (42.do-not-panic.com. [157.230.128.187]) by smtp.gmail.com with ESMTPSA id n8sm15211264pfu.111.2021.05.12.00.02.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 May 2021 00:02:03 -0700 (PDT) Received: by 42.do-not-panic.com (Postfix, from userid 1000) id 1D7DD42079; Wed, 12 May 2021 06:46:35 +0000 (UTC) From: Luis Chamberlain To: axboe@kernel.dk Cc: bvanassche@acm.org, ming.lei@redhat.com, hch@infradead.org, jack@suse.cz, osandov@fb.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Luis Chamberlain Subject: [PATCH v1 1/8] block: refcount the request_queue early in __device_add_disk() Date: Wed, 12 May 2021 06:46:22 +0000 Message-Id: <20210512064629.13899-2-mcgrof@kernel.org> X-Mailer: git-send-email 2.23.0.rc1 In-Reply-To: <20210512064629.13899-1-mcgrof@kernel.org> References: <20210512064629.13899-1-mcgrof@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We refcount the request_queue right now towards the end of the __device_add_disk(), however when we add error handling on this function we'll want to refcount the request_queue first, to help make less complicated changes on drivers on their error paths. For instance, today a driver may call add_disk without error handling but still handle other errors: int foo_block_add(...) { ... queue = blk_mq_init_queue(...); ... disk->queue = queue; disk = alloc_disk(...); if (!disk) goto out_free_queue; ... add_disk(disk); ... return 0; out_free_queue: blk_cleanup_queue(queue); /* Note: we never call put_disk() as add_disk() never failed */ ... } We want drivers to cleanup with put_disk() on the error path if add_disk() fails. However, calling blk_cleanup_queue() will already put the queue, and so the last put_disk() on the error path will be extra. This can be simplified later if once error handling is added to __device_add_disk(), if refcounting the request_queue fails right away on __device_add_disk() we just return early and set disk->NULL for the driver. That would ensure driver error paths chug on with their error paths, and all they'd need to expand with is the missing put_disk(). The collateral evolution for adding error paths for add_disk() becomes larger with the alternative of replacing the blk_cleanup_queue() with a put_disk(). We'd still need to sprinkle then some blk_cleanup_queue() calls on the driver paths up above prior to add_disk(). And how would we know we reached a part of add_disk() which did refcount then? A related commit is 5a0ec388ef0 ("pktcdvd: Fix pkt_setup_dev() error path") which *had* to take the approach of removing the blk_cleanup_queue() because otherwise the driver crashes. Moving this to the top ensure our future error path can easily just handle this itself. For instance, if it was not able to refcount the request_queue it can disk->queue to NULL, that way allowing a blk_cleanup_queue() call followed but a put_disk(). And if the refcount was incremented, we'd still be able to keep the same error path of blk_cleanup_queue() followed by put_disk(). Signed-off-by: Luis Chamberlain --- block/genhd.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/block/genhd.c b/block/genhd.c index 39ca97b0edc6..51dff87c4756 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -504,6 +504,12 @@ static void __device_add_disk(struct device *parent, struct gendisk *disk, dev_t devt; int retval; + /* + * Take an extra ref on queue which will be put on disk_release() + * so that it sticks around as long as @disk is there. + */ + WARN_ON_ONCE(!blk_get_queue(disk->queue)); + /* * The disk queue should now be all set with enough information about * the device for the elevator code to pick an adequate default @@ -556,12 +562,6 @@ static void __device_add_disk(struct device *parent, struct gendisk *disk, if (register_queue) blk_register_queue(disk); - /* - * Take an extra ref on queue which will be put on disk_release() - * so that it sticks around as long as @disk is there. - */ - WARN_ON_ONCE(!blk_get_queue(disk->queue)); - disk_add_events(disk); blk_integrity_add(disk); } -- 2.30.2