.. | .. |
---|
5 | 5 | |
---|
6 | 6 | #define MAX_PAGES (64) |
---|
7 | 7 | |
---|
| 8 | +struct test { |
---|
| 9 | + int alloc_ret; |
---|
| 10 | + unsigned num_pages; |
---|
| 11 | + unsigned *pfn; |
---|
| 12 | + unsigned size; |
---|
| 13 | + unsigned int max_seg; |
---|
| 14 | + unsigned int expected_segments; |
---|
| 15 | +}; |
---|
| 16 | + |
---|
8 | 17 | static void set_pages(struct page **pages, const unsigned *array, unsigned num) |
---|
9 | 18 | { |
---|
10 | 19 | unsigned int i; |
---|
.. | .. |
---|
17 | 26 | |
---|
18 | 27 | #define pfn(...) (unsigned []){ __VA_ARGS__ } |
---|
19 | 28 | |
---|
| 29 | +static void fail(struct test *test, struct sg_table *st, const char *cond) |
---|
| 30 | +{ |
---|
| 31 | + unsigned int i; |
---|
| 32 | + |
---|
| 33 | + fprintf(stderr, "Failed on '%s'!\n\n", cond); |
---|
| 34 | + |
---|
| 35 | + printf("size = %u, max segment = %u, expected nents = %u\nst->nents = %u, st->orig_nents= %u\n", |
---|
| 36 | + test->size, test->max_seg, test->expected_segments, st->nents, |
---|
| 37 | + st->orig_nents); |
---|
| 38 | + |
---|
| 39 | + printf("%u input PFNs:", test->num_pages); |
---|
| 40 | + for (i = 0; i < test->num_pages; i++) |
---|
| 41 | + printf(" %x", test->pfn[i]); |
---|
| 42 | + printf("\n"); |
---|
| 43 | + |
---|
| 44 | + exit(1); |
---|
| 45 | +} |
---|
| 46 | + |
---|
| 47 | +#define VALIDATE(cond, st, test) \ |
---|
| 48 | + if (!(cond)) \ |
---|
| 49 | + fail((test), (st), #cond); |
---|
| 50 | + |
---|
20 | 51 | int main(void) |
---|
21 | 52 | { |
---|
22 | 53 | const unsigned int sgmax = SCATTERLIST_MAX_SEGMENT; |
---|
23 | | - struct test { |
---|
24 | | - int alloc_ret; |
---|
25 | | - unsigned num_pages; |
---|
26 | | - unsigned *pfn; |
---|
27 | | - unsigned size; |
---|
28 | | - unsigned int max_seg; |
---|
29 | | - unsigned int expected_segments; |
---|
30 | | - } *test, tests[] = { |
---|
31 | | - { -EINVAL, 1, pfn(0), PAGE_SIZE, PAGE_SIZE + 1, 1 }, |
---|
| 54 | + struct test *test, tests[] = { |
---|
32 | 55 | { -EINVAL, 1, pfn(0), PAGE_SIZE, 0, 1 }, |
---|
33 | | - { -EINVAL, 1, pfn(0), PAGE_SIZE, sgmax + 1, 1 }, |
---|
| 56 | + { 0, 1, pfn(0), PAGE_SIZE, PAGE_SIZE + 1, 1 }, |
---|
| 57 | + { 0, 1, pfn(0), PAGE_SIZE, sgmax + 1, 1 }, |
---|
34 | 58 | { 0, 1, pfn(0), PAGE_SIZE, sgmax, 1 }, |
---|
35 | 59 | { 0, 1, pfn(0), 1, sgmax, 1 }, |
---|
36 | 60 | { 0, 2, pfn(0, 1), 2 * PAGE_SIZE, sgmax, 1 }, |
---|
.. | .. |
---|
55 | 79 | for (i = 0, test = tests; test->expected_segments; test++, i++) { |
---|
56 | 80 | struct page *pages[MAX_PAGES]; |
---|
57 | 81 | struct sg_table st; |
---|
58 | | - int ret; |
---|
| 82 | + struct scatterlist *sg; |
---|
59 | 83 | |
---|
60 | 84 | set_pages(pages, test->pfn, test->num_pages); |
---|
61 | 85 | |
---|
62 | | - ret = __sg_alloc_table_from_pages(&st, pages, test->num_pages, |
---|
63 | | - 0, test->size, test->max_seg, |
---|
64 | | - GFP_KERNEL); |
---|
65 | | - assert(ret == test->alloc_ret); |
---|
| 86 | + sg = __sg_alloc_table_from_pages(&st, pages, test->num_pages, 0, |
---|
| 87 | + test->size, test->max_seg, NULL, 0, GFP_KERNEL); |
---|
| 88 | + assert(PTR_ERR_OR_ZERO(sg) == test->alloc_ret); |
---|
66 | 89 | |
---|
67 | 90 | if (test->alloc_ret) |
---|
68 | 91 | continue; |
---|
69 | 92 | |
---|
70 | | - assert(st.nents == test->expected_segments); |
---|
71 | | - assert(st.orig_nents == test->expected_segments); |
---|
| 93 | + VALIDATE(st.nents == test->expected_segments, &st, test); |
---|
| 94 | + VALIDATE(st.orig_nents == test->expected_segments, &st, test); |
---|
72 | 95 | |
---|
73 | 96 | sg_free_table(&st); |
---|
74 | 97 | } |
---|