Skip to content

Commit ed1227e

Browse files
mikechristiemartinkpetersen
authored andcommitted
scsi: target: Fix ordered tag handling
This patch fixes the following bugs: 1. If there are multiple ordered cmds queued and multiple simple cmds completing, target_restart_delayed_cmds() could be called on different CPUs and each instance could start a ordered cmd. They could then run in different orders than they were queued. 2. target_restart_delayed_cmds() and target_handle_task_attr() can race where: 1. target_handle_task_attr() has passed the simple_cmds == 0 check. 2. transport_complete_task_attr() then decrements simple_cmds to 0. 3. transport_complete_task_attr() runs target_restart_delayed_cmds() and it does not see any cmds on the delayed_cmd_list. 4. target_handle_task_attr() adds the cmd to the delayed_cmd_list. The cmd will then end up timing out. 3. If we are sent > 1 ordered cmds and simple_cmds == 0, we can execute them out of order, because target_handle_task_attr() will hit that simple_cmds check first and return false for all ordered cmds sent. 4. We run target_restart_delayed_cmds() after every cmd completion, so if there is more than 1 simple cmd running, we start executing ordered cmds after that first cmd instead of waiting for all of them to complete. 5. Ordered cmds are not supposed to start until HEAD OF QUEUE and all older cmds have completed, and not just simple. 6. It's not a bug but it doesn't make sense to take the delayed_cmd_lock for every cmd completion when ordered cmds are almost never used. Just replacing that lock with an atomic increases IOPs by up to 10% when completions are spread over multiple CPUs and there are multiple sessions/ mqs/thread accessing the same device. This patch moves the queued delayed handling to a per device work to serialze the cmd executions for each device and adds a new counter to track HEAD_OF_QUEUE and SIMPLE cmds. We can then check the new counter to determine when to run the work on the completion path. Link: https://lore.kernel.org/r/20210930020422.92578-3-michael.christie@oracle.com Signed-off-by: Mike Christie <michael.christie@oracle.com> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent 945a160 commit ed1227e

4 files changed

Lines changed: 61 additions & 24 deletions

File tree

‎drivers/target/target_core_device.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,8 @@ struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
772772
INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
773773
spin_lock_init(&dev->t10_alua.lba_map_lock);
774774

775+
INIT_WORK(&dev->delayed_cmd_work, target_do_delayed_work);
776+
775777
dev->t10_wwn.t10_dev = dev;
776778
/*
777779
* Use OpenFabrics IEEE Company ID: 00 14 05

‎drivers/target/target_core_internal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ int transport_dump_vpd_ident(struct t10_vpd *, unsigned char *, int);
151151
void transport_clear_lun_ref(struct se_lun *);
152152
sense_reason_t target_cmd_size_check(struct se_cmd *cmd, unsigned int size);
153153
void target_qf_do_work(struct work_struct *work);
154+
void target_do_delayed_work(struct work_struct *work);
154155
bool target_check_wce(struct se_device *dev);
155156
bool target_check_fua(struct se_device *dev);
156157
void __target_execute_cmd(struct se_cmd *, bool);

‎drivers/target/target_core_transport.c‎

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,32 +2173,35 @@ static bool target_handle_task_attr(struct se_cmd *cmd)
21732173
*/
21742174
switch (cmd->sam_task_attr) {
21752175
case TCM_HEAD_TAG:
2176+
atomic_inc_mb(&dev->non_ordered);
21762177
pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x\n",
21772178
cmd->t_task_cdb[0]);
21782179
return false;
21792180
case TCM_ORDERED_TAG:
2180-
atomic_inc_mb(&dev->dev_ordered_sync);
2181+
atomic_inc_mb(&dev->delayed_cmd_count);
21812182

21822183
pr_debug("Added ORDERED for CDB: 0x%02x to ordered list\n",
21832184
cmd->t_task_cdb[0]);
2184-
2185-
/*
2186-
* Execute an ORDERED command if no other older commands
2187-
* exist that need to be completed first.
2188-
*/
2189-
if (!atomic_read(&dev->simple_cmds))
2190-
return false;
21912185
break;
21922186
default:
21932187
/*
21942188
* For SIMPLE and UNTAGGED Task Attribute commands
21952189
*/
2196-
atomic_inc_mb(&dev->simple_cmds);
2190+
atomic_inc_mb(&dev->non_ordered);
2191+
2192+
if (atomic_read(&dev->delayed_cmd_count) == 0)
2193+
return false;
21972194
break;
21982195
}
21992196

2200-
if (atomic_read(&dev->dev_ordered_sync) == 0)
2201-
return false;
2197+
if (cmd->sam_task_attr != TCM_ORDERED_TAG) {
2198+
atomic_inc_mb(&dev->delayed_cmd_count);
2199+
/*
2200+
* We will account for this when we dequeue from the delayed
2201+
* list.
2202+
*/
2203+
atomic_dec_mb(&dev->non_ordered);
2204+
}
22022205

22032206
spin_lock_irq(&cmd->t_state_lock);
22042207
cmd->transport_state &= ~CMD_T_SENT;
@@ -2210,6 +2213,12 @@ static bool target_handle_task_attr(struct se_cmd *cmd)
22102213

22112214
pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to delayed CMD listn",
22122215
cmd->t_task_cdb[0], cmd->sam_task_attr);
2216+
/*
2217+
* We may have no non ordered cmds when this function started or we
2218+
* could have raced with the last simple/head cmd completing, so kick
2219+
* the delayed handler here.
2220+
*/
2221+
schedule_work(&dev->delayed_cmd_work);
22132222
return true;
22142223
}
22152224

@@ -2243,29 +2252,48 @@ EXPORT_SYMBOL(target_execute_cmd);
22432252
* Process all commands up to the last received ORDERED task attribute which
22442253
* requires another blocking boundary
22452254
*/
2246-
static void target_restart_delayed_cmds(struct se_device *dev)
2255+
void target_do_delayed_work(struct work_struct *work)
22472256
{
2248-
for (;;) {
2257+
struct se_device *dev = container_of(work, struct se_device,
2258+
delayed_cmd_work);
2259+
2260+
spin_lock(&dev->delayed_cmd_lock);
2261+
while (!dev->ordered_sync_in_progress) {
22492262
struct se_cmd *cmd;
22502263

2251-
spin_lock(&dev->delayed_cmd_lock);
2252-
if (list_empty(&dev->delayed_cmd_list)) {
2253-
spin_unlock(&dev->delayed_cmd_lock);
2264+
if (list_empty(&dev->delayed_cmd_list))
22542265
break;
2255-
}
22562266

22572267
cmd = list_entry(dev->delayed_cmd_list.next,
22582268
struct se_cmd, se_delayed_node);
2269+
2270+
if (cmd->sam_task_attr == TCM_ORDERED_TAG) {
2271+
/*
2272+
* Check if we started with:
2273+
* [ordered] [simple] [ordered]
2274+
* and we are now at the last ordered so we have to wait
2275+
* for the simple cmd.
2276+
*/
2277+
if (atomic_read(&dev->non_ordered) > 0)
2278+
break;
2279+
2280+
dev->ordered_sync_in_progress = true;
2281+
}
2282+
22592283
list_del(&cmd->se_delayed_node);
2284+
atomic_dec_mb(&dev->delayed_cmd_count);
22602285
spin_unlock(&dev->delayed_cmd_lock);
22612286

2287+
if (cmd->sam_task_attr != TCM_ORDERED_TAG)
2288+
atomic_inc_mb(&dev->non_ordered);
2289+
22622290
cmd->transport_state |= CMD_T_SENT;
22632291

22642292
__target_execute_cmd(cmd, true);
22652293

2266-
if (cmd->sam_task_attr == TCM_ORDERED_TAG)
2267-
break;
2294+
spin_lock(&dev->delayed_cmd_lock);
22682295
}
2296+
spin_unlock(&dev->delayed_cmd_lock);
22692297
}
22702298

22712299
/*
@@ -2283,14 +2311,17 @@ static void transport_complete_task_attr(struct se_cmd *cmd)
22832311
goto restart;
22842312

22852313
if (cmd->sam_task_attr == TCM_SIMPLE_TAG) {
2286-
atomic_dec_mb(&dev->simple_cmds);
2314+
atomic_dec_mb(&dev->non_ordered);
22872315
dev->dev_cur_ordered_id++;
22882316
} else if (cmd->sam_task_attr == TCM_HEAD_TAG) {
2317+
atomic_dec_mb(&dev->non_ordered);
22892318
dev->dev_cur_ordered_id++;
22902319
pr_debug("Incremented dev_cur_ordered_id: %u for HEAD_OF_QUEUE\n",
22912320
dev->dev_cur_ordered_id);
22922321
} else if (cmd->sam_task_attr == TCM_ORDERED_TAG) {
2293-
atomic_dec_mb(&dev->dev_ordered_sync);
2322+
spin_lock(&dev->delayed_cmd_lock);
2323+
dev->ordered_sync_in_progress = false;
2324+
spin_unlock(&dev->delayed_cmd_lock);
22942325

22952326
dev->dev_cur_ordered_id++;
22962327
pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED\n",
@@ -2299,7 +2330,8 @@ static void transport_complete_task_attr(struct se_cmd *cmd)
22992330
cmd->se_cmd_flags &= ~SCF_TASK_ATTR_SET;
23002331

23012332
restart:
2302-
target_restart_delayed_cmds(dev);
2333+
if (atomic_read(&dev->delayed_cmd_count) > 0)
2334+
schedule_work(&dev->delayed_cmd_work);
23032335
}
23042336

23052337
static void transport_complete_qf(struct se_cmd *cmd)

‎include/target/target_core_base.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,9 @@ struct se_device {
812812
atomic_long_t read_bytes;
813813
atomic_long_t write_bytes;
814814
/* Active commands on this virtual SE device */
815-
atomic_t simple_cmds;
816-
atomic_t dev_ordered_sync;
815+
atomic_t non_ordered;
816+
bool ordered_sync_in_progress;
817+
atomic_t delayed_cmd_count;
817818
atomic_t dev_qf_count;
818819
u32 export_count;
819820
spinlock_t delayed_cmd_lock;
@@ -834,6 +835,7 @@ struct se_device {
834835
struct list_head dev_sep_list;
835836
struct list_head dev_tmr_list;
836837
struct work_struct qf_work_queue;
838+
struct work_struct delayed_cmd_work;
837839
struct list_head delayed_cmd_list;
838840
struct list_head qf_cmd_list;
839841
/* Pointer to associated SE HBA */

0 commit comments

Comments
 (0)