Skip to contentSkip to conversation

of: change overlay apply input data from unflattened

PATCH In review [email protected] 6 participants 48 messages Mar 4, 2018 – Apr 26, 2018

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

v7 cover of: change overlay apply input data from unflattened

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

These changes led to migrating some unittest overlay data into their own devicetree source files, and then converting most of them to use sugar syntax instead of hand coding fragments.

Changes from v6:
- patches 2-5 were previously patches 1-4
- patch 1/5 previously submitted separately as:
"x86: devicetree: fix config option around x86_flattree_get_config()"
https://lkml.org/lkml/2018/3/2/1148

Changes from v5:
- Add __init to of_unittest_apply_revert_overlay_check(), of_unittest_overlay_5(), and of_unittest_overlay_11() to fix compile warnings reported by Geert

Changes from v4:
(all in patch 1/4)
- move of_overlay_fdt_apply() prototype from of_private.h to of.h
- of_overlay_apply(): add kfree(fdt) if resolve phandles or initialize changeset fail
- of_overlay_fdt_apply(): add parameter size of overlay_fdt and use it for some additional validation of the overlay fdt
- update unittest.c with additional parameter to of_overlay_fdt_apply()

Changes from v3:
- patch 1/4: OF_OVERLAY: add select OF_FLATTREE

Changes from v2:
- improve error messages in patch 4/4, as suggested by Geert

Changes from v1:
- rebase on v4.16-rc1
- update documentation
- split out error message to a separate patch

Frank Rowand (5):
x86: devicetree: fix config option around x86_flattree_get_config()
of: change overlay apply input data from unflattened to FDT
of: Documentation: of_overlay_apply() replaced by
of_overlay_fdt_apply()
of: convert unittest overlay devicetree source to sugar syntax
of: improve reporting invalid overlay target path

 Documentation/devicetree/overlay-notes.txt       |   4 +-
 arch/x86/kernel/devicetree.c                     |   2 +-
 drivers/of/Kconfig                               |   1 +
 drivers/of/overlay.c                             | 134 ++++++++--
 drivers/of/resolver.c                            |   6 -
 drivers/of/unittest-data/Makefile                |  28 ++-
 drivers/of/unittest-data/overlay.dts             | 101 ++++----
 drivers/of/unittest-data/overlay_0.dts           |  14 ++
 drivers/of/unittest-data/overlay_1.dts           |  14 ++
 drivers/of/unittest-data/overlay_10.dts          |  27 ++
 drivers/of/unittest-data/overlay_11.dts          |  28 +++
 drivers/of/unittest-data/overlay_12.dts          |  14 ++
 drivers/of/unittest-data/overlay_13.dts          |  14 ++
 drivers/of/unittest-data/overlay_15.dts          |  30 +++
 drivers/of/unittest-data/overlay_2.dts           |   9 +
 drivers/of/unittest-data/overlay_3.dts           |   9 +
 drivers/of/unittest-data/overlay_4.dts           |  18 ++
 drivers/of/unittest-data/overlay_5.dts           |   9 +
 drivers/of/unittest-data/overlay_6.dts           |  10 +
 drivers/of/unittest-data/overlay_7.dts           |  10 +
 drivers/of/unittest-data/overlay_8.dts           |  10 +
 drivers/of/unittest-data/overlay_9.dts           |  10 +
 drivers/of/unittest-data/overlay_bad_phandle.dts |  23 +-
 drivers/of/unittest-data/overlay_bad_symbol.dts  |  25 +-
 drivers/of/unittest-data/tests-overlay.dtsi      | 217 +---------------
 drivers/of/unittest.c                            | 300 +++++++++++------------
 include/linux/of.h                               |   6 +-
 27 files changed, 586 insertions(+), 487 deletions(-)
 create mode 100644 drivers/of/unittest-data/overlay_0.dts
 create mode 100644 drivers/of/unittest-data/overlay_1.dts
 create mode 100644 drivers/of/unittest-data/overlay_10.dts
 create mode 100644 drivers/of/unittest-data/overlay_11.dts
 create mode 100644 drivers/of/unittest-data/overlay_12.dts
 create mode 100644 drivers/of/unittest-data/overlay_13.dts
 create mode 100644 drivers/of/unittest-data/overlay_15.dts
 create mode 100644 drivers/of/unittest-data/overlay_2.dts
 create mode 100644 drivers/of/unittest-data/overlay_3.dts
 create mode 100644 drivers/of/unittest-data/overlay_4.dts
 create mode 100644 drivers/of/unittest-data/overlay_5.dts
 create mode 100644 drivers/of/unittest-data/overlay_6.dts
 create mode 100644 drivers/of/unittest-data/overlay_7.dts
 create mode 100644 drivers/of/unittest-data/overlay_8.dts
 create mode 100644 drivers/of/unittest-data/overlay_9.dts
Signature · 1 line
Frank Rowand <[email protected]>
#1 lore raw

PATCH v7 1/5 x86: devicetree: fix config option around x86_flattree_get_config()

From: Frank Rowand <[email protected]>

x86_flattree_get_config() is incorrectly protected by ifdef CONFIG_OF_FLATTREE. It uses of_get_flat_dt_size(), which only exists if CONFIG_OF_EARLY_FLATTREE. This issue has not been exposed previously because OF_FLATTREE did not occur unless it was selected by OF_EARLY_FLATTREE. A devicetree overlay change is selecting OF_FLATTREE directly instead of indirectly enabling it by selecting OF_EARLY_FLATTREE.

This problem was exposed by a randconfig generated by the kbuild test robot, where Platform OLPC was enabled. OLPC selects OF_PROMTREE instead of OF_EARLY_FLATREE. The only other x86 platform that selects OF is X86_INTEL_CE, which does select OF_EARLY_FLATTREE.

---

x86 Maintainers, please ack this patch for Rob to accept

This patch previously submitted separately as:

"x86: devicetree: fix config option around x86_flattree_get_config()"
https://lkml.org/lkml/2018/3/2/1148

so this is a jump from version 1 to version 6.

No changes from version 1.

 arch/x86/kernel/devicetree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/devicetree.c +1 -1
index 25de5f6ca997..45416826f6ee 100644
@@ -259,7 +259,7 @@ static void __init dtb_apic_setup(void)
 	dtb_ioapic_setup();
 }
 
-#ifdef CONFIG_OF_FLATTREE
+#ifdef CONFIG_OF_EARLY_FLATTREE
 static void __init x86_flattree_get_config(void)
 {
 	u32 size, map_len;
Signature · 1 line
Frank Rowand <[email protected]>
#2 lore raw

Re: [PATCH v7 1/5] x86: devicetree: fix config option around x86_flattree_get_config()

Quoted 19 lines · On 03/03/18 16:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

x86_flattree_get_config() is incorrectly protected by ifdef CONFIG_OF_FLATTREE. It uses of_get_flat_dt_size(), which only exists if CONFIG_OF_EARLY_FLATTREE. This issue has not been exposed previously because OF_FLATTREE did not occur unless it was selected by OF_EARLY_FLATTREE. A devicetree overlay change is selecting OF_FLATTREE directly instead of indirectly enabling it by selecting OF_EARLY_FLATTREE.

This problem was exposed by a randconfig generated by the kbuild test robot, where Platform OLPC was enabled. OLPC selects OF_PROMTREE instead of OF_EARLY_FLATREE. The only other x86 platform that selects OF is X86_INTEL_CE, which does select OF_EARLY_FLATTREE.

---

Quoted 8 lines

x86 Maintainers, please ack this patch for Rob to accept

This patch previously submitted separately as:

"x86: devicetree: fix config option around x86_flattree_get_config()"
https://lkml.org/lkml/2018/3/2/1148

so this is a jump from version 1 to version 6.

                                      ^^^^^^^^^
                                      version 7
Quoted 21 lines

No changes from version 1.

 arch/x86/kernel/devicetree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/devicetree.c +1 -1
index 25de5f6ca997..45416826f6ee 100644
@@ -259,7 +259,7 @@ static void __init dtb_apic_setup(void)
 	dtb_ioapic_setup();
 }
 
-#ifdef CONFIG_OF_FLATTREE
+#ifdef CONFIG_OF_EARLY_FLATTREE
 static void __init x86_flattree_get_config(void)
 {
 	u32 size, map_len;
#3 lore raw

Re: [PATCH v7 1/5] x86: devicetree: fix config option around x86_flattree_get_config()

Quoted 17 lines · Frank Rowand
Quoted 16 lines · On 03/03/18 16:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

x86_flattree_get_config() is incorrectly protected by ifdef CONFIG_OF_FLATTREE. It uses of_get_flat_dt_size(), which only exists if CONFIG_OF_EARLY_FLATTREE. This issue has not been exposed previously because OF_FLATTREE did not occur unless it was selected by OF_EARLY_FLATTREE. A devicetree overlay change is selecting OF_FLATTREE directly instead of indirectly enabling it by selecting OF_EARLY_FLATTREE.

This problem was exposed by a randconfig generated by the kbuild test robot, where Platform OLPC was enabled. OLPC selects OF_PROMTREE instead of OF_EARLY_FLATREE. The only other x86 platform that selects OF is X86_INTEL_CE, which does select OF_EARLY_FLATTREE.

Quoted 4 lines
Quoted 4 lines

---

x86 Maintainers, please ack this patch for Rob to accept

#4 lore raw

PATCH v7 2/5 of: change overlay apply input data from unflattened to FDT

In reply to [email protected]

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

Update unittest.c to use of_overlay_fdt_apply() instead of of_overlay_apply().

Move overlay fragments from artificial locations in drivers/of/unittest-data/tests-overlay.dtsi into one devicetree source file per overlay. This led to changes in drivers/of/unitest-data/Makefile and drivers/of/unitest.c.

- Add overlay directives to the overlay devicetree source files so that dtc will compile them as true overlays into one FDT data chunk per overlay.

- Set CFLAGS for drivers/of/unittest-data/testcases.dts so that symbols will be generated for overlay resolution of overlays that are no longer artificially contained in testcases.dts

- Unflatten and apply each unittest overlay FDT using

    of_overlay_fdt_apply().

- Enable the of_resolve_phandles() check for whether the unflattened overlay is detached. This check was previously disabled because the overlays from tests-overlay.dtsi were not unflattened into detached trees.

- Other changes to unittest.c infrastructure to manage multiple test FDTs built into the kernel image (access by name instead of arbitrary number).

- of_unittest_overlay_high_level(): previously unused code to add properties from the overlay_base devicetree to the live tree was triggered by the restructuring of tests-overlay.dtsi and thus testcases.dts. This exposed two bugs: (1) the need to dup a property before adding it, and (2) property 'name' is auto-generated in the unflatten code and thus will be a duplicate in the __symbols__ node - do not treat this duplicate as an error.

---

There are checkpatch warnings. I have reviewed them and feel they can be ignored. They are "line over 80 characters" for either pre-existing long lines, or lines in devicetree source files.

Rob, I have added making unittest a loadable module to my todo list.

Changes from v5:
- Add __init to of_unittest_apply_revert_overlay_check(), of_unittest_overlay_5(), and of_unittest_overlay_11() to fix compile warnings reported by Geert

Changes from v4:
- move of_overlay_fdt_apply() prototype from of_private.h to of.h
- of_overlay_apply(): add kfree(fdt) if resolve phandles or initialize changeset fail
- of_overlay_fdt_apply(): add parameter size of overlay_fdt and use it for some additional validation of the overlay fdt
- update unittest.c with additional parameter to of_overlay_fdt_apply()

Changes from v3:
- OF_OVERLAY: add select OF_FLATTREE

Changes from v1:
- rebase on v4.16-rc1

 drivers/of/Kconfig                          |   1 +
 drivers/of/overlay.c                        | 112 ++++++++++-
 drivers/of/resolver.c                       |   6 -
 drivers/of/unittest-data/Makefile           |  28 ++-
 drivers/of/unittest-data/overlay_0.dts      |  14 ++
 drivers/of/unittest-data/overlay_1.dts      |  14 ++
 drivers/of/unittest-data/overlay_10.dts     |  34 ++++
 drivers/of/unittest-data/overlay_11.dts     |  34 ++++
 drivers/of/unittest-data/overlay_12.dts     |  14 ++
 drivers/of/unittest-data/overlay_13.dts     |  14 ++
 drivers/of/unittest-data/overlay_15.dts     |  35 ++++
 drivers/of/unittest-data/overlay_2.dts      |  14 ++
 drivers/of/unittest-data/overlay_3.dts      |  14 ++
 drivers/of/unittest-data/overlay_4.dts      |  23 +++
 drivers/of/unittest-data/overlay_5.dts      |  14 ++
 drivers/of/unittest-data/overlay_6.dts      |  15 ++
 drivers/of/unittest-data/overlay_7.dts      |  15 ++
 drivers/of/unittest-data/overlay_8.dts      |  15 ++
 drivers/of/unittest-data/overlay_9.dts      |  15 ++
 drivers/of/unittest-data/tests-overlay.dtsi | 213 --------------------
 drivers/of/unittest.c                       | 300 ++++++++++++++--------------
 include/linux/of.h                          |   6 +-
 22 files changed, 562 insertions(+), 388 deletions(-)
 create mode 100644 drivers/of/unittest-data/overlay_0.dts
 create mode 100644 drivers/of/unittest-data/overlay_1.dts
 create mode 100644 drivers/of/unittest-data/overlay_10.dts
 create mode 100644 drivers/of/unittest-data/overlay_11.dts
 create mode 100644 drivers/of/unittest-data/overlay_12.dts
 create mode 100644 drivers/of/unittest-data/overlay_13.dts
 create mode 100644 drivers/of/unittest-data/overlay_15.dts
 create mode 100644 drivers/of/unittest-data/overlay_2.dts
 create mode 100644 drivers/of/unittest-data/overlay_3.dts
 create mode 100644 drivers/of/unittest-data/overlay_4.dts
 create mode 100644 drivers/of/unittest-data/overlay_5.dts
 create mode 100644 drivers/of/unittest-data/overlay_6.dts
 create mode 100644 drivers/of/unittest-data/overlay_7.dts
 create mode 100644 drivers/of/unittest-data/overlay_8.dts
 create mode 100644 drivers/of/unittest-data/overlay_9.dts
drivers/of/Kconfig +1 -0
index 783e0870bd22..ad3fcad4d75b 100644
@@ -92,6 +92,7 @@ config OF_RESOLVE
 config OF_OVERLAY
 	bool "Device Tree overlays"
 	select OF_DYNAMIC
+	select OF_FLATTREE
 	select OF_RESOLVE
 	help
 	  Overlays are a method to dynamically modify part of the kernel's
drivers/of/overlay.c +103 -9

209 lines not on this page · raw message

drivers/of/resolver.c +0 -6
index 740d19bde601..b2f645187213 100644
@@ -269,17 +269,11 @@ int of_resolve_phandles(struct device_node *overlay)
 		goto out;
 	}
 
-#if 0
-	Temporarily disable check so that old style overlay unittests
-	do not fail when of_resolve_phandles() is moved into
-	of_overlay_apply().
-
 	if (!of_node_check_flag(overlay, OF_DETACHED)) {
 		pr_err("overlay not detached\n");
 		err = -EINVAL;
 		goto out;
 	}
-#endif
 
 	phandle_delta = live_tree_max_phandle() + 1;
 	adjust_overlay_phandles(overlay, phandle_delta);
drivers/of/unittest-data/Makefile +23 -5
index df697976740a..8fd0ea4b92b0 100644
@@ -1,8 +1,22 @@
 # SPDX-License-Identifier: GPL-2.0
-DTC_FLAGS_testcases := -Wno-interrupts_property
 obj-y += testcases.dtb.o
 
 obj-$(CONFIG_OF_OVERLAY) += overlay.dtb.o \
+			    overlay_0.dtb.o \
+			    overlay_1.dtb.o \
+			    overlay_2.dtb.o \
+			    overlay_3.dtb.o \
+			    overlay_4.dtb.o \
+			    overlay_5.dtb.o \
+			    overlay_6.dtb.o \
+			    overlay_7.dtb.o \
+			    overlay_8.dtb.o \
+			    overlay_9.dtb.o \
+			    overlay_10.dtb.o \
+			    overlay_11.dtb.o \
+			    overlay_12.dtb.o \
+			    overlay_13.dtb.o \
+			    overlay_15.dtb.o \
 			    overlay_bad_phandle.dtb.o \
 			    overlay_bad_symbol.dtb.o \
 			    overlay_base.dtb.o
@@ -10,10 +24,14 @@ obj-$(CONFIG_OF_OVERLAY) += overlay.dtb.o \
 targets += $(foreach suffix, dtb dtb.S, $(patsubst %.dtb.o,%.$(suffix),$(obj-y)))
 
 # enable creation of __symbols__ node
-DTC_FLAGS_overlay := -@
-DTC_FLAGS_overlay_bad_phandle := -@
-DTC_FLAGS_overlay_bad_symbol := -@
-DTC_FLAGS_overlay_base := -@
+DTC_FLAGS_overlay += -@
+DTC_FLAGS_overlay_bad_phandle += -@
+DTC_FLAGS_overlay_bad_symbol += -@
+DTC_FLAGS_overlay_base += -@
+DTC_FLAGS_testcases += -@
+
+# suppress warnings about intentional errors
+DTC_FLAGS_testcases += -Wno-interrupts_property
 
 .PRECIOUS: \
 	$(obj)/%.dtb.S \
drivers/of/unittest-data/overlay_0.dts new file +14 -0
new file mode 100644
index 000000000000..ac0f9e0fe65f
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_0 - enable using absolute target path */
+
+	fragment@0 {
+		target-path = "/testcase-data/overlay-node/test-bus/test-unittest0";
+		__overlay__ {
+			status = "okay";
+		};
+	};
+};
drivers/of/unittest-data/overlay_1.dts new file +14 -0
new file mode 100644
index 000000000000..e92a626e2948
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_1 - disable using absolute target path */
+
+	fragment@0 {
+		target-path = "/testcase-data/overlay-node/test-bus/test-unittest1";
+		__overlay__ {
+			status = "disabled";
+		};
+	};
+};
drivers/of/unittest-data/overlay_10.dts new file +34 -0
new file mode 100644
index 000000000000..445925a10cd3
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_10 */
+	/* overlays 8, 9, 10, 11 application and removal in bad sequence */
+
+	fragment@0 {
+		target-path = "/testcase-data/overlay-node/test-bus";
+		__overlay__ {
+
+			/* suppress DTC warning */
+			#address-cells = <1>;
+			#size-cells = <0>;
+
+			test-unittest10 {
+				compatible = "unittest";
+				status = "okay";
+				reg = <10>;
+
+				#address-cells = <1>;
+				#size-cells = <0>;
+
+				test-unittest101 {
+					compatible = "unittest";
+					status = "okay";
+					reg = <1>;
+				};
+
+			};
+		};
+	};
+};
drivers/of/unittest-data/overlay_11.dts new file +34 -0
new file mode 100644
index 000000000000..c1d14f34359e
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_11 */
+	/* overlays 8, 9, 10, 11 application and removal in bad sequence */
+
+	fragment@0 {
+		target-path = "/testcase-data/overlay-node/test-bus";
+		__overlay__ {
+
+			/* suppress DTC warning */
+			#address-cells = <1>;
+			#size-cells = <0>;
+
+			test-unittest11 {
+				compatible = "unittest";
+				status = "okay";
+				reg = <11>;
+
+				#address-cells = <1>;
+				#size-cells = <0>;
+
+				test-unittest111 {
+					compatible = "unittest";
+					status = "okay";
+					reg = <1>;
+				};
+
+			};
+		};
+	};
+};
drivers/of/unittest-data/overlay_12.dts new file +14 -0
new file mode 100644
index 000000000000..ca3441e2cbec
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_12 - enable using absolute target path (i2c) */
+
+	fragment@0 {
+		target-path = "/testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12";
+		__overlay__ {
+			status = "okay";
+		};
+	};
+};
drivers/of/unittest-data/overlay_13.dts new file +14 -0
new file mode 100644
index 000000000000..3c30dec63894
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_13 - disable using absolute target path (i2c) */
+
+	fragment@0 {
+		target-path = "/testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13";
+		__overlay__ {
+			status = "disabled";
+		};
+	};
+};
drivers/of/unittest-data/overlay_15.dts new file +35 -0
new file mode 100644
index 000000000000..44e44c62b739
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_15 - mux overlay */
+
+	fragment@0 {
+		target-path = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
+		__overlay__ {
+			#address-cells = <1>;
+			#size-cells = <0>;
+			test-unittest15 {
+				reg = <11>;
+				compatible = "unittest-i2c-mux";
+				status = "okay";
+
+				#address-cells = <1>;
+				#size-cells = <0>;
+
+				i2c@0 {
+					#address-cells = <1>;
+					#size-cells = <0>;
+					reg = <0>;
+
+					test-mux-dev {
+						reg = <32>;
+						compatible = "unittest-i2c-dev";
+						status = "okay";
+					};
+				};
+			};
+		};
+	};
+};
drivers/of/unittest-data/overlay_2.dts new file +14 -0
new file mode 100644
index 000000000000..cf1e4245b7ce
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_2 -  enable using label */
+
+	fragment@0 {
+		target = <&unittest2>;
+		__overlay__ {
+			status = "okay";
+		};
+	};
+};
drivers/of/unittest-data/overlay_3.dts new file +14 -0
new file mode 100644
index 000000000000..158dc44fc20a
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_3 - disable using label */
+
+	fragment@0 {
+		target = <&unittest3>;
+		__overlay__ {
+			status = "disabled";
+		};
+	};
+};
drivers/of/unittest-data/overlay_4.dts new file +23 -0
new file mode 100644
index 000000000000..b4a2e6c6b016
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_4 - test insertion of a full node */
+
+	fragment@0 {
+		target = <&unittestbus>;
+		__overlay__ {
+
+			/* suppress DTC warning */
+			#address-cells = <1>;
+			#size-cells = <0>;
+
+			test-unittest4 {
+				compatible = "unittest";
+				status = "okay";
+				reg = <4>;
+			};
+		};
+	};
+};
drivers/of/unittest-data/overlay_5.dts new file +14 -0
new file mode 100644
index 000000000000..02ad25c1f19c
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_5 - test overlay apply revert */
+
+	fragment@0 {
+		target-path = "/testcase-data/overlay-node/test-bus/test-unittest5";
+		__overlay__ {
+			status = "okay";
+		};
+	};
+};
drivers/of/unittest-data/overlay_6.dts new file +15 -0
new file mode 100644
index 000000000000..a14e965f5497
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_6 */
+	/* overlays 6, 7 application and removal in sequence */
+
+	fragment@0 {
+		target-path = "/testcase-data/overlay-node/test-bus/test-unittest6";
+		__overlay__ {
+			status = "okay";
+		};
+	};
+};
drivers/of/unittest-data/overlay_7.dts new file +15 -0
new file mode 100644
index 000000000000..4bd7e423209c
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_7 */
+	/* overlays 6, 7 application and removal in sequence */
+
+	fragment@0 {
+		target-path = "/testcase-data/overlay-node/test-bus/test-unittest7";
+		__overlay__ {
+			status = "okay";
+		};
+	};
+};
drivers/of/unittest-data/overlay_8.dts new file +15 -0
new file mode 100644
index 000000000000..5b21c53945a9
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_8 */
+	/* overlays 8, 9, 10, 11 application and removal in bad sequence */
+
+	fragment@0 {
+		target-path = "/testcase-data/overlay-node/test-bus/test-unittest8";
+		__overlay__ {
+			status = "okay";
+		};
+	};
+};
drivers/of/unittest-data/overlay_9.dts new file +15 -0
new file mode 100644
index 000000000000..20ff055a5349
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+/ {
+	/* overlay_9 */
+	/* overlays 8, 9, 10, 11 application and removal in bad sequence */
+
+	fragment@0 {
+		target-path = "/testcase-data/overlay-node/test-bus/test-unittest8";
+		__overlay__ {
+			property-foo = "bar";
+		};
+	};
+};
drivers/of/unittest-data/tests-overlay.dtsi +0 -213

218 lines not on this page · raw message

drivers/of/unittest.c +148 -152

599 lines not on this page · raw message

include/linux/of.h +3 -3
index da1ee95241c1..ebf22dd0860c 100644
@@ -1359,8 +1359,8 @@ struct of_overlay_notify_data {
 
 #ifdef CONFIG_OF_OVERLAY
 
-/* ID based overlays; the API for external users */
-int of_overlay_apply(struct device_node *tree, int *ovcs_id);
+int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
+			 int *ovcs_id);
 int of_overlay_remove(int *ovcs_id);
 int of_overlay_remove_all(void);
 
@@ -1369,7 +1369,7 @@ struct of_overlay_notify_data {
 
 #else
 
-static inline int of_overlay_apply(struct device_node *tree, int *ovcs_id)
+static inline int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id)
 {
 	return -ENOTSUPP;
 }
Signature · 1 line
Frank Rowand <[email protected]>
#5 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Thanks,
Jan

[1]
https://github.com/siemens/jailhouse/blob/11ef401a9064644bf7faf28e35c97f27ececd81d/driver/pci.c#L280

#6 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

No. I don't think that will work.

The of_overlay_apply() function is still there, but static. We can export it again if the need arises.

Another option is there is a notifier callback OF_OVERLAY_PRE_APPLY, but I'm not sure we want to make that be the normal interface to make modifications.

Rob

#7 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 37 lines · Rob Herring
Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 4 lines

No. I don't think that will work.

The of_overlay_apply() function is still there, but static. We can export it again if the need arises.

That would be the simplest solution from our perspective, but I'm not sure if that is in the original spirit of this change.

Quoted 4 lines

Another option is there is a notifier callback OF_OVERLAY_PRE_APPLY, but I'm not sure we want to make that be the normal interface to make modifications.

And would calling modification functions from that callback be legal at all?

Jan

#8 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 38 lines · Jan Kiszka
Quoted 37 lines · Rob Herring
Quoted 35 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 7 lines
Quoted 4 lines

No. I don't think that will work.

The of_overlay_apply() function is still there, but static. We can export it again if the need arises.

That would be the simplest solution from our perspective, but I'm not sure if that is in the original spirit of this change.

For short term out of tree usage, exporting of_overlay_apply() is ok.

Yes, for in-tree, exporting it again defeats the attempted process to solve the overlay issues to make them acceptable in main line.

Quoted 6 lines
Quoted 4 lines

Another option is there is a notifier callback OF_OVERLAY_PRE_APPLY, but I'm not sure we want to make that be the normal interface to make modifications.

And would calling modification functions from that callback be legal at all?

It might work in some specific cases, but the result is undefined.

Quoted 3 lines

Jan

#9 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 41 lines · Frank Rowand
Quoted 40 lines · Jan Kiszka
Quoted 39 lines · Rob Herring
Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

No. I don't think that will work.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

The of_overlay_apply() function is still there, but static. We can export it again if the need arises.

That would be the simplest solution from our perspective, but I'm not sure if that is in the original spirit of this change.

For short term out of tree usage, exporting of_overlay_apply() is ok.

Meaning the out of tree users can go export it themselves.

Quoted 2 lines

Yes, for in-tree, exporting it again defeats the attempted process to solve the overlay issues to make them acceptable in main line.

The purpose of providing a function to apply an overlay in one step is to handle the common case and avoid open coding the sequence everywhere. That doesn't mean everyone will fall into the common case.

Of course, it also doesn't mean we have to revert back to the previous way to handle users that need to tweak the unflattened tree before applying.

Rob

#10 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 50 lines · Rob Herring
Quoted 41 lines · Frank Rowand
Quoted 40 lines · Jan Kiszka
Quoted 39 lines · Rob Herring
Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

No. I don't think that will work.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

The of_overlay_apply() function is still there, but static. We can export it again if the need arises.

That would be the simplest solution from our perspective, but I'm not sure if that is in the original spirit of this change.

For short term out of tree usage, exporting of_overlay_apply() is ok.

Quoted 8 lines

Meaning the out of tree users can go export it themselves.

Quoted 2 lines

Yes, for in-tree, exporting it again defeats the attempted process to solve the overlay issues to make them acceptable in main line.

The purpose of providing a function to apply an overlay in one step is to handle the common case and avoid open coding the sequence everywhere. That doesn't mean everyone will fall into the common case.

That was one of the intents behind the change.

The other intent is that with lifetime of the overlay FDT and overlay expanded tree visible outside the devicetree core, we can not free the overlay FDT or the overlay expanded tree because we need to protect against use after free errors. The open coded examples of applying overlays commonly have issues with freeing the overlay FDT and/or the overlay expanded tree, which leads to the potential for use after free errors.

Quoted 6 lines

Of course, it also doesn't mean we have to revert back to the previous way to handle users that need to tweak the unflattened tree before applying.

Rob

#11 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

In reply to Rob Herring
Quoted 31 lines · Rob Herring
Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 7 lines
Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

No. I don't think that will work.

I don't think it will work either. The drivers won't be probed if the nodes are disabled.

Quoted 2 lines

The of_overlay_apply() function is still there, but static. We can export it again if the need arises.

I really don't want to export it again. sigh.

Quoted 3 lines

Another option is there is a notifier callback OF_OVERLAY_PRE_APPLY, but I'm not sure we want to make that be the normal interface to make modifications.

This callback is on my list of yet more ways that pointers into the (overlay) FDT are exposed. Which means another solution to the underlying problem (which was an FPGA problem) needs to be created. I'd prefer that use of this mechanism not be expanded in the mean time.

Even if that were not the case, using the OF_OVERLAY_PRE_APPLY notifier to modify the tree is problematic because the notifier(s) is called after the overlay FDT is in final form

-Frank

Quoted 4 lines

Rob
.

#12 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 38 lines · Frank Rowand
Quoted 37 lines · Rob Herring
Quoted 35 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines
Quoted 1 line

No. I don't think that will work.

I don't think it will work either. The drivers won't be probed if the nodes are disabled.

That would be the purpose of registering it disabled. The question is:
Would it be probed at the point when we enabled it while being applied? If not, strike this.

Jan

#13 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

In reply to Jan Kiszka

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Looking at the driver, I see one potential issue with that approach. The property "interrupt-map" is added directly to the changeset instead of being an existing property in the overlay. Is it possible to have this property in the overlay when needed?

I'll also reply to other comments in this thread.

-Frank

Quoted 8 lines

Thanks,
Jan

[1]
https://github.com/siemens/jailhouse/blob/11ef401a9064644bf7faf28e35c97f27ececd81d/driver/pci.c#L280
.

#14 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 5 lines

Looking at the driver, I see one potential issue with that approach. The property "interrupt-map" is added directly to the changeset instead of being an existing property in the overlay. Is it possible to have this property in the overlay when needed?

Well, the size of that property has a runtime dependency on the gic's #address-cells. If that is easy to account for depends a bit on the available FDT manipulation services. Or it would take multiple templates to handle the different cases (0, 1, or 2 IIRC).

Quoted 2 lines

I'll also reply to other comments in this thread.

TIA!

Jan

#15 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 40 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 35 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 8 lines
Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Quoted 10 lines
Quoted 5 lines

Looking at the driver, I see one potential issue with that approach. The property "interrupt-map" is added directly to the changeset instead of being an existing property in the overlay. Is it possible to have this property in the overlay when needed?

Well, the size of that property has a runtime dependency on the gic's #address-cells. If that is easy to account for depends a bit on the available FDT manipulation services. Or it would take multiple templates to handle the different cases (0, 1, or 2 IIRC).

If I understand create_vpci_of_overlay() correctly, it is assuming a fixed size of 4 cells. Line 314 is: for (n = 0, cell = 0; n < 4; n++) {

Off the top of my head, it is theoretically possible to create a property that can contain the largest possible size for the property, then shrink the size by inserting NOPs at the end of the property to shrink it.

Quoted 7 lines
Quoted 2 lines

I'll also reply to other comments in this thread.

TIA!

Jan

#16 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 47 lines · Frank Rowand
Quoted 46 lines · Jan Kiszka
Quoted 44 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Quoted 7 lines
Quoted 2 lines

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Ah, libfdt is available for kernel drivers as well. That looks like a viable path on first sight. I'll try that and come back in case it does not solve all issues.

Quoted 12 lines
Quoted 11 lines
Quoted 6 lines

Looking at the driver, I see one potential issue with that approach. The property "interrupt-map" is added directly to the changeset instead of being an existing property in the overlay. Is it possible to have this property in the overlay when needed?

Well, the size of that property has a runtime dependency on the gic's #address-cells. If that is easy to account for depends a bit on the available FDT manipulation services. Or it would take multiple templates to handle the different cases (0, 1, or 2 IIRC).

Quoted 6 lines

If I understand create_vpci_of_overlay() correctly, it is assuming a fixed size of 4 cells. Line 314 is: for (n = 0, cell = 0; n < 4; n++) {

Off the top of my head, it is theoretically possible to create a property that can contain the largest possible size for the property, then shrink the size by inserting NOPs at the end of the property to shrink it.

Well, I even find fdt_appendprop which sounds like we could keep adding that property on the fly.

How does memory management work with libfdt? Do I have to ensure that the fdt is already backed by an area large enough also for it modified form?

Thanks,
Jan

#17 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 79 lines · Jan Kiszka
Quoted 47 lines · Frank Rowand
Quoted 46 lines · Jan Kiszka
Quoted 44 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Quoted 7 lines
Quoted 2 lines

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Ah, libfdt is available for kernel drivers as well. That looks like a viable path on first sight. I'll try that and come back in case it does not solve all issues.

Quoted 12 lines
Quoted 11 lines
Quoted 6 lines

Looking at the driver, I see one potential issue with that approach. The property "interrupt-map" is added directly to the changeset instead of being an existing property in the overlay. Is it possible to have this property in the overlay when needed?

Well, the size of that property has a runtime dependency on the gic's #address-cells. If that is easy to account for depends a bit on the available FDT manipulation services. Or it would take multiple templates to handle the different cases (0, 1, or 2 IIRC).

Quoted 6 lines

If I understand create_vpci_of_overlay() correctly, it is assuming a fixed size of 4 cells. Line 314 is: for (n = 0, cell = 0; n < 4; n++) {

Off the top of my head, it is theoretically possible to create a property that can contain the largest possible size for the property, then shrink the size by inserting NOPs at the end of the property to shrink it.

Quoted 5 lines

Well, I even find fdt_appendprop which sounds like we could keep adding that property on the fly.

How does memory management work with libfdt? Do I have to ensure that the fdt is already backed by an area large enough also for it modified form?

I have not looked at those functions, so I don't know.

Quoted 4 lines

Thanks,
Jan

#18 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

In reply to Frank Rowand
Quoted 47 lines · Frank Rowand
Quoted 46 lines · Jan Kiszka
Quoted 44 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Quoted 6 lines
Quoted 2 lines

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

Rob

#19 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

Thanks,
Jan

[1] http://lkml.iu.edu/hypermail/linux/kernel/1606.3/00072.html

#20 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

Jan

#21 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

-Frank

#22 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 93 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 7 lines
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

My old code didn't have the problem because there was no one steeling pointers to my overlay, and I was able to safely release all the resources that I or the core on my behalf allocated. In fact, I recently even dropped the duplication the fdt prior to unflattening it because I got its lifecycle under control (and both kmemleak as well as kasan confirmed this). I still consider this intentional leak a regression of the new API.

Quoted 11 lines

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

Quoted 9 lines

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

I don't see yet how more duplicating of objects would help. Then we would not leak the fdt or the unflattened tree on overlay destruction but that duplicates, no?

Quoted 9 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Quoted 10 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a

But then again the convention has to be that those changeset pointers must not be kept - because the changeset is history after of_overlay_remove.

Quoted 4 lines

practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

We have drivers/fpga/of-fpga-region.c in-tree, and that does not seem to store any pointers to objects, rather consumes them in-place. And I would consider it fair to impose such a limitation on the notifier interface.

Quoted 11 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

As noted above, I don't see there is a technical solution to this issue but it's rather a matter of convention: no overlay notifier callback is allowed to keep references to the passed tree content (unless it reference-counts some tree nodes) beyond the execution of the callback. With that in place, we can safely drop the backing memory IMHO.

Jan

#23 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 155 lines · Jan Kiszka
Quoted 93 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
     [...]
     /*
      * TODO
      *
      * would like to: kfree(ovcs->overlay_tree);
      * but can not since drivers may have pointers into this data
      *
      * would like to: kfree(ovcs->fdt);
      * but can not since drivers may have pointers into this data
      */

     kfree(ovcs);
}
Quoted 7 lines
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

My old code didn't have the problem because there was no one steeling pointers to my overlay, and I was able to safely release all the resources that I or the core on my behalf allocated. In fact, I recently even dropped the duplication the fdt prior to unflattening it because I got its lifecycle under control (and both kmemleak as well as kasan confirmed this). I still consider this intentional leak a regression of the new API.

Quoted 11 lines

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

Quoted 9 lines

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

I don't see yet how more duplicating of objects would help. Then we would not leak the fdt or the unflattened tree on overlay destruction but that duplicates, no?

Quoted 9 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Quoted 10 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a

Quoted 7 lines

But then again the convention has to be that those changeset pointers must not be kept - because the changeset is history after of_overlay_remove.

Quoted 4 lines

practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

That's right.

Quoted 5 lines

We have drivers/fpga/of-fpga-region.c in-tree, and that does not seem to store any pointers to objects, rather consumes them in-place. And I would consider it fair to impose such a limitation on the notifier interface.

The FPGA code was written assuming that overlays could be removed.

Quoted 20 lines
Quoted 10 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

Quoted 2 lines

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

As noted above, I don't see there is a technical solution to this issue but it's rather a matter of convention: no overlay notifier callback is allowed to keep references to the passed tree content (unless it reference-counts some tree nodes) beyond the execution of the callback. With that in place, we can safely drop the backing memory IMHO.

Jan

#24 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 167 lines · Alan Tull
Quoted 155 lines · Jan Kiszka
Quoted 93 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
     [...]
     /*
      * TODO
      *
      * would like to: kfree(ovcs->overlay_tree);
      * but can not since drivers may have pointers into this data
      *
      * would like to: kfree(ovcs->fdt);
      * but can not since drivers may have pointers into this data
      */

     kfree(ovcs);
}
Quoted 7 lines
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

My old code didn't have the problem because there was no one steeling pointers to my overlay, and I was able to safely release all the resources that I or the core on my behalf allocated. In fact, I recently even dropped the duplication the fdt prior to unflattening it because I got its lifecycle under control (and both kmemleak as well as kasan confirmed this). I still consider this intentional leak a regression of the new API.

Quoted 11 lines

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

Quoted 9 lines

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

I don't see yet how more duplicating of objects would help. Then we would not leak the fdt or the unflattened tree on overlay destruction but that duplicates, no?

Quoted 9 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Quoted 10 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a

Quoted 7 lines

But then again the convention has to be that those changeset pointers must not be kept - because the changeset is history after of_overlay_remove.

Quoted 4 lines

practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

That's right.

Quoted 1 line
Quoted 6 lines
Quoted 4 lines

We have drivers/fpga/of-fpga-region.c in-tree, and that does not seem to store any pointers to objects, rather consumes them in-place. And I would consider it fair to impose such a limitation on the notifier interface.

The FPGA code was written assuming that overlays could be removed.

To be more specific, drivers/fpga/of-fpga-region.c currently saves a pointer to the overlay and uses it only during the pre-apply notification.

Quoted 21 lines
Quoted 21 lines
Quoted 11 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

Quoted 2 lines

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

As noted above, I don't see there is a technical solution to this issue but it's rather a matter of convention: no overlay notifier callback is allowed to keep references to the passed tree content (unless it reference-counts some tree nodes) beyond the execution of the callback. With that in place, we can safely drop the backing memory IMHO.

Jan

#25 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 8 lines · Alan Tull
Quoted 7 lines · Alan Tull
Quoted 5 lines · Jan Kiszka

We have drivers/fpga/of-fpga-region.c in-tree, and that does not seem to store any pointers to objects, rather consumes them in-place. And I would consider it fair to impose such a limitation on the notifier interface.

Quoted 5 lines
Quoted 1 line

The FPGA code was written assuming that overlays could be removed.

To be more specific, drivers/fpga/of-fpga-region.c currently saves a pointer to the overlay and uses it only during the pre-apply notification.

Even more exactly: You are saving a reference during pre-apply and freeing that one again on the corresponding post-remove. That is what I would have expected as normal usage of the notification API as well.

Jan

#26 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

In reply to Jan Kiszka
Quoted 102 lines · Jan Kiszka
Quoted 93 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 7 lines
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

Quoted 7 lines

My old code didn't have the problem because there was no one steeling pointers to my overlay, and I was able to safely release all the resources that I or the core on my behalf allocated. In fact, I recently even dropped the duplication the fdt prior to unflattening it because I got its lifecycle under control (and both kmemleak as well as kasan confirmed this). I still consider this intentional leak a regression of the new API.

The API has to work for any user, not just your clean code.

Quoted 20 lines
Quoted 10 lines

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

Quoted 9 lines

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 3 lines

I don't see yet how more duplicating of objects would help. Then we would not leak the fdt or the unflattened tree on overlay destruction but that duplicates, no?

Yes, we would leak the duplicates. That is exactly what the existing overlay remove code does. My long term goal is to remove that leakage. But that leakage can not be resolved until we can guarantee that there are no pointers held to those duplicates.

I don't like adding this additional copy - I would much prefer to change the overlay notify code as proposed below.

Quoted 20 lines
Quoted 9 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Quoted 10 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a

Quoted 2 lines

But then again the convention has to be that those changeset pointers must not be kept - because the changeset is history after of_overlay_remove.

I don't trust convention. The result is fragile code.

Quoted 6 lines
Quoted 4 lines

practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

We have drivers/fpga/of-fpga-region.c in-tree, and that does not seem to

Thanks for finding that. For some reason my 'git grep' did not find that. (I'll blame fat fingers or something...)

Quoted 3 lines

store any pointers to objects, rather consumes them in-place. And I would consider it fair to impose such a limitation on the notifier interface.

How do you enforce that limitation?

Quoted 20 lines
Quoted 10 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

As noted above, I don't see there is a technical solution to this issue but it's rather a matter of convention: no overlay notifier callback is allowed to keep references to the passed tree content (unless it reference-counts some tree nodes) beyond the execution of the callback. With that in place, we can safely drop the backing memory IMHO.

Jan
.

#27 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 111 lines · Frank Rowand
Quoted 102 lines · Jan Kiszka
Quoted 93 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 7 lines
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

Quoted 7 lines

My old code didn't have the problem because there was no one steeling pointers to my overlay, and I was able to safely release all the resources that I or the core on my behalf allocated. In fact, I recently even dropped the duplication the fdt prior to unflattening it because I got its lifecycle under control (and both kmemleak as well as kasan confirmed this). I still consider this intentional leak a regression of the new API.

Quoted 2 lines

The API has to work for any user, not just your clean code.

Please point us to code that does have a real problem. Is there any nice vendor tree, in addition to the known users? Or are we only speculating about how people might be (mis)using the API?

Quoted 25 lines
Quoted 21 lines
Quoted 11 lines

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

Quoted 9 lines

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 3 lines

I don't see yet how more duplicating of objects would help. Then we would not leak the fdt or the unflattened tree on overlay destruction but that duplicates, no?

Quoted 8 lines

Yes, we would leak the duplicates. That is exactly what the existing overlay remove code does. My long term goal is to remove that leakage. But that leakage can not be resolved until we can guarantee that there are no pointers held to those duplicates.

I don't like adding this additional copy - I would much prefer to change the overlay notify code as proposed below.

Replacing one leak with another is no solution. And if it's additionally enforcing an API change, I would call it counterproductive.

Quoted 21 lines
Quoted 21 lines
Quoted 20 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a

Quoted 5 lines
Quoted 2 lines

But then again the convention has to be that those changeset pointers must not be kept - because the changeset is history after of_overlay_remove.

I don't trust convention. The result is fragile code.

Look, we are all programming in C here. There is no implicit reference counting, no garbage collecting, not strong typing, you-name-it. That doesn't leave you with many sharper weapons than well documented conventions.

I'm rather concerned that you are over-designing an API that, due to its nature, cannot be made foolproof.

Quoted 12 lines
Quoted 7 lines
Quoted 5 lines

practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

We have drivers/fpga/of-fpga-region.c in-tree, and that does not seem to

Thanks for finding that. For some reason my 'git grep' did not find that. (I'll blame fat fingers or something...)

Quoted 6 lines
Quoted 3 lines

store any pointers to objects, rather consumes them in-place. And I would consider it fair to impose such a limitation on the notifier interface.

How do you enforce that limitation?

Primarily, code review. Of course, we can't help out-of-tree adventurers this way but, well, they prefer to travel alone anyway.

And then there are also tools like kasan that can be very helpful revealing object lifecycle issues early.

Jan

Quoted 22 lines
Quoted 21 lines
Quoted 11 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

As noted above, I don't see there is a technical solution to this issue but it's rather a matter of convention: no overlay notifier callback is allowed to keep references to the passed tree content (unless it reference-counts some tree nodes) beyond the execution of the callback. With that in place, we can safely drop the backing memory IMHO.

Jan
.

#28 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 112 lines · Jan Kiszka
Quoted 111 lines · Frank Rowand
Quoted 109 lines · Jan Kiszka
Quoted 93 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 7 lines
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

My old code didn't have the problem because there was no one steeling pointers to my overlay, and I was able to safely release all the resources that I or the core on my behalf allocated. In fact, I recently even dropped the duplication the fdt prior to unflattening it because I got its lifecycle under control (and both kmemleak as well as kasan confirmed this). I still consider this intentional leak a regression of the new API.

Quoted 6 lines
Quoted 2 lines

The API has to work for any user, not just your clean code.

Please point us to code that does have a real problem. Is there any nice vendor tree, in addition to the known users? Or are we only speculating about how people might be (mis)using the API?

No, I will not even attempt to find such code.

The underlying problem is that the devicetree access API returns pointers into the devicetree. And we have no way of knowing whether any driver or subsystem has a live pointer into the overlay data in the devicetree when we remove an overlay. The devicetree access API did not anticipate and account for overlays. As overlay related code has been added, this is an issue that has not yet been fixed.

Quoted 30 lines
Quoted 30 lines
Quoted 21 lines
Quoted 11 lines

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

Quoted 9 lines

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 3 lines

I don't see yet how more duplicating of objects would help. Then we would not leak the fdt or the unflattened tree on overlay destruction but that duplicates, no?

Yes, we would leak the duplicates. That is exactly what the existing overlay remove code does. My long term goal is to remove that leakage. But that leakage can not be resolved until we can guarantee that there are no pointers held to those duplicates.

Quoted 5 lines
Quoted 3 lines

I don't like adding this additional copy - I would much prefer to change the overlay notify code as proposed below.

Replacing one leak with another is no solution.

I agree. I do not see it as a viable solution.

Quoted 27 lines

And if it's additionally
enforcing an API change, I would call it counterproductive.

Quoted 24 lines
Quoted 23 lines
Quoted 10 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Quoted 10 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a

But then again the convention has to be that those changeset pointers must not be kept - because the changeset is history after of_overlay_remove.

Quoted 7 lines
Quoted 2 lines

I don't trust convention. The result is fragile code.

Look, we are all programming in C here. There is no implicit reference counting, no garbage collecting, not strong typing, you-name-it. That doesn't leave you with many sharper weapons than well documented conventions.

Nope. We can (hopefully) modify the devicetree access API so that it does not return pointers into the devicetree. For property values, this is "easy", though at a cost. Where the API currently returns a pointer to a property (or property value), copy that data into newly allocated memory and return a pointer to that newly allocated memory -- the caller is now responsible for the new memory and there is no stray pointer into the devicetree. One other place that pointers into the devicetree are exposed are the tree traversal APIs. In theory it should be possible to create an API that uses opaque "iterators" (I'm probably mis-using that word) so that location in the tree while traversing is not exposed in the form of a pointer into the devicetree.

-Frank

Quoted 54 lines

I'm rather concerned that you are over-designing an API that, due to its nature, cannot be made foolproof.

Quoted 12 lines
Quoted 7 lines
Quoted 5 lines

practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

We have drivers/fpga/of-fpga-region.c in-tree, and that does not seem to

Thanks for finding that. For some reason my 'git grep' did not find that. (I'll blame fat fingers or something...)

Quoted 6 lines
Quoted 3 lines

store any pointers to objects, rather consumes them in-place. And I would consider it fair to impose such a limitation on the notifier interface.

How do you enforce that limitation?

Primarily, code review. Of course, we can't help out-of-tree adventurers this way but, well, they prefer to travel alone anyway.

And then there are also tools like kasan that can be very helpful revealing object lifecycle issues early.

Jan

Quoted 22 lines
Quoted 21 lines
Quoted 11 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

As noted above, I don't see there is a technical solution to this issue but it's rather a matter of convention: no overlay notifier callback is allowed to keep references to the passed tree content (unless it reference-counts some tree nodes) beyond the execution of the callback. With that in place, we can safely drop the backing memory IMHO.

Jan
.

#29 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 204 lines · Frank Rowand
Quoted 112 lines · Jan Kiszka
Quoted 111 lines · Frank Rowand
Quoted 109 lines · Jan Kiszka
Quoted 93 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 20 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree
(FDT) into the overlay application code. To accomplish this,
of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree
code, which is thus responsible for freeing the duplicate FDT. The
caller of of_overlay_fdt_apply() remains responsible for freeing the
original FDT.

The unflattened devicetree now belongs to devicetree code, which is
thus responsible for freeing the unflattened devicetree.

These ownership changes prevent early freeing of the duplicated FDT
or the unflattened devicetree, which could result in use after free
errors.

of_overlay_fdt_apply() is a private function for the anticipated
overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 7 lines
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

My old code didn't have the problem because there was no one steeling pointers to my overlay, and I was able to safely release all the resources that I or the core on my behalf allocated. In fact, I recently even dropped the duplication the fdt prior to unflattening it because I got its lifecycle under control (and both kmemleak as well as kasan confirmed this). I still consider this intentional leak a regression of the new API.

Quoted 6 lines
Quoted 2 lines

The API has to work for any user, not just your clean code.

Please point us to code that does have a real problem. Is there any nice vendor tree, in addition to the known users? Or are we only speculating about how people might be (mis)using the API?

No, I will not even attempt to find such code.

The underlying problem is that the devicetree access API returns pointers into the devicetree. And we have no way of knowing whether any driver or subsystem has a live pointer into the overlay data in the devicetree when we remove an overlay. The devicetree access API did not anticipate and account for overlays. As overlay related code has been added, this is an issue that has not yet been fixed.

Quoted 30 lines
Quoted 30 lines
Quoted 21 lines
Quoted 11 lines

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

Quoted 9 lines

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 3 lines

I don't see yet how more duplicating of objects would help. Then we would not leak the fdt or the unflattened tree on overlay destruction but that duplicates, no?

Yes, we would leak the duplicates. That is exactly what the existing overlay remove code does. My long term goal is to remove that leakage. But that leakage can not be resolved until we can guarantee that there are no pointers held to those duplicates.

Quoted 5 lines
Quoted 3 lines

I don't like adding this additional copy - I would much prefer to change the overlay notify code as proposed below.

Replacing one leak with another is no solution.

I agree. I do not see it as a viable solution.

Quoted 27 lines

And if it's additionally
enforcing an API change, I would call it counterproductive.

Quoted 24 lines
Quoted 23 lines
Quoted 10 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Quoted 10 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a

But then again the convention has to be that those changeset pointers must not be kept - because the changeset is history after of_overlay_remove.

Quoted 7 lines
Quoted 2 lines

I don't trust convention. The result is fragile code.

Look, we are all programming in C here. There is no implicit reference counting, no garbage collecting, not strong typing, you-name-it. That doesn't leave you with many sharper weapons than well documented conventions.

Quoted 5 lines

Nope. We can (hopefully) modify the devicetree access API so that it does not return pointers into the devicetree. For property values, this is "easy", though at a cost. Where the API currently returns a pointer to a property (or property value), copy that data into newly allocated memory and return a pointer to that newly allocated

Or the data could be copied to memory designated by a pointer that the caller passed in. I was not intending this paragraph to be an actual thought out design for an API, it is just an attempt to describe what needs to change.

Quoted 65 lines

memory -- the caller is now responsible for the new memory and there is no stray pointer into the devicetree. One other place that pointers into the devicetree are exposed are the tree traversal APIs. In theory it should be possible to create an API that uses opaque "iterators" (I'm probably mis-using that word) so that location in the tree while traversing is not exposed in the form of a pointer into the devicetree.

-Frank

Quoted 54 lines

I'm rather concerned that you are over-designing an API that, due to its nature, cannot be made foolproof.

Quoted 12 lines
Quoted 7 lines
Quoted 5 lines

practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

We have drivers/fpga/of-fpga-region.c in-tree, and that does not seem to

Thanks for finding that. For some reason my 'git grep' did not find that. (I'll blame fat fingers or something...)

Quoted 6 lines
Quoted 3 lines

store any pointers to objects, rather consumes them in-place. And I would consider it fair to impose such a limitation on the notifier interface.

How do you enforce that limitation?

Primarily, code review. Of course, we can't help out-of-tree adventurers this way but, well, they prefer to travel alone anyway.

And then there are also tools like kasan that can be very helpful revealing object lifecycle issues early.

Jan

Quoted 22 lines
Quoted 21 lines
Quoted 11 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

As noted above, I don't see there is a technical solution to this issue but it's rather a matter of convention: no overlay notifier callback is allowed to keep references to the passed tree content (unless it reference-counts some tree nodes) beyond the execution of the callback. With that in place, we can safely drop the backing memory IMHO.

Jan
.

#30 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 123 lines · Frank Rowand
Quoted 122 lines · Frank Rowand
Quoted 112 lines · Jan Kiszka
Quoted 111 lines · Frank Rowand
Quoted 109 lines · Jan Kiszka
Quoted 93 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 35 lines · Jan Kiszka

Hi Frank,

On 2018-03-04 01:17, [email protected] wrote:
From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree
(FDT) into the overlay application code. To accomplish this,
of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree
code, which is thus responsible for freeing the duplicate FDT. The
caller of of_overlay_fdt_apply() remains responsible for freeing the
original FDT.

The unflattened devicetree now belongs to devicetree code, which is
thus responsible for freeing the unflattened devicetree.

These ownership changes prevent early freeing of the duplicated FDT
or the unflattened devicetree, which could result in use after free
errors.

of_overlay_fdt_apply() is a private function for the anticipated
overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the
(out-of-tree) Jailhouse loader driver in order to register a virtual
device during hypervisor activation with Linux. The DT overlay is
created from a a template but modified prior to application to account
for runtime-specific parameters. See [1] for the current implementation.

I'm now wondering how to model that scenario best with the new API.
Given that the loader lost ownership of the unflattened tree but the
modification API exist only for the that DT state, I'm not yet seeing a
clear solution. Should we apply the template in disabled form (status =
"disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 7 lines
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

My old code didn't have the problem because there was no one steeling pointers to my overlay, and I was able to safely release all the resources that I or the core on my behalf allocated. In fact, I recently even dropped the duplication the fdt prior to unflattening it because I got its lifecycle under control (and both kmemleak as well as kasan confirmed this). I still consider this intentional leak a regression of the new API.

Quoted 6 lines
Quoted 2 lines

The API has to work for any user, not just your clean code.

Please point us to code that does have a real problem. Is there any nice vendor tree, in addition to the known users? Or are we only speculating about how people might be (mis)using the API?

No, I will not even attempt to find such code.

Quoted 7 lines
Quoted 7 lines

The underlying problem is that the devicetree access API returns pointers into the devicetree. And we have no way of knowing whether any driver or subsystem has a live pointer into the overlay data in the devicetree when we remove an overlay. The devicetree access API did not anticipate and account for overlays. As overlay related code has been added, this is an issue that has not yet been fixed.

Yes, it returns pointer - which is not that uncommon for other subsystems as well. And for that reason, it would be valuable to have some concrete broken users as example at hand, also to validate solution designs against it. If things are so fundamentally broken, that should not be hard to find.

Quoted 81 lines
Quoted 75 lines
Quoted 31 lines
Quoted 31 lines
Quoted 22 lines
Quoted 12 lines

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

Quoted 9 lines

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 3 lines

I don't see yet how more duplicating of objects would help. Then we would not leak the fdt or the unflattened tree on overlay destruction but that duplicates, no?

Yes, we would leak the duplicates. That is exactly what the existing overlay remove code does. My long term goal is to remove that leakage. But that leakage can not be resolved until we can guarantee that there are no pointers held to those duplicates.

Quoted 5 lines
Quoted 3 lines

I don't like adding this additional copy - I would much prefer to change the overlay notify code as proposed below.

Replacing one leak with another is no solution.

I agree. I do not see it as a viable solution.

Quoted 27 lines

And if it's additionally
enforcing an API change, I would call it counterproductive.

Quoted 24 lines
Quoted 23 lines
Quoted 10 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Quoted 10 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a

But then again the convention has to be that those changeset pointers must not be kept - because the changeset is history after of_overlay_remove.

Quoted 7 lines
Quoted 2 lines

I don't trust convention. The result is fragile code.

Look, we are all programming in C here. There is no implicit reference counting, no garbage collecting, not strong typing, you-name-it. That doesn't leave you with many sharper weapons than well documented conventions.

Quoted 5 lines

Nope. We can (hopefully) modify the devicetree access API so that it does not return pointers into the devicetree. For property values, this is "easy", though at a cost. Where the API currently returns a pointer to a property (or property value), copy that data into newly allocated memory and return a pointer to that newly allocated

Quoted 14 lines

Or the data could be copied to memory designated by a pointer that the caller passed in. I was not intending this paragraph to be an actual thought out design for an API, it is just an attempt to describe what needs to change.

Quoted 8 lines

memory -- the caller is now responsible for the new memory and there is no stray pointer into the devicetree. One other place that pointers into the devicetree are exposed are the tree traversal APIs. In theory it should be possible to create an API that uses opaque "iterators" (I'm probably mis-using that word) so that location in the tree while traversing is not exposed in the form of a pointer into the devicetree.

-Frank

Property pointers and the associated content are currently valid as long as the caller holds a reference counter to the node they come from. So the problem is lacking reference counting by the callers? I would still consider it over-design to punish all users by a kmalloc plus local kfree for some users not properly doing the required of_node_get().

What other pointers are we talking about?

Jan

#31 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Hi Jan,

Quoted 1 line · Jan Kiszka

What other pointers are we talking about?

There's also the issue that some data has been allocated using kmalloc(), while others hasn't. The "other" pointers point to e.g. early bootmem or memblock.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#32 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 8 lines · Geert Uytterhoeven

Hi Jan,

Quoted 1 line · Jan Kiszka

What other pointers are we talking about?

There's also the issue that some data has been allocated using kmalloc(), while others hasn't. The "other" pointers point to e.g. early bootmem or memblock.

Should not be an issue for overlays or of_changesets, should it?

Yes, I've learned the hard way that it is a very bad idea to build changesets with properties that were not dynamically allocated. Maybe that is documented somewhere, how the ownership of those objects change after apply and how they may get handled later on. But now as I know, it's kind of consistent and rather easy to account for.

Jan

#33 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

In reply to Jan Kiszka
Quoted 132 lines · Jan Kiszka
Quoted 123 lines · Frank Rowand
Quoted 122 lines · Frank Rowand
Quoted 112 lines · Jan Kiszka
Quoted 111 lines · Frank Rowand
Quoted 109 lines · Jan Kiszka
Quoted 93 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 44 lines · Frank Rowand

Hi Jan,

On 04/04/18 15:35, Jan Kiszka wrote:
Hi Frank,

On 2018-03-04 01:17, [email protected] wrote:
From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree
(FDT) into the overlay application code. To accomplish this,
of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree
code, which is thus responsible for freeing the duplicate FDT. The
caller of of_overlay_fdt_apply() remains responsible for freeing the
original FDT.

The unflattened devicetree now belongs to devicetree code, which is
thus responsible for freeing the unflattened devicetree.

These ownership changes prevent early freeing of the duplicated FDT
or the unflattened devicetree, which could result in use after free
errors.

of_overlay_fdt_apply() is a private function for the anticipated
overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the
(out-of-tree) Jailhouse loader driver in order to register a virtual
device during hypervisor activation with Linux. The DT overlay is
created from a a template but modified prior to application to account
for runtime-specific parameters. See [1] for the current implementation.

I'm now wondering how to model that scenario best with the new API.
Given that the loader lost ownership of the unflattened tree but the
modification API exist only for the that DT state, I'm not yet seeing a
clear solution. Should we apply the template in disabled form (status =
"disabled"), modify it, and then activate it while it is already applied?

Thank you for the pointer to the driver - that makes it much easier to
understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the
expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 7 lines
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

My old code didn't have the problem because there was no one steeling pointers to my overlay, and I was able to safely release all the resources that I or the core on my behalf allocated. In fact, I recently even dropped the duplication the fdt prior to unflattening it because I got its lifecycle under control (and both kmemleak as well as kasan confirmed this). I still consider this intentional leak a regression of the new API.

Quoted 6 lines
Quoted 2 lines

The API has to work for any user, not just your clean code.

Please point us to code that does have a real problem. Is there any nice vendor tree, in addition to the known users? Or are we only speculating about how people might be (mis)using the API?

No, I will not even attempt to find such code.

Quoted 7 lines
Quoted 7 lines

The underlying problem is that the devicetree access API returns pointers into the devicetree. And we have no way of knowing whether any driver or subsystem has a live pointer into the overlay data in the devicetree when we remove an overlay. The devicetree access API did not anticipate and account for overlays. As overlay related code has been added, this is an issue that has not yet been fixed.

Quoted 5 lines

Yes, it returns pointer - which is not that uncommon for other subsystems as well. And for that reason, it would be valuable to have some concrete broken users as example at hand, also to validate solution designs against it. If things are so fundamentally broken, that should not be hard to find.

We essentially do not support generic overlays in the mainline kernel. Overlays are a feature that is under development. (Yes, there is a driver that directly uses overlays - if I had been a devicetree maintainer at the time that usage was added I would have objected). Acceptance of an overlay manager has been refused several times, as least partly because the underlying overlay features are not yet complete enough.

The fundamental breakage is not exposed if we do not remove overlays and thus do not free the underlying memory.

Quoted 94 lines
Quoted 94 lines
Quoted 75 lines
Quoted 31 lines
Quoted 31 lines
Quoted 22 lines
Quoted 12 lines

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

Quoted 9 lines

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 3 lines

I don't see yet how more duplicating of objects would help. Then we would not leak the fdt or the unflattened tree on overlay destruction but that duplicates, no?

Yes, we would leak the duplicates. That is exactly what the existing overlay remove code does. My long term goal is to remove that leakage. But that leakage can not be resolved until we can guarantee that there are no pointers held to those duplicates.

Quoted 5 lines
Quoted 3 lines

I don't like adding this additional copy - I would much prefer to change the overlay notify code as proposed below.

Replacing one leak with another is no solution.

I agree. I do not see it as a viable solution.

Quoted 27 lines

And if it's additionally
enforcing an API change, I would call it counterproductive.

Quoted 24 lines
Quoted 23 lines
Quoted 10 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Quoted 10 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a

But then again the convention has to be that those changeset pointers must not be kept - because the changeset is history after of_overlay_remove.

Quoted 7 lines
Quoted 2 lines

I don't trust convention. The result is fragile code.

Look, we are all programming in C here. There is no implicit reference counting, no garbage collecting, not strong typing, you-name-it. That doesn't leave you with many sharper weapons than well documented conventions.

Quoted 5 lines

Nope. We can (hopefully) modify the devicetree access API so that it does not return pointers into the devicetree. For property values, this is "easy", though at a cost. Where the API currently returns a pointer to a property (or property value), copy that data into newly allocated memory and return a pointer to that newly allocated

Or the data could be copied to memory designated by a pointer that the caller passed in. I was not intending this paragraph to be an actual thought out design for an API, it is just an attempt to describe what needs to change.

Quoted 7 lines

memory -- the caller is now responsible for the new memory and there is no stray pointer into the devicetree. One other place that pointers into the devicetree are exposed are the tree traversal APIs. In theory it should be possible to create an API that uses opaque "iterators" (I'm probably mis-using that word) so that location in the tree while traversing is not exposed in the form of a pointer into the devicetree.

Quoted 7 lines
Quoted 1 line
Quoted 1 line

-Frank

Property pointers and the associated content are currently valid as long as the caller holds a reference counter to the node they come from. So the problem is lacking reference counting by the callers? I would still consider it over-design to punish all users by a kmalloc plus local kfree for some users not properly doing the required of_node_get().

Properties do not have their own reference counter (as you note in the comment that a caller hold a reference counter to the node that contains the property). But there is no documentation or expectation that a caller of a function that returns a property pointer needs to hold the node reference counter. For example, of_find_property() and of_property_read_string().

We know that node reference counters are often incorrect.

Quoted 5 lines

What other pointers are we talking about?

Jan
.

#34 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

In reply to Frank Rowand

Hi Alan,

Quoted 120 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 8 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Question added below this paragraph.

Quoted 6 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of

Quoted 8 lines

the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

Can the fpga notifiers be changed to have the changeset as an input instead of having the overlay devicetree fragment and target as an input?

The changeset lists nodes and properties to be added, but does not expose any pointers to the overlay fdt or the overlay unflattened devicetree. This guarantees no leakage of pointers into the overlay fdt or the overlay unflattened devicetree. The changeset contains pointers to copies of data, but those copies are never freed (and thus they are yet another existing memory leak).

-Frank

Quoted 15 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

-Frank

#35 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 154 lines · Frank Rowand

Hi Alan,

Quoted 120 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 8 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Question added below this paragraph.

Quoted 6 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of

Quoted 8 lines

the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

Can the fpga notifiers be changed to have the changeset as an input instead of having the overlay devicetree fragment and target as an input?

Quoted 6 lines

The changeset lists nodes and properties to be added, but does not expose any pointers to the overlay fdt or the overlay unflattened devicetree. This guarantees no leakage of pointers into the overlay fdt or the overlay unflattened devicetree. The changeset contains pointers to copies of data, but those copies are never freed (and thus they are yet another existing memory leak).

Also they are freed, of course: When the last reference to the node they point to reaches 0 (e.g. triggered by of_changeset_destroy), that node goes away and takes down remaining dead properties. I've ran through this already. And I also made sure that my code is not triggering such kind of leaks as well.

I agree that object management in dynamic OF code is hairy and easy to get wrong, e.g. by passing in static objects that suddenly change ownership, and the new owner expects dynamically allocated objects... But that is not cured by leaking memory.

Jan

#36 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 162 lines · Jan Kiszka
Quoted 154 lines · Frank Rowand

Hi Alan,

Quoted 120 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 8 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Question added below this paragraph.

Quoted 6 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of

Quoted 8 lines

the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

Can the fpga notifiers be changed to have the changeset as an input instead of having the overlay devicetree fragment and target as an input?

Quoted 6 lines

The changeset lists nodes and properties to be added, but does not expose any pointers to the overlay fdt or the overlay unflattened devicetree. This guarantees no leakage of pointers into the overlay fdt or the overlay unflattened devicetree. The changeset contains pointers to copies of data, but those copies are never freed (and thus they are yet another existing memory leak).

Quoted 5 lines

Also they are freed, of course: When the last reference to the node they point to reaches 0 (e.g. triggered by of_changeset_destroy), that node goes away and takes down remaining dead properties. I've ran through this already. And I also made sure that my code is not triggering such kind of leaks as well.

mea culpa. I go around in circles while trying to remember all the overlay related issues. I needed to go back and read the code to refresh my memory. Thanks for the prod to re-read the code.

Yes, of_changeset_destroy() will lead to the kfree() of the node and it's properties _if_ the node reference count is correct. So what I said about a memory leak was incorrect in a perfect world (and my memory was wrong). However, this is not a perfect world and we know that the reference count on devicetree nodes is often incorrect due to bugs in common infrastructure and drivers. This issue will not be resolved until we pull all reference count manipulation into the devicetree core. The net result is that we should not expect overlay removal to correctly free all memory that was allocated when applying the overlay.

I _think_ (but did not spend the time to verify) that there is a small corner case memory leak even if the reference count on devicetree nodes is correct. If an overlay adds a property to an existing node then removing the overlay will not kfree() the property, and it will remain on the deadprops list. There are some places that properties are removed from deadprops, but I don't think they fully resolve the issue. Again, this is a corner case, and I am willing to document it as a limitation until it gets fixed.

Then returning to me going around in circles... This thread led me to think that since since the overlay apply code copied data into never freed memory (false premise, as you pointed out) that we did not have to worry about drivers retaining pointers into overlay data after the overlay had been freed (with the one remaining exposure being via the overlay notifiers, which _might_ be easily resolved, pending Alan's analysis) -- this would have been great news for removing an issue for general use of overlays.

But now we are back to the long-standing problem that we have no way of knowing whether there are any live pointers to the memory that is freed by of_changeset_destroy(). And I am not aware of any solution to this problem other than changing the devicetree access API so that it never returns any pointer into the live devicetree.

The practical impact of all of this, is if we can change the overlay notifier parameters to include the overlay changeset instead of the overlay devicetree, then I think that of_overlay_apply() will be able to kfree() the overlay fdt and overlay devicetree. And if not of_overlay_apply(), then free_overlay_changeset().

Quoted 7 lines

I agree that object management in dynamic OF code is hairy and easy to get wrong, e.g. by passing in static objects that suddenly change ownership, and the new owner expects dynamically allocated objects... But that is not cured by leaking memory.

Jan

#37 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 173 lines · Frank Rowand
Quoted 162 lines · Jan Kiszka
Quoted 154 lines · Frank Rowand

Hi Alan,

Quoted 120 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 8 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Question added below this paragraph.

Quoted 6 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of

Quoted 8 lines

the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

Can the fpga notifiers be changed to have the changeset as an input instead of having the overlay devicetree fragment and target as an input?

Quoted 6 lines

The changeset lists nodes and properties to be added, but does not expose any pointers to the overlay fdt or the overlay unflattened devicetree. This guarantees no leakage of pointers into the overlay fdt or the overlay unflattened devicetree. The changeset contains pointers to copies of data, but those copies are never freed (and thus they are yet another existing memory leak).

Quoted 5 lines

Also they are freed, of course: When the last reference to the node they point to reaches 0 (e.g. triggered by of_changeset_destroy), that node goes away and takes down remaining dead properties. I've ran through this already. And I also made sure that my code is not triggering such kind of leaks as well.

mea culpa. I go around in circles while trying to remember all the overlay related issues. I needed to go back and read the code to refresh my memory. Thanks for the prod to re-read the code.

Quoted 8 lines

Yes, of_changeset_destroy() will lead to the kfree() of the node and it's properties _if_ the node reference count is correct. So what I said about a memory leak was incorrect in a perfect world (and my memory was wrong). However, this is not a perfect world and we know that the reference count on devicetree nodes is often incorrect due to bugs in common infrastructure and drivers. This issue will not be resolved until we pull all reference count manipulation into the devicetree core.

I don't get this yet. When I want some value from life tree, I do a node search, get a pointer and the core incremented its reference, can query the node and its properties, and when I'm done, I call of_node_put and forget about all pointers I got. What would you do differently?

Quoted 3 lines

The net result is that we should not expect
overlay removal to correctly free all memory that was allocated when applying the overlay.

Depends on the overlay. If you do not modify existing nodes but only add new ones, it is fair to expect complete removal.

Quoted 9 lines

I _think_ (but did not spend the time to verify) that there is a small corner case memory leak even if the reference count on devicetree nodes is correct. If an overlay adds a property to an existing node then removing the overlay will not kfree() the property, and it will remain on the deadprops list. There are some places that properties are removed from deadprops, but I don't think they fully resolve the issue. Again, this is a corner case, and I am willing to document it as a limitation until it gets fixed.

I ran into this the other day: If you modify an existing property, the old value will be put into deadprops and only be freed when the node is freed. It may come back from deadprops if a changeset comes around with the very same property object for another modification.

But that means: if your overlay just adds nodes, all of them, including their deadprops from potential changes on top, will go away on overlay removal.

BTW, here is my new code that exploits this to be leak-free:
https://github.com/siemens/jailhouse/blob/156a93fcc02585d78d4418d3e6761cd72a65b359/driver/pci.c#L296

Quoted 10 lines

Then returning to me going around in circles... This thread led me to think that since since the overlay apply code copied data into never freed memory (false premise, as you pointed out) that we did not have to worry about drivers retaining pointers into overlay data after the overlay had been freed (with the one remaining exposure being via the overlay notifiers, which _might_ be easily resolved, pending Alan's analysis) -- this would have been great news for removing an issue for general use of overlays.

Quoted 5 lines

But now we are back to the long-standing problem that we have no way of knowing whether there are any live pointers to the memory that is freed by of_changeset_destroy(). And I am not aware of any solution to this problem other than changing the devicetree access API so that it never returns any pointer into the live devicetree.

I don't agree yet with this drastic measure until you can point me to code that pulls and stores pointers to arbitrary devicetree content without that node reference counting. The pattern we otherwise see all around it you get a pointer (or a set of them) along with the duty to explicitly drop it again by some put() operation.

Quoted 6 lines

The practical impact of all of this, is if we can change the overlay notifier parameters to include the overlay changeset instead of the overlay devicetree, then I think that of_overlay_apply() will be able to kfree() the overlay fdt and overlay devicetree. And if not of_overlay_apply(), then free_overlay_changeset().

Isn't that just s/node/changeset/ without any other semantic changes? If the receiver of the changeset reference does not take care of lifecycle management for that object either, we are back at square #1. A changeset is just a gate to the nodes and properties that are currently passed directly.

Jan

#38 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 188 lines · Jan Kiszka
Quoted 173 lines · Frank Rowand
Quoted 162 lines · Jan Kiszka
Quoted 154 lines · Frank Rowand

Hi Alan,

Quoted 120 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 85 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 20 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree
(FDT) into the overlay application code. To accomplish this,
of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree
code, which is thus responsible for freeing the duplicate FDT. The
caller of of_overlay_fdt_apply() remains responsible for freeing the
original FDT.

The unflattened devicetree now belongs to devicetree code, which is
thus responsible for freeing the unflattened devicetree.

These ownership changes prevent early freeing of the duplicated FDT
or the unflattened devicetree, which could result in use after free
errors.

of_overlay_fdt_apply() is a private function for the anticipated
overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs) {
[...]
/*
* TODO
*
* would like to: kfree(ovcs->overlay_tree);
* but can not since drivers may have pointers into this data *
* would like to: kfree(ovcs->fdt);
* but can not since drivers may have pointers into this data */

Quoted 6 lines

kfree(ovcs);
}

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 8 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Question added below this paragraph.

Quoted 6 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of

Quoted 8 lines

the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

Can the fpga notifiers be changed to have the changeset as an input instead of having the overlay devicetree fragment and target as an input?

Quoted 6 lines

The changeset lists nodes and properties to be added, but does not expose any pointers to the overlay fdt or the overlay unflattened devicetree. This guarantees no leakage of pointers into the overlay fdt or the overlay unflattened devicetree. The changeset contains pointers to copies of data, but those copies are never freed (and thus they are yet another existing memory leak).

Quoted 5 lines

Also they are freed, of course: When the last reference to the node they point to reaches 0 (e.g. triggered by of_changeset_destroy), that node goes away and takes down remaining dead properties. I've ran through this already. And I also made sure that my code is not triggering such kind of leaks as well.

mea culpa. I go around in circles while trying to remember all the overlay related issues. I needed to go back and read the code to refresh my memory. Thanks for the prod to re-read the code.

Quoted 8 lines

Yes, of_changeset_destroy() will lead to the kfree() of the node and it's properties _if_ the node reference count is correct. So what I said about a memory leak was incorrect in a perfect world (and my memory was wrong). However, this is not a perfect world and we know that the reference count on devicetree nodes is often incorrect due to bugs in common infrastructure and drivers. This issue will not be resolved until we pull all reference count manipulation into the devicetree core.

I don't get this yet. When I want some value from life tree, I do a node search, get a pointer and the core incremented its reference, can query the node and its properties, and when I'm done, I call of_node_put and forget about all pointers I got. What would you do differently?

Quoted 16 lines
Quoted 3 lines

The net result is that we should not expect
overlay removal to correctly free all memory that was allocated when applying the overlay.

Depends on the overlay. If you do not modify existing nodes but only add new ones, it is fair to expect complete removal.

Quoted 9 lines

I _think_ (but did not spend the time to verify) that there is a small corner case memory leak even if the reference count on devicetree nodes is correct. If an overlay adds a property to an existing node then removing the overlay will not kfree() the property, and it will remain on the deadprops list. There are some places that properties are removed from deadprops, but I don't think they fully resolve the issue. Again, this is a corner case, and I am willing to document it as a limitation until it gets fixed.

This doesn't solve all of your concern, but it gets me wondering whether overlay_notify should add a of_node_get(fragment->overlay) before doing the blocking_notifier_call_chain and a of_node_put afterwards.

Quoted 49 lines

I ran into this the other day: If you modify an existing property, the old value will be put into deadprops and only be freed when the node is freed. It may come back from deadprops if a changeset comes around with the very same property object for another modification.

But that means: if your overlay just adds nodes, all of them, including their deadprops from potential changes on top, will go away on overlay removal.

BTW, here is my new code that exploits this to be leak-free:
https://github.com/siemens/jailhouse/blob/156a93fcc02585d78d4418d3e6761cd72a65b359/driver/pci.c#L296

Quoted 10 lines

Then returning to me going around in circles... This thread led me to think that since since the overlay apply code copied data into never freed memory (false premise, as you pointed out) that we did not have to worry about drivers retaining pointers into overlay data after the overlay had been freed (with the one remaining exposure being via the overlay notifiers, which _might_ be easily resolved, pending Alan's analysis) -- this would have been great news for removing an issue for general use of overlays.

Quoted 5 lines

But now we are back to the long-standing problem that we have no way of knowing whether there are any live pointers to the memory that is freed by of_changeset_destroy(). And I am not aware of any solution to this problem other than changing the devicetree access API so that it never returns any pointer into the live devicetree.

I don't agree yet with this drastic measure until you can point me to code that pulls and stores pointers to arbitrary devicetree content without that node reference counting. The pattern we otherwise see all around it you get a pointer (or a set of them) along with the duty to explicitly drop it again by some put() operation.

Quoted 6 lines

The practical impact of all of this, is if we can change the overlay notifier parameters to include the overlay changeset instead of the overlay devicetree, then I think that of_overlay_apply() will be able to kfree() the overlay fdt and overlay devicetree. And if not of_overlay_apply(), then free_overlay_changeset().

Isn't that just s/node/changeset/ without any other semantic changes? If the receiver of the changeset reference does not take care of lifecycle management for that object either, we are back at square #1. A changeset is just a gate to the nodes and properties that are currently passed directly.

Jan

#39 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

In reply to Jan Kiszka

Hi Jan, Alan,

Quoted 183 lines · Jan Kiszka
Quoted 173 lines · Frank Rowand
Quoted 162 lines · Jan Kiszka
Quoted 154 lines · Frank Rowand

Hi Alan,

Quoted 120 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 20 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree
(FDT) into the overlay application code. To accomplish this,
of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree
code, which is thus responsible for freeing the duplicate FDT. The
caller of of_overlay_fdt_apply() remains responsible for freeing the
original FDT.

The unflattened devicetree now belongs to devicetree code, which is
thus responsible for freeing the unflattened devicetree.

These ownership changes prevent early freeing of the duplicated FDT
or the unflattened devicetree, which could result in use after free
errors.

of_overlay_fdt_apply() is a private function for the anticipated
overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
	[...]
	/*
	 * TODO
	 *
	 * would like to: kfree(ovcs->overlay_tree);
	 * but can not since drivers may have pointers into this data
	 *
	 * would like to: kfree(ovcs->fdt);
	 * but can not since drivers may have pointers into this data
	 */

	kfree(ovcs);
}
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 8 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Question added below this paragraph.

Quoted 6 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of

Quoted 8 lines

the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

Can the fpga notifiers be changed to have the changeset as an input instead of having the overlay devicetree fragment and target as an input?

Quoted 6 lines

The changeset lists nodes and properties to be added, but does not expose any pointers to the overlay fdt or the overlay unflattened devicetree. This guarantees no leakage of pointers into the overlay fdt or the overlay unflattened devicetree. The changeset contains pointers to copies of data, but those copies are never freed (and thus they are yet another existing memory leak).

Quoted 5 lines

Also they are freed, of course: When the last reference to the node they point to reaches 0 (e.g. triggered by of_changeset_destroy), that node goes away and takes down remaining dead properties. I've ran through this already. And I also made sure that my code is not triggering such kind of leaks as well.

mea culpa. I go around in circles while trying to remember all the overlay related issues. I needed to go back and read the code to refresh my memory. Thanks for the prod to re-read the code.

Quoted 8 lines

Yes, of_changeset_destroy() will lead to the kfree() of the node and it's properties _if_ the node reference count is correct. So what I said about a memory leak was incorrect in a perfect world (and my memory was wrong). However, this is not a perfect world and we know that the reference count on devicetree nodes is often incorrect due to bugs in common infrastructure and drivers. This issue will not be resolved until we pull all reference count manipulation into the devicetree core.

Quoted 4 lines

I don't get this yet. When I want some value from life tree, I do a node search, get a pointer and the core incremented its reference, can query the node and its properties, and when I'm done, I call of_node_put and forget about all pointers I got. What would you do differently?

I don't know what causes the refcount errors. I started looking at the issue a couple of years ago, but did not finish and the issue did not remain high enough of my todo list.

I added a printk() to of_node_get() and of_node_put(), then post processed the console log to see where the reference count was being modified. I also had a version of the patch that also printed the call stack so I could better investigate where the gets and puts were invoked from.

Here is an example from a random 4.11-rc1 boot (the columns are address of the device_node, minimum reference count seen, maximum reference count seen, final reference count after the system was booted, and full path of the node):

0xeefe759c    2   23   23  /
0xeefeaec8    2    4    3  /adsp-pil
0xeefe78a0    2    3    3  /aliases
0xeefe7734    2    3    3  /chosen
0xeefea9fc    2    4    4  /clocks
0xeefeac48    2    3    3  /clocks/sleep_clk
0xeefeaa98    2    3    3  /clocks/xo_board
0xeefea8f4    2    4    3  /cpu-pmu
0xeefe86f8    2   10   10  /cpus
0xeefe8830    2    4    4  /cpus/cpu@0
0xeefe8a6c    2    4    4  /cpus/cpu@1
0xeefe8ca8    2    4    4  /cpus/cpu@2
0xeefe8ee4    2    4    4  /cpus/cpu@3
0xeefe92cc    2    2    2  /cpus/idle-states
0xeefe9378    2    6    6  /cpus/idle-states/spc
0xeefe9120    2    2    2  /cpus/l2-cache
0xeefec7e4    2    4    2  /firmware
0xeefec888    2    4    3  /firmware/scm
0xeefe79dc    2    2    2  /memory
0xeefe7ae0    2   11   11  /reserved-memory
0xeefe8224    2    3    2  /reserved-memory/smem@fa00000
0xeeff5010    5    7    6  /smd
0xeeff50dc    2    4    3  /smd/adsp
0xeeff5218    2    4    3  /smd/modem
0xeeff5354    3    5    4  /smd/rpm
0xeeff548c    4    6    5  /smd/rpm/rpm_requests
0xeeff55a8    3   12    3  /smd/rpm/rpm_requests/pm8841-regulators
0xeeff56a4    2    4    4  /smd/rpm/rpm_requests/pm8841-regulators/s1
0xeeff57c8    2    4    4  /smd/rpm/rpm_requests/pm8841-regulators/s2
0xeeff5954    2    4    4  /smd/rpm/rpm_requests/pm8841-regulators/s3
0xeeff5a78    2    4    4  /smd/rpm/rpm_requests/pm8841-regulators/s4
0xeeff5b9c    2    4    4  /smd/rpm/rpm_requests/pm8841-regulators/s5
0xeeff5c58    2    4    4  /smd/rpm/rpm_requests/pm8841-regulators/s6
0xeeff5d14    2    4    4  /smd/rpm/rpm_requests/pm8841-regulators/s7
0xeeff5dd0    2    3    3  /smd/rpm/rpm_requests/pm8841-regulators/s8
0xeeff5e8c    3   37    3  /smd/rpm/rpm_requests/pm8941-regulators
0xeeff8a18    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/5vs1
0xeeff8adc    2    3    3  /smd/rpm/rpm_requests/pm8941-regulators/5vs2
0xeeff678c    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l1
0xeeff7308    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l10
0xeeff7460    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l11
0xeeff7584    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l12
0xeeff7710    2    7    7  /smd/rpm/rpm_requests/pm8941-regulators/l13
0xeeff78d0    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l14
0xeeff79f4    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l15
0xeeff7b18    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l16
0xeeff7c3c    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l17
0xeeff7d60    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l18
0xeeff7e84    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l19
0xeeff6918    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l2
0xeeff7fdc    2    7    7  /smd/rpm/rpm_requests/pm8941-regulators/l20
0xeeff8204    2    7    7  /smd/rpm/rpm_requests/pm8941-regulators/l21
0xeeff83c4    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l22
0xeeff84e8    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l23
0xeeff860c    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l24
0xeeff6a3c    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l3
0xeeff6b60    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l4
0xeeff6c84    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l5
0xeeff6da8    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l6
0xeeff6f68    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l7
0xeeff70c0    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l8
0xeeff71e4    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/l9
0xeeff87cc    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/lvs1
0xeeff8890    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/lvs2
0xeeff8954    2    4    4  /smd/rpm/rpm_requests/pm8941-regulators/lvs3
0xeeff60c0    2    8    8  /smd/rpm/rpm_requests/pm8941-regulators/s1
0xeeff62b4    2   10   10  /smd/rpm/rpm_requests/pm8941-regulators/s2
0xeeff6474    2   11   11  /smd/rpm/rpm_requests/pm8941-regulators/s3
0xeeff6668    2    5    5  /smd/rpm/rpm_requests/pm8941-regulators/s4
0xeefeb0d8    2    4    3  /smem
0xeefeb244    2    6    5  /smp2p-adsp
0xeefeb5dc    2   12   12  /smp2p-adsp/slave-kernel
0xeefeb798    2    6    5  /smp2p-modem
0xeefebc20    2    6    5  /smp2p-wcnss
0xeefec0a8    2    8    7  /smsm
0xeefec9c4    2   37   36  /soc
0xeefee954    2    4    3  /soc/clock-controller@f9088000
0xeefeeae4    2    4    3  /soc/clock-controller@f9098000
0xeefeec74    2    4    3  /soc/clock-controller@f90a8000
0xeefeee04    2    4    3  /soc/clock-controller@f90b8000
0xeefef0a8    2    5    5  /soc/clock-controller@fc400000
0xeefef450    2    4    4  /soc/clock-controller@fd8c0000
0xeeff471c    2    4    3  /soc/dma-controller@f9944000
0xeeff194c    2    2    2  /soc/i2c@f9924000
0xeeff1b90    2    2    2  /soc/i2c@f9964000
0xeeff1dd4    2    5    5  /soc/i2c@f9967000
0xeeff211c    2    3    3  /soc/i2c@f9967000/eeprom@52
0xeefecb2c    2   75   74  /soc/interrupt-controller@f9000000
0xeefef7c0    2    4    3  /soc/memory@fc428000
0xeeff0578    2   10   10  /soc/pinctrl@fd510000
0xeeff07f8    3    4    3  /soc/pinctrl@fd510000/i2c11
0xeeff0910    2    2    2  /soc/pinctrl@fd510000/i2c11/mux
0xeeff0f70    4    5    4  /soc/pinctrl@fd510000/sdhc1-pin-active
0xeeff10a0    2    2    2  /soc/pinctrl@fd510000/sdhc1-pin-active/clk
0xeeff11f8    2    2    2  /soc/pinctrl@fd510000/sdhc1-pin-active/cmd-data
0xeeff135c    2    3    2  /soc/pinctrl@fd510000/sdhc2-cd-pin-active
0xeeff1560    4    5    4  /soc/pinctrl@fd510000/sdhc2-pin-active
0xeeff1690    2    2    2  /soc/pinctrl@fd510000/sdhc2-pin-active/clk
0xeeff17e8    2    2    2  /soc/pinctrl@fd510000/sdhc2-pin-active/cmd-data
0xeeff0a28    6    6    6  /soc/pinctrl@fd510000/spi8_default
0xeefee790    2    4    3  /soc/power-controller@f9012000
0xeefee150    2    4    3  /soc/power-controller@f9089000
0xeefee2e0    2    4    3  /soc/power-controller@f9099000
0xeefee470    2    4    3  /soc/power-controller@f90a9000
0xeefee600    2    4    3  /soc/power-controller@f90b9000
0xeefecea8    2    7    7  /soc/qfprom@fc4bc000
0xeefed174    2    2    2  /soc/qfprom@fc4bc000/backup@440
0xeefed024    2    2    2  /soc/qfprom@fc4bc000/calib@d0
0xeefeef94    2    4    3  /soc/restart@fc4ab000
0xeeff0404    2    4    3  /soc/rng@f9bff000
0xeefefd04    2    4    4  /soc/sdhci@f9824900
0xeeff0084    2    4    4  /soc/sdhci@f98a4900
0xeefef93c    2    2    2  /soc/serial@f991d000
0xeefefb20    2    6    5  /soc/serial@f991e000
0xeeff229c    2  238  237  /soc/spmi@fc4cf000
0xeeff25e8    2    7    5  /soc/spmi@fc4cf000/pm8841@4
0xeeff2768    2    4    4  /soc/spmi@fc4cf000/pm8841@4/mpps@a000
0xeeff2928    2    4    3  /soc/spmi@fc4cf000/pm8841@4/temp-alarm@2400
0xeeff2a88    2    3    2  /soc/spmi@fc4cf000/pm8841@5
0xeeff2c08    2   14   12  /soc/spmi@fc4cf000/pm8941@0
0xeeff30d0    2    4    2  /soc/spmi@fc4cf000/pm8941@0/charger@1000
0xeeff42b0    2    2    2  /soc/spmi@fc4cf000/pm8941@0/coincell@2800
0xeeff3260    3    5    5  /soc/spmi@fc4cf000/pm8941@0/gpios@c000
0xeeff3488    2    3    2  /soc/spmi@fc4cf000/pm8941@0/gpios@c000/boost-bypass
0xeeff4124    2    4    3  /soc/spmi@fc4cf000/pm8941@0/iadc@3600
0xeeff3628    2    4    4  /soc/spmi@fc4cf000/pm8941@0/mpps@a000
0xeeff2f10    2    4    3  /soc/spmi@fc4cf000/pm8941@0/pwrkey@800
0xeeff2d88    2    4    3  /soc/spmi@fc4cf000/pm8941@0/rtc@6000
0xeeff37e8    2    4    3  /soc/spmi@fc4cf000/pm8941@0/temp-alarm@2400
0xeeff39e4    7    9    8  /soc/spmi@fc4cf000/pm8941@0/vadc@3100
0xeeff4410    3    4    3  /soc/spmi@fc4cf000/pm8941@1
0xeeff4590    2    2    2  /soc/spmi@fc4cf000/pm8941@1/wled@d800
0xeefecd2c    2    5    5  /soc/syscon@f9011000
0xeefef2d4    2    4    3  /soc/syscon@fd484000
0xeefef614    2    4    3  /soc/tcsr-mutex
0xeefed2c4    2    4    3  /soc/thermal-sensor@fc4a8000
0xeefed4ec    2   11   10  /soc/timer@f9020000
0xeefed6cc    2    3    3  /soc/timer@f9020000/frame@f9021000
0xeeff49ac    2    2    2  /soc/usb-phy@f9a55000
0xeeff4e00    2    2    2  /soc/usb@f9a55000
0xeefe9558    2    6    6  /thermal-zones
0xeefe9604    3    3    3  /thermal-zones/cpu-thermal0
0xeefe9758    4    4    4  /thermal-zones/cpu-thermal0/trips
0xeefe9810    2    3    3  /thermal-zones/cpu-thermal0/trips/trip0
0xeefe9968    2    3    3  /thermal-zones/cpu-thermal0/trips/trip1
0xeefe9ac0    3    3    3  /thermal-zones/cpu-thermal1
0xeefe9c14    4    4    4  /thermal-zones/cpu-thermal1/trips
0xeefe9ccc    2    3    3  /thermal-zones/cpu-thermal1/trips/trip0
0xeefe9e24    2    3    3  /thermal-zones/cpu-thermal1/trips/trip1
0xeefe9f7c    3    3    3  /thermal-zones/cpu-thermal2
0xeefea0d0    4    4    4  /thermal-zones/cpu-thermal2/trips
0xeefea188    2    3    3  /thermal-zones/cpu-thermal2/trips/trip0
0xeefea2e0    2    3    3  /thermal-zones/cpu-thermal2/trips/trip1
0xeefea438    3    3    3  /thermal-zones/cpu-thermal3
0xeefea58c    4    4    4  /thermal-zones/cpu-thermal3/trips
0xeefea644    2    3    3  /thermal-zones/cpu-thermal3/trips/trip0
0xeefea79c    2    3    3  /thermal-zones/cpu-thermal3/trips/trip1
0xeefead90    2    4    3  /timer
0xeeff8ba0    2    4    4  /vreg-boost
0xeeff8e4c    2    4    3  /vreg-vph-pwr
Quoted 7 lines
Quoted 4 lines

The net result is that we should not expect
overlay removal to correctly free all memory that was allocated when applying the overlay.

Depends on the overlay. If you do not modify existing nodes but only add new ones, it is fair to expect complete removal.

Only if reference counts are correct. I assert that reference counts are often not correct.

Quoted 38 lines
Quoted 9 lines

I _think_ (but did not spend the time to verify) that there is a small corner case memory leak even if the reference count on devicetree nodes is correct. If an overlay adds a property to an existing node then removing the overlay will not kfree() the property, and it will remain on the deadprops list. There are some places that properties are removed from deadprops, but I don't think they fully resolve the issue. Again, this is a corner case, and I am willing to document it as a limitation until it gets fixed.

I ran into this the other day: If you modify an existing property, the old value will be put into deadprops and only be freed when the node is freed. It may come back from deadprops if a changeset comes around with the very same property object for another modification.

But that means: if your overlay just adds nodes, all of them, including their deadprops from potential changes on top, will go away on overlay removal.

BTW, here is my new code that exploits this to be leak-free:
https://github.com/siemens/jailhouse/blob/156a93fcc02585d78d4418d3e6761cd72a65b359/driver/pci.c#L296

Quoted 10 lines

Then returning to me going around in circles... This thread led me to think that since since the overlay apply code copied data into never freed memory (false premise, as you pointed out) that we did not have to worry about drivers retaining pointers into overlay data after the overlay had been freed (with the one remaining exposure being via the overlay notifiers, which _might_ be easily resolved, pending Alan's analysis) -- this would have been great news for removing an issue for general use of overlays.

Quoted 5 lines

But now we are back to the long-standing problem that we have no way of knowing whether there are any live pointers to the memory that is freed by of_changeset_destroy(). And I am not aware of any solution to this problem other than changing the devicetree access API so that it never returns any pointer into the live devicetree.

Quoted 5 lines

I don't agree yet with this drastic measure until you can point me to code that pulls and stores pointers to arbitrary devicetree content without that node reference counting. The pattern we otherwise see all around it you get a pointer (or a set of them) along with the duty to explicitly drop it again by some put() operation.

And we have long history of these rules not being followed. Evidence is the patches we get to correct incorrect of_node_get() / of_node_put() usage and the random node reference count report I included above.

That is also conflating the issue of reference counts being correct and whether pointers are held after a reference count is decremented. I am not going to audit the kernel and future patches to ensure that.

Quoted 7 lines
Quoted 6 lines

The practical impact of all of this, is if we can change the overlay notifier parameters to include the overlay changeset instead of the overlay devicetree, then I think that of_overlay_apply() will be able to kfree() the overlay fdt and overlay devicetree. And if not of_overlay_apply(), then free_overlay_changeset().

Quoted 5 lines

Isn't that just s/node/changeset/ without any other semantic changes? If the receiver of the changeset reference does not take care of lifecycle management for that object either, we are back at square #1. A changeset is just a gate to the nodes and properties that are currently passed directly.

Sigh. Yes. Thanks for pointing that out.

So my thought of changing the arguments of the overlay notifier was wrong and we should just stick with the current calling convention for the overlay notifiers. But we need to document the "no retaining pointers into the overlay devicetree" for the overlay notifiers.

Thank you Alan for looking into the alternative approach.

OK, being pragmatic about not letting failure to achieve perfection stand in the way of achieving good enough -- I am willing to accept the review burden of overlay notifiers and that review will be an acceptable defense against errors for the overlay notifiers. Although my long-term goal is to change to devicetree access APIs to not return pointers into the devicetree (in this case, the overlay devicetree, not the live devicetree) to preclude the possibility of pointer leakage.

I will repeat this conditional acceptance in reply to the proposed patch that you sent.

-Frank

Quoted 4 lines

Jan
.

#40 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

In reply to Frank Rowand
Quoted 150 lines · Frank Rowand

Hi Alan,

Quoted 120 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
     [...]
     /*
      * TODO
      *
      * would like to: kfree(ovcs->overlay_tree);
      * but can not since drivers may have pointers into this data
      *
      * would like to: kfree(ovcs->fdt);
      * but can not since drivers may have pointers into this data
      */

     kfree(ovcs);
}
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 8 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Question added below this paragraph.

Quoted 6 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of

Quoted 8 lines

the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

Quoted 3 lines

Can the fpga notifiers be changed to have the changeset as an input instead of having the overlay devicetree fragment and target as an input?

I'll look into it. Just to be clear, are you suggesting passing struct overlay_changeset instead in the notifier?

struct overlay_changeset and struct fragment would have to be moved to a header.

Quoted 26 lines

The changeset lists nodes and properties to be added, but does not expose any pointers to the overlay fdt or the overlay unflattened devicetree. This guarantees no leakage of pointers into the overlay fdt or the overlay unflattened devicetree. The changeset contains pointers to copies of data, but those copies are never freed (and thus they are yet another existing memory leak).

-Frank

Quoted 15 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

-Frank

#41 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 151 lines · Alan Tull
Quoted 150 lines · Frank Rowand

Hi Alan,

Quoted 120 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
     [...]
     /*
      * TODO
      *
      * would like to: kfree(ovcs->overlay_tree);
      * but can not since drivers may have pointers into this data
      *
      * would like to: kfree(ovcs->fdt);
      * but can not since drivers may have pointers into this data
      */

     kfree(ovcs);
}
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 8 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Question added below this paragraph.

Quoted 14 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

Quoted 6 lines
Quoted 3 lines

Can the fpga notifiers be changed to have the changeset as an input instead of having the overlay devicetree fragment and target as an input?

I'll look into it. Just to be clear, are you suggesting passing struct overlay_changeset instead in the notifier?

Ah, poor phrasing on my part. I meant a "struct of_changeset", as is passed into __of_changeset_apply_entries(), which is called from of_overlay_apply(). This means that the call to overlay_notify() would have to move down a few lines to just after calling build_changeset().

Quoted 29 lines

struct overlay_changeset and struct fragment would have to be moved to a header.

Quoted 26 lines

The changeset lists nodes and properties to be added, but does not expose any pointers to the overlay fdt or the overlay unflattened devicetree. This guarantees no leakage of pointers into the overlay fdt or the overlay unflattened devicetree. The changeset contains pointers to copies of data, but those copies are never freed (and thus they are yet another existing memory leak).

-Frank

Quoted 15 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

-Frank

#42 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 156 lines · Frank Rowand
Quoted 155 lines · Alan Tull
Quoted 153 lines · Frank Rowand

Hi Alan,

Quoted 120 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 14 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree (FDT) into the overlay application code. To accomplish this, of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree code, which is thus responsible for freeing the duplicate FDT. The caller of of_overlay_fdt_apply() remains responsible for freeing the original FDT.

The unflattened devicetree now belongs to devicetree code, which is thus responsible for freeing the unflattened devicetree.

Quoted 6 lines

These ownership changes prevent early freeing of the duplicated FDT or the unflattened devicetree, which could result in use after free errors.

of_overlay_fdt_apply() is a private function for the anticipated overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
     [...]
     /*
      * TODO
      *
      * would like to: kfree(ovcs->overlay_tree);
      * but can not since drivers may have pointers into this data
      *
      * would like to: kfree(ovcs->fdt);
      * but can not since drivers may have pointers into this data
      */

     kfree(ovcs);
}
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 8 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Question added below this paragraph.

Quoted 6 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of

Quoted 8 lines

the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

Can the fpga notifiers be changed to have the changeset as an input instead of having the overlay devicetree fragment and target as an input?

Quoted 8 lines
Quoted 2 lines

I'll look into it. Just to be clear, are you suggesting passing struct overlay_changeset instead in the notifier?

Ah, poor phrasing on my part. I meant a "struct of_changeset", as is passed into __of_changeset_apply_entries(), which is called from of_overlay_apply(). This means that the call to overlay_notify() would have to move down a few lines to just after calling build_changeset().

Ah yes, I thought it was looking too easy. :) I had it working with notify data passing the struct overlay_changeset, I was about to send you the patch.

The FPGA code really wants the data as fragments, so it will know first of all what the target is. Passing of_changeset would mean that the code receiving the notification would be essentially be tasked reassembling the changeset into fragments. Perhaps it could be done, but it could easily be broken by changes to overlay.c and it would be
ugly. That breaks the exact thing that I added overlay notifications
for. I really need to see for each fragment what the target is, and all the properties together.

Quoted 32 lines
Quoted 31 lines

struct overlay_changeset and struct fragment would have to be moved to a header.

Quoted 26 lines

The changeset lists nodes and properties to be added, but does not expose any pointers to the overlay fdt or the overlay unflattened devicetree. This guarantees no leakage of pointers into the overlay fdt or the overlay unflattened devicetree. The changeset contains pointers to copies of data, but those copies are never freed (and thus they are yet another existing memory leak).

-Frank

Quoted 15 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

-Frank

#43 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Hi Alan,

Quoted 170 lines · Alan Tull
Quoted 156 lines · Frank Rowand
Quoted 155 lines · Alan Tull
Quoted 153 lines · Frank Rowand

Hi Alan,

Quoted 120 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 30 lines · Jan Kiszka

Hi Frank,

Quoted 20 lines · On 2018-03-04 01:17, [email protected] wrote:

From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree
(FDT) into the overlay application code. To accomplish this,
of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree
code, which is thus responsible for freeing the duplicate FDT. The
caller of of_overlay_fdt_apply() remains responsible for freeing the
original FDT.

The unflattened devicetree now belongs to devicetree code, which is
thus responsible for freeing the unflattened devicetree.

These ownership changes prevent early freeing of the duplicated FDT
or the unflattened devicetree, which could result in use after free
errors.

of_overlay_fdt_apply() is a private function for the anticipated
overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the (out-of-tree) Jailhouse loader driver in order to register a virtual device during hypervisor activation with Linux. The DT overlay is created from a a template but modified prior to application to account for runtime-specific parameters. See [1] for the current implementation.

Quoted 5 lines

I'm now wondering how to model that scenario best with the new API. Given that the loader lost ownership of the unflattened tree but the modification API exist only for the that DT state, I'm not yet seeing a clear solution. Should we apply the template in disabled form (status = "disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
     [...]
     /*
      * TODO
      *
      * would like to: kfree(ovcs->overlay_tree);
      * but can not since drivers may have pointers into this data
      *
      * would like to: kfree(ovcs->fdt);
      * but can not since drivers may have pointers into this data
      */

     kfree(ovcs);
}
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 8 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Question added below this paragraph.

Quoted 6 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of

Quoted 8 lines

the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

Can the fpga notifiers be changed to have the changeset as an input instead of having the overlay devicetree fragment and target as an input?

Quoted 8 lines
Quoted 2 lines

I'll look into it. Just to be clear, are you suggesting passing struct overlay_changeset instead in the notifier?

Ah, poor phrasing on my part. I meant a "struct of_changeset", as is passed into __of_changeset_apply_entries(), which is called from of_overlay_apply(). This means that the call to overlay_notify() would have to move down a few lines to just after calling build_changeset().

Ah yes, I thought it was looking too easy. :) I had it working with notify data passing the struct overlay_changeset, I was about to send you the patch.

Quoted 8 lines

The FPGA code really wants the data as fragments, so it will know first of all what the target is. Passing of_changeset would mean that the code receiving the notification would be essentially be tasked reassembling the changeset into fragments. Perhaps it could be done, but it could easily be broken by changes to overlay.c and it would be
ugly. That breaks the exact thing that I added overlay notifications
for. I really need to see for each fragment what the target is, and all the properties together.

approach I proposed here is not solving the underlying problem, but just moving it to another place. So I am not going to pursue this approach.

-Frank

Quoted 34 lines
Quoted 33 lines
Quoted 32 lines

struct overlay_changeset and struct fragment would have to be moved to a header.

Quoted 26 lines

The changeset lists nodes and properties to be added, but does not expose any pointers to the overlay fdt or the overlay unflattened devicetree. This guarantees no leakage of pointers into the overlay fdt or the overlay unflattened devicetree. The changeset contains pointers to copies of data, but those copies are never freed (and thus they are yet another existing memory leak).

-Frank

Quoted 15 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

-Frank

#44 lore raw

Re: [PATCH v7 2/5] of: change overlay apply input data from unflattened to FDT

Quoted 182 lines · Frank Rowand

Hi Alan,

Quoted 178 lines · Alan Tull
Quoted 156 lines · Frank Rowand
Quoted 155 lines · Alan Tull
Quoted 153 lines · Frank Rowand

Hi Alan,

Quoted 120 lines · Frank Rowand

Hi Jan,

+ Alan Tull for fpga perspective

Quoted 88 lines · Jan Kiszka
Quoted 64 lines · Jan Kiszka
Quoted 51 lines · Rob Herring
Quoted 50 lines · Frank Rowand
Quoted 48 lines · Jan Kiszka
Quoted 39 lines · Frank Rowand

Hi Jan,

Quoted 35 lines · Jan Kiszka

Hi Frank,

On 2018-03-04 01:17, [email protected] wrote:
From: Frank Rowand <[email protected]>

Move duplicating and unflattening of an overlay flattened devicetree
(FDT) into the overlay application code. To accomplish this,
of_overlay_apply() is replaced by of_overlay_fdt_apply().

The copy of the FDT (aka "duplicate FDT") now belongs to devicetree
code, which is thus responsible for freeing the duplicate FDT. The
caller of of_overlay_fdt_apply() remains responsible for freeing the
original FDT.

The unflattened devicetree now belongs to devicetree code, which is
thus responsible for freeing the unflattened devicetree.

These ownership changes prevent early freeing of the duplicated FDT
or the unflattened devicetree, which could result in use after free
errors.

of_overlay_fdt_apply() is a private function for the anticipated
overlay loader.

We are using of_fdt_unflatten_tree + of_overlay_apply in the
(out-of-tree) Jailhouse loader driver in order to register a virtual
device during hypervisor activation with Linux. The DT overlay is
created from a a template but modified prior to application to account
for runtime-specific parameters. See [1] for the current implementation.

I'm now wondering how to model that scenario best with the new API.
Given that the loader lost ownership of the unflattened tree but the
modification API exist only for the that DT state, I'm not yet seeing a
clear solution. Should we apply the template in disabled form (status =
"disabled"), modify it, and then activate it while it is already applied?

Quoted 5 lines

Thank you for the pointer to the driver - that makes it much easier to understand the use case and consider solutions.

If you can make the changes directly on the FDT instead of on the expanded devicetree, then you could move to the new API.

Are there some examples/references on how to edit FDTs in-place in the kernel? I'd like to avoid writing the n-th FDT parser/generator.

Quoted 7 lines
Quoted 3 lines

I don't know of any existing in-kernel edits of the FDT (but they might exist). The functions to access an FDT are in libfdt, which is in scripts/dtc/libfdt/.

Let's please not go down that route of doing FDT modifications. There is little reason to other than for early boot changes. And it is much easier to work on unflattened trees.

I just briefly looked into libfdt, and it would have meant building it into the module as there are no library functions exported by the kernel either. Another reason to drop that.

Quoted 6 lines

What's apparently working now is the pattern I initially suggested:
Register template with status = "disabled" as overlay, then prepare and apply changeset that contains all needed modifications and sets the status to "ok". I might be leaking additional resources, but to find that out, I will now finally have to resolve clean unbinding of the generic PCI host controller [1] first.

static void free_overlay_changeset(struct overlay_changeset *ovcs)

{
     [...]
     /*
      * TODO
      *
      * would like to: kfree(ovcs->overlay_tree);
      * but can not since drivers may have pointers into this data
      *
      * would like to: kfree(ovcs->fdt);
      * but can not since drivers may have pointers into this data
      */

     kfree(ovcs);
}
Quoted 3 lines

What's this? I have kmemleak now jumping at me over this. Who is suppose to plug these leaks? The caller of of_overlay_fdt_apply has no pointers to those objects. I would say that's a regression of the new API.

The problem already existed but it was hidden. We have never been able to kfree() these object because we do not know if there are any pointers into these objects. The new API makes the problem visible to kmemleak.

The reason that we do not know if there are any pointers into these objects is that devicetree access APIs return pointers into the devicetree internal data structures (that is, into the overlay unflattened devicetree). If we want to be able to do the kfree()s, we could change the devicetree access APIs.

The reason that pointers into the overlay flattened tree (ovcs->fdt) are also exposed is that the overlay unflattened devicetree property values are pointers into the overlay fdt.

** This paragraph becomes academic (and not needed) if the fix in the next paragraph can be implemented. **
I _think_ that the fdt issue __for overlays__ can be fixed somewhat easily. (I would want to read through the code again to make sure I'm not missing any issues.) If the of_fdt_unflatten_tree() called by of_overlay_fdt_apply() was modified so that property values were copied into newly allocated memory and the live tree property pointers were set to the copy instead of to the value in the fdt, then I _think_ the fdt could be freed in of_overlay_fdt_apply() after calling of_overlay_apply(). The code that

Quoted 8 lines

frees a devicetree would also have to be aware of this change -- I'm not sure if that leads to ugly complications or if it is easy. The other question to consider is whether to make the same change to of_fdt_unflatten_tree() when it is called in early boot to unflatten the base devicetree. Doing so would increase the memory usage of the live tree (we would not be able to free the base fdt after unflattening it because we make the fdt visible in /sys/firmware/fdt -- though _maybe_ that could be conditioned on CONFIG_KEXEC).

Question added below this paragraph.

Quoted 6 lines

But all of the complexity of that fix is _only_ because of_overlay_apply() and of_overlay_remove() call overlay_notify(), passing in the overlay unflattened devicetree (which has pointers into the overlay fdt). Pointers into the overlay unflattened devicetree are then passed to the notifiers. (Again, I may be missing some other place that the overlay unflattened devicetree is made visible to other code -- a more thorough reading of

Quoted 8 lines

the code is needed.) If the notifiers could be modified to accept the changeset list instead of of pointers to the fragments in the overlay unflattened devicetree then there would be no possibility of the notifiers keeping a pointer into the overlay fdt. I do not know if this is a practical change for the notifiers -- there are no callers of of_overlay_notifier_register() in the mainline kernel source. My recollection is that the overlay notifiers were added for the fpga subsystem.

Can the fpga notifiers be changed to have the changeset as an input instead of having the overlay devicetree fragment and target as an input?

Quoted 8 lines
Quoted 2 lines

I'll look into it. Just to be clear, are you suggesting passing struct overlay_changeset instead in the notifier?

Ah, poor phrasing on my part. I meant a "struct of_changeset", as is passed into __of_changeset_apply_entries(), which is called from of_overlay_apply(). This means that the call to overlay_notify() would have to move down a few lines to just after calling build_changeset().

Ah yes, I thought it was looking too easy. :) I had it working with notify data passing the struct overlay_changeset, I was about to send you the patch.

The FPGA code really wants the data as fragments, so it will know first of all what the target is. Passing of_changeset would mean that the code receiving the notification would be essentially be tasked reassembling the changeset into fragments. Perhaps it could be done, but it could easily be broken by changes to overlay.c and it would be
ugly. That breaks the exact thing that I added overlay notifications
for. I really need to see for each fragment what the target is, and all the properties together.

I inadvertently chopped off part of my reply. I think is also meant to say something like:

As Jan pointed out in another email, the

Quoted 41 lines

approach I proposed here is not solving the underlying problem, but just moving it to another place. So I am not going to pursue this approach.

-Frank

Quoted 34 lines
Quoted 33 lines
Quoted 32 lines

struct overlay_changeset and struct fragment would have to be moved to a header.

Quoted 26 lines

The changeset lists nodes and properties to be added, but does not expose any pointers to the overlay fdt or the overlay unflattened devicetree. This guarantees no leakage of pointers into the overlay fdt or the overlay unflattened devicetree. The changeset contains pointers to copies of data, but those copies are never freed (and thus they are yet another existing memory leak).

-Frank

Quoted 15 lines

Why is overlay_notify() the only issue related to unknown users having pointers into the overlay fdt? The answer is that the overlay code does not directly expose the overlay unflattened devicetree (and thus indirectly the overlay fdt) to the live devicetree -- when the overlay code creates the overlay changeset, it copies from the overlay unflattened devicetree and overlay fdt and only exposes pointers to the copies.

And hopefully the issues with the overlay unflattened devicetree can be resolved in the same way as for the overlay fdt.

-Frank

#45 lore raw

PATCH v7 3/5 of: Documentation: of_overlay_apply() replaced by of_overlay_fdt_apply()

In reply to [email protected]

From: Frank Rowand <[email protected]>

---
 Documentation/devicetree/overlay-notes.txt | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/devicetree/overlay-notes.txt +2 -2
index c4aa0adf13ec..5175a24d387e 100644
@@ -87,8 +87,8 @@ Overlay in-kernel API
 
 The API is quite easy to use.
 
-1. Call of_overlay_apply() to create and apply an overlay changeset. The return
-value is an error or a cookie identifying this overlay.
+1. Call of_overlay_fdt_apply() to create and apply an overlay changeset. The
+return value is an error or a cookie identifying this overlay.
 
 2. Call of_overlay_remove() to remove and cleanup the overlay changeset
 previously created via the call to of_overlay_apply(). Removal of an overlay
Signature · 1 line
Frank Rowand <[email protected]>
#46 lore raw

PATCH v7 4/5 of: convert unittest overlay devicetree source to sugar syntax

In reply to [email protected]

From: Frank Rowand <[email protected]>

The unittest-data overlays have been pulled into proper overlay devicetree source files without changing their format. The next step is to convert them to use sugar syntax instead of hand coding overlay fragments structure.

A few of the overlays can not be converted because they test absolute target paths in the overlay fragment. dtc does not generate this type of target:
overlay_0.dts
overlay_1.dts
overlay_12.dts
overlay_13.dts

Two pre-existing unittest overlay devicetree source files are also converted:
overlay_bad_phandle.dts
overlay_bad_symbol.dts

---

There are checkpatch warnings. I have reviewed them and feel they can be ignored. They are pre-existing warnings of un-documented bindings of made up (fake) compatibles in devicetree source for test overlays.

 drivers/of/unittest-data/overlay.dts             | 101 ++++++++++-------------
 drivers/of/unittest-data/overlay_10.dts          |  39 ++++-----
 drivers/of/unittest-data/overlay_11.dts          |  40 ++++-----
 drivers/of/unittest-data/overlay_15.dts          |  41 ++++-----
 drivers/of/unittest-data/overlay_2.dts           |  11 +--
 drivers/of/unittest-data/overlay_3.dts           |  11 +--
 drivers/of/unittest-data/overlay_4.dts           |  23 ++----
 drivers/of/unittest-data/overlay_5.dts           |  11 +--
 drivers/of/unittest-data/overlay_6.dts           |  13 +--
 drivers/of/unittest-data/overlay_7.dts           |  13 +--
 drivers/of/unittest-data/overlay_8.dts           |  13 +--
 drivers/of/unittest-data/overlay_9.dts           |  13 +--
 drivers/of/unittest-data/overlay_bad_phandle.dts |  23 ++----
 drivers/of/unittest-data/overlay_bad_symbol.dts  |  25 ++----
 drivers/of/unittest-data/tests-overlay.dtsi      |   4 +-
 15 files changed, 148 insertions(+), 233 deletions(-)
drivers/of/unittest-data/overlay.dts +44 -57
index ab5e89b5e27e..3bbc59e922fe 100644
@@ -2,76 +2,63 @@
 /dts-v1/;
 /plugin/;
 
-/ {
+&electric_1 {
 
-	fragment@0 {
-		target = <&electric_1>;
+	status = "okay";
 
-		__overlay__ {
-			status = "okay";
-
-			hvac_2: hvac-large-1 {
-				compatible = "ot,hvac-large";
-				heat-range = < 40 75 >;
-				cool-range = < 65 80 >;
-			};
-		};
+	hvac_2: hvac-large-1 {
+		compatible = "ot,hvac-large";
+		heat-range = < 40 75 >;
+		cool-range = < 65 80 >;
 	};
+};
 
-	fragment@1 {
-		target = <&rides_1>;
-
-		__overlay__ {
-			#address-cells = <1>;
-			#size-cells = <1>;
-			status = "okay";
-
-			ride@100 {
-				#address-cells = <1>;
-				#size-cells = <1>;
-
-				track@30 {
-					incline-up = < 48 32 16 >;
-				};
+&rides_1 {
 
-				track@40 {
-					incline-up = < 47 31 15 >;
-				};
-			};
+	#address-cells = <1>;
+	#size-cells = <1>;
+	status = "okay";
 
-			ride_200: ride@200 {
-				#address-cells = <1>;
-				#size-cells = <1>;
-				compatible = "ot,ferris-wheel";
-				reg = < 0x00000200 0x100 >;
-				hvac-provider = < &hvac_2 >;
-				hvac-thermostat = < 27 32 > ;
-				hvac-zones = < 12 5 >;
-				hvac-zone-names = "operator", "snack-bar";
-				spin-controller = < &spin_ctrl_1 3 >;
-				spin-rph = < 30 >;
-				gondolas = < 16 >;
-				gondola-capacity = < 6 >;
+	ride@100 {
+		#address-cells = <1>;
+		#size-cells = <1>;
 
-				ride_200_left: track@10 {
-					reg = < 0x00000010 0x10 >;
-				};
+		track@30 {
+			incline-up = < 48 32 16 >;
+		};
 
-				ride_200_right: track@20 {
-					reg = < 0x00000020 0x10 >;
-				};
-			};
+		track@40 {
+			incline-up = < 47 31 15 >;
 		};
 	};
 
-	fragment@2 {
-		target = <&lights_2>;
+	ride_200: ride@200 {
+		#address-cells = <1>;
+		#size-cells = <1>;
+		compatible = "ot,ferris-wheel";
+		reg = < 0x00000200 0x100 >;
+		hvac-provider = < &hvac_2 >;
+		hvac-thermostat = < 27 32 > ;
+		hvac-zones = < 12 5 >;
+		hvac-zone-names = "operator", "snack-bar";
+		spin-controller = < &spin_ctrl_1 3 >;
+		spin-rph = < 30 >;
+		gondolas = < 16 >;
+		gondola-capacity = < 6 >;
+
+		ride_200_left: track@10 {
+			reg = < 0x00000010 0x10 >;
+		};
 
-		__overlay__ {
-			status = "okay";
-			color = "purple", "white", "red", "green";
-			rate = < 3 256 >;
+		ride_200_right: track@20 {
+			reg = < 0x00000020 0x10 >;
 		};
 	};
+};
+
+&lights_2 {
 
+	status = "okay";
+	color = "purple", "white", "red", "green";
+	rate = < 3 256 >;
 };
drivers/of/unittest-data/overlay_10.dts +16 -23
index 445925a10cd3..73993bf23bf8 100644
@@ -2,33 +2,26 @@
 /dts-v1/;
 /plugin/;
 
-/ {
-	/* overlay_10 */
-	/* overlays 8, 9, 10, 11 application and removal in bad sequence */
+/* overlay_10 */
+/* overlays 8, 9, 10, 11 application and removal in bad sequence */
 
-	fragment@0 {
-		target-path = "/testcase-data/overlay-node/test-bus";
-		__overlay__ {
+&unittest_test_bus {
+	/* suppress DTC warning */
+	#address-cells = <1>;
+	#size-cells = <0>;
 
-			/* suppress DTC warning */
-			#address-cells = <1>;
-			#size-cells = <0>;
+	test-unittest10 {
+		compatible = "unittest";
+		status = "okay";
+		reg = <10>;
 
-			test-unittest10 {
-				compatible = "unittest";
-				status = "okay";
-				reg = <10>;
+		#address-cells = <1>;
+		#size-cells = <0>;
 
-				#address-cells = <1>;
-				#size-cells = <0>;
-
-				test-unittest101 {
-					compatible = "unittest";
-					status = "okay";
-					reg = <1>;
-				};
-
-			};
+		test-unittest101 {
+			compatible = "unittest";
+			status = "okay";
+			reg = <1>;
 		};
 	};
 };
drivers/of/unittest-data/overlay_11.dts +17 -23
index c1d14f34359e..9a79b253a809 100644
@@ -2,33 +2,27 @@
 /dts-v1/;
 /plugin/;
 
-/ {
-	/* overlay_11 */
-	/* overlays 8, 9, 10, 11 application and removal in bad sequence */
+/* overlay_11 */
+/* overlays 8, 9, 10, 11 application and removal in bad sequence */
 
-	fragment@0 {
-		target-path = "/testcase-data/overlay-node/test-bus";
-		__overlay__ {
+&unittest_test_bus {
+	/* suppress DTC warning */
+	#address-cells = <1>;
+	#size-cells = <0>;
 
-			/* suppress DTC warning */
-			#address-cells = <1>;
-			#size-cells = <0>;
+	test-unittest11 {
+		compatible = "unittest";
+		status = "okay";
+		reg = <11>;
 
-			test-unittest11 {
-				compatible = "unittest";
-				status = "okay";
-				reg = <11>;
+		#address-cells = <1>;
+		#size-cells = <0>;
 
-				#address-cells = <1>;
-				#size-cells = <0>;
-
-				test-unittest111 {
-					compatible = "unittest";
-					status = "okay";
-					reg = <1>;
-				};
-
-			};
+		test-unittest111 {
+			compatible = "unittest";
+			status = "okay";
+			reg = <1>;
 		};
+
 	};
 };
drivers/of/unittest-data/overlay_15.dts +18 -23

51 lines not on this page · raw message

drivers/of/unittest-data/overlay_2.dts +3 -8
index cf1e4245b7ce..db8684ba89d9 100644
@@ -2,13 +2,8 @@
 /dts-v1/;
 /plugin/;
 
-/ {
-	/* overlay_2 -  enable using label */
+/* overlay_2 -  enable using label */
 
-	fragment@0 {
-		target = <&unittest2>;
-		__overlay__ {
-			status = "okay";
-		};
-	};
+&unittest2 {
+	status = "okay";
 };
drivers/of/unittest-data/overlay_3.dts +3 -8
index 158dc44fc20a..40f289e7c237 100644
@@ -2,13 +2,8 @@
 /dts-v1/;
 /plugin/;
 
-/ {
-	/* overlay_3 - disable using label */
+/* overlay_3 - disable using label */
 
-	fragment@0 {
-		target = <&unittest3>;
-		__overlay__ {
-			status = "disabled";
-		};
-	};
+&unittest3 {
+	status = "disabled";
 };
drivers/of/unittest-data/overlay_4.dts +9 -14
index b4a2e6c6b016..a8a77ddf9abe 100644
@@ -2,22 +2,17 @@
 /dts-v1/;
 /plugin/;
 
-/ {
-	/* overlay_4 - test insertion of a full node */
+/* overlay_4 - test insertion of a full node */
 
-	fragment@0 {
-		target = <&unittestbus>;
-		__overlay__ {
+&unittest_test_bus {
 
-			/* suppress DTC warning */
-			#address-cells = <1>;
-			#size-cells = <0>;
+	/* suppress DTC warning */
+	#address-cells = <1>;
+	#size-cells = <0>;
 
-			test-unittest4 {
-				compatible = "unittest";
-				status = "okay";
-				reg = <4>;
-			};
-		};
+	test-unittest4 {
+		compatible = "unittest";
+		status = "okay";
+		reg = <4>;
 	};
 };
drivers/of/unittest-data/overlay_5.dts +3 -8
index 02ad25c1f19c..706f5f1b737c 100644
@@ -2,13 +2,8 @@
 /dts-v1/;
 /plugin/;
 
-/ {
-	/* overlay_5 - test overlay apply revert */
+/* overlay_5 - test overlay apply revert */
 
-	fragment@0 {
-		target-path = "/testcase-data/overlay-node/test-bus/test-unittest5";
-		__overlay__ {
-			status = "okay";
-		};
-	};
+&unittest5 {
+	status = "okay";
 };
drivers/of/unittest-data/overlay_6.dts +4 -9
index a14e965f5497..21a7fa4ca45e 100644
@@ -2,14 +2,9 @@
 /dts-v1/;
 /plugin/;
 
-/ {
-	/* overlay_6 */
-	/* overlays 6, 7 application and removal in sequence */
+/* overlay_6 */
+/* overlays 6, 7 application and removal in sequence */
 
-	fragment@0 {
-		target-path = "/testcase-data/overlay-node/test-bus/test-unittest6";
-		__overlay__ {
-			status = "okay";
-		};
-	};
+&unittest6 {
+	status = "okay";
 };
drivers/of/unittest-data/overlay_7.dts +4 -9
index 4bd7e423209c..58ba1bb51b50 100644
@@ -2,14 +2,9 @@
 /dts-v1/;
 /plugin/;
 
-/ {
-	/* overlay_7 */
-	/* overlays 6, 7 application and removal in sequence */
+/* overlay_7 */
+/* overlays 6, 7 application and removal in sequence */
 
-	fragment@0 {
-		target-path = "/testcase-data/overlay-node/test-bus/test-unittest7";
-		__overlay__ {
-			status = "okay";
-		};
-	};
+&unittest7 {
+	status = "okay";
 };
drivers/of/unittest-data/overlay_8.dts +4 -9
index 5b21c53945a9..e9718d118e38 100644
@@ -2,14 +2,9 @@
 /dts-v1/;
 /plugin/;
 
-/ {
-	/* overlay_8 */
-	/* overlays 8, 9, 10, 11 application and removal in bad sequence */
+/* overlay_8 */
+/* overlays 8, 9, 10, 11 application and removal in bad sequence */
 
-	fragment@0 {
-		target-path = "/testcase-data/overlay-node/test-bus/test-unittest8";
-		__overlay__ {
-			status = "okay";
-		};
-	};
+&unittest8 {
+	status = "okay";
 };
drivers/of/unittest-data/overlay_9.dts +4 -9
index 20ff055a5349..b35e23edae50 100644
@@ -2,14 +2,9 @@
 /dts-v1/;
 /plugin/;
 
-/ {
-	/* overlay_9 */
-	/* overlays 8, 9, 10, 11 application and removal in bad sequence */
+/* overlay_9 */
+/* overlays 8, 9, 10, 11 application and removal in bad sequence */
 
-	fragment@0 {
-		target-path = "/testcase-data/overlay-node/test-bus/test-unittest8";
-		__overlay__ {
-			property-foo = "bar";
-		};
-	};
+&unittest8 {
+	property-foo = "bar";
 };
drivers/of/unittest-data/overlay_bad_phandle.dts +8 -15
index 4d5b99723bad..83b797360318 100644
@@ -2,20 +2,13 @@
 /dts-v1/;
 /plugin/;
 
-/ {
-
-	fragment@0 {
-		target = <&electric_1>;
-
-		__overlay__ {
-
-			// This label should cause an error when the overlay
-			// is applied.  There is already a phandle value
-			// in the base tree for motor-1.
-			spin_ctrl_1_conflict: motor-1 {
-				accelerate = < 3 >;
-				decelerate = < 5 >;
-			};
-		};
+&electric_1 {
+
+	// This label should cause an error when the overlay
+	// is applied.  There is already a phandle value
+	// in the base tree for motor-1.
+	spin_ctrl_1_conflict: motor-1 {
+		accelerate = < 3 >;
+		decelerate = < 5 >;
 	};
 };
drivers/of/unittest-data/overlay_bad_symbol.dts +9 -16
index 135052ee1517..98c6d1de144a 100644
@@ -2,22 +2,15 @@
 /dts-v1/;
 /plugin/;
 
-/ {
+&electric_1 {
 
-	fragment@0 {
-		target = <&electric_1>;
-
-		__overlay__ {
-
-			// This label should cause an error when the overlay
-			// is applied.  There is already a symbol hvac_1
-			// in the base tree
-			hvac_1: hvac-medium-2 {
-				compatible = "ot,hvac-medium";
-				heat-range = < 50 75 >;
-				cool-range = < 60 80 >;
-			};
-
-		};
+	// This label should cause an error when the overlay
+	// is applied.  There is already a symbol hvac_1
+	// in the base tree
+	hvac_1: hvac-medium-2 {
+		compatible = "ot,hvac-medium";
+		heat-range = < 50 75 >;
+		cool-range = < 60 80 >;
 	};
+
 };
drivers/of/unittest-data/tests-overlay.dtsi +2 -2
index fa2fb43bccac..25cf397b8f6b 100644
@@ -5,7 +5,7 @@
 		overlay-node {
 
 			/* test bus */
-			unittestbus: test-bus {
+			unittest_test_bus: test-bus {
 				compatible = "simple-bus";
 				#address-cells = <1>;
 				#size-cells = <0>;
@@ -70,7 +70,7 @@
 					reg = <8>;
 				};
 
-				i2c-test-bus {
+				unittest_i2c_test_bus: i2c-test-bus {
 					compatible = "unittest-i2c-bus";
 					status = "okay";
 					reg = <50>;
Signature · 1 line
Frank Rowand <[email protected]>
#47 lore raw

PATCH v7 5/5 of: improve reporting invalid overlay target path

In reply to [email protected]

From: Frank Rowand <[email protected]>

Errors while developing the patch to create of_overlay_fdt_apply() exposed inadequate error messages to debug problems when overlay devicetree fragment nodes contain an invalid target path. Improve the messages in find_target_node() to remedy this.

---

changes from v2:
- add fragment node name as suggested by Geert
- existing error message printed short node name, thus not discriminating between fragments; change to full node name
- existing error message printed node address, which is devicetree internal debugging, not useful in an overlay apply error message; remove node address from message

 drivers/of/overlay.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
drivers/of/overlay.c +16 -6
index e3d7f69a8333..b930e05d1215 100644
@@ -488,20 +488,30 @@ static int build_changeset(struct overlay_changeset *ovcs)
  */
 static struct device_node *find_target_node(struct device_node *info_node)
 {
+	struct device_node *node;
 	const char *path;
 	u32 val;
 	int ret;
 
 	ret = of_property_read_u32(info_node, "target", &val);
-	if (!ret)
-		return of_find_node_by_phandle(val);
+	if (!ret) {
+		node = of_find_node_by_phandle(val);
+		if (!node)
+			pr_err("find target, node: %pOF, phandle 0x%x not found\n",
+			       info_node, val);
+		return node;
+	}
 
 	ret = of_property_read_string(info_node, "target-path", &path);
-	if (!ret)
-		return of_find_node_by_path(path);
+	if (!ret) {
+		node =  of_find_node_by_path(path);
+		if (!node)
+			pr_err("find target, node: %pOF, path '%s' not found\n",
+			       info_node, path);
+		return node;
+	}
 
-	pr_err("Failed to find target for node %p (%s)\n",
-		info_node, info_node->name);
+	pr_err("find target, node: %pOF, no target property\n", info_node);
 
 	return NULL;
 }
Signature · 1 line
Frank Rowand <[email protected]>