1 // SPDX-License-Identifier: GPL-2.0
5 #include "alloc_cache.h"
7 #define IO_NODE_ALLOC_CACHE_MAX 32
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)
15 IORING_RSRC_BUFFER = 1,
23 struct io_mapped_ubuf *buf;
27 typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
30 struct io_ring_ctx *ctx;
40 struct io_cache_entry cache;
41 struct io_ring_ctx *ctx;
46 struct list_head node;
47 struct io_rsrc_put item;
50 struct io_mapped_ubuf {
53 unsigned int nr_bvecs;
54 unsigned long acct_pages;
55 struct bio_vec bvec[] __counted_by(nr_bvecs);
58 void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
59 void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node);
60 struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx);
61 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, void *rsrc);
63 int io_import_fixed(int ddir, struct iov_iter *iter,
64 struct io_mapped_ubuf *imu,
65 u64 buf_addr, size_t len);
67 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
68 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
69 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
70 unsigned int nr_args, u64 __user *tags);
71 void __io_sqe_files_unregister(struct io_ring_ctx *ctx);
72 int io_sqe_files_unregister(struct io_ring_ctx *ctx);
73 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
74 unsigned nr_args, u64 __user *tags);
76 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
78 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
79 unsigned size, unsigned type);
80 int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
81 unsigned int size, unsigned int type);
83 static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
85 lockdep_assert_held(&ctx->uring_lock);
87 if (node && !--node->refs)
88 io_rsrc_node_ref_zero(node);
91 static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
92 struct io_ring_ctx *ctx)
94 io_put_rsrc_node(ctx, req->rsrc_node);
97 static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx,
98 struct io_rsrc_node *node)
103 static inline void __io_req_set_rsrc_node(struct io_kiocb *req,
104 struct io_ring_ctx *ctx)
106 lockdep_assert_held(&ctx->uring_lock);
107 req->rsrc_node = ctx->rsrc_node;
108 io_charge_rsrc_node(ctx, ctx->rsrc_node);
111 static inline void io_req_set_rsrc_node(struct io_kiocb *req,
112 struct io_ring_ctx *ctx,
113 unsigned int issue_flags)
115 if (!req->rsrc_node) {
116 io_ring_submit_lock(ctx, issue_flags);
117 __io_req_set_rsrc_node(req, ctx);
118 io_ring_submit_unlock(ctx, issue_flags);
122 static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
124 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
125 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
127 return &data->tags[table_idx][off];
130 static inline int io_rsrc_init(struct io_ring_ctx *ctx)
132 ctx->rsrc_node = io_rsrc_node_alloc(ctx);
133 return ctx->rsrc_node ? 0 : -ENOMEM;
136 int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
137 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
139 int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
141 static inline void __io_unaccount_mem(struct user_struct *user,
142 unsigned long nr_pages)
144 atomic_long_sub(nr_pages, &user->locked_vm);