Re: [PATCH v4 06/11] futex: Allow to re-allocate the private hash bucket.

Thomas Gleixner
2024-12-11T15:23:00Z

On Tue, Dec 10 2024 at 23:27, Thomas Gleixner wrote:
> On Tue, Dec 03 2024 at 17:42, Sebastian Andrzej Siewior wrote:
> realloc_or_restore_hash()

swap_hash()
{
old_hash = current->mm->futex.hash;
new_hash = current->mm->futex.new_hash;

rehash(new_hash, old_hash);
rcu_assign_pointer(current->mm->futex.hash, new_hash);
rcu_free(old_hash);
}

> rehash(new_hash, old_hash)
> {
> // old_hash is marked dead, so new waiters cannot queue
> // themself and are stuck on the hash_mutex.
>
> for (i = 0; i < old_hash->size; i++) {
> hb1 = &old_hash->queues[i];
>
> // Protect the old bucket against unqueue(), as it does
> // not try to get a reference on the hash bucket. It
> // solely relies on q->lock_ptr.
> spin_lock(&hb1->lock);
>
> plist_for_each_entry_safe(q, tmp, hb1, list) {
> hb2 = hash_bucket(new_hash, &q->key);
> // Nesting is safe as this is a one time operation
> spin_lock_nested(&hb2->lock);
>
> plist_del(&q->list, &hb->chain);
>
> // Redirect the lock pointer to the new hash
> // bucket. See unqueue().
> q->lock_ptr = &hb2->lock;
>
> plist_add(&q->list, &hb->chain);
> }
> }
> }
>
> fork()
> {
> if (hash_needs_resize())
> hash_putref(mm->futex.hash);

futex_validate_hash();

futex_validate_hash()
{
guard(mutex)();
if (!hash_needs_resize(mm->futex.hash))
return;
if (mm->futex.new_hash && !hash_needs_resize(mm->futex.new_hash))
return;

new = alloc();
if (!new)
return;
current->mm.futex_new_hash = new;
if (!rcuput_ref(mm->futex.hash->ref))
return;

swap_hash();
}