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 if (sg_alloc_table(&pt
->st
, max
,
224 GFP_KERNEL
| __GFP_NORETRY
| __GFP_NOWARN
))
227 /* count should be less than 20 to prevent overflowing sg->length */
228 GEM_BUG_ON(overflows_type(count
* PAGE_SIZE
, sg
->length
));
230 /* Construct a table where each scatterlist contains different number
231 * of entries. The idea is to check that we can iterate the individual
232 * pages from inside the coalesced lists.
234 pt
->start
= PFN_BIAS
;
237 for (n
= 0; n
< count
; n
++) {
238 unsigned long npages
= npages_fn(n
, count
, rnd
);
240 /* Nobody expects the Sparse Memmap! */
241 if (!page_contiguous(pfn_to_page(pfn
),
242 pfn_to_page(pfn
+ npages
),
244 sg_free_table(&pt
->st
);
250 sg_set_page(sg
, pfn_to_page(pfn
), npages
* PAGE_SIZE
, 0);
252 GEM_BUG_ON(page_to_pfn(sg_page(sg
)) != pfn
);
253 GEM_BUG_ON(sg
->length
!= npages
* PAGE_SIZE
);
254 GEM_BUG_ON(sg
->offset
!= 0);
265 static const npages_fn_t npages_funcs
[] = {
270 random_page_size_pages
,
274 static int igt_sg_alloc(void *ignored
)
276 IGT_TIMEOUT(end_time
);
277 const unsigned long max_order
= 20; /* approximating a 4GiB object */
278 struct rnd_state prng
;
280 int alloc_error
= -ENOMEM
;
282 for_each_prime_number(prime
, max_order
) {
283 unsigned long size
= BIT(prime
);
286 for (offset
= -1; offset
<= 1; offset
++) {
287 unsigned long sz
= size
+ offset
;
288 const npages_fn_t
*npages
;
292 for (npages
= npages_funcs
; *npages
; npages
++) {
293 prandom_seed_state(&prng
,
294 i915_selftest
.random_seed
);
295 err
= alloc_table(&pt
, sz
, sz
, *npages
, &prng
,
302 prandom_seed_state(&prng
,
303 i915_selftest
.random_seed
);
304 err
= expect_pfn_sgtable(&pt
, *npages
, &prng
,
307 sg_free_table(&pt
.st
);
313 /* Test at least one continuation before accepting oom */
314 if (size
> SG_MAX_SINGLE_ALLOC
)
315 alloc_error
= -ENOSPC
;
321 static int igt_sg_trim(void *ignored
)
323 IGT_TIMEOUT(end_time
);
324 const unsigned long max
= PAGE_SIZE
; /* not prime! */
327 int alloc_error
= -ENOMEM
;
329 for_each_prime_number(prime
, max
) {
330 const npages_fn_t
*npages
;
333 for (npages
= npages_funcs
; *npages
; npages
++) {
334 struct rnd_state prng
;
336 prandom_seed_state(&prng
, i915_selftest
.random_seed
);
337 err
= alloc_table(&pt
, prime
, max
, *npages
, &prng
,
344 if (i915_sg_trim(&pt
.st
)) {
345 if (pt
.st
.orig_nents
!= prime
||
346 pt
.st
.nents
!= prime
) {
347 pr_err("i915_sg_trim failed (nents %u, orig_nents %u), expected %lu\n",
348 pt
.st
.nents
, pt
.st
.orig_nents
, prime
);
351 prandom_seed_state(&prng
,
352 i915_selftest
.random_seed
);
353 err
= expect_pfn_sgtable(&pt
,
359 sg_free_table(&pt
.st
);
364 /* Test at least one continuation before accepting oom */
365 if (prime
> SG_MAX_SINGLE_ALLOC
)
366 alloc_error
= -ENOSPC
;
372 int scatterlist_mock_selftests(void)
374 static const struct i915_subtest tests
[] = {
375 SUBTEST(igt_sg_alloc
),
376 SUBTEST(igt_sg_trim
),
379 return i915_subtests(tests
, NULL
);