scatterlist.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 unsigned int random_page_size_pages(unsigned long n,
  160. unsigned long count,
  161. struct rnd_state *rnd)
  162. {
  163. /* 4K, 64K, 2M */
  164. static unsigned int page_count[] = {
  165. BIT(12) >> PAGE_SHIFT,
  166. BIT(16) >> PAGE_SHIFT,
  167. BIT(21) >> PAGE_SHIFT,
  168. };
  169. return page_count[(prandom_u32_state(rnd) % 3)];
  170. }
  171. static inline bool page_contiguous(struct page *first,
  172. struct page *last,
  173. unsigned long npages)
  174. {
  175. return first + npages == last;
  176. }
  177. static int alloc_table(struct pfn_table *pt,
  178. unsigned long count, unsigned long max,
  179. npages_fn_t npages_fn,
  180. struct rnd_state *rnd,
  181. int alloc_error)
  182. {
  183. struct scatterlist *sg;
  184. unsigned long n, pfn;
  185. if (sg_alloc_table(&pt->st, max,
  186. GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN))
  187. return alloc_error;
  188. /* count should be less than 20 to prevent overflowing sg->length */
  189. GEM_BUG_ON(overflows_type(count * PAGE_SIZE, sg->length));
  190. /* Construct a table where each scatterlist contains different number
  191. * of entries. The idea is to check that we can iterate the individual
  192. * pages from inside the coalesced lists.
  193. */
  194. pt->start = PFN_BIAS;
  195. pfn = pt->start;
  196. sg = pt->st.sgl;
  197. for (n = 0; n < count; n++) {
  198. unsigned long npages = npages_fn(n, count, rnd);
  199. /* Nobody expects the Sparse Memmap! */
  200. if (!page_contiguous(pfn_to_page(pfn),
  201. pfn_to_page(pfn + npages),
  202. npages)) {
  203. sg_free_table(&pt->st);
  204. return -ENOSPC;
  205. }
  206. if (n)
  207. sg = sg_next(sg);
  208. sg_set_page(sg, pfn_to_page(pfn), npages * PAGE_SIZE, 0);
  209. GEM_BUG_ON(page_to_pfn(sg_page(sg)) != pfn);
  210. GEM_BUG_ON(sg->length != npages * PAGE_SIZE);
  211. GEM_BUG_ON(sg->offset != 0);
  212. pfn += npages;
  213. }
  214. sg_mark_end(sg);
  215. pt->st.nents = n;
  216. pt->end = pfn;
  217. return 0;
  218. }
  219. static const npages_fn_t npages_funcs[] = {
  220. one,
  221. grow,
  222. shrink,
  223. random,
  224. random_page_size_pages,
  225. NULL,
  226. };
  227. static int igt_sg_alloc(void *ignored)
  228. {
  229. IGT_TIMEOUT(end_time);
  230. const unsigned long max_order = 20; /* approximating a 4GiB object */
  231. struct rnd_state prng;
  232. unsigned long prime;
  233. int alloc_error = -ENOMEM;
  234. for_each_prime_number(prime, max_order) {
  235. unsigned long size = BIT(prime);
  236. int offset;
  237. for (offset = -1; offset <= 1; offset++) {
  238. unsigned long sz = size + offset;
  239. const npages_fn_t *npages;
  240. struct pfn_table pt;
  241. int err;
  242. for (npages = npages_funcs; *npages; npages++) {
  243. prandom_seed_state(&prng,
  244. i915_selftest.random_seed);
  245. err = alloc_table(&pt, sz, sz, *npages, &prng,
  246. alloc_error);
  247. if (err == -ENOSPC)
  248. break;
  249. if (err)
  250. return err;
  251. prandom_seed_state(&prng,
  252. i915_selftest.random_seed);
  253. err = expect_pfn_sgtable(&pt, *npages, &prng,
  254. "sg_alloc_table",
  255. end_time);
  256. sg_free_table(&pt.st);
  257. if (err)
  258. return err;
  259. }
  260. }
  261. /* Test at least one continuation before accepting oom */
  262. if (size > SG_MAX_SINGLE_ALLOC)
  263. alloc_error = -ENOSPC;
  264. }
  265. return 0;
  266. }
  267. static int igt_sg_trim(void *ignored)
  268. {
  269. IGT_TIMEOUT(end_time);
  270. const unsigned long max = PAGE_SIZE; /* not prime! */
  271. struct pfn_table pt;
  272. unsigned long prime;
  273. int alloc_error = -ENOMEM;
  274. for_each_prime_number(prime, max) {
  275. const npages_fn_t *npages;
  276. int err;
  277. for (npages = npages_funcs; *npages; npages++) {
  278. struct rnd_state prng;
  279. prandom_seed_state(&prng, i915_selftest.random_seed);
  280. err = alloc_table(&pt, prime, max, *npages, &prng,
  281. alloc_error);
  282. if (err == -ENOSPC)
  283. break;
  284. if (err)
  285. return err;
  286. if (i915_sg_trim(&pt.st)) {
  287. if (pt.st.orig_nents != prime ||
  288. pt.st.nents != prime) {
  289. pr_err("i915_sg_trim failed (nents %u, orig_nents %u), expected %lu\n",
  290. pt.st.nents, pt.st.orig_nents, prime);
  291. err = -EINVAL;
  292. } else {
  293. prandom_seed_state(&prng,
  294. i915_selftest.random_seed);
  295. err = expect_pfn_sgtable(&pt,
  296. *npages, &prng,
  297. "i915_sg_trim",
  298. end_time);
  299. }
  300. }
  301. sg_free_table(&pt.st);
  302. if (err)
  303. return err;
  304. }
  305. /* Test at least one continuation before accepting oom */
  306. if (prime > SG_MAX_SINGLE_ALLOC)
  307. alloc_error = -ENOSPC;
  308. }
  309. return 0;
  310. }
  311. int scatterlist_mock_selftests(void)
  312. {
  313. static const struct i915_subtest tests[] = {
  314. SUBTEST(igt_sg_alloc),
  315. SUBTEST(igt_sg_trim),
  316. };
  317. return i915_subtests(tests, NULL);
  318. }