Skip to contentSkip to conversation

clk: mvebu: ap-cpu-clk: fix refcount leak

PATCH In review Alexander A. Klimov 8 participants 21 messages May 26 – Dec 1

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

PATCH 1/1 clk: mvebu: ap-cpu-clk: fix refcount leak

Every value returned from of_clk_get() is supposed to be cleaned up via clk_put() once not needed anymore.

Fixes f756e362d938 ("clk: mvebu: add CPU clock driver for Armada 7K/8K")
---
 drivers/clk/mvebu/ap-cpu-clk.c | 1 +
 1 file changed, 1 insertion(+)
drivers/clk/mvebu/ap-cpu-clk.c +1 -0
index 1e44ace7d951..7721d5fe50bc 100644
@@ -312,6 +312,7 @@ static int ap_cpu_clock_probe(struct platform_device *pdev)
 		}
 		parent_name =  __clk_get_name(parent);
 		clk_name[12] += cluster_index;
+		clk_put(parent);
 		ap_cpu_clk[cluster_index].clk_name =
 			ap_cp_unique_name(dev, np->parent, clk_name);
 		ap_cpu_clk[cluster_index].cluster = cluster_index;
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/1 clocksource/drivers/clps711x: fix refcount leak

Every value returned from of_clk_get() is supposed to be cleaned up via clk_put() once not needed anymore.

Fixes f0b7fabec327 ("clocksource: clps711x: Add CLPS711X clocksource driver")
---
 drivers/clocksource/clps711x-timer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/clocksource/clps711x-timer.c +4 -1
index bb0a44adaf28..d9298a5f6005 100644
@@ -76,7 +76,7 @@ static int __init _clps711x_clkevt_init(struct clk *clock, void __iomem *base,
 static int __init clps711x_timer_init(struct device_node *np)
 {
 	unsigned int irq = irq_of_parse_and_map(np, 0);
-	struct clk *clock = of_clk_get(np, 0);
+	struct clk *clock;
 	void __iomem *base = of_iomap(np, 0);
 	int ret = 0;
 
@@ -86,6 +86,8 @@ static int __init clps711x_timer_init(struct device_node *np)
 		ret = -EINVAL;
 		goto unmap_io;
 	}
+
+	clock = of_clk_get(np, 0);
 	if (IS_ERR(clock)) {
 		ret = PTR_ERR(clock);
 		goto unmap_io;
@@ -103,6 +105,7 @@ static int __init clps711x_timer_init(struct device_node *np)
 		break;
 	}
 
+	clk_put(clock);
 unmap_io:
 	iounmap(base);
 	return ret;
Signature · 1 line
2.54.0
#2 lore raw

[clocksource: timers/drivers/next] clocksource/drivers/clps711x: Fix clk refcount leak

The following commit has been merged into the timers/drivers/next branch of clocksource:

The following commit has been merged into the timers/drivers/next branch of clocksource:

Commit-ID:     81c90d8c7670d3f66bff49b45f0ef67ab9f10b8c
Gitweb:        https://git.kernel.org/pub/scm/linux/kernel/git/daniel.lezcano/linux.git//81c90d8c7670d3f66bff49b45f0ef67ab9f10b8c
Author:        Alexander A. Klimov <[email protected]>
AuthorDate:    Tue, 26 May 2026 20:40:57 +02:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Wed, 25 Nov 2026 18:41:13 +01:00

clocksource/drivers/clps711x: Fix clk refcount leak

Every value returned from of_clk_get() is supposed to be cleaned up via clk_put() once not needed anymore.

Fixes f0b7fabec327 ("clocksource: clps711x: Add CLPS711X clocksource driver")
---
 drivers/clocksource/clps711x-timer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/clocksource/clps711x-timer.c +4 -1
index 63ae3a6..2fa7ce0 100644
@@ -76,7 +76,7 @@ static int __init _clps711x_clkevt_init(struct clk *clock, void __iomem *base,
 static int __init clps711x_timer_init(struct device_node *np)
 {
 	unsigned int irq = irq_of_parse_and_map(np, 0);
-	struct clk *clock = of_clk_get(np, 0);
+	struct clk *clock;
 	void __iomem *base = of_iomap(np, 0);
 	int ret = 0;
 
@@ -86,6 +86,8 @@ static int __init clps711x_timer_init(struct device_node *np)
 		ret = -EINVAL;
 		goto unmap_io;
 	}
+
+	clock = of_clk_get(np, 0);
 	if (IS_ERR(clock)) {
 		ret = PTR_ERR(clock);
 		goto unmap_io;
@@ -103,6 +105,7 @@ static int __init clps711x_timer_init(struct device_node *np)
 		break;
 	}
 
+	clk_put(clock);
 unmap_io:
 	iounmap(base);
 	return ret;
#3 lore raw

[clocksource: timers/drivers/next] clocksource/drivers/clps711x: Fix clk refcount leak

In reply to Alexander A. Klimov
The following commit has been merged into the timers/drivers/next branch of clocksource:

The following commit has been merged into the timers/drivers/next branch of clocksource:

Commit-ID:     c08ff564ac1fcbfd33aa3a14e3768b879df56c4d
Gitweb:        https://git.kernel.org/pub/scm/linux/kernel/git/daniel.lezcano/linux.git//c08ff564ac1fcbfd33aa3a14e3768b879df56c4d
Author:        Alexander A. Klimov <[email protected]>
AuthorDate:    Tue, 26 May 2026 20:40:57 +02:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Tue, 01 Dec 2026 11:10:12 +01:00

clocksource/drivers/clps711x: Fix clk refcount leak

Every value returned from of_clk_get() is supposed to be cleaned up via clk_put() once not needed anymore.

Fixes f0b7fabec327 ("clocksource: clps711x: Add CLPS711X clocksource driver")
---
 drivers/clocksource/clps711x-timer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/clocksource/clps711x-timer.c +4 -1
index 63ae3a6..2fa7ce0 100644
@@ -76,7 +76,7 @@ static int __init _clps711x_clkevt_init(struct clk *clock, void __iomem *base,
 static int __init clps711x_timer_init(struct device_node *np)
 {
 	unsigned int irq = irq_of_parse_and_map(np, 0);
-	struct clk *clock = of_clk_get(np, 0);
+	struct clk *clock;
 	void __iomem *base = of_iomap(np, 0);
 	int ret = 0;
 
@@ -86,6 +86,8 @@ static int __init clps711x_timer_init(struct device_node *np)
 		ret = -EINVAL;
 		goto unmap_io;
 	}
+
+	clock = of_clk_get(np, 0);
 	if (IS_ERR(clock)) {
 		ret = PTR_ERR(clock);
 		goto unmap_io;
@@ -103,6 +105,7 @@ static int __init clps711x_timer_init(struct device_node *np)
 		break;
 	}
 
+	clk_put(clock);
 unmap_io:
 	iounmap(base);
 	return ret;
#4 lore raw

PATCH 1/1 clocksource/timer-econet-en751221: fix refcount leak

In reply to Alexander A. Klimov

Every value returned from of_clk_get() is supposed to be cleaned up via clk_put() once not needed anymore.

Fixes 3b4c33ac87d0 ("clocksource/drivers: Add EcoNet Timer HPT driver")
---
 drivers/clocksource/timer-econet-en751221.c | 1 +
 1 file changed, 1 insertion(+)
drivers/clocksource/timer-econet-en751221.c +1 -0
index 4008076b1a21..1859335345b5 100644
@@ -181,6 +181,7 @@ static int __init timer_init(struct device_node *np)
 	}
 
 	econet_timer.freq_hz = clk_get_rate(clk);
+	clk_put(clk);
 
 	for (int i = 0; i < num_blocks; i++) {
 		econet_timer.membase[i] = of_iomap(np, i);
Signature · 1 line
2.54.0
#5 lore raw

Re: [PATCH] clocksource/timer-econet-en751221: fix refcount leak

Quoted 2 lines · Alexander A. Klimov

Every value returned from of_clk_get() is supposed to be cleaned up via clk_put() once not needed anymore.

Thank you for this. Since I have an open patchset on this driver, I've integrated your patch in v6.

Thanks,

Caleb

Quoted 19 lines
Fixes 3b4c33ac87d0 ("clocksource/drivers: Add EcoNet Timer HPT driver")
---
  drivers/clocksource/timer-econet-en751221.c | 1 +
  1 file changed, 1 insertion(+)
drivers/clocksource/timer-econet-en751221.c +1 -0
index 4008076b1a21..1859335345b5 100644
@@ -181,6 +181,7 @@ static int __init timer_init(struct device_node *np)
  	}
 
  	econet_timer.freq_hz = clk_get_rate(clk);
+	clk_put(clk);
 
  	for (int i = 0; i < num_blocks; i++) {
  		econet_timer.membase[i] = of_iomap(np, i);
#6 lore raw

PATCH 1/1 microblaze: CCF: fix refcount leak

In reply to Alexander A. Klimov

Every value returned from of_clk_get() is supposed to be cleaned up via clk_put() once not needed anymore.

Fixes c1120542b99a ("microblaze: Add support for CCF")
---
 arch/microblaze/kernel/cpu/cpuinfo.c | 1 +
 1 file changed, 1 insertion(+)
arch/microblaze/kernel/cpu/cpuinfo.c +1 -0
index cd9b4450763b..32c508e85408 100644
@@ -133,6 +133,7 @@ void __init setup_cpuinfo_clk(void)
 		cpuinfo.cpu_clock_freq = fcpu(cpu, "timebase-frequency");
 	} else {
 		cpuinfo.cpu_clock_freq = clk_get_rate(clk);
+		clk_put(clk);
 	}
 
 	if (!cpuinfo.cpu_clock_freq) {
Signature · 1 line
2.54.0
maintainer Applied
#7 lore raw

Re: [PATCH] microblaze: CCF: fix refcount leak

Quoted 14 lines · Alexander A. Klimov

Every value returned from of_clk_get() is supposed to be cleaned up via clk_put() once not needed anymore.

Fixes c1120542b99a ("microblaze: Add support for CCF")
---
  arch/microblaze/kernel/cpu/cpuinfo.c | 1 +
  1 file changed, 1 insertion(+)
arch/microblaze/kernel/cpu/cpuinfo.c +0 -0
index cd9b4450763b..32c508e85408 100644

arch/microblaze/kernel/cpu/cpuinfo.c void __init setup_cpuinfo_clk(void) · line 139

Quoted 7 lines
arch/microblaze/kernel/cpu/cpuinfo.c +1 -0
@@ -133,6 +133,7 @@ void __init setup_cpuinfo_clk(void)
  		cpuinfo.cpu_clock_freq = fcpu(cpu, "timebase-frequency");
  	} else {
  		cpuinfo.cpu_clock_freq = clk_get_rate(clk);
+		clk_put(clk);
  	}
 
  	if (!cpuinfo.cpu_clock_freq) {

Applied.
M

Signature · 6 lines
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Xilinx Microblaze
Maintainer of Linux kernel - Xilinx Zynq ARM and ZynqMP/Versal ARM64 SoCs
U-Boot custodian - Xilinx Microblaze/Zynq/ZynqMP/Versal/Versal NET SoCs
TF-A maintainer - Xilinx ZynqMP/Versal/Versal NET SoCs
#8 lore raw

PATCH 1/1 drm/vc4: fix krealloc() memory leak

In reply to Alexander A. Klimov

Don't just overwrite the original pointer passed to krealloc() with its return value without checking latter:

    MEM = krealloc(MEM, SZ, GFP);

If krealloc() returns NULL, that erases the pointer to the still allocated memory, hence leaks this memory. Instead, use a temporary variable, check it's not NULL and only then assign it to the original pointer:

    TMP = krealloc(MEM, SZ, GFP);
    if (!TMP) return;
    MEM = TMP;
Fixes 6d45c81d229d ("drm/vc4: Add support for branching in shader validation.")
---
 drivers/gpu/drm/vc4/vc4_validate_shaders.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/gpu/drm/vc4/vc4_validate_shaders.c +6 -6
index d48cf76983c0..af48758592f9 100644
@@ -291,14 +291,14 @@ static bool require_uniform_address_uniform(struct vc4_validated_shader_info *va
 	uint32_t o = validated_shader->num_uniform_addr_offsets;
 	uint32_t num_uniforms = validated_shader->uniforms_size / 4;
 
-	validated_shader->uniform_addr_offsets =
-		krealloc(validated_shader->uniform_addr_offsets,
-			 (o + 1) *
-			 sizeof(*validated_shader->uniform_addr_offsets),
-			 GFP_KERNEL);
-	if (!validated_shader->uniform_addr_offsets)
+	void *mem = krealloc(validated_shader->uniform_addr_offsets,
+			     (o + 1) *
+			     sizeof(*validated_shader->uniform_addr_offsets),
+			     GFP_KERNEL);
+	if (!mem)
 		return false;
 
+	validated_shader->uniform_addr_offsets = mem;
 	validated_shader->uniform_addr_offsets[o] = num_uniforms;
 	validated_shader->num_uniform_addr_offsets++;
 
Signature · 1 line
2.54.0
reviewer
#9 lore raw

Re: [PATCH] drm/vc4: fix krealloc() memory leak

Hi Alexander,

LGTM, but a small styling nit:

Quoted 25 lines · Alexander A. Klimov

Don't just overwrite the original pointer passed to krealloc() with its return value without checking latter:

     MEM = krealloc(MEM, SZ, GFP);

If krealloc() returns NULL, that erases the pointer to the still allocated memory, hence leaks this memory. Instead, use a temporary variable, check it's not NULL and only then assign it to the original pointer:

     TMP = krealloc(MEM, SZ, GFP);
     if (!TMP) return;
     MEM = TMP;
Fixes 6d45c81d229d ("drm/vc4: Add support for branching in shader validation.")
---
  drivers/gpu/drm/vc4/vc4_validate_shaders.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
drivers/gpu/drm/vc4/vc4_validate_shaders.c +0 -0
index d48cf76983c0..af48758592f9 100644

drivers/gpu/drm/vc4/vc4_validate_shaders.c static bool require_uniform_address_uniform(struct vc4_validated_shader_info *va · line 292

Quoted 2 lines
drivers/gpu/drm/vc4/vc4_validate_shaders.c +0 -0
@@ -291,14 +291,14 @@ static bool require_uniform_address_uniform(struct vc4_validated_shader_info *va
  	uint32_t o = validated_shader->num_uniform_addr_offsets;
  	uint32_t num_uniforms = validated_shader->uniforms_size / 4;

Declare the variable here.

drivers/gpu/drm/vc4/vc4_validate_shaders.c static bool require_uniform_address_uniform(struct vc4_validated_shader_info *va · line 294

Quoted 8 lines
drivers/gpu/drm/vc4/vc4_validate_shaders.c +1 -6
-	validated_shader->uniform_addr_offsets =
-		krealloc(validated_shader->uniform_addr_offsets,
-			 (o + 1) *
-			 sizeof(*validated_shader->uniform_addr_offsets),
-			 GFP_KERNEL);
-	if (!validated_shader->uniform_addr_offsets)
+	void *mem = krealloc(validated_shader->uniform_addr_offsets,

Instead of void *, you should probably use uint32_t *.

Best regards,
- Maíra

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_validate_shaders.c +5 -0
+			     (o + 1) *
+			     sizeof(*validated_shader->uniform_addr_offsets),
+			     GFP_KERNEL);
+	if (!mem)
  		return false;
 
+	validated_shader->uniform_addr_offsets = mem;
  	validated_shader->uniform_addr_offsets[o] = num_uniforms;
  	validated_shader->num_uniform_addr_offsets++;
 
#10 lore raw

Re: [PATCH] drm/vc4: fix krealloc() memory leak

Quoted 25 lines · Maíra Canal

Hi Alexander,

LGTM, but a small styling nit:

Quoted 20 lines · Alexander A. Klimov

Don't just overwrite the original pointer passed to krealloc() with its return value without checking latter:

     MEM = krealloc(MEM, SZ, GFP);

If krealloc() returns NULL, that erases the pointer to the still allocated memory, hence leaks this memory. Instead, use a temporary variable, check it's not NULL and only then assign it to the original pointer:

     TMP = krealloc(MEM, SZ, GFP);
     if (!TMP) return;
     MEM = TMP;

Fixes 6d45c81d229d ("drm/vc4: Add support for branching in shader validation.")

---
  drivers/gpu/drm/vc4/vc4_validate_shaders.c | 12 ++++++------

  1 file changed, 6 insertions(+), 6 deletions(-)
Quoted 9 lines

drivers/gpu/drm/vc4/vc4_validate_shaders.c static bool require_uniform_address_uniform(struct vc4_validated_shader_info *va · line 292

Quoted 7 lines
drivers/gpu/drm/vc4/vc4_validate_shaders.c +0 -0
index d48cf76983c0..af48758592f9 100644
@@ -291,14 +291,14 @@ static bool require_uniform_address_uniform(struct vc4_validated_shader_info *va
       uint32_t o = validated_shader->num_uniform_addr_offsets;
       uint32_t num_uniforms = validated_shader->uniforms_size / 4;

Declare the variable here.

Strange...
It comes already directly after num_uniforms, doesn't it? At least when I apply my patch to Linus' master.

reviewer
#11 lore raw

Re: [PATCH] drm/vc4: fix krealloc() memory leak

Hi Alexander,

Quoted 39 lines · Alexander A. Klimov
Quoted 25 lines · Maíra Canal

Hi Alexander,

LGTM, but a small styling nit:

Quoted 20 lines · Alexander A. Klimov

Don't just overwrite the original pointer passed to krealloc() with its return value without checking latter:

     MEM = krealloc(MEM, SZ, GFP);

If krealloc() returns NULL, that erases the pointer to the still allocated memory, hence leaks this memory. Instead, use a temporary variable, check it's not NULL and only then assign it to the original pointer:

     TMP = krealloc(MEM, SZ, GFP);
     if (!TMP) return;
     MEM = TMP;

Fixes 6d45c81d229d ("drm/vc4: Add support for branching in shader validation.")

---
  drivers/gpu/drm/vc4/vc4_validate_shaders.c | 12 ++++++------

  1 file changed, 6 insertions(+), 6 deletions(-)
Quoted 10 lines
Quoted 8 lines
drivers/gpu/drm/vc4/vc4_validate_shaders.c +0 -0
index d48cf76983c0..af48758592f9 100644
@@ -291,14 +291,14 @@ static bool 

require_uniform_address_uniform(struct vc4_validated_shader_info *va uint32_t o = validated_shader->num_uniform_addr_offsets; uint32_t num_uniforms = validated_shader->uniforms_size / 4;

Declare the variable here.

Quoted 2 lines

Strange...
It comes already directly after num_uniforms, doesn't it?

Yeah, it does. However, usually, we declare the variables and then we proceed to the functions. Check, for example, the function record_texture_sample() in vc4.

Best regards,
- Maíra

Quoted 1 line

At least when I apply my patch to Linus' master.

#12 lore raw

PATCH v2 1/1 drm/vc4: fix krealloc() memory leak

In reply to Maíra Canal

Don't just overwrite the original pointer passed to krealloc() with its return value without checking latter:

    MEM = krealloc(MEM, SZ, GFP);

If krealloc() returns NULL, that erases the pointer to the still allocated memory, hence leaks this memory. Instead, use a temporary variable, check it's not NULL and only then assign it to the original pointer:

    TMP = krealloc(MEM, SZ, GFP);
    if (!TMP) return;
    MEM = TMP;
Fixes 6d45c81d229d ("drm/vc4: Add support for branching in shader validation.")

---
v2: Declare the variable explicitly
v2: Instead of void *, use u32 *
v2: While on it, enhance variable name

[✓] scripts/checkpatch.pl --strict
[✓] allmodconfig compiled (i686, LLVM)
[✓] localyesconfig booted (IBM T43)

Note to myself: use [email protected]

 drivers/gpu/drm/vc4/vc4_validate_shaders.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/gpu/drm/vc4/vc4_validate_shaders.c +7 -6
index d48cf76983c0..6f52b4db9cc9 100644
@@ -290,15 +290,16 @@ static bool require_uniform_address_uniform(struct vc4_validated_shader_info *va
 {
 	uint32_t o = validated_shader->num_uniform_addr_offsets;
 	uint32_t num_uniforms = validated_shader->uniforms_size / 4;
+	u32 *offsets;
 
-	validated_shader->uniform_addr_offsets =
-		krealloc(validated_shader->uniform_addr_offsets,
-			 (o + 1) *
-			 sizeof(*validated_shader->uniform_addr_offsets),
-			 GFP_KERNEL);
-	if (!validated_shader->uniform_addr_offsets)
+	offsets = krealloc(validated_shader->uniform_addr_offsets,
+			   (o + 1) *
+			   sizeof(*validated_shader->uniform_addr_offsets),
+			   GFP_KERNEL);
+	if (!offsets)
 		return false;
 
+	validated_shader->uniform_addr_offsets = offsets;
 	validated_shader->uniform_addr_offsets[o] = num_uniforms;
 	validated_shader->num_uniform_addr_offsets++;
 
Signature · 1 line
2.54.0
#13 lore raw

Re: [PATCH v2] drm/vc4: fix krealloc() memory leak

Quoted 2 lines · Alexander A. Klimov

Don't just overwrite the original pointer passed to krealloc() with its return value without checking latter:

[]

drivers/gpu/drm/vc4/vc4_validate_shaders.c

Quoted 1 line
drivers/gpu/drm/vc4/vc4_validate_shaders.c +0 -0

[]

Quoted 6 lines
drivers/gpu/drm/vc4/vc4_validate_shaders.c +1 -0
@@ -290,15 +290,16 @@ static bool require_uniform_address_uniform(struct vc4_validated_shader_info *va
 {
 	uint32_t o = validated_shader->num_uniform_addr_offsets;
 	uint32_t num_uniforms = validated_shader->uniforms_size / 4;
+	u32 *offsets;
 

drivers/gpu/drm/vc4/vc4_validate_shaders.c static bool require_uniform_address_uniform(struct vc4_validated_shader_info *va · line 298

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_validate_shaders.c +4 -6
-	validated_shader->uniform_addr_offsets =
-		krealloc(validated_shader->uniform_addr_offsets,
-			 (o + 1) *
-			 sizeof(*validated_shader->uniform_addr_offsets),
-			 GFP_KERNEL);
-	if (!validated_shader->uniform_addr_offsets)
+	offsets = krealloc(validated_shader->uniform_addr_offsets,
+			   (o + 1) *
+			   sizeof(*validated_shader->uniform_addr_offsets),
+			   GFP_KERNEL);

krealloc_array ?

Quoted 6 lines
drivers/gpu/drm/vc4/vc4_validate_shaders.c +2 -0
+	if (!offsets)
 		return false;
 
+	validated_shader->uniform_addr_offsets = offsets;
 	validated_shader->uniform_addr_offsets[o] = num_uniforms;
 	validated_shader->num_uniform_addr_offsets++;
#14 lore raw

PATCH v3 1/1 drm/vc4: fix krealloc() memory leak

Don't just overwrite the original pointer passed to krealloc() with its return value without checking latter:

    MEM = krealloc(MEM, SZ, GFP);

If krealloc() returns NULL, that erases the pointer to the still allocated memory, hence leaks this memory. Instead, use a temporary variable, check it's not NULL and only then assign it to the original pointer:

    TMP = krealloc(MEM, SZ, GFP);
    if (!TMP) return;
    MEM = TMP;

While on it, use krealloc_array().

Fixes 6d45c81d229d ("drm/vc4: Add support for branching in shader validation.")

---
v2: Declare the variable explicitly
v2: Instead of void *, use u32 *
v2: While on it, enhance variable name
v3: Use krealloc_array()

[✓] scripts/checkpatch.pl --strict
[✓] allmodconfig compiled (i686, LLVM)
[✓] localyesconfig booted (IBM T43)

Note to myself: use --in-reply-to=e1edeadb7c161580a5cd5c7e642dc1b28db8ee86.camel@perches.com

 drivers/gpu/drm/vc4/vc4_validate_shaders.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/gpu/drm/vc4/vc4_validate_shaders.c +7 -6
index d48cf76983c0..66502a6a4a8e 100644
@@ -290,15 +290,16 @@ static bool require_uniform_address_uniform(struct vc4_validated_shader_info *va
 {
 	uint32_t o = validated_shader->num_uniform_addr_offsets;
 	uint32_t num_uniforms = validated_shader->uniforms_size / 4;
+	u32 *offsets;
 
-	validated_shader->uniform_addr_offsets =
-		krealloc(validated_shader->uniform_addr_offsets,
-			 (o + 1) *
-			 sizeof(*validated_shader->uniform_addr_offsets),
-			 GFP_KERNEL);
-	if (!validated_shader->uniform_addr_offsets)
+	offsets = krealloc_array(validated_shader->uniform_addr_offsets,
+				 o + 1,
+				 sizeof(*validated_shader->uniform_addr_offsets),
+				 GFP_KERNEL);
+	if (!offsets)
 		return false;
 
+	validated_shader->uniform_addr_offsets = offsets;
 	validated_shader->uniform_addr_offsets[o] = num_uniforms;
 	validated_shader->num_uniform_addr_offsets++;
 
Signature · 1 line
2.54.0
reviewer Reviewed-by on v3 1/1
#15 lore raw

Re: [PATCH v3] drm/vc4: fix krealloc() memory leak

Quoted 10 lines · Alexander A. Klimov

Don't just overwrite the original pointer passed to krealloc() with its return value without checking latter:

     MEM = krealloc(MEM, SZ, GFP);

If krealloc() returns NULL, that erases the pointer to the still allocated memory, hence leaks this memory. Instead, use a temporary variable, check it's not NULL and only then assign it to the original pointer:

Quoted 8 lines
     TMP = krealloc(MEM, SZ, GFP);
     if (!TMP) return;
     MEM = TMP;

While on it, use krealloc_array().

Fixes 6d45c81d229d ("drm/vc4: Add support for branching in shader validation.")

Best regards,
- Maíra

Quoted 42 lines

---
v2: Declare the variable explicitly
v2: Instead of void *, use u32 *
v2: While on it, enhance variable name
v3: Use krealloc_array()

  [✓] scripts/checkpatch.pl --strict
  [✓] allmodconfig compiled (i686, LLVM)
  [✓] localyesconfig booted (IBM T43)

  Note to myself: use --in-reply-to=e1edeadb7c161580a5cd5c7e642dc1b28db8ee86.camel@perches.com
  drivers/gpu/drm/vc4/vc4_validate_shaders.c | 13 +++++++------
  1 file changed, 7 insertions(+), 6 deletions(-)
drivers/gpu/drm/vc4/vc4_validate_shaders.c +7 -6
index d48cf76983c0..66502a6a4a8e 100644
@@ -290,15 +290,16 @@ static bool require_uniform_address_uniform(struct vc4_validated_shader_info *va
  {
  	uint32_t o = validated_shader->num_uniform_addr_offsets;
  	uint32_t num_uniforms = validated_shader->uniforms_size / 4;
+	u32 *offsets;
 
-	validated_shader->uniform_addr_offsets =
-		krealloc(validated_shader->uniform_addr_offsets,
-			 (o + 1) *
-			 sizeof(*validated_shader->uniform_addr_offsets),
-			 GFP_KERNEL);
-	if (!validated_shader->uniform_addr_offsets)
+	offsets = krealloc_array(validated_shader->uniform_addr_offsets,
+				 o + 1,
+				 sizeof(*validated_shader->uniform_addr_offsets),
+				 GFP_KERNEL);
+	if (!offsets)
  		return false;
 
+	validated_shader->uniform_addr_offsets = offsets;
  	validated_shader->uniform_addr_offsets[o] = num_uniforms;
  	validated_shader->num_uniform_addr_offsets++;
 
reviewer Applied
#16 lore raw

Re: [PATCH v3] drm/vc4: fix krealloc() memory leak

In reply to Alexander A. Klimov

Hi Alexander,

Quoted 10 lines · Alexander A. Klimov

Don't just overwrite the original pointer passed to krealloc() with its return value without checking latter:

     MEM = krealloc(MEM, SZ, GFP);

If krealloc() returns NULL, that erases the pointer to the still allocated memory, hence leaks this memory. Instead, use a temporary variable, check it's not NULL and only then assign it to the original pointer:

Quoted 8 lines
     TMP = krealloc(MEM, SZ, GFP);
     if (!TMP) return;
     MEM = TMP;

While on it, use krealloc_array().

Fixes 6d45c81d229d ("drm/vc4: Add support for branching in shader validation.")

Applied to drm/misc/kernel (drm-misc-fixes).

Best Regards,
- Maíra

Quoted 42 lines

---
v2: Declare the variable explicitly
v2: Instead of void *, use u32 *
v2: While on it, enhance variable name
v3: Use krealloc_array()

  [✓] scripts/checkpatch.pl --strict
  [✓] allmodconfig compiled (i686, LLVM)
  [✓] localyesconfig booted (IBM T43)

  Note to myself: use --in-reply-to=e1edeadb7c161580a5cd5c7e642dc1b28db8ee86.camel@perches.com
  drivers/gpu/drm/vc4/vc4_validate_shaders.c | 13 +++++++------
  1 file changed, 7 insertions(+), 6 deletions(-)
drivers/gpu/drm/vc4/vc4_validate_shaders.c +7 -6
index d48cf76983c0..66502a6a4a8e 100644
@@ -290,15 +290,16 @@ static bool require_uniform_address_uniform(struct vc4_validated_shader_info *va
  {
  	uint32_t o = validated_shader->num_uniform_addr_offsets;
  	uint32_t num_uniforms = validated_shader->uniforms_size / 4;
+	u32 *offsets;
 
-	validated_shader->uniform_addr_offsets =
-		krealloc(validated_shader->uniform_addr_offsets,
-			 (o + 1) *
-			 sizeof(*validated_shader->uniform_addr_offsets),
-			 GFP_KERNEL);
-	if (!validated_shader->uniform_addr_offsets)
+	offsets = krealloc_array(validated_shader->uniform_addr_offsets,
+				 o + 1,
+				 sizeof(*validated_shader->uniform_addr_offsets),
+				 GFP_KERNEL);
+	if (!offsets)
  		return false;
 
+	validated_shader->uniform_addr_offsets = offsets;
  	validated_shader->uniform_addr_offsets[o] = num_uniforms;
  	validated_shader->num_uniform_addr_offsets++;
 
#17 lore raw

PATCH 1/1 ibmvnic: fix krealloc() memory leak

In reply to Alexander A. Klimov

Don't just overwrite the original pointer passed to krealloc() with its return value without checking latter:

    MEM = krealloc(MEM, SZ, GFP);

If krealloc() returns NULL, that erases the pointer to the still allocated memory, hence leaks this memory. Instead, use a temporary variable, check it's not NULL and only then assign it to the original pointer:

    TMP = krealloc(MEM, SZ, GFP);
    if (!TMP) return;
    MEM = TMP;
Fixes 4e6759be28e4 ("ibmvnic: Feature implementation of Vital Product Data (VPD) for the ibmvnic driver")
---
 drivers/net/ethernet/ibm/ibmvnic.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/net/ethernet/ibm/ibmvnic.c +8 -7
index 5a510eed335e..25d1d844ad19 100644
@@ -1761,8 +1761,9 @@ static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
 	union ibmvnic_crq crq;
 	int len = 0;
 	int rc;
+	unsigned char *buff = adapter->vpd->buff;
 
-	if (adapter->vpd->buff)
+	if (buff)
 		len = adapter->vpd->len;
 
 	mutex_lock(&adapter->fw_lock);
@@ -1788,17 +1789,17 @@ static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
 	if (!adapter->vpd->len)
 		return -ENODATA;
 
-	if (!adapter->vpd->buff)
-		adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
+	if (!buff)
+		buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
 	else if (adapter->vpd->len != len)
-		adapter->vpd->buff =
-			krealloc(adapter->vpd->buff,
-				 adapter->vpd->len, GFP_KERNEL);
+		buff = krealloc(buff,
+				adapter->vpd->len, GFP_KERNEL);
 
-	if (!adapter->vpd->buff) {
+	if (!buff) {
 		dev_err(dev, "Could allocate VPD buffer\n");
 		return -ENOMEM;
 	}
+	adapter->vpd->buff = buff;
 
 	adapter->vpd->dma_addr =
 		dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
Signature · 1 line
2.54.0
#18 lore raw

Re: [PATCH] ibmvnic: fix krealloc() memory leak

Hi Alex

You patch is missing the prefix with the target tree. Please have a look at [1] for more details on the workflow.

Quoted 25 lines · Alexander A. Klimov

Don't just overwrite the original pointer passed to krealloc() with its return value without checking latter:

    MEM = krealloc(MEM, SZ, GFP);

If krealloc() returns NULL, that erases the pointer to the still allocated memory, hence leaks this memory. Instead, use a temporary variable, check it's not NULL and only then assign it to the original pointer:

    TMP = krealloc(MEM, SZ, GFP);
    if (!TMP) return;
    MEM = TMP;
Fixes 4e6759be28e4 ("ibmvnic: Feature implementation of Vital Product Data (VPD) for the ibmvnic driver")
---
 drivers/net/ethernet/ibm/ibmvnic.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/net/ethernet/ibm/ibmvnic.c +0 -0
index 5a510eed335e..25d1d844ad19 100644

drivers/net/ethernet/ibm/ibmvnic.c static int ibmvnic_get_vpd(struct ibmvnic_adapter · line 1765

Quoted 5 lines
drivers/net/ethernet/ibm/ibmvnic.c +1 -0
@@ -1761,8 +1761,9 @@ static int ibmvnic_get_vpd(struct ibmvnic_adapter 
 *adapter)
 	union ibmvnic_crq crq;
 	int len = 0;
 	int rc;
+	unsigned char *buff = adapter->vpd->buff;

Should be reverse x-mas tree (longest to shortest).

Quoted 11 lines
drivers/net/ethernet/ibm/ibmvnic.c +1 -1
-	if (adapter->vpd->buff)
+	if (buff)
 		len = adapter->vpd->len;
 
 	mutex_lock(&adapter->fw_lock);
@@ -1788,17 +1789,17 @@ static int ibmvnic_get_vpd(struct 
 ibmvnic_adapter *adapter)
 	if (!adapter->vpd->len)
 		return -ENODATA;
 

drivers/net/ethernet/ibm/ibmvnic.c static int ibmvnic_get_vpd(struct · line 1797

Quoted 10 lines
drivers/net/ethernet/ibm/ibmvnic.c +4 -5
-	if (!adapter->vpd->buff)
-		adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
+	if (!buff)
+		buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
 	else if (adapter->vpd->len != len)
-		adapter->vpd->buff =
-			krealloc(adapter->vpd->buff,
-				 adapter->vpd->len, GFP_KERNEL);
+		buff = krealloc(buff,
+				adapter->vpd->len, GFP_KERNEL);

Dead branch? The only caller, init_resources(), kzalloc()s a fresh vpd right before, and resets run release_vpd_data() first, so vpd->buff is always NULL here and kzalloc() above always wins. The leak can't trigger, which makes the Fixes tag misleading.

drivers/net/ethernet/ibm/ibmvnic.c static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter) · line 1802

Quoted 7 lines
drivers/net/ethernet/ibm/ibmvnic.c +2 -1
-	if (!adapter->vpd->buff) {
+	if (!buff) {
 		dev_err(dev, "Could allocate VPD buffer\n");
 		return -ENOMEM;
 	}
+	adapter->vpd->buff = buff;

If you keep it anyway: on failure the old buffer stays in vpd->buff while vpd->len is already the new size, a mismatch the original avoided by NULLing. kfree() it (krealloc() does not free on failure) and NULL before -ENOMEM.

drivers/net/ethernet/ibm/ibmvnic.c static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter) · line 1805

Quoted 3 lines
drivers/net/ethernet/ibm/ibmvnic.c +0 -0
 	adapter->vpd->dma_addr =
 		dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,

[1] https://docs.kernel.org/process/maintainer-netdev.html

Thanks,
Nicolai

#19 lore raw

Re: [PATCH] ibmvnic: fix krealloc() memory leak

Quoted 4 lines · Nicolai Buchwitz

Hi Alex

You patch is missing the prefix with the target tree. Please have a look at [1] for more details on the workflow.

Damn. :-)

So far I've always got away with whatever I copied from the majority of git log --oneline --follow FILE

Quoted 44 lines · Alexander A. Klimov
Quoted 25 lines

Don't just overwrite the original pointer passed to krealloc() with its return value without checking latter:

    MEM = krealloc(MEM, SZ, GFP);

If krealloc() returns NULL, that erases the pointer to the still allocated memory, hence leaks this memory. Instead, use a temporary variable, check it's not NULL and only then assign it to the original pointer:

    TMP = krealloc(MEM, SZ, GFP);
    if (!TMP) return;
    MEM = TMP;

Fixes 4e6759be28e4 ("ibmvnic: Feature implementation of Vital Product Data (VPD) for the ibmvnic driver")

---
 drivers/net/ethernet/ibm/ibmvnic.c | 15 ++++++++-------

 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/net/ethernet/ibm/ibmvnic.c +0 -0
index 5a510eed335e..25d1d844ad19 100644

drivers/net/ethernet/ibm/ibmvnic.c static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter) · line 1764

Quoted 4 lines
drivers/net/ethernet/ibm/ibmvnic.c +1 -0
@@ -1761,8 +1761,9 @@ static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
      union ibmvnic_crq crq;
      int len = 0;
      int rc;
+    unsigned char *buff = adapter->vpd->buff;

Should be reverse x-mas tree (longest to shortest).

Quoted 10 lines
drivers/net/ethernet/ibm/ibmvnic.c +1 -1
-    if (adapter->vpd->buff)
+    if (buff)
          len = adapter->vpd->len;
 
      mutex_lock(&adapter->fw_lock);
@@ -1788,17 +1789,17 @@ static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
      if (!adapter->vpd->len)
          return -ENODATA;
 
Quoted 14 lines

drivers/net/ethernet/ibm/ibmvnic.c static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter) · line 1796

Quoted 10 lines
drivers/net/ethernet/ibm/ibmvnic.c +4 -5
-    if (!adapter->vpd->buff)
-        adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
+    if (!buff)
+        buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
      else if (adapter->vpd->len != len)
-        adapter->vpd->buff =
-            krealloc(adapter->vpd->buff,
-                 adapter->vpd->len, GFP_KERNEL);
+        buff = krealloc(buff,
+                adapter->vpd->len, GFP_KERNEL);

Dead branch? The only caller, init_resources(), kzalloc()s a fresh vpd right before, and resets run release_vpd_data() first, so vpd->buff is always NULL here and kzalloc() above always wins. The leak can't trigger,

Cool! No leak = no problem = nothing to do here