tty: replace page allocator calls with k[mz]alloc()
cover tty: 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/
---
Mike Rapoport (Microsoft) (3):
tty: port: replace get_zeroed_page() with kzalloc()
serial: core: replace get_zeroed_page() with kzalloc()
tty: hvcs: replace __get_free_page() with kmalloc()
drivers/tty/hvc/hvcs.c | 6 +++--- drivers/tty/serial/serial_core.c | 16 ++++++++-------- drivers/tty/tty_port.c | 8 ++++---- 3 files changed, 15 insertions(+), 15 deletions(-)
---
base-commit: 1b78070aaef63512688aebfbc82365ef9d6660f1
change-id: 20260829-tty-84f05b74862a
--
Sincerely yours,
Mike.
PATCH 1/3 tty: port: replace get_zeroed_page() with kzalloc()
tty_port_alloc_xmit_buf() allocates the transmit buffer of a tty port. The buffer only backs the port's kfifo, the data being sent is copied in and out of it.
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/tty/tty_port.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/tty/tty_port.c +4 -4
@@ -240,13 +240,13 @@ EXPORT_SYMBOL_GPL(tty_port_unregister_device);int tty_port_alloc_xmit_buf (struct tty_port *port) { -/* We may sleep in get_zeroed_page() */ +/* We may sleep in kzalloc() */ guard (mutex)(&port->buf_mutex);if (port->xmit_buf)return 0 ; - port->xmit_buf = (u8 *)get_zeroed_page (GFP_KERNEL ); + port->xmit_buf =kzalloc (PAGE_SIZE ,GFP_KERNEL );if (port->xmit_buf ==NULL )return -ENOMEM ;
@@ -259,7 +259,7 @@ EXPORT_SYMBOL(tty_port_alloc_xmit_buf);void tty_port_free_xmit_buf (struct tty_port *port) {guard (mutex)(&port->buf_mutex); -free_page ((unsigned long )port->xmit_buf); +kfree (port->xmit_buf); port->xmit_buf =NULL ;INIT_KFIFO (port->xmit_fifo); }
@@ -288,7 +288,7 @@ static void tty_port_destructor(struct kref *kref)/* check if last port ref was dropped before tty release */ if (WARN_ON (port->itty))return ; -free_page ((unsigned long )port->xmit_buf); +kfree (port->xmit_buf);tty_port_destroy (port);if (port->ops && port->ops->destruct) port->ops->destruct (port);
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 1/3] tty: port: replace get_zeroed_page() with kzalloc()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines
tty_port_alloc_xmit_buf()allocates the transmit buffer of a tty port. The buffer only backs the port's kfifo, the data being sent is copied in and out of it.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 andkfree()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.
Quoted 6 lines
Replace use of
get_zeroed_page()withkzalloc()andfree_page()withkfree().Assisted-by copilot:claude-opusSigned-off-by Mike Rapoport (Microsoft) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 2/3 serial: core: replace get_zeroed_page() with kzalloc()
In reply to Mike Rapoport (Microsoft)uart_alloc_xmit_buf() allocates the transmit buffer of a serial port. The buffer only backs the port's kfifo, the data being sent is copied in and out of it.
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.
While on it, make the local variable holding the buffer a pointer to get rid of the casts.
Replace use of get_zeroed_page() with kzalloc() and free_page() with kfree().
--- drivers/tty/serial/serial_core.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/tty/serial/serial_core.c +8 -8
@@ -247,29 +247,29 @@ static int uart_alloc_xmit_buf(struct tty_port *port) struct uart_state *state = container_of(port, struct uart_state, port);struct uart_port *uport;unsigned long flags; -unsigned long page; +unsigned char *buf;/* * Initialise and allocate the transmit and temporary * buffer. */ - page =get_zeroed_page (GFP_KERNEL ); -if (!page) + buf =kzalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!buf)return -ENOMEM ; uport =uart_port_ref_lock (state, &flags);if (!state->port.xmit_buf) { - state->port.xmit_buf = (unsigned char *)page; + state->port.xmit_buf = buf;kfifo_init (&state->port.xmit_fifo, state->port.xmit_buf,PAGE_SIZE );uart_port_unlock_deref (uport, flags); }else {uart_port_unlock_deref (uport, flags);/* -* Do not free() the page under the port lock, see +* Do not free() the buffer under the port lock, see * uart_free_xmit_buf(). */ -free_page (page); +kfree (buf); }return 0 ;
@@ -283,7 +283,7 @@ static void uart_free_xmit_buf(struct tty_port *port)char *xmit_buf;/* -* Do not free() the transmit buffer page under the port lock since +* Do not free() the transmit buffer under the port lock since * this can create various circular locking scenarios. For instance, * console driver may need to allocate/free a debug object, which * can end up in printk() recursion.
@@ -294,7 +294,7 @@ static void uart_free_xmit_buf(struct tty_port *port)INIT_KFIFO (port->xmit_fifo);uart_port_unlock_deref (uport, flags); -free_page ((unsigned long )xmit_buf); +kfree (xmit_buf); }/*
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 2/3] serial: core: replace get_zeroed_page() with kzalloc()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] The transition from `get_zeroed_page(GFP_KERNEL)` to `kzalloc(PAGE_SIZE, GFP_KERNEL)` for…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] The transition from get_zeroed_page(GFP_KERNEL) to kzalloc(PAGE_SIZE, GFP_KERNEL) for allocating the serial port transmit buffer (tport->xmit_buf) breaks the strict page-alignment guarantee. Several serial drivers inherently rely on this buffer being page-aligned. atmel_serial explicitly panics via BUG_ON(!PAGE_ALIGNED(tport->xmit_buf)), and pch_uart corrupts DMA scatterlist addresses by masking out the lower bits (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) assuming the base address is page-aligned. If kzalloc returns an unaligned address (e.g., when SLUB debugging adds redzones), this results in DMA reading adjacent out-of-bounds kernel memory and transmitting it over the serial line. --
commit 56b6291d1a96458932c4b0e5de37ae0df9e37c2d
Author: Mike Rapoport (Microsoft) <[email protected]>
serial: core: replace get_zeroed_page() with kzalloc()
This commit replaces the page allocator with kzalloc() for the serial port transmit buffer. It aims to simplify the API and remove casts, noting that performance differences are negligible.
Quoted 8 lines
drivers/tty/serial/serial_core.c +0
-0@@ -247,29 +247,29 @@ static int uart_alloc_xmit_buf(struct tty_port *port) struct uart_state *state = container_of(port, struct uart_state, port);struct uart_port *uport;unsigned long flags;
drivers/tty/serial/serial_core.c static int uart_alloc_xmit_buf(struct tty_port *port) · line 256
Quoted 10 lines
drivers/tty/serial/serial_core.c +2
-3-unsigned long page; +unsigned char *buf;/* * Initialise and allocate the transmit and temporary * buffer. */ - page =get_zeroed_page (GFP_KERNEL ); -if (!page) + buf =kzalloc (PAGE_SIZE ,GFP_KERNEL );
[Severity: Critical]
Does replacing get_zeroed_page() with kzalloc() break page alignment requirements for the transmit buffer?
Several serial drivers appear to rely on the transmit buffer being strictly page-aligned. For example, in drivers/tty/serial/atmel_serial.c, atmel_prepare_tx_dma() explicitly checks for this:
BUG_ON(!PAGE_ALIGNED(tport->xmit_buf));
Additionally, in drivers/tty/serial/pch_uart.c, dma_handle_tx() masks out the lower bits of the DMA address, assuming the base is page-aligned:
sg_dma_address(sg) = (sg_dma_address(sg) &
~(UART_XMIT_SIZE - 1)) + sg->offset;If kzalloc() returns an unaligned address (which can happen when SLUB debugging adds redzones), could this result in a kernel panic in atmel_serial or corrupted DMA scatterlist addresses in pch_uart?
Could this DMA corruption lead to out-of-bounds kernel memory being read and transmitted over the serial line?
Quoted 4 lines
drivers/tty/serial/serial_core.c +1
-0+if (!buf)return -ENOMEM ; uport =uart_port_ref_lock (state, &flags);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/3] serial: core: replace get_zeroed_page() with kzalloc()
Quoted 26 lines · On Sun, Aug 30, 2026 at 08:00:54AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] The transition fromget_zeroed_page(GFP_KERNEL)tokzalloc(PAGE_SIZE, GFP_KERNEL)for allocating the serial port transmit buffer (tport->xmit_buf) breaks the strict page-alignment guarantee. Several serial drivers inherently rely on this buffer being page-aligned.atmel_serialexplicitly panics viaBUG_ON(!PAGE_ALIGNED(tport->xmit_buf)), andpch_uartcorrupts DMA scatterlist addresses by masking out the lower bits (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) assuming the base address is page-aligned. Ifkzallocreturns an unaligned address (e.g., when SLUB debugging adds redzones), this results in DMA reading adjacent out-of-bounds kernel memory and transmitting it over the serial line. --commit
56b6291d1a96458932c4b0e5de37ae0df9e37c2d
Author: Mike Rapoport (Microsoft) <[email protected]>serial: core: replace
get_zeroed_page()withkzalloc()This commit replaces the page allocator with
kzalloc()for the serial port transmit buffer. It aims to simplify the API and remove casts, noting that performance differences are negligible.Quoted 11 lines
drivers/tty/serial/serial_core.c +1
-1@@ -247,29 +247,29 @@ static int uart_alloc_xmit_buf(struct tty_port *port) struct uart_state *state = container_of(port, struct uart_state, port);struct uart_port *uport;unsigned long flags; -unsigned long page; +unsigned char *buf;
Quoted 11 lines
drivers/tty/serial/serial_core.c static int uart_alloc_xmit_buf(struct tty_port *port) · line 256
Quoted 7 lines
drivers/tty/serial/serial_core.c +1
-2/* * Initialise and allocate the transmit and temporary * buffer. */ - page =get_zeroed_page (GFP_KERNEL ); -if (!page) + buf =kzalloc (PAGE_SIZE ,GFP_KERNEL );[Severity: Critical]
Does replacingget_zeroed_page()withkzalloc()break page alignment requirements for the transmit buffer?
No, commit 59bb47985c1d ("mm, sl[aou]b: guarantee natural alignment for kmalloc(power-of-two)") guarantees that PAGE_SIZE allocations are page aligned.
Signature · 2 lines
Sincerely yours, Mike.
Jiri Slaby
maintainerRe: [PATCH 2/3] serial: core: replace get_zeroed_page() with kzalloc()
In reply to Mike Rapoport (Microsoft)Quoted 35 lines · Mike Rapoport (Microsoft)
uart_alloc_xmit_buf()allocates the transmit buffer of a serial port. The buffer only backs the port's kfifo, the data being sent is copied in and out of it.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 andkfree()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, make the local variable holding the buffer a pointer to get rid of the casts.
Replace use of
get_zeroed_page()withkzalloc()andfree_page()withkfree().Assisted-by copilot:claude-opusSigned-off-by Mike Rapoport (Microsoft) <[email protected]>--- drivers/tty/serial/serial_core.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)drivers/tty/serial/serial_core.c +0
-0
drivers/tty/serial/serial_core.c static int uart_alloc_xmit_buf(struct tty_port *port) · line 250
Quoted 4 lines
drivers/tty/serial/serial_core.c +0
-1@@ -247,29 +247,29 @@ static int uart_alloc_xmit_buf(struct tty_port *port) struct uart_state *state = container_of(port, struct uart_state, port);struct uart_port *uport;unsigned long flags; -unsigned long page;
1:
Quoted 6 lines
drivers/tty/serial/serial_core.c +1
-0+unsigned char *buf;/* * Initialise and allocate the transmit and temporary * buffer. */
drivers/tty/serial/serial_core.c static int uart_alloc_xmit_buf(struct tty_port *port) · line 262
Quoted 10 lines
drivers/tty/serial/serial_core.c +3
-3- page =get_zeroed_page (GFP_KERNEL ); -if (!page) + buf =kzalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!buf)return -ENOMEM ; uport =uart_port_ref_lock (state, &flags);if (!state->port.xmit_buf) { - state->port.xmit_buf = (unsigned char *)page; + state->port.xmit_buf = buf;
xmit_buf is u8 *. This uchar was omitted when I was changing the type back then. Could you use the right type at 1b now?
thanks,
Signature · 2 lines
js suse labs
Re: [PATCH 2/3] serial: core: replace get_zeroed_page() with kzalloc()
Quoted 52 lines · Jiri Slaby
Quoted 35 lines · Mike Rapoport (Microsoft)
uart_alloc_xmit_buf()allocates the transmit buffer of a serial port. The buffer only backs the port's kfifo, the data being sent is copied in and out of it.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 andkfree()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, make the local variable holding the buffer a pointer to get rid of the casts.
Replace use of
get_zeroed_page()withkzalloc()andfree_page()withkfree().Assisted-by copilot:claude-opusSigned-off-by Mike Rapoport (Microsoft) <[email protected]>--- drivers/tty/serial/serial_core.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)drivers/tty/serial/serial_core.c +0
-0drivers/tty/serial/serial_core.c static int uart_alloc_xmit_buf(struct tty_port *port) · line 250
Quoted 4 lines
drivers/tty/serial/serial_core.c +0
-1@@ -247,29 +247,29 @@ static int uart_alloc_xmit_buf(struct tty_port *port) struct uart_state *state = container_of(port, struct uart_state, port);struct uart_port *uport;unsigned long flags; -unsigned long page;1:
Quoted 9 lines
drivers/tty/serial/serial_core.c +3
-2+unsigned char *buf;/* * Initialise and allocate the transmit and temporary * buffer. */ - page =get_zeroed_page (GFP_KERNEL ); -if (!page) + buf =kzalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!buf)
Quoted 8 lines
drivers/tty/serial/serial_core.c static int uart_alloc_xmit_buf(struct tty_port *port) · line 262
Quoted 5 lines
drivers/tty/serial/serial_core.c +1
-1return -ENOMEM ; uport =uart_port_ref_lock (state, &flags);if (!state->port.xmit_buf) { - state->port.xmit_buf = (unsigned char *)page; + state->port.xmit_buf = buf;
xmit_bufis u8 *. This uchar was omitted when I was changing the type back then. Could you use the right type at 1b now?
Sure, I also made this change in uart_free_xmit_buf().
Quoted 4 lines
thanks,
Signature · 2 lines
js suse labs
Signature · 2 lines
Sincerely yours, Mike.
PATCH 3/3 tty: hvcs: replace __get_free_page() with kmalloc()
In reply to Mike Rapoport (Microsoft)hvcs_initialize() allocates the buffer that receives the partner info returned by the H_VTERM_PARTNER_INFO hypercall. The buffer is passed to the hypervisor as a physical address and it must not cross a page boundary.
kmalloc() guarantees that a power of two sized allocation is aligned to its size, so a PAGE_SIZE allocation is page aligned as well.
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/tty/hvc/hvcs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/tty/hvc/hvcs.c +3 -3
@@ -1470,7 +1470,7 @@ static int hvcs_initialize(void) goto register_fail; } - hvcs_pi_buff = (unsigned long *)__get_free_page (GFP_KERNEL ); + hvcs_pi_buff =kmalloc (PAGE_SIZE ,GFP_KERNEL );if (!hvcs_pi_buff) { rc = -ENOMEM ; goto buff_alloc_fail;
@@ -1486,7 +1486,7 @@ static int hvcs_initialize(void)return 0 ; kthread_fail: -free_page ((unsigned long )hvcs_pi_buff); +kfree (hvcs_pi_buff); buff_alloc_fail:tty_unregister_driver (hvcs_tty_driver); register_fail:
@@ -1528,7 +1528,7 @@ static void __exit hvcs_module_exit(void)kthread_stop (hvcs_task);spin_lock (&hvcs_pi_lock); -free_page ((unsigned long )hvcs_pi_buff); +kfree (hvcs_pi_buff); hvcs_pi_buff =NULL ;spin_unlock (&hvcs_pi_lock);
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 3/3] tty: hvcs: replace __get_free_page() with kmalloc()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Error path in hvcs_initialize() leaves hvcs_task as an ERR_PTR and hvcs_pi_buff as a dangling pointer, leading to a kernel panic and double-free on module exit. --
commit 9b8103eef858c273046b0b8e782a706cf3628f9a
Author: Mike Rapoport (Microsoft) <[email protected]>
tty: hvcs: replace __get_free_page() with kmalloc()
hvcs_initialize() allocates the buffer that receives the partner info returned by the H_VTERM_PARTNER_INFO hypercall. This buffer can be allocated with kmalloc() as there's nothing special about it to go directly to the page allocator.
Quoted 4 lines
drivers/tty/hvc/hvcs.c +0
-0
[ ... ]
drivers/tty/hvc/hvcs.c static int hvcs_initialize(void) · line 1489
Quoted 6 lines
drivers/tty/hvc/hvcs.c +1
-1@@ -1486,7 +1486,7 @@ static int hvcs_initialize(void)return 0 ; kthread_fail: -free_page ((unsigned long )hvcs_pi_buff); +kfree (hvcs_pi_buff);
[Severity: High]
This isn't a bug introduced by this patch, but will this error path leave hvcs_task as an ERR_PTR and hvcs_pi_buff as a dangling pointer?
If hvcs_initialize() fails at kthread_run(), hvcs_task will hold an ERR_PTR and hvcs_pi_buff is freed here but not nullified.
Quoted 3 lines
drivers/tty/hvc/hvcs.c +0
-0buff_alloc_fail:tty_unregister_driver (hvcs_tty_driver); register_fail:
[ ... ]
drivers/tty/hvc/hvcs.c static void __exit hvcs_module_exit(void) · line 1531
Quoted 6 lines
drivers/tty/hvc/hvcs.c +1
-1@@ -1528,7 +1528,7 @@ static void __exit hvcs_module_exit(void)kthread_stop (hvcs_task);spin_lock (&hvcs_pi_lock); -free_page ((unsigned long )hvcs_pi_buff); +kfree (hvcs_pi_buff);
[Severity: High]
This isn't a bug introduced by this patch, but could module unload cause a kernel panic and a double-free if initialization previously failed?
Since hvcs_task is left as an ERR_PTR on failure rather than being reset to NULL, the early return check in hvcs_module_exit() fails to catch it:
drivers/tty/hvc/hvcs.c:hvcs_module_exit() {
if (!hvcs_task)
return;
...
}This allows execution to proceed to kthread_stop(hvcs_task), which will dereference the error pointer.
It also proceeds to double-free the dangling hvcs_pi_buff here, and eventually passes the NULL hvcs_tty_driver to tty_unregister_driver(), resulting in a NULL pointer dereference panic.
Quoted 3 lines
drivers/tty/hvc/hvcs.c +0
-0hvcs_pi_buff =NULL ;spin_unlock (&hvcs_pi_lock);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3