From patchwork Wed Mar 29 15:11:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13192691 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 50C0DC74A5B for ; Wed, 29 Mar 2023 15:11:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D156B280001; Wed, 29 Mar 2023 11:11:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BAF20900002; Wed, 29 Mar 2023 11:11:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A28A1280001; Wed, 29 Mar 2023 11:11:33 -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 893CC900002 for ; Wed, 29 Mar 2023 11:11:33 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 454021C625D for ; Wed, 29 Mar 2023 15:11:33 +0000 (UTC) X-FDA: 80622274866.06.4EE26BD Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf05.hostedemail.com (Postfix) with ESMTP id 70908100022 for ; Wed, 29 Mar 2023 15:11:31 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Dyuhe+7n; spf=pass (imf05.hostedemail.com: domain of 3IlUkZAgKCIUsrjzr7jwpxxpun.lxvurw36-vvt4jlt.x0p@flex--jiaqiyan.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3IlUkZAgKCIUsrjzr7jwpxxpun.lxvurw36-vvt4jlt.x0p@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680102691; a=rsa-sha256; cv=none; b=vN0bAbCcx59bU9+CGvJ3WEA3k566rYwIjvvyz0Pw3HsMjMsdU3J18Lhb3fdpa/VDmQAInV idlHIphGhS7U0A44slpOZfJTQaASQo2PkkNOOw4kLIMhBHbClMUhiiyyHGgV8kAMFziOVv okon47YOX5UlNwTpqtsVOtiwZZKBE0A= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Dyuhe+7n; spf=pass (imf05.hostedemail.com: domain of 3IlUkZAgKCIUsrjzr7jwpxxpun.lxvurw36-vvt4jlt.x0p@flex--jiaqiyan.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3IlUkZAgKCIUsrjzr7jwpxxpun.lxvurw36-vvt4jlt.x0p@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=1680102691; 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=H+gtwlatEJ8DMC6CZIRqkE0iHjFuMvKnIG4rRqiGZVc=; b=5l9ADrYyH6RHZ7MbOqlHr7NUd4YlJ13rd3ANxD/VlTjfcQLNcgMtY18JuFEFBao6o+gSiM 6FBog6QX4qQQlQe5GL7cYDtebk3wpnuIEv65kU+tH+8pFhQ0q+9HvJ0K1k4dWof+20Q5yY ShAfc+xaWq75cpTm3yWtr2kGdpEJuPY= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-544781e30easo156276637b3.1 for ; Wed, 29 Mar 2023 08:11:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680102690; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=H+gtwlatEJ8DMC6CZIRqkE0iHjFuMvKnIG4rRqiGZVc=; b=Dyuhe+7niyozcVMA6ZfeP+KTNWuJt/aEaRxzveJjycaCb/b8jvs7bWcPudJafxB1Mo IDKgyHReGg9abAxZDoBd/Z138l2cPUj47LztZYLJIQxQa+E8x7dP+RTNnu527VNc3qNc w4eQgtfUBaSCDoQuVzZx3V1XWaz00b3A55R9wWqG8f/AELYiXhLMuLeZ3EZO9RiyIpgE 0xHyFoP5Y8OqMXWg0NmU82Gw89K7xU4vfsfePrf36AmvsREzflCijqNonrJPyLxmCo6e 17fbuJ0AmuwPVQyznf/nGVnTfxTqD5kSDW/mFrmhqYx1rw5Vc0HLxCK472smthgVsurj q6Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680102690; 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=H+gtwlatEJ8DMC6CZIRqkE0iHjFuMvKnIG4rRqiGZVc=; b=y9UmOgIhVh4pUklZWERrcf375cndcAywNVgzrNNf9cZTlFZM2jtoqv3zbJ2UYNuNdf q1iQOQCA3oYcIOm9Cp6z+xc+bptRxVcVrCBnLPnLmWhi6fVLztE0dsiCmyqRA2NTwadh cvox1Z2W0GiNRnHFQ0nKKHO1t08P24bj8tsUXonbXkbLwrIbI7oTMFqQ1wArIBqd364Y cF6FVIMFAln7d/0cuQHmsG5E2dZ7Zz7PEBwHZhXkKPnmyYb6u8D8cU4YzGBJ0ihs+KCi KIEwGs/SQHjcBWpF89RPfMJVe3GsKfE4gkNsqt6QjEX0I76bXjqp4PRwUUoaNnt748oe bhtw== X-Gm-Message-State: AAQBX9cB4HAcRxSQe1+sALn3V6+4HKfCrfnxFX9JcKn/aVJg4H0kDBrU NgVnRr/60SaMHzUjYMlTXDg8Sa/XaOKy4w== X-Google-Smtp-Source: AKy350ZcxfJuL1wchjjonaTebnqM7QP4cLP2vpRpMGJEs4S2Yn8kQqp+kN6skd7LgcYnLy/3eoWfCNOd+FMbsQ== X-Received: from yjqkernel.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1837]) (user=jiaqiyan job=sendgmr) by 2002:a05:6902:1586:b0:b68:7b14:186b with SMTP id k6-20020a056902158600b00b687b14186bmr9666036ybu.1.1680102690657; Wed, 29 Mar 2023 08:11:30 -0700 (PDT) Date: Wed, 29 Mar 2023 08:11:21 -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-4-jiaqiyan@google.com> Subject: [PATCH v12 3/3] mm/khugepaged: recover from poisoned file-backed 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: 70908100022 X-Rspamd-Server: rspam01 X-Stat-Signature: 34fs55sqb3dtg5up83jqqqhtz9jjarjc X-HE-Tag: 1680102691-121458 X-HE-Meta: U2FsdGVkX1/XqpIR5wttdG76ISw1drYbutUHUUVGLo6Y2N54cMeoj0JSyNSze+4WrZGIizLbYD/xXFNYVuF5FfJAyRmOVKOqKMh79kRlsWUVhep9XG4oaKfd91BAXwGa7hLGtS8nVaGUe8suEE3RDuA9TB8dNx7dKxQOaY/rbI1lAFLgLqKb7HdVOq1X6QP+iAfqyRnuhFX6lPTXkDYI3Wth1B+GvNnHhKH6JVa6p6N8HQLMuqhi4g/D4z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s7OCZVEHvdNmlpKuxHDbsgf6TF3AIoj54VyO5SAvf1lU4pNNFsyFKqt7pW5ge0TCuHuzH3lhpChGL1PL29aVKg2HOYYA8FweCxv0pb1yxEwkrfDsjy7p3pUZmvm799ud4F5v8nNB8/Hm7R399PTC4VrfkGP+m1psgtWtD61cfE4zQF7HGzajaALG5rHOL9TEFmaopEoUoLcDJXKbr4Ftdu0qFYqu9c+zOZmyQl0ppzjO53IoXZAihvsN6A4hKWwURNepGSTMmraUd9dqnejW9QOENfeGyxeY4ffrpwt56Y= 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 copying operations into a separate loop - postpone the updates for nr_none until both scanning and copying succeeded - postpone joining small xarray entries until both scanning and copying succeeded - postpone the update operations to NR_XXX_THPS until both scanning and copying succeeded - for non-SHMEM file, roll back filemap_nr_thps_inc if scan succeeded but copying failed Tested manually: 0. Enable khugepaged on system under test. Mount tmpfs at /mnt/ramdisk. 1. Start a two-thread application. Each thread allocates a chunk of non-huge memory buffer from /mnt/ramdisk. 2. Pick 4 random buffer address (2 in each thread) and inject uncorrectable memory errors at physical addresses. 3. Signal both threads to make their memory buffer collapsible, i.e. calling madvise(MADV_HUGEPAGE). 4. Wait and then check kernel log: khugepaged is able to recover from poisoned pages by skipping them. 5. Signal both threads to inspect their buffer contents and make sure no data corruption. Signed-off-by: Jiaqi Yan --- mm/khugepaged.c | 88 +++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 32 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 269df3f052e8e..0b04276f764db 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1872,6 +1872,9 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, { struct address_space *mapping = file->f_mapping; struct page *hpage; + struct page *page; + struct page *tmp; + struct folio *folio; pgoff_t index = 0, end = start + HPAGE_PMD_NR; LIST_HEAD(pagelist); XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER); @@ -1916,8 +1919,7 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, xas_set(&xas, start); for (index = start; index < end; index++) { - struct page *page = xas_next(&xas); - struct folio *folio; + page = xas_next(&xas); VM_BUG_ON(index != xas.xa_index); if (is_shmem) { @@ -2097,12 +2099,8 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, put_page(page); goto xa_unlocked; } - nr = thp_nr_pages(hpage); - if (is_shmem) - __mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr); - else { - __mod_lruvec_page_state(hpage, NR_FILE_THPS, nr); + if (!is_shmem) { filemap_nr_thps_inc(mapping); /* * Paired with smp_mb() in do_dentry_open() to ensure @@ -2113,21 +2111,9 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, smp_mb(); if (inode_is_open_for_write(mapping->host)) { result = SCAN_FAIL; - __mod_lruvec_page_state(hpage, NR_FILE_THPS, -nr); filemap_nr_thps_dec(mapping); - goto xa_locked; } } - - if (nr_none) { - __mod_lruvec_page_state(hpage, NR_FILE_PAGES, nr_none); - /* nr_none is always 0 for non-shmem. */ - __mod_lruvec_page_state(hpage, 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, hpage); xa_locked: xas_unlock_irq(&xas); xa_unlocked: @@ -2140,21 +2126,36 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, try_to_unmap_flush(); if (result == SCAN_SUCCEED) { - struct page *page, *tmp; - struct folio *folio; - /* * 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(hpage + (index % HPAGE_PMD_NR)); index++; } - copy_highpage(hpage + (page->index % HPAGE_PMD_NR), - page); + if (copy_mc_highpage(hpage + (page->index % HPAGE_PMD_NR), + page) > 0) { + result = SCAN_COPY_MC; + break; + } + index++; + } + while (result == SCAN_SUCCEED && index < end) { + clear_highpage(hpage + (index % HPAGE_PMD_NR)); + index++; + } + } + + nr = thp_nr_pages(hpage); + 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); @@ -2162,12 +2163,23 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, ClearPageUnevictable(page); unlock_page(page); put_page(page); - index++; } - while (index < end) { - clear_highpage(hpage + (index % HPAGE_PMD_NR)); - index++; + + xas_lock_irq(&xas); + if (is_shmem) + __mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr); + else + __mod_lruvec_page_state(hpage, NR_FILE_THPS, nr); + + if (nr_none) { + __mod_lruvec_page_state(hpage, NR_FILE_PAGES, nr_none); + /* nr_none is always 0 for non-shmem. */ + __mod_lruvec_page_state(hpage, 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, hpage); + xas_unlock_irq(&xas); folio = page_folio(hpage); folio_mark_uptodate(folio); @@ -2185,8 +2197,6 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, unlock_page(hpage); hpage = NULL; } else { - struct page *page; - /* Something went wrong: roll back page cache changes */ xas_lock_irq(&xas); if (nr_none) { @@ -2220,6 +2230,20 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, 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); + /* + * Paired with smp_mb() in do_dentry_open() to + * ensure the update to nr_thps is visible. + */ + smp_mb(); + } + xas_unlock_irq(&xas); hpage->mapping = NULL;