mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	efi: Add unaccepted memory support
efi_config_parse_tables() reserves memory that holds unaccepted memory configuration table so it won't be reused by page allocator. Core-mm requires few helpers to support unaccepted memory: - accept_memory() checks the range of addresses against the bitmap and accept memory if needed. - range_contains_unaccepted_memory() checks if anything within the range requires acceptance. Architectural code has to provide efi_get_unaccepted_table() that returns pointer to the unaccepted memory configuration table. arch_accept_memory() handles arch-specific part of memory acceptance. Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Reviewed-by: Ard Biesheuvel <ardb@kernel.org> Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com> Link: https://lore.kernel.org/r/20230606142637.5171-6-kirill.shutemov@linux.intel.com
This commit is contained in:
		
							parent
							
								
									3fd1239a78
								
							
						
					
					
						commit
						2053bc57f3
					
				
					 5 changed files with 142 additions and 0 deletions
				
			
		| 
						 | 
					@ -96,6 +96,9 @@ static const unsigned long * const efi_tables[] = {
 | 
				
			||||||
#ifdef CONFIG_EFI_COCO_SECRET
 | 
					#ifdef CONFIG_EFI_COCO_SECRET
 | 
				
			||||||
	&efi.coco_secret,
 | 
						&efi.coco_secret,
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					#ifdef CONFIG_UNACCEPTED_MEMORY
 | 
				
			||||||
 | 
						&efi.unaccepted,
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
u64 efi_setup;		/* efi setup_data physical address */
 | 
					u64 efi_setup;		/* efi setup_data physical address */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -41,3 +41,4 @@ obj-$(CONFIG_EFI_CAPSULE_LOADER)	+= capsule-loader.o
 | 
				
			||||||
obj-$(CONFIG_EFI_EARLYCON)		+= earlycon.o
 | 
					obj-$(CONFIG_EFI_EARLYCON)		+= earlycon.o
 | 
				
			||||||
obj-$(CONFIG_UEFI_CPER_ARM)		+= cper-arm.o
 | 
					obj-$(CONFIG_UEFI_CPER_ARM)		+= cper-arm.o
 | 
				
			||||||
obj-$(CONFIG_UEFI_CPER_X86)		+= cper-x86.o
 | 
					obj-$(CONFIG_UEFI_CPER_X86)		+= cper-x86.o
 | 
				
			||||||
 | 
					obj-$(CONFIG_UNACCEPTED_MEMORY)		+= unaccepted_memory.o
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -50,6 +50,9 @@ struct efi __read_mostly efi = {
 | 
				
			||||||
#ifdef CONFIG_EFI_COCO_SECRET
 | 
					#ifdef CONFIG_EFI_COCO_SECRET
 | 
				
			||||||
	.coco_secret		= EFI_INVALID_TABLE_ADDR,
 | 
						.coco_secret		= EFI_INVALID_TABLE_ADDR,
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					#ifdef CONFIG_UNACCEPTED_MEMORY
 | 
				
			||||||
 | 
						.unaccepted		= EFI_INVALID_TABLE_ADDR,
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
EXPORT_SYMBOL(efi);
 | 
					EXPORT_SYMBOL(efi);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -605,6 +608,9 @@ static const efi_config_table_type_t common_tables[] __initconst = {
 | 
				
			||||||
#ifdef CONFIG_EFI_COCO_SECRET
 | 
					#ifdef CONFIG_EFI_COCO_SECRET
 | 
				
			||||||
	{LINUX_EFI_COCO_SECRET_AREA_GUID,	&efi.coco_secret,	"CocoSecret"	},
 | 
						{LINUX_EFI_COCO_SECRET_AREA_GUID,	&efi.coco_secret,	"CocoSecret"	},
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					#ifdef CONFIG_UNACCEPTED_MEMORY
 | 
				
			||||||
 | 
						{LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID,	&efi.unaccepted,	"Unaccepted"	},
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
#ifdef CONFIG_EFI_GENERIC_STUB
 | 
					#ifdef CONFIG_EFI_GENERIC_STUB
 | 
				
			||||||
	{LINUX_EFI_SCREEN_INFO_TABLE_GUID,	&screen_info_table			},
 | 
						{LINUX_EFI_SCREEN_INFO_TABLE_GUID,	&screen_info_table			},
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					@ -759,6 +765,25 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) &&
 | 
				
			||||||
 | 
						    efi.unaccepted != EFI_INVALID_TABLE_ADDR) {
 | 
				
			||||||
 | 
							struct efi_unaccepted_memory *unaccepted;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							unaccepted = early_memremap(efi.unaccepted, sizeof(*unaccepted));
 | 
				
			||||||
 | 
							if (unaccepted) {
 | 
				
			||||||
 | 
								unsigned long size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (unaccepted->version == 1) {
 | 
				
			||||||
 | 
									size = sizeof(*unaccepted) + unaccepted->size;
 | 
				
			||||||
 | 
									memblock_reserve(efi.unaccepted, size);
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									efi.unaccepted = EFI_INVALID_TABLE_ADDR;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								early_memunmap(unaccepted, sizeof(*unaccepted));
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										112
									
								
								drivers/firmware/efi/unaccepted_memory.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								drivers/firmware/efi/unaccepted_memory.c
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,112 @@
 | 
				
			||||||
 | 
					// SPDX-License-Identifier: GPL-2.0-only
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <linux/efi.h>
 | 
				
			||||||
 | 
					#include <linux/memblock.h>
 | 
				
			||||||
 | 
					#include <linux/spinlock.h>
 | 
				
			||||||
 | 
					#include <asm/unaccepted_memory.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* Protects unaccepted memory bitmap */
 | 
				
			||||||
 | 
					static DEFINE_SPINLOCK(unaccepted_memory_lock);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * accept_memory() -- Consult bitmap and accept the memory if needed.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Only memory that is explicitly marked as unaccepted in the bitmap requires
 | 
				
			||||||
 | 
					 * an action. All the remaining memory is implicitly accepted and doesn't need
 | 
				
			||||||
 | 
					 * acceptance.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * No need to accept:
 | 
				
			||||||
 | 
					 *  - anything if the system has no unaccepted table;
 | 
				
			||||||
 | 
					 *  - memory that is below phys_base;
 | 
				
			||||||
 | 
					 *  - memory that is above the memory that addressable by the bitmap;
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void accept_memory(phys_addr_t start, phys_addr_t end)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct efi_unaccepted_memory *unaccepted;
 | 
				
			||||||
 | 
						unsigned long range_start, range_end;
 | 
				
			||||||
 | 
						unsigned long flags;
 | 
				
			||||||
 | 
						u64 unit_size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						unaccepted = efi_get_unaccepted_table();
 | 
				
			||||||
 | 
						if (!unaccepted)
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						unit_size = unaccepted->unit_size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Only care for the part of the range that is represented
 | 
				
			||||||
 | 
						 * in the bitmap.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						if (start < unaccepted->phys_base)
 | 
				
			||||||
 | 
							start = unaccepted->phys_base;
 | 
				
			||||||
 | 
						if (end < unaccepted->phys_base)
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Translate to offsets from the beginning of the bitmap */
 | 
				
			||||||
 | 
						start -= unaccepted->phys_base;
 | 
				
			||||||
 | 
						end -= unaccepted->phys_base;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Make sure not to overrun the bitmap */
 | 
				
			||||||
 | 
						if (end > unaccepted->size * unit_size * BITS_PER_BYTE)
 | 
				
			||||||
 | 
							end = unaccepted->size * unit_size * BITS_PER_BYTE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						range_start = start / unit_size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						spin_lock_irqsave(&unaccepted_memory_lock, flags);
 | 
				
			||||||
 | 
						for_each_set_bitrange_from(range_start, range_end, unaccepted->bitmap,
 | 
				
			||||||
 | 
									   DIV_ROUND_UP(end, unit_size)) {
 | 
				
			||||||
 | 
							unsigned long phys_start, phys_end;
 | 
				
			||||||
 | 
							unsigned long len = range_end - range_start;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							phys_start = range_start * unit_size + unaccepted->phys_base;
 | 
				
			||||||
 | 
							phys_end = range_end * unit_size + unaccepted->phys_base;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							arch_accept_memory(phys_start, phys_end);
 | 
				
			||||||
 | 
							bitmap_clear(unaccepted->bitmap, range_start, len);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						spin_unlock_irqrestore(&unaccepted_memory_lock, flags);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool range_contains_unaccepted_memory(phys_addr_t start, phys_addr_t end)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct efi_unaccepted_memory *unaccepted;
 | 
				
			||||||
 | 
						unsigned long flags;
 | 
				
			||||||
 | 
						bool ret = false;
 | 
				
			||||||
 | 
						u64 unit_size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						unaccepted = efi_get_unaccepted_table();
 | 
				
			||||||
 | 
						if (!unaccepted)
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						unit_size = unaccepted->unit_size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Only care for the part of the range that is represented
 | 
				
			||||||
 | 
						 * in the bitmap.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						if (start < unaccepted->phys_base)
 | 
				
			||||||
 | 
							start = unaccepted->phys_base;
 | 
				
			||||||
 | 
						if (end < unaccepted->phys_base)
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Translate to offsets from the beginning of the bitmap */
 | 
				
			||||||
 | 
						start -= unaccepted->phys_base;
 | 
				
			||||||
 | 
						end -= unaccepted->phys_base;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Make sure not to overrun the bitmap */
 | 
				
			||||||
 | 
						if (end > unaccepted->size * unit_size * BITS_PER_BYTE)
 | 
				
			||||||
 | 
							end = unaccepted->size * unit_size * BITS_PER_BYTE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						spin_lock_irqsave(&unaccepted_memory_lock, flags);
 | 
				
			||||||
 | 
						while (start < end) {
 | 
				
			||||||
 | 
							if (test_bit(start / unit_size, unaccepted->bitmap)) {
 | 
				
			||||||
 | 
								ret = true;
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							start += unit_size;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						spin_unlock_irqrestore(&unaccepted_memory_lock, flags);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return ret;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -646,6 +646,7 @@ extern struct efi {
 | 
				
			||||||
	unsigned long			tpm_final_log;		/* TPM2 Final Events Log table */
 | 
						unsigned long			tpm_final_log;		/* TPM2 Final Events Log table */
 | 
				
			||||||
	unsigned long			mokvar_table;		/* MOK variable config table */
 | 
						unsigned long			mokvar_table;		/* MOK variable config table */
 | 
				
			||||||
	unsigned long			coco_secret;		/* Confidential computing secret table */
 | 
						unsigned long			coco_secret;		/* Confidential computing secret table */
 | 
				
			||||||
 | 
						unsigned long			unaccepted;		/* Unaccepted memory table */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	efi_get_time_t			*get_time;
 | 
						efi_get_time_t			*get_time;
 | 
				
			||||||
	efi_set_time_t			*set_time;
 | 
						efi_set_time_t			*set_time;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue