From: Magnus Karlsson <magnus.karlsson@intel.com>
To: magnus.karlsson@intel.com, bjorn.topel@intel.com, ast@kernel.org,
daniel@iogearbox.net, netdev@vger.kernel.org,
jonathan.lemon@gmail.com, maximmi@mellanox.com
Cc: bpf@vger.kernel.org, jeffrey.t.kirsher@intel.com,
maciej.fijalkowski@intel.com, maciejromanfijalkowski@gmail.com,
cristian.dumitrescu@intel.com
Subject: [PATCH bpf-next 08/14] xsk: net: enable sharing of dma mappings
Date: Thu, 2 Jul 2020 14:19:07 +0200 [thread overview]
Message-ID: <1593692353-15102-9-git-send-email-magnus.karlsson@intel.com> (raw)
In-Reply-To: <1593692353-15102-1-git-send-email-magnus.karlsson@intel.com>
Enable the sharing of dma mappings by moving them out of the umem
structure. Instead we put each dma mapped umem region in a list
in the netdev structure. If dma has already been mapped for this
umem and device, it is not mapped again and the existing dma
mappings are reused.
Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
---
include/linux/netdevice.h | 3 ++
include/net/xsk_buff_pool.h | 7 +++
net/core/dev.c | 3 ++
net/xdp/xsk_buff_pool.c | 112 ++++++++++++++++++++++++++++++++++++--------
4 files changed, 106 insertions(+), 19 deletions(-)
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index e5acc3b..fd794aa 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -2006,6 +2006,9 @@ struct net_device {
unsigned int real_num_rx_queues;
struct bpf_prog __rcu *xdp_prog;
+#ifdef CONFIG_XDP_SOCKETS
+ struct list_head xsk_dma_list;
+#endif
unsigned long gro_flush_timeout;
int napi_defer_hard_irqs;
rx_handler_func_t __rcu *rx_handler;
diff --git a/include/net/xsk_buff_pool.h b/include/net/xsk_buff_pool.h
index 6158a47..197cca8 100644
--- a/include/net/xsk_buff_pool.h
+++ b/include/net/xsk_buff_pool.h
@@ -28,6 +28,13 @@ struct xdp_buff_xsk {
struct list_head free_list_node;
};
+struct xsk_dma_map {
+ dma_addr_t *dma_pages;
+ struct xdp_umem *umem;
+ refcount_t users;
+ struct list_head list; /* Protected by the RTNL_LOCK */
+};
+
struct xsk_buff_pool {
struct xsk_queue *fq;
struct xsk_queue *cq;
diff --git a/net/core/dev.c b/net/core/dev.c
index 6bc2388..fe8a72f 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -9959,6 +9959,9 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
INIT_LIST_HEAD(&dev->ptype_all);
INIT_LIST_HEAD(&dev->ptype_specific);
INIT_LIST_HEAD(&dev->net_notifier_list);
+#ifdef CONFIG_XDP_SOCKETS
+ INIT_LIST_HEAD(&dev->xsk_dma_list);
+#endif
#ifdef CONFIG_NET_SCHED
hash_init(dev->qdisc_hash);
#endif
diff --git a/net/xdp/xsk_buff_pool.c b/net/xdp/xsk_buff_pool.c
index ae27664..3c58d76 100644
--- a/net/xdp/xsk_buff_pool.c
+++ b/net/xdp/xsk_buff_pool.c
@@ -107,6 +107,25 @@ void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq)
}
EXPORT_SYMBOL(xp_set_rxq_info);
+static void xp_disable_drv_zc(struct xsk_buff_pool *pool)
+{
+ struct netdev_bpf bpf;
+ int err;
+
+ ASSERT_RTNL();
+
+ if (pool->umem->zc) {
+ bpf.command = XDP_SETUP_XSK_POOL;
+ bpf.xsk.pool = NULL;
+ bpf.xsk.queue_id = pool->queue_id;
+
+ err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf);
+
+ if (err)
+ WARN(1, "Failed to disable zero-copy!\n");
+ }
+}
+
int xp_assign_dev(struct xsk_buff_pool *pool, struct xdp_sock *xs,
struct net_device *netdev, u16 queue_id, u16 flags)
{
@@ -125,6 +144,8 @@ int xp_assign_dev(struct xsk_buff_pool *pool, struct xdp_sock *xs,
if (xsk_get_pool_from_qid(netdev, queue_id))
return -EBUSY;
+ pool->netdev = netdev;
+ pool->queue_id = queue_id;
err = xsk_reg_pool_at_qid(netdev, pool, queue_id);
if (err)
return err;
@@ -158,11 +179,15 @@ int xp_assign_dev(struct xsk_buff_pool *pool, struct xdp_sock *xs,
if (err)
goto err_unreg_pool;
- pool->netdev = netdev;
- pool->queue_id = queue_id;
+ if (!pool->dma_pages) {
+ WARN(1, "Driver did not DMA map zero-copy buffers");
+ goto err_unreg_xsk;
+ }
pool->umem->zc = true;
return 0;
+err_unreg_xsk:
+ xp_disable_drv_zc(pool);
err_unreg_pool:
if (!force_zc)
err = 0; /* fallback to copy mode */
@@ -173,25 +198,10 @@ int xp_assign_dev(struct xsk_buff_pool *pool, struct xdp_sock *xs,
void xp_clear_dev(struct xsk_buff_pool *pool)
{
- struct netdev_bpf bpf;
- int err;
-
- ASSERT_RTNL();
-
if (!pool->netdev)
return;
- if (pool->umem->zc) {
- bpf.command = XDP_SETUP_XSK_POOL;
- bpf.xsk.pool = NULL;
- bpf.xsk.queue_id = pool->queue_id;
-
- err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf);
-
- if (err)
- WARN(1, "Failed to disable zero-copy!\n");
- }
-
+ xp_disable_drv_zc(pool);
xsk_clear_pool_at_qid(pool->netdev, pool->queue_id);
dev_put(pool->netdev);
pool->netdev = NULL;
@@ -241,14 +251,61 @@ bool xp_validate_queues(struct xsk_buff_pool *pool)
return pool->fq && pool->cq;
}
+static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool)
+{
+ struct xsk_dma_map *dma_map;
+
+ list_for_each_entry(dma_map, &pool->netdev->xsk_dma_list, list) {
+ if (dma_map->umem == pool->umem)
+ return dma_map;
+ }
+
+ return NULL;
+}
+
+static void xp_destroy_dma_map(struct xsk_dma_map *dma_map)
+{
+ list_del(&dma_map->list);
+ kfree(dma_map);
+}
+
+static void xp_put_dma_map(struct xsk_dma_map *dma_map)
+{
+ if (!refcount_dec_and_test(&dma_map->users))
+ return;
+
+ xp_destroy_dma_map(dma_map);
+}
+
+static struct xsk_dma_map *xp_create_dma_map(struct xsk_buff_pool *pool)
+{
+ struct xsk_dma_map *dma_map;
+
+ dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL);
+ if (!dma_map)
+ return NULL;
+
+ dma_map->umem = pool->umem;
+ refcount_set(&dma_map->users, 1);
+ list_add(&dma_map->list, &pool->netdev->xsk_dma_list);
+ return dma_map;
+}
+
void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
{
+ struct xsk_dma_map *dma_map;
dma_addr_t *dma;
u32 i;
if (pool->dma_pages_cnt == 0)
return;
+ dma_map = xp_find_dma_map(pool);
+ if (!dma_map) {
+ WARN(1, "Could not find dma_map for device");
+ return;
+ }
+
for (i = 0; i < pool->dma_pages_cnt; i++) {
dma = &pool->dma_pages[i];
if (*dma) {
@@ -258,6 +315,7 @@ void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
}
}
+ xp_put_dma_map(dma_map);
kvfree(pool->dma_pages);
pool->dma_pages_cnt = 0;
pool->dev = NULL;
@@ -321,14 +379,29 @@ static bool xp_check_cheap_dma(struct xsk_buff_pool *pool)
int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
unsigned long attrs, struct page **pages, u32 nr_pages)
{
+ struct xsk_dma_map *dma_map;
dma_addr_t dma;
u32 i;
+ dma_map = xp_find_dma_map(pool);
+ if (dma_map) {
+ pool->dma_pages = dma_map->dma_pages;
+ refcount_inc(&dma_map->users);
+ return 0;
+ }
+
+ dma_map = xp_create_dma_map(pool);
+ if (!dma_map)
+ return -ENOMEM;
+
pool->dma_pages = kvcalloc(nr_pages, sizeof(*pool->dma_pages),
GFP_KERNEL);
- if (!pool->dma_pages)
+ if (!pool->dma_pages) {
+ xp_destroy_dma_map(dma_map);
return -ENOMEM;
+ }
+ dma_map->dma_pages = pool->dma_pages;
pool->dev = dev;
pool->dma_pages_cnt = nr_pages;
@@ -337,6 +410,7 @@ int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
DMA_BIDIRECTIONAL, attrs);
if (dma_mapping_error(dev, dma)) {
xp_dma_unmap(pool, attrs);
+ xp_destroy_dma_map(dma_map);
return -ENOMEM;
}
pool->dma_pages[i] = dma;
--
2.7.4
next prev parent reply other threads:[~2020-07-02 12:19 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-02 12:18 [PATCH bpf-next 00/14] xsk: support shared umems between devices and queues Magnus Karlsson
2020-07-02 12:19 ` [PATCH bpf-next 01/14] xsk: i40e: ice: ixgbe: mlx5: pass buffer pool to driver instead of umem Magnus Karlsson
2020-07-08 15:00 ` Maxim Mikityanskiy
2020-07-02 12:19 ` [PATCH bpf-next 02/14] xsk: i40e: ice: ixgbe: mlx5: rename xsk zero-copy driver interfaces Magnus Karlsson
2020-07-02 12:19 ` [PATCH bpf-next 03/14] xsk: create and free context independently from umem Magnus Karlsson
2020-07-08 15:00 ` Maxim Mikityanskiy
2020-07-09 6:47 ` Magnus Karlsson
2020-07-02 12:19 ` [PATCH bpf-next 04/14] xsk: move fill and completion rings to buffer pool Magnus Karlsson
2020-07-02 12:19 ` [PATCH bpf-next 05/14] xsk: move queue_id, dev and need_wakeup to context Magnus Karlsson
2020-07-02 12:19 ` [PATCH bpf-next 06/14] xsk: move xsk_tx_list and its lock to buffer pool Magnus Karlsson
2020-07-02 12:19 ` [PATCH bpf-next 07/14] xsk: move addrs from buffer pool to umem Magnus Karlsson
2020-07-02 12:19 ` Magnus Karlsson [this message]
2020-07-02 12:19 ` [PATCH bpf-next 09/14] xsk: rearrange internal structs for better performance Magnus Karlsson
2020-07-02 12:19 ` [PATCH bpf-next 10/14] xsk: add shared umem support between queue ids Magnus Karlsson
2020-07-02 12:19 ` [PATCH bpf-next 11/14] xsk: add shared umem support between devices Magnus Karlsson
2020-07-09 15:20 ` Maxim Mikityanskiy
2020-07-02 12:19 ` [PATCH bpf-next 12/14] libbpf: support shared umems between queues and devices Magnus Karlsson
2020-07-02 12:19 ` [PATCH bpf-next 13/14] samples/bpf: add new sample xsk_fwd.c Magnus Karlsson
2020-07-02 12:19 ` [PATCH bpf-next 14/14] xsk: documentation for XDP_SHARED_UMEM between queues and netdevs Magnus Karlsson
2020-07-06 18:39 ` [PATCH bpf-next 00/14] xsk: support shared umems between devices and queues Daniel Borkmann
2020-07-07 10:37 ` Maxim Mikityanskiy
2020-07-08 15:00 ` Maxim Mikityanskiy
2020-07-09 6:54 ` Magnus Karlsson
2020-07-09 14:56 ` [PATCH 1/2] xsk: i40e: ice: ixgbe: mlx5: pass buffer pool to driver instead of umem Maxim Mikityanskiy
2020-07-09 14:56 ` [PATCH 2/2] xsk: i40e: ice: ixgbe: mlx5: rename xsk zero-copy driver interfaces Maxim Mikityanskiy
2020-07-09 17:23 ` kernel test robot
2020-07-09 16:32 ` [PATCH 1/2] xsk: i40e: ice: ixgbe: mlx5: pass buffer pool to driver instead of umem kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1593692353-15102-9-git-send-email-magnus.karlsson@intel.com \
--to=magnus.karlsson@intel.com \
--cc=ast@kernel.org \
--cc=bjorn.topel@intel.com \
--cc=bpf@vger.kernel.org \
--cc=cristian.dumitrescu@intel.com \
--cc=daniel@iogearbox.net \
--cc=jeffrey.t.kirsher@intel.com \
--cc=jonathan.lemon@gmail.com \
--cc=maciej.fijalkowski@intel.com \
--cc=maciejromanfijalkowski@gmail.com \
--cc=maximmi@mellanox.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).