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

From: Pavel Begunkov
Date: Fri May 21 2021 - 05:34:05 EST


On 5/21/21 2:07 AM, Alexei Starovoitov wrote:
> 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?

Actual submission may be moved out of BPF, so enabling it for both, but
the question I wonder about is what are the plans for sleepable
programs? E.g. if it's a marginal features much limited in
functionality, e.g. iirc as it's not allowed to use some BPF data
types, it may not worth doing.

>
>> + 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?

It works on memory rw shared with userspace, so it already assumes
the worst

> 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.

Thanks for taking a look! I have no idea what bpf_link is, need
to check it out

--
Pavel Begunkov