1PKEY_ALLOC(2) Linux Programmer's Manual PKEY_ALLOC(2)
2
3
4
6 pkey_alloc, pkey_free - allocate or free a protection key
7
9 #include <sys/mman.h>
10
11 int pkey_alloc(unsigned long flags, unsigned long access_rights);
12 int pkey_free(int pkey);
13
15 pkey_alloc() allocates a protection key (pkey) and allows it to be
16 passed to pkey_mprotect(2).
17
18 The pkey_alloc() flags is reserved for future use and currently must
19 always be specified as 0.
20
21 The pkey_alloc() access_rights argument may contain zero or more dis‐
22 able operations:
23
24 PKEY_DISABLE_ACCESS
25 Disable all data access to memory covered by the returned pro‐
26 tection key.
27
28 PKEY_DISABLE_WRITE
29 Disable write access to memory covered by the returned protec‐
30 tion key.
31
32 pkey_free() frees a protection key and makes it available for later
33 allocations. After a protection key has been freed, it may no longer
34 be used in any protection-key-related operations.
35
36 An application should not call pkey_free() on any protection key which
37 has been assigned to an address range by pkey_mprotect(2) and which is
38 still in use. The behavior in this case is undefined and may result in
39 an error.
40
42 On success, pkey_alloc() returns a positive protection key value. On
43 success, pkey_free() returns zero. On error, -1 is returned, and errno
44 is set appropriately.
45
47 EINVAL pkey, flags, or access_rights is invalid.
48
49 ENOSPC (pkey_alloc()) All protection keys available for the current
50 process have been allocated. The number of keys available is
51 architecture-specific and implementation-specific and may be
52 reduced by kernel-internal use of certain keys. There are cur‐
53 rently 15 keys available to user programs on x86.
54
55 This error will also be returned if the processor or operating
56 system does not support protection keys. Applications should
57 always be prepared to handle this error, since factors outside
58 of the application's control can reduce the number of available
59 pkeys.
60
62 pkey_alloc() and pkey_free() were added to Linux in kernel 4.9; library
63 support was added in glibc 2.27.
64
66 The pkey_alloc() and pkey_free() system calls are Linux-specific.
67
69 pkey_alloc() is always safe to call regardless of whether or not the
70 operating system supports protection keys. It can be used in lieu of
71 any other mechanism for detecting pkey support and will simply fail
72 with the error ENOSPC if the operating system has no pkey support.
73
74 The kernel guarantees that the contents of the hardware rights register
75 (PKRU) will be preserved only for allocated protection keys. Any time
76 a key is unallocated (either before the first call returning that key
77 from pkey_alloc() or after it is freed via pkey_free()), the kernel may
78 make arbitrary changes to the parts of the rights register affecting
79 access to that key.
80
82 See pkeys(7).
83
85 pkey_mprotect(2), pkeys(7)
86
88 This page is part of release 4.15 of the Linux man-pages project. A
89 description of the project, information about reporting bugs, and the
90 latest version of this page, can be found at
91 https://www.kernel.org/doc/man-pages/.
92
93
94
95Linux 2018-02-02 PKEY_ALLOC(2)