Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp412680lqp; Wed, 12 Jun 2024 05:43:11 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV5nw0XtleNviT7A+RF8C8hgQu+llHSsUpdqECIzRFUX4vbaVR2cBqJ4ZUGcU6FKmveLusho175p7giECW3b6r2zj/pcwuxbYbx6y3pOw== X-Google-Smtp-Source: AGHT+IHtlrLldYAgIzsGfia1G39I65Flano4x55ej9a7KuN+ftFqYiuIiiVnZv9xW0Y3rSIhxSnd X-Received: by 2002:a05:6359:4599:b0:19f:709c:563b with SMTP id e5c5f4694b2df-19f709c56d1mr60670855d.16.1718196191503; Wed, 12 Jun 2024 05:43:11 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718196191; cv=pass; d=google.com; s=arc-20160816; b=UQJ4Wsq7VmyePGx3RSLuVaibg4CpCASs12JL7zdZfya0U4V3l2UStjYaMqbKdw1IVC B+GaOho3E4Udfev6X14GrXOn6CvggGExA32fVO15tMUc/f9OnfeSuEC6GNC/X4eZCXFf +FiDnEikS6wK4fFyVsg1gfMKaa/oMcPt4Wi2wuwaoIlxvOtUbNn2uKx3pKmVbNWvTBSy NlN6geJ1aVqpDhfNIDbvyw30FaBzKYLJo8j0JbfGQA6EoKWE/Hpi1Jby2Tf8KE2q06jN Gw8FZkNPaOeBN+Cxy7/LUGoZR1wXqTTsUI9CrghSpmdYSPWaE0RxEt7RWy4H3IZECmWK q2vQ== 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=nEd1lRimO6iA7hWFaxTwTmIDTFIM9zKyp5vC+IWBdtc=; fh=f6ldaU9BN2/lhBNt/2c5/oBkGWOJ6qhdD5kzsUVZbQc=; b=EEW29BbMaLjRcb3bcKHndqyS0JrIqHhUTFzkASU2tK98BCQk1GqG8/1zXvWmUzhEpC IfnkTmu6obF5osuAPTTLwuTEMHkJgIEjIJtb5yCS/UC4xZie26eVzmLt9xtGBi0RuWRl 4LfUPp4XQI1qblcpcTd1XOOvgol3csccTDc4hoa1a3NmK5qqQsza6mXVY9h/BqCPdtk7 tlWOWKTltK9viAFKt+Iuv2fRK0iH8VumhlUnp1JzEEIsbEEJfPuF2IR4PMc0KelnW8Gs jxKBrkQMNxCunF6vhr2e/lvShNbWADHAzfBZc1mlW38TOwgtT+fTMOjk3/y6DxxSf6S4 HoNA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vYbsOqaE; 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-211492-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211492-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 41be03b00d2f7-6e7acde42c4si7838784a12.0.2024.06.12.05.43.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 05:43:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-211492-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vYbsOqaE; 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-211492-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211492-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 52B16B210FC for ; Wed, 12 Jun 2024 12:37:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8CADA17167F; Wed, 12 Jun 2024 12:37:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vYbsOqaE" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 1529F171060 for ; Wed, 12 Jun 2024 12:37:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718195837; cv=none; b=O93vvmLuOUS+/6lfsIobJI8iCHav6OZwCGkrUoKSVA1zj1GtKdbILBd9nTo3N6lZrN2yTc6Xx7THBc5wFRCdiM2rZKr02TPsZyOzL5K9E/rdoC2yCXNiT1sbuUWlk66mdJH6d7U56oZu11XIFwvA9Hdym5yQRoBXf4A4mNHWpOI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718195837; c=relaxed/simple; bh=GgEF8zEHf8pEI7O6wjwwr6gKrdf9x38J0uU+CMmF6f0=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=mVnaKQU5AEKpEhACLAJShMDuHlzWcMzr2xFSZoopiAsuFG4NqBW3wgd7rGDClgO3Lzrhk3LMAc+2qPz5MI2iLSfKBH3kkN3yQkqz1IaARXwzd+ThiqSkk28HGZPQ4q+AsiTlYw/gF0PDBQ9zWWa2Hwxk2sipznvqlgQjlJs5DDI= 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=vYbsOqaE; arc=none smtp.client-ip=209.85.128.202 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-yw1-f202.google.com with SMTP id 00721157ae682-627e6fe0303so40242617b3.2 for ; Wed, 12 Jun 2024 05:37:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718195835; x=1718800635; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=nEd1lRimO6iA7hWFaxTwTmIDTFIM9zKyp5vC+IWBdtc=; b=vYbsOqaEzZlBasd/GvXK1rWYSpXjl/Ze24acIjmejBwwMToa5KbSXC+F25tGiC3xCN lBhgKUK0cnDpBlHoAdP+kzECzKdL0z3Axx1PXl+BSP46P/Ryu93gattb4hJ72pBub2/y 8tcNWqTyg/NKvE1+spzSBGdK16pDGukQRvLXSgHUuYOT4vqa0HOa0THCs2FWKC4M/L5S hbezP8xovmZd5qKqsdOWrULQa7FV/0UZKn9uVgNn1pZ9O3D7QKmlgk+iwbYHcyDbbhr4 t8byJICMTFSXHN0Js8Ud9W4umvNwzX9QtiOHxm7j25ucKsRQm3RZAFHuf7pSVTZUzIdV I/4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718195835; x=1718800635; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=nEd1lRimO6iA7hWFaxTwTmIDTFIM9zKyp5vC+IWBdtc=; b=TcDk+wqqsZrSoeRqjGZkwmude4F6k1NeSZ3SZMxQjOM2oxhozjesXxRQdZMaNZonoy Atdvc5MmKrouRDjojsvhiJh9JiPdOlG7UszBEAHMfAqSt3wbd82FH7oy34JAeyxaDAh2 Byp/JYLsmIa5glLLrpLM8cYpO4N53tf5D2AJ6sKaCv2GTvA93h2W+bBHetMXHvv44YmT lJNZ4f6ge0YVsG3zWq3RPOHXzPWB8n1iB5jDCVWoSriMde59RLR5M365hTQ4EkPVS7Fs GZup56acMZgMdaVKiMa+owDVHZAs3rgyGL9kAgWjJJ6LRmde0F2jMVBGtoZDqHxzuQnQ FfMQ== X-Gm-Message-State: AOJu0Yz6RFSiYBlW+4//5GrOtCSMePAPo4b47VFALWoUwjjgV3l2Lcui dlGWR5SJ1PTlKUf433LwoXWQ8xJ5vrSVaeMG3WW0UhA/AKDtPtKtwpv1+U8sKUiSCDiF0Q== X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:690c:6403:b0:62c:c5ea:66ad with SMTP id 00721157ae682-62fb847c4c0mr4792757b3.4.1718195834992; Wed, 12 Jun 2024 05:37:14 -0700 (PDT) Date: Wed, 12 Jun 2024 14:37:11 +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=4718; i=ardb@kernel.org; h=from:subject; bh=OWIOh2L1bo96UhRQWj5TV3Q2gAHpb3WgG7hEvcb06P4=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIS1zWvnlz4mKa/6oxF6ufHovUy5u/bYZb5cfNenh3/PBa 6fFOv8THaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAiU/Yx/LP7Lx8edP9cVKPX zqItt3cf9O9relC06P7vDO3CWfZh1/8wMpzg1AiePOW9ROryoFrrXOtZs6SUIq6lsnafMvQyf/x JgR8A X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240612123710.177557-2-ardb+git@google.com> Subject: [PATCH v2] 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 , Dan Williams 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. While at it, move __efi_memmap_free() into its only caller, and drop the dummy definition for non-x86 architectures, as that is no longer needed. Cc: Ashish Kalra Cc: Dave Young Cc: Mike Rapoport Cc: Borislav Petkov Cc: Dan Williams Fixes: f0ef6523475f ("efi: Fix efi_memmap_alloc() leaks") Link: https://lore.kernel.org/all/36ad5079-4326-45ed-85f6-928ff76483d3@amd.com Signed-off-by: Ard Biesheuvel --- v2: - free old memory map only after installing the new one succeeded - move __efi_memmap_free() into its only caller - drop obsolete dummy declaration from generic code arch/x86/platform/efi/memmap.c | 38 +++++++++++--------- drivers/firmware/efi/memmap.c | 9 ----- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/arch/x86/platform/efi/memmap.c b/arch/x86/platform/efi/memmap.c index 4ef20b49eb5e..602386eead49 100644 --- a/arch/x86/platform/efi/memmap.c +++ b/arch/x86/platform/efi/memmap.c @@ -30,21 +30,6 @@ static phys_addr_t __init __efi_memmap_alloc_late(unsigned long size) return PFN_PHYS(page_to_pfn(p)); } -void __init __efi_memmap_free(u64 phys, unsigned long size, unsigned long flags) -{ - if (flags & EFI_MEMMAP_MEMBLOCK) { - if (slab_is_available()) - memblock_free_late(phys, size); - else - memblock_phys_free(phys, size); - } else if (flags & EFI_MEMMAP_SLAB) { - struct page *p = pfn_to_page(PHYS_PFN(phys)); - unsigned int order = get_order(size); - - free_pages((unsigned long) page_address(p), order); - } -} - /** * efi_memmap_alloc - Allocate memory for the EFI memory map * @num_entries: Number of entries in the allocated map. @@ -92,12 +77,33 @@ int __init efi_memmap_alloc(unsigned int num_entries, */ int __init efi_memmap_install(struct efi_memory_map_data *data) { + unsigned long size = efi.memmap.desc_size * efi.memmap.nr_map; + unsigned long flags = efi.memmap.flags; + u64 phys = efi.memmap.phys_map; + int ret; + efi_memmap_unmap(); if (efi_enabled(EFI_PARAVIRT)) return 0; - return __efi_memmap_init(data); + ret = __efi_memmap_init(data); + if (ret) + return ret; + + if (flags & EFI_MEMMAP_MEMBLOCK) { + if (slab_is_available()) + memblock_free_late(phys, size); + else + memblock_phys_free(phys, size); + } else if (flags & EFI_MEMMAP_SLAB) { + struct page *p = pfn_to_page(PHYS_PFN(phys)); + unsigned int order = get_order(size); + + free_pages((unsigned long)page_address(p), order); + } + + return 0; } /** diff --git a/drivers/firmware/efi/memmap.c b/drivers/firmware/efi/memmap.c index 3365944f7965..34109fd86c55 100644 --- a/drivers/firmware/efi/memmap.c +++ b/drivers/firmware/efi/memmap.c @@ -15,10 +15,6 @@ #include #include -#ifndef __efi_memmap_free -#define __efi_memmap_free(phys, size, flags) do { } while (0) -#endif - /** * __efi_memmap_init - Common code for mapping the EFI memory map * @data: EFI memory map data @@ -51,11 +47,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