blob: e210002389540b671e9cb1f00dcbff4303ae2f75 [file] [log] [blame]
Jens Axboe73572982022-06-13 07:12:45 -06001// SPDX-License-Identifier: GPL-2.0
2#ifndef IOU_RSRC_H
3#define IOU_RSRC_H
4
Pavel Begunkov9eae8652023-04-04 13:39:54 +01005#include "alloc_cache.h"
6
Pavel Begunkov69bbc6a2023-04-04 13:39:57 +01007#define IO_NODE_ALLOC_CACHE_MAX 32
8
Jens Axboe73572982022-06-13 07:12:45 -06009#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)
12
13enum {
14 IORING_RSRC_FILE = 0,
15 IORING_RSRC_BUFFER = 1,
16};
17
18struct io_rsrc_put {
Jens Axboe73572982022-06-13 07:12:45 -060019 u64 tag;
20 union {
21 void *rsrc;
22 struct file *file;
23 struct io_mapped_ubuf *buf;
24 };
25};
26
27typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
28
29struct io_rsrc_data {
30 struct io_ring_ctx *ctx;
31
32 u64 **tags;
33 unsigned int nr;
Pavel Begunkovfc7f3a82023-04-18 14:06:40 +010034 u16 rsrc_type;
Jens Axboe73572982022-06-13 07:12:45 -060035 bool quiesce;
36};
37
38struct io_rsrc_node {
Pavel Begunkov9eae8652023-04-04 13:39:54 +010039 union {
40 struct io_cache_entry cache;
Pavel Begunkov2236b392023-04-18 14:06:41 +010041 struct io_ring_ctx *ctx;
Pavel Begunkov9eae8652023-04-04 13:39:54 +010042 };
Pavel Begunkovef8ae642023-04-04 13:39:49 +010043 int refs;
Pavel Begunkov26147da2023-04-18 14:06:37 +010044 bool empty;
Pavel Begunkov2236b392023-04-18 14:06:41 +010045 u16 type;
Pavel Begunkovc3766442023-04-18 14:06:36 +010046 struct list_head node;
47 struct io_rsrc_put item;
Jens Axboe73572982022-06-13 07:12:45 -060048};
49
Jens Axboead163a72022-06-18 19:44:33 -060050struct io_mapped_ubuf {
51 u64 ubuf;
52 u64 ubuf_end;
53 unsigned int nr_bvecs;
54 unsigned long acct_pages;
Kees Cook04d92442023-08-17 14:21:47 -070055 struct bio_vec bvec[] __counted_by(nr_bvecs);
Jens Axboead163a72022-06-18 19:44:33 -060056};
57
Pavel Begunkovb8fb5b42023-04-04 13:39:45 +010058void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
Pavel Begunkov9eae8652023-04-04 13:39:54 +010059void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node);
Pavel Begunkov2933ae62023-04-11 12:06:07 +010060struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx);
Pavel Begunkov63fea892023-04-18 14:06:35 +010061int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, void *rsrc);
Jens Axboe73572982022-06-13 07:12:45 -060062
Pavel Begunkovc059f7852022-06-20 01:25:59 +010063int io_import_fixed(int ddir, struct iov_iter *iter,
64 struct io_mapped_ubuf *imu,
65 u64 buf_addr, size_t len);
Jens Axboe73572982022-06-13 07:12:45 -060066
67void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
68int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
69int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
70 unsigned int nr_args, u64 __user *tags);
71void __io_sqe_files_unregister(struct io_ring_ctx *ctx);
72int io_sqe_files_unregister(struct io_ring_ctx *ctx);
73int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
74 unsigned nr_args, u64 __user *tags);
75
Jens Axboe73572982022-06-13 07:12:45 -060076int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
77 unsigned nr_args);
78int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
79 unsigned size, unsigned type);
80int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
81 unsigned int size, unsigned int type);
82
Pavel Begunkov1f2c8f62023-04-04 13:39:55 +010083static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
Jens Axboe73572982022-06-13 07:12:45 -060084{
Pavel Begunkov1f2c8f62023-04-04 13:39:55 +010085 lockdep_assert_held(&ctx->uring_lock);
86
Pavel Begunkovef8ae642023-04-04 13:39:49 +010087 if (node && !--node->refs)
88 io_rsrc_node_ref_zero(node);
Jens Axboe73572982022-06-13 07:12:45 -060089}
90
91static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
92 struct io_ring_ctx *ctx)
Jens Axboe73572982022-06-13 07:12:45 -060093{
Pavel Begunkov1f2c8f62023-04-04 13:39:55 +010094 io_put_rsrc_node(ctx, req->rsrc_node);
Jens Axboe73572982022-06-13 07:12:45 -060095}
96
Pavel Begunkov8e15c0e2023-04-04 13:39:46 +010097static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx,
98 struct io_rsrc_node *node)
Pavel Begunkov68ef5572022-07-12 21:52:41 +010099{
Pavel Begunkovef8ae642023-04-04 13:39:49 +0100100 node->refs++;
Pavel Begunkov68ef5572022-07-12 21:52:41 +0100101}
102
Jens Axboe3f302382024-01-11 13:34:33 -0700103static inline void __io_req_set_rsrc_node(struct io_kiocb *req,
104 struct io_ring_ctx *ctx)
105{
106 lockdep_assert_held(&ctx->uring_lock);
107 req->rsrc_node = ctx->rsrc_node;
108 io_charge_rsrc_node(ctx, ctx->rsrc_node);
109}
110
Jens Axboe73572982022-06-13 07:12:45 -0600111static inline void io_req_set_rsrc_node(struct io_kiocb *req,
112 struct io_ring_ctx *ctx,
113 unsigned int issue_flags)
114{
115 if (!req->rsrc_node) {
Pavel Begunkov4ff0b502023-03-29 15:03:43 +0100116 io_ring_submit_lock(ctx, issue_flags);
Jens Axboe3f302382024-01-11 13:34:33 -0700117 __io_req_set_rsrc_node(req, ctx);
Pavel Begunkov4ff0b502023-03-29 15:03:43 +0100118 io_ring_submit_unlock(ctx, issue_flags);
Jens Axboe73572982022-06-13 07:12:45 -0600119 }
120}
121
122static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
123{
124 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
125 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
126
127 return &data->tags[table_idx][off];
128}
129
Pavel Begunkov2933ae62023-04-11 12:06:07 +0100130static inline int io_rsrc_init(struct io_ring_ctx *ctx)
131{
132 ctx->rsrc_node = io_rsrc_node_alloc(ctx);
133 return ctx->rsrc_node ? 0 : -ENOMEM;
134}
135
Pavel Begunkovd9808ce2022-09-01 11:54:02 +0100136int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
137int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
Pavel Begunkov6a9ce662022-07-25 10:52:05 +0100138
139int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
140
141static inline void __io_unaccount_mem(struct user_struct *user,
142 unsigned long nr_pages)
143{
144 atomic_long_sub(nr_pages, &user->locked_vm);
145}
146
Jens Axboe73572982022-06-13 07:12:45 -0600147#endif