__hwspin_trylock(9)
π __hwspin_trylock β Kernel API Manual β¨
okay so this is from the Kernel API Manual, section 9, and it's literally the whole "can i grab this lock real quick" moment but make it hardware. let's get into it π«Ά
πΈ NAME πΈ
__hwspin_trylock β bestie, this attempts to lock one specific hwspinlock. one try. no waiting around. π
β¨ SYNOPSIS β¨
int __hwspin_trylock (struct hwspinlock *hwlock , int mode , unsigned
long *flags );
π ARGUMENTS π
hwlockβ the hwspinlock we're trying to lock, like the one specific lock you've got your eye on πmodeβ this controls whether local interrupts get disabled or not, your callflagsβ a pointer to where the caller's interrupt state gets saved (only if you asked for that, no pressure)
π¦ DESCRIPTION π¦
so this function attempts to lock an hwspinlock, and the way it does it is: it fails immediately if the hwspinlock is already taken. no queue, no vibes, no "just a sec" β instant nope π
β οΈ Caution, and I mean it: if themodeisHWLOCK_RAW, that means the user must protect the routine of getting the hardware lock withmutexorspinlock. why? because in some scenarios the user needs some time-consuming or sleepable operations under the hardware lock, so they need one sleepable lock (like amutex) to protect those operations. iykyk, don't skip this one π
if the mode is neither HWLOCK_IN_ATOMIC nor HWLOCK_RAW, then upon a successful return from this function, preemption (and possibly interrupts) is disabled, so the caller must not sleep, and is advised to release the hwspinlock as soon as possible. this is required in order to minimize remote cores polling on the hardware interconnect β the way those remote cores are just sitting there polling, we gotta be nice to them fr fr π₯Ί
the user decides whether local interrupts are disabled or not, and if yes, whether he wants their previous state to be saved. it's up to the user to choose the appropriate mode of operation β exactly the same way users should decide between spin_trylock, spin_trylock_irq and spin_trylock_irqsave. like, same energy, different lock π
and the headline, the icon, the main character of this whole paragraph: this function will never sleep. periodt. π
β RETURN β
0β slay, we successfully locked the hwspinlock π-EBUSYβ the hwspinlock was already taken, try again (or don't, that's the whole point of *try*lock) π
π₯ SEE ALSO π₯
Kernel file ./drivers/hwspinlock/hwspinlock_core.c β that's the source, go read it bestie π
the whole friend group, in order:
__hwspin_lock_timeout(9)__hwspin_unlock(9)hwspin_lock_bust(9)of_hwspin_lock_simple_xlate(9)of_hwspin_lock_get_id(9)of_hwspin_lock_get_id_byname(9)hwspin_lock_register(9)hwspin_lock_unregister(9)devm_hwspin_lock_unregister(9)devm_hwspin_lock_register(9)__hwspin_lock_request(9)hwspin_lock_request_specific(9)hwspin_lock_free(9)devm_hwspin_lock_free(9)devm_hwspin_lock_request_specific(9)
and that's the page!! π this little gem lives in ./drivers/hwspinlock, dated October 2026, and it goes by __hwspin_trylock(9). mwah, go lock something responsibly π§ββοΈπ
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms