Pavel Begunkov | eb42ceb | 2022-07-12 21:52:38 +0100 | [diff] [blame] | 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" |
Pavel Begunkov | 68ef557 | 2022-07-12 21:52:41 +0100 | [diff] [blame] | 10 | #include "rsrc.h" |
Pavel Begunkov | eb42ceb | 2022-07-12 21:52:38 +0100 | [diff] [blame] | 11 | |
Pavel Begunkov | 42385b0 | 2022-11-04 10:59:46 +0000 | [diff] [blame] | 12 | static void io_notif_complete_tw_ext(struct io_kiocb *notif, bool *locked) |
Pavel Begunkov | eb42ceb | 2022-07-12 21:52:38 +0100 | [diff] [blame] | 13 | { |
Pavel Begunkov | 14b146b | 2022-07-27 10:30:41 +0100 | [diff] [blame] | 14 | struct io_notif_data *nd = io_notif_to_data(notif); |
Pavel Begunkov | eb42ceb | 2022-07-12 21:52:38 +0100 | [diff] [blame] | 15 | struct io_ring_ctx *ctx = notif->ctx; |
| 16 | |
Pavel Begunkov | 42385b0 | 2022-11-04 10:59:46 +0000 | [diff] [blame] | 17 | if (nd->zc_report && (nd->zc_copied || !nd->zc_used)) |
| 18 | notif->cqe.res |= IORING_NOTIF_USAGE_ZC_COPIED; |
| 19 | |
Pavel Begunkov | 14b146b | 2022-07-27 10:30:41 +0100 | [diff] [blame] | 20 | if (nd->account_pages && ctx->user) { |
| 21 | __io_unaccount_mem(ctx->user, nd->account_pages); |
| 22 | nd->account_pages = 0; |
Pavel Begunkov | e29e3bd | 2022-07-12 21:52:44 +0100 | [diff] [blame] | 23 | } |
Pavel Begunkov | 40725d1 | 2022-11-04 10:59:45 +0000 | [diff] [blame] | 24 | io_req_task_complete(notif, locked); |
| 25 | } |
| 26 | |
Pavel Begunkov | 7fa8e84 | 2022-11-04 10:59:43 +0000 | [diff] [blame] | 27 | static void io_tx_ubuf_callback(struct sk_buff *skb, struct ubuf_info *uarg, |
| 28 | bool success) |
Pavel Begunkov | eb42ceb | 2022-07-12 21:52:38 +0100 | [diff] [blame] | 29 | { |
Pavel Begunkov | 14b146b | 2022-07-27 10:30:41 +0100 | [diff] [blame] | 30 | struct io_notif_data *nd = container_of(uarg, struct io_notif_data, uarg); |
| 31 | struct io_kiocb *notif = cmd_to_io_kiocb(nd); |
Pavel Begunkov | eb42ceb | 2022-07-12 21:52:38 +0100 | [diff] [blame] | 32 | |
Pavel Begunkov | 40725d1 | 2022-11-04 10:59:45 +0000 | [diff] [blame] | 33 | if (refcount_dec_and_test(&uarg->refcnt)) |
| 34 | io_req_task_work_add(notif); |
| 35 | } |
| 36 | |
| 37 | static 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 | |
Stefan Metzmacher | e307e66 | 2022-10-27 20:34:45 +0200 | [diff] [blame] | 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 | } |
Pavel Begunkov | 40725d1 | 2022-11-04 10:59:45 +0000 | [diff] [blame] | 48 | io_tx_ubuf_callback(skb, uarg, success); |
| 49 | } |
Stefan Metzmacher | e307e66 | 2022-10-27 20:34:45 +0200 | [diff] [blame] | 50 | |
Pavel Begunkov | 40725d1 | 2022-11-04 10:59:45 +0000 | [diff] [blame] | 51 | void io_notif_set_extended(struct io_kiocb *notif) |
| 52 | { |
| 53 | struct io_notif_data *nd = io_notif_to_data(notif); |
| 54 | |
Pavel Begunkov | 42385b0 | 2022-11-04 10:59:46 +0000 | [diff] [blame] | 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 | } |
Pavel Begunkov | eb4a299 | 2022-07-12 21:52:39 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Pavel Begunkov | b48c312 | 2022-09-01 11:54:04 +0100 | [diff] [blame] | 65 | struct io_kiocb *io_alloc_notif(struct io_ring_ctx *ctx) |
Pavel Begunkov | eb42ceb | 2022-07-12 21:52:38 +0100 | [diff] [blame] | 66 | __must_hold(&ctx->uring_lock) |
| 67 | { |
Pavel Begunkov | 14b146b | 2022-07-27 10:30:41 +0100 | [diff] [blame] | 68 | struct io_kiocb *notif; |
| 69 | struct io_notif_data *nd; |
Pavel Begunkov | eb42ceb | 2022-07-12 21:52:38 +0100 | [diff] [blame] | 70 | |
Pavel Begunkov | 14b146b | 2022-07-27 10:30:41 +0100 | [diff] [blame] | 71 | if (unlikely(!io_alloc_req_refill(ctx))) |
| 72 | return NULL; |
| 73 | notif = io_alloc_req(ctx); |
| 74 | notif->opcode = IORING_OP_NOP; |
| 75 | notif->flags = 0; |
| 76 | notif->file = NULL; |
| 77 | notif->task = current; |
| 78 | io_get_task_refs(1); |
| 79 | notif->rsrc_node = NULL; |
Pavel Begunkov | 42385b0 | 2022-11-04 10:59:46 +0000 | [diff] [blame] | 80 | notif->io_task_work.func = io_req_task_complete; |
Pavel Begunkov | eb42ceb | 2022-07-12 21:52:38 +0100 | [diff] [blame] | 81 | |
Pavel Begunkov | 14b146b | 2022-07-27 10:30:41 +0100 | [diff] [blame] | 82 | nd = io_notif_to_data(notif); |
Pavel Begunkov | 14b146b | 2022-07-27 10:30:41 +0100 | [diff] [blame] | 83 | nd->uarg.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN; |
Pavel Begunkov | 7fa8e84 | 2022-11-04 10:59:43 +0000 | [diff] [blame] | 84 | nd->uarg.callback = io_tx_ubuf_callback; |
Pavel Begunkov | 14b146b | 2022-07-27 10:30:41 +0100 | [diff] [blame] | 85 | refcount_set(&nd->uarg.refcnt, 1); |
Pavel Begunkov | eb42ceb | 2022-07-12 21:52:38 +0100 | [diff] [blame] | 86 | return notif; |
| 87 | } |