1 #include <stdio.h> 2 #include <assert.h> 3 4 #include <linux/scatterlist.h> 5 6 #define MAX_PAGES (64) 7 8 struct test { 9 int alloc_ret; 10 unsigned num_pages; 11 unsigned *pfn; 12 unsigned *pfn_app; 13 unsigned size; 14 unsigned int max_seg; 15 unsigned int expected_segments; 16 }; 17 18 static void set_pages(struct page **pages, const unsigned *array, unsigned num) 19 { 20 unsigned int i; 21 22 assert(num < MAX_PAGES); 23 for (i = 0; i < num; i++) 24 pages[i] = (struct page *)(unsigned long) 25 ((1 + array[i]) * PAGE_SIZE); 26 } 27 28 #define pfn(...) (unsigned []){ __VA_ARGS__ } 29 30 static void fail(struct test *test, struct sg_table *st, const char *cond) 31 { 32 unsigned int i; 33 34 fprintf(stderr, "Failed on '%s'!\n\n", cond); 35 36 printf("size = %u, max segment = %u, expected nents = %u\nst->nents = %u, st->orig_nents= %u\n", 37 test->size, test->max_seg, test->expected_segments, st->nents, 38 st->orig_nents); 39 40 printf("%u input PFNs:", test->num_pages); 41 for (i = 0; i < test->num_pages; i++) 42 printf(" %x", test->pfn[i]); 43 printf("\n"); 44 45 exit(1); 46 } 47 48 #define VALIDATE(cond, st, test) \ 49 if (!(cond)) \ 50 fail((test), (st), #cond); 51 52 int main(void) 53 { 54 const unsigned int sgmax = UINT_MAX; 55 struct test *test, tests[] = { 56 { -EINVAL, 1, pfn(0), NULL, PAGE_SIZE, 0, 1 }, 57 { 0, 1, pfn(0), NULL, PAGE_SIZE, PAGE_SIZE + 1, 1 }, 58 { 0, 1, pfn(0), NULL, PAGE_SIZE, sgmax + 1, 1 }, 59 { 0, 1, pfn(0), NULL, PAGE_SIZE, sgmax, 1 }, 60 { 0, 1, pfn(0), NULL, 1, sgmax, 1 }, 61 { 0, 2, pfn(0, 1), NULL, 2 * PAGE_SIZE, sgmax, 1 }, 62 { 0, 2, pfn(1, 0), NULL, 2 * PAGE_SIZE, sgmax, 2 }, 63 { 0, 3, pfn(0, 1, 2), NULL, 3 * PAGE_SIZE, sgmax, 1 }, 64 { 0, 3, pfn(0, 1, 2), NULL, 3 * PAGE_SIZE, sgmax, 1 }, 65 { 0, 3, pfn(0, 1, 2), pfn(3, 4, 5), 3 * PAGE_SIZE, sgmax, 1 }, 66 { 0, 3, pfn(0, 1, 2), pfn(4, 5, 6), 3 * PAGE_SIZE, sgmax, 2 }, 67 { 0, 3, pfn(0, 2, 1), NULL, 3 * PAGE_SIZE, sgmax, 3 }, 68 { 0, 3, pfn(0, 1, 3), NULL, 3 * PAGE_SIZE, sgmax, 2 }, 69 { 0, 3, pfn(1, 2, 4), NULL, 3 * PAGE_SIZE, sgmax, 2 }, 70 { 0, 3, pfn(1, 3, 4), NULL, 3 * PAGE_SIZE, sgmax, 2 }, 71 { 0, 4, pfn(0, 1, 3, 4), NULL, 4 * PAGE_SIZE, sgmax, 2 }, 72 { 0, 5, pfn(0, 1, 3, 4, 5), NULL, 5 * PAGE_SIZE, sgmax, 2 }, 73 { 0, 5, pfn(0, 1, 3, 4, 6), NULL, 5 * PAGE_SIZE, sgmax, 3 }, 74 { 0, 5, pfn(0, 1, 2, 3, 4), NULL, 5 * PAGE_SIZE, sgmax, 1 }, 75 { 0, 5, pfn(0, 1, 2, 3, 4), NULL, 5 * PAGE_SIZE, 2 * PAGE_SIZE, 76 3 }, 77 { 0, 6, pfn(0, 1, 2, 3, 4, 5), NULL, 6 * PAGE_SIZE, 78 2 * PAGE_SIZE, 3 }, 79 { 0, 6, pfn(0, 2, 3, 4, 5, 6), NULL, 6 * PAGE_SIZE, 80 2 * PAGE_SIZE, 4 }, 81 { 0, 6, pfn(0, 1, 3, 4, 5, 6), pfn(7, 8, 9, 10, 11, 12), 82 6 * PAGE_SIZE, 12 * PAGE_SIZE, 2 }, 83 { 0, 0, NULL, NULL, 0, 0, 0 }, 84 }; 85 unsigned int i; 86 87 for (i = 0, test = tests; test->expected_segments; test++, i++) { 88 int left_pages = test->pfn_app ? test->num_pages : 0; 89 struct page *pages[MAX_PAGES]; 90 struct sg_table st; 91 struct scatterlist *sg; 92 93 set_pages(pages, test->pfn, test->num_pages); 94 95 sg = __sg_alloc_table_from_pages(&st, pages, test->num_pages, 0, 96 test->size, test->max_seg, NULL, left_pages, GFP_KERNEL); 97 assert(PTR_ERR_OR_ZERO(sg) == test->alloc_ret); 98 99 if (test->alloc_ret) 100 continue; 101 102 if (test->pfn_app) { 103 set_pages(pages, test->pfn_app, test->num_pages); 104 sg = __sg_alloc_table_from_pages(&st, pages, test->num_pages, 0, 105 test->size, test->max_seg, sg, 0, GFP_KERNEL); 106 107 assert(PTR_ERR_OR_ZERO(sg) == test->alloc_ret); 108 } 109 110 VALIDATE(st.nents == test->expected_segments, &st, test); 111 if (!test->pfn_app) 112 VALIDATE(st.orig_nents == test->expected_segments, &st, test); 113 114 sg_free_table(&st); 115 } 116 117 assert(i == (sizeof(tests) / sizeof(tests[0])) - 1); 118 119 return 0; 120 } 121