Re: [PATCH 15/23] io_uring: enable BPF to submit SQEs

From: Alexei Starovoitov
Date: Thu May 20 2021 - 21:08:02 EST


On Wed, May 19, 2021 at 03:13:26PM +0100, Pavel Begunkov wrote:
>
> +BPF_CALL_3(io_bpf_queue_sqe, struct io_bpf_ctx *, bpf_ctx,
> + const struct io_uring_sqe *, sqe,
> + u32, sqe_len)
> +{
> + struct io_ring_ctx *ctx = bpf_ctx->ctx;
> + struct io_kiocb *req;
> +
> + if (sqe_len != sizeof(struct io_uring_sqe))
> + return -EINVAL;
> +
> + req = io_alloc_req(ctx);

that is GFP_KERNEL allocation.
It's only allowed from sleepable bpf progs and further down
there is a correct check for it, so all good.
But submitting sqe is a fundemntal io_uring operation,
so what is the use case for non-sleepable?
In other words why bother? Allow sleepable only and simplify the code?

> + if (unlikely(!req))
> + return -ENOMEM;
> + if (!percpu_ref_tryget_many(&ctx->refs, 1)) {
> + kmem_cache_free(req_cachep, req);
> + return -EAGAIN;
> + }
> + percpu_counter_add(&current->io_uring->inflight, 1);
> + refcount_add(1, &current->usage);
> +
> + /* returns number of submitted SQEs or an error */
> + return !io_submit_sqe(ctx, req, sqe);

A buggy bpf prog will be able to pass junk sizeof(struct io_uring_sqe)
as 'sqe' here.
What kind of validation io_submit_sqe() does to avoid crashing the kernel?

General comments that apply to all patches:
- commit logs are way too terse. Pls expand with details.
- describe new bpf helpers in comments in bpf.h. Just adding them to an enum is not enough.
- selftest/bpf are mandatory for all new bpf features.
- consider bpf_link style of attaching bpf progs. We had enough issues with progs
that get stuck due to application bugs. Auto-detach saves the day more often than not.