The patch-set was divided from following thread's patch-set.
https://lkml.org/lkml/2012/9/5/201
The last version of this patchset:
https://lkml.org/lkml/2012/11/1/93
If you want to know the reason, please read following thread.
https://lkml.org/lkml/2012/10/2/83
The patch-set has only the function of kernel core side for physical
memory hot remove. So if you use the patch, please apply following
patches.
- bug fix for memory hot remove
https://lkml.org/lkml/2012/10/31/269
- acpi framework
https://lkml.org/lkml/2012/10/26/175
The patches can free/remove the following things:
- /sys/firmware/memmap/X/{end, start, type} : [PATCH 2/10]
- mem_section and related sysfs files : [PATCH 3-4/10]
- memmap of sparse-vmemmap : [PATCH 5-7/10]
- page table of removed memory : [RFC PATCH 8/10]
- node and related sysfs files : [RFC PATCH 9-10/10]
* [PATCH 2/10] checks whether the memory can be removed or not.
If you find lack of function for physical memory hot-remove, please let me
know.
How to test this patchset?
1. apply this patchset and build the kernel. MEMORY_HOTPLUG, MEMORY_HOTREMOVE,
ACPI_HOTPLUG_MEMORY must be selected.
2. load the module acpi_memhotplug
3. hotplug the memory device(it depends on your hardware)
You will see the memory device under the directory /sys/bus/acpi/devices/.
Its name is PNP0C80:XX.
4. online/offline pages provided by this memory device
You can write online/offline to /sys/devices/system/memory/memoryX/state to
online/offline pages provided by this memory device
5. hotremove the memory device
You can hotremove the memory device by the hardware, or writing 1 to
/sys/bus/acpi/devices/PNP0C80:XX/eject.
Note: if the memory provided by the memory device is used by the kernel, it
can't be offlined. It is not a bug.
Known problems:
1. hotremoving memory device may cause kernel panicked
This bug will be fixed by Liu Jiang's patch:
https://lkml.org/lkml/2012/7/3/1
Changelogs from v3 to v4:
Patch7: remove unused codes.
Patch8: fix nr_pages that is passed to free_map_bootmem()
Changelogs from v2 to v3:
Patch9: call sync_global_pgds() if pgd is changed
Patch10: fix a problem int the patch
Changelogs from v1 to v2:
Patch1: new patch, offline memory twice. 1st iterate: offline every non primary
memory block. 2nd iterate: offline primary (i.e. first added) memory
block.
Patch3: new patch, no logical change, just remove reduntant codes.
Patch9: merge the patch from wujianguo into this patch. flush tlb on all cpu
after the pagetable is changed.
Patch12: new patch, free node_data when a node is offlined
Wen Congyang (6):
memory-hotplug: try to offline the memory twice to avoid dependence
memory-hotplug: remove redundant codes
memory-hotplug: introduce new function arch_remove_memory() for
removing page table depends on architecture
memory-hotplug: remove page table of x86_64 architecture
memory-hotplug: remove sysfs file of node
memory-hotplug: free node_data when a node is offlined
Yasuaki Ishimatsu (6):
memory-hotplug: check whether all memory blocks are offlined or not
when removing memory
memory-hotplug: remove /sys/firmware/memmap/X sysfs
memory-hotplug: unregister memory section on SPARSEMEM_VMEMMAP
memory-hotplug: implement register_page_bootmem_info_section of
sparse-vmemmap
memory-hotplug: remove memmap of sparse-vmemmap
memory-hotplug: memory_hotplug: clear zone when removing the memory
arch/ia64/mm/discontig.c | 14 ++
arch/ia64/mm/init.c | 18 ++
arch/powerpc/mm/init_64.c | 14 ++
arch/powerpc/mm/mem.c | 12 +
arch/s390/mm/init.c | 12 +
arch/s390/mm/vmem.c | 14 ++
arch/sh/mm/init.c | 17 ++
arch/sparc/mm/init_64.c | 14 ++
arch/tile/mm/init.c | 8 +
arch/x86/include/asm/pgtable_types.h | 1 +
arch/x86/mm/init_32.c | 12 +
arch/x86/mm/init_64.c | 417 +++++++++++++++++++++++++++++++++++
arch/x86/mm/pageattr.c | 47 ++--
drivers/acpi/acpi_memhotplug.c | 8 +-
drivers/base/memory.c | 6 +
drivers/firmware/memmap.c | 98 +++++++-
include/linux/firmware-map.h | 6 +
include/linux/memory_hotplug.h | 15 +-
include/linux/mm.h | 5 +-
mm/memory_hotplug.c | 405 ++++++++++++++++++++++++++++++++--
mm/sparse.c | 19 +-
21 files changed, 1098 insertions(+), 64 deletions(-)
--
1.8.0
memory can't be offlined when CONFIG_MEMCG is selected.
For example: there is a memory device on node 1. The address range
is [1G, 1.5G). You will find 4 new directories memory8, memory9, memory10,
and memory11 under the directory /sys/devices/system/memory/.
If CONFIG_MEMCG is selected, we will allocate memory to store page cgroup
when we online pages. When we online memory8, the memory stored page cgroup
is not provided by this memory device. But when we online memory9, the memory
stored page cgroup may be provided by memory8. So we can't offline memory8
now. We should offline the memory in the reversed order.
When the memory device is hotremoved, we will auto offline memory provided
by this memory device. But we don't know which memory is onlined first, so
offlining memory may fail. In such case, iterate twice to offline the memory.
1st iterate: offline every non primary memory block.
2nd iterate: offline primary (i.e. first added) memory block.
This idea is suggested by KOSAKI Motohiro.
CC: David Rientjes <rientjes@google.com>
CC: Jiang Liu <redacted>
CC: Len Brown <redacted>
CC: Christoph Lameter <redacted>
Cc: Minchan Kim <redacted>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: KOSAKI Motohiro <redacted>
CC: Yasuaki Ishimatsu <redacted>
Signed-off-by: Wen Congyang <redacted>
---
mm/memory_hotplug.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
From: Yasuaki Ishimatsu <redacted>
Currently __remove_section for SPARSEMEM_VMEMMAP does nothing. But even if
we use SPARSEMEM_VMEMMAP, we can unregister the memory_section.
So the patch add unregister_memory_section() into __remove_section().
CC: David Rientjes <rientjes@google.com>
CC: Jiang Liu <redacted>
CC: Len Brown <redacted>
CC: Christoph Lameter <redacted>
Cc: Minchan Kim <redacted>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: KOSAKI Motohiro <redacted>
Signed-off-by: Yasuaki Ishimatsu <redacted>
Signed-off-by: Wen Congyang <redacted>
---
mm/memory_hotplug.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
From: Yasuaki Ishimatsu <redacted>
All pages of virtual mapping in removed memory cannot be freed, since some pages
used as PGD/PUD includes not only removed memory but also other memory. So the
patch checks whether page can be freed or not.
How to check whether page can be freed or not?
1. When removing memory, the page structs of the revmoved memory are filled
with 0FD.
2. All page structs are filled with 0xFD on PT/PMD, PT/PMD can be cleared.
In this case, the page used as PT/PMD can be freed.
Applying patch, __remove_section() of CONFIG_SPARSEMEM_VMEMMAP is integrated
into one. So __remove_section() of CONFIG_SPARSEMEM_VMEMMAP is deleted.
Note: vmemmap_kfree() and vmemmap_free_bootmem() are not implemented for ia64,
ppc, s390, and sparc.
CC: David Rientjes <rientjes@google.com>
CC: Jiang Liu <redacted>
CC: Len Brown <redacted>
CC: Christoph Lameter <redacted>
Cc: Minchan Kim <redacted>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: KOSAKI Motohiro <redacted>
Signed-off-by: Yasuaki Ishimatsu <redacted>
Signed-off-by: Jianguo Wu <redacted>
Signed-off-by: Wen Congyang <redacted>
---
arch/ia64/mm/discontig.c | 8 ++++
arch/powerpc/mm/init_64.c | 8 ++++
arch/s390/mm/vmem.c | 8 ++++
arch/sparc/mm/init_64.c | 8 ++++
arch/x86/mm/init_64.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++
include/linux/mm.h | 2 +
mm/memory_hotplug.c | 17 +------
mm/sparse.c | 19 ++++----
8 files changed, 165 insertions(+), 24 deletions(-)
@@ -1642,6 +1642,8 @@ int vmemmap_populate(struct page *start_page, unsigned long pages, int node);voidvmemmap_populate_print_last(void);voidregister_page_bootmem_memmap(unsignedlongsection_nr,structpage*map,unsignedlongsize);+voidvmemmap_kfree(structpage*memmpa,unsignedlongnr_pages);+voidvmemmap_free_bootmem(structpage*memmpa,unsignedlongnr_pages);enummf_flags{MF_COUNT_INCREASED=1<<0,
@@ -613,12 +613,13 @@ static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,/* This will make the necessary allocations eventually. */returnsparse_mem_map_populate(pnum,nid);}-staticvoid__kfree_section_memmap(structpage*memmap,unsignedlongnr_pages)+staticvoid__kfree_section_memmap(structpage*page,unsignedlongnr_pages){-return;/* XXX: Not implemented yet */+vmemmap_kfree(page,nr_pages);}-staticvoidfree_map_bootmem(structpage*page,unsignedlongnr_pages)+staticvoidfree_map_bootmem(structpage*page){+vmemmap_free_bootmem(page,PAGES_PER_SECTION);}#elsestaticstructpage*__kmalloc_section_memmap(unsignedlongnr_pages)
For removing memory, we need to remove page table. But it depends
on architecture. So the patch introduce arch_remove_memory() for
removing page table. Now it only calls __remove_pages().
Note: __remove_pages() for some archtecuture is not implemented
(I don't know how to implement it for s390).
CC: David Rientjes <rientjes@google.com>
CC: Jiang Liu <redacted>
CC: Len Brown <redacted>
CC: Benjamin Herrenschmidt <benh@kernel.crashing.org>
CC: Paul Mackerras <redacted>
CC: Christoph Lameter <redacted>
Cc: Minchan Kim <redacted>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: KOSAKI Motohiro <redacted>
CC: Yasuaki Ishimatsu <redacted>
Signed-off-by: Wen Congyang <redacted>
---
arch/ia64/mm/init.c | 18 ++++++++++++++++++
arch/powerpc/mm/mem.c | 12 ++++++++++++
arch/s390/mm/init.c | 12 ++++++++++++
arch/sh/mm/init.c | 17 +++++++++++++++++
arch/tile/mm/init.c | 8 ++++++++
arch/x86/mm/init_32.c | 12 ++++++++++++
arch/x86/mm/init_64.c | 15 +++++++++++++++
include/linux/memory_hotplug.h | 1 +
mm/memory_hotplug.c | 2 ++
9 files changed, 97 insertions(+)
@@ -85,6 +85,7 @@ extern void __online_page_free(struct page *page);#ifdef CONFIG_MEMORY_HOTREMOVEexternboolis_pageblock_removable_nolock(structpage*page);+externintarch_remove_memory(u64start,u64size);#endif /* CONFIG_MEMORY_HOTREMOVE *//* reasonably generic interface to expand the physical pages in a zone */
We call hotadd_new_pgdat() to allocate memory to store node_data. So we
should free it when removing a node.
CC: David Rientjes <rientjes@google.com>
CC: Jiang Liu <redacted>
CC: Len Brown <redacted>
CC: Benjamin Herrenschmidt <benh@kernel.crashing.org>
CC: Paul Mackerras <redacted>
CC: Christoph Lameter <redacted>
Cc: Minchan Kim <redacted>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: KOSAKI Motohiro <redacted>
CC: Yasuaki Ishimatsu <redacted>
Signed-off-by: Wen Congyang <redacted>
---
mm/memory_hotplug.c | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
@@ -1309,9 +1309,12 @@ static int check_cpu_on_node(void *data)/* offline the node if all memory sections of this node are removed */staticvoidtry_offline_node(intnid){+pg_data_t*pgdat=NODE_DATA(nid);unsignedlongstart_pfn=NODE_DATA(nid)->node_start_pfn;-unsignedlongend_pfn=start_pfn+NODE_DATA(nid)->node_spanned_pages;+unsignedlongend_pfn=start_pfn+pgdat->node_spanned_pages;unsignedlongpfn;+structpage*pgdat_page=virt_to_page(pgdat);+inti;for(pfn=start_pfn;pfn<end_pfn;pfn+=PAGES_PER_SECTION){unsignedlongsection_nr=pfn_to_section_nr(pfn);
@@ -1338,6 +1341,21 @@ static void try_offline_node(int nid)*/node_set_offline(nid);unregister_one_node(nid);++if(!PageSlab(pgdat_page)&&!PageCompound(pgdat_page))+/* node data is allocated from boot memory */+return;++/* free waittable in each zone */+for(i=0;i<MAX_NR_ZONES;i++){+structzone*zone=pgdat->node_zones+i;++if(zone->wait_table)+vfree(zone->wait_table);+}++arch_refresh_nodedata(nid,NULL);+arch_free_nodedata(pgdat);}int__refremove_memory(intnid,u64start,u64size)
This patch introduces a new function try_offline_node() to
remove sysfs file of node when all memory sections of this
node are removed. If some memory sections of this node are
not removed, this function does nothing.
CC: David Rientjes <rientjes@google.com>
CC: Jiang Liu <redacted>
CC: Len Brown <redacted>
CC: Christoph Lameter <redacted>
Cc: Minchan Kim <redacted>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: KOSAKI Motohiro <redacted>
CC: Yasuaki Ishimatsu <redacted>
Signed-off-by: Wen Congyang <redacted>
---
drivers/acpi/acpi_memhotplug.c | 8 +++++-
include/linux/memory_hotplug.h | 2 +-
mm/memory_hotplug.c | 58 ++++++++++++++++++++++++++++++++++++++++--
3 files changed, 64 insertions(+), 4 deletions(-)
@@ -1288,7 +1289,58 @@ static int is_memblock_offlined_cb(struct memory_block *mem, void *arg)returnret;}-int__refremove_memory(u64start,u64size)+staticintcheck_cpu_on_node(void*data)+{+structpglist_data*pgdat=data;+intcpu;++for_each_present_cpu(cpu){+if(cpu_to_node(cpu)==pgdat->node_id)+/*+*thecpuonthisnodeisn'tremoved,andwecan't+*offlinethisnode.+*/+return-EBUSY;+}++return0;+}++/* offline the node if all memory sections of this node are removed */+staticvoidtry_offline_node(intnid)+{+unsignedlongstart_pfn=NODE_DATA(nid)->node_start_pfn;+unsignedlongend_pfn=start_pfn+NODE_DATA(nid)->node_spanned_pages;+unsignedlongpfn;++for(pfn=start_pfn;pfn<end_pfn;pfn+=PAGES_PER_SECTION){+unsignedlongsection_nr=pfn_to_section_nr(pfn);++if(!present_section_nr(section_nr))+continue;++if(pfn_to_nid(pfn)!=nid)+continue;++/*+*somememorysectionsofthisnodearenotremoved,andwe+*can'tofflinenodenow.+*/+return;+}++if(stop_machine(check_cpu_on_node,NODE_DATA(nid),NULL))+return;++/*+*allmemory/cpuofthisnodeareremoved,wecanofflinethis+*nodenow.+*/+node_set_offline(nid);+unregister_one_node(nid);+}++int__refremove_memory(intnid,u64start,u64size){unsignedlongstart_pfn,end_pfn;intret=0;
@@ -1344,7 +1398,7 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages){return-EINVAL;}-intremove_memory(u64start,u64size)+intremove_memory(intnid,u64start,u64size){return-EINVAL;}
For hot removing memory, we sholud remove page table about the memory.
So the patch searches a page table about the removed memory, and clear
page table.
CC: David Rientjes <rientjes@google.com>
CC: Jiang Liu <redacted>
CC: Len Brown <redacted>
CC: Christoph Lameter <redacted>
Cc: Minchan Kim <redacted>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: KOSAKI Motohiro <redacted>
CC: Yasuaki Ishimatsu <redacted>
Signed-off-by: Wen Congyang <redacted>
Signed-off-by: Jianguo Wu <redacted>
Signed-off-by: Jiang Liu <redacted>
---
arch/x86/include/asm/pgtable_types.h | 1 +
arch/x86/mm/init_64.c | 231 +++++++++++++++++++++++++++++++++++
arch/x86/mm/pageattr.c | 47 +++----
3 files changed, 257 insertions(+), 22 deletions(-)
@@ -680,6 +680,235 @@ int arch_add_memory(int nid, u64 start, u64 size)}EXPORT_SYMBOL_GPL(arch_add_memory);+staticinlinevoidfree_pagetable(structpage*page)+{+structzone*zone;+boolbootmem=false;++/* bootmem page has reserved flag */+if(PageReserved(page)){+__ClearPageReserved(page);+bootmem=true;+}++__free_page(page);++if(bootmem){+zone=page_zone(page);+zone_span_writelock(zone);+zone->present_pages++;+zone_span_writeunlock(zone);+totalram_pages++;+}+}++staticvoidfree_pte_table(pte_t*pte_start,pmd_t*pmd)+{+pte_t*pte;+inti;++for(i=0;i<PTRS_PER_PTE;i++){+pte=pte_start+i;+if(pte_val(*pte))+return;+}++/* free a pte talbe */+free_pagetable(pmd_page(*pmd));+pmd_clear(pmd);+}++staticvoidfree_pmd_table(pmd_t*pmd_start,pud_t*pud)+{+pmd_t*pmd;+inti;++for(i=0;i<PTRS_PER_PMD;i++){+pmd=pmd_start+i;+if(pmd_val(*pmd))+return;+}++/* free a pmd talbe */+free_pagetable(pud_page(*pud));+pud_clear(pud);+}++/* return true if pgd is changed, otherwise return false */+staticboolfree_pud_table(pud_t*pud_start,pgd_t*pgd)+{+pud_t*pud;+inti;++for(i=0;i<PTRS_PER_PUD;i++){+pud=pud_start+i;+if(pud_val(*pud))+returnfalse;+}++/* free a pud table */+free_pagetable(pgd_page(*pgd));+pgd_clear(pgd);++returntrue;+}++staticvoid__meminit+phys_pte_remove(pte_t*pte_page,unsignedlongaddr,unsignedlongend)+{+unsignedpages=0;+inti=pte_index(addr);++pte_t*pte=pte_page+pte_index(addr);++for(;i<PTRS_PER_PTE;i++,addr+=PAGE_SIZE,pte++){++if(addr>=end)+break;++if(!pte_present(*pte))+continue;++pages++;+set_pte(pte,__pte(0));+}++update_page_count(PG_LEVEL_4K,-pages);+}++staticvoid__meminit+phys_pmd_remove(pmd_t*pmd_page,unsignedlongaddr,unsignedlongend)+{+unsignedlongpages=0,next;+inti=pmd_index(addr);++for(;i<PTRS_PER_PMD&&addr<end;i++,addr=next){+unsignedlongpte_phys;+pmd_t*pmd=pmd_page+pmd_index(addr);+pte_t*pte;++next=pmd_addr_end(addr,end);++if(!pmd_present(*pmd))+continue;++if(pmd_large(*pmd)){+if(IS_ALIGNED(addr,PMD_SIZE)&&+IS_ALIGNED(next,PMD_SIZE)){+set_pmd(pmd,__pmd(0));+pages++;+continue;+}++/*+*Weuse2Mpage,butweneedtoremovepartofthem,+*sosplit2Mpageto4Kpage.+*/+pte=alloc_low_page(&pte_phys);+BUG_ON(!pte);+__split_large_page((pte_t*)pmd,+(unsignedlong)__va(addr),pte);++spin_lock(&init_mm.page_table_lock);+pmd_populate_kernel(&init_mm,pmd,__va(pte_phys));+spin_unlock(&init_mm.page_table_lock);++/* Do a global flush tlb after splitting a large page */+flush_tlb_all();+}++spin_lock(&init_mm.page_table_lock);+pte=map_low_page((pte_t*)pmd_page_vaddr(*pmd));+phys_pte_remove(pte,addr,next);+free_pte_table(pte,pmd);+unmap_low_page(pte);+spin_unlock(&init_mm.page_table_lock);+}+update_page_count(PG_LEVEL_2M,-pages);+}++staticvoid__meminit+phys_pud_remove(pud_t*pud_page,unsignedlongaddr,unsignedlongend)+{+unsignedlongpages=0,next;+inti=pud_index(addr);++for(;i<PTRS_PER_PUD&&addr<end;i++,addr=next){+unsignedlongpmd_phys;+pud_t*pud=pud_page+pud_index(addr);+pmd_t*pmd;++next=pud_addr_end(addr,end);++if(!pud_present(*pud))+continue;++if(pud_large(*pud)){+if(IS_ALIGNED(addr,PUD_SIZE)&&+IS_ALIGNED(next,PUD_SIZE)){+set_pud(pud,__pud(0));+pages++;+continue;+}++/*+*Weuse1Gpage,butweneedtoremovepartofthem,+*sosplit1Gpageto2Mpage.+*/+pmd=alloc_low_page(&pmd_phys);+BUG_ON(!pmd);+__split_large_page((pte_t*)pud,+(unsignedlong)__va(addr),+(pte_t*)pmd);++spin_lock(&init_mm.page_table_lock);+pud_populate(&init_mm,pud,__va(pmd_phys));+spin_unlock(&init_mm.page_table_lock);++/* Do a global flush tlb after splitting a large page */+flush_tlb_all();+}++pmd=map_low_page((pmd_t*)pud_page_vaddr(*pud));+phys_pmd_remove(pmd,addr,next);+free_pmd_table(pmd,pud);+unmap_low_page(pmd);+}++update_page_count(PG_LEVEL_1G,-pages);+}++void__meminit+kernel_physical_mapping_remove(unsignedlongstart,unsignedlongend)+{+unsignedlongnext;+boolpgd_changed=false;++start=(unsignedlong)__va(start);+end=(unsignedlong)__va(end);++for(;start<end;start=next){+pgd_t*pgd=pgd_offset_k(start);+pud_t*pud;++next=pgd_addr_end(start,end);++if(!pgd_present(*pgd))+continue;++pud=map_low_page((pud_t*)pgd_page_vaddr(*pgd));+phys_pud_remove(pud,__pa(start),__pa(next));+if(free_pud_table(pud,pgd))+pgd_changed=true;+unmap_low_page(pud);+}++if(pgd_changed)+sync_global_pgds(start,end-1);++flush_tlb_all();+}+#ifdef CONFIG_MEMORY_HOTREMOVEint__refarch_remove_memory(u64start,u64size){
@@ -692,6 +921,8 @@ int __ref arch_remove_memory(u64 start, u64 size)ret=__remove_pages(zone,start_pfn,nr_pages);WARN_ON_ONCE(ret);+kernel_physical_mapping_remove(start,start+size);+returnret;}#endif
From: Yasuaki Ishimatsu <redacted>
When a memory is added, we update zone's and pgdat's start_pfn and
spanned_pages in the function __add_zone(). So we should revert them
when the memory is removed.
The patch adds a new function __remove_zone() to do this.
CC: David Rientjes <rientjes@google.com>
CC: Jiang Liu <redacted>
CC: Len Brown <redacted>
CC: Christoph Lameter <redacted>
Cc: Minchan Kim <redacted>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: KOSAKI Motohiro <redacted>
Signed-off-by: Yasuaki Ishimatsu <redacted>
Signed-off-by: Wen Congyang <redacted>
---
mm/memory_hotplug.c | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 207 insertions(+)
@@ -301,10 +301,213 @@ static int __meminit __add_section(int nid, struct zone *zone,returnregister_new_memory(nid,__pfn_to_section(phys_start_pfn));}+/* find the smallest valid pfn in the range [start_pfn, end_pfn) */+staticintfind_smallest_section_pfn(intnid,structzone*zone,+unsignedlongstart_pfn,+unsignedlongend_pfn)+{+structmem_section*ms;++for(;start_pfn<end_pfn;start_pfn+=PAGES_PER_SECTION){+ms=__pfn_to_section(start_pfn);++if(unlikely(!valid_section(ms)))+continue;++if(unlikely(pfn_to_nid(start_pfn)!=nid))+continue;++if(zone&&zone!=page_zone(pfn_to_page(start_pfn)))+continue;++returnstart_pfn;+}++return0;+}++/* find the biggest valid pfn in the range [start_pfn, end_pfn). */+staticintfind_biggest_section_pfn(intnid,structzone*zone,+unsignedlongstart_pfn,+unsignedlongend_pfn)+{+structmem_section*ms;+unsignedlongpfn;++/* pfn is the end pfn of a memory section. */+pfn=end_pfn-1;+for(;pfn>=start_pfn;pfn-=PAGES_PER_SECTION){+ms=__pfn_to_section(pfn);++if(unlikely(!valid_section(ms)))+continue;++if(unlikely(pfn_to_nid(pfn)!=nid))+continue;++if(zone&&zone!=page_zone(pfn_to_page(pfn)))+continue;++returnpfn;+}++return0;+}++staticvoidshrink_zone_span(structzone*zone,unsignedlongstart_pfn,+unsignedlongend_pfn)+{+unsignedlongzone_start_pfn=zone->zone_start_pfn;+unsignedlongzone_end_pfn=zone->zone_start_pfn+zone->spanned_pages;+unsignedlongpfn;+structmem_section*ms;+intnid=zone_to_nid(zone);++zone_span_writelock(zone);+if(zone_start_pfn==start_pfn){+/*+*Ifthesectionissmallestsectioninthezone,itneed+*shrinkzone->zone_start_pfnandzone->zone_spanned_pages.+*Inthiscase,wefindsecondsmallestvalidmem_section+*forshrinkingzone.+*/+pfn=find_smallest_section_pfn(nid,zone,end_pfn,+zone_end_pfn);+if(pfn){+zone->zone_start_pfn=pfn;+zone->spanned_pages=zone_end_pfn-pfn;+}+}elseif(zone_end_pfn==end_pfn){+/*+*Ifthesectionisbiggestsectioninthezone,itneed+*shrinkzone->spanned_pages.+*Inthiscase,wefindsecondbiggestvalidmem_sectionfor+*shrinkingzone.+*/+pfn=find_biggest_section_pfn(nid,zone,zone_start_pfn,+start_pfn);+if(pfn)+zone->spanned_pages=pfn-zone_start_pfn+1;+}++/*+*Thesectionisnotbiggestorsmallestmem_sectioninthezone,it+*onlycreatesaholeinthezone.Sointhiscase,weneednot+*changethezone.Butperhaps,thezonehasonlyholedata.Thus+*itcheckthezonehasonlyholeornot.+*/+pfn=zone_start_pfn;+for(;pfn<zone_end_pfn;pfn+=PAGES_PER_SECTION){+ms=__pfn_to_section(pfn);++if(unlikely(!valid_section(ms)))+continue;++if(page_zone(pfn_to_page(pfn))!=zone)+continue;++/* If the section is current section, it continues the loop */+if(start_pfn==pfn)+continue;++/* If we find valid section, we have nothing to do */+zone_span_writeunlock(zone);+return;+}++/* The zone has no valid section */+zone->zone_start_pfn=0;+zone->spanned_pages=0;+zone_span_writeunlock(zone);+}++staticvoidshrink_pgdat_span(structpglist_data*pgdat,+unsignedlongstart_pfn,unsignedlongend_pfn)+{+unsignedlongpgdat_start_pfn=pgdat->node_start_pfn;+unsignedlongpgdat_end_pfn=+pgdat->node_start_pfn+pgdat->node_spanned_pages;+unsignedlongpfn;+structmem_section*ms;+intnid=pgdat->node_id;++if(pgdat_start_pfn==start_pfn){+/*+*Ifthesectionissmallestsectioninthepgdat,itneed+*shrinkpgdat->node_start_pfnandpgdat->node_spanned_pages.+*Inthiscase,wefindsecondsmallestvalidmem_section+*forshrinkingzone.+*/+pfn=find_smallest_section_pfn(nid,NULL,end_pfn,+pgdat_end_pfn);+if(pfn){+pgdat->node_start_pfn=pfn;+pgdat->node_spanned_pages=pgdat_end_pfn-pfn;+}+}elseif(pgdat_end_pfn==end_pfn){+/*+*Ifthesectionisbiggestsectioninthepgdat,itneed+*shrinkpgdat->node_spanned_pages.+*Inthiscase,wefindsecondbiggestvalidmem_sectionfor+*shrinkingzone.+*/+pfn=find_biggest_section_pfn(nid,NULL,pgdat_start_pfn,+start_pfn);+if(pfn)+pgdat->node_spanned_pages=pfn-pgdat_start_pfn+1;+}++/*+*Ifthesectionisnotbiggestorsmallestmem_sectioninthepgdat,+*itonlycreatesaholeinthepgdat.Sointhiscase,weneednot+*changethepgdat.+*Butperhaps,thepgdathasonlyholedata.Thusitcheckthepgdat+*hasonlyholeornot.+*/+pfn=pgdat_start_pfn;+for(;pfn<pgdat_end_pfn;pfn+=PAGES_PER_SECTION){+ms=__pfn_to_section(pfn);++if(unlikely(!valid_section(ms)))+continue;++if(pfn_to_nid(pfn)!=nid)+continue;++/* If the section is current section, it continues the loop */+if(start_pfn==pfn)+continue;++/* If we find valid section, we have nothing to do */+return;+}++/* The pgdat has no valid section */+pgdat->node_start_pfn=0;+pgdat->node_spanned_pages=0;+}++staticvoid__remove_zone(structzone*zone,unsignedlongstart_pfn)+{+structpglist_data*pgdat=zone->zone_pgdat;+intnr_pages=PAGES_PER_SECTION;+intzone_type;+unsignedlongflags;++zone_type=zone-pgdat->node_zones;++pgdat_resize_lock(zone->zone_pgdat,&flags);+shrink_zone_span(zone,start_pfn,start_pfn+nr_pages);+shrink_pgdat_span(pgdat,start_pfn,start_pfn+nr_pages);+pgdat_resize_unlock(zone->zone_pgdat,&flags);+}+staticint__remove_section(structzone*zone,structmem_section*ms){unsignedlongflags;structpglist_data*pgdat=zone->zone_pgdat;+unsignedlongstart_pfn;+intscn_nr;intret=-EINVAL;if(!valid_section(ms))
@@ -314,6 +517,10 @@ static int __remove_section(struct zone *zone, struct mem_section *ms)if(ret)returnret;+scn_nr=__section_nr(ms);+start_pfn=section_nr_to_pfn(scn_nr);+__remove_zone(zone,start_pfn);+pgdat_resize_lock(pgdat,&flags);sparse_remove_one_section(zone,ms);pgdat_resize_unlock(pgdat,&flags);
offlining memory blocks and checking whether memory blocks are offlined
are very similar. This patch introduces a new function to remove
redundant codes.
CC: David Rientjes <rientjes@google.com>
CC: Jiang Liu <redacted>
CC: Len Brown <redacted>
CC: Christoph Lameter <redacted>
Cc: Minchan Kim <redacted>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: KOSAKI Motohiro <redacted>
CC: Yasuaki Ishimatsu <redacted>
Signed-off-by: Wen Congyang <redacted>
---
mm/memory_hotplug.c | 101 ++++++++++++++++++++++++++++------------------------
1 file changed, 55 insertions(+), 46 deletions(-)
@@ -1005,20 +1005,14 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages)return__offline_pages(start_pfn,start_pfn+nr_pages,120*HZ);}-intremove_memory(u64start,u64size)+staticintwalk_memory_range(unsignedlongstart_pfn,unsignedlongend_pfn,+void*arg,int(*func)(structmemory_block*,void*)){structmemory_block*mem=NULL;structmem_section*section;-unsignedlongstart_pfn,end_pfn;unsignedlongpfn,section_nr;intret;-intreturn_on_error=0;-intretry=0;--start_pfn=PFN_DOWN(start);-end_pfn=start_pfn+PFN_DOWN(size);-repeat:for(pfn=start_pfn;pfn<end_pfn;pfn+=PAGES_PER_SECTION){section_nr=pfn_to_section_nr(pfn);if(!present_section_nr(section_nr))
@@ -1035,22 +1029,61 @@ repeat:if(!mem)continue;-ret=offline_memory_block(mem);+ret=func(mem,arg);if(ret){-if(return_on_error){-kobject_put(&mem->dev.kobj);-returnret;-}else{-retry=1;-}+kobject_put(&mem->dev.kobj);+returnret;}}if(mem)kobject_put(&mem->dev.kobj);-if(retry){-return_on_error=1;+return0;+}++staticintoffline_memory_block_cb(structmemory_block*mem,void*arg)+{+int*ret=arg;+interror=offline_memory_block(mem);++if(error!=0&&*ret==0)+*ret=error;++return0;+}++staticintis_memblock_offlined_cb(structmemory_block*mem,void*arg)+{+intret=!is_memblock_offlined(mem);++if(unlikely(ret))+pr_warn("removing memory fails, because memory "+"[%#010llx-%#010llx] is onlined\n",+PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)),+PFN_PHYS(section_nr_to_pfn(mem->end_section_nr+1))-1);++returnret;+}++intremove_memory(u64start,u64size)+{+unsignedlongstart_pfn,end_pfn;+intret=0;+intretry=1;++start_pfn=PFN_DOWN(start);+end_pfn=start_pfn+PFN_DOWN(size);++repeat:+walk_memory_range(start_pfn,end_pfn,&ret,+offline_memory_block_cb);+if(ret){+if(!retry)+returnret;++retry=0;+ret=0;gotorepeat;}
@@ -1068,37 +1101,13 @@ repeat:*memoryblocksareofflined.*/-for(pfn=start_pfn;pfn<end_pfn;pfn+=PAGES_PER_SECTION){-section_nr=pfn_to_section_nr(pfn);-if(!present_section_nr(section_nr))-continue;--section=__nr_to_section(section_nr);-/* same memblock? */-if(mem)-if((section_nr>=mem->start_section_nr)&&-(section_nr<=mem->end_section_nr))-continue;--mem=find_memory_block_hinted(section,mem);-if(!mem)-continue;--ret=is_memblock_offlined(mem);-if(!ret){-pr_warn("removing memory fails, because memory "-"[%#010llx-%#010llx] is onlined\n",-PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)),-PFN_PHYS(section_nr_to_pfn(mem->end_section_nr+1))-1);--kobject_put(&mem->dev.kobj);-unlock_memory_hotplug();-returnret;-}+ret=walk_memory_range(start_pfn,end_pfn,NULL,+is_memblock_offlined_cb);+if(ret){+unlock_memory_hotplug();+returnret;}-if(mem)-kobject_put(&mem->dev.kobj);unlock_memory_hotplug();return0;
From: Yasuaki Ishimatsu <redacted>
When (hot)adding memory into system, /sys/firmware/memmap/X/{end, start, type}
sysfs files are created. But there is no code to remove these files. The patch
implements the function to remove them.
Note: The code does not free firmware_map_entry which is allocated by bootmem.
So the patch makes memory leak. But I think the memory leak size is
very samll. And it does not affect the system.
CC: David Rientjes <rientjes@google.com>
CC: Jiang Liu <redacted>
CC: Len Brown <redacted>
CC: Christoph Lameter <redacted>
Cc: Minchan Kim <redacted>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: KOSAKI Motohiro <redacted>
Signed-off-by: Wen Congyang <redacted>
Signed-off-by: Yasuaki Ishimatsu <redacted>
---
drivers/firmware/memmap.c | 98 +++++++++++++++++++++++++++++++++++++++++++-
include/linux/firmware-map.h | 6 +++
mm/memory_hotplug.c | 5 ++-
3 files changed, 106 insertions(+), 3 deletions(-)
@@ -41,6 +42,7 @@ struct firmware_map_entry {constchar*type;/* type of the memory range */structlist_headlist;/* entry for the linked list */structkobjectkobj;/* kobject for each entry */+unsignedintbootmem:1;/* allocated from bootmem */};/*
@@ -79,7 +81,26 @@ static const struct sysfs_ops memmap_attr_ops = {.show=memmap_attr_show,};++staticinlinestructfirmware_map_entry*+to_memmap_entry(structkobject*kobj)+{+returncontainer_of(kobj,structfirmware_map_entry,kobj);+}++staticvoidrelease_firmware_map_entry(structkobject*kobj)+{+structfirmware_map_entry*entry=to_memmap_entry(kobj);++if(entry->bootmem)+/* There is no way to free memory allocated from bootmem */+return;++kfree(entry);+}+staticstructkobj_typememmap_ktype={+.release=release_firmware_map_entry,.sysfs_ops=&memmap_attr_ops,.default_attrs=def_attrs,};
From: Yasuaki Ishimatsu <redacted>
For removing memmap region of sparse-vmemmap which is allocated bootmem,
memmap region of sparse-vmemmap needs to be registered by get_page_bootmem().
So the patch searches pages of virtual mapping and registers the pages by
get_page_bootmem().
Note: register_page_bootmem_memmap() is not implemented for ia64, ppc, s390,
and sparc.
CC: David Rientjes <rientjes@google.com>
CC: Jiang Liu <redacted>
CC: Len Brown <redacted>
CC: Christoph Lameter <redacted>
Cc: Minchan Kim <redacted>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: KOSAKI Motohiro <redacted>
Signed-off-by: Wen Congyang <redacted>
Signed-off-by: Yasuaki Ishimatsu <redacted>
---
arch/ia64/mm/discontig.c | 6 +++++
arch/powerpc/mm/init_64.c | 6 +++++
arch/s390/mm/vmem.c | 6 +++++
arch/sparc/mm/init_64.c | 6 +++++
arch/x86/mm/init_64.c | 52 ++++++++++++++++++++++++++++++++++++++++++
include/linux/memory_hotplug.h | 11 ++-------
include/linux/mm.h | 3 ++-
mm/memory_hotplug.c | 33 +++++++++++++++++++++++----
8 files changed, 109 insertions(+), 14 deletions(-)
From: Yasuaki Ishimatsu <redacted>
We remove the memory like this:
1. lock memory hotplug
2. offline a memory block
3. unlock memory hotplug
4. repeat 1-3 to offline all memory blocks
5. lock memory hotplug
6. remove memory(TODO)
7. unlock memory hotplug
All memory blocks must be offlined before removing memory. But we don't hold
the lock in the whole operation. So we should check whether all memory blocks
are offlined before step6. Otherwise, kernel maybe panicked.
CC: David Rientjes <rientjes@google.com>
CC: Jiang Liu <redacted>
CC: Len Brown <redacted>
CC: Christoph Lameter <redacted>
Cc: Minchan Kim <redacted>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: KOSAKI Motohiro <redacted>
Signed-off-by: Wen Congyang <redacted>
Signed-off-by: Yasuaki Ishimatsu <redacted>
---
drivers/base/memory.c | 6 ++++++
include/linux/memory_hotplug.h | 1 +
mm/memory_hotplug.c | 47 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 54 insertions(+)
@@ -675,6 +675,12 @@ int offline_memory_block(struct memory_block *mem)returnret;}+/* return true if the memory block is offlined, otherwise, return false */+boolis_memblock_offlined(structmemory_block*mem)+{+returnmem->state==MEM_OFFLINE;+}+/**Initializethesysfssupportformemorydevices...*/
@@ -1054,6 +1054,53 @@ repeat:gotorepeat;}+lock_memory_hotplug();++/*+*wehaveofflinedallmemoryblockslikethis:+*1.lockmemoryhotplug+*2.offlineamemoryblock+*3.unlockmemoryhotplug+*+*repeatstep1-3toofflinethememoryblock.Allmemoryblocks+*mustbeofflinedbeforeremovingmemory.Butwedon'tholdthe+*lockinthewholeoperation.Soweshouldcheckwhetherall+*memoryblocksareofflined.+*/++for(pfn=start_pfn;pfn<end_pfn;pfn+=PAGES_PER_SECTION){+section_nr=pfn_to_section_nr(pfn);+if(!present_section_nr(section_nr))+continue;++section=__nr_to_section(section_nr);+/* same memblock? */+if(mem)+if((section_nr>=mem->start_section_nr)&&+(section_nr<=mem->end_section_nr))+continue;++mem=find_memory_block_hinted(section,mem);+if(!mem)+continue;++ret=is_memblock_offlined(mem);+if(!ret){+pr_warn("removing memory fails, because memory "+"[%#010llx-%#010llx] is onlined\n",+PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)),+PFN_PHYS(section_nr_to_pfn(mem->end_section_nr+1))-1);++kobject_put(&mem->dev.kobj);+unlock_memory_hotplug();+returnret;+}+}++if(mem)+kobject_put(&mem->dev.kobj);+unlock_memory_hotplug();+return0;}#else
Please include the rationale within each version of the patchset rather
than by linking to an old email. Because
a) this way, more people are likely to read it
b) it permits the text to be maimtained as the code evolves
c) it permits the text to be included in the mainlnie commit, where
people can find it.
Please include the rationale within each version of the patchset rather
than by linking to an old email. Because
a) this way, more people are likely to read it
b) it permits the text to be maimtained as the code evolves
c) it permits the text to be included in the mainlnie commit, where
people can find it.
What's happening with the acpi framework? has it received any feedback
from the ACPI developers?
This particular series is in my tree waiting for the v3.8 merge window.
There's a new one sent yesterday and this one is pending a review. I'm
not sure if the $subject patchset depends on it, though.
It looks like there are too many hotplug patchsets flying left and right and
it's getting hard to keep track of them all.
Thanks,
Rafael
--
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
As we're now at -rc7 I'd prefer to take a look at all of this after the
3.7 release - please resend everything shortly after 3.8-rc1.
Almost patches about memory hotplug has been merged into your and Rafael's
tree. And these patches are waiting to open the v3.8 merge window.
Remaining patches are only this patch-set. So we hope that this patch-set
is merged into v3.8.
In merging this patch-set into v3.8, Linux on x86_64 makes a memory hot plug
possible.
Thanks,
Yasuaki Ishimatsu
Please include the rationale within each version of the patchset rather
than by linking to an old email. Because
a) this way, more people are likely to read it
b) it permits the text to be maimtained as the code evolves
c) it permits the text to be included in the mainlnie commit, where
people can find it.
Hi Congyang,
I think vmemmap's pgtable pages should be freed after all entries are cleared, I have a patch to do this.
The code logic is the same as [Patch v4 09/12] memory-hotplug: remove page table of x86_64 architecture.
How do you think about this?
Signed-off-by: Jianguo Wu <redacted>
Signed-off-by: Jiang Liu <redacted>
---
include/linux/mm.h | 1 +
mm/sparse-vmemmap.c | 214 +++++++++++++++++++++++++++++++++++++++++++++++++++
mm/sparse.c | 5 +-
3 files changed, 218 insertions(+), 2 deletions(-)
@@ -1642,6 +1642,7 @@ int vmemmap_populate(struct page *start_page, unsigned long pages, int node);voidvmemmap_populate_print_last(void);voidregister_page_bootmem_memmap(unsignedlongsection_nr,structpage*map,unsignedlongsize);+voidvmemmap_free(structpage*memmap,unsignedlongnr_pages);enummf_flags{MF_COUNT_INCREASED=1<<0,
@@ -613,12 +613,13 @@ static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,/* This will make the necessary allocations eventually. */returnsparse_mem_map_populate(pnum,nid);}-staticvoid__kfree_section_memmap(structpage*memmap,unsignedlongnr_pages)+staticvoid__kfree_section_memmap(structpage*page,unsignedlongnr_pages){-return;/* XXX: Not implemented yet */+vmemmap_free(page,nr_pages);}staticvoidfree_map_bootmem(structpage*page,unsignedlongnr_pages){+vmemmap_free(page,nr_pages);}#elsestaticstructpage*__kmalloc_section_memmap(unsignedlongnr_pages)
--
1.7.6.1
On 2012/11/27 18:00, Wen Congyang wrote:
> From: Yasuaki Ishimatsu <isimatu.yasuaki@jp.fujitsu.com>
>
> All pages of virtual mapping in removed memory cannot be freed, since some pages
> used as PGD/PUD includes not only removed memory but also other memory. So the
> patch checks whether page can be freed or not.
>
> How to check whether page can be freed or not?
> 1. When removing memory, the page structs of the revmoved memory are filled
> with 0FD.
> 2. All page structs are filled with 0xFD on PT/PMD, PT/PMD can be cleared.
> In this case, the page used as PT/PMD can be freed.
>
> Applying patch, __remove_section() of CONFIG_SPARSEMEM_VMEMMAP is integrated
> into one. So __remove_section() of CONFIG_SPARSEMEM_VMEMMAP is deleted.
>
> Note: vmemmap_kfree() and vmemmap_free_bootmem() are not implemented for ia64,
> ppc, s390, and sparc.
>
> CC: David Rientjes <rientjes@google.com>
> CC: Jiang Liu <liuj97@gmail.com>
> CC: Len Brown <len.brown@intel.com>
> CC: Christoph Lameter <cl@linux.com>
> Cc: Minchan Kim <minchan.kim@gmail.com>
> CC: Andrew Morton <akpm@linux-foundation.org>
> CC: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
> Signed-off-by: Yasuaki Ishimatsu <isimatu.yasuaki@jp.fujitsu.com>
> Signed-off-by: Jianguo Wu <wujianguo@huawei.com>
> Signed-off-by: Wen Congyang <wency@cn.fujitsu.com>
> ---
> arch/ia64/mm/discontig.c | 8 ++++
> arch/powerpc/mm/init_64.c | 8 ++++
> arch/s390/mm/vmem.c | 8 ++++
> arch/sparc/mm/init_64.c | 8 ++++
> arch/x86/mm/init_64.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++
> include/linux/mm.h | 2 +
> mm/memory_hotplug.c | 17 +------
> mm/sparse.c | 19 ++++----
> 8 files changed, 165 insertions(+), 24 deletions(-)
>
> diff --git a/arch/ia64/mm/discontig.c b/arch/ia64/mm/discontig.c
> index 33943db..0d23b69 100644
> --- a/arch/ia64/mm/discontig.c
> +++ b/arch/ia64/mm/discontig.c
> @@ -823,6 +823,14 @@ int __meminit vmemmap_populate(struct page *start_page,
> return vmemmap_populate_basepages(start_page, size, node);
> }
>
> +void vmemmap_kfree(struct page *memmap, unsigned long nr_pages)
> +{
> +}
> +
> +void vmemmap_free_bootmem(struct page *memmap, unsigned long nr_pages)
> +{
> +}
> +
> void register_page_bootmem_memmap(unsigned long section_nr,
> struct page *start_page, unsigned long size)
> {
> diff --git a/arch/powerpc/mm/init_64.c b/arch/powerpc/mm/init_64.c
> index 6466440..df7d155 100644
> --- a/arch/powerpc/mm/init_64.c
> +++ b/arch/powerpc/mm/init_64.c
> @@ -298,6 +298,14 @@ int __meminit vmemmap_populate(struct page *start_page,
> return 0;
> }
>
> +void vmemmap_kfree(struct page *memmap, unsigned long nr_pages)
> +{
> +}
> +
> +void vmemmap_free_bootmem(struct page *memmap, unsigned long nr_pages)
> +{
> +}
> +
> void register_page_bootmem_memmap(unsigned long section_nr,
> struct page *start_page, unsigned long size)
> {
> diff --git a/arch/s390/mm/vmem.c b/arch/s390/mm/vmem.c
> index 4f4803a..ab69c34 100644
> --- a/arch/s390/mm/vmem.c
> +++ b/arch/s390/mm/vmem.c
> @@ -236,6 +236,14 @@ out:
> return ret;
> }
>
> +void vmemmap_kfree(struct page *memmap, unsigned long nr_pages)
> +{
> +}
> +
> +void vmemmap_free_bootmem(struct page *memmap, unsigned long nr_pages)
> +{
> +}
> +
> void register_page_bootmem_memmap(unsigned long section_nr,
> struct page *start_page, unsigned long size)
> {
> diff --git a/arch/sparc/mm/init_64.c b/arch/sparc/mm/init_64.c
> index 75a984b..546855d 100644
> --- a/arch/sparc/mm/init_64.c
> +++ b/arch/sparc/mm/init_64.c
> @@ -2232,6 +2232,14 @@ void __meminit vmemmap_populate_print_last(void)
> }
> }
>
> +void vmemmap_kfree(struct page *memmap, unsigned long nr_pages)
> +{
> +}
> +
> +void vmemmap_free_bootmem(struct page *memmap, unsigned long nr_pages)
> +{
> +}
> +
> void register_page_bootmem_memmap(unsigned long section_nr,
> struct page *start_page, unsigned long size)
> {
> diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c
> index 795dae3..e85626d 100644
> --- a/arch/x86/mm/init_64.c
> +++ b/arch/x86/mm/init_64.c
> @@ -998,6 +998,125 @@ vmemmap_populate(struct page *start_page, unsigned long size, int node)
> return 0;
> }
>
> +#define PAGE_INUSE 0xFD
> +
> +unsigned long find_and_clear_pte_page(unsigned long addr, unsigned long end,
> + struct page **pp, int *page_size)
> +{
> + pgd_t *pgd;
> + pud_t *pud;
> + pmd_t *pmd;
> + pte_t *pte = NULL;
> + void *page_addr;
> + unsigned long next;
> +
> + *pp = NULL;
> +
> + pgd = pgd_offset_k(addr);
> + if (pgd_none(*pgd))
> + return pgd_addr_end(addr, end);
> +
> + pud = pud_offset(pgd, addr);
> + if (pud_none(*pud))
> + return pud_addr_end(addr, end);
> +
> + if (!cpu_has_pse) {
> + next = (addr + PAGE_SIZE) & PAGE_MASK;
> + pmd = pmd_offset(pud, addr);
> + if (pmd_none(*pmd))
> + return next;
> +
> + pte = pte_offset_kernel(pmd, addr);
> + if (pte_none(*pte))
> + return next;
> +
> + *page_size = PAGE_SIZE;
> + *pp = pte_page(*pte);
> + } else {
> + next = pmd_addr_end(addr, end);
> +
> + pmd = pmd_offset(pud, addr);
> + if (pmd_none(*pmd))
> + return next;
> +
> + *page_size = PMD_SIZE;
> + *pp = pmd_page(*pmd);
> + }
> +
> + /*
> + * Removed page structs are filled with 0xFD.
> + */
> + memset((void *)addr, PAGE_INUSE, next - addr);
> +
> + page_addr = page_address(*pp);
> +
> + /*
> + * Check the page is filled with 0xFD or not.
> + * memchr_inv() returns the address. In this case, we cannot
> + * clear PTE/PUD entry, since the page is used by other.
> + * So we cannot also free the page.
> + *
> + * memchr_inv() returns NULL. In this case, we can clear
> + * PTE/PUD entry, since the page is not used by other.
> + * So we can also free the page.
> + */
> + if (memchr_inv(page_addr, PAGE_INUSE, *page_size)) {
> + *pp = NULL;
> + return next;
> + }
> +
> + if (!cpu_has_pse)
> + pte_clear(&init_mm, addr, pte);
> + else
> + pmd_clear(pmd);
> +
> + return next;
> +}
> +
> +void vmemmap_kfree(struct page *memmap, unsigned long nr_pages)
> +{
> + unsigned long addr = (unsigned long)memmap;
> + unsigned long end = (unsigned long)(memmap + nr_pages);
> + unsigned long next;
> + struct page *page;
> + int page_size;
> +
> + for (; addr < end; addr = next) {
> + page = NULL;
> + page_size = 0;
> + next = find_and_clear_pte_page(addr, end, &page, &page_size);
> + if (!page)
> + continue;
> +
> + free_pages((unsigned long)page_address(page),
> + get_order(page_size));
> + __flush_tlb_one(addr);
> + }
> +}
> +
> +void vmemmap_free_bootmem(struct page *memmap, unsigned long nr_pages)
> +{
> + unsigned long addr = (unsigned long)memmap;
> + unsigned long end = (unsigned long)(memmap + nr_pages);
> + unsigned long next;
> + struct page *page;
> + int page_size;
> + unsigned long magic;
> +
> + for (; addr < end; addr = next) {
> + page = NULL;
> + page_size = 0;
> + next = find_and_clear_pte_page(addr, end, &page, &page_size);
> + if (!page)
> + continue;
> +
> + magic = (unsigned long) page->lru.next;
> + if (magic == SECTION_INFO)
> + put_page_bootmem(page);
> + flush_tlb_kernel_range(addr, end);
> + }
> +}
> +
> void register_page_bootmem_memmap(unsigned long section_nr,
> struct page *start_page, unsigned long size)
> {
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 5657670..94d5ccd 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -1642,6 +1642,8 @@ int vmemmap_populate(struct page *start_page, unsigned long pages, int node);
> void vmemmap_populate_print_last(void);
> void register_page_bootmem_memmap(unsigned long section_nr, struct page *map,
> unsigned long size);
> +void vmemmap_kfree(struct page *memmpa, unsigned long nr_pages);
> +void vmemmap_free_bootmem(struct page *memmpa, unsigned long nr_pages);
>
> enum mf_flags {
> MF_COUNT_INCREASED = 1 << 0,
> diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
> index ccc11b6..7797e91 100644
> --- a/mm/memory_hotplug.c
> +++ b/mm/memory_hotplug.c
> @@ -301,19 +301,6 @@ static int __meminit __add_section(int nid, struct zone *zone,
> return register_new_memory(nid, __pfn_to_section(phys_start_pfn));
> }
>
> -#ifdef CONFIG_SPARSEMEM_VMEMMAP
> -static int __remove_section(struct zone *zone, struct mem_section *ms)
> -{
> - int ret = -EINVAL;
> -
> - if (!valid_section(ms))
> - return ret;
> -
> - ret = unregister_memory_section(ms);
> -
> - return ret;
> -}
> -#else
> static int __remove_section(struct zone *zone, struct mem_section *ms)
> {
> unsigned long flags;
> @@ -330,9 +317,9 @@ static int __remove_section(struct zone *zone, struct mem_section *ms)
> pgdat_resize_lock(pgdat, &flags);
> sparse_remove_one_section(zone, ms);
> pgdat_resize_unlock(pgdat, &flags);
> - return 0;
> +
> + return ret;
> }
> -#endif
>
> /*
> * Reasonably generic function for adding memory. It is
> diff --git a/mm/sparse.c b/mm/sparse.c
> index fac95f2..c723bc2 100644
> --- a/mm/sparse.c
> +++ b/mm/sparse.c
> @@ -613,12 +613,13 @@ static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
> /* This will make the necessary allocations eventually. */
> return sparse_mem_map_populate(pnum, nid);
> }
> -static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
> +static void __kfree_section_memmap(struct page *page, unsigned long nr_pages)
> {
> - return; /* XXX: Not implemented yet */
> + vmemmap_kfree(page, nr_pages);
> }
> -static void free_map_bootmem(struct page *page, unsigned long nr_pages)
> +static void free_map_bootmem(struct page *page)
> {
> + vmemmap_free_bootmem(page, PAGES_PER_SECTION);
> }
> #else
> static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
> @@ -658,10 +659,14 @@ static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
> get_order(sizeof(struct page) * nr_pages));
> }
>
> -static void free_map_bootmem(struct page *page, unsigned long nr_pages)
> +static void free_map_bootmem(struct page *page)
> {
> unsigned long maps_section_nr, removing_section_nr, i;
> unsigned long magic;
> + unsigned long nr_pages;
> +
> + nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
> + >> PAGE_SHIFT;
>
> for (i = 0; i < nr_pages; i++, page++) {
> magic = (unsigned long) page->lru.next;
> @@ -688,7 +693,6 @@ static void free_map_bootmem(struct page *page, unsigned long nr_pages)
> static void free_section_usemap(struct page *memmap, unsigned long *usemap)
> {
> struct page *usemap_page;
> - unsigned long nr_pages;
>
> if (!usemap)
> return;
> @@ -713,10 +717,7 @@ static void free_section_usemap(struct page *memmap, unsigned long *usemap)
> struct page *memmap_page;
> memmap_page = virt_to_page(memmap);
>
> - nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
> - >> PAGE_SHIFT;
> -
> - free_map_bootmem(memmap_page, nr_pages);
> + free_map_bootmem(memmap_page);
> }
> }
>
Hi Congyang,
I think vmemmap's pgtable pages should be freed after all entries are cleared, I have a patch to do this.
The code logic is the same as [Patch v4 09/12] memory-hotplug: remove page table of x86_64 architecture.
How do you think about this?
Signed-off-by: Jianguo Wu <redacted>
Signed-off-by: Jiang Liu <redacted>
---
include/linux/mm.h | 1 +
mm/sparse-vmemmap.c | 214 +++++++++++++++++++++++++++++++++++++++++++++++++++
mm/sparse.c | 5 +-
3 files changed, 218 insertions(+), 2 deletions(-)
@@ -1642,6 +1642,7 @@ int vmemmap_populate(struct page *start_page, unsigned long pages, int node);voidvmemmap_populate_print_last(void);voidregister_page_bootmem_memmap(unsignedlongsection_nr,structpage*map,unsignedlongsize);+voidvmemmap_free(structpage*memmap,unsignedlongnr_pages);enummf_flags{MF_COUNT_INCREASED=1<<0,
@@ -613,12 +613,13 @@ static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,/* This will make the necessary allocations eventually. */returnsparse_mem_map_populate(pnum,nid);}-staticvoid__kfree_section_memmap(structpage*memmap,unsignedlongnr_pages)+staticvoid__kfree_section_memmap(structpage*page,unsignedlongnr_pages)
Hi Congyang,
Thanks for your review and comments.
On 2012/11/30 9:45, Wen Congyang wrote:
At 11/28/2012 05:40 PM, Jianguo Wu Wrote:
quoted
Hi Congyang,
I think vmemmap's pgtable pages should be freed after all entries are cleared, I have a patch to do this.
The code logic is the same as [Patch v4 09/12] memory-hotplug: remove page table of x86_64 architecture.
How do you think about this?
Signed-off-by: Jianguo Wu <redacted>
Signed-off-by: Jiang Liu <redacted>
---
include/linux/mm.h | 1 +
mm/sparse-vmemmap.c | 214 +++++++++++++++++++++++++++++++++++++++++++++++++++
mm/sparse.c | 5 +-
3 files changed, 218 insertions(+), 2 deletions(-)
@@ -1642,6 +1642,7 @@ int vmemmap_populate(struct page *start_page, unsigned long pages, int node);voidvmemmap_populate_print_last(void);voidregister_page_bootmem_memmap(unsignedlongsection_nr,structpage*map,unsignedlongsize);+voidvmemmap_free(structpage*memmap,unsignedlongnr_pages);enummf_flags{MF_COUNT_INCREASED=1<<0,
Hmm, vmemmap doesn't use vmalloc() to allocate memory.
yes, this can be removed.
quoted
+ else
+ free_pages((unsigned long)page_address(page), order);
+ }
+}
+
+static void free_pte_table(pmd_t *pmd)
+{
+ pte_t *pte, *pte_start;
+ int i;
+
+ pte_start = (pte_t *)pmd_page_vaddr(*pmd);
+ for (i = 0; i < PTRS_PER_PTE; i++) {
+ pte = pte_start + i;
+ if (pte_val(*pte))
+ return;
+ }
+
+ /* free a pte talbe */
+ vmemmap_free_pages(pmd_page(*pmd), 0);
+ spin_lock(&init_mm.page_table_lock);
+ pmd_clear(pmd);
+ spin_unlock(&init_mm.page_table_lock);
+}
+
+static void free_pmd_table(pud_t *pud)
+{
+ pmd_t *pmd, *pmd_start;
+ int i;
+
+ pmd_start = (pmd_t *)pud_page_vaddr(*pud);
+ for (i = 0; i < PTRS_PER_PMD; i++) {
+ pmd = pmd_start + i;
+ if (pmd_val(*pmd))
+ return;
+ }
+
+ /* free a pmd talbe */
+ vmemmap_free_pages(pud_page(*pud), 0);
+ spin_lock(&init_mm.page_table_lock);
+ pud_clear(pud);
+ spin_unlock(&init_mm.page_table_lock);
+}
+
+static void free_pud_table(pgd_t *pgd)
+{
+ pud_t *pud, *pud_start;
+ int i;
+
+ pud_start = (pud_t *)pgd_page_vaddr(*pgd);
+ for (i = 0; i < PTRS_PER_PUD; i++) {
+ pud = pud_start + i;
+ if (pud_val(*pud))
+ return;
+ }
+
+ /* free a pud table */
+ vmemmap_free_pages(pgd_page(*pgd), 0);
+ spin_lock(&init_mm.page_table_lock);
+ pgd_clear(pgd);
+ spin_unlock(&init_mm.page_table_lock);
+}
+
+static int split_large_page(pte_t *kpte, unsigned long address, pte_t *pbase)
+{
+ struct page *page = pmd_page(*(pmd_t *)kpte);
+ int i = 0;
+ unsigned long magic;
+ unsigned long section_nr;
+
+ __split_large_page(kpte, address, pbase);
+ __flush_tlb_all();
+
+ magic = (unsigned long) page->lru.next;
+ if (magic == SECTION_INFO) {
+ section_nr = pfn_to_section_nr(page_to_pfn(page));
+ while (i < PTRS_PER_PMD) {
+ page++;
+ i++;
+ get_page_bootmem(section_nr, page, SECTION_INFO);
+ }
+ }
+
+ return 0;
+}
+
+static void vmemmap_pte_remove(pmd_t *pmd, unsigned long addr, unsigned long end)
+{
+ pte_t *pte;
+ unsigned long next;
+
+ pte = pte_offset_kernel(pmd, addr);
+ for (; addr < end; pte++, addr += PAGE_SIZE) {
+ next = (addr + PAGE_SIZE) & PAGE_MASK;
+ if (next > end)
+ next = end;
+
+ if (pte_none(*pte))
+ continue;
+ if (IS_ALIGNED(addr, PAGE_SIZE) &&
+ IS_ALIGNED(end, PAGE_SIZE)) {
+ vmemmap_free_pages(pte_page(*pte), 0);
+ spin_lock(&init_mm.page_table_lock);
+ pte_clear(&init_mm, addr, pte);
+ spin_unlock(&init_mm.page_table_lock);
If addr or end is not alianed with PAGE_SIZE, you may leak some
memory.
yes, I think we can handle this situation with the method you mentioned in the change log:
1. When removing memory, the page structs of the revmoved memory are filled
with 0xFD.
2. All page structs are filled with 0xFD on PT/PMD, PT/PMD can be cleared.
In this case, the page used as PT/PMD can be freed.
By the way, why is 0xFD?
quoted
+ }
+ }
+
+ free_pte_table(pmd);
+ __flush_tlb_all();
+}
+
+static void vmemmap_pmd_remove(pud_t *pud, unsigned long addr, unsigned long end)
+{
+ unsigned long next;
+ pmd_t *pmd;
+
+ pmd = pmd_offset(pud, addr);
+ for (; addr < end; addr = next, pmd++) {
+ next = pmd_addr_end(addr, end);
+ if (pmd_none(*pmd))
+ continue;
+
+ if (cpu_has_pse) {
+ unsigned long pte_base;
+
+ if (IS_ALIGNED(addr, PMD_SIZE) &&
+ IS_ALIGNED(next, PMD_SIZE)) {
+ vmemmap_free_pages(pmd_page(*pmd),
+ get_order(PMD_SIZE));
+ spin_lock(&init_mm.page_table_lock);
+ pmd_clear(pmd);
+ spin_unlock(&init_mm.page_table_lock);
+ continue;
+ }
+
+ /*
+ * We use 2M page, but we need to remove part of them,
+ * so split 2M page to 4K page.
+ */
+ pte_base = get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
get_zeored_page() may fail. You should handle this error.
That means system is out of memory, I will trigger a bug_on.
@@ -613,12 +613,13 @@ static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,/* This will make the necessary allocations eventually. */returnsparse_mem_map_populate(pnum,nid);}-staticvoid__kfree_section_memmap(structpage*memmap,unsignedlongnr_pages)+staticvoid__kfree_section_memmap(structpage*page,unsignedlongnr_pages)
Hi Congyang,
Thanks for your review and comments.
On 2012/11/30 9:45, Wen Congyang wrote:
quoted
At 11/28/2012 05:40 PM, Jianguo Wu Wrote:
quoted
Hi Congyang,
I think vmemmap's pgtable pages should be freed after all entries are cleared, I have a patch to do this.
The code logic is the same as [Patch v4 09/12] memory-hotplug: remove page table of x86_64 architecture.
How do you think about this?
Signed-off-by: Jianguo Wu <redacted>
Signed-off-by: Jiang Liu <redacted>
---
include/linux/mm.h | 1 +
mm/sparse-vmemmap.c | 214 +++++++++++++++++++++++++++++++++++++++++++++++++++
mm/sparse.c | 5 +-
3 files changed, 218 insertions(+), 2 deletions(-)
@@ -1642,6 +1642,7 @@ int vmemmap_populate(struct page *start_page, unsigned long pages, int node);voidvmemmap_populate_print_last(void);voidregister_page_bootmem_memmap(unsignedlongsection_nr,structpage*map,unsignedlongsize);+voidvmemmap_free(structpage*memmap,unsignedlongnr_pages);enummf_flags{MF_COUNT_INCREASED=1<<0,
Hmm, vmemmap doesn't use vmalloc() to allocate memory.
yes, this can be removed.
quoted
quoted
+ else
+ free_pages((unsigned long)page_address(page), order);
+ }
+}
+
+static void free_pte_table(pmd_t *pmd)
+{
+ pte_t *pte, *pte_start;
+ int i;
+
+ pte_start = (pte_t *)pmd_page_vaddr(*pmd);
+ for (i = 0; i < PTRS_PER_PTE; i++) {
+ pte = pte_start + i;
+ if (pte_val(*pte))
+ return;
+ }
+
+ /* free a pte talbe */
+ vmemmap_free_pages(pmd_page(*pmd), 0);
+ spin_lock(&init_mm.page_table_lock);
+ pmd_clear(pmd);
+ spin_unlock(&init_mm.page_table_lock);
+}
+
+static void free_pmd_table(pud_t *pud)
+{
+ pmd_t *pmd, *pmd_start;
+ int i;
+
+ pmd_start = (pmd_t *)pud_page_vaddr(*pud);
+ for (i = 0; i < PTRS_PER_PMD; i++) {
+ pmd = pmd_start + i;
+ if (pmd_val(*pmd))
+ return;
+ }
+
+ /* free a pmd talbe */
+ vmemmap_free_pages(pud_page(*pud), 0);
+ spin_lock(&init_mm.page_table_lock);
+ pud_clear(pud);
+ spin_unlock(&init_mm.page_table_lock);
+}
+
+static void free_pud_table(pgd_t *pgd)
+{
+ pud_t *pud, *pud_start;
+ int i;
+
+ pud_start = (pud_t *)pgd_page_vaddr(*pgd);
+ for (i = 0; i < PTRS_PER_PUD; i++) {
+ pud = pud_start + i;
+ if (pud_val(*pud))
+ return;
+ }
+
+ /* free a pud table */
+ vmemmap_free_pages(pgd_page(*pgd), 0);
+ spin_lock(&init_mm.page_table_lock);
+ pgd_clear(pgd);
+ spin_unlock(&init_mm.page_table_lock);
+}
+
+static int split_large_page(pte_t *kpte, unsigned long address, pte_t *pbase)
+{
+ struct page *page = pmd_page(*(pmd_t *)kpte);
+ int i = 0;
+ unsigned long magic;
+ unsigned long section_nr;
+
+ __split_large_page(kpte, address, pbase);
+ __flush_tlb_all();
+
+ magic = (unsigned long) page->lru.next;
+ if (magic == SECTION_INFO) {
+ section_nr = pfn_to_section_nr(page_to_pfn(page));
+ while (i < PTRS_PER_PMD) {
+ page++;
+ i++;
+ get_page_bootmem(section_nr, page, SECTION_INFO);
+ }
+ }
+
+ return 0;
+}
+
+static void vmemmap_pte_remove(pmd_t *pmd, unsigned long addr, unsigned long end)
+{
+ pte_t *pte;
+ unsigned long next;
+
+ pte = pte_offset_kernel(pmd, addr);
+ for (; addr < end; pte++, addr += PAGE_SIZE) {
+ next = (addr + PAGE_SIZE) & PAGE_MASK;
+ if (next > end)
+ next = end;
+
+ if (pte_none(*pte))
+ continue;
+ if (IS_ALIGNED(addr, PAGE_SIZE) &&
+ IS_ALIGNED(end, PAGE_SIZE)) {
+ vmemmap_free_pages(pte_page(*pte), 0);
+ spin_lock(&init_mm.page_table_lock);
+ pte_clear(&init_mm, addr, pte);
+ spin_unlock(&init_mm.page_table_lock);
If addr or end is not alianed with PAGE_SIZE, you may leak some
memory.
yes, I think we can handle this situation with the method you mentioned in the change log:
1. When removing memory, the page structs of the revmoved memory are filled
with 0xFD.
2. All page structs are filled with 0xFD on PT/PMD, PT/PMD can be cleared.
In this case, the page used as PT/PMD can be freed.
By the way, why is 0xFD?
There is no reason. I just filled the page with unique number.
Thanks,
Yasuaki Ishimatsu
quoted
quoted
+ }
+ }
+
+ free_pte_table(pmd);
+ __flush_tlb_all();
+}
+
+static void vmemmap_pmd_remove(pud_t *pud, unsigned long addr, unsigned long end)
+{
+ unsigned long next;
+ pmd_t *pmd;
+
+ pmd = pmd_offset(pud, addr);
+ for (; addr < end; addr = next, pmd++) {
+ next = pmd_addr_end(addr, end);
+ if (pmd_none(*pmd))
+ continue;
+
+ if (cpu_has_pse) {
+ unsigned long pte_base;
+
+ if (IS_ALIGNED(addr, PMD_SIZE) &&
+ IS_ALIGNED(next, PMD_SIZE)) {
+ vmemmap_free_pages(pmd_page(*pmd),
+ get_order(PMD_SIZE));
+ spin_lock(&init_mm.page_table_lock);
+ pmd_clear(pmd);
+ spin_unlock(&init_mm.page_table_lock);
+ continue;
+ }
+
+ /*
+ * We use 2M page, but we need to remove part of them,
+ * so split 2M page to 4K page.
+ */
+ pte_base = get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
get_zeored_page() may fail. You should handle this error.
That means system is out of memory, I will trigger a bug_on.
@@ -613,12 +613,13 @@ static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,/* This will make the necessary allocations eventually. */returnsparse_mem_map_populate(pnum,nid);}-staticvoid__kfree_section_memmap(structpage*memmap,unsignedlongnr_pages)+staticvoid__kfree_section_memmap(structpage*page,unsignedlongnr_pages)
What's happening with the acpi framework? has it received any feedback
from the ACPI developers?
About ACPI framework, we are trying to do the following.
The memory device can be removed by 2 ways:
1. send eject request by SCI
2. echo 1 >/sys/bus/pci/devices/PNP0C80:XX/eject
In the 1st case, acpi_memory_disable_device() will be called.
In the 2nd case, acpi_memory_device_remove() will be called.
acpi_memory_device_remove() will also be called when we unbind the
memory device from the driver acpi_memhotplug or a driver
initialization fails.
acpi_memory_disable_device() has already implemented a code which
offlines memory and releases acpi_memory_info struct . But
acpi_memory_device_remove() has not implemented it yet.
So the patch prepares the framework for hot removing memory and
adds the framework into acpi_memory_device_remove().
All the ACPI related patches have been put into the linux-next branch
of the linux-pm.git tree as v3.8 material.Please refer to the following
url.
https://lkml.org/lkml/2012/11/2/160
So for now, with this patch set, we can do memory hot-remove on x86_64
linux.
I do hope you would merge them before 3.8-rc1, so that we can use this
functionality in 3.8.
As we are still testing all memory hotplug related functionalities, I
hope we can do the bug fix during 3.8 rc.
Thanks. :)
@@ -1642,6 +1642,7 @@ int vmemmap_populate(struct page *start_page, unsigned long pages, int node);voidvmemmap_populate_print_last(void);voidregister_page_bootmem_memmap(unsignedlongsection_nr,structpage*map,unsignedlongsize);+voidvmemmap_free(structpage*memmap,unsignedlongnr_pages);enummf_flags{MF_COUNT_INCREASED=1<<0,
@@ -1642,6 +1642,7 @@ int vmemmap_populate(struct page *start_page, unsigned long pages, int node);voidvmemmap_populate_print_last(void);voidregister_page_bootmem_memmap(unsignedlongsection_nr,structpage*map,unsignedlongsize);+voidvmemmap_free(structpage*memmap,unsignedlongnr_pages);enummf_flags{MF_COUNT_INCREASED=1<<0,
Seems that we have different ways to handle pages allocated by bootmem
or by regular allocator. Is the checking way in [PATCH 09/12] available
here ?
+ /* bootmem page has reserved flag */
+ if (PageReserved(page)) {
......
+ }
If so, I think we can just merge these two functions.
All the free_xxx_table() are very similar to the functions in
[PATCH 09/12]. Could we reuse them anyway ?
+
+static int split_large_page(pte_t *kpte, unsigned long address, pte_t *pbase)
+{
+ struct page *page = pmd_page(*(pmd_t *)kpte);
+ int i = 0;
+ unsigned long magic;
+ unsigned long section_nr;
+
+ __split_large_page(kpte, address, pbase);
Is this patch going to replace [PATCH 08/12] ?
If so, __split_large_page() was added and exported in [PATCH 09/12],
then we should move it here, right ?
If not, free_map_bootmem() and __kfree_section_memmap() were changed in
[PATCH 08/12], and we need to handle this.
quoted hunk
+ __flush_tlb_all();
+
+ magic = (unsigned long) page->lru.next;
+ if (magic == SECTION_INFO) {
+ section_nr = pfn_to_section_nr(page_to_pfn(page));
+ while (i< PTRS_PER_PMD) {
+ page++;
+ i++;
+ get_page_bootmem(section_nr, page, SECTION_INFO);
+ }
+ }
+
+ return 0;
+}
+
+static void vmemmap_pte_remove(pmd_t *pmd, unsigned long addr, unsigned long end)
+{
+ pte_t *pte;
+ unsigned long next;
+ void *page_addr;
+
+ pte = pte_offset_kernel(pmd, addr);
+ for (; addr< end; pte++, addr += PAGE_SIZE) {
+ next = (addr + PAGE_SIZE)& PAGE_MASK;
+ if (next> end)
+ next = end;
+
+ if (pte_none(*pte))
+ continue;
+ if (IS_ALIGNED(addr, PAGE_SIZE)&&
+ IS_ALIGNED(next, PAGE_SIZE)) {
+ vmemmap_free_pages(pte_page(*pte), 0);
+ spin_lock(&init_mm.page_table_lock);
+ pte_clear(&init_mm, addr, pte);
+ spin_unlock(&init_mm.page_table_lock);
+ } else {
+ /*
+ * Removed page structs are filled with 0xFD.
+ */
+ memset((void *)addr, PAGE_INUSE, next - addr);
+ page_addr = page_address(pte_page(*pte));
+
+ if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
+ spin_lock(&init_mm.page_table_lock);
+ pte_clear(&init_mm, addr, pte);
+ spin_unlock(&init_mm.page_table_lock);
+ }
+ }
+ }
+
+ free_pte_table(pmd);
+ __flush_tlb_all();
+}
+
+static void vmemmap_pmd_remove(pud_t *pud, unsigned long addr, unsigned long end)
+{
+ unsigned long next;
+ pmd_t *pmd;
+
+ pmd = pmd_offset(pud, addr);
+ for (; addr< end; addr = next, pmd++) {
+ next = pmd_addr_end(addr, end);
+ if (pmd_none(*pmd))
+ continue;
+
+ if (cpu_has_pse) {
+ unsigned long pte_base;
+
+ if (IS_ALIGNED(addr, PMD_SIZE)&&
+ IS_ALIGNED(next, PMD_SIZE)) {
+ vmemmap_free_pages(pmd_page(*pmd),
+ get_order(PMD_SIZE));
+ spin_lock(&init_mm.page_table_lock);
+ pmd_clear(pmd);
+ spin_unlock(&init_mm.page_table_lock);
+ continue;
+ }
+
+ /*
+ * We use 2M page, but we need to remove part of them,
+ * so split 2M page to 4K page.
+ */
+ pte_base = get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
+ if (!pte_base) {
+ WARN_ON(1);
+ continue;
+ }
+
+ split_large_page((pte_t *)pmd, addr, (pte_t *)pte_base);
+ __flush_tlb_all();
+
+ spin_lock(&init_mm.page_table_lock);
+ pmd_populate_kernel(&init_mm, pmd, (pte_t *)pte_base);
+ spin_unlock(&init_mm.page_table_lock);
+ }
+
+ vmemmap_pte_remove(pmd, addr, next);
+ }
+
+ free_pmd_table(pud);
+ __flush_tlb_all();
+}
+
+static void vmemmap_pud_remove(pgd_t *pgd, unsigned long addr, unsigned long end)
+{
+ unsigned long next;
+ pud_t *pud;
+
+ pud = pud_offset(pgd, addr);
+ for (; addr< end; addr = next, pud++) {
+ next = pud_addr_end(addr, end);
+ if (pud_none(*pud))
+ continue;
+
+ vmemmap_pmd_remove(pud, addr, next);
+ }
+
+ free_pud_table(pgd);
+ __flush_tlb_all();
+}
+
+void vmemmap_free(struct page *memmap, unsigned long nr_pages)
+{
+ unsigned long addr = (unsigned long)memmap;
+ unsigned long end = (unsigned long)(memmap + nr_pages);
+ unsigned long next;
+
+ for (; addr< end; addr = next) {
+ pgd_t *pgd = pgd_offset_k(addr);
+
+ next = pgd_addr_end(addr, end);
+ if (!pgd_present(*pgd))
+ continue;
+
+ vmemmap_pud_remove(pgd, addr, next);
+ sync_global_pgds(addr, next - 1);
+ }
+}
+#endif
memory can't be offlined when CONFIG_MEMCG is selected.
For example: there is a memory device on node 1. The address range
is [1G, 1.5G). You will find 4 new directories memory8, memory9, memory10,
and memory11 under the directory /sys/devices/system/memory/.
If CONFIG_MEMCG is selected, we will allocate memory to store page cgroup
when we online pages. When we online memory8, the memory stored page cgroup
is not provided by this memory device. But when we online memory9, the memory
stored page cgroup may be provided by memory8. So we can't offline memory8
now. We should offline the memory in the reversed order.
When the memory device is hotremoved, we will auto offline memory provided
by this memory device. But we don't know which memory is onlined first, so
offlining memory may fail. In such case, iterate twice to offline the memory.
1st iterate: offline every non primary memory block.
2nd iterate: offline primary (i.e. first added) memory block.
This idea is suggested by KOSAKI Motohiro.
CC: David Rientjes<rientjes@google.com>
CC: Jiang Liu<redacted>
CC: Len Brown<redacted>
CC: Christoph Lameter<redacted>
Cc: Minchan Kim<redacted>
CC: Andrew Morton<akpm@linux-foundation.org>
CC: KOSAKI Motohiro<redacted>
CC: Yasuaki Ishimatsu<redacted>
Signed-off-by: Wen Congyang<redacted>
From: Yasuaki Ishimatsu<redacted>
We remove the memory like this:
1. lock memory hotplug
2. offline a memory block
3. unlock memory hotplug
4. repeat 1-3 to offline all memory blocks
5. lock memory hotplug
6. remove memory(TODO)
7. unlock memory hotplug
All memory blocks must be offlined before removing memory. But we don't hold
the lock in the whole operation. So we should check whether all memory blocks
are offlined before step6. Otherwise, kernel maybe panicked.
CC: David Rientjes<rientjes@google.com>
CC: Jiang Liu<redacted>
CC: Len Brown<redacted>
CC: Christoph Lameter<redacted>
Cc: Minchan Kim<redacted>
CC: Andrew Morton<akpm@linux-foundation.org>
CC: KOSAKI Motohiro<redacted>
Signed-off-by: Wen Congyang<redacted>
Signed-off-by: Yasuaki Ishimatsu<redacted>
@@ -675,6 +675,12 @@ int offline_memory_block(struct memory_block *mem)returnret;}+/* return true if the memory block is offlined, otherwise, return false */+boolis_memblock_offlined(structmemory_block*mem)+{+returnmem->state==MEM_OFFLINE;+}+/**Initializethesysfssupportformemorydevices...*/
@@ -1054,6 +1054,53 @@ repeat:gotorepeat;}+lock_memory_hotplug();++/*+*wehaveofflinedallmemoryblockslikethis:+*1.lockmemoryhotplug+*2.offlineamemoryblock+*3.unlockmemoryhotplug+*+*repeatstep1-3toofflinethememoryblock.Allmemoryblocks+*mustbeofflinedbeforeremovingmemory.Butwedon'tholdthe+*lockinthewholeoperation.Soweshouldcheckwhetherall+*memoryblocksareofflined.+*/++for(pfn=start_pfn;pfn<end_pfn;pfn+=PAGES_PER_SECTION){+section_nr=pfn_to_section_nr(pfn);+if(!present_section_nr(section_nr))+continue;++section=__nr_to_section(section_nr);+/* same memblock? */+if(mem)+if((section_nr>=mem->start_section_nr)&&+(section_nr<=mem->end_section_nr))+continue;++mem=find_memory_block_hinted(section,mem);+if(!mem)+continue;++ret=is_memblock_offlined(mem);+if(!ret){+pr_warn("removing memory fails, because memory "+"[%#010llx-%#010llx] is onlined\n",+PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)),+PFN_PHYS(section_nr_to_pfn(mem->end_section_nr+1))-1);++kobject_put(&mem->dev.kobj);+unlock_memory_hotplug();+returnret;+}+}++if(mem)+kobject_put(&mem->dev.kobj);+unlock_memory_hotplug();+return0;}#else
offlining memory blocks and checking whether memory blocks are offlined
are very similar. This patch introduces a new function to remove
redundant codes.
CC: David Rientjes<rientjes@google.com>
CC: Jiang Liu<redacted>
CC: Len Brown<redacted>
CC: Christoph Lameter<redacted>
Cc: Minchan Kim<redacted>
CC: Andrew Morton<akpm@linux-foundation.org>
CC: KOSAKI Motohiro<redacted>
CC: Yasuaki Ishimatsu<redacted>
Signed-off-by: Wen Congyang<redacted>
Can we merge this patch with [PATCH 03/12] ?
Reviewed-by: Tang Chen <redacted>
@@ -1005,20 +1005,14 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages)return__offline_pages(start_pfn,start_pfn+nr_pages,120*HZ);}-intremove_memory(u64start,u64size)+staticintwalk_memory_range(unsignedlongstart_pfn,unsignedlongend_pfn,+void*arg,int(*func)(structmemory_block*,void*)){structmemory_block*mem=NULL;structmem_section*section;-unsignedlongstart_pfn,end_pfn;unsignedlongpfn,section_nr;intret;-intreturn_on_error=0;-intretry=0;--start_pfn=PFN_DOWN(start);-end_pfn=start_pfn+PFN_DOWN(size);-repeat:for(pfn=start_pfn;pfn<end_pfn;pfn+=PAGES_PER_SECTION){section_nr=pfn_to_section_nr(pfn);if(!present_section_nr(section_nr))
@@ -1035,22 +1029,61 @@ repeat:if(!mem)continue;-ret=offline_memory_block(mem);+ret=func(mem,arg);if(ret){-if(return_on_error){-kobject_put(&mem->dev.kobj);-returnret;-}else{-retry=1;-}+kobject_put(&mem->dev.kobj);+returnret;}}if(mem)kobject_put(&mem->dev.kobj);-if(retry){-return_on_error=1;+return0;+}++staticintoffline_memory_block_cb(structmemory_block*mem,void*arg)+{+int*ret=arg;+interror=offline_memory_block(mem);++if(error!=0&&*ret==0)+*ret=error;++return0;+}++staticintis_memblock_offlined_cb(structmemory_block*mem,void*arg)+{+intret=!is_memblock_offlined(mem);++if(unlikely(ret))+pr_warn("removing memory fails, because memory "+"[%#010llx-%#010llx] is onlined\n",+PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)),+PFN_PHYS(section_nr_to_pfn(mem->end_section_nr+1))-1);++returnret;+}++intremove_memory(u64start,u64size)+{+unsignedlongstart_pfn,end_pfn;+intret=0;+intretry=1;++start_pfn=PFN_DOWN(start);+end_pfn=start_pfn+PFN_DOWN(size);++repeat:+walk_memory_range(start_pfn,end_pfn,&ret,+offline_memory_block_cb);+if(ret){+if(!retry)+returnret;++retry=0;+ret=0;gotorepeat;}
@@ -1068,37 +1101,13 @@ repeat:*memoryblocksareofflined.*/-for(pfn=start_pfn;pfn<end_pfn;pfn+=PAGES_PER_SECTION){-section_nr=pfn_to_section_nr(pfn);-if(!present_section_nr(section_nr))-continue;--section=__nr_to_section(section_nr);-/* same memblock? */-if(mem)-if((section_nr>=mem->start_section_nr)&&-(section_nr<=mem->end_section_nr))-continue;--mem=find_memory_block_hinted(section,mem);-if(!mem)-continue;--ret=is_memblock_offlined(mem);-if(!ret){-pr_warn("removing memory fails, because memory "-"[%#010llx-%#010llx] is onlined\n",-PFN_PHYS(section_nr_to_pfn(mem->start_section_nr)),-PFN_PHYS(section_nr_to_pfn(mem->end_section_nr+1))-1);--kobject_put(&mem->dev.kobj);-unlock_memory_hotplug();-returnret;-}+ret=walk_memory_range(start_pfn,end_pfn,NULL,+is_memblock_offlined_cb);+if(ret){+unlock_memory_hotplug();+returnret;}-if(mem)-kobject_put(&mem->dev.kobj);unlock_memory_hotplug();return0;
For removing memory, we need to remove page table. But it depends
on architecture. So the patch introduce arch_remove_memory() for
removing page table. Now it only calls __remove_pages().
Note: __remove_pages() for some archtecuture is not implemented
(I don't know how to implement it for s390).
CC: David Rientjes<rientjes@google.com>
CC: Jiang Liu<redacted>
CC: Len Brown<redacted>
CC: Benjamin Herrenschmidt<benh@kernel.crashing.org>
CC: Paul Mackerras<redacted>
CC: Christoph Lameter<redacted>
Cc: Minchan Kim<redacted>
CC: Andrew Morton<akpm@linux-foundation.org>
CC: KOSAKI Motohiro<redacted>
CC: Yasuaki Ishimatsu<redacted>
Signed-off-by: Wen Congyang<redacted>
---
arch/ia64/mm/init.c | 18 ++++++++++++++++++
arch/powerpc/mm/mem.c | 12 ++++++++++++
arch/s390/mm/init.c | 12 ++++++++++++
arch/sh/mm/init.c | 17 +++++++++++++++++
arch/tile/mm/init.c | 8 ++++++++
arch/x86/mm/init_32.c | 12 ++++++++++++
arch/x86/mm/init_64.c | 15 +++++++++++++++
include/linux/memory_hotplug.h | 1 +
mm/memory_hotplug.c | 2 ++
9 files changed, 97 insertions(+)
@@ -689,6 +689,24 @@ int arch_add_memory(int nid, u64 start, u64 size)returnret;}++#ifdef CONFIG_MEMORY_HOTREMOVE+intarch_remove_memory(u64start,u64size)+{+unsignedlongstart_pfn=start>>PAGE_SHIFT;+unsignedlongnr_pages=size>>PAGE_SHIFT;+structzone*zone;+intret;++zone=page_zone(pfn_to_page(start_pfn));+ret=__remove_pages(zone,start_pfn,nr_pages);+if(ret)+pr_warn("%s: Problem encountered in __remove_pages() as"+" ret=%d\n",__func__,ret);++returnret;
Just a little question, why do we have different handlers for ret on
different platforms ? Sometimes we print a msg, sometimes we just
return, and sometimes we give a WARN_ON(). But no big deal. :)
Reviewed-by: Tang Chen <redacted>
@@ -85,6 +85,7 @@ extern void __online_page_free(struct page *page);#ifdef CONFIG_MEMORY_HOTREMOVEexternboolis_pageblock_removable_nolock(structpage*page);+externintarch_remove_memory(u64start,u64size);#endif /* CONFIG_MEMORY_HOTREMOVE *//* reasonably generic interface to expand the physical pages in a zone */
From: Yasuaki Ishimatsu<redacted>
Currently __remove_section for SPARSEMEM_VMEMMAP does nothing. But even if
we use SPARSEMEM_VMEMMAP, we can unregister the memory_section.
So the patch add unregister_memory_section() into __remove_section().
CC: David Rientjes<rientjes@google.com>
CC: Jiang Liu<redacted>
CC: Len Brown<redacted>
CC: Christoph Lameter<redacted>
Cc: Minchan Kim<redacted>
CC: Andrew Morton<akpm@linux-foundation.org>
CC: KOSAKI Motohiro<redacted>
Signed-off-by: Yasuaki Ishimatsu<redacted>
Signed-off-by: Wen Congyang<redacted>
__remove_section() of CONFIG_SPARSEMEM_VMEMMAP will be integrated
into one in [PATCH 08/12], so I think we can merge this patch into
[PATCH 08/12].
Reviewed-by: Tang Chen <redacted>
From: Yasuaki Ishimatsu<redacted>
When a memory is added, we update zone's and pgdat's start_pfn and
spanned_pages in the function __add_zone(). So we should revert them
when the memory is removed.
The patch adds a new function __remove_zone() to do this.
CC: David Rientjes<rientjes@google.com>
CC: Jiang Liu<redacted>
CC: Len Brown<redacted>
CC: Christoph Lameter<redacted>
Cc: Minchan Kim<redacted>
CC: Andrew Morton<akpm@linux-foundation.org>
CC: KOSAKI Motohiro<redacted>
Signed-off-by: Yasuaki Ishimatsu<redacted>
Signed-off-by: Wen Congyang<redacted>
@@ -301,10 +301,213 @@ static int __meminit __add_section(int nid, struct zone *zone,returnregister_new_memory(nid,__pfn_to_section(phys_start_pfn));}+/* find the smallest valid pfn in the range [start_pfn, end_pfn) */+staticintfind_smallest_section_pfn(intnid,structzone*zone,+unsignedlongstart_pfn,+unsignedlongend_pfn)+{+structmem_section*ms;++for(;start_pfn<end_pfn;start_pfn+=PAGES_PER_SECTION){+ms=__pfn_to_section(start_pfn);++if(unlikely(!valid_section(ms)))+continue;++if(unlikely(pfn_to_nid(start_pfn)!=nid))+continue;++if(zone&&zone!=page_zone(pfn_to_page(start_pfn)))+continue;++returnstart_pfn;+}++return0;+}++/* find the biggest valid pfn in the range [start_pfn, end_pfn). */+staticintfind_biggest_section_pfn(intnid,structzone*zone,+unsignedlongstart_pfn,+unsignedlongend_pfn)+{+structmem_section*ms;+unsignedlongpfn;++/* pfn is the end pfn of a memory section. */+pfn=end_pfn-1;+for(;pfn>=start_pfn;pfn-=PAGES_PER_SECTION){+ms=__pfn_to_section(pfn);++if(unlikely(!valid_section(ms)))+continue;++if(unlikely(pfn_to_nid(pfn)!=nid))+continue;++if(zone&&zone!=page_zone(pfn_to_page(pfn)))+continue;++returnpfn;+}++return0;+}++staticvoidshrink_zone_span(structzone*zone,unsignedlongstart_pfn,+unsignedlongend_pfn)+{+unsignedlongzone_start_pfn=zone->zone_start_pfn;+unsignedlongzone_end_pfn=zone->zone_start_pfn+zone->spanned_pages;+unsignedlongpfn;+structmem_section*ms;+intnid=zone_to_nid(zone);++zone_span_writelock(zone);+if(zone_start_pfn==start_pfn){+/*+*Ifthesectionissmallestsectioninthezone,itneed+*shrinkzone->zone_start_pfnandzone->zone_spanned_pages.+*Inthiscase,wefindsecondsmallestvalidmem_section+*forshrinkingzone.+*/+pfn=find_smallest_section_pfn(nid,zone,end_pfn,+zone_end_pfn);+if(pfn){+zone->zone_start_pfn=pfn;+zone->spanned_pages=zone_end_pfn-pfn;+}+}elseif(zone_end_pfn==end_pfn){+/*+*Ifthesectionisbiggestsectioninthezone,itneed+*shrinkzone->spanned_pages.+*Inthiscase,wefindsecondbiggestvalidmem_sectionfor+*shrinkingzone.+*/+pfn=find_biggest_section_pfn(nid,zone,zone_start_pfn,+start_pfn);+if(pfn)+zone->spanned_pages=pfn-zone_start_pfn+1;+}++/*+*Thesectionisnotbiggestorsmallestmem_sectioninthezone,it+*onlycreatesaholeinthezone.Sointhiscase,weneednot+*changethezone.Butperhaps,thezonehasonlyholedata.Thus+*itcheckthezonehasonlyholeornot.+*/+pfn=zone_start_pfn;+for(;pfn<zone_end_pfn;pfn+=PAGES_PER_SECTION){+ms=__pfn_to_section(pfn);++if(unlikely(!valid_section(ms)))+continue;++if(page_zone(pfn_to_page(pfn))!=zone)+continue;++/* If the section is current section, it continues the loop */+if(start_pfn==pfn)+continue;++/* If we find valid section, we have nothing to do */+zone_span_writeunlock(zone);+return;+}++/* The zone has no valid section */+zone->zone_start_pfn=0;+zone->spanned_pages=0;+zone_span_writeunlock(zone);+}++staticvoidshrink_pgdat_span(structpglist_data*pgdat,+unsignedlongstart_pfn,unsignedlongend_pfn)+{+unsignedlongpgdat_start_pfn=pgdat->node_start_pfn;+unsignedlongpgdat_end_pfn=+pgdat->node_start_pfn+pgdat->node_spanned_pages;+unsignedlongpfn;+structmem_section*ms;+intnid=pgdat->node_id;++if(pgdat_start_pfn==start_pfn){+/*+*Ifthesectionissmallestsectioninthepgdat,itneed+*shrinkpgdat->node_start_pfnandpgdat->node_spanned_pages.+*Inthiscase,wefindsecondsmallestvalidmem_section+*forshrinkingzone.+*/+pfn=find_smallest_section_pfn(nid,NULL,end_pfn,+pgdat_end_pfn);+if(pfn){+pgdat->node_start_pfn=pfn;+pgdat->node_spanned_pages=pgdat_end_pfn-pfn;+}+}elseif(pgdat_end_pfn==end_pfn){+/*+*Ifthesectionisbiggestsectioninthepgdat,itneed+*shrinkpgdat->node_spanned_pages.+*Inthiscase,wefindsecondbiggestvalidmem_sectionfor+*shrinkingzone.+*/+pfn=find_biggest_section_pfn(nid,NULL,pgdat_start_pfn,+start_pfn);+if(pfn)+pgdat->node_spanned_pages=pfn-pgdat_start_pfn+1;+}++/*+*Ifthesectionisnotbiggestorsmallestmem_sectioninthepgdat,+*itonlycreatesaholeinthepgdat.Sointhiscase,weneednot+*changethepgdat.+*Butperhaps,thepgdathasonlyholedata.Thusitcheckthepgdat+*hasonlyholeornot.+*/+pfn=pgdat_start_pfn;+for(;pfn<pgdat_end_pfn;pfn+=PAGES_PER_SECTION){+ms=__pfn_to_section(pfn);++if(unlikely(!valid_section(ms)))+continue;++if(pfn_to_nid(pfn)!=nid)+continue;++/* If the section is current section, it continues the loop */+if(start_pfn==pfn)+continue;++/* If we find valid section, we have nothing to do */+return;+}++/* The pgdat has no valid section */+pgdat->node_start_pfn=0;+pgdat->node_spanned_pages=0;+}++staticvoid__remove_zone(structzone*zone,unsignedlongstart_pfn)+{+structpglist_data*pgdat=zone->zone_pgdat;+intnr_pages=PAGES_PER_SECTION;+intzone_type;+unsignedlongflags;++zone_type=zone-pgdat->node_zones;++pgdat_resize_lock(zone->zone_pgdat,&flags);+shrink_zone_span(zone,start_pfn,start_pfn+nr_pages);+shrink_pgdat_span(pgdat,start_pfn,start_pfn+nr_pages);+pgdat_resize_unlock(zone->zone_pgdat,&flags);+}+staticint__remove_section(structzone*zone,structmem_section*ms){unsignedlongflags;structpglist_data*pgdat=zone->zone_pgdat;+unsignedlongstart_pfn;+intscn_nr;intret=-EINVAL;if(!valid_section(ms))
@@ -314,6 +517,10 @@ static int __remove_section(struct zone *zone, struct mem_section *ms)if(ret)returnret;+scn_nr=__section_nr(ms);+start_pfn=section_nr_to_pfn(scn_nr);+__remove_zone(zone,start_pfn);+pgdat_resize_lock(pgdat,&flags);sparse_remove_one_section(zone,ms);pgdat_resize_unlock(pgdat,&flags);
This patch introduces a new function try_offline_node() to
remove sysfs file of node when all memory sections of this
node are removed. If some memory sections of this node are
not removed, this function does nothing.
CC: David Rientjes<rientjes@google.com>
CC: Jiang Liu<redacted>
CC: Len Brown<redacted>
CC: Christoph Lameter<redacted>
Cc: Minchan Kim<redacted>
CC: Andrew Morton<akpm@linux-foundation.org>
CC: KOSAKI Motohiro<redacted>
CC: Yasuaki Ishimatsu<redacted>
Signed-off-by: Wen Congyang<redacted>
@@ -1288,7 +1289,58 @@ static int is_memblock_offlined_cb(struct memory_block *mem, void *arg)returnret;}-int__refremove_memory(u64start,u64size)+staticintcheck_cpu_on_node(void*data)+{+structpglist_data*pgdat=data;+intcpu;++for_each_present_cpu(cpu){+if(cpu_to_node(cpu)==pgdat->node_id)+/*+*thecpuonthisnodeisn'tremoved,andwecan't+*offlinethisnode.+*/+return-EBUSY;+}++return0;+}++/* offline the node if all memory sections of this node are removed */+staticvoidtry_offline_node(intnid)+{+unsignedlongstart_pfn=NODE_DATA(nid)->node_start_pfn;+unsignedlongend_pfn=start_pfn+NODE_DATA(nid)->node_spanned_pages;+unsignedlongpfn;++for(pfn=start_pfn;pfn<end_pfn;pfn+=PAGES_PER_SECTION){+unsignedlongsection_nr=pfn_to_section_nr(pfn);++if(!present_section_nr(section_nr))+continue;++if(pfn_to_nid(pfn)!=nid)+continue;++/*+*somememorysectionsofthisnodearenotremoved,andwe+*can'tofflinenodenow.+*/+return;+}++if(stop_machine(check_cpu_on_node,NODE_DATA(nid),NULL))+return;++/*+*allmemory/cpuofthisnodeareremoved,wecanofflinethis+*nodenow.+*/+node_set_offline(nid);+unregister_one_node(nid);+}++int__refremove_memory(intnid,u64start,u64size){unsignedlongstart_pfn,end_pfn;intret=0;
@@ -1344,7 +1398,7 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages){return-EINVAL;}-intremove_memory(u64start,u64size)+intremove_memory(intnid,u64start,u64size){return-EINVAL;}
We call hotadd_new_pgdat() to allocate memory to store node_data. So we
should free it when removing a node.
CC: David Rientjes<rientjes@google.com>
CC: Jiang Liu<redacted>
CC: Len Brown<redacted>
CC: Benjamin Herrenschmidt<benh@kernel.crashing.org>
CC: Paul Mackerras<redacted>
CC: Christoph Lameter<redacted>
Cc: Minchan Kim<redacted>
CC: Andrew Morton<akpm@linux-foundation.org>
CC: KOSAKI Motohiro<redacted>
CC: Yasuaki Ishimatsu<redacted>
Signed-off-by: Wen Congyang<redacted>
@@ -1309,9 +1309,12 @@ static int check_cpu_on_node(void *data)/* offline the node if all memory sections of this node are removed */staticvoidtry_offline_node(intnid){+pg_data_t*pgdat=NODE_DATA(nid);unsignedlongstart_pfn=NODE_DATA(nid)->node_start_pfn;-unsignedlongend_pfn=start_pfn+NODE_DATA(nid)->node_spanned_pages;+unsignedlongend_pfn=start_pfn+pgdat->node_spanned_pages;unsignedlongpfn;+structpage*pgdat_page=virt_to_page(pgdat);+inti;for(pfn=start_pfn;pfn<end_pfn;pfn+=PAGES_PER_SECTION){unsignedlongsection_nr=pfn_to_section_nr(pfn);
@@ -1338,6 +1341,21 @@ static void try_offline_node(int nid)*/node_set_offline(nid);unregister_one_node(nid);++if(!PageSlab(pgdat_page)&&!PageCompound(pgdat_page))+/* node data is allocated from boot memory */+return;++/* free waittable in each zone */+for(i=0;i<MAX_NR_ZONES;i++){+structzone*zone=pgdat->node_zones+i;++if(zone->wait_table)+vfree(zone->wait_table);+}++arch_refresh_nodedata(nid,NULL);+arch_free_nodedata(pgdat);}int__refremove_memory(intnid,u64start,u64size)
offlining memory blocks and checking whether memory blocks are offlined
are very similar. This patch introduces a new function to remove
redundant codes.
CC: David Rientjes<rientjes@google.com>
CC: Jiang Liu<redacted>
CC: Len Brown<redacted>
CC: Christoph Lameter<redacted>
Cc: Minchan Kim<redacted>
CC: Andrew Morton<akpm@linux-foundation.org>
CC: KOSAKI Motohiro<redacted>
CC: Yasuaki Ishimatsu<redacted>
Signed-off-by: Wen Congyang<redacted>
Can we merge this patch with [PATCH 03/12] ?
Sorry, I think we can merge this patch into [PATCH 02/12].
Thanks. :)
@@ -1642,6 +1642,7 @@ int vmemmap_populate(struct page *start_page, unsigned long pages, int node);voidvmemmap_populate_print_last(void);voidregister_page_bootmem_memmap(unsignedlongsection_nr,structpage*map,unsignedlongsize);+voidvmemmap_free(structpage*memmap,unsignedlongnr_pages);enummf_flags{MF_COUNT_INCREASED=1<<0,
Seems that we have different ways to handle pages allocated by bootmem
or by regular allocator. Is the checking way in [PATCH 09/12] available
here ?
+ /* bootmem page has reserved flag */
+ if (PageReserved(page)) {
......
+ }
If so, I think we can just merge these two functions.
Hmm, direct mapping table isn't allocated by bootmem allocator such as memblock, can't be free by put_page_bootmem().
But I will try to merge these two functions.
All the free_xxx_table() are very similar to the functions in
[PATCH 09/12]. Could we reuse them anyway ?
yes, we can reuse them.
quoted
+
+static int split_large_page(pte_t *kpte, unsigned long address, pte_t *pbase)
+{
+ struct page *page = pmd_page(*(pmd_t *)kpte);
+ int i = 0;
+ unsigned long magic;
+ unsigned long section_nr;
+
+ __split_large_page(kpte, address, pbase);
Is this patch going to replace [PATCH 08/12] ?
I wish to replace [PATCH 08/12], but need Congyang and Yasuaki to confirm first:)
If so, __split_large_page() was added and exported in [PATCH 09/12],
then we should move it here, right ?
yes.
and what do you think about moving vmemmap_pud[pmd/pte]_remove() to arch/x86/mm/init_64.c,
to be consistent with vmemmap_populate() ?
I will rework [PATCH 08/12] and [PATCH 09/12] soon.
Thanks,
Jianguo Wu.
If not, free_map_bootmem() and __kfree_section_memmap() were changed in
[PATCH 08/12], and we need to handle this.
quoted
+ __flush_tlb_all();
+
+ magic = (unsigned long) page->lru.next;
+ if (magic == SECTION_INFO) {
+ section_nr = pfn_to_section_nr(page_to_pfn(page));
+ while (i< PTRS_PER_PMD) {
+ page++;
+ i++;
+ get_page_bootmem(section_nr, page, SECTION_INFO);
+ }
+ }
+
+ return 0;
+}
+
+static void vmemmap_pte_remove(pmd_t *pmd, unsigned long addr, unsigned long end)
+{
+ pte_t *pte;
+ unsigned long next;
+ void *page_addr;
+
+ pte = pte_offset_kernel(pmd, addr);
+ for (; addr< end; pte++, addr += PAGE_SIZE) {
+ next = (addr + PAGE_SIZE)& PAGE_MASK;
+ if (next> end)
+ next = end;
+
+ if (pte_none(*pte))
+ continue;
+ if (IS_ALIGNED(addr, PAGE_SIZE)&&
+ IS_ALIGNED(next, PAGE_SIZE)) {
+ vmemmap_free_pages(pte_page(*pte), 0);
+ spin_lock(&init_mm.page_table_lock);
+ pte_clear(&init_mm, addr, pte);
+ spin_unlock(&init_mm.page_table_lock);
+ } else {
+ /*
+ * Removed page structs are filled with 0xFD.
+ */
+ memset((void *)addr, PAGE_INUSE, next - addr);
+ page_addr = page_address(pte_page(*pte));
+
+ if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
+ spin_lock(&init_mm.page_table_lock);
+ pte_clear(&init_mm, addr, pte);
+ spin_unlock(&init_mm.page_table_lock);
+ }
+ }
+ }
+
+ free_pte_table(pmd);
+ __flush_tlb_all();
+}
+
+static void vmemmap_pmd_remove(pud_t *pud, unsigned long addr, unsigned long end)
+{
+ unsigned long next;
+ pmd_t *pmd;
+
+ pmd = pmd_offset(pud, addr);
+ for (; addr< end; addr = next, pmd++) {
+ next = pmd_addr_end(addr, end);
+ if (pmd_none(*pmd))
+ continue;
+
+ if (cpu_has_pse) {
+ unsigned long pte_base;
+
+ if (IS_ALIGNED(addr, PMD_SIZE)&&
+ IS_ALIGNED(next, PMD_SIZE)) {
+ vmemmap_free_pages(pmd_page(*pmd),
+ get_order(PMD_SIZE));
+ spin_lock(&init_mm.page_table_lock);
+ pmd_clear(pmd);
+ spin_unlock(&init_mm.page_table_lock);
+ continue;
+ }
+
+ /*
+ * We use 2M page, but we need to remove part of them,
+ * so split 2M page to 4K page.
+ */
+ pte_base = get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
+ if (!pte_base) {
+ WARN_ON(1);
+ continue;
+ }
+
+ split_large_page((pte_t *)pmd, addr, (pte_t *)pte_base);
+ __flush_tlb_all();
+
+ spin_lock(&init_mm.page_table_lock);
+ pmd_populate_kernel(&init_mm, pmd, (pte_t *)pte_base);
+ spin_unlock(&init_mm.page_table_lock);
+ }
+
+ vmemmap_pte_remove(pmd, addr, next);
+ }
+
+ free_pmd_table(pud);
+ __flush_tlb_all();
+}
+
+static void vmemmap_pud_remove(pgd_t *pgd, unsigned long addr, unsigned long end)
+{
+ unsigned long next;
+ pud_t *pud;
+
+ pud = pud_offset(pgd, addr);
+ for (; addr< end; addr = next, pud++) {
+ next = pud_addr_end(addr, end);
+ if (pud_none(*pud))
+ continue;
+
+ vmemmap_pmd_remove(pud, addr, next);
+ }
+
+ free_pud_table(pgd);
+ __flush_tlb_all();
+}
+
+void vmemmap_free(struct page *memmap, unsigned long nr_pages)
+{
+ unsigned long addr = (unsigned long)memmap;
+ unsigned long end = (unsigned long)(memmap + nr_pages);
+ unsigned long next;
+
+ for (; addr< end; addr = next) {
+ pgd_t *pgd = pgd_offset_k(addr);
+
+ next = pgd_addr_end(addr, end);
+ if (!pgd_present(*pgd))
+ continue;
+
+ vmemmap_pud_remove(pgd, addr, next);
+ sync_global_pgds(addr, next - 1);
+ }
+}
+#endif
Hi Wu,
On 12/04/2012 08:20 PM, Jianguo Wu wrote:
(snip)
quoted
Seems that we have different ways to handle pages allocated by bootmem
or by regular allocator. Is the checking way in [PATCH 09/12] available
here ?
+ /* bootmem page has reserved flag */
+ if (PageReserved(page)) {
......
+ }
If so, I think we can just merge these two functions.
Hmm, direct mapping table isn't allocated by bootmem allocator such as memblock, can't be free by put_page_bootmem().
But I will try to merge these two functions.
Oh, I didn't notice this, thanks. :)
(snip)
quoted
quoted
+
+ __split_large_page(kpte, address, pbase);
Is this patch going to replace [PATCH 08/12] ?
I wish to replace [PATCH 08/12], but need Congyang and Yasuaki to confirm first:)
quoted
If so, __split_large_page() was added and exported in [PATCH 09/12],
then we should move it here, right ?
yes.
and what do you think about moving vmemmap_pud[pmd/pte]_remove() to arch/x86/mm/init_64.c,
to be consistent with vmemmap_populate() ?
It is a good idea since pud/pmd/pte related code could be platform
dependent. And I'm also trying to move vmemmap_free() to
arch/x86/mm/init_64.c too. I want to have a common interface just
like vmemmap_populate(). :)
I will rework [PATCH 08/12] and [PATCH 09/12] soon.
I am rebasing the whole patch set now. And I think I chould finish part
of your work too. A new patch-set is coming soon, and your rework is
also welcome. :)
Thanks. :)
Hi Wu,
On 12/04/2012 08:20 PM, Jianguo Wu wrote:
(snip)
quoted
quoted
Seems that we have different ways to handle pages allocated by bootmem
or by regular allocator. Is the checking way in [PATCH 09/12] available
here ?
+ /* bootmem page has reserved flag */
+ if (PageReserved(page)) {
......
+ }
If so, I think we can just merge these two functions.
Hmm, direct mapping table isn't allocated by bootmem allocator such as memblock, can't be free by put_page_bootmem().
But I will try to merge these two functions.
Oh, I didn't notice this, thanks. :)
(snip)
quoted
quoted
quoted
+
+ __split_large_page(kpte, address, pbase);
Is this patch going to replace [PATCH 08/12] ?
I wish to replace [PATCH 08/12], but need Congyang and Yasuaki to confirm first:)
quoted
If so, __split_large_page() was added and exported in [PATCH 09/12],
then we should move it here, right ?
yes.
and what do you think about moving vmemmap_pud[pmd/pte]_remove() to arch/x86/mm/init_64.c,
to be consistent with vmemmap_populate() ?
It is a good idea since pud/pmd/pte related code could be platform
dependent. And I'm also trying to move vmemmap_free() to
arch/x86/mm/init_64.c too. I want to have a common interface just
like vmemmap_populate(). :)
Great.
quoted
I will rework [PATCH 08/12] and [PATCH 09/12] soon.
I am rebasing the whole patch set now. And I think I chould finish part
of your work too. A new patch-set is coming soon, and your rework is
also welcome. :)
Since you are rebasing now, I will wait for your new patche-set :).
Thanks.
Jianguo Wu
Hi Wu,
I met some problems when I was digging into the code. It's very
kind of you if you could help me with that. :)
If I misunderstood your code, please tell me.
Please see below. :)
On 12/03/2012 10:23 AM, Jianguo Wu wrote:
@@ -1642,6 +1642,7 @@ int vmemmap_populate(struct page *start_page, unsigned long pages, int node);voidvmemmap_populate_print_last(void);voidregister_page_bootmem_memmap(unsignedlongsection_nr,structpage*map,unsignedlongsize);+voidvmemmap_free(structpage*memmap,unsignedlongnr_pages);enummf_flags{MF_COUNT_INCREASED=1<<0,
Here, I think SECTION_INFO and MIX_SECTION_INFO pages are all allocated
by bootmem, so I put this function this way.
I'm not sure if parameter order is necessary here. It will always be 0
in your code. Is this OK to you ?
static void free_pagetable(struct page *page)
{
struct zone *zone;
bool bootmem = false;
unsigned long magic;
/* bootmem page has reserved flag */
if (PageReserved(page)) {
__ClearPageReserved(page);
bootmem = true;
}
magic = (unsigned long) page->lru.next;
if (magic == SECTION_INFO || magic == MIX_SECTION_INFO)
put_page_bootmem(page);
else
__free_page(page);
/*
* SECTION_INFO pages and MIX_SECTION_INFO pages
* are all allocated by bootmem.
*/
if (bootmem) {
zone = page_zone(page);
zone_span_writelock(zone);
zone->present_pages++;
zone_span_writeunlock(zone);
totalram_pages++;
}
}
(snip)
+
+static void vmemmap_pte_remove(pmd_t *pmd, unsigned long addr, unsigned long end)
+{
+ pte_t *pte;
+ unsigned long next;
+ void *page_addr;
+
+ pte = pte_offset_kernel(pmd, addr);
+ for (; addr< end; pte++, addr += PAGE_SIZE) {
+ next = (addr + PAGE_SIZE)& PAGE_MASK;
+ if (next> end)
+ next = end;
+
+ if (pte_none(*pte))
Here, you checked xxx_none() in your vmemmap_xxx_remove(), but you used
!xxx_present() in your x86_64 patches. Is it OK if I only check
!xxx_present() ?
And by the way, there isn't pte_addr_end() in kernel, why ?
I saw you calculated it like this:
next = (addr + PAGE_SIZE) & PAGE_MASK;
if (next > end)
next = end;
This logic is very similar to {pmd|pud|pgd}_addr_end(). Shall we add a
pte_addr_end() or something ? :)
Since there is no such code in kernel for a long time, I think there
must be some reasons.
I merged free_xxx_table() and remove_xxx_table() as common interfaces.
And again, thanks for your patient and nice explanation. :)
(snip)
Hi Wu,
I met some problems when I was digging into the code. It's very
kind of you if you could help me with that. :)
If I misunderstood your code, please tell me.
Please see below. :)
On 12/03/2012 10:23 AM, Jianguo Wu wrote:
@@ -1642,6 +1642,7 @@ int vmemmap_populate(struct page *start_page, unsigned long pages, int node);voidvmemmap_populate_print_last(void);voidregister_page_bootmem_memmap(unsignedlongsection_nr,structpage*map,unsignedlongsize);+voidvmemmap_free(structpage*memmap,unsignedlongnr_pages);enummf_flags{MF_COUNT_INCREASED=1<<0,
Here, I think SECTION_INFO and MIX_SECTION_INFO pages are all allocated
by bootmem, so I put this function this way.
I'm not sure if parameter order is necessary here. It will always be 0
in your code. Is this OK to you ?
parameter order is necessary in cpu_has_pse case:
vmemmap_pmd_remove
free_pagetable(pmd_page(*pmd), get_order(PMD_SIZE))
static void free_pagetable(struct page *page)
{
struct zone *zone;
bool bootmem = false;
unsigned long magic;
/* bootmem page has reserved flag */
if (PageReserved(page)) {
__ClearPageReserved(page);
bootmem = true;
}
magic = (unsigned long) page->lru.next;
if (magic == SECTION_INFO || magic == MIX_SECTION_INFO)
put_page_bootmem(page);
else
__free_page(page);
/*
* SECTION_INFO pages and MIX_SECTION_INFO pages
* are all allocated by bootmem.
*/
if (bootmem) {
zone = page_zone(page);
zone_span_writelock(zone);
zone->present_pages++;
zone_span_writeunlock(zone);
totalram_pages++;
}
}
(snip)
quoted
+
+static void vmemmap_pte_remove(pmd_t *pmd, unsigned long addr, unsigned long end)
+{
+ pte_t *pte;
+ unsigned long next;
+ void *page_addr;
+
+ pte = pte_offset_kernel(pmd, addr);
+ for (; addr< end; pte++, addr += PAGE_SIZE) {
+ next = (addr + PAGE_SIZE)& PAGE_MASK;
+ if (next> end)
+ next = end;
+
+ if (pte_none(*pte))
Here, you checked xxx_none() in your vmemmap_xxx_remove(), but you used
!xxx_present() in your x86_64 patches. Is it OK if I only check
!xxx_present() ?
And by the way, there isn't pte_addr_end() in kernel, why ?
I saw you calculated it like this:
next = (addr + PAGE_SIZE) & PAGE_MASK;
if (next > end)
next = end;
This logic is very similar to {pmd|pud|pgd}_addr_end(). Shall we add a
pte_addr_end() or something ? :)
Maybe just keep this for now if no other place need pte_addr_end()?
Since there is no such code in kernel for a long time, I think there
must be some reasons.
Maybe in current kernel, doesn't deal not PTE_SIZE alignment address?
I merged free_xxx_table() and remove_xxx_table() as common interfaces.
Greate!
Thanks for your work:).
And again, thanks for your patient and nice explanation. :)
(snip)
.
For hot removing memory, we sholud remove page table about the memory.
So the patch searches a page table about the removed memory, and clear
page table.
(snip)
+void __meminit
+kernel_physical_mapping_remove(unsigned long start, unsigned long end)
+{
+ unsigned long next;
+ bool pgd_changed = false;
+
+ start = (unsigned long)__va(start);
+ end = (unsigned long)__va(end);
Hi Wu,
Here, you expect start and end are physical addresses. But in
phys_xxx_remove() function, I think using virtual addresses is just
fine. Functions like pmd_addr_end() and pud_index() only calculate
an offset.
So, would you please tell me if we have to use physical addresses here ?
Thanks. :)
For hot removing memory, we sholud remove page table about the memory.
So the patch searches a page table about the removed memory, and clear
page table.
(snip)
quoted
+void __meminit
+kernel_physical_mapping_remove(unsigned long start, unsigned long end)
+{
+ unsigned long next;
+ bool pgd_changed = false;
+
+ start = (unsigned long)__va(start);
+ end = (unsigned long)__va(end);
Hi Wu,
Here, you expect start and end are physical addresses. But in
phys_xxx_remove() function, I think using virtual addresses is just
fine. Functions like pmd_addr_end() and pud_index() only calculate
an offset.
Hi Tang,
Virtual addresses will work fine, I used physical addresses in order to
keep consistent with phys_pud[pmd/pte]_init(), So I think we should keep this.
Thanks,
Jianguo Wu
So, would you please tell me if we have to use physical addresses here ?
Thanks. :)