From: Jens Axboe <axboe@kernel.dk>
To: io-uring@vger.kernel.org
Cc: Jens Axboe <axboe@kernel.dk>
Subject: [PATCH 4/5] io_uring: allow IORING_OP_ASYNC_CANCEL with 'fd' key
Date: Mon, 18 Apr 2022 10:44:01 -0600 [thread overview]
Message-ID: <20220418164402.75259-5-axboe@kernel.dk> (raw)
In-Reply-To: <20220418164402.75259-1-axboe@kernel.dk>
Currently sqe->addr must contain the user_data of the request being
canceled. Introduce the IORING_ASYNC_CANCEL_FD flag, which tells the
kernel that we're keying off the file fd instead for cancelation. This
allows canceling any request that a) uses a file, and b) was assigned the
file based on the value being passed in.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
---
fs/io_uring.c | 72 ++++++++++++++++++++++++++++++-----
include/uapi/linux/io_uring.h | 3 ++
2 files changed, 66 insertions(+), 9 deletions(-)
diff --git a/fs/io_uring.c b/fs/io_uring.c
index c6c00c22940a..70050c94560e 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -570,6 +570,7 @@ struct io_cancel {
struct file *file;
u64 addr;
u32 flags;
+ s32 fd;
};
struct io_timeout {
@@ -975,7 +976,10 @@ struct io_defer_entry {
struct io_cancel_data {
struct io_ring_ctx *ctx;
- u64 data;
+ union {
+ u64 data;
+ struct file *file;
+ };
u32 flags;
int seq;
};
@@ -6288,6 +6292,29 @@ static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
return NULL;
}
+static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
+ struct io_cancel_data *cd)
+ __must_hold(&ctx->completion_lock)
+{
+ struct io_kiocb *req;
+ int i;
+
+ for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
+ struct hlist_head *list;
+
+ list = &ctx->cancel_hash[i];
+ hlist_for_each_entry(req, list, hash_node) {
+ if (req->file != cd->file)
+ continue;
+ if (cd->seq == req->work.cancel_seq)
+ continue;
+ req->work.cancel_seq = cd->seq;
+ return req;
+ }
+ }
+ return NULL;
+}
+
static bool io_poll_disarm(struct io_kiocb *req)
__must_hold(&ctx->completion_lock)
{
@@ -6301,8 +6328,12 @@ static bool io_poll_disarm(struct io_kiocb *req)
static int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
__must_hold(&ctx->completion_lock)
{
- struct io_kiocb *req = io_poll_find(ctx, false, cd);
+ struct io_kiocb *req;
+ if (cd->flags & IORING_ASYNC_CANCEL_FD)
+ req = io_poll_file_find(ctx, cd);
+ else
+ req = io_poll_find(ctx, false, cd);
if (!req)
return -ENOENT;
io_poll_cancel_req(req);
@@ -6751,8 +6782,13 @@ static bool io_cancel_cb(struct io_wq_work *work, void *data)
if (req->ctx != cd->ctx)
return false;
- if (req->cqe.user_data != cd->data)
- return false;
+ if (cd->flags & IORING_ASYNC_CANCEL_FD) {
+ if (req->file != cd->file)
+ return false;
+ } else {
+ if (req->cqe.user_data != cd->data)
+ return false;
+ }
if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
if (cd->seq == req->work.cancel_seq)
return false;
@@ -6807,9 +6843,11 @@ static int io_try_cancel(struct io_kiocb *req, struct io_cancel_data *cd)
if (ret != -ENOENT)
goto out;
- spin_lock_irq(&ctx->timeout_lock);
- ret = io_timeout_cancel(ctx, cd);
- spin_unlock_irq(&ctx->timeout_lock);
+ if (!(cd->flags & IORING_ASYNC_CANCEL_FD)) {
+ spin_lock_irq(&ctx->timeout_lock);
+ ret = io_timeout_cancel(ctx, cd);
+ spin_unlock_irq(&ctx->timeout_lock);
+ }
out:
spin_unlock(&ctx->completion_lock);
return ret;
@@ -6820,15 +6858,17 @@ static int io_async_cancel_prep(struct io_kiocb *req,
{
if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
return -EINVAL;
- if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
+ if (unlikely(req->flags & REQ_F_BUFFER_SELECT))
return -EINVAL;
if (sqe->ioprio || sqe->off || sqe->len || sqe->splice_fd_in)
return -EINVAL;
req->cancel.addr = READ_ONCE(sqe->addr);
req->cancel.flags = READ_ONCE(sqe->cancel_flags);
- if (req->cancel.flags & ~IORING_ASYNC_CANCEL_ALL)
+ if (req->cancel.flags & ~(IORING_ASYNC_CANCEL_ALL|IORING_ASYNC_CANCEL_FD))
return -EINVAL;
+ if (req->cancel.flags & IORING_ASYNC_CANCEL_FD)
+ req->cancel.fd = READ_ONCE(sqe->fd);
return 0;
}
@@ -6877,7 +6917,21 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
};
int ret;
+ if (cd.flags & IORING_ASYNC_CANCEL_FD) {
+ if (req->flags & REQ_F_FIXED_FILE)
+ req->file = io_file_get_fixed(req, req->cancel.fd,
+ issue_flags);
+ else
+ req->file = io_file_get_normal(req, req->cancel.fd);
+ if (!req->file) {
+ ret = -EBADF;
+ goto done;
+ }
+ cd.file = req->file;
+ }
+
ret = __io_async_cancel(&cd, req, issue_flags);
+done:
if (ret < 0)
req_set_fail(req);
io_req_complete_post(req, ret, 0);
diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h
index 476e58a2837f..cc7fe82a1798 100644
--- a/include/uapi/linux/io_uring.h
+++ b/include/uapi/linux/io_uring.h
@@ -191,8 +191,11 @@ enum {
* ASYNC_CANCEL flags.
*
* IORING_ASYNC_CANCEL_ALL Cancel all requests that match the given key
+ * IORING_ASYNC_CANCEL_FD Key off 'fd' for cancelation rather than the
+ * request 'user_data'
*/
#define IORING_ASYNC_CANCEL_ALL (1U << 0)
+#define IORING_ASYNC_CANCEL_FD (1U << 1)
/*
* IO completion data structure (Completion Queue Entry)
--
2.35.1
next prev parent reply other threads:[~2022-04-18 16:44 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-18 16:43 [PATCHSET v4 next 0/5] Extend cancelation support Jens Axboe
2022-04-18 16:43 ` [PATCH 1/5] io_uring: remove dead 'poll_only' argument to io_poll_cancel() Jens Axboe
2022-04-20 22:37 ` Jens Axboe
2022-04-18 16:43 ` [PATCH 2/5] io_uring: pass in struct io_cancel_data consistently Jens Axboe
2022-04-18 16:44 ` [PATCH 3/5] io_uring: add support for IORING_ASYNC_CANCEL_ALL Jens Axboe
2022-04-18 16:44 ` Jens Axboe [this message]
2022-04-18 16:44 ` [PATCH 5/5] io_uring: add support for IORING_ASYNC_CANCEL_ANY Jens Axboe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220418164402.75259-5-axboe@kernel.dk \
--to=axboe@kernel.dk \
--cc=io-uring@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.