Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp127724pxb; Wed, 20 Jan 2021 03:02:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJzOgQX/TNSE5yi8ZAEWq/nxOyxDxwUldokzyn0Pgz5cjszwIBWc3iOPCPByPiT3zl/4XcN8 X-Received: by 2002:a17:906:8609:: with SMTP id o9mr5769529ejx.241.1611140549845; Wed, 20 Jan 2021 03:02:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611140549; cv=none; d=google.com; s=arc-20160816; b=RRXGqI7KiekUZa04VA2IBne6tA7hZ6LbKUyTzmeCyw0wdTiIJb/TAJgCUpd8DnIWjg UP2mU0tQPVY0eDO2d/CT+uvFWmaDAET4l0saW+0FiT5XJkWuTqvX/d0En3SCvsKxN0q7 fdVodAjga0FFB6m7w+Tbm4bPyGprq4OO+PyvJY3DDEWu96Vc1lZHAjB8iRoGjLQuAwM4 UNumxbyqlhiRxsniJLJWf4cgnPRPivVS5pJLVp3I3e204XvjoWUO5nAKeZsan7DY4v7n X/QpxXfnmvcQl8XhFUex5Uf8rPawnSog8ahB3/HiF9yIN++dKlgEmqC7iSXgdZl8H3/C rz7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=lF5TRUlxO0/lcaCIviV9+0NlF5loPXJ3PpB7tlHczWw=; b=J6pVESjZT4Y/cfAF6qlpif+jHVCkgVh/kGXd+9BlK9myfaOGYpPmjgN0Bc3HdgwW6a xU/Xdzyn22BOSB+kBnbBQ0BluljOWgzGaahQmV6TMdsfNshkVRjMLQT7abfIp7E/AWS1 SXRkLF/WDo++DlON+WH7xm3u2I9ONXajvmR8UQndpNg8wpJdniYYMHpQXku+yZqVXkaa TuLELYsYEkj1DrmZTMYcTyTJq3OMKcwo1l48XLieGPxqhDhN9omSk+Y7zR2bU46ipeQh ZLNXmCkLnDWHVo8ZEzV0jA0Bx+/TlXgQOFfSi9OOwIrqycA5gJGJq4eZ/8ahU4WqGJ6b KEdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CCjy0SVb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bs12si517495ejb.127.2021.01.20.03.02.02; Wed, 20 Jan 2021 03:02:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CCjy0SVb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732207AbhATKRo (ORCPT + 99 others); Wed, 20 Jan 2021 05:17:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:43144 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731474AbhATJes (ORCPT ); Wed, 20 Jan 2021 04:34:48 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 226462333B; Wed, 20 Jan 2021 09:33:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1611135235; bh=O0F6nt2qctM6JqozhShXeHZqpZiqCUOY7pO9GCVxo5o=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=CCjy0SVbpAtcByDd1IMtGHULHpLqtMb8gFB5aY1lDIayFYaFPEPbjuDW8JNWwnoeb L2UP27ytyXnviDUCM+Uldc00x60S7SqE/P7joTTZrbhK0v4RYmmwjdQRz7q+aw2vnD VIQI1Q+CKb+D9tgWzfBTkQHmBo0O0z+540uuxs2OoUfPxgjwH1BKEnd4RmMepa9mVO tE0SgtMQxTPPskS2ztxwsvHN/UhHFV3cRmhE16G1yfO23bDSUEkJzJQUZxPKOtcviX 8C0zatABwh5r074MkfjRiR5HJb83QMqTUBvBSo9mqMItbdcjJGQ//nVWg7DQmXTuyq hX1jqMTcqnb8A== Received: by mail-ot1-f46.google.com with SMTP id 63so323602oty.0; Wed, 20 Jan 2021 01:33:55 -0800 (PST) X-Gm-Message-State: AOAM530l+kZtB+M8tXKhOwzPuCXLuncr1tciMmJ5rWCQqqF/wnCu9ZIu UXAynud88xoL01gAZ9yUwLvXE+T229EnibRCSm4= X-Received: by 2002:a05:6830:1614:: with SMTP id g20mr656140otr.77.1611135234286; Wed, 20 Jan 2021 01:33:54 -0800 (PST) MIME-Version: 1.0 References: <20210107223424.4135538-1-arnd@kernel.org> <20210118202409.GG30090@zn.tnic> In-Reply-To: From: Ard Biesheuvel Date: Wed, 20 Jan 2021 10:33:43 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] x86: efi: avoid BUILD_BUG_ON() for non-constant p4d_index To: Arvind Sankar Cc: Borislav Petkov , Arnd Bergmann , Thomas Gleixner , Ingo Molnar , X86 ML , Nathan Chancellor , Nick Desaulniers , Arnd Bergmann , Darren Hart , Andy Shevchenko , "H. Peter Anvin" , linux-efi , platform-driver-x86@vger.kernel.org, Linux Kernel Mailing List , clang-built-linux , "Kirill A. Shutemov" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 18 Jan 2021 at 22:42, Arvind Sankar wrote: > > On Mon, Jan 18, 2021 at 09:24:09PM +0100, Borislav Petkov wrote: > > > > > As a matter of fact, it seems like the four assertions could be combined > > > > > into: > > > > > BUILD_BUG_ON((EFI_VA_END & P4D_MASK) != (MODULES_END & P4D_MASK)); > > > > > BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK)); > > > > > instead of separately asserting they're the same PGD entry and the same > > > > > P4D entry. > > > > > > > > > > Thanks. > > > > > > > > I actually don't quite get the MODULES_END check -- Ard, do you know > > > > what that's for? > > > > > > > > > > Maybe Boris remembers? He wrote the original code for the 'new' EFI > > > page table layout. > > > > That was added by Kirill for 5-level pgtables: > > > > e981316f5604 ("x86/efi: Add 5-level paging support") > > That just duplicates the existing pgd_index() check for the p4d_index() > as well. It looks like the original commit adding > efi_sync_low_kernel_mappings() used to copy upto the PGD entry including > MODULES_END: > d2f7cbe7b26a7 ("x86/efi: Runtime services virtual mapping") > and then Matt changed that when creating efi_mm: > 67a9108ed4313 ("x86/efi: Build our own page table structures") > to use EFI_VA_END instead but have a check that EFI_VA_END is in the > same entry as MODULES_END. > > AFAICT, MODULES_END is only relevant as being something that happens to > be in the top 512GiB, and -1ul would be clearer. > > > > > Documentation/x86/x86_64/mm.rst should explain the pagetable layout: > > > > ffffff8000000000 | -512 GB | ffffffeeffffffff | 444 GB | ... unused hole > > ffffffef00000000 | -68 GB | fffffffeffffffff | 64 GB | EFI region mapping space > > ffffffff00000000 | -4 GB | ffffffff7fffffff | 2 GB | ... unused hole > > ffffffff80000000 | -2 GB | ffffffff9fffffff | 512 MB | kernel text mapping, mapped to physical address 0 > > ffffffff80000000 |-2048 MB | | | > > ffffffffa0000000 |-1536 MB | fffffffffeffffff | 1520 MB | module mapping space > > ffffffffff000000 | -16 MB | | | > > FIXADDR_START | ~-11 MB | ffffffffff5fffff | ~0.5 MB | kernel-internal fixmap range, variable size and offset > > > > That thing which starts at -512 GB above is the last PGD on the > > pagetable. In it, between -4G and -68G there are 64G which are the EFI > > region mapping space for runtime services. > > > > Frankly I'm not sure what this thing is testing because the EFI VA range > > is hardcoded and I can't imagine it being somewhere else *except* in the > > last PGD. > > It's just so that someone doesn't just change the #define's for > EFI_VA_END/START and think that it will work, I guess. > > Another reasonable option, for example, would be to reserve an entire > PGD entry, allowing everything but the PGD level to be shared, and > adding the EFI PGD to the pgd_list and getting rid of > efi_sync_low_kernel_mappings() altogether. There aren't that many PGD > entries still unused though, so this is probably not worth it. > The churn doesn't seem to be worth it, tbh. So could we get rid of the complexity here, and only build_bug() on the start address of the EFI region being outside the topmost p4d? That should make the PGD test redundant as well.