io_uring: force multishot CQEs into task context
[linux-block.git] / io_uring / rsrc.h
CommitLineData
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
11enum {
12 IORING_RSRC_FILE = 0,
13 IORING_RSRC_BUFFER = 1,
14};
15
16struct 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
26typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
27
28struct 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
39struct io_rsrc_node {
40 struct percpu_ref 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};
47
ad163a7e
JA
48struct io_mapped_ubuf {
49 u64 ubuf;
50 u64 ubuf_end;
51 unsigned int nr_bvecs;
52 unsigned long acct_pages;
53 struct bio_vec bvec[];
54};
55
73572984
JA
56void io_rsrc_put_work(struct work_struct *work);
57void io_rsrc_refs_refill(struct io_ring_ctx *ctx);
58void io_wait_rsrc_data(struct io_rsrc_data *data);
59void io_rsrc_node_destroy(struct io_rsrc_node *ref_node);
60void io_rsrc_refs_drop(struct io_ring_ctx *ctx);
61int io_rsrc_node_switch_start(struct io_ring_ctx *ctx);
62int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
63 struct io_rsrc_node *node, void *rsrc);
64void io_rsrc_node_switch(struct io_ring_ctx *ctx,
65 struct io_rsrc_data *data_to_kill);
66
c059f785
PB
67int io_import_fixed(int ddir, struct iov_iter *iter,
68 struct io_mapped_ubuf *imu,
69 u64 buf_addr, size_t len);
73572984
JA
70
71void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
72int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
73int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
74 unsigned int nr_args, u64 __user *tags);
75void __io_sqe_files_unregister(struct io_ring_ctx *ctx);
76int io_sqe_files_unregister(struct io_ring_ctx *ctx);
77int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
78 unsigned nr_args, u64 __user *tags);
79
80int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file);
81
82#if defined(CONFIG_UNIX)
83static inline bool io_file_need_scm(struct file *filp)
84{
73572984 85 return !!unix_get_socket(filp);
73572984
JA
86}
87#else
88static inline bool io_file_need_scm(struct file *filp)
89{
90 return false;
91}
92#endif
93
94static inline int io_scm_file_account(struct io_ring_ctx *ctx,
95 struct file *file)
96{
97 if (likely(!io_file_need_scm(file)))
98 return 0;
99 return __io_scm_file_account(ctx, file);
100}
101
102int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
103 unsigned nr_args);
104int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
105 unsigned size, unsigned type);
106int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
107 unsigned int size, unsigned int type);
108
109static inline void io_rsrc_put_node(struct io_rsrc_node *node, int nr)
110{
111 percpu_ref_put_many(&node->refs, nr);
112}
113
114static inline void io_req_put_rsrc(struct io_kiocb *req)
115{
116 if (req->rsrc_node)
117 io_rsrc_put_node(req->rsrc_node, 1);
118}
119
120static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
121 struct io_ring_ctx *ctx)
122 __must_hold(&ctx->uring_lock)
123{
124 struct io_rsrc_node *node = req->rsrc_node;
125
126 if (node) {
127 if (node == ctx->rsrc_node)
128 ctx->rsrc_cached_refs++;
129 else
130 io_rsrc_put_node(node, 1);
131 }
132}
133
68ef5578
PB
134static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx)
135{
136 ctx->rsrc_cached_refs--;
137 if (unlikely(ctx->rsrc_cached_refs < 0))
138 io_rsrc_refs_refill(ctx);
139}
140
73572984
JA
141static inline void io_req_set_rsrc_node(struct io_kiocb *req,
142 struct io_ring_ctx *ctx,
143 unsigned int issue_flags)
144{
145 if (!req->rsrc_node) {
146 req->rsrc_node = ctx->rsrc_node;
147
148 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
149 lockdep_assert_held(&ctx->uring_lock);
68ef5578
PB
150
151 io_charge_rsrc_node(ctx);
73572984
JA
152 } else {
153 percpu_ref_get(&req->rsrc_node->refs);
154 }
155 }
156}
157
158static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
159{
160 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
161 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
162
163 return &data->tags[table_idx][off];
164}
165
d9808ceb
PB
166int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
167int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
6a9ce66f
PB
168
169int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
170
171static inline void __io_unaccount_mem(struct user_struct *user,
172 unsigned long nr_pages)
173{
174 atomic_long_sub(nr_pages, &user->locked_vm);
175}
176
73572984 177#endif