From patchwork Tue Mar 8 05:51:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 549621 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA0F0C43217 for ; Tue, 8 Mar 2022 05:52:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236050AbiCHFxH (ORCPT ); Tue, 8 Mar 2022 00:53:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233708AbiCHFxF (ORCPT ); Tue, 8 Mar 2022 00:53:05 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81FEEC1C; Mon, 7 Mar 2022 21:52:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=O6z0IYbmbkmAh88yybQCF6FYRkBw9zbZ117XaxaFvAs=; b=kJdWMTuHboJZEyGA/dnmkvdAPC iUHIdtnbKlJq7aDIfMiLorcCCXslcFPLvAzDTO7V9FQX578AuK+PnX9EfQEV7C68FcQ4Y3sf+G7jC 7NbS5VSSpfbGUCJcbJWbDk5bp1H1OuIteyTvmI0LdW+r4A7nxcnvX4UaztgUBqjJky6oNF5E11U0F J657rDQrzJnouuS/NxY7OmMIvJV6CnoW7V+ITbd5f/v3lAUrIiRzROH+z6g8Mnwx5xrIUXAOvqAfW TwugHpj+qdhF3RSsmAvydVnEUoDqhUNz60keSHMDYAUkj4hheSDTUE5HxB23mbMhC1pFlbO6JtUnI F7+A7o/A==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSlQ-002ikq-SI; Tue, 08 Mar 2022 05:52:05 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org, Chaitanya Kulkarni Subject: [PATCH 01/14] blk-mq: do not include passthrough requests in I/O accounting Date: Tue, 8 Mar 2022 06:51:47 +0100 Message-Id: <20220308055200.735835-2-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org I/O accounting buckets I/O into the read/write/discard categories into which passthrough I/O does not fit at all. It also accounts to the block_device, which may not even exist for passthrough I/O. Signed-off-by: Christoph Hellwig Reviewed-by: Bart Van Assche Reviewed-by: Chaitanya Kulkarni Reviewed-by: Martin K. Petersen --- block/blk-mq.c | 11 ++++++++--- block/blk.h | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index a05ce77250316..ab4b646551334 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -883,10 +883,15 @@ static inline void blk_account_io_done(struct request *req, u64 now) static void __blk_account_io_start(struct request *rq) { - /* passthrough requests can hold bios that do not have ->bi_bdev set */ - if (rq->bio && rq->bio->bi_bdev) + /* + * All non-passthrough requests are created from a bio with one + * exception: when a flush command that is part of a flush sequence + * generated by the state machine in blk-flush.c is cloned onto the + * lower device by dm-multipath we can get here without a bio. + */ + if (rq->bio) rq->part = rq->bio->bi_bdev; - else if (rq->q->disk) + else rq->part = rq->q->disk->part0; part_stat_lock(); diff --git a/block/blk.h b/block/blk.h index ebaa59ca46ca6..6f21859c7f0ff 100644 --- a/block/blk.h +++ b/block/blk.h @@ -325,7 +325,7 @@ int blk_dev_init(void); */ static inline bool blk_do_io_stat(struct request *rq) { - return (rq->rq_flags & RQF_IO_STAT) && rq->q->disk; + return (rq->rq_flags & RQF_IO_STAT) && !blk_rq_is_passthrough(rq); } void update_io_ticks(struct block_device *part, unsigned long now, bool end); From patchwork Tue Mar 8 05:51:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 550021 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17BBCC433FE for ; Tue, 8 Mar 2022 05:52:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238210AbiCHFxO (ORCPT ); Tue, 8 Mar 2022 00:53:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236018AbiCHFxG (ORCPT ); Tue, 8 Mar 2022 00:53:06 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83B1FC1C; Mon, 7 Mar 2022 21:52:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=EhggpzGbDBqxfiiX/p0aj5KOXJOK0wNvzGJmqfILDUA=; b=bUNCPvDXO4TuisINGJNRLHkOst 8zozLVvYn6lDsZNIW1gF2Z5YbOyDyZpCZ2+3SYpz1meXIqzbIn2uCQc7Tl6dHsOE7Mf/+IoCg+M0p prr14xV5YhN3srj2fZZplBWtjV/WCNNzcBcugADg4enjAeRA9u8mg1ykeuAiQ3G/lIE/m9A7U+K9J f8/ni76mCmjScQFVriFQflSIAoule4VzkyNpU6UUGUpv0vkxJo0/E24aDFDdwkxQp9ttjL62vDmOI 68/iRAUBrNoCXFFAcJj+Vl/sP36Jbo1CTB3v55MOZr/3ntVW2YzYz0ESNuJXyg2sOptBYBvdcpO+B O/cUK7ow==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSlT-002il0-BZ; Tue, 08 Mar 2022 05:52:07 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 02/14] blk-mq: handle already freed tags gracefully in blk_mq_free_rqs Date: Tue, 8 Mar 2022 06:51:48 +0100 Message-Id: <20220308055200.735835-3-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org From: Ming Lei To simplify further changes allow for double calling blk_mq_free_rqs on a queue. Signed-off-by: Ming Lei [hch: split out from a larger patch] Signed-off-by: Christoph Hellwig Reviewed-by: Bart Van Assche Reviewed-by: Martin K. Petersen --- block/blk-mq.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/block/blk-mq.c b/block/blk-mq.c index ab4b646551334..6fd0b0f652514 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -3070,6 +3070,9 @@ void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, struct blk_mq_tags *drv_tags; struct page *page; + if (list_empty(&tags->page_list)) + return; + if (blk_mq_is_shared_tags(set->flags)) drv_tags = set->shared_tags; else From patchwork Tue Mar 8 05:51:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 549620 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F94EC43219 for ; Tue, 8 Mar 2022 05:52:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242219AbiCHFxP (ORCPT ); Tue, 8 Mar 2022 00:53:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237389AbiCHFxK (ORCPT ); Tue, 8 Mar 2022 00:53:10 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B094F68; Mon, 7 Mar 2022 21:52:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=olqNH2ZSkn6K/fZ+9YrPRQpZDuDq0jOuMDnw88L12GM=; b=kLzc++bhOu2AHGEKLAF2ZAkR+g JDhta2N9Q/wLd5DEOFqeHZdgl6DnsSxP/1D6J+txmAzIwxR8QLE7oVvtMLKgpzW7jL4yDE4yZqO3q DEoo68oKoMpSc9X+AktrJD5T4wROMpdNyK/QOwVHPD7wReQhJA4Vh1PTj1JW9cKC5tx3WK2Nzq259 L4zo6diUc0DweM8XlklYMtuPz+ePGvCYsoZgBJWipMPXu26HtTMCwZttMoZcFq+JIDBV/7T2vs0Vo aD5TXTffmd1zlTRfaIhV6/yfUWx3MdVfBIqnmTVXgYeozLbMfUfKhbksok0EeW/2WKKdFsqvyoBCJ TbcxvL5Q==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSlV-002ilA-Sw; Tue, 08 Mar 2022 05:52:10 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org, Chaitanya Kulkarni Subject: [PATCH 03/14] scsi: don't use disk->private_data to find the scsi_driver Date: Tue, 8 Mar 2022 06:51:49 +0100 Message-Id: <20220308055200.735835-4-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Requiring every ULP to have the scsi_drive as first member of the private data is rather fragile and not necessary anyway. Just use the driver hanging off the SCSI device instead. Signed-off-by: Christoph Hellwig Reviewed-by: Bart Van Assche Reviewed-by: Chaitanya Kulkarni Reviewed-by: Ming Lei Reviewed-by: Martin K. Petersen --- drivers/scsi/sd.c | 3 +-- drivers/scsi/sd.h | 3 +-- drivers/scsi/sr.c | 5 ++--- drivers/scsi/sr.h | 1 - drivers/scsi/st.c | 1 - drivers/scsi/st.h | 1 - include/scsi/scsi_cmnd.h | 9 --------- include/scsi/scsi_driver.h | 9 +++++++-- 8 files changed, 11 insertions(+), 21 deletions(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 2d648d27bfd71..2a1e19e871d30 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -3515,7 +3515,6 @@ static int sd_probe(struct device *dev) } sdkp->device = sdp; - sdkp->driver = &sd_template; sdkp->disk = gd; sdkp->index = index; sdkp->max_retries = SD_MAX_RETRIES; @@ -3548,7 +3547,7 @@ static int sd_probe(struct device *dev) gd->minors = SD_MINORS; gd->fops = &sd_fops; - gd->private_data = &sdkp->driver; + gd->private_data = sdkp; /* defaults, until the device tells us otherwise */ sdp->sector_size = 512; diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h index 2e5932bde43d1..303aa1c23aefb 100644 --- a/drivers/scsi/sd.h +++ b/drivers/scsi/sd.h @@ -68,7 +68,6 @@ enum { }; struct scsi_disk { - struct scsi_driver *driver; /* always &sd_template */ struct scsi_device *device; struct device dev; struct gendisk *disk; @@ -131,7 +130,7 @@ struct scsi_disk { static inline struct scsi_disk *scsi_disk(struct gendisk *disk) { - return container_of(disk->private_data, struct scsi_disk, driver); + return disk->private_data; } #define sd_printk(prefix, sdsk, fmt, a...) \ diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c index f925b1f1f9ada..569bda76a5175 100644 --- a/drivers/scsi/sr.c +++ b/drivers/scsi/sr.c @@ -147,7 +147,7 @@ static void sr_kref_release(struct kref *kref); static inline struct scsi_cd *scsi_cd(struct gendisk *disk) { - return container_of(disk->private_data, struct scsi_cd, driver); + return disk->private_data; } static int sr_runtime_suspend(struct device *dev) @@ -692,7 +692,6 @@ static int sr_probe(struct device *dev) cd->device = sdev; cd->disk = disk; - cd->driver = &sr_template; cd->capacity = 0x1fffff; cd->device->changed = 1; /* force recheck CD type */ cd->media_present = 1; @@ -713,7 +712,7 @@ static int sr_probe(struct device *dev) sr_vendor_init(cd); set_capacity(disk, cd->capacity); - disk->private_data = &cd->driver; + disk->private_data = cd; if (register_cdrom(disk, &cd->cdi)) goto fail_minor; diff --git a/drivers/scsi/sr.h b/drivers/scsi/sr.h index 1609f02ed29ac..d80af3fcb6f97 100644 --- a/drivers/scsi/sr.h +++ b/drivers/scsi/sr.h @@ -32,7 +32,6 @@ struct scsi_device; typedef struct scsi_cd { - struct scsi_driver *driver; unsigned capacity; /* size in blocks */ struct scsi_device *device; unsigned int vendor; /* vendor code, see sr_vendor.c */ diff --git a/drivers/scsi/st.c b/drivers/scsi/st.c index e869e90e05afe..ebe9412c86f43 100644 --- a/drivers/scsi/st.c +++ b/drivers/scsi/st.c @@ -4276,7 +4276,6 @@ static int st_probe(struct device *dev) goto out_buffer_free; } kref_init(&tpnt->kref); - tpnt->driver = &st_template; tpnt->device = SDp; if (SDp->scsi_level <= 2) diff --git a/drivers/scsi/st.h b/drivers/scsi/st.h index c0ef0d9aaf8a2..7a68eaba7e810 100644 --- a/drivers/scsi/st.h +++ b/drivers/scsi/st.h @@ -117,7 +117,6 @@ struct scsi_tape_stats { /* The tape drive descriptor */ struct scsi_tape { - struct scsi_driver *driver; struct scsi_device *device; struct mutex lock; /* For serialization */ struct completion wait; /* For SCSI commands */ diff --git a/include/scsi/scsi_cmnd.h b/include/scsi/scsi_cmnd.h index 6794d7322cbde..e3a4c67794b14 100644 --- a/include/scsi/scsi_cmnd.h +++ b/include/scsi/scsi_cmnd.h @@ -13,7 +13,6 @@ #include struct Scsi_Host; -struct scsi_driver; /* * MAX_COMMAND_SIZE is: @@ -159,14 +158,6 @@ static inline void *scsi_cmd_priv(struct scsi_cmnd *cmd) return cmd + 1; } -/* make sure not to use it with passthrough commands */ -static inline struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd) -{ - struct request *rq = scsi_cmd_to_rq(cmd); - - return *(struct scsi_driver **)rq->q->disk->private_data; -} - void scsi_done(struct scsi_cmnd *cmd); extern void scsi_finish_command(struct scsi_cmnd *cmd); diff --git a/include/scsi/scsi_driver.h b/include/scsi/scsi_driver.h index 6dffa8555a390..4ce1988b2ba01 100644 --- a/include/scsi/scsi_driver.h +++ b/include/scsi/scsi_driver.h @@ -4,11 +4,10 @@ #include #include +#include struct module; struct request; -struct scsi_cmnd; -struct scsi_device; struct scsi_driver { struct device_driver gendrv; @@ -31,4 +30,10 @@ extern int scsi_register_interface(struct class_interface *); #define scsi_unregister_interface(intf) \ class_interface_unregister(intf) +/* make sure not to use it with passthrough commands */ +static inline struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd) +{ + return to_scsi_driver(cmd->device->sdev_gendev.driver); +} + #endif /* _SCSI_SCSI_DRIVER_H */ From patchwork Tue Mar 8 05:51:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 550020 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1F340C4321E for ; Tue, 8 Mar 2022 05:52:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237389AbiCHFxR (ORCPT ); Tue, 8 Mar 2022 00:53:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237449AbiCHFxL (ORCPT ); Tue, 8 Mar 2022 00:53:11 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63BF11FCC6; Mon, 7 Mar 2022 21:52:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=hJeqyUHmsPbFplsMKP9CPrSZ3RWVJMmkYTfvXkgnPLQ=; b=MgPJ99B2Y1kJHvenp1BerxyymB 3+/goUuNgPu3GYVBgM3pzHnIPpt03PmMdeH9jGkYGVhsBtTSyuG12QIN21iIEQOPggemtgnBq8f+1 Ir+xdXSZvewc5Bwjxf/bRy4P4RfqkvcKJLlUE3QO3tKnUzMZm8vuxPEV6lw3uSEUf0quXjoB/isDm 7lzg8MLVrlOV+s8AI7sqdTGrQE0tLs/i7G9FFPIWVEHM2GLaquKBjSFKj2ZjevDo2cf8fyA+3JsJP dqLAWBQaBGpxAyY0aw65gy/v0NJBuyhd752HLegJf2J2ojVdKgZ7v45VT8Df3roK0c3xV/oFz/Nzc NWWkZdCg==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSlY-002ilQ-ER; Tue, 08 Mar 2022 05:52:12 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org, Chaitanya Kulkarni Subject: [PATCH 04/14] sd: rename the scsi_disk.dev field Date: Tue, 8 Mar 2022 06:51:50 +0100 Message-Id: <20220308055200.735835-5-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org dev is very hard to grep for. Give the field a more descriptive name and documents its purpose. Signed-off-by: Christoph Hellwig Reviewed-by: Bart Van Assche Reviewed-by: Chaitanya Kulkarni Reviewed-by: Ming Lei Reviewed-by: Martin K. Petersen --- drivers/scsi/sd.c | 22 +++++++++++----------- drivers/scsi/sd.h | 9 +++++++-- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 2a1e19e871d30..7479e7cb36b43 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -672,7 +672,7 @@ static struct scsi_disk *scsi_disk_get(struct gendisk *disk) if (disk->private_data) { sdkp = scsi_disk(disk); if (scsi_device_get(sdkp->device) == 0) - get_device(&sdkp->dev); + get_device(&sdkp->disk_dev); else sdkp = NULL; } @@ -685,7 +685,7 @@ static void scsi_disk_put(struct scsi_disk *sdkp) struct scsi_device *sdev = sdkp->device; mutex_lock(&sd_ref_mutex); - put_device(&sdkp->dev); + put_device(&sdkp->disk_dev); scsi_device_put(sdev); mutex_unlock(&sd_ref_mutex); } @@ -3529,14 +3529,14 @@ static int sd_probe(struct device *dev) SD_MOD_TIMEOUT); } - device_initialize(&sdkp->dev); - sdkp->dev.parent = get_device(dev); - sdkp->dev.class = &sd_disk_class; - dev_set_name(&sdkp->dev, "%s", dev_name(dev)); + device_initialize(&sdkp->disk_dev); + sdkp->disk_dev.parent = get_device(dev); + sdkp->disk_dev.class = &sd_disk_class; + dev_set_name(&sdkp->disk_dev, "%s", dev_name(dev)); - error = device_add(&sdkp->dev); + error = device_add(&sdkp->disk_dev); if (error) { - put_device(&sdkp->dev); + put_device(&sdkp->disk_dev); goto out; } @@ -3577,7 +3577,7 @@ static int sd_probe(struct device *dev) error = device_add_disk(dev, gd, NULL); if (error) { - put_device(&sdkp->dev); + put_device(&sdkp->disk_dev); goto out; } @@ -3628,7 +3628,7 @@ static int sd_remove(struct device *dev) sdkp = dev_get_drvdata(dev); scsi_autopm_get_device(sdkp->device); - device_del(&sdkp->dev); + device_del(&sdkp->disk_dev); del_gendisk(sdkp->disk); sd_shutdown(dev); @@ -3636,7 +3636,7 @@ static int sd_remove(struct device *dev) mutex_lock(&sd_ref_mutex); dev_set_drvdata(dev, NULL); - put_device(&sdkp->dev); + put_device(&sdkp->disk_dev); mutex_unlock(&sd_ref_mutex); return 0; diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h index 303aa1c23aefb..0a33a4b68ffbc 100644 --- a/drivers/scsi/sd.h +++ b/drivers/scsi/sd.h @@ -69,7 +69,12 @@ enum { struct scsi_disk { struct scsi_device *device; - struct device dev; + + /* + * disk_dev is used to show attributes in /sys/class/scsi_disk/, + * but otherwise not really needed. Do not use for refcounting. + */ + struct device disk_dev; struct gendisk *disk; struct opal_dev *opal_dev; #ifdef CONFIG_BLK_DEV_ZONED @@ -126,7 +131,7 @@ struct scsi_disk { unsigned security : 1; unsigned ignore_medium_access_errors : 1; }; -#define to_scsi_disk(obj) container_of(obj,struct scsi_disk,dev) +#define to_scsi_disk(obj) container_of(obj, struct scsi_disk, disk_dev) static inline struct scsi_disk *scsi_disk(struct gendisk *disk) { From patchwork Tue Mar 8 05:51:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 549619 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A744AC43217 for ; Tue, 8 Mar 2022 05:52:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244530AbiCHFxS (ORCPT ); Tue, 8 Mar 2022 00:53:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242260AbiCHFxP (ORCPT ); Tue, 8 Mar 2022 00:53:15 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E3D9F68; Mon, 7 Mar 2022 21:52:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=Kmr8DQV2AQKxHmpsIFQdnpKEXmC5Yomzad1nAgCe54c=; b=ftZhHow4+k1jlEl9lab6mIeaan xH6DlZ2uGJ2QUJRF9/0qeliz19bxg8qsK3rMJUvqaH5LoTtesatIR01hKYCNOKyDt/7oGsxnGo3Hj PlWMEGlt3tvjoal2WrXj69EPOT+mOZSndj+M+GBYSVBorKiXjp1oKPxn20rj61/WghCyziLn8L9r7 xOUoWUWbxiW3zKitHH/lGHlYibgyB4HYXjZ/l81u2QmM2jBb+mPaaWPGLsz11h061bt4nUUaFdyjS hwjbYT8/CMRCKM79ihSLgG6C+9rPRG2w+rRZmbw2PAM3eJB/ceiJxnegwiM0in0JNjCjSAATWaIwl dRCwU8Kg==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSla-002imd-UX; Tue, 08 Mar 2022 05:52:15 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 05/14] sd: call sd_zbc_release_disk before releasing the scsi_device reference Date: Tue, 8 Mar 2022 06:51:51 +0100 Message-Id: <20220308055200.735835-6-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org sd_zbc_release_disk accesses disk->device, so ensure that actually still has a valid reference. Signed-off-by: Christoph Hellwig Reviewed-by: Bart Van Assche Reviewed-by: Ming Lei Reviewed-by: Martin K. Petersen --- drivers/scsi/sd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 7479e7cb36b43..7bfebf5b2832d 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -3672,9 +3672,9 @@ static void scsi_disk_release(struct device *dev) disk->private_data = NULL; put_disk(disk); - put_device(&sdkp->device->sdev_gendev); sd_zbc_release_disk(sdkp); + put_device(&sdkp->device->sdev_gendev); kfree(sdkp); } From patchwork Tue Mar 8 05:51:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 550019 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC95CC433EF for ; Tue, 8 Mar 2022 05:52:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343561AbiCHFxe (ORCPT ); Tue, 8 Mar 2022 00:53:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242260AbiCHFxV (ORCPT ); Tue, 8 Mar 2022 00:53:21 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 169472B261; Mon, 7 Mar 2022 21:52:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=ey4iYGQo/5eUoycN3wqBUIHIMNTI6TKepd9z/u4kEp4=; b=3p+4hiAqO05ZHr/jAr1ZaPqMPa s26SoDDmkgnRCJLAu36MwfXd4fh2HlZpSv4/TwOCNiLg+8XtlXVEEy73UJLeE1ko1ODegSh14Otwr 8lEqvNw8yxx9j8XV/lxAxXKoudDrmH0FjknvkSPxns3gPV1It9z1NEJwPx58Hu+Q8YTbR1SUjcmUV VIoHRgmrvqdmko1Te8+vrsDeTowDeTMN7hWguS6LgcUzgpB27+/jNPwgfQIbU8zVAi9Vfjg+jPb1Y otdninwaokpPLVyZrw9dc3lDsY+GjowA/L8Ou+32mT0ka/YDfh30Rgq3uJt1l1xwBpnO3ZZrJInlC QdRNja6w==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSld-002ino-CS; Tue, 08 Mar 2022 05:52:17 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org, Chaitanya Kulkarni Subject: [PATCH 06/14] sd: delay calling free_opal_dev Date: Tue, 8 Mar 2022 06:51:52 +0100 Message-Id: <20220308055200.735835-7-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Call free_opal_dev from scsi_disk_release as the opal_dev field is accessed from the ioctl handler, which isn't synchronized vs sd_release and thus can be accessed during or after sd_release was called. Signed-off-by: Christoph Hellwig Reviewed-by: Bart Van Assche Reviewed-by: Chaitanya Kulkarni Reviewed-by: Ming Lei Reviewed-by: Martin K. Petersen --- drivers/scsi/sd.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 7bfebf5b2832d..346b8d62de7d1 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -3632,8 +3632,6 @@ static int sd_remove(struct device *dev) del_gendisk(sdkp->disk); sd_shutdown(dev); - free_opal_dev(sdkp->opal_dev); - mutex_lock(&sd_ref_mutex); dev_set_drvdata(dev, NULL); put_device(&sdkp->disk_dev); @@ -3675,6 +3673,7 @@ static void scsi_disk_release(struct device *dev) sd_zbc_release_disk(sdkp); put_device(&sdkp->device->sdev_gendev); + free_opal_dev(sdkp->opal_dev); kfree(sdkp); } From patchwork Tue Mar 8 05:51:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 549618 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4F91CC433EF for ; Tue, 8 Mar 2022 05:52:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344378AbiCHFxg (ORCPT ); Tue, 8 Mar 2022 00:53:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343782AbiCHFxe (ORCPT ); Tue, 8 Mar 2022 00:53:34 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14BDBD7F; Mon, 7 Mar 2022 21:52:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=gE6pOLmsfSp3ZRv8RixKxwhCbuvenlNJg6rpkeQQsVM=; b=AFnRXTQzI0EfNaZw9q4XGbehjE UIzSMH5XdPW3yd1SMGXizYWEU3ywhiEEvIbUva2LOZAy0pfH5GUIjWj7PKxxWxGh/5Y4JWy7Myf1l eJA8ld+cUyw9z+oMuuvRGUgJzdqNU1hr9gwR8egZmbrkEGFr+iQ8nYqB62i2bRlc/z6LkGNqohLt9 NXd3E4afOZ/l/SOvOrpGhj+EOUyiPSBBJKfGIhuMlF4FZfF1LrprlLB7OmxV3H6H2LvDr5Ob37UAo OeT5MNWyMzNt4aecieTq+MGRx5B1nGPM5VWCD1WUJ8JYB9X2RTSkySDdp5ZcxzxUjxeC7DeKaB9Gx iWiPrAjw==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSlf-002ioY-Px; Tue, 08 Mar 2022 05:52:20 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 07/14] sd: implement ->free_disk to simplify refcounting Date: Tue, 8 Mar 2022 06:51:53 +0100 Message-Id: <20220308055200.735835-8-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Implement the ->free_disk method to to put struct scsi_disk when the last gendisk reference count goes away. This removes the need to clear ->private_data and thus freeze the queue on unbind. Signed-off-by: Christoph Hellwig Reviewed-by: Ming Lei Reviewed-by: Martin K. Petersen Reviewed-by: Bart Van Assche --- drivers/scsi/sd.c | 90 +++++++++-------------------------------------- 1 file changed, 16 insertions(+), 74 deletions(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 346b8d62de7d1..73e6f5f0f37c2 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -121,11 +121,6 @@ static void scsi_disk_release(struct device *cdev); static DEFINE_IDA(sd_index_ida); -/* This semaphore is used to mediate the 0->1 reference get in the - * face of object destruction (i.e. we can't allow a get on an - * object after last put) */ -static DEFINE_MUTEX(sd_ref_mutex); - static struct kmem_cache *sd_cdb_cache; static mempool_t *sd_cdb_pool; static mempool_t *sd_page_pool; @@ -663,33 +658,6 @@ static int sd_major(int major_idx) } } -static struct scsi_disk *scsi_disk_get(struct gendisk *disk) -{ - struct scsi_disk *sdkp = NULL; - - mutex_lock(&sd_ref_mutex); - - if (disk->private_data) { - sdkp = scsi_disk(disk); - if (scsi_device_get(sdkp->device) == 0) - get_device(&sdkp->disk_dev); - else - sdkp = NULL; - } - mutex_unlock(&sd_ref_mutex); - return sdkp; -} - -static void scsi_disk_put(struct scsi_disk *sdkp) -{ - struct scsi_device *sdev = sdkp->device; - - mutex_lock(&sd_ref_mutex); - put_device(&sdkp->disk_dev); - scsi_device_put(sdev); - mutex_unlock(&sd_ref_mutex); -} - #ifdef CONFIG_BLK_SED_OPAL static int sd_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len, bool send) @@ -1418,17 +1386,15 @@ static bool sd_need_revalidate(struct block_device *bdev, **/ static int sd_open(struct block_device *bdev, fmode_t mode) { - struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk); - struct scsi_device *sdev; + struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk); + struct scsi_device *sdev = sdkp->device; int retval; - if (!sdkp) + if (scsi_device_get(sdev)) return -ENXIO; SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n")); - sdev = sdkp->device; - /* * If the device is in error recovery, wait until it is done. * If the device is offline, then disallow any access to it. @@ -1473,7 +1439,7 @@ static int sd_open(struct block_device *bdev, fmode_t mode) return 0; error_out: - scsi_disk_put(sdkp); + scsi_device_put(sdev); return retval; } @@ -1502,7 +1468,7 @@ static void sd_release(struct gendisk *disk, fmode_t mode) scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW); } - scsi_disk_put(sdkp); + scsi_device_put(sdev); } static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo) @@ -1616,7 +1582,7 @@ static int media_not_present(struct scsi_disk *sdkp, **/ static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing) { - struct scsi_disk *sdkp = scsi_disk_get(disk); + struct scsi_disk *sdkp = disk->private_data; struct scsi_device *sdp; int retval; bool disk_changed; @@ -1679,7 +1645,6 @@ static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing) */ disk_changed = sdp->changed; sdp->changed = 0; - scsi_disk_put(sdkp); return disk_changed ? DISK_EVENT_MEDIA_CHANGE : 0; } @@ -1887,6 +1852,13 @@ static const struct pr_ops sd_pr_ops = { .pr_clear = sd_pr_clear, }; +static void scsi_disk_free_disk(struct gendisk *disk) +{ + struct scsi_disk *sdkp = scsi_disk(disk); + + put_device(&sdkp->disk_dev); +} + static const struct block_device_operations sd_fops = { .owner = THIS_MODULE, .open = sd_open, @@ -1898,6 +1870,7 @@ static const struct block_device_operations sd_fops = { .unlock_native_capacity = sd_unlock_native_capacity, .report_zones = sd_zbc_report_zones, .get_unique_id = sd_get_unique_id, + .free_disk = scsi_disk_free_disk, .pr_ops = &sd_pr_ops, }; @@ -3623,54 +3596,23 @@ static int sd_probe(struct device *dev) **/ static int sd_remove(struct device *dev) { - struct scsi_disk *sdkp; + struct scsi_disk *sdkp = dev_get_drvdata(dev); - sdkp = dev_get_drvdata(dev); scsi_autopm_get_device(sdkp->device); device_del(&sdkp->disk_dev); del_gendisk(sdkp->disk); sd_shutdown(dev); - mutex_lock(&sd_ref_mutex); - dev_set_drvdata(dev, NULL); - put_device(&sdkp->disk_dev); - mutex_unlock(&sd_ref_mutex); - + put_disk(sdkp->disk); return 0; } -/** - * scsi_disk_release - Called to free the scsi_disk structure - * @dev: pointer to embedded class device - * - * sd_ref_mutex must be held entering this routine. Because it is - * called on last put, you should always use the scsi_disk_get() - * scsi_disk_put() helpers which manipulate the semaphore directly - * and never do a direct put_device. - **/ static void scsi_disk_release(struct device *dev) { struct scsi_disk *sdkp = to_scsi_disk(dev); - struct gendisk *disk = sdkp->disk; - struct request_queue *q = disk->queue; ida_free(&sd_index_ida, sdkp->index); - - /* - * Wait until all requests that are in progress have completed. - * This is necessary to avoid that e.g. scsi_end_request() crashes - * due to clearing the disk->private_data pointer. Wait from inside - * scsi_disk_release() instead of from sd_release() to avoid that - * freezing and unfreezing the request queue affects user space I/O - * in case multiple processes open a /dev/sd... node concurrently. - */ - blk_mq_freeze_queue(q); - blk_mq_unfreeze_queue(q); - - disk->private_data = NULL; - put_disk(disk); - sd_zbc_release_disk(sdkp); put_device(&sdkp->device->sdev_gendev); free_opal_dev(sdkp->opal_dev); From patchwork Tue Mar 8 05:51:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 550018 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B9A0C433FE for ; Tue, 8 Mar 2022 05:52:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242260AbiCHFxj (ORCPT ); Tue, 8 Mar 2022 00:53:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343837AbiCHFxe (ORCPT ); Tue, 8 Mar 2022 00:53:34 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C02FD13F80; Mon, 7 Mar 2022 21:52:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=fBDTYqF0RACXnuRDDwgW/Y1A9v3rQJ8DHW4yBU6Re9w=; b=EqLoNC1MvRe917ySR/3/86UdYR jBVVWA+5pPxL8tTQdKI6XA7rmN7PmrBNfoILsazruWQaTs/5/JgIcrDxQHjr6WjcKkklppcTZlYyx GtFbmQSRCr9a3alegWquHyW4hqw9d6PQtsdVeE3skFrQ2qTaSYBGjpYO9ncc7YD56ANlJoza0c6jc QSlGYU4bIwQF86e4luqlu2nHATqAHG7PlDhn7fKAnILYksKS+eAfHacwW5RoJBeZlFcm8tXQGD5+W jk4IoC4joteq900Oeo5gPR5LYrYyrNk0w4XzDQ5/tSDXoDYvfN1Omzq1C1Of+hzYVWjhbrsRQXV/M a+sK5/Pw==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSli-002ipM-8C; Tue, 08 Mar 2022 05:52:22 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 08/14] sr: implement ->free_disk to simplify refcounting Date: Tue, 8 Mar 2022 06:51:54 +0100 Message-Id: <20220308055200.735835-9-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Simplify the refcounting and remove the need to clear disk->private_data by implementing the ->free_disk method. Signed-off-by: Christoph Hellwig Reviewed-by: Bart Van Assche Reviewed-by: Ming Lei Reviewed-by: Martin K. Petersen --- drivers/scsi/sr.c | 124 ++++++++++------------------------------------ drivers/scsi/sr.h | 4 -- 2 files changed, 26 insertions(+), 102 deletions(-) diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c index 569bda76a5175..11fbdc75bb711 100644 --- a/drivers/scsi/sr.c +++ b/drivers/scsi/sr.c @@ -109,11 +109,6 @@ static DEFINE_SPINLOCK(sr_index_lock); static struct lock_class_key sr_bio_compl_lkclass; -/* This semaphore is used to mediate the 0->1 reference get in the - * face of object destruction (i.e. we can't allow a get on an - * object after last put) */ -static DEFINE_MUTEX(sr_ref_mutex); - static int sr_open(struct cdrom_device_info *, int); static void sr_release(struct cdrom_device_info *); @@ -143,8 +138,6 @@ static const struct cdrom_device_ops sr_dops = { .capability = SR_CAPABILITIES, }; -static void sr_kref_release(struct kref *kref); - static inline struct scsi_cd *scsi_cd(struct gendisk *disk) { return disk->private_data; @@ -163,38 +156,6 @@ static int sr_runtime_suspend(struct device *dev) return 0; } -/* - * The get and put routines for the struct scsi_cd. Note this entity - * has a scsi_device pointer and owns a reference to this. - */ -static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk) -{ - struct scsi_cd *cd = NULL; - - mutex_lock(&sr_ref_mutex); - if (disk->private_data == NULL) - goto out; - cd = scsi_cd(disk); - kref_get(&cd->kref); - if (scsi_device_get(cd->device)) { - kref_put(&cd->kref, sr_kref_release); - cd = NULL; - } - out: - mutex_unlock(&sr_ref_mutex); - return cd; -} - -static void scsi_cd_put(struct scsi_cd *cd) -{ - struct scsi_device *sdev = cd->device; - - mutex_lock(&sr_ref_mutex); - kref_put(&cd->kref, sr_kref_release); - scsi_device_put(sdev); - mutex_unlock(&sr_ref_mutex); -} - static unsigned int sr_get_events(struct scsi_device *sdev) { u8 buf[8]; @@ -522,15 +483,13 @@ static void sr_revalidate_disk(struct scsi_cd *cd) static int sr_block_open(struct block_device *bdev, fmode_t mode) { - struct scsi_cd *cd; - struct scsi_device *sdev; + struct scsi_cd *cd = cd = scsi_cd(bdev->bd_disk); + struct scsi_device *sdev = cd->device; int ret = -ENXIO; - cd = scsi_cd_get(bdev->bd_disk); - if (!cd) - goto out; + if (scsi_device_get(cd->device)) + return -ENXIO; - sdev = cd->device; scsi_autopm_get_device(sdev); if (bdev_check_media_change(bdev)) sr_revalidate_disk(cd); @@ -541,9 +500,7 @@ static int sr_block_open(struct block_device *bdev, fmode_t mode) scsi_autopm_put_device(sdev); if (ret) - scsi_cd_put(cd); - -out: + scsi_device_put(cd->device); return ret; } @@ -555,7 +512,7 @@ static void sr_block_release(struct gendisk *disk, fmode_t mode) cdrom_release(&cd->cdi, mode); mutex_unlock(&cd->lock); - scsi_cd_put(cd); + scsi_device_put(cd->device); } static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, @@ -595,18 +552,24 @@ static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, static unsigned int sr_block_check_events(struct gendisk *disk, unsigned int clearing) { - unsigned int ret = 0; - struct scsi_cd *cd; + struct scsi_cd *cd = disk->private_data; - cd = scsi_cd_get(disk); - if (!cd) + if (atomic_read(&cd->device->disk_events_disable_depth)) return 0; + return cdrom_check_events(&cd->cdi, clearing); +} - if (!atomic_read(&cd->device->disk_events_disable_depth)) - ret = cdrom_check_events(&cd->cdi, clearing); +static void sr_free_disk(struct gendisk *disk) +{ + struct scsi_cd *cd = disk->private_data; - scsi_cd_put(cd); - return ret; + spin_lock(&sr_index_lock); + clear_bit(MINOR(disk_devt(disk)), sr_index_bits); + spin_unlock(&sr_index_lock); + + unregister_cdrom(&cd->cdi); + mutex_destroy(&cd->lock); + kfree(cd); } static const struct block_device_operations sr_bdops = @@ -617,6 +580,7 @@ static const struct block_device_operations sr_bdops = .ioctl = sr_block_ioctl, .compat_ioctl = blkdev_compat_ptr_ioctl, .check_events = sr_block_check_events, + .free_disk = sr_free_disk, }; static int sr_open(struct cdrom_device_info *cdi, int purpose) @@ -660,8 +624,6 @@ static int sr_probe(struct device *dev) if (!cd) goto fail; - kref_init(&cd->kref); - disk = __alloc_disk_node(sdev->request_queue, NUMA_NO_NODE, &sr_bio_compl_lkclass); if (!disk) @@ -727,10 +689,8 @@ static int sr_probe(struct device *dev) sr_revalidate_disk(cd); error = device_add_disk(&sdev->sdev_gendev, disk, NULL); - if (error) { - kref_put(&cd->kref, sr_kref_release); - goto fail; - } + if (error) + goto unregister_cdrom; sdev_printk(KERN_DEBUG, sdev, "Attached scsi CD-ROM %s\n", cd->cdi.name); @@ -738,6 +698,8 @@ static int sr_probe(struct device *dev) return 0; +unregister_cdrom: + unregister_cdrom(&cd->cdi); fail_minor: spin_lock(&sr_index_lock); clear_bit(minor, sr_index_bits); @@ -1009,36 +971,6 @@ static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf, return ret; } - -/** - * sr_kref_release - Called to free the scsi_cd structure - * @kref: pointer to embedded kref - * - * sr_ref_mutex must be held entering this routine. Because it is - * called on last put, you should always use the scsi_cd_get() - * scsi_cd_put() helpers which manipulate the semaphore directly - * and never do a direct kref_put(). - **/ -static void sr_kref_release(struct kref *kref) -{ - struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref); - struct gendisk *disk = cd->disk; - - spin_lock(&sr_index_lock); - clear_bit(MINOR(disk_devt(disk)), sr_index_bits); - spin_unlock(&sr_index_lock); - - unregister_cdrom(&cd->cdi); - - disk->private_data = NULL; - - put_disk(disk); - - mutex_destroy(&cd->lock); - - kfree(cd); -} - static int sr_remove(struct device *dev) { struct scsi_cd *cd = dev_get_drvdata(dev); @@ -1046,11 +978,7 @@ static int sr_remove(struct device *dev) scsi_autopm_get_device(cd->device); del_gendisk(cd->disk); - dev_set_drvdata(dev, NULL); - - mutex_lock(&sr_ref_mutex); - kref_put(&cd->kref, sr_kref_release); - mutex_unlock(&sr_ref_mutex); + put_disk(cd->disk); return 0; } diff --git a/drivers/scsi/sr.h b/drivers/scsi/sr.h index d80af3fcb6f97..1175f2e213b56 100644 --- a/drivers/scsi/sr.h +++ b/drivers/scsi/sr.h @@ -18,7 +18,6 @@ #ifndef _SR_H #define _SR_H -#include #include #define MAX_RETRIES 3 @@ -51,9 +50,6 @@ typedef struct scsi_cd { struct cdrom_device_info cdi; struct mutex lock; - /* We hold gendisk and scsi_device references on probe and use - * the refs on this kref to decide when to release them */ - struct kref kref; struct gendisk *disk; } Scsi_CD; From patchwork Tue Mar 8 05:51:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 549617 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D12D6C433F5 for ; Tue, 8 Mar 2022 05:52:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344394AbiCHFxo (ORCPT ); Tue, 8 Mar 2022 00:53:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344221AbiCHFxe (ORCPT ); Tue, 8 Mar 2022 00:53:34 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64F292626; Mon, 7 Mar 2022 21:52:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=L323DoilXkDnnIBV8jLi0aI4+eaMTnzS/OHC5w/A1xU=; b=T/+LS16/OneyjTiGn6t6Rj0lyi AsZESsYflX7L1UuQN5ZqThvdpWUXUpIYkpolLzS6Lr/ZlXwGK74dkrHfBvidpFo2aMRcrrThiu1WW +zV6W2LIe1pm53GXdpONHCcXHAlABgnL4fvm9SPZnT68nLB+uHvo864Ii+J6GPcyUv7qTt7hDVGZb qyahVvFesJH9piLim2KTO9bTPuekJ5E/HRbq4q/NrLrlCo6VyqwkycFl28uTX4/0/n4NUJJSIJYEQ B0D2/aErenXwjocIE2uGkCh1jGs6dlE7iRdCRtilNzAHoaBz+y6tOFX3jDyvbgwToYo0lWmDF7pO3 10MRBX9g==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSlk-002iqj-NO; Tue, 08 Mar 2022 05:52:25 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org, Chaitanya Kulkarni Subject: [PATCH 09/14] block: move blkcg initialization/destroy into disk allocation/release handler Date: Tue, 8 Mar 2022 06:51:55 +0100 Message-Id: <20220308055200.735835-10-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org From: Ming Lei blkcg works on FS bio level, so it is reasonable to make both blkcg and gendisk sharing same lifetime. Meantime there won't be any FS IO when releasing disk, so safe to move blkcg initialization/destroy into disk allocation/release handler Long term, we can move blkcg into gendisk completely. Signed-off-by: Ming Lei Signed-off-by: Christoph Hellwig Reviewed-by: Bart Van Assche Reviewed-by: Chaitanya Kulkarni --- block/blk-core.c | 5 ----- block/blk-sysfs.c | 7 ------- block/genhd.c | 8 ++++++++ 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/block/blk-core.c b/block/blk-core.c index 4d858fc08f8ba..3fa2f08d3750b 100644 --- a/block/blk-core.c +++ b/block/blk-core.c @@ -496,17 +496,12 @@ struct request_queue *blk_alloc_queue(int node_id, bool alloc_srcu) PERCPU_REF_INIT_ATOMIC, GFP_KERNEL)) goto fail_stats; - if (blkcg_init_queue(q)) - goto fail_ref; - blk_queue_dma_alignment(q, 511); blk_set_default_limits(&q->limits); q->nr_requests = BLKDEV_DEFAULT_RQ; return q; -fail_ref: - percpu_ref_exit(&q->q_usage_counter); fail_stats: blk_free_queue_stats(q->stats); fail_split: diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c index 241ded62f458f..220085109d7f0 100644 --- a/block/blk-sysfs.c +++ b/block/blk-sysfs.c @@ -751,13 +751,6 @@ static void blk_exit_queue(struct request_queue *q) ioc_clear_queue(q); elevator_exit(q); } - - /* - * Remove all references to @q from the block cgroup controller before - * restoring @q->queue_lock to avoid that restoring this pointer causes - * e.g. blkcg_print_blkgs() to crash. - */ - blkcg_exit_queue(q); } /** diff --git a/block/genhd.c b/block/genhd.c index 54f60ded2ee6f..073e93f2fc40b 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -1120,9 +1120,12 @@ static void disk_release(struct device *dev) blk_mq_cancel_work_sync(disk->queue); + blkcg_exit_queue(disk->queue); + disk_release_events(disk); kfree(disk->random); xa_destroy(&disk->part_tbl); + disk->queue->disk = NULL; blk_put_queue(disk->queue); @@ -1328,6 +1331,9 @@ struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id, if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL)) goto out_destroy_part_tbl; + if (blkcg_init_queue(q)) + goto out_erase_part0; + rand_initialize_disk(disk); disk_to_dev(disk)->class = &block_class; disk_to_dev(disk)->type = &disk_type; @@ -1340,6 +1346,8 @@ struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id, #endif return disk; +out_erase_part0: + xa_erase(&disk->part_tbl, 0); out_destroy_part_tbl: xa_destroy(&disk->part_tbl); disk->part0->bd_disk = NULL; From patchwork Tue Mar 8 05:51:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 550017 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC073C433FE for ; Tue, 8 Mar 2022 05:52:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343696AbiCHFxp (ORCPT ); Tue, 8 Mar 2022 00:53:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344287AbiCHFxe (ORCPT ); Tue, 8 Mar 2022 00:53:34 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D7E12AEB; Mon, 7 Mar 2022 21:52:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=JANkEFHHfjZyPR6miGkscmr3ZdzOwSYp1qXO2PrN6lc=; b=o+g6BcrU+HFXCzLsOfKAb8H+wh t8val6m6x3X0g9HNCfWDrAQ4GP7bYJwYZMiNu1V3hRxVYTMB8jus8t5Yb3JcXGi+3GrCwoyC85mBl Tb0v0nN2g+l4M2+Fwv8IXevcnLZ7iOOMbHVpNTI/K0+fQa0oEfZ84HIPDEVzCGECafPI2mQX60jir 8k8Mf/0lF8ewx7firwrA9gQgEXIdo4gGpjlG37Q/nJ77oKB0oVtOL6hd3MzCtbQoHcl5Ohw5IyHMa KvvNyuL56SabmakLmV4R6Aij67HyrcGRDFh02YHhGvwLB/bu6yRGwD0PKE7YmPKrWDLBq8O3S1fLi k5GQoCag==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSln-002irl-6M; Tue, 08 Mar 2022 05:52:27 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 10/14] block: don't remove hctx debugfs dir from blk_mq_exit_queue Date: Tue, 8 Mar 2022 06:51:56 +0100 Message-Id: <20220308055200.735835-11-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org From: Ming Lei The queue's top debugfs dir is removed from blk_release_queue(), so all hctx's debugfs dirs are removed from there. Given blk_mq_exit_queue() is only called from blk_cleanup_queue(), it isn't necessary to remove hctx debugfs from blk_mq_exit_queue(). So remove it from blk_mq_exit_queue(). Signed-off-by: Ming Lei Signed-off-by: Christoph Hellwig --- block/blk-mq.c | 1 - 1 file changed, 1 deletion(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index 6fd0b0f652514..b38e125a710c9 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -3434,7 +3434,6 @@ static void blk_mq_exit_hw_queues(struct request_queue *q, queue_for_each_hw_ctx(q, hctx, i) { if (i == nr_queue) break; - blk_mq_debugfs_unregister_hctx(hctx); blk_mq_exit_hctx(q, set, hctx, i); } } From patchwork Tue Mar 8 05:51:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 549616 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 78DF3C433EF for ; Tue, 8 Mar 2022 05:53:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233834AbiCHFxx (ORCPT ); Tue, 8 Mar 2022 00:53:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343956AbiCHFxg (ORCPT ); Tue, 8 Mar 2022 00:53:36 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3E365F96; Mon, 7 Mar 2022 21:52:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=KZb1inv74NTrTRQmzFEzSuJr4DVlkAB47aKNlYQ78Qs=; b=2QhWyG44aH0uGRkcMJ5tsUeJoB QCasa2Z8e2o9LG9tpzmkENWsff327eZUt7tE1p5ltfoL0dxtqNOI1FyzhVRSRYQlGX9yG1US1uy2s zkRazfaCwyLgisBD1cdXnbrRQqr/pThnedaRQt9m4JPfL538VQpsWJkQczJeZx7b0mKWbCvSdA+Z9 AxXJFl6XzFM6EPY64lqZr29rExjg7fuLSUV5xOL6f4ZNvbK9TwPWABNXLCHQRMveAnZ0NWggOdwco tYmoNKLgATIByKZgU5Wgpwj5Ze5Cd+Ee3ZGidoVpS9WtR8vQgPgNI4VBsIazt2KoMwPFk/Ja2camM zKC/KQCA==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSlp-002isb-Jr; Tue, 08 Mar 2022 05:52:30 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 11/14] block: move q_usage_counter release into blk_queue_release Date: Tue, 8 Mar 2022 06:51:57 +0100 Message-Id: <20220308055200.735835-12-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org From: Ming Lei After blk_cleanup_queue() returns, disk may not be released yet, so probably bio may still be submitted and ->q_usage_counter may be touched, so far this way seems safe, but not good from API's viewpoint. Move the release q_usage_counter into blk_queue_release(). Signed-off-by: Ming Lei Reviewed-by: Bart Van Assche Signed-off-by: Christoph Hellwig --- block/blk-core.c | 2 -- block/blk-sysfs.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/block/blk-core.c b/block/blk-core.c index 3fa2f08d3750b..a97918d107a01 100644 --- a/block/blk-core.c +++ b/block/blk-core.c @@ -342,8 +342,6 @@ void blk_cleanup_queue(struct request_queue *q) blk_mq_sched_free_rqs(q); mutex_unlock(&q->sysfs_lock); - percpu_ref_exit(&q->q_usage_counter); - /* @q is and will stay empty, shutdown and put */ blk_put_queue(q); } diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c index 220085109d7f0..af5a6d86073f1 100644 --- a/block/blk-sysfs.c +++ b/block/blk-sysfs.c @@ -780,6 +780,8 @@ static void blk_release_queue(struct kobject *kobj) might_sleep(); + percpu_ref_exit(&q->q_usage_counter); + if (q->poll_stat) blk_stat_remove_callback(q, q->poll_cb); blk_stat_free_callback(q->poll_cb); From patchwork Tue Mar 8 05:51:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 550016 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87B82C4332F for ; Tue, 8 Mar 2022 05:53:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233650AbiCHFxz (ORCPT ); Tue, 8 Mar 2022 00:53:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244002AbiCHFxl (ORCPT ); Tue, 8 Mar 2022 00:53:41 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C425C1261C; Mon, 7 Mar 2022 21:52:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=olkS7i/OWJESMOkpxfW/l1wgYaKHCoBQyMvPpMK7Sv4=; b=tAaaB4GBr9eHjto3x8vke+Pv+R NBxyvSVrBWaIFTmbkVy9AjV1Cz3VcFBshnh11RDrL1tNhrqcw5IXG9FxrpmREIypzDakG+r3Vf6wZ DqZ9/zROvkuB9g/D7nO8ELe8gJe8sAJu3uDACEvxQdOk4JOzGP9usE/Zdg6CnjCZQmKny8bilSiMm NEuQU7tVBUZIj7vT/SjwEwdffmdsa/E6+kYxvH2pPpdnx199dXh3x2O7lex61AStllujcwj4uFSLu /o0xxR/3wFffbFwch6Vg3IuyhPfaCOQHrs2egS17oQwtTS6XTA137idwtZ6KSIWBaV/3amMykcvkl V3pIlrvw==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSls-002itL-Am; Tue, 08 Mar 2022 05:52:32 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 12/14] block: move blk_exit_queue into disk_release Date: Tue, 8 Mar 2022 06:51:58 +0100 Message-Id: <20220308055200.735835-13-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org From: Ming Lei There can't be file system I/O in disk_release(), so move the call to blk_exit_queue() there, preparing to have the teardown of file system I/O only functionality in one place, when the gendisk that is needed for it is torn down. We still need to freeze queue here since the request is freed after the bio is completed and passthrough request rely on scheduler tags as well. The disk can be released before or after queue is cleaned up, and we have to free the scheduler request pool before blk_cleanup_queue returns, while the static request pool has to be freed before exiting the I/O scheduler. Signed-off-by: Ming Lei [hch: rebased, updated the commit log] Signed-off-by: Christoph Hellwig Reviewed-by: Bart Van Assche --- block/blk-sysfs.c | 16 ---------------- block/genhd.c | 32 +++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c index af5a6d86073f1..85c4ba006671d 100644 --- a/block/blk-sysfs.c +++ b/block/blk-sysfs.c @@ -739,20 +739,6 @@ static void blk_free_queue_rcu(struct rcu_head *rcu_head) kmem_cache_free(blk_get_queue_kmem_cache(blk_queue_has_srcu(q)), q); } -/* Unconfigure the I/O scheduler and dissociate from the cgroup controller. */ -static void blk_exit_queue(struct request_queue *q) -{ - /* - * Since the I/O scheduler exit code may access cgroup information, - * perform I/O scheduler exit before disassociating from the block - * cgroup controller. - */ - if (q->elevator) { - ioc_clear_queue(q); - elevator_exit(q); - } -} - /** * blk_release_queue - releases all allocated resources of the request_queue * @kobj: pointer to a kobject, whose container is a request_queue @@ -786,8 +772,6 @@ static void blk_release_queue(struct kobject *kobj) blk_stat_remove_callback(q, q->poll_cb); blk_stat_free_callback(q->poll_cb); - blk_exit_queue(q); - blk_free_queue_stats(q->stats); kfree(q->poll_stat); diff --git a/block/genhd.c b/block/genhd.c index 073e93f2fc40b..73705a749ea92 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -29,6 +29,7 @@ #include "blk.h" #include "blk-mq-sched.h" #include "blk-rq-qos.h" +#include "blk-cgroup.h" static struct kobject *block_depr; @@ -1097,6 +1098,34 @@ static const struct attribute_group *disk_attr_groups[] = { NULL }; +static void disk_release_mq(struct request_queue *q) +{ + blk_mq_cancel_work_sync(q); + + /* + * There can't be any non non-passthrough bios in flight here, but + * requests stay around longer, including passthrough ones so we + * still need to freeze the queue here. + */ + blk_mq_freeze_queue(q); + + /* + * Since the I/O scheduler exit code may access cgroup information, + * perform I/O scheduler exit before disassociating from the block + * cgroup controller. + */ + if (q->elevator) { + ioc_clear_queue(q); + + mutex_lock(&q->sysfs_lock); + blk_mq_sched_free_rqs(q); + elevator_exit(q); + mutex_unlock(&q->sysfs_lock); + } + + __blk_mq_unfreeze_queue(q, true); +} + /** * disk_release - releases all allocated resources of the gendisk * @dev: the device representing this disk @@ -1118,7 +1147,8 @@ static void disk_release(struct device *dev) might_sleep(); WARN_ON_ONCE(disk_live(disk)); - blk_mq_cancel_work_sync(disk->queue); + if (queue_is_mq(disk->queue)) + disk_release_mq(disk->queue); blkcg_exit_queue(disk->queue); From patchwork Tue Mar 8 05:51:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 549615 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D2DBC433EF for ; Tue, 8 Mar 2022 05:53:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344355AbiCHFyA (ORCPT ); Tue, 8 Mar 2022 00:54:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344180AbiCHFxm (ORCPT ); Tue, 8 Mar 2022 00:53:42 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75FA7134; Mon, 7 Mar 2022 21:52:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=kr2Ma9+jMQ/2zrqLJVpq3QKcmngw6kqE4xgFwcP/IeM=; b=WXVDpWY6CUoqmn/t0JO0ILrNLY Ejs8nEasaFf3yiXIQpetcSMplBG/jBL0OWyXmFUIBRknb/jJmt2u8DZq6GfJ+jXkgvXmuGY7iNzQ5 or+3MRYULofffkzcUXaY4SP0Tj1mnX8YA+MAaQJVxFYzyZxXaDwtHdy1gjM7u69MRXwSygeUSXH73 Nug1kS+RQ6FX5hcJjRLZSNlAi5dgWoOK4vsz5kXz5ds/8NA0QhPqi5JEHTRQFdcHJsAMJs3DwoHoC +vi6Cb/06BlAWtjFASTxWiwGcYqVnzCu7CnyABOtvaPqlZmx6Nr4Xt6+k/fpIJycLBcvOvAiFbyvP n69ph8vg==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSlu-002iua-Rs; Tue, 08 Mar 2022 05:52:35 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 13/14] block: do more work in elevator_exit Date: Tue, 8 Mar 2022 06:51:59 +0100 Message-Id: <20220308055200.735835-14-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Move the calls to ioc_clear_queue and blk_mq_sched_free_rqs into elevator_exit. Except for one call where we know we can't have io_cq structures yet these always go together, and that extra call in an error path is harmless. Signed-off-by: Christoph Hellwig --- block/elevator.c | 6 +++--- block/genhd.c | 3 --- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/block/elevator.c b/block/elevator.c index a842e4b8ebc66..20a4e7c7c7745 100644 --- a/block/elevator.c +++ b/block/elevator.c @@ -192,6 +192,9 @@ void elevator_exit(struct request_queue *q) { struct elevator_queue *e = q->elevator; + ioc_clear_queue(q); + blk_mq_sched_free_rqs(q); + mutex_lock(&e->sysfs_lock); blk_mq_exit_sched(q, e); mutex_unlock(&e->sysfs_lock); @@ -596,8 +599,6 @@ int elevator_switch_mq(struct request_queue *q, if (q->elevator) { elv_unregister_queue(q); - ioc_clear_queue(q); - blk_mq_sched_free_rqs(q); elevator_exit(q); } @@ -608,7 +609,6 @@ int elevator_switch_mq(struct request_queue *q, if (new_e) { ret = elv_register_queue(q, true); if (ret) { - blk_mq_sched_free_rqs(q); elevator_exit(q); goto out; } diff --git a/block/genhd.c b/block/genhd.c index 73705a749ea92..857e0a54da7dd 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -1115,10 +1115,7 @@ static void disk_release_mq(struct request_queue *q) * cgroup controller. */ if (q->elevator) { - ioc_clear_queue(q); - mutex_lock(&q->sysfs_lock); - blk_mq_sched_free_rqs(q); elevator_exit(q); mutex_unlock(&q->sysfs_lock); } From patchwork Tue Mar 8 05:52:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 550015 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A9E5C433FE for ; Tue, 8 Mar 2022 05:53:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344390AbiCHFyH (ORCPT ); Tue, 8 Mar 2022 00:54:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344388AbiCHFxm (ORCPT ); Tue, 8 Mar 2022 00:53:42 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4524C2D; Mon, 7 Mar 2022 21:52:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=3yB2XsyLKYaJ70QzihkaDUnfu7i+Z20pSW9GReQZ4mo=; b=NgbX3854/TcBPXU6tCmJXwC+8I ReGuzC4h+br/bV1ckqOQ5fYuI81HZLOm8GUc7NQHM5m09B7EKhOsJmzVsKMkEknlU0VCFcUJLbTJ0 KlCYRJ4IRDD8gRNhQSu1JopKzBAjytBEy2K7ylEbLcAuSRBFw/dxJpvXV7P6ZuTfuRp/kx4pEdYQI qqj4u3IbzC8zkLOkNjxK4FYnCRYpicwK5PNV1PDfRQPUzHeKHKQqBwPCqzMNsjY/IZwoI3nupiUjl eaaFH/qhis65UEkD1C7Ug68ahqVnlmQvre9hllHc1QPOnryuJgDHKgDppn01DDZIpbHQjTMlQoiqd WJt27MLA==; Received: from [2001:4bb8:184:7746:6f50:7a98:3141:c37b] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRSlx-002ivg-DI; Tue, 08 Mar 2022 05:52:37 +0000 From: Christoph Hellwig To: Jens Axboe Cc: "Martin K. Petersen" , Ming Lei , Bart Van Assche , linux-block@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 14/14] block: move rq_qos_exit() into disk_release() Date: Tue, 8 Mar 2022 06:52:00 +0100 Message-Id: <20220308055200.735835-15-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220308055200.735835-1-hch@lst.de> References: <20220308055200.735835-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org From: Ming Lei Keep all teardown of file system I/O related functionality in one place. There can't be file system I/O in disk_release(), so it is safe to move rq_qos_exit() there. Signed-off-by: Ming Lei Signed-off-by: Christoph Hellwig --- block/genhd.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/block/genhd.c b/block/genhd.c index 857e0a54da7dd..56f66c6fee943 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -627,7 +627,6 @@ void del_gendisk(struct gendisk *disk) blk_mq_freeze_queue_wait(q); - rq_qos_exit(q); blk_sync_queue(q); blk_flush_integrity(); /* @@ -1119,7 +1118,7 @@ static void disk_release_mq(struct request_queue *q) elevator_exit(q); mutex_unlock(&q->sysfs_lock); } - + rq_qos_exit(q); __blk_mq_unfreeze_queue(q, true); }