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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EFEF0CE8359 for ; Mon, 30 Sep 2024 15:13:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=7NAgTPJPGqm/JioQk2+iBCxTaRDo5H0zF+EOeiTkB1o=; b=tgKZI60t9EBgh7aAC8nUy4C9si HZZv6KEb9eBIqGvlw9DojELg/zOdRF+CK/qk7UHGWS8It5zDiem9VEV0JMUFy9LOLaW2CZvWzHKp+ 3p2/Wjtkbl8fQkgzalFMvioVRwk2Fhq0TV4Kype/0feEtEf2THAYFcuf7BrM0bVM/3qOrilcWnKii R5YkiXAZAx7Rl/wHzV+tyvW6rkT4bXT64MMahuRxIwS/XK9Q2fRlbKidBDVC/bI2p3jkakHCP1NUp NVeHOWsEVBiXFTym9N14PT5noSubh5JUDCOwfyEj1YiA9GJyH63hShNEsDd2HWCC+pqnSDkv2FehM WwSrKDMw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1svI5S-000000001VK-0jWa; Mon, 30 Sep 2024 15:13:22 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1svI4D-000000001PX-2iAJ for linux-arm-kernel@lists.infradead.org; Mon, 30 Sep 2024 15:12:07 +0000 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 C59DD367; Mon, 30 Sep 2024 08:12:33 -0700 (PDT) Received: from [10.1.29.171] (XHFQ2J9959.cambridge.arm.com [10.1.29.171]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F1A503F58B; Mon, 30 Sep 2024 08:12:02 -0700 (PDT) Message-ID: <14aa1cbe-7793-4d2b-9c17-9394903b82b4@arm.com> Date: Mon, 30 Sep 2024 16:12:01 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v8 36/43] arm64: mm: Add support for folding PUDs at runtime Content-Language: en-GB To: Ard Biesheuvel Cc: Ard Biesheuvel , linux-arm-kernel@lists.infradead.org, Catalin Marinas , Will Deacon , Marc Zyngier , Mark Rutland , Anshuman Khandual , Kees Cook References: <20240214122845.2033971-45-ardb+git@google.com> <20240214122845.2033971-81-ardb+git@google.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240930_081205_807818_AB53099C X-CRM114-Status: GOOD ( 23.09 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 30/09/2024 15:53, Ard Biesheuvel wrote: > On Mon, 30 Sept 2024 at 16:36, Ryan Roberts wrote: >> >> Hi Ard, >> >> On 14/02/2024 12:29, Ard Biesheuvel wrote: >>> From: Ard Biesheuvel >>> >>> In order to support LPA2 on 16k pages in a way that permits non-LPA2 >>> systems to run the same kernel image, we have to be able to fall back to >>> at most 48 bits of virtual addressing. >>> >>> Falling back to 48 bits would result in a level 0 with only 2 entries, >>> which is suboptimal in terms of TLB utilization. So instead, let's fall >>> back to 47 bits in that case. This means we need to be able to fold PUDs >>> dynamically, similar to how we fold P4Ds for 48 bit virtual addressing >>> on LPA2 with 4k pages. >>> >>> Signed-off-by: Ard Biesheuvel >> >> [...] >> >>> >>> +#define pud_index(addr) (((addr) >> PUD_SHIFT) & (PTRS_PER_PUD - 1)) >>> + >>> +static inline pud_t *p4d_to_folded_pud(p4d_t *p4dp, unsigned long addr) >>> +{ >>> + return (pud_t *)PTR_ALIGN_DOWN(p4dp, PAGE_SIZE) + pud_index(addr); >>> +} >>> + >> >> I wonder if you could explain what this function (and its equivalents at other >> levels) is doing? Why isn't it just returning p4dp cast to a (pud_t *)? >> > > Because the p4dp index is derived from a different part of the VA, so > it points into the right page but at the wrong entry. OK, yeah I think that's sunk in. TBH, the folding stuff melts my brain. Thanks for the quick response. So the code is definitely correct, and it's needed because the PxD_SHIFT and PTRS_PER_PxD are "wrong" for the real geometry. > >> I'm working on a prototype for boot-time page size selection. For this, I'm >> compile-time enabling all levels, then run-time folding the ones I don't need, >> based on the selected page size and VA size. >> > > Nice! Certainly in principle. Hoping to get an RFC out during October. > >> I'm trying to reuse your run-time folding code, but I have a case where this >> function is broken as written. Replacing with "return (pud_t *)p4dp;" resolves >> the problem; If VA_BITS=48 and pagesize=64K, the pgd has 64 entries. p4dp is >> pointing to the correct entry in the pgd already, but this code aligns back to >> the start of the page, then adds pud_index(), which is wrong because >> PTRS_PER_PUD != PTRS_PER_PGDIR. (In my case, these 2 macros are actually >> boot-time selected values rather than compile-time constants). >> >> I think your code is probably correct and working around PTRS_PER_PXD being >> compile-time constants for the non-folded case, but I can't quite convince myself. >> > > Is your p4dp pointing to the correct descriptor because you changed > the runtime behavior of p4d_index() perhaps? Yes; in my prototype, P4D_SHIFT and PTRS_PER_P4D are set at runtime and end up the same as they would have been if pgtable-nop4d.h was included for compile-time folding.