Skip to contentSkip to conversation

public: add RING_NR_UNCONSUMED_*() macros to ring.h

PATCH Superseded Juergen Gross 5 participants 7 messages Nov 26, 2021 – Nov 28, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 public: add RING_NR_UNCONSUMED_*() macros to ring.h

Today RING_HAS_UNCONSUMED_*() macros are returning the number of unconsumed requests or responses instead of a boolean as the name of the macros would imply.

As this "feature" is already being used, rename the macros to RING_NR_UNCONSUMED_*() and define the RING_HAS_UNCONSUMED_*() macros by using the new RING_NR_UNCONSUMED_*() macros. In order to avoid future misuse let RING_HAS_UNCONSUMED_*() really return a boolean.

Note that the known misuses need to be switched to the new RING_NR_UNCONSUMED_*() macros when using this version of ring.h.

Cc Roger Pau Monne <[email protected]>
Cc Manuel Bouyer <[email protected]>
Cc Simon Kuenzer <[email protected]>
Cc Paul Durrant <[email protected]>

---
I have checked Xen, Mini-OS, qemu, grub2, OVMF and Linux kernel for misuses of the RING_HAS_UNCONSUMED_*() macros. There is currently only one instance in the Linux kernel netback driver. The BSDs, UNIKRAFT and Windows PV drivers should be checked for misuse, too.

---
 xen/include/public/io/ring.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
xen/include/public/io/ring.h +6 -3
index c486c457e0..efbc152174 100644
@@ -208,11 +208,11 @@ typedef struct __name##_back_ring __name##_back_ring_t
     (RING_FREE_REQUESTS(_r) == 0)
 
 /* Test if there are outstanding messages to be processed on a ring. */
-#define RING_HAS_UNCONSUMED_RESPONSES(_r)                               \
+#define RING_NR_UNCONSUMED_RESPONSES(_r)                                \
     ((_r)->sring->rsp_prod - (_r)->rsp_cons)
 
 #ifdef __GNUC__
-#define RING_HAS_UNCONSUMED_REQUESTS(_r) ({                             \
+#define RING_NR_UNCONSUMED_REQUESTS(_r) ({                              \
     unsigned int req = (_r)->sring->req_prod - (_r)->req_cons;          \
     unsigned int rsp = RING_SIZE(_r) -                                  \
         ((_r)->req_cons - (_r)->rsp_prod_pvt);                          \
@@ -220,13 +220,16 @@ typedef struct __name##_back_ring __name##_back_ring_t
 })
 #else
 /* Same as above, but without the nice GCC ({ ... }) syntax. */
-#define RING_HAS_UNCONSUMED_REQUESTS(_r)                                \
+#define RING_NR_UNCONSUMED_REQUESTS(_r)                                 \
     ((((_r)->sring->req_prod - (_r)->req_cons) <                        \
       (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ?        \
      ((_r)->sring->req_prod - (_r)->req_cons) :                         \
      (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
 #endif
 
+#define RING_HAS_UNCONSUMED_RESPONSES(_r) (!!RING_NR_UNCONSUMED_RESPONSES(_r))
+#define RING_HAS_UNCONSUMED_REQUESTS(_r)  (!!RING_NR_UNCONSUMED_REQUESTS(_r))
+
 /* Direct access to individual ring elements, by index. */
 #define RING_GET_REQUEST(_r, _idx)                                      \
     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
Signature · 1 line
2.26.2
#1 lore raw
Quoted 8 lines · Juergen Gross

Today RING_HAS_UNCONSUMED_*() macros are returning the number of unconsumed requests or responses instead of a boolean as the name of the macros would imply.

As this "feature" is already being used, rename the macros to RING_NR_UNCONSUMED_*() and define the RING_HAS_UNCONSUMED_*() macros by using the new RING_NR_UNCONSUMED_*() macros. In order to avoid future misuse let RING_HAS_UNCONSUMED_*() really return a boolean.

I don't think we can go this far; consumers of our headers may choose to do so, but anyone taking the headers verbatim would be at risk of getting screwed if they had any instance of abuse in their trees. IOW I think the original-name macros ought to be direct aliases of the new-name ones, and only in Linux'es clone you could then go further.

Jan

#2 lore raw
Quoted 10 lines · Jan Beulich
Quoted 8 lines · Juergen Gross

Today RING_HAS_UNCONSUMED_*() macros are returning the number of unconsumed requests or responses instead of a boolean as the name of the macros would imply.

As this "feature" is already being used, rename the macros to RING_NR_UNCONSUMED_*() and define the RING_HAS_UNCONSUMED_*() macros by using the new RING_NR_UNCONSUMED_*() macros. In order to avoid future misuse let RING_HAS_UNCONSUMED_*() really return a boolean.

Quoted 5 lines

I don't think we can go this far; consumers of our headers may choose to do so, but anyone taking the headers verbatim would be at risk of getting screwed if they had any instance of abuse in their trees. IOW I think the original-name macros ought to be direct aliases of the new-name ones, and only in Linux'es clone you could then go further.

Fine with me. I'm inclined to add a comment hinting at that possibility then.

Juergen

#3 lore raw
Quoted 11 lines · Juergen Gross
Quoted 10 lines · Jan Beulich
Quoted 8 lines · Juergen Gross

Today RING_HAS_UNCONSUMED_*() macros are returning the number of unconsumed requests or responses instead of a boolean as the name of the macros would imply.

As this "feature" is already being used, rename the macros to RING_NR_UNCONSUMED_*() and define the RING_HAS_UNCONSUMED_*() macros by using the new RING_NR_UNCONSUMED_*() macros. In order to avoid future misuse let RING_HAS_UNCONSUMED_*() really return a boolean.

Quoted 8 lines
Quoted 5 lines

I don't think we can go this far; consumers of our headers may choose to do so, but anyone taking the headers verbatim would be at risk of getting screwed if they had any instance of abuse in their trees. IOW I think the original-name macros ought to be direct aliases of the new-name ones, and only in Linux'es clone you could then go further.

Fine with me. I'm inclined to add a comment hinting at that possibility then.

Indeed, please do.

Jan

#4 lore raw
In reply to Juergen Gross

Hi Juergen,

thanks a lot for putting us in CC. From the Unikraft perspective, we are fine with the change because we currently maintain a copy of the Xen headers in our tree. Our main reason is that we aim to keep compiling easier by avoiding off-tree references. Obviously, we have to update our copy but a diff will tell us where we need to adopt our code. In general, I like the clarity of the interface change that you are suggesting.

Simon

Quoted 67 lines · Juergen Gross

Today RING_HAS_UNCONSUMED_*() macros are returning the number of unconsumed requests or responses instead of a boolean as the name of the macros would imply.

As this "feature" is already being used, rename the macros to RING_NR_UNCONSUMED_*() and define the RING_HAS_UNCONSUMED_*() macros by using the new RING_NR_UNCONSUMED_*() macros. In order to avoid future misuse let RING_HAS_UNCONSUMED_*() really return a boolean.

Note that the known misuses need to be switched to the new RING_NR_UNCONSUMED_*() macros when using this version of ring.h.

Cc Roger Pau Monne <[email protected]>
Cc Manuel Bouyer <[email protected]>
Cc Simon Kuenzer <[email protected]>
Cc Paul Durrant <[email protected]>

---
I have checked Xen, Mini-OS, qemu, grub2, OVMF and Linux kernel for misuses of the RING_HAS_UNCONSUMED_*() macros. There is currently only one instance in the Linux kernel netback driver. The BSDs, UNIKRAFT and Windows PV drivers should be checked for misuse, too.
---
xen/include/public/io/ring.h | 9 ++++++---

1 file changed, 6 insertions(+), 3 deletions(-)
xen/include/public/io/ring.h +6 -3
index c486c457e0..efbc152174 100644
@@ -208,11 +208,11 @@ typedef struct __name##_back_ring __name##_back_ring_t
    (RING_FREE_REQUESTS(_r) == 0)
 
 /* Test if there are outstanding messages to be processed on a ring. */
-#define RING_HAS_UNCONSUMED_RESPONSES(_r)                               \
+#define RING_NR_UNCONSUMED_RESPONSES(_r)                                \
    ((_r)->sring->rsp_prod - (_r)->rsp_cons)
 
 #ifdef __GNUC__
-#define RING_HAS_UNCONSUMED_REQUESTS(_r) ({                             \
+#define RING_NR_UNCONSUMED_REQUESTS(_r) ({                              \
    unsigned int req = (_r)->sring->req_prod - (_r)->req_cons;          \
    unsigned int rsp = RING_SIZE(_r) -                                  \
        ((_r)->req_cons - (_r)->rsp_prod_pvt);                          \
@@ -220,13 +220,16 @@ typedef struct __name##_back_ring __name##_back_ring_t
 })
 #else
 /* Same as above, but without the nice GCC ({ ... }) syntax. */
-#define RING_HAS_UNCONSUMED_REQUESTS(_r)                                \
+#define RING_NR_UNCONSUMED_REQUESTS(_r)                                 \
    ((((_r)->sring->req_prod - (_r)->req_cons) <                        \
      (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ?        \
     ((_r)->sring->req_prod - (_r)->req_cons) :                         \
     (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
 #endif
 
+#define RING_HAS_UNCONSUMED_RESPONSES(_r) (!!RING_NR_UNCONSUMED_RESPONSES(_r))
+#define RING_HAS_UNCONSUMED_REQUESTS(_r)  (!!RING_NR_UNCONSUMED_REQUESTS(_r))
+
 /* Direct access to individual ring elements, by index. */
 #define RING_GET_REQUEST(_r, _idx)                                      \
    (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
Signature · 1 line
2.26.2
#5 lore raw
In reply to Juergen Gross
Quoted 11 lines · Juergen Gross

Today RING_HAS_UNCONSUMED_*() macros are returning the number of unconsumed requests or responses instead of a boolean as the name of the macros would imply.

As this "feature" is already being used, rename the macros to RING_NR_UNCONSUMED_*() and define the RING_HAS_UNCONSUMED_*() macros by using the new RING_NR_UNCONSUMED_*() macros. In order to avoid future misuse let RING_HAS_UNCONSUMED_*() really return a boolean.

Note that the known misuses need to be switched to the new RING_NR_UNCONSUMED_*() macros when using this version of ring.h.

AFAIK NetBSD is using RING_HAS_UNCONSUMED as a booleanm so it should be fine with this change

Signature · 3 lines
Manuel Bouyer <[email protected]>
     NetBSD: 26 ans d'experience feront toujours la difference
--
#6 lore raw
In reply to Juergen Gross
Quoted 17 lines · Juergen Gross

Today RING_HAS_UNCONSUMED_*() macros are returning the number of unconsumed requests or responses instead of a boolean as the name of the macros would imply.

As this "feature" is already being used, rename the macros to RING_NR_UNCONSUMED_*() and define the RING_HAS_UNCONSUMED_*() macros by using the new RING_NR_UNCONSUMED_*() macros. In order to avoid future misuse let RING_HAS_UNCONSUMED_*() really return a boolean.

Note that the known misuses need to be switched to the new RING_NR_UNCONSUMED_*() macros when using this version of ring.h.

Cc Roger Pau Monne <[email protected]>
Cc Manuel Bouyer <[email protected]>
Cc Simon Kuenzer <[email protected]>
Cc Paul Durrant <[email protected]>
Quoted 5 lines

---
I have checked Xen, Mini-OS, qemu, grub2, OVMF and Linux kernel for misuses of the RING_HAS_UNCONSUMED_*() macros. There is currently only one instance in the Linux kernel netback driver. The BSDs, UNIKRAFT and Windows PV drivers should be checked for misuse, too.

I don't think there will be any problem with Windows.

Paul