Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm: Add a simple generator of random permutations

When testing, we want a random but yet reproducible order in which to
process elements. Here we create an array which is a random (using the
Tausworthe PRNG) permutation of the order in which to execute.

Note these are simple helpers intended to be merged upstream in lib/

v2: Tidier code by David Herrmann
v3: Add reminder that this code is intended to be temporary, with at
least the bulk of the prandom changes going to lib/

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
Cc: David Herrmann <dh.herrmann@gmail.com>
Reviewed-by: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/20161222083641.2691-6-chris@chris-wilson.co.uk

authored by

Chris Wilson and committed by
Daniel Vetter
a33d42dd cf4a7207

+71
+4
drivers/gpu/drm/Kconfig
··· 321 321 chipset. If M is selected the module will be called savage. 322 322 323 323 endif # DRM_LEGACY 324 + 325 + config DRM_LIB_RANDOM 326 + bool 327 + default n
+1
drivers/gpu/drm/Makefile
··· 18 18 drm_plane.o drm_color_mgmt.o drm_print.o \ 19 19 drm_dumb_buffers.o drm_mode_config.o 20 20 21 + drm-$(CONFIG_DRM_LIB_RANDOM) += lib/drm_random.o 21 22 drm-$(CONFIG_COMPAT) += drm_ioc32.o 22 23 drm-$(CONFIG_DRM_GEM_CMA_HELPER) += drm_gem_cma_helper.o 23 24 drm-$(CONFIG_PCI) += ati_pcigart.o
+41
drivers/gpu/drm/lib/drm_random.c
··· 1 + #include <linux/bitops.h> 2 + #include <linux/kernel.h> 3 + #include <linux/random.h> 4 + #include <linux/slab.h> 5 + #include <linux/types.h> 6 + 7 + #include "drm_random.h" 8 + 9 + static inline u32 drm_prandom_u32_max_state(u32 ep_ro, struct rnd_state *state) 10 + { 11 + return upper_32_bits((u64)prandom_u32_state(state) * ep_ro); 12 + } 13 + 14 + void drm_random_reorder(unsigned int *order, unsigned int count, 15 + struct rnd_state *state) 16 + { 17 + unsigned int i, j; 18 + 19 + for (i = 0; i < count; ++i) { 20 + BUILD_BUG_ON(sizeof(unsigned int) > sizeof(u32)); 21 + j = drm_prandom_u32_max_state(count, state); 22 + swap(order[i], order[j]); 23 + } 24 + } 25 + EXPORT_SYMBOL(drm_random_reorder); 26 + 27 + unsigned int *drm_random_order(unsigned int count, struct rnd_state *state) 28 + { 29 + unsigned int *order, i; 30 + 31 + order = kmalloc_array(count, sizeof(*order), GFP_TEMPORARY); 32 + if (!order) 33 + return order; 34 + 35 + for (i = 0; i < count; i++) 36 + order[i] = i; 37 + 38 + drm_random_reorder(order, count, state); 39 + return order; 40 + } 41 + EXPORT_SYMBOL(drm_random_order);
+25
drivers/gpu/drm/lib/drm_random.h
··· 1 + #ifndef __DRM_RANDOM_H__ 2 + #define __DRM_RANDOM_H__ 3 + 4 + /* This is a temporary home for a couple of utility functions that should 5 + * be transposed to lib/ at the earliest convenience. 6 + */ 7 + 8 + #include <linux/random.h> 9 + 10 + #define DRM_RND_STATE_INITIALIZER(seed__) ({ \ 11 + struct rnd_state state__; \ 12 + prandom_seed_state(&state__, (seed__)); \ 13 + state__; \ 14 + }) 15 + 16 + #define DRM_RND_STATE(name__, seed__) \ 17 + struct rnd_state name__ = DRM_RND_STATE_INITIALIZER(seed__) 18 + 19 + unsigned int *drm_random_order(unsigned int count, 20 + struct rnd_state *state); 21 + void drm_random_reorder(unsigned int *order, 22 + unsigned int count, 23 + struct rnd_state *state); 24 + 25 + #endif /* !__DRM_RANDOM_H__ */