@@ -232,6 +232,8 @@ config SCSI_SCAN_ASYNC
Note that this setting also affects whether resuming from
system suspend will be performed asynchronously.
+source "drivers/scsi/Kconfig.kunit"
+
menu "SCSI Transports"
depends on SCSI
new file mode 100644
@@ -0,0 +1,4 @@
+config SCSI_ERROR_TEST
+ tristate "scsi_error.c unit tests" if !KUNIT_ALL_TESTS
+ depends on SCSI_MOD && KUNIT
+ default KUNIT_ALL_TESTS
@@ -168,6 +168,8 @@ scsi_mod-$(CONFIG_PM) += scsi_pm.o
scsi_mod-$(CONFIG_SCSI_DH) += scsi_dh.o
scsi_mod-$(CONFIG_BLK_DEV_BSG) += scsi_bsg.o
+include $(srctree)/drivers/scsi/Makefile.kunit
+
hv_storvsc-y := storvsc_drv.o
sd_mod-objs := sd.o
new file mode 100644
@@ -0,0 +1 @@
+obj-$(CONFIG_SCSI_ERROR_TEST) += scsi_error_test.o
@@ -27,6 +27,7 @@
#include <linux/blkdev.h>
#include <linux/delay.h>
#include <linux/jiffies.h>
+#include <linux/list_sort.h>
#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
@@ -2186,6 +2187,46 @@ void scsi_eh_ready_devs(struct Scsi_Host *shost,
}
EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
+/*
+ * Comparison function that allows to sort SCSI commands by ULD driver.
+ */
+static int scsi_cmp_uld(void *priv, const struct list_head *_a,
+ const struct list_head *_b)
+{
+ struct scsi_cmnd *a = list_entry(_a, typeof(*a), eh_entry);
+ struct scsi_cmnd *b = list_entry(_b, typeof(*b), eh_entry);
+
+ /* See also the comment above the list_sort() definition. */
+ return scsi_cmd_to_driver(a) > scsi_cmd_to_driver(b);
+}
+
+void scsi_call_prepare_resubmit(struct list_head *done_q)
+{
+ struct scsi_cmnd *scmd, *next;
+
+ /* Sort pending SCSI commands by ULD. */
+ list_sort(NULL, done_q, scsi_cmp_uld);
+
+ /*
+ * Call .eh_prepare_resubmit for each range of commands with identical
+ * ULD driver pointer.
+ */
+ list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
+ struct scsi_driver *uld = scsi_cmd_to_driver(scmd);
+ struct list_head *prev, uld_cmd_list;
+
+ while (&next->eh_entry != done_q &&
+ scsi_cmd_to_driver(next) == uld)
+ next = list_next_entry(next, eh_entry);
+ if (!uld->eh_prepare_resubmit)
+ continue;
+ prev = scmd->eh_entry.prev;
+ list_cut_position(&uld_cmd_list, prev, next->eh_entry.prev);
+ uld->eh_prepare_resubmit(&uld_cmd_list);
+ list_splice(&uld_cmd_list, prev);
+ }
+}
+
/**
* scsi_eh_flush_done_q - finish processed commands or retry them.
* @done_q: list_head of processed commands.
@@ -2194,6 +2235,8 @@ void scsi_eh_flush_done_q(struct list_head *done_q)
{
struct scsi_cmnd *scmd, *next;
+ scsi_call_prepare_resubmit(done_q);
+
list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
list_del_init(&scmd->eh_entry);
if (scsi_device_online(scmd->device) &&
new file mode 100644
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright 2023 Google LLC
+ */
+#include <kunit/test.h>
+#include <scsi/scsi_cmnd.h>
+#include <scsi/scsi_driver.h>
+#include "scsi_priv.h"
+
+static struct kunit *kunit_test;
+
+static struct scsi_driver *uld1, *uld2, *uld3;
+
+static void uld1_prepare_resubmit(struct list_head *cmd_list)
+{
+ struct scsi_cmnd *cmd;
+
+ KUNIT_EXPECT_EQ(kunit_test, list_count_nodes(cmd_list), 2);
+ list_for_each_entry(cmd, cmd_list, eh_entry)
+ KUNIT_EXPECT_PTR_EQ(kunit_test, scsi_cmd_to_driver(cmd), uld1);
+}
+
+static void uld2_prepare_resubmit(struct list_head *cmd_list)
+{
+ struct scsi_cmnd *cmd;
+
+ KUNIT_EXPECT_EQ(kunit_test, list_count_nodes(cmd_list), 2);
+ list_for_each_entry(cmd, cmd_list, eh_entry)
+ KUNIT_EXPECT_PTR_EQ(kunit_test, scsi_cmd_to_driver(cmd), uld2);
+}
+
+static void test_prepare_resubmit(struct kunit *test)
+{
+ static struct scsi_cmnd cmd1, cmd2, cmd3, cmd4, cmd5, cmd6;
+ static struct scsi_device dev1, dev2, dev3;
+ struct scsi_driver *uld;
+ LIST_HEAD(cmd_list);
+
+ uld = kzalloc(3 * sizeof(uld), GFP_KERNEL);
+ uld1 = &uld[0];
+ uld1->eh_prepare_resubmit = uld1_prepare_resubmit;
+ uld2 = &uld[1];
+ uld2->eh_prepare_resubmit = uld2_prepare_resubmit;
+ uld3 = &uld[2];
+ dev1.sdev_gendev.driver = &uld1->gendrv;
+ dev2.sdev_gendev.driver = &uld2->gendrv;
+ dev3.sdev_gendev.driver = &uld3->gendrv;
+ cmd1.device = &dev1;
+ cmd2.device = &dev1;
+ cmd3.device = &dev2;
+ cmd4.device = &dev2;
+ cmd5.device = &dev3;
+ cmd6.device = &dev3;
+ list_add_tail(&cmd1.eh_entry, &cmd_list);
+ list_add_tail(&cmd3.eh_entry, &cmd_list);
+ list_add_tail(&cmd5.eh_entry, &cmd_list);
+ list_add_tail(&cmd2.eh_entry, &cmd_list);
+ list_add_tail(&cmd4.eh_entry, &cmd_list);
+ list_add_tail(&cmd6.eh_entry, &cmd_list);
+
+ KUNIT_EXPECT_EQ(test, list_count_nodes(&cmd_list), 6);
+ kunit_test = test;
+ scsi_call_prepare_resubmit(&cmd_list);
+ kunit_test = NULL;
+ KUNIT_EXPECT_EQ(test, list_count_nodes(&cmd_list), 6);
+ KUNIT_EXPECT_TRUE(test, uld1 < uld2);
+ KUNIT_EXPECT_TRUE(test, uld2 < uld3);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next, &cmd1.eh_entry);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next->next, &cmd2.eh_entry);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next->next->next, &cmd3.eh_entry);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next->next->next->next,
+ &cmd4.eh_entry);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next->next->next->next->next,
+ &cmd5.eh_entry);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next->next->next->next->next->next,
+ &cmd6.eh_entry);
+ kfree(uld);
+}
+
+static struct kunit_case prepare_resubmit_test_cases[] = {
+ KUNIT_CASE(test_prepare_resubmit),
+ {}
+};
+
+static struct kunit_suite prepare_resubmit_test_suite = {
+ .name = "prepare_resubmit",
+ .test_cases = prepare_resubmit_test_cases,
+};
+kunit_test_suite(prepare_resubmit_test_suite);
+
+MODULE_AUTHOR("Bart Van Assche");
+MODULE_LICENSE("GPL");
@@ -101,6 +101,7 @@ int scsi_eh_get_sense(struct list_head *work_q,
struct list_head *done_q);
bool scsi_noretry_cmd(struct scsi_cmnd *scmd);
void scsi_eh_done(struct scsi_cmnd *scmd);
+void scsi_call_prepare_resubmit(struct list_head *done_q);
/* scsi_lib.c */
extern int scsi_maybe_unblock_host(struct scsi_device *sdev);
@@ -18,6 +18,7 @@ struct scsi_driver {
int (*done)(struct scsi_cmnd *);
int (*eh_action)(struct scsi_cmnd *, int);
void (*eh_reset)(struct scsi_cmnd *);
+ void (*eh_prepare_resubmit)(struct list_head *cmd_list);
};
#define to_scsi_driver(drv) \
container_of((drv), struct scsi_driver, gendrv)
Make the error handler call .eh_prepare_resubmit() before resubmitting commands. A later patch will use this functionality to sort SCSI commands per LBA from inside the SCSI disk driver. Cc: Martin K. Petersen <martin.petersen@oracle.com> Cc: Damien Le Moal <dlemoal@kernel.org> Cc: Christoph Hellwig <hch@lst.de> Cc: Ming Lei <ming.lei@redhat.com> Signed-off-by: Bart Van Assche <bvanassche@acm.org> --- drivers/scsi/Kconfig | 2 + drivers/scsi/Kconfig.kunit | 4 ++ drivers/scsi/Makefile | 2 + drivers/scsi/Makefile.kunit | 1 + drivers/scsi/scsi_error.c | 43 ++++++++++++++++ drivers/scsi/scsi_error_test.c | 92 ++++++++++++++++++++++++++++++++++ drivers/scsi/scsi_priv.h | 1 + include/scsi/scsi_driver.h | 1 + 8 files changed, 146 insertions(+) create mode 100644 drivers/scsi/Kconfig.kunit create mode 100644 drivers/scsi/Makefile.kunit create mode 100644 drivers/scsi/scsi_error_test.c