1 // SPDX-License-Identifier: GPL-2.0
5 #include <net/af_unix.h>
7 #include "alloc_cache.h"
9 #define IO_NODE_ALLOC_CACHE_MAX 32
11 #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3)
12 #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT)
13 #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1)
17 IORING_RSRC_BUFFER = 1,
21 struct list_head list;
26 struct io_mapped_ubuf *buf;
30 typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
33 struct io_ring_ctx *ctx;
38 struct completion done;
45 struct io_cache_entry cache;
46 struct io_rsrc_data *rsrc_data;
48 struct list_head node;
49 struct llist_node llist;
54 * Keeps a list of struct io_rsrc_put to be completed. Each entry
55 * represents one rsrc (e.g. file or buffer), but all of them should've
56 * came from the same table and so are of the same type.
58 struct list_head item_list;
59 struct io_rsrc_put item;
63 struct io_mapped_ubuf {
66 unsigned int nr_bvecs;
67 unsigned long acct_pages;
68 struct bio_vec bvec[];
71 void io_rsrc_put_tw(struct callback_head *cb);
72 void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
73 void io_rsrc_put_work(struct work_struct *work);
74 void io_wait_rsrc_data(struct io_rsrc_data *data);
75 void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node);
76 int io_rsrc_node_switch_start(struct io_ring_ctx *ctx);
77 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
78 struct io_rsrc_node *node, void *rsrc);
79 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
80 struct io_rsrc_data *data_to_kill);
82 int io_import_fixed(int ddir, struct iov_iter *iter,
83 struct io_mapped_ubuf *imu,
84 u64 buf_addr, size_t len);
86 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
87 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
88 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
89 unsigned int nr_args, u64 __user *tags);
90 void __io_sqe_files_unregister(struct io_ring_ctx *ctx);
91 int io_sqe_files_unregister(struct io_ring_ctx *ctx);
92 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
93 unsigned nr_args, u64 __user *tags);
95 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file);
97 #if defined(CONFIG_UNIX)
98 static inline bool io_file_need_scm(struct file *filp)
100 return !!unix_get_socket(filp);
103 static inline bool io_file_need_scm(struct file *filp)
109 static inline int io_scm_file_account(struct io_ring_ctx *ctx,
112 if (likely(!io_file_need_scm(file)))
114 return __io_scm_file_account(ctx, file);
117 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
119 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
120 unsigned size, unsigned type);
121 int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
122 unsigned int size, unsigned int type);
124 static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
126 lockdep_assert_held(&ctx->uring_lock);
128 if (node && !--node->refs)
129 io_rsrc_node_ref_zero(node);
132 static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
133 struct io_ring_ctx *ctx)
135 io_put_rsrc_node(ctx, req->rsrc_node);
138 static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx,
139 struct io_rsrc_node *node)
144 static inline void io_req_set_rsrc_node(struct io_kiocb *req,
145 struct io_ring_ctx *ctx,
146 unsigned int issue_flags)
148 if (!req->rsrc_node) {
149 io_ring_submit_lock(ctx, issue_flags);
151 lockdep_assert_held(&ctx->uring_lock);
153 req->rsrc_node = ctx->rsrc_node;
154 io_charge_rsrc_node(ctx, ctx->rsrc_node);
155 io_ring_submit_unlock(ctx, issue_flags);
159 static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
161 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
162 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
164 return &data->tags[table_idx][off];
167 int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
168 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
170 int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
172 static inline void __io_unaccount_mem(struct user_struct *user,
173 unsigned long nr_pages)
175 atomic_long_sub(nr_pages, &user->locked_vm);