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 C93CEC3DA49 for ; Tue, 23 Jul 2024 15:03:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2EFF46B0088; Tue, 23 Jul 2024 11:03:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 29F6E6B0089; Tue, 23 Jul 2024 11:03:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 18F1E6B0095; Tue, 23 Jul 2024 11:03:03 -0400 (EDT) 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 EB9416B0088 for ; Tue, 23 Jul 2024 11:03:02 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 49AF4C01ED for ; Tue, 23 Jul 2024 15:03:02 +0000 (UTC) X-FDA: 82371335004.06.E35107F Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf16.hostedemail.com (Postfix) with ESMTP id 0A194180143 for ; Tue, 23 Jul 2024 15:02:33 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=K8vvUtP9; spf=pass (imf16.hostedemail.com: domain of ardb@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=ardb@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721746918; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=N1yYPo0egNrenPnj7ZX3ycI0kjrTJ8Pk4i7mApAUwOE=; b=HEe6YUyI/2KTFPz3/shfjFzD4zoUNrjvBs/xxY4LvM7tc/ZvWm3XOfokkJpOcuvggmcO9r 1StHUJXJpPZ3/2eFuprYDi1IzrD7UJPnRMcQNjL9BWHmKvnxREcAOlW59fIu6y+9pEIi40 +ZYET5RVECoDdxEBN0n4+W/pGu3Qaqw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=K8vvUtP9; spf=pass (imf16.hostedemail.com: domain of ardb@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=ardb@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721746918; a=rsa-sha256; cv=none; b=rdZIgyAlEdPSrO1Uya/kefvJ5pvB3cH0fwcYqlvTyPFXre6VZNyjZW9QftH1uxduKhlS4c 99ZI+95WUwsfkauyAw2QtC1gK2AgjStiISbxK5yTzhcyETe0OHqE4iywgI3OtSDuDMx8bM +DJsAWPatXoqcSOYCYNvINlGDqo4IeI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id A27DCCE1028 for ; Tue, 23 Jul 2024 15:02:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DEE11C4AF0C for ; Tue, 23 Jul 2024 15:02:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721746948; bh=xb2br442o0CzRVEGoDDCnvZdYGa/Zy7goBOWiolJR/0=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=K8vvUtP9DAdqEU2cS1+rc8/5eSXl+TUancEgF9Tfb859maB33Xntnv+4EQu7FpXEn xgzDIkGHSpsHH6NNE6msB04Hctri6LOz6AZaXcEIibrbYSGWzUBeBv692GNY0Zques SqGnbTdag4KrutjW7gAU2aUjAgerDFNRRj7A0Y0JTtbH94TLRH61Bvcek7+kcPmV2J w4Xmsfq0cr/4BaxP2FaRvZxgh/b/8G2AVRAirdpi7PKhb2Pnmgrr3PG3pouqO7Gotz HlQJ22VCS4OFSkFzwpAid8+84hRshdPzNZg4kuXR8Mxp6rnXEUKIJDsv1heV34ISs+ YtIBuo8x18V3Q== Received: by mail-lj1-f172.google.com with SMTP id 38308e7fff4ca-2ef2cce8be8so29280371fa.1 for ; Tue, 23 Jul 2024 08:02:28 -0700 (PDT) X-Forwarded-Encrypted: i=1; AJvYcCUzKDS0acWA54TbHAS/sSiybLNYM3kxPi6Ix6QhdgKxUqTBZVUdfKKVbt+encVgZtOjEHeCBxlyx9AHn6tjDZC9J9U= X-Gm-Message-State: AOJu0YzB9rBGfZAXL2HXB76+MaXED7bVUfRH3Lpndm8Q4y9GRZnWNLgq zI1VJjXyC1gDr5V4Mea1UUjDzWRD5gGivOpzj9FavctRO2Pda47lJPlwTmHtz1RUkgTdp+sr9WP jHPgoRnP59w17VL42JSjDNTVlovE= X-Google-Smtp-Source: AGHT+IFhuDrUuAIaLprKGCMnkYvkdJ7xMDPkYihA099ElTqnhewJF7rPpzY+SAsjsnx51wHA9zO5EDGDguIS8RDugZI= X-Received: by 2002:a05:651c:1a2c:b0:2ef:2b56:36a9 with SMTP id 38308e7fff4ca-2ef2b563725mr68364231fa.16.1721746947037; Tue, 23 Jul 2024 08:02:27 -0700 (PDT) MIME-Version: 1.0 References: <50360968-13fb-4e6f-8f52-1725b3177215@asahilina.net> <20240718131428.GA21243@willie-the-truck> <20240723145214.GA26403@willie-the-truck> In-Reply-To: <20240723145214.GA26403@willie-the-truck> From: Ard Biesheuvel Date: Tue, 23 Jul 2024 17:02:15 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: LPA2 on non-LPA2 hardware broken with 16K pages To: Will Deacon Cc: Asahi Lina , linux-mm@kvack.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, linux-arm-kernel@lists.infradead.org, Catalin Marinas , ryan.roberts@arm.com, mark.rutland@arm.com Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: 1o5rxjbuem8teo6kf5jb4fuympmcj5kx X-Rspam-User: X-Rspamd-Queue-Id: 0A194180143 X-Rspamd-Server: rspam02 X-HE-Tag: 1721746953-448341 X-HE-Meta: U2FsdGVkX18/1KzkFLuW+wSq23PeNA7aXINa+/admIljNb8xRDa1M9pt4VxPs+ArHERZs1w+VnH7Tu94wI4dDe7kMvsoPZtnT5PrNF5l5qdPiRp8k2F5rZQgEeh/HtxlzAD71uVEc/0Q/zs365Gny2BncHCOZIXGL0A5c02o+Nnf9SqwoSPyE9rMQBp4STJ/3tTo/0EEb5SUx3IEmNcXMOfUHCoOMR8VdX65sCblyXlf/vCm3+GfoohAaEN3mavvzzbSmxWiOxO/2v137IjfBlo2clEvOVOOeCUITSRZ67nO3HRGgBFw3pfJ09jyK7R62caiSGkbYrg2nurPMTJZI/EERz2YYRatS1XSp1scu32dbvrqWRcCJAAlqUbSb1RMAmYhNxxhr8UH+BZb4WUavF8IKrAc+cXm2rWN1TLpxYKjXt5WwNgD+diOBtFpURimovMNy2fx696EVaZe/+oQI/lUZ5rFxRDWemJllT+U81Advn0s7fqh4+pIm+Qer5WkG4P0nehmllNw1umLFE9Gbqswdx4DmOTtRNhGBXjFqPX2xCjVMm/wQUXixNbWdXUGF1sYnWlWSzTPJxeGW4twd5rtUepR2VParvOdklrju/zHi53QYiDO0yRIR9XMJ5faQZoCHDTecRDbnmEqaNy4ol6pbihU1gli2y1kAWIFcbqkaPKN6wG6KaM+C2kaYLHjswAFzzxMxJhZlOtlc8zqxPwa0YIw4QuSeg2YbHMLBQQshXtcQHz0M8x3K14tYg5EwnxHyS2A1wS6K6y9pZmJzzD8G+C4eoQzQdJf2tTqeXwmH2JFk6P/2D5SILugAxDwb+gxsWefioDGta+Ss+JB++MVoG1D77w1Mb7I0AqD6gokaaTr5kJU6V+PJmsn3ldH/ulx9OEoLG6WLxV+XtGWLC7TLy1TUUdU7fwwuY0bczxZ4/aB0tdrzKx2Ugb92lGGGQRyy3/bAjdclLVOFOs 14lI5r+9 5rvgPPQHlojPrsl5WYamk0AKO2zat4iJ4usiaCIiZeS+IU+FRupJTEv9PIa66guPGRd5d9THRpJ4v+Z6lJMDKJZbtq+qARKtol0f36QILwRXGVoWSdEjOuUlXGoWQmmyQgEpmBwemdcFEQZgpqXLWfqsG9ZIb4MFsyIYOngj4N+B/4x8Jvs2UkGivyoBJmtfPJsnnZ5EmO2Iin67ac3e01QBon7yWw+E5p+uM6mbRXmdDmaTSVGYmzaJtXBjXJyrcUC/Hk6xNnHRe0RYDYUcN2NjLQhNIZoSR9X1lFxmJqtBtsrH9Cyu8q7nl4g== 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 Tue, 23 Jul 2024 at 16:52, Will Deacon wrote: > > Hey Ard, > > On Fri, Jul 19, 2024 at 11:02:29AM -0700, Ard Biesheuvel wrote: > > Thanks for the cc, and thanks to Lina for the excellent diagnosis - > > this is really helpful. > > > > > diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h > > > index f8efbc128446..3afe624a39e1 100644 > > > --- a/arch/arm64/include/asm/pgtable.h > > > +++ b/arch/arm64/include/asm/pgtable.h > > > @@ -1065,6 +1065,13 @@ static inline bool pgtable_l5_enabled(void) { return false; } > > > > > > #define p4d_offset_kimg(dir,addr) ((p4d_t *)dir) > > > > > > +static inline > > > +p4d_t *p4d_offset_lockless(pgd_t *pgdp, pgd_t pgd, unsigned long addr) > > > > This is in the wrong place, I think - we already define this for the > > 5-level case (around line 1760). > > Hmm, I'm a bit confused. In my tree, we have one definition at line 1012, > which is for the 5-level case (i.e. guarded by > '#if CONFIG_PGTABLE_LEVELS > 4'). I'm adding a new one at line 1065, > which puts it in the '#else' block and means we use an override instead > of the problematic generic version when we're folding. > Indeed. I failed to spot from the context (which is there in the diff) that this is in the else branch. > > We'll need to introduce another version for the 4-level case, so > > perhaps, to reduce the risk of confusion, we might define it as > > > > static inline > > p4d_t *p4d_offset_lockless_folded(pgd_t *pgdp, pgd_t pgd, unsigned long addr) > > { > > ... > > } > > #ifdef __PAGETABLE_P4D_FOLDED > > #define p4d_offset_lockless p4d_offset_lockless_folded > > #endif > > Renaming will definitely make this easier on the eye, so I'll do that. > I don't think I need the 'ifdef' though. > Indeed. > > > +{ > > > > We might add > > > > if (pgtable_l4_enabled()) > > pgdp = &pgd; > > > > here to preserve the existing 'lockless' behavior when PUDs are not > > folded. > > The code still needs to be 'lockless' for the 5-level case, so I don't > think this is necessary. The 5-level case is never handled here. There is the 3-level case, where the runtime PUD folding needs the actual address in order to recalculate the descriptor address using the correct shift. In this case, we don't dereference the pointer anyway so the 'lockless' thing doesn't matter (afaict) In the 4-level case, we want to preserve the original behavior, where pgd is not reloaded from pgdp. Setting pgdp to &pgd achieves that. > Yes, we'll load the same entry multiple times, > but it should be fine because they're in the context of a different > (albeit folded) level. > I don't understand what you are saying here. Why is that fine? > > > + return p4d_offset(pgdp, addr); > > > +} > > > +#define p4d_offset_lockless p4d_offset_lockless > > > + > > > #endif /* CONFIG_PGTABLE_LEVELS > 4 */ > > > > > > > I suggest we also add something like the below so we can catch these > > issues more easily > > > > --- a/arch/arm64/include/asm/pgtable.h > > +++ b/arch/arm64/include/asm/pgtable.h > > @@ -874,9 +874,26 @@ static inline phys_addr_t p4d_page_paddr(p4d_t p4d) > > > > static inline pud_t *p4d_to_folded_pud(p4d_t *p4dp, unsigned long addr) > > { > > + /* > > + * The transformation below does not work correctly for descriptors > > + * copied to the stack. > > + */ > > + VM_WARN_ON((u64)p4dp >= VMALLOC_START && !__is_kernel((u64)p4dp)); > > Hmm, this is a bit coarse. Does it work properly with the fixmap? > Good point. I did some boot tests with this but I'm not sure if it is 100% safe with the fixmap.