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 X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17904C433B4 for ; Wed, 21 Apr 2021 00:57:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 69862613F5 for ; Wed, 21 Apr 2021 00:57:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 69862613F5 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E85B18D0002; Tue, 20 Apr 2021 20:57:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E5F3B6B0071; Tue, 20 Apr 2021 20:57:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CFE718D0002; Tue, 20 Apr 2021 20:57:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0208.hostedemail.com [216.40.44.208]) by kanga.kvack.org (Postfix) with ESMTP id B6ED26B0070 for ; Tue, 20 Apr 2021 20:57:43 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 6588D1804EE06 for ; Wed, 21 Apr 2021 00:57:43 +0000 (UTC) X-FDA: 78054561606.01.1146BF3 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf01.hostedemail.com (Postfix) with ESMTP id 31AB95001533 for ; Wed, 21 Apr 2021 00:57:41 +0000 (UTC) Received: by mail-pf1-f180.google.com with SMTP id a12so27035804pfc.7 for ; Tue, 20 Apr 2021 17:57:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2nq3/ECMlWFOh9aBfURRW3bFhH3w1pC1RXXgmopGjoU=; b=RX8GqcDVqZryXJsFgtBirG/p+dv1y2g1b+RWkMv0FQyh6r5Ipj7L6leC1fIJSC/9n3 28PxkUuszl8U/a7UgsjXKZyGL6Sj2ptJBc/sts2+QhRCiZ8s+ZMW2bFg7eWXdiWzr1TA Xaf8FKzhhDJ/zSf1ilrHy17GfNF0xRqvhOs55tac5DqxOZTAoU0rwZ6OAookbT5S6yHT 57ptXEh+pvDkg0orHgcgYYvztvSjIJWtn/JEmQB59nxhlMNIFZnqxSV/IqI7iP1N5BTL DLH4GuI2CsgZUCqrHe+4LYcJ7OJLPdwyP9VPfRvG74jnazmIaY1ZFE2iuC58AFfMm1vy 6Nww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2nq3/ECMlWFOh9aBfURRW3bFhH3w1pC1RXXgmopGjoU=; b=OmIdC0SYHcf9uqCQvrhHO4qwjoTDPC+QIWaiaBMT8KOBTzM0Oh0OFL6yI6in7U1WLS gxZ63kS455dYhr5IhFypZF/KZxcyAufwnG321Kv8jg4suePIt7QEkMPdmZYOqhdjKRH0 MNm47DY6f640QFRBIXcoSvADchlM5SdLnrcrF74+XwENkui0rQ7wHLpQhBjJ7f6OW7Xx Y8VOixegL8FfXKm8EZzpfit75ZqizzypPfemKO/5jO9Xw+3btn7GEEihg7KonTHH1lWN VujSQR0yRXF2Hsp3yivG9+BaFYEsNpbB20a28S7Auob5G8uOj6LSd4VSUT/Je5zImR82 ezYw== X-Gm-Message-State: AOAM533/BJgb/ehU+pFjptG/S2tPACUf6FGTteHJvUTzGBZ0g+9lgeC5 CSK8BD+/R1p9kQdo30sy/qoPNN/09q4b1hI= X-Google-Smtp-Source: ABdhPJwZIo48/xU0japGGgAYMRnjMEC6BjOY0TIBYvR1Z6I1QyWJulIj08Iac3q+WSoNK7ZCcx85rw== X-Received: by 2002:a63:344:: with SMTP id 65mr19429870pgd.24.1618966662035; Tue, 20 Apr 2021 17:57:42 -0700 (PDT) Received: from localhost.localdomain (h175-177-040-153.catv02.itscom.jp. [175.177.40.153]) by smtp.gmail.com with ESMTPSA id e13sm178278pfi.199.2021.04.20.17.57.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Apr 2021 17:57:41 -0700 (PDT) From: Naoya Horiguchi To: linux-mm@kvack.org, Tony Luck , Aili Yao Cc: Andrew Morton , Oscar Salvador , David Hildenbrand , Borislav Petkov , Andy Lutomirski , Naoya Horiguchi , Jue Wang , linux-kernel@vger.kernel.org Subject: [PATCH v3 1/3] mm/memory-failure: Use a mutex to avoid memory_failure() races Date: Wed, 21 Apr 2021 09:57:26 +0900 Message-Id: <20210421005728.1994268-2-nao.horiguchi@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210421005728.1994268-1-nao.horiguchi@gmail.com> References: <20210421005728.1994268-1-nao.horiguchi@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 31AB95001533 X-Stat-Signature: dgeutsjqhi17r1nu99tp3f747rowojpw Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf01; identity=mailfrom; envelope-from=""; helo=mail-pf1-f180.google.com; client-ip=209.85.210.180 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1618966661-61254 Content-Transfer-Encoding: quoted-printable 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: From: Tony Luck There can be races when multiple CPUs consume poison from the same page. The first into memory_failure() atomically sets the HWPoison page flag and begins hunting for tasks that map this page. Eventually it invalidates those mappings and may send a SIGBUS to the affected tasks. But while all that work is going on, other CPUs see a "success" return code from memory_failure() and so they believe the error has been handled and continue executing. Fix by wrapping most of the internal parts of memory_failure() in a mutex. Signed-off-by: Tony Luck Signed-off-by: Naoya Horiguchi --- mm/memory-failure.c | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git v5.12-rc8/mm/memory-failure.c v5.12-rc8_patched/mm/memory-fail= ure.c index 24210c9bd843..4087308e4b32 100644 --- v5.12-rc8/mm/memory-failure.c +++ v5.12-rc8_patched/mm/memory-failure.c @@ -1381,6 +1381,8 @@ static int memory_failure_dev_pagemap(unsigned long= pfn, int flags, return rc; } =20 +static DEFINE_MUTEX(mf_mutex); + /** * memory_failure - Handle memory failure of a page. * @pfn: Page Number of the corrupted page @@ -1404,7 +1406,7 @@ int memory_failure(unsigned long pfn, int flags) struct page *hpage; struct page *orig_head; struct dev_pagemap *pgmap; - int res; + int res =3D 0; unsigned long page_flags; bool retry =3D true; =20 @@ -1424,13 +1426,18 @@ int memory_failure(unsigned long pfn, int flags) return -ENXIO; } =20 + mutex_lock(&mf_mutex); + try_again: - if (PageHuge(p)) - return memory_failure_hugetlb(pfn, flags); + if (PageHuge(p)) { + res =3D memory_failure_hugetlb(pfn, flags); + goto unlock_mutex; + } + if (TestSetPageHWPoison(p)) { pr_err("Memory failure: %#lx: already hardware poisoned\n", pfn); - return 0; + goto unlock_mutex; } =20 orig_head =3D hpage =3D compound_head(p); @@ -1463,17 +1470,19 @@ int memory_failure(unsigned long pfn, int flags) res =3D MF_FAILED; } action_result(pfn, MF_MSG_BUDDY, res); - return res =3D=3D MF_RECOVERED ? 0 : -EBUSY; + res =3D res =3D=3D MF_RECOVERED ? 0 : -EBUSY; } else { action_result(pfn, MF_MSG_KERNEL_HIGH_ORDER, MF_IGNORED); - return -EBUSY; + res =3D -EBUSY; } + goto unlock_mutex; } =20 if (PageTransHuge(hpage)) { if (try_to_split_thp_page(p, "Memory Failure") < 0) { action_result(pfn, MF_MSG_UNSPLIT_THP, MF_IGNORED); - return -EBUSY; + res =3D -EBUSY; + goto unlock_mutex; } VM_BUG_ON_PAGE(!page_count(p), p); } @@ -1497,7 +1506,7 @@ int memory_failure(unsigned long pfn, int flags) if (PageCompound(p) && compound_head(p) !=3D orig_head) { action_result(pfn, MF_MSG_DIFFERENT_COMPOUND, MF_IGNORED); res =3D -EBUSY; - goto out; + goto unlock_page; } =20 /* @@ -1517,14 +1526,14 @@ int memory_failure(unsigned long pfn, int flags) num_poisoned_pages_dec(); unlock_page(p); put_page(p); - return 0; + goto unlock_mutex; } if (hwpoison_filter(p)) { if (TestClearPageHWPoison(p)) num_poisoned_pages_dec(); unlock_page(p); put_page(p); - return 0; + goto unlock_mutex; } =20 if (!PageTransTail(p) && !PageLRU(p)) @@ -1543,7 +1552,7 @@ int memory_failure(unsigned long pfn, int flags) if (!hwpoison_user_mappings(p, pfn, flags, &p)) { action_result(pfn, MF_MSG_UNMAP_FAILED, MF_IGNORED); res =3D -EBUSY; - goto out; + goto unlock_page; } =20 /* @@ -1552,13 +1561,15 @@ int memory_failure(unsigned long pfn, int flags) if (PageLRU(p) && !PageSwapCache(p) && p->mapping =3D=3D NULL) { action_result(pfn, MF_MSG_TRUNCATED_LRU, MF_IGNORED); res =3D -EBUSY; - goto out; + goto unlock_page; } =20 identify_page_state: res =3D identify_page_state(pfn, p, page_flags); -out: +unlock_page: unlock_page(p); +unlock_mutex: + mutex_unlock(&mf_mutex); return res; } EXPORT_SYMBOL_GPL(memory_failure); --=20 2.25.1