From patchwork Thu May 6 01:31:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Naoya Horiguchi X-Patchwork-Id: 12241435 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=-10.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 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 67D5DC433B4 for ; Thu, 6 May 2021 01:31:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C412D6139A for ; Thu, 6 May 2021 01:31:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C412D6139A 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 4E3406B006C; Wed, 5 May 2021 21:31:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 493C46B006E; Wed, 5 May 2021 21:31:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 30CBA6B0070; Wed, 5 May 2021 21:31:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0220.hostedemail.com [216.40.44.220]) by kanga.kvack.org (Postfix) with ESMTP id 12A3C6B006C for ; Wed, 5 May 2021 21:31:28 -0400 (EDT) Received: from smtpin38.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id C2E99180AD830 for ; Thu, 6 May 2021 01:31:27 +0000 (UTC) X-FDA: 78109078614.38.0C5E5E3 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf23.hostedemail.com (Postfix) with ESMTP id A69C5A0003AF for ; Thu, 6 May 2021 01:31:19 +0000 (UTC) Received: by mail-pl1-f174.google.com with SMTP id a11so2505404plh.3 for ; Wed, 05 May 2021 18:31:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=JqBOzORC8TCN+TAvEv5mh2kPfbvuSNtE/HHJhl6GmcY=; b=pQp18OSOYI9c0F5d+qtvUO9Xq7cnQ1nVHDgXxhXa01MQrzUkF1MRziKdmJGECRvGJV /TmbXl+Z+GGyNaZ8XVCCZiWI3AG/9PoOROehVrnEJxnN6Ijvx8FkTzZbmDZbhSb7MOts ZYb1m2b1ai0DS2t4zdmqPO5J2vws4HW5HLimHVYUqn7Hh0vz5GXePAeskXDDDWNCfHzg UxAwPptWFFvpA24KgV6ke5sujxaI7Mcs+NLg+vpKOGKLarLw9++hCDkJ698DreVQJSOs U6OrGOmjVLXlvYWNOLsjIileCweVtcvDm2NOTKuh475tjlpkUUQOkiuHakeDzK2M/jtM 6xsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=JqBOzORC8TCN+TAvEv5mh2kPfbvuSNtE/HHJhl6GmcY=; b=pdjfibyyYB2yUi4nHNfKD9Ceej2lQhlYzrJWiDYKxTlbpS9P4s214591wVvAY6EYPZ Oy5lKEKfqq3nHrUGMooJqq1hfwLu57N/FbTAEQfeKMvYSVs184bBQSNSaxuUxmcuLHz7 2CdaqKQnfLAudMHCDNN6S56nJ+iEGDxcW0F+hZ7VoBtwysUNSEoT/Q3PAZFJTsKG2c4U mv97G5ux6YCUlZserAy7LEBmPsX9pRMDzD02uppw6G+o4WRbACPigEpcWtEOpP7fsLru 4G1syCRUrmNfR7vmDJtVvMw/uTbl2sYM8KiYRFLv9YB6qrRLrV5/IGHkhAmNrCP2sVm8 v0ag== X-Gm-Message-State: AOAM531uKqv7KYly61FJa0HmMyK+CnqB2Fda8s+iJDuCQHnvPtnXAfpN /2UZ2gqxYivVAFhO/yAqDg== X-Google-Smtp-Source: ABdhPJzYPpQHgAokgwGqUEZIvxt+P6HAjlKLcIwbKMvD3pZDWr4ePaf2osbkv/QF6ieCF7hxIeoi+A== X-Received: by 2002:a17:903:3091:b029:ee:ef64:c389 with SMTP id u17-20020a1709033091b02900eeef64c389mr1810668plc.74.1620264686322; Wed, 05 May 2021 18:31:26 -0700 (PDT) Received: from u2004 (h175-177-040-153.catv02.itscom.jp. [175.177.40.153]) by smtp.gmail.com with ESMTPSA id z18sm405232pfa.39.2021.05.05.18.31.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 May 2021 18:31:25 -0700 (PDT) Date: Thu, 6 May 2021 10:31:22 +0900 From: Naoya Horiguchi To: Oscar Salvador Cc: Mike Kravetz , Michal Hocko , Muchun Song , "akpm@linux-foundation.org" , HORIGUCHI =?utf-8?b?TkFPWUEo5aCA5Y+j44CA55u05LmfKQ==?= , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" Subject: [PATCH v2] mm,hwpoison: fix race with compound page allocation Message-ID: <20210506013122.GA2240524@u2004> References: <20210423080153.GA78658@hori.linux.bs1.fc.nec.co.jp> <20210428074654.GA2093897@u2004> <20210428082344.GA29213@linux> <20210428091835.GA273940@hori.linux.bs1.fc.nec.co.jp> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210428091835.GA273940@hori.linux.bs1.fc.nec.co.jp> X-Rspamd-Queue-Id: A69C5A0003AF Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=pQp18OSO; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf23.hostedemail.com: domain of naohoriguchi@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=naohoriguchi@gmail.com X-Rspamd-Server: rspam04 X-Stat-Signature: jb8hy7xjxg9aymtegihzb9hh16yo5wi8 Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf23; identity=mailfrom; envelope-from=""; helo=mail-pl1-f174.google.com; client-ip=209.85.214.174 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620264679-223354 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 Wed, Apr 28, 2021 at 09:18:36AM +0000, HORIGUCHI NAOYA(堀口 直也) wrote: > On Wed, Apr 28, 2021 at 10:23:49AM +0200, Oscar Salvador wrote: > > On Wed, Apr 28, 2021 at 04:46:54PM +0900, Naoya Horiguchi wrote: > > > --- > > > From: Naoya Horiguchi > > > Date: Wed, 28 Apr 2021 15:55:47 +0900 > > > Subject: [PATCH] mm,hwpoison: fix race with compound page allocation > > > > > > When hugetlb page fault (under overcommiting situation) and memory_failure() > > > race, VM_BUG_ON_PAGE() is triggered by the following race: > > > > > > CPU0: CPU1: > > > > > > gather_surplus_pages() > > > page = alloc_surplus_huge_page() > > > memory_failure_hugetlb() > > > get_hwpoison_page(page) > > > __get_hwpoison_page(page) > > > get_page_unless_zero(page) > > > zero = put_page_testzero(page) > > > VM_BUG_ON_PAGE(!zero, page) > > > enqueue_huge_page(h, page) > > > put_page(page) > > > > > > __get_hwpoison_page() only checks page refcount before taking additional > > > one for memory error handling, which is wrong because there's time > > > windows where compound pages have non-zero refcount during initialization. > > > > > > So makes __get_hwpoison_page() check more page status for a few types > > > of compound pages. PageSlab() check is added because otherwise > > > "non anonymous thp" path is wrongly chosen for slab pages. > > > > Was it wrongly chosen even before? If so, maybe a Fix tag is warranted. > > OK, I'll check when this was introduced. > > > > > > > > > Signed-off-by: Naoya Horiguchi > > > Reported-by: Muchun Song > > > --- > > > mm/memory-failure.c | 48 +++++++++++++++++++++++++-------------------- > > > 1 file changed, 27 insertions(+), 21 deletions(-) > > > > > > diff --git a/mm/memory-failure.c b/mm/memory-failure.c > > > index a3659619d293..61988e332712 100644 > > > --- a/mm/memory-failure.c > > > +++ b/mm/memory-failure.c > > > @@ -1095,30 +1095,36 @@ static int __get_hwpoison_page(struct page *page) > > > > > + if (PageCompound(page)) { > > > + if (PageSlab(page)) { > > > + return get_page_unless_zero(page); > > > + } else if (PageHuge(head)) { > > > + if (HPageFreed(head) || HPageMigratable(head)) > > > + return get_page_unless_zero(head); > > > > There were concerns raised wrt. memory-failure should not be fiddling with page's > > refcount without holding a hugetlb lock. > > So, if we really want to make this more stable, we might want to hold the lock > > here. > > > > The clearing and setting of HPageFreed happens under the lock, and for HPageMigratable > > that is also true for the clearing part, so I think it would be more sane to do > > this under the lock to close any possible race. > > > > Does it make sense? > > Thanks, I'll update to do the check under hugetlb_lock. Hi, Let me share the update below. Two changes: - hold hugetlb_lock in hugetlb path, - added Fixes tag and cc to stable. I limited the stable branch only to 5.12+ due to the dependency on HPage* pseudo flags. - Naoya --- From: Naoya Horiguchi Date: Thu, 6 May 2021 09:54:39 +0900 Subject: [PATCH] mm,hwpoison: fix race with compound page allocation When hugetlb page fault (under overcommiting situation) and memory_failure() race, VM_BUG_ON_PAGE() is triggered by the following race: CPU0: CPU1: gather_surplus_pages() page = alloc_surplus_huge_page() memory_failure_hugetlb() get_hwpoison_page(page) __get_hwpoison_page(page) get_page_unless_zero(page) zero = put_page_testzero(page) VM_BUG_ON_PAGE(!zero, page) enqueue_huge_page(h, page) put_page(page) __get_hwpoison_page() only checks page refcount before taking additional one for memory error handling, which is wrong because there's time windows where compound pages have non-zero refcount during initialization. So makes __get_hwpoison_page() check more page status for a few types of compound pages. PageSlab() check is added because otherwise "non anonymous thp" path is wrongly chosen. Fixes: ead07f6a867b ("mm/memory-failure: introduce get_hwpoison_page() for consistent refcount handling") Signed-off-by: Naoya Horiguchi Reported-by: Muchun Song Cc: stable@vger.kernel.org # 5.12+ --- mm/memory-failure.c | 53 +++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index a3659619d293..966a1d6b0bc8 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1095,30 +1095,41 @@ static int __get_hwpoison_page(struct page *page) { struct page *head = compound_head(page); - if (!PageHuge(head) && PageTransHuge(head)) { - /* - * Non anonymous thp exists only in allocation/free time. We - * can't handle such a case correctly, so let's give it up. - * This should be better than triggering BUG_ON when kernel - * tries to touch the "partially handled" page. - */ - if (!PageAnon(head)) { - pr_err("Memory failure: %#lx: non anonymous thp\n", - page_to_pfn(page)); - return 0; + if (PageCompound(page)) { + if (PageSlab(page)) { + return get_page_unless_zero(page); + } else if (PageHuge(head)) { + int ret = 0; + + spin_lock(&hugetlb_lock); + if (HPageFreed(head) || HPageMigratable(head)) + ret = get_page_unless_zero(head); + spin_unlock(&hugetlb_lock); + return ret; + } else if (PageTransHuge(head)) { + /* + * Non anonymous thp exists only in allocation/free time. We + * can't handle such a case correctly, so let's give it up. + * This should be better than triggering BUG_ON when kernel + * tries to touch the "partially handled" page. + */ + if (!PageAnon(head)) { + pr_err("Memory failure: %#lx: non anonymous thp\n", + page_to_pfn(page)); + return 0; + } + if (get_page_unless_zero(head)) { + if (head == compound_head(page)) + return 1; + pr_info("Memory failure: %#lx cannot catch tail\n", + page_to_pfn(page)); + put_page(head); + } } + return 0; } - if (get_page_unless_zero(head)) { - if (head == compound_head(page)) - return 1; - - pr_info("Memory failure: %#lx cannot catch tail\n", - page_to_pfn(page)); - put_page(head); - } - - return 0; + return get_page_unless_zero(page); } /*