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 callpkey_free()on any protection key that has been assigned to an address range bypkey_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,
-1is returned, anderrnois set to indicate the error. π
π₯ ERRORS π₯
EINVALβ pkey, flags, or access_rights is invalid.ENOSPC(frompkey_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 πΈ
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
- Copyright 2016, Intel Corp
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms