io_uring/rsrc: add lockdep sanity checks
[platform/kernel/linux-starfive.git] / io_uring / rsrc.c
index f2da9e2..d4bca5e 100644 (file)
@@ -27,21 +27,10 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
                                  struct io_mapped_ubuf **pimu,
                                  struct page **last_hpage);
 
-#define IO_RSRC_REF_BATCH      100
-
 /* only define max */
 #define IORING_MAX_FIXED_FILES (1U << 20)
 #define IORING_MAX_REG_BUFFERS (1U << 14)
 
-void io_rsrc_refs_drop(struct io_ring_ctx *ctx)
-       __must_hold(&ctx->uring_lock)
-{
-       if (ctx->rsrc_cached_refs) {
-               io_rsrc_put_node(ctx->rsrc_node, ctx->rsrc_cached_refs);
-               ctx->rsrc_cached_refs = 0;
-       }
-}
-
 int __io_account_mem(struct user_struct *user, unsigned long nr_pages)
 {
        unsigned long page_limit, cur_pages, new_pages;
@@ -151,126 +140,84 @@ static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slo
        *slot = NULL;
 }
 
-void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
-       __must_hold(&ctx->uring_lock)
+static void io_rsrc_put_work_one(struct io_rsrc_data *rsrc_data,
+                                struct io_rsrc_put *prsrc)
 {
-       ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH;
-       refcount_add(IO_RSRC_REF_BATCH, &ctx->rsrc_node->refs);
+       struct io_ring_ctx *ctx = rsrc_data->ctx;
+
+       if (prsrc->tag)
+               io_post_aux_cqe(ctx, prsrc->tag, 0, 0);
+       rsrc_data->do_put(ctx, prsrc);
 }
 
 static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
 {
        struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
-       struct io_ring_ctx *ctx = rsrc_data->ctx;
        struct io_rsrc_put *prsrc, *tmp;
 
-       list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
-               list_del(&prsrc->list);
-
-               if (prsrc->tag) {
-                       if (ctx->flags & IORING_SETUP_IOPOLL) {
-                               mutex_lock(&ctx->uring_lock);
-                               io_post_aux_cqe(ctx, prsrc->tag, 0, 0);
-                               mutex_unlock(&ctx->uring_lock);
-                       } else {
-                               io_post_aux_cqe(ctx, prsrc->tag, 0, 0);
-                       }
-               }
+       if (ref_node->inline_items)
+               io_rsrc_put_work_one(rsrc_data, &ref_node->item);
 
-               rsrc_data->do_put(ctx, prsrc);
+       list_for_each_entry_safe(prsrc, tmp, &ref_node->item_list, list) {
+               list_del(&prsrc->list);
+               io_rsrc_put_work_one(rsrc_data, prsrc);
                kfree(prsrc);
        }
 
-       io_rsrc_node_destroy(ref_node);
+       io_rsrc_node_destroy(rsrc_data->ctx, ref_node);
        if (atomic_dec_and_test(&rsrc_data->refs))
                complete(&rsrc_data->done);
 }
 
-void io_rsrc_put_work(struct work_struct *work)
-{
-       struct io_ring_ctx *ctx;
-       struct llist_node *node;
-
-       ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
-       node = llist_del_all(&ctx->rsrc_put_llist);
-
-       while (node) {
-               struct io_rsrc_node *ref_node;
-               struct llist_node *next = node->next;
-
-               ref_node = llist_entry(node, struct io_rsrc_node, llist);
-               __io_rsrc_put_work(ref_node);
-               node = next;
-       }
-}
-
-void io_rsrc_put_tw(struct callback_head *cb)
-{
-       struct io_ring_ctx *ctx = container_of(cb, struct io_ring_ctx,
-                                              rsrc_put_tw);
-
-       io_rsrc_put_work(&ctx->rsrc_put_work.work);
-}
-
 void io_wait_rsrc_data(struct io_rsrc_data *data)
 {
        if (data && !atomic_dec_and_test(&data->refs))
                wait_for_completion(&data->done);
 }
 
-void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
+void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
 {
-       kfree(ref_node);
+       if (!io_alloc_cache_put(&ctx->rsrc_node_cache, &node->cache))
+               kfree(node);
 }
 
-__cold void io_rsrc_node_ref_zero(struct io_rsrc_node *node)
+void io_rsrc_node_ref_zero(struct io_rsrc_node *node)
+       __must_hold(&node->rsrc_data->ctx->uring_lock)
 {
        struct io_ring_ctx *ctx = node->rsrc_data->ctx;
-       unsigned long flags;
-       bool first_add = false;
-       unsigned long delay = HZ;
 
-       spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
        node->done = true;
-
-       /* if we are mid-quiesce then do not delay */
-       if (node->rsrc_data->quiesce)
-               delay = 0;
-
        while (!list_empty(&ctx->rsrc_ref_list)) {
                node = list_first_entry(&ctx->rsrc_ref_list,
                                            struct io_rsrc_node, node);
                /* recycle ref nodes in order */
                if (!node->done)
                        break;
-               list_del(&node->node);
-               first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
-       }
-       spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
-
-       if (!first_add)
-               return;
 
-       if (ctx->submitter_task) {
-               if (!task_work_add(ctx->submitter_task, &ctx->rsrc_put_tw,
-                                  ctx->notify_method))
-                       return;
+               list_del(&node->node);
+               __io_rsrc_put_work(node);
        }
-       mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
 }
 
-static struct io_rsrc_node *io_rsrc_node_alloc(void)
+static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
 {
        struct io_rsrc_node *ref_node;
+       struct io_cache_entry *entry;
 
-       ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
-       if (!ref_node)
-               return NULL;
+       entry = io_alloc_cache_get(&ctx->rsrc_node_cache);
+       if (entry) {
+               ref_node = container_of(entry, struct io_rsrc_node, cache);
+       } else {
+               ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
+               if (!ref_node)
+                       return NULL;
+       }
 
-       refcount_set(&ref_node->refs, 1);
+       ref_node->refs = 1;
        INIT_LIST_HEAD(&ref_node->node);
-       INIT_LIST_HEAD(&ref_node->rsrc_list);
+       INIT_LIST_HEAD(&ref_node->item_list);
        ref_node->done = false;
+       ref_node->inline_items = 0;
        return ref_node;
 }
 
@@ -281,19 +228,15 @@ void io_rsrc_node_switch(struct io_ring_ctx *ctx,
        WARN_ON_ONCE(!ctx->rsrc_backup_node);
        WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
 
-       io_rsrc_refs_drop(ctx);
-
        if (data_to_kill) {
                struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
 
                rsrc_node->rsrc_data = data_to_kill;
-               spin_lock_irq(&ctx->rsrc_ref_lock);
                list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
-               spin_unlock_irq(&ctx->rsrc_ref_lock);
 
                atomic_inc(&data_to_kill->refs);
                /* put master ref */
-               io_rsrc_put_node(rsrc_node, 1);
+               io_put_rsrc_node(ctx, rsrc_node);
                ctx->rsrc_node = NULL;
        }
 
@@ -307,7 +250,7 @@ int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
 {
        if (ctx->rsrc_backup_node)
                return 0;
-       ctx->rsrc_backup_node = io_rsrc_node_alloc();
+       ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
        return ctx->rsrc_backup_node ? 0 : -ENOMEM;
 }
 
@@ -335,13 +278,11 @@ __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
                if (ret < 0) {
                        atomic_inc(&data->refs);
                        /* wait for all works potentially completing data->done */
-                       flush_delayed_work(&ctx->rsrc_put_work);
                        reinit_completion(&data->done);
                        mutex_lock(&ctx->uring_lock);
                        break;
                }
 
-               flush_delayed_work(&ctx->rsrc_put_work);
                ret = wait_for_completion_interruptible(&data->done);
                if (!ret) {
                        mutex_lock(&ctx->uring_lock);
@@ -753,15 +694,23 @@ int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
 {
        u64 *tag_slot = io_get_tag_slot(data, idx);
        struct io_rsrc_put *prsrc;
+       bool inline_item = true;
 
-       prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
-       if (!prsrc)
-               return -ENOMEM;
+       if (!node->inline_items) {
+               prsrc = &node->item;
+               node->inline_items++;
+       } else {
+               prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
+               if (!prsrc)
+                       return -ENOMEM;
+               inline_item = false;
+       }
 
        prsrc->tag = *tag_slot;
        *tag_slot = 0;
        prsrc->rsrc = rsrc;
-       list_add(&prsrc->list, &node->rsrc_list);
+       if (!inline_item)
+               list_add(&prsrc->list, &node->item_list);
        return 0;
 }