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