]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/madvise.2
grfix
[thirdparty/man-pages.git] / man2 / madvise.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (C) 2001 David Gómez <davidge@jazzfree.com>
4 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
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 .\"
29 .TH MADVISE 2 2007-07-26 "Linux" "Linux Programmer's Manual"
30 .SH NAME
31 madvise \- give advice about use of memory
32 .SH SYNOPSIS
33 .br
34 .B #include <sys/mman.h>
35 .sp
36 .BI "int madvise(void *" start ", size_t " length ", int " advice );
37 .sp
38 .in -4n
39 Feature Test Macro Requirements for glibc (see
40 .BR feature_test_macros (7)):
41 .in
42 .sp
43 .BR madvise ():
44 _BSD_SOURCE
45 .SH DESCRIPTION
46 The
47 .BR madvise ()
48 system call advises the kernel about how to handle paging input/output in
49 the address range beginning at address
50 .I start
51 and with size
52 .I length
53 bytes.
54 It allows an application to tell the kernel how it expects to use
55 some mapped or shared memory areas, so that the kernel can choose
56 appropriate read-ahead and caching techniques.
57 This call does not influence the semantics of the application
58 (except in the case of
59 .BR MADV_DONTNEED ),
60 but
61 may influence its performance.
62 The kernel is free to ignore the advice.
63 .LP
64 The advice is indicated in the
65 .I advice
66 parameter which can be
67 .TP
68 .B MADV_NORMAL
69 No special treatment.
70 This is the default.
71 .TP
72 .B MADV_RANDOM
73 Expect page references in random order.
74 (Hence, read ahead may be less useful than normally.)
75 .TP
76 .B MADV_SEQUENTIAL
77 Expect page references in sequential order.
78 (Hence, pages in the given range can be aggressively read ahead,
79 and may be freed soon after they are accessed.)
80 .TP
81 .B MADV_WILLNEED
82 Expect access in the near future.
83 (Hence, it might be a good idea to read some pages ahead.)
84 .TP
85 .B MADV_DONTNEED
86 Do not expect access in the near future.
87 (For the time being, the application is finished with the given range,
88 so the kernel can free resources associated with it.)
89 Subsequent accesses of pages in this range will succeed, but will result
90 either in re-loading of the memory contents from the underlying mapped file
91 (see
92 .BR mmap (2))
93 or zero-fill-on-demand pages for mappings
94 without an underlying file.
95 .TP
96 .BR MADV_REMOVE " (Since Linux 2.6.16)"
97 Free up a given range of pages
98 and its associated backing store.
99 Currently,
100 .\" 2.6.18-rc5
101 only shmfs/tmpfs supports this; other filesystems return with the
102 error
103 .BR ENOSYS .
104 .\" Databases want to use this feature to drop a section of their
105 .\" bufferpool (shared memory segments) - without writing back to
106 .\" disk/swap space. This feature is also useful for supporting
107 .\" hot-plug memory on UML.
108 .TP
109 .BR MADV_DONTFORK " (Since Linux 2.6.16)"
110 .\" See http://lwn.net/Articles/171941/
111 Do not make the pages in this range available to the child after a
112 .BR fork (2).
113 This is useful to prevent copy-on-write semantics from changing
114 the physical location of a page(s) if the parent writes to it after a
115 .BR fork (2).
116 (Such page relocations cause problems for hardware that
117 DMAs into the page(s).)
118 .\" [PATCH] madvise MADV_DONTFORK/MADV_DOFORK
119 .\" Currently, copy-on-write may change the physical address of
120 .\" a page even if the user requested that the page is pinned in
121 .\" memory (either by mlock or by get_user_pages). This happens
122 .\" if the process forks meanwhile, and the parent writes to that
123 .\" page. As a result, the page is orphaned: in case of
124 .\" get_user_pages, the application will never see any data hardware
125 .\" DMA's into this page after the COW. In case of mlock'd memory,
126 .\" the parent is not getting the realtime/security benefits of mlock.
127 .\"
128 .\" In particular, this affects the Infiniband modules which do DMA from
129 .\" and into user pages all the time.
130 .\"
131 .\" This patch adds madvise options to control whether memory range is
132 .\" inherited across fork. Useful e.g. for when hardware is doing DMA
133 .\" from/into these pages. Could also be useful to an application
134 .\" wanting to speed up its forks by cutting large areas out of
135 .\" consideration.
136 .TP
137 .BR MADV_DOFORK " (Since Linux 2.6.16)"
138 Undo the effect of
139 .BR MADV_DONTFORK ,
140 restoring the default behavior, whereby a mapping is inherited across
141 .BR fork (2).
142 .SH "RETURN VALUE"
143 On success
144 .BR madvise ()
145 returns zero.
146 On error, it returns \-1 and
147 .I errno
148 is set appropriately.
149 .SH ERRORS
150 .TP
151 .B EAGAIN
152 A kernel resource was temporarily unavailable.
153 .TP
154 .B EBADF
155 The map exists, but the area maps something that isn't a file.
156 .TP
157 .B EINVAL
158 The value
159 .I len
160 is negative,
161 .\" .I len
162 .\" is zero,
163 .I start
164 is not page-aligned,
165 .I advice
166 is not a valid value, or the application is attempting
167 to release locked or shared pages (with
168 .BR MADV_DONTNEED ).
169 .TP
170 .B EIO
171 (for
172 .BR MADV_WILLNEED )
173 Paging in this area would exceed the process's
174 maximum resident set size.
175 .TP
176 .B ENOMEM
177 (for
178 .BR MADV_WILLNEED )
179 Not enough memory: paging in failed.
180 .TP
181 .B ENOMEM
182 Addresses in the specified range are not currently
183 mapped, or are outside the address space of the process.
184 .SH "CONFORMING TO"
185 POSIX.1b.
186 POSIX.1-2001 describes
187 .BR posix_madvise (3)
188 with constants
189 .BR POSIX_MADV_NORMAL ,
190 etc.,
191 with a behavior close to that described here.
192 There is a similar
193 .BR posix_fadvise (2)
194 for file access.
195
196 .BR MADV_REMOVE ,
197 .BR MADV_DONTFORK ,
198 and
199 .B MADV_DOFORK
200 are Linux-specific.
201 .SH NOTES
202 .SS "Linux Notes"
203 .LP
204 The current Linux implementation (2.4.0) views this system call
205 more as a command than as advice and hence may return an error
206 when it cannot do what it usually would do in response to this
207 advice.
208 (See the ERRORS description above.)
209 This is nonstandard behavior.
210 .LP
211 The Linux implementation requires that the address
212 .I start
213 be page-aligned, and allows
214 .I length
215 to be zero.
216 If there are some parts of the specified address range
217 that are not mapped, the Linux version of
218 .BR madvise ()
219 ignores them and applies the call to the rest (but returns
220 .B ENOMEM
221 from the system call, as it should).
222 .\" .SH HISTORY
223 .\" The
224 .\" .BR madvise ()
225 .\" function first appeared in 4.4BSD.
226 .SH "SEE ALSO"
227 .BR getrlimit (2),
228 .BR mincore (2),
229 .BR mmap (2),
230 .BR mprotect (2),
231 .BR msync (2),
232 .BR munmap (2)