Skip to content

Commit a264cf5

Browse files
tyreldmartinkpetersen
authored andcommitted
scsi: ibmvfc: Fix command state accounting and stale response detection
Prior to commit 1f4a4a1 ("scsi: ibmvfc: Complete commands outside the host/queue lock") responses to commands were completed sequentially with the host lock held such that a command had a basic binary state of active or free. It was therefore a simple affair of ensuring the assocaiated ibmvfc_event to a VIOS response was valid by testing that it was not already free. The lock relexation work to complete commands outside the lock inadverdently made it a trinary command state such that a command is either in flight, received and being completed, or completed and now free. This breaks the stale command detection logic as a command may be still marked active and been placed on the delayed completion list when a second stale response for the same command arrives. This can lead to double completions and list corruption. This issue was exposed by a recent VIOS regression were a missing memory barrier could occasionally result in the ibmvfc client receiving a duplicate response for the same command. Fix the issue by introducing the atomic ibmvfc_event.active to track the trinary state of a command. The state is explicitly set to 1 when a command is successfully sent. The CRQ response handlers use atomic_dec_if_positive() to test for stale responses and correctly transition to the completion state when a active command is received. Finally, atomic_dec_and_test() is used to sanity check transistions when commands are freed as a result of a completion, or moved to the purge list as a result of error handling or adapter reset. Link: https://lore.kernel.org/r/[email protected] Fixes: 1f4a4a1 ("scsi: ibmvfc: Complete commands outside the host/queue lock") Cc: [email protected] Signed-off-by: Tyrel Datwyler <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 70edd2e commit a264cf5

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

drivers/scsi/ibmvscsi/ibmvfc.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,13 @@ static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost,
807807
for (i = 0; i < size; ++i) {
808808
struct ibmvfc_event *evt = &pool->events[i];
809809

810+
/*
811+
* evt->active states
812+
* 1 = in flight
813+
* 0 = being completed
814+
* -1 = free/freed
815+
*/
816+
atomic_set(&evt->active, -1);
810817
atomic_set(&evt->free, 1);
811818
evt->crq.valid = 0x80;
812819
evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
@@ -1017,6 +1024,7 @@ static void ibmvfc_free_event(struct ibmvfc_event *evt)
10171024

10181025
BUG_ON(!ibmvfc_valid_event(pool, evt));
10191026
BUG_ON(atomic_inc_return(&evt->free) != 1);
1027+
BUG_ON(atomic_dec_and_test(&evt->active));
10201028

10211029
spin_lock_irqsave(&evt->queue->l_lock, flags);
10221030
list_add_tail(&evt->queue_list, &evt->queue->free);
@@ -1072,6 +1080,12 @@ static void ibmvfc_complete_purge(struct list_head *purge_list)
10721080
**/
10731081
static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
10741082
{
1083+
/*
1084+
* Anything we are failing should still be active. Otherwise, it
1085+
* implies we already got a response for the command and are doing
1086+
* something bad like double completing it.
1087+
*/
1088+
BUG_ON(!atomic_dec_and_test(&evt->active));
10751089
if (evt->cmnd) {
10761090
evt->cmnd->result = (error_code << 16);
10771091
evt->done = ibmvfc_scsi_eh_done;
@@ -1723,6 +1737,7 @@ static int ibmvfc_send_event(struct ibmvfc_event *evt,
17231737

17241738
evt->done(evt);
17251739
} else {
1740+
atomic_set(&evt->active, 1);
17261741
spin_unlock_irqrestore(&evt->queue->l_lock, flags);
17271742
ibmvfc_trc_start(evt);
17281743
}
@@ -3251,7 +3266,7 @@ static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
32513266
return;
32523267
}
32533268

3254-
if (unlikely(atomic_read(&evt->free))) {
3269+
if (unlikely(atomic_dec_if_positive(&evt->active))) {
32553270
dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
32563271
crq->ioba);
32573272
return;
@@ -3778,7 +3793,7 @@ static void ibmvfc_handle_scrq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost
37783793
return;
37793794
}
37803795

3781-
if (unlikely(atomic_read(&evt->free))) {
3796+
if (unlikely(atomic_dec_if_positive(&evt->active))) {
37823797
dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
37833798
crq->ioba);
37843799
return;

drivers/scsi/ibmvscsi/ibmvfc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ struct ibmvfc_event {
745745
struct ibmvfc_target *tgt;
746746
struct scsi_cmnd *cmnd;
747747
atomic_t free;
748+
atomic_t active;
748749
union ibmvfc_iu *xfer_iu;
749750
void (*done)(struct ibmvfc_event *evt);
750751
void (*_done)(struct ibmvfc_event *evt);

0 commit comments

Comments
 (0)