Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1751420pxj; Wed, 19 May 2021 13:04:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyF5ornQiNK9zJ6isw5yNFFGgbZW7SLZA37gwXnZYAXiDtRWqzcnkPSYP+zuRW7XYxTBl4J X-Received: by 2002:a17:907:9612:: with SMTP id gb18mr900670ejc.408.1621454691895; Wed, 19 May 2021 13:04:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621454691; cv=none; d=google.com; s=arc-20160816; b=gzNO9Zh44jn8+rL+5GJJ/rODGtHLSboJrnwyxSFXsZbVmW+8MozndDDLbQGPKwNczI s0eswtLRY8j69xK0BDjvkDBNJgVSaIoCVzoHgDq65EgqywDh6Xb5l10iOXcKsVfgPwZ/ ohf/5idG191PNebW7sYYc41D27RHZaNNv57Q9rf9BvW0Tc9JS270bSb40JUrKC2ionEH NhCu2NG8zyFNPQIZZR+w00OTNY1csEKJGjIuetA5PHnQfbVb+2U93assyBKQJWhfDKYU kQ4SGXxB0mJshNDPkc1q4/UAiSWuwczWQQKFhfR7uZhgJAl0vQcc7lAMniQhwO+wRyn3 db7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=h10pgO2Ruz23aXWMMYRea29pOW4EecEeGd3AsobhL9Y=; b=ReAatLWzmEY7nitSpYjhR2419NGFKxV0h//kGCSP8pYZHpWUR9ATjg1unIZCBPF9F/ 8YjqOwgApRUio6lr6K+ul78HKsk+jMU//D8aDG71DoDX5hGvhI6+eq1QMgSrH/w1qCC9 LjlwI6UR/PMHL8KB29/xy1D/sKle+Iaa7yIyMPzjVZTrU8HQhOnJ7+NnUrSw6xUS3wYR mdyudfgq0rck83tMLHIBuYnZWEs22xdkYKGUR15d5pTM3Bvx8aMtBPTv86mv8t9NuDEg ZuQIMi3OFFP1U8EJDOzCJDFopnw8wmW0Bx40tEpbqy+ZPWm6qr3Z/nSLN/LtQTcc0WNT dGgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=TTGtF9J0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y22si182114edu.331.2021.05.19.13.04.27; Wed, 19 May 2021 13:04:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=TTGtF9J0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346982AbhESOQ7 (ORCPT + 99 others); Wed, 19 May 2021 10:16:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354040AbhESOPt (ORCPT ); Wed, 19 May 2021 10:15:49 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75F82C0613CE; Wed, 19 May 2021 07:14:12 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id l18-20020a1ced120000b029014c1adff1edso3530350wmh.4; Wed, 19 May 2021 07:14:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=h10pgO2Ruz23aXWMMYRea29pOW4EecEeGd3AsobhL9Y=; b=TTGtF9J0FTnKB4QaPpfc7o8poBmJ4z/D3nXatcnaCO5uZpjltSL03x9BZTehpnPJO6 jwFUZL3OpnxkD7O5/p7ECuqrH4V9vLl5Fvzz8qIpRFKzZ8mp0xiMjlC2TBr4m0VDR6VJ miFSU2db+Ph7fGajw1OT1Jv4y5o4L9+PPCzboHQP+mK+inYq5VUdaLIImNIWIEtT/wtL CfC0tdYfgWyPe877Eb2KUD55n4nCZSIH935Ox1qeDkRK8cfIrvoN73m4+ZEWEm2PU546 bAtzhTdoziCudpYx2ZqMTmBu0CWZPYXk1DFK/wTI799s7yXFkFzEodwMC4yzOKMtMvrJ /jkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=h10pgO2Ruz23aXWMMYRea29pOW4EecEeGd3AsobhL9Y=; b=n17Y7nELT3uSarWYEsdNVepF8CofQjQOlB+KMlahdHCAfax+2e5GsHNvWVVp2PZ8Pk O6ZHSphvQDB1UY+rWI/5QPBPHoJARKlAOvKfr6Xbnkch5PwyNPVrEfvmSo9jGFqqlyT7 SCY+4bbxcsO50DHwpxuD/tZ+S1QFEg87867xDsh+Iz1pGNbvRNIt0gTFH9nkbdYQmU9G Odaif0mp5VYjfha+mdCXcQXhmVVM5ae3OSmaR3IDEqqsVNq48viFAHdHPkVes025cxyl i2Y3hxCXh4W16/dM/Xg3g7HQbbb3H4z3/73187oxYDuBqyVL7kCOcq0TbpVBCa1pNoAU 6dHQ== X-Gm-Message-State: AOAM531T/vgnkmQaft8BnQrfu+8txZaZcfQWEV611RZ35Fx6NlMxuZfo amuIhVxfNOfggOL2beHSH3/46fuq/Rm1BM+R X-Received: by 2002:a7b:c92e:: with SMTP id h14mr11734868wml.179.1621433650847; Wed, 19 May 2021 07:14:10 -0700 (PDT) Received: from localhost.localdomain ([85.255.235.154]) by smtp.gmail.com with ESMTPSA id z3sm6233569wrq.42.2021.05.19.07.14.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 May 2021 07:14:10 -0700 (PDT) From: Pavel Begunkov To: io-uring@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Jens Axboe , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Horst Schirmeier , "Franz-B . Tuneke" , Christian Dietrich Subject: [PATCH 17/23] io_uring: enable bpf to reap CQEs Date: Wed, 19 May 2021 15:13:28 +0100 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Signed-off-by: Pavel Begunkov --- fs/io_uring.c | 48 ++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/bpf.h | 1 + 2 files changed, 49 insertions(+) diff --git a/fs/io_uring.c b/fs/io_uring.c index 464d630904e2..7c165b2ce8e4 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -10394,6 +10394,42 @@ BPF_CALL_5(io_bpf_emit_cqe, struct io_bpf_ctx *, bpf_ctx, return submitted ? 0 : -ENOMEM; } +BPF_CALL_4(io_bpf_reap_cqe, struct io_bpf_ctx *, bpf_ctx, + u32, cq_idx, + struct io_uring_cqe *, cqe_out, + u32, cqe_len) +{ + struct io_ring_ctx *ctx = bpf_ctx->ctx; + struct io_uring_cqe *cqe; + struct io_cqring *cq; + struct io_rings *r; + unsigned tail, head, mask; + int ret = -EINVAL; + + if (unlikely(cqe_len != sizeof(*cqe_out))) + goto err; + if (unlikely(cq_idx >= ctx->cq_nr)) + goto err; + + cq = &ctx->cqs[cq_idx]; + r = cq->rings; + tail = READ_ONCE(r->cq.tail); + head = smp_load_acquire(&r->cq.head); + + ret = -ENOENT; + if (unlikely(tail == head)) + goto err; + + mask = cq->entries - 1; + cqe = &r->cqes[head & mask]; + memcpy(cqe_out, cqe, sizeof(*cqe_out)); + WRITE_ONCE(r->cq.head, head + 1); + return 0; +err: + memset(cqe_out, 0, sizeof(*cqe_out)); + return ret; +} + const struct bpf_func_proto io_bpf_queue_sqe_proto = { .func = io_bpf_queue_sqe, .gpl_only = false, @@ -10414,6 +10450,16 @@ const struct bpf_func_proto io_bpf_emit_cqe_proto = { .arg5_type = ARG_ANYTHING, }; +const struct bpf_func_proto io_bpf_reap_cqe_proto = { + .func = io_bpf_reap_cqe, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_ANYTHING, + .arg3_type = ARG_PTR_TO_UNINIT_MEM, + .arg4_type = ARG_CONST_SIZE, +}; + static const struct bpf_func_proto * io_bpf_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -10424,6 +10470,8 @@ io_bpf_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return prog->aux->sleepable ? &io_bpf_queue_sqe_proto : NULL; case BPF_FUNC_iouring_emit_cqe: return &io_bpf_emit_cqe_proto; + case BPF_FUNC_iouring_reap_cqe: + return &io_bpf_reap_cqe_proto; default: return bpf_base_func_proto(func_id); } diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index c6b023be7848..7719ec4a33e7 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4084,6 +4084,7 @@ union bpf_attr { FN(check_mtu), \ FN(iouring_queue_sqe), \ FN(iouring_emit_cqe), \ + FN(iouring_reap_cqe), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper -- 2.31.1