All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/3] Prepare for constifying SCSI host templates
@ 2022-09-08 23:35 Bart Van Assche
  2022-09-08 23:35 ` [PATCH v3 1/3] scsi: esas2r: Introduce scsi_template_proc_dir() Bart Van Assche
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Bart Van Assche @ 2022-09-08 23:35 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. Please consider
this patch series for the next merge window.

Thanks,

Bart.

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 (3):
  scsi: esas2r: Introduce scsi_template_proc_dir()
  scsi: core: Introduce a new list for SCSI proc directory entries
  scsi: core: Rework the code for dropping the LLD module reference

 drivers/scsi/esas2r/esas2r_main.c |  18 +++--
 drivers/scsi/scsi_priv.h          |   4 +-
 drivers/scsi/scsi_proc.c          | 119 ++++++++++++++++++++++++++----
 drivers/scsi/scsi_sysfs.c         |   7 +-
 include/scsi/scsi_device.h        |   1 +
 include/scsi/scsi_host.h          |  18 ++---
 6 files changed, 126 insertions(+), 41 deletions(-)


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

* [PATCH v3 1/3] scsi: esas2r: Introduce scsi_template_proc_dir()
  2022-09-08 23:35 [PATCH v3 0/3] Prepare for constifying SCSI host templates Bart Van Assche
@ 2022-09-08 23:35 ` Bart Van Assche
  2022-09-13 14:05   ` John Garry
  2022-09-08 23:35 ` [PATCH v3 2/3] scsi: core: Introduce a new list for SCSI proc directory entries Bart Van Assche
  2022-09-08 23:36 ` [PATCH v3 3/3] scsi: core: Rework the code for dropping the LLD module reference Bart Van Assche
  2 siblings, 1 reply; 9+ messages in thread
From: Bart Van Assche @ 2022-09-08 23:35 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, Bradley Grove, Christoph Hellwig,
	Ming Lei, Hannes Reinecke, John Garry, 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.

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: 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/esas2r/esas2r_main.c | 18 +++++++++++-------
 drivers/scsi/scsi_proc.c          | 11 +++++++++++
 include/scsi/scsi_host.h          |  6 ++++++
 3 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/drivers/scsi/esas2r/esas2r_main.c b/drivers/scsi/esas2r/esas2r_main.c
index 7a4eadad23d7..fbf7fdb3b52a 100644
--- a/drivers/scsi/esas2r/esas2r_main.c
+++ b/drivers/scsi/esas2r/esas2r_main.c
@@ -248,7 +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,
@@ -637,10 +636,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;
@@ -730,11 +732,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..eeb9261c93f7 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(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 *);

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

* [PATCH v3 2/3] scsi: core: Introduce a new list for SCSI proc directory entries
  2022-09-08 23:35 [PATCH v3 0/3] Prepare for constifying SCSI host templates Bart Van Assche
  2022-09-08 23:35 ` [PATCH v3 1/3] scsi: esas2r: Introduce scsi_template_proc_dir() Bart Van Assche
@ 2022-09-08 23:35 ` Bart Van Assche
  2022-09-12 16:01   ` Mike Christie
  2022-09-13 14:26   ` John Garry
  2022-09-08 23:36 ` [PATCH v3 3/3] scsi: core: Rework the code for dropping the LLD module reference Bart Van Assche
  2 siblings, 2 replies; 9+ messages in thread
From: Bart Van Assche @ 2022-09-08 23:35 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

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 <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_priv.h |   4 +-
 drivers/scsi/scsi_proc.c | 110 +++++++++++++++++++++++++++++++++------
 include/scsi/scsi_host.h |  12 -----
 3 files changed, 95 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 eeb9261c93f7..e04db652a81e 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 char		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(scsi_template_proc_dir);
 
@@ -109,34 +152,56 @@ 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);
+		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 +213,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 +240,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;
 

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

* [PATCH v3 3/3] scsi: core: Rework the code for dropping the LLD module reference
  2022-09-08 23:35 [PATCH v3 0/3] Prepare for constifying SCSI host templates Bart Van Assche
  2022-09-08 23:35 ` [PATCH v3 1/3] scsi: esas2r: Introduce scsi_template_proc_dir() Bart Van Assche
  2022-09-08 23:35 ` [PATCH v3 2/3] scsi: core: Introduce a new list for SCSI proc directory entries Bart Van Assche
@ 2022-09-08 23:36 ` Bart Van Assche
  2 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2022-09-08 23:36 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

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 <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_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 */

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

* Re: [PATCH v3 2/3] scsi: core: Introduce a new list for SCSI proc directory entries
  2022-09-08 23:35 ` [PATCH v3 2/3] scsi: core: Introduce a new list for SCSI proc directory entries Bart Van Assche
@ 2022-09-12 16:01   ` Mike Christie
  2022-09-13 14:26   ` John Garry
  1 sibling, 0 replies; 9+ messages in thread
From: Mike Christie @ 2022-09-12 16:01 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, Christoph Hellwig, Ming Lei, Hannes Reinecke,
	John Garry, Krzysztof Kozlowski, James E.J. Bottomley

On 9/8/22 6:35 PM, Bart Van Assche wrote:
> 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:
> 

Reviewed-by: Mike Christie <michael.christie@oracle.com>


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

* Re: [PATCH v3 1/3] scsi: esas2r: Introduce scsi_template_proc_dir()
  2022-09-08 23:35 ` [PATCH v3 1/3] scsi: esas2r: Introduce scsi_template_proc_dir() Bart Van Assche
@ 2022-09-13 14:05   ` John Garry
  2022-09-13 19:51     ` Bart Van Assche
  0 siblings, 1 reply; 9+ messages in thread
From: John Garry @ 2022-09-13 14:05 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, Bradley Grove, Christoph Hellwig, Ming Lei,
	Hannes Reinecke, Mike Christie, Krzysztof Kozlowski,
	James E.J. Bottomley

On 09/09/2022 00:35, Bart Van Assche wrote:
> Prepare for removing the 'proc_dir' and 'present' members from the SCSI
> host template. This patch does not change any functionality.
> 
> 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: 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/esas2r/esas2r_main.c | 18 +++++++++++-------
>   drivers/scsi/scsi_proc.c          | 11 +++++++++++
>   include/scsi/scsi_host.h          |  6 ++++++
>   3 files changed, 28 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/scsi/esas2r/esas2r_main.c b/drivers/scsi/esas2r/esas2r_main.c
> index 7a4eadad23d7..fbf7fdb3b52a 100644
> --- a/drivers/scsi/esas2r/esas2r_main.c
> +++ b/drivers/scsi/esas2r/esas2r_main.c
> @@ -248,7 +248,6 @@ static struct scsi_host_template driver_template = {
>   	.sg_tablesize			= SG_CHUNK_SIZE,
>   	.cmd_per_lun			=
>   		ESAS2R_DEFAULT_CMD_PER_LUN,
> -	.present			= 0,

nit: this really could be a separate change. It's not really strictly 
related to introducing scsi_template_proc_dir()

>   	.emulated			= 0,
>   	.proc_name			= ESAS2R_DRVR_NAME,
>   	.change_queue_depth		= scsi_change_queue_depth,
> @@ -637,10 +636,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;
> @@ -730,11 +732,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..eeb9261c93f7 100644
> --- a/drivers/scsi/scsi_proc.c
> +++ b/drivers/scsi/scsi_proc.c

Again, seems it should be a separate change - this is not esas2r code now

> @@ -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(scsi_template_proc_dir);

Don't we encourage EXPORT_SYMBOL_GPL? The core code seems a mishmash.

> +
>   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 *


I don't feel too strongly about this, but elsewhere we have extern and I 
thought that consistency trumps coding standards.

> +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	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/3] scsi: core: Introduce a new list for SCSI proc directory entries
  2022-09-08 23:35 ` [PATCH v3 2/3] scsi: core: Introduce a new list for SCSI proc directory entries Bart Van Assche
  2022-09-12 16:01   ` Mike Christie
@ 2022-09-13 14:26   ` John Garry
  2022-09-13 18:38     ` Bart Van Assche
  1 sibling, 1 reply; 9+ messages in thread
From: John Garry @ 2022-09-13 14:26 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, Christoph Hellwig, Ming Lei, Hannes Reinecke,
	Mike Christie, Krzysztof Kozlowski, James E.J. Bottomley

On 09/09/2022 00:35, Bart Van Assche wrote:
> 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 <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_priv.h |   4 +-
>   drivers/scsi/scsi_proc.c | 110 +++++++++++++++++++++++++++++++++------
>   include/scsi/scsi_host.h |  12 -----
>   3 files changed, 95 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 eeb9261c93f7..e04db652a81e 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 char		present;

is there really a hard limit of 255 hosts?

> +};
>   
>   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);

I'm not sure we really need this - maybe a comment would be better. I 
don't care too much either way.

> +
> +	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(scsi_template_proc_dir);
>   
> @@ -109,34 +152,56 @@ 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);
> +		goto unlock;

hmmm... in this case we don't add e to the list. As unlikely as it 
seems, if a subsequent attempt to create the proc dir passes for another 
host with the same sht, then e->present would be incorrect, right?

> +	}
> +	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 +213,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 +240,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;
>   
> .


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

* Re: [PATCH v3 2/3] scsi: core: Introduce a new list for SCSI proc directory entries
  2022-09-13 14:26   ` John Garry
@ 2022-09-13 18:38     ` Bart Van Assche
  0 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2022-09-13 18:38 UTC (permalink / raw)
  To: John Garry, Martin K . Petersen
  Cc: linux-scsi, Christoph Hellwig, Ming Lei, Hannes Reinecke,
	Mike Christie, Krzysztof Kozlowski, James E.J. Bottomley

On 9/13/22 07:26, John Garry wrote:
> On 09/09/2022 00:35, Bart Van Assche wrote:
>> +struct scsi_proc_entry {
>> +    struct list_head    entry;
>> +    const struct scsi_host_template *sht;
>> +    struct proc_dir_entry    *proc_dir;
>> +    unsigned char        present;
> 
> is there really a hard limit of 255 hosts?

Apparently that limit exists today. I can use a wider data type for the 
'present' member if you want.

>> +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);
> 
> I'm not sure we really need this - maybe a comment would be better. I 
> don't care too much either way.

lockdep_assert_held() statements get verified at runtime if lockdep is 
enabled but comments not. This is why I prefer lockdep_assert_held() 
over a comment.

>> +    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);
>> +        goto unlock;
> 
> hmmm... in this case we don't add e to the list. As unlikely as it 
> seems, if a subsequent attempt to create the proc dir passes for another 
> host with the same sht, then e->present would be incorrect, right?

Right, this needs to be fixed.

Thanks,

Bart.

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

* Re: [PATCH v3 1/3] scsi: esas2r: Introduce scsi_template_proc_dir()
  2022-09-13 14:05   ` John Garry
@ 2022-09-13 19:51     ` Bart Van Assche
  0 siblings, 0 replies; 9+ messages in thread
From: Bart Van Assche @ 2022-09-13 19:51 UTC (permalink / raw)
  To: John Garry, Martin K . Petersen
  Cc: linux-scsi, Bradley Grove, Christoph Hellwig, Ming Lei,
	Hannes Reinecke, Mike Christie, Krzysztof Kozlowski,
	James E.J. Bottomley

On 9/13/22 07:05, John Garry wrote:
> On 09/09/2022 00:35, Bart Van Assche wrote:
>> diff --git a/drivers/scsi/esas2r/esas2r_main.c 
>> b/drivers/scsi/esas2r/esas2r_main.c
>> index 7a4eadad23d7..fbf7fdb3b52a 100644
>> --- a/drivers/scsi/esas2r/esas2r_main.c
>> +++ b/drivers/scsi/esas2r/esas2r_main.c
>> @@ -248,7 +248,6 @@ static struct scsi_host_template driver_template = {
>>       .sg_tablesize            = SG_CHUNK_SIZE,
>>       .cmd_per_lun            =
>>           ESAS2R_DEFAULT_CMD_PER_LUN,
>> -    .present            = 0,
> 
> nit: this really could be a separate change. It's not really strictly 
> related to introducing scsi_template_proc_dir()

I will move this change into a separate patch.

>> diff --git a/drivers/scsi/scsi_proc.c b/drivers/scsi/scsi_proc.c
>> index 95aee1ad1383..eeb9261c93f7 100644
>> --- a/drivers/scsi/scsi_proc.c
>> +++ b/drivers/scsi/scsi_proc.c
> 
> Again, seems it should be a separate change - this is not esas2r code now

In the kernel community it is strongly recommended to introduce new 
functions in the same patch that adds a user. Hence the presence of 
scsi_proc.c changes in this patch.

>> +EXPORT_SYMBOL(scsi_template_proc_dir);
> 
> Don't we encourage EXPORT_SYMBOL_GPL? The core code seems a mishmash.

I will change this into EXPORT_SYMBOL_GPL().

>> +#if defined(CONFIG_SCSI_PROC_FS)
>> +struct proc_dir_entry *
> 
> I don't feel too strongly about this, but elsewhere we have extern and I 
> thought that consistency trumps coding standards.

Should I submit a separate patch that removes superfluous uses of the 
'extern' keyword?

Thanks,

Bart.

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

end of thread, other threads:[~2022-09-13 19:51 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-08 23:35 [PATCH v3 0/3] Prepare for constifying SCSI host templates Bart Van Assche
2022-09-08 23:35 ` [PATCH v3 1/3] scsi: esas2r: Introduce scsi_template_proc_dir() Bart Van Assche
2022-09-13 14:05   ` John Garry
2022-09-13 19:51     ` Bart Van Assche
2022-09-08 23:35 ` [PATCH v3 2/3] scsi: core: Introduce a new list for SCSI proc directory entries Bart Van Assche
2022-09-12 16:01   ` Mike Christie
2022-09-13 14:26   ` John Garry
2022-09-13 18:38     ` Bart Van Assche
2022-09-08 23:36 ` [PATCH v3 3/3] scsi: core: Rework the code for dropping the LLD module reference 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.