Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp735364lqt; Thu, 6 Jun 2024 17:56:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV6mJ00EpH/KYn3/VNMI6l8rYn5g60Vy+ffqM/TKENw36JbngpDIDGIH81Wx3is1NIEry8lQ16XsOZKgUByXb2hSgkjdTK0WsOt+aOaZg== X-Google-Smtp-Source: AGHT+IEFfXbSYufFRhdu6v2EEuIrUgWHLLLsC/I72gX2SyP29xiIVoxhOy/sHQJuQ7jgiTx6k8Y3 X-Received: by 2002:a05:6a20:3c8e:b0:1af:dc83:75bc with SMTP id adf61e73a8af0-1b2f969de1bmr1331723637.3.1717721806714; Thu, 06 Jun 2024 17:56:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717721806; cv=pass; d=google.com; s=arc-20160816; b=KZWZMYC6U65G+y5B/1B8W8fUT3dTzGis5i8rl9MiOouYrS/RJ0715oHpQf9WfW1D6J JiovZbAn+S5YZKA/4Zq+IUaSt5tfbh2PEAXTk5cGe5HETuMWmhF5xq7s+fe522WybqLN XAchHxBREaBiGox88ixaVeLGL/ES7qhatnGF2HmuF9wjcPZea8k+0DMvgcdmZ0mgQFHb gI0wHFTzo8GQs+uPQ4rJhEW8psW5HtcLTuLSdxrlMWNSTBw3XdLT0rZB/dLSmkI6S8zh WnEwN5yo2oYCvcrrPnD0JzBOD1cQLwhgknnoCmID3SMj8u4WRx5zQQEeb8fVAh6UMzX7 qxNw== 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=lhomENoNfMaJWS7XUI4qqmt9JkG0zt+Z7c540SVLWSw=; fh=S34TOCuNqakO+lNq6Fk8zxGnhUjOgfTuUz+qG5SoC6g=; b=UBwdct7cTacPKiyMrvXKvUs1IgdQapdbnpAle5mPjax3pWLRHjcTd1T2NjKvS1XwYB yzlXczLNoK6CqedJPoG/cH58TfZiQ/biZt8fuZFgoL0sv1SK5iyj9NtJenLYHmF6aqwD HLQqOAT9d1Az7QXKhH4dnQhnwjbTrEAn3ubZsz95cEkJbnCx/4go3bqcc5ZTejysT+I/ eCOyuGFQJpxov1f7UMNuedVpZ/DB+XbkJ4PT9+ynIqQ42/JjfkocEepzGbLom+HAEOEQ me/ZDVqb/kpPgwfCZ+XmEMuxowF5IRpRLb+r1MoqGAlEPpC8q/n3091zONnM5pouuh1S 9G3g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=jjn90raK; 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-205203-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205203-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. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2c29c227baasi1985880a91.78.2024.06.06.17.56.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 17:56:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205203-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=jjn90raK; 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-205203-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205203-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 7B4CD2879BE for ; Fri, 7 Jun 2024 00:56:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3E09B14F11C; Fri, 7 Jun 2024 00:52:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jjn90raK" 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 B3CC713D521 for ; Fri, 7 Jun 2024 00:51:55 +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=1717721522; cv=none; b=qT52asP719YGqpEN+/Ybm5FHWFcTr85LZJa/6hUkv/+Cv3O4OE59XY8ak0jP/V3ySObSZ8qruV3B0lhveGfpwv8HjyVXBA0DdRZPaYweH2d5IX600nLpc/dqvpyFfo3yStK/4UCqa87Ojvkgcer1o9qu/JZGwpubgrLPtA9SvtA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717721522; c=relaxed/simple; bh=f9VsrjuCX6q/T5RS3zWwoZ3HtwjQAo1nlQZyi7PhpT8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kYzSLA7KfzJpF42TFbJ+VXW5pjtLoc9HRgGw4WE/3kjCmGM7QVoOWBcFlcYcujew4/oOoaZmEGtmpJIyiJKORxwHrk6utDCLTaDkMQR54RsjAUNmoR2fZDawEmLTWxronmhxT9AplJVLekn7VB7JJlpw40t3WBc1ZX7DxPrwYos= 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=jjn90raK; 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-dfa74b3e6cbso2775479276.2 for ; Thu, 06 Jun 2024 17:51:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717721513; x=1718326313; 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=lhomENoNfMaJWS7XUI4qqmt9JkG0zt+Z7c540SVLWSw=; b=jjn90raKqtliehPqv5N54uK/P+H3ESguztiVBaizAE+BtqsehsgX5QeNHSPfP5HgSq oVMFiObqsZcL7Ga3e0Tt/cK4SIdLXGIqkRq6X+NR1MQTvSU/mZvZi2Gau4KCiyScaXSy MLO89h1Lvn7gMNnR5g5O1vi0aQ8i25z+cT13+sChRnuJBNquffp0R2gPcRzA3Qcha48y dGphhH97Qc7xQKWLyv4tBi58exW7GR9Rbt0031eztPAu6NbVVVPGqvtFQZ+oyQ9gN8AS 6BdxPLYr08ydcKWUh1YKaT32FuhzrtOfkbXBdTSrcLsCI3QTOYGQwFuliLSvdAtSRibt /FFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717721513; x=1718326313; 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=lhomENoNfMaJWS7XUI4qqmt9JkG0zt+Z7c540SVLWSw=; b=HIBxhZyzEwrXFkd5/0adQf6TIcX9KvkpSO3+8qQ2oXkGkNwA3M+4TI7hxPQAJrTi5x Dr3TsueejVarUOp/y6jWpLWXGKHCtpvxeOKEGNVWS5fSiVn+Q45sWofibP4p/at/WAQq fN2MhD47J3e+m6JSvbNx2n3nV5JPB8XNZ4oWqJmRGPP6qfwpeiEPIdrXGavLP9KsnR/G 8XXqHNebXmXGiNP4RK4iOJU+QC6d90ha/ym9cx7TiKgw1CDp5+CvEx6unGvKjLVgULvV qeqX54y7zaouSsJWM5HbUNGpPQwd5ZapZf5zt2JWmCU5TsAn/YUaVG1Ny4ZLOCzxowdn jM3g== X-Forwarded-Encrypted: i=1; AJvYcCXLS9gCxqTQc0hOK8JoaQXe/ncXh8bXGMWQUgL5AEJXW2UnnZaknPaiC5f/B2d8N7xby+xyxt5EQp9c3gfjpyMMg3SNm6+QqP9qVcu8 X-Gm-Message-State: AOJu0Yxlxs8pe3eEQjwUWv10rXa4KWxRBU611kzR0eYuJYsY2Y9+VL65 xGzOdLCwWYiop4XLRfcpocPn+Ws7tOyKDy4E4LdC2HqR7P9JX1LOT2irY/6puGugmd7gQinY53Q b6APFol7Qa0Xdsl78eiP2hg== X-Received: from almasrymina.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:4bc5]) (user=almasrymina job=sendgmr) by 2002:a25:df01:0:b0:df4:db4d:5db2 with SMTP id 3f1490d57ef6-dfaf6593c78mr188900276.7.1717721512592; Thu, 06 Jun 2024 17:51:52 -0700 (PDT) Date: Fri, 7 Jun 2024 00:51:21 +0000 In-Reply-To: <20240607005127.3078656-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: <20240607005127.3078656-1-almasrymina@google.com> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240607005127.3078656-12-almasrymina@google.com> Subject: [PATCH net-next v11 11/13] 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?=" , Bagas Sanjaya , Christoph Hellwig , Nikolay Aleksandrov , 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 --- v10: - Fix leak of tokens (Nikolay). 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 69baddcfbd8c1..41586277c8dbb 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 = 0; + + 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)) { + kvfree(tokens); + return -EFAULT; + } + + 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 @@ -1211,6 +1268,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.2.505.gda0bf45e8d-goog