__pt_guarded_by(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… __pt_guarded_by β€” the Kernel API Manual girlies are gonna eat this UP

Okay so bestie, we're diving into __pt_guarded_by(9) today, straight out of the Kernel API Manual, and honestly? This one is iconic. It's a struct member and globals attribute, and what it does is declare that the data your pointer points to is only accessible within the active context β€” like, the vibe check is enforced at compile time, no cap. πŸŽ€


✨ NAME ✨

__pt_guarded_by β€” it's the struct member and globals attribute that declares pointed-to data is only accessible within active context. Periodt. πŸ’…


🌸 SYNOPSIS 🌸

__pt_guarded_by (... );

πŸ¦‹ ARGUMENTS πŸ¦‹

  • ... β€” the context lock instance pointer(s), like, the lock(s) that are literally holding down this whole situation πŸ”₯

πŸ’– DESCRIPTION πŸ’–

So here's the tea, bestie: this attribute declares that the data pointed to by the struct member pointer (or the global pointer) is only accessible within the context entered by the given context lock(s). It's giving "you shall not pass" but make it concurrency-safe. 🧚

And the rules? They're lowkey specific, so listen up:

  • Read operations on that data require shared access to at least one of the context locks πŸ‘€
  • Write operations require exclusive access to all of the listed context locks πŸ”’

Which is honestly so fair of them. Shared reads, exclusive writes, everyone stays in their lane.

Single lock, one pointer? Simple. Slay:

struct some_state {
        spinlock_t lock;
        long *counter __pt_guarded_by(lock);
};

Two locks and you need both for writes? Also slay, also still just vibes:

struct some_state {
        spinlock_t lock1, lock2;
        long *counter __pt_guarded_by(lock1, lock2);
};

No cap, the second one is highkey the main character of the write-path rules. πŸ‘‘


🫢 SEE ALSO 🫢

Okay so there are so many friends in this extended universe, and they all deserve their moment. This whole crew lives in the kernel file ./include/linux/compiler-context-analysis.h β€” mwah, obsessed πŸ’œ


Real talk bestie: these are the context-analysis annotations, so if you're skipping one, you're basically leaving the group chat on read. Don't do that. 🫢

And that's the whole page, no notes. πŸ’œ This little beauty lives over at ./include/linux and it was dated October 2026 β€” literally stamped for the future, iykyk. Bye bestie, go annotate something safely! πŸŒˆβœ¨πŸŽ€

πŸ“¦ where this page comes from