scatterlist.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright © 2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. */
  23. #include <linux/prime_numbers.h>
  24. #include <linux/random.h>
  25. #include "../i915_selftest.h"
  26. #define PFN_BIAS (1 << 10)
  27. struct pfn_table {
  28. struct sg_table st;
  29. unsigned long start, end;
  30. };
  31. typedef unsigned int (*npages_fn_t)(unsigned long n,
  32. unsigned long count,
  33. struct rnd_state *rnd);
  34. static noinline int expect_pfn_sg(struct pfn_table *pt,
  35. npages_fn_t npages_fn,
  36. struct rnd_state *rnd,
  37. const char *who,
  38. unsigned long timeout)
  39. {
  40. struct scatterlist *sg;
  41. unsigned long pfn, n;
  42. pfn = pt->start;
  43. for_each_sg(pt->st.sgl, sg, pt->st.nents, n) {
  44. struct page *page = sg_page(sg);
  45. unsigned int npages = npages_fn(n, pt->st.nents, rnd);
  46. if (page_to_pfn(page) != pfn) {
  47. pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sg)\n",
  48. __func__, who, pfn, page_to_pfn(page));
  49. return -EINVAL;
  50. }
  51. if (sg->length != npages * PAGE_SIZE) {
  52. pr_err("%s: %s copied wrong sg length, expected size %lu, found %u (using for_each_sg)\n",
  53. __func__, who, npages * PAGE_SIZE, sg->length);
  54. return -EINVAL;
  55. }
  56. if (igt_timeout(timeout, "%s timed out\n", who))
  57. return -EINTR;
  58. pfn += npages;
  59. }
  60. if (pfn != pt->end) {
  61. pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n",
  62. __func__, who, pt->end, pfn);
  63. return -EINVAL;
  64. }
  65. return 0;
  66. }
  67. static noinline int expect_pfn_sg_page_iter(struct pfn_table *pt,
  68. const char *who,
  69. unsigned long timeout)
  70. {
  71. struct sg_page_iter sgiter;
  72. unsigned long pfn;
  73. pfn = pt->start;
  74. for_each_sg_page(pt->st.sgl, &sgiter, pt->st.nents, 0) {
  75. struct page *page = sg_page_iter_page(&sgiter);
  76. if (page != pfn_to_page(pfn)) {
  77. pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sg_page)\n",
  78. __func__, who, pfn, page_to_pfn(page));
  79. return -EINVAL;
  80. }
  81. if (igt_timeout(timeout, "%s timed out\n", who))
  82. return -EINTR;
  83. pfn++;
  84. }
  85. if (pfn != pt->end) {
  86. pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n",
  87. __func__, who, pt->end, pfn);
  88. return -EINVAL;
  89. }
  90. return 0;
  91. }
  92. static noinline int expect_pfn_sgtiter(struct pfn_table *pt,
  93. const char *who,
  94. unsigned long timeout)
  95. {
  96. struct sgt_iter sgt;
  97. struct page *page;
  98. unsigned long pfn;
  99. pfn = pt->start;
  100. for_each_sgt_page(page, sgt, &pt->st) {
  101. if (page != pfn_to_page(pfn)) {
  102. pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sgt_page)\n",
  103. __func__, who, pfn, page_to_pfn(page));
  104. return -EINVAL;
  105. }
  106. if (igt_timeout(timeout, "%s timed out\n", who))
  107. return -EINTR;
  108. pfn++;
  109. }
  110. if (pfn != pt->end) {
  111. pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n",
  112. __func__, who, pt->end, pfn);
  113. return -EINVAL;
  114. }
  115. return 0;
  116. }
  117. static int expect_pfn_sgtable(struct pfn_table *pt,
  118. npages_fn_t npages_fn,
  119. struct rnd_state *rnd,
  120. const char *who,
  121. unsigned long timeout)
  122. {
  123. int err;
  124. err = expect_pfn_sg(pt, npages_fn, rnd, who, timeout);
  125. if (err)
  126. return err;
  127. err = expect_pfn_sg_page_iter(pt, who, timeout);
  128. if (err)
  129. return err;
  130. err = expect_pfn_sgtiter(pt, who, timeout);
  131. if (err)
  132. return err;
  133. return 0;
  134. }
  135. static unsigned int one(unsigned long n,
  136. unsigned long count,
  137. struct rnd_state *rnd)
  138. {
  139. return 1;
  140. }
  141. static unsigned int grow(unsigned long n,
  142. unsigned long count,
  143. struct rnd_state *rnd)
  144. {
  145. return n + 1;
  146. }
  147. static unsigned int shrink(unsigned long n,
  148. unsigned long count,
  149. struct rnd_state *rnd)
  150. {
  151. return count - n;
  152. }
  153. static unsigned int random(unsigned long n,
  154. unsigned long count,
  155. struct rnd_state *rnd)
  156. {
  157. return 1 + (prandom_u32_state(rnd) % 1024);
  158. }
  159. static inline bool page_contiguous(struct page *first,
  160. struct page *last,
  161. unsigned long npages)
  162. {
  163. return first + npages == last;
  164. }
  165. static int alloc_table(struct pfn_table *pt,
  166. unsigned long count, unsigned long max,
  167. npages_fn_t npages_fn,
  168. struct rnd_state *rnd,
  169. int alloc_error)
  170. {
  171. struct scatterlist *sg;
  172. unsigned long n, pfn;
  173. if (sg_alloc_table(&pt->st, max,
  174. GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN))
  175. return alloc_error;
  176. /* count should be less than 20 to prevent overflowing sg->length */
  177. GEM_BUG_ON(overflows_type(count * PAGE_SIZE, sg->length));
  178. /* Construct a table where each scatterlist contains different number
  179. * of entries. The idea is to check that we can iterate the individual
  180. * pages from inside the coalesced lists.
  181. */
  182. pt->start = PFN_BIAS;
  183. pfn = pt->start;
  184. sg = pt->st.sgl;
  185. for (n = 0; n < count; n++) {
  186. unsigned long npages = npages_fn(n, count, rnd);
  187. /* Nobody expects the Sparse Memmap! */
  188. if (!page_contiguous(pfn_to_page(pfn),
  189. pfn_to_page(pfn + npages),
  190. npages)) {
  191. sg_free_table(&pt->st);
  192. return -ENOSPC;
  193. }
  194. if (n)
  195. sg = sg_next(sg);
  196. sg_set_page(sg, pfn_to_page(pfn), npages * PAGE_SIZE, 0);
  197. GEM_BUG_ON(page_to_pfn(sg_page(sg)) != pfn);
  198. GEM_BUG_ON(sg->length != npages * PAGE_SIZE);
  199. GEM_BUG_ON(sg->offset != 0);
  200. pfn += npages;
  201. }
  202. sg_mark_end(sg);
  203. pt->st.nents = n;
  204. pt->end = pfn;
  205. return 0;
  206. }
  207. static const npages_fn_t npages_funcs[] = {
  208. one,
  209. grow,
  210. shrink,
  211. random,
  212. NULL,
  213. };
  214. static int igt_sg_alloc(void *ignored)
  215. {
  216. IGT_TIMEOUT(end_time);
  217. const unsigned long max_order = 20; /* approximating a 4GiB object */
  218. struct rnd_state prng;
  219. unsigned long prime;
  220. int alloc_error = -ENOMEM;
  221. for_each_prime_number(prime, max_order) {
  222. unsigned long size = BIT(prime);
  223. int offset;
  224. for (offset = -1; offset <= 1; offset++) {
  225. unsigned long sz = size + offset;
  226. const npages_fn_t *npages;
  227. struct pfn_table pt;
  228. int err;
  229. for (npages = npages_funcs; *npages; npages++) {
  230. prandom_seed_state(&prng,
  231. i915_selftest.random_seed);
  232. err = alloc_table(&pt, sz, sz, *npages, &prng,
  233. alloc_error);
  234. if (err == -ENOSPC)
  235. break;
  236. if (err)
  237. return err;
  238. prandom_seed_state(&prng,
  239. i915_selftest.random_seed);
  240. err = expect_pfn_sgtable(&pt, *npages, &prng,
  241. "sg_alloc_table",
  242. end_time);
  243. sg_free_table(&pt.st);
  244. if (err)
  245. return err;
  246. }
  247. }
  248. /* Test at least one continuation before accepting oom */
  249. if (size > SG_MAX_SINGLE_ALLOC)
  250. alloc_error = -ENOSPC;
  251. }
  252. return 0;
  253. }
  254. static int igt_sg_trim(void *ignored)
  255. {
  256. IGT_TIMEOUT(end_time);
  257. const unsigned long max = PAGE_SIZE; /* not prime! */
  258. struct pfn_table pt;
  259. unsigned long prime;
  260. int alloc_error = -ENOMEM;
  261. for_each_prime_number(prime, max) {
  262. const npages_fn_t *npages;
  263. int err;
  264. for (npages = npages_funcs; *npages; npages++) {
  265. struct rnd_state prng;
  266. prandom_seed_state(&prng, i915_selftest.random_seed);
  267. err = alloc_table(&pt, prime, max, *npages, &prng,
  268. alloc_error);
  269. if (err == -ENOSPC)
  270. break;
  271. if (err)
  272. return err;
  273. if (i915_sg_trim(&pt.st)) {
  274. if (pt.st.orig_nents != prime ||
  275. pt.st.nents != prime) {
  276. pr_err("i915_sg_trim failed (nents %u, orig_nents %u), expected %lu\n",
  277. pt.st.nents, pt.st.orig_nents, prime);
  278. err = -EINVAL;
  279. } else {
  280. prandom_seed_state(&prng,
  281. i915_selftest.random_seed);
  282. err = expect_pfn_sgtable(&pt,
  283. *npages, &prng,
  284. "i915_sg_trim",
  285. end_time);
  286. }
  287. }
  288. sg_free_table(&pt.st);
  289. if (err)
  290. return err;
  291. }
  292. /* Test at least one continuation before accepting oom */
  293. if (prime > SG_MAX_SINGLE_ALLOC)
  294. alloc_error = -ENOSPC;
  295. }
  296. return 0;
  297. }
  298. int scatterlist_mock_selftests(void)
  299. {
  300. static const struct i915_subtest tests[] = {
  301. SUBTEST(igt_sg_alloc),
  302. SUBTEST(igt_sg_trim),
  303. };
  304. return i915_subtests(tests, NULL);
  305. }