Message ID | 20231214020530.2267499-3-almasrymina@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8267663dys; Wed, 13 Dec 2023 18:06:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IF2oU2K823wCBjwyAnucpPQFRGY+l8GQhahzbiOeGL1QBkPrFcMbGxD+8BU4BYdnuGtKuby X-Received: by 2002:a05:6871:14b:b0:203:1efd:44d3 with SMTP id z11-20020a056871014b00b002031efd44d3mr2610171oab.79.1702519588663; Wed, 13 Dec 2023 18:06:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702519588; cv=none; d=google.com; s=arc-20160816; b=kSVOpnuM6JQw0ZNVa6Chvc2uiE4CJulN1IziguoSioezcc7988jGanSbeLM5Bnsd6F Skho4UX5ZPLFEnNkWfKR1wcZtrLYjTtkXfQCUAGIqBpr5lKQa+gIdYPrLV/WbNxf3472 KRYItDCNRtsLquRS6kYv8P8tTflpW45CDKhqQYhR3UzYrinQGtlPtFpUYKWpHzx6mmo9 qcj83ilJmyX1m601xPXzVrniGI2A3Ts8b0FH5vTlicquy6UPwVD8/b23Y7C6So+d8n3e 5jMBj29RCGXf4pjafI8zHXZRkSCRbtofroNFlJt1LB39B/qXYGS9P+6strSLQiaYGV5y MsNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=KzTXKjA5KWsaYAjqKhDbOIpqWzWFT9AS368JXZ2MRs8=; fh=2cTpkOQdVNm4Kv9XaiKb49A4tmrQwCTD9dCd1XGCiM8=; b=dBNNSPbTJxC5KrVS90vJnsNsm24WdPsdgnkRZeN8Pl/DSMvw8O6MmPweYCH8Okym5i 4NaIENYf9x4d7IHrCXAcyn/8dRwi78Z0bEDLZDDQ9VDlNUVlEwwQB0SMyYuG03VtWZgH 3ycdKbKP8rccLX2h87dHqoLet2R8GYveo9kX7D67MNA18kS8WjIQt2i+YeNMyv9G+IK7 +v9h3svS9KDaRnXxHmUUOxefnrcSDXMWNHIj8/R0x83QCMtc1SL6n7/d2qiWnH9K8TE8 fAQ2HQL45Q7yF3kQtZ1hgrGM3IjBKotBvUj1sXU/Fev62hw4biK4SgqRlQ+VfEqKjHto 3CvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="dvhIQC/v"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id by12-20020a056a02058c00b005c66c2d0a5csi10830831pgb.484.2023.12.13.18.06.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 18:06:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="dvhIQC/v"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 95A1580C2563; Wed, 13 Dec 2023 18:06:25 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235353AbjLNCFq (ORCPT <rfc822;dexuan.linux@gmail.com> + 99 others); Wed, 13 Dec 2023 21:05:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229739AbjLNCFe (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 13 Dec 2023 21:05:34 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B5F1F4 for <linux-kernel@vger.kernel.org>; Wed, 13 Dec 2023 18:05:40 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-dbc4f389835so6643954276.2 for <linux-kernel@vger.kernel.org>; Wed, 13 Dec 2023 18:05:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702519539; x=1703124339; 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=KzTXKjA5KWsaYAjqKhDbOIpqWzWFT9AS368JXZ2MRs8=; b=dvhIQC/v1AHHBiu9xhrWi4kRmfpWMCzJe4P5d7bNTHkaEX/Qw7QPHgnupS2yzV1Ca6 65rtV+JwRqEnrclvbw34QOSyjJxt76awp7bgZTZmjNT74uxiopeWERjDxZEksP540jFX GCA+9GUSCr7ZRwCX03Nd1GUYiKKLj2IIZHiQJP6GlNIQMBVdSjUWp+HzDF8n4byMRohe l7IMSOTHQSFM5q7BoHS+SwbLYc9SzcANV2njO7MSHovSofSXzA8sQW6gut83NShKcioj g4OHMyif42YSMQysGddkTrRUGw0RA4XT2HZQR1i4ZAObE5pcSziEfhTUUOU7xfns0eCl 4JdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702519539; x=1703124339; 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=KzTXKjA5KWsaYAjqKhDbOIpqWzWFT9AS368JXZ2MRs8=; b=v7nreXq3fMMIKPv0UdZwnZqvJhMdq35bLzQ/iOdBUbzGAg7yuKpELZB3/AHYK/fHw0 Y3DHtZ3siItzccNT8fHq8fiX9W5A3q+c0RWjFnpxvF2q49oW2WN6FWggXOM5QCWjwVoj G5/SY7pYstno5QRAC0U2Ro6LOb4bjiP84U8TNC2nzKTjgOr7THggOJ3UUq86hgnV1Xed NxEwmytzMXON8y7xIYNQW+zcoEPlMJxkC1NDm6e6xYTCcSsEtKadit5U/NzlpdSiKSUO e+BMCoiGln5m1kMKueLCB3vnvGDp7IL7FToDpZK2wcmCiJhPT+aW7QrlYEFKO5iv8svL OFIQ== X-Gm-Message-State: AOJu0YzFXi3Ooa2zqCq7h1+KhbJ6s0xKGiOlmbdfacQD9Ju1Pmwnkdfn LgP6KBlWXGwU5J5zKAiBnNns8/LcF6CsevK8e42J4i665UATDVGVnXwwlxw2nU0vP7/3sbO2p/u +paWHrp5kMqXitvtHRmh2DALmoSGj+eHrYf5ExN7nDjZlnSntxHca+bT76uq6gPy/mhye3GYE8P RkdMlW9SM= X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2c4:200:d31b:c1a:fb6a:2488]) (user=almasrymina job=sendgmr) by 2002:a25:abeb:0:b0:db4:5eed:8907 with SMTP id v98-20020a25abeb000000b00db45eed8907mr77688ybi.8.1702519538827; Wed, 13 Dec 2023 18:05:38 -0800 (PST) Date: Wed, 13 Dec 2023 18:05:25 -0800 In-Reply-To: <20231214020530.2267499-1-almasrymina@google.com> Mime-Version: 1.0 References: <20231214020530.2267499-1-almasrymina@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231214020530.2267499-3-almasrymina@google.com> Subject: [RFC PATCH net-next v1 2/4] net: introduce abstraction for network memory From: Mina Almasry <almasrymina@google.com> To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Mina Almasry <almasrymina@google.com>, Thomas Gleixner <tglx@linutronix.de>, Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>, Dave Hansen <dave.hansen@linux.intel.com>, x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, "Rafael J. Wysocki" <rafael@kernel.org>, Sumit Semwal <sumit.semwal@linaro.org>, " =?utf-8?q?Christian_K=C3=B6nig?= " <christian.koenig@amd.com>, Michael Chan <michael.chan@broadcom.com>, "David S. Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@google.com>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Alexei Starovoitov <ast@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, Jesper Dangaard Brouer <hawk@kernel.org>, John Fastabend <john.fastabend@gmail.com>, Wei Fang <wei.fang@nxp.com>, Shenwei Wang <shenwei.wang@nxp.com>, Clark Wang <xiaoning.wang@nxp.com>, NXP Linux Team <linux-imx@nxp.com>, Jeroen de Borst <jeroendb@google.com>, Praveen Kaligineedi <pkaligineedi@google.com>, Shailend Chand <shailend@google.com>, Yisen Zhuang <yisen.zhuang@huawei.com>, Salil Mehta <salil.mehta@huawei.com>, Jesse Brandeburg <jesse.brandeburg@intel.com>, Tony Nguyen <anthony.l.nguyen@intel.com>, Thomas Petazzoni <thomas.petazzoni@bootlin.com>, Marcin Wojtas <mw@semihalf.com>, Russell King <linux@armlinux.org.uk>, Sunil Goutham <sgoutham@marvell.com>, Geetha sowjanya <gakula@marvell.com>, Subbaraya Sundeep <sbhatta@marvell.com>, hariprasad <hkelam@marvell.com>, Felix Fietkau <nbd@nbd.name>, John Crispin <john@phrozen.org>, Sean Wang <sean.wang@mediatek.com>, Mark Lee <Mark-MC.Lee@mediatek.com>, Lorenzo Bianconi <lorenzo@kernel.org>, Matthias Brugger <matthias.bgg@gmail.com>, AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>, Saeed Mahameed <saeedm@nvidia.com>, Leon Romanovsky <leon@kernel.org>, Horatiu Vultur <horatiu.vultur@microchip.com>, UNGLinuxDriver@microchip.com, "K. Y. Srinivasan" <kys@microsoft.com>, Haiyang Zhang <haiyangz@microsoft.com>, Wei Liu <wei.liu@kernel.org>, Dexuan Cui <decui@microsoft.com>, Jassi Brar <jaswinder.singh@linaro.org>, Ilias Apalodimas <ilias.apalodimas@linaro.org>, Alexandre Torgue <alexandre.torgue@foss.st.com>, Jose Abreu <joabreu@synopsys.com>, Maxime Coquelin <mcoquelin.stm32@gmail.com>, Siddharth Vadapalli <s-vadapalli@ti.com>, Ravi Gunasekaran <r-gunasekaran@ti.com>, Roger Quadros <rogerq@kernel.org>, Jiawen Wu <jiawenwu@trustnetic.com>, Mengyuan Lou <mengyuanlou@net-swift.com>, Ronak Doshi <doshir@vmware.com>, VMware PV-Drivers Reviewers <pv-drivers@vmware.com>, Ryder Lee <ryder.lee@mediatek.com>, Shayne Chen <shayne.chen@mediatek.com>, Kalle Valo <kvalo@kernel.org>, Juergen Gross <jgross@suse.com>, Stefano Stabellini <sstabellini@kernel.org>, Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>, Andrii Nakryiko <andrii@kernel.org>, Martin KaFai Lau <martin.lau@linux.dev>, Song Liu <song@kernel.org>, Yonghong Song <yonghong.song@linux.dev>, KP Singh <kpsingh@kernel.org>, Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>, Jiri Olsa <jolsa@kernel.org>, Stefan Hajnoczi <stefanha@redhat.com>, Stefano Garzarella <sgarzare@redhat.com>, Shuah Khan <shuah@kernel.org>, " =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= " <mic@digikod.net>, Nathan Chancellor <nathan@kernel.org>, Nick Desaulniers <ndesaulniers@google.com>, Bill Wendling <morbo@google.com>, Justin Stitt <justinstitt@google.com>, Jason Gunthorpe <jgg@nvidia.com>, Shakeel Butt <shakeelb@google.com>, Yunsheng Lin <linyunsheng@huawei.com>, Willem de Bruijn <willemdebruijn.kernel@gmail.com> Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> 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 Dec 2023 18:06:25 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785221180153551022 X-GMAIL-MSGID: 1785221180153551022 |
Series |
Abstract page from net stack
|
|
Commit Message
Mina Almasry
Dec. 14, 2023, 2:05 a.m. UTC
Add the netmem_t type, an abstraction for network memory.
To add support for new memory types to the net stack, we must first
abstract the current memory type from the net stack. Currently parts of
the net stack use struct page directly:
- page_pool
- drivers
- skb_frag_t
Originally the plan was to reuse struct page* for the new memory types,
and to set the LSB on the page* to indicate it's not really a page.
However, for compiler type checking we need to introduce a new type.
netmem_t is introduced to abstract the underlying memory type. Currently
it's a no-op abstraction that is always a struct page underneath. In
parallel there is an undergoing effort to add support for devmem to the
net stack:
https://lore.kernel.org/netdev/20231208005250.2910004-1-almasrymina@google.com/
Signed-off-by: Mina Almasry <almasrymina@google.com>
---
include/net/netmem.h | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
create mode 100644 include/net/netmem.h
Comments
On 12/13/23 7:05 PM, Mina Almasry wrote: > diff --git a/include/net/netmem.h b/include/net/netmem.h > new file mode 100644 > index 000000000000..e4309242d8be > --- /dev/null > +++ b/include/net/netmem.h > @@ -0,0 +1,35 @@ > +/* SPDX-License-Identifier: GPL-2.0 > + * > + * netmem.h > + * Author: Mina Almasry <almasrymina@google.com> > + * Copyright (C) 2023 Google LLC > + */ > + > +#ifndef _NET_NETMEM_H > +#define _NET_NETMEM_H > + > +struct netmem { > + union { > + struct page page; > + > + /* Stub to prevent compiler implicitly converting from page* > + * to netmem_t* and vice versa. > + * > + * Other memory type(s) net stack would like to support > + * can be added to this union. > + */ > + void *addr; > + }; > +}; > + > +static inline struct page *netmem_to_page(struct netmem *netmem) > +{ > + return &netmem->page; > +} > + > +static inline struct netmem *page_to_netmem(struct page *page) > +{ > + return (struct netmem *)page; container_of; no typecasts. > +} > + > +#endif /* _NET_NETMEM_H */
On Wed, 13 Dec 2023 18:05:25 -0800 Mina Almasry wrote: > +struct netmem { > + union { > + struct page page; > + > + /* Stub to prevent compiler implicitly converting from page* > + * to netmem_t* and vice versa. > + * > + * Other memory type(s) net stack would like to support > + * can be added to this union. > + */ > + void *addr; > + }; > +}; My mind went to something like: typedef unsigned long __bitwise netmem_ref; instead. struct netmem does not exist, it's a handle which has to be converted to a real type using a helper.
On Fri, Dec 15, 2023 at 6:52 PM Jakub Kicinski <kuba@kernel.org> wrote: > > On Wed, 13 Dec 2023 18:05:25 -0800 Mina Almasry wrote: > > +struct netmem { > > + union { > > + struct page page; > > + > > + /* Stub to prevent compiler implicitly converting from page* > > + * to netmem_t* and vice versa. > > + * > > + * Other memory type(s) net stack would like to support > > + * can be added to this union. > > + */ > > + void *addr; > > + }; > > +}; > > My mind went to something like: > > typedef unsigned long __bitwise netmem_ref; > > instead. struct netmem does not exist, it's a handle which has > to be converted to a real type using a helper. Sure thing I can do that. Is it better to do something like: struct netmem_ref; like in this patch: https://lore.kernel.org/linux-mm/20221108194139.57604-1-torvalds@linux-foundation.org/ Asking because checkpatch tells me not to add typedefs to the kernel, but checkpatch can be ignored if you think it's OK. Also with this approach I can't use container_of and I need to do a cast, I assume that's fine.
On 12/16/23 2:10 PM, Mina Almasry wrote: > On Fri, Dec 15, 2023 at 6:52 PM Jakub Kicinski <kuba@kernel.org> wrote: >> >> On Wed, 13 Dec 2023 18:05:25 -0800 Mina Almasry wrote: >>> +struct netmem { >>> + union { >>> + struct page page; >>> + >>> + /* Stub to prevent compiler implicitly converting from page* >>> + * to netmem_t* and vice versa. >>> + * >>> + * Other memory type(s) net stack would like to support >>> + * can be added to this union. >>> + */ >>> + void *addr; >>> + }; >>> +}; >> >> My mind went to something like: >> >> typedef unsigned long __bitwise netmem_ref; >> >> instead. struct netmem does not exist, it's a handle which has >> to be converted to a real type using a helper. > > Sure thing I can do that. Is it better to do something like: > > struct netmem_ref; > > like in this patch: > > https://lore.kernel.org/linux-mm/20221108194139.57604-1-torvalds@linux-foundation.org/ > > Asking because checkpatch tells me not to add typedefs to the kernel, > but checkpatch can be ignored if you think it's OK. > > Also with this approach I can't use container_of and I need to do a > cast, I assume that's fine. > Isn't that the whole point of this set - to introduce a new data type and avoid casts?
On Sat, Dec 16, 2023 at 5:45 PM David Ahern <dsahern@kernel.org> wrote: > > On 12/16/23 2:10 PM, Mina Almasry wrote: > > On Fri, Dec 15, 2023 at 6:52 PM Jakub Kicinski <kuba@kernel.org> wrote: > >> > >> On Wed, 13 Dec 2023 18:05:25 -0800 Mina Almasry wrote: > >>> +struct netmem { > >>> + union { > >>> + struct page page; > >>> + > >>> + /* Stub to prevent compiler implicitly converting from page* > >>> + * to netmem_t* and vice versa. > >>> + * > >>> + * Other memory type(s) net stack would like to support > >>> + * can be added to this union. > >>> + */ > >>> + void *addr; > >>> + }; > >>> +}; > >> > >> My mind went to something like: > >> > >> typedef unsigned long __bitwise netmem_ref; > >> > >> instead. struct netmem does not exist, it's a handle which has > >> to be converted to a real type using a helper. > > > > Sure thing I can do that. Is it better to do something like: > > > > struct netmem_ref; > > > > like in this patch: > > > > https://lore.kernel.org/linux-mm/20221108194139.57604-1-torvalds@linux-foundation.org/ > > > > Asking because checkpatch tells me not to add typedefs to the kernel, > > but checkpatch can be ignored if you think it's OK. > > > > Also with this approach I can't use container_of and I need to do a > > cast, I assume that's fine. > > > > Isn't that the whole point of this set - to introduce a new data type > and avoid casts? My understanding here the requirements from Jason are: 1. Never pass a non-page to an mm api. 2. If a mangle a pointer to indicate it's not a page, then I must not call it mm's struct page*, I must add a new type. I think both requirements are met regardless of whether netmem_to_page() is implemented using union/container_of or straight casts. folios implemented something similar being unioned with struct page to avoid casts. I honestly could go either way on this. The union provides some self documenting code and avoids casts. The implementation without the union obfuscates the type and makes it much more opaque. I finished addressing the rest of the comments and I have this series and the next devmem TCP series ready to go, so I fired v2 of this patchset. If one feels strongly about this let me know and I will re-spin.
On Sun, 17 Dec 2023 00:14:59 -0800 Mina Almasry wrote: > > > Sure thing I can do that. Is it better to do something like: > > > > > > struct netmem_ref; > > > > > > like in this patch: > > > > > > https://lore.kernel.org/linux-mm/20221108194139.57604-1-torvalds@linux-foundation.org/ > > > > > > Asking because checkpatch tells me not to add typedefs to the kernel, > > > but checkpatch can be ignored if you think it's OK. > > > > > > Also with this approach I can't use container_of and I need to do a > > > cast, I assume that's fine. > > > > > > > Isn't that the whole point of this set - to introduce a new data type > > and avoid casts? I don't see how we can avoid casts if the type of the referenced object is encoded on the low bits of the pointer. If we had a separate member we could so something like: struct netmem_ref { enum netmem_type type; union { struct page *p; struct page_pool_iov *pi; }; }; barring crazy things with endian-aware bitfields, we need at least one cast. > My understanding here the requirements from Jason are: > > 1. Never pass a non-page to an mm api. > 2. If a mangle a pointer to indicate it's not a page, then I must not > call it mm's struct page*, I must add a new type. > > I think both requirements are met regardless of whether > netmem_to_page() is implemented using union/container_of or straight > casts. folios implemented something similar being unioned with struct > page to avoid casts. Folios overlay a real struct page. It's completely different. > I honestly could go either way on this. The union > provides some self documenting code and avoids casts. Maybe you guys know some trick to mask out the bottom bit :S > The implementation without the union obfuscates the type and makes it much > more opaque. Some would say that that's the damn point of the wrapping.. You don't want non-core code futzing with the inside of the struct. > I finished addressing the rest of the comments and I have this series > and the next devmem TCP series ready to go, so I fired v2 of this > patchset. If one feels strongly about this let me know and I will > re-spin. You didn't address my feedback :| struct netmem which contains struct page by value is almost as bad as passing around pretend struct page pointers.
On Mon, Dec 18, 2023 at 2:06 PM Jakub Kicinski <kuba@kernel.org> wrote: > > On Sun, 17 Dec 2023 00:14:59 -0800 Mina Almasry wrote: > > > > Sure thing I can do that. Is it better to do something like: > > > > > > > > struct netmem_ref; > > > > > > > > like in this patch: > > > > > > > > https://lore.kernel.org/linux-mm/20221108194139.57604-1-torvalds@linux-foundation.org/ > > > > > > > > Asking because checkpatch tells me not to add typedefs to the kernel, > > > > but checkpatch can be ignored if you think it's OK. > > > > > > > > Also with this approach I can't use container_of and I need to do a > > > > cast, I assume that's fine. > > > > > > > > > > Isn't that the whole point of this set - to introduce a new data type > > > and avoid casts? > > I don't see how we can avoid casts if the type of the referenced object > is encoded on the low bits of the pointer. If we had a separate member > we could so something like: > > struct netmem_ref { > enum netmem_type type; > union { > struct page *p; > struct page_pool_iov *pi; > }; > }; > > barring crazy things with endian-aware bitfields, we need at least one > cast. > > > My understanding here the requirements from Jason are: > > > > 1. Never pass a non-page to an mm api. > > 2. If a mangle a pointer to indicate it's not a page, then I must not > > call it mm's struct page*, I must add a new type. > > > > I think both requirements are met regardless of whether > > netmem_to_page() is implemented using union/container_of or straight > > casts. folios implemented something similar being unioned with struct > > page to avoid casts. > > Folios overlay a real struct page. It's completely different. > > > I honestly could go either way on this. The union > > provides some self documenting code and avoids casts. > > Maybe you guys know some trick to mask out the bottom bit :S > > > The implementation without the union obfuscates the type and makes it much > > more opaque. > > Some would say that that's the damn point of the wrapping.. > > You don't want non-core code futzing with the inside of the struct. > > > I finished addressing the rest of the comments and I have this series > > and the next devmem TCP series ready to go, so I fired v2 of this > > patchset. If one feels strongly about this let me know and I will > > re-spin. > > You didn't address my feedback :| > > struct netmem which contains struct page by value is almost as bad > as passing around pretend struct page pointers. Sorry about that. I misread your original request as 'here is something else you can do if you want', not something that you feel is critical. Honestly I missed the subtlety and the approaches seemed roughly equivalent to me. I will respin after the 24hr cooldown.
On Mon, Dec 18, 2023 at 2:39 PM Mina Almasry <almasrymina@google.com> wrote: > [...] > > > > You didn't address my feedback :| > > > > struct netmem which contains struct page by value is almost as bad > > as passing around pretend struct page pointers. > > Sorry about that. I misread your original request as 'here is > something else you can do if you want', not something that you feel is > critical. Honestly I missed the subtlety and the approaches seemed > roughly equivalent to me. I will respin after the 24hr cooldown. > Jakub's suggestion aligns more with the encoded_page approach as well, so let's proceed with that. Waiting for your respin.
diff --git a/include/net/netmem.h b/include/net/netmem.h new file mode 100644 index 000000000000..e4309242d8be --- /dev/null +++ b/include/net/netmem.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * netmem.h + * Author: Mina Almasry <almasrymina@google.com> + * Copyright (C) 2023 Google LLC + */ + +#ifndef _NET_NETMEM_H +#define _NET_NETMEM_H + +struct netmem { + union { + struct page page; + + /* Stub to prevent compiler implicitly converting from page* + * to netmem_t* and vice versa. + * + * Other memory type(s) net stack would like to support + * can be added to this union. + */ + void *addr; + }; +}; + +static inline struct page *netmem_to_page(struct netmem *netmem) +{ + return &netmem->page; +} + +static inline struct netmem *page_to_netmem(struct page *page) +{ + return (struct netmem *)page; +} + +#endif /* _NET_NETMEM_H */