public inbox for intel-gfx@lists.freedesktop.org
 help / color / mirror / Atom feed
From: Lukas Wunner <lukas@wunner.de>
To: Chris Wilson <chris@chris-wilson.co.uk>
Cc: intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v2 08/40] drm: Add a simple prime number generator
Date: Fri, 16 Dec 2016 10:31:17 +0100	[thread overview]
Message-ID: <20161216093117.GA11910@wunner.de> (raw)
In-Reply-To: <20161216074718.32500-9-chris@chris-wilson.co.uk>

On Fri, Dec 16, 2016 at 07:46:46AM +0000, Chris Wilson wrote:
> Prime numbers are interesting for testing components that use multiplies
> and divides, such as testing struct drm_mm alignment computations.
> 
> Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
> ---
>  drivers/gpu/drm/Kconfig                 |   4 +
>  drivers/gpu/drm/Makefile                |   1 +
>  drivers/gpu/drm/lib/drm_prime_numbers.c | 175 ++++++++++++++++++++++++++++++++
>  drivers/gpu/drm/lib/drm_prime_numbers.h |  10 ++
>  4 files changed, 190 insertions(+)
>  create mode 100644 drivers/gpu/drm/lib/drm_prime_numbers.c
>  create mode 100644 drivers/gpu/drm/lib/drm_prime_numbers.h

Hm, why not put this in lib/ ?  Don't see anything DRM-specific here
at first glance and this might be useful to others.  Or others might
come up with improvements and they'll be more likely to discover it
outside of DRM.

Same for the random permutations patch.

Thanks,

Lukas

> 
> diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig
> index 2e6ae95459e4..93895898d596 100644
> --- a/drivers/gpu/drm/Kconfig
> +++ b/drivers/gpu/drm/Kconfig
> @@ -53,6 +53,7 @@ config DRM_DEBUG_MM_SELFTEST
>  	depends on DRM
>  	depends on DEBUG_KERNEL
>  	select DRM_LIB_RANDOM
> +	select DRM_LIB_PRIMES
>  	default n
>  	help
>  	  This option provides a kernel module that can be used to test
> @@ -340,3 +341,6 @@ config DRM_LIB_RANDOM
>  	bool
>  	default n
>  
> +config DRM_LIB_PRIMES
> +	bool
> +	default n
> diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
> index 0fa16275fdae..bbd390fa8914 100644
> --- a/drivers/gpu/drm/Makefile
> +++ b/drivers/gpu/drm/Makefile
> @@ -19,6 +19,7 @@ drm-y       :=	drm_auth.o drm_bufs.o drm_cache.o \
>  		drm_dumb_buffers.o drm_mode_config.o
>  
>  drm-$(CONFIG_DRM_LIB_RANDOM) += lib/drm_random.o
> +obj-$(CONFIG_DRM_LIB_PRIMES) += lib/drm_prime_numbers.o
>  obj-$(CONFIG_DRM_DEBUG_MM_SELFTEST) += selftests/test-drm_mm.o
>  
>  drm-$(CONFIG_COMPAT) += drm_ioc32.o
> diff --git a/drivers/gpu/drm/lib/drm_prime_numbers.c b/drivers/gpu/drm/lib/drm_prime_numbers.c
> new file mode 100644
> index 000000000000..839563d9b787
> --- /dev/null
> +++ b/drivers/gpu/drm/lib/drm_prime_numbers.c
> @@ -0,0 +1,175 @@
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/slab.h>
> +
> +#include "drm_prime_numbers.h"
> +
> +static DEFINE_MUTEX(lock);
> +
> +static struct primes {
> +	struct rcu_head rcu;
> +	unsigned long last, sz;
> +	unsigned long primes[];
> +} __rcu *primes;
> +
> +static bool slow_is_prime_number(unsigned long x)
> +{
> +	unsigned long y = int_sqrt(x) + 1;
> +
> +	while (y > 1) {
> +		if ((x % y) == 0)
> +			break;
> +		y--;
> +	}
> +
> +	return y == 1;
> +}
> +
> +static unsigned long slow_next_prime_number(unsigned long x)
> +{
> +	for (;;) {
> +		if (slow_is_prime_number(++x))
> +			return x;
> +	}
> +}
> +
> +static unsigned long mark_multiples(unsigned long x,
> +				    unsigned long *p,
> +				    unsigned long start,
> +				    unsigned long end)
> +{
> +	unsigned long m;
> +
> +	m = 2 * x;
> +	if (m < start)
> +		m = (start / x + 1) * x;
> +
> +	while (m < end) {
> +		__clear_bit(m, p);
> +		m += x;
> +	}
> +
> +	return x;
> +}
> +
> +static struct primes *expand(unsigned long x)
> +{
> +	unsigned long sz, y, prev;
> +	struct primes *p, *new;
> +
> +	sz = x * x;
> +	if (sz < x)
> +		return NULL;
> +
> +	mutex_lock(&lock);
> +	p = rcu_dereference_protected(primes, lockdep_is_held(&lock));
> +	if (p && x < p->last)
> +		goto unlock;
> +
> +	sz = round_up(sz, BITS_PER_LONG);
> +	new = kmalloc(sizeof(*new) + sz / sizeof(long), GFP_KERNEL);
> +	if (!new) {
> +		p = NULL;
> +		goto unlock;
> +	}
> +
> +	/* Where memory permits, track the primes using the
> +	 * Sieve of Eratosthenes.
> +	 */
> +	if (p) {
> +		prev = p->sz;
> +		memcpy(new->primes, p->primes, prev / BITS_PER_LONG);
> +	} else {
> +		prev = 0;
> +	}
> +	memset(new->primes + prev / BITS_PER_LONG,
> +	       0xff, (sz - prev) / sizeof(long));
> +	for (y = 2UL; y < sz; y = find_next_bit(new->primes, sz, y + 1))
> +		new->last = mark_multiples(y, new->primes, prev, sz);
> +	new->sz = sz;
> +
> +	rcu_assign_pointer(primes, new);
> +	if (p)
> +		kfree_rcu(p, rcu);
> +	p = new;
> +
> +unlock:
> +	mutex_unlock(&lock);
> +	return p;
> +}
> +
> +unsigned long drm_next_prime_number(unsigned long x)
> +{
> +	struct primes *p;
> +
> +	if (x < 2)
> +		return 2;
> +
> +	rcu_read_lock();
> +	p = rcu_dereference(primes);
> +	if (!p || x >= p->last) {
> +		rcu_read_unlock();
> +
> +		p = expand(x);
> +		if (!p)
> +			return slow_next_prime_number(x);
> +
> +		rcu_read_lock();
> +	}
> +
> +	x = find_next_bit(p->primes, p->last, x + 1);
> +	rcu_read_unlock();
> +
> +	return x;
> +}
> +EXPORT_SYMBOL(drm_next_prime_number);
> +
> +bool drm_is_prime_number(unsigned long x)
> +{
> +	struct primes *p;
> +	bool result;
> +
> +	switch (x) {
> +	case 0:
> +		return false;
> +	case 1:
> +	case 2:
> +	case 3:
> +		return true;
> +	}
> +
> +	rcu_read_lock();
> +	p = rcu_dereference(primes);
> +	if (!p || x >= p->last) {
> +		rcu_read_unlock();
> +
> +		p = expand(x);
> +		if (!p)
> +			return slow_is_prime_number(x);
> +
> +		rcu_read_lock();
> +	}
> +
> +	result = test_bit(x, p->primes);
> +	rcu_read_unlock();
> +
> +	return result;
> +}
> +EXPORT_SYMBOL(drm_is_prime_number);
> +
> +static int __init drm_primes_init(void)
> +{
> +	return 0;
> +}
> +
> +static void __exit drm_primes_exit(void)
> +{
> +	if (primes)
> +		kfree_rcu(primes, rcu);
> +}
> +
> +module_init(drm_primes_init);
> +module_exit(drm_primes_exit);
> +
> +MODULE_AUTHOR("Intel Corporation");
> +MODULE_LICENSE("GPL");
> diff --git a/drivers/gpu/drm/lib/drm_prime_numbers.h b/drivers/gpu/drm/lib/drm_prime_numbers.h
> new file mode 100644
> index 000000000000..7bc58cf9a86c
> --- /dev/null
> +++ b/drivers/gpu/drm/lib/drm_prime_numbers.h
> @@ -0,0 +1,10 @@
> +#ifndef __DRM_PRIMES_H__
> +#define __DRM_PRIMES_H__
> +
> +bool drm_is_prime_number(unsigned long x);
> +unsigned long drm_next_prime_number(unsigned long x);
> +
> +#define drm_for_each_prime(prime, max) \
> +	for (prime = 1;	prime < (max); prime = drm_next_prime_number(prime))
> +
> +#endif /* __DRM_PRIMES_H__ */
> -- 
> 2.11.0
> 
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel
_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

  reply	other threads:[~2016-12-16  9:31 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-16  7:46 drm_mm range manager fixes, take 2 Chris Wilson
2016-12-16  7:46 ` [PATCH v2 01/40] drm/i915: Use the MRU stack search after evicting Chris Wilson
2016-12-16  9:03   ` Joonas Lahtinen
2016-12-16  7:46 ` [PATCH v2 02/40] drm/i915: Simplify i915_gtt_color_adjust() Chris Wilson
2016-12-16  7:46 ` [PATCH v2 03/40] drm: Add drm_mm_for_each_node_safe() Chris Wilson
2016-12-16  9:06   ` Joonas Lahtinen
2016-12-16 13:32     ` Daniel Vetter
2016-12-16  7:46 ` [PATCH v2 04/40] drm: Constify the drm_mm API Chris Wilson
2016-12-16 13:45   ` Daniel Vetter
2016-12-16  7:46 ` [PATCH v2 05/40] drm: Compile time enabling for asserts in drm_mm Chris Wilson
2016-12-16  7:46 ` [PATCH v2 06/40] drm: Add some kselftests for the DRM range manager (struct drm_mm) Chris Wilson
2016-12-16  7:46 ` [PATCH v2 07/40] drm: Add a simple generator of random permutations Chris Wilson
2016-12-16  9:38   ` Joonas Lahtinen
2016-12-16  7:46 ` [PATCH v2 08/40] drm: Add a simple prime number generator Chris Wilson
2016-12-16  9:31   ` Lukas Wunner [this message]
2016-12-16  9:43     ` Chris Wilson
2016-12-16 10:08       ` Lukas Wunner
2016-12-16 10:25         ` Chris Wilson
2016-12-16 13:23   ` [PATCH] lib: " Chris Wilson
2016-12-16 13:53     ` Chris Wilson
2016-12-16 14:02     ` [PATCH v3] " Chris Wilson
2016-12-16 18:47       ` kbuild test robot
2016-12-16  7:46 ` [PATCH v2 09/40] drm: kselftest for drm_mm_init() Chris Wilson
2016-12-16  9:41   ` Joonas Lahtinen
2016-12-16  7:46 ` [PATCH v2 10/40] drm: kselftest for drm_mm_debug() Chris Wilson
2016-12-16  9:44   ` Joonas Lahtinen
2016-12-16 10:01     ` Chris Wilson
2016-12-16  7:46 ` [PATCH v2 11/40] drm: kselftest for drm_mm_reserve_node() Chris Wilson
2016-12-16  7:46 ` [PATCH v2 12/40] drm: kselftest for drm_mm_insert_node() Chris Wilson
2016-12-16 14:02   ` Joonas Lahtinen
2016-12-16 14:59     ` Chris Wilson
2016-12-16  7:46 ` [PATCH v2 13/40] drm: kselftest for drm_mm_replace_node() Chris Wilson
2016-12-16  7:46 ` [PATCH v2 14/40] drm: kselftest for drm_mm_insert_node_in_range() Chris Wilson
2016-12-16 14:08   ` Joonas Lahtinen
2016-12-16  7:46 ` [PATCH v2 15/40] drm: kselftest for drm_mm and alignment Chris Wilson
2016-12-16  7:46 ` [PATCH v2 16/40] drm: kselftest for drm_mm and eviction Chris Wilson
2016-12-16  7:46 ` [PATCH v2 17/40] drm: kselftest for drm_mm and range restricted eviction Chris Wilson
2016-12-16  7:46 ` [PATCH v2 18/40] drm: kselftest for drm_mm and top-down allocation Chris Wilson
2016-12-16  7:46 ` [PATCH v2 19/40] drm: kselftest for drm_mm and color adjustment Chris Wilson
2016-12-16  7:46 ` [PATCH v2 20/40] drm: kselftest for drm_mm and color eviction Chris Wilson
2016-12-16 14:38   ` Joonas Lahtinen
2016-12-16 14:48     ` Chris Wilson
2016-12-16  7:46 ` [PATCH v2 21/40] drm: kselftest for drm_mm and restricted " Chris Wilson
2016-12-16 14:41   ` Joonas Lahtinen
2016-12-16  7:47 ` [PATCH v2 22/40] drm/i915: Build DRM range manager selftests for CI Chris Wilson
2016-12-16  7:47 ` [PATCH v2 23/40] drm: Promote drm_mm alignment to u64 Chris Wilson
2016-12-16  7:47 ` [PATCH v2 24/40] drm: Fix kerneldoc for drm_mm_scan_remove_block() Chris Wilson
2016-12-16 10:09   ` Joonas Lahtinen
2016-12-16  7:47 ` [PATCH v2 25/40] drm: Detect overflow in drm_mm_reserve_node() Chris Wilson
2016-12-16 10:11   ` Joonas Lahtinen
2016-12-16  7:47 ` [PATCH v2 26/40] drm: Simplify drm_mm_clean() Chris Wilson
2016-12-16  7:47 ` [PATCH v2 27/40] drm: Add asserts to catch overflow in drm_mm_init() and drm_mm_init_scan() Chris Wilson
2016-12-16 13:10   ` Joonas Lahtinen
2016-12-16  7:47 ` [PATCH v2 28/40] drm: Extract struct drm_mm_scan from struct drm_mm Chris Wilson
2016-12-16  7:47 ` [PATCH v2 29/40] drm: Rename prev_node to hole in drm_mm_scan_add_block() Chris Wilson
2016-12-16 14:41   ` Joonas Lahtinen
2016-12-16  7:47 ` [PATCH v2 30/40] drm: Unconditionally do the range check " Chris Wilson
2016-12-16  7:47 ` [PATCH v2 31/40] drm: Fix application of color vs range restriction when scanning drm_mm Chris Wilson
2016-12-16  7:47 ` [PATCH v2 32/40] drm: Compute tight evictions for drm_mm_scan Chris Wilson
2016-12-16  7:47 ` [PATCH v2 33/40] drm: Optimise power-of-two alignments in drm_mm_scan_add_block() Chris Wilson
2016-12-16  7:47 ` [PATCH v2 34/40] drm: Simplify drm_mm scan-list manipulation Chris Wilson
2016-12-16 13:06   ` Joonas Lahtinen
2016-12-16  7:47 ` [PATCH v2 35/40] drm: Apply tight eviction scanning to color_adjust Chris Wilson
2016-12-16 14:14   ` Joonas Lahtinen
2016-12-16 14:24     ` Chris Wilson
2016-12-16  7:47 ` [PATCH v2 36/40] drm: Wrap drm_mm_node.hole_follows Chris Wilson
2016-12-16 13:04   ` Joonas Lahtinen
2016-12-16 13:31     ` Chris Wilson
2016-12-16 13:41       ` Joonas Lahtinen
2016-12-16  7:47 ` [PATCH v2 37/40] drm: Apply range restriction after color adjustment when allocation Chris Wilson
2016-12-16 12:57   ` Joonas Lahtinen
2016-12-16  7:47 ` [PATCH v2 38/40] drm: Use drm_mm_insert_node_in_range_generic() for everyone Chris Wilson
2016-12-16 13:01   ` Joonas Lahtinen
2016-12-16  7:47 ` [PATCH v2 39/40] drm: Improve drm_mm search (and fix topdown allocation) with rbtrees Chris Wilson
2016-12-16 13:46   ` Joonas Lahtinen
2016-12-16 13:56     ` Chris Wilson
2016-12-16  7:47 ` [PATCH v2 40/40] drm: kselftest for drm_mm and bottom-up allocation Chris Wilson
2016-12-16  9:15 ` ✓ Fi.CI.BAT: success for series starting with [v2,01/40] drm/i915: Use the MRU stack search after evicting Patchwork
2016-12-16 16:31 ` ✗ Fi.CI.BAT: failure for series starting with [v2,01/40] drm/i915: Use the MRU stack search after evicting (rev3) Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161216093117.GA11910@wunner.de \
    --to=lukas@wunner.de \
    --cc=chris@chris-wilson.co.uk \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox