Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH v2 0/5] RDMA, IB: replace __get_free_pages() with kmalloc()
@ 2026-07-13  7:17 Mike Rapoport (Microsoft)
  2026-07-13  7:17 ` [PATCH v2 1/5] RDMA/umem: ib_umem_get(): use kmalloc() to allocate page array Mike Rapoport (Microsoft)
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-07-13  7:17 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Dennis Dalessandro, Mike Rapoport, linux-kernel, linux-mm,
	linux-rdma

This is a (small) part of larger work of replacing page allocator calls
with kmalloc.

My initial intention a few month ago was to remove ugly casts [1], but then
willy pointed out that Linus objected to something like this [2] and it
looks like more than a decade old technical debt.

Largely, anything that doesn't need struct page (or a memdesc in the
future) should just use kmalloc() or kvmalloc() to allocate memory.
kmalloc() guarantees alignment, physical contiguity and working
virt_to_phys() and beside nicer API that returns void * on alloc and
doesn't require to know the allocation size on free, kmalloc() provides
better debugging capabilities than page allocator.

Another thing is that touching these allocation sites gives the reviewers
opportunity to see if a PAGE_SIZE buffer is actually needed or maybe
another size is appropriate.

For larger allocations that don't need physically contiguous memory
kvmalloc() can be a better option that __get_free_pages() because under
memory pressure it's is easier to allocate several order-0 pages than a
physically contiguous chunk with the same number of pages.

And last, but not least, removing needless calls to page allocator should
help with memdesc (aka project folio) conversion. There will be way less
places to audit to see if the user was actually using struct page.

Also in git:
https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git gfp-to-kmalloc/rdma

[1] https://lore.kernel.org/all/20251018093002.3660549-1-rppt@kernel.org/
[2] https://lore.kernel.org/all/CA+55aFwp4iy4rtX2gE2WjBGFL=NxMVnoFeHqYa2j1dYOMMGqxg@mail.gmail.com/ 

---
v2 changes:
* add comment to keep markers for sites that need "fast and as large as
  possible" allocation helper

v1: https://patch.msgid.link/20260630-b4-rdma-v1-0-ab42bcf0de92@kernel.org

---
Mike Rapoport (Microsoft) (5):
      RDMA/umem: ib_umem_get(): use kmalloc() to allocate page array
      RDMA/mlx5: replace __get_free_page() with kmalloc()
      IB/mthca: mthca_reg_user_mr(): use kmalloc() to allocate addresses array
      IB/mthca: allocate mthca_array memory with kzalloc()
      IB/rdmavt: use kzalloc() to allocate QPN-map pages

 drivers/infiniband/core/umem.c                | 5 +++--
 drivers/infiniband/hw/mlx5/odp.c              | 6 ++++--
 drivers/infiniband/hw/mthca/mthca_allocator.c | 6 +++---
 drivers/infiniband/hw/mthca/mthca_provider.c  | 5 +++--
 drivers/infiniband/sw/rdmavt/qp.c             | 8 ++++----
 5 files changed, 17 insertions(+), 13 deletions(-)
---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260610-b4-rdma-44625922fe16

--
Sincerely yours,
Mike.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/5] RDMA/umem: ib_umem_get(): use kmalloc() to allocate page array
  2026-07-13  7:17 [PATCH v2 0/5] RDMA, IB: replace __get_free_pages() with kmalloc() Mike Rapoport (Microsoft)
@ 2026-07-13  7:17 ` Mike Rapoport (Microsoft)
  2026-07-13  7:17 ` [PATCH v2 2/5] RDMA/mlx5: replace __get_free_page() with kmalloc() Mike Rapoport (Microsoft)
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-07-13  7:17 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Dennis Dalessandro, Mike Rapoport, linux-kernel, linux-mm,
	linux-rdma

ib_umem_get() allocates an array of pointers to struct page for
pin_user_pages_fast() calls during memory registration.

This array can be allocated with kmalloc() as there's nothing special
about it to go directly to the page allocator.

kmalloc() provides a better API that does not require ugly casts and
kfree() does not need to know the size of the freed object.

Performance difference between kmalloc() and __get_free_pages() is not
measurable as both allocators take an object/page from a per-CPU list for
fast path allocations.

For the slow path the performance is anyway determined by the amount of
reclaim involved rather than by what allocator is used.

Replace use of __get_free_page() with kmalloc() and free_page() with
kfree().

Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
 drivers/infiniband/core/umem.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/infiniband/core/umem.c b/drivers/infiniband/core/umem.c
index 73498723a5d5..81f44dadfa52 100644
--- a/drivers/infiniband/core/umem.c
+++ b/drivers/infiniband/core/umem.c
@@ -209,7 +209,8 @@ static struct ib_umem *__ib_umem_get_va(struct ib_device *device,
 
 	mmgrab(mm);
 
-	page_list = (struct page **) __get_free_page(GFP_KERNEL);
+	/* TODO: switch to "fast and as large as possible" allocation helper */
+	page_list = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!page_list) {
 		ret = -ENOMEM;
 		goto umem_kfree;
@@ -269,7 +270,7 @@ static struct ib_umem *__ib_umem_get_va(struct ib_device *device,
 	__ib_umem_release(device, umem, 0);
 	atomic64_sub(ib_umem_num_pages(umem), &mm->pinned_vm);
 out:
-	free_page((unsigned long) page_list);
+	kfree(page_list);
 umem_kfree:
 	if (ret) {
 		mmdrop(umem->owning_mm);

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 2/5] RDMA/mlx5: replace __get_free_page() with kmalloc()
  2026-07-13  7:17 [PATCH v2 0/5] RDMA, IB: replace __get_free_pages() with kmalloc() Mike Rapoport (Microsoft)
  2026-07-13  7:17 ` [PATCH v2 1/5] RDMA/umem: ib_umem_get(): use kmalloc() to allocate page array Mike Rapoport (Microsoft)
@ 2026-07-13  7:17 ` Mike Rapoport (Microsoft)
  2026-07-13  7:17 ` [PATCH v2 3/5] IB/mthca: mthca_reg_user_mr(): use kmalloc() to allocate addresses array Mike Rapoport (Microsoft)
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-07-13  7:17 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Dennis Dalessandro, Mike Rapoport, linux-kernel, linux-mm,
	linux-rdma

mlx5_ib_mr_wqe_pfault_handler() allocates a scratch buffer for
parsing work queue entries during page fault handling.

This buffer can be allocated with kmalloc() as there's nothing special
about it to go directly to the page allocator.

kmalloc() provides a better API that does not require ugly casts and
kfree() does not need to know the size of the freed object.

Performance difference between kmalloc() and __get_free_pages() is not
measurable as both allocators take an object/page from a per-CPU list for
fast path allocations.

For the slow path the performance is anyway determined by the amount of
reclaim involved rather than by what allocator is used.

Replace use of __get_free_page() with kmalloc() and free_page() with
kfree().

Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
 drivers/infiniband/hw/mlx5/odp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/infiniband/hw/mlx5/odp.c b/drivers/infiniband/hw/mlx5/odp.c
index 1badec9bf527..b8618610737a 100644
--- a/drivers/infiniband/hw/mlx5/odp.c
+++ b/drivers/infiniband/hw/mlx5/odp.c
@@ -37,6 +37,7 @@
 #include <linux/hmm.h>
 #include <linux/hmm-dma.h>
 #include <linux/pci-p2pdma.h>
+#include <linux/slab.h>
 
 #include "mlx5_ib.h"
 #include "cmd.h"
@@ -1414,7 +1415,8 @@ static void mlx5_ib_mr_wqe_pfault_handler(struct mlx5_ib_dev *dev,
 		goto resolve_page_fault;
 	}
 
-	wqe_start = (void *)__get_free_page(GFP_KERNEL);
+	/* TODO: switch to "fast and as large as possible" allocation helper */
+	wqe_start = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!wqe_start) {
 		mlx5_ib_err(dev, "Error allocating memory for IO page fault handling.\n");
 		goto resolve_page_fault;
@@ -1475,7 +1477,7 @@ static void mlx5_ib_mr_wqe_pfault_handler(struct mlx5_ib_dev *dev,
 		    pfault->wqe.wq_num, resume_with_error,
 		    pfault->type);
 	mlx5_core_res_put(res);
-	free_page((unsigned long)wqe_start);
+	kfree(wqe_start);
 }
 
 static void mlx5_ib_mr_rdma_pfault_handler(struct mlx5_ib_dev *dev,

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 3/5] IB/mthca: mthca_reg_user_mr(): use kmalloc() to allocate addresses array
  2026-07-13  7:17 [PATCH v2 0/5] RDMA, IB: replace __get_free_pages() with kmalloc() Mike Rapoport (Microsoft)
  2026-07-13  7:17 ` [PATCH v2 1/5] RDMA/umem: ib_umem_get(): use kmalloc() to allocate page array Mike Rapoport (Microsoft)
  2026-07-13  7:17 ` [PATCH v2 2/5] RDMA/mlx5: replace __get_free_page() with kmalloc() Mike Rapoport (Microsoft)
@ 2026-07-13  7:17 ` Mike Rapoport (Microsoft)
  2026-07-13  7:17 ` [PATCH v2 4/5] IB/mthca: allocate mthca_array memory with kzalloc() Mike Rapoport (Microsoft)
  2026-07-13  7:17 ` [PATCH v2 5/5] IB/rdmavt: use kzalloc() to allocate QPN-map pages Mike Rapoport (Microsoft)
  4 siblings, 0 replies; 6+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-07-13  7:17 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Dennis Dalessandro, Mike Rapoport, linux-kernel, linux-mm,
	linux-rdma

mthca_reg_user_mr() allocates an array of DMA addresses during memory
registration.

This buffer can be allocated with kmalloc() as there's nothing special
about it to go directly to the page allocator.

kmalloc() provides a better API that does not require ugly casts and
kfree() does not need to know the size of the freed object.

Performance difference between kmalloc() and __get_free_pages() is not
measurable as both allocators take an object/page from a per-CPU list for
fast path allocations.

For the slow path the performance is anyway determined by the amount of
reclaim involved rather than by what allocator is used.

Replace use of __get_free_page() with kmalloc() and free_page() with
kfree().

Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
 drivers/infiniband/hw/mthca/mthca_provider.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/infiniband/hw/mthca/mthca_provider.c b/drivers/infiniband/hw/mthca/mthca_provider.c
index f90f67afc8fa..9940165781e1 100644
--- a/drivers/infiniband/hw/mthca/mthca_provider.c
+++ b/drivers/infiniband/hw/mthca/mthca_provider.c
@@ -895,7 +895,8 @@ static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
 		goto err_umem;
 	}
 
-	pages = (u64 *) __get_free_page(GFP_KERNEL);
+	/* TODO: switch to "fast and as large as possible" allocation helper */
+	pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!pages) {
 		err = -ENOMEM;
 		goto err_mtt;
@@ -924,7 +925,7 @@ static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
 	if (i)
 		err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
 mtt_done:
-	free_page((unsigned long) pages);
+	kfree(pages);
 	if (err)
 		goto err_mtt;
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 4/5] IB/mthca: allocate mthca_array memory with kzalloc()
  2026-07-13  7:17 [PATCH v2 0/5] RDMA, IB: replace __get_free_pages() with kmalloc() Mike Rapoport (Microsoft)
                   ` (2 preceding siblings ...)
  2026-07-13  7:17 ` [PATCH v2 3/5] IB/mthca: mthca_reg_user_mr(): use kmalloc() to allocate addresses array Mike Rapoport (Microsoft)
@ 2026-07-13  7:17 ` Mike Rapoport (Microsoft)
  2026-07-13  7:17 ` [PATCH v2 5/5] IB/rdmavt: use kzalloc() to allocate QPN-map pages Mike Rapoport (Microsoft)
  4 siblings, 0 replies; 6+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-07-13  7:17 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Dennis Dalessandro, Mike Rapoport, linux-kernel, linux-mm,
	linux-rdma

mthca_array is essentially a sparse array of pointers and there is no
need to allocate its memory using page allocator.

kmalloc() provides a better API that does not require ugly casts and
kfree() does not need to know the size of the freed object.

Performance difference between kmalloc() and __get_free_pages() is not
measurable as both allocators take an object/page from a per-CPU list for
fast path allocations.

For the slow path the performance is anyway determined by the amount of
reclaim involved rather than by what allocator is used.

Replace use of get_zeroed_page() with kzalloc() and free_page() with
kfree().

Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
 drivers/infiniband/hw/mthca/mthca_allocator.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/infiniband/hw/mthca/mthca_allocator.c b/drivers/infiniband/hw/mthca/mthca_allocator.c
index dedc301235a0..117a070e784e 100644
--- a/drivers/infiniband/hw/mthca/mthca_allocator.c
+++ b/drivers/infiniband/hw/mthca/mthca_allocator.c
@@ -126,7 +126,7 @@ int mthca_array_set(struct mthca_array *array, int index, void *value)
 
 	/* Allocate with GFP_ATOMIC because we'll be called with locks held. */
 	if (!array->page_list[p].page)
-		array->page_list[p].page = (void **) get_zeroed_page(GFP_ATOMIC);
+		array->page_list[p].page = kzalloc(PAGE_SIZE, GFP_ATOMIC);
 
 	if (!array->page_list[p].page)
 		return -ENOMEM;
@@ -142,7 +142,7 @@ void mthca_array_clear(struct mthca_array *array, int index)
 	int p = (index * sizeof (void *)) >> PAGE_SHIFT;
 
 	if (--array->page_list[p].used == 0) {
-		free_page((unsigned long) array->page_list[p].page);
+		kfree(array->page_list[p].page);
 		array->page_list[p].page = NULL;
 	} else
 		array->page_list[p].page[index & MTHCA_ARRAY_MASK] = NULL;
@@ -174,7 +174,7 @@ void mthca_array_cleanup(struct mthca_array *array, int nent)
 	int i;
 
 	for (i = 0; i < (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE; ++i)
-		free_page((unsigned long) array->page_list[i].page);
+		kfree(array->page_list[i].page);
 
 	kfree(array->page_list);
 }

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 5/5] IB/rdmavt: use kzalloc() to allocate QPN-map pages
  2026-07-13  7:17 [PATCH v2 0/5] RDMA, IB: replace __get_free_pages() with kmalloc() Mike Rapoport (Microsoft)
                   ` (3 preceding siblings ...)
  2026-07-13  7:17 ` [PATCH v2 4/5] IB/mthca: allocate mthca_array memory with kzalloc() Mike Rapoport (Microsoft)
@ 2026-07-13  7:17 ` Mike Rapoport (Microsoft)
  4 siblings, 0 replies; 6+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-07-13  7:17 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Dennis Dalessandro, Mike Rapoport, linux-kernel, linux-mm,
	linux-rdma

get_map_page() allocates bitmap pages using get_zeroed_page().

The bitmaps can be allocated with kmalloc() as there's nothing special
about them to go directly to the page allocator.

kmalloc() provides a better API that does not require ugly casts and
kfree() does not need to know the size of the freed object.

Performance difference between kmalloc() and __get_free_pages() is not
measurable as both allocators take an object/page from a per-CPU list for
fast path allocations.

For the slow path the performance is anyway determined by the amount of
reclaim involved rather than by what allocator is used.

Replace use of get_zeroed_page() with kzalloc() and free_page() with
kfree().

Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
 drivers/infiniband/sw/rdmavt/qp.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/infiniband/sw/rdmavt/qp.c b/drivers/infiniband/sw/rdmavt/qp.c
index 70e7d08fdce6..c40cce69e945 100644
--- a/drivers/infiniband/sw/rdmavt/qp.c
+++ b/drivers/infiniband/sw/rdmavt/qp.c
@@ -263,7 +263,7 @@ static inline bool wss_exceeds_threshold(struct rvt_wss *wss)
 static void get_map_page(struct rvt_qpn_table *qpt,
 			 struct rvt_qpn_map *map)
 {
-	unsigned long page = get_zeroed_page(GFP_KERNEL);
+	void *page = kzalloc(PAGE_SIZE, GFP_KERNEL);
 
 	/*
 	 * Free the page if someone raced with us installing it.
@@ -271,9 +271,9 @@ static void get_map_page(struct rvt_qpn_table *qpt,
 
 	spin_lock(&qpt->lock);
 	if (map->page)
-		free_page(page);
+		kfree(page);
 	else
-		map->page = (void *)page;
+		map->page = page;
 	spin_unlock(&qpt->lock);
 }
 
@@ -343,7 +343,7 @@ static void free_qpn_table(struct rvt_qpn_table *qpt)
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
-		free_page((unsigned long)qpt->map[i].page);
+		kfree(qpt->map[i].page);
 }
 
 /**

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-07-13  7:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-13  7:17 [PATCH v2 0/5] RDMA, IB: replace __get_free_pages() with kmalloc() Mike Rapoport (Microsoft)
2026-07-13  7:17 ` [PATCH v2 1/5] RDMA/umem: ib_umem_get(): use kmalloc() to allocate page array Mike Rapoport (Microsoft)
2026-07-13  7:17 ` [PATCH v2 2/5] RDMA/mlx5: replace __get_free_page() with kmalloc() Mike Rapoport (Microsoft)
2026-07-13  7:17 ` [PATCH v2 3/5] IB/mthca: mthca_reg_user_mr(): use kmalloc() to allocate addresses array Mike Rapoport (Microsoft)
2026-07-13  7:17 ` [PATCH v2 4/5] IB/mthca: allocate mthca_array memory with kzalloc() Mike Rapoport (Microsoft)
2026-07-13  7:17 ` [PATCH v2 5/5] IB/rdmavt: use kzalloc() to allocate QPN-map pages Mike Rapoport (Microsoft)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox