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=-0.6 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no 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 C0EC1C4332D for ; Thu, 19 Mar 2020 10:49:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4EB1F20663 for ; Thu, 19 Mar 2020 10:49:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=shutemov-name.20150623.gappssmtp.com header.i=@shutemov-name.20150623.gappssmtp.com header.b="V5D/Uhrq" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4EB1F20663 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AB6756B0003; Thu, 19 Mar 2020 06:49:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A67D76B0005; Thu, 19 Mar 2020 06:49:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9565A6B0006; Thu, 19 Mar 2020 06:49:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0085.hostedemail.com [216.40.44.85]) by kanga.kvack.org (Postfix) with ESMTP id 7A6A06B0003 for ; Thu, 19 Mar 2020 06:49:40 -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 425748248076 for ; Thu, 19 Mar 2020 10:49:40 +0000 (UTC) X-FDA: 76611790920.11.taste38_8e970e94e3429 X-HE-Tag: taste38_8e970e94e3429 X-Filterd-Recvd-Size: 7175 Received: from mail-lf1-f67.google.com (mail-lf1-f67.google.com [209.85.167.67]) by imf42.hostedemail.com (Postfix) with ESMTP for ; Thu, 19 Mar 2020 10:49:39 +0000 (UTC) Received: by mail-lf1-f67.google.com with SMTP id y2so1187934lfe.11 for ; Thu, 19 Mar 2020 03:49:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=ZgArXkgg7VigGgHtZdS83FecIAuksnVOCfY2x1G6swc=; b=V5D/UhrqH9nir4DZuCT4o+58QLB+3H1nrQQixA2gKjXeJQzY/DIorUMc+vaBDVHV+b ryXYXpjG4vmEFYYYVE86zFyWPPDfMJ1/UJs4LX1RyF5SxpmYiyWnpw4jdRQ4RNCpWtOW AqDpFTwvbuDmJqhpigmd006fEeUkdpT5qdSk+PTxjVTS3q3FqIrkRr2xviFbcRG2Paii p4U9dRJVnt6g6FZF2CHdd1XO0ZY6koh4VqQNShVdEj+gFtsBEwSW9oPwO9yTJ2R0uMx2 cy/PBB9FwpVNfe4TKE9IQ9Y5lOF6fTBto1YnVIzWkBUxXfE8BTjaZkIcAF7XYQ22I8tg f6tg== 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=ZgArXkgg7VigGgHtZdS83FecIAuksnVOCfY2x1G6swc=; b=fijhHZ20U4WQQvHO188ffxk1CpE2bnU2NVRxvcpcN0eUasNsrcBGtIwPmCOPBAn+d7 8MEjBm2CN4x0YAuUmwOPN13aqej6zjDXjW/MWaCcTqGPpfDURBTFQ/o+kMl0dM2s6c/X dpprlMA2LYnJyc8FBsb69HeGMCWvRqMyj9FgYKrxrUuRp5h7RJYd6dOHCzX0tsC1WYHI iUmDKY/cnfsUpw6dty2IP80a2zN/L2Kp1MvaFrhI6+p1yw5fod3m3misa3/16SerY4Lx ntvCL/dUN0qjCRjbUSzIy/J2I5Hif8aSVtkFEBs2Mkltogj2AtMWQ7yq1vUZ8+WpZsvi UbTg== X-Gm-Message-State: ANhLgQ1a3NIN/SRHjzASCzaMm5gwwm1oSFQDYbZo1ibH1PPZ4JTS9MoA ZvGkptwbAUMncfoBiEAAUAf+gg== X-Google-Smtp-Source: ADFU+vsfw8i29iF8nIn7p+4M7LH5qStagxnbxJgTlPmOqZ1DYd2bXsG3komKYQucbAMOFr45pqpK6A== X-Received: by 2002:ac2:5f58:: with SMTP id 24mr1748538lfz.81.1584614977988; Thu, 19 Mar 2020 03:49:37 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id e9sm1167045ljp.24.2020.03.19.03.49.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Mar 2020 03:49:37 -0700 (PDT) Received: by box.localdomain (Postfix, from userid 1000) id D3BCE100D62; Thu, 19 Mar 2020 13:49:38 +0300 (+03) Date: Thu, 19 Mar 2020 13:49:38 +0300 From: "Kirill A. Shutemov" To: Yang Shi Cc: kirill.shutemov@linux.intel.com, hughd@google.com, aarcange@redhat.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm: khugepaged: fix potential page state corruption Message-ID: <20200319104938.vphyajoyz6ob6jtl@box> References: <1584573582-116702-1-git-send-email-yang.shi@linux.alibaba.com> <20200319001258.creziw6ffw4jvwl3@box> <2cdc734c-c222-4b9d-9114-1762b29dafb4@linux.alibaba.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: 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: On Wed, Mar 18, 2020 at 10:39:21PM -0700, Yang Shi wrote: >=20 >=20 > On 3/18/20 5:55 PM, Yang Shi wrote: > >=20 > >=20 > > On 3/18/20 5:12 PM, Kirill A. Shutemov wrote: > > > On Thu, Mar 19, 2020 at 07:19:42AM +0800, Yang Shi wrote: > > > > When khugepaged collapses anonymous pages, the base pages would > > > > be freed > > > > via pagevec or free_page_and_swap_cache().=A0 But, the anonymous = page may > > > > be added back to LRU, then it might result in the below race: > > > >=20 > > > > =A0=A0=A0=A0CPU A=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 CP= U B > > > > khugepaged: > > > > =A0=A0 unlock page > > > > =A0=A0 putback_lru_page > > > > =A0=A0=A0=A0 add to lru > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 page reclaim: > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 isolate this = page > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 try_to_unmap > > > > =A0=A0 page_remove_rmap <-- corrupt _mapcount > > > >=20 > > > > It looks nothing would prevent the pages from isolating by reclai= mer. > > > Hm. Why should it? > > >=20 > > > try_to_unmap() doesn't exclude parallel page unmapping. _mapcount i= s > > > protected by ptl. And this particular _mapcount pin is reachable fo= r > > > reclaim as it's not part of usual page table tree. Basically > > > try_to_unmap() will never succeeds until we give up the _mapcount o= n > > > khugepaged side. > >=20 > > I don't quite get. What does "not part of usual page table tree" mean= s? > >=20 > > How's about try_to_unmap() acquires ptl before khugepaged? The page table we are dealing with was detached from the process' page table tree: see pmdp_collapse_flush(). try_to_unmap() will not see the pte. try_to_unmap() can only reach the ptl if split ptl is disabled (mm->page_table_lock is used), but it still will not be able to reach pte= . > > >=20 > > > I don't see the issue right away. > > >=20 > > > > The other problem is the page's active or unevictable flag might = be > > > > still set when freeing the page via free_page_and_swap_cache(). > > > So what? > >=20 > > The flags may leak to page free path then kernel may complain if > > DEBUG_VM is set. Could you elaborate on what codepath you are talking about? > > > > The putback_lru_page() would not clear those two flags if the pag= es are > > > > released via pagevec, it sounds nothing prevents from isolating a= ctive >=20 > Sorry, this is a typo. If the page is freed via pagevec, active and > unevictable flag would get cleared before freeing by page_off_lru(). >=20 > But, if the page is freed by free_page_and_swap_cache(), these two flag= s are > not cleared. But, it seems this path is hit rare, the pages are freed b= y > pagevec for the most cases. >=20 > > > > or unevictable pages. > > > Again, why should it? vmscan is equipped to deal with this. > >=20 > > I don't mean vmscan, I mean khugepaged may isolate active and > > unevictable pages since it just simply walks page table. Why it is wrong? lru_cache_add() only complains if both flags set, it shouldn't happen. > > > > However I didn't really run into these problems, just in theory > > > > by visual > > > > inspection. > > > >=20 > > > > And, it also seems unnecessary to have the pages add back to LRU > > > > again since > > > > they are about to be freed when reaching this point.=A0 So, > > > > clearing active > > > > and unevictable flags, unlocking and dropping refcount from isola= te > > > > instead of calling putback_lru_page() as what page cache collapse= does. > > > Hm? But we do call putback_lru_page() on the way out. I do not foll= ow. > >=20 > > It just calls putback_lru_page() at error path, not success path. > > Putting pages back to lru on error path definitely makes sense. Here = it > > is the success path. I agree that putting the apage on LRU just before free the page is suboptimal, but I don't see it as a critical issue. --=20 Kirill A. Shutemov