Skip to contentSkip to conversation

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

PATCH In review Juergen Gross 2 participants 2 messages Jan 27, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 public: add XEN_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 XEN_RING_NR_UNCONSUMED_*() and define the RING_HAS_UNCONSUMED_*() macros by using the new XEN_RING_NR_UNCONSUMED_*() macros. In order to avoid future misuse let RING_HAS_UNCONSUMED_*() optionally really return a boolean (can be activated by defining XEN_RING_HAS_UNCONSUMED_IS_BOOL).

Note that the known misuses need to be switched to the new XEN_RING_NR_UNCONSUMED_*() macros when using the RING_HAS_UNCONSUMED_*() variants returning a boolean value.

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

---
V3:
- add XEN_ prefix to new macros (Jan Beulich)

---
 xen/include/public/io/ring.h | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
xen/include/public/io/ring.h +17 -3
index c486c457e0..277af36e61 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 XEN_RING_NR_UNCONSUMED_RESPONSES(_r)                            \
     ((_r)->sring->rsp_prod - (_r)->rsp_cons)
 
 #ifdef __GNUC__
-#define RING_HAS_UNCONSUMED_REQUESTS(_r) ({                             \
+#define XEN_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,27 @@ 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 XEN_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
 
+#ifdef XEN_RING_HAS_UNCONSUMED_IS_BOOL
+/*
+ * These variants should only be used in case no caller is abusing them for
+ * obtaining the number of unconsumed responses/requests.
+ */
+#define RING_HAS_UNCONSUMED_RESPONSES(_r) \
+    (!!XEN_RING_NR_UNCONSUMED_RESPONSES(_r))
+#define RING_HAS_UNCONSUMED_REQUESTS(_r)  \
+    (!!XEN_RING_NR_UNCONSUMED_REQUESTS(_r))
+#else
+#define RING_HAS_UNCONSUMED_RESPONSES(_r) XEN_RING_NR_UNCONSUMED_RESPONSES(_r)
+#define RING_HAS_UNCONSUMED_REQUESTS(_r)  XEN_RING_NR_UNCONSUMED_REQUESTS(_r)
+#endif
+
 /* 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.31.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 14 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 XEN_RING_NR_UNCONSUMED_*() and define the RING_HAS_UNCONSUMED_*() macros by using the new XEN_RING_NR_UNCONSUMED_*() macros. In order to avoid future misuse let RING_HAS_UNCONSUMED_*() optionally really return a boolean (can be activated by defining XEN_RING_HAS_UNCONSUMED_IS_BOOL).

Note that the known misuses need to be switched to the new XEN_RING_NR_UNCONSUMED_*() macros when using the RING_HAS_UNCONSUMED_*() variants returning a boolean value.

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