Skip to content

Commit f29623e

Browse files
MiaoheLinakpm00
authored andcommitted
mm: memory-failure: fix potential unexpected return value from unpoison_memory()
If unpoison_memory() fails to clear page hwpoisoned flag, return value ret is expected to be -EBUSY. But when get_hwpoison_page() returns 1 and fails to clear page hwpoisoned flag due to races, return value will be unexpected 1 leading to users being confused. And there's a code smell that the variable "ret" is used not only to save the return value of unpoison_memory(), but also the return value from get_hwpoison_page(). Make a further cleanup by using another auto-variable solely to save the return value of get_hwpoison_page() as suggested by Naoya. Link: https://lkml.kernel.org/r/[email protected] Fixes: bf181c5 ("mm/hwpoison: fix unpoison_memory()") Signed-off-by: Miaohe Lin <[email protected]> Cc: Kefeng Wang <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Cc: Naoya Horiguchi <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent f985fc3 commit f29623e

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

mm/memory-failure.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,7 +2466,7 @@ int unpoison_memory(unsigned long pfn)
24662466
{
24672467
struct folio *folio;
24682468
struct page *p;
2469-
int ret = -EBUSY;
2469+
int ret = -EBUSY, ghp;
24702470
unsigned long count = 1;
24712471
bool huge = false;
24722472
static DEFINE_RATELIMIT_STATE(unpoison_rs, DEFAULT_RATELIMIT_INTERVAL,
@@ -2514,29 +2514,28 @@ int unpoison_memory(unsigned long pfn)
25142514
if (folio_test_slab(folio) || PageTable(&folio->page) || folio_test_reserved(folio))
25152515
goto unlock_mutex;
25162516

2517-
ret = get_hwpoison_page(p, MF_UNPOISON);
2518-
if (!ret) {
2517+
ghp = get_hwpoison_page(p, MF_UNPOISON);
2518+
if (!ghp) {
25192519
if (PageHuge(p)) {
25202520
huge = true;
25212521
count = folio_free_raw_hwp(folio, false);
2522-
if (count == 0) {
2523-
ret = -EBUSY;
2522+
if (count == 0)
25242523
goto unlock_mutex;
2525-
}
25262524
}
25272525
ret = folio_test_clear_hwpoison(folio) ? 0 : -EBUSY;
2528-
} else if (ret < 0) {
2529-
if (ret == -EHWPOISON) {
2526+
} else if (ghp < 0) {
2527+
if (ghp == -EHWPOISON) {
25302528
ret = put_page_back_buddy(p) ? 0 : -EBUSY;
2531-
} else
2529+
} else {
2530+
ret = ghp;
25322531
unpoison_pr_info("Unpoison: failed to grab page %#lx\n",
25332532
pfn, &unpoison_rs);
2533+
}
25342534
} else {
25352535
if (PageHuge(p)) {
25362536
huge = true;
25372537
count = folio_free_raw_hwp(folio, false);
25382538
if (count == 0) {
2539-
ret = -EBUSY;
25402539
folio_put(folio);
25412540
goto unlock_mutex;
25422541
}

0 commit comments

Comments
 (0)