From patchwork Tue Sep 13 19:57:13 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: 605665 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 B88A3C54EE9 for ; Tue, 13 Sep 2022 19:57:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229646AbiIMT50 (ORCPT ); Tue, 13 Sep 2022 15:57:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229555AbiIMT5Y (ORCPT ); Tue, 13 Sep 2022 15:57:24 -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 C40A211C06 for ; Tue, 13 Sep 2022 12:57:23 -0700 (PDT) Received: by mail-pl1-f175.google.com with SMTP id s18so6965470plr.4 for ; Tue, 13 Sep 2022 12:57:23 -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=8zLInkMXy4Y623bDVqTAWssOqmTeopreJ8eFabmptYY=; b=hK81f+IhnqXJ/zrfmUjA1JMCqJAFDE+i+H5WtZ6bE+Ngr7eDjCFtvdHhZcLJj0Km0s qdT3Rl6aEzG3f9V+nIncJUbJ4PgefyPHjlN3ZUOhSmkLf9XLquh9Df8vBuS8uOpT9SmJ 56RoKS/3+0Papc5GMeyAd+OcOqPllMb4OhNo4io+WwnCJ15ibS0gzaQmQbSU08RKXwdG a7xQ/5E+HdI85HSiGqP6VSzTkKcJct9eaAVwzdp86rRiJPMIfC6gYJDHJIGIUrBMI3dD GPQqP9UShRA4aO+YLOKYLbzLbMYOBQ0VWhhNkfhabZxe/79uzKacm+HCybamYupjFUF/ XvGw== X-Gm-Message-State: ACgBeo0pBs5+bzV56eF27QrK2rucRKyuD6yTzLO5z6KBpFfE7ma2kQ9C KE7PaRGTPDmfeKpsR008am8= X-Google-Smtp-Source: AA6agR615u9xiiqWoMdUKWF5GIgALs0y60oTjSGgSxz57laj/T+pfB+TV0xzQcUZ4ZEmcL0d6rygwg== X-Received: by 2002:a17:902:d890:b0:16c:abb4:94d0 with SMTP id b16-20020a170902d89000b0016cabb494d0mr33538693plz.50.1663099043249; Tue, 13 Sep 2022 12:57:23 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:515b:d33a:21be:45a]) by smtp.gmail.com with ESMTPSA id p15-20020a1709027ecf00b0017825ab5320sm6739987plb.251.2022.09.13.12.57.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Sep 2022 12:57:22 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , Bradley Grove , Christoph Hellwig , Ming Lei , Hannes Reinecke , John Garry , Mike Christie , Krzysztof Kozlowski , "James E.J. Bottomley" Subject: [PATCH v4 1/4] scsi: esas2r: Initialize two host template members implicitly Date: Tue, 13 Sep 2022 12:57:13 -0700 Message-Id: <20220913195716.3966875-2-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220913195716.3966875-1-bvanassche@acm.org> References: <20220913195716.3966875-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'. Cc: Bradley Grove 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/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 Tue Sep 13 19:57:14 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: 606082 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 6603AC6FA82 for ; Tue, 13 Sep 2022 19:57:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229666AbiIMT52 (ORCPT ); Tue, 13 Sep 2022 15:57:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229572AbiIMT50 (ORCPT ); Tue, 13 Sep 2022 15:57:26 -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 E26C772EF6 for ; Tue, 13 Sep 2022 12:57:25 -0700 (PDT) Received: by mail-pj1-f46.google.com with SMTP id q9-20020a17090a178900b0020265d92ae3so16654246pja.5 for ; Tue, 13 Sep 2022 12:57:25 -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=r4b7G+FR0CDZJL2WSp766pAJi2QZl8cA4HixhP8Yg4Y=; b=JmSdbcpQ170i4Lbnis+Xdd8Q3Ktpnp+vmrOFPKdrVn9yZSBBdfH4elkQ8Ozq2hcWoj jS03v+GlKBGLqgVHkR8zYWlzRCT76rtW5BME3H0Ar11h7XjWjt1Z0xaCwicLwJn98vqh EuPiVuWYgo0Dw07qVFSd3l8A7bZ1hPQiuQ5TAXnhGh0hy2foQg+YicUnlD83DSb4Enar sjA2SLJtin5oR1Zu2SsR+/8e22YRNgDaH8q0uhfhNLQzhSGqtMXp/j54Vn99GA1pPZNq xzVpOkoB0D8yLgyIE4paUBBgG81a/Qe58J5ZDNR7IldJMroenEIu2I1eA0V1GrO3zym3 ILYQ== X-Gm-Message-State: ACgBeo0w5U3dvQ8MHaed/0lxl3/WQqn6jAu5nn01TLAkPcAdrcGKfnfJ rNVq2z6JKEx8QH/SiWqNLhk= X-Google-Smtp-Source: AA6agR5xtK+D0C0J7OIVpe0H2wBGFVhtS00FPqN5mIp3jD45nv4Oopzjcc/XJqfsQrWhTS36Lbke1A== X-Received: by 2002:a17:902:c1cd:b0:177:e483:51b0 with SMTP id c13-20020a170902c1cd00b00177e48351b0mr31373581plc.41.1663099045258; Tue, 13 Sep 2022 12:57:25 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:515b:d33a:21be:45a]) by smtp.gmail.com with ESMTPSA id p15-20020a1709027ecf00b0017825ab5320sm6739987plb.251.2022.09.13.12.57.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Sep 2022 12:57:24 -0700 (PDT) From: Bart Van Assche To: "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, Bart Van Assche , Bradley Grove , Christoph Hellwig , Ming Lei , Hannes Reinecke , John Garry , Mike Christie , Krzysztof Kozlowski , "James E.J. Bottomley" Subject: [PATCH v4 2/4] scsi: esas2r: Introduce scsi_template_proc_dir() Date: Tue, 13 Sep 2022 12:57:14 -0700 Message-Id: <20220913195716.3966875-3-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220913195716.3966875-1-bvanassche@acm.org> References: <20220913195716.3966875-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. Cc: Bradley Grove Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: John Garry 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 Tue Sep 13 19:57: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: 606081 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 04639ECAAD8 for ; Tue, 13 Sep 2022 19:57:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229681AbiIMT5f (ORCPT ); Tue, 13 Sep 2022 15:57:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229669AbiIMT53 (ORCPT ); Tue, 13 Sep 2022 15:57:29 -0400 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E4E072EFA for ; Tue, 13 Sep 2022 12:57:28 -0700 (PDT) Received: by mail-pj1-f45.google.com with SMTP id go6so8184421pjb.2 for ; Tue, 13 Sep 2022 12:57:28 -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=Tc0Yo88uQfzIugZTyXtB+wWedqy1ibPn7Tcz49L6SPA=; b=hkWZEjEXxq4KyBgzrfccKRHJEG3K+KtE5ipkybnDgI0YqyeTxmlOP2m1w/pEmySod/ ScogAlqog1/THJKT4ThsIVVWcbC0FDK982VYiqHGmEwJrn+HXkx0zZK0hxnrhDs+nILW jxuOKJA+umF0IbvFUTXtxrepmN605Xt9uiYA3GY3yw0WTr2Wbt8VpBns2hDbIvKNa0z6 5IC7fNDjvDN6+jq+0YGwrr2WKmqRR3vhZdeRm2T3Zw+0ch36QZAoONT/NM9xFka0sYMT AWCFlfEOvfDkm2Ty7q6K1wRGpV99zv1j8H0tLuWqxNMkkWv/7N+0mpT17MvXSUxcFb30 qEvg== X-Gm-Message-State: ACrzQf0tkI4sUCAUzs+ph4jXHf4IUgkmMgfbYgUugLEPpVwrHtC1fLMU WAZnvDhzeA3hsbC50hhGcFWfIb5PR+k= X-Google-Smtp-Source: AMsMyM4jqtcQ3yehqWvebG5eWEKGTrh8SBz2rDyV7pJBHe85vufcwxCWt+PAmSS1eyAzfMtEJZrvyQ== X-Received: by 2002:a17:90a:9309:b0:1fa:d28b:3751 with SMTP id p9-20020a17090a930900b001fad28b3751mr846339pjo.189.1663099047644; Tue, 13 Sep 2022 12:57:27 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:515b:d33a:21be:45a]) by smtp.gmail.com with ESMTPSA id p15-20020a1709027ecf00b0017825ab5320sm6739987plb.251.2022.09.13.12.57.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Sep 2022 12:57:26 -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 v4 3/4] scsi: core: Introduce a new list for SCSI proc directory entries Date: Tue, 13 Sep 2022 12:57:15 -0700 Message-Id: <20220913195716.3966875-4-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220913195716.3966875-1-bvanassche@acm.org> References: <20220913195716.3966875-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 --- drivers/scsi/scsi_priv.h | 4 +- drivers/scsi/scsi_proc.c | 111 +++++++++++++++++++++++++++++++++------ include/scsi/scsi_host.h | 12 ----- 3 files changed, 96 insertions(+), 31 deletions(-) diff --git a/drivers/scsi/scsi_priv.h b/drivers/scsi/scsi_priv.h index f385b3f04d6e..be5d7c9b7f39 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 void scsi_proc_hostdir_add(struct scsi_host_template *); -extern void scsi_proc_hostdir_rm(struct scsi_host_template *); +extern void 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 456b43097288..7dffa73d6f92 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); @@ -109,34 +152,57 @@ 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) +void scsi_proc_hostdir_add(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 = 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); + e = __scsi_lookup_proc_entry(sht); + if (!e) { + e = kzalloc(sizeof(*e), GFP_KERNEL); + if (!e) + goto unlock; } + if (e->present++) { + e = NULL; + goto unlock; + } + 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); + e->present--; + goto unlock; + } + e->sht = sht; + list_add_tail(&e->entry, &scsi_proc_list); + e = NULL; +unlock: mutex_unlock(&global_host_template_mutex); + + kfree(e); } /** * 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); } @@ -148,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, @@ -170,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 Tue Sep 13 19:57: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: 605664 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 7AA6DECAAD8 for ; Tue, 13 Sep 2022 19:57:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229683AbiIMT5e (ORCPT ); Tue, 13 Sep 2022 15:57:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229572AbiIMT5c (ORCPT ); Tue, 13 Sep 2022 15:57:32 -0400 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61B9172EF1 for ; Tue, 13 Sep 2022 12:57:31 -0700 (PDT) Received: by mail-pf1-f173.google.com with SMTP id u132so12753003pfc.6 for ; Tue, 13 Sep 2022 12:57:31 -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=ylKZD8iGuK1Qfl5i65YCidupdDov7GWxNJ2GDKD/xGY=; b=eYhY0Ds648Jz5DJ/dQI+e5OvEznloAKQoRrPjRwDgcMFQc4jmem8BO9t1OZgaCSyXS 3OmHudn2/m8DAhuN5d+32dqV8/3v1RgcsqSUGaDrxrQNLjPjsMk1nP/TQ+ksIfW9ifo+ LsQzKtASYdcaFW41oNDZfGaLjNTRN47FlcpIPPNEjhHdErM8e9H8qh5A/HFacUgXYB7M p8WjKo6Ty0kn/JbfVaASONuGDIBtMG1VE7EyXPkJCcAFCfQ6mkhTeY8504LPr1TR5d04 u9EgZM+kXadCS7pSqw79M0aQEri4nAKUtrUTaMNjQ5jAen8HqDYLmT1HxWZ/aq8mpvZi ZIZg== X-Gm-Message-State: ACgBeo2YmuM16hx0ndksmxdrESGeXTPhVO8r/VDvDm1mAlLeVT87ryWF 76/AIGpbQYba4npKICtJS6M= X-Google-Smtp-Source: AA6agR6OpHZdcBm8UJz1BAIjwDb+PCKC1HvYCIBuNsZNTMusT3zSsIlOfSNUKEr9OHwxZnaPoh66kA== X-Received: by 2002:a63:2c0b:0:b0:434:ebb6:7594 with SMTP id s11-20020a632c0b000000b00434ebb67594mr27184514pgs.245.1663099049792; Tue, 13 Sep 2022 12:57:29 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:515b:d33a:21be:45a]) by smtp.gmail.com with ESMTPSA id p15-20020a1709027ecf00b0017825ab5320sm6739987plb.251.2022.09.13.12.57.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Sep 2022 12:57:28 -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 v4 4/4] scsi: core: Rework the code for dropping the LLD module reference Date: Tue, 13 Sep 2022 12:57:16 -0700 Message-Id: <20220913195716.3966875-5-bvanassche@acm.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220913195716.3966875-1-bvanassche@acm.org> References: <20220913195716.3966875-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, set the 'drop_module_ref' SCSI device member. This patch prepares for constifying the SCSI host template. Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: John Garry Cc: Mike Christie Cc: Krzysztof Kozlowski Signed-off-by: Bart Van Assche --- drivers/scsi/scsi_sysfs.c | 7 +++---- include/scsi/scsi_device.h | 1 + 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c index 5d61f58399dc..822ae60a64b9 100644 --- a/drivers/scsi/scsi_sysfs.c +++ b/drivers/scsi/scsi_sysfs.c @@ -454,7 +454,7 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work) sdev = container_of(work, struct scsi_device, ew.work); - mod = sdev->host->hostt->module; + mod = sdev->drop_module_ref ? sdev->host->hostt->module : NULL; scsi_dh_release_device(sdev); @@ -525,9 +525,8 @@ 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; + if (try_module_get(sdp->host->hostt->module)) + sdp->drop_module_ref = true; execute_in_process_context(scsi_device_dev_release_usercontext, &sdp->ew); diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h index 2493bd65351a..b03176b69056 100644 --- a/include/scsi/scsi_device.h +++ b/include/scsi/scsi_device.h @@ -214,6 +214,7 @@ struct scsi_device { * creation time */ unsigned ignore_media_change:1; /* Ignore MEDIA CHANGE on resume */ unsigned silence_suspend:1; /* Do not print runtime PM related messages */ + unsigned drop_module_ref:1; unsigned int queue_stopped; /* request queue is quiesced */ bool offline_already; /* Device offline message logged */