From patchwork Mon Mar 27 21:15:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13190089 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 371C5C76195 for ; Mon, 27 Mar 2023 21:16:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B6C2900005; Mon, 27 Mar 2023 17:16:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9677B900002; Mon, 27 Mar 2023 17:16:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74429900005; Mon, 27 Mar 2023 17:16:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5CFC9900002 for ; Mon, 27 Mar 2023 17:16:00 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 258BB4084E for ; Mon, 27 Mar 2023 21:16:00 +0000 (UTC) X-FDA: 80615935680.07.7041D12 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf17.hostedemail.com (Postfix) with ESMTP id 511AE4001C for ; Mon, 27 Mar 2023 21:15:58 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=HylNha+8; spf=pass (imf17.hostedemail.com: domain of 3jQciZAgKCEwxwo4wCo1u22uzs.q20zw18B-00y9oqy.25u@flex--jiaqiyan.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3jQciZAgKCEwxwo4wCo1u22uzs.q20zw18B-00y9oqy.25u@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679951758; a=rsa-sha256; cv=none; b=uFIx97KmGH2jrll4OS3156M84zWfuF4yE5pb6tOUuyDeGGwdgFztXBX27SGI36kDPzVhIZ Qt9qE428JhbFSDdRdMYzzAfaqPiRNa4XK+3kG1+lN8RxBqR9SgRUz6qpKgZNihRMW+urhX P8ohmXCwfcRoN1HN87jiUGR9tYn/PBA= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=HylNha+8; spf=pass (imf17.hostedemail.com: domain of 3jQciZAgKCEwxwo4wCo1u22uzs.q20zw18B-00y9oqy.25u@flex--jiaqiyan.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3jQciZAgKCEwxwo4wCo1u22uzs.q20zw18B-00y9oqy.25u@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=1679951758; 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=MqyuxLW2kXP6Ga2JVdfW9yCiE/P5K6gIQXlw5p9VGh0=; b=xAN6Dx6ppDO7EOuGrG5eGb7C29G+mL1a8LeqQOLHtkAFCAyYOCTo1NbRMeSNCCZ3GexYJm POHAFlfjBbjagrsCTAQg1NkzyHyv9Pg+rgIfTau3c6VPzwkdMd7gWiWBfLyQnnlELSo6VX UhaQ7eYncbqxgb9PGJb+4A5I698VQn8= Received: by mail-pf1-f202.google.com with SMTP id d4-20020a056a00198400b00628000145bcso4801591pfl.0 for ; Mon, 27 Mar 2023 14:15:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679951757; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MqyuxLW2kXP6Ga2JVdfW9yCiE/P5K6gIQXlw5p9VGh0=; b=HylNha+8Y8ZEDY7M/u1Ej07Pt4dh9rMU5YvvQdzClv16N8R9ehRsgsQJvWRVI/6el+ clPKbViN7WvbtPSI7RaREabFMxja94Xxefi6OFok1siC6Vfrxc7kVjSCJzVHSOXtCm/x cn3LPaesXPv5hO2oJCvBFNPYtJO8LgcVHbQ+BdvxZXOMJ5lif7eAHFDWTDdpYPtkia/7 xxaUZNLUC9eruRMP/M3YVh6DFWsV3tcwXF4rmylRnhLk7cCUOMEbJZUpHqS3EKFXBSqY 3vQI1wcb5atcsr+HiFWrKgOHaM8AJHfEoFiH8VEV5Opj8zQTiV7mPR0aD+diOGYr+lDZ weow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679951757; 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=MqyuxLW2kXP6Ga2JVdfW9yCiE/P5K6gIQXlw5p9VGh0=; b=LQdOxJFYWpTxIRAZU8JDCLjoI5d6EVzWOQHZY3fQ42kbc7QOLPWGwLLz8DL4TAagx2 3vailPHU4Eel4AcP0D3exb9OaQwqUq8CcZWvjQZ0Zy0XJvz5uaM5ap2K8R68con903Ch Hq/A2yjf3C6kCoagivI7bgzu1SKMqdojd5DuIGj2uJcoTzUMDrzuBUn84OR7C+Lr+6pY CdUjKpYTfnl7wdX72A2fYuPisuLD0BCDmnoTslmWFMzxzMugdrQqJgyBmQHsJpHTKL8G W7hxm3wMU8mG4YCwEtPFUuFwhK/IawZeEqw404tjdQqDU1ajQ0ZSeW6BfbHG7m0FecjI jIHQ== X-Gm-Message-State: AAQBX9ee39FnzKUS6j1Ed5MxrA+Kj6ww4+1IXfHQX2iqS4xlfx/jz47H 4Di5rAo/sXJY5kRfXGTimEn2DNQbjCzEgw== X-Google-Smtp-Source: AKy350aqZiKW45DpLdxGdpAIesp01xJcl5NN4Z4odQg2RNyzQPEn1ASMkIUjNO5tLYrJIqgGhlVmG1kKA6iYtA== X-Received: from yjqkernel.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1837]) (user=jiaqiyan job=sendgmr) by 2002:a05:6a00:8c7:b0:625:cda5:c28c with SMTP id s7-20020a056a0008c700b00625cda5c28cmr6422119pfu.6.1679951757244; Mon, 27 Mar 2023 14:15:57 -0700 (PDT) Date: Mon, 27 Mar 2023 14:15:46 -0700 In-Reply-To: <20230327211548.462509-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20230327211548.462509-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230327211548.462509-2-jiaqiyan@google.com> Subject: [PATCH v11 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-Rspam-User: X-Rspamd-Queue-Id: 511AE4001C X-Rspamd-Server: rspam01 X-Stat-Signature: 75heqodupwn15upksznkyykh7jeo37r4 X-HE-Tag: 1679951758-870464 X-HE-Meta: U2FsdGVkX1/+n5y/9nPHTUOmowmj9aWfcwERdZ3Ex0NUqegysryxfxdlJ8Z/5ZPZP5yvcAjEVy2N/cW1CRKHF2BuqO04wgL4+ZwpqXZu+PtSSPZetIpzKXq1Bg5UzmkX/+k8C3vjMKhCb0o1yQ6GLdeZPhpZSlqLcnUwlf50WorkE5RmWrJcvTuJdpyBLgXuFPUO/RhEiQNTW0RmZx+ntHvoXS25QA/jc66IEOghWQbzUQjAZgg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l+wlqrHRe/0SgvIp4FF5nH6wWqhwTyPqCHYM02go1nbB3uAueHugs+6uw1coNrRaTbm9Zkg488UYi4FC09R5oU9tIuXIqUsdGu4l6c/67TQrhmia0D6JVsg8J7ANOaV2xHEK0obUaA9TIkzemEx//UUCEkTF3Xv86XtM7JDVi+gQNU5CDCGcKcZOnqusDMHJwN5nH4FkcZ9ev8ImGgOeVouDPRlNbu6Qb0+j2Z9ExuTmdluBpK9lGjXauAb1exE9110GyToGRbPJDdEXhl0HK3rCWjkZJMa0X5h5x+RbuUU551GIlNyMkbHsyf3a2nyY3Ej/mZ+w8lEw= 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 Reviewed-by: Yang Shi --- include/trace/events/huge_memory.h | 3 +- mm/khugepaged.c | 114 +++++++++++++++++++++++++---- 2 files changed, 103 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..bef68286345c8 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,22 @@ 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, + pmd_t *pmd, + 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 +709,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 +735,88 @@ 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, + unsigned long address, + 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 + * @pte_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 *pte_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, pmd, vma, address, + pte_ptl, compound_pagelist); + else + __collapse_huge_page_copy_failed(pte, pmd, orig_pmd, vma, + address, compound_pagelist); + + return result; +} + static void khugepaged_alloc_sleep(void) { DEFINE_WAIT(wait); @@ -1106,9 +1190,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