]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/mmap.2
mmap.2: Minor rewording of MAP_FIXED text
[thirdparty/man-pages.git] / man2 / mmap.2
CommitLineData
a62f5121 1.\" Copyright (C) 1996 Andries Brouwer <aeb@cwi.nl>
c11b1abf 2.\" and Copyright (C) 2006, 2007 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 13.\"
fea681da
MK
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.
c13182ef 21.\"
fea681da
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
fea681da
MK
25.\"
26.\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
27.\" Modified 2000-03-25 by Jim Van Zandt <jrv@vanzandt.mv.com>
28.\" Modified 2001-10-04 by John Levon <moz@compsoc.man.ac.uk>
29.\" Modified 2003-02-02 by Andi Kleen <ak@muc.de>
c11b1abf 30.\" Modified 2003-05-21 by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 31.\" MAP_LOCKED works from 2.5.37
c11b1abf 32.\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 33.\" Modified 2004-09-11 by aeb
1a956089
MK
34.\" Modified 2004-12-08, from Eric Estievenart <eric.estievenart@free.fr>
35.\" Modified 2004-12-08, mtk, formatting tidy-ups
a62f5121 36.\" Modified 2006-12-04, mtk, various parts rewritten
74fa61b7 37.\" 2007-07-10, mtk, Added an example program.
e6205b0c 38.\" 2008-11-18, mtk, document MAP_STACK
fea681da 39.\"
30c8dbeb 40.TH MMAP 2 2017-12-18 "Linux" "Linux Programmer's Manual"
fea681da
MK
41.SH NAME
42mmap, munmap \- map or unmap files or devices into memory
43.SH SYNOPSIS
e0037472 44.nf
fea681da 45.B #include <sys/mman.h>
68e4db0a 46.PP
14f5ae6d 47.BI "void *mmap(void *" addr ", size_t " length \
a62f5121 48", int " prot ", int " flags ,
e0037472 49.BI " int " fd ", off_t " offset );
14f5ae6d 50.BI "int munmap(void *" addr ", size_t " length );
e0037472 51.fi
dbfe9c70 52.PP
45e97e2a 53See NOTES for information on feature test macro requirements.
fea681da 54.SH DESCRIPTION
1a956089 55.BR mmap ()
c13182ef 56creates a new mapping in the virtual address space of
5e8cde2f
MK
57the calling process.
58The starting address for the new mapping is specified in
14f5ae6d 59.IR addr .
5e8cde2f
MK
60The
61.I length
c19250ad 62argument specifies the length of the mapping (which must be greater than 0).
efeece04 63.PP
5e8cde2f 64If
14f5ae6d 65.I addr
5e8cde2f 66is NULL,
365a54c7
MK
67then the kernel chooses the (page-aligned) address
68at which to create the mapping;
5e8cde2f 69this is the most portable method of creating a new mapping.
c13182ef 70If
14f5ae6d 71.I addr
5e8cde2f
MK
72is not NULL,
73then the kernel takes it as a hint about where to place the mapping;
6aa7db0a
MK
74on Linux, the mapping will be created at a nearby page boundary.
75.\" Before Linux 2.6.24, the address was rounded up to the next page
29328361 76.\" boundary; since 2.6.24, it is rounded down!
5e8cde2f 77The address of the new mapping is returned as the result of the call.
efeece04 78.PP
5e8cde2f
MK
79The contents of a file mapping (as opposed to an anonymous mapping; see
80.B MAP_ANONYMOUS
d9bfdb9c 81below), are initialized using
fea681da
MK
82.I length
83bytes starting at offset
84.I offset
5e8cde2f
MK
85in the file (or other object) referred to by the file descriptor
86.IR fd .
a62f5121
MK
87.I offset
88must be a multiple of the page size as returned by
89.IR sysconf(_SC_PAGE_SIZE) .
dd3568a1 90.PP
fea681da
MK
91The
92.I prot
c13182ef 93argument describes the desired memory protection of the mapping
5e8cde2f
MK
94(and must not conflict with the open mode of the file).
95It is either
fea681da 96.B PROT_NONE
a62f5121 97or the bitwise OR of one or more of the following flags:
fea681da
MK
98.TP 1.1i
99.B PROT_EXEC
100Pages may be executed.
101.TP
102.B PROT_READ
103Pages may be read.
104.TP
105.B PROT_WRITE
106Pages may be written.
107.TP
108.B PROT_NONE
109Pages may not be accessed.
dd3568a1 110.PP
fea681da
MK
111The
112.I flags
a62f5121
MK
113argument determines whether updates to the mapping
114are visible to other processes mapping the same region,
ba7cb080 115and whether updates are carried through to the underlying file.
d9bfdb9c 116This behavior is determined by including exactly one
a62f5121 117of the following values in
5e8cde2f 118.IR flags :
ca90e95a 119.TP
fea681da 120.B MAP_SHARED
c13182ef 121Share this mapping.
bf525e90
MK
122Updates to the mapping are visible to other processes mapping the same region,
123and (in the case of file-backed mappings)
124are carried through to the underlying file.
72e8bdae
MK
125(To precisely control when updates are carried through
126to the underlying file requires the use of
127.BR msync (2).)
fea681da
MK
128.TP
129.B MAP_PRIVATE
130Create a private copy-on-write mapping.
a62f5121
MK
131Updates to the mapping are not visible to other processes
132mapping the same file, and are not carried through to
133the underlying file.
fea681da 134It is unspecified whether changes made to the file after the
1a956089 135.BR mmap ()
fea681da 136call are visible in the mapped region.
dd3568a1 137.PP
78cdbef7 138Both of these flags are described in POSIX.1-2001 and POSIX.1-2008.
efeece04 139.PP
a62f5121
MK
140In addition, zero or more of the following values can be ORed in
141.IR flags :
fea681da 142.TP
c368e7ca
MK
143.BR MAP_32BIT " (since Linux 2.4.20, 2.6)"
144Put the mapping into the first 2 Gigabytes of the process address space.
33a0ccb2 145This flag is supported only on x86-64, for 64-bit programs.
c368e7ca 146It was added to allow thread stacks to be allocated somewhere
ee8655b5 147in the first 2\ GB of memory,
c368e7ca
MK
148so as to improve context-switch performance on some early
14964-bit processors.
150.\" See http://lwn.net/Articles/294642 "Tangled up in threads", 19 Aug 08
151Modern x86-64 processors no longer have this performance problem,
152so use of this flag is not required on those systems.
153The
83314009 154.B MAP_32BIT
c368e7ca 155flag is ignored when
a62f5121 156.B MAP_FIXED
83314009 157is set.
fea681da 158.TP
a62f5121 159.B MAP_ANON
c13182ef
MK
160Synonym for
161.BR MAP_ANONYMOUS .
a62f5121
MK
162Deprecated.
163.TP
fea681da 164.B MAP_ANONYMOUS
5e8cde2f 165The mapping is not backed by any file;
d9bfdb9c 166its contents are initialized to zero.
5e8cde2f 167The
fea681da 168.I fd
423cb9f7 169argument is ignored;
c13182ef 170however, some implementations require
a62f5121 171.I fd
c13182ef
MK
172to be \-1 if
173.B MAP_ANONYMOUS
174(or
175.BR MAP_ANON )
a62f5121
MK
176is specified,
177and portable applications should ensure this.
423cb9f7
JH
178The
179.I offset
180argument should be zero.
181.\" See the pgoff overflow check in do_mmap().
182.\" See the offset check in sys_mmap in arch/x86/kernel/sys_x86_64.c.
c13182ef 183The use of
a62f5121 184.B MAP_ANONYMOUS
c13182ef 185in conjunction with
51ffcca0 186.B MAP_SHARED
33a0ccb2 187is supported on Linux only since kernel 2.4.
fea681da 188.TP
83314009
MK
189.B MAP_DENYWRITE
190This flag is ignored.
191.\" Introduced in 1.1.36, removed in 1.3.24.
d9bfdb9c 192(Long ago, it signaled that attempts to write to the underlying file
83314009
MK
193should fail with
194.BR ETXTBUSY .
195But this was a source of denial-of-service attacks.)
196.TP
197.B MAP_EXECUTABLE
198This flag is ignored.
199.\" Introduced in 1.1.38, removed in 1.3.24. Flag tested in proc_follow_link.
d9bfdb9c 200.\" (Long ago, it signaled that the underlying file is an executable.
83314009
MK
201.\" However, that information was not really used anywhere.)
202.\" Linus talked about DOS related to MAP_EXECUTABLE, but he was thinking of
203.\" MAP_DENYWRITE?
204.TP
fea681da 205.B MAP_FILE
c13182ef
MK
206Compatibility flag.
207Ignored.
988db661 208.\" On some systems, this was required as the opposite of
83314009 209.\" MAP_ANONYMOUS -- mtk, 1 May 2007
fea681da 210.TP
51ffcca0 211.B MAP_FIXED
83314009 212Don't interpret
14f5ae6d 213.I addr
83314009 214as a hint: place the mapping at exactly that address.
14f5ae6d 215.I addr
87c8ded5 216must be suitably aligned: for most architectures a multiple of the page
04bb0b99 217size is sufficient; however, some architectures may impose additional
e2da344f
MK
218restrictions.
219If the memory region specified by
14f5ae6d 220.I addr
83314009
MK
221and
222.I len
223overlaps pages of any existing mapping(s), then the overlapped
224part of the existing mapping(s) will be discarded.
225If the specified address cannot be used,
226.BR mmap ()
227will fail.
0113b287 228.IP
e2da344f 229Software that aspires to be portable should use this option with care,
9b92bc14 230keeping in mind that the exact layout of a process's memory mappings
e2da344f
MK
231is allowed to change significantly between kernel versions,
232C library versions, and operating system releases.
e2da344f 233Furthermore, this option is extremely hazardous (when used on its own),
33f01a06
MK
234because it forcibly removes preexisting mappings,
235making it easy for a multithreaded process to corrupt its own address space.
04bb0b99 236.IP
0113b287 237For example, suppose that thread A looks through
04bb0b99 238.I /proc/<pid>/maps
e2da344f
MK
239and locates an available address range,
240while thread B simultaneously acquires part or all of that same
241address range.
9b92bc14 242Thread A then calls
b9f34840
MK
243.BR mmap(MAP_FIXED) ,
244effectively overwriting the mapping that thread B created.
0113b287
MK
245In this scenario,
246thread B need not create a mapping directly; simply making a library call
04bb0b99 247that, internally, uses
b9f34840 248.BR dlopen (3)
0113b287 249to load some other shared library, will suffice.
b9f34840
MK
250The
251.BR dlopen (3)
252call will map the library into the process's address space.
3c778f69
JH
253Furthermore, almost any library call may be implemented in a way that
254adds memory mappings to the address space, either with this technique,
c6bd0e71
MK
255or by simply allocating memory.
256Examples include
b9f34840
MK
257.BR brk (2),
258.BR malloc (3),
259.BR pthread_create (3),
260and the PAM libraries
261.UR http://www.linux-pam.org
262.UE .
0113b287 263.IP
c6a51c06
MH
264Newer kernels
265(Linux 4.17 and later) have a
266.B MAP_FIXED_NOREPLACE
228d889f
MK
267option that avoids the corruption problem; if available,
268.B MAP_FIXED_NOREPLACE
269should be preferred over
270.BR MAP_FIXED .
c6a51c06
MH
271.TP
272.BR MAP_FIXED_NOREPLACE " (since Linux 4.17)"
3a548c59
MK
273.\" commit a4ff8e8620d3f4f50ac4b41e8067b7d395056843
274This flag provides behavior that is similar to
228d889f
MK
275.B MAP_FIXED
276with respect to the
277.I addr
3a548c59 278enforcement, but differs in that
228d889f 279.B MAP_FIXED_NOREPLACE
509935b7 280never clobbers a preexisting mapped range.
007d7833 281If the requested range would collide with an existing mapping,
3a548c59 282then this call fails with the error
c6a51c06 283.B EEXIST.
007d7833
MK
284This flag can therefore be used as a way to atomically
285(with respect to other threads) attempt to map an address range:
286one thread will succeed; all others will report failure.
3a548c59 287Note that older kernels which do not recognize this
509935b7 288flag will typically (upon detecting a collision with a preexisting mapping)
73cb50e2
MK
289fall back to a "non-\c
290.B MAP_FIXED\c
291" type of behavior:
3a548c59 292they will return an address that is different from the requested address.
007d7833 293Therefore, backward-compatible software
c6a51c06 294should check the returned address against the requested address.
fea681da 295.TP
83314009 296.B MAP_GROWSDOWN
86f12eb0
MK
297This flag is used for stacks.
298It indicates to the kernel virtual memory system that the mapping
5fab2e7c 299should extend downward in memory.
176b1a76
MK
300The return address is one page lower than the memory area that is
301actually created in the process's virtual address space.
302Touching an address in the "guard" page below the mapping will cause
303the mapping to grow by a page.
304This growth can be repeated until the mapping grows to within a
305page of the high end of the next lower mapping,
306at which point touching the "guard" page will result in a
307.B SIGSEGV
308signal.
83314009 309.TP
76a34baa
MK
310.BR MAP_HUGETLB " (since Linux 2.6.32)"
311Allocate the mapping using "huge pages."
66a9882e 312See the Linux kernel source file
76a34baa 313.I Documentation/vm/hugetlbpage.txt
f1461fe1 314for further information, as well as NOTES, below.
76a34baa 315.TP
5d2038b6
MK
316.BR MAP_HUGE_2MB ", " MAP_HUGE_1GB " (since Linux 3.8)"
317.\" See https://lwn.net/Articles/533499/
318Used in conjunction with
319.B MAP_HUGETLB
c4b7e5ac 320to select alternative hugetlb page sizes (respectively, 2\ MB and 1\ GB)
5d2038b6 321on systems that support multiple hugetlb page sizes.
efeece04 322.IP
5d2038b6
MK
323More generally, the desired huge page size can be configured by encoding
324the base-2 logarithm of the desired page size in the six bits at the offset
325.BR MAP_HUGE_SHIFT .
326(A value of zero in this bit field provides the default huge page size;
327the default huge page size can be discovered vie the
328.I Hugepagesize
329field exposed by
330.IR /proc/meminfo .)
331Thus, the above two constants are defined as:
efeece04 332.IP
5d2038b6 333.in +4n
b8302363 334.EX
5d2038b6
MK
335#define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT)
336#define MAP_HUGE_1GB (30 << MAP_HUGE_SHIFT)
b8302363 337.EE
e646a1ba 338.in
efeece04 339.IP
5d2038b6
MK
340The range of huge page sizes that are supported by the system
341can be discovered by listing the subdirectories in
342.IR /sys/kernel/mm/hugepages .
343.TP
83314009 344.BR MAP_LOCKED " (since Linux 2.5.37)"
db4e8e25 345Mark the mapped region to be locked in the same way as
74d32233 346.BR mlock (2).
db4e8e25
MK
347This implementation will try to populate (prefault) the whole range but the
348.BR mmap ()
349call doesn't fail with
7e3786bc 350.B ENOMEM
911f1c7a
MK
351if this fails.
352Therefore major faults might happen later on.
353So the semantic is not as strong as
7e3786bc 354.BR mlock (2).
911f1c7a 355One should use
bf7bc8b8 356.BR mmap ()
911f1c7a 357plus
7e3786bc 358.BR mlock (2)
911f1c7a
MK
359when major faults are not acceptable after the initialization of the mapping.
360The
361.BR MAP_LOCKED
362flag is ignored in older kernels.
83314009 363.\" If set, the mapped pages will not be swapped out.
fea681da
MK
364.TP
365.BR MAP_NONBLOCK " (since Linux 2.5.46)"
3f06ade3 366This flag is meaningful only in conjunction with
51ffcca0 367.BR MAP_POPULATE .
c13182ef 368Don't perform read-ahead:
33a0ccb2 369create page tables entries only for pages
51ffcca0 370that are already present in RAM.
7c40de08 371Since Linux 2.6.23, this flag causes
f38fa944
MK
372.BR MAP_POPULATE
373to do nothing.
487c2f05 374One day, the combination of
f38fa944
MK
375.BR MAP_POPULATE
376and
377.BR MAP_NONBLOCK
3b777aff 378may be reimplemented.
83314009
MK
379.TP
380.B MAP_NORESERVE
381Do not reserve swap space for this mapping.
382When swap space is reserved, one has the guarantee
383that it is possible to modify the mapping.
8bd58774
MK
384When swap space is not reserved one might get
385.B SIGSEGV
386upon a write
83314009
MK
387if no physical memory is available.
388See also the discussion of the file
389.I /proc/sys/vm/overcommit_memory
390in
391.BR proc (5).
33a0ccb2 392In kernels before 2.6, this flag had effect only for
83314009
MK
393private writable mappings.
394.TP
395.BR MAP_POPULATE " (since Linux 2.5.46)"
f38fa944
MK
396Populate (prefault) page tables for a mapping.
397For a file mapping, this causes read-ahead on the file.
bbebbb6d 398This will help to reduce blocking on page faults later.
f38fa944 399.BR MAP_POPULATE
33a0ccb2 400is supported for private mappings only since Linux 2.6.23.
e6205b0c
MK
401.TP
402.BR MAP_STACK " (since Linux 2.6.27)"
403Allocate the mapping at an address suitable for a process
404or thread stack.
405This flag is currently a no-op,
406but is used in the glibc threading implementation so that
407if some architectures require special treatment for stack allocations,
408support can later be transparently implemented for glibc.
67b59ff5 409.\" See http://lwn.net/Articles/294642 "Tangled up in threads", 19 Aug 08
e6205b0c
MK
410.\" commit cd98a04a59e2f94fa64d5bf1e26498d27427d5e7
411.\" http://thread.gmane.org/gmane.linux.kernel/720412
412.\" "pthread_create() slow for many threads; also time to revisit 64b
413.\" context switch optimization?"
12062404
MK
414.TP
415.BR MAP_UNINITIALIZED " (since Linux 2.6.33)"
416Don't clear anonymous pages.
417This flag is intended to improve performance on embedded devices.
33a0ccb2 418This flag is honored only if the kernel was configured with the
12062404
MK
419.B CONFIG_MMAP_ALLOW_UNINITIALIZED
420option.
421Because of the security implications,
422that option is normally enabled only on embedded devices
423(i.e., devices where one has complete control of the contents of user memory).
dd3568a1 424.PP
7c7adcbe
MK
425Of the above flags, only
426.B MAP_FIXED
78cdbef7 427is specified in POSIX.1-2001 and POSIX.1-2008.
7c7adcbe
MK
428However, most systems also support
429.B MAP_ANONYMOUS
430(or its synonym
431.BR MAP_ANON ).
f5f41651
MK
432.\" FIXME . for later review when Issue 8 is one day released...
433.\" POSIX may add MAP_ANON in the future
434.\" http://austingroupbugs.net/tag_view_page.php?tag_id=8
435.\" http://austingroupbugs.net/view.php?id=850
dd3568a1 436.PP
fea681da 437Memory mapped by
1a956089 438.BR mmap ()
fea681da
MK
439is preserved across
440.BR fork (2),
441with the same attributes.
dd3568a1 442.PP
c13182ef
MK
443A file is mapped in multiples of the page size.
444For a file that is not
fea681da 445a multiple of the page size, the remaining memory is zeroed when mapped,
c13182ef
MK
446and writes to that region are not written out to the file.
447The effect of
fea681da
MK
448changing the size of the underlying file of a mapping on the pages that
449correspond to added or removed regions of the file is unspecified.
de5f7e28 450.SS munmap()
fea681da 451The
1a956089 452.BR munmap ()
fea681da
MK
453system call deletes the mappings for the specified address range, and
454causes further references to addresses within the range to generate
c13182ef
MK
455invalid memory references.
456The region is also automatically unmapped
457when the process is terminated.
458On the other hand, closing the file
fea681da 459descriptor does not unmap the region.
dd3568a1 460.PP
fea681da 461The address
14f5ae6d 462.I addr
0e824bcb
MK
463must be a multiple of the page size (but
464.I length
465need not be).
c13182ef 466All pages containing a part
fea681da 467of the indicated range are unmapped, and subsequent references
8bd58774
MK
468to these pages will generate
469.BR SIGSEGV .
c13182ef 470It is not an error if the
fea681da 471indicated range does not contain any mapped pages.
47297adb 472.SH RETURN VALUE
fea681da 473On success,
1a956089 474.BR mmap ()
fea681da
MK
475returns a pointer to the mapped area.
476On error, the value
477.B MAP_FAILED
c13182ef 478(that is,
009df872 479.IR "(void\ *)\ \-1" )
5e8cde2f 480is returned, and
fea681da 481.I errno
80691a91 482is set to indicate the cause of the error.
efeece04 483.PP
fea681da 484On success,
1a956089 485.BR munmap ()
80691a91
MK
486returns 0.
487On failure, it returns \-1, and
fea681da 488.I errno
80691a91 489is set to indicate the cause of the error (probably to
51ffcca0 490.BR EINVAL ).
fea681da
MK
491.SH ERRORS
492.TP
493.B EACCES
494A file descriptor refers to a non-regular file.
5e7c71f6 495Or a file mapping was requested, but
fea681da
MK
496.I fd
497is not open for reading.
c13182ef
MK
498Or
499.B MAP_SHARED
500was requested and
501.B PROT_WRITE
51ffcca0 502is set, but
fea681da 503.I fd
682edefb
MK
504is not open in read/write
505.RB ( O_RDWR )
506mode.
c13182ef
MK
507Or
508.B PROT_WRITE
51ffcca0 509is set, but the file is append-only.
fea681da
MK
510.TP
511.B EAGAIN
83cd3686
MK
512The file has been locked, or too much memory has been locked (see
513.BR setrlimit (2)).
fea681da
MK
514.TP
515.B EBADF
516.I fd
c13182ef 517is not a valid file descriptor (and
51ffcca0
MK
518.B MAP_ANONYMOUS
519was not set).
fea681da 520.TP
c6a51c06
MH
521.B EEXIST
522range covered by
523.IR addr ,
524.IR length
525is clashing with an existing mapping.
526.TP
fea681da
MK
527.B EINVAL
528We don't like
14f5ae6d 529.IR addr ,
62a04c81 530.IR length ,
fea681da 531or
0daa9e92 532.I offset
62a04c81
MK
533(e.g., they are too large, or not aligned on a page boundary).
534.TP
535.B EINVAL
f99fc197 536(since Linux 2.6.12)
fea681da 537.I length
62a04c81
MK
538was 0.
539.TP
540.B EINVAL
541.I flags
542contained neither
543.B MAP_PRIVATE
fea681da 544or
62a04c81
MK
545.BR MAP_SHARED ,
546or contained both of these values.
fea681da
MK
547.TP
548.B ENFILE
549.\" This is for shared anonymous segments
550.\" [2.6.7] shmem_zero_setup()-->shmem_file_setup()-->get_empty_filp()
e258766b 551The system-wide limit on the total number of open files has been reached.
fea681da
MK
552.\" .TP
553.\" .B ENOEXEC
554.\" A file could not be mapped for reading.
555.TP
556.B ENODEV
9ee4a2b6 557The underlying filesystem of the specified file does not support
fea681da
MK
558memory mapping.
559.TP
560.B ENOMEM
74309bed
MK
561No memory is available.
562.TP
563.B ENOMEM
564The process's maximum number of mappings would have been exceeded.
c0b89788 565This error can also occur for
3804d39d 566.BR munmap (),
c0b89788
MK
567when unmapping a region in the middle of an existing mapping,
568since this results in two smaller mappings on either side of
569the region being unmapped.
fea681da 570.TP
c87d084b
JG
571.B ENOMEM
572(since Linux 4.7)
573The process's
574.B RLIMIT_DATA
575limit, described in
576.BR getrlimit (2),
577would have been exceeded.
578.TP
038f5175
MK
579.B EOVERFLOW
580On 32-bit architecture together with the large file extension
581(i.e., using 64-bit
582.IR off_t ):
583the number of pages used for
584.I length
585plus number of pages used for
586.I offset
587would overflow
588.I "unsigned long"
589(32 bits).
590.TP
fea681da
MK
591.B EPERM
592The
593.I prot
594argument asks for
595.B PROT_EXEC
9ee4a2b6 596but the mapped area belongs to a file on a filesystem that
fea681da
MK
597was mounted no-exec.
598.\" (Since 2.4.25 / 2.6.0.)
599.TP
fbab10e5
MK
600.B EPERM
601The operation was prevented by a file seal; see
602.BR fcntl (2).
603.TP
fea681da 604.B ETXTBSY
c13182ef 605.B MAP_DENYWRITE
51ffcca0 606was set but the object specified by
fea681da
MK
607.I fd
608is open for writing.
dd3568a1 609.PP
fea681da
MK
610Use of a mapped region can result in these signals:
611.TP
612.B SIGSEGV
1e321034 613Attempted write into a region mapped as read-only.
fea681da
MK
614.TP
615.B SIGBUS
616Attempted access to a portion of the buffer that does not correspond
617to the file (for example, beyond the end of the file, including the
618case where another process has truncated the file).
8fddf95a
MS
619.SH ATTRIBUTES
620For an explanation of the terms used in this section, see
621.BR attributes (7).
622.TS
623allbox;
624lbw18 lb lb
625l l l.
626Interface Attribute Value
627T{
628.BR mmap (),
629.BR munmap ()
630T} Thread safety MT-Safe
631.TE
47297adb 632.SH CONFORMING TO
78cdbef7 633POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD.
2b2581ee
MK
634.\" SVr4 documents additional error codes ENXIO and ENODEV.
635.\" SUSv2 documents additional error codes EMFILE and EOVERFLOW.
fea681da
MK
636.SH AVAILABILITY
637On POSIX systems on which
1a956089 638.BR mmap (),
9af134cd 639.BR msync (2),
fea681da 640and
1a956089 641.BR munmap ()
fea681da
MK
642are available,
643.B _POSIX_MAPPED_FILES
6387216b
MK
644is defined in \fI<unistd.h>\fP to a value greater than 0.
645(See also
fea681da 646.BR sysconf (3).)
97c1eac8 647.\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
fea681da
MK
648.\" -1: unavailable, 0: ask using sysconf().
649.\" glibc defines it to 1.
a1d5f77c 650.SH NOTES
34ccb744 651On some hardware architectures (e.g., i386),
0daa9e92 652.B PROT_WRITE
f3edaabb
MK
653implies
654.BR PROT_READ .
a1d5f77c
MK
655It is architecture dependent whether
656.B PROT_READ
657implies
658.B PROT_EXEC
659or not.
660Portable programs should always set
661.B PROT_EXEC
662if they intend to execute code in the new mapping.
efeece04 663.PP
80d17cfa
MK
664The portable way to create a mapping is to specify
665.I addr
666as 0 (NULL), and omit
667.B MAP_FIXED
668from
669.IR flags .
670In this case, the system chooses the address for the mapping;
46cdb997 671the address is chosen so as not to conflict with any existing mapping,
80d17cfa
MK
672and will not be 0.
673If the
674.B MAP_FIXED
675flag is specified, and
676.I addr
491cd2f0 677is 0 (NULL), then the mapped address will be 0 (NULL).
efeece04 678.PP
45e97e2a
MK
679Certain
680.I flags
f8619b6a
MK
681constants are defined only if suitable feature test macros are defined
682(possibly by default):
683.BR _DEFAULT_SOURCE
684with glibc 2.19 or later;
685or
45e97e2a
MK
686.BR _BSD_SOURCE
687or
688.BR _SVID_SOURCE
f8619b6a 689in glibc 2.19 and earlier.
50efeef8 690(Employing
45e97e2a
MK
691.BR _GNU_SOURCE
692also suffices,
693and requiring that macro specifically would have been more logical,
76c637e1 694since these flags are all Linux-specific.)
45e97e2a
MK
695The relevant flags are:
696.BR MAP_32BIT ,
697.BR MAP_ANONYMOUS
698(and the synonym
699.BR MAP_ANON ),
700.BR MAP_DENYWRITE ,
701.BR MAP_EXECUTABLE ,
702.BR MAP_FILE ,
703.BR MAP_GROWSDOWN ,
704.BR MAP_HUGETLB ,
705.BR MAP_LOCKED ,
706.BR MAP_NONBLOCK ,
707.BR MAP_NORESERVE ,
708.BR MAP_POPULATE ,
709and
710.BR MAP_STACK .
efeece04 711.PP
3bd859bf
MK
712An application can determine which pages of a mapping are
713currently resident in the buffer/page cache using
714.BR mincore (2).
35c189fb 715.\"
0f319769
MK
716.SS Timestamps changes for file-backed mappings
717For file-backed mappings, the
718.I st_atime
719field for the mapped file may be updated at any time between the
720.BR mmap ()
721and the corresponding unmapping; the first reference to a mapped
722page will update the field if it has not been already.
dd3568a1 723.PP
0f319769
MK
724The
725.I st_ctime
726and
727.I st_mtime
728field for a file mapped with
729.B PROT_WRITE
730and
731.B MAP_SHARED
732will be updated after
733a write to the mapped region, and before a subsequent
734.BR msync (2)
735with the
736.B MS_SYNC
737or
738.B MS_ASYNC
739flag, if one occurs.
740.\"
f1461fe1
MK
741.SS Huge page (Huge TLB) mappings
742For mappings that employ huge pages, the requirements for the arguments of
743.BR mmap ()
744and
745.BR munmap ()
746differ somewhat from the requirements for mappings
747that use the native system page size.
efeece04 748.PP
f1461fe1
MK
749For
750.BR mmap (),
751.I offset
752must be a multiple of the underlying huge page size.
753The system automatically aligns
754.I length
755to be a multiple of the underlying huge page size.
efeece04 756.PP
f1461fe1
MK
757For
758.BR munmap (),
759.I addr
760and
761.I length
762must both be a multiple of the underlying huge page size.
763.\"
0722a578 764.SS C library/kernel differences
35c189fb
MK
765This page describes the interface provided by the glibc
766.BR mmap ()
767wrapper function.
768Originally, this function invoked a system call of the same name.
769Since kernel 2.4, that system call has been superseded by
770.BR mmap2 (2),
771and nowadays
772.\" Since around glibc 2.1/2.2, depending on the platform.
773the glibc
774.BR mmap ()
775wrapper function invokes
776.BR mmap2 (2)
777with a suitably adjusted value for
778.IR offset .
fea681da 779.SH BUGS
329ad271 780On Linux, there are no guarantees like those suggested above under
c13182ef 781.BR MAP_NORESERVE .
dbc53ca8 782By default, any process can be killed
fea681da 783at any moment when the system runs out of memory.
efeece04 784.PP
dbc53ca8
MK
785In kernels before 2.6.7, the
786.B MAP_POPULATE
33a0ccb2 787flag has effect only if
dbc53ca8
MK
788.I prot
789is specified as
790.BR PROT_NONE .
efeece04 791.PP
c13182ef 792SUSv3 specifies that
c8f3e580
MK
793.BR mmap ()
794should fail if
795.I length
796is 0.
797However, in kernels before 2.6.12,
798.BR mmap ()
799succeeded in this case: no mapping was created and the call returned
14f5ae6d 800.IR addr .
c8f3e580
MK
801Since kernel 2.6.12,
802.BR mmap ()
803fails with the error
804.B EINVAL
805for this case.
efeece04 806.PP
a780f17b
MK
807POSIX specifies that the system shall always
808zero fill any partial page at the end
b072a788 809of the object and that system will never write any modification of the
a780f17b
MK
810object beyond its end.
811On Linux, when you write data to such partial page after the end
b072a788 812of the object, the data stays in the page cache even after the file
a780f17b
MK
813is closed and unmapped
814and even though the data is never written to the file itself,
815subsequent mappings may see the modified content.
816In some cases, this could be fixed by calling
817.BR msync (2)
818before the unmap takes place;
4e07c70f
MK
819however, this doesn't work on
820.BR tmpfs (5)
b758a50a 821(for example, when using the POSIX shared memory interface documented in
a780f17b 822.BR shm_overview (7)).
74fa61b7 823.SH EXAMPLE
2e001ad4
MK
824.\" FIXME . Add an example here that uses an anonymous shared region for
825.\" IPC between parent and child.
74fa61b7
MK
826.PP
827The following program prints part of the file specified in
828its first command-line argument to standard output.
829The range of bytes to be printed is specified via offset and length
830values in the second and third command-line arguments.
831The program creates a memory mapping of the required
832pages of the file and then uses
833.BR write (2)
834to output the desired bytes.
f30b7415 835.SS Program source
e7d0bb47 836.EX
74fa61b7
MK
837#include <sys/mman.h>
838#include <sys/stat.h>
839#include <fcntl.h>
840#include <stdio.h>
841#include <stdlib.h>
842#include <unistd.h>
843
4407d3d8
MK
844#define handle_error(msg) \\
845 do { perror(msg); exit(EXIT_FAILURE); } while (0)
846
74fa61b7
MK
847int
848main(int argc, char *argv[])
849{
850 char *addr;
851 int fd;
852 struct stat sb;
853 off_t offset, pa_offset;
854 size_t length;
855 ssize_t s;
856
fbbfa7ce 857 if (argc < 3 || argc > 4) {
74fa61b7
MK
858 fprintf(stderr, "%s file offset [length]\\n", argv[0]);
859 exit(EXIT_FAILURE);
860 }
861
862 fd = open(argv[1], O_RDONLY);
4407d3d8 863 if (fd == \-1)
8568021d 864 handle_error("open");
74fa61b7 865
4407d3d8
MK
866 if (fstat(fd, &sb) == \-1) /* To obtain file size */
867 handle_error("fstat");
74fa61b7
MK
868
869 offset = atoi(argv[2]);
870 pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) \- 1);
871 /* offset for mmap() must be page aligned */
872
873 if (offset >= sb.st_size) {
874 fprintf(stderr, "offset is past end of file\\n");
875 exit(EXIT_FAILURE);
876 }
877
878 if (argc == 4) {
879 length = atoi(argv[3]);
880 if (offset + length > sb.st_size)
881 length = sb.st_size \- offset;
f81fb444 882 /* Can\(aqt display bytes past end of file */
5b6adad1 883
74fa61b7
MK
884 } else { /* No length arg ==> display to end of file */
885 length = sb.st_size \- offset;
886 }
887
888 addr = mmap(NULL, length + offset \- pa_offset, PROT_READ,
889 MAP_PRIVATE, fd, pa_offset);
4407d3d8
MK
890 if (addr == MAP_FAILED)
891 handle_error("mmap");
74fa61b7
MK
892
893 s = write(STDOUT_FILENO, addr + offset \- pa_offset, length);
894 if (s != length) {
895 if (s == \-1)
4407d3d8
MK
896 handle_error("write");
897
898 fprintf(stderr, "partial write");
74fa61b7
MK
899 exit(EXIT_FAILURE);
900 }
901
40142309
MK
902 munmap(addr, length + offset \- pa_offset);
903 close(fd);
904
74fa61b7 905 exit(EXIT_SUCCESS);
c54ed37e 906}
e7d0bb47 907.EE
47297adb 908.SH SEE ALSO
74f25490 909.BR ftruncate (2),
fea681da 910.BR getpagesize (2),
c4d76cd9 911.BR memfd_create (2),
f75c3a3b 912.BR mincore (2),
fea681da
MK
913.BR mlock (2),
914.BR mmap2 (2),
54504ac3 915.BR mprotect (2),
fea681da
MK
916.BR mremap (2),
917.BR msync (2),
931e4e25 918.BR remap_file_pages (2),
83cd3686 919.BR setrlimit (2),
7921f13b 920.BR shmat (2),
13acca70 921.BR userfaultfd (2),
f93af9c6
MK
922.BR shm_open (3),
923.BR shm_overview (7)
efeece04 924.PP
0bf14b87
MK
925The descriptions of the following files in
926.BR proc (5):
927.IR /proc/[pid]/maps ,
928.IR /proc/[pid]/map_files ,
929and
930.IR /proc/[pid]/smaps .
efeece04 931.PP
d2fdb1e3 932B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128\(en129 and 389\(en391.
fea681da
MK
933.\"
934.\" Repeat after me: private read-only mappings are 100% equivalent to
935.\" shared read-only mappings. No ifs, buts, or maybes. -- Linus