/* register memory section under specified node if it spans that node */ int register_mem_sect_under_node(struct memory_block *mem_blk, int nid) { int ret; unsigned long pfn, sect_start_pfn, sect_end_pfn; if (!mem_blk) return -EFAULT; if (!node_online(nid)) return 0; sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr); sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr); sect_end_pfn += PAGES_PER_SECTION - 1; for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) { int page_nid; page_nid = get_nid_for_pfn(pfn); if (page_nid < 0) continue; if (page_nid != nid) continue; ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj, &mem_blk->dev.kobj, kobject_name(&mem_blk->dev.kobj)); if (ret) return ret; return sysfs_create_link_nowarn(&mem_blk->dev.kobj, &node_devices[nid]->dev.kobj, kobject_name(&node_devices[nid]->dev.kobj)); } /* mem section does not span the specified node */ return 0; }
/** * alloc_bootmem_section - allocate boot memory from a specific section * @size: size of the request in bytes * @section_nr: sparse map section to allocate from * * Return NULL on failure. */ void * __init alloc_bootmem_section(unsigned long size, unsigned long section_nr) { unsigned long pfn, goal, limit; pfn = section_nr_to_pfn(section_nr); goal = pfn << PAGE_SHIFT; limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT; return __alloc_memory_core_early(early_pfn_to_nid(pfn), size, SMP_CACHE_BYTES, goal, limit); }
/* unregister memory section under all nodes that it spans */ int unregister_mem_sect_under_nodes(struct memory_block *mem_blk) { nodemask_t unlinked_nodes; unsigned long pfn, sect_start_pfn, sect_end_pfn; if (!mem_blk) return -EFAULT; nodes_clear(unlinked_nodes); sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index); sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1; for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) { int nid; nid = get_nid_for_pfn(pfn); if (nid < 0) continue; if (!node_online(nid)) continue; if (node_test_and_set(nid, unlinked_nodes)) continue; sysfs_remove_link(&node_devices[nid].sysdev.kobj, kobject_name(&mem_blk->sysdev.kobj)); } return 0; }
static int pseries_add_memory(struct device_node *np) { const char *type; const unsigned int *my_index; const unsigned int *regs; u64 start_pfn; int ret = -EINVAL; /* * Check to see if we are actually adding memory */ type = of_get_property(np, "device_type", NULL); if (type == NULL || strcmp(type, "memory") != 0) return 0; /* * Find the memory index and size of the added section */ my_index = of_get_property(np, "ibm,my-drc-index", NULL); if (!my_index) return ret; regs = of_get_property(np, "reg", NULL); if (!regs) return ret; start_pfn = section_nr_to_pfn(*my_index & 0xffff); /* * Update memory region to represent the memory add */ lmb_add(start_pfn << PAGE_SHIFT, regs[3]); return 0; }
static ssize_t show_valid_zones(struct device *dev, struct device_attribute *attr, char *buf) { struct memory_block *mem = to_memory_block(dev); unsigned long start_pfn, end_pfn; unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; struct page *first_page; struct zone *zone; start_pfn = section_nr_to_pfn(mem->start_section_nr); end_pfn = start_pfn + nr_pages; first_page = pfn_to_page(start_pfn); /* The block contains more than one zone can not be offlined. */ if (!test_pages_in_a_zone(start_pfn, end_pfn)) return sprintf(buf, "none\n"); zone = page_zone(first_page); if (zone_idx(zone) == ZONE_MOVABLE - 1) { /*The mem block is the last memoryblock of this zone.*/ if (end_pfn == zone_end_pfn(zone)) return sprintf(buf, "%s %s\n", zone->name, (zone + 1)->name); } if (zone_idx(zone) == ZONE_MOVABLE) { /*The mem block is the first memoryblock of ZONE_MOVABLE.*/ if (start_pfn == zone->zone_start_pfn) return sprintf(buf, "%s %s\n", zone->name, (zone - 1)->name); } return sprintf(buf, "%s\n", zone->name); }
static int init_memory_block(struct memory_block **memory, struct mem_section *section, unsigned long state) { struct memory_block *mem; unsigned long start_pfn; int scn_nr; int ret = 0; mem = kzalloc(sizeof(*mem), GFP_KERNEL); if (!mem) return -ENOMEM; scn_nr = __section_nr(section); mem->start_section_nr = base_memory_block_id(scn_nr) * sections_per_block; mem->end_section_nr = mem->start_section_nr + sections_per_block - 1; mem->state = state; start_pfn = section_nr_to_pfn(mem->start_section_nr); mem->phys_device = arch_get_memory_phys_device(start_pfn); ret = register_memory(mem); *memory = mem; return ret; }
int ehea_create_busmap( void ) { u64 vaddr = EHEA_BUSMAP_START; unsigned long high_section_index = 0; int i; /* * Sections are not in ascending order -> Loop over all sections and * find the highest PFN to compute the required map size. */ ehea_bmap.valid_sections = 0; for (i = 0; i < NR_MEM_SECTIONS; i++) if (valid_section_nr(i)) high_section_index = i; ehea_bmap.entries = high_section_index + 1; ehea_bmap.vaddr = vmalloc(ehea_bmap.entries * sizeof(*ehea_bmap.vaddr)); if (!ehea_bmap.vaddr) return -ENOMEM; for (i = 0 ; i < ehea_bmap.entries; i++) { unsigned long pfn = section_nr_to_pfn(i); if (pfn_valid(pfn)) { ehea_bmap.vaddr[i] = vaddr; vaddr += EHEA_SECTSIZE; ehea_bmap.valid_sections++; } else ehea_bmap.vaddr[i] = 0; } return 0; }
/* * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is * OK to have direct references to sparsemem variables in here. * Must already be protected by mem_hotplug_begin(). */ static int memory_block_action(unsigned long phys_index, unsigned long action, int online_type) { unsigned long start_pfn; unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; struct page *first_page; int ret; start_pfn = section_nr_to_pfn(phys_index); first_page = pfn_to_page(start_pfn); switch (action) { case MEM_ONLINE: if (!pages_correctly_reserved(start_pfn)) return -EBUSY; ret = online_pages(start_pfn, nr_pages, online_type); break; case MEM_OFFLINE: ret = offline_pages(start_pfn, nr_pages); break; default: WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: " "%ld\n", __func__, phys_index, action, action); ret = -EINVAL; } return ret; }
/* unregister memory section under all nodes that it spans */ int unregister_mem_sect_under_nodes(struct memory_block *mem_blk, unsigned long phys_index) { NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL); unsigned long pfn, sect_start_pfn, sect_end_pfn; if (!mem_blk) { NODEMASK_FREE(unlinked_nodes); return -EFAULT; } if (!unlinked_nodes) return -ENOMEM; nodes_clear(*unlinked_nodes); sect_start_pfn = section_nr_to_pfn(phys_index); sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1; for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) { int nid; nid = get_nid_for_pfn(pfn); if (nid < 0) continue; if (!node_online(nid)) continue; if (node_test_and_set(nid, *unlinked_nodes)) continue; sysfs_remove_link(&node_devices[nid]->dev.kobj, kobject_name(&mem_blk->dev.kobj)); sysfs_remove_link(&mem_blk->dev.kobj, kobject_name(&node_devices[nid]->dev.kobj)); } NODEMASK_FREE(unlinked_nodes); return 0; }
/* register memory section under specified node if it spans that node */ int register_mem_sect_under_node(struct memory_block *mem_blk, int nid) { int ret; unsigned long pfn, sect_start_pfn, sect_end_pfn; if (!mem_blk) return -EFAULT; if (!node_online(nid)) return 0; sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr); sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr); sect_end_pfn += PAGES_PER_SECTION - 1; for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) { int page_nid; /* * memory block could have several absent sections from start. * skip pfn range from absent section */ if (!pfn_present(pfn)) { pfn = round_down(pfn + PAGES_PER_SECTION, PAGES_PER_SECTION) - 1; continue; } page_nid = get_nid_for_pfn(pfn); if (page_nid < 0) continue; if (page_nid != nid) continue; ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj, &mem_blk->dev.kobj, kobject_name(&mem_blk->dev.kobj)); if (ret) return ret; return sysfs_create_link_nowarn(&mem_blk->dev.kobj, &node_devices[nid]->dev.kobj, kobject_name(&node_devices[nid]->dev.kobj)); } /* mem section does not span the specified node */ return 0; }
static int pseries_remove_memory(struct device_node *np) { const char *type; const unsigned int *my_index; const unsigned int *regs; u64 start_pfn, start; struct zone *zone; int ret = -EINVAL; /* * Check to see if we are actually removing memory */ type = of_get_property(np, "device_type", NULL); if (type == NULL || strcmp(type, "memory") != 0) return 0; /* * Find the memory index and size of the removing section */ my_index = of_get_property(np, "ibm,my-drc-index", NULL); if (!my_index) return ret; regs = of_get_property(np, "reg", NULL); if (!regs) return ret; start_pfn = section_nr_to_pfn(*my_index & 0xffff); zone = page_zone(pfn_to_page(start_pfn)); /* * Remove section mappings and sysfs entries for the * section of the memory we are removing. * * NOTE: Ideally, this should be done in generic code like * remove_memory(). But remove_memory() gets called by writing * to sysfs "state" file and we can't remove sysfs entries * while writing to it. So we have to defer it to here. */ ret = __remove_pages(zone, start_pfn, regs[3] >> PAGE_SHIFT); if (ret) return ret; /* * Update memory regions for memory remove */ lmb_remove(start_pfn << PAGE_SHIFT, regs[3]); /* * Remove htab bolted mappings for this section of memory */ start = (unsigned long)__va(start_pfn << PAGE_SHIFT); ret = remove_section_mapping(start, start + regs[3]); return ret; }
/* * Show whether the section of memory is likely to be hot-removable */ static ssize_t show_mem_removable(struct sys_device *dev, struct sysdev_attribute *attr, char *buf) { unsigned long start_pfn; int ret; struct memory_block *mem = container_of(dev, struct memory_block, sysdev); start_pfn = section_nr_to_pfn(mem->phys_index); ret = is_mem_section_removable(start_pfn, PAGES_PER_SECTION); return sprintf(buf, "%d\n", ret); }
/* * Show whether the section of memory is likely to be hot-removable */ static ssize_t show_mem_removable(struct sys_device *dev, struct sysdev_attribute *attr, char *buf) { unsigned long i, pfn; int ret = 1; struct memory_block *mem = container_of(dev, struct memory_block, sysdev); for (i = 0; i < sections_per_block; i++) { pfn = section_nr_to_pfn(mem->start_section_nr + i); ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION); } return sprintf(buf, "%d\n", ret); }
/* * Show whether the section of memory is likely to be hot-removable */ static ssize_t show_mem_removable(struct device *dev, struct device_attribute *attr, char *buf) { unsigned long i, pfn; int ret = 1; struct memory_block *mem = to_memory_block(dev); for (i = 0; i < sections_per_block; i++) { if (!present_section_nr(mem->start_section_nr + i)) continue; pfn = section_nr_to_pfn(mem->start_section_nr + i); ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION); } return sprintf(buf, "%d\n", ret); }
static struct page *sparse_early_mem_map_alloc(unsigned long pnum) { struct page *map; int nid = early_pfn_to_nid(section_nr_to_pfn(pnum)); map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION); if (map) return map; map = alloc_bootmem_node(NODE_DATA(nid), sizeof(struct page) * PAGES_PER_SECTION); if (map) return map; printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__); mem_section[pnum].section_mem_map = 0; return NULL; }
static ssize_t show_valid_zones(struct device *dev, struct device_attribute *attr, char *buf) { struct memory_block *mem = to_memory_block(dev); unsigned long start_pfn, end_pfn; unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; struct page *first_page; struct zone *zone; int zone_shift = 0; start_pfn = section_nr_to_pfn(mem->start_section_nr); end_pfn = start_pfn + nr_pages; first_page = pfn_to_page(start_pfn); /* The block contains more than one zone can not be offlined. */ if (!test_pages_in_a_zone(start_pfn, end_pfn)) return sprintf(buf, "none\n"); zone = page_zone(first_page); /* MMOP_ONLINE_KEEP */ sprintf(buf, "%s", zone->name); /* MMOP_ONLINE_KERNEL */ zone_shift = zone_can_shift(start_pfn, nr_pages, ZONE_NORMAL); if (zone_shift) { strcat(buf, " "); strcat(buf, (zone + zone_shift)->name); } /* MMOP_ONLINE_MOVABLE */ zone_shift = zone_can_shift(start_pfn, nr_pages, ZONE_MOVABLE); if (zone_shift) { strcat(buf, " "); strcat(buf, (zone + zone_shift)->name); } strcat(buf, "\n"); return strlen(buf); }
static int init_memory_block(struct memory_block **memory, struct mem_section *section, unsigned long state) { struct memory_block *mem; unsigned long start_pfn; int scn_nr; int ret = 0; mem = kzalloc(sizeof(*mem), GFP_KERNEL); if (!mem) return -ENOMEM; scn_nr = __section_nr(section); mem->start_section_nr = base_memory_block_id(scn_nr) * sections_per_block; mem->end_section_nr = mem->start_section_nr + sections_per_block - 1; mem->state = state; mem->section_count++; mutex_init(&mem->state_mutex); start_pfn = section_nr_to_pfn(mem->start_section_nr); mem->phys_device = arch_get_memory_phys_device(start_pfn); ret = register_memory(mem); if (!ret) ret = mem_create_simple_file(mem, phys_index); if (!ret) ret = mem_create_simple_file(mem, end_phys_index); if (!ret) ret = mem_create_simple_file(mem, state); if (!ret) ret = mem_create_simple_file(mem, phys_device); if (!ret) ret = mem_create_simple_file(mem, removable); *memory = mem; return ret; }
/* * Subtle, we encode the real pfn into the mem_map such that * the identity pfn - section_mem_map will return the actual * physical page frame number. */ static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum) { return (unsigned long)(mem_map - (section_nr_to_pfn(pnum))); }
struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum) { return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum); }