io_uring: don't put nodes under spinlocks
[platform/kernel/linux-starfive.git] / io_uring / rsrc.h
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 {
40         refcount_t                      refs;
41         struct list_head                node;
42         struct list_head                rsrc_list;
43         struct io_rsrc_data             *rsrc_data;
44         struct llist_node               llist;
45         bool                            done;
46         int                             cached_refs;
47 };
48
49 struct io_mapped_ubuf {
50         u64             ubuf;
51         u64             ubuf_end;
52         unsigned int    nr_bvecs;
53         unsigned long   acct_pages;
54         struct bio_vec  bvec[];
55 };
56
57 void io_rsrc_put_tw(struct callback_head *cb);
58 void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
59 void io_rsrc_put_work(struct work_struct *work);
60 void io_rsrc_refs_refill(struct io_ring_ctx *ctx, struct io_rsrc_node *node);
61 void io_wait_rsrc_data(struct io_rsrc_data *data);
62 void io_rsrc_node_destroy(struct io_rsrc_node *ref_node);
63 void io_rsrc_refs_drop(struct io_ring_ctx *ctx);
64 int io_rsrc_node_switch_start(struct io_ring_ctx *ctx);
65 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
66                           struct io_rsrc_node *node, void *rsrc);
67 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
68                          struct io_rsrc_data *data_to_kill);
69
70 int io_import_fixed(int ddir, struct iov_iter *iter,
71                            struct io_mapped_ubuf *imu,
72                            u64 buf_addr, size_t len);
73
74 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
75 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
76 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
77                             unsigned int nr_args, u64 __user *tags);
78 void __io_sqe_files_unregister(struct io_ring_ctx *ctx);
79 int io_sqe_files_unregister(struct io_ring_ctx *ctx);
80 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
81                           unsigned nr_args, u64 __user *tags);
82
83 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file);
84
85 #if defined(CONFIG_UNIX)
86 static inline bool io_file_need_scm(struct file *filp)
87 {
88         return !!unix_get_socket(filp);
89 }
90 #else
91 static inline bool io_file_need_scm(struct file *filp)
92 {
93         return false;
94 }
95 #endif
96
97 static inline int io_scm_file_account(struct io_ring_ctx *ctx,
98                                       struct file *file)
99 {
100         if (likely(!io_file_need_scm(file)))
101                 return 0;
102         return __io_scm_file_account(ctx, file);
103 }
104
105 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
106                              unsigned nr_args);
107 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
108                             unsigned size, unsigned type);
109 int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
110                         unsigned int size, unsigned int type);
111
112 static inline void io_rsrc_put_node(struct io_rsrc_node *node, int nr)
113 {
114         if (refcount_sub_and_test(nr, &node->refs))
115                 io_rsrc_node_ref_zero(node);
116 }
117
118 static inline void io_put_rsrc_node(struct io_rsrc_node *node)
119 {
120         if (node)
121                 io_rsrc_put_node(node, 1);
122 }
123
124 static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
125                                           struct io_ring_ctx *ctx)
126         __must_hold(&ctx->uring_lock)
127 {
128         struct io_rsrc_node *node = req->rsrc_node;
129
130         if (node) {
131                 if (node == ctx->rsrc_node)
132                         node->cached_refs++;
133                 else
134                         io_rsrc_put_node(node, 1);
135         }
136 }
137
138 static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx,
139                                        struct io_rsrc_node *node)
140 {
141         node->cached_refs--;
142         if (unlikely(node->cached_refs < 0))
143                 io_rsrc_refs_refill(ctx, node);
144 }
145
146 static inline void io_req_set_rsrc_node(struct io_kiocb *req,
147                                         struct io_ring_ctx *ctx,
148                                         unsigned int issue_flags)
149 {
150         if (!req->rsrc_node) {
151                 io_ring_submit_lock(ctx, issue_flags);
152
153                 lockdep_assert_held(&ctx->uring_lock);
154
155                 req->rsrc_node = ctx->rsrc_node;
156                 io_charge_rsrc_node(ctx, ctx->rsrc_node);
157                 io_ring_submit_unlock(ctx, issue_flags);
158         }
159 }
160
161 static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
162 {
163         unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
164         unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
165
166         return &data->tags[table_idx][off];
167 }
168
169 int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
170 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
171
172 int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
173
174 static inline void __io_unaccount_mem(struct user_struct *user,
175                                       unsigned long nr_pages)
176 {
177         atomic_long_sub(nr_pages, &user->locked_vm);
178 }
179
180 #endif