Skip to contentSkip to conversation

Improve device information handling

PATCH In review Bart Van Assche 2 participants 6 messages Nov 15, 2017 – Nov 16, 2017 SCSI

lore
Share & tools
lore
#0 lore raw

cover Improve device information handling

Hello Martin,

There are multiple issues with the device information flag handling (a.k.a. blacklist flags):
* For neither scsi_get_device_flags() nor scsi_get_device_flags_keyed() it is possible to determine whether an error code has been returned or whether the flag with bit position 31 was set.
* A large number of sparse warnings is generated by the blacklist flags code. The three patches in this series address these issues.

Please consider these patches for inclusion in the upstream kernel.

Thanks,

Bart.

Bart Van Assche (3):
device information: Do not interpret error codes as flags Constify scsi_dev_info_list_add_keyed() string arguments Introduce a type for device information flags, namely bflags_t

 drivers/scsi/scsi_devinfo.c       | 48 +++++++++++++++++++-----------------
 drivers/scsi/scsi_priv.h          | 12 +++++----
 drivers/scsi/scsi_scan.c          | 28 +++++++++++----------
 drivers/scsi/scsi_sysfs.c         |  4 +--
 drivers/scsi/scsi_transport_spi.c | 11 +++++----
 include/scsi/scsi_device.h        |  3 ++-
 include/scsi/scsi_devinfo.h       | 52 ++++++++++++++++++++-------------------
 include/scsi/scsi_types.h         |  6 +++++
 8 files changed, 90 insertions(+), 74 deletions(-)
 create mode 100644 include/scsi/scsi_types.h
Signature · 1 line
2.15.0
#1 lore raw

PATCH 1/3 device information: Do not interpret error codes as flags

Since commit 28a0bc4120d3 ("scsi: sd: Implement blacklist option for WRITE SAME w/ UNMAP") bit 31 is a valid device information flag. Separate device information flags and return codes such that it becomes possible to check whether or not scsi_get_device_flags_keyed(). succeeded. This patch also avoids that error codes returned by scsi_get_device_flags_keyed() are interpret as device flags.

Cc Christoph Hellwig <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
---
 drivers/scsi/scsi_devinfo.c       | 27 +++++++++++++++------------
 drivers/scsi/scsi_priv.h          |  6 ++++--
 drivers/scsi/scsi_scan.c          | 13 +++++++------
 drivers/scsi/scsi_transport_spi.c |  7 ++++---
 4 files changed, 30 insertions(+), 23 deletions(-)
drivers/scsi/scsi_devinfo.c +15 -12
index fe5a9ea27b5e..e63873537139 100644
@@ -564,6 +564,7 @@ static int scsi_dev_info_list_add_str(char *dev_list)
  * @sdev:       &scsi_device to get flags for
  * @vendor:	vendor name
  * @model:	model name
+ * @flags:      (output) device specific flags
  *
  * Description:
  *     Search the global scsi_dev_info_list (specified by list zero)
@@ -571,12 +572,11 @@ static int scsi_dev_info_list_add_str(char *dev_list)
  *     matching flags value, else return the host or global default
  *     settings.  Called during scan time.
  **/
-int scsi_get_device_flags(struct scsi_device *sdev,
-			  const unsigned char *vendor,
-			  const unsigned char *model)
+int scsi_get_device_flags(struct scsi_device *sdev, const unsigned char *vendor,
+			  const unsigned char *model, unsigned int *flags)
 {
 	return scsi_get_device_flags_keyed(sdev, vendor, model,
-					   SCSI_DEVINFO_GLOBAL);
+					   SCSI_DEVINFO_GLOBAL, flags);
 }
 
 
@@ -586,6 +586,7 @@ int scsi_get_device_flags(struct scsi_device *sdev,
  * @vendor:	vendor name
  * @model:	model name
  * @key:	list to look up
+ * @flags:      (output) device specific flags
  *
  * Description:
  *     Search the scsi_dev_info_list specified by @key for an entry
@@ -596,28 +597,30 @@ int scsi_get_device_flags(struct scsi_device *sdev,
 int scsi_get_device_flags_keyed(struct scsi_device *sdev,
 				const unsigned char *vendor,
 				const unsigned char *model,
-				int key)
+				int key, unsigned int *flags)
 {
 	struct scsi_dev_info_list *devinfo;
 	int err;
 
 	devinfo = scsi_dev_info_list_find(vendor, model, key);
-	if (!IS_ERR(devinfo))
-		return devinfo->flags;
+	if (!IS_ERR(devinfo)) {
+		*flags = devinfo->flags;
+		return 0;
+	}
 
 	err = PTR_ERR(devinfo);
 	if (err != -ENOENT)
 		return err;
 
 	/* nothing found, return nothing */
-	if (key != SCSI_DEVINFO_GLOBAL)
+	if (key != SCSI_DEVINFO_GLOBAL) {
+		*flags = 0;
 		return 0;
+	}
 
 	/* except for the global list, where we have an exception */
-	if (sdev->sdev_bflags)
-		return sdev->sdev_bflags;
-
-	return scsi_default_dev_flags;
+	*flags = sdev->sdev_bflags ? : scsi_default_dev_flags;
+	return 0;
 }
 EXPORT_SYMBOL(scsi_get_device_flags_keyed);
 
drivers/scsi/scsi_priv.h +4 -2
index 320318487bd4..d79759271fe3 100644
@@ -52,10 +52,12 @@ enum {
 
 extern int scsi_get_device_flags(struct scsi_device *sdev,
 				 const unsigned char *vendor,
-				 const unsigned char *model);
+				 const unsigned char *model,
+				 unsigned int *flags);
 extern int scsi_get_device_flags_keyed(struct scsi_device *sdev,
 				       const unsigned char *vendor,
-				       const unsigned char *model, int key);
+				       const unsigned char *model, int key,
+				       unsigned int *flags);
 extern int scsi_dev_info_list_add_keyed(int compatible, char *vendor,
 					char *model, char *strflags,
 					int flags, int key);
drivers/scsi/scsi_scan.c +7 -6
index a0f2a20ea9e9..5b9571f30d82 100644
@@ -650,8 +650,8 @@ static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
 		 * corresponding bit fields in scsi_device, so bflags
 		 * need not be passed as an argument.
 		 */
-		*bflags = scsi_get_device_flags(sdev, &inq_result[8],
-				&inq_result[16]);
+		scsi_get_device_flags(sdev, &inq_result[8], &inq_result[16],
+				      bflags);
 
 		/* When the first pass succeeds we gain information about
 		 * what larger transfer lengths might work. */
@@ -1074,10 +1074,11 @@ static int scsi_probe_and_add_lun(struct scsi_target *starget,
 			else
 				scsi_device_put(sdev);
 
-			if (bflagsp)
-				*bflagsp = scsi_get_device_flags(sdev,
-								 sdev->vendor,
-								 sdev->model);
+			if (bflagsp) {
+				*bflagsp = 0;
+				scsi_get_device_flags(sdev, sdev->vendor,
+						      sdev->model, bflagsp);
+			}
 			return SCSI_SCAN_LUN_PRESENT;
 		}
 		scsi_device_put(sdev);
drivers/scsi/scsi_transport_spi.c +4 -3
index d0219e36080c..5f4cea2d07c8 100644
@@ -221,9 +221,10 @@ static int spi_device_configure(struct transport_container *tc,
 {
 	struct scsi_device *sdev = to_scsi_device(dev);
 	struct scsi_target *starget = sdev->sdev_target;
-	unsigned bflags = scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8],
-						      &sdev->inquiry[16],
-						      SCSI_DEVINFO_SPI);
+	unsigned int bflags = 0;
+
+	scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8], &sdev->inquiry[16],
+				    SCSI_DEVINFO_SPI, &bflags);
 
 	/* Populate the target capability fields with the values
 	 * gleaned from the device inquiry */
Signature · 1 line
2.15.0
#2 lore raw

Re: [PATCH 1/3] device information: Do not interpret error codes as flags

Hi Bart,

Quoted 2 lines

This patch also avoids that error codes returned by scsi_get_device_flags_keyed() are interpret as device flags.

Nobody expects this and scsi_get_device_flags() to ever return an error. So I'd rather have the != -ENOENT case fixed up to return 0.

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#3 lore raw

PATCH 2/3 Constify scsi_dev_info_list_add_keyed() string arguments

In reply to Bart Van Assche

This patch does not change any functionality.

Cc Christoph Hellwig <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
---
 drivers/scsi/scsi_devinfo.c | 9 +++++----
 drivers/scsi/scsi_priv.h    | 4 ++--
 2 files changed, 7 insertions(+), 6 deletions(-)
drivers/scsi/scsi_devinfo.c +5 -4
index e63873537139..d195560a641c 100644
@@ -292,8 +292,8 @@ static struct scsi_dev_info_list_table *scsi_devinfo_lookup_by_key(int key)
  * scsi_strcpy_devinfo: called from scsi_dev_info_list_add to copy into
  * devinfo vendor and model strings.
  */
-static void scsi_strcpy_devinfo(char *name, char *to, size_t to_length,
-				char *from, int compatible)
+static void scsi_strcpy_devinfo(const char *name, char *to, size_t to_length,
+				const char *from, int compatible)
 {
 	size_t from_length;
 
@@ -360,8 +360,9 @@ static int scsi_dev_info_list_add(int compatible, char *vendor, char *model,
  *
  * Returns: 0 OK, -error on failure.
  **/
-int scsi_dev_info_list_add_keyed(int compatible, char *vendor, char *model,
-				 char *strflags, int flags, int key)
+int scsi_dev_info_list_add_keyed(int compatible, const char *vendor,
+				 const char *model, const char *strflags,
+				 int flags, int key)
 {
 	struct scsi_dev_info_list *devinfo;
 	struct scsi_dev_info_list_table *devinfo_table =
drivers/scsi/scsi_priv.h +2 -2
index d79759271fe3..b82238577b19 100644
@@ -58,8 +58,8 @@ extern int scsi_get_device_flags_keyed(struct scsi_device *sdev,
 				       const unsigned char *vendor,
 				       const unsigned char *model, int key,
 				       unsigned int *flags);
-extern int scsi_dev_info_list_add_keyed(int compatible, char *vendor,
-					char *model, char *strflags,
+extern int scsi_dev_info_list_add_keyed(int compatible, const char *vendor,
+					const char *model, const char *strflags,
 					int flags, int key);
 extern int scsi_dev_info_list_del_keyed(char *vendor, char *model, int key);
 extern int scsi_dev_info_add_list(int key, const char *name);
Signature · 1 line
2.15.0
#4 lore raw

PATCH 3/3 Introduce a type for device information flags, namely bflags_t

In reply to Bart Van Assche

Additionally, suppress recently introduced sparse warnings related to blacklist flags.

Cc Christoph Hellwig <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
---
 drivers/scsi/scsi_devinfo.c       | 20 +++++++--------
 drivers/scsi/scsi_priv.h          |  6 ++---
 drivers/scsi/scsi_scan.c          | 15 +++++------
 drivers/scsi/scsi_sysfs.c         |  4 +--
 drivers/scsi/scsi_transport_spi.c |  6 ++---
 include/scsi/scsi_device.h        |  3 ++-
 include/scsi/scsi_devinfo.h       | 52 ++++++++++++++++++++-------------------
 include/scsi/scsi_types.h         |  6 +++++
 8 files changed, 60 insertions(+), 52 deletions(-)
 create mode 100644 include/scsi/scsi_types.h
drivers/scsi/scsi_devinfo.c +9 -11
index d195560a641c..575da174dad7 100644
@@ -22,7 +22,7 @@ struct scsi_dev_info_list {
 	struct list_head dev_info_list;
 	char vendor[8];
 	char model[16];
-	unsigned flags;
+	bflags_t flags;
 	unsigned compatible; /* for use with scsi_static_device_list entries */
 };
 
@@ -52,7 +52,7 @@ static struct {
 	char *vendor;
 	char *model;
 	char *revision;	/* revision known to be bad, unused */
-	unsigned flags;
+	bflags_t flags;
 } scsi_static_device_list[] __initdata = {
 	/*
 	 * The following devices are known not to tolerate a lun != 0 scan
@@ -335,7 +335,7 @@ static void scsi_strcpy_devinfo(const char *name, char *to, size_t to_length,
  * Returns: 0 OK, -error on failure.
  **/
 static int scsi_dev_info_list_add(int compatible, char *vendor, char *model,
-			    char *strflags, int flags)
+			    char *strflags, bflags_t flags)
 {
 	return scsi_dev_info_list_add_keyed(compatible, vendor, model,
 					    strflags, flags,
@@ -362,7 +362,7 @@ static int scsi_dev_info_list_add(int compatible, char *vendor, char *model,
  **/
 int scsi_dev_info_list_add_keyed(int compatible, const char *vendor,
 				 const char *model, const char *strflags,
-				 int flags, int key)
+				 bflags_t flags, int key)
 {
 	struct scsi_dev_info_list *devinfo;
 	struct scsi_dev_info_list_table *devinfo_table =
@@ -383,10 +383,8 @@ int scsi_dev_info_list_add_keyed(int compatible, const char *vendor,
 			    model, compatible);
 
 	if (strflags)
-		devinfo->flags = simple_strtoul(strflags, NULL, 0);
-	else
-		devinfo->flags = flags;
-
+		flags = (__force bflags_t)simple_strtoul(strflags, NULL, 0);
+	devinfo->flags = flags;
 	devinfo->compatible = compatible;
 
 	if (compatible)
@@ -574,7 +572,7 @@ static int scsi_dev_info_list_add_str(char *dev_list)
  *     settings.  Called during scan time.
  **/
 int scsi_get_device_flags(struct scsi_device *sdev, const unsigned char *vendor,
-			  const unsigned char *model, unsigned int *flags)
+			  const unsigned char *model, bflags_t *flags)
 {
 	return scsi_get_device_flags_keyed(sdev, vendor, model,
 					   SCSI_DEVINFO_GLOBAL, flags);
@@ -598,7 +596,7 @@ int scsi_get_device_flags(struct scsi_device *sdev, const unsigned char *vendor,
 int scsi_get_device_flags_keyed(struct scsi_device *sdev,
 				const unsigned char *vendor,
 				const unsigned char *model,
-				int key, unsigned int *flags)
+				int key, bflags_t *flags)
 {
 	struct scsi_dev_info_list *devinfo;
 	int err;
@@ -620,7 +618,7 @@ int scsi_get_device_flags_keyed(struct scsi_device *sdev,
 	}
 
 	/* except for the global list, where we have an exception */
-	*flags = sdev->sdev_bflags ? : scsi_default_dev_flags;
+	*flags = sdev->sdev_bflags ? : (__force bflags_t)scsi_default_dev_flags;
 	return 0;
 }
 EXPORT_SYMBOL(scsi_get_device_flags_keyed);
drivers/scsi/scsi_priv.h +3 -3
index b82238577b19..e8b12e2ce2e3 100644
@@ -53,14 +53,14 @@ enum {
 extern int scsi_get_device_flags(struct scsi_device *sdev,
 				 const unsigned char *vendor,
 				 const unsigned char *model,
-				 unsigned int *flags);
+				 bflags_t *flags);
 extern int scsi_get_device_flags_keyed(struct scsi_device *sdev,
 				       const unsigned char *vendor,
 				       const unsigned char *model, int key,
-				       unsigned int *flags);
+				       bflags_t *flags);
 extern int scsi_dev_info_list_add_keyed(int compatible, const char *vendor,
 					const char *model, const char *strflags,
-					int flags, int key);
+					bflags_t flags, int key);
 extern int scsi_dev_info_list_del_keyed(char *vendor, char *model, int key);
 extern int scsi_dev_info_add_list(int key, const char *name);
 extern int scsi_dev_info_remove_list(int key);
drivers/scsi/scsi_scan.c +8 -7
index 5b9571f30d82..0a87ad88c6fb 100644
@@ -566,7 +566,7 @@ EXPORT_SYMBOL(scsi_sanitize_inquiry_string);
  *     are copied to the scsi_device any flags value is stored in *@bflags.
  **/
 static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
-			  int result_len, int *bflags)
+			  int result_len, bflags_t *bflags)
 {
 	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
 	int first_inquiry_len, try_inquiry_len, next_inquiry_len;
@@ -770,7 +770,7 @@ static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
  *     SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
  **/
 static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
-		int *bflags, int async)
+		bflags_t *bflags, int async)
 {
 	int ret;
 
@@ -1049,14 +1049,15 @@ static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
  *   - SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
  **/
 static int scsi_probe_and_add_lun(struct scsi_target *starget,
-				  u64 lun, int *bflagsp,
+				  u64 lun, bflags_t *bflagsp,
 				  struct scsi_device **sdevp,
 				  enum scsi_scan_mode rescan,
 				  void *hostdata)
 {
 	struct scsi_device *sdev;
 	unsigned char *result;
-	int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
+	bflags_t bflags;
+	int res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
 
 	/*
@@ -1202,7 +1203,7 @@ static int scsi_probe_and_add_lun(struct scsi_target *starget,
  *     Modifies sdevscan->lun.
  **/
 static void scsi_sequential_lun_scan(struct scsi_target *starget,
-				     int bflags, int scsi_level,
+				     bflags_t bflags, int scsi_level,
 				     enum scsi_scan_mode rescan)
 {
 	uint max_dev_lun;
@@ -1293,7 +1294,7 @@ static void scsi_sequential_lun_scan(struct scsi_target *starget,
  *     0: scan completed (or no memory, so further scanning is futile)
  *     1: could not scan with REPORT LUN
  **/
-static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
+static int scsi_report_lun_scan(struct scsi_target *starget, bflags_t bflags,
 				enum scsi_scan_mode rescan)
 {
 	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
@@ -1539,7 +1540,7 @@ static void __scsi_scan_target(struct device *parent, unsigned int channel,
 		unsigned int id, u64 lun, enum scsi_scan_mode rescan)
 {
 	struct Scsi_Host *shost = dev_to_shost(parent);
-	int bflags = 0;
+	bflags_t bflags = 0;
 	int res;
 	struct scsi_target *starget;
 
drivers/scsi/scsi_sysfs.c +2 -2
index cbc0fe2c5485..1bce5c7305ff 100644
@@ -967,7 +967,7 @@ sdev_show_wwid(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR(wwid, S_IRUGO, sdev_show_wwid, NULL);
 
-#define BLIST_FLAG_NAME(name) [ilog2(BLIST_##name)] = #name
+#define BLIST_FLAG_NAME(name) [ilog2((__force u32)BLIST_##name)] = #name
 static const char *const sdev_bflags_name[] = {
 #include "scsi_devinfo_tbl.c"
 };
@@ -984,7 +984,7 @@ sdev_show_blacklist(struct device *dev, struct device_attribute *attr,
 	for (i = 0; i < sizeof(sdev->sdev_bflags) * BITS_PER_BYTE; i++) {
 		const char *name = NULL;
 
-		if (!(sdev->sdev_bflags & BIT(i)))
+		if (!((__force u32)sdev->sdev_bflags & BIT(i)))
 			continue;
 		if (i < ARRAY_SIZE(sdev_bflags_name) && sdev_bflags_name[i])
 			name = sdev_bflags_name[i];
drivers/scsi/scsi_transport_spi.c +3 -3
index 5f4cea2d07c8..c4ecdbbd458c 100644
@@ -50,14 +50,14 @@
 
 /* Our blacklist flags */
 enum {
-	SPI_BLIST_NOIUS = 0x1,
+	SPI_BLIST_NOIUS = (__force bflags_t)0x1,
 };
 
 /* blacklist table, modelled on scsi_devinfo.c */
 static struct {
 	char *vendor;
 	char *model;
-	unsigned flags;
+	bflags_t flags;
 } spi_static_device_list[] __initdata = {
 	{"HP", "Ultrium 3-SCSI", SPI_BLIST_NOIUS },
 	{"IBM", "ULTRIUM-TD3", SPI_BLIST_NOIUS },
@@ -221,7 +221,7 @@ static int spi_device_configure(struct transport_container *tc,
 {
 	struct scsi_device *sdev = to_scsi_device(dev);
 	struct scsi_target *starget = sdev->sdev_target;
-	unsigned int bflags = 0;
+	bflags_t bflags = 0;
 
 	scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8], &sdev->inquiry[16],
 				    SCSI_DEVINFO_SPI, &bflags);
include/scsi/scsi_device.h +2 -1
index 1fb6ad3c5006..13f0def0dbfc 100644
@@ -7,6 +7,7 @@
 #include <linux/workqueue.h>
 #include <linux/blkdev.h>
 #include <scsi/scsi.h>
+#include <scsi/scsi_types.h>
 #include <linux/atomic.h>
 
 struct device;
@@ -141,7 +142,7 @@ struct scsi_device {
 	unsigned char current_tag;	/* current tag */
 	struct scsi_target      *sdev_target;   /* used only for single_lun */
 
-	unsigned int	sdev_bflags; /* black/white flags as also found in
+	bflags_t	sdev_bflags; /* black/white flags as also found in
 				 * scsi_devinfo.[hc]. For now used only to
 				 * pass settings from slave_alloc to scsi
 				 * core. */
include/scsi/scsi_devinfo.h +27 -25

83 lines not on this page · raw message

include/scsi/scsi_types.h new file +6 -0
new file mode 100644
index 000000000000..3ed29c31270a
@@ -0,0 +1,6 @@
+#ifndef _SCSI_SCSI_TYPES_H_
+#define _SCSI_SCSI_TYPES_H_
+
+typedef __u32 __bitwise bflags_t;
+
+#endif
Signature · 1 line
2.15.0
#5 lore raw

Re: [PATCH 3/3] Introduce a type for device information flags, namely bflags_t

Bart,

Quoted 1 line

+typedef __u32 __bitwise bflags_t;

Since we're already out of flags, let's not perpetuate the u32 limitation.

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering