Skip to content

Commit a69307a

Browse files
committed
io_uring/kbuf: turn io_buffer_list booleans into flags
We could just move these two and save some space, but in preparation for adding another flag, turn them into flags first. This saves 8 bytes in struct io_buffer_list, making it exactly half a cacheline on 64-bit archs now rather than 40 bytes. Signed-off-by: Jens Axboe <[email protected]>
1 parent 566a424 commit a69307a

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

io_uring/kbuf.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
189189

190190
bl = io_buffer_get_list(ctx, req->buf_index);
191191
if (likely(bl)) {
192-
if (bl->is_buf_ring)
192+
if (bl->flags & IOBL_BUF_RING)
193193
ret = io_ring_buffer_select(req, len, bl, issue_flags);
194194
else
195195
ret = io_provided_buffer_select(req, len, bl);
@@ -287,7 +287,7 @@ int io_buffers_select(struct io_kiocb *req, struct buf_sel_arg *arg,
287287
if (unlikely(!bl))
288288
goto out_unlock;
289289

290-
if (bl->is_buf_ring) {
290+
if (bl->flags & IOBL_BUF_RING) {
291291
ret = io_ring_buffers_peek(req, arg, bl);
292292
/*
293293
* Don't recycle these buffers if we need to go through poll.
@@ -320,7 +320,7 @@ int io_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg)
320320
if (unlikely(!bl))
321321
return -ENOENT;
322322

323-
if (bl->is_buf_ring) {
323+
if (bl->flags & IOBL_BUF_RING) {
324324
ret = io_ring_buffers_peek(req, arg, bl);
325325
if (ret > 0)
326326
req->flags |= REQ_F_BUFFERS_COMMIT;
@@ -340,22 +340,22 @@ static int __io_remove_buffers(struct io_ring_ctx *ctx,
340340
if (!nbufs)
341341
return 0;
342342

343-
if (bl->is_buf_ring) {
343+
if (bl->flags & IOBL_BUF_RING) {
344344
i = bl->buf_ring->tail - bl->head;
345345
if (bl->buf_nr_pages) {
346346
int j;
347347

348-
if (!bl->is_mmap) {
348+
if (!(bl->flags & IOBL_MMAP)) {
349349
for (j = 0; j < bl->buf_nr_pages; j++)
350350
unpin_user_page(bl->buf_pages[j]);
351351
}
352352
io_pages_unmap(bl->buf_ring, &bl->buf_pages,
353-
&bl->buf_nr_pages, bl->is_mmap);
354-
bl->is_mmap = 0;
353+
&bl->buf_nr_pages, bl->flags & IOBL_MMAP);
354+
bl->flags &= ~IOBL_MMAP;
355355
}
356356
/* make sure it's seen as empty */
357357
INIT_LIST_HEAD(&bl->buf_list);
358-
bl->is_buf_ring = 0;
358+
bl->flags &= ~IOBL_BUF_RING;
359359
return i;
360360
}
361361

@@ -442,7 +442,7 @@ int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
442442
if (bl) {
443443
ret = -EINVAL;
444444
/* can't use provide/remove buffers command on mapped buffers */
445-
if (!bl->is_buf_ring)
445+
if (!(bl->flags & IOBL_BUF_RING))
446446
ret = __io_remove_buffers(ctx, bl, p->nbufs);
447447
}
448448
io_ring_submit_unlock(ctx, issue_flags);
@@ -589,7 +589,7 @@ int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
589589
}
590590
}
591591
/* can't add buffers via this command for a mapped buffer ring */
592-
if (bl->is_buf_ring) {
592+
if (bl->flags & IOBL_BUF_RING) {
593593
ret = -EINVAL;
594594
goto err;
595595
}
@@ -641,8 +641,8 @@ static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg,
641641
bl->buf_pages = pages;
642642
bl->buf_nr_pages = nr_pages;
643643
bl->buf_ring = br;
644-
bl->is_buf_ring = 1;
645-
bl->is_mmap = 0;
644+
bl->flags |= IOBL_BUF_RING;
645+
bl->flags &= ~IOBL_MMAP;
646646
return 0;
647647
error_unpin:
648648
unpin_user_pages(pages, nr_pages);
@@ -665,8 +665,7 @@ static int io_alloc_pbuf_ring(struct io_ring_ctx *ctx,
665665
return -ENOMEM;
666666
}
667667

668-
bl->is_buf_ring = 1;
669-
bl->is_mmap = 1;
668+
bl->flags |= (IOBL_BUF_RING | IOBL_MMAP);
670669
return 0;
671670
}
672671

@@ -705,7 +704,7 @@ int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
705704
bl = io_buffer_get_list(ctx, reg.bgid);
706705
if (bl) {
707706
/* if mapped buffer ring OR classic exists, don't allow */
708-
if (bl->is_buf_ring || !list_empty(&bl->buf_list))
707+
if (bl->flags & IOBL_BUF_RING || !list_empty(&bl->buf_list))
709708
return -EEXIST;
710709
} else {
711710
free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL);
@@ -747,7 +746,7 @@ int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
747746
bl = io_buffer_get_list(ctx, reg.bgid);
748747
if (!bl)
749748
return -ENOENT;
750-
if (!bl->is_buf_ring)
749+
if (!(bl->flags & IOBL_BUF_RING))
751750
return -EINVAL;
752751

753752
xa_erase(&ctx->io_bl_xa, bl->bgid);
@@ -771,7 +770,7 @@ int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg)
771770
bl = io_buffer_get_list(ctx, buf_status.buf_group);
772771
if (!bl)
773772
return -ENOENT;
774-
if (!bl->is_buf_ring)
773+
if (!(bl->flags & IOBL_BUF_RING))
775774
return -EINVAL;
776775

777776
buf_status.head = bl->head;
@@ -802,7 +801,7 @@ struct io_buffer_list *io_pbuf_get_bl(struct io_ring_ctx *ctx,
802801
bl = xa_load(&ctx->io_bl_xa, bgid);
803802
/* must be a mmap'able buffer ring and have pages */
804803
ret = false;
805-
if (bl && bl->is_mmap)
804+
if (bl && bl->flags & IOBL_MMAP)
806805
ret = atomic_inc_not_zero(&bl->refs);
807806
rcu_read_unlock();
808807

io_uring/kbuf.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
#include <uapi/linux/io_uring.h>
66

7+
enum {
8+
/* ring mapped provided buffers */
9+
IOBL_BUF_RING = 1,
10+
/* ring mapped provided buffers, but mmap'ed by application */
11+
IOBL_MMAP = 2,
12+
};
13+
714
struct io_buffer_list {
815
/*
916
* If ->buf_nr_pages is set, then buf_pages/buf_ring are used. If not,
@@ -25,12 +32,9 @@ struct io_buffer_list {
2532
__u16 head;
2633
__u16 mask;
2734

28-
atomic_t refs;
35+
__u16 flags;
2936

30-
/* ring mapped provided buffers */
31-
__u8 is_buf_ring;
32-
/* ring mapped provided buffers, but mmap'ed by application */
33-
__u8 is_mmap;
37+
atomic_t refs;
3438
};
3539

3640
struct io_buffer {

0 commit comments

Comments
 (0)