Skip to content

Commit 6f7a644

Browse files
committed
io_uring/register: cache old SQ/CQ head reading for copies
The SQ and CQ ring heads are read twice - once for verifying that it's within bounds, and once inside the loops copying SQE and CQE entries. This is technically incorrect, in case the values could get modified in between verifying them and using them in the copy loop. While this won't lead to anything truly nefarious, it may cause longer loop times for the copies than expected. Read the ring head values once, and use the verified value in the copy loops. Signed-off-by: Jens Axboe <[email protected]>
1 parent 2c5aae1 commit 6f7a644

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

io_uring/register.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,8 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
405405
{
406406
struct io_ring_ctx_rings o = { }, n = { }, *to_free = NULL;
407407
size_t size, sq_array_offset;
408+
unsigned i, tail, old_head;
408409
struct io_uring_params p;
409-
unsigned i, tail;
410410
void *ptr;
411411
int ret;
412412

@@ -518,9 +518,10 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
518518
*/
519519
n.sq_sqes = ptr;
520520
tail = READ_ONCE(o.rings->sq.tail);
521-
if (tail - READ_ONCE(o.rings->sq.head) > p.sq_entries)
521+
old_head = READ_ONCE(o.rings->sq.head);
522+
if (tail - old_head > p.sq_entries)
522523
goto overflow;
523-
for (i = READ_ONCE(o.rings->sq.head); i < tail; i++) {
524+
for (i = old_head; i < tail; i++) {
524525
unsigned src_head = i & (ctx->sq_entries - 1);
525526
unsigned dst_head = i & (p.sq_entries - 1);
526527

@@ -530,7 +531,8 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
530531
WRITE_ONCE(n.rings->sq.tail, READ_ONCE(o.rings->sq.tail));
531532

532533
tail = READ_ONCE(o.rings->cq.tail);
533-
if (tail - READ_ONCE(o.rings->cq.head) > p.cq_entries) {
534+
old_head = READ_ONCE(o.rings->cq.head);
535+
if (tail - old_head > p.cq_entries) {
534536
overflow:
535537
/* restore old rings, and return -EOVERFLOW via cleanup path */
536538
ctx->rings = o.rings;
@@ -539,7 +541,7 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
539541
ret = -EOVERFLOW;
540542
goto out;
541543
}
542-
for (i = READ_ONCE(o.rings->cq.head); i < tail; i++) {
544+
for (i = old_head; i < tail; i++) {
543545
unsigned src_head = i & (ctx->cq_entries - 1);
544546
unsigned dst_head = i & (p.cq_entries - 1);
545547

0 commit comments

Comments
 (0)