Skip to contentSkip to conversation

block: partitions: replace __get_free_page() with kmalloc()

PATCH Applied Mike Rapoport (Microsoft) 2 participants 2 messages May 27

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

PATCH v2 1/1 block: partitions: replace __get_free_page() with kmalloc()

check_partition() allocates a buffer to use as backing memory for seq_buf.

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.

For a single allocation on the cold path the 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 (tiny) part of larger work of replacing page allocator calls with kmalloc:

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

---
v2 changes:
* reword changelog

To: Jens Axboe <[email protected]>
Cc: [email protected]
Cc: [email protected]
v1: https://patch.msgid.link/[email protected]

---
 block/partitions/core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
block/partitions/core.c +3 -3
index 5d5332ce586b..b5c59b79ca7c 100644
@@ -124,7 +124,7 @@ static struct parsed_partitions *check_partition(struct gendisk *hd)
 	state = allocate_partitions(hd);
 	if (!state)
 		return NULL;
-	state->pp_buf.buffer = (char *)__get_free_page(GFP_KERNEL);
+	state->pp_buf.buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	if (!state->pp_buf.buffer) {
 		free_partitions(state);
 		return NULL;
@@ -154,7 +154,7 @@ static struct parsed_partitions *check_partition(struct gendisk *hd)
 	if (res > 0) {
 		printk(KERN_INFO "%s", seq_buf_str(&state->pp_buf));
 
-		free_page((unsigned long)state->pp_buf.buffer);
+		kfree(state->pp_buf.buffer);
 		return state;
 	}
 	if (state->access_beyond_eod)
@@ -170,7 +170,7 @@ static struct parsed_partitions *check_partition(struct gendisk *hd)
 		printk(KERN_INFO "%s", seq_buf_str(&state->pp_buf));
 	}
 
-	free_page((unsigned long)state->pp_buf.buffer);
+	kfree(state->pp_buf.buffer);
 	free_partitions(state);
 	return ERR_PTR(res);
 }

---
base-commit: 5d6919055dec134de3c40167a490f33c74c12581
change-id: 20260520-block-25582753fd38

Best regards,

Signature · 2 lines
Sincerely yours,
Mike.
maintainer Applied
#1 lore raw
Quoted 10 lines · Mike Rapoport (Microsoft)

check_partition() allocates a buffer to use as backing memory for seq_buf.

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.

[...]

Applied, thanks!

[1/1] block: partitions: replace __get_free_page() with kmalloc()

      commit: 17d7492a50251d913ae7101f898cf30ede856cde

Best regards,

Signature · 1 line
Jens Axboe