Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp811192lqo; Fri, 10 May 2024 16:27:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVm5Wkmvac1LZ5cKxcxTwRjc+tCWLCsDIMjJDIbQa2X5moFFnzHHMGH43zpoJgXeEtedYPAXxZLqutoO/WD8AlBItgIqvhov9KNSFwgCw== X-Google-Smtp-Source: AGHT+IF+DIEXdHC0uMCcn5MefbxUCFyZLzthEgiDo9dyaiMC3JdC+7pJU6LtwvvjfD1+ce2VA7By X-Received: by 2002:a17:902:ba89:b0:1eb:1485:9d2e with SMTP id d9443c01a7336-1ef43e280e8mr42122835ad.30.1715383642795; Fri, 10 May 2024 16:27:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715383642; cv=pass; d=google.com; s=arc-20160816; b=jProro6EdwssrkDvW1eNCAoijFmZdS/XSIbdoelwK7zFinPkQHh4ztHx3NzXWEGMRJ MQW1Tmnyacp2CEL8d+pE/nLwe8KH+KWTicluqY+bkM5GvYK8njqNIjQ3H3MUvTnq01CQ Nwa51d2yKcsGEegOU6lEhpO/XFre91r0BXezslyRCkRA4mDbrXoKtrNZMjJzysX4SHxo LXWCZvWC3gBiaeDW9jlAIP7u0qiFW4VLS7lSYVEZpC1bgR9sVlW5XxxmVNq26lUvC8o5 P6EJpta03CGaS2Xdg3TiQnf8VKYMs7GZGKicaiP0K77l5uoIoNj1DMjC2pgi7RXZrKC9 dmFQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=OG7Mz+4GZ1p3m4frK9Am4WCrpGGx8QYnGhT78AdGhqU=; fh=IK4R3X+cKCkcwAHJD+BNrljAgefPD2hCXslgZ1PKzHY=; b=z/nNuKEgITrDi1kgO2JlMz8IutbcPGzSVmSo5fHnC9MGBOMePmoPkiajZJ9zSDJfkn qkIqTGxZOWossWXnNXcdAfeLgsPx8+rrVs7FMb0OvHcWoWCrBfkKl0EZfg9QRV/J83SM +He5FxsrwlbB08vYgsPGA8Jyf/0BSVebTYJlcoVeEg0tYskMXZdILkwGt/cYVeoHGSjB MU3YW9lHhPAitrK86rdmfzAfrCbxBcserUyReM6vKMmKlnkZHjdAVzBehtx2I8OoStPu m0HaGFV1RubPvXnxpFsUo1wfPvvCq/ezwV8l5bifU+ljvb0JkZo/+j016PFnCC1lhPen 6qUA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=QKKfnZxH; arc=pass (i=1 spf=pass spfdomain=flex--almasrymina.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-176295-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176295-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d9443c01a7336-1ef0bad615csi43203335ad.160.2024.05.10.16.27.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 May 2024 16:27:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-176295-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=QKKfnZxH; arc=pass (i=1 spf=pass spfdomain=flex--almasrymina.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-176295-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176295-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 6874F28178F for ; Fri, 10 May 2024 23:27:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DDF8D181CE2; Fri, 10 May 2024 23:22:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QKKfnZxH" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8BD60181B91 for ; Fri, 10 May 2024 23:21:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715383332; cv=none; b=Hr015SGCjvGQr0BCNsGas4DLExKuuKzJetDLGywvYdF7Z+pkYIHig4oLCDMZQk7OUKRQn837gdd2MS9IYw+rfUqf+qBFz0k+tYftOkQOcmcw+f97lAfz18BDNOlzCzcG4eWl9r/QqyLMBGww4M2HxoxU8NtFnU4hsFsktBdPa8U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715383332; c=relaxed/simple; bh=Bb9qpS3dXlnEuHVbYyYEQY9HcyK4JssNCaiTh+g4dPc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Co0qL7mJlGExHbGrrJUIHJtaT5Zhxe51rbua8HV7kUfJ8YQ6Nau9Vk0Lf+TVmzquHdjqHprBRZN4c/3k76QJEAlndHP7lYq53TLmGNKYnyWr+G8yaOY9fvdQaZcpdEFKAcqFurys48s2p9Y8UjaPuy+G7bk2E7PBAjEkkdnaGsc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QKKfnZxH; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dee601899c5so1053269276.3 for ; Fri, 10 May 2024 16:21:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715383318; x=1715988118; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OG7Mz+4GZ1p3m4frK9Am4WCrpGGx8QYnGhT78AdGhqU=; b=QKKfnZxHENimgz1bcJe5Lu+c2JiJJVdEP09qaseVPJBpmzCXU7sA5p/qq8SWfjlXwU tJ6nHQoDPNzgr2KQOcZRMgjUNnlDZn/kK1VXUHzI2/wywWs2fLws7okwkCm4PBwyOUrT 4+XznjTfjERaY0S0r9aEwlrSyAD4XePv75vlgCf3CbaSsShJ5wP72WyIDjNi/GZq0MaJ +ZmESCneN8KogwB4uj2LEy+KARiL4qh3pWS72KP/mm3GUSVKsFAjZfDXjrb9gLs08MKJ glzKSydHq/GvMLHpTpNXUWGofYzUr9VQS7sIB3YU/HzvW6eRkTxfSq+O6RC5AUe+XPpI cr2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715383318; x=1715988118; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OG7Mz+4GZ1p3m4frK9Am4WCrpGGx8QYnGhT78AdGhqU=; b=RvWQhiLFm0n1MvlmJZ3eR5jn5vs+LfSkUcE3fl43HdsNhqmtKj5oT+qYJGbjInnzyG 7nuHL2/16tqH1iGElV8M3gCcWmQouS9E4ZhMmmRuMbYuOg0d/uLRR4oO/BKGA3clQSaW 4UJudJTFOeR4yTd2VaDiz6X/kPiLoA+C+p6dI95zzNw8JVnkK9c7JA80UavFiYKuxa8S tMG0aXpkqoz2eRl23Dpd/1fahSE6InCoYHRM/McV8sdCOLhnemtIIGuqMLrzIhkgWctU Zf4V2/+IcVuPUfbuyH2vfmc2c5CTckc80ui+adK5rHS9kCPUeJSXsVMh7xAfFYhODdUf Chug== X-Forwarded-Encrypted: i=1; AJvYcCUYfJM+HXZJszAeZS1tVdQY+ao59kjTY6HrqS9+jHHGl3SlvrAn+8jrv/WvQC57T/dreGu85CfH7tMtRNrFy26K3bAM0BPgeuhLgspi X-Gm-Message-State: AOJu0YzL7LpZvCh4oDyz/kaFAHL0S8zLtB51fi/fx/BR8nK+EhZdgEpV zE67BnlOnHFLvfhxykkDMOYOOz3t8R3mLTI0Hx++J3Z4lb8N53UfW9TGbV8w6pW1LM0fS82hmXl PEIpzqmz3IyyFjIbnhLiFIw== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2c4:200:3428:1b4e:e75a:d31b]) (user=almasrymina job=sendgmr) by 2002:a05:6902:1004:b0:dc7:68b5:4f21 with SMTP id 3f1490d57ef6-dee4f508f29mr1080465276.9.1715383317903; Fri, 10 May 2024 16:21:57 -0700 (PDT) Date: Fri, 10 May 2024 16:21:23 -0700 In-Reply-To: <20240510232128.1105145-1-almasrymina@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240510232128.1105145-1-almasrymina@google.com> X-Mailer: git-send-email 2.45.0.118.g7fe29c98d7-goog Message-ID: <20240510232128.1105145-13-almasrymina@google.com> Subject: [PATCH net-next v9 12/14] net: add SO_DEVMEM_DONTNEED setsockopt to release RX frags From: Mina Almasry To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-alpha@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, sparclinux@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-arch@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Cc: Mina Almasry , Donald Hunter , Jakub Kicinski , "David S. Miller" , Eric Dumazet , Paolo Abeni , Jonathan Corbet , Richard Henderson , Ivan Kokshaysky , Matt Turner , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , Andreas Larsson , Jesper Dangaard Brouer , Ilias Apalodimas , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Arnd Bergmann , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Steffen Klassert , Herbert Xu , David Ahern , Willem de Bruijn , Shuah Khan , Sumit Semwal , "=?UTF-8?q?Christian=20K=C3=B6nig?=" , Pavel Begunkov , David Wei , Jason Gunthorpe , Yunsheng Lin , Shailend Chand , Harshitha Ramamurthy , Shakeel Butt , Jeroen de Borst , Praveen Kaligineedi , Willem de Bruijn , Kaiyuan Zhang Content-Type: text/plain; charset="UTF-8" Add an interface for the user to notify the kernel that it is done reading the devmem dmabuf frags returned as cmsg. The kernel will drop the reference on the frags to make them available for reuse. Signed-off-by: Willem de Bruijn Signed-off-by: Kaiyuan Zhang Signed-off-by: Mina Almasry --- v7: - Updated SO_DEVMEM_* uapi to use the next available entry (Arnd). v6: - Squash in locking optimizations from edumazet@google.com. With his changes we lock the xarray once per sock_devmem_dontneed operation rather than once per frag. Changes in v1: - devmemtoken -> dmabuf_token (David). - Use napi_pp_put_page() for refcounting (Yunsheng). - Fix build error with missing socket options on other asms. --- arch/alpha/include/uapi/asm/socket.h | 1 + arch/mips/include/uapi/asm/socket.h | 1 + arch/parisc/include/uapi/asm/socket.h | 1 + arch/sparc/include/uapi/asm/socket.h | 1 + include/uapi/asm-generic/socket.h | 1 + include/uapi/linux/uio.h | 4 ++ net/core/sock.c | 61 +++++++++++++++++++++++++++ 7 files changed, 70 insertions(+) diff --git a/arch/alpha/include/uapi/asm/socket.h b/arch/alpha/include/uapi/asm/socket.h index ef4656a41058a..251b73c5481ea 100644 --- a/arch/alpha/include/uapi/asm/socket.h +++ b/arch/alpha/include/uapi/asm/socket.h @@ -144,6 +144,7 @@ #define SCM_DEVMEM_LINEAR SO_DEVMEM_LINEAR #define SO_DEVMEM_DMABUF 79 #define SCM_DEVMEM_DMABUF SO_DEVMEM_DMABUF +#define SO_DEVMEM_DONTNEED 80 #if !defined(__KERNEL__) diff --git a/arch/mips/include/uapi/asm/socket.h b/arch/mips/include/uapi/asm/socket.h index 414807d55e33f..8ab7582291abf 100644 --- a/arch/mips/include/uapi/asm/socket.h +++ b/arch/mips/include/uapi/asm/socket.h @@ -155,6 +155,7 @@ #define SCM_DEVMEM_LINEAR SO_DEVMEM_LINEAR #define SO_DEVMEM_DMABUF 79 #define SCM_DEVMEM_DMABUF SO_DEVMEM_DMABUF +#define SO_DEVMEM_DONTNEED 80 #if !defined(__KERNEL__) diff --git a/arch/parisc/include/uapi/asm/socket.h b/arch/parisc/include/uapi/asm/socket.h index 2b817efd45444..38fc0b188e084 100644 --- a/arch/parisc/include/uapi/asm/socket.h +++ b/arch/parisc/include/uapi/asm/socket.h @@ -136,6 +136,7 @@ #define SCM_DEVMEM_LINEAR SO_DEVMEM_LINEAR #define SO_DEVMEM_DMABUF 79 #define SCM_DEVMEM_DMABUF SO_DEVMEM_DMABUF +#define SO_DEVMEM_DONTNEED 80 #if !defined(__KERNEL__) diff --git a/arch/sparc/include/uapi/asm/socket.h b/arch/sparc/include/uapi/asm/socket.h index 00248fc689773..57084ed2f3c4e 100644 --- a/arch/sparc/include/uapi/asm/socket.h +++ b/arch/sparc/include/uapi/asm/socket.h @@ -137,6 +137,7 @@ #define SCM_DEVMEM_LINEAR SO_DEVMEM_LINEAR #define SO_DEVMEM_DMABUF 0x0058 #define SCM_DEVMEM_DMABUF SO_DEVMEM_DMABUF +#define SO_DEVMEM_DONTNEED 0x0059 #if !defined(__KERNEL__) diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h index 25a2f5255f523..1acb77780f103 100644 --- a/include/uapi/asm-generic/socket.h +++ b/include/uapi/asm-generic/socket.h @@ -135,6 +135,7 @@ #define SO_PASSPIDFD 76 #define SO_PEERPIDFD 77 +#define SO_DEVMEM_DONTNEED 97 #define SO_DEVMEM_LINEAR 98 #define SCM_DEVMEM_LINEAR SO_DEVMEM_LINEAR #define SO_DEVMEM_DMABUF 99 diff --git a/include/uapi/linux/uio.h b/include/uapi/linux/uio.h index 3a22ddae376a2..d17f8fcd93ec9 100644 --- a/include/uapi/linux/uio.h +++ b/include/uapi/linux/uio.h @@ -33,6 +33,10 @@ struct dmabuf_cmsg { */ }; +struct dmabuf_token { + __u32 token_start; + __u32 token_count; +}; /* * UIO_MAXIOV shall be at least 16 1003.1g (5.4.1.1) */ diff --git a/net/core/sock.c b/net/core/sock.c index 8d6e638b5426d..2edb988259e8d 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -124,6 +124,7 @@ #include #include #include +#include #include #include #include @@ -1049,6 +1050,62 @@ static int sock_reserve_memory(struct sock *sk, int bytes) return 0; } +#ifdef CONFIG_PAGE_POOL +static noinline_for_stack int +sock_devmem_dontneed(struct sock *sk, sockptr_t optval, unsigned int optlen) +{ + unsigned int num_tokens, i, j, k, netmem_num = 0; + struct dmabuf_token *tokens; + netmem_ref netmems[16]; + int ret; + + if (sk->sk_type != SOCK_STREAM || sk->sk_protocol != IPPROTO_TCP) + return -EBADF; + + if (optlen % sizeof(struct dmabuf_token) || + optlen > sizeof(*tokens) * 128) + return -EINVAL; + + tokens = kvmalloc_array(128, sizeof(*tokens), GFP_KERNEL); + if (!tokens) + return -ENOMEM; + + num_tokens = optlen / sizeof(struct dmabuf_token); + if (copy_from_sockptr(tokens, optval, optlen)) + return -EFAULT; + + ret = 0; + + xa_lock_bh(&sk->sk_user_frags); + for (i = 0; i < num_tokens; i++) { + for (j = 0; j < tokens[i].token_count; j++) { + netmem_ref netmem = (__force netmem_ref)__xa_erase( + &sk->sk_user_frags, tokens[i].token_start + j); + + if (netmem && + !WARN_ON_ONCE(!netmem_is_net_iov(netmem))) { + netmems[netmem_num++] = netmem; + if (netmem_num == ARRAY_SIZE(netmems)) { + xa_unlock_bh(&sk->sk_user_frags); + for (k = 0; k < netmem_num; k++) + WARN_ON_ONCE(!napi_pp_put_page(netmems[k])); + netmem_num = 0; + xa_lock_bh(&sk->sk_user_frags); + } + ret++; + } + } + } + + xa_unlock_bh(&sk->sk_user_frags); + for (k = 0; k < netmem_num; k++) + WARN_ON_ONCE(!napi_pp_put_page(netmems[k])); + + kvfree(tokens); + return ret; +} +#endif + void sockopt_lock_sock(struct sock *sk) { /* When current->bpf_ctx is set, the setsockopt is called from @@ -1200,6 +1257,10 @@ int sk_setsockopt(struct sock *sk, int level, int optname, ret = -EOPNOTSUPP; return ret; } +#ifdef CONFIG_PAGE_POOL + case SO_DEVMEM_DONTNEED: + return sock_devmem_dontneed(sk, optval, optlen); +#endif } sockopt_lock_sock(sk); -- 2.45.0.118.g7fe29c98d7-goog