From patchwork Wed Sep 13 15:27:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 139046 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:a8d:b0:3f2:4152:657d with SMTP id gr13csp35018vqb; Wed, 13 Sep 2023 12:21:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG0wUn7u0AksyswD3m9+FgQLAEX999BLC0rlyLOzOI/iTa/Q4IGC2bmA06ORAVmdahdxo3V X-Received: by 2002:a17:903:26d4:b0:1c1:d4f4:cd3c with SMTP id jg20-20020a17090326d400b001c1d4f4cd3cmr3123838plb.31.1694632881407; Wed, 13 Sep 2023 12:21:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694632881; cv=none; d=google.com; s=arc-20160816; b=r5G8ioN/kbFFnfy5Dz8omcoORoACJvxkUlYMq3GRfNRatj8IwqxgmU1jdW125MoyAM PbKAul8r8ladPl3jITSxAjXEmhSyj59G6YCciXhWC+agsDy9gIT4fxriz/z0Zk3V/HtC Vm2aZE3SsuY2vVgNhUGcdOGRjmPJ2qcVPNh6TY3hgN19Yh/G953fruzbHxbpIuAVT0o4 ywMiIzcJ7Wp8d0PBTQorQOAa47VCOYciohK9etAbCeFbZt+Dlvht9k1uHkl2hHJp3tnK 6l7aau0qdzZENFwnrKMU0CyBdySOxcnJe/jIeiVgvrIf94AnVTzsFH2X7Dr9+vd4hk0o +6Ww== 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=tOs9crnAl9vjwzVTjWnpdwyDk2frGHnXKfwQDgSR+NE=; b=dy992PcCSYrnNoqQUzZYHJVIjs3HYvhh/dgk1L1i3AESS8nX3wFMwRwqTIX9b8eJdP /5Pcs1ZdXmFpI8Mr3Z/qiqECZxQInQEl4GE4E1tbcsQ4wPNj5iY5LIUAQa7VL1tnkbJb Y7Rpqc6z8zO+j2Awj8h+ZmYYl4gQIZzhn0pS74IXHN4uhHs8Mh9sDHyVNY7IXnv3hIfd DX/B84//L49HPEkBOnEGRWGZPSnc21u3/LD2cfCBfsxs1Up3UaXKFhLQp1T9L1onRQZ9 ideYpo3qCTJ9xS36NvLcCl+mJI4bC6OH6dwDoPzjzWXhakgBxxOQhKc1F2VXWhFz2LVR Jk2g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id u6-20020a17090341c600b001c3ae631b86si6953116ple.112.2023.09.13.12.21.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 12:21:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 35C6B8452EF4; Wed, 13 Sep 2023 08:28:48 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229533AbjIMP2b (ORCPT + 35 others); Wed, 13 Sep 2023 11:28:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229470AbjIMP2Y (ORCPT ); Wed, 13 Sep 2023 11:28:24 -0400 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38E47CCD; Wed, 13 Sep 2023 08:28:20 -0700 (PDT) Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-99c1d03e124so867744766b.2; Wed, 13 Sep 2023 08:28:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694618898; x=1695223698; 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=r0JDYjI2cbC89op4ypM56Lucvd/o0QrOrjFCoOnwuqf8paPw/YaaiOg8jA2FapF+if fj3In3b+UnWOv8v7TV+aBpzoTp1fJpqV2QHyzlNlG2WKF7aH1d6VSkWVmemIQ8DacD9m hTQu0q392R1rMW/9VdsjcHBz6sit7O3KiqZ/pnWWIbG8SNWB+lhKOjE67FXZlHaZhlQi 6YmM4ZJuz//WH3I5bi8TZU2BAnlfyZGOFSIYK8NlJUfVHnowMBoLUDXnJ1GfaoADrzzt lVsLLRM9NhAbvamcKoRlgwSUo3j5vo2yfwIP/JJ0nKYPC4gTfog/rzNls1AhhpNw6K43 JVQw== X-Gm-Message-State: AOJu0YyFS9csFcf4pGBzrEtRzuU6y2SXU5qlpsw5Wub/vn5ZXULjkwgG nzY0IURLNW4KzrtAhizhITGiTCHK0zM= X-Received: by 2002:a17:906:18aa:b0:9a5:d83a:65be with SMTP id c10-20020a17090618aa00b009a5d83a65bemr2303502ejf.27.1694618898511; Wed, 13 Sep 2023 08:28:18 -0700 (PDT) Received: from localhost (fwdproxy-cln-005.fbsv.net. [2a03:2880:31ff:5::face:b00c]) by smtp.gmail.com with ESMTPSA id t14-20020a1709064f0e00b0099e05fb8f95sm8647352eju.137.2023.09.13.08.28.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:28:18 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, pabeni@redhat.com, martin.lau@linux.dev, krisman@suse.de, "David S. Miller" , Eric Dumazet Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, io-uring@vger.kernel.org, Willem de Bruijn Subject: [PATCH v6 1/8] net/socket: Break down __sys_setsockopt Date: Wed, 13 Sep 2023 08:27:37 -0700 Message-Id: <20230913152744.2333228-2-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230913152744.2333228-1-leitao@debian.org> References: <20230913152744.2333228-1-leitao@debian.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 13 Sep 2023 08:28:48 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776951368216758994 X-GMAIL-MSGID: 1776951368216758994 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 Wed Sep 13 15:27:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 139211 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp44157vqi; Wed, 13 Sep 2023 18:27:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEGkZeSPBezuYtUWRPIN2hSO7HmLIdVeZMLyn8nwxnfQaTnuH2CYvNqjhxZYKBOM1RFvO3B X-Received: by 2002:a05:6a21:3d83:b0:13a:43e8:3fa6 with SMTP id bj3-20020a056a213d8300b0013a43e83fa6mr3664048pzc.23.1694654834367; Wed, 13 Sep 2023 18:27:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694654834; cv=none; d=google.com; s=arc-20160816; b=rhCpU2iC+xti/q9OMFFK+M8lWkfTFnlxc3sME3wfcAWJvvR+zJMGpNOWPsVAgzNhEg 1E8mVpQakcTx5SY0NFs9PVHelWIR+E/6+D+VztiXs5Vqn6sYQvGujPl6oHLe8fxSB0XQ VcowZ3csPoeZk+puF50eDAjuDVkIuM/UrLqHdt63qwHD4l5cdR05HQqciyhyTo6+GcQn XQ2TPvJndeDYfj2tc1G9kh1bVDcrjjzBZI+gFoCuC56Swk39WXZFqjG0/mTCJdp06Lbz 2WSMsr255koLhHC9k5GBsHxDfoeYDYbUCPak2/euWfiKFXbOQFu8Plz0oRe8TuTwqE8H xmfg== 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=HNDakB63GeZKBPdnx2EtHPN64oGZn+t0qvDI93lexvQ=; fh=HmFj3TSCWJbXSlIbjcHElL/SJMYYS7knVWpkGPW/7+c=; b=yxTtKhPzoKIbh0Fl6NkCksKBPu7xW8iRRUIkgErnjKyqJG8YK0OjDn7KkIpz6ZW8lH bAj6TxcAV5iFP0tCDJgXYqxm8NFcQOunLNV04iO+gpSPiGB6r3yDIueOUAGhV1tg67N2 NjN5TbI952W2Y8GI2ZZL4YrHhi/XOiBWbrio5sD+BWEvyVXdEGKeUVMVBZkY4etpE7Y7 g+uABmlhNERIaMfIBaW3QBx9K8RwTPbn9Ccb7iiNgavcec4TZ6oSWqbFZbAUkIQHWq7Z cUuaE1HDi4upmqAO5+Li3e9F3REIAAonz0uYdvc6ZFqkvtssz+RSLj0yRYP8U+QlGeor r0ig== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id s8-20020a170902ea0800b001b9d335180dsi589404plg.618.2023.09.13.18.27.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 18:27:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 48B92801F750; Wed, 13 Sep 2023 08:28:42 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229472AbjIMP2c (ORCPT + 35 others); Wed, 13 Sep 2023 11:28:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229511AbjIMP20 (ORCPT ); Wed, 13 Sep 2023 11:28:26 -0400 Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12B811BC3; Wed, 13 Sep 2023 08:28:22 -0700 (PDT) Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-502e0b7875dso925656e87.0; Wed, 13 Sep 2023 08:28:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694618900; x=1695223700; 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=HNDakB63GeZKBPdnx2EtHPN64oGZn+t0qvDI93lexvQ=; b=fvCHy6ZcoUIYrtFltO0XIziKj5/zzrXcoANIOXCNMIGvhE7dW4y/2RfYPpDblDZlIb hBTxL++nfoMUvP8wKbluFXmMSzDb+7+iZIEaKTrXbhlETCFMIQ7WwGRoB8YJtIq+PCq+ n+mP0F7cLlfiqwQ/xnMatPavlzjUiFZ0nF4nRwWDNt4ucOS6ukt/OEI/qTFlAocX0ZSP T65d74W3ZvXaf8sH8ngpAmWEoxiuXPPZ7Av5lLDVbNTxSfJ4D7lsWgr/QStFVlrJAZWh So65SFqEpkqeNTLn1wHgTv0T1mqSK8FVhe/+droqTT5lt2pcw67D+SMlHZSjZjGrB35G H5xw== X-Gm-Message-State: AOJu0YyD4hrFwSBwkpebL58O4cV7D9N1lzD9A5kbo4eVTnU+xLVf3CMi +T5eCPdAx5+vkGGBjwa5b0E= X-Received: by 2002:ac2:53a9:0:b0:500:b64a:ad15 with SMTP id j9-20020ac253a9000000b00500b64aad15mr2104579lfh.52.1694618900057; Wed, 13 Sep 2023 08:28:20 -0700 (PDT) Received: from localhost (fwdproxy-cln-016.fbsv.net. [2a03:2880:31ff:10::face:b00c]) by smtp.gmail.com with ESMTPSA id n13-20020a170906b30d00b0099cc36c4681sm8596578ejz.157.2023.09.13.08.28.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:28:19 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, pabeni@redhat.com, martin.lau@linux.dev, krisman@suse.de, "David S. Miller" , Eric Dumazet Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, io-uring@vger.kernel.org Subject: [PATCH v6 2/8] net/socket: Break down __sys_getsockopt Date: Wed, 13 Sep 2023 08:27:38 -0700 Message-Id: <20230913152744.2333228-3-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230913152744.2333228-1-leitao@debian.org> References: <20230913152744.2333228-1-leitao@debian.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 13 Sep 2023 08:28:42 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776974387892567172 X-GMAIL-MSGID: 1776974387892567172 Split __sys_getsockopt() into two functions by removing the core logic into a sub-function (do_sock_getsockopt()). This will avoid code duplication when executing the same operation in other callers, for instance. do_sock_getsockopt() will be called by io_uring getsockopt() command operation in the following patch. Suggested-by: Martin KaFai Lau Signed-off-by: Breno Leitao --- include/net/sock.h | 3 +++ net/socket.c | 48 +++++++++++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/include/net/sock.h b/include/net/sock.h index aa8fb54ad0af..fbd568a43d28 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1863,6 +1863,9 @@ 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 do_sock_getsockopt(struct socket *sock, bool compat, int level, + int optname, char __user *user_optval, + int __user *user_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 360332e098d4..fb943602186e 100644 --- a/net/socket.c +++ b/net/socket.c @@ -2333,28 +2333,17 @@ SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname, INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level, int optname)); -/* - * Get a socket option. Because we don't know the option lengths we have - * to pass a user mode parameter for the protocols to sort out. - */ -int __sys_getsockopt(int fd, int level, int optname, char __user *optval, - int __user *optlen) +int do_sock_getsockopt(struct socket *sock, bool compat, int level, + int optname, char __user *optval, + int __user *optlen) { int max_optlen __maybe_unused; const struct proto_ops *ops; - int err, fput_needed; - struct socket *sock; - - sock = sockfd_lookup_light(fd, &err, &fput_needed); - if (!sock) - return err; + int err; err = security_socket_getsockopt(sock, level, optname); if (err) - goto out_put; - - if (!in_compat_syscall()) - max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen); + return err; ops = READ_ONCE(sock->ops); if (level == SOL_SOCKET) @@ -2365,11 +2354,34 @@ int __sys_getsockopt(int fd, int level, int optname, char __user *optval, err = ops->getsockopt(sock, level, optname, optval, optlen); - if (!in_compat_syscall()) + if (!compat) { + max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen); err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname, optval, optlen, max_optlen, err); -out_put: + } + + return err; +} +EXPORT_SYMBOL(do_sock_getsockopt); + +/* Get a socket option. Because we don't know the option lengths we have + * to pass a user mode parameter for the protocols to sort out. + */ +int __sys_getsockopt(int fd, int level, int optname, char __user *optval, + int __user *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_getsockopt(sock, compat, level, optname, optval, + optlen); + fput_light(sock->file, fput_needed); return err; } From patchwork Wed Sep 13 15:27:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 138893 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp1167493vqx; Wed, 13 Sep 2023 08:30:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGptew3JakKzsD8q1s6o9BNzEuV8Vn3cqOKwiPVBMAsriYb7R6FiiqGITFUPFx/N1FQA+hA X-Received: by 2002:a05:6871:813:b0:1d5:a72e:154e with SMTP id q19-20020a056871081300b001d5a72e154emr3294358oap.36.1694619054385; Wed, 13 Sep 2023 08:30:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694619054; cv=none; d=google.com; s=arc-20160816; b=s1DKLIXheAVCxFEUOI2urszaK3o1sDHJzvpeQCR+SRc/dHPBQUmNK8JnBLu91NsokG Flry8PlKRiM+zGLH+wcgXg7rVcfz8jRX7RddJbLMcVsbiqGvE8wVI03lc+n0apVpVTQ5 FQR85I4IdzdBLVcmar2UZAojuS0XGoFC/oAZIBYjTS44qppxlKuiPl38+bwsPnrmfi3M FHcRcbCIwknxkkOCaSPmOoE4ZTmhovQe596XI906Hgkdqtixn5h4Oeh9DWxx610j6hEu zHct3Bak2ikOF8tZ6SQsfJGvgvDq+frOAjjsJta7mhsHu19fS2lhvmRgluDl1Yq5v5da nNlA== 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=+vvxyHXTfiSW+8d09yTsLSiY7Ry74wlSEWGj/O1wQD8=; fh=U2H3/FPocPBB6GPezKHW9HS+wZ8AoMiBoUwvb1zfBQs=; b=sCzkLce7jwnykigHB6A8hlY2weu+8l4eerhJd1kdRIlB5EIHeV8vqKS3vA2i+SvLYV bYPf4Q5APl3F961TsMs92sPJzbUS5ObOPrI0En4kMkF/C48keMk0hAnYYqyAYXqwplH7 TrX4LygmIl2VGMWKDMETfNxRqxKnEU4B8FUl/E6p4YC+BKyeThhj+vxWXnJaAA++QwGq zHsZ/ZwQrPLQ2lCYMBURx7xL4nPMIJrAnro50YiuA5znVBRBtQ7TtssTHu+BozekmS1w LaVAjyXLUizaNCmNroahrikR8AKsbQPElfiZ9rCMfHukl8mv+GgTCfuy0diHZ0b2FQWW tbxg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id f20-20020a63f114000000b0055c8f4a56d5si10161577pgi.600.2023.09.13.08.30.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:30:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id A2CE980E608B; Wed, 13 Sep 2023 08:29:20 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229536AbjIMP2e (ORCPT + 35 others); Wed, 13 Sep 2023 11:28:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229475AbjIMP21 (ORCPT ); Wed, 13 Sep 2023 11:28:27 -0400 Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 794111BC9; Wed, 13 Sep 2023 08:28:23 -0700 (PDT) Received: by mail-lf1-f49.google.com with SMTP id 2adb3069b0e04-50098cc8967so11230224e87.1; Wed, 13 Sep 2023 08:28:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694618902; x=1695223702; 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=+vvxyHXTfiSW+8d09yTsLSiY7Ry74wlSEWGj/O1wQD8=; b=ID3aFaZrNBfatNHIcF2NhNrJIe4CRwfzK1LnEMS+UWByX5zZqaEFxRRYdSFrRwDwy+ aNW7Q1nsuGzP7dJtrYxscAaRZWHnl1APV8enrlZ+27UWhn9kJhcLGQkqe04OwN82OsFz Q/WgW9/28cKN3p5bpukpjCH3hrf9QDummyU6I6WZZuytCK5C/eWiN0XoiXTVv3tWrpSm niVoPVTPrrJY6a3lQEX6hzkiOK5QXk2on4NbZBT2rsO4aFolozc1+hDbzETk9a2QSClI 3y444Zr3BkgudiyHzsJnaBRZ6l4HZzPGzA27v1Kh1d/IZCEIfrv8AkuY/4hbEwV6RGSv 7CHA== X-Gm-Message-State: AOJu0YzIBropAgKvxi40wTJe7b3a4PHGDvxeLssJqbqSrvVWIghiazHG LmnAxa5125iMx11ingdvHho= X-Received: by 2002:a05:6512:3e17:b0:4fe:676:8c0b with SMTP id i23-20020a0565123e1700b004fe06768c0bmr3139980lfv.11.1694618901572; Wed, 13 Sep 2023 08:28:21 -0700 (PDT) Received: from localhost (fwdproxy-cln-017.fbsv.net. [2a03:2880:31ff:11::face:b00c]) by smtp.gmail.com with ESMTPSA id oz13-20020a170906cd0d00b0098951bb4dc3sm8608469ejb.184.2023.09.13.08.28.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:28:21 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, pabeni@redhat.com, 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 Subject: [PATCH v6 3/8] io_uring/cmd: Pass compat mode in issue_flags Date: Wed, 13 Sep 2023 08:27:39 -0700 Message-Id: <20230913152744.2333228-4-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230913152744.2333228-1-leitao@debian.org> References: <20230913152744.2333228-1-leitao@debian.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 13 Sep 2023 08:29:20 -0700 (PDT) X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776936869390213940 X-GMAIL-MSGID: 1776936869390213940 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. Reviewed-by: Gabriel Krisman Bertazi 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 Wed Sep 13 15:27:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 138900 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp1172347vqx; Wed, 13 Sep 2023 08:38:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGXOPATDKzka9QCbehqCqpIz5WbI0CkSBYMTfXfNpiUrLOOCZNmbmknjN6iyIYb5hrbGhi8 X-Received: by 2002:a05:6a20:cea1:b0:14c:628b:4b98 with SMTP id if33-20020a056a20cea100b0014c628b4b98mr2261497pzb.4.1694619497515; Wed, 13 Sep 2023 08:38:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694619497; cv=none; d=google.com; s=arc-20160816; b=wF7JGHgyqJGH93cvVPg3VrHh2qwnq3TlRgDapln/RmDQFStJ39iofwu2PrwhYgHSSJ LK6/FXb0U2+3iiCbowinMc0r5tndsQGa1UWx/74+Nm9hFC7/jKtyVHNPr4jOVkr/Dr8h z0lCwywsyoOEoCh07VTxeXvujzhwaCrtPS79HYns9j08+BxC4pRDnQPiaFhhqxlw+Dmb qD/A3CeyQbvsYKZeh7z2+OIAmU5laieNQj5vLyiAwtFs0cu00unsCfXZSDZaDGu5pE9z O+AhbF14c3iBRWK1JsVhmBFDSQ3ZGA8REnfdjtw9d6exTwhMaMo5jlLUM+rDJHidIlyj yMGg== 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=HzQ2L3VA3K1qXdfZEOlnENEcthLwu4DsWTTSpmxxUbA=; fh=Cla3q9+mA/2pdaUGIlHmh2g4S6mAIaOrlJD44w2eJjg=; b=n2Ja8jAZIm54AdsjC2X+EZ/XAbzZpXbszAEAr9F+5IdLWnUGNVQaTXanGI/kGe4V75 8gvnTQ+D1DuXBJ+Vxl81/5y6OcAiz1Z5CNR9N6Rajaty+q9/YNCOEOGht0T7jdyxNJaO kNOKlIHses8rNZ0hsdA+CaO4ArENIvcxxuloovfS2byZ5NDR3vDa0/vUvNnRZFFVvWp2 VrfV8uo401sTMEqKyJpmsbjdq/gQm6RKuYbW+ssg+skFgkd6TREecZln8pLH+QrbTAC9 viRCQJal3TlsB2f0t84pv+Mj1SLGZliDKh3wYK1OHbKS+HWT3kbUD22Wa++7O7jT7OXP x0uw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id l191-20020a6391c8000000b0056a290addadsi9925502pge.787.2023.09.13.08.38.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:38:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id BB06D8182EC5; Wed, 13 Sep 2023 08:29:04 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229643AbjIMP2p (ORCPT + 35 others); Wed, 13 Sep 2023 11:28:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229508AbjIMP2g (ORCPT ); Wed, 13 Sep 2023 11:28:36 -0400 Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com [209.85.208.45]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F50F1BDC; Wed, 13 Sep 2023 08:28:32 -0700 (PDT) Received: by mail-ed1-f45.google.com with SMTP id 4fb4d7f45d1cf-52a250aa012so8971770a12.3; Wed, 13 Sep 2023 08:28:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694618911; x=1695223711; 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=HzQ2L3VA3K1qXdfZEOlnENEcthLwu4DsWTTSpmxxUbA=; b=VrCxKy3gVQL3CNibU6StX8GECh4MpChUUWRlG7FbItfiO5ucoIMz1tDSXpStYIBaNE 4YOSqUumfZqMTLPcvJV7KpL73ZqqLejOoEofafQh7X+oOpTbZ/P430hdiBzz4gg1RTR+ iaatU66j7wArEbc3GKmGLdINNEqOTmjcTn1oXco57WInC7eNMOlXGPxxjckV43hTJ6Op glK2vWeOj2c/S1ZZ9Yuj/IXusRhCmqF1Su/qW3JJBILw8xMLfhLvOCQlsHSbfHtigG+k I1BrGeBANRdprjVL7X8Mt5aU3flMBlwP2v7CMWgeJayEMlekt/kSqks9BBQWHyymgGYH nfYQ== X-Gm-Message-State: AOJu0YxgcedgtQppuc7IPxSDgSOdHpzGBtCrSkSL9X+QrbuQmCmPGB+H pN+bm8uRmhO7OWGhEowTAMU= X-Received: by 2002:a17:906:3091:b0:9a5:c944:8e71 with SMTP id 17-20020a170906309100b009a5c9448e71mr2187858ejv.60.1694618910695; Wed, 13 Sep 2023 08:28:30 -0700 (PDT) Received: from localhost (fwdproxy-cln-000.fbsv.net. [2a03:2880:31ff::face:b00c]) by smtp.gmail.com with ESMTPSA id t1-20020a170906268100b0099cce6f7d50sm8586983ejc.64.2023.09.13.08.28.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:28:30 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, pabeni@redhat.com, martin.lau@linux.dev, krisman@suse.de, "David S. Miller" , Eric Dumazet , 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 v6 4/8] selftests/net: Extract uring helpers to be reusable Date: Wed, 13 Sep 2023 08:27:40 -0700 Message-Id: <20230913152744.2333228-5-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230913152744.2333228-1-leitao@debian.org> References: <20230913152744.2333228-1-leitao@debian.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 13 Sep 2023 08:29:04 -0700 (PDT) X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776937334276324930 X-GMAIL-MSGID: 1776937334276324930 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 liburing infrastructure. This is basically a copy of what we have in io_uring_zerocopy_tx with some minor improvements to make checkpatch happy. 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 Wed Sep 13 15:27:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 139213 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp44276vqi; Wed, 13 Sep 2023 18:27:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG0urj7KmNV4F8WxHYnhaxxt2O4RHKlt8TQCk0bSgkzE74G1bpGYQstcSgSCnTy/LGy1orL X-Received: by 2002:a05:6a20:a127:b0:132:f61e:7d41 with SMTP id q39-20020a056a20a12700b00132f61e7d41mr4567642pzk.5.1694654857745; Wed, 13 Sep 2023 18:27:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694654857; cv=none; d=google.com; s=arc-20160816; b=rESqJ747U8toqMiaNkiCZhnZ2tt5prbeYdm6B/NZIA09CNWfUkwaQDW0ktmll/RxI/ t8jNmQo6ptGZHPFvYlozYlvc2MYONUJA3vTdBjUrpLBf7eXUBpmw4vkhEbliJcal4FJ1 WlhmXXDGnTzjnxFBljshSSvPRCdDO69I/j2syDArc7QrgVuAmSK+cLpYttq1tUp+v2Dr y6UhBaRYFK4itp1JMpnTK33ZGw7hfmcKVajK8ipKNjXTHxmuxk/0wWlUfbLyPqA4ZDIY 5u+rYp9zbT2FHLuscauyWAaQNZEaeoLdzPhCTrmXEQph5TIREnVkIHQ+pSJqeeEf3kO4 2Ihw== 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=rF5iM8ZkJqphGmjRFU1s0SbK7ImZ/Xg4gUBQOimY8QI=; fh=U2H3/FPocPBB6GPezKHW9HS+wZ8AoMiBoUwvb1zfBQs=; b=EwJzlSOBOvWaRMj1f2lXiS7ee2sTE3UBQHCcrZ9aieDixstNh9ZFgypS27MhptUDHL LppQo4Vn9KQDcxmZeftk2rrxOmKQ7G0ELCOqs4rgn6rmNiYOTzUesv44c/8sa9mdllJ8 DbR2NETPu6FVxcjiHV2/oitaoM38ZU+4YG5DvMuuQS/1FbDnRobgQD33BePEnNGl58kw zOih40sMqARTL5SPuWPli50e/xAmw6o4WT44xK35OxjQM7FpDoLiJQN/+qqmxMU8hBbn dt2I9maxp/5RMP2IV1TsDxlgUCXjwcrRB7s4thp5yWEzoQ7X4C9drfuH/xm3xWW2q0Xu aQSw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id cq6-20020a056a00330600b0068e47f1fc7esi463210pfb.159.2023.09.13.18.27.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 18:27:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 67001801D0F6; Wed, 13 Sep 2023 08:28:50 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229564AbjIMP2t (ORCPT + 35 others); Wed, 13 Sep 2023 11:28:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229587AbjIMP2i (ORCPT ); Wed, 13 Sep 2023 11:28:38 -0400 Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD7BC1BE9; Wed, 13 Sep 2023 08:28:33 -0700 (PDT) Received: by mail-lf1-f49.google.com with SMTP id 2adb3069b0e04-502b0d23f28so6812681e87.2; Wed, 13 Sep 2023 08:28:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694618912; x=1695223712; 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=rF5iM8ZkJqphGmjRFU1s0SbK7ImZ/Xg4gUBQOimY8QI=; b=i6HhVNKpabsOIC2vJKOGy/osVxXwPvIZ/0kg4nRvpeW7rlqSCb1WtpJvR0lMM09S17 J8HcNioLp2n4DbmP/s0PE+Cx37xLFQnqsGTt1q5oV+0tjQhOUnO2s2g4YpDqr0hcZpVV +wmiSyFHqRk7c/FXKqZ9/7odXustALINmGb73P30w0ZsdZwdZClnRaeqlh045jqSqYNJ P3h8eSIEfmfPOedoXjUZ6fCEau/tQETJjAY2+BgBFCdjibIpPxcTmXIEwWLLfxh9NUfo myMzxPlTF/YHEq1UnJuzKhkuwVGZf/dBtk85RChMXTsJOEiWtS+Lrh8mEBGe+f+W2ELg JcXA== X-Gm-Message-State: AOJu0Yy1OmCJGvkJJiddeQWIz0PrJy5qLdOPyI3QzvaFOlcNkez8eFY5 rEWUHMNu7+aLuEEsWmHwWXQ= X-Received: by 2002:a19:2d08:0:b0:4fe:1681:9378 with SMTP id k8-20020a192d08000000b004fe16819378mr2448500lfj.66.1694618912053; Wed, 13 Sep 2023 08:28:32 -0700 (PDT) Received: from localhost (fwdproxy-cln-001.fbsv.net. [2a03:2880:31ff:1::face:b00c]) by smtp.gmail.com with ESMTPSA id kj27-20020a170907765b00b0099b5a71b0bfsm8705015ejc.94.2023.09.13.08.28.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:28:31 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, pabeni@redhat.com, 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 Subject: [PATCH v6 5/8] io_uring/cmd: return -EOPNOTSUPP if net is disabled Date: Wed, 13 Sep 2023 08:27:41 -0700 Message-Id: <20230913152744.2333228-6-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230913152744.2333228-1-leitao@debian.org> References: <20230913152744.2333228-1-leitao@debian.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 13 Sep 2023 08:28:50 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776974412375133174 X-GMAIL-MSGID: 1776974412375133174 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 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/io_uring/uring_cmd.c b/io_uring/uring_cmd.c index 60f843a357e0..5753c3611b74 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,4 @@ int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags) } } EXPORT_SYMBOL_GPL(io_uring_cmd_sock); +#endif From patchwork Wed Sep 13 15:27:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 138894 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp1168116vqx; Wed, 13 Sep 2023 08:31:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEKbKa283f1Y7x6IDKht+/WJBbWH2UggaMvBZbKyu0ploZTzgKgNNhCq0983+COQ7VYgtqa X-Received: by 2002:a05:6a20:728c:b0:14e:a1f0:a8ea with SMTP id o12-20020a056a20728c00b0014ea1f0a8eamr3077360pzk.3.1694619106753; Wed, 13 Sep 2023 08:31:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694619106; cv=none; d=google.com; s=arc-20160816; b=UbbE9md9VJZ3Mt1h38ZsSTwpG/+iKnIOznxBZjy0t6CBhLcVSQkz00fGxENOBoXfV7 bcAzBI/FU41WcjhcjaMxeIl6fmn/RpBWtndp+RCVTGQKJYOcM/ebn8lA5bkSN3O9GbNN iN6GI3vxnIHGd5SJMgG8WRBhqHN5lGH371RIfq3fKy3+Ui1OouUX3yGqp1e6zTKnSllq t6l9aDWe6t8BiNBDb9oPfBgqf52xdjPWX7gQFIhc21UvFyZGgprihJOO++nWfxr/93fk Vv/Ix40KvP8BNMwrysgyRtCcNF2K2F0KmCb5ptYYTwSfHIA5K7tB3HnrW1hnAhmApZrV K/AQ== 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=lDiXjrFRlF6VHQWvFjoZXtrdxjTNkGukVcEtNKPtPXI=; fh=U2H3/FPocPBB6GPezKHW9HS+wZ8AoMiBoUwvb1zfBQs=; b=SlhdqOe2rZnKAjZNwecvpVN1xRQTlWoiXvFtUfG1Jf1P3oH2U5Og9aZhayV8dtw/QH mA61zAF59FEhAK4ByBZba7Kh7AM5tUqWkCPZPI2mrTRLQFTub9L7A2ZqIy+h0pzZeJHc OZc4b735sFibPaO8FSQ6fCRx2VsGczuY0Q+exYCiJbEm+457aZ0eQ1HaxZ42Zv7mWmBW 97N1yi55bnTaf28vsMovR0UE7xPNPUPKmK0+pI6vHxqA8j89/XkDmXiJEYZnwWlqCxF8 mBjx8dVBHvC0mB1NVcvcurF6fbhbYP01GiLkFQXG9671DAvsdC5qk04Fa7uZJZuPtd4Q F8pA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id k20-20020a637b54000000b0056da0ae25a2si7344156pgn.32.2023.09.13.08.31.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:31:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 5658F811B05D; Wed, 13 Sep 2023 08:29:16 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229518AbjIMP2w (ORCPT + 35 others); Wed, 13 Sep 2023 11:28:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229607AbjIMP2j (ORCPT ); Wed, 13 Sep 2023 11:28:39 -0400 Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15F4B1BF2; Wed, 13 Sep 2023 08:28:35 -0700 (PDT) Received: by mail-ed1-f48.google.com with SMTP id 4fb4d7f45d1cf-52f31fb26e2so5405107a12.3; Wed, 13 Sep 2023 08:28:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694618913; x=1695223713; 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=lDiXjrFRlF6VHQWvFjoZXtrdxjTNkGukVcEtNKPtPXI=; b=Sh3e4sFKi8aoMbuK3oScZ0WdKdjGR+lB9KaHR3tb17jej2kpnaMCNIJ2kbztNYGqzw fI2JXxV6k/Bs77yN1D/jXD/r3cpbjsejG3cpe3vhINoxUFqUYK/WI31nhXWn7/xt1FCw m3y1pV21Z8bpzrhvKlmAHX8vmyn7ZCeDS8nz9jCyKh+us2EA3l4HAlHvLVt1xFap/0qI NKn6BzGLsk2I3UJEc+zi0mfZOlLi9HevJM7gy4NCsVy2Gh3RlHHVUhzwgmg/Y0bhB6Er LM7E4uJXqlUQxF0moNLFd6j6doNMnTD/pTc1ONYrT/+uHU/ToFTYFGrOdUs6GsZu8XMC vj+w== X-Gm-Message-State: AOJu0YxwbNAaFJmFJg7CzTwHYmNG6JZw8ZXQQ9TiEx4YgRtoTiGYu31Z 7cBXC43QgGipnPvDM2YWQEk= X-Received: by 2002:a05:6402:1b04:b0:523:d1e0:7079 with SMTP id by4-20020a0564021b0400b00523d1e07079mr2423946edb.21.1694618913506; Wed, 13 Sep 2023 08:28:33 -0700 (PDT) Received: from localhost (fwdproxy-cln-009.fbsv.net. [2a03:2880:31ff:9::face:b00c]) by smtp.gmail.com with ESMTPSA id c2-20020aa7c982000000b0052e2aa1a0fcsm7495234edt.77.2023.09.13.08.28.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:28:33 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, pabeni@redhat.com, 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 Subject: [PATCH v6 6/8] io_uring/cmd: Introduce SOCKET_URING_OP_GETSOCKOPT Date: Wed, 13 Sep 2023 08:27:42 -0700 Message-Id: <20230913152744.2333228-7-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230913152744.2333228-1-leitao@debian.org> References: <20230913152744.2333228-1-leitao@debian.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 13 Sep 2023 08:29:16 -0700 (PDT) X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776936924395054251 X-GMAIL-MSGID: 1776936924395054251 Add support for getsockopt command (SOCKET_URING_OP_GETSOCKOPT), where level is SOL_SOCKET. This is similar to the getsockopt(2) system call, and both parameters are pointers to userspace. Important to say that userspace needs to keep the pointer alive until the CQE is completed. Reviewed-by: Gabriel Krisman Bertazi Signed-off-by: Breno Leitao --- include/uapi/linux/io_uring.h | 9 +++++++++ io_uring/uring_cmd.c | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index 8e61f8b7c2ce..1c789ee6462d 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -43,6 +43,10 @@ struct io_uring_sqe { union { __u64 addr; /* pointer to buffer or iovecs */ __u64 splice_off_in; + struct { + __u32 level; + __u32 optname; + }; }; __u32 len; /* buffer size or number of iovecs */ union { @@ -89,6 +93,10 @@ struct io_uring_sqe { __u64 addr3; __u64 __pad2[1]; }; + struct { + __u64 optval; + __u64 optlen; + }; /* * If the ring is initialized with IORING_SETUP_SQE128, then * this field is used for 80 bytes of arbitrary command data @@ -734,6 +742,7 @@ struct io_uring_recvmsg_out { enum { SOCKET_URING_OP_SIOCINQ = 0, SOCKET_URING_OP_SIOCOUTQ, + SOCKET_URING_OP_GETSOCKOPT, }; #ifdef __cplusplus diff --git a/io_uring/uring_cmd.c b/io_uring/uring_cmd.c index 5753c3611b74..a2a6ac0c503b 100644 --- a/io_uring/uring_cmd.c +++ b/io_uring/uring_cmd.c @@ -167,6 +167,19 @@ int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, } EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed); +static inline int io_uring_cmd_getsockopt(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 __user *optlen = u64_to_user_ptr(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_getsockopt(sock, compat, level, optname, optval, optlen); +} + #if defined(CONFIG_NET) int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags) { @@ -189,6 +202,8 @@ int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags) if (ret) return ret; return arg; + case SOCKET_URING_OP_GETSOCKOPT: + return io_uring_cmd_getsockopt(sock, cmd, issue_flags); default: return -EOPNOTSUPP; } From patchwork Wed Sep 13 15:27:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 138896 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp1170862vqx; Wed, 13 Sep 2023 08:35:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFD5D18xHwJVYcBjeY6eSo7KX4v0d/dbzVCwejf7KtQEz60sBsn2SgL71VuBJUSqPOPFt2q X-Received: by 2002:a05:6870:c152:b0:1be:dbd2:2bfa with SMTP id g18-20020a056870c15200b001bedbd22bfamr3287945oad.20.1694619352119; Wed, 13 Sep 2023 08:35:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694619352; cv=none; d=google.com; s=arc-20160816; b=wGPrJ1fwBCCZqMBDdA+1V6dckCSZeSRnMJ3u0Pwf7aSpMVQngyAt4vouHXwZZJb/BB lHonF6G4WMdooQiSzoPCRAEHXMbGFubqzgZhKGl+dM9n+80l8RtFlO9v+ApNPa0U6L5x APVwRSqWppCENWquRdYwvUI1NK1VmWC49V62D2ies5uXEjHxXFHUSPKnEAp3HEZ+bjEL Z1rsiKP62sMw9n1TJDCPbvFqQgKpOT03cdjrmVRtn7mkVDqisjq9j+jIrscBRatHMRA/ ELV6V/El8EyDMdjNdLeCPs2KI3H15Y8SS6xs+nSYgeKw/AGQAVV+Rp6lzHu9dHPt6mvr djZw== 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=U2/uGjka4vxabDaOe18mLCK6HUraAejeoqQZCxCnFKY=; fh=U2H3/FPocPBB6GPezKHW9HS+wZ8AoMiBoUwvb1zfBQs=; b=wkiPn0xiiLrjC6gziU6mpJ5bA7JOMLNy5dMohJuGzKUtmUqkxPhnjQteq8FhgSps9g nRHubTuuyLi433fuz0r/h2OHkLYXeKeK3dqP1+TRIvAjeFglAsXV2ojtlqEfvVJIpe+z QHNZKHJVXHmzq7yxoQ8haL6/vHpHIq+3djVM3F8GlayZbiFpRBe3ePH35ijrKpDWC6g3 NEMqn74237QRFEmr1R2M6dAPfhzYMNK8oo6HjWjX4gViyfmqwbSkVzVyojn/XVsQQmej 7roQY3WM383zIEVg2tX7RiPY3pmmRX2xmd7wdnVGCCaPOLT3oOIii6TyGgHQsS1irAOu gYJA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id f29-20020a63381d000000b00577812dd270si4891000pga.573.2023.09.13.08.35.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:35:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id EED59830D631; Wed, 13 Sep 2023 08:29:16 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229571AbjIMP25 (ORCPT + 35 others); Wed, 13 Sep 2023 11:28:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229632AbjIMP2l (ORCPT ); Wed, 13 Sep 2023 11:28:41 -0400 Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 982B71BFE; Wed, 13 Sep 2023 08:28:36 -0700 (PDT) Received: by mail-ej1-f48.google.com with SMTP id a640c23a62f3a-9ada6b0649fso89404266b.1; Wed, 13 Sep 2023 08:28:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694618915; x=1695223715; 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=U2/uGjka4vxabDaOe18mLCK6HUraAejeoqQZCxCnFKY=; b=VfJSKC77ByBcDP5hwQNKU/LaZwJU8mhW2J4lGCflQxJPYPBlWv8MiUWM07pMeF2wNd imYd9kG5dFn/Lf7mka/dZ3KEm6lSVQAGB02YgPBle68EW5RHWzgArYNVQAtTW/jEWkXM BosC1Hztq8dQtrhNZgt828eA69e44l7VpJWko32kQ+ob058GwExIm6SDoqKWATVmRaGZ UKrSSDg/RL8eWx32NeMIUYBR2gn33WjeQhU/ms1mi+SqLVcH0+cyOqBQoWGawsrOSkXn hzHyING9VjwoB++ZbrrQt0cxEDKHQ8QWeDxJUn7m+OZ+URrv+KJ0c2dbJdUjstsc4hwM Iuag== X-Gm-Message-State: AOJu0YxN2qqEXXLVi6WWDSmv0En5elmsYUId1mLdbnrccbVTEG9TzYWt DnqR8Y5h79cpMEuXqEvNBuzL98F7oAM= X-Received: by 2002:a17:906:1050:b0:9aa:17a2:e3fa with SMTP id j16-20020a170906105000b009aa17a2e3famr2523073ejj.72.1694618914957; Wed, 13 Sep 2023 08:28:34 -0700 (PDT) Received: from localhost (fwdproxy-cln-011.fbsv.net. [2a03:2880:31ff:b::face:b00c]) by smtp.gmail.com with ESMTPSA id l26-20020a1709061c5a00b009894b476310sm8516152ejg.163.2023.09.13.08.28.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:28:34 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, pabeni@redhat.com, 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 Subject: [PATCH v6 7/8] io_uring/cmd: Introduce SOCKET_URING_OP_SETSOCKOPT Date: Wed, 13 Sep 2023 08:27:43 -0700 Message-Id: <20230913152744.2333228-8-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230913152744.2333228-1-leitao@debian.org> References: <20230913152744.2333228-1-leitao@debian.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 13 Sep 2023 08:29:17 -0700 (PDT) X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776937181832852056 X-GMAIL-MSGID: 1776937181832852056 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 a2a6ac0c503b..2266c829d1c4 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 Wed Sep 13 15:27:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Breno Leitao X-Patchwork-Id: 139047 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:a8d:b0:3f2:4152:657d with SMTP id gr13csp35612vqb; Wed, 13 Sep 2023 12:22:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHnUeHaBw0swmIywWqmfQwuuSTvw/X9Z7oPxcB3BIH6I2wxFFLVQJcMkBXDV5GDhH7G77Lf X-Received: by 2002:a05:6a20:914d:b0:134:2b44:decf with SMTP id x13-20020a056a20914d00b001342b44decfmr3622539pzc.21.1694632939613; Wed, 13 Sep 2023 12:22:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694632939; cv=none; d=google.com; s=arc-20160816; b=RWFyPlJM7FO0K+0N5tIMNUt4U/ARMwbEOrJw3EjI2aqt1wv/8w27nhFWblLLxX97cD ngVe9BulHHUt/PZ3cEJl8Pekj6ZKj36tVvBG/XcPzD9LRdMlaMGv55q0CrIUMqJo0/Ci 2akdeuz1+TAKZOFVXv8hzWftttQqma8tGsUGE1ymoDJhEqQS2/0qaPH1PqUmEgfGNzK/ ysFu9yhxKTnEm46Puh4QwWj27eKrpCHCyy5U9MmqHRImfj2JNgHsKSlAoFbLkpib6IMT Fd9JYohEARjiNOAHgwq3ferPk/v+czWlP4lAuieDACZA/5eaoIMKQLraYhtT/KJ3hVHR rGJw== 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=Q/hkXOJqYFdiXMNumcnCvuCa51XTbvQha3otwAo32Sg=; b=otm9d5FVI/eD3Amy2vgBDHioDWLgaaZwoTFbAzIPnMAt7kU2U8YrqpT8cIOsB36WUd 2gXAnAn8gLMkUwhhNQZOhPVnBhjUlzRMDqjsFid/fS8ZWTugSDiowv27EtEJ91Q4iss/ gfe/AZiFAOZ5u707i1GYKau9naX26le/sGvY2X4k/zO9SfLRfJ3lKzSbrNrZlcHn9d6J pg/RIVlJy7UtTPbmnQQwNNIxcCi69srIOPRdzcdegiEUEEY2dOi5EcpFk11L32Z5Z3oN HSA0hxqyAJ/D/ElVKY8DHR757AZlKMSTB2vzbX9/5i1Xab48CbMliqNKActESFKUUGIz X9GA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id x2-20020a17090a8a8200b0026f91b95730si2030854pjn.90.2023.09.13.12.22.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 12:22:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id B554F8452EF9; Wed, 13 Sep 2023 08:29:08 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229735AbjIMP3B (ORCPT + 35 others); Wed, 13 Sep 2023 11:29:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229573AbjIMP2n (ORCPT ); Wed, 13 Sep 2023 11:28:43 -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 BE9A61BD6; Wed, 13 Sep 2023 08:28:39 -0700 (PDT) Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-99c3c8adb27so888272466b.1; Wed, 13 Sep 2023 08:28:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694618918; x=1695223718; 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=A11Q5a1VP/fcQ9TgOWN78oPoZg6kkmtsNi8QeNGpOkGuscvR8HqQ6es6BHcDKvKp2x lBr2okhW4SWaRc0xECzgePXGbkx53DUIerGucgd5ltRQdrnaZX1FW5qZwaDU3IgLEZ/5 tW3urGbcup0SaCqGrLa6idF3tGoqyRzxZ9eQoOCgzMuTThsnaf4rjR/FRMUDHAk1lj8/ qBHkYYbd95m76Pxyzal2pZKIzPqb0j/1E+u52NBGs9hULg8ElXMQwFfwZjuI0l0dcSLH FCMmdZpbvxuKPQboKGIqYdMMG2s5K7THQNlvjIVVXjnPxQaQPmGaxU+/xjplB5XwD3ZA noLw== X-Gm-Message-State: AOJu0YzCcGjpJd6aYMOGT5rgjkO0gH3gY+w4pBLs4Blmejm/B1PVWTYG iJKu5vdGCKZLxTCDtLkvzlo= X-Received: by 2002:a17:906:3151:b0:9a1:c495:66bb with SMTP id e17-20020a170906315100b009a1c49566bbmr2361258eje.60.1694618918240; Wed, 13 Sep 2023 08:28:38 -0700 (PDT) Received: from localhost (fwdproxy-cln-009.fbsv.net. [2a03:2880:31ff:9::face:b00c]) by smtp.gmail.com with ESMTPSA id rn3-20020a170906d92300b0099cc3c7ace2sm8709378ejb.140.2023.09.13.08.28.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 08:28:37 -0700 (PDT) From: Breno Leitao To: sdf@google.com, axboe@kernel.dk, asml.silence@gmail.com, willemdebruijn.kernel@gmail.com, kuba@kernel.org, pabeni@redhat.com, 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, =?utf-8?q?Daniel_M=C3=BCller?= , Wang Yufen , linux-kselftest@vger.kernel.org (open list:KERNEL SELFTEST FRAMEWORK) Subject: [PATCH v6 8/8] selftests/bpf/sockopt: Add io_uring support Date: Wed, 13 Sep 2023 08:27:44 -0700 Message-Id: <20230913152744.2333228-9-leitao@debian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230913152744.2333228-1-leitao@debian.org> References: <20230913152744.2333228-1-leitao@debian.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 13 Sep 2023 08:29:08 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776951429411386585 X-GMAIL-MSGID: 1776951429411386585 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);