Add io_uring_cqe_seen()
There's a failure case where an application gets a cqe entry, but
the kernel can then overwrite it before the application is done
reading it. This can happen since the io_uring_{get,wait}_completion()
interface both returns a CQE pointer AND increments the ring index.
If the kernel reuses this entry before the applications is done reading
it, the contents may be corrupted.
Remove the CQ head increment from the CQE retrieval, and put it into
a separate helper, io_uring_cqe_seen(). The application must call this
helper when it got a new CQE entry through one of the above calls, and
it's now done reading it.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <inttypes.h>
|
||||
#include "compat.h"
|
||||
#include "io_uring.h"
|
||||
#include "barrier.h"
|
||||
|
||||
/*
|
||||
* Library interface to io_uring
|
||||
@@ -67,6 +68,25 @@ extern int io_uring_wait_completion(struct io_uring *ring,
|
||||
extern int io_uring_submit(struct io_uring *ring);
|
||||
extern struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring);
|
||||
|
||||
/*
|
||||
* Must be called after io_uring_{get,wait}_completion() after the cqe has
|
||||
* been processed by the application.
|
||||
*/
|
||||
static inline void io_uring_cqe_seen(struct io_uring *ring,
|
||||
struct io_uring_cqe *cqe)
|
||||
{
|
||||
if (cqe) {
|
||||
struct io_uring_cq *cq = &ring->cq;
|
||||
|
||||
(*cq->khead)++;
|
||||
/*
|
||||
* Ensure that the kernel sees our new head, the kernel has
|
||||
* the matching read barrier.
|
||||
*/
|
||||
write_barrier();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Command prep helpers
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user