1MADVISE(2)                 Linux Programmer's Manual                MADVISE(2)
2
3
4

NAME

6       madvise - give advice about use of memory
7

SYNOPSIS

9       #include <sys/mman.h>
10
11       int madvise(void *addr, size_t length, int advice);
12
13   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15       madvise(): _BSD_SOURCE
16

DESCRIPTION

18       The madvise() system call advises the kernel about how to handle paging
19       input/output in the address range beginning at address  addr  and  with
20       size  length bytes.  It allows an application to tell the kernel how it
21       expects to use some mapped or shared memory areas, so that  the  kernel
22       can  choose  appropriate  read-ahead and caching techniques.  This call
23       does not influence the semantics of the application (except in the case
24       of  MADV_DONTNEED),  but  may influence its performance.  The kernel is
25       free to ignore the advice.
26
27       The advice is indicated in the advice argument which can be
28
29       MADV_NORMAL
30              No special treatment.  This is the default.
31
32       MADV_RANDOM
33              Expect page references in random order.  (Hence, read ahead  may
34              be less useful than normally.)
35
36       MADV_SEQUENTIAL
37              Expect  page  references  in sequential order.  (Hence, pages in
38              the given range can be aggressively read ahead, and may be freed
39              soon after they are accessed.)
40
41       MADV_WILLNEED
42              Expect  access  in  the near future.  (Hence, it might be a good
43              idea to read some pages ahead.)
44
45       MADV_DONTNEED
46              Do not expect access in the near future.  (For the  time  being,
47              the  application is finished with the given range, so the kernel
48              can free resources associated with it.)  Subsequent accesses  of
49              pages  in  this  range  will  succeed, but will result either in
50              reloading of the memory contents from the underlying mapped file
51              (see  mmap(2)) or zero-fill-on-demand pages for mappings without
52              an underlying file.
53
54       MADV_REMOVE (Since Linux 2.6.16)
55              Free up a given range of pages and its associated backing store.
56              Currently,  only  shmfs/tmpfs  supports this; other file systems
57              return with the error ENOSYS.
58
59       MADV_DONTFORK (Since Linux 2.6.16)
60              Do not make the pages in this range available to the child after
61              a  fork(2).   This  is useful to prevent copy-on-write semantics
62              from changing the physical location of a page(s) if  the  parent
63              writes  to  it  after  a  fork(2).  (Such page relocations cause
64              problems for hardware that DMAs into the page(s).)
65
66       MADV_DOFORK (Since Linux 2.6.16)
67              Undo the effect of MADV_DONTFORK, restoring the  default  behav‐
68              ior, whereby a mapping is inherited across fork(2).
69
70       MADV_HWPOISON (Since Linux 2.6.32)
71              Poison  a  page and handle it like a hardware memory corruption.
72              This operation is available only for privileged  (CAP_SYS_ADMIN)
73              processes.   This  operation  may  result in the calling process
74              receiving a SIGBUS and the page being unmapped.  This feature is
75              intended for testing of memory error-handling code; it is avail‐
76              able only if the kernel was configured with  CONFIG_MEMORY_FAIL‐
77              URE.
78
79       MADV_SOFT_OFFLINE (Since Linux 2.6.33)
80              Soft  offline  the  pages  in  the  range  specified by addr and
81              length.  The memory of each page in the specified range is  pre‐
82              served (i.e., when next accessed, the same content will be visi‐
83              ble, but in a new physical page frame), and the original page is
84              offlined  (i.e.,  no longer used, and taken out of normal memory
85              management).  The effect of the MADV_SOFT_OFFLINE  operation  is
86              invisible  to (i.e., does not change the semantics of) the call‐
87              ing process.  This feature is intended  for  testing  of  memory
88              error-handling code; it is available only if the kernel was con‐
89              figured with CONFIG_MEMORY_FAILURE.
90
91       MADV_MERGEABLE (since Linux 2.6.32)
92              Enable Kernel Samepage Merging (KSM) for the pages in the  range
93              specified  by addr and length.  The kernel regularly scans those
94              areas of user memory that have been marked as mergeable, looking
95              for  pages with identical content.  These are replaced by a sin‐
96              gle write-protected page (which is  automatically  copied  if  a
97              process  later  wants  to  update the content of the page).  KSM
98              merges only private anonymous pages (see mmap(2)).  The KSM fea‐
99              ture  is  intended for applications that generate many instances
100              of the same data (e.g., virtualization systems such as KVM).  It
101              can  consume  a lot of processing power; use with care.  See the
102              Linux  kernel  source  file  Documentation/vm/ksm.txt  for  more
103              details.  The MADV_MERGEABLE and MADV_UNMERGEABLE operations are
104              available only if the kernel was configured with CONFIG_KSM.
105
106       MADV_UNMERGEABLE (since Linux 2.6.32)
107              Undo the effect of an earlier MADV_MERGEABLE  operation  on  the
108              specified  address  range;  KSM  unmerges  whatever pages it had
109              merged in the address range specified by addr and length.
110
111       MADV_HUGEPAGE (since Linux 2.6.38)
112              Enables Transparent Huge Pages (THP)  for  pages  in  the  range
113              specified by addr and length.  Currently, Transparent Huge Pages
114              work only with private anonymous pages (see mmap(2)).  The  ker‐
115              nel will regularly scan the areas marked as huge page candidates
116              to replace them with huge pages.  The kernel will also  allocate
117              huge  pages directly when the region is naturally aligned to the
118              huge page size (see posix_memalign(2)).  This feature is primar‐
119              ily  aimed  at  applications that use large mappings of data and
120              access large regions of that memory at a time (e.g., virtualiza‐
121              tion  systems  such  as  QEMU).  It can very easily waste memory
122              (e.g., a 2MB mapping that only ever accesses 1 byte will  result
123              in  2MB of wired memory instead of one 4KB page).  See the Linux
124              kernel  source  file  Documentation/vm/transhuge.txt  for   more
125              details.   The  MADV_HUGEPAGE and MADV_NOHUGEPAGE operations are
126              available only if the kernel was configured  with  CONFIG_TRANS‐
127              PARENT_HUGEPAGE.
128
129       MADV_NOHUGEPAGE (since Linux 2.6.38)
130              Ensures  that  memory in the address range specified by addr and
131              length will not be collapsed into huge pages.
132
133       MADV_DONTDUMP (since Linux 3.4)
134              Exclude from a core dump those pages in the range  specified  by
135              addr and length.  This is useful in applications that have large
136              areas of memory that are known not to be useful in a core  dump.
137              The  effect  of MADV_DONTDUMP takes precedence over the bit mask
138              that  is  set  via  the  /proc/PID/coredump_filter   file   (see
139              core(5)).
140
141       MADV_DODUMP (since Linux 3.4)
142              Undo the effect of an earlier MADV_DONTDUMP.
143

RETURN VALUE

145       On  success  madvise() returns zero.  On error, it returns -1 and errno
146       is set appropriately.
147

ERRORS

149       EAGAIN A kernel resource was temporarily unavailable.
150
151       EBADF  The map exists, but the area maps something that isn't a file.
152
153       EINVAL This error can occur for the following reasons:
154
155              *  The value len is negative.
156
157              *  addr is not page-aligned.
158
159              *  advice is not a valid value
160
161              *  The application is attempting to  release  locked  or  shared
162                 pages (with MADV_DONTNEED).
163
164              *  MADV_MERGEABLE  or  MADV_UNMERGEABLE was specified in advice,
165                 but the kernel was not configured with CONFIG_KSM.
166
167       EIO    (for  MADV_WILLNEED)  Paging  in  this  area  would  exceed  the
168              process's maximum resident set size.
169
170       ENOMEM (for MADV_WILLNEED) Not enough memory: paging in failed.
171
172       ENOMEM Addresses  in  the  specified range are not currently mapped, or
173              are outside the address space of the process.
174

CONFORMING TO

176       POSIX.1b.   POSIX.1-2001  describes  posix_madvise(3)  with   constants
177       POSIX_MADV_NORMAL,  etc., with a behavior close to that described here.
178       There is a similar posix_fadvise(2) for file access.
179
180       MADV_REMOVE, MADV_DONTFORK, MADV_DOFORK, MADV_HWPOISON, MADV_MERGEABLE,
181       and MADV_UNMERGEABLE are Linux-specific.
182

NOTES

184   Linux notes
185       The current Linux implementation (2.4.0) views this system call more as
186       a command than as advice and hence may return an error when  it  cannot
187       do  what  it  usually  would  do  in response to this advice.  (See the
188       ERRORS description above.)  This is nonstandard behavior.
189
190       The Linux implementation  requires  that  the  address  addr  be  page-
191       aligned,  and allows length to be zero.  If there are some parts of the
192       specified address range that are not mapped, the Linux version of  mad‐
193       vise()  ignores  them  and  applies  the  call to the rest (but returns
194       ENOMEM from the system call, as it should).
195

SEE ALSO

197       getrlimit(2), mincore(2), mmap(2),  mprotect(2),  msync(2),  munmap(2),
198       core(5)
199

COLOPHON

201       This  page  is  part of release 3.53 of the Linux man-pages project.  A
202       description of the project, and information about reporting  bugs,  can
203       be found at http://www.kernel.org/doc/man-pages/.
204
205
206
207Linux                             2012-04-28                        MADVISE(2)
Impressum