io_uring/rsrc: add lockdep sanity checks
[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 #include "alloc_cache.h"
8
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 {
19         struct list_head list;
20         u64 tag;
21         union {
22                 void *rsrc;
23                 struct file *file;
24                 struct io_mapped_ubuf *buf;
25         };
26 };
27
28 typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
29
30 struct io_rsrc_data {
31         struct io_ring_ctx              *ctx;
32
33         u64                             **tags;
34         unsigned int                    nr;
35         rsrc_put_fn                     *do_put;
36         atomic_t                        refs;
37         struct completion               done;
38         bool                            quiesce;
39 };
40
41 struct io_rsrc_node {
42         union {
43                 struct io_cache_entry           cache;
44                 struct io_rsrc_data             *rsrc_data;
45         };
46         struct list_head                node;
47         struct llist_node               llist;
48         int                             refs;
49         bool                            done;
50
51         /*
52          * Keeps a list of struct io_rsrc_put to be completed. Each entry
53          * represents one rsrc (e.g. file or buffer), but all of them should've
54          * came from the same table and so are of the same type.
55          */
56         struct list_head                item_list;
57         struct io_rsrc_put              item;
58         int                             inline_items;
59 };
60
61 struct io_mapped_ubuf {
62         u64             ubuf;
63         u64             ubuf_end;
64         unsigned int    nr_bvecs;
65         unsigned long   acct_pages;
66         struct bio_vec  bvec[];
67 };
68
69 void io_rsrc_put_tw(struct callback_head *cb);
70 void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
71 void io_rsrc_put_work(struct work_struct *work);
72 void io_wait_rsrc_data(struct io_rsrc_data *data);
73 void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node);
74 int io_rsrc_node_switch_start(struct io_ring_ctx *ctx);
75 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
76                           struct io_rsrc_node *node, void *rsrc);
77 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
78                          struct io_rsrc_data *data_to_kill);
79
80 int io_import_fixed(int ddir, struct iov_iter *iter,
81                            struct io_mapped_ubuf *imu,
82                            u64 buf_addr, size_t len);
83
84 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
85 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
86 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
87                             unsigned int nr_args, u64 __user *tags);
88 void __io_sqe_files_unregister(struct io_ring_ctx *ctx);
89 int io_sqe_files_unregister(struct io_ring_ctx *ctx);
90 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
91                           unsigned nr_args, u64 __user *tags);
92
93 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file);
94
95 #if defined(CONFIG_UNIX)
96 static inline bool io_file_need_scm(struct file *filp)
97 {
98         return !!unix_get_socket(filp);
99 }
100 #else
101 static inline bool io_file_need_scm(struct file *filp)
102 {
103         return false;
104 }
105 #endif
106
107 static inline int io_scm_file_account(struct io_ring_ctx *ctx,
108                                       struct file *file)
109 {
110         if (likely(!io_file_need_scm(file)))
111                 return 0;
112         return __io_scm_file_account(ctx, file);
113 }
114
115 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
116                              unsigned nr_args);
117 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
118                             unsigned size, unsigned type);
119 int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
120                         unsigned int size, unsigned int type);
121
122 static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
123 {
124         lockdep_assert_held(&ctx->uring_lock);
125
126         if (node && !--node->refs)
127                 io_rsrc_node_ref_zero(node);
128 }
129
130 static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
131                                           struct io_ring_ctx *ctx)
132 {
133         io_put_rsrc_node(ctx, req->rsrc_node);
134 }
135
136 static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx,
137                                        struct io_rsrc_node *node)
138 {
139         node->refs++;
140 }
141
142 static inline void io_req_set_rsrc_node(struct io_kiocb *req,
143                                         struct io_ring_ctx *ctx,
144                                         unsigned int issue_flags)
145 {
146         if (!req->rsrc_node) {
147                 io_ring_submit_lock(ctx, issue_flags);
148
149                 lockdep_assert_held(&ctx->uring_lock);
150
151                 req->rsrc_node = ctx->rsrc_node;
152                 io_charge_rsrc_node(ctx, ctx->rsrc_node);
153                 io_ring_submit_unlock(ctx, issue_flags);
154         }
155 }
156
157 static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
158 {
159         unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
160         unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
161
162         return &data->tags[table_idx][off];
163 }
164
165 int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
166 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
167
168 int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
169
170 static inline void __io_unaccount_mem(struct user_struct *user,
171                                       unsigned long nr_pages)
172 {
173         atomic_long_sub(nr_pages, &user->locked_vm);
174 }
175
176 #endif