Debian Codegraph @

upstream/debian-unstable/man2/pkey_alloc.2

raw
1 .\" Copyright 2016, Intel Corp.
2 .\" Copyright, the authors of the Linux man-pages project
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH pkey_alloc 2 2026-02-08 "Linux man-pages 6.18"
7 .SH NAME
8 pkey_alloc, pkey_free \- allocate or free a protection key
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ,\~ \-lc )
12 .SH SYNOPSIS
13 .nf
14 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
15 .B #include <sys/mman.h>
16 .P
17 .BI "int pkey_alloc(unsigned int " flags ", unsigned int " access_rights ");"
18 .BI "int pkey_free(int " pkey ");"
19 .fi
20 .SH DESCRIPTION
21 .BR pkey_alloc ()
22 allocates a protection key (pkey) and allows it to be passed to
23 .BR pkey_mprotect (2).
24 .P
25 The
26 .BR pkey_alloc ()
27 .I flags
28 is reserved for future use and currently must always be specified as 0.
29 .P
30 The
31 .BR pkey_alloc ()
32 .I access_rights
33 argument may contain zero or more disable operations:
34 .TP
35 .B PKEY_DISABLE_ACCESS
36 Disable all data access to memory covered by the returned protection key.
37 .TP
38 .B PKEY_DISABLE_WRITE
39 Disable write access to memory covered by the returned protection key.
40 .P
41 .BR pkey_free ()
42 frees a protection key and makes it available for later
43 allocations.
44 After a protection key has been freed, it may no longer be used
45 in any protection-key-related operations.
46 .P
47 An application should not call
48 .BR pkey_free ()
49 on any protection key which has been assigned to an address
50 range by
51 .BR pkey_mprotect (2)
52 and which is still in use.
53 The behavior in this case is undefined and may result in an error.
54 .SH RETURN VALUE
55 On success,
56 .BR pkey_alloc ()
57 returns a positive protection key value.
58 On success,
59 .BR pkey_free ()
60 returns zero.
61 On error, \-1 is returned, and
62 .I errno
63 is set to indicate the error.
64 .SH ERRORS
65 .TP
66 .B EINVAL
67 .IR pkey ,
68 .IR flags ,
69 or
70 .I access_rights
71 is invalid.
72 .TP
73 .B ENOSPC
74 .RB ( pkey_alloc ())
75 All protection keys available for the current process have
76 been allocated.
77 The number of keys available is architecture-specific and
78 implementation-specific and may be reduced by kernel-internal use
79 of certain keys.
80 There are currently 15 keys available to user programs on x86.
81 .IP
82 This error will also be returned if the processor or operating system
83 does not support protection keys.
84 Applications should always be prepared to handle this error, since
85 factors outside of the application's control can reduce the number
86 of available pkeys.
87 .SH STANDARDS
88 Linux.
89 .SH HISTORY
90 Linux 4.9,
91 glibc 2.27.
92 .SH NOTES
93 .BR pkey_alloc ()
94 is always safe to call regardless of whether or not the operating system
95 supports protection keys.
96 It can be used in lieu of any other mechanism for detecting pkey support
97 and will simply fail with the error
98 .B ENOSPC
99 if the operating system has no pkey support.
100 .P
101 The kernel guarantees that the contents of the hardware rights
102 register (PKRU) will be preserved only for allocated protection
103 keys.
104 Any time a key is unallocated (either before the first call
105 returning that key from
106 .BR pkey_alloc ()
107 or after it is freed via
108 .BR pkey_free ()),
109 the kernel may make arbitrary changes to the parts of the
110 rights register affecting access to that key.
111 .SH EXAMPLES
112 See
113 .BR pkeys (7).
114 .SH SEE ALSO
115 .BR pkey_mprotect (2),
116 .BR pkeys (7)