Debian Codegraph @

upstream/debian-trixie/man2/mremap.2

raw
1 .\" Copyright (c) 1996 Tom Bjorkholm <[email protected]>
2 .\"
3 .\" SPDX-License-Identifier: GPL-2.0-or-later
4 .\"
5 .\" 1996-04-11 Tom Bjorkholm <[email protected]>
6 .\" First version written (1.3.86)
7 .\" 1996-04-12 Tom Bjorkholm <[email protected]>
8 .\" Update for Linux 1.3.87 and later
9 .\" 2005-10-11 mtk: Added NOTES for MREMAP_FIXED; revised EINVAL text.
10 .\"
11 .TH mremap 2 2024-05-02 "Linux man-pages 6.9.1"
12 .SH NAME
13 mremap \- remap a virtual memory address
14 .SH LIBRARY
15 Standard C library
16 .RI ( libc ", " \-lc )
17 .SH SYNOPSIS
18 .nf
19 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
20 .B #include <sys/mman.h>
21 .P
22 .BI "void *mremap(void " old_address [. old_size "], size_t " old_size ,
23 .BI " size_t " new_size ", int " flags ", ... /* void *" new_address " */);"
24 .fi
25 .SH DESCRIPTION
26 .BR mremap ()
27 expands (or shrinks) an existing memory mapping, potentially
28 moving it at the same time (controlled by the \fIflags\fP argument and
29 the available virtual address space).
30 .P
31 \fIold_address\fP is the old address of the virtual memory block that you
32 want to expand (or shrink).
33 Note that \fIold_address\fP has to be page
34 aligned.
35 \fIold_size\fP is the old size of the
36 virtual memory block.
37 \fInew_size\fP is the requested size of the
38 virtual memory block after the resize.
39 An optional fifth argument,
40 .IR new_address ,
41 may be provided; see the description of
42 .B MREMAP_FIXED
43 below.
44 .P
45 If the value of \fIold_size\fP is zero, and \fIold_address\fP refers to
46 a shareable mapping
47 (see the description of
48 .B MAP_SHARED
49 in
50 .BR mmap (2)),
51 then
52 .BR mremap ()
53 will create a new mapping of the same pages.
54 \fInew_size\fP
55 will be the size of the new mapping and the location of the new mapping
56 may be specified with \fInew_address\fP; see the description of
57 .B MREMAP_FIXED
58 below.
59 If a new mapping is requested via this method, then the
60 .B MREMAP_MAYMOVE
61 flag must also be specified.
62 .P
63 The \fIflags\fP bit-mask argument may be 0, or include the following flags:
64 .TP
65 .B MREMAP_MAYMOVE
66 By default, if there is not sufficient space to expand a mapping
67 at its current location, then
68 .BR mremap ()
69 fails.
70 If this flag is specified, then the kernel is permitted to
71 relocate the mapping to a new virtual address, if necessary.
72 If the mapping is relocated,
73 then absolute pointers into the old mapping location
74 become invalid (offsets relative to the starting address of
75 the mapping should be employed).
76 .TP
77 .BR MREMAP_FIXED " (since Linux 2.3.31)"
78 This flag serves a similar purpose to the
79 .B MAP_FIXED
80 flag of
81 .BR mmap (2).
82 If this flag is specified, then
83 .BR mremap ()
84 accepts a fifth argument,
85 .IR "void\ *new_address" ,
86 which specifies a page-aligned address to which the mapping must
87 be moved.
88 Any previous mapping at the address range specified by
89 .I new_address
90 and
91 .I new_size
92 is unmapped.
93 .IP
94 If
95 .B MREMAP_FIXED
96 is specified, then
97 .B MREMAP_MAYMOVE
98 must also be specified.
99 .TP
100 .BR MREMAP_DONTUNMAP " (since Linux 5.7)"
101 .\" commit e346b3813067d4b17383f975f197a9aa28a3b077
102 This flag, which must be used in conjunction with
103 .BR MREMAP_MAYMOVE ,
104 remaps a mapping to a new address but does not unmap the mapping at
105 .IR old_address .
106 .IP
107 The
108 .B MREMAP_DONTUNMAP
109 flag can be used only with private anonymous mappings
110 (see the description of
111 .B MAP_PRIVATE
112 and
113 .B MAP_ANONYMOUS
114 in
115 .BR mmap (2)).
116 .IP
117 After completion,
118 any access to the range specified by
119 .I old_address
120 and
121 .I old_size
122 will result in a page fault.
123 The page fault will be handled by a
124 .BR userfaultfd (2)
125 handler
126 if the address is in a range previously registered with
127 .BR userfaultfd (2).
128 Otherwise, the kernel allocates a zero-filled page to handle the fault.
129 .IP
130 The
131 .B MREMAP_DONTUNMAP
132 flag may be used to atomically move a mapping while leaving the source
133 mapped.
134 See NOTES for some possible applications of
135 .BR MREMAP_DONTUNMAP .
136 .P
137 If the memory segment specified by
138 .I old_address
139 and
140 .I old_size
141 is locked (using
142 .BR mlock (2)
143 or similar), then this lock is maintained when the segment is
144 resized and/or relocated.
145 As a consequence, the amount of memory locked by the process may change.
146 .SH RETURN VALUE
147 On success
148 .BR mremap ()
149 returns a pointer to the new virtual memory area.
150 On error, the value
151 .B MAP_FAILED
152 (that is, \fI(void\ *)\ \-1\fP) is returned,
153 and \fIerrno\fP is set to indicate the error.
154 .SH ERRORS
155 .TP
156 .B EAGAIN
157 The caller tried to expand a memory segment that is locked,
158 but this was not possible without exceeding the
159 .B RLIMIT_MEMLOCK
160 resource limit.
161 .TP
162 .B EFAULT
163 Some address in the range
164 \fIold_address\fP to \fIold_address\fP+\fIold_size\fP is an invalid
165 virtual memory address for this process.
166 You can also get
167 .B EFAULT
168 even if there exist mappings that cover the
169 whole address space requested, but those mappings are of different types.
170 .TP
171 .B EINVAL
172 An invalid argument was given.
173 Possible causes are:
174 .RS
175 .IP \[bu] 3
176 \fIold_address\fP was not
177 page aligned;
178 .IP \[bu]
179 a value other than
180 .B MREMAP_MAYMOVE
181 or
182 .B MREMAP_FIXED
183 or
184 .B MREMAP_DONTUNMAP
185 was specified in
186 .IR flags ;
187 .IP \[bu]
188 .I new_size
189 was zero;
190 .IP \[bu]
191 .I new_size
192 or
193 .I new_address
194 was invalid;
195 .IP \[bu]
196 the new address range specified by
197 .I new_address
198 and
199 .I new_size
200 overlapped the old address range specified by
201 .I old_address
202 and
203 .IR old_size ;
204 .IP \[bu]
205 .B MREMAP_FIXED
206 or
207 .B MREMAP_DONTUNMAP
208 was specified without also specifying
209 .BR MREMAP_MAYMOVE ;
210 .IP \[bu]
211 .B MREMAP_DONTUNMAP
212 was specified, but one or more pages in the range specified by
213 .I old_address
214 and
215 .I old_size
216 were not private anonymous;
217 .IP \[bu]
218 .B MREMAP_DONTUNMAP
219 was specified and
220 .I old_size
221 was not equal to
222 .IR new_size ;
223 .IP \[bu]
224 \fIold_size\fP was zero and \fIold_address\fP does not refer to a
225 shareable mapping (but see BUGS);
226 .IP \[bu]
227 \fIold_size\fP was zero and the
228 .B MREMAP_MAYMOVE
229 flag was not specified.
230 .RE
231 .TP
232 .B ENOMEM
233 Not enough memory was available to complete the operation.
234 Possible causes are:
235 .RS
236 .IP \[bu] 3
237 The memory area cannot be expanded at the current virtual address, and the
238 .B MREMAP_MAYMOVE
239 flag is not set in \fIflags\fP.
240 Or, there is not enough (virtual) memory available.
241 .IP \[bu]
242 .B MREMAP_DONTUNMAP
243 was used causing a new mapping to be created that would exceed the
244 (virtual) memory available.
245 Or, it would exceed the maximum number of allowed mappings.
246 .RE
247 .SH STANDARDS
248 Linux.
249 .SH HISTORY
250 .\" 4.2BSD had a (never actually implemented)
251 .\" .BR mremap (2)
252 .\" call with completely different semantics.
253 .\" .P
254 Prior to glibc 2.4, glibc did not expose the definition of
255 .BR MREMAP_FIXED ,
256 and the prototype for
257 .BR mremap ()
258 did not allow for the
259 .I new_address
260 argument.
261 .SH NOTES
262 .BR mremap ()
263 changes the
264 mapping between virtual addresses and memory pages.
265 This can be used to implement a very efficient
266 .BR realloc (3).
267 .P
268 In Linux, memory is divided into pages.
269 A process has (one or)
270 several linear virtual memory segments.
271 Each virtual memory segment has one
272 or more mappings to real memory pages (in the page table).
273 Each virtual memory segment has its own
274 protection (access rights), which may cause
275 a segmentation violation
276 .RB ( SIGSEGV )
277 if the memory is accessed incorrectly (e.g.,
278 writing to a read-only segment).
279 Accessing virtual memory outside of the
280 segments will also cause a segmentation violation.
281 .P
282 If
283 .BR mremap ()
284 is used to move or expand an area locked with
285 .BR mlock (2)
286 or equivalent, the
287 .BR mremap ()
288 call will make a best effort to populate the new area but will not fail
289 with
290 .B ENOMEM
291 if the area cannot be populated.
292 .\"
293 .SS MREMAP_DONTUNMAP use cases
294 Possible applications for
295 .B MREMAP_DONTUNMAP
296 include:
297 .IP \[bu] 3
298 Non-cooperative
299 .BR userfaultfd (2):
300 an application can yank out a virtual address range using
301 .B MREMAP_DONTUNMAP
302 and then employ a
303 .BR userfaultfd (2)
304 handler to handle the page faults that subsequently occur
305 as other threads in the process touch pages in the yanked range.
306 .IP \[bu]
307 Garbage collection:
308 .B MREMAP_DONTUNMAP
309 can be used in conjunction with
310 .BR userfaultfd (2)
311 to implement garbage collection algorithms (e.g., in a Java virtual machine).
312 Such an implementation can be cheaper (and simpler)
313 than conventional garbage collection techniques that involve
314 marking pages with protection
315 .B PROT_NONE
316 in conjunction with the use of a
317 .B SIGSEGV
318 handler to catch accesses to those pages.
319 .SH BUGS
320 Before Linux 4.14,
321 if
322 .I old_size
323 was zero and the mapping referred to by
324 .I old_address
325 was a private mapping
326 (see the description of
327 .B MAP_PRIVATE
328 in
329 .BR mmap (2)),
330 .BR mremap ()
331 created a new private mapping unrelated to the original mapping.
332 This behavior was unintended
333 and probably unexpected in user-space applications
334 (since the intention of
335 .BR mremap ()
336 is to create a new mapping based on the original mapping).
337 Since Linux 4.14,
338 .\" commit dba58d3b8c5045ad89c1c95d33d01451e3964db7
339 .BR mremap ()
340 fails with the error
341 .B EINVAL
342 in this scenario.
343 .SH SEE ALSO
344 .BR brk (2),
345 .BR getpagesize (2),
346 .BR getrlimit (2),
347 .BR mlock (2),
348 .BR mmap (2),
349 .BR sbrk (2),
350 .BR malloc (3),
351 .BR realloc (3)
352 .P
353 Your favorite text book on operating systems
354 for more information on paged memory
355 (e.g., \fIModern Operating Systems\fP by Andrew S.\& Tanenbaum,
356 \fIInside Linux\fP by Randolph Bentson,
357 \fIThe Design of the UNIX Operating System\fP by Maurice J.\& Bach)