All of lore.kernel.org
 help / color / mirror / Atom feed
From: Douglas Gilbert <dgilbert@interlog.com>
To: linux-scsi@vger.kernel.org
Cc: martin.petersen@oracle.com, jejb@linux.vnet.ibm.com,
	hare@suse.de, Damien.LeMoal@wdc.com
Subject: [PATCH v4 01/14] scsi_debug: randomize command completion time
Date: Tue, 25 Feb 2020 01:23:38 -0500	[thread overview]
Message-ID: <20200225062351.21267-2-dgilbert@interlog.com> (raw)
In-Reply-To: <20200225062351.21267-1-dgilbert@interlog.com>

Add a new command line option (e.g. random=1) and sysfs attribute
that causes subsequent command completion times to be between the
current command delay setting and 0. A unformily distributed 32
bit, kernel provided integer is used for this purpose.

Since the existing 'delay' whose units are jiffies (typically
milliseconds) and 'ndelay' (units: nanoseconds) options (and sysfs
attributes) span a range greater than 32 bits, some scaling is
required.

The purpose of this patch is to widen the range of testing cases
that are visited in long running tests. Put simply: rarely struct
race conditions are more likely to be found when this facility is
used.

The default is the previous case in which all command completions
were roughly equal to (if not, slightly longer) than the value
given by the 'delay' or 'ndelay' settings (or their defaults).
This option's default is equivalent to setting 'random=0' .

Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Douglas Gilbert <dgilbert@interlog.com>
---
 drivers/scsi/scsi_debug.c | 42 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)

diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
index 44cb054d5e66..18c07e9ace15 100644
--- a/drivers/scsi/scsi_debug.c
+++ b/drivers/scsi/scsi_debug.c
@@ -38,6 +38,7 @@
 #include <linux/hrtimer.h>
 #include <linux/uuid.h>
 #include <linux/t10-pi.h>
+#include <linux/random.h>
 
 #include <net/checksum.h>
 
@@ -125,6 +126,7 @@ static const char *sdebug_version_date = "20190125";
 #define DEF_PHYSBLK_EXP 0
 #define DEF_OPT_XFERLEN_EXP 0
 #define DEF_PTYPE   TYPE_DISK
+#define DEF_RANDOM false
 #define DEF_REMOVABLE false
 #define DEF_SCSI_LEVEL   7    /* INQUIRY, byte2 [6->SPC-4; 7->SPC-5] */
 #define DEF_SECTOR_SIZE 512
@@ -655,6 +657,7 @@ static unsigned int sdebug_unmap_max_blocks = DEF_UNMAP_MAX_BLOCKS;
 static unsigned int sdebug_unmap_max_desc = DEF_UNMAP_MAX_DESC;
 static unsigned int sdebug_write_same_length = DEF_WRITESAME_LENGTH;
 static int sdebug_uuid_ctl = DEF_UUID_CTL;
+static bool sdebug_random = DEF_RANDOM;
 static bool sdebug_removable = DEF_REMOVABLE;
 static bool sdebug_clustering;
 static bool sdebug_host_lock = DEF_HOST_LOCK;
@@ -4354,9 +4357,21 @@ static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,
 		ktime_t kt;
 
 		if (delta_jiff > 0) {
-			kt = ns_to_ktime((u64)delta_jiff * (NSEC_PER_SEC / HZ));
-		} else
-			kt = ndelay;
+			u64 ns = jiffies_to_nsecs(delta_jiff);
+
+			if (sdebug_random && ns < U32_MAX) {
+				ns = prandom_u32_max((u32)ns);
+			} else if (sdebug_random) {
+				ns >>= 12;	/* scale to 4 usec precision */
+				if (ns < U32_MAX)	/* over 4 hours max */
+					ns = prandom_u32_max((u32)ns);
+				ns <<= 12;
+			}
+			kt = ns_to_ktime(ns);
+		} else {	/* ndelay has a 4.2 second max */
+			kt = sdebug_random ? prandom_u32_max((u32)ndelay) :
+					     (u32)ndelay;
+		}
 		if (!sd_dp->init_hrt) {
 			sd_dp->init_hrt = true;
 			sqcp->sd_dp = sd_dp;
@@ -4451,6 +4466,7 @@ module_param_named(opts, sdebug_opts, int, S_IRUGO | S_IWUSR);
 module_param_named(physblk_exp, sdebug_physblk_exp, int, S_IRUGO);
 module_param_named(opt_xferlen_exp, sdebug_opt_xferlen_exp, int, S_IRUGO);
 module_param_named(ptype, sdebug_ptype, int, S_IRUGO | S_IWUSR);
+module_param_named(random, sdebug_random, bool, S_IRUGO | S_IWUSR);
 module_param_named(removable, sdebug_removable, bool, S_IRUGO | S_IWUSR);
 module_param_named(scsi_level, sdebug_scsi_level, int, S_IRUGO);
 module_param_named(sector_size, sdebug_sector_size, int, S_IRUGO);
@@ -4511,6 +4527,7 @@ MODULE_PARM_DESC(opts, "1->noise, 2->medium_err, 4->timeout, 8->recovered_err...
 MODULE_PARM_DESC(physblk_exp, "physical block exponent (def=0)");
 MODULE_PARM_DESC(opt_xferlen_exp, "optimal transfer length granularity exponent (def=physblk_exp)");
 MODULE_PARM_DESC(ptype, "SCSI peripheral type(def=0[disk])");
+MODULE_PARM_DESC(random, "If set, uniformly randomize command duration between 0 and delay_in_ns");
 MODULE_PARM_DESC(removable, "claim to have removable media (def=0)");
 MODULE_PARM_DESC(scsi_level, "SCSI level to simulate(def=7[SPC-5])");
 MODULE_PARM_DESC(sector_size, "logical block size in bytes (def=512)");
@@ -5101,6 +5118,24 @@ static ssize_t map_show(struct device_driver *ddp, char *buf)
 }
 static DRIVER_ATTR_RO(map);
 
+static ssize_t random_show(struct device_driver *ddp, char *buf)
+{
+	return scnprintf(buf, PAGE_SIZE, "%d\n", (int)sdebug_random);
+}
+
+static ssize_t random_store(struct device_driver *ddp, const char *buf,
+			    size_t count)
+{
+	int n;
+
+	if (count > 0 && kstrtoint(buf, 10, &n) == 0 && n >= 0) {
+		sdebug_random = (n > 0);
+		return count;
+	}
+	return -EINVAL;
+}
+static DRIVER_ATTR_RW(random);
+
 static ssize_t removable_show(struct device_driver *ddp, char *buf)
 {
 	return scnprintf(buf, PAGE_SIZE, "%d\n", sdebug_removable ? 1 : 0);
@@ -5211,6 +5246,7 @@ static struct attribute *sdebug_drv_attrs[] = {
 	&driver_attr_guard.attr,
 	&driver_attr_ato.attr,
 	&driver_attr_map.attr,
+	&driver_attr_random.attr,
 	&driver_attr_removable.attr,
 	&driver_attr_host_lock.attr,
 	&driver_attr_ndelay.attr,
-- 
2.25.1


  reply	other threads:[~2020-02-25  6:24 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-25  6:23 [PATCH v4 00/14] scsi_debug: host managed ZBC + doublestore Douglas Gilbert
2020-02-25  6:23 ` Douglas Gilbert [this message]
2020-04-13 22:24   ` [PATCH v4 01/14] scsi_debug: randomize command completion time Martin K. Petersen
2020-02-25  6:23 ` [PATCH v4 02/14] scsi_debug: add doublestore option Douglas Gilbert
2020-04-13 22:36   ` Martin K. Petersen
2020-02-25  6:23 ` [PATCH v4 03/14] scsi_debug: implement verify(10), add verify(16) Douglas Gilbert
2020-04-13 22:48   ` Martin K. Petersen
2020-02-25  6:23 ` [PATCH v4 04/14] scsi_debug: weaken rwlock around ramdisk access Douglas Gilbert
2020-02-25  6:23 ` [PATCH v4 05/14] scsi_debug: improve command duration calculation Douglas Gilbert
2020-04-13 22:50   ` Martin K. Petersen
2020-02-25  6:23 ` [PATCH v4 06/14] scsi_debug: implement pre-fetch commands Douglas Gilbert
2020-04-13 22:57   ` Martin K. Petersen
2020-04-19 18:01     ` Douglas Gilbert
2020-04-19 18:22       ` Julian Wiedmann
2020-04-19 21:53         ` Douglas Gilbert
2020-02-25  6:23 ` [PATCH v4 07/14] scsi_debug: expand zbc support Douglas Gilbert
2020-04-13 23:00   ` Martin K. Petersen
2020-04-13 23:05     ` Damien Le Moal
2020-04-13 23:06       ` Damien Le Moal
2020-04-14  1:55         ` Martin K. Petersen
2020-04-14  5:38           ` Damien Le Moal
2020-02-25  6:23 ` [PATCH v4 08/14] scsi_debug: add zone commands Douglas Gilbert
2020-02-25  6:23 ` [PATCH v4 09/14] scsi_debug: add zbc parameter Douglas Gilbert
2020-04-15  2:10   ` Martin K. Petersen
2020-02-25  6:23 ` [PATCH v4 10/14] scsi_debug: re-arrange parameters alphabetically Douglas Gilbert
2020-02-25  6:23 ` [PATCH v4 11/14] scsi_debug: zone_max_open module parameter Douglas Gilbert
2020-02-25  6:23 ` [PATCH v4 12/14] scsi_debug: zone_nr_conv " Douglas Gilbert
2020-02-25  6:23 ` [PATCH v4 13/14] scsi_debug: zone_size_mb " Douglas Gilbert
2020-02-25  6:23 ` [PATCH v4 14/14] scsi_debug: bump to version 1.89 Douglas Gilbert
2020-04-13 23:19 ` [PATCH v4 00/14] scsi_debug: host managed ZBC + doublestore Martin K. Petersen
2020-04-14  2:49   ` Douglas Gilbert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200225062351.21267-2-dgilbert@interlog.com \
    --to=dgilbert@interlog.com \
    --cc=Damien.LeMoal@wdc.com \
    --cc=hare@suse.de \
    --cc=jejb@linux.vnet.ibm.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.