Commit | Line | Data |
---|---|---|
73572984 JA |
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 | ||
7 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) | |
8 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) | |
9 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
10 | ||
11 | enum { | |
12 | IORING_RSRC_FILE = 0, | |
13 | IORING_RSRC_BUFFER = 1, | |
14 | }; | |
15 | ||
16 | struct io_rsrc_put { | |
17 | struct list_head list; | |
18 | u64 tag; | |
19 | union { | |
20 | void *rsrc; | |
21 | struct file *file; | |
22 | struct io_mapped_ubuf *buf; | |
23 | }; | |
24 | }; | |
25 | ||
26 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); | |
27 | ||
28 | struct io_rsrc_data { | |
29 | struct io_ring_ctx *ctx; | |
30 | ||
31 | u64 **tags; | |
32 | unsigned int nr; | |
33 | rsrc_put_fn *do_put; | |
34 | atomic_t refs; | |
35 | struct completion done; | |
36 | bool quiesce; | |
37 | }; | |
38 | ||
39 | struct io_rsrc_node { | |
73572984 | 40 | struct list_head node; |
73572984 JA |
41 | struct io_rsrc_data *rsrc_data; |
42 | struct llist_node llist; | |
ef8ae64f | 43 | int refs; |
73572984 | 44 | bool done; |
c824986c PB |
45 | |
46 | /* | |
47 | * Keeps a list of struct io_rsrc_put to be completed. Each entry | |
48 | * represents one rsrc (e.g. file or buffer), but all of them should've | |
49 | * came from the same table and so are of the same type. | |
50 | */ | |
51 | struct list_head item_list; | |
73572984 JA |
52 | }; |
53 | ||
ad163a7e JA |
54 | struct io_mapped_ubuf { |
55 | u64 ubuf; | |
56 | u64 ubuf_end; | |
57 | unsigned int nr_bvecs; | |
58 | unsigned long acct_pages; | |
59 | struct bio_vec bvec[]; | |
60 | }; | |
61 | ||
d34b1b0b | 62 | void io_rsrc_put_tw(struct callback_head *cb); |
b8fb5b4f | 63 | void io_rsrc_node_ref_zero(struct io_rsrc_node *node); |
73572984 | 64 | void io_rsrc_put_work(struct work_struct *work); |
73572984 JA |
65 | void io_wait_rsrc_data(struct io_rsrc_data *data); |
66 | void io_rsrc_node_destroy(struct io_rsrc_node *ref_node); | |
73572984 JA |
67 | int io_rsrc_node_switch_start(struct io_ring_ctx *ctx); |
68 | int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, | |
69 | struct io_rsrc_node *node, void *rsrc); | |
70 | void io_rsrc_node_switch(struct io_ring_ctx *ctx, | |
71 | struct io_rsrc_data *data_to_kill); | |
72 | ||
c059f785 PB |
73 | int io_import_fixed(int ddir, struct iov_iter *iter, |
74 | struct io_mapped_ubuf *imu, | |
75 | u64 buf_addr, size_t len); | |
73572984 JA |
76 | |
77 | void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx); | |
78 | int io_sqe_buffers_unregister(struct io_ring_ctx *ctx); | |
79 | int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, | |
80 | unsigned int nr_args, u64 __user *tags); | |
81 | void __io_sqe_files_unregister(struct io_ring_ctx *ctx); | |
82 | int io_sqe_files_unregister(struct io_ring_ctx *ctx); | |
83 | int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, | |
84 | unsigned nr_args, u64 __user *tags); | |
85 | ||
86 | int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file); | |
87 | ||
88 | #if defined(CONFIG_UNIX) | |
89 | static inline bool io_file_need_scm(struct file *filp) | |
90 | { | |
73572984 | 91 | return !!unix_get_socket(filp); |
73572984 JA |
92 | } |
93 | #else | |
94 | static inline bool io_file_need_scm(struct file *filp) | |
95 | { | |
96 | return false; | |
97 | } | |
98 | #endif | |
99 | ||
100 | static inline int io_scm_file_account(struct io_ring_ctx *ctx, | |
101 | struct file *file) | |
102 | { | |
103 | if (likely(!io_file_need_scm(file))) | |
104 | return 0; | |
105 | return __io_scm_file_account(ctx, file); | |
106 | } | |
107 | ||
108 | int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, | |
109 | unsigned nr_args); | |
110 | int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
111 | unsigned size, unsigned type); | |
112 | int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, | |
113 | unsigned int size, unsigned int type); | |
114 | ||
2ad4c6d0 | 115 | static inline void io_put_rsrc_node(struct io_rsrc_node *node) |
73572984 | 116 | { |
ef8ae64f PB |
117 | if (node && !--node->refs) |
118 | io_rsrc_node_ref_zero(node); | |
73572984 JA |
119 | } |
120 | ||
121 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
122 | struct io_ring_ctx *ctx) | |
73572984 | 123 | { |
ef8ae64f | 124 | io_put_rsrc_node(req->rsrc_node); |
73572984 JA |
125 | } |
126 | ||
8e15c0e7 PB |
127 | static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx, |
128 | struct io_rsrc_node *node) | |
68ef5578 | 129 | { |
ef8ae64f | 130 | node->refs++; |
68ef5578 PB |
131 | } |
132 | ||
73572984 JA |
133 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
134 | struct io_ring_ctx *ctx, | |
135 | unsigned int issue_flags) | |
136 | { | |
137 | if (!req->rsrc_node) { | |
4ff0b50d | 138 | io_ring_submit_lock(ctx, issue_flags); |
73572984 | 139 | |
4ff0b50d | 140 | lockdep_assert_held(&ctx->uring_lock); |
68ef5578 | 141 | |
4ff0b50d | 142 | req->rsrc_node = ctx->rsrc_node; |
8e15c0e7 | 143 | io_charge_rsrc_node(ctx, ctx->rsrc_node); |
4ff0b50d | 144 | io_ring_submit_unlock(ctx, issue_flags); |
73572984 JA |
145 | } |
146 | } | |
147 | ||
148 | static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) | |
149 | { | |
150 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
151 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
152 | ||
153 | return &data->tags[table_idx][off]; | |
154 | } | |
155 | ||
d9808ceb PB |
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); | |
6a9ce66f PB |
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 | ||
73572984 | 167 | #endif |