efi_pstore: Introducing workqueue updating sysfs
authorSeiji Aguchi <seiji.aguchi@hds.com>
Tue, 12 Feb 2013 21:04:41 +0000 (13:04 -0800)
committerBen Hutchings <ben@decadent.org.uk>
Wed, 27 Mar 2013 02:41:20 +0000 (02:41 +0000)
commit a93bc0c6e07ed9bac44700280e65e2945d864fd4 upstream.

[Problem]
efi_pstore creates sysfs entries, which enable users to access to NVRAM,
in a write callback. If a kernel panic happens in an interrupt context,
it may fail because it could sleep due to dynamic memory allocations during
creating sysfs entries.

[Patch Description]
This patch removes sysfs operations from a write callback by introducing
a workqueue updating sysfs entries which is scheduled after the write
callback is called.

Also, the workqueue is kicked in a just oops case.
A system will go down in other cases such as panic, clean shutdown and emergency
restart. And we don't need to create sysfs entries because there is no chance for
users to access to them.

efi_pstore will be robust against a kernel panic in an interrupt context with this patch.

Signed-off-by: Seiji Aguchi <seiji.aguchi@hds.com>
Acked-by: Matt Fleming <matt.fleming@intel.com>
Signed-off-by: Tony Luck <tony.luck@intel.com>
[bwh: Backported to 3.2:
 - Adjust contest
 - Don't check reason in efi_pstore_write(), as it is not given as a
   parameter
 - Move up declaration of __efivars]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
drivers/firmware/efivars.c
include/linux/efi.h

index 75f7f17..480d2c5 100644 (file)
@@ -127,6 +127,8 @@ struct efivar_attribute {
        ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
 };
 
+static struct efivars __efivars;
+
 #define PSTORE_EFI_ATTRIBUTES \
        (EFI_VARIABLE_NON_VOLATILE | \
         EFI_VARIABLE_BOOTSERVICE_ACCESS | \
@@ -151,6 +153,13 @@ efivar_create_sysfs_entry(struct efivars *efivars,
                          efi_char16_t *variable_name,
                          efi_guid_t *vendor_guid);
 
+/*
+ * Prototype for workqueue functions updating sysfs entry
+ */
+
+static void efivar_update_sysfs_entries(struct work_struct *);
+static DECLARE_WORK(efivar_work, efivar_update_sysfs_entries);
+
 /* Return the number of unicode characters in data */
 static unsigned long
 utf16_strnlen(efi_char16_t *s, size_t maxlength)
@@ -833,11 +842,7 @@ static int efi_pstore_write(enum pstore_type_id type, u64 *id,
        if (found)
                efivar_unregister(found);
 
-       if (size)
-               ret = efivar_create_sysfs_entry(efivars,
-                                         utf16_strsize(efi_name,
-                                                       DUMP_NAME_LEN * 2),
-                                         efi_name, &vendor);
+       schedule_work(&efivar_work);
 
        *id = part;
        return ret;
@@ -1016,6 +1021,75 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
        return count;
 }
 
+static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor)
+{
+       struct efivar_entry *entry, *n;
+       struct efivars *efivars = &__efivars;
+       unsigned long strsize1, strsize2;
+       bool found = false;
+
+       strsize1 = utf16_strsize(variable_name, 1024);
+       list_for_each_entry_safe(entry, n, &efivars->list, list) {
+               strsize2 = utf16_strsize(entry->var.VariableName, 1024);
+               if (strsize1 == strsize2 &&
+                       !memcmp(variable_name, &(entry->var.VariableName),
+                               strsize2) &&
+                       !efi_guidcmp(entry->var.VendorGuid,
+                               *vendor)) {
+                       found = true;
+                       break;
+               }
+       }
+       return found;
+}
+
+static void efivar_update_sysfs_entries(struct work_struct *work)
+{
+       struct efivars *efivars = &__efivars;
+       efi_guid_t vendor;
+       efi_char16_t *variable_name;
+       unsigned long variable_name_size = 1024;
+       efi_status_t status = EFI_NOT_FOUND;
+       bool found;
+
+       /* Add new sysfs entries */
+       while (1) {
+               variable_name = kzalloc(variable_name_size, GFP_KERNEL);
+               if (!variable_name) {
+                       pr_err("efivars: Memory allocation failed.\n");
+                       return;
+               }
+
+               spin_lock_irq(&efivars->lock);
+               found = false;
+               while (1) {
+                       variable_name_size = 1024;
+                       status = efivars->ops->get_next_variable(
+                                                       &variable_name_size,
+                                                       variable_name,
+                                                       &vendor);
+                       if (status != EFI_SUCCESS) {
+                               break;
+                       } else {
+                               if (!variable_is_present(variable_name,
+                                   &vendor)) {
+                                       found = true;
+                                       break;
+                               }
+                       }
+               }
+               spin_unlock_irq(&efivars->lock);
+
+               if (!found) {
+                       kfree(variable_name);
+                       break;
+               } else
+                       efivar_create_sysfs_entry(efivars,
+                                                 variable_name_size,
+                                                 variable_name, &vendor);
+       }
+}
+
 /*
  * Let's not leave out systab information that snuck into
  * the efivars driver
@@ -1272,7 +1346,6 @@ out:
 }
 EXPORT_SYMBOL_GPL(register_efivars);
 
-static struct efivars __efivars;
 static struct efivar_operations ops;
 
 /*
@@ -1330,6 +1403,8 @@ err_put:
 static void __exit
 efivars_exit(void)
 {
+       cancel_work_sync(&efivar_work);
+
        if (efi_enabled(EFI_RUNTIME_SERVICES)) {
                unregister_efivars(&__efivars);
                kobject_put(efi_kobj);
index ce95a4b..8469f3f 100644 (file)
@@ -484,7 +484,8 @@ struct efivars {
         * 1) ->list - adds, removals, reads, writes
         * 2) ops.[gs]et_variable() calls.
         * It must not be held when creating sysfs entries or calling kmalloc.
-        * ops.get_next_variable() is only called from register_efivars(),
+        * ops.get_next_variable() is only called from register_efivars()
+        * or efivar_update_sysfs_entries(),
         * which is protected by the BKL, so that path is safe.
         */
        spinlock_t lock;