Skip to contentSkip to conversation

efi: Don't use spinlocks for efi vars

PATCH In review [email protected] 5 participants 24 messages Dec 18, 2015 – Feb 11, 2016 9 messages not in the archive

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 efi: Don't use spinlocks for efi vars

From: Sylvain Chouleur <[email protected]>

All efivars operations are protected by a spinlock which prevents interruptions and preemption. This is too restricted, we just need a lock preventing concurency.
The idea is to use a semaphore of count 1 and to have two ways of locking, depending on the context:
- In interrupt context, we call down_trylock(), if it fails we return an error
- In normal context, we call down_interruptible()

We don't use a mutex here because the mutex_trylock() function must not be called from interrupt context, whereas the down_trylock() can.

This patch also remove the efivars lock to use a single lock for the whole vars.c file. The goal of this lock is to protect concurrent calls to efi variable services, registering and unregistering. This allows to register new efivars operations without having in-progress call.

---
 drivers/firmware/efi/efi-pstore.c |  34 +++++++---
 drivers/firmware/efi/efivars.c    |  10 ++-
 drivers/firmware/efi/vars.c       | 130 +++++++++++++++++++++++++-------------
 fs/efivarfs/inode.c               |   5 +-
 fs/efivarfs/super.c               |   8 ++-
 include/linux/efi.h               |  12 +---
 6 files changed, 130 insertions(+), 69 deletions(-)
drivers/firmware/efi/efi-pstore.c +25 -9
index eac76a79a880..c604af1243fd 100644
@@ -121,16 +121,19 @@ static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos,
  * @entry: deleting entry
  * @turn_off_scanning: Check if a scanning flag should be turned off
  */
-static inline void __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
+static inline int __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
 						bool turn_off_scanning)
 {
 	if (entry->deleting) {
 		list_del(&entry->list);
 		efivar_entry_iter_end();
 		efivar_unregister(entry);
-		efivar_entry_iter_begin();
+		if (efivar_entry_iter_begin())
+			return -EINTR;
 	} else if (turn_off_scanning)
 		entry->scanning = false;
+
+	return 0;
 }
 
 /**
@@ -140,13 +143,16 @@ static inline void __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
  * @head: list head
  * @stop: a flag checking if scanning will stop
  */
-static void efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
+static int efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
 				       struct efivar_entry *next,
 				       struct list_head *head, bool stop)
 {
-	__efi_pstore_scan_sysfs_exit(pos, true);
+	int ret = __efi_pstore_scan_sysfs_exit(pos, true);
+	if (ret)
+		return ret;
 	if (stop)
-		__efi_pstore_scan_sysfs_exit(next, &next->list != head);
+		ret = __efi_pstore_scan_sysfs_exit(next, &next->list != head);
+	return ret;
 }
 
 /**
@@ -168,13 +174,17 @@ static int efi_pstore_sysfs_entry_iter(void *data, struct efivar_entry **pos)
 	struct efivar_entry *entry, *n;
 	struct list_head *head = &efivar_sysfs_list;
 	int size = 0;
+	int ret;
 
 	if (!*pos) {
 		list_for_each_entry_safe(entry, n, head, list) {
 			efi_pstore_scan_sysfs_enter(entry, n, head);
 
 			size = efi_pstore_read_func(entry, data);
-			efi_pstore_scan_sysfs_exit(entry, n, head, size < 0);
+			ret = efi_pstore_scan_sysfs_exit(entry, n, head,
+							 size < 0);
+			if (ret)
+				return ret;
 			if (size)
 				break;
 		}
@@ -186,7 +196,9 @@ static int efi_pstore_sysfs_entry_iter(void *data, struct efivar_entry **pos)
 		efi_pstore_scan_sysfs_enter((*pos), n, head);
 
 		size = efi_pstore_read_func((*pos), data);
-		efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
+		ret = efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
+		if (ret)
+			return ret;
 		if (size)
 			break;
 	}
@@ -226,7 +238,10 @@ static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
 	if (!*data.buf)
 		return -ENOMEM;
 
-	efivar_entry_iter_begin();
+	if (efivar_entry_iter_begin()) {
+		kfree(*data.buf);
+		return -EINTR;
+	}
 	size = efi_pstore_sysfs_entry_iter(&data,
 					   (struct efivar_entry **)&psi->data);
 	efivar_entry_iter_end();
@@ -341,7 +356,8 @@ static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
 	edata.time = time;
 	edata.name = efi_name;
 
-	efivar_entry_iter_begin();
+	if (efivar_entry_iter_begin())
+		return -EINTR;
 	found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
 
 	if (found && !entry->scanning) {
drivers/firmware/efi/efivars.c +7 -3
index 756eca8c4cf8..3998373d92ef 100644
@@ -509,7 +509,8 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
 		vendor = del_var->VendorGuid;
 	}
 
-	efivar_entry_iter_begin();
+	if (efivar_entry_iter_begin())
+		return -EINTR;
 	entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
 	if (!entry)
 		err = -EINVAL;
@@ -582,7 +583,8 @@ efivar_create_sysfs_entry(struct efivar_entry *new_var)
 		return ret;
 
 	kobject_uevent(&new_var->kobj, KOBJ_ADD);
-	efivar_entry_add(new_var, &efivar_sysfs_list);
+	if (efivar_entry_add(new_var, &efivar_sysfs_list))
+		return -EINTR;
 
 	return 0;
 }
@@ -697,7 +699,9 @@ static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
 
 static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
 {
-	efivar_entry_remove(entry);
+	int err = efivar_entry_remove(entry);
+	if (err)
+		return err;
 	efivar_unregister(entry);
 	return 0;
 }
drivers/firmware/efi/vars.c +85 -45

357 lines not on this page · raw message

fs/efivarfs/inode.c +4 -1
index 3381b9da9ee6..5cea84ef7a44 100644
@@ -132,7 +132,10 @@ static int efivarfs_create(struct inode *dir, struct dentry *dentry,
 
 	inode->i_private = var;
 
-	efivar_entry_add(var, &efivarfs_list);
+	err = efivar_entry_add(var, &efivarfs_list);
+	if (err)
+		goto out;
+
 	d_instantiate(dentry, inode);
 	dget(dentry);
 out:
fs/efivarfs/super.c +6 -2
index 86a2121828c3..45ef421f545f 100644
@@ -158,7 +158,9 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
 	kfree(name);
 
 	efivar_entry_size(entry, &size);
-	efivar_entry_add(entry, &efivarfs_list);
+	err = efivar_entry_add(entry, &efivarfs_list);
+	if (err)
+		goto fail_inode;
 
 	mutex_lock(&inode->i_mutex);
 	inode->i_private = entry;
@@ -179,7 +181,9 @@ fail:
 
 static int efivarfs_destroy(struct efivar_entry *entry, void *data)
 {
-	efivar_entry_remove(entry);
+	int err = efivar_entry_remove(entry);
+	if (err)
+		return err;
 	kfree(entry);
 	return 0;
 }
include/linux/efi.h +3 -9
index 569b5a866bb1..2e60803fa4ba 100644
@@ -1096,12 +1096,6 @@ struct efivar_operations {
 };
 
 struct efivars {
-	/*
-	 * ->lock protects two things:
-	 * 1) efivarfs_list and efivars_sysfs_list
-	 * 2) ->ops calls
-	 */
-	spinlock_t lock;
 	struct kset *kset;
 	struct kobject *kobject;
 	const struct efivar_operations *ops;
@@ -1169,8 +1163,8 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 		void *data, bool atomic, bool duplicates,
 		struct list_head *head);
 
-void efivar_entry_add(struct efivar_entry *entry, struct list_head *head);
-void efivar_entry_remove(struct efivar_entry *entry);
+int efivar_entry_add(struct efivar_entry *entry, struct list_head *head);
+int efivar_entry_remove(struct efivar_entry *entry);
 
 int __efivar_entry_delete(struct efivar_entry *entry);
 int efivar_entry_delete(struct efivar_entry *entry);
@@ -1187,7 +1181,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
 			  bool block, unsigned long size, void *data);
 
-void efivar_entry_iter_begin(void);
+int efivar_entry_iter_begin(void);
 void efivar_entry_iter_end(void);
 
 int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
Signature · 1 line
2.6.3
#2 lore raw

PATCH 2/2 efi: implement interruptible runtime services

In reply to a message not in the archive

From: Sylvain Chouleur <[email protected]>

This patch adds an implementation of EFI runtime services wappers which allow interrupts and preemption during execution of BIOS code.

It's needed by Broxton platform which requires the OS to do the write of the non volatile variables. To do that, the OS will receive an interrupt coming from the firmware to notify that it must write the data.

BIOS code must be executed using a specific PGD. This is currently done by efi_call() which force value of CR3 before calling BIOS and restore previous value after.

We use a dedicated kthread which will execute all interruptible runtime services. This efi_kthread is special because it has it's own mm_struct whereas kthread should not have one. This mm_struct contains the EFI PGD so the scheduler will load it before running any BIOS code.

Obviously, an interruptible runtime service must never be called from an interrupt context. EFI pstore is the only use case here, that's why we require it to be disabled when activating interruptible runtime services.

---
 arch/x86/Kconfig                          |  14 +++
 arch/x86/include/asm/efi.h                |   1 +
 arch/x86/platform/efi/Makefile            |   1 +
 arch/x86/platform/efi/efi_32.c            |   5 +
 arch/x86/platform/efi/efi_64.c            |   5 +
 arch/x86/platform/efi/efi_interruptible.c | 191 ++++++++++++++++++++++++++++++
 6 files changed, 217 insertions(+)
 create mode 100644 arch/x86/platform/efi/efi_interruptible.c
arch/x86/Kconfig +14 -0
index db3622f22b61..3ddd5a9cab30 100644
@@ -1720,6 +1720,20 @@ config EFI_MIXED
 
 	   If unsure, say N.
 
+if X86_64
+config EFI_INTERRUPTIBLE
+	bool "Interruptible Efi Runtime Services"
+	depends on EFI && !EFI_VARS_PSTORE
+	default n
+	help
+          This is an interruptible implementation of efi runtime
+	  services wrappers. If you say Y, BIOS code could be
+	  interrupted and preempted as a standard process. Enable this
+	  only if you are sure that your BIOS will support
+	  interruptible efi calls
+	  In doubt, say "N".
+endif
+
 config SECCOMP
 	def_bool y
 	prompt "Enable seccomp to safely compute untrusted bytecode"
arch/x86/include/asm/efi.h +1 -0
index 0010c78c4998..5e9620b3688b 100644
@@ -111,6 +111,7 @@ extern void __init efi_memory_uc(u64 addr, unsigned long size);
 extern void __init efi_map_region(efi_memory_desc_t *md);
 extern void __init efi_map_region_fixed(efi_memory_desc_t *md);
 extern void efi_sync_low_kernel_mappings(void);
+extern pgd_t *efi_get_pgd(void);
 extern int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages);
 extern void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages);
 extern void __init old_map_region(efi_memory_desc_t *md);
arch/x86/platform/efi/Makefile +1 -0
index 2846aaab5103..dcc894d1e603 100644
@@ -2,3 +2,4 @@ obj-$(CONFIG_EFI) 		+= quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o
 obj-$(CONFIG_ACPI_BGRT) += efi-bgrt.o
 obj-$(CONFIG_EARLY_PRINTK_EFI)	+= early_printk.o
 obj-$(CONFIG_EFI_MIXED)		+= efi_thunk_$(BITS).o
+obj-$(CONFIG_EFI_INTERRUPTIBLE)		+= efi_interruptible.o
arch/x86/platform/efi/efi_32.c +5 -0
index ed5b67338294..02a3c616c59e 100644
@@ -56,6 +56,11 @@ void __init efi_map_region(efi_memory_desc_t *md)
 void __init efi_map_region_fixed(efi_memory_desc_t *md) {}
 void __init parse_efi_setup(u64 phys_addr, u32 data_len) {}
 
+pgd_t *efi_get_pgd(void)
+{
+	return initial_page_table;
+}
+
 pgd_t * __init efi_call_phys_prolog(void)
 {
 	struct desc_ptr gdt_descr;
arch/x86/platform/efi/efi_64.c +5 -0
index d347e854a5e4..67cffb69d405 100644
@@ -143,6 +143,11 @@ void efi_sync_low_kernel_mappings(void)
 		sizeof(pgd_t) * num_pgds);
 }
 
+pgd_t *efi_get_pgd(void)
+{
+	return __va(efi_scratch.efi_pgt);
+}
+
 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
 {
 	unsigned long text;
arch/x86/platform/efi/efi_interruptible.c new file +191 -0

191 lines not on this page · raw message

Signature · 1 line
2.6.3
#8 lore raw
In reply to a message not in the archive

(Cc'ing Ard since he has recently been in this area)

Quoted 21 lines · On Fri, 18 Dec, at 11:29:50AM, [email protected]…

From: Sylvain Chouleur <[email protected]>

All efivars operations are protected by a spinlock which prevents interruptions and preemption. This is too restricted, we just need a lock preventing concurency.
The idea is to use a semaphore of count 1 and to have two ways of locking, depending on the context:
- In interrupt context, we call down_trylock(), if it fails we return an error
- In normal context, we call down_interruptible()

We don't use a mutex here because the mutex_trylock() function must not be called from interrupt context, whereas the down_trylock() can.

This patch also remove the efivars lock to use a single lock for the whole vars.c file. The goal of this lock is to protect concurrent calls to efi variable services, registering and unregistering. This allows to register new efivars operations without having in-progress call.

Quoted 8 lines
---
 drivers/firmware/efi/efi-pstore.c |  34 +++++++---
 drivers/firmware/efi/efivars.c    |  10 ++-
 drivers/firmware/efi/vars.c       | 130 +++++++++++++++++++++++++-------------
 fs/efivarfs/inode.c               |   5 +-
 fs/efivarfs/super.c               |   8 ++-
 include/linux/efi.h               |  12 +---
 6 files changed, 130 insertions(+), 69 deletions(-)

This needs splitting into more than 1 patch.

You need separate patches to,

- Split out __efivars->lock into a file local lock
- Convert the lock to a semaphore
- Print a message when efi_operations are registered

Further comments below.

Quoted 15 lines
drivers/firmware/efi/efivars.c +2 -1
index 756eca8c4cf8..3998373d92ef 100644
@@ -509,7 +509,8 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
 		vendor = del_var->VendorGuid;
 	}
 
-	efivar_entry_iter_begin();
+	if (efivar_entry_iter_begin())
+		return -EINTR;
 	entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
 	if (!entry)
 		err = -EINVAL;

drivers/firmware/efi/efivars.c efivar_create_sysfs_entry(struct efivar_entry *new_var) · line 590

Quoted 9 lines
drivers/firmware/efi/efivars.c +2 -1
@@ -582,7 +583,8 @@ efivar_create_sysfs_entry(struct efivar_entry *new_var)
 		return ret;
 
 	kobject_uevent(&new_var->kobj, KOBJ_ADD);
-	efivar_entry_add(new_var, &efivar_sysfs_list);
+	if (efivar_entry_add(new_var, &efivar_sysfs_list))
+		return -EINTR;
 
 	return 0;
 }

This looks like it's missing a call to efivar_unregister() in the -EINTR case.

drivers/firmware/efi/efivars.c static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor, · line 707

Quoted 11 lines
drivers/firmware/efi/efivars.c +3 -1
@@ -697,7 +699,9 @@ static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
 
 static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
 {
-	efivar_entry_remove(entry);
+	int err = efivar_entry_remove(entry);
+	if (err)
+		return err;
 	efivar_unregister(entry);
 	return 0;
 }

You now need to return early from efivars_sysfs_exit() if you hit the error path in efivar_sysfs_destroy().

Quoted 5 lines
drivers/firmware/efi/vars.c +0 -0
index 70a0fb10517f..8a44351211e5 100644

drivers/firmware/efi/vars.c line 46

Quoted 10 lines
drivers/firmware/efi/vars.c +7 -0
@@ -37,6 +37,13 @@
 /* Private pointer to registered efivars */
 static struct efivars *__efivars;
 
+/*
+ * ->lock protects two things:
+ * 1) efivarfs_list and efivars_sysfs_list
+ * 2) ->ops calls
+ */
+DEFINE_SEMAPHORE(efivars_lock);
+

Now it also protects registration of __efivars, so that needs to be documented too.

Quoted 26 lines
drivers/firmware/efi/vars.c +5 -2
 static bool efivar_wq_enabled = true;
 DECLARE_WORK(efivar_work, NULL);
 EXPORT_SYMBOL_GPL(efivar_work);
@@ -376,7 +383,10 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 		return -ENOMEM;
 	}
 
-	spin_lock_irq(&__efivars->lock);
+	if (down_interruptible(&efivars_lock)) {
+		err = -EINTR;
+		goto free;
+	}
 
 	/*
 	 * Per EFI spec, the maximum storage allocated for both
@@ -392,7 +402,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 		switch (status) {
 		case EFI_SUCCESS:
 			if (!atomic)
-				spin_unlock_irq(&__efivars->lock);
+				up(&efivars_lock);
 
 			variable_name_size = var_name_strnsize(variable_name,
 							       variable_name_size);
@@ -410,7 +420,10 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 				dup_variable_bug(variable_name, &vendor_guid,

drivers/firmware/efi/vars.c int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *), · line 429

Quoted 10 lines
drivers/firmware/efi/vars.c +4 -1
 						 variable_name_size);
 				if (!atomic)
-					spin_lock_irq(&__efivars->lock);
+					if (down_interruptible(&efivars_lock)) {
+						err = -EINTR;
+						goto free;
+					}
 
 				status = EFI_NOT_FOUND;
 				break;

Add braces to the if(!atomic) clause please to help with readability.

drivers/firmware/efi/vars.c int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *), · line 443

Quoted 12 lines
drivers/firmware/efi/vars.c +4 -1
@@ -421,7 +434,10 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 				status = EFI_NOT_FOUND;
 
 			if (!atomic)
-				spin_lock_irq(&__efivars->lock);
+				if (down_interruptible(&efivars_lock)) {
+					err = -EINTR;
+					goto free;
+				}
 
 			break;
 		case EFI_NOT_FOUND:

Ditto.

Quoted 7 lines
drivers/firmware/efi/vars.c +2 -1
@@ -533,12 +559,14 @@ int efivar_entry_delete(struct efivar_entry *entry)
 	const struct efivar_operations *ops = __efivars->ops;
 	efi_status_t status;
 
-	spin_lock_irq(&__efivars->lock);
+	if (down_interruptible(&efivars_lock))
+		return -EINTR;

drivers/firmware/efi/vars.c int efivar_entry_delete(struct efivar_entry *entry) · line 572

Quoted 10 lines
drivers/firmware/efi/vars.c +2 -1
+
 	status = ops->set_variable(entry->var.VariableName,
 				   &entry->var.VendorGuid,
 				   0, 0, NULL);
 	if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
-		spin_unlock_irq(&__efivars->lock);
+		up(&efivars_lock);
 		return efi_status_to_err(status);
 	}
 

Please update the documentation for this function to note that we return -EINTR if we can't grab the semaphore.

Quoted 15 lines
drivers/firmware/efi/vars.c +3 -3
@@ -576,10 +604,10 @@ int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
 	efi_char16_t *name = entry->var.VariableName;
 	efi_guid_t vendor = entry->var.VendorGuid;
 
-	spin_lock_irq(&__efivars->lock);
-
+	if (down_interruptible(&efivars_lock))
+		return -EINTR;
 	if (head && efivar_entry_find(name, vendor, head, false)) {
-		spin_unlock_irq(&__efivars->lock);
+		up(&efivars_lock);
 		return -EEXIST;
 	}
 

drivers/firmware/efi/vars.c int efivar_entry_set(struct efivar_entry *entry, u32 attributes, · line 622

Quoted 8 lines
drivers/firmware/efi/vars.c +1 -1
@@ -588,7 +616,7 @@ int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
 		status = ops->set_variable(name, &vendor,
 					   attributes, size, data);
 
-	spin_unlock_irq(&__efivars->lock);
+	up(&efivars_lock);
 
 	return efi_status_to_err(status);
 

Function documentation for the return values needs updating here too.

drivers/firmware/efi/vars.c int efivars_register(struct efivars *efivars, · line 1092

Quoted 8 lines
drivers/firmware/efi/vars.c +3 -1
@@ -1055,12 +1087,16 @@ int efivars_register(struct efivars *efivars,
 		     const struct efivar_operations *ops,
 		     struct kobject *kobject)
 {
-	spin_lock_init(&efivars->lock);
+	if (down_trylock(&efivars_lock))
+		return -EBUSY;
+

Is this correct? I would have assumed that you'd want to return -EINTR here, not -EBUSY since if an EFI variable operation is currently running we should spin waiting for the semaphore to be released.

Quoted 11 lines
drivers/firmware/efi/vars.c +2 -0
 	efivars->ops = ops;
 	efivars->kobject = kobject;
 
 	__efivars = efivars;
 
+	up(&efivars_lock);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(efivars_register);

drivers/firmware/efi/vars.c int efivars_unregister(struct efivars *efivars) · line 1117

Quoted 6 lines
drivers/firmware/efi/vars.c +3 -0
@@ -1076,6 +1112,9 @@ int efivars_unregister(struct efivars *efivars)
 {
 	int rv;
 
+	if (down_trylock(&efivars_lock))
+		return -EBUSY;
+

Same logic applies in the unregister case.

#3 lore raw

Re: [PATCH 2/2] efi: implement interruptible runtime services

In reply to [email protected]
Quoted 46 lines · On Fri, 18 Dec, at 11:29:51AM, [email protected]…

From: Sylvain Chouleur <[email protected]>

This patch adds an implementation of EFI runtime services wappers which allow interrupts and preemption during execution of BIOS code.

It's needed by Broxton platform which requires the OS to do the write of the non volatile variables. To do that, the OS will receive an interrupt coming from the firmware to notify that it must write the data.

BIOS code must be executed using a specific PGD. This is currently done by efi_call() which force value of CR3 before calling BIOS and restore previous value after.

We use a dedicated kthread which will execute all interruptible runtime services. This efi_kthread is special because it has it's own mm_struct whereas kthread should not have one. This mm_struct contains the EFI PGD so the scheduler will load it before running any BIOS code.

Obviously, an interruptible runtime service must never be called from an interrupt context. EFI pstore is the only use case here, that's why we require it to be disabled when activating interruptible runtime services.

---
 arch/x86/Kconfig                          |  14 +++
 arch/x86/include/asm/efi.h                |   1 +
 arch/x86/platform/efi/Makefile            |   1 +
 arch/x86/platform/efi/efi_32.c            |   5 +
 arch/x86/platform/efi/efi_64.c            |   5 +
 arch/x86/platform/efi/efi_interruptible.c | 191 ++++++++++++++++++++++++++++++
 6 files changed, 217 insertions(+)
 create mode 100644 arch/x86/platform/efi/efi_interruptible.c
arch/x86/Kconfig +4 -0
index db3622f22b61..3ddd5a9cab30 100644
@@ -1720,6 +1720,20 @@ config EFI_MIXED
 
 	   If unsure, say N.
 
+if X86_64
+config EFI_INTERRUPTIBLE
+	bool "Interruptible Efi Runtime Services"
+	depends on EFI && !EFI_VARS_PSTORE

arch/x86/Kconfig config EFI_MIXED · line 1736

Quoted 10 lines
arch/x86/Kconfig +10 -0
+	default n
+	help
+          This is an interruptible implementation of efi runtime
+	  services wrappers. If you say Y, BIOS code could be
+	  interrupted and preempted as a standard process. Enable this
+	  only if you are sure that your BIOS will support
+	  interruptible efi calls
+	  In doubt, say "N".
+endif
+

These words should be a little more assertive. Almost everyone is going to want to turn this feature off.

arch/x86/Kconfig config EFI_MIXED

Quoted 10 lines
arch/x86/Kconfig +10 -0
+static efi_status_t
+non_blocking_not_allowed(__attribute__((unused)) efi_char16_t *name,
+			 __attribute__((unused)) efi_guid_t *vendor,
+			 __attribute__((unused)) u32 attr,
+			 __attribute__((unused)) unsigned long data_size,
+			 __attribute__((unused)) void *data)
+{
+	pr_err("efi_interruptible: non bloking operation is not allowed\n");
+	return EFI_UNSUPPORTED;
+}

Typo, s/bloking/blocking/

Quoted 12 lines
arch/x86/Kconfig +12 -0
+static int efi_interruptible_panic_notifier_call(
+	struct notifier_block *notifier,
+	unsigned long what, void *data)
+{
+	static struct efivars generic_efivars;
+	static struct efivar_operations generic_ops;
+
+	generic_ops.get_variable = efi.get_variable;
+	generic_ops.set_variable = efi.set_variable;
+	generic_ops.get_next_variable = efi.get_next_variable;
+	generic_ops.query_variable_store = efi_query_variable_store;
+

arch/x86/Kconfig

Quoted 10 lines
arch/x86/Kconfig +10 -0
+	efivars_register(&generic_efivars, &generic_ops, efivars_kobject());
+
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block panic_nb = {
+	.notifier_call = efi_interruptible_panic_notifier_call,
+	.priority = 100,
+};
+

What's the purpose of this? The only reason you'd want to do this that I can think of is because you'd want efi-pstore to run and attempt to save some crash data for you. But you can't build with efi-pstore enable and CONFIG_EFI_INTERRUPTIBLE.

I'd be tempted to just delete this notifier code.

Quoted 21 lines
arch/x86/Kconfig +21 -0
+static struct task_struct *efi_kworker_task;
+static struct efivar_operations interruptible_ops;
+static __init int efi_interruptible_init(void)
+{
+	int ret;
+
+	efi_kworker_task = kthread_create(kthread_worker_fn, &efi_kworker,
+					  "efi_kthread");
+	if (IS_ERR(efi_kworker_task)) {
+		pr_err("efi_interruptible: Failed to create kthread\n");
+		ret = PTR_ERR(efi_kworker_task);
+		efi_kworker_task = NULL;
+		return ret;
+	}
+
+	efi_kworker_task->mm = mm_alloc();
+	efi_kworker_task->active_mm = efi_kworker_task->mm;
+	efi_kworker_task->mm->pgd = efi_get_pgd();
+	wake_up_process(efi_kworker_task);
+
+	atomic_notifier_chain_register(&panic_notifier_list, &panic_nb);

arch/x86/Kconfig

Quoted 10 lines
arch/x86/Kconfig +10 -0
+
+	interruptible_ops.get_variable = get_variable_interruptible;
+	interruptible_ops.set_variable = set_variable_interruptible;
+	interruptible_ops.set_variable_nonblocking = non_blocking_not_allowed;
+	interruptible_ops.get_next_variable = get_next_variable_interruptible;
+	interruptible_ops.query_variable_store = efi_query_variable_store;
+	return efivars_register(&interruptible_efivars, &interruptible_ops,
+				efivars_kobject());
+}
+

Is there some way we can match DMI/PCI identifiers for Broxton so that we don't start seeing people accidentally running with preemptible EFI services on non-BXT hardware?

#10 lore raw
In reply to a message not in the archive

2016-01-06 13:24 GMT+01:00 Matt Fleming <matt-mF/unelCI9GS6iBeEJttW/[email protected]>:

Quoted 66 lines

(Cc'ing Ard since he has recently been in this area)

Quoted 21 lines · On Fri, 18 Dec, at 11:29:50AM, [email protected]…

From: Sylvain Chouleur <[email protected]>

All efivars operations are protected by a spinlock which prevents interruptions and preemption. This is too restricted, we just need a lock preventing concurency.
The idea is to use a semaphore of count 1 and to have two ways of locking, depending on the context:
- In interrupt context, we call down_trylock(), if it fails we return an error
- In normal context, we call down_interruptible()

We don't use a mutex here because the mutex_trylock() function must not be called from interrupt context, whereas the down_trylock() can.

This patch also remove the efivars lock to use a single lock for the whole vars.c file. The goal of this lock is to protect concurrent calls to efi variable services, registering and unregistering. This allows to register new efivars operations without having in-progress call.

Quoted 8 lines
---
 drivers/firmware/efi/efi-pstore.c |  34 +++++++---
 drivers/firmware/efi/efivars.c    |  10 ++-
 drivers/firmware/efi/vars.c       | 130 +++++++++++++++++++++++++-------------
 fs/efivarfs/inode.c               |   5 +-
 fs/efivarfs/super.c               |   8 ++-
 include/linux/efi.h               |  12 +---
 6 files changed, 130 insertions(+), 69 deletions(-)

This needs splitting into more than 1 patch.

You need separate patches to,

- Split out __efivars->lock into a file local lock
- Convert the lock to a semaphore
- Print a message when efi_operations are registered

Further comments below.

Quoted 22 lines
drivers/firmware/efi/efivars.c +4 -2
index 756eca8c4cf8..3998373d92ef 100644
@@ -509,7 +509,8 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
              vendor = del_var->VendorGuid;
      }
 
-     efivar_entry_iter_begin();
+     if (efivar_entry_iter_begin())
+             return -EINTR;
      entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
      if (!entry)
              err = -EINVAL;
@@ -582,7 +583,8 @@ efivar_create_sysfs_entry(struct efivar_entry *new_var)
              return ret;
 
      kobject_uevent(&new_var->kobj, KOBJ_ADD);
-     efivar_entry_add(new_var, &efivar_sysfs_list);
+     if (efivar_entry_add(new_var, &efivar_sysfs_list))
+             return -EINTR;
 
Quoted 5 lines

drivers/firmware/efi/efivars.c efivar_create_sysfs_entry(struct efivar_entry *new_var) · line 590

Quoted 2 lines
drivers/firmware/efi/efivars.c +0 -0
      return 0;
 }

This looks like it's missing a call to efivar_unregister() in the -EINTR case.

I don't see why
It's returning an error code as other lines in the same function. Can you develop your thoughts?

Quoted 136 lines

drivers/firmware/efi/efivars.c static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor, · line 707

Quoted 12 lines
drivers/firmware/efi/efivars.c +3 -1
@@ -697,7 +699,9 @@ static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
 
 static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
 {
-     efivar_entry_remove(entry);
+     int err = efivar_entry_remove(entry);
+     if (err)
+             return err;
      efivar_unregister(entry);
      return 0;
 }

You now need to return early from efivars_sysfs_exit() if you hit the error path in efivar_sysfs_destroy().

Quoted 5 lines
drivers/firmware/efi/vars.c +0 -0
index 70a0fb10517f..8a44351211e5 100644

drivers/firmware/efi/vars.c line 46

Quoted 10 lines
drivers/firmware/efi/vars.c +7 -0
@@ -37,6 +37,13 @@
 /* Private pointer to registered efivars */
 static struct efivars *__efivars;
 
+/*
+ * ->lock protects two things:
+ * 1) efivarfs_list and efivars_sysfs_list
+ * 2) ->ops calls
+ */
+DEFINE_SEMAPHORE(efivars_lock);
+

Now it also protects registration of __efivars, so that needs to be documented too.

Quoted 26 lines
drivers/firmware/efi/vars.c +5 -2
 static bool efivar_wq_enabled = true;
 DECLARE_WORK(efivar_work, NULL);
 EXPORT_SYMBOL_GPL(efivar_work);
@@ -376,7 +383,10 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
              return -ENOMEM;
      }
 
-     spin_lock_irq(&__efivars->lock);
+     if (down_interruptible(&efivars_lock)) {
+             err = -EINTR;
+             goto free;
+     }
 
      /*
       * Per EFI spec, the maximum storage allocated for both
@@ -392,7 +402,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
              switch (status) {
              case EFI_SUCCESS:
                      if (!atomic)
-                             spin_unlock_irq(&__efivars->lock);
+                             up(&efivars_lock);
 
                      variable_name_size = var_name_strnsize(variable_name,
                                                             variable_name_size);
@@ -410,7 +420,10 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
                              dup_variable_bug(variable_name, &vendor_guid,

drivers/firmware/efi/vars.c int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *), · line 429

Quoted 10 lines
drivers/firmware/efi/vars.c +4 -1
                                               variable_name_size);
                              if (!atomic)
-                                     spin_lock_irq(&__efivars->lock);
+                                     if (down_interruptible(&efivars_lock)) {
+                                             err = -EINTR;
+                                             goto free;
+                                     }
 
                              status = EFI_NOT_FOUND;
                              break;

Add braces to the if(!atomic) clause please to help with readability.

drivers/firmware/efi/vars.c int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *), · line 443

Quoted 12 lines
drivers/firmware/efi/vars.c +4 -1
@@ -421,7 +434,10 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
                              status = EFI_NOT_FOUND;
 
                      if (!atomic)
-                             spin_lock_irq(&__efivars->lock);
+                             if (down_interruptible(&efivars_lock)) {
+                                     err = -EINTR;
+                                     goto free;
+                             }
 
                      break;
              case EFI_NOT_FOUND:

Ditto.

Quoted 7 lines
drivers/firmware/efi/vars.c +2 -1
@@ -533,12 +559,14 @@ int efivar_entry_delete(struct efivar_entry *entry)
      const struct efivar_operations *ops = __efivars->ops;
      efi_status_t status;
 
-     spin_lock_irq(&__efivars->lock);
+     if (down_interruptible(&efivars_lock))
+             return -EINTR;

drivers/firmware/efi/vars.c int efivar_entry_delete(struct efivar_entry *entry) · line 572

Quoted 10 lines
drivers/firmware/efi/vars.c +2 -1
+
      status = ops->set_variable(entry->var.VariableName,
                                 &entry->var.VendorGuid,
                                 0, 0, NULL);
      if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
-             spin_unlock_irq(&__efivars->lock);
+             up(&efivars_lock);
              return efi_status_to_err(status);
      }
 

Please update the documentation for this function to note that we return -EINTR if we can't grab the semaphore.

Quoted 15 lines
drivers/firmware/efi/vars.c +3 -3
@@ -576,10 +604,10 @@ int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
      efi_char16_t *name = entry->var.VariableName;
      efi_guid_t vendor = entry->var.VendorGuid;
 
-     spin_lock_irq(&__efivars->lock);
-
+     if (down_interruptible(&efivars_lock))
+             return -EINTR;
      if (head && efivar_entry_find(name, vendor, head, false)) {
-             spin_unlock_irq(&__efivars->lock);
+             up(&efivars_lock);
              return -EEXIST;
      }
 

drivers/firmware/efi/vars.c int efivar_entry_set(struct efivar_entry *entry, u32 attributes, · line 622

Quoted 8 lines
drivers/firmware/efi/vars.c +1 -1
@@ -588,7 +616,7 @@ int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
              status = ops->set_variable(name, &vendor,
                                         attributes, size, data);
 
-     spin_unlock_irq(&__efivars->lock);
+     up(&efivars_lock);
 
      return efi_status_to_err(status);
 

Function documentation for the return values needs updating here too.

Quoted 12 lines

drivers/firmware/efi/vars.c int efivars_register(struct efivars *efivars, · line 1092

Quoted 8 lines
drivers/firmware/efi/vars.c +3 -1
@@ -1055,12 +1087,16 @@ int efivars_register(struct efivars *efivars,
                   const struct efivar_operations *ops,
                   struct kobject *kobject)
 {
-     spin_lock_init(&efivars->lock);
+     if (down_trylock(&efivars_lock))
+             return -EBUSY;
+

Is this correct? I would have assumed that you'd want to return -EINTR here, not -EBUSY since if an EFI variable operation is currently running we should spin waiting for the semaphore to be released.

No because this is a trylock, it will not wait for the semaphore to be release, it will return as soon as it sees that the semaphore is locked.
We don't want to use down_interruptible() here because we want to be able to (un)register efivars operation in an interrupt context.

Quoted 15 lines
Quoted 15 lines
drivers/firmware/efi/vars.c +2 -0
      efivars->ops = ops;
      efivars->kobject = kobject;
 
      __efivars = efivars;
 
+     up(&efivars_lock);
+
      return 0;
 }
 EXPORT_SYMBOL_GPL(efivars_register);
@@ -1076,6 +1112,9 @@ int efivars_unregister(struct efivars *efivars)
 {
      int rv;
 
Quoted 5 lines

drivers/firmware/efi/vars.c int efivars_register(struct efivars *efivars, · line 1092

Quoted 3 lines
drivers/firmware/efi/vars.c +3 -0
+     if (down_trylock(&efivars_lock))
+             return -EBUSY;
+

Same logic applies in the unregister case.

Thanks Matt,

I'll provide updated patches soon

Signature · 1 line
Sylvain
#4 lore raw

Re: [PATCH 2/2] efi: implement interruptible runtime services

In reply to Matt Fleming

2016-01-06 13:58 GMT+01:00 Matt Fleming <matt-mF/unelCI9GS6iBeEJttW/[email protected]>:

Quoted 102 lines · On Fri, 18 Dec, at 11:29:51AM, [email protected]…
Quoted 46 lines

From: Sylvain Chouleur <[email protected]>

This patch adds an implementation of EFI runtime services wappers which allow interrupts and preemption during execution of BIOS code.

It's needed by Broxton platform which requires the OS to do the write of the non volatile variables. To do that, the OS will receive an interrupt coming from the firmware to notify that it must write the data.

BIOS code must be executed using a specific PGD. This is currently done by efi_call() which force value of CR3 before calling BIOS and restore previous value after.

We use a dedicated kthread which will execute all interruptible runtime services. This efi_kthread is special because it has it's own mm_struct whereas kthread should not have one. This mm_struct contains the EFI PGD so the scheduler will load it before running any BIOS code.

Obviously, an interruptible runtime service must never be called from an interrupt context. EFI pstore is the only use case here, that's why we require it to be disabled when activating interruptible runtime services.

---
 arch/x86/Kconfig                          |  14 +++
 arch/x86/include/asm/efi.h                |   1 +
 arch/x86/platform/efi/Makefile            |   1 +
 arch/x86/platform/efi/efi_32.c            |   5 +
 arch/x86/platform/efi/efi_64.c            |   5 +
 arch/x86/platform/efi/efi_interruptible.c | 191 ++++++++++++++++++++++++++++++
 6 files changed, 217 insertions(+)
 create mode 100644 arch/x86/platform/efi/efi_interruptible.c
arch/x86/Kconfig +4 -0
index db3622f22b61..3ddd5a9cab30 100644
@@ -1720,6 +1720,20 @@ config EFI_MIXED
 
         If unsure, say N.
 
+if X86_64
+config EFI_INTERRUPTIBLE
+     bool "Interruptible Efi Runtime Services"
+     depends on EFI && !EFI_VARS_PSTORE

arch/x86/Kconfig config EFI_MIXED · line 1736

Quoted 10 lines
arch/x86/Kconfig +10 -0
+     default n
+     help
+          This is an interruptible implementation of efi runtime
+       services wrappers. If you say Y, BIOS code could be
+       interrupted and preempted as a standard process. Enable this
+       only if you are sure that your BIOS will support
+       interruptible efi calls
+       In doubt, say "N".
+endif
+

These words should be a little more assertive. Almost everyone is going to want to turn this feature off.

arch/x86/Kconfig config EFI_MIXED

Quoted 10 lines
arch/x86/Kconfig +10 -0
+static efi_status_t
+non_blocking_not_allowed(__attribute__((unused)) efi_char16_t *name,
+                      __attribute__((unused)) efi_guid_t *vendor,
+                      __attribute__((unused)) u32 attr,
+                      __attribute__((unused)) unsigned long data_size,
+                      __attribute__((unused)) void *data)
+{
+     pr_err("efi_interruptible: non bloking operation is not allowed\n");
+     return EFI_UNSUPPORTED;
+}

Typo, s/bloking/blocking/

Quoted 12 lines
arch/x86/Kconfig +12 -0
+static int efi_interruptible_panic_notifier_call(
+     struct notifier_block *notifier,
+     unsigned long what, void *data)
+{
+     static struct efivars generic_efivars;
+     static struct efivar_operations generic_ops;
+
+     generic_ops.get_variable = efi.get_variable;
+     generic_ops.set_variable = efi.set_variable;
+     generic_ops.get_next_variable = efi.get_next_variable;
+     generic_ops.query_variable_store = efi_query_variable_store;
+

arch/x86/Kconfig

Quoted 10 lines
arch/x86/Kconfig +10 -0
+     efivars_register(&generic_efivars, &generic_ops, efivars_kobject());
+
+     return NOTIFY_DONE;
+}
+
+static struct notifier_block panic_nb = {
+     .notifier_call = efi_interruptible_panic_notifier_call,
+     .priority = 100,
+};
+

What's the purpose of this? The only reason you'd want to do this that I can think of is because you'd want efi-pstore to run and attempt to save some crash data for you. But you can't build with efi-pstore enable and CONFIG_EFI_INTERRUPTIBLE.

Quoted 1 line

I'd be tempted to just delete this notifier code.

This is indeed to be able to try write some crash data when we are in a panic context. In fact with this restoration of legacy efi handlers on a panic we should be able to have pstore working with CONFIG_EFI_INTERRUPTIBLE.

I say should because there is still issues with BIOS to have the panic flow working.

Quoted 28 lines
Quoted 28 lines
arch/x86/Kconfig +27 -0
+static struct task_struct *efi_kworker_task;
+static struct efivar_operations interruptible_ops;
+static __init int efi_interruptible_init(void)
+{
+     int ret;
+
+     efi_kworker_task = kthread_create(kthread_worker_fn, &efi_kworker,
+                                       "efi_kthread");
+     if (IS_ERR(efi_kworker_task)) {
+             pr_err("efi_interruptible: Failed to create kthread\n");
+             ret = PTR_ERR(efi_kworker_task);
+             efi_kworker_task = NULL;
+             return ret;
+     }
+
+     efi_kworker_task->mm = mm_alloc();
+     efi_kworker_task->active_mm = efi_kworker_task->mm;
+     efi_kworker_task->mm->pgd = efi_get_pgd();
+     wake_up_process(efi_kworker_task);
+
+     atomic_notifier_chain_register(&panic_notifier_list, &panic_nb);
+
+     interruptible_ops.get_variable = get_variable_interruptible;
+     interruptible_ops.set_variable = set_variable_interruptible;
+     interruptible_ops.set_variable_nonblocking = non_blocking_not_allowed;
+     interruptible_ops.get_next_variable = get_next_variable_interruptible;
+     interruptible_ops.query_variable_store = efi_query_variable_store;
Quoted 8 lines

arch/x86/Kconfig

Quoted 4 lines
arch/x86/Kconfig +4 -0
+     return efivars_register(&interruptible_efivars, &interruptible_ops,
+                             efivars_kobject());
+}
+

Is there some way we can match DMI/PCI identifiers for Broxton so that we don't start seeing people accidentally running with preemptible EFI services on non-BXT hardware?

I don't see how since this depends on a storage strategy more than the SoC itself, it can be used on future platforms or we can also get rid of it on BXT if an other storage is used for efi variables.
Can't the KConfig protection be enough?

Signature · 1 line
Sylvain
#15 lore raw

v2 cover efi interruptible runtime services

In reply to [email protected]

From: Sylvain Chouleur <[email protected]>

Changes in v2:
- Split patch 1 in patches 1 and 2
- Document (un)registration of __efivars protection
- Return early from efivars_sysfs_exit() in case of failure
- Improve readability
- Update functions documentation
- Fix typo
- Fix checkpatch warnings
- Warning in KConfig description

Sylvain Chouleur (3):
efi: implement interruptible runtime services
efi: don't use spinlocks for efi vars
efi: use a file local lock for efivars

 arch/x86/Kconfig                          |   17 +++++
 arch/x86/include/asm/efi.h                |    1 
 arch/x86/platform/efi/Makefile            |    1 
 arch/x86/platform/efi/efi_32.c            |    5 +
 arch/x86/platform/efi/efi_64.c            |    5 +
 arch/x86/platform/efi/efi_interruptible.c |  191 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/firmware/efi/efi-pstore.c         |   36 +++++++---
 drivers/firmware/efi/efivars.c            |   20 ++++-
 drivers/firmware/efi/vars.c               |  230 ++++++++++++++++++++++++++++++++++++++++----------------------------
 fs/efivarfs/inode.c                       |    5 +
 fs/efivarfs/super.c                       |    9 ++
 include/linux/efi.h                       |   12 ---
 12 files changed, 415 insertions(+), 117 deletions(-)
#17 lore raw

PATCH v2 1/3 efi: use a file local lock for efivars

In reply to a message not in the archive

From: Sylvain Chouleur <[email protected]>

This patch remove the efivars lock to use a single lock for the whole vars.c file. The goal of this lock is to protect concurrent calls to efi variable services, registering and unregistering. This allows to register new efivars operations without having in-progress call.

---
 drivers/firmware/efi/vars.c | 85 +++++++++++++++++++++++++--------------------
 include/linux/efi.h         |  6 ----
 2 files changed, 48 insertions(+), 43 deletions(-)
drivers/firmware/efi/vars.c +48 -37
index d2a49626a335..1851f492368f 100644
@@ -37,6 +37,14 @@
 /* Private pointer to registered efivars */
 static struct efivars *__efivars;
 
+/*
+ * efivars_lock protects three things:
+ * 1) efivarfs_list and efivars_sysfs_list
+ * 2) ->ops calls
+ * 3) (un)registration of __efivars
+ */
+static DEFINE_SPINLOCK(efivars_lock);
+
 static bool efivar_wq_enabled = true;
 DECLARE_WORK(efivar_work, NULL);
 EXPORT_SYMBOL_GPL(efivar_work);
@@ -387,7 +395,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 		return -ENOMEM;
 	}
 
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 
 	/*
 	 * Per EFI spec, the maximum storage allocated for both
@@ -403,7 +411,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 		switch (status) {
 		case EFI_SUCCESS:
 			if (!atomic)
-				spin_unlock_irq(&__efivars->lock);
+				spin_unlock_irq(&efivars_lock);
 
 			variable_name_size = var_name_strnsize(variable_name,
 							       variable_name_size);
@@ -421,7 +429,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 				dup_variable_bug(variable_name, &vendor_guid,
 						 variable_name_size);
 				if (!atomic)
-					spin_lock_irq(&__efivars->lock);
+					spin_lock_irq(&efivars_lock);
 
 				status = EFI_NOT_FOUND;
 				break;
@@ -432,7 +440,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 				status = EFI_NOT_FOUND;
 
 			if (!atomic)
-				spin_lock_irq(&__efivars->lock);
+				spin_lock_irq(&efivars_lock);
 
 			break;
 		case EFI_NOT_FOUND:
@@ -446,7 +454,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 
 	} while (status != EFI_NOT_FOUND);
 
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 
 	kfree(variable_name);
 
@@ -461,9 +469,9 @@ EXPORT_SYMBOL_GPL(efivar_init);
  */
 void efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
 {
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 	list_add(&entry->list, head);
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 }
 EXPORT_SYMBOL_GPL(efivar_entry_add);
 
@@ -473,9 +481,9 @@ EXPORT_SYMBOL_GPL(efivar_entry_add);
  */
 void efivar_entry_remove(struct efivar_entry *entry)
 {
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 	list_del(&entry->list);
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 }
 EXPORT_SYMBOL_GPL(efivar_entry_remove);
 
@@ -492,10 +500,10 @@ EXPORT_SYMBOL_GPL(efivar_entry_remove);
  */
 static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
 {
-	lockdep_assert_held(&__efivars->lock);
+	lockdep_assert_held(&efivars_lock);
 
 	list_del(&entry->list);
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 }
 
 /**
@@ -518,7 +526,7 @@ int __efivar_entry_delete(struct efivar_entry *entry)
 	const struct efivar_operations *ops = __efivars->ops;
 	efi_status_t status;
 
-	lockdep_assert_held(&__efivars->lock);
+	lockdep_assert_held(&efivars_lock);
 
 	status = ops->set_variable(entry->var.VariableName,
 				   &entry->var.VendorGuid,
@@ -544,12 +552,12 @@ int efivar_entry_delete(struct efivar_entry *entry)
 	const struct efivar_operations *ops = __efivars->ops;
 	efi_status_t status;
 
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 	status = ops->set_variable(entry->var.VariableName,
 				   &entry->var.VendorGuid,
 				   0, 0, NULL);
 	if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
-		spin_unlock_irq(&__efivars->lock);
+		spin_unlock_irq(&efivars_lock);
 		return efi_status_to_err(status);
 	}
 
@@ -587,10 +595,10 @@ int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
 	efi_char16_t *name = entry->var.VariableName;
 	efi_guid_t vendor = entry->var.VendorGuid;
 
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 
 	if (head && efivar_entry_find(name, vendor, head, false)) {
-		spin_unlock_irq(&__efivars->lock);
+		spin_unlock_irq(&efivars_lock);
 		return -EEXIST;
 	}
 
@@ -599,7 +607,7 @@ int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
 		status = ops->set_variable(name, &vendor,
 					   attributes, size, data);
 
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 
 	return efi_status_to_err(status);
 
@@ -613,7 +621,7 @@ EXPORT_SYMBOL_GPL(efivar_entry_set);
  * from crash/panic handlers.
  *
  * Crucially, this function will not block if it cannot acquire
- * __efivars->lock. Instead, it returns -EBUSY.
+ * efivars_lock. Instead, it returns -EBUSY.
  */
 static int
 efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
@@ -623,20 +631,20 @@ efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
 	unsigned long flags;
 	efi_status_t status;
 
-	if (!spin_trylock_irqsave(&__efivars->lock, flags))
+	if (!spin_trylock_irqsave(&efivars_lock, flags))
 		return -EBUSY;
 
 	status = check_var_size_nonblocking(attributes,
 					    size + ucs2_strsize(name, 1024));
 	if (status != EFI_SUCCESS) {
-		spin_unlock_irqrestore(&__efivars->lock, flags);
+		spin_unlock_irqrestore(&efivars_lock, flags);
 		return -ENOSPC;
 	}
 
 	status = ops->set_variable_nonblocking(name, &vendor, attributes,
 					       size, data);
 
-	spin_unlock_irqrestore(&__efivars->lock, flags);
+	spin_unlock_irqrestore(&efivars_lock, flags);
 	return efi_status_to_err(status);
 }
 
@@ -682,21 +690,21 @@ int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
 						    size, data);
 
 	if (!block) {
-		if (!spin_trylock_irqsave(&__efivars->lock, flags))
+		if (!spin_trylock_irqsave(&efivars_lock, flags))
 			return -EBUSY;
 	} else {
-		spin_lock_irqsave(&__efivars->lock, flags);
+		spin_lock_irqsave(&efivars_lock, flags);
 	}
 
 	status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
 	if (status != EFI_SUCCESS) {
-		spin_unlock_irqrestore(&__efivars->lock, flags);
+		spin_unlock_irqrestore(&efivars_lock, flags);
 		return -ENOSPC;
 	}
 
 	status = ops->set_variable(name, &vendor, attributes, size, data);
 
-	spin_unlock_irqrestore(&__efivars->lock, flags);
+	spin_unlock_irqrestore(&efivars_lock, flags);
 
 	return efi_status_to_err(status);
 }
@@ -726,7 +734,7 @@ struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
 	int strsize1, strsize2;
 	bool found = false;
 
-	lockdep_assert_held(&__efivars->lock);
+	lockdep_assert_held(&efivars_lock);
 
 	list_for_each_entry_safe(entry, n, head, list) {
 		strsize1 = ucs2_strsize(name, 1024);
@@ -769,10 +777,10 @@ int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
 
 	*size = 0;
 
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 	status = ops->get_variable(entry->var.VariableName,
 				   &entry->var.VendorGuid, NULL, size, NULL);
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 
 	if (status != EFI_BUFFER_TOO_SMALL)
 		return efi_status_to_err(status);
@@ -798,7 +806,7 @@ int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
 	const struct efivar_operations *ops = __efivars->ops;
 	efi_status_t status;
 
-	lockdep_assert_held(&__efivars->lock);
+	lockdep_assert_held(&efivars_lock);
 
 	status = ops->get_variable(entry->var.VariableName,
 				   &entry->var.VendorGuid,
@@ -821,11 +829,11 @@ int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
 	const struct efivar_operations *ops = __efivars->ops;
 	efi_status_t status;
 
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 	status = ops->get_variable(entry->var.VariableName,
 				   &entry->var.VendorGuid,
 				   attributes, size, data);
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 
 	return efi_status_to_err(status);
 }
@@ -872,7 +880,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 	 * set_variable call, and removal of the variable from the efivars
 	 * list (in the case of an authenticated delete).
 	 */
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 
 	/*
 	 * Ensure that the available space hasn't shrunk below the safe level
@@ -912,7 +920,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 	if (status == EFI_NOT_FOUND)
 		efivar_entry_list_del_unlock(entry);
 	else
-		spin_unlock_irq(&__efivars->lock);
+		spin_unlock_irq(&efivars_lock);
 
 	if (status && status != EFI_BUFFER_TOO_SMALL)
 		return efi_status_to_err(status);
@@ -920,7 +928,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 	return 0;
 
 out:
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 	return err;
 
 }
@@ -935,7 +943,7 @@ EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
  */
 void efivar_entry_iter_begin(void)
 {
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 }
 EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
 
@@ -946,7 +954,7 @@ EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
  */
 void efivar_entry_iter_end(void)
 {
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 }
 EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
 
@@ -1067,11 +1075,12 @@ int efivars_register(struct efivars *efivars,
 		     const struct efivar_operations *ops,
 		     struct kobject *kobject)
 {
-	spin_lock_init(&efivars->lock);
+	spin_lock_irq(&efivars_lock);
 	efivars->ops = ops;
 	efivars->kobject = kobject;
 
 	__efivars = efivars;
+	spin_unlock_irq(&efivars_lock);
 
 	return 0;
 }
@@ -1088,6 +1097,7 @@ int efivars_unregister(struct efivars *efivars)
 {
 	int rv;
 
+	spin_lock_irq(&efivars_lock);
 	if (!__efivars) {
 		printk(KERN_ERR "efivars not registered\n");
 		rv = -EINVAL;
@@ -1103,6 +1113,7 @@ int efivars_unregister(struct efivars *efivars)
 
 	rv = 0;
 out:
+	spin_unlock_irq(&efivars_lock);
 	return rv;
 }
 EXPORT_SYMBOL_GPL(efivars_unregister);
include/linux/efi.h +0 -6
index 09f1559e7525..fba46fb6c4a8 100644
@@ -1099,12 +1099,6 @@ struct efivar_operations {
 };
 
 struct efivars {
-	/*
-	 * ->lock protects two things:
-	 * 1) efivarfs_list and efivars_sysfs_list
-	 * 2) ->ops calls
-	 */
-	spinlock_t lock;
 	struct kset *kset;
 	struct kobject *kobject;
 	const struct efivar_operations *ops;
Signature · 1 line
2.6.4
#18 lore raw

PATCH v2 2/3 efi: don't use spinlocks for efi vars

In reply to a message not in the archive

From: Sylvain Chouleur <[email protected]>

All efivars operations are protected by a spinlock which prevents interruptions and preemption. This is too restricted, we just need a lock preventing concurency.
The idea is to use a semaphore of count 1 and to have two ways of locking, depending on the context:
- In interrupt context, we call down_trylock(), if it fails we return an error
- In normal context, we call down_interruptible()

We don't use a mutex here because the mutex_trylock() function must not be called from interrupt context, whereas the down_trylock() can.

---
 drivers/firmware/efi/efi-pstore.c |  36 +++++++---
 drivers/firmware/efi/efivars.c    |  20 ++++--
 drivers/firmware/efi/vars.c       | 145 +++++++++++++++++++++++---------------
 fs/efivarfs/inode.c               |   5 +-
 fs/efivarfs/super.c               |   9 ++-
 include/linux/efi.h               |   6 +-
 6 files changed, 147 insertions(+), 74 deletions(-)
drivers/firmware/efi/efi-pstore.c +27 -9
index eac76a79a880..7bfaffb0e672 100644
@@ -121,16 +121,19 @@ static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos,
  * @entry: deleting entry
  * @turn_off_scanning: Check if a scanning flag should be turned off
  */
-static inline void __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
+static inline int __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
 						bool turn_off_scanning)
 {
 	if (entry->deleting) {
 		list_del(&entry->list);
 		efivar_entry_iter_end();
 		efivar_unregister(entry);
-		efivar_entry_iter_begin();
+		if (efivar_entry_iter_begin())
+			return -EINTR;
 	} else if (turn_off_scanning)
 		entry->scanning = false;
+
+	return 0;
 }
 
 /**
@@ -140,13 +143,18 @@ static inline void __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
  * @head: list head
  * @stop: a flag checking if scanning will stop
  */
-static void efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
+static int efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
 				       struct efivar_entry *next,
 				       struct list_head *head, bool stop)
 {
-	__efi_pstore_scan_sysfs_exit(pos, true);
+	int ret = __efi_pstore_scan_sysfs_exit(pos, true);
+
+	if (ret)
+		return ret;
+
 	if (stop)
-		__efi_pstore_scan_sysfs_exit(next, &next->list != head);
+		ret = __efi_pstore_scan_sysfs_exit(next, &next->list != head);
+	return ret;
 }
 
 /**
@@ -168,13 +176,17 @@ static int efi_pstore_sysfs_entry_iter(void *data, struct efivar_entry **pos)
 	struct efivar_entry *entry, *n;
 	struct list_head *head = &efivar_sysfs_list;
 	int size = 0;
+	int ret;
 
 	if (!*pos) {
 		list_for_each_entry_safe(entry, n, head, list) {
 			efi_pstore_scan_sysfs_enter(entry, n, head);
 
 			size = efi_pstore_read_func(entry, data);
-			efi_pstore_scan_sysfs_exit(entry, n, head, size < 0);
+			ret = efi_pstore_scan_sysfs_exit(entry, n, head,
+							 size < 0);
+			if (ret)
+				return ret;
 			if (size)
 				break;
 		}
@@ -186,7 +198,9 @@ static int efi_pstore_sysfs_entry_iter(void *data, struct efivar_entry **pos)
 		efi_pstore_scan_sysfs_enter((*pos), n, head);
 
 		size = efi_pstore_read_func((*pos), data);
-		efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
+		ret = efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
+		if (ret)
+			return ret;
 		if (size)
 			break;
 	}
@@ -226,7 +240,10 @@ static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
 	if (!*data.buf)
 		return -ENOMEM;
 
-	efivar_entry_iter_begin();
+	if (efivar_entry_iter_begin()) {
+		kfree(*data.buf);
+		return -EINTR;
+	}
 	size = efi_pstore_sysfs_entry_iter(&data,
 					   (struct efivar_entry **)&psi->data);
 	efivar_entry_iter_end();
@@ -341,7 +358,8 @@ static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
 	edata.time = time;
 	edata.name = efi_name;
 
-	efivar_entry_iter_begin();
+	if (efivar_entry_iter_begin())
+		return -EINTR;
 	found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
 
 	if (found && !entry->scanning) {
drivers/firmware/efi/efivars.c +16 -4
index 756eca8c4cf8..705feb129c97 100644
@@ -509,7 +509,8 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
 		vendor = del_var->VendorGuid;
 	}
 
-	efivar_entry_iter_begin();
+	if (efivar_entry_iter_begin())
+		return -EINTR;
 	entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
 	if (!entry)
 		err = -EINVAL;
@@ -582,7 +583,8 @@ efivar_create_sysfs_entry(struct efivar_entry *new_var)
 		return ret;
 
 	kobject_uevent(&new_var->kobj, KOBJ_ADD);
-	efivar_entry_add(new_var, &efivar_sysfs_list);
+	if (efivar_entry_add(new_var, &efivar_sysfs_list))
+		return -EINTR;
 
 	return 0;
 }
@@ -697,7 +699,10 @@ static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
 
 static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
 {
-	efivar_entry_remove(entry);
+	int err = efivar_entry_remove(entry);
+
+	if (err)
+		return err;
 	efivar_unregister(entry);
 	return 0;
 }
@@ -705,7 +710,14 @@ static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
 static void efivars_sysfs_exit(void)
 {
 	/* Remove all entries and destroy */
-	__efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list, NULL, NULL);
+	int err;
+
+	err = __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list,
+				  NULL, NULL);
+	if (err) {
+		pr_err("efivars: Failed to destroy sysfs entries\n");
+		return;
+	}
 
 	if (efivars_new_var)
 		sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
drivers/firmware/efi/vars.c +90 -55

382 lines not on this page · raw message

fs/efivarfs/inode.c +4 -1
index 3381b9da9ee6..5cea84ef7a44 100644
@@ -132,7 +132,10 @@ static int efivarfs_create(struct inode *dir, struct dentry *dentry,
 
 	inode->i_private = var;
 
-	efivar_entry_add(var, &efivarfs_list);
+	err = efivar_entry_add(var, &efivarfs_list);
+	if (err)
+		goto out;
+
 	d_instantiate(dentry, inode);
 	dget(dentry);
 out:
fs/efivarfs/super.c +7 -2
index 86a2121828c3..6825f1e9718b 100644
@@ -158,7 +158,9 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
 	kfree(name);
 
 	efivar_entry_size(entry, &size);
-	efivar_entry_add(entry, &efivarfs_list);
+	err = efivar_entry_add(entry, &efivarfs_list);
+	if (err)
+		goto fail_inode;
 
 	mutex_lock(&inode->i_mutex);
 	inode->i_private = entry;
@@ -179,7 +181,10 @@ fail:
 
 static int efivarfs_destroy(struct efivar_entry *entry, void *data)
 {
-	efivar_entry_remove(entry);
+	int err = efivar_entry_remove(entry);
+
+	if (err)
+		return err;
 	kfree(entry);
 	return 0;
 }
include/linux/efi.h +3 -3
index fba46fb6c4a8..6b38829795a9 100644
@@ -1166,8 +1166,8 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 		void *data, bool atomic, bool duplicates,
 		struct list_head *head);
 
-void efivar_entry_add(struct efivar_entry *entry, struct list_head *head);
-void efivar_entry_remove(struct efivar_entry *entry);
+int efivar_entry_add(struct efivar_entry *entry, struct list_head *head);
+int efivar_entry_remove(struct efivar_entry *entry);
 
 int __efivar_entry_delete(struct efivar_entry *entry);
 int efivar_entry_delete(struct efivar_entry *entry);
@@ -1184,7 +1184,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
 			  bool block, unsigned long size, void *data);
 
-void efivar_entry_iter_begin(void);
+int efivar_entry_iter_begin(void);
 void efivar_entry_iter_end(void);
 
 int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
Signature · 1 line
2.6.4
#19 lore raw

PATCH v2 3/3 efi: implement interruptible runtime services

In reply to Sylvain Chouleur

From: Sylvain Chouleur <[email protected]>

This patch adds an implementation of EFI runtime services wappers which allow interrupts and preemption during execution of BIOS code.

It's needed by Broxton platform which requires the OS to do the write of the non volatile variables. To do that, the OS will receive an interrupt coming from the firmware to notify that it must write the data.

BIOS code must be executed using a specific PGD. This is currently done by efi_call() which force value of CR3 before calling BIOS and restore previous value after.

We use a dedicated kthread which will execute all interruptible runtime services. This efi_kthread is special because it has it's own mm_struct whereas kthread should not have one. This mm_struct contains the EFI PGD so the scheduler will load it before running any BIOS code.

Obviously, an interruptible runtime service must never be called from an interrupt context. EFI pstore is the only use case here, that's why we require it to be disabled when activating interruptible runtime services.

---
 arch/x86/Kconfig                          |  17 +++
 arch/x86/include/asm/efi.h                |   1 +
 arch/x86/platform/efi/Makefile            |   1 +
 arch/x86/platform/efi/efi_32.c            |   5 +
 arch/x86/platform/efi/efi_64.c            |   5 +
 arch/x86/platform/efi/efi_interruptible.c | 191 ++++++++++++++++++++++++++++++
 6 files changed, 220 insertions(+)
 create mode 100644 arch/x86/platform/efi/efi_interruptible.c
arch/x86/Kconfig +17 -0
index db3622f22b61..37301516f4e6 100644
@@ -1720,6 +1720,23 @@ config EFI_MIXED
 
 	   If unsure, say N.
 
+if X86_64
+config EFI_INTERRUPTIBLE
+	bool "Interruptible Efi Runtime Services"
+	depends on EFI && !EFI_VARS_PSTORE
+	default n
+	---help---
+	  Only use this if you really know what you are doing, you
+	  possibly risk to break your BIOS.
+
+	  This is an interruptible implementation of efi runtime
+	  services wrappers. If you say Y, BIOS code could be
+	  interrupted and preempted as a standard process. Enable this
+	  only if you are sure that your BIOS will support
+	  interruptible efi calls
+	  In doubt, say "N".
+endif
+
 config SECCOMP
 	def_bool y
 	prompt "Enable seccomp to safely compute untrusted bytecode"
arch/x86/include/asm/efi.h +1 -0
index 0010c78c4998..5e9620b3688b 100644
@@ -111,6 +111,7 @@ extern void __init efi_memory_uc(u64 addr, unsigned long size);
 extern void __init efi_map_region(efi_memory_desc_t *md);
 extern void __init efi_map_region_fixed(efi_memory_desc_t *md);
 extern void efi_sync_low_kernel_mappings(void);
+extern pgd_t *efi_get_pgd(void);
 extern int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages);
 extern void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages);
 extern void __init old_map_region(efi_memory_desc_t *md);
arch/x86/platform/efi/Makefile +1 -0
index 2846aaab5103..dcc894d1e603 100644
@@ -2,3 +2,4 @@ obj-$(CONFIG_EFI) 		+= quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o
 obj-$(CONFIG_ACPI_BGRT) += efi-bgrt.o
 obj-$(CONFIG_EARLY_PRINTK_EFI)	+= early_printk.o
 obj-$(CONFIG_EFI_MIXED)		+= efi_thunk_$(BITS).o
+obj-$(CONFIG_EFI_INTERRUPTIBLE)		+= efi_interruptible.o
arch/x86/platform/efi/efi_32.c +5 -0
index ed5b67338294..02a3c616c59e 100644
@@ -56,6 +56,11 @@ void __init efi_map_region(efi_memory_desc_t *md)
 void __init efi_map_region_fixed(efi_memory_desc_t *md) {}
 void __init parse_efi_setup(u64 phys_addr, u32 data_len) {}
 
+pgd_t *efi_get_pgd(void)
+{
+	return initial_page_table;
+}
+
 pgd_t * __init efi_call_phys_prolog(void)
 {
 	struct desc_ptr gdt_descr;
arch/x86/platform/efi/efi_64.c +5 -0
index d347e854a5e4..67cffb69d405 100644
@@ -143,6 +143,11 @@ void efi_sync_low_kernel_mappings(void)
 		sizeof(pgd_t) * num_pgds);
 }
 
+pgd_t *efi_get_pgd(void)
+{
+	return __va(efi_scratch.efi_pgt);
+}
+
 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
 {
 	unsigned long text;
arch/x86/platform/efi/efi_interruptible.c new file +191 -0

191 lines not on this page · raw message

Signature · 1 line
2.6.4
#5 lore raw

Re: [PATCH 2/2] efi: implement interruptible runtime services

In reply to Sylvain Chouleur
Quoted 32 lines · Sylvain Chouleur

2016-01-06 13:58 GMT+01:00 Matt Fleming <matt-mF/unelCI9GS6iBeEJttW/[email protected]>:

Quoted 31 lines · On Fri, 18 Dec, at 11:29:51AM, [email protected]…
Quoted 14 lines

From: Sylvain Chouleur <[email protected]>

arch/x86/platform/efi/efi_interruptible.c new file +12 -0
+static int efi_interruptible_panic_notifier_call(
+     struct notifier_block *notifier,
+     unsigned long what, void *data)
+{
+     static struct efivars generic_efivars;
+     static struct efivar_operations generic_ops;
+
+     generic_ops.get_variable = efi.get_variable;
+     generic_ops.set_variable = efi.set_variable;
+     generic_ops.get_next_variable = efi.get_next_variable;
+     generic_ops.query_variable_store = efi_query_variable_store;
+

arch/x86/platform/efi/efi_interruptible.c line 145

Quoted 10 lines
arch/x86/platform/efi/efi_interruptible.c new file +10 -0
+     efivars_register(&generic_efivars, &generic_ops, efivars_kobject());
+
+     return NOTIFY_DONE;
+}
+
+static struct notifier_block panic_nb = {
+     .notifier_call = efi_interruptible_panic_notifier_call,
+     .priority = 100,
+};
+

What's the purpose of this? The only reason you'd want to do this that I can think of is because you'd want efi-pstore to run and attempt to save some crash data for you. But you can't build with efi-pstore enable and CONFIG_EFI_INTERRUPTIBLE.

Quoted 6 lines
Quoted 1 line

I'd be tempted to just delete this notifier code.

This is indeed to be able to try write some crash data when we are in a panic context. In fact with this restoration of legacy efi handlers on a panic we should be able to have pstore working with CONFIG_EFI_INTERRUPTIBLE.

This makes the efivar registration more complicated because now it can fail if someone else is holding efivars_lock. Which is a strange semantic for a registration function.

Quoted 2 lines

I say should because there is still issues with BIOS to have the panic flow working.

How would it work though? You'd need the kernel thread controlling access to the flash to be run in order for any data to appear in the EFI variable store. Except you're in the middle of a panic and all bets are off regarding which tasks are going to be run by the scheduler.

Until those issues are resolved, please delete the notifier code. But honestly, I doubt this will ever be useful in practice. Setting up panic handlers *once* you've crashed is far too late.

Quoted 36 lines
Quoted 27 lines
Quoted 27 lines
arch/x86/platform/efi/efi_interruptible.c new file +27 -0
+static struct task_struct *efi_kworker_task;
+static struct efivar_operations interruptible_ops;
+static __init int efi_interruptible_init(void)
+{
+     int ret;
+
+     efi_kworker_task = kthread_create(kthread_worker_fn, &efi_kworker,
+                                       "efi_kthread");
+     if (IS_ERR(efi_kworker_task)) {
+             pr_err("efi_interruptible: Failed to create kthread\n");
+             ret = PTR_ERR(efi_kworker_task);
+             efi_kworker_task = NULL;
+             return ret;
+     }
+
+     efi_kworker_task->mm = mm_alloc();
+     efi_kworker_task->active_mm = efi_kworker_task->mm;
+     efi_kworker_task->mm->pgd = efi_get_pgd();
+     wake_up_process(efi_kworker_task);
+
+     atomic_notifier_chain_register(&panic_notifier_list, &panic_nb);
+
+     interruptible_ops.get_variable = get_variable_interruptible;
+     interruptible_ops.set_variable = set_variable_interruptible;
+     interruptible_ops.set_variable_nonblocking = non_blocking_not_allowed;
+     interruptible_ops.get_next_variable = get_next_variable_interruptible;
+     interruptible_ops.query_variable_store = efi_query_variable_store;
Quoted 8 lines

arch/x86/platform/efi/efi_interruptible.c line 176

Quoted 4 lines
arch/x86/platform/efi/efi_interruptible.c new file +4 -0
+     return efivars_register(&interruptible_efivars, &interruptible_ops,
+                             efivars_kobject());
+}
+

Is there some way we can match DMI/PCI identifiers for Broxton so that we don't start seeing people accidentally running with preemptible EFI services on non-BXT hardware?

Quoted 5 lines

I don't see how since this depends on a storage strategy more than the SoC itself, it can be used on future platforms or we can also get rid of it on BXT if an other storage is used for efi variables.
Can't the KConfig protection be enough?

Kconfig is a last resort because it's a build-time decision and greatly limits the flexibility of the kernel. It becomes no longer possible to run a single kernel image with various CONFIG_* enabled on x86 hardware - you now need a special EFI_INTERRUPTIBLE build.

Which apart from being a major headache for distributions in general is generally frowned upon for the x86 architecture.

If there's any way at all of making this a runtime decision that would be much better.

#12 lore raw
In reply to a message not in the archive
Quoted 15 lines · Sylvain Chouleur

2016-01-06 13:24 GMT+01:00 Matt Fleming <matt-mF/unelCI9GS6iBeEJttW/[email protected]>:

Quoted 8 lines
Quoted 8 lines
drivers/firmware/efi/efivars.c +2 -1
@@ -582,7 +583,8 @@ efivar_create_sysfs_entry(struct efivar_entry *new_var)
              return ret;
 
      kobject_uevent(&new_var->kobj, KOBJ_ADD);
-     efivar_entry_add(new_var, &efivar_sysfs_list);
+     if (efivar_entry_add(new_var, &efivar_sysfs_list))
+             return -EINTR;
 
Quoted 5 lines

Review efivar_create_sysfs_entry(struct efivar_entry *new_var) · line 590

Quoted 2 lines
(quoted diff) +0 -0
      return 0;
 }

This looks like it's missing a call to efivar_unregister() in the -EINTR case.

Quoted 3 lines

I don't see why
It's returning an error code as other lines in the same function. Can you develop your thoughts?

kobject_uevent() just notified userspace that a new object was available. But because it hasn't been put onto 'efivar_sysfs_list' when efivar_entry_add() fails, we'll never ever call kobject_put().

Before your changes efivar_entry_add() never failed and 'new_var' was always added to the list, and always removed in efivar_sysfs_destroy().

That invariant no longer holds. Now we leak new_var->kobj.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/firmware/efi/vars.c +0 -0
@@ -1055,12 +1087,16 @@ int efivars_register(struct efivars *efivars,
                   const struct efivar_operations *ops,
                   struct kobject *kobject)
 {
Quoted 8 lines

drivers/firmware/efi/vars.c int efivars_register(struct efivars *efivars, · line 1092

Quoted 4 lines
drivers/firmware/efi/vars.c +3 -1
-     spin_lock_init(&efivars->lock);
+     if (down_trylock(&efivars_lock))
+             return -EBUSY;
+

Is this correct? I would have assumed that you'd want to return -EINTR here, not -EBUSY since if an EFI variable operation is currently running we should spin waiting for the semaphore to be released.

Quoted 4 lines

No because this is a trylock, it will not wait for the semaphore to be release, it will return as soon as it sees that the semaphore is locked.
We don't want to use down_interruptible() here because we want to be able to (un)register efivars operation in an interrupt context.

Like I said in the other mail, I think supporting registration from within a panic context is a bad idea. Using down_interruptible() here would be much better.

#6 lore raw

Re: [PATCH 2/2] efi: implement interruptible runtime services

In reply to Matt Fleming

2016-01-08 11:38 GMT+01:00 Matt Fleming <[email protected]>:

Quoted 35 lines · Sylvain Chouleur
Quoted 34 lines

2016-01-06 13:58 GMT+01:00 Matt Fleming <[email protected]>:

Quoted 32 lines · On Fri, 18 Dec, at 11:29:51AM, [email protected] wrote:
Quoted 14 lines

From: Sylvain Chouleur <[email protected]>

arch/x86/platform/efi/efi_interruptible.c new file +12 -0
+static int efi_interruptible_panic_notifier_call(
+     struct notifier_block *notifier,
+     unsigned long what, void *data)
+{
+     static struct efivars generic_efivars;
+     static struct efivar_operations generic_ops;
+
+     generic_ops.get_variable = efi.get_variable;
+     generic_ops.set_variable = efi.set_variable;
+     generic_ops.get_next_variable = efi.get_next_variable;
+     generic_ops.query_variable_store = efi_query_variable_store;
+

arch/x86/platform/efi/efi_interruptible.c line 145

Quoted 10 lines
arch/x86/platform/efi/efi_interruptible.c new file +10 -0
+     efivars_register(&generic_efivars, &generic_ops, efivars_kobject());
+
+     return NOTIFY_DONE;
+}
+
+static struct notifier_block panic_nb = {
+     .notifier_call = efi_interruptible_panic_notifier_call,
+     .priority = 100,
+};
+

What's the purpose of this? The only reason you'd want to do this that I can think of is because you'd want efi-pstore to run and attempt to save some crash data for you. But you can't build with efi-pstore enable and CONFIG_EFI_INTERRUPTIBLE.

I'd be tempted to just delete this notifier code.

Quoted 8 lines
Quoted 4 lines

This is indeed to be able to try write some crash data when we are in a panic context. In fact with this restoration of legacy efi handlers on a panic we should be able to have pstore working with CONFIG_EFI_INTERRUPTIBLE.

This makes the efivar registration more complicated because now it can fail if someone else is holding efivars_lock. Which is a strange semantic for a registration function.

Right, I'll change the code to call legacy handlers from efi_interruptible code directly it will be more consistent with a panic specific behavior.

Quoted 9 lines
Quoted 3 lines

I say should because there is still issues with BIOS to have the panic flow working.

How would it work though? You'd need the kernel thread controlling access to the flash to be run in order for any data to appear in the EFI variable store. Except you're in the middle of a panic and all bets are off regarding which tasks are going to be run by the scheduler.

The trick here is that in panic context, the cse is notified of this special context and is asked to write data itself to the storage instead of sending interrupt to the kernel. Then we don't need interruptible handler in panic context. That's why we want to use legacy efi variable handlers.

Quoted 4 lines

Until those issues are resolved, please delete the notifier code. But honestly, I doubt this will ever be useful in practice. Setting up panic handlers *once* you've crashed is far too late.

I understand, like I said above I'll modify efi_interruptible handlers to call legacy ones in case of panic context.
I would like to avoid removing the panic part of this patch and take time to clean it before merging the whole.

Quoted 48 lines
Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
arch/x86/platform/efi/efi_interruptible.c new file +27 -0
+static struct task_struct *efi_kworker_task;
+static struct efivar_operations interruptible_ops;
+static __init int efi_interruptible_init(void)
+{
+     int ret;
+
+     efi_kworker_task = kthread_create(kthread_worker_fn, &efi_kworker,
+                                       "efi_kthread");
+     if (IS_ERR(efi_kworker_task)) {
+             pr_err("efi_interruptible: Failed to create kthread\n");
+             ret = PTR_ERR(efi_kworker_task);
+             efi_kworker_task = NULL;
+             return ret;
+     }
+
+     efi_kworker_task->mm = mm_alloc();
+     efi_kworker_task->active_mm = efi_kworker_task->mm;
+     efi_kworker_task->mm->pgd = efi_get_pgd();
+     wake_up_process(efi_kworker_task);
+
+     atomic_notifier_chain_register(&panic_notifier_list, &panic_nb);
+
+     interruptible_ops.get_variable = get_variable_interruptible;
+     interruptible_ops.set_variable = set_variable_interruptible;
+     interruptible_ops.set_variable_nonblocking = non_blocking_not_allowed;
+     interruptible_ops.get_next_variable = get_next_variable_interruptible;
+     interruptible_ops.query_variable_store = efi_query_variable_store;
Quoted 8 lines

arch/x86/platform/efi/efi_interruptible.c line 176

Quoted 4 lines
arch/x86/platform/efi/efi_interruptible.c new file +4 -0
+     return efivars_register(&interruptible_efivars, &interruptible_ops,
+                             efivars_kobject());
+}
+

Is there some way we can match DMI/PCI identifiers for Broxton so that we don't start seeing people accidentally running with preemptible EFI services on non-BXT hardware?

Quoted 5 lines

I don't see how since this depends on a storage strategy more than the SoC itself, it can be used on future platforms or we can also get rid of it on BXT if an other storage is used for efi variables.
Can't the KConfig protection be enough?

Kconfig is a last resort because it's a build-time decision and greatly limits the flexibility of the kernel. It becomes no longer possible to run a single kernel image with various CONFIG_* enabled on x86 hardware - you now need a special EFI_INTERRUPTIBLE build.

Quoted 5 lines

Which apart from being a major headache for distributions in general is generally frowned upon for the x86 architecture.

If there's any way at all of making this a runtime decision that would be much better.

I think the best would be to bind this driver with the one which receives the interrupts from CSE to write the variables. Then we would have a consistency on the feature.
Does this seems ok for you?

Signature · 1 line
Sylvain
#14 lore raw
In reply to a message not in the archive

2016-01-08 12:08 GMT+01:00 Matt Fleming <matt-mF/unelCI9GS6iBeEJttW/[email protected]>:

Quoted 20 lines · Sylvain Chouleur
Quoted 15 lines

2016-01-06 13:24 GMT+01:00 Matt Fleming <matt-mF/unelCI9GS6iBeEJttW/[email protected]>:

Quoted 8 lines
Quoted 8 lines
(quoted diff) +2 -1
@@ -582,7 +583,8 @@ efivar_create_sysfs_entry(struct efivar_entry *new_var)
              return ret;
 
      kobject_uevent(&new_var->kobj, KOBJ_ADD);
-     efivar_entry_add(new_var, &efivar_sysfs_list);
+     if (efivar_entry_add(new_var, &efivar_sysfs_list))
+             return -EINTR;
 
Quoted 5 lines

Review efivar_create_sysfs_entry(struct efivar_entry *new_var) · line 590

Quoted 2 lines
(quoted diff) +0 -0
      return 0;
 }

This looks like it's missing a call to efivar_unregister() in the -EINTR case.

Quoted 3 lines

I don't see why
It's returning an error code as other lines in the same function. Can you develop your thoughts?

Quoted 8 lines

kobject_uevent() just notified userspace that a new object was available. But because it hasn't been put onto 'efivar_sysfs_list' when efivar_entry_add() fails, we'll never ever call kobject_put().

Before your changes efivar_entry_add() never failed and 'new_var' was always added to the list, and always removed in efivar_sysfs_destroy().

That invariant no longer holds. Now we leak new_var->kobj.

Got it.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines

Review int efivars_register(struct efivars *efivars, · line 1092

Quoted 9 lines
(quoted diff) +3 -1
@@ -1055,12 +1087,16 @@ int efivars_register(struct efivars *efivars,
                   const struct efivar_operations *ops,
                   struct kobject *kobject)
 {
-     spin_lock_init(&efivars->lock);
+     if (down_trylock(&efivars_lock))
+             return -EBUSY;
+

Is this correct? I would have assumed that you'd want to return -EINTR here, not -EBUSY since if an EFI variable operation is currently running we should spin waiting for the semaphore to be released.

Quoted 8 lines
Quoted 4 lines

No because this is a trylock, it will not wait for the semaphore to be release, it will return as soon as it sees that the semaphore is locked.
We don't want to use down_interruptible() here because we want to be able to (un)register efivars operation in an interrupt context.

Like I said in the other mail, I think supporting registration from within a panic context is a bad idea. Using down_interruptible() here would be much better.

Ok

#20 lore raw

v3 cover efi interruptible runtime services

In reply to Sylvain Chouleur

From: Sylvain Chouleur <[email protected]>

Changes in v3:
- efivars_(un)register: use down_interruptible()
- efivar_create_sysfs_entry: unregister new_var in case of failure
- efi_interruptible: in case of panic call directly legacy handlers

Sylvain Chouleur (3):
efi: implement interruptible runtime services
efi: don't use spinlocks for efi vars
efi: use a file local lock for efivars

 arch/x86/Kconfig                          |   17 +++
 arch/x86/include/asm/efi.h                |    1 
 arch/x86/platform/efi/Makefile            |    1 
 arch/x86/platform/efi/efi_32.c            |    5 +
 arch/x86/platform/efi/efi_64.c            |    5 +
 arch/x86/platform/efi/efi_interruptible.c |  189 ++++++++++++++++++++++++++++++++++++++++
 drivers/firmware/efi/efi-pstore.c         |   36 +++++--
 drivers/firmware/efi/efivars.c            |   22 +++-
 drivers/firmware/efi/vars.c               |  315 ++++++++++++++++++++++++++++++++++++++++----------------------------
 fs/efivarfs/inode.c                       |    5 -
 fs/efivarfs/super.c                       |    9 +
 include/linux/efi.h                       |   18 ---
 12 files changed, 463 insertions(+), 160 deletions(-)
#22 lore raw

PATCH v3 1/3 efi: use a file local lock for efivars

In reply to a message not in the archive

From: Sylvain Chouleur <[email protected]>

This patch remove the efivars lock to use a single lock for the whole vars.c file. The goal of this lock is to protect concurrent calls to efi variable services, registering and unregistering. This allows to register new efivars operations without having in-progress call.

---
 drivers/firmware/efi/vars.c | 85 +++++++++++++++++++++++++--------------------
 include/linux/efi.h         |  6 ----
 2 files changed, 48 insertions(+), 43 deletions(-)
drivers/firmware/efi/vars.c +48 -37
index d2a49626a335..1851f492368f 100644
@@ -37,6 +37,14 @@
 /* Private pointer to registered efivars */
 static struct efivars *__efivars;
 
+/*
+ * efivars_lock protects three things:
+ * 1) efivarfs_list and efivars_sysfs_list
+ * 2) ->ops calls
+ * 3) (un)registration of __efivars
+ */
+static DEFINE_SPINLOCK(efivars_lock);
+
 static bool efivar_wq_enabled = true;
 DECLARE_WORK(efivar_work, NULL);
 EXPORT_SYMBOL_GPL(efivar_work);
@@ -387,7 +395,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 		return -ENOMEM;
 	}
 
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 
 	/*
 	 * Per EFI spec, the maximum storage allocated for both
@@ -403,7 +411,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 		switch (status) {
 		case EFI_SUCCESS:
 			if (!atomic)
-				spin_unlock_irq(&__efivars->lock);
+				spin_unlock_irq(&efivars_lock);
 
 			variable_name_size = var_name_strnsize(variable_name,
 							       variable_name_size);
@@ -421,7 +429,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 				dup_variable_bug(variable_name, &vendor_guid,
 						 variable_name_size);
 				if (!atomic)
-					spin_lock_irq(&__efivars->lock);
+					spin_lock_irq(&efivars_lock);
 
 				status = EFI_NOT_FOUND;
 				break;
@@ -432,7 +440,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 				status = EFI_NOT_FOUND;
 
 			if (!atomic)
-				spin_lock_irq(&__efivars->lock);
+				spin_lock_irq(&efivars_lock);
 
 			break;
 		case EFI_NOT_FOUND:
@@ -446,7 +454,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 
 	} while (status != EFI_NOT_FOUND);
 
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 
 	kfree(variable_name);
 
@@ -461,9 +469,9 @@ EXPORT_SYMBOL_GPL(efivar_init);
  */
 void efivar_entry_add(struct efivar_entry *entry, struct list_head *head)
 {
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 	list_add(&entry->list, head);
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 }
 EXPORT_SYMBOL_GPL(efivar_entry_add);
 
@@ -473,9 +481,9 @@ EXPORT_SYMBOL_GPL(efivar_entry_add);
  */
 void efivar_entry_remove(struct efivar_entry *entry)
 {
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 	list_del(&entry->list);
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 }
 EXPORT_SYMBOL_GPL(efivar_entry_remove);
 
@@ -492,10 +500,10 @@ EXPORT_SYMBOL_GPL(efivar_entry_remove);
  */
 static void efivar_entry_list_del_unlock(struct efivar_entry *entry)
 {
-	lockdep_assert_held(&__efivars->lock);
+	lockdep_assert_held(&efivars_lock);
 
 	list_del(&entry->list);
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 }
 
 /**
@@ -518,7 +526,7 @@ int __efivar_entry_delete(struct efivar_entry *entry)
 	const struct efivar_operations *ops = __efivars->ops;
 	efi_status_t status;
 
-	lockdep_assert_held(&__efivars->lock);
+	lockdep_assert_held(&efivars_lock);
 
 	status = ops->set_variable(entry->var.VariableName,
 				   &entry->var.VendorGuid,
@@ -544,12 +552,12 @@ int efivar_entry_delete(struct efivar_entry *entry)
 	const struct efivar_operations *ops = __efivars->ops;
 	efi_status_t status;
 
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 	status = ops->set_variable(entry->var.VariableName,
 				   &entry->var.VendorGuid,
 				   0, 0, NULL);
 	if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND)) {
-		spin_unlock_irq(&__efivars->lock);
+		spin_unlock_irq(&efivars_lock);
 		return efi_status_to_err(status);
 	}
 
@@ -587,10 +595,10 @@ int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
 	efi_char16_t *name = entry->var.VariableName;
 	efi_guid_t vendor = entry->var.VendorGuid;
 
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 
 	if (head && efivar_entry_find(name, vendor, head, false)) {
-		spin_unlock_irq(&__efivars->lock);
+		spin_unlock_irq(&efivars_lock);
 		return -EEXIST;
 	}
 
@@ -599,7 +607,7 @@ int efivar_entry_set(struct efivar_entry *entry, u32 attributes,
 		status = ops->set_variable(name, &vendor,
 					   attributes, size, data);
 
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 
 	return efi_status_to_err(status);
 
@@ -613,7 +621,7 @@ EXPORT_SYMBOL_GPL(efivar_entry_set);
  * from crash/panic handlers.
  *
  * Crucially, this function will not block if it cannot acquire
- * __efivars->lock. Instead, it returns -EBUSY.
+ * efivars_lock. Instead, it returns -EBUSY.
  */
 static int
 efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
@@ -623,20 +631,20 @@ efivar_entry_set_nonblocking(efi_char16_t *name, efi_guid_t vendor,
 	unsigned long flags;
 	efi_status_t status;
 
-	if (!spin_trylock_irqsave(&__efivars->lock, flags))
+	if (!spin_trylock_irqsave(&efivars_lock, flags))
 		return -EBUSY;
 
 	status = check_var_size_nonblocking(attributes,
 					    size + ucs2_strsize(name, 1024));
 	if (status != EFI_SUCCESS) {
-		spin_unlock_irqrestore(&__efivars->lock, flags);
+		spin_unlock_irqrestore(&efivars_lock, flags);
 		return -ENOSPC;
 	}
 
 	status = ops->set_variable_nonblocking(name, &vendor, attributes,
 					       size, data);
 
-	spin_unlock_irqrestore(&__efivars->lock, flags);
+	spin_unlock_irqrestore(&efivars_lock, flags);
 	return efi_status_to_err(status);
 }
 
@@ -682,21 +690,21 @@ int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
 						    size, data);
 
 	if (!block) {
-		if (!spin_trylock_irqsave(&__efivars->lock, flags))
+		if (!spin_trylock_irqsave(&efivars_lock, flags))
 			return -EBUSY;
 	} else {
-		spin_lock_irqsave(&__efivars->lock, flags);
+		spin_lock_irqsave(&efivars_lock, flags);
 	}
 
 	status = check_var_size(attributes, size + ucs2_strsize(name, 1024));
 	if (status != EFI_SUCCESS) {
-		spin_unlock_irqrestore(&__efivars->lock, flags);
+		spin_unlock_irqrestore(&efivars_lock, flags);
 		return -ENOSPC;
 	}
 
 	status = ops->set_variable(name, &vendor, attributes, size, data);
 
-	spin_unlock_irqrestore(&__efivars->lock, flags);
+	spin_unlock_irqrestore(&efivars_lock, flags);
 
 	return efi_status_to_err(status);
 }
@@ -726,7 +734,7 @@ struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
 	int strsize1, strsize2;
 	bool found = false;
 
-	lockdep_assert_held(&__efivars->lock);
+	lockdep_assert_held(&efivars_lock);
 
 	list_for_each_entry_safe(entry, n, head, list) {
 		strsize1 = ucs2_strsize(name, 1024);
@@ -769,10 +777,10 @@ int efivar_entry_size(struct efivar_entry *entry, unsigned long *size)
 
 	*size = 0;
 
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 	status = ops->get_variable(entry->var.VariableName,
 				   &entry->var.VendorGuid, NULL, size, NULL);
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 
 	if (status != EFI_BUFFER_TOO_SMALL)
 		return efi_status_to_err(status);
@@ -798,7 +806,7 @@ int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
 	const struct efivar_operations *ops = __efivars->ops;
 	efi_status_t status;
 
-	lockdep_assert_held(&__efivars->lock);
+	lockdep_assert_held(&efivars_lock);
 
 	status = ops->get_variable(entry->var.VariableName,
 				   &entry->var.VendorGuid,
@@ -821,11 +829,11 @@ int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,
 	const struct efivar_operations *ops = __efivars->ops;
 	efi_status_t status;
 
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 	status = ops->get_variable(entry->var.VariableName,
 				   &entry->var.VendorGuid,
 				   attributes, size, data);
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 
 	return efi_status_to_err(status);
 }
@@ -872,7 +880,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 	 * set_variable call, and removal of the variable from the efivars
 	 * list (in the case of an authenticated delete).
 	 */
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 
 	/*
 	 * Ensure that the available space hasn't shrunk below the safe level
@@ -912,7 +920,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 	if (status == EFI_NOT_FOUND)
 		efivar_entry_list_del_unlock(entry);
 	else
-		spin_unlock_irq(&__efivars->lock);
+		spin_unlock_irq(&efivars_lock);
 
 	if (status && status != EFI_BUFFER_TOO_SMALL)
 		return efi_status_to_err(status);
@@ -920,7 +928,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 	return 0;
 
 out:
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 	return err;
 
 }
@@ -935,7 +943,7 @@ EXPORT_SYMBOL_GPL(efivar_entry_set_get_size);
  */
 void efivar_entry_iter_begin(void)
 {
-	spin_lock_irq(&__efivars->lock);
+	spin_lock_irq(&efivars_lock);
 }
 EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
 
@@ -946,7 +954,7 @@ EXPORT_SYMBOL_GPL(efivar_entry_iter_begin);
  */
 void efivar_entry_iter_end(void)
 {
-	spin_unlock_irq(&__efivars->lock);
+	spin_unlock_irq(&efivars_lock);
 }
 EXPORT_SYMBOL_GPL(efivar_entry_iter_end);
 
@@ -1067,11 +1075,12 @@ int efivars_register(struct efivars *efivars,
 		     const struct efivar_operations *ops,
 		     struct kobject *kobject)
 {
-	spin_lock_init(&efivars->lock);
+	spin_lock_irq(&efivars_lock);
 	efivars->ops = ops;
 	efivars->kobject = kobject;
 
 	__efivars = efivars;
+	spin_unlock_irq(&efivars_lock);
 
 	return 0;
 }
@@ -1088,6 +1097,7 @@ int efivars_unregister(struct efivars *efivars)
 {
 	int rv;
 
+	spin_lock_irq(&efivars_lock);
 	if (!__efivars) {
 		printk(KERN_ERR "efivars not registered\n");
 		rv = -EINVAL;
@@ -1103,6 +1113,7 @@ int efivars_unregister(struct efivars *efivars)
 
 	rv = 0;
 out:
+	spin_unlock_irq(&efivars_lock);
 	return rv;
 }
 EXPORT_SYMBOL_GPL(efivars_unregister);
include/linux/efi.h +0 -6
index 3c6cbbdae4aa..ca005eac825d 100644
@@ -1100,12 +1100,6 @@ struct efivar_operations {
 };
 
 struct efivars {
-	/*
-	 * ->lock protects two things:
-	 * 1) efivarfs_list and efivars_sysfs_list
-	 * 2) ->ops calls
-	 */
-	spinlock_t lock;
 	struct kset *kset;
 	struct kobject *kobject;
 	const struct efivar_operations *ops;
Signature · 1 line
2.6.3
#27 lore raw

PATCH v3 2/3 efi: don't use spinlocks for efi vars

In reply to a message not in the archive

From: Sylvain Chouleur <[email protected]>

All efivars operations are protected by a spinlock which prevents interruptions and preemption. This is too restricted, we just need a lock preventing concurency.
The idea is to use a semaphore of count 1 and to have two ways of locking, depending on the context:
- In interrupt context, we call down_trylock(), if it fails we return an error
- In normal context, we call down_interruptible()

We don't use a mutex here because the mutex_trylock() function must not be called from interrupt context, whereas the down_trylock() can.

---
 drivers/firmware/efi/efi-pstore.c |  36 +++++++---
 drivers/firmware/efi/efivars.c    |  22 ++++--
 drivers/firmware/efi/vars.c       | 145 +++++++++++++++++++++++---------------
 fs/efivarfs/inode.c               |   5 +-
 fs/efivarfs/super.c               |   9 ++-
 include/linux/efi.h               |   6 +-
 6 files changed, 149 insertions(+), 74 deletions(-)
drivers/firmware/efi/efi-pstore.c +27 -9
index eac76a79a880..7bfaffb0e672 100644
@@ -121,16 +121,19 @@ static void efi_pstore_scan_sysfs_enter(struct efivar_entry *pos,
  * @entry: deleting entry
  * @turn_off_scanning: Check if a scanning flag should be turned off
  */
-static inline void __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
+static inline int __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
 						bool turn_off_scanning)
 {
 	if (entry->deleting) {
 		list_del(&entry->list);
 		efivar_entry_iter_end();
 		efivar_unregister(entry);
-		efivar_entry_iter_begin();
+		if (efivar_entry_iter_begin())
+			return -EINTR;
 	} else if (turn_off_scanning)
 		entry->scanning = false;
+
+	return 0;
 }
 
 /**
@@ -140,13 +143,18 @@ static inline void __efi_pstore_scan_sysfs_exit(struct efivar_entry *entry,
  * @head: list head
  * @stop: a flag checking if scanning will stop
  */
-static void efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
+static int efi_pstore_scan_sysfs_exit(struct efivar_entry *pos,
 				       struct efivar_entry *next,
 				       struct list_head *head, bool stop)
 {
-	__efi_pstore_scan_sysfs_exit(pos, true);
+	int ret = __efi_pstore_scan_sysfs_exit(pos, true);
+
+	if (ret)
+		return ret;
+
 	if (stop)
-		__efi_pstore_scan_sysfs_exit(next, &next->list != head);
+		ret = __efi_pstore_scan_sysfs_exit(next, &next->list != head);
+	return ret;
 }
 
 /**
@@ -168,13 +176,17 @@ static int efi_pstore_sysfs_entry_iter(void *data, struct efivar_entry **pos)
 	struct efivar_entry *entry, *n;
 	struct list_head *head = &efivar_sysfs_list;
 	int size = 0;
+	int ret;
 
 	if (!*pos) {
 		list_for_each_entry_safe(entry, n, head, list) {
 			efi_pstore_scan_sysfs_enter(entry, n, head);
 
 			size = efi_pstore_read_func(entry, data);
-			efi_pstore_scan_sysfs_exit(entry, n, head, size < 0);
+			ret = efi_pstore_scan_sysfs_exit(entry, n, head,
+							 size < 0);
+			if (ret)
+				return ret;
 			if (size)
 				break;
 		}
@@ -186,7 +198,9 @@ static int efi_pstore_sysfs_entry_iter(void *data, struct efivar_entry **pos)
 		efi_pstore_scan_sysfs_enter((*pos), n, head);
 
 		size = efi_pstore_read_func((*pos), data);
-		efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
+		ret = efi_pstore_scan_sysfs_exit((*pos), n, head, size < 0);
+		if (ret)
+			return ret;
 		if (size)
 			break;
 	}
@@ -226,7 +240,10 @@ static ssize_t efi_pstore_read(u64 *id, enum pstore_type_id *type,
 	if (!*data.buf)
 		return -ENOMEM;
 
-	efivar_entry_iter_begin();
+	if (efivar_entry_iter_begin()) {
+		kfree(*data.buf);
+		return -EINTR;
+	}
 	size = efi_pstore_sysfs_entry_iter(&data,
 					   (struct efivar_entry **)&psi->data);
 	efivar_entry_iter_end();
@@ -341,7 +358,8 @@ static int efi_pstore_erase(enum pstore_type_id type, u64 id, int count,
 	edata.time = time;
 	edata.name = efi_name;
 
-	efivar_entry_iter_begin();
+	if (efivar_entry_iter_begin())
+		return -EINTR;
 	found = __efivar_entry_iter(efi_pstore_erase_func, &efivar_sysfs_list, &edata, &entry);
 
 	if (found && !entry->scanning) {
drivers/firmware/efi/efivars.c +18 -4
index c5b0d2bc1111..6e5871f19c91 100644
@@ -509,7 +509,8 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
 		vendor = del_var->VendorGuid;
 	}
 
-	efivar_entry_iter_begin();
+	if (efivar_entry_iter_begin())
+		return -EINTR;
 	entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
 	if (!entry)
 		err = -EINVAL;
@@ -582,7 +583,10 @@ efivar_create_sysfs_entry(struct efivar_entry *new_var)
 		return ret;
 
 	kobject_uevent(&new_var->kobj, KOBJ_ADD);
-	efivar_entry_add(new_var, &efivar_sysfs_list);
+	if (efivar_entry_add(new_var, &efivar_sysfs_list)) {
+		efivar_unregister(new_var);
+		return -EINTR;
+	}
 
 	return 0;
 }
@@ -697,7 +701,10 @@ static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
 
 static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
 {
-	efivar_entry_remove(entry);
+	int err = efivar_entry_remove(entry);
+
+	if (err)
+		return err;
 	efivar_unregister(entry);
 	return 0;
 }
@@ -705,7 +712,14 @@ static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
 static void efivars_sysfs_exit(void)
 {
 	/* Remove all entries and destroy */
-	__efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list, NULL, NULL);
+	int err;
+
+	err = __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list,
+				  NULL, NULL);
+	if (err) {
+		pr_err("efivars: Failed to destroy sysfs entries\n");
+		return;
+	}
 
 	if (efivars_new_var)
 		sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
drivers/firmware/efi/vars.c +90 -55

382 lines not on this page · raw message

fs/efivarfs/inode.c +4 -1
index 3381b9da9ee6..5cea84ef7a44 100644
@@ -132,7 +132,10 @@ static int efivarfs_create(struct inode *dir, struct dentry *dentry,
 
 	inode->i_private = var;
 
-	efivar_entry_add(var, &efivarfs_list);
+	err = efivar_entry_add(var, &efivarfs_list);
+	if (err)
+		goto out;
+
 	d_instantiate(dentry, inode);
 	dget(dentry);
 out:
fs/efivarfs/super.c +7 -2
index 86a2121828c3..6825f1e9718b 100644
@@ -158,7 +158,9 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
 	kfree(name);
 
 	efivar_entry_size(entry, &size);
-	efivar_entry_add(entry, &efivarfs_list);
+	err = efivar_entry_add(entry, &efivarfs_list);
+	if (err)
+		goto fail_inode;
 
 	mutex_lock(&inode->i_mutex);
 	inode->i_private = entry;
@@ -179,7 +181,10 @@ fail:
 
 static int efivarfs_destroy(struct efivar_entry *entry, void *data)
 {
-	efivar_entry_remove(entry);
+	int err = efivar_entry_remove(entry);
+
+	if (err)
+		return err;
 	kfree(entry);
 	return 0;
 }
include/linux/efi.h +3 -3
index ca005eac825d..1a2363fb69b6 100644
@@ -1167,8 +1167,8 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 		void *data, bool atomic, bool duplicates,
 		struct list_head *head);
 
-void efivar_entry_add(struct efivar_entry *entry, struct list_head *head);
-void efivar_entry_remove(struct efivar_entry *entry);
+int efivar_entry_add(struct efivar_entry *entry, struct list_head *head);
+int efivar_entry_remove(struct efivar_entry *entry);
 
 int __efivar_entry_delete(struct efivar_entry *entry);
 int efivar_entry_delete(struct efivar_entry *entry);
@@ -1185,7 +1185,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes,
 			  bool block, unsigned long size, void *data);
 
-void efivar_entry_iter_begin(void);
+int efivar_entry_iter_begin(void);
 void efivar_entry_iter_end(void);
 
 int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *),
Signature · 1 line
2.6.3
#30 lore raw

PATCH v3 3/3 efi: implement interruptible runtime services

In reply to Sylvain Chouleur

From: Sylvain Chouleur <[email protected]>

This patch adds an implementation of EFI runtime services wappers which allow interrupts and preemption during execution of BIOS code.

It's needed by Broxton platform which requires the OS to do the write of the non volatile variables. To do that, the OS will receive an interrupt coming from the firmware to notify that it must write the data.

BIOS code must be executed using a specific PGD. This is currently done by efi_call() which force value of CR3 before calling BIOS and restore previous value after.

We use a dedicated kthread which will execute all interruptible runtime services. This efi_kthread is special because it has it's own mm_struct whereas kthread should not have one. This mm_struct contains the EFI PGD so the scheduler will load it before running any BIOS code.

Obviously, an interruptible runtime service must never be called from an interrupt context. EFI pstore is the only use case here, that's why we require it to be disabled when activating interruptible runtime services.

---
 arch/x86/Kconfig                          |  17 +++
 arch/x86/include/asm/efi.h                |   1 +
 arch/x86/platform/efi/Makefile            |   1 +
 arch/x86/platform/efi/efi_32.c            |   5 +
 arch/x86/platform/efi/efi_64.c            |   5 +
 arch/x86/platform/efi/efi_interruptible.c | 189 ++++++++++++++++++++++++++++++
 6 files changed, 218 insertions(+)
 create mode 100644 arch/x86/platform/efi/efi_interruptible.c
arch/x86/Kconfig +17 -0
index db3622f22b61..37301516f4e6 100644
@@ -1720,6 +1720,23 @@ config EFI_MIXED
 
 	   If unsure, say N.
 
+if X86_64
+config EFI_INTERRUPTIBLE
+	bool "Interruptible Efi Runtime Services"
+	depends on EFI && !EFI_VARS_PSTORE
+	default n
+	---help---
+	  Only use this if you really know what you are doing, you
+	  possibly risk to break your BIOS.
+
+	  This is an interruptible implementation of efi runtime
+	  services wrappers. If you say Y, BIOS code could be
+	  interrupted and preempted as a standard process. Enable this
+	  only if you are sure that your BIOS will support
+	  interruptible efi calls
+	  In doubt, say "N".
+endif
+
 config SECCOMP
 	def_bool y
 	prompt "Enable seccomp to safely compute untrusted bytecode"
arch/x86/include/asm/efi.h +1 -0
index 8fd9e637629a..5b94d9dd9409 100644
@@ -137,6 +137,7 @@ extern void __init efi_map_region(efi_memory_desc_t *md);
 extern void __init efi_map_region_fixed(efi_memory_desc_t *md);
 extern void efi_sync_low_kernel_mappings(void);
 extern int __init efi_alloc_page_tables(void);
+extern pgd_t *efi_get_pgd(void);
 extern int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages);
 extern void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages);
 extern void __init old_map_region(efi_memory_desc_t *md);
arch/x86/platform/efi/Makefile +1 -0
index 2846aaab5103..ff57d3840842 100644
@@ -2,3 +2,4 @@ obj-$(CONFIG_EFI) 		+= quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o
 obj-$(CONFIG_ACPI_BGRT) += efi-bgrt.o
 obj-$(CONFIG_EARLY_PRINTK_EFI)	+= early_printk.o
 obj-$(CONFIG_EFI_MIXED)		+= efi_thunk_$(BITS).o
+obj-$(CONFIG_EFI_INTERRUPTIBLE)	+= efi_interruptible.o
arch/x86/platform/efi/efi_32.c +5 -0
index 58d669bc8250..f2403e16a8bb 100644
@@ -61,6 +61,11 @@ void __init efi_map_region(efi_memory_desc_t *md)
 void __init efi_map_region_fixed(efi_memory_desc_t *md) {}
 void __init parse_efi_setup(u64 phys_addr, u32 data_len) {}
 
+pgd_t *efi_get_pgd(void)
+{
+	return initial_page_table;
+}
+
 pgd_t * __init efi_call_phys_prolog(void)
 {
 	struct desc_ptr gdt_descr;
arch/x86/platform/efi/efi_64.c +5 -0
index b492521503fe..7886c7527cdf 100644
@@ -212,6 +212,11 @@ void efi_sync_low_kernel_mappings(void)
 	memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
 }
 
+pgd_t *efi_get_pgd(void)
+{
+	return __va(efi_scratch.efi_pgt);
+}
+
 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
 {
 	unsigned long pfn, text;
arch/x86/platform/efi/efi_interruptible.c new file +189 -0

189 lines not on this page · raw message

Signature · 1 line
2.6.3
#7 lore raw

Re: [PATCH 2/2] efi: implement interruptible runtime services

In reply to Sylvain Chouleur
Quoted 5 lines · Sylvain Chouleur

I understand, like I said above I'll modify efi_interruptible handlers to call legacy ones in case of panic context.
I would like to avoid removing the panic part of this patch and take time to clean it before merging the whole.

OK, well at least split out the panic diddling into a separate patch so that we can discuss the merits of it separately to the other, less contentious changes.

Quoted 8 lines
Quoted 8 lines

Kconfig is a last resort because it's a build-time decision and greatly limits the flexibility of the kernel. It becomes no longer possible to run a single kernel image with various CONFIG_* enabled on x86 hardware - you now need a special EFI_INTERRUPTIBLE build.

Which apart from being a major headache for distributions in general is generally frowned upon for the x86 architecture.

Quoted 7 lines
Quoted 2 lines

If there's any way at all of making this a runtime decision that would be much better.

I think the best would be to bind this driver with the one which receives the interrupts from CSE to write the variables. Then we would have a consistency on the feature.
Does this seems ok for you?

I think that'd be an improvement, yeah.

#24 lore raw

Re: [PATCH v3 1/3] efi: use a file local lock for efivars

In reply to a message not in the archive
Quoted 7 lines · Sylvain Chouleur

From: Sylvain Chouleur <[email protected]>

This patch remove the efivars lock to use a single lock for the whole vars.c file. The goal of this lock is to protect concurrent calls to efi variable services, registering and unregistering. This allows to register new efivars operations without having in-progress call.

Quoted 5 lines
---
 drivers/firmware/efi/vars.c | 85 +++++++++++++++++++++++++--------------------
 include/linux/efi.h         |  6 ----
 2 files changed, 48 insertions(+), 43 deletions(-)

Sorry for the delay in reviewing this. I wasn't on Cc so I only found it when searching through the linux-efi archive.

This change looks fine to me and a nice clean up. Ard any comments?

#26 lore raw

Re: [PATCH v3 1/3] efi: use a file local lock for efivars

In reply to a message not in the archive
Quoted 14 lines · Matt Fleming
Quoted 7 lines · Sylvain Chouleur

From: Sylvain Chouleur <[email protected]>

This patch remove the efivars lock to use a single lock for the whole vars.c file. The goal of this lock is to protect concurrent calls to efi variable services, registering and unregistering. This allows to register new efivars operations without having in-progress call.

Quoted 5 lines
---
 drivers/firmware/efi/vars.c | 85 +++++++++++++++++++++++++--------------------
 include/linux/efi.h         |  6 ----
 2 files changed, 48 insertions(+), 43 deletions(-)
Quoted 4 lines

Sorry for the delay in reviewing this. I wasn't on Cc so I only found it when searching through the linux-efi archive.

This change looks fine to me and a nice clean up. Ard any comments?

No looks good to me

#29 lore raw

Re: [PATCH v3 2/3] efi: don't use spinlocks for efi vars

In reply to a message not in the archive
Quoted 15 lines · Sylvain Chouleur

From: Sylvain Chouleur <[email protected]>

All efivars operations are protected by a spinlock which prevents interruptions and preemption. This is too restricted, we just need a lock preventing concurency.
The idea is to use a semaphore of count 1 and to have two ways of locking, depending on the context:
- In interrupt context, we call down_trylock(), if it fails we return an error
- In normal context, we call down_interruptible()

We don't use a mutex here because the mutex_trylock() function must not be called from interrupt context, whereas the down_trylock() can.

Quoted 8 lines
---
 drivers/firmware/efi/efi-pstore.c |  36 +++++++---
 drivers/firmware/efi/efivars.c    |  22 ++++--
 drivers/firmware/efi/vars.c       | 145 +++++++++++++++++++++++---------------
 fs/efivarfs/inode.c               |   5 +-
 fs/efivarfs/super.c               |   9 ++-
 include/linux/efi.h               |   6 +-
 6 files changed, 149 insertions(+), 74 deletions(-)

Looks fine to me.

#31 lore raw

Re: [PATCH v3 3/3] efi: implement interruptible runtime services

In reply to Sylvain Chouleur
Quoted 24 lines · Sylvain Chouleur

From: Sylvain Chouleur <[email protected]>

This patch adds an implementation of EFI runtime services wappers which allow interrupts and preemption during execution of BIOS code.

It's needed by Broxton platform which requires the OS to do the write of the non volatile variables. To do that, the OS will receive an interrupt coming from the firmware to notify that it must write the data.

BIOS code must be executed using a specific PGD. This is currently done by efi_call() which force value of CR3 before calling BIOS and restore previous value after.

We use a dedicated kthread which will execute all interruptible runtime services. This efi_kthread is special because it has it's own mm_struct whereas kthread should not have one. This mm_struct contains the EFI PGD so the scheduler will load it before running any BIOS code.

Obviously, an interruptible runtime service must never be called from an interrupt context. EFI pstore is the only use case here, that's why we require it to be disabled when activating interruptible runtime services.

Quoted 9 lines
---
 arch/x86/Kconfig                          |  17 +++
 arch/x86/include/asm/efi.h                |   1 +
 arch/x86/platform/efi/Makefile            |   1 +
 arch/x86/platform/efi/efi_32.c            |   5 +
 arch/x86/platform/efi/efi_64.c            |   5 +
 arch/x86/platform/efi/efi_interruptible.c | 189 ++++++++++++++++++++++++++++++
 6 files changed, 218 insertions(+)
 create mode 100644 arch/x86/platform/efi/efi_interruptible.c

Last time Peter and I talked about this patch (sometime last year), he said he had a different approach in mind.

Peter, do you have any comments on this patch?

Quoted 55 lines
arch/x86/Kconfig +17 -0
index db3622f22b61..37301516f4e6 100644
@@ -1720,6 +1720,23 @@ config EFI_MIXED
 
 	   If unsure, say N.
 
+if X86_64
+config EFI_INTERRUPTIBLE
+	bool "Interruptible Efi Runtime Services"
+	depends on EFI && !EFI_VARS_PSTORE
+	default n
+	---help---
+	  Only use this if you really know what you are doing, you
+	  possibly risk to break your BIOS.
+
+	  This is an interruptible implementation of efi runtime
+	  services wrappers. If you say Y, BIOS code could be
+	  interrupted and preempted as a standard process. Enable this
+	  only if you are sure that your BIOS will support
+	  interruptible efi calls
+	  In doubt, say "N".
+endif
+
 config SECCOMP
 	def_bool y
 	prompt "Enable seccomp to safely compute untrusted bytecode"
arch/x86/include/asm/efi.h +1 -0
index 8fd9e637629a..5b94d9dd9409 100644
@@ -137,6 +137,7 @@ extern void __init efi_map_region(efi_memory_desc_t *md);
 extern void __init efi_map_region_fixed(efi_memory_desc_t *md);
 extern void efi_sync_low_kernel_mappings(void);
 extern int __init efi_alloc_page_tables(void);
+extern pgd_t *efi_get_pgd(void);
 extern int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages);
 extern void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages);
 extern void __init old_map_region(efi_memory_desc_t *md);
arch/x86/platform/efi/Makefile +1 -0
index 2846aaab5103..ff57d3840842 100644
@@ -2,3 +2,4 @@ obj-$(CONFIG_EFI) 		+= quirks.o efi.o efi_$(BITS).o efi_stub_$(BITS).o
 obj-$(CONFIG_ACPI_BGRT) += efi-bgrt.o
 obj-$(CONFIG_EARLY_PRINTK_EFI)	+= early_printk.o
 obj-$(CONFIG_EFI_MIXED)		+= efi_thunk_$(BITS).o
+obj-$(CONFIG_EFI_INTERRUPTIBLE)	+= efi_interruptible.o
arch/x86/platform/efi/efi_32.c +0 -0
index 58d669bc8250..f2403e16a8bb 100644
@@ -61,6 +61,11 @@ void __init efi_map_region(efi_memory_desc_t *md)
 void __init efi_map_region_fixed(efi_memory_desc_t *md) {}

arch/x86/platform/efi/efi_32.c void __init efi_map_region(efi_memory_desc_t *md) · line 71

Quoted 10 lines
arch/x86/platform/efi/efi_32.c +5 -0
 void __init parse_efi_setup(u64 phys_addr, u32 data_len) {}
 
+pgd_t *efi_get_pgd(void)
+{
+	return initial_page_table;
+}
+
 pgd_t * __init efi_call_phys_prolog(void)
 {
 	struct desc_ptr gdt_descr;

Shouldn't be necessary. You can't build this for 32-bit because of the Kconfig magic.

arch/x86/platform/efi/efi_64.c void efi_sync_low_kernel_mappings(void) · line 219

Quoted 13 lines
arch/x86/platform/efi/efi_64.c +5 -0
index b492521503fe..7886c7527cdf 100644
@@ -212,6 +212,11 @@ void efi_sync_low_kernel_mappings(void)
 	memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
 }
 
+pgd_t *efi_get_pgd(void)
+{
+	return __va(efi_scratch.efi_pgt);
+}
+

You could make this easier and just return 'efi_pgd', since we now have entirely separate EFI page tables.

arch/x86/platform/efi/efi_64.c void efi_sync_low_kernel_mappings(void)

Quoted 9 lines
arch/x86/platform/efi/efi_64.c +9 -0
+static struct efivars interruptible_efivars;
+
+static int efi_interruptible_panic_notifier_call(
+	struct notifier_block *notifier,
+	unsigned long what, void *data)
+{
+	in_panic = true;
+	return NOTIFY_DONE;
+}

Please make the panic notifier changes a separate patch.

Quoted 14 lines
arch/x86/platform/efi/efi_64.c +14 -0
+static struct notifier_block panic_nb = {
+	.notifier_call = efi_interruptible_panic_notifier_call,
+	.priority = 100,
+};
+
+static struct task_struct *efi_kworker_task;
+static struct efivar_operations interruptible_ops;
+static __init int efi_interruptible_init(void)
+{
+	int ret;
+
+	efi_kworker_task = kthread_create(kthread_worker_fn, &efi_kworker,
+					  "efi_kthread");
+	if (IS_ERR(efi_kworker_task)) {

arch/x86/platform/efi/efi_64.c

Quoted 10 lines
arch/x86/platform/efi/efi_64.c +10 -0
+		pr_err("efi_interruptible: Failed to create kthread\n");
+		ret = PTR_ERR(efi_kworker_task);
+		efi_kworker_task = NULL;
+		return ret;
+	}
+
+	efi_kworker_task->mm = mm_alloc();
+	efi_kworker_task->active_mm = efi_kworker_task->mm;
+	efi_kworker_task->mm->pgd = efi_get_pgd();
+	wake_up_process(efi_kworker_task);

Did you have any luck binding this driver to the other Broxton-specific drivers?

Additionally, you're going to want to make sure that efi_enabled(EFI_OLD_MEMMAP) is false in efi_interruptible_init(), otherwise you can't use the new EFI page table scheme, e.g. if someone boots with efi=old_map.

#32 lore raw

Re: [PATCH v3 3/3] efi: implement interruptible runtime services

2016-02-11 15:19 GMT+01:00 Matt Fleming <matt-mF/unelCI9GS6iBeEJttW/[email protected]>:

Quoted 7 lines

Did you have any luck binding this driver to the other Broxton-specific drivers?

Additionally, you're going to want to make sure that efi_enabled(EFI_OLD_MEMMAP) is false in efi_interruptible_init(), otherwise you can't use the new EFI page table scheme, e.g. if someone boots with efi=old_map.

I didn't have very much time to work on this but yes, we should be able to make efi_interruptible a platform driver which would be probed by SPD driver or something like that.
I'll upload it as soon as I've something

Signature · 1 line
Sylvain