char/misc: replace page allocator calls with k[mz]alloc()
Subsystems (5)
cover char/misc: replace page allocator calls with k[mz]alloc()
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.
The patches are deliberately kept small: each one deals with a single driver or subsystem and with a single type of allocation, so that every conversion can be reviewed, and if needed reverted, on its own.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/CA+55aFwp4iy4rtX2gE2WjBGFL=NxMVnoFeHqYa2j1dYOMMGqxg@mail.gmail.com/
Note that the xillybus 'salami' buffer in xillybus_core.c is left alone: it is allocated with __GFP_DMA32 which kmalloc() does not support.
---
Mike Rapoport (Microsoft) (4):
char: xilinx_hwicap: replace page allocator calls with k[mz]alloc()
char: xillybus: replace __get_free_pages() with kmalloc()
misc: ibmvmc: replace get_zeroed_page() with kzalloc()
platform: goldfish: pipe: replace __get_free_page() with kmalloc()
drivers/char/xilinx_hwicap/xilinx_hwicap.c | 18 +++++++++--------- drivers/char/xillybus/xillyusb.c | 25 +++++++------------------ drivers/misc/ibmvmc.c | 6 +++--- drivers/platform/goldfish/goldfish_pipe.c | 22 ++++++++++------------ 4 files changed, 29 insertions(+), 42 deletions(-)
---
base-commit: 1b78070aaef63512688aebfbc82365ef9d6660f1
change-id: 20260829-char-misc-dcc4e33778fd
--
Sincerely yours,
Mike.
PATCH 1/4 char: xilinx_hwicap: replace page allocator calls with k[mz]alloc()
hwicap_read() and hwicap_write() allocate temporary buffers used to pass the FPGA configuration data to and from userspace.
These buffers 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_free_page() and get_zeroed_page() with kmalloc() and kzalloc() and free_page() with kfree().
--- drivers/char/xilinx_hwicap/xilinx_hwicap.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/char/xilinx_hwicap/xilinx_hwicap.c +9 -9
@@ -382,7 +382,7 @@ hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 4 - bytes_to_read); }else {/* Get new data from the ICAP, and return what was requested. */ - kbuf = (u32 *)get_zeroed_page (GFP_KERNEL ); + kbuf =kzalloc (PAGE_SIZE ,GFP_KERNEL );if (!kbuf) { status = -ENOMEM ; goto error;
@@ -412,13 +412,13 @@ hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)/* If we didn't read correctly, then bail out. */ if (status) { -free_page ((unsigned long )kbuf); +kfree (kbuf); goto error; }/* If we fail to return the data to the user, then bail out. */ if (copy_to_user (buf, kbuf, bytes_to_read)) { -free_page ((unsigned long )kbuf); +kfree (kbuf); status = -EFAULT ; goto error; }
@@ -426,7 +426,7 @@ hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) kbuf, bytes_remaining); drvdata->read_buffer_in_use = bytes_remaining; -free_page ((unsigned long )kbuf); +kfree (kbuf); } status = bytes_to_read; error:
@@ -457,7 +457,7 @@ hwicap_write(struct file *file, const char __user *buf, goto error; } - kbuf = (u32 *)__get_free_page (GFP_KERNEL ); + kbuf =kmalloc (PAGE_SIZE ,GFP_KERNEL );if (!kbuf) { status = -ENOMEM ; goto error;
@@ -479,13 +479,13 @@ hwicap_write(struct file *file, const char __user *buf, (((char *)kbuf) + drvdata->write_buffer_in_use), buf + written, len - (drvdata->write_buffer_in_use))) { -free_page ((unsigned long )kbuf); +kfree (kbuf); status = -EFAULT ; goto error; } }else {if (copy_from_user (kbuf, buf + written, len)) { -free_page ((unsigned long )kbuf); +kfree (kbuf); status = -EFAULT ; goto error; }
@@ -495,7 +495,7 @@ hwicap_write(struct file *file, const char __user *buf, kbuf, len >> 2);if (status) { -free_page ((unsigned long )kbuf); +kfree (kbuf); status = -EFAULT ; goto error; }
@@ -516,7 +516,7 @@ hwicap_write(struct file *file, const char __user *buf, } } -free_page ((unsigned long )kbuf); +kfree (kbuf); status = written; error:mutex_unlock (&drvdata->sem);
Signature · 1 line
2.53.0
PATCH 2/4 char: xillybus: replace __get_free_pages() with kmalloc()
In reply to Mike Rapoport (Microsoft)fifo_init() allocates the buffers backing the software FIFO and endpoint_alloc() allocates the transfer buffers of a USB endpoint.
These buffers 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.
Since kmalloc() does not need the allocation order, ask it for the buffer sizes the driver already tracks and drop the now unused order fields. For a FIFO smaller than a page this also stops rounding the allocation up to PAGE_SIZE.
Replace use of __get_free_pages() with kmalloc() and free_pages() with kfree().
--- drivers/char/xillybus/xillyusb.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-)
drivers/char/xillybus/xillyusb.c +7 -18
@@ -71,7 +71,6 @@ struct xillyfifo {unsigned int bufsize;/* In bytes, always a power of 2 */ unsigned int bufnum;unsigned int size;/* Lazy: Equals bufsize * bufnum */ -unsigned int buf_order;int fill;/* Number of bytes in the FIFO */ spinlock_t lock;
@@ -95,7 +94,6 @@ struct xillyusb_endpoint {struct list_head filled_buffers;spinlock_t buffers_lock;/* protect these two lists */ -unsigned int order;unsigned int buffer_size;unsigned int fill_mask;
@@ -370,7 +368,6 @@ static int fifo_init(struct xillyfifo *fifo, unsigned int log2_size) {unsigned int log2_bufnum; -unsigned int buf_order;int i;unsigned int log2_fifo_buf_size;
@@ -380,18 +377,14 @@ static int fifo_init(struct xillyfifo *fifo,if (log2_size > log2_fifo_buf_size) { log2_bufnum = log2_size - log2_fifo_buf_size; - buf_order = fifo_buf_order; fifo->bufsize =1 << log2_fifo_buf_size; }else { log2_bufnum =0 ; - buf_order = (log2_size >PAGE_SHIFT ) ? - log2_size -PAGE_SHIFT :0 ; fifo->bufsize =1 << log2_size; } fifo->bufnum =1 << log2_bufnum; fifo->size = fifo->bufnum * fifo->bufsize; - fifo->buf_order = buf_order; fifo->mem =kmalloc_array (fifo->bufnum,sizeof (void *),GFP_KERNEL );
@@ -399,8 +392,7 @@ static int fifo_init(struct xillyfifo *fifo,return -ENOMEM ;for (i =0 ; i < fifo->bufnum; i++) { - fifo->mem[i] = (void *) -__get_free_pages (GFP_KERNEL , buf_order); + fifo->mem[i] =kmalloc (fifo->bufsize,GFP_KERNEL );if (!fifo->mem[i]) goto memfail;
@@ -417,7 +409,7 @@ static int fifo_init(struct xillyfifo *fifo, memfail:for (i--; i >=0 ; i--) -free_pages ((unsigned long )fifo->mem[i], buf_order); +kfree (fifo->mem[i]);kfree (fifo->mem); fifo->mem =NULL ;
@@ -438,7 +430,7 @@ static void fifo_mem_release(struct xillyfifo *fifo)return ;for (i =0 ; i < fifo->bufnum; i++) -free_pages ((unsigned long )fifo->mem[i], fifo->buf_order); +kfree (fifo->mem[i]);kfree (fifo->mem); }
@@ -477,7 +469,7 @@ static void endpoint_dealloc(struct xillyusb_endpoint *ep)struct xillybuffer *xb = list_entry(this, struct xillybuffer, entry); -free_pages ((unsigned long )xb->buf, ep->order); +kfree (xb->buf);kfree (xb); }
@@ -509,8 +501,7 @@ static struct xillyusb_endpointinit_usb_anchor (&ep->anchor);INIT_WORK (&ep->workitem, work); - ep->order = order; - ep->buffer_size =1 << (PAGE_SHIFT + order); + ep->buffer_size =1 << (PAGE_SHIFT + order); ep->outstanding_urbs =0 ; ep->drained = true; ep->wake_on_drain = false;
@@ -520,7 +511,6 @@ static struct xillyusb_endpointfor (i =0 ; i < bufnum; i++) {struct xillybuffer *xb; -unsigned long addr; xb =kzalloc_obj (*xb);
@@ -529,15 +519,14 @@ static struct xillyusb_endpointreturn NULL ; } - addr =__get_free_pages (GFP_KERNEL , order); + xb->buf =kmalloc (ep->buffer_size,GFP_KERNEL ); -if (!addr) { +if (!xb->buf) {kfree (xb);endpoint_dealloc (ep);return NULL ; } - xb->buf = (void *)addr; xb->ep = ep;list_add_tail (&xb->entry, &ep->buffers); }
Signature · 1 line
2.53.0
Eli Billauer
maintainerRe: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 5 lines · Mike Rapoport (Microsoft)
fifo_init()allocates the buffers backing the software FIFO andendpoint_alloc()allocates the transfer buffers of a USB endpoint.These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.
Except that they are a bit large.
fifo_init() may be requested to allocate up to 256 MB of buffer memory. This memory resource is split into an array of buffers (fifo->mem[i]), each 64 kiB (or less, if there's trouble obtaining segments of this size).
Citing Documentation/core-api/memory-allocation.rst:
"The maximal size of a chunk that can be allocated with kmalloc is limited. The actual limit depends on the hardware and the kernel configuration, but it is a good practice to use kmalloc for objects smaller than page size."
Regards,
Eli
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 13 lines · Eli Billauer
Quoted 5 lines · Mike Rapoport (Microsoft)
fifo_init()allocates the buffers backing the software FIFO andendpoint_alloc()allocates the transfer buffers of a USB endpoint.These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.Except that they are a bit large.
fifo_init()may be requested to allocate up to 256 MB of buffer memory. This memory resource is split into an array of buffers (fifo->mem[i]), each 64 kiB (or less, if there's trouble obtaining segments of this size).
Quoted 6 lines
Citing
Documentation/core-api/memory-allocation.rst:"The maximal size of a chunk that can be allocated with
kmallocis limited. The actual limit depends on the hardware and the kernel configuration, but it is a good practice to usekmallocfor objects smaller than page size."
For large sizes kmalloc falls back to page allocator anyway.
And the doc is out of date unfortunately.
Quoted 2 lines
Regards,
Eli
Signature · 2 lines
Sincerely yours, Mike.
Eli Billauer
maintainerRe: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 3 lines · Mike Rapoport
For large sizes kmalloc falls back to page allocator anyway.
And the doc is out of date unfortunately.
I think it would be a good idea to update the doc first, and by doing so officially change the API. After getting this change approved, it's indeed safe to rely on kmalloc() even for larger memory allocations.
Regards,
Eli
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 8 lines · Eli Billauer
Quoted 3 lines · Mike Rapoport
For large sizes kmalloc falls back to page allocator anyway.
And the doc is out of date unfortunately.
I think it would be a good idea to update the doc first, and by doing so officially change the API. After getting this change approved, it's indeed safe to rely on
kmalloc()even for larger memory allocations.
The documentation always lags behind the code and "official" API is what include/linux/slab.h says :)
When I wrote that document, SLAB and SLOB were still around and had different properties than SLUB for larger allocations.
Still, the documentation update is on it's way upstream:
https://lore.kernel.org/linux-mm/[email protected]/
Quoted 2 lines
Regards,
Eli
Signature · 2 lines
Sincerely yours, Mike.
Eli Billauer
maintainerRe: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 10 lines · Mike Rapoport
Quoted 7 lines · Eli Billauer
Quoted 3 lines · Mike Rapoport
For large sizes kmalloc falls back to page allocator anyway.
And the doc is out of date unfortunately.
I think it would be a good idea to update the doc first, and by doing so officially change the API. After getting this change approved, it's indeed safe to rely on
kmalloc()even for larger memory allocations.The documentation always lags behind the code and "official" API is what
include/linux/slab.hsays :)
This is what I found in slab.h:
*
* kmalloc is the normal method of allocating memory
* for objects smaller than page size in the kernel. *
Maybe this needs a clarification too?
Regards,
Eli
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 12 lines · Eli Billauer
Quoted 10 lines · Mike Rapoport
Quoted 7 lines · Eli Billauer
Quoted 3 lines · Mike Rapoport
For large sizes kmalloc falls back to page allocator anyway.
And the doc is out of date unfortunately.
I think it would be a good idea to update the doc first, and by doing so officially change the API. After getting this change approved, it's indeed safe to rely on
kmalloc()even for larger memory allocations.The documentation always lags behind the code and "official" API is what
include/linux/slab.hsays :)
Quoted 6 lines
This is what I found in
slab.h:*
* kmalloc is the normal method of allocating memory
* for objects smaller than page size in the kernel. *
Citing vbabka:
that sentence doesn't exclude >PAGE_SIZE allocations :)
Quoted 3 lines
Regards,
Eli
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
In reply to Eli BillauerQuoted 4 lines · On Mon, 31 Aug 2026 12:13:08 +0200 Eli Billauer <[email protected]> wrote:
Quoted 3 lines · Mike Rapoport (Microsoft)
fifo_init()allocates the buffers backing the software FIFO andendpoint_alloc()allocates the transfer buffers of a USB endpoint.
Quoted 8 lines
Quoted 2 lines
These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.Except that they are a bit large.
fifo_init()may be requested to allocate up to 256 MB of buffer memory. This memory resource is split into an array of buffers (fifo->mem[i]), each 64 kiB (or less, if there's trouble obtaining segments of this size).
Would it really make sense to allocate the four buffers separately? And/or use vmalloc().
David
Quoted 11 lines
Citing
Documentation/core-api/memory-allocation.rst:"The maximal size of a chunk that can be allocated with
kmallocis limited. The actual limit depends on the hardware and the kernel configuration, but it is a good practice to usekmallocfor objects smaller than page size."Regards,
Eli
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 10 lines · David Laight
Quoted 7 lines · On Mon, 31 Aug 2026 12:13:08 +0200 Eli Billauer <[email protected]> wrote:
Quoted 5 lines · Mike Rapoport (Microsoft)
fifo_init()allocates the buffers backing the software FIFO andendpoint_alloc()allocates the transfer buffers of a USB endpoint.These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.
Quoted 8 lines
Quoted 5 lines
Except that they are a bit large.
fifo_init()may be requested to allocate up to 256 MB of buffer memory. This memory resource is split into an array of buffers (fifo->mem[i]), each 64 kiB (or less, if there's trouble obtaining segments of this size).Would it really make sense to allocate the four buffers separately? And/or use
vmalloc().
My understanding is that the buffers don't need to be physically contiguous and vmalloc()ing the entire fifo->mem in one go should work.
Eli, what do you think?
Quoted 1 line
David
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 17 lines · On Tue, 1 Sep 2026 10:59:16 +0300 Mike Rapoport <[email protected]> wrote:
Quoted 16 lines · David Laight
Quoted 12 lines · On Mon, 31 Aug 2026 12:13:08 +0200 Eli Billauer <[email protected]> wrote:
Quoted 5 lines · Mike Rapoport (Microsoft)
fifo_init()allocates the buffers backing the software FIFO andendpoint_alloc()allocates the transfer buffers of a USB endpoint.These buffers can be allocated with
kmalloc()as there's nothing special about them to go directly to the page allocator.Except that they are a bit large.
fifo_init()may be requested to allocate up to 256 MB of buffer memory. This memory resource is split into an array of buffers (fifo->mem[i]), each 64 kiB (or less, if there's trouble obtaining segments of this size).
Quoted 5 lines
Quoted 2 lines
Would it really make sense to allocate the four buffers separately? And/or use
vmalloc().My understanding is that the buffers don't need to be physically contiguous and vmalloc()ing the entire
fifo->memin one go should work.
What is the minimum size?
Remember PAGE_SIZE can be 64k.
David
Quoted 5 lines
Eli, what do you think?
Quoted 1 line
David
Eli Billauer
maintainerRe: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
In reply to Mike RapoportQuoted 4 lines · Mike Rapoport
Quoted 2 lines
Would it really make sense to allocate the four buffers separately? And/or use
vmalloc().My understanding is that the buffers don't need to be physically contiguous and vmalloc()ing the entire
fifo->memin one go should work.
vmalloc() is an interesting point.
fifo_init(), fifo_write(), fifo_read() and fifo_mem_release() implement a FIFO in software that the XillyUSB driver uses internally.
The memory for this FIFO is allocated in fifo_init() by calling __get_free_pages() with requests for up to 64 kiB. With the maximal total buffer size of 256 MiB, we have a possibility of 4096 allocations into an array of buffers. And if __get_free_pages() fails, the size of each buffer is halved in the following attempt, which tries to allocate 8192 buffers, each 32 kiB, in this example. And so on.
This mechanism with an array of buffers complicates the implementation of the other functions as well.
So why not replace this with a single call to vmalloc(), possibly asking for 256 MiB in one call? That would mean simplifying all four functions.
When I wrote this driver back in 2020, I avoided vmalloc() because Linus wrote "vmalloc() is NOT SOMETHING YOU SHOULD EVER USE!". (See [1]). He also noted that vmalloc() is a restricted resource. But that's from 2003, so maybe things have changed since?
Questions that arise in this context:
* Does vmalloc() guarantee that non-pageable physical RAM is allocated when it returns?
* Can copy_to/from_user() be used with memory allocated with vmalloc().
* Is vmalloc() guaranteed to successfully allocate memory in the same situation that __get_free_pages() could have been used to obtain the same amount of memory (in smaller chunks, as with fifo_init() )? Maybe they allocate memory from separate memory pools?
And most important: In what way, if at all, is memory obtained with vmalloc() practically different from memory allocated by __get_free_pages(), if it's never used for DMA?
Does the API offer clear answers to these questions?
Thanks in advance,
Eli
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Hi Eli,
Thanks for the detailed explanation!
Quoted 11 lines · Eli Billauer
Quoted 4 lines · Mike Rapoport
Quoted 2 lines
Would it really make sense to allocate the four buffers separately? And/or use
vmalloc().My understanding is that the buffers don't need to be physically contiguous and vmalloc()ing the entire
fifo->memin one go should work.
vmalloc()is an interesting point.
fifo_init(),fifo_write(),fifo_read()andfifo_mem_release()implement a FIFO in software that the XillyUSB driver uses internally.
Quoted 6 lines
The memory for this FIFO is allocated in
fifo_init()by calling__get_free_pages()with requests for up to 64 kiB. With the maximal total buffer size of 256 MiB, we have a possibility of 4096 allocations into an array of buffers. And if__get_free_pages()fails, the size of each buffer is halved in the following attempt, which tries to allocate 8192 buffers, each 32 kiB, in this example. And so on.
With vmalloc() you'd get all 256 MiB in one go if there are indeed free 256 MiB in the system. Unlike get_free_pages()/kmalloc(), vmalloc() does not try to allocate physically contiguous chunks and it's not affected by fragmentation.
Quoted 10 lines
This mechanism with an array of buffers complicates the implementation of the other functions as well.
So why not replace this with a single call to
vmalloc(), possibly asking for 256 MiB in one call? That would mean simplifying all four functions.When I wrote this driver back in 2020, I avoided
vmalloc()because Linus wrote "vmalloc()is NOT SOMETHING YOU SHOULD EVER USE!". (See [1]). He also noted thatvmalloc()is a restricted resource. But that's from 2003, so maybe things have changed since?
I believe so, we have kvmalloc() that falls back from kmalloc() to vmalloc() for larger allocations and we do have about 1k callers of vmalloc() family.
In 2003 the majority of machines that ran Linux were 32 bit and those had limited virtual address space. And yes, vmalloc() is slower than kmalloc() or get_free_pages().
Quoted 4 lines
Questions that arise in this context:
* Does
vmalloc()guarantee that non-pageable physical RAM is allocated when it returns?
It's not pageable in the sense of demand paging. Some architectures lazily synchronize vmalloc page tables and this can cause page faults that will take care of the page table synchronization.
Quoted 1 line
* Can copy_to/from_user() be used with memory allocated with
vmalloc().
Yes.
Quoted 4 lines
* Is
vmalloc()guaranteed to successfully allocate memory in the same situation that__get_free_pages()could have been used to obtain the same amount of memory (in smaller chunks, as withfifo_init())? Maybe they allocate memory from separate memory pools?
The pools are the same in the end, vmalloc() allocates memory using page allocator, just like __get_free_pages(). The difference is that vmalloc() does not try to allocate physically contiguous chunks, but rather a collection of assorted order-0 pages.
This is actually more likely to succeed than multiple large order allocations.
Quoted 3 lines
And most important: In what way, if at all, is memory obtained with
vmalloc()practically different from memory allocated by__get_free_pages(), if it's never used for DMA?
The memory is not physically contiguous and cannot be used for DMA. Some accesses may generate a fault to synchronize the kernel page tables. The memory is there, but some processes may have not-yet-synced page tables.
The allocation itself does more work and it is slower.
Quoted 1 line
Does the API offer clear answers to these questions?
Thanks for the detailed explanation
Quoted 4 lines
Thanks in advance,
Eli
Signature · 2 lines
Sincerely yours, Mike.
Eli Billauer
maintainerRe: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 6 lines · Mike Rapoport
Quoted 2 lines
* Does
vmalloc()guarantee that non-pageable physical RAM is allocated when it returns?It's not pageable in the sense of demand paging. Some architectures lazily synchronize vmalloc page tables and this can cause page faults that will take care of the page table synchronization.
Thanks for this clarification.
This sounds like a showstopper to me. Immediately after the allocation of these buffers, data from the USB device can arrive at 400 MB/s. If the data flow is throttled as a result of handling page faults while trying to write the data to these buffers, this could lead to an overflow in the USB device's own RAM buffers. This is because the USB device is an FPGA, where RAM is an expensive resource.
So using vmalloc() may result in a malfunction in the data transport where the __get_free_pages() would have been successful. This outweighs the benefit of a somewhat higher probability of success in allocating very large buffers, and surely the improved aesthetics of the kernel code.
Regards,
Eli
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 17 lines · Eli Billauer
Quoted 6 lines · Mike Rapoport
Quoted 2 lines
* Does
vmalloc()guarantee that non-pageable physical RAM is allocated when it returns?It's not pageable in the sense of demand paging. Some architectures lazily synchronize vmalloc page tables and this can cause page faults that will take care of the page table synchronization.
Thanks for this clarification.
This sounds like a showstopper to me. Immediately after the allocation of these buffers, data from the USB device can arrive at 400 MB/s. If the data flow is throttled as a result of handling page faults while trying to write the data to these buffers, this could lead to an overflow in the USB device's own RAM buffers. This is because the USB device is an FPGA, where RAM is an expensive resource.
Quoted 4 lines
So using
vmalloc()may result in a malfunction in the data transport where the__get_free_pages()would have been successful. This outweighs the benefit of a somewhat higher probability of success in allocating very large buffers, and surely the improved aesthetics of the kernel code.
kmalloc() still works, just like __get_free_pages()
Quoted 3 lines
Regards,
Eli
Signature · 2 lines
Sincerely yours, Mike.
Eli Billauer
maintainerRe: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 6 lines · Mike Rapoport
Quoted 4 lines
So using
vmalloc()may result in a malfunction in the data transport where the__get_free_pages()would have been successful. This outweighs the benefit of a somewhat higher probability of success in allocating very large buffers, and surely the improved aesthetics of the kernel code.
kmalloc()still works, just like__get_free_pages()
After these few days of discussion, this is what I make of this topic.
The memory allocation in fifo_init() is somewhat ugly, but this driver has been out there for five years, and no complaints from users.
The vmalloc() option may be successful sometimes where the current allocation will not. On the other hand, there is a possibility that it will cause real trouble with page faults. Plus, a large allocation with vmalloc() may be a weak spot on a platform to which the kernel is partially or poorly ported.
With __get_free_pages(), there is no doubt that usable memory is allocated right away, and that it will work that way on every single esoteric platform. vmalloc() leaves us with "we're pretty sure there is probably no problem".
So I vote against vmalloc().
As for replacing __get_free_pages() with kmalloc(), it's a matter of taste. I think that if you're after a buffer that has a power-of-two number of pages, __get_free_pages() is the natural choice, and using it makes it easier for the reader to understand what you're doing. Replacing it with a kmalloc() is confusing in my opinion, and requires that the reader is aware that kmalloc() falls back to __get_free_pages() in this case. So we actually want __get_free_pages(), but call kmalloc() instead and let it fall back to __get_free_pages() so we don't have a cast and don't need to save the order, and assume that everyone knows that this is equivalent.
So I vote against the kmalloc() thing as well.
If this patch is part of a larger kernel cleanup, I suppose my votes don't count for much. But for the record, this is my opinion.
Thanks and regards,
Eli
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 10 lines · Eli Billauer
Quoted 6 lines · Mike Rapoport
Quoted 4 lines
So using
vmalloc()may result in a malfunction in the data transport where the__get_free_pages()would have been successful. This outweighs the benefit of a somewhat higher probability of success in allocating very large buffers, and surely the improved aesthetics of the kernel code.
kmalloc()still works, just like__get_free_pages()After these few days of discussion, this is what I make of this topic.
Quoted 8 lines
The memory allocation in
fifo_init()is somewhat ugly, but this driver has been out there for five years, and no complaints from users.The
vmalloc()option may be successful sometimes where the current allocation will not. On the other hand, there is a possibility that it will cause real trouble with page faults. Plus, a large allocation withvmalloc()may be a weak spot on a platform to which the kernel is partially or poorly ported.
vmalloc() has its limitations, yes...
Quoted 11 lines
With
__get_free_pages(), there is no doubt that usable memory is allocated right away, and that it will work that way on every single esoteric platform.vmalloc()leaves us with "we're pretty sure there is probably no problem".So I vote against
vmalloc().As for replacing
__get_free_pages()withkmalloc(), it's a matter of taste. I think that if you're after a buffer that has a power-of-two number of pages,__get_free_pages()is the natural choice, and using it makes it easier for the reader to understand what you're doing.
If you are only after a buffer (that's physically contiguous and aligned) and don't need the struct page itself, then kmalloc() is the natural choice. This is where we are heading with the upcoming memdesc conversions etc.
Quoted 2 lines
Replacing it with a
kmalloc()is confusing in my opinion, and requires that the reader is aware thatkmalloc()falls back to__get_free_pages()
Why? The reader has only to know that kmalloc() will provide such a buffer (up to sizes that the page allocator would) and whether it falls back to the page allocator or not is an implementation detail.
BTW, it's only at >2*PAGE_SIZE where it falls back. Which could change at any point and nobody should care.
Quoted 4 lines
in this case. So we actually want
__get_free_pages(), but callkmalloc()instead and let it fall back to__get_free_pages()so we don't have a cast and don't need to save the order, and assume that everyone knows that this is equivalent.
Oh right with __get_free_pages() you need to pass the order for freeing. That's a real practical argument for kmalloc(). So I don't see why you'd hold it against kmalloc().
Quoted 7 lines
So I vote against the
kmalloc()thing as well.If this patch is part of a larger kernel cleanup, I suppose my votes don't count for much. But for the record, this is my opinion.
Thanks and regards,
Eli
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Quoted 4 lines · Vlastimil Babka (SUSE)
Quoted 3 lines · Eli Billauer
On 02/09/2026 14:09, Mike Rapoport wrote:
in this case. So we actually want
__get_free_pages(), but callkmalloc()
Just to add to what Vlastimil said, if you want a buffer you want kmalloc().
Signature · 2 lines
Sincerely yours, Mike.
Eli Billauer
maintainerRe: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
In reply to Vlastimil Babka (SUSE)Quoted 5 lines · Vlastimil Babka (SUSE)
Quoted 2 lines
Replacing it with a
kmalloc()is confusing in my opinion, and requires that the reader is aware thatkmalloc()falls back to__get_free_pages()Why? The reader has only to know that
kmalloc()will provide such a buffer (up to sizes that the page allocator would) and whether it falls back to the page allocator or not is an implementation detail.
When I see __get_free_pages(), I automatically assume there is some ugly low-level memory management going on, which is exactly what fifo_init() does. kmalloc() feels like something you use more for allocating memory for a struct. There is no such rule, of course, but this is my subjective view on these two functions. As I wrote earlier, this is a matter of taste. Maybe it's only me thinking like that.
And as I'm not the one deciding whether this patch is applied or not, it doesn't matter so much what I think about this matter. I've humbly voiced my opinion, and that's about as much as I can do.
Regards,
Eli
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
Hi Eli,
Quoted 10 lines · Eli Billauer
Quoted 5 lines · Vlastimil Babka (SUSE)
Quoted 2 lines
Replacing it with a
kmalloc()is confusing in my opinion, and requires that the reader is aware thatkmalloc()falls back to__get_free_pages()Why? The reader has only to know that
kmalloc()will provide such a buffer (up to sizes that the page allocator would) and whether it falls back to the page allocator or not is an implementation detail.When I see
__get_free_pages(), I automatically assume there is some ugly low-level memory management going on, which is exactly whatfifo_init()does.
However you allocate memory, there will be some ugly low-level memory management underneath :)
Quoted 2 lines
kmalloc()feels like something you use more for allocating memory for a struct.
For that we have k[mz]malloc_obj() now...
Quoted 2 lines
There is no such rule, of course, but this is my subjective view on these two functions.
... and using it to allocate struct is becoming a rule pretty much.
Quoted 2 lines
As I wrote earlier, this is a matter of taste. Maybe it's only me thinking like that.
Let's agree to disagree :)
I think it's more about using the right tool for a job.
For your usecase I really think vmalloc() should work. Presuming you can spare 256MB for fifo buffers, the driver can only run on quite recent and decent hardware and memory rates there are way higher than 400MB/s.
Even the slowest DDR3 is above 6GB/s, so taking a fault to sync page tables on the first access is really a non-issue.
Quoted 6 lines
And as I'm not the one deciding whether this patch is applied or not, it doesn't matter so much what I think about this matter. I've humbly voiced my opinion, and that's about as much as I can do.
Regards,
Eli
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
In reply to Eli BillauerQuoted 8 lines · Eli Billauer
Quoted 6 lines · Mike Rapoport
Quoted 2 lines
* Does
vmalloc()guarantee that non-pageable physical RAM is allocated when it returns?It's not pageable in the sense of demand paging. Some architectures lazily synchronize vmalloc page tables and this can cause page faults that will take care of the page table synchronization.
Quoted 5 lines
Thanks for this clarification.
This sounds like a showstopper to me. Immediately after the allocation of these buffers, data from the USB device can arrive at 400 MB/s. If the data flow is throttled as a result of handling page faults while trying to write
Note that these page faults are supposed to be uncommon in nature. They will only happen in architectures where the top-level page tables are not pre-populated, and when they weren't populated before. On a 64-bit architecture that would be something like every 512 GiB of address space :)
Quoted 12 lines
the data to these buffers, this could lead to an overflow in the USB device's own RAM buffers. This is because the USB device is an FPGA, where RAM is an expensive resource.
So using
vmalloc()may result in a malfunction in the data transport where the__get_free_pages()would have been successful. This outweighs the benefit of a somewhat higher probability of success in allocating very large buffers, and surely the improved aesthetics of the kernel code.Regards,
Eli
Signature · 1 line
Pedro
Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
In reply to Eli BillauerQuoted 8 lines · On Wed, 2 Sep 2026 13:41:54 +0200 Eli Billauer <[email protected]> wrote:
Quoted 6 lines · Mike Rapoport
Quoted 2 lines
* Does
vmalloc()guarantee that non-pageable physical RAM is allocated when it returns?It's not pageable in the sense of demand paging. Some architectures lazily synchronize vmalloc page tables and this can cause page faults that will take care of the page table synchronization.
Quoted 8 lines
Thanks for this clarification.
This sounds like a showstopper to me. Immediately after the allocation of these buffers, data from the USB device can arrive at 400 MB/s. If the data flow is throttled as a result of handling page faults while trying to write the data to these buffers, this could lead to an overflow in the USB device's own RAM buffers. This is because the USB device is an FPGA, where RAM is an expensive resource.
IIUC The faults don't usually happen.
But if one cpu write the address somewhere and another cpu picks it up immediately and access the buffer you can get a fault.
Even if they do happen they are unlikely to be worse than normal interrupt latency.
If you are trying to receive data at very high speed I'd have thought that you'd want the USB data written directly into you buffer list. (Much the same way as most ethernet hardware handles rx frames.) I realise the the usb kernel code isn't really written for that sort of access (some of usbnet could do with it).
David
Quoted 8 lines
So using
vmalloc()may result in a malfunction in the data transport where the__get_free_pages()would have been successful. This outweighs the benefit of a somewhat higher probability of success in allocating very large buffers, and surely the improved aesthetics of the kernel code.Regards,
Eli
PATCH 3/4 misc: ibmvmc: replace get_zeroed_page() with kzalloc()
In reply to Mike Rapoport (Microsoft)ibmvmc_init_crq_queue() allocates the CRQ message queue that is shared with the hypervisor. The queue is mapped for DMA and registered with the hypervisor by its DMA address.
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/misc/ibmvmc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/misc/ibmvmc.c +3 -3
@@ -154,7 +154,7 @@ static void ibmvmc_release_crq_queue(struct crq_server_adapter *adapter)dma_unmap_single (adapter->dev, queue->msg_token, queue->size *sizeof (*queue->msgs),DMA_BIDIRECTIONAL ); -free_page ((unsigned long )queue->msgs); +kfree (queue->msgs); }/**
@@ -2130,7 +2130,7 @@ static int ibmvmc_init_crq_queue(struct crq_server_adapter *adapter)int rc =0 ;int retrc =0 ; - queue->msgs = (struct ibmvmc_crq_msg *)get_zeroed_page (GFP_KERNEL ); + queue->msgs =kzalloc (PAGE_SIZE ,GFP_KERNEL );if (!queue->msgs) goto malloc_failed;
@@ -2192,7 +2192,7 @@ static int ibmvmc_init_crq_queue(struct crq_server_adapter *adapter) queue->msg_token, queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL); map_failed: -free_page ((unsigned long )queue->msgs); +kfree (queue->msgs); malloc_failed:return -ENOMEM ; }
Signature · 1 line
2.53.0
PATCH 4/4 platform: goldfish: pipe: replace __get_free_page() with kmalloc()
In reply to Mike Rapoport (Microsoft)goldfish_pipe_open() allocates the per-pipe command buffer and goldfish_pipe_device_init() allocates the buffers the device shares with the host. Both are passed to the host as physical addresses and must be physically contiguous, which kmalloc() guarantees.
These buffers 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.
While on it, size both allocations after the structures they hold instead of always taking a full page.
Replace use of __get_free_page() with kmalloc() and free_page() with kfree().
--- drivers/platform/goldfish/goldfish_pipe.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
drivers/platform/goldfish/goldfish_pipe.c +10 -12
@@ -708,12 +708,11 @@ static int goldfish_pipe_open(struct inode *inode, struct file *file)init_waitqueue_head (&pipe->wake_queue);/* -* Command buffer needs to be allocated on its own page to make sure -* it is physically contiguous in host's address space. +* The command buffer is passed to the host as a physical address, so +* it must be physically contiguous, which kmalloc() guarantees. */ BUILD_BUG_ON (sizeof (struct goldfish_pipe_command ) >PAGE_SIZE ); - pipe->command_buffer = - (struct goldfish_pipe_command *)__get_free_page (GFP_KERNEL ); + pipe->command_buffer =kmalloc_obj (*pipe->command_buffer);if (!pipe->command_buffer) { status = -ENOMEM ; goto err_pipe;
@@ -749,7 +748,7 @@ static int goldfish_pipe_open(struct inode *inode, struct file *file) dev->pipes[id] =NULL ; err_id_locked:spin_unlock_irqrestore (&dev->lock, flags); -free_page ((unsigned long )pipe->command_buffer); +kfree (pipe->command_buffer); err_pipe:kfree (pipe);return status;
@@ -770,7 +769,7 @@ static int goldfish_pipe_release(struct inode *inode, struct file *filp)spin_unlock_irqrestore (&dev->lock, flags); filp->private_data =NULL ; -free_page ((unsigned long )pipe->command_buffer); +kfree (pipe->command_buffer);kfree (pipe);return 0 ; }
@@ -833,13 +832,12 @@ static int goldfish_pipe_device_init(struct platform_device *pdev,/* * We're going to pass two buffers, open_command_params and -* signalled_pipe_buffers, to the host. This means each of those buffers -* needs to be contained in a single physical page. The easiest choice -* is to just allocate a page and place the buffers in it. +* signalled_pipe_buffers, to the host as physical addresses. This means +* each of those buffers needs to be physically contiguous, which +* kmalloc() guarantees. */ BUILD_BUG_ON (sizeof (struct goldfish_pipe_dev_buffers ) >PAGE_SIZE ); - dev->buffers = (struct goldfish_pipe_dev_buffers *) -__get_free_page (GFP_KERNEL ); + dev->buffers =kmalloc_obj (*dev->buffers);if (!dev->buffers) {kfree (dev->pipes);misc_deregister (&dev->miscdev);
@@ -867,7 +865,7 @@ static void goldfish_pipe_device_deinit(struct platform_device *pdev, {misc_deregister (&dev->miscdev);kfree (dev->pipes); -free_page ((unsigned long )dev->buffers); +kfree (dev->buffers); }static int goldfish_pipe_probe (struct platform_device *pdev)
Signature · 1 line
2.53.0