Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp25270lqi; Wed, 6 Mar 2024 09:05:41 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWSddy72850pma7HpjiUIpb0DvjgExvCEI87GGJhBXgWWaX6P0F+BpF4S4xY1idDKGGkyy+L96vPUHBTsBWC2wu+xOyz08L9sFgIycmVg== X-Google-Smtp-Source: AGHT+IHaHRyfvjYyLRnV1QfNxYV8NSsP6iMAoR8uKiGBEKP7jdlM9uuLgsTEih1g+nlQ1BbaLdMi X-Received: by 2002:a05:6602:4245:b0:7c7:cd35:4082 with SMTP id cc5-20020a056602424500b007c7cd354082mr17451471iob.9.1709744740807; Wed, 06 Mar 2024 09:05:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709744740; cv=pass; d=google.com; s=arc-20160816; b=jJpCCyyIy05jIY7tOB3hNSa69WFdeZLN1WJcAs1Jy7VRDYZXT4QtHl3tPI8bRfzJUb hSX5vQuHoLyPDnGXqeTl6fRyTvMIXGsQK0jpb6OYXSv18gLC4J9Gx3Ryjuy1/DEDAUwb n+YTfeomQ76S9UuwcNohnfueH42dOPoq36A6qjJjvmKfVz871DdzHhO868mJLQg50cuf Mog/UrisYOyOG5elzTauCjJrFBKjubneCfXfhyvu+ML0h0/vtoUrGsD2oE7IQVNOtg2A J0iZyN5n/x90yD8GKY43hHu0PdAr6xT5sOTLxYy8IiDFgMBL6RulWHVEN87XdwaKkbI9 WtTw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=A1xSBjsofT/9pZS7Yx9G3XGpR9XoFLam+fQU7TnybVs=; fh=xKeFNMJruXO4LHhdqEpwP/4OIgdQXuqWrmM2+MuravA=; b=RqatgVhhleifgN00d2OfFcCb8QBYkFEb3bCIncaYdWKL8JKUjSUhPG7bkh+4qKwkiJ MxqKxCaAD2A734/MqXDcbWoDoQjdEgT7Hpz5unc89VXXYmoxqV4lnorHACRqBSh1UpYn p31MW4Q/d817yYPkvqCw6SXQl66K9QIylH+RamVbJv7VZjEqlmx4LcD4lVxNcixMb7X8 2y97RDSXj9dc6LwVG0uw2OPsSVP7vL/IVBjUnpNBloJ7Jx1woRP34W7QyfT13CaxTsiK PeuzkFmLf8afzxtr2xaSgH1wY2abgp4PYbpMWtfRnSQsKZHAne2XbyjKligEArrdvBYH Kw6Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=YGPSFtgj; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-94310-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94310-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 n3-20020a056638264300b00472c561c132si1346044jat.57.2024.03.06.09.05.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 09:05:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-94310-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=YGPSFtgj; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-94310-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94310-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 4CAB1288823 for ; Wed, 6 Mar 2024 17:05:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 01B9813958C; Wed, 6 Mar 2024 17:05:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YGPSFtgj" Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (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 F0902132C0C for ; Wed, 6 Mar 2024 17:05:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709744711; cv=none; b=pj8px3c4h3YCtT+KaPyFysqzHYpJxlcNMa9ZHIyAMiryqyzfkVLCJfyYhbTH7vf2uovmSJgJT2uF92uBku5MwqyKD3Y4uIHFIblsPadIbIqDU5YThEqbT5OhbeCYdHB3JkQA/GPDtiKF+L9/cii/4NiFYcdUc3EDrt6v3qVaRak= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709744711; c=relaxed/simple; bh=f1WmBqp+S6Tcw7A8Oq9HJf0VJDtBrJ0ATA6lz8ebmoc=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=VsF4Iyjb+MQfUTBVWsEB9X78dU/tUSC7ehSwQ81fMFAVhsoFDMEDTRHINkOliybf/gy6o6VxOAuCcT89Ohdahjx0Jf/QGrTStyoQDJrEcE9xLmXBMX1IzLAy0Hxuu/Oycwzm+7x0/9h9JOv8t2t21FRHIIFIVZ7xrEzMag7Iauo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YGPSFtgj; arc=none smtp.client-ip=209.85.218.47 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=google.com Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a44628725e3so2304866b.0 for ; Wed, 06 Mar 2024 09:05:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709744707; x=1710349507; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=A1xSBjsofT/9pZS7Yx9G3XGpR9XoFLam+fQU7TnybVs=; b=YGPSFtgjiZCZ2/fuo+t9hwJeE6OWJF42MA00e/1bbwxL4sNb2/4OygmNqZk6G1kDV3 SmmGm7Z2FiRELYmj/mWtsxnVBpHGTCMFWS1a0BaKZalgzAxl0ni/yS8mqyV38GnbNBRr yb5Mb8zHgmrFvODzq1rVBfG+fJ0WfZROjkaIjECLEAnstyK2x++QDjrxpzz75DILwvqm D5Cn45O4ZK52+wqd/abE/c7vgFqhD1Zn4HWJNk7RFSxd2bjYfoz+Yz3CXo5qaYUpaxL9 2HSwptCAvjycaff/WigX2F0Ns/pl/6wmEn3v/qS4GPz1tpIuvc7rj5x9lhqJqzb8U0GW nGMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709744707; x=1710349507; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=A1xSBjsofT/9pZS7Yx9G3XGpR9XoFLam+fQU7TnybVs=; b=W6AlgmwmijyAuVex2ESkbqz4rvxscm4PjVOxZRzYZomOGAJcdfVDym9dlIb9MaguBu 1lu4/uz9l7VQUzJVAQ9hvtQ6NHMidZRFxJcX7LGhd1rCVJmFby/D/J+Jjb+nPkcOKt74 qwey5qt5emK0eiBrRBUV5PAK7oZsUnHECm74aPmgtWEwtVVmp2DAouqPzznT3P/1gLbw DiRwQpt9B0ZsrgPvMCqTIa7e51Mj0sQ4aEy1YtRmJ7h9OKBu60Hqd9euAcNI6XVNMtyy BWvm+IBmsv/0enFvCsHyEjpDB2rt14WTy4MQZD2mNp5kiec90WYhjHCNcOIkvxj2ge/8 s/Mg== X-Forwarded-Encrypted: i=1; AJvYcCWg5oIMR1p4PsM+79q61XgD60b8+X0AxbHH8kNw/yl459AWlj1NNkI2fcIv83gj9dOe6xydLMw9RbsE3GmGpS713+agcmrXUntC5Nmi X-Gm-Message-State: AOJu0Yw4nXP0JBYdg/JsSdwaYRcn8VGfSbJcegiW2NFEbe/b9NKNQh+B 6uJLcVAiHBQwgW0rW3LluByD65ADiNnIf3mI2rXkOgZIv/vD9JikqbHgfZcsv/1O5A8Iase7Lqn 0n8m/w32V5yQpQ0M0rCKlye4Hhg0wPrCj6iDr X-Received: by 2002:a17:906:b00b:b0:a44:c583:dfc8 with SMTP id v11-20020a170906b00b00b00a44c583dfc8mr9370474ejy.48.1709744706904; Wed, 06 Mar 2024 09:05:06 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240305020153.2787423-1-almasrymina@google.com> <20240305020153.2787423-3-almasrymina@google.com> <1b57dac2-4b04-4bec-b2d7-d0edb4fcabbc@davidwei.uk> <417f293a-848e-4eb2-b690-c8696079b452@gmail.com> In-Reply-To: <417f293a-848e-4eb2-b690-c8696079b452@gmail.com> From: Mina Almasry Date: Wed, 6 Mar 2024 09:04:54 -0800 Message-ID: Subject: Re: [RFC PATCH net-next v6 02/15] net: page_pool: create hooks for custom page providers To: Pavel Begunkov Cc: David Wei , 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, "David S. Miller" , Eric Dumazet , Jakub Kicinski , 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 , David Ahern , Willem de Bruijn , Shuah Khan , Sumit Semwal , =?UTF-8?Q?Christian_K=C3=B6nig?= , Jason Gunthorpe , Yunsheng Lin , Shailend Chand , Harshitha Ramamurthy , Jeroen de Borst , Praveen Kaligineedi , shakeel.butt@linux.dev Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Mar 6, 2024 at 6:30=E2=80=AFAM Pavel Begunkov wrote: > > On 3/5/24 22:36, Mina Almasry wrote: > > On Tue, Mar 5, 2024 at 1:55=E2=80=AFPM David Wei wrote= : > >> > >> On 2024-03-04 18:01, Mina Almasry wrote: > >>> +struct memory_provider_ops { > >>> + int (*init)(struct page_pool *pool); > >>> + void (*destroy)(struct page_pool *pool); > >>> + struct page *(*alloc_pages)(struct page_pool *pool, gfp_t gfp); > >>> + bool (*release_page)(struct page_pool *pool, struct page *page)= ; > >> > >> For ZC Rx we added a scrub() function to memory_provider_ops that is > >> called from page_pool_scrub(). Does TCP devmem not custom behaviour > >> waiting for all netmem_refs to return before destroying the page pool? > >> What happens if e.g. application crashes? > > > > (sorry for the long reply, but he refcounting is pretty complicated to > > explain and I feel like we need to agree on how things currently work) > > > > Yeah, the addition of the page_pool_scrub() function is a bit of a > > head scratcher for me. Here is how the (complicated) refcounting works > > for devmem TCP (assuming the driver is not doing its own recycling > > logic which complicates things further): > > > > 1. When a netmem_ref is allocated by the page_pool (from dmabuf or > > page), the netmem_get_pp_ref_count_ref()=3D=3D1 and belongs to the page > > pool as long as the netmem is waiting in the pool for driver > > allocation. > > > > 2. When a netmem is allocated by the driver, no refcounting is > > changed, but the ownership of the netmem_get_pp_ref_count_ref() is > > implicitly transferred from the page pool to the driver. i.e. the ref > > now belongs to the driver until an skb is formed. > > > > 3. When the driver forms an skb using skb_rx_add_frag_netmem(), no > > refcounting is changed, but the ownership of the > > netmem_get_pp_ref_count_ref() is transferred from the driver to the > > TCP stack. > > > > 4. When the TCP stack hands the skb to the application, the TCP stack > > obtains an additional refcount, so netmem_get_pp_ref_count_ref()=3D=3D2= , > > and frees the skb using skb_frag_unref(), which drops the > > netmem_get_pp_ref_count_ref()=3D=3D1. > > > > 5. When the user is done with the skb, the user calls the > > DEVMEM_DONTNEED setsockopt which calls napi_pp_put_netmem() which > > recycles the netmem back to the page pool. This doesn't modify any > > refcounting, but the refcount ownership transfers from the userspace > > back to the page pool, and we're back at step 1. > > > > So all in all netmem can belong either to (a) the page pool, or (b) > > the driver, or (c) the TCP stack, or (d) the application depending on > > where exactly it is in the RX path. > > > > When an application running devmem TCP crashes, the netmem that belong > > to the page pool or driver are not touched, because the page pool is > > not tied to the application in our case really. However, the TCP stack > > notices the devmem socket of the application close, and when it does, > > the TCP stack will: > > > > 1. Free all the skbs in the sockets receive queue. This is not custom > > behavior for devmem TCP, it's just standard for TCP to free all skbs > > waiting to be received by the application. > > 2. The TCP stack will free references that belong to the application. > > Since the application crashed, it will not call the DEVMEM_DONTNEED > > setsockopt, so we need to free those on behalf of the application. > > This is done in this diff: > > > > @@ -2498,6 +2498,15 @@ static void tcp_md5sig_info_free_rcu(struct > > rcu_head *head) > > void tcp_v4_destroy_sock(struct sock *sk) > > { > > struct tcp_sock *tp =3D tcp_sk(sk); > > + __maybe_unused unsigned long index; > > + __maybe_unused void *netmem; > > + > > +#ifdef CONFIG_PAGE_POOL > > + xa_for_each(&sk->sk_user_frags, index, netmem) > > + WARN_ON_ONCE(!napi_pp_put_page((__force netmem_ref)netmem, false)); > > +#endif > > + > > + xa_destroy(&sk->sk_user_frags); > > > > trace_tcp_destroy_sock(sk); > > > > To be honest, I think it makes sense for the TCP stack to be > > responsible for putting the references that belong to it and the > > application. To me, it does not make much sense for the page pool to > > be responsible for putting the reference that belongs to the TCP stack > > or driver via a page_pool_scrub() function, as those references do not > > belong to the page pool really. I'm not sure why there is a diff > > between our use cases here because I'm not an io_uring expert. Why do > > you need to scrub all the references on page pool destruction? Don't > > these belong to non-page pool components like io_uring stack or TCP > > stack ol otherwise? > > That one is about cleaning buffers that are in b/w 4 and 5, i.e. > owned by the user, which devmem does at sock destruction. io_uring > could get by without scrub, dropping user refs while unregistering > ifq, but then it'd need to wait for all requests to finish so there > is no step 4 in the meantime. Might change, can be useful, but it > was much easier to hook into the pp release loop. > > Another concern is who and when can reset ifq / kill pp outside > of io_uring/devmem. I assume it can happen on a whim, which is > hard to handle gracefully. > If this is about dropping application refs in step 4 & step 5, then from devmem TCP perspective it must be done on socket close & skb freeing AFAIU, and not delayed until page_pool destruction. Think about a stupid or malicious user that does something like: 1. Set up dmabuf binding using netlink api. 2. While (100000): 3. create devmem TCP socket. 4. receive some devmem data on TCP socket. 5. close TCP socket without calling DEVMEM_DONTNEED. 6. clean up dmabuf binding using netlink api. In this case, we need to drop the references in step 5 when the socket is destroyed, so the memory is freed to the page pool and available for the next socket in step 3. We cannot delay the freeing until step 6 when the rx queue is recreated and the page pool is destroyed, otherwise the net_iovs would leak in the loop and eventually the NIC would fail to find available memory. The same bug would be reproducible with io_uring unless you're creating a new page pool for each new io_uring socket equivalent. But even outside of this, I think it's a bit semantically off to ask the page_pool to drop references that belong to the application IMO, because those references are not the page_pool's. > -- > Pavel Begunkov --=20 Thanks, Mina