* [PATCH 1/4] char: xilinx_hwicap: replace page allocator calls with k[mz]alloc()
2026-08-30 7:47 [PATCH 0/4] char/misc: replace page allocator calls with k[mz]alloc() Mike Rapoport (Microsoft)
@ 2026-08-30 7:47 ` Mike Rapoport (Microsoft)
2026-08-30 7:47 ` [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc() Mike Rapoport (Microsoft)
` (2 subsequent siblings)
3 siblings, 0 replies; 14+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-08-30 7:47 UTC (permalink / raw)
To: Arnd Bergmann, Brad Warrum, Eli Billauer, Greg Kroah-Hartman,
Michal Simek, Ritu Agarwal
Cc: Andrew Morton, David Hildenbrand, Matthew Wilcox, Mike Rapoport,
Vlastimil Babka, linux-arm-kernel, linux-kernel, linux-mm,
linuxppc-dev
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().
Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com
Assisted-by: copilot:claude-opus
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
drivers/char/xilinx_hwicap/xilinx_hwicap.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/drivers/char/xilinx_hwicap/xilinx_hwicap.c b/drivers/char/xilinx_hwicap/xilinx_hwicap.c
index 9bb5fa642fd8..bbeb9b6060dc 100644
--- a/drivers/char/xilinx_hwicap/xilinx_hwicap.c
+++ b/drivers/char/xilinx_hwicap/xilinx_hwicap.c
@@ -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);
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
2026-08-30 7:47 [PATCH 0/4] char/misc: replace page allocator calls with k[mz]alloc() Mike Rapoport (Microsoft)
2026-08-30 7:47 ` [PATCH 1/4] char: xilinx_hwicap: " Mike Rapoport (Microsoft)
@ 2026-08-30 7:47 ` Mike Rapoport (Microsoft)
2026-08-31 10:13 ` Eli Billauer
2026-08-30 7:48 ` [PATCH 3/4] misc: ibmvmc: replace get_zeroed_page() with kzalloc() Mike Rapoport (Microsoft)
2026-08-30 7:48 ` [PATCH 4/4] platform: goldfish: pipe: replace __get_free_page() with kmalloc() Mike Rapoport (Microsoft)
3 siblings, 1 reply; 14+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-08-30 7:47 UTC (permalink / raw)
To: Arnd Bergmann, Brad Warrum, Eli Billauer, Greg Kroah-Hartman,
Michal Simek, Ritu Agarwal
Cc: Andrew Morton, David Hildenbrand, Matthew Wilcox, Mike Rapoport,
Vlastimil Babka, linux-arm-kernel, linux-kernel, linux-mm,
linuxppc-dev
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().
Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com
Assisted-by: copilot:claude-opus
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
drivers/char/xillybus/xillyusb.c | 25 +++++++------------------
1 file changed, 7 insertions(+), 18 deletions(-)
diff --git a/drivers/char/xillybus/xillyusb.c b/drivers/char/xillybus/xillyusb.c
index 34e7ad3bcab3..193e2a5b599e 100644
--- a/drivers/char/xillybus/xillyusb.c
+++ b/drivers/char/xillybus/xillyusb.c
@@ -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_endpoint
init_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_endpoint
for (i = 0; i < bufnum; i++) {
struct xillybuffer *xb;
- unsigned long addr;
xb = kzalloc_obj(*xb);
@@ -529,15 +519,14 @@ static struct xillyusb_endpoint
return 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);
}
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread* Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
2026-08-30 7:47 ` [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc() Mike Rapoport (Microsoft)
@ 2026-08-31 10:13 ` Eli Billauer
2026-08-31 11:32 ` Mike Rapoport
2026-08-31 11:39 ` David Laight
0 siblings, 2 replies; 14+ messages in thread
From: Eli Billauer @ 2026-08-31 10:13 UTC (permalink / raw)
To: Mike Rapoport (Microsoft), Arnd Bergmann, Brad Warrum,
Greg Kroah-Hartman, Michal Simek, Ritu Agarwal
Cc: Andrew Morton, David Hildenbrand, Matthew Wilcox, Vlastimil Babka,
linux-arm-kernel, linux-kernel, linux-mm, linuxppc-dev
On 30/08/2026 9:47, Mike Rapoport (Microsoft) wrote:
> 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.
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
^ permalink raw reply [flat|nested] 14+ messages in thread* Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
2026-08-31 10:13 ` Eli Billauer
@ 2026-08-31 11:32 ` Mike Rapoport
2026-09-01 8:42 ` Eli Billauer
2026-08-31 11:39 ` David Laight
1 sibling, 1 reply; 14+ messages in thread
From: Mike Rapoport @ 2026-08-31 11:32 UTC (permalink / raw)
To: Eli Billauer
Cc: Arnd Bergmann, Brad Warrum, Greg Kroah-Hartman, Michal Simek,
Ritu Agarwal, Andrew Morton, David Hildenbrand, Matthew Wilcox,
Vlastimil Babka, linux-arm-kernel, linux-kernel, linux-mm,
linuxppc-dev
On Mon, Aug 31, 2026 at 12:13:08PM +0200, Eli Billauer wrote:
> On 30/08/2026 9:47, Mike Rapoport (Microsoft) wrote:
> > 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.
>
> 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."
For large sizes kmalloc falls back to page allocator anyway.
And the doc is out of date unfortunately.
> Regards,
> Eli
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
2026-08-31 11:32 ` Mike Rapoport
@ 2026-09-01 8:42 ` Eli Billauer
2026-09-01 17:24 ` Mike Rapoport
0 siblings, 1 reply; 14+ messages in thread
From: Eli Billauer @ 2026-09-01 8:42 UTC (permalink / raw)
To: Mike Rapoport
Cc: Arnd Bergmann, Brad Warrum, Greg Kroah-Hartman, Michal Simek,
Ritu Agarwal, Andrew Morton, David Hildenbrand, Matthew Wilcox,
Vlastimil Babka, linux-arm-kernel, linux-kernel, linux-mm,
linuxppc-dev
On 31/08/2026 13:32, Mike Rapoport wrote:
> 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
^ permalink raw reply [flat|nested] 14+ messages in thread* Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
2026-09-01 8:42 ` Eli Billauer
@ 2026-09-01 17:24 ` Mike Rapoport
0 siblings, 0 replies; 14+ messages in thread
From: Mike Rapoport @ 2026-09-01 17:24 UTC (permalink / raw)
To: Eli Billauer
Cc: Arnd Bergmann, Brad Warrum, Greg Kroah-Hartman, Michal Simek,
Ritu Agarwal, Andrew Morton, David Hildenbrand, Matthew Wilcox,
Vlastimil Babka, linux-arm-kernel, linux-kernel, linux-mm,
linuxppc-dev
On Tue, Sep 01, 2026 at 10:42:47AM +0200, Eli Billauer wrote:
> On 31/08/2026 13:32, Mike Rapoport wrote:
> > 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/20260831-docs-memalloc-guide-v1-0-547718c274c1@kernel.org/
> Regards,
> Eli
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
2026-08-31 10:13 ` Eli Billauer
2026-08-31 11:32 ` Mike Rapoport
@ 2026-08-31 11:39 ` David Laight
2026-09-01 7:59 ` Mike Rapoport
1 sibling, 1 reply; 14+ messages in thread
From: David Laight @ 2026-08-31 11:39 UTC (permalink / raw)
To: Eli Billauer
Cc: Mike Rapoport (Microsoft), Arnd Bergmann, Brad Warrum,
Greg Kroah-Hartman, Michal Simek, Ritu Agarwal, Andrew Morton,
David Hildenbrand, Matthew Wilcox, Vlastimil Babka,
linux-arm-kernel, linux-kernel, linux-mm, linuxppc-dev
On Mon, 31 Aug 2026 12:13:08 +0200
Eli Billauer <eli.billauer@gmail.com> wrote:
> On 30/08/2026 9:47, Mike Rapoport (Microsoft) wrote:
> > 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.
>
> 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
>
> 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
>
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
2026-08-31 11:39 ` David Laight
@ 2026-09-01 7:59 ` Mike Rapoport
2026-09-01 8:37 ` David Laight
2026-09-01 8:44 ` Eli Billauer
0 siblings, 2 replies; 14+ messages in thread
From: Mike Rapoport @ 2026-09-01 7:59 UTC (permalink / raw)
To: David Laight
Cc: Eli Billauer, Arnd Bergmann, Brad Warrum, Greg Kroah-Hartman,
Michal Simek, Ritu Agarwal, Andrew Morton, David Hildenbrand,
Matthew Wilcox, Vlastimil Babka, linux-arm-kernel, linux-kernel,
linux-mm, linuxppc-dev
On Mon, Aug 31, 2026 at 12:39:37PM +0100, David Laight wrote:
> On Mon, 31 Aug 2026 12:13:08 +0200
> Eli Billauer <eli.billauer@gmail.com> wrote:
>
> > On 30/08/2026 9:47, Mike Rapoport (Microsoft) wrote:
> > > 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.
> >
> > 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?
> David
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
2026-09-01 7:59 ` Mike Rapoport
@ 2026-09-01 8:37 ` David Laight
2026-09-01 8:44 ` Eli Billauer
1 sibling, 0 replies; 14+ messages in thread
From: David Laight @ 2026-09-01 8:37 UTC (permalink / raw)
To: Mike Rapoport
Cc: Eli Billauer, Arnd Bergmann, Brad Warrum, Greg Kroah-Hartman,
Michal Simek, Ritu Agarwal, Andrew Morton, David Hildenbrand,
Matthew Wilcox, Vlastimil Babka, linux-arm-kernel, linux-kernel,
linux-mm, linuxppc-dev
On Tue, 1 Sep 2026 10:59:16 +0300
Mike Rapoport <rppt@kernel.org> wrote:
> On Mon, Aug 31, 2026 at 12:39:37PM +0100, David Laight wrote:
> > On Mon, 31 Aug 2026 12:13:08 +0200
> > Eli Billauer <eli.billauer@gmail.com> wrote:
> >
> > > On 30/08/2026 9:47, Mike Rapoport (Microsoft) wrote:
> > > > 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.
> > >
> > > 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.
What is the minimum size?
Remember PAGE_SIZE can be 64k.
David
>
> Eli, what do you think?
>
> > David
>
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
2026-09-01 7:59 ` Mike Rapoport
2026-09-01 8:37 ` David Laight
@ 2026-09-01 8:44 ` Eli Billauer
2026-09-01 17:46 ` Mike Rapoport
1 sibling, 1 reply; 14+ messages in thread
From: Eli Billauer @ 2026-09-01 8:44 UTC (permalink / raw)
To: Mike Rapoport, David Laight
Cc: Arnd Bergmann, Brad Warrum, Greg Kroah-Hartman, Michal Simek,
Ritu Agarwal, Andrew Morton, David Hildenbrand, Matthew Wilcox,
Vlastimil Babka, linux-arm-kernel, linux-kernel, linux-mm,
linuxppc-dev
On 01/09/2026 9:59, Mike Rapoport wrote:
>> 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.
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
[1] https://lwn.net/Articles/57804/
^ permalink raw reply [flat|nested] 14+ messages in thread* Re: [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc()
2026-09-01 8:44 ` Eli Billauer
@ 2026-09-01 17:46 ` Mike Rapoport
0 siblings, 0 replies; 14+ messages in thread
From: Mike Rapoport @ 2026-09-01 17:46 UTC (permalink / raw)
To: Eli Billauer
Cc: David Laight, Arnd Bergmann, Brad Warrum, Greg Kroah-Hartman,
Michal Simek, Ritu Agarwal, Andrew Morton, David Hildenbrand,
Matthew Wilcox, Vlastimil Babka, linux-arm-kernel, linux-kernel,
linux-mm, linuxppc-dev
Hi Eli,
Thanks for the detailed explanation!
On Tue, Sep 01, 2026 at 10:44:20AM +0200, Eli Billauer wrote:
> On 01/09/2026 9:59, Mike Rapoport wrote:
> > > 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.
>
> 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.
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.
> 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?
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().
> 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.
> * Can copy_to/from_user() be used with memory allocated with vmalloc().
Yes.
> * 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?
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.
> 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.
> Does the API offer clear answers to these questions?
Thanks for the detailed explanation
> Thanks in advance,
> Eli
>
> [1] https://lwn.net/Articles/57804/
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH 3/4] misc: ibmvmc: replace get_zeroed_page() with kzalloc()
2026-08-30 7:47 [PATCH 0/4] char/misc: replace page allocator calls with k[mz]alloc() Mike Rapoport (Microsoft)
2026-08-30 7:47 ` [PATCH 1/4] char: xilinx_hwicap: " Mike Rapoport (Microsoft)
2026-08-30 7:47 ` [PATCH 2/4] char: xillybus: replace __get_free_pages() with kmalloc() Mike Rapoport (Microsoft)
@ 2026-08-30 7:48 ` Mike Rapoport (Microsoft)
2026-08-30 7:48 ` [PATCH 4/4] platform: goldfish: pipe: replace __get_free_page() with kmalloc() Mike Rapoport (Microsoft)
3 siblings, 0 replies; 14+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-08-30 7:48 UTC (permalink / raw)
To: Arnd Bergmann, Brad Warrum, Eli Billauer, Greg Kroah-Hartman,
Michal Simek, Ritu Agarwal
Cc: Andrew Morton, David Hildenbrand, Matthew Wilcox, Mike Rapoport,
Vlastimil Babka, linux-arm-kernel, linux-kernel, linux-mm,
linuxppc-dev
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().
Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com
Assisted-by: copilot:claude-opus
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
drivers/misc/ibmvmc.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/misc/ibmvmc.c b/drivers/misc/ibmvmc.c
index da82b1edb7c9..7d503ebd9f3c 100644
--- a/drivers/misc/ibmvmc.c
+++ b/drivers/misc/ibmvmc.c
@@ -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;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH 4/4] platform: goldfish: pipe: replace __get_free_page() with kmalloc()
2026-08-30 7:47 [PATCH 0/4] char/misc: replace page allocator calls with k[mz]alloc() Mike Rapoport (Microsoft)
` (2 preceding siblings ...)
2026-08-30 7:48 ` [PATCH 3/4] misc: ibmvmc: replace get_zeroed_page() with kzalloc() Mike Rapoport (Microsoft)
@ 2026-08-30 7:48 ` Mike Rapoport (Microsoft)
3 siblings, 0 replies; 14+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-08-30 7:48 UTC (permalink / raw)
To: Arnd Bergmann, Brad Warrum, Eli Billauer, Greg Kroah-Hartman,
Michal Simek, Ritu Agarwal
Cc: Andrew Morton, David Hildenbrand, Matthew Wilcox, Mike Rapoport,
Vlastimil Babka, linux-arm-kernel, linux-kernel, linux-mm,
linuxppc-dev
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().
Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com
Assisted-by: copilot:claude-opus
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
drivers/platform/goldfish/goldfish_pipe.c | 22 ++++++++++------------
1 file changed, 10 insertions(+), 12 deletions(-)
diff --git a/drivers/platform/goldfish/goldfish_pipe.c b/drivers/platform/goldfish/goldfish_pipe.c
index fa241ca8feb0..3f34db896ece 100644
--- a/drivers/platform/goldfish/goldfish_pipe.c
+++ b/drivers/platform/goldfish/goldfish_pipe.c
@@ -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)
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread