| 1 |
.\" Copyright 2021, Suren Baghdasaryan <[email protected]> |
| 2 |
.\" Copyright 2021, Minchan Kim <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.\" Commit ecb8ac8b1f146915aa6b96449b66dd48984caacc |
| 8 |
.\" |
| 9 |
.TH process_madvise 2 2026-02-22 "Linux man-pages 6.18" |
| 10 |
.SH NAME |
| 11 |
process_madvise \- give advice about use of memory to a process |
| 12 |
.SH LIBRARY |
| 13 |
Standard C library |
| 14 |
.RI ( libc ,\~ \-lc ) |
| 15 |
.SH SYNOPSIS |
| 16 |
.nf |
| 17 |
.B #include <sys/mman.h> |
| 18 |
.P |
| 19 |
.BI "ssize_t process_madvise(size_t " n ; |
| 20 |
.BI " int " pidfd ", const struct iovec " iovec [ n ], |
| 21 |
.BI " size_t " n ", int " advice \ |
| 22 |
", unsigned int " flags ); |
| 23 |
.fi |
| 24 |
.SH DESCRIPTION |
| 25 |
The |
| 26 |
.BR process_madvise () |
| 27 |
system call is used to give advice or directions to the kernel about the |
| 28 |
address ranges of another process or of the calling process. |
| 29 |
It provides the advice for the address ranges described by |
| 30 |
.I iovec |
| 31 |
and |
| 32 |
.IR n . |
| 33 |
The goal of such advice is to improve system or application performance. |
| 34 |
.P |
| 35 |
The |
| 36 |
.I pidfd |
| 37 |
argument is a PID file descriptor (see |
| 38 |
.BR pidfd_open (2)) |
| 39 |
that specifies the process to which the advice is to be applied. |
| 40 |
.P |
| 41 |
The pointer |
| 42 |
.I iovec |
| 43 |
points to an array of |
| 44 |
.I iovec |
| 45 |
structures, described in |
| 46 |
.BR iovec (3type). |
| 47 |
.P |
| 48 |
.I n |
| 49 |
specifies the number of elements in the array of |
| 50 |
.I iovec |
| 51 |
structures. |
| 52 |
This value must be less than or equal to |
| 53 |
.B IOV_MAX |
| 54 |
(defined in |
| 55 |
.I <limits.h> |
| 56 |
or accessible via the call |
| 57 |
.IR sysconf(_SC_IOV_MAX) ). |
| 58 |
.P |
| 59 |
If manipulating another process, |
| 60 |
or before Linux 6.13, |
| 61 |
the |
| 62 |
.I advice |
| 63 |
argument is one of the following values: |
| 64 |
.TP |
| 65 |
.B MADV_COLD |
| 66 |
See |
| 67 |
.BR madvise (2). |
| 68 |
.TP |
| 69 |
.B MADV_COLLAPSE |
| 70 |
See |
| 71 |
.BR madvise (2). |
| 72 |
.TP |
| 73 |
.B MADV_PAGEOUT |
| 74 |
See |
| 75 |
.BR madvise (2). |
| 76 |
.TP |
| 77 |
.B MADV_WILLNEED |
| 78 |
See |
| 79 |
.BR madvise (2). |
| 80 |
.P |
| 81 |
Since Linux 6.13, |
| 82 |
when manipulating the calling process, |
| 83 |
any advice flag is permitted. |
| 84 |
.P |
| 85 |
The |
| 86 |
.I flags |
| 87 |
argument is reserved for future use; |
| 88 |
currently, |
| 89 |
this argument must be specified as 0. |
| 90 |
.P |
| 91 |
The |
| 92 |
.I n |
| 93 |
and |
| 94 |
.I iovec |
| 95 |
arguments are checked before applying any advice. |
| 96 |
If |
| 97 |
.I n |
| 98 |
is too big, or |
| 99 |
.I iovec |
| 100 |
is invalid, |
| 101 |
then an error will be returned immediately and no advice will be applied. |
| 102 |
.P |
| 103 |
The advice might be applied to only a part of |
| 104 |
.I iovec |
| 105 |
if one of its elements points to an invalid memory region in the |
| 106 |
remote process. |
| 107 |
No further elements will be processed beyond that point. |
| 108 |
(See the discussion regarding partial advice in RETURN VALUE.) |
| 109 |
.P |
| 110 |
.\" commit 96cfe2c0fd23ea7c2368d14f769d287e7ae1082e |
| 111 |
Since Linux 5.12, |
| 112 |
permission to apply advice to another process is governed by |
| 113 |
ptrace access mode |
| 114 |
.B PTRACE_MODE_READ_FSCREDS |
| 115 |
check (see |
| 116 |
.BR ptrace (2)); |
| 117 |
in addition, |
| 118 |
because of the performance implications of applying the advice, |
| 119 |
the caller must have the |
| 120 |
.B CAP_SYS_NICE |
| 121 |
capability |
| 122 |
(see |
| 123 |
.BR capabilities (7)). |
| 124 |
.SH RETURN VALUE |
| 125 |
On success, |
| 126 |
.BR process_madvise () |
| 127 |
returns the number of bytes advised. |
| 128 |
This return value may be less than the total number of requested bytes, |
| 129 |
if an error occurred after some |
| 130 |
.I iovec |
| 131 |
elements were already processed. |
| 132 |
The caller should check the return value to determine whether a partial |
| 133 |
advice occurred. |
| 134 |
.P |
| 135 |
On error, \-1 is returned and |
| 136 |
.I errno |
| 137 |
is set to indicate the error. |
| 138 |
.SH ERRORS |
| 139 |
.TP |
| 140 |
.B EBADF |
| 141 |
.I pidfd |
| 142 |
is not a valid PID file descriptor. |
| 143 |
.TP |
| 144 |
.B EFAULT |
| 145 |
The memory described by |
| 146 |
.I iovec |
| 147 |
is outside the accessible address space of the process referred to by |
| 148 |
.IR pidfd . |
| 149 |
.TP |
| 150 |
.B EINVAL |
| 151 |
.I flags |
| 152 |
is not 0. |
| 153 |
.TP |
| 154 |
.B EINVAL |
| 155 |
The sum of the |
| 156 |
.I iov_len |
| 157 |
values of |
| 158 |
.I iovec |
| 159 |
overflows a |
| 160 |
.I ssize_t |
| 161 |
value. |
| 162 |
.TP |
| 163 |
.B EINVAL |
| 164 |
.I n |
| 165 |
is too large. |
| 166 |
.TP |
| 167 |
.B ENOMEM |
| 168 |
Could not allocate memory for internal copies of the |
| 169 |
.I iovec |
| 170 |
structures. |
| 171 |
.TP |
| 172 |
.B EPERM |
| 173 |
The caller does not have permission to access the address space of the process |
| 174 |
.IR pidfd . |
| 175 |
.TP |
| 176 |
.B ESRCH |
| 177 |
The target process does not exist (i.e., it has terminated and been waited on). |
| 178 |
.P |
| 179 |
See |
| 180 |
.BR madvise (2) |
| 181 |
for |
| 182 |
.IR advice -specific |
| 183 |
errors. |
| 184 |
.SH STANDARDS |
| 185 |
Linux. |
| 186 |
.SH HISTORY |
| 187 |
Linux 5.10, |
| 188 |
.\" Linux commit ecb8ac8b1f146915aa6b96449b66dd48984caacc |
| 189 |
glibc 2.36. |
| 190 |
.\" glibc commit d19ee3473d68ca0e794f3a8b7677a0983ae1342e |
| 191 |
.P |
| 192 |
Support for this system call is optional, |
| 193 |
depending on the setting of the |
| 194 |
.B CONFIG_ADVISE_SYSCALLS |
| 195 |
configuration option. |
| 196 |
.P |
| 197 |
When this system call first appeared in Linux 5.10, |
| 198 |
permission to apply advice to another process was entirely governed by |
| 199 |
ptrace access mode |
| 200 |
.B PTRACE_MODE_ATTACH_FSCREDS |
| 201 |
check (see |
| 202 |
.BR ptrace (2)). |
| 203 |
This requirement was relaxed in Linux 5.12 so that the caller didn't require |
| 204 |
full control over the target process. |
| 205 |
.SH SEE ALSO |
| 206 |
.BR madvise (2), |
| 207 |
.BR pidfd_open (2), |
| 208 |
.BR process_vm_readv (2), |
| 209 |
.BR process_vm_write (2) |