subset zonelists and big numa friendly mempolicy MPOL_MBIND
Subsystems (5)
PATCH 1/1 subset zonelists and big numa friendly mempolicy MPOL_MBIND
I hope that this patch will end up in *-mm soon.
But not yet -- first it must be reviewed by Andi Kleen, the mempolicy author, and whomever else would like to comment.
This patch adds support for subset_zonelists. These are zonelists suitable for passing to __alloc_pages() for allocations that are restricted to some subset of the systems memory nodes.
This patch also modifies the MPOL_BIND code in mm/mempolicy.c to use these subset_zonelists. This replaces an earlier, simpler, implementation of such restricted zonelists that was in mm/mempolicy.c.
The current implementation provides a single list, sorted in node order, for all nodes in the subset. This is numa unfriendly when used on sets of several memory nodes. It loads up the first node in the list, not trying the cpu local node first, and pays no regard to distance between nodes (not trying near nodes before far nodes).
This new implementation preserves the "first-touch" preference (allocate on the node currently executing on, if that node is in the allowed subset). It also preserves the zonelist ordering that favors nearer nodes over further nodes on large NUMA systems. And it removes the mempolicy restriction that only the highest zone in the zone hierarchy is policied.
For smaller (a few nodes) dedicated application servers, this is probably not essential. But for larger (hundreds of nodes) NUMA systems, running several simultaneous jobs in disjoint node subsets of dozens or hundreds of nodes each, the numa friendly placement of memory within each subset is critical to system performance.
The bulk of the code for constructing subset zonelists is moved into mm/page_alloc.c, where other zonelist management code lives. The code goes to some length to keep the data representation of a subset zonelist small. On systems where all memory is GFP_DMA, the system wide zonelists off each nodes NODE_DATA are inflated about 9x larger than necessary. For the initial zonelists created in the restricted context of boottime, this is probably not worth worrying about. But for dozens or hundreds of subset zonelists that are dynamically created in a large system (one each call to mbind or mempolicy requesting MPOL_BIND), it is worthwhile to compress the zonelists to a more compact representation. The code to compress the zonelists (removing unused and duplicate entries) is arch-neutral, NUMA-only, not performance critical and fairly isolated. So it won't impact non-NUMA kernel text size, and once it's working, it should be low maintenance.
This version of the patch is against 2.6.8-rc2-mm1. It has been compiled, booted and passed a simple test for sn2_defconfig (ia64 arch, SN2 hardware).
include/linux/mempolicy.h | 2 include/linux/mmzone.h | 40 +++++ mm/mempolicy.c | 99 ++++---------- mm/page_alloc.c | 313 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 383 insertions(+), 71 deletions(-)
include/linux/mempolicy.h +1 -1
@@ -61,7 +61,7 @@ struct mempolicy {atomic_t refcnt;short policy;/* See MPOL_* above */ union { -struct zonelist *zonelist;/* bind */ +struct subset_zonelists *zonelists;/* bind */ short preferred_node;/* preferred */ DECLARE_BITMAP(nodes, MAX_NUMNODES); /* interleave *//* undefined for default */
include/linux/mmzone.h +40 -0
@@ -11,6 +11,7 @@#include <linux/cache.h> #include <linux/threads.h> #include <linux/numa.h> +#include <linux/kref.h> #include <asm/atomic.h> /* Free memory management - zoned buddy allocator. */
@@ -237,6 +238,45 @@ struct zonelist {struct zone *zones[MAX_NUMNODES *MAX_NR_ZONES +1 ];// NULL delimited }; +#ifdef CONFIG_NUMA + +/* +* Subset_zonelists are used when allocating memory from a restricted +* set of Memory Nodes. They provide for each node in the set +* a zonelist containing only the zones on the nodes in the set. +* These restricted zonelists are extracted from the system-wide +* NODE_DATA node_zonelists, preserving order. +* +* Subset_zonelists are used for running jobs on a strictly limited +* subsets of a systems entire available memory (e.g. MPOL_MBIND). +* +* The 'zones' field points to a linear list of zonelists, packed one +* after the other, each NULL terminated. The 'cg2z' (for "cpu gfp +* to zone map" field is a kmalloc'd 2-D array cg2z[cpu][gfp_type] +* of indices into 'zones', where the zonelist to use for faults on +* a given 'cpu' of 'gfp_type' begins at zone + cg2z[cpu][gfp_type]. +* +* The special case of zones == cg2z == NULL is used to represent +* the set of all nodes. Use the normal system-wide NODE_DATA +* node_zonelists directly in that case. +* +* The 'nodes' field caches a bitmap of the nodes in the subset. +* +* These can be big and they're shared and read-only once built, so +* 'ref' field provides a reference count. On release, kfree 'cg2z' +* and 'zones'. +*/ + +struct subset_zonelists { +struct zone **zones; +int (*cg2z)[GFP_ZONETYPES ]; +struct kref ref; + DECLARE_BITMAP(nodes, MAX_NUMNODES); +}; + +struct subset_zonelists *build_subset_zonelists (const unsigned long *nodes); + +#endif /* * The pg_data_t structure is used in machines with CONFIG_DISCONTIGMEM
mm/mempolicy.c +29 -75
182 lines not on this page · raw message
mm/page_alloc.c +314 -0
320 lines not on this page · raw message
Signature · 3 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.650.933.1373Quoted 1 line · On Mon, 2 Aug 2004 16:35:06 -0700 (PDT) Paul Jackson <[email protected]> wrote:
I hope that this patch will end up in *-mm soon.
[...]
That's a *lot* of complexity and overhead you add. I hope it is really worth it. I suppose you need this for your cpu memset stuff, right? On a large machine it will be quite cache blowing too.
What's the worst case memory usage of all this?
And do you have any benchmarks that show that it is worth it?
My first reaction that if you really want to do that, just pass the policy node bitmap to alloc_pages and try_to_free_pages and use the normal per node zone list with the bitmap as filter. This way you would get the same effect with a lot less complexity and only slightly more overhead in the fallback case.
For the simple libnuma it was still good enough to construct the zones without core changes, but for such complex things it is better to attack the core of things.
-Andi
Thanks for your quick reply, Andi.
Quoted 2 lines
I suppose you need this for your cpu memset stuff, right? On a large machine it will be quite cache blowing too.
Yes - subset zonelists will also be used in Simon Derr's and my cpuset stuff. As to cache blowing, actually I'd hope not. Once a subset zonelist is setup, it shouldn't hit anymore cache lines during normal use than the system NODE_DATA zonelists. Normal __alloc_pages() use succeeds with the first zone pointer in the list, in any case.
Quoted 1 line
What's the worst case memory usage of all this?
They are N-squared, in the number of zones. On a system with only GFP_DMA memory, for a job bound to say 8 nodes, thats 72 (8 * 8, plus 8 NULL's) zone pointers, plus a linear array of 3 integer gfp indices per CPU. If someone tries to bind something to 254 out of 256 nodes on a big system, then, yes, it eats about 0.5 Mbytes of space. They are shared and kref'd. Of course, on such a system, the NODE_DATA zone lists are consuming perhaps 6 Mbytes, if my math is right. This worst case 0.5 Mbytes could be trimmed by an order of magnitude, by cutting off the tails of each zonelist (give up the search after 16 nodes, say) -- I had that coded, but removed it because I didn't figure it was worth it.
Basically, on these big numa boxes, the jobs don't overlap - they get dedicated resources. So the worst case is one job using _almost_ the entire machine, needing a single 0.5 Mbyte subset zonelist (on a 256 node, GFP_DMA only, system). Next worst is two jobs, half the machine each, needing two subset zonelists of 0.13 Mbytes each (0.25 Mb total). The more jobs, the smaller each is, and the less the overall space.
When someone buys a 512 CPU, 256 node system to run a single dedicated job, 0.5 Mbytes of memory to get the right numa placement is no problem.
Quoted 3 lines
My first reaction that if you really want to do that, just pass the policy node bitmap to
alloc_pagesandtry_to_free_pagesand use the normal per node zone list with the bitmap as filter.
Yes - you're right - that is the alternative. And it's what occurred to me first, as well.
It's what I coded in SGI's 2.4 kernel cpumemset patches. It has poor cache performance on big iron. For a modest job on a big system, the allocator has to walk down an average of 128 out of 256 zone pointers in the list, derefencing each one into the zone struct, then into the struct pglist_data, before it finds one that matches an allowed node id. That's a nasty memory footprint for a hot code path.
Passing in a bitmap node filter to __alloc_pages() is also more intrusive into the core allocator code. That's one of the reasons that I liked your approach, of preparing custom zonelists to pass in to __alloc_pages(). It was less intrusive on the higher maintenance __alloc_pages() code, so should be easier to maintain.
Either way, passing in a bitmap node filter to __alloc_pages, or passing a custom zonelist, the memory policy represented by MPOL_BIND is critical to our big iron. We must restrict jobs from using memory outside of their allowed set. And either way, _within_ that set, we must follow numa friendly placement, trying to allocate on the current node first, and then on the closest neighbors next. And either way, the cpuset work of Simon and myself will make heavy use of this facility.
So - yes - complexity. But very isolated complexity. The coding of your mm/mempolicy.c got a bit simpler, and no change at all is made to __alloc_pages(). Just a single new 4 element data structure is added, and just one new external routine - build_subset_zonelists(), which has simple semantics - return a custom zonelist for the requested nodes. All the complexity is isolated on a cold code path, arch-neutral and NUMA only, behind a simple API.
Signature · 3 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.650.933.1373Quoted 5 lines · I (pj)
It has poor cache performance on big iron. For a modest job on a big system, the allocator has to walk down an average of 128 out of 256 zone pointers in the list, derefencing each one into the zone struct, then into the struct
pglist_data, before it finds one that matches an allowed node id. That's a nasty memory footprint for a hot code path.
This paragraph is B.S. Most tasks are running on CPUs that are on nodes whose memory they are allowed to use. That node is at the front of the local zonelist, and they get their memory on the first node they look.
Damn ... hate it when that happens ;).
Still, either MPOL_BIND needs a more numa friendly set of zonelists having a differently sorted list for each node in the set, or it's usefulness for binding to more than one or a few very close nodes, if you care about memory performance, falls off quickly as the number of nodes increases. As you well know, any such numa-friendly set of sorted zonelists will require space on the Order of N**2, for N the node count, given the NULL-terminated linear list form in which they must be handed to __alloc_pages.
I suspect that the English phrase you are searching for now to tell me is "if it hurts, don't use it ;)." That is, you are clearly advising me not to use MPOL_BIND if I need a fancy zonelist sort.
The place I ran into the most complexity doing this in the 2.4 kernel was in the per-memory region binding. You're dealing with this in the 2.6 kernels, and when you get to stuff like shared memory and huge pages, it's not easy. At least the vma splitting code is better in
2.6 than it was in 2.4. Whatever I do for cpusets must _not_ duplicate
your virtual address range specific work (mbind). Too much detail to be done twice.
Quoted 3 lines · Andi
My first reaction that if you really want to do that, just pass the policy node bitmap to
alloc_pagesandtry_to_free_pagesand use the normal per node zone list with the bitmap as filter.
Pass in, or add to task_struct? I can imagine adding a:
nodemask_t mems_allowed;
to task_struct, and ending up with a CONFIG_CPUSET enabled macro called in a few places in __alloc_pages() and try_to_free_pages() that amounts to:
if (!in_interrupt()) if (!node_isset(z->zone_pgdat->node_id, current->mems_allowed)) continue;
In any event, cpusets provides the larger "container" on bigger numa systems, and mbind/mempolicy provides the more detailed, and vma specific, placement within the container (or within the entire system if cpusets not configured).
I'll try coding this up and see how it looks.
I welcome your further comments.
Thank-you.
Signature · 3 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.650.933.1373Quoted 3 lines · Andi
My first reaction that if you really want to do that, just pass the policy node bitmap to
alloc_pagesandtry_to_free_pagesand use the normal per node zone list with the bitmap as filter.
I have coded your suggestion up, and indeed, it is much simpler
I added an optional (#ifdef CONFIG_CPUSET) "mems_allowed" field to the task_struct, and filter zones on this mask (if not in_interrupt()) in the page allocation and free'ing code.
Thank-you. I think you're right, and it's the way to go.
I will be posting a full cpuset patch that includes this change on lse-tech and lkml, hopefully in a few hours. I will be most grateful for any further feedback.
Providing support for dividing very large systems into subsets of nodes (cpu and memory), in order to obtain good memory locality and good isolation of big jobs from each other, is essential to the success of certain high end Linux users.
Signature · 3 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.650.933.1373