[RFC v2 4/6] scsi_debug: weaken rwlock around ramdisk access

Douglas Gilbert
2020-01-09T08:30:45Z

The design of this driver is to do any ramdisk access on the
same thread that invoked the queuecommand() call. That is
assumed to be user space context. The command
duration is implemented by setting the delay with a high
resolution timer. The hr timer's callback may well be in
interrupt context, but it doesn't touch the ramdisk. So try
removing the _irqsave()/_irqrestore() portion on the read-
write lock that protects ramdisk access.

Signed-off-by: Douglas Gilbert <dgilbert@interlog.com>
---
drivers/scsi/scsi_debug.c | 59 +++++++++++++++------------------------
1 file changed, 22 insertions(+), 37 deletions(-)

diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
index b94a453ea0f7..7cafbf4aefe8 100644
--- a/drivers/scsi/scsi_debug.c
+++ b/drivers/scsi/scsi_debug.c
@@ -2716,7 +2716,6 @@ static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
u32 num;
u32 ei_lba;
int acc_num = scp2acc_num(scp);
- unsigned long iflags;
u64 lba;
int ret;
bool check_prot;
@@ -2801,22 +2800,21 @@ static int resp_read_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
return check_condition_result;
}

- read_lock_irqsave(ramdisk_lck_a[acc_num % 2], iflags);
+ read_lock(ramdisk_lck_a[acc_num % 2]);

/* DIX + T10 DIF */
if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
int prot_ret = prot_verify_read(scp, lba, num, ei_lba);

if (prot_ret) {
- read_unlock_irqrestore(ramdisk_lck_a[acc_num % 2],
- iflags);
+ read_unlock(ramdisk_lck_a[acc_num % 2]);
mk_sense_buffer(scp, ABORTED_COMMAND, 0x10, prot_ret);
return illegal_condition_result;
}
}

ret = do_device_access(scp, 0, lba, num, false);
- read_unlock_irqrestore(ramdisk_lck_a[acc_num % 2], iflags);
+ read_unlock(ramdisk_lck_a[acc_num % 2]);
if (unlikely(ret == -1))
return DID_ERROR << 16;

@@ -3023,7 +3021,6 @@ static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
u32 ei_lba;
int acc_num = scp2acc_num(scp);
int ret;
- unsigned long iflags;
u64 lba;
u8 *cmd = scp->cmnd;

@@ -3081,15 +3078,14 @@ static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
ret = check_device_access_params(scp, lba, num, true);
if (ret)
return ret;
- write_lock_irqsave(ramdisk_lck_a[acc_num % 2], iflags);
+ write_lock(ramdisk_lck_a[acc_num % 2]);

/* DIX + T10 DIF */
if (unlikely(sdebug_dix && scsi_prot_sg_count(scp))) {
int prot_ret = prot_verify_write(scp, lba, num, ei_lba);

if (prot_ret) {
- write_unlock_irqrestore(ramdisk_lck_a[acc_num % 2],
- iflags);
+ write_unlock(ramdisk_lck_a[acc_num % 2]);
mk_sense_buffer(scp, ILLEGAL_REQUEST, 0x10, prot_ret);
return illegal_condition_result;
}
@@ -3098,7 +3094,7 @@ static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
ret = do_device_access(scp, 0, lba, num, true);
if (unlikely(scsi_debug_lbp()))
map_region(lba, num);
- write_unlock_irqrestore(ramdisk_lck_a[acc_num % 2], iflags);
+ write_unlock(ramdisk_lck_a[acc_num % 2]);
if (unlikely(-1 == ret))
return DID_ERROR << 16;
else if (unlikely(sdebug_verbose &&
@@ -3146,7 +3142,6 @@ static int resp_write_scat(struct scsi_cmnd *scp,
u32 ei_lba;
int acc_num = scp2acc_num(scp);
u64 lba;
- unsigned long iflags;
int ret, res;
bool is_16;
static const u32 lrd_size = 32; /* + parameter list header size */
@@ -3208,7 +3203,7 @@ static int resp_write_scat(struct scsi_cmnd *scp,
goto err_out;
}

- write_lock_irqsave(ramdisk_lck_a[acc_num % 2], iflags);
+ write_lock(ramdisk_lck_a[acc_num % 2]);
sg_off = lbdof_blen;
/* Spec says Buffer xfer Length field in number of LBs in dout */
cum_lb = 0;
@@ -3291,7 +3286,7 @@ static int resp_write_scat(struct scsi_cmnd *scp,
}
ret = 0;
err_out_unlock:
- write_unlock_irqrestore(ramdisk_lck_a[acc_num % 2], iflags);
+ write_unlock(ramdisk_lck_a[acc_num % 2]);
err_out:
kfree(lrdp);
return ret;
@@ -3300,7 +3295,6 @@ static int resp_write_scat(struct scsi_cmnd *scp,
static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,
u32 ei_lba, bool unmap, bool ndob)
{
- unsigned long iflags;
unsigned long long i;
u64 block, lbaa;
u32 lb_size = sdebug_sector_size;
@@ -3313,7 +3307,7 @@ static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,
if (ret)
return ret;

- write_lock_irqsave(ramdisk_lck_a[acc_num % 2], iflags);
+ write_lock(ramdisk_lck_a[acc_num % 2]);

if (unmap && scsi_debug_lbp()) {
unmap_region(lba, num, acc_num);
@@ -3331,7 +3325,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_irqrestore(ramdisk_lck_a[acc_num % 2], iflags);
+ write_unlock(ramdisk_lck_a[acc_num % 2]);
return DID_ERROR << 16;
} else if (sdebug_verbose && !ndob && (ret < lb_size))
sdev_printk(KERN_INFO, scp->device,
@@ -3347,7 +3341,7 @@ static int resp_write_same(struct scsi_cmnd *scp, u64 lba, u32 num,
if (scsi_debug_lbp())
map_region(lba, num);
out:
- write_unlock_irqrestore(ramdisk_lck_a[acc_num % 2], iflags);
+ write_unlock(ramdisk_lck_a[acc_num % 2]);

return 0;
}
@@ -3463,7 +3457,6 @@ static int resp_comp_write(struct scsi_cmnd *scp,
u32 dnum;
u32 lb_size = sdebug_sector_size;
u8 num;
- unsigned long iflags;
int ret;
int retval = 0;
int acc_num = scp2acc_num(scp);
@@ -3493,7 +3486,7 @@ static int resp_comp_write(struct scsi_cmnd *scp,
return check_condition_result;
}

- write_lock_irqsave(ramdisk_lck_a[acc_num % 2], iflags);
+ write_lock(ramdisk_lck_a[acc_num % 2]);

ret = do_dout_fetch(scp, dnum, arr);
if (ret == -1) {
@@ -3511,7 +3504,7 @@ static int resp_comp_write(struct scsi_cmnd *scp,
if (scsi_debug_lbp())
map_region(lba, num);
cleanup:
- write_unlock_irqrestore(ramdisk_lck_a[acc_num % 2], iflags);
+ write_unlock(ramdisk_lck_a[acc_num % 2]);
kfree(arr);
return retval;
}
@@ -3529,8 +3522,6 @@ static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
unsigned int i, payload_len, descriptors;
int ret;
int acc_num = scp2acc_num(scp);
- unsigned long iflags;
-

if (!scsi_debug_lbp())
return 0; /* fib and say its done */
@@ -3557,7 +3548,7 @@ static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)

desc = (void *)&buf[8];

- write_lock_irqsave(ramdisk_lck_a[acc_num % 2], iflags);
+ write_lock(ramdisk_lck_a[acc_num % 2]);

for (i = 0 ; i < descriptors ; i++) {
unsigned long long lba = get_unaligned_be64(&desc[i].lba);
@@ -3573,7 +3564,7 @@ static int resp_unmap(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
ret = 0;

out:
- write_unlock_irqrestore(ramdisk_lck_a[acc_num % 2], iflags);
+ write_unlock(ramdisk_lck_a[acc_num % 2]);
kfree(buf);

return ret;
@@ -3756,7 +3747,6 @@ static int resp_verify(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
int acc_num = scp2acc_num(scp);
u32 vnum, a_num, off;
const u32 lb_size = sdebug_sector_size;
- unsigned long iflags;
u64 lba;
u8 *arr;
u8 *cmd = scp->cmnd;
@@ -3796,7 +3786,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_irqsave(ramdisk_lck_a[acc_num % 2], iflags);
+ read_lock(ramdisk_lck_a[acc_num % 2]);

ret = do_dout_fetch(scp, a_num, arr);
if (ret == -1) {
@@ -3818,7 +3808,7 @@ static int resp_verify(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
goto cleanup;
}
cleanup:
- read_unlock_irqrestore(ramdisk_lck_a[acc_num % 2], iflags);
+ read_unlock(ramdisk_lck_a[acc_num % 2]);
kfree(arr);
return ret;
}
@@ -4467,9 +4457,6 @@ static int schedule_resp(struct scsi_cmnd *cmnd, struct sdebug_dev_info *devip,

cmnd->result = pfp != NULL ? pfp(cmnd, devip) : 0;
if (cmnd->result & SDEG_RES_IMMED_MASK) {
- /*
- * This is the F_DELAY_OVERR case. No delay.
- */
cmnd->result &= ~SDEG_RES_IMMED_MASK;
delta_jiff = ndelay = 0;
}
@@ -4994,16 +4981,14 @@ static ssize_t doublestore_store(struct device_driver *ddp, const char *buf,
int n;

if (count > 0 && kstrtoint(buf, 10, &n) == 0 && n >= 0) {
- unsigned long iflags;
-
if (sdebug_doublestore == (n > 0))
return count; /* no state change */
if (n <= 0) {
- write_lock_irqsave(ramdisk_lck_a[1], iflags);
+ write_lock(ramdisk_lck_a[1]);
sdebug_doublestore = false;
vfree(fake_store_a[1]);
fake_store_a[1] = NULL;
- write_unlock_irqrestore(ramdisk_lck_a[1], iflags);
+ write_unlock(ramdisk_lck_a[1]);
} else {
unsigned long sz = (unsigned long)sdebug_dev_size_mb *
1048576;
@@ -5013,10 +4998,10 @@ static ssize_t doublestore_store(struct device_driver *ddp, const char *buf,
return -ENOMEM;
if (sdebug_num_parts > 0)
sdebug_build_parts(fsp, sz);
- write_lock_irqsave(ramdisk_lck_a[1], iflags);
+ write_lock(ramdisk_lck_a[1]);
fake_store_a[1] = fsp;
sdebug_doublestore = true;
- write_unlock_irqrestore(ramdisk_lck_a[1], iflags);
+ write_unlock(ramdisk_lck_a[1]);
}
return count;
}
@@ -5949,7 +5934,7 @@ static int scsi_debug_queuecommand(struct Scsi_Host *shost,
pfp = r_pfp; /* if leaf function ptr NULL, try the root's */

fini:
- if (F_DELAY_OVERR & flags)
+ if (F_DELAY_OVERR & flags) /* cmds like INQUIRY respond asap */
return schedule_resp(scp, devip, errsts, pfp, 0, 0);
else if ((flags & F_LONG_DELAY) && (sdebug_jdelay > 0 ||
sdebug_ndelay > 10000)) {
--
2.24.1