From patchwork Wed Mar 29 15:11:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13192690 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 86D75C761AF for ; Wed, 29 Mar 2023 15:11:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DAD6E900004; Wed, 29 Mar 2023 11:11:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D5BFB900002; Wed, 29 Mar 2023 11:11:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C23B9900004; Wed, 29 Mar 2023 11:11:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B407C900002 for ; Wed, 29 Mar 2023 11:11:30 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 08E1B1A0609 for ; Wed, 29 Mar 2023 15:11:30 +0000 (UTC) X-FDA: 80622274740.20.A8D6036 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf19.hostedemail.com (Postfix) with ESMTP id 138F21A0018 for ; Wed, 29 Mar 2023 15:11:27 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rIXXwFUI; spf=pass (imf19.hostedemail.com: domain of 3H1UkZAgKCIIpogwo4gtmuumrk.iusrot03-ssq1giq.uxm@flex--jiaqiyan.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3H1UkZAgKCIIpogwo4gtmuumrk.iusrot03-ssq1giq.uxm@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680102688; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Ky+wdshhLqyJAiHPRVddmE8oftGUteWHVoj9BXSbSfw=; b=NNOxY515kkaHOQzjoO1yPFdCQ7z5YLi/lbyi788LvVyj/EYAQ5YB/wg/dPHaopqdYma407 NykxIuDdwGwF9arztUSCPNFCZPyTNG6jHB7KZ0JustXIYR80ZoKcbkBEhHCtxuE4wc9Rnt MFLGhPnSuS1jkwic1VCTPqELwG9ALAI= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rIXXwFUI; spf=pass (imf19.hostedemail.com: domain of 3H1UkZAgKCIIpogwo4gtmuumrk.iusrot03-ssq1giq.uxm@flex--jiaqiyan.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3H1UkZAgKCIIpogwo4gtmuumrk.iusrot03-ssq1giq.uxm@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680102688; a=rsa-sha256; cv=none; b=jHb9HZzkh56tF41fR/ujGw68PqiuEny9Qn4wXnCiaUsxx+ZRauDkusLge6ht0YiLB6k+5d GzDdWZDojs5Mzlr/Q9xEHVxNgZ7N7I3knGZkMbGEZz3UrjzFaFGKXOnfEMui5bOmpAPxtU 41BR/kOF8U6jgkC0w5Oso0GXo99vd9s= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-54574d6204aso157714857b3.15 for ; Wed, 29 Mar 2023 08:11:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680102687; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Ky+wdshhLqyJAiHPRVddmE8oftGUteWHVoj9BXSbSfw=; b=rIXXwFUI6eJ032lyoW1kidSd6RT2JO4l/1la76d/LckVnqUWT5EDLNULN+X10ncDKw 5HNo0KPppjwizpDk9ORg6UlyWk57QJakxLvs/XfESDj1/qlaNJjX0ATNLMiDfXjvCbb8 uPIkzmraLepCFt+E+gxMoc2e2wE1f+jrRRsmzsk0VCHH3FNBd339x52kaPY5fG75cEoX MLtAObOXZWdShFAnRQdyszu3LKOGsHoZq3XnfHXSxGI50cTEYQVJTZVrSJ9bRelfgnHF ZKEaL+NelDkSO3FUwy68Lm6uOvZqiPzexhHyS/Ci8D69u2f/ci0Tk2iSzxF4FXuCcM2k BC2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680102687; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Ky+wdshhLqyJAiHPRVddmE8oftGUteWHVoj9BXSbSfw=; b=Z/4FiEQtWVzoBNY0rCeYC8p9iPFgdIHLom4PTcJoHmq6pGxxCwWvd1rXf3UP59tx5D h9GUgcOS5wSg/SIcE/705oaQgRojlu76jAKv3mInSVoqPJ3Z7mUgfBnMHPxwtodOSXGJ 7vnUruAbdP1OPy66NFrQiCV3zFaqW8U8qT/HzQXElz3pl8rnMAT9Y2OfbWwlfc54RSnl NoZ90L58O34ra/1b6TxIETBwsTweEDwCyIIKR20CZiJj7UVXVdjjog0OP7gsCHML5eYL KGDQoC+uFF23nriUutfTn9LZtX/LyOPmNqS2ub0xHN5MIpi/Ng/Iii1Hc9fitqvLzAmQ xSYw== X-Gm-Message-State: AAQBX9cbUBWLW2oIujySRNCi4iDVRbZwMaJqzV9pxniftqNaksXE4A9j UGuQvI57m984WAokPjbupKq8n5mYAfu02A== X-Google-Smtp-Source: AKy350ayqDIcmr4ppXW9akRiYY4BDgZ2qFWuwxIcTx0bDUdihcLgb4G7WCcSvrK7dsqRKj5ySfX22m0JkY4hEg== X-Received: from yjqkernel.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1837]) (user=jiaqiyan job=sendgmr) by 2002:a05:6902:1507:b0:b78:8bd8:6e88 with SMTP id q7-20020a056902150700b00b788bd86e88mr11787681ybu.6.1680102687204; Wed, 29 Mar 2023 08:11:27 -0700 (PDT) Date: Wed, 29 Mar 2023 08:11:19 -0700 In-Reply-To: <20230329151121.949896-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20230329151121.949896-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230329151121.949896-2-jiaqiyan@google.com> Subject: [PATCH v12 1/3] mm/khugepaged: recover from poisoned anonymous memory From: Jiaqi Yan To: kirill.shutemov@linux.intel.com, kirill@shutemov.name, shy828301@gmail.com, tongtiangen@huawei.com, tony.luck@intel.com Cc: naoya.horiguchi@nec.com, linmiaohe@huawei.com, jiaqiyan@google.com, linux-mm@kvack.org, akpm@linux-foundation.org, osalvador@suse.de, wangkefeng.wang@huawei.com, stevensd@chromium.org, hughd@google.com X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 138F21A0018 X-Rspam-User: X-Stat-Signature: th53opfk7hqs9io7mfgi6iczirkcfh7i X-HE-Tag: 1680102687-164661 X-HE-Meta: U2FsdGVkX18T8mb4KPZqaWFKiNhsmnqLj4qNnVYKX6KDboLp7dxTIrVs2raCFvAX7/yGl4kKnD8ckLrsxyoZ3N17+q5fuyQ+NOk6gGQqcEXskHjK3af3xKwwApAQBynpl9HcymDMDDOEzdEn/OywTNN2qiHd7PEObHW6BaYwIx1BKQvzfE19IL1NpUbUdyAkgOrevC7pvBoEMruYN7r+rYabCnuUt31ID85zFWGXmV+Cl9vFBuSwa7cLHRlWcy+SFv5/0yMk+lSzQ9iAnNy+ml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c2neerbiiQW0sVwoc1sRg6ezLEqbS04kyxmoBtaWIa1WxCn82QadhQp/j9M0r9d/lkDyc3kjNo0nr+h57rIQIQ/UK+ZXQgKTveGSswl+SG/Senh5SerjPjpsQni76daEQCi9B1uShjMcNx5aR1SViJfFkeioP3mB3N3vy4q8W5STIAQCrFa3BuN76tmcZyFJiowwtP5j1SSu5GeDs8DnmA2HrEAAgMkA5JzbDqELcuf6A19eackgGjKQFM0bk+d3ereI98If9OWj0hWZsjs= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Make __collapse_huge_page_copy return whether copying anonymous pages succeeded, and make collapse_huge_page handle the return status. Break existing PTE scan loop into two for-loops. The first loop copies source pages into target huge page, and can fail gracefully when running into memory errors in source pages. If copying all pages succeeds, the second loop releases and clears up these normal pages. Otherwise, the second loop rolls back the page table and page states by: - re-establishing the original PTEs-to-PMD connection. - releasing source pages back to their LRU list. Tested manually: 0. Enable khugepaged on system under test. 1. Start a two-thread application. Each thread allocates a chunk of non-huge anonymous memory buffer. 2. Pick 4 random buffer locations (2 in each thread) and inject uncorrectable memory errors at corresponding physical addresses. 3. Signal both threads to make their memory buffer collapsible, i.e. calling madvise(MADV_HUGEPAGE). 4. Wait and check kernel log: khugepaged is able to recover from poisoned pages and skips collapsing them. 5. Signal both threads to inspect their buffer contents and make sure no data corruption. Signed-off-by: Jiaqi Yan --- include/trace/events/huge_memory.h | 3 +- mm/khugepaged.c | 112 +++++++++++++++++++++++++---- 2 files changed, 101 insertions(+), 14 deletions(-) diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge_memory.h index 3e6fb05852f9a..46cce509957ba 100644 --- a/include/trace/events/huge_memory.h +++ b/include/trace/events/huge_memory.h @@ -36,7 +36,8 @@ EM( SCAN_ALLOC_HUGE_PAGE_FAIL, "alloc_huge_page_failed") \ EM( SCAN_CGROUP_CHARGE_FAIL, "ccgroup_charge_failed") \ EM( SCAN_TRUNCATED, "truncated") \ - EMe(SCAN_PAGE_HAS_PRIVATE, "page_has_private") \ + EM( SCAN_PAGE_HAS_PRIVATE, "page_has_private") \ + EMe(SCAN_COPY_MC, "copy_poisoned_page") \ #undef EM #undef EMe diff --git a/mm/khugepaged.c b/mm/khugepaged.c index bee7fd7db380a..269df3f052e8e 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -55,6 +55,7 @@ enum scan_result { SCAN_CGROUP_CHARGE_FAIL, SCAN_TRUNCATED, SCAN_PAGE_HAS_PRIVATE, + SCAN_COPY_MC, }; #define CREATE_TRACE_POINTS @@ -681,20 +682,21 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, return result; } -static void __collapse_huge_page_copy(pte_t *pte, struct page *page, - struct vm_area_struct *vma, - unsigned long address, - spinlock_t *ptl, - struct list_head *compound_pagelist) +static void __collapse_huge_page_copy_succeeded(pte_t *pte, + struct vm_area_struct *vma, + unsigned long address, + spinlock_t *ptl, + struct list_head *compound_pagelist) { - struct page *src_page, *tmp; + struct page *src_page; + struct page *tmp; pte_t *_pte; - for (_pte = pte; _pte < pte + HPAGE_PMD_NR; - _pte++, page++, address += PAGE_SIZE) { - pte_t pteval = *_pte; + pte_t pteval; + for (_pte = pte; _pte < pte + HPAGE_PMD_NR; + _pte++, address += PAGE_SIZE) { + pteval = *_pte; if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { - clear_user_highpage(page, address); add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1); if (is_zero_pfn(pte_pfn(pteval))) { /* @@ -706,7 +708,6 @@ static void __collapse_huge_page_copy(pte_t *pte, struct page *page, } } else { src_page = pte_page(pteval); - copy_user_highpage(page, src_page, address, vma); if (!PageCompound(src_page)) release_pte_page(src_page); /* @@ -733,6 +734,87 @@ static void __collapse_huge_page_copy(pte_t *pte, struct page *page, } } +static void __collapse_huge_page_copy_failed(pte_t *pte, + pmd_t *pmd, + pmd_t orig_pmd, + struct vm_area_struct *vma, + struct list_head *compound_pagelist) +{ + spinlock_t *pmd_ptl; + + /* + * Re-establish the PMD to point to the original page table + * entry. Restoring PMD needs to be done prior to releasing + * pages. Since pages are still isolated and locked here, + * acquiring anon_vma_lock_write is unnecessary. + */ + pmd_ptl = pmd_lock(vma->vm_mm, pmd); + pmd_populate(vma->vm_mm, pmd, pmd_pgtable(orig_pmd)); + spin_unlock(pmd_ptl); + /* + * Release both raw and compound pages isolated + * in __collapse_huge_page_isolate. + */ + release_pte_pages(pte, pte + HPAGE_PMD_NR, compound_pagelist); +} + +/* + * __collapse_huge_page_copy - attempts to copy memory contents from raw + * pages to a hugepage. Cleans up the raw pages if copying succeeds; + * otherwise restores the original page table and releases isolated raw pages. + * Returns SCAN_SUCCEED if copying succeeds, otherwise returns SCAN_COPY_MC. + * + * @pte: starting of the PTEs to copy from + * @page: the new hugepage to copy contents to + * @pmd: pointer to the new hugepage's PMD + * @orig_pmd: the original raw pages' PMD + * @vma: the original raw pages' virtual memory area + * @address: starting address to copy + * @ptl: lock on raw pages' PTEs + * @compound_pagelist: list that stores compound pages + */ +static int __collapse_huge_page_copy(pte_t *pte, + struct page *page, + pmd_t *pmd, + pmd_t orig_pmd, + struct vm_area_struct *vma, + unsigned long address, + spinlock_t *ptl, + struct list_head *compound_pagelist) +{ + struct page *src_page; + pte_t *_pte; + pte_t pteval; + unsigned long _address; + int result = SCAN_SUCCEED; + + /* + * Copying pages' contents is subject to memory poison at any iteration. + */ + for (_pte = pte, _address = address; _pte < pte + HPAGE_PMD_NR; + _pte++, page++, _address += PAGE_SIZE) { + pteval = *_pte; + if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { + clear_user_highpage(page, _address); + continue; + } + src_page = pte_page(pteval); + if (copy_mc_user_highpage(page, src_page, _address, vma) > 0) { + result = SCAN_COPY_MC; + break; + } + } + + if (likely(result == SCAN_SUCCEED)) + __collapse_huge_page_copy_succeeded(pte, vma, address, ptl, + compound_pagelist); + else + __collapse_huge_page_copy_failed(pte, pmd, orig_pmd, vma, + compound_pagelist); + + return result; +} + static void khugepaged_alloc_sleep(void) { DEFINE_WAIT(wait); @@ -1106,9 +1188,13 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, */ anon_vma_unlock_write(vma->anon_vma); - __collapse_huge_page_copy(pte, hpage, vma, address, pte_ptl, - &compound_pagelist); + result = __collapse_huge_page_copy(pte, hpage, pmd, _pmd, + vma, address, pte_ptl, + &compound_pagelist); pte_unmap(pte); + if (unlikely(result != SCAN_SUCCEED)) + goto out_up_write; + /* * spin_lock() below is not the equivalent of smp_wmb(), but * the smp_wmb() inside __SetPageUptodate() can be reused to