* [PATCH 0/3] tty: replace page allocator calls with k[mz]alloc()
@ 2026-08-30 7:49 Mike Rapoport (Microsoft)
2026-08-30 7:49 ` [PATCH 1/3] tty: port: replace get_zeroed_page() with kzalloc() Mike Rapoport (Microsoft)
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-08-30 7:49 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: Andrew Morton, David Hildenbrand, Matthew Wilcox, Mike Rapoport,
Vlastimil Babka, linux-kernel, linux-mm, linux-serial,
linuxppc-dev
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/20251018093002.3660549-1-rppt@kernel.org/
[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.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 1/3] tty: port: replace get_zeroed_page() with kzalloc()
2026-08-30 7:49 [PATCH 0/3] tty: replace page allocator calls with k[mz]alloc() Mike Rapoport (Microsoft)
@ 2026-08-30 7:49 ` Mike Rapoport (Microsoft)
2026-08-30 8:07 ` sashiko-bot
2026-08-30 7:49 ` [PATCH 2/3] serial: core: " Mike Rapoport (Microsoft)
2026-08-30 7:49 ` [PATCH 3/3] tty: hvcs: replace __get_free_page() with kmalloc() Mike Rapoport (Microsoft)
2 siblings, 1 reply; 10+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-08-30 7:49 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: Andrew Morton, David Hildenbrand, Matthew Wilcox, Mike Rapoport,
Vlastimil Babka, linux-kernel, linux-mm, linux-serial,
linuxppc-dev
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().
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/tty/tty_port.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/drivers/tty/tty_port.c b/drivers/tty/tty_port.c
index 54359310e293..44122921fc45 100644
--- a/drivers/tty/tty_port.c
+++ b/drivers/tty/tty_port.c
@@ -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);
--
2.53.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 2/3] serial: core: replace get_zeroed_page() with kzalloc()
2026-08-30 7:49 [PATCH 0/3] tty: replace page allocator calls with k[mz]alloc() Mike Rapoport (Microsoft)
2026-08-30 7:49 ` [PATCH 1/3] tty: port: replace get_zeroed_page() with kzalloc() Mike Rapoport (Microsoft)
@ 2026-08-30 7:49 ` Mike Rapoport (Microsoft)
2026-08-30 8:00 ` sashiko-bot
2026-08-31 3:02 ` Jiri Slaby
2026-08-30 7:49 ` [PATCH 3/3] tty: hvcs: replace __get_free_page() with kmalloc() Mike Rapoport (Microsoft)
2 siblings, 2 replies; 10+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-08-30 7:49 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: Andrew Morton, David Hildenbrand, Matthew Wilcox, Mike Rapoport,
Vlastimil Babka, linux-kernel, linux-mm, linux-serial,
linuxppc-dev
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().
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/tty/serial/serial_core.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 95774b0f1484..f12ce7d190fe 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -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);
}
/*
--
2.53.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 3/3] tty: hvcs: replace __get_free_page() with kmalloc()
2026-08-30 7:49 [PATCH 0/3] tty: replace page allocator calls with k[mz]alloc() Mike Rapoport (Microsoft)
2026-08-30 7:49 ` [PATCH 1/3] tty: port: replace get_zeroed_page() with kzalloc() Mike Rapoport (Microsoft)
2026-08-30 7:49 ` [PATCH 2/3] serial: core: " Mike Rapoport (Microsoft)
@ 2026-08-30 7:49 ` Mike Rapoport (Microsoft)
2026-08-30 8:05 ` sashiko-bot
2 siblings, 1 reply; 10+ messages in thread
From: Mike Rapoport (Microsoft) @ 2026-08-30 7:49 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: Andrew Morton, David Hildenbrand, Matthew Wilcox, Mike Rapoport,
Vlastimil Babka, linux-kernel, linux-mm, linux-serial,
linuxppc-dev
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().
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/tty/hvc/hvcs.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/tty/hvc/hvcs.c b/drivers/tty/hvc/hvcs.c
index d32b1e3c50bf..b94d59fa3627 100644
--- a/drivers/tty/hvc/hvcs.c
+++ b/drivers/tty/hvc/hvcs.c
@@ -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);
--
2.53.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH 2/3] serial: core: replace get_zeroed_page() with kzalloc()
2026-08-30 7:49 ` [PATCH 2/3] serial: core: " Mike Rapoport (Microsoft)
@ 2026-08-30 8:00 ` sashiko-bot
2026-08-30 8:44 ` Mike Rapoport
2026-08-31 3:02 ` Jiri Slaby
1 sibling, 1 reply; 10+ messages in thread
From: sashiko-bot @ 2026-08-30 8:00 UTC (permalink / raw)
To: Mike Rapoport (Microsoft); +Cc: linux-serial
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) <rppt@kernel.org>
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.
Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com
> diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
> index 95774b0f1484b..f12ce7d190fe6 100644
> --- a/drivers/tty/serial/serial_core.c
> +++ b/drivers/tty/serial/serial_core.c
> @@ -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);
[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?
> + if (!buf)
> return -ENOMEM;
>
> uport = uart_port_ref_lock(state, &flags);
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260830-tty-v1-0-0cbe6170649b@kernel.org?part=2
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 3/3] tty: hvcs: replace __get_free_page() with kmalloc()
2026-08-30 7:49 ` [PATCH 3/3] tty: hvcs: replace __get_free_page() with kmalloc() Mike Rapoport (Microsoft)
@ 2026-08-30 8:05 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-08-30 8:05 UTC (permalink / raw)
To: Mike Rapoport (Microsoft); +Cc: linux-serial
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) <rppt@kernel.org>
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.
Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com
> diff --git a/drivers/tty/hvc/hvcs.c b/drivers/tty/hvc/hvcs.c
> index d32b1e3c50bfd..b94d59fa36274 100644
> --- a/drivers/tty/hvc/hvcs.c
> +++ b/drivers/tty/hvc/hvcs.c
[ ... ]
> @@ -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.
> 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);
[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.
> hvcs_pi_buff = NULL;
> spin_unlock(&hvcs_pi_lock);
>
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260830-tty-v1-0-0cbe6170649b@kernel.org?part=3
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 1/3] tty: port: replace get_zeroed_page() with kzalloc()
2026-08-30 7:49 ` [PATCH 1/3] tty: port: replace get_zeroed_page() with kzalloc() Mike Rapoport (Microsoft)
@ 2026-08-30 8:07 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-08-30 8:07 UTC (permalink / raw)
To: Mike Rapoport (Microsoft); +Cc: linux-serial
> 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().
>
> 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>
Sashiko has reviewed this patch and found no issues. It looks great!
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260830-tty-v1-0-0cbe6170649b@kernel.org?part=1
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 2/3] serial: core: replace get_zeroed_page() with kzalloc()
2026-08-30 8:00 ` sashiko-bot
@ 2026-08-30 8:44 ` Mike Rapoport
0 siblings, 0 replies; 10+ messages in thread
From: Mike Rapoport @ 2026-08-30 8:44 UTC (permalink / raw)
To: sashiko-reviews; +Cc: linux-serial
On Sun, Aug 30, 2026 at 08:00:54AM +0000, sashiko-bot@kernel.org wrote:
> 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) <rppt@kernel.org>
>
> 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.
>
> Link: https://lore.kernel.org/all/635405e4-9423-4a25-a6e7-e03c8ea0bcbe@redhat.com
>
> > diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
> > index 95774b0f1484b..f12ce7d190fe6 100644
> > --- a/drivers/tty/serial/serial_core.c
> > +++ b/drivers/tty/serial/serial_core.c
> > @@ -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);
>
> [Severity: Critical]
> Does replacing get_zeroed_page() with kzalloc() 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.
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 2/3] serial: core: replace get_zeroed_page() with kzalloc()
2026-08-30 7:49 ` [PATCH 2/3] serial: core: " Mike Rapoport (Microsoft)
2026-08-30 8:00 ` sashiko-bot
@ 2026-08-31 3:02 ` Jiri Slaby
2026-08-31 8:28 ` Mike Rapoport
1 sibling, 1 reply; 10+ messages in thread
From: Jiri Slaby @ 2026-08-31 3:02 UTC (permalink / raw)
To: Mike Rapoport (Microsoft), Greg Kroah-Hartman
Cc: Andrew Morton, David Hildenbrand, Matthew Wilcox, Vlastimil Babka,
linux-kernel, linux-mm, linux-serial, linuxppc-dev
On 30. 08. 26, 9:49, Mike Rapoport (Microsoft) wrote:
> 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().
>
> 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/tty/serial/serial_core.c | 16 ++++++++--------
> 1 file changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
> index 95774b0f1484..f12ce7d190fe 100644
> --- a/drivers/tty/serial/serial_core.c
> +++ b/drivers/tty/serial/serial_core.c
> @@ -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:
> + 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;
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,
--
js
suse labs
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 2/3] serial: core: replace get_zeroed_page() with kzalloc()
2026-08-31 3:02 ` Jiri Slaby
@ 2026-08-31 8:28 ` Mike Rapoport
0 siblings, 0 replies; 10+ messages in thread
From: Mike Rapoport @ 2026-08-31 8:28 UTC (permalink / raw)
To: Jiri Slaby
Cc: Greg Kroah-Hartman, Andrew Morton, David Hildenbrand,
Matthew Wilcox, Vlastimil Babka, linux-kernel, linux-mm,
linux-serial, linuxppc-dev
On Mon, Aug 31, 2026 at 05:02:54AM +0200, Jiri Slaby wrote:
> On 30. 08. 26, 9:49, Mike Rapoport (Microsoft) wrote:
> > 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().
> >
> > 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/tty/serial/serial_core.c | 16 ++++++++--------
> > 1 file changed, 8 insertions(+), 8 deletions(-)
> >
> > diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
> > index 95774b0f1484..f12ce7d190fe 100644
> > --- a/drivers/tty/serial/serial_core.c
> > +++ b/drivers/tty/serial/serial_core.c
> > @@ -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:
>
> > + 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;
>
> 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?
Sure, I also made this change in uart_free_xmit_buf().
> thanks,
> --
> js
> suse labs
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-08-31 8:28 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-30 7:49 [PATCH 0/3] tty: replace page allocator calls with k[mz]alloc() Mike Rapoport (Microsoft)
2026-08-30 7:49 ` [PATCH 1/3] tty: port: replace get_zeroed_page() with kzalloc() Mike Rapoport (Microsoft)
2026-08-30 8:07 ` sashiko-bot
2026-08-30 7:49 ` [PATCH 2/3] serial: core: " Mike Rapoport (Microsoft)
2026-08-30 8:00 ` sashiko-bot
2026-08-30 8:44 ` Mike Rapoport
2026-08-31 3:02 ` Jiri Slaby
2026-08-31 8:28 ` Mike Rapoport
2026-08-30 7:49 ` [PATCH 3/3] tty: hvcs: replace __get_free_page() with kmalloc() Mike Rapoport (Microsoft)
2026-08-30 8:05 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox