Skip to contentSkip to conversation

net/mlx5: allocate doorbells from dma pools

PATCH Applied Tariq Toukan 2 participants 5 messages Aug 3 – Aug 6

Subsystems (5)
lore
Share & tools
lore

Tariq Toukan

author maintainer
#0 lore raw

v2 cover net/mlx5: allocate doorbells from dma pools

Hi,

This series by Nimrod extends the mlx5 internal DMA pool infrastructure to doorbell objects.

Doorbell allocations currently use a separate pgdir allocator. Moving them to the common mlx5 DMA pool implementation keeps the same coherent DMA allocation model while sharing the pool code used by mlx5_frag_buf.

The series first creates per-node doorbell pools, then switches doorbell allocation to use them, and finally exposes their usage through debugfs.

Regards,
Tariq

V2:
- Dropped defensive check in patch 3 (Leon).

Nimrod Oren (3):
net/mlx5: initialize doorbell dma pools net/mlx5: allocate doorbells from dma pools net/mlx5: add debugfs stats for doorbell dma pools

 .../net/ethernet/mellanox/mlx5/core/alloc.c   | 139 ++++++++----------
 .../net/ethernet/mellanox/mlx5/core/main.c    |  11 +-
 .../ethernet/mellanox/mlx5/core/mlx5_core.h   |   2 +
 include/linux/mlx5/driver.h                   |   8 +-
 4 files changed, 76 insertions(+), 84 deletions(-)

base-commit: 69963a0678a347d57c4ac8b16939dba216eb95ce

Signature · 1 line
2.44.0
author maintainer
#1 lore raw

PATCH v2 1/3 net/mlx5: initialize doorbell dma pools

From: Nimrod Oren <[email protected]>

Add per-node doorbell dma pool creation and cleanup to mdev lifecycle.

---
 .../net/ethernet/mellanox/mlx5/core/alloc.c   | 37 +++++++++++++++++++
 .../net/ethernet/mellanox/mlx5/core/main.c    |  7 ++++
 .../ethernet/mellanox/mlx5/core/mlx5_core.h   |  2 +
 include/linux/mlx5/driver.h                   |  2 +
 4 files changed, 48 insertions(+)
drivers/net/ethernet/mellanox/mlx5/core/alloc.c +37 -0
index 4fe9d7d4f143..3c9938068c56 100644
@@ -444,6 +444,43 @@ void mlx5_frag_buf_free(struct mlx5_core_dev *dev, struct mlx5_frag_buf *buf)
 }
 EXPORT_SYMBOL_GPL(mlx5_frag_buf_free);
 
+void mlx5_db_pools_cleanup(struct mlx5_core_dev *dev)
+{
+	struct mlx5_priv *priv = &dev->priv;
+	int node;
+
+	for_each_node_state(node, N_POSSIBLE)
+		if (priv->db_node_pools[node])
+			mlx5_dma_pool_destroy(priv->db_node_pools[node]);
+
+	kfree(priv->db_node_pools);
+	priv->db_node_pools = NULL;
+}
+
+int mlx5_db_pools_init(struct mlx5_core_dev *dev)
+{
+	struct mlx5_priv *priv = &dev->priv;
+	int node;
+
+	priv->db_node_pools = kzalloc_objs(*priv->db_node_pools, nr_node_ids);
+	if (!priv->db_node_pools)
+		return -ENOMEM;
+
+	for_each_node_state(node, N_POSSIBLE) {
+		struct mlx5_dma_pool *pool;
+
+		pool = mlx5_dma_pool_create(dev, node,
+					    order_base_2(cache_line_size()));
+		if (!pool) {
+			mlx5_db_pools_cleanup(dev);
+			return -ENOMEM;
+		}
+		priv->db_node_pools[node] = pool;
+	}
+
+	return 0;
+}
+
 static struct mlx5_db_pgdir *mlx5_alloc_db_pgdir(struct mlx5_core_dev *dev,
 						 int node)
 {
drivers/net/ethernet/mellanox/mlx5/core/main.c +7 -0
index 643b4aac2033..b3cb090b5677 100644
@@ -1830,6 +1830,10 @@ int mlx5_mdev_init(struct mlx5_core_dev *dev, int profile_idx)
 	if (err)
 		goto err_frag_buf_pools_init;
 
+	err = mlx5_db_pools_init(dev);
+	if (err)
+		goto err_db_pools_init;
+
 	INIT_LIST_HEAD(&priv->traps);
 
 	err = mlx5_cmd_init(dev);
@@ -1891,6 +1895,8 @@ int mlx5_mdev_init(struct mlx5_core_dev *dev, int profile_idx)
 err_timeout_init:
 	mlx5_cmd_cleanup(dev);
 err_cmd_init:
+	mlx5_db_pools_cleanup(dev);
+err_db_pools_init:
 	mlx5_frag_buf_pools_cleanup(dev);
 err_frag_buf_pools_init:
 	debugfs_remove(dev->priv.dbg.dbg_root);
@@ -1917,6 +1923,7 @@ void mlx5_mdev_uninit(struct mlx5_core_dev *dev)
 	mlx5_health_cleanup(dev);
 	mlx5_tout_cleanup(dev);
 	mlx5_cmd_cleanup(dev);
+	mlx5_db_pools_cleanup(dev);
 	mlx5_frag_buf_pools_cleanup(dev);
 	debugfs_remove_recursive(dev->priv.dbg.dbg_root);
 	mutex_destroy(&priv->pgdir_mutex);
drivers/net/ethernet/mellanox/mlx5/core/mlx5_core.h +2 -0
index 09e669f83dba..d6713a2ce676 100644
@@ -438,6 +438,8 @@ int mlx5_mdev_init(struct mlx5_core_dev *dev, int profile_idx);
 void mlx5_mdev_uninit(struct mlx5_core_dev *dev);
 int mlx5_frag_buf_pools_init(struct mlx5_core_dev *dev);
 void mlx5_frag_buf_pools_cleanup(struct mlx5_core_dev *dev);
+int mlx5_db_pools_init(struct mlx5_core_dev *dev);
+void mlx5_db_pools_cleanup(struct mlx5_core_dev *dev);
 int mlx5_init_one(struct mlx5_core_dev *dev);
 int mlx5_init_one_devl_locked(struct mlx5_core_dev *dev);
 void mlx5_uninit_one(struct mlx5_core_dev *dev);
include/linux/mlx5/driver.h +2 -0
index b1871c0821d0..4246d6d904ba 100644
@@ -569,6 +569,7 @@ enum mlx5_page_mgt_mode {
 };
 
 struct mlx5_frag_buf_node_pools;
+struct mlx5_dma_pool;
 struct mlx5_ft_pool;
 struct mlx5_priv {
 	/* IRQ table valid only for real pci devices PF or VF */
@@ -602,6 +603,7 @@ struct mlx5_priv {
 	struct list_head        pgdir_list;
 
 	struct mlx5_frag_buf_node_pools **frag_buf_node_pools;
+	struct mlx5_dma_pool **db_node_pools;
 	/* end: alloc stuff */
 
 	struct mlx5_adev       **adev;
Signature · 1 line
2.44.0
author maintainer
#2 lore raw

PATCH v2 2/3 net/mlx5: allocate doorbells from dma pools

In reply to Tariq Toukan

From: Nimrod Oren <[email protected]>

Allocate doorbells from dma pools instead of the pgdir allocator. Doorbell records remain cache-line sized coherent DMA allocations, but their sub-allocation is now handled by the common mlx5 DMA pool infrastructure.

This also makes doorbell allocation honor the requested NUMA node when reusing existing backing pages. The old pgdir allocator used the requested node only when allocating a new pgdir page; later allocations scanned one global pgdir list and could take any pgdir with a free entry, even if that page had been allocated for a different NUMA node. Selecting the per-node DMA pool before sub-allocation keeps reused doorbell records on pages allocated for the requested node.

---
 .../net/ethernet/mellanox/mlx5/core/alloc.c   | 111 +++---------------
 .../net/ethernet/mellanox/mlx5/core/main.c    |   4 -
 include/linux/mlx5/driver.h                   |   5 +-
 3 files changed, 18 insertions(+), 102 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/alloc.c +17 -94
index 3c9938068c56..3975c726d35c 100644
@@ -48,13 +48,6 @@
 #define MLX5_FRAG_BUF_POOLS_NUM \
 	(PAGE_SHIFT - MLX5_FRAG_BUF_POOL_MIN_BLOCK_SHIFT + 1)
 
-struct mlx5_db_pgdir {
-	struct list_head	list;
-	unsigned long	       *bitmap;
-	__be32		       *db_page;
-	dma_addr_t		db_dma;
-};
-
 struct mlx5_dma_pool {
 	/* Protects page_list and per-page allocation bitmaps. */
 	struct mutex lock;
@@ -481,106 +474,36 @@ int mlx5_db_pools_init(struct mlx5_core_dev *dev)
 	return 0;
 }
 
-static struct mlx5_db_pgdir *mlx5_alloc_db_pgdir(struct mlx5_core_dev *dev,
-						 int node)
-{
-	u32 db_per_page = PAGE_SIZE / cache_line_size();
-	struct mlx5_db_pgdir *pgdir;
-
-	pgdir = kzalloc_node(sizeof(*pgdir), GFP_KERNEL, node);
-	if (!pgdir)
-		return NULL;
-
-	pgdir->bitmap = bitmap_zalloc_node(db_per_page, GFP_KERNEL, node);
-	if (!pgdir->bitmap) {
-		kfree(pgdir);
-		return NULL;
-	}
-
-	bitmap_fill(pgdir->bitmap, db_per_page);
-
-	pgdir->db_page = mlx5_dma_zalloc_coherent_node(dev, PAGE_SIZE,
-						       &pgdir->db_dma, node);
-	if (!pgdir->db_page) {
-		bitmap_free(pgdir->bitmap);
-		kfree(pgdir);
-		return NULL;
-	}
-
-	return pgdir;
-}
-
-static int mlx5_alloc_db_from_pgdir(struct mlx5_db_pgdir *pgdir,
-				    struct mlx5_db *db)
+int mlx5_db_alloc_node(struct mlx5_core_dev *dev, struct mlx5_db *db, int node)
 {
-	u32 db_per_page = PAGE_SIZE / cache_line_size();
+	struct mlx5_dma_pool_page *page;
+	struct mlx5_dma_pool *pool;
+	unsigned long idx;
 	int offset;
-	int i;
-
-	i = find_first_bit(pgdir->bitmap, db_per_page);
-	if (i >= db_per_page)
-		return -ENOMEM;
 
-	__clear_bit(i, pgdir->bitmap);
+	node = node == NUMA_NO_NODE ? numa_mem_id() : node;
 
-	db->u.pgdir = pgdir;
-	db->index   = i;
-	offset = db->index * cache_line_size();
-	db->db      = pgdir->db_page + offset / sizeof(*pgdir->db_page);
-	db->dma     = pgdir->db_dma  + offset;
+	pool = dev->priv.db_node_pools[node];
+	page = mlx5_dma_pool_alloc(pool, &idx);
+	if (!page)
+		return -ENOMEM;
 
-	db->db[0] = 0;
-	db->db[1] = 0;
+	offset = idx << pool->block_shift;
+	db->u.pool_page = page;
+	db->index = idx;
+	db->db = (__be32 *)((u8 *)page->buf + offset);
+	db->dma = page->dma + offset;
 
 	return 0;
 }
-
-int mlx5_db_alloc_node(struct mlx5_core_dev *dev, struct mlx5_db *db, int node)
-{
-	struct mlx5_db_pgdir *pgdir;
-	int ret = 0;
-
-	mutex_lock(&dev->priv.pgdir_mutex);
-
-	list_for_each_entry(pgdir, &dev->priv.pgdir_list, list)
-		if (!mlx5_alloc_db_from_pgdir(pgdir, db))
-			goto out;
-
-	pgdir = mlx5_alloc_db_pgdir(dev, node);
-	if (!pgdir) {
-		ret = -ENOMEM;
-		goto out;
-	}
-
-	list_add(&pgdir->list, &dev->priv.pgdir_list);
-
-	/* This should never fail -- we just allocated an empty page: */
-	WARN_ON(mlx5_alloc_db_from_pgdir(pgdir, db));
-
-out:
-	mutex_unlock(&dev->priv.pgdir_mutex);
-
-	return ret;
-}
 EXPORT_SYMBOL_GPL(mlx5_db_alloc_node);
 
 void mlx5_db_free(struct mlx5_core_dev *dev, struct mlx5_db *db)
 {
-	u32 db_per_page = PAGE_SIZE / cache_line_size();
-
-	mutex_lock(&dev->priv.pgdir_mutex);
-
-	__set_bit(db->index, db->u.pgdir->bitmap);
-
-	if (bitmap_full(db->u.pgdir->bitmap, db_per_page)) {
-		dma_free_coherent(mlx5_core_dma_dev(dev), PAGE_SIZE,
-				  db->u.pgdir->db_page, db->u.pgdir->db_dma);
-		list_del(&db->u.pgdir->list);
-		bitmap_free(db->u.pgdir->bitmap);
-		kfree(db->u.pgdir);
-	}
+	struct mlx5_dma_pool_page *page = db->u.pool_page;
+	struct mlx5_dma_pool *pool = page->pool;
 
-	mutex_unlock(&dev->priv.pgdir_mutex);
+	mlx5_dma_pool_free(pool, page, db->index);
 }
 EXPORT_SYMBOL_GPL(mlx5_db_free);
 
drivers/net/ethernet/mellanox/mlx5/core/main.c +0 -4
index b3cb090b5677..5f28d906c35b 100644
@@ -1819,8 +1819,6 @@ int mlx5_mdev_init(struct mlx5_core_dev *dev, int profile_idx)
 	INIT_LIST_HEAD(&priv->bfregs.wc_head.list);
 
 	mutex_init(&priv->alloc_mutex);
-	mutex_init(&priv->pgdir_mutex);
-	INIT_LIST_HEAD(&priv->pgdir_list);
 
 	priv->numa_node = dev_to_node(mlx5_core_dma_dev(dev));
 	priv->dbg.dbg_root = debugfs_create_dir(dev_name(dev->device),
@@ -1900,7 +1898,6 @@ int mlx5_mdev_init(struct mlx5_core_dev *dev, int profile_idx)
 	mlx5_frag_buf_pools_cleanup(dev);
 err_frag_buf_pools_init:
 	debugfs_remove(dev->priv.dbg.dbg_root);
-	mutex_destroy(&priv->pgdir_mutex);
 	mutex_destroy(&priv->alloc_mutex);
 	mutex_destroy(&priv->bfregs.wc_head.lock);
 	mutex_destroy(&priv->bfregs.reg_head.lock);
@@ -1926,7 +1923,6 @@ void mlx5_mdev_uninit(struct mlx5_core_dev *dev)
 	mlx5_db_pools_cleanup(dev);
 	mlx5_frag_buf_pools_cleanup(dev);
 	debugfs_remove_recursive(dev->priv.dbg.dbg_root);
-	mutex_destroy(&priv->pgdir_mutex);
 	mutex_destroy(&priv->alloc_mutex);
 	mutex_destroy(&priv->bfregs.wc_head.lock);
 	mutex_destroy(&priv->bfregs.reg_head.lock);
include/linux/mlx5/driver.h +1 -4
index 4246d6d904ba..2c56bb1d676d 100644
@@ -599,9 +599,6 @@ struct mlx5_priv {
 	struct mutex            alloc_mutex;
 	int                     numa_node;
 
-	struct mutex            pgdir_mutex;
-	struct list_head        pgdir_list;
-
 	struct mlx5_frag_buf_node_pools **frag_buf_node_pools;
 	struct mlx5_dma_pool **db_node_pools;
 	/* end: alloc stuff */
@@ -808,7 +805,7 @@ struct mlx5_core_dev {
 struct mlx5_db {
 	__be32			*db;
 	union {
-		struct mlx5_db_pgdir		*pgdir;
+		struct mlx5_dma_pool_page	*pool_page;
 		struct mlx5_ib_user_db_page	*user_page;
 	}			u;
 	dma_addr_t		dma;
Signature · 1 line
2.44.0
author maintainer
#3 lore raw

PATCH v2 3/3 net/mlx5: add debugfs stats for doorbell dma pools

In reply to Tariq Toukan

From: Nimrod Oren <[email protected]>

Add a debugfs file exposing per-node DMA pool usage for doorbell allocations.

  # cat /sys/kernel/debug/mlx5/<dev>/db_dma_pools
  node  block_size  used_blocks  allocated_blocks
     0          64            0                 0
     1          64            0                 0
---
 .../net/ethernet/mellanox/mlx5/core/alloc.c   | 27 +++++++++++++++++++
 include/linux/mlx5/driver.h                   |  1 +
 2 files changed, 28 insertions(+)
drivers/net/ethernet/mellanox/mlx5/core/alloc.c +27 -0
index 3975c726d35c..a92cf545bdaf 100644
@@ -437,11 +437,34 @@ void mlx5_frag_buf_free(struct mlx5_core_dev *dev, struct mlx5_frag_buf *buf)
 }
 EXPORT_SYMBOL_GPL(mlx5_frag_buf_free);
 
+static int mlx5_db_dma_pools_debugfs_show(struct seq_file *file, void *priv)
+{
+	struct mlx5_core_dev *dev = file->private;
+	int node;
+
+	mlx5_dma_pools_debugfs_print_header(file);
+
+	for_each_node_state(node, N_POSSIBLE) {
+		struct mlx5_dma_pool *pool = dev->priv.db_node_pools[node];
+
+		if (!pool)
+			continue;
+
+		mlx5_dma_pool_debugfs_stats_print(file, pool);
+	}
+
+	return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(mlx5_db_dma_pools_debugfs);
+
 void mlx5_db_pools_cleanup(struct mlx5_core_dev *dev)
 {
 	struct mlx5_priv *priv = &dev->priv;
 	int node;
 
+	debugfs_remove(priv->dbg.db_dma_pools_debugfs);
+	priv->dbg.db_dma_pools_debugfs = NULL;
+
 	for_each_node_state(node, N_POSSIBLE)
 		if (priv->db_node_pools[node])
 			mlx5_dma_pool_destroy(priv->db_node_pools[node]);
@@ -471,6 +494,10 @@ int mlx5_db_pools_init(struct mlx5_core_dev *dev)
 		priv->db_node_pools[node] = pool;
 	}
 
+	priv->dbg.db_dma_pools_debugfs =
+		debugfs_create_file("db_dma_pools", 0444, priv->dbg.dbg_root,
+				    dev, &mlx5_db_dma_pools_debugfs_fops);
+
 	return 0;
 }
 
include/linux/mlx5/driver.h +1 -0
index 2c56bb1d676d..83d0a83bbfbc 100644
@@ -548,6 +548,7 @@ struct mlx5_debugfs_entries {
 	struct dentry *cq_debugfs;
 	struct dentry *cmdif_debugfs;
 	struct dentry *frag_buf_dma_pools_debugfs;
+	struct dentry *db_dma_pools_debugfs;
 	struct dentry *pages_debugfs;
 	struct dentry *lag_debugfs;
 };
Signature · 1 line
2.44.0
#4 lore raw
In reply to Tariq Toukan
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Mon, 3 Aug 2026 16:25:17 +0300 you wrote:

Hi,

This series by Nimrod extends the mlx5 internal DMA pool infrastructure to doorbell objects.

Doorbell allocations currently use a separate pgdir allocator. Moving them to the common mlx5 DMA pool implementation keeps the same coherent DMA allocation model while sharing the pool code used by mlx5_frag_buf.

[...]

Here is the summary with links:
- [net-next,V2,1/3] net/mlx5: initialize doorbell dma pools

    https://git.kernel.org/netdev/net-next/c/46f636cd620e
  - [net-next,V2,2/3] net/mlx5: allocate doorbells from dma pools
    https://git.kernel.org/netdev/net-next/c/c29235677f5e

- [net-next,V2,3/3] net/mlx5: add debugfs stats for doorbell dma pools
https://git.kernel.org/netdev/net-next/c/b7cfcc9d3dfd

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html