Skip to contentSkip to conversation

efi: move noefi early param code out of x86 arch code

PATCH In review Dave Young 4 participants 12 messages Aug 12, 2014 – Aug 13, 2014

Subsystems (6)
Documentation x86 Library code arm64 +2 more subsystems
lore
Share & tools
lore

Dave Young

author
#0 lore raw

PATCH 1/5 efi: move noefi early param code out of x86 arch code

noefi param can be used for arches other than X86 later, thus move it out of x86 platform code.

---
 arch/x86/platform/efi/efi.c | 10 +---------
 drivers/firmware/efi/efi.c  | 13 +++++++++++++
 include/linux/efi.h         |  1 +
 3 files changed, 15 insertions(+), 9 deletions(-)
arch/x86/platform/efi/efi.c +1 -9
index 850da94..dd7aaa1 100644
@@ -70,14 +70,6 @@ static efi_config_table_type_t arch_tables[] __initdata = {
 
 u64 efi_setup;		/* efi setup_data physical address */
 
-static bool disable_runtime __initdata = false;
-static int __init setup_noefi(char *arg)
-{
-	disable_runtime = true;
-	return 0;
-}
-early_param("noefi", setup_noefi);
-
 int add_efi_memmap;
 EXPORT_SYMBOL(add_efi_memmap);
 
@@ -492,7 +484,7 @@ void __init efi_init(void)
 	if (!efi_runtime_supported())
 		pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
 	else {
-		if (disable_runtime || efi_runtime_init())
+		if (efi_runtime_disabled() || efi_runtime_init())
 			return;
 	}
 	if (efi_memmap_init())
drivers/firmware/efi/efi.c +13 -0
index 64ecbb5..c8f01a7 100644
@@ -41,6 +41,19 @@ struct efi __read_mostly efi = {
 };
 EXPORT_SYMBOL(efi);
 
+static bool disable_runtime;
+static int __init setup_noefi(char *arg)
+{
+	disable_runtime = true;
+	return 0;
+}
+early_param("noefi", setup_noefi);
+
+bool efi_runtime_disabled(void)
+{
+	return disable_runtime;
+}
+
 static struct kobject *efi_kobj;
 static struct kobject *efivars_kobj;
 
include/linux/efi.h +1 -0
index 45cb4ff..06d7c03 100644
@@ -1227,4 +1227,5 @@ efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
 				  unsigned long *load_addr,
 				  unsigned long *load_size);
 
+bool efi_runtime_disabled(void);
 #endif /* _LINUX_EFI_H */
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 2/5 efi: add kernel param efi=noruntime

noefi kernel param means actually disabling efi runtime, Per suggestion from Leif Lindholm efi=noruntime should be better. But since noefi is already used in X86 thus just adding another param efi=noruntime for same purpose.

---
 Documentation/kernel-parameters.txt |  4 +++-
 arch/x86/platform/efi/efi.c         | 12 ------------
 drivers/firmware/efi/efi.c          | 22 ++++++++++++++++++++++
 3 files changed, 25 insertions(+), 13 deletions(-)
Documentation/kernel-parameters.txt +3 -1
index a8eb6af..09d8c54 100644
@@ -992,11 +992,13 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			Format: {"off" | "on" | "skip[mbr]"}
 
 	efi=		[EFI]
-			Format: { "old_map" }
+			Format: { "old_map" | "noruntime" }
 			old_map [X86-64]: switch to the old ioremap-based EFI
 			runtime services mapping. 32-bit still uses this one by
 			default.
 
+			noruntime : disable EFI runtime services support
+
 	efi_no_storage_paranoia [EFI; X86]
 			Using this parameter you can use more than 50% of
 			your efi variable storage. Use this parameter only if
arch/x86/platform/efi/efi.c +0 -12
index dd7aaa1..eae91be 100644
@@ -929,15 +929,3 @@ u64 efi_mem_attributes(unsigned long phys_addr)
 	}
 	return 0;
 }
-
-static int __init parse_efi_cmdline(char *str)
-{
-	if (*str == '=')
-		str++;
-
-	if (!strncmp(str, "old_map", 7))
-		set_bit(EFI_OLD_MEMMAP, &efi.flags);
-
-	return 0;
-}
-early_param("efi", parse_efi_cmdline);
drivers/firmware/efi/efi.c +22 -0
index c8f01a7..f3ec3f2 100644
@@ -24,6 +24,7 @@
 #include <linux/of_fdt.h>
 #include <linux/io.h>
 #include <linux/platform_device.h>
+#include <asm/efi.h>
 
 struct efi __read_mostly efi = {
 	.mps        = EFI_INVALID_TABLE_ADDR,
@@ -54,6 +55,27 @@ bool efi_runtime_disabled(void)
 	return disable_runtime;
 }
 
+static int __init parse_efi_cmdline(char *str)
+{
+	if (*str == '=')
+		str++;
+
+#ifdef CONFIG_X86
+	if (!strncmp(str, "old_map", 7)) {
+		set_bit(EFI_OLD_MEMMAP, &efi.flags);
+		goto out;
+	}
+#endif
+	if (!strncmp(str, "noruntime", 9)) {
+		disable_runtime = true;
+		goto out;
+	}
+
+out:
+	return 0;
+}
+early_param("efi", parse_efi_cmdline);
+
 static struct kobject *efi_kobj;
 static struct kobject *efivars_kobj;
 
Signature · 1 line
1.8.3.1
author
#4 lore raw

PATCH 3/5 efi arm64: do not enter virtual mode in case booting with efi=noruntime or noefi

In reply to Dave Young

In case efi runtime disabled via noefi kernel cmdline arm64_enter_virtual_mode should error out.

At the same time move early_memunmap(memmap.map, mapsize) to the beginning of the function or it will leak early mem.

---
 arch/arm64/kernel/efi.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/arm64/kernel/efi.c +4 -3
index e72f310..324cdd1 100644
@@ -386,7 +386,10 @@ static int __init arm64_enter_virtual_mode(void)
 	int count = 0;
 	unsigned long flags;
 
-	if (!efi_enabled(EFI_BOOT)) {
+	mapsize = memmap.map_end - memmap.map;
+	early_memunmap(memmap.map, mapsize);
+
+	if (!efi_enabled(EFI_BOOT) || efi_runtime_disabled()) {
 		pr_info("EFI services will not be available.\n");
 		return -1;
 	}
@@ -394,8 +397,6 @@ static int __init arm64_enter_virtual_mode(void)
 	pr_info("Remapping and enabling EFI services.\n");
 
 	/* replace early memmap mapping with permanent mapping */
-	mapsize = memmap.map_end - memmap.map;
-	early_memunmap(memmap.map, mapsize);
 	memmap.map = (__force void *)ioremap_cache((phys_addr_t)memmap.phys_map,
 						   mapsize);
 	memmap.map_end = memmap.map + mapsize;
Signature · 1 line
1.8.3.1
author
#7 lore raw

PATCH 4/5 efi x86: clear EFI_RUNTIME_SERVICES bit in case failures other than SetVirtualAddressMap

In reply to Dave Young

If enter virtual mode failed due to some reason other than the efi call the EFI_RUNTIME_SERVICES bit in efi.flags should be cleared thus users of efi runtime services can check the bit and handle the case instead of assume efi runtime is ok.

Per Matt, if efi call SetVirtualAddressMap fails we will be not sure it's safe to make any assumptions about the state of the system. so kernel panics instead of clears EFI_RUNTIME_SERVICES bit.

---
 arch/x86/platform/efi/efi.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
arch/x86/platform/efi/efi.c +8 -2
index eae91be..c42f49b 100644
@@ -724,6 +724,7 @@ static void __init kexec_enter_virtual_mode(void)
 	 */
 	if (!efi_is_native()) {
 		efi_unmap_memmap();
+		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 		return;
 	}
 
@@ -797,6 +798,7 @@ static void __init __efi_enter_virtual_mode(void)
 	new_memmap = efi_map_regions(&count, &pg_shift);
 	if (!new_memmap) {
 		pr_err("Error reallocating memory, EFI runtime non-functional!\n");
+		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 		return;
 	}
 
@@ -804,8 +806,10 @@ static void __init __efi_enter_virtual_mode(void)
 
 	BUG_ON(!efi.systab);
 
-	if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
+	if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift)) {
+		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 		return;
+	}
 
 	efi_sync_low_kernel_mappings();
 	efi_dump_pagetable();
@@ -882,8 +886,10 @@ static void __init __efi_enter_virtual_mode(void)
 
 void __init efi_enter_virtual_mode(void)
 {
-	if (efi_enabled(EFI_PARAVIRT))
+	if (efi_enabled(EFI_PARAVIRT)) {
+		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 		return;
+	}
 
 	if (efi_setup)
 		kexec_enter_virtual_mode();
Signature · 1 line
1.8.3.1
author
#9 lore raw

PATCH 5/5 efi_rtc: probe function error out in case no efi runtime enabled

In reply to Dave Young

efi rtc depends on efi runtime services, so if efi runtime services are not usable it should error out.

Without this patch rtc-efi will panic with 'noefi' boot

---
 drivers/rtc/rtc-efi.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/rtc/rtc-efi.c +3 -0
index 8225b89..8c02ed8 100644
@@ -213,6 +213,9 @@ static int __init efi_rtc_probe(struct platform_device *dev)
 {
 	struct rtc_device *rtc;
 
+	if (!efi_enabled(EFI_RUNTIME_SERVICES))
+		return -ENODEV;
+
 	rtc = devm_rtc_device_register(&dev->dev, "rtc-efi", &efi_rtc_ops,
 					THIS_MODULE);
 	if (IS_ERR(rtc))
Signature · 1 line
1.8.3.1
reviewer
#10 lore raw
In reply to Dave Young
Quoted 9 lines · Dave Young

noefi param can be used for arches other than X86 later, thus move it out of x86 platform code.

---
 arch/x86/platform/efi/efi.c | 10 +---------
 drivers/firmware/efi/efi.c  | 13 +++++++++++++
 include/linux/efi.h         |  1 +
 3 files changed, 15 insertions(+), 9 deletions(-)

This line in Documentation/kernel-parameters.txt needs to be updated:

noefi [X86] Disable EFI runtime services support.

Quoted 63 lines
arch/x86/platform/efi/efi.c +1 -9
index 850da94..dd7aaa1 100644
@@ -70,14 +70,6 @@ static efi_config_table_type_t arch_tables[] __initdata = {
 
 u64 efi_setup;		/* efi setup_data physical address */
 
-static bool disable_runtime __initdata = false;
-static int __init setup_noefi(char *arg)
-{
-	disable_runtime = true;
-	return 0;
-}
-early_param("noefi", setup_noefi);
-
 int add_efi_memmap;
 EXPORT_SYMBOL(add_efi_memmap);
 
@@ -492,7 +484,7 @@ void __init efi_init(void)
 	if (!efi_runtime_supported())
 		pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
 	else {
-		if (disable_runtime || efi_runtime_init())
+		if (efi_runtime_disabled() || efi_runtime_init())
 			return;
 	}
 	if (efi_memmap_init())
drivers/firmware/efi/efi.c +13 -0
index 64ecbb5..c8f01a7 100644
@@ -41,6 +41,19 @@ struct efi __read_mostly efi = {
 };
 EXPORT_SYMBOL(efi);
 
+static bool disable_runtime;
+static int __init setup_noefi(char *arg)
+{
+	disable_runtime = true;
+	return 0;
+}
+early_param("noefi", setup_noefi);
+
+bool efi_runtime_disabled(void)
+{
+	return disable_runtime;
+}
+
 static struct kobject *efi_kobj;
 static struct kobject *efivars_kobj;
 
include/linux/efi.h +1 -0
index 45cb4ff..06d7c03 100644
@@ -1227,4 +1227,5 @@ efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
 				  unsigned long *load_addr,
 				  unsigned long *load_size);
 
+bool efi_runtime_disabled(void);
 #endif /* _LINUX_EFI_H */
Signature · 1 line
~Randy
author
#11 lore raw
Quoted 11 lines · Randy Dunlap
Quoted 9 lines · Dave Young

noefi param can be used for arches other than X86 later, thus move it out of x86 platform code.

---
 arch/x86/platform/efi/efi.c | 10 +---------
 drivers/firmware/efi/efi.c  | 13 +++++++++++++
 include/linux/efi.h         |  1 +
 3 files changed, 15 insertions(+), 9 deletions(-)
Quoted 3 lines

This line in Documentation/kernel-parameters.txt needs to be updated:

noefi [X86] Disable EFI runtime services support.

Randy, thanks

Will update the documentation.

Dave

#5 lore raw

Re: [PATCH 3/5] efi arm64: do not enter virtual mode in case booting with efi=noruntime or noefi

In reply to Dave Young
Quoted 16 lines · Dave Young

In case efi runtime disabled via noefi kernel cmdline arm64_enter_virtual_mode should error out.

At the same time move early_memunmap(memmap.map, mapsize) to the beginning of the function or it will leak early mem.

---
 arch/arm64/kernel/efi.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/arm64/kernel/efi.c +0 -0
index e72f310..324cdd1 100644

arch/arm64/kernel/efi.c static int __init arm64_enter_virtual_mode(void) · line 392

Quoted 8 lines
arch/arm64/kernel/efi.c +4 -1
@@ -386,7 +386,10 @@ static int __init arm64_enter_virtual_mode(void)
 	int count = 0;
 	unsigned long flags;
 
-	if (!efi_enabled(EFI_BOOT)) {
+	mapsize = memmap.map_end - memmap.map;
+	early_memunmap(memmap.map, mapsize);
+
+	if (!efi_enabled(EFI_BOOT) || efi_runtime_disabled()) {

Hmm, is this right? We only set EFI_BOOT if we find EFI parameters in the DT (see efi_init -> uefi_init), so you run the risk of unmapping something we never mapped here.

Furthermore, there seems to be a leak in uefi_init anyway, as we return -EINVAL if the signature of the EFI table doesn't match without unmapping it first.

Will

author
#6 lore raw

Re: [PATCH 3/5] efi arm64: do not enter virtual mode in case booting with efi=noruntime or noefi

Quoted 20 lines · Will Deacon
Quoted 19 lines · Dave Young

In case efi runtime disabled via noefi kernel cmdline arm64_enter_virtual_mode should error out.

At the same time move early_memunmap(memmap.map, mapsize) to the beginning of the function or it will leak early mem.

---
 arch/arm64/kernel/efi.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/arm64/kernel/efi.c +0 -0
index e72f310..324cdd1 100644
@@ -386,7 +386,10 @@ static int __init arm64_enter_virtual_mode(void)
 	int count = 0;
 	unsigned long flags;
 
Quoted 9 lines

arch/arm64/kernel/efi.c static int __init arm64_enter_virtual_mode(void) · line 392

Quoted 5 lines
arch/arm64/kernel/efi.c +4 -1
-	if (!efi_enabled(EFI_BOOT)) {
+	mapsize = memmap.map_end - memmap.map;
+	early_memunmap(memmap.map, mapsize);
+
+	if (!efi_enabled(EFI_BOOT) || efi_runtime_disabled()) {

Hmm, is this right? We only set EFI_BOOT if we find EFI parameters in the DT (see efi_init -> uefi_init), so you run the risk of unmapping something we never mapped here.

Good catch, I missed the case. will fix it in next update

Quoted 4 lines

Furthermore, there seems to be a leak in uefi_init anyway, as we return -EINVAL if the signature of the EFI table doesn't match without unmapping it first.

Yes, will add a new patch to fix the error handling in uefi_init.

BTW, Matt, I forgot to cc Xen/SGI people, will send an update soon and cc them.

Thanks
Dave

author
#2 lore raw

Re: [PATCH 2/5] efi: add kernel param efi=noruntime

In reply to Dave Young
Quoted 72 lines · Dave Young

noefi kernel param means actually disabling efi runtime, Per suggestion from Leif Lindholm efi=noruntime should be better. But since noefi is already used in X86 thus just adding another param efi=noruntime for same purpose.

---
 Documentation/kernel-parameters.txt |  4 +++-
 arch/x86/platform/efi/efi.c         | 12 ------------
 drivers/firmware/efi/efi.c          | 22 ++++++++++++++++++++++
 3 files changed, 25 insertions(+), 13 deletions(-)
Documentation/kernel-parameters.txt +3 -1
index a8eb6af..09d8c54 100644
@@ -992,11 +992,13 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			Format: {"off" | "on" | "skip[mbr]"}
 
 	efi=		[EFI]
-			Format: { "old_map" }
+			Format: { "old_map" | "noruntime" }
 			old_map [X86-64]: switch to the old ioremap-based EFI
 			runtime services mapping. 32-bit still uses this one by
 			default.
 
+			noruntime : disable EFI runtime services support
+
 	efi_no_storage_paranoia [EFI; X86]
 			Using this parameter you can use more than 50% of
 			your efi variable storage. Use this parameter only if
arch/x86/platform/efi/efi.c +0 -12
index dd7aaa1..eae91be 100644
@@ -929,15 +929,3 @@ u64 efi_mem_attributes(unsigned long phys_addr)
 	}
 	return 0;
 }
-
-static int __init parse_efi_cmdline(char *str)
-{
-	if (*str == '=')
-		str++;
-
-	if (!strncmp(str, "old_map", 7))
-		set_bit(EFI_OLD_MEMMAP, &efi.flags);
-
-	return 0;
-}
-early_param("efi", parse_efi_cmdline);
drivers/firmware/efi/efi.c +7 -0
index c8f01a7..f3ec3f2 100644
@@ -24,6 +24,7 @@
 #include <linux/of_fdt.h>
 #include <linux/io.h>
 #include <linux/platform_device.h>
+#include <asm/efi.h>
 
 struct efi __read_mostly efi = {
 	.mps        = EFI_INVALID_TABLE_ADDR,
@@ -54,6 +55,27 @@ bool efi_runtime_disabled(void)
 	return disable_runtime;
 }
 
+static int __init parse_efi_cmdline(char *str)
+{
+	if (*str == '=')
+		str++;
+
+#ifdef CONFIG_X86

drivers/firmware/efi/efi.c bool efi_runtime_disabled(void) · line 73

Quoted 10 lines
drivers/firmware/efi/efi.c +10 -0
+	if (!strncmp(str, "old_map", 7)) {
+		set_bit(EFI_OLD_MEMMAP, &efi.flags);
+		goto out;
+	}
+#endif
+	if (!strncmp(str, "noruntime", 9)) {
+		disable_runtime = true;
+		goto out;
+	}
+

For sharing functions I moved old_map here, but rethinking about it maybe it's better to keep it in arch code.

Matt, I see you have some code for libstub option parsing, I will rebase on your efi/next and I'm thinking to add a generic function for parsing params like below (for old_map, but libstub can not use it):

arch/x86/platform/efi/efi.c +2 -20
index c90d3cd..c73a7df5 100644
@@ -932,26 +932,8 @@ u64 efi_mem_attributes(unsigned long phys_addr)
 
 static int __init parse_efi_cmdline(char *str)
 {
-	if (*str == '=')
-		str++;
-
-	while (*str) {
-		if (!strncmp(str, "old_map", 7)) {
-			set_bit(EFI_OLD_MEMMAP, &efi.flags);
-			str += strlen("old_map");
-		}
-
-		/*
-		 * Skip any options we don't understand. Presumably
-		 * they apply to the EFI boot stub.
-		 */
-		while (*str && *str != ',')
-			str++;
-
-		/* If we hit a delimiter, skip it */
-		if (*str == ',')
-			str++;
-	}
+	if (parse_option_str(str, "old_map"))
+		set_bit(EFI_OLD_MEMMAP, &efi.flags);
 
 	return 0;
 }
include/linux/kernel.h +1 -0
index 4c52907..d612718 100644
@@ -407,6 +407,7 @@ int vsscanf(const char *, const char *, va_list);
 extern int get_option(char **str, int *pint);
 extern char *get_options(const char *str, int nints, int *ints);
 extern unsigned long long memparse(const char *ptr, char **retptr);
+extern bool parse_option_str(const char *str, const char *option);
 
 extern int core_kernel_text(unsigned long addr);
 extern int core_kernel_data(unsigned long addr);
lib/cmdline.c +29 -0
index d4932f7..f384125 100644
@@ -155,3 +155,32 @@ unsigned long long memparse(const char *ptr, char **retptr)
 	return ret;
 }
 EXPORT_SYMBOL(memparse);
+
+/**
+ *	parse_option_str - Parse a string and check an option is set or not 
+ *	@str: String to be parsed
+ *	@option: the option name
+ *
+ *	This function parses a string containing a comma-separated
+ *	list of strings like a=b,c.
+ *
+ *	Return true if there's such option in the string, or return false.
+ */
++bool parse_option_str(const char *str, const char *option)
+{
+	while (*str) {
+		if (!strncmp(str, option, strlen(option))) {
+			str += strlen(option);
+			if (!*str || *str == ',')
+				return true;
+		}
+
+		while (*str && *str != ',')
+			str++;
+
+		if (*str == ',')
+			str++;
+	}
+
+	return false;
+}
#3 lore raw

Re: [PATCH 2/5] efi: add kernel param efi=noruntime

Quoted 7 lines · Dave Young

For sharing functions I moved old_map here, but rethinking about it maybe it's better to keep it in arch code.

Matt, I see you have some code for libstub option parsing, I will rebase on your efi/next and I'm thinking to add a generic function for parsing params like below (for old_map, but libstub can not use it):

Makes sense to me.

Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
#8 lore raw

Re: [PATCH 4/5] efi x86: clear EFI_RUNTIME_SERVICES bit in case failures other than SetVirtualAddressMap

In reply to Dave Young
Quoted 13 lines · Dave Young

If enter virtual mode failed due to some reason other than the efi call the EFI_RUNTIME_SERVICES bit in efi.flags should be cleared thus users of efi runtime services can check the bit and handle the case instead of assume efi runtime is ok.

Per Matt, if efi call SetVirtualAddressMap fails we will be not sure it's safe to make any assumptions about the state of the system. so kernel panics instead of clears EFI_RUNTIME_SERVICES bit.

---
 arch/x86/platform/efi/efi.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

[...]

arch/x86/platform/efi/efi.c static void __init __efi_enter_virtual_mode(void) · line 892

Quoted 9 lines
arch/x86/platform/efi/efi.c +3 -1
@@ -882,8 +886,10 @@ static void __init __efi_enter_virtual_mode(void)
 
 void __init efi_enter_virtual_mode(void)
 {
-	if (efi_enabled(EFI_PARAVIRT))
+	if (efi_enabled(EFI_PARAVIRT)) {
+		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 		return;
+	}

The EFI_PARAVIRT folks purposely do not set this bit, so there should be no reason to clear it here.

Signature · 1 line
Matt Fleming, Intel Open Source Technology Center