From patchwork Wed Sep 14 22:56:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 606075 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 8812CECAAD3 for ; Wed, 14 Sep 2022 22:56:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229768AbiINW4v (ORCPT ); Wed, 14 Sep 2022 18:56:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229708AbiINW4t (ORCPT ); Wed, 14 Sep 2022 18:56:49 -0400 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB0B282F95 for ; Wed, 14 Sep 2022 15:56:48 -0700 (PDT) Received: by mail-pj1-f52.google.com with SMTP id n23-20020a17090a091700b00202a51cc78bso14279878pjn.2 for ; Wed, 14 Sep 2022 15:56:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=SpmXLmWlYQVnn1BcyLp3cCBXB/Od6Q2CqnBaVZAdo34=; b=5l/cRVXFVk4wvWcG7SoyUpjk07FsIzX54k01dXKcT8DZs1FiEYecFuVqiYR4BjrEED /pWrI5ueHPycytJ6KrV29vDG355YhkqIVvjz9TBfijZ9tVQt1o0CrqDgk/oAIjmmtTdl vl32AFWbFohYi3A1n0dT49mnZ6RGLiohg9fVtbZWuRBUldkXyGwPk1ow34nc13lXF0qh CgR8n2PW8y5ik69/vJ5dJqi1w2+1ms5nZMUCTx7EqggRxkd5q4CG+Xllp6XpB/i0sh0w 9RJOFwb2Z7V/TtwTWA5+Q6aqHi/8KgwgvwC8olgXTnB6b/2CSCxzTxR5eF8g65D9rVdM jCvQ== X-Gm-Message-State: ACrzQf2eWk3UMPZoXx4psFc5zwMIcuwfeF1sSv9lw5xT/bIexCVryxXB rrwtBy5x/+6dHgaibF4h4jwXBLha86g= X-Google-Smtp-Source: AMsMyM6O+oUGQqIm4FYtCdXbSnE6bkw7VZ2zPLFCK/+ZtL3DmAmqrdpfbHGC29eg1JxgipmQ1JLI+w== X-Received: by 2002:a17:902:bc84:b0:174:505b:2d67 with SMTP id bb4-20020a170902bc8400b00174505b2d67mr1268540plb.33.1663196208362; Wed, 14 Sep 2022 15:56:48 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:9147:e0c1:9227:cf53]) by smtp.gmail.com with ESMTPSA id w9-20020a170902d70900b0016d1b70872asm2606926ply.134.2022.09.14.15.56.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Sep 2022 15:56:47 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , John Garry , Bradley Grove , Christoph Hellwig , Ming Lei , Hannes Reinecke , Mike Christie , Krzysztof Kozlowski , "James E.J. Bottomley" Subject: [PATCH v5 1/7] scsi: esas2r: Initialize two host template members implicitly Date: Wed, 14 Sep 2022 15:56:15 -0700 Message-Id: <20220914225621.415631-2-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220914225621.415631-1-bvanassche@acm.org> References: <20220914225621.415631-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Prepare for removing the 'proc_dir' and 'present' members from the SCSI host template by implicitly initializing 'present' and 'emulated' in 'driver_template'. Reviewed-by: John Garry Cc: Bradley Grove Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: Mike Christie Cc: Krzysztof Kozlowski Signed-off-by: Bart Van Assche --- drivers/scsi/esas2r/esas2r_main.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/drivers/scsi/esas2r/esas2r_main.c b/drivers/scsi/esas2r/esas2r_main.c index 7a4eadad23d7..27f6e7ccded8 100644 --- a/drivers/scsi/esas2r/esas2r_main.c +++ b/drivers/scsi/esas2r/esas2r_main.c @@ -248,8 +248,6 @@ static struct scsi_host_template driver_template = { .sg_tablesize = SG_CHUNK_SIZE, .cmd_per_lun = ESAS2R_DEFAULT_CMD_PER_LUN, - .present = 0, - .emulated = 0, .proc_name = ESAS2R_DRVR_NAME, .change_queue_depth = scsi_change_queue_depth, .max_sectors = 0xFFFF, From patchwork Wed Sep 14 22:56:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 606387 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 76201C6FA82 for ; Wed, 14 Sep 2022 22:56:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229787AbiINW46 (ORCPT ); Wed, 14 Sep 2022 18:56:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229636AbiINW4v (ORCPT ); Wed, 14 Sep 2022 18:56:51 -0400 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E99FE7FE6A for ; Wed, 14 Sep 2022 15:56:50 -0700 (PDT) Received: by mail-pj1-f46.google.com with SMTP id i15-20020a17090a4b8f00b0020073b4ac27so15796342pjh.3 for ; Wed, 14 Sep 2022 15:56:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=uiMee14k/5xV+wn1UzfOXtnjcQOvrNZaKrl7u1MuRkE=; b=PPiQsEO6DqGI1EQwqNs6+Y2i/0/3Cyp924dZQ9FD2HICDNOD84yGovN0c42Q27rOXW gw1EBIRfJOU94jv83IzpwhWKNoWr6obJIpDD28LbFW+KhSas8Vgkst630CNBQ25kHbXc GRIFrr2oYIGJIONfNAY1bsGgsioaWhC7oykKo1P8EyKnGRSd8viD2kDr6Wo7jE893GZQ nauQcBhJW1c6qFT8wFNMp9KcTTim3+evMADv9+kJVEAtdc3YV22PXH/BduwNag5Q+s6U crSIZZRyq0AROGrR5QJ6N9vyOaCQf4FqlB/GHJZMzua+I21lnKPhLP4iM3rlggDrb2vI IMcQ== X-Gm-Message-State: ACrzQf2xygUV6pf0KZmEc6QpZ8geabsnZPvC8ThIqACOOlBpPdAt8kQ3 3TTJaExRg7Cqnv0fD0M7/uM= X-Google-Smtp-Source: AMsMyM4eEAxufVNN4/jCpzZRaJp6DOtyNQ9NEWF5vhdGquxvicNZ2pWe2MGkbxPI+HsAGbwr9inCcw== X-Received: by 2002:a17:902:ccc2:b0:178:29e1:899e with SMTP id z2-20020a170902ccc200b0017829e1899emr1290416ple.114.1663196210347; Wed, 14 Sep 2022 15:56:50 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:9147:e0c1:9227:cf53]) by smtp.gmail.com with ESMTPSA id w9-20020a170902d70900b0016d1b70872asm2606926ply.134.2022.09.14.15.56.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Sep 2022 15:56:49 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , John Garry , Bradley Grove , Christoph Hellwig , Ming Lei , Hannes Reinecke , Mike Christie , Krzysztof Kozlowski , "James E.J. Bottomley" Subject: [PATCH v5 2/7] scsi: esas2r: Introduce scsi_template_proc_dir() Date: Wed, 14 Sep 2022 15:56:16 -0700 Message-Id: <20220914225621.415631-3-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220914225621.415631-1-bvanassche@acm.org> References: <20220914225621.415631-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Prepare for removing the 'proc_dir' and 'present' members from the SCSI host template. This patch does not change any functionality. Reviewed-by: John Garry Cc: Bradley Grove Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: Mike Christie Cc: Krzysztof Kozlowski Signed-off-by: Bart Van Assche --- drivers/scsi/esas2r/esas2r_main.c | 17 +++++++++++------ drivers/scsi/scsi_proc.c | 11 +++++++++++ include/scsi/scsi_host.h | 6 ++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/drivers/scsi/esas2r/esas2r_main.c b/drivers/scsi/esas2r/esas2r_main.c index 27f6e7ccded8..d7a2c49ff5ee 100644 --- a/drivers/scsi/esas2r/esas2r_main.c +++ b/drivers/scsi/esas2r/esas2r_main.c @@ -635,10 +635,13 @@ static void __exit esas2r_exit(void) esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__); if (esas2r_proc_major > 0) { + struct proc_dir_entry *proc_dir; + esas2r_log(ESAS2R_LOG_INFO, "unregister proc"); - remove_proc_entry(ATTONODE_NAME, - esas2r_proc_host->hostt->proc_dir); + proc_dir = scsi_template_proc_dir(esas2r_proc_host->hostt); + if (proc_dir) + remove_proc_entry(ATTONODE_NAME, proc_dir); unregister_chrdev(esas2r_proc_major, ESAS2R_DRVR_NAME); esas2r_proc_major = 0; @@ -728,11 +731,13 @@ const char *esas2r_info(struct Scsi_Host *sh) esas2r_proc_major); if (esas2r_proc_major > 0) { - struct proc_dir_entry *pde; + struct proc_dir_entry *proc_dir; + struct proc_dir_entry *pde = NULL; - pde = proc_create(ATTONODE_NAME, 0, - sh->hostt->proc_dir, - &esas2r_proc_ops); + proc_dir = scsi_template_proc_dir(sh->hostt); + if (proc_dir) + pde = proc_create(ATTONODE_NAME, 0, proc_dir, + &esas2r_proc_ops); if (!pde) { esas2r_log_dev(ESAS2R_LOG_WARN, diff --git a/drivers/scsi/scsi_proc.c b/drivers/scsi/scsi_proc.c index 95aee1ad1383..456b43097288 100644 --- a/drivers/scsi/scsi_proc.c +++ b/drivers/scsi/scsi_proc.c @@ -83,6 +83,17 @@ static int proc_scsi_host_open(struct inode *inode, struct file *file) 4 * PAGE_SIZE); } +/** + * scsi_template_proc_dir() - returns the procfs dir for a SCSI host template + * @sht: SCSI host template pointer. + */ +struct proc_dir_entry * +scsi_template_proc_dir(const struct scsi_host_template *sht) +{ + return sht->proc_dir; +} +EXPORT_SYMBOL_GPL(scsi_template_proc_dir); + static const struct proc_ops proc_scsi_ops = { .proc_open = proc_scsi_host_open, .proc_release = single_release, diff --git a/include/scsi/scsi_host.h b/include/scsi/scsi_host.h index 9b0a028bf053..030faca947d2 100644 --- a/include/scsi/scsi_host.h +++ b/include/scsi/scsi_host.h @@ -751,6 +751,12 @@ extern struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *, int); extern int __must_check scsi_add_host_with_dma(struct Scsi_Host *, struct device *, struct device *); +#if defined(CONFIG_SCSI_PROC_FS) +struct proc_dir_entry * +scsi_template_proc_dir(const struct scsi_host_template *sht); +#else +#define scsi_template_proc_dir(sht) NULL +#endif extern void scsi_scan_host(struct Scsi_Host *); extern void scsi_rescan_device(struct device *); extern void scsi_remove_host(struct Scsi_Host *); From patchwork Wed Sep 14 22:56:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 606074 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 27E97ECAAD3 for ; Wed, 14 Sep 2022 22:57:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229708AbiINW47 (ORCPT ); Wed, 14 Sep 2022 18:56:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229541AbiINW4x (ORCPT ); Wed, 14 Sep 2022 18:56:53 -0400 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3B3683059 for ; Wed, 14 Sep 2022 15:56:52 -0700 (PDT) Received: by mail-pf1-f179.google.com with SMTP id 9so5195935pfz.12 for ; Wed, 14 Sep 2022 15:56:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=LyWeacTwFK6UBYW2Wo6wy8WwfqX6JKz5f/CDUDhhqE4=; b=mpYFy5muO+5AuZ2X1sF6UxBVkn5bVGWN/qsCQOOyJ0AnlWErWHD2iYhSs9ckBk2wHI Lsl31/pr2daNaNQFhx4pQCIA/w4TmsQ06sxMRh7KBXPWaCJ6DMhrOGZESRYojxRX54Gy RJLwCKOSyntWiv//5x+OoD3My9hwR1TiHg0jwRvN2pbkSV37pT5tAuPrYhaHwOOclIGx 7XbNPqrdgGTptzCjmvELfY4m56MsaTE6ocLYrNc4OKjsOPrnFvEAadPwHRxhJHBByXc6 oiIfo4FOxlNj3i/9kKue5L554f0/u0XDTunbX2t2TLkkiQqh25cJn/cjK/DqkwjpVCLu 8uxA== X-Gm-Message-State: ACgBeo124j446148DHTRsrw+kIHmJCbyb8zWBtars6cekpjKvKjU67BI OBIaRIR6c41UpxYCL+EAtJc= X-Google-Smtp-Source: AA6agR7yb0LrbxJW82PixX7+gXexyitSoC4vsRIJjnJpG/DvdFseCpUdSo/jflRPM77dYo+DaUNiKA== X-Received: by 2002:a05:6a00:a90:b0:530:2f3c:da43 with SMTP id b16-20020a056a000a9000b005302f3cda43mr39626071pfl.50.1663196212348; Wed, 14 Sep 2022 15:56:52 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:9147:e0c1:9227:cf53]) by smtp.gmail.com with ESMTPSA id w9-20020a170902d70900b0016d1b70872asm2606926ply.134.2022.09.14.15.56.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Sep 2022 15:56:51 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , Christoph Hellwig , Ming Lei , Hannes Reinecke , John Garry , Mike Christie , Krzysztof Kozlowski , "James E.J. Bottomley" Subject: [PATCH v5 3/7] scsi: core: Fail host creation if creating the proc directory fails Date: Wed, 14 Sep 2022 15:56:17 -0700 Message-Id: <20220914225621.415631-4-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220914225621.415631-1-bvanassche@acm.org> References: <20220914225621.415631-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Users expect that the contents of /proc/scsi is in sync with the contents of /sys/class/scsi_host. Hence fail host creation if creating the proc directory fails. Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: John Garry Cc: Mike Christie Cc: Krzysztof Kozlowski Signed-off-by: Bart Van Assche Reviewed-by: John Garry --- drivers/scsi/hosts.c | 3 ++- drivers/scsi/scsi_priv.h | 4 ++-- drivers/scsi/scsi_proc.c | 13 +++++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c index 9857dba09c95..12346e2297fd 100644 --- a/drivers/scsi/hosts.c +++ b/drivers/scsi/hosts.c @@ -519,7 +519,8 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) "failed to create tmf workq\n"); goto fail; } - scsi_proc_hostdir_add(shost->hostt); + if (scsi_proc_hostdir_add(shost->hostt) < 0) + goto fail; return shost; fail: /* diff --git a/drivers/scsi/scsi_priv.h b/drivers/scsi/scsi_priv.h index f385b3f04d6e..8c2e32121db1 100644 --- a/drivers/scsi/scsi_priv.h +++ b/drivers/scsi/scsi_priv.h @@ -100,14 +100,14 @@ extern void scsi_evt_thread(struct work_struct *work); /* scsi_proc.c */ #ifdef CONFIG_SCSI_PROC_FS -extern void scsi_proc_hostdir_add(struct scsi_host_template *); +extern int scsi_proc_hostdir_add(struct scsi_host_template *); extern void scsi_proc_hostdir_rm(struct scsi_host_template *); extern void scsi_proc_host_add(struct Scsi_Host *); extern void scsi_proc_host_rm(struct Scsi_Host *); extern int scsi_init_procfs(void); extern void scsi_exit_procfs(void); #else -# define scsi_proc_hostdir_add(sht) do { } while (0) +# define scsi_proc_hostdir_add(sht) 0 # define scsi_proc_hostdir_rm(sht) do { } while (0) # define scsi_proc_host_add(shost) do { } while (0) # define scsi_proc_host_rm(shost) do { } while (0) diff --git a/drivers/scsi/scsi_proc.c b/drivers/scsi/scsi_proc.c index 456b43097288..8c84f1a74773 100644 --- a/drivers/scsi/scsi_proc.c +++ b/drivers/scsi/scsi_proc.c @@ -108,20 +108,25 @@ static const struct proc_ops proc_scsi_ops = { * * Sets sht->proc_dir to the new directory. */ - -void scsi_proc_hostdir_add(struct scsi_host_template *sht) +int scsi_proc_hostdir_add(struct scsi_host_template *sht) { + int ret = 0; + if (!sht->show_info) - return; + return 0; mutex_lock(&global_host_template_mutex); if (!sht->present++) { sht->proc_dir = proc_mkdir(sht->proc_name, proc_scsi); - if (!sht->proc_dir) + if (!sht->proc_dir) { printk(KERN_ERR "%s: proc_mkdir failed for %s\n", __func__, sht->proc_name); + ret = -ENOMEM; + } } mutex_unlock(&global_host_template_mutex); + + return ret; } /** From patchwork Wed Sep 14 22:56:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 606386 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 73D44C6FA86 for ; Wed, 14 Sep 2022 22:57:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229812AbiINW5A (ORCPT ); Wed, 14 Sep 2022 18:57:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229696AbiINW4z (ORCPT ); Wed, 14 Sep 2022 18:56:55 -0400 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9FA5832E5 for ; Wed, 14 Sep 2022 15:56:54 -0700 (PDT) Received: by mail-pg1-f175.google.com with SMTP id 78so15619825pgb.13 for ; Wed, 14 Sep 2022 15:56:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=aHSxg66mAblQJI0tstrBDcWmXhdguGtA+2LBSohuXX4=; b=g8xXtnKJUbapzWcEosyRy5qX1+YZQzqC0ksSGjsd2NTqx1MjwTeUteErQ4+n+wpaYh p1DDZWJDBJmZB2WK0K+PigvdXlPLiU0rfWlYf5376mEJWCPczYHvisPAHfd0P9yv8Gwt KZEk7rH9vodHaWj7Dz3h4svQ1fD+8NEZIvyx9NVV8+4CbSgZwfqfjW4mmI5n9zuzVSsC TOqDIYWP0N4tQqmj9LFYC+L/z6xyVL7QS8LGcRJ5CPgGwMJJPyV4aWp7Il7OMFpP97qu Au2i5402i29Ekl0sHV5D0JXIyqjr8udEefX8PZn+sQidoBIbOXTpIbMlKc2ij1tv7N48 Hzxg== X-Gm-Message-State: ACgBeo2h9WCRpwMXI6uK7kGYAtGUeVCHC6Yz+zLhOdJn+czlddi0v6MB K6nobhHnXHeKoKmbgyW2dYY= X-Google-Smtp-Source: AA6agR48W2pllHrq28gBx5cdUvNe3fN1NIme/HVYSkU9dYYaH4f8ffxDmjG2muYJOgSvuXB445BRjw== X-Received: by 2002:a05:6a00:1a8d:b0:547:c564:1614 with SMTP id e13-20020a056a001a8d00b00547c5641614mr5220783pfv.71.1663196214206; Wed, 14 Sep 2022 15:56:54 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:9147:e0c1:9227:cf53]) by smtp.gmail.com with ESMTPSA id w9-20020a170902d70900b0016d1b70872asm2606926ply.134.2022.09.14.15.56.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Sep 2022 15:56:53 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , Christoph Hellwig , Ming Lei , Hannes Reinecke , John Garry , Mike Christie , Krzysztof Kozlowski , "James E.J. Bottomley" Subject: [PATCH v5 4/7] scsi: core: Introduce a new list for SCSI proc directory entries Date: Wed, 14 Sep 2022 15:56:18 -0700 Message-Id: <20220914225621.415631-5-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220914225621.415631-1-bvanassche@acm.org> References: <20220914225621.415631-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Instead of using scsi_host_template members to track the SCSI proc directory entries, track these entries in a list. This patch changes the time needed for looking up the proc dir pointer from O(1) into O(n). I think this is acceptable since the number of SCSI host adapter types per host is usually small (less than ten). This patch has been tested by attaching two USB storage devices to a qemu host: $ grep -aH . /proc/scsi/usb-storage/* /proc/scsi/usb-storage/7: Host scsi7: usb-storage /proc/scsi/usb-storage/7: Vendor: QEMU /proc/scsi/usb-storage/7: Product: QEMU USB HARDDRIVE /proc/scsi/usb-storage/7:Serial Number: 1-0000:00:02.1:00.0-6 /proc/scsi/usb-storage/7: Protocol: Transparent SCSI /proc/scsi/usb-storage/7: Transport: Bulk /proc/scsi/usb-storage/7: Quirks: SANE_SENSE /proc/scsi/usb-storage/8: Host scsi8: usb-storage /proc/scsi/usb-storage/8: Vendor: QEMU /proc/scsi/usb-storage/8: Product: QEMU USB HARDDRIVE /proc/scsi/usb-storage/8:Serial Number: 1-0000:00:02.1:00.0-7 /proc/scsi/usb-storage/8: Protocol: Transparent SCSI /proc/scsi/usb-storage/8: Transport: Bulk /proc/scsi/usb-storage/8: Quirks: SANE_SENSE This patch prepares for constifying most SCSI host templates. Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: John Garry Cc: Mike Christie Cc: Krzysztof Kozlowski Signed-off-by: Bart Van Assche Reviewed-by: John Garry --- drivers/scsi/scsi_priv.h | 4 +- drivers/scsi/scsi_proc.c | 112 ++++++++++++++++++++++++++++++++------- include/scsi/scsi_host.h | 12 ----- 3 files changed, 94 insertions(+), 34 deletions(-) diff --git a/drivers/scsi/scsi_priv.h b/drivers/scsi/scsi_priv.h index 8c2e32121db1..456ff53fe404 100644 --- a/drivers/scsi/scsi_priv.h +++ b/drivers/scsi/scsi_priv.h @@ -100,8 +100,8 @@ extern void scsi_evt_thread(struct work_struct *work); /* scsi_proc.c */ #ifdef CONFIG_SCSI_PROC_FS -extern int scsi_proc_hostdir_add(struct scsi_host_template *); -extern void scsi_proc_hostdir_rm(struct scsi_host_template *); +extern int scsi_proc_hostdir_add(const struct scsi_host_template *); +extern void scsi_proc_hostdir_rm(const struct scsi_host_template *); extern void scsi_proc_host_add(struct Scsi_Host *); extern void scsi_proc_host_rm(struct Scsi_Host *); extern int scsi_init_procfs(void); diff --git a/drivers/scsi/scsi_proc.c b/drivers/scsi/scsi_proc.c index 8c84f1a74773..76229ca89e2f 100644 --- a/drivers/scsi/scsi_proc.c +++ b/drivers/scsi/scsi_proc.c @@ -43,8 +43,23 @@ static struct proc_dir_entry *proc_scsi; -/* Protect sht->present and sht->proc_dir */ +/* Protects scsi_proc_list */ static DEFINE_MUTEX(global_host_template_mutex); +static LIST_HEAD(scsi_proc_list); + +/** + * struct scsi_proc_entry - (host template, SCSI proc dir) association + * @entry: entry in scsi_proc_list. + * @sht: SCSI host template associated with the procfs directory. + * @proc_dir: procfs directory associated with the SCSI host template. + * @present: Number of SCSI hosts instantiated for @sht. + */ +struct scsi_proc_entry { + struct list_head entry; + const struct scsi_host_template *sht; + struct proc_dir_entry *proc_dir; + unsigned int present; +}; static ssize_t proc_scsi_host_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) @@ -83,6 +98,32 @@ static int proc_scsi_host_open(struct inode *inode, struct file *file) 4 * PAGE_SIZE); } +static struct scsi_proc_entry * +__scsi_lookup_proc_entry(const struct scsi_host_template *sht) +{ + struct scsi_proc_entry *e; + + lockdep_assert_held(&global_host_template_mutex); + + list_for_each_entry(e, &scsi_proc_list, entry) + if (e->sht == sht) + return e; + + return NULL; +} + +static struct scsi_proc_entry * +scsi_lookup_proc_entry(const struct scsi_host_template *sht) +{ + struct scsi_proc_entry *e; + + mutex_lock(&global_host_template_mutex); + e = __scsi_lookup_proc_entry(sht); + mutex_unlock(&global_host_template_mutex); + + return e; +} + /** * scsi_template_proc_dir() - returns the procfs dir for a SCSI host template * @sht: SCSI host template pointer. @@ -90,7 +131,9 @@ static int proc_scsi_host_open(struct inode *inode, struct file *file) struct proc_dir_entry * scsi_template_proc_dir(const struct scsi_host_template *sht) { - return sht->proc_dir; + struct scsi_proc_entry *e = scsi_lookup_proc_entry(sht); + + return e ? e->proc_dir : NULL; } EXPORT_SYMBOL_GPL(scsi_template_proc_dir); @@ -108,24 +151,38 @@ static const struct proc_ops proc_scsi_ops = { * * Sets sht->proc_dir to the new directory. */ -int scsi_proc_hostdir_add(struct scsi_host_template *sht) +int scsi_proc_hostdir_add(const struct scsi_host_template *sht) { - int ret = 0; + struct scsi_proc_entry *e; + int ret = -ENOMEM; if (!sht->show_info) return 0; mutex_lock(&global_host_template_mutex); - if (!sht->present++) { - sht->proc_dir = proc_mkdir(sht->proc_name, proc_scsi); - if (!sht->proc_dir) { - printk(KERN_ERR "%s: proc_mkdir failed for %s\n", - __func__, sht->proc_name); - ret = -ENOMEM; - } + e = __scsi_lookup_proc_entry(sht); + if (!e) { + e = kzalloc(sizeof(*e), GFP_KERNEL); + if (!e) + goto unlock; } + if (e->present++) + goto success; + e->proc_dir = proc_mkdir(sht->proc_name, proc_scsi); + if (!e->proc_dir) { + printk(KERN_ERR "%s: proc_mkdir failed for %s\n", __func__, + sht->proc_name); + goto unlock; + } + e->sht = sht; + list_add_tail(&e->entry, &scsi_proc_list); +success: + e = NULL; + ret = 0; +unlock: mutex_unlock(&global_host_template_mutex); + kfree(e); return ret; } @@ -133,15 +190,19 @@ int scsi_proc_hostdir_add(struct scsi_host_template *sht) * scsi_proc_hostdir_rm - remove directory in /proc for a scsi host * @sht: owner of directory */ -void scsi_proc_hostdir_rm(struct scsi_host_template *sht) +void scsi_proc_hostdir_rm(const struct scsi_host_template *sht) { + struct scsi_proc_entry *e; + if (!sht->show_info) return; mutex_lock(&global_host_template_mutex); - if (!--sht->present && sht->proc_dir) { + e = __scsi_lookup_proc_entry(sht); + if (e && !--e->present) { remove_proc_entry(sht->proc_name, proc_scsi); - sht->proc_dir = NULL; + list_del(&e->entry); + kfree(e); } mutex_unlock(&global_host_template_mutex); } @@ -153,16 +214,21 @@ void scsi_proc_hostdir_rm(struct scsi_host_template *sht) */ void scsi_proc_host_add(struct Scsi_Host *shost) { - struct scsi_host_template *sht = shost->hostt; + const struct scsi_host_template *sht = shost->hostt; + struct scsi_proc_entry *e; struct proc_dir_entry *p; char name[10]; - if (!sht->proc_dir) + if (!sht->show_info) + return; + + e = scsi_lookup_proc_entry(sht); + if (!e) return; sprintf(name,"%d", shost->host_no); - p = proc_create_data(name, S_IRUGO | S_IWUSR, - sht->proc_dir, &proc_scsi_ops, shost); + p = proc_create_data(name, S_IRUGO | S_IWUSR, e->proc_dir, + &proc_scsi_ops, shost); if (!p) printk(KERN_ERR "%s: Failed to register host %d in" "%s\n", __func__, shost->host_no, @@ -175,13 +241,19 @@ void scsi_proc_host_add(struct Scsi_Host *shost) */ void scsi_proc_host_rm(struct Scsi_Host *shost) { + const struct scsi_host_template *sht = shost->hostt; + struct scsi_proc_entry *e; char name[10]; - if (!shost->hostt->proc_dir) + if (!sht->show_info) + return; + + e = scsi_lookup_proc_entry(sht); + if (!e) return; sprintf(name,"%d", shost->host_no); - remove_proc_entry(name, shost->hostt->proc_dir); + remove_proc_entry(name, e->proc_dir); } /** * proc_print_scsidevice - return data about this host diff --git a/include/scsi/scsi_host.h b/include/scsi/scsi_host.h index 030faca947d2..fb8184d87384 100644 --- a/include/scsi/scsi_host.h +++ b/include/scsi/scsi_host.h @@ -357,12 +357,6 @@ struct scsi_host_template { */ const char *proc_name; - /* - * Used to store the procfs directory if a driver implements the - * show_info method. - */ - struct proc_dir_entry *proc_dir; - /* * This determines if we will use a non-interrupt driven * or an interrupt driven scheme. It is set to the maximum number @@ -423,12 +417,6 @@ struct scsi_host_template { */ short cmd_per_lun; - /* - * present contains counter indicating how many boards of this - * type were found when we did the scan. - */ - unsigned char present; - /* If use block layer to manage tags, this is tag allocation policy */ int tag_alloc_policy; From patchwork Wed Sep 14 22:56:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 606073 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 DD297ECAAD3 for ; Wed, 14 Sep 2022 22:57:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229696AbiINW5B (ORCPT ); Wed, 14 Sep 2022 18:57:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229773AbiINW45 (ORCPT ); Wed, 14 Sep 2022 18:56:57 -0400 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A82AB7FF9D for ; Wed, 14 Sep 2022 15:56:56 -0700 (PDT) Received: by mail-pj1-f49.google.com with SMTP id q15-20020a17090a304f00b002002ac83485so15824521pjl.0 for ; Wed, 14 Sep 2022 15:56:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=J2XQsX+0ms+bazFRVKIAEDMgyTBXxMOAVpbtlR1zqtY=; b=1vfOALCrW15xX+iUJPvGCekvwPKF+muwNTGc7eKX1NIIDbrwUn1anNoxbgtXJfFu2u bLss5e3BEmazUWqCBOhVaLvMZa5aHtRFGJz5lzgP/8rCwGRaWAfNP3W2P0alevoNQtl7 ebIahDhHGrF6Uxb39j8RIHrxi43Mz6Wj0aN52Rx5xoI67kDpE60YZA1PtOPb4t3Ks3n9 SxtJwZvIVTyz1qG1OzSlghkZ8do73re0bYFseK7GzDFrSQhR+IgDBw+nuyF9QiKLXUQS kxwz1qwtVRE+I4OWSrGzFtVFZ4VHBPWZez0diUVuOqMSomGEfPkmwic23lCa8VoipbVI BcLA== X-Gm-Message-State: ACrzQf37ueFOdofmbmVgIgEBY4aRuU3DXal8vGLKonsSJdxP68WRBifu /kuUMMDlvnqkke9oGyQ3jBg= X-Google-Smtp-Source: AMsMyM6j2wPL/eYW0Zte5z2hG12fNb4iMJ1zU63Va2E57M2aFOgghIX1DUEah8hfzscTBi9rheD64Q== X-Received: by 2002:a17:902:7082:b0:177:f7fc:5290 with SMTP id z2-20020a170902708200b00177f7fc5290mr1238807plk.143.1663196216113; Wed, 14 Sep 2022 15:56:56 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:9147:e0c1:9227:cf53]) by smtp.gmail.com with ESMTPSA id w9-20020a170902d70900b0016d1b70872asm2606926ply.134.2022.09.14.15.56.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Sep 2022 15:56:55 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , Jun'ichi Nomura , Christoph Hellwig , Ming Lei , Hannes Reinecke , John Garry , Mike Christie , Krzysztof Kozlowski , "James E.J. Bottomley" , James Bottomley Subject: [PATCH v5 5/7] scsi: core: Fix a use-after-free related to releasing device handlers Date: Wed, 14 Sep 2022 15:56:19 -0700 Message-Id: <20220914225621.415631-6-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220914225621.415631-1-bvanassche@acm.org> References: <20220914225621.415631-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org The SCSI device name can be freed by kobject_cleanup() before scsi_device_dev_release_usercontext() is called since the latter function may be called asynchronously. Hence, the SCSI device name must not be dereferenced from inside the SCSI device release function. Since scsi_dh_release_device() dereferences the SCSI device name, call it earlier. This patch fixes the following use-after-free: BUG: KASAN: use-after-free in string+0xdc/0x1d0 Read of size 1 at addr ffff8881280d05f0 by task kworker/54:2/1373 CPU: 54 PID: 1373 Comm: kworker/54:2 Tainted: G E 6.0.0-rc5-dbg #12 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.0-debian-1.16.0-4 04/01/2014 Workqueue: events scsi_device_dev_release_usercontext Call Trace: show_stack+0x4e/0x53 dump_stack_lvl+0x51/0x66 print_address_description.constprop.0.cold+0xd5/0x412 print_report.cold+0x90/0x219 kasan_report+0xb1/0xe0 __asan_load1+0x4d/0x50 string+0xdc/0x1d0 vsnprintf+0x44d/0x7f0 snprintf+0x88/0xa0 dev_vprintk_emit+0x19c/0x1dc dev_printk_emit+0x8c/0xa6 __dev_printk+0x73/0x8f _dev_printk+0xa8/0xbe sdev_prefix_printk+0x12c/0x180 scsi_dh_release_device+0x74/0xa0 scsi_device_dev_release_usercontext+0x60/0x8a0 process_one_work+0x571/0xa40 worker_thread+0x90/0x650 kthread+0x185/0x1c0 ret_from_fork+0x1f/0x30 Freed by task 509: kasan_save_stack+0x26/0x50 kasan_set_track+0x25/0x30 kasan_set_free_info+0x24/0x40 ____kasan_slab_free+0x155/0x1c0 __kasan_slab_free+0x12/0x20 kfree+0x1fe/0x3e0 kfree_const+0x21/0x30 kobject_cleanup+0x8d/0x1c0 kobject_put+0x6e/0x90 put_device+0x13/0x20 __scsi_remove_device+0x140/0x200 scsi_forget_host+0xa7/0xb0 scsi_remove_host+0x9b/0x1b0 srp_remove_work+0x12b/0x2e0 [ib_srp] process_one_work+0x571/0xa40 worker_thread+0x90/0x650 kthread+0x185/0x1c0 ret_from_fork+0x1f/0x30 Cc: Jun'ichi Nomura Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: John Garry Cc: Mike Christie Cc: Krzysztof Kozlowski Fixes: 23695e41a1ca ("scsi_dh: fix use-after-free when removing scsi device") Signed-off-by: Bart Van Assche --- drivers/scsi/scsi_sysfs.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c index 5d61f58399dc..a3aaafdeac1d 100644 --- a/drivers/scsi/scsi_sysfs.c +++ b/drivers/scsi/scsi_sysfs.c @@ -456,8 +456,6 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work) mod = sdev->host->hostt->module; - scsi_dh_release_device(sdev); - parent = sdev->sdev_gendev.parent; spin_lock_irqsave(sdev->host->host_lock, flags); @@ -1479,6 +1477,9 @@ void __scsi_remove_device(struct scsi_device *sdev) kref_put(&sdev->host->tagset_refcnt, scsi_mq_free_tags); cancel_work_sync(&sdev->requeue_work); + /* Only detach the device handler after I/O processing has finished. */ + scsi_dh_release_device(sdev); + if (sdev->host->hostt->slave_destroy) sdev->host->hostt->slave_destroy(sdev); transport_destroy_device(dev); From patchwork Wed Sep 14 22:56:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 606072 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 53A3AC6FA86 for ; Wed, 14 Sep 2022 22:57:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229622AbiINW5K (ORCPT ); Wed, 14 Sep 2022 18:57:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229772AbiINW5A (ORCPT ); Wed, 14 Sep 2022 18:57:00 -0400 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E969083059; Wed, 14 Sep 2022 15:56:58 -0700 (PDT) Received: by mail-pl1-f175.google.com with SMTP id l10so16568348plb.10; Wed, 14 Sep 2022 15:56:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=hIeIUTsBV4QxjBZVDpe2dp3qI8fehLgRx48IHpHPzW8=; b=wdIrNcyTufCnLE2MrarhWcgNI47ziV7uNfT9DSr4fNIANC8H59e3bIu4qvfIjcaOmS 9w27RE8tuYJEGH6jqrrbqSN7++hKQJQ7+muFaEPzS3pJ0Ap1kqihckqKZ/OlB9IgaH7J wNudswNnMFHg+EjA0AkgCbGsf231t+qvtoxX5lVEECfK9wkqPYplQpga6lr87jL1/wus cSSOINTUHvtavMGymA440x9VbPvgxUuKVymo+AfRiT6/6+JPsXnEvxPYcOQxxrV6Bghn YZYzLq3Wp3+FRTcyr9XpORH79uc+mxG9UVWE3sqRh0PkTYWRPaBTu3ej+zDidPw7lg5x Uo8Q== X-Gm-Message-State: ACrzQf0ammYT0b7vP2LZwSdp16c61gRQQqUJ9UILsgELsigOJLjqwh6o Zknmx938O9syRXCWVxtjCao= X-Google-Smtp-Source: AMsMyM7xbb0K/O3zh7pzBk0SS0ANyvZ/W2+59zP+9cC7ZtIfUPFv4j7NqiRjg9PlRZ0Cr9vyvQoM7g== X-Received: by 2002:a17:90b:b06:b0:200:aff0:2e68 with SMTP id bf6-20020a17090b0b0600b00200aff02e68mr7411578pjb.159.1663196218342; Wed, 14 Sep 2022 15:56:58 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:9147:e0c1:9227:cf53]) by smtp.gmail.com with ESMTPSA id w9-20020a170902d70900b0016d1b70872asm2606926ply.134.2022.09.14.15.56.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Sep 2022 15:56:57 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , Luis Chamberlain , Christoph Hellwig , Ming Lei , Hannes Reinecke , John Garry , Mike Christie , Krzysztof Kozlowski , Greg Kroah-Hartman , linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, "James E.J. Bottomley" , Tejun Heo Subject: [PATCH v5 6/7] module: Improve support for asynchronous module exit code Date: Wed, 14 Sep 2022 15:56:20 -0700 Message-Id: <20220914225621.415631-7-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220914225621.415631-1-bvanassche@acm.org> References: <20220914225621.415631-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Some kernel modules call device_del() from their module exit code and schedule asynchronous work from inside the .release callback without waiting until that callback has finished. As an example, many SCSI LLD drivers call scsi_remove_host() from their module exit code. scsi_remove_host() may invoke scsi_device_dev_release_usercontext() asynchronously. scsi_device_dev_release_usercontext() uses the host template pointer and that pointer usually exists in static storage in the SCSI LLD. Support using the module reference count to keep the module around until asynchronous module exiting has completed by waiting in the delete_module() system call until the module reference count drops to zero. The following debug patch has been used to make the new wait_event() call wait: diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c index 8be8e08fb67d..fead694ff95a 100644 --- a/drivers/scsi/scsi_sysfs.c +++ b/drivers/scsi/scsi_sysfs.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -518,6 +519,7 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work) if (parent) put_device(parent); + msleep(100); module_put(mod); } diff --git a/kernel/module/main.c b/kernel/module/main.c index a271126d7d59..0bf75ec3f5a8 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -756,8 +756,10 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user, * unloading is not forced, wait for the module reference count to drop * to zero again. */ - if (!forced) + if (!forced) { + WARN_ON_ONCE(atomic_read(&mod->refcnt)); wait_event(mod->refcnt_wq, atomic_read(&mod->refcnt) == 0); + } blocking_notifier_call_chain(&module_notify_list, MODULE_STATE_GOING, mod); klp_module_going(mod); diff --git a/kernel/workqueue.c b/kernel/workqueue.c index aeea9731ef80..f021625f2caa 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -3355,7 +3355,7 @@ int schedule_on_each_cpu(work_func_t func) */ int execute_in_process_context(work_func_t fn, struct execute_work *ew) { - if (!in_interrupt()) { + if (false && !in_interrupt()) { fn(&ew->work); return 0; } Cc: Luis Chamberlain Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: John Garry Cc: Mike Christie Cc: Krzysztof Kozlowski Cc: Greg Kroah-Hartman Cc: linux-modules@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Bart Van Assche --- include/linux/module.h | 1 + kernel/module/main.c | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/include/linux/module.h b/include/linux/module.h index 518296ea7f73..3a77d2bd4198 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -533,6 +533,7 @@ struct module { /* Destruction function. */ void (*exit)(void); + wait_queue_head_t refcnt_wq; atomic_t refcnt; #endif diff --git a/kernel/module/main.c b/kernel/module/main.c index a4e4d84b6f4e..a271126d7d59 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -550,6 +550,7 @@ static int module_unload_init(struct module *mod) /* Hold reference count during initialization. */ atomic_inc(&mod->refcnt); + init_waitqueue_head(&mod->refcnt_wq); return 0; } @@ -750,6 +751,13 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user, /* Final destruction now no one is using it. */ if (mod->exit != NULL) mod->exit(); + /* + * If the module reference count was increased by mod->exit() and if + * unloading is not forced, wait for the module reference count to drop + * to zero again. + */ + if (!forced) + wait_event(mod->refcnt_wq, atomic_read(&mod->refcnt) == 0); blocking_notifier_call_chain(&module_notify_list, MODULE_STATE_GOING, mod); klp_module_going(mod); @@ -854,6 +862,8 @@ void module_put(struct module *module) WARN_ON(ret < 0); /* Failed to put refcount */ trace_module_put(module, _RET_IP_); preempt_enable(); + if (ret == 0) + wake_up(&module->refcnt_wq); } } EXPORT_SYMBOL(module_put); From patchwork Wed Sep 14 22:56:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 606385 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 845EDC6FA82 for ; Wed, 14 Sep 2022 22:57:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229833AbiINW5I (ORCPT ); Wed, 14 Sep 2022 18:57:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229616AbiINW5B (ORCPT ); Wed, 14 Sep 2022 18:57:01 -0400 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3A69832E6 for ; Wed, 14 Sep 2022 15:57:00 -0700 (PDT) Received: by mail-pl1-f178.google.com with SMTP id b21so16578326plz.7 for ; Wed, 14 Sep 2022 15:57:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=vUM2HolRGWQC4/RRb/dHgvk9K9vdj30UGyVRRJl+YPo=; b=wjHQIj02cDKMDyZKLub1WR6VQ+b3M3cofkUODcaukw03nc/7Ar2CVn+feLnvsf3g25 W+lhSmhlkZSsxB0NykSeNlli3E3xrkVsD9RF88QoIIF61u9FkIACMfIueQwRwU4W4t0T teF1JUWeWHUI93Wrq+onKIGKqlMV8XharWHLttpZDQ8GdPwRb6ZCWa7O4leK71UZskc6 qOsQ6n74vGJxk2cDvhWx2zckZdX1nHRhBG9HTnirUMT3R1Uw9Ctl6zWv90GsBVazeyUw J5Rb+LR3XR5gTLpjoBVL+Tzi/pXaQwVKbNy1d92BBcp5XlS8ykO+1qMp9ECOZ1nTyG8c Nh/w== X-Gm-Message-State: ACrzQf2DRI3mEyJLwE4bgKDJBkuLnFbBVHcyM9g50T2Ptpzd1l+HCDOh tiIows3N+OX2KDrwdx2OGdP5Cdk6aGY= X-Google-Smtp-Source: AMsMyM7x6fZkDQi7WnRLkODg9HiXyItdWEXdojYBIU4rYArM6WNJcI/+7hwJYwcupS6RDJowTR343A== X-Received: by 2002:a17:90b:4a4f:b0:202:5bbb:b76f with SMTP id lb15-20020a17090b4a4f00b002025bbbb76fmr7412193pjb.230.1663196220236; Wed, 14 Sep 2022 15:57:00 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:9147:e0c1:9227:cf53]) by smtp.gmail.com with ESMTPSA id w9-20020a170902d70900b0016d1b70872asm2606926ply.134.2022.09.14.15.56.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Sep 2022 15:56:59 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , Christoph Hellwig , Ming Lei , Hannes Reinecke , John Garry , Mike Christie , Krzysztof Kozlowski , Luis Chamberlain , "James E.J. Bottomley" Subject: [PATCH v5 7/7] scsi: core: Improve SCSI device removal Date: Wed, 14 Sep 2022 15:56:21 -0700 Message-Id: <20220914225621.415631-8-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220914225621.415631-1-bvanassche@acm.org> References: <20220914225621.415631-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Instead of clearing the host template module pointer if the LLD kernel module is being unloaded, call __module_get() unconditionally. This patch is a bug fix because it prevents that a SCSI LLD is unloaded after scsi_device_dev_release() returns and before scsi_device_dev_release_usercontext() is called. Suggested-by: Christoph Hellwig Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: John Garry Cc: Mike Christie Cc: Krzysztof Kozlowski Cc: Luis Chamberlain Signed-off-by: Bart Van Assche --- drivers/scsi/scsi_sysfs.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c index a3aaafdeac1d..661c2bdd4588 100644 --- a/drivers/scsi/scsi_sysfs.c +++ b/drivers/scsi/scsi_sysfs.c @@ -523,9 +523,7 @@ static void scsi_device_dev_release(struct device *dev) { struct scsi_device *sdp = to_scsi_device(dev); - /* Set module pointer as NULL in case of module unloading */ - if (!try_module_get(sdp->host->hostt->module)) - sdp->host->hostt->module = NULL; + __module_get(sdp->host->hostt->module); execute_in_process_context(scsi_device_dev_release_usercontext, &sdp->ew);