From patchwork Tue Apr 5 20:51:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 12801952 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 9FE7EC433EF for ; Tue, 5 Apr 2022 20:53:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FF478D0002; Tue, 5 Apr 2022 16:52:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8ADFA6B0074; Tue, 5 Apr 2022 16:52:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 728C18D0002; Tue, 5 Apr 2022 16:52:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0083.hostedemail.com [216.40.44.83]) by kanga.kvack.org (Postfix) with ESMTP id 548686B0073 for ; Tue, 5 Apr 2022 16:52:05 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id F26D8AB9B7 for ; Tue, 5 Apr 2022 20:51:54 +0000 (UTC) X-FDA: 79324022190.30.130BB51 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf03.hostedemail.com (Postfix) with ESMTP id 814622000D for ; Tue, 5 Apr 2022 20:51:54 +0000 (UTC) Received: by mail-pj1-f74.google.com with SMTP id lr15-20020a17090b4b8f00b001c646e432baso2407073pjb.3 for ; Tue, 05 Apr 2022 13:51:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=kS4uon/vzKSFHkD3fE+oJGoGYW5gRPVFsbz2CaKwVOA=; b=nQ2JUlrPSUA/IIgnR+TcEgaSj2Mu0cDaYsJCE0R2TYP7FD8b+wSKJNh0Epk1a6s0SU 2gPjq0u2DJHE50RYV6tFAqp+dIPpC+kK10uCu42fHK/Xrbj1KJsY60zVK67+ZyRm7cob it4q2BGqvrfDsYojODON1QogBwb4d+tkA9J5sC3vmAQjZcyJHOJ1IFkfXdMs0K9gLohs uiR9Vfmp7TOo/0Ysfv7gKDzEjU1qOKPt5kh0sVH+HZj8M3TpcxCEnKo4jWtATk4N1pih 5zTh6jBfgEXdeJS7M8i6ci2sl20DqVDLnrC4fNwxSff9N24K5snIOBNM48w+s19+gxPH P0xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=kS4uon/vzKSFHkD3fE+oJGoGYW5gRPVFsbz2CaKwVOA=; b=FpybZnQHhmIvmBU4Qrt+CqAXZAsFFkH5vbBP1HMyCZBPaszPzvP3dVx7XGKi1A+jWE RGUARFLFOymmlENTm3fdnvkkIA30kJkM3pu4gMY3C065Pmz5Gnafi2MdDNK6pzmi/d67 e6Xt+iODIv2+1Id95M31mmvQ0mTNmSYGzJ5pyCL5NCvCgze1IR6Y0ikZuUc0dtbs77RG p0JyDhd3uZy4bQJoBu3C5vKiZJHt+092eMfKTRE0wMQ68BPW0MBUtAC8yHwgblc24Eoo 0Q532V9uYqZWw0raVxVy3VMFpVzy5xx7R09JXa7JeY8Zv+xIcwSYOwaMaMFyCji6AueZ mu7Q== X-Gm-Message-State: AOAM531nxHsfRLOtYdvSTl7xe1OCqYHM767Sgo0ULXz3l8+dNPrLCZb/ pVxFFXK3UZBcozDWL0+nldJXf88eArId+w== X-Google-Smtp-Source: ABdhPJykUsIqisjVgWko3q+IP+1aZuR8slAKyLNkpORl11H2jlNroQNOWc4dNKYrZjdXpHXaBSSx8AttWGkAeg== X-Received: from yjqkernel.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1837]) (user=jiaqiyan job=sendgmr) by 2002:a17:903:246:b0:153:87f0:a93e with SMTP id j6-20020a170903024600b0015387f0a93emr5316809plh.171.1649191913405; Tue, 05 Apr 2022 13:51:53 -0700 (PDT) Date: Tue, 5 Apr 2022 13:51:46 -0700 In-Reply-To: <20220405205146.411595-1-jiaqiyan@google.com> Message-Id: <20220405205146.411595-3-jiaqiyan@google.com> Mime-Version: 1.0 References: <20220405205146.411595-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.35.1.1094.g7c7d902a7c-goog Subject: [RFC v2 2/2] mm: khugepaged: recover from poisoned file-backed memory From: Jiaqi Yan To: shy828301@gmail.com, tongtiangen@huawei.com Cc: tony.luck@intel.com, naoya.horiguchi@nec.com, kirill.shutemov@linux.intel.com, linmiaohe@huawei.com, juew@google.com, jiaqiyan@google.com, linux-mm@kvack.org X-Rspam-User: X-Stat-Signature: c8dr53n9gw7c5wrbokbtob6ose75euuy Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=nQ2JUlrP; spf=pass (imf03.hostedemail.com: domain of 36atMYggKCIwzyq6yEq3w44w1u.s421y3AD-220Bqs0.47w@flex--jiaqiyan.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=36atMYggKCIwzyq6yEq3w44w1u.s421y3AD-220Bqs0.47w@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 814622000D X-HE-Tag: 1649191914-905300 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_file roll back when copying pages failed. More concretely: * extract copy operations into a separate loop * postpone the updates for nr_none until both scan and copy succeeded * postpone joining small xarray entries until both scan and copy succeeded * postpone the update operations to NR_XXX_THPS * for non-SHMEM file, roll back filemap_nr_thps_inc if scan succeeded but copy failed Signed-off-by: Jiaqi Yan --- mm/khugepaged.c | 77 ++++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index eecaeaf6eaebe..0c06758395931 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1710,7 +1710,7 @@ static void collapse_file(struct mm_struct *mm, { struct address_space *mapping = file->f_mapping; gfp_t gfp; - struct page *new_page; + struct page *new_page, *page, *tmp; pgoff_t index, end = start + HPAGE_PMD_NR; LIST_HEAD(pagelist); XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER); @@ -1766,7 +1766,7 @@ static void collapse_file(struct mm_struct *mm, xas_set(&xas, start); for (index = start; index < end; index++) { - struct page *page = xas_next(&xas); + page = xas_next(&xas); VM_BUG_ON(index != xas.xa_index); if (is_shmem) { @@ -1938,10 +1938,7 @@ static void collapse_file(struct mm_struct *mm, } nr = thp_nr_pages(new_page); - if (is_shmem) - __mod_lruvec_page_state(new_page, NR_SHMEM_THPS, nr); - else { - __mod_lruvec_page_state(new_page, NR_FILE_THPS, nr); + if (!is_shmem) { filemap_nr_thps_inc(mapping); /* * Paired with smp_mb() in do_dentry_open() to ensure @@ -1952,40 +1949,44 @@ static void collapse_file(struct mm_struct *mm, smp_mb(); if (inode_is_open_for_write(mapping->host)) { result = SCAN_FAIL; - __mod_lruvec_page_state(new_page, NR_FILE_THPS, -nr); filemap_nr_thps_dec(mapping); goto xa_locked; } } - if (nr_none) { - __mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none); - if (is_shmem) - __mod_lruvec_page_state(new_page, NR_SHMEM, nr_none); - } - - /* Join all the small entries into a single multi-index entry */ - xas_set_order(&xas, start, HPAGE_PMD_ORDER); - xas_store(&xas, new_page); xa_locked: xas_unlock_irq(&xas); xa_unlocked: if (result == SCAN_SUCCEED) { - struct page *page, *tmp; - /* * Replacing old pages with new one has succeeded, now we - * need to copy the content and free the old pages. + * attempt to copy the contents. */ index = start; - list_for_each_entry_safe(page, tmp, &pagelist, lru) { + list_for_each_entry(page, &pagelist, lru) { while (index < page->index) { clear_highpage(new_page + (index % HPAGE_PMD_NR)); index++; } - copy_highpage(new_page + (page->index % HPAGE_PMD_NR), - page); + if (copy_highpage_mc(new_page + (page->index % HPAGE_PMD_NR), page)) { + result = SCAN_COPY_MC; + break; + } + index++; + } + while (result == SCAN_SUCCEED && index < end) { + clear_highpage(new_page + (page->index % HPAGE_PMD_NR)); + index++; + } + } + + if (result == SCAN_SUCCEED) { + /* + * Copying old pages to huge one has succeeded, now we + * need to free the old pages. + */ + list_for_each_entry_safe(page, tmp, &pagelist, lru) { list_del(&page->lru); page->mapping = NULL; page_ref_unfreeze(page, 1); @@ -1993,12 +1994,23 @@ static void collapse_file(struct mm_struct *mm, ClearPageUnevictable(page); unlock_page(page); put_page(page); - index++; } - while (index < end) { - clear_highpage(new_page + (index % HPAGE_PMD_NR)); - index++; + + xas_lock_irq(&xas); + if (is_shmem) + __mod_lruvec_page_state(new_page, NR_SHMEM_THPS, nr); + else + __mod_lruvec_page_state(new_page, NR_FILE_THPS, nr); + + if (nr_none) { + __mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none); + if (is_shmem) + __mod_lruvec_page_state(new_page, NR_SHMEM, nr_none); } + /* Join all the small entries into a single multi-index entry. */ + xas_set_order(&xas, start, HPAGE_PMD_ORDER); + xas_store(&xas, new_page); + xas_unlock_irq(&xas); SetPageUptodate(new_page); page_ref_add(new_page, HPAGE_PMD_NR - 1); @@ -2014,9 +2026,9 @@ static void collapse_file(struct mm_struct *mm, khugepaged_pages_collapsed++; } else { - struct page *page; - - /* Something went wrong: roll back page cache changes */ + /* + * Something went wrong: roll back page cache changes + */ xas_lock_irq(&xas); mapping->nrpages -= nr_none; @@ -2049,6 +2061,13 @@ static void collapse_file(struct mm_struct *mm, xas_lock_irq(&xas); } VM_BUG_ON(nr_none); + /* + * Undo the updates of filemap_nr_thps_inc for non-SHMEM file only. + * This undo is not needed unless failure is due to SCAN_COPY_MC. + */ + if (!is_shmem && result == SCAN_COPY_MC) + filemap_nr_thps_dec(mapping); + xas_unlock_irq(&xas); new_page->mapping = NULL;