From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35B8E3AEF2F for ; Thu, 19 Mar 2026 09:06:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911167; cv=none; b=RoRJblljF7oE/9Glfr5RiFGyl1VTJfYNfCGCHt6GQuiBJnODshLXSnLo+BKzNI37OOX4FUzS0IbfNEm/ESn06gYkaL27Q0sYbTBVYm+EYyU6ggc/ppbexCE14Ufh6B/oOXTNEDiwgsP7Uqg4P71re103C6n6qHx5x3tLy3jhOxg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911167; c=relaxed/simple; bh=K7LzeW+1GNQObLL0mpttPEC6ObCBqOhm8uDhYnRS0oQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hLdtnGUleKdM10I6GvG7g5479F+G5+BKb2qPNPSdFzCIZuumeE5+hczHRiFRs1PBvS6gyC28hpabmXKpANerRp412z5Vzgzh05WUikqqVfb3ZbB0XGqZxY68iqsDRJtaxpP3WSTwx1MGx6WHasL7B2cpahghAq1oFTAC+Gyput0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jsiFOTtG; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jsiFOTtG" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-485397788b3so3467755e9.2 for ; Thu, 19 Mar 2026 02:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911162; x=1774515962; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=VsWNL3bo4KHMv0mdy45EiTzsJhVneOBuPnWT5ACRpoE=; b=jsiFOTtGp8q4u400w/Qr6v8soCLI7YXIspFv4Cc4UtmBv9NTDcN+0nrnzV8wc0lV/a J7EtYcyOikuHo4H56Mc10VqKIbJl4GiMsCM/oPf3VtFBfUkUdOBcQ5X9OyIXBgUmXEUO cQ+1vXvSKjZBvj4DQtTxlGSWT4gfbd3QnWjEmA1iQgvdALWCtmiiAOHsgbakWMWOK2gu c4HwZaDvu6+Wi3xRHFBzH5zkXwENmsfnCHSssN5SJ+1xZ4r0l+tuqL6OfJumAMgy4co3 5gwDVmaa6dlMzb5cziNkbdbwhqDClvYlfX+l8PfFtzhYGSIuhSTxtpxzEfg3SSuDMJib cInA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911162; x=1774515962; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VsWNL3bo4KHMv0mdy45EiTzsJhVneOBuPnWT5ACRpoE=; b=IiPTVoD1ZyBvVEuTOj25tH8ETwC/bEAmVOPbLjrvfmWG3oN5G/CVhAGFxdAhZ/4Hzm xOAKxuzceY7k6zg1ZWIxAXpSzljxvT+F/4uQDVu+lHPbKtaEdL0LXcYdQh7ANcM/v0Zw x5RKL2Lws45VFkrcvHFy4B6QNCSdAsM+NWB3Y6WodnyMPXpPXAc916ljsA3xgbqdT5Fb EL6ee+8MGXByW/DqlFhlPXukwBz5HyXmxSG+qf17B/j3J0P9zRA2ZKw+4tGbP9Zke1pb 8hYHjmb3ni0NMEkrArNzo5YMKV5KYZ2hOXuAiXcwg82lUgbCvN3DYBVKCQfdNbwebato +nBA== X-Gm-Message-State: AOJu0YzsiMSKAxadnjRpFi6PIeJrLxHzqAN8R57do8u5mpMQZMkK/ZT2 hWUmHj0GgNTLRdL+Ksn/gablGJOEoFfCp4dxH5GHtOdkDSavvav7FnKIh/yCjynP5/8bzukN495 hAnAnQXrh9cVirsU+1H4o381X6FQ4VeSU6zeO0rO7IA/o2RSz+6fu5kiCotKsH3WllvIQd5D8zK 6B2n5zsWcuge/5uw7aSeMsNQTP/wKt2fgPXg== X-Received: from wmro18.prod.google.com ([2002:a05:600c:3792:b0:485:3f57:3523]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1d10:b0:483:a27e:6706 with SMTP id 5b1f17b1804b1-486f442647bmr110126145e9.9.1773911162159; Thu, 19 Mar 2026 02:06:02 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:31 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2068; i=ardb@kernel.org; h=from:subject; bh=p/YATWOCB2qrU14En0xTPnxEAeYpYsyvR5e6jBr/J4s=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3nmit/rWqL44+ntaztsZOhY0/z03H3OZumPCXWU/CD fp2m4h3lLIwiHExyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgImIbmT4nzJ3kfBnE5kNvPZf 484G73UR5WNYuWfTT+PbVVe643IzlBgZDtY3aPkr7mjf0LNE3E3p3PTdN95fcbJ7mXVs8ol3f6K 9OAE= X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-22-ardb+git@google.com> Subject: [PATCH v2 01/19] memblock: Permit existing reserved regions to be marked RSRV_KERN From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Permit existing memblock reservations to be marked as RSRV_KERN. This will be used by the EFI code on x86 to distinguish between reservations of boot services data regions that have actual significance to the kernel and regions that are reserved temporarily to work around buggy firmware. Acked-by: Mike Rapoport (Microsoft) Signed-off-by: Ard Biesheuvel --- include/linux/memblock.h | 1 + mm/memblock.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/linux/memblock.h b/include/linux/memblock.h index 6ec5e9ac0699..9eac4f268359 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -155,6 +155,7 @@ int memblock_mark_mirror(phys_addr_t base, phys_addr_t = size); int memblock_mark_nomap(phys_addr_t base, phys_addr_t size); int memblock_clear_nomap(phys_addr_t base, phys_addr_t size); int memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t size); +int memblock_reserved_mark_kern(phys_addr_t base, phys_addr_t size); int memblock_mark_kho_scratch(phys_addr_t base, phys_addr_t size); int memblock_clear_kho_scratch(phys_addr_t base, phys_addr_t size); =20 diff --git a/mm/memblock.c b/mm/memblock.c index b3ddfdec7a80..2505ce8b319c 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -1115,6 +1115,21 @@ int __init_memblock memblock_reserved_mark_noinit(ph= ys_addr_t base, phys_addr_t MEMBLOCK_RSRV_NOINIT); } =20 +/** + * memblock_reserved_mark_kern - Mark a reserved memory region with flag + * MEMBLOCK_RSRV_KERN + * + * @base: the base phys addr of the region + * @size: the size of the region + * + * Return: 0 on success, -errno on failure. + */ +int __init_memblock memblock_reserved_mark_kern(phys_addr_t base, phys_add= r_t size) +{ + return memblock_setclr_flag(&memblock.reserved, base, size, 1, + MEMBLOCK_RSRV_KERN); +} + /** * memblock_mark_kho_scratch - Mark a memory region as MEMBLOCK_KHO_SCRATC= H. * @base: the base phys addr of the region --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B544A372673 for ; Thu, 19 Mar 2026 09:06:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911168; cv=none; b=ZMcwgykPd44iSrguhpBVyVvT6orI4+g6bI+QvZD3x6tX0PVF/YNJj2kuqFSFH1GXqUEYaWV5pMmHhOoOoO2e1pv6K4e+qC4jJFBmN70/DX540njNMhEknHhydtuRjbpPX1FI1MvuZwttueWp/xuXfOqiX6VNuqKylA7LWW7MTBU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911168; c=relaxed/simple; bh=r9Vz9NqghlFORjUZYdo1Ptfh1K/PtuFgYpapuy/P9kM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nH2HUQgquAWvXnEsKcQEfI6F1+S4elrv9M/24zTwbZFw4RyfJXHLwioNpFNTN5ubRPY73uMTaNy+nzll0BZ6ogZLb2thhTuo9KmBnAvlHz3Tu9VStyPGm57Hi5QetESFTPF6d7+/q4Ab4pN35gqG5sftkKw0GN/efP2lg7J3j6w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SSHxAAo6; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SSHxAAo6" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-486fc42c83aso2163115e9.0 for ; Thu, 19 Mar 2026 02:06:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911164; x=1774515964; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=E1s5SdGot5lUTMBrHwcD4YZ4DKEFwdicOeXDJpxhF+0=; b=SSHxAAo61vpVdjF7yGxOrrqe7JvAw6Ub5PtQ7tH4btvDRy61HydXzaG0Xmgihw7nL1 xYJu4In3ZHI1Koebo2NP2xBvTCveSlV2liw3oShiYC8Y1hY6CEcVoPVhly1AAfA7qIAu eguAtVhFrTAc2YOtFIi3tgw1utFlu4ipSKxKy1WFfkRTq/bDWf+XpZVGutsmRQMClFnl m4Bm5DBwRBH0NgKlce5wAo+dSNx5T5530RrFAAMyK8VdH1MpRwHp5S/sSh9Ka9hXfzTM 9Bky71FmigjwHOUX443LM2N7BtbSXfgLmlJWOGdAzN+PP/IRWZ1VGkuyZbOAlieBkUfU RV2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911164; x=1774515964; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=E1s5SdGot5lUTMBrHwcD4YZ4DKEFwdicOeXDJpxhF+0=; b=OBcBO3rp2r3WPEc718OaXylysx078cHUr+NzH6T5c9mY1zSgMV9mdPqIPTWXcKAalE JM+kMXZkYT4UoxIxVD9PvzJmBJ34A1rfBO6D1I/iLpY1kVQmI45j9KGXNpVU1xp1kIcM cYTfTbT5a7kIKVJM7ZMYz0oc1E39EpHGYqkBRe6bb6Ze4uqsj6We1IePCYqNeTrYtqKT rrL3tW+0JTfKwpRrWuTdrt1WyQKqVzv92Ka2UqMoBazuAnWcZz5Gvws3UQoNmGH5guu2 7UaAxT11gjRAHvcrZ6+/WE5BGOVlqEmsHQG9M6Rb8Iz/Npu3wHLYU8+3LTKasEicsmt8 ST/A== X-Gm-Message-State: AOJu0YxLih1bcXCrb1xk+4GshsOIvzEGtKEwt8adDx7i2SQhdEkwBrSR Qy0YnoQkko8Dr1Eg4nO+prlo+BLcobNhjYTuC4d5G1Uy6fv/QhXln/61znGI+dLMJbE6LzY7eIk eWHgBNJudQtVFTQ/kK7Vno33aT5Rz7NSRDryAQU3REnTXjl0B++JKtXLUNP9Yyq7ppXAclQLzx3 Cm/MO1sg/KPzKpe7hIn1WJPMNAPMffWQz2dA== X-Received: from wmma23.prod.google.com ([2002:a05:600c:2257:b0:485:7f49:ccc1]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a7b:cd95:0:b0:483:71f7:2782 with SMTP id 5b1f17b1804b1-486f44462d6mr74588345e9.12.1773911163415; Thu, 19 Mar 2026 02:06:03 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:32 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1630; i=ardb@kernel.org; h=from:subject; bh=nZezo0cCrAQqX5A/WjlHKfqCVN4vt8TmOwEAeiC7jRc=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3npgUw2jm3yzx3nkNP3avXZWxsf24iPP35vJY/vzlc n48Ig86SlkYxLgYZMUUWQRm/3238/REqVrnWbIwc1iZQIYwcHEKwEQWKTIybLvO/HDT6Zu5B/en Pv5Zc1eq7ZPkznu/EqV5X2QdnPY0ZyEjw4ZPF/InJmTzJHkqzLg74/DidHaPksWVspelq89IrL1 4ghsA X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-23-ardb+git@google.com> Subject: [PATCH v2 02/19] efi: Tag memblock reservations of boot services regions as RSRV_KERN From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel By definition, EFI memory regions of type boot services code or data have no special significance to the firmware at runtime, only to the OS. In some cases, the firmware will allocate tables and other assets that are passed in memory in regions of this type, and leave it up to the OS to decide whether or not to treat the allocation as special, or simply consume the contents at boot and recycle the RAM for ordinary use. The reason for this approach is that it avoids needless memory reservations for assets that the OS knows nothing about, and therefore doesn't know how to free either. This means that any memblock reservations covering such regions can be marked as MEMBLOCK_RSRV_KERN - this is a better match semantically, and is useful on x86 to distinguish true reservations from temporary reservations that are only needed to work around firmware bugs. Acked-by: Mike Rapoport (Microsoft) Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/efi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index b2fb92a4bbd1..e4ab7481bbf6 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -600,7 +600,9 @@ void __init efi_mem_reserve(phys_addr_t addr, u64 size) return; =20 if (!memblock_is_region_reserved(addr, size)) - memblock_reserve(addr, size); + memblock_reserve_kern(addr, size); + else + memblock_reserved_mark_kern(addr, size); =20 /* * Some architectures (x86) reserve all boot services ranges --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A874B3AA50C for ; Thu, 19 Mar 2026 09:06:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911168; cv=none; b=UfhmaBLGZbX27CFR5xYzBISphzmKOX3RWDaPJINwHnbcQSWy2JNSif8GvPIgvAcXhYbgoY32H0NHI6RCombfKLW/fDeETwUoD2cPt+3wxQyB9CbfAZ7Uzdh/Ek/uwWOw9nabol2dAV5ajtRCOrOrqqRK56bpaPhQmWL4fHjNGP0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911168; c=relaxed/simple; bh=SnsylKaR6XKsL6r4nGeFPuFvYgu+US9QjRHnBUE+4rc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dH2YqDCkUqDYPHma1Hv54v7YgdDGqiWioty5MbKRHX0UuIiirZU1BSxHVWvLnmVr3ajLRTybpdObTpCWng51/DOLPn+D6AlFmTBaTZmnEiGQKy7W1ivTdpWI8pD36HpH3M4prnayJUgvfUDBiQP9P0EiW6tm7unDNsFuOZPihyE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=g+JloLkZ; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="g+JloLkZ" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-43b3a0d25d8so397801f8f.3 for ; Thu, 19 Mar 2026 02:06:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911165; x=1774515965; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=cN0DSOT4FMFHSpYhHh0KxRkDumLq71iDg3aOibYQgUs=; b=g+JloLkZnfVRXXdjfHQVG4MYYYRXgsGdeD1vQrkDP+5yc5xsAeQIhmix8fYr4VbfDv MSLc7jUeRDo5kj4FNRQHWPdzmPSjdyQLhBgdySGuFUHG0Wck2d5ihLJUTBYrzxV5ACSo 1R8X7Vd4Zkj7DWE4wBT5H0t6A9PnYFBzA7fV06t6eKO756dGZ7jayaNg+6E/rer11BH1 BGE5e4HSZCtowx6LVddKHhIseAZwI1z1EOhyTmyyfdj/N8WlUbQZxO+TP0GeeU6E4Z4A RmThAoynODf0978rSSTqTv2TtLGL2vdXMNvwe2svx6ZBnku7yh9MrQyLIRNmj7RtoLQ+ A0CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911165; x=1774515965; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cN0DSOT4FMFHSpYhHh0KxRkDumLq71iDg3aOibYQgUs=; b=XzlWC1ZK2jP642Qebn4XGyoSpQDf9hwMzkBjLulirLiOMjK3MhH3Z6pkQFsHSzTpp3 HPcKmQ5WLaDF4D2tloZFeOhghi6pU6NbFY0jYRokMpQho40e9LGJl331KWE841zZ1dRO E+NUQJPogOMG143vSKZgBtiCstcsEFMGgaA2UqV4LQrWuO0n/S1CoyUo9f3FOxBeur/Z erXquYWwbgmesfUMIfudQuQRy9yArR8iYpIrAmifrd3Ps8HGdHc/JscgJnRsSVXENflQ +W0cOYBU+guklv26eAjaUVyVk/KUnT0DvA0Q07m+lEipnbEk4ikm8KJVaRmVOKIZIIPC OfzQ== X-Gm-Message-State: AOJu0Yw95lieuzPBQoN6vNQjiRTipvPkAXxvhp1pQ4c3fiL0v55DacRO /D44Co5GVyDIE9F+G/UpsTN25DYauU5MO7uho5cCPM7QH519X417fiFp4k6mtLL/n66k0E3/e9D VQEdOvler9FDD6DlkBUbGeDVATL86AWavHtCpZUr1IKzebmCpp8ZraDXJi9FC7LQ8JOG76WYjeX XlYKe9NAmCEBL3EgL3MVMoObtFL+6LzEl5RQ== X-Received: from wmlu26.prod.google.com ([2002:a05:600c:211a:b0:486:f89b:7f11]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4e49:b0:485:4136:99a8 with SMTP id 5b1f17b1804b1-486f4441072mr95249125e9.22.1773911164511; Thu, 19 Mar 2026 02:06:04 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:33 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1532; i=ardb@kernel.org; h=from:subject; bh=JO9hOFF5DJXI3o8vMSXVUdCxa1qCtfQllbLj7lJACi8=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3ntgl865zO81VqV1yL3TyltBDlcfiFzrvC58rtMM7p uWkaPqyjlIWBjEuBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCRk1sZ/qnf1H3sV7+s+ahI UOeTy5sZLjgs6TojF8GyQ2PPwzNSL5oZfjE1PlD65qXyb10jR/dfiQ2z/Q3muL9WKPfS3vA1X+g kHwMA X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-24-ardb+git@google.com> Subject: [PATCH v2 03/19] x86/efi: Unmap kernel-reserved boot regions from EFI page tables From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Currently, the logic that unmaps boot services code and data regions that were mapped temporarily to work around firmware bugs disregards regions that have been marked as EFI_MEMORY_RUNTIME. However, such regions only have significance to the OS, and there is no reason to retain the mapping in the EFI page tables, given that the runtime firmware must never touch those regions. So pull the unmap forward. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 35caa5746115..30b8012eafaa 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -442,12 +442,6 @@ void __init efi_unmap_boot_services(void) continue; } =20 - /* Do not free, someone else owns it: */ - if (md->attribute & EFI_MEMORY_RUNTIME) { - num_entries++; - continue; - } - /* * Before calling set_virtual_address_map(), EFI boot services * code/data regions were mapped as a quirk for buggy firmware. @@ -455,6 +449,12 @@ void __init efi_unmap_boot_services(void) */ efi_unmap_pages(md); =20 + /* Do not free, someone else owns it: */ + if (md->attribute & EFI_MEMORY_RUNTIME) { + num_entries++; + continue; + } + /* * Nasty quirk: if all sub-1MB memory is used for boot * services, we can get here without having allocated the --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D069D3B0AD6 for ; Thu, 19 Mar 2026 09:06:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911170; cv=none; b=j9XBlZJbVMVgip8xyinAqtyAQUlkos8jhMbEc6MPcwEN8g1DxitPNSGaprvO+TQSzCMWGRMtrThbw8Q19hN3jCUHi0cpKfkHJRV6foYwXfGaaz6+3EsUjz6TROkNLHdrg8PfNh6YH862gt42ZsmuAmyJoLzeTbTOc9GkW4aMySs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911170; c=relaxed/simple; bh=854LtT0Mit761SGh8E/7U5q38tsJmJYsa8qexFXvjoQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aRY2uDjY+R8M+2OQ1NlA91ejjRGYc6uke9sWV8wJxLWaxHzRX/quQ4WkrbwRnuzCWLnL89eGEJC8JgbdSQwOxiHnfHWs964y2k/RfVC3S045tPsFZAsl4Zg+lSl7OuoY65cYn7MvpPrT0yTR9ZkbuCSl7Y8oRgY9wT+oxVdsepI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=FH6Pz+OB; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FH6Pz+OB" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-485397788b3so3468305e9.2 for ; Thu, 19 Mar 2026 02:06:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911166; x=1774515966; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=1rmEEk9Gx/T0XLLjgZJ4axQ0xMTHf7COba7Cc07zn54=; b=FH6Pz+OBbjEGiru9jVigRmYuBbKoZAF5LAMzshtqk3qNWqb0t5FbiwKbh34OXmE7MN kgSuwHgJMDroWELH1rxPraqk0lGsPgtI50/6VtW9Fa5KqJUCiTCIqS3oh1Jlntdcqqkd Lm0iVRPOLFFzhTZss+8s4yX09/M2rvUyvRIIrfq1Vvc6kJwS4jZQXVLYKWcSU/YS3/Xc dNOihUwS59sRoxfDz+RayBItZ8P//ctKHbhYq5su+i6kf3kqiD19dlA7sGMsR0w2ocuL L7xFexAtasLC/KVpCXk8hEl9AY7iTdeXgvmst3qT3HY+wU5MloLnJdIKSImsTaWU6y/f PdbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911166; x=1774515966; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=1rmEEk9Gx/T0XLLjgZJ4axQ0xMTHf7COba7Cc07zn54=; b=mglpKBmGTtOds6ic3I8L77SDCd/mURe+WXos+dfzDD+w9npwbMWkFFw74RV6W8rKbh /MsVENkewgtd4C1kzbkCFSg6Ifb/JarlSGud0MMke4nHODTb4Skc5CfnaS7R3OgXOeZK Scnzi9kROfQlQF66LmmYwbFDDC8Q4DIcr1IomY1dZAGvpFNLTcqkb1vkOErLC0/+DhDw R/cFffRHlH9B9h5x7N+GUgAI/T8eFZWFbIlZ/r5DeS/sZ0ysXKRJfjUJYeeuzXLLfsbo 8RDsZOp5gzuKQsab+UJhBidgUjw5aTx5UwNXQv5mWuLcWblSxGNU+COcJoNIJO4ZzmyO XrKw== X-Gm-Message-State: AOJu0YzftxzhTxhgAYv984rEndJjrzpQ8EUxJBJ2/eiOshM9F4gs7W0Y SfjTtaTujGAQijZpItTA60bRWDhtUSa2/qm5K6lQT587Lnr6xEmDfet0C/qMACmNtOlZLnRjCCo 0aYXkN+PxlX3gpUd6ZLUUDClAikuidt6kUTu/iktn+5WsY4Dw/+pPLP3pUMmAdaULZ06XyR8zuL NBetZ1zEBWo0oW9wgmwPYnon1hKAi5St37nA== X-Received: from wmbg28.prod.google.com ([2002:a05:600c:a41c:b0:485:2fba:7a70]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8b31:b0:483:78c5:d743 with SMTP id 5b1f17b1804b1-486f4460539mr107542135e9.28.1773911165742; Thu, 19 Mar 2026 02:06:05 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:34 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1981; i=ardb@kernel.org; h=from:subject; bh=f/opshs+6rg0pwskhV0dEYjgzCkmUcLDOcw5+/P+l0U=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3nnj2pwuzVmZPejLt++GGvp+mJ9xFnmuKznPq5pIIv SDP/ICto5SFQYyLQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEzkUS8jw4dfExJ2XXpzOKbA MPtig7f82gteR+IyJ6wplozfa6Hz35Lhn45gx9UTJh1NhZG2+rcFc2T+T9z61/SZZDjvzj+Hczw W8gMA X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-25-ardb+git@google.com> Subject: [PATCH v2 04/19] x86/efi: Drop EFI_MEMORY_RUNTIME check from __ioremap_check_other() From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt , Tom Lendacky Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel __ioremap_check_other() is called when memremap() is used on memory that turns out to be reserved. This may be the case for ESRT or MOK tables that are reserved via efi_mem_reserve(), in which case they will be covered by EfiBootServicesData entries in the EFI memory map. Such entries are created with the EFI_MEMORY_RUNTIME attribute set, to distinguish them from EfiBootServicesData entries that were reserved only temporarily, in order to work around firmware bugs. However, given that a) __ioremap_check_other() is only called for memory that could not be mapped using try_ram_remap(), b) on x86, the EFI memory map only retains EfiBootServicesData entries that cover a permanent reservation, the EFI_MEMORY_RUNTIME check is redundant, and can be dropped. This removes the need to set this attribute in the first place, which is desirable as it results in considerable complexity in managing the EFI memory map on x86. This is implemented in subsequent patches. While at it, use switch() rather than if() to avoid multiple calls to efi_mem_type(), which is backed by a hypervisor call in some cases. Cc: Tom Lendacky Signed-off-by: Ard Biesheuvel --- arch/x86/mm/ioremap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c index 12c8180ca1ba..2d0e1cfb9054 100644 --- a/arch/x86/mm/ioremap.c +++ b/arch/x86/mm/ioremap.c @@ -124,9 +124,9 @@ static void __ioremap_check_other(resource_size_t addr,= struct ioremap_desc *des if (!IS_ENABLED(CONFIG_EFI)) return; =20 - if (efi_mem_type(addr) =3D=3D EFI_RUNTIME_SERVICES_DATA || - (efi_mem_type(addr) =3D=3D EFI_BOOT_SERVICES_DATA && - efi_mem_attributes(addr) & EFI_MEMORY_RUNTIME)) + switch (efi_mem_type(addr)) + case EFI_RUNTIME_SERVICES_DATA: + case EFI_BOOT_SERVICES_DATA: desc->flags |=3D IORES_MAP_ENCRYPTED; } =20 --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DF676371892 for ; Thu, 19 Mar 2026 09:06:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911171; cv=none; b=JlyA8hgfMsYt+7NXd+N8k+CQH4ChIuv6Yfc8Q0EgeQVPW38WzKkBD77XiyFjf8QF+Bvmj1t7b5vBRtF+4WsKnCm3HjDiezFwsiIHlqCPHHAb6MmoxuIAXvN30wuLyECMBJtbC43oqnRtp18I0gLPxGAfo26/7plWOhZyER6XAZc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911171; c=relaxed/simple; bh=Fy9MOluLOggD8cfRnDqkDt/FGOVFD/iQX9eNsrTSNHQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=s4WXWjCLIHElqi/dvSKYbehCgXm63IVZXM0YHdANivTIGfIHvNXstlKVPqOMvymAp6Lu+HeEZhBR/+mendfZeR0CqrznQ9xlo89ibuXqGB2EQd1obwfh/gz9IMSV14yQxZR7loI6AvUsPJaBNnhSmz/CYI+386OzmTK/oCXj31c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ToGG9nbl; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ToGG9nbl" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-486fcc05b20so909215e9.3 for ; Thu, 19 Mar 2026 02:06:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911167; x=1774515967; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sMI4yfdxgjto84eW4zoc8n45SmfOaq24fXHg38YbzuI=; b=ToGG9nblDfKt/tYJvpgXrr9djoYo1iEsv2HOcAQk+ozUGPDk7HvPi5dJ4TLnR9n+Ou w4idhFo0cbmXnkkqJLK+k/Pep9C5AKFb0d66xj5VO90i1mMEqkaMJdtDl5I7rTWyFCjG ATBj+IyFaLvWlIOSu4Hu7gJf7LtNDZPXv4cIY+KpxfD8y5aPSEkvg1zFMkejw950uLxv 4RyHY4ZOrw216WzyE6QPHeU2UsiotjtfL5w0nDy28G6iJHt0qjzDG5Ztv/o1nyYvA7Bn JXU2k5eUoYBF5ldKeno7Y/cUPN70S8RdnFtHAru5ho/MG0s/rEeK19hc6cfXjHIPOjiU //kQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911167; x=1774515967; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sMI4yfdxgjto84eW4zoc8n45SmfOaq24fXHg38YbzuI=; b=FM6mgVLRiYpxSEdvFIScYGEVn9b64TIUfOttFLBgj6UBc93AyqTHcUxmPAhCw/utRH n/zZqT+TPb95E4wY8E9ynKpB7Yo5bWLYziatmlpbZOVdbbEYDFRhUnlPbE23No/oz/gC c3mBMKoeO+IqamqUCz5GCnKyS+TmU9JPvFX0sT/9IA80yGf0EobxZXPXA+eRlYrgz+Mg Mj2/wgbFupNplk2wfF8jAR02ZeaYIaWfH4N9ddAe8xW0y0n3HiaJ72l8pb/QPuvFZ8HY oU+2c49QYEtgFBgQyAA0ZFoKPllrBA5n2alXCD45tZajN0WQryvisx8cu0uLbTMxl9BD 2/0Q== X-Gm-Message-State: AOJu0YwPu1IaHNZCkSStJb3QwYEerZgjzFbGovobaJEH2FpIfga80eNI eMEL4KugV4bNYApqw8O6E4Eao7Lf8FMuYFFO3Z4VgtMN72NCR2CO3zCIoT2gEb47kucnQ+JaHfg EGEChLAPi9SXrUnC0kAr/RCmFgFl6DmTOYflRlg/MFXFbvvTujSkNw6RlwP15aj5Sai+7vcAOAc HPPBWU5gJr4z7hVNoK0nZIZc/lToGstk4Apw== X-Received: from wmbjx4.prod.google.com ([2002:a05:600c:5784:b0:485:4f4a:bd84]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1c0a:b0:486:f8e9:add5 with SMTP id 5b1f17b1804b1-486f8e9b51amr37917475e9.19.1773911166985; Thu, 19 Mar 2026 02:06:06 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:35 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2714; i=ardb@kernel.org; h=from:subject; bh=tRLOeo/RL3/RaVOez+iRRs5FLr7HogiqFZmpRAxB1M0=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3ngRZWQZRadEw5mmLFY6W3Fo9tc6Icc6pxvjeg73Lz qmcLYnvKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABNx4WFk6Gq757L275NjizT2 r1oitMVvmuaa9+9tbkgXvVm35UzmyypGhgd7qhRczY9bydet4GvSY928ufTO6qerZLX+5lQvvB5 izQ8A X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-26-ardb+git@google.com> Subject: [PATCH v2 05/19] x86/efi: Omit RSRV_KERN memblock reservations when freeing boot regions From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Instead of freeing all EFI boot services code and data regions that were preliminarily reserved during early boot to work around buggy firmware, take care to only free those parts that are not marked as MEMBLOCK_RSRV_KERN. This marking is used by the generic implementation of efi_mem_reserve() to mark things like informational tables that are provided to the OS by the firmware, but where the contents of memory have no significance to the firmware itself. Such assets are often passed in a EFI boot service data region, leaving it to the OS to decide whether it needs to be reserved or not. This removes the need to mark such regions as EFI_MEMORY_RUNTIME, which is a hack that results in a lot of complexity in updating and re-allocating the EFI memory map, which would otherwise not need to be modified at all. Note that x86 is the only EFI arch that does any of this, others just treat the EFI memory map as immutable. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 40 +++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 30b8012eafaa..906e29754026 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -536,6 +536,40 @@ void __init efi_unmap_boot_services(void) } } =20 +static unsigned long __init +efi_free_unreserved_subregions(u64 range_start, u64 range_end) +{ + struct memblock_region *region; + unsigned long freed =3D 0; + + for_each_reserved_mem_region(region) { + u64 region_end =3D region->base + region->size; + u64 start, end; + + /* memblock tables are sorted so no need to carry on */ + if (region->base >=3D range_end) + break; + + if (region_end < range_start) + continue; + + if (region->flags & MEMBLOCK_RSRV_KERN) + continue; + + start =3D PAGE_ALIGN(max(range_start, region->base)); + end =3D PAGE_ALIGN_DOWN(min(range_end, region_end)); + + if (start >=3D end) + continue; + + free_reserved_area(phys_to_virt(start), + phys_to_virt(end), -1, NULL); + freed +=3D (end - start); + } + + return freed; +} + static int __init efi_free_boot_services(void) { struct efi_freeable_range *range =3D ranges_to_free; @@ -545,11 +579,7 @@ static int __init efi_free_boot_services(void) return 0; =20 while (range->start) { - void *start =3D phys_to_virt(range->start); - void *end =3D phys_to_virt(range->end); - - free_reserved_area(start, end, -1, NULL); - freed +=3D (end - start); + freed +=3D efi_free_unreserved_subregions(range->start, range->end); range++; } kfree(ranges_to_free); --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BCA53B2FEB for ; Thu, 19 Mar 2026 09:06:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911172; cv=none; b=cBebwApJWe5egyveGFAl78t1l0JNLlTrwQ0010adBnGICyKcBABl9fUcCQYCE6Go10NO/DmJOGCxpBkjFySuZRsDynLZbt6v8GYbzqXc1xv6Rh+rElfEhcTkywX/A606pp4BBb273SoxE5WzTEeOGbg7hc0GLTvztxxnVxLUr8M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911172; c=relaxed/simple; bh=pU9XzhtWF/juNTzf+Kd9/yslgyVQiqAiygT2kcnBFrg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pl88IP90QOejCFzaaUoGmeuYt70OE4AlX+vY0o48gfoDgweROZJi4hFOsCOyr7aH5xw5+XtldmHn+dng3N7uXpAbca4T+k5CeFfV2mDbWUrqm7VhP0+tJZ+rIi+668JuRbWEm93XrXiQQjoReOz3LDAHHn2VILcT/hRbthjzClA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DCBDzp3J; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DCBDzp3J" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-43b3c9568a3so373925f8f.0 for ; Thu, 19 Mar 2026 02:06:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911168; x=1774515968; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=YGawXAqaadB5xFeamVCn/5S0jZogRGa3Ig3UqDmOVOM=; b=DCBDzp3JfKNW1kpC9lj9cE0vDuD9qIgZVJfAPFnlm6bnJWPNGoqv60KuLlud0v9Gq7 jDusbsgaX4oSGyMC73XZHn4OTfvkzVFSOD4xdkhJa+q78rYN5W3VOvNksC5jyX2nNbh3 vTLxZV7WnqMDUre3cD9s15ghr8sY8evDDt4ypvojJT1ExeSJxlRF986xoGrDPNPojh7X UsC26/CNc/StVIjkxQ5YLZtAT+bdJMDd2Oce6g0WZ/87daWKehNTuCTa6NjpfcKUOJik OUJDngT1FzKgis28VfXVzIKJBVZ9SjxUFM6KYZxMUXTOhs1hbzfBPTHS1R9JyJfUCIcr KGdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911168; x=1774515968; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YGawXAqaadB5xFeamVCn/5S0jZogRGa3Ig3UqDmOVOM=; b=VMwySQQz+vk31RFXLBCNAW8h7J/CQYMD0MYs1+06Zk364JnYK16PIdW3J+xZxTQZEm frTz8/fzmW88IGDocMxNrbQOc+jwfNnvGK/5RDVOI+a4UnRv13z7ZmizikG6JEgoPN2h TBN72Ck9tzZCwPdU8XQYR2NMnkTc9gdi+52BpegchNoKbbZQrW1fvaE5g7UkohWFq4Rz mHi+uj0yHDpQB+cA1e6y82tb57x7DNfdm0eloq9UFh2iYo5jifDDgiCrM2T8KPWSgZjp NLoWlIwR0S+LNm41NPQNgFoxQzwCb6TxPw6v8XgYIMKG7OxjSEN5zVEGpPnm92yZIMz0 38pQ== X-Gm-Message-State: AOJu0YwmZo8M+vDh8GP3Q37y3y73KRtG7lEWCfkI6wMn6217lq463QvE 5qpySqLY2Y+Z2pftz4ZlwTrLJJ1wUSlP9SbAmKOww5gDQuXz9K1X4Od/R39tS4KdIrMsreKAbyR kRGswfDBhVukDxWVHsBzWULvWv80CHpd+p1Y7wZgnyukd/SmfSOleIvZeclhX0apBziEf9U2xUW EhqjRmkIL/tIaLjIsN3U9ifevqQUDMqaqDRg== X-Received: from wrxa13.prod.google.com ([2002:a05:6000:100d:b0:439:b6b2:283c]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2512:b0:43b:47ee:4586 with SMTP id ffacd0b85a97d-43b527c2008mr10897294f8f.29.1773911168156; Thu, 19 Mar 2026 02:06:08 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:36 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1712; i=ardb@kernel.org; h=from:subject; bh=NdkNKC4V8OlXuyY5MZEVWounQEXYEBvCLAYHi0Q+s/Q=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3nsQ7cS/Kf2Qda1M9qaxzN5yrUvW45bwardb5q34uv ptutPtNRykLgxgXg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZhIDzvD/8ozkkLNiwqUNt8N aT4Tcm3Z18Sd93dXKKXyTpDIP//x62tGhueC7xz+CqwPbFZae+rL/wPfJ52Qn1TEqrDYpuLn3Y+ KExgA X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-27-ardb+git@google.com> Subject: [PATCH v2 06/19] x86/efi: Defer sub-1M check from unmap to free stage From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel As a first step towards moving the free logic to a later stage altogether, and only keeping the unmap and the realmode trampoline hack during the early stage of freeing the boot service code and data regions, move the logic that avoids freeing memory below 1M to the later stage. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 24 +++++++++----------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 906e29754026..25f51d673ad6 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -475,18 +475,6 @@ void __init efi_unmap_boot_services(void) size -=3D rm_size; } =20 - /* - * Don't free memory under 1M for two reasons: - * - BIOS might clobber it - * - Crash kernel needs it to be reserved - */ - if (start + size < SZ_1M) - continue; - if (start < SZ_1M) { - size -=3D (SZ_1M - start); - start =3D SZ_1M; - } - /* * With CONFIG_DEFERRED_STRUCT_PAGE_INIT parts of the memory * map are still not initialized and we can't reliably free @@ -579,7 +567,17 @@ static int __init efi_free_boot_services(void) return 0; =20 while (range->start) { - freed +=3D efi_free_unreserved_subregions(range->start, range->end); + /* + * Don't free memory under 1M for two reasons: + * - BIOS might clobber it + * - Crash kernel needs it to be reserved + */ + u64 start =3D max(range->start, SZ_1M); + + if (start >=3D range->end) + continue; + + freed +=3D efi_free_unreserved_subregions(start, range->end); range++; } kfree(ranges_to_free); --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54D813B38BA for ; Thu, 19 Mar 2026 09:06:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911174; cv=none; b=DNn7EypPcswiHT9fy03MGj9XkkB/6ikGWPLGyF//SZdPpWjvK7wXMqzcX/cxj7nP9yLH6fZv/Zx2FkMcFVZtkyCsxwv/W3K0wpV7aPa3nhlo7hJC2bS4Eypb0VxfRzpnFBIM9mnW6AJhSUWztwzAqBBDsC8uInQF0cUEIWjjjmE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911174; c=relaxed/simple; bh=KgBhK+TEZgstaXzTuRCtDnUCXmvs4UoSUv4TGHBNHUQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fZgP+UsX+9n++ZCzfHq7EwIO2xSskfyw/GHxEaz6tsUAXwj2ecm9QdkntllRDpgzd+KaSsqvG39hCQR1TvK1AI4rMAjKg/zPaknTBoIub42OpLM9xFNN1em/8h2P4f2TOo8y8pmbGlv47aOgImB1yaeNaq6v4mN9vEvcsOkztlE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=X5UiTZJ0; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="X5UiTZJ0" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-48542d5aa9eso7057835e9.0 for ; Thu, 19 Mar 2026 02:06:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911169; x=1774515969; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TjNiMrLy4tEOouEU93qnu1juEIPlINFhm86Rn+zMuwM=; b=X5UiTZJ0vnrJtqveqWnQtNKcY7RuBsZrLUKhrzyrog/H3mq1WEh3fWaSXi5XEX2+et UIyK9BDjAYOZmrthRgzJltcW1GUGvtsCrda7RHChmaAClGSGhuAKTsX3EDpBjr9klLk8 2dz56i8iXbH105h/TLo13uRMRMQtk+JCOWACZo/2vVhQuAbRNa7fQ58O8Q7dyctO8m2o trqYhbYBsc9zR33A1BDCc7ufNOcW0TnPPJi5Ah6BeDeAwMlZAPi56XHGpmPWX0bDl+B1 H1G693db4+bxS3zbwFPxmAhs0MiPWEfoK2o2JOwXYztzrZiRf0MwdzDtkm2z0DXFV7VZ 0+YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911169; x=1774515969; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TjNiMrLy4tEOouEU93qnu1juEIPlINFhm86Rn+zMuwM=; b=fDjuFPgB+mjUvcSRp2CmvSkhgTbFX1xEGMw1tzW/b+QN6WN0sNAoZmGFOelCOhWDZ5 vlbUOtWzUaOqJKtYFP1chIKXwcxDsq6QMJKhLRo6mDO5D6hE5kx7myXxTkQEajvkRy+s 5lc2D9cFO95zKEP4l3bjy/4KeCKFTYi+N7eKqlPsSTepVLaS2XtttNczllGJ3ZGCT4BG nLU7++UhszUnlCX+zqg7gx08d56KXwvaCZEpAvxlhWBANu2SfdGqVGUt3gi1rCx1Z1Ns aRI5P/GOjD7qjM1PBEaaJtcIJPjBfSUM8lfPicja8EOyhxdxv21b9+v9a/N5qJFAU5S7 +uRA== X-Gm-Message-State: AOJu0YzuG9ui2U+WVlaA7JJN6RpfJyPWD56tr8YX71MKkflR+DPfU6av +Bq0Svqrlb5wiOhQCf0ARw9u+3FTIUh6xQSWoXJCeYGaXvV7ZLz6r8YTxrAasC4q1M7DxYFT65S 9Jp2Pi9SARcMhMkh9MpZffipMiDJyH7K5lBGBwxmOtS5vERSYVtcXcY2zKhT0l+ANji7qib8KML 3aq/H08kJE1kufN3vTeHJEp/CDrKP+Rp8lWw== X-Received: from wmdd11.prod.google.com ([2002:a05:600c:a20b:b0:485:4f26:fe69]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:470f:b0:485:3baa:af14 with SMTP id 5b1f17b1804b1-486f44410acmr105060685e9.18.1773911169118; Thu, 19 Mar 2026 02:06:09 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:37 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=3749; i=ardb@kernel.org; h=from:subject; bh=gWD7ppoTqNzON1lfITM1AwwqfOFv6YDKqkDVs5WiapY=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3nmSxE7ZHrgXsmOXV77/IrFvpHu/7/X+U2sWYrqs4i nIzlC7uKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABPRn8zwT0tU+cKqWb7vpi2/ 0PRU8rv7ub22s7sbnnXMjTjjYFX70ZXhf2H21ojw7OD0uyWXdcXZD6+v14wwaF94Uvz1KT2/47N W8gMA X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-28-ardb+git@google.com> Subject: [PATCH v2 07/19] x86/efi: Simplify real mode trampoline allocation quirk From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel To work around a common bug in EFI firmware for x86 systems, Linux reserves all EFI boot services code and data regions until after it has invoked the SetVirtualAddressMap() EFI runtime service. This is needed because those regions may still be accessed by the firmware during that call, even though the EFI spec says that they shouldn't. This includes any boot services data regions below 1M, which might mean that by the time the real mode trampoline is being allocated, all memory below 1M is already exhausted. Commit 5bc653b73182 ("x86/efi: Allocate a trampoline if needed in efi_free_boot_= services()") added a quirk to detect this condition, and to make another attempt at allocating the real mode trampoline when freeing those boot services regions again. This is a rather crude hack, which gets in the way of cleanup work on the EFI/x86 memory map handling code. Given that - the real mode trampoline is normally allocated soon after all EFI boot services regions are reserved temporarily, - this allocation logic marks all memory below 1M as reserved, - the trampoline memory is not actually populated until an early initcall, there is actually no need to reserve any boot services regions below 1M, even if they are mapped into the EFI page tables during the call to SetVirtualAddressMap(). So cap the lower bound of the reserved regions to 1M, and fix up the size accordingly when making the reservation. This allows the additional quirk to be dropped entirely. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 29 ++++---------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 25f51d673ad6..c867153eab8a 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -329,10 +329,14 @@ void __init efi_reserve_boot_services(void) return; =20 for_each_efi_memory_desc(md) { - u64 start =3D md->phys_addr; - u64 size =3D md->num_pages << EFI_PAGE_SHIFT; + u64 start =3D max(md->phys_addr, SZ_1M); + u64 end =3D md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT); + u64 size =3D end - start; bool already_reserved; =20 + if (end < start) + continue; + if (md->type !=3D EFI_BOOT_SERVICES_CODE && md->type !=3D EFI_BOOT_SERVICES_DATA) continue; @@ -434,7 +438,6 @@ void __init efi_unmap_boot_services(void) for_each_efi_memory_desc(md) { unsigned long long start =3D md->phys_addr; unsigned long long size =3D md->num_pages << EFI_PAGE_SHIFT; - size_t rm_size; =20 if (md->type !=3D EFI_BOOT_SERVICES_CODE && md->type !=3D EFI_BOOT_SERVICES_DATA) { @@ -455,26 +458,6 @@ void __init efi_unmap_boot_services(void) continue; } =20 - /* - * Nasty quirk: if all sub-1MB memory is used for boot - * services, we can get here without having allocated the - * real mode trampoline. It's too late to hand boot services - * memory back to the memblock allocator, so instead - * try to manually allocate the trampoline if needed. - * - * I've seen this on a Dell XPS 13 9350 with firmware - * 1.4.4 with SGX enabled booting Linux via Fedora 24's - * grub2-efi on a hard disk. (And no, I don't know why - * this happened, but Linux should still try to boot rather - * panicking early.) - */ - rm_size =3D real_mode_size_needed(); - if (rm_size && (start + rm_size) < (1<<20) && size >=3D rm_size) { - set_real_mode_mem(start); - start +=3D rm_size; - size -=3D rm_size; - } - /* * With CONFIG_DEFERRED_STRUCT_PAGE_INIT parts of the memory * map are still not initialized and we can't reliably free --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 451693B27F9 for ; Thu, 19 Mar 2026 09:06:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911175; cv=none; b=nQc6J4FzygWABbjr1boPoNcWof/i2Pz9eOmbbXbCIsidjPPl0HApS094enrN/UnvJVYRUBAVLpYKVfX872T3R2LBGOvPUBsEljcnHxnmN+kjzwIF5BP7dwaJK0Ihf9mRuglJxN8XQvYoamz1o4FKg36eMizCmjXrZbG8mgoNUuQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911175; c=relaxed/simple; bh=exoya81jXoIK8WWhopcrulNyNM1oh6DkePORwiM7+yQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UhZTIadjv83eKnnndtSCv6GSqF7yvDJNPyAv0DAZEOj5aDa0YW7OKIWkHRLnfdf21z4gpcKou/fVBU8/bZt3QsKwbmcG4d/1ucGkiqKoliH1kSdAD99piqTMWiSCC5ZBiokoyHpApfazSSY8u7zyjmTrE7qwvL8n/pW80Z5wP2k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Zjgku3Ss; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Zjgku3Ss" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-43b53023d60so365085f8f.2 for ; Thu, 19 Mar 2026 02:06:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911170; x=1774515970; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NH8dUmPRO71CDP7E1Gavchx0/GEscmuj+FtvRs5QlzI=; b=Zjgku3Ss3KtBhZA9YrXCj93KOiQ9aVHZ0obliMYyg5nhvgeIeZyJye4PhfEKmMaQVJ iDsW4+QOiVukePrcEbrdymnGNrL8dSs3yPWG+hJdzHgocp9wjLRPcwU8xLrrEwNzZCiO i4cyJwx9Vw19TdyoBOv2x5vx9NHIrsjEvchJ2FpHS1xHR9E+D/6H41URmVr+SfrJZjhE wW8m0sAkSBIAURdK7a4NM38CDu8w11Ip6BmWEBOzMv1Sd009SehuSEhdVqEhoo/NHqKT ToKaN888dLoLOP/QaO7B8gV32pTP2s+o/GG4l6FOPtDrqVpCDUSpzdNHSIIV3KdQTHxB 4Lag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911170; x=1774515970; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NH8dUmPRO71CDP7E1Gavchx0/GEscmuj+FtvRs5QlzI=; b=Vfbwa6xL9IqJk8TskcQdLg8Kl7fYPcfj7Tlj+UnZB5rJjdZNTboAPMGPSIxTRqvWjg 7947dquzWr6tpTCfVE6ujX5sxep/MjjrcZ1KbLEHcQn+tU//Y6uBDA3nqSEQEaLOxyGa vuIW+cMcFYq+mk4Jc2TnAX/QoMiTJS8QedQGGttMEypFqjhLJ5S2J97xjlkQ8r60sNis FQcRiekmGMg5PLPuiFUivgu6IB7RhUulp4fncISV9BrmIV/Is0Qa008A+Dps0w5jeUaQ 1WynAgj7qEhibooh9gtvDAxa982DVJmPNtO7GMJyoX9hgpAtVda0wyvlka+sljFK7wQS gGqA== X-Gm-Message-State: AOJu0YyUix/M45R10HZubOIPJQW4uq17i6X+N4CobJBv+inA2K5qcVsq NWKq+IMMXG86iTBzmTG3dpDfq4ZzmD32SFUkvt5yxRGt9gBT2He/my8JAS/XqsFBCEIy+IaoRJq HjUsg0tomesUX5ic3KprAZ8TNLs0z0jelzzV1Lt+IW3LIWnLddKY7VknbDJxrrGnqyATF4e+ZMO WfIEICKMS2jjAOKhUl5vXEQA5ohG5GoFJH6g== X-Received: from wrwj9.prod.google.com ([2002:a5d:5649:0:b0:43b:3a9b:25c0]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2006:b0:439:b629:42d7 with SMTP id ffacd0b85a97d-43b527c6c7emr11257711f8f.46.1773911170062; Thu, 19 Mar 2026 02:06:10 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:38 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2084; i=ardb@kernel.org; h=from:subject; bh=u9C0lu/wWMOyoxfP7svUhbN70AoJL0lab86hPX1DebA=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3npSNl9qYZP+LJR/tDZqsYyh4rHH3urOxihcttfmrP s7kYVPvKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABPx28bwT2+GQvPLs5Fmohne 7xe83BNl0vTe28H4vtkq379RH/2bHjEyrGA8qe0yXyi0XHBe99cld9W23DQsrwn7fuDBrDun9Vm fMgEA X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-29-ardb+git@google.com> Subject: [PATCH v2 08/19] x86/efi: Omit redundant kernel image overlap check From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel The physical region covering the kernel's executable image is memblock_reserve()'d in early_mem_reserve(), and so it is guaranteed not to intersect with the regions passed to can_free_region(). So remove the pointless overlap check. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index c867153eab8a..13d9e036a23a 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -305,16 +305,11 @@ void __init efi_arch_mem_reserve(phys_addr_t addr, u6= 4 size) * can free regions in efi_free_boot_services(). * * Use this function to ensure we do not free regions owned by somebody - * else. We must only reserve (and then free) regions: - * - * - Not within any part of the kernel - * - Not the BIOS reserved area (E820_TYPE_RESERVED, E820_TYPE_NVS, etc) + * else. We must only reserve (and then free) regions that do not intersect + * with the BIOS reserved area (E820_TYPE_RESERVED, E820_TYPE_NVS, etc) */ static __init bool can_free_region(u64 start, u64 size) { - if (start + size > __pa_symbol(_text) && start <=3D __pa_symbol(_end)) - return false; - if (!e820__mapped_all(start, start+size, E820_TYPE_RAM)) return false; =20 @@ -347,10 +342,8 @@ void __init efi_reserve_boot_services(void) * Because the following memblock_reserve() is paired * with free_reserved_area() for this region in * efi_free_boot_services(), we must be extremely - * careful not to reserve, and subsequently free, - * critical regions of memory (like the kernel image) or - * those regions that somebody else has already - * reserved. + * careful not to reserve, and subsequently free, critical + * regions of memory that somebody else has already reserved. * * A good example of a critical region that must not be * freed is page zero (first 4Kb of memory), which may --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E9A03A7F61 for ; Thu, 19 Mar 2026 09:06:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911175; cv=none; b=UIWJiQnZ0YCth+68AKI8XsryZt7tNzl0bccL2bGQogExG1vJfGxsK4aLa3islqo+J2iH3zilXlrn8QtpyzKNA/5XyuORZGDXo9bf7/pooSNm231auf1p6r/rfFCV36rm61HqUKPv7GiujslijKZtFXWIhmwU5k8R/iaCfwOaCFY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911175; c=relaxed/simple; bh=HOt4tgF+H86w/ifeMUACxXFqRdDJ2s7lGOmb3GciFEE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nEQScv/Gl9/OqNl+P4nWadLOP0EwAYuygKCAFxOa0EKhkljU/848MgyJCw/+XFnpDejac9J3QdsZ6CMLhn5u5bzXQ7PCQFxGPMsb8+xVzeI0+vJDPkIdlgmZUh930qSQx9EQKAVCRJpJm3JZHIRsgAwAbodrbmybJtGI544UzZE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=S2Gl9e5+; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="S2Gl9e5+" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4853a9467c5so8676085e9.2 for ; Thu, 19 Mar 2026 02:06:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911171; x=1774515971; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=lEs3mmx0viyT5mPQ14qRORhKgCypSb/SW/teub5WiMg=; b=S2Gl9e5+UKiuBR/q08cJ4i0sl2GN1ulSf/5/WZRY/wKkUwnEnE4IHrPosF6KvbTi6q qdK2cFgG5/YWYENoacArCECUISVk9u1tev2Pv3y2C7jdvgnM9LsB6E+Fi9YaowSvJfbi RkHxY+SfapsbxYMFVl81yI/kkFrLsKxlnoSyJbKYp4rLg3hQQuqVlasSKoMmBCHNf/C7 NR4dXLCzMnG/yq+WR1OYABy+kGNaCZHK952tPTNbvinE3hNfuRBPPaJE3hVVtfw8ipCj u0C9W0o79vU6+DlRre/fqdqfOEHAmg2I4mHwdRpj/h9f0YEeSEIQgoeV2nwCOCiH51DY kmXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911171; x=1774515971; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lEs3mmx0viyT5mPQ14qRORhKgCypSb/SW/teub5WiMg=; b=DJonis+FJAE6+v8DStFyHMJVvFtuITq5VT53yf9ge2u0NUJOPVwMcPSOKe0GR7Os0d cx+ulBSI8xtdfqsQvGj7dsWmxPSO3F3ntGHOqTDqjqU6dxk6ya7fKEi9UXsENPmHvnaf mTZCvAZn4LKHF0GxbjraOyRSeQxDDP7kw/ZhRO+rqrXYPy2cV5TVEoBEuUDz97UdSFE7 IOSfDU3DgW/TGjV9bN+LBBn3ZijnSY3TDrYoFK+Cx8dofMKF0bIuZcYraxuG5DB/hyWq BfSQ4UzZ0vYgtvErIn2PyZaaAfTloeWJ4j/QLq1i+3HPina0mgY9JxqDciJkjKuqArE7 OBiQ== X-Gm-Message-State: AOJu0YxL1BW5/cRHzVlN2TbB4wrkhU7cmoybcDO8eu9gQA4Y/DFo0QT4 c6mHGw2H3JIm8UOD/snDzAB4yxfDduX8RcAUTIB/2evGW0wIXreigyPftMhtmfPOOpBmoQEdClx IUfEZ0cMne0U6stTN9aDT2YIVUPKDcVY4xHwgCnqSlaoTGmVhtFc6qL3EpvYf3BM4Kqfw/nggqH rZuy1tLsFNJ6M+0YWePf0Kj0Ub+5f08aQ01A== X-Received: from wmnk10.prod.google.com ([2002:a05:600c:168a:b0:486:fb09:30b1]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1e0f:b0:486:fb5c:3b20 with SMTP id 5b1f17b1804b1-486fb5c3c68mr21321045e9.13.1773911171394; Thu, 19 Mar 2026 02:06:11 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:39 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=914; i=ardb@kernel.org; h=from:subject; bh=iWpHiCO4bmybSs4v9cDyeXiFivmsE9/7Y/5o33bpjDQ=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3ntSmgny5l9pxCbvl07dfOPJm5Y6kndsOiYr6cmQ0L vuz+ZFsRykLgxgXg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjIwWZGhkmKBx+HB7JuZ3C5 vd0mzenE4jq1uNLte7YuO1jV9aPi30SG/z4/LG8ZyUunbut7tYArZvWsrVHcEqbdNZdUPGbEdmp YswIA X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-30-ardb+git@google.com> Subject: [PATCH v2 09/19] x86/efi: Drop redundant EFI_PARAVIRT check From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel efi_memblock_x86_reserve_range() exits early if EFI_PARAVIRT is set, so there is no point in checking it a second time further down. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/efi.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index d84c6020dda1..b60f8454a1ec 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -211,11 +211,9 @@ int __init efi_memblock_x86_reserve_range(void) data.desc_size =3D e->efi_memdesc_size; data.desc_version =3D e->efi_memdesc_version; =20 - if (!efi_enabled(EFI_PARAVIRT)) { - rv =3D efi_memmap_init_early(&data); - if (rv) - return rv; - } + rv =3D efi_memmap_init_early(&data); + if (rv) + return rv; =20 if (add_efi_memmap || do_efi_soft_reserve()) do_add_efi_memmap(); --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D34D03B52E6 for ; Thu, 19 Mar 2026 09:06:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911177; cv=none; b=OWulRzLP/PU949OHHEuDfrLVoJSG0De+KK6ZtqBoPyKSTP1DqAuLeOlKaADjjTvuK+fgkFVMwAlmgXNVT1sBets8bXgo24yyS0KKCXOtQ80EwFpp6J6rzzyYjY9vCD+8vXAOZYk8gOjdQBa4pCYy4fuFvwyo3HdItg4VpE1yCKQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911177; c=relaxed/simple; bh=vSc9gvmi0wPzgh7KJsC1mdV0JebdP0SL03rCP7lN6is=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=And6PtepmFv8EPpzVlwiYXzexG3y1ZXN8B0AfsFNnZj5X3QMFfufcrw1rWMi6OsSHd5YOawnh2Fv+30kkUs0z2l3C12S20w1h/by5humquNnKiWhoDJPFtwj7DQuiFFd4+1zEfcvZJIP7BLu/dp/YoBt7aQF6DRL1vwVtrHcSHo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HYKsgzM0; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HYKsgzM0" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-43b4d3919e4so517519f8f.0 for ; Thu, 19 Mar 2026 02:06:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911173; x=1774515973; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=iCBt8tT5p3mHgHNAvRatHWbwLoCmzdFJgF+LJbDgnnY=; b=HYKsgzM0hojKMSs5K04Vi2lw0S9qfl3W1cB+wG/1u9xlVFVDZSHFKGqGH3CF95NCb0 je6LaZilhX+Y5Iz1B5S68ffn79CW5sCZRIwgc4765KobKy/o0F2mN82wwQ2umBc2BRWw IqWgh4mXmuJTs0d0pAkQQCbP/DUrxe2dHNq8i3tKCqrv9Jc1KTb5LNbQObhXJU41j4ze zsoe/Ny9zBSYFAnsyid35Y9D5wcse9dRiyOw4I/ntJaO0ZGym2qKUmQb6pyT4DfTGQ0s qQ2L+q5B6aK6rgsA6eJXjt+mBhX5agieqO05oralO+Y4Dpl52FiOwkpryq8aRuEswvGc IoUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911173; x=1774515973; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=iCBt8tT5p3mHgHNAvRatHWbwLoCmzdFJgF+LJbDgnnY=; b=rnyTGMGbUP3Nm2MX2l4rk7qnASmTKJzCJt/rV+XP2IbVu17l+r/zg+ReFBQPgGguGj gTRQUw+O2vr9YCprPTr9T09cacf42DOOMQDWPWsLtL4BXz88PnVaXHbWbanNCKGh5X4p H+jgSrct95q1qGCJxhpDvKLwhDtg7Q8Sporu4IR8/H6PZDPW6iUSgJ2lSgVM52M/xW2l iM+deBS0K9RXJL4pyHtOd8PHi9IJ5BfQWuwS4Ng+mCrPbRY21F++X0oQjiJgF7GXunFq 1DtnBxpVTrhrc8bVoh/K02BNtNJpoib6BpUfjqKuLJhhxgCKwye8tAMPk/MGn335XRDr +esQ== X-Gm-Message-State: AOJu0Yy8rR3dopLlFX4kEGfFPSn55vqJxCus33hl9S0waZJJU+B0TuiP 21vS8Hcs5+6v4MvV8iBKMiHzHdfHsX1WGjlz2k9zN1+Qgm8AIn36AVEWD1VJP0DL6HfWafH4rZw Y9uWxa+WiM6v74ovWZGssbxSNYouk80iCv1YfTXAsXcWxjHiuzSasQsrqvl1hdlUwQdxj9LM1sh 2HIISzkwOFlHixn0mSL0yiyuMvTKq167BbLA== X-Received: from wrua10.prod.google.com ([2002:a5d:4d4a:0:b0:43a:5b:6a88]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:240c:b0:439:b791:f920 with SMTP id ffacd0b85a97d-43b527aa3dfmr12107986f8f.17.1773911172656; Thu, 19 Mar 2026 02:06:12 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:40 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=9050; i=ardb@kernel.org; h=from:subject; bh=KHztfi82Kf5HDtfcwgQyx5hl7BAgKVNcVh63vdm/NJE=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3njTlctWeZXV3Nh3NTDloeei237f3SW1pZk0HE2YF3 q2atWVtRykLgxgXg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjItTJGhl39vA4b5BflzL+U 0up8Tebq3U6VskCpZUnXVlV8mqqqupGR4XeoVFzz3P9dHnfbWCukN+oumOK9/lKAUcpBH+VpP+3 4GAA= X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-31-ardb+git@google.com> Subject: [PATCH v2 10/19] x86/efi: Do not rely on EFI_MEMORY_RUNTIME bit and avoid entry splitting From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Now that efi_mem_reserve() has been updated to rely on RSRV_KERN memblock reservations, it is no longer needed to mark memblock reserved boot services regions as EFI_MEMORY_RUNTIME. This means that it is no longer needed to split existing entries in the EFI memory map, removing the need to re-allocate/copy/remap the entire EFI memory map on every call to efi_mem_reserve(). So drop this functionality - it is no longer needed. Note that, for the time being, the E820 map needs to be consulted to decide whether or not an entry with the EFI_MEMORY_RUNTIME bit cleared needs to be preserved in the runtime map or not. However, this will be superseded and removed by a subsequent patch, which combines the map compaction with the actual freeing, in which case the freeing logic can answer this question directly. Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/efi.h | 4 - arch/x86/platform/efi/memmap.c | 138 -------------------- arch/x86/platform/efi/quirks.c | 62 +-------- 3 files changed, 6 insertions(+), 198 deletions(-) diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index 51b4cdbea061..b01dd639bf62 100644 --- a/arch/x86/include/asm/efi.h +++ b/arch/x86/include/asm/efi.h @@ -396,10 +396,6 @@ extern int __init efi_memmap_alloc(unsigned int num_en= tries, struct efi_memory_map_data *data); =20 extern int __init efi_memmap_install(struct efi_memory_map_data *data); -extern int __init efi_memmap_split_count(efi_memory_desc_t *md, - struct range *range); -extern void __init efi_memmap_insert(struct efi_memory_map *old_memmap, - void *buf, struct efi_mem_range *mem); =20 extern enum efi_secureboot_mode __x86_ima_efi_boot_mode(void); =20 diff --git a/arch/x86/platform/efi/memmap.c b/arch/x86/platform/efi/memmap.c index 023697c88910..8ef45014c7e7 100644 --- a/arch/x86/platform/efi/memmap.c +++ b/arch/x86/platform/efi/memmap.c @@ -110,141 +110,3 @@ int __init efi_memmap_install(struct efi_memory_map_d= ata *data) __efi_memmap_free(phys, size, flags); return 0; } - -/** - * efi_memmap_split_count - Count number of additional EFI memmap entries - * @md: EFI memory descriptor to split - * @range: Address range (start, end) to split around - * - * Returns the number of additional EFI memmap entries required to - * accommodate @range. - */ -int __init efi_memmap_split_count(efi_memory_desc_t *md, struct range *ran= ge) -{ - u64 m_start, m_end; - u64 start, end; - int count =3D 0; - - start =3D md->phys_addr; - end =3D start + (md->num_pages << EFI_PAGE_SHIFT) - 1; - - /* modifying range */ - m_start =3D range->start; - m_end =3D range->end; - - if (m_start <=3D start) { - /* split into 2 parts */ - if (start < m_end && m_end < end) - count++; - } - - if (start < m_start && m_start < end) { - /* split into 3 parts */ - if (m_end < end) - count +=3D 2; - /* split into 2 parts */ - if (end <=3D m_end) - count++; - } - - return count; -} - -/** - * efi_memmap_insert - Insert a memory region in an EFI memmap - * @old_memmap: The existing EFI memory map structure - * @buf: Address of buffer to store new map - * @mem: Memory map entry to insert - * - * It is suggested that you call efi_memmap_split_count() first - * to see how large @buf needs to be. - */ -void __init efi_memmap_insert(struct efi_memory_map *old_memmap, void *buf, - struct efi_mem_range *mem) -{ - u64 m_start, m_end, m_attr; - efi_memory_desc_t *md; - u64 start, end; - void *old, *new; - - /* modifying range */ - m_start =3D mem->range.start; - m_end =3D mem->range.end; - m_attr =3D mem->attribute; - - /* - * The EFI memory map deals with regions in EFI_PAGE_SIZE - * units. Ensure that the region described by 'mem' is aligned - * correctly. - */ - if (!IS_ALIGNED(m_start, EFI_PAGE_SIZE) || - !IS_ALIGNED(m_end + 1, EFI_PAGE_SIZE)) { - WARN_ON(1); - return; - } - - for (old =3D old_memmap->map, new =3D buf; - old < old_memmap->map_end; - old +=3D old_memmap->desc_size, new +=3D old_memmap->desc_size) { - - /* copy original EFI memory descriptor */ - memcpy(new, old, old_memmap->desc_size); - md =3D new; - start =3D md->phys_addr; - end =3D md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1; - - if (m_start <=3D start && end <=3D m_end) - md->attribute |=3D m_attr; - - if (m_start <=3D start && - (start < m_end && m_end < end)) { - /* first part */ - md->attribute |=3D m_attr; - md->num_pages =3D (m_end - md->phys_addr + 1) >> - EFI_PAGE_SHIFT; - /* latter part */ - new +=3D old_memmap->desc_size; - memcpy(new, old, old_memmap->desc_size); - md =3D new; - md->phys_addr =3D m_end + 1; - md->num_pages =3D (end - md->phys_addr + 1) >> - EFI_PAGE_SHIFT; - } - - if ((start < m_start && m_start < end) && m_end < end) { - /* first part */ - md->num_pages =3D (m_start - md->phys_addr) >> - EFI_PAGE_SHIFT; - /* middle part */ - new +=3D old_memmap->desc_size; - memcpy(new, old, old_memmap->desc_size); - md =3D new; - md->attribute |=3D m_attr; - md->phys_addr =3D m_start; - md->num_pages =3D (m_end - m_start + 1) >> - EFI_PAGE_SHIFT; - /* last part */ - new +=3D old_memmap->desc_size; - memcpy(new, old, old_memmap->desc_size); - md =3D new; - md->phys_addr =3D m_end + 1; - md->num_pages =3D (end - m_end) >> - EFI_PAGE_SHIFT; - } - - if ((start < m_start && m_start < end) && - (end <=3D m_end)) { - /* first part */ - md->num_pages =3D (m_start - md->phys_addr) >> - EFI_PAGE_SHIFT; - /* latter part */ - new +=3D old_memmap->desc_size; - memcpy(new, old, old_memmap->desc_size); - md =3D new; - md->phys_addr =3D m_start; - md->num_pages =3D (end - md->phys_addr + 1) >> - EFI_PAGE_SHIFT; - md->attribute |=3D m_attr; - } - } -} diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 13d9e036a23a..ae4ad6389f9e 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -239,63 +239,9 @@ EXPORT_SYMBOL_GPL(efi_query_variable_store); * buggy implementations we reserve boot services region during EFI * init and make sure it stays executable. Then, after * SetVirtualAddressMap(), it is discarded. - * - * However, some boot services regions contain data that is required - * by drivers, so we need to track which memory ranges can never be - * freed. This is done by tagging those regions with the - * EFI_MEMORY_RUNTIME attribute. - * - * Any driver that wants to mark a region as reserved must use - * efi_mem_reserve() which will insert a new EFI memory descriptor - * into efi.memmap (splitting existing regions if necessary) and tag - * it with EFI_MEMORY_RUNTIME. */ void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size) { - struct efi_memory_map_data data =3D { 0 }; - struct efi_mem_range mr; - efi_memory_desc_t md; - int num_entries; - void *new; - - if (efi_mem_desc_lookup(addr, &md) || - md.type !=3D EFI_BOOT_SERVICES_DATA) { - pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr); - return; - } - - if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) { - pr_err("Region spans EFI memory descriptors, %pa\n", &addr); - return; - } - - size +=3D addr % EFI_PAGE_SIZE; - size =3D round_up(size, EFI_PAGE_SIZE); - addr =3D round_down(addr, EFI_PAGE_SIZE); - - mr.range.start =3D addr; - mr.range.end =3D addr + size - 1; - mr.attribute =3D md.attribute | EFI_MEMORY_RUNTIME; - - num_entries =3D efi_memmap_split_count(&md, &mr.range); - num_entries +=3D efi.memmap.nr_map; - - if (efi_memmap_alloc(num_entries, &data) !=3D 0) { - pr_err("Could not allocate boot services memmap\n"); - return; - } - - new =3D early_memremap_prot(data.phys_map, data.size, - pgprot_val(pgprot_encrypted(FIXMAP_PAGE_NORMAL))); - if (!new) { - pr_err("Failed to map new boot services memmap\n"); - return; - } - - efi_memmap_insert(&efi.memmap, new, &mr); - early_memunmap(new, data.size); - - efi_memmap_install(&data); e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED); e820__update_table(e820_table); } @@ -446,7 +392,8 @@ void __init efi_unmap_boot_services(void) efi_unmap_pages(md); =20 /* Do not free, someone else owns it: */ - if (md->attribute & EFI_MEMORY_RUNTIME) { + if ((md->attribute & EFI_MEMORY_RUNTIME) || + !can_free_region(start, size)) { num_entries++; continue; } @@ -485,8 +432,11 @@ void __init efi_unmap_boot_services(void) for_each_efi_memory_desc(md) { if (!(md->attribute & EFI_MEMORY_RUNTIME) && (md->type =3D=3D EFI_BOOT_SERVICES_CODE || - md->type =3D=3D EFI_BOOT_SERVICES_DATA)) + md->type =3D=3D EFI_BOOT_SERVICES_DATA) && + can_free_region(md->phys_addr, + md->num_pages << EFI_PAGE_SHIFT)) { continue; + } =20 memcpy(new_md, md, efi.memmap.desc_size); new_md +=3D efi.memmap.desc_size; --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5C933B5838 for ; Thu, 19 Mar 2026 09:06:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911178; cv=none; b=DA2PEZDMmoHY1qunVjud35a+b8OQ5XP489viybwys92XGaQjUHcwFKaWnikmqTWtakIlLRSwdwuEr1AsSlaj4GeEo1PtzeXveYhjjDRyMpjo3c1KO60XKpQFeAP65oHoAQQKl8A3BCPrQCYJgQRRl7M2njhCgrwJLcGHtDsKJ9M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911178; c=relaxed/simple; bh=uAp3bNwnj43nm1d/5FZow5go2uc2Pe86fT9wiWD6Jws=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qgHIGUiZPWGhjbWCORJyO0EbSD94dWeoHuSb8vRB0tID2dyvo+XoyRb8BJQuUxgvNFNRECeOVj6Tkv3arC8Z4CvcSktcskLuyC7D0WvJ+uTi2YZcgNwna/JMeY8wQX0+SIRGua6U/+eQbL8KuM3y1gBwQtcq/keYI6hsAkmLeHI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iDbZq4tD; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iDbZq4tD" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-439bab2d095so317504f8f.3 for ; Thu, 19 Mar 2026 02:06:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911174; x=1774515974; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hvWEMBG1+vtssKaXaA+A+ef2cjhNjvbnMsrhKNB1E1M=; b=iDbZq4tDWCugl/DsJ9+j3Eb/dfDT4UywvEoQiSK79kuow0fQMdI8zQdgLGL7zVKPGf STAnzBsXFFMaJDs+rQb5Xsh9rJRa/S1BixvQw+AnW0C6wxNdMyiaVejtkaaOnLjFUL9M ssA+vlbitgtIqxKsFWeUNquD0nNyaSkV0M+kLqufz8XO3/f43HxRopMeS6NImXcxqZLW gPk0Zz6EzK/8M+9AITXGisxJGUAehrX/oC6uFyMbHQ3rIKkwtEfapNY2fxn7GFr3RxKI iVm200JrC8x/P5XbhlKb8k5cB9BS0B6bUcu1v/opn5Ro10fjgIwm/bcZy9akWic2agdk dxNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911174; x=1774515974; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hvWEMBG1+vtssKaXaA+A+ef2cjhNjvbnMsrhKNB1E1M=; b=HprKu5A70w1itMjA5jMmejAkjX0aPFVNaBYfTV6kzPk9YhggpLnRx3MGC3WPRPnInK Aub3jpIVZpSieI3BRHdfPTZj1dX0vK7wYKDShxZ4VvtGgy3auvN5KdSQVjouYrwwZsxS k+uhdPCPR8gXYf2zEjtC40hrV9YXmpIQ6lWuKvyV/wOo6VSG4FtktepVGW6QGCm6P0Mg uFLb5cuiPiA4jHp01SEWUO5UWdmgwPCHOWCcsZwBu69hkJvadvwPv/KiF7pmfKIdJntA X+eEychjJulTIX7Mv2hzchKA3rNRGWsIYWsqWl7LT2DdTIkPofc6jxIKEZkp15I6sGmY VpRg== X-Gm-Message-State: AOJu0Yx0XdC2zjVIriD+RHiIdJHA+qojXrFLr4pAvLUVgdFHu3TPG9gY rBMwBtZdijMvZBAuJ83nZSH6IhOuQHRxT3sIjLEqkWXJwbJCdJEo2ETgrHut19zZ4P2G0HsSf45 SvNHfE8GHed9ODq+ogm9huCB9qLT2a6TPKj4PrCbRNAFP7s0PJ4Y4bEgujX53Y5RvyOgYmMyEmF 77T+JYZ8G78pFl2bMt4t7KNS3NW3ZJADdxPw== X-Received: from wrul18.prod.google.com ([2002:a5d:6692:0:b0:43b:45d1:f433]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1d10:b0:485:3d3e:167b with SMTP id 5b1f17b1804b1-486f441fc5bmr105805365e9.5.1773911173849; Thu, 19 Mar 2026 02:06:13 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:41 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=10133; i=ardb@kernel.org; h=from:subject; bh=kvX6bjgQom/3kqkMau2ISxTwGfxxMsz3oICe3SqhhUw=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3noywwD0n+yYWzhNSefRwplS4f1PSpJdxpy45K9YFz AjzNg/uKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABOpnsLIcDiPWeHowz+OV9ZY ry5MWGxotXObyXqd6QtrZruk3ff5fZjhv2vPogtblk1eZsn6wkYj+7edSmdOp9sBb52Cz6xnn9Q 3cwAA X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-32-ardb+git@google.com> Subject: [PATCH v2 11/19] efi: Use nr_map not map_end to find the last valid memory map entry From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Currently, the efi.memmap struct keeps track of the start and the end of the EFI memory map in memory, as well as the number of entries. Let's repaint the nr_map field as the number of *valid* entries, and update all the iterators and other memory map traversal routines accordingly. This allows pruning of invalid or unneeded entries by moving the remaining entries to the start of the map, without the need for freeing/reallocating or unmapping and remapping. Now that entries are never added, but only removed, it is possible to retain the same allocation throughout the boot process, and free the part that is no longer in use afterwards. While at it, implement a version of for_each_efi_memory_desc() that traverses the memory map in opposite order. It will be used by a subsequent patch. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/efi.c | 15 +++++++----- arch/x86/platform/efi/memmap.c | 2 +- arch/x86/platform/efi/quirks.c | 2 +- arch/x86/platform/efi/runtime-map.c | 4 ++-- drivers/firmware/efi/arm-runtime.c | 2 +- drivers/firmware/efi/memattr.c | 2 +- drivers/firmware/efi/memmap.c | 8 +++---- drivers/firmware/efi/riscv-runtime.c | 2 +- include/linux/efi.h | 24 ++++++++++++++++---- 9 files changed, 39 insertions(+), 22 deletions(-) diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index b60f8454a1ec..183cca8fe4a6 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -222,7 +222,7 @@ int __init efi_memblock_x86_reserve_range(void) "Unexpected EFI_MEMORY_DESCRIPTOR version %ld", efi.memmap.desc_version); =20 - memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size); + memblock_reserve(pmap, efi.memmap.num_valid_entries * efi.memmap.desc_siz= e); set_bit(EFI_PRESERVE_BS_REGIONS, &efi.flags); =20 return 0; @@ -289,7 +289,7 @@ static void __init efi_clean_memmap(void) .phys_map =3D efi.memmap.phys_map, .desc_version =3D efi.memmap.desc_version, .desc_size =3D efi.memmap.desc_size, - .size =3D efi.memmap.desc_size * (efi.memmap.nr_map - n_removal), + .size =3D efi.memmap.desc_size * (efi.memmap.num_valid_entries - n_rem= oval), .flags =3D 0, }; =20 @@ -564,7 +564,8 @@ static inline void *efi_map_next_entry_reverse(void *en= try) { /* Initial call */ if (!entry) - return efi.memmap.map_end - efi.memmap.desc_size; + return efi_memdesc_ptr(efi.memmap.map, efi.memmap.desc_size, + efi.memmap.num_valid_entries - 1); =20 entry -=3D efi.memmap.desc_size; if (entry < efi.memmap.map) @@ -612,7 +613,9 @@ static void *efi_map_next_entry(void *entry) return efi.memmap.map; =20 entry +=3D efi.memmap.desc_size; - if (entry >=3D efi.memmap.map_end) + if (entry >=3D (void *)efi_memdesc_ptr(efi.memmap.map, + efi.memmap.desc_size, + efi.memmap.num_valid_entries)) return NULL; =20 return entry; @@ -743,13 +746,13 @@ static void __init kexec_enter_virtual_mode(void) efi_memmap_unmap(); =20 if (efi_memmap_init_late(efi.memmap.phys_map, - efi.memmap.desc_size * efi.memmap.nr_map)) { + efi.memmap.desc_size * efi.memmap.num_valid_entries)) { pr_err("Failed to remap late EFI memory map\n"); clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); return; } =20 - num_pages =3D ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE); + num_pages =3D ALIGN(efi.memmap.num_valid_entries * efi.memmap.desc_size, = PAGE_SIZE); num_pages >>=3D PAGE_SHIFT; =20 if (efi_setup_page_tables(efi.memmap.phys_map, num_pages)) { diff --git a/arch/x86/platform/efi/memmap.c b/arch/x86/platform/efi/memmap.c index 8ef45014c7e7..fa580c4122c4 100644 --- a/arch/x86/platform/efi/memmap.c +++ b/arch/x86/platform/efi/memmap.c @@ -93,7 +93,7 @@ int __init efi_memmap_alloc(unsigned int num_entries, */ int __init efi_memmap_install(struct efi_memory_map_data *data) { - unsigned long size =3D efi.memmap.desc_size * efi.memmap.nr_map; + unsigned long size =3D efi.memmap.map_end - efi.memmap.map; unsigned long flags =3D efi.memmap.flags; u64 phys =3D efi.memmap.phys_map; int ret; diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index ae4ad6389f9e..eecaa745d352 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -367,7 +367,7 @@ void __init efi_unmap_boot_services(void) if (efi_enabled(EFI_DBG)) return; =20 - sz =3D sizeof(*ranges_to_free) * efi.memmap.nr_map + 1; + sz =3D sizeof(*ranges_to_free) * efi.memmap.num_valid_entries + 1; ranges_to_free =3D kzalloc(sz, GFP_KERNEL); if (!ranges_to_free) { pr_err("Failed to allocate storage for freeable EFI regions\n"); diff --git a/arch/x86/platform/efi/runtime-map.c b/arch/x86/platform/efi/ru= ntime-map.c index 053ff161eb9a..fc8ca1974730 100644 --- a/arch/x86/platform/efi/runtime-map.c +++ b/arch/x86/platform/efi/runtime-map.c @@ -138,7 +138,7 @@ add_sysfs_runtime_map_entry(struct kobject *kobj, int n= r, =20 int efi_get_runtime_map_size(void) { - return efi.memmap.nr_map * efi.memmap.desc_size; + return efi.memmap.num_valid_entries * efi.memmap.desc_size; } =20 int efi_get_runtime_map_desc_size(void) @@ -166,7 +166,7 @@ static int __init efi_runtime_map_init(void) if (!efi_enabled(EFI_MEMMAP) || !efi_kobj) return 0; =20 - map_entries =3D kzalloc_objs(entry, efi.memmap.nr_map); + map_entries =3D kzalloc_objs(entry, efi.memmap.num_valid_entries); if (!map_entries) { ret =3D -ENOMEM; goto out; diff --git a/drivers/firmware/efi/arm-runtime.c b/drivers/firmware/efi/arm-= runtime.c index 3167cab62014..e19997c09175 100644 --- a/drivers/firmware/efi/arm-runtime.c +++ b/drivers/firmware/efi/arm-runtime.c @@ -96,7 +96,7 @@ static int __init arm_enable_runtime_services(void) =20 efi_memmap_unmap(); =20 - mapsize =3D efi.memmap.desc_size * efi.memmap.nr_map; + mapsize =3D efi.memmap.desc_size * efi.memmap.num_valid_entries; =20 if (efi_memmap_init_late(efi.memmap.phys_map, mapsize)) { pr_err("Failed to remap EFI memory map\n"); diff --git a/drivers/firmware/efi/memattr.c b/drivers/firmware/efi/memattr.c index e727cc5909cb..36f733b37df2 100644 --- a/drivers/firmware/efi/memattr.c +++ b/drivers/firmware/efi/memattr.c @@ -49,7 +49,7 @@ void __init efi_memattr_init(void) * just be ignored altogether. */ size =3D tbl->num_entries * tbl->desc_size; - if (size > 3 * efi.memmap.nr_map * efi.memmap.desc_size) { + if (size > 3 * efi.memmap.num_valid_entries * efi.memmap.desc_size) { pr_warn(FW_BUG "Corrupted EFI Memory Attributes Table detected! (version= =3D=3D %u, desc_size =3D=3D %u, num_entries =3D=3D %u)\n", tbl->version, tbl->desc_size, tbl->num_entries); goto unmap; diff --git a/drivers/firmware/efi/memmap.c b/drivers/firmware/efi/memmap.c index f1c04d7cfd71..035089791c93 100644 --- a/drivers/firmware/efi/memmap.c +++ b/drivers/firmware/efi/memmap.c @@ -49,7 +49,7 @@ int __init __efi_memmap_init(struct efi_memory_map_data *= data) } =20 map.phys_map =3D data->phys_map; - map.nr_map =3D data->size / data->desc_size; + map.num_valid_entries =3D data->size / data->desc_size; map.map_end =3D map.map + data->size; =20 map.desc_version =3D data->desc_version; @@ -87,10 +87,8 @@ void __init efi_memmap_unmap(void) return; =20 if (!(efi.memmap.flags & EFI_MEMMAP_LATE)) { - unsigned long size; - - size =3D efi.memmap.desc_size * efi.memmap.nr_map; - early_memunmap(efi.memmap.map, size); + early_memunmap(efi.memmap.map, + efi.memmap.map_end - efi.memmap.map); } else { memunmap(efi.memmap.map); } diff --git a/drivers/firmware/efi/riscv-runtime.c b/drivers/firmware/efi/ri= scv-runtime.c index 60cdf7bf141f..087a7f8a74e6 100644 --- a/drivers/firmware/efi/riscv-runtime.c +++ b/drivers/firmware/efi/riscv-runtime.c @@ -66,7 +66,7 @@ static int __init riscv_enable_runtime_services(void) =20 efi_memmap_unmap(); =20 - mapsize =3D efi.memmap.desc_size * efi.memmap.nr_map; + mapsize =3D efi.memmap.desc_size * efi.memmap.num_valid_entries; =20 if (efi_memmap_init_late(efi.memmap.phys_map, mapsize)) { pr_err("Failed to remap EFI memory map\n"); diff --git a/include/linux/efi.h b/include/linux/efi.h index 664898d09ff5..b0c3e9648126 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -568,7 +568,7 @@ struct efi_memory_map { phys_addr_t phys_map; void *map; void *map_end; - int nr_map; + int num_valid_entries; unsigned long desc_version; unsigned long desc_size; #define EFI_MEMMAP_LATE (1UL << 0) @@ -803,9 +803,15 @@ extern int efi_memattr_apply_permissions(struct mm_str= uct *mm, =20 /* Iterate through an efi_memory_map */ #define for_each_efi_memory_desc_in_map(m, md) \ - for ((md) =3D (m)->map; \ - (md) && ((void *)(md) + (m)->desc_size) <=3D (m)->map_end; \ - (md) =3D (void *)(md) + (m)->desc_size) + for (int __idx =3D 0; \ + (md) =3D efi_memdesc_ptr((m)->map, (m)->desc_size, __idx), \ + __idx < (m)->num_valid_entries; ++__idx) + +/* Iterate through an efi_memory_map in reverse order */ +#define for_each_efi_memory_desc_in_map_rev(m, md) \ + for (int __idx =3D (m)->num_valid_entries - 1; \ + (md) =3D efi_memdesc_ptr((m)->map, (m)->desc_size, __idx), \ + __idx >=3D 0; --__idx) =20 /** * for_each_efi_memory_desc - iterate over descriptors in efi.memmap @@ -816,6 +822,16 @@ extern int efi_memattr_apply_permissions(struct mm_str= uct *mm, #define for_each_efi_memory_desc(md) \ for_each_efi_memory_desc_in_map(&efi.memmap, md) =20 +/** + * for_each_efi_memory_desc_rev - iterate over descriptors in efi.memmap in + * reverse order + * @md: the efi_memory_desc_t * iterator + * + * Once the loop finishes @md must not be accessed. + */ +#define for_each_efi_memory_desc_rev(md) \ + for_each_efi_memory_desc_in_map_rev(&efi.memmap, md) + /* * Format an EFI memory descriptor's type and attributes to a user-provided * character buffer, as per snprintf(), and return the buffer. --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D2A03AA50C for ; Thu, 19 Mar 2026 09:06:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911179; cv=none; b=hHwiV0bViMyU97WOvUaQCEu/0CZyL9eF+J8i/xIZVyKpGnvPb1DX85WEzhLUB6xnzS/u/xTteu7fXEWEtYr/vzCniXW4Sq7FDH+dCwZM4QoO8oKlaUJmY3i4d4vxONPSkG12SMpHU0uRtqAnbfj/FjkVsoYAs1B0q6U4R4fPjFA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911179; c=relaxed/simple; bh=cbbspSu/En8wMoIk09xHoGHLe/eK50A9ypzP7V+nnys=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dVvtWiLpsZKLxW4zrvFMMuzxcN+qGYtK3dazBex/Su2xBWePd5kLl3bdLjthMzOlBcGje+jWIqcpyq3xFIV+FqZdI1gfVO7GzareMrQTC25GFu0L6MzGbBKhHmI4Z3ZwyoCH+TGFeL8kf1XWn3cf7Vg2SkEVq6KVsUoh3WxQdG4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=mOJ+CmKc; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mOJ+CmKc" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-43b3d3d2d22so339421f8f.0 for ; Thu, 19 Mar 2026 02:06:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911176; x=1774515976; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GGWJD/oSO/zqs1fjiBr8BSE5CyULnCYZrvaP5HxSasQ=; b=mOJ+CmKcl0jL/+RmqPXEjUDAYrV/4wecURM4r3Lo0o2YuqwzW5AvXbpUD6iUNW9Woz vYCCSxctTrixfVsDItjFlW/+ZbYbkEqm/KOR5+3lF6Pc3dgECEGK6smTXU3faeyQJKlk o7Kw4SgYDWHDSyEdmMQXEAq69KQdLv/CtGVeXWZ+W/IAMelclXMU6jFlnEUzJ7eSrbig u1u0CCaOWlh+IKSGzdJsB7ELmyAVhnUKkYm0h3g1NmSHWmfOzS1M2yCfwu2zmRp37Opy M2ZuRRg+1SJMA/DJANOK91NAjBHROi/d63TueYLlJQNzOBZZGG8PX8PKK3m/r/66Yrn7 ryIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911176; x=1774515976; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GGWJD/oSO/zqs1fjiBr8BSE5CyULnCYZrvaP5HxSasQ=; b=sHlqNErZkUbzo1eSCXbC87oqSuiygzBn29fu1YmH2QB9jVystPZ46gYJLKxbFQIKuo jCY42A4crcg1dzyvUT6mO74EaTdlajAPbIHUq2LurcrVLLyULsYG7xXq7FIQzVJ34UHw IsUi57k/tqaw55PCGmIyIYn2/gPPCD1Elzu8hqCIGvab4pCEuUmPpqeUWOfDV56rToBC ixzrA6pP4wRQFpn9Ujb5h0k+ug9fsmPvOIK8OmdZhTBsuovAsBXcfJHGZ6sbui3mpy0i ptXtOmDA8gQQkZEC+TVi3i+usxYZjiUqDDURRbkYu7m9GvVXljXc933HIeBQnK5PdFEU +Z+g== X-Gm-Message-State: AOJu0Yx3f835dyihQPFjPwwE6esfTEVoxFHuakCuiIBTtwtc1Kwpp4Fq 32sTZLDfnN8v/7/eaA2qbApWnAqZHQ8M3uN94JGfeneNE034Tp6oXEFsiAq9gUnfqMIM4MuLNsC oLzAqawCkvVfGNPd+zyhfTNQHZiWNs33lEM4Mpv1BV8HYaFhmoCjYR0UY01lBiwY0byNUjjzROr 2hpWfZ9cCBO1WmOPOuzryAGBDXAokalg0zJA== X-Received: from wmlz10.prod.google.com ([2002:a05:600c:220a:b0:485:32b2:6b8d]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1c2a:b0:485:2c61:9457 with SMTP id 5b1f17b1804b1-486f44379a6mr102042845e9.10.1773911174993; Thu, 19 Mar 2026 02:06:14 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:42 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=4144; i=ardb@kernel.org; h=from:subject; bh=MRuY0aJg5kwsKdStVH+fBQF2uFes8p/NfZYMA6nA7UM=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3nsxfJaU2qo+XFZ7qXemwsXv/0+q2Spbyy0kb5Pwzl JXPBU3sKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABMJKmVkuGy7Wvf3x+uaGy4x Zz97LLJh65PFQn8vZew5Jzn/yuQMc2dGhks8HrYabPrfmVbYe34NLDAsCwu2yJo2J+OHnn2/yYY EbgA= X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-33-ardb+git@google.com> Subject: [PATCH v2 12/19] x86/efi: Only merge EFI memory map entries on 32-bit systems From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Commit 202f9d0a4180 ("x86, efi: Merge contiguous memory regions of the same type= and attribute") introduced a pass over the EFI memory map, ensuring that contiguous regions of the same type and attribute are coalesced into a single entry. This was needed because relative references may exist between those regions, and so the virtual remapping needs to preserve the relative placement of these regions. This virtual remapping was based on ioremap() at the time, which does not guarantee that adjacent physical addresses are mapped adjacently in the virtual space. Commit d2f7cbe7b26a ("x86/efi: Runtime services virtual mapping") introduced a new strategy for virtually remapping the EFI runtime services, which is now the only remaining one, and commit a5caa209ba9c ("x86/efi: Fix boot crash by mapping EFI memmap entries bott= om-up at runtime, instead of top-down") tweaked the logic to ensure that the relative offset of adjacent regions of any type is preserved on 64-bit systems, by reversing the order in which the EFI memory map is traversed when choosing the virtual placement. This means that merging regions is no longer needed on 64-bit, given that the relative placement of adjacent regions is guaranteed to be preserved in the virtual space. So make this hack 32-bit only. Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/efi.h | 6 ++++ arch/x86/platform/efi/efi.c | 31 -------------------- arch/x86/platform/efi/efi_32.c | 31 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index b01dd639bf62..44cdd3c1055e 100644 --- a/arch/x86/include/asm/efi.h +++ b/arch/x86/include/asm/efi.h @@ -143,6 +143,12 @@ extern void efi_unmap_boot_services(void); void arch_efi_call_virt_setup(void); void arch_efi_call_virt_teardown(void); =20 +#ifdef CONFIG_X86_32 +void efi_merge_regions(void); +#else +static inline void efi_merge_regions(void) {} +#endif + extern u64 efi_setup; =20 #ifdef CONFIG_EFI diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index 183cca8fe4a6..a6081e3f1b88 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -502,37 +502,6 @@ void __init efi_init(void) efi_print_memmap(); } =20 -/* Merge contiguous regions of the same type and attribute */ -static void __init efi_merge_regions(void) -{ - efi_memory_desc_t *md, *prev_md =3D NULL; - - for_each_efi_memory_desc(md) { - u64 prev_size; - - if (!prev_md) { - prev_md =3D md; - continue; - } - - if (prev_md->type !=3D md->type || - prev_md->attribute !=3D md->attribute) { - prev_md =3D md; - continue; - } - - prev_size =3D prev_md->num_pages << EFI_PAGE_SHIFT; - - if (md->phys_addr =3D=3D (prev_md->phys_addr + prev_size)) { - prev_md->num_pages +=3D md->num_pages; - md->type =3D EFI_RESERVED_TYPE; - md->attribute =3D 0; - continue; - } - prev_md =3D md; - } -} - static void *realloc_pages(void *old_memmap, int old_shift) { void *ret; diff --git a/arch/x86/platform/efi/efi_32.c b/arch/x86/platform/efi/efi_32.c index b2cc7b4552a1..886ede4117b5 100644 --- a/arch/x86/platform/efi/efi_32.c +++ b/arch/x86/platform/efi/efi_32.c @@ -152,3 +152,34 @@ void arch_efi_call_virt_teardown(void) firmware_restrict_branch_speculation_end(); efi_fpu_end(); } + +/* Merge contiguous regions of the same type and attribute */ +void __init efi_merge_regions(void) +{ + efi_memory_desc_t *md, *prev_md =3D NULL; + + for_each_efi_memory_desc(md) { + u64 prev_size; + + if (!prev_md) { + prev_md =3D md; + continue; + } + + if (prev_md->type !=3D md->type || + prev_md->attribute !=3D md->attribute) { + prev_md =3D md; + continue; + } + + prev_size =3D prev_md->num_pages << EFI_PAGE_SHIFT; + + if (md->phys_addr =3D=3D (prev_md->phys_addr + prev_size)) { + prev_md->num_pages +=3D md->num_pages; + md->type =3D EFI_RESERVED_TYPE; + md->attribute =3D 0; + continue; + } + prev_md =3D md; + } +} --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 805EA3AE717 for ; Thu, 19 Mar 2026 09:06:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911180; cv=none; b=oICeRpRuSyXyFHbCTOiitlR8S+2ktgDjshu+dU0proJ9yhHnU1A9gKirZoEFUQP2YQsEPv69NEkfYrArDmi7z2Kct8gCuNW0ybqU2kiMVyKlromNz9REfmd+85R2icvipTEQG1HIOXtOQ/Q2zjYAxtLS2rQyh2vV2ihbrfGNeEU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911180; c=relaxed/simple; bh=K4sZ6Y4U9VrKzKj4K3DstnUJLlIlrJWHRBRpbne44zM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tNQfslKtiQt/rDwAUe7WiHW/F127TvGtT+ADt3GQqrtXRSMFxp7RKlwqMhX5LRuCrZ2+xY026Ji8mCwavdXd3waq+ysoQ7zh5rkKi1nG8HCUn/hVIGfApm3iPxT+qPvr5uxgIhyth6oGgh4Qa0Q7TZAWV5NrLqlS7ZDcttBdJy8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Z5JbIPMm; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Z5JbIPMm" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-486f89b7f69so3121225e9.0 for ; Thu, 19 Mar 2026 02:06:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911177; x=1774515977; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=UOQi+adgpvbqtFGURNDthC/99Udxt3gsOjaHUFmz4zQ=; b=Z5JbIPMmeWZIMpSOafsGwcyGaSrSYoy0QomyMqala4Oj9BNqg6QawnS02olzZeCrAW iWLvTX1orvzTPhzTH63TpPCoqP9DQ+AW9CjvTUMWCm3dBlGBdwgrymQtDf+XsbtBukZO 9HSmN8YkSMdsjJBtj9adzCbz0guvyKrGDCFtDlXc1hNqybWI/rRsnazRz23Eyw+8h0Z5 UW4rk0DN3JiSniPdjyAdqXpcbShDpeT3ceX9AO5YB5ZZci/9bNIMHzv8u5tCxbmRLg0x vIqOC+C5UGg2gK7gnNjRq+MVUDQDR6pGdnyJHm/PhymUJ5vYHnPpVUJEa8NSqucOlNet osAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911177; x=1774515977; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UOQi+adgpvbqtFGURNDthC/99Udxt3gsOjaHUFmz4zQ=; b=agTaDF9KY8nfc+M1bLAlkvtTWc6WMLz8A/WzUYHEcyD7koplDyvUNQ4wSYC6GtCHpz IhQAQLOnitiPZLGZXPSu5r/EvWi2gnK+aOhsuZyii3eyEdPjydsQywu3ABKvSFo3ZUVJ +w6iQzMNaQj3SdUrllZF1rSpiKwbmOJ9cuEenDKCycGlQHGjabfBU3qJPES8OrzXM0aT 4TE4Nt0g3gG5OiTSeeWXbjrBT4RYyElxwP3v2sE0HftPuhTqnpaZhZTrH/tQzApsdMy4 MtRjUwrM1E22cnyv0mS1QDVS6jhTD7xG0g5n990BmmjceAhOYS1p33vqHTMLYnYGnPxy NH4w== X-Gm-Message-State: AOJu0YwZ97R/LYnKSprFCGdGydp7+V3cYaTU8buM/gqlXHKTlIv5glgH utr5q+QH7I/xLJj/r9Z7EcwlWw9bx5DNBI/tP6otK+MLXYoN96XgCE3T3nwCpZKj/N4T5YLAUUu 1xA66Lw06N3quCIGJWAUzqw5BoLx2catgVUoHDEZrQr75nmXzOvT0XKI3UR+6asyXRnObkXY2ts xSXo+Iz2GhQJbHXDfnrrcstIKpCOr4pDQTJg== X-Received: from wmhf4.prod.google.com ([2002:a7b:cc04:0:b0:485:4553:1a97]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4fc6:b0:485:2ce2:4c87 with SMTP id 5b1f17b1804b1-486f442e58dmr105683935e9.4.1773911176439; Thu, 19 Mar 2026 02:06:16 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:43 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2411; i=ardb@kernel.org; h=from:subject; bh=eOPgqP7IEm50GkDKbe7ob4juUFD7i8TXH31+J/KxfWQ=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3niyvPbz/lto67V0jIdPpov9+79H5S2dUsYvuOxtyl 2//l8ufO0pZGMS4GGTFFFkEZv99t/P0RKla51myMHNYmUCGMHBxCsBEOn4xMvxgbZrcx3jTN6LJ J+Pp0U1PPUzXS2ZMm2Fy4cahCgW/+Y2MDH+mLC85usql3fVG0fE4mbifwlttl7boT7CcuYdfY9H 5Tm4A X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-34-ardb+git@google.com> Subject: [PATCH v2 13/19] x86/efi: Clean the memory map using iterator and filter API From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Instead of open coding the iteration logic, use the existing iterator API to iterate over all valid entries in the EFI memory map. In addition, break out the logic that iterates over and conditionally suppresses memory map entries so it can be reused later, as something similar is happening two more times during boot. Note that actually reinstalling the EFI memory map, which involves unmapping and remapping it, is no longer needed, given that the number of valid entries can only go down. So omit efi_memmap_install() and just update the number of valid entries. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/efi.c | 30 +++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index a6081e3f1b88..e9b84ecc859b 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -266,36 +266,32 @@ static bool __init efi_memmap_entry_valid(const efi_m= emory_desc_t *md, int i) return false; } =20 -static void __init efi_clean_memmap(void) +static int __init +efi_memmap_filter_entries(bool (*callback)(const efi_memory_desc_t *, int)) { efi_memory_desc_t *out =3D efi.memmap.map; const efi_memory_desc_t *in =3D out; - const efi_memory_desc_t *end =3D efi.memmap.map_end; - int i, n_removal; + int i =3D 0, filtered =3D 0; =20 - for (i =3D n_removal =3D 0; in < end; i++) { - if (efi_memmap_entry_valid(in, i)) { + for_each_efi_memory_desc(in) { + if (callback(in, i++)) { if (out !=3D in) memcpy(out, in, efi.memmap.desc_size); out =3D (void *)out + efi.memmap.desc_size; } else { - n_removal++; + filtered++; } - in =3D (void *)in + efi.memmap.desc_size; } + efi.memmap.num_valid_entries -=3D filtered; + return filtered; +} =20 - if (n_removal > 0) { - struct efi_memory_map_data data =3D { - .phys_map =3D efi.memmap.phys_map, - .desc_version =3D efi.memmap.desc_version, - .desc_size =3D efi.memmap.desc_size, - .size =3D efi.memmap.desc_size * (efi.memmap.num_valid_entries - n_rem= oval), - .flags =3D 0, - }; +static void __init efi_clean_memmap(void) +{ + int n_removal =3D efi_memmap_filter_entries(efi_memmap_entry_valid); =20 + if (n_removal > 0) pr_warn("Removing %d invalid memory map entries.\n", n_removal); - efi_memmap_install(&data); - } } =20 /* --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D2953AEF20 for ; Thu, 19 Mar 2026 09:06:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911182; cv=none; b=RorxD259ly4uqM7DvaKQQG8jdxeUokszNmHBsQopUzRfYgZILz1diBEUn+pA57e3bZ4AiEwcW0qZNCKjbU1CYR/knXiBFhZEIQ4Qquyd3k2fnHwUV4mxnYJd6Qr3frIwcUGw1wAnVwIrkVm19xk8QGHYrJcD4lNOkwTAp1JiST8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911182; c=relaxed/simple; bh=9OprONxDWnCOJYX+FgMjLUb77QLi2ZEk9RUwqdxBQHw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ayfm/t/Mux6SjBv7UIHRj8fQuK4pXRpTnFSdcQpBBemd7k4xpT3Uzoou7AQbtdXAKRgUXdGdcx2KYyE/BGM6XB8wfADDwG/aEKLpYzD/frUXXjoNeuLLM/6nl2mR2/jkts2SccAZOWTo8+eLMJZ6X5qNOR1mZKEu7U+yUBqG8QY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=r7/I/yvB; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="r7/I/yvB" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-486fb29a8b8so3573075e9.0 for ; Thu, 19 Mar 2026 02:06:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911177; x=1774515977; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=PRYZyyQvMVeRrU4Yx4WOuIvt5z1Qake/UrKB6R2VYpU=; b=r7/I/yvBljtkOiS281eBbyBFV5D/yWZryy939l+pB33384Go7fo5lmEYNU3uDhR4yV 5GvX/tNZKqY5eYzFGUSeMDYpuyvwioswYj+kPUza36XAIcuieepsAkx+gVTvgIH95qmA rw9vVsvSd/L6C0Zv3qnfMWVGViftAbmopwAgaMJdLQTSea2fr4PGE2N85osQYI3JKU5/ ltLryX3v3vz37vf2XV2BYrUBPT/9W2tx8r/DIUneL2QRpc1vhnkHbRd3/I9GcguK1IaP /4GtfOC84E4cOFmvTPsF4gU6xDUPsTGeZHvv5vVHnLPUO+OT4vijn6yHO5ZjudJ8PFBa soBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911177; x=1774515977; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PRYZyyQvMVeRrU4Yx4WOuIvt5z1Qake/UrKB6R2VYpU=; b=BBdjlZzXJP39HVlrrmYgDL8UiIkC/XhdvdpD0lBXflvBO/K4SUubmfwqKgn2ZP+iRe qOdlPw/vkX3+CHCkiHXlz5cxlQ+CNV15nniank+YuApa9zhFmdtjDA5iFWdkOhZ6G7CP 9APAbEtnJNAeuyngK9TY1SYA9vVl/JV+hLxKwAy1amN4ncwm21g6+KgN5RcHqLIh9szd PJit86vy2BLZoKuVaDnIj+f1YxLtoy47e0oeSkhgHBOaz/OsCOReLiNi3BjmoFiLY52G kjIt9RwNBcRPnWGGSwEumXSwUppFWl65aGHLMWcCgqJ9TJuj44myY8bdXUWONes8ldxF lMNA== X-Gm-Message-State: AOJu0YzzyAKCQuxN360AKSUKUkny8uMVFUTcgviXiEZvOUsFzaEU4O8Y lxTcSCbpn29tYbAYHmm7nP4ZhgRIKVNB64fDjgdeqzV9ljlp7ek4LEOaUsLYOcfuPXWbvoIHHzq Cc3hJudmB67fZ2nEEy+Hr0sZ4e6Q/Yi+9MIa9L3r8jzJlkZJBd+aEtYnwJ01jgoyARGeNb/k7u8 MZ/mEW4OqxT6xt5/MsTiCFX6BOO4iDbzD+xA== X-Received: from wmqn21.prod.google.com ([2002:a05:600c:4f95:b0:480:4be7:3f3a]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4703:b0:485:ae14:8187 with SMTP id 5b1f17b1804b1-486f441fc66mr106760255e9.2.1773911177432; Thu, 19 Mar 2026 02:06:17 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:44 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5125; i=ardb@kernel.org; h=from:subject; bh=+Cbk1AaYHmwQCKK7zA0yoqZBDfzZ+oWlWRJYofF/tdY=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3nmyFSc/z12bPeCOdY1pXu7RBJpIzdZNLnID7uY/TN 8eW18/oKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABMJmMjIcE9w9asjf/nSF2z9 3V25bHmhw+dLCptvzXg7Z+P5HR32604y/Pcrfvhl6zPjrLhp200dxRbamt782nmcpfCl0TxPQ0G vCAYA X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-35-ardb+git@google.com> Subject: [PATCH v2 14/19] x86/efi: Update the runtime map in place From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel When creating the EFI runtime map, a copy is created containing only the entries that will be mapped on behalf of the firmware, but the assignment of the virtual address field is applied to both copies. Subsequently, the copy is installed as the new EFI memory map, and the old one is just leaked. This means that there is no reason whatsoever to allocate and install the copy, and it is much easier to just update the existing memory map in place to set the virtual addresses and suppress unused entries. So reuse the filter function used by efi_clean_memmap() to drop all entries that are irrelevant, and then apply the existing logic to assign the virtual addresses and create the mappings in the EFI page tables. Note that x86_64 and i386 traverse the memory map in opposite order, so this part remains a separate pass as before. This logic will be further simplified in subsequent patch. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/efi.c | 89 +++++--------------- 1 file changed, 19 insertions(+), 70 deletions(-) diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index e9b84ecc859b..44d106879120 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -498,27 +498,6 @@ void __init efi_init(void) efi_print_memmap(); } =20 -static void *realloc_pages(void *old_memmap, int old_shift) -{ - void *ret; - - ret =3D (void *)__get_free_pages(GFP_KERNEL, old_shift + 1); - if (!ret) - goto out; - - /* - * A first-time allocation doesn't have anything to copy. - */ - if (!old_memmap) - return ret; - - memcpy(ret, old_memmap, PAGE_SIZE << old_shift); - -out: - free_pages((unsigned long)old_memmap, old_shift); - return ret; -} - /* * Iterate the EFI memory map in reverse order because the regions * will be mapped top-down. The end result is the same as if we had @@ -586,7 +565,7 @@ static void *efi_map_next_entry(void *entry) return entry; } =20 -static bool should_map_region(efi_memory_desc_t *md) +static bool should_map_region(const efi_memory_desc_t *md, int unused) { /* * Runtime regions always require runtime mappings (obviously). @@ -639,40 +618,14 @@ static bool should_map_region(efi_memory_desc_t *md) * Map the efi memory ranges of the runtime services and update new_mmap w= ith * virtual addresses. */ -static void * __init efi_map_regions(int *count, int *pg_shift) +static void __init efi_map_regions(void) { - void *p, *new_memmap =3D NULL; - unsigned long left =3D 0; - unsigned long desc_size; efi_memory_desc_t *md; =20 - desc_size =3D efi.memmap.desc_size; - - p =3D NULL; - while ((p =3D efi_map_next_entry(p))) { - md =3D p; - - if (!should_map_region(md)) - continue; + efi_memmap_filter_entries(should_map_region); =20 + while ((md =3D efi_map_next_entry(md))) efi_map_region(md); - - if (left < desc_size) { - new_memmap =3D realloc_pages(new_memmap, *pg_shift); - if (!new_memmap) - return NULL; - - left +=3D PAGE_SIZE << *pg_shift; - (*pg_shift)++; - } - - memcpy(new_memmap + (*count * desc_size), md, desc_size); - - left -=3D desc_size; - (*count)++; - } - - return new_memmap; } =20 static void __init kexec_enter_virtual_mode(void) @@ -749,25 +702,10 @@ static void __init kexec_enter_virtual_mode(void) */ static void __init __efi_enter_virtual_mode(void) { - int count =3D 0, pg_shift =3D 0; - void *new_memmap =3D NULL; efi_status_t status; + unsigned long size; unsigned long pa; =20 - if (efi_alloc_page_tables()) { - pr_err("Failed to allocate EFI page tables\n"); - goto err; - } - - efi_merge_regions(); - new_memmap =3D efi_map_regions(&count, &pg_shift); - if (!new_memmap) { - pr_err("Error reallocating memory, EFI runtime non-functional!\n"); - goto err; - } - - pa =3D __pa(new_memmap); - /* * Unregister the early EFI memmap from efi_init() and install * the new EFI memory map that we are about to pass to the @@ -775,22 +713,33 @@ static void __init __efi_enter_virtual_mode(void) */ efi_memmap_unmap(); =20 - if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) { + if (efi_alloc_page_tables()) { + pr_err("Failed to allocate EFI page tables\n"); + goto err; + } + + size =3D efi.memmap.desc_size * efi.memmap.num_valid_entries; + if (efi_memmap_init_late(efi.memmap.phys_map, size)) { pr_err("Failed to remap late EFI memory map\n"); goto err; } =20 + efi_merge_regions(); + efi_map_regions(); + if (efi_enabled(EFI_DBG)) { pr_info("EFI runtime memory map:\n"); efi_print_memmap(); } =20 - if (efi_setup_page_tables(pa, 1 << pg_shift)) + if (efi_setup_page_tables(efi.memmap.phys_map, + DIV_ROUND_UP(size, PAGE_SIZE))) goto err; =20 efi_sync_low_kernel_mappings(); =20 - status =3D efi_set_virtual_address_map(efi.memmap.desc_size * count, + pa =3D efi.memmap.phys_map; + status =3D efi_set_virtual_address_map(size, efi.memmap.desc_size, efi.memmap.desc_version, (efi_memory_desc_t *)pa, --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 734103B19B8 for ; Thu, 19 Mar 2026 09:06:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911182; cv=none; b=kPMqFJZs1SZTCyBEIAKXhIUFQC6xl3SUGDjEPTVHx3aoVfV/1yPkwO6XbS9p4Too9q0J1fBWJDw9pNKN1vxhH7+2+3qnGUcFKxc1BwexlVpKpb3pEy0spUP+IDdeSXp9gQfQC2rbyiyuSQBBk9fav2Xg9LWcZy6AlQhKqdIW7Io= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911182; c=relaxed/simple; bh=5L36GgwG9oV73XOZeL4vV75HgPZS6IG/54OunU8ZP+M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QUYNkAn36e8JgFgCqjEl4gTwPf4k496uY3t3r66SE+l2FvNWoBGQHJQz7yJ/EXtXleDur6A0CbXrG/sWLpISAbyWUxxM2Q2JqVAoc9jl5IhCbdO/B4M0WFr5kQt5/N99DdrkNgKR/Et6hjsomuVYoKlXMrH/95LgLkFSJIiDJ8Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=U8naiSs8; arc=none smtp.client-ip=209.85.208.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="U8naiSs8" Received: by mail-ed1-f73.google.com with SMTP id 4fb4d7f45d1cf-66142e571c9so1109831a12.3 for ; Thu, 19 Mar 2026 02:06:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911179; x=1774515979; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=wclTurTdnWGKGBQy4DZr4fdQXetijg29sgghGdGIfUI=; b=U8naiSs8A0/xemXYHF/GpziN06BoEMGMx3oHjQ71CpmaE4dx5imFR17O7otYoYO5jG d0YStooHqAIhxVlcjms5eJPg6DYG9kO4ZKvFiIIBKJ331cYFk7W/Iw6vn6lD3FDlIT9O EAIb6ca7elVQOVxshHNfTJRCF2d9Qhry1ZItKkWFnSw255CbkzCegM3U1LQlyC14q5z/ infN2aiFe/iWH6y3zI/zDlSIki0yQQPVNRjNi7NOohUs4oGIU5GwQvZj9BiXsVd+0Ddm 1aLCQEaXCgved+6Bfgo4JhgAdqCjXk6eQ7Ka/R9Bf6n8gshEzbBVHr/QBDH6TwJ702dm 8yZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911179; x=1774515979; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wclTurTdnWGKGBQy4DZr4fdQXetijg29sgghGdGIfUI=; b=AJGnRt92CFBKa1gEasQ5VeCLwJ9F8/EdKgIOE+goGdHngRX55X99cbrGB164SR3LxQ qxnUWdq4XnI5ZEhkggX+/JP47m1fKn8DTw7hDxjUtS6fHYhh0fX85PcejLqgM4m3zdf/ zdQ37nRQWUpF1SwrKC4uBXjDI5Nd4hZ4g4CTlhrn502/ARyVDyJnZXM20V+UZKoPzC9v TObUvonEzkAaUjYhADm0HKP6XITBy0g7EdaYa7yEtbWAHuBtfqoe9TQLfvalG5eTT6U7 rycPnePXYY1p6rVvkriW0/8jvXqBgE/3aFv6WAvwd3KdjHRWxrP1QMeBaol8i6Me8Bbn eEpQ== X-Gm-Message-State: AOJu0Yzf1xa2kNyAzJ9fNun85qR2bH6HT7bZmkW1BC67QzvaUEpOJeQo f5/722cd4WhsyH41hqZH4wkbMWhak1P4y947eLvbUgqI2Vf9s68W2/4TJNQcTtBgm62lZK6OkY7 HeXrZ8q2sr8GViM5DMYl/eRqTrM5WWge8IBg7uYARqrdl+uIObNq1/3BzMu9b+Hf3oo9wHAnk0D +7LMG/DGiqAd3ssEkv2aeuI7mEJxDP3b979w== X-Received: from eddq7.prod.google.com ([2002:a05:6402:5187:b0:667:94ad:60ed]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6402:27ca:b0:667:53aa:d27e with SMTP id 4fb4d7f45d1cf-667b2ff64d0mr4218306a12.18.1773911178384; Thu, 19 Mar 2026 02:06:18 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:45 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=4012; i=ardb@kernel.org; h=from:subject; bh=osR4eXnUzICwAqwqWOcXt+WcNC9ItrGU/4bEgYiufqI=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3ntz7+v/3iuzbtW97YHWT+IYpWpr8P08ebnpx2muJs uwBxm77jlIWBjEuBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjARG1ZGhqVbUhuXfnj0qvSg uqHOBreweDW9uinB/w0Zt3CvUOd+Y8PIsGHapeaPEwLWvn3O+/PxDs/k68VmkVOXrXup4rxBef+ 37TwA X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-36-ardb+git@google.com> Subject: [PATCH v2 15/19] x86/efi: Use iterator API when mapping EFI regions for runtime From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Use the generic EFI memory map iterators to invoke efi_map_region() on each entry in the map. x86_64 and i386 traverse the map in opposite order, so the two cases are handled separately. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/efi.c | 90 +++++--------------- 1 file changed, 21 insertions(+), 69 deletions(-) diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index 44d106879120..8778ad441c42 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -498,73 +498,6 @@ void __init efi_init(void) efi_print_memmap(); } =20 -/* - * Iterate the EFI memory map in reverse order because the regions - * will be mapped top-down. The end result is the same as if we had - * mapped things forward, but doesn't require us to change the - * existing implementation of efi_map_region(). - */ -static inline void *efi_map_next_entry_reverse(void *entry) -{ - /* Initial call */ - if (!entry) - return efi_memdesc_ptr(efi.memmap.map, efi.memmap.desc_size, - efi.memmap.num_valid_entries - 1); - - entry -=3D efi.memmap.desc_size; - if (entry < efi.memmap.map) - return NULL; - - return entry; -} - -/* - * efi_map_next_entry - Return the next EFI memory map descriptor - * @entry: Previous EFI memory map descriptor - * - * This is a helper function to iterate over the EFI memory map, which - * we do in different orders depending on the current configuration. - * - * To begin traversing the memory map @entry must be %NULL. - * - * Returns %NULL when we reach the end of the memory map. - */ -static void *efi_map_next_entry(void *entry) -{ - if (efi_enabled(EFI_64BIT)) { - /* - * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE - * config table feature requires us to map all entries - * in the same order as they appear in the EFI memory - * map. That is to say, entry N must have a lower - * virtual address than entry N+1. This is because the - * firmware toolchain leaves relative references in - * the code/data sections, which are split and become - * separate EFI memory regions. Mapping things - * out-of-order leads to the firmware accessing - * unmapped addresses. - * - * Since we need to map things this way whether or not - * the kernel actually makes use of - * EFI_PROPERTIES_TABLE, let's just switch to this - * scheme by default for 64-bit. - */ - return efi_map_next_entry_reverse(entry); - } - - /* Initial call */ - if (!entry) - return efi.memmap.map; - - entry +=3D efi.memmap.desc_size; - if (entry >=3D (void *)efi_memdesc_ptr(efi.memmap.map, - efi.memmap.desc_size, - efi.memmap.num_valid_entries)) - return NULL; - - return entry; -} - static bool should_map_region(const efi_memory_desc_t *md, int unused) { /* @@ -624,8 +557,27 @@ static void __init efi_map_regions(void) =20 efi_memmap_filter_entries(should_map_region); =20 - while ((md =3D efi_map_next_entry(md))) - efi_map_region(md); + /* + * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE config table feature + * requires us to map all entries in the same order as they appear in + * the EFI memory map. That is to say, entry N must have a lower + * virtual address than entry N+1. This is because the firmware + * toolchain leaves relative references in the code/data sections, + * which are split and become separate EFI memory regions. Mapping + * things out-of-order leads to the firmware accessing unmapped + * addresses. + * + * Since we need to map things this way whether or not the kernel + * actually makes use of EFI_PROPERTIES_TABLE, let's just switch to + * this scheme by default for 64-bit. + */ + if (efi_enabled(EFI_64BIT)) { + for_each_efi_memory_desc_rev(md) + efi_map_region(md); + } else { + for_each_efi_memory_desc(md) + efi_map_region(md); + } } =20 static void __init kexec_enter_virtual_mode(void) --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4232B3B8939 for ; Thu, 19 Mar 2026 09:06:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911183; cv=none; b=S/Cz+3zN7u0xIlImJRXpMypMlUuyd9rbXSTqqPZzMbRQ1MlGiRzj0/Bckb2uNY7gtpe2L2a4iF6wyTPL6bRhht+j1shGE5V6GSO48aZyL1Ek2CoSd8CQ26V5QK1yrCMKVZAum4S+FE2M3q/VDE4VU6G+lgCvZnXJYMxfrhFraAE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911183; c=relaxed/simple; bh=t3nPoGOyUjM7hYdKWk6iAYk/xsxkEy+tCPQyZ8JyK60=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pZ0Mc5wifFiAxALec8hHiPFcrjuFRqBzmmPykrIZi+4J0fiuX6EWTxuTV7wEMQRlPAPB1DSeh/pYkhh2vjdx1B/5Deg+EwSU6sDZy+TJJmj0yB6Y1Z+5bKP8+nLj4Io+ZBV5T9HvWwiGwcBf9skbuvg1Q7LZnO3ZaP1DBvbASTg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uPnFYDHl; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uPnFYDHl" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-48535f4d5e1so6696505e9.0 for ; Thu, 19 Mar 2026 02:06:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911180; x=1774515980; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=nLbx4kYoDNEcWBWJkoMx59qTYGPcW/RbepHiDjr2l9w=; b=uPnFYDHl9r3zKHLQfMoQx1wNlQhJMX6kfAnZmG6J5W9uHyiSOCCqX41s1O64i+lv3w OmNBwY1f5VHLQAgG1SFsVnpcH8vmuDC1NiX/P39XJrhGLnZELYpsnMK7RT8JaQd8TqDd z7bfHco+KUWnxAo8RxbsXBq/4Rczk1ck1TYvgJ1WfSfinFAPNTJaGqXQfXzkVVSOJWS+ 762qBzsLVzEwYBuzF4cIlkwT/UAoo56Mshvyt3OWp31hPVSfE2El1DdMdw3Ed9OQeuP+ QfXma07hvUSj7L4UozVJMUBMx+BcMkKsu9Lct8WR+Ox25KIcUm+K1mE6F/XpCK8+FEzQ TpyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911180; x=1774515980; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nLbx4kYoDNEcWBWJkoMx59qTYGPcW/RbepHiDjr2l9w=; b=kEbrSwLEUjPLi0b8BPEJtGtt7tzvONlZgGnGg8YaHJ5necNZtuD5Vvvne+ecPcQTM0 Md/kpI51HYcSeEI5EDbsTVoNIOFEF9PhxNRMMWfaHkopw/oIraeq12Ei55EPuGz3Igq5 OSTE4yzCWVk9KsGgZ3kGgU16v04pHKWmO1x+sX+uv/d4S52cpMyTFsx1hginy2CTtpHo qsyErxDGdc86RuT+vUKiFmvTqU/9l67omc3mQqFzKGqEoUsq2aN1auKvmYR1hv8C9Hqd JZd8DOgMQuN6MxfWorpKNPpXhDcx1tb4VZYuTwnOt9uu9T0pycPYCLWYwv2Tz7kxHAqQ x7IA== X-Gm-Message-State: AOJu0YwxMf4MzqKV6fvFUGcNdBca9wAMSqmaB0Xm/M0/j7iFmlC5049U Vh9sD9mEiEb/Gy+lKKKRj5gGXVmOC3mUapQ80AnSfn20nV1aHIRjAY7HHW7pNFjJwjsAFz6QPqp ZRbkrFgZkSMdbk1I+U+vpV8peiXbL2xNMfTCijYR11XDF4ZxI4Axk787XuCewxJnP9ii6wyJNHO 8K3QM4PUP8tBp/uvEdxJJ+sp286jv1Hb/yCg== X-Received: from wmdd6.prod.google.com ([2002:a05:600c:a206:b0:485:3f38:3dd2]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3b95:b0:486:fc46:be9e with SMTP id 5b1f17b1804b1-486fc46c095mr12817435e9.24.1773911179440; Thu, 19 Mar 2026 02:06:19 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:46 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=8551; i=ardb@kernel.org; h=from:subject; bh=WbQDZgAuzEkcDHmpW7jdUizVT209J0oavj2ncp5TycQ=; b=kA0DAAoWMG4JVi59LVwByyZiAGm7vG6gdoNi8n4/Q9w++NnvTrfzQHIW/IagFmcE96HWm1dtV Ih1BAAWCgAdFiEEEJv97rnLkRp9Q5odMG4JVi59LVwFAmm7vG4ACgkQMG4JVi59LVx0JwD+Ksoc DlvEzSIjnSSSSvgr4/2kKWaJ0mnFScvYmGYTqykBAKvh2UPqDhm5xcD9IXG3TVeTjgQ1WDYRsgy FEb3SNaEP X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-37-ardb+git@google.com> Subject: [PATCH v2 16/19] x86/efi: Reuse memory map instead of reallocating it From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel The EFI memory map consists of 10s to 100s of entries of around 40 bytes each. The initial version is allocated and populated by the EFI stub, but later on, after freeing the boot services data regions and pruning the associated entries, a new memory map is allocated with room for only the remaining entries, which are typically much fewer in number. Given that the original allocation is never freed, this does not actually save any memory currently, and it is much simpler to just move the entries that need to be preserved to the beginning of the map, and truncate it. That way, a lot of the complicated memory map allocation and freeing code can simply be dropped. Signed-off-by: Ard Biesheuvel --- arch/x86/include/asm/efi.h | 5 - arch/x86/platform/efi/Makefile | 2 +- arch/x86/platform/efi/memmap.c | 112 -------------------- arch/x86/platform/efi/quirks.c | 30 +----- include/linux/efi.h | 5 +- 5 files changed, 6 insertions(+), 148 deletions(-) diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index 44cdd3c1055e..f21b9e85f544 100644 --- a/arch/x86/include/asm/efi.h +++ b/arch/x86/include/asm/efi.h @@ -398,11 +398,6 @@ static inline void efi_reserve_boot_services(void) } #endif /* CONFIG_EFI */ =20 -extern int __init efi_memmap_alloc(unsigned int num_entries, - struct efi_memory_map_data *data); - -extern int __init efi_memmap_install(struct efi_memory_map_data *data); - extern enum efi_secureboot_mode __x86_ima_efi_boot_mode(void); =20 #define arch_ima_efi_boot_mode __x86_ima_efi_boot_mode() diff --git a/arch/x86/platform/efi/Makefile b/arch/x86/platform/efi/Makefile index 500cab4a7f7c..28772e046a1b 100644 --- a/arch/x86/platform/efi/Makefile +++ b/arch/x86/platform/efi/Makefile @@ -2,7 +2,7 @@ KASAN_SANITIZE :=3D n GCOV_PROFILE :=3D n =20 -obj-$(CONFIG_EFI) +=3D memmap.o quirks.o efi.o efi_$(BITS).o \ +obj-$(CONFIG_EFI) +=3D quirks.o efi.o efi_$(BITS).o \ efi_stub_$(BITS).o obj-$(CONFIG_EFI_MIXED) +=3D efi_thunk_$(BITS).o obj-$(CONFIG_EFI_RUNTIME_MAP) +=3D runtime-map.o diff --git a/arch/x86/platform/efi/memmap.c b/arch/x86/platform/efi/memmap.c deleted file mode 100644 index fa580c4122c4..000000000000 --- a/arch/x86/platform/efi/memmap.c +++ /dev/null @@ -1,112 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* - * Common EFI memory map functions. - */ - -#define pr_fmt(fmt) "efi: " fmt - -#include -#include -#include -#include -#include -#include -#include -#include - -static phys_addr_t __init __efi_memmap_alloc_early(unsigned long size) -{ - return memblock_phys_alloc(size, SMP_CACHE_BYTES); -} - -static phys_addr_t __init __efi_memmap_alloc_late(unsigned long size) -{ - unsigned int order =3D get_order(size); - struct page *p =3D alloc_pages(GFP_KERNEL, order); - - if (!p) - return 0; - - return PFN_PHYS(page_to_pfn(p)); -} - -static -void __init __efi_memmap_free(u64 phys, unsigned long size, unsigned long = flags) -{ - if (flags & EFI_MEMMAP_MEMBLOCK) { - if (slab_is_available()) - memblock_free_late(phys, size); - else - memblock_phys_free(phys, size); - } else if (flags & EFI_MEMMAP_SLAB) { - struct page *p =3D pfn_to_page(PHYS_PFN(phys)); - unsigned int order =3D get_order(size); - - __free_pages(p, order); - } -} - -/** - * efi_memmap_alloc - Allocate memory for the EFI memory map - * @num_entries: Number of entries in the allocated map. - * @data: efi memmap installation parameters - * - * Depending on whether mm_init() has already been invoked or not, - * either memblock or "normal" page allocation is used. - * - * Returns zero on success, a negative error code on failure. - */ -int __init efi_memmap_alloc(unsigned int num_entries, - struct efi_memory_map_data *data) -{ - /* Expect allocation parameters are zero initialized */ - WARN_ON(data->phys_map || data->size); - - data->size =3D num_entries * efi.memmap.desc_size; - data->desc_version =3D efi.memmap.desc_version; - data->desc_size =3D efi.memmap.desc_size; - data->flags &=3D ~(EFI_MEMMAP_SLAB | EFI_MEMMAP_MEMBLOCK); - data->flags |=3D efi.memmap.flags & EFI_MEMMAP_LATE; - - if (slab_is_available()) { - data->flags |=3D EFI_MEMMAP_SLAB; - data->phys_map =3D __efi_memmap_alloc_late(data->size); - } else { - data->flags |=3D EFI_MEMMAP_MEMBLOCK; - data->phys_map =3D __efi_memmap_alloc_early(data->size); - } - - if (!data->phys_map) - return -ENOMEM; - return 0; -} - -/** - * efi_memmap_install - Install a new EFI memory map in efi.memmap - * @data: efi memmap installation parameters - * - * Unlike efi_memmap_init_*(), this function does not allow the caller - * to switch from early to late mappings. It simply uses the existing - * mapping function and installs the new memmap. - * - * Returns zero on success, a negative error code on failure. - */ -int __init efi_memmap_install(struct efi_memory_map_data *data) -{ - unsigned long size =3D efi.memmap.map_end - efi.memmap.map; - unsigned long flags =3D efi.memmap.flags; - u64 phys =3D efi.memmap.phys_map; - int ret; - - efi_memmap_unmap(); - - if (efi_enabled(EFI_PARAVIRT)) - return 0; - - ret =3D __efi_memmap_init(data); - if (ret) - return ret; - - __efi_memmap_free(phys, size, flags); - return 0; -} diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index eecaa745d352..dc90c35480f8 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -356,12 +356,10 @@ static struct efi_freeable_range *ranges_to_free; =20 void __init efi_unmap_boot_services(void) { - struct efi_memory_map_data data =3D { 0 }; efi_memory_desc_t *md; - int num_entries =3D 0; + void *new_md; int idx =3D 0; size_t sz; - void *new, *new_md; =20 /* Keep all regions for /sys/kernel/debug/efi */ if (efi_enabled(EFI_DBG)) @@ -374,13 +372,13 @@ void __init efi_unmap_boot_services(void) return; } =20 + new_md =3D efi.memmap.map; for_each_efi_memory_desc(md) { unsigned long long start =3D md->phys_addr; unsigned long long size =3D md->num_pages << EFI_PAGE_SHIFT; =20 if (md->type !=3D EFI_BOOT_SERVICES_CODE && md->type !=3D EFI_BOOT_SERVICES_DATA) { - num_entries++; continue; } =20 @@ -394,7 +392,6 @@ void __init efi_unmap_boot_services(void) /* Do not free, someone else owns it: */ if ((md->attribute & EFI_MEMORY_RUNTIME) || !can_free_region(start, size)) { - num_entries++; continue; } =20 @@ -409,26 +406,12 @@ void __init efi_unmap_boot_services(void) idx++; } =20 - if (!num_entries) - return; - - if (efi_memmap_alloc(num_entries, &data) !=3D 0) { - pr_err("Failed to allocate new EFI memmap\n"); - return; - } - - new =3D memremap(data.phys_map, data.size, MEMREMAP_WB); - if (!new) { - pr_err("Failed to map new EFI memmap\n"); - return; - } - /* * Build a new EFI memmap that excludes any boot services * regions that are not tagged EFI_MEMORY_RUNTIME, since those * regions have now been freed. */ - new_md =3D new; + new_md =3D efi.memmap.map; for_each_efi_memory_desc(md) { if (!(md->attribute & EFI_MEMORY_RUNTIME) && (md->type =3D=3D EFI_BOOT_SERVICES_CODE || @@ -442,12 +425,7 @@ void __init efi_unmap_boot_services(void) new_md +=3D efi.memmap.desc_size; } =20 - memunmap(new); - - if (efi_memmap_install(&data) !=3D 0) { - pr_err("Could not install new EFI memmap\n"); - return; - } + efi.memmap.num_valid_entries =3D (new_md - efi.memmap.map) / efi.memmap.d= esc_size; } =20 static unsigned long __init diff --git a/include/linux/efi.h b/include/linux/efi.h index b0c3e9648126..58279538d9d8 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -553,8 +553,7 @@ struct efi_unaccepted_memory { =20 /* * Architecture independent structure for describing a memory map for the - * benefit of efi_memmap_init_early(), and for passing context between - * efi_memmap_alloc() and efi_memmap_install(). + * benefit of efi_memmap_init_early(). */ struct efi_memory_map_data { phys_addr_t phys_map; @@ -572,8 +571,6 @@ struct efi_memory_map { unsigned long desc_version; unsigned long desc_size; #define EFI_MEMMAP_LATE (1UL << 0) -#define EFI_MEMMAP_MEMBLOCK (1UL << 1) -#define EFI_MEMMAP_SLAB (1UL << 2) unsigned long flags; }; =20 --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 301423B8954 for ; Thu, 19 Mar 2026 09:06:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911184; cv=none; b=kb94d0Sm7seU7YQpPigIklFoWmEOaqiKDUxJk4eso7cxYw+i8nRNlqRoEXfCDwNEPMSwRAaompfUNYvkjngzOHQeWPKwtCxszVW0Uaefvql6k4TqF/mLsN+Ro8V1aEbnCN+lwmz1hW9QDH9isozo+UA4eXnoUQYQ+oxqh+k6OwA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911184; c=relaxed/simple; bh=nI4mHJyEteFzkyvhEuW7RcAeMyYIXmhiDsYHb2L7p68=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oAovLsNXT9kGH6RfbMvTfIa2zrI5isC1rJMVCe7bKq4S4LF+Kxtj3l4tbZ8lQDrEzegJ7c7YoX2gXFojRnG+UeRve8dt3LqAAYoJiBksP2hkAsQgq4Obx0kvQHyCbpaOauYTLgO0eqqjHII4J++2UG7q72lcN0nPMBbq3NmQXcs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=neXXHhvo; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="neXXHhvo" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-486fb142205so3556835e9.1 for ; Thu, 19 Mar 2026 02:06:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911180; x=1774515980; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=URGE+FNnkxaT+QXVuJx3nb8mizqLFv3DlCZi6MMrS8M=; b=neXXHhvoiuF3UPY1TF+kV9uWak3RFLPiHxDGCNEuF+Ic5DWqbmOfIPiVRyEGcwPzCd T6WUc1Z/gou4xPtlSgmvvb+tdgOeHKzURrLeRsEto9WHb/ZHX/Yvq1Bb94/iSO847E0H ClrHhMyhmMk/WcuWV5YM1lQHxruckzk6YU3kqlnQk/TxRivvyOEo5l5v5kFcRYxjAJin +yZPPSgzhN2Tz05pl60nM4BpmkC8IIjdCKC00lWGFs9Marg7aFMZZjcWOWHsd+1ASgKJ AeLstJVPxqfhJEVmzxzg2y0FgCTXCByfac/BbAFShcsbEjxept1CEM/+LiOBTTXVo6xk iR0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911180; x=1774515980; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=URGE+FNnkxaT+QXVuJx3nb8mizqLFv3DlCZi6MMrS8M=; b=BMF4Hq8yokPR/+w2ZTv+cGb3bCJn16BzYSLNTNfR392B8eIIGKPYMqC3IZPlqLl8JD LIaTe5lMyvxIuEjKfFC6CXATrjN8WnNreMHgyHC/DDcSg9QaYRyeWvfAEEkOEJ3qjRsV ZeBNhOQpEoQcfiSofwd1fRdJGmEZbWeSiiMHqjQnUzK/klkCCzpHFiQuWx+QUzDZjQsK kfGTp/Fm5A1/5hfEfILkdj92fQvEoOXRWIrsnJbbaSD06G7uTcSMcrb9hukGPJDHvPwT qhYD383C93BzpNAKJfhNLG8PHWRINeFOgNFiuM1C9EcVULjqlCDxMpIeg+mgFD2eNbb2 habQ== X-Gm-Message-State: AOJu0YywOu+i1PxoeNbU42/XzugEm/lr96NfYROJzCL9uAjNC9YF7C3e JLhqwr7zsawDqsAHsvLb+2wCYItLlLzgzF/P6QcJBBJoe8Nkp1D6XRnV689YYf0swFgWMESMra5 vFwSPSta7g4OZHR1l2Ik3YYjpp4Ebx5ocLL1X80XdK5M07PfGMQ6pyTOXRPxdceF8kFSnk7pwhw 8V9Mhry1cXoKSymqZP71SSIMqIikSYgHou7g== X-Received: from wrwr1.prod.google.com ([2002:a5d:6941:0:b0:439:cf20:ddf7]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:198f:b0:485:5981:1411 with SMTP id 5b1f17b1804b1-486f4472ee8mr109908875e9.23.1773911180344; Thu, 19 Mar 2026 02:06:20 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:47 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5938; i=ardb@kernel.org; h=from:subject; bh=eNWFdjaeeovD+4Yb/UStx9oLjOGRMrmlsCh1tKCjcgI=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3nvy39kftbt9f8+z3rwD9C1rfEnQ6+97uatHlyVg7M 4A5Le9gRykLgxgXg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZiI+QuG/94rHGOtDsv8Mdb5 U9hRKR+bPGFi3d75roz8ftfN52mXzmRk6Fi306h9rczpvav4ddku2jw/UfM+Y6I8p6L1A2ur2ux cRgA= X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-38-ardb+git@google.com> Subject: [PATCH v2 17/19] x86/efi: Defer compaction of the EFI memory map From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Currently, the EFI memory map is compacted early at boot, to leave only the entries that are significant to the current kernel or potentially a kexec'ed kernel that comes after, and to suppress all boot services code and data entries that have no correspondence with anything that either the firmware or the kernel treats as reserved for firmware use. Given that actually freeing those regions to the page allocator is not possible yet at this point, those suppressed entries are converted into yet another type of temporary memory reservation map, and freed during an arch_initcall(), which is the earliest convenient time to actually perform this operation. Given that compacting the memory map does not need to occur that early to begin with, move it to the arch_initcall(). This removes the need for the special memory reservation map, as the entries still exist at this point, and can be consulted directly to decide whether they need to be preserved in their entirety or only partially. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 110 +++++++------------- 1 file changed, 39 insertions(+), 71 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index dc90c35480f8..bc9dfe7925aa 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -347,36 +347,11 @@ static void __init efi_unmap_pages(efi_memory_desc_t = *md) pr_err("Failed to unmap VA mapping for 0x%llx\n", va); } =20 -struct efi_freeable_range { - u64 start; - u64 end; -}; - -static struct efi_freeable_range *ranges_to_free; - void __init efi_unmap_boot_services(void) { efi_memory_desc_t *md; - void *new_md; - int idx =3D 0; - size_t sz; =20 - /* Keep all regions for /sys/kernel/debug/efi */ - if (efi_enabled(EFI_DBG)) - return; - - sz =3D sizeof(*ranges_to_free) * efi.memmap.num_valid_entries + 1; - ranges_to_free =3D kzalloc(sz, GFP_KERNEL); - if (!ranges_to_free) { - pr_err("Failed to allocate storage for freeable EFI regions\n"); - return; - } - - new_md =3D efi.memmap.map; for_each_efi_memory_desc(md) { - unsigned long long start =3D md->phys_addr; - unsigned long long size =3D md->num_pages << EFI_PAGE_SHIFT; - if (md->type !=3D EFI_BOOT_SERVICES_CODE && md->type !=3D EFI_BOOT_SERVICES_DATA) { continue; @@ -385,47 +360,10 @@ void __init efi_unmap_boot_services(void) /* * Before calling set_virtual_address_map(), EFI boot services * code/data regions were mapped as a quirk for buggy firmware. - * Unmap them from efi_pgd before freeing them up. + * Unmap them from efi_pgd, they will be freed later. */ efi_unmap_pages(md); - - /* Do not free, someone else owns it: */ - if ((md->attribute & EFI_MEMORY_RUNTIME) || - !can_free_region(start, size)) { - continue; - } - - /* - * With CONFIG_DEFERRED_STRUCT_PAGE_INIT parts of the memory - * map are still not initialized and we can't reliably free - * memory here. - * Queue the ranges to free at a later point. - */ - ranges_to_free[idx].start =3D start; - ranges_to_free[idx].end =3D start + size; - idx++; } - - /* - * Build a new EFI memmap that excludes any boot services - * regions that are not tagged EFI_MEMORY_RUNTIME, since those - * regions have now been freed. - */ - new_md =3D efi.memmap.map; - for_each_efi_memory_desc(md) { - if (!(md->attribute & EFI_MEMORY_RUNTIME) && - (md->type =3D=3D EFI_BOOT_SERVICES_CODE || - md->type =3D=3D EFI_BOOT_SERVICES_DATA) && - can_free_region(md->phys_addr, - md->num_pages << EFI_PAGE_SHIFT)) { - continue; - } - - memcpy(new_md, md, efi.memmap.desc_size); - new_md +=3D efi.memmap.desc_size; - } - - efi.memmap.num_valid_entries =3D (new_md - efi.memmap.map) / efi.memmap.d= esc_size; } =20 static unsigned long __init @@ -464,27 +402,57 @@ efi_free_unreserved_subregions(u64 range_start, u64 r= ange_end) =20 static int __init efi_free_boot_services(void) { - struct efi_freeable_range *range =3D ranges_to_free; unsigned long freed =3D 0; + efi_memory_desc_t *md; + void *new_md; + + /* No EFI memory map or it came from the preceding kernel? */ + if (efi_setup || !efi_enabled(EFI_MEMMAP)) + return 0; =20 - if (!ranges_to_free) + /* Keep all regions for /sys/kernel/debug/efi */ + if (efi_enabled(EFI_DBG)) return 0; =20 - while (range->start) { + new_md =3D efi.memmap.map; + for_each_efi_memory_desc(md) { /* * Don't free memory under 1M for two reasons: * - BIOS might clobber it * - Crash kernel needs it to be reserved */ - u64 start =3D max(range->start, SZ_1M); + u64 md_start =3D max(md->phys_addr, SZ_1M); + u64 md_end =3D md->phys_addr + md->num_pages * EFI_PAGE_SIZE; + bool preserve_entry =3D md->attribute & EFI_MEMORY_RUNTIME; =20 - if (start >=3D range->end) + if (md_start >=3D md_end) continue; =20 - freed +=3D efi_free_unreserved_subregions(start, range->end); - range++; + if (!(md->attribute & EFI_MEMORY_RUNTIME) && + (md->type =3D=3D EFI_BOOT_SERVICES_CODE || + md->type =3D=3D EFI_BOOT_SERVICES_DATA)) { + u64 f =3D efi_free_unreserved_subregions(md_start, md_end); + + /* + * Omit the memory map entry of this region only if it + * has been freed entirely. This ensures that boot data + * regions for things like ESRT and BGRT tables carry + * over correctly during kexec. + */ + if (f < md_end - md_start) + preserve_entry =3D true; + + freed +=3D f; + } + + if (preserve_entry) { + if (new_md !=3D md) + memcpy(new_md, md, efi.memmap.desc_size); + new_md +=3D efi.memmap.desc_size; + } } - kfree(ranges_to_free); + + efi.memmap.num_valid_entries =3D (new_md - efi.memmap.map) / efi.memmap.d= esc_size; =20 if (freed) pr_info("Freeing EFI boot services memory: %ldK\n", freed / SZ_1K); --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F01E63B8BC4 for ; Thu, 19 Mar 2026 09:06:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911185; cv=none; b=soVyd/q5HRER33EgSpaPew0Uk6Sr5nk7OjISUo4IA71AJrDJjLdabDiqNKOE+8TWAZfqftB+DtOpRRMQzYQ+5WAoxdqIuZlDs7DpDZSMUWEnk4H+JR/dcD2gRw7nulw70xbU4ykb27+2nH5sLVhGRBlBB/6UIwFkTVDotdtS6Ug= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911185; c=relaxed/simple; bh=mj1/FdNrxSJomn4MqIFLdYl2xhCRYAPFvrFp7cM1XGg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GkEyYngE92qCRtRly7yqPUBe2beCiELUJABJj1VMF4eqQFWiuzWizec4EvqB3kOZa9LlY8TH3+9dl4rBqq1uTonsg211mIHS8J+Q/d9jhf8+3u5s5SfZ8o1LfHWVa+1sULjVh7pvaQDG+SJJRZQWN/75oXVXqCi3UiJwqE+TIcY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=FWZ1d8y+; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FWZ1d8y+" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-486f830f4e4so7852565e9.1 for ; Thu, 19 Mar 2026 02:06:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911181; x=1774515981; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FUd0sbvQPAQ2xS+BnxTruRhjwkkPtC2MAYLp44Di0G0=; b=FWZ1d8y+uyicpXySBLl5Rz1PHbCmma5CJrIfZm1F3nnwgRtiQFXuqiKwtd7d/tLzdd ZbStXY3oennDnoTUh2l861GRV29V8+C+mmG4gkS1ZAIIDBjjDKPA/1XmAe2BKS/ra53u FYaZe48LBHAt3Oh6cpv5V+fGD/WVUMBEGspknLVI9506zWT0mkLWLgxJJ91WW2gIXHeC 7coQ15VLnKVImMksdtcntoG9fnc6tFAZqZEfd581atiAgfbyugmRXu94pn+Dq/Uj9KYy QgRByIFLEEK4ZZ4ApeZcVQ5rlmW2A1XRPU0O/Ql742XZzFHqxyWtCVQjy0OrQJ+hSMyG I2gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911181; x=1774515981; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FUd0sbvQPAQ2xS+BnxTruRhjwkkPtC2MAYLp44Di0G0=; b=o6VtVOynqNQBbIjzu82LnRMtOpKUBQ7Ptqo4HkzKnoP3AjnTk6jRA+9yJGA94yRhNG TtraRl/6vdZKg0Zg6fH/zNROa/CAvfl/vPHjSTSeqqtnmKhiWEBcW++X0WPnEcAAryGG N4oZ0Dw7Sh6FiZVOIG5GzsBvt7sqQye5Lc72UxqrspcvlSLe8tZ1rIG8/sQ8Eb7WReJF gGscqfCmmjYiQNcC8CpFvw97mfDRtSXJYwjvudnMwTxAwFWbpnn4rh1iK9flRjAdior/ WH920Gy1DmcTR/A7Q+p05AV14TUOJIfVmihtuU3SPDlxlk0mcD63mwdDwV+piCyNL5TS Umgg== X-Gm-Message-State: AOJu0YxhpoISaT0AGSOgAdqTplCCAdj55AZgLqftEr1f2DAF9MMfsNrF LHRtSJACbenw95Y5kjcIamphH6rY4eKqk5GOoXEeBh9fJAp3PKGXRY0eN1LXNDdjOBo3Uy5UEzL IcscQ6npaRaXD9hkSUchx8sZh72SQOZRwM0e9nVFFpylytzT41szzCKVu8vy4FUrQ5dSSJVytrG Opr0i7Xvx94qVLc03kJE8FoQXElEVlaRi8Hw== X-Received: from wmla9.prod.google.com ([2002:a05:600d:2389:b0:483:2ce9:2b05]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:19d4:b0:483:6a8d:b2f9 with SMTP id 5b1f17b1804b1-486f4440f10mr104373905e9.5.1773911181125; Thu, 19 Mar 2026 02:06:21 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:48 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=3551; i=ardb@kernel.org; h=from:subject; bh=RgYBBHo63dVXcto8c6eRW4IKNVj2ghfqwhRHPXlXxVo=; b=kA0DAAoWMG4JVi59LVwByyZiAGm7vHDIobmpC70ggS1pj4SINEncGlW8wWrCfQ0jZalrKMRXE 4h1BAAWCgAdFiEEEJv97rnLkRp9Q5odMG4JVi59LVwFAmm7vHAACgkQMG4JVi59LVxv0gD/aujA z6zrIy1wbJRsucHeItyXF97MaCh3f6yCiQw6osIBALb4WdyVOHpr3KsQxhZgsDHRj0VeMhhAEog 4NeVDeFUO X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-39-ardb+git@google.com> Subject: [PATCH v2 18/19] x86/efi: Do not abuse RUNTIME bit to mark boot regions as reserved From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel efi_reserve_boot_regions() marks all EFI boot services memory regions as memblock_reserve()'d temporarily, so that they can be mapped in the EFI page tables during the call to the SetVirtualAddressMap() runtime service. This means it has to take care to distinguish between regions that are entirely unused from regions that are already covered by some prior reservations, either by the kernel itself via memblock, or via the firmware or bootloader via the E820 map. For this reason, it only memblock_reserve()'s boot services regions that are not covered by any prior memblock reservation. Otherwise, it will set the EFI_MEMORY_RUNTIME flag for the region, which indicates to the freeing code that runs later that the region must remain reserved. It also sets the EFI_MEMORY_RUNTIME flag for the region if it covers any E820 region that is not E820_RAM, so that -again- the entire region remains reserved indefinitely. This is inefficient, and abusing the EFI_MEMORY_RUNTIME flag for this is not great either. It would be better to respect the actual memblock or E820 reservations instead, which is feasible now that the freeing code takes the MEMBLOCK_RSRV_KERN flag into account. So drop the EFI_MEMORY_RUNTIME hack, and instead, respect existing memblock reservations by upgrading them to MEMBLOCK_RSRV_KERN reservations. Take E820 reservations into account by cross-referencing them with the EFI and memblock reservations when actually returning the pages back to the page allocator. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 29 ++++++-------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index bc9dfe7925aa..8f2dc477eee0 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -298,26 +298,13 @@ void __init efi_reserve_boot_services(void) */ if (!already_reserved) { memblock_reserve(start, size); - + } else { /* - * If we are the first to reserve the region, no - * one else cares about it. We own it and can - * free it later. + * Mark existing reservations as MEMBLOCK_RSRV_KERN so + * they will be respected by efi_free_boot_services(). */ - if (can_free_region(start, size)) - continue; + memblock_reserved_mark_kern(start, size); } - - /* - * We don't own the region. We must not free it. - * - * Setting this bit for a boot services region really - * doesn't make sense as far as the firmware is - * concerned, but it does provide us with a way to tag - * those regions that must not be paired with - * memblock_free_late(). - */ - md->attribute |=3D EFI_MEMORY_RUNTIME; } } =20 @@ -392,6 +379,9 @@ efi_free_unreserved_subregions(u64 range_start, u64 ran= ge_end) if (start >=3D end) continue; =20 + if (!can_free_region(start, end - start)) + continue; + free_reserved_area(phys_to_virt(start), phys_to_virt(end), -1, NULL); freed +=3D (end - start); @@ -428,9 +418,8 @@ static int __init efi_free_boot_services(void) if (md_start >=3D md_end) continue; =20 - if (!(md->attribute & EFI_MEMORY_RUNTIME) && - (md->type =3D=3D EFI_BOOT_SERVICES_CODE || - md->type =3D=3D EFI_BOOT_SERVICES_DATA)) { + if (md->type =3D=3D EFI_BOOT_SERVICES_CODE || + md->type =3D=3D EFI_BOOT_SERVICES_DATA) { u64 f =3D efi_free_unreserved_subregions(md_start, md_end); =20 /* --=20 2.53.0.851.ga537e3e6e9-goog From nobody Mon Apr 6 14:08:37 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F1BC3B8D4B for ; Thu, 19 Mar 2026 09:06:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911185; cv=none; b=hfBrotMXrrcwJ+OaR0v33XNYzAougim3OFrnvTAsTX47BKGciCeZ/ZeiBswQBCRMLno1obMP9np5YfxdfSIIL4h5AhAPBPMpNSHe74ZJGcXn1miyYqgJO0ebPx1/DlkRLZ81h3kdWGG+d0WvE4JnvHjgYVuwUBj8kUHxPlHNd5U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773911185; c=relaxed/simple; bh=Wf8kQNEMmtzZGyUJPwnpA/dfE7IQk+3+2ogaLZM7sX4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lvJF9q1t03O2MX5w/czlUaVBXDK5HoX2ZI4sL6j3JOWtN9xKSeiVMTsn8fxNWSEBa3u7gc9Nc/Y4pdDYAdEvHWElZA0UP4rgTMFlQDBeTT9kme2foO/SA0y9MOgPTfOX6obqJDsvgai5SDOYT6dqiUhH+5YB0XOHCBECuEqugaA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jddZIGLu; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jddZIGLu" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-48539bda3dcso8316535e9.2 for ; Thu, 19 Mar 2026 02:06:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773911182; x=1774515982; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hzByK+ynICbqxc0B14saHqMa64fxvk7Hl+Eh4R4V85s=; b=jddZIGLuISgU8tC5CNzndN5ZBFWO1IjGxHw1bWHUefI3FnwGU3mqaTkrV/0NCkrZpA 9j3gGbUp3528z+uoovMm02eyCavVIi0gP52F1OvCspnT8Q1rs8ZRPnK6+NRSsOs9bUzW Ho+Y1WPRWEmxdS6TXvQbPlzgDFSCMwZOwVPq4QdCuuaWRaL+tb+evZujgG3t+BAHb+xz Mr42EheGipVZZUIwDs6bhVtqc7vVKeFVot+a4lhoMMP+MZXrzHgw8yej7Kr/Z5XiXIwK xQTV9x7YgVmnuN04iL1OJ4JdEqeAGysQBkWI+ODuo7DZoAwzJmcs7LGE0/0Zi+s+Fwea /GAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773911182; x=1774515982; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hzByK+ynICbqxc0B14saHqMa64fxvk7Hl+Eh4R4V85s=; b=nI9Mmwco3Z6eWolLHKmq6m9QcaAOXt9n9NXYGvwzy6iJmc6Kd41jkHc9u/T79XwwGu u5/wRYUG7QGZXrn3BVEg3QcNCtRCs3QWiU96lixIxFvyHmt5fLYW0BHiEip2suMHsNgu 63W2LpObMR7/NkUCzYbV1Ipygfdtgnzr6PWPP0j3LcYO8De1bP5cj2P0aw/PRpKN+ePi 9Zy+9RiVrBsI12g2QyFbot5Fl9qlqLxM8SifcbjGoidTw2XHYJsufDoWOX9imZaT/qxr beJ5UBBJ5mQv1nKR6T2cbd44JqcRKPgvvPowe97kCopy3KcZCgB0sM8RFM9OtBc1sHEi aoQA== X-Gm-Message-State: AOJu0Yz8LfXakM5wfWaEi58qrh+7PUOApo+Wju5fFwk11HIoASgsI7Sa xKVHWE+NbdCtSVAedeJwG3ByHXSuwxGtta6MZ+a+jvsr1VlmTOtgwhagwPuAWgvdaSr12TFIdgE zav546OpzeDUErw3JrbAsj8HEtjXtbnOy46FecPmQViUR6x/OzYzKTjcjt6rcvMYVDKh2JjaHz6 cQ59/+8ZMQ8XgkdwOMjdM8FWZLALmvUmxaLA== X-Received: from wmlf11.prod.google.com ([2002:a7b:c8cb:0:b0:480:6c91:3efb]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8183:b0:486:f634:ef1 with SMTP id 5b1f17b1804b1-486f634100dmr86779245e9.17.1773911182276; Thu, 19 Mar 2026 02:06:22 -0700 (PDT) Date: Thu, 19 Mar 2026 10:05:49 +0100 In-Reply-To: <20260319090529.1091660-21-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260319090529.1091660-21-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=916; i=ardb@kernel.org; h=from:subject; bh=MmnX/fkpzOzS5g7YhXoL61ypB3Du4JDzx3K+Oahbalc=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXP3niLjpUdDS16wOQfX3j/PGq69bMfZEm+z978Lbs3fa 5pbPr+oo5SFQYyLQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEykWImRYdfkg05/dFVZ7h34 d2j16SezlRdtu7HMYWKTS6L+W4PVkjEM//1zlQQmfOksF7pwZn/dIfNnpxw3u235ohVQJexm3JG XywkA X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319090529.1091660-40-ardb+git@google.com> Subject: [PATCH v2 19/19] x86/efi: Free unused tail of the EFI memory map From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: linux-efi@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , "Mike Rapoport (Microsoft)" , Benjamin Herrenschmidt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel After moving the relevant entries to the start of the map, the remainder can be handed back to the page allocator. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 8f2dc477eee0..3b3652c4b90e 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -443,6 +443,10 @@ static int __init efi_free_boot_services(void) =20 efi.memmap.num_valid_entries =3D (new_md - efi.memmap.map) / efi.memmap.d= esc_size; =20 + /* Free the part of the memory map allocation that has become unused */ + free_reserved_area(new_md, efi.memmap.map_end, -1, NULL); + freed +=3D (void *)efi.memmap.map_end - new_md; + if (freed) pr_info("Freeing EFI boot services memory: %ldK\n", freed / SZ_1K); =20 --=20 2.53.0.851.ga537e3e6e9-goog