Commit 970f256e authored by Jens Axboe's avatar Jens Axboe

io_uring: add support for IORING_ASYNC_CANCEL_ANY

Rather than match on a specific key, be it user_data or file, allow
canceling any request that we can lookup. Works like
IORING_ASYNC_CANCEL_ALL in that it cancels multiple requests, but it
doesn't key off user_data or the file.

Can't be set with IORING_ASYNC_CANCEL_FD, as that's a key selector.
Only one may be used at the time.
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
Link: https://lore.kernel.org/r/20220418164402.75259-6-axboe@kernel.dkSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 4bf94615
...@@ -6348,7 +6348,8 @@ static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx, ...@@ -6348,7 +6348,8 @@ static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
list = &ctx->cancel_hash[i]; list = &ctx->cancel_hash[i];
hlist_for_each_entry(req, list, hash_node) { hlist_for_each_entry(req, list, hash_node) {
if (req->file != cd->file) if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) &&
req->file != cd->file)
continue; continue;
if (cd->seq == req->work.cancel_seq) if (cd->seq == req->work.cancel_seq)
continue; continue;
...@@ -6374,7 +6375,7 @@ static int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd) ...@@ -6374,7 +6375,7 @@ static int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
{ {
struct io_kiocb *req; struct io_kiocb *req;
if (cd->flags & IORING_ASYNC_CANCEL_FD) if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_ANY))
req = io_poll_file_find(ctx, cd); req = io_poll_file_find(ctx, cd);
else else
req = io_poll_find(ctx, false, cd); req = io_poll_find(ctx, false, cd);
...@@ -6543,9 +6544,10 @@ static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, ...@@ -6543,9 +6544,10 @@ static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
bool found = false; bool found = false;
list_for_each_entry(req, &ctx->timeout_list, timeout.list) { list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
if (cd->data != req->cqe.user_data) if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) &&
cd->data != req->cqe.user_data)
continue; continue;
if (cd->flags & IORING_ASYNC_CANCEL_ALL) { if (cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY)) {
if (cd->seq == req->work.cancel_seq) if (cd->seq == req->work.cancel_seq)
continue; continue;
req->work.cancel_seq = cd->seq; req->work.cancel_seq = cd->seq;
...@@ -6827,14 +6829,16 @@ static bool io_cancel_cb(struct io_wq_work *work, void *data) ...@@ -6827,14 +6829,16 @@ static bool io_cancel_cb(struct io_wq_work *work, void *data)
if (req->ctx != cd->ctx) if (req->ctx != cd->ctx)
return false; return false;
if (cd->flags & IORING_ASYNC_CANCEL_FD) { if (cd->flags & IORING_ASYNC_CANCEL_ANY) {
;
} else if (cd->flags & IORING_ASYNC_CANCEL_FD) {
if (req->file != cd->file) if (req->file != cd->file)
return false; return false;
} else { } else {
if (req->cqe.user_data != cd->data) if (req->cqe.user_data != cd->data)
return false; return false;
} }
if (cd->flags & IORING_ASYNC_CANCEL_ALL) { if (cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY)) {
if (cd->seq == req->work.cancel_seq) if (cd->seq == req->work.cancel_seq)
return false; return false;
req->work.cancel_seq = cd->seq; req->work.cancel_seq = cd->seq;
...@@ -6847,12 +6851,13 @@ static int io_async_cancel_one(struct io_uring_task *tctx, ...@@ -6847,12 +6851,13 @@ static int io_async_cancel_one(struct io_uring_task *tctx,
{ {
enum io_wq_cancel cancel_ret; enum io_wq_cancel cancel_ret;
int ret = 0; int ret = 0;
bool all;
if (!tctx || !tctx->io_wq) if (!tctx || !tctx->io_wq)
return -ENOENT; return -ENOENT;
cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, cd, all = cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY);
cd->flags & IORING_ASYNC_CANCEL_ALL); cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, cd, all);
switch (cancel_ret) { switch (cancel_ret) {
case IO_WQ_CANCEL_OK: case IO_WQ_CANCEL_OK:
ret = 0; ret = 0;
...@@ -6894,6 +6899,9 @@ static int io_try_cancel(struct io_kiocb *req, struct io_cancel_data *cd) ...@@ -6894,6 +6899,9 @@ static int io_try_cancel(struct io_kiocb *req, struct io_cancel_data *cd)
return ret; return ret;
} }
#define CANCEL_FLAGS (IORING_ASYNC_CANCEL_ALL | IORING_ASYNC_CANCEL_FD | \
IORING_ASYNC_CANCEL_ANY)
static int io_async_cancel_prep(struct io_kiocb *req, static int io_async_cancel_prep(struct io_kiocb *req,
const struct io_uring_sqe *sqe) const struct io_uring_sqe *sqe)
{ {
...@@ -6906,10 +6914,13 @@ static int io_async_cancel_prep(struct io_kiocb *req, ...@@ -6906,10 +6914,13 @@ static int io_async_cancel_prep(struct io_kiocb *req,
req->cancel.addr = READ_ONCE(sqe->addr); req->cancel.addr = READ_ONCE(sqe->addr);
req->cancel.flags = READ_ONCE(sqe->cancel_flags); req->cancel.flags = READ_ONCE(sqe->cancel_flags);
if (req->cancel.flags & ~(IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_FD)) if (req->cancel.flags & ~CANCEL_FLAGS)
return -EINVAL; return -EINVAL;
if (req->cancel.flags & IORING_ASYNC_CANCEL_FD) if (req->cancel.flags & IORING_ASYNC_CANCEL_FD) {
if (req->cancel.flags & IORING_ASYNC_CANCEL_ANY)
return -EINVAL;
req->cancel.fd = READ_ONCE(sqe->fd); req->cancel.fd = READ_ONCE(sqe->fd);
}
return 0; return 0;
} }
...@@ -6917,7 +6928,7 @@ static int io_async_cancel_prep(struct io_kiocb *req, ...@@ -6917,7 +6928,7 @@ static int io_async_cancel_prep(struct io_kiocb *req,
static int __io_async_cancel(struct io_cancel_data *cd, struct io_kiocb *req, static int __io_async_cancel(struct io_cancel_data *cd, struct io_kiocb *req,
unsigned int issue_flags) unsigned int issue_flags)
{ {
bool cancel_all = cd->flags & IORING_ASYNC_CANCEL_ALL; bool all = cd->flags & (IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_ANY);
struct io_ring_ctx *ctx = cd->ctx; struct io_ring_ctx *ctx = cd->ctx;
struct io_tctx_node *node; struct io_tctx_node *node;
int ret, nr = 0; int ret, nr = 0;
...@@ -6926,7 +6937,7 @@ static int __io_async_cancel(struct io_cancel_data *cd, struct io_kiocb *req, ...@@ -6926,7 +6937,7 @@ static int __io_async_cancel(struct io_cancel_data *cd, struct io_kiocb *req,
ret = io_try_cancel(req, cd); ret = io_try_cancel(req, cd);
if (ret == -ENOENT) if (ret == -ENOENT)
break; break;
if (!cancel_all) if (!all)
return ret; return ret;
nr++; nr++;
} while (1); } while (1);
...@@ -6939,13 +6950,13 @@ static int __io_async_cancel(struct io_cancel_data *cd, struct io_kiocb *req, ...@@ -6939,13 +6950,13 @@ static int __io_async_cancel(struct io_cancel_data *cd, struct io_kiocb *req,
ret = io_async_cancel_one(tctx, cd); ret = io_async_cancel_one(tctx, cd);
if (ret != -ENOENT) { if (ret != -ENOENT) {
if (!cancel_all) if (!all)
break; break;
nr++; nr++;
} }
} }
io_ring_submit_unlock(ctx, issue_flags); io_ring_submit_unlock(ctx, issue_flags);
return cancel_all ? nr : ret; return all ? nr : ret;
} }
static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
......
...@@ -193,9 +193,11 @@ enum { ...@@ -193,9 +193,11 @@ enum {
* IORING_ASYNC_CANCEL_ALL Cancel all requests that match the given key * IORING_ASYNC_CANCEL_ALL Cancel all requests that match the given key
* IORING_ASYNC_CANCEL_FD Key off 'fd' for cancelation rather than the * IORING_ASYNC_CANCEL_FD Key off 'fd' for cancelation rather than the
* request 'user_data' * request 'user_data'
* IORING_ASYNC_CANCEL_ANY Match any request
*/ */
#define IORING_ASYNC_CANCEL_ALL (1U << 0) #define IORING_ASYNC_CANCEL_ALL (1U << 0)
#define IORING_ASYNC_CANCEL_FD (1U << 1) #define IORING_ASYNC_CANCEL_FD (1U << 1)
#define IORING_ASYNC_CANCEL_ANY (1U << 2)
/* /*
* IO completion data structure (Completion Queue Entry) * IO completion data structure (Completion Queue Entry)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment