From nobody Wed Jun 10 09:22:26 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 BF9BF326930 for ; Thu, 23 Apr 2026 15:21:01 +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=1776957663; cv=none; b=SKUEAmqnLNGu8P7b2PhULkFiVC3mjtDmOzIyE+q/Cegq+DdTAaXV0qRmuaaKTZ2+bzXYDlH0KetnkRd0TdQPYd5Cn9ot5up0yyDQ0+3gRNzSMnTmw1QU8QMrziLlpntWOPc7cdlxl0k0qaVsASvQVeFXe/kB0SOlKaeCOXKg9sY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957663; c=relaxed/simple; bh=rDCZLCXVdfhYaKY8LVOXFIF/6b3Fy0ov0xYCP0dCALs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=o0mgExQQ1adXFo0Ih1P2BnQ23t+yztMMLipaoPaOeSlW4munVSrc5i/iaDqzIZd9LWCiKudVKEMayRhjtYTD9ANM9ewvBOypO8QnNI1RVSod+Yz33YcyT/iZsFcNl0tAA55ce0ZyZcLuZlAQ1TKpyZD1I1kHnpnk9N5kOhF8sdE= 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=ZSQQFBdl; 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="ZSQQFBdl" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-488c2aa6becso63671085e9.2 for ; Thu, 23 Apr 2026 08:21:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957660; x=1777562460; 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=4mSa+7vyhBL8YFiiwNvF07aeMvbJQOJ+ZSj43s2ayiM=; b=ZSQQFBdlG2mmBfYm1RV8vyz5JyOZ2ATNHEhJkqiAcQKfcGyYYIzcQCv8j4eY/hnh4C L2NbR5ngJ0uGRe6JbjJCe8pv35mGkWWaSCKgMCT3DVLNLogrTqKVSjx5089XoqLEYrkl RKtABHbp49SNPgtrlLTaMzLP9J2trhRVj7hxjoI+qjk8tcz4Sj1x+ZhZpPJidr8+LHof pPCZMKS9s7Vs96guZ2LHsixDidMT+kyQNWmrWIvyvFhb35ffiqAEiBQDASH7Cgl0g4TO idta5nMtCWLCT9FQMjVXPKzABqQx11uTXSMFJUVWkt0yh8Xo6TO59DuBDof1vaTQ74Yo Dilw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957660; x=1777562460; 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=4mSa+7vyhBL8YFiiwNvF07aeMvbJQOJ+ZSj43s2ayiM=; b=l4Z15975Ph0LTV2YW7noYBqmniqk77pUH2lNKSUOIMA+x7Jy9aVNAKOFayRe11J53q Bn+vXyPeohEg0c342OJQ79BwIzONi0K+goA3MdS9AcVleImcT7Y3625zlrWSpQoiGW9I eX3hBIE91iLuQuGorereOPsqh/x2Si6Xw1HUlhDqSrfK4QZxqVXrt+AOIL2LtkwLKNn6 CsDz4lg/jhx40hamZ6o6aviXuI4jSr92diZr9GqMYpz/4PORCDDV+wdQztdXNC96jYI1 435OcYZ3AcgeO/zxEahrNNoYebuEp4/KO5vGPBhsQ4fHG9EhqFlZYeyJJ3/zebDcF7jE DoIg== X-Gm-Message-State: AOJu0Ywamcn2BVci+5cQtlEIZAMFpYns7vps4CncKbjq3oONTzY8FEtg OxxCN8NA008vqgxkUy7Cd03CIpFPt6V5eXYUtQGA1zlwjZcbmhWoC8OqHyrKjuTdqE5MpNhgd5Z 8NihIVVu2DLmW7xiwtvBpgUvgDqp2ulMIY5zMuh4COBgX5IMc/JNEkOInHW+LA/aDeQMo88lQHz C3bZYc6B0BkIPBbgmFCvM6J69atiAYaMKYeQ== X-Received: from wmbz21.prod.google.com ([2002:a05:600c:c095:b0:488:7ca9:439c]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4e4f:b0:488:d6eb:e63c with SMTP id 5b1f17b1804b1-488fb778703mr395298005e9.15.1776957660235; Thu, 23 Apr 2026 08:21:00 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:26 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2088; i=ardb@kernel.org; h=from:subject; bh=F9s4TuifOtslvPyT1K7byiUAjZGW0HaWaUoXOgAp+vY=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxa66dP0X1VxWvcv/LdjrtKPXNtzmx9yivcdiMha6v r5oeeN+RykLgxgXg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZiIxUmG/+FsKz61C5z6yzNz a4n302Va4hGaO1zc4l97crffv7WCsY+Roc3wV1dBuHr3ZPtO03e1B04ya70zyjOtkZA4e3KG+Sk jfgA= X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-20-ardb+git@google.com> Subject: [PATCH v3 01/17] 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 , Dave Young , Gregory Price 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 Reviewed-by: Gregory Price Reviewed-by: Mike Rapoport (Microsoft) --- 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 df24ffc6105d..4d8de7c6ce59 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 @@ -343,10 +338,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.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 DE1BB33261F for ; Thu, 23 Apr 2026 15:21:02 +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=1776957664; cv=none; b=LoRkhlI4o7cDPpSAwR0vYd79QJZzukck7LudVqBoYBkZkeh1rOZOBXii7hb5e2FYdGNCwhyMo6yLyHdjYgnwo3wsdxJt1w8lumT50uKjBikRmE21qgeUIxGZb7yfmNqnzi9rF/ExTp6C/mLOBK+NdhtJdh4sLGZkddcx/bi2mQY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957664; c=relaxed/simple; bh=wee0QVzVreWi9RJ0YemJikUQ3D9dEMKQ3RAWuVtiUJ8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=F/ykZJdvvZJRpG7MsTxv4wUY7d/PW1vxrLvx3ZJRW6+ggEBYHNNxoJt7RZZYfBlFMgvQj6wfHtvGoe6g9ToWyaRh00dWmyfutboiEXLnocDbrlQqWNr7B0aEnjYvLkYh8pH+LRDaQnCxuFZ1SXdvn9YMzWVlbiXxzMYcwEvglrw= 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=wKPYJr/a; 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="wKPYJr/a" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-440d12a472eso4034559f8f.3 for ; Thu, 23 Apr 2026 08:21:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957661; x=1777562461; 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=4cggla2hHE+Ip2W4Jmjd/BLtckR5M9vhjG/3uRS7ZNk=; b=wKPYJr/aFXQ+rNsjk7IeImwHEOisC1zSp2D7Kj9ph3eefbTy+aEvySW73D5ukszp5N i4ZoHas7gMvq4/Tf9QEr6V1tjNf3MgJqXuh9a7tkVPf4BcJGt8v1gIIyEGuF4RJJUDHz o+QtyB1/eBua1OY/OWCModgkYoQm6j8xj5PkEC1mtkKxkdbcCjsCLBKIOgx8TcvBXR0g yn2MIyKQN9RwIeZ7o4qOfd3/f4Pcn5lZraC0EAud7VMpjXD4CAycILqTlluCg5XRxmOK qjf0O7GFA0/8f27Oo+fqAug+DF+VNeIJhfNoZ2k4aCHMwnB11jitWUzRX77oLnfA29ZM bljQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957661; x=1777562461; 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=4cggla2hHE+Ip2W4Jmjd/BLtckR5M9vhjG/3uRS7ZNk=; b=QY39fWsBtfcJFS695ZcsHB2OEiZk/z73VjaRtKGuHtPW82ksDEUyHZ16Tpq5kQocUr l0Hn4GzY/4ZUbeR0MDoEPwIDmONv/1auuog/BTaJKfAXj7XUf/IlTl2eLHs0czA3ERaO ZrbGPvqjg+AZvWFAf3gHZBA3JdZ1VujEI8/sAsKPzEVNLJjjp+uarjViSbWGNvBFaKZy MNTDhMR2NslkBXblTh39r7+ajiKpKng33Gb31QggS6wXQNJzubnsLfTX2bgSRBZzbanc GNX8SbddH9gYSmlRJWQ30PkXUkITJDk5zvM0lgVxBd3DOr/6EZo/gFTq8nsn+eRGALq8 5FMg== X-Gm-Message-State: AOJu0YyIMZ14BrJJVfr5m2pYqsyuBQqInqLYVLDPDN1rpWRtITjnbDbI Ae6RvRn9Zo/mqmaX66h90vBeaWMv4HfSTTNfH5dBhBWYQW5/HacCSrZubaeSph1XdRKj/smwE2M uGn6xu8unqUfLYmXGtC9UKez2o17Uf3Ksu7FWGD6YEbzskojH8qRJZX9WpXxpa07ViBMerhRlw/ qAFuJyNqB7mUcvuD72L9uAga9GNzdKDaqK0w== X-Received: from wrux5.prod.google.com ([2002:a5d:6505:0:b0:43d:7225:237d]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a5d:5d05:0:b0:441:247a:e98e with SMTP id ffacd0b85a97d-441247ae9ebmr13356827f8f.24.1776957661125; Thu, 23 Apr 2026 08:21:01 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:27 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=918; i=ardb@kernel.org; h=from:subject; bh=VrZOK8Rp6qriysr4XEO2Q6DCvrCik8fTVotJeLqYD5g=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxZ7UqLbj/S8/a9zavszqbPis1h3e8ntL7ebPPxD/z vipB79rRykLgxgXg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjI0SBGhk2f56kdW7apsnVd wcV/elc/N9m/WxFxZ9VrG34b5vKfFWEM/yPnnwxn3nNilgNbuF0zJ1PZzSdvtjSGG3+dPknv5ln zbawA X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-21-ardb+git@google.com> Subject: [PATCH v3 02/17] 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 , Dave Young , Gregory Price 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 Reviewed-by: Gregory Price Reviewed-by: Mike Rapoport (Microsoft) --- 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 0c39adb96b91..1b7a0cd54d08 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.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 3FE0E351C3B for ; Thu, 23 Apr 2026 15:21:04 +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=1776957665; cv=none; b=EtoQUnLmMGPsEMtWt8p5HJKo/h65J2DO4i774IhCTglD8NYEuSPPIlpG/qj4F7HYaAaQnRWrR6FQVH0v20tcJFLC5I8B9TxSkK5h2xWhuNMI+M7sj9UwkTjS003PlyK5pNZtnNszqn0Bfd35yaDtkNXqxa+9LxpSFOeojBeNZKc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957665; c=relaxed/simple; bh=bUHL//Am2oa5BMc+mwzx9NssfOKaE6GrNTig/ad9cCM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dFlg4tfX5RPMyQwMQcIDMRhl8770oqkQSufPcVyNdJAQqeeGC0bb9YIGuxpR6MurXsXNAp9TgNxiZzjW0LI/wPkk/UTwy6S5CNZE+cA76jW+vZEYcTykz8kcgSuODhlAJK4JvPWU7h2fkn37z1D3STmVv2ffCO+64KLaIyfOPp8= 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=pIUo69hT; 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="pIUo69hT" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-488e097a270so48719135e9.1 for ; Thu, 23 Apr 2026 08:21:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957663; x=1777562463; 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=aUJqvSaANOdDR9L/33SKcbzCGTEvwMTq0jUvoX5ntMM=; b=pIUo69hT3PbZoap+eZIxurVMtruqgcyjtlqi/3o+dqrYMkmsBd2x4+ae9i5X7mapZK lTPaLGDeVo+/kVb4C2i8Vq5Bz7y/xalNHtrC7XhlCPQe+6upP1lNo7m6gWuTWsHu/8G0 vF4+c0YMF4wycE/7phkQ6SdFnFji+bS1YAwLePtneJjZZZREgAcTdL2NzvRqboV4aiHY TWE8eYGV0G7e+18f9oXeqCWiMnGD34APNS3JASUheNrbelKzgl/8VTG7Hg1gGTr79yXY pxUylIUMMFp1ZWJUZMchBu6z68DMtDUatgHFgO1EnyVuaVTR7gBxwMWlFimJWOnSH2lD jj4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957663; x=1777562463; 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=aUJqvSaANOdDR9L/33SKcbzCGTEvwMTq0jUvoX5ntMM=; b=KjR9dSN09Wg9j2STxmXNHQoyXTLr8214PpCvOXMcYzhhlGC62TtfywtuKCz4rYILcb KunkS+WzacZkKZlvQtDcfKbDCB9D+B7ksGei4IiBNG4zS7KMWMvM1sG6jbdTE7QhEjOb TSlFZWfTaplLp9NfUySjYy3wKzEbcl3RFoy347VCjm8rRO/TS0k1pu1aHFULRvLcZ26f dWjFvx2XHrhAxCq0FWIrcc7+LXAUZQYEATIJmb8StYgRIo63HVJ8JS0aNJPq3LMSRthD 89JmPa3WbacUm4mCNO0U8MiIN4Y09OL5/k5SdH/vu8VnSKvhB9YZo+EB+SCdiZWZ7Grs Yiaw== X-Gm-Message-State: AOJu0YzAeoUPFKdHpWV5XY5rFrKs88cO1nOuLlXXOemeBIX884/nCaCM WgJ/VJk53ab5ljEnWGw6baEXPK/rOqr9a+9h689lE0lZPgXRxhFR4bmeEvZKHk+AptC7s9qfRNE XIANWJzNgH6tOldBq7kSrS1604fwq1fF8zSicRaRiLG4ibLXv+GFrkvcuOQ/lNDtKVsiXShb2D4 PSL+19lbEYP9Zv+lojol0TILVUtuOrGa+55w== X-Received: from wma5.prod.google.com ([2002:a05:600c:8905:b0:485:3aa2:da59]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8b38:b0:487:59c:2bb8 with SMTP id 5b1f17b1804b1-488fb7925dcmr383448975e9.27.1776957662443; Thu, 23 Apr 2026 08:21:02 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:28 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=4148; i=ardb@kernel.org; h=from:subject; bh=HyyOeSodMSg9BQJay8XzIUTfk5/dlS8V1D5JXM1rIKc=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxV7NotIf/rZvlhwLPnv68pXIQ33PrY5IdDOszpW0e bKMt8e5o5SFQYyLQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEzk1kxGhhu39jrfT/FRyZ91 efEy5zOrDj3cuEjg6a1Xa4v7fpVJ+jxnZDj8UXjNFYXd+2T+XN0lJ/n/7e0m5f0RqwN73lYEFO5 6/owFAA== X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-22-ardb+git@google.com> Subject: [PATCH v3 03/17] 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 , Dave Young , Gregory Price 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 Reviewed-by: Gregory Price --- 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 dc8fe1361c18..f291845b403a 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 1b7a0cd54d08..c0195b5eab21 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.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 50DBB364058 for ; Thu, 23 Apr 2026 15:21:05 +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=1776957666; cv=none; b=kQj/QLIlIf/bANg2WBlFCWYqwCMkavwDQux+zHVDfDl33RisJzsyhubNdOw4KOlvCaWB/W/lMaGk0lz5vs4OThpHHrlwbjSI9dZoEjjXA6+8uajLzPOmTtN9hmO8AWYHAYInblunzYC+TkNKJUcgw/Darw+iSTU1ZoCFhIeB7cg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957666; c=relaxed/simple; bh=/OVhF57WbQigCK5DeTOM6aYd0BdynAOnjWEYScbyD2s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MS0vEJ6y3q7/Mlv1zYw/pO12u3QPjLB7z4/Z0zygT6QLuOM+V7/lPoQ5JdC8D4uMau7OgnzmiK42fP1brAJmgGaRaa4FPRuUfrhUnW+CqMWB4L2mxjvNFTA1EO6QobAlKWyLgFPS8ynKeGnSqrUfroJJ4kpz9coSjvDKP8ZzBA8= 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=fxq53Qgj; 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="fxq53Qgj" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-43d7757463eso4494048f8f.0 for ; Thu, 23 Apr 2026 08:21:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957664; x=1777562464; 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=5s0+CUEMaJcjjFm/1CcbVMArs93hKlDfpfsoFtL45Lw=; b=fxq53Qgjns/E3D7z0LcC/AipKRVqwH93SSequh03Mliqp8j5OHIKhPl63RHjGZoY94 zVG8iB+BBg7z6Epcnp0uMLgp3Tx0bTJF3o+V/wkYLIXj2A6tmLINhUD1AN/AyDdWDflE ktj6MG7B9NdSfIEX4c9SpjnUF3a4zV8gejDS5PSi8X/7xJYk7jJ1nB44sxr23JWqp4y6 h3/m8UHnPT/qUKecpDha0m4mCxZxk0SChwmNqek1TxdoU+xe32kc0DdSB0VlzHMG2LeS 8FNXb1p8YilYnTgOSN52oijyd4gPm1CQYSS9APVC+en0t3+v8sCphpXfRf76qpBab3h0 ZEJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957664; x=1777562464; 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=5s0+CUEMaJcjjFm/1CcbVMArs93hKlDfpfsoFtL45Lw=; b=Wk+V9M+tJBpOUtof5xZUTRGBDJq+olkTsdllzDuaZJw/SnBfBGvdEGZGrlpkN68swe yuMs/bFosdx6uVqMcl8BnwyapqNuzVSTnFkzttEnCC4qBYyFCB49HUl7G/TjYkg/eAwN l+aiVQcTR05avaVqO2d6ickzxto9XJ3xXJPQwJHtaPm7Lv7Nc9U7v2SFfr2WobSzo8py /X0hac+msxKEtqnjaeMSM+TM+iMkFvfN4dSB5EjJedLvSQGrXuvo1z1A8bohOptxnL7S Q1Pal/F1KP/ZV4S28g/YPk+CDa/ikuCwowFKTdfQfRm3pciyGeQpo5NMu2ZwKKcyy1ce gB/A== X-Gm-Message-State: AOJu0YxjErDig5uOgxFZu1DO8989R4n314LEX9pTIF/D6IWxWfP7Ec25 nGWpxe/zUST54kfqcAY01/4RfyhdOtrDGBaAO9ebDBpHFsU3AVr2fIiZX+VAOfYRuMkQYdrcp1k iilCzi/HoAQOnUun1b3/9rAIY7vbROkn76WOtJGJXaYDNA6+b20Wo1nh7c14om5tAlNV+vMssEE tgZdRdY1ZCJJklMUZvtQtVm09bkDiSaPil3w== X-Received: from wmim14.prod.google.com ([2002:a7b:cb8e:0:b0:485:4f4a:bd84]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:a410:b0:48a:5821:6006 with SMTP id 5b1f17b1804b1-48a5821692dmr132219625e9.4.1776957663617; Thu, 23 Apr 2026 08:21:03 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:29 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1907; i=ardb@kernel.org; h=from:subject; bh=x0whcXz7bSCxvxkEWlMbHW/Kp62wz+iO//Bp/09SSqE=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxf7IWZ7ndT7IHZ6m1Zrzd9JvkTZxP+3oZ2vPS+hvO rhz7yrdjlIWBjEuBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCR5B5Ghn0HshbqazK+y0hx cplcer10bm7q9aooNvtFQcbJX5VEtjL8T2SoLF2ok8PhZeUp0fqJU8whgnVuQBrHZRZHvV6tzLX sAA== X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-23-ardb+git@google.com> Subject: [PATCH v3 04/17] 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 , Dave Young , Gregory Price 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 Acked-by: Mike Rapoport (Microsoft) --- arch/x86/platform/efi/quirks.c | 28 +++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 4d8de7c6ce59..e2e57e9201a9 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -468,18 +468,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 @@ -537,12 +525,20 @@ static int __init efi_free_boot_services(void) if (!ranges_to_free) return 0; =20 - while (range->start) { - void *start =3D phys_to_virt(range->start); + while (range->start || range->end) { + /* + * Don't free memory under 1M for two reasons: + * - BIOS might clobber it + * - Crash kernel needs it to be reserved + */ + unsigned long s =3D max(range->start, SZ_1M); + void *start =3D phys_to_virt(s); void *end =3D phys_to_virt(range->end); =20 - free_reserved_area(start, end, -1, NULL); - freed +=3D (end - start); + if (start < end) { + free_reserved_area(start, end, -1, NULL); + freed +=3D (end - start); + } range++; } kfree(ranges_to_free); --=20 2.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 C5B97340273 for ; Thu, 23 Apr 2026 15:21:07 +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=1776957669; cv=none; b=jIXRJWZWghvO5bZiocfyWga33VTMtzuHBVS12DuILGnDVrqfTswXeXKC1wGgJIHaybmeym4rrnye/ERYzKpUODj+Nme6AzwlDao3Ef0UXJB3LBoHXXZH5iwp0Y8k4GTBnudFjFGxbrd5tYNRBkrL4zG59o81Sy4bAdC38U5B36g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957669; c=relaxed/simple; bh=csMz6OTWa744MWvUvvooa2OQVxqluJ1t6vQj1ZvJETY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GvWJTQSl0PB/6PzsTi6/B0FbAC226K31eNe0BHsCqK/6lYW4T5VTc0RxOFakehOnIq7EfV30+/ERBvWisbYnwe3F7/9FOQZ29WzU7RPak4Mh7dRYykQusUcfjNdjnAVfBvt0CBPdgXuPhlykCIMkP/zfqoQnPeOPwByI4z3JuMI= 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=uWZGUpcC; 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="uWZGUpcC" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-43cff5ef652so4362178f8f.2 for ; Thu, 23 Apr 2026 08:21:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957666; x=1777562466; 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=X1BgsYQQEh+WL/GlE+9+D3uI3VKv2eWJ1o6ED2se/NE=; b=uWZGUpcCxmJwbp6SkGxbhtOljKFS6pfwsDKig0jMTU774Vh6byCBDO0bO51rzNICte vupL3LenytKmTBqW9l+MGp5TEJ/CVvz70ls+mo2cpbXM+kgk0b1iV9BBtevKb/hElySm +Bl2HKnGn8qSVTABua48SPm2hN8+vaJXQdFknQ56KTpPPKRSRiAlEaUAi8COgZsHzQNZ +eRekxhAe+LQA9N2ug5VuDxbge2C7n5Adtvz5HkO9Oqy/RpGP1k1rkFQjVt/5E4u9tMX MW1tRkuA6wFMd5TYL4uWYDxbrvl+xLENKm3+SbgoTp4VFREhG2WNirx3f1yj23eLw8a4 5GWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957666; x=1777562466; 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=X1BgsYQQEh+WL/GlE+9+D3uI3VKv2eWJ1o6ED2se/NE=; b=HcfB+VBEDb2OxsM0MVf/2/duUUeqTyv95zO6AofKHVTnymfHwMouhzwjL+posd3UeP XJXnBNRlhznsH9jaIjoUXkLZSAG4ZovkNwDBHuqnZCsoyktIRLv+DpDBH7fsoAjZXEW5 NY9Jmc/T+wIaWA65aLOmhGFnBVVngex45IOFmeHAjVeCJ5BgZpRbFcRaaixRGvsLxoL+ 9AfaoN2Y0EWzLQgecnH5fBUG1djpPmXb8T2AzF/tA33pRFG+cCbJN3Pd2rrSOoxhFWzX UPPtB/2pjEBQZP/jRxrS88EszF1ma18llodjoCmcWuoksBPqxHaCXGZ1rMaXQ+0RBpME C/UA== X-Gm-Message-State: AOJu0YwVw/HHGjNDk+7yfvAyGqi0Qxr6aRC38sKwZwpJ3SgzkwvVuwI/ IonkWrLG5zdbzQk+X5uwnRksJkxawHJ/aOE0cx91wOchRrdNIp3WmUiVBfrUB7cdbfyS8h/05Di x+kjklCtxca9d2KPdbcaYRMExxZorDG2TAqg6eRK6jtSzmEmLtlqr4WtB59PkCmIs+mvjsZZixm Asd9P7zYgVIaE2kVjAXwQr5gMQmgvybaDnxw== X-Received: from wrrx13.prod.google.com ([2002:a5d:444d:0:b0:43c:f9db:4c36]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2303:b0:43d:73d4:b34 with SMTP id ffacd0b85a97d-43fe3dcb1e9mr43538482f8f.16.1776957665004; Thu, 23 Apr 2026 08:21:05 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:30 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=4315; i=ardb@kernel.org; h=from:subject; bh=mtKZ0aXjRN4p0VVWJRqKtVcFXFyhdtP/RFJ1WrypNIQ=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxYEVbD1Ntl4urAk1+p93f9h9NErl98KPuUmpP1/FT 5nyUWRRRykLgxgXg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjIul6Gf9rrYvIDd+343sD1 +1fFs22Z515qbD9wf77YibBfxw/+zzjEyPDATtTz/MPUPiXX14dOX/hmxNVwepL6i+p51rLeX1/ vuMMFAA== X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-24-ardb+git@google.com> Subject: [PATCH v3 05/17] 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 , Dave Young , Gregory Price 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 Acked-by: Mike Rapoport (Microsoft) --- arch/x86/platform/efi/quirks.c | 34 ++++---------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index e2e57e9201a9..e79fb94c1bf6 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -324,10 +324,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 <=3D start) + continue; + if (md->type !=3D EFI_BOOT_SERVICES_CODE && md->type !=3D EFI_BOOT_SERVICES_DATA) continue; @@ -340,11 +344,6 @@ void __init efi_reserve_boot_services(void) * efi_free_boot_services(), we must be extremely * 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 - * contain boot services code/data but is marked - * E820_TYPE_RESERVED by trim_bios_range(). */ if (!already_reserved) { memblock_reserve(start, size); @@ -427,7 +426,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) { @@ -448,26 +446,6 @@ void __init efi_unmap_boot_services(void) */ efi_unmap_pages(md); =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.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 2EB2C36D51E for ; Thu, 23 Apr 2026 15:21:08 +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=1776957669; cv=none; b=RvyVDWt06v6M1QEUVjRd12UyjmnIwGEOwgCw2uPTOnPAd07K/8DHur7NpPpnx36hy05kqLT9YXSom73rO+UKubnFJcgRT+YoN7LoB4AoSOJFHFMav5kxSnkks8dfcgrnBMwckxAPmxkrKCpbN4WA0DRTq8EIFhEYWnfhbT6we9Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957669; c=relaxed/simple; bh=yXlZQvg17gY3fAUY4jJcduAUEt6ZwFpw/pQ46EpXddg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OcIOkqw6e0DZVwxQ5p9cI4MLNdDc+Rqiwt0zSbR9JUks0j/ORTwVKYLOFunU1etrmkowTNJ6+YVpLiguZoMkTyM98ek3caKmzO3odS13k7ZAVZ3vsT3kqU5Mja2o2GzScQaL4FWkASK5CoTzhU61VRWxCtRMdYM0fKI+HsN8cNM= 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=vrV9AF+1; 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="vrV9AF+1" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-43ff19e54beso3632414f8f.2 for ; Thu, 23 Apr 2026 08:21:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957667; x=1777562467; 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=LbAiKGuplKRxB5rqKcaYbo7jdF2aTV0smrny3EZRzJE=; b=vrV9AF+1EpsRxvHSq5PDdUKdVYYh3rCGQM1murUeAp0SNAi+4S7u04fNcG/C5jITMZ whHtdY+QSDfPurWaQ7j6BhMMhClmZUPnidoYUfr0s9MGca8M3PfbQAV59hhBQEfzZYw4 zYlS+kKGimCoKLHDnhko+gmuNmBjOQUemDFyR+uEq1dwpjJRKNALFkGaJ4+TfchjWjtt eo0i6d3tj1xmBhpOVQ/DsB+/DoRhwjT6vtR/EtmIOXidBk6YMVnCWAjhPPBsixELd01t Muvw04hcw2Y3q1I4LrOShyB5mfVo0DFedRZ+LkNq6jDdJHVIblRdVP/S9+22St1ZBNZM Y9Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957667; x=1777562467; 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=LbAiKGuplKRxB5rqKcaYbo7jdF2aTV0smrny3EZRzJE=; b=oE7SEywRhsvzqj+N5ua86byOQen24GZeslYVQG5bznM5MRsYpaTKZIY3ZJrw2068Nw fHRQG/8xPZZz42xgYm+1mOSOKMaQ6oUz01yY61Z9uZNJ7C9tLV2E35OevRLZwYVbetA3 puPW88K9mmJrVTF5ffB+HvTfAgOz8gGFN7SnvhT/Qnp5SxePH4Jmbs3dMYDoJbZkVyTY DC6Il9mNfaGRWstbFKTeNTs9n6Y75Zvd8cOnmykarPg7Y9qsa5wx5L/rYUUnV5rrhwXW 5MxJLTNHVAgmtUSNv6jvxUt4rB5i4dwYMP9VDsyGz4se9zVAa6r1/Me4a9mqHzEe6zG8 9B3g== X-Gm-Message-State: AOJu0YxpeRth+U9h6uiQaE7RqW7Rj3wJR+uH8ET1M3yyqRlZQEklL+Xw yaRjDCfUspOZLO3vot5mfKEqm3NWgODkklB6TBJrPBlTyO3fJjQlvq3SjpTZ52aqTN4DEwjytpR LZ79SQy7YssxpqPRGM8OG0NOILjTh1cNMfyhSup75+tFNKY+XtkXfg2yduc1JDoVGlJIhSvN9XH 5btKzKSGKj+mN9QpvJg4ervXhzyXoRqw3SwA== X-Received: from wrqu15.prod.google.com ([2002:a5d:468f:0:b0:43d:70b2:d29a]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2c0c:b0:43e:b0f7:9ce9 with SMTP id ffacd0b85a97d-43fe3dc49c6mr44414203f8f.14.1776957666460; Thu, 23 Apr 2026 08:21:06 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:31 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1544; i=ardb@kernel.org; h=from:subject; bh=XoOv4APS7l/c88gRUS9wFyxalKNkl5Mjj1u3QN7wgaI=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxaEZ3lvnpK04VMAe7/nMeiLrMlV1tnBzzr01P+cK+ s7/43aio5SFQYyLQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAExk7SRGhp1HRe167uYzSW7W 5tD/Itw6LelaWUZh/cM5NSW1gsJdzAz/q+KSi8pq1586pDCryCI/2fK0pfj1rRIL/t26yJv05kk 5HwA= X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-25-ardb+git@google.com> Subject: [PATCH v3 06/17] 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 , Dave Young , Gregory Price 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 e79fb94c1bf6..1d10277796b7 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -433,12 +433,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. @@ -446,6 +440,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; + } + /* * With CONFIG_DEFERRED_STRUCT_PAGE_INIT parts of the memory * map are still not initialized and we can't reliably free --=20 2.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 52AF336DA1C for ; Thu, 23 Apr 2026 15:21:09 +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=1776957670; cv=none; b=ijwvkTGCQOd20E55+Gok19j1aCqfUNflIuo87K51R6zh1eZPmO0EG12HCrrVoeoFWxPRd2izoDsXvVtTvbhI0gcYe/A4htUxeXkW5GJ/OAmGRlGAukROLAKR7MRA3LedMpth4D08zY/ZVHN7IJQD6TlLAYw1/mIMMW7B99+3738= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957670; c=relaxed/simple; bh=ecAmlKD2UQkybmDAzI1zpd+FbZQvA7blVj9VpuIkWTM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=O7a1WMW9lzhTa7dd/LNDtJNKq9jOsg31K19WBQkzAoLyR6ioNdgyDKtCsWzjoGAbpKMeThi+ZNIH4uACcnsfcbWSrEExAWR1sUQiOJ2DjYHvarhQFUYSzArGSjUotdk8JfGjsN80o/a7FbWYlNtzoVKDAt+iKwoDKTeGv0k1PoE= 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=gk2xpW4a; 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="gk2xpW4a" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-48a5adc12ffso15057875e9.0 for ; Thu, 23 Apr 2026 08:21:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957668; x=1777562468; 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=DgaUOuQsmpy7UIfoAx7B9eDC63iILUiw+rYZIcX3QCg=; b=gk2xpW4aUvoCoxkbWddnq6+8kD3lZ4Q+JYipR16IYpa61dOY+DNMXSqNhOXQTCjLGM SJ5wyBh7Kl3uC0CiXJNRoQ7TYhVG6S0S8ve9lPclWJ3TYkOZWrHZg87yDnb5atDcvQL4 8oKFocZOgFGlrH9A+a6LrVgKAYDmd/qUILRxxn6X8bTaJx0maLiFHk56gsj7MiuFRzMi 6qLuyPQdR9H6tRE7d90ALDoW98xM8nVe8hskMOTutbio01dXpcWRl/shf3clbo6r9ZaN w7u1dQre1a/2ZKkincbOyxGp8g1DN7X4m/7DT0xk3+MAe+boIaTVvYIWsBHu+xXNswVC HfkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957668; x=1777562468; 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=DgaUOuQsmpy7UIfoAx7B9eDC63iILUiw+rYZIcX3QCg=; b=oMS4yZTZGRlCrwiGEQYl4YpPVPShtkcQT1gSBF/iLW+5Ii2ybwN1N31Sz5QKnqLrpp ya++ocNr1/9bXjk7wXXivR6iKZvx39+ElXXPYLMFB6SUzca2V0+YoRtl8bKYpf14Lmgk IfcflygIFS8s4QZAXKIRfNvNqzHMnTP9XbrlgupoKfSkE57YDscppVXSP59hc7+lElf0 grQt/NDX0XitH2gykWmtXJb53d0hJf4NCq67Nai1Cy15g/0puZsTeaBK3a5/+Ox0mbpY 4AAB2jVKzCZda56MoxmtJTK+elNtnOyqEibNcIbYhRw4iW1waogNisGfd4+141ihzOGU lPkA== X-Gm-Message-State: AOJu0YyCxnUMrTjqGppweOd/OweG2iKyqm1UOVvKZT6qJoGlnwAgfRSF p9JwP/jcqiiHBDBKat9uxoCipN9RQl0Pj7beZJf2TE8YykInQwN1z9KV8KLKFSqczatNInD2E3s o6t236JyxyPQVLWGMw52v45z9jA8RZWW3PUzUrkLA8bOJYNep+cT+uM7yvxp4hIIjnJ54COG4VU t08ConOmV2X+mfgcW6IJ6wZnATWGCJCY5PUA== X-Received: from wmhn18.prod.google.com ([2002:a05:600c:3052:b0:489:1ffd:15c7]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4f8c:b0:48a:58ae:993b with SMTP id 5b1f17b1804b1-48a58ae9faemr164289565e9.16.1776957667506; Thu, 23 Apr 2026 08:21:07 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:32 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2080; i=ardb@kernel.org; h=from:subject; bh=gwbvKwBOjjoC55cL0c8AdYcFSGCt7/IeToq2a8GYrj0=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxWFuHveZf6acvvI/YuI/zXVJW1mrpuceY9u3RO4Ry +3zV/c96yhlYRDjYpAVU2QRmP333c7TE6VqnWfJwsxhZQIZwsDFKQAT2X6N4X+cUe31Ov20qx6X kiwefOOt31zFq9Ngadf3errrtCcJc+4zMvQVz54wUfOrC//J7MNebvvmhGy7tj9xwbYCRkMf3mw +bk4A X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-26-ardb+git@google.com> Subject: [PATCH v3 07/17] 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 , Dave Young , Gregory Price , 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 will be addressed in follow-up work. 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 | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c index 12c8180ca1ba..50377423d422 100644 --- a/arch/x86/mm/ioremap.c +++ b/arch/x86/mm/ioremap.c @@ -124,10 +124,12 @@ static void __ioremap_check_other(resource_size_t add= r, 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; + break; + } } =20 static int __ioremap_collect_map_flags(struct resource *res, void *arg) --=20 2.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 8CFA434E745 for ; Thu, 23 Apr 2026 15:21:10 +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=1776957672; cv=none; b=X5dWTZTZOSo05erg0JLk6K80vLH2f9k5Beqkyj0BPbEou65jc+uoVXJ23GnLK7S9sOffCr78xB86Cq3DdVmAxwIiJ6tItXyR0TwGHKecJxSeX2VlrXtKyDnMCaXobZGTMX0Z3PrsC+Ayc8O1wjpqLNE/fRIBThKHzlkH40xvBy0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957672; c=relaxed/simple; bh=5fjE4LXIEJmHTzQ/V/kRAovHQOQihCgfBFkxYfOGr8M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cyqeiALmDWOg2oZCn7g7AYHoWOaBpAV/FfxDssUCedJdxBZNItFh2Z3f57gSLNhfzIrFhF7MjeDWWYEf4AbputeEWN2ZgFcIru44j/qFm187Zpn47gu9UjPvgx0SC73lf0ng1AJnfiJmaHCOY9llMY563XJMpH12Ik3ehF4CLSI= 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=XS/mv8kU; 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="XS/mv8kU" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-43d7d03e1e7so5356573f8f.3 for ; Thu, 23 Apr 2026 08:21:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957669; x=1777562469; 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=rdeqA1VGz5Iv1QFq/4S2zcxPd6cWnmrt4GnD1rhl1K8=; b=XS/mv8kUBFQAJXj0tldxoFGS36RPEumby6O0QUWbWfdqSm/ycgOWA+tbdYeQgAhPMl p4XyA+SNaB7q9HjEYL8n6w+3Ow2WIoc1Vm5rpNE2jmj4s3WOCqaNluzXpX2w9i0TAyzp OkwT7CbrzaYTk6NXxETdYSS1b9bTYt27RukiplG2u7JlzqZC1E0sYeZPtyGlc2q9b0iB 2Jxp7/H5ro2L4HJIFjK1xPRhcZDb49KEfE5cBL7W+QXKoMYaSSY1amNZ3C6iqLvObajP 4PkrQCuldN0JwjZqtEuriI5sRvCIPd3TX2IiEcz6hI358tIcKO91rIdpC0pvISAXsxQP wVLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957669; x=1777562469; 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=rdeqA1VGz5Iv1QFq/4S2zcxPd6cWnmrt4GnD1rhl1K8=; b=Ncwgc9/0kRrXl09B7KvAf59EqVynN6dBUVAQ1eoP3bU1U1u9GeKaPlXG4j3N8AT7sm 33PE4kmKyhm03o0lwyNL+cGTEbzJHiIsUJb/UuhE5qjpp1bMD2/6+vflXyEGkpbs5xpB uRIsRZXVcDYOosyzmeN0oFdDjqEObAHWlfKQvN3eUf8CbkdN+8oaJOJiZKkRgsdu+C4O 7Y1+/py82tLH+TZJKRaLomAxhmTFtq7OuQJkiCnpXbvwKe1HNOjtcrnUe9CIG6i7f69k lnukvlWJ5bs8/NGWI3zyi2KBmg0Yt5jZ8aaOoDQgSwyDvkRHOH1wgUZ4uh8e3FYRikxN vB1g== X-Gm-Message-State: AOJu0Yy31e1Lr8Yz9P9pwSrWePAvJ5sMlnY9vwYNI4uhqI945wjKlcY+ Rc5FuY2uCBTF8dZuXrxHp167VegqrE28YR5ckjGtpcZrcO48FIJwXn3n9lVWXNqUp7ICq6Ue9wz 6qtHQBeEcY6MzQCoAsIhGjS3hwtzr+updy6sPssU4Jd+s3zzUEgC40n2yxSfnBCu91BwCMB70d7 ykN82yMdMBssidqx+wfMTykR6T4N/cHBfqcw== X-Received: from wmaw6.prod.google.com ([2002:a05:600c:6d46:b0:488:9438:99d5]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4f13:b0:489:1a65:dd6e with SMTP id 5b1f17b1804b1-4891a65de3emr293129545e9.8.1776957668705; Thu, 23 Apr 2026 08:21:08 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:33 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=3017; i=ardb@kernel.org; h=from:subject; bh=dGldVZd5uK6BXjGk3oUfE6gDN9Ky6KDworVGSvSdAhY=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxRGRQnvlk08WJp1/tT/o1m3NXhmZwsKOwGXHzzzet 2NO6mr7jlIWBjEuBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCR9a2MDE8j18+z+fPo6z82 Fcbv1wo/C6wUXMS35IuYSWLqycjgb4sZ/oofYbnNYpml2So6P07yeHRouj3DiumZYT4Tivv1nl1 7xgsA X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-27-ardb+git@google.com> Subject: [PATCH v3 08/17] x86/efi: Allow ranges_to_free array to grow beyond initial size 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 , Dave Young , Gregory Price Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel In order to avoid the need to mangle the EFI memory map, which is being done to keep track of which boot services data regions are really really reserved, and which ones are only reserved temporarily, this information needs to be recorded in a different manner. The temporary ranges_to_free array is a suitable candidate, as it is specifically intended to capture which boot services data regions should be handed back to the page allocator once deferred struct page initialization is done. This requires that boot services data regions are intersected with the memblock reserved list, and this may result in more ranges_to_free elements than the current upper bound of the number of EFI memory map entries. So reallocate the array when running out of slots. Signed-off-by: Ard Biesheuvel Reviewed-by: Mike Rapoport (Microsoft) --- arch/x86/platform/efi/quirks.c | 40 ++++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 1d10277796b7..ce452e5c2f0a 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -401,23 +401,46 @@ struct efi_freeable_range { u64 end; }; =20 -static struct efi_freeable_range *ranges_to_free; +static struct efi_freeable_range *ranges_to_free __initdata; +static int num_to_free __initdata; + +static int __init efi_add_range_to_free(u64 range_start, u64 range_end) +{ + static int idx __initdata; + + ranges_to_free[idx].start =3D range_start; + ranges_to_free[idx].end =3D range_end; + + if (++idx >=3D num_to_free) { + num_to_free *=3D 2; + ranges_to_free =3D krealloc_array(ranges_to_free, + num_to_free, + sizeof(ranges_to_free[0]), + GFP_KERNEL); + if (!ranges_to_free) + return -ENOMEM; + } + + /* add a terminating entry at the end */ + ranges_to_free[idx].start =3D ranges_to_free[idx].end =3D 0; + + return 0; +} =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; - 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)) return; =20 - sz =3D sizeof(*ranges_to_free) * (efi.memmap.nr_map + 1); - ranges_to_free =3D kzalloc(sz, GFP_KERNEL); + num_to_free =3D efi.memmap.nr_map; + ranges_to_free =3D kmalloc_array(num_to_free, sizeof(ranges_to_free[0]), + GFP_KERNEL); if (!ranges_to_free) { pr_err("Failed to allocate storage for freeable EFI regions\n"); return; @@ -452,9 +475,10 @@ void __init efi_unmap_boot_services(void) * 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++; + if (efi_add_range_to_free(start, start + size)) { + pr_err("Failed to reallocate storage for freeable EFI regions\n"); + return; + } } =20 if (!num_entries) --=20 2.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 849A5356A38 for ; Thu, 23 Apr 2026 15:21: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=1776957673; cv=none; b=XwJxmiZ2Rsj1RXMDv+wj1mt0GUtMKkkzzFgWmqGKlZafeauSkc4D7kyv1ocKoHHcjAGiP8WyQrFxe6zTCMBp+CesmKGwee2IU/BelYBUOMV7YIZNE/3G/jvQIZrlmnq0Tnh5ylgwNACJhmrEpF9RIwZCeZQTg4f9REqf+1pMEJ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957673; c=relaxed/simple; bh=tnB6iuZ6E5vbcxeFaPRRMPwzhcy4pO9KyeduoWMF7rE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iRL3RzAmhUEBWjO6k7BjXMXYNT1/ktdUqCXV4E+bDaz8+BN3B2mSoDPToOW/hfeVITwNcohfgoOq9uUb6/rUGn15wJdgVwl7vmx9p9RY9oKpiqYqw7Ips5+2VzzOpTJRr8AmdbZHSCwJczyDdx+SUVvlIgXguGw33dXTJk/Xac4= 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=SHDd7lWz; 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="SHDd7lWz" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-488cc31ea57so53029495e9.3 for ; Thu, 23 Apr 2026 08:21:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957670; x=1777562470; 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=RFFa0eyeL35xeyu45ExmOZX3U0AnLJJNTndQMnU6Phc=; b=SHDd7lWzZi3z/3436vI0/dPhQr8kp5ZIW8ZzWCEcKo0EwK+EHHTEZCxwUV0+SNQ1kO 0yTcdy0u6HsMglM0LkYRa/pXJBJ5ump+FMvwz0iyYkQ4h4bb06ul5IrCruaiTwStCdU1 1bhMYyjleGg0imiACvkdi8wgvxcS3VIZE7P+LNK5oU3UO6gZhs277org7rjBixZYiwMN GI14S1u+O9tXR7uHsO9fwajqMtKpCEj9s06W605nJN63U09hSP6sSBiKHe9KmY5zBHS1 NpwisSxcDDHH0AQ7gne0u3sTtoTGaFhmYu3DTA7CP7Har1AC7O/S89VqQ6N1P6CL3lCs sixA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957670; x=1777562470; 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=RFFa0eyeL35xeyu45ExmOZX3U0AnLJJNTndQMnU6Phc=; b=OPtIVNciihrDImtUBK8Yxs168ART/qOFzbVlsLBHtayXM3yo2OghdfEbdgVarQVl5w V0XjsKm3Rvio0pCR1Pd0Owm0sNjmK6peGEZUA8tJOhZ53liRsx7q+IlDHU9eGfV2Z19q b44tccU+qtevNCiehIWl9/fDzZz1JqNDd2pE3NAChf6TNZc9IJkrFMSRkwyA3QP0WWMR CqfQfpU2TxofGJ7nFk4lyrc8O+9kb8C7oveoAyQr0fr/1tLgyTE1/vW00GIHHtC128WG SDOiiPzlYum7vkpjll5tcJi1KaTJgi/0xcEbt0iPHXekIU1yHIIpR4/TIAXK7+IXiOep dGhQ== X-Gm-Message-State: AOJu0YzpJEKGuEWGXgYPbLJGZsF75vWEUSQbAjPxKSAtUAGgtiYB7JB+ pYaXbzYHvpxncK2TRJU76gbHvbUDNXvuB+7Nd0nUczqRrnJ0OogMj6xxLanEpN+OfozhHAKASez YZxVNWSDGdQe81VLSMCC2UsR1kKgY/y57Q0i1G6kzEyYW4+phHpEz/m41w3NKN/rEn+p8+SRVrS 9WbXSbxRtWQx+Isg5mDpaXIIVusHmxyrApiQ== X-Received: from wmbgz9-n2.prod.google.com ([2002:a05:600c:8889:20b0:483:27fd:cf2b]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4ec8:b0:485:7f02:afd5 with SMTP id 5b1f17b1804b1-488fb755caamr407991925e9.13.1776957669759; Thu, 23 Apr 2026 08:21:09 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:34 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2056; i=ardb@kernel.org; h=from:subject; bh=jc52JwSM1lqsY0b8j2g3nRYFPgUE93s/+qfuH/7VRek=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxbGysBDho4vj7f/fasxW4QxWD1e52xv61MdFUOj3Q wPjV6UdpSwMYlwMsmKKLAKz/77beXqiVK3zLFmYOaxMIEMYuDgFYCJ7/jH8d9vyLcv7Li/3Jraf AiKppit1Nf5FdWow+Ns8kpNSzF2WzsjwK7vot4Jk6ia9ivuGC7fuW9sX/IFf6tIkJ7Yryg563KV 8AA== X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-28-ardb+git@google.com> Subject: [PATCH v3 09/17] x86/efi: Intersect ranges_to_free with MEMBLOCK_RSRV_KERN 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 , Dave Young , Gregory Price Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel efi_mem_reserve() will mark reservations in memblock with the MEMBLOCK_RSRV_KERN attribute, so take this into account when building the ranges_to_free array. This removes the need to split EFI memory map entries and tag them with the EFI_MEMORY_RUNTIME attribute. Signed-off-by: Ard Biesheuvel Acked-by: Mike Rapoport (Microsoft) --- arch/x86/platform/efi/quirks.c | 44 +++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index ce452e5c2f0a..ce06a388fc11 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -406,19 +406,41 @@ static int num_to_free __initdata; =20 static int __init efi_add_range_to_free(u64 range_start, u64 range_end) { + struct memblock_region *region; static int idx __initdata; =20 - ranges_to_free[idx].start =3D range_start; - ranges_to_free[idx].end =3D range_end; - - if (++idx >=3D num_to_free) { - num_to_free *=3D 2; - ranges_to_free =3D krealloc_array(ranges_to_free, - num_to_free, - sizeof(ranges_to_free[0]), - GFP_KERNEL); - if (!ranges_to_free) - return -ENOMEM; + 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; + + ranges_to_free[idx].start =3D start; + ranges_to_free[idx].end =3D end; + + if (++idx >=3D num_to_free) { + num_to_free *=3D 2; + ranges_to_free =3D krealloc_array(ranges_to_free, + num_to_free, + sizeof(ranges_to_free[0]), + GFP_KERNEL); + if (!ranges_to_free) + return -ENOMEM; + } } =20 /* add a terminating entry at the end */ --=20 2.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 5FB8C3783B1 for ; Thu, 23 Apr 2026 15:21:12 +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=1776957674; cv=none; b=E+vMOWFN94PaUq5jgviWHG1Kovgc1AT+VtjosxGO3qszN3rVm3lShLXlUot1QSAuB5OZzesWBLJ+11ZKqd1qOBY5zjzRSCwjyL075x0x+TDwGuK4ZMy8B1D3To7LvAgN7n4yTm/zf7xjgrym5BnCXRsr6HnaKBEVzUlWc5GYLTc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957674; c=relaxed/simple; bh=tiRoGkPFrmHA8DhoFA6DaUdZvOus7sOeLm2EdZ9QXNI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=No0otZRujpg2744/FtCbVfSItwMuNIU/8vZaL3k01bghdvUKq4fJ2RlqeJhw1n2KqDsUPIg7zG8qLn6oiP38wkPs8qhDX75Kvt6d+OpXQ8QT7lUegQHbL/QUHCxYBBzTmi/gceL+xRmClje388WfKk/FnMAeruYGUIs6rPnmf+E= 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=Wgf22wtM; 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="Wgf22wtM" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-488d9e1e61aso57949655e9.0 for ; Thu, 23 Apr 2026 08:21:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957671; x=1777562471; 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=EqIsK2LttDMvDAmHIy33jLO4k6bzz8jvDDP3urmiklw=; b=Wgf22wtM/iCv10rXW35YWxzJjE+RtqZcX7J0Tr+HMzJOQg/VqwcDJn12WXryc56ET4 7+bJOdAfXCfzkmXkmaBQ8gQwagyEssu4UkwQSSVtgP1XkF1/ex6s/VyTSCCDpv0777qu PXDsuiTEB1RQlRrqNHp8lAfdUu04yrO7ETVebfleqbhM8tz7eJcH9ZUR6HItB64Sxec5 EbMzjhXmwHJ/R1OWTDOUGsTSy/OZASu+qe+Lf/i8XOX9ysNvzTrU3qxcWDALJGVEYhbI 3mC6l3ElRySFZUC1fW051nttSJLcTlvBy5fMj+Rb/xpGfywTVCk2JMtzshKN7nBUMRTl jEMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957671; x=1777562471; 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=EqIsK2LttDMvDAmHIy33jLO4k6bzz8jvDDP3urmiklw=; b=Ae2P0mW5z7DJVb87auSIkyA2R0icgSg7ZyXTXiwo66W4INpOPTUmzx10SpDsgPiZj6 T7PICrhYqXrKWVYWCgjcDxQn9y2kRUrSa/c0TAUUzgZ8JY3F6P3SnaUFpXUa/oSPsmrX ko2eHSpMH0WetdHEBlwAFWUY/iWSfQ3EI61QgOTHJyybHUM25WhtNbQOZMKeOZOJoEzo MBpVQ4LTxA1IEzMGd+Bn4L6AvOVzv7l+s51XCYdnA1UsFaUCi2S0t4xR9DSO4dUDhOWY MN2Sq/A667GsUKzTZYFPUm6YZ1IAtAiCsCutE7tagKDEdL4qRgNAmlHvEslfHqdvRfs5 uYfg== X-Gm-Message-State: AOJu0YxrSY8fiD84Cuxuff/U2k9P3Hj92ijyYNqo2aN69acjdvnmSEsv 5YW8k7gXqSdAMzkoQsHSnIXddTtvM7jVx/ZmGC9PX+smaY0BaNSpdQbWaNEgY0+Tzs2x0sVYRlP n+95cP/gJMWnXqDl8byg4Xt/6z4FVxeOj2nMqbyO/8l2MVXvtLKqUWGpEHZC+fMDZ0af3U2is9b Ho/ud7QrRkr8muE7+1IH2TyONQsLqP2XDigQ== X-Received: from wmbz10.prod.google.com ([2002:a05:600c:c08a:b0:488:c1c7:5024]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3596:b0:48a:563c:c8c5 with SMTP id 5b1f17b1804b1-48a563cd1b2mr172866315e9.8.1776957670758; Thu, 23 Apr 2026 08:21:10 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:35 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=10109; i=ardb@kernel.org; h=from:subject; bh=dWoVGXM0HSTwZN50WA1NRBKn6uv2bVPhj6xEuCOuz6w=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxXHnK+le10oeHFgvLrjjnsnG3MNPuwT0dTTy/+6/b uqcfmhvRykLgxgXg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZiI2VWGf7Zit/eLMnX++18e 59BYVbtaI8+dt/SgfMEeiX+39xr/smZkmDL/gLzP91NHzgdYfp2gNdXlxReJJOafwuZlr+L3zfb UZwQA X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-29-ardb+git@google.com> Subject: [PATCH v3 10/17] 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 , Dave Young , Gregory Price 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. 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 | 77 +++-------- 3 files changed, 17 insertions(+), 202 deletions(-) diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index f291845b403a..7d8f627805df 100644 --- a/arch/x86/include/asm/efi.h +++ b/arch/x86/include/asm/efi.h @@ -402,10 +402,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 enum efi_secureboot_mode __x86_efi_boot_mode(void); =20 diff --git a/arch/x86/platform/efi/memmap.c b/arch/x86/platform/efi/memmap.c index 697a9a26a005..da7483a33d68 100644 --- a/arch/x86/platform/efi/memmap.c +++ b/arch/x86/platform/efi/memmap.c @@ -107,141 +107,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 ce06a388fc11..efb828b7e2ab 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); } @@ -404,7 +350,8 @@ struct efi_freeable_range { static struct efi_freeable_range *ranges_to_free __initdata; static int num_to_free __initdata; =20 -static int __init efi_add_range_to_free(u64 range_start, u64 range_end) +static int __init efi_add_range_to_free(u64 range_start, u64 range_end, + bool *has_reservations) { struct memblock_region *region; static int idx __initdata; @@ -420,15 +367,18 @@ static int __init efi_add_range_to_free(u64 range_sta= rt, u64 range_end) if (region_end < range_start) continue; =20 - 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)); =20 if (start >=3D end) continue; =20 + if ((region->flags & MEMBLOCK_RSRV_KERN) || + !can_free_region(start, end - start)) { + *has_reservations =3D true; + continue; + } + ranges_to_free[idx].start =3D start; ranges_to_free[idx].end =3D end; =20 @@ -471,6 +421,7 @@ 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; + bool has_reservations =3D false; =20 if (md->type !=3D EFI_BOOT_SERVICES_CODE && md->type !=3D EFI_BOOT_SERVICES_DATA) { @@ -497,10 +448,13 @@ void __init efi_unmap_boot_services(void) * memory here. * Queue the ranges to free at a later point. */ - if (efi_add_range_to_free(start, start + size)) { + if (efi_add_range_to_free(start, start + size, &has_reservations)) { pr_err("Failed to reallocate storage for freeable EFI regions\n"); return; } + + if (has_reservations) + num_entries++; } =20 if (!num_entries) @@ -526,8 +480,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.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 37B56372EC0 for ; Thu, 23 Apr 2026 15:21:13 +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=1776957674; cv=none; b=sKpbGMMJctVjGk03USJBLyK1Afbu0QUJ0BaDXvhSUFzQuK9mSnMiKO7L1aJtFxKooLWsK9LcIu7A/9MYfEe7O1m27PmbBBMu0gWNCENTi1k4iWPGndI1lmSBNe696p4zulYjcEUguhmaqaZj0WENjaVjK5PGsX68MC5HhUXYm0Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957674; c=relaxed/simple; bh=QJhcTCp3YWRk82k5dY7bOrapMMe8ofCD/shtK0L/ecw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dnzxQQMVvWLD4UYT+QYGAgFYP9Ofnvabxh+hYayUO+eDYT87uvnR+IJXvf2EJHBab77cnnPvK/fqjK/dgHAOnT4QAosa52EyvkCajFX/+1NXIqsOYnkzvcEcsBZJmLixv+wqQqFWKo8HdgGmaIdWHbjKXcha4xQ5cSQohNihwn0= 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=SEFpaJp5; 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="SEFpaJp5" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-4411a36715dso3367899f8f.2 for ; Thu, 23 Apr 2026 08:21:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957672; x=1777562472; 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=Rm/hickqQysiSynTiIHCGr3FHLXl71ce3ok8oWE0Hnc=; b=SEFpaJp5cXMYQe26/f7fcQaPmbWKGJ98AdbPqKHOGt1tiddPfdogUw3jBcliCERUhm d8AG/jebqK/qUB36UqGnCzxi968xerE1d2A66Eb73M/fq/t6pavEma90byq6St/YSePC TTxDJ+YekQzH/ycEQo/42GituZJJPam6p0d+0m2YabMSLnC4tVe0OydTQ9xt+1jmY3B9 +cZHFG6PvjUf8WHuW3WsqLOBfh0e7uqBs544ST3sHGKs72GEHo1+AwRrGrkog6fkGfOR 2rKrXRCZT/jeSpAFsrMXQEQOXSe6X/zs6Q8Tx1kxR4w78tMG08mzlKrxOb+2JNVLbSR1 vJcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957672; x=1777562472; 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=Rm/hickqQysiSynTiIHCGr3FHLXl71ce3ok8oWE0Hnc=; b=Ngr5d9whfEoGn9XkoU8nBMt5VMHNOY763R/1XPPiD8LOGykki8b5REuMIKiZZa4tRY efczszOQUMhiGqNEPWDxqvuTpX9xljygB4jNgnxxLk9QOveUIOyuJOMHiIM8mk005qHv BxZmjwQ9QWdkB98dhxwBPQQgjMTgPbkR/u+ov4NPfh+DcysMKXBGow/XC79MgvWJqkL3 apr7ORMWvj/flh0mJMajeLkwYCe3PINInm3a1YV55yz0uLF/xVo/Ms2Q9tMLW6ZXAQLE rTIan2HDCg1nNs9bIdelip5LkIi/JiPumqByAh+r8kJjhs4v6GgIRx/XV01XyCQoqLNf GdwA== X-Gm-Message-State: AOJu0Yz3xWmO2HBBTybqhE6nyvjHyMDmdH5ygrbXjrOaRf9FNGU2dr9q IxvNftzAANlzOgqknt3MrpBjYo96NeyPrcrTMykzGPUSjPAptW5l3zrIKNg7Ie1TXPgiP+rs3zu UKmlWmQE7bAntzN1hz1WvCfW2NJAUiXnLuugb6WPW84Z8YUQXviRmluccFuzfPUxhRksdL7eCTl Cny1HzmM/Ot8cgPx57Ugo0yoIegwOZyU/8Cg== X-Received: from wmlu21.prod.google.com ([2002:a05:600c:2115:b0:488:7c5a:b238]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4707:b0:488:bfc3:efc with SMTP id 5b1f17b1804b1-488fb6e8eb5mr393522635e9.0.1776957671595; Thu, 23 Apr 2026 08:21:11 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:36 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=8578; i=ardb@kernel.org; h=from:subject; bh=n4XiAYaFtIBUTDvCKEmsnxORvxDae62lgJVzXOe0/QA=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxckdRn8r9L9LN2ytNX68atpX434fC/3jjoqeX9+sX MD/Lyyuo5SFQYyLQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEzkqCvDX7nex3fFWyv9H/J/ O2eQ1DS3/Gv4z5y0tqvGjz/fytO5YsvwP3Wylanm2tevEv9r7TD5oThvYZelZZZMZ9T+1Tt+MBU 0cgIA X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-30-ardb+git@google.com> Subject: [PATCH v3 11/17] 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 , Dave Young , Gregory Price 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. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/efi.c | 18 ++++++++++++------ 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/memmap.c | 8 +++----- drivers/firmware/efi/riscv-runtime.c | 2 +- include/linux/efi.h | 8 ++++---- 8 files changed, 25 insertions(+), 21 deletions(-) diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index c0195b5eab21..edbf6efe3947 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 @@ -533,7 +533,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) @@ -555,6 +556,9 @@ static inline void *efi_map_next_entry_reverse(void *en= try) */ static void *efi_map_next_entry(void *entry) { + if (!efi.memmap.num_valid_entries) + return NULL; + if (efi_enabled(EFI_64BIT)) { /* * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE @@ -581,7 +585,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; @@ -712,13 +718,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 da7483a33d68..524e9f2ef276 100644 --- a/arch/x86/platform/efi/memmap.c +++ b/arch/x86/platform/efi/memmap.c @@ -90,7 +90,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 efb828b7e2ab..eb00130bcb66 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -410,7 +410,7 @@ void __init efi_unmap_boot_services(void) if (efi_enabled(EFI_DBG)) return; =20 - num_to_free =3D efi.memmap.nr_map; + num_to_free =3D efi.memmap.num_valid_entries; ranges_to_free =3D kmalloc_array(num_to_free, sizeof(ranges_to_free[0]), GFP_KERNEL); if (!ranges_to_free) { 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/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 72e76ec54641..a8406ca92332 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,9 @@ extern int efi_memattr_apply_permissions(struct mm_stru= ct *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) =20 /** * for_each_efi_memory_desc - iterate over descriptors in efi.memmap --=20 2.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 338B137CD50 for ; Thu, 23 Apr 2026 15:21: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=1776957675; cv=none; b=LDcjLfGpB27oDuf6eg4Ay2gev/GWtCEV3PWegBHscezuH+T/LQJHpzgtQxSlCu+DrLPNecAqUSeuustuWBoLT6sO/P9K3NgYSbDOo/3SaQ86XisbVMP7ka2S+aBhlIK9uikpWt2dXOnovSgwyihgtrO1SLZEeWKCwEKDQf81lHM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957675; c=relaxed/simple; bh=NFY6SCRdR50/UvbbI0XJ8aRN7kaq2SSzEgofs7VmHPU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iyQyIHX9E5uFAwsnTpqE4iEmOHpOJlrl1qZ3l6hP0BW+xveGc+DrnLXaP9l3KVMPvHlME/xedgDNcohClyy8Kab+V2ewPvg4s6veQj7//kOpjkaPrKtqrqOVbvuE7/3lJN4uN+trxnxO1nHyiLXOSByaeVsSr71MU1PEUqJSUvY= 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=FDDtE7sj; 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="FDDtE7sj" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-43d7b7bacddso4820789f8f.0 for ; Thu, 23 Apr 2026 08:21:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957673; x=1777562473; 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=9wk+GPNJ0EjdJDEaWn+TxDE0CUn7/Zc7fS8GRI/B9Ic=; b=FDDtE7sjoS4dkWxutsWDmCZYQ3DZcEVoqPFdQdaTeO46pVK4jGINW3axSsmaoClH6+ vJ2Aj2D0SZjfNMWhTDYwkkmLlwr4HWGGCE6SsOTx9QjRfLaJdMbKuIQ0Ybt4dQk/DE3E ZEBJy5omI00rQ/bGAnHQTV04JfC9OOTXytoS1U6/qNnommAQ0KOYIn5BUgzZKl+6+mmq Psj2VBRmrbWXGw/jBfQYezjrDpk1wm4EV62edIF7WuipsIH8EqtGXHWKziw+0T9TxqZc BCoI6Gy16no+mgke1d+cgjzs70zOradh7JHcgVQj0vhK2xNwphLz8EwU5aNV2dOYhku5 JSYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957673; x=1777562473; 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=9wk+GPNJ0EjdJDEaWn+TxDE0CUn7/Zc7fS8GRI/B9Ic=; b=OA3CSFqNhefBlyNklUViDlq/VkQc7vbX+MplIEt+Ea5rWcUEma8qlO5JXPzAlQJlD7 sq9UP5OikwAfFz29sP7lAwR1eLsP5AhV86F3XfJzKAEup9OWg2KICoj5pMBHrPvWIcjL zdklspXMunLP+9lWn4t01WuwbGLshjt0Q41z/Ay/phrwwUxD5Ze4b89SCYfaWKxCp1LL cbkTmZ3Dy3Jz8a/gGKyvwn3shx2Iu7dCoP4aXhZ/LGr5eucN8EbCWoE1LIR9h5iL03fY GKZ5G78JWfF0OYC2xt/YCu95PBuSIzFJKtRODLJIKJukvZjxOQMJf9BuBC+UX6F+YZUX f+dA== X-Gm-Message-State: AOJu0YzI/EzrfwjgoAI9gkw7I2VINRiT83phcoMeGNDtpjtWv2/XVW7a OPtxkO0WumzmvZud7FoObm74vTXHM5XY32cWHx2ijQpVCXUDzCKU48PN6oh5hLzjQC2SIFKwoWD Rv2z4pACI7YXjGEOURrHtq4CPu7TmhxF0lOM44abUHIvOB8um4H0fW1OiIQkHpA1WxrtUy8BYPa z0Xgc5DyeuYCtfy9qz49+oflunn/lKCbJaYA== X-Received: from wmbes10.prod.google.com ([2002:a05:600c:810a:b0:488:b96e:9939]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:a30b:b0:487:2671:fb8f with SMTP id 5b1f17b1804b1-488fb74dfe5mr270391755e9.8.1776957672489; Thu, 23 Apr 2026 08:21:12 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:37 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2415; i=ardb@kernel.org; h=from:subject; bh=UgW0djVOr4KqgQMmxvoDDuu3bEWRtuv/b3T+lF82Vmg=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxal1rFs7XVhd14qndS0+KBw3Va722Y7AM19yKpp0n R+duSTVUcrCIMbFICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACay4AYjw1lONyY7g28cCZen LyyNSJ/utd9eavN/ywWv11veNXEJ7GNkmFptoMe+Wb9BKEDwfTn7HM2Ob8YHl585zOJzvZ+//n4 lCwA= X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-31-ardb+git@google.com> Subject: [PATCH v3 12/17] 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 , Dave Young , Gregory Price 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 edbf6efe3947..459b5c13167a 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.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 4AA9137D135 for ; Thu, 23 Apr 2026 15:21:15 +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=1776957676; cv=none; b=phIlKsx4x+GCj/poDLvNFxQLMmkWURCD4oRFfEVtenzBXIIO68RE8yNf8CPiHJP86pA3Hyn0qdrcs/b7R8NKER565iFbH9QtlMi/sM6AVvJNERJodVAUs32yhp4Op3z79YEPabcZkd1ue8TmQ1AqKQEh2vbe0yVLwnSuW3A0+WM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957676; c=relaxed/simple; bh=Eqd18fIs66CS6j98MlJeICalwSHrv5VVObvH1lqDgtI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nZSlX2K16tpjFJLJTuVXHvTbDwIGCRuUZHpi7/umGdBK0JIMFrH8nYAMZXvllKR3+rRA/aFk6S9JgKnyYyJEV66chuOjUr/f8NtdvixW/nnbSZWuE2VdYE6d49HxvUq3N+eF2aISm5lrVUTf1ilJpGzO05mdlVVTsfLNKHXhH9c= 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=QHmp4PEg; 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="QHmp4PEg" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-48a5fb71a84so5623145e9.1 for ; Thu, 23 Apr 2026 08:21:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957674; x=1777562474; 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=deBMwt4GdP8bouiDOJACDn+Qi42vPHSoLuOshlsff+o=; b=QHmp4PEgKXtpnfV1+0s8B+A800yo07cYOTRZqQ1HadgaAqRtt2fpvCYxM65TxNQuQx LrXhmIe5lN6DxyEAd3P00BHjDqy5HU7MX8pL+98QIcLDtNfW0X6e6AIK2Omge6gE7nwc ZO99jLdPeh2xTEjmUdNPfC9hXcWa2X0OGOpvUHiOIfSaJqOflR1o6F9lja/EnD/jY0Tp VDzBsqXBBjMoB0X7hPPcoaR/CUCErCcZMnAn/YCNuoMbXxzRC3+IotyhtO0n+1xy2ZpE aDGzk70wCxu9JesduJjfbm+iWM9ir6aLE+GHiQtV4D0oXdpHY0n3kSOSuuZJq8l0+4rr pe/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957674; x=1777562474; 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=deBMwt4GdP8bouiDOJACDn+Qi42vPHSoLuOshlsff+o=; b=ElEx6OtOATyVGyEF61LuAqfvGg8ngW4J8okHBx2adwkIQKdBjXwz6gwGWWlvYoPqKI pVrm+CU/jLz1N9eH9VTYjVfDRBiWjoaYumdEmxfc/SNuZQv0q6ukoSvzn+ADxcRnlnQH x61yyoRQmS6PLuAaU/inIPVKRolFTabW7rLTE0W62uf2tOKPjQeMMiHQ5nu0rjgMBcqm qDORQDAf7A7U8tI07KSCctiw3SOMrLmb4+dy9C/bATrVAvzHsyPTFIMk4uBWRNhezSFF AZfgTNppnfh5o7L6wTxuOPWlacSqFttgYPD8qeFiSL9BElijWD35tR4cq3NhrQJnrez7 EErA== X-Gm-Message-State: AOJu0Yw8K1tVyCouza7lR6HQ+PsoAgTJPgBaNyMs7lsFVotREUjij7x/ SyW8ssYUW+w0hLUmuEPiYbWHaITlw3zyVyXqSJgrXeDehlil1hCZcJL6iA8RP5uxHXjAhuGouG1 JnLw908/VKs3SP4GocItwc5EGS68Pw0Cw+rgp2Y+kdnttKJF3fR2+NajnVSMnF17cgAHh1zXkH4 c51kxEzGCYq4phGjAaMpB21KF7CqfLvbaqeg== X-Received: from wmbjq20.prod.google.com ([2002:a05:600c:55d4:b0:487:1fc4:8071]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8b6b:b0:485:3c2e:60d5 with SMTP id 5b1f17b1804b1-488fb8859ddmr362834385e9.2.1776957673474; Thu, 23 Apr 2026 08:21:13 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:38 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5135; i=ardb@kernel.org; h=from:subject; bh=AaX0BrB4ny6TQQx+jQeJOTVDUbOld0PQ8UIy7f/AVTo=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxWkLudl8iYwFN/qirmx3VD23+NG0yZJrnoXylCT1B oZ5PTbtKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABP5L8vI0NDi7l/w9+fujVfr w2fcNv4u6rBPvbtblP1YkkDacWU3N4Z/2vMvlgZ02ZxXOHt0hkXxq9tF0oXbXXo9/1/t3/ZcLfg bFwA= X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-32-ardb+git@google.com> Subject: [PATCH v3 13/17] 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 , Dave Young , Gregory Price 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 | 82 ++++---------------- 1 file changed, 16 insertions(+), 66 deletions(-) diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index 459b5c13167a..f67ea6d4cad0 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 @@ -589,7 +568,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). @@ -642,40 +621,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; - - desc_size =3D efi.memmap.desc_size; - - p =3D NULL; - while ((p =3D efi_map_next_entry(p))) { - md =3D p; + efi_memory_desc_t *md =3D NULL; =20 - 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) @@ -752,9 +705,8 @@ 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()) { @@ -762,15 +714,6 @@ static void __init __efi_enter_virtual_mode(void) goto err; } =20 - 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 @@ -778,22 +721,29 @@ static void __init __efi_enter_virtual_mode(void) */ efi_memmap_unmap(); =20 - if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) { + 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)) + size =3D efi.memmap.desc_size * efi.memmap.num_valid_entries; + 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.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 AE3D63659F9 for ; Thu, 23 Apr 2026 15:21:16 +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=1776957678; cv=none; b=BBegEYNr7XjAYI0dduRvlsmiSNfzEFpWFMr95DkNhov7hRzHZHS9AICHMncj5UAbBGZQtI/HrAtLpmMJ2IHpg/cVdGKvA2z20/2Fo2aMkq/CAy7pzMOKuGm+75LRP+3LQZ9av5OyaBPsyrTCYPAPvFwz2NYRm1k2DsOlGuru+/8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957678; c=relaxed/simple; bh=6UTg2nGC4iZqxbau1FIEpRP2Pc8beHmLnbPtrGexZpM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FXn4Wk/Dy1pR8LWYcL2TI4PajnytaoknnsTflc4ukoYJUxr6dZqtd52WqbH9JNVGCGUwaQx/gDlGwizN3Xw5AGhmVhgUQk/6wKnF6L5CZ8J8pIP+10xlkPMVUWf2iLnFgM2Hn6asXtVCAUxBtBg+zTHmfG1AI9lJXFKM54wXpqQ= 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=bEb5H1vK; 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="bEb5H1vK" Received: by mail-ed1-f74.google.com with SMTP id 4fb4d7f45d1cf-6765040703bso4081962a12.0 for ; Thu, 23 Apr 2026 08:21:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957675; x=1777562475; 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=SuIowFjMZ4cjeA3ecCqoefzK6wmJDnxO5W9+odyl7wM=; b=bEb5H1vKIHLcCtO0/gceqDtb7czfdxT5RPKVro/mehdrumC1ecCHnODsTvydUsg9nV EtE1yW9msgweTtTSGFuqTBHwLEMRBrvWixaLFv0Y0o9a8PnoAQlWp6w7jr6Yi1cj9qZh lKzu0plclgTzgzN3KZygnA6k+StFV3J7hFHRVTOGmMP40zjuM93zP1MSy78JKvyQQ0Sy 4o6AlrcI/k0rP+G5/00b9zcIWPlwv3Be4KVlnwXegLSnK279lMEQD+KQKMNkRSlBbvFy mP3NYwXvsDt2xG08UqrbZ6XsIL7KI7T3effWcFR3tq6F3lXh/05J/cIhQHFOqUEpY1Rc 5EZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957675; x=1777562475; 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=SuIowFjMZ4cjeA3ecCqoefzK6wmJDnxO5W9+odyl7wM=; b=J0+s0WmXSUD7tBh0NJupOPONxmT/bgq0Ct3CcWngMRSHarQS7gvgitY8+9CSDfWHu8 v/wOEzWSW3dv4Jmdor+LS0XEpJV2JwPhPw+3IsX8tsTqOcVygM4Ycf+6dWzPcKfcfCD4 1x16IMYqWPEQ/ooQ2SYwmLRqnnXXcfJEijJWsadCDl6B5MsgGa6kBYzyqs3Yun+hniOt aLqd+414B1H2s8q9erdNvElOjp9Navuseg2Jei8P2JrBcVXS2rS9ib2XeqN9bhZJkFfM w20s7Xo2PIdgvErn3EatWcnNw2fctIns5PR55rq+5RrqHTYujPWROClYzg7zvhxqiIjK 1DHA== X-Gm-Message-State: AOJu0YwNCFPkVHpqJUmTs/ESiLxNQhV+a2WBiK3qNdcMx+aoG5GYygLk G6ZJhsIrFJE8o1Jg7U7pi7hbfTsEWeM+NzN1e3pyk69C3DfcYLkcB+MLjVBRrBPRrSVrBJn/P+X G32FLn3k9dzQEmJpKvMGm5mhA0hakf9kAD5Ouj8A2p70lk/c3JanSFVE6+M7vGTaVvkj7cPpPSI KH9xLcWDNC2LTO+glTKKpzxvYC8fP1y6E+dQ== X-Received: from edcz15.prod.google.com ([2002:a05:6402:35cf:b0:678:93c2:af38]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6402:90b:b0:66e:41a9:1198 with SMTP id 4fb4d7f45d1cf-672bfdd9dc0mr12404769a12.22.1776957674861; Thu, 23 Apr 2026 08:21:14 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:39 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=8508; i=ardb@kernel.org; h=from:subject; bh=mY2wIGuTZxHT+BE9hZpFmkeVJsQQEn8VQ5qRxln43xk=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxdlzknN2/Gq/FSio+zA8uPN8mfcNxvbL2itP+c85y Oi2+t6zjlIWBjEuBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCRoz0Mf0VVtU8azwg54/r0 sA9bg8ckjjuTs3fl5i5YJV7N1ddYs4aRYfO+os9LHx5U79k/qZ7L4otijLpu2t1anz8q6bzMsVO Z2QA= X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-33-ardb+git@google.com> Subject: [PATCH v3 14/17] 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 , Dave Young , Gregory Price 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 | 109 -------------------- arch/x86/platform/efi/quirks.c | 35 +------ include/linux/efi.h | 5 +- 5 files changed, 7 insertions(+), 149 deletions(-) diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index 7d8f627805df..0de92193764b 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); - enum efi_secureboot_mode __x86_efi_boot_mode(void); =20 #define arch_efi_boot_mode __x86_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 524e9f2ef276..000000000000 --- a/arch/x86/platform/efi/memmap.c +++ /dev/null @@ -1,109 +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) { - 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 eb00130bcb66..98fdc286eb40 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -401,10 +401,8 @@ static int __init efi_add_range_to_free(u64 range_star= t, u64 range_end, =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, *new_md; + void *new_md; =20 /* Keep all regions for /sys/kernel/debug/efi */ if (efi_enabled(EFI_DBG)) @@ -425,7 +423,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 @@ -438,7 +435,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 @@ -452,23 +448,6 @@ void __init efi_unmap_boot_services(void) pr_err("Failed to reallocate storage for freeable EFI regions\n"); return; } - - if (has_reservations) - num_entries++; - } - - 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; } =20 /* @@ -476,7 +455,7 @@ void __init efi_unmap_boot_services(void) * 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 || @@ -486,16 +465,12 @@ void __init efi_unmap_boot_services(void) continue; } =20 - memcpy(new_md, md, efi.memmap.desc_size); + if (new_md !=3D md) + memcpy(new_md, md, efi.memmap.desc_size); 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 int __init efi_free_boot_services(void) diff --git a/include/linux/efi.h b/include/linux/efi.h index a8406ca92332..5986e565a249 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.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 DD6BF3803C2 for ; Thu, 23 Apr 2026 15:21:17 +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=1776957679; cv=none; b=TujWZqcp+w9wrZ/Mi1gKZBA/AzcfOLLB8Xa2cP4uDnrayoWQnhGfw4fkX7lXCLM7/qrXhDh0n8sY95r6TJOPlbHIcE8reE69OAWtvkNncmvuP4gqwyEC/LQala7QWYPlo/YWGo+dhDM2oU/QpQGDVD3u+wc37V2WSvgwwcwB55s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957679; c=relaxed/simple; bh=BCHBdFu7uj0WMDlyV3LNlEBXBdnZDeTgsZHS9yOthWw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=vCKSLyItaWSZZFV/lrpt93X+ekvSGwXTdKvikDdnGtTpXjXa1/pCIvkjkLxI0WXC2cK3PYMFoHSmzPAkSNmwrco5X6GiBYLqKeIk5ONDScMokpzbzLwukEUlrDmbYVgvHhZz+hQq95G2BuMBUNdHnDmP3Xy+Cp2V0CwkwGzvaS8= 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=wdXLftys; 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="wdXLftys" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-440d0c4401aso4202335f8f.0 for ; Thu, 23 Apr 2026 08:21:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957676; x=1777562476; 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=fua20tDRknPI0AcOnbhBXrprPjr6FkRST/bHlhqglgk=; b=wdXLftysjiwWCV7cIpnoYuWgFBfCFynyUF5gSciVFlsD24Igmd2KS0j6+NB3Oi5qfm MsJcOKHNaBEsP+A1MPQDEd9Y8uya3cZAVuJBQrBAhF9sTDxChZZHGz+sOFnth5hr/NMu b0HZ3KOy2B6+kg1OeofAn5Hd+YMXaBAW2vvBgD/824PKcZQGP4A3g6azu+B6ff3RlUuB l8osIg0Xwlch2hUYeT2LP6+ppnDWRPJTIM6LMEP3b2uzmKUnP3piFfO5bEITUKVMaVV8 e6fqJd/aQyapBHX3ULW2lzc6Rcn4SIMEeJbG0VSEUguDSYHWsqk60H07AzkM4tbJEiKB Nswg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957676; x=1777562476; 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=fua20tDRknPI0AcOnbhBXrprPjr6FkRST/bHlhqglgk=; b=g/P4odkMKDFD5jhRhfrhrFJUoJSkk4Etk1kA2PJSx+c8VUTMd3nTQS5FDdgUvht37t Qc4QQyOmrbTtB13CdJUhD7Ug+9/CVRpAi4GlmXzpvwoFi94xqF4/b2kvOU6YtllJri4L z0LD87+FzN/cF/3JkfM7pcGeLuttq/khanSMRZ3LFxbdMn6ej3kdIYVmlHTO6zLJ9Zol YX57AFxJxCCzSIgmtetztWKczNPePw5YrCRzFh4n3IJVP7ikMOl6YU6WUFJVzwucnu8b LCjxH/07IiUx98w83dY8O9Z9jLCzmoFiNqOg0P4pimEXPAXtftMTPf+wxfvBctaHYdCv 0icA== X-Gm-Message-State: AOJu0YwuazngtitznF73ymeEzXLYMrI65VFbc0+dzbeq1A7b90HNdPCA PJCgBRTKuoagN7Eg/TAwcTBOWBjRHqUXCFcNWEegvYAisLKRZQaFGxZM1aAhsNO7YgxHhOCF10F 9mHN7WbN6ZeMvqMSuLapl9c1d0LkSvvXLpf4GPb+nhAGiBmRMCMNM1IoJNXoHF/tnbhEkE3BGu3 mJFEGLC96Qm1fRREuY3+BUQ1xVul1lKlMcvQ== X-Received: from wrsn16.prod.google.com ([2002:a5d:4850:0:b0:43e:ade7:8d95]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:4283:b0:43d:73ff:fd59 with SMTP id ffacd0b85a97d-43fe3db9bc9mr42465799f8f.10.1776957676034; Thu, 23 Apr 2026 08:21:16 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:40 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=3599; i=ardb@kernel.org; h=from:subject; bh=7zfXJlzwkp063m58UDmY7Gt1EcrqwR4mQhLXAixBhyI=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxblQU1Uenk3mzE9fpL6b9KO5rile8u3uezPuHsnXq Z3B8up4RykLgxgXg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjILQ1GhkUz3RZLnHjf72u2 3OHjy4tpYqxpM890/mvQEne5I2uyR4eR4cj+Kc8+WmbF65r/kXHc9jG39dLx1K6JsVGfrgWHsNb r8AEA X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-34-ardb+git@google.com> Subject: [PATCH v3 15/17] x86/efi: Merge two traversals of the memory map 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 , Dave Young , Gregory Price Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Combine two traversals of the EFI memory map, one that instantiates ranges_to_free array elements, and one that prunes EFI memory map entries that do not need to be preserved. This will make it easier to determine whether or not a EFI boot services region was freed in its entirety, and this informs the decision whether an entry needs to be preserved in the EFI runtime map. This will allow the distinction between early reservations of EFI boot services memory (marked with the EFI_MEMORY_RUNTIME attribute) and late ones (marked using the MEMBLOCK_RSRV_KERN attribute) to be dropped in a subsequent patch. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 58 +++++++++----------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 98fdc286eb40..b7c8337d8f88 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -416,13 +416,23 @@ 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; bool has_reservations =3D false; =20 + /* + * Build a new EFI memmap that excludes any boot services data + * regions that do not cover any reserved areas, since those + * regions are being freed. + */ + if (new_md !=3D md) + memcpy(new_md, md, efi.memmap.desc_size); + if (md->type !=3D EFI_BOOT_SERVICES_CODE && md->type !=3D EFI_BOOT_SERVICES_DATA) { + new_md +=3D efi.memmap.desc_size; continue; } =20 @@ -433,40 +443,26 @@ 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) { - 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. - */ - if (efi_add_range_to_free(start, start + size, &has_reservations)) { - pr_err("Failed to reallocate storage for freeable EFI regions\n"); - return; - } - } + if (!(md->attribute & EFI_MEMORY_RUNTIME)) { + /* + * 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. + */ + if (efi_add_range_to_free(start, start + size, &has_reservations)) { + pr_err("Failed to reallocate storage for freeable EFI regions\n"); + clear_bit(EFI_MEMMAP, &efi.flags); + return; + } + + /* Continue without advancing new_md so this region is omitted */ + if (!has_reservations) + continue; =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) && - can_free_region(md->phys_addr, - md->num_pages << EFI_PAGE_SHIFT)) { - continue; } =20 - if (new_md !=3D md) - memcpy(new_md, md, efi.memmap.desc_size); + /* Advance new_md so this region is preserved in the EFI memory map */ new_md +=3D efi.memmap.desc_size; } =20 --=20 2.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 D0C83382288 for ; Thu, 23 Apr 2026 15:21:18 +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=1776957680; cv=none; b=Pq932hETJlqsxN4P7xv3mHZvEc+eGAvXlCZHh2hmUAcH312lfmRt922twqVCqdgA9oGCUy+W9OrKXFOFP3DPsmqqkv5UwecRAHPQjbtFMIq3tOU36+D6qorg8zOBI3qVxj5hhLcq1RtF7bTqBybCPxI7da9HvMohe9Gg+Gx1xxU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957680; c=relaxed/simple; bh=/FrkEXbR6BKCZAc2NMh4D/rpko7QrXBn+oyA+ZQFggY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YLKz59l77b2nR6M3CTUr2iun0XNccp3Msrkhx8u88v3ot6J5S2l9WQ+SzdKsPM3KhPKN5ZzXFtptRpfcedBcEPjy+CRTmvAQAfSFkByPzMxJAcpzu7FwCmE4DYOIDqAfQmHtnaPkHrJYtWI5aeSmzR5lJepaTlkEujrE5TkFC2E= 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=kTkvAk1V; 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="kTkvAk1V" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-48a55ecc249so24416515e9.1 for ; Thu, 23 Apr 2026 08:21:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957677; x=1777562477; 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=CMx9vXyaaNF/GCKNIFsaZ8/8V2fmYY75CqyB0473Pr8=; b=kTkvAk1V67gtzYIeHBvBbHCj4HO9PwPpelnHtdXhs0eZF2+UQGYBG5R3yJ2bqN+5/b fxFtlSyRi1VWWBFIEPpVDYgzfp7vTJdUfubPZJRE1eNSPsw5re/K6mUdn9SRy7195T95 MGm/lnTes4kSF5xmgocDzK9MChEbOhMHHk87x82turmTK/g8cPXwwaKM8fkfmxqZ+OPj bhKqcg0jjSCtWFHCuh+FL6a5AeNrjeOal+uZqOegqWyl8DyF8u+7aReUi2gwXAdi7ZD2 e3NWQ10zn1NVGVcGkOerqcIduoc/82px0hpEgFFbDHdAbupCYkTNrn7LQSu6+iqMTNAo M5kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957677; x=1777562477; 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=CMx9vXyaaNF/GCKNIFsaZ8/8V2fmYY75CqyB0473Pr8=; b=kSOlFFwBFpUuT72EkKL+8/N4+ggDRBiH4kHiyAWl7qx+DbEYUVa/9XSqHaN2MHAeKg bUVeC0KIxChEo4xnRhQk0+oBF3I7DwshwXR7lSKse39UAWJhb6dy9onRZ74eoaeZSQ01 Z/A1YrZN1QlsVsRPu67W+8Q0OwxX9JwuP+swGFHsHGXJp7G9HaY/Dak/1dt3oV6ZhWII JXp/daUc5RyYcl/NbWEA6nOAsfrPnQvfbbexyPUPlYba93W45dG5fRfjo545CsCbkTOv qG5bGPCPSVqB1Dx/e9zuA45/EjTURYH8M9VntCepPgMnyBSC37lZWg8mw30l9Qmw04VZ Rw0w== X-Gm-Message-State: AOJu0YzSgUX43vfOEtOD9/rnvnKlP2Fg9TVIHcI5U07nRTsOjWk7dk+c GpafwAnf7fN7lXtloNSZNOWKkdES26slUjuKPdSHUz1yBBoek3MX5lAdi3Po/4iknka4h7X0Xyq Cm3YvslxN2XR828kxEqHzSh6rrHBT+l9alxmjoRWU/cGhpXu9szQ327ZXwDR+aJiIVVZ9s5K72s BhpltwucXgg7iq7qmV8pgo+FhDzcexUnNBbA== X-Received: from wrob7.prod.google.com ([2002:adf:ee87:0:b0:43d:7d01:5235]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8588:b0:489:2005:b36e with SMTP id 5b1f17b1804b1-4892005b4e9mr157083885e9.19.1776957677108; Thu, 23 Apr 2026 08:21:17 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:41 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=4160; i=ardb@kernel.org; h=from:subject; bh=5pSlsIgmqgTBkw/HCh3I/VbXVQ1Fg4Uy0UDAtPG9sW0=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxQUmmwn/f0w/cHrWgvfrfkxrrt0TvdbJr7E17cxsA edzDw5LdpSyMIhxMciKKbIIzP77bufpiVK1zrNkYeawMoEMYeDiFICJ7D/ByPDFf2uCulFkR98z Bas9G7a9XTVTaUFCTCDbTMV5x663epUwMlx4cklu8mTGsKxQ5eifK8zPSUttU2CuzGM68muRD+s 1eT4A X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-35-ardb+git@google.com> Subject: [PATCH v3 16/17] x86/efi: Avoid EFI_MEMORY_RUNTIME for early EFI boot memory reservations 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 , Dave Young , Gregory Price Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Currently, memblock reservations of EFI boot services memory made before all EFI boot services memory is temporarily reserved are upgraded, by being marked with the EFI_MEMORY_RUNTIME bit, and this results in the entire region to remain reserved permanently, regardless of the size of the original memblock reservation that triggered this. This is a hack, and may be quite inefficient in cases where the firmware does a good job of merging memory map entries. So instead, rely on the MEMBLOCK_RSRV_KERN flag, by marking existing memblock reservations with this flag before creating the new, temporary ones with the flag cleared. This unifies the treatment of early vs late memblock reservations inside EFI boot services memory, and avoids clobbering the EFI memory map. Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 66 ++++++-------------- 1 file changed, 18 insertions(+), 48 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index b7c8337d8f88..fc6a15c2ace6 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -273,7 +273,6 @@ void __init efi_reserve_boot_services(void) 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 <=3D start) continue; @@ -282,37 +281,11 @@ void __init efi_reserve_boot_services(void) md->type !=3D EFI_BOOT_SERVICES_DATA) continue; =20 - already_reserved =3D memblock_is_region_reserved(start, size); + /* upgrade existing reservations to MEMBLOCK_RSRV_KERN */ + if (memblock_is_region_reserved(start, size)) + memblock_reserved_mark_kern(start, size); =20 - /* - * 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 that somebody else has already reserved. - */ - if (!already_reserved) { - memblock_reserve(start, size); - - /* - * If we are the first to reserve the region, no - * one else cares about it. We own it and can - * free it later. - */ - if (can_free_region(start, size)) - continue; - } - - /* - * 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_phys_free(). - */ - md->attribute |=3D EFI_MEMORY_RUNTIME; + memblock_reserve(start, size); } } =20 @@ -443,25 +416,22 @@ void __init efi_unmap_boot_services(void) */ efi_unmap_pages(md); =20 - if (!(md->attribute & EFI_MEMORY_RUNTIME)) { - /* - * 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. - */ - if (efi_add_range_to_free(start, start + size, &has_reservations)) { - pr_err("Failed to reallocate storage for freeable EFI regions\n"); - clear_bit(EFI_MEMMAP, &efi.flags); - return; - } - - /* Continue without advancing new_md so this region is omitted */ - if (!has_reservations) - 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. + */ + if (efi_add_range_to_free(start, start + size, &has_reservations)) { + pr_err("Failed to reallocate storage for freeable EFI regions\n"); + clear_bit(EFI_MEMMAP, &efi.flags); + return; } =20 + /* Continue without advancing new_md so this region is omitted */ + if (!has_reservations) + continue; + /* Advance new_md so this region is preserved in the EFI memory map */ new_md +=3D efi.memmap.desc_size; } --=20 2.54.0.rc2.544.gc7ae2d5bb8-goog From nobody Wed Jun 10 09:22:26 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 F3811383C94 for ; Thu, 23 Apr 2026 15:21:19 +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=1776957681; cv=none; b=sfUinkFmk1RpKgI5glxxyJP/k6SeFVPClwyZEkoumEzh71MpicWuOtU9821MXfNUFnX0ivvmnSn60GpOElM98O86vfMA+dzPVKPkT+y95IfB3VNw8YNjc0MAdCz8Bb6lXcjplr6TpdPcPjzwzQiKOfGVUM4XeD2RdNbAQHVVDME= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776957681; c=relaxed/simple; bh=kHWXmbw7E3NC74c6976UkwqhW+N95Bw48E7zKD2zH+M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CRBJhij0c9q9Mnz3+znzZk8bXreBib3EbZdzHNWeVkZiLFVA9C+0tIMg0rsj2tDMUoPwKqwOn8D3oeoR8a1txcOaB/v86lDzjTeMKTxgTl6NVVovRqrHvqWcZ/PT0PLxp7+Mtu1tmgBbjlGPxRUAA0XhmwlXPxXCYXvw+njR8n4= 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=NuJCq6tp; 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="NuJCq6tp" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-488ba2f4094so39229805e9.1 for ; Thu, 23 Apr 2026 08:21:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776957678; x=1777562478; 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=Ozc/YFu4zY+6dm6Y0MVQ/zDDn7vwY45tnWdyVpLDfCM=; b=NuJCq6tp9e/0m5RtOMDIdyaVhp6iRyuxV8/Lt45Tu8dUTX/n1sxX537XhufzI+tgHA JolX8QL9Y72dw3h6lhWpfD4sMnryVzIcPb+uj5CaUeIzuFkVKmPIasKhitcQRAutjJx4 XQ27hdy1ah451RX/lYxoaeHLPLy5/mlCfNNZMalTRjTXt1wQti+AIuppy6muqsCucGSl VfLzqIRg7UkmlUQyqKsI9zcFrLv0fQTX/WWXGk8s7YJF2e7e4KA6dWXOSLlpD5i9iyW2 yT34JuY4V5ImF8HbThCArvizEF6WhQW2tbHW9D4ge4YJMkoVhkCmDhFBrQw4xrpYOlP3 L5gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776957678; x=1777562478; 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=Ozc/YFu4zY+6dm6Y0MVQ/zDDn7vwY45tnWdyVpLDfCM=; b=lz6SH/dENFFpJnpTEaLcCeMEqNHGz3ZxE2YrQ2HIkxrqwdfFykupPKyiX399CXRE5z kFAczZsTm4ZYKxrMd5HdAUJqsGoLOESYGN+SyEF9xoGqWeUCehMa7mwBWU3XEV1pEgqh UpYyNXWyfAcCWLjPByXY6oC/4YEhPL5H/MmpcpmuIwdmIrq+badZRollqAXP3jga42om ywTOj8sVr5yX3LdmQYA3BEdL7257gBd6AuXOlnMIQTmj0WmStiBxcRbchbR1O+EwYbd0 Sm+iXsFxCfJFS7LuapRl1bbMgA5+0bqlstItyfhNWovw1XBs6IgX+oy7HBDO3/8MWlv2 Q97A== X-Gm-Message-State: AOJu0YxwbVFYC58IdmO942dTrYQlGGaM408Ce4WmjpFay/2P3U1HrkZR W50PH/1zP4lJ5+ddmZfjYgmvWfv70o/Yzp2xIhOQCGIvw/QRtcptDK4uvQ3RBVcRaeG+syui5bM fLiykiIIOz0Brd+CM58F4FHZ16r0fPUemiDvejbX5P4RoY3zc/WYpsKndSdFC2B2qXwcfrudGHJ ZZi4oOrUq8DFgI+X6TsWJSvV/8unbm+7F3AA== X-Received: from wmim14.prod.google.com ([2002:a7b:cb8e:0:b0:485:4f4a:bd84]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1389:b0:485:9a50:338d with SMTP id 5b1f17b1804b1-488fb739d3amr369175715e9.3.1776957678057; Thu, 23 Apr 2026 08:21:18 -0700 (PDT) Date: Thu, 23 Apr 2026 17:20:42 +0200 In-Reply-To: <20260423152024.1098465-19-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: <20260423152024.1098465-19-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1264; i=ardb@kernel.org; h=from:subject; bh=4kXARMQ9wUsH59E3D6X32IJFsIA2YMKbNS4DiGE+Qis=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIfOVxcVPMbXVL7m23fAPD/CcxF4m9NNWeiWvb+smlZrJy vvvT9PoKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABMJ9mJk+HGiY/W3dL1nO97J S5lHv7ymPdfNeteKX3NmzNtVJco+9xXDf9eLpzY7vQg78qyZQ1ng2C1BjYhpTi+U4ia81Zm9zuL HO2YA X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423152024.1098465-36-ardb+git@google.com> Subject: [PATCH v3 17/17] x86/efi: Drop kexec quirk for the EFI memory attributes table 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 , Dave Young , Gregory Price Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Now that the EFI memory attributes table is preserved properly, and the quirk to detect corrupted tables has been updated not to result in false positives when the number of EFI memory map entries is low compared to the number of EFI memory attributes table entries, there is no longer a need to ignore the latter when doing a kexec boot. So drop the workaround. This reverts commit 64b45dd46e15 ("x86/efi: skip memattr table on kexec boot") Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/quirks.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index fc6a15c2ace6..92d37f2a5cbe 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -522,10 +522,6 @@ int __init efi_reuse_config(u64 tables, int nr_tables) if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) ((efi_config_table_64_t *)p)->table =3D data->smbios; =20 - /* Do not bother to play with mem attr table across kexec */ - if (!efi_guidcmp(guid, EFI_MEMORY_ATTRIBUTES_TABLE_GUID)) - ((efi_config_table_64_t *)p)->table =3D EFI_INVALID_TABLE_ADDR; - p +=3D sz; } early_memunmap(tablep, nr_tables * sz); --=20 2.54.0.rc2.544.gc7ae2d5bb8-goog