mm/hwpoison: fix race between hugetlb free/demotion and memory_failure_hugetlb()
Subsystems (4)
PATCH v6 1/1 mm/hwpoison: fix race between hugetlb free/demotion and memory_failure_hugetlb()
From: Naoya Horiguchi <[email protected]>
There is a race condition between memory_failure_hugetlb() and hugetlb free/demotion, which causes setting PageHWPoison flag on the wrong page. The one simple result is that wrong processes can be killed, but another (more serious) one is that the actual error is left unhandled, so no one prevents later access to it, and that might lead to more serious results like consuming corrupted data.
Think about the below race window:
CPU 1 CPU 2
memory_failure_hugetlb
struct page *head = compound_head(p);
hugetlb page might be freed to buddy, or even changed to another compound page.
get_hwpoison_page -- page is not what we want now...
The current code first does prechecks roughly and then reconfirms after taking refcount, but it's found that it makes code overly complicated, so move the prechecks in a single hugetlb_lock range.
A newly introduced function, try_memory_failure_hugetlb(), always takes hugetlb_lock (even for non-hugetlb pages). That can be improved, but memory_failure() is rare in principle, so should not be a big problem.
---
ChangeLog v5 -> v6:
- Moved racy precheck operations into hugetlb_lock (based on Mike's comment).
- rebased onto v5.18-rc1.
- dropped CC to stable.
ChangeLog v4 -> v5:
- call TestSetPageHWPoison() when page_handle_poison() fails.
- call TestSetPageHWPoison() for unhandlable cases (MF_MSG_UNKNOWN and MF_MSG_DIFFERENT_PAGE_SIZE).
- Set PageHWPoison on the head page only when the error page is surely a hugepage, otherwise set the flag on the raw page.
- rebased onto v5.17-rc8-mmotm-2022-03-16-17-42
ChangeLog v3 -> v4:
- squash with "mm/memory-failure.c: fix race with changing page compound again".
- update patch subject and description based on it.
ChangeLog v2 -> v3:
- rename the patch because page lock is not the primary factor to solve the reported issue.
- updated description in the same manner.
- call page_handle_poison() instead of __page_handle_poison() for free hugepage case.
- reorder put_page and unlock_page (thanks to Miaohe Lin)
ChangeLog v1 -> v2:
- pass subpage to get_hwpoison_huge_page() instead of head page.
- call compound_head() in hugetlb_lock to avoid race with hugetlb demotion/free.
--- include/linux/hugetlb.h | 6 ++ include/linux/mm.h | 8 +++ mm/hugetlb.c | 10 +++ mm/memory-failure.c | 153 +++++++++++++++++++++++++++++----------- 4 files changed, 135 insertions(+), 42 deletions(-)
include/linux/hugetlb.h +6 -0
@@ -169,6 +169,7 @@ long hugetlb_unreserve_pages(struct inode *inode, long start, long end, long freed);bool isolate_huge_page (struct page *page,struct list_head *list);int get_hwpoison_huge_page (struct page *page,bool *hugetlb); +int get_huge_page_for_hwpoison (unsigned long pfn,int flags);void putback_active_hugepage (struct page *page);void move_hugetlb_state (struct page *oldpage,struct page *newpage,int reason);void free_huge_page (struct page *page);
@@ -378,6 +379,11 @@ static inline int get_hwpoison_huge_page(struct page *page, bool *hugetlb)return 0 ; } +static inline int get_huge_page_for_hwpoison (unsigned long pfn,int flags) +{ +return 0 ; +} +static inline void putback_active_hugepage (struct page *page) { }
include/linux/mm.h +8 -0
@@ -3197,6 +3197,14 @@ extern int sysctl_memory_failure_recovery;extern void shake_page (struct page *p);extern atomic_long_t num_poisoned_pages__read_mostly ;extern int soft_offline_page (unsigned long pfn,int flags); +#ifdef CONFIG_MEMORY_FAILURE +extern int __get_huge_page_for_hwpoison (unsigned long pfn,int flags); +#else +static inline int __get_huge_page_for_hwpoison (unsigned long pfn,int flags) +{ +return 0 ; +} +#endif #ifndef arch_memory_failurestatic inline int arch_memory_failure (unsigned long pfn,int flags)
mm/hugetlb.c +10 -0
@@ -6782,6 +6782,16 @@ int get_hwpoison_huge_page(struct page *page, bool *hugetlb)return ret; } +int get_huge_page_for_hwpoison (unsigned long pfn,int flags) +{ +int ret; + +spin_lock_irq (&hugetlb_lock); + ret =__get_huge_page_for_hwpoison (pfn, flags); +spin_unlock_irq (&hugetlb_lock); +return ret; +} +void putback_active_hugepage (struct page *page) {spin_lock_irq (&hugetlb_lock);
mm/memory-failure.c +111 -42
197 lines not on this page · raw message
Signature · 1 line
2.25.1
Andrew Morton
maintainerQuoted 6 lines · On Mon, 4 Apr 2022 18:21:31 +0900 Naoya Horiguchi <[email protected]>…
There is a race condition between
memory_failure_hugetlb()and hugetlb free/demotion, which causes setting PageHWPoison flag on the wrong page. The one simple result is that wrong processes can be killed, but another (more serious) one is that the actual error is left unhandled, so no one prevents later access to it, and that might lead to more serious results like consuming corrupted data.
Should this fix be backported into stable kernels?
Quoted 10 lines · Andrew Morton
Quoted 6 lines · On Mon, 4 Apr 2022 18:21:31 +0900 Naoya Horiguchi <[email protected]>…
There is a race condition between
memory_failure_hugetlb()and hugetlb free/demotion, which causes setting PageHWPoison flag on the wrong page. The one simple result is that wrong processes can be killed, but another (more serious) one is that the actual error is left unhandled, so no one prevents later access to it, and that might lead to more serious results like consuming corrupted data.Should this fix be backported into stable kernels?
This is a bug fix, so eligible to send to stable. But I thought that this patch is larger than 100 lines (and hard to separeter to finer patches), which seems to violate the rule stated in Documentation/process/stable-kernel-rules.rst.
But actually this rule might not be strictly applied (some patches in v5.16.y do have more than 100 lines diff...). So if we can ignore this rule exceptionally, that's OK and I'll add CC to stable again.
The target commit of Fixed: tag is 761ad8d7c7b5 ("mm: hwpoison: introduce memory_failure_hugetlb()") which was introduced in 4.13, so most of active stable trees are affected.
Thanks,
Naoya Horiguchi
Andrew Morton
maintainerQuoted 12 lines · On Tue, 5 Apr 2022 01:45:45 +0000 HORIGUCHI NAOYA(堀口 直也)…
Quoted 10 lines · Andrew Morton
Quoted 6 lines · On Mon, 4 Apr 2022 18:21:31 +0900 Naoya Horiguchi <[email protected]>…
There is a race condition between
memory_failure_hugetlb()and hugetlb free/demotion, which causes setting PageHWPoison flag on the wrong page. The one simple result is that wrong processes can be killed, but another (more serious) one is that the actual error is left unhandled, so no one prevents later access to it, and that might lead to more serious results like consuming corrupted data.Should this fix be backported into stable kernels?
Quoted 8 lines
This is a bug fix, so eligible to send to stable. But I thought that this patch is larger than 100 lines (and hard to separeter to finer patches), which seems to violate the rule stated in
Documentation/process/stable-kernel-rules.rst.But actually this rule might not be strictly applied (some patches in v5.16.y do have more than 100 lines diff...). So if we can ignore this rule exceptionally, that's OK and I'll add CC to stable again.
I never actually knew about that rule ;)
Thanks, I added the cc:stable.
Quoted 3 lines
The target commit of Fixed: tag is
761ad8d7c7b5("mm: hwpoison: introducememory_failure_hugetlb()") which was introduced in 4.13, so most of active stable trees are affected.
Oh. That's good to know. The original patch didn't have a Fixes: line. I added that as well.