man-pages/man2/madvise.2.html
2021-03-31 01:06:50 +01:00

795 lines
18 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of MADVISE</TITLE>
</HEAD><BODY>
<H1>MADVISE</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
madvise - give advice about use of memory
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/mman.h">sys/mman.h</A>&gt;</B>
<P>
<B>int madvise(void *</B><I>addr</I><B>, size_t </B><I>length</I><B>, int </B><I>advice</I><B>);</B>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>madvise</B>():
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2">Since glibc 2.19:<DD>
_DEFAULT_SOURCE
<DT id="3">Up to and including glibc 2.19:<DD>
_BSD_SOURCE
</DL>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>madvise</B>()
system call is used to give advice or directions to the kernel
about the address range beginning at address
<I>addr</I>
and with size
<I>length</I>
bytes
In most cases,
the goal of such advice is to improve system or application performance.
<P>
Initially, the system call supported a set of &quot;conventional&quot;
<I>advice</I>
values, which are also available on several other implementations.
(Note, though, that
<B>madvise</B>()
is not specified in POSIX.)
Subsequently, a number of Linux-specific
<I>advice</I>
values have been added.
<A NAME="lbAE">&nbsp;</A>
<H3>Conventional advice values</H3>
The
<I>advice</I>
values listed below
allow an application to tell the kernel how it expects to use
some mapped or shared memory areas, so that the kernel can choose
appropriate read-ahead and caching techniques.
These
<I>advice</I>
values do not influence the semantics of the application
(except in the case of
<B>MADV_DONTNEED</B>),
but may influence its performance.
All of the
<I>advice</I>
values listed here have analogs in the POSIX-specified
<B><A HREF="/cgi-bin/man/man2html?3+posix_madvise">posix_madvise</A></B>(3)
function, and the values have the same meanings, with the exception of
<B>MADV_DONTNEED</B>.
<P>
The advice is indicated in the
<I>advice</I>
argument, which is one of the following:
<DL COMPACT>
<DT id="4"><B>MADV_NORMAL</B>
<DD>
No special treatment.
This is the default.
<DT id="5"><B>MADV_RANDOM</B>
<DD>
Expect page references in random order.
(Hence, read ahead may be less useful than normally.)
<DT id="6"><B>MADV_SEQUENTIAL</B>
<DD>
Expect page references in sequential order.
(Hence, pages in the given range can be aggressively read ahead,
and may be freed soon after they are accessed.)
<DT id="7"><B>MADV_WILLNEED</B>
<DD>
Expect access in the near future.
(Hence, it might be a good idea to read some pages ahead.)
<DT id="8"><B>MADV_DONTNEED</B>
<DD>
Do not expect access in the near future.
(For the time being, the application is finished with the given range,
so the kernel can free resources associated with it.)
<DT id="9"><DD>
After a successful
<B>MADV_DONTNEED</B>
operation,
the semantics of memory access in the specified region are changed:
subsequent accesses of pages in the range will succeed, but will result
in either repopulating the memory contents from the
up-to-date contents of the underlying mapped file
(for shared file mappings, shared anonymous mappings,
and shmem-based techniques such as System V shared memory segments)
or zero-fill-on-demand pages for anonymous private mappings.
<DT id="10"><DD>
Note that, when applied to shared mappings,
<B>MADV_DONTNEED</B>
might not lead to immediate freeing of the pages in the range.
The kernel is free to delay freeing the pages until an appropriate moment.
The resident set size (RSS) of the calling process will be immediately
reduced however.
<DT id="11"><DD>
<B>MADV_DONTNEED</B>
cannot be applied to locked pages, Huge TLB pages, or
<B>VM_PFNMAP</B>
pages.
(Pages marked with the kernel-internal
<B>VM_PFNMAP</B>
flag are special memory areas that are not managed
by the virtual memory subsystem.
Such pages are typically created by device drivers that
map the pages into user space.)
</DL>
<A NAME="lbAF">&nbsp;</A>
<H3>Linux-specific advice values</H3>
The following Linux-specific
<I>advice</I>
values have no counterparts in the POSIX-specified
<B><A HREF="/cgi-bin/man/man2html?3+posix_madvise">posix_madvise</A></B>(3),
and may or may not have counterparts in the
<B>madvise</B>()
interface available on other implementations.
Note that some of these operations change the semantics of memory accesses.
<DL COMPACT>
<DT id="12"><B>MADV_REMOVE</B> (since Linux 2.6.16)
<DD>
Free up a given range of pages
and its associated backing store.
This is equivalent to punching a hole in the corresponding byte
range of the backing store (see
<B><A HREF="/cgi-bin/man/man2html?2+fallocate">fallocate</A></B>(2)).
Subsequent accesses in the specified address range will see
bytes containing zero.
<DT id="13"><DD>
The specified address range must be mapped shared and writable.
This flag cannot be applied to locked pages, Huge TLB pages, or
<B>VM_PFNMAP</B>
pages.
<DT id="14"><DD>
In the initial implementation, only
<B><A HREF="/cgi-bin/man/man2html?5+tmpfs">tmpfs</A></B>(5)
was supported
<B>MADV_REMOVE</B>;
but since Linux 3.5,
any filesystem which supports the
<B><A HREF="/cgi-bin/man/man2html?2+fallocate">fallocate</A></B>(2)
<B>FALLOC_FL_PUNCH_HOLE</B>
mode also supports
<B>MADV_REMOVE</B>.
Hugetlbfs fails with the error
<B>EINVAL</B>
and other filesystems fail with the error
<B>EOPNOTSUPP</B>.
<DT id="15"><B>MADV_DONTFORK</B> (since Linux 2.6.16)
<DD>
Do not make the pages in this range available to the child after a
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2).
This is useful to prevent copy-on-write semantics from changing
the physical location of a page if the parent writes to it after a
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2).
(Such page relocations cause problems for hardware that
DMAs into the page.)
<DT id="16"><B>MADV_DOFORK</B> (since Linux 2.6.16)
<DD>
Undo the effect of
<B>MADV_DONTFORK</B>,
restoring the default behavior, whereby a mapping is inherited across
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2).
<DT id="17"><B>MADV_HWPOISON</B> (since Linux 2.6.32)
<DD>
Poison the pages in the range specified by
<I>addr</I>
and
<I>length</I>
and handle subsequent references to those pages
like a hardware memory corruption.
This operation is available only for privileged
(<B>CAP_SYS_ADMIN</B>)
processes.
This operation may result in the calling process receiving a
<B>SIGBUS</B>
and the page being unmapped.
<DT id="18"><DD>
This feature is intended for testing of memory error-handling code;
it is available only if the kernel was configured with
<B>CONFIG_MEMORY_FAILURE</B>.
<DT id="19"><B>MADV_MERGEABLE</B> (since Linux 2.6.32)
<DD>
Enable Kernel Samepage Merging (KSM) for the pages in the range specified by
<I>addr</I>
and
<I>length</I>.
The kernel regularly scans those areas of user memory that have
been marked as mergeable,
looking for pages with identical content.
These are replaced by a single write-protected page (which is automatically
copied if a process later wants to update the content of the page).
KSM merges only private anonymous pages (see
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)).
<DT id="20"><DD>
The KSM feature is intended for applications that generate many
instances of the same data (e.g., virtualization systems such as KVM).
It can consume a lot of processing power; use with care.
See the Linux kernel source file
<I>Documentation/admin-guide/mm/ksm.rst</I>
for more details.
<DT id="21"><DD>
The
<B>MADV_MERGEABLE</B>
and
<B>MADV_UNMERGEABLE</B>
operations are available only if the kernel was configured with
<B>CONFIG_KSM</B>.
<DT id="22"><B>MADV_UNMERGEABLE</B> (since Linux 2.6.32)
<DD>
Undo the effect of an earlier
<B>MADV_MERGEABLE</B>
operation on the specified address range;
KSM unmerges whatever pages it had merged in the address range specified by
<I>addr</I>
and
<I>length</I>.
<DT id="23"><B>MADV_SOFT_OFFLINE</B> (since Linux 2.6.33)
<DD>
Soft offline the pages in the range specified by
<I>addr</I>
and
<I>length</I>.
The memory of each page in the specified range is preserved
(i.e., when next accessed, the same content will be visible,
but in a new physical page frame),
and the original page is offlined
(i.e., no longer used, and taken out of normal memory management).
The effect of the
<B>MADV_SOFT_OFFLINE</B>
operation is invisible to (i.e., does not change the semantics of)
the calling process.
<DT id="24"><DD>
This feature is intended for testing of memory error-handling code;
it is available only if the kernel was configured with
<B>CONFIG_MEMORY_FAILURE</B>.
<DT id="25"><B>MADV_HUGEPAGE</B> (since Linux 2.6.38)
<DD>
Enable Transparent Huge Pages (THP) for pages in the range specified by
<I>addr</I>
and
<I>length</I>.
Currently, Transparent Huge Pages work only with private anonymous pages (see
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)).
The kernel will regularly scan the areas marked as huge page candidates
to replace them with huge pages.
The kernel will also allocate huge pages directly when the region is
naturally aligned to the huge page size (see
<B><A HREF="/cgi-bin/man/man2html?2+posix_memalign">posix_memalign</A></B>(2)).
<DT id="26"><DD>
This feature is primarily aimed at applications that use large mappings of
data and access large regions of that memory at a time (e.g., virtualization
systems such as QEMU).
It can very easily waste memory (e.g., a 2&nbsp;MB mapping that only ever accesses
1 byte will result in 2&nbsp;MB of wired memory instead of one 4&nbsp;KB page).
See the Linux kernel source file
<I>Documentation/admin-guide/mm/transhuge.rst</I>
for more details.
<DT id="27"><DD>
The
<B>MADV_HUGEPAGE</B>
and
<B>MADV_NOHUGEPAGE</B>
operations are available only if the kernel was configured with
<B>CONFIG_TRANSPARENT_HUGEPAGE</B>.
<DT id="28"><B>MADV_NOHUGEPAGE</B> (since Linux 2.6.38)
<DD>
Ensures that memory in the address range specified by
<I>addr</I>
and
<I>length</I>
will not be collapsed into huge pages.
<DT id="29"><B>MADV_DONTDUMP</B> (since Linux 3.4)
<DD>
Exclude from a core dump those pages in the range specified by
<I>addr</I>
and
<I>length</I>.
This is useful in applications that have large areas of memory
that are known not to be useful in a core dump.
The effect of
<B>MADV_DONTDUMP</B>
takes precedence over the bit mask that is set via the
<I>/proc/[pid]/coredump_filter</I>
file (see
<B><A HREF="/cgi-bin/man/man2html?5+core">core</A></B>(5)).
<DT id="30"><B>MADV_DODUMP</B> (since Linux 3.4)
<DD>
Undo the effect of an earlier
<B>MADV_DONTDUMP</B>.
<DT id="31"><B>MADV_FREE</B> (since Linux 4.5)
<DD>
The application no longer requires the pages in the range specified by
<I>addr</I>
and
<I>len</I>.
The kernel can thus free these pages,
but the freeing could be delayed until memory pressure occurs.
For each of the pages that has been marked to be freed
but has not yet been freed,
the free operation will be canceled if the caller writes into the page.
After a successful
<B>MADV_FREE</B>
operation, any stale data (i.e., dirty, unwritten pages) will be lost
when the kernel frees the pages.
However, subsequent writes to pages in the range will succeed
and then kernel cannot free those dirtied pages,
so that the caller can always see just written data.
If there is no subsequent write,
the kernel can free the pages at any time.
Once pages in the range have been freed, the caller will
see zero-fill-on-demand pages upon subsequent page references.
<DT id="32"><DD>
The
<B>MADV_FREE</B>
operation
can be applied only to private anonymous pages (see
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)).
In Linux before version 4.12,
when freeing pages on a swapless system,
the pages in the given range are freed instantly,
regardless of memory pressure.
<DT id="33"><B>MADV_WIPEONFORK</B> (since Linux 4.14)
<DD>
Present the child process with zero-filled memory in this range after a
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2).
This is useful in forking servers in order to ensure
that sensitive per-process data
(for example, PRNG seeds, cryptographic secrets, and so on)
is not handed to child processes.
<DT id="34"><DD>
The
<B>MADV_WIPEONFORK</B>
operation can be applied only to private anonymous pages (see
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)).
<DT id="35"><DD>
Within the child created by
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
the
<B>MADV_WIPEONFORK</B>
setting remains in place on the specified address range.
This setting is cleared during
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
<DT id="36"><B>MADV_KEEPONFORK</B> (since Linux 4.14)
<DD>
Undo the effect of an earlier
<B>MADV_WIPEONFORK</B>.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>madvise</B>()
returns zero.
On error, it returns -1 and
<I>errno</I>
is set appropriately.
<A NAME="lbAH">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="37"><B>EACCES</B>
<DD>
<I>advice</I>
is
<B>MADV_REMOVE</B>,
but the specified address range is not a shared writable mapping.
<DT id="38"><B>EAGAIN</B>
<DD>
A kernel resource was temporarily unavailable.
<DT id="39"><B>EBADF</B>
<DD>
The map exists, but the area maps something that isn't a file.
<DT id="40"><B>EINVAL</B>
<DD>
<I>addr</I>
is not page-aligned or
<I>length</I>
is negative.
<DT id="41"><B>EINVAL</B>
<DD>
<I>advice</I>
is not a valid.
<DT id="42"><B>EINVAL</B>
<DD>
<I>advice</I>
is
<B>MADV_DONTNEED</B>
or
<B>MADV_REMOVE</B>
and the specified address range includes locked, Huge TLB pages, or
<B>VM_PFNMAP</B>
pages.
<DT id="43"><B>EINVAL</B>
<DD>
<I>advice</I>
is
<B>MADV_MERGEABLE</B>
or
<B>MADV_UNMERGEABLE</B>,
but the kernel was not configured with
<B>CONFIG_KSM</B>.
<DT id="44"><B>EINVAL</B>
<DD>
<I>advice</I>
is
<B>MADV_FREE</B>
or
<B>MADV_WIPEONFORK</B>
but the specified address range includes file, Huge TLB,
<B>MAP_SHARED</B>,
or
<B>VM_PFNMAP</B>
ranges.
<DT id="45"><B>EIO</B>
<DD>
(for
<B>MADV_WILLNEED</B>)
Paging in this area would exceed the process's
maximum resident set size.
<DT id="46"><B>ENOMEM</B>
<DD>
(for
<B>MADV_WILLNEED</B>)
Not enough memory: paging in failed.
<DT id="47"><B>ENOMEM</B>
<DD>
Addresses in the specified range are not currently
mapped, or are outside the address space of the process.
<DT id="48"><B>EPERM</B>
<DD>
<I>advice</I>
is
<B>MADV_HWPOISON</B>,
but the caller does not have the
<B>CAP_SYS_ADMIN</B>
capability.
</DL>
<A NAME="lbAI">&nbsp;</A>
<H2>VERSIONS</H2>
Since Linux 3.18,
support for this system call is optional,
depending on the setting of the
<B>CONFIG_ADVISE_SYSCALLS</B>
configuration option.
<A NAME="lbAJ">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>madvise</B>()
is not specified by any standards.
Versions of this system call, implementing a wide variety of
<I>advice</I>
values, exist on many other implementations.
Other implementations typically implement at least the flags listed
above under
<I>Conventional advice flags</I>,
albeit with some variation in semantics.
<P>
POSIX.1-2001 describes
<B><A HREF="/cgi-bin/man/man2html?3+posix_madvise">posix_madvise</A></B>(3)
with constants
<B>POSIX_MADV_NORMAL</B>,
<B>POSIX_MADV_RANDOM</B>,
<B>POSIX_MADV_SEQUENTIAL</B>,
<B>POSIX_MADV_WILLNEED</B>,
and
<B>POSIX_MADV_DONTNEED</B>,
and so on, with behavior close to the similarly named flags listed above.
<A NAME="lbAK">&nbsp;</A>
<H2>NOTES</H2>
<A NAME="lbAL">&nbsp;</A>
<H3>Linux notes</H3>
The Linux implementation requires that the address
<I>addr</I>
be page-aligned, and allows
<I>length</I>
to be zero.
If there are some parts of the specified address range
that are not mapped, the Linux version of
<B>madvise</B>()
ignores them and applies the call to the rest (but returns
<B>ENOMEM</B>
from the system call, as it should).
<A NAME="lbAM">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+getrlimit">getrlimit</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+mincore">mincore</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+mprotect">mprotect</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+msync">msync</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+munmap">munmap</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+prctl">prctl</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+posix_madvise">posix_madvise</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?5+core">core</A></B>(5)
<A NAME="lbAN">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="49"><A HREF="#lbAB">NAME</A><DD>
<DT id="50"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="51"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="52"><A HREF="#lbAE">Conventional advice values</A><DD>
<DT id="53"><A HREF="#lbAF">Linux-specific advice values</A><DD>
</DL>
<DT id="54"><A HREF="#lbAG">RETURN VALUE</A><DD>
<DT id="55"><A HREF="#lbAH">ERRORS</A><DD>
<DT id="56"><A HREF="#lbAI">VERSIONS</A><DD>
<DT id="57"><A HREF="#lbAJ">CONFORMING TO</A><DD>
<DT id="58"><A HREF="#lbAK">NOTES</A><DD>
<DL>
<DT id="59"><A HREF="#lbAL">Linux notes</A><DD>
</DL>
<DT id="60"><A HREF="#lbAM">SEE ALSO</A><DD>
<DT id="61"><A HREF="#lbAN">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:33 GMT, March 31, 2021
</BODY>
</HTML>