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