Loading...
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#include "alloc_cache.h"
8
9#define IO_NODE_ALLOC_CACHE_MAX 32
10
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)
14
15enum {
16 IORING_RSRC_FILE = 0,
17 IORING_RSRC_BUFFER = 1,
18};
19
20struct io_rsrc_put {
21 u64 tag;
22 union {
23 void *rsrc;
24 struct file *file;
25 struct io_mapped_ubuf *buf;
26 };
27};
28
29typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
30
31struct io_rsrc_data {
32 struct io_ring_ctx *ctx;
33
34 u64 **tags;
35 unsigned int nr;
36 u16 rsrc_type;
37 bool quiesce;
38};
39
40struct io_rsrc_node {
41 union {
42 struct io_cache_entry cache;
43 struct io_ring_ctx *ctx;
44 };
45 int refs;
46 bool empty;
47 u16 type;
48 struct list_head node;
49 struct io_rsrc_put item;
50};
51
52struct io_mapped_ubuf {
53 u64 ubuf;
54 u64 ubuf_end;
55 unsigned int nr_bvecs;
56 unsigned long acct_pages;
57 struct bio_vec bvec[] __counted_by(nr_bvecs);
58};
59
60void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
61void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node);
62struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx);
63int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, void *rsrc);
64
65int io_import_fixed(int ddir, struct iov_iter *iter,
66 struct io_mapped_ubuf *imu,
67 u64 buf_addr, size_t len);
68
69void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
70int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
71int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
72 unsigned int nr_args, u64 __user *tags);
73void __io_sqe_files_unregister(struct io_ring_ctx *ctx);
74int io_sqe_files_unregister(struct io_ring_ctx *ctx);
75int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
76 unsigned nr_args, u64 __user *tags);
77
78int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
79 unsigned nr_args);
80int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
81 unsigned size, unsigned type);
82int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
83 unsigned int size, unsigned int type);
84
85static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
86{
87 lockdep_assert_held(&ctx->uring_lock);
88
89 if (node && !--node->refs)
90 io_rsrc_node_ref_zero(node);
91}
92
93static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
94 struct io_ring_ctx *ctx)
95{
96 io_put_rsrc_node(ctx, req->rsrc_node);
97}
98
99static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx,
100 struct io_rsrc_node *node)
101{
102 node->refs++;
103}
104
105static inline void __io_req_set_rsrc_node(struct io_kiocb *req,
106 struct io_ring_ctx *ctx)
107{
108 lockdep_assert_held(&ctx->uring_lock);
109 req->rsrc_node = ctx->rsrc_node;
110 io_charge_rsrc_node(ctx, ctx->rsrc_node);
111}
112
113static inline void io_req_set_rsrc_node(struct io_kiocb *req,
114 struct io_ring_ctx *ctx,
115 unsigned int issue_flags)
116{
117 if (!req->rsrc_node) {
118 io_ring_submit_lock(ctx, issue_flags);
119 __io_req_set_rsrc_node(req, ctx);
120 io_ring_submit_unlock(ctx, issue_flags);
121 }
122}
123
124static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
125{
126 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
127 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
128
129 return &data->tags[table_idx][off];
130}
131
132static inline int io_rsrc_init(struct io_ring_ctx *ctx)
133{
134 ctx->rsrc_node = io_rsrc_node_alloc(ctx);
135 return ctx->rsrc_node ? 0 : -ENOMEM;
136}
137
138int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
139int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
140
141int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
142
143static inline void __io_unaccount_mem(struct user_struct *user,
144 unsigned long nr_pages)
145{
146 atomic_long_sub(nr_pages, &user->locked_vm);
147}
148
149#endif
1// SPDX-License-Identifier: GPL-2.0
2#ifndef IOU_RSRC_H
3#define IOU_RSRC_H
4
5#define IO_NODE_ALLOC_CACHE_MAX 32
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_node {
17 unsigned char type;
18 int refs;
19
20 u64 tag;
21 union {
22 unsigned long file_ptr;
23 struct io_mapped_ubuf *buf;
24 };
25};
26
27struct io_mapped_ubuf {
28 u64 ubuf;
29 unsigned int len;
30 unsigned int nr_bvecs;
31 unsigned int folio_shift;
32 refcount_t refs;
33 unsigned long acct_pages;
34 struct bio_vec bvec[] __counted_by(nr_bvecs);
35};
36
37struct io_imu_folio_data {
38 /* Head folio can be partially included in the fixed buf */
39 unsigned int nr_pages_head;
40 /* For non-head/tail folios, has to be fully included */
41 unsigned int nr_pages_mid;
42 unsigned int folio_shift;
43};
44
45struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx, int type);
46void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node);
47void io_rsrc_data_free(struct io_ring_ctx *ctx, struct io_rsrc_data *data);
48int io_rsrc_data_alloc(struct io_rsrc_data *data, unsigned nr);
49
50int io_import_fixed(int ddir, struct iov_iter *iter,
51 struct io_mapped_ubuf *imu,
52 u64 buf_addr, size_t len);
53
54int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg);
55int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
56int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
57 unsigned int nr_args, u64 __user *tags);
58int io_sqe_files_unregister(struct io_ring_ctx *ctx);
59int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
60 unsigned nr_args, u64 __user *tags);
61
62int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
63 unsigned nr_args);
64int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
65 unsigned size, unsigned type);
66int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
67 unsigned int size, unsigned int type);
68
69static inline struct io_rsrc_node *io_rsrc_node_lookup(struct io_rsrc_data *data,
70 int index)
71{
72 if (index < data->nr)
73 return data->nodes[array_index_nospec(index, data->nr)];
74 return NULL;
75}
76
77static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
78{
79 if (node && !--node->refs)
80 io_free_rsrc_node(ctx, node);
81}
82
83static inline bool io_reset_rsrc_node(struct io_ring_ctx *ctx,
84 struct io_rsrc_data *data, int index)
85{
86 struct io_rsrc_node *node = data->nodes[index];
87
88 if (!node)
89 return false;
90 io_put_rsrc_node(ctx, node);
91 data->nodes[index] = NULL;
92 return true;
93}
94
95static inline void io_req_put_rsrc_nodes(struct io_kiocb *req)
96{
97 if (req->file_node) {
98 io_put_rsrc_node(req->ctx, req->file_node);
99 req->file_node = NULL;
100 }
101 if (req->flags & REQ_F_BUF_NODE) {
102 io_put_rsrc_node(req->ctx, req->buf_node);
103 req->buf_node = NULL;
104 }
105}
106
107static inline void io_req_assign_rsrc_node(struct io_rsrc_node **dst_node,
108 struct io_rsrc_node *node)
109{
110 node->refs++;
111 *dst_node = node;
112}
113
114static inline void io_req_assign_buf_node(struct io_kiocb *req,
115 struct io_rsrc_node *node)
116{
117 io_req_assign_rsrc_node(&req->buf_node, node);
118 req->flags |= REQ_F_BUF_NODE;
119}
120
121int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
122int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
123
124int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
125
126static inline void __io_unaccount_mem(struct user_struct *user,
127 unsigned long nr_pages)
128{
129 atomic_long_sub(nr_pages, &user->locked_vm);
130}
131
132#endif