Skip to contentSkip to conversation

ptp: ocp: replace __get_free_page() with kmalloc()

PATCH Applied Mike Rapoport (Microsoft) 3 participants 3 messages Nov 8 – Nov 12

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

PATCH 1/1 ptp: ocp: replace __get_free_page() with kmalloc()

ptp_ocp_summary_show() allocates a temporary buffer for formatting PTP OCP device summary information.

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

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

Largely, anything that doesn't need struct page (or a memdesc in the future) should just use kmalloc() or kvmalloc() to allocate memory. Removing needless calls to page allocator helps with memdesc (aka project folio) conversion and gives a nicer API without casts.

---
 drivers/ptp/ptp_ocp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/ptp/ptp_ocp.c +3 -2
index 625a85b2dc7e5..decc56892059b 100644
@@ -18,6 +18,7 @@
 #include <linux/spi/spi.h>
 #include <linux/spi/xilinx_spi.h>
 #include <linux/spi/altera.h>
+#include <linux/slab.h>
 #include <net/devlink.h>
 #include <linux/i2c.h>
 #include <linux/iopoll.h>
@@ -5281,7 +5282,7 @@ ptp_ocp_summary_show(struct seq_file *s, void *data)
 	bool on, map;
 	int i;
 
-	buf = (char *)__get_free_page(GFP_KERNEL);
+	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 
@@ -5497,7 +5498,7 @@ ptp_ocp_summary_show(struct seq_file *s, void *data)
 			   post_ns - pre_ns);
 	}
 
-	free_page((unsigned long)buf);
+	kfree(buf);
 	return 0;
 }
 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary);

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260624-b4-ptp-ed56a38a6afd

--
Sincerely yours,
Mike.

maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 68 lines · Mike Rapoport (Microsoft)

ptp_ocp_summary_show() allocates a temporary buffer for formatting PTP OCP device summary information.

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

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

Largely, anything that doesn't need struct page (or a memdesc in the future) should just use kmalloc() or kvmalloc() to allocate memory. Removing needless calls to page allocator helps with memdesc (aka project folio) conversion and gives a nicer API without casts.

---
  drivers/ptp/ptp_ocp.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
drivers/ptp/ptp_ocp.c +3 -3
index 625a85b2dc7e5..decc56892059b 100644
@@ -18,6 +18,7 @@
  #include <linux/spi/spi.h>
  #include <linux/spi/xilinx_spi.h>
  #include <linux/spi/altera.h>
+#include <linux/slab.h>
  #include <net/devlink.h>
  #include <linux/i2c.h>
  #include <linux/iopoll.h>
@@ -5281,7 +5282,7 @@ ptp_ocp_summary_show(struct seq_file *s, void *data)
  	bool on, map;
  	int i;
 
-	buf = (char *)__get_free_page(GFP_KERNEL);
+	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  	if (!buf)
  		return -ENOMEM;
 
@@ -5497,7 +5498,7 @@ ptp_ocp_summary_show(struct seq_file *s, void *data)
  			   post_ns - pre_ns);
  	}
 
-	free_page((unsigned long)buf);
+	kfree(buf);
  	return 0;
  }
  DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary);
 
---

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260624-b4-ptp-ed56a38a6afd

Quoted 4 lines

--
Sincerely yours,
Mike.

Thanks,

#2 lore raw
In reply to Mike Rapoport (Microsoft)
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

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

Quoted 10 lines · On Sun, 08 Nov 2026 10:40:36 +0200 you wrote:

ptp_ocp_summary_show() allocates a temporary buffer for formatting PTP OCP device summary information.

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.

[...]

Here is the summary with links:
- [net-next] ptp: ocp: replace __get_free_page() with kmalloc()

    https://git.kernel.org/netdev/net-next/c/c88fd6989aea

You are awesome, thank you!

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