Debian Codegraph @

upstream/fedora-44/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-11-13 "Linux man-pages 6.13"
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 mappings that are not
110 .B VM_DONTEXPAND
111 or
112 .BR VM_MIXEDMAP .
113 Before Linux 5.13, the
114 .B MREMAP_DONTUNMAP
115 flag could be used only with private anonymous mappings
116 (see the description of
117 .B MAP_PRIVATE
118 and
119 .B MAP_ANONYMOUS
120 in
121 .BR mmap (2)).
122 .IP
123 After completion,
124 any access to the range specified by
125 .I old_address
126 and
127 .I old_size
128 will result in a page fault.
129 The page fault will be handled by a
130 .BR userfaultfd (2)
131 handler
132 if the address is in a range previously registered with
133 .BR userfaultfd (2).
134 Otherwise, the kernel allocates a zero-filled page to handle the fault.
135 .IP
136 The
137 .B MREMAP_DONTUNMAP
138 flag may be used to atomically move a mapping while leaving the source
139 mapped.
140 See NOTES for some possible applications of
141 .BR MREMAP_DONTUNMAP .
142 .P
143 If the memory segment specified by
144 .I old_address
145 and
146 .I old_size
147 is locked (using
148 .BR mlock (2)
149 or similar), then this lock is maintained when the segment is
150 resized and/or relocated.
151 As a consequence, the amount of memory locked by the process may change.
152 .SH RETURN VALUE
153 On success
154 .BR mremap ()
155 returns a pointer to the new virtual memory area.
156 On error, the value
157 .B MAP_FAILED
158 (that is, \fI(void\ *)\ \-1\fP) is returned,
159 and \fIerrno\fP is set to indicate the error.
160 .SH ERRORS
161 .TP
162 .B EAGAIN
163 The caller tried to expand a memory segment that is locked,
164 but this was not possible without exceeding the
165 .B RLIMIT_MEMLOCK
166 resource limit.
167 .TP
168 .B EFAULT
169 Some address in the range
170 \fIold_address\fP to \fIold_address\fP+\fIold_size\fP is an invalid
171 virtual memory address for this process.
172 You can also get
173 .B EFAULT
174 even if there exist mappings that cover the
175 whole address space requested, but those mappings are of different types.
176 .TP
177 .B EINVAL
178 An invalid argument was given.
179 Possible causes are:
180 .RS
181 .IP \[bu] 3
182 \fIold_address\fP was not
183 page aligned;
184 .IP \[bu]
185 a value other than
186 .B MREMAP_MAYMOVE
187 or
188 .B MREMAP_FIXED
189 or
190 .B MREMAP_DONTUNMAP
191 was specified in
192 .IR flags ;
193 .IP \[bu]
194 .I new_size
195 was zero;
196 .IP \[bu]
197 .I new_size
198 or
199 .I new_address
200 was invalid;
201 .IP \[bu]
202 the new address range specified by
203 .I new_address
204 and
205 .I new_size
206 overlapped the old address range specified by
207 .I old_address
208 and
209 .IR old_size ;
210 .IP \[bu]
211 .B MREMAP_FIXED
212 or
213 .B MREMAP_DONTUNMAP
214 was specified without also specifying
215 .BR MREMAP_MAYMOVE ;
216 .IP \[bu]
217 .B MREMAP_DONTUNMAP
218 was specified, but one or more pages in the range specified by
219 .I old_address
220 and
221 .I old_size
222 were not private anonymous;
223 .IP \[bu]
224 .B MREMAP_DONTUNMAP
225 was specified and
226 .I old_size
227 was not equal to
228 .IR new_size ;
229 .IP \[bu]
230 \fIold_size\fP was zero and \fIold_address\fP does not refer to a
231 shareable mapping (but see BUGS);
232 .IP \[bu]
233 \fIold_size\fP was zero and the
234 .B MREMAP_MAYMOVE
235 flag was not specified.
236 .RE
237 .TP
238 .B ENOMEM
239 Not enough memory was available to complete the operation.
240 Possible causes are:
241 .RS
242 .IP \[bu] 3
243 The memory area cannot be expanded at the current virtual address, and the
244 .B MREMAP_MAYMOVE
245 flag is not set in \fIflags\fP.
246 Or, there is not enough (virtual) memory available.
247 .IP \[bu]
248 .B MREMAP_DONTUNMAP
249 was used causing a new mapping to be created that would exceed the
250 (virtual) memory available.
251 Or, it would exceed the maximum number of allowed mappings.
252 .RE
253 .SH STANDARDS
254 Linux.
255 .SH HISTORY
256 .\" 4.2BSD had a (never actually implemented)
257 .\" .BR mremap (2)
258 .\" call with completely different semantics.
259 .\" .P
260 Prior to glibc 2.4, glibc did not expose the definition of
261 .BR MREMAP_FIXED ,
262 and the prototype for
263 .BR mremap ()
264 did not allow for the
265 .I new_address
266 argument.
267 .SH NOTES
268 .BR mremap ()
269 changes the
270 mapping between virtual addresses and memory pages.
271 This can be used to implement a very efficient
272 .BR realloc (3).
273 .P
274 In Linux, memory is divided into pages.
275 A process has (one or)
276 several linear virtual memory segments.
277 Each virtual memory segment has one
278 or more mappings to real memory pages (in the page table).
279 Each virtual memory segment has its own
280 protection (access rights), which may cause
281 a segmentation violation
282 .RB ( SIGSEGV )
283 if the memory is accessed incorrectly (e.g.,
284 writing to a read-only segment).
285 Accessing virtual memory outside of the
286 segments will also cause a segmentation violation.
287 .P
288 If
289 .BR mremap ()
290 is used to move or expand an area locked with
291 .BR mlock (2)
292 or equivalent, the
293 .BR mremap ()
294 call will make a best effort to populate the new area but will not fail
295 with
296 .B ENOMEM
297 if the area cannot be populated.
298 .\"
299 .SS MREMAP_DONTUNMAP use cases
300 Possible applications for
301 .B MREMAP_DONTUNMAP
302 include:
303 .IP \[bu] 3
304 Non-cooperative
305 .BR userfaultfd (2):
306 an application can yank out a virtual address range using
307 .B MREMAP_DONTUNMAP
308 and then employ a
309 .BR userfaultfd (2)
310 handler to handle the page faults that subsequently occur
311 as other threads in the process touch pages in the yanked range.
312 .IP \[bu]
313 Garbage collection:
314 .B MREMAP_DONTUNMAP
315 can be used in conjunction with
316 .BR userfaultfd (2)
317 to implement garbage collection algorithms (e.g., in a Java virtual machine).
318 Such an implementation can be cheaper (and simpler)
319 than conventional garbage collection techniques that involve
320 marking pages with protection
321 .B PROT_NONE
322 in conjunction with the use of a
323 .B SIGSEGV
324 handler to catch accesses to those pages.
325 .SH BUGS
326 Before Linux 4.14,
327 if
328 .I old_size
329 was zero and the mapping referred to by
330 .I old_address
331 was a private mapping
332 (see the description of
333 .B MAP_PRIVATE
334 in
335 .BR mmap (2)),
336 .BR mremap ()
337 created a new private mapping unrelated to the original mapping.
338 This behavior was unintended
339 and probably unexpected in user-space applications
340 (since the intention of
341 .BR mremap ()
342 is to create a new mapping based on the original mapping).
343 Since Linux 4.14,
344 .\" commit dba58d3b8c5045ad89c1c95d33d01451e3964db7
345 .BR mremap ()
346 fails with the error
347 .B EINVAL
348 in this scenario.
349 .SH SEE ALSO
350 .BR brk (2),
351 .BR getpagesize (2),
352 .BR getrlimit (2),
353 .BR mlock (2),
354 .BR mmap (2),
355 .BR sbrk (2),
356 .BR malloc (3),
357 .BR realloc (3)
358 .P
359 Your favorite text book on operating systems
360 for more information on paged memory
361 (e.g., \fIModern Operating Systems\fP by Andrew S.\& Tanenbaum,
362 \fIInside Linux\fP by Randolph Bentson,
363 \fIThe Design of the UNIX Operating System\fP by Maurice J.\& Bach)