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