Skip to contentSkip to conversation

xen: run_in_exception_handler() cleanup

PATCH In review Andrew Cooper 3 participants 8 messages Dec 15, 2023 – Dec 20, 2023

lore
Share & tools
lore
#0 lore raw

cover xen: run_in_exception_handler() cleanup

Cleanup from looking at "[XEN RFC] xen/bug: introduce bug_fn_nonconst_t to validate run_in_exception_handle()". Not really relevant to the MISRA side of things.

Gitlab run:
https://gitlab.com/xen-project/people/andyhhp/xen/-/pipelines/1108032069

Andrew Cooper (2):
driver/char: Drop run_in_exception_handler() ifdefary xen/bug: Complete outstanding TODO

 xen/drivers/char/ehci-dbgp.c |  4 ----
 xen/drivers/char/ns16550.c   |  4 ----
 xen/include/xen/bug.h        | 16 +++++++---------
 3 files changed, 7 insertions(+), 17 deletions(-)

base-commit: cad6f9a4c12dd4d5cdb2620e3fe24727ee81c7ce

Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/2 driver/char: Drop run_in_exception_handler() ifdefary

Since commit 60a9b0715055 ("xen: introduce CONFIG_GENERIC_BUG_FRAME"), the new common bug.h provides an implementation of run_in_exception_handler() in all cases, making the #else here dead.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Federico Serafini <[email protected]>

---
 xen/drivers/char/ehci-dbgp.c | 4 ----
 xen/drivers/char/ns16550.c   | 4 ----
 2 files changed, 8 deletions(-)
xen/drivers/char/ehci-dbgp.c +0 -4
index 0f5b41899ef5..bbf4e51c0263 100644
@@ -1293,11 +1293,7 @@ static void cf_check _ehci_dbgp_poll(struct cpu_user_regs *regs)
 static void cf_check ehci_dbgp_poll(void *data)
 {
     poll_port = data;
-#ifdef run_in_exception_handler
     run_in_exception_handler(_ehci_dbgp_poll);
-#else
-    _ehci_dbgp_poll(guest_cpu_user_regs());
-#endif
 }
 
 static bool ehci_dbgp_setup_preirq(struct ehci_dbgp *dbgp)
xen/drivers/char/ns16550.c +0 -4
index c8a51ed66008..29691e8d3ae6 100644
@@ -233,11 +233,7 @@ static void cf_check __ns16550_poll(struct cpu_user_regs *regs)
 static void cf_check ns16550_poll(void *data)
 {
     this_cpu(poll_port) = data;
-#ifdef run_in_exception_handler
     run_in_exception_handler(__ns16550_poll);
-#else
-    __ns16550_poll(guest_cpu_user_regs());
-#endif
 }
 
 static int cf_check ns16550_tx_ready(struct serial_port *port)
Signature · 1 line
2.30.2
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] driver/char: Drop run_in_exception_handler() ifdefary

Hi Andrew,

Quoted 3 lines · Andrew Cooper

Since commit 60a9b0715055 ("xen: introduce CONFIG_GENERIC_BUG_FRAME"), the new common bug.h provides an implementation of run_in_exception_handler() in all cases, making the #else here dead.

This reminds me, I need to look at using CONFIG_GENERIC_BUG_FRAME for Arm. In the past, we had some issues compiling it.

Quoted 2 lines

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw

PATCH 2/2 xen/bug: Complete outstanding TODO

In reply to Andrew Cooper

Since this TODO was written, BUILD_BUG_ON() has been moved out of xen/lib.h into xen/macros.h, which has done all the hard work.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Federico Serafini <[email protected]>

---
 xen/include/xen/bug.h | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
xen/include/xen/bug.h +7 -9
index cb5138410ea7..8cca4486a477 100644
@@ -20,7 +20,8 @@
 #define BUG_DEBUGGER_TRAP_FATAL(regs) 0
 #endif
 
-#include <xen/lib.h>
+#include <xen/macros.h>
+#include <xen/types.h>
 
 #ifndef BUG_FRAME_STRUCT
 
@@ -104,14 +105,11 @@ typedef void bug_fn_t(const struct cpu_user_regs *regs);
 
 #ifndef run_in_exception_handler
 
-/*
- * TODO: untangle header dependences, break BUILD_BUG_ON() out of xen/lib.h,
- * and use a real static inline here to get proper type checking of fn().
- */
-#define run_in_exception_handler(fn) do {                   \
-    (void)((fn) == (void (*)(struct cpu_user_regs *))NULL); \
-    BUG_FRAME(BUGFRAME_run_fn, 0, fn, 0, NULL);             \
-} while ( false )
+static void always_inline run_in_exception_handler(
+    void (*fn)(struct cpu_user_regs *regs))
+{
+    BUG_FRAME(BUGFRAME_run_fn, 0, fn, 0, NULL);
+}
 
 #endif /* run_in_exception_handler */
 
Signature · 1 line
2.30.2
#5 lore raw

Re: [PATCH 2/2] xen/bug: Complete outstanding TODO

Hi Andrew,

Quoted 33 lines · Andrew Cooper

Since this TODO was written, BUILD_BUG_ON() has been moved out of xen/lib.h into xen/macros.h, which has done all the hard work.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Federico Serafini <[email protected]>

---
  xen/include/xen/bug.h | 16 +++++++---------
  1 file changed, 7 insertions(+), 9 deletions(-)
xen/include/xen/bug.h +2 -1
index cb5138410ea7..8cca4486a477 100644
@@ -20,7 +20,8 @@
  #define BUG_DEBUGGER_TRAP_FATAL(regs) 0
  #endif
 
-#include <xen/lib.h>
+#include <xen/macros.h>
+#include <xen/types.h>
 
  #ifndef BUG_FRAME_STRUCT
 
@@ -104,14 +105,11 @@ typedef void bug_fn_t(const struct cpu_user_regs *regs);
 
  #ifndef run_in_exception_handler
 

xen/include/xen/bug.h typedef void bug_fn_t(const struct cpu_user_regs *regs); · line 109

Quoted 10 lines
xen/include/xen/bug.h +2 -8
-/*
- * TODO: untangle header dependences, break BUILD_BUG_ON() out of xen/lib.h,
- * and use a real static inline here to get proper type checking of fn().
- */
-#define run_in_exception_handler(fn) do {                   \
-    (void)((fn) == (void (*)(struct cpu_user_regs *))NULL); \
-    BUG_FRAME(BUGFRAME_run_fn, 0, fn, 0, NULL);             \
-} while ( false )
+static void always_inline run_in_exception_handler(
+    void (*fn)(struct cpu_user_regs *regs))

Based on the other threads, shouldn't this be using bug_fn_t?

Also coding style NIT: I was under the impression we would format prototype like that:

static void always_inline run_in_exception_handler(void (*fn)(struct cpu_user_regs *regs))

Anyway, that comment would be moot if we were using bug_fn_t.

Cheers,

Signature · 1 line
Julien Grall
Reviewed-by
#6 lore raw

Re: [PATCH 2/2] xen/bug: Complete outstanding TODO

Quoted 37 lines · Julien Grall

Hi Andrew,

Quoted 34 lines · Andrew Cooper

Since this TODO was written, BUILD_BUG_ON() has been moved out of xen/lib.h into xen/macros.h, which has done all the hard work.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Federico Serafini <[email protected]>

---
  xen/include/xen/bug.h | 16 +++++++---------
  1 file changed, 7 insertions(+), 9 deletions(-)
xen/include/xen/bug.h +2 -1
index cb5138410ea7..8cca4486a477 100644
@@ -20,7 +20,8 @@
  #define BUG_DEBUGGER_TRAP_FATAL(regs) 0
  #endif
  -#include <xen/lib.h>
+#include <xen/macros.h>
+#include <xen/types.h>
    #ifndef BUG_FRAME_STRUCT
  @@ -104,14 +105,11 @@ typedef void bug_fn_t(const struct cpu_user_regs
 *regs);
    #ifndef run_in_exception_handler
  -/*
- * TODO: untangle header dependences, break BUILD_BUG_ON() out of

xen/lib.h,
- * and use a real static inline here to get proper type checking of fn().
- */

Quoted 8 lines

xen/include/xen/bug.h typedef void bug_fn_t(const struct cpu_user_regs *regs); · line 109

Quoted 6 lines
xen/include/xen/bug.h +2 -4
-#define run_in_exception_handler(fn) do {                   \
-    (void)((fn) == (void (*)(struct cpu_user_regs *))NULL); \
-    BUG_FRAME(BUGFRAME_run_fn, 0, fn, 0, NULL);             \
-} while ( false )
+static void always_inline run_in_exception_handler(
+    void (*fn)(struct cpu_user_regs *regs))

Based on the other threads, shouldn't this be using bug_fn_t?

Unfortunately it doesn't compile:

common/bug.c: In function ‘do_bug_frame’:
common/bug.c:72:38: error: passing argument 1 of ‘run_in_exception_handler’ from incompatible pointer type [-Werror=incompatible-pointer-types]

   72 |             run_in_exception_handler(fn);
      |                                      ^~
      |                                      |
      |                                      void (*)(struct cpu_user_regs *)

due to the missing const in common/bug.c:72.

Not to make things more complicated in this patch, I think we should take the patch as is as a simple cleanup (and do further cleanups in the future):

#7 lore raw

Re: [PATCH 2/2] xen/bug: Complete outstanding TODO

Hi Stefano,

Quoted 56 lines · Stefano Stabellini
Quoted 37 lines · Julien Grall

Hi Andrew,

Quoted 34 lines · Andrew Cooper

Since this TODO was written, BUILD_BUG_ON() has been moved out of xen/lib.h into xen/macros.h, which has done all the hard work.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
CC: Federico Serafini <[email protected]>

---
   xen/include/xen/bug.h | 16 +++++++---------
   1 file changed, 7 insertions(+), 9 deletions(-)
xen/include/xen/bug.h +2 -1
index cb5138410ea7..8cca4486a477 100644
@@ -20,7 +20,8 @@
   #define BUG_DEBUGGER_TRAP_FATAL(regs) 0
   #endif
   -#include <xen/lib.h>
+#include <xen/macros.h>
+#include <xen/types.h>
     #ifndef BUG_FRAME_STRUCT
   @@ -104,14 +105,11 @@ typedef void bug_fn_t(const struct cpu_user_regs
 *regs);
     #ifndef run_in_exception_handler
   -/*
- * TODO: untangle header dependences, break BUILD_BUG_ON() out of

xen/lib.h,
- * and use a real static inline here to get proper type checking of fn().
- */

Quoted 8 lines

xen/include/xen/bug.h typedef void bug_fn_t(const struct cpu_user_regs *regs); · line 109

Quoted 6 lines
xen/include/xen/bug.h +2 -4
-#define run_in_exception_handler(fn) do {                   \
-    (void)((fn) == (void (*)(struct cpu_user_regs *))NULL); \
-    BUG_FRAME(BUGFRAME_run_fn, 0, fn, 0, NULL);             \
-} while ( false )
+static void always_inline run_in_exception_handler(
+    void (*fn)(struct cpu_user_regs *regs))

Based on the other threads, shouldn't this be using bug_fn_t?

Unfortunately it doesn't compile:

common/bug.c: In function ‘do_bug_frame’:
common/bug.c:72:38: error: passing argument 1 of ‘run_in_exception_handler’ from incompatible pointer type [-Werror=incompatible-pointer-types]

    72 |             run_in_exception_handler(fn);
       |                                      ^~
       |                                      |
       |                                      void (*)(struct cpu_user_regs *)
Quoted 6 lines

due to the missing const in common/bug.c:72.

Not to make things more complicated in this patch, I think we should take the patch as is as a simple cleanup (and do further cleanups in the future):

I was sort of expecting an error and I should have proposed a solution, sorry. I don't think we need to go all the way of adding 'const' to all the callbacks.

Instead, we can remove the const in the bug_fn_t (I realize this is a pre-existing bug for Arm). I understand this is not the way we want to go in the longer term, but this would be accurate with the current code base.

Today we have a weird situation:

-> run_in_exception_handler() is called with non-const regs. -> On arm, the callback is cast to a const regs
-> The callback will use a non-const regs

This most likely violate MISRA (I think rule 11.8) in a very subtle way. And none of the analyzer would be able to catch it because how we build the BUG frame.

So I think for this patch we should drop the const in bug_fn_t for consistency. We can then update all the callback to const in a future work.

I would be ok to send a separate patch to update bug_fn_t if this is preferred.

Cheers,

Signature · 1 line
Julien Grall