Skip to content

Commit 171936d

Browse files
aegltorvalds
authored andcommitted
mm/memory-failure: use a mutex to avoid memory_failure() races
Patch series "mm,hwpoison: fix sending SIGBUS for Action Required MCE", v5. I wrote this patchset to materialize what I think is the current allowable solution mentioned by the previous discussion [1]. I simply borrowed Tony's mutex patch and Aili's return code patch, then I queued another one to find error virtual address in the best effort manner. I know that this is not a perfect solution, but should work for some typical case. [1]: https://lore.kernel.org/linux-mm/20210331192540.2141052f@alex-virtual-machine/ This patch (of 2): There can be races when multiple CPUs consume poison from the same page. The first into memory_failure() atomically sets the HWPoison page flag and begins hunting for tasks that map this page. Eventually it invalidates those mappings and may send a SIGBUS to the affected tasks. But while all that work is going on, other CPUs see a "success" return code from memory_failure() and so they believe the error has been handled and continue executing. Fix by wrapping most of the internal parts of memory_failure() in a mutex. [[email protected]: make mf_mutex local to memory_failure()] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Tony Luck <[email protected]> Signed-off-by: Naoya Horiguchi <[email protected]> Reviewed-by: Borislav Petkov <[email protected]> Reviewed-by: Oscar Salvador <[email protected]> Cc: Aili Yao <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Jue Wang <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent fe19bd3 commit 171936d

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

mm/memory-failure.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,9 +1429,10 @@ int memory_failure(unsigned long pfn, int flags)
14291429
struct page *hpage;
14301430
struct page *orig_head;
14311431
struct dev_pagemap *pgmap;
1432-
int res;
1432+
int res = 0;
14331433
unsigned long page_flags;
14341434
bool retry = true;
1435+
static DEFINE_MUTEX(mf_mutex);
14351436

14361437
if (!sysctl_memory_failure_recovery)
14371438
panic("Memory failure on page %lx", pfn);
@@ -1449,13 +1450,18 @@ int memory_failure(unsigned long pfn, int flags)
14491450
return -ENXIO;
14501451
}
14511452

1453+
mutex_lock(&mf_mutex);
1454+
14521455
try_again:
1453-
if (PageHuge(p))
1454-
return memory_failure_hugetlb(pfn, flags);
1456+
if (PageHuge(p)) {
1457+
res = memory_failure_hugetlb(pfn, flags);
1458+
goto unlock_mutex;
1459+
}
1460+
14551461
if (TestSetPageHWPoison(p)) {
14561462
pr_err("Memory failure: %#lx: already hardware poisoned\n",
14571463
pfn);
1458-
return 0;
1464+
goto unlock_mutex;
14591465
}
14601466

14611467
orig_head = hpage = compound_head(p);
@@ -1488,17 +1494,19 @@ int memory_failure(unsigned long pfn, int flags)
14881494
res = MF_FAILED;
14891495
}
14901496
action_result(pfn, MF_MSG_BUDDY, res);
1491-
return res == MF_RECOVERED ? 0 : -EBUSY;
1497+
res = res == MF_RECOVERED ? 0 : -EBUSY;
14921498
} else {
14931499
action_result(pfn, MF_MSG_KERNEL_HIGH_ORDER, MF_IGNORED);
1494-
return -EBUSY;
1500+
res = -EBUSY;
14951501
}
1502+
goto unlock_mutex;
14961503
}
14971504

14981505
if (PageTransHuge(hpage)) {
14991506
if (try_to_split_thp_page(p, "Memory Failure") < 0) {
15001507
action_result(pfn, MF_MSG_UNSPLIT_THP, MF_IGNORED);
1501-
return -EBUSY;
1508+
res = -EBUSY;
1509+
goto unlock_mutex;
15021510
}
15031511
VM_BUG_ON_PAGE(!page_count(p), p);
15041512
}
@@ -1522,7 +1530,7 @@ int memory_failure(unsigned long pfn, int flags)
15221530
if (PageCompound(p) && compound_head(p) != orig_head) {
15231531
action_result(pfn, MF_MSG_DIFFERENT_COMPOUND, MF_IGNORED);
15241532
res = -EBUSY;
1525-
goto out;
1533+
goto unlock_page;
15261534
}
15271535

15281536
/*
@@ -1542,14 +1550,14 @@ int memory_failure(unsigned long pfn, int flags)
15421550
num_poisoned_pages_dec();
15431551
unlock_page(p);
15441552
put_page(p);
1545-
return 0;
1553+
goto unlock_mutex;
15461554
}
15471555
if (hwpoison_filter(p)) {
15481556
if (TestClearPageHWPoison(p))
15491557
num_poisoned_pages_dec();
15501558
unlock_page(p);
15511559
put_page(p);
1552-
return 0;
1560+
goto unlock_mutex;
15531561
}
15541562

15551563
/*
@@ -1573,7 +1581,7 @@ int memory_failure(unsigned long pfn, int flags)
15731581
if (!hwpoison_user_mappings(p, pfn, flags, &p)) {
15741582
action_result(pfn, MF_MSG_UNMAP_FAILED, MF_IGNORED);
15751583
res = -EBUSY;
1576-
goto out;
1584+
goto unlock_page;
15771585
}
15781586

15791587
/*
@@ -1582,13 +1590,15 @@ int memory_failure(unsigned long pfn, int flags)
15821590
if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) {
15831591
action_result(pfn, MF_MSG_TRUNCATED_LRU, MF_IGNORED);
15841592
res = -EBUSY;
1585-
goto out;
1593+
goto unlock_page;
15861594
}
15871595

15881596
identify_page_state:
15891597
res = identify_page_state(pfn, p, page_flags);
1590-
out:
1598+
unlock_page:
15911599
unlock_page(p);
1600+
unlock_mutex:
1601+
mutex_unlock(&mf_mutex);
15921602
return res;
15931603
}
15941604
EXPORT_SYMBOL_GPL(memory_failure);

0 commit comments

Comments
 (0)