1 .\" Copyright (C) 2001 David Gómez <davidge@jazzfree.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\" Based on comments from mm/filemap.c. Last modified on 10-06-2001
6 .\" Modified, 25 Feb 2002, Michael Kerrisk, <mtk.manpages@gmail.com>
7 .\" Added notes on MADV_DONTNEED
8 .\" 2010-06-19, mtk, Added documentation of MADV_MERGEABLE and
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 <cardoe@cardoe.com>
13 .\" Document MADV_HUGEPAGE and MADV_NOHUGEPAGE
15 .TH MADVISE 2 2021-03-22 "Linux" "Linux Programmer's Manual"
17 madvise \- give advice about use of memory
20 .RI ( libc ", " \-lc )
23 .B #include <sys/mman.h>
25 .BI "int madvise(void *" addr ", size_t " length ", int " advice );
29 Feature Test Macro Requirements for glibc (see
30 .BR feature_test_macros (7)):
37 Up to and including glibc 2.19:
43 system call is used to give advice or directions to the kernel
44 about the address range beginning at address
50 the goal of such advice is to improve system or application performance.
52 Initially, the system call supported a set of "conventional"
54 values, which are also available on several other implementations.
57 is not specified in POSIX.)
58 Subsequently, a number of Linux-specific
60 values have been added.
62 .\" ======================================================================
64 .SS Conventional advice values
68 allow an application to tell the kernel how it expects to use
69 some mapped or shared memory areas, so that the kernel can choose
70 appropriate read-ahead and caching techniques.
73 values do not influence the semantics of the application
74 (except in the case of
76 but may influence its performance.
79 values listed here have analogs in the POSIX-specified
81 function, and the values have the same meanings, with the exception of
84 The advice is indicated in the
86 argument, which is one of the following:
93 Expect page references in random order.
94 (Hence, read ahead may be less useful than normally.)
97 Expect page references in sequential order.
98 (Hence, pages in the given range can be aggressively read ahead,
99 and may be freed soon after they are accessed.)
102 Expect access in the near future.
103 (Hence, it might be a good idea to read some pages ahead.)
106 Do not expect access in the near future.
107 (For the time being, the application is finished with the given range,
108 so the kernel can free resources associated with it.)
113 the semantics of memory access in the specified region are changed:
114 subsequent accesses of pages in the range will succeed, but will result
115 in either repopulating the memory contents from the
116 up-to-date contents of the underlying mapped file
117 (for shared file mappings, shared anonymous mappings,
118 and shmem-based techniques such as System V shared memory segments)
119 or zero-fill-on-demand pages for anonymous private mappings.
121 Note that, when applied to shared mappings,
123 might not lead to immediate freeing of the pages in the range.
124 The kernel is free to delay freeing the pages until an appropriate moment.
125 The resident set size (RSS) of the calling process will be immediately
129 cannot be applied to locked pages, Huge TLB pages, or
132 (Pages marked with the kernel-internal
134 .\" http://lwn.net/Articles/162860/
135 flag are special memory areas that are not managed
136 by the virtual memory subsystem.
137 Such pages are typically created by device drivers that
138 map the pages into user space.)
140 .\" ======================================================================
142 .SS Linux-specific advice values
143 The following Linux-specific
145 values have no counterparts in the POSIX-specified
146 .BR posix_madvise (3),
147 and may or may not have counterparts in the
149 interface available on other implementations.
150 Note that some of these operations change the semantics of memory accesses.
152 .BR MADV_REMOVE " (since Linux 2.6.16)"
153 .\" commit f6b3ec238d12c8cc6cc71490c6e3127988460349
154 Free up a given range of pages
155 and its associated backing store.
156 This is equivalent to punching a hole in the corresponding byte
157 range of the backing store (see
159 Subsequent accesses in the specified address range will see
160 bytes containing zero.
161 .\" Databases want to use this feature to drop a section of their
162 .\" bufferpool (shared memory segments) - without writing back to
163 .\" disk/swap space. This feature is also useful for supporting
164 .\" hot-plug memory on UML.
166 The specified address range must be mapped shared and writable.
167 This flag cannot be applied to locked pages, Huge TLB pages, or
171 In the initial implementation, only
176 .\" commit 3f31d07571eeea18a7d34db9af21d2285b807a17
177 any filesystem which supports the
179 .B FALLOC_FL_PUNCH_HOLE
182 Hugetlbfs fails with the error
184 and other filesystems fail with the error
187 .BR MADV_DONTFORK " (since Linux 2.6.16)"
188 .\" commit f822566165dd46ff5de9bf895cfa6c51f53bb0c4
189 .\" See http://lwn.net/Articles/171941/
190 Do not make the pages in this range available to the child after a
192 This is useful to prevent copy-on-write semantics from changing
193 the physical location of a page if the parent writes to it after a
195 (Such page relocations cause problems for hardware that
197 .\" [PATCH] madvise MADV_DONTFORK/MADV_DOFORK
198 .\" Currently, copy-on-write may change the physical address of
199 .\" a page even if the user requested that the page is pinned in
200 .\" memory (either by mlock or by get_user_pages). This happens
201 .\" if the process forks meanwhile, and the parent writes to that
202 .\" page. As a result, the page is orphaned: in case of
203 .\" get_user_pages, the application will never see any data hardware
204 .\" DMA's into this page after the COW. In case of mlock'd memory,
205 .\" the parent is not getting the realtime/security benefits of mlock.
207 .\" In particular, this affects the Infiniband modules which do DMA from
208 .\" and into user pages all the time.
210 .\" This patch adds madvise options to control whether memory range is
211 .\" inherited across fork. Useful e.g. for when hardware is doing DMA
212 .\" from/into these pages. Could also be useful to an application
213 .\" wanting to speed up its forks by cutting large areas out of
216 .\" SEE ALSO: http://lwn.net/Articles/171941/
217 .\" "Tweaks to madvise() and posix_fadvise()", 14 Feb 2006
219 .BR MADV_DOFORK " (since Linux 2.6.16)"
222 restoring the default behavior, whereby a mapping is inherited across
225 .BR MADV_HWPOISON " (since Linux 2.6.32)"
226 .\" commit 9893e49d64a4874ea67849ee2cfbf3f3d6817573
227 Poison the pages in the range specified by
231 and handle subsequent references to those pages
232 like a hardware memory corruption.
233 This operation is available only for privileged
234 .RB ( CAP_SYS_ADMIN )
236 This operation may result in the calling process receiving a
238 and the page being unmapped.
240 This feature is intended for testing of memory error-handling code;
241 it is available only if the kernel was configured with
242 .BR CONFIG_MEMORY_FAILURE .
244 .BR MADV_MERGEABLE " (since Linux 2.6.32)"
245 .\" commit f8af4da3b4c14e7267c4ffb952079af3912c51c5
246 Enable Kernel Samepage Merging (KSM) for the pages in the range specified by
250 The kernel regularly scans those areas of user memory that have
251 been marked as mergeable,
252 looking for pages with identical content.
253 These are replaced by a single write-protected page (which is automatically
254 copied if a process later wants to update the content of the page).
255 KSM merges only private anonymous pages (see
258 The KSM feature is intended for applications that generate many
259 instances of the same data (e.g., virtualization systems such as KVM).
260 It can consume a lot of processing power; use with care.
261 See the Linux kernel source file
262 .I Documentation/admin\-guide/mm/ksm.rst
269 operations are available only if the kernel was configured with
272 .BR MADV_UNMERGEABLE " (since Linux 2.6.32)"
273 Undo the effect of an earlier
275 operation on the specified address range;
276 KSM unmerges whatever pages it had merged in the address range specified by
281 .BR MADV_SOFT_OFFLINE " (since Linux 2.6.33)"
282 .\" commit afcf938ee0aac4ef95b1a23bac704c6fbeb26de6
283 Soft offline the pages in the range specified by
287 The memory of each page in the specified range is preserved
288 (i.e., when next accessed, the same content will be visible,
289 but in a new physical page frame),
290 and the original page is offlined
291 (i.e., no longer used, and taken out of normal memory management).
294 operation is invisible to (i.e., does not change the semantics of)
297 This feature is intended for testing of memory error-handling code;
298 it is available only if the kernel was configured with
299 .BR CONFIG_MEMORY_FAILURE .
301 .BR MADV_HUGEPAGE " (since Linux 2.6.38)"
302 .\" commit 0af4e98b6b095c74588af04872f83d333c958c32
303 .\" http://lwn.net/Articles/358904/
304 .\" https://lwn.net/Articles/423584/
305 Enable Transparent Huge Pages (THP) for pages in the range specified by
309 Currently, Transparent Huge Pages work only with private anonymous pages (see
311 The kernel will regularly scan the areas marked as huge page candidates
312 to replace them with huge pages.
313 The kernel will also allocate huge pages directly when the region is
314 naturally aligned to the huge page size (see
315 .BR posix_memalign (2)).
317 This feature is primarily aimed at applications that use large mappings of
318 data and access large regions of that memory at a time (e.g., virtualization
319 systems such as QEMU).
320 It can very easily waste memory (e.g., a 2\ MB mapping that only ever accesses
321 1 byte will result in 2\ MB of wired memory instead of one 4\ KB page).
322 See the Linux kernel source file
323 .I Documentation/admin\-guide/mm/transhuge.rst
326 Most common kernels configurations provide
327 .BR MADV_HUGEPAGE -style
328 behavior by default, and thus
330 is normally not necessary.
331 It is mostly intended for embedded systems, where
332 .BR MADV_HUGEPAGE -style
333 behavior may not be enabled by default in the kernel.
335 this flag can be used in order to selectively enable THP.
338 is used, it should always be in regions of memory with
339 an access pattern that the developer knows in advance won't risk
340 to increase the memory footprint of the application when transparent
341 hugepages are enabled.
347 operations are available only if the kernel was configured with
348 .BR CONFIG_TRANSPARENT_HUGEPAGE .
350 .BR MADV_NOHUGEPAGE " (since Linux 2.6.38)"
351 Ensures that memory in the address range specified by
355 will not be backed by transparent hugepages.
357 .BR MADV_DONTDUMP " (since Linux 3.4)"
358 .\" commit 909af768e88867016f427264ae39d27a57b6a8ed
359 .\" commit accb61fe7bb0f5c2a4102239e4981650f9048519
360 Exclude from a core dump those pages in the range specified by
364 This is useful in applications that have large areas of memory
365 that are known not to be useful in a core dump.
368 takes precedence over the bit mask that is set via the
369 .I /proc/[pid]/coredump_filter
373 .BR MADV_DODUMP " (since Linux 3.4)"
374 Undo the effect of an earlier
377 .BR MADV_FREE " (since Linux 4.5)"
378 The application no longer requires the pages in the range specified by
382 The kernel can thus free these pages,
383 but the freeing could be delayed until memory pressure occurs.
384 For each of the pages that has been marked to be freed
385 but has not yet been freed,
386 the free operation will be canceled if the caller writes into the page.
389 operation, any stale data (i.e., dirty, unwritten pages) will be lost
390 when the kernel frees the pages.
391 However, subsequent writes to pages in the range will succeed
392 and then kernel cannot free those dirtied pages,
393 so that the caller can always see just written data.
394 If there is no subsequent write,
395 the kernel can free the pages at any time.
396 Once pages in the range have been freed, the caller will
397 see zero-fill-on-demand pages upon subsequent page references.
402 can be applied only to private anonymous pages (see
404 In Linux before version 4.12,
405 .\" commit 93e06c7a645343d222c9a838834a51042eebbbf7
406 when freeing pages on a swapless system,
407 the pages in the given range are freed instantly,
408 regardless of memory pressure.
410 .BR MADV_WIPEONFORK " (since Linux 4.14)"
411 .\" commit d2cd9ede6e193dd7d88b6d27399e96229a551b19
412 Present the child process with zero-filled memory in this range after a
414 This is useful in forking servers in order to ensure
415 that sensitive per-process data
416 (for example, PRNG seeds, cryptographic secrets, and so on)
417 is not handed to child processes.
421 operation can be applied only to private anonymous pages (see
424 Within the child created by
428 setting remains in place on the specified address range.
429 This setting is cleared during
432 .BR MADV_KEEPONFORK " (since Linux 4.14)"
433 .\" commit d2cd9ede6e193dd7d88b6d27399e96229a551b19
434 Undo the effect of an earlier
435 .BR MADV_WIPEONFORK .
437 .BR MADV_COLD " (since Linux 5.4)"
438 .\" commit 9c276cc65a58faf98be8e56962745ec99ab87636
439 Deactivate a given range of pages.
440 This will make the pages a more probable
441 reclaim target should there be a memory pressure.
442 This is a nondestructive operation.
443 The advice might be ignored for some pages in the range when it is not
446 .BR MADV_PAGEOUT " (since Linux 5.4)"
447 .\" commit 1a4e58cce84ee88129d5d49c064bd2852b481357
448 Reclaim a given range of pages.
449 This is done to free up memory occupied by these pages.
450 If a page is anonymous, it will be swapped out.
451 If a page is file-backed and dirty, it will be written back to the backing
453 The advice might be ignored for some pages in the range when it is not
456 .BR MADV_POPULATE_READ " (since Linux 5.14)"
457 "Populate (prefault) page tables readable,
458 faulting in all pages in the range just as if manually reading from each page;
460 avoid the actual memory access that would have been performed after handling
465 .B MADV_POPULATE_READ
466 does not hide errors,
467 can be applied to (parts of) existing mappings and will always populate
468 (prefault) page tables readable.
469 One example use case is prefaulting a file mapping,
470 reading all file content from disk;
472 pages won't be dirtied and consequently won't have to be written back to disk
473 when evicting the pages from memory.
475 Depending on the underlying mapping,
476 map the shared zeropage,
477 preallocate memory or read the underlying file;
478 files with holes might or might not preallocate blocks.
482 signal is not generated; instead, an error is returned.
485 .B MADV_POPULATE_READ
487 all page tables have been populated (prefaulted) readable once.
489 .B MADV_POPULATE_READ
491 some page tables might have been populated.
493 .B MADV_POPULATE_READ
494 cannot be applied to mappings without read permissions
495 and special mappings,
497 mappings marked with kernel-internal flags such as
501 or secret memory regions created using
502 .BR memfd_secret(2) .
505 .BR MADV_POPULATE_READ ,
506 the process can be killed at any moment when the system runs out of memory.
508 .BR MADV_POPULATE_WRITE " (since Linux 5.14)"
509 Populate (prefault) page tables writable,
510 faulting in all pages in the range just as if manually writing to each
513 avoid the actual memory access that would have been performed after handling
518 MADV_POPULATE_WRITE does not hide errors,
519 can be applied to (parts of) existing mappings and will always populate
520 (prefault) page tables writable.
521 One example use case is preallocating memory,
522 breaking any CoW (Copy on Write).
524 Depending on the underlying mapping,
525 preallocate memory or read the underlying file;
526 files with holes will preallocate blocks.
530 signal is not generated; instead, an error is returned.
533 .B MADV_POPULATE_WRITE
535 all page tables have been populated (prefaulted) writable once.
537 .B MADV_POPULATE_WRITE
539 some page tables might have been populated.
541 .B MADV_POPULATE_WRITE
542 cannot be applied to mappings without write permissions
543 and special mappings,
545 mappings marked with kernel-internal flags such as
549 or secret memory regions created using
550 .BR memfd_secret(2) .
553 .BR MADV_POPULATE_WRITE ,
554 the process can be killed at any moment when the system runs out of memory.
559 On error, it returns \-1 and
561 is set to indicate the error.
568 but the specified address range is not a shared writable mapping.
571 A kernel resource was temporarily unavailable.
574 The map exists, but the area maps something that isn't a file.
579 .B MADV_POPULATE_READ
581 .BR MADV_POPULATE_WRITE ,
582 and populating (prefaulting) page tables failed because a
584 would have been generated on actual memory access and the reason is not a
586 (HW poisoned pages can,
590 flag described elsewhere in this page).
594 is not page-aligned or
610 and the specified address range includes locked, Huge TLB pages, or
620 and the specified address range includes locked, Huge TLB pages, or
629 .BR MADV_UNMERGEABLE ,
630 but the kernel was not configured with
639 but the specified address range includes file, Huge TLB,
648 .B MADV_POPULATE_READ
650 .BR MADV_POPULATE_WRITE ,
651 but the specified address range includes ranges with insufficient permissions
654 mappings marked with kernel-internal flags such a
658 or secret memory regions created using
659 .BR memfd_secret(2) .
664 Paging in this area would exceed the process's
665 maximum resident set size.
670 Not enough memory: paging in failed.
673 Addresses in the specified range are not currently
674 mapped, or are outside the address space of the process.
679 .B MADV_POPULATE_READ
681 .BR MADV_POPULATE_WRITE ,
682 and populating (prefaulting) page tables failed because there was not enough
689 but the caller does not have the
696 .B MADV_POPULATE_READ
698 .BR MADV_POPULATE_WRITE ,
699 and populating (prefaulting) page tables failed because a HW poisoned page
700 (HW poisoned pages can,
704 flag described elsewhere in this page)
708 .\" commit d3ac21cacc24790eb45d735769f35753f5b56ceb
709 support for this system call is optional,
710 depending on the setting of the
711 .B CONFIG_ADVISE_SYSCALLS
712 configuration option.
715 is not specified by any standards.
716 Versions of this system call, implementing a wide variety of
718 values, exist on many other implementations.
719 Other implementations typically implement at least the flags listed
721 .IR "Conventional advice flags" ,
722 albeit with some variation in semantics.
724 POSIX.1-2001 describes
725 .BR posix_madvise (3)
727 .BR POSIX_MADV_NORMAL ,
728 .BR POSIX_MADV_RANDOM ,
729 .BR POSIX_MADV_SEQUENTIAL ,
730 .BR POSIX_MADV_WILLNEED ,
732 .BR POSIX_MADV_DONTNEED ,
733 and so on, with behavior close to the similarly named flags listed above.
736 The Linux implementation requires that the address
738 be page-aligned, and allows
741 If there are some parts of the specified address range
742 that are not mapped, the Linux version of
744 ignores them and applies the call to the rest (but returns
746 from the system call, as it should).
750 .\" function first appeared in 4.4BSD.
753 .BR memfd_secret (2),
760 .BR process_madvise (2),
761 .BR posix_madvise (3),