From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
Generalize node_random and make it available to general bitmaps and
cpumasks users.
Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
---
include/linux/find.h | 2 ++
include/linux/nodemask.h | 16 +---------------
lib/find_bit.c | 17 ++++++++++++++++-
3 files changed, 19 insertions(+), 16 deletions(-)
diff --git a/include/linux/find.h b/include/linux/find.h
index 5a2c267ea7f9..98c61838002c 100644
--- a/include/linux/find.h
+++ b/include/linux/find.h
@@ -44,6 +44,8 @@ unsigned long _find_next_bit_le(const unsigned long *addr, unsigned
long size, unsigned long offset);
#endif
+unsigned long find_random_bit(const unsigned long *addr, unsigned long size);
+
#ifndef find_next_bit
/**
* find_next_bit - find the next set bit in a memory region
diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h
index f08ae71585fa..1cedc7132b76 100644
--- a/include/linux/nodemask.h
+++ b/include/linux/nodemask.h
@@ -492,21 +492,7 @@ static __always_inline int num_node_state(enum node_states state)
static __always_inline int node_random(const nodemask_t *maskp)
{
#if defined(CONFIG_NUMA) && (MAX_NUMNODES > 1)
- int w, bit;
-
- w = nodes_weight(*maskp);
- switch (w) {
- case 0:
- bit = NUMA_NO_NODE;
- break;
- case 1:
- bit = first_node(*maskp);
- break;
- default:
- bit = find_nth_bit(maskp->bits, MAX_NUMNODES, get_random_u32_below(w));
- break;
- }
- return bit;
+ return find_random_bit(maskp->bits, MAX_NUMNODES);
#else
return 0;
#endif
diff --git a/lib/find_bit.c b/lib/find_bit.c
index 06b6342aa3ae..2118ea23bed8 100644
--- a/lib/find_bit.c
+++ b/lib/find_bit.c
@@ -18,6 +18,7 @@
#include <linux/math.h>
#include <linux/minmax.h>
#include <linux/swab.h>
+#include <linux/random.h>
/*
* Common helper for find_bit() function family
@@ -287,7 +288,21 @@ unsigned long _find_next_bit_le(const unsigned long *addr,
return FIND_NEXT_BIT(addr[idx], swab, size, offset);
}
EXPORT_SYMBOL(_find_next_bit_le);
-
#endif
+unsigned long find_random_bit(const unsigned long *addr, unsigned long size)
+{
+ int w = bitmap_weight(addr, size);
+
+ switch (w) {
+ case 0:
+ return size;
+ case 1:
+ return find_first_bit(addr, size);
+ default:
+ return find_nth_bit(addr, size, get_random_u32_below(w));
+ }
+}
+EXPORT_SYMBOL(find_random_bit);
+
#endif /* __BIG_ENDIAN */
--
2.43.0
On Wed, 4 Jun 2025 17:21:21 -0400 Yury Norov <yury.norov@gmail.com> wrote:
> Generalize node_random and make it available to general bitmaps and
> cpumasks users.
Seems sensible.
> --- a/lib/find_bit.c
> +++ b/lib/find_bit.c
>
> +unsigned long find_random_bit(const unsigned long *addr, unsigned long size)
> +{
> + int w = bitmap_weight(addr, size);
> +
> + switch (w) {
> + case 0:
> + return size;
> + case 1:
> + return find_first_bit(addr, size);
Is the `1' special case useful? The `default' case should still work OK?
> + default:
> + return find_nth_bit(addr, size, get_random_u32_below(w));
> + }
> +}
> +EXPORT_SYMBOL(find_random_bit);
Some kerneldoc, please?
Of course, the hard-coding of get_random_u32_below() might be
unsuitable for some future potential callers but we can deal with that
if it ever occurs.
On Wed, Jun 04, 2025 at 02:34:42PM -0700, Andrew Morton wrote:
> On Wed, 4 Jun 2025 17:21:21 -0400 Yury Norov <yury.norov@gmail.com> wrote:
>
> > Generalize node_random and make it available to general bitmaps and
> > cpumasks users.
>
> Seems sensible.
>
> > --- a/lib/find_bit.c
> > +++ b/lib/find_bit.c
> >
> > +unsigned long find_random_bit(const unsigned long *addr, unsigned long size)
> > +{
> > + int w = bitmap_weight(addr, size);
> > +
> > + switch (w) {
> > + case 0:
> > + return size;
> > + case 1:
> > + return find_first_bit(addr, size);
>
> Is the `1' special case useful? The `default' case should still work OK?
find_first_bit() is faster that find_nth_bit(), so this is a
performance optimization. See 3e061d924fe9c7b4 ("lib/nodemask: optimize
node_random for nodemask with single NUMA node").
> > + default:
> > + return find_nth_bit(addr, size, get_random_u32_below(w));
> > + }
> > +}
> > +EXPORT_SYMBOL(find_random_bit);
>
> Some kerneldoc, please?
Indeed, will send v2.
> Of course, the hard-coding of get_random_u32_below() might be
> unsuitable for some future potential callers but we can deal with that
> if it ever occurs.
Can you please elaborate?
Thanks,
Yury
On Wed, 4 Jun 2025 17:46:59 -0400 Yury Norov <yury.norov@gmail.com> wrote: > > Of course, the hard-coding of get_random_u32_below() might be > > unsuitable for some future potential callers but we can deal with that > > if it ever occurs. > > Can you please elaborate? Some callers might want to use a different random number generator. Seems unlikely, I know.
© 2016 - 2025 Red Hat, Inc.