From patchwork Mon Jan 29 12:46:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13535515 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 9F810C47422 for ; Mon, 29 Jan 2024 12:47:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 371F66B008A; Mon, 29 Jan 2024 07:47:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3225F6B008C; Mon, 29 Jan 2024 07:47:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1EB7C6B0092; Mon, 29 Jan 2024 07:47:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 0AFD56B008A for ; Mon, 29 Jan 2024 07:47:10 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 913AB80A41 for ; Mon, 29 Jan 2024 12:47:09 +0000 (UTC) X-FDA: 81732323778.15.82CCABB Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf26.hostedemail.com (Postfix) with ESMTP id D3D91140004 for ; Mon, 29 Jan 2024 12:47:07 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=X9eZ37T6; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf26.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706532427; a=rsa-sha256; cv=none; b=cf8f8g+ghOU+SuDiLtscEkEY66v9kEtmfNk7eAuvidBGk5i2LFRyOxLhw7hYE3MIMsR51l 0kZqPwKpdFGE8OMuM0qhkLKaPpzF+cDdk7OrFCkZ8nMqLm9w+CRdEbz79gVX70ZPJYdxWq z3jkchNXNcaHfa17EfHYuPTGwNNuV7I= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=X9eZ37T6; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf26.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706532427; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3xazwhF5EUmMqWzaAq7c+f2E0xGFlCS1HyOOY3hDGx8=; b=IPcjPBuZLti3YBtrsajE1GENDtxlVMUYSTcensIZ6pfsOIhys9Ri6pBBxIvTRsv9uqxCkl Fx0TOuCTQvcMscVWuhZdMLdS9j7DfXuWmGE7C4wE7y56AocHewH24Q6YtHoBIJn6pN2Hju 2gfxGjKuu4K4A44tiyeZICbnU+14B40= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706532427; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3xazwhF5EUmMqWzaAq7c+f2E0xGFlCS1HyOOY3hDGx8=; b=X9eZ37T6ZptrpLNqE8WpWFciKnIJazgtIcQ6ImCl1h2HUSzHWdTg8xSu8NxrDCWvl+i+i6 FLyGS4FkwjddEEaY0yf3WRXU2WsmszwvwrVICyJFU0lf7UfEnYPbBjzWkmf/UxIgpiyYuO 5YcVAux6aPpdxsKwhaXDIzfExVAbaNQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-583-MiBqcbXQPtSpFj1e-vh6bg-1; Mon, 29 Jan 2024 07:47:03 -0500 X-MC-Unique: MiBqcbXQPtSpFj1e-vh6bg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AFCBF867943; Mon, 29 Jan 2024 12:47:02 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 118438B; Mon, 29 Jan 2024 12:46:56 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Matthew Wilcox , Ryan Roberts , Russell King , Catalin Marinas , Will Deacon , Dinh Nguyen , Michael Ellerman , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , "Naveen N. Rao" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexander Gordeev , Gerald Schaefer , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , Sven Schnelle , "David S. Miller" , linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org Subject: [PATCH v3 01/15] arm64/mm: Make set_ptes() robust when OAs cross 48-bit boundary Date: Mon, 29 Jan 2024 13:46:35 +0100 Message-ID: <20240129124649.189745-2-david@redhat.com> In-Reply-To: <20240129124649.189745-1-david@redhat.com> References: <20240129124649.189745-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D3D91140004 X-Stat-Signature: ex555j1a9gg1e4hkkh773c79pg3b54an X-HE-Tag: 1706532427-447820 X-HE-Meta: U2FsdGVkX18lWwl80JIEZz0ucZ15XqEGUXFyvGfVaRfLaXE1kAz2/W3NyowwjgddJKoA6Q2KaYdp75QGVoIHdKVXjDLGSj2GOuHuQPcAPra+S9NAcW4kEymJNCTYqqU7pPT9gjYMmptFzpd9394hMdQmUwsALbxu+OIJMTf2fSweBLofh21kXuadX+Hly9S8GLrBF7u262RDshD0h3uTPyJ+jqcow/M61v18am3FARDM0/XAOutfjzslnaGz5SKN6Ujyf3PPKn/mlPmKrkYqar2KN4cG+5JHxWfZL+wo4ZkCTpK9PsTieK8FGLpU6mshFj8GKHDhN7ygi9aMbNEqKaU7IB4mfcqDe5L5Gj10cbGRY9g5+zlLS754qicR7apT/qHLTt9QlblJHgC3lakPSaN5lHySH4D9GR+175Qg1TfiIyuFB46OSdq2xBm6UJa/eqzRBNiUAgcdSeXq1XbmrpIO+NB7N2BJgEPfMjrya9CCuWdEuGBBhP8ZAU9ysaeP2dHB/FMt52ENyIEZV47vTrUEA3ESHOP2haHCYuonOhoou1SCZZAFL7qW/rGx9G0QC+ebi5EK4agm3Pv+KdH5ofAP/OqZkOyOhM/EoHRZK4HziVUdP4AhWhvduUl2NNqhCxlCIr68iML+lQhjpOk5dsW69/mggDGsC7sfos4f02jvk7CngyQH3nz9YCT3/FbqERtJma3PcTTGwqmvFVmiaqQGL7mQo4XFBF01rce5I7HXlt5g9/SHXOrmv5VVCzX9slBmLiuRXbrYxNrUKNn+uw1+N6GqdLlUJxVAIAu+4+w3zOPsQ347pW5z/WDiMOoLDdDgFpXpbQEmH3V8gJInC0Z8HEbX3yuV2SVH8o+GVcSCovyXGnBxyATO3wKTYR3XGArYB1/Rutq7LQXyGkWKuk4F2vNuluCp1xoFRZ2wi0sxMlLNSqiO8aa6+nSd7R7i4o0+J4FnQLICQWGJ0n2 z6sQXUgT 6ZzfvIningcBi6s75OCVzWDW69VjGbhrxbT7ox+tg4EcoiPsu2pTGkllYdqT3jX8t8COYje57B00ZV7kA90HOA4REsFg9Y7ry0Mg31M13bqpGD3TiGbR9Ykra2dzoIR+2BV/EnPymgBrSf9HcOTPb/WUXcqO0Hf+yu3af/j8GpyhIqSpgrJX8lDuk5TP5nVKY7Pbenn/4xXVXM6BUD4De/TvwtULU90kJ+NF1AiuUBT6D7EaEaJOQ40U5Ulzu1uCJKkCxhY72qpFqYvahUwJQcGxYuHj0zgv710WhgF4+l0mvQSkkpxczX15sKgIDZK1B4CsAapWpEO5C1cYfRG06qmkYIA== 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: From: Ryan Roberts Since the high bits [51:48] of an OA are not stored contiguously in the PTE, there is a theoretical bug in set_ptes(), which just adds PAGE_SIZE to the pte to get the pte with the next pfn. This works until the pfn crosses the 48-bit boundary, at which point we overflow into the upper attributes. Of course one could argue (and Matthew Wilcox has :) that we will never see a folio cross this boundary because we only allow naturally aligned power-of-2 allocation, so this would require a half-petabyte folio. So its only a theoretical bug. But its better that the code is robust regardless. I've implemented pte_next_pfn() as part of the fix, which is an opt-in core-mm interface. So that is now available to the core-mm, which will be needed shortly to support forthcoming fork()-batching optimizations. Link: https://lkml.kernel.org/r/20240125173534.1659317-1-ryan.roberts@arm.com Fixes: 4a169d61c2ed ("arm64: implement the new page table range API") Closes: https://lore.kernel.org/linux-mm/fdaeb9a5-d890-499a-92c8-d171df43ad01@arm.com/ Signed-off-by: Ryan Roberts Reviewed-by: Catalin Marinas Reviewed-by: David Hildenbrand Signed-off-by: David Hildenbrand Reviewed-by: Mike Rapoport (IBM) --- arch/arm64/include/asm/pgtable.h | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index b50270107e2f..9428801c1040 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -341,6 +341,22 @@ static inline void __sync_cache_and_tags(pte_t pte, unsigned int nr_pages) mte_sync_tags(pte, nr_pages); } +/* + * Select all bits except the pfn + */ +static inline pgprot_t pte_pgprot(pte_t pte) +{ + unsigned long pfn = pte_pfn(pte); + + return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte)); +} + +#define pte_next_pfn pte_next_pfn +static inline pte_t pte_next_pfn(pte_t pte) +{ + return pfn_pte(pte_pfn(pte) + 1, pte_pgprot(pte)); +} + static inline void set_ptes(struct mm_struct *mm, unsigned long __always_unused addr, pte_t *ptep, pte_t pte, unsigned int nr) @@ -354,7 +370,7 @@ static inline void set_ptes(struct mm_struct *mm, if (--nr == 0) break; ptep++; - pte_val(pte) += PAGE_SIZE; + pte = pte_next_pfn(pte); } } #define set_ptes set_ptes @@ -433,16 +449,6 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte) return clear_pte_bit(pte, __pgprot(PTE_SWP_EXCLUSIVE)); } -/* - * Select all bits except the pfn - */ -static inline pgprot_t pte_pgprot(pte_t pte) -{ - unsigned long pfn = pte_pfn(pte); - - return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte)); -} - #ifdef CONFIG_NUMA_BALANCING /* * See the comment in include/linux/pgtable.h