Skip to content

Commit 7c0caeb

Browse files
htejunH. Peter Anvin
authored andcommitted
memblock: Add optional region->nid
From 83103b92f3234ec830852bbc5c45911bd6cbdb20 Mon Sep 17 00:00:00 2001 From: Tejun Heo <tj@kernel.org> Date: Thu, 14 Jul 2011 11:22:16 +0200 Add optional region->nid which can be enabled by arch using CONFIG_HAVE_MEMBLOCK_NODE_MAP. When enabled, memblock also carries NUMA node information and replaces early_node_map[]. Newly added memblocks have MAX_NUMNODES as nid. Arch can then call memblock_set_node() to set node information. memblock takes care of merging and node affine allocations w.r.t. node information. When MEMBLOCK_NODE_MAP is enabled, early_node_map[], related data structures and functions to manipulate and iterate it are disabled. memblock version of __next_mem_pfn_range() is provided such that for_each_mem_pfn_range() behaves the same and its users don't have to be updated. -v2: Yinghai spotted section mismatch caused by missing __init_memblock in memblock_set_node(). Fixed. Signed-off-by: Tejun Heo <tj@kernel.org> Link: http://lkml.kernel.org/r/20110714094342.GF3455@htj.dyndns.org Cc: Yinghai Lu <yinghai@kernel.org> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
1 parent 67e24bc commit 7c0caeb

5 files changed

Lines changed: 183 additions & 37 deletions

File tree

‎include/linux/memblock.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
struct memblock_region {
2525
phys_addr_t base;
2626
phys_addr_t size;
27+
#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
28+
int nid;
29+
#endif
2730
};
2831

2932
struct memblock_type {
@@ -58,6 +61,29 @@ extern long memblock_remove(phys_addr_t base, phys_addr_t size);
5861
extern long memblock_free(phys_addr_t base, phys_addr_t size);
5962
extern long memblock_reserve(phys_addr_t base, phys_addr_t size);
6063

64+
#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
65+
extern int memblock_set_node(phys_addr_t base, phys_addr_t size, int nid);
66+
67+
static inline void memblock_set_region_node(struct memblock_region *r, int nid)
68+
{
69+
r->nid = nid;
70+
}
71+
72+
static inline int memblock_get_region_node(const struct memblock_region *r)
73+
{
74+
return r->nid;
75+
}
76+
#else
77+
static inline void memblock_set_region_node(struct memblock_region *r, int nid)
78+
{
79+
}
80+
81+
static inline int memblock_get_region_node(const struct memblock_region *r)
82+
{
83+
return 0;
84+
}
85+
#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
86+
6187
/* The numa aware allocator is only available if
6288
* CONFIG_ARCH_POPULATES_NODE_MAP is set
6389
*/

‎include/linux/mm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,12 +1307,14 @@ extern void free_area_init_node(int nid, unsigned long * zones_size,
13071307
* CONFIG_ARCH_POPULATES_NODE_MAP
13081308
*/
13091309
extern void free_area_init_nodes(unsigned long *max_zone_pfn);
1310+
#ifndef CONFIG_HAVE_MEMBLOCK_NODE_MAP
13101311
extern void add_active_range(unsigned int nid, unsigned long start_pfn,
13111312
unsigned long end_pfn);
13121313
extern void remove_active_range(unsigned int nid, unsigned long start_pfn,
13131314
unsigned long end_pfn);
13141315
extern void remove_all_active_ranges(void);
13151316
void sort_node_map(void);
1317+
#endif
13161318
unsigned long node_map_pfn_alignment(void);
13171319
unsigned long __absent_pages_in_range(int nid, unsigned long start_pfn,
13181320
unsigned long end_pfn);

‎mm/Kconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ config SPARSEMEM_VMEMMAP
131131
config HAVE_MEMBLOCK
132132
boolean
133133

134+
config HAVE_MEMBLOCK_NODE_MAP
135+
boolean
136+
134137
# eventually, we can have this option just 'select SPARSEMEM'
135138
config MEMORY_HOTPLUG
136139
bool "Allow for memory hot-add"

‎mm/memblock.c‎

Lines changed: 124 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,16 @@ int __init_memblock memblock_reserve_reserved_regions(void)
161161

162162
static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
163163
{
164-
unsigned long i;
165-
166-
for (i = r; i < type->cnt - 1; i++) {
167-
type->regions[i].base = type->regions[i + 1].base;
168-
type->regions[i].size = type->regions[i + 1].size;
169-
}
164+
memmove(&type->regions[r], &type->regions[r + 1],
165+
(type->cnt - (r + 1)) * sizeof(type->regions[r]));
170166
type->cnt--;
171167

172168
/* Special case for empty arrays */
173169
if (type->cnt == 0) {
174170
type->cnt = 1;
175171
type->regions[0].base = 0;
176172
type->regions[0].size = 0;
173+
memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
177174
}
178175
}
179176

@@ -266,7 +263,9 @@ static void __init_memblock memblock_merge_regions(struct memblock_type *type)
266263
struct memblock_region *this = &type->regions[i];
267264
struct memblock_region *next = &type->regions[i + 1];
268265

269-
if (this->base + this->size != next->base) {
266+
if (this->base + this->size != next->base ||
267+
memblock_get_region_node(this) !=
268+
memblock_get_region_node(next)) {
270269
BUG_ON(this->base + this->size > next->base);
271270
i++;
272271
continue;
@@ -290,14 +289,15 @@ static void __init_memblock memblock_merge_regions(struct memblock_type *type)
290289
*/
291290
static void __init_memblock memblock_insert_region(struct memblock_type *type,
292291
int idx, phys_addr_t base,
293-
phys_addr_t size)
292+
phys_addr_t size, int nid)
294293
{
295294
struct memblock_region *rgn = &type->regions[idx];
296295

297296
BUG_ON(type->cnt >= type->max);
298297
memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
299298
rgn->base = base;
300299
rgn->size = size;
300+
memblock_set_region_node(rgn, nid);
301301
type->cnt++;
302302
}
303303

@@ -327,6 +327,7 @@ static long __init_memblock memblock_add_region(struct memblock_type *type,
327327
WARN_ON(type->cnt != 1);
328328
type->regions[0].base = base;
329329
type->regions[0].size = size;
330+
memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
330331
return 0;
331332
}
332333
repeat:
@@ -355,7 +356,7 @@ static long __init_memblock memblock_add_region(struct memblock_type *type,
355356
nr_new++;
356357
if (insert)
357358
memblock_insert_region(type, i++, base,
358-
rbase - base);
359+
rbase - base, MAX_NUMNODES);
359360
}
360361
/* area below @rend is dealt with, forget about it */
361362
base = min(rend, end);
@@ -365,7 +366,8 @@ static long __init_memblock memblock_add_region(struct memblock_type *type,
365366
if (base < end) {
366367
nr_new++;
367368
if (insert)
368-
memblock_insert_region(type, i, base, end - base);
369+
memblock_insert_region(type, i, base, end - base,
370+
MAX_NUMNODES);
369371
}
370372

371373
/*
@@ -459,6 +461,101 @@ long __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
459461
return memblock_add_region(_rgn, base, size);
460462
}
461463

464+
#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
465+
/*
466+
* Common iterator interface used to define for_each_mem_range().
467+
*/
468+
void __init_memblock __next_mem_pfn_range(int *idx, int nid,
469+
unsigned long *out_start_pfn,
470+
unsigned long *out_end_pfn, int *out_nid)
471+
{
472+
struct memblock_type *type = &memblock.memory;
473+
struct memblock_region *r;
474+
475+
while (++*idx < type->cnt) {
476+
r = &type->regions[*idx];
477+
478+
if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
479+
continue;
480+
if (nid == MAX_NUMNODES || nid == r->nid)
481+
break;
482+
}
483+
if (*idx >= type->cnt) {
484+
*idx = -1;
485+
return;
486+
}
487+
488+
if (out_start_pfn)
489+
*out_start_pfn = PFN_UP(r->base);
490+
if (out_end_pfn)
491+
*out_end_pfn = PFN_DOWN(r->base + r->size);
492+
if (out_nid)
493+
*out_nid = r->nid;
494+
}
495+
496+
/**
497+
* memblock_set_node - set node ID on memblock regions
498+
* @base: base of area to set node ID for
499+
* @size: size of area to set node ID for
500+
* @nid: node ID to set
501+
*
502+
* Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
503+
* Regions which cross the area boundaries are split as necessary.
504+
*
505+
* RETURNS:
506+
* 0 on success, -errno on failure.
507+
*/
508+
int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
509+
int nid)
510+
{
511+
struct memblock_type *type = &memblock.memory;
512+
phys_addr_t end = base + size;
513+
int i;
514+
515+
/* we'll create at most two more regions */
516+
while (type->cnt + 2 > type->max)
517+
if (memblock_double_array(type) < 0)
518+
return -ENOMEM;
519+
520+
for (i = 0; i < type->cnt; i++) {
521+
struct memblock_region *rgn = &type->regions[i];
522+
phys_addr_t rbase = rgn->base;
523+
phys_addr_t rend = rbase + rgn->size;
524+
525+
if (rbase >= end)
526+
break;
527+
if (rend <= base)
528+
continue;
529+
530+
if (rbase < base) {
531+
/*
532+
* @rgn intersects from below. Split and continue
533+
* to process the next region - the new top half.
534+
*/
535+
rgn->base = base;
536+
rgn->size = rend - rgn->base;
537+
memblock_insert_region(type, i, rbase, base - rbase,
538+
rgn->nid);
539+
} else if (rend > end) {
540+
/*
541+
* @rgn intersects from above. Split and redo the
542+
* current region - the new bottom half.
543+
*/
544+
rgn->base = end;
545+
rgn->size = rend - rgn->base;
546+
memblock_insert_region(type, i--, rbase, end - rbase,
547+
rgn->nid);
548+
} else {
549+
/* @rgn is fully contained, set ->nid */
550+
rgn->nid = nid;
551+
}
552+
}
553+
554+
memblock_merge_regions(type);
555+
return 0;
556+
}
557+
#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
558+
462559
phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
463560
{
464561
phys_addr_t found;
@@ -689,19 +786,26 @@ void __init_memblock memblock_set_current_limit(phys_addr_t limit)
689786
memblock.current_limit = limit;
690787
}
691788

692-
static void __init_memblock memblock_dump(struct memblock_type *region, char *name)
789+
static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
693790
{
694791
unsigned long long base, size;
695792
int i;
696793

697-
pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
698-
699-
for (i = 0; i < region->cnt; i++) {
700-
base = region->regions[i].base;
701-
size = region->regions[i].size;
794+
pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
702795

703-
pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes\n",
704-
name, i, base, base + size - 1, size);
796+
for (i = 0; i < type->cnt; i++) {
797+
struct memblock_region *rgn = &type->regions[i];
798+
char nid_buf[32] = "";
799+
800+
base = rgn->base;
801+
size = rgn->size;
802+
#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
803+
if (memblock_get_region_node(rgn) != MAX_NUMNODES)
804+
snprintf(nid_buf, sizeof(nid_buf), " on node %d",
805+
memblock_get_region_node(rgn));
806+
#endif
807+
pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
808+
name, i, base, base + size - 1, size, nid_buf);
705809
}
706810
}
707811

@@ -759,11 +863,13 @@ void __init memblock_init(void)
759863
*/
760864
memblock.memory.regions[0].base = 0;
761865
memblock.memory.regions[0].size = 0;
866+
memblock_set_region_node(&memblock.memory.regions[0], MAX_NUMNODES);
762867
memblock.memory.cnt = 1;
763868

764869
/* Ditto. */
765870
memblock.reserved.regions[0].base = 0;
766871
memblock.reserved.regions[0].size = 0;
872+
memblock_set_region_node(&memblock.reserved.regions[0], MAX_NUMNODES);
767873
memblock.reserved.cnt = 1;
768874

769875
memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;

‎mm/page_alloc.c‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -182,28 +182,31 @@ static unsigned long __meminitdata nr_all_pages;
182182
static unsigned long __meminitdata dma_reserve;
183183

184184
#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
185-
/*
186-
* MAX_ACTIVE_REGIONS determines the maximum number of distinct
187-
* ranges of memory (RAM) that may be registered with add_active_range().
188-
* Ranges passed to add_active_range() will be merged if possible
189-
* so the number of times add_active_range() can be called is
190-
* related to the number of nodes and the number of holes
191-
*/
192-
#ifdef CONFIG_MAX_ACTIVE_REGIONS
193-
/* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
194-
#define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
195-
#else
196-
#if MAX_NUMNODES >= 32
197-
/* If there can be many nodes, allow up to 50 holes per node */
198-
#define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
185+
#ifndef CONFIG_HAVE_MEMBLOCK_NODE_MAP
186+
/*
187+
* MAX_ACTIVE_REGIONS determines the maximum number of distinct ranges
188+
* of memory (RAM) that may be registered with add_active_range().
189+
* Ranges passed to add_active_range() will be merged if possible so
190+
* the number of times add_active_range() can be called is related to
191+
* the number of nodes and the number of holes
192+
*/
193+
#ifdef CONFIG_MAX_ACTIVE_REGIONS
194+
/* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
195+
#define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
199196
#else
200-
/* By default, allow up to 256 distinct regions */
201-
#define MAX_ACTIVE_REGIONS 256
197+
#if MAX_NUMNODES >= 32
198+
/* If there can be many nodes, allow up to 50 holes per node */
199+
#define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
200+
#else
201+
/* By default, allow up to 256 distinct regions */
202+
#define MAX_ACTIVE_REGIONS 256
203+
#endif
202204
#endif
203-
#endif
204205

205-
static struct node_active_region __meminitdata early_node_map[MAX_ACTIVE_REGIONS];
206-
static int __meminitdata nr_nodemap_entries;
206+
static struct node_active_region __meminitdata early_node_map[MAX_ACTIVE_REGIONS];
207+
static int __meminitdata nr_nodemap_entries;
208+
#endif /* !CONFIG_HAVE_MEMBLOCK_NODE_MAP */
209+
207210
static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
208211
static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
209212
static unsigned long __initdata required_kernelcore;
@@ -4268,6 +4271,7 @@ static inline void setup_nr_node_ids(void)
42684271
}
42694272
#endif
42704273

4274+
#ifndef CONFIG_HAVE_MEMBLOCK_NODE_MAP
42714275
/*
42724276
* Common iterator interface used to define for_each_mem_pfn_range().
42734277
*/
@@ -4456,6 +4460,11 @@ void __init sort_node_map(void)
44564460
sizeof(struct node_active_region),
44574461
cmp_node_active_region, NULL);
44584462
}
4463+
#else /* !CONFIG_HAVE_MEMBLOCK_NODE_MAP */
4464+
static inline void sort_node_map(void)
4465+
{
4466+
}
4467+
#endif
44594468

44604469
/**
44614470
* node_map_pfn_alignment - determine the maximum internode alignment

0 commit comments

Comments
 (0)