Back to home page

OSCL-LXR

 
 

    


0001 #include <stdio.h>
0002 #include <assert.h>
0003 
0004 #include <linux/scatterlist.h>
0005 
0006 #define MAX_PAGES (64)
0007 
0008 struct test {
0009     int alloc_ret;
0010     unsigned num_pages;
0011     unsigned *pfn;
0012     unsigned *pfn_app;
0013     unsigned size;
0014     unsigned int max_seg;
0015     unsigned int expected_segments;
0016 };
0017 
0018 static void set_pages(struct page **pages, const unsigned *array, unsigned num)
0019 {
0020     unsigned int i;
0021 
0022     assert(num < MAX_PAGES);
0023     for (i = 0; i < num; i++)
0024         pages[i] = (struct page *)(unsigned long)
0025                ((1 + array[i]) * PAGE_SIZE);
0026 }
0027 
0028 #define pfn(...) (unsigned []){ __VA_ARGS__ }
0029 
0030 static void fail(struct test *test, struct sg_table *st, const char *cond)
0031 {
0032     unsigned int i;
0033 
0034     fprintf(stderr, "Failed on '%s'!\n\n", cond);
0035 
0036     printf("size = %u, max segment = %u, expected nents = %u\nst->nents = %u, st->orig_nents= %u\n",
0037            test->size, test->max_seg, test->expected_segments, st->nents,
0038            st->orig_nents);
0039 
0040     printf("%u input PFNs:", test->num_pages);
0041     for (i = 0; i < test->num_pages; i++)
0042         printf(" %x", test->pfn[i]);
0043     printf("\n");
0044 
0045     exit(1);
0046 }
0047 
0048 #define VALIDATE(cond, st, test) \
0049     if (!(cond)) \
0050         fail((test), (st), #cond);
0051 
0052 int main(void)
0053 {
0054     const unsigned int sgmax = UINT_MAX;
0055     struct test *test, tests[] = {
0056         { -EINVAL, 1, pfn(0), NULL, PAGE_SIZE, 0, 1 },
0057         { 0, 1, pfn(0), NULL, PAGE_SIZE, PAGE_SIZE + 1, 1 },
0058         { 0, 1, pfn(0), NULL, PAGE_SIZE, sgmax, 1 },
0059         { 0, 1, pfn(0), NULL, 1, sgmax, 1 },
0060         { 0, 2, pfn(0, 1), NULL, 2 * PAGE_SIZE, sgmax, 1 },
0061         { 0, 2, pfn(1, 0), NULL, 2 * PAGE_SIZE, sgmax, 2 },
0062         { 0, 3, pfn(0, 1, 2), NULL, 3 * PAGE_SIZE, sgmax, 1 },
0063         { 0, 3, pfn(0, 1, 2), NULL, 3 * PAGE_SIZE, sgmax, 1 },
0064         { 0, 3, pfn(0, 1, 2), pfn(3, 4, 5), 3 * PAGE_SIZE, sgmax, 1 },
0065         { 0, 3, pfn(0, 1, 2), pfn(4, 5, 6), 3 * PAGE_SIZE, sgmax, 2 },
0066         { 0, 3, pfn(0, 2, 1), NULL, 3 * PAGE_SIZE, sgmax, 3 },
0067         { 0, 3, pfn(0, 1, 3), NULL, 3 * PAGE_SIZE, sgmax, 2 },
0068         { 0, 3, pfn(1, 2, 4), NULL, 3 * PAGE_SIZE, sgmax, 2 },
0069         { 0, 3, pfn(1, 3, 4), NULL, 3 * PAGE_SIZE, sgmax, 2 },
0070         { 0, 4, pfn(0, 1, 3, 4), NULL, 4 * PAGE_SIZE, sgmax, 2 },
0071         { 0, 5, pfn(0, 1, 3, 4, 5), NULL, 5 * PAGE_SIZE, sgmax, 2 },
0072         { 0, 5, pfn(0, 1, 3, 4, 6), NULL, 5 * PAGE_SIZE, sgmax, 3 },
0073         { 0, 5, pfn(0, 1, 2, 3, 4), NULL, 5 * PAGE_SIZE, sgmax, 1 },
0074         { 0, 5, pfn(0, 1, 2, 3, 4), NULL, 5 * PAGE_SIZE, 2 * PAGE_SIZE,
0075           3 },
0076         { 0, 6, pfn(0, 1, 2, 3, 4, 5), NULL, 6 * PAGE_SIZE,
0077           2 * PAGE_SIZE, 3 },
0078         { 0, 6, pfn(0, 2, 3, 4, 5, 6), NULL, 6 * PAGE_SIZE,
0079           2 * PAGE_SIZE, 4 },
0080         { 0, 6, pfn(0, 1, 3, 4, 5, 6), pfn(7, 8, 9, 10, 11, 12),
0081           6 * PAGE_SIZE, 12 * PAGE_SIZE, 2 },
0082         { 0, 0, NULL, NULL, 0, 0, 0 },
0083     };
0084     unsigned int i;
0085 
0086     for (i = 0, test = tests; test->expected_segments; test++, i++) {
0087         int left_pages = test->pfn_app ? test->num_pages : 0;
0088         struct sg_append_table append = {};
0089         struct page *pages[MAX_PAGES];
0090         int ret;
0091 
0092         set_pages(pages, test->pfn, test->num_pages);
0093 
0094         if (test->pfn_app)
0095             ret = sg_alloc_append_table_from_pages(
0096                 &append, pages, test->num_pages, 0, test->size,
0097                 test->max_seg, left_pages, GFP_KERNEL);
0098         else
0099             ret = sg_alloc_table_from_pages_segment(
0100                 &append.sgt, pages, test->num_pages, 0,
0101                 test->size, test->max_seg, GFP_KERNEL);
0102 
0103         assert(ret == test->alloc_ret);
0104 
0105         if (test->alloc_ret)
0106             continue;
0107 
0108         if (test->pfn_app) {
0109             set_pages(pages, test->pfn_app, test->num_pages);
0110             ret = sg_alloc_append_table_from_pages(
0111                 &append, pages, test->num_pages, 0, test->size,
0112                 test->max_seg, 0, GFP_KERNEL);
0113 
0114             assert(ret == test->alloc_ret);
0115         }
0116 
0117         VALIDATE(append.sgt.nents == test->expected_segments,
0118              &append.sgt, test);
0119         if (!test->pfn_app)
0120             VALIDATE(append.sgt.orig_nents ==
0121                      test->expected_segments,
0122                  &append.sgt, test);
0123 
0124         if (test->pfn_app)
0125             sg_free_append_table(&append);
0126         else
0127             sg_free_table(&append.sgt);
0128     }
0129 
0130     assert(i == (sizeof(tests) / sizeof(tests[0])) - 1);
0131 
0132     return 0;
0133 }