[PATCH v5 03/16] mm,madvise: call soft_offline_page() without MF_COUNT_INCREASED

From: nao . horiguchi
Date: Fri Jul 31 2020 - 08:21:36 EST


From: Naoya Horiguchi <naoya.horiguchi@xxxxxxx>

The call to get_user_pages_fast is only to get the pointer to a struct
page of a given address, pinning it is memory-poisoning handler's job,
so drop the refcount grabbed by get_user_pages_fast().

Note that the target page is still pinned after this put_page() because
the current process should have refcount from mapping.

Signed-off-by: Naoya Horiguchi <naoya.horiguchi@xxxxxxx>
Signed-off-by: Oscar Salvador <osalvador@xxxxxxx>
Reviewed-by: Mike Kravetz <mike.kravetz@xxxxxxxxxx>
---
mm/madvise.c | 24 ++++++++++++------------
1 file changed, 12 insertions(+), 12 deletions(-)

diff --git v5.8-rc7-mmotm-2020-07-27-18-18/mm/madvise.c v5.8-rc7-mmotm-2020-07-27-18-18_patched/mm/madvise.c
index a16dba21cdf6..1fe89a5b8d33 100644
--- v5.8-rc7-mmotm-2020-07-27-18-18/mm/madvise.c
+++ v5.8-rc7-mmotm-2020-07-27-18-18_patched/mm/madvise.c
@@ -910,16 +910,24 @@ static int madvise_inject_error(int behavior,
*/
size = page_size(compound_head(page));

- if (PageHWPoison(page)) {
- put_page(page);
+ /*
+ * The get_user_pages_fast() is just to get the pfn of the
+ * given address, and the refcount has nothing to do with
+ * what we try to test, so it should be released immediately.
+ * This is racy but it's intended because the real hardware
+ * errors could happen at any moment and memory error handlers
+ * must properly handle the race.
+ */
+ put_page(page);
+
+ if (PageHWPoison(page))
continue;
- }

if (behavior == MADV_SOFT_OFFLINE) {
pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
pfn, start);

- ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
+ ret = soft_offline_page(pfn, 0);
if (ret)
return ret;
continue;
@@ -927,14 +935,6 @@ static int madvise_inject_error(int behavior,

pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
pfn, start);
-
- /*
- * Drop the page reference taken by get_user_pages_fast(). In
- * the absence of MF_COUNT_INCREASED the memory_failure()
- * routine is responsible for pinning the page to prevent it
- * from being released back to the page allocator.
- */
- put_page(page);
ret = memory_failure(pfn, 0);
if (ret)
return ret;
--
2.17.1