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 E3AB2C4345F for ; Mon, 29 Apr 2024 05:31:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F20A6B007B; Mon, 29 Apr 2024 01:31:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A1566B0083; Mon, 29 Apr 2024 01:31:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 290336B0085; Mon, 29 Apr 2024 01:31:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 0B5FA6B007B for ; Mon, 29 Apr 2024 01:31:17 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 677301A1792 for ; Mon, 29 Apr 2024 05:31:16 +0000 (UTC) X-FDA: 82061446152.24.CF5190E Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf20.hostedemail.com (Postfix) with ESMTP id AF0541C000C for ; Mon, 29 Apr 2024 05:31:14 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf20.hostedemail.com: domain of anshuman.khandual@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=anshuman.khandual@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714368674; 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=GtZFXoOa+oPCYh5vPUC/nJy4CT2tWrQil31MrnQ5rz4=; b=IeRUnRuoFZnwcTXqOxh3/mLH/pgkFN85dfqnB1e9kAxpsqsUfMT8+viOsoBoVXmV1dGxMy tnCRRaSsWffZrs2Ecve+7yrL4zpqHzBbqqUpkDLOoXBiK5zmJ40C0r7b7sCCa2WkTcL4nE SI0rh0wQK7qK9DwzH1FIk7nuJCph7mk= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf20.hostedemail.com: domain of anshuman.khandual@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=anshuman.khandual@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714368674; a=rsa-sha256; cv=none; b=k/uQedHSAYrjCWk+2klsQOoHH8eaCk7e0X8iygvo9Dc3YqQX3iqUkSjP5oMHbEgW0zXjyq kl3ztnkBaxZnok7+rc9VVQpSu4dzv7WDx58tKbNYZ1QecExwrKVZwcuYR+d7mh04OjCbmr klKf6V0kpx4ubGuNoOhNi3XJUhCHF4o= 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 0CAD92F4; Sun, 28 Apr 2024 22:31:41 -0700 (PDT) Received: from [10.162.42.72] (a077893.blr.arm.com [10.162.42.72]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 888473F793; Sun, 28 Apr 2024 22:31:11 -0700 (PDT) Message-ID: <755b4f9e-437f-468f-a43e-c93742ac9828@arm.com> Date: Mon, 29 Apr 2024 11:01:08 +0530 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-US To: Zi Yan , John Hubbard Cc: Ryan Roberts , Andrew Morton , "Aneesh Kumar K.V" , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20240425170704.3379492-1-ryan.roberts@arm.com> <39d0236c-4297-4001-bc7e-f516bc674f7b@arm.com> <87df74b7-2352-4ad6-b9f9-baed9291cbed@arm.com> <80148272-208A-4306-A8EC-98C79BBBDCA0@nvidia.com> <1734de6e-1bcd-492e-b07b-9ad712967e1d@nvidia.com> <6F2BB00A-DBCD-4482-B16E-B71A02847F0D@nvidia.com> From: Anshuman Khandual In-Reply-To: <6F2BB00A-DBCD-4482-B16E-B71A02847F0D@nvidia.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Stat-Signature: 5tt1y5s1mk6xsxc4b9ynaurp8hrtwhsy X-Rspamd-Queue-Id: AF0541C000C X-Rspamd-Server: rspam10 X-Rspam-User: X-HE-Tag: 1714368674-22627 X-HE-Meta: U2FsdGVkX1+OXLlzAUYtMUNxsaxhv9gX2eLe9725DrBQMfwFzwNNfn9fjk1H6s3gauWyqPqGkyQI6C3r3Ww/fWdBHkVUJwHVdz3nUVZqmj3s9QqCtTHkK+TvpmiyZIUVunn8SwpEecCQm60QPwJd37Y3V5VbmXtT6hjYMYx0lEa35miTt2wbhA0ldiksRQPOtn143129Si7EkfN//np2Y7AL8AKCIK/ZjKDbfhHPomieRVCY9rC1SyljZcDCoI1n8xXJpKxgw7yseVAQkKVYLfs3GcIEprR5NZ1bKsfZGsKDgFZxva2i3OLG53U3RfIzGU/vNjrtgAiOWvjFiPOzMTe/o1LznqdXR8fZwsZ26I7LhNmbVISIf0c5GucIQZmw3yq/ZHrgLxaCGjjehRkhaVM56OcQHxgINZxtDs3ivvKs5OVhZpsaW3sSJwEWbJMfBg+YWU0zP0Qc+TE6eLy3EY8w0aonU1hYioemeI/SCB7r01uPXDkTfnnP9gS3AJKGhMdA9LmJcYL2s/++9Lw828HryaahNGg4UUNx7Z1uv2m5Guf2/MjHajoXTr4Des0UKsJWiOGVmDqmp8O/GCSRxTBJEnbhmHtq2u1O91NdSsSaTsM+oTwRDikg5YXZldzsWw0A/1hH0y8Xxsb9O/+Ar4IzPEeXW2on+9HrUbtv3V1RGy12nLp7pecbUxDntAZ7P6kteCxF4BcY4WcL93Tas6Oi2lpL7cC8J0RfJF9q44H9Ql/aYfK4qMH/RCYPnkWmKw3ookjkTu9GKFmsdobCg5DiUIdaR3+B/trRXnkDXE+EoaJSvu+yVqkN7egsIKH4Os1XjRohn80581gB9V8iOmURqx0wEEm96/6xC7IZTIQTYrAFMfvZzHij9iCyxhV8Wgpn9nsDpUQxLnPvf+9z7JKuLIQIr1AZc6x95IUf8s10eLX1jVcLfLt2tIcZYid22DT63XV4ty97WiyXCCK lOXsMy2F BUfywo3GhFRzs3jzWrun7BCqvFGHw7Byv4EusMPkZVuQRjc/11II8u58iujDS9JJA1/qVCK2W73/ze8d65uoe3OGaKxZ6mHuzF8VyA/CfTWNxYDTTfDptpSqDSWEobENNDy29f/uNhTIiAn/rVbiWXFdxy8mHnFsaVPlpnn3SOIDzQI1Grnvq3TrGhKE3T+/MZUZe 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 4/27/24 20:37, Zi Yan wrote: > On 27 Apr 2024, at 0:25, John Hubbard wrote: > >> On 4/26/24 7:53 AM, Zi Yan wrote: >> >> Hi Zi (and Ryan)! >> >>>>>>> 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. >>>>>> Could you please explain how bad things might happen ? >>>>> See 2 places where pmdp_get_lockless() is called in gup.c, without the PTL. >>>>> These could both return the swap pte for which pmd_mkinvalid() has been called. >>>>> In both cases, this would lead to the pmd_present() check eroneously returning >>>>> true, eventually causing incorrect interpretation of the pte fields. e.g.: >>>>> >>>>> gup_pmd_range() >>>>> pmd_t pmd = pmdp_get_lockless(pmdp); >>>>> gup_huge_pmd(pmd, ...) >>>>> page = nth_page(pmd_page(orig), (addr & ~PMD_MASK) >> PAGE_SHIFT); >>>>> >>>>> page is guff. >>>>> >>>>> Let me know what you think! >>> Add JohnH to check GUP code. >> Ryan is correct about this behavior. >> >> By the way, remember that gup is not the only lockless page table >> walker: there is also the CPU hardware itself, which inconveniently >> refuses to bother with taking page table locks. 🙂 >> >> So if we have code that can make a non-present PTE appear to be present >> to any of these page walkers, whether software or hardware, it's a >> definitely Not Good and will lead directly to bugs. > This issue does not bother hardware, because the PTE_VALID/PMD_SECT_VALID > is always unset and hardware always sees this PMD as invalid. It is a pure > software issue, since for THP splitting, we do not want hardware to access > the page but still allow kernel to user pmd_page() to get the pfn, so > pmd_present() returns true even if PTE_VALID/PMD_SECT_VALID is unset by > setting and checking PMD_PRESENT_INVALID bit. pmd_mkinvalid() sets > PMD_PRESENT_INVALID, turning a migration entry from !pmd_present() to > pmd_present(), while it is always a invalid PMD to hardware. Agreed, this is not a HW issue at all, MMU sees such an entry as invalid even if pmd_present() returns true.