Skip to content

Commit c43a87f

Browse files
George Spelvinjnikula
authored andcommitted
drm/i915/selftests: Avoid passing a random 0 into ilog2
igt_mm_config() calls ilog2() on the (pseudo)random 21-bit number s>>12. Once in 2 million seeds, this is zero and ilog2 summons the nasal demons. There was an attempt to handle this case with a max(), but that's too late; ms could already be something bizarre. Given that the low 12 bits of s and ms are always zero, it's a lot simpler just to divide them by 4096, then everything fits into 32 bits, and we can easily generate a random number 1 <= s <= 0x1fffff. Fixes: 14d1b9a ("drm/i915: buddy allocator") Signed-off-by: George Spelvin <[email protected]> Cc: Matthew Auld <[email protected]> Cc: Jani Nikula <[email protected]> Cc: Joonas Lahtinen <[email protected]> Cc: Rodrigo Vivi <[email protected]> Cc: [email protected] Reviewed-by: Matthew Auld <[email protected]> Signed-off-by: Chris Wilson <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected] Signed-off-by: Rodrigo Vivi <[email protected]> (cherry picked from commit 21118e8) Signed-off-by: Jani Nikula <[email protected]>
1 parent c67f0c2 commit c43a87f

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

drivers/gpu/drm/i915/selftests/i915_buddy.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include "../i915_selftest.h"
99
#include "i915_random.h"
1010

11-
#define SZ_8G (1ULL << 33)
12-
1311
static void __igt_dump_block(struct i915_buddy_mm *mm,
1412
struct i915_buddy_block *block,
1513
bool buddy)
@@ -281,18 +279,22 @@ static int igt_check_mm(struct i915_buddy_mm *mm)
281279
static void igt_mm_config(u64 *size, u64 *chunk_size)
282280
{
283281
I915_RND_STATE(prng);
284-
u64 s, ms;
282+
u32 s, ms;
285283

286284
/* Nothing fancy, just try to get an interesting bit pattern */
287285

288286
prandom_seed_state(&prng, i915_selftest.random_seed);
289287

290-
s = i915_prandom_u64_state(&prng) & (SZ_8G - 1);
291-
ms = BIT_ULL(12 + (prandom_u32_state(&prng) % ilog2(s >> 12)));
292-
s = max(s & -ms, ms);
288+
/* Let size be a random number of pages up to 8 GB (2M pages) */
289+
s = 1 + i915_prandom_u32_max_state((BIT(33 - 12)) - 1, &prng);
290+
/* Let the chunk size be a random power of 2 less than size */
291+
ms = BIT(i915_prandom_u32_max_state(ilog2(s), &prng));
292+
/* Round size down to the chunk size */
293+
s &= -ms;
293294

294-
*chunk_size = ms;
295-
*size = s;
295+
/* Convert from pages to bytes */
296+
*chunk_size = (u64)ms << 12;
297+
*size = (u64)s << 12;
296298
}
297299

298300
static int igt_buddy_alloc_smoke(void *arg)

0 commit comments

Comments
 (0)