1 #include <linux/kernel.h>
2 #include <linux/errno.h>
3 #include <linux/file.h>
4 #include <linux/slab.h>
6 #include <linux/io_uring.h>
12 static void __io_notif_complete_tw(struct callback_head *cb)
14 struct io_notif *notif = container_of(cb, struct io_notif, task_work);
15 struct io_rsrc_node *rsrc_node = notif->rsrc_node;
16 struct io_ring_ctx *ctx = notif->ctx;
18 if (likely(notif->task)) {
19 io_put_task(notif->task, 1);
24 io_fill_cqe_aux(ctx, notif->tag, 0, notif->seq, true);
26 list_add(¬if->cache_node, &ctx->notif_list_locked);
27 ctx->notif_locked_nr++;
28 io_cq_unlock_post(ctx);
30 io_rsrc_put_node(rsrc_node, 1);
31 percpu_ref_put(&ctx->refs);
34 static inline void io_notif_complete(struct io_notif *notif)
36 __io_notif_complete_tw(¬if->task_work);
39 static void io_notif_complete_wq(struct work_struct *work)
41 struct io_notif *notif = container_of(work, struct io_notif, commit_work);
43 io_notif_complete(notif);
46 static void io_uring_tx_zerocopy_callback(struct sk_buff *skb,
47 struct ubuf_info *uarg,
50 struct io_notif *notif = container_of(uarg, struct io_notif, uarg);
52 if (!refcount_dec_and_test(&uarg->refcnt))
55 if (likely(notif->task)) {
56 init_task_work(¬if->task_work, __io_notif_complete_tw);
57 if (likely(!task_work_add(notif->task, ¬if->task_work,
62 INIT_WORK(¬if->commit_work, io_notif_complete_wq);
63 queue_work(system_unbound_wq, ¬if->commit_work);
66 static void io_notif_splice_cached(struct io_ring_ctx *ctx)
67 __must_hold(&ctx->uring_lock)
69 spin_lock(&ctx->completion_lock);
70 list_splice_init(&ctx->notif_list_locked, &ctx->notif_list);
71 ctx->notif_locked_nr = 0;
72 spin_unlock(&ctx->completion_lock);
75 void io_notif_cache_purge(struct io_ring_ctx *ctx)
76 __must_hold(&ctx->uring_lock)
78 io_notif_splice_cached(ctx);
80 while (!list_empty(&ctx->notif_list)) {
81 struct io_notif *notif = list_first_entry(&ctx->notif_list,
82 struct io_notif, cache_node);
84 list_del(¬if->cache_node);
89 static inline bool io_notif_has_cached(struct io_ring_ctx *ctx)
90 __must_hold(&ctx->uring_lock)
92 if (likely(!list_empty(&ctx->notif_list)))
94 if (data_race(READ_ONCE(ctx->notif_locked_nr) <= IO_NOTIF_SPLICE_BATCH))
96 io_notif_splice_cached(ctx);
97 return !list_empty(&ctx->notif_list);
100 struct io_notif *io_alloc_notif(struct io_ring_ctx *ctx,
101 struct io_notif_slot *slot)
102 __must_hold(&ctx->uring_lock)
104 struct io_notif *notif;
106 if (likely(io_notif_has_cached(ctx))) {
107 notif = list_first_entry(&ctx->notif_list,
108 struct io_notif, cache_node);
109 list_del(¬if->cache_node);
111 notif = kzalloc(sizeof(*notif), GFP_ATOMIC | __GFP_ACCOUNT);
114 /* pre-initialise some fields */
116 notif->uarg.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN;
117 notif->uarg.callback = io_uring_tx_zerocopy_callback;
120 notif->seq = slot->seq++;
121 notif->tag = slot->tag;
122 /* master ref owned by io_notif_slot, will be dropped on flush */
123 refcount_set(¬if->uarg.refcnt, 1);
124 percpu_ref_get(&ctx->refs);
125 notif->rsrc_node = ctx->rsrc_node;
126 io_charge_rsrc_node(ctx);
130 static void io_notif_slot_flush(struct io_notif_slot *slot)
131 __must_hold(&ctx->uring_lock)
133 struct io_notif *notif = slot->notif;
137 if (WARN_ON_ONCE(in_interrupt()))
139 /* drop slot's master ref */
140 if (refcount_dec_and_test(¬if->uarg.refcnt))
141 io_notif_complete(notif);
144 __cold int io_notif_unregister(struct io_ring_ctx *ctx)
145 __must_hold(&ctx->uring_lock)
149 if (!ctx->notif_slots)
152 for (i = 0; i < ctx->nr_notif_slots; i++) {
153 struct io_notif_slot *slot = &ctx->notif_slots[i];
157 if (WARN_ON_ONCE(slot->notif->task))
158 slot->notif->task = NULL;
159 io_notif_slot_flush(slot);
162 kvfree(ctx->notif_slots);
163 ctx->notif_slots = NULL;
164 ctx->nr_notif_slots = 0;