Skip to contentSkip to conversation

xen/evtchn: Purge ERROR_EXIT{,_DOM}()

PATCH In review Andrew Cooper 5 participants 9 messages Jun 13, 2023 – Jun 14, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/evtchn: Purge ERROR_EXIT{,_DOM}()

These are disliked specifically by MISRA, but they also interfere with code legibility by hiding control flow. Expand and drop them.

* Rearrange the order of actions to write into rc, then render rc in the gdprintk().
* Drop redundant "rc = rc" assignments
* Switch to using %pd for rendering domains

No functional change. Resulting binary is identical.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>

---
 xen/common/event_channel.c | 79 +++++++++++++++++++++++++-------------
 1 file changed, 52 insertions(+), 27 deletions(-)
xen/common/event_channel.c +52 -27
index f5e0b12d1520..a7a004a08429 100644
@@ -36,23 +36,6 @@
 #include <asm/guest.h>
 #endif
 
-#define ERROR_EXIT(_errno)                                          \
-    do {                                                            \
-        gdprintk(XENLOG_WARNING,                                    \
-                "EVTCHNOP failure: error %d\n",                     \
-                (_errno));                                          \
-        rc = (_errno);                                              \
-        goto out;                                                   \
-    } while ( 0 )
-#define ERROR_EXIT_DOM(_errno, _dom)                                \
-    do {                                                            \
-        gdprintk(XENLOG_WARNING,                                    \
-                "EVTCHNOP failure: domain %d, error %d\n",          \
-                (_dom)->domain_id, (_errno));                       \
-        rc = (_errno);                                              \
-        goto out;                                                   \
-    } while ( 0 )
-
 #define consumer_is_xen(e) (!!(e)->xen_consumer)
 
 /*
@@ -336,7 +319,11 @@ int evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc, evtchn_port_t port)
 
     port = rc = evtchn_get_port(d, port);
     if ( rc < 0 )
-        ERROR_EXIT(rc);
+    {
+        gdprintk(XENLOG_WARNING, "EVTCHNOP failure: error %d\n", rc);
+        goto out;
+    }
+
     rc = 0;
 
     chn = evtchn_from_port(d, port);
@@ -412,17 +399,30 @@ int evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind, struct domain *ld,
 
     lport = rc = evtchn_get_port(ld, lport);
     if ( rc < 0 )
-        ERROR_EXIT(rc);
+    {
+        gdprintk(XENLOG_WARNING, "EVTCHNOP failure: error %d\n", rc);
+        goto out;
+    }
+
     rc = 0;
 
     lchn = evtchn_from_port(ld, lport);
 
     rchn = _evtchn_from_port(rd, rport);
     if ( !rchn )
-        ERROR_EXIT_DOM(-EINVAL, rd);
+    {
+        rc = -EINVAL;
+        gdprintk(XENLOG_WARNING, "EVTCHNOP failure: %pd, error %d\n", rd, rc);
+        goto out;
+    }
+
     if ( (rchn->state != ECS_UNBOUND) ||
          (rchn->u.unbound.remote_domid != ld->domain_id) )
-        ERROR_EXIT_DOM(-EINVAL, rd);
+    {
+        rc = -EINVAL;
+        gdprintk(XENLOG_WARNING, "EVTCHNOP failure: %pd, error %d\n", rd, rc);
+        goto out;
+    }
 
     rc = xsm_evtchn_interdomain(XSM_HOOK, ld, lchn, rd, rchn);
     if ( rc )
@@ -487,11 +487,19 @@ int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port)
     write_lock(&d->event_lock);
 
     if ( read_atomic(&v->virq_to_evtchn[virq]) )
-        ERROR_EXIT(-EEXIST);
+    {
+        rc = -EEXIST;
+        gdprintk(XENLOG_WARNING, "EVTCHNOP failure: error %d\n", rc);
+        goto out;
+    }
 
     port = rc = evtchn_get_port(d, port);
     if ( rc < 0 )
-        ERROR_EXIT(rc);
+    {
+        gdprintk(XENLOG_WARNING, "EVTCHNOP failure: error %d\n", rc);
+        goto out;
+    }
+
     rc = 0;
 
     chn = evtchn_from_port(d, port);
@@ -535,7 +543,11 @@ static int evtchn_bind_ipi(evtchn_bind_ipi_t *bind)
     write_lock(&d->event_lock);
 
     if ( (port = get_free_port(d)) < 0 )
-        ERROR_EXIT(port);
+    {
+        rc = port;
+        gdprintk(XENLOG_WARNING, "EVTCHNOP failure: error %d\n", rc);
+        goto out;
+    }
 
     chn = evtchn_from_port(d, port);
 
@@ -599,16 +611,29 @@ static int evtchn_bind_pirq(evtchn_bind_pirq_t *bind)
     write_lock(&d->event_lock);
 
     if ( pirq_to_evtchn(d, pirq) != 0 )
-        ERROR_EXIT(-EEXIST);
+    {
+        rc = -EEXIST;
+        gdprintk(XENLOG_WARNING, "EVTCHNOP failure: error %d\n", rc);
+        goto out;
+    }
 
     if ( (port = get_free_port(d)) < 0 )
-        ERROR_EXIT(port);
+    {
+        rc = port;
+        gdprintk(XENLOG_WARNING, "EVTCHNOP failure: error %d\n", rc);
+        goto out;
+    }
 
     chn = evtchn_from_port(d, port);
 
     info = pirq_get_info(d, pirq);
     if ( !info )
-        ERROR_EXIT(-ENOMEM);
+    {
+        rc = -ENOMEM;
+        gdprintk(XENLOG_WARNING, "EVTCHNOP failure: error %d\n", rc);
+        goto out;
+    }
+
     info->evtchn = port;
     rc = (!is_hvm_domain(d)
           ? pirq_guest_bind(v, info,

base-commit: f29363922c1b41310c3d87fd9a861ffa9db9204a

Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 1 line · Andrew Cooper

These are disliked specifically by MISRA, but they also interfere with code

Please explicitly name the rule.

Quoted 10 lines

legibility by hiding control flow. Expand and drop them.

* Rearrange the order of actions to write into rc, then render rc in the

    gdprintk().

* Drop redundant "rc = rc" assignments
* Switch to using %pd for rendering domains

No functional change. Resulting binary is identical.

[...]

Quoted 1 line

base-commit: f29363922c1b41310c3d87fd9a861ffa9db9204a

I notice you have a few e-mail contain this tag. This is a pretty useful when reviewing patches. Do you know which tool is creating it?

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw
Quoted 7 lines · Julien Grall

Hi,

Quoted 2 lines · Andrew Cooper

These are disliked specifically by MISRA, but they also interfere with code

Please explicitly name the rule.

I can't remember it off the top of my head.

Stefano/Bertrand?

Quoted 9 lines
Quoted 9 lines

legibility by hiding control flow.  Expand and drop them.

  * Rearrange the order of actions to write into rc, then render rc in the
    gdprintk().
  * Drop redundant "rc = rc" assignments
  * Switch to using %pd for rendering domains

Quoted 5 lines
Quoted 3 lines

No functional change.  Resulting binary is identical.

Thanks.

Quoted 5 lines
Quoted 2 lines

base-commit: f29363922c1b41310c3d87fd9a861ffa9db9204a

I notice you have a few e-mail contain this tag. This is a pretty useful when reviewing patches. Do you know which tool is creating it?

Plain git, and the capability has been around for years and years but nigh on impossible to search for or find in the manual.  Searching for "base-commit" gets you a tonne of intros of how to rebase.

You want

[format]
        useAutoBase = "whenAble"

or pass --base=auto to git-format-patch

~Andrew

#3 lore raw
Quoted 4 lines · Andrew Cooper
Quoted 3 lines · Julien Grall

Hi,

On 13/06/2023 17:22, Andrew Cooper wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

These are disliked specifically by MISRA, but they also interfere with code

Please explicitly name the rule.

I can't remember it off the top of my head.

Stefano/Bertrand?

Rule 2.1

Quoted 35 lines
Quoted 8 lines
Quoted 8 lines

legibility by hiding control flow.  Expand and drop them.

  * Rearrange the order of actions to write into rc, then render rc
in the
    gdprintk().
  * Drop redundant "rc = rc" assignments
  * Switch to using %pd for rendering domains

Quoted 5 lines
Quoted 3 lines

No functional change.  Resulting binary is identical.

Thanks.

Quoted 5 lines
Quoted 2 lines

base-commit: f29363922c1b41310c3d87fd9a861ffa9db9204a

I notice you have a few e-mail contain this tag. This is a pretty useful when reviewing patches. Do you know which tool is creating it?

Plain git, and the capability has been around for years and years but nigh on impossible to search for or find in the manual.  Searching for "base-commit" gets you a tonne of intros of how to rebase.

You want

[format]
        useAutoBase = "whenAble"

or pass --base=auto to git-format-patch

~Andrew

#4 lore raw
Quoted 8 lines · Roberto Bagnara
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Julien Grall

Hi,

Quoted 2 lines · Andrew Cooper

These are disliked specifically by MISRA, but they also interfere with code

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Please explicitly name the rule.

I can't remember it off the top of my head.

Stefano/Bertrand?

Rule 2.1

That's about unreachable code, but inside the constructs there's nothing that's unreachable afaics. Plus expanding "manually" them wouldn't change reachability, would it?

Jan

#5 lore raw
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Roberto Bagnara
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Julien Grall

Hi,

Quoted 2 lines · Andrew Cooper

These are disliked specifically by MISRA, but they also interfere with code

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

Please explicitly name the rule.

I can't remember it off the top of my head.

Stefano/Bertrand?

Rule 2.1

That's about unreachable code, but inside the constructs there's nothing that's unreachable afaics. Plus expanding "manually" them wouldn't change reachability, would it?

I bet it's complaining about the while() after the goto.

I can see why things end up caring - because this violation can only be spotted in the fully-preprocessed source where the macro-ness has gone away, and *then* applying blanket rules.

Which comes back to the original point I made on the call yesterday that do{}while(0) correctness for macros is far more important than some, honestly suspect, claim about the resulting code being somehow "better" without the macro safety.

~Andrew

#6 lore raw
Quoted 18 lines · Andrew Cooper
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Roberto Bagnara
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Julien Grall

Hi,

Quoted 2 lines · Andrew Cooper

These are disliked specifically by MISRA, but they also interfere with code

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

Please explicitly name the rule.

I can't remember it off the top of my head.

Stefano/Bertrand?

Rule 2.1

That's about unreachable code, but inside the constructs there's nothing that's unreachable afaics. Plus expanding "manually" them wouldn't change reachability, would it?

Quoted 5 lines

I bet it's complaining about the while() after the goto.

I can see why things end up caring - because this violation can only be spotted in the fully-preprocessed source where the macro-ness has gone away, and *then* applying blanket rules.

Hmm, perhaps.

Quoted 4 lines

Which comes back to the original point I made on the call yesterday that do{}while(0) correctness for macros is far more important than some, honestly suspect, claim about the resulting code being somehow "better" without the macro safety.

Even further I would claim that the while(0) part of the construct isn't unreachable code simply because it doesn't result in any code being generated. But of course "code" here may mean "source code", not the resulting binary representation.

Jan

#7 lore raw
In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper

These are disliked specifically by MISRA, but they also interfere with code legibility by hiding control flow. Expand and drop them.

* Rearrange the order of actions to write into rc, then render rc in the gdprintk().
* Drop redundant "rc = rc" assignments
* Switch to using %pd for rendering domains

With this change, ...

Quoted 1 line

No functional change. Resulting binary is identical.

... I doubt this. Even .text being entirely identical would be pure luck, as message offsets might change slightly depending on how much padding the compiler inserts between them. Furthermore I wonder whether ...

xen/common/event_channel.c int evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc, evtchn_port_t port) · line 325

Quoted 9 lines
xen/common/event_channel.c +4 -1
@@ -336,7 +319,11 @@ int evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc, evtchn_port_t port)
 
     port = rc = evtchn_get_port(d, port);
     if ( rc < 0 )
-        ERROR_EXIT(rc);
+    {
+        gdprintk(XENLOG_WARNING, "EVTCHNOP failure: error %d\n", rc);
+        goto out;
+    }

... it wouldn't make sense to mention the actual operation that failed, now that each function has its own message(s). In turn I question the usesfulness of "error" in the message text.

Then again I wonder whether it isn't time to purge these gdprintk()s altogether. Surely they served a purpose for bringing up initial Linux and mini-os and alike, but that's been two decades ago now.

Jan

#8 lore raw

Hi Jan,

Quoted 31 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper

These are disliked specifically by MISRA, but they also interfere with code legibility by hiding control flow. Expand and drop them.

* Rearrange the order of actions to write into rc, then render rc in the

    gdprintk().

* Drop redundant "rc = rc" assignments
* Switch to using %pd for rendering domains

With this change, ...

Quoted 1 line

No functional change. Resulting binary is identical.

... I doubt this. Even .text being entirely identical would be pure luck, as message offsets might change slightly depending on how much padding the compiler inserts between them. Furthermore I wonder whether ...

xen/common/event_channel.c int evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc, evtchn_port_t port) · line 325

Quoted 9 lines
xen/common/event_channel.c +4 -1
@@ -336,7 +319,11 @@ int evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc, evtchn_port_t port)
 
      port = rc = evtchn_get_port(d, port);
      if ( rc < 0 )
-        ERROR_EXIT(rc);
+    {
+        gdprintk(XENLOG_WARNING, "EVTCHNOP failure: error %d\n", rc);
+        goto out;
+    }

... it wouldn't make sense to mention the actual operation that failed, now that each function has its own message(s). In turn I question the usesfulness of "error" in the message text.

Quoted 3 lines

Then again I wonder whether it isn't time to purge these gdprintk()s altogether. Surely they served a purpose for bringing up initial Linux and mini-os and alike, but that's been two decades ago now.

There are still port of new OS on Xen (e.g. QNX, FreeRTOS...) happening time to time. Also, having messages in error path is something I wish most of Xen had. Quite often when I end up to debug an hypercall, I will add printks().

So I am not in favor of removing the gdprintk()s.

Cheers,

Signature · 1 line
Julien Grall