1eb42cebbSPavel Begunkov #include <linux/kernel.h>
2eb42cebbSPavel Begunkov #include <linux/errno.h>
3eb42cebbSPavel Begunkov #include <linux/file.h>
4eb42cebbSPavel Begunkov #include <linux/slab.h>
5eb42cebbSPavel Begunkov #include <linux/net.h>
6eb42cebbSPavel Begunkov #include <linux/io_uring.h>
7eb42cebbSPavel Begunkov
8eb42cebbSPavel Begunkov #include "io_uring.h"
9eb42cebbSPavel Begunkov #include "notif.h"
1068ef5578SPavel Begunkov #include "rsrc.h"
11eb42cebbSPavel Begunkov
io_notif_complete_tw_ext(struct io_kiocb * notif,struct io_tw_state * ts)12a282967cSPavel Begunkov static void io_notif_complete_tw_ext(struct io_kiocb *notif, struct io_tw_state *ts)
13eb42cebbSPavel Begunkov {
1414b146b6SPavel Begunkov struct io_notif_data *nd = io_notif_to_data(notif);
15eb42cebbSPavel Begunkov struct io_ring_ctx *ctx = notif->ctx;
16eb42cebbSPavel Begunkov
1742385b02SPavel Begunkov if (nd->zc_report && (nd->zc_copied || !nd->zc_used))
1842385b02SPavel Begunkov notif->cqe.res |= IORING_NOTIF_USAGE_ZC_COPIED;
1942385b02SPavel Begunkov
2014b146b6SPavel Begunkov if (nd->account_pages && ctx->user) {
2114b146b6SPavel Begunkov __io_unaccount_mem(ctx->user, nd->account_pages);
2214b146b6SPavel Begunkov nd->account_pages = 0;
23e29e3bd4SPavel Begunkov }
24a282967cSPavel Begunkov io_req_task_complete(notif, ts);
2540725d1bSPavel Begunkov }
2640725d1bSPavel Begunkov
io_tx_ubuf_callback(struct sk_buff * skb,struct ubuf_info * uarg,bool success)277fa8e841SPavel Begunkov static void io_tx_ubuf_callback(struct sk_buff *skb, struct ubuf_info *uarg,
28eb42cebbSPavel Begunkov bool success)
29eb42cebbSPavel Begunkov {
3014b146b6SPavel Begunkov struct io_notif_data *nd = container_of(uarg, struct io_notif_data, uarg);
3114b146b6SPavel Begunkov struct io_kiocb *notif = cmd_to_io_kiocb(nd);
32eb42cebbSPavel Begunkov
3340725d1bSPavel Begunkov if (refcount_dec_and_test(&uarg->refcnt))
348751d154SPavel Begunkov __io_req_task_work_add(notif, IOU_F_TWQ_LAZY_WAKE);
3540725d1bSPavel Begunkov }
3640725d1bSPavel Begunkov
io_tx_ubuf_callback_ext(struct sk_buff * skb,struct ubuf_info * uarg,bool success)3740725d1bSPavel Begunkov static void io_tx_ubuf_callback_ext(struct sk_buff *skb, struct ubuf_info *uarg,
3840725d1bSPavel Begunkov bool success)
3940725d1bSPavel Begunkov {
4040725d1bSPavel Begunkov struct io_notif_data *nd = container_of(uarg, struct io_notif_data, uarg);
4140725d1bSPavel Begunkov
42e307e669SStefan Metzmacher if (nd->zc_report) {
43e307e669SStefan Metzmacher if (success && !nd->zc_used && skb)
44e307e669SStefan Metzmacher WRITE_ONCE(nd->zc_used, true);
45e307e669SStefan Metzmacher else if (!success && !nd->zc_copied)
46e307e669SStefan Metzmacher WRITE_ONCE(nd->zc_copied, true);
47e307e669SStefan Metzmacher }
4840725d1bSPavel Begunkov io_tx_ubuf_callback(skb, uarg, success);
4940725d1bSPavel Begunkov }
50e307e669SStefan Metzmacher
io_notif_set_extended(struct io_kiocb * notif)5140725d1bSPavel Begunkov void io_notif_set_extended(struct io_kiocb *notif)
5240725d1bSPavel Begunkov {
5340725d1bSPavel Begunkov struct io_notif_data *nd = io_notif_to_data(notif);
5440725d1bSPavel Begunkov
5542385b02SPavel Begunkov if (nd->uarg.callback != io_tx_ubuf_callback_ext) {
5642385b02SPavel Begunkov nd->account_pages = 0;
5740725d1bSPavel Begunkov nd->zc_report = false;
5840725d1bSPavel Begunkov nd->zc_used = false;
5940725d1bSPavel Begunkov nd->zc_copied = false;
6042385b02SPavel Begunkov nd->uarg.callback = io_tx_ubuf_callback_ext;
6140725d1bSPavel Begunkov notif->io_task_work.func = io_notif_complete_tw_ext;
6242385b02SPavel Begunkov }
63eb4a299bSPavel Begunkov }
64eb4a299bSPavel Begunkov
io_alloc_notif(struct io_ring_ctx * ctx)65b48c312bSPavel Begunkov struct io_kiocb *io_alloc_notif(struct io_ring_ctx *ctx)
66eb42cebbSPavel Begunkov __must_hold(&ctx->uring_lock)
67eb42cebbSPavel Begunkov {
6814b146b6SPavel Begunkov struct io_kiocb *notif;
6914b146b6SPavel Begunkov struct io_notif_data *nd;
70eb42cebbSPavel Begunkov
71c8576f3eSPavel Begunkov if (unlikely(!io_alloc_req(ctx, ¬if)))
72eb42cebbSPavel Begunkov return NULL;
7314b146b6SPavel Begunkov notif->opcode = IORING_OP_NOP;
7414b146b6SPavel Begunkov notif->flags = 0;
7514b146b6SPavel Begunkov notif->file = NULL;
7614b146b6SPavel Begunkov notif->task = current;
7714b146b6SPavel Begunkov io_get_task_refs(1);
7814b146b6SPavel Begunkov notif->rsrc_node = NULL;
7942385b02SPavel Begunkov notif->io_task_work.func = io_req_task_complete;
80eb4a299bSPavel Begunkov
8114b146b6SPavel Begunkov nd = io_notif_to_data(notif);
82*519760dfSPavel Begunkov nd->uarg.flags = IO_NOTIF_UBUF_FLAGS;
837fa8e841SPavel Begunkov nd->uarg.callback = io_tx_ubuf_callback;
8414b146b6SPavel Begunkov refcount_set(&nd->uarg.refcnt, 1);
85eb42cebbSPavel Begunkov return notif;
86eb42cebbSPavel Begunkov }
87