Skip to content

Commit e1d4995

Browse files
isilenceaxboe
authored andcommitted
io_uring: introduce struct iou_vec
I need a convenient way to pass around and work with iovec+size pair, put them into a structure and makes use of it in rw.c Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Link: https://lore.kernel.org/r/d39fadafc9e9047b0a292e5be6db3cf2f48bb1f7.1741362889.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 6e3da40 commit e1d4995

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

include/linux/io_uring_types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ struct io_uring_task {
110110
} ____cacheline_aligned_in_smp;
111111
};
112112

113+
struct iou_vec {
114+
struct iovec *iovec;
115+
unsigned nr;
116+
};
117+
113118
struct io_uring {
114119
u32 head;
115120
u32 tail;

io_uring/rsrc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,3 +1260,12 @@ int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg)
12601260
fput(file);
12611261
return ret;
12621262
}
1263+
1264+
void io_vec_free(struct iou_vec *iv)
1265+
{
1266+
if (!iv->iovec)
1267+
return;
1268+
kfree(iv->iovec);
1269+
iv->iovec = NULL;
1270+
iv->nr = 0;
1271+
}

io_uring/rsrc.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,20 @@ static inline void __io_unaccount_mem(struct user_struct *user,
145145
atomic_long_sub(nr_pages, &user->locked_vm);
146146
}
147147

148+
void io_vec_free(struct iou_vec *iv);
149+
150+
static inline void io_vec_reset_iovec(struct iou_vec *iv,
151+
struct iovec *iovec, unsigned nr)
152+
{
153+
io_vec_free(iv);
154+
iv->iovec = iovec;
155+
iv->nr = nr;
156+
}
157+
158+
static inline void io_alloc_cache_vec_kasan(struct iou_vec *iv)
159+
{
160+
if (IS_ENABLED(CONFIG_KASAN))
161+
io_vec_free(iv);
162+
}
163+
148164
#endif

io_uring/rw.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ static int io_import_vec(int ddir, struct io_kiocb *req,
8787
int ret, nr_segs;
8888
struct iovec *iov;
8989

90-
if (io->free_iovec) {
91-
nr_segs = io->free_iov_nr;
92-
iov = io->free_iovec;
90+
if (io->vec.iovec) {
91+
nr_segs = io->vec.nr;
92+
iov = io->vec.iovec;
9393
} else {
9494
nr_segs = 1;
9595
iov = &io->fast_iov;
@@ -101,9 +101,7 @@ static int io_import_vec(int ddir, struct io_kiocb *req,
101101
return ret;
102102
if (iov) {
103103
req->flags |= REQ_F_NEED_CLEANUP;
104-
io->free_iov_nr = io->iter.nr_segs;
105-
kfree(io->free_iovec);
106-
io->free_iovec = iov;
104+
io_vec_reset_iovec(&io->vec, iov, io->iter.nr_segs);
107105
}
108106
return 0;
109107
}
@@ -151,7 +149,7 @@ static void io_rw_recycle(struct io_kiocb *req, unsigned int issue_flags)
151149
if (unlikely(issue_flags & IO_URING_F_UNLOCKED))
152150
return;
153151

154-
io_alloc_cache_kasan(&rw->free_iovec, &rw->free_iov_nr);
152+
io_alloc_cache_vec_kasan(&rw->vec);
155153
if (io_alloc_cache_put(&req->ctx->rw_cache, rw)) {
156154
req->async_data = NULL;
157155
req->flags &= ~REQ_F_ASYNC_DATA;
@@ -201,7 +199,7 @@ static int io_rw_alloc_async(struct io_kiocb *req)
201199
rw = io_uring_alloc_async_data(&ctx->rw_cache, req);
202200
if (!rw)
203201
return -ENOMEM;
204-
if (rw->free_iovec)
202+
if (rw->vec.iovec)
205203
req->flags |= REQ_F_NEED_CLEANUP;
206204
rw->bytes_done = 0;
207205
return 0;
@@ -1327,7 +1325,6 @@ void io_rw_cache_free(const void *entry)
13271325
{
13281326
struct io_async_rw *rw = (struct io_async_rw *) entry;
13291327

1330-
if (rw->free_iovec)
1331-
kfree(rw->free_iovec);
1328+
io_vec_free(&rw->vec);
13321329
kfree(rw);
13331330
}

io_uring/rw.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ struct io_meta_state {
99
};
1010

1111
struct io_async_rw {
12+
struct iou_vec vec;
1213
size_t bytes_done;
13-
struct iovec *free_iovec;
14+
1415
struct_group(clear,
1516
struct iov_iter iter;
1617
struct iov_iter_state iter_state;
1718
struct iovec fast_iov;
18-
int free_iov_nr;
1919
/*
2020
* wpq is for buffered io, while meta fields are used with
2121
* direct io

0 commit comments

Comments
 (0)