Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / lib / kunit_iov_iter.c
blob13e15687675a84b7bf92a08513d8f05f6b3ea04e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* I/O iterator tests. This can only test kernel-backed iterator types.
4 * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
12 #include <linux/mm.h>
13 #include <linux/uio.h>
14 #include <linux/bvec.h>
15 #include <linux/folio_queue.h>
16 #include <kunit/test.h>
18 MODULE_DESCRIPTION("iov_iter testing");
19 MODULE_AUTHOR("David Howells <dhowells@redhat.com>");
20 MODULE_LICENSE("GPL");
22 struct kvec_test_range {
23 int from, to;
26 static const struct kvec_test_range kvec_test_ranges[] = {
27 { 0x00002, 0x00002 },
28 { 0x00027, 0x03000 },
29 { 0x05193, 0x18794 },
30 { 0x20000, 0x20000 },
31 { 0x20000, 0x24000 },
32 { 0x24000, 0x27001 },
33 { 0x29000, 0xffffb },
34 { 0xffffd, 0xffffe },
35 { -1 }
38 static inline u8 pattern(unsigned long x)
40 return x & 0xff;
43 static void iov_kunit_unmap(void *data)
45 vunmap(data);
48 static void *__init iov_kunit_create_buffer(struct kunit *test,
49 struct page ***ppages,
50 size_t npages)
52 struct page **pages;
53 unsigned long got;
54 void *buffer;
56 pages = kunit_kcalloc(test, npages, sizeof(struct page *), GFP_KERNEL);
57 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pages);
58 *ppages = pages;
60 got = alloc_pages_bulk_array(GFP_KERNEL, npages, pages);
61 if (got != npages) {
62 release_pages(pages, got);
63 KUNIT_ASSERT_EQ(test, got, npages);
66 for (int i = 0; i < npages; i++)
67 pages[i]->index = i;
69 buffer = vmap(pages, npages, VM_MAP | VM_MAP_PUT_PAGES, PAGE_KERNEL);
70 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buffer);
72 kunit_add_action_or_reset(test, iov_kunit_unmap, buffer);
73 return buffer;
76 static void __init iov_kunit_load_kvec(struct kunit *test,
77 struct iov_iter *iter, int dir,
78 struct kvec *kvec, unsigned int kvmax,
79 void *buffer, size_t bufsize,
80 const struct kvec_test_range *pr)
82 size_t size = 0;
83 int i;
85 for (i = 0; i < kvmax; i++, pr++) {
86 if (pr->from < 0)
87 break;
88 KUNIT_ASSERT_GE(test, pr->to, pr->from);
89 KUNIT_ASSERT_LE(test, pr->to, bufsize);
90 kvec[i].iov_base = buffer + pr->from;
91 kvec[i].iov_len = pr->to - pr->from;
92 size += pr->to - pr->from;
94 KUNIT_ASSERT_LE(test, size, bufsize);
96 iov_iter_kvec(iter, dir, kvec, i, size);
100 * Test copying to a ITER_KVEC-type iterator.
102 static void __init iov_kunit_copy_to_kvec(struct kunit *test)
104 const struct kvec_test_range *pr;
105 struct iov_iter iter;
106 struct page **spages, **bpages;
107 struct kvec kvec[8];
108 u8 *scratch, *buffer;
109 size_t bufsize, npages, size, copied;
110 int i, patt;
112 bufsize = 0x100000;
113 npages = bufsize / PAGE_SIZE;
115 scratch = iov_kunit_create_buffer(test, &spages, npages);
116 for (i = 0; i < bufsize; i++)
117 scratch[i] = pattern(i);
119 buffer = iov_kunit_create_buffer(test, &bpages, npages);
120 memset(buffer, 0, bufsize);
122 iov_kunit_load_kvec(test, &iter, READ, kvec, ARRAY_SIZE(kvec),
123 buffer, bufsize, kvec_test_ranges);
124 size = iter.count;
126 copied = copy_to_iter(scratch, size, &iter);
128 KUNIT_EXPECT_EQ(test, copied, size);
129 KUNIT_EXPECT_EQ(test, iter.count, 0);
130 KUNIT_EXPECT_EQ(test, iter.nr_segs, 0);
132 /* Build the expected image in the scratch buffer. */
133 patt = 0;
134 memset(scratch, 0, bufsize);
135 for (pr = kvec_test_ranges; pr->from >= 0; pr++)
136 for (i = pr->from; i < pr->to; i++)
137 scratch[i] = pattern(patt++);
139 /* Compare the images */
140 for (i = 0; i < bufsize; i++) {
141 KUNIT_EXPECT_EQ_MSG(test, buffer[i], scratch[i], "at i=%x", i);
142 if (buffer[i] != scratch[i])
143 return;
146 KUNIT_SUCCEED(test);
150 * Test copying from a ITER_KVEC-type iterator.
152 static void __init iov_kunit_copy_from_kvec(struct kunit *test)
154 const struct kvec_test_range *pr;
155 struct iov_iter iter;
156 struct page **spages, **bpages;
157 struct kvec kvec[8];
158 u8 *scratch, *buffer;
159 size_t bufsize, npages, size, copied;
160 int i, j;
162 bufsize = 0x100000;
163 npages = bufsize / PAGE_SIZE;
165 buffer = iov_kunit_create_buffer(test, &bpages, npages);
166 for (i = 0; i < bufsize; i++)
167 buffer[i] = pattern(i);
169 scratch = iov_kunit_create_buffer(test, &spages, npages);
170 memset(scratch, 0, bufsize);
172 iov_kunit_load_kvec(test, &iter, WRITE, kvec, ARRAY_SIZE(kvec),
173 buffer, bufsize, kvec_test_ranges);
174 size = min(iter.count, bufsize);
176 copied = copy_from_iter(scratch, size, &iter);
178 KUNIT_EXPECT_EQ(test, copied, size);
179 KUNIT_EXPECT_EQ(test, iter.count, 0);
180 KUNIT_EXPECT_EQ(test, iter.nr_segs, 0);
182 /* Build the expected image in the main buffer. */
183 i = 0;
184 memset(buffer, 0, bufsize);
185 for (pr = kvec_test_ranges; pr->from >= 0; pr++) {
186 for (j = pr->from; j < pr->to; j++) {
187 buffer[i++] = pattern(j);
188 if (i >= bufsize)
189 goto stop;
192 stop:
194 /* Compare the images */
195 for (i = 0; i < bufsize; i++) {
196 KUNIT_EXPECT_EQ_MSG(test, scratch[i], buffer[i], "at i=%x", i);
197 if (scratch[i] != buffer[i])
198 return;
201 KUNIT_SUCCEED(test);
204 struct bvec_test_range {
205 int page, from, to;
208 static const struct bvec_test_range bvec_test_ranges[] = {
209 { 0, 0x0002, 0x0002 },
210 { 1, 0x0027, 0x0893 },
211 { 2, 0x0193, 0x0794 },
212 { 3, 0x0000, 0x1000 },
213 { 4, 0x0000, 0x1000 },
214 { 5, 0x0000, 0x1000 },
215 { 6, 0x0000, 0x0ffb },
216 { 6, 0x0ffd, 0x0ffe },
217 { -1, -1, -1 }
220 static void __init iov_kunit_load_bvec(struct kunit *test,
221 struct iov_iter *iter, int dir,
222 struct bio_vec *bvec, unsigned int bvmax,
223 struct page **pages, size_t npages,
224 size_t bufsize,
225 const struct bvec_test_range *pr)
227 struct page *can_merge = NULL, *page;
228 size_t size = 0;
229 int i;
231 for (i = 0; i < bvmax; i++, pr++) {
232 if (pr->from < 0)
233 break;
234 KUNIT_ASSERT_LT(test, pr->page, npages);
235 KUNIT_ASSERT_LT(test, pr->page * PAGE_SIZE, bufsize);
236 KUNIT_ASSERT_GE(test, pr->from, 0);
237 KUNIT_ASSERT_GE(test, pr->to, pr->from);
238 KUNIT_ASSERT_LE(test, pr->to, PAGE_SIZE);
240 page = pages[pr->page];
241 if (pr->from == 0 && pr->from != pr->to && page == can_merge) {
242 i--;
243 bvec[i].bv_len += pr->to;
244 } else {
245 bvec_set_page(&bvec[i], page, pr->to - pr->from, pr->from);
248 size += pr->to - pr->from;
249 if ((pr->to & ~PAGE_MASK) == 0)
250 can_merge = page + pr->to / PAGE_SIZE;
251 else
252 can_merge = NULL;
255 iov_iter_bvec(iter, dir, bvec, i, size);
259 * Test copying to a ITER_BVEC-type iterator.
261 static void __init iov_kunit_copy_to_bvec(struct kunit *test)
263 const struct bvec_test_range *pr;
264 struct iov_iter iter;
265 struct bio_vec bvec[8];
266 struct page **spages, **bpages;
267 u8 *scratch, *buffer;
268 size_t bufsize, npages, size, copied;
269 int i, b, patt;
271 bufsize = 0x100000;
272 npages = bufsize / PAGE_SIZE;
274 scratch = iov_kunit_create_buffer(test, &spages, npages);
275 for (i = 0; i < bufsize; i++)
276 scratch[i] = pattern(i);
278 buffer = iov_kunit_create_buffer(test, &bpages, npages);
279 memset(buffer, 0, bufsize);
281 iov_kunit_load_bvec(test, &iter, READ, bvec, ARRAY_SIZE(bvec),
282 bpages, npages, bufsize, bvec_test_ranges);
283 size = iter.count;
285 copied = copy_to_iter(scratch, size, &iter);
287 KUNIT_EXPECT_EQ(test, copied, size);
288 KUNIT_EXPECT_EQ(test, iter.count, 0);
289 KUNIT_EXPECT_EQ(test, iter.nr_segs, 0);
291 /* Build the expected image in the scratch buffer. */
292 b = 0;
293 patt = 0;
294 memset(scratch, 0, bufsize);
295 for (pr = bvec_test_ranges; pr->from >= 0; pr++, b++) {
296 u8 *p = scratch + pr->page * PAGE_SIZE;
298 for (i = pr->from; i < pr->to; i++)
299 p[i] = pattern(patt++);
302 /* Compare the images */
303 for (i = 0; i < bufsize; i++) {
304 KUNIT_EXPECT_EQ_MSG(test, buffer[i], scratch[i], "at i=%x", i);
305 if (buffer[i] != scratch[i])
306 return;
309 KUNIT_SUCCEED(test);
313 * Test copying from a ITER_BVEC-type iterator.
315 static void __init iov_kunit_copy_from_bvec(struct kunit *test)
317 const struct bvec_test_range *pr;
318 struct iov_iter iter;
319 struct bio_vec bvec[8];
320 struct page **spages, **bpages;
321 u8 *scratch, *buffer;
322 size_t bufsize, npages, size, copied;
323 int i, j;
325 bufsize = 0x100000;
326 npages = bufsize / PAGE_SIZE;
328 buffer = iov_kunit_create_buffer(test, &bpages, npages);
329 for (i = 0; i < bufsize; i++)
330 buffer[i] = pattern(i);
332 scratch = iov_kunit_create_buffer(test, &spages, npages);
333 memset(scratch, 0, bufsize);
335 iov_kunit_load_bvec(test, &iter, WRITE, bvec, ARRAY_SIZE(bvec),
336 bpages, npages, bufsize, bvec_test_ranges);
337 size = iter.count;
339 copied = copy_from_iter(scratch, size, &iter);
341 KUNIT_EXPECT_EQ(test, copied, size);
342 KUNIT_EXPECT_EQ(test, iter.count, 0);
343 KUNIT_EXPECT_EQ(test, iter.nr_segs, 0);
345 /* Build the expected image in the main buffer. */
346 i = 0;
347 memset(buffer, 0, bufsize);
348 for (pr = bvec_test_ranges; pr->from >= 0; pr++) {
349 size_t patt = pr->page * PAGE_SIZE;
351 for (j = pr->from; j < pr->to; j++) {
352 buffer[i++] = pattern(patt + j);
353 if (i >= bufsize)
354 goto stop;
357 stop:
359 /* Compare the images */
360 for (i = 0; i < bufsize; i++) {
361 KUNIT_EXPECT_EQ_MSG(test, scratch[i], buffer[i], "at i=%x", i);
362 if (scratch[i] != buffer[i])
363 return;
366 KUNIT_SUCCEED(test);
369 static void iov_kunit_destroy_folioq(void *data)
371 struct folio_queue *folioq, *next;
373 for (folioq = data; folioq; folioq = next) {
374 next = folioq->next;
375 for (int i = 0; i < folioq_nr_slots(folioq); i++)
376 if (folioq_folio(folioq, i))
377 folio_put(folioq_folio(folioq, i));
378 kfree(folioq);
382 static void __init iov_kunit_load_folioq(struct kunit *test,
383 struct iov_iter *iter, int dir,
384 struct folio_queue *folioq,
385 struct page **pages, size_t npages)
387 struct folio_queue *p = folioq;
388 size_t size = 0;
389 int i;
391 for (i = 0; i < npages; i++) {
392 if (folioq_full(p)) {
393 p->next = kzalloc(sizeof(struct folio_queue), GFP_KERNEL);
394 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p->next);
395 folioq_init(p->next);
396 p->next->prev = p;
397 p = p->next;
399 folioq_append(p, page_folio(pages[i]));
400 size += PAGE_SIZE;
402 iov_iter_folio_queue(iter, dir, folioq, 0, 0, size);
405 static struct folio_queue *iov_kunit_create_folioq(struct kunit *test)
407 struct folio_queue *folioq;
409 folioq = kzalloc(sizeof(struct folio_queue), GFP_KERNEL);
410 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, folioq);
411 kunit_add_action_or_reset(test, iov_kunit_destroy_folioq, folioq);
412 folioq_init(folioq);
413 return folioq;
417 * Test copying to a ITER_FOLIOQ-type iterator.
419 static void __init iov_kunit_copy_to_folioq(struct kunit *test)
421 const struct kvec_test_range *pr;
422 struct iov_iter iter;
423 struct folio_queue *folioq;
424 struct page **spages, **bpages;
425 u8 *scratch, *buffer;
426 size_t bufsize, npages, size, copied;
427 int i, patt;
429 bufsize = 0x100000;
430 npages = bufsize / PAGE_SIZE;
432 folioq = iov_kunit_create_folioq(test);
434 scratch = iov_kunit_create_buffer(test, &spages, npages);
435 for (i = 0; i < bufsize; i++)
436 scratch[i] = pattern(i);
438 buffer = iov_kunit_create_buffer(test, &bpages, npages);
439 memset(buffer, 0, bufsize);
441 iov_kunit_load_folioq(test, &iter, READ, folioq, bpages, npages);
443 i = 0;
444 for (pr = kvec_test_ranges; pr->from >= 0; pr++) {
445 size = pr->to - pr->from;
446 KUNIT_ASSERT_LE(test, pr->to, bufsize);
448 iov_iter_folio_queue(&iter, READ, folioq, 0, 0, pr->to);
449 iov_iter_advance(&iter, pr->from);
450 copied = copy_to_iter(scratch + i, size, &iter);
452 KUNIT_EXPECT_EQ(test, copied, size);
453 KUNIT_EXPECT_EQ(test, iter.count, 0);
454 KUNIT_EXPECT_EQ(test, iter.iov_offset, pr->to % PAGE_SIZE);
455 i += size;
456 if (test->status == KUNIT_FAILURE)
457 goto stop;
460 /* Build the expected image in the scratch buffer. */
461 patt = 0;
462 memset(scratch, 0, bufsize);
463 for (pr = kvec_test_ranges; pr->from >= 0; pr++)
464 for (i = pr->from; i < pr->to; i++)
465 scratch[i] = pattern(patt++);
467 /* Compare the images */
468 for (i = 0; i < bufsize; i++) {
469 KUNIT_EXPECT_EQ_MSG(test, buffer[i], scratch[i], "at i=%x", i);
470 if (buffer[i] != scratch[i])
471 return;
474 stop:
475 KUNIT_SUCCEED(test);
479 * Test copying from a ITER_FOLIOQ-type iterator.
481 static void __init iov_kunit_copy_from_folioq(struct kunit *test)
483 const struct kvec_test_range *pr;
484 struct iov_iter iter;
485 struct folio_queue *folioq;
486 struct page **spages, **bpages;
487 u8 *scratch, *buffer;
488 size_t bufsize, npages, size, copied;
489 int i, j;
491 bufsize = 0x100000;
492 npages = bufsize / PAGE_SIZE;
494 folioq = iov_kunit_create_folioq(test);
496 buffer = iov_kunit_create_buffer(test, &bpages, npages);
497 for (i = 0; i < bufsize; i++)
498 buffer[i] = pattern(i);
500 scratch = iov_kunit_create_buffer(test, &spages, npages);
501 memset(scratch, 0, bufsize);
503 iov_kunit_load_folioq(test, &iter, READ, folioq, bpages, npages);
505 i = 0;
506 for (pr = kvec_test_ranges; pr->from >= 0; pr++) {
507 size = pr->to - pr->from;
508 KUNIT_ASSERT_LE(test, pr->to, bufsize);
510 iov_iter_folio_queue(&iter, WRITE, folioq, 0, 0, pr->to);
511 iov_iter_advance(&iter, pr->from);
512 copied = copy_from_iter(scratch + i, size, &iter);
514 KUNIT_EXPECT_EQ(test, copied, size);
515 KUNIT_EXPECT_EQ(test, iter.count, 0);
516 KUNIT_EXPECT_EQ(test, iter.iov_offset, pr->to % PAGE_SIZE);
517 i += size;
520 /* Build the expected image in the main buffer. */
521 i = 0;
522 memset(buffer, 0, bufsize);
523 for (pr = kvec_test_ranges; pr->from >= 0; pr++) {
524 for (j = pr->from; j < pr->to; j++) {
525 buffer[i++] = pattern(j);
526 if (i >= bufsize)
527 goto stop;
530 stop:
532 /* Compare the images */
533 for (i = 0; i < bufsize; i++) {
534 KUNIT_EXPECT_EQ_MSG(test, scratch[i], buffer[i], "at i=%x", i);
535 if (scratch[i] != buffer[i])
536 return;
539 KUNIT_SUCCEED(test);
542 static void iov_kunit_destroy_xarray(void *data)
544 struct xarray *xarray = data;
546 xa_destroy(xarray);
547 kfree(xarray);
550 static void __init iov_kunit_load_xarray(struct kunit *test,
551 struct iov_iter *iter, int dir,
552 struct xarray *xarray,
553 struct page **pages, size_t npages)
555 size_t size = 0;
556 int i;
558 for (i = 0; i < npages; i++) {
559 void *x = xa_store(xarray, i, pages[i], GFP_KERNEL);
561 KUNIT_ASSERT_FALSE(test, xa_is_err(x));
562 size += PAGE_SIZE;
564 iov_iter_xarray(iter, dir, xarray, 0, size);
567 static struct xarray *iov_kunit_create_xarray(struct kunit *test)
569 struct xarray *xarray;
571 xarray = kzalloc(sizeof(struct xarray), GFP_KERNEL);
572 xa_init(xarray);
573 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, xarray);
574 kunit_add_action_or_reset(test, iov_kunit_destroy_xarray, xarray);
575 return xarray;
579 * Test copying to a ITER_XARRAY-type iterator.
581 static void __init iov_kunit_copy_to_xarray(struct kunit *test)
583 const struct kvec_test_range *pr;
584 struct iov_iter iter;
585 struct xarray *xarray;
586 struct page **spages, **bpages;
587 u8 *scratch, *buffer;
588 size_t bufsize, npages, size, copied;
589 int i, patt;
591 bufsize = 0x100000;
592 npages = bufsize / PAGE_SIZE;
594 xarray = iov_kunit_create_xarray(test);
596 scratch = iov_kunit_create_buffer(test, &spages, npages);
597 for (i = 0; i < bufsize; i++)
598 scratch[i] = pattern(i);
600 buffer = iov_kunit_create_buffer(test, &bpages, npages);
601 memset(buffer, 0, bufsize);
603 iov_kunit_load_xarray(test, &iter, READ, xarray, bpages, npages);
605 i = 0;
606 for (pr = kvec_test_ranges; pr->from >= 0; pr++) {
607 size = pr->to - pr->from;
608 KUNIT_ASSERT_LE(test, pr->to, bufsize);
610 iov_iter_xarray(&iter, READ, xarray, pr->from, size);
611 copied = copy_to_iter(scratch + i, size, &iter);
613 KUNIT_EXPECT_EQ(test, copied, size);
614 KUNIT_EXPECT_EQ(test, iter.count, 0);
615 KUNIT_EXPECT_EQ(test, iter.iov_offset, size);
616 i += size;
619 /* Build the expected image in the scratch buffer. */
620 patt = 0;
621 memset(scratch, 0, bufsize);
622 for (pr = kvec_test_ranges; pr->from >= 0; pr++)
623 for (i = pr->from; i < pr->to; i++)
624 scratch[i] = pattern(patt++);
626 /* Compare the images */
627 for (i = 0; i < bufsize; i++) {
628 KUNIT_EXPECT_EQ_MSG(test, buffer[i], scratch[i], "at i=%x", i);
629 if (buffer[i] != scratch[i])
630 return;
633 KUNIT_SUCCEED(test);
637 * Test copying from a ITER_XARRAY-type iterator.
639 static void __init iov_kunit_copy_from_xarray(struct kunit *test)
641 const struct kvec_test_range *pr;
642 struct iov_iter iter;
643 struct xarray *xarray;
644 struct page **spages, **bpages;
645 u8 *scratch, *buffer;
646 size_t bufsize, npages, size, copied;
647 int i, j;
649 bufsize = 0x100000;
650 npages = bufsize / PAGE_SIZE;
652 xarray = iov_kunit_create_xarray(test);
654 buffer = iov_kunit_create_buffer(test, &bpages, npages);
655 for (i = 0; i < bufsize; i++)
656 buffer[i] = pattern(i);
658 scratch = iov_kunit_create_buffer(test, &spages, npages);
659 memset(scratch, 0, bufsize);
661 iov_kunit_load_xarray(test, &iter, READ, xarray, bpages, npages);
663 i = 0;
664 for (pr = kvec_test_ranges; pr->from >= 0; pr++) {
665 size = pr->to - pr->from;
666 KUNIT_ASSERT_LE(test, pr->to, bufsize);
668 iov_iter_xarray(&iter, WRITE, xarray, pr->from, size);
669 copied = copy_from_iter(scratch + i, size, &iter);
671 KUNIT_EXPECT_EQ(test, copied, size);
672 KUNIT_EXPECT_EQ(test, iter.count, 0);
673 KUNIT_EXPECT_EQ(test, iter.iov_offset, size);
674 i += size;
677 /* Build the expected image in the main buffer. */
678 i = 0;
679 memset(buffer, 0, bufsize);
680 for (pr = kvec_test_ranges; pr->from >= 0; pr++) {
681 for (j = pr->from; j < pr->to; j++) {
682 buffer[i++] = pattern(j);
683 if (i >= bufsize)
684 goto stop;
687 stop:
689 /* Compare the images */
690 for (i = 0; i < bufsize; i++) {
691 KUNIT_EXPECT_EQ_MSG(test, scratch[i], buffer[i], "at i=%x", i);
692 if (scratch[i] != buffer[i])
693 return;
696 KUNIT_SUCCEED(test);
700 * Test the extraction of ITER_KVEC-type iterators.
702 static void __init iov_kunit_extract_pages_kvec(struct kunit *test)
704 const struct kvec_test_range *pr;
705 struct iov_iter iter;
706 struct page **bpages, *pagelist[8], **pages = pagelist;
707 struct kvec kvec[8];
708 u8 *buffer;
709 ssize_t len;
710 size_t bufsize, size = 0, npages;
711 int i, from;
713 bufsize = 0x100000;
714 npages = bufsize / PAGE_SIZE;
716 buffer = iov_kunit_create_buffer(test, &bpages, npages);
718 iov_kunit_load_kvec(test, &iter, READ, kvec, ARRAY_SIZE(kvec),
719 buffer, bufsize, kvec_test_ranges);
720 size = iter.count;
722 pr = kvec_test_ranges;
723 from = pr->from;
724 do {
725 size_t offset0 = LONG_MAX;
727 for (i = 0; i < ARRAY_SIZE(pagelist); i++)
728 pagelist[i] = (void *)(unsigned long)0xaa55aa55aa55aa55ULL;
730 len = iov_iter_extract_pages(&iter, &pages, 100 * 1024,
731 ARRAY_SIZE(pagelist), 0, &offset0);
732 KUNIT_EXPECT_GE(test, len, 0);
733 if (len < 0)
734 break;
735 KUNIT_EXPECT_GE(test, (ssize_t)offset0, 0);
736 KUNIT_EXPECT_LT(test, offset0, PAGE_SIZE);
737 KUNIT_EXPECT_LE(test, len, size);
738 KUNIT_EXPECT_EQ(test, iter.count, size - len);
739 size -= len;
741 if (len == 0)
742 break;
744 for (i = 0; i < ARRAY_SIZE(pagelist); i++) {
745 struct page *p;
746 ssize_t part = min_t(ssize_t, len, PAGE_SIZE - offset0);
747 int ix;
749 KUNIT_ASSERT_GE(test, part, 0);
750 while (from == pr->to) {
751 pr++;
752 from = pr->from;
753 if (from < 0)
754 goto stop;
756 ix = from / PAGE_SIZE;
757 KUNIT_ASSERT_LT(test, ix, npages);
758 p = bpages[ix];
759 KUNIT_EXPECT_PTR_EQ(test, pagelist[i], p);
760 KUNIT_EXPECT_EQ(test, offset0, from % PAGE_SIZE);
761 from += part;
762 len -= part;
763 KUNIT_ASSERT_GE(test, len, 0);
764 if (len == 0)
765 break;
766 offset0 = 0;
769 if (test->status == KUNIT_FAILURE)
770 break;
771 } while (iov_iter_count(&iter) > 0);
773 stop:
774 KUNIT_EXPECT_EQ(test, size, 0);
775 KUNIT_EXPECT_EQ(test, iter.count, 0);
776 KUNIT_SUCCEED(test);
780 * Test the extraction of ITER_BVEC-type iterators.
782 static void __init iov_kunit_extract_pages_bvec(struct kunit *test)
784 const struct bvec_test_range *pr;
785 struct iov_iter iter;
786 struct page **bpages, *pagelist[8], **pages = pagelist;
787 struct bio_vec bvec[8];
788 ssize_t len;
789 size_t bufsize, size = 0, npages;
790 int i, from;
792 bufsize = 0x100000;
793 npages = bufsize / PAGE_SIZE;
795 iov_kunit_create_buffer(test, &bpages, npages);
796 iov_kunit_load_bvec(test, &iter, READ, bvec, ARRAY_SIZE(bvec),
797 bpages, npages, bufsize, bvec_test_ranges);
798 size = iter.count;
800 pr = bvec_test_ranges;
801 from = pr->from;
802 do {
803 size_t offset0 = LONG_MAX;
805 for (i = 0; i < ARRAY_SIZE(pagelist); i++)
806 pagelist[i] = (void *)(unsigned long)0xaa55aa55aa55aa55ULL;
808 len = iov_iter_extract_pages(&iter, &pages, 100 * 1024,
809 ARRAY_SIZE(pagelist), 0, &offset0);
810 KUNIT_EXPECT_GE(test, len, 0);
811 if (len < 0)
812 break;
813 KUNIT_EXPECT_GE(test, (ssize_t)offset0, 0);
814 KUNIT_EXPECT_LT(test, offset0, PAGE_SIZE);
815 KUNIT_EXPECT_LE(test, len, size);
816 KUNIT_EXPECT_EQ(test, iter.count, size - len);
817 size -= len;
819 if (len == 0)
820 break;
822 for (i = 0; i < ARRAY_SIZE(pagelist); i++) {
823 struct page *p;
824 ssize_t part = min_t(ssize_t, len, PAGE_SIZE - offset0);
825 int ix;
827 KUNIT_ASSERT_GE(test, part, 0);
828 while (from == pr->to) {
829 pr++;
830 from = pr->from;
831 if (from < 0)
832 goto stop;
834 ix = pr->page + from / PAGE_SIZE;
835 KUNIT_ASSERT_LT(test, ix, npages);
836 p = bpages[ix];
837 KUNIT_EXPECT_PTR_EQ(test, pagelist[i], p);
838 KUNIT_EXPECT_EQ(test, offset0, from % PAGE_SIZE);
839 from += part;
840 len -= part;
841 KUNIT_ASSERT_GE(test, len, 0);
842 if (len == 0)
843 break;
844 offset0 = 0;
847 if (test->status == KUNIT_FAILURE)
848 break;
849 } while (iov_iter_count(&iter) > 0);
851 stop:
852 KUNIT_EXPECT_EQ(test, size, 0);
853 KUNIT_EXPECT_EQ(test, iter.count, 0);
854 KUNIT_SUCCEED(test);
858 * Test the extraction of ITER_FOLIOQ-type iterators.
860 static void __init iov_kunit_extract_pages_folioq(struct kunit *test)
862 const struct kvec_test_range *pr;
863 struct folio_queue *folioq;
864 struct iov_iter iter;
865 struct page **bpages, *pagelist[8], **pages = pagelist;
866 ssize_t len;
867 size_t bufsize, size = 0, npages;
868 int i, from;
870 bufsize = 0x100000;
871 npages = bufsize / PAGE_SIZE;
873 folioq = iov_kunit_create_folioq(test);
875 iov_kunit_create_buffer(test, &bpages, npages);
876 iov_kunit_load_folioq(test, &iter, READ, folioq, bpages, npages);
878 for (pr = kvec_test_ranges; pr->from >= 0; pr++) {
879 from = pr->from;
880 size = pr->to - from;
881 KUNIT_ASSERT_LE(test, pr->to, bufsize);
883 iov_iter_folio_queue(&iter, WRITE, folioq, 0, 0, pr->to);
884 iov_iter_advance(&iter, from);
886 do {
887 size_t offset0 = LONG_MAX;
889 for (i = 0; i < ARRAY_SIZE(pagelist); i++)
890 pagelist[i] = (void *)(unsigned long)0xaa55aa55aa55aa55ULL;
892 len = iov_iter_extract_pages(&iter, &pages, 100 * 1024,
893 ARRAY_SIZE(pagelist), 0, &offset0);
894 KUNIT_EXPECT_GE(test, len, 0);
895 if (len < 0)
896 break;
897 KUNIT_EXPECT_LE(test, len, size);
898 KUNIT_EXPECT_EQ(test, iter.count, size - len);
899 if (len == 0)
900 break;
901 size -= len;
902 KUNIT_EXPECT_GE(test, (ssize_t)offset0, 0);
903 KUNIT_EXPECT_LT(test, offset0, PAGE_SIZE);
905 for (i = 0; i < ARRAY_SIZE(pagelist); i++) {
906 struct page *p;
907 ssize_t part = min_t(ssize_t, len, PAGE_SIZE - offset0);
908 int ix;
910 KUNIT_ASSERT_GE(test, part, 0);
911 ix = from / PAGE_SIZE;
912 KUNIT_ASSERT_LT(test, ix, npages);
913 p = bpages[ix];
914 KUNIT_EXPECT_PTR_EQ(test, pagelist[i], p);
915 KUNIT_EXPECT_EQ(test, offset0, from % PAGE_SIZE);
916 from += part;
917 len -= part;
918 KUNIT_ASSERT_GE(test, len, 0);
919 if (len == 0)
920 break;
921 offset0 = 0;
924 if (test->status == KUNIT_FAILURE)
925 goto stop;
926 } while (iov_iter_count(&iter) > 0);
928 KUNIT_EXPECT_EQ(test, size, 0);
929 KUNIT_EXPECT_EQ(test, iter.count, 0);
932 stop:
933 KUNIT_SUCCEED(test);
937 * Test the extraction of ITER_XARRAY-type iterators.
939 static void __init iov_kunit_extract_pages_xarray(struct kunit *test)
941 const struct kvec_test_range *pr;
942 struct iov_iter iter;
943 struct xarray *xarray;
944 struct page **bpages, *pagelist[8], **pages = pagelist;
945 ssize_t len;
946 size_t bufsize, size = 0, npages;
947 int i, from;
949 bufsize = 0x100000;
950 npages = bufsize / PAGE_SIZE;
952 xarray = iov_kunit_create_xarray(test);
954 iov_kunit_create_buffer(test, &bpages, npages);
955 iov_kunit_load_xarray(test, &iter, READ, xarray, bpages, npages);
957 for (pr = kvec_test_ranges; pr->from >= 0; pr++) {
958 from = pr->from;
959 size = pr->to - from;
960 KUNIT_ASSERT_LE(test, pr->to, bufsize);
962 iov_iter_xarray(&iter, WRITE, xarray, from, size);
964 do {
965 size_t offset0 = LONG_MAX;
967 for (i = 0; i < ARRAY_SIZE(pagelist); i++)
968 pagelist[i] = (void *)(unsigned long)0xaa55aa55aa55aa55ULL;
970 len = iov_iter_extract_pages(&iter, &pages, 100 * 1024,
971 ARRAY_SIZE(pagelist), 0, &offset0);
972 KUNIT_EXPECT_GE(test, len, 0);
973 if (len < 0)
974 break;
975 KUNIT_EXPECT_LE(test, len, size);
976 KUNIT_EXPECT_EQ(test, iter.count, size - len);
977 if (len == 0)
978 break;
979 size -= len;
980 KUNIT_EXPECT_GE(test, (ssize_t)offset0, 0);
981 KUNIT_EXPECT_LT(test, offset0, PAGE_SIZE);
983 for (i = 0; i < ARRAY_SIZE(pagelist); i++) {
984 struct page *p;
985 ssize_t part = min_t(ssize_t, len, PAGE_SIZE - offset0);
986 int ix;
988 KUNIT_ASSERT_GE(test, part, 0);
989 ix = from / PAGE_SIZE;
990 KUNIT_ASSERT_LT(test, ix, npages);
991 p = bpages[ix];
992 KUNIT_EXPECT_PTR_EQ(test, pagelist[i], p);
993 KUNIT_EXPECT_EQ(test, offset0, from % PAGE_SIZE);
994 from += part;
995 len -= part;
996 KUNIT_ASSERT_GE(test, len, 0);
997 if (len == 0)
998 break;
999 offset0 = 0;
1002 if (test->status == KUNIT_FAILURE)
1003 goto stop;
1004 } while (iov_iter_count(&iter) > 0);
1006 KUNIT_EXPECT_EQ(test, size, 0);
1007 KUNIT_EXPECT_EQ(test, iter.count, 0);
1008 KUNIT_EXPECT_EQ(test, iter.iov_offset, pr->to - pr->from);
1011 stop:
1012 KUNIT_SUCCEED(test);
1015 static struct kunit_case __refdata iov_kunit_cases[] = {
1016 KUNIT_CASE(iov_kunit_copy_to_kvec),
1017 KUNIT_CASE(iov_kunit_copy_from_kvec),
1018 KUNIT_CASE(iov_kunit_copy_to_bvec),
1019 KUNIT_CASE(iov_kunit_copy_from_bvec),
1020 KUNIT_CASE(iov_kunit_copy_to_folioq),
1021 KUNIT_CASE(iov_kunit_copy_from_folioq),
1022 KUNIT_CASE(iov_kunit_copy_to_xarray),
1023 KUNIT_CASE(iov_kunit_copy_from_xarray),
1024 KUNIT_CASE(iov_kunit_extract_pages_kvec),
1025 KUNIT_CASE(iov_kunit_extract_pages_bvec),
1026 KUNIT_CASE(iov_kunit_extract_pages_folioq),
1027 KUNIT_CASE(iov_kunit_extract_pages_xarray),
1031 static struct kunit_suite iov_kunit_suite = {
1032 .name = "iov_iter",
1033 .test_cases = iov_kunit_cases,
1036 kunit_test_suites(&iov_kunit_suite);