Skip to content

Commit c5e79ef

Browse files
David Hildenbrandtorvalds
authored andcommitted
mm/memory_hotplug.c: don't allow to online/offline memory blocks with holes
Our onlining/offlining code is unnecessarily complicated. Only memory blocks added during boot can have holes (a range that is not IORESOURCE_SYSTEM_RAM). Hotplugged memory never has holes (e.g., see add_memory_resource()). All memory blocks that belong to boot memory are already online. Note that boot memory can have holes and the memmap of the holes is marked PG_reserved. However, also memory allocated early during boot is PG_reserved - basically every page of boot memory that is not given to the buddy is PG_reserved. Therefore, when we stop allowing to offline memory blocks with holes, we implicitly no longer have to deal with onlining memory blocks with holes. E.g., online_pages() will do a walk_system_ram_range(..., online_pages_range), whereby online_pages_range() will effectively only free the memory holes not falling into a hole to the buddy. The other pages (holes) are kept PG_reserved (via move_pfn_range_to_zone()->memmap_init_zone()). This allows to simplify the code. For example, we no longer have to worry about marking pages that fall into memory holes PG_reserved when onlining memory. We can stop setting pages PG_reserved completely in memmap_init_zone(). Offlining memory blocks added during boot is usually not guaranteed to work either way (unmovable data might have easily ended up on that memory during boot). So stopping to do that should not really hurt. Also, people are not even aware of a setup where onlining/offlining of memory blocks with holes used to work reliably (see [1] and [2] especially regarding the hotplug path) - I doubt it worked reliably. For the use case of offlining memory to unplug DIMMs, we should see no change. (holes on DIMMs would be weird). Please note that hardware errors (PG_hwpoison) are not memory holes and are not affected by this change when offlining. [1] https://lkml.org/lkml/2019/10/22/135 [2] https://lkml.org/lkml/2019/8/14/1365 Link: http://lkml.kernel.org/r/20191119115237.6662-1-david@redhat.com Reviewed-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: David Hildenbrand <david@redhat.com> Acked-by: Michal Hocko <mhocko@suse.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: Pavel Tatashin <pasha.tatashin@soleen.com> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: Naoya Horiguchi <nao.horiguchi@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 848e19a commit c5e79ef

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

‎mm/memory_hotplug.c‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,10 +1485,19 @@ static void node_states_clear_node(int node, struct memory_notify *arg)
14851485
node_clear_state(node, N_MEMORY);
14861486
}
14871487

1488+
static int count_system_ram_pages_cb(unsigned long start_pfn,
1489+
unsigned long nr_pages, void *data)
1490+
{
1491+
unsigned long *nr_system_ram_pages = data;
1492+
1493+
*nr_system_ram_pages += nr_pages;
1494+
return 0;
1495+
}
1496+
14881497
static int __ref __offline_pages(unsigned long start_pfn,
14891498
unsigned long end_pfn)
14901499
{
1491-
unsigned long pfn, nr_pages;
1500+
unsigned long pfn, nr_pages = 0;
14921501
unsigned long offlined_pages = 0;
14931502
int ret, node, nr_isolate_pageblock;
14941503
unsigned long flags;
@@ -1499,6 +1508,22 @@ static int __ref __offline_pages(unsigned long start_pfn,
14991508

15001509
mem_hotplug_begin();
15011510

1511+
/*
1512+
* Don't allow to offline memory blocks that contain holes.
1513+
* Consequently, memory blocks with holes can never get onlined
1514+
* via the hotplug path - online_pages() - as hotplugged memory has
1515+
* no holes. This way, we e.g., don't have to worry about marking
1516+
* memory holes PG_reserved, don't need pfn_valid() checks, and can
1517+
* avoid using walk_system_ram_range() later.
1518+
*/
1519+
walk_system_ram_range(start_pfn, end_pfn - start_pfn, &nr_pages,
1520+
count_system_ram_pages_cb);
1521+
if (nr_pages != end_pfn - start_pfn) {
1522+
ret = -EINVAL;
1523+
reason = "memory holes";
1524+
goto failed_removal;
1525+
}
1526+
15021527
/* This makes hotplug much easier...and readable.
15031528
we assume this for now. .*/
15041529
if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start,
@@ -1510,7 +1535,6 @@ static int __ref __offline_pages(unsigned long start_pfn,
15101535

15111536
zone = page_zone(pfn_to_page(valid_start));
15121537
node = zone_to_nid(zone);
1513-
nr_pages = end_pfn - start_pfn;
15141538

15151539
/* set above range as isolated */
15161540
ret = start_isolate_page_range(start_pfn, end_pfn,

0 commit comments

Comments
 (0)