From nobody Thu Apr 9 16:32:36 2026 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.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 51EFE3EBF38 for ; Fri, 6 Mar 2026 15:57:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812643; cv=none; b=JOMNyvchvqxDIZiIxyoqX/mOU0Dg1nZ74hR3OADzvhFakxqVRbueA1zf4ok/J+0dTjSfosj4R/3GrGM4/znKepKCd6MTGy6uNteS9q45duM8L9buwRKoMN8stpz5h7ErjVIKp1bcVJFk2Fiszd6KLIs72j77nT/QEc4qsEy4bos= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812643; c=relaxed/simple; bh=DAh2QfDt6t1E53RT+FSV0rNEnP55d2Z3JtOi9oMWG+I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OvrHEmNrhhfTmoISX3ebyMKgvYpt+W7UlJAE1Ts0hbOvqhDaGsObbJ7rB9z+/gCluvOEWrg3CMerPCHjKDFWpUWQIjNYWJ5i4L1GZ1Z55/R3d136CAkvBXYfiFqPfYfTTuUPb5xO/u3DC08KSuH8Hx4M7Ns0AE8jFGkmVBoYY9s= 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=l1T+WYGt; arc=none smtp.client-ip=209.85.208.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="l1T+WYGt" Received: by mail-ed1-f74.google.com with SMTP id 4fb4d7f45d1cf-6613513ac46so1812880a12.3 for ; Fri, 06 Mar 2026 07:57:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772812640; x=1773417440; 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=PyUJVB6MFtFQFvl4CFebEtQPHCRh2l9RlIxPiMDk2SI=; b=l1T+WYGt5rPA0dwN4Bt/RythlEgkPP32EeH0Gzw+ySUDmU7EW1QfSfyTwb7t38/Ajs Ao20OY/zeiNrVkU2UxaKbAcO+zxs5BThbC7XAnQgdUZHVUj+G56DceWKc5NyGkLaiwdT nq6ZBxKGlauUo+18YBh2xSI0d2lOWA76cl3cFG3N6xwwW72Bm8ju2TpXVrY0eV4zI38D d2fLr4TBPbYz4WSbSo7nBJqEVXrLqYMtx+vFMlzsu7QWBQhgfs086eUr66TfDtu6mQmM IMJb8EBvbWGkvydUtjAEXrrl5k8CzQo5wW4FlUkoysHe/M4p9riAB6hjFmZNuYGROv6f 8+fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772812640; x=1773417440; 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=PyUJVB6MFtFQFvl4CFebEtQPHCRh2l9RlIxPiMDk2SI=; b=hRt70Yp+in3E578Qw4U50fu6k2M6ySxcFdPb99deRqvVRBEqvr1iV3iLAhEs67LbMK H7vYLIqLhjp9bRzjyqkR19m6gtMCldW5B5v7sUOZmIeBElZPiwwT/gSfjmPg9y2dfVOM 1iLTtA+terH9v/b3H9KPsy7tgnjp0+XIOXN90NGv/hM+tTBUFrVH33ygNyPvGYKREALr enbz92xYsnoCp7Wo/ThjqXfzhZ6eDyTU0mUhGGCSgzXmh21wSxTtIazbdVGZg98cCBio jkSwRZQwSg8M+NM5d+8BGwJHokUzMpQDob/uAJ1hUBoTzMUPm6gzLr/bYDMiHn7BlCX/ ukUQ== X-Gm-Message-State: AOJu0YwflZIHkdPd/BQ9xiZX4hhBqIh7wqdu6VtbeWXnMMg1pudfI/fI TEju7nV64iWuDYETph0s1Pkj5TdWWNmdMK98ntx+dW9dieILMeKfqKyyUx0i0tiYQ+wsxZjoNBW wiNfGWIDTytS+V9SSS2r0MlkRjmElskZVYBzgYLookfn/9ZZtTiiJu0tfAQepMqQVDJlTebqqbw PIwkb3ss5S6AgPBfJF/P1MIvci27c7ejmZlg== X-Received: from edsq5.prod.google.com ([2002:aa7:da85:0:b0:660:cd0d:914e]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6402:3491:b0:65b:ec2d:e615 with SMTP id 4fb4d7f45d1cf-6619d49ccc9mr1313854a12.10.1772812639305; Fri, 06 Mar 2026 07:57:19 -0800 (PST) Date: Fri, 6 Mar 2026 16:57:05 +0100 In-Reply-To: <20260306155703.815272-12-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: <20260306155703.815272-12-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2013; i=ardb@kernel.org; h=from:subject; bh=I4NX+o2ZQ1RbPq3q2GfBRlvmTDgCe/3St3lTTXDwWr8=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXPVzwDZU4G7L/ndDcmf2VCR7uP4onz/ycTO5etsj9Z9Z mfoSt3QUcrCIMbFICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACbyt5OR4Uxc3/Iw7/jPrVuL jTfqTl6z7e6DjZkz2TcmfxNon5XlOJmR4fdUhbnRnFEcfh+Lee11n51lOXKc43IUi7f1lRCRt9e 0uAA= X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260306155703.815272-13-ardb+git@google.com> Subject: [RFC PATCH 1/9] 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. Signed-off-by: Ard Biesheuvel Acked-by: Mike Rapoport (Microsoft) --- 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.473.g4a7958ca14-goog From nobody Thu Apr 9 16:32:36 2026 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.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 14F6E3EBF30 for ; Fri, 6 Mar 2026 15:57:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812643; cv=none; b=WB/7ZatNBYz7U3y77RuEmiXEba+ZFU8E9xuXhWcmsImHZuR6QhuvUp9AyXCKChE7vaMQbrT+SQgQWzZKb2sJWk8BOy7PmDsOnowCnr/v68Sw9uBuQXj+CsUhfwQyLIqxOSs5XxR4sT9olg+651ixdnJQHHxfLlgxVHBxtD1SQFQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812643; c=relaxed/simple; bh=/MRK+SjiyRNaac8LbSy7nBfgg4IyeWFqijEC2lPSsQw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eVC2AlTcgTNco6jrfXYpua0qt/TEuHgZCNfD2aeO9fUVFFKJOs8BAufCdRTBD2DjEXmiP/EvPZi3hW3rlu7Blprx7FN29dXoGy7ncKOqChBF4qLAkOgtRjy/reZUmG4rAubLlC0sBvxZdH2Wjvx4E/4U+orab2QeP6lFNG/98z0= 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=Zw3toSMA; arc=none smtp.client-ip=209.85.218.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="Zw3toSMA" Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-b9410af72c3so290599666b.2 for ; Fri, 06 Mar 2026 07:57:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772812640; x=1773417440; 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=SCYcLEMt7nWCnDNSdFlgn1zDzWox2lvsThVrYioJNy4=; b=Zw3toSMAIEkUnCb153MbvTSnL0AZiNSIXJvYhQyYo07Ep7VoPn0XYvkrYGpxVCmMK5 yjlTGF5V/cHTZa35lS6mpdZsFz3tKthRNAOMMtbYTgpbEWpWUV3Gw28gnpZcgN1ZedfJ JVX8GFfmEEmBRPimDMFWm1vbeqq23nourVkG4w5Oe1uguDK050LrUEBl5vmDa1geUxM+ oOu7dutZe6wA1j7IoqWuwmoRyfQkKb+MODJGsfGmZhuWGUv4LjlbyiJagsuFJcqTk2OG wcEUapun4nJJr3/D59rFpv0d4B61EykWU0JyNgGzgOXdsfO9PniAsN6ofGcwwFvtumJj CXnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772812640; x=1773417440; 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=SCYcLEMt7nWCnDNSdFlgn1zDzWox2lvsThVrYioJNy4=; b=WMGJzGZRVHRJXpHGUfqfTqvOBAHa+txQf30o5//6arleMhEtGjGssr1vFzZxqdLbF3 UzCnYbSjvdKdoelki9OUcQn8AQNx4ax07gFB3nI54jd9Z6s1vewNW1RsWeLZ5IuaB3k1 uGwWx8JLQyHD7Rw5YCJafUcDKU/KK+cDZsZLrtlkOx5TEIlGN/HC/056RwFX+oxjubAx g12wRT5EdBNtInJedr1D1lKrxJ2GGB1/BZ8KKsV/qUgyXyicCIgKPt359WUlHWVx59WY 3K3pjmxA/fHVTY4/GgvzaCg/HGYnwPZ6L8Pu8ZOVx/T/hlUAjpKD4xRUB8Jre3BnQb2x iPow== X-Gm-Message-State: AOJu0YyST+FhN0AkR3jWeq4RZG1CLSx7RpGOCUuDUnR7a2WXca4sHzfh b4zC1tGXmFMx4S+Ujmc+fJVT2eZhyoynKmIS0Kh6HVSrgGpx8Pfs8zGI/swp4Kw54as5KJPWKSk UnSoxRkK+67RYEGaNOodyJFnmKbUqrfnYC4sGcYNI4N+riJ5Dgd8navDmHCkyx/bDPf7ybgO6tS Qd8Juw7pu7eej6lLXEip8lTH2PtaTvuptuMg== X-Received: from edgi13-n2.prod.google.com ([2002:a05:6402:a58d:20b0:660:d4bb:7c68]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:906:f588:b0:b8e:d13f:e20f with SMTP id a640c23a62f3a-b942dfc6843mr136487766b.55.1772812640286; Fri, 06 Mar 2026 07:57:20 -0800 (PST) Date: Fri, 6 Mar 2026 16:57:06 +0100 In-Reply-To: <20260306155703.815272-12-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: <20260306155703.815272-12-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1575; i=ardb@kernel.org; h=from:subject; bh=ygaQ7rljKsyjeL4u3D3xh0peTcsV1i7sL8/+ssx1FjY=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXPVz8CTYQtSWqY7zXJmUC02em513CfHL/DBjbirJt9a3 uZq707pKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABNh+crwvzTrm+RCvpnijLv5 Fp4vltbe0nEp1cJUObTrn2ScXVyUL8M/ExGxxLgZoUc3nj+3/ysHi8+vuZm/5lg0Vd/9lKJ3Qjq DGwA= X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260306155703.815272-14-ardb+git@google.com> Subject: [RFC PATCH 2/9] 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. Signed-off-by: Ard Biesheuvel Acked-by: Mike Rapoport (Microsoft) --- 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.473.g4a7958ca14-goog From nobody Thu Apr 9 16:32:36 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 BA6AD3ECBE3 for ; Fri, 6 Mar 2026 15:57: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=1772812644; cv=none; b=CZ+Tsr0GjEuaSDQyRVAKY8W9JZ741HG9bTymqOUmbT4JScux1m3voLXZooTCD7ByYN4VBRbJ1sF1kt46SMK+j1SaWNdTJuKvFM5en+5mV6YCUYugGkIVVl6+PFazS8krtpW1PEEdYUed0+iW1spT34txnLVnYoA2iCUArL4n0rU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812644; c=relaxed/simple; bh=sKSDOBrHSWS+dLKRjI5r9/gcEPNpg/wY0lNM7xnUL/8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rSd1OMCVRl3yfk1AFv+ERGjOZqDilVoKAxMvrmErCrQCIagI3GzQs6e0Z2/QC43c73gBKShPdRvLAAGjphYO4vgL/C1PahzhUW5rqOYbmS9qBfvC1VbKHGrl6NxQwVdAIq71z7kYs164Tj0emGLXhAV1/FTk7vMl/FFLrVaLevk= 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=r4kBmzXh; 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="r4kBmzXh" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4837bfcfe0dso115511405e9.1 for ; Fri, 06 Mar 2026 07:57:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772812641; x=1773417441; 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=MnaHIt/OTaWwpYMzS8k0B7qGsuGw8MKHCh4rNU5dnyA=; b=r4kBmzXheYFfouHNRG0qJ2GD5Yxqm+WcvB/5r+bxsduKtf1g41GRKYUgN+clOK16iY 9NZsqTG+LqVyNFhTQDF+Vc2XNX/GqV2NXulZl5Yruj/5IfHre6/4K5gH8feMorRh8Iv4 oOyqeCHenqGZj/yxdMDu/Y9ZepTpKYNIPdWocJemtlwPmC1WSonmk5SluyO2BU2lcE8J LpGThIGjLZIEacr7sXOXZ370HTD8K78TosHxFvDguziXYbYBo9JIck6dWnhdDmEgUYBT 9ef6oeq9OGHzT/kvLaAw5IA0Sg3SLLWGxhFgNWYeB/7Dsb0CwjwOVcBChSaxFdqsZ4Im 3cnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772812641; x=1773417441; 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=MnaHIt/OTaWwpYMzS8k0B7qGsuGw8MKHCh4rNU5dnyA=; b=BnnRPkRTY8jX9zTvuAIXvSdxr9/oNetfQM6V2BpBjmimT7Hg0sItSFF3cwdk73w9Vp v/3j/PutgxPI7LcUdqNrsfzLnBozJ80S/8OybI31Lj+YymfJuOE7SBNl1MRo+lLnphzh DX+z3e/7UcqRl3/zSsuhpGzuOYJ4t5hgocbv14tUxtVsM7sArZQgOElZbvYEWiIGzhtD j1/mkAqHHA+pTS5qGF+JB5kaF+SDGORWiRNib4wfkRju8y93JWKfKHQ0HCj6IdgNYLYX HRF1ll9JvZHZTG8EiuEaVvsXPDAX77HhH6zJOlBnVXfoASsR0i/25QrKBtI25cy6LAjW J3/w== X-Gm-Message-State: AOJu0YzfzFwPeTGO9xqiX3AS+/bliBcyYKwV9s2/1LFbW15NS4ykdAv5 EJeCaFEHdOeTEwOW7TBw2WtT1pZgcvthJzP04Bh5vWFimdTWd6UEQOXvNFzRK7NhLTobJRdGE5Q oCvk4Riic+N1u4Z2QU5YIMCzloZGzmUknnP9TPYw+STL7Ur5gf633cVsEcGf+JhCv/sqsyfc3Xn xQ3UJHdwlZS6DeZPUDT6EXTTafI7xfogNd3Q== X-Received: from wmkg17.prod.google.com ([2002:a7b:c4d1:0:b0:483:29ff:3c47]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3b8d:b0:47e:e946:3a72 with SMTP id 5b1f17b1804b1-485269692e2mr45231865e9.27.1772812641095; Fri, 06 Mar 2026 07:57:21 -0800 (PST) Date: Fri, 6 Mar 2026 16:57:07 +0100 In-Reply-To: <20260306155703.815272-12-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: <20260306155703.815272-12-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=nBMjymnVgVsODc2LWGkG56q+yyVl6fsi1H4MendJiyU=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXPVz8ANsvuXZv7tEJZ32G7yUzQq+cbHZ2ua22qjLoboS 83fWy3XUcrCIMbFICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACbS3sLwP5/7+6Ybq/+/9kiN vdfkfVZFRn15/gPexgknLhov8mewLGFk+FYU773fLv+owGtzlvbZh3ZmLi6ormLqlTzOViV8KnA bKwA= X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260306155703.815272-15-ardb+git@google.com> Subject: [RFC PATCH 3/9] 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 35caa5746115..f896930cecda 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.473.g4a7958ca14-goog From nobody Thu Apr 9 16:32:36 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 AF5613ED12D for ; Fri, 6 Mar 2026 15:57: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=1772812646; cv=none; b=DW5yR5r+YJGNhuPkoIyIRlzxmHeon8DLNMLvvPtXOj+IYTH3HHk9A+1qZ2R5mlmTCbzyfw2zT65jCQw+j70ZlMpQ4ZM5psSEwZIv/PtlMUb/+pmIRSjfuMm3o00ix/nGqoHVNKHQJxR09zhVgzbjHUp+BZw3Klo41iVB5TR+mTE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812646; c=relaxed/simple; bh=vTYAQNY3Wjeqjeo20q+YJOZUG2bhyt1rtftQ4oTgN3Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TTXrms6tPraUHece5o3HEF4E8Zi2Kxe29o88OqFiOkwRpckIEzYgC0RIOnDidFSi4ooX7+JeAL5gnayA45k2Ru+YMcOExIdTyy9whu62X0NW8SVLoLAzbrGOeeNpbe9RZBQVmLofFT9n+sP0bd6WSqP+tKYSjCpB/nMIlQrAG8g= 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=gRbIXEoy; 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="gRbIXEoy" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4836bf1a920so100858825e9.3 for ; Fri, 06 Mar 2026 07:57:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772812643; x=1773417443; 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=NjiQWvjHyJpWfWYstxbrMEc+P5m9/w7Dl080OfHlOyk=; b=gRbIXEoy84Bt1G0Zz0/DFp4oHtmlb60j11I4nM1NiVYrkZLkoVg/piS8yhazyVFoxW dUMu2BY1O7aZJ/V+SEtwNuw6J3VpzueDVJpGgdEp9X/cFPcr8gsXNbgzbZtVaP2Pu3xN yLWV6BFN+W9UY8bg+MXfSdck6wAafdo25ujcGjF6PilmfuYTMlETMPgX8S2W2gT3TyYl 7au1xd4VWbjkjG9dFbw0Lg39R1kB5prPAt++zt1uDy8QrtG7lSHrQP5MTJoTJgqFdy// dLkWXnjhe8G8O5QjkYZ8eZScfAvH0nQtMdx3gyOg1aqVSrd5x5IVCEGz09O85zN0H9+1 BWlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772812643; x=1773417443; 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=NjiQWvjHyJpWfWYstxbrMEc+P5m9/w7Dl080OfHlOyk=; b=fHKftfEKAzYMEotaoq9Olvx5PB9ZpspZkLjAuQW/dDJnrZdPhfqvRkIHs2j+eOodoG RGzuqrNUCm07pr44q4xOxUDei/YnKK1dQAbjtTpnvJmFX1iGe3FSr2EsmmC7p6fMKH5r ivVnDRGrLG2gBlKDy+0e96/1kdXdlGY0FaoRkeSNCNz+r/pcBsz3cy+hPjMrWAYXtRqf /52lKkPS+7fTFnuBWWM1o6Dt5gGBh7Ea2kyf9aoYe+WTELQ5uTwuPgkEFitjZTgqB6EM sqhBPu8dd8KAXY+bv8hY/8lGbjnJ9V2YUUdLeQttSHQ59WdM9XnuW/SIvOHIKohXTM/e pu5g== X-Gm-Message-State: AOJu0Yz8p7uxZqSTXFuFGaUJg5vjsAr5zZqPlqa18GYZ475psguNVt4j l925Qhh8vt1od2CWFWXjdfDPbTZY++qXBbXNIWzcyJme2XuqXT7L8mWnlRIeL8LeEz0AItUiWwj tcFYo27KIfCT+o3gs7T4Q7CyJLV6mP76oXal10hghqp/jpczlS46Z4L2SbPtuqwLCpV2R+kf0jd IBnEQIepqaUfmkTBF72bIpII93EdmXAIOfWw== X-Received: from wmbjw13.prod.google.com ([2002:a05:600c:574d:b0:483:a1ee:5eca]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:46d1:b0:483:6de6:37ad with SMTP id 5b1f17b1804b1-48526979684mr44974755e9.34.1772812643044; Fri, 06 Mar 2026 07:57:23 -0800 (PST) Date: Fri, 6 Mar 2026 16:57:09 +0100 In-Reply-To: <20260306155703.815272-12-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: <20260306155703.815272-12-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=7836; i=ardb@kernel.org; h=from:subject; bh=lQHAoieqpClT33fFpHsqU6z28LZv8gyQTPoLL65HPCg=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXPVzyCnjv6fdztblIPX2TjtdfhyR3Huc70LN66HFBsox zadkf7bUcrCIMbFICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACby7wDD/0LZinPdbTf1/4jk OO8J/dW59ET71u8LGTaf3eOQZm3usZ+R4aL0lS87rnIebVb7vNpTaHal2dWJQnsO7tdQVtH6UzQ niQcA X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260306155703.815272-17-ardb+git@google.com> Subject: [PATCH 4/4] x86/efi: Omit kernel reservations of boot services memory from memmap 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 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. 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 | 54 -------- 3 files changed, 196 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 af766694f7ee..8d2bfbd3a0ce 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); } --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 9 16:32:36 2026 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.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 EB3393ECBFF for ; Fri, 6 Mar 2026 15:57:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812645; cv=none; b=Uuul4zIDbEwatjwkoiUEVvGDkzrxswYGeRgaRHNQBLIXzEGBJCMG3N2V3ogTYYw4ZUvDBtpnCUN3RiQu4ouI9+s0jHJ1IYNwwgos3ffMeMswnq7KMIo6C5SFMV3TQ+Sv6t/Ytoxd4Sp7LAARpnbzs3eibgsOO0T+b8fd0FYvA38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812645; c=relaxed/simple; bh=ZTnS4RveV5BcCOB3XTR/KA1YUAH3LEITzjMxI69JZuQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VIFAAJqFiGwZUYFPHUXP2WlNhl7qUUzPPH1krvo/nGjHchMg/iM9W8UT8nUzuZ7Eke41iM5bPxg27tLeJ0/h/4C0ZaGZSRyPY/KPUtcUY0zjX+ai2Ym14Em9A/4qt9BkNcMEH+LULDtj+QVjD6aRLMpnbwlAhWKB2e/PQHnjaMQ= 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=abpJgkS2; arc=none smtp.client-ip=209.85.208.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="abpJgkS2" Received: by mail-ed1-f74.google.com with SMTP id 4fb4d7f45d1cf-660af6dee06so5029046a12.3 for ; Fri, 06 Mar 2026 07:57:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772812642; x=1773417442; 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=pemDVNzQpQnPK8E+QcSXNZ28feWHqXpZeP4jd6MxjXo=; b=abpJgkS2I83f6noK0Kkn6TYYYZojjrnXymvZ4g0qS2uqK7wuEWI/jJXDZ2OvKtq35B jJujoR3UgsPiaAigf8QoFarbg2t5GDmxMBkmHD5zmZDTg/3CYDXvLFQv8Jdg4GS5OMti 2qgP5LaO3HEjQALIgYyAy8vBZcdzAV4+KDA2pgIa29mq7JtWRIsw+aGovRq5fvTT75SZ tOuZXFJdUKHayd78uGHlz/P2fdRNjCfLH40UkZxjoQGGYT/gTTzgM9yLpiTjyOnjY9VS 6KJ0IGQTcqjO0fa+qJsc5VRWOTKYwm+R5/oSNxAv9A8MgZ8QxR012ty7PuW8tTgFVCnZ NL9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772812642; x=1773417442; 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=pemDVNzQpQnPK8E+QcSXNZ28feWHqXpZeP4jd6MxjXo=; b=xMm34W9iwfartFD27Q/fCgntMMSsggsOxUx32HiMvlUM428aE0Z/Zmk4E7dEkwsOTj miCQcY+e3Hoppv7dMvCGZ2VYBcJPO13HjelogI2GGk8mlOKQEM7EQtbH0nvzkqbvgLSb PetEZlPBhPhosl6KU8vdEUnsRTatgkNSTuEXaxwYBv7jk2VounDAukmccJEUKUGuF1Wp Ygct5MCxXaK85bzZoLy12XtpdobhWGhww2KJYPaM6im1/TNfuKyi9o7zwfgJMQE3jj+a wpxPVKlPNA0uNie6KHNqLsvy0fHdR20Nplh4+Hthr/UoI9pFmwnQVy8p7zhuNxWweYGj YVMw== X-Gm-Message-State: AOJu0YwiB27+WuWkd07vkXTwlNSJPDYUDBROf8RWehPmqPKY5VXV7gB4 h88IKzEDQluySfiOTV5k1kAqrCwKZ1PpSXofU4BS2bIvQ9zORkAUXPaCReVOifIjy6N9Neu/aaL 1oo6n+9i9KA8Iizcfk7eUcH7IFjGoDvwB65D7hiFvyRw5m11CCRaJ63BtIxv8WAVn5feXOSIcVO 4v7bmDeNbxckH37GhzMQVW4Iwaivn3yiEQRQ== X-Received: from edcy15.prod.google.com ([2002:a05:6402:358f:b0:658:115a:7265]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6402:520b:b0:65f:aecc:4f0e with SMTP id 4fb4d7f45d1cf-6619d5274c9mr1386700a12.19.1772812641980; Fri, 06 Mar 2026 07:57:21 -0800 (PST) Date: Fri, 6 Mar 2026 16:57:08 +0100 In-Reply-To: <20260306155703.815272-12-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: <20260306155703.815272-12-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1664; i=ardb@kernel.org; h=from:subject; bh=EeSnSz6nXHhpEi61P5BRppL/JAG5B1Jjj0fvpIrf2nk=; b=kA0DAAoWMG4JVi59LVwByyZiAGmq+VKjBinn9SsHx0HzQYNL4p/cP/eaDyxrHBrlmpnpshxTM Ih1BAAWCgAdFiEEEJv97rnLkRp9Q5odMG4JVi59LVwFAmmq+VIACgkQMG4JVi59LVw8kAD/Q3j+ rAfLCupT/TsV8nAZg4bilFrBYtQPH2fEwGiACM4A/1r6ZLGhfH7UgYqkLjkVkRpq10pUntP74mz TKx7qHWAK X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260306155703.815272-16-ardb+git@google.com> Subject: [RFC PATCH 4/9] 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 | 21 ++++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index f896930cecda..58d00ffb1d59 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,14 @@ 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); + + freed +=3D efi_free_unreserved_subregions(start, range->end); range++; } kfree(ranges_to_free); --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 9 16:32:36 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 36E2C3ED5AF for ; Fri, 6 Mar 2026 15:57:26 +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=1772812647; cv=none; b=pnb5LhbXZIybwy0sulC2TKpoFQYPtOXf9eZA6xAmIAdMqhjpKvVKfbKGR9Q7CV9kEzBBDoaGR8n7YnV4ZXZnGJjJhxZO7drgRH1FKXTeG8VyC1X9vPzK+zvppE8xjQ+GvmU3lLs41/1gpbO49lR7jBamSCLENjfLNu2xSLCwa9U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812647; c=relaxed/simple; bh=1307fs8XamZL7Wt4P7DAyoEiTy4wLKbb450ovEmio6c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JWs+ZyA5BWQ9FM6MgjyhR3Mr2kg6GUj60ktAg4gDpLPtcQXmxQyHYb5T9WV/OuAnD/vlxV63JvDAtT+KAEn3LeUcPhxhjLTVWLq6qjNGQeyU37OLX3APKlkD//FMUbesqsu9o4nA5rXRBdgNBkQTohvgNArdrsIV4v+GCZ31UYE= 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=1DUzqCeS; 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="1DUzqCeS" Received: by mail-ed1-f73.google.com with SMTP id 4fb4d7f45d1cf-661420734b7so3206699a12.1 for ; Fri, 06 Mar 2026 07:57:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772812645; x=1773417445; 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=ITFaBcrhAJjnw7yibj9RtbA12e7zZ2KLF2hSYHPAq2I=; b=1DUzqCeS8B/IjLVaizt1/eohVHy/KXXvtVMaeUf7dothdA7+IFrm8yQ691KUzFbCWX HitZVc3hssCJPbISnfM9Uxaps7yb4+mBjH5RQcBKZa2m3N6gsfFR6pGuRg6JWAsVBLdq ABvZIvORNFamUMLmQj2ayhqf0IX+8GGSdXs4Qd9mCiLSkhJD0vEIsaMrTV4pefb5eTTQ ymA43iwCzlejfspU0S8F7FuwhcOmulvbNeDOPkPi5lwhlpF2IIobUxSRKm91Xdi69hEP rb8w4PHs+PJvyC8BaMxOwSVXaHLLyBQ7rBH+KmbaUEwM3HYUlSCUUMF/QGU+uwN9vv7t T2AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772812645; x=1773417445; 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=ITFaBcrhAJjnw7yibj9RtbA12e7zZ2KLF2hSYHPAq2I=; b=QQhRhjX7WpPoCEV16vdHFZMwRewYtqL7LQdmYambqirzKTIA91ujGxprPouC17wPuF V/PAaAnfG7uhEJ13/ASDywD6Wc6GDgP472pZc2He/OJJWQ5oApYaDXmyUN2OI0A/P2Fa 66DusViImsdgH8MVSrB5yJZgdwuUTdtAHO9/M1xG0gFJqkKPmSJ5yzeUf6swnBFnjx4P XTlCDWUeP5juhrJk29qLlpJioLu+tFhxCtu6W3PUqHhMj3ktP8vxYelrNiqa5aLnXkEo UgmUS/xErI2TpMKewDmuQw2R279W8SD1CrZaRKpRMRUP7DsMMwSLdKcTOlIOEfwJP6Zp 7cUg== X-Gm-Message-State: AOJu0YyhDKKAvDSd+iXSNN5QxuCDGTZv82EiojEdeFChH4opdocIcBEC oQYyTxdvZdZRA29432xE4MzQ1BvnV2yztd/wHQSr0yyBJWnGNxfQ/1+XhHCEwZUVmSqlG7f8hn1 vB9T1IqcjOhofLiVnCxILt2bedpR8cA2f9NrTMu3ntnNi+faE+njpK1gk0dbY+wp8/p3m4GvyW5 AY6D93sSzBiKM1CiAIdKovRtQg1cLpKGUE3w== X-Received: from edqi7.prod.google.com ([2002:aa7:c707:0:b0:660:a86d:ee96]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6402:1d48:b0:65f:9c21:e68e with SMTP id 4fb4d7f45d1cf-6619d45a9f3mr1570773a12.5.1772812644211; Fri, 06 Mar 2026 07:57:24 -0800 (PST) Date: Fri, 6 Mar 2026 16:57:10 +0100 In-Reply-To: <20260306155703.815272-12-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: <20260306155703.815272-12-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1533; i=ardb@kernel.org; h=from:subject; bh=St8MkQamdXukXyHB8ojHbIf0pOg2QO7eOCnOt3Cl8f4=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXPVz+CVD+fP4+i6r3Wi7v251sMnTjQ8KnHqamUQ8N79e P7/xhV+HaUsDGJcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAiEz4wMqwUZH/2NTM5e0PP r/ouez3NlUsSd3RdTNy85FtVg3hn+F2Gf/o9l6dODjv/VvipXfPWxfEW+gz/RNc9ZDTJlt/U9/u OJS8A X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260306155703.815272-18-ardb+git@google.com> Subject: [RFC PATCH 5/9] 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 the 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 58d00ffb1d59..e72e8b23598e 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.473.g4a7958ca14-goog From nobody Thu Apr 9 16:32:36 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 4412A3EDAC4 for ; Fri, 6 Mar 2026 15:57:27 +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=1772812649; cv=none; b=nZN2wRFIF4F4Baj7QYdeghbPv9KWgvkxrkNPw5Rl5pWZfUcpvUwETC9oipqKQSLG/0N2C1tlo5ajPi2lFhctMXcTzxx+vvEAjhkvlCqODNtPhV3snnttMGaFj6BMaUqQ2YoVup9laOI0r41Yw5FaMeIZgw7j/tPz5Pc0j/E4KBg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812649; c=relaxed/simple; bh=uLd9V1oCpPRWtIo1XizCv09BniqJ8HX1YepTkcjMXh4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=e/gqYA/ckPcoeEbMIIjC5jmZQ6JZ7vMkrgA5K+ppRFnIaelIhrGYIVnijs7s/+S9fZ7SQv0jfFYoEsJT9KK1+vOCQEaK0IRDBDR7ArYL4yKjQFBhzavOkQ0Uwxxe5rJDOeGUP7heexW6W9DbUFceS6G7RcuaN7D1b12IxeNuNhk= 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=DP6UKx7M; 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="DP6UKx7M" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-439b3011be7so3780958f8f.1 for ; Fri, 06 Mar 2026 07:57:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772812646; x=1773417446; 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=1VJwwrxymdAZuyNsFopMLfzW51d6bGAIMUJLL86H4+U=; b=DP6UKx7MwA//RudFySkPKdjiyGguCG48uZvZfLhT+P6onvmPv1h2qjvfmLYLbScN+y NzqvG+OrYG0A2lKUOL8leyWD9eu3Sqd+ID1uskG/P+A5csNL3OJceiUw6QcKPLUOap+b pyPEJrAT9wREu2ZsrkCGaDkJZGOrNLeHgZMbsV/6r4uyjGuKdgoUf01Vnt/MzpPN6DD/ iNbm8rmK5UETckPsE7EB2GnF23/1VKBBUZQY+ezPmwqrwve2xuI9wgYeZQA5nAbxwCvx A2Worhk23YfAc7MZczhs9eSbZUIRs9FkQJ/UdTclh6bElHnIS1G40nKzZmmMLhnOfEho lW3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772812646; x=1773417446; 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=1VJwwrxymdAZuyNsFopMLfzW51d6bGAIMUJLL86H4+U=; b=fmu/VOrgxakdZhgcQVupgR6qAz5thvHqlR4CEUx+ZtIB07kQZongVg9mEfoo4wpitQ JTwntt4zoTca6TLlSSJB6XHgcMpfQmCQT6+W+9ChdUl6CAh+um6YT4Nyy5Sa08patJJm NaFOt3wVhCixkhKwBMFJqM+iGFi5WSRRatfqMhixq1u6bYfrSs652yxncmXUfYXIvXyD 6yKHkHenFPRDY7TBUnew1JXkg8QFwKJvtMQJQ9pziez+wrKy2/ah5psxNvT98Z7QQJYU NUPlcsGrwzGYvMc3Wzr9H77EPzVjoA6d8/U8/ntHr81FJEd4i2m5018abV5wfosr1hZS Rj7g== X-Gm-Message-State: AOJu0Ywvjpx6G/h5mtT1EoZC1buGNTY0O092TsE8QzjjdH43SpuFwKix nFVCVxoknuKx/yMyvQxL0zD1XS9lUCsX6MNOkdvXctHvTh8h2SGyqgAjJXi8aO82BkeYDnnf8RJ 5ASdTPbfTPhzG7kuORhqdYB0VQp8GhZvE5uSnqXhUt/vWKAc/rIALSfdeGGkidbDcaknq0dqCe6 1lHQZnyWy96eLvd30lZlYaPtustTlYeB+88A== X-Received: from wrbcm14.prod.google.com ([2002:a5d:5f4e:0:b0:439:b8a5:41be]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:420b:b0:439:a958:434b with SMTP id ffacd0b85a97d-439da31dbf6mr4340230f8f.2.1772812645563; Fri, 06 Mar 2026 07:57:25 -0800 (PST) Date: Fri, 6 Mar 2026 16:57:11 +0100 In-Reply-To: <20260306155703.815272-12-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: <20260306155703.815272-12-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=8782; i=ardb@kernel.org; h=from:subject; bh=jGYP0/YOGkv3IJMXLQNb+76CSu4ibyPhkWVRrJnk7UU=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXPVz+BDxzlYs5S/eFz1rVL1W9D7QVpxYfvfxUzOQqbf7 yyZNF2qo5SFQYyLQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAExkfSwjw9HXytFe0jkcxXMV vC6xdxa9v66VtndDbYONM5vJ3PLXuYwMb+fqZp08vKjeyKj5YFzMVMZDS3nu3V1axyr85ZNLk/l WfgA= X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260306155703.815272-19-ardb+git@google.com> Subject: [RFC PATCH 6/9] 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 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, this requires the E820 map to be consulted when deciding whether or not an entry with the EFI_MEMORY_RUNTIME 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 | 60 +-------- 3 files changed, 5 insertions(+), 197 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 e72e8b23598e..8a4a0c6b64bc 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); } @@ -509,8 +455,12 @@ 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) && + !e820__mapped_any(md->phys_addr, + md->phys_addr + md->num_pages * EFI_PAGE_SIZE, + E820_TYPE_RESERVED)) { continue; + } =20 memcpy(new_md, md, efi.memmap.desc_size); new_md +=3D efi.memmap.desc_size; --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 9 16:32:36 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 3B0DA3EBF39 for ; Fri, 6 Mar 2026 15:57:28 +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=1772812650; cv=none; b=gPYifSz8MtDN1QfC0RjYnFLT0Ub36gAKgbYxxcafbTYuVYVH0WP6cs8d3GVhDWLCg+X5a18aemadC1/Vzanado0RCQwei/WtydRxcSYVA1yrlrOjei4Z5A4z6HBPubBYZNXQjBtTZ+grod+Gl713vpyre+Ews5V5Tuv6HQG+WmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812650; c=relaxed/simple; bh=AcDvV7hr6f9ZvnKPw0LOtcj7T7Cb7fyegl+5ztfQwus=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PrIFvxLSHNByOC0biJ/e8W9jDRU3MA9DBQ1+0JrCbIzqLR8ZOIby47bOGEaPTW9/o6tHH28UZA4y0ikHVv+ptGKTa5E9Qz0qw4UM0Q4kR0tjM4AJrZCQAzx74UZAj37dvypRen2R8fxsc17WkFJ+Sko/K1JvumYxS485cvAYNDk= 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=FQW5IGVQ; 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="FQW5IGVQ" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4837b7903f3so124911605e9.2 for ; Fri, 06 Mar 2026 07:57:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772812646; x=1773417446; 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=uUL+2jwG2aYHm6Demo1pooALvVPYq2HBm66gmXpaBJo=; b=FQW5IGVQf2ZFh4X01lzvzWfVEzp+LdaXq+cYhSncdw1YWLHQWl97i4rgozfuIh4YYv Z3vKsRY/7Ky2/Ex0YE1QNkaSKY3KjPa2QSvvtq80G8+wNtcCtfohmBRaqddhnz5hrZsW ADN5bUXPhUJyJxLPTfyEHTI5seFQ9Hnkps115oWbo1dMLRQPWkPL1faiYf6MoakAv9kN 0wGia21ON+HcbvwNF4ekYD8rHdPiukgNZcSEn2dqnSjJYfuay7i04bNx3VxqAR3v1JFT c2n8LNoHQxlv5cJ8BhTCjz5EPXdQdA5PNCCWPYBe8yw2lcTZVw4jZyrzCsY+bJHQvyd4 GLRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772812646; x=1773417446; 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=uUL+2jwG2aYHm6Demo1pooALvVPYq2HBm66gmXpaBJo=; b=WfQQBKrgQ3QZv0b7xvO9uUYgembeuerRcrSfyL8iwSIRJyXbwhjcD4CnQBWIqps8TZ ggSVDvxwGco+eY15Dy42wbelv7WP6fYEU76sZHWspBjfgQI85uL7O4tOGi9lPjFcxloL zuyn4KUYHg8hfuUlPPJmz71/5x96Mrw04eE/8pBTdOzyhXKBdcvlwrg07Erur/DYnLpI FYFxiHuQe82cYw4zSJv8Jft4Il+2NX0016CyqTE1cghxOdMMk0sytCp5Nxlr9QBDpaNA mw+VicOUH8RZZwsYbbxM2n9GmAoUmCw0zbFjhzNA23L4MoefCxpqiIXgJIbKUTN2ySUK HHQg== X-Gm-Message-State: AOJu0YxVnzYgkBTMop6h/nBeBr3o2MjQSLZvmaSTaWQcH+JGbrRD4gAR ftcywsGOIp8SJcZOpJW0NIkZ8ND6+SD2WXiKS2N80qGAyN5+n72eMY9fuSGOBSHnRhE/kkwCgUz vk+FJzQhpj9TgZ1hFxZSWJ+tPdcjphz0YDNPq9cO1MkYqcTpsuOloDpgdsceQdu+6xhM1NJauK9 VDYEsj9pzkWx3K5WQaV+Bak1KD76+DFwm2GA== X-Received: from wmqn19.prod.google.com ([2002:a05:600c:4f93:b0:483:a1ee:5eb8]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:6386:b0:483:afbb:a064 with SMTP id 5b1f17b1804b1-48526918f7fmr47374065e9.1.1772812646554; Fri, 06 Mar 2026 07:57:26 -0800 (PST) Date: Fri, 6 Mar 2026 16:57:12 +0100 In-Reply-To: <20260306155703.815272-12-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: <20260306155703.815272-12-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=7331; i=ardb@kernel.org; h=from:subject; bh=QVcwFSJX1AHsu94pZJ2sqfnsDGPzuZFlAdmjpouGCM8=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXPVz5DtH4KtmL+Fv049n8IrOol/LaPi4kWK13kXHbqUu u5l2JGUjlIWBjEuBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCR1lCG//Xrp1YwPzPlEF+w bhPjwt3X3+6/IOQ0J+FTZFep6et3Tj8ZGT4c0r0veCXzoYicc2ntltM/z8jm9gUk5fK+SbOtMVx YywAA X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260306155703.815272-20-ardb+git@google.com> Subject: [RFC PATCH 7/9] 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, and it is much simpler to just move the entries that need to be preserved to the beginning of the map, and to 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 | 3 - arch/x86/platform/efi/memmap.c | 83 +------------------- arch/x86/platform/efi/quirks.c | 30 +++---- include/linux/efi.h | 2 - 4 files changed, 10 insertions(+), 108 deletions(-) diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index b01dd639bf62..ec352a8f6e7a 100644 --- a/arch/x86/include/asm/efi.h +++ b/arch/x86/include/asm/efi.h @@ -392,9 +392,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); =20 extern enum efi_secureboot_mode __x86_ima_efi_boot_mode(void); diff --git a/arch/x86/platform/efi/memmap.c b/arch/x86/platform/efi/memmap.c index 8ef45014c7e7..951a90235abb 100644 --- a/arch/x86/platform/efi/memmap.c +++ b/arch/x86/platform/efi/memmap.c @@ -8,78 +8,7 @@ #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; -} =20 /** * efi_memmap_install - Install a new EFI memory map in efi.memmap @@ -93,20 +22,10 @@ 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 flags =3D efi.memmap.flags; - u64 phys =3D efi.memmap.phys_map; - int ret; - efi_memmap_unmap(); =20 if (efi_enabled(EFI_PARAVIRT)) return 0; =20 - ret =3D __efi_memmap_init(data); - if (ret) - return ret; - - __efi_memmap_free(phys, size, flags); - return 0; + return __efi_memmap_init(data); } diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 8a4a0c6b64bc..5bf97376c1a0 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -359,12 +359,15 @@ static struct efi_freeable_range *ranges_to_free; =20 void __init efi_unmap_boot_services(void) { - struct efi_memory_map_data data =3D { 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, + }; 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)) @@ -377,6 +380,7 @@ 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; @@ -384,7 +388,6 @@ void __init efi_unmap_boot_services(void) =20 if (md->type !=3D EFI_BOOT_SERVICES_CODE && md->type !=3D EFI_BOOT_SERVICES_DATA) { - num_entries++; continue; } =20 @@ -397,7 +400,6 @@ void __init efi_unmap_boot_services(void) =20 /* Do not free, someone else owns it: */ if (md->attribute & EFI_MEMORY_RUNTIME) { - num_entries++; continue; } =20 @@ -432,26 +434,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 || @@ -466,7 +454,7 @@ void __init efi_unmap_boot_services(void) new_md +=3D efi.memmap.desc_size; } =20 - memunmap(new); + data.size =3D new_md - efi.memmap.map; =20 if (efi_memmap_install(&data) !=3D 0) { pr_err("Could not install new EFI memmap\n"); diff --git a/include/linux/efi.h b/include/linux/efi.h index 664898d09ff5..dbf5971dd1c5 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -572,8 +572,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.473.g4a7958ca14-goog From nobody Thu Apr 9 16:32:36 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 EF5C13EF0B6 for ; Fri, 6 Mar 2026 15:57:28 +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=1772812650; cv=none; b=NuCe8tL+H+n81XvcWxaMr/jor0CXMf1AOcIZkNufmNiPdVyCPsxhoTPyQb7m0QcrwspFYtoIYbZnBMwyU/1ZwZg2vSUIucEz3QaVLVhU7kwhffysajFY57q373i7o+uLyU4r6UyGt1e36NEzPEv54bzP5UcSC62yxjKmEkT1ew0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812650; c=relaxed/simple; bh=5zd9SfMdyJBxjHTVPHqLum9qpDstlixyq0HqoJ8o+18=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uRmYLuz2MBF0xd2atUQhqY93ihNEhNi1uHDb4HbIaRmionmDVJTgJd2RiRxYdVpE3n3vr7AIhbCtUcIG1P1X37OgR7SRbemCoGZEj6Gy3ylCKgDzzftl+rW3hot1W5rMJx5pR+hk/3S5/rOu64Xo4/BHuwMKGLj5fhbLJ8rkV9g= 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=hXc1BGKG; 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="hXc1BGKG" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4836bf1a920so100859655e9.3 for ; Fri, 06 Mar 2026 07:57:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772812647; x=1773417447; 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=rmprDLEjUjr/uUOMNaPL1M9LRCd51D3rl5+x6D9Z3Tc=; b=hXc1BGKGk3kg30wOKdG6sGnzeP6AxG6uFxinPrNC15DNbRcu7QBTclzsiM9za00Zxm YFZzwI9vZK/A7SzFIyDG7ipB/tqNGt5+/ayfTH+TVPpl9u1YiDyqNfXzbyixpfwbkngw 9jl8mvnLy/faVfXNZFf5wl/CU93TzjC+fV0lAJW/ZTRvvaTgKMqPKZAelFo5SWImIf4h 3Ao/2wpo3LzRp3jLP76hANEoIiWlDi1Sg5tHPBeaxrVyDnQzwUzuz8/oaOkuseqn+11V TYkqUCcir0tFqfT+qJnK33J3jFXCwlncHKe6gc/BOXSau6dbIbvD55nNWRLugx64xnKf fqeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772812647; x=1773417447; 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=rmprDLEjUjr/uUOMNaPL1M9LRCd51D3rl5+x6D9Z3Tc=; b=JpIefHKI+jKaOVOPgJtwf019xy4wy1DLqJgsrABWKd/mroSXYZJroLCyfJWNKZqKd/ ABLqrRLuQ63IFvq8Zw5fHM4nfLDlHyrTxmX5I7lYsIJUTHfHsxkUrU36LKJ8a4IMq+9B S1eXOAVNzhonyC/xuPIltCHOole5+65npo3VXYwurTnaYbUcNx27KVD3XqZwNXsREK4g 9SsaU7phc4lMMvYWMxe6aO00EFeXnNCmnFFxyvgmcf0/aHdUe/yMxObOSYR3OXiUteN6 +q2I4NZtIpRuCT1TG2CAYpAjNMVXP0uUml3qJc60iLSK5JY54MIEt+ymK818h9dWhu6Y 9hQQ== X-Gm-Message-State: AOJu0YzB/ogVx9kMJtzZCQvRAEXptMMwJRg2THmxRuhuC66qV5wSCqLt 1qGN/8wo6hextA80OXfLbgg9lDjYtTpA6HKZZQ47TLxY4f2mZY7KW5oDUNTxzE8y3jXkEuH1uy5 8IkrFof6bHCquGSJAHONA6Cy3jgXvC8+jYGvVFydvbQKIv2F9YLSYCbI5PXpQz/5MkIlmxtFxLs PXo4gjtGv559hyV+W3VAwLZzVvlfDuQWFl2Q== X-Received: from wmjy24.prod.google.com ([2002:a7b:cd98:0:b0:485:2ab7:1775]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:a0d:b0:485:1878:7b8c with SMTP id 5b1f17b1804b1-4852695b6fcmr41463395e9.18.1772812647341; Fri, 06 Mar 2026 07:57:27 -0800 (PST) Date: Fri, 6 Mar 2026 16:57:13 +0100 In-Reply-To: <20260306155703.815272-12-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: <20260306155703.815272-12-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=6680; i=ardb@kernel.org; h=from:subject; bh=hw+BPkyMQz0wZCk+FdNvJtlzl9MRgrvosyieBfbdoWE=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXPVz5AK78klujLeSpYr56mwCLXJ25x16v/zJv7DZZ9vJ Qbmz/d2lLIwiHExyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgIlwVzEy9M5Ouli1S+Zvi19s yIccya9X5r745Px09bad9z0v8Hg5zWP4K6LQvOKg4knf4z9Lb5yOW6NelXjz6RyHc2EsZ7b1vQu vZQAA X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260306155703.815272-21-ardb+git@google.com> Subject: [RFC PATCH 8/9] 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 | 130 +++++++------------- 1 file changed, 46 insertions(+), 84 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 5bf97376c1a0..d7a64b404bea 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -350,37 +350,10 @@ 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) { - 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, - }; 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.nr_map + 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; @@ -394,15 +367,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); =20 - /* Do not free, someone else owns it: */ - if (md->attribute & EFI_MEMORY_RUNTIME) { - continue; - } - /* * Nasty quirk: if all sub-1MB memory is used for boot * services, we can get here without having allocated the @@ -416,49 +384,14 @@ void __init efi_unmap_boot_services(void) * this happened, but Linux should still try to boot rather * panicking early.) */ - rm_size =3D real_mode_size_needed(); + rm_size =3D PAGE_ALIGN(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 - * 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++; - } =20 - /* - * 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) && - !e820__mapped_any(md->phys_addr, - md->phys_addr + md->num_pages * EFI_PAGE_SIZE, - E820_TYPE_RESERVED)) { - continue; + /* Remove the allocated space from the descriptor */ + md->phys_addr +=3D rm_size; + md->num_pages -=3D rm_size / EFI_PAGE_SIZE; } - - memcpy(new_md, md, efi.memmap.desc_size); - new_md +=3D efi.memmap.desc_size; - } - - data.size =3D new_md - efi.memmap.map; - - if (efi_memmap_install(&data) !=3D 0) { - pr_err("Could not install new EFI memmap\n"); - return; } } =20 @@ -498,24 +431,53 @@ 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; + 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, + }; unsigned long freed =3D 0; + efi_memory_desc_t *md; + void *new_md; =20 - if (!ranges_to_free) + /* Keep all regions for /sys/kernel/debug/efi */ + if (efi_enabled(EFI_DBG)) return 0; =20 - while (range->start) { - /* - * 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); + new_md =3D efi.memmap.map; + for_each_efi_memory_desc(md) { + 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 true; + + 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 =3D=3D md_end - md_start) + preserve_entry =3D false; + + freed +=3D f; + } =20 - freed +=3D efi_free_unreserved_subregions(start, range->end); - range++; + 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); + + data.size =3D new_md - efi.memmap.map; + + if (efi_memmap_install(&data) !=3D 0) + pr_err("Could not install new EFI memmap\n"); =20 if (freed) pr_info("Freeing EFI boot services memory: %ldK\n", freed / SZ_1K); --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 9 16:32:36 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 54A2037AA94 for ; Fri, 6 Mar 2026 15:57:30 +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=1772812651; cv=none; b=j5l1sK3pZnO5nSTuqIcZ6CniRIUyH4P/kEX//B9hbTVHQ0hPcSpY3RyolQseCSdixLg9/ykkpAvEe1hyk2qtvO5NDX/jVaDOoVmc5vLjGVCKv0Zd1cMzoqRtsgPFzDdpjmVQw7vv8KX51TbvBMXs1fXdjLFi3eWTV9Qtj/E9OuU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772812651; c=relaxed/simple; bh=NTukV1lib8v+vpZCsopNIIYxXpLwfNJUh7xyQcr+uoA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XUv5qOac/292cBFmqq8fRA2ql9gLljpLP2t8F1Q17zwHSH83wQziCd16K5QODPJOh77ajlAW0v3ATTPZmON+2asgKetIWy9mxF2f2UCySD5z385gsa+nazZ5LcEok9xU45EclUS1i07zeNFTXGszbO833Tc6zcioVPQ50l4vzi4= 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=sNnDzOAM; 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="sNnDzOAM" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4837f288194so77462075e9.2 for ; Fri, 06 Mar 2026 07:57:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772812649; x=1773417449; 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=kVq6C3tTKNfvUg57fXa3zcUeuZQjjjP6B3JtLdaXvj4=; b=sNnDzOAMagM07YDByKVoth8d3PyOJbwZAEkQC35xuKWbRJgThhT1ZoDjxqTI6ARFmA JfwvqEk65RuSVCZ/jNV82GDWtIVI0vJPOZbomtYV10+Cx/DKrGof/HA2HJ6tRYNHfqfv wzY+SK0amvyR2x6ITZ+KsuaRK+4xdwBMJ0RMfin5xT96z70MA+WnQwkXnMOYC0pzirWP fRrvd22eXOq+kGen9l1tX5nSXcCNVKzbXM9qCRvcrh6cOQX4U9j5IvRZ1En1H2Q8fT60 oYEYQCz7REUQWnhoQQbxB0Iu5alk3fuhyMFyZK1ByOO7oHGkq/dRDSsnfYX3hlxJMrwW Xs3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772812649; x=1773417449; 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=kVq6C3tTKNfvUg57fXa3zcUeuZQjjjP6B3JtLdaXvj4=; b=CEyxKuiXAMlTf5d8pUgNVBZMs2xI4mlvNnXOGKZEp3G03xnBjUnyJrOuAnQEKSiZ6V JVpNy9ea44SDr+obIs1J5oIcPCy7OTm86nfRU3+P+30DyCmhXVMVL32hspufVw08jMy6 gg3UO9wTSyufqNa8NJE6gMblAXzCUhW/deuGXXlFFlFv+NgVqPUDY5uNY3BuWqUWaNT1 okpARKLMVRSb8lRJhzyyvMPKmRWPMk0MdZpQZmSR8fz3GY4YHOVFQj3UbL3e4qKw4aM8 8Mnvwhg4ngdnxIbsRy3Al8OD0thYUxWK7OZOFy9vjIOJgiqBAJek+3cNNTTqBQIVz/kg 9c1A== X-Gm-Message-State: AOJu0YwH3kUzJUCd/wWTVDt0oKCuboYaI0rXsyCBtu0uCaw3w1zFkSWL AlHsj9AAIM66sRpvF/2l+Yzm41jYljWmPv/j26zlXC5CQs+57uA46h7oS2Z1pNMEKfmxUqb/eTk rMUvwDtKiNi3jadjBfUDczSE9sSXRKfWBVEhmI4Zx5ynbmRLDQLwtIIt4mIrefI6I+x9sx5duYj mZO/aPCIIXtCm3iKxO4JCPS8OBmV/obTLnDw== X-Received: from wmqn1.prod.google.com ([2002:a05:600c:4f81:b0:480:4a03:7b7a]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:c16d:b0:477:afc5:fb02 with SMTP id 5b1f17b1804b1-485269581camr47830975e9.21.1772812648681; Fri, 06 Mar 2026 07:57:28 -0800 (PST) Date: Fri, 6 Mar 2026 16:57:14 +0100 In-Reply-To: <20260306155703.815272-12-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: <20260306155703.815272-12-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=967; i=ardb@kernel.org; h=from:subject; bh=cmG4a34xlBm+ipBeX0cGGT+ZotSRXaNsY5p36e9IOq4=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIXPVz9BvnuuYPXdYXp5e/+PHv7bVAoEd25fXSMyf+p659 d3hfR19HSUsDGJcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAi624x/Gh9cf3PldovajMT g7wMVqqc2yIRsWqWyMmoKwsND7yx12L4Z2y/xkGjsEzxttNV6ZPdXn8Zrh50/33StqxZvcZjiZI LAwA= X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260306155703.815272-22-ardb+git@google.com> Subject: [RFC PATCH 9/9] 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 | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index d7a64b404bea..4d94b1e82c28 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -475,10 +475,15 @@ static int __init efi_free_boot_services(void) } =20 data.size =3D new_md - efi.memmap.map; + md =3D efi.memmap.map_end; =20 if (efi_memmap_install(&data) !=3D 0) pr_err("Could not install new EFI memmap\n"); =20 + /* Free the part of the memory map allocation that has become unused */ + free_reserved_area(new_md, md, -1, NULL); + freed +=3D (void *)md - new_md; + if (freed) pr_info("Freeing EFI boot services memory: %ldK\n", freed / SZ_1K); =20 --=20 2.53.0.473.g4a7958ca14-goog