Skip to contentSkip to conversation

async scsi scanning, version 12

PATCH In review Matthew Wilcox 3 participants 5 messages Sep 28, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 async scsi scanning, version 12

Hopefully the final version ... I've addressed all the comments I received on version 11. Please review the implementation of shost_for_each_device_safe.

Add ability to scan scsi busses asynchronously

Since it often takes around 20-30 seconds to scan a scsi bus, it's highly advantageous to do this in parallel with other things. The bulk of this patch is ensuring that devices don't change numbering, and that all devices are discovered prior to trying to start init. For those who build SCSI as modules, there's a new scsi_wait_scan module that will ensure all bus scans are finished.

This patch only handles drivers which call scsi_scan_host. Fibre Channel, SAS, SATA, USB and Firewire all need additional work.

Documentation/kernel-parameters.txt +5 -0
index 5498324..4b687ef 100644
@@ -1426,6 +1426,11 @@ running once the system is up.
 
 	scsi_logging=	[SCSI]
 
+	scsi_mod.scan=	[SCSI] sync (default) scans SCSI busses as they are
+			discovered.  async scans them in kernel threads,
+			allowing boot to proceed.  none ignores them, expecting
+			user space to do the scan.
+
 	selinux		[SELINUX] Disable or enable SELinux at boot time.
 			Format: { "0" | "1" }
 			See security/selinux/Kconfig help text.
drivers/scsi/Makefile +2 -0
index 1ef951b..38d0e25 100644
@@ -140,6 +140,8 @@ obj-$(CONFIG_CHR_DEV_SCH)	+= ch.o
 # This goes last, so that "real" scsi devices probe earlier
 obj-$(CONFIG_SCSI_DEBUG)	+= scsi_debug.o
 
+obj-$(CONFIG_SCSI)		+= scsi_wait_scan.o
+
 scsi_mod-y			+= scsi.o hosts.o scsi_ioctl.o constants.o \
 				   scsicam.o scsi_error.o scsi_lib.o \
 				   scsi_scan.o scsi_sysfs.o \
drivers/scsi/scsi_priv.h +3 -0
index 5d023d4..f458c2f 100644
@@ -39,6 +39,9 @@ static inline void scsi_log_completion(s
 	{ };
 #endif
 
+/* scsi_scan.c */
+int scsi_complete_async_scans(void);
+
 /* scsi_devinfo.c */
 extern int scsi_get_device_flags(struct scsi_device *sdev,
 				 const unsigned char *vendor,
drivers/scsi/scsi_scan.c +182 -6

248 lines not on this page · raw message

drivers/scsi/scsi_wait_scan.c new file +31 -0
new file mode 100644
index 0000000..f7aea46
@@ -0,0 +1,31 @@
+/*
+ * scsi_wait_scan.c
+ *
+ * Copyright (C) 2006 James Bottomley <[email protected]>
+ *
+ * This is a simple module to wait until all the async scans are
+ * complete.  The idea is to use it in initrd/initramfs scripts.  You
+ * modprobe it after all the modprobes of the root SCSI drivers and it
+ * will wait until they have all finished scanning their busses before
+ * allowing the boot to proceed
+ */
+
+#include <linux/module.h>
+#include "scsi_priv.h"
+
+static int __init wait_scan_init(void)
+{
+	scsi_complete_async_scans();
+	return 0;
+}
+
+static void __exit wait_scan_exit(void)
+{
+}
+ 
+MODULE_DESCRIPTION("SCSI wait for scans");
+MODULE_AUTHOR("James Bottomley");
+MODULE_LICENSE("GPL");
+ 
+late_initcall(wait_scan_init);
+module_exit(wait_scan_exit);
include/scsi/scsi_device.h +19 -1
index 895d212..f6e33ab 100644
@@ -227,7 +227,7 @@ extern struct scsi_device *__scsi_iterat
  * @sdev:	iterator
  * @host:	host whiches devices we want to iterate over
  *
- * This traverses over each devices of @shost.  The devices have
+ * This iterates over each device of @shost.  The devices have
  * a reference that must be released by scsi_host_put when breaking
  * out of the loop.
  */
@@ -237,6 +237,24 @@ #define shost_for_each_device(sdev, shos
 	     (sdev) = __scsi_iterate_devices((shost), (sdev)))
 
 /**
+ * shost_for_each_device_safe - iterate over all devices of a host allowing removal
+ * @sdev:	iterator
+ * @n:		another &struct scsi_device for temporary storage
+ * @host:	host whiches devices we want to iterate over
+ *
+ * This iterates over each device of @shost in a way that is safe against
+ * removals of the element being processed.  The devices have
+ * a reference that must be released by scsi_host_put when breaking
+ * out of the loop.
+ */
+#define shost_for_each_device_safe(sdev, n, shost) \
+	for ((sdev) = __scsi_iterate_devices((shost), NULL), \
+		(n) = __scsi_iterate_devices((shost), (sdev)); \
+	     (sdev); \
+	     (sdev) = (n), \
+		(n) = (n) ? __scsi_iterate_devices((shost), (n)) : NULL)
+
+/**
  * __shost_for_each_device  -  iterate over all devices of a host (UNLOCKED)
  * @sdev:	iterator
  * @host:	host whiches devices we want to iterate over
include/scsi/scsi_host.h +3 -0
index 39c6f8c..ba5b3eb 100644
@@ -552,6 +552,9 @@ struct Scsi_Host {
 	/* task mgmt function in progress */
 	unsigned tmf_in_progress:1;
 
+	/* Asynchronous scan in progress */
+	unsigned async_scan:1;
+
 	/*
 	 * Optional work queue to be utilized by the transport
 	 */
#1 lore raw
Quoted 23 lines · Matthew Wilcox

Hopefully the final version ... I've addressed all the comments I received on version 11. Please review the implementation of shost_for_each_device_safe.

Add ability to scan scsi busses asynchronously

Since it often takes around 20-30 seconds to scan a scsi bus, it's highly advantageous to do this in parallel with other things. The bulk of this patch is ensuring that devices don't change numbering, and that all devices are discovered prior to trying to start init. For those who build SCSI as modules, there's a new scsi_wait_scan module that will ensure all bus scans are finished.

This patch only handles drivers which call scsi_scan_host. Fibre Channel, SAS, SATA, USB and Firewire all need additional work.

Documentation/kernel-parameters.txt +0 -0
index 5498324..4b687ef 100644

Documentation/kernel-parameters.txt running once the system is up. · line 1432

Quoted 7 lines
Documentation/kernel-parameters.txt +4 -0
@@ -1426,6 +1426,11 @@ running once the system is up.
 
 	scsi_logging=	[SCSI]
 
+	scsi_mod.scan=	[SCSI] sync (default) scans SCSI busses as they are
+			discovered.  async scans them in kernel threads,
+			allowing boot to proceed.  none ignores them, expecting
+			user space to do the scan.

Matthew,
I like the "none" which is no doubt a place holder at the moment.

For the user space to do discovery, it either needs an out of band mechanism (e.g. IP) or the ability to talk to a host in the absence of any "devices" (targets or logical units). That requires a device node (e.g. /dev/mptctl) or something equivalent in sysfs (yuk).

Your "none" explanation could be slightly extended to say that the LLD (and/or its firmware) might do the discovery. As an example the SAS-2 draft now has self-configuring expanders (the terms "edge" and "fanout" have been dropped) which effectively discover the topology and track changes, configuring themselves and dumber expanders as required. Then host discovery becomes importing the topology from an external device. However not all devices may be visible to self-configuring expanders (e.g. a SATA disk could be directly attached to a SAS HBA). So some extra work may be required.

So at one extreme the discovery process may be distributed and at the other extreme there is no discovery at all. With modern transports (e.g. FC, SAS, iSCSI?) having world wide unique names who needs discovery?

Doug Gilbert

#3 lore raw
In reply to Matthew Wilcox
Quoted 7 lines · On Thu, 28 Sep 2006 12:24:38 -0600 Matthew Wilcox <[email protected]> wrote:

Add ability to scan scsi busses asynchronously

...

drivers/scsi/Makefile obj-$(CONFIG_CHR_DEV_SCH) += ch.o

Quoted 10 lines
drivers/scsi/Makefile +2 -0
@@ -140,6 +140,8 @@ obj-$(CONFIG_CHR_DEV_SCH)	+= ch.o
 # This goes last, so that "real" scsi devices probe earlier
 obj-$(CONFIG_SCSI_DEBUG)	+= scsi_debug.o
 
+obj-$(CONFIG_SCSI)		+= scsi_wait_scan.o
+
 scsi_mod-y			+= scsi.o hosts.o scsi_ioctl.o constants.o \
 				   scsicam.o scsi_error.o scsi_lib.o \
 				   scsi_scan.o scsi_sysfs.o \
 
...

I think that's supposed to go into scsi_mod-y, at line 146.

Quoted 2 lines

+static char scsi_scan_type[6] = "sync";

That wasted a byte.

drivers/scsi/Makefile MODULE_PARM_DESC(inq_timeout, · line 118

Quoted 12 lines
drivers/scsi/Makefile +5 -0
+
+module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO);
+MODULE_PARM_DESC(scan, "sync, async or none");
+
 /*
  * max_scsi_report_luns: the maximum number of LUNS that will be
  * returned from the REPORT LUNS command. 8 times this value must
@@ -108,6 +115,53 @@ MODULE_PARM_DESC(inq_timeout, 
 		 "Timeout (in seconds) waiting for devices to answer INQUIRY."
 		 " Default is 5. Some non-compliant devices need more.");
 
+static spinlock_t async_scan_lock = SPIN_LOCK_UNLOCKED;

DEFINE_SPINLOCK()

Quoted 9 lines
drivers/scsi/Makefile +9 -0
+static LIST_HEAD(scanning_hosts);
+
+struct async_scan_data {
+	struct list_head list;
+	struct Scsi_Host *shost;
+	struct completion prev_finished;
+};
+
+int scsi_complete_async_scans(void)

drivers/scsi/Makefile MODULE_PARM_DESC(inq_timeout, · line 137

Quoted 10 lines
drivers/scsi/Makefile +10 -0
+{
+	struct async_scan_data *data;
+
+	do {
+		if (list_empty(&scanning_hosts))
+			return 0;
+		data = kmalloc(sizeof(*data), GFP_KERNEL);
+		if (!data)
+			msleep(1);
+	} while (!data);

ick. Immortal allocation loops are poor form. If there's really no alternative, please use __GFP_NOFAIL, so people can easily grep for your sins.

drivers/scsi/Makefile MODULE_PARM_DESC(inq_timeout, · line 145

Quoted 8 lines
drivers/scsi/Makefile +8 -0
+	data->shost = NULL;
+	init_completion(&data->prev_finished);
+
+	spin_lock(&async_scan_lock);
+	if (list_empty(&scanning_hosts))
+		goto done;
+	list_add_tail(&data->list, &scanning_hosts);
+	spin_unlock(&async_scan_lock);

"If the list is not empty, stick something on it".

What an unusual thing to do. I'm sure it makes sense, but some comments explaining what's going on would be nice.

Quoted 6 lines
drivers/scsi/Makefile +6 -0
+	printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n");
+	wait_for_completion(&data->prev_finished);
+
+	spin_lock(&async_scan_lock);
+	list_del(&data->list);
+ done:

drivers/scsi/Makefile MODULE_PARM_DESC(inq_timeout, · line 161

Quoted 10 lines
drivers/scsi/Makefile +10 -0
+	spin_unlock(&async_scan_lock);
+
+	kfree(data);
+	return 0;
+}
+
+#ifdef MODULE
+/* Only exported for the benefit of scsi_wait_scan */
+EXPORT_SYMBOL_GPL(scsi_complete_async_scans);
+#endif

Is that actually needed? AFACIT this .o file will just get linked against the one which contains scsi_wait_scan() anyway.

#2 lore raw
In reply to Douglas Gilbert
Quoted 8 lines · Douglas Gilbert

Documentation/kernel-parameters.txt running once the system is up. · line 1432

Quoted 4 lines
Documentation/kernel-parameters.txt +4 -0
+	scsi_mod.scan=	[SCSI] sync (default) scans SCSI busses as they are
+			discovered.  async scans them in kernel threads,
+			allowing boot to proceed.  none ignores them, expecting
+			user space to do the scan.

Matthew,
I like the "none" which is no doubt a place holder at the moment.

I wouldn't say it was extensively tested, but no, there's checks for the value "none" and it does indeed fail to discover any devices ;-)

Quoted 5 lines

For the user space to do discovery, it either needs an out of band mechanism (e.g. IP) or the ability to talk to a host in the absence of any "devices" (targets or logical units). That requires a device node (e.g. /dev/mptctl) or something equivalent in sysfs (yuk).

I must confess to having not thought about how userspace probes a scsi host to find out what devices are behind it. This was a feature that James asked for and it was easy to add.

Quoted 2 lines

Your "none" explanation could be slightly extended to say that the LLD (and/or its firmware) might do the discovery.

Note that by specifying "none", not even the FC/SAS/etc drivers can register targets as they find them -- it really is up to userspace to echo scsi-add-single-device H C T L >/proc/scsi/scsi

I'm a little uncomfortable with that, and I'd be open to adding another word that means "no scanning, but if the driver's been told about the device by a switch, add it automatically, don't wait for userspace". I do think that none needs to mean none though.

Quoted 9 lines

As an
example the SAS-2 draft now has self-configuring expanders (the terms "edge" and "fanout" have been dropped) which effectively discover the topology and track changes, configuring themselves and dumber expanders as required. Then host discovery becomes importing the topology from an external device. However not all devices may be visible to self-configuring expanders (e.g. a SATA disk could be directly attached to a SAS HBA). So some extra work may be required.

That would be up to userspace in the "none" view of the world. I could see people wanting to ignore the self-configuring expander and impose a new (incorrect) topology on the system.

BTW, there'll be a lucky version 13 in a few minutes ... shost_for_each_device_safe isn't.

#4 lore raw
In reply to Andrew Morton
Quoted 7 lines · Andrew Morton

drivers/scsi/Makefile obj-$(CONFIG_CHR_DEV_SCH) += ch.o · line 147

Quoted 5 lines
drivers/scsi/Makefile +2 -0
+obj-$(CONFIG_SCSI)		+= scsi_wait_scan.o
+
 scsi_mod-y			+= scsi.o hosts.o scsi_ioctl.o constants.o \
 				   scsicam.o scsi_error.o scsi_lib.o \
 				   scsi_scan.o scsi_sysfs.o \

I think that's supposed to go into scsi_mod-y, at line 146.

Nope. If CONFIG_SCSI is a module, we want this module around so people can insmod it in order to ensure that scsi discovery is complete. If CONFIG_SCSI is built-in, we want this built-in too.

Quoted 3 lines
Quoted 1 line

+static char scsi_scan_type[6] = "sync";

That wasted a byte.

If you specify "async" then the buffer needs to be 6 bytes long.

Quoted 4 lines
Quoted 2 lines

+static spinlock_t async_scan_lock = SPIN_LOCK_UNLOCKED;

DEFINE_SPINLOCK()

OK.

Quoted 7 lines
Quoted 7 lines
drivers/scsi/scsi_scan.c +7 -0
+int scsi_complete_async_scans(void)
+{
+	struct async_scan_data *data;
+
+	do {
+		if (list_empty(&scanning_hosts))
+			return 0;
Quoted 8 lines

drivers/scsi/scsi_scan.c MODULE_PARM_DESC(inq_timeout, · line 137

Quoted 4 lines
drivers/scsi/scsi_scan.c +4 -0
+		data = kmalloc(sizeof(*data), GFP_KERNEL);
+		if (!data)
+			msleep(1);
+	} while (!data);

ick. Immortal allocation loops are poor form. If there's really no alternative, please use __GFP_NOFAIL, so people can easily grep for your sins.

It's not immortal. The scanning threads will eventually terminate, and so will this loop.

Quoted 11 lines

drivers/scsi/scsi_scan.c MODULE_PARM_DESC(inq_timeout, · line 146

Quoted 8 lines
drivers/scsi/scsi_scan.c +8 -0
+	data->shost = NULL;
+	init_completion(&data->prev_finished);
+
+	spin_lock(&async_scan_lock);
+	if (list_empty(&scanning_hosts))
+		goto done;
+	list_add_tail(&data->list, &scanning_hosts);
+	spin_unlock(&async_scan_lock);

"If the list is not empty, stick something on it".

Quoted 2 lines

What an unusual thing to do. I'm sure it makes sense, but some comments explaining what's going on would be nice.

OK.

Quoted 7 lines

drivers/scsi/scsi_scan.c MODULE_PARM_DESC(inq_timeout, · line 163

Quoted 4 lines
drivers/scsi/scsi_scan.c +4 -0
+#ifdef MODULE
+/* Only exported for the benefit of scsi_wait_scan */
+EXPORT_SYMBOL_GPL(scsi_complete_async_scans);
+#endif

Is that actually needed? AFACIT this .o file will just get linked against the one which contains scsi_wait_scan() anyway.

See above.