Skip to contentSkip to conversation

auxdisplay: Introduce charlcd_free()

PATCH In review Andy Shevchenko 3 participants 15 messages Mar 12, 2019 – Mar 17, 2019

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

v2 cover auxdisplay: Introduce charlcd_free()

I have found a memory leak in hd44780 and it becomes that we have no counterpart to charlcd_alloc() that developers can easily miss.

So, this series fixes a leak and introduces the charlcd_free().

In v2:
- add new patch to convert to_priv() to charlcd_to_priv()
- address Geert's comment what should be freed

Cc Geert Uytterhoeven <[email protected]>

Andy Shevchenko (5):
auxdisplay: hd44780: Fix memory leak on ->remove()
auxdisplay: charlcd: Move to_priv() to charlcd namespace
auxdisplay: charlcd: Introduce charlcd_free() helper
auxdisplay: panel: Convert to use charlcd_free()
auxdisplay: hd44780: Convert to use charlcd_free()

 drivers/auxdisplay/charlcd.c | 32 +++++++++++++++++++-------------
 drivers/auxdisplay/hd44780.c |  4 +++-
 drivers/auxdisplay/panel.c   |  4 ++--
 include/misc/charlcd.h       |  1 +
 4 files changed, 25 insertions(+), 16 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 1/5 auxdisplay: hd44780: Fix memory leak on ->remove()

We have to free on ->remove() the allocated resources on ->probe().

Fixes d47d88361fee ("auxdisplay: Add HD44780 Character LCD support")
Cc Geert Uytterhoeven <[email protected]>
---
 drivers/auxdisplay/hd44780.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/auxdisplay/hd44780.c +2 -0
index 9ad93ea42fdc..3cde351fb5c9 100644
@@ -280,6 +280,8 @@ static int hd44780_remove(struct platform_device *pdev)
 	struct charlcd *lcd = platform_get_drvdata(pdev);
 
 	charlcd_unregister(lcd);
+
+	kfree(lcd);
 	return 0;
 }
 
Signature · 1 line
2.20.1
reviewer Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] auxdisplay: hd44780: Fix memory leak on ->remove()

Quoted 5 lines · Andy Shevchenko

We have to free on ->remove() the allocated resources on ->probe().

Fixes d47d88361fee ("auxdisplay: Add HD44780 Character LCD support")
Cc Geert Uytterhoeven <[email protected]>

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
#3 lore raw

PATCH v2 2/5 auxdisplay: charlcd: Move to_priv() to charlcd namespace

In reply to Andy Shevchenko

In order to be more particular in names, rename to_priv() macro to charlcd_to_priv().

No functional change intended.

Cc Geert Uytterhoeven <[email protected]>
---
 drivers/auxdisplay/charlcd.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
drivers/auxdisplay/charlcd.c +13 -13
index 60e0b772673f..407acd22efa8 100644
@@ -91,7 +91,7 @@ struct charlcd_priv {
 	unsigned long long drvdata[0];
 };
 
-#define to_priv(p)	container_of(p, struct charlcd_priv, lcd)
+#define charlcd_to_priv(p)	container_of(p, struct charlcd_priv, lcd)
 
 /* Device single-open policy control */
 static atomic_t charlcd_available = ATOMIC_INIT(1);
@@ -105,7 +105,7 @@ static void long_sleep(int ms)
 /* turn the backlight on or off */
 static void charlcd_backlight(struct charlcd *lcd, int on)
 {
-	struct charlcd_priv *priv = to_priv(lcd);
+	struct charlcd_priv *priv = charlcd_to_priv(lcd);
 
 	if (!lcd->ops->backlight)
 		return;
@@ -134,7 +134,7 @@ static void charlcd_bl_off(struct work_struct *work)
 /* turn the backlight on for a little while */
 void charlcd_poke(struct charlcd *lcd)
 {
-	struct charlcd_priv *priv = to_priv(lcd);
+	struct charlcd_priv *priv = charlcd_to_priv(lcd);
 
 	if (!lcd->ops->backlight)
 		return;
@@ -152,7 +152,7 @@ EXPORT_SYMBOL_GPL(charlcd_poke);
 
 static void charlcd_gotoxy(struct charlcd *lcd)
 {
-	struct charlcd_priv *priv = to_priv(lcd);
+	struct charlcd_priv *priv = charlcd_to_priv(lcd);
 	unsigned int addr;
 
 	/*
@@ -170,7 +170,7 @@ static void charlcd_gotoxy(struct charlcd *lcd)
 
 static void charlcd_home(struct charlcd *lcd)
 {
-	struct charlcd_priv *priv = to_priv(lcd);
+	struct charlcd_priv *priv = charlcd_to_priv(lcd);
 
 	priv->addr.x = 0;
 	priv->addr.y = 0;
@@ -179,7 +179,7 @@ static void charlcd_home(struct charlcd *lcd)
 
 static void charlcd_print(struct charlcd *lcd, char c)
 {
-	struct charlcd_priv *priv = to_priv(lcd);
+	struct charlcd_priv *priv = charlcd_to_priv(lcd);
 
 	if (priv->addr.x < lcd->bwidth) {
 		if (lcd->char_conv)
@@ -211,7 +211,7 @@ static void charlcd_clear_fast(struct charlcd *lcd)
 /* clears the display and resets X/Y */
 static void charlcd_clear_display(struct charlcd *lcd)
 {
-	struct charlcd_priv *priv = to_priv(lcd);
+	struct charlcd_priv *priv = charlcd_to_priv(lcd);
 
 	lcd->ops->write_cmd(lcd, LCD_CMD_DISPLAY_CLEAR);
 	priv->addr.x = 0;
@@ -223,7 +223,7 @@ static void charlcd_clear_display(struct charlcd *lcd)
 static int charlcd_init_display(struct charlcd *lcd)
 {
 	void (*write_cmd_raw)(struct charlcd *lcd, int cmd);
-	struct charlcd_priv *priv = to_priv(lcd);
+	struct charlcd_priv *priv = charlcd_to_priv(lcd);
 	u8 init;
 
 	if (lcd->ifwidth != 4 && lcd->ifwidth != 8)
@@ -369,7 +369,7 @@ static bool parse_xy(const char *s, unsigned long *x, unsigned long *y)
 
 static inline int handle_lcd_special_code(struct charlcd *lcd)
 {
-	struct charlcd_priv *priv = to_priv(lcd);
+	struct charlcd_priv *priv = charlcd_to_priv(lcd);
 
 	/* LCD special codes */
 
@@ -580,7 +580,7 @@ static inline int handle_lcd_special_code(struct charlcd *lcd)
 
 static void charlcd_write_char(struct charlcd *lcd, char c)
 {
-	struct charlcd_priv *priv = to_priv(lcd);
+	struct charlcd_priv *priv = charlcd_to_priv(lcd);
 
 	/* first, we'll test if we're in escape mode */
 	if ((c != '\n') && priv->esc_seq.len >= 0) {
@@ -705,7 +705,7 @@ static ssize_t charlcd_write(struct file *file, const char __user *buf,
 
 static int charlcd_open(struct inode *inode, struct file *file)
 {
-	struct charlcd_priv *priv = to_priv(the_charlcd);
+	struct charlcd_priv *priv = charlcd_to_priv(the_charlcd);
 	int ret;
 
 	ret = -EBUSY;
@@ -766,7 +766,7 @@ static void charlcd_puts(struct charlcd *lcd, const char *s)
 /* initialize the LCD driver */
 static int charlcd_init(struct charlcd *lcd)
 {
-	struct charlcd_priv *priv = to_priv(lcd);
+	struct charlcd_priv *priv = charlcd_to_priv(lcd);
 	int ret;
 
 	if (lcd->ops->backlight) {
@@ -866,7 +866,7 @@ EXPORT_SYMBOL_GPL(charlcd_register);
 
 int charlcd_unregister(struct charlcd *lcd)
 {
-	struct charlcd_priv *priv = to_priv(lcd);
+	struct charlcd_priv *priv = charlcd_to_priv(lcd);
 
 	unregister_reboot_notifier(&panel_notifier);
 	charlcd_puts(lcd, "\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
Signature · 1 line
2.20.1
reviewer Reviewed-by on v2 2/5
#4 lore raw

Re: [PATCH v2 2/5] auxdisplay: charlcd: Move to_priv() to charlcd namespace

Hi Andy,

Quoted 2 lines · Andy Shevchenko

In order to be more particular in names, rename to_priv() macro to charlcd_to_priv().

As this is a macro, not a function, the name doesn't end up as a symbol in the binary anyway, and it's for internal use by the driver only.

Quoted 4 lines

No functional change intended.

Cc Geert Uytterhoeven <[email protected]>

Regardless:

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
#5 lore raw

Re: [PATCH v2 2/5] auxdisplay: charlcd: Move to_priv() to charlcd namespace

Quoted 9 lines · Geert Uytterhoeven

Hi Andy,

Quoted 2 lines · Andy Shevchenko

In order to be more particular in names, rename to_priv() macro to charlcd_to_priv().

As this is a macro, not a function, the name doesn't end up as a symbol in the binary anyway, and it's for internal use by the driver only.

I understand this, and I did in order to avoid potential collisions in the future.

Quoted 8 lines
Quoted 5 lines

No functional change intended.

Cc Geert Uytterhoeven <[email protected]>

Regardless:

Thanks!

Quoted 11 lines

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
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

PATCH v2 3/5 auxdisplay: charlcd: Introduce charlcd_free() helper

In reply to Andy Shevchenko

The charlcd_free() is a counterpart to charlcd_alloc() and should be called symmetrically on tear down.

Cc Geert Uytterhoeven <[email protected]>
---
 drivers/auxdisplay/charlcd.c | 6 ++++++
 include/misc/charlcd.h       | 1 +
 2 files changed, 7 insertions(+)
drivers/auxdisplay/charlcd.c +6 -0
index 407acd22efa8..a351a9400054 100644
@@ -818,6 +818,12 @@ struct charlcd *charlcd_alloc(unsigned int drvdata_size)
 }
 EXPORT_SYMBOL_GPL(charlcd_alloc);
 
+void charlcd_free(struct charlcd *lcd)
+{
+	kfree(charlcd_to_priv(lcd));
+}
+EXPORT_SYMBOL_GPL(charlcd_free);
+
 static int panel_notify_sys(struct notifier_block *this, unsigned long code,
 			    void *unused)
 {
include/misc/charlcd.h +1 -0
index 23f61850f363..1832402324ce 100644
@@ -35,6 +35,7 @@ struct charlcd_ops {
 };
 
 struct charlcd *charlcd_alloc(unsigned int drvdata_size);
+void charlcd_free(struct charlcd *lcd);
 
 int charlcd_register(struct charlcd *lcd);
 int charlcd_unregister(struct charlcd *lcd);
Signature · 1 line
2.20.1
reviewer Reviewed-by on v2 3/5
#7 lore raw

Re: [PATCH v2 3/5] auxdisplay: charlcd: Introduce charlcd_free() helper

Quoted 5 lines · Andy Shevchenko

The charlcd_free() is a counterpart to charlcd_alloc() and should be called symmetrically on tear down.

Cc Geert Uytterhoeven <[email protected]>

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
#8 lore raw

PATCH v2 4/5 auxdisplay: panel: Convert to use charlcd_free()

In reply to Andy Shevchenko

Convert to use charlcd_free() instead of kfree() for sake of type check.

---
 drivers/auxdisplay/panel.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/auxdisplay/panel.c +2 -2
index 21b9b2f2470a..e06de63497cf 100644
@@ -1620,7 +1620,7 @@ static void panel_attach(struct parport *port)
 	if (lcd.enabled)
 		charlcd_unregister(lcd.charlcd);
 err_unreg_device:
-	kfree(lcd.charlcd);
+	charlcd_free(lcd.charlcd);
 	lcd.charlcd = NULL;
 	parport_unregister_device(pprt);
 	pprt = NULL;
@@ -1647,7 +1647,7 @@ static void panel_detach(struct parport *port)
 	if (lcd.enabled) {
 		charlcd_unregister(lcd.charlcd);
 		lcd.initialized = false;
-		kfree(lcd.charlcd);
+		charlcd_free(lcd.charlcd);
 		lcd.charlcd = NULL;
 	}
 
Signature · 1 line
2.20.1
reviewer Reviewed-by on v2 4/5
#9 lore raw

Re: [PATCH v2 4/5] auxdisplay: panel: Convert to use charlcd_free()

Quoted 3 lines · Andy Shevchenko

Convert to use charlcd_free() instead of kfree() for sake of type check.

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
#10 lore raw

PATCH v2 5/5 auxdisplay: hd44780: Convert to use charlcd_free()

In reply to Andy Shevchenko

Convert to use charlcd_free() instead of kfree() for sake of type check.

---
 drivers/auxdisplay/hd44780.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/auxdisplay/hd44780.c +2 -2
index 3cde351fb5c9..ab15b64707ad 100644
@@ -271,7 +271,7 @@ static int hd44780_probe(struct platform_device *pdev)
 	return 0;
 
 fail:
-	kfree(lcd);
+	charlcd_free(lcd);
 	return ret;
 }
 
@@ -281,7 +281,7 @@ static int hd44780_remove(struct platform_device *pdev)
 
 	charlcd_unregister(lcd);
 
-	kfree(lcd);
+	charlcd_free(lcd);
 	return 0;
 }
 
Signature · 1 line
2.20.1
reviewer Reviewed-by on v2 5/5
#11 lore raw

Re: [PATCH v2 5/5] auxdisplay: hd44780: Convert to use charlcd_free()

Quoted 3 lines · Andy Shevchenko

Convert to use charlcd_free() instead of kfree() for sake of type check.

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
#12 lore raw
In reply to Andy Shevchenko
Quoted 3 lines · Andy Shevchenko

I have found a memory leak in hd44780 and it becomes that we have no counterpart to charlcd_alloc() that developers can easily miss.

Side-note now that I see these patches: I forgot to CC you in a series for charlcd that we got from Mans a week or two ago -- please take a chance to review them if you want! Also pinging Geert & Willy again in case they want to take a look.

Cheers,
Miguel

#13 lore raw
Quoted 10 lines · Miguel Ojeda
Quoted 3 lines · Andy Shevchenko

I have found a memory leak in hd44780 and it becomes that we have no counterpart to charlcd_alloc() that developers can easily miss.

Side-note now that I see these patches: I forgot to CC you in a series for charlcd that we got from Mans a week or two ago -- please take a chance to review them if you want! Also pinging Geert & Willy again in case they want to take a look.

Done.

It seems these two series are orthogonal to each other, so, can be applied separately.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#14 lore raw
Quoted 3 lines · Andy Shevchenko

It seems these two series are orthogonal to each other, so, can be applied separately.

Sent to -next for a few days.

Cheers,
Miguel