io_uring: limit inflight IO
Jens Axboe
maintainerPATCH v4 1/1 io_uring: limit inflight IO
With unbounded request times, we can potentially have a lot of IO inflight. As we provide no real backpressure unless IORING_SETUP_CQ_NODROP is set, and even there there's quite some delay between overflows and backpressure being applied, let's put some safety in place to avoid going way overboard.
This limits the maximum number of inflight IO for any given io_ring_ctx to twice the CQ ring size. This is a losely managed limit, we only check for every SQ ring size number of events. That should be good enough to achieve our goal, which is to prevent massively deep queues. If these are async requests, they would just be waiting for an execution slot anyway.
We return -EBUSY if we can't queue anymore IO. The caller should reap some completions and retry the operation after that. Note that this is a "should never hit this" kind of condition, as driving the depth into CQ overflow situations is unreliable.
---
Changes since v3:
- Fixup masking for when to check (Pavel)
fs/io_uring.c +33 -8
@@ -745,7 +745,7 @@ static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,struct io_kiocb *req;if (!percpu_ref_tryget (&ctx->refs)) -return NULL ; +return ERR_PTR (-ENXIO );if (!state) { req =kmem_cache_alloc (req_cachep, gfp);
@@ -791,7 +791,7 @@ static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,if (req) goto got_it;percpu_ref_put (&ctx->refs); -return NULL ; +return ERR_PTR (-EBUSY ); }static void io_free_req_many (struct io_ring_ctx *ctx,void **reqs,int *nr)
@@ -2997,6 +2997,31 @@ static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)return false; } +static bool io_sq_over_limit (struct io_ring_ctx *ctx,unsigned to_submit) +{ +unsigned inflight; + +if (!list_empty (&ctx->cq_overflow_list)) { +io_cqring_overflow_flush (ctx, false); +return true; + } + +/* +* This doesn't need to be super precise, so only check every once +* in a while. +*/ +if ((ctx->cached_sq_head & ~ctx->sq_mask) != + ((ctx->cached_sq_head + to_submit) & ~ctx->sq_mask)) +return false; + +/* +* Limit us to 2x the CQ ring size +*/ + inflight = ctx->cached_sq_head - + (ctx->cached_cq_tail +atomic_read (&ctx->cached_cq_overflow)); +return inflight >2 * ctx->cq_entries; +} +static int io_submit_sqes (struct io_ring_ctx *ctx,unsigned int nr,struct file *ring_file,int ring_fd,struct mm_struct **mm,bool async)
@@ -3007,10 +3032,8 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,int i, submitted =0 ;bool mm_fault = false; -if (!list_empty (&ctx->cq_overflow_list)) { -io_cqring_overflow_flush (ctx, false); +if (unlikely (io_sq_over_limit (ctx, nr)))return -EBUSY ; - }if (nr >IO_PLUG_THRESHOLD ) {io_submit_state_start (&state, ctx, nr);
@@ -3022,9 +3045,9 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,unsigned int sqe_flags; req =io_get_req (ctx, statep); -if (unlikely (!req)) { +if (unlikely (IS_ERR (req))) {if (!submitted) - submitted = -EAGAIN ; + submitted =PTR_ERR (req);break ; }if (!io_get_sqring (ctx, &req->submit)) {
@@ -3045,8 +3068,10 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,if (link && (sqe_flags &IOSQE_IO_DRAIN )) {if (!shadow_req) { shadow_req =io_get_req (ctx,NULL ); -if (unlikely (!shadow_req)) +if (unlikely (IS_ERR (shadow_req))) { + shadow_req =NULL ; goto out; + } shadow_req->flags |= (REQ_F_IO_DRAIN |REQ_F_SHADOW_DRAIN );refcount_dec (&shadow_req->refs); }
Signature · 1 line
Jens Axboe
Quoted 57 lines · Jens Axboe
With unbounded request times, we can potentially have a lot of IO inflight. As we provide no real backpressure unless
IORING_SETUP_CQ_NODROPis set, and even there there's quite some delay between overflows and backpressure being applied, let's put some safety in place to avoid going way overboard.This limits the maximum number of inflight IO for any given
io_ring_ctxto twice the CQ ring size. This is a losely managed limit, we only check for every SQ ring size number of events. That should be good enough to achieve our goal, which is to prevent massively deep queues. If these are async requests, they would just be waiting for an execution slot anyway.We return
-EBUSYif we can't queue anymore IO. The caller should reap some completions and retry the operation after that. Note that this is a "should never hit this" kind of condition, as driving the depth into CQ overflow situations is unreliable.Signed-off-by Jens Axboe <[email protected]>---
Changes since v3:
- Fixup masking for when to check (Pavel)fs/io_uring.c +8
-2@@ -745,7 +745,7 @@ static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,struct io_kiocb *req;if (!percpu_ref_tryget (&ctx->refs)) -return NULL ; +return ERR_PTR (-ENXIO );if (!state) { req =kmem_cache_alloc (req_cachep, gfp);@@ -791,7 +791,7 @@ static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,if (req) goto got_it;percpu_ref_put (&ctx->refs); -return NULL ; +return ERR_PTR (-EBUSY ); }static void io_free_req_many (struct io_ring_ctx *ctx,void **reqs,int *nr)@@ -2997,6 +2997,31 @@ static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)return false; } +static bool io_sq_over_limit (struct io_ring_ctx *ctx,unsigned to_submit) +{ +unsigned inflight; + +if (!list_empty (&ctx->cq_overflow_list)) { +io_cqring_overflow_flush (ctx, false);
fs/io_uring.c static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s) · line 3015
Quoted 10 lines
fs/io_uring.c +10
-0+return true; + } + +/* +* This doesn't need to be super precise, so only check every once +* in a while. +*/ +if ((ctx->cached_sq_head & ~ctx->sq_mask) != + ((ctx->cached_sq_head + to_submit) & ~ctx->sq_mask)) +return false;
Still not quite agree, with "!=" it will check almost every time. We want to continue with the checks only when we overrun sq_entries (i.e. changed higher bits).
There is a simplified trace: let
1) mask == 3, ~mask == 4
2) head == 0,
3) submitting by one
1: (0 & 4) != (1 & 4): false // do checks
2: (1 & 4) != (2 & 4): false // do checks
3: (2 & 4) != (3 & 4): false // do checks
4: (3 & 4) != (4 & 4): true // skip, return true
The rest looks good,
Quoted 49 lines
fs/io_uring.c +15
-6+ +/* +* Limit us to 2x the CQ ring size +*/ + inflight = ctx->cached_sq_head - + (ctx->cached_cq_tail +atomic_read (&ctx->cached_cq_overflow)); +return inflight >2 * ctx->cq_entries; +} +static int io_submit_sqes (struct io_ring_ctx *ctx,unsigned int nr,struct file *ring_file,int ring_fd,struct mm_struct **mm,bool async)@@ -3007,10 +3032,8 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,int i, submitted =0 ;bool mm_fault = false; -if (!list_empty (&ctx->cq_overflow_list)) { -io_cqring_overflow_flush (ctx, false); +if (unlikely (io_sq_over_limit (ctx, nr)))return -EBUSY ; - }if (nr >IO_PLUG_THRESHOLD ) {io_submit_state_start (&state, ctx, nr);@@ -3022,9 +3045,9 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,unsigned int sqe_flags; req =io_get_req (ctx, statep); -if (unlikely (!req)) { +if (unlikely (IS_ERR (req))) {if (!submitted) - submitted = -EAGAIN ; + submitted =PTR_ERR (req);break ; }if (!io_get_sqring (ctx, &req->submit)) {@@ -3045,8 +3068,10 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,if (link && (sqe_flags &IOSQE_IO_DRAIN )) {if (!shadow_req) { shadow_req =io_get_req (ctx,NULL ); -if (unlikely (!shadow_req)) +if (unlikely (IS_ERR (shadow_req))) { + shadow_req =NULL ; goto out; + } shadow_req->flags |= (REQ_F_IO_DRAIN |REQ_F_SHADOW_DRAIN );refcount_dec (&shadow_req->refs); }
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerQuoted 31 lines · Pavel Begunkov
Quoted 10 lines
fs/io_uring.c +6
-0@@ -2997,6 +2997,31 @@ static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)return false; } +static bool io_sq_over_limit (struct io_ring_ctx *ctx,unsigned to_submit) +{ +unsigned inflight; + +if (!list_empty (&ctx->cq_overflow_list)) { +io_cqring_overflow_flush (ctx, false);fs/io_uring.c static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s) · line 3015
Quoted 10 lines
fs/io_uring.c +10
-0+return true; + } + +/* +* This doesn't need to be super precise, so only check every once +* in a while. +*/ +if ((ctx->cached_sq_head & ~ctx->sq_mask) != + ((ctx->cached_sq_head + to_submit) & ~ctx->sq_mask)) +return false;Still not quite agree, with "!=" it will check almost every time. We want to continue with the checks only when we overrun
sq_entries(i.e. changed higher bits).There is a simplified trace: let
1) mask == 3, ~mask == 4
2) head == 0,
3) submitting by one
Quoted 4 lines
1: (0 & 4) != (1 & 4): false // do checks
2: (1 & 4) != (2 & 4): false // do checks
3: (2 & 4) != (3 & 4): false // do checks
4: (3 & 4) != (4 & 4): true // skip, return true
Huh yes, it's reversed, isn't it. Should be:
if ((ctx->cached_sq_head & ~ctx->sq_mask) == ((ctx->cached_sq_head + to_submit) & ~ctx->sq_mask)) return false;
Quoted 2 lines
The rest looks good,
Reviewed-by Pavel Begunkov <[email protected]>
Thanks, I'll make the above change too.
Signature · 1 line
Jens Axboe