Skip to content

Commit d21c362

Browse files
committed
io_uring/bpf_filter: move filter size and populate helper into struct
Rather than open-code this logic in io_uring_populate_bpf_ctx() with a switch, move it to the issue side definitions. Outside of making this easier to extend in the future, it's also a prep patch for using the pdu size for a given opcode filter elsewhere. Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 22dbb09 commit d21c362

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

io_uring/bpf_filter.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ static const struct io_bpf_filter dummy_filter;
2626
static void io_uring_populate_bpf_ctx(struct io_uring_bpf_ctx *bctx,
2727
struct io_kiocb *req)
2828
{
29+
const struct io_issue_def *def = &io_issue_defs[req->opcode];
30+
2931
bctx->opcode = req->opcode;
3032
bctx->sqe_flags = (__force int) req->flags & SQE_VALID_FLAGS;
3133
bctx->user_data = req->cqe.user_data;
@@ -34,19 +36,12 @@ static void io_uring_populate_bpf_ctx(struct io_uring_bpf_ctx *bctx,
3436
sizeof(*bctx) - offsetof(struct io_uring_bpf_ctx, pdu_size));
3537

3638
/*
37-
* Opcodes can provide a handler fo populating more data into bctx,
39+
* Opcodes can provide a handler for populating more data into bctx,
3840
* for filters to use.
3941
*/
40-
switch (req->opcode) {
41-
case IORING_OP_SOCKET:
42-
bctx->pdu_size = sizeof(bctx->socket);
43-
io_socket_bpf_populate(bctx, req);
44-
break;
45-
case IORING_OP_OPENAT:
46-
case IORING_OP_OPENAT2:
47-
bctx->pdu_size = sizeof(bctx->open);
48-
io_openat_bpf_populate(bctx, req);
49-
break;
42+
if (def->filter_pdu_size) {
43+
bctx->pdu_size = def->filter_pdu_size;
44+
def->filter_populate(bctx, req);
5045
}
5146
}
5247

io_uring/opdef.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ const struct io_issue_def io_issue_defs[] = {
221221
.issue = io_fallocate,
222222
},
223223
[IORING_OP_OPENAT] = {
224+
.filter_pdu_size = sizeof_field(struct io_uring_bpf_ctx, open),
224225
.prep = io_openat_prep,
225226
.issue = io_openat,
227+
.filter_populate = io_openat_bpf_populate,
226228
},
227229
[IORING_OP_CLOSE] = {
228230
.prep = io_close_prep,
@@ -309,8 +311,10 @@ const struct io_issue_def io_issue_defs[] = {
309311
#endif
310312
},
311313
[IORING_OP_OPENAT2] = {
314+
.filter_pdu_size = sizeof_field(struct io_uring_bpf_ctx, open),
312315
.prep = io_openat2_prep,
313316
.issue = io_openat2,
317+
.filter_populate = io_openat_bpf_populate,
314318
},
315319
[IORING_OP_EPOLL_CTL] = {
316320
.unbound_nonreg_file = 1,
@@ -406,8 +410,10 @@ const struct io_issue_def io_issue_defs[] = {
406410
[IORING_OP_SOCKET] = {
407411
.audit_skip = 1,
408412
#if defined(CONFIG_NET)
413+
.filter_pdu_size = sizeof_field(struct io_uring_bpf_ctx, socket),
409414
.prep = io_socket_prep,
410415
.issue = io_socket,
416+
.filter_populate = io_socket_bpf_populate,
411417
#else
412418
.prep = io_eopnotsupp_prep,
413419
#endif

io_uring/opdef.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#ifndef IOU_OP_DEF_H
33
#define IOU_OP_DEF_H
44

5+
struct io_uring_bpf_ctx;
6+
57
struct io_issue_def {
68
/* needs req->file assigned */
79
unsigned needs_file : 1;
@@ -33,8 +35,12 @@ struct io_issue_def {
3335
/* size of async data needed, if any */
3436
unsigned short async_size;
3537

38+
/* bpf filter pdu size, if any */
39+
unsigned short filter_pdu_size;
40+
3641
int (*issue)(struct io_kiocb *, unsigned int);
3742
int (*prep)(struct io_kiocb *, const struct io_uring_sqe *);
43+
void (*filter_populate)(struct io_uring_bpf_ctx *, struct io_kiocb *);
3844
};
3945

4046
struct io_cold_def {

0 commit comments

Comments
 (0)