RE: [PATCH v2 1/1] futex: Add FUTEX_SPIN operation
From: David Laight
Date: Fri May 24 2024 - 03:52:54 EST
From: André Almeida
> Sent: 23 May 2024 21:07
>
> Add a new mode for futex wait, the futex spin.
>
> Given the FUTEX2_SPIN flag, parse the futex value as the TID of the lock
> owner. Then, before going to the normal wait path, spins while the lock
> owner is running in a different CPU, to avoid the whole context switch
> operation and to quickly return to userspace. If the lock owner is not
> running, just sleep as the normal futex wait path.
>
> The user value is masked with FUTEX_TID_MASK, to allow some bits for
> future use.
>
> The check for the owner to be running or not is important to avoid
> spinning for something that won't be released quickly. Userspace is
> responsible on providing the proper TID, the kernel does a basic check.
The kernel needs to do something to stop a user-process spinning in-kernel
indefinitely.
...
> +static inline bool task_on_cpu(struct task_struct *p)
> +{
> +#ifdef CONFIG_SMP
> + return !!(p->on_cpu);
> +#else
> + return false;
> +#endif
> +}
I suspect that isn't going to work in a VM where the entire 'cpu'
can be sleeping.
This is similar to the (I don't think works properly) check
in the 'osq' (optimistic spin queue) used when waiting for
the thread spinning on a mutex/rwlock to change state.
IIRC that code also checks whether the current thread should
be pre-empted by a higher priority process.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)