of: overlay: unittest: fix overlay notify error, add overlay notifier tests, fix bug revealed by new unittests
cover of: overlay: unittest: fix overlay notify error, add overlay notifier tests, fix bug revealed by new unittests
From: Frank Rowand <[email protected]>
patch 1/3: fix bug in overlay notifier handling
patch 2/3: add overlay notifier unittests, expose another bug patch 3/3: fix bug exposed in patch 2, update unittests for fix
Frank Rowand (3):
of: overlay: add entry to of_overlay_action_name[]
of: overlay: unittest: add tests for overlay notifiers
of: overlay: do not free changeset when of_overlay_apply returns error
drivers/of/overlay.c | 56 ++++--- drivers/of/unittest-data/Makefile | 10 ++ drivers/of/unittest-data/overlay_16.dts | 15 ++ drivers/of/unittest-data/overlay_17.dts | 15 ++ drivers/of/unittest-data/overlay_18.dts | 15 ++ drivers/of/unittest-data/overlay_19.dts | 15 ++ drivers/of/unittest-data/overlay_20.dts | 15 ++ drivers/of/unittest.c | 204 ++++++++++++++++++++++++ include/linux/of.h | 13 ++ 9 files changed, 333 insertions(+), 25 deletions(-) create mode 100644 drivers/of/unittest-data/overlay_16.dts create mode 100644 drivers/of/unittest-data/overlay_17.dts create mode 100644 drivers/of/unittest-data/overlay_18.dts create mode 100644 drivers/of/unittest-data/overlay_19.dts create mode 100644 drivers/of/unittest-data/overlay_20.dts
Signature · 1 line
Frank Rowand <[email protected]>
PATCH 1/3 of: overlay: add entry to of_overlay_action_name[]
From: Frank Rowand <[email protected]>
The values of enum of_overlay_notify_action are used to index into array of_overlay_action_name. Add an entry to of_overlay_action_name for the value recently added to of_overlay_notify_action.
Array of_overlay_action_name[] is moved into include/linux/of.h adjacent to enum of_overlay_notify_action to make the connection between the two more obvious if either is modified in the future.
The only use of of_overlay_action_name is for error reporting in overlay_notify(). All callers of overlay_notify() report the same error, but with fewer details. Remove the redundant error reports in the callers.
--- drivers/of/overlay.c | 27 +++++---------------------- include/linux/of.h | 13 +++++++++++++ 2 files changed, 18 insertions(+), 22 deletions(-)
drivers/of/overlay.c +5 -22
@@ -152,13 +152,6 @@ int of_overlay_notifier_unregister(struct notifier_block *nb) }EXPORT_SYMBOL_GPL (of_overlay_notifier_unregister); -static char *of_overlay_action_name[] = { -"pre-apply" , -"post-apply" , -"pre-remove" , -"post-remove" , -}; -static int overlay_notify (struct overlay_changeset *ovcs,enum of_overlay_notify_action action) {
@@ -180,7 +173,7 @@ static int overlay_notify(struct overlay_changeset *ovcs,if (ret) { ret =notifier_to_errno (ret);pr_err ("overlay changeset %s notifier error %d, target: %pOF\n" , - of_overlay_action_name[action], ret, nd.target); +of_overlay_action_name (action), ret, nd.target);return ret; } }
@@ -927,10 +920,8 @@ static int of_overlay_apply(struct overlay_changeset *ovcs) goto out; ret =overlay_notify (ovcs,OF_OVERLAY_PRE_APPLY ); -if (ret) { -pr_err ("overlay changeset pre-apply notify error %d\n" , ret); +if (ret) goto out; - } ret =build_changeset (ovcs);if (ret)
@@ -953,12 +944,9 @@ static int of_overlay_apply(struct overlay_changeset *ovcs)/* notify failure is not fatal, continue */ ret_tmp =overlay_notify (ovcs,OF_OVERLAY_POST_APPLY ); -if (ret_tmp) { -pr_err ("overlay changeset post-apply notify error %d\n" , - ret_tmp); +if (ret_tmp)if (!ret) ret = ret_tmp; - } out:pr_debug ("%s() err=%d\n" , __func__, ret);
@@ -1194,10 +1182,8 @@ int of_overlay_remove(int *ovcs_id) } ret =overlay_notify (ovcs,OF_OVERLAY_PRE_REMOVE ); -if (ret) { -pr_err ("overlay changeset pre-remove notify error %d\n" , ret); +if (ret) goto err_unlock; - } ret_apply =0 ; ret =__of_changeset_revert_entries (&ovcs->cset, &ret_apply);
@@ -1220,12 +1206,9 @@ int of_overlay_remove(int *ovcs_id)* OF_OVERLAY_POST_REMOVE returns an error. */ ret_tmp =overlay_notify (ovcs,OF_OVERLAY_POST_REMOVE ); -if (ret_tmp) { -pr_err ("overlay changeset post-remove notify error %d\n" , - ret_tmp); +if (ret_tmp)if (!ret) ret = ret_tmp; - }free_overlay_changeset (ovcs);
include/linux/of.h +13 -0
@@ -1550,6 +1550,19 @@ enum of_overlay_notify_action { OF_OVERLAY_POST_REMOVE, }; +static inline char *of_overlay_action_name (enum of_overlay_notify_action action) +{ +static char *of_overlay_action_name[] = { +"init" , +"pre-apply" , +"post-apply" , +"pre-remove" , +"post-remove" , + }; + +return of_overlay_action_name[action]; +} +struct of_overlay_notify_data {struct device_node *overlay;struct device_node *target;
Signature · 1 line
Frank Rowand <[email protected]>
PATCH 2/3 of: overlay: unittest: add tests for overlay notifiers
In reply to [email protected]From: Frank Rowand <[email protected]>
Add tests for overlay apply and remove notifiers. Trigger errors for each of the notifier actions.
These tests will reveal a memory leak problem when a notifier returns an error for action OF_OVERLAY_POST_APPLY. The pr_err() message is:
OF: ERROR: memory leak, expected refcount 1 instead of 3, of_node_get()/of_node_put() unbalanced - destroy cset entry: attach overlay node /testcase-data/overlay-node/test-bus/test-unittest17
---
===
Output of the new overlay notifier unittests, as filtered by scripts/dtc/of_unittest_expect:
### dt-test ### pass of_unittest_overlay_notify():2825
ok OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus
### dt-test ### pass of_unittest_overlay_notify():2846
### dt-test ### pass of_unittest_overlay_notify():2851
ok OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus
OF: ERROR: memory leak, expected refcount 1 instead of 3, of_node_get()/of_node_put() unbalanced - destroy cset entry: attach overlay node /testcase-data/overlay-node/test-bus/test-unittest17 ### dt-test ### pass of_unittest_overlay_notify():2857
### dt-test ### pass of_unittest_overlay_notify():2862
### dt-test ### pass of_unittest_overlay_notify():2866
### dt-test ### pass of_unittest_overlay_notify():2869
ok OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus
### dt-test ### pass of_unittest_overlay_notify():2880
### dt-test ### pass of_unittest_overlay_notify():2888
### dt-test ### pass of_unittest_overlay_notify():2892
### dt-test ### pass of_unittest_overlay_notify():2895
ok OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus
### dt-test ### pass of_unittest_overlay_notify():2902
### dt-test ### pass of_unittest_overlay_notify():2909
### dt-test ### pass of_unittest_overlay_notify():2914
### dt-test ### pass of_unittest_overlay_notify():2926
Note that four new kernel error messages are triggered by the new tests, and of_unittest_expect labels them as expected errors with the string 'ok ' in the first three columns.
A fifth new kernel error message is a real bug, which will be fixed in patch 3/3:
OF: ERROR: memory leak, expected refcount 1 instead of 3, of_node_get()/of_node_put() unbalanced - destroy cset entry: attach overlay node /testcase-data/overlay-node/test-bus/test-unittest17
drivers/of/unittest-data/Makefile | 10 ++ drivers/of/unittest-data/overlay_16.dts | 15 ++ drivers/of/unittest-data/overlay_17.dts | 15 ++ drivers/of/unittest-data/overlay_18.dts | 15 ++ drivers/of/unittest-data/overlay_19.dts | 15 ++ drivers/of/unittest-data/overlay_20.dts | 15 ++ drivers/of/unittest.c | 198 ++++++++++++++++++++++++ 7 files changed, 283 insertions(+) create mode 100644 drivers/of/unittest-data/overlay_16.dts create mode 100644 drivers/of/unittest-data/overlay_17.dts create mode 100644 drivers/of/unittest-data/overlay_18.dts create mode 100644 drivers/of/unittest-data/overlay_19.dts create mode 100644 drivers/of/unittest-data/overlay_20.dts
drivers/of/unittest-data/Makefile +10 -0
@@ -17,6 +17,11 @@ obj-$(CONFIG_OF_OVERLAY) += overlay.dtb.o \ overlay_12.dtb.o \ overlay_13.dtb.o \ overlay_15.dtb.o \ + overlay_16.dtb.o \ + overlay_17.dtb.o \ + overlay_18.dtb.o \ + overlay_19.dtb.o \ + overlay_20.dtb.o \ overlay_bad_add_dup_node.dtb.o \ overlay_bad_add_dup_prop.dtb.o \ overlay_bad_phandle.dtb.o \
@@ -75,6 +80,11 @@ apply_static_overlay_1 := overlay_0.dtbo \ overlay_12.dtbo \ overlay_13.dtbo \ overlay_15.dtbo \ + overlay_16.dtbo \ + overlay_17.dtbo \ + overlay_18.dtbo \ + overlay_19.dtbo \ + overlay_20.dtbo \ overlay_gpio_01.dtbo \ overlay_gpio_02a.dtbo \ overlay_gpio_02b.dtbo \
drivers/of/unittest-data/overlay_16.dts new file +15 -0
@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +/dts-v1/; +/plugin/; + +/* overlay_16 - notify test */ + +&unittest_test_bus { + #address-cells = <1>; + #size-cells = <0>; + + test-unittest16 { + compatible = "unittest"; + reg = <16>; + }; +};
drivers/of/unittest-data/overlay_17.dts new file +15 -0
@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +/dts-v1/; +/plugin/; + +/* overlay_17 - notify test */ + +&unittest_test_bus { + #address-cells = <1>; + #size-cells = <0>; + + test-unittest17 { + compatible = "unittest"; + reg = <17>; + }; +};
drivers/of/unittest-data/overlay_18.dts new file +15 -0
@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +/dts-v1/; +/plugin/; + +/* overlay_18 - notify test */ + +&unittest_test_bus { + #address-cells = <1>; + #size-cells = <0>; + + test-unittest18 { + compatible = "unittest"; + reg = <18>; + }; +};
drivers/of/unittest-data/overlay_19.dts new file +15 -0
@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +/dts-v1/; +/plugin/; + +/* overlay_19 - notify test */ + +&unittest_test_bus { + #address-cells = <1>; + #size-cells = <0>; + + test-unittest19 { + compatible = "unittest"; + reg = <19>; + }; +};
drivers/of/unittest-data/overlay_20.dts new file +15 -0
@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +/dts-v1/; +/plugin/; + +/* overlay_20 - notify test */ + +&unittest_test_bus { + #address-cells = <1>; + #size-cells = <0>; + + test-unittest20 { + compatible = "unittest"; + reg = <20>; + }; +};
drivers/of/unittest.c +198 -0
222 lines not on this page · raw message
Signature · 1 line
Frank Rowand <[email protected]>
PATCH 3/3 of: overlay: do not free changeset when of_overlay_apply returns error
In reply to [email protected]From: Frank Rowand <[email protected]>
New unittests for overlay notifiers reveal a memory leak in of_overlay_apply() when a notifier returns an error for action OF_OVERLAY_POST_APPLY. The pr_err() message is:
OF: ERROR: memory leak, expected refcount 1 instead of 3, of_node_get()/of_node_put() unbalanced - destroy cset entry: attach overlay node /testcase-data/overlay-node/test-bus/test-unittest17
Change the error path to no longer call free_overlay_changeset(), and document that the caller of of_overlay_fdt_apply() may choose to remove the overlay.
Update the unittest that triggered the error to expect the changed return values and to call of_overlay_remove().
---
===
Output of the new overlay notifier unittests, as filtered by scripts/dtc/of_unittest_expect:
### dt-test ### pass of_unittest_overlay_notify():2825
ok OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus
### dt-test ### pass of_unittest_overlay_notify():2846
### dt-test ### pass of_unittest_overlay_notify():2851
ok OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus
### dt-test ### pass of_unittest_overlay_notify():2857
### dt-test ### pass of_unittest_overlay_notify():2862
### dt-test ### pass of_unittest_overlay_notify():2866
### dt-test ### pass of_unittest_overlay_notify():2872
### dt-test ### pass of_unittest_overlay_notify():2875
ok OF: overlay: overlay changeset pre-remove notifier error -18, target: /testcase-data/overlay-node/test-bus
### dt-test ### pass of_unittest_overlay_notify():2886
### dt-test ### pass of_unittest_overlay_notify():2894
### dt-test ### pass of_unittest_overlay_notify():2898
### dt-test ### pass of_unittest_overlay_notify():2901
ok OF: overlay: overlay changeset post-remove notifier error -19, target: /testcase-data/overlay-node/test-bus
### dt-test ### pass of_unittest_overlay_notify():2908
### dt-test ### pass of_unittest_overlay_notify():2915
### dt-test ### pass of_unittest_overlay_notify():2920
### dt-test ### pass of_unittest_overlay_notify():2932
drivers/of/overlay.c | 29 ++++++++++++++++++++++++++--- drivers/of/unittest.c | 10 ++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-)
drivers/of/overlay.c +26 -3
@@ -954,6 +954,25 @@ static int of_overlay_apply(struct overlay_changeset *ovcs)return ret; } +/* +* of_overlay_fdt_apply() - Create and apply an overlay changeset +* @overlay_fdt: pointer to overlay FDT +* @overlay_fdt_size: number of bytes in @overlay_fdt +* @ret_ovcs_id: pointer for returning created changeset id +* +* Creates and applies an overlay changeset. +* +* See of_overlay_apply() for important behavior information. +* +* Return: 0 on success, or a negative error number. *@ret_ovcs_id is set to +* the value of overlay changeset id, which can be passed to of_overlay_remove() +* to remove the overlay. +* +* On error return, the changeset may be partially applied. This is especially +* likely if an OF_OVERLAY_POST_APPLY notifier returns an error. In this case +* the caller should call of_overlay_remove() with the value in *@ret_ovcs_id. +*/ +int of_overlay_fdt_apply (const void *overlay_fdt,u32 overlay_fdt_size,int *ret_ovcs_id) {
@@ -1021,15 +1040,19 @@ int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size, ovcs->overlay_mem = overlay_mem; ret =of_overlay_apply (ovcs); -if (ret <0 ) - goto err_free_ovcs; +/* +* If of_overlay_apply() error, calling free_overlay_changeset() may +* result in a memory leak if the apply partly succeeded, so do NOT +* goto err_free_ovcs. Instead, the caller of of_overlay_fdt_apply() +* can call of_overlay_remove(); +*/ mutex_unlock (&of_mutex);of_overlay_mutex_unlock (); *ret_ovcs_id = ovcs->id; -return 0 ; +return ret; err_free_ovcs:free_overlay_changeset (ovcs);
drivers/of/unittest.c +8 -2
@@ -2848,7 +2848,7 @@ static void __init of_unittest_overlay_notify(void)EXPECT_END (KERN_INFO ,"OF: overlay: overlay changeset pre-apply notifier error -16, target: /testcase-data/overlay-node/test-bus" ); -unittest (!ovcs_id,"ovcs_id created for overlay_16\n" ); +unittest (ovcs_id,"ovcs_id not created for overlay_16\n" );/* --- overlay 17 --- */
@@ -2859,7 +2859,13 @@ static void __init of_unittest_overlay_notify(void)EXPECT_END (KERN_INFO ,"OF: overlay: overlay changeset post-apply notifier error -17, target: /testcase-data/overlay-node/test-bus" ); -unittest (!ovcs_id,"ovcs_id created for overlay_17\n" ); +unittest (ovcs_id,"ovcs_id not created for overlay_17\n" ); + +if (ovcs_id) { + ret =of_overlay_remove (&ovcs_id); +unittest (!ret, +"overlay_17 of_overlay_remove(), ret = %d\n" , ret); + }/* --- overlay 18 --- */
Signature · 1 line
Frank Rowand <[email protected]>
Rob Herring
maintainerRe: [PATCH 1/3] of: overlay: add entry to of_overlay_action_name[]
In reply to [email protected]Quoted 15 lines · On Sat, Apr 30, 2022 at 07:05:41PM -0500, [email protected] wrote:
From: Frank Rowand <[email protected]>
The values of enum
of_overlay_notify_actionare used to index into arrayof_overlay_action_name. Add an entry toof_overlay_action_namefor the value recently added toof_overlay_notify_action.Array of_overlay_action_name[] is moved into
include/linux/of.hadjacent to enumof_overlay_notify_actionto make the connection between the two more obvious if either is modified in the future.The only use of
of_overlay_action_nameis for error reporting inoverlay_notify(). All callers ofoverlay_notify()report the same error, but with fewer details. Remove the redundant error reports in the callers.
Quoted 7 lines
Fixes 067c098766c6 ("of: overlay: rework overlay apply and remove kfree()s")Reported-by Dan Carpenter <[email protected]>Signed-off-by Frank Rowand <[email protected]>--- drivers/of/overlay.c | 27 +++++---------------------- include/linux/of.h | 13 +++++++++++++ 2 files changed, 18 insertions(+), 22 deletions(-)
This isn't applying for me.
Rob
Re: [PATCH 1/3] of: overlay: add entry to of_overlay_action_name[]
Quoted 24 lines · Rob Herring
Quoted 15 lines · On Sat, Apr 30, 2022 at 07:05:41PM -0500, [email protected] wrote:
From: Frank Rowand <[email protected]>
The values of enum
of_overlay_notify_actionare used to index into arrayof_overlay_action_name. Add an entry toof_overlay_action_namefor the value recently added toof_overlay_notify_action.Array of_overlay_action_name[] is moved into
include/linux/of.hadjacent to enumof_overlay_notify_actionto make the connection between the two more obvious if either is modified in the future.The only use of
of_overlay_action_nameis for error reporting inoverlay_notify(). All callers ofoverlay_notify()report the same error, but with fewer details. Remove the redundant error reports in the callers.Quoted 7 lines
Fixes 067c098766c6 ("of: overlay: rework overlay apply and remove kfree()s")Reported-by Dan Carpenter <[email protected]>Signed-off-by Frank Rowand <[email protected]>--- drivers/of/overlay.c | 27 +++++---------------------- include/linux/of.h | 13 +++++++++++++ 2 files changed, 18 insertions(+), 22 deletions(-)
Quoted 1 line
This isn't applying for me.
Weird, patch can apply it, but 'git am' does not work. I see that when I try that on your dt/next branch.
The problem seems to be that I did not create the series on top of dt/next: 5f756a2eaa44 of: overlay: do not break notify on NOTIFY_{OK|STOP}.
I have rebased the series on top of that patch and am sending v2.
-Frank
Quoted 2 lines
Rob
Rob Herring
maintainerRe: [PATCH 1/3] of: overlay: add entry to of_overlay_action_name[]
Quoted 26 lines · Frank Rowand
Quoted 24 lines · Rob Herring
Quoted 22 lines · On Sat, Apr 30, 2022 at 07:05:41PM -0500, [email protected] wrote:
From: Frank Rowand <[email protected]>
The values of enum
of_overlay_notify_actionare used to index into arrayof_overlay_action_name. Add an entry toof_overlay_action_namefor the value recently added toof_overlay_notify_action.Array of_overlay_action_name[] is moved into
include/linux/of.hadjacent to enumof_overlay_notify_actionto make the connection between the two more obvious if either is modified in the future.The only use of
of_overlay_action_nameis for error reporting inoverlay_notify(). All callers ofoverlay_notify()report the same error, but with fewer details. Remove the redundant error reports in the callers.Fixes 067c098766c6 ("of: overlay: rework overlay apply and remove kfree()s")Reported-by Dan Carpenter <[email protected]>Signed-off-by Frank Rowand <[email protected]>--- drivers/of/overlay.c | 27 +++++---------------------- include/linux/of.h | 13 +++++++++++++ 2 files changed, 18 insertions(+), 22 deletions(-)
Quoted 7 lines
Quoted 1 line
This isn't applying for me.
Weird, patch can apply it, but 'git am' does not work. I see that when I try that on your dt/next branch.
The problem seems to be that I did not create the series on top of dt/next:
5f756a2eaa44of: overlay: do not break notify on NOTIFY_{OK|STOP}.
It should be more than just that. It was also not any base I have in my tree, so 'git am' wouldn't try a 3-way merge either.
Quoted 1 line
I have rebased the series on top of that patch and am sending v2.
Thanks.
Rob
Re: [PATCH 1/3] of: overlay: add entry to of_overlay_action_name[]
Quoted 29 lines · Rob Herring
Quoted 28 lines · Frank Rowand
Quoted 25 lines · Rob Herring
Quoted 15 lines · On Sat, Apr 30, 2022 at 07:05:41PM -0500, [email protected] wrote:
From: Frank Rowand <[email protected]>
The values of enum
of_overlay_notify_actionare used to index into arrayof_overlay_action_name. Add an entry toof_overlay_action_namefor the value recently added toof_overlay_notify_action.Array of_overlay_action_name[] is moved into
include/linux/of.hadjacent to enumof_overlay_notify_actionto make the connection between the two more obvious if either is modified in the future.The only use of
of_overlay_action_nameis for error reporting inoverlay_notify(). All callers ofoverlay_notify()report the same error, but with fewer details. Remove the redundant error reports in the callers.Quoted 7 lines
Fixes 067c098766c6 ("of: overlay: rework overlay apply and remove kfree()s")Reported-by Dan Carpenter <[email protected]>Signed-off-by Frank Rowand <[email protected]>--- drivers/of/overlay.c | 27 +++++---------------------- include/linux/of.h | 13 +++++++++++++ 2 files changed, 18 insertions(+), 22 deletions(-)This isn't applying for me.
Quoted 8 lines
Quoted 5 lines
Weird, patch can apply it, but 'git am' does not work. I see that when I try that on your dt/next branch.
The problem seems to be that I did not create the series on top of dt/next:
5f756a2eaa44of: overlay: do not break notify on NOTIFY_{OK|STOP}.It should be more than just that. It was also not any base I have in my tree, so 'git am' wouldn't try a 3-way merge either.
That was the only thing patch mentioned when it applied successfully, a little bit of fuzz around the few lines in that patch. I thought it a little weird that 'git am' didn't handle that, but it was easy enough to rebase instead of debugging git.
Quoted 4 lines
Quoted 2 lines
I have rebased the series on top of that patch and am sending v2.
Thanks.
I just now tried the v2 series emails on top of your dt/next and 'git am' is happy with it.
Quoted 2 lines
Rob