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

686 lines
14 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of MBIND</TITLE>
</HEAD><BODY>
<H1>MBIND</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<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>
mbind - set memory policy for a memory range
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/numaif.h">numaif.h</A>&gt;</B>
<B>long mbind(void *</B><I>addr</I><B>, unsigned long </B><I>len</I><B>, int </B><I>mode</I><B>,</B>
<B> const unsigned long *</B><I>nodemask</I><B>, unsigned long </B><I>maxnode</I><B>,</B>
<B> unsigned </B><I>flags</I><B>);</B>
Link with <I>-lnuma</I>.
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>mbind</B>()
sets the NUMA memory policy,
which consists of a policy mode and zero or more nodes,
for the memory range starting with
<I>addr</I>
and continuing for
<I>len</I>
bytes.
The memory policy defines from which node memory is allocated.
<P>
If the memory range specified by the
<I>addr</I> and <I>len</I>
arguments includes an &quot;anonymous&quot; region of memory---that is
a region of memory created using the
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)
system call with the
<B>MAP_ANONYMOUS</B>---or
a memory-mapped file, mapped using the
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)
system call with the
<B>MAP_PRIVATE</B>
flag, pages will be allocated only according to the specified
policy when the application writes (stores) to the page.
For anonymous regions, an initial read access will use a shared
page in the kernel containing all zeros.
For a file mapped with
<B>MAP_PRIVATE</B>,
an initial read access will allocate pages according to the
memory policy of the thread that causes the page to be allocated.
This may not be the thread that called
<B>mbind</B>().
<P>
The specified policy will be ignored for any
<B>MAP_SHARED</B>
mappings in the specified memory range.
Rather the pages will be allocated according to the memory policy
of the thread that caused the page to be allocated.
Again, this may not be the thread that called
<B>mbind</B>().
<P>
If the specified memory range includes a shared memory region
created using the
<B><A HREF="/cgi-bin/man/man2html?2+shmget">shmget</A></B>(2)
system call and attached using the
<B><A HREF="/cgi-bin/man/man2html?2+shmat">shmat</A></B>(2)
system call,
pages allocated for the anonymous or shared memory region will
be allocated according to the policy specified, regardless of which
process attached to the shared memory segment causes the allocation.
If, however, the shared memory region was created with the
<B>SHM_HUGETLB</B>
flag,
the huge pages will be allocated according to the policy specified
only if the page allocation is caused by the process that calls
<B>mbind</B>()
for that region.
<P>
By default,
<B>mbind</B>()
has an effect only for new allocations; if the pages inside
the range have been already touched before setting the policy,
then the policy has no effect.
This default behavior may be overridden by the
<B>MPOL_MF_MOVE</B>
and
<B>MPOL_MF_MOVE_ALL</B>
flags described below.
<P>
The
<I>mode</I>
argument must specify one of
<B>MPOL_DEFAULT</B>,
<B>MPOL_BIND</B>,
<B>MPOL_INTERLEAVE</B>,
<B>MPOL_PREFERRED</B>,
or
<B>MPOL_LOCAL</B>
(which are described in detail below).
All policy modes except
<B>MPOL_DEFAULT</B>
require the caller to specify the node or nodes to which the mode applies,
via the
<I>nodemask</I>
argument.
<P>
The
<I>mode</I>
argument may also include an optional
<I>mode flag</I>.
The supported
<I>mode flags</I>
are:
<DL COMPACT>
<DT id="1"><B>MPOL_F_STATIC_NODES</B> (since Linux-2.6.26)
<DD>
A nonempty
<I>nodemask</I>
specifies physical node IDs.
Linux does not remap the
<I>nodemask</I>
when the thread moves to a different cpuset context,
nor when the set of nodes allowed by the thread's
current cpuset context changes.
<DT id="2"><B>MPOL_F_RELATIVE_NODES</B> (since Linux-2.6.26)
<DD>
A nonempty
<I>nodemask</I>
specifies node IDs that are relative to the set of
node IDs allowed by the thread's current cpuset.
</DL>
<P>
<I>nodemask</I>
points to a bit mask of nodes containing up to
<I>maxnode</I>
bits.
The bit mask size is rounded to the next multiple of
<I>sizeof(unsigned long)</I>,
but the kernel will use bits only up to
<I>maxnode</I>.
A NULL value of
<I>nodemask</I>
or a
<I>maxnode</I>
value of zero specifies the empty set of nodes.
If the value of
<I>maxnode</I>
is zero,
the
<I>nodemask</I>
argument is ignored.
Where a
<I>nodemask</I>
is required, it must contain at least one node that is on-line,
allowed by the thread's current cpuset context
(unless the
<B>MPOL_F_STATIC_NODES</B>
mode flag is specified),
and contains memory.
<P>
The
<I>mode</I>
argument must include one of the following values:
<DL COMPACT>
<DT id="3"><B>MPOL_DEFAULT</B>
<DD>
This mode requests that any nondefault policy be removed,
restoring default behavior.
When applied to a range of memory via
<B>mbind</B>(),
this means to use the thread memory policy,
which may have been set with
<B><A HREF="/cgi-bin/man/man2html?2+set_mempolicy">set_mempolicy</A></B>(2).
If the mode of the thread memory policy is also
<B>MPOL_DEFAULT</B>,
the system-wide default policy will be used.
The system-wide default policy allocates
pages on the node of the CPU that triggers the allocation.
For
<B>MPOL_DEFAULT</B>,
the
<I>nodemask</I>
and
<I>maxnode</I>
arguments must be specify the empty set of nodes.
<DT id="4"><B>MPOL_BIND</B>
<DD>
This mode specifies a strict policy that restricts memory allocation to
the nodes specified in
<I>nodemask</I>.
If
<I>nodemask</I>
specifies more than one node, page allocations will come from
the node with sufficient free memory that is closest to
the node where the allocation takes place.
Pages will not be allocated from any node not specified in the
IR nodemask .
(Before Linux 2.6.26,
page allocations came from
the node with the lowest numeric node ID first, until that node
contained no free memory.
Allocations then came from the node with the next highest
node ID specified in
<I>nodemask</I>
and so forth, until none of the specified nodes contained free memory.)
<DT id="5"><B>MPOL_INTERLEAVE</B>
<DD>
This mode specifies that page allocations be interleaved across the
set of nodes specified in
<I>nodemask</I>.
This optimizes for bandwidth instead of latency
by spreading out pages and memory accesses to those pages across
multiple nodes.
To be effective the memory area should be fairly large,
at least 1&nbsp;MB or bigger with a fairly uniform access pattern.
Accesses to a single page of the area will still be limited to
the memory bandwidth of a single node.
<DT id="6"><B>MPOL_PREFERRED</B>
<DD>
This mode sets the preferred node for allocation.
The kernel will try to allocate pages from this
node first and fall back to other nodes if the
preferred nodes is low on free memory.
If
<I>nodemask</I>
specifies more than one node ID, the first node in the
mask will be selected as the preferred node.
If the
<I>nodemask</I>
and
<I>maxnode</I>
arguments specify the empty set, then the memory is allocated on
the node of the CPU that triggered the allocation.
<DT id="7"><B>MPOL_LOCAL</B> (since Linux 3.8)
<DD>
This mode specifies &quot;local allocation&quot;; the memory is allocated on
the node of the CPU that triggered the allocation (the &quot;local node&quot;).
The
<I>nodemask</I>
and
<I>maxnode</I>
arguments must specify the empty set.
If the &quot;local node&quot; is low on free memory,
the kernel will try to allocate memory from other nodes.
The kernel will allocate memory from the &quot;local node&quot;
whenever memory for this node is available.
If the &quot;local node&quot; is not allowed by the thread's current cpuset context,
the kernel will try to allocate memory from other nodes.
The kernel will allocate memory from the &quot;local node&quot; whenever
it becomes allowed by the thread's current cpuset context.
By contrast,
<B>MPOL_DEFAULT</B>
reverts to the memory policy of the thread (which may be set via
<B><A HREF="/cgi-bin/man/man2html?2+set_mempolicy">set_mempolicy</A></B>(2));
that policy may be something other than &quot;local allocation&quot;.
</DL>
<P>
If
<B>MPOL_MF_STRICT</B>
is passed in
<I>flags</I>
and
<I>mode</I>
is not
<B>MPOL_DEFAULT</B>,
then the call fails with the error
<B>EIO</B>
if the existing pages in the memory range don't follow the policy.
<P>
If
<B>MPOL_MF_MOVE</B>
is specified in
<I>flags</I>,
then the kernel will attempt to move all the existing pages
in the memory range so that they follow the policy.
Pages that are shared with other processes will not be moved.
If
<B>MPOL_MF_STRICT</B>
is also specified, then the call fails with the error
<B>EIO</B>
if some pages could not be moved.
<P>
If
<B>MPOL_MF_MOVE_ALL</B>
is passed in
<I>flags</I>,
then the kernel will attempt to move all existing pages in the memory range
regardless of whether other processes use the pages.
The calling thread must be privileged
(<B>CAP_SYS_NICE</B>)
to use this flag.
If
<B>MPOL_MF_STRICT</B>
is also specified, then the call fails with the error
<B>EIO</B>
if some pages could not be moved.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>mbind</B>()
returns 0;
on error, -1 is returned and
<I>errno</I>
is set to indicate the error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="8"><B>EFAULT</B>
<DD>
Part or all of the memory range specified by
<I>nodemask</I>
and
<I>maxnode</I>
points outside your accessible address space.
Or, there was an unmapped hole in the specified memory range specified by
<I>addr</I>
and
<I>len</I>.
<DT id="9"><B>EINVAL</B>
<DD>
An invalid value was specified for
<I>flags</I>
or
<I>mode</I>;
or
<I>addr + len</I>
was less than
<I>addr</I>;
or
<I>addr</I>
is not a multiple of the system page size.
Or,
<I>mode</I>
is
<B>MPOL_DEFAULT</B>
and
<I>nodemask</I>
specified a nonempty set;
or
<I>mode</I>
is
<B>MPOL_BIND</B>
or
<B>MPOL_INTERLEAVE</B>
and
<I>nodemask</I>
is empty.
Or,
<I>maxnode</I>
exceeds a kernel-imposed limit.
Or,
<I>nodemask</I>
specifies one or more node IDs that are
greater than the maximum supported node ID.
Or, none of the node IDs specified by
<I>nodemask</I>
are on-line and allowed by the thread's current cpuset context,
or none of the specified nodes contain memory.
Or, the
<I>mode</I>
argument specified both
<B>MPOL_F_STATIC_NODES</B>
and
<B>MPOL_F_RELATIVE_NODES</B>.
<DT id="10"><B>EIO</B>
<DD>
<B>MPOL_MF_STRICT</B>
was specified and an existing page was already on a node
that does not follow the policy;
or
<B>MPOL_MF_MOVE</B>
or
<B>MPOL_MF_MOVE_ALL</B>
was specified and the kernel was unable to move all existing
pages in the range.
<DT id="11"><B>ENOMEM</B>
<DD>
Insufficient kernel memory was available.
<DT id="12"><B>EPERM</B>
<DD>
The
<I>flags</I>
argument included the
<B>MPOL_MF_MOVE_ALL</B>
flag and the caller does not have the
<B>CAP_SYS_NICE</B>
privilege.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
The
<B>mbind</B>()
system call was added to the Linux kernel in version 2.6.7.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
This system call is Linux-specific.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
For information on library support, see
<B><A HREF="/cgi-bin/man/man2html?7+numa">numa</A></B>(7).
<P>
NUMA policy is not supported on a memory-mapped file range
that was mapped with the
<B>MAP_SHARED</B>
flag.
<P>
The
<B>MPOL_DEFAULT</B>
mode can have different effects for
<B>mbind</B>()
and
<B><A HREF="/cgi-bin/man/man2html?2+set_mempolicy">set_mempolicy</A></B>(2).
When
<B>MPOL_DEFAULT</B>
is specified for
<B><A HREF="/cgi-bin/man/man2html?2+set_mempolicy">set_mempolicy</A></B>(2),
the thread's memory policy reverts to the system default policy
or local allocation.
When
<B>MPOL_DEFAULT</B>
is specified for a range of memory using
<B>mbind</B>(),
any pages subsequently allocated for that range will use
the thread's memory policy, as set by
<B><A HREF="/cgi-bin/man/man2html?2+set_mempolicy">set_mempolicy</A></B>(2).
This effectively removes the explicit policy from the
specified range, &quot;falling back&quot; to a possibly nondefault
policy.
To select explicit &quot;local allocation&quot; for a memory range,
specify a
<I>mode</I>
of
<B>MPOL_LOCAL</B>
or
<B>MPOL_PREFERRED</B>
with an empty set of nodes.
This method will work for
<B><A HREF="/cgi-bin/man/man2html?2+set_mempolicy">set_mempolicy</A></B>(2),
as well.
<P>
Support for huge page policy was added with 2.6.16.
For interleave policy to be effective on huge page mappings the
policied memory needs to be tens of megabytes or larger.
<P>
<B>MPOL_MF_STRICT</B>
is ignored on huge page mappings.
<P>
<B>MPOL_MF_MOVE</B>
and
<B>MPOL_MF_MOVE_ALL</B>
are available only on Linux 2.6.16 and later.
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+get_mempolicy">get_mempolicy</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+getcpu">getcpu</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+set_mempolicy">set_mempolicy</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+shmat">shmat</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+shmget">shmget</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+numa">numa</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+cpuset">cpuset</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+numa">numa</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?8+numactl">numactl</A></B>(8)
<A NAME="lbAK">&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="13"><A HREF="#lbAB">NAME</A><DD>
<DT id="14"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="15"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="16"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="17"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="18"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="19"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="20"><A HREF="#lbAI">NOTES</A><DD>
<DT id="21"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="22"><A HREF="#lbAK">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>