convert efivarfs to manage object data correctly
Subsystems (4)
v3 cover convert efivarfs to manage object data correctly
I've added fsdevel because I'm hopping some kind vfs person will check the shift from efivarfs managing its own data to its data being managed as part of the vfs object lifetimes. The following paragraph should describe all you need to know about the unusual features of the filesystem.
efivarfs is a filesystem projecting the current state of the UEFI variable store and allowing updates via write. Because EFI variables contain both contents and a set of attributes, which can't be mapped to filesystem data, the u32 attribute is prepended to the output of the file and, since UEFI variables can't be empty, this makes every file at least 5 characters long. EFI variables can be removed either by doing an unlink (easy) or by doing a conventional write update that reduces the content to zero size, which means any write update can potentially remove the file.
Currently efivarfs has two bugs: it leaks memory and if a create is attempted that results in an error in the write, it creates a zero length file remnant that doesn't represent an EFI variable (i.e. the state reflection of the EFI variable store goes out of sync).
The code uses inode->i_private to point to additionaly allocated information but tries to maintain a global list of the shadowed varibles for internal tracking. Forgetting to kfree() entries in this list when they are deleted is the source of the memory leak.
I've tried to make the patches as easily reviewable by non-EFI people as possible, so some possible cleanups (like consolidating or removing the efi lock handling and possibly removing the additional entry allocation entirely in favour of simply converting the dentry name to the variable name and guid) are left for later.
The first patch removes some unused fields in the entry; patches 2-3 eliminate the list search for duplication (some EFI variable stores have buggy iterators) and replaces it with a dcache lookup. Patch 4 move responsibility for freeing the entry data to inode_alloc/inode_free which both fixes the memory leak and also means we no longer need to iterate over the variable list and free its entries in kill_sb. Since the variable list is now unused, patch 5 removes it and its helper functions.
Patch 6 fixes the second bug by introducing a file_operations->release method that checks to see if the inode size is zero when the file is closed and removes it if it is. Since all files must be at least 5 in length we use a zero i_size as an indicator that either the variable was removed on write or that it wasn't correctly created in the first place.
Patch 7 fixes the old self tests which check for zero length files on incorrect variable creation (these are now removed).
Patch 8 adds a new set of self tests for multi threaded variable updates checking for the new behaviour.
v2: folded in feedback from Al Viro: check errors on lookup and delete
zero length file on last close
v3: convert to alloc/free instead of evict and use a boolean in
efivar_entry under the inode lock to indicate removal and add additional selftests
James
---
James Bottomley (8):
efivarfs: remove unused efi_varaible.Attributes and .kobj
efivarfs: add helper to convert from UC16 name and GUID to utf8 name
efivarfs: make variable_is_present use dcache lookup
efivarfs: move variable lifetime management into the inodes
efivarfs: remove unused efivarfs_list
efivarfs: fix error on write to new variable leaving remnants selftests/efivarfs: fix tests for failed write removal selftests/efivarfs: add concurrent update tests
fs/efivarfs/file.c | 59 +++++- fs/efivarfs/inode.c | 41 ++--- fs/efivarfs/internal.h | 26 +-- fs/efivarfs/super.c | 106 ++++++----- fs/efivarfs/vars.c | 179 +++++-------------- tools/testing/selftests/efivarfs/efivarfs.sh | 125 ++++++++++++- 6 files changed, 300 insertions(+), 236 deletions(-)
Signature · 1 line
2.35.3
PATCH v3 1/8 efivarfs: remove unused efi_varaible.Attributes and .kobj
These fields look to be remnants of older code: Attributes was likely meant to stash the variable attributes, but doesn't because we always read them from the variable store and kobj was likely left over from an older iteration of code where we manually created the objects instead of using a filesystem.
--- fs/efivarfs/internal.h | 2 -- fs/efivarfs/super.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-)
fs/efivarfs/internal.h +0 -2
@@ -24,13 +24,11 @@ struct efivarfs_fs_info {struct efi_variable {efi_char16_t VariableName[EFI_VAR_NAME_LEN /sizeof (efi_char16_t)];efi_guid_t VendorGuid; -__u32 Attributes; };struct efivar_entry {struct efi_variable var;struct list_head list; -struct kobject kobj; }; int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *,
fs/efivarfs/super.c +1 -1
@@ -245,7 +245,7 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,inode_lock (inode); inode->i_private = entry; -i_size_write (inode, size +sizeof (entry->var.Attributes)); +i_size_write (inode, size +sizeof (__u32));/* attributes + data */ inode_unlock (inode);d_add (dentry, inode);
Signature · 1 line
2.35.3
PATCH v3 2/8 efivarfs: add helper to convert from UC16 name and GUID to utf8 name
In reply to James BottomleyThese will be used by a later patch to check for uniqueness on initial EFI variable iteration.
--- fs/efivarfs/internal.h | 1 + fs/efivarfs/super.c | 17 +++-------------- fs/efivarfs/vars.c | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 14 deletions(-)
fs/efivarfs/internal.h +1 -0
@@ -55,6 +55,7 @@ bool efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data, unsigned long data_size);bool efivar_variable_is_removable (efi_guid_t vendor,const char *name,size_t len); +char *efivar_get_utf8name (const efi_char16_t *name16,efi_guid_t *vendor);extern const struct file_operations efivarfs_file_operations;extern const struct inode_operations efivarfs_dir_inode_operations;
fs/efivarfs/super.c +3 -14
@@ -205,27 +205,16 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,memcpy (entry->var.VariableName, name16, name_size);memcpy (&(entry->var.VendorGuid), &vendor,sizeof (efi_guid_t)); - len =ucs2_utf8size (entry->var.VariableName); - -/* name, plus '-', plus GUID, plus NUL*/ - name =kmalloc (len +1 +EFI_VARIABLE_GUID_LEN +1 ,GFP_KERNEL ); + name =efivar_get_utf8name (name16, &vendor);if (!name) goto fail; -ucs2_as_utf8 (name, entry->var.VariableName, len); +/* length of the variable name itself: remove GUID and separator */ + len =strlen (name) -EFI_VARIABLE_GUID_LEN -1 ;if (efivar_variable_is_removable (entry->var.VendorGuid, name, len)) is_removable = true; - name[len] ='-' ; - -efi_guid_to_str (&entry->var.VendorGuid, name + len +1 ); - - name[len +EFI_VARIABLE_GUID_LEN +1 ] ='\0' ; - -/* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */ -strreplace (name,'/' ,'!' ); - inode =efivarfs_get_inode (sb,d_inode (root),S_IFREG |0644 ,0 , is_removable);if (!inode)
fs/efivarfs/vars.c +25 -0
@@ -225,6 +225,31 @@ variable_matches(const char *var_name, size_t len, const char *match_name, } } +char * +efivar_get_utf8name (const efi_char16_t *name16,efi_guid_t *vendor) +{ +int len =ucs2_utf8size (name16); +char *name; + +/* name, plus '-', plus GUID, plus NUL*/ + name =kmalloc (len +1 +EFI_VARIABLE_GUID_LEN +1 ,GFP_KERNEL ); +if (!name) +return NULL ; + +ucs2_as_utf8 (name, name16, len); + + name[len] ='-' ; + +efi_guid_to_str (vendor, name + len +1 ); + + name[len +EFI_VARIABLE_GUID_LEN +1 ] ='\0' ; + +/* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */ +strreplace (name,'/' ,'!' ); + +return name; +} +bool efivar_validate (efi_guid_t vendor,efi_char16_t *var_name,u8 *data,unsigned long data_size)
Signature · 1 line
2.35.3
PATCH v3 3/8 efivarfs: make variable_is_present use dcache lookup
In reply to James BottomleyInstead of searching the variable entry list for a variable, use the dcache lookup functions to find it instead. Also add an efivarfs_ prefix to the function now it is no longer static.
---
v2: add IS_ERR_OR_NULL check before doing dput
--- fs/efivarfs/internal.h | 2 ++ fs/efivarfs/super.c | 29 +++++++++++++++++++++++++++++ fs/efivarfs/vars.c | 26 ++------------------------ 3 files changed, 33 insertions(+), 24 deletions(-)
fs/efivarfs/internal.h +2 -0
@@ -56,6 +56,8 @@ bool efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,bool efivar_variable_is_removable (efi_guid_t vendor,const char *name,size_t len);char *efivar_get_utf8name (const efi_char16_t *name16,efi_guid_t *vendor); +bool efivarfs_variable_is_present (efi_char16_t *variable_name, +efi_guid_t *vendor,void *data);extern const struct file_operations efivarfs_file_operations;extern const struct inode_operations efivarfs_dir_inode_operations;
fs/efivarfs/super.c +29 -0
@@ -181,6 +181,35 @@ static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)return ERR_PTR (-ENOMEM ); } +bool efivarfs_variable_is_present (efi_char16_t *variable_name, +efi_guid_t *vendor,void *data) +{ +char *name =efivar_get_utf8name (variable_name, vendor); +struct super_block *sb = data; +struct dentry *dentry; +struct qstr qstr; + +if (!name) +/* +* If the allocation failed there'll already be an +* error in the log (and likely a huge and growing +* number of them since they system will be under +* extreme memory pressure), so simply assume +* collision for safety but don't add to the log +* flood. +*/ +return true; + + qstr.name = name; + qstr.len =strlen (name); + dentry =d_hash_and_lookup (sb->s_root, &qstr); +kfree (name); +if (!IS_ERR_OR_NULL (dentry)) +dput (dentry); + +return dentry !=NULL ; +} +static int efivarfs_callback (efi_char16_t *name16,efi_guid_t vendor,unsigned long name_size,void *data,struct list_head *list)
fs/efivarfs/vars.c +2 -24
@@ -313,28 +313,6 @@ efivar_variable_is_removable(efi_guid_t vendor, const char *var_name,return found; } -static bool variable_is_present (efi_char16_t *variable_name,efi_guid_t *vendor, -struct list_head *head) -{ -struct efivar_entry *entry, *n; -unsigned long strsize1, strsize2; -bool found = false; - - strsize1 =ucs2_strsize (variable_name,EFI_VAR_NAME_LEN ); -list_for_each_entry_safe (entry, n, head, list) { - strsize2 =ucs2_strsize (entry->var.VariableName,EFI_VAR_NAME_LEN ); -if (strsize1 == strsize2 && - !memcmp (variable_name, &(entry->var.VariableName), - strsize2) && - !efi_guidcmp (entry->var.VendorGuid, - *vendor)) { - found = true; -break ; - } - } -return found; -} -/* * Returns the size of variable_name, in bytes, including the * terminating NULL character, or variable_name_size if no NULL
@@ -439,8 +417,8 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *,* we'll ever see a different variable name, * and may end up looping here forever. */ -if (variable_is_present (variable_name, &vendor_guid, - head)) { +if (efivarfs_variable_is_present (variable_name, + &vendor_guid, data)) {dup_variable_bug (variable_name, &vendor_guid, variable_name_size); status =EFI_NOT_FOUND ;
Signature · 1 line
2.35.3
PATCH v3 4/8 efivarfs: move variable lifetime management into the inodes
In reply to James BottomleyMake the inodes the default management vehicle for struct efivar_entry, so they are now all freed automatically if the file is removed and on unmount in kill_litter_super(). Remove the now superfluous iterator to free the entries after kill_litter_super().
Also fixes a bug where some entry freeing was missing causing efivarfs to leak memory.
---
v3: Move from evict_inode to alloc_inode/free_inode
--- fs/efivarfs/inode.c | 23 +++++++++---------- fs/efivarfs/internal.h | 7 +++++- fs/efivarfs/super.c | 51 ++++++++++++++++++++++++------------------ fs/efivarfs/vars.c | 39 +++----------------------------- 4 files changed, 48 insertions(+), 72 deletions(-)
fs/efivarfs/inode.c +10 -13
@@ -82,26 +82,23 @@ static int efivarfs_create(struct mnt_idmap *idmap, struct inode *dir,struct efivar_entry *var;int namelen, i =0 , err =0 ;bool is_removable = false; +efi_guid_t vendor;if (!efivarfs_valid_name (dentry->d_name.name, dentry->d_name.len))return -EINVAL ; - var =kzalloc (sizeof (struct efivar_entry ),GFP_KERNEL ); -if (!var) -return -ENOMEM ; -/* length of the variable name itself: remove GUID and separator */ namelen = dentry->d_name.len -EFI_VARIABLE_GUID_LEN -1 ; - err =guid_parse (dentry->d_name.name + namelen +1 , &var->var.VendorGuid); + err =guid_parse (dentry->d_name.name + namelen +1 , &vendor);if (err) goto out; -if (guid_equal (&var->var.VendorGuid, &LINUX_EFI_RANDOM_SEED_TABLE_GUID )) { +if (guid_equal (&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID )) { err = -EPERM ; goto out; } -if (efivar_variable_is_removable (var->var.VendorGuid, +if (efivar_variable_is_removable (vendor, dentry->d_name.name, namelen)) is_removable = true;
@@ -110,6 +107,9 @@ static int efivarfs_create(struct mnt_idmap *idmap, struct inode *dir, err = -ENOMEM ; goto out; } + var =efivar_entry (inode); + + var->var.VendorGuid = vendor;for (i =0 ; i < namelen; i++) var->var.VariableName[i] = dentry->d_name.name[i];
@@ -117,7 +117,6 @@ static int efivarfs_create(struct mnt_idmap *idmap, struct inode *dir, var->var.VariableName[i] ='\0' ; inode->i_private = var; -kmemleak_ignore (var); err =efivar_entry_add (var, &info->efivarfs_list);if (err)
@@ -126,11 +125,9 @@ static int efivarfs_create(struct mnt_idmap *idmap, struct inode *dir,d_instantiate (dentry, inode);dget (dentry); out: -if (err) { -kfree (var); -if (inode) -iput (inode); - } +if (err && inode) +iput (inode); +return err; }
fs/efivarfs/internal.h +6 -1
@@ -29,15 +29,20 @@ struct efi_variable {struct efivar_entry {struct efi_variable var;struct list_head list; +struct inode vfs_inode; }; +static inline struct efivar_entry *efivar_entry (struct inode *inode) +{ + return container_of(inode, struct efivar_entry, vfs_inode); +} +int efivar_init (int (*func)(efi_char16_t *,efi_guid_t ,unsigned long ,void *,struct list_head *),void *data,struct list_head *head);int efivar_entry_add (struct efivar_entry *entry,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_delete (struct efivar_entry *entry);int efivar_entry_size (struct efivar_entry *entry,unsigned long *size);
fs/efivarfs/super.c +29 -22
95 lines not on this page · raw message
fs/efivarfs/vars.c +3 -36
@@ -485,34 +485,6 @@ void __efivar_entry_add(struct efivar_entry *entry, struct list_head *head)list_add (&entry->list, head); } -/** -* efivar_entry_remove - remove entry from variable list -* @entry: entry to remove from list -* -* Returns 0 on success, or a kernel error code on failure. -*/ -void efivar_entry_remove (struct efivar_entry *entry) -{ -list_del (&entry->list); -} - -/* -* efivar_entry_list_del_unlock - remove entry from variable list -* @entry: entry to remove -* -* Remove @entry from the variable list and release the list lock. -* -* NOTE: slightly weird locking semantics here - we expect to be -* called with the efivars lock already held, and we release it before -* returning. This is because this function is usually called after -* set_variable() while the lock is still held. -*/ -static void efivar_entry_list_del_unlock (struct efivar_entry *entry) -{ -list_del (&entry->list); -efivar_unlock (); -} -/** * efivar_entry_delete - delete variable and remove entry from list * @entry: entry containing variable to delete
@@ -536,12 +508,10 @@ int efivar_entry_delete(struct efivar_entry *entry) status =efivar_set_variable_locked (entry->var.VariableName, &entry->var.VendorGuid,0 ,0 ,NULL , false); -if (!(status ==EFI_SUCCESS || status ==EFI_NOT_FOUND )) { -efivar_unlock (); +efivar_unlock (); +if (!(status ==EFI_SUCCESS || status ==EFI_NOT_FOUND ))return efi_status_to_err (status); - } -efivar_entry_list_del_unlock (entry);return 0 ; }
@@ -679,10 +649,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, &entry->var.VendorGuid, NULL, size, NULL); -if (status ==EFI_NOT_FOUND ) -efivar_entry_list_del_unlock (entry); -else -efivar_unlock (); +efivar_unlock ();if (status && status !=EFI_BUFFER_TOO_SMALL )return efi_status_to_err (status);
Signature · 1 line
2.35.3
PATCH v3 5/8 efivarfs: remove unused efivarfs_list
In reply to James BottomleyRemove all function helpers and mentions of the efivarfs_list now that all consumers of the list have been removed and entry management goes exclusively through the inode.
---
v3: tidy up returns in efivarfs_create (iput was never used)
--- fs/efivarfs/inode.c | 24 +++--------- fs/efivarfs/internal.h | 12 +----- fs/efivarfs/super.c | 12 +----- fs/efivarfs/vars.c | 89 ++++++------------------------------------ 4 files changed, 21 insertions(+), 116 deletions(-)
fs/efivarfs/inode.c +6 -18
@@ -77,7 +77,6 @@ static bool efivarfs_valid_name(const char *str, int len)static int efivarfs_create (struct mnt_idmap *idmap,struct inode *dir,struct dentry *dentry,umode_t mode,bool excl) { -struct efivarfs_fs_info *info = dir->i_sb->s_fs_info;struct inode *inode =NULL ;struct efivar_entry *var;int namelen, i =0 , err =0 ;
@@ -92,21 +91,17 @@ static int efivarfs_create(struct mnt_idmap *idmap, struct inode *dir, err =guid_parse (dentry->d_name.name + namelen +1 , &vendor);if (err) - goto out; -if (guid_equal (&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID )) { - err = -EPERM ; - goto out; - } +return err; +if (guid_equal (&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID )) +return -EPERM ;if (efivar_variable_is_removable (vendor, dentry->d_name.name, namelen)) is_removable = true; inode =efivarfs_get_inode (dir->i_sb, dir, mode,0 , is_removable); -if (!inode) { - err = -ENOMEM ; - goto out; - } +if (!inode) +return -ENOMEM ; var =efivar_entry (inode); var->var.VendorGuid = vendor;
@@ -118,17 +113,10 @@ static int efivarfs_create(struct mnt_idmap *idmap, struct inode *dir, inode->i_private = var; - err =efivar_entry_add (var, &info->efivarfs_list); -if (err) - goto out; -d_instantiate (dentry, inode);dget (dentry); -out: -if (err && inode) -iput (inode); -return err; +return 0 ; }static int efivarfs_unlink (struct inode *dir,struct dentry *dentry)
fs/efivarfs/internal.h +2 -10
@@ -6,7 +6,6 @@#ifndef EFIVAR_FS_INTERNAL_H #define EFIVAR_FS_INTERNAL_H -#include <linux/list.h> #include <linux/efi.h> struct efivarfs_mount_opts {
@@ -16,7 +15,6 @@ struct efivarfs_mount_opts {struct efivarfs_fs_info {struct efivarfs_mount_opts mount_opts; -struct list_head efivarfs_list;struct super_block *sb;struct notifier_block nb; };
@@ -28,7 +26,6 @@ struct efi_variable {struct efivar_entry {struct efi_variable var; -struct list_head list;struct inode vfs_inode; };
@@ -37,12 +34,9 @@ static inline struct efivar_entry *efivar_entry(struct inode *inode) return container_of(inode, struct efivar_entry, vfs_inode); } -int efivar_init (int (*func)(efi_char16_t *,efi_guid_t ,unsigned long ,void *, -struct list_head *), -void *data,struct list_head *head); +int efivar_init (int (*func)(efi_char16_t *,efi_guid_t ,unsigned long ,void *), +void *data); -int efivar_entry_add (struct efivar_entry *entry,struct list_head *head); -void __efivar_entry_add (struct efivar_entry *entry,struct list_head *head);int efivar_entry_delete (struct efivar_entry *entry);int efivar_entry_size (struct efivar_entry *entry,unsigned long *size);
@@ -53,8 +47,6 @@ int efivar_entry_get(struct efivar_entry *entry, u32 *attributes,int efivar_entry_set_get_size (struct efivar_entry *entry,u32 attributes,unsigned long *size,void *data,bool *set); -int efivar_entry_iter (int (*func)(struct efivar_entry *,void *), -struct list_head *head,void *data);bool efivar_validate (efi_guid_t vendor,efi_char16_t *var_name,u8 *data,unsigned long data_size);
fs/efivarfs/super.c +2 -10
@@ -55,8 +55,6 @@ static void efivarfs_free_inode(struct inode *inode) {struct efivar_entry *entry =efivar_entry (inode); -if (inode->i_private) -list_del (&entry->list);kfree (entry); }
@@ -228,8 +226,7 @@ bool efivarfs_variable_is_present(efi_char16_t *variable_name, }static int efivarfs_callback (efi_char16_t *name16,efi_guid_t vendor, -unsigned long name_size,void *data, -struct list_head *list) +unsigned long name_size,void *data) {struct super_block *sb = (struct super_block *)data;struct efivar_entry *entry;
@@ -271,7 +268,6 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor, }__efivar_entry_get (entry,NULL , &size,NULL ); -__efivar_entry_add (entry, list);/* copied by the above to local storage in the dentry. */ kfree (name);
@@ -361,7 +357,7 @@ static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc)if (err)return err; -return efivar_init (efivarfs_callback, sb, &sfi->efivarfs_list); +return efivar_init (efivarfs_callback, sb); }static int efivarfs_get_tree (struct fs_context *fc)
@@ -396,8 +392,6 @@ static int efivarfs_init_fs_context(struct fs_context *fc)if (!sfi)return -ENOMEM ; -INIT_LIST_HEAD (&sfi->efivarfs_list); - sfi->mount_opts.uid =GLOBAL_ROOT_UID ; sfi->mount_opts.gid =GLOBAL_ROOT_GID ;
@@ -413,8 +407,6 @@ static void efivarfs_kill_sb(struct super_block *sb)blocking_notifier_chain_unregister (&efivar_ops_nh, &sfi->nb);kill_litter_super (sb); -/* Remove all entries and destroy */ -WARN_ON (!list_empty (&sfi->efivarfs_list));kfree (sfi); }
fs/efivarfs/vars.c +11 -78
129 lines not on this page · raw message
Signature · 1 line
2.35.3
PATCH v3 6/8 efivarfs: fix error on write to new variable leaving remnants
In reply to James BottomleyMake variable cleanup go through the fops release mechanism and use zero inode size as the indicator to delete the file. Since all EFI variables must have an initial u32 attribute, zero size occurs either because the update deleted the variable or because an unsuccessful write after create caused the size never to be set in the first place. In the case of multiple racing opens and closes, the open is counted to ensure that the zero size check is done on the last close.
Even though this fixes the bug that a create either not followed by a write or followed by a write that errored would leave a remnant file for the variable, the file will appear momentarily globally visible until the last close of the fd deletes it. This is safe because the normal filesystem operations will mediate any races; however, it is still possible for a directory listing at that instant between create and close contain a zero size variable that doesn't exist in the EFI table.
---
v2: implement counter for last close
v3: add removed state to struct efivar_entry and use in place of d_unhashed()
--- fs/efivarfs/file.c | 59 +++++++++++++++++++++++++++++++++++------- fs/efivarfs/internal.h | 2 ++ fs/efivarfs/super.c | 1 + 3 files changed, 53 insertions(+), 9 deletions(-)
fs/efivarfs/file.c +50 -9
@@ -36,28 +36,41 @@ static ssize_t efivarfs_file_write(struct file *file,if (IS_ERR (data))return PTR_ERR (data); +inode_lock (inode); +if (var->removed) { +/* +* file got removed; don't allow a set. Caused by an +* unsuccessful create or successful delete write +* racing with us. +*/ + bytes = -EIO ; + goto out; + } + bytes =efivar_entry_set_get_size (var, attributes, &datasize, data, &set); -if (!set && bytes) { +if (!set) {if (bytes == -ENOENT ) bytes = -EIO ; goto out; }if (bytes == -ENOENT ) { -drop_nlink (inode); -d_delete (file->f_path.dentry); -dput (file->f_path.dentry); +/* +* zero size signals to release that the write deleted +* the variable +*/ +i_size_write (inode,0 ); }else { -inode_lock (inode);i_size_write (inode, datasize +sizeof (attributes));inode_set_mtime_to_ts (inode,inode_set_ctime_current (inode)); -inode_unlock (inode); } bytes = count; out: +inode_unlock (inode); +kfree (data);return bytes;
@@ -106,8 +119,36 @@ static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,return size; } +static int efivarfs_file_release (struct inode *inode,struct file *file) +{ +struct efivar_entry *var = inode->i_private; + +inode_lock (inode); + var->removed = (--var->open_count ==0 &&i_size_read (inode) ==0 ); +inode_unlock (inode); + +if (var->removed) +simple_recursive_removal (file->f_path.dentry,NULL ); + +return 0 ; +} + +static int efivarfs_file_open (struct inode *inode,struct file *file) +{ +struct efivar_entry *entry = inode->i_private; + + file->private_data = entry; + +inode_lock (inode); + entry->open_count++; +inode_unlock (inode); + +return 0 ; +} +const struct file_operations efivarfs_file_operations = { - .open = simple_open, - .read = efivarfs_file_read, - .write = efivarfs_file_write, + .open = efivarfs_file_open, + .read = efivarfs_file_read, + .write = efivarfs_file_write, + .release = efivarfs_file_release, };
fs/efivarfs/internal.h +2 -0
@@ -27,6 +27,8 @@ struct efi_variable {struct efivar_entry {struct efi_variable var;struct inode vfs_inode; +unsigned long open_count; +bool removed; };static inline struct efivar_entry *efivar_entry (struct inode *inode)
fs/efivarfs/super.c +1 -0
@@ -47,6 +47,7 @@ static struct inode *efivarfs_alloc_inode(struct super_block *sb)return NULL ;inode_init_once (&entry->vfs_inode); + entry->removed = false;return &entry->vfs_inode; }
Signature · 1 line
2.35.3
PATCH v3 7/8 selftests/efivarfs: fix tests for failed write removal
In reply to James BottomleyThe current self tests expect the zero size remnants that failed variable creation leaves. Update the tests to verify these are now absent.
--- tools/testing/selftests/efivarfs/efivarfs.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
tools/testing/selftests/efivarfs/efivarfs.sh +7 -4
@@ -76,11 +76,11 @@ test_create_empty() : > $file - if [ ! -e $file ]; then - echo "$file can not be created without writing" >&2 + if [ -e $file ]; then + echo "$file can be created without writing" >&2 + file_cleanup $file exit 1 fi - file_cleanup $file } test_create_read()
@@ -89,10 +89,13 @@ test_create_read() ./create-read $file if [ $? -ne 0 ]; then echo "create and read $file failed" + exit 1 + fi + if [ -e $file ]; then + echo "file still exists and should not" file_cleanup $file exit 1 fi - file_cleanup $file } test_delete()
Signature · 1 line
2.35.3
PATCH v3 8/8 selftests/efivarfs: add concurrent update tests
In reply to James BottomleyThe delete on last close functionality can now only be tested properly by using multiple threads to hold open the variable files and testing what happens as they complete.
--- tools/testing/selftests/efivarfs/efivarfs.sh | 114 +++++++++++++++++++ 1 file changed, 114 insertions(+)
tools/testing/selftests/efivarfs/efivarfs.sh +114 -0
@@ -227,6 +227,116 @@ test_no_set_size() exit $ret } +setup_test_multiple() +{ + ## + # we're going to do multi-threaded tests, so create a couple + # of pipes for synchronization + ## + + # empty is because arrays number from 0 but jobs number from 1 + p=("" /tmp/efivarfs_pipe1 /tmp/efivarfs_pipe2 /tmp/efivarfs_pipe3) + # create but ignore failure + mknod ${p[1]} p + mknod ${p[2]} p + mknod ${p[3]} p + + declare -g var=$efivarfs_mount/test_multiple-$test_guid + + cleanup() { + for f in ${p[@]}; do + rm -f ${f} + done + if [ -e $var ]; then + file_cleanup $var + fi + } + trap cleanup exit + + waitpipe() { + cat ${p[$1]} > /dev/null + } + + endjob() { + echo 1 > ${p[$1]} + while jobs %${1}; do true; done > /dev/null 2>&1 + } +} + +test_multiple_zero_size() +{ + ## + # check for remove on last close, set up three threads all + # holding the variable (one write and two reads) and then + # close them sequentially (waiting for completion) and check + # the state of the variable + ## + + { waitpipe 1; echo 1; } > $var 2> /dev/null & + # zero length file should exist + [ -e $var ] || exit 1 + # second and third delayed close + { waitpipe 2; } < $var & + { waitpipe 3; } < $var & + # close first fd + endjob 1 + # var should only be deleted on last close + [ -e $var ] || exit 1 + # close second fd + endjob 2 + [ -e $var ] || exit 1 + # file should go on last close + endjob 3 + [ ! -e $var ] || exit 1 +} + +test_multiple_create() +{ + ## + # set multiple threads to access the variable but delay + # the final write to check the close of 2 and 3. The + # final write should succeed in creating the variable + ## + { waitpipe 1; printf '\x07\x00\x00\x00\x54'; } > $var & + [ -e $var -a ! -s $var ] || exit 1 + { waitpipe 2; } < $var & + { waitpipe 3; } < $var & + # close second and third fds + endjob 2 + # var should only be created (have size) on last close + [ -e $var -a ! -s $var ] || exit 1 + endjob 3 + [ -e $var -a ! -s $var ] || exit 1 + # close first fd + endjob 1 + # variable should still exist + [ -s $var ] || exit 1 + file_cleanup $var +} + +test_multiple_delete_on_write() { + ## + # delete the variable on final write; seqencing similar + # to test_multiple_create() + ## + printf '\x07\x00\x00\x00\x54' > $var + chattr -i $var + { waitpipe 1; printf '\x07\x00\x00\x00'; } > $var & + [ -e $var -a -s $var ] || exit 1 + { waitpipe 2; } < $var & + { waitpipe 3; } < $var & + # close first fd; write should set variable size to zero + endjob 1 + # var should only be deleted on last close + [ -e $var -a ! -s $var ] || exit 1 + endjob 2 + [ -e $var ] || exit 1 + # close last fd + endjob 3 + # variable should now be removed + [ ! -e $var ] || exit 1 +} + check_prereqs rc=0
@@ -240,5 +350,9 @@ run_test test_open_unlink run_test test_valid_filenames run_test test_invalid_filenames run_test test_no_set_size +setup_test_multiple +run_test test_multiple_zero_size +run_test test_multiple_create +run_test test_multiple_delete_on_write exit $rc
Signature · 1 line
2.35.3
Ard Biesheuvel
maintainerQuoted 55 lines · James Bottomley
I've added fsdevel because I'm hopping some kind vfs person will check the shift from efivarfs managing its own data to its data being managed as part of the vfs object lifetimes. The following paragraph should describe all you need to know about the unusual features of the filesystem.
efivarfs is a filesystem projecting the current state of the UEFI variable store and allowing updates via write. Because EFI variables contain both contents and a set of attributes, which can't be mapped to filesystem data, the u32 attribute is prepended to the output of the file and, since UEFI variables can't be empty, this makes every file at least 5 characters long. EFI variables can be removed either by doing an unlink (easy) or by doing a conventional write update that reduces the content to zero size, which means any write update can potentially remove the file.
Currently efivarfs has two bugs: it leaks memory and if a create is attempted that results in an error in the write, it creates a zero length file remnant that doesn't represent an EFI variable (i.e. the state reflection of the EFI variable store goes out of sync).
The code uses
inode->i_privateto point to additionaly allocated information but tries to maintain a global list of the shadowed varibles for internal tracking. Forgetting tokfree()entries in this list when they are deleted is the source of the memory leak.I've tried to make the patches as easily reviewable by non-EFI people as possible, so some possible cleanups (like consolidating or removing the efi lock handling and possibly removing the additional entry allocation entirely in favour of simply converting the dentry name to the variable name and guid) are left for later.
The first patch removes some unused fields in the entry; patches 2-3 eliminate the list search for duplication (some EFI variable stores have buggy iterators) and replaces it with a dcache lookup. Patch 4 move responsibility for freeing the entry data to inode_alloc/inode_free which both fixes the memory leak and also means we no longer need to iterate over the variable list and free its entries in
kill_sb. Since the variable list is now unused, patch 5 removes it and its helper functions.Patch 6 fixes the second bug by introducing a
file_operations->releasemethod that checks to see if the inode size is zero when the file is closed and removes it if it is. Since all files must be at least 5 in length we use a zeroi_sizeas an indicator that either the variable was removed on write or that it wasn't correctly created in the first place.Patch 7 fixes the old self tests which check for zero length files on incorrect variable creation (these are now removed).
Patch 8 adds a new set of self tests for multi threaded variable updates checking for the new behaviour.
Quoted 7 lines
v2: folded in feedback from Al Viro: check errors on lookup and delete
zero length file on last closev3: convert to alloc/free instead of evict and use a boolean in
efivar_entryunder the inode lock to indicate removal and add additional selftests
Thanks James. I've queued up this version now, so we'll get some coverage from the robots. I'll redo my own testing tomorrow, but I'll omit these changes from my initial PR to Linus. If we're confident that things are sound, I'll send another PR during the second half of the merge window.
Ard Biesheuvel
maintainerQuoted 4 lines · Ard Biesheuvel
Quoted 1 line · James Bottomley
...
Quoted 6 lines
Thanks James. I've queued up this version now, so we'll get some coverage from the robots. I'll redo my own testing tomorrow, but I'll omit these changes from my initial PR to Linus. If we're confident that things are sound, I'll send another PR during the second half of the merge window.
I'm hitting the failure cases below. The first one appears to hit the same 'Operation not permitted' condition on the write, the error message is just hidden by the /dev/null redirect.
I'm running the make command from a root shell. Using printf from the command line works happily so I suspect there is some issue with the concurrency and the subshells?
# --------------------
# running test_multiple_zero_size # -------------------- # [FAIL] # -------------------- # running test_multiple_create # -------------------- # ./efivarfs.sh: line 294: /sys/firmware/efi/efivars/test_multiple-210be57c-9849-4fc7-a635-e6382d1aec27: Operation not permitted # [FAIL] # -------------------- # running test_multiple_delete_on_write # -------------------- # [PASS] not ok 1 selftests: efivarfs: efivarfs.sh # exit=1
Quoted 15 lines · Ard Biesheuvel
Quoted 4 lines · Ard Biesheuvel
Quoted 1 line · James Bottomley
...
Quoted 8 lines
Thanks James. I've queued up this version now, so we'll get some coverage from the robots. I'll redo my own testing tomorrow, but I'll
omit these changes from my initial PR to Linus. If we're confident that things are sound, I'll send another PR during the second half of
the merge window.
Quoted 7 lines
I'm hitting the failure cases below. The first one appears to hit the same 'Operation not permitted' condition on the write, the error message is just hidden by the
/dev/nullredirect.I'm running the make command from a root shell. Using printf from the command line works happily so I suspect there is some issue with the concurrency and the subshells?
It could be that the file isn't opened until the subshell is spawned. I can probably use the pipe to wait for the subshell to start; I'll try to code that up.
Regards,
James
Quoted 19 lines · James Bottomley
Quoted 18 lines · Ard Biesheuvel
Quoted 4 lines · Ard Biesheuvel
Quoted 1 line · James Bottomley
...
Quoted 6 lines
Thanks James. I've queued up this version now, so we'll get some coverage from the robots. I'll redo my own testing tomorrow, but I'll omit these changes from my initial PR to Linus. If we're confident that things are sound, I'll send another PR during the second half of the merge window.
I'm hitting the failure cases below. The first one appears to hit the same 'Operation not permitted' condition on the write, the error message is just hidden by the
/dev/nullredirect.
Quoted 7 lines
Quoted 3 lines
I'm running the make command from a root shell. Using printf from the command line works happily so I suspect there is some issue with the concurrency and the subshells?
It could be that the file isn't opened until the subshell is spawned. I can probably use the pipe to wait for the subshell to start; I'll try to code that up.
OK, this is what I came up with. It works for me (but then so did the other script ... I think my testing VM is probably a little slow).
Regards,
James
--->8>8>8<8<8<8---
From: James Bottomley <[email protected]>
Subject: [PATCH] selftests/efivarfs: add concurrent update tests
The delete on last close functionality can now only be tested properly by using multiple threads to hold open the variable files and testing what happens as they complete.
---
tools/testing/selftests/efivarfs/efivarfs.sh +134 -0
@@ -227,6 +227,136 @@ test_no_set_size() exit $ret } +setup_test_multiple() +{ + ## + # we're going to do multi-threaded tests, so create a set of + # pipes for synchronization. We use pipes 1..3 to start the + # stalled shell job and pipes 4..6 as indicators that the job + # has started. If you need more than 3 jobs the two +3's below + # need increasing + ## + + declare -ag p + + # empty is because arrays number from 0 but jobs number from 1 + p[0]="" + + for f in 1 2 3 4 5 6; do + p[$f]=/tmp/efivarfs_pipe${f} + mknod ${p[$f]} p + done + + declare -g var=$efivarfs_mount/test_multiple-$test_guid + + cleanup() { + for f in ${p[@]}; do + rm -f ${f} + done + if [ -e $var ]; then + file_cleanup $var + fi + } + trap cleanup exit + + waitstart() { + cat ${p[$[$1+3]]} > /dev/null + } + + waitpipe() { + echo 1 > ${p[$[$1+3]]} + cat ${p[$1]} > /dev/null + } + + endjob() { + echo 1 > ${p[$1]} + wait -n %$1 + } +} + +test_multiple_zero_size() +{ + ## + # check for remove on last close, set up three threads all + # holding the variable (one write and two reads) and then + # close them sequentially (waiting for completion) and check + # the state of the variable + ## + + { waitpipe 1; echo 1; } > $var 2> /dev/null & + waitstart 1 + # zero length file should exist + [ -e $var ] || exit 1 + # second and third delayed close + { waitpipe 2; } < $var & + waitstart 2 + { waitpipe 3; } < $var & + waitstart 3 + # close first fd + endjob 1 + # var should only be deleted on last close + [ -e $var ] || exit 1 + # close second fd + endjob 2 + [ -e $var ] || exit 1 + # file should go on last close + endjob 3 + [ ! -e $var ] || exit 1 +} + +test_multiple_create() +{ + ## + # set multiple threads to access the variable but delay + # the final write to check the close of 2 and 3. The + # final write should succeed in creating the variable + ## + { waitpipe 1; printf '\x07\x00\x00\x00\x54'; } > $var & + waitstart 1 + [ -e $var -a ! -s $var ] || exit 1 + { waitpipe 2; } < $var & + waitstart 2 + { waitpipe 3; } < $var & + waitstart 3 + # close second and third fds + endjob 2 + # var should only be created (have size) on last close + [ -e $var -a ! -s $var ] || exit 1 + endjob 3 + [ -e $var -a ! -s $var ] || exit 1 + # close first fd + endjob 1 + # variable should still exist + [ -s $var ] || exit 1 + file_cleanup $var +} + +test_multiple_delete_on_write() { + ## + # delete the variable on final write; seqencing similar + # to test_multiple_create() + ## + printf '\x07\x00\x00\x00\x54' > $var + chattr -i $var + { waitpipe 1; printf '\x07\x00\x00\x00'; } > $var & + waitstart 1 + [ -e $var -a -s $var ] || exit 1 + { waitpipe 2; } < $var & + waitstart 2 + { waitpipe 3; } < $var & + waitstart 3 + # close first fd; write should set variable size to zero + endjob 1 + # var should only be deleted on last close + [ -e $var -a ! -s $var ] || exit 1 + endjob 2 + [ -e $var ] || exit 1 + # close last fd + endjob 3 + # variable should now be removed + [ ! -e $var ] || exit 1 +} + check_prereqs rc=0
@@ -240,5 +370,9 @@ run_test test_open_unlink run_test test_valid_filenames run_test test_invalid_filenames run_test test_no_set_size +setup_test_multiple +run_test test_multiple_zero_size +run_test test_multiple_create +run_test test_multiple_delete_on_write exit $rc
Ard Biesheuvel
maintainerQuoted 25 lines · James Bottomley
Quoted 23 lines · James Bottomley
Quoted 21 lines · Ard Biesheuvel
Quoted 4 lines · Ard Biesheuvel
Quoted 1 line · James Bottomley
...
Quoted 6 lines
Thanks James. I've queued up this version now, so we'll get some coverage from the robots. I'll redo my own testing tomorrow, but I'll omit these changes from my initial PR to Linus. If we're confident that things are sound, I'll send another PR during the second half of the merge window.
I'm hitting the failure cases below. The first one appears to hit the same 'Operation not permitted' condition on the write, the error message is just hidden by the
/dev/nullredirect.I'm running the make command from a root shell. Using printf from the command line works happily so I suspect there is some issue with the concurrency and the subshells?
Quoted 7 lines
Quoted 3 lines
It could be that the file isn't opened until the subshell is spawned. I can probably use the pipe to wait for the subshell to start; I'll try to code that up.
OK, this is what I came up with. It works for me (but then so did the other script ... I think my testing VM is probably a little slow).
Thanks - that works on my end too.
I am testing this on a bare metal arm64 machine, but an ancient and very slow one. So it might be the other way around.
I am going to grab this version and queue it up.
Thanks.