| 1 |
.\" Copyright 2001, David Gómez <[email protected]> |
| 2 |
.\" Copyright, the authors of the Linux man-pages project |
| 3 |
.\" |
| 4 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 5 |
.\" |
| 6 |
.TH madvise 2 2026-02-08 "Linux man-pages 6.18" |
| 7 |
.SH NAME |
| 8 |
madvise \- give advice about use of memory |
| 9 |
.SH LIBRARY |
| 10 |
Standard C library |
| 11 |
.RI ( libc ,\~ \-lc ) |
| 12 |
.SH SYNOPSIS |
| 13 |
.nf |
| 14 |
.B #include <sys/mman.h> |
| 15 |
.P |
| 16 |
.BR "int madvise(" "size_t size;" |
| 17 |
.BI " void " addr [ size "], size_t " size ", int " advice ); |
| 18 |
.fi |
| 19 |
.P |
| 20 |
.RS -4 |
| 21 |
Feature Test Macro Requirements for glibc (see |
| 22 |
.BR feature_test_macros (7)): |
| 23 |
.RE |
| 24 |
.P |
| 25 |
.BR madvise (): |
| 26 |
.nf |
| 27 |
Since glibc 2.19: |
| 28 |
_DEFAULT_SOURCE |
| 29 |
Up to and including glibc 2.19: |
| 30 |
_BSD_SOURCE |
| 31 |
.fi |
| 32 |
.SH DESCRIPTION |
| 33 |
The |
| 34 |
.BR madvise () |
| 35 |
system call is used to give advice or directions to the kernel |
| 36 |
about the address range beginning at address |
| 37 |
.I addr |
| 38 |
and with size |
| 39 |
.IR size . |
| 40 |
.BR madvise () |
| 41 |
only operates on whole pages, therefore |
| 42 |
.I addr |
| 43 |
must be page-aligned. |
| 44 |
The value of |
| 45 |
.I size |
| 46 |
is rounded up to a multiple of page size. |
| 47 |
In most cases, |
| 48 |
the goal of such advice is to improve system or application performance. |
| 49 |
.P |
| 50 |
Initially, the system call supported a set of "conventional" |
| 51 |
.I advice |
| 52 |
values, which are also available on several other implementations. |
| 53 |
(Note, though, that |
| 54 |
.BR madvise () |
| 55 |
is not specified in POSIX.) |
| 56 |
Subsequently, a number of Linux-specific |
| 57 |
.I advice |
| 58 |
values have been added. |
| 59 |
.\" |
| 60 |
.\" ====================================================================== |
| 61 |
.\" |
| 62 |
.SS Conventional advice values |
| 63 |
The |
| 64 |
.I advice |
| 65 |
values listed below |
| 66 |
allow an application to tell the kernel how it expects to use |
| 67 |
some mapped or shared memory areas, so that the kernel can choose |
| 68 |
appropriate read-ahead and caching techniques. |
| 69 |
These |
| 70 |
.I advice |
| 71 |
values do not influence the semantics of the application |
| 72 |
(except in the case of |
| 73 |
.BR MADV_DONTNEED ), |
| 74 |
but may influence its performance. |
| 75 |
All of the |
| 76 |
.I advice |
| 77 |
values listed here have analogs in the POSIX-specified |
| 78 |
.BR posix_madvise (3) |
| 79 |
function, and the values have the same meanings, with the exception of |
| 80 |
.BR MADV_DONTNEED . |
| 81 |
.P |
| 82 |
The advice is indicated in the |
| 83 |
.I advice |
| 84 |
argument, which is one of the following: |
| 85 |
.TP |
| 86 |
.B MADV_NORMAL |
| 87 |
No special treatment. |
| 88 |
This is the default. |
| 89 |
.TP |
| 90 |
.B MADV_RANDOM |
| 91 |
Expect page references in random order. |
| 92 |
(Hence, read ahead may be less useful than normally.) |
| 93 |
.TP |
| 94 |
.B MADV_SEQUENTIAL |
| 95 |
Expect page references in sequential order. |
| 96 |
(Hence, pages in the given range can be aggressively read ahead, |
| 97 |
and may be freed soon after they are accessed.) |
| 98 |
.TP |
| 99 |
.B MADV_WILLNEED |
| 100 |
Expect access in the near future. |
| 101 |
(Hence, it might be a good idea to read some pages ahead.) |
| 102 |
.TP |
| 103 |
.B MADV_DONTNEED |
| 104 |
Do not expect access in the near future. |
| 105 |
(For the time being, the application is finished with the given range, |
| 106 |
so the kernel can free resources associated with it.) |
| 107 |
.IP |
| 108 |
After a successful |
| 109 |
.B MADV_DONTNEED |
| 110 |
operation, |
| 111 |
the semantics of memory access in the specified region are changed: |
| 112 |
subsequent accesses of pages in the range will succeed, but will result |
| 113 |
in either repopulating the memory contents from the |
| 114 |
up-to-date contents of the underlying mapped file |
| 115 |
(for shared file mappings, shared anonymous mappings, |
| 116 |
and shmem-based techniques such as System V shared memory segments) |
| 117 |
or zero-fill-on-demand pages for anonymous private mappings. |
| 118 |
.IP |
| 119 |
Note that, when applied to shared mappings, |
| 120 |
.B MADV_DONTNEED |
| 121 |
might not lead to immediate freeing of the pages in the range. |
| 122 |
The kernel is free to delay freeing the pages until an appropriate moment. |
| 123 |
The resident set size (RSS) of the calling process will be immediately |
| 124 |
reduced however. |
| 125 |
.IP |
| 126 |
.B MADV_DONTNEED |
| 127 |
cannot be applied to locked pages, or |
| 128 |
.B VM_PFNMAP |
| 129 |
pages. |
| 130 |
(Pages marked with the kernel-internal |
| 131 |
.B VM_PFNMAP |
| 132 |
.\" http://lwn.net/Articles/162860/ |
| 133 |
flag are special memory areas that are not managed |
| 134 |
by the virtual memory subsystem. |
| 135 |
Such pages are typically created by device drivers that |
| 136 |
map the pages into user space.) |
| 137 |
.IP |
| 138 |
Support for Huge TLB pages was added in Linux 5.18. |
| 139 |
Addresses within a mapping backed by Huge TLB pages must be aligned |
| 140 |
to the underlying Huge TLB page size, |
| 141 |
and the range size is rounded up |
| 142 |
to a multiple of the underlying Huge TLB page size. |
| 143 |
.\" |
| 144 |
.\" ====================================================================== |
| 145 |
.\" |
| 146 |
.SS Linux-specific advice values |
| 147 |
The following Linux-specific |
| 148 |
.I advice |
| 149 |
values have no counterparts in the POSIX-specified |
| 150 |
.BR posix_madvise (3), |
| 151 |
and may or may not have counterparts in the |
| 152 |
.BR madvise () |
| 153 |
interface available on other implementations. |
| 154 |
Note that some of these operations change the semantics of memory accesses. |
| 155 |
.TP |
| 156 |
.BR MADV_REMOVE " (since Linux 2.6.16)" |
| 157 |
.\" commit f6b3ec238d12c8cc6cc71490c6e3127988460349 |
| 158 |
Free up a given range of pages |
| 159 |
and its associated backing store. |
| 160 |
This is equivalent to punching a hole in the corresponding |
| 161 |
range of the backing store (see |
| 162 |
.BR fallocate (2)). |
| 163 |
Subsequent accesses in the specified address range will see |
| 164 |
data with a value of zero. |
| 165 |
.\" Databases want to use this feature to drop a section of their |
| 166 |
.\" bufferpool (shared memory segments) - without writing back to |
| 167 |
.\" disk/swap space. This feature is also useful for supporting |
| 168 |
.\" hot-plug memory on UML. |
| 169 |
.IP |
| 170 |
The specified address range must be mapped shared and writable. |
| 171 |
This flag cannot be applied to locked pages, or |
| 172 |
.B VM_PFNMAP |
| 173 |
pages. |
| 174 |
.IP |
| 175 |
In the initial implementation, only |
| 176 |
.BR tmpfs (5) |
| 177 |
supported |
| 178 |
.BR MADV_REMOVE ; |
| 179 |
but since Linux 3.5, |
| 180 |
.\" commit 3f31d07571eeea18a7d34db9af21d2285b807a17 |
| 181 |
any filesystem which supports the |
| 182 |
.BR fallocate (2) |
| 183 |
.B FALLOC_FL_PUNCH_HOLE |
| 184 |
mode also supports |
| 185 |
.BR MADV_REMOVE . |
| 186 |
Filesystems which do not support |
| 187 |
.B MADV_REMOVE |
| 188 |
fail with the error |
| 189 |
.BR EOPNOTSUPP . |
| 190 |
.IP |
| 191 |
Support for the Huge TLB filesystem was added in Linux 4.3. |
| 192 |
.TP |
| 193 |
.BR MADV_DONTFORK " (since Linux 2.6.16)" |
| 194 |
.\" commit f822566165dd46ff5de9bf895cfa6c51f53bb0c4 |
| 195 |
.\" See http://lwn.net/Articles/171941/ |
| 196 |
Do not make the pages in this range available to the child after a |
| 197 |
.BR fork (2). |
| 198 |
This is useful to prevent copy-on-write semantics from changing |
| 199 |
the physical location of a page if the parent writes to it after a |
| 200 |
.BR fork (2). |
| 201 |
(Such page relocations cause problems for hardware that |
| 202 |
DMAs into the page.) |
| 203 |
.\" [PATCH] madvise MADV_DONTFORK/MADV_DOFORK |
| 204 |
.\" Currently, copy-on-write may change the physical address of |
| 205 |
.\" a page even if the user requested that the page is pinned in |
| 206 |
.\" memory (either by mlock or by get_user_pages). This happens |
| 207 |
.\" if the process forks meanwhile, and the parent writes to that |
| 208 |
.\" page. As a result, the page is orphaned: in case of |
| 209 |
.\" get_user_pages, the application will never see any data hardware |
| 210 |
.\" DMA's into this page after the COW. In case of mlock'd memory, |
| 211 |
.\" the parent is not getting the realtime/security benefits of mlock. |
| 212 |
.\" |
| 213 |
.\" In particular, this affects the Infiniband modules which do DMA from |
| 214 |
.\" and into user pages all the time. |
| 215 |
.\" |
| 216 |
.\" This patch adds madvise options to control whether memory range is |
| 217 |
.\" inherited across fork. Useful, e.g., for when hardware is doing DMA |
| 218 |
.\" from/into these pages. Could also be useful to an application |
| 219 |
.\" wanting to speed up its forks by cutting large areas out of |
| 220 |
.\" consideration. |
| 221 |
.\" |
| 222 |
.\" SEE ALSO: http://lwn.net/Articles/171941/ |
| 223 |
.\" "Tweaks to madvise() and posix_fadvise()", 14 Feb 2006 |
| 224 |
.TP |
| 225 |
.BR MADV_DOFORK " (since Linux 2.6.16)" |
| 226 |
Undo the effect of |
| 227 |
.BR MADV_DONTFORK , |
| 228 |
restoring the default behavior, whereby a mapping is inherited across |
| 229 |
.BR fork (2). |
| 230 |
.TP |
| 231 |
.BR MADV_HWPOISON " (since Linux 2.6.32)" |
| 232 |
.\" commit 9893e49d64a4874ea67849ee2cfbf3f3d6817573 |
| 233 |
Poison the pages in the range specified by |
| 234 |
.I addr |
| 235 |
and |
| 236 |
.I size |
| 237 |
and handle subsequent references to those pages |
| 238 |
like a hardware memory corruption. |
| 239 |
This operation is available only for privileged |
| 240 |
.RB ( CAP_SYS_ADMIN ) |
| 241 |
processes. |
| 242 |
This operation may result in the calling process receiving a |
| 243 |
.B SIGBUS |
| 244 |
and the page being unmapped. |
| 245 |
.IP |
| 246 |
This feature is intended for testing of memory error-handling code; |
| 247 |
it is available only if the kernel was configured with |
| 248 |
.BR CONFIG_MEMORY_FAILURE . |
| 249 |
.TP |
| 250 |
.BR MADV_MERGEABLE " (since Linux 2.6.32)" |
| 251 |
.\" commit f8af4da3b4c14e7267c4ffb952079af3912c51c5 |
| 252 |
Enable Kernel Samepage Merging (KSM) for the pages in the range specified by |
| 253 |
.I addr |
| 254 |
and |
| 255 |
.IR size . |
| 256 |
The kernel regularly scans those areas of user memory that have |
| 257 |
been marked as mergeable, |
| 258 |
looking for pages with identical content. |
| 259 |
These are replaced by a single write-protected page (which is automatically |
| 260 |
copied if a process later wants to update the content of the page). |
| 261 |
KSM merges only private anonymous pages (see |
| 262 |
.BR mmap (2)). |
| 263 |
.IP |
| 264 |
The KSM feature is intended for applications that generate many |
| 265 |
instances of the same data (e.g., virtualization systems such as KVM). |
| 266 |
It can consume a lot of processing power; |
| 267 |
use with care. |
| 268 |
See the Linux kernel source file |
| 269 |
.I Documentation/admin\-guide/mm/ksm.rst |
| 270 |
for more details. |
| 271 |
.IP |
| 272 |
The |
| 273 |
.B MADV_MERGEABLE |
| 274 |
and |
| 275 |
.B MADV_UNMERGEABLE |
| 276 |
operations are available only if the kernel was configured with |
| 277 |
.BR CONFIG_KSM . |
| 278 |
.TP |
| 279 |
.BR MADV_UNMERGEABLE " (since Linux 2.6.32)" |
| 280 |
Undo the effect of an earlier |
| 281 |
.B MADV_MERGEABLE |
| 282 |
operation on the specified address range; |
| 283 |
KSM unmerges whatever pages it had merged in the address range specified by |
| 284 |
.I addr |
| 285 |
and |
| 286 |
.IR size . |
| 287 |
.TP |
| 288 |
.BR MADV_SOFT_OFFLINE " (since Linux 2.6.33)" |
| 289 |
.\" commit afcf938ee0aac4ef95b1a23bac704c6fbeb26de6 |
| 290 |
Soft offline the pages in the range specified by |
| 291 |
.I addr |
| 292 |
and |
| 293 |
.IR size . |
| 294 |
The memory of each page in the specified range is preserved |
| 295 |
(i.e., when next accessed, the same content will be visible, |
| 296 |
but in a new physical page frame), |
| 297 |
and the original page is offlined |
| 298 |
(i.e., no longer used, and taken out of normal memory management). |
| 299 |
The effect of the |
| 300 |
.B MADV_SOFT_OFFLINE |
| 301 |
operation is invisible to (i.e., does not change the semantics of) |
| 302 |
the calling process. |
| 303 |
.IP |
| 304 |
This feature is intended for testing of memory error-handling code; |
| 305 |
it is available only if the kernel was configured with |
| 306 |
.BR CONFIG_MEMORY_FAILURE . |
| 307 |
.TP |
| 308 |
.BR MADV_HUGEPAGE " (since Linux 2.6.38)" |
| 309 |
.\" commit 0af4e98b6b095c74588af04872f83d333c958c32 |
| 310 |
.\" http://lwn.net/Articles/358904/ |
| 311 |
.\" https://lwn.net/Articles/423584/ |
| 312 |
Enable Transparent Huge Pages (THP) for pages in the range specified by |
| 313 |
.I addr |
| 314 |
and |
| 315 |
.IR size . |
| 316 |
The kernel will regularly scan the areas marked as huge page candidates |
| 317 |
to replace them with huge pages. |
| 318 |
The kernel will also allocate huge pages directly when the region is |
| 319 |
naturally aligned to the huge page size (see |
| 320 |
.BR posix_memalign (2)). |
| 321 |
.IP |
| 322 |
This feature is primarily aimed at applications that use large mappings of |
| 323 |
data and access large regions of that memory at a time (e.g., virtualization |
| 324 |
systems such as QEMU). |
| 325 |
It can very easily waste memory (e.g., a 2\ MB mapping that only ever accesses |
| 326 |
1 byte will result in 2\ MB of wired memory instead of one 4\ KB page). |
| 327 |
See the Linux kernel source file |
| 328 |
.I Documentation/admin\-guide/mm/transhuge.rst |
| 329 |
for more details. |
| 330 |
.IP |
| 331 |
Most common kernels configurations provide |
| 332 |
.BR MADV_HUGEPAGE -style |
| 333 |
behavior by default, and thus |
| 334 |
.B MADV_HUGEPAGE |
| 335 |
is normally not necessary. |
| 336 |
It is mostly intended for embedded systems, where |
| 337 |
.BR MADV_HUGEPAGE -style |
| 338 |
behavior may not be enabled by default in the kernel. |
| 339 |
On such systems, |
| 340 |
this flag can be used in order to selectively enable THP. |
| 341 |
Whenever |
| 342 |
.B MADV_HUGEPAGE |
| 343 |
is used, it should always be in regions of memory with |
| 344 |
an access pattern that the developer knows in advance won't risk |
| 345 |
to increase the memory footprint of the application when transparent |
| 346 |
hugepages are enabled. |
| 347 |
.IP |
| 348 |
.\" commit 99cb0dbd47a15d395bf3faa78dc122bc5efe3fc0 |
| 349 |
Since Linux 5.4, |
| 350 |
automatic scan of eligible areas and replacement by huge pages works with |
| 351 |
private anonymous pages (see |
| 352 |
.BR mmap (2)), |
| 353 |
shmem pages, |
| 354 |
and file-backed pages. |
| 355 |
For all memory types, |
| 356 |
memory may only be replaced by huge pages on hugepage-aligned boundaries. |
| 357 |
For file-mapped memory |
| 358 |
\[em]including tmpfs (see |
| 359 |
.BR tmpfs (2))\[em] |
| 360 |
the mapping must also be naturally hugepage-aligned within the file. |
| 361 |
Additionally, |
| 362 |
for file-backed, |
| 363 |
non-tmpfs memory, |
| 364 |
the file must not be open for write and the mapping must be executable. |
| 365 |
.IP |
| 366 |
The VMA must not be marked |
| 367 |
.BR VM_NOHUGEPAGE , |
| 368 |
.BR VM_HUGETLB , |
| 369 |
.BR VM_IO , |
| 370 |
.BR VM_DONTEXPAND , |
| 371 |
.BR VM_MIXEDMAP , |
| 372 |
or |
| 373 |
.BR VM_PFNMAP , |
| 374 |
nor can it be stack memory or backed by a DAX-enabled device |
| 375 |
(unless the DAX device is hot-plugged as System RAM). |
| 376 |
The process can have |
| 377 |
.B PR_SET_THP_DISABLE |
| 378 |
set only if |
| 379 |
.B PR_THP_DISABLE_EXCEPT_ADVISED |
| 380 |
is set (see |
| 381 |
.BR prctl (2)). |
| 382 |
.IP |
| 383 |
The |
| 384 |
.BR MADV_HUGEPAGE , |
| 385 |
.BR MADV_NOHUGEPAGE , |
| 386 |
and |
| 387 |
.B MADV_COLLAPSE |
| 388 |
operations are available only if the kernel was configured with |
| 389 |
.B CONFIG_TRANSPARENT_HUGEPAGE |
| 390 |
and file/shmem memory is only supported if the kernel was configured with |
| 391 |
.BR CONFIG_READ_ONLY_THP_FOR_FS . |
| 392 |
.TP |
| 393 |
.BR MADV_NOHUGEPAGE " (since Linux 2.6.38)" |
| 394 |
Ensures that memory in the address range specified by |
| 395 |
.I addr |
| 396 |
and |
| 397 |
.I size |
| 398 |
will not be backed by transparent hugepages. |
| 399 |
.TP |
| 400 |
.BR MADV_COLLAPSE " (since Linux 6.1)" |
| 401 |
.\" commit 7d8faaf155454f8798ec56404faca29a82689c77 |
| 402 |
.\" commit 34488399fa08faaf664743fa54b271eb6f9e1321 |
| 403 |
Perform a best-effort synchronous collapse of |
| 404 |
the native pages mapped by the memory range |
| 405 |
into Transparent Huge Pages (THPs). |
| 406 |
.B MADV_COLLAPSE |
| 407 |
operates on the current state of memory of the calling process and |
| 408 |
makes no persistent changes or guarantees on how pages will be mapped, |
| 409 |
constructed, |
| 410 |
or faulted in the future. |
| 411 |
.IP |
| 412 |
.B MADV_COLLAPSE |
| 413 |
supports private anonymous pages (see |
| 414 |
.BR mmap (2)), |
| 415 |
shmem pages, |
| 416 |
and file-backed pages. |
| 417 |
See |
| 418 |
.B MADV_HUGEPAGE |
| 419 |
for general information on memory requirements for THP. |
| 420 |
If the range provided spans multiple VMAs, |
| 421 |
the semantics of the collapse over each VMA is independent from the others. |
| 422 |
If collapse of a given huge page-aligned/sized region fails, |
| 423 |
the operation may continue to attempt collapsing |
| 424 |
the remainder of the specified memory. |
| 425 |
.B MADV_COLLAPSE |
| 426 |
will automatically clamp the provided range to be hugepage-aligned. |
| 427 |
.IP |
| 428 |
All non-resident pages covered by the range |
| 429 |
will first be swapped/faulted-in, |
| 430 |
before being copied onto a freshly allocated hugepage. |
| 431 |
If the native pages compose the same PTE-mapped hugepage, |
| 432 |
and are suitably aligned, |
| 433 |
allocation of a new hugepage may be elided and |
| 434 |
collapse may happen in-place. |
| 435 |
Unmapped pages will have their data directly initialized to 0 |
| 436 |
in the new hugepage. |
| 437 |
However, |
| 438 |
for every eligible hugepage-aligned/sized region to be collapsed, |
| 439 |
at least one page must currently be backed by physical memory. |
| 440 |
.IP |
| 441 |
.B MADV_COLLAPSE |
| 442 |
is independent of any sysfs |
| 443 |
(see |
| 444 |
.BR sysfs (5)) |
| 445 |
setting under |
| 446 |
.IR /sys/kernel/mm/transparent_hugepage , |
| 447 |
both in terms of determining THP eligibility, |
| 448 |
and allocation semantics. |
| 449 |
See Linux kernel source file |
| 450 |
.I Documentation/admin\-guide/mm/transhuge.rst |
| 451 |
for more information. |
| 452 |
.B MADV_COLLAPSE |
| 453 |
also ignores |
| 454 |
.B huge= |
| 455 |
tmpfs mount when operating on tmpfs files. |
| 456 |
Allocation for the new hugepage may enter direct reclaim and/or compaction, |
| 457 |
regardless of VMA flags |
| 458 |
(though |
| 459 |
.B VM_NOHUGEPAGE |
| 460 |
is still respected). |
| 461 |
.IP |
| 462 |
When the system has multiple NUMA nodes, |
| 463 |
the hugepage will be allocated from |
| 464 |
the node providing the most native pages. |
| 465 |
.IP |
| 466 |
If all hugepage-sized/aligned regions covered by the provided range were |
| 467 |
either successfully collapsed, |
| 468 |
or were already PMD-mapped THPs, |
| 469 |
this operation will be deemed successful. |
| 470 |
Note that this doesn't guarantee anything about |
| 471 |
other possible mappings of the memory. |
| 472 |
In the event multiple hugepage-aligned/sized areas fail to collapse, |
| 473 |
only the most-recently\[en]failed code will be set in |
| 474 |
.IR errno . |
| 475 |
.TP |
| 476 |
.BR MADV_DONTDUMP " (since Linux 3.4)" |
| 477 |
.\" commit 909af768e88867016f427264ae39d27a57b6a8ed |
| 478 |
.\" commit accb61fe7bb0f5c2a4102239e4981650f9048519 |
| 479 |
Exclude from a core dump those pages in the range specified by |
| 480 |
.I addr |
| 481 |
and |
| 482 |
.IR size . |
| 483 |
This is useful in applications that have large areas of memory |
| 484 |
that are known not to be useful in a core dump. |
| 485 |
The effect of |
| 486 |
.B MADV_DONTDUMP |
| 487 |
takes precedence over the bit mask that is set via the |
| 488 |
.IR /proc/ pid /coredump_filter |
| 489 |
file (see |
| 490 |
.BR core (5)). |
| 491 |
.TP |
| 492 |
.BR MADV_DODUMP " (since Linux 3.4)" |
| 493 |
Undo the effect of an earlier |
| 494 |
.BR MADV_DONTDUMP . |
| 495 |
.TP |
| 496 |
.BR MADV_FREE " (since Linux 4.5)" |
| 497 |
The application no longer requires the pages in the range specified by |
| 498 |
.I addr |
| 499 |
and |
| 500 |
.IR size . |
| 501 |
The kernel can thus free these pages, |
| 502 |
but the freeing could be delayed until memory pressure occurs. |
| 503 |
For each of the pages that has been marked to be freed |
| 504 |
but has not yet been freed, |
| 505 |
the free operation will be canceled if the caller writes into the page. |
| 506 |
After a successful |
| 507 |
.B MADV_FREE |
| 508 |
operation, any stale data (i.e., dirty, unwritten pages) will be lost |
| 509 |
when the kernel frees the pages. |
| 510 |
However, subsequent writes to pages in the range will succeed |
| 511 |
and then kernel cannot free those dirtied pages, |
| 512 |
so that the caller can always see just written data. |
| 513 |
If there is no subsequent write, |
| 514 |
the kernel can free the pages at any time. |
| 515 |
Once pages in the range have been freed, the caller will |
| 516 |
see zero-fill-on-demand pages upon subsequent page references. |
| 517 |
.IP |
| 518 |
The |
| 519 |
.B MADV_FREE |
| 520 |
operation |
| 521 |
can be applied only to private anonymous pages (see |
| 522 |
.BR mmap (2)). |
| 523 |
Before Linux 4.12, |
| 524 |
.\" commit 93e06c7a645343d222c9a838834a51042eebbbf7 |
| 525 |
when freeing pages on a swapless system, |
| 526 |
the pages in the given range are freed instantly, |
| 527 |
regardless of memory pressure. |
| 528 |
.TP |
| 529 |
.BR MADV_WIPEONFORK " (since Linux 4.14)" |
| 530 |
.\" commit d2cd9ede6e193dd7d88b6d27399e96229a551b19 |
| 531 |
Present the child process with zero-filled memory in this range after a |
| 532 |
.BR fork (2). |
| 533 |
This is useful in forking servers in order to ensure |
| 534 |
that sensitive per-process data |
| 535 |
(for example, PRNG seeds, cryptographic secrets, and so on) |
| 536 |
is not handed to child processes. |
| 537 |
.IP |
| 538 |
The |
| 539 |
.B MADV_WIPEONFORK |
| 540 |
operation can be applied only to private anonymous pages (see |
| 541 |
.BR mmap (2)). |
| 542 |
.IP |
| 543 |
Within the child created by |
| 544 |
.BR fork (2), |
| 545 |
the |
| 546 |
.B MADV_WIPEONFORK |
| 547 |
setting remains in place on the specified address range. |
| 548 |
This setting is cleared during |
| 549 |
.BR execve (2). |
| 550 |
.TP |
| 551 |
.BR MADV_KEEPONFORK " (since Linux 4.14)" |
| 552 |
.\" commit d2cd9ede6e193dd7d88b6d27399e96229a551b19 |
| 553 |
Undo the effect of an earlier |
| 554 |
.BR MADV_WIPEONFORK . |
| 555 |
.TP |
| 556 |
.BR MADV_COLD " (since Linux 5.4)" |
| 557 |
.\" commit 9c276cc65a58faf98be8e56962745ec99ab87636 |
| 558 |
Deactivate a given range of pages. |
| 559 |
This will make the pages a more probable |
| 560 |
reclaim target should there be a memory pressure. |
| 561 |
This is a nondestructive operation. |
| 562 |
The advice might be ignored for some pages in the range when it is not |
| 563 |
applicable. |
| 564 |
.TP |
| 565 |
.BR MADV_PAGEOUT " (since Linux 5.4)" |
| 566 |
.\" commit 1a4e58cce84ee88129d5d49c064bd2852b481357 |
| 567 |
Reclaim a given range of pages. |
| 568 |
This is done to free up memory occupied by these pages. |
| 569 |
If a page is anonymous, it will be swapped out. |
| 570 |
If a page is file-backed and dirty, it will be written back to the backing |
| 571 |
storage. |
| 572 |
The advice might be ignored for some pages in the range when it is not |
| 573 |
applicable. |
| 574 |
.TP |
| 575 |
.BR MADV_POPULATE_READ " (since Linux 5.14)" |
| 576 |
"Populate (prefault) page tables readable, |
| 577 |
faulting in all pages in the range just as if manually reading from each page; |
| 578 |
however, |
| 579 |
avoid the actual memory access that would have been performed after handling |
| 580 |
the fault. |
| 581 |
.IP |
| 582 |
In contrast to |
| 583 |
.BR MAP_POPULATE , |
| 584 |
.B MADV_POPULATE_READ |
| 585 |
does not hide errors, |
| 586 |
can be applied to (parts of) existing mappings and will always populate |
| 587 |
(prefault) page tables readable. |
| 588 |
One example use case is prefaulting a file mapping, |
| 589 |
reading all file content from disk; |
| 590 |
however, |
| 591 |
pages won't be dirtied and consequently won't have to be written back to disk |
| 592 |
when evicting the pages from memory. |
| 593 |
.IP |
| 594 |
Depending on the underlying mapping, |
| 595 |
map the shared zeropage, |
| 596 |
preallocate memory or read the underlying file; |
| 597 |
files with holes might or might not preallocate blocks. |
| 598 |
If populating fails, |
| 599 |
a |
| 600 |
.B SIGBUS |
| 601 |
signal is not generated; |
| 602 |
instead, an error is returned. |
| 603 |
.IP |
| 604 |
If |
| 605 |
.B MADV_POPULATE_READ |
| 606 |
succeeds, |
| 607 |
all page tables have been populated (prefaulted) readable once. |
| 608 |
If |
| 609 |
.B MADV_POPULATE_READ |
| 610 |
fails, |
| 611 |
some page tables might have been populated. |
| 612 |
.IP |
| 613 |
.B MADV_POPULATE_READ |
| 614 |
cannot be applied to mappings without read permissions |
| 615 |
and special mappings, |
| 616 |
for example, |
| 617 |
mappings marked with kernel-internal flags such as |
| 618 |
.B VM_PFNMAP |
| 619 |
or |
| 620 |
.BR VM_IO , |
| 621 |
or secret memory regions created using |
| 622 |
.BR memfd_secret(2) . |
| 623 |
.IP |
| 624 |
Note that with |
| 625 |
.BR MADV_POPULATE_READ , |
| 626 |
the process can be killed at any moment when the system runs out of memory. |
| 627 |
.TP |
| 628 |
.BR MADV_POPULATE_WRITE " (since Linux 5.14)" |
| 629 |
Populate (prefault) page tables writable, |
| 630 |
faulting in all pages in the range just as if manually writing to each |
| 631 |
each page; |
| 632 |
however, |
| 633 |
avoid the actual memory access that would have been performed after handling |
| 634 |
the fault. |
| 635 |
.IP |
| 636 |
In contrast to |
| 637 |
.BR MAP_POPULATE , |
| 638 |
MADV_POPULATE_WRITE does not hide errors, |
| 639 |
can be applied to (parts of) existing mappings and will always populate |
| 640 |
(prefault) page tables writable. |
| 641 |
One example use case is preallocating memory, |
| 642 |
breaking any CoW (Copy on Write). |
| 643 |
.IP |
| 644 |
Depending on the underlying mapping, |
| 645 |
preallocate memory or read the underlying file; |
| 646 |
files with holes will preallocate blocks. |
| 647 |
If populating fails, |
| 648 |
a |
| 649 |
.B SIGBUS |
| 650 |
signal is not generated; |
| 651 |
instead, an error is returned. |
| 652 |
.IP |
| 653 |
If |
| 654 |
.B MADV_POPULATE_WRITE |
| 655 |
succeeds, |
| 656 |
all page tables have been populated (prefaulted) writable once. |
| 657 |
If |
| 658 |
.B MADV_POPULATE_WRITE |
| 659 |
fails, |
| 660 |
some page tables might have been populated. |
| 661 |
.IP |
| 662 |
.B MADV_POPULATE_WRITE |
| 663 |
cannot be applied to mappings without write permissions |
| 664 |
and special mappings, |
| 665 |
for example, |
| 666 |
mappings marked with kernel-internal flags such as |
| 667 |
.B VM_PFNMAP |
| 668 |
or |
| 669 |
.BR VM_IO , |
| 670 |
or secret memory regions created using |
| 671 |
.BR memfd_secret(2) . |
| 672 |
.IP |
| 673 |
Note that with |
| 674 |
.BR MADV_POPULATE_WRITE , |
| 675 |
the process can be killed at any moment when the system runs out of memory. |
| 676 |
.TP |
| 677 |
.BR MADV_GUARD_INSTALL " (since Linux 6.13)" |
| 678 |
Install a lightweight guard region |
| 679 |
into the range specified by |
| 680 |
.I addr |
| 681 |
and |
| 682 |
.IR size , |
| 683 |
causing any read or write in the range to result in a |
| 684 |
.B SIGSEGV |
| 685 |
signal being raised. |
| 686 |
.IP |
| 687 |
If the region maps memory pages |
| 688 |
those mappings will be replaced |
| 689 |
as part of the operation, |
| 690 |
though if |
| 691 |
.B MADV_GUARD_INSTALL |
| 692 |
is applied to regions |
| 693 |
containing pre-existing lightweight guard regions, |
| 694 |
they are left in place. |
| 695 |
.IP |
| 696 |
Prior to Linux 6.15, |
| 697 |
this operation was supported |
| 698 |
only for writable anonymous private mappings. |
| 699 |
Since Linux 6.15, |
| 700 |
both anonymous and file-backed mappings are supported, |
| 701 |
including read-only mappings. |
| 702 |
.IP |
| 703 |
The mapping must not be mlock'd, |
| 704 |
map hugetlb ranges, |
| 705 |
nor contain special mappings. |
| 706 |
For example, |
| 707 |
mappings marked with kernel-internal flags such as |
| 708 |
.B VM_PFNMAP |
| 709 |
or |
| 710 |
.BR VM_IO , |
| 711 |
or secret memory regions created using |
| 712 |
.BR memfd_secret (2). |
| 713 |
.IP |
| 714 |
An |
| 715 |
.B EINVAL |
| 716 |
error is returned if it is attempted on any other kind of mapping. |
| 717 |
.IP |
| 718 |
This operation is more efficient than mapping a new region of memory |
| 719 |
.BR PROT_NONE , |
| 720 |
as it does not require the establishment of new mappings. |
| 721 |
Instead, |
| 722 |
regions of an existing mapping |
| 723 |
simply have their page tables |
| 724 |
manipulated to establish the desired behavior. |
| 725 |
No additional memory is used. |
| 726 |
.IP |
| 727 |
Lightweight guard regions remain on fork |
| 728 |
(except for any parts which have had |
| 729 |
.B MADV_WIPEONFORK |
| 730 |
applied to them), |
| 731 |
and are not removed by |
| 732 |
.BR MADV_DONTNEED , |
| 733 |
.BR MADV_FREE , |
| 734 |
.BR MADV_PAGEOUT , |
| 735 |
or |
| 736 |
.BR MADV_COLD . |
| 737 |
.IP |
| 738 |
Attempting to |
| 739 |
.BR mlock (2) |
| 740 |
lightweight guard regions will fail, |
| 741 |
as will |
| 742 |
.B MADV_POPULATE_READ |
| 743 |
or |
| 744 |
.BR MADV_POPULATE_WRITE . |
| 745 |
.IP |
| 746 |
If the mapping has its attributes changed, |
| 747 |
or is split or partially unmapped, |
| 748 |
any existing guard regions remain in place |
| 749 |
(except if they are unmapped). |
| 750 |
.IP |
| 751 |
If a mapping is moved using |
| 752 |
.BR mremap (2), |
| 753 |
lightweight guard regions are moved with it. |
| 754 |
.IP |
| 755 |
Lightweight guard regions are removed when unmapped, |
| 756 |
on process teardown, |
| 757 |
or when the |
| 758 |
.B MADV_GUARD_REMOVE |
| 759 |
operation is applied to them. |
| 760 |
.TP |
| 761 |
.BR MADV_GUARD_REMOVE " (since Linux 6.13)" |
| 762 |
Remove any lightweight guard regions |
| 763 |
which exist in the range specified by |
| 764 |
.I addr |
| 765 |
and |
| 766 |
.IR size . |
| 767 |
.IP |
| 768 |
All mappings in the range |
| 769 |
other than lightweight guard regions |
| 770 |
are left in place. |
| 771 |
The operation is supported on those mappings |
| 772 |
permitted by |
| 773 |
.B MADV_GUARD_INSTALL |
| 774 |
in addition to mlock()'d mappings, |
| 775 |
returning an |
| 776 |
.B EINVAL |
| 777 |
error otherwise. |
| 778 |
.IP |
| 779 |
When lightweight guard regions are removed, |
| 780 |
they act as empty regions of the containing mapping. |
| 781 |
Therefore, |
| 782 |
anonymous private mappings become |
| 783 |
zero-fill-on-demand pages, |
| 784 |
and file-backed mappings are repopulated with the |
| 785 |
memory contents from the up-to-date contents of the |
| 786 |
underlying mapped file. |
| 787 |
.IP |
| 788 |
If any transparent huge pages are encountered in the operation, |
| 789 |
they are left in place. |
| 790 |
.SH RETURN VALUE |
| 791 |
On success, |
| 792 |
.BR madvise () |
| 793 |
returns zero. |
| 794 |
On error, it returns \-1 and |
| 795 |
.I errno |
| 796 |
is set to indicate the error. |
| 797 |
.SH ERRORS |
| 798 |
.TP |
| 799 |
.B EACCES |
| 800 |
.I advice |
| 801 |
is |
| 802 |
.BR MADV_REMOVE , |
| 803 |
but the specified address range is not a shared writable mapping. |
| 804 |
.TP |
| 805 |
.B EAGAIN |
| 806 |
A kernel resource was temporarily unavailable. |
| 807 |
.TP |
| 808 |
.B EBADF |
| 809 |
The map exists, but the area maps something that isn't a file. |
| 810 |
.TP |
| 811 |
.B EBUSY |
| 812 |
(for |
| 813 |
.BR MADV_COLLAPSE ) |
| 814 |
Could not charge hugepage to cgroup: cgroup limit exceeded. |
| 815 |
.TP |
| 816 |
.B EBUSY |
| 817 |
(for |
| 818 |
.BR MADV_SOFT_OFFLINE ) |
| 819 |
Any pages within the specified address range |
| 820 |
could not be offlined. |
| 821 |
This might occur if the page is currently in use or locked. |
| 822 |
.TP |
| 823 |
.B EFAULT |
| 824 |
.I advice |
| 825 |
is |
| 826 |
.B MADV_POPULATE_READ |
| 827 |
or |
| 828 |
.BR MADV_POPULATE_WRITE , |
| 829 |
and populating (prefaulting) page tables failed because a |
| 830 |
.B SIGBUS |
| 831 |
would have been generated on actual memory access and the reason is not a |
| 832 |
HW poisoned page |
| 833 |
(HW poisoned pages can, |
| 834 |
for example, |
| 835 |
be created using the |
| 836 |
.B MADV_HWPOISON |
| 837 |
flag described elsewhere in this page). |
| 838 |
.TP |
| 839 |
.B EINVAL |
| 840 |
.I addr |
| 841 |
is not page-aligned or |
| 842 |
.I size |
| 843 |
is negative. |
| 844 |
.\" .I size |
| 845 |
.\" is zero, |
| 846 |
.TP |
| 847 |
.B EINVAL |
| 848 |
.I advice |
| 849 |
is not a valid. |
| 850 |
.TP |
| 851 |
.B EINVAL |
| 852 |
.I advice |
| 853 |
is |
| 854 |
.B MADV_COLD |
| 855 |
or |
| 856 |
.B MADV_PAGEOUT |
| 857 |
and the specified address range includes locked, Huge TLB pages, or |
| 858 |
.B VM_PFNMAP |
| 859 |
pages. |
| 860 |
.TP |
| 861 |
.B EINVAL |
| 862 |
.I advice |
| 863 |
is |
| 864 |
.B MADV_DONTNEED |
| 865 |
or |
| 866 |
.B MADV_REMOVE |
| 867 |
and the specified address range includes locked, Huge TLB pages, or |
| 868 |
.B VM_PFNMAP |
| 869 |
pages. |
| 870 |
.TP |
| 871 |
.B EINVAL |
| 872 |
.I advice |
| 873 |
is |
| 874 |
.B MADV_MERGEABLE |
| 875 |
or |
| 876 |
.BR MADV_UNMERGEABLE , |
| 877 |
but the kernel was not configured with |
| 878 |
.BR CONFIG_KSM . |
| 879 |
.TP |
| 880 |
.B EINVAL |
| 881 |
.I advice |
| 882 |
is |
| 883 |
.B MADV_FREE |
| 884 |
or |
| 885 |
.B MADV_WIPEONFORK |
| 886 |
but the specified address range includes file, Huge TLB, |
| 887 |
.BR MAP_SHARED , |
| 888 |
or |
| 889 |
.B VM_PFNMAP |
| 890 |
ranges. |
| 891 |
.TP |
| 892 |
.B EINVAL |
| 893 |
.I advice |
| 894 |
is |
| 895 |
.B MADV_POPULATE_READ |
| 896 |
or |
| 897 |
.BR MADV_POPULATE_WRITE , |
| 898 |
but the specified address range includes ranges with insufficient permissions |
| 899 |
or special mappings, |
| 900 |
for example, |
| 901 |
mappings marked with kernel-internal flags such a |
| 902 |
.B VM_IO |
| 903 |
or |
| 904 |
.BR VM_PFNMAP , |
| 905 |
or secret memory regions created using |
| 906 |
.BR memfd_secret(2) . |
| 907 |
.TP |
| 908 |
.B EINVAL |
| 909 |
.I advice |
| 910 |
is |
| 911 |
.B MADV_GUARD_INSTALL |
| 912 |
or |
| 913 |
.BR MADV_GUARD_REMOVE , |
| 914 |
but the specified address range contains an unsupported mapping. |
| 915 |
.TP |
| 916 |
.B EIO |
| 917 |
(for |
| 918 |
.BR MADV_WILLNEED ) |
| 919 |
Paging in this area would exceed the process's |
| 920 |
maximum resident set size. |
| 921 |
.TP |
| 922 |
.B ENOMEM |
| 923 |
(for |
| 924 |
.BR MADV_WILLNEED ) |
| 925 |
Not enough memory: paging in failed. |
| 926 |
.TP |
| 927 |
.B ENOMEM |
| 928 |
(for |
| 929 |
.BR MADV_COLLAPSE ) |
| 930 |
Not enough memory: could not allocate hugepage. |
| 931 |
.TP |
| 932 |
.B ENOMEM |
| 933 |
Addresses in the specified range are not currently |
| 934 |
mapped, or are outside the address space of the process. |
| 935 |
.TP |
| 936 |
.B ENOMEM |
| 937 |
.I advice |
| 938 |
is |
| 939 |
.B MADV_POPULATE_READ |
| 940 |
or |
| 941 |
.BR MADV_POPULATE_WRITE , |
| 942 |
and populating (prefaulting) page tables failed because there was not enough |
| 943 |
memory. |
| 944 |
.TP |
| 945 |
.B EPERM |
| 946 |
.I advice |
| 947 |
is |
| 948 |
.BR MADV_HWPOISON , |
| 949 |
but the caller does not have the |
| 950 |
.B CAP_SYS_ADMIN |
| 951 |
capability. |
| 952 |
.TP |
| 953 |
.B EHWPOISON |
| 954 |
.I advice |
| 955 |
is |
| 956 |
.B MADV_POPULATE_READ |
| 957 |
or |
| 958 |
.BR MADV_POPULATE_WRITE , |
| 959 |
and populating (prefaulting) page tables failed because a HW poisoned page |
| 960 |
(HW poisoned pages can, |
| 961 |
for example, |
| 962 |
be created using the |
| 963 |
.B MADV_HWPOISON |
| 964 |
flag described elsewhere in this page) |
| 965 |
was encountered. |
| 966 |
.SH VERSIONS |
| 967 |
Versions of this system call, implementing a wide variety of |
| 968 |
.I advice |
| 969 |
values, exist on many other implementations. |
| 970 |
Other implementations typically implement at least the flags listed |
| 971 |
above under |
| 972 |
.IR "Conventional advice flags" , |
| 973 |
albeit with some variation in semantics. |
| 974 |
.P |
| 975 |
POSIX.1-2001 describes |
| 976 |
.BR posix_madvise (3) |
| 977 |
with constants |
| 978 |
.BR POSIX_MADV_NORMAL , |
| 979 |
.BR POSIX_MADV_RANDOM , |
| 980 |
.BR POSIX_MADV_SEQUENTIAL , |
| 981 |
.BR POSIX_MADV_WILLNEED , |
| 982 |
and |
| 983 |
.BR POSIX_MADV_DONTNEED , |
| 984 |
and so on, with behavior close to the similarly named flags listed above. |
| 985 |
.SS Linux |
| 986 |
The Linux implementation requires that the address |
| 987 |
.I addr |
| 988 |
be page-aligned, and allows |
| 989 |
.I size |
| 990 |
to be zero. |
| 991 |
If there are some parts of the specified address range |
| 992 |
that are not mapped, the Linux version of |
| 993 |
.BR madvise () |
| 994 |
ignores them and applies the call to the rest (but returns |
| 995 |
.B ENOMEM |
| 996 |
from the system call, as it should). |
| 997 |
.P |
| 998 |
.I madvise(0,\ 0,\ advice) |
| 999 |
will return zero iff |
| 1000 |
.I advice |
| 1001 |
is supported by the kernel and can be relied on to probe for support. |
| 1002 |
.SH STANDARDS |
| 1003 |
None. |
| 1004 |
.SH HISTORY |
| 1005 |
First appeared in 4.4BSD. |
| 1006 |
.P |
| 1007 |
Since Linux 3.18, |
| 1008 |
.\" commit d3ac21cacc24790eb45d735769f35753f5b56ceb |
| 1009 |
support for this system call is optional, |
| 1010 |
depending on the setting of the |
| 1011 |
.B CONFIG_ADVISE_SYSCALLS |
| 1012 |
configuration option. |
| 1013 |
.SH SEE ALSO |
| 1014 |
.BR getrlimit (2), |
| 1015 |
.BR memfd_secret (2), |
| 1016 |
.BR mincore (2), |
| 1017 |
.BR mmap (2), |
| 1018 |
.BR mprotect (2), |
| 1019 |
.BR msync (2), |
| 1020 |
.BR munmap (2), |
| 1021 |
.BR prctl (2), |
| 1022 |
.BR process_madvise (2), |
| 1023 |
.BR posix_madvise (3), |
| 1024 |
.BR core (5) |