| 1 | |
| 2 | #ifndef IOU_RSRC_H
|
| 3 | #define IOU_RSRC_H
|
| 4 |
|
| 5 | #include <linux/bvec.h>
|
| 6 | #include <linux/io_uring_types.h>
|
| 7 | #include <linux/lockdep.h>
|
| 8 | #include <linux/uio.h>
|
| 9 |
|
| 10 | #define IO_VEC_CACHE_SOFT_CAP 256
|
| 11 |
|
| 12 | enum {
|
| 13 | IORING_RSRC_FILE = 0,
|
| 14 | IORING_RSRC_BUFFER = 1,
|
| 15 | };
|
| 16 |
|
| 17 | struct io_rsrc_node {
|
| 18 | unsigned char type;
|
| 19 | int refs;
|
| 20 |
|
| 21 | u64 tag;
|
| 22 | union {
|
| 23 | unsigned long file_ptr;
|
| 24 | struct io_mapped_ubuf *buf;
|
| 25 | };
|
| 26 | };
|
| 27 |
|
| 28 | enum {
|
| 29 | IO_REGBUF_F_KBUF = 1,
|
| 30 | };
|
| 31 |
|
| 32 | struct io_mapped_ubuf {
|
| 33 | u64 ubuf;
|
| 34 | size_t len;
|
| 35 | unsigned int nr_bvecs;
|
| 36 | unsigned int folio_shift;
|
| 37 | refcount_t refs;
|
| 38 | u8 flags;
|
| 39 | u8 dir;
|
| 40 | void (*release)(void *);
|
| 41 | void *priv;
|
| 42 | struct bio_vec bvec[] __counted_by(nr_bvecs);
|
| 43 | };
|
| 44 |
|
| 45 | struct io_imu_folio_data {
|
| 46 |
|
| 47 | unsigned int nr_pages_head;
|
| 48 |
|
| 49 | unsigned int nr_pages_mid;
|
| 50 | unsigned int folio_shift;
|
| 51 | unsigned int nr_folios;
|
| 52 | unsigned long first_folio_page_idx;
|
| 53 | };
|
| 54 |
|
| 55 | bool io_rsrc_cache_init(struct io_ring_ctx *ctx);
|
| 56 | void io_rsrc_cache_free(struct io_ring_ctx *ctx);
|
| 57 | struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx, int type);
|
| 58 | void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node);
|
| 59 | void io_rsrc_data_free(struct io_ring_ctx *ctx, struct io_rsrc_data *data);
|
| 60 | int io_rsrc_data_alloc(struct io_rsrc_data *data, unsigned nr);
|
| 61 |
|
| 62 | struct io_rsrc_node *io_find_buf_node(struct io_kiocb *req,
|
| 63 | unsigned issue_flags);
|
| 64 | int io_import_reg_buf(struct io_kiocb *req, struct iov_iter *iter,
|
| 65 | u64 buf_addr, size_t len, int ddir,
|
| 66 | unsigned issue_flags);
|
| 67 | int io_import_reg_vec(int ddir, struct iov_iter *iter,
|
| 68 | struct io_kiocb *req, struct iou_vec *vec,
|
| 69 | unsigned nr_iovs, unsigned issue_flags);
|
| 70 | int io_prep_reg_iovec(struct io_kiocb *req, struct iou_vec *iv,
|
| 71 | const struct iovec __user *uvec, size_t uvec_segs);
|
| 72 |
|
| 73 | int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg);
|
| 74 | int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
|
| 75 | int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
|
| 76 | unsigned int nr_args, u64 __user *tags);
|
| 77 | int io_sqe_files_unregister(struct io_ring_ctx *ctx);
|
| 78 | int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
|
| 79 | unsigned nr_args, u64 __user *tags);
|
| 80 |
|
| 81 | int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
|
| 82 | unsigned nr_args);
|
| 83 | int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
|
| 84 | unsigned size, unsigned type);
|
| 85 | int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
|
| 86 | unsigned int size, unsigned int type);
|
| 87 | int io_validate_user_buf_range(u64 uaddr, u64 ulen);
|
| 88 |
|
| 89 | bool io_check_coalesce_buffer(struct page **page_array, int nr_pages,
|
| 90 | struct io_imu_folio_data *data);
|
| 91 |
|
| 92 | static inline struct io_rsrc_node *io_rsrc_node_lookup(struct io_rsrc_data *data,
|
| 93 | unsigned int index)
|
| 94 | {
|
| 95 | if (index < data->nr)
|
| 96 | return data->nodes[array_index_nospec(index, data->nr)];
|
| 97 | return NULL;
|
| 98 | }
|
| 99 |
|
| 100 | static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
|
| 101 | {
|
| 102 | lockdep_assert_held(&ctx->uring_lock);
|
| 103 | if (!--node->refs)
|
| 104 | io_free_rsrc_node(ctx, node);
|
| 105 | }
|
| 106 |
|
| 107 | static inline bool io_reset_rsrc_node(struct io_ring_ctx *ctx,
|
| 108 | struct io_rsrc_data *data,
|
| 109 | unsigned int index)
|
| 110 | {
|
| 111 | struct io_rsrc_node *node;
|
| 112 |
|
| 113 | if (index >= data->nr)
|
| 114 | return false;
|
| 115 | index = array_index_nospec(index, data->nr);
|
| 116 | node = data->nodes[index];
|
| 117 | if (!node)
|
| 118 | return false;
|
| 119 | io_put_rsrc_node(ctx, node);
|
| 120 | data->nodes[index] = NULL;
|
| 121 | return true;
|
| 122 | }
|
| 123 |
|
| 124 | int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
|
| 125 | int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
|
| 126 |
|
| 127 | int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
|
| 128 | int io_account_mem(struct user_struct *user, struct mm_struct *mm_account,
|
| 129 | unsigned long nr_pages);
|
| 130 | void io_unaccount_mem(struct user_struct *user, struct mm_struct *mm_account,
|
| 131 | unsigned long nr_pages);
|
| 132 |
|
| 133 | static inline void __io_unaccount_mem(struct user_struct *user,
|
| 134 | unsigned long nr_pages)
|
| 135 | {
|
| 136 | atomic_long_sub(nr_pages, &user->locked_vm);
|
| 137 | }
|
| 138 |
|
| 139 | void io_vec_free(struct iou_vec *iv);
|
| 140 | int io_vec_realloc(struct iou_vec *iv, unsigned nr_entries);
|
| 141 |
|
| 142 | static inline void io_vec_reset_iovec(struct iou_vec *iv,
|
| 143 | struct iovec *iovec, unsigned nr)
|
| 144 | {
|
| 145 | io_vec_free(iv);
|
| 146 | iv->iovec = iovec;
|
| 147 | iv->nr = nr;
|
| 148 | }
|
| 149 |
|
| 150 | static inline void io_alloc_cache_vec_kasan(struct iou_vec *iv)
|
| 151 | {
|
| 152 | if (IS_ENABLED(CONFIG_KASAN))
|
| 153 | io_vec_free(iv);
|
| 154 | }
|
| 155 |
|
| 156 | #endif
|