Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | /* * Copyright © 2016 Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ #include <linux/prime_numbers.h> #include <linux/random.h> #include "i915_selftest.h" #include "i915_utils.h" #define PFN_BIAS (1 << 10) struct pfn_table { struct sg_table st; unsigned long start, end; }; typedef unsigned int (*npages_fn_t)(unsigned long n, unsigned long count, struct rnd_state *rnd); static noinline int expect_pfn_sg(struct pfn_table *pt, npages_fn_t npages_fn, struct rnd_state *rnd, const char *who, unsigned long timeout) { struct scatterlist *sg; unsigned long pfn, n; pfn = pt->start; for_each_sg(pt->st.sgl, sg, pt->st.nents, n) { struct page *page = sg_page(sg); unsigned int npages = npages_fn(n, pt->st.nents, rnd); if (page_to_pfn(page) != pfn) { pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sg)\n", __func__, who, pfn, page_to_pfn(page)); return -EINVAL; } if (sg->length != npages * PAGE_SIZE) { pr_err("%s: %s copied wrong sg length, expected size %lu, found %u (using for_each_sg)\n", __func__, who, npages * PAGE_SIZE, sg->length); return -EINVAL; } if (igt_timeout(timeout, "%s timed out\n", who)) return -EINTR; pfn += npages; } if (pfn != pt->end) { pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n", __func__, who, pt->end, pfn); return -EINVAL; } return 0; } static noinline int expect_pfn_sg_page_iter(struct pfn_table *pt, const char *who, unsigned long timeout) { struct sg_page_iter sgiter; unsigned long pfn; pfn = pt->start; for_each_sg_page(pt->st.sgl, &sgiter, pt->st.nents, 0) { struct page *page = sg_page_iter_page(&sgiter); if (page != pfn_to_page(pfn)) { pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sg_page)\n", __func__, who, pfn, page_to_pfn(page)); return -EINVAL; } if (igt_timeout(timeout, "%s timed out\n", who)) return -EINTR; pfn++; } if (pfn != pt->end) { pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n", __func__, who, pt->end, pfn); return -EINVAL; } return 0; } static noinline int expect_pfn_sgtiter(struct pfn_table *pt, const char *who, unsigned long timeout) { struct sgt_iter sgt; struct page *page; unsigned long pfn; pfn = pt->start; for_each_sgt_page(page, sgt, &pt->st) { if (page != pfn_to_page(pfn)) { pr_err("%s: %s left pages out of order, expected pfn %lu, found pfn %lu (using for_each_sgt_page)\n", __func__, who, pfn, page_to_pfn(page)); return -EINVAL; } if (igt_timeout(timeout, "%s timed out\n", who)) return -EINTR; pfn++; } if (pfn != pt->end) { pr_err("%s: %s finished on wrong pfn, expected %lu, found %lu\n", __func__, who, pt->end, pfn); return -EINVAL; } return 0; } static int expect_pfn_sgtable(struct pfn_table *pt, npages_fn_t npages_fn, struct rnd_state *rnd, const char *who, unsigned long timeout) { int err; err = expect_pfn_sg(pt, npages_fn, rnd, who, timeout); if (err) return err; err = expect_pfn_sg_page_iter(pt, who, timeout); if (err) return err; err = expect_pfn_sgtiter(pt, who, timeout); if (err) return err; return 0; } static unsigned int one(unsigned long n, unsigned long count, struct rnd_state *rnd) { return 1; } static unsigned int grow(unsigned long n, unsigned long count, struct rnd_state *rnd) { return n + 1; } static unsigned int shrink(unsigned long n, unsigned long count, struct rnd_state *rnd) { return count - n; } static unsigned int random(unsigned long n, unsigned long count, struct rnd_state *rnd) { return 1 + (prandom_u32_state(rnd) % 1024); } static unsigned int random_page_size_pages(unsigned long n, unsigned long count, struct rnd_state *rnd) { /* 4K, 64K, 2M */ static unsigned int page_count[] = { BIT(12) >> PAGE_SHIFT, BIT(16) >> PAGE_SHIFT, BIT(21) >> PAGE_SHIFT, }; return page_count[(prandom_u32_state(rnd) % 3)]; } static inline bool page_contiguous(struct page *first, struct page *last, unsigned long npages) { return first + npages == last; } static int alloc_table(struct pfn_table *pt, unsigned long count, unsigned long max, npages_fn_t npages_fn, struct rnd_state *rnd, int alloc_error) { struct scatterlist *sg; unsigned long n, pfn; /* restricted by sg_alloc_table */ if (overflows_type(max, unsigned int)) return -E2BIG; if (sg_alloc_table(&pt->st, max, GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN)) return alloc_error; /* count should be less than 20 to prevent overflowing sg->length */ GEM_BUG_ON(overflows_type(count * PAGE_SIZE, sg->length)); /* Construct a table where each scatterlist contains different number * of entries. The idea is to check that we can iterate the individual * pages from inside the coalesced lists. */ pt->start = PFN_BIAS; pfn = pt->start; sg = pt->st.sgl; for (n = 0; n < count; n++) { unsigned long npages = npages_fn(n, count, rnd); /* Nobody expects the Sparse Memmap! */ if (!page_contiguous(pfn_to_page(pfn), pfn_to_page(pfn + npages), npages)) { sg_free_table(&pt->st); return -ENOSPC; } if (n) sg = sg_next(sg); sg_set_page(sg, pfn_to_page(pfn), npages * PAGE_SIZE, 0); GEM_BUG_ON(page_to_pfn(sg_page(sg)) != pfn); GEM_BUG_ON(sg->length != npages * PAGE_SIZE); GEM_BUG_ON(sg->offset != 0); pfn += npages; } sg_mark_end(sg); pt->st.nents = n; pt->end = pfn; return 0; } static const npages_fn_t npages_funcs[] = { one, grow, shrink, random, random_page_size_pages, NULL, }; static int igt_sg_alloc(void *ignored) { IGT_TIMEOUT(end_time); const unsigned long max_order = 20; /* approximating a 4GiB object */ struct rnd_state prng; unsigned long prime; int alloc_error = -ENOMEM; for_each_prime_number(prime, max_order) { unsigned long size = BIT(prime); int offset; for (offset = -1; offset <= 1; offset++) { unsigned long sz = size + offset; const npages_fn_t *npages; struct pfn_table pt; int err; for (npages = npages_funcs; *npages; npages++) { prandom_seed_state(&prng, i915_selftest.random_seed); err = alloc_table(&pt, sz, sz, *npages, &prng, alloc_error); if (err == -ENOSPC) break; if (err) return err; prandom_seed_state(&prng, i915_selftest.random_seed); err = expect_pfn_sgtable(&pt, *npages, &prng, "sg_alloc_table", end_time); sg_free_table(&pt.st); if (err) return err; } } /* Test at least one continuation before accepting oom */ if (size > SG_MAX_SINGLE_ALLOC) alloc_error = -ENOSPC; } return 0; } static int igt_sg_trim(void *ignored) { IGT_TIMEOUT(end_time); const unsigned long max = PAGE_SIZE; /* not prime! */ struct pfn_table pt; unsigned long prime; int alloc_error = -ENOMEM; for_each_prime_number(prime, max) { const npages_fn_t *npages; int err; for (npages = npages_funcs; *npages; npages++) { struct rnd_state prng; prandom_seed_state(&prng, i915_selftest.random_seed); err = alloc_table(&pt, prime, max, *npages, &prng, alloc_error); if (err == -ENOSPC) break; if (err) return err; if (i915_sg_trim(&pt.st)) { if (pt.st.orig_nents != prime || pt.st.nents != prime) { pr_err("i915_sg_trim failed (nents %u, orig_nents %u), expected %lu\n", pt.st.nents, pt.st.orig_nents, prime); err = -EINVAL; } else { prandom_seed_state(&prng, i915_selftest.random_seed); err = expect_pfn_sgtable(&pt, *npages, &prng, "i915_sg_trim", end_time); } } sg_free_table(&pt.st); if (err) return err; } /* Test at least one continuation before accepting oom */ if (prime > SG_MAX_SINGLE_ALLOC) alloc_error = -ENOSPC; } return 0; } int scatterlist_mock_selftests(void) { static const struct i915_subtest tests[] = { SUBTEST(igt_sg_alloc), SUBTEST(igt_sg_trim), }; return i915_subtests(tests, NULL); } |