Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2542325pxb; Tue, 13 Apr 2021 04:39:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwGS1d9H7O0Ci2FggJlhnzVqZ0LkWfkKlWSDEVqgEztqCqeAlm5H+MTqIEXxadwwGHtBNHD X-Received: by 2002:a17:906:600b:: with SMTP id o11mr32151469ejj.345.1618313952315; Tue, 13 Apr 2021 04:39:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618313952; cv=none; d=google.com; s=arc-20160816; b=lHiBFrCpGVdNntF7T2kljqG11H5CTm8IJmtBELU8aOHsPQ3FVaTL2bhA/UaDJJFAno 4FoALSNiGw9cvbIxz5ktWmGFzxi5Uzh5n2+rSOs5Ln0GktsHeRxKj60D9uRwsRDDwAl9 UgA8OseQOi1FtJ+pKm6/1qOU8kll9uFYDbY0czPTj9LXUfPhHHKkYRQ28Ztk+qNlJUyw jMCFy2dFGtTe5LwvZeAQ/1bpAZTdTewp4dTnK9mUtVfl1iw8f2Ds6WP3H6cGJXws+C+3 YDXpIs5WCedQ9hrEAk/MojvtUHLv2FN87meo5AZ78seng1IW+6XRkmNAJH7hlpDKeQKT mOvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JkA3Ny2V6Qq9Dk3aSySw7xYrKKcRmImrxlIoiSgIjQ0=; b=y7Ttv5oWlcuEqLgxsHSYhoK7H2a5gkgAi12FUc5Tl3+DyjL37bNFAE2sogQIGFAdOd zi1L8HH6ZbNkv7M3h80/twTWAfhRXosDCGn6MiXZxoFZHxaLXMZY7eC1G8ywGycF2GTo 8TC4o5BLL1kMtgkPycsk01ZIIjoCL2vHG0v+2f7Lliie4R6G/IOr+nUY5/T4OfzFUO1S X+UvYrgauE1V3kf0euURqlrW/yklNXlGuN9jeLVy/MpOwQZTT8koK5h3gPiiWaL9RRSH AB5BA6BwV/vYfgWg2pDiO8/4IBjkQDPRvxE3QcQmwuQBEf3wWhOjHVWxc/6qS8gnf6ja KlwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=X0R7t1a+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ay8si9306977edb.225.2021.04.13.04.38.48; Tue, 13 Apr 2021 04:39:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=X0R7t1a+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343790AbhDLWnt (ORCPT + 99 others); Mon, 12 Apr 2021 18:43:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238901AbhDLWnt (ORCPT ); Mon, 12 Apr 2021 18:43:49 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7258C061574 for ; Mon, 12 Apr 2021 15:43:30 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id p16so3201916plf.12 for ; Mon, 12 Apr 2021 15:43:30 -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=JkA3Ny2V6Qq9Dk3aSySw7xYrKKcRmImrxlIoiSgIjQ0=; b=X0R7t1a+/vUebM2KusNVgIyrf8ZQZp4ygxUvDy25rGxEtvJoLMDsHRy7aV1gFRm/wk NM5SL9lNBEg7xDvdaBfI05WJqBVaArAYzFx5PMGvhQ5sEkph/y8LEjcXZStSlTqB2Xej XqaytIGgnAlq2YhnsXpj+uu0h1YWIE2qVUwrtrVWtKn9q9exriujEzk8rvqkz2mOwQuN +9mL+t1FV8qrKEJHe/TBJgLUA+fqpY6vGIjUCQULhxghq2pxhqpGoz6aPe6gILzvEIJ0 msoxnEDFV+G5c0zmVQLP3NhTVz4ca+9EmUwwP3rwHFWLzuxkbdjTnqjPGDcGuBFcDzpp E2DA== 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=JkA3Ny2V6Qq9Dk3aSySw7xYrKKcRmImrxlIoiSgIjQ0=; b=sXy/zCnYJM/6YuhTygT0j2N6mVmAHTRcMYJ305oetNIGyEE0d8gNsomWg/X96cNDTa VVWftJuh65uaiAgo7u8OeVApiUCQp3YUTRk3ND6OPbajXOhsCcBz6kM6e37gvxe6rFRv sqR+gz1BgzHh9A9h6D/WOjhMpXe/RWBOsO1Mpd/EtZuRLhrgP+szwtEYiI0t18h1un8J xiAsgmIccZtLaekldcniWbrnY0PrEZzSsm2/d5hPXU8+/Tq7axf9LuaAizI920P6LHw7 Fc6O293KHgg5nxZIqGD/nkMYUhOgK7hS7yTbgNBJxJiDUlJmBWhtdSLfsRhpo6LkIran D05A== X-Gm-Message-State: AOAM531sK2TYCzIYwVaDNxUakI8R1LA2ra3Kh1I4wiRivOPriEzTIxe6 sclPaFtBQpJyDkhK0BHWeA== X-Received: by 2002:a17:90b:4b8c:: with SMTP id lr12mr1556254pjb.124.1618267410185; Mon, 12 Apr 2021 15:43:30 -0700 (PDT) Received: from localhost.localdomain (h175-177-040-153.catv02.itscom.jp. [175.177.40.153]) by smtp.gmail.com with ESMTPSA id l25sm13365373pgu.72.2021.04.12.15.43.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 15:43:29 -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 , linux-kernel@vger.kernel.org Subject: [PATCH v1 1/3] mm/memory-failure: Use a mutex to avoid memory_failure() races Date: Tue, 13 Apr 2021 07:43:18 +0900 Message-Id: <20210412224320.1747638-2-nao.horiguchi@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210412224320.1747638-1-nao.horiguchi@gmail.com> References: <20210412224320.1747638-1-nao.horiguchi@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git v5.12-rc5/mm/memory-failure.c v5.12-rc5_patched/mm/memory-failure.c index 24210c9bd843..c1509f4b565e 100644 --- v5.12-rc5/mm/memory-failure.c +++ v5.12-rc5_patched/mm/memory-failure.c @@ -1381,6 +1381,8 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags, return rc; } +static DEFINE_MUTEX(mf_mutex); + /** * memory_failure - Handle memory failure of a page. * @pfn: Page Number of the corrupted page @@ -1424,12 +1426,18 @@ int memory_failure(unsigned long pfn, int flags) return -ENXIO; } + mutex_lock(&mf_mutex); + try_again: - if (PageHuge(p)) - return memory_failure_hugetlb(pfn, flags); + if (PageHuge(p)) { + res = memory_failure_hugetlb(pfn, flags); + goto out2; + } + if (TestSetPageHWPoison(p)) { pr_err("Memory failure: %#lx: already hardware poisoned\n", pfn); + mutex_unlock(&mf_mutex); return 0; } @@ -1463,9 +1471,11 @@ int memory_failure(unsigned long pfn, int flags) res = MF_FAILED; } action_result(pfn, MF_MSG_BUDDY, res); + mutex_unlock(&mf_mutex); return res == MF_RECOVERED ? 0 : -EBUSY; } else { action_result(pfn, MF_MSG_KERNEL_HIGH_ORDER, MF_IGNORED); + mutex_unlock(&mf_mutex); return -EBUSY; } } @@ -1473,6 +1483,7 @@ int memory_failure(unsigned long pfn, int flags) if (PageTransHuge(hpage)) { if (try_to_split_thp_page(p, "Memory Failure") < 0) { action_result(pfn, MF_MSG_UNSPLIT_THP, MF_IGNORED); + mutex_unlock(&mf_mutex); return -EBUSY; } VM_BUG_ON_PAGE(!page_count(p), p); @@ -1517,6 +1528,7 @@ int memory_failure(unsigned long pfn, int flags) num_poisoned_pages_dec(); unlock_page(p); put_page(p); + mutex_unlock(&mf_mutex); return 0; } if (hwpoison_filter(p)) { @@ -1524,6 +1536,7 @@ int memory_failure(unsigned long pfn, int flags) num_poisoned_pages_dec(); unlock_page(p); put_page(p); + mutex_unlock(&mf_mutex); return 0; } @@ -1559,6 +1572,8 @@ int memory_failure(unsigned long pfn, int flags) res = identify_page_state(pfn, p, page_flags); out: unlock_page(p); +out2: + mutex_unlock(&mf_mutex); return res; } EXPORT_SYMBOL_GPL(memory_failure); -- 2.25.1