4 #include <linux/scatterlist.h>
15 unsigned int expected_segments
;
18 static void set_pages(struct page
**pages
, const unsigned *array
, unsigned num
)
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
);
28 #define pfn(...) (unsigned []){ __VA_ARGS__ }
30 static void fail(struct test
*test
, struct sg_table
*st
, const char *cond
)
34 fprintf(stderr
, "Failed on '%s'!\n\n", cond
);
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
,
40 printf("%u input PFNs:", test
->num_pages
);
41 for (i
= 0; i
< test
->num_pages
; i
++)
42 printf(" %x", test
->pfn
[i
]);
48 #define VALIDATE(cond, st, test) \
50 fail((test), (st), #cond);
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
,
77 { 0, 6, pfn(0, 1, 2, 3, 4, 5), NULL
, 6 * PAGE_SIZE
,
79 { 0, 6, pfn(0, 2, 3, 4, 5, 6), NULL
, 6 * PAGE_SIZE
,
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 },
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
];
91 struct scatterlist
*sg
;
93 set_pages(pages
, test
->pfn
, test
->num_pages
);
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
);
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
);
107 assert(PTR_ERR_OR_ZERO(sg
) == test
->alloc_ret
);
110 VALIDATE(st
.nents
== test
->expected_segments
, &st
, test
);
112 VALIDATE(st
.orig_nents
== test
->expected_segments
, &st
, test
);
117 assert(i
== (sizeof(tests
) / sizeof(tests
[0])) - 1);