futex_proxy_trylock_atomic(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… futex_proxy_trylock_atomic(9) πŸ’…

okay so bestie, welcome to the kernel API manual β€” this one lives in section 9 (the kernel developer zone, iykyk) and it's literally called futex_proxy_trylock_atomic. yes that's a mouthful. no we're not renaming it. it's iconic and we respect it 🫢


✨ NAME ✨

futex_proxy_trylock_atomic β€” Attempt an atomic lock for the top waiter

like... it tries to grab the lock for the top waiter, atomically. that's the whole vibe check. πŸŽ€


🌸 SYNOPSIS 🌸

int futex_proxy_trylock_atomic (u32 __user *pifutex , struct
futex_hash_bucket *hb1 , struct futex_hash_bucket *hb2 , union futex_key
*key1 , union futex_key *key2 , struct futex_pi_state **ps , struct
task_struct **exiting , int set_waiters );

look at all those parameters. main character energy fr fr. πŸ’…


πŸ¦‹ ARGUMENTS πŸ¦‹

  • pifutex β€” the user address of the to futex. yep, "the to futex" β€” that's the wording, we're keeping it, don't come for me πŸ™ˆ
  • hb1 β€” the from futex hash bucket, and it must be locked by the caller. not optional. it's giving "you had one job" πŸ’€
  • hb2 β€” the to futex hash bucket, which also must be locked by the caller. same energy.
  • key1 β€” the from futex key πŸ—οΈ
  • key2 β€” the to futex key πŸ—οΈ
  • ps β€” address to store the pi_state pointer. it's a pointer to a pointer, we love a double pointer moment ✨
  • exiting β€” Pointer to store the task pointer of the owner task which is in the middle of exiting. lowkey dramatic but okay πŸ‘‹
  • set_waiters β€” force setting the FUTEX_WAITERS bit (1) or not (0). binary queen. either you're setting it or you're not 🌈

πŸ’– DESCRIPTION πŸ’–

So the whole point: Try and get the lock on behalf of the top waiter if we can do it atomically. Wake the top waiter if we succeed. literally "I gotchu bestie, here's the lock, go off πŸ‘‘".

If the caller specified set_waiters, then it directs futex_lock_pi_atomic to force setting the FUTEX_WAITERS bit. just passing along the instruction, no cap. 🎯

And hb1 and hb2 must be held by the caller. like, both of them. held. by you. before you call this. πŸ”’

Now the important bit, pay attention: exiting is only set when the return value is -EBUSY. If so, this holds a refcount on the exiting task on return and the caller needs to drop it after waiting for the exit to complete. so if you see -EBUSY, don't just walk away β€” you're holding a refcount, and you gotta drop it once the exit finishes. it's giving responsibility πŸ’€


πŸ”₯ RETURN πŸ”₯

  • 0 β€” failed to acquire the lock atomically; 😭 no lock for you
  • >0 β€” acquired the lock, return value is vpid of the top_waiter πŸŽ‰ slay
  • <0 β€” error πŸ’€

🌟 SEE ALSO 🌟

Kernel file ./kernel/futex/requeue.c β€” plus all these besties:

go read them. iykyk 🧚


⚠️ friendly reminder bestie: hb1 and hb2 must be locked by the caller, and if you get -EBUSY back you are holding a refcount on the exiting task. drop it after waiting for the exit to complete or the vibes will be rancid. fr fr.

and that's the page!! brought to you by ./kernel/futex, dated October 2026 β€” mwah, ily, go lock something atomically πŸ’œβœ¨πŸŽ€

πŸ“¦ where this page comes from