From patchwork Tue Sep 14 18:37:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 12494423 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 A690DC433FE for ; Tue, 14 Sep 2021 18:37:37 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2922961211 for ; Tue, 14 Sep 2021 18:37:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2922961211 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id B40126B0073; Tue, 14 Sep 2021 14:37:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A0512900003; Tue, 14 Sep 2021 14:37:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 85997900002; Tue, 14 Sep 2021 14:37:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0121.hostedemail.com [216.40.44.121]) by kanga.kvack.org (Postfix) with ESMTP id 6DE036B0073 for ; Tue, 14 Sep 2021 14:37:36 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 290D118297704 for ; Tue, 14 Sep 2021 18:37:36 +0000 (UTC) X-FDA: 78587037312.25.06F0B47 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) by imf03.hostedemail.com (Postfix) with ESMTP id DBF3E3000099 for ; Tue, 14 Sep 2021 18:37:35 +0000 (UTC) Received: by mail-pg1-f176.google.com with SMTP id g184so44293pgc.6 for ; Tue, 14 Sep 2021 11:37:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yprth0nsSrttMIWYc9OKOWe+bft4LdAzLPFBmBWUobY=; b=kvvcClInUIG9SRN8aa+z1bR/Pc+UNmTjmjVVAtD2xIqPk47pROSY/f0CSvTRGyYci1 NuEppMVsjGtD/RELsrt0o1bMAdlrYJOnF8+uO285BnIi7c6TeVSibj/kB05I1evFrmuo qwaTYKmipY7nIfdjaRVcfxwJ/y+tlLJrv4ntSajPKY9pj1MKVEzhsUeD0nn2BlGJzAFL Yt5PB0KqXOUi1opqLZwVQ9iHL5M3wLIdiQCJKdJ/bFxTbCDnwG6ParQRC7AchD/mBeDw 8BQoPQU1iQltx438xfOnGjqq8dQ3V5m5PZxMHacSKO/d/D4DZKUqeQ0/AZZuwVxNr1Zv yBjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yprth0nsSrttMIWYc9OKOWe+bft4LdAzLPFBmBWUobY=; b=1siqhido5EhLVQAyDlZUeMaQG/C0XK+ecPFDux7LruZ1gu8OQ18wzVUHqEzK7sFMCv sSudC+BuL4YfixooSoqhnTRBJDXUJFAcGXtV8gciisgYJV7stSI69QJmPZDxmc2kLoti 2Rcu3hgD63T30YNEs8efgTpKG7NL/2qTWJvXNsEWxgHm6GQ/OBlKflewjDJ3Y2d2UVNf ndyMRrb0Eglw+cGAXdVkHGPP88GPan4bjHJrgE1GqzxluUijsf1+2PXDUoqMMfkAhqfA aWNiRx5zhqpTIp27IggkySDi6jR0kK0guzslK0Tvffr/rX7ynzCNiIVLhK8EE0/GI9D3 nywQ== X-Gm-Message-State: AOAM532SH/zSoShxpwPMuI1LZsveWos0iIvVWWJ2kFalHHsXFJvlrFwj 15OdeHABj62svtrV7VG7FEc= X-Google-Smtp-Source: ABdhPJzG/S59EV7n4OKhsdYA4k/qedVimHWvPxBnASOQFFBJgdUHomIzqVIxDQbzmk/JcfRKhIQ6iw== X-Received: by 2002:a62:dd83:0:b029:2e8:e511:c32f with SMTP id w125-20020a62dd830000b02902e8e511c32fmr6077555pff.49.1631644655029; Tue, 14 Sep 2021 11:37:35 -0700 (PDT) Received: from localhost.localdomain (c-73-93-239-127.hsd1.ca.comcast.net. [73.93.239.127]) by smtp.gmail.com with ESMTPSA id y3sm12003965pge.44.2021.09.14.11.37.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 11:37:34 -0700 (PDT) From: Yang Shi To: naoya.horiguchi@nec.com, hughd@google.com, kirill.shutemov@linux.intel.com, willy@infradead.org, osalvador@suse.de, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/4] mm: khugepaged: check if file page is on LRU after locking page Date: Tue, 14 Sep 2021 11:37:16 -0700 Message-Id: <20210914183718.4236-3-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210914183718.4236-1-shy828301@gmail.com> References: <20210914183718.4236-1-shy828301@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: DBF3E3000099 X-Stat-Signature: 9798zip6uu3brxfjx5w3torwu1t9k5ce Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=kvvcClIn; spf=pass (imf03.hostedemail.com: domain of shy828301@gmail.com designates 209.85.215.176 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1631644655-615203 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: The khugepaged does check if the page is on LRU or not but it doesn't hold page lock. And it doesn't check this again after holding page lock. So it may race with some others, e.g. reclaimer, migration, etc. All of them isolates page from LRU then lock the page then do something. But it could pass the refcount check done by khugepaged to proceed collapse. Typically such race is not fatal. But if the page has been isolated from LRU before khugepaged it likely means the page may be not suitable for collapse for now. The other more fatal case is the following patch will keep the poisoned page in page cache for shmem, so khugepaged may collapse a poisoned page since the refcount check could pass. 3 refcounts come from: - hwpoison - page cache - khugepaged Since it is not on LRU so no refcount is incremented from LRU isolation. This is definitely not expected. Checking if it is on LRU or not after holding page lock could help serialize against hwpoison handler. But there is still a small race window between setting hwpoison flag and bump refcount in hwpoison handler. It could be closed by checking hwpoison flag in khugepaged, however this race seems unlikely to happen in real life workload. So just check LRU flag for now to avoid over-engineering. Signed-off-by: Yang Shi --- mm/khugepaged.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 045cc579f724..bdc161dc27dc 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1808,6 +1808,12 @@ static void collapse_file(struct mm_struct *mm, goto out_unlock; } + /* The hwpoisoned page is off LRU but in page cache */ + if (!PageLRU(page)) { + result = SCAN_PAGE_LRU; + goto out_unlock; + } + if (isolate_lru_page(page)) { result = SCAN_DEL_PAGE_LRU; goto out_unlock;