From patchwork Thu Jan 25 19:32:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13531542 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 1294EC47422 for ; Thu, 25 Jan 2024 19:32:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7BD27280007; Thu, 25 Jan 2024 14:32:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 76DC2280004; Thu, 25 Jan 2024 14:32:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65C65280007; Thu, 25 Jan 2024 14:32:48 -0500 (EST) 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 57580280004 for ; Thu, 25 Jan 2024 14:32:48 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 34744140D9B for ; Thu, 25 Jan 2024 19:32:48 +0000 (UTC) X-FDA: 81718830816.05.EB5B46D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf14.hostedemail.com (Postfix) with ESMTP id 76AF6100018 for ; Thu, 25 Jan 2024 19:32:45 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="WYDWu/02"; spf=pass (imf14.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706211165; 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=82AmjG/O5udPRimJWVv8/kqN5WdZj3aSPBj/SjaK3u4=; b=m8zRUxcV1D5ABsPiW5BzXAhM4HfSxIJBUtkN8xewh2YVdeihXaSYnFd1jMCoBtlNczrARe p3y2skxdn4+KfKOlaO7EcOaIUNDPsINWcILwpjfogkT73eAV2OZR7ntkt1xwA11cqWG7Wu WvR+WFkNnieABIlYBQJjJQskHLtgEHM= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="WYDWu/02"; spf=pass (imf14.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706211165; a=rsa-sha256; cv=none; b=cowlj8+QCMf+KTZPhYrsbMB9NmFgKm8GGMJdQrWWA4wNpyX+c7pWa8UpwysM/KatOj5LOY 5IkciiAFosJ0yykHc138AXuo8DzNmrgUKCzSAalmaWWigcJyAt/asqqD7bH4iWwBImBdbA UJBTYFSkuvse5nato/W+vrxtAY4is+0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706211164; 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=82AmjG/O5udPRimJWVv8/kqN5WdZj3aSPBj/SjaK3u4=; b=WYDWu/02TD2ILhX4757Wal76HqJSKpOk2/A5kSi6jYrLyt9z7KpbIxsSwBOldV8rP2RbGI Dj1/nz6nBlYfBVaZ6/R5cyG8lxD/ME4NAsOfrcIJb9tghAueaHZ1A6vYvCuxdFShS19eTu F61DN9RubXCBGCRkL2/ASgwnhJys+qs= 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-581-J0yUPAt0OCGNPncPGz6sEA-1; Thu, 25 Jan 2024 14:32:42 -0500 X-MC-Unique: J0yUPAt0OCGNPncPGz6sEA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 EFA1F8432A6; Thu, 25 Jan 2024 19:32:40 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.193.154]) by smtp.corp.redhat.com (Postfix) with ESMTP id 81D45492BC6; Thu, 25 Jan 2024 19:32:35 +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 v2 01/15] arm64/mm: Make set_ptes() robust when OAs cross 48-bit boundary Date: Thu, 25 Jan 2024 20:32:13 +0100 Message-ID: <20240125193227.444072-2-david@redhat.com> In-Reply-To: <20240125193227.444072-1-david@redhat.com> References: <20240125193227.444072-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Rspamd-Queue-Id: 76AF6100018 X-Rspam-User: X-Stat-Signature: dq8gebcdu6cpsi4184ttr1e9m4iwkqx1 X-Rspamd-Server: rspam01 X-HE-Tag: 1706211165-970915 X-HE-Meta: U2FsdGVkX18d4e9Q+2mMQG6NW9/LZbgC+JifXJAIxDyyxscVwErsqcNj239vCthUjx9U4hScshMb+2sbgDUYbSHryI5kLyi1w0UNtGyhPQj9IQGz5BGqsLtm4XibdYNAfI+ar6L4gzmG3vQ+d2KahlcZJUDL76N3ge02E5dLEIc95pl4Dr6Owa++QJyy+swNt0cxWC1czh5T4ftj/yU7UPnO+K5JY26OXNtJDiYQdrJPqk/YZlDGOff/ienUOPc3EZEra/rdeGgJ9M6ovd7p294dL83U5oHD+0cFuqlj3NyiV7S4xsXqwIr9sTdXWoHJrM080tbGza66HjvlIYTc8MC6+6UVgVagu/qtk0gBYCjyLdOSyX79H4uxjiucwErJ5Mwl12zHi3u0DqReMW6URdZiIpqc45H+ZoYHbTmTF53gux4X+aleWPSg2zaI9BhBR9TTBIf3LF7YLmyYsVeW4YmpjQGIjtP8d41zTsRFCcGECbLR85ms5c6MpNS3tRwBOQ5lMhmNgIW1MKdDaD3XUyRg6hBL2HLlrHvcb2/1t/nkKURnnHvgt7MNWgfx6m01+98zn1p5GZtbGlQOWX1aoW6Phdjj8VYIxq15pSFjeohlbaYRAeD598FxAxoV/9Z6gZCrA3iCwjPHFXZYxzuywlyEMdaU2MUdl0i4BQRqxaepHmn8YfOgZezZvVRHqpWqrI+8128hSt32328+N2ADZbwiDZg7M9tiHr+H2iDR9iBSo1svGezUKZzPJbXkw/AVmSV0AD5bLktkiyFNlx3OfJZka/x5IkoFjb9dABVPKo8BAvv4TY4MMj8jsGnsQXryDVcNZ54PxNccItt/q+l5zgBdTG1twaKn/wmScAlskOFppOMwxvTTl0D4raTWetIfeWvtzmVxCSUaJhYZvRxOOPrq4kZgevnGbSYeEgCD5vYKyEIAQe0YLL6FfTTLlf/zhxWCTeMfez+HlTw+jDu OI8CGZuG T0peKOPzRRb2TY77hNovka3W4wByGunNLmiD+KJoiypdmjEZkGzH78Xo/i5erEgYFebjnWQWIjLvQuHEeSV1iNKNxv4Un+2LU0Vc9RhuV5jqslRKjHRiynX9wFEaBK9NHz/cNAgedfbk7lJ8mOihzN2nGQ8XzgUklZmTzeA5PItSRgm8Tuuubjidumy4Ub/F+1HwFeTESzC+xZNfffZXhwxj+H9CxjeL61xCWMVxec7f1gYu+JnrJQnBzDt/HtYwlyf3BYDztk9G4dyhNbSNAFSAl/6tmGpfMiljg8Lpia+smphcY+ypGdKcM0CLXjucrQhRkcIQmV7Vfl6L7GpWOw954NQ== 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 --- 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 79ce70fbb751c..52d0b0a763f16 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