Skip to content

Commit a96dfdd

Browse files
toshikanitorvalds
authored andcommitted
base/memory, hotplug: fix a kernel oops in show_valid_zones()
Reading a sysfs "memoryN/valid_zones" file leads to the following oops when the first page of a range is not backed by struct page. show_valid_zones() assumes that 'start_pfn' is always valid for page_zone(). BUG: unable to handle kernel paging request at ffffea017a000000 IP: show_valid_zones+0x6f/0x160 This issue may happen on x86-64 systems with 64GiB or more memory since their memory block size is bumped up to 2GiB. [1] An example of such systems is desribed below. 0x3240000000 is only aligned by 1GiB and this memory block starts from 0x3200000000, which is not backed by struct page. BIOS-e820: [mem 0x0000003240000000-0x000000603fffffff] usable Since test_pages_in_a_zone() already checks holes, fix this issue by extending this function to return 'valid_start' and 'valid_end' for a given range. show_valid_zones() then proceeds with the valid range. [1] 'Commit bdee237 ("x86: mm: Use 2GB memory block size on large-memory x86-64 systems")' Link: http://lkml.kernel.org/r/20170127222149.30893-3-toshi.kani@hpe.com Signed-off-by: Toshi Kani <toshi.kani@hpe.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Zhang Zhen <zhenzhang.zhang@huawei.com> Cc: Reza Arbab <arbab@linux.vnet.ibm.com> Cc: David Rientjes <rientjes@google.com> Cc: Dan Williams <dan.j.williams@intel.com> Cc: <stable@vger.kernel.org> [4.4+] Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent deb88a2 commit a96dfdd

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎drivers/base/memory.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,33 +389,33 @@ static ssize_t show_valid_zones(struct device *dev,
389389
{
390390
struct memory_block *mem = to_memory_block(dev);
391391
unsigned long start_pfn, end_pfn;
392+
unsigned long valid_start, valid_end, valid_pages;
392393
unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
393-
struct page *first_page;
394394
struct zone *zone;
395395
int zone_shift = 0;
396396

397397
start_pfn = section_nr_to_pfn(mem->start_section_nr);
398398
end_pfn = start_pfn + nr_pages;
399-
first_page = pfn_to_page(start_pfn);
400399

401400
/* The block contains more than one zone can not be offlined. */
402-
if (!test_pages_in_a_zone(start_pfn, end_pfn))
401+
if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
403402
return sprintf(buf, "none\n");
404403

405-
zone = page_zone(first_page);
404+
zone = page_zone(pfn_to_page(valid_start));
405+
valid_pages = valid_end - valid_start;
406406

407407
/* MMOP_ONLINE_KEEP */
408408
sprintf(buf, "%s", zone->name);
409409

410410
/* MMOP_ONLINE_KERNEL */
411-
zone_can_shift(start_pfn, nr_pages, ZONE_NORMAL, &zone_shift);
411+
zone_can_shift(valid_start, valid_pages, ZONE_NORMAL, &zone_shift);
412412
if (zone_shift) {
413413
strcat(buf, " ");
414414
strcat(buf, (zone + zone_shift)->name);
415415
}
416416

417417
/* MMOP_ONLINE_MOVABLE */
418-
zone_can_shift(start_pfn, nr_pages, ZONE_MOVABLE, &zone_shift);
418+
zone_can_shift(valid_start, valid_pages, ZONE_MOVABLE, &zone_shift);
419419
if (zone_shift) {
420420
strcat(buf, " ");
421421
strcat(buf, (zone + zone_shift)->name);

‎include/linux/memory_hotplug.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ extern int zone_grow_waitqueues(struct zone *zone, unsigned long nr_pages);
8585
extern int add_one_highpage(struct page *page, int pfn, int bad_ppro);
8686
/* VM interface that may be used by firmware interface */
8787
extern int online_pages(unsigned long, unsigned long, int);
88-
extern int test_pages_in_a_zone(unsigned long, unsigned long);
88+
extern int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
89+
unsigned long *valid_start, unsigned long *valid_end);
8990
extern void __offline_isolated_pages(unsigned long, unsigned long);
9091

9192
typedef void (*online_page_callback_t)(struct page *page);

‎mm/memory_hotplug.c‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,10 +1484,13 @@ bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages)
14841484

14851485
/*
14861486
* Confirm all pages in a range [start, end) belong to the same zone.
1487+
* When true, return its valid [start, end).
14871488
*/
1488-
int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
1489+
int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,
1490+
unsigned long *valid_start, unsigned long *valid_end)
14891491
{
14901492
unsigned long pfn, sec_end_pfn;
1493+
unsigned long start, end;
14911494
struct zone *zone = NULL;
14921495
struct page *page;
14931496
int i;
@@ -1509,14 +1512,20 @@ int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn)
15091512
page = pfn_to_page(pfn + i);
15101513
if (zone && page_zone(page) != zone)
15111514
return 0;
1515+
if (!zone)
1516+
start = pfn + i;
15121517
zone = page_zone(page);
1518+
end = pfn + MAX_ORDER_NR_PAGES;
15131519
}
15141520
}
15151521

1516-
if (zone)
1522+
if (zone) {
1523+
*valid_start = start;
1524+
*valid_end = end;
15171525
return 1;
1518-
else
1526+
} else {
15191527
return 0;
1528+
}
15201529
}
15211530

15221531
/*
@@ -1843,6 +1852,7 @@ static int __ref __offline_pages(unsigned long start_pfn,
18431852
long offlined_pages;
18441853
int ret, drain, retry_max, node;
18451854
unsigned long flags;
1855+
unsigned long valid_start, valid_end;
18461856
struct zone *zone;
18471857
struct memory_notify arg;
18481858

@@ -1853,10 +1863,10 @@ static int __ref __offline_pages(unsigned long start_pfn,
18531863
return -EINVAL;
18541864
/* This makes hotplug much easier...and readable.
18551865
we assume this for now. .*/
1856-
if (!test_pages_in_a_zone(start_pfn, end_pfn))
1866+
if (!test_pages_in_a_zone(start_pfn, end_pfn, &valid_start, &valid_end))
18571867
return -EINVAL;
18581868

1859-
zone = page_zone(pfn_to_page(start_pfn));
1869+
zone = page_zone(pfn_to_page(valid_start));
18601870
node = zone_to_nid(zone);
18611871
nr_pages = end_pfn - start_pfn;
18621872

0 commit comments

Comments
 (0)