]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/madvise.2
d64ce0f940ff44d49f4aa33d2a229ef393687439
[thirdparty/man-pages.git] / man2 / madvise.2
1 .\" Copyright (C) 2001 David Gómez <davidge@jazzfree.com>
2 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
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
29 .\" MADV_UNMERGEABLE
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
34 .\"
35 .TH MADVISE 2 2014-12-31 "Linux" "Linux Programmer's Manual"
36 .SH NAME
37 madvise \- give advice about use of memory
38 .SH SYNOPSIS
39 .B #include <sys/mman.h>
40 .sp
41 .BI "int madvise(void *" addr ", size_t " length ", int " advice );
42 .sp
43 .in -4n
44 Feature Test Macro Requirements for glibc (see
45 .BR feature_test_macros (7)):
46 .in
47 .sp
48 .BR madvise ():
49 _BSD_SOURCE
50 .SH DESCRIPTION
51 The
52 .BR madvise ()
53 system call is used to give advice or directions to the kernel
54 about the address range beginning at address
55 .I addr
56 and with size
57 .I length
58 bytes.
59 Initially, the system call supported a set of "conventional"
60 .I advice
61 values, which are also available on several other implementations.
62 (Note, though, that
63 .BR madvise ()
64 is not specified in POSIX.)
65 Subsequently, a number of Linux-specific
66 .IR advice
67 values have been added.
68 .\"
69 .\" ======================================================================
70 .\"
71 .SS Conventional advice values
72 The
73 .I advice
74 values listed below
75 allow an application to tell the kernel how it expects to use
76 some mapped or shared memory areas, so that the kernel can choose
77 appropriate read-ahead and caching techniques.
78 These
79 .I advice
80 values do not influence the semantics of the application
81 (except in the case of
82 .BR MADV_DONTNEED ),
83 but may influence its performance.
84 All of the
85 .I advice
86 values listed here have analogs in the POSIX-specified
87 .BR posix_madvise (3)
88 function, and the values have the same meanings, with the exception of
89 .BR MADV_DONTNEED .
90 .LP
91 The advice is indicated in the
92 .I advice
93 argument, which is one of the following:
94 .TP
95 .B MADV_NORMAL
96 No special treatment.
97 This is the default.
98 .TP
99 .B MADV_RANDOM
100 Expect page references in random order.
101 (Hence, read ahead may be less useful than normally.)
102 .TP
103 .B MADV_SEQUENTIAL
104 Expect page references in sequential order.
105 (Hence, pages in the given range can be aggressively read ahead,
106 and may be freed soon after they are accessed.)
107 .TP
108 .B MADV_WILLNEED
109 Expect access in the near future.
110 (Hence, it might be a good idea to read some pages ahead.)
111 .TP
112 .B MADV_DONTNEED
113 Do not expect access in the near future.
114 (For the time being, the application is finished with the given range,
115 so the kernel can free resources associated with it.)
116
117 After a successful
118 .B MADV_DONTNEED
119 operation,
120 the semantics of memory access in the specified region are changed:
121 subsequent accesses of pages in the range will succeed, but will result
122 in either reloading of the memory contents from 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
127 .B MADV_DONTNEED
128 cannot be applied to locked pages or Huge TLB pages.
129 .\"
130 .\" ======================================================================
131 .\"
132 .SS Linux-specific advice values
133 The following Linux-specific
134 .I advice
135 values have no counterparts in the POSIX-specified
136 .BR posix_madvise (3),
137 and may or may not have counterparts in the
138 .BR madvise ()
139 interface available on other implementations.
140 Note that some of these operations change the semantics of memory accesses.
141 .TP
142 .BR MADV_REMOVE " (since Linux 2.6.16)"
143 .\" commit f6b3ec238d12c8cc6cc71490c6e3127988460349
144 Free up a given range of pages
145 and its associated backing store.
146 This is equivalent to punching a hole in the corresponding byte
147 range of the backing store (see
148 .BR fallocate (2)).
149 Subsequent accesses in the specified address range will see
150 bytes containing zero.
151 .\" Databases want to use this feature to drop a section of their
152 .\" bufferpool (shared memory segments) - without writing back to
153 .\" disk/swap space. This feature is also useful for supporting
154 .\" hot-plug memory on UML.
155
156 The specified address range must be mapped shared and writable.
157 This flag cannot be applied to locked pages or Huge TLB pages.
158
159 In the initial implementation, only shmfs/tmpfs supported
160 .BR MADV_REMOVE ;
161 but since Linux 3.5,
162 .\" commit 3f31d07571eeea18a7d34db9af21d2285b807a17
163 any filesystem which supports the
164 .BR fallocate (2)
165 .BR FALLOC_FL_PUNCH_HOLE
166 mode also supports
167 .BR MADV_REMOVE .
168 Other filesystems fail with the error
169 .BR EOPNOTSUPP .
170 .TP
171 .BR MADV_DONTFORK " (since Linux 2.6.16)"
172 .\" commit f822566165dd46ff5de9bf895cfa6c51f53bb0c4
173 .\" See http://lwn.net/Articles/171941/
174 Do not make the pages in this range available to the child after a
175 .BR fork (2).
176 This is useful to prevent copy-on-write semantics from changing
177 the physical location of a page if the parent writes to it after a
178 .BR fork (2).
179 (Such page relocations cause problems for hardware that
180 DMAs into the page.)
181 .\" [PATCH] madvise MADV_DONTFORK/MADV_DOFORK
182 .\" Currently, copy-on-write may change the physical address of
183 .\" a page even if the user requested that the page is pinned in
184 .\" memory (either by mlock or by get_user_pages). This happens
185 .\" if the process forks meanwhile, and the parent writes to that
186 .\" page. As a result, the page is orphaned: in case of
187 .\" get_user_pages, the application will never see any data hardware
188 .\" DMA's into this page after the COW. In case of mlock'd memory,
189 .\" the parent is not getting the realtime/security benefits of mlock.
190 .\"
191 .\" In particular, this affects the Infiniband modules which do DMA from
192 .\" and into user pages all the time.
193 .\"
194 .\" This patch adds madvise options to control whether memory range is
195 .\" inherited across fork. Useful e.g. for when hardware is doing DMA
196 .\" from/into these pages. Could also be useful to an application
197 .\" wanting to speed up its forks by cutting large areas out of
198 .\" consideration.
199 .\"
200 .\" SEE ALSO: http://lwn.net/Articles/171941/
201 .\" "Tweaks to madvise() and posix_fadvise()", 14 Feb 2006
202 .TP
203 .BR MADV_DOFORK " (since Linux 2.6.16)"
204 Undo the effect of
205 .BR MADV_DONTFORK ,
206 restoring the default behavior, whereby a mapping is inherited across
207 .BR fork (2).
208 .TP
209 .BR MADV_HWPOISON " (since Linux 2.6.32)
210 .\" commit 9893e49d64a4874ea67849ee2cfbf3f3d6817573
211 Poison a page and handle it like a hardware memory corruption.
212 This operation is available only for privileged
213 .RB ( CAP_SYS_ADMIN )
214 processes.
215 This operation may result in the calling process receiving a
216 .B SIGBUS
217 and the page being unmapped.
218
219 This feature is intended for testing of memory error-handling code;
220 it is available only if the kernel was configured with
221 .BR CONFIG_MEMORY_FAILURE .
222 .TP
223 .BR MADV_SOFT_OFFLINE " (since Linux 2.6.33)
224 .\" commit afcf938ee0aac4ef95b1a23bac704c6fbeb26de6
225 Soft offline the pages in the range specified by
226 .I addr
227 and
228 .IR length .
229 The memory of each page in the specified range is preserved
230 (i.e., when next accessed, the same content will be visible,
231 but in a new physical page frame),
232 and the original page is offlined
233 (i.e., no longer used, and taken out of normal memory management).
234 The effect of the
235 .B MADV_SOFT_OFFLINE
236 operation is invisible to (i.e., does not change the semantics of)
237 the calling process.
238
239 This feature is intended for testing of memory error-handling code;
240 it is available only if the kernel was configured with
241 .BR CONFIG_MEMORY_FAILURE .
242 .TP
243 .BR MADV_MERGEABLE " (since Linux 2.6.32)"
244 .\" commit f8af4da3b4c14e7267c4ffb952079af3912c51c5
245 Enable Kernel Samepage Merging (KSM) for the pages in the range specified by
246 .I addr
247 and
248 .IR length .
249 The kernel regularly scans those areas of user memory that have
250 been marked as mergeable,
251 looking for pages with identical content.
252 These are replaced by a single write-protected page (which is automatically
253 copied if a process later wants to update the content of the page).
254 KSM merges only private anonymous pages (see
255 .BR mmap (2)).
256
257 The KSM feature is intended for applications that generate many
258 instances of the same data (e.g., virtualization systems such as KVM).
259 It can consume a lot of processing power; use with care.
260 See the Linux kernel source file
261 .I Documentation/vm/ksm.txt
262 for more details.
263
264 The
265 .BR MADV_MERGEABLE
266 and
267 .BR MADV_UNMERGEABLE
268 operations are available only if the kernel was configured with
269 .BR CONFIG_KSM .
270 .TP
271 .BR MADV_UNMERGEABLE " (since Linux 2.6.32)"
272 Undo the effect of an earlier
273 .BR MADV_MERGEABLE
274 operation on the specified address range;
275 KSM unmerges whatever pages it had merged in the address range specified by
276 .IR addr
277 and
278 .IR length .
279 .TP
280 .BR MADV_HUGEPAGE " (since Linux 2.6.38)"
281 .\" commit 0af4e98b6b095c74588af04872f83d333c958c32
282 .\" http://lwn.net/Articles/358904/
283 .\" https://lwn.net/Articles/423584/
284 Enable Transparent Huge Pages (THP) for pages in the range specified by
285 .I addr
286 and
287 .IR length .
288 Currently, Transparent Huge Pages work only with private anonymous pages (see
289 .BR mmap (2)).
290 The kernel will regularly scan the areas marked as huge page candidates
291 to replace them with huge pages.
292 The kernel will also allocate huge pages directly when the region is
293 naturally aligned to the huge page size (see
294 .BR posix_memalign (2)).
295
296 This feature is primarily aimed at applications that use large mappings of
297 data and access large regions of that memory at a time (e.g., virtualization
298 systems such as QEMU).
299 It can very easily waste memory (e.g., a 2MB mapping that only ever accesses
300 1 byte will result in 2MB of wired memory instead of one 4KB page).
301 See the Linux kernel source file
302 .I Documentation/vm/transhuge.txt
303 for more details.
304
305 The
306 .BR MADV_HUGEPAGE
307 and
308 .BR MADV_NOHUGEPAGE
309 operations are available only if the kernel was configured with
310 .BR CONFIG_TRANSPARENT_HUGEPAGE .
311 .TP
312 .BR MADV_NOHUGEPAGE " (since Linux 2.6.38)"
313 Ensures that memory in the address range specified by
314 .IR addr
315 and
316 .IR length
317 will not be collapsed into huge pages.
318 .TP
319 .BR MADV_DONTDUMP " (since Linux 3.4)"
320 .\" commit 909af768e88867016f427264ae39d27a57b6a8ed
321 .\" commit accb61fe7bb0f5c2a4102239e4981650f9048519
322 Exclude from a core dump those pages in the range specified by
323 .I addr
324 and
325 .IR length .
326 This is useful in applications that have large areas of memory
327 that are known not to be useful in a core dump.
328 The effect of
329 .BR MADV_DONTDUMP
330 takes precedence over the bit mask that is set via the
331 .I /proc/PID/coredump_filter
332 file (see
333 .BR core (5)).
334 .TP
335 .BR MADV_DODUMP " (since Linux 3.4)"
336 Undo the effect of an earlier
337 .BR MADV_DONTDUMP .
338 .SH RETURN VALUE
339 On success,
340 .BR madvise ()
341 returns zero.
342 On error, it returns \-1 and
343 .I errno
344 is set appropriately.
345 .SH ERRORS
346 .TP
347 .B EACCES
348 .I advice
349 is
350 .BR MADV_REMOVE ,
351 but the specified address range is not a shared writable mapping.
352 .TP
353 .B EAGAIN
354 A kernel resource was temporarily unavailable.
355 .TP
356 .B EBADF
357 The map exists, but the area maps something that isn't a file.
358 .TP
359 .B EINVAL
360 .I addr
361 is not page-aligned or
362 .I length
363 is negative.
364 .\" .I length
365 .\" is zero,
366 .TP
367 .B EINVAL
368 .I advice
369 is not a valid.
370 .TP
371 .B EINVAL
372 .I advice
373 is
374 .B MADV_DONTNEED
375 or
376 .BR MADV_REMOVE
377 and the specified address range includes locked or Huge TLB pages.
378 .TP
379 .B EINVAL
380 .I advice
381 is
382 .BR MADV_MERGEABLE
383 or
384 .BR MADV_UNMERGEABLE ,
385 but the kernel was not configured with
386 .BR CONFIG_KSM .
387 .TP
388 .B EIO
389 (for
390 .BR MADV_WILLNEED )
391 Paging in this area would exceed the process's
392 maximum resident set size.
393 .TP
394 .B ENOMEM
395 (for
396 .BR MADV_WILLNEED )
397 Not enough memory: paging in failed.
398 .TP
399 .B ENOMEM
400 Addresses in the specified range are not currently
401 mapped, or are outside the address space of the process.
402 .TP
403 .B EPERM
404 .I advice
405 is
406 .BR MADV_HWPOISON ,
407 but the caller does not have the
408 .B CAP_SYS_ADMIN
409 capability.
410 .SH VERSIONS
411 Since Linux 3.18,
412 .\" commit d3ac21cacc24790eb45d735769f35753f5b56ceb
413 support for this system call is optional,
414 depending on the setting of the
415 .B CONFIG_ADVISE_SYSCALLS
416 configuration option.
417 .SH CONFORMING TO
418 .BR madvise ()
419 is not specified by any standards.
420 Versions of this system call, implementing a wide variety of
421 .I advice
422 values, exist on many other implementations.
423 Other implementations typically implement at least the flags listed
424 above under
425 .IR "Conventional advice flags" ,
426 albeit with some variation in semantics.
427
428 POSIX.1-2001 describes
429 .BR posix_madvise (3)
430 with constants
431 .BR POSIX_MADV_NORMAL ,
432 .BR POSIX_MADV_RANDOM ,
433 .BR POSIX_MADV_SEQUENTIAL ,
434 .BR POSIX_MADV_WILLNEED ,
435 and
436 .BR POSIX_MADV_DONTNEED ,
437 and so on, with behavior close to the similarly named flags listed above.
438 (POSIX.1-2008 adds a further flag,
439 .BR POSIX_MADV_NOREUSE ,
440 that has no analog in
441 .BR madvise (2).)
442 .SH NOTES
443 .SS Linux notes
444 The Linux implementation requires that the address
445 .I addr
446 be page-aligned, and allows
447 .I length
448 to be zero.
449 If there are some parts of the specified address range
450 that are not mapped, the Linux version of
451 .BR madvise ()
452 ignores them and applies the call to the rest (but returns
453 .B ENOMEM
454 from the system call, as it should).
455 .\" .SH HISTORY
456 .\" The
457 .\" .BR madvise ()
458 .\" function first appeared in 4.4BSD.
459 .SH SEE ALSO
460 .BR getrlimit (2),
461 .BR mincore (2),
462 .BR mmap (2),
463 .BR mprotect (2),
464 .BR msync (2),
465 .BR munmap (2),
466 .BR posix_fadvise (2),
467 .BR prctl (2),
468 .BR core (5)