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;
43 struct io_cache_entry cache;
44 struct io_rsrc_data *rsrc_data;
46 struct list_head node;
47 struct llist_node llist;
51 * Keeps a list of struct io_rsrc_put to be completed. Each entry
52 * represents one rsrc (e.g. file or buffer), but all of them should've
53 * came from the same table and so are of the same type.
55 struct list_head item_list;
56 struct io_rsrc_put item;
60 struct io_mapped_ubuf {
63 unsigned int nr_bvecs;
64 unsigned long acct_pages;
65 struct bio_vec bvec[];
68 void io_rsrc_put_tw(struct callback_head *cb);
69 void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
70 void io_rsrc_put_work(struct work_struct *work);
71 void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node);
72 int io_rsrc_node_switch_start(struct io_ring_ctx *ctx);
73 struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx);
74 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
75 struct io_rsrc_node *node, void *rsrc);
76 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
77 struct io_rsrc_data *data_to_kill);
79 int io_import_fixed(int ddir, struct iov_iter *iter,
80 struct io_mapped_ubuf *imu,
81 u64 buf_addr, size_t len);
83 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
84 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
85 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
86 unsigned int nr_args, u64 __user *tags);
87 void __io_sqe_files_unregister(struct io_ring_ctx *ctx);
88 int io_sqe_files_unregister(struct io_ring_ctx *ctx);
89 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
90 unsigned nr_args, u64 __user *tags);
92 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file);
94 #if defined(CONFIG_UNIX)
95 static inline bool io_file_need_scm(struct file *filp)
97 return !!unix_get_socket(filp);
100 static inline bool io_file_need_scm(struct file *filp)
106 static inline int io_scm_file_account(struct io_ring_ctx *ctx,
109 if (likely(!io_file_need_scm(file)))
111 return __io_scm_file_account(ctx, file);
114 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
116 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
117 unsigned size, unsigned type);
118 int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
119 unsigned int size, unsigned int type);
121 static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
123 lockdep_assert_held(&ctx->uring_lock);
125 if (node && !--node->refs)
126 io_rsrc_node_ref_zero(node);
129 static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
130 struct io_ring_ctx *ctx)
132 io_put_rsrc_node(ctx, req->rsrc_node);
135 static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx,
136 struct io_rsrc_node *node)
141 static inline void io_req_set_rsrc_node(struct io_kiocb *req,
142 struct io_ring_ctx *ctx,
143 unsigned int issue_flags)
145 if (!req->rsrc_node) {
146 io_ring_submit_lock(ctx, issue_flags);
148 lockdep_assert_held(&ctx->uring_lock);
150 req->rsrc_node = ctx->rsrc_node;
151 io_charge_rsrc_node(ctx, ctx->rsrc_node);
152 io_ring_submit_unlock(ctx, issue_flags);
156 static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
158 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
159 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
161 return &data->tags[table_idx][off];
164 static inline int io_rsrc_init(struct io_ring_ctx *ctx)
166 ctx->rsrc_node = io_rsrc_node_alloc(ctx);
167 return ctx->rsrc_node ? 0 : -ENOMEM;
170 int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
171 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
173 int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
175 static inline void __io_unaccount_mem(struct user_struct *user,
176 unsigned long nr_pages)
178 atomic_long_sub(nr_pages, &user->locked_vm);