Skip to contentSkip to conversation

tty: replace __get_free_pages() with kmalloc()

PATCH In review Mike Rapoport (Microsoft) 2 participants 7 messages Jun 30 – Jul 11

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 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().

---
v3 changes:
* rebased on v7.2-rc1

v2: https://patch.msgid.link/[email protected]
* fix placement of include <linux/slab.h> in men_z135_uart

To: Greg Kroah-Hartman <[email protected]>
To: Jiri Slaby <[email protected]>
Cc: [email protected]
Cc: [email protected]
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: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260528-b4-tty-7c6e90f41d13

Best regards,

Signature · 2 lines
Sincerely yours,
Mike.
#1 lore raw

PATCH v3 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
index 80e31c4d9536..5f7f073f285e 100644
@@ -1662,7 +1662,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;
 
@@ -1735,7 +1735,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:
@@ -1750,7 +1750,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
#2 lore raw

PATCH v3 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
index 81eaca751541..28af0fd98181 100644
@@ -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
#3 lore raw

PATCH v3 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
index 6fad57fee912..9138fa29d301 100644
@@ -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
#4 lore raw

PATCH v3 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
index 7d40eacc21b3..bf1502fd5bd4 100644
@@ -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
#5 lore raw
In reply to Mike Rapoport (Microsoft)
Quoted 5 lines · Mike Rapoport (Microsoft)

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().

I already applied an earlier version of this series, so can you just send the fixes you made between them?

thanks,
greg k-h

#6 lore raw
Quoted 9 lines · Greg Kroah-Hartman
Quoted 5 lines · Mike Rapoport (Microsoft)

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().

I already applied an earlier version of this series, so can you just send the fixes you made between them?

The only change was the placement of include <linux/slab.h> in men_z135_uart and it looks like you fixed it up when applying :)

Quoted 2 lines

thanks,
greg k-h

Signature · 2 lines
Sincerely yours,
Mike.