main.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <linux/scatterlist.h>
  4. #define MAX_PAGES (64)
  5. static void set_pages(struct page **pages, const unsigned *array, unsigned num)
  6. {
  7. unsigned int i;
  8. assert(num < MAX_PAGES);
  9. for (i = 0; i < num; i++)
  10. pages[i] = (struct page *)(unsigned long)
  11. ((1 + array[i]) * PAGE_SIZE);
  12. }
  13. #define pfn(...) (unsigned []){ __VA_ARGS__ }
  14. int main(void)
  15. {
  16. const unsigned int sgmax = SCATTERLIST_MAX_SEGMENT;
  17. struct test {
  18. int alloc_ret;
  19. unsigned num_pages;
  20. unsigned *pfn;
  21. unsigned size;
  22. unsigned int max_seg;
  23. unsigned int expected_segments;
  24. } *test, tests[] = {
  25. { -EINVAL, 1, pfn(0), PAGE_SIZE, PAGE_SIZE + 1, 1 },
  26. { -EINVAL, 1, pfn(0), PAGE_SIZE, 0, 1 },
  27. { -EINVAL, 1, pfn(0), PAGE_SIZE, sgmax + 1, 1 },
  28. { 0, 1, pfn(0), PAGE_SIZE, sgmax, 1 },
  29. { 0, 1, pfn(0), 1, sgmax, 1 },
  30. { 0, 2, pfn(0, 1), 2 * PAGE_SIZE, sgmax, 1 },
  31. { 0, 2, pfn(1, 0), 2 * PAGE_SIZE, sgmax, 2 },
  32. { 0, 3, pfn(0, 1, 2), 3 * PAGE_SIZE, sgmax, 1 },
  33. { 0, 3, pfn(0, 2, 1), 3 * PAGE_SIZE, sgmax, 3 },
  34. { 0, 3, pfn(0, 1, 3), 3 * PAGE_SIZE, sgmax, 2 },
  35. { 0, 3, pfn(1, 2, 4), 3 * PAGE_SIZE, sgmax, 2 },
  36. { 0, 3, pfn(1, 3, 4), 3 * PAGE_SIZE, sgmax, 2 },
  37. { 0, 4, pfn(0, 1, 3, 4), 4 * PAGE_SIZE, sgmax, 2 },
  38. { 0, 5, pfn(0, 1, 3, 4, 5), 5 * PAGE_SIZE, sgmax, 2 },
  39. { 0, 5, pfn(0, 1, 3, 4, 6), 5 * PAGE_SIZE, sgmax, 3 },
  40. { 0, 5, pfn(0, 1, 2, 3, 4), 5 * PAGE_SIZE, sgmax, 1 },
  41. { 0, 5, pfn(0, 1, 2, 3, 4), 5 * PAGE_SIZE, 2 * PAGE_SIZE, 3 },
  42. { 0, 6, pfn(0, 1, 2, 3, 4, 5), 6 * PAGE_SIZE, 2 * PAGE_SIZE, 3 },
  43. { 0, 6, pfn(0, 2, 3, 4, 5, 6), 6 * PAGE_SIZE, 2 * PAGE_SIZE, 4 },
  44. { 0, 6, pfn(0, 1, 3, 4, 5, 6), 6 * PAGE_SIZE, 2 * PAGE_SIZE, 3 },
  45. { 0, 0, NULL, 0, 0, 0 },
  46. };
  47. unsigned int i;
  48. for (i = 0, test = tests; test->expected_segments; test++, i++) {
  49. struct page *pages[MAX_PAGES];
  50. struct sg_table st;
  51. int ret;
  52. set_pages(pages, test->pfn, test->num_pages);
  53. ret = __sg_alloc_table_from_pages(&st, pages, test->num_pages,
  54. 0, test->size, test->max_seg,
  55. GFP_KERNEL);
  56. assert(ret == test->alloc_ret);
  57. if (test->alloc_ret)
  58. continue;
  59. assert(st.nents == test->expected_segments);
  60. assert(st.orig_nents == test->expected_segments);
  61. sg_free_table(&st);
  62. }
  63. assert(i == (sizeof(tests) / sizeof(tests[0])) - 1);
  64. return 0;
  65. }