Message ID | 20231220214505.2303297-3-almasrymina@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-7478-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2483:b0:fb:cd0c:d3e with SMTP id q3csp28579dyi; Wed, 20 Dec 2023 13:46:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IHI3fDG/lN9Dh153iIMuI2/WpF24G84qslbdflLVbByRPs7IAIIbyvdydkGp1lmowZi3O2J X-Received: by 2002:a50:d514:0:b0:553:2fce:a5f8 with SMTP id u20-20020a50d514000000b005532fcea5f8mr2327990edi.106.1703108798569; Wed, 20 Dec 2023 13:46:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703108798; cv=none; d=google.com; s=arc-20160816; b=qLNOpYXCMgHbUii4QKoOwOwdbYEGRIO2Hxxw1ZHizbpvk/epyWN8SwpPvR6R1skvrf Vr/zCRiYNQL+Tqfv6aG2nWG+SWgwHv2Zda0tIzbu/+dwe0JXfeXwFtdGi5OzixZ4eYDj IYnW3mEy4qLZscUdUg1tWjH9e/ApS0WNky8YkTMgeLTsgZnU9qi/TVkoviCBtyRXFHJG dRxnmUJUM0RKaLersqGsqLq4knJs702hl3PFyaOd5mrDxluyymxfHgauBtL2xBtq0gM4 3Mesf2b8iPG5CPKTwD489weawde667Z8Q+eGGPZjldVURmZFOgTbFJpIhYpFhl5Higaf izbg== ARC-Message-Signature: i=1; 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=9DyntanP2TiYwqFVgk0wpxjTNS8jI1v6Olu/+yXZqok=; fh=JlPK37Up5EiBiZMpR+3rbUHDZrxBspzZ+EmMm14SzR0=; b=O8MzceAHU33/AaN/A82Qdu5Ju84IUhp6OxempZx5bvQ9uzrrYYz4ZxysRFuiOEZndI Ksa0i9Mj8GkIKo/ug2bnTlWIIsIfAgWZpZ8rW6vc+4NgVuBgs2kRKqo+cf8GXlkwr11s cuLbE3oJkza6VRVtJlquDgA1bclaNMU7285N12OzF3+607RSETzZHo3fHjSB6mxe0VHZ EYc91SopEA8M1k2ZZliBweywIQcorLFOy0FO8o/J7Zg8b3g0qWeY9pdck95W+TrjR2LV dom1bLTxDL0yg04Rf0j4PSFX4CrrIGuNA2AChatbD+ArHyWKgZ18hrAltX0obYXMv0mK GaOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=eOOSYvLB; spf=pass (google.com: domain of linux-kernel+bounces-7478-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7478-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id cx2-20020a05640222a200b00553a31da76fsi202382edb.184.2023.12.20.13.46.38 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 13:46:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-7478-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=eOOSYvLB; spf=pass (google.com: domain of linux-kernel+bounces-7478-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7478-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 31E151F242E1 for <ouuuleilei@gmail.com>; Wed, 20 Dec 2023 21:46:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D65624BA98; Wed, 20 Dec 2023 21:45:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eOOSYvLB" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 A624C4AF79 for <linux-kernel@vger.kernel.org>; Wed, 20 Dec 2023 21:45:13 +0000 (UTC) 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-yw1-f201.google.com with SMTP id 00721157ae682-5e20c9c4080so3734947b3.3 for <linux-kernel@vger.kernel.org>; Wed, 20 Dec 2023 13:45:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1703108713; x=1703713513; 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=9DyntanP2TiYwqFVgk0wpxjTNS8jI1v6Olu/+yXZqok=; b=eOOSYvLBWn2T2H42i1DVzx2QNCS5zbBTluHDw7IYKLT7zZuZ9VE+KFuleyoQQ2KuLo cLqSqjic02BvPpsb6y/lWJHyL5mMWnlisBYGyiSEFASJ9LYu/cawnw8DdbxGTN92ZZf+ AJ2bfCzLkl4Gbmd1yX7ABKNp0D1QFD9/SjhU1CzgrKlDsWb99dUCqxRvvsADnnb553nI orhYUU/KGyzuBLR74NuXfTQXJImADxImGCYep/pt4ElS294/Lv4YpWdMG6s09m5INTZd DcmaCvwX4dSo8HLhxCS4Dbz42FmpsCaJmb1/6IGZdVMpKZzqCpm+tCAh7xI8bBNQXXh7 X4YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703108713; x=1703713513; 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=9DyntanP2TiYwqFVgk0wpxjTNS8jI1v6Olu/+yXZqok=; b=h7XCEqu0sNNPRm/8VAvRNtn+zNPgEytBSmDBJPth86lz0WAVRgg+1QI9wcfTwEbqEy VXix3jn3OWFcNRviWuPJC1qE2UZiSyFE8Qm6DAcSyLwKilwowM0Zx1olAsAh+OEdYXWL lS7uEE3ni46O3L/fioadoA2MG2TIUHIi83c6n2MnEm/yAtabQ3czE+9JN46FR0RM6HTQ YJIwu9qIy82yM/4S2nE5TEwLAP9k0ERRSc+o+djH4KssQA1g+1BbqZfYf+eSPl9w30pq O5wRo45GgPg9/ytS4OBPxWNPwUCnJwM/jk4BVqOeoo+url9CXgj1OCTwqzvp3JBFeDV+ WJdA== X-Gm-Message-State: AOJu0YyIa41wjGKJFXpVrI42gXvX+BgO7N8vA+k46LgaNpXBo6iWnaAG fGuoMeWNKu7GdLAdmt3/aB2qya+vRF2qxIYR/NOuqLEcl4OswgOyUmfeopmSE//Eaixwl95FQBY w3T5FiZb1WUScmEYFsGj+NOK9PbCySHmZ/MLvppqKQoqv6Vjf96rwZc7sP5x2RBnLqRGCRjIwg0 5c0l7mcno= X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2c4:200:13cc:a33:a435:3fe9]) (user=almasrymina job=sendgmr) by 2002:a25:a28a:0:b0:dbd:b756:983a with SMTP id c10-20020a25a28a000000b00dbdb756983amr151334ybi.9.1703108712675; Wed, 20 Dec 2023 13:45:12 -0800 (PST) Date: Wed, 20 Dec 2023 13:45:01 -0800 In-Reply-To: <20231220214505.2303297-1-almasrymina@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> Mime-Version: 1.0 References: <20231220214505.2303297-1-almasrymina@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231220214505.2303297-3-almasrymina@google.com> Subject: [PATCH net-next v3 2/3] net: introduce abstraction for network memory From: Mina Almasry <almasrymina@google.com> To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux.dev Cc: Mina Almasry <almasrymina@google.com>, "David S. Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@google.com>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, Stefano Garzarella <sgarzare@redhat.com>, David Howells <dhowells@redhat.com>, Jason Gunthorpe <jgg@nvidia.com>, " =?utf-8?q?Christian_K=C3=B6nig?= " <christian.koenig@amd.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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785839011866903119 X-GMAIL-MSGID: 1785839011866903119 |
Series |
Abstract page from net stack
|
|
Commit Message
Mina Almasry
Dec. 20, 2023, 9:45 p.m. UTC
Add the netmem_ref 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. 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_ref 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>
---
v3:
- Modify struct netmem from a union of struct page + new types to an opaque
netmem_ref type. I went with:
+typedef void *__bitwise netmem_ref;
rather than this that Jakub recommended:
+typedef unsigned long __bitwise netmem_ref;
Because with the latter the compiler issues warnings to cast NULL to
netmem_ref. I hope that's ok.
- Add some function docs.
v2:
- Use container_of instead of a type cast (David).
---
include/net/netmem.h | 41 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
create mode 100644 include/net/netmem.h
Comments
On Wed, Dec 20, 2023 at 01:45:01PM -0800, Mina Almasry wrote: > Add the netmem_ref 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. 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_ref 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> > > --- > > v3: > > - Modify struct netmem from a union of struct page + new types to an opaque > netmem_ref type. I went with: > > +typedef void *__bitwise netmem_ref; > > rather than this that Jakub recommended: > > +typedef unsigned long __bitwise netmem_ref; > > Because with the latter the compiler issues warnings to cast NULL to > netmem_ref. I hope that's ok. > Can you share what the warning was? You might just need __force attribute. However you might need this __force a lot. I wonder if you can just follow struct encoded_page example verbatim here.
On Thu, Dec 21, 2023 at 3:23 PM Shakeel Butt <shakeelb@google.com> wrote: > > On Wed, Dec 20, 2023 at 01:45:01PM -0800, Mina Almasry wrote: > > Add the netmem_ref 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. 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_ref 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> > > > > --- > > > > v3: > > > > - Modify struct netmem from a union of struct page + new types to an opaque > > netmem_ref type. I went with: > > > > +typedef void *__bitwise netmem_ref; > > > > rather than this that Jakub recommended: > > > > +typedef unsigned long __bitwise netmem_ref; > > > > Because with the latter the compiler issues warnings to cast NULL to > > netmem_ref. I hope that's ok. > > > > Can you share what the warning was? You might just need __force > attribute. However you might need this __force a lot. I wonder if you > can just follow struct encoded_page example verbatim here. > The warning is like so: ./include/net/page_pool/helpers.h: In function ‘page_pool_alloc’: ./include/linux/stddef.h:8:14: warning: returning ‘void *’ from a function with return type ‘netmem_ref’ {aka ‘long unsigned int’} makes integer from pointer without a cast [-Wint-conversion] 8 | #define NULL ((void *)0) | ^ ./include/net/page_pool/helpers.h:132:24: note: in expansion of macro ‘NULL’ 132 | return NULL; | ^~~~ And happens in all the code where: netmem_ref func() { return NULL; } It's fixable by changing the return to `return (netmem_ref NULL);` or `return 0;`, but I feel like netmem_ref should be some type which allows a cast from NULL implicitly. Also as you (and patchwork) noticed, __bitwise should not be used with void*; it's only meant for integer types. Sorry I missed that in the docs and was not running make C=2.
On Thu, 21 Dec 2023 15:44:22 -0800 Mina Almasry wrote: > The warning is like so: > > ./include/net/page_pool/helpers.h: In function ‘page_pool_alloc’: > ./include/linux/stddef.h:8:14: warning: returning ‘void *’ from a > function with return type ‘netmem_ref’ {aka ‘long unsigned int’} makes > integer from pointer without a cast [-Wint-conversion] > 8 | #define NULL ((void *)0) > | ^ > ./include/net/page_pool/helpers.h:132:24: note: in expansion of macro > ‘NULL’ > 132 | return NULL; > | ^~~~ > > And happens in all the code where: > > netmem_ref func() > { > return NULL; > } > > It's fixable by changing the return to `return (netmem_ref NULL);` or > `return 0;`, but I feel like netmem_ref should be some type which > allows a cast from NULL implicitly. Why do you think we should be able to cast NULL implicitly? netmem_ref is a handle, it could possibly be some form of an ID in the future, rather than a pointer. Or have more low bits stolen for specific use cases. unsigned long, and returning 0 as "no handle" makes perfect sense to me. Note that 0 is a special case, bitwise types are allowed to convert to 0/bool and 0 is implicitly allowed to become a bitwise type. This will pass without a warning: typedef unsigned long __bitwise netmem_ref; netmem_ref some_code(netmem_ref ref) { // direct test is fine if (!ref) // 0 "upgrades" without casts return 0; // 1 does not, we need __force return (__force netmem_ref)1 | ref; } The __bitwise annotation will make catching people trying to cast to struct page * trivial. You seem to be trying hard to make struct netmem a thing. Perhaps you have a reason I'm not getting?
On Thu, Jan 4, 2024 at 1:44 PM Jakub Kicinski <kuba@kernel.org> wrote: > > On Thu, 21 Dec 2023 15:44:22 -0800 Mina Almasry wrote: > > The warning is like so: > > > > ./include/net/page_pool/helpers.h: In function ‘page_pool_alloc’: > > ./include/linux/stddef.h:8:14: warning: returning ‘void *’ from a > > function with return type ‘netmem_ref’ {aka ‘long unsigned int’} makes > > integer from pointer without a cast [-Wint-conversion] > > 8 | #define NULL ((void *)0) > > | ^ > > ./include/net/page_pool/helpers.h:132:24: note: in expansion of macro > > ‘NULL’ > > 132 | return NULL; > > | ^~~~ > > > > And happens in all the code where: > > > > netmem_ref func() > > { > > return NULL; > > } > > > > It's fixable by changing the return to `return (netmem_ref NULL);` or > > `return 0;`, but I feel like netmem_ref should be some type which > > allows a cast from NULL implicitly. > > Why do you think we should be able to cast NULL implicitly? > netmem_ref is a handle, it could possibly be some form of > an ID in the future, rather than a pointer. Or have more low > bits stolen for specific use cases. > > unsigned long, and returning 0 as "no handle" makes perfect sense to me. > > Note that 0 is a special case, bitwise types are allowed to convert > to 0/bool and 0 is implicitly allowed to become a bitwise type. > This will pass without a warning: > > typedef unsigned long __bitwise netmem_ref; > > netmem_ref some_code(netmem_ref ref) > { > // direct test is fine > if (!ref) > // 0 "upgrades" without casts > return 0; > // 1 does not, we need __force > return (__force netmem_ref)1 | ref; > } > > The __bitwise annotation will make catching people trying > to cast to struct page * trivial. > > You seem to be trying hard to make struct netmem a thing. > Perhaps you have a reason I'm not getting? There are a number of functions that return struct page* today that I convert to return struct netmem* later in the child devmem series, one example is something like: struct page *page_pool_alloc(...); // returns NULL on failure. becomes: struct netmem *page_pool_alloc(...); // also returns NULL on failure. rather than, netmem_ref page_pool_alloc(...); // returns 0 on failure. I guess in my mind having NULL be castable to the new type makes it so that I can avoid the additional code churn of converting a bunch of `return NULL;` to `return 0;`, and maybe the transition from page pointers to netmem pointers can be more easily done if they're both compatible pointer types. But that is not any huge blocker or critical point in my mind, I just thought this approach is preferred. If conversion to unsigned long makes more sense to you, I'll respin this like that and do the `NULL -> 0` conversion everywhere as needed.
On Thu, Jan 4, 2024 at 1:44 PM Jakub Kicinski <kuba@kernel.org> wrote: > [...] > > You seem to be trying hard to make struct netmem a thing. > Perhaps you have a reason I'm not getting? Mina already went with your suggestion and that is fine. To me, struct netmem is more aesthetically aligned with the existing struct encoded_page approach, but I don't have a strong opinion one way or the other. However it seems like you have a stronger preference for __bitwise approach. Is there a technical reason or just aesthetic?
On Wed, 10 Jan 2024 09:50:08 -0800 Shakeel Butt wrote: > On Thu, Jan 4, 2024 at 1:44 PM Jakub Kicinski <kuba@kernel.org> wrote: > > You seem to be trying hard to make struct netmem a thing. > > Perhaps you have a reason I'm not getting? > > Mina already went with your suggestion and that is fine. To me, struct > netmem is more aesthetically aligned with the existing struct > encoded_page approach, but I don't have a strong opinion one way or > the other. However it seems like you have a stronger preference for > __bitwise approach. Is there a technical reason or just aesthetic? Yes, right above the text you quoted: The __bitwise annotation will make catching people trying to cast to struct page * trivial. https://lore.kernel.org/all/20240104134424.399fee0a@kernel.org/
diff --git a/include/net/netmem.h b/include/net/netmem.h new file mode 100644 index 000000000000..edd977326203 --- /dev/null +++ b/include/net/netmem.h @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Network memory + * + * Author: Mina Almasry <almasrymina@google.com> + */ + +#ifndef _NET_NETMEM_H +#define _NET_NETMEM_H + +/** + * typedef netmem_ref - a nonexistent type marking a reference to generic + * network memory. + * + * A netmem_ref currently is always a reference to a struct page. This + * abstraction is introduced so support for new memory types can be added. + * + * Use the supplied helpers to obtain the underlying memory pointer and fields. + */ +typedef void *__bitwise netmem_ref; + +/* This conversion fails (returns NULL) if the netmem_ref is not struct page + * backed. + * + * Currently struct page is the only possible netmem, and this helper never + * fails. + */ +static inline struct page *netmem_to_page(netmem_ref netmem) +{ + return (struct page *)netmem; +} + +/* Converting from page to netmem is always safe, because a page can always be + * a netmem. + */ +static inline netmem_ref page_to_netmem(struct page *page) +{ + return (netmem_ref)page; +} + +#endif /* _NET_NETMEM_H */