Skip to contentSkip to conversation

x86/mce: /dev/mcelog: Dynamically allocate space for machine check records

PATCH Superseded Tony Luck 3 participants 5 messages Feb 8, 2020 – Mar 10, 2020

Subsystems (2)
lore
Share & tools
lore

Tony Luck

author maintainer
#0 lore raw

PATCH 1/1 x86/mce: /dev/mcelog: Dynamically allocate space for machine check records

We have had a hard coded limit of 32 machine check records since the dawn of time. But as numbers of cores increase, it is possible for more than 32 errors to be reported before a user process reads from /dev/mcelog. In this case the additional errors are lost.

Keep 32 as the minimum. But tune the maximum value up based on the number of processors.

---
 arch/x86/include/asm/mce.h           |  6 ++--
 arch/x86/kernel/cpu/mce/dev-mcelog.c | 46 ++++++++++++++++------------
 2 files changed, 29 insertions(+), 23 deletions(-)
arch/x86/include/asm/mce.h +3 -3
index 4359b955e0b7..c9411a3dcdbe 100644
@@ -102,7 +102,7 @@
 
 #define MCE_OVERFLOW 0		/* bit 0 in flags means overflow */
 
-#define MCE_LOG_LEN 32
+#define MCE_LOG_MIN_LEN 32
 #define MCE_LOG_SIGNATURE	"MACHINECHECK"
 
 /* AMD Scalable MCA */
@@ -135,11 +135,11 @@
  */
 struct mce_log_buffer {
 	char signature[12]; /* "MACHINECHECK" */
-	unsigned len;	    /* = MCE_LOG_LEN */
+	unsigned len;	    /* = elements in .mce_entry[] */
 	unsigned next;
 	unsigned flags;
 	unsigned recordlen;	/* length of struct mce */
-	struct mce entry[MCE_LOG_LEN];
+	struct mce entry[];
 };
 
 enum mce_notifier_prios {
arch/x86/kernel/cpu/mce/dev-mcelog.c +26 -20

111 lines not on this page · raw message

Signature · 1 line
2.21.1
author maintainer
#1 lore raw
Quoted 6 lines · Tony Luck

We have had a hard coded limit of 32 machine check records since the dawn of time. But as numbers of cores increase, it is possible for more than 32 errors to be reported

Note that this is not a theoretical issue. Validation team is hitting this in a case that doesn’t look completely implausible.

-Tony

maintainer
#2 lore raw
In reply to Tony Luck
Quoted 13 lines · Tony Luck

We have had a hard coded limit of 32 machine check records since the dawn of time. But as numbers of cores increase, it is possible for more than 32 errors to be reported before a user process reads from /dev/mcelog. In this case the additional errors are lost.

Keep 32 as the minimum. But tune the maximum value up based on the number of processors.

---
 arch/x86/include/asm/mce.h           |  6 ++--
 arch/x86/kernel/cpu/mce/dev-mcelog.c | 46 ++++++++++++++++------------
 2 files changed, 29 insertions(+), 23 deletions(-)

...

arch/x86/kernel/cpu/mce/dev-mcelog.c static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf, · line 213

Quoted 6 lines
arch/x86/kernel/cpu/mce/dev-mcelog.c +1 -1
@@ -214,21 +210,21 @@ static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
 
 	/* Only supports full reads right now */
 	err = -EINVAL;
-	if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
+	if (*off != 0 || usize < mcelog->len*sizeof(struct mce))

Add spaces around that *

Quoted 21 lines
arch/x86/kernel/cpu/mce/dev-mcelog.c +4 -4
 		goto out;
 
-	next = mcelog.next;
+	next = mcelog->next;
 	err = 0;
 
 	for (i = 0; i < next; i++) {
-		struct mce *m = &mcelog.entry[i];
+		struct mce *m = &mcelog->entry[i];
 
 		err |= copy_to_user(buf, m, sizeof(*m));
 		buf += sizeof(*m);
 	}
 
-	memset(mcelog.entry, 0, next * sizeof(struct mce));
-	mcelog.next = 0;
+	memset(mcelog->entry, 0, next * sizeof(struct mce));
+	mcelog->next = 0;
 
 	if (err)
 		err = -EFAULT;

...

arch/x86/kernel/cpu/mce/dev-mcelog.c static struct miscdevice mce_chrdev_device = { · line 339

Quoted 5 lines
arch/x86/kernel/cpu/mce/dev-mcelog.c +1 -0
@@ -340,6 +336,15 @@ static struct miscdevice mce_chrdev_device = {
 static __init int dev_mcelog_init_device(void)
 {
 	int err;
+	int mce_log_len;

Please sort function local variables declaration in a reverse christmas tree order:

	<type A> longest_variable_name;
	<type B> shorter_var_name;
	<type C> even_shorter;
	<type D> i;

arch/x86/kernel/cpu/mce/dev-mcelog.c static struct miscdevice mce_chrdev_device = { · line 341

Quoted 2 lines
arch/x86/kernel/cpu/mce/dev-mcelog.c +2 -0
+
+	mce_log_len = max(MCE_LOG_MIN_LEN, num_online_cpus());

arch/x86/kernel/cpu/mce/dev-mcelog.c: In function ‘dev_mcelog_init_device’:
./include/linux/kernel.h:835:29: warning: comparison of distinct pointer types lacks a cast
835 | (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
| ^~
./include/linux/kernel.h:849:4: note: in expansion of macro ‘__typecheck’
849 | (__typecheck(x, y) && __no_side_effects(x, y))

      |    ^~~~~~~~~~~
./include/linux/kernel.h:859:24: note: in expansion of macro ‘__safe_cmp’
  859 |  __builtin_choose_expr(__safe_cmp(x, y), \
      |                        ^~~~~~~~~~
./include/linux/kernel.h:875:19: note: in expansion of macro ‘__careful_cmp’
  875 | #define max(x, y) __careful_cmp(x, y, >)
      |                   ^~~~~~~~~~~~~

arch/x86/kernel/cpu/mce/dev-mcelog.c:341:16: note: in expansion of macro ‘max’
341 | mce_log_len = max(MCE_LOG_MIN_LEN, num_online_cpus());
| ^~~

That MCE_LOG_MIN_LEN wants to be 32U.

arch/x86/kernel/cpu/mce/dev-mcelog.c static struct miscdevice mce_chrdev_device = { · line 344

Quoted 3 lines
arch/x86/kernel/cpu/mce/dev-mcelog.c +3 -0
+	mcelog = kzalloc(sizeof(*mcelog) + mce_log_len * sizeof(struct mce), GFP_KERNEL);
+	if (!mcelog)
+		return -ENOMEM;

<---- newline here.

arch/x86/kernel/cpu/mce/dev-mcelog.c static struct miscdevice mce_chrdev_device = { · line 350

Quoted 6 lines
arch/x86/kernel/cpu/mce/dev-mcelog.c +3 -0
+	strncpy(mcelog->signature, MCE_LOG_SIGNATURE, sizeof(mcelog->signature));
+	mcelog->len = mce_log_len;
+	mcelog->recordlen = sizeof(struct mce);
 
 	/* register character device /dev/mcelog */
 	err = misc_register(&mce_chrdev_device);

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#3 lore raw

PATCH v2 1/1 x86/mce: /dev/mcelog: Dynamically allocate space for machine check records

We have had a hard coded limit of 32 machine check records since the dawn of time. But as numbers of cores increase, it is possible for more than 32 errors to be reported before a user process reads from /dev/mcelog. In this case the additional errors are lost.

Keep 32 as the minimum. But tune the maximum value up based on the number of processors.

---
v2: Fix comments from Boris
1) Spaces around a multiply '*' operator
2) Reverse Christmas tree order for local variables
3) MCE_LOG_MIN_LEN need to be unsigned for use by max()
4) Missing blank line

 arch/x86/include/asm/mce.h           |  6 ++--
 arch/x86/kernel/cpu/mce/dev-mcelog.c | 47 ++++++++++++++++------------
 2 files changed, 30 insertions(+), 23 deletions(-)
arch/x86/include/asm/mce.h +3 -3
index 4359b955e0b7..9d5b09913ef3 100644
@@ -102,7 +102,7 @@
 
 #define MCE_OVERFLOW 0		/* bit 0 in flags means overflow */
 
-#define MCE_LOG_LEN 32
+#define MCE_LOG_MIN_LEN 32U
 #define MCE_LOG_SIGNATURE	"MACHINECHECK"
 
 /* AMD Scalable MCA */
@@ -135,11 +135,11 @@
  */
 struct mce_log_buffer {
 	char signature[12]; /* "MACHINECHECK" */
-	unsigned len;	    /* = MCE_LOG_LEN */
+	unsigned len;	    /* = elements in .mce_entry[] */
 	unsigned next;
 	unsigned flags;
 	unsigned recordlen;	/* length of struct mce */
-	struct mce entry[MCE_LOG_LEN];
+	struct mce entry[];
 };
 
 enum mce_notifier_prios {
arch/x86/kernel/cpu/mce/dev-mcelog.c +27 -20

114 lines not on this page · raw message

Signature · 1 line
2.21.1
merge bot Applied
#4 lore raw

[tip: ras/core] x86/mce/dev-mcelog: Dynamically allocate space for machine check records

The following commit has been merged into the ras/core branch of tip:

The following commit has been merged into the ras/core branch of tip:

Commit-ID:     d8ecca4043f2d9d89daab7915eca8c2ec6254d0f
Gitweb:        https://git.kernel.org/tip/d8ecca4043f2d9d89daab7915eca8c2ec6254d0f
Author:        Tony Luck <[email protected]>
AuthorDate:    Tue, 18 Feb 2020 10:44:08 -08:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Tue, 10 Mar 2020 10:25:14 +01:00

x86/mce/dev-mcelog: Dynamically allocate space for machine check records

We have had a hard coded limit of 32 machine check records since the dawn of time. But as numbers of cores increase, it is possible for more than 32 errors to be reported before a user process reads from /dev/mcelog. In this case the additional errors are lost.

Keep 32 as the minimum. But tune the maximum value up based on the number of processors.

---
 arch/x86/include/asm/mce.h           |  6 +--
 arch/x86/kernel/cpu/mce/dev-mcelog.c | 47 +++++++++++++++------------
 2 files changed, 30 insertions(+), 23 deletions(-)
arch/x86/include/asm/mce.h +3 -3
index 4359b95..9d5b099 100644
@@ -102,7 +102,7 @@
 
 #define MCE_OVERFLOW 0		/* bit 0 in flags means overflow */
 
-#define MCE_LOG_LEN 32
+#define MCE_LOG_MIN_LEN 32U
 #define MCE_LOG_SIGNATURE	"MACHINECHECK"
 
 /* AMD Scalable MCA */
@@ -135,11 +135,11 @@
  */
 struct mce_log_buffer {
 	char signature[12]; /* "MACHINECHECK" */
-	unsigned len;	    /* = MCE_LOG_LEN */
+	unsigned len;	    /* = elements in .mce_entry[] */
 	unsigned next;
 	unsigned flags;
 	unsigned recordlen;	/* length of struct mce */
-	struct mce entry[MCE_LOG_LEN];
+	struct mce entry[];
 };
 
 enum mce_notifier_prios {
arch/x86/kernel/cpu/mce/dev-mcelog.c +27 -20

114 lines not on this page · raw message