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