Merge tag 'please-pull-pstore' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl...
authorLinus Torvalds <torvalds@linux-foundation.org>
Thu, 21 Feb 2013 17:38:18 +0000 (09:38 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 21 Feb 2013 17:38:18 +0000 (09:38 -0800)
Pull pstore patches from Tony Luck:
 "A few fixes to reduce places where pstore might hang a system in the
  crash path.  Plus a new mountpoint (/sys/fs/pstore ...  makes more
  sense then /dev/pstore)."

Fix up trivial conflict in drivers/firmware/efivars.c

* tag 'please-pull-pstore' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux:
  pstore: Create a convenient mount point for pstore
  efi_pstore: Introducing workqueue updating sysfs
  efivars: Disable external interrupt while holding efivars->lock
  efi_pstore: Avoid deadlock in non-blocking paths
  pstore: Avoid deadlock in panic and emergency-restart path

1  2 
drivers/firmware/efivars.c
include/linux/efi.h

@@@ -158,6 -158,13 +158,13 @@@ efivar_create_sysfs_entry(struct efivar
                          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)
@@@ -405,10 -412,11 +412,11 @@@ static efi_status_
  get_var_data(struct efivars *efivars, struct efi_variable *var)
  {
        efi_status_t status;
+       unsigned long flags;
  
-       spin_lock(&efivars->lock);
+       spin_lock_irqsave(&efivars->lock, flags);
        status = get_var_data_locked(efivars, var);
-       spin_unlock(&efivars->lock);
+       spin_unlock_irqrestore(&efivars->lock, flags);
  
        if (status != EFI_SUCCESS) {
                printk(KERN_WARNING "efivars: get_variable() failed 0x%lx!\n",
@@@ -537,14 -545,14 +545,14 @@@ efivar_store_raw(struct efivar_entry *e
                return -EINVAL;
        }
  
-       spin_lock(&efivars->lock);
+       spin_lock_irq(&efivars->lock);
        status = efivars->ops->set_variable(new_var->VariableName,
                                            &new_var->VendorGuid,
                                            new_var->Attributes,
                                            new_var->DataSize,
                                            new_var->Data);
  
-       spin_unlock(&efivars->lock);
+       spin_unlock_irq(&efivars->lock);
  
        if (status != EFI_SUCCESS) {
                printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
@@@ -674,7 -682,7 +682,7 @@@ static int efi_status_to_err(efi_status
                err = -EACCES;
                break;
        case EFI_NOT_FOUND:
 -              err = -ENOENT;
 +              err = -EIO;
                break;
        default:
                err = -EINVAL;
@@@ -713,7 -721,7 +721,7 @@@ static ssize_t efivarfs_file_write(stru
         * amounts of memory. Pick a default size of 64K if
         * QueryVariableInfo() isn't supported by the firmware.
         */
-       spin_lock(&efivars->lock);
+       spin_lock_irq(&efivars->lock);
  
        if (!efivars->ops->query_variable_info)
                status = EFI_UNSUPPORTED;
                                                   &remaining_size, &max_size);
        }
  
-       spin_unlock(&efivars->lock);
+       spin_unlock_irq(&efivars->lock);
  
        if (status != EFI_SUCCESS) {
                if (status != EFI_UNSUPPORTED)
         * set_variable call, and removal of the variable from the efivars
         * list (in the case of an authenticated delete).
         */
-       spin_lock(&efivars->lock);
+       spin_lock_irq(&efivars->lock);
  
        status = efivars->ops->set_variable(var->var.VariableName,
                                            &var->var.VendorGuid,
                                            data);
  
        if (status != EFI_SUCCESS) {
-               spin_unlock(&efivars->lock);
+               spin_unlock_irq(&efivars->lock);
                kfree(data);
  
                return efi_status_to_err(status);
                                            NULL);
  
        if (status == EFI_BUFFER_TOO_SMALL) {
-               spin_unlock(&efivars->lock);
+               spin_unlock_irq(&efivars->lock);
                mutex_lock(&inode->i_mutex);
                i_size_write(inode, newdatasize + sizeof(attributes));
                mutex_unlock(&inode->i_mutex);
  
        } else if (status == EFI_NOT_FOUND) {
                list_del(&var->list);
-               spin_unlock(&efivars->lock);
+               spin_unlock_irq(&efivars->lock);
                efivar_unregister(var);
                drop_nlink(inode);
 +              d_delete(file->f_dentry);
                dput(file->f_dentry);
  
        } else {
-               spin_unlock(&efivars->lock);
+               spin_unlock_irq(&efivars->lock);
                pr_warn("efivarfs: inconsistent EFI variable implementation? "
                                "status = %lx\n", status);
        }
@@@ -819,11 -826,11 +827,11 @@@ static ssize_t efivarfs_file_read(struc
        void *data;
        ssize_t size = 0;
  
-       spin_lock(&efivars->lock);
+       spin_lock_irq(&efivars->lock);
        status = efivars->ops->get_variable(var->var.VariableName,
                                            &var->var.VendorGuid,
                                            &attributes, &datasize, NULL);
-       spin_unlock(&efivars->lock);
+       spin_unlock_irq(&efivars->lock);
  
        if (status != EFI_BUFFER_TOO_SMALL)
                return efi_status_to_err(status);
        if (!data)
                return -ENOMEM;
  
-       spin_lock(&efivars->lock);
+       spin_lock_irq(&efivars->lock);
        status = efivars->ops->get_variable(var->var.VariableName,
                                            &var->var.VendorGuid,
                                            &attributes, &datasize,
                                            (data + sizeof(attributes)));
-       spin_unlock(&efivars->lock);
+       spin_unlock_irq(&efivars->lock);
  
        if (status != EFI_SUCCESS) {
                size = efi_status_to_err(status);
@@@ -966,9 -973,9 +974,9 @@@ static int efivarfs_create(struct inod
                goto out;
  
        kobject_uevent(&var->kobj, KOBJ_ADD);
-       spin_lock(&efivars->lock);
+       spin_lock_irq(&efivars->lock);
        list_add(&var->list, &efivars->list);
-       spin_unlock(&efivars->lock);
+       spin_unlock_irq(&efivars->lock);
        d_instantiate(dentry, inode);
        dget(dentry);
  out:
@@@ -985,7 -992,7 +993,7 @@@ static int efivarfs_unlink(struct inod
        struct efivars *efivars = var->efivars;
        efi_status_t status;
  
-       spin_lock(&efivars->lock);
+       spin_lock_irq(&efivars->lock);
  
        status = efivars->ops->set_variable(var->var.VariableName,
                                            &var->var.VendorGuid,
  
        if (status == EFI_SUCCESS || status == EFI_NOT_FOUND) {
                list_del(&var->list);
-               spin_unlock(&efivars->lock);
+               spin_unlock_irq(&efivars->lock);
                efivar_unregister(var);
 -              drop_nlink(dir);
 +              drop_nlink(dentry->d_inode);
                dput(dentry);
                return 0;
        }
  
-       spin_unlock(&efivars->lock);
+       spin_unlock_irq(&efivars->lock);
        return -EINVAL;
  };
  
@@@ -1066,13 -1073,13 +1074,13 @@@ static int efivarfs_fill_super(struct s
                /* copied by the above to local storage in the dentry. */
                kfree(name);
  
-               spin_lock(&efivars->lock);
+               spin_lock_irq(&efivars->lock);
                efivars->ops->get_variable(entry->var.VariableName,
                                           &entry->var.VendorGuid,
                                           &entry->var.Attributes,
                                           &size,
                                           NULL);
-               spin_unlock(&efivars->lock);
+               spin_unlock_irq(&efivars->lock);
  
                mutex_lock(&inode->i_mutex);
                inode->i_private = entry;
@@@ -1123,7 -1130,7 +1131,7 @@@ static int efi_pstore_open(struct pstor
  {
        struct efivars *efivars = psi->data;
  
-       spin_lock(&efivars->lock);
+       spin_lock_irq(&efivars->lock);
        efivars->walk_entry = list_first_entry(&efivars->list,
                                               struct efivar_entry, list);
        return 0;
@@@ -1133,7 -1140,7 +1141,7 @@@ static int efi_pstore_close(struct psto
  {
        struct efivars *efivars = psi->data;
  
-       spin_unlock(&efivars->lock);
+       spin_unlock_irq(&efivars->lock);
        return 0;
  }
  
@@@ -1209,8 -1216,18 +1217,18 @@@ static int efi_pstore_write(enum pstore
        int i, ret = 0;
        u64 storage_space, remaining_space, max_variable_size;
        efi_status_t status = EFI_NOT_FOUND;
-       spin_lock(&efivars->lock);
+       unsigned long flags;
+       if (pstore_cannot_block_path(reason)) {
+               /*
+                * If the lock is taken by another cpu in non-blocking path,
+                * this driver returns without entering firmware to avoid
+                * hanging up.
+                */
+               if (!spin_trylock_irqsave(&efivars->lock, flags))
+                       return -EBUSY;
+       } else
+               spin_lock_irqsave(&efivars->lock, flags);
  
        /*
         * Check if there is a space enough to log.
                                                   &remaining_space,
                                                   &max_variable_size);
        if (status || remaining_space < size + DUMP_NAME_LEN * 2) {
-               spin_unlock(&efivars->lock);
+               spin_unlock_irqrestore(&efivars->lock, flags);
                *id = part;
                return -ENOSPC;
        }
        efivars->ops->set_variable(efi_name, &vendor, PSTORE_EFI_ATTRIBUTES,
                                   size, psi->buf);
  
-       spin_unlock(&efivars->lock);
+       spin_unlock_irqrestore(&efivars->lock, flags);
  
-       if (size)
-               ret = efivar_create_sysfs_entry(efivars,
-                                         utf16_strsize(efi_name,
-                                                       DUMP_NAME_LEN * 2),
-                                         efi_name, &vendor);
+       if (reason == KMSG_DUMP_OOPS)
+               schedule_work(&efivar_work);
  
        *id = part;
        return ret;
@@@ -1263,7 -1277,7 +1278,7 @@@ static int efi_pstore_erase(enum pstore
        sprintf(name, "dump-type%u-%u-%d-%lu", type, (unsigned int)id, count,
                time.tv_sec);
  
-       spin_lock(&efivars->lock);
+       spin_lock_irq(&efivars->lock);
  
        for (i = 0; i < DUMP_NAME_LEN; i++)
                efi_name[i] = name[i];
        if (found)
                list_del(&found->list);
  
-       spin_unlock(&efivars->lock);
+       spin_unlock_irq(&efivars->lock);
  
        if (found)
                efivar_unregister(found);
@@@ -1377,7 -1391,7 +1392,7 @@@ static ssize_t efivar_create(struct fil
                return -EINVAL;
        }
  
-       spin_lock(&efivars->lock);
+       spin_lock_irq(&efivars->lock);
  
        /*
         * Does this variable already exist?
                }
        }
        if (found) {
-               spin_unlock(&efivars->lock);
+               spin_unlock_irq(&efivars->lock);
                return -EINVAL;
        }
  
        if (status != EFI_SUCCESS) {
                printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
                        status);
-               spin_unlock(&efivars->lock);
+               spin_unlock_irq(&efivars->lock);
                return -EIO;
        }
-       spin_unlock(&efivars->lock);
+       spin_unlock_irq(&efivars->lock);
  
        /* Create the entry in sysfs.  Locking is not required here */
        status = efivar_create_sysfs_entry(efivars,
@@@ -1440,7 -1454,7 +1455,7 @@@ static ssize_t efivar_delete(struct fil
        if (!capable(CAP_SYS_ADMIN))
                return -EACCES;
  
-       spin_lock(&efivars->lock);
+       spin_lock_irq(&efivars->lock);
  
        /*
         * Does this variable already exist?
                }
        }
        if (!found) {
-               spin_unlock(&efivars->lock);
+               spin_unlock_irq(&efivars->lock);
                return -EINVAL;
        }
        /* force the Attributes/DataSize to 0 to ensure deletion */
        if (status != EFI_SUCCESS) {
                printk(KERN_WARNING "efivars: set_variable() failed: status=%lx\n",
                        status);
-               spin_unlock(&efivars->lock);
+               spin_unlock_irq(&efivars->lock);
                return -EIO;
        }
        list_del(&search_efivar->list);
        /* We need to release this lock before unregistering. */
-       spin_unlock(&efivars->lock);
+       spin_unlock_irq(&efivars->lock);
        efivar_unregister(search_efivar);
  
        /* It's dead Jim.... */
        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
@@@ -1594,9 -1677,9 +1678,9 @@@ efivar_create_sysfs_entry(struct efivar
        kfree(short_name);
        short_name = NULL;
  
-       spin_lock(&efivars->lock);
+       spin_lock_irq(&efivars->lock);
        list_add(&new_efivar->list, &efivars->list);
-       spin_unlock(&efivars->lock);
+       spin_unlock_irq(&efivars->lock);
  
        return 0;
  }
@@@ -1665,9 -1748,9 +1749,9 @@@ void unregister_efivars(struct efivars 
        struct efivar_entry *entry, *n;
  
        list_for_each_entry_safe(entry, n, &efivars->list, list) {
-               spin_lock(&efivars->lock);
+               spin_lock_irq(&efivars->lock);
                list_del(&entry->list);
-               spin_unlock(&efivars->lock);
+               spin_unlock_irq(&efivars->lock);
                efivar_unregister(entry);
        }
        if (efivars->new_var)
@@@ -1783,7 -1866,7 +1867,7 @@@ efivars_init(void
        printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
               EFIVARS_DATE);
  
 -      if (!efi_enabled)
 +      if (!efi_enabled(EFI_RUNTIME_SERVICES))
                return 0;
  
        /* For now we'll register the efi directory at /sys/firmware/efi */
@@@ -1823,7 -1906,9 +1907,9 @@@ err_put
  static void __exit
  efivars_exit(void)
  {
 -      if (efi_enabled) {
+       cancel_work_sync(&efivar_work);
 +      if (efi_enabled(EFI_RUNTIME_SERVICES)) {
                unregister_efivars(&__efivars);
                kobject_put(efi_kobj);
        }
diff --combined include/linux/efi.h
@@@ -618,30 -618,18 +618,30 @@@ extern int __init efi_setup_pcdp_consol
  #endif
  
  /*
 - * We play games with efi_enabled so that the compiler will, if possible, remove
 - * EFI-related code altogether.
 + * We play games with efi_enabled so that the compiler will, if
 + * possible, remove EFI-related code altogether.
   */
 +#define EFI_BOOT              0       /* Were we booted from EFI? */
 +#define EFI_SYSTEM_TABLES     1       /* Can we use EFI system tables? */
 +#define EFI_CONFIG_TABLES     2       /* Can we use EFI config tables? */
 +#define EFI_RUNTIME_SERVICES  3       /* Can we use runtime services? */
 +#define EFI_MEMMAP            4       /* Can we use EFI memory map? */
 +#define EFI_64BIT             5       /* Is the firmware 64-bit? */
 +
  #ifdef CONFIG_EFI
  # ifdef CONFIG_X86
 -   extern int efi_enabled;
 -   extern bool efi_64bit;
 +extern int efi_enabled(int facility);
  # else
 -#  define efi_enabled 1
 +static inline int efi_enabled(int facility)
 +{
 +      return 1;
 +}
  # endif
  #else
 -# define efi_enabled 0
 +static inline int efi_enabled(int facility)
 +{
 +      return 0;
 +}
  #endif
  
  /*
@@@ -740,7 -728,8 +740,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;