tty: replace __get_free_pages() with kmalloc()
Subsystems (2)
v2 cover tty: replace __get_free_pages() with kmalloc()
This is a (tiny) part of larger work of replacing page allocator calls with kmalloc.
Nowadays the right way to say "I need a buffer" is kmalloc() rather than ancient and ugly __get_free_pages().
---
v2 changes:
* fix placement of include <linux/slab.h> in men_z135_uart
v1: https://patch.msgid.link/[email protected]
---
Mike Rapoport (Microsoft) (4):
serial: pch: replace __get_free_page() with kmalloc()
tty: amiserial: replace get_zeroed_page() with kzalloc()
tty: serial: men_z135_uart: replace __get_free_page() with kmalloc()
vc_screen: replace __get_free_pages() with kmalloc()
drivers/tty/amiserial.c | 14 +++++++------- drivers/tty/serial/men_z135_uart.c | 7 ++++--- drivers/tty/serial/pch_uart.c | 6 +++--- drivers/tty/vt/vc_screen.c | 6 ++---- 4 files changed, 16 insertions(+), 17 deletions(-)
---
base-commit: 5d6919055dec134de3c40167a490f33c74c12581
change-id: 20260528-b4-tty-7c6e90f41d13
Best regards,
Signature · 2 lines
Sincerely yours, Mike.
PATCH v2 1/4 serial: pch: replace __get_free_page() with kmalloc()
pch_uart_init_port() allocates a staging buffer for non-DMA receive path using __get_free_page().
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.
Replace use of __get_free_page() with kmalloc() and free_page() with kfree().
--- drivers/tty/serial/pch_uart.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/tty/serial/pch_uart.c +3 -3
@@ -1655,7 +1655,7 @@ static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,if (priv ==NULL ) goto init_port_alloc_err; - rxbuf = (unsigned char *)__get_free_page (GFP_KERNEL ); + rxbuf =kmalloc (PAGE_SIZE ,GFP_KERNEL );if (!rxbuf) goto init_port_free_txbuf;
@@ -1728,7 +1728,7 @@ static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,#ifdef CONFIG_SERIAL_PCH_UART_CONSOLE pch_uart_ports[board->line_no] =NULL ;#endif -free_page ((unsigned long )rxbuf); +kfree (rxbuf); init_port_free_txbuf:kfree (priv); init_port_alloc_err:
@@ -1743,7 +1743,7 @@ static void pch_uart_exit_port(struct eg20t_port *priv)snprintf (name,sizeof (name),"uart%d_regs" , priv->port.line);debugfs_lookup_and_remove (name,NULL );uart_remove_one_port (&pch_uart_driver, &priv->port); -free_page ((unsigned long )priv->rxbuf.buf); +kfree (priv->rxbuf.buf); }static void pch_uart_pci_remove (struct pci_dev *pdev)
Signature · 1 line
2.53.0
PATCH v2 2/4 tty: amiserial: replace get_zeroed_page() with kzalloc()
In reply to Mike Rapoport (Microsoft)rs_startup() allocates a transmit ring buffer that is used to buffer reads and writes from/to serial data register.
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.
Replace use of get_zeroed_page() with kzalloc() and free_page() with kfree().
--- drivers/tty/amiserial.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/tty/amiserial.c +7 -7
@@ -443,23 +443,23 @@ static int rs_startup(struct tty_struct *tty, struct serial_state *info)struct tty_port *port = &info->tport;unsigned long flags;int retval=0 ; -unsigned long page; +void *buffer; - page =get_zeroed_page (GFP_KERNEL ); -if (!page) + buffer =kzalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!buffer)return -ENOMEM ;local_irq_save (flags);if (tty_port_initialized (port)) { -free_page (page); +kfree (buffer); goto errout; }if (info->xmit.buf) -free_page (page); +kfree (buffer);else - info->xmit.buf = (unsigned char *) page; + info->xmit.buf = buffer;#ifdef SERIAL_DEBUG_OPEN printk ("starting up ttys%d ..." , info->line);
@@ -537,7 +537,7 @@ static void rs_shutdown(struct tty_struct *tty, struct serial_state *info)*/ free_irq (IRQ_AMIGA_VERTB , info); -free_page ((unsigned long )info->xmit.buf); +kfree (info->xmit.buf); info->xmit.buf =NULL ; info->IER =0 ;
Signature · 1 line
2.53.0
Re: [PATCH v2 2/4] tty: amiserial: replace get_zeroed_page() with kzalloc()
Hi Mike,
Quoted 6 lines · Mike Rapoport (Microsoft)
rs_startup()allocates a transmit ring buffer that is used to buffer reads and writes from/to serial data register.This buffer can be allocated with
kmalloc()as there's nothing special about it to go directly to the page allocator.
Quoted 8 lines
kmalloc()provides a better API that does not require ugly casts andkfree()does not need to know the size of the freed object.Replace use of
get_zeroed_page()withkzalloc()andfree_page()withkfree().
Thanks for your patch, which is now commit d26ed502d0c7c05c ("tty:
amiserial: replace get_zeroed_page() with kzalloc()") in tty/tty-next
drivers/tty/amiserial.c static int rs_startup(struct tty_struct *tty, struct serial_state *info) · line 446
Quoted 8 lines
drivers/tty/amiserial.c +1
-1@@ -443,23 +443,23 @@ static int rs_startup(struct tty_struct *tty, struct serial_state *info)struct tty_port *port = &info->tport;unsigned long flags;int retval=0 ; -unsigned long page; +void *buffer;
"char *", to match the type of info->xmit.buf?
Quoted 14 lines
drivers/tty/amiserial.c +3
-3- page =get_zeroed_page (GFP_KERNEL ); -if (!page) + buffer =kzalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!buffer)return -ENOMEM ;local_irq_save (flags);if (tty_port_initialized (port)) { -free_page (page); +kfree (buffer); goto errout; }
drivers/tty/amiserial.c static int rs_startup(struct tty_struct *tty, struct serial_state *info) · line 465
Quoted 10 lines
drivers/tty/amiserial.c +2
-2if (info->xmit.buf) -free_page (page); +kfree (buffer);else - info->xmit.buf = (unsigned char *) page; + info->xmit.buf = buffer;#ifdef SERIAL_DEBUG_OPEN printk ("starting up ttys%d ..." , info->line);
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH v2 2/4] tty: amiserial: replace get_zeroed_page() with kzalloc()
Quoted 10 lines · Geert Uytterhoeven
Quoted 9 lines · Mike Rapoport (Microsoft)
rs_startup()allocates a transmit ring buffer that is used to buffer reads and writes from/to serial data register.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.
Quoted 8 lines
Quoted 5 lines
Replace use of
get_zeroed_page()withkzalloc()andfree_page()withkfree().Thanks for your patch, which is now commit
d26ed502d0c7c05c("tty:
amiserial: replaceget_zeroed_page()withkzalloc()") in tty/tty-next
Sorry, I replied to the work version: the applied patch is not this v2, or the even newer v3, but v1?
Quoted 35 lines
drivers/tty/amiserial.c static int rs_startup(struct tty_struct *tty, struct serial_state *info) · line 446
Quoted 8 lines
drivers/tty/amiserial.c +1
-1@@ -443,23 +443,23 @@ static int rs_startup(struct tty_struct *tty, struct serial_state *info)struct tty_port *port = &info->tport;unsigned long flags;int retval=0 ; -unsigned long page; +void *buffer;"char *", to match the type of
info->xmit.buf?Quoted 24 lines
drivers/tty/amiserial.c +5
-5- page =get_zeroed_page (GFP_KERNEL ); -if (!page) + buffer =kzalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!buffer)return -ENOMEM ;local_irq_save (flags);if (tty_port_initialized (port)) { -free_page (page); +kfree (buffer); goto errout; }if (info->xmit.buf) -free_page (page); +kfree (buffer);else - info->xmit.buf = (unsigned char *) page; + info->xmit.buf = buffer;#ifdef SERIAL_DEBUG_OPEN printk ("starting up ttys%d ..." , info->line);
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH v2 2/4] tty: amiserial: replace get_zeroed_page() with kzalloc()
Quoted 14 lines · Geert Uytterhoeven
Quoted 13 lines · Geert Uytterhoeven
Quoted 12 lines · Mike Rapoport (Microsoft)
rs_startup()allocates a transmit ring buffer that is used to buffer reads and writes from/to serial data register.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.Replace use of
get_zeroed_page()withkzalloc()andfree_page()withkfree().
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Thanks for your patch, which is now commit
d26ed502d0c7c05c("tty:
amiserial: replaceget_zeroed_page()withkzalloc()") in tty/tty-nextSorry, I replied to the work version: the applied patch is not this v2, or the even newer v3, but v1?
s/work/wrong/, where is my morning coffee?
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH v2 2/4] tty: amiserial: replace get_zeroed_page() with kzalloc()
In reply to Geert UytterhoevenHi Geert,
Quoted 21 lines · Geert Uytterhoeven
Hi Mike,
Quoted 6 lines · Mike Rapoport (Microsoft)
rs_startup()allocates a transmit ring buffer that is used to buffer reads and writes from/to serial data register.This buffer can be allocated with
kmalloc()as there's nothing special about it to go directly to the page allocator.Quoted 8 lines
kmalloc()provides a better API that does not require ugly casts andkfree()does not need to know the size of the freed object.Replace use of
get_zeroed_page()withkzalloc()andfree_page()withkfree().Thanks for your patch, which is now commit
d26ed502d0c7c05c("tty:
amiserial: replaceget_zeroed_page()withkzalloc()") in tty/tty-next
Quoted 10 lines
drivers/tty/amiserial.c static int rs_startup(struct tty_struct *tty, struct serial_state *info) · line 446
Quoted 8 lines
drivers/tty/amiserial.c +1
-1@@ -443,23 +443,23 @@ static int rs_startup(struct tty_struct *tty, struct serial_state *info)struct tty_port *port = &info->tport;unsigned long flags;int retval=0 ; -unsigned long page; +void *buffer;"char *", to match the type of
info->xmit.buf?
Does it really matter that much to respin? ;-)
Signature · 2 lines
Sincerely yours, Mike.
PATCH v2 3/4 tty: serial: men_z135_uart: replace __get_free_page() with kmalloc()
In reply to Mike Rapoport (Microsoft)men_z135_probe() allocates a receive staging buffer filled by the CPU via memcpy_fromio() from the device MMIO region.
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.
Replace use of __get_free_page() with kmalloc() and free_page() with kfree().
--- drivers/tty/serial/men_z135_uart.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/tty/serial/men_z135_uart.c +4 -3
@@ -16,6 +16,7 @@#include <linux/tty_flip.h> #include <linux/bitops.h> #include <linux/mcb.h> +#include <linux/slab.h> #define MEN_Z135_MAX_PORTS 12#define MEN_Z135_BASECLK 29491200
@@ -811,7 +812,7 @@ static int men_z135_probe(struct mcb_device *mdev,if (!uart)return -ENOMEM ; - uart->rxbuf = (unsigned char *)__get_free_page (GFP_KERNEL ); + uart->rxbuf =kmalloc (PAGE_SIZE ,GFP_KERNEL );if (!uart->rxbuf)return -ENOMEM ;
@@ -841,7 +842,7 @@ static int men_z135_probe(struct mcb_device *mdev,return 0 ; err: -free_page ((unsigned long ) uart->rxbuf); +kfree (uart->rxbuf);dev_err (dev,"Failed to add UART: %d\n" , err);return err;
@@ -858,7 +859,7 @@ static void men_z135_remove(struct mcb_device *mdev) line--;uart_remove_one_port (&men_z135_driver, &uart->port); -free_page ((unsigned long ) uart->rxbuf); +kfree (uart->rxbuf); }static const struct mcb_device_id men_z135_ids[] = {
Signature · 1 line
2.53.0
PATCH v2 4/4 vc_screen: replace __get_free_pages() with kmalloc()
In reply to Mike Rapoport (Microsoft)vcs_read() and vcs_write() allocate staging buffers with __get_free_pages().
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 it's a modern way of saying "I need a page-sized buffer"
Replace use of __get_free_page() with kmalloc() and drop unused now DEFINE_FREE(free_page_ptr ...)
--- drivers/tty/vt/vc_screen.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/tty/vt/vc_screen.c +2 -4
@@ -53,8 +53,6 @@#define HEADER_SIZE 4u#define CON_BUF_SIZE (IS_ENABLED(CONFIG_BASE_SMALL) ? 256 : PAGE_SIZE) -DEFINE_FREE(free_page_ptr, void *, if (_T) free_page((unsigned long)_T)); -/* * Our minor space: *
@@ -371,7 +369,7 @@ vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)loff_t pos;bool viewed, attr, uni_mode; - char *con_buf __free(free_page_ptr) = (char *)__get_free_page(GFP_KERNEL); + char *con_buf __free(kfree) = kmalloc(PAGE_SIZE, GFP_KERNEL);if (!con_buf)return -ENOMEM ;
@@ -596,7 +594,7 @@ vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)if (use_unicode (inode))return -EOPNOTSUPP ; - char *con_buf __free(free_page_ptr) = (char *)__get_free_page(GFP_KERNEL); + char *con_buf __free(kfree) = kmalloc(PAGE_SIZE, GFP_KERNEL);if (!con_buf)return -ENOMEM ;
Signature · 1 line
2.53.0