]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/madvise.2
Various pages: Use .nf/.fi in SYNOPSIS
[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 2020-04-11 "Linux" "Linux Programmer's Manual"
36 .SH NAME
37 madvise \- give advice about use of memory
38 .SH SYNOPSIS
39 .nf
40 .B #include <sys/mman.h>
41 .PP
42 .BI "int madvise(void *" addr ", size_t " length ", int " advice );
43 .fi
44 .PP
45 .RS -4
46 Feature Test Macro Requirements for glibc (see
47 .BR feature_test_macros (7)):
48 .RE
49 .PP
50 .BR madvise ():
51 .PD 0
52 .RS 4
53 .TP 4
54 Since glibc 2.19:
55 _DEFAULT_SOURCE
56 .TP
57 Up to and including glibc 2.19:
58 _BSD_SOURCE
59 .RE
60 .PD
61 .SH DESCRIPTION
62 The
63 .BR madvise ()
64 system call is used to give advice or directions to the kernel
65 about the address range beginning at address
66 .I addr
67 and with size
68 .I length
69 bytes
70 In most cases,
71 the goal of such advice is to improve system or application performance.
72 .PP
73 Initially, the system call supported a set of "conventional"
74 .I advice
75 values, which are also available on several other implementations.
76 (Note, though, that
77 .BR madvise ()
78 is not specified in POSIX.)
79 Subsequently, a number of Linux-specific
80 .IR advice
81 values have been added.
82 .\"
83 .\" ======================================================================
84 .\"
85 .SS Conventional advice values
86 The
87 .I advice
88 values listed below
89 allow an application to tell the kernel how it expects to use
90 some mapped or shared memory areas, so that the kernel can choose
91 appropriate read-ahead and caching techniques.
92 These
93 .I advice
94 values do not influence the semantics of the application
95 (except in the case of
96 .BR MADV_DONTNEED ),
97 but may influence its performance.
98 All of the
99 .I advice
100 values listed here have analogs in the POSIX-specified
101 .BR posix_madvise (3)
102 function, and the values have the same meanings, with the exception of
103 .BR MADV_DONTNEED .
104 .PP
105 The advice is indicated in the
106 .I advice
107 argument, which is one of the following:
108 .TP
109 .B MADV_NORMAL
110 No special treatment.
111 This is the default.
112 .TP
113 .B MADV_RANDOM
114 Expect page references in random order.
115 (Hence, read ahead may be less useful than normally.)
116 .TP
117 .B MADV_SEQUENTIAL
118 Expect page references in sequential order.
119 (Hence, pages in the given range can be aggressively read ahead,
120 and may be freed soon after they are accessed.)
121 .TP
122 .B MADV_WILLNEED
123 Expect access in the near future.
124 (Hence, it might be a good idea to read some pages ahead.)
125 .TP
126 .B MADV_DONTNEED
127 Do not expect access in the near future.
128 (For the time being, the application is finished with the given range,
129 so the kernel can free resources associated with it.)
130 .IP
131 After a successful
132 .B MADV_DONTNEED
133 operation,
134 the semantics of memory access in the specified region are changed:
135 subsequent accesses of pages in the range will succeed, but will result
136 in either repopulating the memory contents from the
137 up-to-date contents of the underlying mapped file
138 (for shared file mappings, shared anonymous mappings,
139 and shmem-based techniques such as System V shared memory segments)
140 or zero-fill-on-demand pages for anonymous private mappings.
141 .IP
142 Note that, when applied to shared mappings,
143 .BR MADV_DONTNEED
144 might not lead to immediate freeing of the pages in the range.
145 The kernel is free to delay freeing the pages until an appropriate moment.
146 The resident set size (RSS) of the calling process will be immediately
147 reduced however.
148 .IP
149 .B MADV_DONTNEED
150 cannot be applied to locked pages, Huge TLB pages, or
151 .BR VM_PFNMAP
152 pages.
153 (Pages marked with the kernel-internal
154 .B VM_PFNMAP
155 .\" http://lwn.net/Articles/162860/
156 flag are special memory areas that are not managed
157 by the virtual memory subsystem.
158 Such pages are typically created by device drivers that
159 map the pages into user space.)
160 .\"
161 .\" ======================================================================
162 .\"
163 .SS Linux-specific advice values
164 The following Linux-specific
165 .I advice
166 values have no counterparts in the POSIX-specified
167 .BR posix_madvise (3),
168 and may or may not have counterparts in the
169 .BR madvise ()
170 interface available on other implementations.
171 Note that some of these operations change the semantics of memory accesses.
172 .TP
173 .BR MADV_REMOVE " (since Linux 2.6.16)"
174 .\" commit f6b3ec238d12c8cc6cc71490c6e3127988460349
175 Free up a given range of pages
176 and its associated backing store.
177 This is equivalent to punching a hole in the corresponding byte
178 range of the backing store (see
179 .BR fallocate (2)).
180 Subsequent accesses in the specified address range will see
181 bytes containing zero.
182 .\" Databases want to use this feature to drop a section of their
183 .\" bufferpool (shared memory segments) - without writing back to
184 .\" disk/swap space. This feature is also useful for supporting
185 .\" hot-plug memory on UML.
186 .IP
187 The specified address range must be mapped shared and writable.
188 This flag cannot be applied to locked pages, Huge TLB pages, or
189 .BR VM_PFNMAP
190 pages.
191 .IP
192 In the initial implementation, only
193 .BR tmpfs (5)
194 was supported
195 .BR MADV_REMOVE ;
196 but since Linux 3.5,
197 .\" commit 3f31d07571eeea18a7d34db9af21d2285b807a17
198 any filesystem which supports the
199 .BR fallocate (2)
200 .BR FALLOC_FL_PUNCH_HOLE
201 mode also supports
202 .BR MADV_REMOVE .
203 Hugetlbfs fails with the error
204 .BR EINVAL
205 and other filesystems fail with the error
206 .BR EOPNOTSUPP .
207 .TP
208 .BR MADV_DONTFORK " (since Linux 2.6.16)"
209 .\" commit f822566165dd46ff5de9bf895cfa6c51f53bb0c4
210 .\" See http://lwn.net/Articles/171941/
211 Do not make the pages in this range available to the child after a
212 .BR fork (2).
213 This is useful to prevent copy-on-write semantics from changing
214 the physical location of a page if the parent writes to it after a
215 .BR fork (2).
216 (Such page relocations cause problems for hardware that
217 DMAs into the page.)
218 .\" [PATCH] madvise MADV_DONTFORK/MADV_DOFORK
219 .\" Currently, copy-on-write may change the physical address of
220 .\" a page even if the user requested that the page is pinned in
221 .\" memory (either by mlock or by get_user_pages). This happens
222 .\" if the process forks meanwhile, and the parent writes to that
223 .\" page. As a result, the page is orphaned: in case of
224 .\" get_user_pages, the application will never see any data hardware
225 .\" DMA's into this page after the COW. In case of mlock'd memory,
226 .\" the parent is not getting the realtime/security benefits of mlock.
227 .\"
228 .\" In particular, this affects the Infiniband modules which do DMA from
229 .\" and into user pages all the time.
230 .\"
231 .\" This patch adds madvise options to control whether memory range is
232 .\" inherited across fork. Useful e.g. for when hardware is doing DMA
233 .\" from/into these pages. Could also be useful to an application
234 .\" wanting to speed up its forks by cutting large areas out of
235 .\" consideration.
236 .\"
237 .\" SEE ALSO: http://lwn.net/Articles/171941/
238 .\" "Tweaks to madvise() and posix_fadvise()", 14 Feb 2006
239 .TP
240 .BR MADV_DOFORK " (since Linux 2.6.16)"
241 Undo the effect of
242 .BR MADV_DONTFORK ,
243 restoring the default behavior, whereby a mapping is inherited across
244 .BR fork (2).
245 .TP
246 .BR MADV_HWPOISON " (since Linux 2.6.32)"
247 .\" commit 9893e49d64a4874ea67849ee2cfbf3f3d6817573
248 Poison the pages in the range specified by
249 .I addr
250 and
251 .IR length
252 and handle subsequent references to those pages
253 like a hardware memory corruption.
254 This operation is available only for privileged
255 .RB ( CAP_SYS_ADMIN )
256 processes.
257 This operation may result in the calling process receiving a
258 .B SIGBUS
259 and the page being unmapped.
260 .IP
261 This feature is intended for testing of memory error-handling code;
262 it is available only if the kernel was configured with
263 .BR CONFIG_MEMORY_FAILURE .
264 .TP
265 .BR MADV_MERGEABLE " (since Linux 2.6.32)"
266 .\" commit f8af4da3b4c14e7267c4ffb952079af3912c51c5
267 Enable Kernel Samepage Merging (KSM) for the pages in the range specified by
268 .I addr
269 and
270 .IR length .
271 The kernel regularly scans those areas of user memory that have
272 been marked as mergeable,
273 looking for pages with identical content.
274 These are replaced by a single write-protected page (which is automatically
275 copied if a process later wants to update the content of the page).
276 KSM merges only private anonymous pages (see
277 .BR mmap (2)).
278 .IP
279 The KSM feature is intended for applications that generate many
280 instances of the same data (e.g., virtualization systems such as KVM).
281 It can consume a lot of processing power; use with care.
282 See the Linux kernel source file
283 .I Documentation/admin-guide/mm/ksm.rst
284 for more details.
285 .IP
286 The
287 .BR MADV_MERGEABLE
288 and
289 .BR MADV_UNMERGEABLE
290 operations are available only if the kernel was configured with
291 .BR CONFIG_KSM .
292 .TP
293 .BR MADV_UNMERGEABLE " (since Linux 2.6.32)"
294 Undo the effect of an earlier
295 .BR MADV_MERGEABLE
296 operation on the specified address range;
297 KSM unmerges whatever pages it had merged in the address range specified by
298 .IR addr
299 and
300 .IR length .
301 .TP
302 .BR MADV_SOFT_OFFLINE " (since Linux 2.6.33)"
303 .\" commit afcf938ee0aac4ef95b1a23bac704c6fbeb26de6
304 Soft offline the pages in the range specified by
305 .I addr
306 and
307 .IR length .
308 The memory of each page in the specified range is preserved
309 (i.e., when next accessed, the same content will be visible,
310 but in a new physical page frame),
311 and the original page is offlined
312 (i.e., no longer used, and taken out of normal memory management).
313 The effect of the
314 .B MADV_SOFT_OFFLINE
315 operation is invisible to (i.e., does not change the semantics of)
316 the calling process.
317 .IP
318 This feature is intended for testing of memory error-handling code;
319 it is available only if the kernel was configured with
320 .BR CONFIG_MEMORY_FAILURE .
321 .TP
322 .BR MADV_HUGEPAGE " (since Linux 2.6.38)"
323 .\" commit 0af4e98b6b095c74588af04872f83d333c958c32
324 .\" http://lwn.net/Articles/358904/
325 .\" https://lwn.net/Articles/423584/
326 Enable Transparent Huge Pages (THP) for pages in the range specified by
327 .I addr
328 and
329 .IR length .
330 Currently, Transparent Huge Pages work only with private anonymous pages (see
331 .BR mmap (2)).
332 The kernel will regularly scan the areas marked as huge page candidates
333 to replace them with huge pages.
334 The kernel will also allocate huge pages directly when the region is
335 naturally aligned to the huge page size (see
336 .BR posix_memalign (2)).
337 .IP
338 This feature is primarily aimed at applications that use large mappings of
339 data and access large regions of that memory at a time (e.g., virtualization
340 systems such as QEMU).
341 It can very easily waste memory (e.g., a 2\ MB mapping that only ever accesses
342 1 byte will result in 2\ MB of wired memory instead of one 4\ KB page).
343 See the Linux kernel source file
344 .I Documentation/admin-guide/mm/transhuge.rst
345 for more details.
346 .IP
347 Most common kernels configurations provide
348 .BR MADV_HUGEPAGE -style
349 behavior by default, and thus
350 .BR MADV_HUGEPAGE
351 is normally not necessary.
352 It is mostly intended for embedded systems, where
353 .BR MADV_HUGEPAGE -style
354 behavior may not be enabled by default in the kernel.
355 On such systems,
356 this flag can be used in order to selectively enable THP.
357 Whenever
358 .BR MADV_HUGEPAGE
359 is used, it should always be in regions of memory with
360 an access pattern that the developer knows in advance won't risk
361 to increase the memory footprint of the application when transparent
362 hugepages are enabled.
363 .IP
364 The
365 .BR MADV_HUGEPAGE
366 and
367 .BR MADV_NOHUGEPAGE
368 operations are available only if the kernel was configured with
369 .BR CONFIG_TRANSPARENT_HUGEPAGE .
370 .TP
371 .BR MADV_NOHUGEPAGE " (since Linux 2.6.38)"
372 Ensures that memory in the address range specified by
373 .IR addr
374 and
375 .IR length
376 will not be backed by transparent hugepages.
377 .TP
378 .BR MADV_DONTDUMP " (since Linux 3.4)"
379 .\" commit 909af768e88867016f427264ae39d27a57b6a8ed
380 .\" commit accb61fe7bb0f5c2a4102239e4981650f9048519
381 Exclude from a core dump those pages in the range specified by
382 .I addr
383 and
384 .IR length .
385 This is useful in applications that have large areas of memory
386 that are known not to be useful in a core dump.
387 The effect of
388 .BR MADV_DONTDUMP
389 takes precedence over the bit mask that is set via the
390 .I /proc/[pid]/coredump_filter
391 file (see
392 .BR core (5)).
393 .TP
394 .BR MADV_DODUMP " (since Linux 3.4)"
395 Undo the effect of an earlier
396 .BR MADV_DONTDUMP .
397 .TP
398 .BR MADV_FREE " (since Linux 4.5)"
399 The application no longer requires the pages in the range specified by
400 .IR addr
401 and
402 .IR len .
403 The kernel can thus free these pages,
404 but the freeing could be delayed until memory pressure occurs.
405 For each of the pages that has been marked to be freed
406 but has not yet been freed,
407 the free operation will be canceled if the caller writes into the page.
408 After a successful
409 .B MADV_FREE
410 operation, any stale data (i.e., dirty, unwritten pages) will be lost
411 when the kernel frees the pages.
412 However, subsequent writes to pages in the range will succeed
413 and then kernel cannot free those dirtied pages,
414 so that the caller can always see just written data.
415 If there is no subsequent write,
416 the kernel can free the pages at any time.
417 Once pages in the range have been freed, the caller will
418 see zero-fill-on-demand pages upon subsequent page references.
419 .IP
420 The
421 .B MADV_FREE
422 operation
423 can be applied only to private anonymous pages (see
424 .BR mmap (2)).
425 In Linux before version 4.12,
426 .\" commit 93e06c7a645343d222c9a838834a51042eebbbf7
427 when freeing pages on a swapless system,
428 the pages in the given range are freed instantly,
429 regardless of memory pressure.
430 .TP
431 .BR MADV_WIPEONFORK " (since Linux 4.14)"
432 .\" commit d2cd9ede6e193dd7d88b6d27399e96229a551b19
433 Present the child process with zero-filled memory in this range after a
434 .BR fork (2).
435 This is useful in forking servers in order to ensure
436 that sensitive per-process data
437 (for example, PRNG seeds, cryptographic secrets, and so on)
438 is not handed to child processes.
439 .IP
440 The
441 .B MADV_WIPEONFORK
442 operation can be applied only to private anonymous pages (see
443 .BR mmap (2)).
444 .IP
445 Within the child created by
446 .BR fork (2),
447 the
448 .B MADV_WIPEONFORK
449 setting remains in place on the specified address range.
450 This setting is cleared during
451 .BR execve (2).
452 .TP
453 .BR MADV_KEEPONFORK " (since Linux 4.14)"
454 .\" commit d2cd9ede6e193dd7d88b6d27399e96229a551b19
455 Undo the effect of an earlier
456 .BR MADV_WIPEONFORK .
457 .SH RETURN VALUE
458 On success,
459 .BR madvise ()
460 returns zero.
461 On error, it returns \-1 and
462 .I errno
463 is set appropriately.
464 .SH ERRORS
465 .TP
466 .B EACCES
467 .I advice
468 is
469 .BR MADV_REMOVE ,
470 but the specified address range is not a shared writable mapping.
471 .TP
472 .B EAGAIN
473 A kernel resource was temporarily unavailable.
474 .TP
475 .B EBADF
476 The map exists, but the area maps something that isn't a file.
477 .TP
478 .B EINVAL
479 .I addr
480 is not page-aligned or
481 .I length
482 is negative.
483 .\" .I length
484 .\" is zero,
485 .TP
486 .B EINVAL
487 .I advice
488 is not a valid.
489 .TP
490 .B EINVAL
491 .I advice
492 is
493 .B MADV_DONTNEED
494 or
495 .BR MADV_REMOVE
496 and the specified address range includes locked, Huge TLB pages, or
497 .B VM_PFNMAP
498 pages.
499 .TP
500 .B EINVAL
501 .I advice
502 is
503 .BR MADV_MERGEABLE
504 or
505 .BR MADV_UNMERGEABLE ,
506 but the kernel was not configured with
507 .BR CONFIG_KSM .
508 .TP
509 .B EINVAL
510 .I advice
511 is
512 .BR MADV_FREE
513 or
514 .BR MADV_WIPEONFORK
515 but the specified address range includes file, Huge TLB,
516 .BR MAP_SHARED ,
517 or
518 .BR VM_PFNMAP
519 ranges.
520 .TP
521 .B EIO
522 (for
523 .BR MADV_WILLNEED )
524 Paging in this area would exceed the process's
525 maximum resident set size.
526 .TP
527 .B ENOMEM
528 (for
529 .BR MADV_WILLNEED )
530 Not enough memory: paging in failed.
531 .TP
532 .B ENOMEM
533 Addresses in the specified range are not currently
534 mapped, or are outside the address space of the process.
535 .TP
536 .B EPERM
537 .I advice
538 is
539 .BR MADV_HWPOISON ,
540 but the caller does not have the
541 .B CAP_SYS_ADMIN
542 capability.
543 .SH VERSIONS
544 Since Linux 3.18,
545 .\" commit d3ac21cacc24790eb45d735769f35753f5b56ceb
546 support for this system call is optional,
547 depending on the setting of the
548 .B CONFIG_ADVISE_SYSCALLS
549 configuration option.
550 .SH CONFORMING TO
551 .BR madvise ()
552 is not specified by any standards.
553 Versions of this system call, implementing a wide variety of
554 .I advice
555 values, exist on many other implementations.
556 Other implementations typically implement at least the flags listed
557 above under
558 .IR "Conventional advice flags" ,
559 albeit with some variation in semantics.
560 .PP
561 POSIX.1-2001 describes
562 .BR posix_madvise (3)
563 with constants
564 .BR POSIX_MADV_NORMAL ,
565 .BR POSIX_MADV_RANDOM ,
566 .BR POSIX_MADV_SEQUENTIAL ,
567 .BR POSIX_MADV_WILLNEED ,
568 and
569 .BR POSIX_MADV_DONTNEED ,
570 and so on, with behavior close to the similarly named flags listed above.
571 .SH NOTES
572 .SS Linux notes
573 The Linux implementation requires that the address
574 .I addr
575 be page-aligned, and allows
576 .I length
577 to be zero.
578 If there are some parts of the specified address range
579 that are not mapped, the Linux version of
580 .BR madvise ()
581 ignores them and applies the call to the rest (but returns
582 .B ENOMEM
583 from the system call, as it should).
584 .\" .SH HISTORY
585 .\" The
586 .\" .BR madvise ()
587 .\" function first appeared in 4.4BSD.
588 .SH SEE ALSO
589 .BR getrlimit (2),
590 .BR mincore (2),
591 .BR mmap (2),
592 .BR mprotect (2),
593 .BR msync (2),
594 .BR munmap (2),
595 .BR prctl (2),
596 .BR posix_madvise (3),
597 .BR core (5)