Skip to contentSkip to conversation

Improve fatal error handling implementation

PATCH New Jessica Clarke 3 participants 3 messages Nov 21, 2021 – Nov 22, 2021 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Improve fatal error handling implementation

BUG and BUG_ON are not informative and are rather lazy interfaces, onnly telling the user that something went wrong in a given function, but not what, requiring the user to find the sources corresponding to their firmware (which may not be available) and figure out how that BUG(_ON) was hit. Even SBI_ASSERT in its current form, which does include the condition that triggered it in the output, isn't necessarily very informative. In some cases, the error may be fixable by the user, but they need to know the problem in order to have any hope of fixing it. It's also a nuisance for developers, whose development trees may have changed significantly since the release in question being used, and so line numbers can make it harder for them to understand which error case a user has hit.

This patch introduces a new sbi_panic function which is printf-like, allowing detailed error messages to be printed to the console. BUG and BUG_ON are removed, since the former is just a worse form of sbi_panic and the latter is a worse version of SBI_ASSERT. Finally, SBI_ASSERT is augmented to take a set of arguments to pass to sbi_panic on failure, used like so (sbi_boot_print_hart's current error case, which currently manually calls sbi_printf and sbi_hart_hang):

  SBI_ASSERT(xlen >= 1, ("Error %d getting MISA XLEN\n", xlen));

The existing users of BUG are replaced with calls to sbi_panic along with informative error messages. BUG_ON and SBI_ASSERT were unused (and, in the case of SBI_ASSERT, remain unused).

Many existing users of sbi_hart_hang should be converted to use either sbi_panic or SBI_ASSERT after this commit.

---
 include/sbi/sbi_console.h | 22 +++++-----------------
 lib/sbi/riscv_asm.c       |  7 ++++---
 lib/sbi/sbi_console.c     | 13 +++++++++++++
 3 files changed, 22 insertions(+), 20 deletions(-)
include/sbi/sbi_console.h +5 -17
index 28b4a79..db35a2f 100644
@@ -44,6 +44,8 @@ int __printf(1, 2) sbi_printf(const char *format, ...);
 
 int __printf(1, 2) sbi_dprintf(const char *format, ...);
 
+void __printf(1, 2) __attribute__((noreturn)) sbi_panic(const char *format, ...);
+
 const struct sbi_console_device *sbi_console_get_device(void);
 
 void sbi_console_set_device(const struct sbi_console_device *dev);
@@ -52,23 +54,9 @@ struct sbi_scratch;
 
 int sbi_console_init(struct sbi_scratch *scratch);
 
-#define BUG() do { \
-	sbi_printf("BUG: failure at %s:%d/%s()!\n", \
-		   __FILE__, __LINE__, __func__); \
-	sbi_hart_hang(); \
-} while (0)
-
-#define BUG_ON(cond) do { \
-	if (cond)	\
-		BUG();	\
-} while (0)
-
-#define SBI_ASSERT(cond) do { \
-	if (!(cond)) { \
-		sbi_printf("ASSERT: %s:%d/%s(): Assertion `%s` failed.\n", \
-			   __FILE__,__LINE__,__func__, #cond);\
-		sbi_hart_hang(); \
-	} \
+#define SBI_ASSERT(cond, args) do { \
+	if (unlikely(!(cond))) \
+		sbi_panic args; \
 } while (0)
 
 #endif
lib/sbi/riscv_asm.c +4 -3
index 1642ac6..2e2e148 100644
@@ -76,7 +76,8 @@ void misa_string(int xlen, char *out, unsigned int out_sz)
 			out[pos++] = '8';
 			break;
 		default:
-			BUG();
+			sbi_panic("%s: Unknown misa.MXL encoding %d",
+				   __func__, xlen);
 			return;
 		}
 	}
@@ -145,7 +146,7 @@ unsigned long csr_read_num(int csr_num)
 #endif
 
 	default:
-		BUG();
+		sbi_panic("%s: Unknown CSR %#x", __func__, csr_num);
 		break;
 	};
 
@@ -213,7 +214,7 @@ void csr_write_num(int csr_num, unsigned long val)
 	switchcase_csr_write_16(CSR_MHPMEVENT16, val)
 
 	default:
-		BUG();
+		sbi_panic("%s: Unknown CSR %#x", __func__, csr_num);
 		break;
 	};
 
lib/sbi/sbi_console.c +13 -0
index 29eede3..15fad1e 100644
@@ -397,6 +397,19 @@ int sbi_dprintf(const char *format, ...)
 	return retval;
 }
 
+void sbi_panic(const char *format, ...)
+{
+	va_list args;
+
+	spin_lock(&console_out_lock);
+	va_start(args, format);
+	print(NULL, NULL, format, args);
+	va_end(args);
+	spin_unlock(&console_out_lock);
+
+	sbi_hart_hang();
+}
+
 const struct sbi_console_device *sbi_console_get_device(void)
 {
 	return console_dev;
Signature · 1 line
2.33.1
#1 lore raw

PATCH 1/1 Improve fatal error handling implementation

On Sun, Nov 21, 2021 at 11:00 PM Jessica Clarke <[email protected]> wrote:

Slightly better PATCH subject can be:
"lib: sbi: Improve fatal error handling"

Quoted 2 lines

BUG and BUG_ON are not informative and are rather lazy interfaces, onnly

s/onnly/only

Quoted 64 lines

telling the user that something went wrong in a given function, but not what, requiring the user to find the sources corresponding to their firmware (which may not be available) and figure out how that BUG(_ON) was hit. Even SBI_ASSERT in its current form, which does include the condition that triggered it in the output, isn't necessarily very informative. In some cases, the error may be fixable by the user, but they need to know the problem in order to have any hope of fixing it. It's also a nuisance for developers, whose development trees may have changed significantly since the release in question being used, and so line numbers can make it harder for them to understand which error case a user has hit.

This patch introduces a new sbi_panic function which is printf-like, allowing detailed error messages to be printed to the console. BUG and BUG_ON are removed, since the former is just a worse form of sbi_panic and the latter is a worse version of SBI_ASSERT. Finally, SBI_ASSERT is augmented to take a set of arguments to pass to sbi_panic on failure, used like so (sbi_boot_print_hart's current error case, which currently manually calls sbi_printf and sbi_hart_hang):

  SBI_ASSERT(xlen >= 1, ("Error %d getting MISA XLEN\n", xlen));

The existing users of BUG are replaced with calls to sbi_panic along with informative error messages. BUG_ON and SBI_ASSERT were unused (and, in the case of SBI_ASSERT, remain unused).

Many existing users of sbi_hart_hang should be converted to use either sbi_panic or SBI_ASSERT after this commit.

---
 include/sbi/sbi_console.h | 22 +++++-----------------
 lib/sbi/riscv_asm.c       |  7 ++++---
 lib/sbi/sbi_console.c     | 13 +++++++++++++
 3 files changed, 22 insertions(+), 20 deletions(-)
include/sbi/sbi_console.h +2 -11
index 28b4a79..db35a2f 100644
@@ -44,6 +44,8 @@ int __printf(1, 2) sbi_printf(const char *format, ...);
 
 int __printf(1, 2) sbi_dprintf(const char *format, ...);
 
+void __printf(1, 2) __attribute__((noreturn)) sbi_panic(const char *format, ...);
+
 const struct sbi_console_device *sbi_console_get_device(void);
 
 void sbi_console_set_device(const struct sbi_console_device *dev);
@@ -52,23 +54,9 @@ struct sbi_scratch;
 
 int sbi_console_init(struct sbi_scratch *scratch);
 
-#define BUG() do { \
-       sbi_printf("BUG: failure at %s:%d/%s()!\n", \
-                  __FILE__, __LINE__, __func__); \
-       sbi_hart_hang(); \
-} while (0)
-
-#define BUG_ON(cond) do { \
-       if (cond)       \
-               BUG();  \
-} while (0)
-

include/sbi/sbi_console.h struct sbi_scratch; · line 60

Quoted 10 lines
include/sbi/sbi_console.h +3 -6
-#define SBI_ASSERT(cond) do { \
-       if (!(cond)) { \
-               sbi_printf("ASSERT: %s:%d/%s(): Assertion `%s` failed.\n", \
-                          __FILE__,__LINE__,__func__, #cond);\
-               sbi_hart_hang(); \
-       } \
+#define SBI_ASSERT(cond, args) do { \
+       if (unlikely(!(cond))) \
+               sbi_panic args; \
 } while (0)

Remove the unwanted "#include <sbi/sbi_hart.h>" from here as well and if required add it in sbi_console.c

Quoted 62 lines
include/sbi/sbi_console.h +0 -0
 #endif
lib/sbi/riscv_asm.c +4 -3
index 1642ac6..2e2e148 100644
@@ -76,7 +76,8 @@ void misa_string(int xlen, char *out, unsigned int out_sz)
                        out[pos++] = '8';
                        break;
                default:
-                       BUG();
+                       sbi_panic("%s: Unknown misa.MXL encoding %d",
+                                  __func__, xlen);
                        return;
                }
        }
@@ -145,7 +146,7 @@ unsigned long csr_read_num(int csr_num)
 #endif
 
        default:
-               BUG();
+               sbi_panic("%s: Unknown CSR %#x", __func__, csr_num);
                break;
        };
 
@@ -213,7 +214,7 @@ void csr_write_num(int csr_num, unsigned long val)
        switchcase_csr_write_16(CSR_MHPMEVENT16, val)
 
        default:
-               BUG();
+               sbi_panic("%s: Unknown CSR %#x", __func__, csr_num);
                break;
        };
 
lib/sbi/sbi_console.c +13 -1
index 29eede3..15fad1e 100644
@@ -397,6 +397,19 @@ int sbi_dprintf(const char *format, ...)
        return retval;
 }
 
+void sbi_panic(const char *format, ...)
+{
+       va_list args;
+
+       spin_lock(&console_out_lock);
+       va_start(args, format);
+       print(NULL, NULL, format, args);
+       va_end(args);
+       spin_unlock(&console_out_lock);
+
+       sbi_hart_hang();
+}
+
 const struct sbi_console_device *sbi_console_get_device(void)
 {
        return console_dev;
--

2.33.1

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Apart from minor comments above, this looks good to me.

Thanks,
Anup

#2 lore raw

PATCH 1/1 Improve fatal error handling implementation

In reply to Jessica Clarke

? 2021-11-21???? 17:30 +0000?Jessica Clarke???

Quoted 37 lines

BUG and BUG_ON are not informative and are rather lazy interfaces, onnly
telling the user that something went wrong in a given function, but not
what, requiring the user to find the sources corresponding to their firmware (which may not be available) and figure out how that BUG(_ON)
was hit. Even SBI_ASSERT in its current form, which does include the condition that triggered it in the output, isn't necessarily very informative. In some cases, the error may be fixable by the user, but they need to know the problem in order to have any hope of fixing it. It's also a nuisance for developers, whose development trees may have changed significantly since the release in question being used, and so
line numbers can make it harder for them to understand which error case
a user has hit.

This patch introduces a new sbi_panic function which is printf-like, allowing detailed error messages to be printed to the console. BUG and
BUG_ON are removed, since the former is just a worse form of sbi_panic
and the latter is a worse version of SBI_ASSERT. Finally, SBI_ASSERT is
augmented to take a set of arguments to pass to sbi_panic on failure, used like so (sbi_boot_print_hart's current error case, which currently
manually calls sbi_printf and sbi_hart_hang):

? SBI_ASSERT(xlen >= 1, ("Error %d getting MISA XLEN\n", xlen));

The existing users of BUG are replaced with calls to sbi_panic along with informative error messages. BUG_ON and SBI_ASSERT were unused (and,
in the case of SBI_ASSERT, remain unused).

Quoted 5 lines

Many existing users of sbi_hart_hang should be converted to use either
sbi_panic or SBI_ASSERT after this commit.

Look good to me.

Quoted 111 lines

---

?include/sbi/sbi_console.h | 22 +++++-----------------
?lib/sbi/riscv_asm.c?????? |? 7 ++++---
?lib/sbi/sbi_console.c???? | 13 +++++++++++++

?3 files changed, 22 insertions(+), 20 deletions(-)

include/sbi/sbi_console.h +0 -0
index 28b4a79..db35a2f 100644
@@ -44,6 +44,8 @@ int __printf(1, 2) sbi_printf(const char *format,
 ...);

?
?int __printf(1, 2) sbi_dprintf(const char *format, ...); ?
+void __printf(1, 2) __attribute__((noreturn)) sbi_panic(const char *format, ...);
+
?const struct sbi_console_device *sbi_console_get_device(void); ?
?void sbi_console_set_device(const struct sbi_console_device *dev);

include/sbi/sbi_console.h +0 -0
@@ -52,23 +54,9 @@ struct sbi_scratch;

?
?int sbi_console_init(struct sbi_scratch *scratch); ?

include/sbi/sbi_console.h +0 -14
-#define BUG() do { \
-???????sbi_printf("BUG: failure at %s:%d/%s()!\n", \
-????????????????? __FILE__, __LINE__, __func__); \
-???????sbi_hart_hang(); \
-} while (0)
-
-#define BUG_ON(cond) do { \
-???????if (cond)???????\
-???????????????BUG();??\
-} while (0)
-
-#define SBI_ASSERT(cond) do { \
-???????if (!(cond)) { \
-???????????????sbi_printf("ASSERT: %s:%d/%s(): Assertion `%s`

failed.\n", \
-????????????????????????? __FILE__,__LINE__,__func__, #cond);\
-???????????????sbi_hart_hang(); \
-???????} \
+#define SBI_ASSERT(cond, args) do { \
+???????if (unlikely(!(cond))) \
+???????????????sbi_panic args; \
?} while (0)
?
?#endif

lib/sbi/riscv_asm.c +0 -0
index 1642ac6..2e2e148 100644
@@ -76,7 +76,8 @@ void misa_string(int xlen, char *out, unsigned int
 out_sz)
 ????????????????????????out[pos++] = '8';
 ????????????????????????break;

????????????????default:
-???????????????????????BUG(); +???????????????????????sbi_panic("%s: Unknown misa.MXL encoding %d", +????????????????????????????????? __func__, xlen); ????????????????????????return;
????????????????}
????????}

lib/sbi/riscv_asm.c +0 -0
@@ -145,7 +146,7 @@ unsigned long csr_read_num(int csr_num)

?#endif
?
????????default:
-???????????????BUG();
+???????????????sbi_panic("%s: Unknown CSR %#x", __func__, csr_num); ????????????????break;
????????};
?

lib/sbi/riscv_asm.c +0 -0
@@ -213,7 +214,7 @@ void csr_write_num(int csr_num, unsigned long
 val)
 ????????switchcase_csr_write_16(CSR_MHPMEVENT16, val)

?
????????default:
-???????????????BUG();
+???????????????sbi_panic("%s: Unknown CSR %#x", __func__, csr_num); ????????????????break;
????????};
?

lib/sbi/sbi_console.c +0 -0
index 29eede3..15fad1e 100644
@@ -397,6 +397,19 @@ int sbi_dprintf(const char *format, ...)
 ????????return retval;
 ?}

?

lib/sbi/sbi_console.c +13 -0
+void sbi_panic(const char *format, ...)
+{
+???????va_list args;
+
+???????spin_lock(&console_out_lock);
+???????va_start(args, format);
+???????print(NULL, NULL, format, args);
+???????va_end(args);
+???????spin_unlock(&console_out_lock);
+
+???????sbi_hart_hang();
+}
+

?const struct sbi_console_device *sbi_console_get_device(void) ?{
????????return console_dev;

Signature · 1 line
2.33.1