Skip to content

Commit 4c4b7f9

Browse files
davidhildenbrandtorvalds
authored andcommitted
mm/memory_hotplug: remove memory block devices before arch_remove_memory()
Let's factor out removing of memory block devices, which is only necessary for memory added via add_memory() and friends that created memory block devices. Remove the devices before calling arch_remove_memory(). This finishes factoring out memory block device handling from arch_add_memory() and arch_remove_memory(). Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: David Hildenbrand <[email protected]> Reviewed-by: Dan Williams <[email protected]> Acked-by: Michal Hocko <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: "Rafael J. Wysocki" <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: "[email protected]" <[email protected]> Cc: Andrew Banman <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Alex Deucher <[email protected]> Cc: "David S. Miller" <[email protected]> Cc: Mark Brown <[email protected]> Cc: Chris Wilson <[email protected]> Cc: Oscar Salvador <[email protected]> Cc: Jonathan Cameron <[email protected]> Cc: Arun KS <[email protected]> Cc: Mathieu Malaterre <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Anshuman Khandual <[email protected]> Cc: Ard Biesheuvel <[email protected]> Cc: Baoquan He <[email protected]> Cc: Benjamin Herrenschmidt <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Chintan Pandya <[email protected]> Cc: Christophe Leroy <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Fenghua Yu <[email protected]> Cc: Heiko Carstens <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Joonsoo Kim <[email protected]> Cc: Jun Yao <[email protected]> Cc: "Kirill A. Shutemov" <[email protected]> Cc: Logan Gunthorpe <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Masahiro Yamada <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Nicholas Piggin <[email protected]> Cc: Oscar Salvador <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Pavel Tatashin <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Qian Cai <[email protected]> Cc: Rich Felker <[email protected]> Cc: Rob Herring <[email protected]> Cc: Robin Murphy <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Tony Luck <[email protected]> Cc: Vasily Gorbik <[email protected]> Cc: Wei Yang <[email protected]> Cc: Will Deacon <[email protected]> Cc: Yoshinori Sato <[email protected]> Cc: Yu Zhao <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 05f800a commit 4c4b7f9

File tree

5 files changed

+30
-31
lines changed

5 files changed

+30
-31
lines changed

drivers/base/memory.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -759,32 +759,31 @@ int create_memory_block_devices(unsigned long start, unsigned long size)
759759
return ret;
760760
}
761761

762-
void unregister_memory_section(struct mem_section *section)
762+
/*
763+
* Remove memory block devices for the given memory area. Start and size
764+
* have to be aligned to memory block granularity. Memory block devices
765+
* have to be offline.
766+
*/
767+
void remove_memory_block_devices(unsigned long start, unsigned long size)
763768
{
769+
const int start_block_id = pfn_to_block_id(PFN_DOWN(start));
770+
const int end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
764771
struct memory_block *mem;
772+
int block_id;
765773

766-
if (WARN_ON_ONCE(!present_section(section)))
774+
if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
775+
!IS_ALIGNED(size, memory_block_size_bytes())))
767776
return;
768777

769778
mutex_lock(&mem_sysfs_mutex);
770-
771-
/*
772-
* Some users of the memory hotplug do not want/need memblock to
773-
* track all sections. Skip over those.
774-
*/
775-
mem = find_memory_block(section);
776-
if (!mem)
777-
goto out_unlock;
778-
779-
unregister_mem_sect_under_nodes(mem, __section_nr(section));
780-
781-
mem->section_count--;
782-
if (mem->section_count == 0)
779+
for (block_id = start_block_id; block_id != end_block_id; block_id++) {
780+
mem = find_memory_block_by_id(block_id, NULL);
781+
if (WARN_ON_ONCE(!mem))
782+
continue;
783+
mem->section_count = 0;
784+
unregister_memory_block_under_nodes(mem);
783785
unregister_memory(mem);
784-
else
785-
put_device(&mem->dev);
786-
787-
out_unlock:
786+
}
788787
mutex_unlock(&mem_sysfs_mutex);
789788
}
790789

drivers/base/node.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -802,9 +802,10 @@ int register_mem_sect_under_node(struct memory_block *mem_blk, void *arg)
802802
return 0;
803803
}
804804

805-
/* unregister memory section under all nodes that it spans */
806-
int unregister_mem_sect_under_nodes(struct memory_block *mem_blk,
807-
unsigned long phys_index)
805+
/*
806+
* Unregister memory block device under all nodes that it spans.
807+
*/
808+
int unregister_memory_block_under_nodes(struct memory_block *mem_blk)
808809
{
809810
NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL);
810811
unsigned long pfn, sect_start_pfn, sect_end_pfn;
@@ -817,8 +818,8 @@ int unregister_mem_sect_under_nodes(struct memory_block *mem_blk,
817818
return -ENOMEM;
818819
nodes_clear(*unlinked_nodes);
819820

820-
sect_start_pfn = section_nr_to_pfn(phys_index);
821-
sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
821+
sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
822+
sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
822823
for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
823824
int nid;
824825

include/linux/memory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ extern void unregister_memory_notifier(struct notifier_block *nb);
112112
extern int register_memory_isolate_notifier(struct notifier_block *nb);
113113
extern void unregister_memory_isolate_notifier(struct notifier_block *nb);
114114
int create_memory_block_devices(unsigned long start, unsigned long size);
115-
extern void unregister_memory_section(struct mem_section *);
115+
void remove_memory_block_devices(unsigned long start, unsigned long size);
116116
extern int memory_dev_init(void);
117117
extern int memory_notify(unsigned long val, void *v);
118118
extern int memory_isolate_notify(unsigned long val, void *v);

include/linux/node.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ extern int register_cpu_under_node(unsigned int cpu, unsigned int nid);
139139
extern int unregister_cpu_under_node(unsigned int cpu, unsigned int nid);
140140
extern int register_mem_sect_under_node(struct memory_block *mem_blk,
141141
void *arg);
142-
extern int unregister_mem_sect_under_nodes(struct memory_block *mem_blk,
143-
unsigned long phys_index);
142+
extern int unregister_memory_block_under_nodes(struct memory_block *mem_blk);
144143

145144
extern int register_memory_node_under_compute_node(unsigned int mem_nid,
146145
unsigned int cpu_nid,
@@ -176,8 +175,7 @@ static inline int register_mem_sect_under_node(struct memory_block *mem_blk,
176175
{
177176
return 0;
178177
}
179-
static inline int unregister_mem_sect_under_nodes(struct memory_block *mem_blk,
180-
unsigned long phys_index)
178+
static inline int unregister_memory_block_under_nodes(struct memory_block *mem_blk)
181179
{
182180
return 0;
183181
}

mm/memory_hotplug.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,6 @@ static void __remove_section(struct zone *zone, struct mem_section *ms,
520520
if (WARN_ON_ONCE(!valid_section(ms)))
521521
return;
522522

523-
unregister_memory_section(ms);
524-
525523
scn_nr = __section_nr(ms);
526524
start_pfn = section_nr_to_pfn((unsigned long)scn_nr);
527525
__remove_zone(zone, start_pfn);
@@ -1834,6 +1832,9 @@ static int __ref try_remove_memory(int nid, u64 start, u64 size)
18341832
memblock_free(start, size);
18351833
memblock_remove(start, size);
18361834

1835+
/* remove memory block devices before removing memory */
1836+
remove_memory_block_devices(start, size);
1837+
18371838
arch_remove_memory(nid, start, size, NULL);
18381839
__release_memory_resource(start, size);
18391840

0 commit comments

Comments
 (0)