scsi_debug: collection of additions
cover scsi_debug: collection of additions
The first patch is to address possible modprobe rmmod races.
The second patch adds READ_ONCE and WRITE_ONCE on several high frequency variables.
See the headers of patches 3 and 4.
In patch 5 use different asc/ascq codes to distiguish between and newly added LU and a reset on an existing LU.
In patch 6 add a no_rwlock module option to bypass the read-write locks around user data reads and writes.
Patch 7 adds sdeb_sgl_copy_sgl, sdeb_sgl_compare_sgl_idx, sdeb_sgl_compare_sgl and sdeb_sgl_memset functions for performing sgl t0 sgl copy and compare operations currently unsupported by scatterlist.h . This patch was originally put forward as an enhancement of scatterlist.h but failed to gain traction. So that patch is redirected to this driver with the "sdeb_" added to each function. Compiling the driver after this patch will result in warnings that those newly introduced static functions are not used. Since LLDs receive and send data "up" the stack via sgl_s, using sgl_s as the storage medium for this driver bypasses one level of conversions.
Patch 8 uses the new functions introduced in patch 7 to change the internal store(s) used the this driver into sgl_s rather than big vmalloc() allocations. The biggest improvement is with the VERIFY(BYTCHK=1) performance that went from about 20% slower than a copy operation to 50% faster (up to 15 GB/s on my equipment). Before this patch VERIFY(BYTCHK=1) needed a temporary, potentially large, buffer for the incoming data-out transfer. The COMPARE AND WRITE command also benefits from stores made of sgl_s, and in addition it needs to know the index of the first mismatched byte (if any).
Finally some issues have arisen in smartmontools with SCSI log subpages. To aid is fixing this, the last patch adds the driver's first log subpage (Environmental reporting).
This patchset is based on lk 5.16.0-rc7 rather than MKP's repository (based on rc1). Fixes have been added to this driver between rc1 and rc7 that would break a single patchset. So the most recent rc was chosen.
Douglas Gilbert (9):
scsi_debug: address races following module load
scsi_debug: strengthen defer_t accesses
scsi_debug: use task set full more
scsi_debug: refine sdebug_blk_mq_poll
scsi_debug: divide power on reset unit attention
scsi_debug: add no_rwlock parameter
scsi_debug: add sdeb_sgl_copy_sgl and friends
scsi_debug: change store from vmalloc to sgl
scsi_debug: add environmental reporting log subpage
drivers/scsi/Kconfig | 3 +- drivers/scsi/scsi_debug.c | 1078 +++++++++++++++++++++++++++---------- 2 files changed, 800 insertions(+), 281 deletions(-)
Signature · 1 line
2.25.1
PATCH 2/9 scsi_debug: strengthen defer_t accesses
Use READ_ONCE() and WRITE_ONCE() macros when accessing the sdebug_defer::defer_t value.
--- drivers/scsi/scsi_debug.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
drivers/scsi/scsi_debug.c +11 -11
@@ -4782,7 +4782,7 @@ static void sdebug_q_cmd_complete(struct sdebug_defer *sd_dp)return ; }spin_lock_irqsave (&sqp->qc_lock, iflags); - sd_dp->defer_t =SDEB_DEFER_NONE ; +WRITE_ONCE (sd_dp->defer_t,SDEB_DEFER_NONE ); sqcp = &sqp->qc_arr[qc_idx]; scp = sqcp->a_cmnd;if (unlikely (scp ==NULL )) {
@@ -5103,8 +5103,8 @@ static bool stop_queued_cmnd(struct scsi_cmnd *cmnd) sqcp->a_cmnd =NULL ; sd_dp = sqcp->sd_dp;if (sd_dp) { - l_defer_t = sd_dp->defer_t; - sd_dp->defer_t =SDEB_DEFER_NONE ; + l_defer_t =READ_ONCE (sd_dp->defer_t); +WRITE_ONCE (sd_dp->defer_t,SDEB_DEFER_NONE ); }else l_defer_t =SDEB_DEFER_NONE ;spin_unlock_irqrestore (&sqp->qc_lock, iflags);
@@ -5145,8 +5145,8 @@ static void stop_all_queued(bool done_with_no_conn) sqcp->a_cmnd =NULL ; sd_dp = sqcp->sd_dp;if (sd_dp) { - l_defer_t = sd_dp->defer_t; - sd_dp->defer_t =SDEB_DEFER_NONE ; + l_defer_t =READ_ONCE (sd_dp->defer_t); +WRITE_ONCE (sd_dp->defer_t,SDEB_DEFER_NONE ); }else l_defer_t =SDEB_DEFER_NONE ;spin_unlock_irqrestore (&sqp->qc_lock, iflags);
@@ -5627,7 +5627,7 @@ static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, sd_dp->sqa_idx = sqp - sdebug_q_arr; sd_dp->qc_idx = k; } - sd_dp->defer_t =SDEB_DEFER_POLL ; +WRITE_ONCE (sd_dp->defer_t,SDEB_DEFER_POLL );spin_unlock_irqrestore (&sqp->qc_lock, iflags); }else {if (!sd_dp->init_hrt) {
@@ -5639,7 +5639,7 @@ static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, sd_dp->sqa_idx = sqp - sdebug_q_arr; sd_dp->qc_idx = k; } - sd_dp->defer_t =SDEB_DEFER_HRT ; +WRITE_ONCE (sd_dp->defer_t,SDEB_DEFER_HRT );/* schedule the invocation of scsi_done() for a later time */ hrtimer_start (&sd_dp->hrt, kt,HRTIMER_MODE_REL_PINNED ); }
@@ -5658,7 +5658,7 @@ static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, sd_dp->sqa_idx = sqp - sdebug_q_arr; sd_dp->qc_idx = k; } - sd_dp->defer_t =SDEB_DEFER_POLL ; +WRITE_ONCE (sd_dp->defer_t,SDEB_DEFER_POLL );spin_unlock_irqrestore (&sqp->qc_lock, iflags); }else {if (!sd_dp->init_wq) {
@@ -5668,7 +5668,7 @@ static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, sd_dp->qc_idx = k;INIT_WORK (&sd_dp->ew.work, sdebug_q_cmd_wq_complete); } - sd_dp->defer_t =SDEB_DEFER_WQ ; +WRITE_ONCE (sd_dp->defer_t,SDEB_DEFER_WQ );schedule_work (&sd_dp->ew.work); }if (sdebug_statistics)
@@ -7436,7 +7436,7 @@ static int sdebug_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num) queue_num, qc_idx, __func__);break ; } -if (sd_dp->defer_t ==SDEB_DEFER_POLL ) { +if (READ_ONCE (sd_dp->defer_t) ==SDEB_DEFER_POLL ) {if (kt_from_boot < sd_dp->cmpl_ts)continue ;
@@ -7470,7 +7470,7 @@ static int sdebug_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)else atomic_set (&retired_max_queue, k +1 ); } - sd_dp->defer_t =SDEB_DEFER_NONE ; +WRITE_ONCE (sd_dp->defer_t,SDEB_DEFER_NONE );spin_unlock_irqrestore (&sqp->qc_lock, iflags);scsi_done (scp);/* callback to mid level */ spin_lock_irqsave (&sqp->qc_lock, iflags);
Signature · 1 line
2.25.1
PATCH 5/9 scsi_debug: divide power on reset unit attention
In reply to Douglas GilbertTo distinguish between resets sent by the SCSI mid-level error handling and newly introduced devices (LUs), this Unit Attention:
power on, reset, or bus reset occurred [0x29,0x0]
has been subdivided into that UA for the reset case and this new UA:
power on occurred [0x29,0x1]
for the new device (LU) case. This makes debug a little easier to follow when it is turned on (e.g. 'echo 0x1 > opts').
Bump driver version number.
--- drivers/scsi/scsi_debug.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/scsi/scsi_debug.c +19 -11
@@ -7,7 +7,7 @@* anything out of the ordinary is seen. * ^^^^^^^^^^^^^^^^^^^^^^^ Original ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * -* Copyright (C) 2001 - 2020 Douglas Gilbert +* Copyright (C) 2001 - 2021 Douglas Gilbert * * For documentation see http://sg.danny.cz/sg/scsi_debug.html */
@@ -61,8 +61,8 @@#include "scsi_logging.h" /* make sure inq_product_rev string corresponds to this version */ -#define SDEBUG_VERSION "0190"/* format to fit INQUIRY revision field */ -static const char *sdebug_version_date ="20200710" ; +#define SDEBUG_VERSION "0191"/* format to fit INQUIRY revision field */ +static const char *sdebug_version_date ="20210520" ;#define MY_NAME "scsi_debug"
@@ -84,6 +84,7 @@ static const char *sdebug_version_date = "20200710";#define INSUFF_RES_ASC 0x55#define INSUFF_RES_ASCQ 0x3#define POWER_ON_RESET_ASCQ 0x0 +#define POWER_ON_OCCURRED_ASCQ 0x1#define BUS_RESET_ASCQ 0x2/* scsi bus reset occurred */ #define MODE_CHANGED_ASCQ 0x1/* mode parameters changed */ #define CAPACITY_CHANGED_ASCQ 0x9
@@ -197,13 +198,14 @@ static const char *sdebug_version_date = "20200710";* priority. The UA numbers should be a sequence starting from 0 with * SDEBUG_NUM_UAS being 1 higher than the highest numbered UA. */ #define SDEBUG_UA_POR 0/* Power on, reset, or bus device reset */ -#define SDEBUG_UA_BUS_RESET 1 -#define SDEBUG_UA_MODE_CHANGED 2 -#define SDEBUG_UA_CAPACITY_CHANGED 3 -#define SDEBUG_UA_LUNS_CHANGED 4 -#define SDEBUG_UA_MICROCODE_CHANGED 5/* simulate firmware change */ -#define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 6 -#define SDEBUG_NUM_UAS 7 +#define SDEBUG_UA_POOCCUR 1/* Power on occurred */ +#define SDEBUG_UA_BUS_RESET 2 +#define SDEBUG_UA_MODE_CHANGED 3 +#define SDEBUG_UA_CAPACITY_CHANGED 4 +#define SDEBUG_UA_LUNS_CHANGED 5 +#define SDEBUG_UA_MICROCODE_CHANGED 6/* simulate firmware change */ +#define SDEBUG_UA_MICROCODE_CHANGED_WO_RESET 7 +#define SDEBUG_NUM_UAS 8/* when 1==SDEBUG_OPT_MEDIUM_ERR, a medium error is simulated at this * sector on read commands: */
@@ -1086,6 +1088,12 @@ static int make_ua(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)if (sdebug_verbose) cp ="power on reset" ;break ; +case SDEBUG_UA_POOCCUR : +mk_sense_buffer (scp,UNIT_ATTENTION ,UA_RESET_ASC , +POWER_ON_OCCURRED_ASCQ ); +if (sdebug_verbose) + cp ="power on occurred" ; +break ;case SDEBUG_UA_BUS_RESET :mk_sense_buffer (scp,UNIT_ATTENTION ,UA_RESET_ASC ,BUS_RESET_ASCQ );
@@ -5007,7 +5015,7 @@ static struct sdebug_dev_info *find_build_dev_info(struct scsi_device *sdev) open_devip->lun = sdev->lun; open_devip->sdbg_host = sdbg_host;atomic_set (&open_devip->num_in_q,0 ); -set_bit (SDEBUG_UA_POR , open_devip->uas_bm); +set_bit (SDEBUG_UA_POOCCUR , open_devip->uas_bm); open_devip->used = true;return open_devip; }
Signature · 1 line
2.25.1
PATCH 1/9 scsi_debug: address races following module load
In reply to Douglas GilbertWhen scsi_debug is loaded as a module with many (simulated) hosts, targets, and devices (LUs), modprobe can take a long time to return. Only a small amount of this time is spent in the scsi_debug_init(); the rest is other parts of the kernel reacting to to the appearance of new storage devices. As soon as scsi_debug_init() has completed the user space may call 'rmmod scsi_debug' and this was found to cause race problems as outlined here:
https://bugzilla.kernel.org/show_bug.cgi?id=212337
To reliably generate this race a sysfs parameter called rm_all_hosts was added and the code was strengthened in this area. The main change was to make the count of scsi_debug hosts present an atomic. Then it was found that the handling of the existing add_host parameter needed the same strengthening. Further:
'echo -9999 > /sys/bus/pseudo/drivers/scsi_debug/add_host
has the same effect as rm_all_hosts so rm_all_hosts was not needed.
To inhibit a race between two invocations of writes to add_host, a mutex was added. Also address a possible race when rmmod is called but LUs are still being added.
The logic to remove (all) hosts is rather crude: it works backwards down a linked lists of hosts. Any pending requests are terminated with DID_NO_CONNECT as are any new requests. In the case where not all hosts are being removed, the ones that remain may have lost requests as just outlined. The lowest numbered host (id) hosts will remain.
This patch was developed several months ago after discussions with Luis Chamberlain. It may have been overtaken by more recent work by Luis.
--- drivers/scsi/scsi_debug.c | 197 ++++++++++++++++++++++++++++---------- 1 file changed, 146 insertions(+), 51 deletions(-)
drivers/scsi/scsi_debug.c +146 -51
@@ -33,6 +33,7 @@#include <linux/blkdev.h> #include <linux/crc-t10dif.h> #include <linux/spinlock.h> +#include <linux/mutex.h> #include <linux/interrupt.h> #include <linux/atomic.h> #include <linux/hrtimer.h>
@@ -730,7 +731,9 @@ static const struct opcode_info_t opcode_info_arr[SDEB_I_LAST_ELEM_P1 + 1] = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }, }; -static int sdebug_num_hosts; +static atomic_t sdebug_num_hosts; +static DEFINE_MUTEX (add_host_mutex); +static int sdebug_add_host =DEF_NUM_HOST ;/* in sysfs this is relative */ static int sdebug_ato =DEF_ATO ;static int sdebug_cdb_len =DEF_CDB_LEN ;
@@ -777,6 +780,7 @@ static int sdebug_uuid_ctl = DEF_UUID_CTL;static bool sdebug_random =DEF_RANDOM ;static bool sdebug_per_host_store =DEF_PER_HOST_STORE ;static bool sdebug_removable =DEF_REMOVABLE ; +static bool sdebug_deflect_incoming;static bool sdebug_clustering;static bool sdebug_host_lock =DEF_HOST_LOCK ;static bool sdebug_strict =DEF_STRICT ;
@@ -5026,6 +5030,10 @@ static int scsi_debug_slave_configure(struct scsi_device *sdp) sdp->host->host_no, sdp->channel, sdp->id, sdp->lun);if (sdp->host->max_cmd_len !=SDEBUG_MAX_CMD_LEN ) sdp->host->max_cmd_len =SDEBUG_MAX_CMD_LEN ; +if (READ_ONCE (sdebug_deflect_incoming)) { +pr_info ("Exit early due to deflect_incoming\n" ); +return 1 ; + }if (devip ==NULL ) { devip =find_build_dev_info (sdp);if (devip ==NULL )
@@ -5111,7 +5119,7 @@ static bool stop_queued_cmnd(struct scsi_cmnd *cmnd) }/* Deletes (stops) timers or work queues of all queued commands */ -static void stop_all_queued (void ) +static void stop_all_queued (bool done_with_no_conn) {unsigned long iflags;int j, k;
@@ -5120,13 +5128,15 @@ static void stop_all_queued(void)struct sdebug_queued_cmd *sqcp;struct sdebug_dev_info *devip;struct sdebug_defer *sd_dp; +struct scsi_cmnd *scp;for (j =0 , sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {spin_lock_irqsave (&sqp->qc_lock, iflags);for (k =0 ; k <SDEBUG_CANQUEUE ; ++k) {if (test_bit (k, sqp->in_use_bm)) { sqcp = &sqp->qc_arr[k]; -if (sqcp->a_cmnd ==NULL ) + scp = sqcp->a_cmnd; +if (!scp)continue ; devip = (struct sdebug_dev_info *) sqcp->a_cmnd->device->hostdata;
@@ -5141,6 +5151,10 @@ static void stop_all_queued(void) l_defer_t =SDEB_DEFER_NONE ;spin_unlock_irqrestore (&sqp->qc_lock, iflags);stop_qc_helper (sd_dp, l_defer_t); +if (done_with_no_conn && l_defer_t !=SDEB_DEFER_NONE ) { + scp->result =DID_NO_CONNECT <<16 ; +scsi_done (scp); + }clear_bit (k, sqp->in_use_bm);spin_lock_irqsave (&sqp->qc_lock, iflags); }
@@ -5283,7 +5297,7 @@ static int scsi_debug_host_reset(struct scsi_cmnd *SCpnt) } }spin_unlock (&sdebug_host_list_lock); -stop_all_queued (); +stop_all_queued (false);if (SDEBUG_OPT_RESET_NOISE & sdebug_opts)sdev_printk (KERN_INFO , SCpnt->device,"%s: %d device(s) found\n" , __func__, k);
@@ -5343,13 +5357,50 @@ static void sdebug_build_parts(unsigned char *ramp, unsigned long store_size) } } -static void block_unblock_all_queues (bool block) +static void sdeb_block_all_queues (void ) +{ +int j; +struct sdebug_queue *sqp; + +for (j =0 , sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) +atomic_set (&sqp->blocked, (int )true); +} + +static void sdeb_unblock_all_queues (void ) {int j;struct sdebug_queue *sqp;for (j =0 , sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) -atomic_set (&sqp->blocked, (int )block); +atomic_set (&sqp->blocked, (int )false); +} + +static void +sdeb_add_n_hosts (int num_hosts) +{ +if (num_hosts <1 ) +return ; +do { +bool found; +unsigned long idx; +struct sdeb_store_info *sip; +bool want_phs = (sdebug_fake_rw ==0 ) && sdebug_per_host_store; + + found = false; +if (want_phs) { +xa_for_each_marked (per_store_ap, idx, sip,SDEB_XA_NOT_IN_USE ) { + sdeb_most_recent_idx = (int )idx; + found = true; +break ; + } +if (found)/* re-use case */ +sdebug_add_host_helper ((int )idx); +else +sdebug_do_add_host (true/* make new store */ ); + }else { +sdebug_do_add_host (false); + } + }while (--num_hosts); }/* Adjust (by rounding down) the sdebug_cmnd_count so abs(every_nth)-1
@@ -5362,10 +5413,10 @@ static void tweak_cmnd_count(void) modulo =abs (sdebug_every_nth);if (modulo <2 )return ; -block_unblock_all_queues (true); +sdeb_block_all_queues (); count =atomic_read (&sdebug_cmnd_count);atomic_set (&sdebug_cmnd_count, (count / modulo) * modulo); -block_unblock_all_queues (false); +sdeb_unblock_all_queues (); }static void clear_queue_stats (void )
@@ -5383,6 +5434,15 @@ static bool inject_on_this_cmd(void)return (atomic_read (&sdebug_cmnd_count) %abs (sdebug_every_nth)) ==0 ; } +static int process_deflect_incoming (struct scsi_cmnd *scp) +{ +u8 opcode = scp->cmnd[0 ]; + +if (opcode ==SYNCHRONIZE_CACHE || opcode ==SYNCHRONIZE_CACHE_16 ) +return 0 ; +return DID_NO_CONNECT <<16 ; +} +#define INCLUSIVE_TIMING_MAX_NS 1000000/* 1 millisecond */ /* Complete the processing of the thread that queued a SCSI command to this
@@ -5392,8 +5452,7 @@ static bool inject_on_this_cmd(void)*/ static int schedule_resp (struct scsi_cmnd *cmnd,struct sdebug_dev_info *devip,int scsi_result, -int (*pfp)(struct scsi_cmnd *, -struct sdebug_dev_info *), +int (*pfp)(struct scsi_cmnd *,struct sdebug_dev_info *),int delta_jiff,int ndelay) {bool new_sd_dp;
@@ -5414,13 +5473,27 @@ static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, } sdp = cmnd->device; -if (delta_jiff ==0 ) +if (delta_jiff ==0 ) { + sqp =get_queue (cmnd); +if (atomic_read (&sqp->blocked)) { +if (READ_ONCE (sdebug_deflect_incoming)) +return process_deflect_incoming (cmnd); +else +return SCSI_MLQUEUE_HOST_BUSY ; + } goto respond_in_thread; + } sqp =get_queue (cmnd);spin_lock_irqsave (&sqp->qc_lock, iflags);if (unlikely (atomic_read (&sqp->blocked))) {spin_unlock_irqrestore (&sqp->qc_lock, iflags); +if (READ_ONCE (sdebug_deflect_incoming)) { + scsi_result =process_deflect_incoming (cmnd); + goto respond_in_thread; + } +if (sdebug_verbose) +pr_info ("blocked --> SCSI_MLQUEUE_HOST_BUSY\n" );return SCSI_MLQUEUE_HOST_BUSY ; } num_in_q =atomic_read (&devip->num_in_q);
@@ -5616,8 +5689,12 @@ static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip, respond_in_thread:/* call back to mid-layer using invocation thread */ cmnd->result = pfp !=NULL ?pfp (cmnd, devip) :0 ; cmnd->result &= ~SDEG_RES_IMMED_MASK ; -if (cmnd->result ==0 && scsi_result !=0 ) +if (cmnd->result ==0 && scsi_result !=0 ) { cmnd->result = scsi_result; +if (sdebug_verbose) +pr_info ("respond_in_thread: tag=0x%x, scp->result=0x%x\n" , +blk_mq_unique_tag (scsi_cmd_to_rq (cmnd)), scsi_result); + }scsi_done (cmnd);return 0 ; }
@@ -5900,7 +5977,7 @@ static ssize_t delay_store(struct device_driver *ddp, const char *buf,int j, k;struct sdebug_queue *sqp; -block_unblock_all_queues (true); +sdeb_block_all_queues ();for (j =0 , sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { k = find_first_bit(sqp->in_use_bm,
@@ -5914,7 +5991,7 @@ static ssize_t delay_store(struct device_driver *ddp, const char *buf, sdebug_jdelay = jdelay; sdebug_ndelay =0 ; } -block_unblock_all_queues (false); +sdeb_unblock_all_queues (); }return res; }
@@ -5940,7 +6017,7 @@ static ssize_t ndelay_store(struct device_driver *ddp, const char *buf,int j, k;struct sdebug_queue *sqp; -block_unblock_all_queues (true); +sdeb_block_all_queues ();for (j =0 , sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) { k = find_first_bit(sqp->in_use_bm,
@@ -5955,7 +6032,7 @@ static ssize_t ndelay_store(struct device_driver *ddp, const char *buf, sdebug_jdelay = ndelay ?JDELAY_OVERRIDDEN :DEF_JDELAY ; } -block_unblock_all_queues (false); +sdeb_unblock_all_queues (); }return res; }
@@ -6269,7 +6346,7 @@ static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,if ((count >0 ) && (1 ==sscanf (buf,"%d" , &n)) && (n >0 ) && (n <=SDEBUG_CANQUEUE ) && (sdebug_host_max_queue ==0 )) { -block_unblock_all_queues (true); +sdeb_block_all_queues (); k =0 ;for (j =0 , sqp = sdebug_q_arr; j < submit_queues; ++j, ++sqp) {
@@ -6284,7 +6361,7 @@ static ssize_t max_queue_store(struct device_driver *ddp, const char *buf,atomic_set (&retired_max_queue, k +1 ); else atomic_set(&retired_max_queue, 0); -block_unblock_all_queues (false); +sdeb_unblock_all_queues ();return count; }return -EINVAL ;
@@ -6356,43 +6433,48 @@ static DRIVER_ATTR_RW(virtual_gb);static ssize_t add_host_show (struct device_driver *ddp,char *buf) {/* absolute number of hosts currently active is what is shown */ -return scnprintf (buf,PAGE_SIZE ,"%d\n" , sdebug_num_hosts); +return scnprintf (buf,PAGE_SIZE ,"%d\n" ,atomic_read (&sdebug_num_hosts)); } +/* +* Accept positive and negative values. Hex values (only positive) may be prefixed by '0x'. +* To remove all hosts use a large negative number (e.g. -9999). The value 0 does nothing. +* Returns -EBUSY if another add_host sysfs invocation is active. +*/ static ssize_t add_host_store (struct device_driver *ddp,const char *buf,size_t count) { -bool found; -unsigned long idx; -struct sdeb_store_info *sip; -bool want_phs = (sdebug_fake_rw ==0 ) && sdebug_per_host_store;int delta_hosts; -if (sscanf (buf,"%d" , &delta_hosts) !=1 ) +if (count ==0 ||kstrtoint (buf,0 , &delta_hosts))return -EINVAL ; +if (sdebug_verbose) +pr_info ("prior num_hosts=%d, num_to_add=%d\n" , +atomic_read (&sdebug_num_hosts), delta_hosts); +if (delta_hosts ==0 ) +return count; +if (mutex_trylock (&add_host_mutex) ==0 ) +return -EBUSY ;if (delta_hosts >0 ) { -do { - found = false; -if (want_phs) { -xa_for_each_marked (per_store_ap, idx, sip, -SDEB_XA_NOT_IN_USE ) { - sdeb_most_recent_idx = (int )idx; - found = true; -break ; - } -if (found)/* re-use case */ -sdebug_add_host_helper ((int )idx); -else -sdebug_do_add_host (true); - }else { -sdebug_do_add_host (false); - } - }while (--delta_hosts); +sdeb_add_n_hosts (delta_hosts); }else if (delta_hosts <0 ) { +WRITE_ONCE (sdebug_deflect_incoming, true); +sdeb_block_all_queues (); +if (delta_hosts >=atomic_read (&sdebug_num_hosts)) +stop_all_queued (true);do { +if (atomic_read (&sdebug_num_hosts) <1 ) { +free_all_queued (); +break ; + }sdebug_do_remove_host (false); }while (++delta_hosts); +sdeb_unblock_all_queues (); +WRITE_ONCE (sdebug_deflect_incoming, false); } +mutex_unlock (&add_host_mutex); +if (sdebug_verbose) +pr_info ("post num_hosts=%d\n" ,atomic_read (&sdebug_num_hosts));return count; }static DRIVER_ATTR_RW (add_host);
@@ -6902,6 +6984,10 @@ static int __init scsi_debug_init(void) sdebug_add_host =0 ;for (k =0 ; k < hosts_to_add; k++) { +if (READ_ONCE (sdebug_deflect_incoming)) { +pr_info ("exit early as sdebug_deflect_incoming is set\n" ); +return 0 ; + }if (want_store && k ==0 ) { ret =sdebug_add_host_helper (idx);if (ret <0 ) {
@@ -6919,8 +7005,12 @@ static int __init scsi_debug_init(void) } }if (sdebug_verbose) -pr_info ("built %d host(s)\n" , sdebug_num_hosts); +pr_info ("built %d host(s)\n" ,atomic_read (&sdebug_num_hosts)); +/* +* Even though all the hosts have been established, due to async device (LU) scanning +* by the scsi mid-level, there may still be devices (LUs) being set up. +*/ return 0 ; bus_unreg:
@@ -6936,12 +7026,17 @@ static int __init scsi_debug_init(void) static void __exit scsi_debug_exit(void) { -int k = sdebug_num_hosts; +int k; -stop_all_queued (); -for (; k; k--) +/* Possible race with LUs still being set up; stop them asap */ +sdeb_block_all_queues (); +WRITE_ONCE (sdebug_deflect_incoming, true); +stop_all_queued (false); +for (k =0 ;atomic_read (&sdebug_num_hosts) >0 ; k++)sdebug_do_remove_host (true);free_all_queued (); +if (sdebug_verbose) +pr_info ("removed %d hosts\n" , k);driver_unregister (&sdebug_driverfs_driver);bus_unregister (&pseudo_lld_bus);root_device_unregister (pseudo_primary);
@@ -7111,13 +7206,13 @@ static int sdebug_add_host_helper(int per_host_idx) sdbg_host->dev.bus = &pseudo_lld_bus; sdbg_host->dev.parent = pseudo_primary; sdbg_host->dev.release = &sdebug_release_adapter; -dev_set_name (&sdbg_host->dev,"adapter%d" , sdebug_num_hosts); +dev_set_name (&sdbg_host->dev,"adapter%d" ,atomic_read (&sdebug_num_hosts)); error =device_register (&sdbg_host->dev);if (error) goto clean; - ++sdebug_num_hosts; +atomic_inc (&sdebug_num_hosts);return 0 ; clean:
@@ -7181,7 +7276,7 @@ static void sdebug_do_remove_host(bool the_end)return ;device_unregister (&sdbg_host->dev); - --sdebug_num_hosts; +atomic_dec (&sdebug_num_hosts); }static int sdebug_change_qdepth (struct scsi_device *sdev,int qdepth)
@@ -7189,10 +7284,10 @@ static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth)int num_in_q =0 ;struct sdebug_dev_info *devip; -block_unblock_all_queues (true); +sdeb_block_all_queues (); devip = (struct sdebug_dev_info *)sdev->hostdata;if (NULL == devip) { -block_unblock_all_queues (false); +sdeb_unblock_all_queues ();return -ENODEV ; } num_in_q =atomic_read (&devip->num_in_q);
@@ -7211,7 +7306,7 @@ static int sdebug_change_qdepth(struct scsi_device *sdev, int qdepth)sdev_printk (KERN_INFO , sdev,"%s: qdepth=%d, num_in_q=%d\n" , __func__, qdepth, num_in_q); } -block_unblock_all_queues (false); +sdeb_unblock_all_queues ();return sdev->queue_depth; }
Signature · 1 line
2.25.1
PATCH 6/9 scsi_debug: add no_rwlock parameter
In reply to Douglas GilbertBy default, this driver places a read lock around all user data fetches and a write lock around all user data modifying operations (e.g. WRITE commands). These locks have "per store" granularity. Other drivers that have a similar function (e.g. null_blk) do not take this data integrity step and run significantly faster in some tests.
In the common case of a (simulated) device to device copy (e.g. what dd and its variants do) there should be no need for locks around data accesses. So add the driver and sysfs parameter no_rwlock which is boolean and when set does what its name suggests. The default is false for backward comaptibility.
--- drivers/scsi/scsi_debug.c | 152 +++++++++++++++++++++++++------------- 1 file changed, 102 insertions(+), 50 deletions(-)
drivers/scsi/scsi_debug.c +102 -50
@@ -787,6 +787,7 @@ static bool sdebug_clustering;static bool sdebug_host_lock =DEF_HOST_LOCK ;static bool sdebug_strict =DEF_STRICT ;static bool sdebug_any_injecting_opt; +static bool sdebug_no_rwlock;static bool sdebug_verbose;static bool have_dif_prot;static bool write_since_sync;
@@ -3130,6 +3131,50 @@ static int prot_verify_read(struct scsi_cmnd *scp, sector_t start_sec,return ret; } +static inline void +sdeb_read_lock (struct sdeb_store_info *sip) +{ +if (sdebug_no_rwlock) +return ; +if (sip) +read_lock (&sip->macc_lck); +else +read_lock (&sdeb_fake_rw_lck); +} + +static inline void +sdeb_read_unlock (struct sdeb_store_info *sip) +{ +if (sdebug_no_rwlock) +return ; +if (sip) +read_unlock (&sip->macc_lck); +else +read_unlock (&sdeb_fake_rw_lck); +} + +static inline void +sdeb_write_lock (struct sdeb_store_info *sip) +{ +if (sdebug_no_rwlock) +return ; +if (sip) +write_lock (&sip->macc_lck); +else +write_lock (&sdeb_fake_rw_lck); +} + +static inline void +sdeb_write_unlock (struct sdeb_store_info *sip) +{ +if (sdebug_no_rwlock) +return ; +if (sip) +write_unlock (&sip->macc_lck); +else +write_unlock (&sdeb_fake_rw_lck); +} +static int resp_read_dt0 (struct scsi_cmnd *scp,struct sdebug_dev_info *devip) {bool check_prot;
@@ -3138,7 +3183,6 @@ static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)int ret;u64 lba;struct sdeb_store_info *sip =devip2sip (devip, true); -rwlock_t *macc_lckp = sip ? &sip->macc_lck : &sdeb_fake_rw_lck;u8 *cmd = scp->cmnd;switch (cmd[0 ]) {
@@ -3217,29 +3261,29 @@ static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)return check_condition_result; } -read_lock (macc_lckp); +sdeb_read_lock (sip);/* DIX + T10 DIF */ if (unlikely (sdebug_dix &&scsi_prot_sg_count (scp))) {switch (prot_verify_read (scp, lba, num, ei_lba)) {case 1 :/* Guard tag error */ if (cmd[1 ] >>5 !=3 ) {/* RDPROTECT != 3 */ -read_unlock (macc_lckp); +sdeb_read_unlock (sip);mk_sense_buffer (scp,ABORTED_COMMAND ,0x10 ,1 );return check_condition_result; }else if (scp->prot_flags &SCSI_PROT_GUARD_CHECK ) { -read_unlock (macc_lckp); +sdeb_read_unlock (sip);mk_sense_buffer (scp,ILLEGAL_REQUEST ,0x10 ,1 );return illegal_condition_result; }break ;case 3 :/* Reference tag error */ if (cmd[1 ] >>5 !=3 ) {/* RDPROTECT != 3 */ -read_unlock (macc_lckp); +sdeb_read_unlock (sip);mk_sense_buffer (scp,ABORTED_COMMAND ,0x10 ,3 );return check_condition_result; }else if (scp->prot_flags &SCSI_PROT_REF_CHECK ) { -read_unlock (macc_lckp); +sdeb_read_unlock (sip);mk_sense_buffer (scp,ILLEGAL_REQUEST ,0x10 ,3 );return illegal_condition_result; }
@@ -3248,7 +3292,7 @@ static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) } ret =do_device_access (sip, scp,0 , lba, num, false); -read_unlock (macc_lckp); +sdeb_read_unlock (sip);if (unlikely (ret ==-1 ))return DID_ERROR <<16 ;
@@ -3436,7 +3480,6 @@ static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)int ret;u64 lba;struct sdeb_store_info *sip =devip2sip (devip, true); -rwlock_t *macc_lckp = &sip->macc_lck;u8 *cmd = scp->cmnd;switch (cmd[0 ]) {
@@ -3491,10 +3534,10 @@ static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) "to DIF device\n"); } -write_lock (macc_lckp); +sdeb_write_lock (sip); ret =check_device_access_params (scp, lba, num, true);if (ret) { -write_unlock (macc_lckp); +sdeb_write_unlock (sip);return ret; }
@@ -3503,22 +3546,22 @@ static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)switch (prot_verify_write (scp, lba, num, ei_lba)) {case 1 :/* Guard tag error */ if (scp->prot_flags &SCSI_PROT_GUARD_CHECK ) { -write_unlock (macc_lckp); +sdeb_write_unlock (sip);mk_sense_buffer (scp,ILLEGAL_REQUEST ,0x10 ,1 );return illegal_condition_result; }else if (scp->cmnd[1 ] >>5 !=3 ) {/* WRPROTECT != 3 */ -write_unlock (macc_lckp); +sdeb_write_unlock (sip);mk_sense_buffer (scp,ABORTED_COMMAND ,0x10 ,1 );return check_condition_result; }break ;case 3 :/* Reference tag error */ if (scp->prot_flags &SCSI_PROT_REF_CHECK ) { -write_unlock (macc_lckp); +sdeb_write_unlock (sip);mk_sense_buffer (scp,ILLEGAL_REQUEST ,0x10 ,3 );return illegal_condition_result; }else if (scp->cmnd[1 ] >>5 !=3 ) {/* WRPROTECT != 3 */ -write_unlock (macc_lckp); +sdeb_write_unlock (sip);mk_sense_buffer (scp,ABORTED_COMMAND ,0x10 ,3 );return check_condition_result; }
@@ -3532,7 +3575,7 @@ static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)/* If ZBC zone then bump its write pointer */ if (sdebug_dev_is_zoned (devip))zbc_inc_wp (devip, lba, num); -write_unlock (macc_lckp); +sdeb_write_unlock (sip);if (unlikely (-1 == ret))return DID_ERROR <<16 ; else if (unlikely(sdebug_verbose &&
@@ -3572,7 +3615,6 @@ static int resp_write_scat(struct scsi_cmnd *scp,u8 *lrdp =NULL ;u8 *up;struct sdeb_store_info *sip =devip2sip (devip, true); -rwlock_t *macc_lckp = &sip->macc_lck;u8 wrprotect;u16 lbdof, num_lrd, k;u32 num, num_by, bt_len, lbdof_blen, sg_off, cum_lb;
@@ -3640,7 +3682,7 @@ static int resp_write_scat(struct scsi_cmnd *scp, goto err_out; } -write_lock (macc_lckp); +sdeb_write_lock (sip); sg_off = lbdof_blen;/* Spec says Buffer xfer Length field in number of LBs in dout */ cum_lb =0 ;
@@ -3722,7 +3764,7 @@ static int resp_write_scat(struct scsi_cmnd *scp, } ret =0 ; err_out_unlock: -write_unlock (macc_lckp); +sdeb_write_unlock (sip); err_out:kfree (lrdp);return ret;
@@ -3739,15 +3781,14 @@ static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,int ret;struct sdeb_store_info *sip =devip2sip ((struct sdebug_dev_info *) scp->device->hostdata, true); -rwlock_t *macc_lckp = &sip->macc_lck;u8 *fs1p;u8 *fsp; -write_lock (macc_lckp); +sdeb_write_lock (sip); ret =check_device_access_params (scp, lba, num, true);if (ret) { -write_unlock (macc_lckp); +sdeb_write_unlock (sip);return ret; }
@@ -3767,7 +3808,7 @@ static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num, ret =fetch_to_dev_buffer (scp, fs1p, lb_size);if (-1 == ret) { -write_unlock (&sip->macc_lck); +sdeb_write_unlock (sip);return DID_ERROR <<16 ; }else if (sdebug_verbose && !ndob && (ret < lb_size)) sdev_printk(KERN_INFO, scp->device,
@@ -3786,7 +3827,7 @@ static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,if (sdebug_dev_is_zoned (devip))zbc_inc_wp (devip, lba, num); out: -write_unlock (macc_lckp); +sdeb_write_unlock (sip);return 0 ; }
@@ -3899,7 +3940,6 @@ static int resp_comp_write(struct scsi_cmnd *scp,u8 *cmd = scp->cmnd;u8 *arr;struct sdeb_store_info *sip =devip2sip (devip, true); -rwlock_t *macc_lckp = &sip->macc_lck;u64 lba;u32 dnum;u32 lb_size = sdebug_sector_size;
@@ -3932,7 +3972,7 @@ static int resp_comp_write(struct scsi_cmnd *scp,return check_condition_result; } -write_lock (macc_lckp); +sdeb_write_lock (sip); ret =do_dout_fetch (scp, dnum, arr);if (ret ==-1 ) {
@@ -3950,7 +3990,7 @@ static int resp_comp_write(struct scsi_cmnd *scp,if (scsi_debug_lbp ())map_region (sip, lba, num); cleanup: -write_unlock (macc_lckp); +sdeb_write_unlock (sip);kfree (arr);return retval; }
@@ -3966,7 +4006,6 @@ static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)unsigned char *buf;struct unmap_block_desc *desc;struct sdeb_store_info *sip =devip2sip (devip, true); -rwlock_t *macc_lckp = &sip->macc_lck;unsigned int i, payload_len, descriptors;int ret;
@@ -3995,7 +4034,7 @@ static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) desc = (void *)&buf[8 ]; -write_lock (macc_lckp); +sdeb_write_lock (sip);for (i =0 ; i < descriptors ; i++) {unsigned long long lba =get_unaligned_be64 (&desc[i].lba);
@@ -4011,7 +4050,7 @@ static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) ret =0 ; out: -write_unlock (macc_lckp); +sdeb_write_unlock (sip);kfree (buf);return ret;
@@ -4103,7 +4142,6 @@ static int resp_pre_fetch(struct scsi_cmnd *scp,u32 nblks;u8 *cmd = scp->cmnd;struct sdeb_store_info *sip =devip2sip (devip, true); -rwlock_t *macc_lckp = &sip->macc_lck;u8 *fsp = sip->storep;if (cmd[0 ] ==PRE_FETCH ) {/* 10 byte cdb */
@@ -4125,12 +4163,12 @@ static int resp_pre_fetch(struct scsi_cmnd *scp, rest = block + nblks - sdebug_store_sectors;/* Try to bring the PRE-FETCH range into CPU's cache */ -read_lock (macc_lckp); +sdeb_read_lock (sip);prefetch_range (fsp + (sdebug_sector_size * block), (nblks - rest) * sdebug_sector_size);if (rest)prefetch_range (fsp, rest * sdebug_sector_size); -read_unlock (macc_lckp); +sdeb_read_unlock (sip); fini:if (cmd[1 ] &0x2 ) res =SDEG_RES_IMMED_MASK ;
@@ -4251,7 +4289,6 @@ static int resp_verify(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)u8 *arr;u8 *cmd = scp->cmnd;struct sdeb_store_info *sip =devip2sip (devip, true); -rwlock_t *macc_lckp = &sip->macc_lck; bytchk = (cmd[1 ] >>1 ) &0x3 ;if (bytchk ==0 ) {
@@ -4290,7 +4327,7 @@ static int resp_verify(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)return check_condition_result; }/* Not changing store, so only need read access */ -read_lock (macc_lckp); +sdeb_read_lock (sip); ret =do_dout_fetch (scp, a_num, arr);if (ret ==-1 ) {
@@ -4312,7 +4349,7 @@ static int resp_verify(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) goto cleanup; } cleanup: -read_unlock (macc_lckp); +sdeb_read_unlock (sip);kfree (arr);return ret; }
@@ -4332,7 +4369,6 @@ static int resp_report_zones(struct scsi_cmnd *scp,u8 *cmd = scp->cmnd;struct sdeb_zone_state *zsp;struct sdeb_store_info *sip =devip2sip (devip, false); -rwlock_t *macc_lckp = sip ? &sip->macc_lck : &sdeb_fake_rw_lck;if (!sdebug_dev_is_zoned (devip)) {mk_sense_invalid_opcode (scp);
@@ -4361,7 +4397,7 @@ static int resp_report_zones(struct scsi_cmnd *scp,return check_condition_result; } -read_lock (macc_lckp); +sdeb_read_lock (sip); desc = arr +64 ;for (i =0 ; i < max_zones; i++) {
@@ -4449,7 +4485,7 @@ static int resp_report_zones(struct scsi_cmnd *scp, ret =fill_from_dev_buffer (scp, arr,min_t (u32, alloc_len, rep_len)); fini: -read_unlock (macc_lckp); +sdeb_read_unlock (sip);kfree (arr);return ret; }
@@ -4475,14 +4511,13 @@ static int resp_open_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)struct sdeb_zone_state *zsp;bool all = cmd[14 ] &0x01 ;struct sdeb_store_info *sip =devip2sip (devip, false); -rwlock_t *macc_lckp = sip ? &sip->macc_lck : &sdeb_fake_rw_lck;if (!sdebug_dev_is_zoned (devip)) {mk_sense_invalid_opcode (scp);return check_condition_result; } -write_lock (macc_lckp); +sdeb_write_lock (sip);if (all) {/* Check if all closed zones can be open */
@@ -4531,7 +4566,7 @@ static int resp_open_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)zbc_open_zone (devip, zsp, true); fini: -write_unlock (macc_lckp); +sdeb_write_unlock (sip);return res; }
@@ -4552,14 +4587,13 @@ static int resp_close_zone(struct scsi_cmnd *scp,struct sdeb_zone_state *zsp;bool all = cmd[14 ] &0x01 ;struct sdeb_store_info *sip =devip2sip (devip, false); -rwlock_t *macc_lckp = sip ? &sip->macc_lck : &sdeb_fake_rw_lck;if (!sdebug_dev_is_zoned (devip)) {mk_sense_invalid_opcode (scp);return check_condition_result; } -write_lock (macc_lckp); +sdeb_write_lock (sip);if (all) {zbc_close_all (devip);
@@ -4588,7 +4622,7 @@ static int resp_close_zone(struct scsi_cmnd *scp,zbc_close_zone (devip, zsp); fini: -write_unlock (macc_lckp); +sdeb_write_unlock (sip);return res; }
@@ -4625,14 +4659,13 @@ static int resp_finish_zone(struct scsi_cmnd *scp,u8 *cmd = scp->cmnd;bool all = cmd[14 ] &0x01 ;struct sdeb_store_info *sip =devip2sip (devip, false); -rwlock_t *macc_lckp = sip ? &sip->macc_lck : &sdeb_fake_rw_lck;if (!sdebug_dev_is_zoned (devip)) {mk_sense_invalid_opcode (scp);return check_condition_result; } -write_lock (macc_lckp); +sdeb_write_lock (sip);if (all) {zbc_finish_all (devip);
@@ -4661,7 +4694,7 @@ static int resp_finish_zone(struct scsi_cmnd *scp,zbc_finish_zone (devip, zsp, true); fini: -write_unlock (macc_lckp); +sdeb_write_unlock (sip);return res; }
@@ -4706,14 +4739,13 @@ static int resp_rwp_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)u8 *cmd = scp->cmnd;bool all = cmd[14 ] &0x01 ;struct sdeb_store_info *sip =devip2sip (devip, false); -rwlock_t *macc_lckp = sip ? &sip->macc_lck : &sdeb_fake_rw_lck;if (!sdebug_dev_is_zoned (devip)) {mk_sense_invalid_opcode (scp);return check_condition_result; } -write_lock (macc_lckp); +sdeb_write_lock (sip);if (all) {zbc_rwp_all (devip);
@@ -4741,7 +4773,7 @@ static int resp_rwp_zone(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)zbc_rwp_zone (devip, zsp); fini: -write_unlock (macc_lckp); +sdeb_write_unlock (sip);return res; }
@@ -5740,6 +5772,7 @@ module_param_named(medium_error_start, sdebug_medium_error_start, int, S_IRUGO | S_IWUSR);module_param_named (ndelay, sdebug_ndelay, int,S_IRUGO |S_IWUSR );module_param_named (no_lun_0, sdebug_no_lun_0, int,S_IRUGO |S_IWUSR ); +module_param_named (no_rwlock, sdebug_no_rwlock, bool,S_IRUGO |S_IWUSR );module_param_named (no_uld, sdebug_no_uld, int,S_IRUGO );module_param_named (num_parts, sdebug_num_parts, int,S_IRUGO );module_param_named (num_tgts, sdebug_num_tgts, int,S_IRUGO |S_IWUSR );
@@ -5812,6 +5845,7 @@ MODULE_PARM_DESC(medium_error_count, "count of sectors to return follow on MEDIUMODULE_PARM_DESC (medium_error_start,"starting sector number to return MEDIUM error" );MODULE_PARM_DESC (ndelay,"response delay in nanoseconds (def=0 -> ignore)" );MODULE_PARM_DESC (no_lun_0,"no LU number 0 (def=0 -> have lun 0)" ); +MODULE_PARM_DESC (no_rwlock,"don't protect user data reads+writes (def=0)" );MODULE_PARM_DESC (no_uld,"stop ULD (e.g. sd driver) attaching (def=0))" );MODULE_PARM_DESC (num_parts,"number of partitions(def=0)" );MODULE_PARM_DESC (num_tgts,"number of targets per host to simulate(def=1)" );
@@ -6374,6 +6408,23 @@ static ssize_t host_max_queue_show(struct device_driver *ddp, char *buf)return scnprintf (buf,PAGE_SIZE ,"%d\n" , sdebug_host_max_queue); } +static ssize_t no_rwlock_show (struct device_driver *ddp,char *buf) +{ +return scnprintf (buf,PAGE_SIZE ,"%d\n" , sdebug_no_rwlock); +} + +static ssize_t no_rwlock_store (struct device_driver *ddp,const char *buf,size_t count) +{ +bool v; + +if (kstrtobool (buf, &v)) +return -EINVAL ; + + sdebug_no_rwlock = v; +return count; +} +static DRIVER_ATTR_RW (no_rwlock); +/* * Since this is used for .can_queue, and we get the hc_idx tag from the bitmap * in range [0, sdebug_host_max_queue), we can't change it.
@@ -6739,6 +6790,7 @@ static struct attribute *sdebug_drv_attrs[] = { &driver_attr_lun_format.attr, &driver_attr_max_luns.attr, &driver_attr_max_queue.attr, + &driver_attr_no_rwlock.attr, &driver_attr_no_uld.attr, &driver_attr_scsi_level.attr, &driver_attr_virtual_gb.attr,
Signature · 1 line
2.25.1
PATCH 9/9 scsi_debug: add environmental reporting log subpage
In reply to Douglas GilbertLog subpages are starting to appear in real devices (e.g. SSDs) so add support for one. Adopt approach where all "wild" sub-pages are themselves listed as long as there is at least one non-wild page or subpage for a given page number.
--- drivers/scsi/scsi_debug.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
drivers/scsi/scsi_debug.c +33 -0
@@ -2793,6 +2793,18 @@ static int resp_ie_l_pg(unsigned char *arr)return sizeof (ie_l_pg); } +static int resp_env_rep_l_spg (unsigned char *arr) +{ +unsigned char env_rep_l_spg[] = {0x0 ,0x0 ,0x23 ,0x8 , +0x0 ,40 ,72 ,0xff ,45 ,18 ,0 ,0 , +0x1 ,0x0 ,0x23 ,0x8 , +0x0 ,55 ,72 ,35 ,55 ,45 ,0 ,0 , + }; + +memcpy (arr, env_rep_l_spg,sizeof (env_rep_l_spg)); +return sizeof (env_rep_l_spg); +} +#define SDEBUG_MAX_LSENSE_SZ 512 static int resp_log_sense(struct scsi_cmnd *scp,
@@ -2845,26 +2857,47 @@ static int resp_log_sense(struct scsi_cmnd *scp, arr[n++] =0xff ;/* this page */ arr[n++] =0xd ; arr[n++] =0x0 ;/* Temperature */ + arr[n++] =0xd ; + arr[n++] =0x1 ;/* Environment reporting */ + arr[n++] =0xd ; + arr[n++] =0xff ;/* all 0xd subpages */ arr[n++] =0x2f ; arr[n++] =0x0 ;/* Informational exceptions */ + arr[n++] =0x2f ; + arr[n++] =0xff ;/* all 0x2f subpages */ arr[3 ] = n -4 ;break ;case 0xd :/* Temperature subpages */ n =4 ; arr[n++] =0xd ; arr[n++] =0x0 ;/* Temperature */ + arr[n++] =0xd ; + arr[n++] =0x1 ;/* Environment reporting */ + arr[n++] =0xd ; + arr[n++] =0xff ;/* these subpages */ arr[3 ] = n -4 ;break ;case 0x2f :/* Informational exceptions subpages */ n =4 ; arr[n++] =0x2f ; arr[n++] =0x0 ;/* Informational exceptions */ + arr[n++] =0x2f ; + arr[n++] =0xff ;/* these subpages */ arr[3 ] = n -4 ;break ;default :mk_sense_invalid_fld (scp,SDEB_IN_CDB ,2 ,5 );return check_condition_result; } + }else if (subpcode >0 ) { + arr[0 ] |=0x40 ; + arr[1 ] = subpcode; +if (pcode ==0xd && subpcode ==1 ) + arr[3 ] =resp_env_rep_l_spg (arr +4 ); +else { +mk_sense_invalid_fld (scp,SDEB_IN_CDB ,2 ,5 ); +return check_condition_result; + } }else {mk_sense_invalid_fld (scp,SDEB_IN_CDB ,3 ,-1 );return check_condition_result;
Signature · 1 line
2.25.1
PATCH 4/9 scsi_debug: refine sdebug_blk_mq_poll
In reply to Douglas GilbertRefine the sdebug_blk_mq_poll() function so it only takes the spinlock on the queue when it can see one or more requests with the in_use bitmap flag set.
--- drivers/scsi/scsi_debug.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)
drivers/scsi/scsi_debug.c +16 -5
@@ -7396,6 +7396,7 @@ static int sdebug_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num) {bool first;bool retiring = false; +bool locked = false;int num_entries =0 ;unsigned int qc_idx =0 ;unsigned long iflags;
@@ -7407,16 +7408,23 @@ static int sdebug_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)struct sdebug_defer *sd_dp; sqp = sdebug_q_arr + queue_num; -spin_lock_irqsave (&sqp->qc_lock, iflags); + qc_idx =find_first_bit (sqp->in_use_bm, sdebug_max_queue); +if (qc_idx >= sdebug_max_queue) +return 0 ;for (first = true; first || qc_idx +1 < sdebug_max_queue; ) { +if (!locked) { +spin_lock_irqsave (&sqp->qc_lock, iflags); + locked = true; + }if (first) { - qc_idx =find_first_bit (sqp->in_use_bm, sdebug_max_queue); first = false; +if (!test_bit (qc_idx, sqp->in_use_bm)) +continue ; }else { qc_idx =find_next_bit (sqp->in_use_bm, sdebug_max_queue, qc_idx +1 ); } -if (unlikely (qc_idx >= sdebug_max_queue)) +if (qc_idx >= sdebug_max_queue)break ; sqcp = &sqp->qc_arr[qc_idx];
@@ -7465,11 +7473,14 @@ static int sdebug_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num) }WRITE_ONCE (sd_dp->defer_t,SDEB_DEFER_NONE );spin_unlock_irqrestore (&sqp->qc_lock, iflags); + locked = false;scsi_done (scp);/* callback to mid level */ -spin_lock_irqsave (&sqp->qc_lock, iflags); num_entries++; +if (find_first_bit (sqp->in_use_bm, sdebug_max_queue) >= sdebug_max_queue) +break ;/* if no more then exit without retaking spinlock */ } -spin_unlock_irqrestore (&sqp->qc_lock, iflags); +if (locked) +spin_unlock_irqrestore (&sqp->qc_lock, iflags);if (num_entries >0 )atomic_add (num_entries, &sdeb_mq_poll_count);return num_entries;
Signature · 1 line
2.25.1
PATCH 7/9 scsi_debug: add sdeb_sgl_copy_sgl and friends
In reply to Douglas GilbertThe scatter-gather list helper functions in this patch have been proposed for inclusion in lib/scatterlist.c where they would be exported. Before that happens they need to be accepted via another kernel subsystem maintainer. In the meantime they are placed in this driver with a "sdeb_" prefix and with static scope. The next patch in this set makes extensive use of these functions as the backing store(s) of the scsi_debug driver is changed from vmalloc() based to sgl based.
These functions where proposed for the patchset whose cover was
titled: "[PATCH v4 0/4] scatterlist: add new capabilities". That patchset was sent to the linux-block, linux-scsi and linux-kernel lists on 20201016. Since all patches in that set were reviewed by Bodo Stroesser his review is carried over to this patch.
--- drivers/scsi/scsi_debug.c | 127 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+)
drivers/scsi/scsi_debug.c +127 -0
@@ -1206,6 +1206,133 @@ static int p_fill_from_dev_buffer(struct scsi_cmnd *scp, const void *arr,return 0 ; } +/* +* The following 4 functions are proposed for lib/scatterlist.c (without the +* "sdeb_" prefix) and will be removed from this driver if the proposal is accepted. +*/ +static size_t sdeb_sgl_copy_sgl (struct scatterlist *d_sgl,unsigned int d_nents,off_t d_skip, +struct scatterlist *s_sgl,unsigned int s_nents,off_t s_skip, +size_t n_bytes) +{ +size_t len; +size_t offset =0 ; +struct sg_mapping_iter d_iter, s_iter; + +if (n_bytes ==0 ) +return 0 ; +sg_miter_start (&s_iter, s_sgl, s_nents,SG_MITER_ATOMIC |SG_MITER_FROM_SG ); +sg_miter_start (&d_iter, d_sgl, d_nents,SG_MITER_ATOMIC |SG_MITER_TO_SG ); +if (!sg_miter_skip (&s_iter, s_skip)) + goto fini; +if (!sg_miter_skip (&d_iter, d_skip)) + goto fini; + +while (offset < n_bytes) { +if (!sg_miter_next (&s_iter)) +break ; +if (!sg_miter_next (&d_iter)) +break ; + len =min3 (d_iter.length, s_iter.length, n_bytes - offset); + +memcpy (d_iter.addr, s_iter.addr, len); + offset += len; +/* LIFO order (stop d_iter before s_iter) needed with SG_MITER_ATOMIC */ + d_iter.consumed = len; +sg_miter_stop (&d_iter); + s_iter.consumed = len; +sg_miter_stop (&s_iter); + } +fini: +sg_miter_stop (&d_iter); +sg_miter_stop (&s_iter); +return offset; +} + +static bool sdeb_sgl_compare_sgl_idx (struct scatterlist *x_sgl,unsigned int x_nents,off_t x_skip, +struct scatterlist *y_sgl,unsigned int y_nents,off_t y_skip, +size_t n_bytes,size_t *miscompare_idx) +{ +bool equ = true; +size_t len; +size_t offset =0 ; +struct sg_mapping_iter x_iter, y_iter; + +if (n_bytes ==0 ) +return true; +sg_miter_start (&x_iter, x_sgl, x_nents,SG_MITER_ATOMIC |SG_MITER_FROM_SG ); +sg_miter_start (&y_iter, y_sgl, y_nents,SG_MITER_ATOMIC |SG_MITER_FROM_SG ); +if (!sg_miter_skip (&x_iter, x_skip)) + goto fini; +if (!sg_miter_skip (&y_iter, y_skip)) + goto fini; + +while (offset < n_bytes) { +if (!sg_miter_next (&x_iter)) +break ; +if (!sg_miter_next (&y_iter)) +break ; + len =min3 (x_iter.length, y_iter.length, n_bytes - offset); + + equ = !memcmp (x_iter.addr, y_iter.addr, len); +if (!equ) + goto fini; + offset += len; +/* LIFO order is important when SG_MITER_ATOMIC is used */ + y_iter.consumed = len; +sg_miter_stop (&y_iter); + x_iter.consumed = len; +sg_miter_stop (&x_iter); + } +fini: +if (miscompare_idx && !equ) { +u8 *xp = x_iter.addr; +u8 *yp = y_iter.addr; +u8 *x_endp; + +for (x_endp = xp + len ; xp < x_endp; ++xp, ++yp) { +if (*xp != *yp) +break ; + } + *miscompare_idx = offset + len - (x_endp - xp); + } +sg_miter_stop (&y_iter); +sg_miter_stop (&x_iter); +return equ; +} + +static bool sdeb_sgl_compare_sgl (struct scatterlist *x_sgl,unsigned int x_nents,off_t x_skip, +struct scatterlist *y_sgl,unsigned int y_nents,off_t y_skip, +size_t n_bytes) +{ +return sdeb_sgl_compare_sgl_idx (x_sgl, x_nents, x_skip, y_sgl, y_nents, y_skip, n_bytes, +NULL ); +} + +static size_t sdeb_sgl_memset (struct scatterlist *sgl,unsigned int nents,off_t skip, +u8 val,size_t n_bytes) +{ +size_t offset =0 ; +size_t len; +struct sg_mapping_iter miter; + +if (n_bytes ==0 ) +return 0 ; +sg_miter_start (&miter, sgl, nents,SG_MITER_ATOMIC |SG_MITER_TO_SG ); +if (!sg_miter_skip (&miter, skip)) + goto fini; + +while ((offset < n_bytes) &&sg_miter_next (&miter)) { + len =min (miter.length, n_bytes - offset); +memset (miter.addr, val, len); + offset += len; + } +fini: +sg_miter_stop (&miter); +return offset; +} + +/* end of functions proposed for lib/scatterlist.c */ +/* Fetches from SCSI "data-out" buffer. Returns number of bytes fetched into * 'arr' or -1 if error. */
Signature · 1 line
2.25.1
PATCH 8/9 scsi_debug: change store from vmalloc to sgl
In reply to Douglas GilbertA long time ago this driver's store was allocated by kmalloc() or alloc_pages(). When this was switched to vmalloc() the author noticed slower ramdisk access times and more variability in repeated tests. So try going back with sgl_alloc_order() to get uniformly sized allocations in a sometimes large scatter gather _array_. That array is the basis of maintaining O(1) access to the store.
Using sgl_alloc_order() and friends requires CONFIG_SGL_ALLOC so add a 'select' to the Kconfig file.
Remove kcalloc() in resp_verify() as sgl_s can now be compared directly without forming an intermediate buffer. This is a performance win for the SCSI VERIFY command implementation.
Make the SCSI COMPARE AND WRITE command yield the offset of the first miscompared byte when the compare fails (as required by T10).
This patch previously depended on: "[PATCH v4 0/4] scatterlist:
add new capabilities". However while that patchset is being considered, those functions have been replicated by the previous patch with a "sdeb_" prefix so they can be used by this patch.
--- drivers/scsi/Kconfig | 3 +- drivers/scsi/scsi_debug.c | 491 ++++++++++++++++++++++++++------------ 2 files changed, 347 insertions(+), 147 deletions(-)
drivers/scsi/Kconfig +2 -1
@@ -1240,13 +1240,14 @@ config SCSI_DEBUG tristate "SCSI debugging host and device simulator" depends on SCSI select CRC_T10DIF + select SGL_ALLOC help This pseudo driver simulates one or more hosts (SCSI initiators), each with one or more targets, each with one or more logical units. Defaults to one of each, creating a small RAM disk device. Many parameters found in the /sys/bus/pseudo/drivers/scsi_debug directory can be tweaked at run time. - See <http://sg.danny.cz/sg/sdebug26.html> for more information. + See <https://sg.danny.cz/sg/scsi_debug.html> for more information. Mainly used for testing and best as a module. If unsure, say N. config SCSI_MESH
drivers/scsi/scsi_debug.c +345 -146
764 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH 3/9 scsi_debug: use task set full more
In reply to Douglas GilbertWhen the internal in_use bit array in this driver is full returning SCSI_MLQUEUE_HOST_BUSY leads to the mid-level re-issueing the request which is unhelpful. Previously TASK SET FULL status was only returned if ALL_TSF [0x400] is placed in the opts variable (at load time or via sysfs). Now ignore that setting and always return TASK SET FULL when in_use array is full. Also set DID_ABORT together with TASK SET FULL so the mid-level gives up immediately.
Aside: the situations addressed by this patch lead to lockups and timeouts. They have only been detected when blk_poll() is used. That mechanism is relatively new in the SCSI subsystem suggesting the mid-level may need more work in that area.
--- drivers/scsi/scsi_debug.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-)
drivers/scsi/scsi_debug.c +6 -13
@@ -174,7 +174,7 @@ static const char *sdebug_version_date = "20200710";#define SDEBUG_OPT_MAC_TIMEOUT 128#define SDEBUG_OPT_SHORT_TRANSFER 0x100#define SDEBUG_OPT_Q_NOISE 0x200 -#define SDEBUG_OPT_ALL_TSF 0x400 +#define SDEBUG_OPT_ALL_TSF 0x400/* ignore */ #define SDEBUG_OPT_RARE_TSF 0x800#define SDEBUG_OPT_N_WCE 0x1000#define SDEBUG_OPT_RESET_NOISE 0x2000
@@ -861,7 +861,7 @@ static const int illegal_condition_result = (DID_ABORT <<16 ) |SAM_STAT_CHECK_CONDITION ;static const int device_qfull_result = - (DID_OK <<16 ) |SAM_STAT_TASK_SET_FULL ; + (DID_ABORT <<16 ) |SAM_STAT_TASK_SET_FULL ;static const int condition_met_result =SAM_STAT_CONDITION_MET ;
@@ -5521,18 +5521,11 @@ static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,spin_unlock_irqrestore (&sqp->qc_lock, iflags);if (scsi_result) goto respond_in_thread; -else if (SDEBUG_OPT_ALL_TSF & sdebug_opts) - scsi_result = device_qfull_result; + scsi_result = device_qfull_result;if (SDEBUG_OPT_Q_NOISE & sdebug_opts) -sdev_printk (KERN_INFO , sdp, -"%s: max_queue=%d exceeded, %s\n" , - __func__, sdebug_max_queue, - (scsi_result ?"status: TASK SET FULL" : -"report: host busy" )); -if (scsi_result) - goto respond_in_thread; -else -return SCSI_MLQUEUE_HOST_BUSY ; +sdev_printk (KERN_INFO , sdp,"%s: max_queue=%d exceeded: TASK SET FULL\n" , + __func__, sdebug_max_queue); + goto respond_in_thread; }set_bit (k, sqp->in_use_bm);atomic_inc (&devip->num_in_q);
Signature · 1 line
2.25.1
Re: [PATCH 8/9] scsi_debug: change store from vmalloc to sgl
In reply to Douglas GilbertQuoted 18 lines · Douglas Gilbert
A long time ago this driver's store was allocated by
kmalloc()oralloc_pages(). When this was switched tovmalloc()the author noticed slower ramdisk access times and more variability in repeated tests. So try going back withsgl_alloc_order()to get uniformly sized allocations in a sometimes large scatter gather _array_. That array is the basis of maintaining O(1) access to the store.Using
sgl_alloc_order()and friends requiresCONFIG_SGL_ALLOCso add a 'select' to theKconfigfile.Remove
kcalloc()inresp_verify()assgl_scan now be compared directly without forming an intermediate buffer. This is a performance win for the SCSI VERIFY command implementation.Make the SCSI COMPARE AND WRITE command yield the offset of the first miscompared byte when the compare fails (as required by T10).
Quoted 6 lines
This patch previously depended on: "[PATCH v4 0/4] scatterlist:
add new capabilities". However while that patchset is being considered, those functions have been replicated by the previous patch with a "sdeb_" prefix so they can be used by this patch.Signed-off-by Douglas Gilbert <[email protected]>
Doug, thank you for your work on scsi_debug. This patch touches the reset write pointer handler for scsi_debug ZBC devices. I have confirmed that the handler works as expected after applying this patch. Good.
Signature · 2 lines
Best Regards, Shin'ichiro Kawasaki
Re: [PATCH 1/9] scsi_debug: address races following module load
In reply to Douglas Gilbertdrivers/scsi/scsi_debug.c static int scsi_debug_slave_configure(struct scsi_device *sdp) · line 5036
Quoted 4 lines · Douglas Gilbert
drivers/scsi/scsi_debug.c +4
-0+if (READ_ONCE (sdebug_deflect_incoming)) { +pr_info ("Exit early due to deflect_incoming\n" ); +return 1 ; + }
The new mechanism will work fine on x86 but not on ARM systems. Please change all READ_ONCE(sdebug_deflect_incoming) calls into smp_load_acquire(&sdebug_deflect_incoming) and all WRITE_ONCE(sdebug_deflect_incoming) calls into smp_store_release(&sdebug_deflect_incoming).
Thanks,
Bart.
Re: [PATCH 1/9] scsi_debug: address races following module load
In reply to Douglas GilbertQuoted 3 lines · Douglas Gilbert
This patch was developed several months ago after discussions with Luis Chamberlain. It may have been overtaken by more recent work by Luis.
Yeah well pretty much I ended up concluding that even though the driver *can* be sloppy ultimately we expose tons of knobs through userspace to easily bump module refcounts at userpace's whim's. For instance a userspace loop issuing open() on any block driver will bump the module refcnt. A simple reproducer is provided through korg#214015 [0], see busy-open-block-device-sleep.c, you run that while running the test script. In short userspace's simple open() triggers blkdev_open() and that bumps the module refcnt. This means the module refcnt is finicky.
In so far as a fix is concerned, you have to consider that long ago this race was considered theoretical, and we had in-kernel support to wait to get the refcnt to 0 before unloading a module. That code proved complex, and since the race was theoretical it was removed. The race is clearly possible with any block driver, and therefore any type of driver too, and so the fix really is to implement a proper patient module remover in userspace.
I already implemented and posted patches for kmod for this, they are in review. In the meantime we have needed to also open code solutions for fstests and blktest while kmod is not released yet with the new changes. I already posted patches for both, and fstests has this fix merged a while ago. Blktests folks just need to finish testing things and merge the changes.
All that said, drivers can certainly be enhanced to reduce these sorts of races too. It is all driver specific and so outside of the scope of generic drivers.
[0] https://bugzilla.kernel.org/show_bug.cgi?id=214015
Luis