diff options
author | Toshi Kani <toshi.kani@hpe.com> | 2017-02-03 16:13:23 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2017-02-03 17:13:19 -0500 |
commit | a96dfddbcc04336bbed50dc2b24823e45e09e80c (patch) | |
tree | 0a6501e0d11cb978a46d8d3753dd449e57a97717 | |
parent | deb88a2a19e85842d79ba96b05031739ec327ff4 (diff) |
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 bdee237c0343 ("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>
-rw-r--r-- | drivers/base/memory.c | 12 | ||||
-rw-r--r-- | include/linux/memory_hotplug.h | 3 | ||||
-rw-r--r-- | mm/memory_hotplug.c | 20 |
3 files changed, 23 insertions, 12 deletions
diff --git a/drivers/base/memory.c b/drivers/base/memory.c index dacb6a8418aa..fa26ffd25fa6 100644 --- a/drivers/base/memory.c +++ b/drivers/base/memory.c | |||
@@ -389,33 +389,33 @@ static ssize_t show_valid_zones(struct device *dev, | |||
389 | { | 389 | { |
390 | struct memory_block *mem = to_memory_block(dev); | 390 | struct memory_block *mem = to_memory_block(dev); |
391 | unsigned long start_pfn, end_pfn; | 391 | unsigned long start_pfn, end_pfn; |
392 | unsigned long valid_start, valid_end, valid_pages; | ||
392 | unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; | 393 | unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; |
393 | struct page *first_page; | ||
394 | struct zone *zone; | 394 | struct zone *zone; |
395 | int zone_shift = 0; | 395 | int zone_shift = 0; |
396 | 396 | ||
397 | start_pfn = section_nr_to_pfn(mem->start_section_nr); | 397 | start_pfn = section_nr_to_pfn(mem->start_section_nr); |
398 | end_pfn = start_pfn + nr_pages; | 398 | end_pfn = start_pfn + nr_pages; |
399 | first_page = pfn_to_page(start_pfn); | ||
400 | 399 | ||
401 | /* The block contains more than one zone can not be offlined. */ | 400 | /* 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)) |
403 | return sprintf(buf, "none\n"); | 402 | return sprintf(buf, "none\n"); |
404 | 403 | ||
405 | zone = page_zone(first_page); | 404 | zone = page_zone(pfn_to_page(valid_start)); |
405 | valid_pages = valid_end - valid_start; | ||
406 | 406 | ||
407 | /* MMOP_ONLINE_KEEP */ | 407 | /* MMOP_ONLINE_KEEP */ |
408 | sprintf(buf, "%s", zone->name); | 408 | sprintf(buf, "%s", zone->name); |
409 | 409 | ||
410 | /* MMOP_ONLINE_KERNEL */ | 410 | /* 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); |
412 | if (zone_shift) { | 412 | if (zone_shift) { |
413 | strcat(buf, " "); | 413 | strcat(buf, " "); |
414 | strcat(buf, (zone + zone_shift)->name); | 414 | strcat(buf, (zone + zone_shift)->name); |
415 | } | 415 | } |
416 | 416 | ||
417 | /* MMOP_ONLINE_MOVABLE */ | 417 | /* 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); |
419 | if (zone_shift) { | 419 | if (zone_shift) { |
420 | strcat(buf, " "); | 420 | strcat(buf, " "); |
421 | strcat(buf, (zone + zone_shift)->name); | 421 | strcat(buf, (zone + zone_shift)->name); |
diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index c1784c0b4f35..134a2f69c21a 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h | |||
@@ -85,7 +85,8 @@ extern int zone_grow_waitqueues(struct zone *zone, unsigned long nr_pages); | |||
85 | extern int add_one_highpage(struct page *page, int pfn, int bad_ppro); | 85 | extern int add_one_highpage(struct page *page, int pfn, int bad_ppro); |
86 | /* VM interface that may be used by firmware interface */ | 86 | /* VM interface that may be used by firmware interface */ |
87 | extern int online_pages(unsigned long, unsigned long, int); | 87 | 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); | ||
89 | extern void __offline_isolated_pages(unsigned long, unsigned long); | 90 | extern void __offline_isolated_pages(unsigned long, unsigned long); |
90 | 91 | ||
91 | typedef void (*online_page_callback_t)(struct page *page); | 92 | typedef void (*online_page_callback_t)(struct page *page); |
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 1218f73890b6..b8c11e063ff0 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c | |||
@@ -1484,10 +1484,13 @@ bool is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages) | |||
1484 | 1484 | ||
1485 | /* | 1485 | /* |
1486 | * Confirm all pages in a range [start, end) belong to the same zone. | 1486 | * Confirm all pages in a range [start, end) belong to the same zone. |
1487 | * When true, return its valid [start, end). | ||
1487 | */ | 1488 | */ |
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) | ||
1489 | { | 1491 | { |
1490 | unsigned long pfn, sec_end_pfn; | 1492 | unsigned long pfn, sec_end_pfn; |
1493 | unsigned long start, end; | ||
1491 | struct zone *zone = NULL; | 1494 | struct zone *zone = NULL; |
1492 | struct page *page; | 1495 | struct page *page; |
1493 | int i; | 1496 | int i; |
@@ -1509,14 +1512,20 @@ int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn) | |||
1509 | page = pfn_to_page(pfn + i); | 1512 | page = pfn_to_page(pfn + i); |
1510 | if (zone && page_zone(page) != zone) | 1513 | if (zone && page_zone(page) != zone) |
1511 | return 0; | 1514 | return 0; |
1515 | if (!zone) | ||
1516 | start = pfn + i; | ||
1512 | zone = page_zone(page); | 1517 | zone = page_zone(page); |
1518 | end = pfn + MAX_ORDER_NR_PAGES; | ||
1513 | } | 1519 | } |
1514 | } | 1520 | } |
1515 | 1521 | ||
1516 | if (zone) | 1522 | if (zone) { |
1523 | *valid_start = start; | ||
1524 | *valid_end = end; | ||
1517 | return 1; | 1525 | return 1; |
1518 | else | 1526 | } else { |
1519 | return 0; | 1527 | return 0; |
1528 | } | ||
1520 | } | 1529 | } |
1521 | 1530 | ||
1522 | /* | 1531 | /* |
@@ -1843,6 +1852,7 @@ static int __ref __offline_pages(unsigned long start_pfn, | |||
1843 | long offlined_pages; | 1852 | long offlined_pages; |
1844 | int ret, drain, retry_max, node; | 1853 | int ret, drain, retry_max, node; |
1845 | unsigned long flags; | 1854 | unsigned long flags; |
1855 | unsigned long valid_start, valid_end; | ||
1846 | struct zone *zone; | 1856 | struct zone *zone; |
1847 | struct memory_notify arg; | 1857 | struct memory_notify arg; |
1848 | 1858 | ||
@@ -1853,10 +1863,10 @@ static int __ref __offline_pages(unsigned long start_pfn, | |||
1853 | return -EINVAL; | 1863 | return -EINVAL; |
1854 | /* This makes hotplug much easier...and readable. | 1864 | /* This makes hotplug much easier...and readable. |
1855 | we assume this for now. .*/ | 1865 | 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)) |
1857 | return -EINVAL; | 1867 | return -EINVAL; |
1858 | 1868 | ||
1859 | zone = page_zone(pfn_to_page(start_pfn)); | 1869 | zone = page_zone(pfn_to_page(valid_start)); |
1860 | node = zone_to_nid(zone); | 1870 | node = zone_to_nid(zone); |
1861 | nr_pages = end_pfn - start_pfn; | 1871 | nr_pages = end_pfn - start_pfn; |
1862 | 1872 | ||