Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #ifndef IOU_RSRC_H |
| 3 | #define IOU_RSRC_H |
| 4 | |
| 5 | #include <net/af_unix.h> |
| 6 | |
Pavel Begunkov | 9eae865 | 2023-04-04 13:39:54 +0100 | [diff] [blame] | 7 | #include "alloc_cache.h" |
| 8 | |
Pavel Begunkov | 69bbc6a | 2023-04-04 13:39:57 +0100 | [diff] [blame] | 9 | #define IO_NODE_ALLOC_CACHE_MAX 32 |
| 10 | |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 11 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
| 12 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
| 13 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) |
| 14 | |
| 15 | enum { |
| 16 | IORING_RSRC_FILE = 0, |
| 17 | IORING_RSRC_BUFFER = 1, |
| 18 | }; |
| 19 | |
| 20 | struct io_rsrc_put { |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 21 | u64 tag; |
| 22 | union { |
| 23 | void *rsrc; |
| 24 | struct file *file; |
| 25 | struct io_mapped_ubuf *buf; |
| 26 | }; |
| 27 | }; |
| 28 | |
| 29 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
| 30 | |
| 31 | struct io_rsrc_data { |
| 32 | struct io_ring_ctx *ctx; |
| 33 | |
| 34 | u64 **tags; |
| 35 | unsigned int nr; |
Pavel Begunkov | fc7f3a8 | 2023-04-18 14:06:40 +0100 | [diff] [blame] | 36 | u16 rsrc_type; |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 37 | bool quiesce; |
| 38 | }; |
| 39 | |
| 40 | struct io_rsrc_node { |
Pavel Begunkov | 9eae865 | 2023-04-04 13:39:54 +0100 | [diff] [blame] | 41 | union { |
| 42 | struct io_cache_entry cache; |
Pavel Begunkov | 2236b39 | 2023-04-18 14:06:41 +0100 | [diff] [blame] | 43 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9eae865 | 2023-04-04 13:39:54 +0100 | [diff] [blame] | 44 | }; |
Pavel Begunkov | ef8ae64 | 2023-04-04 13:39:49 +0100 | [diff] [blame] | 45 | int refs; |
Pavel Begunkov | 26147da | 2023-04-18 14:06:37 +0100 | [diff] [blame] | 46 | bool empty; |
Pavel Begunkov | 2236b39 | 2023-04-18 14:06:41 +0100 | [diff] [blame] | 47 | u16 type; |
Pavel Begunkov | c376644 | 2023-04-18 14:06:36 +0100 | [diff] [blame] | 48 | struct list_head node; |
| 49 | struct io_rsrc_put item; |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 50 | }; |
| 51 | |
Jens Axboe | ad163a7 | 2022-06-18 19:44:33 -0600 | [diff] [blame] | 52 | struct io_mapped_ubuf { |
| 53 | u64 ubuf; |
| 54 | u64 ubuf_end; |
| 55 | unsigned int nr_bvecs; |
| 56 | unsigned long acct_pages; |
Kees Cook | 04d9244 | 2023-08-17 14:21:47 -0700 | [diff] [blame] | 57 | struct bio_vec bvec[] __counted_by(nr_bvecs); |
Jens Axboe | ad163a7 | 2022-06-18 19:44:33 -0600 | [diff] [blame] | 58 | }; |
| 59 | |
Pavel Begunkov | b8fb5b4 | 2023-04-04 13:39:45 +0100 | [diff] [blame] | 60 | void io_rsrc_node_ref_zero(struct io_rsrc_node *node); |
Pavel Begunkov | 9eae865 | 2023-04-04 13:39:54 +0100 | [diff] [blame] | 61 | void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node); |
Pavel Begunkov | 2933ae6 | 2023-04-11 12:06:07 +0100 | [diff] [blame] | 62 | struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx); |
Pavel Begunkov | 63fea89 | 2023-04-18 14:06:35 +0100 | [diff] [blame] | 63 | int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, void *rsrc); |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 64 | |
Pavel Begunkov | c059f785 | 2022-06-20 01:25:59 +0100 | [diff] [blame] | 65 | int io_import_fixed(int ddir, struct iov_iter *iter, |
| 66 | struct io_mapped_ubuf *imu, |
| 67 | u64 buf_addr, size_t len); |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 68 | |
| 69 | void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx); |
| 70 | int io_sqe_buffers_unregister(struct io_ring_ctx *ctx); |
| 71 | int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
| 72 | unsigned int nr_args, u64 __user *tags); |
| 73 | void __io_sqe_files_unregister(struct io_ring_ctx *ctx); |
| 74 | int io_sqe_files_unregister(struct io_ring_ctx *ctx); |
| 75 | int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 76 | unsigned nr_args, u64 __user *tags); |
| 77 | |
| 78 | int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file); |
| 79 | |
| 80 | #if defined(CONFIG_UNIX) |
| 81 | static inline bool io_file_need_scm(struct file *filp) |
| 82 | { |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 83 | return !!unix_get_socket(filp); |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 84 | } |
| 85 | #else |
| 86 | static inline bool io_file_need_scm(struct file *filp) |
| 87 | { |
| 88 | return false; |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | static inline int io_scm_file_account(struct io_ring_ctx *ctx, |
| 93 | struct file *file) |
| 94 | { |
| 95 | if (likely(!io_file_need_scm(file))) |
| 96 | return 0; |
| 97 | return __io_scm_file_account(ctx, file); |
| 98 | } |
| 99 | |
| 100 | int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 101 | unsigned nr_args); |
| 102 | int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, |
| 103 | unsigned size, unsigned type); |
| 104 | int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
| 105 | unsigned int size, unsigned int type); |
| 106 | |
Pavel Begunkov | 1f2c8f6 | 2023-04-04 13:39:55 +0100 | [diff] [blame] | 107 | static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node) |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 108 | { |
Pavel Begunkov | 1f2c8f6 | 2023-04-04 13:39:55 +0100 | [diff] [blame] | 109 | lockdep_assert_held(&ctx->uring_lock); |
| 110 | |
Pavel Begunkov | ef8ae64 | 2023-04-04 13:39:49 +0100 | [diff] [blame] | 111 | if (node && !--node->refs) |
| 112 | io_rsrc_node_ref_zero(node); |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, |
| 116 | struct io_ring_ctx *ctx) |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 117 | { |
Pavel Begunkov | 1f2c8f6 | 2023-04-04 13:39:55 +0100 | [diff] [blame] | 118 | io_put_rsrc_node(ctx, req->rsrc_node); |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 119 | } |
| 120 | |
Pavel Begunkov | 8e15c0e | 2023-04-04 13:39:46 +0100 | [diff] [blame] | 121 | static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx, |
| 122 | struct io_rsrc_node *node) |
Pavel Begunkov | 68ef557 | 2022-07-12 21:52:41 +0100 | [diff] [blame] | 123 | { |
Pavel Begunkov | ef8ae64 | 2023-04-04 13:39:49 +0100 | [diff] [blame] | 124 | node->refs++; |
Pavel Begunkov | 68ef557 | 2022-07-12 21:52:41 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 127 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
| 128 | struct io_ring_ctx *ctx, |
| 129 | unsigned int issue_flags) |
| 130 | { |
| 131 | if (!req->rsrc_node) { |
Pavel Begunkov | 4ff0b50 | 2023-03-29 15:03:43 +0100 | [diff] [blame] | 132 | io_ring_submit_lock(ctx, issue_flags); |
| 133 | |
| 134 | lockdep_assert_held(&ctx->uring_lock); |
| 135 | |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 136 | req->rsrc_node = ctx->rsrc_node; |
Pavel Begunkov | 8e15c0e | 2023-04-04 13:39:46 +0100 | [diff] [blame] | 137 | io_charge_rsrc_node(ctx, ctx->rsrc_node); |
Pavel Begunkov | 4ff0b50 | 2023-03-29 15:03:43 +0100 | [diff] [blame] | 138 | io_ring_submit_unlock(ctx, issue_flags); |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
| 143 | { |
| 144 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; |
| 145 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; |
| 146 | |
| 147 | return &data->tags[table_idx][off]; |
| 148 | } |
| 149 | |
Pavel Begunkov | 2933ae6 | 2023-04-11 12:06:07 +0100 | [diff] [blame] | 150 | static inline int io_rsrc_init(struct io_ring_ctx *ctx) |
| 151 | { |
| 152 | ctx->rsrc_node = io_rsrc_node_alloc(ctx); |
| 153 | return ctx->rsrc_node ? 0 : -ENOMEM; |
| 154 | } |
| 155 | |
Pavel Begunkov | d9808ce | 2022-09-01 11:54:02 +0100 | [diff] [blame] | 156 | int io_files_update(struct io_kiocb *req, unsigned int issue_flags); |
| 157 | int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); |
Pavel Begunkov | 6a9ce66 | 2022-07-25 10:52:05 +0100 | [diff] [blame] | 158 | |
| 159 | int __io_account_mem(struct user_struct *user, unsigned long nr_pages); |
| 160 | |
| 161 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 162 | unsigned long nr_pages) |
| 163 | { |
| 164 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 165 | } |
| 166 | |
Jens Axboe | 7357298 | 2022-06-13 07:12:45 -0600 | [diff] [blame] | 167 | #endif |