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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E3949C7619A for ; Wed, 5 Apr 2023 18:25:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 63E926B0071; Wed, 5 Apr 2023 14:25:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EF9F6B0074; Wed, 5 Apr 2023 14:25:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4905F6B0075; Wed, 5 Apr 2023 14:25:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 36C1A6B0071 for ; Wed, 5 Apr 2023 14:25:40 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0FB83160820 for ; Wed, 5 Apr 2023 18:25:40 +0000 (UTC) X-FDA: 80648165640.07.25EF961 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf30.hostedemail.com (Postfix) with ESMTP id 3DB7B8000B for ; Wed, 5 Apr 2023 18:25:37 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=b0wEGUnl; spf=pass (imf30.hostedemail.com: domain of shy828301@gmail.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680719137; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iCptc+kA5zXDdmAuA5of1pjj6ECPBD9EyosUWG9DOXY=; b=jjY8BzCbuR3nIK0Du+MFJS7UUYSe0iZaSqOvOCe7y/xxzPwgNBDb2Km7qLchXdF/RrtASG 4vE4/xV+8DKGUyBXi3ffoATTRTfS9h/yBgazWIGvSE9Thi8LnQTDuVjMjMbzufSI3PDbJd X8CYWryrqKrJ2u9v23E24kkEB3NcDn0= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=b0wEGUnl; spf=pass (imf30.hostedemail.com: domain of shy828301@gmail.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680719137; a=rsa-sha256; cv=none; b=GfpyVcPSwNv/Qaoti/ZD6ACSC9RV1e5BCHmCbVF1rp+5yxIqfy9+ivCKY9wbAOXQgbzG4M DMnPGRWjaW1v+5AiD9MNeS56eXKg36feRSSedu1qK5XtkGT+GhccUahEd/1Nede1wUqBui RrQnEwXwE4huW2+YgxgiuwiVl+gHHRw= Received: by mail-pf1-f177.google.com with SMTP id bm13so10139882pfb.5 for ; Wed, 05 Apr 2023 11:25:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680719136; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=iCptc+kA5zXDdmAuA5of1pjj6ECPBD9EyosUWG9DOXY=; b=b0wEGUnlTwRu184txYCFSgU2+1bYA32X8C5SLFPd+pjOQ2zXGpQxoat+6txV4sYjCm cL7gvZagcwGc+pjq2zl2c0aNLW5CJZdYMhU+25BnYZhFxT7+Waako8RpTlAFUaZHlP86 JHYKZMqcULWxbGSVQziwbxkFOLSqIAkYqURZ+1nt4Ad5TRFe9N+fTSgOtVO6AuRCtCvb 42mmmtEKBsoe2QTXn215aqHmlo4ThRkTNco3b17whfWNwPWTuYO3AJxqjlaYJsZ4ivFq D6e8C5I9wuQmNJNLYy3/HuTJ/S9PG46kFbAHWTUukbRb4zblNUYsbZ0XCJE4Tl6GPY1K aLyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680719136; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iCptc+kA5zXDdmAuA5of1pjj6ECPBD9EyosUWG9DOXY=; b=7jogt1Onofs8E8sX1xhqP0dLIxWf8vDpipre44iluDaafcuDwCcfxLjf5HCKJqzNw/ vxUfe5f8Cp6h6S2UfyBEe3+Jz2WVfAnjDM1NuPrAyPDYC/udVqrz2A7YhmLnNFcm+26n 5S8BVGRWbJ8WUUN3uL5RPcRQ3T29UfXTKJldcl1NEXgKGAqRgAZmZ2FyLFEqbr1TFWCF ekqjdGTjzBwUg0i8eRX62/mizuxvAkZt7Mv6tm9/QANmkMStiHRrv6vtUet3muFAdOLy FLeJOH6vYYkjRnImJcj8CzpXQW5PahpG7lwNbyzBvOEc4qdvOyr/3Xeoa3B46EvK1Hpw Z/RQ== X-Gm-Message-State: AAQBX9e3M6JUYQGWZ9AqsS44GAqzfCene04OGeyc1vkUtMsQupt93PmD GksloVYBEv3eoKa8I5EN/uL53gY+2YgFEcxWj08= X-Google-Smtp-Source: AKy350b2Wbwyv1L6f98Vzq50lFWkxGC/rY4GOuWs7FtED4TSHS3YhFkk1MiIL8vg5A5HXY/zuoZ7yyYU0CHr6Hvz1hw= X-Received: by 2002:a05:6a00:1783:b0:62b:e52e:1bb with SMTP id s3-20020a056a00178300b0062be52e01bbmr3905606pfg.0.1680719135951; Wed, 05 Apr 2023 11:25:35 -0700 (PDT) MIME-Version: 1.0 References: <20230405155120.3608140-1-peterx@redhat.com> In-Reply-To: From: Yang Shi Date: Wed, 5 Apr 2023 11:25:24 -0700 Message-ID: Subject: Re: [PATCH] mm/khugepaged: Check again on anon uffd-wp during isolation To: Peter Xu Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Axel Rasmussen , Nadav Amit , David Hildenbrand , Andrew Morton , Andrea Arcangeli , Mike Rapoport , linux-stable Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3DB7B8000B X-Stat-Signature: ms8w91s551jk5soigdm5ooars1faieen X-HE-Tag: 1680719137-958443 X-HE-Meta: U2FsdGVkX1/X4zDicCzt/tNsD5ZOqEl4c7AtGx6Topy2eF7Lk1AKbtmA6zqZJ3OiIu0cL5UzRxsLlqP1a8/Yf3i+UrLi9omBaFotZwRgVcUViL1eHk+A9gqpvsEgbnexKXMgBO22cetwSZygjSNCw6r9yKmDg8x1t88vzfhvx3KO8knmgAJ1qYa/LaPpZageXVqGIj5+fxnpseFri80aPJAVwgmIe/mElNzV4PkzW4VyV9nLyCNq2gwncQnZcQDPzUseo4ps/702pDbcYHfMmohf2Ew2CTrjNWHZJdFBX2BKqnga6WXqfMP2MFabQMzVxrNi/8TGr79l9IKJjGqAD+3c9XC4605tXrpamzftoZDA3o0h9Ws0k7HxZEdqzC/r3dskI2Tz4Nuq1WKpOeQ3STKNZEdeQ9XlDbBx1vRXMb+rdhdMPS2V3Yxx7uCLEpf1yykudLzDfiVhyDfyI1Ub2doKW0Q7nt8NCrsPnIQMicE6N1uDe0/8sB69ID2y8lWxuLa7hnn9IAn4WdIQeDv6+XIT9qoK1jrPm5Gj0mmbWTyPk6aZASfoED9cw5kVYOA79O4tustcsxD66JDp5r6OADuDTwJ6nwzTCBLB/KWQV0EHjQwfgYxZ5O/EFIWl+yb7Y94PQ9VlPYzroxqWUzZTuQi1DN+S7crT1SaJN1q7P3OZT3odqhFvCZjtN1Szcajtf7U+00SqRuQV0fpV57HsU04eWA6lKG+Z6b6VMtwW6tqqoSYfZt/HDonJyKYe7/r4unsb1mDWYZR43N97we+r9B+SQ6y0xuBmUJxaEGJPJnuJFBD9AACw895cxlq5PrbEKOqIDys6IJD3qGoofUbeQBILo1/8qvsTUzGR4Jhv6Sp9KcOM15nYKclf7GrrYhKkJ6F3GOcRUPAKJ+J6NY4hEqecU/sRI8RcWr0921RKIp/AP2Q4xXf4eYsxBeGouHLZFqXP3sVpnwa3Fs+OZxq dKEXIbh2 jhCaVErFfshiCqpaFihDnjTlr2Myb9ECf5wRuKbXtR1gF4n5ClhJdHam8rGHHspvlianE3AKeBdDrWaCSVkGTvjrj5uUBaXgX14KNetz0B1UrhTjORFaQzFvsjijSvvageNJwF66usasocWmZ01Z0uCt5Oid4dvZTX6lJCYfTTplFjiYz8jPUIgPSLwQmBsTuSKB93K3AaTLou4FOZ8siCACT7VSjp7ekESMI9x7q/Wh0UijpyCpaJNd++4NHly39gXiZqx70+tm51TXYHWYzN+rKYTwP7si5synMPyQZWcCrVnuyryrlV/IyyHh/doqHMu2/iUQv13m/Xvhd5FwoxVUd9SRIGghzFhVzjH6qsTW1hMAISlwmnfjt9Y2DzendMDzcrEeroBwzOqlncpwO9mwQLDFALCh+ZbVnI6eOwxDU5Whmp4RcGwZTzEynQFUz/btwLYSZ9kOdvP2NKL8AupW/U2mBZLSxiCL9fpWaEG2FFCo= 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 5, 2023 at 11:10=E2=80=AFAM Peter Xu wrote: > > On Wed, Apr 05, 2023 at 09:59:15AM -0700, Yang Shi wrote: > > On Wed, Apr 5, 2023 at 8:51=E2=80=AFAM Peter Xu wro= te: > > > > > > Khugepaged collapse an anonymous thp in two rounds of scans. The 2nd= round > > > done in __collapse_huge_page_isolate() after hpage_collapse_scan_pmd(= ), > > > during which all the locks will be released temporarily. It means the > > > pgtable can change during this phase before 2nd round starts. > > > > > > It's logically possible some ptes got wr-protected during this phase,= and > > > we can errornously collapse a thp without noticing some ptes are > > > wr-protected by userfault. e1e267c7928f wanted to avoid it but it on= ly did > > > that for the 1st phase, not the 2nd phase. > > > > > > Since __collapse_huge_page_isolate() happens after a round of small p= age > > > swapins, we don't need to worry on any !present ptes - if it existed > > > khugepaged will already bail out. So we only need to check present p= tes > > > with uffd-wp bit set there. > > > > > > This is something I found only but never had a reproducer, I thought = it was > > > one caused a bug in Muhammad's recent pagemap new ioctl work, but it = turns > > > out it's not the cause of that but an userspace bug. However this se= ems to > > > still be a real bug even with a very small race window, still worth t= o have > > > it fixed and copy stable. > > > > Yeah, I agree. But I got confused by userfaultfd_wp(vma) and > > pte_uffd_wp(pte). If a vma is armed with uffd wp, shall we skip the > > whole vma? If so, whether it is better to just check vma? We do > > revalidate vma once reacquiring mmap_lock, so we should be able to > > bail out earlier. > > Checking against VMA is safe too, the difference is current code still > allows thp to be collapsed as long as none of the page is explicitly > protected over the thp range, even if the range is registered with > userfault-wp. That's also what e1e267c7928f does. > > Here we have slightly different handling between anon / file thps (file > thps checks against the vma flags), IMHO mostly because we don't scan > pgtables when making decisions to collapse a shmem thp, so we made it > simple by checking against vma flags. We can make it the same as anon bu= t > it might be an overkill just to scan the entries for uffd-wp purpose. > > For anon we always scans the pgtable anyway so it's easier to make a more > accurate decision. Aha, I see. It does resolve my confusion. Thanks for elaborating. The patch looks good to me. Reviewed-by: Yang Shi > > Thanks, > > -- > Peter Xu >