Skip to contentSkip to conversation

drivers/net/ethernet: replace __get_free_pages() with kmalloc()

PATCH Applied Mike Rapoport (Microsoft) 5 participants 14 messages Jul 1 – Jul 8

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

cover drivers/net/ethernet: replace __get_free_pages() with kmalloc()

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/drivers-net-ethernet

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

---
v2 changes:
- split out ethernet drivers from a larger set

v1: https://patch.msgid.link/[email protected]

---
Mike Rapoport (Microsoft) (4):
bnx2x: use kzalloc() to allocate mac filtering list
ice: use kzalloc() to allocate staging buffer for reading from GNSS sfc/siena: use kmalloc() to allocate logging buffer
sfc: use kmalloc() to allocate logging buffer

 drivers/net/ethernet/broadcom/bnx2x/bnx2x_sp.c | 6 +++---
 drivers/net/ethernet/intel/ice/ice_gnss.c      | 5 +++--
 drivers/net/ethernet/sfc/mcdi.c                | 7 ++++---
 drivers/net/ethernet/sfc/siena/mcdi.c          | 7 ++++---
 4 files changed, 14 insertions(+), 11 deletions(-)

---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260630-b4-drivers-ethernet-b5e085b98ab1

Best regards,

Signature · 2 lines
Sincerely yours,
Mike.
#1 lore raw

PATCH 1/4 bnx2x: use kzalloc() to allocate mac filtering list

bnx2x_mcast_enqueue_cmd() allocates memory for mac filtering list using __get_free_pages().

This memory can be allocated with kzalloc() 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 kzalloc() and free_page() with kfree().

---
 drivers/net/ethernet/broadcom/bnx2x/bnx2x_sp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/ethernet/broadcom/bnx2x/bnx2x_sp.c +3 -3
index 07a908a2c72f..d560524d317d 100644
@@ -26,6 +26,7 @@
 #include <linux/netdevice.h>
 #include <linux/etherdevice.h>
 #include <linux/crc32c.h>
+#include <linux/slab.h>
 #include "bnx2x.h"
 #include "bnx2x_cmn.h"
 #include "bnx2x_sp.h"
@@ -2664,7 +2665,7 @@ static void bnx2x_free_groups(struct list_head *mcast_group_list)
 				      struct bnx2x_mcast_elem_group,
 				      mcast_group_link);
 		list_del(&current_mcast_group->mcast_group_link);
-		free_page((unsigned long)current_mcast_group);
+		kfree(current_mcast_group);
 	}
 }
 
@@ -2713,8 +2714,7 @@ static int bnx2x_mcast_enqueue_cmd(struct bnx2x *bp,
 				total_elems = BNX2X_MCAST_BINS_NUM;
 		}
 		while (total_elems > 0) {
-			elem_group = (struct bnx2x_mcast_elem_group *)
-				     __get_free_page(GFP_ATOMIC | __GFP_ZERO);
+			elem_group = kzalloc(PAGE_SIZE, GFP_ATOMIC);
 			if (!elem_group) {
 				bnx2x_free_groups(&new_cmd->group_head);
 				kfree(new_cmd);
Signature · 1 line
2.53.0
maintainer
#2 lore raw

Re: [PATCH 1/4] bnx2x: use kzalloc() to allocate mac filtering list

drivers/net/ethernet/broadcom/bnx2x/bnx2x_sp.c static int bnx2x_mcast_enqueue_cmd(struct bnx2x *bp, · line 2717

Quoted 8 lines
drivers/net/ethernet/broadcom/bnx2x/bnx2x_sp.c +1 -2
@@ -2713,8 +2714,7 @@ static int bnx2x_mcast_enqueue_cmd(struct bnx2x *bp,
  				total_elems = BNX2X_MCAST_BINS_NUM;
  		}
  		while (total_elems > 0) {
-			elem_group = (struct bnx2x_mcast_elem_group *)
-				     __get_free_page(GFP_ATOMIC | __GFP_ZERO);
+			elem_group = kzalloc(PAGE_SIZE, GFP_ATOMIC);

what is the current rule of thumb for kzalloc vs kvzalloc size under GFP_ATOMIC?

Quoted 4 lines
drivers/net/ethernet/broadcom/bnx2x/bnx2x_sp.c +0 -0
  			if (!elem_group) {
  				bnx2x_free_groups(&new_cmd->group_head);
  				kfree(new_cmd);
#3 lore raw

Re: [PATCH 1/4] bnx2x: use kzalloc() to allocate mac filtering list

Quoted 11 lines · Przemek Kitszel

drivers/net/ethernet/broadcom/bnx2x/bnx2x_sp.c static int bnx2x_mcast_enqueue_cmd(struct bnx2x *bp, · line 2717

Quoted 8 lines
drivers/net/ethernet/broadcom/bnx2x/bnx2x_sp.c +1 -2
@@ -2713,8 +2714,7 @@ static int bnx2x_mcast_enqueue_cmd(struct bnx2x *bp,
  				total_elems = BNX2X_MCAST_BINS_NUM;
  		}
  		while (total_elems > 0) {
-			elem_group = (struct bnx2x_mcast_elem_group *)
-				     __get_free_page(GFP_ATOMIC | __GFP_ZERO);
+			elem_group = kzalloc(PAGE_SIZE, GFP_ATOMIC);

what is the current rule of thumb for kzalloc vs kvzalloc size under GFP_ATOMIC?

kvzalloc() with GFP_ATOMIC is kzalloc():

	/*
	 * vmalloc uses GFP_KERNEL for some internal allocations (e.g page tables)
	 * so the given set of flags has to be compatible.
	 */
	if ((flags & GFP_KERNEL) != GFP_KERNEL)
		return kmalloc_node(size, flags, node);

so it's only physically contiguous allocations.

Quoted 5 lines
Quoted 4 lines
drivers/net/ethernet/broadcom/bnx2x/bnx2x_sp.c +0 -0
  			if (!elem_group) {
  				bnx2x_free_groups(&new_cmd->group_head);
  				kfree(new_cmd);
Signature · 2 lines
Sincerely yours,
Mike.
#4 lore raw

PATCH 2/4 ice: use kzalloc() to allocate staging buffer for reading from GNSS

In reply to Mike Rapoport (Microsoft)

ice_gnss_read() uses get_zeroed_page() to allocate a staging buffer for reading GNSS module data via I2C bus.

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_zeroed_page() with kzalloc() and free_page() with kfree().

---
 drivers/net/ethernet/intel/ice/ice_gnss.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/net/ethernet/intel/ice/ice_gnss.c +3 -2
index 8fd954f1ebd6..7d21c3417b0b 100644
@@ -2,6 +2,7 @@
 /* Copyright (C) 2021-2022, Intel Corporation. */
 
 #include "ice.h"
+#include <linux/slab.h>
 #include "ice_lib.h"
 
 /**
@@ -124,7 +125,7 @@ static void ice_gnss_read(struct kthread_work *work)
 
 	data_len = min_t(typeof(data_len), data_len, PAGE_SIZE);
 
-	buf = (char *)get_zeroed_page(GFP_KERNEL);
+	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!buf) {
 		err = -ENOMEM;
 		goto requeue;
@@ -151,7 +152,7 @@ static void ice_gnss_read(struct kthread_work *work)
 			 count, i);
 	delay = ICE_GNSS_TIMER_DELAY_TIME;
 free_buf:
-	free_page((unsigned long)buf);
+	kfree(buf);
 requeue:
 	kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, delay);
 	if (err)
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 2/4
#5 lore raw

Re: [PATCH 2/4] ice: use kzalloc() to allocate staging buffer for reading from GNSS

Quoted 38 lines · Mike Rapoport (Microsoft)

ice_gnss_read() uses get_zeroed_page() to allocate a staging buffer for reading GNSS module data via I2C bus.

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_zeroed_page() with kzalloc() and free_page() with kfree().

---
  drivers/net/ethernet/intel/ice/ice_gnss.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
drivers/net/ethernet/intel/ice/ice_gnss.c +1 -0
index 8fd954f1ebd6..7d21c3417b0b 100644
@@ -2,6 +2,7 @@
  /* Copyright (C) 2021-2022, Intel Corporation. */
 
  #include "ice.h"
+#include <linux/slab.h>
  #include "ice_lib.h"
 
  /**

drivers/net/ethernet/intel/ice/ice_gnss.c static void ice_gnss_read(struct kthread_work *work) · line 128

Quoted 5 lines
drivers/net/ethernet/intel/ice/ice_gnss.c +1 -1
@@ -124,7 +125,7 @@ static void ice_gnss_read(struct kthread_work *work)
 
  	data_len = min_t(typeof(data_len), data_len, PAGE_SIZE);
 
-	buf = (char *)get_zeroed_page(GFP_KERNEL);
+	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);

nit:
from the code it is clear that we read at most a page, and @data_len stores the actual amount needed

comment:
I don't know why we limit to a page, it's outside of the scope of this series, but likely you have removed the limit (which will go into the loop - single AQ call is likely limited by a PAGE too).

Quoted 13 lines
drivers/net/ethernet/intel/ice/ice_gnss.c +1 -1
  	if (!buf) {
  		err = -ENOMEM;
  		goto requeue;
@@ -151,7 +152,7 @@ static void ice_gnss_read(struct kthread_work *work)
  			 count, i);
  	delay = ICE_GNSS_TIMER_DELAY_TIME;
  free_buf:
-	free_page((unsigned long)buf);
+	kfree(buf);
  requeue:
  	kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, delay);
  	if (err)
#6 lore raw

Re: [PATCH 2/4] ice: use kzalloc() to allocate staging buffer for reading from GNSS

Quoted 45 lines · Przemek Kitszel
Quoted 36 lines · Mike Rapoport (Microsoft)

ice_gnss_read() uses get_zeroed_page() to allocate a staging buffer for reading GNSS module data via I2C bus.

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_zeroed_page() with kzalloc() and free_page() with kfree().

---
  drivers/net/ethernet/intel/ice/ice_gnss.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
drivers/net/ethernet/intel/ice/ice_gnss.c +1 -0
index 8fd954f1ebd6..7d21c3417b0b 100644
@@ -2,6 +2,7 @@
  /* Copyright (C) 2021-2022, Intel Corporation. */
  #include "ice.h"
+#include <linux/slab.h>
  #include "ice_lib.h"
  /**

drivers/net/ethernet/intel/ice/ice_gnss.c static void ice_gnss_read(struct kthread_work *work) · line 126

Quoted 3 lines
drivers/net/ethernet/intel/ice/ice_gnss.c +1 -1
@@ -124,7 +125,7 @@ static void ice_gnss_read(struct kthread_work *work)
  	data_len = min_t(typeof(data_len), data_len, PAGE_SIZE);
-	buf = (char *)get_zeroed_page(GFP_KERNEL);
+	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);

nit:
from the code it is clear that we read at most a page, and @data_len stores the actual amount needed

Quoted 4 lines

comment:
I don't know why we limit to a page, it's outside of the scope of this series, but likely you have removed the limit (which will go into the loop - single AQ call is likely limited by a PAGE too).

Not sure I follow how the limit changed. buf remains PAGE_SIZE regardless of allocation API.

Quoted 1 line

Thanks!

Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#7 lore raw

Re: [Intel-wired-lan] [PATCH 2/4] ice: use kzalloc() to allocate staging buffer for reading from GNSS

Quoted 46 lines · Mike Rapoport
Quoted 45 lines · Przemek Kitszel
Quoted 36 lines · Mike Rapoport (Microsoft)

ice_gnss_read() uses get_zeroed_page() to allocate a staging buffer for reading GNSS module data via I2C bus.

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_zeroed_page() with kzalloc() and free_page() with kfree().

---
   drivers/net/ethernet/intel/ice/ice_gnss.c | 5 +++--
   1 file changed, 3 insertions(+), 2 deletions(-)
drivers/net/ethernet/intel/ice/ice_gnss.c +1 -0
index 8fd954f1ebd6..7d21c3417b0b 100644
@@ -2,6 +2,7 @@
   /* Copyright (C) 2021-2022, Intel Corporation. */
   #include "ice.h"
+#include <linux/slab.h>
   #include "ice_lib.h"
   /**

drivers/net/ethernet/intel/ice/ice_gnss.c static void ice_gnss_read(struct kthread_work *work) · line 126

Quoted 3 lines
drivers/net/ethernet/intel/ice/ice_gnss.c +1 -1
@@ -124,7 +125,7 @@ static void ice_gnss_read(struct kthread_work *work)
   	data_len = min_t(typeof(data_len), data_len, PAGE_SIZE);
-	buf = (char *)get_zeroed_page(GFP_KERNEL);
+	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);

nit:
from the code it is clear that we read at most a page, and @data_len stores the actual amount needed

Quoted 7 lines
Quoted 4 lines

comment:
I don't know why we limit to a page, it's outside of the scope of this series, but likely you have removed the limit (which will go into the loop - single AQ call is likely limited by a PAGE too).

Not sure I follow how the limit changed. buf remains PAGE_SIZE regardless of allocation API.

data_len passed by the caller could be bigger than PAGE_SIZE, we cap it to PAGE_SIZE, likely due to the old allocation method
but I don't insist on any change to this patch (esp. given all other are fine)

Quoted 5 lines
Quoted 2 lines

Thanks!

We are fine to merge it directly via netdev.

Reviewed-by on v1 2/4
#8 lore raw

RE: [Intel-wired-lan] [PATCH 2/4] ice: use kzalloc() to allocate staging buffer for reading from GNSS

In reply to Mike Rapoport (Microsoft)
Quoted 80 lines

-----Original Message-----
From: Intel-wired-lan <[email protected]> On Behalf Of Mike Rapoport (Microsoft)
Sent: Wednesday, July 1, 2026 3:57 PM
To: Andrew Lunn <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Manish Chopra <[email protected]>; Paolo Abeni <[email protected]>
Cc: Edward Cree <[email protected]>; Kitszel, Przemyslaw <[email protected]>; Sudarsana Kalluru <[email protected]>; Nguyen, Anthony L <[email protected]>; Mike Rapoport <[email protected]>; intel- [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]
Subject: [Intel-wired-lan] [PATCH 2/4] ice: use kzalloc() to allocate staging buffer for reading from GNSS

ice_gnss_read() uses get_zeroed_page() to allocate a staging buffer for reading GNSS module data via I2C bus.

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_zeroed_page() with kzalloc() and free_page() with kfree().

[email protected]

---
 drivers/net/ethernet/intel/ice/ice_gnss.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/net/ethernet/intel/ice/ice_gnss.c +2 -1
index 8fd954f1ebd6..7d21c3417b0b 100644
@@ -2,6 +2,7 @@
 /* Copyright (C) 2021-2022, Intel Corporation. */
 
 #include "ice.h"
+#include <linux/slab.h>
 #include "ice_lib.h"
 
 /**
@@ -124,7 +125,7 @@ static void ice_gnss_read(struct kthread_work
 *work)
 
 	data_len = min_t(typeof(data_len), data_len, PAGE_SIZE);
 
-	buf = (char *)get_zeroed_page(GFP_KERNEL);
+	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!buf) {
 		err = -ENOMEM;
drivers/net/ethernet/intel/ice/ice_gnss.c +1 -1
 		goto requeue;
@@ -151,7 +152,7 @@ static void ice_gnss_read(struct kthread_work
 *work)
 			 count, i);
 	delay = ICE_GNSS_TIMER_DELAY_TIME;
 free_buf:
-	free_page((unsigned long)buf);
+	kfree(buf);
 requeue:
 	kthread_queue_delayed_work(gnss->kworker, &gnss->read_work,

delay);
if (err)

Quoted 2 lines

--
2.53.0

#9 lore raw

PATCH 3/4 sfc/siena: use kmalloc() to allocate logging buffer

In reply to Mike Rapoport (Microsoft)

efx_siena_mcdi_init() allocates a logging buffer for MCDI firmware communication diagnostics.

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().

---
 drivers/net/ethernet/sfc/siena/mcdi.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/ethernet/sfc/siena/mcdi.c +4 -3
index 4d0d6bd5d3d1..048c1e6017c0 100644
@@ -7,6 +7,7 @@
 #include <linux/delay.h>
 #include <linux/moduleparam.h>
 #include <linux/atomic.h>
+#include <linux/slab.h>
 #include "net_driver.h"
 #include "nic.h"
 #include "io.h"
@@ -73,7 +74,7 @@ int efx_siena_mcdi_init(struct efx_nic *efx)
 	mcdi->efx = efx;
 #ifdef CONFIG_SFC_SIENA_MCDI_LOGGING
 	/* consuming code assumes buffer is page-sized */
-	mcdi->logging_buffer = (char *)__get_free_page(GFP_KERNEL);
+	mcdi->logging_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!mcdi->logging_buffer)
 		goto fail1;
 	mcdi->logging_enabled = efx_siena_mcdi_logging_default;
@@ -116,7 +117,7 @@ int efx_siena_mcdi_init(struct efx_nic *efx)
 	return 0;
 fail2:
 #ifdef CONFIG_SFC_SIENA_MCDI_LOGGING
-	free_page((unsigned long)mcdi->logging_buffer);
+	kfree(mcdi->logging_buffer);
 fail1:
 #endif
 	kfree(efx->mcdi);
@@ -142,7 +143,7 @@ void efx_siena_mcdi_fini(struct efx_nic *efx)
 		return;
 
 #ifdef CONFIG_SFC_SIENA_MCDI_LOGGING
-	free_page((unsigned long)efx->mcdi->iface.logging_buffer);
+	kfree(efx->mcdi->iface.logging_buffer);
 #endif
 
 	kfree(efx->mcdi);
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 3/4
#10 lore raw

Re: [PATCH 3/4] sfc/siena: use kmalloc() to allocate logging buffer

Quoted 13 lines · Mike Rapoport (Microsoft)

efx_siena_mcdi_init() allocates a logging buffer for MCDI firmware communication diagnostics.

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.

Quoted 8 lines

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().

(resending since I hit 'reply' instead of 'reply all' the first time)

Quoted 44 lines
---
 drivers/net/ethernet/sfc/siena/mcdi.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/ethernet/sfc/siena/mcdi.c +4 -3
index 4d0d6bd5d3d1..048c1e6017c0 100644
@@ -7,6 +7,7 @@
 #include <linux/delay.h>
 #include <linux/moduleparam.h>
 #include <linux/atomic.h>
+#include <linux/slab.h>
 #include "net_driver.h"
 #include "nic.h"
 #include "io.h"
@@ -73,7 +74,7 @@ int efx_siena_mcdi_init(struct efx_nic *efx)
 	mcdi->efx = efx;
 #ifdef CONFIG_SFC_SIENA_MCDI_LOGGING
 	/* consuming code assumes buffer is page-sized */
-	mcdi->logging_buffer = (char *)__get_free_page(GFP_KERNEL);
+	mcdi->logging_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!mcdi->logging_buffer)
 		goto fail1;
 	mcdi->logging_enabled = efx_siena_mcdi_logging_default;
@@ -116,7 +117,7 @@ int efx_siena_mcdi_init(struct efx_nic *efx)
 	return 0;
 fail2:
 #ifdef CONFIG_SFC_SIENA_MCDI_LOGGING
-	free_page((unsigned long)mcdi->logging_buffer);
+	kfree(mcdi->logging_buffer);
 fail1:
 #endif
 	kfree(efx->mcdi);
@@ -142,7 +143,7 @@ void efx_siena_mcdi_fini(struct efx_nic *efx)
 		return;
 
 #ifdef CONFIG_SFC_SIENA_MCDI_LOGGING
-	free_page((unsigned long)efx->mcdi->iface.logging_buffer);
+	kfree(efx->mcdi->iface.logging_buffer);
 #endif
 
 	kfree(efx->mcdi);
#11 lore raw

PATCH 4/4 sfc: use kmalloc() to allocate logging buffer

In reply to Mike Rapoport (Microsoft)

efx_mcdi_init() allocates a logging buffer for MCDI firmware communication diagnostics.

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().

---
 drivers/net/ethernet/sfc/mcdi.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/ethernet/sfc/mcdi.c +4 -3
index e65db9b70724..b806d3d90c42 100644
@@ -7,6 +7,7 @@
 #include <linux/delay.h>
 #include <linux/moduleparam.h>
 #include <linux/atomic.h>
+#include <linux/slab.h>
 #include "net_driver.h"
 #include "nic.h"
 #include "io.h"
@@ -71,7 +72,7 @@ int efx_mcdi_init(struct efx_nic *efx)
 	mcdi->efx = efx;
 #ifdef CONFIG_SFC_MCDI_LOGGING
 	/* consuming code assumes buffer is page-sized */
-	mcdi->logging_buffer = (char *)__get_free_page(GFP_KERNEL);
+	mcdi->logging_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!mcdi->logging_buffer)
 		goto fail1;
 	mcdi->logging_enabled = mcdi_logging_default;
@@ -112,7 +113,7 @@ int efx_mcdi_init(struct efx_nic *efx)
 	return 0;
 fail2:
 #ifdef CONFIG_SFC_MCDI_LOGGING
-	free_page((unsigned long)mcdi->logging_buffer);
+	kfree(mcdi->logging_buffer);
 fail1:
 #endif
 	kfree(efx->mcdi);
@@ -138,7 +139,7 @@ void efx_mcdi_fini(struct efx_nic *efx)
 		return;
 
 #ifdef CONFIG_SFC_MCDI_LOGGING
-	free_page((unsigned long)efx->mcdi->iface.logging_buffer);
+	kfree(efx->mcdi->iface.logging_buffer);
 #endif
 
 	kfree(efx->mcdi);
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 4/4
#12 lore raw

Re: [PATCH 4/4] sfc: use kmalloc() to allocate logging buffer

Quoted 13 lines · Mike Rapoport (Microsoft)

efx_mcdi_init() allocates a logging buffer for MCDI firmware communication diagnostics.

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.

Quoted 8 lines

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().

Quoted 44 lines
---
 drivers/net/ethernet/sfc/mcdi.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/ethernet/sfc/mcdi.c +4 -3
index e65db9b70724..b806d3d90c42 100644
@@ -7,6 +7,7 @@
 #include <linux/delay.h>
 #include <linux/moduleparam.h>
 #include <linux/atomic.h>
+#include <linux/slab.h>
 #include "net_driver.h"
 #include "nic.h"
 #include "io.h"
@@ -71,7 +72,7 @@ int efx_mcdi_init(struct efx_nic *efx)
 	mcdi->efx = efx;
 #ifdef CONFIG_SFC_MCDI_LOGGING
 	/* consuming code assumes buffer is page-sized */
-	mcdi->logging_buffer = (char *)__get_free_page(GFP_KERNEL);
+	mcdi->logging_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!mcdi->logging_buffer)
 		goto fail1;
 	mcdi->logging_enabled = mcdi_logging_default;
@@ -112,7 +113,7 @@ int efx_mcdi_init(struct efx_nic *efx)
 	return 0;
 fail2:
 #ifdef CONFIG_SFC_MCDI_LOGGING
-	free_page((unsigned long)mcdi->logging_buffer);
+	kfree(mcdi->logging_buffer);
 fail1:
 #endif
 	kfree(efx->mcdi);
@@ -138,7 +139,7 @@ void efx_mcdi_fini(struct efx_nic *efx)
 		return;
 
 #ifdef CONFIG_SFC_MCDI_LOGGING
-	free_page((unsigned long)efx->mcdi->iface.logging_buffer);
+	kfree(efx->mcdi->iface.logging_buffer);
 #endif
 
 	kfree(efx->mcdi);
#13 lore raw
In reply to Mike Rapoport (Microsoft)
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

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

Quoted 8 lines · On Wed, 01 Jul 2026 16:57:17 +0300 you wrote:

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.

[...]

Here is the summary with links:
- [1/4] bnx2x: use kzalloc() to allocate mac filtering list

    https://git.kernel.org/netdev/net-next/c/28a236c54c9a
  - [2/4] ice: use kzalloc() to allocate staging buffer for reading from GNSS
    https://git.kernel.org/netdev/net-next/c/764f2a0c6d1e
  - [3/4] sfc/siena: use kmalloc() to allocate logging buffer
    https://git.kernel.org/netdev/net-next/c/50bfc5eac4cb
  - [4/4] sfc: use kmalloc() to allocate logging buffer
    https://git.kernel.org/netdev/net-next/c/e8cfc70720ea

You are awesome, thank you!

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