1 // SPDX-License-Identifier: GPL-2.0 2 // KUnit tests for the SPI core DMA mapping error paths. 3 // 4 // A mapping error must clear all SG tables and *_sg_mapped flags while 5 // cur_{tx,rx}_dma_dev identify the devices used for the attempted mapping. 6 // Zero-length transfers make sg_alloc_table() fail with -EINVAL, providing 7 // deterministic failure injection without test hooks. 8 9 #include <kunit/device.h> 10 #include <kunit/test.h> 11 #include <linux/dma-mapping.h> 12 #include <linux/limits.h> 13 #include <linux/spi/spi.h> 14 15 #include "../internals.h" 16 17 MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); 18 19 #define SPI_DMA_TEST_LEN 256 20 #define SPI_DMA_TEST_XFERS 2 21 22 struct spi_dma_test_ctx { 23 struct spi_controller *ctlr; 24 struct spi_device *spi; 25 struct device *dma_dev; 26 struct device *stale_dma_dev; 27 struct spi_transfer xfer[SPI_DMA_TEST_XFERS]; 28 struct spi_message msg; 29 void *buf[SPI_DMA_TEST_XFERS * 2]; 30 }; 31 32 static bool spi_dma_test_can_dma(struct spi_controller *ctlr, 33 struct spi_device *spi, 34 struct spi_transfer *xfer) 35 { 36 /* Opt every transfer into the core DMA mapping path. */ 37 return true; 38 } 39 40 /* 41 * A bare controller is sufficient because the mapping helpers do not 42 * dereference ctlr->dev. With dma_tx and dma_rx unset, both directions use 43 * dma_map_dev, so the controller need not be registered. 44 */ 45 static struct spi_dma_test_ctx *spi_dma_test_ctx_new(struct kunit *test) 46 { 47 struct spi_dma_test_ctx *ctx; 48 49 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); 50 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); 51 52 ctx->dma_dev = kunit_device_register(test, "spi-dma-error-path"); 53 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->dma_dev); 54 ctx->stale_dma_dev = 55 kunit_device_register(test, "spi-dma-stale-device"); 56 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->stale_dma_dev); 57 58 /* Keep both devices valid if an assertion aborts the test. */ 59 KUNIT_ASSERT_EQ(test, 0, 60 dma_coerce_mask_and_coherent(ctx->dma_dev, 61 DMA_BIT_MASK(64))); 62 KUNIT_ASSERT_EQ(test, 0, 63 dma_coerce_mask_and_coherent(ctx->stale_dma_dev, 64 DMA_BIT_MASK(64))); 65 66 ctx->ctlr = kunit_kzalloc(test, sizeof(*ctx->ctlr), GFP_KERNEL); 67 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->ctlr); 68 69 ctx->spi = kunit_kzalloc(test, sizeof(*ctx->spi), GFP_KERNEL); 70 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->spi); 71 72 ctx->ctlr->can_dma = spi_dma_test_can_dma; 73 ctx->ctlr->dma_map_dev = ctx->dma_dev; 74 /* Normally initialized by spi_register_controller(). */ 75 ctx->ctlr->max_dma_len = INT_MAX; 76 77 ctx->spi->controller = ctx->ctlr; 78 spi_message_init(&ctx->msg); 79 ctx->msg.spi = ctx->spi; 80 81 return ctx; 82 } 83 84 static void *spi_dma_test_buf(struct kunit *test, struct spi_dma_test_ctx *ctx, 85 unsigned int slot) 86 { 87 KUNIT_ASSERT_LT(test, slot, ARRAY_SIZE(ctx->buf)); 88 89 ctx->buf[slot] = kunit_kzalloc(test, SPI_DMA_TEST_LEN, GFP_KERNEL); 90 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->buf[slot]); 91 92 return ctx->buf[slot]; 93 } 94 95 /* 96 * Emulate DMA devices retained from an earlier message. Using valid devices 97 * also lets the unfixed path reach the assertions instead of dereferencing 98 * NULL during cleanup. 99 */ 100 static void spi_dma_test_pin_stale_dma_devs(struct spi_dma_test_ctx *ctx) 101 { 102 ctx->ctlr->cur_tx_dma_dev = ctx->stale_dma_dev; 103 ctx->ctlr->cur_rx_dma_dev = ctx->stale_dma_dev; 104 } 105 106 static void spi_dma_test_assert_dma_devs_published(struct kunit *test, 107 struct spi_dma_test_ctx *ctx) 108 { 109 KUNIT_ASSERT_PTR_EQ(test, ctx->ctlr->cur_tx_dma_dev, ctx->dma_dev); 110 KUNIT_ASSERT_PTR_EQ(test, ctx->ctlr->cur_rx_dma_dev, ctx->dma_dev); 111 } 112 113 static void spi_dma_test_assert_nothing_mapped(struct kunit *test, 114 struct spi_dma_test_ctx *ctx, 115 unsigned int nr_xfers) 116 { 117 unsigned int i; 118 119 for (i = 0; i < nr_xfers; i++) { 120 KUNIT_ASSERT_FALSE_MSG(test, ctx->xfer[i].tx_sg_mapped, 121 "xfer[%u] still claims a TX mapping after __spi_map_msg() failed", 122 i); 123 KUNIT_ASSERT_FALSE_MSG(test, ctx->xfer[i].rx_sg_mapped, 124 "xfer[%u] still claims an RX mapping after __spi_map_msg() failed", 125 i); 126 KUNIT_EXPECT_NULL(test, ctx->xfer[i].tx_sg.sgl); 127 KUNIT_EXPECT_EQ(test, ctx->xfer[i].tx_sg.orig_nents, 0U); 128 KUNIT_EXPECT_EQ(test, ctx->xfer[i].tx_sg.nents, 0U); 129 KUNIT_EXPECT_NULL(test, ctx->xfer[i].rx_sg.sgl); 130 KUNIT_EXPECT_EQ(test, ctx->xfer[i].rx_sg.orig_nents, 0U); 131 KUNIT_EXPECT_EQ(test, ctx->xfer[i].rx_sg.nents, 0U); 132 } 133 } 134 135 /* 136 * xfer0 maps TX and RX; zero-length xfer1 then fails its TX mapping. 137 * The failure must unwind xfer0 and update cur_*_dma_dev. 138 */ 139 static void spi_dma_later_tx_fail_rolls_back_earlier(struct kunit *test) 140 { 141 struct spi_dma_test_ctx *ctx = spi_dma_test_ctx_new(test); 142 int ret; 143 144 ctx->xfer[0].tx_buf = spi_dma_test_buf(test, ctx, 0); 145 ctx->xfer[0].rx_buf = spi_dma_test_buf(test, ctx, 1); 146 ctx->xfer[0].len = SPI_DMA_TEST_LEN; 147 148 ctx->xfer[1].tx_buf = spi_dma_test_buf(test, ctx, 2); 149 ctx->xfer[1].rx_buf = NULL; 150 ctx->xfer[1].len = 0; /* forces -EINVAL */ 151 152 spi_message_add_tail(&ctx->xfer[0], &ctx->msg); 153 spi_message_add_tail(&ctx->xfer[1], &ctx->msg); 154 155 spi_dma_test_pin_stale_dma_devs(ctx); 156 157 ret = __spi_map_msg(ctx->ctlr, &ctx->msg); 158 KUNIT_ASSERT_EQ(test, ret, -EINVAL); 159 160 spi_dma_test_assert_dma_devs_published(test, ctx); 161 spi_dma_test_assert_nothing_mapped(test, ctx, SPI_DMA_TEST_XFERS); 162 163 KUNIT_EXPECT_EQ(test, 0, __spi_unmap_msg(ctx->ctlr, &ctx->msg)); 164 } 165 166 /* 167 * xfer0 maps TX and RX; zero-length RX-only xfer1 then fails. 168 * The failure must unwind xfer0 without leaving either mapping flag set. 169 */ 170 static void spi_dma_later_rx_fail_rolls_back_earlier(struct kunit *test) 171 { 172 struct spi_dma_test_ctx *ctx = spi_dma_test_ctx_new(test); 173 int ret; 174 175 ctx->xfer[0].tx_buf = spi_dma_test_buf(test, ctx, 0); 176 ctx->xfer[0].rx_buf = spi_dma_test_buf(test, ctx, 1); 177 ctx->xfer[0].len = SPI_DMA_TEST_LEN; 178 179 ctx->xfer[1].tx_buf = NULL; 180 ctx->xfer[1].rx_buf = spi_dma_test_buf(test, ctx, 2); 181 ctx->xfer[1].len = 0; /* forces -EINVAL */ 182 183 spi_message_add_tail(&ctx->xfer[0], &ctx->msg); 184 spi_message_add_tail(&ctx->xfer[1], &ctx->msg); 185 186 spi_dma_test_pin_stale_dma_devs(ctx); 187 188 ret = __spi_map_msg(ctx->ctlr, &ctx->msg); 189 KUNIT_ASSERT_EQ(test, ret, -EINVAL); 190 191 spi_dma_test_assert_dma_devs_published(test, ctx); 192 spi_dma_test_assert_nothing_mapped(test, ctx, SPI_DMA_TEST_XFERS); 193 194 KUNIT_EXPECT_EQ(test, 0, __spi_unmap_msg(ctx->ctlr, &ctx->msg)); 195 } 196 197 /* Ensure the error unwind does not affect successful mappings. */ 198 static void spi_dma_map_success_publishes_dma_devs(struct kunit *test) 199 { 200 struct spi_dma_test_ctx *ctx = spi_dma_test_ctx_new(test); 201 int ret; 202 203 ctx->xfer[0].tx_buf = spi_dma_test_buf(test, ctx, 0); 204 ctx->xfer[0].rx_buf = spi_dma_test_buf(test, ctx, 1); 205 ctx->xfer[0].len = SPI_DMA_TEST_LEN; 206 207 spi_message_add_tail(&ctx->xfer[0], &ctx->msg); 208 209 ret = __spi_map_msg(ctx->ctlr, &ctx->msg); 210 KUNIT_ASSERT_EQ(test, ret, 0); 211 212 KUNIT_EXPECT_TRUE(test, ctx->xfer[0].tx_sg_mapped); 213 KUNIT_EXPECT_TRUE(test, ctx->xfer[0].rx_sg_mapped); 214 KUNIT_EXPECT_PTR_EQ(test, ctx->ctlr->cur_tx_dma_dev, ctx->dma_dev); 215 KUNIT_EXPECT_PTR_EQ(test, ctx->ctlr->cur_rx_dma_dev, ctx->dma_dev); 216 217 KUNIT_EXPECT_EQ(test, 0, __spi_unmap_msg(ctx->ctlr, &ctx->msg)); 218 219 KUNIT_EXPECT_FALSE(test, ctx->xfer[0].tx_sg_mapped); 220 KUNIT_EXPECT_FALSE(test, ctx->xfer[0].rx_sg_mapped); 221 KUNIT_EXPECT_NULL(test, ctx->xfer[0].tx_sg.sgl); 222 KUNIT_EXPECT_NULL(test, ctx->xfer[0].rx_sg.sgl); 223 } 224 225 /* A transfer without buffers requires no DMA mapping. */ 226 static void spi_dma_map_nothing_is_success(struct kunit *test) 227 { 228 struct spi_dma_test_ctx *ctx = spi_dma_test_ctx_new(test); 229 int ret; 230 231 ctx->xfer[0].tx_buf = NULL; 232 ctx->xfer[0].rx_buf = NULL; 233 ctx->xfer[0].len = SPI_DMA_TEST_LEN; 234 235 spi_message_add_tail(&ctx->xfer[0], &ctx->msg); 236 237 ret = __spi_map_msg(ctx->ctlr, &ctx->msg); 238 KUNIT_EXPECT_EQ(test, ret, 0); 239 240 spi_dma_test_assert_nothing_mapped(test, ctx, 1); 241 } 242 243 static struct kunit_case spi_dma_error_path_cases[] = { 244 KUNIT_CASE(spi_dma_later_tx_fail_rolls_back_earlier), 245 KUNIT_CASE(spi_dma_later_rx_fail_rolls_back_earlier), 246 KUNIT_CASE(spi_dma_map_success_publishes_dma_devs), 247 KUNIT_CASE(spi_dma_map_nothing_is_success), 248 {} 249 }; 250 251 static struct kunit_suite spi_dma_error_path_suite = { 252 .name = "spi_dma", 253 .test_cases = spi_dma_error_path_cases, 254 }; 255 256 kunit_test_suite(spi_dma_error_path_suite); 257 258 MODULE_DESCRIPTION("KUnit tests for SPI core DMA mapping"); 259 MODULE_LICENSE("GPL"); 260