Skip to content

Commit 4bfb0c9

Browse files
Christoph Hellwigaxboe
authored andcommitted
io_uring: add helpers to decode the fixed file file_ptr
Remove all the open coded magic on slot->file_ptr by introducing two helpers that return the file pointer and the flags instead. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20230620113235.920399-9-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent f432c8c commit 4bfb0c9

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

io_uring/filetable.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,
7878
file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
7979

8080
if (file_slot->file_ptr) {
81-
struct file *old_file;
82-
83-
old_file = (struct file *)(file_slot->file_ptr & FFS_MASK);
84-
ret = io_queue_rsrc_removal(ctx->file_data, slot_index, old_file);
81+
ret = io_queue_rsrc_removal(ctx->file_data, slot_index,
82+
io_slot_file(file_slot));
8583
if (ret)
8684
return ret;
8785

@@ -140,7 +138,6 @@ int io_fixed_fd_install(struct io_kiocb *req, unsigned int issue_flags,
140138
int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
141139
{
142140
struct io_fixed_file *file_slot;
143-
struct file *file;
144141
int ret;
145142

146143
if (unlikely(!ctx->file_data))
@@ -153,8 +150,8 @@ int io_fixed_fd_remove(struct io_ring_ctx *ctx, unsigned int offset)
153150
if (!file_slot->file_ptr)
154151
return -EBADF;
155152

156-
file = (struct file *)(file_slot->file_ptr & FFS_MASK);
157-
ret = io_queue_rsrc_removal(ctx->file_data, offset, file);
153+
ret = io_queue_rsrc_removal(ctx->file_data, offset,
154+
io_slot_file(file_slot));
158155
if (ret)
159156
return ret;
160157

io_uring/filetable.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
#include <linux/file.h>
66
#include <linux/io_uring_types.h>
77

8-
#define FFS_NOWAIT 0x1UL
9-
#define FFS_ISREG 0x2UL
10-
#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG)
11-
128
bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files);
139
void io_free_file_tables(struct io_file_table *table);
1410

@@ -43,12 +39,24 @@ io_fixed_file_slot(struct io_file_table *table, unsigned i)
4339
return &table->files[i];
4440
}
4541

42+
#define FFS_NOWAIT 0x1UL
43+
#define FFS_ISREG 0x2UL
44+
#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG)
45+
46+
static inline unsigned int io_slot_flags(struct io_fixed_file *slot)
47+
{
48+
return (slot->file_ptr & ~FFS_MASK) << REQ_F_SUPPORT_NOWAIT_BIT;
49+
}
50+
51+
static inline struct file *io_slot_file(struct io_fixed_file *slot)
52+
{
53+
return (struct file *)(slot->file_ptr & FFS_MASK);
54+
}
55+
4656
static inline struct file *io_file_from_index(struct io_file_table *table,
4757
int index)
4858
{
49-
struct io_fixed_file *slot = io_fixed_file_slot(table, index);
50-
51-
return (struct file *) (slot->file_ptr & FFS_MASK);
59+
return io_slot_file(io_fixed_file_slot(table, index));
5260
}
5361

5462
static inline void io_fixed_file_set(struct io_fixed_file *file_slot,

io_uring/io_uring.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,19 +2028,17 @@ inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
20282028
unsigned int issue_flags)
20292029
{
20302030
struct io_ring_ctx *ctx = req->ctx;
2031+
struct io_fixed_file *slot;
20312032
struct file *file = NULL;
2032-
unsigned long file_ptr;
20332033

20342034
io_ring_submit_lock(ctx, issue_flags);
20352035

20362036
if (unlikely((unsigned int)fd >= ctx->nr_user_files))
20372037
goto out;
20382038
fd = array_index_nospec(fd, ctx->nr_user_files);
2039-
file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
2040-
file = (struct file *) (file_ptr & FFS_MASK);
2041-
file_ptr &= ~FFS_MASK;
2042-
/* mask in overlapping REQ_F and FFS bits */
2043-
req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT);
2039+
slot = io_fixed_file_slot(&ctx->file_table, fd);
2040+
file = io_slot_file(slot);
2041+
req->flags |= io_slot_flags(slot);
20442042
io_req_set_rsrc_node(req, ctx, 0);
20452043
out:
20462044
io_ring_submit_unlock(ctx, issue_flags);

io_uring/rsrc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
354354
__s32 __user *fds = u64_to_user_ptr(up->data);
355355
struct io_rsrc_data *data = ctx->file_data;
356356
struct io_fixed_file *file_slot;
357-
struct file *file;
358357
int fd, i, err = 0;
359358
unsigned int done;
360359

@@ -382,15 +381,16 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
382381
file_slot = io_fixed_file_slot(&ctx->file_table, i);
383382

384383
if (file_slot->file_ptr) {
385-
file = (struct file *)(file_slot->file_ptr & FFS_MASK);
386-
err = io_queue_rsrc_removal(data, i, file);
384+
err = io_queue_rsrc_removal(data, i,
385+
io_slot_file(file_slot));
387386
if (err)
388387
break;
389388
file_slot->file_ptr = 0;
390389
io_file_bitmap_clear(&ctx->file_table, i);
391390
}
392391
if (fd != -1) {
393-
file = fget(fd);
392+
struct file *file = fget(fd);
393+
394394
if (!file) {
395395
err = -EBADF;
396396
break;

0 commit comments

Comments
 (0)