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