Re: [RFC PATCH 2/3] futex: Add basic infrastructure for local task local hash.
From: Peter Zijlstra
Date: Wed Oct 30 2024 - 17:08:44 EST
On Mon, Oct 28, 2024 at 01:02:34PM +0100, Thomas Gleixner wrote:
> That's what we did with the original series, but with this model it's
> daft. What we maybe could do there is:
Not sure what's daft -- a single JVM running on 400+ CPUs with 4
hashbuckets sounds awesome.
>
> private_hash()
> scoped_guard(rcu) {
> hash = rcu_dereference(current->signal->futex_hash);
So I really do think mm_struct is a better place for this than signal
struct -- CLONE_SIGHAND is not mandatory when CLONE_VM.
I've long forgotten which JVM used the naked CLONE_VM, but there is some
creative code out there.
And futexes fundamentally live in the memory address space.
> if (hash && rcuref_get(&hash->ref))
> return hash;
> }
>
> guard(spinlock_irq)(&task->sighand->siglock);
> hash = current->signal->futex_hash;
> if (hash && rcuref_get(&hash->ref))
> return hash;
> // Let alloc scale according to signal->nr_threads
mm->mm_users
> // alloc acquires a reference count
> ....
It might make sense to have a prctl() setting that inhibits the hash
allocation entirely, reverting back to the global hash tables.
> And on fork do the following:
>
> scoped_guard(spinlock_irq, &task->sighand->siglock) {
> hash = current->signal->futex_hash;
> if (!hash || hash_size_ok())
> return hash;
>
> // Drop the initial reference, which forces the last
> // user and subsequent new users into the respective
> // slow paths, where they get stuck on sighand lock.
> if (!rcuref_put(&hash->ref))
> return;
>
> // rcuref_put() dropped the last reference
> old_hash = realloc_hash(hash);
> hash = current->signal->futex_hash;
> }
> kfree_rcu(old_hash);
> return hash;
>
> A similar logic is required when putting the last reference
>
> futex_hash_put()
> {
> if (!rcuref_put(&hash->ref))
> return;
>
> scoped_guard(spinlock_irq, &task->sighand->siglock) {
> // Fork might have raced with this
> if (hash != current->signal->futex_hash)
> return;
> old_hash = realloc_hash(hash);
> }
> kfree_rcu(old_hash);
> }
I'm not sure having that rehash under siglock is a fine idea. It's
convenient, no doubt, but urgh, could get expensive.
Another scheme would be to have 2 concurrent hash-tables for a little
while.