Commit 5eef150c authored by Linus Torvalds's avatar Linus Torvalds

Merge branch 'stable/e820-3.2' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/xen

* 'stable/e820-3.2' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/xen:
  xen: release all pages within 1-1 p2m mappings
  xen: allow extra memory to be in multiple regions
  xen: allow balloon driver to use more than one memory region
  xen/balloon: simplify test for the end of usable RAM
  xen/balloon: account for pages released during memory setup
parents 315eb8a2 f3f436e3
...@@ -37,7 +37,10 @@ extern void xen_syscall_target(void); ...@@ -37,7 +37,10 @@ extern void xen_syscall_target(void);
extern void xen_syscall32_target(void); extern void xen_syscall32_target(void);
/* Amount of extra memory space we add to the e820 ranges */ /* Amount of extra memory space we add to the e820 ranges */
phys_addr_t xen_extra_mem_start, xen_extra_mem_size; struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
/* Number of pages released from the initial allocation. */
unsigned long xen_released_pages;
/* /*
* The maximum amount of extra memory compared to the base size. The * The maximum amount of extra memory compared to the base size. The
...@@ -51,48 +54,47 @@ phys_addr_t xen_extra_mem_start, xen_extra_mem_size; ...@@ -51,48 +54,47 @@ phys_addr_t xen_extra_mem_start, xen_extra_mem_size;
*/ */
#define EXTRA_MEM_RATIO (10) #define EXTRA_MEM_RATIO (10)
static void __init xen_add_extra_mem(unsigned long pages) static void __init xen_add_extra_mem(u64 start, u64 size)
{ {
unsigned long pfn; unsigned long pfn;
int i;
u64 size = (u64)pages * PAGE_SIZE; for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) {
u64 extra_start = xen_extra_mem_start + xen_extra_mem_size; /* Add new region. */
if (xen_extra_mem[i].size == 0) {
if (!pages) xen_extra_mem[i].start = start;
return; xen_extra_mem[i].size = size;
break;
e820_add_region(extra_start, size, E820_RAM); }
sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); /* Append to existing region. */
if (xen_extra_mem[i].start + xen_extra_mem[i].size == start) {
memblock_x86_reserve_range(extra_start, extra_start + size, "XEN EXTRA"); xen_extra_mem[i].size += size;
break;
}
}
if (i == XEN_EXTRA_MEM_MAX_REGIONS)
printk(KERN_WARNING "Warning: not enough extra memory regions\n");
xen_extra_mem_size += size; memblock_x86_reserve_range(start, start + size, "XEN EXTRA");
xen_max_p2m_pfn = PFN_DOWN(extra_start + size); xen_max_p2m_pfn = PFN_DOWN(start + size);
for (pfn = PFN_DOWN(extra_start); pfn <= xen_max_p2m_pfn; pfn++) for (pfn = PFN_DOWN(start); pfn <= xen_max_p2m_pfn; pfn++)
__set_phys_to_machine(pfn, INVALID_P2M_ENTRY); __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
} }
static unsigned long __init xen_release_chunk(phys_addr_t start_addr, static unsigned long __init xen_release_chunk(unsigned long start,
phys_addr_t end_addr) unsigned long end)
{ {
struct xen_memory_reservation reservation = { struct xen_memory_reservation reservation = {
.address_bits = 0, .address_bits = 0,
.extent_order = 0, .extent_order = 0,
.domid = DOMID_SELF .domid = DOMID_SELF
}; };
unsigned long start, end;
unsigned long len = 0; unsigned long len = 0;
unsigned long pfn; unsigned long pfn;
int ret; int ret;
start = PFN_UP(start_addr);
end = PFN_DOWN(end_addr);
if (end <= start)
return 0;
for(pfn = start; pfn < end; pfn++) { for(pfn = start; pfn < end; pfn++) {
unsigned long mfn = pfn_to_mfn(pfn); unsigned long mfn = pfn_to_mfn(pfn);
...@@ -117,72 +119,52 @@ static unsigned long __init xen_release_chunk(phys_addr_t start_addr, ...@@ -117,72 +119,52 @@ static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
return len; return len;
} }
static unsigned long __init xen_return_unused_memory(unsigned long max_pfn, static unsigned long __init xen_set_identity_and_release(
const struct e820map *e820) const struct e820entry *list, size_t map_size, unsigned long nr_pages)
{ {
phys_addr_t max_addr = PFN_PHYS(max_pfn); phys_addr_t start = 0;
phys_addr_t last_end = ISA_END_ADDRESS;
unsigned long released = 0; unsigned long released = 0;
int i;
/* Free any unused memory above the low 1Mbyte. */
for (i = 0; i < e820->nr_map && last_end < max_addr; i++) {
phys_addr_t end = e820->map[i].addr;
end = min(max_addr, end);
if (last_end < end)
released += xen_release_chunk(last_end, end);
last_end = max(last_end, e820->map[i].addr + e820->map[i].size);
}
if (last_end < max_addr)
released += xen_release_chunk(last_end, max_addr);
printk(KERN_INFO "released %lu pages of unused memory\n", released);
return released;
}
static unsigned long __init xen_set_identity(const struct e820entry *list,
ssize_t map_size)
{
phys_addr_t last = xen_initial_domain() ? 0 : ISA_END_ADDRESS;
phys_addr_t start_pci = last;
const struct e820entry *entry;
unsigned long identity = 0; unsigned long identity = 0;
const struct e820entry *entry;
int i; int i;
/*
* Combine non-RAM regions and gaps until a RAM region (or the
* end of the map) is reached, then set the 1:1 map and
* release the pages (if available) in those non-RAM regions.
*
* The combined non-RAM regions are rounded to a whole number
* of pages so any partial pages are accessible via the 1:1
* mapping. This is needed for some BIOSes that put (for
* example) the DMI tables in a reserved region that begins on
* a non-page boundary.
*/
for (i = 0, entry = list; i < map_size; i++, entry++) { for (i = 0, entry = list; i < map_size; i++, entry++) {
phys_addr_t start = entry->addr; phys_addr_t end = entry->addr + entry->size;
phys_addr_t end = start + entry->size;
if (start < last) if (entry->type == E820_RAM || i == map_size - 1) {
start = last; unsigned long start_pfn = PFN_DOWN(start);
unsigned long end_pfn = PFN_UP(end);
if (end <= start) if (entry->type == E820_RAM)
continue; end_pfn = PFN_UP(entry->addr);
/* Skip over the 1MB region. */ if (start_pfn < end_pfn) {
if (last > end) if (start_pfn < nr_pages)
continue; released += xen_release_chunk(
start_pfn, min(end_pfn, nr_pages));
if ((entry->type == E820_RAM) || (entry->type == E820_UNUSABLE)) {
if (start > start_pci)
identity += set_phys_range_identity( identity += set_phys_range_identity(
PFN_UP(start_pci), PFN_DOWN(start)); start_pfn, end_pfn);
}
/* Without saving 'last' we would gooble RAM too start = end;
* at the end of the loop. */
last = end;
start_pci = end;
continue;
} }
start_pci = min(start, start_pci);
last = end;
} }
if (last > start_pci)
identity += set_phys_range_identity( printk(KERN_INFO "Released %lu pages of unused memory\n", released);
PFN_UP(start_pci), PFN_DOWN(last)); printk(KERN_INFO "Set %ld page(s) to 1-1 mapping\n", identity);
return identity;
return released;
} }
static unsigned long __init xen_get_max_pages(void) static unsigned long __init xen_get_max_pages(void)
...@@ -197,21 +179,32 @@ static unsigned long __init xen_get_max_pages(void) ...@@ -197,21 +179,32 @@ static unsigned long __init xen_get_max_pages(void)
return min(max_pages, MAX_DOMAIN_PAGES); return min(max_pages, MAX_DOMAIN_PAGES);
} }
static void xen_align_and_add_e820_region(u64 start, u64 size, int type)
{
u64 end = start + size;
/* Align RAM regions to page boundaries. */
if (type == E820_RAM) {
start = PAGE_ALIGN(start);
end &= ~((u64)PAGE_SIZE - 1);
}
e820_add_region(start, end - start, type);
}
/** /**
* machine_specific_memory_setup - Hook for machine specific memory setup. * machine_specific_memory_setup - Hook for machine specific memory setup.
**/ **/
char * __init xen_memory_setup(void) char * __init xen_memory_setup(void)
{ {
static struct e820entry map[E820MAX] __initdata; static struct e820entry map[E820MAX] __initdata;
static struct e820entry map_raw[E820MAX] __initdata;
unsigned long max_pfn = xen_start_info->nr_pages; unsigned long max_pfn = xen_start_info->nr_pages;
unsigned long long mem_end; unsigned long long mem_end;
int rc; int rc;
struct xen_memory_map memmap; struct xen_memory_map memmap;
unsigned long max_pages;
unsigned long extra_pages = 0; unsigned long extra_pages = 0;
unsigned long extra_limit;
unsigned long identity_pages = 0;
int i; int i;
int op; int op;
...@@ -237,58 +230,65 @@ char * __init xen_memory_setup(void) ...@@ -237,58 +230,65 @@ char * __init xen_memory_setup(void)
} }
BUG_ON(rc); BUG_ON(rc);
memcpy(map_raw, map, sizeof(map)); /* Make sure the Xen-supplied memory map is well-ordered. */
e820.nr_map = 0; sanitize_e820_map(map, memmap.nr_entries, &memmap.nr_entries);
xen_extra_mem_start = mem_end;
for (i = 0; i < memmap.nr_entries; i++) { max_pages = xen_get_max_pages();
unsigned long long end; if (max_pages > max_pfn)
extra_pages += max_pages - max_pfn;
/* Guard against non-page aligned E820 entries. */
if (map[i].type == E820_RAM) /*
map[i].size -= (map[i].size + map[i].addr) % PAGE_SIZE; * Set P2M for all non-RAM pages and E820 gaps to be identity
* type PFNs. Any RAM pages that would be made inaccesible by
end = map[i].addr + map[i].size; * this are first released.
if (map[i].type == E820_RAM && end > mem_end) { */
/* RAM off the end - may be partially included */ xen_released_pages = xen_set_identity_and_release(
u64 delta = min(map[i].size, end - mem_end); map, memmap.nr_entries, max_pfn);
extra_pages += xen_released_pages;
map[i].size -= delta;
end -= delta; /*
* Clamp the amount of extra memory to a EXTRA_MEM_RATIO
extra_pages += PFN_DOWN(delta); * factor the base size. On non-highmem systems, the base
/* * size is the full initial memory allocation; on highmem it
* Set RAM below 4GB that is not for us to be unusable. * is limited to the max size of lowmem, so that it doesn't
* This prevents "System RAM" address space from being * get completely filled.
* used as potential resource for I/O address (happens *
* when 'allocate_resource' is called). * In principle there could be a problem in lowmem systems if
*/ * the initial memory is also very large with respect to
if (delta && * lowmem, but we won't try to deal with that here.
(xen_initial_domain() && end < 0x100000000ULL)) */
e820_add_region(end, delta, E820_UNUSABLE); extra_pages = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
extra_pages);
i = 0;
while (i < memmap.nr_entries) {
u64 addr = map[i].addr;
u64 size = map[i].size;
u32 type = map[i].type;
if (type == E820_RAM) {
if (addr < mem_end) {
size = min(size, mem_end - addr);
} else if (extra_pages) {
size = min(size, (u64)extra_pages * PAGE_SIZE);
extra_pages -= size / PAGE_SIZE;
xen_add_extra_mem(addr, size);
} else
type = E820_UNUSABLE;
} }
if (map[i].size > 0 && end > xen_extra_mem_start) xen_align_and_add_e820_region(addr, size, type);
xen_extra_mem_start = end;
/* Add region if any remains */ map[i].addr += size;
if (map[i].size > 0) map[i].size -= size;
e820_add_region(map[i].addr, map[i].size, map[i].type); if (map[i].size == 0)
i++;
} }
/* Align the balloon area so that max_low_pfn does not get set
* to be at the _end_ of the PCI gap at the far end (fee01000).
* Note that xen_extra_mem_start gets set in the loop above to be
* past the last E820 region. */
if (xen_initial_domain() && (xen_extra_mem_start < (1ULL<<32)))
xen_extra_mem_start = (1ULL<<32);
/* /*
* In domU, the ISA region is normal, usable memory, but we * In domU, the ISA region is normal, usable memory, but we
* reserve ISA memory anyway because too many things poke * reserve ISA memory anyway because too many things poke
* about in there. * about in there.
*
* In Dom0, the host E820 information can leave gaps in the
* ISA range, which would cause us to release those pages. To
* avoid this, we unconditionally reserve them here.
*/ */
e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS, e820_add_region(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS,
E820_RESERVED); E820_RESERVED);
...@@ -305,44 +305,6 @@ char * __init xen_memory_setup(void) ...@@ -305,44 +305,6 @@ char * __init xen_memory_setup(void)
sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
extra_limit = xen_get_max_pages();
if (max_pfn + extra_pages > extra_limit) {
if (extra_limit > max_pfn)
extra_pages = extra_limit - max_pfn;
else
extra_pages = 0;
}
extra_pages += xen_return_unused_memory(xen_start_info->nr_pages, &e820);
/*
* Clamp the amount of extra memory to a EXTRA_MEM_RATIO
* factor the base size. On non-highmem systems, the base
* size is the full initial memory allocation; on highmem it
* is limited to the max size of lowmem, so that it doesn't
* get completely filled.
*
* In principle there could be a problem in lowmem systems if
* the initial memory is also very large with respect to
* lowmem, but we won't try to deal with that here.
*/
extra_limit = min(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
max_pfn + extra_pages);
if (extra_limit >= max_pfn)
extra_pages = extra_limit - max_pfn;
else
extra_pages = 0;
xen_add_extra_mem(extra_pages);
/*
* Set P2M for all non-RAM pages and E820 gaps to be identity
* type PFNs. We supply it with the non-sanitized version
* of the E820.
*/
identity_pages = xen_set_identity(map_raw, memmap.nr_entries);
printk(KERN_INFO "Set %ld page(s) to 1-1 mapping.\n", identity_pages);
return "Xen"; return "Xen";
} }
......
...@@ -555,17 +555,40 @@ void free_xenballooned_pages(int nr_pages, struct page** pages) ...@@ -555,17 +555,40 @@ void free_xenballooned_pages(int nr_pages, struct page** pages)
} }
EXPORT_SYMBOL(free_xenballooned_pages); EXPORT_SYMBOL(free_xenballooned_pages);
static int __init balloon_init(void) static void __init balloon_add_region(unsigned long start_pfn,
unsigned long pages)
{ {
unsigned long pfn, extra_pfn_end; unsigned long pfn, extra_pfn_end;
struct page *page; struct page *page;
/*
* If the amount of usable memory has been limited (e.g., with
* the 'mem' command line parameter), don't add pages beyond
* this limit.
*/
extra_pfn_end = min(max_pfn, start_pfn + pages);
for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
page = pfn_to_page(pfn);
/* totalram_pages and totalhigh_pages do not
include the boot-time balloon extension, so
don't subtract from it. */
__balloon_append(page);
}
}
static int __init balloon_init(void)
{
int i;
if (!xen_domain()) if (!xen_domain())
return -ENODEV; return -ENODEV;
pr_info("xen/balloon: Initialising balloon driver.\n"); pr_info("xen/balloon: Initialising balloon driver.\n");
balloon_stats.current_pages = xen_pv_domain() ? min(xen_start_info->nr_pages, max_pfn) : max_pfn; balloon_stats.current_pages = xen_pv_domain()
? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
: max_pfn;
balloon_stats.target_pages = balloon_stats.current_pages; balloon_stats.target_pages = balloon_stats.current_pages;
balloon_stats.balloon_low = 0; balloon_stats.balloon_low = 0;
balloon_stats.balloon_high = 0; balloon_stats.balloon_high = 0;
...@@ -584,24 +607,13 @@ static int __init balloon_init(void) ...@@ -584,24 +607,13 @@ static int __init balloon_init(void)
#endif #endif
/* /*
* Initialise the balloon with excess memory space. We need * Initialize the balloon with pages from the extra memory
* to make sure we don't add memory which doesn't exist or * regions (see arch/x86/xen/setup.c).
* logically exist. The E820 map can be trimmed to be smaller
* than the amount of physical memory due to the mem= command
* line parameter. And if this is a 32-bit non-HIGHMEM kernel
* on a system with memory which requires highmem to access,
* don't try to use it.
*/ */
extra_pfn_end = min(min(max_pfn, e820_end_of_ram_pfn()), for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
(unsigned long)PFN_DOWN(xen_extra_mem_start + xen_extra_mem_size)); if (xen_extra_mem[i].size)
for (pfn = PFN_UP(xen_extra_mem_start); balloon_add_region(PFN_UP(xen_extra_mem[i].start),
pfn < extra_pfn_end; PFN_DOWN(xen_extra_mem[i].size));
pfn++) {
page = pfn_to_page(pfn);
/* totalram_pages and totalhigh_pages do not include the boot-time
balloon extension, so don't subtract from it. */
__balloon_append(page);
}
return 0; return 0;
} }
......
...@@ -3,6 +3,16 @@ ...@@ -3,6 +3,16 @@
#include <asm/xen/page.h> #include <asm/xen/page.h>
extern phys_addr_t xen_extra_mem_start, xen_extra_mem_size; struct xen_memory_region {
phys_addr_t start;
phys_addr_t size;
};
#define XEN_EXTRA_MEM_MAX_REGIONS 128 /* == E820MAX */
extern __initdata
struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS];
extern unsigned long xen_released_pages;
#endif /* _XEN_PAGE_H */ #endif /* _XEN_PAGE_H */
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