1 // SPDX-License-Identifier: GPL-2.0
5 #include <net/af_unix.h>
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)
13 IORING_RSRC_BUFFER = 1,
17 struct list_head list;
22 struct io_mapped_ubuf *buf;
26 typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
29 struct io_ring_ctx *ctx;
35 struct completion done;
40 struct list_head node;
41 struct io_rsrc_data *rsrc_data;
42 struct llist_node llist;
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.
51 struct list_head item_list;
52 struct io_rsrc_put item;
56 struct io_mapped_ubuf {
59 unsigned int nr_bvecs;
60 unsigned long acct_pages;
61 struct bio_vec bvec[];
64 void io_rsrc_put_tw(struct callback_head *cb);
65 void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
66 void io_rsrc_put_work(struct work_struct *work);
67 void io_wait_rsrc_data(struct io_rsrc_data *data);
68 void io_rsrc_node_destroy(struct io_rsrc_node *ref_node);
69 int io_rsrc_node_switch_start(struct io_ring_ctx *ctx);
70 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
71 struct io_rsrc_node *node, void *rsrc);
72 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
73 struct io_rsrc_data *data_to_kill);
75 int io_import_fixed(int ddir, struct iov_iter *iter,
76 struct io_mapped_ubuf *imu,
77 u64 buf_addr, size_t len);
79 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
80 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
81 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
82 unsigned int nr_args, u64 __user *tags);
83 void __io_sqe_files_unregister(struct io_ring_ctx *ctx);
84 int io_sqe_files_unregister(struct io_ring_ctx *ctx);
85 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
86 unsigned nr_args, u64 __user *tags);
88 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file);
90 #if defined(CONFIG_UNIX)
91 static inline bool io_file_need_scm(struct file *filp)
93 return !!unix_get_socket(filp);
96 static inline bool io_file_need_scm(struct file *filp)
102 static inline int io_scm_file_account(struct io_ring_ctx *ctx,
105 if (likely(!io_file_need_scm(file)))
107 return __io_scm_file_account(ctx, file);
110 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
112 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
113 unsigned size, unsigned type);
114 int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
115 unsigned int size, unsigned int type);
117 static inline void io_put_rsrc_node(struct io_rsrc_node *node)
119 if (node && !--node->refs)
120 io_rsrc_node_ref_zero(node);
123 static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
124 struct io_ring_ctx *ctx)
126 io_put_rsrc_node(req->rsrc_node);
129 static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx,
130 struct io_rsrc_node *node)
135 static inline void io_req_set_rsrc_node(struct io_kiocb *req,
136 struct io_ring_ctx *ctx,
137 unsigned int issue_flags)
139 if (!req->rsrc_node) {
140 io_ring_submit_lock(ctx, issue_flags);
142 lockdep_assert_held(&ctx->uring_lock);
144 req->rsrc_node = ctx->rsrc_node;
145 io_charge_rsrc_node(ctx, ctx->rsrc_node);
146 io_ring_submit_unlock(ctx, issue_flags);
150 static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
152 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
153 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
155 return &data->tags[table_idx][off];
158 int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
159 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
161 int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
163 static inline void __io_unaccount_mem(struct user_struct *user,
164 unsigned long nr_pages)
166 atomic_long_sub(nr_pages, &user->locked_vm);