Re: [PATCH v8 13/15] futex: Resize local futex hash table based on number of threads.

From: Sebastian Andrzej Siewior
Date: Wed Feb 05 2025 - 03:05:42 EST


On 2025-02-04 11:21:46 [+0100], Peter Zijlstra wrote:
> On Mon, Feb 03, 2025 at 02:59:33PM +0100, Sebastian Andrzej Siewior wrote:
> > Automatically size the local hash based on the number of threads. The
> > logic tries to allocate between 16 and futex_hashsize (the default for
> > the system wide hash bucket) and uses 4 * number-of-threads.
> > On CONFIG_BASE_SMALL configs the suggested size is always 2.
>
> > + scoped_guard(rcu) {
> > + threads = get_nr_threads(current);
>
> How about something like:
>
> threads = min(get_nr_threads(), num_online_cpus())
>
> ?
makes sense.

> > + hb_p = rcu_dereference(current->mm->futex_phash);
> > + if (hb_p)
> > + current_buckets = hb_p->hash_mask + 1;
> > + }
> > +
> > + if (IS_ENABLED(CONFIG_BASE_SMALL)) {
> > + buckets = 2;
> > +
>
> Or... you just disable the local thing entirely for BASE_SMALL and have
> it fall back to the global hash.

Okay, why not.

> > + } else {
> > + /*
> > + * The default allocation will remain within
> > + * 16 <= threads * 4 <= global hash size
> > + */
> > + buckets = roundup_pow_of_two(4 * threads);
> > + buckets = max(buckets, 16);
> > + buckets = min(buckets, futex_hashsize);
> > + }
> > + if (current_buckets >= buckets)
> > + return 0;
> > +
> > + return futex_hash_allocate(buckets);
> > }
> >
> > static int futex_hash_get_slots(void)

Sebastian