Skip to contentSkip to conversation

efivarfs: Cache occupied and available space

PATCH Applied Ard Biesheuvel 2 participants 5 messages Aug 1 – Aug 17

Subsystems (3)
lore
Share & tools
lore

Ard Biesheuvel

author maintainer
#0 lore raw

PATCH 1/1 efivarfs: Cache occupied and available space

Ravi reports that statfs() may be called by unprivileged users on the efivarfs mount point, resulting in repeated calls to QueryVariableInfo() which are disproportionately costly on x86 systems where the variable store is backed by SMM, as each SMM entry requires a rendez-vous of all the CPUs.

So cache the output of QueryVariableInfo(), and invalidate the cached values when an entry is added or deleted, or the variable store is synchronized after a suspend/resume sequence.

Fixes d86ff3333cb1 ("efivarfs: expose used and total size")
---
 fs/efivarfs/internal.h |  2 ++
 fs/efivarfs/super.c    | 21 +++++++++++---------
 fs/efivarfs/vars.c     |  9 +++++++++
 3 files changed, 23 insertions(+), 9 deletions(-)
fs/efivarfs/internal.h +2 -0
index f913b6824289..d43789bc7839 100644
@@ -31,6 +31,8 @@ struct efivar_entry {
 	bool removed;
 };
 
+extern u64 efivar_storage_space, efivar_remaining_space;
+
 static inline struct efivar_entry *efivar_entry(struct inode *inode)
 {
 	return container_of(inode, struct efivar_entry, vfs_inode);
fs/efivarfs/super.c +12 -9
index 733c19571f1c..3691a2a087a0 100644
@@ -23,6 +23,8 @@
 #include "internal.h"
 #include "../internal.h"
 
+u64 efivar_storage_space, efivar_remaining_space;
+
 static int efivarfs_ops_notifier(struct notifier_block *nb, unsigned long event,
 				 void *data)
 {
@@ -82,15 +84,16 @@ static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	const u32 attr = EFI_VARIABLE_NON_VOLATILE |
 			 EFI_VARIABLE_BOOTSERVICE_ACCESS |
 			 EFI_VARIABLE_RUNTIME_ACCESS;
-	u64 storage_space, remaining_space, max_variable_size;
 	u64 id = huge_encode_dev(dentry->d_sb->s_dev);
+	u64 max_variable_size;
 	efi_status_t status;
 
 	/* Some UEFI firmware does not implement QueryVariableInfo() */
-	storage_space = remaining_space = 0;
-	if (efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
-		status = efivar_query_variable_info(attr, &storage_space,
-						    &remaining_space,
+	if (efivar_storage_space == 0 &&
+	    efivar_remaining_space == 0 &&
+	    efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
+		status = efivar_query_variable_info(attr, &efivar_storage_space,
+						    &efivar_remaining_space,
 						    &max_variable_size);
 		if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED)
 			pr_warn_ratelimited("query_variable_info() failed: 0x%lx\n",
@@ -104,8 +107,8 @@ static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	 */
 	buf->f_bsize	= 1;
 	buf->f_namelen	= NAME_MAX;
-	buf->f_blocks	= storage_space;
-	buf->f_bfree	= remaining_space;
+	buf->f_blocks	= efivar_storage_space;
+	buf->f_bfree	= efivar_remaining_space;
 	buf->f_type	= dentry->d_sb->s_magic;
 	buf->f_fsid	= u64_to_fsid(id);
 
@@ -114,8 +117,8 @@ static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	 * when the storage_paranoia x86 quirk is active. To use more, users
 	 * should boot the kernel with efi_no_storage_paranoia.
 	 */
-	if (remaining_space > efivar_reserved_space())
-		buf->f_bavail = remaining_space - efivar_reserved_space();
+	if (efivar_remaining_space > efivar_reserved_space())
+		buf->f_bavail = efivar_remaining_space - efivar_reserved_space();
 	else
 		buf->f_bavail = 0;
 
fs/efivarfs/vars.c +9 -0
index 6833c3d24b54..4196963695bd 100644
@@ -361,6 +361,11 @@ static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
 	kfree(str8);
 }
 
+static void efivar_invalidate_cached_storage_space(void)
+{
+	efivar_storage_space = efivar_remaining_space = 0;
+}
+
 /**
  * efivar_init - build the initial list of EFI variables
  * @func: callback function to invoke for every variable
@@ -450,6 +455,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 	} while (status != EFI_NOT_FOUND);
 
 	efivar_unlock();
+	efivar_invalidate_cached_storage_space();
 free:
 	kfree(variable_name);
 
@@ -480,6 +486,8 @@ int efivar_entry_delete(struct efivar_entry *entry)
 					    &entry->var.VendorGuid,
 					    0, 0, NULL, false);
 	efivar_unlock();
+	efivar_invalidate_cached_storage_space();
+
 	if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND))
 		return efi_status_to_err(status);
 
@@ -620,6 +628,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 				    NULL, size, NULL);
 
 	efivar_unlock();
+	efivar_invalidate_cached_storage_space();
 
 	if (status && status != EFI_BUFFER_TOO_SMALL)
 		return efi_status_to_err(status);
Signature · 1 line
2.55.0.508.g3f0d502094-goog
author maintainer Applied
#1 lore raw
Quoted 14 lines · Ard Biesheuvel

Ravi reports that statfs() may be called by unprivileged users on the efivarfs mount point, resulting in repeated calls to QueryVariableInfo() which are disproportionately costly on x86 systems where the variable store is backed by SMM, as each SMM entry requires a rendez-vous of all the CPUs.

So cache the output of QueryVariableInfo(), and invalidate the cached values when an entry is added or deleted, or the variable store is synchronized after a suspend/resume sequence.

Fixes d86ff3333cb1 ("efivarfs: expose used and total size")
Quoted 6 lines
---
 fs/efivarfs/internal.h |  2 ++
 fs/efivarfs/super.c    | 21 +++++++++++---------
 fs/efivarfs/vars.c     |  9 +++++++++
 3 files changed, 23 insertions(+), 9 deletions(-)

Applied to efi/next

#2 lore raw
In reply to Ard Biesheuvel

Hi Ard,

Please find a few comments below,

Quoted 38 lines · Ard Biesheuvel

Ravi reports that statfs() may be called by unprivileged users on the efivarfs mount point, resulting in repeated calls to QueryVariableInfo() which are disproportionately costly on x86 systems where the variable store is backed by SMM, as each SMM entry requires a rendez-vous of all the CPUs.

So cache the output of QueryVariableInfo(), and invalidate the cached values when an entry is added or deleted, or the variable store is synchronized after a suspend/resume sequence.

Fixes d86ff3333cb1 ("efivarfs: expose used and total size")
---
 fs/efivarfs/internal.h |  2 ++
 fs/efivarfs/super.c    | 21 +++++++++++---------
 fs/efivarfs/vars.c     |  9 +++++++++
 3 files changed, 23 insertions(+), 9 deletions(-)
fs/efivarfs/internal.h +2 -0
index f913b6824289..d43789bc7839 100644
@@ -31,6 +31,8 @@ struct efivar_entry {
 	bool removed;
 };
 
+extern u64 efivar_storage_space, efivar_remaining_space;
+
 static inline struct efivar_entry *efivar_entry(struct inode *inode)
 {
 	return container_of(inode, struct efivar_entry, vfs_inode);
fs/efivarfs/super.c +0 -0
index 733c19571f1c..3691a2a087a0 100644

fs/efivarfs/super.c line 27

Quoted 5 lines
fs/efivarfs/super.c +2 -0
@@ -23,6 +23,8 @@
 #include "internal.h"
 #include "../internal.h"
 
+u64 efivar_storage_space, efivar_remaining_space;
+

Since those are globals (before they were only local to the function), I'd put them behind a lock now, probably efivar_lock, because we need something that's globally available.

statfs could be raced from userspace vs the rest of the code; and efivar_invalidate_cached_storage_space is only called outside the lock.

Quoted 15 lines
fs/efivarfs/super.c +1 -3
 static int efivarfs_ops_notifier(struct notifier_block *nb, unsigned long event,
 				 void *data)
 {
@@ -82,15 +84,16 @@ static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	const u32 attr = EFI_VARIABLE_NON_VOLATILE |
 			 EFI_VARIABLE_BOOTSERVICE_ACCESS |
 			 EFI_VARIABLE_RUNTIME_ACCESS;
-	u64 storage_space, remaining_space, max_variable_size;
 	u64 id = huge_encode_dev(dentry->d_sb->s_dev);
+	u64 max_variable_size;
 	efi_status_t status;
 
 	/* Some UEFI firmware does not implement QueryVariableInfo() */
-	storage_space = remaining_space = 0;
-	if (efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {

fs/efivarfs/super.c static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf) · line 99

Quoted 10 lines
fs/efivarfs/super.c +5 -2
-		status = efivar_query_variable_info(attr, &storage_space,
-						    &remaining_space,
+	if (efivar_storage_space == 0 &&
+	    efivar_remaining_space == 0 &&
+	    efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
+		status = efivar_query_variable_info(attr, &efivar_storage_space,
+						    &efivar_remaining_space,
 						    &max_variable_size);
 		if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED)
 			pr_warn_ratelimited("query_variable_info() failed: 0x%lx\n",

We've had issues with firmware in the past, shouldn't we be a bit more defensive here in case of errors? I'd reset both variables on error in case an incomplete write to one of the variables gives us an invalid state.

Quoted 56 lines
fs/efivarfs/super.c +4 -4
@@ -104,8 +107,8 @@ static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	 */
 	buf->f_bsize	= 1;
 	buf->f_namelen	= NAME_MAX;
-	buf->f_blocks	= storage_space;
-	buf->f_bfree	= remaining_space;
+	buf->f_blocks	= efivar_storage_space;
+	buf->f_bfree	= efivar_remaining_space;
 	buf->f_type	= dentry->d_sb->s_magic;
 	buf->f_fsid	= u64_to_fsid(id);
 
@@ -114,8 +117,8 @@ static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	 * when the storage_paranoia x86 quirk is active. To use more, users
 	 * should boot the kernel with efi_no_storage_paranoia.
 	 */
-	if (remaining_space > efivar_reserved_space())
-		buf->f_bavail = remaining_space - efivar_reserved_space();
+	if (efivar_remaining_space > efivar_reserved_space())
+		buf->f_bavail = efivar_remaining_space - efivar_reserved_space();
 	else
 		buf->f_bavail = 0;
 
fs/efivarfs/vars.c +8 -0
index 6833c3d24b54..4196963695bd 100644
@@ -361,6 +361,11 @@ static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
 	kfree(str8);
 }
 
+static void efivar_invalidate_cached_storage_space(void)
+{
+	efivar_storage_space = efivar_remaining_space = 0;
+}
+
 /**
  * efivar_init - build the initial list of EFI variables
  * @func: callback function to invoke for every variable
@@ -450,6 +455,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 	} while (status != EFI_NOT_FOUND);
 
 	efivar_unlock();
+	efivar_invalidate_cached_storage_space();
 free:
 	kfree(variable_name);
 
@@ -480,6 +486,8 @@ int efivar_entry_delete(struct efivar_entry *entry)
 					    &entry->var.VendorGuid,
 					    0, 0, NULL, false);
 	efivar_unlock();
+	efivar_invalidate_cached_storage_space();
+
 	if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND))
 		return efi_status_to_err(status);
 

fs/efivarfs/vars.c int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, · line 631

Quoted 4 lines
fs/efivarfs/vars.c +1 -0
@@ -620,6 +628,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 				    NULL, size, NULL);
 
 	efivar_unlock();
+	efivar_invalidate_cached_storage_space();

In this function, the error path for efi_set_variable_locked (that could have triggered an EFI_OUT_OF_RESOURCES) is not covered by the invalidation.

fs/efivarfs/vars.c int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, · line 634

Quoted 4 lines
fs/efivarfs/vars.c +0 -0
 	if (status && status != EFI_BUFFER_TOO_SMALL)
 		return efi_status_to_err(status);

Couldn't the variables be written outside of efivarfs as well? efivar_set_variable can be called from other code? pstore could maybe be ignored if it's only used during crashes (I'm not sure), but I see at least one other driver calling it as well. Wouldn't that change the available/free size?

Regards,

Anisse

author maintainer
#3 lore raw

Hi,

Thanks for the review.

Quoted 46 lines · Anisse Astier

Hi Ard,

Please find a few comments below,

Quoted 41 lines · Ard Biesheuvel

Ravi reports that statfs() may be called by unprivileged users on the efivarfs mount point, resulting in repeated calls to QueryVariableInfo() which are disproportionately costly on x86 systems where the variable store is backed by SMM, as each SMM entry requires a rendez-vous of all the CPUs.

So cache the output of QueryVariableInfo(), and invalidate the cached values when an entry is added or deleted, or the variable store is synchronized after a suspend/resume sequence.

Fixes d86ff3333cb1 ("efivarfs: expose used and total size")
---
 fs/efivarfs/internal.h |  2 ++
 fs/efivarfs/super.c    | 21 +++++++++++---------
 fs/efivarfs/vars.c     |  9 +++++++++
 3 files changed, 23 insertions(+), 9 deletions(-)
fs/efivarfs/internal.h +2 -0
index f913b6824289..d43789bc7839 100644
@@ -31,6 +31,8 @@ struct efivar_entry {
 	bool removed;
 };
 
+extern u64 efivar_storage_space, efivar_remaining_space;
+
 static inline struct efivar_entry *efivar_entry(struct inode *inode)
 {
 	return container_of(inode, struct efivar_entry, vfs_inode);
fs/efivarfs/super.c +0 -0
index 733c19571f1c..3691a2a087a0 100644
@@ -23,6 +23,8 @@
 #include "internal.h"
 #include "../internal.h"
 
Quoted 9 lines

fs/efivarfs/super.c line 27

Quoted 2 lines
fs/efivarfs/super.c +2 -0
+u64 efivar_storage_space, efivar_remaining_space;
+

Since those are globals (before they were only local to the function), I'd put them behind a lock now, probably efivar_lock, because we need something that's globally available.

statfs could be raced from userspace vs the rest of the code; and efivar_invalidate_cached_storage_space is only called outside the lock.

Right. So we might inadvertently report zero size and zero available space even though QueryVariableInfo() is supported and working correctly. Not the end of the world imo, but better avoided if we can.

Quoted 21 lines
Quoted 21 lines
fs/efivarfs/super.c +5 -5
 static int efivarfs_ops_notifier(struct notifier_block *nb, unsigned long event,
 				 void *data)
 {
@@ -82,15 +84,16 @@ static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	const u32 attr = EFI_VARIABLE_NON_VOLATILE |
 			 EFI_VARIABLE_BOOTSERVICE_ACCESS |
 			 EFI_VARIABLE_RUNTIME_ACCESS;
-	u64 storage_space, remaining_space, max_variable_size;
 	u64 id = huge_encode_dev(dentry->d_sb->s_dev);
+	u64 max_variable_size;
 	efi_status_t status;
 
 	/* Some UEFI firmware does not implement QueryVariableInfo() */
-	storage_space = remaining_space = 0;
-	if (efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
-		status = efivar_query_variable_info(attr, &storage_space,
-						    &remaining_space,
+	if (efivar_storage_space == 0 &&
+	    efivar_remaining_space == 0 &&
+	    efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
+		status = efivar_query_variable_info(attr, &efivar_storage_space,
Quoted 9 lines

fs/efivarfs/super.c static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf) · line 99

Quoted 4 lines
fs/efivarfs/super.c +1 -0
+						    &efivar_remaining_space,
 						    &max_variable_size);
 		if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED)
 			pr_warn_ratelimited("query_variable_info() failed: 0x%lx\n",

We've had issues with firmware in the past, shouldn't we be a bit more defensive here in case of errors? I'd reset both variables on error in case an incomplete write to one of the variables gives us an invalid state.

This is a pre-existing issue, right? The only difference being the fact that the variables are now globals?

Quoted 58 lines
Quoted 58 lines
fs/efivarfs/super.c +4 -4
@@ -104,8 +107,8 @@ static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	 */
 	buf->f_bsize	= 1;
 	buf->f_namelen	= NAME_MAX;
-	buf->f_blocks	= storage_space;
-	buf->f_bfree	= remaining_space;
+	buf->f_blocks	= efivar_storage_space;
+	buf->f_bfree	= efivar_remaining_space;
 	buf->f_type	= dentry->d_sb->s_magic;
 	buf->f_fsid	= u64_to_fsid(id);
 
@@ -114,8 +117,8 @@ static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	 * when the storage_paranoia x86 quirk is active. To use more, users
 	 * should boot the kernel with efi_no_storage_paranoia.
 	 */
-	if (remaining_space > efivar_reserved_space())
-		buf->f_bavail = remaining_space - efivar_reserved_space();
+	if (efivar_remaining_space > efivar_reserved_space())
+		buf->f_bavail = efivar_remaining_space - efivar_reserved_space();
 	else
 		buf->f_bavail = 0;
 
fs/efivarfs/vars.c +8 -0
index 6833c3d24b54..4196963695bd 100644
@@ -361,6 +361,11 @@ static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid,
 	kfree(str8);
 }
 
+static void efivar_invalidate_cached_storage_space(void)
+{
+	efivar_storage_space = efivar_remaining_space = 0;
+}
+
 /**
  * efivar_init - build the initial list of EFI variables
  * @func: callback function to invoke for every variable
@@ -450,6 +455,7 @@ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *),
 	} while (status != EFI_NOT_FOUND);
 
 	efivar_unlock();
+	efivar_invalidate_cached_storage_space();
 free:
 	kfree(variable_name);
 
@@ -480,6 +486,8 @@ int efivar_entry_delete(struct efivar_entry *entry)
 					    &entry->var.VendorGuid,
 					    0, 0, NULL, false);
 	efivar_unlock();
+	efivar_invalidate_cached_storage_space();
+
 	if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND))
 		return efi_status_to_err(status);
 
@@ -620,6 +628,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 				    NULL, size, NULL);
 
Quoted 7 lines

fs/efivarfs/vars.c int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, · line 631

Quoted 2 lines
fs/efivarfs/vars.c +1 -0
 	efivar_unlock();
+	efivar_invalidate_cached_storage_space();

In this function, the error path for efi_set_variable_locked (that could have triggered an EFI_OUT_OF_RESOURCES) is not covered by the invalidation.

Right. So under the assumption that the state of the EFI variable store might change even after a failed SetVariable(), the cached values may have become inaccurate. Is that what you are saying?

Quoted 11 lines

fs/efivarfs/vars.c int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, · line 634

Quoted 4 lines
fs/efivarfs/vars.c +0 -0
 	if (status && status != EFI_BUFFER_TOO_SMALL)
 		return efi_status_to_err(status);

Couldn't the variables be written outside of efivarfs as well? efivar_set_variable can be called from other code? pstore could maybe be ignored if it's only used during crashes (I'm not sure), but I see at least one other driver calling it as well. Wouldn't that change the available/free size?

Yeah, that is a very good point.

#4 lore raw

Hi,

Quoted 10 lines · Ard Biesheuvel

Hi,

Thanks for the review.

Quoted 5 lines · Anisse Astier

Hi Ard,

Please find a few comments below,

On Sat, Aug 01, 2026 at 04:42:59PM +0200, Ard Biesheuvel wrote:

[snip]

Quoted 14 lines
Quoted 14 lines

fs/efivarfs/super.c line 27

Quoted 10 lines
fs/efivarfs/super.c +2 -0
index 733c19571f1c..3691a2a087a0 100644
@@ -23,6 +23,8 @@
 #include "internal.h"
 #include "../internal.h"
 
+u64 efivar_storage_space, efivar_remaining_space;
+

Since those are globals (before they were only local to the function), I'd put them behind a lock now, probably efivar_lock, because we need something that's globally available.

Quoted 7 lines
Quoted 3 lines

statfs could be raced from userspace vs the rest of the code; and efivar_invalidate_cached_storage_space is only called outside the lock.

Right. So we might inadvertently report zero size and zero available space even though QueryVariableInfo() is supported and working correctly. Not the end of the world imo, but better avoided if we can.

Yes, I don't think it's that performance sensitive (famous last words), so putting it all behind a lock is a cheap way to avoid this type of issue (unless you have a better idea?).

Quoted 33 lines
Quoted 23 lines
Quoted 23 lines
fs/efivarfs/super.c +5 -5
 static int efivarfs_ops_notifier(struct notifier_block *nb, unsigned long event,
 				 void *data)
 {
@@ -82,15 +84,16 @@ static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	const u32 attr = EFI_VARIABLE_NON_VOLATILE |
 			 EFI_VARIABLE_BOOTSERVICE_ACCESS |
 			 EFI_VARIABLE_RUNTIME_ACCESS;
-	u64 storage_space, remaining_space, max_variable_size;
 	u64 id = huge_encode_dev(dentry->d_sb->s_dev);
+	u64 max_variable_size;
 	efi_status_t status;
 
 	/* Some UEFI firmware does not implement QueryVariableInfo() */
-	storage_space = remaining_space = 0;
-	if (efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
-		status = efivar_query_variable_info(attr, &storage_space,
-						    &remaining_space,
+	if (efivar_storage_space == 0 &&
+	    efivar_remaining_space == 0 &&
+	    efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
+		status = efivar_query_variable_info(attr, &efivar_storage_space,
Quoted 9 lines

fs/efivarfs/super.c static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf) · line 99

Quoted 4 lines
fs/efivarfs/super.c +1 -0
+						    &efivar_remaining_space,
 						    &max_variable_size);
 		if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED)
 			pr_warn_ratelimited("query_variable_info() failed: 0x%lx\n",

We've had issues with firmware in the past, shouldn't we be a bit more defensive here in case of errors? I'd reset both variables on error in case an incomplete write to one of the variables gives us an invalid state.

Quoted 2 lines

This is a pre-existing issue, right? The only difference being the fact that the variables are now globals?

Yes and no. Before, a bad value might be returned once. Now, it might get inadvertently cached and returned until the next variable modification.

Yes, this is an edge case, but I think it can be worked around very easily by either implementing the suggestion above, or simply writing the values to other temporary variables, and updating the global ones on success.

Quoted 20 lines
Quoted 12 lines
Quoted 12 lines
fs/efivarfs/super.c +2 -0
@@ -480,6 +486,8 @@ int efivar_entry_delete(struct efivar_entry *entry)
 					    &entry->var.VendorGuid,
 					    0, 0, NULL, false);
 	efivar_unlock();
+	efivar_invalidate_cached_storage_space();
+
 	if (!(status == EFI_SUCCESS || status == EFI_NOT_FOUND))
 		return efi_status_to_err(status);
 
@@ -620,6 +628,7 @@ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes,
 				    NULL, size, NULL);
 
Quoted 7 lines

fs/efivarfs/super.c int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, · line 631

Quoted 2 lines
fs/efivarfs/super.c +1 -0
 	efivar_unlock();
+	efivar_invalidate_cached_storage_space();

In this function, the error path for efi_set_variable_locked (that could have triggered an EFI_OUT_OF_RESOURCES) is not covered by the invalidation.

Quoted 3 lines

Right. So under the assumption that the state of the EFI variable store might change even after a failed SetVariable(), the cached values may have become inaccurate. Is that what you are saying?

Yes, the actual free space might change but failed to be updated.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
fs/efivarfs/super.c +0 -0
 	if (status && status != EFI_BUFFER_TOO_SMALL)
 		return efi_status_to_err(status);
Quoted 9 lines
Quoted 6 lines

Couldn't the variables be written outside of efivarfs as well? efivar_set_variable can be called from other code? pstore could maybe be ignored if it's only used during crashes (I'm not sure), but I see at least one other driver calling it as well. Wouldn't that change the available/free size?

Yeah, that is a very good point.

FYI, since this patch was initially merged, it helped us catch errors in production *twice* thanks to existing monitoring on filesystem free
space: bad configurations or logic that wrote too many variables.

I can also see why this type of regular monitoring triggering SMM rendez-vous of all CPUs might be bad for performance.

Regards,

Anisse