mbind
Section: System Calls (2)
Updated: 2026-02-08
Index
JM Home Page
roff page
名前
mbind - メモリー領域に対してメモリーポリシーを設定する
ライブラリ
NUMA (Non-Uniform Memory Access) ポリシーライブラリ (libnuma,~-lnuma)
書式
#include <numaif.h>
long mbind(unsigned long size, unsigned long maxnode;
void addr[size], unsigned long size, int mode,
const unsigned long nodemask[(maxnode + ULONG_WIDTH - 1)
/ ULONG_WIDTH],
unsigned long maxnode, unsigned int flags);
説明
mbind() sets the NUMA memory policy, which consists of a policy mode and
zero or more nodes, for the memory range starting with addr and
continuing for size bytes. The memory policy defines from which node
memory is allocated.
If the memory range specified by the addr and size arguments includes
an "anonymous" region of memory---that is a region of memory created using
the mmap(2) system call with the MAP_ANONYMOUS---or a memory-mapped
file, mapped using the mmap(2) system call with the MAP_PRIVATE 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 MAP_PRIVATE, 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 mbind().
指定されたメモリー範囲内にある MAP_SHARED のマッピングでは指定されたポリシーは無視され、
ページ割り当てが発生するきっかけとなったスレッドのメモリポリシーに したがってページの割り当てが行われることになる。
繰り返しになるが、ページ割り当てのきっかけとなったスレッドは、 mbind() を呼び出したスレッドと同じとは限らない。
指定されたメモリー範囲に、 shmget(2) システムコールを使って作成されたり、 shmat(2) システムコールを使って付加
(attach) されたりした共有メモリー領域が 含まれる場合、無名メモリー領域や共有メモリー領域に対するページ割り当ては、
共有メモリーセグメントへポリシーの設定を行ったプロセスがページ割り当て のきっかけとなったかに関わらず、指定されたポリシーにしたがって割り当て
が行われる。 しかしながら、共有メモリー領域が SHM_HUGETLB フラグを指定して作成された場合には、ヒュージページ (huge page)
の割り当てが 指定されたポリシーにしたがって行われるのは、その領域に対して mbind()
を呼び出したプロセスがページ割り当てのきっかけとなった場合のみである。
デフォルトでは、 mbind() は新規のメモリー割り当てに対してのみ効果を持つ。
ポリシーが設定される前にすでに使用されている範囲内のページに対しては、 ポリシーは影響しない。 このデフォルトの動作は、以下で説明するフラグ
MPOL_MF_MOVE や MPOL_MF_MOVE_ALL により上書きされる可能性がある。
The mode argument must specify one of MPOL_DEFAULT, MPOL_BIND,
MPOL_INTERLEAVE, MPOL_WEIGHTED_INTERLEAVE, MPOL_PREFERRED,
MPOL_PREFERRED_MANY, or MPOL_LOCAL (which are described in detail
below). All policy modes except MPOL_DEFAULT require the caller to
specify the node or nodes to which the mode applies, via the nodemask
argument.
mode 引数には、追加で モードフラグ を含めることもできる。 サポートされている モードフラグ は以下の通りである。
- MPOL_F_NUMA_BALANCING (Linux 5.15 以降)
-
When mode is MPOL_BIND, enable the kernel NUMA balancing for the task
if it is supported by the kernel. If the flag isn't supported by the
kernel, or is used with mode other than MPOL_BIND, -1 is returned and
errno is set to EINVAL.
- MPOL_F_STATIC_NODES (Linux-2.6.26 以降)
-
空でない nodemask は、物理ノード ID である。 Linux では、そのスレッドが異なる CPU 集合コンテキスト (cpuset
context) に移動した場合でも、そのスレッドの現在の CPU 集合コンテキストで 許可されているノード集合が変化した場合でも、
nodemask をマッピングし直すことはない。
- MPOL_F_RELATIVE_NODES (Linux-2.6.26 以降)
-
空でない nodemask は、そのスレッドの現在の CPU 集合で許可されているノード ID 集合 における相対的なノード ID である。
nodemask は、最大で maxnode ビットから構成されるノードのビットマスクを指す。 ビットマスクの大きさは、直近の
sizeof(unsigned long) の倍数に切り上げられるが、カーネルが使用するのは maxnode 個までのビットだけである。
NULL 値の nodemask もしくは値が 0 の maxnode はノードの空集合を表す。 maxnode の値が 0 の場合、
nodemask 引数は無視される。 nodemask が必須の場面では、 nodemask に、オンラインで、そのスレッドの現在の
CPU 集合コンテキストで 許可されており (MPOL_F_STATIC_NODES モードフラグが指定されていない場合)、メモリーがあるノードが
少なくとも一つ入っていなければならない。
mode 引数には、以下の値のいずれかひとつを含めなければならない。
- MPOL_DEFAULT
-
このモードはデフォルトではないメモリーポリシーを削除し、 デフォルトの動作に戻すことを指定するものである。 mbind()
経由で、あるメモリー領域に対して MPOL_DEFAULT が適用された場合、スレッドのメモリポリシーを使用することを意味する。
スレッドのメモリポリシーは、 set_mempolicy(2) で変更されているかもしれない。 スレッドのメモリポリシーのモードも
MPOL_DEFAULT の場合、システム全体のデフォルトポリシーが使用される。 システム全体のデフォルトポリシーでは、割り当てのきっかけとなった
CPU のノードからページの割り当てを行う。 MPOL_DEFAULT では、引数 nodemask と maxnode
にノードの空集合を指定しなければならない。
- MPOL_BIND
-
This mode specifies a strict policy that restricts memory allocation to the
nodes specified in nodemask. If nodemask 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 nodemask
and so forth, until none of the specified nodes contained free memory.)
- MPOL_INTERLEAVE
-
このモードは、メモリー割り当てが nodemask に指定されたノード間で交互に行われることを指定するものである。
このポリシーでは、複数のノードにページを広げて配置し、これらのページへの メモリーアクセスを分散することで、遅延ではなく、帯域を最適化する。
効果を得るには、メモリー領域をある程度大きくすべきであり、 メモリーアクセスのパターンがかなり均一な場合でも 少なくとも 1 MB
以上にすべきである。 このモードでも、一つのページへのアクセスに関しては 一つのノードのメモリー帯域が上限となることは変わりない。
- MPOL_WEIGHTED_INTERLEAVE (Linux 6.9 以降)
-
This mode interleaves page allocations across the nodes specified in
nodemask according to the weights in
/sys/kernel/mm/mempolicy/weighted_interleave. For example, if bits 0, 2,
and 5 are set in nodemask, and the contents of
/sys/kernel/mm/mempolicy/weighted_interleave/node0,
/sys/.../node2, and /sys/.../node5 are 4, 7, and 9,
respectively, then pages in this region will be allocated on nodes 0, 2, and
5 in a 4:7:9 ratio.
- MPOL_PREFERRED
-
このモードは、割り当て時に優先されるノードを設定する。 カーネルはまず優先ノードにページ割り当てを行おうとし、
優先ノードに空きメモリーが少ない場合に他のノードに割り当てを行う。 nodemask に複数のノード ID が指定された場合は、
nodemask 内の最初のノードが優先ノードとして選択される。 引数 nodemask, maxnode
で空集合が指定された場合は、割り当てのきっかけとなった CPU のノードに メモリー割り当てが行われる。
- MPOL_PREFERRED_MANY (Linux 5.15 以降)
-
Specifies a set of nodes for allocation; see set_mempolicy(2).
- MPOL_LOCAL (Linux 3.8 以降)
-
This mode specifies "local allocation"; the memory is allocated on the node
of the CPU that triggered the allocation (the "local node"). The
nodemask and maxnode arguments must specify the empty set. If the
"local node" is low on free memory, the kernel will try to allocate memory
from other nodes. The kernel will allocate memory from the "local node"
whenever memory for this node is available. If the "local node" 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
"local node" whenever it becomes allowed by the thread's current cpuset
context. By contrast, MPOL_DEFAULT reverts to the memory policy of the
thread (which may be set via set_mempolicy(2)); that policy may be
something other than "local allocation".
flags に MPOL_MF_STRICT が 指定され、 mode が MPOL_DEFAULT でない場合、
指定されたポリシーに従っていないメモリー領域にページが存在すると、 mbind() はエラー EIO で失敗する。
If MPOL_MF_MOVE is specified in flags, 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
MPOL_MF_STRICT is also specified, then the call fails with the error
EIO if some pages could not be moved. If the MPOL_INTERLEAVE policy
was specified, pages already residing on the specified nodes will not be
moved such that they are interleaved.
If MPOL_MF_MOVE_ALL is passed in flags, 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
(CAP_SYS_NICE) to use this flag. If MPOL_MF_STRICT is also
specified, then the call fails with the error EIO if some pages could not
be moved. If the MPOL_INTERLEAVE policy was specified, pages already
residing on the specified nodes will not be moved such that they are
interleaved.
返り値
成功すると、 mbind() は 0 を返す。エラーの場合、-1 を返し、 errno にエラーを示す値を設定する。
エラー
- EFAULT
-
Part or all of the memory range specified by nodemask and maxnode
points outside your accessible address space. Or, there was an unmapped
hole in the specified memory range specified by addr and size.
- EINVAL
-
An invalid value was specified for flags or mode; or addr + size
was less than addr; or addr is not a multiple of the system page
size. Or, mode is MPOL_DEFAULT and nodemask specified a nonempty
set; or mode is MPOL_BIND or MPOL_INTERLEAVE and nodemask is
empty. Or, maxnode exceeds a kernel-imposed limit. Or, nodemask
specifies one or more node IDs that are greater than the maximum supported
node ID. Or, none of the node IDs specified by nodemask are on-line and
allowed by the thread's current cpuset context, or none of the specified
nodes contain memory. Or, the mode argument specified both
MPOL_F_STATIC_NODES and MPOL_F_RELATIVE_NODES.
- EIO
-
MPOL_MF_STRICT が指定されたが、このポリシーに従っていないノードに すでにページが存在していた。 もしくは
MPOL_MF_MOVE か MPOL_MF_MOVE_ALL が指定されたが、カーネルが指定された領域内の既存の全てのページを
移動することができなかった。
- ENOMEM
-
利用可能なカーネルメモリーが十分でなかった。
- EPERM
-
flags 引数に MPOL_MF_MOVE_ALL フラグが含まれているが、呼び出し元が CAP_SYS_NICE 特権を持たない。
標準規格
Linux.
歴史
Linux 2.6.7.
Support for huge page policy was added with Linux 2.6.16. For interleave
policy to be effective on huge page mappings the policied memory needs to be
tens of megabytes or larger.
Linux 5.7 より前では、 MPOL_MF_STRICT はヒュージページのマッピングでは無視されていた。
MPOL_MF_MOVE と MPOL_MF_MOVE_ALL は Linux 2.6.16 以降でのみ利用可能である。
注意
ライブラリによるサポートについては numa(7) を参照。
NUMA ポリシーは、 MAP_SHARED フラグが指定されてマップされたメモリーマップファイルの領域では サポートされていない。
MPOL_DEFAULT モードは、 mbind() と set_mempolicy(2) で異なる効果を持つことができる。
set_mempolicy(2) で MPOL_DEFAULT が指定された場合、そのスレッドのメモリポリシーはシステムの
デフォルトポリシー、すなわちローカルからの割り当て、に戻る。 mbind() を使ってメモリーのある領域に MPOL_DEFAULT
が指定された場合、その範囲に対してそれ以降に行われるページの割り当てでは、 set_mempolicy(2)
で設定したのと同じように、そのスレッドのメモリポリシーが適用される。 これにより、特定のメモリー領域についてだけ明示的なポリシーを削除し、
デフォルトのポリシーに「戻す」ことができる。 あるメモリー領域に対して「ローカルからの割り当て」を明示的に設定するには、 mode に
MPOL_LOCAL か MPOL_PREFERRED を指定し、 nodemask に空集合のノードを指定すればよい。 この方法は
set_mempolicy(2) でも通用する。
関連項目
get_mempolicy(2), getcpu(2), mmap(2), set_mempolicy(2),
shmat(2), shmget(2), numa(3), cpuset(7), numa(7),
numactl(8)
Index
- 名前
-
- ライブラリ
-
- 書式
-
- 説明
-
- 返り値
-
- エラー
-
- 標準規格
-
- 歴史
-
- 注意
-
- 関連項目
-
This document was created by
man2html,
using the manual pages.