Commit 21118e8e authored by George Spelvin's avatar George Spelvin Committed by Rodrigo Vivi

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: 14d1b9a6 ("drm/i915: buddy allocator")
Signed-off-by: default avatarGeorge Spelvin <lkml@sdf.org>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Jani Nikula <jani.nikula@linux.intel.com>
Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: intel-gfx@lists.freedesktop.org
Reviewed-by: default avatarMatthew Auld <matthew.auld@intel.com>
Signed-off-by: default avatarChris Wilson <chris@chris-wilson.co.uk>
Link: https://patchwork.freedesktop.org/patch/msgid/20200325192429.GA8865@SDF.ORGSigned-off-by: default avatarRodrigo Vivi <rodrigo.vivi@intel.com>
parent 13149e8b
...@@ -8,8 +8,6 @@ ...@@ -8,8 +8,6 @@
#include "../i915_selftest.h" #include "../i915_selftest.h"
#include "i915_random.h" #include "i915_random.h"
#define SZ_8G (1ULL << 33)
static void __igt_dump_block(struct i915_buddy_mm *mm, static void __igt_dump_block(struct i915_buddy_mm *mm,
struct i915_buddy_block *block, struct i915_buddy_block *block,
bool buddy) bool buddy)
...@@ -281,18 +279,22 @@ static int igt_check_mm(struct i915_buddy_mm *mm) ...@@ -281,18 +279,22 @@ static int igt_check_mm(struct i915_buddy_mm *mm)
static void igt_mm_config(u64 *size, u64 *chunk_size) static void igt_mm_config(u64 *size, u64 *chunk_size)
{ {
I915_RND_STATE(prng); I915_RND_STATE(prng);
u64 s, ms; u32 s, ms;
/* Nothing fancy, just try to get an interesting bit pattern */ /* Nothing fancy, just try to get an interesting bit pattern */
prandom_seed_state(&prng, i915_selftest.random_seed); prandom_seed_state(&prng, i915_selftest.random_seed);
s = i915_prandom_u64_state(&prng) & (SZ_8G - 1); /* Let size be a random number of pages up to 8 GB (2M pages) */
ms = BIT_ULL(12 + (prandom_u32_state(&prng) % ilog2(s >> 12))); s = 1 + i915_prandom_u32_max_state((BIT(33 - 12)) - 1, &prng);
s = max(s & -ms, ms); /* Let the chunk size be a random power of 2 less than size */
ms = BIT(i915_prandom_u32_max_state(ilog2(s), &prng));
/* Round size down to the chunk size */
s &= -ms;
*chunk_size = ms; /* Convert from pages to bytes */
*size = s; *chunk_size = (u64)ms << 12;
*size = (u64)s << 12;
} }
static int igt_buddy_alloc_smoke(void *arg) static int igt_buddy_alloc_smoke(void *arg)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment