efi: Determine how much space is used by boot services-only variables
Subsystems (3)
PATCH 1/3 efi: Determine how much space is used by boot services-only variables
EFI variables can be flagged as being accessible only within boot services. This makes it awkward for us to figure out how much space they use at runtime. In theory we could figure this out by simply comparing the results from QueryVariableInfo() to the space used by all of our variables, but that fails if the platform doesn't garbage collect on every boot. Thankfully, calling QueryVariableInfo() while still inside boot services gives a more reliable answer. This patch passes that information from the EFI boot stub up to the efivars code, letting us calculate a reasonably accurate value.
--- arch/x86/boot/compressed/eboot.c | 47 +++++++++++++++++++++++++++++++++++ arch/x86/include/asm/efi.h | 10 ++++++++ arch/x86/include/uapi/asm/bootparam.h | 1 + arch/x86/platform/efi/efi.c | 24 ++++++++++++++++++ drivers/firmware/efivars.c | 29 +++++++++++++++++++++ 5 files changed, 111 insertions(+)
arch/x86/boot/compressed/eboot.c +47 -0
@@ -251,6 +251,51 @@ static void find_bits(unsigned long mask, u8 *pos, u8 *size) *size = len; } +static efi_status_t setup_efi_vars (struct boot_params *params) +{ +struct setup_data *data; +struct efi_var_bootdata *efidata; +u64 store_size, remaining_size, var_size; +efi_status_t status; + +if (!sys_table->runtime->query_variable_info) +return EFI_UNSUPPORTED ; + + data = (struct setup_data *)(unsigned long )params->hdr.setup_data; + +while (data && data->next) + data = (struct setup_data *)(unsigned long )data->next; + + status =efi_call_phys4 (sys_table->runtime->query_variable_info, +EFI_VARIABLE_NON_VOLATILE | +EFI_VARIABLE_BOOTSERVICE_ACCESS | +EFI_VARIABLE_RUNTIME_ACCESS , &store_size, + &remaining_size, &var_size); + +if (status !=EFI_SUCCESS ) +return status; + + status =efi_call_phys3 (sys_table->boottime->allocate_pool, +EFI_LOADER_DATA ,sizeof (*efidata), &efidata); + +if (status !=EFI_SUCCESS ) +return status; + + efidata->data.type =SETUP_EFI_VARS ; + efidata->data.len =sizeof (struct efi_var_bootdata ) - +sizeof (struct setup_data ); + efidata->data.next =0 ; + efidata->store_size = store_size; + efidata->remaining_size = remaining_size; + efidata->max_var_size = var_size; + +if (data) + data->next = (unsigned long )efidata; +else + params->hdr.setup_data = (unsigned long )efidata; + +} +static efi_status_t setup_efi_pci (struct boot_params *params) {efi_pci_io_protocol *pci;
@@ -1157,6 +1202,8 @@ struct boot_params *efi_main(void *handle, efi_system_table_t *_table,setup_graphics (boot_params); +setup_efi_vars (boot_params); +setup_efi_pci (boot_params); status = efi_call_phys3(sys_table->boottime->allocate_pool,
arch/x86/include/asm/efi.h +10 -0
@@ -93,6 +93,9 @@ extern void __iomem *efi_ioremap(unsigned long addr, unsigned long size,#endif /* CONFIG_X86_32 */ +extern u64 efi_var_store_size; +extern u64 efi_var_remaining_size; +extern u64 efi_var_max_var_size;extern int add_efi_memmap;extern unsigned long x86_efi_facility;extern void efi_set_executable (efi_memory_desc_t *md,bool executable);
@@ -102,6 +105,13 @@ extern void efi_call_phys_epilog(void);extern void efi_unmap_memmap (void );extern void efi_memory_uc (u64 addr,unsigned long size); +struct efi_var_bootdata { +struct setup_data data; +u64 store_size; +u64 remaining_size; +u64 max_var_size; +}; +#ifdef CONFIG_EFI static inline bool efi_is_native (void )
arch/x86/include/uapi/asm/bootparam.h +1 -0
@@ -6,6 +6,7 @@#define SETUP_E820_EXT 1#define SETUP_DTB 2#define SETUP_PCI 3 +#define SETUP_EFI_VARS 4/* ram_size flags */ #define RAMDISK_IMAGE_START_MASK 0x07FF
arch/x86/platform/efi/efi.c +24 -0
@@ -71,6 +71,13 @@ static efi_system_table_t efi_systab __initdata;unsigned long x86_efi_facility; +u64 efi_var_store_size; +EXPORT_SYMBOL (efi_var_store_size); +u64 efi_var_remaining_size; +EXPORT_SYMBOL (efi_var_remaining_size); +u64 efi_var_max_var_size; +EXPORT_SYMBOL (efi_var_max_var_size); +/* * Returns 1 if 'facility' is enabled, 0 otherwise. */
@@ -682,6 +689,9 @@ void __init efi_init(void)char vendor[100 ] ="unknown" ;int i =0 ;void *tmp; +struct setup_data *data; +struct efi_var_bootdata *efi_var_data; +u64 pa_data;#ifdef CONFIG_X86_32 if (boot_params.efi_info.efi_systab_hi ||
@@ -699,6 +709,20 @@ void __init efi_init(void)if (efi_systab_init (efi_phys.systab))return ; + pa_data = boot_params.hdr.setup_data; +while (pa_data) { + data =early_ioremap (pa_data,sizeof (*efi_var_data)); +if (data->type ==SETUP_EFI_VARS ) { + efi_var_data = (struct efi_var_bootdata *)data; + + efi_var_store_size = efi_var_data->store_size; + efi_var_remaining_size = efi_var_data->remaining_size; + efi_var_max_var_size = efi_var_data->max_var_size; + } + pa_data = data->next; +early_iounmap (data,sizeof (*efi_var_data)); + } +set_bit (EFI_SYSTEM_TABLES , &x86_efi_facility);/*
drivers/firmware/efivars.c +29 -0
@@ -85,6 +85,7 @@#include <linux/ramfs.h> #include <linux/pagemap.h> +#include <asm/efi.h> #include <asm/uaccess.h> #define EFIVARS_VERSION "0.08"
@@ -139,6 +140,8 @@ struct efivar_attribute {static struct efivars __efivars;static struct efivar_operations ops; +static u64 boot_var_size; +static u64 boot_used_size;#define PSTORE_EFI_ATTRIBUTES \ (EFI_VARIABLE_NON_VOLATILE | \
@@ -2002,6 +2005,9 @@ int register_efivars(struct efivars *efivars,efi_char16_t *variable_name;unsigned long variable_name_size =1024 ;int error =0 ; +struct efivar_entry *entry; +struct efi_variable *var; +u64 active_size =0 ; variable_name =kzalloc (variable_name_size,GFP_KERNEL );if (!variable_name) {
@@ -2074,6 +2080,25 @@ int register_efivars(struct efivars *efivars, } }while (status !=EFI_NOT_FOUND ); +if (boot_used_size) { +list_for_each_entry (entry, &efivars->list, list) { + var = &entry->var; + status =get_var_data (efivars, var); + +if (status || + !(var->Attributes &EFI_VARIABLE_NON_VOLATILE )) +continue ; + + active_size += var->DataSize; + active_size +=utf16_strsize (var->VariableName,1024 ); + } + +if (active_size < boot_used_size) + boot_var_size = boot_used_size - active_size; +else + printk(KERN_WARNING FW_BUG "efivars: Inconsistent initial sizes\n"); + } + error =create_efivars_bin_attributes (efivars);if (error)unregister_efivars (efivars);
@@ -2121,6 +2146,10 @@ efivars_init(void) ops.get_next_variable = efi.get_next_variable; ops.query_variable_store = efi_query_variable_store; +#ifdef CONFIG_X86 + boot_used_size = efi_var_store_size - efi_var_remaining_size; +#endif + error =register_efivars (&__efivars, &ops, efi_kobj);if (error) goto err_put;
Signature · 1 line
1.8.1.2
PATCH 2/3 Revert "x86, efivars: firmware bug workarounds should be in platform code"
This reverts commit a6e4d5a03e9e3587e88aba687d8f225f4f04c792. Doing this workaround properly requires us to work within the variable code.
--- arch/x86/platform/efi/efi.c | 25 ------------------------- drivers/firmware/efivars.c | 18 +++++++++++++++--- include/linux/efi.h | 9 +-------- 3 files changed, 16 insertions(+), 36 deletions(-)
arch/x86/platform/efi/efi.c +0 -25
@@ -1023,28 +1023,3 @@ u64 efi_mem_attributes(unsigned long phys_addr) }return 0 ; } - -/* -* Some firmware has serious problems when using more than 50% of the EFI -* variable store, i.e. it triggers bugs that can brick machines. Ensure that -* we never use more than this safe limit. -* -* Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable -* store. -*/ -efi_status_t efi_query_variable_store (u32 attributes,unsigned long size) -{ -efi_status_t status; -u64 storage_size, remaining_size, max_size; - - status = efi.query_variable_info (attributes, &storage_size, - &remaining_size, &max_size); -if (status !=EFI_SUCCESS ) -return status; - -if (!storage_size || size > remaining_size || size > max_size || - (remaining_size - size) < (storage_size /2 )) -return EFI_OUT_OF_RESOURCES ; - -return EFI_SUCCESS ; -}
drivers/firmware/efivars.c +15 -3
@@ -439,12 +439,24 @@ static efi_status_t check_var_size_locked(struct efivars *efivars, u32 attributes, unsigned long size) { + u64 storage_size, remaining_size, max_size; +efi_status_t status;const struct efivar_operations *fops = efivars->ops; -if (!efivars->ops->query_variable_store) +if (!efivars->ops->query_variable_info)return EFI_UNSUPPORTED ; -return fops->query_variable_store (attributes, size); + status = fops->query_variable_info (attributes, &storage_size, + &remaining_size, &max_size); + +if (status !=EFI_SUCCESS ) +return status; + +if (!storage_size || size > remaining_size || size > max_size || + (remaining_size - size) < (storage_size /2 )) +return EFI_OUT_OF_RESOURCES ; + +return status; }
@@ -2144,7 +2156,7 @@ efivars_init(void) ops.get_variable = efi.get_variable; ops.set_variable = efi.set_variable; ops.get_next_variable = efi.get_next_variable; - ops.query_variable_store = efi_query_variable_store; + ops.query_variable_info = efi.query_variable_info;#ifdef CONFIG_X86 boot_used_size = efi_var_store_size - efi_var_remaining_size;
include/linux/efi.h +1 -8
@@ -333,7 +333,6 @@ typedef efi_status_t efi_query_capsule_caps_t(efi_capsule_header_t **capsules,unsigned long count, u64 *max_size, int *reset_type); -typedef efi_status_t efi_query_variable_store_t (u32 attributes,unsigned long size);/* * EFI Configuration Table and GUID definitions
@@ -576,15 +575,9 @@ extern void efi_enter_virtual_mode (void); /* switch EFI to virtual mode, if pos#ifdef CONFIG_X86 extern void efi_late_init (void );extern void efi_free_boot_services (void ); -extern efi_status_t efi_query_variable_store (u32 attributes,unsigned long size);#else static inline void efi_late_init (void ) {}static inline void efi_free_boot_services (void ) {} - -static inline efi_status_t efi_query_variable_store (u32 attributes,unsigned long size) -{ -return EFI_SUCCESS ; -}#endif extern void __iomem *efi_lookup_mapped_addr (u64 phys_addr);extern u64 efi_get_iobase (void );
@@ -738,7 +731,7 @@ struct efivar_operations {efi_get_variable_t *get_variable;efi_get_next_variable_t *get_next_variable;efi_set_variable_t *set_variable; -efi_query_variable_store_t *query_variable_store; +efi_query_variable_info_t *query_variable_info; };struct efivars {
Signature · 1 line
1.8.1.2
PATCH 3/3 efi: Distinguish between "remaining space" and actually used space
In reply to Matthew GarrettEFI implementations distinguish between space that is actively used by a variable and space that merely hasn't been garbage collected yet. Space that hasn't yet been garbage collected isn't available for use and so isn't counted in the remaining_space field returned by QueryVariableInfo().
Combined with commit 68d9298 this can cause problems. Some implementations don't garbage collect until the remaining space is smaller than the maximum variable size, and as a result check_var_size() will always fail once more than 50% of the variable store has been used even if most of that space is marked as available for garbage collection. The user is unable to create new variables, and deleting variables doesn't increase the remaining space.
The problem that 68d9298 was attempting to avoid was one where certain platforms fail if the actively used space is greater than 50% of the available storage space. We should be able to calculate that by simply summing the size of each available variable and subtracting that from the total storage space. With luck this will fix the problem described in
https://bugzilla.kernel.org/show_bug.cgi?id=55471 without permitting damage to occur to the machines 68d9298 was attempting to fix.
--- drivers/firmware/efivars.c | 104 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-)
drivers/firmware/efivars.c +101 -3
@@ -104,6 +104,13 @@ MODULE_VERSION(EFIVARS_VERSION);*/ #define GUID_LEN 36 +/* +* There's some additional metadata associated with each +* variable. Intel's reference implementation is 60 bytes - bump that +* to account for potential alignment constraints +*/ +#define VAR_METADATA_SIZE 64 +static bool efivars_pstore_disable =IS_ENABLED (CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE );
@@ -405,6 +412,52 @@ validate_var(struct efi_variable *var, u8 *data, unsigned long len) }static efi_status_t +get_var_size_locked (struct efivars *efivars,struct efi_variable *var) +{ +efi_status_t status; +void *dummy; + + var->DataSize =0 ; + status = efivars->ops->get_variable (var->VariableName, + &var->VendorGuid, + &var->Attributes, + &var->DataSize, + var->Data); + +if (status !=EFI_BUFFER_TOO_SMALL ) +return status; + + dummy =kmalloc (var->DataSize,GFP_ATOMIC ); + + status = efivars->ops->get_variable (var->VariableName, + &var->VendorGuid, + &var->Attributes, + &var->DataSize, + dummy); + +kfree (dummy); + +return status; +} + +static efi_status_t +get_var_size (struct efivars *efivars,struct efi_variable *var) +{ +efi_status_t status; +unsigned long flags; + +spin_lock_irqsave (&efivars->lock, flags); + status =get_var_size_locked (efivars, var); +spin_unlock_irqrestore (&efivars->lock, flags); + +if (status !=EFI_SUCCESS ) { +printk (KERN_WARNING "efivars: Failed to get var size 0x%lx!\n" , + status); + } +return status; +} + +static efi_status_t get_var_data_locked (struct efivars *efivars,struct efi_variable *var) {efi_status_t status;
@@ -415,6 +468,10 @@ get_var_data_locked(struct efivars *efivars, struct efi_variable *var) &var->Attributes, &var->DataSize, var->Data); + +if (status !=EFI_SUCCESS ) + var->DataSize =0 ; +return status; }
@@ -440,8 +497,18 @@ check_var_size_locked(struct efivars *efivars, u32 attributes, unsigned long size) {u64 storage_size, remaining_size, max_size; +struct efivar_entry *entry; +struct efi_variable *var;efi_status_t status;const struct efivar_operations *fops = efivars->ops; +unsigned long active_size =0 ; + +/* +* Any writes other than EFI_VARIABLE_NON_VOLATILE will only hit +* RAM, not flash, so ignore them. +*/ +if (!(attributes &EFI_VARIABLE_NON_VOLATILE )) +return EFI_SUCCESS ;if (!efivars->ops->query_variable_info)return EFI_UNSUPPORTED ;
@@ -452,8 +519,39 @@ check_var_size_locked(struct efivars *efivars, u32 attributes,if (status !=EFI_SUCCESS )return status; -if (!storage_size || size > remaining_size || size > max_size || - (remaining_size - size) < (storage_size /2 )) +list_for_each_entry (entry, &efivars->list, list) { + var = &entry->var; + status =EFI_SUCCESS ; + +if (var->DataSize ==0 ) + status =get_var_size_locked (efivars, var); + +if (status || !(var->Attributes &EFI_VARIABLE_NON_VOLATILE )) +continue ; + + active_size += var->DataSize; + active_size +=utf16_strsize (var->VariableName,1024 ); + active_size +=VAR_METADATA_SIZE ; + } + +/* +* Add on the size of any boot services-only variables +*/ + active_size += boot_var_size; + +/* +* Some firmware implementations refuse to boot if there's insufficient +* space in the variable store. We account for that by refusing the +* write if permitting it would reduce the available space to under +* 50%. However, some firmware won't reclaim variable space until +* after the used (not merely the actively used) space drops below +* a threshold. We can approximate that case with the value calculated +* above. If both the firmware and our calculations indicate that the +* available space would drop below 50%, refuse the write. +*/ +if (!storage_size || size > remaining_size || + ((active_size + size +VAR_METADATA_SIZE > storage_size /2 ) && + (remaining_size - size -VAR_METADATA_SIZE < storage_size /2 )))return EFI_OUT_OF_RESOURCES ;return status;
@@ -2095,7 +2193,7 @@ int register_efivars(struct efivars *efivars,if (boot_used_size) {list_for_each_entry (entry, &efivars->list, list) { var = &entry->var; - status =get_var_data (efivars, var); + status =get_var_size (efivars, var);if (status || !(var->Attributes &EFI_VARIABLE_NON_VOLATILE ))
Signature · 1 line
1.8.1.2
Re: [PATCH 3/3] efi: Distinguish between "remaining space" and actually used space
drivers/firmware/efivars.c check_var_size_locked(struct efivars *efivars, u32 attributes, · line 554
Quoted 3 lines · Matthew Garrett
drivers/firmware/efivars.c +3
-0+if (!storage_size || size > remaining_size || + ((active_size + size +VAR_METADATA_SIZE > storage_size /2 ) && + (remaining_size - size -VAR_METADATA_SIZE < storage_size /2 )))
This could overflow.
(u64)32768 - (u64)32768 - VAR_METADATA_SIZE < (u64)65536 / 2 == false
PATCH v4 1/3 efi: Determine how much space is used by boot services-only variables
In reply to Matthew GarrettEFI variables can be flagged as being accessible only within boot services. This makes it awkward for us to figure out how much space they use at runtime. In theory we could figure this out by simply comparing the results from QueryVariableInfo() to the space used by all of our variables, but that fails if the platform doesn't garbage collect on every boot. Thankfully, calling QueryVariableInfo() while still inside boot services gives a more reliable answer. This patch passes that information from the EFI boot stub up to the efivars code, letting us calculate a reasonably accurate value.
--- arch/x86/boot/compressed/eboot.c | 47 +++++++++++++++++++++++++++++++++++ arch/x86/include/asm/efi.h | 10 ++++++++ arch/x86/include/uapi/asm/bootparam.h | 1 + arch/x86/platform/efi/efi.c | 24 ++++++++++++++++++ drivers/firmware/efivars.c | 29 +++++++++++++++++++++ 5 files changed, 111 insertions(+)
arch/x86/boot/compressed/eboot.c +47 -0
@@ -251,6 +251,51 @@ static void find_bits(unsigned long mask, u8 *pos, u8 *size) *size = len; } +static efi_status_t setup_efi_vars (struct boot_params *params) +{ +struct setup_data *data; +struct efi_var_bootdata *efidata; +u64 store_size, remaining_size, var_size; +efi_status_t status; + +if (!sys_table->runtime->query_variable_info) +return EFI_UNSUPPORTED ; + + data = (struct setup_data *)(unsigned long )params->hdr.setup_data; + +while (data && data->next) + data = (struct setup_data *)(unsigned long )data->next; + + status =efi_call_phys4 (sys_table->runtime->query_variable_info, +EFI_VARIABLE_NON_VOLATILE | +EFI_VARIABLE_BOOTSERVICE_ACCESS | +EFI_VARIABLE_RUNTIME_ACCESS , &store_size, + &remaining_size, &var_size); + +if (status !=EFI_SUCCESS ) +return status; + + status =efi_call_phys3 (sys_table->boottime->allocate_pool, +EFI_LOADER_DATA ,sizeof (*efidata), &efidata); + +if (status !=EFI_SUCCESS ) +return status; + + efidata->data.type =SETUP_EFI_VARS ; + efidata->data.len =sizeof (struct efi_var_bootdata ) - +sizeof (struct setup_data ); + efidata->data.next =0 ; + efidata->store_size = store_size; + efidata->remaining_size = remaining_size; + efidata->max_var_size = var_size; + +if (data) + data->next = (unsigned long )efidata; +else + params->hdr.setup_data = (unsigned long )efidata; + +} +static efi_status_t setup_efi_pci (struct boot_params *params) {efi_pci_io_protocol *pci;
@@ -1157,6 +1202,8 @@ struct boot_params *efi_main(void *handle, efi_system_table_t *_table,setup_graphics (boot_params); +setup_efi_vars (boot_params); +setup_efi_pci (boot_params); status = efi_call_phys3(sys_table->boottime->allocate_pool,
arch/x86/include/asm/efi.h +10 -0
@@ -93,6 +93,9 @@ extern void __iomem *efi_ioremap(unsigned long addr, unsigned long size,#endif /* CONFIG_X86_32 */ +extern u64 efi_var_store_size; +extern u64 efi_var_remaining_size; +extern u64 efi_var_max_var_size;extern int add_efi_memmap;extern unsigned long x86_efi_facility;extern void efi_set_executable (efi_memory_desc_t *md,bool executable);
@@ -102,6 +105,13 @@ extern void efi_call_phys_epilog(void);extern void efi_unmap_memmap (void );extern void efi_memory_uc (u64 addr,unsigned long size); +struct efi_var_bootdata { +struct setup_data data; +u64 store_size; +u64 remaining_size; +u64 max_var_size; +}; +#ifdef CONFIG_EFI static inline bool efi_is_native (void )
arch/x86/include/uapi/asm/bootparam.h +1 -0
@@ -6,6 +6,7 @@#define SETUP_E820_EXT 1#define SETUP_DTB 2#define SETUP_PCI 3 +#define SETUP_EFI_VARS 4/* ram_size flags */ #define RAMDISK_IMAGE_START_MASK 0x07FF
arch/x86/platform/efi/efi.c +24 -0
@@ -71,6 +71,13 @@ static efi_system_table_t efi_systab __initdata;unsigned long x86_efi_facility; +u64 efi_var_store_size; +EXPORT_SYMBOL (efi_var_store_size); +u64 efi_var_remaining_size; +EXPORT_SYMBOL (efi_var_remaining_size); +u64 efi_var_max_var_size; +EXPORT_SYMBOL (efi_var_max_var_size); +/* * Returns 1 if 'facility' is enabled, 0 otherwise. */
@@ -682,6 +689,9 @@ void __init efi_init(void)char vendor[100 ] ="unknown" ;int i =0 ;void *tmp; +struct setup_data *data; +struct efi_var_bootdata *efi_var_data; +u64 pa_data;#ifdef CONFIG_X86_32 if (boot_params.efi_info.efi_systab_hi ||
@@ -699,6 +709,20 @@ void __init efi_init(void)if (efi_systab_init (efi_phys.systab))return ; + pa_data = boot_params.hdr.setup_data; +while (pa_data) { + data =early_ioremap (pa_data,sizeof (*efi_var_data)); +if (data->type ==SETUP_EFI_VARS ) { + efi_var_data = (struct efi_var_bootdata *)data; + + efi_var_store_size = efi_var_data->store_size; + efi_var_remaining_size = efi_var_data->remaining_size; + efi_var_max_var_size = efi_var_data->max_var_size; + } + pa_data = data->next; +early_iounmap (data,sizeof (*efi_var_data)); + } +set_bit (EFI_SYSTEM_TABLES , &x86_efi_facility);/*
drivers/firmware/efivars.c +29 -0
@@ -85,6 +85,7 @@#include <linux/ramfs.h> #include <linux/pagemap.h> +#include <asm/efi.h> #include <asm/uaccess.h> #define EFIVARS_VERSION "0.08"
@@ -139,6 +140,8 @@ struct efivar_attribute {static struct efivars __efivars;static struct efivar_operations ops; +static u64 boot_var_size; +static u64 boot_used_size;#define PSTORE_EFI_ATTRIBUTES \ (EFI_VARIABLE_NON_VOLATILE | \
@@ -2002,6 +2005,9 @@ int register_efivars(struct efivars *efivars,efi_char16_t *variable_name;unsigned long variable_name_size =1024 ;int error =0 ; +struct efivar_entry *entry; +struct efi_variable *var; +u64 active_size =0 ; variable_name =kzalloc (variable_name_size,GFP_KERNEL );if (!variable_name) {
@@ -2074,6 +2080,25 @@ int register_efivars(struct efivars *efivars, } }while (status !=EFI_NOT_FOUND ); +if (boot_used_size) { +list_for_each_entry (entry, &efivars->list, list) { + var = &entry->var; + status =get_var_data (efivars, var); + +if (status || + !(var->Attributes &EFI_VARIABLE_NON_VOLATILE )) +continue ; + + active_size += var->DataSize; + active_size +=utf16_strsize (var->VariableName,1024 ); + } + +if (active_size < boot_used_size) + boot_var_size = boot_used_size - active_size; +else + printk(KERN_WARNING FW_BUG "efivars: Inconsistent initial sizes\n"); + } + error =create_efivars_bin_attributes (efivars);if (error)unregister_efivars (efivars);
@@ -2121,6 +2146,10 @@ efivars_init(void) ops.get_next_variable = efi.get_next_variable; ops.query_variable_store = efi_query_variable_store; +#ifdef CONFIG_X86 + boot_used_size = efi_var_store_size - efi_var_remaining_size; +#endif + error =register_efivars (&__efivars, &ops, efi_kobj);if (error) goto err_put;
Signature · 1 line
1.8.1.2
PATCH v4 2/3 Revert "x86, efivars: firmware bug workarounds should be in platform code"
This reverts commit a6e4d5a03e9e3587e88aba687d8f225f4f04c792. Doing this workaround properly requires us to work within the variable code.
--- arch/x86/platform/efi/efi.c | 25 ------------------------- drivers/firmware/efivars.c | 18 +++++++++++++++--- include/linux/efi.h | 9 +-------- 3 files changed, 16 insertions(+), 36 deletions(-)
arch/x86/platform/efi/efi.c +0 -25
@@ -1023,28 +1023,3 @@ u64 efi_mem_attributes(unsigned long phys_addr) }return 0 ; } - -/* -* Some firmware has serious problems when using more than 50% of the EFI -* variable store, i.e. it triggers bugs that can brick machines. Ensure that -* we never use more than this safe limit. -* -* Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable -* store. -*/ -efi_status_t efi_query_variable_store (u32 attributes,unsigned long size) -{ -efi_status_t status; -u64 storage_size, remaining_size, max_size; - - status = efi.query_variable_info (attributes, &storage_size, - &remaining_size, &max_size); -if (status !=EFI_SUCCESS ) -return status; - -if (!storage_size || size > remaining_size || size > max_size || - (remaining_size - size) < (storage_size /2 )) -return EFI_OUT_OF_RESOURCES ; - -return EFI_SUCCESS ; -}
drivers/firmware/efivars.c +15 -3
@@ -439,12 +439,24 @@ static efi_status_t check_var_size_locked(struct efivars *efivars, u32 attributes, unsigned long size) { + u64 storage_size, remaining_size, max_size; +efi_status_t status;const struct efivar_operations *fops = efivars->ops; -if (!efivars->ops->query_variable_store) +if (!efivars->ops->query_variable_info)return EFI_UNSUPPORTED ; -return fops->query_variable_store (attributes, size); + status = fops->query_variable_info (attributes, &storage_size, + &remaining_size, &max_size); + +if (status !=EFI_SUCCESS ) +return status; + +if (!storage_size || size > remaining_size || size > max_size || + (remaining_size - size) < (storage_size /2 )) +return EFI_OUT_OF_RESOURCES ; + +return status; }
@@ -2144,7 +2156,7 @@ efivars_init(void) ops.get_variable = efi.get_variable; ops.set_variable = efi.set_variable; ops.get_next_variable = efi.get_next_variable; - ops.query_variable_store = efi_query_variable_store; + ops.query_variable_info = efi.query_variable_info;#ifdef CONFIG_X86 boot_used_size = efi_var_store_size - efi_var_remaining_size;
include/linux/efi.h +1 -8
@@ -333,7 +333,6 @@ typedef efi_status_t efi_query_capsule_caps_t(efi_capsule_header_t **capsules,unsigned long count, u64 *max_size, int *reset_type); -typedef efi_status_t efi_query_variable_store_t (u32 attributes,unsigned long size);/* * EFI Configuration Table and GUID definitions
@@ -576,15 +575,9 @@ extern void efi_enter_virtual_mode (void); /* switch EFI to virtual mode, if pos#ifdef CONFIG_X86 extern void efi_late_init (void );extern void efi_free_boot_services (void ); -extern efi_status_t efi_query_variable_store (u32 attributes,unsigned long size);#else static inline void efi_late_init (void ) {}static inline void efi_free_boot_services (void ) {} - -static inline efi_status_t efi_query_variable_store (u32 attributes,unsigned long size) -{ -return EFI_SUCCESS ; -}#endif extern void __iomem *efi_lookup_mapped_addr (u64 phys_addr);extern u64 efi_get_iobase (void );
@@ -738,7 +731,7 @@ struct efivar_operations {efi_get_variable_t *get_variable;efi_get_next_variable_t *get_next_variable;efi_set_variable_t *set_variable; -efi_query_variable_store_t *query_variable_store; +efi_query_variable_info_t *query_variable_info; };struct efivars {
Signature · 1 line
1.8.1.2
Re: [PATCH V4 2/3] Revert "x86, efivars: firmware bug workarounds should be in platform code"
Quoted 9 lines · Matthew Garrett
This reverts commit
a6e4d5a03e9e3587e88aba687d8f225f4f04c792. Doing this workaround properly requires us to work within the variable code.--- arch/x86/platform/efi/efi.c | 25 ------------------------- drivers/firmware/efivars.c | 18 +++++++++++++++--- include/linux/efi.h | 9 +-------- 3 files changed, 16 insertions(+), 36 deletions(-)
Does it really? Why can't you just hook into the get_next_variable() and set_variable() functions in arch/x86/platform/efi/efi.c?
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
Re: [PATCH V4 2/3] Revert "x86, efivars: firmware bug workarounds should be in platform code"
Quoted 11 lines · Matt Fleming
Quoted 9 lines · Matthew Garrett
This reverts commit
a6e4d5a03e9e3587e88aba687d8f225f4f04c792. Doing this workaround properly requires us to work within the variable code.Signed-off-by Matthew Garrett <[email protected]>--- arch/x86/platform/efi/efi.c | 25 ------------------------- drivers/firmware/efivars.c | 18 +++++++++++++++--- include/linux/efi.h | 9 +-------- 3 files changed, 16 insertions(+), 36 deletions(-)
Quoted 2 lines
Does it really? Why can't you just hook into the
get_next_variable()andset_variable()functions inarch/x86/platform/efi/efi.c?
struct efi_variable isn't exported from efivars.c, and chunks of the workaround need to be called at efivars init anyway.
Signature · 1 line
Matthew Garrett | [email protected]
PATCH v4 3/3 efi: Distinguish between "remaining space" and actually used space
In reply to Matthew GarrettEFI implementations distinguish between space that is actively used by a variable and space that merely hasn't been garbage collected yet. Space that hasn't yet been garbage collected isn't available for use and so isn't counted in the remaining_space field returned by QueryVariableInfo().
Combined with commit 68d9298 this can cause problems. Some implementations don't garbage collect until the remaining space is smaller than the maximum variable size, and as a result check_var_size() will always fail once more than 50% of the variable store has been used even if most of that space is marked as available for garbage collection. The user is unable to create new variables, and deleting variables doesn't increase the remaining space.
The problem that 68d9298 was attempting to avoid was one where certain platforms fail if the actively used space is greater than 50% of the available storage space. We should be able to calculate that by simply summing the size of each available variable and subtracting that from the total storage space. With luck this will fix the problem described in
https://bugzilla.kernel.org/show_bug.cgi?id=55471 without permitting damage to occur to the machines 68d9298 was attempting to fix.
--- drivers/firmware/efivars.c | 104 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-)
drivers/firmware/efivars.c +101 -3
@@ -104,6 +104,13 @@ MODULE_VERSION(EFIVARS_VERSION);*/ #define GUID_LEN 36 +/* +* There's some additional metadata associated with each +* variable. Intel's reference implementation is 60 bytes - bump that +* to account for potential alignment constraints +*/ +#define VAR_METADATA_SIZE 64 +static bool efivars_pstore_disable =IS_ENABLED (CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE );
@@ -405,6 +412,52 @@ validate_var(struct efi_variable *var, u8 *data, unsigned long len) }static efi_status_t +get_var_size_locked (struct efivars *efivars,struct efi_variable *var) +{ +efi_status_t status; +void *dummy; + + var->DataSize =0 ; + status = efivars->ops->get_variable (var->VariableName, + &var->VendorGuid, + &var->Attributes, + &var->DataSize, + var->Data); + +if (status !=EFI_BUFFER_TOO_SMALL ) +return status; + + dummy =kmalloc (var->DataSize,GFP_ATOMIC ); + + status = efivars->ops->get_variable (var->VariableName, + &var->VendorGuid, + &var->Attributes, + &var->DataSize, + dummy); + +kfree (dummy); + +return status; +} + +static efi_status_t +get_var_size (struct efivars *efivars,struct efi_variable *var) +{ +efi_status_t status; +unsigned long flags; + +spin_lock_irqsave (&efivars->lock, flags); + status =get_var_size_locked (efivars, var); +spin_unlock_irqrestore (&efivars->lock, flags); + +if (status !=EFI_SUCCESS ) { +printk (KERN_WARNING "efivars: Failed to get var size 0x%lx!\n" , + status); + } +return status; +} + +static efi_status_t get_var_data_locked (struct efivars *efivars,struct efi_variable *var) {efi_status_t status;
@@ -415,6 +468,10 @@ get_var_data_locked(struct efivars *efivars, struct efi_variable *var) &var->Attributes, &var->DataSize, var->Data); + +if (status !=EFI_SUCCESS ) + var->DataSize =0 ; +return status; }
@@ -440,8 +497,18 @@ check_var_size_locked(struct efivars *efivars, u32 attributes, unsigned long size) {u64 storage_size, remaining_size, max_size; +struct efivar_entry *entry; +struct efi_variable *var;efi_status_t status;const struct efivar_operations *fops = efivars->ops; +unsigned long active_size =0 ; + +/* +* Any writes other than EFI_VARIABLE_NON_VOLATILE will only hit +* RAM, not flash, so ignore them. +*/ +if (!(attributes &EFI_VARIABLE_NON_VOLATILE )) +return EFI_SUCCESS ;if (!efivars->ops->query_variable_info)return EFI_UNSUPPORTED ;
@@ -452,8 +519,39 @@ check_var_size_locked(struct efivars *efivars, u32 attributes,if (status !=EFI_SUCCESS )return status; -if (!storage_size || size > remaining_size || size > max_size || - (remaining_size - size) < (storage_size /2 )) +list_for_each_entry (entry, &efivars->list, list) { + var = &entry->var; + status =EFI_SUCCESS ; + +if (var->DataSize ==0 ) + status =get_var_size_locked (efivars, var); + +if (status || !(var->Attributes &EFI_VARIABLE_NON_VOLATILE )) +continue ; + + active_size += var->DataSize; + active_size +=utf16_strsize (var->VariableName,1024 ); + active_size +=VAR_METADATA_SIZE ; + } + +/* +* Add on the size of any boot services-only variables +*/ + active_size += boot_var_size; + +/* +* Some firmware implementations refuse to boot if there's insufficient +* space in the variable store. We account for that by refusing the +* write if permitting it would reduce the available space to under +* 50%. However, some firmware won't reclaim variable space until +* after the used (not merely the actively used) space drops below +* a threshold. We can approximate that case with the value calculated +* above. If both the firmware and our calculations indicate that the +* available space would drop below 50%, refuse the write. +*/ +if (!storage_size || size > remaining_size || + ((active_size + size +VAR_METADATA_SIZE > storage_size /2 ) && + (remaining_size - size < storage_size /2 )))return EFI_OUT_OF_RESOURCES ;return status;
@@ -2095,7 +2193,7 @@ int register_efivars(struct efivars *efivars,if (boot_used_size) {list_for_each_entry (entry, &efivars->list, list) { var = &entry->var; - status =get_var_data (efivars, var); + status =get_var_size (efivars, var);if (status || !(var->Attributes &EFI_VARIABLE_NON_VOLATILE ))
Signature · 1 line
1.8.1.2
Quoted 1 line · Matthew Garrett
ops.query_variable_store = efi_query_variable_store;
Can't apply here. ops.query_variable_info = efi.query_variable_info?
Quoted 4 lines · Lingzhu Xiang
Quoted 1 line · Matthew Garrett
ops.query_variable_store = efi_query_variable_store;Can't apply here. ops.query_variable_info = efi.query_variable_info?
It's against the 'urgent' branch at,
git://git.kernel.org/pub/scm/linux/kernel/git/mfleming/efi.git
Quoted 10 lines · Matthew Garrett
EFI variables can be flagged as being accessible only within boot services. This makes it awkward for us to figure out how much space they use at runtime. In theory we could figure this out by simply comparing the results from
QueryVariableInfo()to the space used by all of our variables, but that fails if the platform doesn't garbage collect on every boot. Thankfully, callingQueryVariableInfo()while still inside boot services gives a more reliable answer. This patch passes that information from the EFI boot stub up to the efivars code, letting us calculate a reasonably accurate value.
I just tried this 3.9-rc6 with this patchset with pstore fulling up space and various attempts to manually fulling up space. So far I've been able to delete variables and continue creating variables.
Since QueryVariableInfo is no longer trusted and the accounting is done by the kernel, I'm somewhat concerned that variables can be repeatedly created and deleted until nvram is full of garbage to collect and the firmware hits EFI_OUT_OF_RESOURCES. Could this be any kind of problem?
Lingzhu Xiang
PATCH v5 1/2 efi: Pass boot services variable info to runtime code
In reply to Matthew GarrettEFI variables can be flagged as being accessible only within boot services. This makes it awkward for us to figure out how much space they use at runtime. In theory we could figure this out by simply comparing the results from QueryVariableInfo() to the space used by all of our variables, but that fails if the platform doesn't garbage collect on every boot. Thankfully, calling QueryVariableInfo() while still inside boot services gives a more reliable answer. This patch passes that information from the EFI boot stub up to the efi platform code.
--- arch/x86/boot/compressed/eboot.c | 47 +++++++++++++++++++++++++++++++++++ arch/x86/include/asm/efi.h | 7 ++++++ arch/x86/include/uapi/asm/bootparam.h | 1 + arch/x86/platform/efi/efi.c | 21 ++++++++++++++++ 4 files changed, 76 insertions(+)
arch/x86/boot/compressed/eboot.c +47 -0
@@ -251,6 +251,51 @@ static void find_bits(unsigned long mask, u8 *pos, u8 *size) *size = len; } +static efi_status_t setup_efi_vars (struct boot_params *params) +{ +struct setup_data *data; +struct efi_var_bootdata *efidata; +u64 store_size, remaining_size, var_size; +efi_status_t status; + +if (!sys_table->runtime->query_variable_info) +return EFI_UNSUPPORTED ; + + data = (struct setup_data *)(unsigned long )params->hdr.setup_data; + +while (data && data->next) + data = (struct setup_data *)(unsigned long )data->next; + + status =efi_call_phys4 (sys_table->runtime->query_variable_info, +EFI_VARIABLE_NON_VOLATILE | +EFI_VARIABLE_BOOTSERVICE_ACCESS | +EFI_VARIABLE_RUNTIME_ACCESS , &store_size, + &remaining_size, &var_size); + +if (status !=EFI_SUCCESS ) +return status; + + status =efi_call_phys3 (sys_table->boottime->allocate_pool, +EFI_LOADER_DATA ,sizeof (*efidata), &efidata); + +if (status !=EFI_SUCCESS ) +return status; + + efidata->data.type =SETUP_EFI_VARS ; + efidata->data.len =sizeof (struct efi_var_bootdata ) - +sizeof (struct setup_data ); + efidata->data.next =0 ; + efidata->store_size = store_size; + efidata->remaining_size = remaining_size; + efidata->max_var_size = var_size; + +if (data) + data->next = (unsigned long )efidata; +else + params->hdr.setup_data = (unsigned long )efidata; + +} +static efi_status_t setup_efi_pci (struct boot_params *params) {efi_pci_io_protocol *pci;
@@ -1157,6 +1202,8 @@ struct boot_params *efi_main(void *handle, efi_system_table_t *_table,setup_graphics (boot_params); +setup_efi_vars (boot_params); +setup_efi_pci (boot_params); status = efi_call_phys3(sys_table->boottime->allocate_pool,
arch/x86/include/asm/efi.h +7 -0
@@ -102,6 +102,13 @@ extern void efi_call_phys_epilog(void);extern void efi_unmap_memmap (void );extern void efi_memory_uc (u64 addr,unsigned long size); +struct efi_var_bootdata { +struct setup_data data; +u64 store_size; +u64 remaining_size; +u64 max_var_size; +}; +#ifdef CONFIG_EFI static inline bool efi_is_native (void )
arch/x86/include/uapi/asm/bootparam.h +1 -0
@@ -6,6 +6,7 @@#define SETUP_E820_EXT 1#define SETUP_DTB 2#define SETUP_PCI 3 +#define SETUP_EFI_VARS 4/* ram_size flags */ #define RAMDISK_IMAGE_START_MASK 0x07FF
arch/x86/platform/efi/efi.c +21 -0
@@ -69,6 +69,10 @@ struct efi_memory_map memmap;static struct efi efi_phys__initdata ;static efi_system_table_t efi_systab__initdata ; +static u64 efi_var_store_size; +static u64 efi_var_remaining_size; +static u64 efi_var_max_var_size; +unsigned long x86_efi_facility;/*
@@ -682,6 +686,9 @@ void __init efi_init(void)char vendor[100 ] ="unknown" ;int i =0 ;void *tmp; +struct setup_data *data; +struct efi_var_bootdata *efi_var_data; +u64 pa_data;#ifdef CONFIG_X86_32 if (boot_params.efi_info.efi_systab_hi ||
@@ -699,6 +706,20 @@ void __init efi_init(void)if (efi_systab_init (efi_phys.systab))return ; + pa_data = boot_params.hdr.setup_data; +while (pa_data) { + data =early_ioremap (pa_data,sizeof (*efi_var_data)); +if (data->type ==SETUP_EFI_VARS ) { + efi_var_data = (struct efi_var_bootdata *)data; + + efi_var_store_size = efi_var_data->store_size; + efi_var_remaining_size = efi_var_data->remaining_size; + efi_var_max_var_size = efi_var_data->max_var_size; + } + pa_data = data->next; +early_iounmap (data,sizeof (*efi_var_data)); + } +set_bit (EFI_SYSTEM_TABLES , &x86_efi_facility);/*
Signature · 1 line
1.8.1.2
PATCH v5 2/2 efi: Distinguish between "remaining space" and actually used space
EFI implementations distinguish between space that is actively used by a variable and space that merely hasn't been garbage collected yet. Space that hasn't yet been garbage collected isn't available for use and so isn't counted in the remaining_space field returned by QueryVariableInfo().
Combined with commit 68d9298 this can cause problems. Some implementations don't garbage collect until the remaining space is smaller than the maximum variable size, and as a result check_var_size() will always fail once more than 50% of the variable store has been used even if most of that space is marked as available for garbage collection. The user is unable to create new variables, and deleting variables doesn't increase the remaining space.
The problem that 68d9298 was attempting to avoid was one where certain platforms fail if the actively used space is greater than 50% of the available storage space. We should be able to calculate that by simply summing the size of each available variable and subtracting that from the total storage space. With luck this will fix the problem described in
https://bugzilla.kernel.org/show_bug.cgi?id=55471 without permitting damage to occur to the machines 68d9298 was attempting to fix.
--- arch/x86/platform/efi/efi.c | 109 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 7 deletions(-)
arch/x86/platform/efi/efi.c +102 -7
@@ -41,6 +41,7 @@#include <linux/io.h> #include <linux/reboot.h> #include <linux/bcd.h> +#include <linux/ucs2_string.h> #include <asm/setup.h> #include <asm/efi.h>
@@ -51,6 +52,13 @@#define EFI_DEBUG 1 +/* +* There's some additional metadata associated with each +* variable. Intel's reference implementation is 60 bytes - bump that +* to account for potential alignment constraints +*/ +#define VAR_METADATA_SIZE 64 +struct efi __read_mostly efi = { .mps =EFI_INVALID_TABLE_ADDR , .acpi =EFI_INVALID_TABLE_ADDR ,
@@ -72,6 +80,9 @@ static efi_system_table_t efi_systab __initdata;static u64 efi_var_store_size;static u64 efi_var_remaining_size;static u64 efi_var_max_var_size; +static u64 boot_used_size; +static u64 boot_var_size; +static u64 active_size;unsigned long x86_efi_facility;
@@ -166,8 +177,53 @@ static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) { -return efi_call_virt3 (get_next_variable, - name_size, name, vendor); +efi_status_t status; +static bool finished = false; +static u64 var_size; + + status =efi_call_virt3 (get_next_variable, + name_size, name, vendor); + +if (status ==EFI_NOT_FOUND ) { + finished = true; +if (var_size < boot_used_size) { + boot_var_size = boot_used_size - var_size; + active_size += boot_var_size; + }else { + printk(KERN_WARNING FW_BUG "efi: Inconsistent initial sizes\n"); + } + } + +if (boot_used_size && !finished) { +unsigned long size; +u32 attr; +efi_status_t s; +void *tmp; + + s =virt_efi_get_variable (name, vendor, &attr, &size,NULL ); + +if (s !=EFI_BUFFER_TOO_SMALL || !size) +return status; + + tmp =kmalloc (size,GFP_ATOMIC ); + +if (!tmp) +return status; + + s =virt_efi_get_variable (name, vendor, &attr, &size, tmp); + +if (s ==EFI_SUCCESS && (attr &EFI_VARIABLE_NON_VOLATILE )) { + var_size += size; + var_size +=ucs2_strsize (name,1024 ); + active_size += size; + active_size +=VAR_METADATA_SIZE ; + active_size +=ucs2_strsize (name,1024 ); + } + +kfree (tmp); + } + +return status; } static efi_status_t virt_efi_set_variable(efi_char16_t *name,
@@ -176,9 +232,34 @@ static efi_status_t virt_efi_set_variable(efi_char16_t *name,unsigned long data_size, void *data) { - return efi_call_virt5(set_variable, -name ,vendor ,attr , -data_size ,data ); + efi_status_t status; +u32 orig_attr =0 ; +unsigned long orig_size =0 ; + + status =virt_efi_get_variable (name, vendor, &orig_attr, &orig_size, +NULL ); + +if (status !=EFI_BUFFER_TOO_SMALL ) + orig_size =0 ; + + status =efi_call_virt5 (set_variable, + name, vendor, attr, + data_size, data); + +if (status ==EFI_SUCCESS ) { +if (orig_size) { + active_size -= orig_size; + active_size -=ucs2_strsize (name,1024 ); + active_size -=VAR_METADATA_SIZE ; + } +if (data_size) { + active_size += data_size; + active_size +=ucs2_strsize (name,1024 ); + active_size +=VAR_METADATA_SIZE ; + } + } + +return status; } static efi_status_t virt_efi_query_variable_info(u32 attr,
@@ -720,6 +801,8 @@ void __init efi_init(void)early_iounmap (data,sizeof (*efi_var_data)); } + boot_used_size = efi_var_store_size - efi_var_remaining_size; +set_bit (EFI_SYSTEM_TABLES , &x86_efi_facility);/*
@@ -1039,8 +1122,20 @@ efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)if (status !=EFI_SUCCESS )return status; -if (!storage_size || size > remaining_size || size > max_size || - (remaining_size - size) < (storage_size /2 )) +/* +* Some firmware implementations refuse to boot if there's insufficient +* space in the variable store. We account for that by refusing the +* write if permitting it would reduce the available space to under +* 50%. However, some firmware won't reclaim variable space until +* after the used (not merely the actively used) space drops below +* a threshold. We can approximate that case with the value calculated +* above. If both the firmware and our calculations indicate that the +* available space would drop below 50%, refuse the write. +*/ + +if (!storage_size || size > remaining_size || + ((active_size + size +VAR_METADATA_SIZE > storage_size /2 ) && + (remaining_size - size < storage_size /2 )))return EFI_OUT_OF_RESOURCES ;return EFI_SUCCESS ;
Signature · 1 line
1.8.1.2
Fix UEFI variable paranoia
In reply to Matthew GarrettIdentical to V5, except with the UCS2 helper functions included this time.
PATCH v6 1/3 Move utf16 functions to kernel core and rename
We want to be able to use the utf16 functions that are currently present in the EFI variables code in platform-specific code as well. Move them to the kernel core, and in the process rename them to accurately describe what they do - they don't handle UTF16, only UCS2.
--- drivers/firmware/Kconfig | 1 + drivers/firmware/efivars.c | 80 ++++++++++----------------------------------- include/linux/ucs2_string.h | 14 ++++++++ lib/Kconfig | 3 ++ lib/Makefile | 2 ++ lib/ucs2_string.c | 51 +++++++++++++++++++++++++++++ 6 files changed, 89 insertions(+), 62 deletions(-) create mode 100644 include/linux/ucs2_string.h create mode 100644 lib/ucs2_string.c
drivers/firmware/Kconfig +1 -0
@@ -39,6 +39,7 @@ config FIRMWARE_MEMMAP config EFI_VARS tristate "EFI Variable Support via sysfs" depends on EFI + select UCS2_STRING default n help If you say Y here, you are able to get EFI (Extensible Firmware
drivers/firmware/efivars.c +18 -62
165 lines not on this page · raw message
include/linux/ucs2_string.h new file +14 -0
@@ -0,0 +1,14 @@ +#ifndef _LINUX_UCS2_STRING_H_ +#define _LINUX_UCS2_STRING_H_ + +#include <linux/types.h> /* for size_t */ +#include <linux/stddef.h> /* for NULL */ + +typedef u16 ucs2_char_t ; + +unsigned long ucs2_strnlen (const ucs2_char_t *s,size_t maxlength); +unsigned long ucs2_strlen (const ucs2_char_t *s); +unsigned long ucs2_strsize (const ucs2_char_t *data,unsigned long maxlength); +int ucs2_strncmp (const ucs2_char_t *a,const ucs2_char_t *b,size_t len); + +#endif /* _LINUX_UCS2_STRING_H_ */
lib/Kconfig +3 -0
@@ -404,4 +404,7 @@ config OID_REGISTRY help Enable fast lookup object identifier registry. +config UCS2_STRING + tristate + endmenu
lib/Makefile +2 -0
@@ -174,3 +174,5 @@ quiet_cmd_build_OID_registry = GEN $@ cmd_build_OID_registry = perl $(srctree)/$(src)/build_OID_registry $< $@ clean-files += oid_registry_data.c + +obj-$(CONFIG_UCS2_STRING) += ucs2_string.o
lib/ucs2_string.c new file +51 -0
51 lines not on this page · raw message
Signature · 1 line
1.8.1.2
PATCH v6 2/3 efi: Pass boot services variable info to runtime code
In reply to Matthew GarrettEFI variables can be flagged as being accessible only within boot services. This makes it awkward for us to figure out how much space they use at runtime. In theory we could figure this out by simply comparing the results from QueryVariableInfo() to the space used by all of our variables, but that fails if the platform doesn't garbage collect on every boot. Thankfully, calling QueryVariableInfo() while still inside boot services gives a more reliable answer. This patch passes that information from the EFI boot stub up to the efi platform code.
--- arch/x86/boot/compressed/eboot.c | 47 +++++++++++++++++++++++++++++++++++ arch/x86/include/asm/efi.h | 7 ++++++ arch/x86/include/uapi/asm/bootparam.h | 1 + arch/x86/platform/efi/efi.c | 21 ++++++++++++++++ 4 files changed, 76 insertions(+)
arch/x86/boot/compressed/eboot.c +47 -0
@@ -251,6 +251,51 @@ static void find_bits(unsigned long mask, u8 *pos, u8 *size) *size = len; } +static efi_status_t setup_efi_vars (struct boot_params *params) +{ +struct setup_data *data; +struct efi_var_bootdata *efidata; +u64 store_size, remaining_size, var_size; +efi_status_t status; + +if (!sys_table->runtime->query_variable_info) +return EFI_UNSUPPORTED ; + + data = (struct setup_data *)(unsigned long )params->hdr.setup_data; + +while (data && data->next) + data = (struct setup_data *)(unsigned long )data->next; + + status =efi_call_phys4 (sys_table->runtime->query_variable_info, +EFI_VARIABLE_NON_VOLATILE | +EFI_VARIABLE_BOOTSERVICE_ACCESS | +EFI_VARIABLE_RUNTIME_ACCESS , &store_size, + &remaining_size, &var_size); + +if (status !=EFI_SUCCESS ) +return status; + + status =efi_call_phys3 (sys_table->boottime->allocate_pool, +EFI_LOADER_DATA ,sizeof (*efidata), &efidata); + +if (status !=EFI_SUCCESS ) +return status; + + efidata->data.type =SETUP_EFI_VARS ; + efidata->data.len =sizeof (struct efi_var_bootdata ) - +sizeof (struct setup_data ); + efidata->data.next =0 ; + efidata->store_size = store_size; + efidata->remaining_size = remaining_size; + efidata->max_var_size = var_size; + +if (data) + data->next = (unsigned long )efidata; +else + params->hdr.setup_data = (unsigned long )efidata; + +} +static efi_status_t setup_efi_pci (struct boot_params *params) {efi_pci_io_protocol *pci;
@@ -1157,6 +1202,8 @@ struct boot_params *efi_main(void *handle, efi_system_table_t *_table,setup_graphics (boot_params); +setup_efi_vars (boot_params); +setup_efi_pci (boot_params); status = efi_call_phys3(sys_table->boottime->allocate_pool,
arch/x86/include/asm/efi.h +7 -0
@@ -102,6 +102,13 @@ extern void efi_call_phys_epilog(void);extern void efi_unmap_memmap (void );extern void efi_memory_uc (u64 addr,unsigned long size); +struct efi_var_bootdata { +struct setup_data data; +u64 store_size; +u64 remaining_size; +u64 max_var_size; +}; +#ifdef CONFIG_EFI static inline bool efi_is_native (void )
arch/x86/include/uapi/asm/bootparam.h +1 -0
@@ -6,6 +6,7 @@#define SETUP_E820_EXT 1#define SETUP_DTB 2#define SETUP_PCI 3 +#define SETUP_EFI_VARS 4/* ram_size flags */ #define RAMDISK_IMAGE_START_MASK 0x07FF
arch/x86/platform/efi/efi.c +21 -0
@@ -69,6 +69,10 @@ struct efi_memory_map memmap;static struct efi efi_phys__initdata ;static efi_system_table_t efi_systab__initdata ; +static u64 efi_var_store_size; +static u64 efi_var_remaining_size; +static u64 efi_var_max_var_size; +unsigned long x86_efi_facility;/*
@@ -682,6 +686,9 @@ void __init efi_init(void)char vendor[100 ] ="unknown" ;int i =0 ;void *tmp; +struct setup_data *data; +struct efi_var_bootdata *efi_var_data; +u64 pa_data;#ifdef CONFIG_X86_32 if (boot_params.efi_info.efi_systab_hi ||
@@ -699,6 +706,20 @@ void __init efi_init(void)if (efi_systab_init (efi_phys.systab))return ; + pa_data = boot_params.hdr.setup_data; +while (pa_data) { + data =early_ioremap (pa_data,sizeof (*efi_var_data)); +if (data->type ==SETUP_EFI_VARS ) { + efi_var_data = (struct efi_var_bootdata *)data; + + efi_var_store_size = efi_var_data->store_size; + efi_var_remaining_size = efi_var_data->remaining_size; + efi_var_max_var_size = efi_var_data->max_var_size; + } + pa_data = data->next; +early_iounmap (data,sizeof (*efi_var_data)); + } +set_bit (EFI_SYSTEM_TABLES , &x86_efi_facility);/*
Signature · 1 line
1.8.1.2
Re: [PATCH V6 2/3] efi: Pass boot services variable info to runtime code
Quoted 10 lines · Matthew Garrett
EFI variables can be flagged as being accessible only within boot services. This makes it awkward for us to figure out how much space they use at runtime. In theory we could figure this out by simply comparing the results from
QueryVariableInfo()to the space used by all of our variables, but that fails if the platform doesn't garbage collect on every boot. Thankfully, callingQueryVariableInfo()while still inside boot services gives a more reliable answer. This patch passes that information from the EFI boot stub up to the efi platform code.
This seems to be the patch that has become commit cc5a080c5d40c36089bb08a8a16fa3fc7047fe0f ("efi: Pass boot services variable info to runtime code"), which is part of v3.9-rc8.
It triggers this GCC warning (on Fedora 17, 32 bit):
arch/x86/boot/compressed/eboot.c: In function ‘setup_efi_vars’:
arch/x86/boot/compressed/eboot.c:269:2: warning: passing argument 1 of ‘efi_call_phys’ makes pointer from integer without a cast [enabled by default] In file included from arch/x86/boot/compressed/eboot.c:12:0:
[...]/arch/x86/include/asm/efi.h:8:33: note: expected ‘void *’ but argument is of type ‘long unsigned int’
Quoted 12 lines
arch/x86/boot/compressed/eboot.c +6
-0@@ -251,6 +251,51 @@ static void find_bits(unsigned long mask, u8 *pos, u8 *size) *size = len; } +static efi_status_t setup_efi_vars (struct boot_params *params) +{ +struct setup_data *data; +struct efi_var_bootdata *efidata; +u64 store_size, remaining_size, var_size; +efi_status_t status;
arch/x86/boot/compressed/eboot.c static void find_bits(unsigned long mask, u8 *pos, u8 *size) · line 269
Quoted 10 lines
arch/x86/boot/compressed/eboot.c +10
-0+ +if (!sys_table->runtime->query_variable_info) +return EFI_UNSUPPORTED ; + + data = (struct setup_data *)(unsigned long )params->hdr.setup_data; + +while (data && data->next) + data = (struct setup_data *)(unsigned long )data->next; + + status = efi_call_phys4(sys_table->runtime->query_variable_info,
Casting to void * here makes the warning go away. But the code is sufficiently complicated (for me) to make it unobvious whether that is the right thing to do. Besides, I don't think I have hardware to test this.
Quoted 22 lines
arch/x86/boot/compressed/eboot.c +22
-0+EFI_VARIABLE_NON_VOLATILE | +EFI_VARIABLE_BOOTSERVICE_ACCESS | +EFI_VARIABLE_RUNTIME_ACCESS , &store_size, + &remaining_size, &var_size); + +if (status !=EFI_SUCCESS ) +return status; + + status =efi_call_phys3 (sys_table->boottime->allocate_pool, +EFI_LOADER_DATA ,sizeof (*efidata), &efidata); + +if (status !=EFI_SUCCESS ) +return status; + + efidata->data.type =SETUP_EFI_VARS ; + efidata->data.len =sizeof (struct efi_var_bootdata ) - +sizeof (struct setup_data ); + efidata->data.next =0 ; + efidata->store_size = store_size; + efidata->remaining_size = remaining_size; + efidata->max_var_size = var_size; +
arch/x86/boot/compressed/eboot.c static void find_bits(unsigned long mask, u8 *pos, u8 *size) · line 301
Quoted 10 lines
arch/x86/boot/compressed/eboot.c +7
-0+if (data) + data->next = (unsigned long )efidata; +else + params->hdr.setup_data = (unsigned long )efidata; + +} +static efi_status_t setup_efi_pci (struct boot_params *params) {efi_pci_io_protocol *pci;
Paul Bolle
PATCH v6 3/3 efi: Distinguish between "remaining space" and actually used space
In reply to Matthew GarrettEFI implementations distinguish between space that is actively used by a variable and space that merely hasn't been garbage collected yet. Space that hasn't yet been garbage collected isn't available for use and so isn't counted in the remaining_space field returned by QueryVariableInfo().
Combined with commit 68d9298 this can cause problems. Some implementations don't garbage collect until the remaining space is smaller than the maximum variable size, and as a result check_var_size() will always fail once more than 50% of the variable store has been used even if most of that space is marked as available for garbage collection. The user is unable to create new variables, and deleting variables doesn't increase the remaining space.
The problem that 68d9298 was attempting to avoid was one where certain platforms fail if the actively used space is greater than 50% of the available storage space. We should be able to calculate that by simply summing the size of each available variable and subtracting that from the total storage space. With luck this will fix the problem described in
https://bugzilla.kernel.org/show_bug.cgi?id=55471 without permitting damage to occur to the machines 68d9298 was attempting to fix.
--- arch/x86/platform/efi/efi.c | 109 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 7 deletions(-)
arch/x86/platform/efi/efi.c +102 -7
@@ -41,6 +41,7 @@#include <linux/io.h> #include <linux/reboot.h> #include <linux/bcd.h> +#include <linux/ucs2_string.h> #include <asm/setup.h> #include <asm/efi.h>
@@ -51,6 +52,13 @@#define EFI_DEBUG 1 +/* +* There's some additional metadata associated with each +* variable. Intel's reference implementation is 60 bytes - bump that +* to account for potential alignment constraints +*/ +#define VAR_METADATA_SIZE 64 +struct efi __read_mostly efi = { .mps =EFI_INVALID_TABLE_ADDR , .acpi =EFI_INVALID_TABLE_ADDR ,
@@ -72,6 +80,9 @@ static efi_system_table_t efi_systab __initdata;static u64 efi_var_store_size;static u64 efi_var_remaining_size;static u64 efi_var_max_var_size; +static u64 boot_used_size; +static u64 boot_var_size; +static u64 active_size;unsigned long x86_efi_facility;
@@ -166,8 +177,53 @@ static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) { -return efi_call_virt3 (get_next_variable, - name_size, name, vendor); +efi_status_t status; +static bool finished = false; +static u64 var_size; + + status =efi_call_virt3 (get_next_variable, + name_size, name, vendor); + +if (status ==EFI_NOT_FOUND ) { + finished = true; +if (var_size < boot_used_size) { + boot_var_size = boot_used_size - var_size; + active_size += boot_var_size; + }else { + printk(KERN_WARNING FW_BUG "efi: Inconsistent initial sizes\n"); + } + } + +if (boot_used_size && !finished) { +unsigned long size; +u32 attr; +efi_status_t s; +void *tmp; + + s =virt_efi_get_variable (name, vendor, &attr, &size,NULL ); + +if (s !=EFI_BUFFER_TOO_SMALL || !size) +return status; + + tmp =kmalloc (size,GFP_ATOMIC ); + +if (!tmp) +return status; + + s =virt_efi_get_variable (name, vendor, &attr, &size, tmp); + +if (s ==EFI_SUCCESS && (attr &EFI_VARIABLE_NON_VOLATILE )) { + var_size += size; + var_size +=ucs2_strsize (name,1024 ); + active_size += size; + active_size +=VAR_METADATA_SIZE ; + active_size +=ucs2_strsize (name,1024 ); + } + +kfree (tmp); + } + +return status; } static efi_status_t virt_efi_set_variable(efi_char16_t *name,
@@ -176,9 +232,34 @@ static efi_status_t virt_efi_set_variable(efi_char16_t *name,unsigned long data_size, void *data) { - return efi_call_virt5(set_variable, -name ,vendor ,attr , -data_size ,data ); + efi_status_t status; +u32 orig_attr =0 ; +unsigned long orig_size =0 ; + + status =virt_efi_get_variable (name, vendor, &orig_attr, &orig_size, +NULL ); + +if (status !=EFI_BUFFER_TOO_SMALL ) + orig_size =0 ; + + status =efi_call_virt5 (set_variable, + name, vendor, attr, + data_size, data); + +if (status ==EFI_SUCCESS ) { +if (orig_size) { + active_size -= orig_size; + active_size -=ucs2_strsize (name,1024 ); + active_size -=VAR_METADATA_SIZE ; + } +if (data_size) { + active_size += data_size; + active_size +=ucs2_strsize (name,1024 ); + active_size +=VAR_METADATA_SIZE ; + } + } + +return status; } static efi_status_t virt_efi_query_variable_info(u32 attr,
@@ -720,6 +801,8 @@ void __init efi_init(void)early_iounmap (data,sizeof (*efi_var_data)); } + boot_used_size = efi_var_store_size - efi_var_remaining_size; +set_bit (EFI_SYSTEM_TABLES , &x86_efi_facility);/*
@@ -1039,8 +1122,20 @@ efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)if (status !=EFI_SUCCESS )return status; -if (!storage_size || size > remaining_size || size > max_size || - (remaining_size - size) < (storage_size /2 )) +/* +* Some firmware implementations refuse to boot if there's insufficient +* space in the variable store. We account for that by refusing the +* write if permitting it would reduce the available space to under +* 50%. However, some firmware won't reclaim variable space until +* after the used (not merely the actively used) space drops below +* a threshold. We can approximate that case with the value calculated +* above. If both the firmware and our calculations indicate that the +* available space would drop below 50%, refuse the write. +*/ + +if (!storage_size || size > remaining_size || + ((active_size + size +VAR_METADATA_SIZE > storage_size /2 ) && + (remaining_size - size < storage_size /2 )))return EFI_OUT_OF_RESOURCES ;return EFI_SUCCESS ;
Signature · 1 line
1.8.1.2
PATCH 1/2 x86/Kconfig: Make EFI select UCS2_STRING
The commit "efi: Distinguish between "remaining space" and actually used space" added usage of ucs2_*() functions to arch/x86/platform/efi/efi.c, but the only thing which selected UCS2_STRING was EFI_VARS, which is technically optional and can be built as a module.
--- arch/x86/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/x86/Kconfig +1 -0
@@ -1549,6 +1549,7 @@ config X86_SMAP config EFI bool "EFI runtime service support" depends on ACPI + select UCS2_STRING ---help--- This enables the kernel to use EFI runtime services that are available (such as the EFI variable services).
Signature · 1 line
1.8.1.1
PATCH 2/2 efi: Export efi_query_variable_store() for efivars.ko
Fixes build with CONFIG_EFI_VARS=m which was broken after the commit "x86, efivars: firmware bug workarounds should be in platform code".
--- arch/x86/platform/efi/efi.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/platform/efi/efi.c +1 -0
@@ -1144,3 +1144,4 @@ efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)return EFI_SUCCESS ; } +EXPORT_SYMBOL_GPL (efi_query_variable_store);
Signature · 1 line
1.8.1.1
Matt Fleming
AppliedRe: [PATCH 2/2] efi: Export efi_query_variable_store() for efivars.ko
Quoted 7 lines · Sergey Vlasov
Fixes build with
CONFIG_EFI_VARS=mwhich was broken after the commit "x86, efivars: firmware bug workarounds should be in platform code".Signed-off-by Sergey Vlasov <vsu-u2l5PoMzF/[email protected]>--- arch/x86/platform/efi/efi.c | 1 + 1 file changed, 1 insertion(+)
Applied, thanks!
Matt Fleming
AppliedRe: [PATCH 1/2] x86/Kconfig: Make EFI select UCS2_STRING
In reply to Sergey VlasovQuoted 9 lines · Sergey Vlasov
The commit "efi: Distinguish between "remaining space" and actually used space" added usage of ucs2_*() functions to
arch/x86/platform/efi/efi.c, but the only thing which selectedUCS2_STRINGwasEFI_VARS, which is technically optional and can be built as a module.Signed-off-by Sergey Vlasov <[email protected]>--- arch/x86/Kconfig | 1 + 1 file changed, 1 insertion(+)
Applied, thanks!
Re: [PATCH V6 3/3] efi: Distinguish between "remaining space" and actually used space
In reply to Matthew GarrettQuoted 69 lines · Matthew Garrett
EFI implementations distinguish between space that is actively used by a variable and space that merely hasn't been garbage collected yet. Space that hasn't yet been garbage collected isn't available for use and so isn't counted in the
remaining_spacefield returned byQueryVariableInfo().Combined with commit 68d9298 this can cause problems. Some implementations don't garbage collect until the remaining space is smaller than the maximum variable size, and as a result
check_var_size()will always fail once more than 50% of the variable store has been used even if most of that space is marked as available for garbage collection. The user is unable to create new variables, and deleting variables doesn't increase the remaining space.The problem that 68d9298 was attempting to avoid was one where certain platforms fail if the actively used space is greater than 50% of the available storage space. We should be able to calculate that by simply summing the size of each available variable and subtracting that from the total storage space. With luck this will fix the problem described in
https://bugzilla.kernel.org/show_bug.cgi?id=55471 without permitting damage to occur to the machines 68d9298 was attempting to fix.--- arch/x86/platform/efi/efi.c | 109 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 7 deletions(-)arch/x86/platform/efi/efi.c +13
-2@@ -41,6 +41,7 @@#include <linux/io.h> #include <linux/reboot.h> #include <linux/bcd.h> +#include <linux/ucs2_string.h> #include <asm/setup.h> #include <asm/efi.h> @@ -51,6 +52,13 @@#define EFI_DEBUG 1 +/* +* There's some additional metadata associated with each +* variable. Intel's reference implementation is 60 bytes - bump that +* to account for potential alignment constraints +*/ +#define VAR_METADATA_SIZE 64 +struct efi __read_mostly efi = { .mps =EFI_INVALID_TABLE_ADDR , .acpi =EFI_INVALID_TABLE_ADDR ,@@ -72,6 +80,9 @@ static efi_system_table_t efi_systab __initdata;static u64 efi_var_store_size;static u64 efi_var_remaining_size;static u64 efi_var_max_var_size; +static u64 boot_used_size; +static u64 boot_var_size; +static u64 active_size;unsigned long x86_efi_facility;@@ -166,8 +177,53 @@ static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) { -return efi_call_virt3 (get_next_variable, - name_size, name, vendor); +efi_status_t status; +static bool finished = false;
arch/x86/platform/efi/efi.c static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, · line 191
Quoted 10 lines
arch/x86/platform/efi/efi.c +10
-0+static u64 var_size; + + status =efi_call_virt3 (get_next_variable, + name_size, name, vendor); + +if (status ==EFI_NOT_FOUND ) { + finished = true; +if (var_size < boot_used_size) { + boot_var_size = boot_used_size - var_size; + active_size += boot_var_size;
Doesn't work sometimes. Here, if garbage is not collected, then boot_used_size will contain the size of garbage, and get added into active_size. This defeats the purpose of active_size.
I added a big printk before "return EFI_OUT_OF_RESOURCES", here are the results (Dell XPS 8500, Secure Boot capable):
Failed to create variables in efivarfs, printk:
size=22
storage_size=131072
remaining_size=5230
max_size=5204
efi_var_store_size=131072
efi_var_remaining_size=5378
efi_var_max_var_size=5352
boot_used_size=125694
boot_var_size=125694
active_size=125694
After a few reboots,
EFI shell, QueryVariableInfo.efi:
MaximumVariableStorageSize=131072
RemainingVariableStorageSize=102113
MaximumVariableSize=65509
After several more pstore crash dumps, printk:
size=22
storage_size=131072
remaining_size=53064
max_size=53038
efi_var_store_size=131072
efi_var_remaining_size=53212
efi_var_max_var_size=53186
boot_used_size=77860
boot_var_size=77860
active_size=77860
After reboot, EFI shell, QueryVariableInfo.efi:
MaximumVariableStorageSize=131072
RemainingVariableStorageSize=50456
MaximumVariableSize=50430
-> reset ...
RemainingVariableStorageSize=47922
MaximumVariableSize=47896
-> reset ...
RemainingVariableStorageSize=45462
MaximumVariableSize=45436
-> reset ...
RemainingVariableStorageSize=43002
MaximumVariableSize=42976
-> reset ...
RemainingVariableStorageSize=40542
MaximumVariableSize=40516
Each reboot will consume some nvram? This is consistent with
http://article.gmane.org/gmane.linux.kernel.stable/47156
Re: [PATCH V6 3/3] efi: Distinguish between "remaining space" and actually used space
In reply to Matthew GarrettHi all,
於 一,2013-04-15 於 13:09 -0700,Matthew Garrett 提到:
Quoted 29 lines
EFI implementations distinguish between space that is actively used by a variable and space that merely hasn't been garbage collected yet. Space that hasn't yet been garbage collected isn't available for use and so isn't counted in the
remaining_spacefield returned byQueryVariableInfo().Combined with commit 68d9298 this can cause problems. Some implementations don't garbage collect until the remaining space is smaller than the maximum variable size, and as a result
check_var_size()will always fail once more than 50% of the variable store has been used even if most of that space is marked as available for garbage collection. The user is unable to create new variables, and deleting variables doesn't increase the remaining space.The problem that 68d9298 was attempting to avoid was one where certain platforms fail if the actively used space is greater than 50% of the available storage space. We should be able to calculate that by simply summing the size of each available variable and subtracting that from the total storage space. With luck this will fix the problem described in
https://bugzilla.kernel.org/show_bug.cgi?id=55471 without permitting damage to occur to the machines 68d9298 was attempting to fix.--- arch/x86/platform/efi/efi.c | 109 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 7 deletions(-)arch/x86/platform/efi/efi.c +0
-0
...
Quoted 10 lines
arch/x86/platform/efi/efi.c +4
-2@@ -1039,8 +1122,20 @@ efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)if (status !=EFI_SUCCESS )return status; -if (!storage_size || size > remaining_size || size > max_size || - (remaining_size - size) < (storage_size /2 )) +/* +* Some firmware implementations refuse to boot if there's insufficient +* space in the variable store. We account for that by refusing the +* write if permitting it would reduce the available space to under
arch/x86/platform/efi/efi.c efi_status_t efi_query_variable_store(u32 attributes, unsigned long size) · line 1138
Quoted 10 lines
arch/x86/platform/efi/efi.c +10
-0+* 50%. However, some firmware won't reclaim variable space until +* after the used (not merely the actively used) space drops below +* a threshold. We can approximate that case with the value calculated +* above. If both the firmware and our calculations indicate that the +* available space would drop below 50%, refuse the write. +*/ + +if (!storage_size || size > remaining_size || + ((active_size + size +VAR_METADATA_SIZE > storage_size /2 ) && + (remaining_size - size < storage_size /2 )))
I am afraid it could not completely avoid to brick Samsung machines when binding active_size and remaining_size logic with AND.
I don't have machine for testing, but my concern is we can run delete/create variables many cycles at runtime for trigger brick Samsung machines.
It causes the garbage size increased and remaining_size decreased. But we still can create new variable because active_size doesn't increase due to we delete variable before create new. So, the condition "remaining_size - size < storage_size / 2" will not really hit because active_size condition is pass.
And, here also can not use OR for binding active_size and remaining_size logic, that causes many machines will not trigger garbage collection because remaining_size will never tight.
Please let me know if I lost any other conditions or background information.
Thanks a lot!
Joey Lee
Re: [PATCH V6 3/3] efi: Distinguish between "remaining space" and actually used space
Quoted 5 lines · joeyli
It causes the garbage size increased and
remaining_sizedecreased. But we still can create new variable becauseactive_sizedoesn't increase due to we delete variable before create new. So, the condition "remaining_size- size <storage_size/ 2" will not really hit becauseactive_sizecondition is pass.
That's fine - the (limited) information we have from Samsung is that there's no problem if all the space is dirty, only if all the space is marked as active.
Signature · 1 line
Matthew Garrett | [email protected]
Re: [PATCH V6 3/3] efi: Distinguish between "remaining space" and actually used space
於 三,2013-04-24 於 10:14 +0000,Matthew Garrett 提到:
Quoted 8 lines · joeyli
Quoted 6 lines
It causes the garbage size increased and
remaining_sizedecreased. But we still can create new variable becauseactive_sizedoesn't increase due to we delete variable before create new. So, the condition "remaining_size- size <storage_size/ 2" will not really hit becauseactive_sizecondition is pass.
Quoted 6 lines
That's fine - the (limited) information we have from Samsung is that there's no problem if all the space is dirty, only if all the space is marked as active.
Signature · 1 line
Matthew Garrett | [email protected]
Got it! Thanks for your explanation!
Then why we don't just remove the "remaining_size" condition but only monitor the active_size should not larger then 1/2 storage_size?
Does that because we allow one and only one VAR_METADATA_SIZE beyond the 1/2 storage? If so, why we don't just give ( 1/2 storage_size + VAR_METADATA_SIZ) space for active_size using?
Thanks a lot!
Joey Lee
Re: [PATCH V6 3/3] efi: Distinguish between "remaining space" and actually used space
Quoted 2 lines · joeyli
Then why we don't just remove the "
remaining_size" condition but only monitor theactive_sizeshould not larger then 1/2storage_size?
If we calculate active_size as using more than 50% of the storage space but remaining_size says we still have more than 50% available, we should trust remaining_size instead of active_size.
Signature · 1 line
Matthew Garrett | [email protected]
Re: [PATCH V6 3/3] efi: Distinguish between "remaining space" and actually used space
於 三,2013-04-24 於 11:57 +0000,Matthew Garrett 提到:
Quoted 11 lines · joeyli
Quoted 3 lines
Then why we don't just remove the "
remaining_size" condition but only monitor theactive_sizeshould not larger then 1/2storage_size?If we calculate
active_sizeas using more than 50% of the storage space butremaining_sizesays we still have more than 50% available, we should trustremaining_sizeinstead ofactive_size.Signature · 1 line
Matthew Garrett | [email protected]
That makes sense because BIOS base on remaining_size to trigger garbage collection.
Thanks for your clarification!
Joey Lee
Re: Fix UEFI variable paranoia
In reply to Matthew GarrettQuoted 1 line · Matthew Garrett
Identical to V5, except with the UCS2 helper functions included this time.
Thanks these looks good to me. I've applied all three patches to the 'urgent' branch.
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center