1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (C) 2001 David Gómez <davidge@jazzfree.com>
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" 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.
33 .TH MADVISE 2 2010-06-20 "Linux" "Linux Programmer's Manual"
35 madvise \- give advice about use of memory
37 .B #include <sys/mman.h>
39 .BI "int madvise(void *" addr ", size_t " length ", int " advice );
42 Feature Test Macro Requirements for glibc (see
43 .BR feature_test_macros (7)):
51 system call advises the kernel about how to handle paging input/output in
52 the address range beginning at address
57 It allows an application to tell the kernel how it expects to use
58 some mapped or shared memory areas, so that the kernel can choose
59 appropriate read-ahead and caching techniques.
60 This call does not influence the semantics of the application
61 (except in the case of
64 may influence its performance.
65 The kernel is free to ignore the advice.
67 The advice is indicated in the
76 Expect page references in random order.
77 (Hence, read ahead may be less useful than normally.)
80 Expect page references in sequential order.
81 (Hence, pages in the given range can be aggressively read ahead,
82 and may be freed soon after they are accessed.)
85 Expect access in the near future.
86 (Hence, it might be a good idea to read some pages ahead.)
89 Do not expect access in the near future.
90 (For the time being, the application is finished with the given range,
91 so the kernel can free resources associated with it.)
92 Subsequent accesses of pages in this range will succeed, but will result
93 either in reloading of the memory contents from the underlying mapped file
96 or zero-fill-on-demand pages for mappings
97 without an underlying file.
99 .BR MADV_REMOVE " (Since Linux 2.6.16)"
100 Free up a given range of pages
101 and its associated backing store.
104 only shmfs/tmpfs supports this; other file systems return with the
107 .\" Databases want to use this feature to drop a section of their
108 .\" bufferpool (shared memory segments) - without writing back to
109 .\" disk/swap space. This feature is also useful for supporting
110 .\" hot-plug memory on UML.
112 .BR MADV_DONTFORK " (Since Linux 2.6.16)"
113 .\" See http://lwn.net/Articles/171941/
114 Do not make the pages in this range available to the child after a
116 This is useful to prevent copy-on-write semantics from changing
117 the physical location of a page(s) if the parent writes to it after a
119 (Such page relocations cause problems for hardware that
120 DMAs into the page(s).)
121 .\" [PATCH] madvise MADV_DONTFORK/MADV_DOFORK
122 .\" Currently, copy-on-write may change the physical address of
123 .\" a page even if the user requested that the page is pinned in
124 .\" memory (either by mlock or by get_user_pages). This happens
125 .\" if the process forks meanwhile, and the parent writes to that
126 .\" page. As a result, the page is orphaned: in case of
127 .\" get_user_pages, the application will never see any data hardware
128 .\" DMA's into this page after the COW. In case of mlock'd memory,
129 .\" the parent is not getting the realtime/security benefits of mlock.
131 .\" In particular, this affects the Infiniband modules which do DMA from
132 .\" and into user pages all the time.
134 .\" This patch adds madvise options to control whether memory range is
135 .\" inherited across fork. Useful e.g. for when hardware is doing DMA
136 .\" from/into these pages. Could also be useful to an application
137 .\" wanting to speed up its forks by cutting large areas out of
140 .\" SEE ALSO: http://lwn.net/Articles/171941/
141 .\" "Tweaks to madvise() and posix_fadvise()", 14 Feb 2006
143 .BR MADV_DOFORK " (Since Linux 2.6.16)"
146 restoring the default behavior, whereby a mapping is inherited across
149 .BR MADV_HWPOISON " (Since Linux 2.6.32)
150 Poison a page and handle it like a hardware memory corruption.
151 This operation is only available for privileged
152 .RB ( CAP_SYS_ADMIN )
154 This operation may result in the calling process receiving a
156 and the page being unmapped.
157 This feature is intended for testing of memory error-handling code;
158 it is only available if the kernel was configured with
159 .BR CONFIG_MEMORY_FAILURE .
161 .BR MADV_SOFT_OFFLINE " (Since Linux 2.6.33)
162 Soft offline the pages in the range specified by
166 The memory of each page in the specified range is preserved
167 (i.e., when next accessed, the same content will be visible,
168 but in a new physical page frame),
169 and the original page is offlined
170 (i.e., no longer used, and taken out of normal memory management).
173 operation is invisible to (i.e., does not change the semantics of)
175 This feature is intended for testing of memory error-handling code;
176 it is only available if the kernel was configured with
177 .BR CONFIG_MEMORY_FAILURE .
179 .BR MADV_MERGEABLE " (since Linux 2.6.32)"
180 Enable Kernel Samepage Merging (KSM) for the pages in the range specified by
184 The kernel regularly scans those areas of user memory that have
185 been marked as mergeable,
186 looking for pages with identical content.
187 These are replaced by a single write-protected page (which is automatically
188 copied if a process later wants to update the content of the page).
189 KSM only merges private anonymous pages (see
191 The KSM feature is intended for applications that generate many
192 instances of the same data (e.g., virtualization systems such as KVM).
193 It can consume a lot of processing power; use with care.
194 See the kernel source file
195 .I Documentation/vm/ksm.txt
201 operations are only available if the kernel was configured with
204 .BR MADV_UNMERGEABLE " (since Linux 2.6.32)"
205 Undo the effect of an earlier
207 operation on the specified address range;
208 KSM unmerges whatever pages it had merged in the address range specified by
216 On error, it returns \-1 and
218 is set appropriately.
222 A kernel resource was temporarily unavailable.
225 The map exists, but the area maps something that isn't a file.
228 This error can occur for the following reasons:
243 The application is attempting to release locked or shared pages (with
251 but the kernel was not configured with
258 Paging in this area would exceed the process's
259 maximum resident set size.
264 Not enough memory: paging in failed.
267 Addresses in the specified range are not currently
268 mapped, or are outside the address space of the process.
271 POSIX.1-2001 describes
272 .BR posix_madvise (3)
273 .\" FIXME . Write a posix_fadvise(3) page.
275 .BR POSIX_MADV_NORMAL ,
277 with a behavior close to that described here.
279 .BR posix_fadvise (2)
293 The current Linux implementation (2.4.0) views this system call
294 more as a command than as advice and hence may return an error
295 when it cannot do what it usually would do in response to this
297 (See the ERRORS description above.)
298 This is nonstandard behavior.
300 The Linux implementation requires that the address
302 be page-aligned, and allows
305 If there are some parts of the specified address range
306 that are not mapped, the Linux version of
308 ignores them and applies the call to the rest (but returns
310 from the system call, as it should).
314 .\" function first appeared in 4.4BSD.