scatterlist.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. #include "i915_utils.h"
  27. #define PFN_BIAS (1 << 10)
  28. struct pfn_table {
  29. struct sg_table st;
  30. unsigned long start, end;
  31. };
  32. typedef unsigned int (*npages_fn_t)(unsigned long n,
  33. unsigned long count,
  34. struct rnd_state *rnd);
  35. static noinline int expect_pfn_sg(struct pfn_table *pt,
  36. npages_fn_t npages_fn,
  37. struct rnd_state *rnd,
  38. const char *who,
  39. unsigned long timeout)
  40. {
  41. struct scatterlist *sg;
  42. unsigned long pfn, n;
  43. pfn = pt->start;
  44. for_each_sg(pt->st.sgl, sg, pt->st.nents, n) {
  45. struct page *page = sg_page(sg);
  46. unsigned int npages = npages_fn(n, pt->st.nents, rnd);
  47. if (page_to_pfn(page) != pfn) {
  48. pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sg)\n",
  49. __func__, who, pfn, page_to_pfn(page));
  50. return -EINVAL;
  51. }
  52. if (sg->length != npages * PAGE_SIZE) {
  53. pr_err("%s: %s copied wrong sg length, expected size %lu, found %u (using for_each_sg)\n",
  54. __func__, who, npages * PAGE_SIZE, sg->length);
  55. return -EINVAL;
  56. }
  57. if (igt_timeout(timeout, "%s timed out\n", who))
  58. return -EINTR;
  59. pfn += npages;
  60. }
  61. if (pfn != pt->end) {
  62. pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n",
  63. __func__, who, pt->end, pfn);
  64. return -EINVAL;
  65. }
  66. return 0;
  67. }
  68. static noinline int expect_pfn_sg_page_iter(struct pfn_table *pt,
  69. const char *who,
  70. unsigned long timeout)
  71. {
  72. struct sg_page_iter sgiter;
  73. unsigned long pfn;
  74. pfn = pt->start;
  75. for_each_sg_page(pt->st.sgl, &sgiter, pt->st.nents, 0) {
  76. struct page *page = sg_page_iter_page(&sgiter);
  77. if (page != pfn_to_page(pfn)) {
  78. pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sg_page)\n",
  79. __func__, who, pfn, page_to_pfn(page));
  80. return -EINVAL;
  81. }
  82. if (igt_timeout(timeout, "%s timed out\n", who))
  83. return -EINTR;
  84. pfn++;
  85. }
  86. if (pfn != pt->end) {
  87. pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n",
  88. __func__, who, pt->end, pfn);
  89. return -EINVAL;
  90. }
  91. return 0;
  92. }
  93. static noinline int expect_pfn_sgtiter(struct pfn_table *pt,
  94. const char *who,
  95. unsigned long timeout)
  96. {
  97. struct sgt_iter sgt;
  98. struct page *page;
  99. unsigned long pfn;
  100. pfn = pt->start;
  101. for_each_sgt_page(page, sgt, &pt->st) {
  102. if (page != pfn_to_page(pfn)) {
  103. pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sgt_page)\n",
  104. __func__, who, pfn, page_to_pfn(page));
  105. return -EINVAL;
  106. }
  107. if (igt_timeout(timeout, "%s timed out\n", who))
  108. return -EINTR;
  109. pfn++;
  110. }
  111. if (pfn != pt->end) {
  112. pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n",
  113. __func__, who, pt->end, pfn);
  114. return -EINVAL;
  115. }
  116. return 0;
  117. }
  118. static int expect_pfn_sgtable(struct pfn_table *pt,
  119. npages_fn_t npages_fn,
  120. struct rnd_state *rnd,
  121. const char *who,
  122. unsigned long timeout)
  123. {
  124. int err;
  125. err = expect_pfn_sg(pt, npages_fn, rnd, who, timeout);
  126. if (err)
  127. return err;
  128. err = expect_pfn_sg_page_iter(pt, who, timeout);
  129. if (err)
  130. return err;
  131. err = expect_pfn_sgtiter(pt, who, timeout);
  132. if (err)
  133. return err;
  134. return 0;
  135. }
  136. static unsigned int one(unsigned long n,
  137. unsigned long count,
  138. struct rnd_state *rnd)
  139. {
  140. return 1;
  141. }
  142. static unsigned int grow(unsigned long n,
  143. unsigned long count,
  144. struct rnd_state *rnd)
  145. {
  146. return n + 1;
  147. }
  148. static unsigned int shrink(unsigned long n,
  149. unsigned long count,
  150. struct rnd_state *rnd)
  151. {
  152. return count - n;
  153. }
  154. static unsigned int random(unsigned long n,
  155. unsigned long count,
  156. struct rnd_state *rnd)
  157. {
  158. return 1 + (prandom_u32_state(rnd) % 1024);
  159. }
  160. static unsigned int random_page_size_pages(unsigned long n,
  161. unsigned long count,
  162. struct rnd_state *rnd)
  163. {
  164. /* 4K, 64K, 2M */
  165. static unsigned int page_count[] = {
  166. BIT(12) >> PAGE_SHIFT,
  167. BIT(16) >> PAGE_SHIFT,
  168. BIT(21) >> PAGE_SHIFT,
  169. };
  170. return page_count[(prandom_u32_state(rnd) % 3)];
  171. }
  172. static inline bool page_contiguous(struct page *first,
  173. struct page *last,
  174. unsigned long npages)
  175. {
  176. return first + npages == last;
  177. }
  178. static int alloc_table(struct pfn_table *pt,
  179. unsigned long count, unsigned long max,
  180. npages_fn_t npages_fn,
  181. struct rnd_state *rnd,
  182. int alloc_error)
  183. {
  184. struct scatterlist *sg;
  185. unsigned long n, pfn;
  186. /* restricted by sg_alloc_table */
  187. if (overflows_type(max, unsigned int))
  188. return -E2BIG;
  189. if (sg_alloc_table(&pt->st, max,
  190. GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN))
  191. return alloc_error;
  192. /* count should be less than 20 to prevent overflowing sg->length */
  193. GEM_BUG_ON(overflows_type(count * PAGE_SIZE, sg->length));
  194. /* Construct a table where each scatterlist contains different number
  195. * of entries. The idea is to check that we can iterate the individual
  196. * pages from inside the coalesced lists.
  197. */
  198. pt->start = PFN_BIAS;
  199. pfn = pt->start;
  200. sg = pt->st.sgl;
  201. for (n = 0; n < count; n++) {
  202. unsigned long npages = npages_fn(n, count, rnd);
  203. /* Nobody expects the Sparse Memmap! */
  204. if (!page_contiguous(pfn_to_page(pfn),
  205. pfn_to_page(pfn + npages),
  206. npages)) {
  207. sg_free_table(&pt->st);
  208. return -ENOSPC;
  209. }
  210. if (n)
  211. sg = sg_next(sg);
  212. sg_set_page(sg, pfn_to_page(pfn), npages * PAGE_SIZE, 0);
  213. GEM_BUG_ON(page_to_pfn(sg_page(sg)) != pfn);
  214. GEM_BUG_ON(sg->length != npages * PAGE_SIZE);
  215. GEM_BUG_ON(sg->offset != 0);
  216. pfn += npages;
  217. }
  218. sg_mark_end(sg);
  219. pt->st.nents = n;
  220. pt->end = pfn;
  221. return 0;
  222. }
  223. static const npages_fn_t npages_funcs[] = {
  224. one,
  225. grow,
  226. shrink,
  227. random,
  228. random_page_size_pages,
  229. NULL,
  230. };
  231. static int igt_sg_alloc(void *ignored)
  232. {
  233. IGT_TIMEOUT(end_time);
  234. const unsigned long max_order = 20; /* approximating a 4GiB object */
  235. struct rnd_state prng;
  236. unsigned long prime;
  237. int alloc_error = -ENOMEM;
  238. for_each_prime_number(prime, max_order) {
  239. unsigned long size = BIT(prime);
  240. int offset;
  241. for (offset = -1; offset <= 1; offset++) {
  242. unsigned long sz = size + offset;
  243. const npages_fn_t *npages;
  244. struct pfn_table pt;
  245. int err;
  246. for (npages = npages_funcs; *npages; npages++) {
  247. prandom_seed_state(&prng,
  248. i915_selftest.random_seed);
  249. err = alloc_table(&pt, sz, sz, *npages, &prng,
  250. alloc_error);
  251. if (err == -ENOSPC)
  252. break;
  253. if (err)
  254. return err;
  255. prandom_seed_state(&prng,
  256. i915_selftest.random_seed);
  257. err = expect_pfn_sgtable(&pt, *npages, &prng,
  258. "sg_alloc_table",
  259. end_time);
  260. sg_free_table(&pt.st);
  261. if (err)
  262. return err;
  263. }
  264. }
  265. /* Test at least one continuation before accepting oom */
  266. if (size > SG_MAX_SINGLE_ALLOC)
  267. alloc_error = -ENOSPC;
  268. }
  269. return 0;
  270. }
  271. static int igt_sg_trim(void *ignored)
  272. {
  273. IGT_TIMEOUT(end_time);
  274. const unsigned long max = PAGE_SIZE; /* not prime! */
  275. struct pfn_table pt;
  276. unsigned long prime;
  277. int alloc_error = -ENOMEM;
  278. for_each_prime_number(prime, max) {
  279. const npages_fn_t *npages;
  280. int err;
  281. for (npages = npages_funcs; *npages; npages++) {
  282. struct rnd_state prng;
  283. prandom_seed_state(&prng, i915_selftest.random_seed);
  284. err = alloc_table(&pt, prime, max, *npages, &prng,
  285. alloc_error);
  286. if (err == -ENOSPC)
  287. break;
  288. if (err)
  289. return err;
  290. if (i915_sg_trim(&pt.st)) {
  291. if (pt.st.orig_nents != prime ||
  292. pt.st.nents != prime) {
  293. pr_err("i915_sg_trim failed (nents %u, orig_nents %u), expected %lu\n",
  294. pt.st.nents, pt.st.orig_nents, prime);
  295. err = -EINVAL;
  296. } else {
  297. prandom_seed_state(&prng,
  298. i915_selftest.random_seed);
  299. err = expect_pfn_sgtable(&pt,
  300. *npages, &prng,
  301. "i915_sg_trim",
  302. end_time);
  303. }
  304. }
  305. sg_free_table(&pt.st);
  306. if (err)
  307. return err;
  308. }
  309. /* Test at least one continuation before accepting oom */
  310. if (prime > SG_MAX_SINGLE_ALLOC)
  311. alloc_error = -ENOSPC;
  312. }
  313. return 0;
  314. }
  315. int scatterlist_mock_selftests(void)
  316. {
  317. static const struct i915_subtest tests[] = {
  318. SUBTEST(igt_sg_alloc),
  319. SUBTEST(igt_sg_trim),
  320. };
  321. return i915_subtests(tests, NULL);
  322. }