#include "io_uring.h"
#include "sqpoll.h"
#include "fdinfo.h"
+#include "cancel.h"
#ifdef CONFIG_PROC_FS
static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id,
mutex_unlock(&ctx->uring_lock);
seq_puts(m, "PollList:\n");
- spin_lock(&ctx->completion_lock);
for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
- struct hlist_head *list = &ctx->cancel_hash[i];
+ struct io_hash_bucket *hb = &ctx->cancel_hash[i];
struct io_kiocb *req;
- hlist_for_each_entry(req, list, hash_node)
+ spin_lock(&hb->lock);
+ hlist_for_each_entry(req, &hb->list, hash_node)
seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
task_work_pending(req->task));
+ spin_unlock(&hb->lock);
}
seq_puts(m, "CqOverflowList:\n");
+ spin_lock(&ctx->completion_lock);
list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
struct io_uring_cqe *cqe = &ocqe->cqe;
#include "opdef.h"
#include "kbuf.h"
#include "poll.h"
+#include "cancel.h"
struct io_poll_update {
struct file *file;
static void io_poll_req_insert(struct io_kiocb *req)
{
struct io_ring_ctx *ctx = req->ctx;
- struct hlist_head *list;
+ u32 index = hash_long(req->cqe.user_data, ctx->cancel_hash_bits);
+ struct io_hash_bucket *hb = &ctx->cancel_hash[index];
- list = &ctx->cancel_hash[hash_long(req->cqe.user_data, ctx->cancel_hash_bits)];
- hlist_add_head(&req->hash_node, list);
+ spin_lock(&hb->lock);
+ hlist_add_head(&req->hash_node, &hb->list);
+ spin_unlock(&hb->lock);
+}
+
+static void io_poll_req_delete(struct io_kiocb *req, struct io_ring_ctx *ctx)
+{
+ u32 index = hash_long(req->cqe.user_data, ctx->cancel_hash_bits);
+ spinlock_t *lock = &ctx->cancel_hash[index].lock;
+
+ spin_lock(lock);
+ hash_del(&req->hash_node);
+ spin_unlock(lock);
}
static void io_init_poll_iocb(struct io_poll *poll, __poll_t events,
}
io_poll_remove_entries(req);
+ io_poll_req_delete(req, ctx);
spin_lock(&ctx->completion_lock);
- hash_del(&req->hash_node);
req->cqe.flags = 0;
__io_req_complete_post(req);
io_commit_cqring(ctx);
static void io_apoll_task_func(struct io_kiocb *req, bool *locked)
{
- struct io_ring_ctx *ctx = req->ctx;
int ret;
ret = io_poll_check_events(req, locked);
return;
io_poll_remove_entries(req);
- spin_lock(&ctx->completion_lock);
- hash_del(&req->hash_node);
- spin_unlock(&ctx->completion_lock);
+ io_poll_req_delete(req, req->ctx);
if (!ret)
io_req_task_submit(req, locked);
return 0;
}
- spin_lock(&ctx->completion_lock);
io_poll_req_insert(req);
- spin_unlock(&ctx->completion_lock);
if (mask && (poll->events & EPOLLET)) {
/* can't multishot if failed, just queue the event we've got */
bool found = false;
int i;
- spin_lock(&ctx->completion_lock);
for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
- struct hlist_head *list;
+ struct io_hash_bucket *hb = &ctx->cancel_hash[i];
- list = &ctx->cancel_hash[i];
- hlist_for_each_entry_safe(req, tmp, list, hash_node) {
+ spin_lock(&hb->lock);
+ hlist_for_each_entry_safe(req, tmp, &hb->list, hash_node) {
if (io_match_task_safe(req, tsk, cancel_all)) {
hlist_del_init(&req->hash_node);
io_poll_cancel_req(req);
found = true;
}
}
+ spin_unlock(&hb->lock);
}
- spin_unlock(&ctx->completion_lock);
return found;
}
static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
struct io_cancel_data *cd)
- __must_hold(&ctx->completion_lock)
{
- struct hlist_head *list;
struct io_kiocb *req;
+ u32 index = hash_long(cd->data, ctx->cancel_hash_bits);
+ struct io_hash_bucket *hb = &ctx->cancel_hash[index];
- list = &ctx->cancel_hash[hash_long(cd->data, ctx->cancel_hash_bits)];
- hlist_for_each_entry(req, list, hash_node) {
+ spin_lock(&hb->lock);
+ hlist_for_each_entry(req, &hb->list, hash_node) {
if (cd->data != req->cqe.user_data)
continue;
if (poll_only && req->opcode != IORING_OP_POLL_ADD)
}
return req;
}
+ spin_unlock(&hb->lock);
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;
+ struct io_hash_bucket *hb = &ctx->cancel_hash[i];
- list = &ctx->cancel_hash[i];
- hlist_for_each_entry(req, list, hash_node) {
+ spin_lock(&hb->lock);
+ hlist_for_each_entry(req, &hb->list, hash_node) {
if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) &&
req->file != cd->file)
continue;
req->work.cancel_seq = cd->seq;
return req;
}
+ spin_unlock(&hb->lock);
}
return NULL;
}
static bool io_poll_disarm(struct io_kiocb *req)
- __must_hold(&ctx->completion_lock)
{
if (!io_poll_get_ownership(req))
return false;
}
int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
- __must_hold(&ctx->completion_lock)
{
struct io_kiocb *req;
+ u32 index;
+ spinlock_t *lock;
if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_ANY))
req = io_poll_file_find(ctx, cd);
else
req = io_poll_find(ctx, false, cd);
- if (!req)
+ if (!req) {
return -ENOENT;
+ } else {
+ index = hash_long(req->cqe.user_data, ctx->cancel_hash_bits);
+ lock = &ctx->cancel_hash[index].lock;
+ }
io_poll_cancel_req(req);
+ spin_unlock(lock);
return 0;
}
struct io_poll_update *poll_update = io_kiocb_to_cmd(req);
struct io_cancel_data cd = { .data = poll_update->old_user_data, };
struct io_ring_ctx *ctx = req->ctx;
+ u32 index = hash_long(cd.data, ctx->cancel_hash_bits);
+ spinlock_t *lock = &ctx->cancel_hash[index].lock;
struct io_kiocb *preq;
int ret2, ret = 0;
bool locked;
- spin_lock(&ctx->completion_lock);
preq = io_poll_find(ctx, true, &cd);
- if (!preq || !io_poll_disarm(preq)) {
- spin_unlock(&ctx->completion_lock);
- ret = preq ? -EALREADY : -ENOENT;
+ if (!preq) {
+ ret = -ENOENT;
+ goto out;
+ }
+ ret2 = io_poll_disarm(preq);
+ spin_unlock(lock);
+ if (!ret2) {
+ ret = -EALREADY;
goto out;
}
- spin_unlock(&ctx->completion_lock);
if (poll_update->update_events || poll_update->update_user_data) {
/* only mask one event flags, keep behavior flags */