From patchwork Fri May 21 03:01:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naoya Horiguchi X-Patchwork-Id: 12271791 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,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 D299BC433ED for ; Fri, 21 May 2021 03:02:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6877F6108D for ; Fri, 21 May 2021 03:02:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6877F6108D 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 086658D001C; Thu, 20 May 2021 23:02:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 05F7D8D0001; Thu, 20 May 2021 23:02:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E41728D001C; Thu, 20 May 2021 23:02:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id A9F988D0001 for ; Thu, 20 May 2021 23:02:08 -0400 (EDT) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 4B74E8248068 for ; Fri, 21 May 2021 03:02:08 +0000 (UTC) X-FDA: 78163739136.11.F10BC79 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf17.hostedemail.com (Postfix) with ESMTP id E378C40B8CE8 for ; Fri, 21 May 2021 03:02:06 +0000 (UTC) Received: by mail-pl1-f182.google.com with SMTP id e15so3576805plh.1 for ; Thu, 20 May 2021 20:02:07 -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=Gmn+77caXUMJ4caoqVqQUZ1bs4QzEShm2/YkSqe/I/E=; b=gBprfNO0r40StCr1EKtjQyFNmJ/3h0TWADQlG9H2814dyqK6YYA0B+jBSv/lpDJajG uR/ACB82FwsWQN+4K8gaoIBAazyMtsRsXADQpQE2msBBJlrJM3ukzxxDPAJV1RXrMjim FqY6oJ+EGHKlkhbOzgo1LGnQuRW1N5Ez0pNXdqnO0ZHVHmpGN/qDPfrfdfhjkvpjQM+i YOw7hITzdED2GnCiPuBqhpgIuzjeVV9nYZuP0Gr3m3GSRnWPUo15rsYzKX7+o223L8Y8 Cm62D8GQx8wH6fzZoMlQTdh786b/dLIfxFRwHJPYmhlfmsFSoHmU/xNF1RSm4G52WK7t BzrA== 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=Gmn+77caXUMJ4caoqVqQUZ1bs4QzEShm2/YkSqe/I/E=; b=NgYpPSyrMczOwqPyM5Ld4P/o79me2ISPhQ3PbW0T5qWw9yX+PbGUmBuEgrCV0dx1uZ XiEuyHjsGXBB5X3Rc1pKmDZEsPQhvB/+hrd1wR9qR71YGUyvrr+fi7ei5uLCmxNQhnUn D59dkk4kXyoCeIgJpa7LBmgbPGm8w9LRf88pqXdQh8/iM+sgjGPEwDFIH9xCoLQSZ7gJ AlZEXG7uv24I4IO+c6u+xW4jLhMSm1fNKxGYBCQ6jmCVvQ9w/+xxEX3xLKeYqjUE5ZME M/ItujzOfacHTDngBku7kY5IPbm5T1Ty4xOHdgz3q7lyx06XYTrttotVbRQYueFHpK+A LooQ== X-Gm-Message-State: AOAM533dQbmu3iMWkPXCLOAp5OAliLEUvvPbBoshYQEmXLxALs6ajsGq 8VzOW3jLf79EQzPKgIwMPkWt+ZiCvJtF9cQ= X-Google-Smtp-Source: ABdhPJxRtjfTUESVH4jVOuiT/Er6wTL+7Qo724pplulYgttBkTDucJcWrjra9CjvWSPfrPRJmTASLQ== X-Received: by 2002:a17:90a:29c4:: with SMTP id h62mr8613595pjd.177.1621566126847; Thu, 20 May 2021 20:02:06 -0700 (PDT) Received: from localhost.localdomain (h175-177-040-153.catv02.itscom.jp. [175.177.40.153]) by smtp.gmail.com with ESMTPSA id 191sm2959677pfx.121.2021.05.20.20.02.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 May 2021 20:02:06 -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 v5 1/3] mm/memory-failure: Use a mutex to avoid memory_failure() races Date: Fri, 21 May 2021 12:01:54 +0900 Message-Id: <20210521030156.2612074-2-nao.horiguchi@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210521030156.2612074-1-nao.horiguchi@gmail.com> References: <20210521030156.2612074-1-nao.horiguchi@gmail.com> MIME-Version: 1.0 Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=gBprfNO0; spf=pass (imf17.hostedemail.com: domain of naohoriguchi@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=naohoriguchi@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: E378C40B8CE8 X-Stat-Signature: a5pbneoqsk1o1gdj87nwt85mz7aft7w1 X-HE-Tag: 1621566126-571038 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 Reviewed-by: Borislav Petkov Reviewed-by: Oscar Salvador --- mm/memory-failure.c | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git v5.13-rc2/mm/memory-failure.c v5.13-rc2_patched/mm/memory-failure.c index 9a7c12ace9e2..0f0b932ccbca 100644 --- v5.13-rc2/mm/memory-failure.c +++ v5.13-rc2_patched/mm/memory-failure.c @@ -1400,6 +1400,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 @@ -1423,7 +1425,7 @@ int memory_failure(unsigned long pfn, int flags) struct page *hpage; struct page *orig_head; struct dev_pagemap *pgmap; - int res; + int res = 0; unsigned long page_flags; bool retry = true; @@ -1443,13 +1445,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 unlock_mutex; + } + if (TestSetPageHWPoison(p)) { pr_err("Memory failure: %#lx: already hardware poisoned\n", pfn); - return 0; + goto unlock_mutex; } orig_head = hpage = compound_head(p); @@ -1482,17 +1489,19 @@ int memory_failure(unsigned long pfn, int flags) res = MF_FAILED; } action_result(pfn, MF_MSG_BUDDY, res); - return res == MF_RECOVERED ? 0 : -EBUSY; + res = res == MF_RECOVERED ? 0 : -EBUSY; } else { action_result(pfn, MF_MSG_KERNEL_HIGH_ORDER, MF_IGNORED); - return -EBUSY; + res = -EBUSY; } + goto unlock_mutex; } 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 = -EBUSY; + goto unlock_mutex; } VM_BUG_ON_PAGE(!page_count(p), p); } @@ -1516,7 +1525,7 @@ int memory_failure(unsigned long pfn, int flags) if (PageCompound(p) && compound_head(p) != orig_head) { action_result(pfn, MF_MSG_DIFFERENT_COMPOUND, MF_IGNORED); res = -EBUSY; - goto out; + goto unlock_page; } /* @@ -1536,14 +1545,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; } if (!PageTransTail(p) && !PageLRU(p)) @@ -1562,7 +1571,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 = -EBUSY; - goto out; + goto unlock_page; } /* @@ -1571,13 +1580,15 @@ int memory_failure(unsigned long pfn, int flags) if (PageLRU(p) && !PageSwapCache(p) && p->mapping == NULL) { action_result(pfn, MF_MSG_TRUNCATED_LRU, MF_IGNORED); res = -EBUSY; - goto out; + goto unlock_page; } identify_page_state: res = 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);