Skip to content

Commit 06be6ff

Browse files
osalvadorvilardagatorvalds
authored andcommitted
mm,hwpoison: rework soft offline for free pages
When trying to soft-offline a free page, we need to first take it off the buddy allocator. Once we know is out of reach, we can safely flag it as poisoned. take_page_off_buddy will be used to take a page meant to be poisoned off the buddy allocator. take_page_off_buddy calls break_down_buddy_pages, which splits a higher-order page in case our page belongs to one. Once the page is under our control, we call page_handle_poison to set it as poisoned and grab a refcount on it. Signed-off-by: Oscar Salvador <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Acked-by: Naoya Horiguchi <[email protected]> Cc: "Aneesh Kumar K.V" <[email protected]> Cc: Aneesh Kumar K.V <[email protected]> Cc: Aristeu Rozanski <[email protected]> Cc: Dave Hansen <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Dmitry Yakunin <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Mike Kravetz <[email protected]> Cc: Oscar Salvador <[email protected]> Cc: Qian Cai <[email protected]> Cc: Tony Luck <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 694bf0b commit 06be6ff

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

include/linux/page-flags.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ PAGEFLAG(HWPoison, hwpoison, PF_ANY)
432432
TESTSCFLAG(HWPoison, hwpoison, PF_ANY)
433433
#define __PG_HWPOISON (1UL << PG_hwpoison)
434434
extern bool set_hwpoison_free_buddy_page(struct page *page);
435+
extern bool take_page_off_buddy(struct page *page);
435436
#else
436437
PAGEFLAG_FALSE(HWPoison)
437438
static inline bool set_hwpoison_free_buddy_page(struct page *page)

mm/memory-failure.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ int sysctl_memory_failure_recovery __read_mostly = 1;
6565

6666
atomic_long_t num_poisoned_pages __read_mostly = ATOMIC_LONG_INIT(0);
6767

68+
static void page_handle_poison(struct page *page)
69+
{
70+
SetPageHWPoison(page);
71+
page_ref_inc(page);
72+
num_poisoned_pages_inc();
73+
}
74+
6875
#if defined(CONFIG_HWPOISON_INJECT) || defined(CONFIG_HWPOISON_INJECT_MODULE)
6976

7077
u32 hwpoison_filter_enable = 0;
@@ -1884,14 +1891,13 @@ static int soft_offline_in_use_page(struct page *page, int flags)
18841891

18851892
static int soft_offline_free_page(struct page *page)
18861893
{
1887-
int rc = dissolve_free_huge_page(page);
1894+
int rc = -EBUSY;
18881895

1889-
if (!rc) {
1890-
if (set_hwpoison_free_buddy_page(page))
1891-
num_poisoned_pages_inc();
1892-
else
1893-
rc = -EBUSY;
1896+
if (!dissolve_free_huge_page(page) && take_page_off_buddy(page)) {
1897+
page_handle_poison(page);
1898+
rc = 0;
18941899
}
1900+
18951901
return rc;
18961902
}
18971903

mm/page_alloc.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8777,6 +8777,74 @@ bool is_free_buddy_page(struct page *page)
87778777
}
87788778

87798779
#ifdef CONFIG_MEMORY_FAILURE
8780+
/*
8781+
* Break down a higher-order page in sub-pages, and keep our target out of
8782+
* buddy allocator.
8783+
*/
8784+
static void break_down_buddy_pages(struct zone *zone, struct page *page,
8785+
struct page *target, int low, int high,
8786+
int migratetype)
8787+
{
8788+
unsigned long size = 1 << high;
8789+
struct page *current_buddy, *next_page;
8790+
8791+
while (high > low) {
8792+
high--;
8793+
size >>= 1;
8794+
8795+
if (target >= &page[size]) {
8796+
next_page = page + size;
8797+
current_buddy = page;
8798+
} else {
8799+
next_page = page;
8800+
current_buddy = page + size;
8801+
}
8802+
8803+
if (set_page_guard(zone, current_buddy, high, migratetype))
8804+
continue;
8805+
8806+
if (current_buddy != target) {
8807+
add_to_free_list(current_buddy, zone, high, migratetype);
8808+
set_page_order(current_buddy, high);
8809+
page = next_page;
8810+
}
8811+
}
8812+
}
8813+
8814+
/*
8815+
* Take a page that will be marked as poisoned off the buddy allocator.
8816+
*/
8817+
bool take_page_off_buddy(struct page *page)
8818+
{
8819+
struct zone *zone = page_zone(page);
8820+
unsigned long pfn = page_to_pfn(page);
8821+
unsigned long flags;
8822+
unsigned int order;
8823+
bool ret = false;
8824+
8825+
spin_lock_irqsave(&zone->lock, flags);
8826+
for (order = 0; order < MAX_ORDER; order++) {
8827+
struct page *page_head = page - (pfn & ((1 << order) - 1));
8828+
int buddy_order = page_order(page_head);
8829+
8830+
if (PageBuddy(page_head) && buddy_order >= order) {
8831+
unsigned long pfn_head = page_to_pfn(page_head);
8832+
int migratetype = get_pfnblock_migratetype(page_head,
8833+
pfn_head);
8834+
8835+
del_page_from_free_list(page_head, zone, buddy_order);
8836+
break_down_buddy_pages(zone, page_head, page, 0,
8837+
buddy_order, migratetype);
8838+
ret = true;
8839+
break;
8840+
}
8841+
if (page_count(page_head) > 0)
8842+
break;
8843+
}
8844+
spin_unlock_irqrestore(&zone->lock, flags);
8845+
return ret;
8846+
}
8847+
87808848
/*
87818849
* Set PG_hwpoison flag if a given page is confirmed to be a free page. This
87828850
* test is performed under the zone lock to prevent a race against page

0 commit comments

Comments
 (0)