From patchwork Fri Dec 8 00:52:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 13484580 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EDFC6C10F04 for ; Fri, 8 Dec 2023 00:53:35 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 04DCE10E9C6; Fri, 8 Dec 2023 00:53:33 +0000 (UTC) Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0BD9F10E9BA for ; Fri, 8 Dec 2023 00:53:24 +0000 (UTC) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d7e7e10231so9954507b3.1 for ; Thu, 07 Dec 2023 16:53:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701996804; x=1702601604; darn=lists.freedesktop.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=wUh6+R8PyJI5UjQ2PsAiU6Bt8ES298Z5QR8+6xQcmYE=; b=iShsZoKii68ShZ/FlLsBzDuUP6+SPIiwDG87b3Opc58MxbwydD4NrbniFZgS/8SHsK 0aebkeSGGJcKxUpkjIVHS+zYChxDjHL4DOZQXQlbjp65/F2BpqDNdYQeTtUitl0KB7QT 3+0gSoKVBTwitLN/DAmG8QKA6LM6UVQ8C3T0s2v7toO3YoauoHEcAtVGQac0BlNRXUFk eDzQU9QpmmO1JL4Iy1H0QLbDFiDXCFmZCwuKXvJ2DcgjLTXiyFlmWdiycxKZL2O6GE9L I0vTvaZxOH20/NIN3uUBgjVQmqb6zS4RxwzMMg62bUIOvclDHoGMyRwCD7AM2cTsBGzq CZ/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701996804; x=1702601604; 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=wUh6+R8PyJI5UjQ2PsAiU6Bt8ES298Z5QR8+6xQcmYE=; b=W936FulQO0gIaF8VMS3OjodtcfZ5/NfqDqp/rRncFiA5L+KHZCc8O0cGhSqYMEa6G8 SnWSHa9nCoe4ftn/AQTp9OC03AIB8YvtfdtBkbqICXE4zSN7DhSzSCTQqqQhDSmd3T+9 50FFXJCBZAuwY/XLj1YxHyHwcvgxQTBbXSP1RplJ9t+w8GBq5NzXHuSfwpMc6SMRB8fn ZF+i5cTyb9M3DGZ/dxr6KPd7p3VU6DR7i+sqvHKM8NEV+QmR70H7e7MiBRCjY+kcSV6n sd4uQDBVR2GYloz9yO+/EQkTv9+SFNjlSNvK1CID7lWs07uSQcbHRnGQ9e87nufBXIHD 1UeA== X-Gm-Message-State: AOJu0YyC9v64Clll+6QGGTCyLa4Sujjo+umoPSmzKhwgWxuoMucqII8e zXPxZE3BJsprGEo/zleDdNKI+D+pxUSNKTCSnQ== X-Google-Smtp-Source: AGHT+IHVuzVckyE/E0TPu0QHBrVQTT3kDuCWzit0Enzhqk5IFqRCJ3FkAoDBVqkfvr2rYGREMqR0vzwsTFGcsut5qw== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2c4:200:f1cf:c733:235b:9fff]) (user=almasrymina job=sendgmr) by 2002:a25:9d10:0:b0:ca3:3341:6315 with SMTP id i16-20020a259d10000000b00ca333416315mr2159ybp.0.1701996804012; Thu, 07 Dec 2023 16:53:24 -0800 (PST) Date: Thu, 7 Dec 2023 16:52:45 -0800 In-Reply-To: <20231208005250.2910004-1-almasrymina@google.com> Mime-Version: 1.0 References: <20231208005250.2910004-1-almasrymina@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231208005250.2910004-15-almasrymina@google.com> Subject: [net-next v1 14/16] net: add SO_DEVMEM_DONTNEED setsockopt to release RX frags From: Mina Almasry To: Shailend Chand , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org, bpf@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mina Almasry , Willem de Bruijn , Kaiyuan Zhang , Jeroen de Borst , Jesper Dangaard Brouer , Arnd Bergmann , Jonathan Corbet , David Ahern , Ilias Apalodimas , Yunsheng Lin , Sumit Semwal , Eric Dumazet , Shakeel Butt , Willem de Bruijn , Harshitha Ramamurthy , Praveen Kaligineedi , Jakub Kicinski , =?utf-8?q?Christian_K=C3=B6nig?= , Paolo Abeni , Shuah Khan , "David S. Miller" Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 re-use. Signed-off-by: Willem de Bruijn Signed-off-by: Kaiyuan Zhang Signed-off-by: Mina Almasry --- Changes in v1: - devmemtoken -> dmabuf_token (David). - Use napi_pp_put_page() for refcounting (Yunsheng). --- include/uapi/asm-generic/socket.h | 1 + include/uapi/linux/uio.h | 4 ++++ net/core/sock.c | 38 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h index 25a2f5255f52..1acb77780f10 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 ad92e37699da..65f33178a601 100644 --- a/include/uapi/linux/uio.h +++ b/include/uapi/linux/uio.h @@ -30,6 +30,10 @@ struct dmabuf_cmsg { __u32 dmabuf_id; /* dmabuf id this frag belongs to. */ }; +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 fef349dd72fa..521bdc4ff260 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1051,6 +1051,41 @@ static int sock_reserve_memory(struct sock *sk, int bytes) return 0; } +static noinline_for_stack int +sock_devmem_dontneed(struct sock *sk, sockptr_t optval, unsigned int optlen) +{ + struct dmabuf_token tokens[128]; + unsigned int num_tokens, i, j; + int ret; + + if (sk->sk_type != SOCK_STREAM || sk->sk_protocol != IPPROTO_TCP) + return -EBADF; + + if (optlen % sizeof(struct dmabuf_token) || optlen > sizeof(tokens)) + return -EINVAL; + + num_tokens = optlen / sizeof(struct dmabuf_token); + if (copy_from_sockptr(tokens, optval, optlen)) + return -EFAULT; + + ret = 0; + for (i = 0; i < num_tokens; i++) { + for (j = 0; j < tokens[i].token_count; j++) { + struct page *page = xa_erase(&sk->sk_user_pages, + tokens[i].token_start + j); + + if (page) { + if (WARN_ON_ONCE(!napi_pp_put_page(page, + false))) + page_pool_page_put_many(page, 1); + ret++; + } + } + } + + return ret; +} + void sockopt_lock_sock(struct sock *sk) { /* When current->bpf_ctx is set, the setsockopt is called from @@ -1538,6 +1573,9 @@ int sk_setsockopt(struct sock *sk, int level, int optname, break; } + case SO_DEVMEM_DONTNEED: + ret = sock_devmem_dontneed(sk, optval, optlen); + break; default: ret = -ENOPROTOOPT; break;