From: Bart Van Assche <bvanassche@acm.org>
To: "Martin K . Petersen" <martin.petersen@oracle.com>
Cc: linux-scsi@vger.kernel.org, linux-block@vger.kernel.org,
Jens Axboe <axboe@kernel.dk>, Christoph Hellwig <hch@lst.de>,
Bart Van Assche <bvanassche@acm.org>,
Damien Le Moal <dlemoal@kernel.org>,
Ming Lei <ming.lei@redhat.com>,
"James E.J. Bottomley" <jejb@linux.ibm.com>
Subject: [PATCH v15 07/19] scsi: core: Add unit tests for scsi_call_prepare_resubmit()
Date: Tue, 14 Nov 2023 13:16:15 -0800 [thread overview]
Message-ID: <20231114211804.1449162-8-bvanassche@acm.org> (raw)
In-Reply-To: <20231114211804.1449162-1-bvanassche@acm.org>
Triggering all code paths in scsi_call_prepare_resubmit() via manual
testing is difficult. Hence add unit tests for this function.
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 | 5 +
drivers/scsi/scsi_error.c | 4 +
drivers/scsi/scsi_error_test.c | 233 +++++++++++++++++++++++++++++++++
3 files changed, 242 insertions(+)
create mode 100644 drivers/scsi/scsi_error_test.c
diff --git a/drivers/scsi/Kconfig b/drivers/scsi/Kconfig
index addac7fbe37b..2e57afdbbc4d 100644
--- a/drivers/scsi/Kconfig
+++ b/drivers/scsi/Kconfig
@@ -232,6 +232,11 @@ config SCSI_SCAN_ASYNC
Note that this setting also affects whether resuming from
system suspend will be performed asynchronously.
+config SCSI_ERROR_TEST
+ tristate "scsi_error.c unit tests" if !KUNIT_ALL_TESTS
+ depends on SCSI && KUNIT
+ default KUNIT_ALL_TESTS
+
menu "SCSI Transports"
depends on SCSI
diff --git a/drivers/scsi/scsi_error.c b/drivers/scsi/scsi_error.c
index 4214d7b79b06..3a2643293abf 100644
--- a/drivers/scsi/scsi_error.c
+++ b/drivers/scsi/scsi_error.c
@@ -2621,3 +2621,7 @@ bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
}
}
EXPORT_SYMBOL(scsi_get_sense_info_fld);
+
+#ifdef CONFIG_SCSI_ERROR_TEST
+#include "scsi_error_test.c"
+#endif
diff --git a/drivers/scsi/scsi_error_test.c b/drivers/scsi/scsi_error_test.c
new file mode 100644
index 000000000000..46362766ad48
--- /dev/null
+++ b/drivers/scsi/scsi_error_test.c
@@ -0,0 +1,233 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright 2023 Google LLC
+ */
+#include <kunit/test.h>
+#include <linux/cleanup.h>
+#include <scsi/scsi_cmnd.h>
+#include <scsi/scsi_driver.h>
+#include <scsi/scsi_host.h>
+
+#define ALLOC(type, ...) \
+ ({ \
+ type *obj; \
+ obj = kmalloc(sizeof(*obj), GFP_KERNEL); \
+ if (obj) \
+ *obj = (type){ __VA_ARGS__ }; \
+ obj; \
+ })
+
+#define ALLOC_DISK(...) ALLOC(struct gendisk, __VA_ARGS__)
+
+#define ALLOC_Q(...) ALLOC(struct request_queue, __VA_ARGS__)
+
+#define ALLOC_SDEV(...) ALLOC(struct scsi_device, __VA_ARGS__)
+
+#define ALLOC_CMD(...) ALLOC(struct rq_and_cmd, __VA_ARGS__)
+
+static struct kunit *kunit_test;
+
+static void uld_prepare_resubmit(struct list_head *cmd_list)
+{
+ /* This function must not be called. */
+ KUNIT_EXPECT_TRUE(kunit_test, false);
+}
+
+/*
+ * Verify that .eh_prepare_resubmit() is not called if needs_prepare_resubmit is
+ * false.
+ */
+static void test_prepare_resubmit1(struct kunit *test)
+{
+ struct gendisk *disk __free(kfree) = ALLOC_DISK();
+ struct request_queue *q __free(kfree) = ALLOC_Q(
+ .limits = {
+ .driver_preserves_write_order = false,
+ .use_zone_write_lock = true,
+ .zoned = BLK_ZONED_HM,
+ }
+ );
+ static struct scsi_driver uld = {
+ .eh_prepare_resubmit = uld_prepare_resubmit,
+ };
+ static const struct scsi_host_template host_template;
+ static struct Scsi_Host host = {
+ .hostt = &host_template,
+ };
+ struct scsi_device *dev __free(kfree) = ALLOC_SDEV(
+ .request_queue = q,
+ .sdev_gendev.driver = &uld.gendrv,
+ .host = &host,
+ );
+ struct rq_and_cmd {
+ struct request rq;
+ struct scsi_cmnd cmd;
+ } *cmd1 __free(kfree) = NULL, *cmd2 __free(kfree);
+ LIST_HEAD(cmd_list);
+
+ BUILD_BUG_ON(scsi_cmd_to_rq(&cmd1->cmd) != &cmd1->rq);
+
+ q->disk = disk;
+ disk->queue = q;
+ cmd1 = ALLOC_CMD(
+ .rq = {
+ .q = q,
+ .cmd_flags = REQ_OP_WRITE,
+ .__sector = 2,
+ },
+ .cmd.device = dev,
+ );
+ cmd2 = ALLOC_CMD(
+ .rq = {
+ .q = q,
+ .cmd_flags = REQ_OP_WRITE,
+ .__sector = 1,
+ },
+ .cmd.device = dev,
+ );
+ list_add_tail(&cmd1->cmd.eh_entry, &cmd_list);
+ list_add_tail(&cmd2->cmd.eh_entry, &cmd_list);
+
+ KUNIT_EXPECT_EQ(test, list_count_nodes(&cmd_list), 2);
+ kunit_test = test;
+ scsi_call_prepare_resubmit(&host, &cmd_list);
+ kunit_test = NULL;
+ KUNIT_EXPECT_EQ(test, list_count_nodes(&cmd_list), 2);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next, &cmd1->cmd.eh_entry);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next->next, &cmd2->cmd.eh_entry);
+}
+
+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_resubmit2(struct kunit *test)
+{
+ static const struct scsi_host_template host_template = {
+ .needs_prepare_resubmit = true,
+ };
+ static struct Scsi_Host host = {
+ .hostt = &host_template,
+ };
+ struct gendisk *disk __free(kfree);
+ struct request_queue *q __free(kfree) =
+ ALLOC_Q(.limits = {
+ .driver_preserves_write_order = true,
+ .use_zone_write_lock = false,
+ .zoned = BLK_ZONED_HM,
+ });
+ struct rq_and_cmd {
+ struct request rq;
+ struct scsi_cmnd cmd;
+ } *cmd1 __free(kfree), *cmd2 __free(kfree), *cmd3 __free(kfree),
+ *cmd4 __free(kfree), *cmd5 __free(kfree), *cmd6 __free(kfree);
+ struct scsi_device *dev1 __free(kfree), *dev2 __free(kfree),
+ *dev3 __free(kfree);
+ struct scsi_driver *uld __free(kfree);
+ LIST_HEAD(cmd_list);
+
+ BUILD_BUG_ON(scsi_cmd_to_rq(&cmd1->cmd) != &cmd1->rq);
+
+ 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];
+ disk = ALLOC_DISK();
+ disk->queue = q;
+ q->disk = disk;
+ dev1 = ALLOC_SDEV(.sdev_gendev.driver = &uld1->gendrv,
+ .request_queue = q, .host = &host);
+ dev2 = ALLOC_SDEV(.sdev_gendev.driver = &uld2->gendrv,
+ .request_queue = q, .host = &host);
+ dev3 = ALLOC_SDEV(.sdev_gendev.driver = &uld3->gendrv,
+ .request_queue = q, .host = &host);
+ cmd1 = ALLOC_CMD(
+ .rq = {
+ .q = q,
+ .cmd_flags = REQ_OP_WRITE,
+ .__sector = 3,
+ },
+ .cmd.device = dev1,
+ );
+ cmd2 = ALLOC_CMD();
+ *cmd2 = *cmd1;
+ cmd2->rq.__sector = 4;
+ cmd3 = ALLOC_CMD(
+ .rq = {
+ .q = q,
+ .cmd_flags = REQ_OP_WRITE,
+ .__sector = 1,
+ },
+ .cmd.device = dev2,
+ );
+ cmd4 = kmemdup(cmd3, sizeof(*cmd3), GFP_KERNEL);
+ cmd4->rq.__sector = 2;
+ cmd5 = ALLOC_CMD(
+ .rq = {
+ .q = q,
+ .cmd_flags = REQ_OP_WRITE,
+ .__sector = 5,
+ },
+ .cmd.device = dev3,
+ );
+ cmd6 = kmemdup(cmd5, sizeof(*cmd3), GFP_KERNEL);
+ cmd6->rq.__sector = 6;
+ list_add_tail(&cmd3->cmd.eh_entry, &cmd_list);
+ list_add_tail(&cmd1->cmd.eh_entry, &cmd_list);
+ list_add_tail(&cmd2->cmd.eh_entry, &cmd_list);
+ list_add_tail(&cmd5->cmd.eh_entry, &cmd_list);
+ list_add_tail(&cmd6->cmd.eh_entry, &cmd_list);
+ list_add_tail(&cmd4->cmd.eh_entry, &cmd_list);
+
+ KUNIT_EXPECT_EQ(test, list_count_nodes(&cmd_list), 6);
+ kunit_test = test;
+ scsi_call_prepare_resubmit(&host, &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->cmd.eh_entry);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next->next, &cmd2->cmd.eh_entry);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next->next->next,
+ &cmd3->cmd.eh_entry);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next->next->next->next,
+ &cmd4->cmd.eh_entry);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next->next->next->next->next,
+ &cmd5->cmd.eh_entry);
+ KUNIT_EXPECT_PTR_EQ(test, cmd_list.next->next->next->next->next->next,
+ &cmd6->cmd.eh_entry);
+}
+
+static struct kunit_case prepare_resubmit_test_cases[] = {
+ KUNIT_CASE(test_prepare_resubmit1),
+ KUNIT_CASE(test_prepare_resubmit2),
+ {}
+};
+
+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_DESCRIPTION("scsi_call_prepare_resubmit() unit tests");
+MODULE_AUTHOR("Bart Van Assche");
+MODULE_LICENSE("GPL");
next prev parent reply other threads:[~2023-11-14 21:18 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-14 21:16 [PATCH v15 00/19] Improve write performance for zoned UFS devices Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 01/19] block: Introduce more member variables related to zone write locking Bart Van Assche
2023-11-19 23:29 ` Damien Le Moal
2023-11-20 20:44 ` Bart Van Assche
2023-11-20 23:02 ` Damien Le Moal
2023-11-20 23:58 ` Bart Van Assche
2023-11-21 1:21 ` Damien Le Moal
2023-11-21 2:12 ` Damien Le Moal
2023-11-14 21:16 ` [PATCH v15 02/19] block: Only use write locking if necessary Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 03/19] block: Preserve the order of requeued zoned writes Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 04/19] block/mq-deadline: Only use zone locking if necessary Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 05/19] scsi: Pass SCSI host pointer to scsi_eh_flush_done_q() Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 06/19] scsi: core: Introduce a mechanism for reordering requests in the error handler Bart Van Assche
2023-11-14 21:16 ` Bart Van Assche [this message]
2023-11-14 21:16 ` [PATCH v15 08/19] scsi: sd: Support sorting commands by LBA before resubmitting Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 09/19] scsi: sd: Add a unit test for sd_cmp_sector() Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 10/19] scsi: core: Retry unaligned zoned writes Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 11/19] scsi: sd_zbc: Only require an I/O scheduler if needed Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 12/19] scsi: scsi_debug: Add the preserves_write_order module parameter Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 13/19] scsi: scsi_debug: Support injecting unaligned write errors Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 14/19] scsi: ufs: hisi: Rework the code that disables auto-hibernation Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 15/19] scsi: ufs: Rename ufshcd_auto_hibern8_enable() and make it static Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 16/19] scsi: ufs: Change the return type of ufshcd_auto_hibern8_update() Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 17/19] scsi: ufs: Simplify ufshcd_auto_hibern8_update() Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 18/19] scsi: ufs: Forbid auto-hibernation without I/O scheduler Bart Van Assche
2023-11-14 21:16 ` [PATCH v15 19/19] scsi: ufs: Inform the block layer about write ordering Bart Van Assche
2023-11-28 1:45 ` Can Guo
2023-11-28 21:49 ` Bart Van Assche
2023-11-27 7:09 ` [PATCH v15 00/19] Improve write performance for zoned UFS devices Christoph Hellwig
2023-11-27 19:35 ` [PATCH v15 00/19] Improve write performance for zoned UFS devices Bart Van Assche
2023-11-28 12:53 ` Christoph Hellwig
2023-11-28 17:36 ` Bart Van Assche
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231114211804.1449162-8-bvanassche@acm.org \
--to=bvanassche@acm.org \
--cc=axboe@kernel.dk \
--cc=dlemoal@kernel.org \
--cc=hch@lst.de \
--cc=jejb@linux.ibm.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=ming.lei@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox