xen: Add RING_COPY_RESPONSE()
PATCH 1/1 xen: Add RING_COPY_RESPONSE()
Using RING_GET_RESPONSE() on a shared ring is easy to use incorrectly (i.e., by not considering that the other end may alter the data in the shared ring while it is being inspected). Safe usage of a response generally requires taking a local copy.
Provide a RING_COPY_RESPONSE() macro to use instead of RING_GET_RESPONSE() and an open-coded memcpy(). This takes care of ensuring that the copy is done correctly regardless of any possible compiler optimizations.
Use a volatile source to prevent the compiler from reordering or omitting the copy.
This generalizes similar RING_COPY_REQUEST() macro added in 3f20b8def0.
--- xen/include/public/io/ring.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
xen/include/public/io/ring.h +9 -6
@@ -231,22 +231,25 @@ typedef struct __name##_back_ring __name##_back_ring_t#define RING_GET_REQUEST (_r, _idx) \ (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req)) +#define RING_GET_RESPONSE (_r, _idx) \ + (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp)) +/* -* Get a local copy of a request. +* Get a local copy of a request/response. * -* Use this in preference to RING_GET_REQUEST() so all processing is +* Use this in preference to RING_GET_{REQUEST,RESPONSE}() so all processing is * done on a local copy that cannot be modified by the other end. * * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this * to be ineffective where _req is a struct which consists of only bitfields. */ -#define RING_COPY_REQUEST (_r, _idx, _req) do { \ +#define RING_COPY_ (action, _r, _idx, _req) do { \/* Use volatile to force the copy into _req. */ \ - *(_req) = *(volatile typeof (_req ))RING_GET_REQUEST (_r, _idx); \ + *(_req) = *(volatile typeof(_req))RING_GET_##action(_r, _idx); \ } while (0) -#define RING_GET_RESPONSE (_r, _idx) \ - (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp)) +#define RING_COPY_REQUEST (_r, _idx, _req) RING_COPY_(REQUEST, _r, _idx, _req) +#define RING_COPY_RESPONSE (_r, _idx, _req) RING_COPY_(RESPONSE, _r, _idx, _req)/* Loop termination condition: Would the specified index overflow the ring? */ #define RING_REQUEST_CONS_OVERFLOW (_r, _cons) \
Signature · 1 line
2.25.4
Quoted 34 lines · Marek Marczykowski-Górecki
Using
RING_GET_RESPONSE()on a shared ring is easy to use incorrectly (i.e., by not considering that the other end may alter the data in the shared ring while it is being inspected). Safe usage of a response generally requires taking a local copy.Provide a
RING_COPY_RESPONSE()macro to use instead ofRING_GET_RESPONSE()and an open-codedmemcpy(). This takes care of ensuring that the copy is done correctly regardless of any possible compiler optimizations.Use a volatile source to prevent the compiler from reordering or omitting the copy.
This generalizes similar
RING_COPY_REQUEST()macro added in3f20b8def0.Signed-off-by Marek Marczykowski-Górecki <[email protected]>--- xen/include/public/io/ring.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)xen/include/public/io/ring.h +4
-1@@ -231,22 +231,25 @@ typedef struct __name##_back_ring __name##_back_ring_t#define RING_GET_REQUEST (_r, _idx) \ (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req)) +#define RING_GET_RESPONSE (_r, _idx) \ + (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp)) +/* -* Get a local copy of a request. +* Get a local copy of a request/response.
xen/include/public/io/ring.h typedef struct __name##_back_ring __name##_back_ring_t · line 246
Quoted 10 lines
xen/include/public/io/ring.h +2
-2* -* Use this in preference to RING_GET_REQUEST() so all processing is +* Use this in preference to RING_GET_{REQUEST,RESPONSE}() so all processing is * done on a local copy that cannot be modified by the other end. * * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this * to be ineffective where _req is a struct which consists of only bitfields. */ -#define RING_COPY_REQUEST (_r, _idx, _req) do { \ +#define RING_COPY_ (action, _r, _idx, _req) do { \
What about renaming _req to _dest in order to reflect that it can be a request _or_ a response?
"action" is misnamed, IMO. What about "type"?
xen/include/public/io/ring.h typedef struct __name##_back_ring __name##_back_ring_t · line 252
Quoted 9 lines
xen/include/public/io/ring.h +3
-3/* Use volatile to force the copy into _req. */ \ - *(_req) = *(volatile typeof (_req ))RING_GET_REQUEST (_r, _idx); \ + *(_req) = *(volatile typeof(_req))RING_GET_##action(_r, _idx); \ } while (0) -#define RING_GET_RESPONSE(_r, _idx) \ - (&((_r)->sring->ring[((_idx) & (RING_SIZE (_r) -1 ))].rsp)) +#define RING_COPY_REQUEST (_r, _idx, _req) RING_COPY_(REQUEST, _r, _idx, _req) +#define RING_COPY_RESPONSE (_r, _idx, _req) RING_COPY_(RESPONSE, _r, _idx, _req)
Use _rsp instead of _req for RING_COPY_RESPONSE()?
Juergen
Quoted 56 lines · Jürgen Groß
Quoted 34 lines · Marek Marczykowski-Górecki
Using
RING_GET_RESPONSE()on a shared ring is easy to use incorrectly (i.e., by not considering that the other end may alter the data in the shared ring while it is being inspected). Safe usage of a response generally requires taking a local copy.Provide a
RING_COPY_RESPONSE()macro to use instead ofRING_GET_RESPONSE()and an open-codedmemcpy(). This takes care of ensuring that the copy is done correctly regardless of any possible compiler optimizations.Use a volatile source to prevent the compiler from reordering or omitting the copy.
This generalizes similar
RING_COPY_REQUEST()macro added in3f20b8def0.Signed-off-by Marek Marczykowski-Górecki <[email protected]>--- xen/include/public/io/ring.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)xen/include/public/io/ring.h +4
-1@@ -231,22 +231,25 @@ typedef struct __name##_back_ring __name##_back_ring_t#define RING_GET_REQUEST (_r, _idx) \ (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req)) +#define RING_GET_RESPONSE (_r, _idx) \ + (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp)) +/* -* Get a local copy of a request. +* Get a local copy of a request/response. xen/include/public/io/ring.h typedef struct __name##_back_ring __name##_back_ring_t · line 246
Quoted 10 lines
xen/include/public/io/ring.h +2
-2* -* Use this in preference to RING_GET_REQUEST() so all processing is +* Use this in preference to RING_GET_{REQUEST,RESPONSE}() so all processing is * done on a local copy that cannot be modified by the other end. * * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this * to be ineffective where _req is a struct which consists of only bitfields. */ -#define RING_COPY_REQUEST (_r, _idx, _req) do { \ +#define RING_COPY_ (action, _r, _idx, _req) do { \What about renaming
_reqto_destin order to reflect that it can be a request _or_ a response?"action" is misnamed, IMO. What about "type"?
Quoted 5 lines
xen/include/public/io/ring.h +1
-1/* Use volatile to force the copy into _req. */ \ - *(_req) = *(volatile typeof (_req ))RING_GET_REQUEST (_r, _idx); \ + *(_req) = *(volatile typeof(_req))RING_GET_##action(_r, _idx); \ } while (0)
Quoted 6 lines
xen/include/public/io/ring.h typedef struct __name##_back_ring __name##_back_ring_t · line 252
Quoted 4 lines
xen/include/public/io/ring.h +2
-2-#define RING_GET_RESPONSE (_r, _idx) \ - (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp)) +#define RING_COPY_REQUEST (_r, _idx, _req) RING_COPY_(REQUEST, _r, _idx, _req) +#define RING_COPY_RESPONSE (_r, _idx, _req) RING_COPY_(RESPONSE, _r, _idx, _req)Use
_rspinstead of_reqforRING_COPY_RESPONSE()?
And, while at it, get rid of the leading underscores of macro parameter names wherever possible without extra code churn?
Jan