Skip to contentSkip to conversation

pinctrl: use non-devm kmalloc versions for free functions

PATCH In review Andre Przywara 6 participants 15 messages May 3, 2017 – May 22, 2017 Pin control

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pinctrl: use non-devm kmalloc versions for free functions

When a pinctrl driver gets interrupted during its probe process (returning -EPROBE_DEFER), the devres system cleans up all allocated resources. During this process it calls pinmux_generic_free_functions() and pinctrl_generic_free_groups(), which in turn use managed kmalloc calls for temporarily allocating some memory. Now those calls seem to get added to the devres list, but are apparently not covered by the cleanup process, because this is actually just running and iterating the existing list. This leads to those mallocs being left with the device, which the devres manager complains about when the driver eventually gets probed again:

[    0.825239] ------------[ cut here ]------------
[    0.825256] WARNING: CPU: 1 PID: 89 at drivers/base/dd.c:349 driver_probe_device+0x2ac/0x2e8
[    0.825258] Modules linked in:
[    0.825262]
[    0.825270] CPU: 1 PID: 89 Comm: kworker/1:1 Not tainted 4.11.0 #307
[    0.825272] Hardware name: Pine64+ (DT)
[    0.825283] Workqueue: events deferred_probe_work_func
[    0.825288] task: ffff80007c19c100 task.stack: ffff80007c16c000
[    0.825292] PC is at driver_probe_device+0x2ac/0x2e8
[    0.825296] LR is at driver_probe_device+0x108/0x2e8
[    0.825300] pc : [<ffff000008559234>] lr : [<ffff000008559090>] pstate: 20000045

....
This warning is triggered because the devres list is not empty. In this case the allocations were using 0 bytes, so no real leaks, but still this ugly warning.
Looking more closely at these *cleanup* functions, devm_kzalloc() is actually not needed, because the memory is just allocated temporarily and can be freed just before returning from this function.
So fix this issue by using the bog standard kcalloc() call instead of devm_kzalloc() and kfree()ing the memory at the end.

This fixes above warnings on boot, which can be observed on *some* builds for the Pine64, where the pinctrl driver gets loaded early, but it missing resources, so gets deferred and is loaded again (successfully) later. kernelci caught this as well [1].

[1] https://storage.kernelci.org/net-next/master/v4.11-rc8-2122-gc08bac03d289/arm64/defconfig/lab-baylibre-seattle/boot-sun50i-a64-pine64-plus.html
---
Hi,

not sure this is the right fix, I am open to suggestions.

Cheers,
Andre.

 drivers/pinctrl/core.c   | 5 +++--
 drivers/pinctrl/pinmux.c | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)
drivers/pinctrl/core.c +3 -2
index 32822b0d..5198415 100644
@@ -690,8 +690,8 @@ static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
 	void **slot;
 	int i = 0;
 
-	indices = devm_kzalloc(pctldev->dev, sizeof(*indices) *
-			       pctldev->num_groups, GFP_KERNEL);
+	indices = kcalloc(pctldev->num_groups, sizeof(*indices),
+			  GFP_KERNEL);
 	if (!indices)
 		return;
 
@@ -704,6 +704,7 @@ static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
 		radix_tree_delete(&pctldev->pin_group_tree, indices[i]);
 		devm_kfree(pctldev->dev, group);
 	}
+	kfree(indices);
 
 	pctldev->num_groups = 0;
 }
drivers/pinctrl/pinmux.c +3 -2
index 29ad315..6a70339 100644
@@ -836,8 +836,8 @@ void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
 	void **slot;
 	int i = 0;
 
-	indices = devm_kzalloc(pctldev->dev, sizeof(*indices) *
-			       pctldev->num_functions, GFP_KERNEL);
+	indices = kcalloc(pctldev->num_functions, sizeof(*indices),
+			  GFP_KERNEL);
 	if (!indices)
 		return;
 
@@ -850,6 +850,7 @@ void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
 		radix_tree_delete(&pctldev->pin_function_tree, indices[i]);
 		devm_kfree(pctldev->dev, function);
 	}
+	kfree(indices);
 
 	pctldev->num_functions = 0;
 }
Signature · 1 line
2.8.2
#1 lore raw

Hi Andre,

Quoted 67 lines · Andre Przywara

When a pinctrl driver gets interrupted during its probe process (returning -EPROBE_DEFER), the devres system cleans up all allocated resources. During this process it calls pinmux_generic_free_functions() and pinctrl_generic_free_groups(), which in turn use managed kmalloc calls for temporarily allocating some memory. Now those calls seem to get added to the devres list, but are apparently not covered by the cleanup process, because this is actually just running and iterating the existing list. This leads to those mallocs being left with the device, which the devres manager complains about when the driver eventually gets probed again:

[    0.825239] ------------[ cut here ]------------
[    0.825256] WARNING: CPU: 1 PID: 89 at drivers/base/dd.c:349 driver_probe_device+0x2ac/0x2e8
[    0.825258] Modules linked in:
[    0.825262]
[    0.825270] CPU: 1 PID: 89 Comm: kworker/1:1 Not tainted 4.11.0 #307
[    0.825272] Hardware name: Pine64+ (DT)
[    0.825283] Workqueue: events deferred_probe_work_func
[    0.825288] task: ffff80007c19c100 task.stack: ffff80007c16c000
[    0.825292] PC is at driver_probe_device+0x2ac/0x2e8
[    0.825296] LR is at driver_probe_device+0x108/0x2e8
[    0.825300] pc : [<ffff000008559234>] lr : [<ffff000008559090>] pstate: 20000045

....
This warning is triggered because the devres list is not empty. In this case the allocations were using 0 bytes, so no real leaks, but still this ugly warning.
Looking more closely at these *cleanup* functions, devm_kzalloc() is actually not needed, because the memory is just allocated temporarily and can be freed just before returning from this function.
So fix this issue by using the bog standard kcalloc() call instead of devm_kzalloc() and kfree()ing the memory at the end.

This fixes above warnings on boot, which can be observed on *some* builds for the Pine64, where the pinctrl driver gets loaded early, but it missing resources, so gets deferred and is loaded again (successfully) later. kernelci caught this as well [1].

[1] https://storage.kernelci.org/net-next/master/v4.11-rc8-2122-gc08bac03d289/arm64/defconfig/lab-baylibre-seattle/boot-sun50i-a64-pine64-plus.html
---
Hi,

not sure this is the right fix, I am open to suggestions.

Cheers,
Andre.

 drivers/pinctrl/core.c   | 5 +++--
 drivers/pinctrl/pinmux.c | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)
drivers/pinctrl/core.c +2 -2
index 32822b0d..5198415 100644
@@ -690,8 +690,8 @@ static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
 	void **slot;
 	int i = 0;
 
-	indices = devm_kzalloc(pctldev->dev, sizeof(*indices) *
-			       pctldev->num_groups, GFP_KERNEL);
+	indices = kcalloc(pctldev->num_groups, sizeof(*indices),
+			  GFP_KERNEL);
 	if (!indices)
 		return;
 

drivers/pinctrl/core.c static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev) · line 707

Quoted 4 lines
drivers/pinctrl/core.c +1 -0
@@ -704,6 +704,7 @@ static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
 		radix_tree_delete(&pctldev->pin_group_tree, indices[i]);
 		devm_kfree(pctldev->dev, group);
 	}
+	kfree(indices);

We use devm_kfree for other allocations done here, maybe we can just have the same thing here? We would be consistant, and we would still keep the resource tracking.

Maxime

Signature · 10 lines
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20170504/1e19f6b1/attachment.sig>
#2 lore raw

Hello,

Quoted 9 lines · Maxime Ripard

drivers/pinctrl/core.c static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev) · line 707

Quoted 5 lines
drivers/pinctrl/core.c +1 -0
@@ -704,6 +704,7 @@ static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
 		radix_tree_delete(&pctldev->pin_group_tree, indices[i]);
 		devm_kfree(pctldev->dev, group);
 	}
+	kfree(indices);

We use devm_kfree for other allocations done here, maybe we can just have the same thing here? We would be consistant, and we would still keep the resource tracking.

It doesn't make any sense to use the managed functions from the release functions and if you're always matching devm_kmalloc() with devm_kfree(), the only thing it'd do is confusing its readers.

And the function in question just looks weird to me - give up on freeing resources if memory allocation fails? And why call devm_kfree() on objects which are being released anyway? Or if the group can be released w/o the device being detached, why use devm allocations on the members at all?

As for removal, can't it just call radix_tree_iter_delete() while iterating? Why allocate memory to iterate and store all indices and to look them up again and delete them?

Thanks.

Signature · 1 line
tejun
#3 lore raw

On 04/05/17 17:00, Tejun Heo wrote:

Hi,

Quoted 13 lines

Hello,

Quoted 9 lines · Maxime Ripard

drivers/pinctrl/core.c static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev) · line 707

Quoted 5 lines
drivers/pinctrl/core.c +1 -0
@@ -704,6 +704,7 @@ static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
 		radix_tree_delete(&pctldev->pin_group_tree, indices[i]);
 		devm_kfree(pctldev->dev, group);
 	}
+	kfree(indices);

We use devm_kfree for other allocations done here, maybe we can just have the same thing here? We would be consistant, and we would still keep the resource tracking.

Quoted 3 lines

It doesn't make any sense to use the managed functions from the release functions and if you're always matching devm_kmalloc() with devm_kfree(), the only thing it'd do is confusing its readers.

I agree, the idea of this patch is to get rid of managed functions. Also my understanding is that devm_kfree needs to match some devm_kmalloc().

Quoted 9 lines

And the function in question just looks weird to me - give up on freeing resources if memory allocation fails? And why call devm_kfree() on objects which are being released anyway? Or if the group can be released w/o the device being detached, why use devm allocations on the members at all?

As for removal, can't it just call radix_tree_iter_delete() while iterating? Why allocate memory to iterate and store all indices and to look them up again and delete them?

Yeah, I was wondering about this as well. It looks like it just wants to clean up everything; the implementation of ida_destroy() seems to have the same intention and uses radix_tree_iter_delete().

Using this algorithm would avoid the memory allocation in the first place, so fix the issue as well and simplify the code.

So is there anything we miss here why we store all indices and iterate twice over the tree?

Cheers,
Andre.

#4 lore raw
In reply to Tejun Heo
Quoted 13 lines · Tejun Heo

Hello,

Quoted 9 lines · Maxime Ripard

drivers/pinctrl/core.c static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev) · line 707

Quoted 5 lines
drivers/pinctrl/core.c +1 -0
@@ -704,6 +704,7 @@ static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
 		radix_tree_delete(&pctldev->pin_group_tree, indices[i]);
 		devm_kfree(pctldev->dev, group);
 	}
+	kfree(indices);

We use devm_kfree for other allocations done here, maybe we can just have the same thing here? We would be consistant, and we would still keep the resource tracking.

Quoted 3 lines

It doesn't make any sense to use the managed functions from the release functions and if you're always matching devm_kmalloc() with devm_kfree(), the only thing it'd do is confusing its readers.

I wouldn't say that being able to recover and free whatever memory leak we might have not making sense, but ok. That was one of the options, let's discard it.

The other one is: refactor the rest of the allocations so that you don't have a mix of devm_kmalloc / devm_kfree and kmalloc / kfree for the same purpose in the same function.

Maxime

Signature · 10 lines
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20170505/cf11918c/attachment-0001.sig>
#5 lore raw

Hello, Maxime.

Quoted 11 lines · Maxime Ripard
Quoted 3 lines

It doesn't make any sense to use the managed functions from the release functions and if you're always matching devm_kmalloc() with devm_kfree(), the only thing it'd do is confusing its readers.

I wouldn't say that being able to recover and free whatever memory leak we might have not making sense, but ok. That was one of the options, let's discard it.

The other one is: refactor the rest of the allocations so that you don't have a mix of devm_kmalloc / devm_kfree and kmalloc / kfree for the same purpose in the same function.

So, I think the issues here are

1. Use of devm functions in a devres release function. This just doesn't make sense.

2. If a resource is always allocated and freed in the same function, there's no reason to use devres for it. I understand that there can be exceptions for this, e.g. consistency, but it doesn't seem to apply here.

3. Last but not least, allocating memory to destroy a radix tree. It should be able to do that without allocating any memory.

Thanks.

Signature · 1 line
tejun
#6 lore raw
In reply to Andre Przywara
Quoted 36 lines · Andre Przywara

When a pinctrl driver gets interrupted during its probe process (returning -EPROBE_DEFER), the devres system cleans up all allocated resources. During this process it calls pinmux_generic_free_functions() and pinctrl_generic_free_groups(), which in turn use managed kmalloc calls for temporarily allocating some memory. Now those calls seem to get added to the devres list, but are apparently not covered by the cleanup process, because this is actually just running and iterating the existing list. This leads to those mallocs being left with the device, which the devres manager complains about when the driver eventually gets probed again:

[    0.825239] ------------[ cut here ]------------
[    0.825256] WARNING: CPU: 1 PID: 89 at drivers/base/dd.c:349 driver_probe_device+0x2ac/0x2e8
[    0.825258] Modules linked in:
[    0.825262]
[    0.825270] CPU: 1 PID: 89 Comm: kworker/1:1 Not tainted 4.11.0 #307
[    0.825272] Hardware name: Pine64+ (DT)
[    0.825283] Workqueue: events deferred_probe_work_func
[    0.825288] task: ffff80007c19c100 task.stack: ffff80007c16c000
[    0.825292] PC is at driver_probe_device+0x2ac/0x2e8
[    0.825296] LR is at driver_probe_device+0x108/0x2e8
[    0.825300] pc : [<ffff000008559234>] lr : [<ffff000008559090>] pstate: 20000045

....
This warning is triggered because the devres list is not empty. In this case the allocations were using 0 bytes, so no real leaks, but still this ugly warning.
Looking more closely at these *cleanup* functions, devm_kzalloc() is actually not needed, because the memory is just allocated temporarily and can be freed just before returning from this function.
So fix this issue by using the bog standard kcalloc() call instead of devm_kzalloc() and kfree()ing the memory at the end.

This fixes above warnings on boot, which can be observed on *some* builds for the Pine64, where the pinctrl driver gets loaded early, but it missing resources, so gets deferred and is loaded again (successfully) later. kernelci caught this as well [1].

Quoted 7 lines

[1] https://storage.kernelci.org/net-next/master/v4.11-rc8-2122-gc08bac03d289/arm64/defconfig/lab-baylibre-seattle/boot-sun50i-a64-pine64-plus.html
---
Hi,

not sure this is the right fix, I am open to suggestions.

I have queued this as a tentative v4.12-rc1 fix, but a bit undertain.

Tejun, do I read your comments on the patch as an ACK?

Yours,
Linus Walleij

#7 lore raw

? 2017?5?11? GMT+08:00 ??10:01:54, Linus Walleij <[email protected]> ??:

Quoted 54 lines
Quoted 3 lines · Andre Przywara

When a pinctrl driver gets interrupted during its probe process (returning -EPROBE_DEFER), the devres system cleans up all allocated resources. During this process it calls

pinmux_generic_free_functions()

Quoted 4 lines

and pinctrl_generic_free_groups(), which in turn use managed kmalloc calls for temporarily allocating some memory. Now those calls seem to get added to the devres list, but are apparently not covered by the cleanup process, because this is actually just running and iterating

the

Quoted 1 line

existing list. This leads to those mallocs being left with the

device,

Quoted 1 line

which the devres manager complains about when the driver eventually

gets

Quoted 3 lines

probed again:

[    0.825239] ------------[ cut here ]------------
[    0.825256] WARNING: CPU: 1 PID: 89 at drivers/base/dd.c:349

driver_probe_device+0x2ac/0x2e8

Quoted 3 lines
[    0.825258] Modules linked in:
[    0.825262]
[    0.825270] CPU: 1 PID: 89 Comm: kworker/1:1 Not tainted 4.11.0

#307

Quoted 6 lines
[    0.825272] Hardware name: Pine64+ (DT)
[    0.825283] Workqueue: events deferred_probe_work_func
[    0.825288] task: ffff80007c19c100 task.stack: ffff80007c16c000
[    0.825292] PC is at driver_probe_device+0x2ac/0x2e8
[    0.825296] LR is at driver_probe_device+0x108/0x2e8
[    0.825300] pc : [<ffff000008559234>] lr : [<ffff000008559090>]

pstate: 20000045

Quoted 2 lines

....
This warning is triggered because the devres list is not empty. In

this

Quoted 1 line

case the allocations were using 0 bytes, so no real leaks, but still

this

Quoted 2 lines

ugly warning.
Looking more closely at these *cleanup* functions, devm_kzalloc() is

actually

Quoted 1 line

not needed, because the memory is just allocated temporarily and can

be

Quoted 5 lines

freed just before returning from this function.
So fix this issue by using the bog standard kcalloc() call instead of devm_kzalloc() and kfree()ing the memory at the end.

This fixes above warnings on boot, which can be observed on *some*

builds

Quoted 1 line

for the Pine64, where the pinctrl driver gets loaded early, but it

missing

Quoted 5 lines

resources, so gets deferred and is loaded again (successfully) later. kernelci caught this as well [1].

Quoted 8 lines
Quoted 1 line

[1]

https://storage.kernelci.org/net-next/master/v4.11-rc8-2122-gc08bac03d289/arm64/defconfig/lab-baylibre-seattle/boot-sun50i-a64-pine64-plus.html

Quoted 4 lines

---
Hi,

not sure this is the right fix, I am open to suggestions.

I have queued this as a tentative v4.12-rc1 fix, but a bit undertain.

I think 4.11 has also this issue.

Quoted 5 lines

Tejun, do I read your comments on the patch as an ACK?

Yours,
Linus Walleij

#8 lore raw
In reply to Linus Walleij

Hi Linus,

Quoted 44 lines · Linus Walleij
Quoted 42 lines · Andre Przywara

When a pinctrl driver gets interrupted during its probe process (returning -EPROBE_DEFER), the devres system cleans up all allocated resources. During this process it calls pinmux_generic_free_functions() and pinctrl_generic_free_groups(), which in turn use managed kmalloc calls for temporarily allocating some memory. Now those calls seem to get added to the devres list, but are apparently not covered by the cleanup process, because this is actually just running and iterating the existing list. This leads to those mallocs being left with the device, which the devres manager complains about when the driver eventually gets probed again:

[    0.825239] ------------[ cut here ]------------
[    0.825256] WARNING: CPU: 1 PID: 89 at drivers/base/dd.c:349 driver_probe_device+0x2ac/0x2e8
[    0.825258] Modules linked in:
[    0.825262]
[    0.825270] CPU: 1 PID: 89 Comm: kworker/1:1 Not tainted 4.11.0 #307
[    0.825272] Hardware name: Pine64+ (DT)
[    0.825283] Workqueue: events deferred_probe_work_func
[    0.825288] task: ffff80007c19c100 task.stack: ffff80007c16c000
[    0.825292] PC is at driver_probe_device+0x2ac/0x2e8
[    0.825296] LR is at driver_probe_device+0x108/0x2e8
[    0.825300] pc : [<ffff000008559234>] lr : [<ffff000008559090>] pstate: 20000045

....
This warning is triggered because the devres list is not empty. In this case the allocations were using 0 bytes, so no real leaks, but still this ugly warning.
Looking more closely at these *cleanup* functions, devm_kzalloc() is actually not needed, because the memory is just allocated temporarily and can be freed just before returning from this function.
So fix this issue by using the bog standard kcalloc() call instead of devm_kzalloc() and kfree()ing the memory at the end.

This fixes above warnings on boot, which can be observed on *some* builds for the Pine64, where the pinctrl driver gets loaded early, but it missing resources, so gets deferred and is loaded again (successfully) later. kernelci caught this as well [1].

[1] https://storage.kernelci.org/net-next/master/v4.11-rc8-2122-gc08bac03d289/arm64/defconfig/lab-baylibre-seattle/boot-sun50i-a64-pine64-plus.html
---
Hi,

Quoted 5 lines
Quoted 1 line

not sure this is the right fix, I am open to suggestions.

I have queued this as a tentative v4.12-rc1 fix, but a bit undertain.

Tejun, do I read your comments on the patch as an ACK?

Tejun and I were wondering why we need this "create an array with the indices" in the first place. If we can just call radix_tree_delete() directly from the radix_tree_for_each_slot() loop, we can have a much better fix (omitting the memory allocation at all)

Linus, can you shed some light if this array creation serves some purpose?

Cheers,
Andre.

#9 lore raw

Hello,

Quoted 6 lines · Andre Przywara
Quoted 1 line

Tejun, do I read your comments on the patch as an ACK?

Tejun and I were wondering why we need this "create an array with the indices" in the first place. If we can just call radix_tree_delete() directly from the radix_tree_for_each_slot() loop, we can have a much better fix (omitting the memory allocation at all)

Yeah, it doesn't make sense to allocate to destroy a radix tree. It'd be much better to cleanup the code so that it doesn't need allocation in the first place.

Thanks.

Signature · 1 line
tejun
#10 lore raw
In reply to Andre Przywara
Quoted 46 lines · Andre Przywara
Quoted 46 lines · Andre Przywara
Quoted 44 lines

When a pinctrl driver gets interrupted during its probe process (returning -EPROBE_DEFER), the devres system cleans up all allocated resources. During this process it calls pinmux_generic_free_functions() and pinctrl_generic_free_groups(), which in turn use managed kmalloc calls for temporarily allocating some memory. Now those calls seem to get added to the devres list, but are apparently not covered by the cleanup process, because this is actually just running and iterating the existing list. This leads to those mallocs being left with the device, which the devres manager complains about when the driver eventually gets probed again:

[    0.825239] ------------[ cut here ]------------
[    0.825256] WARNING: CPU: 1 PID: 89 at drivers/base/dd.c:349 driver_probe_device+0x2ac/0x2e8
[    0.825258] Modules linked in:
[    0.825262]
[    0.825270] CPU: 1 PID: 89 Comm: kworker/1:1 Not tainted 4.11.0 #307
[    0.825272] Hardware name: Pine64+ (DT)
[    0.825283] Workqueue: events deferred_probe_work_func
[    0.825288] task: ffff80007c19c100 task.stack: ffff80007c16c000
[    0.825292] PC is at driver_probe_device+0x2ac/0x2e8
[    0.825296] LR is at driver_probe_device+0x108/0x2e8
[    0.825300] pc : [<ffff000008559234>] lr : [<ffff000008559090>] pstate: 20000045

....
This warning is triggered because the devres list is not empty. In this case the allocations were using 0 bytes, so no real leaks, but still this ugly warning.
Looking more closely at these *cleanup* functions, devm_kzalloc() is actually not needed, because the memory is just allocated temporarily and can be freed just before returning from this function.
So fix this issue by using the bog standard kcalloc() call instead of devm_kzalloc() and kfree()ing the memory at the end.

This fixes above warnings on boot, which can be observed on *some* builds for the Pine64, where the pinctrl driver gets loaded early, but it missing resources, so gets deferred and is loaded again (successfully) later. kernelci caught this as well [1].

[1] https://storage.kernelci.org/net-next/master/v4.11-rc8-2122-gc08bac03d289/arm64/defconfig/lab-baylibre-seattle/boot-sun50i-a64-pine64-plus.html
---
Hi,

not sure this is the right fix, I am open to suggestions.

Quoted 8 lines
Quoted 3 lines

I have queued this as a tentative v4.12-rc1 fix, but a bit undertain.

Tejun, do I read your comments on the patch as an ACK?

Tejun and I were wondering why we need this "create an array with the indices" in the first place. If we can just call radix_tree_delete() directly from the radix_tree_for_each_slot() loop, we can have a much better fix (omitting the memory allocation at all)

OK I pulled the patch out again for now.

Quoted 1 line

Linus, can you shed some light if this array creation serves some purpose?

Tony [author of this function] can you look at this?

The code in pinctrl_generic_free_groups() does look a bit weird, allocating these indices just to remove the radix tree. Do you think we can clean it up?

Yours,
Linus Walleij

#11 lore raw
Quoted 58 lines · Linus Walleij
Quoted 46 lines · Andre Przywara
Quoted 46 lines · Andre Przywara
Quoted 44 lines

When a pinctrl driver gets interrupted during its probe process (returning -EPROBE_DEFER), the devres system cleans up all allocated resources. During this process it calls pinmux_generic_free_functions() and pinctrl_generic_free_groups(), which in turn use managed kmalloc calls for temporarily allocating some memory. Now those calls seem to get added to the devres list, but are apparently not covered by the cleanup process, because this is actually just running and iterating the existing list. This leads to those mallocs being left with the device, which the devres manager complains about when the driver eventually gets probed again:

[    0.825239] ------------[ cut here ]------------
[    0.825256] WARNING: CPU: 1 PID: 89 at drivers/base/dd.c:349 driver_probe_device+0x2ac/0x2e8
[    0.825258] Modules linked in:
[    0.825262]
[    0.825270] CPU: 1 PID: 89 Comm: kworker/1:1 Not tainted 4.11.0 #307
[    0.825272] Hardware name: Pine64+ (DT)
[    0.825283] Workqueue: events deferred_probe_work_func
[    0.825288] task: ffff80007c19c100 task.stack: ffff80007c16c000
[    0.825292] PC is at driver_probe_device+0x2ac/0x2e8
[    0.825296] LR is at driver_probe_device+0x108/0x2e8
[    0.825300] pc : [<ffff000008559234>] lr : [<ffff000008559090>] pstate: 20000045

....
This warning is triggered because the devres list is not empty. In this case the allocations were using 0 bytes, so no real leaks, but still this ugly warning.
Looking more closely at these *cleanup* functions, devm_kzalloc() is actually not needed, because the memory is just allocated temporarily and can be freed just before returning from this function.
So fix this issue by using the bog standard kcalloc() call instead of devm_kzalloc() and kfree()ing the memory at the end.

This fixes above warnings on boot, which can be observed on *some* builds for the Pine64, where the pinctrl driver gets loaded early, but it missing resources, so gets deferred and is loaded again (successfully) later. kernelci caught this as well [1].

[1] https://storage.kernelci.org/net-next/master/v4.11-rc8-2122-gc08bac03d289/arm64/defconfig/lab-baylibre-seattle/boot-sun50i-a64-pine64-plus.html
---
Hi,

not sure this is the right fix, I am open to suggestions.

Quoted 8 lines
Quoted 3 lines

I have queued this as a tentative v4.12-rc1 fix, but a bit undertain.

Tejun, do I read your comments on the patch as an ACK?

Tejun and I were wondering why we need this "create an array with the indices" in the first place. If we can just call radix_tree_delete() directly from the radix_tree_for_each_slot() loop, we can have a much better fix (omitting the memory allocation at all)

OK I pulled the patch out again for now.

Quoted 7 lines
Quoted 1 line

Linus, can you shed some light if this array creation serves some purpose?

Tony [author of this function] can you look at this?

The code in pinctrl_generic_free_groups() does look a bit weird, allocating these indices just to remove the radix tree. Do you think we can clean it up?

Yup indeed it seems totally pointless. Also the same code can be removed from pinmux_generic_free_functions().

It must be left over code from my initial attempts to to add generic pinctrl groups and functions when I still though we need to keep a static array around for the indices to keep pinctrl happy. Then I probably did some robotic compile fixes after updating things to use just the radix tree and added indices locally to both functions..

Regards,

Tony

#12 lore raw
Quoted 13 lines · Tony Lindgren
Quoted 8 lines · Linus Walleij
Quoted 1 line · Andre Przywara

Linus, can you shed some light if this array creation serves some purpose?

Tony [author of this function] can you look at this?

The code in pinctrl_generic_free_groups() does look a bit weird, allocating these indices just to remove the radix tree. Do you think we can clean it up?

Yup indeed it seems totally pointless. Also the same code can be removed from pinmux_generic_free_functions().

Quoted 6 lines

It must be left over code from my initial attempts to to add generic pinctrl groups and functions when I still though we need to keep a static array around for the indices to keep pinctrl happy. Then I probably did some robotic compile fixes after updating things to use just the radix tree and added indices locally to both functions..

Hmm no that, can't be, I think I figured it out.. See the patch below.

Regards,

Tony

8< ---------------------------

Quoted 1 line

From tony Mon Sep 17 00:00:00 2001

From: Tony Lindgren <[email protected]>
Date: Fri, 12 May 2017 08:47:57 -0700
Subject: [PATCH] pinctrl: core: Fix warning by removing bogus code

Andre Przywara <[email protected]> noticed that we can get the following warning with -EPROBE_DEFER:

"WARNING: CPU: 1 PID: 89 at drivers/base/dd.c:349
driver_probe_device+0x2ac/0x2e8"

Let's fix the issue by removing the indices as suggested by Tejun Heo <[email protected]>. All we have to do here is kill the radix tree.

I probably ended up with the indices after grepping for removal of all entries using radix_tree_for_each_slot() and the first match found was gmap_radix_tree_free(). Anyways, no need for indices here, and we can just do remove all the entries using radix_tree_for_each_slot() along how the item_kill_tree() test case does.

Fixes c7059c5ac70a ("pinctrl: core: Add generic pinctrl functions

for managing groups")

Fixes a76edc89b100 ("pinctrl: core: Add generic pinctrl functions

for managing groups")

---
 drivers/pinctrl/core.c   | 20 +++-----------------
 drivers/pinctrl/pinmux.c | 21 ++++-----------------
 2 files changed, 7 insertions(+), 34 deletions(-)
drivers/pinctrl/core.c +3 -17
@@ -680,30 +680,16 @@ EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
  * pinctrl_generic_free_groups() - removes all pin groups
  * @pctldev: pin controller device
  *
- * Note that the caller must take care of locking.
+ * Note that the caller must take care of locking. The pinctrl groups
+ * are allocated with devm_kzalloc() so no need to free them here.
  */
 static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
 {
 	struct radix_tree_iter iter;
-	struct group_desc *group;
-	unsigned long *indices;
 	void **slot;
-	int i = 0;
-
-	indices = devm_kzalloc(pctldev->dev, sizeof(*indices) *
-			       pctldev->num_groups, GFP_KERNEL);
-	if (!indices)
-		return;
 
 	radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
-		indices[i++] = iter.index;
-
-	for (i = 0; i < pctldev->num_groups; i++) {
-		group = radix_tree_lookup(&pctldev->pin_group_tree,
-					  indices[i]);
-		radix_tree_delete(&pctldev->pin_group_tree, indices[i]);
-		devm_kfree(pctldev->dev, group);
-	}
+		radix_tree_delete(&pctldev->pin_group_tree, iter.index);
 
 	pctldev->num_groups = 0;
 }
drivers/pinctrl/pinmux.c +4 -17
@@ -826,30 +826,17 @@ EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
  * pinmux_generic_free_functions() - removes all functions
  * @pctldev: pin controller device
  *
- * Note that the caller must take care of locking.
+ * Note that the caller must take care of locking. The pinctrl
+ * functions are allocated with devm_kzalloc() so no need to free
+ * them here.
  */
 void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
 {
 	struct radix_tree_iter iter;
-	struct function_desc *function;
-	unsigned long *indices;
 	void **slot;
-	int i = 0;
-
-	indices = devm_kzalloc(pctldev->dev, sizeof(*indices) *
-			       pctldev->num_functions, GFP_KERNEL);
-	if (!indices)
-		return;
 
 	radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
-		indices[i++] = iter.index;
-
-	for (i = 0; i < pctldev->num_functions; i++) {
-		function = radix_tree_lookup(&pctldev->pin_function_tree,
-					     indices[i]);
-		radix_tree_delete(&pctldev->pin_function_tree, indices[i]);
-		devm_kfree(pctldev->dev, function);
-	}
+		radix_tree_delete(&pctldev->pin_function_tree, iter.index);
 
 	pctldev->num_functions = 0;
 }
Signature · 1 line
2.13.0
author Reviewed-by Tested-by
#13 lore raw
Quoted 44 lines · Tony Lindgren
Quoted 13 lines · Tony Lindgren
Quoted 8 lines · Linus Walleij
Quoted 1 line · Andre Przywara

Linus, can you shed some light if this array creation serves some purpose?

Tony [author of this function] can you look at this?

The code in pinctrl_generic_free_groups() does look a bit weird, allocating these indices just to remove the radix tree. Do you think we can clean it up?

Yup indeed it seems totally pointless. Also the same code can be removed from pinmux_generic_free_functions().

Quoted 6 lines

It must be left over code from my initial attempts to to add generic pinctrl groups and functions when I still though we need to keep a static array around for the indices to keep pinctrl happy. Then I probably did some robotic compile fixes after updating things to use just the radix tree and added indices locally to both functions..

Hmm no that, can't be, I think I figured it out.. See the patch below.

Regards,

Tony

8< ---------------------------
From tony Mon Sep 17 00:00:00 2001
From: Tony Lindgren <[email protected]>
Date: Fri, 12 May 2017 08:47:57 -0700
Subject: [PATCH] pinctrl: core: Fix warning by removing bogus code

Andre Przywara <[email protected]> noticed that we can get the following warning with -EPROBE_DEFER:

"WARNING: CPU: 1 PID: 89 at drivers/base/dd.c:349
driver_probe_device+0x2ac/0x2e8"

Let's fix the issue by removing the indices as suggested by Tejun Heo <[email protected]>. All we have to do here is kill the radix tree.

Quoted 6 lines

I probably ended up with the indices after grepping for removal of all entries using radix_tree_for_each_slot() and the first match found was gmap_radix_tree_free(). Anyways, no need for indices here, and we can just do remove all the entries using radix_tree_for_each_slot() along how the item_kill_tree() test case does.

Yeah, I was hoping for exactly that!

Quoted 5 lines
Fixes c7059c5ac70a ("pinctrl: core: Add generic pinctrl functions

for managing groups")

Fixes a76edc89b100 ("pinctrl: core: Add generic pinctrl functions

for managing groups")

Thanks!
Andre

Quoted 82 lines
---
 drivers/pinctrl/core.c   | 20 +++-----------------
 drivers/pinctrl/pinmux.c | 21 ++++-----------------
 2 files changed, 7 insertions(+), 34 deletions(-)
drivers/pinctrl/core.c +3 -17
@@ -680,30 +680,16 @@ EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
  * pinctrl_generic_free_groups() - removes all pin groups
  * @pctldev: pin controller device
  *
- * Note that the caller must take care of locking.
+ * Note that the caller must take care of locking. The pinctrl groups
+ * are allocated with devm_kzalloc() so no need to free them here.
  */
 static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
 {
 	struct radix_tree_iter iter;
-	struct group_desc *group;
-	unsigned long *indices;
 	void **slot;
-	int i = 0;
-
-	indices = devm_kzalloc(pctldev->dev, sizeof(*indices) *
-			       pctldev->num_groups, GFP_KERNEL);
-	if (!indices)
-		return;
 
 	radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
-		indices[i++] = iter.index;
-
-	for (i = 0; i < pctldev->num_groups; i++) {
-		group = radix_tree_lookup(&pctldev->pin_group_tree,
-					  indices[i]);
-		radix_tree_delete(&pctldev->pin_group_tree, indices[i]);
-		devm_kfree(pctldev->dev, group);
-	}
+		radix_tree_delete(&pctldev->pin_group_tree, iter.index);
 
 	pctldev->num_groups = 0;
 }
drivers/pinctrl/pinmux.c +4 -17
@@ -826,30 +826,17 @@ EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
  * pinmux_generic_free_functions() - removes all functions
  * @pctldev: pin controller device
  *
- * Note that the caller must take care of locking.
+ * Note that the caller must take care of locking. The pinctrl
+ * functions are allocated with devm_kzalloc() so no need to free
+ * them here.
  */
 void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
 {
 	struct radix_tree_iter iter;
-	struct function_desc *function;
-	unsigned long *indices;
 	void **slot;
-	int i = 0;
-
-	indices = devm_kzalloc(pctldev->dev, sizeof(*indices) *
-			       pctldev->num_functions, GFP_KERNEL);
-	if (!indices)
-		return;
 
 	radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
-		indices[i++] = iter.index;
-
-	for (i = 0; i < pctldev->num_functions; i++) {
-		function = radix_tree_lookup(&pctldev->pin_function_tree,
-					     indices[i]);
-		radix_tree_delete(&pctldev->pin_function_tree, indices[i]);
-		devm_kfree(pctldev->dev, function);
-	}
+		radix_tree_delete(&pctldev->pin_function_tree, iter.index);
 
 	pctldev->num_functions = 0;
 }
#14 lore raw
In reply to Tony Lindgren
Quoted 22 lines · Tony Lindgren

From tony Mon Sep 17 00:00:00 2001
From: Tony Lindgren <[email protected]>
Date: Fri, 12 May 2017 08:47:57 -0700
Subject: [PATCH] pinctrl: core: Fix warning by removing bogus code

Andre Przywara <[email protected]> noticed that we can get the following warning with -EPROBE_DEFER:

"WARNING: CPU: 1 PID: 89 at drivers/base/dd.c:349
driver_probe_device+0x2ac/0x2e8"

Let's fix the issue by removing the indices as suggested by Tejun Heo <[email protected]>. All we have to do here is kill the radix tree.

I probably ended up with the indices after grepping for removal of all entries using radix_tree_for_each_slot() and the first match found was gmap_radix_tree_free(). Anyways, no need for indices here, and we can just do remove all the entries using radix_tree_for_each_slot() along how the item_kill_tree() test case does.

Quoted 6 lines
Fixes c7059c5ac70a ("pinctrl: core: Add generic pinctrl functions

for managing groups")

Fixes a76edc89b100 ("pinctrl: core: Add generic pinctrl functions

for managing groups")

Thanks! This nice inline patch applied for fixes with Andr?'s tags.

Yours,
Linus Walleij