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