mbind(2)

๐Ÿ’– NAME ๐Ÿ’–

mbind โ€” set memory policy for a memory range โœจ

okay bestie, welcome to mbind(2), straight out of the System Calls Manual, and it's giving "I decide which physical node your memory lives on" royalty energy ๐Ÿ‘‘ let's get into it ๐ŸŒˆ

๐Ÿ“š LIBRARY ๐Ÿ“š

NUMA (Non-Uniform Memory Access) policy library โ€” that's libnuma, and you link against it with -lnuma ๐Ÿซถ

๐ŸŽ€ SYNOPSIS ๐ŸŽ€

#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);

ngl that prototype is a little unhinged ๐Ÿ’€ but we reproduce it exactly, no edits, periodt.


โœจ DESCRIPTION โœจ

mbind() sets the NUMA memory policy for the memory range that starts at addr and keeps going for size bytes. And what is a memory policy, you ask? It's a combo meal: a policy mode plus zero or more nodes. And it defines from which node memory is allocated. That's the whole vibe ๐Ÿ‘‘

If the memory range you handed it (via the addr and size arguments) includes an "anonymous" region of memory โ€” meaning a region created using the mmap(2) system call with MAP_ANONYMOUS โ€” or a memory-mapped file mapped using mmap(2) with the MAP_PRIVATE flag, then pages get allocated according to the specified policy only when the application writes (stores) to the page. Ooh and here's the tea for each case:

  • 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. And heads up, bestie: that may not be the thread that called mbind() ๐Ÿ˜ญ

The specified policy will be ignored for any MAP_SHARED mappings inside that memory range ๐Ÿ™ˆ Instead, the pages get allocated according to the memory policy of the thread that caused the page to be allocated. And again โ€” surprise! โ€” that may not be the thread that called mbind() ๐Ÿ’…

Now, if the specified memory range includes a shared memory region created with the shmget(2) system call and attached with the shmat(2) system call, then pages allocated for the anonymous or shared memory region will be allocated according to the policy you specified, no matter which process attached to the shared memory segment and caused the allocation ๐Ÿซถ But โ€” plot twist โ€” if that shared memory region was created with the SHM_HUGETLB flag, the huge pages get allocated according to the specified policy only if the page allocation is caused by the process that calls mbind() for that region โญ

By default, mbind() only has an effect on new allocations. So if the pages inside the range were already touched before you set the policy, thenโ€ฆ the policy does nothing. Tragic but true ๐Ÿ’€ That default behavior can be overridden with the MPOL_MF_MOVE and MPOL_MF_MOVE_ALL flags described below ๐Ÿ‘‡

The mode argument must specify one of MPOL_DEFAULT, MPOL_BIND, MPOL_INTERLEAVE, MPOL_WEIGHTED_INTERLEAVE, MPOL_PREFERRED, MPOL_PREFERRED_MANY, or MPOL_LOCAL (all described in detail below, don't worry bestie). And heads up: every policy mode except MPOL_DEFAULT requires the caller to specify the node or nodes the mode applies to, via the nodemask argument ๐Ÿ“Œ

The mode argument may also include an optional mode flag. The supported mode flags are:

  • MPOL_F_NUMA_BALANCING (since Linux 5.15) โ€” when mode is MPOL_BIND, this enables the kernel NUMA balancing for the task, if it is supported by the kernel. If the flag isn't supported by the kernel, or if it's used with a mode other than MPOL_BIND, then -1 is returned and errno is set to EINVAL โš ๏ธ
  • MPOL_F_STATIC_NODES (since Linux-2.6.26) โ€” a nonempty nodemask specifies physical node IDs. Linux does not remap the nodemask when the thread moves to a different cpuset context, nor when the set of nodes allowed by the thread's current cpuset context changes ๐Ÿ’…
  • MPOL_F_RELATIVE_NODES (since Linux-2.6.26) โ€” a nonempty nodemask specifies node IDs that are relative to the set of node IDs allowed by the thread's current cpuset ๐ŸŒธ

nodemask points to a bit mask of nodes containing up to maxnode bits. The bit mask size is rounded to the next multiple of sizeof(unsigned long), but the kernel will only use bits up to maxnode. A NULL value of nodemask or a maxnode value of zero specifies the empty set of nodes. If maxnode is zero, the nodemask argument is ignored ๐Ÿ™ˆ And wherever a nodemask is required, it must contain at least one node that is:

  • on-line โœ…
  • allowed by the thread's current cpuset context (unless the MPOL_F_STATIC_NODES mode flag is specified) โœ…
  • contains memory โœ…

The mode argument must include one of the following values:

  • MPOL_DEFAULT โ€” this mode requests that any nondefault policy be removed, restoring default behavior. When applied to a range of memory via mbind(), that means: use the thread memory policy, which may have been set with set_mempolicy(2). If the mode of the thread memory policy is also MPOL_DEFAULT, then the system-wide default policy is used. And the system-wide default policy allocates pages on the node of the CPU that triggers the allocation ๐ŸŽฏ For MPOL_DEFAULT, the nodemask and maxnode arguments must specify the empty set of nodes.
  • 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 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 โ€” this mode specifies that page allocations be interleaved across the set of nodes specified in nodemask ๐ŸŒˆ 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 MB or bigger โ€” with a fairly uniform access pattern. Just so you know: accesses to a single page of the area will still be limited to the memory bandwidth of a single node ๐Ÿ“‰
  • MPOL_WEIGHTED_INTERLEAVE (since 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. math ate that ๐Ÿ’…
  • MPOL_PREFERRED โ€” 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 node is low on free memory ๐Ÿฅบ If nodemask specifies more than one node ID, the first node in the mask will be selected as the preferred node. And if the nodemask and maxnode arguments specify the empty set, then the memory is allocated on the node of the CPU that triggered the allocation ๐ŸŽ€
  • MPOL_PREFERRED_MANY (since Linux 5.15) โ€” specifies a set of nodes for allocation; see set_mempolicy(2) ๐Ÿ“–
  • MPOL_LOCAL (since 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" โœจ

If MPOL_MF_STRICT is passed in flags and mode is not MPOL_DEFAULT, then the call fails with the error EIO if the existing pages in the memory range don't follow the policy ๐Ÿ’€

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. And 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 โ€” yeah, it's giving main character energy only ๐Ÿ” If MPOL_MF_STRICT is also specified, then the call fails with the error EIO if some pages could not be moved. And if the MPOL_INTERLEAVE policy was specified, pages already residing on the specified nodes will not be moved such that they are interleaved ๐Ÿ™…โ™€๏ธ


๐ŸŒŸ RETURN VALUE ๐ŸŒŸ

On success, mbind() returns 0 โœ… On error, -1 is returned and errno is set to indicate the error ๐Ÿ˜ญ

๐Ÿ’€ ERRORS ๐Ÿ’€

bestie if mbind() is mad at you, it's gonna say so, and here's literally every reason why ๐Ÿ‘‡

  • 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. (translation: you pointed at memory that isn't yours, or there's a gap in the range you handed over ๐Ÿ™ˆ)
  • EINVAL โ€” An invalid value was specified for flags or mode. Or, any of these other situations, because EINVAL is the overachiever:
    • addr + size was less than addr.
    • addr is not a multiple of the system page size.
    • mode is MPOL_DEFAULT and nodemask specified a nonempty set.
    • mode is MPOL_BIND or MPOL_INTERLEAVE and nodemask is empty.
    • maxnode exceeds a kernel-imposed limit.
    • nodemask specifies one or more node IDs that are greater than the maximum supported node ID.
    • 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.
    • the mode argument specified both MPOL_F_STATIC_NODES and MPOL_F_RELATIVE_NODES.
  • EIO โ€” MPOL_MF_STRICT was specified and an existing page was already on a node that does not follow the policy. Or, MPOL_MF_MOVE or MPOL_MF_MOVE_ALL was specified and the kernel was unable to move all existing pages in the range. (the kernel tried to relocate your pages and lowkey could not ๐Ÿ˜ญ)
  • ENOMEM โ€” Insufficient kernel memory was available. it's not you, it's the kernel being broke ๐Ÿ“
  • EPERM โ€” The flags argument included the MPOL_MF_MOVE_ALL flag and the caller does not have the CAP_SYS_NICE privilege. no cap, you need the cap ๐Ÿ’…

๐Ÿ“œ STANDARDS ๐Ÿ“œ

Linux. that's it, that's the standard, periodt โœจ


๐Ÿ•ฐ๏ธ HISTORY ๐Ÿ•ฐ๏ธ

  • Linux 2.6.7. the origin story ๐Ÿ‘‘
  • Support for huge page policy was added with Linux 2.6.16. And heads up: for interleave policy to be effective on huge page mappings, the policied memory needs to be tens of megabytes or larger. like, no tiny little ranges bestie, go big or it does nothing ๐Ÿฆ‹
  • Before Linux 5.7, MPOL_MF_STRICT was ignored on huge page mappings. (was giving nothing, honestly)
  • MPOL_MF_MOVE and MPOL_MF_MOVE_ALL are available only on Linux 2.6.16 and later.

๐Ÿ“ NOTES ๐Ÿ“

  • For information on library support, see numa(7).
  • NUMA policy is not supported on a memory-mapped file range that was mapped with the MAP_SHARED flag. nope nope nope ๐Ÿšซ
  • The MPOL_DEFAULT mode can have different effects for mbind() and set_mempolicy(2), and this is the plot twist:
    • When MPOL_DEFAULT is specified for set_mempolicy(2), the thread's memory policy reverts to the system default policy or local allocation.
    • When MPOL_DEFAULT is specified for a range of memory using mbind(), any pages subsequently allocated for that range will use the thread's memory policy, as set by set_mempolicy(2). This effectively removes the explicit policy from the specified range, "falling back" to a possibly nondefault policy.
    • To select explicit "local allocation" for a memory range, specify a mode of MPOL_LOCAL or MPOL_PREFERRED with an empty set of nodes. This method will work for set_mempolicy(2), as well.
ok so the tl;dr of that whole thing: MPOL_DEFAULT is not "do nothing", it's "fall back to whatever the thread says", and that can lowkey surprise you. if you actually want local allocation, say MPOL_LOCAL or MPOL_PREFERRED with an empty node set. iykyk ๐Ÿ’–

๐Ÿ”— SEE ALSO ๐Ÿ”—


okay that's the whole page bestie, you ate it ๐Ÿ’…โœจ this version came to you from Linux man-pages 6.20, dated 2026-02-08 โ€” go forth and bind your memory correctly, no cap ๐Ÿซถ๐Ÿ’–

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft-var
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms