From patchwork Thu Feb 1 21:34:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 195475 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp37049dyc; Thu, 1 Feb 2024 13:35:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IGNB4KA49Q7MepA1QYpZN2u7TaiSeIKQ9YYWzYDQqAT9/Pzyu9vARhrNwVGSpN7fAWrGOB8 X-Received: by 2002:a05:622a:15d3:b0:42b:f5f6:23bb with SMTP id d19-20020a05622a15d300b0042bf5f623bbmr2932813qty.40.1706823331745; Thu, 01 Feb 2024 13:35:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706823331; cv=pass; d=google.com; s=arc-20160816; b=ml7fycRb3V3g1iMKtC+YkIWYTJJUAF0c9IRTjZ5aNRxgn8ZThxeXDybi2Tu3Bd2Zd6 QFAGqqC++f86SL0hzdDAuLjRl7Q+oOX82AldtTqC3w7mPlHiC7jnEylEtB9hXfamf9Fr wOnN7M0Zq4M24AAsKFyyMfy3VjATtFVQCBG0WFAzdxDA8ffjMqD/gYx41cylN4oUgW4V cVUYYSTlNJPD6FNhda1hT4itUdQxNOF3J45mnrao+cmUPu60QFr4piFZs0yd4zyino76 NBB01yratqMgnipCvR8thrxJwDCYlcYDrvN5A9T+uUQQ29qPT3nfVqwGklRTugxQBSbM uyBw== 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=p/ZSORHpensQ8OOs9+q/lB48Cp/PdVGCaffXK4MOExw=; fh=Rb/FrY0Rl8ao7H7/QqfmcxJgX6H46UIhBaJjqpZ24z4=; b=dGJFQAopXq6UuSLNWo2gUGPcxvx/+t5UX/qO4lOu+55js0HkyHm4HG501K/jwVRWpe YmirvjtwRLfuw/xjQ42OskYDmsoQP+tGzKa/7vuQF2z31xtTmc4dQKZHNMQb5um2qV+S TfWLDyR9dWt6BGlL/NTzglqyfy3guMy3kJdufDshphsOkuhq0NRnn3OLd0d6fz81zvyI dqDAR+XV7zBVkPsdsn5YJGUoGgI9JJRgZAV2mY+Tp2STFuREZx/qruby4oS/NDwgv5II +zPgpChSg1ZvK52OzECtC+cH2W3UZA+J0vJxDFaynMwTZzDbnDoH3tnlzfanSnGMVU4e 7/bA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EBYOicDO; 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-48881-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-48881-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCWh5cIswER5ryQRWI/M1xuHgnEZjcwZ86QBeDP12VTAgoA+jlm5f7jorW3mq5PpNWgdue3G8LvzT6t3WvwIo6xWJZNGMQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id u26-20020a05622a199a00b0042b2fd9b161si458213qtc.165.2024.02.01.13.35.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 13:35:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-48881-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EBYOicDO; 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-48881-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-48881-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 610551C20F12 for ; Thu, 1 Feb 2024 21:35:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3DE453FE5D; Thu, 1 Feb 2024 21:34:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="EBYOicDO" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 9097E3E491 for ; Thu, 1 Feb 2024 21:34:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706823278; cv=none; b=fxEq6pwfF3zINaPzGYaGJMdLS+g9r8aEH7P2LjXw3GqFhEP3Z+rRs0mGRofMs2bkNIDD/AQVEI+R8tZyXH6edNbwKjCBrC3t0KgoBs3D4F9oyyt1Jgq2a7iDCuJtWpqzAPf2y0l1MjHUGWf29H/2pF+gqW0kcOLKyTMHbwcQ/s0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706823278; c=relaxed/simple; bh=rZrTvmLO32ue83Wuz3xqEY+uT6j76FWBRjAdq8bhOuk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TxLkTLEoigEFsGnE1hxY19a3iLeBHPc8IPpjGiyoq26RhsAelzcnp18mxhRVtU9TJlTIpwoYjvCqxVvnD9wseklTZi767yiOZBKr/AS+u05jLqHAM/m7kdF8UbPfATsmJ/rE7yrEHOPS3t1wKZifad9wPfgpgOPrba4kA1Z2YpM= 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=EBYOicDO; arc=none smtp.client-ip=209.85.128.202 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-f202.google.com with SMTP id 00721157ae682-60406626b50so30953127b3.0 for ; Thu, 01 Feb 2024 13:34:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706823275; x=1707428075; 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=p/ZSORHpensQ8OOs9+q/lB48Cp/PdVGCaffXK4MOExw=; b=EBYOicDOkYD9Zr6NiY9Owzl3mJWl1+hn97ADfF1G6SNR6FHi65plCuKGvH3S5l/D9t UsYL3HvCdtLB5CzoW82EmFRf5KYLOpQnRvSJrLkBlbEmX89NBggTiXtMzPuOr0G2j402 jfsaEeIPgKaAWTbarh+m+GSkeu71UbgajsZLlw29dB/9V7tiWtKtA/XCYffmmpERR+k0 MFoLt0TTHLbN5mKba/sJq/DtKuBf9MAHM5RxasNL0zm5Gaz1Q77jinlno2tLU+qtJ6ry i5d7wuS8KcsxwVu0ZNNqlDpTiwgWTXlUnIosHbpyG/5pv95tcFGWmi27Z1d8a8Fs2ajd XgaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706823275; x=1707428075; 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=p/ZSORHpensQ8OOs9+q/lB48Cp/PdVGCaffXK4MOExw=; b=hsaSyUUZakV5hDjXIwUEmUr7vWNaFR5dcXG7htnKN6YPsYIKvBItX9jBKANxgZjSxG jy8A3OdA8A9TTT5LcFUw/7QOt9hV5ooFfyA16FQwnLmh0FiEL3dxUlOsMg4eKayNcQ0g ixIRAkprrfVF8OeEEPvyHdNwApDkWpER8S6B/Sx89M7RpAq+AETTpq9KzT3lQ0AjaBQZ 9EprcXK71wciswKJ7L5ewDIjEVTebzByucalx/YTYIl5oCq28CUuzWTrjS15eRTrpCpe NW0OhppE9rxWX6BMNhKEMepXwS+0va8KBtnSxtaM66F6tFAV4SPpKcldO1SKuXUNKne9 JL4g== X-Gm-Message-State: AOJu0YxmH02lrXf/du0jVbc6DFK34rnrv2m8rdCo3+erTJzFXQHSlARQ t20qUMN8h2GC2SnNwD+JCVmE/doYfrakaIt5d/GvNWa1flVJMbk8ez0a63IAH7NEj9diywSy6es jxZPFAtyQL1E3DUn2Uov7q71p4UyXgfPqTJmxnfdl9nENBx8B7MsF3QVQSPZ6/7eFe8cFCNtjCq gHWZvVChJlMQhve+r4dGfhJCBqgxkLHa7j3sLVaLgDKEraylIhCxwDRjTAj+aOWLiyZd4= X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2c4:200:4c26:ae83:34fc:80a5]) (user=almasrymina job=sendgmr) by 2002:a05:6902:108e:b0:dbd:b165:441 with SMTP id v14-20020a056902108e00b00dbdb1650441mr80976ybu.0.1706823275454; Thu, 01 Feb 2024 13:34:35 -0800 (PST) Date: Thu, 1 Feb 2024 13:34:25 -0800 In-Reply-To: <20240201213429.4120839-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: <20240201213429.4120839-1-almasrymina@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240201213429.4120839-2-almasrymina@google.com> Subject: [PATCH net-next v7 1/2] net: introduce abstraction for network memory From: Mina Almasry To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: Mina Almasry , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Jason Gunthorpe , " =?utf-8?q?Christian_K=C3=B6nig?= " , Shakeel Butt , Yunsheng Lin , Willem de Bruijn X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789733981648361008 X-GMAIL-MSGID: 1789733981648361008 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/ netmem_ref can be pointers to different underlying memory types, and the low bits are set to indicate the memory type. Helpers are provided to convert netmem pointers to the underlying memory type (currently only struct page). In the devmem series helpers are provided so that calling code can use netmem without worrying about the underlying memory type unless absolutely necessary. Reviewed-by: Shakeel Butt Signed-off-by: Mina Almasry --- v7: - Fix netmem_ref kdoc. v6: - Applied Reviewed-by from Shakeel. rfc v5: - RFC due to merge window. - Change to 'typedef unsigned long __bitwise netmem_ref;' - Fixed commit message (Shakeel). - Did not apply Shakeel's reviewed-by since the code changed significantly. v4: - use 'struct netmem;' instead of 'typedef void *__bitwise netmem_ref;' Using __bitwise with a non-integer type was wrong and triggered many patchwork bot errors/warnings. Using an integer type causes the compiler to warn when casting NULL to the integer type. Attempt to use an empty struct for our opaque network memory. 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 diff --git a/include/net/netmem.h b/include/net/netmem.h new file mode 100644 index 000000000000..d8b810245c1d --- /dev/null +++ b/include/net/netmem.h @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Network memory + * + * Author: Mina Almasry + */ + +#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 unsigned long __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 (__force 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 (__force netmem_ref)page; +} + +#endif /* _NET_NETMEM_H */ From patchwork Thu Feb 1 21:34:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 195476 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp37268dyc; Thu, 1 Feb 2024 13:35:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IEPIw4VgsHPLEo7thU6nH1npAMZPavGxWpPZn18oS0CR3xW0Qvdph2Bvm9Fr6JHqgvuMgnj X-Received: by 2002:a17:906:b283:b0:a36:55e8:ea40 with SMTP id q3-20020a170906b28300b00a3655e8ea40mr2410920ejz.41.1706823353822; Thu, 01 Feb 2024 13:35:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706823353; cv=pass; d=google.com; s=arc-20160816; b=JB5LY2qS50wrXS0SS5+FBivIf8PiXMbX1tLfPRYEwSUk8Nnsm3O0Xoz+5UTfHi82ls ZEKKqBcFQdtpZp8sOaw4892Nt3KY51zjLa3f6gfhwd8Ga5maUMZTFeu3XkidKHLJUSx1 dcu8gOlp7IC9XnV5Cq83ZbxIAlXZEsDnngQQnPfyGhT4T1W+Mo1uiTNFWfpfD8e+xEXZ FHjs5r7TsSvHq52slWC3C7jKLgBDqksEcIiyZPl5lu9efygUU8RKqxZNXbEh2YWzVp50 HgP31DSZGbtU/a6nT2hFiGSQ6qkpfDOQ5JHOg2wjQM3/n0z+1o48TgkJRuypQk5GS2ga Or/w== 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=BWLmWK2Td4HLWqajG1M4UvIFhKc5O1hv4SsJBoHY8ds=; fh=+iQkRDWhjifQo8R4IRkipKRNrJ0pHXp7WtusAzXfG4I=; b=iT6nwWYfHx2oj8x0oehblQ7PQkjbYhbOmGftr4Jh3pxTjWLIKu5qdHNLjuqgAMqsxa DpFW6SvnUsntliSXFzCo7av6n411XBWpQHgiv+fCtZHJPBnijJxDI61S2m5OGL0iCIa6 08Omal6BCEbKAWodVMAHRo/t/IXupXJ3lxjJIwylrDBelHm8PugRyj+PDdeIkmg0S5MM Nh7Qxfm8Q65mrLiOVfZQVH/s5b63WH3+E7DLY2oVYWeg1nXaYbEt2BrOnU18gTCJ0iTk Wc55krLXeNYtZNoZQ9CpZX6Y7s4cExNUI1XFo9EsRIXcr8Pyo0EEH7i5/gxSoFLYrd98 TE1g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=108sp8ca; 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-48882-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-48882-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCVhBIQvkmmQhqba0jf6ys3gpdz9jZOsDm1wsrBUce+RW1xdEMAIwRmh3wyWxYazKK7sW5Fj/qJUB3c1yGuPuofN8jHd7Q== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id v17-20020a1709060b5100b00a36c32891c5si169958ejg.228.2024.02.01.13.35.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 13:35:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-48882-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=108sp8ca; 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-48882-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-48882-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 2D61E1F255E4 for ; Thu, 1 Feb 2024 21:35:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4FE9C40BF2; Thu, 1 Feb 2024 21:34:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="108sp8ca" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 C5AFA3FB0C for ; Thu, 1 Feb 2024 21:34:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706823280; cv=none; b=RsRXFO+VqeNDxLWVwmA3bIeW/5AKFs61Qzy1aH+qyQDc66TCs2nPloiVXpfPv6iPwm/UkeiIdzKdKwoeqbsRI3V+QEYBRz/7tK+a7Tsnhoz4UKy+pVjgud6SxeEaM6KlR5io0Xjf5xjX+NvtRlParIUT4Su6F9roFZre0d/D0YY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706823280; c=relaxed/simple; bh=XsTm4G/QN8bn2Q4iit9UfUbT2i7xvQSURHOPoEOGK4o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tBMSXiwjb96cfslXxAXl8chVbmhjOalvOo+MLHOVwLJp1gqJr6GSaZoePihmnTF6049dqeBVZYXPij92Tn6Jj2x/pLA7n0ipo0ikOXwdYKfSGRyiv1bzer37NeCW7FPtv6J+CFcu+bYY9plMy2K5Y8oBmSca7E5BIr0P36UM0No= 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=108sp8ca; arc=none smtp.client-ip=209.85.128.202 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-f202.google.com with SMTP id 00721157ae682-5f0fd486b9aso22107267b3.2 for ; Thu, 01 Feb 2024 13:34:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706823278; x=1707428078; 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=BWLmWK2Td4HLWqajG1M4UvIFhKc5O1hv4SsJBoHY8ds=; b=108sp8caluXK1k2HF/SM/b9ID5VS1URZzVQTMf1bVer4n5ZAVUsZ16nKU/YCYsohfL bDDlic5US5Or+Z+2zDonSPRw4P8iw+WFmTLI3Y11NFmRSPVJo68iJ0v51gX8B7XhiHXG tLHwuFxLI/+qidkqRKP6+GGG0SpvJeZxOwJpN8BlwO+iefaohQPhUt7PZ6lKEC551BPF 91FlI8N9C5D2KHCThulMgulo0TH2qTWJ9D6cJHKKuxaONkleFrGmMDmUATbcPBGcrEa3 bQJTZ7u7oDNAs0BLtLIPvydZ+A+H36RF4ix0xQiYfF7H7cIBm+IHX8L9Aa25jkIlUg1q 5mRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706823278; x=1707428078; 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=BWLmWK2Td4HLWqajG1M4UvIFhKc5O1hv4SsJBoHY8ds=; b=HDXKAKtUjXNuZGQiVShpGUaMHXipstWn2TfMPOm5SP6ItZrhwZANLTzvpRjkKkPt5h JsHWlGS8Y/z8n+sAncxaJgoXL89zB6IIIqguTv4h6VjwCT+gq0GMV6wfo46KPWlayDQ1 FHh7deYD5bOcpYSewpOIZ19qg3PAmIbls9eIuuDp6lNqX1CgbCWqNANj/BGiVAwMO59l /dgFSD8tT3G9YCedJ4XWw2RsJtXmJ2lzBZtd4uSEjUZFAgCU6QFjNu6O7xkM0gLN8hlI kK19hWlHo/qFKcnWEEmTZHtlh0VT6FyPofutv9Z4cnmV/B8wOyxtbnnDYjknut1KDPmx n31w== X-Gm-Message-State: AOJu0Yy02OLqXEd/haQMI2uKiz9Ke0Kw/5YAcb+2U5gDAlVkTRNlDFM0 o3VgwqyGvRXIXC6HMLC6x39vCUq1JIorZ8zZcfFKoFqTlw9BRhiFJQtLaHnJ8KZSMT7UgytDZNe /waBMqTAxOKUkMvjaTXrs/TwE32ECZiK8pM4Sk8uSh6TyLzIxEBz3C6vKf8cjn96c0cy35mEtDs 8NA/j0lzjKlTfqruGn7O1pxova/eHXy61Ghewh9Pg5Tgh0vat0xUFDDijMnr1VVPfgsjs= X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2c4:200:4c26:ae83:34fc:80a5]) (user=almasrymina job=sendgmr) by 2002:a05:690c:f8d:b0:604:1eea:a39e with SMTP id df13-20020a05690c0f8d00b006041eeaa39emr443690ywb.3.1706823277800; Thu, 01 Feb 2024 13:34:37 -0800 (PST) Date: Thu, 1 Feb 2024 13:34:26 -0800 In-Reply-To: <20240201213429.4120839-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: <20240201213429.4120839-1-almasrymina@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240201213429.4120839-3-almasrymina@google.com> Subject: [PATCH net-next v7 2/2] net: add netmem to skb_frag_t From: Mina Almasry To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: Mina Almasry , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Jason Gunthorpe , " =?utf-8?q?Christian_K=C3=B6nig?= " , Shakeel Butt , Yunsheng Lin , Willem de Bruijn X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789734004975718759 X-GMAIL-MSGID: 1789734004975718759 Use struct netmem* instead of page in skb_frag_t. Currently struct netmem* is always a struct page underneath, but the abstraction allows efforts to add support for skb frags not backed by pages. There is unfortunately 1 instance where the skb_frag_t is assumed to be a exactly a bio_vec in kcm. For this case, WARN_ON_ONCE and return error before doing a cast. Add skb[_frag]_fill_netmem_*() and skb_add_rx_frag_netmem() helpers so that the API can be used to create netmem skbs. Signed-off-by: Mina Almasry --- v7: - Move skb_rx_add_frag to header file (Paolo). - Move combine kcm skb_frag_page() check with nr_frags check. v6: - Add static_asserts to validate skb_frag_t to bio_vec cast in kcm_write_msg (Yunsheng) v4: - Handle error in kcm_write_msgs() instead of only warning (Willem) v3: - Renamed the fields in skb_frag_t. v2: - Add skb frag filling helpers. --- include/linux/skbuff.h | 109 ++++++++++++++++++++++++++++++----------- net/core/skbuff.c | 38 ++++++++------ net/kcm/kcmsock.c | 7 +-- 3 files changed, 107 insertions(+), 47 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 2dde34c29203..7d1c03e59017 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -37,6 +37,7 @@ #endif #include #include +#include /** * DOC: skb checksums @@ -359,7 +360,11 @@ extern int sysctl_max_skb_frags; */ #define GSO_BY_FRAGS 0xFFFF -typedef struct bio_vec skb_frag_t; +typedef struct skb_frag { + netmem_ref netmem; + unsigned int len; + unsigned int offset; +} skb_frag_t; /** * skb_frag_size() - Returns the size of a skb fragment @@ -367,7 +372,7 @@ typedef struct bio_vec skb_frag_t; */ static inline unsigned int skb_frag_size(const skb_frag_t *frag) { - return frag->bv_len; + return frag->len; } /** @@ -377,7 +382,7 @@ static inline unsigned int skb_frag_size(const skb_frag_t *frag) */ static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size) { - frag->bv_len = size; + frag->len = size; } /** @@ -387,7 +392,7 @@ static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size) */ static inline void skb_frag_size_add(skb_frag_t *frag, int delta) { - frag->bv_len += delta; + frag->len += delta; } /** @@ -397,7 +402,7 @@ static inline void skb_frag_size_add(skb_frag_t *frag, int delta) */ static inline void skb_frag_size_sub(skb_frag_t *frag, int delta) { - frag->bv_len -= delta; + frag->len -= delta; } /** @@ -417,7 +422,7 @@ static inline bool skb_frag_must_loop(struct page *p) * skb_frag_foreach_page - loop over pages in a fragment * * @f: skb frag to operate on - * @f_off: offset from start of f->bv_page + * @f_off: offset from start of f->netmem * @f_len: length from f_off to loop over * @p: (temp var) current page * @p_off: (temp var) offset from start of current page, @@ -2429,22 +2434,37 @@ static inline unsigned int skb_pagelen(const struct sk_buff *skb) return skb_headlen(skb) + __skb_pagelen(skb); } +static inline void skb_frag_fill_netmem_desc(skb_frag_t *frag, + netmem_ref netmem, int off, + int size) +{ + frag->netmem = netmem; + frag->offset = off; + skb_frag_size_set(frag, size); +} + static inline void skb_frag_fill_page_desc(skb_frag_t *frag, struct page *page, int off, int size) { - frag->bv_page = page; - frag->bv_offset = off; - skb_frag_size_set(frag, size); + skb_frag_fill_netmem_desc(frag, page_to_netmem(page), off, size); +} + +static inline void __skb_fill_netmem_desc_noacc(struct skb_shared_info *shinfo, + int i, netmem_ref netmem, + int off, int size) +{ + skb_frag_t *frag = &shinfo->frags[i]; + + skb_frag_fill_netmem_desc(frag, netmem, off, size); } static inline void __skb_fill_page_desc_noacc(struct skb_shared_info *shinfo, int i, struct page *page, int off, int size) { - skb_frag_t *frag = &shinfo->frags[i]; - - skb_frag_fill_page_desc(frag, page, off, size); + __skb_fill_netmem_desc_noacc(shinfo, i, page_to_netmem(page), off, + size); } /** @@ -2460,10 +2480,10 @@ static inline void skb_len_add(struct sk_buff *skb, int delta) } /** - * __skb_fill_page_desc - initialise a paged fragment in an skb + * __skb_fill_netmem_desc - initialise a fragment in an skb * @skb: buffer containing fragment to be initialised - * @i: paged fragment index to initialise - * @page: the page to use for this fragment + * @i: fragment index to initialise + * @netmem: the netmem to use for this fragment * @off: the offset to the data with @page * @size: the length of the data * @@ -2472,10 +2492,12 @@ static inline void skb_len_add(struct sk_buff *skb, int delta) * * Does not take any additional reference on the fragment. */ -static inline void __skb_fill_page_desc(struct sk_buff *skb, int i, - struct page *page, int off, int size) +static inline void __skb_fill_netmem_desc(struct sk_buff *skb, int i, + netmem_ref netmem, int off, int size) { - __skb_fill_page_desc_noacc(skb_shinfo(skb), i, page, off, size); + struct page *page = netmem_to_page(netmem); + + __skb_fill_netmem_desc_noacc(skb_shinfo(skb), i, netmem, off, size); /* Propagate page pfmemalloc to the skb if we can. The problem is * that not all callers have unique ownership of the page but rely @@ -2483,7 +2505,20 @@ static inline void __skb_fill_page_desc(struct sk_buff *skb, int i, */ page = compound_head(page); if (page_is_pfmemalloc(page)) - skb->pfmemalloc = true; + skb->pfmemalloc = true; +} + +static inline void __skb_fill_page_desc(struct sk_buff *skb, int i, + struct page *page, int off, int size) +{ + __skb_fill_netmem_desc(skb, i, page_to_netmem(page), off, size); +} + +static inline void skb_fill_netmem_desc(struct sk_buff *skb, int i, + netmem_ref netmem, int off, int size) +{ + __skb_fill_netmem_desc(skb, i, netmem, off, size); + skb_shinfo(skb)->nr_frags = i + 1; } /** @@ -2503,8 +2538,7 @@ static inline void __skb_fill_page_desc(struct sk_buff *skb, int i, static inline void skb_fill_page_desc(struct sk_buff *skb, int i, struct page *page, int off, int size) { - __skb_fill_page_desc(skb, i, page, off, size); - skb_shinfo(skb)->nr_frags = i + 1; + skb_fill_netmem_desc(skb, i, page_to_netmem(page), off, size); } /** @@ -2528,8 +2562,25 @@ static inline void skb_fill_page_desc_noacc(struct sk_buff *skb, int i, shinfo->nr_frags = i + 1; } -void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, - int size, unsigned int truesize); +static inline void skb_add_rx_frag_netmem(struct sk_buff *skb, int i, + netmem_ref netmem, int off, int size, + unsigned int truesize) +{ + DEBUG_NET_WARN_ON_ONCE(size > truesize); + + skb_fill_netmem_desc(skb, i, netmem, off, size); + skb->len += size; + skb->data_len += size; + skb->truesize += truesize; +} + +static inline void skb_add_rx_frag(struct sk_buff *skb, int i, + struct page *page, int off, int size, + unsigned int truesize) +{ + skb_add_rx_frag_netmem(skb, i, page_to_netmem(page), off, size, + truesize); +} void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, unsigned int truesize); @@ -3378,7 +3429,7 @@ static inline void skb_propagate_pfmemalloc(const struct page *page, */ static inline unsigned int skb_frag_off(const skb_frag_t *frag) { - return frag->bv_offset; + return frag->offset; } /** @@ -3388,7 +3439,7 @@ static inline unsigned int skb_frag_off(const skb_frag_t *frag) */ static inline void skb_frag_off_add(skb_frag_t *frag, int delta) { - frag->bv_offset += delta; + frag->offset += delta; } /** @@ -3398,7 +3449,7 @@ static inline void skb_frag_off_add(skb_frag_t *frag, int delta) */ static inline void skb_frag_off_set(skb_frag_t *frag, unsigned int offset) { - frag->bv_offset = offset; + frag->offset = offset; } /** @@ -3409,7 +3460,7 @@ static inline void skb_frag_off_set(skb_frag_t *frag, unsigned int offset) static inline void skb_frag_off_copy(skb_frag_t *fragto, const skb_frag_t *fragfrom) { - fragto->bv_offset = fragfrom->bv_offset; + fragto->offset = fragfrom->offset; } /** @@ -3420,7 +3471,7 @@ static inline void skb_frag_off_copy(skb_frag_t *fragto, */ static inline struct page *skb_frag_page(const skb_frag_t *frag) { - return frag->bv_page; + return netmem_to_page(frag->netmem); } /** @@ -3524,7 +3575,7 @@ static inline void *skb_frag_address_safe(const skb_frag_t *frag) static inline void skb_frag_page_copy(skb_frag_t *fragto, const skb_frag_t *fragfrom) { - fragto->bv_page = fragfrom->bv_page; + fragto->netmem = fragfrom->netmem; } bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t prio); diff --git a/net/core/skbuff.c b/net/core/skbuff.c index edbbef563d4d..a754978cbed8 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -115,6 +115,24 @@ static struct kmem_cache *skb_small_head_cache __ro_after_init; int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; EXPORT_SYMBOL(sysctl_max_skb_frags); +/* kcm_write_msgs() relies on casting paged frags to bio_vec to use + * iov_iter_bvec(). These static asserts ensure the cast is valid is long as the + * netmem is a page. + */ +static_assert(offsetof(struct bio_vec, bv_page) == + offsetof(skb_frag_t, netmem)); +static_assert(sizeof_field(struct bio_vec, bv_page) == + sizeof_field(skb_frag_t, netmem)); + +static_assert(offsetof(struct bio_vec, bv_len) == offsetof(skb_frag_t, len)); +static_assert(sizeof_field(struct bio_vec, bv_len) == + sizeof_field(skb_frag_t, len)); + +static_assert(offsetof(struct bio_vec, bv_offset) == + offsetof(skb_frag_t, offset)); +static_assert(sizeof_field(struct bio_vec, bv_offset) == + sizeof_field(skb_frag_t, offset)); + #undef FN #define FN(reason) [SKB_DROP_REASON_##reason] = #reason, static const char * const drop_reasons[] = { @@ -845,18 +863,6 @@ struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, } EXPORT_SYMBOL(__napi_alloc_skb); -void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, - int size, unsigned int truesize) -{ - DEBUG_NET_WARN_ON_ONCE(size > truesize); - - skb_fill_page_desc(skb, i, page, off, size); - skb->len += size; - skb->data_len += size; - skb->truesize += truesize; -} -EXPORT_SYMBOL(skb_add_rx_frag); - void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, unsigned int truesize) { @@ -1906,10 +1912,11 @@ int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) /* skb frags point to kernel buffers */ for (i = 0; i < new_frags - 1; i++) { - __skb_fill_page_desc(skb, i, head, 0, psize); + __skb_fill_netmem_desc(skb, i, page_to_netmem(head), 0, psize); head = (struct page *)page_private(head); } - __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); + __skb_fill_netmem_desc(skb, new_frags - 1, page_to_netmem(head), 0, + d_off); skb_shinfo(skb)->nr_frags = new_frags; release: @@ -3647,7 +3654,8 @@ skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) if (plen) { page = virt_to_head_page(from->head); offset = from->data - (unsigned char *)page_address(page); - __skb_fill_page_desc(to, 0, page, offset, plen); + __skb_fill_netmem_desc(to, 0, page_to_netmem(page), + offset, plen); get_page(page); j = 1; len -= plen; diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c index 1184d40167b8..73c200c5c8e4 100644 --- a/net/kcm/kcmsock.c +++ b/net/kcm/kcmsock.c @@ -627,7 +627,8 @@ static int kcm_write_msgs(struct kcm_sock *kcm) skb = txm->frag_skb; } - if (WARN_ON(!skb_shinfo(skb)->nr_frags)) { + if (WARN_ON(!skb_shinfo(skb)->nr_frags) || + WARN_ON_ONCE(!skb_frag_page(&skb_shinfo(skb)->frags[0]))) { ret = -EINVAL; goto out; } @@ -637,8 +638,8 @@ static int kcm_write_msgs(struct kcm_sock *kcm) msize += skb_frag_size(&skb_shinfo(skb)->frags[i]); iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, - skb_shinfo(skb)->frags, skb_shinfo(skb)->nr_frags, - msize); + (const struct bio_vec *)skb_shinfo(skb)->frags, + skb_shinfo(skb)->nr_frags, msize); iov_iter_advance(&msg.msg_iter, txm->frag_offset); do {