Commit bbc7b92e authored by Franck Bui-Huu's avatar Franck Bui-Huu Committed by Linus Torvalds

[PATCH] bootmem: use pfn/page conversion macros

It also creates get_mapsize() helper in order to make the code more readable
when it calculates the boot bitmap size.
Signed-off-by: default avatarFranck Bui-Huu <vagabon.xyz@gmail.com>
Cc: Dave Hansen <haveblue@us.ibm.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent e786e86a
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
* system memory and memory holes as well. * system memory and memory holes as well.
*/ */
#include <linux/init.h> #include <linux/init.h>
#include <linux/pfn.h>
#include <linux/bootmem.h> #include <linux/bootmem.h>
#include <linux/module.h> #include <linux/module.h>
...@@ -68,6 +69,18 @@ static void __init link_bootmem(bootmem_data_t *bdata) ...@@ -68,6 +69,18 @@ static void __init link_bootmem(bootmem_data_t *bdata)
return; return;
} }
/*
* Given an initialised bdata, it returns the size of the boot bitmap
*/
static unsigned long __init get_mapsize(bootmem_data_t *bdata)
{
unsigned long mapsize;
unsigned long start = PFN_DOWN(bdata->node_boot_start);
unsigned long end = bdata->node_low_pfn;
mapsize = ((end - start) + 7) / 8;
return ALIGN(mapsize, sizeof(long));
}
/* /*
* Called once to set up the allocator itself. * Called once to set up the allocator itself.
...@@ -76,11 +89,10 @@ static unsigned long __init init_bootmem_core (pg_data_t *pgdat, ...@@ -76,11 +89,10 @@ static unsigned long __init init_bootmem_core (pg_data_t *pgdat,
unsigned long mapstart, unsigned long start, unsigned long end) unsigned long mapstart, unsigned long start, unsigned long end)
{ {
bootmem_data_t *bdata = pgdat->bdata; bootmem_data_t *bdata = pgdat->bdata;
unsigned long mapsize = ((end - start)+7)/8; unsigned long mapsize;
mapsize = ALIGN(mapsize, sizeof(long)); bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
bdata->node_bootmem_map = phys_to_virt(mapstart << PAGE_SHIFT); bdata->node_boot_start = PFN_PHYS(start);
bdata->node_boot_start = (start << PAGE_SHIFT);
bdata->node_low_pfn = end; bdata->node_low_pfn = end;
link_bootmem(bdata); link_bootmem(bdata);
...@@ -88,6 +100,7 @@ static unsigned long __init init_bootmem_core (pg_data_t *pgdat, ...@@ -88,6 +100,7 @@ static unsigned long __init init_bootmem_core (pg_data_t *pgdat,
* Initially all pages are reserved - setup_arch() has to * Initially all pages are reserved - setup_arch() has to
* register free RAM areas explicitly. * register free RAM areas explicitly.
*/ */
mapsize = get_mapsize(bdata);
memset(bdata->node_bootmem_map, 0xff, mapsize); memset(bdata->node_bootmem_map, 0xff, mapsize);
return mapsize; return mapsize;
...@@ -101,20 +114,19 @@ static unsigned long __init init_bootmem_core (pg_data_t *pgdat, ...@@ -101,20 +114,19 @@ static unsigned long __init init_bootmem_core (pg_data_t *pgdat,
static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long addr, static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
unsigned long size) unsigned long size)
{ {
unsigned long sidx, eidx;
unsigned long i; unsigned long i;
/* /*
* round up, partially reserved pages are considered * round up, partially reserved pages are considered
* fully reserved. * fully reserved.
*/ */
unsigned long sidx = (addr - bdata->node_boot_start)/PAGE_SIZE;
unsigned long eidx = (addr + size - bdata->node_boot_start +
PAGE_SIZE-1)/PAGE_SIZE;
unsigned long end = (addr + size + PAGE_SIZE-1)/PAGE_SIZE;
BUG_ON(!size); BUG_ON(!size);
BUG_ON(sidx >= eidx); BUG_ON(PFN_DOWN(addr) >= bdata->node_low_pfn);
BUG_ON((addr >> PAGE_SHIFT) >= bdata->node_low_pfn); BUG_ON(PFN_UP(addr + size) > bdata->node_low_pfn);
BUG_ON(end > bdata->node_low_pfn);
sidx = PFN_DOWN(addr - bdata->node_boot_start);
eidx = PFN_UP(addr + size - bdata->node_boot_start);
for (i = sidx; i < eidx; i++) for (i = sidx; i < eidx; i++)
if (test_and_set_bit(i, bdata->node_bootmem_map)) { if (test_and_set_bit(i, bdata->node_bootmem_map)) {
...@@ -127,18 +139,15 @@ static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long add ...@@ -127,18 +139,15 @@ static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long add
static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
unsigned long size) unsigned long size)
{ {
unsigned long sidx, eidx;
unsigned long i; unsigned long i;
unsigned long start;
/* /*
* round down end of usable mem, partially free pages are * round down end of usable mem, partially free pages are
* considered reserved. * considered reserved.
*/ */
unsigned long sidx;
unsigned long eidx = (addr + size - bdata->node_boot_start)/PAGE_SIZE;
unsigned long end = (addr + size)/PAGE_SIZE;
BUG_ON(!size); BUG_ON(!size);
BUG_ON(end > bdata->node_low_pfn); BUG_ON(PFN_DOWN(addr + size) > bdata->node_low_pfn);
if (addr < bdata->last_success) if (addr < bdata->last_success)
bdata->last_success = addr; bdata->last_success = addr;
...@@ -146,8 +155,8 @@ static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, ...@@ -146,8 +155,8 @@ static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
/* /*
* Round up the beginning of the address. * Round up the beginning of the address.
*/ */
start = (addr + PAGE_SIZE-1) / PAGE_SIZE; sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
sidx = start - (bdata->node_boot_start/PAGE_SIZE); eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
for (i = sidx; i < eidx; i++) { for (i = sidx; i < eidx; i++) {
if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map))) if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
...@@ -173,7 +182,7 @@ __alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size, ...@@ -173,7 +182,7 @@ __alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
unsigned long align, unsigned long goal, unsigned long limit) unsigned long align, unsigned long goal, unsigned long limit)
{ {
unsigned long offset, remaining_size, areasize, preferred; unsigned long offset, remaining_size, areasize, preferred;
unsigned long i, start = 0, incr, eidx, end_pfn = bdata->node_low_pfn; unsigned long i, start = 0, incr, eidx, end_pfn;
void *ret; void *ret;
if(!size) { if(!size) {
...@@ -185,23 +194,22 @@ __alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size, ...@@ -185,23 +194,22 @@ __alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
if (limit && bdata->node_boot_start >= limit) if (limit && bdata->node_boot_start >= limit)
return NULL; return NULL;
limit >>=PAGE_SHIFT; end_pfn = bdata->node_low_pfn;
limit = PFN_DOWN(limit);
if (limit && end_pfn > limit) if (limit && end_pfn > limit)
end_pfn = limit; end_pfn = limit;
eidx = end_pfn - (bdata->node_boot_start >> PAGE_SHIFT); eidx = end_pfn - PFN_DOWN(bdata->node_boot_start);
offset = 0; offset = 0;
if (align && if (align && (bdata->node_boot_start & (align - 1UL)) != 0)
(bdata->node_boot_start & (align - 1UL)) != 0) offset = align - (bdata->node_boot_start & (align - 1UL));
offset = (align - (bdata->node_boot_start & (align - 1UL))); offset = PFN_DOWN(offset);
offset >>= PAGE_SHIFT;
/* /*
* We try to allocate bootmem pages above 'goal' * We try to allocate bootmem pages above 'goal'
* first, then we try to allocate lower pages. * first, then we try to allocate lower pages.
*/ */
if (goal && (goal >= bdata->node_boot_start) && if (goal && goal >= bdata->node_boot_start && PFN_DOWN(goal) < end_pfn) {
((goal >> PAGE_SHIFT) < end_pfn)) {
preferred = goal - bdata->node_boot_start; preferred = goal - bdata->node_boot_start;
if (bdata->last_success >= preferred) if (bdata->last_success >= preferred)
...@@ -210,9 +218,8 @@ __alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size, ...@@ -210,9 +218,8 @@ __alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
} else } else
preferred = 0; preferred = 0;
preferred = ALIGN(preferred, align) >> PAGE_SHIFT; preferred = PFN_DOWN(ALIGN(preferred, align)) + offset;
preferred += offset; areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
areasize = (size+PAGE_SIZE-1)/PAGE_SIZE;
incr = align >> PAGE_SHIFT ? : 1; incr = align >> PAGE_SHIFT ? : 1;
restart_scan: restart_scan:
...@@ -243,7 +250,7 @@ restart_scan: ...@@ -243,7 +250,7 @@ restart_scan:
return NULL; return NULL;
found: found:
bdata->last_success = start << PAGE_SHIFT; bdata->last_success = PFN_PHYS(start);
BUG_ON(start >= eidx); BUG_ON(start >= eidx);
/* /*
...@@ -301,8 +308,8 @@ static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat) ...@@ -301,8 +308,8 @@ static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
count = 0; count = 0;
/* first extant page of the node */ /* first extant page of the node */
pfn = bdata->node_boot_start >> PAGE_SHIFT; pfn = PFN_DOWN(bdata->node_boot_start);
idx = bdata->node_low_pfn - (bdata->node_boot_start >> PAGE_SHIFT); idx = bdata->node_low_pfn - pfn;
map = bdata->node_bootmem_map; map = bdata->node_bootmem_map;
/* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */ /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
if (bdata->node_boot_start == 0 || if (bdata->node_boot_start == 0 ||
...@@ -343,9 +350,10 @@ static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat) ...@@ -343,9 +350,10 @@ static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
*/ */
page = virt_to_page(bdata->node_bootmem_map); page = virt_to_page(bdata->node_bootmem_map);
count = 0; count = 0;
for (i = 0; i < ((bdata->node_low_pfn-(bdata->node_boot_start >> PAGE_SHIFT))/8 + PAGE_SIZE-1)/PAGE_SIZE; i++,page++) { idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT;
count++; for (i = 0; i < idx; i++, page++) {
__free_pages_bootmem(page, 0); __free_pages_bootmem(page, 0);
count++;
} }
total += count; total += count;
bdata->node_bootmem_map = NULL; bdata->node_bootmem_map = NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment