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 9895DC4345F for ; Mon, 29 Apr 2024 15:35:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0B9D36B008C; Mon, 29 Apr 2024 11:35:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 06A966B0096; Mon, 29 Apr 2024 11:35:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E73BF6B009A; Mon, 29 Apr 2024 11:35:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CA6156B008C for ; Mon, 29 Apr 2024 11:35:01 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 79C2A1A09D4 for ; Mon, 29 Apr 2024 15:35:01 +0000 (UTC) X-FDA: 82062967602.13.DCBAA40 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf14.hostedemail.com (Postfix) with ESMTP id 8AF5D10001B for ; Mon, 29 Apr 2024 15:34:59 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714404900; 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; bh=WUoF1Kagu9akrJxmqhAJIQcTKDyCY1M/h63tASpl00Y=; b=3+FXAtWzRWk03omFB9NNOlKxSX9bQSzxDSOSu7OFDEqdSVxHQlERsri+gPCl9fH7+uQEgB Gzn9vpyzWccs+TzwapeSU1ThTzn441a5dNmwtBIfK8wvZXOFRH9cYPr6VC1VCSI39vtsey /7A8LPUSnAaRBU94sKBDI+QLVwPS0vg= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714404900; a=rsa-sha256; cv=none; b=pybE5nTJCDPbWmGMrGpcmPxSsu7Akhxij1CzIP2yZ4c9Aj9cThl8dXgtNn34mzKa1QEABs beB4kLydd310C6aFLxXnS/GpXAwt4xTKsBUXSVxAdfCcJw0uJyMMvpP3S0PPEZMnAJM8Q7 NjUN5M5pVUb2o2ZtFTNB6zICBCjyEvY= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 29D292F4; Mon, 29 Apr 2024 08:35:25 -0700 (PDT) Received: from [10.57.65.53] (unknown [10.57.65.53]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 623AE3F793; Mon, 29 Apr 2024 08:34:57 -0700 (PDT) Message-ID: <99384a25-9ff5-43c9-b09d-5a048c456d02@arm.com> Date: Mon, 29 Apr 2024 16:34:56 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1] mm: Fix race between __split_huge_pmd_locked() and GUP-fast Content-Language: en-GB To: Zi Yan Cc: John Hubbard , Andrew Morton , Zi Yan , "Aneesh Kumar K.V" , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20240425170704.3379492-1-ryan.roberts@arm.com> <17956e0f-1101-42d7-9cba-87e196312484@nvidia.com> <2C9D897B-0E96-42F0-B3C4-9926E6DF5F4B@nvidia.com> <328b2b69-e4ab-4a00-9137-1f7c3dc2d195@nvidia.com> <23fbca83-a175-4d5a-8cf7-ed54c1830d37@arm.com> <64037B35-3E80-4367-BA0B-334356373A78@nvidia.com> From: Ryan Roberts In-Reply-To: <64037B35-3E80-4367-BA0B-334356373A78@nvidia.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Queue-Id: 8AF5D10001B X-Rspamd-Server: rspam06 X-Stat-Signature: rda1f9nhzomho67wdaqqq45okfb8ydjf X-HE-Tag: 1714404899-139631 X-HE-Meta: U2FsdGVkX185e3bFhTZBxT9ciyVgBgdXZ8ajr0vVWx7qgn+qm1gXZU4x8btrlMwJRXcFn9nctiQZajeOz0bk1hN5ah/1gx8XfJ/JgDuBucTvABucbmHlyNdDD/U1rQdWjlCVNFxi+ToeODfBYWwF40L0kYg9rxUh4ZuTyocjbLcQqDM5GRwGpk2AWijocimjWLxrbpMjHl7p2hFLqvxUFM5tT5jEpPfeOhCDAuLTq0wFkUoVc/SDxrIwcaEHM42rPqa8RigHy12FguRqs5V0kXHIg9iaICE2GR4KOmOYtQr0mTedJAwXF0olpooUyLlKPOh0Rhj4/UPHTAnHFVyhGKZi4OGTvrrcDD3DmDYdhUzGRT5dAVT5V2ndcES0bKQZf3e+ifaqFj36F4YCyzCIMhWPYcsB7e032TxgbxhKWrZbMTd9GrQTkP70XpalNP+0cTZtbm+tvgerfDf+OLV6D7Og4jY+39CI2KGO6Q9Bg9KYzVAgHZ1wDe+OmkTQwZ2FK0IxmOd3SVVp5sRz9/o77nFq3snBjI0cimaQLKY/4cfRwcPRsztgjmO1VzITQs1rM6ekZRzSTrayc22dMuZ4stcgOJLXKtkwmQYZXyHPBxJbuJieMMGvq284Bybm9lpcPtYT7io1JmC1noNHPcZWC9o4mx07ctcj5XubIeComSLnaJN1I4vxJpncB/XBTAGmPEDEulPAI6U2elByS0A5u1ziEUMwGCrwcWtt0fuWSPycD8iCyBZXnDR4OuSp9P/PJhTesCM7WFjsvYOBiSIs8nfo1Rj1q6XQPmtkRuKPmtBykJf2lJCqaI27x9oMu8QGV86xVuJu02iERBw2mvoce6dREOUXh+4ztUht/RypsVu7NGByRje3I21m+93ThMsOAw7VTL2YBnM= 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: List-Subscribe: List-Unsubscribe: On 29/04/2024 15:45, Zi Yan wrote: > On 29 Apr 2024, at 5:29, Ryan Roberts wrote: > >> On 27/04/2024 20:11, John Hubbard wrote: >>> On 4/27/24 8:14 AM, Zi Yan wrote: >>>> On 27 Apr 2024, at 0:41, John Hubbard wrote: >>>>> On 4/25/24 10:07 AM, Ryan Roberts wrote: >>>>>> __split_huge_pmd_locked() can be called for a present THP, devmap or >>>>>> (non-present) migration entry. It calls pmdp_invalidate() >>>>>> unconditionally on the pmdp and only determines if it is present or not >>>>>> based on the returned old pmd. This is a problem for the migration entry >>>>>> case because pmd_mkinvalid(), called by pmdp_invalidate() must only be >>>>>> called for a present pmd. >>>>>> >>>>>> On arm64 at least, pmd_mkinvalid() will mark the pmd such that any >>>>>> future call to pmd_present() will return true. And therefore any >>>>>> lockless pgtable walker could see the migration entry pmd in this state >>>>>> and start interpretting the fields as if it were present, leading to >>>>>> BadThings (TM). GUP-fast appears to be one such lockless pgtable walker. >>>>>> I suspect the same is possible on other architectures. >>>>>> >>>>>> Fix this by only calling pmdp_invalidate() for a present pmd. And for >>>>> >>>>> Yes, this seems like a good design decision (after reading through the >>>>> discussion that you all had in the other threads). >>>> >>>> This will only be good for arm64 and does not prevent other arch developers >>>> to write code breaking arm64, since only arm64's pmd_mkinvalid() can turn >>>> a swap entry to a pmd_present() entry. >>> >>> Well, let's characterize it in a bit more detail, then: >> >> Hi All, >> >> Thanks for all the feedback! I had thought that this patch would be entirely >> uncontraversial - obviously I was wrong :) >> >> I've read all the emails, and trying to summarize a way forward here... >> >>> >>> 1) This patch will make things better for arm64. That's important! >>> >>> 2) Equally important, this patch does not make anything worse for >>>    other CPU arches. >>> >>> 3) This patch represents a new design constraint on the CPU arch >>>    layer, and thus requires documentation and whatever enforcement >>>    we can provide, in order to keep future code out of trouble. >> >> I know its only semantics, but I don't view this as a new design constraint. I >> see it as an existing constraint that was previously being violated, and this >> patch aims to fix that. The generic version of pmdp_invalidate() unconditionally >> does a tlb invalidation on the address range covered by the pmd. That makes no >> sense unless the pmd was previously present. So my conclusion is that the >> function only expects to be called for present pmds. >> >> Additionally Documentation/mm/arch_pgtable_helpers.rst already says this: >> >> " >> | pmd_mkinvalid | Invalidates a mapped PMD [1] | >> " >> >> I read "mapped" to be a synonym for "present". So I think its already >> documented. Happy to explcitly change "mapped" to "present" though, if it helps? >> >> Finally, [1] which is linked from Documentation/mm/arch_pgtable_helpers.rst, >> also implies this constraint, although it doesn't explicitly say it. >> >> [1] https://lore.kernel.org/linux-mm/20181017020930.GN30832@redhat.com/ >> >>> >>> 3.a) See the VM_WARN_ON() hunks below. >> >> It sounds like everybody would be happy if I sprinkle these into the arches that >> override pmdp_invalidate[_ad]()? There are 3 arches that have their own version >> of pmdp_invalidate(); powerpc, s390 and sparc. And 1 that has its own version of >> pmdp_invalidate_ad(); x86. I'll add them in all of those. >> >> I'll use VM_WARN_ON_ONCE() as suggested by John. >> >> I'd rather not put it directly into pmd_mkinvalid() since that would set a >> precedent for adding them absolutely everywhere. (e.g. pte_mkdirty(), ...). > > I understand your concern here. I assume you also understand the potential issue > with this, namely it does not prevent one from using pmd_mkinvalid() improperly > and causing a bug and the bug might only appear on arm64. Are you saying that arm64 is the *only* arch where pmd_mkinvalid() can turn a swap pte into a present pte? I hadn't appreciated that; in your first reply to this patch you said "I notice that x86, risc-v, mips behave the same" - I thought you were saying they behaved the same as arm64, but on re-reading, I think I've taken that out of context. But in spite of that, it still remains my view that making arm64's pmd_mkinvalid() robust to non-present ptes is not the right fix - or at least not sufficient on its own. That change on its own would still result in issuing a TLBI for the non-present pte from pmdp_invalidate(). That's not a correctness issue, but certainly could be a performance concern. I think its much better to have the design constraint that pmd_mkinvalid(), pmdp_invalidate() and pmdp_invalidate_ad() can only be called for present ptes. And I think the combination of WARNs and docs that we've discussed should be enough to allay your concerns about introduction of arm64-specific bugs. > >> >>> >>> 3.b) I like the new design constraint, because it is reasonable and >>>      clearly understandable: don't invalidate a non-present page >>>      table entry. >>> >>> I do wonder if there is somewhere else that this should be documented? >> >> If I change: >> >> " >> | pmd_mkinvalid | Invalidates a mapped PMD [1] | >> " >> >> To: >> >> " >> | pmd_mkinvalid | Invalidates a present PMD; do not call for | >> | non-present pmd [1] | >> " >> >> Is that sufficient? (I'll do the same for pud_mkinvalid() too. > > Sounds good to me. > > Also, if you move pmdp_invalidate(), please move the big comment with it to > avoid confusion. Thanks. Yes good spot, I'll move it. > > -- > Best Regards, > Yan, Zi