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 A6140EE49AE for ; Tue, 22 Aug 2023 18:54:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E00794001E; Tue, 22 Aug 2023 14:54:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2904E940007; Tue, 22 Aug 2023 14:54:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17E9994001E; Tue, 22 Aug 2023 14:54:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 09AA4940007 for ; Tue, 22 Aug 2023 14:54:48 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C7D48140444 for ; Tue, 22 Aug 2023 18:54:47 +0000 (UTC) X-FDA: 81152642214.28.0DE0C0C Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) by imf25.hostedemail.com (Postfix) with ESMTP id E642EA0023 for ; Tue, 22 Aug 2023 18:54:45 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=1YIoFqVN; spf=pass (imf25.hostedemail.com: domain of hughd@google.com designates 209.85.128.176 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692730485; a=rsa-sha256; cv=none; b=QO9ap8r45c1UzAh1Q+Y+lum2B7FbkCPHpBmLsOCust2Fsb1XxlLIeuy+ltYN6ov8/KE8kG qeTZ+S4Bjtt4MPoMSSBzbUkeg3EeUAFvSlih8diOSxl65lhCZ2jrOzQOyyrpSgNzA46ZtB GQmXINhGudUToeH6UyJVy2L12cbEkBk= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=1YIoFqVN; spf=pass (imf25.hostedemail.com: domain of hughd@google.com designates 209.85.128.176 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692730485; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=EC4FoWXo6SNQKwsXxt28KsctazZB0KZm2Dgg9LreMAM=; b=8bgWpVbZYQ3db02MajqSjxyNyWIjgsMk0tJxIklts2Gzu/ZkOQi5YZAR5khZuShuPPYT5+ ZLQD4ii1Oq6uYrqDppP8c6ok2qy4ziMhRGDU7pT/4pIoKcfOfjzXn/tfBEM6hAwJwH1l3J sqdZQX2NBsyelA5GCos2mQCcn4HKnWM= Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-59209b12c50so30417467b3.0 for ; Tue, 22 Aug 2023 11:54:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692730485; x=1693335285; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=EC4FoWXo6SNQKwsXxt28KsctazZB0KZm2Dgg9LreMAM=; b=1YIoFqVN55j3XEBWw5UYnqxvNjoz+koElbxuVIP/CJlJ0hkI9N8eMY39So2s6Zh8e0 lYOJwoMEQBCBGM9ObvuUJ43RLHkBTQhPIi4D2e21eC0WOzSMo3w6Pej51GXssjJncxsr 40V/Mu1pMG/D3T0D49sODOZIfKYGqNqFAaOkTMSHTKzO6xb16qy4PWhFgzJJXzDEWNDd syAPBovHWWRT8jV5/JwCsG9j6lWmyoq166pYQYj+5LjUZx2PNOAhbu+jAuVXXYMq6btV oWcKnK9p1XjuZsNocTtOJIfHWsLUDWxxgnOuAfTnpiEG8/nc/26eHsq/XlCM+fMpNl17 lGng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692730485; x=1693335285; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EC4FoWXo6SNQKwsXxt28KsctazZB0KZm2Dgg9LreMAM=; b=Z1V3h6zldhqeqOEOK6cAAofaYeC5Mape3S27mCUMUKV7fHnqe23in3dPVD8gpCEhUj v4cQp5vgb4CfKP2pX7wnUrsVer8yYHYAMVS1K9sMvNam2RQkOROnt7PTbDzDYgDN/QjY XPj35Xjdx8EbohcBQkw6N1zN3pmulb1NxacDLysrf6n5sBNYdhDw0k0RndYHUjdNWzHD EpHYiPriZWY37CFnloFK9Njh9ObVP46FPGt6HHgX5imXr51zI+mkJtVwgXqgpQoEdcHQ jAP5cc1tj5+igIt8lOQDjqfN4nqSOnsSjk51YZi5qIiZLSQ+4OxdIj/owXUSdRh++4PW PVeg== X-Gm-Message-State: AOJu0YxuQHFc+aKyQIYc6gTdE7XitbhI28fcUyDhUwbRR8AvnHnf6g+1 38q5r4D+9V5aynd13rKRrUcpVw== X-Google-Smtp-Source: AGHT+IFpHQvIMwN5WYlfI56GAofuwQT9IU67DR/OnesR0CbZe8XpbWpAMdSzZjh6aZoI7atOZrgvQQ== X-Received: by 2002:a81:4843:0:b0:583:2df2:35f3 with SMTP id v64-20020a814843000000b005832df235f3mr9917420ywa.1.1692730484669; Tue, 22 Aug 2023 11:54:44 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id r64-20020a815d43000000b00545a08184cesm2915469ywb.94.2023.08.22.11.54.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 11:54:44 -0700 (PDT) Date: Tue, 22 Aug 2023 11:54:39 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Jann Horn cc: Hugh Dickins , Andrew Morton , Mike Kravetz , Mike Rapoport , "Kirill A. Shutemov" , Matthew Wilcox , David Hildenbrand , Suren Baghdasaryan , Qi Zheng , Yang Shi , Mel Gorman , Peter Xu , Peter Zijlstra , Will Deacon , Yu Zhao , Alistair Popple , Ralph Campbell , Ira Weiny , Steven Price , SeongJae Park , Lorenzo Stoakes , Huang Ying , Naoya Horiguchi , Christophe Leroy , Zack Rusin , Jason Gunthorpe , Axel Rasmussen , Anshuman Khandual , Pasha Tatashin , Miaohe Lin , Minchan Kim , Christoph Hellwig , Song Liu , Thomas Hellstrom , Russell King , "David S. Miller" , Michael Ellerman , "Aneesh Kumar K.V" , Heiko Carstens , Christian Borntraeger , Claudio Imbrenda , Alexander Gordeev , Gerald Schaefer , Vasily Gorbik , Vishal Moola , Vlastimil Babka , Zi Yan , Zach O'Keefe , Linux ARM , sparclinux@vger.kernel.org, linuxppc-dev , linux-s390 , kernel list , Linux-MM Subject: Re: [PATCH mm-unstable] mm/khugepaged: fix collapse_pte_mapped_thp() versus uffd In-Reply-To: Message-ID: <82d294-c9b0-d7b4-71c9-cfed3925c47b@google.com> References: <4d31abf5-56c0-9f3d-d12f-c9317936691@google.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="-1463760895-405531799-1692730484=:3162" X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E642EA0023 X-Stat-Signature: kuuzoutqgbsq1g6bagctpokp7n81gcx1 X-Rspam-User: X-HE-Tag: 1692730485-903202 X-HE-Meta: U2FsdGVkX19waLcN+UNTahINenuxwmjkPx9hwc6+1YiDgYaN83POkaLkCHA2QumzPXfD6631Dk5xaVHvc//nVZLMH5Cmx3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IOaRz0sGcx7rla7CgJ9I5oJRDw8lL5rwDk7zKjVJ8sH6amnRI2t4M7r8hk3xqiDdObkwDDDxuQ6ItDQGzVvK4LRNoiZvyTHLh+2rqEp6LmL7vT915PTSg9OEENWvbBsXPQ4risAofHw8PjzH2+zVGAQAYDHQ5imEcIad8FEFkW5eIk8mv1NLDdbaFc4vQgSQsnMTHkDKjosAVVc4lVD72sVcijPaEWfLExOHru3h87bsB1/e4LbNPTu6ljVObxylEDc827n4JV8y0/8kqKodLDa8CzdBmUmePtQQztD6+E6OfkYnMx6ws6T2Es+JzPFNPXpInvwBouw4d3v2f+IITNnlKzqSQCQdhU32N809HVsWkDRD38H3Yqcjrd6GB80EROIyqdzRrxY7kzo1MkHlwlQnQY= 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: This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---1463760895-405531799-1692730484=:3162 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE On Tue, 22 Aug 2023, Jann Horn wrote: > On Tue, Aug 22, 2023 at 4:51=E2=80=AFAM Hugh Dickins w= rote: > > On Mon, 21 Aug 2023, Jann Horn wrote: > > > On Mon, Aug 21, 2023 at 9:51=E2=80=AFPM Hugh Dickins wrote: > > > > Just for this case, take the pmd_lock() two steps earlier: not beca= use > > > > it gives any protection against this case itself, but because ptloc= k > > > > nests inside it, and it's the dropping of ptlock which let the bug = in. > > > > In other cases, continue to minimize the pmd_lock() hold time. > > > > > > Special-casing userfaultfd like this makes me a bit uncomfortable; bu= t > > > I also can't find anything other than userfaultfd that would insert > > > pages into regions that are khugepaged-compatible, so I guess this > > > works? > > > > I'm as sure as I can be that it's solely because userfaultfd breaks > > the usual rules here (and in fairness, IIRC Andrea did ask my permissio= n > > before making it behave that way on shmem, COWing without a source page= ). > > > > Perhaps something else will want that same behaviour in future (it's > > tempting, but difficult to guarantee correctness); for now, it is just > > userfaultfd (but by saying "_armed" rather than "_missing", I'm half- > > expecting uffd to add more such exceptional modes in future). >=20 > Hm, yeah, sounds okay. (I guess we'd also run into this if we ever > wanted to make it possible to reliably install PTE markers with > madvise() or something like that, which might be nice for allowing > userspace to create guard pages without unnecessary extra VMAs...) I see the mailthread has taken inspiration from your comment there, and veered off in that direction: but I'll ignore those futures. >=20 > > > I guess an alternative would be to use a spin_trylock() instead of th= e > > > current pmd_lock(), and if that fails, temporarily drop the page tabl= e > > > lock and then restart from step 2 with both locks held - and at that > > > point the page table scan should be fast since we expect it to usuall= y > > > be empty. > > > > That's certainly a good idea, if collapse on userfaultfd_armed private > > is anything of a common case (I doubt, but I don't know). It may be a > > better idea anyway (saving a drop and retake of ptlock). >=20 > I was thinking it also has the advantage that it would still perform > okay if we got rid of the userfaultfd_armed() condition at some point > - though I realize that designing too much for hypothetical future > features is an antipattern. >=20 > > I gave it a try, expecting to end up with something that would lead > > me to say "I tried it, but it didn't work out well"; but actually it > > looks okay to me. I wouldn't say I prefer it, but it seems reasonable, > > and no more complicated (as Peter rightly observes) than the original. > > > > It's up to you and Peter, and whoever has strong feelings about it, > > to choose between them: I don't mind (but I shall be sad if someone > > demands that I indent that comment deeper - I'm not a fan of long > > multi-line comments near column 80). >=20 > I prefer this version because it would make it easier to remove the > "userfaultfd_armed()" check in the future if we have to, but I guess > we could also always change it later if that becomes necessary, so I > don't really have strong feelings on it at this point. Thanks for considering them both, Jann. I do think your trylock way, as in v2, is in principle superior, and we may well have good reason to switch over to it in future; but I find it slightly more confusing, so will follow your and Peter's "no strong feelings" for now, and ask Andrew please to take the original (implicit v1). Overriding reason: I realized overnight that v2 is not quite correct: I was clever enough to realize that nr_ptes needed to be reset to 0 to get the accounting right with a recheck pass, but not clever enough to realize that resetting it to 0 there would likely skip the abort path's flush_tlb_mm(mm), when we actually had cleared entries on the first pass. It needs a separate bool to decide the flush_tlb_mm(mm), or it needs that (ridiculously minor!) step 3 to be moved down. But rather than reworking it, please let's just go with v1 for now. Thanks, Hugh ---1463760895-405531799-1692730484=:3162--