All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v6 0/8] Prepare for constifying SCSI host templates
@ 2022-09-29 22:44 Bart Van Assche
  2022-09-29 22:44 ` [PATCH v6 1/8] scsi: esas2r: Initialize two host template members implicitly Bart Van Assche
                   ` (7 more replies)
  0 siblings, 8 replies; 11+ messages in thread
From: Bart Van Assche @ 2022-09-29 22:44 UTC (permalink / raw)
  To: Martin K . Petersen; +Cc: linux-scsi, Bart Van Assche

Hi Martin,

This patch series prepares for constifying SCSI host templates by moving the
members that are not constant out of the SCSI host template. This patch series
is based on Linus' master branch instead of the SCSI for-next branch. Please
consider this patch series for the next merge window.

Thanks,

Bart.

Changes compared to v5:
- Addressed John Garry's feedback on patch 4/7.
- Dropped the kernel/module patch and replaced it with a series of patches
  that rework the scsi_device_put() calls that happen from atomic context and
  one patch that makes scsi_device_put() synchronous.

Changes compared to v4:
- Added three additional patches: fail host creation if creating the proc
  directory fails, a use-after-free fix and an improvement for the kernel
  module unload code.

Changes compared to v3:
- Changed the 'present' counter from 8 to 32 bits.
- Fixed a bug in an error path (reported by John Garry).
- Changed EXPORT_SYMBOL() into EXPORT_SYMBOL_GPL().
- Split patch 1/3 into two patches.

Changes compared to v2:
- Optimized the show_info == NULL case.
- Added a patch that removes the code that clears the module pointer in the host
  template.

Changes compared to v1:
- Fix the CONFIG_SCSI_PROC_FS=n build.
Bart Van Assche (8):
  scsi: esas2r: Initialize two host template members implicitly
  scsi: esas2r: Introduce scsi_template_proc_dir()
  scsi: core: Fail host creation if creating the proc directory fails
  scsi: core: Introduce a new list for SCSI proc directory entries
  scsi: core: Rework scsi_single_lun_run()
  scsi: ufs: Simplify ufshcd_set_dev_pwr_mode()
  scsi: core: Remove the put_device() call from scsi_device_get()
  scsi: core: Release SCSI devices synchronously

 drivers/scsi/esas2r/esas2r_main.c |  19 +++--
 drivers/scsi/hosts.c              |   3 +-
 drivers/scsi/scsi.c               |  12 +--
 drivers/scsi/scsi_lib.c           |  32 ++++---
 drivers/scsi/scsi_priv.h          |   6 +-
 drivers/scsi/scsi_proc.c          | 137 +++++++++++++++++++++++++-----
 drivers/scsi/scsi_sysfs.c         |  12 +--
 drivers/ufs/core/ufshcd.c         |   9 +-
 include/scsi/scsi_device.h        |   1 -
 include/scsi/scsi_host.h          |  18 ++--
 10 files changed, 164 insertions(+), 85 deletions(-)


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v6 1/8] scsi: esas2r: Initialize two host template members implicitly
  2022-09-29 22:44 [PATCH v6 0/8] Prepare for constifying SCSI host templates Bart Van Assche
@ 2022-09-29 22:44 ` Bart Van Assche
  2022-09-29 22:44 ` [PATCH v6 2/8] scsi: esas2r: Introduce scsi_template_proc_dir() Bart Van Assche
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Bart Van Assche @ 2022-09-29 22:44 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, John Garry, Bradley Grove,
	Christoph Hellwig, Ming Lei, Hannes Reinecke, Mike Christie,
	Krzysztof Kozlowski, James E.J. Bottomley

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 <john.garry@huawei.com>
Cc: Bradley Grove <linuxdrivers@attotech.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Hannes Reinecke <hare@suse.de>
Cc: Mike Christie <michael.christie@oracle.com>
Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 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,

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v6 2/8] scsi: esas2r: Introduce scsi_template_proc_dir()
  2022-09-29 22:44 [PATCH v6 0/8] Prepare for constifying SCSI host templates Bart Van Assche
  2022-09-29 22:44 ` [PATCH v6 1/8] scsi: esas2r: Initialize two host template members implicitly Bart Van Assche
@ 2022-09-29 22:44 ` Bart Van Assche
  2022-09-29 22:44 ` [PATCH v6 3/8] scsi: core: Fail host creation if creating the proc directory fails Bart Van Assche
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Bart Van Assche @ 2022-09-29 22:44 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, John Garry, Bradley Grove,
	Christoph Hellwig, Ming Lei, Hannes Reinecke, Mike Christie,
	Krzysztof Kozlowski, James E.J. Bottomley

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 <john.garry@huawei.com>
Cc: Bradley Grove <linuxdrivers@attotech.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Hannes Reinecke <hare@suse.de>
Cc: Mike Christie <michael.christie@oracle.com>
Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 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 aa7b7496c93a..c2b8427e0305 100644
--- a/include/scsi/scsi_host.h
+++ b/include/scsi/scsi_host.h
@@ -752,6 +752,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 *);

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v6 3/8] scsi: core: Fail host creation if creating the proc directory fails
  2022-09-29 22:44 [PATCH v6 0/8] Prepare for constifying SCSI host templates Bart Van Assche
  2022-09-29 22:44 ` [PATCH v6 1/8] scsi: esas2r: Initialize two host template members implicitly Bart Van Assche
  2022-09-29 22:44 ` [PATCH v6 2/8] scsi: esas2r: Introduce scsi_template_proc_dir() Bart Van Assche
@ 2022-09-29 22:44 ` Bart Van Assche
  2022-09-29 22:44 ` [PATCH v6 4/8] scsi: core: Introduce a new list for SCSI proc directory entries Bart Van Assche
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Bart Van Assche @ 2022-09-29 22:44 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, John Garry, Christoph Hellwig,
	Ming Lei, Hannes Reinecke, Mike Christie, Krzysztof Kozlowski,
	James E.J. Bottomley

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.

Suggested-by: John Garry <john.garry@huawei.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Hannes Reinecke <hare@suse.de>
Cc: John Garry <john.garry@huawei.com>
Cc: Mike Christie <michael.christie@oracle.com>
Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 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 0738238ed6cc..098d1970c451 100644
--- a/drivers/scsi/hosts.c
+++ b/drivers/scsi/hosts.c
@@ -517,7 +517,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 2b9e0559ddcb..a7da51bc22da 100644
--- a/drivers/scsi/scsi_priv.h
+++ b/drivers/scsi/scsi_priv.h
@@ -111,14 +111,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;
 }
 
 /**

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v6 4/8] scsi: core: Introduce a new list for SCSI proc directory entries
  2022-09-29 22:44 [PATCH v6 0/8] Prepare for constifying SCSI host templates Bart Van Assche
                   ` (2 preceding siblings ...)
  2022-09-29 22:44 ` [PATCH v6 3/8] scsi: core: Fail host creation if creating the proc directory fails Bart Van Assche
@ 2022-09-29 22:44 ` Bart Van Assche
  2022-09-29 22:44 ` [PATCH v6 5/8] scsi: core: Rework scsi_single_lun_run() Bart Van Assche
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Bart Van Assche @ 2022-09-29 22:44 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, John Garry, Christoph Hellwig,
	Ming Lei, Hannes Reinecke, Mike Christie, Krzysztof Kozlowski,
	James E.J. Bottomley

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.

Reviewed-by: John Garry <john.garry@huawei.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Hannes Reinecke <hare@suse.de>
Cc: Mike Christie <michael.christie@oracle.com>
Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/scsi/scsi_priv.h |   4 +-
 drivers/scsi/scsi_proc.c | 121 ++++++++++++++++++++++++++++++++-------
 include/scsi/scsi_host.h |  12 ----
 3 files changed, 102 insertions(+), 35 deletions(-)

diff --git a/drivers/scsi/scsi_priv.h b/drivers/scsi/scsi_priv.h
index a7da51bc22da..41bb763724f7 100644
--- a/drivers/scsi/scsi_priv.h
+++ b/drivers/scsi/scsi_priv.h
@@ -111,8 +111,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..4a6eb1741be0 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,41 @@ 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;
 
 	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);
+	e = __scsi_lookup_proc_entry(sht);
+	if (!e) {
+		e = kzalloc(sizeof(*e), GFP_KERNEL);
+		if (!e) {
 			ret = -ENOMEM;
+			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);
+		ret = -ENOMEM;
+		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 +193,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,20 +217,29 @@ 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)
+		goto err;
+
 	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,
-		       sht->proc_name);
+		goto err;
+	return;
+
+err:
+	shost_printk(KERN_ERR, shost,
+		     "%s: Failed to register host (%s failed)\n", __func__,
+		     e ? "proc_create_data()" : "scsi_proc_hostdir_add()");
 }
 
 /**
@@ -175,13 +248,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 c2b8427e0305..f2294d44013c 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;
 

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v6 5/8] scsi: core: Rework scsi_single_lun_run()
  2022-09-29 22:44 [PATCH v6 0/8] Prepare for constifying SCSI host templates Bart Van Assche
                   ` (3 preceding siblings ...)
  2022-09-29 22:44 ` [PATCH v6 4/8] scsi: core: Introduce a new list for SCSI proc directory entries Bart Van Assche
@ 2022-09-29 22:44 ` Bart Van Assche
  2022-09-29 22:44 ` [PATCH v6 6/8] scsi: ufs: Simplify ufshcd_set_dev_pwr_mode() Bart Van Assche
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Bart Van Assche @ 2022-09-29 22:44 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, Christoph Hellwig, Ming Lei,
	Hannes Reinecke, John Garry, Mike Christie, Krzysztof Kozlowski,
	James E.J. Bottomley

Use __starget_for_each_device() instead of open-coding
starget_for_each_device(). This patch removes code that calls
scsi_device_put() from atomic context.

Cc: Christoph Hellwig <hch@lst.de>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Hannes Reinecke <hare@suse.de>
Cc: John Garry <john.garry@huawei.com>
Cc: Mike Christie <michael.christie@oracle.com>
Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/scsi/scsi_lib.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)

diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 473d9403f0c1..da1e6f9f9221 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -302,6 +302,18 @@ static void scsi_kick_queue(struct request_queue *q)
 	blk_mq_run_hw_queues(q, false);
 }
 
+/*
+ * Kick the queue of SCSI device @sdev if @sdev != current_sdev. Called with
+ * interrupts disabled.
+ */
+static void scsi_kick_sdev_queue(struct scsi_device *sdev, void *data)
+{
+	struct scsi_device *current_sdev = data;
+
+	if (sdev != current_sdev)
+		blk_mq_run_hw_queues(sdev->request_queue, true);
+}
+
 /*
  * Called for single_lun devices on IO completion. Clear starget_sdev_user,
  * and call blk_run_queue for all the scsi_devices on the target -
@@ -312,7 +324,6 @@ static void scsi_kick_queue(struct request_queue *q)
 static void scsi_single_lun_run(struct scsi_device *current_sdev)
 {
 	struct Scsi_Host *shost = current_sdev->host;
-	struct scsi_device *sdev, *tmp;
 	struct scsi_target *starget = scsi_target(current_sdev);
 	unsigned long flags;
 
@@ -329,22 +340,9 @@ static void scsi_single_lun_run(struct scsi_device *current_sdev)
 	scsi_kick_queue(current_sdev->request_queue);
 
 	spin_lock_irqsave(shost->host_lock, flags);
-	if (starget->starget_sdev_user)
-		goto out;
-	list_for_each_entry_safe(sdev, tmp, &starget->devices,
-			same_target_siblings) {
-		if (sdev == current_sdev)
-			continue;
-		if (scsi_device_get(sdev))
-			continue;
-
-		spin_unlock_irqrestore(shost->host_lock, flags);
-		scsi_kick_queue(sdev->request_queue);
-		spin_lock_irqsave(shost->host_lock, flags);
-
-		scsi_device_put(sdev);
-	}
- out:
+	if (!starget->starget_sdev_user)
+		__starget_for_each_device(starget, current_sdev,
+					  scsi_kick_sdev_queue);
 	spin_unlock_irqrestore(shost->host_lock, flags);
 }
 

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v6 6/8] scsi: ufs: Simplify ufshcd_set_dev_pwr_mode()
  2022-09-29 22:44 [PATCH v6 0/8] Prepare for constifying SCSI host templates Bart Van Assche
                   ` (4 preceding siblings ...)
  2022-09-29 22:44 ` [PATCH v6 5/8] scsi: core: Rework scsi_single_lun_run() Bart Van Assche
@ 2022-09-29 22:44 ` Bart Van Assche
  2022-10-03  6:28   ` Adrian Hunter
  2022-09-29 22:44 ` [PATCH v6 7/8] scsi: core: Remove the put_device() call from scsi_device_get() Bart Van Assche
  2022-09-29 22:44 ` [PATCH v6 8/8] scsi: core: Release SCSI devices synchronously Bart Van Assche
  7 siblings, 1 reply; 11+ messages in thread
From: Bart Van Assche @ 2022-09-29 22:44 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, Adrian Hunter, Avri Altman,
	James E.J. Bottomley, Bean Huo, Jinyoung Choi

Simplify the code for incrementing the SCSI device reference count in
ufshcd_set_dev_pwr_mode(). This patch removes one scsi_device_put() call
that happens from atomic context.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Avri Altman <avri.altman@wdc.com>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/ufs/core/ufshcd.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 7c15cbc737b4..6e61372fe027 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -8753,15 +8753,10 @@ static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
 
 	spin_lock_irqsave(hba->host->host_lock, flags);
 	sdp = hba->ufs_device_wlun;
-	if (sdp) {
+	if (sdp && scsi_device_online(sdp))
 		ret = scsi_device_get(sdp);
-		if (!ret && !scsi_device_online(sdp)) {
-			ret = -ENODEV;
-			scsi_device_put(sdp);
-		}
-	} else {
+	else
 		ret = -ENODEV;
-	}
 	spin_unlock_irqrestore(hba->host->host_lock, flags);
 
 	if (ret)

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v6 7/8] scsi: core: Remove the put_device() call from scsi_device_get()
  2022-09-29 22:44 [PATCH v6 0/8] Prepare for constifying SCSI host templates Bart Van Assche
                   ` (5 preceding siblings ...)
  2022-09-29 22:44 ` [PATCH v6 6/8] scsi: ufs: Simplify ufshcd_set_dev_pwr_mode() Bart Van Assche
@ 2022-09-29 22:44 ` Bart Van Assche
  2022-09-29 22:44 ` [PATCH v6 8/8] scsi: core: Release SCSI devices synchronously Bart Van Assche
  7 siblings, 0 replies; 11+ messages in thread
From: Bart Van Assche @ 2022-09-29 22:44 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, Christoph Hellwig, Ming Lei,
	Hannes Reinecke, Mike Christie, Krzysztof Kozlowski,
	James E.J. Bottomley

scsi_device_get() may be called from atomic context, e.g. by
shost_for_each_device(). A later patch will allow put_device() to sleep
for SCSI devices. Hence this patch that removes the put_device() call
from scsi_device_get().

According to Rusty Russell's "Module Refcount and Stuff mini-FAQ",
calling module_put() from atomic context is allowed since considerable
time. See also https://lkml.org/lkml/2002/11/18/330.

Cc: Christoph Hellwig <hch@lst.de>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Hannes Reinecke <hare@suse.de>
Cc: Mike Christie <michael.christie@oracle.com>
Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/scsi/scsi.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/scsi/scsi.c b/drivers/scsi/scsi.c
index 086ec5b5862d..87bddd697fc6 100644
--- a/drivers/scsi/scsi.c
+++ b/drivers/scsi/scsi.c
@@ -563,14 +563,14 @@ int scsi_device_get(struct scsi_device *sdev)
 {
 	if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
 		goto fail;
-	if (!get_device(&sdev->sdev_gendev))
-		goto fail;
 	if (!try_module_get(sdev->host->hostt->module))
-		goto fail_put_device;
+		goto fail;
+	if (!get_device(&sdev->sdev_gendev))
+		goto fail_put_module;
 	return 0;
 
-fail_put_device:
-	put_device(&sdev->sdev_gendev);
+fail_put_module:
+	module_put(sdev->host->hostt->module);
 fail:
 	return -ENXIO;
 }

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v6 8/8] scsi: core: Release SCSI devices synchronously
  2022-09-29 22:44 [PATCH v6 0/8] Prepare for constifying SCSI host templates Bart Van Assche
                   ` (6 preceding siblings ...)
  2022-09-29 22:44 ` [PATCH v6 7/8] scsi: core: Remove the put_device() call from scsi_device_get() Bart Van Assche
@ 2022-09-29 22:44 ` Bart Van Assche
  7 siblings, 0 replies; 11+ messages in thread
From: Bart Van Assche @ 2022-09-29 22:44 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, Christoph Hellwig, Ming Lei,
	Hannes Reinecke, John Garry, Mike Christie, Krzysztof Kozlowski,
	James E.J. Bottomley

All upstream scsi_device_put() calls happen from thread context. Hence
simplify scsi_device_put() by always calling the release function
synchronously. This patch prepares for constifying the SCSI host template
by removing an assignment that clears the module pointer in the SCSI host
template.

scsi_device_dev_release_usercontext() was introduced in 2006 via
commit 65110b216895 ("[SCSI] fix wrong context bugs in SCSI").

Cc: Christoph Hellwig <hch@lst.de>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Hannes Reinecke <hare@suse.de>
Cc: John Garry <john.garry@huawei.com>
Cc: Mike Christie <michael.christie@oracle.com>
Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/scsi/scsi.c        |  2 ++
 drivers/scsi/scsi_sysfs.c  | 12 ++----------
 include/scsi/scsi_device.h |  1 -
 3 files changed, 4 insertions(+), 11 deletions(-)

diff --git a/drivers/scsi/scsi.c b/drivers/scsi/scsi.c
index 87bddd697fc6..e8bc4db297ab 100644
--- a/drivers/scsi/scsi.c
+++ b/drivers/scsi/scsi.c
@@ -586,6 +586,8 @@ EXPORT_SYMBOL(scsi_device_get);
  */
 void scsi_device_put(struct scsi_device *sdev)
 {
+	might_sleep();
+
 	/*
 	 * Decreasing the module reference count before the device reference
 	 * count is safe since scsi_remove_host() only returns after all
diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
index dc4bda4d2319..dd8e0d0399cd 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -441,10 +441,9 @@ static void scsi_device_cls_release(struct device *class_dev)
 	put_device(&sdev->sdev_gendev);
 }
 
-static void scsi_device_dev_release_usercontext(struct work_struct *work)
+static void scsi_device_dev_release(struct device *dev)
 {
-	struct scsi_device *sdev = container_of(work, struct scsi_device,
-						ew.work);
+	struct scsi_device *sdev = to_scsi_device(dev);
 	struct scsi_target *starget = sdev->sdev_target;
 	struct device *parent;
 	struct list_head *this, *tmp;
@@ -518,13 +517,6 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
 		put_device(parent);
 }
 
-static void scsi_device_dev_release(struct device *dev)
-{
-	struct scsi_device *sdp = to_scsi_device(dev);
-	execute_in_process_context(scsi_device_dev_release_usercontext,
-				   &sdp->ew);
-}
-
 static struct class sdev_class = {
 	.name		= "scsi_device",
 	.dev_release	= scsi_device_cls_release,
diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
index 78039d1ec405..fa6070ce8b9b 100644
--- a/include/scsi/scsi_device.h
+++ b/include/scsi/scsi_device.h
@@ -236,7 +236,6 @@ struct scsi_device {
 	struct device		sdev_gendev,
 				sdev_dev;
 
-	struct execute_work	ew; /* used to get process context on put */
 	struct work_struct	requeue_work;
 
 	struct scsi_device_handler *handler;

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v6 6/8] scsi: ufs: Simplify ufshcd_set_dev_pwr_mode()
  2022-09-29 22:44 ` [PATCH v6 6/8] scsi: ufs: Simplify ufshcd_set_dev_pwr_mode() Bart Van Assche
@ 2022-10-03  6:28   ` Adrian Hunter
  2022-10-03 16:23     ` Bart Van Assche
  0 siblings, 1 reply; 11+ messages in thread
From: Adrian Hunter @ 2022-10-03  6:28 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, Avri Altman, James E.J. Bottomley, Bean Huo, Jinyoung Choi

On 30/09/22 01:44, Bart Van Assche wrote:
> Simplify the code for incrementing the SCSI device reference count in
> ufshcd_set_dev_pwr_mode(). This patch removes one scsi_device_put() call
> that happens from atomic context.
> 
> Cc: Adrian Hunter <adrian.hunter@intel.com>
> Cc: Avri Altman <avri.altman@wdc.com>
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>

Not sure what patch set "v6 6/8" refers to, nevertheless:

Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>

> ---
>  drivers/ufs/core/ufshcd.c | 9 ++-------
>  1 file changed, 2 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
> index 7c15cbc737b4..6e61372fe027 100644
> --- a/drivers/ufs/core/ufshcd.c
> +++ b/drivers/ufs/core/ufshcd.c
> @@ -8753,15 +8753,10 @@ static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
>  
>  	spin_lock_irqsave(hba->host->host_lock, flags);
>  	sdp = hba->ufs_device_wlun;
> -	if (sdp) {
> +	if (sdp && scsi_device_online(sdp))
>  		ret = scsi_device_get(sdp);
> -		if (!ret && !scsi_device_online(sdp)) {
> -			ret = -ENODEV;
> -			scsi_device_put(sdp);
> -		}
> -	} else {
> +	else
>  		ret = -ENODEV;
> -	}
>  	spin_unlock_irqrestore(hba->host->host_lock, flags);
>  
>  	if (ret)


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v6 6/8] scsi: ufs: Simplify ufshcd_set_dev_pwr_mode()
  2022-10-03  6:28   ` Adrian Hunter
@ 2022-10-03 16:23     ` Bart Van Assche
  0 siblings, 0 replies; 11+ messages in thread
From: Bart Van Assche @ 2022-10-03 16:23 UTC (permalink / raw)
  To: Adrian Hunter, Martin K . Petersen
  Cc: linux-scsi, Avri Altman, James E.J. Bottomley, Bean Huo, Jinyoung Choi

On 10/2/22 23:28, Adrian Hunter wrote:
> On 30/09/22 01:44, Bart Van Assche wrote:
>> Simplify the code for incrementing the SCSI device reference count in
>> ufshcd_set_dev_pwr_mode(). This patch removes one scsi_device_put() call
>> that happens from atomic context.
>>
>> Cc: Adrian Hunter <adrian.hunter@intel.com>
>> Cc: Avri Altman <avri.altman@wdc.com>
>> Signed-off-by: Bart Van Assche <bvanassche@acm.org>
> 
> Not sure what patch set "v6 6/8" refers to, nevertheless:
> 
> Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>

Hi Adrian,

That means that this is the sixth patch of eight in version 6 of the 
patch series that prepares for constifying the SCSI host template. The 
entire patch series is available here: 
https://lore.kernel.org/linux-scsi/20220929224421.587465-1-bvanassche@acm.org/

Anyway, thanks for the review.

Bart.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-10-03 16:23 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-29 22:44 [PATCH v6 0/8] Prepare for constifying SCSI host templates Bart Van Assche
2022-09-29 22:44 ` [PATCH v6 1/8] scsi: esas2r: Initialize two host template members implicitly Bart Van Assche
2022-09-29 22:44 ` [PATCH v6 2/8] scsi: esas2r: Introduce scsi_template_proc_dir() Bart Van Assche
2022-09-29 22:44 ` [PATCH v6 3/8] scsi: core: Fail host creation if creating the proc directory fails Bart Van Assche
2022-09-29 22:44 ` [PATCH v6 4/8] scsi: core: Introduce a new list for SCSI proc directory entries Bart Van Assche
2022-09-29 22:44 ` [PATCH v6 5/8] scsi: core: Rework scsi_single_lun_run() Bart Van Assche
2022-09-29 22:44 ` [PATCH v6 6/8] scsi: ufs: Simplify ufshcd_set_dev_pwr_mode() Bart Van Assche
2022-10-03  6:28   ` Adrian Hunter
2022-10-03 16:23     ` Bart Van Assche
2022-09-29 22:44 ` [PATCH v6 7/8] scsi: core: Remove the put_device() call from scsi_device_get() Bart Van Assche
2022-09-29 22:44 ` [PATCH v6 8/8] scsi: core: Release SCSI devices synchronously Bart Van Assche

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.