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