Received: by 2002:ab2:784b:0:b0:1fd:adc2:8405 with SMTP id m11csp387291lqp; Mon, 10 Jun 2024 07:11:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUqKFZu77dkPOYW9w0rMGsG/+DM7IBurD68Nu2o35Nq4rgkrIDfrwp6EF6QX9Wpibz38z/JpxTRBYT8NkSGnudoF3TXd+z7HFou7MGGrQ== X-Google-Smtp-Source: AGHT+IEFHPsrZb0GfUltr8WAnNbaxIUMf1nggapW5cYRbJ4Y0c/aWr0RdJ7VaPkneLy1cR7qqIhO X-Received: by 2002:a05:6808:15a0:b0:3d2:2877:5147 with SMTP id 5614622812f47-3d2287753b4mr3544709b6e.43.1718028672789; Mon, 10 Jun 2024 07:11:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718028672; cv=pass; d=google.com; s=arc-20160816; b=gDH0iFyn0y60dGKSgIHm60ZRamvxnVTHiVEy9EN+UXs2a4T+rkPPu0THYJhP4glv+5 Eur4Qtb3iRAQMFqraySAys/qEFdY2MR81Fi1QcXS8Cf2GUDNcnohfv73xF38KUylV55v ahuh1msXLWV9XgxlHbBx9OaZ+iaaFUXxkpDzfkcb8yBxK6ex28KUQzxu4K8vgUyAFFfG 3sTrIeDcJJ7IJESDE/+ep0nnWc6n6OvZNaxO6pyWk/1cCRpNhP7jY49s7mJanyyIlD3u 82e1CLG+NY9GjsBXhvQyikp6fFqvtVpifIsPlGdoat9byhHj1xHPqjgn20Z7wKf5yW+7 DCkw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature; bh=dgenhHanHHj3lnmpj2N/ZQ5FyHhx2VF3uOYFtsXU2e4=; fh=VCaguOrF4Bz5iOf4BdNHJ99inNsIE7y+0vtXkqmwDts=; b=yB8dkJ5bKNvy17w/Nyr5k3PCCgjEhitkAyQf7bPo3rqa91juJMu3eokvTjnwa0ngOb UCLIwYHnIG3q1BRFmmAqoXZZtuCDgAbmkiumpCiDYTVvuVEKZ3gHJak02pMLS93cF0ac tCOwsE9mhNL/e99DhDGeUdsOV+c4Q/ZdrrYRhsBsi5TOvgPIXZxvYAE/+a1z3VAgi/kS B3mKPMv1j7rU7w/bjSthw9aFKX9K+LYzWvHK4QjWhxHQNqqGEaJ3eKOMrjOoPC1AiMGV mt/P5+0CnrP3fGrFmEPGp2MF3u2JpPe14I0PW2Q88VT8i0MFNiFS3SwR4bpUaM9IjtXW rw0w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=B4hHeUqQ; arc=pass (i=1 spf=pass spfdomain=flex--ardb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-208338-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208338-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id a1e0cc1a2514c-80bba1efd01si246106241.35.2024.06.10.07.11.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 07:11:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-208338-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=B4hHeUqQ; arc=pass (i=1 spf=pass spfdomain=flex--ardb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-208338-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208338-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E8FEC1C21E3C for ; Mon, 10 Jun 2024 14:10:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 38549130ADA; Mon, 10 Jun 2024 14:09:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="B4hHeUqQ" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 D110D12FB26 for ; Mon, 10 Jun 2024 14:09:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718028584; cv=none; b=mpWxtk9qDN1oRbYampbXkOC1Wgewa1wKpHKp56CuXsg92oT/ixjmEN46xHdCn2YVQUvQRxMoKr8kfyXi2CcxF7o1SjQA5/z0Btgw/ATLYWR39utfn/RZao9em3sVKjW/55KtsHxQukliBmMDHRkeJESI4i02KDQFgjTMKcuKJzU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718028584; c=relaxed/simple; bh=4aaAgEJ4no9Xmp4mAov9FrzUsh19wG6FNUPI7ez7R6M=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=lEdq7k1W7+IC0sxN+PITsDYCreuABu1Mo84eQymMudi1trLJ6TF/Kr34qLOjt35hOBbTI1RqZYHtpu4WAb6ZSkyVdIgJjB5XC+427ujbmFcxFnXNL9tILemFLeykxdfambdZp4CFei+6CM1d7Rdw1vDF5uEVv2MLvvc54g1ofo8= 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=B4hHeUqQ; arc=none smtp.client-ip=209.85.219.201 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 Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dfab38b7f6bso7036817276.0 for ; Mon, 10 Jun 2024 07:09:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718028582; x=1718633382; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=dgenhHanHHj3lnmpj2N/ZQ5FyHhx2VF3uOYFtsXU2e4=; b=B4hHeUqQTdR9/6sc+/8ej7P3vjULmwRZVzp6Mmi2/PKlcTT2FMwIEglnJzb+y1+7Ig FggeewVtvTqSGj/eVHdNJdN3IRhId543Xj12DVjEfGjCxZLF/NwoaAtLewtHY6pQcPXJ JbSeQSr5xef5bwSL5gIxrHu4VayrJEIqgMJn5vaCx0KUec3mbamJk7hL0xpJ0l7zVDmL +aEa9mjt6YAoUn7GjB6Ls/SLg8rPH1/1XCQS/EbmGps901El864m2DvOm7Y5PcquneuH RNTfQfNKZWkvlExtBgH9tGT7/4fj2V41RgtnSxlN7iMdDSowmks9dAKpR/TH8P80x8LG j9Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718028582; x=1718633382; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=dgenhHanHHj3lnmpj2N/ZQ5FyHhx2VF3uOYFtsXU2e4=; b=gomsDdFq7o2u/BY9Hgntc88rWxSqt/qo1Gx++BRwXH+XXNcPeov9eQJBPxwV4OjRN5 0kKq6k0Li5RWwe5WtTHFt3TYwxVLWkY1Lzk+3UeDatocc0mcujMCxlDzR7Bu7uyvA+Qc L4y0QtxKKxmX3cKVvJgIGUFnYiqKFOVnMTuzXdMYNZw3lcRv8r5XZOu2/YpTHjZUcGb+ 2r3ypC59sUXQn8dV4noAgZo1uZleAfvXs2xgEiJSeyHil/BWPmGBH3ueokhmrOSTcqCZ Sjqft9/86EJ/166C+HqD6/HQvKZE+12WnSRGJXoM6jktL5L6frN3fopN2z1e8kFk31t5 Bq5A== X-Gm-Message-State: AOJu0YxK4zIhF7Iuwj0WRXRZopxS1a+Zi0bkz2sOJ1B+vm0Z3jDMr8fb gt4sH+dKl0CocwLWVV1wk37xv05rgCzwcHmQBdWe1JfDB7Ydx0cjDOQHob5b7xjL6dJwgQ== X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:6902:70b:b0:dfb:210f:3ad5 with SMTP id 3f1490d57ef6-dfb210f3d5cmr1232075276.11.1718028581873; Mon, 10 Jun 2024 07:09:41 -0700 (PDT) Date: Mon, 10 Jun 2024 16:09:33 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2783; i=ardb@kernel.org; h=from:subject; bh=7tiDVDTWIXbfAudS++fHHxGeLcuYd15jw6ZEmeO+/MU=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIS2dU9az1Pxb3uenUiVclcU1PN5qVm32v5zOOZyJKvD++ ihkalZHKQuDGAeDrJgii8Dsv+92np4oVes8SxZmDisTyBAGLk4BmIjnP0aG26Ux2Xcmy3AcLN9+ VjxE7nVQn75Ox6HN9hs9lM3ksv99ZPjv7HhEeN3/U5Zpp6wubl54XfX8c2M3mxpHI+YNmYsTrqz mBAA= X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240610140932.2489527-2-ardb+git@google.com> Subject: [PATCH] x86/efi: Free EFI memory map only when installing a new one. From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, x86@kernel.org, Ard Biesheuvel , Ashish Kalra , Dave Young , Mike Rapoport , Borislav Petkov Content-Type: text/plain; charset="UTF-8" From: Ard Biesheuvel The logic in __efi_memmap_init() is shared between two different execution flows: - mapping the EFI memory map early or late into the kernel VA space, so that its entries can be accessed; - cloning the EFI memory map in order to insert new entries that are created as a result of creating a memory reservation (efi_arch_mem_reserve()) In the former case, the underlying memory containing the kernel's view of the EFI memory map (which may be heavily modified by the kernel itself on x86) is not modified at all, and the only thing that changes is the virtual mapping of this memory, which is different between early and late boot. In the latter case, an entirely new allocation is created that carries a new, updated version of the kernel's view of the EFI memory map. When installing this new version, the old version will no longer be referenced, and if the memory was allocated by the kernel, it will leak unless it gets freed. The logic that implements this freeing currently lives on the code path that is shared between these two use cases, but it should only apply to the latter. So move it to the correct spot. Cc: Ashish Kalra Cc: Dave Young Cc: Mike Rapoport Cc: Borislav Petkov Link: https://lore.kernel.org/all/36ad5079-4326-45ed-85f6-928ff76483d3@amd.com Signed-off-by: Ard Biesheuvel --- arch/x86/platform/efi/memmap.c | 5 +++++ drivers/firmware/efi/memmap.c | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/x86/platform/efi/memmap.c b/arch/x86/platform/efi/memmap.c index 4ef20b49eb5e..4990244e5168 100644 --- a/arch/x86/platform/efi/memmap.c +++ b/arch/x86/platform/efi/memmap.c @@ -97,6 +97,11 @@ int __init efi_memmap_install(struct efi_memory_map_data *data) if (efi_enabled(EFI_PARAVIRT)) return 0; + if (efi.memmap.flags & (EFI_MEMMAP_MEMBLOCK | EFI_MEMMAP_SLAB)) + __efi_memmap_free(efi.memmap.phys_map, + efi.memmap.desc_size * efi.memmap.nr_map, + efi.memmap.flags); + return __efi_memmap_init(data); } diff --git a/drivers/firmware/efi/memmap.c b/drivers/firmware/efi/memmap.c index 3365944f7965..3759e95a7407 100644 --- a/drivers/firmware/efi/memmap.c +++ b/drivers/firmware/efi/memmap.c @@ -51,11 +51,6 @@ int __init __efi_memmap_init(struct efi_memory_map_data *data) return -ENOMEM; } - if (efi.memmap.flags & (EFI_MEMMAP_MEMBLOCK | EFI_MEMMAP_SLAB)) - __efi_memmap_free(efi.memmap.phys_map, - efi.memmap.desc_size * efi.memmap.nr_map, - efi.memmap.flags); - map.phys_map = data->phys_map; map.nr_map = data->size / data->desc_size; map.map_end = map.map + data->size; -- 2.45.2.505.gda0bf45e8d-goog