1 .\" Copyright (C) 2001 David Gómez <davidge@jazzfree.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Based on comments from mm/filemap.c. Last modified on 10-06-2001
26 .\" Modified, 25 Feb 2002, Michael Kerrisk, <mtk.manpages@gmail.com>
27 .\" Added notes on MADV_DONTNEED
28 .\" 2010-06-19, mtk, Added documentation of MADV_MERGEABLE and
30 .\" 2010-06-15, Andi Kleen, Add documentation of MADV_HWPOISON.
31 .\" 2010-06-19, Andi Kleen, Add documentation of MADV_SOFT_OFFLINE.
32 .\" 2011-09-18, Doug Goldstein <cardoe@cardoe.com>
33 .\" Document MADV_HUGEPAGE and MADV_NOHUGEPAGE
35 .TH MADVISE 2 2014-04-20 "Linux" "Linux Programmer's Manual"
37 madvise \- give advice about use of memory
39 .B #include <sys/mman.h>
41 .BI "int madvise(void *" addr ", size_t " length ", int " advice );
44 Feature Test Macro Requirements for glibc (see
45 .BR feature_test_macros (7)):
53 system call advises the kernel about how to handle paging input/output in
54 the address range beginning at address
59 It allows an application to tell the kernel how it expects to use
60 some mapped or shared memory areas, so that the kernel can choose
61 appropriate read-ahead and caching techniques.
62 This call does not influence the semantics of the application
63 (except in the case of
66 may influence its performance.
67 The kernel is free to ignore the advice.
69 The advice is indicated in the
78 Expect page references in random order.
79 (Hence, read ahead may be less useful than normally.)
82 Expect page references in sequential order.
83 (Hence, pages in the given range can be aggressively read ahead,
84 and may be freed soon after they are accessed.)
87 Expect access in the near future.
88 (Hence, it might be a good idea to read some pages ahead.)
91 Do not expect access in the near future.
92 (For the time being, the application is finished with the given range,
93 so the kernel can free resources associated with it.)
94 Subsequent accesses of pages in this range will succeed, but will result
95 either in reloading of the memory contents from the underlying mapped file
98 or zero-fill-on-demand pages for mappings
99 without an underlying file.
101 .BR MADV_REMOVE " (since Linux 2.6.16)"
102 Free up a given range of pages
103 and its associated backing store.
106 only shmfs/tmpfs supports this; other filesystems return with the
109 .\" Databases want to use this feature to drop a section of their
110 .\" bufferpool (shared memory segments) - without writing back to
111 .\" disk/swap space. This feature is also useful for supporting
112 .\" hot-plug memory on UML.
114 .BR MADV_DONTFORK " (since Linux 2.6.16)"
115 .\" See http://lwn.net/Articles/171941/
116 Do not make the pages in this range available to the child after a
118 This is useful to prevent copy-on-write semantics from changing
119 the physical location of a page(s) if the parent writes to it after a
121 (Such page relocations cause problems for hardware that
122 DMAs into the page(s).)
123 .\" [PATCH] madvise MADV_DONTFORK/MADV_DOFORK
124 .\" Currently, copy-on-write may change the physical address of
125 .\" a page even if the user requested that the page is pinned in
126 .\" memory (either by mlock or by get_user_pages). This happens
127 .\" if the process forks meanwhile, and the parent writes to that
128 .\" page. As a result, the page is orphaned: in case of
129 .\" get_user_pages, the application will never see any data hardware
130 .\" DMA's into this page after the COW. In case of mlock'd memory,
131 .\" the parent is not getting the realtime/security benefits of mlock.
133 .\" In particular, this affects the Infiniband modules which do DMA from
134 .\" and into user pages all the time.
136 .\" This patch adds madvise options to control whether memory range is
137 .\" inherited across fork. Useful e.g. for when hardware is doing DMA
138 .\" from/into these pages. Could also be useful to an application
139 .\" wanting to speed up its forks by cutting large areas out of
142 .\" SEE ALSO: http://lwn.net/Articles/171941/
143 .\" "Tweaks to madvise() and posix_fadvise()", 14 Feb 2006
145 .BR MADV_DOFORK " (since Linux 2.6.16)"
148 restoring the default behavior, whereby a mapping is inherited across
151 .BR MADV_HWPOISON " (since Linux 2.6.32)
152 Poison a page and handle it like a hardware memory corruption.
153 This operation is available only for privileged
154 .RB ( CAP_SYS_ADMIN )
156 This operation may result in the calling process receiving a
158 and the page being unmapped.
159 This feature is intended for testing of memory error-handling code;
160 it is available only if the kernel was configured with
161 .BR CONFIG_MEMORY_FAILURE .
163 .BR MADV_SOFT_OFFLINE " (since Linux 2.6.33)
164 Soft offline the pages in the range specified by
168 The memory of each page in the specified range is preserved
169 (i.e., when next accessed, the same content will be visible,
170 but in a new physical page frame),
171 and the original page is offlined
172 (i.e., no longer used, and taken out of normal memory management).
175 operation is invisible to (i.e., does not change the semantics of)
177 This feature is intended for testing of memory error-handling code;
178 it is available only if the kernel was configured with
179 .BR CONFIG_MEMORY_FAILURE .
181 .BR MADV_MERGEABLE " (since Linux 2.6.32)"
182 Enable Kernel Samepage Merging (KSM) for the pages in the range specified by
186 The kernel regularly scans those areas of user memory that have
187 been marked as mergeable,
188 looking for pages with identical content.
189 These are replaced by a single write-protected page (which is automatically
190 copied if a process later wants to update the content of the page).
191 KSM merges only private anonymous pages (see
193 The KSM feature is intended for applications that generate many
194 instances of the same data (e.g., virtualization systems such as KVM).
195 It can consume a lot of processing power; use with care.
196 See the Linux kernel source file
197 .I Documentation/vm/ksm.txt
203 operations are available only if the kernel was configured with
206 .BR MADV_UNMERGEABLE " (since Linux 2.6.32)"
207 Undo the effect of an earlier
209 operation on the specified address range;
210 KSM unmerges whatever pages it had merged in the address range specified by
215 .BR MADV_HUGEPAGE " (since Linux 2.6.38)"
216 .\" http://lwn.net/Articles/358904/
217 .\" https://lwn.net/Articles/423584/
218 Enables Transparent Huge Pages (THP) for pages in the range specified by
222 Currently, Transparent Huge Pages work only with private anonymous pages (see
224 The kernel will regularly scan the areas marked as huge page candidates
225 to replace them with huge pages.
226 The kernel will also allocate huge pages directly when the region is
227 naturally aligned to the huge page size (see
228 .BR posix_memalign (2)).
229 This feature is primarily aimed at applications that use large mappings of
230 data and access large regions of that memory at a time (e.g., virtualization
231 systems such as QEMU).
232 It can very easily waste memory (e.g., a 2MB mapping that only ever accesses
233 1 byte will result in 2MB of wired memory instead of one 4KB page).
234 See the Linux kernel source file
235 .I Documentation/vm/transhuge.txt
241 operations are available only if the kernel was configured with
242 .BR CONFIG_TRANSPARENT_HUGEPAGE .
244 .BR MADV_NOHUGEPAGE " (since Linux 2.6.38)"
245 Ensures that memory in the address range specified by
249 will not be collapsed into huge pages.
251 .BR MADV_DONTDUMP " (since Linux 3.4)"
252 Exclude from a core dump those pages in the range specified by
256 This is useful in applications that have large areas of memory
257 that are known not to be useful in a core dump.
260 takes precedence over the bit mask that is set via the
261 .I /proc/PID/coredump_filter
265 .BR MADV_DODUMP " (since Linux 3.4)"
266 Undo the effect of an earlier
272 On error, it returns \-1 and
274 is set appropriately.
278 A kernel resource was temporarily unavailable.
281 The map exists, but the area maps something that isn't a file.
284 This error can occur for the following reasons:
299 The application is attempting to release locked or shared pages (with
307 but the kernel was not configured with
314 Paging in this area would exceed the process's
315 maximum resident set size.
320 Not enough memory: paging in failed.
323 Addresses in the specified range are not currently
324 mapped, or are outside the address space of the process.
327 POSIX.1-2001 describes
328 .BR posix_madvise (3)
329 .\" FIXME . Write a posix_madvise(3) page.
331 .BR POSIX_MADV_NORMAL ,
332 .BR POSIX_MADV_RANDOM ,
334 with a behavior close to that described here.
336 .BR posix_fadvise (2)
350 The current Linux implementation (2.4.0) views this system call
351 more as a command than as advice and hence may return an error
352 when it cannot do what it usually would do in response to this
354 (See the ERRORS description above.)
355 This is nonstandard behavior.
357 The Linux implementation requires that the address
359 be page-aligned, and allows
362 If there are some parts of the specified address range
363 that are not mapped, the Linux version of
365 ignores them and applies the call to the rest (but returns
367 from the system call, as it should).
371 .\" function first appeared in 4.4BSD.