Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma
[linux-block.git] / io_uring / notif.c
CommitLineData
eb42cebb
PB
1#include <linux/kernel.h>
2#include <linux/errno.h>
3#include <linux/file.h>
4#include <linux/slab.h>
5#include <linux/net.h>
6#include <linux/io_uring.h>
7
8#include "io_uring.h"
9#include "notif.h"
68ef5578 10#include "rsrc.h"
eb42cebb 11
a282967c 12static void io_notif_complete_tw_ext(struct io_kiocb *notif, struct io_tw_state *ts)
eb42cebb 13{
14b146b6 14 struct io_notif_data *nd = io_notif_to_data(notif);
eb42cebb
PB
15 struct io_ring_ctx *ctx = notif->ctx;
16
42385b02
PB
17 if (nd->zc_report && (nd->zc_copied || !nd->zc_used))
18 notif->cqe.res |= IORING_NOTIF_USAGE_ZC_COPIED;
19
14b146b6
PB
20 if (nd->account_pages && ctx->user) {
21 __io_unaccount_mem(ctx->user, nd->account_pages);
22 nd->account_pages = 0;
e29e3bd4 23 }
a282967c 24 io_req_task_complete(notif, ts);
40725d1b
PB
25}
26
7fa8e841
PB
27static void io_tx_ubuf_callback(struct sk_buff *skb, struct ubuf_info *uarg,
28 bool success)
eb42cebb 29{
14b146b6
PB
30 struct io_notif_data *nd = container_of(uarg, struct io_notif_data, uarg);
31 struct io_kiocb *notif = cmd_to_io_kiocb(nd);
eb4a299b 32
40725d1b 33 if (refcount_dec_and_test(&uarg->refcnt))
8751d154 34 __io_req_task_work_add(notif, IOU_F_TWQ_LAZY_WAKE);
40725d1b
PB
35}
36
37static void io_tx_ubuf_callback_ext(struct sk_buff *skb, struct ubuf_info *uarg,
38 bool success)
39{
40 struct io_notif_data *nd = container_of(uarg, struct io_notif_data, uarg);
41
e307e669
SM
42 if (nd->zc_report) {
43 if (success && !nd->zc_used && skb)
44 WRITE_ONCE(nd->zc_used, true);
45 else if (!success && !nd->zc_copied)
46 WRITE_ONCE(nd->zc_copied, true);
47 }
40725d1b
PB
48 io_tx_ubuf_callback(skb, uarg, success);
49}
e307e669 50
40725d1b
PB
51void io_notif_set_extended(struct io_kiocb *notif)
52{
53 struct io_notif_data *nd = io_notif_to_data(notif);
54
42385b02
PB
55 if (nd->uarg.callback != io_tx_ubuf_callback_ext) {
56 nd->account_pages = 0;
57 nd->zc_report = false;
58 nd->zc_used = false;
59 nd->zc_copied = false;
60 nd->uarg.callback = io_tx_ubuf_callback_ext;
61 notif->io_task_work.func = io_notif_complete_tw_ext;
62 }
eb4a299b
PB
63}
64
b48c312b 65struct io_kiocb *io_alloc_notif(struct io_ring_ctx *ctx)
eb42cebb
PB
66 __must_hold(&ctx->uring_lock)
67{
14b146b6
PB
68 struct io_kiocb *notif;
69 struct io_notif_data *nd;
70
c8576f3e 71 if (unlikely(!io_alloc_req(ctx, &notif)))
14b146b6 72 return NULL;
14b146b6
PB
73 notif->opcode = IORING_OP_NOP;
74 notif->flags = 0;
75 notif->file = NULL;
76 notif->task = current;
77 io_get_task_refs(1);
78 notif->rsrc_node = NULL;
42385b02 79 notif->io_task_work.func = io_req_task_complete;
14b146b6
PB
80
81 nd = io_notif_to_data(notif);
519760df 82 nd->uarg.flags = IO_NOTIF_UBUF_FLAGS;
7fa8e841 83 nd->uarg.callback = io_tx_ubuf_callback;
14b146b6 84 refcount_set(&nd->uarg.refcnt, 1);
eb42cebb
PB
85 return notif;
86}