2 * Copyright © 2016 Intel Corporation
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:
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
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
24 #include <linux/prime_numbers.h>
25 #include <linux/random.h>
27 #include "i915_selftest.h"
28 #include "i915_utils.h"
30 #define PFN_BIAS (1 << 10)
34 unsigned long start, end;
37 typedef unsigned int (*npages_fn_t)(unsigned long n,
39 struct rnd_state *rnd);
41 static noinline int expect_pfn_sg(struct pfn_table *pt,
42 npages_fn_t npages_fn,
43 struct rnd_state *rnd,
45 unsigned long timeout)
47 struct scatterlist *sg;
51 for_each_sg(pt->st.sgl, sg, pt->st.nents, n) {
52 struct page *page = sg_page(sg);
53 unsigned int npages = npages_fn(n, pt->st.nents, rnd);
55 if (page_to_pfn(page) != pfn) {
56 pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sg)\n",
57 __func__, who, pfn, page_to_pfn(page));
61 if (sg->length != npages * PAGE_SIZE) {
62 pr_err("%s: %s copied wrong sg length, expected size %lu, found %u (using for_each_sg)\n",
63 __func__, who, npages * PAGE_SIZE, sg->length);
67 if (igt_timeout(timeout, "%s timed out\n", who))
73 pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n",
74 __func__, who, pt->end, pfn);
81 static noinline int expect_pfn_sg_page_iter(struct pfn_table *pt,
83 unsigned long timeout)
85 struct sg_page_iter sgiter;
89 for_each_sg_page(pt->st.sgl, &sgiter, pt->st.nents, 0) {
90 struct page *page = sg_page_iter_page(&sgiter);
92 if (page != pfn_to_page(pfn)) {
93 pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sg_page)\n",
94 __func__, who, pfn, page_to_pfn(page));
98 if (igt_timeout(timeout, "%s timed out\n", who))
103 if (pfn != pt->end) {
104 pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n",
105 __func__, who, pt->end, pfn);
112 static noinline int expect_pfn_sgtiter(struct pfn_table *pt,
114 unsigned long timeout)
121 for_each_sgt_page(page, sgt, &pt->st) {
122 if (page != pfn_to_page(pfn)) {
123 pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sgt_page)\n",
124 __func__, who, pfn, page_to_pfn(page));
128 if (igt_timeout(timeout, "%s timed out\n", who))
133 if (pfn != pt->end) {
134 pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n",
135 __func__, who, pt->end, pfn);
142 static int expect_pfn_sgtable(struct pfn_table *pt,
143 npages_fn_t npages_fn,
144 struct rnd_state *rnd,
146 unsigned long timeout)
150 err = expect_pfn_sg(pt, npages_fn, rnd, who, timeout);
154 err = expect_pfn_sg_page_iter(pt, who, timeout);
158 err = expect_pfn_sgtiter(pt, who, timeout);
165 static unsigned int one(unsigned long n,
167 struct rnd_state *rnd)
172 static unsigned int grow(unsigned long n,
174 struct rnd_state *rnd)
179 static unsigned int shrink(unsigned long n,
181 struct rnd_state *rnd)
186 static unsigned int random(unsigned long n,
188 struct rnd_state *rnd)
190 return 1 + (prandom_u32_state(rnd) % 1024);
193 static unsigned int random_page_size_pages(unsigned long n,
195 struct rnd_state *rnd)
198 static unsigned int page_count[] = {
199 BIT(12) >> PAGE_SHIFT,
200 BIT(16) >> PAGE_SHIFT,
201 BIT(21) >> PAGE_SHIFT,
204 return page_count[(prandom_u32_state(rnd) % 3)];
207 static inline bool page_contiguous(struct page *first,
209 unsigned long npages)
211 return first + npages == last;
214 static int alloc_table(struct pfn_table *pt,
215 unsigned long count, unsigned long max,
216 npages_fn_t npages_fn,
217 struct rnd_state *rnd,
220 struct scatterlist *sg;
221 unsigned long n, pfn;
223 /* restricted by sg_alloc_table */
224 if (overflows_type(max, unsigned int))
227 if (sg_alloc_table(&pt->st, max,
228 GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN))
231 /* count should be less than 20 to prevent overflowing sg->length */
232 GEM_BUG_ON(overflows_type(count * PAGE_SIZE, sg->length));
234 /* Construct a table where each scatterlist contains different number
235 * of entries. The idea is to check that we can iterate the individual
236 * pages from inside the coalesced lists.
238 pt->start = PFN_BIAS;
241 for (n = 0; n < count; n++) {
242 unsigned long npages = npages_fn(n, count, rnd);
244 /* Nobody expects the Sparse Memmap! */
245 if (!page_contiguous(pfn_to_page(pfn),
246 pfn_to_page(pfn + npages),
248 sg_free_table(&pt->st);
254 sg_set_page(sg, pfn_to_page(pfn), npages * PAGE_SIZE, 0);
256 GEM_BUG_ON(page_to_pfn(sg_page(sg)) != pfn);
257 GEM_BUG_ON(sg->length != npages * PAGE_SIZE);
258 GEM_BUG_ON(sg->offset != 0);
269 static const npages_fn_t npages_funcs[] = {
274 random_page_size_pages,
278 static int igt_sg_alloc(void *ignored)
280 IGT_TIMEOUT(end_time);
281 const unsigned long max_order = 20; /* approximating a 4GiB object */
282 struct rnd_state prng;
284 int alloc_error = -ENOMEM;
286 for_each_prime_number(prime, max_order) {
287 unsigned long size = BIT(prime);
290 for (offset = -1; offset <= 1; offset++) {
291 unsigned long sz = size + offset;
292 const npages_fn_t *npages;
296 for (npages = npages_funcs; *npages; npages++) {
297 prandom_seed_state(&prng,
298 i915_selftest.random_seed);
299 err = alloc_table(&pt, sz, sz, *npages, &prng,
306 prandom_seed_state(&prng,
307 i915_selftest.random_seed);
308 err = expect_pfn_sgtable(&pt, *npages, &prng,
311 sg_free_table(&pt.st);
317 /* Test at least one continuation before accepting oom */
318 if (size > SG_MAX_SINGLE_ALLOC)
319 alloc_error = -ENOSPC;
325 static int igt_sg_trim(void *ignored)
327 IGT_TIMEOUT(end_time);
328 const unsigned long max = PAGE_SIZE; /* not prime! */
331 int alloc_error = -ENOMEM;
333 for_each_prime_number(prime, max) {
334 const npages_fn_t *npages;
337 for (npages = npages_funcs; *npages; npages++) {
338 struct rnd_state prng;
340 prandom_seed_state(&prng, i915_selftest.random_seed);
341 err = alloc_table(&pt, prime, max, *npages, &prng,
348 if (i915_sg_trim(&pt.st)) {
349 if (pt.st.orig_nents != prime ||
350 pt.st.nents != prime) {
351 pr_err("i915_sg_trim failed (nents %u, orig_nents %u), expected %lu\n",
352 pt.st.nents, pt.st.orig_nents, prime);
355 prandom_seed_state(&prng,
356 i915_selftest.random_seed);
357 err = expect_pfn_sgtable(&pt,
363 sg_free_table(&pt.st);
368 /* Test at least one continuation before accepting oom */
369 if (prime > SG_MAX_SINGLE_ALLOC)
370 alloc_error = -ENOSPC;
376 int scatterlist_mock_selftests(void)
378 static const struct i915_subtest tests[] = {
379 SUBTEST(igt_sg_alloc),
380 SUBTEST(igt_sg_trim),
383 return i915_subtests(tests, NULL);