1935a2f77SChris Wilson /*
2935a2f77SChris Wilson * Copyright © 2016 Intel Corporation
3935a2f77SChris Wilson *
4935a2f77SChris Wilson * Permission is hereby granted, free of charge, to any person obtaining a
5935a2f77SChris Wilson * copy of this software and associated documentation files (the "Software"),
6935a2f77SChris Wilson * to deal in the Software without restriction, including without limitation
7935a2f77SChris Wilson * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8935a2f77SChris Wilson * and/or sell copies of the Software, and to permit persons to whom the
9935a2f77SChris Wilson * Software is furnished to do so, subject to the following conditions:
10935a2f77SChris Wilson *
11935a2f77SChris Wilson * The above copyright notice and this permission notice (including the next
12935a2f77SChris Wilson * paragraph) shall be included in all copies or substantial portions of the
13935a2f77SChris Wilson * Software.
14935a2f77SChris Wilson *
15935a2f77SChris Wilson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16935a2f77SChris Wilson * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17935a2f77SChris Wilson * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18935a2f77SChris Wilson * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19935a2f77SChris Wilson * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20935a2f77SChris Wilson * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21935a2f77SChris Wilson * IN THE SOFTWARE.
22935a2f77SChris Wilson */
23935a2f77SChris Wilson
24935a2f77SChris Wilson #include <linux/prime_numbers.h>
25935a2f77SChris Wilson #include <linux/random.h>
26935a2f77SChris Wilson
2737d63f8fSChris Wilson #include "i915_selftest.h"
2837d63f8fSChris Wilson #include "i915_utils.h"
29935a2f77SChris Wilson
30935a2f77SChris Wilson #define PFN_BIAS (1 << 10)
31935a2f77SChris Wilson
32935a2f77SChris Wilson struct pfn_table {
33935a2f77SChris Wilson struct sg_table st;
34935a2f77SChris Wilson unsigned long start, end;
35935a2f77SChris Wilson };
36935a2f77SChris Wilson
37935a2f77SChris Wilson typedef unsigned int (*npages_fn_t)(unsigned long n,
38935a2f77SChris Wilson unsigned long count,
39935a2f77SChris Wilson struct rnd_state *rnd);
40935a2f77SChris Wilson
expect_pfn_sg(struct pfn_table * pt,npages_fn_t npages_fn,struct rnd_state * rnd,const char * who,unsigned long timeout)41935a2f77SChris Wilson static noinline int expect_pfn_sg(struct pfn_table *pt,
42935a2f77SChris Wilson npages_fn_t npages_fn,
43935a2f77SChris Wilson struct rnd_state *rnd,
44935a2f77SChris Wilson const char *who,
45935a2f77SChris Wilson unsigned long timeout)
46935a2f77SChris Wilson {
47935a2f77SChris Wilson struct scatterlist *sg;
48935a2f77SChris Wilson unsigned long pfn, n;
49935a2f77SChris Wilson
50935a2f77SChris Wilson pfn = pt->start;
51935a2f77SChris Wilson for_each_sg(pt->st.sgl, sg, pt->st.nents, n) {
52935a2f77SChris Wilson struct page *page = sg_page(sg);
53935a2f77SChris Wilson unsigned int npages = npages_fn(n, pt->st.nents, rnd);
54935a2f77SChris Wilson
55935a2f77SChris Wilson if (page_to_pfn(page) != pfn) {
56935a2f77SChris Wilson pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sg)\n",
57935a2f77SChris Wilson __func__, who, pfn, page_to_pfn(page));
58935a2f77SChris Wilson return -EINVAL;
59935a2f77SChris Wilson }
60935a2f77SChris Wilson
61935a2f77SChris Wilson if (sg->length != npages * PAGE_SIZE) {
62935a2f77SChris Wilson pr_err("%s: %s copied wrong sg length, expected size %lu, found %u (using for_each_sg)\n",
63935a2f77SChris Wilson __func__, who, npages * PAGE_SIZE, sg->length);
64935a2f77SChris Wilson return -EINVAL;
65935a2f77SChris Wilson }
66935a2f77SChris Wilson
67935a2f77SChris Wilson if (igt_timeout(timeout, "%s timed out\n", who))
68935a2f77SChris Wilson return -EINTR;
69935a2f77SChris Wilson
70935a2f77SChris Wilson pfn += npages;
71935a2f77SChris Wilson }
72935a2f77SChris Wilson if (pfn != pt->end) {
73935a2f77SChris Wilson pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n",
74935a2f77SChris Wilson __func__, who, pt->end, pfn);
75935a2f77SChris Wilson return -EINVAL;
76935a2f77SChris Wilson }
77935a2f77SChris Wilson
78935a2f77SChris Wilson return 0;
79935a2f77SChris Wilson }
80935a2f77SChris Wilson
expect_pfn_sg_page_iter(struct pfn_table * pt,const char * who,unsigned long timeout)81935a2f77SChris Wilson static noinline int expect_pfn_sg_page_iter(struct pfn_table *pt,
82935a2f77SChris Wilson const char *who,
83935a2f77SChris Wilson unsigned long timeout)
84935a2f77SChris Wilson {
85935a2f77SChris Wilson struct sg_page_iter sgiter;
86935a2f77SChris Wilson unsigned long pfn;
87935a2f77SChris Wilson
88935a2f77SChris Wilson pfn = pt->start;
89935a2f77SChris Wilson for_each_sg_page(pt->st.sgl, &sgiter, pt->st.nents, 0) {
90935a2f77SChris Wilson struct page *page = sg_page_iter_page(&sgiter);
91935a2f77SChris Wilson
92935a2f77SChris Wilson if (page != pfn_to_page(pfn)) {
93935a2f77SChris Wilson pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sg_page)\n",
94935a2f77SChris Wilson __func__, who, pfn, page_to_pfn(page));
95935a2f77SChris Wilson return -EINVAL;
96935a2f77SChris Wilson }
97935a2f77SChris Wilson
98935a2f77SChris Wilson if (igt_timeout(timeout, "%s timed out\n", who))
99935a2f77SChris Wilson return -EINTR;
100935a2f77SChris Wilson
101935a2f77SChris Wilson pfn++;
102935a2f77SChris Wilson }
103935a2f77SChris Wilson if (pfn != pt->end) {
104935a2f77SChris Wilson pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n",
105935a2f77SChris Wilson __func__, who, pt->end, pfn);
106935a2f77SChris Wilson return -EINVAL;
107935a2f77SChris Wilson }
108935a2f77SChris Wilson
109935a2f77SChris Wilson return 0;
110935a2f77SChris Wilson }
111935a2f77SChris Wilson
expect_pfn_sgtiter(struct pfn_table * pt,const char * who,unsigned long timeout)112935a2f77SChris Wilson static noinline int expect_pfn_sgtiter(struct pfn_table *pt,
113935a2f77SChris Wilson const char *who,
114935a2f77SChris Wilson unsigned long timeout)
115935a2f77SChris Wilson {
116935a2f77SChris Wilson struct sgt_iter sgt;
117935a2f77SChris Wilson struct page *page;
118935a2f77SChris Wilson unsigned long pfn;
119935a2f77SChris Wilson
120935a2f77SChris Wilson pfn = pt->start;
121935a2f77SChris Wilson for_each_sgt_page(page, sgt, &pt->st) {
122935a2f77SChris Wilson if (page != pfn_to_page(pfn)) {
123935a2f77SChris Wilson pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sgt_page)\n",
124935a2f77SChris Wilson __func__, who, pfn, page_to_pfn(page));
125935a2f77SChris Wilson return -EINVAL;
126935a2f77SChris Wilson }
127935a2f77SChris Wilson
128935a2f77SChris Wilson if (igt_timeout(timeout, "%s timed out\n", who))
129935a2f77SChris Wilson return -EINTR;
130935a2f77SChris Wilson
131935a2f77SChris Wilson pfn++;
132935a2f77SChris Wilson }
133935a2f77SChris Wilson if (pfn != pt->end) {
134935a2f77SChris Wilson pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n",
135935a2f77SChris Wilson __func__, who, pt->end, pfn);
136935a2f77SChris Wilson return -EINVAL;
137935a2f77SChris Wilson }
138935a2f77SChris Wilson
139935a2f77SChris Wilson return 0;
140935a2f77SChris Wilson }
141935a2f77SChris Wilson
expect_pfn_sgtable(struct pfn_table * pt,npages_fn_t npages_fn,struct rnd_state * rnd,const char * who,unsigned long timeout)142935a2f77SChris Wilson static int expect_pfn_sgtable(struct pfn_table *pt,
143935a2f77SChris Wilson npages_fn_t npages_fn,
144935a2f77SChris Wilson struct rnd_state *rnd,
145935a2f77SChris Wilson const char *who,
146935a2f77SChris Wilson unsigned long timeout)
147935a2f77SChris Wilson {
148935a2f77SChris Wilson int err;
149935a2f77SChris Wilson
150935a2f77SChris Wilson err = expect_pfn_sg(pt, npages_fn, rnd, who, timeout);
151935a2f77SChris Wilson if (err)
152935a2f77SChris Wilson return err;
153935a2f77SChris Wilson
154935a2f77SChris Wilson err = expect_pfn_sg_page_iter(pt, who, timeout);
155935a2f77SChris Wilson if (err)
156935a2f77SChris Wilson return err;
157935a2f77SChris Wilson
158935a2f77SChris Wilson err = expect_pfn_sgtiter(pt, who, timeout);
159935a2f77SChris Wilson if (err)
160935a2f77SChris Wilson return err;
161935a2f77SChris Wilson
162935a2f77SChris Wilson return 0;
163935a2f77SChris Wilson }
164935a2f77SChris Wilson
one(unsigned long n,unsigned long count,struct rnd_state * rnd)165935a2f77SChris Wilson static unsigned int one(unsigned long n,
166935a2f77SChris Wilson unsigned long count,
167935a2f77SChris Wilson struct rnd_state *rnd)
168935a2f77SChris Wilson {
169935a2f77SChris Wilson return 1;
170935a2f77SChris Wilson }
171935a2f77SChris Wilson
grow(unsigned long n,unsigned long count,struct rnd_state * rnd)172935a2f77SChris Wilson static unsigned int grow(unsigned long n,
173935a2f77SChris Wilson unsigned long count,
174935a2f77SChris Wilson struct rnd_state *rnd)
175935a2f77SChris Wilson {
176935a2f77SChris Wilson return n + 1;
177935a2f77SChris Wilson }
178935a2f77SChris Wilson
shrink(unsigned long n,unsigned long count,struct rnd_state * rnd)179935a2f77SChris Wilson static unsigned int shrink(unsigned long n,
180935a2f77SChris Wilson unsigned long count,
181935a2f77SChris Wilson struct rnd_state *rnd)
182935a2f77SChris Wilson {
183935a2f77SChris Wilson return count - n;
184935a2f77SChris Wilson }
185935a2f77SChris Wilson
random(unsigned long n,unsigned long count,struct rnd_state * rnd)186935a2f77SChris Wilson static unsigned int random(unsigned long n,
187935a2f77SChris Wilson unsigned long count,
188935a2f77SChris Wilson struct rnd_state *rnd)
189935a2f77SChris Wilson {
190935a2f77SChris Wilson return 1 + (prandom_u32_state(rnd) % 1024);
191935a2f77SChris Wilson }
192935a2f77SChris Wilson
random_page_size_pages(unsigned long n,unsigned long count,struct rnd_state * rnd)1937924d9d4SMatthew Auld static unsigned int random_page_size_pages(unsigned long n,
1947924d9d4SMatthew Auld unsigned long count,
1957924d9d4SMatthew Auld struct rnd_state *rnd)
1967924d9d4SMatthew Auld {
1977924d9d4SMatthew Auld /* 4K, 64K, 2M */
1987924d9d4SMatthew Auld static unsigned int page_count[] = {
1997924d9d4SMatthew Auld BIT(12) >> PAGE_SHIFT,
2007924d9d4SMatthew Auld BIT(16) >> PAGE_SHIFT,
2017924d9d4SMatthew Auld BIT(21) >> PAGE_SHIFT,
2027924d9d4SMatthew Auld };
2037924d9d4SMatthew Auld
2047924d9d4SMatthew Auld return page_count[(prandom_u32_state(rnd) % 3)];
2057924d9d4SMatthew Auld }
2067924d9d4SMatthew Auld
page_contiguous(struct page * first,struct page * last,unsigned long npages)207272bce17SArnd Bergmann static inline bool page_contiguous(struct page *first,
208272bce17SArnd Bergmann struct page *last,
209272bce17SArnd Bergmann unsigned long npages)
210272bce17SArnd Bergmann {
211272bce17SArnd Bergmann return first + npages == last;
212272bce17SArnd Bergmann }
213272bce17SArnd Bergmann
alloc_table(struct pfn_table * pt,unsigned long count,unsigned long max,npages_fn_t npages_fn,struct rnd_state * rnd,int alloc_error)214935a2f77SChris Wilson static int alloc_table(struct pfn_table *pt,
215935a2f77SChris Wilson unsigned long count, unsigned long max,
216935a2f77SChris Wilson npages_fn_t npages_fn,
217935a2f77SChris Wilson struct rnd_state *rnd,
218935a2f77SChris Wilson int alloc_error)
219935a2f77SChris Wilson {
220935a2f77SChris Wilson struct scatterlist *sg;
221935a2f77SChris Wilson unsigned long n, pfn;
222935a2f77SChris Wilson
223*c3bfba9aSChris Wilson /* restricted by sg_alloc_table */
224*c3bfba9aSChris Wilson if (overflows_type(max, unsigned int))
225*c3bfba9aSChris Wilson return -E2BIG;
226*c3bfba9aSChris Wilson
227935a2f77SChris Wilson if (sg_alloc_table(&pt->st, max,
228935a2f77SChris Wilson GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN))
229935a2f77SChris Wilson return alloc_error;
230935a2f77SChris Wilson
231935a2f77SChris Wilson /* count should be less than 20 to prevent overflowing sg->length */
232935a2f77SChris Wilson GEM_BUG_ON(overflows_type(count * PAGE_SIZE, sg->length));
233935a2f77SChris Wilson
234935a2f77SChris Wilson /* Construct a table where each scatterlist contains different number
235935a2f77SChris Wilson * of entries. The idea is to check that we can iterate the individual
236935a2f77SChris Wilson * pages from inside the coalesced lists.
237935a2f77SChris Wilson */
238935a2f77SChris Wilson pt->start = PFN_BIAS;
239935a2f77SChris Wilson pfn = pt->start;
240935a2f77SChris Wilson sg = pt->st.sgl;
241935a2f77SChris Wilson for (n = 0; n < count; n++) {
242935a2f77SChris Wilson unsigned long npages = npages_fn(n, count, rnd);
243935a2f77SChris Wilson
244935a2f77SChris Wilson /* Nobody expects the Sparse Memmap! */
245272bce17SArnd Bergmann if (!page_contiguous(pfn_to_page(pfn),
246272bce17SArnd Bergmann pfn_to_page(pfn + npages),
247272bce17SArnd Bergmann npages)) {
248935a2f77SChris Wilson sg_free_table(&pt->st);
249935a2f77SChris Wilson return -ENOSPC;
250935a2f77SChris Wilson }
251935a2f77SChris Wilson
252935a2f77SChris Wilson if (n)
253935a2f77SChris Wilson sg = sg_next(sg);
254935a2f77SChris Wilson sg_set_page(sg, pfn_to_page(pfn), npages * PAGE_SIZE, 0);
255935a2f77SChris Wilson
256935a2f77SChris Wilson GEM_BUG_ON(page_to_pfn(sg_page(sg)) != pfn);
257935a2f77SChris Wilson GEM_BUG_ON(sg->length != npages * PAGE_SIZE);
258935a2f77SChris Wilson GEM_BUG_ON(sg->offset != 0);
259935a2f77SChris Wilson
260935a2f77SChris Wilson pfn += npages;
261935a2f77SChris Wilson }
262935a2f77SChris Wilson sg_mark_end(sg);
263935a2f77SChris Wilson pt->st.nents = n;
264935a2f77SChris Wilson pt->end = pfn;
265935a2f77SChris Wilson
266935a2f77SChris Wilson return 0;
267935a2f77SChris Wilson }
268935a2f77SChris Wilson
269935a2f77SChris Wilson static const npages_fn_t npages_funcs[] = {
270935a2f77SChris Wilson one,
271935a2f77SChris Wilson grow,
272935a2f77SChris Wilson shrink,
273935a2f77SChris Wilson random,
2747924d9d4SMatthew Auld random_page_size_pages,
275935a2f77SChris Wilson NULL,
276935a2f77SChris Wilson };
277935a2f77SChris Wilson
igt_sg_alloc(void * ignored)278935a2f77SChris Wilson static int igt_sg_alloc(void *ignored)
279935a2f77SChris Wilson {
280935a2f77SChris Wilson IGT_TIMEOUT(end_time);
281935a2f77SChris Wilson const unsigned long max_order = 20; /* approximating a 4GiB object */
282935a2f77SChris Wilson struct rnd_state prng;
283935a2f77SChris Wilson unsigned long prime;
284935a2f77SChris Wilson int alloc_error = -ENOMEM;
285935a2f77SChris Wilson
286935a2f77SChris Wilson for_each_prime_number(prime, max_order) {
287935a2f77SChris Wilson unsigned long size = BIT(prime);
288935a2f77SChris Wilson int offset;
289935a2f77SChris Wilson
290935a2f77SChris Wilson for (offset = -1; offset <= 1; offset++) {
291935a2f77SChris Wilson unsigned long sz = size + offset;
292935a2f77SChris Wilson const npages_fn_t *npages;
293935a2f77SChris Wilson struct pfn_table pt;
294935a2f77SChris Wilson int err;
295935a2f77SChris Wilson
296935a2f77SChris Wilson for (npages = npages_funcs; *npages; npages++) {
297935a2f77SChris Wilson prandom_seed_state(&prng,
298935a2f77SChris Wilson i915_selftest.random_seed);
299935a2f77SChris Wilson err = alloc_table(&pt, sz, sz, *npages, &prng,
300935a2f77SChris Wilson alloc_error);
301935a2f77SChris Wilson if (err == -ENOSPC)
302935a2f77SChris Wilson break;
303935a2f77SChris Wilson if (err)
304935a2f77SChris Wilson return err;
305935a2f77SChris Wilson
306935a2f77SChris Wilson prandom_seed_state(&prng,
307935a2f77SChris Wilson i915_selftest.random_seed);
308935a2f77SChris Wilson err = expect_pfn_sgtable(&pt, *npages, &prng,
309935a2f77SChris Wilson "sg_alloc_table",
310935a2f77SChris Wilson end_time);
311935a2f77SChris Wilson sg_free_table(&pt.st);
312935a2f77SChris Wilson if (err)
313935a2f77SChris Wilson return err;
314935a2f77SChris Wilson }
315935a2f77SChris Wilson }
316935a2f77SChris Wilson
317935a2f77SChris Wilson /* Test at least one continuation before accepting oom */
318935a2f77SChris Wilson if (size > SG_MAX_SINGLE_ALLOC)
319935a2f77SChris Wilson alloc_error = -ENOSPC;
320935a2f77SChris Wilson }
321935a2f77SChris Wilson
322935a2f77SChris Wilson return 0;
323935a2f77SChris Wilson }
324935a2f77SChris Wilson
igt_sg_trim(void * ignored)325935a2f77SChris Wilson static int igt_sg_trim(void *ignored)
326935a2f77SChris Wilson {
327935a2f77SChris Wilson IGT_TIMEOUT(end_time);
328935a2f77SChris Wilson const unsigned long max = PAGE_SIZE; /* not prime! */
329935a2f77SChris Wilson struct pfn_table pt;
330935a2f77SChris Wilson unsigned long prime;
331935a2f77SChris Wilson int alloc_error = -ENOMEM;
332935a2f77SChris Wilson
333935a2f77SChris Wilson for_each_prime_number(prime, max) {
334935a2f77SChris Wilson const npages_fn_t *npages;
335935a2f77SChris Wilson int err;
336935a2f77SChris Wilson
337935a2f77SChris Wilson for (npages = npages_funcs; *npages; npages++) {
338935a2f77SChris Wilson struct rnd_state prng;
339935a2f77SChris Wilson
340935a2f77SChris Wilson prandom_seed_state(&prng, i915_selftest.random_seed);
341935a2f77SChris Wilson err = alloc_table(&pt, prime, max, *npages, &prng,
342935a2f77SChris Wilson alloc_error);
343935a2f77SChris Wilson if (err == -ENOSPC)
344935a2f77SChris Wilson break;
345935a2f77SChris Wilson if (err)
346935a2f77SChris Wilson return err;
347935a2f77SChris Wilson
348935a2f77SChris Wilson if (i915_sg_trim(&pt.st)) {
349935a2f77SChris Wilson if (pt.st.orig_nents != prime ||
350935a2f77SChris Wilson pt.st.nents != prime) {
351935a2f77SChris Wilson pr_err("i915_sg_trim failed (nents %u, orig_nents %u), expected %lu\n",
352935a2f77SChris Wilson pt.st.nents, pt.st.orig_nents, prime);
353935a2f77SChris Wilson err = -EINVAL;
354935a2f77SChris Wilson } else {
355935a2f77SChris Wilson prandom_seed_state(&prng,
356935a2f77SChris Wilson i915_selftest.random_seed);
357935a2f77SChris Wilson err = expect_pfn_sgtable(&pt,
358935a2f77SChris Wilson *npages, &prng,
359935a2f77SChris Wilson "i915_sg_trim",
360935a2f77SChris Wilson end_time);
361935a2f77SChris Wilson }
362935a2f77SChris Wilson }
363935a2f77SChris Wilson sg_free_table(&pt.st);
364935a2f77SChris Wilson if (err)
365935a2f77SChris Wilson return err;
366935a2f77SChris Wilson }
367935a2f77SChris Wilson
368935a2f77SChris Wilson /* Test at least one continuation before accepting oom */
369935a2f77SChris Wilson if (prime > SG_MAX_SINGLE_ALLOC)
370935a2f77SChris Wilson alloc_error = -ENOSPC;
371935a2f77SChris Wilson }
372935a2f77SChris Wilson
373935a2f77SChris Wilson return 0;
374935a2f77SChris Wilson }
375935a2f77SChris Wilson
scatterlist_mock_selftests(void)376935a2f77SChris Wilson int scatterlist_mock_selftests(void)
377935a2f77SChris Wilson {
378935a2f77SChris Wilson static const struct i915_subtest tests[] = {
379935a2f77SChris Wilson SUBTEST(igt_sg_alloc),
380935a2f77SChris Wilson SUBTEST(igt_sg_trim),
381935a2f77SChris Wilson };
382935a2f77SChris Wilson
383935a2f77SChris Wilson return i915_subtests(tests, NULL);
384935a2f77SChris Wilson }
385