From patchwork Mon Sep 11 10:34:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 137929 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp17368vqx; Mon, 11 Sep 2023 14:38:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFw3a8rYztPUFFDqoXuAYdAhJJgWilAjvKQdEiZqTeTeCV8GDdpEMgc78NpFur1p4KfcVjm X-Received: by 2002:aa7:d694:0:b0:525:6d6e:ed53 with SMTP id d20-20020aa7d694000000b005256d6eed53mr8134053edr.27.1694468327310; Mon, 11 Sep 2023 14:38:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694468327; cv=none; d=google.com; s=arc-20160816; b=Ic5GRGOOJlzSjKUn+p5n/02fklcIkJnuRhk6LyvBZZQoXH4KePoxM9YwicJlWbnxi6 pgRIEh1iYe7ZXZ5/pvte9s+UvSg7l6aJdjCbbr5fOjZMVa9JBrMUArKUmypPMs9h1i5R lfQZ67GfOZif3fDlYoXiFxmekKPn8D3cSJIISQYgRx86hgpCVZybv+MT6OAoWfudkgr9 8G6S+K+UR1lovf26B0RRdZgMFNoyw0UXX3EwGRdfrC6sRfgIBUBDX68f9RcccL/pL3h1 gboGbe93AiI0fF3wKGL4/5XBxsRszBIUGnMek2FmNaMIpDHwbcK2WyNBcgNFPE/RsSN1 t1Vw== 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; bh=T63D+iseOM6feL2ViRPYbx+xbxxkgrRy6waBaJFXQkc=; fh=RtbdsQwkhxv3nsDPwbAwqLTGOpj8SJeGLKms3wv3Z/8=; b=pNtfB48lQAPIPYF3rmCIMeeplyXBboX+W+fU6kf5fZYG1Jxavdo1vbV6Ag+JRn9Pqg WVv3goddriLuGFfHHM+dBS0LH9HC7FUrBPFbidGkKAapbyXFCvm1MfvG1Q3Ow23wRqcF fhZMBooOVwRJ4iBr/Jo3dMEk4HUuc9KV0vye9VcWDTrEGHuaH27tnzspKvVot4e6qK87 ktmjRNzHsiTjm4yZMipBBtrRuMIuS7D7BxZV5taEGkL0ARmPOBuJqI0nmJBb4k5RYRrP 1IcumJtsXDy2YceMpO0tTxpwQYMuG4ZkxBjGi0a/ur4kGfgebZxX6LwkR/zZ1XK1O5GF ZEww== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m16-20020aa7d350000000b0052e25f4b2d1si7048672edr.223.2023.09.11.14.38.23; Mon, 11 Sep 2023 14:38:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349811AbjIKVez (ORCPT + 99 others); Mon, 11 Sep 2023 17:34:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236406AbjIKKeb (ORCPT ); Mon, 11 Sep 2023 06:34:31 -0400 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0C94120; Mon, 11 Sep 2023 03:34:26 -0700 (PDT) Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-99bcfe28909so533695066b.3; Mon, 11 Sep 2023 03:34:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694428465; x=1695033265; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T63D+iseOM6feL2ViRPYbx+xbxxkgrRy6waBaJFXQkc=; b=F5hyEGX3FBvG4wSuDLNanCDzwWIu0uUOZNxdN8JjGHbsgxO+rQbRomrOMtGbMHLwNI yVZFUx5Iwmpj7NrGlcKxqx8y9p6hkzV3kq7cu/mRAYanHjzeA8fp6cm46QsGYYBtGPUq q1nf3BHWvTqEv1lGtQ17Nrectxa78Dl9rOh7YpjEtCpkwFulDc+G/0qAC1rWDSc0i3cA E8nTWR+zp/F8xB4uCzus3if/osegoqdcm9hHgG37+V5Sm0z7C4OiEI+cCu1OOU4TMLlw 1Gq9oMCZIIRDbp2Em97YMNVknjnoFHcCvfdJUIBettRV4AwvuP164z2jrl4+7CZ+GWwV 4mYw== X-Gm-Message-State: AOJu0Yxrr8yNPVW4FYX3w/nXB/7u70i8BLfK19sHfpBZYDWRG6FCM8C6 hPzoepT+foj+2xAjX5pTuMA= X-Received: by 2002:a17:906:1cf:b0:9a1:c991:a521 with SMTP id 15-20020a17090601cf00b009a1c991a521mr8145455ejj.4.1694428465179; Mon, 11 Sep 2023 03:34:25 -0700 (PDT) Received: from localhost (fwdproxy-cln-012.fbsv.net. [2a03:2880:31ff:c::face:b00c]) by smtp.gmail.com with ESMTPSA id b9-20020a170906490900b00992f2befcbcsm5131977ejq.180.2023.09.11.03.34.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 03:34:24 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, martin.lau@linux.dev, krisman@suse.de, "David S. Miller" , Eric Dumazet , Paolo Abeni Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, io-uring@vger.kernel.org, Willem de Bruijn Subject: [PATCH v5 1/8] net/socket: Break down __sys_setsockopt Date: Mon, 11 Sep 2023 03:34:00 -0700 Message-Id: <20230911103407.1393149-2-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230911103407.1393149-1-leitao@debian.org> References: <20230911103407.1393149-1-leitao@debian.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776778820778556727 X-GMAIL-MSGID: 1776778820778556727 Split __sys_setsockopt() into two functions by removing the core logic into a sub-function (do_sock_setsockopt()). This will avoid code duplication when doing the same operation in other callers, for instance. do_sock_setsockopt() will be called by io_uring setsockopt() command operation in the following patch. Signed-off-by: Breno Leitao Reviewed-by: Willem de Bruijn --- include/net/sock.h | 2 ++ net/socket.c | 38 +++++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/include/net/sock.h b/include/net/sock.h index 11d503417591..aa8fb54ad0af 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1861,6 +1861,8 @@ int sk_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, unsigned int optlen); int sock_setsockopt(struct socket *sock, int level, int op, sockptr_t optval, unsigned int optlen); +int do_sock_setsockopt(struct socket *sock, bool compat, int level, + int optname, char __user *user_optval, int optlen); int sk_getsockopt(struct sock *sk, int level, int optname, sockptr_t optval, sockptr_t optlen); diff --git a/net/socket.c b/net/socket.c index 77f28328e387..360332e098d4 100644 --- a/net/socket.c +++ b/net/socket.c @@ -2261,31 +2261,22 @@ static bool sock_use_custom_sol_socket(const struct socket *sock) return test_bit(SOCK_CUSTOM_SOCKOPT, &sock->flags); } -/* - * Set a socket option. Because we don't know the option lengths we have - * to pass the user mode parameter for the protocols to sort out. - */ -int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval, - int optlen) +int do_sock_setsockopt(struct socket *sock, bool compat, int level, + int optname, char __user *user_optval, int optlen) { sockptr_t optval = USER_SOCKPTR(user_optval); const struct proto_ops *ops; char *kernel_optval = NULL; - int err, fput_needed; - struct socket *sock; + int err; if (optlen < 0) return -EINVAL; - sock = sockfd_lookup_light(fd, &err, &fput_needed); - if (!sock) - return err; - err = security_socket_setsockopt(sock, level, optname); if (err) goto out_put; - if (!in_compat_syscall()) + if (!compat) err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname, user_optval, &optlen, &kernel_optval); @@ -2308,6 +2299,27 @@ int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval, optlen); kfree(kernel_optval); out_put: + return err; +} +EXPORT_SYMBOL(do_sock_setsockopt); + +/* Set a socket option. Because we don't know the option lengths we have + * to pass the user mode parameter for the protocols to sort out. + */ +int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval, + int optlen) +{ + bool compat = in_compat_syscall(); + int err, fput_needed; + struct socket *sock; + + sock = sockfd_lookup_light(fd, &err, &fput_needed); + if (!sock) + return err; + + err = do_sock_setsockopt(sock, compat, level, optname, user_optval, + optlen); + fput_light(sock->file, fput_needed); return err; } From patchwork Mon Sep 11 10:34:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 137928 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp17062vqx; Mon, 11 Sep 2023 14:37:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGGb1iuu95e76uVjxWo5B4oVJrq3JTabrpz/K4grqlSq89nAdJ0u7NnMLm9SoUygO2vi6EA X-Received: by 2002:a05:6a20:948f:b0:153:5366:dec1 with SMTP id hs15-20020a056a20948f00b001535366dec1mr10714757pzb.15.1694468276202; Mon, 11 Sep 2023 14:37:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694468276; cv=none; d=google.com; s=arc-20160816; b=DDffGJNzQEyf44OI6j0mF26yTT3Wckcz6THHYtnwA7r0ctDZYAa9t2ttepCKOIOkHm yr15cxZi/waIh8QWGGTMWZBV8BVDIHg7fgkXhtxpQAzdRgVGjUX2UldJlhDF7mw96xDk kdIEErasXXTiee0Wy/pcMlPCjHFL4PgjVeTvtxr3EeVqekXQW+tnbrwJGX6Ngxr/UKwj dCg3lzPOmMzYOFEWkFEGuH0PQPCD+1tNYnsEsDf6ZAZCquXEkdlFTnXHVJ9k+5ISptTm pLJ1DNRLtXwCRqEqeJHzNPJFUt4kxfF9GIwuXJXraoJdOsthhzLTHnPj0A1Hp97LHkOz tw5g== 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; bh=gptGKutv8UrS/zDN0Qmum7MxSe9eEjLIE1n6bMLfanE=; fh=MOKrsmM1ARKzuWu5sqYicQNV1JqmXT2kr5gY0NYg2nQ=; b=PPRfLTqtNFHOvXUgtbmzGhv2ZkpKVU3Ddr9+cJJfEEY7u6jXErdEz8UgccHF86uO8S iopTjnIknqPAjIlGhTKNPcvH5qvmjROI8Krk+gyrCjyUG7rxeVfvkLzHIO+reOlchf21 nbeg9hRSGquZBeJUbzGt66gmk80+q4t9Iy8o6KfFDS2wHkxizOsvZXVE2iTDVoOV/drA OQo+1OZI7bKYa9T/ZJHDa9N9ru0nHOqFixl+iTC1s1VdtDq1atukx5HbPCmuX3GUzcTR Smnd9B+5/Gnkitpc4iwM4bqWRcYSL3jmugdlAqO6z2deTVUipH3CJ0rZJ84xGoapoHEB RLGQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l5-20020a056a00140500b0068fcb9a7349si1843658pfu.349.2023.09.11.14.37.33; Mon, 11 Sep 2023 14:37:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349400AbjIKVd1 (ORCPT + 99 others); Mon, 11 Sep 2023 17:33:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236412AbjIKKef (ORCPT ); Mon, 11 Sep 2023 06:34:35 -0400 Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F9E0E5F; Mon, 11 Sep 2023 03:34:30 -0700 (PDT) Received: by mail-lf1-f44.google.com with SMTP id 2adb3069b0e04-501cef42bc9so6936103e87.0; Mon, 11 Sep 2023 03:34:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694428468; x=1695033268; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gptGKutv8UrS/zDN0Qmum7MxSe9eEjLIE1n6bMLfanE=; b=fjwGZqU0XX1eFlobZPhQwLGEaFslXYsfR6JtFiPdtqpiFqSNZZY+KEIiAh5YtLlAX6 aPGRIpoyf2qtUGko6JOnhRFw0ofVPtyV4M7DUOagMbWTYfc9ATkPDcMC5b+mxG3eJLkF AuJePBlUumkPNrXhVZy9mvrVoYPo7IqufPSErTzu6mSjZAJ70T9b5MCkYZHpOhc6GfUI GK7+X5mzMZv0GWBYpBQWVmzu2XvgN4Fj3yHXVjblddwKQ+TzmvrFqKMy+SjkLOyrK8Yb 0UK/vsEddCL15EcPcaMoG27aoP8PjrPGxD5Wv13hcFyhWMZSdM8B+E/0skHgEeF5gNYk fanA== X-Gm-Message-State: AOJu0YzXg5hMgTwoNHfscE3OIJlqwOxE6jOg4F8DiNV7CRLurgvnz1LW HGTNCtq+XPBLN8Ah7qEUpxU= X-Received: by 2002:a05:6512:3d09:b0:4fb:89b3:3373 with SMTP id d9-20020a0565123d0900b004fb89b33373mr9263203lfv.43.1694428468123; Mon, 11 Sep 2023 03:34:28 -0700 (PDT) Received: from localhost (fwdproxy-cln-118.fbsv.net. [2a03:2880:31ff:76::face:b00c]) by smtp.gmail.com with ESMTPSA id s10-20020aa7d78a000000b00523653295f9sm4399748edq.94.2023.09.11.03.34.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 03:34:27 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, martin.lau@linux.dev, krisman@suse.de Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, io-uring@vger.kernel.org, pabeni@redhat.com Subject: [PATCH v5 3/8] io_uring/cmd: Pass compat mode in issue_flags Date: Mon, 11 Sep 2023 03:34:02 -0700 Message-Id: <20230911103407.1393149-4-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230911103407.1393149-1-leitao@debian.org> References: <20230911103407.1393149-1-leitao@debian.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776778766945706291 X-GMAIL-MSGID: 1776778766945706291 Create a new flag to track if the operation is running compat mode. This basically check the context->compat and pass it to the issue_flags, so, it could be queried later in the callbacks. Signed-off-by: Breno Leitao --- include/linux/io_uring.h | 1 + io_uring/uring_cmd.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/include/linux/io_uring.h b/include/linux/io_uring.h index 106cdc55ff3b..bc53b35966ed 100644 --- a/include/linux/io_uring.h +++ b/include/linux/io_uring.h @@ -20,6 +20,7 @@ enum io_uring_cmd_flags { IO_URING_F_SQE128 = (1 << 8), IO_URING_F_CQE32 = (1 << 9), IO_URING_F_IOPOLL = (1 << 10), + IO_URING_F_COMPAT = (1 << 11), }; struct io_uring_cmd { diff --git a/io_uring/uring_cmd.c b/io_uring/uring_cmd.c index 537795fddc87..60f843a357e0 100644 --- a/io_uring/uring_cmd.c +++ b/io_uring/uring_cmd.c @@ -128,6 +128,8 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags) issue_flags |= IO_URING_F_SQE128; if (ctx->flags & IORING_SETUP_CQE32) issue_flags |= IO_URING_F_CQE32; + if (ctx->compat) + issue_flags |= IO_URING_F_COMPAT; if (ctx->flags & IORING_SETUP_IOPOLL) { if (!file->f_op->uring_cmd_iopoll) return -EOPNOTSUPP; From patchwork Mon Sep 11 10:34:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 137919 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bf16:0:b0:3f4:cb6f:45e2 with SMTP id bk22csp14963vqb; Mon, 11 Sep 2023 13:56:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGNK8pNw9W2CFjtxMyOVCoQTr4s9cAFh1BNt5Mw1QAI46QvoyNdodoj2Pepl87gmE97OedU X-Received: by 2002:a17:902:e549:b0:1b8:6987:de84 with SMTP id n9-20020a170902e54900b001b86987de84mr13152155plf.48.1694465771936; Mon, 11 Sep 2023 13:56:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694465771; cv=none; d=google.com; s=arc-20160816; b=Db7Bv6Y5NALAKvEi99wgsHyQOUDJPI/i3Zpqzv6tqkWQl5d/cmPF73NQmlxFOw5ZwH y9M1620MQRYjxPdqsR1gZq7htjaADYyQMIOBPMqvHcyLlffA6mgFPVjKS/ZG+cRD+4ei qGo11X8hVIpm/C1+kWiXvXkpL8O7GheA2KmJhkhTs3obIJCSvOJ/yx2wqA5VpmBOuVWI QIDOLP/Oz0gQB3uo08LtseyMxAkhscquJDofsLBURPty3N015kxFlV1RnQ8xtk8b0pXQ i17/JwdYEM1+9XuZr1etCJp6rTe4IDe4w3dS1yrP6y5rdAv3H7zqvpXvO0pX/eE6sAdk wtYQ== 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; bh=B9zL1pmqINyZLXRlcnhMXvmJgzBzwPRNb5BCtJUaRIM=; fh=7WHcCM4z+uBxiDMz1Gu4QteA1ive6dLlrDcpYzmRJ4Y=; b=v7Vv+6VE5ypsPpdgpFnX4ThpElzDxfShSGt8RmlDHyK13jTevjVVR7+8gnsDXbY0Y2 cMD/Dui30NkMvHTp2WjmHr6J51o26nsNozrITLWQEFAdclId5J3zX+8RLgJ3GGqknd3N LTpGysaxFkCgxk2lZfT4umjFiAmkWcOiKqENZVrkvn7pRmSyVuND8tRK7trSfftcH6pk pEI0lmgXlMDX9eIPwKccZ3/gdP2Kez++OB6+DIcvhZmCejalJ8UyVLvgMReWIdSxkkA3 CFqhheWMqyCipaS32m5zYo1RZ8gKf4h1k4stKauIA944jV1M53kxFYsGp0XPYIcQrl05 c9eQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w8-20020a170902904800b001b84c7d4716si6792077plz.32.2023.09.11.13.55.51; Mon, 11 Sep 2023 13:56:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239252AbjIKUys (ORCPT + 99 others); Mon, 11 Sep 2023 16:54:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236413AbjIKKep (ORCPT ); Mon, 11 Sep 2023 06:34:45 -0400 Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A0A3120; Mon, 11 Sep 2023 03:34:39 -0700 (PDT) Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-9a58dbd5daeso560708866b.2; Mon, 11 Sep 2023 03:34:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694428478; x=1695033278; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=B9zL1pmqINyZLXRlcnhMXvmJgzBzwPRNb5BCtJUaRIM=; b=djWXp/nyhoDwGTfRRp1VDIXXd+Y1zFdtOPaXbZAd1q6AubDA5aZ5ByiChwvSnzHtPz e5W2t0jDWC3PYJNoVQi/0roxNOjQ8AsCvyYPwrpUxAH9S8LJZVESl9h4l/4O/2dIZG7J 8A0gdG0xG/eRS86Ol1+GMqVkJ6ZsDu2BPtoCsMI5AvLac64g79Sj2/WREm3EpK/Xq2OI OItJ185Tq/UmmnhI2p1aMrUicy8i3oVf1pRRWNhEMlANRqQpgfuNn+gjaIbzjjXSO8VC Ko1lcXlU+lJRPxlq+aQv73Ob//UYN40/BZPx0mGmimzdcVxU/MjBCIMgIri2/n9Dvgm8 fLOw== X-Gm-Message-State: AOJu0Yxhu4s3Lb8+v/mpCzq7hvwO3L88ka2P7gPIJOiA+evrVbvud0Jv WRcyoxv/P7rl0cq/6wOM3b0= X-Received: by 2002:a17:907:7795:b0:9a1:db97:62a1 with SMTP id ky21-20020a170907779500b009a1db9762a1mr7028205ejc.46.1694428477772; Mon, 11 Sep 2023 03:34:37 -0700 (PDT) Received: from localhost (fwdproxy-cln-004.fbsv.net. [2a03:2880:31ff:4::face:b00c]) by smtp.gmail.com with ESMTPSA id me15-20020a170906aecf00b009a193a5acffsm5073750ejb.121.2023.09.11.03.34.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 03:34:37 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, martin.lau@linux.dev, krisman@suse.de, "David S. Miller" , Eric Dumazet , Paolo Abeni , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, io-uring@vger.kernel.org, linux-kselftest@vger.kernel.org (open list:KERNEL SELFTEST FRAMEWORK) Subject: [PATCH v5 4/8] selftests/net: Extract uring helpers to be reusable Date: Mon, 11 Sep 2023 03:34:03 -0700 Message-Id: <20230911103407.1393149-5-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230911103407.1393149-1-leitao@debian.org> References: <20230911103407.1393149-1-leitao@debian.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776776141347643290 X-GMAIL-MSGID: 1776776141347643290 Instead of defining basic io_uring functions in the test case, move them to a common directory, so, other tests can use them. This simplify the test code and reuse the common "mini" liburing infrastructure. This is basically a copy of what we have in io_uring_zerocopy_tx with some minor improvements. A follow-up test will use the same helpers in a BPF sockopt test. Signed-off-by: Breno Leitao --- tools/include/io_uring/mini_liburing.h | 292 ++++++++++++++++++ tools/testing/selftests/net/Makefile | 1 + .../selftests/net/io_uring_zerocopy_tx.c | 268 +--------------- 3 files changed, 295 insertions(+), 266 deletions(-) create mode 100644 tools/include/io_uring/mini_liburing.h diff --git a/tools/include/io_uring/mini_liburing.h b/tools/include/io_uring/mini_liburing.h new file mode 100644 index 000000000000..e0e1e76def25 --- /dev/null +++ b/tools/include/io_uring/mini_liburing.h @@ -0,0 +1,292 @@ +/* SPDX-License-Identifier: MIT */ + +#include +#include +#include +#include +#include +#include + +struct io_sq_ring { + unsigned int *head; + unsigned int *tail; + unsigned int *ring_mask; + unsigned int *ring_entries; + unsigned int *flags; + unsigned int *array; +}; + +struct io_cq_ring { + unsigned int *head; + unsigned int *tail; + unsigned int *ring_mask; + unsigned int *ring_entries; + struct io_uring_cqe *cqes; +}; + +struct io_uring_sq { + unsigned int *khead; + unsigned int *ktail; + unsigned int *kring_mask; + unsigned int *kring_entries; + unsigned int *kflags; + unsigned int *kdropped; + unsigned int *array; + struct io_uring_sqe *sqes; + + unsigned int sqe_head; + unsigned int sqe_tail; + + size_t ring_sz; +}; + +struct io_uring_cq { + unsigned int *khead; + unsigned int *ktail; + unsigned int *kring_mask; + unsigned int *kring_entries; + unsigned int *koverflow; + struct io_uring_cqe *cqes; + + size_t ring_sz; +}; + +struct io_uring { + struct io_uring_sq sq; + struct io_uring_cq cq; + int ring_fd; +}; + +#if defined(__x86_64) || defined(__i386__) +#define read_barrier() __asm__ __volatile__("":::"memory") +#define write_barrier() __asm__ __volatile__("":::"memory") +#else +#define read_barrier() __sync_synchronize() +#define write_barrier() __sync_synchronize() +#endif + +static inline int io_uring_mmap(int fd, struct io_uring_params *p, + struct io_uring_sq *sq, struct io_uring_cq *cq) +{ + size_t size; + void *ptr; + int ret; + + sq->ring_sz = p->sq_off.array + p->sq_entries * sizeof(unsigned int); + ptr = mmap(0, sq->ring_sz, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING); + if (ptr == MAP_FAILED) + return -errno; + sq->khead = ptr + p->sq_off.head; + sq->ktail = ptr + p->sq_off.tail; + sq->kring_mask = ptr + p->sq_off.ring_mask; + sq->kring_entries = ptr + p->sq_off.ring_entries; + sq->kflags = ptr + p->sq_off.flags; + sq->kdropped = ptr + p->sq_off.dropped; + sq->array = ptr + p->sq_off.array; + + size = p->sq_entries * sizeof(struct io_uring_sqe); + sq->sqes = mmap(0, size, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES); + if (sq->sqes == MAP_FAILED) { + ret = -errno; +err: + munmap(sq->khead, sq->ring_sz); + return ret; + } + + cq->ring_sz = p->cq_off.cqes + p->cq_entries * sizeof(struct io_uring_cqe); + ptr = mmap(0, cq->ring_sz, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING); + if (ptr == MAP_FAILED) { + ret = -errno; + munmap(sq->sqes, p->sq_entries * sizeof(struct io_uring_sqe)); + goto err; + } + cq->khead = ptr + p->cq_off.head; + cq->ktail = ptr + p->cq_off.tail; + cq->kring_mask = ptr + p->cq_off.ring_mask; + cq->kring_entries = ptr + p->cq_off.ring_entries; + cq->koverflow = ptr + p->cq_off.overflow; + cq->cqes = ptr + p->cq_off.cqes; + return 0; +} + +static inline int io_uring_setup(unsigned int entries, + struct io_uring_params *p) +{ + return syscall(__NR_io_uring_setup, entries, p); +} + +static inline int io_uring_enter(int fd, unsigned int to_submit, + unsigned int min_complete, + unsigned int flags, sigset_t *sig) +{ + return syscall(__NR_io_uring_enter, fd, to_submit, min_complete, + flags, sig, _NSIG / 8); +} + +static inline int io_uring_queue_init(unsigned int entries, + struct io_uring *ring, + unsigned int flags) +{ + struct io_uring_params p; + int fd, ret; + + memset(ring, 0, sizeof(*ring)); + memset(&p, 0, sizeof(p)); + p.flags = flags; + + fd = io_uring_setup(entries, &p); + if (fd < 0) + return fd; + ret = io_uring_mmap(fd, &p, &ring->sq, &ring->cq); + if (!ret) + ring->ring_fd = fd; + else + close(fd); + return ret; +} + +/* Get a sqe */ +static inline struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring) +{ + struct io_uring_sq *sq = &ring->sq; + + if (sq->sqe_tail + 1 - sq->sqe_head > *sq->kring_entries) + return NULL; + return &sq->sqes[sq->sqe_tail++ & *sq->kring_mask]; +} + +static inline int io_uring_wait_cqe(struct io_uring *ring, + struct io_uring_cqe **cqe_ptr) +{ + struct io_uring_cq *cq = &ring->cq; + const unsigned int mask = *cq->kring_mask; + unsigned int head = *cq->khead; + int ret; + + *cqe_ptr = NULL; + do { + read_barrier(); + if (head != *cq->ktail) { + *cqe_ptr = &cq->cqes[head & mask]; + break; + } + ret = io_uring_enter(ring->ring_fd, 0, 1, + IORING_ENTER_GETEVENTS, NULL); + if (ret < 0) + return -errno; + } while (1); + + return 0; +} + +static inline int io_uring_submit(struct io_uring *ring) +{ + struct io_uring_sq *sq = &ring->sq; + const unsigned int mask = *sq->kring_mask; + unsigned int ktail, submitted, to_submit; + int ret; + + read_barrier(); + if (*sq->khead != *sq->ktail) { + submitted = *sq->kring_entries; + goto submit; + } + if (sq->sqe_head == sq->sqe_tail) + return 0; + + ktail = *sq->ktail; + to_submit = sq->sqe_tail - sq->sqe_head; + for (submitted = 0; submitted < to_submit; submitted++) { + read_barrier(); + sq->array[ktail++ & mask] = sq->sqe_head++ & mask; + } + if (!submitted) + return 0; + + if (*sq->ktail != ktail) { + write_barrier(); + *sq->ktail = ktail; + write_barrier(); + } +submit: + ret = io_uring_enter(ring->ring_fd, submitted, 0, + IORING_ENTER_GETEVENTS, NULL); + return ret < 0 ? -errno : ret; +} + +static inline void io_uring_queue_exit(struct io_uring *ring) +{ + struct io_uring_sq *sq = &ring->sq; + + munmap(sq->sqes, *sq->kring_entries * sizeof(struct io_uring_sqe)); + munmap(sq->khead, sq->ring_sz); + close(ring->ring_fd); +} + +/* Prepare and send the SQE */ +static inline void io_uring_prep_cmd(struct io_uring_sqe *sqe, int op, + int sockfd, + int level, int optname, + const void *optval, + const socklen_t optlen) +{ + memset(sqe, 0, sizeof(*sqe)); + sqe->opcode = (__u8)IORING_OP_URING_CMD; + sqe->fd = sockfd; + sqe->cmd_op = op; + + sqe->level = level; + sqe->optname = optname; + sqe->optval = (unsigned long long)optval; + sqe->optlen = (unsigned long long)optlen; +} + +static inline void io_uring_prep_cmd_get(struct io_uring_sqe *sqe, int op, + int sockfd, + int level, int optname, + const void *optval, + const socklen_t *optlen) +{ + io_uring_prep_cmd(sqe, op, sockfd, level, optname, optval, 0); + sqe->optlen = (unsigned long long)optlen; +} + +static inline int io_uring_register_buffers(struct io_uring *ring, + const struct iovec *iovecs, + unsigned int nr_iovecs) +{ + int ret; + + ret = syscall(__NR_io_uring_register, ring->ring_fd, + IORING_REGISTER_BUFFERS, iovecs, nr_iovecs); + return (ret < 0) ? -errno : ret; +} + +static inline void io_uring_prep_send(struct io_uring_sqe *sqe, int sockfd, + const void *buf, size_t len, int flags) +{ + memset(sqe, 0, sizeof(*sqe)); + sqe->opcode = (__u8)IORING_OP_SEND; + sqe->fd = sockfd; + sqe->addr = (unsigned long)buf; + sqe->len = len; + sqe->msg_flags = (__u32)flags; +} + +static inline void io_uring_prep_sendzc(struct io_uring_sqe *sqe, int sockfd, + const void *buf, size_t len, int flags, + unsigned int zc_flags) +{ + io_uring_prep_send(sqe, sockfd, buf, len, flags); + sqe->opcode = (__u8)IORING_OP_SEND_ZC; + sqe->ioprio = zc_flags; +} + +static inline void io_uring_cqe_seen(struct io_uring *ring) +{ + *(&ring->cq)->khead += 1; + write_barrier(); +} diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index 8b017070960d..f8d99837b9dc 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -98,6 +98,7 @@ $(OUTPUT)/reuseport_bpf_numa: LDLIBS += -lnuma $(OUTPUT)/tcp_mmap: LDLIBS += -lpthread -lcrypto $(OUTPUT)/tcp_inq: LDLIBS += -lpthread $(OUTPUT)/bind_bhash: LDLIBS += -lpthread +$(OUTPUT)/io_uring_zerocopy_tx: CFLAGS += -I../../../include/ # Rules to generate bpf obj nat6to4.o CLANG ?= clang diff --git a/tools/testing/selftests/net/io_uring_zerocopy_tx.c b/tools/testing/selftests/net/io_uring_zerocopy_tx.c index 154287740172..76e604e4810e 100644 --- a/tools/testing/selftests/net/io_uring_zerocopy_tx.c +++ b/tools/testing/selftests/net/io_uring_zerocopy_tx.c @@ -36,6 +36,8 @@ #include #include +#include + #define NOTIF_TAG 0xfffffffULL #define NONZC_TAG 0 #define ZC_TAG 1 @@ -60,272 +62,6 @@ static struct sockaddr_storage cfg_dst_addr; static char payload[IP_MAXPACKET] __attribute__((aligned(4096))); -struct io_sq_ring { - unsigned *head; - unsigned *tail; - unsigned *ring_mask; - unsigned *ring_entries; - unsigned *flags; - unsigned *array; -}; - -struct io_cq_ring { - unsigned *head; - unsigned *tail; - unsigned *ring_mask; - unsigned *ring_entries; - struct io_uring_cqe *cqes; -}; - -struct io_uring_sq { - unsigned *khead; - unsigned *ktail; - unsigned *kring_mask; - unsigned *kring_entries; - unsigned *kflags; - unsigned *kdropped; - unsigned *array; - struct io_uring_sqe *sqes; - - unsigned sqe_head; - unsigned sqe_tail; - - size_t ring_sz; -}; - -struct io_uring_cq { - unsigned *khead; - unsigned *ktail; - unsigned *kring_mask; - unsigned *kring_entries; - unsigned *koverflow; - struct io_uring_cqe *cqes; - - size_t ring_sz; -}; - -struct io_uring { - struct io_uring_sq sq; - struct io_uring_cq cq; - int ring_fd; -}; - -#ifdef __alpha__ -# ifndef __NR_io_uring_setup -# define __NR_io_uring_setup 535 -# endif -# ifndef __NR_io_uring_enter -# define __NR_io_uring_enter 536 -# endif -# ifndef __NR_io_uring_register -# define __NR_io_uring_register 537 -# endif -#else /* !__alpha__ */ -# ifndef __NR_io_uring_setup -# define __NR_io_uring_setup 425 -# endif -# ifndef __NR_io_uring_enter -# define __NR_io_uring_enter 426 -# endif -# ifndef __NR_io_uring_register -# define __NR_io_uring_register 427 -# endif -#endif - -#if defined(__x86_64) || defined(__i386__) -#define read_barrier() __asm__ __volatile__("":::"memory") -#define write_barrier() __asm__ __volatile__("":::"memory") -#else - -#define read_barrier() __sync_synchronize() -#define write_barrier() __sync_synchronize() -#endif - -static int io_uring_setup(unsigned int entries, struct io_uring_params *p) -{ - return syscall(__NR_io_uring_setup, entries, p); -} - -static int io_uring_enter(int fd, unsigned int to_submit, - unsigned int min_complete, - unsigned int flags, sigset_t *sig) -{ - return syscall(__NR_io_uring_enter, fd, to_submit, min_complete, - flags, sig, _NSIG / 8); -} - -static int io_uring_register_buffers(struct io_uring *ring, - const struct iovec *iovecs, - unsigned nr_iovecs) -{ - int ret; - - ret = syscall(__NR_io_uring_register, ring->ring_fd, - IORING_REGISTER_BUFFERS, iovecs, nr_iovecs); - return (ret < 0) ? -errno : ret; -} - -static int io_uring_mmap(int fd, struct io_uring_params *p, - struct io_uring_sq *sq, struct io_uring_cq *cq) -{ - size_t size; - void *ptr; - int ret; - - sq->ring_sz = p->sq_off.array + p->sq_entries * sizeof(unsigned); - ptr = mmap(0, sq->ring_sz, PROT_READ | PROT_WRITE, - MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING); - if (ptr == MAP_FAILED) - return -errno; - sq->khead = ptr + p->sq_off.head; - sq->ktail = ptr + p->sq_off.tail; - sq->kring_mask = ptr + p->sq_off.ring_mask; - sq->kring_entries = ptr + p->sq_off.ring_entries; - sq->kflags = ptr + p->sq_off.flags; - sq->kdropped = ptr + p->sq_off.dropped; - sq->array = ptr + p->sq_off.array; - - size = p->sq_entries * sizeof(struct io_uring_sqe); - sq->sqes = mmap(0, size, PROT_READ | PROT_WRITE, - MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES); - if (sq->sqes == MAP_FAILED) { - ret = -errno; -err: - munmap(sq->khead, sq->ring_sz); - return ret; - } - - cq->ring_sz = p->cq_off.cqes + p->cq_entries * sizeof(struct io_uring_cqe); - ptr = mmap(0, cq->ring_sz, PROT_READ | PROT_WRITE, - MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING); - if (ptr == MAP_FAILED) { - ret = -errno; - munmap(sq->sqes, p->sq_entries * sizeof(struct io_uring_sqe)); - goto err; - } - cq->khead = ptr + p->cq_off.head; - cq->ktail = ptr + p->cq_off.tail; - cq->kring_mask = ptr + p->cq_off.ring_mask; - cq->kring_entries = ptr + p->cq_off.ring_entries; - cq->koverflow = ptr + p->cq_off.overflow; - cq->cqes = ptr + p->cq_off.cqes; - return 0; -} - -static int io_uring_queue_init(unsigned entries, struct io_uring *ring, - unsigned flags) -{ - struct io_uring_params p; - int fd, ret; - - memset(ring, 0, sizeof(*ring)); - memset(&p, 0, sizeof(p)); - p.flags = flags; - - fd = io_uring_setup(entries, &p); - if (fd < 0) - return fd; - ret = io_uring_mmap(fd, &p, &ring->sq, &ring->cq); - if (!ret) - ring->ring_fd = fd; - else - close(fd); - return ret; -} - -static int io_uring_submit(struct io_uring *ring) -{ - struct io_uring_sq *sq = &ring->sq; - const unsigned mask = *sq->kring_mask; - unsigned ktail, submitted, to_submit; - int ret; - - read_barrier(); - if (*sq->khead != *sq->ktail) { - submitted = *sq->kring_entries; - goto submit; - } - if (sq->sqe_head == sq->sqe_tail) - return 0; - - ktail = *sq->ktail; - to_submit = sq->sqe_tail - sq->sqe_head; - for (submitted = 0; submitted < to_submit; submitted++) { - read_barrier(); - sq->array[ktail++ & mask] = sq->sqe_head++ & mask; - } - if (!submitted) - return 0; - - if (*sq->ktail != ktail) { - write_barrier(); - *sq->ktail = ktail; - write_barrier(); - } -submit: - ret = io_uring_enter(ring->ring_fd, submitted, 0, - IORING_ENTER_GETEVENTS, NULL); - return ret < 0 ? -errno : ret; -} - -static inline void io_uring_prep_send(struct io_uring_sqe *sqe, int sockfd, - const void *buf, size_t len, int flags) -{ - memset(sqe, 0, sizeof(*sqe)); - sqe->opcode = (__u8) IORING_OP_SEND; - sqe->fd = sockfd; - sqe->addr = (unsigned long) buf; - sqe->len = len; - sqe->msg_flags = (__u32) flags; -} - -static inline void io_uring_prep_sendzc(struct io_uring_sqe *sqe, int sockfd, - const void *buf, size_t len, int flags, - unsigned zc_flags) -{ - io_uring_prep_send(sqe, sockfd, buf, len, flags); - sqe->opcode = (__u8) IORING_OP_SEND_ZC; - sqe->ioprio = zc_flags; -} - -static struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring) -{ - struct io_uring_sq *sq = &ring->sq; - - if (sq->sqe_tail + 1 - sq->sqe_head > *sq->kring_entries) - return NULL; - return &sq->sqes[sq->sqe_tail++ & *sq->kring_mask]; -} - -static int io_uring_wait_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr) -{ - struct io_uring_cq *cq = &ring->cq; - const unsigned mask = *cq->kring_mask; - unsigned head = *cq->khead; - int ret; - - *cqe_ptr = NULL; - do { - read_barrier(); - if (head != *cq->ktail) { - *cqe_ptr = &cq->cqes[head & mask]; - break; - } - ret = io_uring_enter(ring->ring_fd, 0, 1, - IORING_ENTER_GETEVENTS, NULL); - if (ret < 0) - return -errno; - } while (1); - - return 0; -} - -static inline void io_uring_cqe_seen(struct io_uring *ring) -{ - *(&ring->cq)->khead += 1; - write_barrier(); -} - static unsigned long gettimeofday_ms(void) { struct timeval tv; From patchwork Mon Sep 11 10:34:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 137922 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bf16:0:b0:3f4:cb6f:45e2 with SMTP id bk22csp15552vqb; Mon, 11 Sep 2023 13:57:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGhs8g3n3ORCKN7p6ofiwd4R1jZEQ1laOETn4v0pcut93VF5nUwdvyae2Y8VhVbIf2+uYoe X-Received: by 2002:a17:902:9a42:b0:1c3:6e38:3943 with SMTP id x2-20020a1709029a4200b001c36e383943mr8235756plv.56.1694465853184; Mon, 11 Sep 2023 13:57:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694465853; cv=none; d=google.com; s=arc-20160816; b=aFi/sEg57ZWcHKRyN6RaNBXxtQokltDawfN1p68sJc8gFIyaYNwovl8cHwGqzkZXOm DuPekjsLG81+Fo6hU0uFrGwb8wVRgPQbZnkRJFXyFp4qybPASV8EfgUne3uLcHDsRbnT 4bNlSy8uJgBedH0rwot4woe7/0/tlroEccylBm5sKJM2T2sz3Kl80GCJD+Ey3vM76YxW Oq/k+DxNmRxPVmIyIb417Ftxiz26JtoVRhdJDaWiq2WV1wWsEdN98UueZ0KXVXTQsgx3 ju8+HDwg01qYoHOU2a2kiHvzePRvGxmFXlUigxhdtCagNQaiDMbpEvkNjNtq1GUU22RA dHvw== 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; bh=CCQEu/ILqoq7skisc09M8SCm5EUq/HC3ZQfY+FZ+QRY=; fh=MOKrsmM1ARKzuWu5sqYicQNV1JqmXT2kr5gY0NYg2nQ=; b=HRvAU9k8mGjAxccuN9Tn1iRjD43qWObXxHdku5xIpxYg4uISBKHUy9ofDnvtY9Rt8m Hx/watj5OQn7kEJfstiXj9m5jdvyNuZs2/vTOt/hRZ3Zyjkbt+5QjExHPiwB66qjSiht AgutAJt5xGGXClTQjNzmbPNVu0QtRWzhUjoxGwe1CekYE/UaY7qzSIi1aarozzSbemIx j6akS28AVpjFMM9B0DZWR3Bh/Zrc5tu+Yg/4ukBBNC+0AMLd7m3AND7TtIBLNTe1eWLT F2BVf1lwWWeN3VQnvtr0I1/clWkrLTK8dttTMszTpN8J6m/dDGMuVabrYO9SJqz/LEfQ vuJw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d18-20020a170902e15200b001bbb39c68b2si6771540pla.178.2023.09.11.13.56.22; Mon, 11 Sep 2023 13:57:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238715AbjIKUzU (ORCPT + 99 others); Mon, 11 Sep 2023 16:55:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236414AbjIKKep (ORCPT ); Mon, 11 Sep 2023 06:34:45 -0400 Received: from mail-lj1-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26C7EE5F; Mon, 11 Sep 2023 03:34:41 -0700 (PDT) Received: by mail-lj1-f179.google.com with SMTP id 38308e7fff4ca-2bb9a063f26so72589331fa.2; Mon, 11 Sep 2023 03:34:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694428479; x=1695033279; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CCQEu/ILqoq7skisc09M8SCm5EUq/HC3ZQfY+FZ+QRY=; b=RIZHRvPIMnqDZyF2bfa2HpIYt0H0q6LPJd8I+nY4iE9CSsGeMSxO159HycFDPerDn8 3Nna0KTL1tNnxfJMekW77jMDey9z4p2Zvy2864q2e/1yFqm2wKwb1eHVwxqy0KFZomlZ XqAqGrgdfA6MOeo2zUeiXeXDOmDGeofHsH6geQ+awE1Hbfi10b7tggi0vXQ6ZkjE2b8O qxGxjOdcwTlrzh+eat4zl1uCC90+mSsmEKLx2gyN5DFa1llrj6ySogjo36C2XJjXgyBQ 1wKTz0qI7WmHQ40QMooa61FTyvBRVYo7JdtsqGBWpAxO6P0FM2lMKPgI3+7EaqRusXQF tz3A== X-Gm-Message-State: AOJu0Yxrii0gFRZxsDQu/Jy2rkM60zBroGy5Azk+Ij/+ZMxBELyLNiDJ bQ36Q8i9pXctD21QJuG84Mo= X-Received: by 2002:a2e:3609:0:b0:2bc:d993:bdaa with SMTP id d9-20020a2e3609000000b002bcd993bdaamr8173814lja.18.1694428479114; Mon, 11 Sep 2023 03:34:39 -0700 (PDT) Received: from localhost (fwdproxy-cln-011.fbsv.net. [2a03:2880:31ff:b::face:b00c]) by smtp.gmail.com with ESMTPSA id o6-20020a17090611c600b0099cf9bf4c98sm5192137eja.8.2023.09.11.03.34.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 03:34:38 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, martin.lau@linux.dev, krisman@suse.de Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, io-uring@vger.kernel.org, pabeni@redhat.com Subject: [PATCH v5 5/8] io_uring/cmd: return -EOPNOTSUPP if net is disabled Date: Mon, 11 Sep 2023 03:34:04 -0700 Message-Id: <20230911103407.1393149-6-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230911103407.1393149-1-leitao@debian.org> References: <20230911103407.1393149-1-leitao@debian.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776776226686029367 X-GMAIL-MSGID: 1776776226686029367 Protect io_uring_cmd_sock() to be called if CONFIG_NET is not set. If network is not enabled, but io_uring is, then we want to return -EOPNOTSUPP for any possible socket operation. This is helpful because io_uring_cmd_sock() can now call functions that only exits if CONFIG_NET is enabled without having #ifdef CONFIG_NET inside the function itself. Signed-off-by: Breno Leitao --- io_uring/uring_cmd.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/io_uring/uring_cmd.c b/io_uring/uring_cmd.c index 60f843a357e0..a7d6a7d112b7 100644 --- a/io_uring/uring_cmd.c +++ b/io_uring/uring_cmd.c @@ -167,6 +167,7 @@ int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, } EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed); +#if defined(CONFIG_NET) int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags) { struct socket *sock = cmd->file->private_data; @@ -193,3 +194,10 @@ int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags) } } EXPORT_SYMBOL_GPL(io_uring_cmd_sock); +#else +int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags) +{ + return -EOPNOTSUPP; +} +#endif + From patchwork Mon Sep 11 10:34:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 137931 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp17440vqx; Mon, 11 Sep 2023 14:38:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGkmyh2TuZp79FuwnwrLyfzJ7gp2OiViFqoW6gx0K1L5sNJ71H3eyXFika+m5zKBMkaxHvf X-Received: by 2002:a05:6a00:23ca:b0:68a:45a1:c0f2 with SMTP id g10-20020a056a0023ca00b0068a45a1c0f2mr7951521pfc.34.1694468338201; Mon, 11 Sep 2023 14:38:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694468338; cv=none; d=google.com; s=arc-20160816; b=w72BJ2mo2//jjdGF9MZjCHwOOS2rEMKpXJNuLCSooKYhfE5kPv00hDoeN4HKI3ITsK h8itPBUHOtt5qT4RA1sRtsjciLpxkiGeZpogkMs1DG0OZfZ3+kINgTUq9w8vhIQQ/23m W7Nh21RuhH5K560CU0LtakdsxvWPySkqgyJEFQ9XNXYNO7RjKY3ERc/s5ixMRZQtCGP2 9SVahugwUiEEZhRyj7SaqeFavOuJvbK7I2EIynIu6lbM5PYFAz8Z5eO5fpheD/+GT/0v ljlXCRDZ0HxGpRwuvWjBLSDd89V/NwzM2otBauyS1A5rEkwHpQBIetbnA9nrHYDOmDDm iUKw== 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; bh=L/I9YoLGdl+XHs7ScA4HJEHOnhgbL9HGLXsGbg3gSig=; fh=MOKrsmM1ARKzuWu5sqYicQNV1JqmXT2kr5gY0NYg2nQ=; b=yPD0xaCfhMOs5N3KtfDsAksApFdN7iWJbsB+CvUF1dRLTRCSWQWr6cYpfBWAjp0uoE nqDWE4HblXq2qnO0VLFypCV7awAyAwebu6Ygyhrk6Xpm44WYnNMgrsRdg0y/igR5dtDa kfOJ+5J2kBocv00BoK6sHFMaF1lb8sB/UuRgBcDM41EF45rCzDoxeXuPpabJGaY/098s 9WEun9ro+mRpJrRr2Ty0ltHlxsCzweM22p/7DGvSBzhj+btzvYg39m5WV6wQq7yxi0xg +R+CIp1SPbiImRME9FdBGJf8RInqcyuwga2BZjAsvEdJtuHitE8dMD4KRVjfEAmky5da Aq3A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ef6-20020a056a002c8600b00656f1d69ec6si6801292pfb.292.2023.09.11.14.38.42; Mon, 11 Sep 2023 14:38:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349829AbjIKVfC (ORCPT + 99 others); Mon, 11 Sep 2023 17:35:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236420AbjIKKes (ORCPT ); Mon, 11 Sep 2023 06:34:48 -0400 Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 16EDE120; Mon, 11 Sep 2023 03:34:44 -0700 (PDT) Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-99bed101b70so535449366b.3; Mon, 11 Sep 2023 03:34:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694428482; x=1695033282; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=L/I9YoLGdl+XHs7ScA4HJEHOnhgbL9HGLXsGbg3gSig=; b=n/jB+W5t3QhD4NRePzymMX3aCeY1Vglvrg1A/QWIliOLblPqR8eZcK8eqrGZbqdGa/ cH8+UgKemF9nzE8uS4cedCOXorsWTx7S5YCwWyfXd9SraBIQ3PlclC9dQ6qwlOMQUQKF gOnQpfDB1yr63KOGloKw3cLsQLDid3Wsh+/sA1d0FE4RjTBBUUIh1x3EbPlSeoj5KET5 hpmnrwWRBtZXK9qazoculzUyUEvu2HNAYjXzYm3wF93/JPAiJXdnxPl/rHC2WHLBYOPB IbZ/TjDWdnkX6/B0DRXN5tdM84Ov43Qh35itora8o3Tk0kvIeUdQLyPLkBQHtbhRsLN1 SY/g== X-Gm-Message-State: AOJu0Yxi1lvEvGwl4/5r30AkKp+fllFhkFQc+Bs/bSp7P+QZXl94WEnt ogoXJgdKovXdJGfCIdiXcNE= X-Received: by 2002:a17:906:535d:b0:9a3:c4f4:12de with SMTP id j29-20020a170906535d00b009a3c4f412demr5746001ejo.37.1694428482195; Mon, 11 Sep 2023 03:34:42 -0700 (PDT) Received: from localhost (fwdproxy-cln-010.fbsv.net. [2a03:2880:31ff:a::face:b00c]) by smtp.gmail.com with ESMTPSA id rl9-20020a170907216900b0099cbe71f3b5sm5071048ejb.0.2023.09.11.03.34.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 03:34:41 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, martin.lau@linux.dev, krisman@suse.de Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, io-uring@vger.kernel.org, pabeni@redhat.com Subject: [PATCH v5 7/8] io_uring/cmd: Introduce SOCKET_URING_OP_SETSOCKOPT Date: Mon, 11 Sep 2023 03:34:06 -0700 Message-Id: <20230911103407.1393149-8-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230911103407.1393149-1-leitao@debian.org> References: <20230911103407.1393149-1-leitao@debian.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776778832646763580 X-GMAIL-MSGID: 1776778832646763580 Add support for SOCKET_URING_OP_SETSOCKOPT. This new command is similar to setsockopt(2). This implementation leverages the function do_sock_setsockopt(), which is shared with the setsockopt() system call path. Important to say that userspace needs to keep the pointer's memory alive until the operation is completed. I.e, the memory could not be deallocated before the CQE is returned to userspace. Reviewed-by: Gabriel Krisman Bertazi Signed-off-by: Breno Leitao --- include/uapi/linux/io_uring.h | 1 + io_uring/uring_cmd.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index 1c789ee6462d..99cdb2c1e240 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -743,6 +743,7 @@ enum { SOCKET_URING_OP_SIOCINQ = 0, SOCKET_URING_OP_SIOCOUTQ, SOCKET_URING_OP_GETSOCKOPT, + SOCKET_URING_OP_SETSOCKOPT, }; #ifdef __cplusplus diff --git a/io_uring/uring_cmd.c b/io_uring/uring_cmd.c index 2806330a021f..7592af629b18 100644 --- a/io_uring/uring_cmd.c +++ b/io_uring/uring_cmd.c @@ -180,6 +180,20 @@ static inline int io_uring_cmd_getsockopt(struct socket *sock, return do_sock_getsockopt(sock, compat, level, optname, optval, optlen); } +static inline int io_uring_cmd_setsockopt(struct socket *sock, + struct io_uring_cmd *cmd, + unsigned int issue_flags) +{ + void __user *optval = u64_to_user_ptr(READ_ONCE(cmd->sqe->optval)); + int optlen = READ_ONCE(cmd->sqe->optlen); + bool compat = !!(issue_flags & IO_URING_F_COMPAT); + int optname = READ_ONCE(cmd->sqe->optname); + int level = READ_ONCE(cmd->sqe->level); + + return do_sock_setsockopt(sock, compat, level, optname, optval, + optlen); +} + #if defined(CONFIG_NET) int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags) { @@ -204,6 +218,8 @@ int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags) return arg; case SOCKET_URING_OP_GETSOCKOPT: return io_uring_cmd_getsockopt(sock, cmd, issue_flags); + case SOCKET_URING_OP_SETSOCKOPT: + return io_uring_cmd_setsockopt(sock, cmd, issue_flags); default: return -EOPNOTSUPP; } From patchwork Mon Sep 11 10:34:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 137938 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp53855vqx; Mon, 11 Sep 2023 16:04:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEDNJlDBuXfby+ekRtWRAVO8qT0LATfd0f6Mh5/oJ/SJ7ojCI3YcrwP5+y8bfkI9Kwgogcp X-Received: by 2002:a05:6870:911f:b0:1bf:dc25:a5aa with SMTP id o31-20020a056870911f00b001bfdc25a5aamr13406241oae.39.1694473490195; Mon, 11 Sep 2023 16:04:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694473490; cv=none; d=google.com; s=arc-20160816; b=V+P/ZRYXRch2ud+8zBJ97uDNaKvasI87kShq+LJh+ut8S/li08g88qWdIBm3M57aax TvWXTnzywTjk6vTIhFd7JUn5PnuzZkrSgqbPrDYdlF+KIgH3voQh3CAOhKPaKtK4W+t1 GTKmjhYQ0HJp/u8VvAwNY30vf1euRjB/FbEg+RYE2hjAh9e8Co2FoYMJOlmrWQxPR+qk I7qaoMqRB+qwzy2Yey6w+YwfdBLxbGj4/M+WBio0BIPBf+V0B08AztFawn/syYL/pKWS JHoGrqTXyj47fZEnGJVQA7w3htKhe7FBEtU1jOeOnHn8qL3vhDnLAzRMmOTYqiQksw6Z STig== 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; bh=f1vGDg0uCtdiqjKqDiwFtuGhRJ18xP0o/QuRB7i2CeI=; fh=YRyV99DRruU13A/IQqPbcKAGcpJ0hI++x0gY3qjUj68=; b=a9xZa9jY7EFalJP2/8d2gmdoraudYtDHO8mSlw4CMsY5w25oba2loE5824Y5pjshwy /Pvi0gSKkF337o7U2uDAUNZCRSZtZHc6FpCwd+o00re7WIXzC2LT3FnSjMxTvBU4YE7X pf7I6QmaddXJw4FBxFwMVPVribYCprfS/WkZdHGKEPbwXar7stNNXElnaLGD35+Y+NkI RDVOnPEr39g0Q0MpLIfHsyAnrqnncag05RCIFDTUAYfXLnl2AFd+X9hk61t23YDREHqR GbnY8W8Z3lhkS2qnkv+YbavCcVbUGGjB+th2+CiASbn/D5S7VRbpBR1QseKN4l8DikQX +DRw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m1-20020a656a01000000b0057416a797ebsi7376861pgu.12.2023.09.11.16.04.24; Mon, 11 Sep 2023 16:04:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349100AbjIKVcf (ORCPT + 90 others); Mon, 11 Sep 2023 17:32:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236421AbjIKKev (ORCPT ); Mon, 11 Sep 2023 06:34:51 -0400 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D8FE120; Mon, 11 Sep 2023 03:34:47 -0700 (PDT) Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-9a64619d8fbso538062766b.0; Mon, 11 Sep 2023 03:34:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694428485; x=1695033285; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f1vGDg0uCtdiqjKqDiwFtuGhRJ18xP0o/QuRB7i2CeI=; b=RRs/t1JazOZS4/jgQq2jQjDx4LDbMPqL1zI0BTFaJrcDkRUzIWjG1x6ZdZOEogKVRd jz/pAJuHMynH8MaGc2HKOUf8yP4oGvsSWiqqtmkSHp2zPDYGqqu0cHCwCMtOsHiB/Wyk tTmMNavKDvlvs2QuFUBOfz0Bq9oMmErIXpNZpLM/DP0Dr9EPG8T8JTPU1RRkqcyT6r/n jHk+gaIsQQKUxr5HdIw3a471GbByqqPdwkFBpof0ni0qQ+XsqxEbypx24OalDWySdHuP K3looho0TZRJalPHYkdOgL23cnRwaUlAO7x4bwdya2jsXY6poa7+MfaUQHgFA1o+reau 3u9w== X-Gm-Message-State: AOJu0YxS8OLJQusa3UXHwbDSByLHE7l6FHYcbQ78Hh3SZXDdL/tJy4HV A2D6Cqg8tok1ejrPnbCLzH4= X-Received: by 2002:a17:907:b18:b0:99c:e38d:e484 with SMTP id h24-20020a1709070b1800b0099ce38de484mr6984550ejl.6.1694428485510; Mon, 11 Sep 2023 03:34:45 -0700 (PDT) Received: from localhost (fwdproxy-cln-013.fbsv.net. [2a03:2880:31ff:d::face:b00c]) by smtp.gmail.com with ESMTPSA id v14-20020a17090690ce00b0099c53c44083sm5131989ejw.79.2023.09.11.03.34.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 03:34:45 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, martin.lau@linux.dev, krisman@suse.de, Andrii Nakryiko , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Song Liu , Yonghong Song , John Fastabend , KP Singh , Hao Luo , Jiri Olsa , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, io-uring@vger.kernel.org, pabeni@redhat.com, Wang Yufen , =?utf-8?q?Daniel_M=C3=BCller?= , linux-kselftest@vger.kernel.org (open list:KERNEL SELFTEST FRAMEWORK) Subject: [PATCH v5 8/8] selftests/bpf/sockopt: Add io_uring support Date: Mon, 11 Sep 2023 03:34:07 -0700 Message-Id: <20230911103407.1393149-9-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230911103407.1393149-1-leitao@debian.org> References: <20230911103407.1393149-1-leitao@debian.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776784234468226002 X-GMAIL-MSGID: 1776784234468226002 Expand the BPF sockopt test to use also check for io_uring {g,s}etsockopt commands operations. Create infrastructure to run io_uring tests using the mini_liburing helpers, so, the {g,s}etsockopt operation could either be called from system calls, or, via io_uring. Add a 'use_io_uring' parameter to run_test(), to specify if the test should be run using io_uring if the parameter is set, or via the regular system calls if false. Call *all* tests twice, using the regular io_uring path, and the new io_uring path. Signed-off-by: Breno Leitao --- .../selftests/bpf/prog_tests/sockopt.c | 95 +++++++++++++++++-- 1 file changed, 89 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockopt.c b/tools/testing/selftests/bpf/prog_tests/sockopt.c index 9e6a5e3ed4de..40fb4c315ad9 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockopt.c +++ b/tools/testing/selftests/bpf/prog_tests/sockopt.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include "cgroup_helpers.h" static char bpf_log_buf[4096]; @@ -940,7 +941,85 @@ static int load_prog(const struct bpf_insn *insns, return fd; } -static int run_test(int cgroup_fd, struct sockopt_test *test) +/* Core function that handles io_uring ring initialization, + * sending SQE with sockopt command and waiting for the CQE. + */ +static int uring_sockopt(int op, int fd, int level, int optname, + const void *optval, socklen_t *optlen) +{ + struct io_uring_cqe *cqe; + struct io_uring_sqe *sqe; + struct io_uring ring; + int err; + + err = io_uring_queue_init(1, &ring, 0); + if (!ASSERT_OK(err, "Initialize io_uring ring")) + return err; + + sqe = io_uring_get_sqe(&ring); + if (!ASSERT_NEQ(sqe, NULL, "Get an SQE")) { + err = -1; + goto fail; + } + + if (op == SOCKET_URING_OP_GETSOCKOPT) + io_uring_prep_cmd_get(sqe, op, fd, level, optname, optval, + optlen); + else + io_uring_prep_cmd(sqe, op, fd, level, optname, optval, + *optlen); + + err = io_uring_submit(&ring); + if (!ASSERT_EQ(err, 1, "Submit SQE")) + goto fail; + + err = io_uring_wait_cqe(&ring, &cqe); + if (!ASSERT_OK(err, "Wait for CQE")) + goto fail; + + err = cqe->res; + +fail: + io_uring_queue_exit(&ring); + + return err; +} + +static int uring_setsockopt(int fd, int level, int optname, const void *optval, + socklen_t *optlen) +{ + return uring_sockopt(SOCKET_URING_OP_SETSOCKOPT, fd, level, optname, + optval, optlen); +} + +static int uring_getsockopt(int fd, int level, int optname, void *optval, + socklen_t *optlen) +{ + return uring_sockopt(SOCKET_URING_OP_GETSOCKOPT, fd, level, optname, + optval, optlen); +} + +/* Execute the setsocktopt operation */ +static int call_setsockopt(bool use_io_uring, int fd, int level, int optname, + const void *optval, socklen_t optlen) +{ + if (use_io_uring) + return uring_setsockopt(fd, level, optname, optval, &optlen); + + return setsockopt(fd, level, optname, optval, optlen); +} + +/* Execute the getsocktopt operation */ +static int call_getsockopt(bool use_io_uring, int fd, int level, int optname, + void *optval, socklen_t *optlen) +{ + if (use_io_uring) + return uring_getsockopt(fd, level, optname, optval, optlen); + + return getsockopt(fd, level, optname, optval, optlen); +} + +static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring) { int sock_fd, err, prog_fd; void *optval = NULL; @@ -980,8 +1059,9 @@ static int run_test(int cgroup_fd, struct sockopt_test *test) test->set_optlen = num_pages * sysconf(_SC_PAGESIZE) + remainder; } - err = setsockopt(sock_fd, test->set_level, test->set_optname, - test->set_optval, test->set_optlen); + err = call_setsockopt(use_io_uring, sock_fd, test->set_level, + test->set_optname, test->set_optval, + test->set_optlen); if (err) { if (errno == EPERM && test->error == EPERM_SETSOCKOPT) goto close_sock_fd; @@ -1008,8 +1088,8 @@ static int run_test(int cgroup_fd, struct sockopt_test *test) socklen_t expected_get_optlen = test->get_optlen_ret ?: test->get_optlen; - err = getsockopt(sock_fd, test->get_level, test->get_optname, - optval, &optlen); + err = call_getsockopt(use_io_uring, sock_fd, test->get_level, + test->get_optname, optval, &optlen); if (err) { if (errno == EOPNOTSUPP && test->error == EOPNOTSUPP_GETSOCKOPT) goto free_optval; @@ -1063,7 +1143,10 @@ void test_sockopt(void) if (!test__start_subtest(tests[i].descr)) continue; - ASSERT_OK(run_test(cgroup_fd, &tests[i]), tests[i].descr); + ASSERT_OK(run_test(cgroup_fd, &tests[i], false), + tests[i].descr); + ASSERT_OK(run_test(cgroup_fd, &tests[i], true), + tests[i].descr); } close(cgroup_fd);