From mboxrd@z Thu Jan 1 00:00:00 1970 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 6FDC1C05027 for ; Wed, 1 Feb 2023 05:17:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CFA976B0071; Wed, 1 Feb 2023 00:16:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CABBA6B0072; Wed, 1 Feb 2023 00:16:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B716D6B0074; Wed, 1 Feb 2023 00:16:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A7A8A6B0071 for ; Wed, 1 Feb 2023 00:16:59 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 73190140941 for ; Wed, 1 Feb 2023 05:16:59 +0000 (UTC) X-FDA: 80417563758.27.A87E2A8 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf04.hostedemail.com (Postfix) with ESMTP id A924A4001B for ; Wed, 1 Feb 2023 05:16:57 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=aU4xzQBe; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of jiaqiyan@google.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=jiaqiyan@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675228617; 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=HiUhy1cwEwlGDHWgQ7C++ggrAR2VYdqRQiaKIpuvaYU=; b=kaZv9HA2itxADvr9y5Hii1YGxTxOTeRWOOIM/9XIWregz1VTNzmmFLgon9qFdRrQhn1LyP rdPEXQ6Sc0YcJBcj4NXv9UnRSKUaip5f9iTGrHeOsBgjKLvQS4icRQfnUkY0137ulJ3DXr YYyjbv8mBZxb+TznsmEoIUQwxCar4ro= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=aU4xzQBe; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of jiaqiyan@google.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=jiaqiyan@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675228617; a=rsa-sha256; cv=none; b=3UI1ZT7I9SdofFJeOEwBNHUfwxlmRyyoaBk483H23la8k8/8x733Ojt8/hCceRr0+KRywl GgEjro/LveEAfbRSCRlfTFS4iklvWs01KORDa9+LxE/hHZNrgcGOgdtb08LCNqkY14gIyJ 7Ley7Aoc/gGJbHahpXtOvNkVm9mQZOQ= Received: by mail-pj1-f47.google.com with SMTP id ha3-20020a17090af3c300b00230222051a6so860529pjb.2 for ; Tue, 31 Jan 2023 21:16:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=HiUhy1cwEwlGDHWgQ7C++ggrAR2VYdqRQiaKIpuvaYU=; b=aU4xzQBemrLoTlvHp2wpxSLAOMDIHv9j8UvEE3mEzCuw1LWSdWmeb7vZb9SAVCOjUk DYaQ8v8cv9bpG2Q7XpbZWDfK4Klx10885/cUKoWa8Dy7iftycf+IiOlz7LxlJfq7j/Xa aWU7+YlZw5nHmQtAKfCg8x6hwT1G1KWWgOoNSFgTu6IQibD37p7S4TZTC1gECoTM0Zg6 pdsLVIn8UqJ4ST9ZVr9JuuxYtNVjFluSUD0RDIMm6UryO+alxnBWyw2BFeDiw55paD/b pmIXmhtUL6vajGYORbg3hP4H+Uot5T1u7dkyXFkP+r2u9+YigsmBTcHLA5M46wENQI20 RZPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=HiUhy1cwEwlGDHWgQ7C++ggrAR2VYdqRQiaKIpuvaYU=; b=YtiqIxktbyo5r0itaee9ZKz7ubkeBQm/RnR4Y41r+fJefH+fuRJxFUzRl9XF3AT6TL Db7Fr9aYf5CEMy+Y6xRyA4+kxJxEF7WxmipK2TxMGdBvG+cbeF15x2lmlZ43vNpn5MK+ lnZafJC82kkxxDgQrkp7Px+1xDmm1EWpT77xs+BtMXhPFD+lJogtX35VHzxgxaMkuv8b xCrM+rNwmGTqYgsfHE9YCVZFOppwxxSg/zH2kSvn+76NHs9vpuAHhqBeRtQQN/qTDxD5 KY9sli3tOndhOHCmnc1attyGaWohm+4XKdZKO+sATah7qeRd+VdKyajBM9nbHyadU2Ui wJ9w== X-Gm-Message-State: AO0yUKXvk6x8SkG3HDEXK5DxgDC+x/wnFP/aMRob1d9uwhxkqxOO+TSe 4gAjVxtv2NnF/75OHkNOf5mrumOR65XwtnTtQk5LMQ== X-Google-Smtp-Source: AK7set8Okh+N0SCcXOhi36QPJAIjzoxSA4p24mf5Waf9/T/ap9A91sMksJhpgZMnYrg+uAUjC2NGNF+RkI175+j0aVE= X-Received: by 2002:a17:90a:fc8e:b0:22c:34ef:2065 with SMTP id ci14-20020a17090afc8e00b0022c34ef2065mr149990pjb.89.1675228616231; Tue, 31 Jan 2023 21:16:56 -0800 (PST) MIME-Version: 1.0 References: <20221205234059.42971-1-jiaqiyan@google.com> <20221205234059.42971-2-jiaqiyan@google.com> <20230119150258.npfadnefkpny5fd3@box.shutemov.name> <20230124003349.m64heg7mnqw7snyh@box.shutemov.name> In-Reply-To: <20230124003349.m64heg7mnqw7snyh@box.shutemov.name> From: Jiaqi Yan Date: Tue, 31 Jan 2023 21:16:45 -0800 Message-ID: Subject: Re: [PATCH v9 1/2] mm/khugepaged: recover from poisoned anonymous memory To: kirill.shutemov@linux.intel.com Cc: kirill@shutemov.name, shy828301@gmail.com, tongtiangen@huawei.com, tony.luck@intel.com, akpm@linux-foundation.org, wangkefeng.wang@huawei.com, naoya.horiguchi@nec.com, linmiaohe@huawei.com, linux-mm@kvack.org, osalvador@suse.de Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: A924A4001B X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 4bw5tyyyg39m68m73qb1cjjb91mhdx8k X-HE-Tag: 1675228617-220586 X-HE-Meta: U2FsdGVkX1+kUmDipPk8qepq4sz5HWPw7sb6J0X5kdSRAoeJvw9hKMDN3wWbq+ZbilDiM5OQzfuzKp93cWW7J462IvDa3JVbUMeGgCW1++G/TdW99haV6GiT+838ydbi36b2UxhJi5VPKGk5a60uvtLh5uAuchULeeoTP7VcchgyJ0ztiGPIOIGpwZFZrfW0gBDrRJVIE69nANlSO05uLeJFsaIRssFyV1DbGrwW9yPYTUko64csaNl/YkjzSbGoP6wCfwXuRji5y/c1TgncDyxU5n/ERAeuF8fD1Mm8X85CBeQkbxK6Pfy9OQOU35j1PlOxe9sqwpaPcvs11S69t6BwSwE70hp5X7igXp3CDuwxBnKThZXttcRxCFxQneEk2dQB/PFVwaJMpI9dFgzlcSKBWSXSWhGmjErzius63J5jN9F/+4g9rB7znC1HnrSiRrI9Ko3bUeMKbKVkh0hNETELi7YJnPi9fpEISSV38SibicZiRCB+Ewb2hiTjOJg4Nq9JcX4olUfTiDQY+f+1q632r99jP0i38rrM6xBl01YfiWAr69T9i2TbZQvPQDr5ut+uZt1K6pxDJiWcWYirNriSWQykkBfKm3XYqtKlqrEScrgeMk6lDIyi8aLK/GZ7pPpcJf4Jki1S7X5lbYVxjqqpai4Jmiwzal3gM4GTlg2kFCvgt9ayp7M8NrN/CC5TkH1i7iYPf7nqnEkiPMdlEUie1nZZoKoEzqHl2LijLLyvzcjjLrkEstK12VRi0Oi+dQ/ln7XB3exLq1Lax1tbwatr72WDi+UUPJLAyukWWUwJ+WtvmexesGMpQZ7Bz082vRKeSLm9lnuprvGPkl4LKaVHtoSMzguT9OMsFKWOFWp94l8iODdYAB6K73XaDeqoLbyXEm0+gLXuy9JyIsB4/JX5yIcIEgDh9RbfdP5EMcP2BMLw9D623jU1++0rx89/aaLo8a/V/MUVlDka9Ip LdChqfPe S0ntR6O6iwW1e4p/wL002hXankdhbDLek3S6tJfVZjnU+XRYUyD/r0xtiXW829ayT+2f5hC+TRSPCQiw1RK+ezGnBi8ZcyRag+pGZceV3w+yQ24R0E6ORTMrB9tL6vKOlTatVl7dcgJojfqvuy/O/v8SyO3oSsnGwm0GDi3s3veWf7X9oLmmbz6RconfFlVq4FMfVWK6xKan+nb88Kr84yjYwPYINCvfHTd5I16IXeCCiVr6SkJGlDiQN9Gifd2IISY51lhxZlYxDCHAFDOi1U4ExoA51TVozd2cT 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: On Mon, Jan 23, 2023 at 4:34 PM wrote: > > On Fri, Jan 20, 2023 at 07:56:15AM -0800, Jiaqi Yan wrote: > > On Thu, Jan 19, 2023 at 7:03 AM wrote: > > > > > > On Mon, Dec 05, 2022 at 03:40:58PM -0800, Jiaqi Yan wrote: > > > > 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 | 179 ++++++++++++++++++++++------- > > > > 2 files changed, 139 insertions(+), 43 deletions(-) > > > > > > > > diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge_memory.h > > > > index 35d759d3b0104..5743ae970af31 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 5a7d2d5093f9c..0f1b9e05e17ec 100644 > > > > --- a/mm/khugepaged.c > > > > +++ b/mm/khugepaged.c > > > > @@ -19,6 +19,7 @@ > > > > #include > > > > #include > > > > #include > > > > +#include > > > > > > > > #include > > > > #include > > > > @@ -55,6 +56,7 @@ enum scan_result { > > > > SCAN_CGROUP_CHARGE_FAIL, > > > > SCAN_TRUNCATED, > > > > SCAN_PAGE_HAS_PRIVATE, > > > > + SCAN_COPY_MC, > > > > }; > > > > > > > > #define CREATE_TRACE_POINTS > > > > @@ -530,6 +532,27 @@ static bool is_refcount_suitable(struct page *page) > > > > return page_count(page) == expected_refcount; > > > > } > > > > > > > > +/* > > > > + * Copies memory with #MC in source page (@from) handled. Returns number > > > > + * of bytes not copied if there was an exception; otherwise 0 for success. > > > > + * Note handling #MC requires arch opt-in. > > > > + */ > > > > +static int copy_mc_page(struct page *to, struct page *from) > > > > +{ > > > > + char *vfrom, *vto; > > > > + unsigned long ret; > > > > + > > > > + vfrom = kmap_local_page(from); > > > > + vto = kmap_local_page(to); > > > > + ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE); > > > > + if (ret == 0) > > > > + kmsan_copy_page_meta(to, from); > > > > + kunmap_local(vto); > > > > + kunmap_local(vfrom); > > > > + > > > > + return ret; > > > > +} > > > > > > > > > It is very similar to copy_mc_user_highpage(), but uses > > > kmsan_copy_page_meta() instead of kmsan_unpoison_memory(). > > > > > > Could you explain the difference? I don't quite get it. > > > > copy_mc_page is actually the MC version of copy_highpage, which uses > > kmsan_copy_page_meta instead of kmsan_unpoison_memory. > > > > My understanding is kmsan_copy_page_meta covers kmsan_unpoison_memory. > > When there is no metadata (kmsan_shadow or kmsan_origin), both > > kmsan_copy_page_meta and kmsan_unpoison_memory just do > > kmsan_internal_unpoison_memory to mark the memory range as > > initialized; when there is metadata in src page, kmsan_copy_page_meta > > will copy whatever metadata in src to dst. So I think > > kmsan_copy_page_meta is the right thing to do. > > Should we fix copy_mc_user_highpage() then? I think it depends on what copy_user_highpage() (the original of copy_mc_user_highpage) is used for. copy_mc_user_highpage is currently only used by __wp_page_copy_user, is it possible that here we don't want to (or don't need to) copy page metadata for userspace pages? > > > > Indentation levels get out of control. Maybe some code restructuring is > > > required? > > > > v10 will change to something like this to reduce 1 level of indentation: > > > > if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) > > continue; > > src_page = pte_page(pteval); > > if (!PageCompound(src_page)) > > release_pte_page(src_page); > > I hoped for deeper rework. Maybe split the function into several functions > and make overall structure more readable? How about turning the 2nd loop into __collapse_huge_page_copy_succeeded and __collapse_huge_page_copy_failed, one for the case copy succeeded, and one for failed? Like this: if (likely(result == SCAN_SUCCEED)) __collapse_huge_page_copy_succeeded(...); else __collapse_huge_page_copy_failed(...); My prototype shows it could reduce the level indents. > > -- > Kiryl Shutsemau / Kirill A. Shutemov