pkey_alloc(2)

πŸ’– pkey_alloc(2) β€” bestie, let's talk protection keys

okay so this is a System Calls Manual page, section 2, and it's all about pkey_alloc and pkey_free. Buckle up, we're doing memory protection keys today πŸ¦‹


✨ NAME ✨

pkey_alloc, pkey_free β€” allocate or free a protection key

πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc)

πŸŽ€ SYNOPSIS πŸŽ€

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <sys/mman.h>

int pkey_alloc(unsigned int flags, unsigned int access_rights);
int pkey_free(int pkey);

πŸ’… DESCRIPTION πŸ’…

So pkey_alloc() allocates a protection key (we call it a pkey, iykyk) and lets you hand it over to pkey_mprotect(2). That's the whole point. πŸ‘‘

The pkey_alloc() flags argument? Yeah, it's reserved for future use, so right now you literally must always pass it as 0. No cap. Don't try to be cute with it.

The pkey_alloc() access_rights argument can hold zero or more disable operations, and here's the menu:

  • PKEY_DISABLE_ACCESS β€” disables all data access to memory covered by the returned protection key. All of it. Gone. πŸ™ˆ
  • PKEY_DISABLE_WRITE β€” disables write access to memory covered by the returned protection key.

Then pkey_free() frees a protection key and makes it available again for later allocations. The way that a freed key is just... recyclable ✨. But heads up: after a protection key has been freed, it may no longer be used in any protection-key-related operations. Periodt.

🚨 bestie listen: an application should not call pkey_free() on any protection key that has been assigned to an address range by pkey_mprotect(2) and is still in use. The behavior in that case is undefined and may result in an error. That's not a vibe. That's chaos. πŸ’€

🌈 RETURN VALUE 🌈

  • On success, pkey_alloc() returns a positive protection key value. Slay.
  • On success, pkey_free() returns zero.
  • On error, -1 is returned, and errno is set to indicate the error. 😭

πŸ”₯ ERRORS πŸ”₯

  • EINVAL β€” pkey, flags, or access_rights is invalid.
  • ENOSPC (from pkey_alloc()) β€” all protection keys available for the current process have been allocated. The number of keys available is architecture-specific and implementation-specific, and may be reduced by kernel-internal use of certain keys. There are currently 15 keys available to user programs on x86.

This error will also be returned if the processor or operating system does not support protection keys. Applications should always be prepared to handle this error, since factors outside of the application's control can reduce the number of available pkeys. Fr fr, always handle it. 🫢

⭐ STANDARDS ⭐

Linux.

πŸ•°οΈ HISTORY πŸ•°οΈ

Linux 4.9, glibc 2.27. Vintage-ish but iconic.

πŸ‘‘ NOTES πŸ‘‘

pkey_alloc() is always safe to call regardless of whether or not the operating system supports protection keys. You can use it in lieu of any other mechanism for detecting pkey support β€” it will simply fail with the error ENOSPC if the operating system has no pkey support. Built-in vibe check, honestly. πŸ’–

The kernel guarantees that the contents of the hardware rights register (PKRU) will be preserved only for allocated protection keys. Any time a key is unallocated β€” either before the first call returning that key from pkey_alloc() or after it is freed via pkey_free() β€” the kernel may make arbitrary changes to the parts of the rights register affecting access to that key. So like... don't go reading those bits, bestie. The kernel said what it said. 🧚

πŸ“ EXAMPLES πŸ“

See pkeys(7).

🌸 SEE ALSO 🌸

pkey_mprotect(2), pkeys(7)


okay that's the whole page, you ate that πŸ’… this was Linux man-pages 6.20, dated 2026-02-08 β€” go protect some memory, bestie, and remember: free responsibly πŸ¦‹πŸ’œ

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms