Skip to contentSkip to conversation

of: change overlay apply input data from unflattened

PATCH Superseded [email protected] 3 participants 12 messages Mar 1, 2018

Subsystems (2)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 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 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 (4):
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 +-
 drivers/of/Kconfig                               |   1 +
 drivers/of/of_private.h                          |   1 +
 drivers/of/overlay.c                             | 129 ++++++++--
 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                            | 294 +++++++++++------------
 include/linux/of.h                               |   7 -
 27 files changed, 575 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 v4 1/4 of: change overlay apply input data from unflattened to FDT

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.

Changes from v3:
- OF_OVERLAY: add select OF_FLATTREE

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

 drivers/of/Kconfig                          |   1 +
 drivers/of/of_private.h                     |   1 +
 drivers/of/overlay.c                        | 107 +++++++++-
 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                       | 294 ++++++++++++++--------------
 include/linux/of.h                          |   7 -
 23 files changed, 552 insertions(+), 389 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/of_private.h +1 -0
index 0c609e7d0334..6e39dce3a979 100644
@@ -77,6 +77,7 @@ static inline void __of_detach_node_sysfs(struct device_node *np) {}
 #endif
 
 #if defined(CONFIG_OF_OVERLAY)
+int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id);
 void of_overlay_mutex_lock(void);
 void of_overlay_mutex_unlock(void);
 #else
drivers/of/overlay.c +98 -9

204 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 +145 -149

575 lines not on this page · raw message

include/linux/of.h +0 -7
index da1ee95241c1..fbcb106fa7c2 100644
@@ -1359,8 +1359,6 @@ 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_remove(int *ovcs_id);
 int of_overlay_remove_all(void);
 
@@ -1369,11 +1367,6 @@ struct of_overlay_notify_data {
 
 #else
 
-static inline int of_overlay_apply(struct device_node *tree, int *ovcs_id)
-{
-	return -ENOTSUPP;
-}
-
 static inline int of_overlay_remove(int *ovcs_id)
 {
 	return -ENOTSUPP;
Signature · 1 line
Frank Rowand <[email protected]>
#8 lore raw

PATCH v4 2/4 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]>
#9 lore raw

PATCH v4 3/4 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]>
#10 lore raw

PATCH v4 4/4 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 5f6c5569e97d..852e566d7744 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]>
#2 lore raw

Re: [PATCH v4 1/4] of: change overlay apply input data from unflattened to FDT

In reply to [email protected]

Hi Frank,

Thank you for the patch.

Quoted 129 lines · On Thursday, 1 March 2018 20:00:53 EET [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.

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.

Changes from v3:
- OF_OVERLAY: add select OF_FLATTREE

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

 drivers/of/Kconfig                          |   1 +
 drivers/of/of_private.h                     |   1 +
 drivers/of/overlay.c                        | 107 +++++++++-
 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                       | 294 ++++++++++++------------
 include/linux/of.h                          |   7 -
 23 files changed, 552 insertions(+), 389 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/of_private.h +0 -0
index 0c609e7d0334..6e39dce3a979 100644
@@ -77,6 +77,7 @@ static inline void __of_detach_node_sysfs(struct

device_node *np) {} #endif

Quoted 2 lines

#if defined(CONFIG_OF_OVERLAY)
+int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id);

As discussed on IRC, could you move this to include/linux/of.h ?

Quoted 80 lines

void of_overlay_mutex_lock(void);
void of_overlay_mutex_unlock(void);
#else

drivers/of/overlay.c +5 -0
index 3397d7642958..5f6c5569e97d 100644
@@ -12,10 +12,12 @@
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
+#include <linux/of_fdt.h>
 #include <linux/string.h>
 #include <linux/ctype.h>
 #include <linux/errno.h>
 #include <linux/slab.h>
+#include <linux/libfdt.h>
 #include <linux/err.h>
 #include <linux/idr.h>
 
@@ -33,7 +35,9 @@ struct fragment {
 
 /**
  * struct overlay_changeset
+ * @id:			changeset identifier
  * @ovcs_list:		list on which we are located
+ * @fdt:		FDT that was unflattened to create @overlay_tree
  * @overlay_tree:	expanded device tree that contains the fragment nodes
  * @count:		count of fragment structures
  * @fragments:		fragment nodes in the overlay expanded device tree
@@ -43,6 +47,7 @@ struct fragment {
 struct overlay_changeset {
 	int id;
 	struct list_head ovcs_list;
+	const void *fdt;
 	struct device_node *overlay_tree;
 	int count;
 	struct fragment *fragments;
@@ -503,7 +508,8 @@ static struct device_node *find_target_node(struct
 device_node *info_node)
 
 /**
  * init_overlay_changeset() - initialize overlay changeset from overlay

tree
- * @ovcs Overlay changeset to build
+ * @ovcs: Overlay changeset to build
+ * @fdt: the FDT that was unflattened to create @tree
* @tree: Contains all the overlay fragments and overlay fixup nodes
*
* Initialize @ovcs. Populate @ovcs->fragments with node information from

drivers/of/overlay.c +0 -0
@@ -514,7 +520,7 @@ static struct device_node *find_target_node(struct
 device_node *info_node) * detected in @tree, or -ENOSPC if idr_alloc()

error.
*/
static int init_overlay_changeset(struct overlay_changeset *ovcs,

drivers/of/overlay.c +3 -1
-		struct device_node *tree)
+		const void *fdt, struct device_node *tree)
 {
 	struct device_node *node, *overlay_node;
 	struct fragment *fragment;
@@ -535,6 +541,7 @@ static int init_overlay_changeset(struct
 overlay_changeset *ovcs, pr_debug("%s() tree is not root\n", __func__);
 
 	ovcs->overlay_tree = tree;
+	ovcs->fdt = fdt;
 
 	INIT_LIST_HEAD(&ovcs->ovcs_list);
 
@@ -606,6 +613,7 @@ static int init_overlay_changeset(struct
 overlay_changeset *ovcs, }
 
 	if (!cnt) {
+		pr_err("no fragments or symbols in overlay\n");
 		ret = -EINVAL;
 		goto err_free_fragments;
 	}
@@ -642,11 +650,24 @@ static void free_overlay_changeset(struct
 overlay_changeset *ovcs) }
 	kfree(ovcs->fragments);
 

drivers/of/overlay.c static void free_overlay_changeset(struct · line 662

Quoted 10 lines
drivers/of/overlay.c +10 -0
+	/*
+	 * 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
+	 */
+

I assume you'll fix it at some point ? :-)

Quoted 78 lines
drivers/of/overlay.c +4 -1
 	kfree(ovcs);
 }
 
-/**
+/*
+ * internal documentation
+ *
  * of_overlay_apply() - Create and apply an overlay changeset
+ * @fdt:	the FDT that was unflattened to create @tree
  * @tree:	Expanded overlay device tree
  * @ovcs_id:	Pointer to overlay changeset id
  *
@@ -685,21 +706,29 @@ static void free_overlay_changeset(struct

overlay_changeset *ovcs) * id is returned to *ovcs_id.
*/

drivers/of/overlay.c +18 -5
-int of_overlay_apply(struct device_node *tree, int *ovcs_id)
+static int of_overlay_apply(const void *fdt, struct device_node *tree,
+		int *ovcs_id)
 {
 	struct overlay_changeset *ovcs;
 	int ret = 0, ret_revert, ret_tmp;
 
-	*ovcs_id = 0;
+	/*
+	 * As of this point, fdt and tree belong to the overlay changeset.
+	 * overlay changeset code is responsible for freeing them.
+	 */
 
 	if (devicetree_corrupt()) {
 		pr_err("devicetree state suspect, refuse to apply overlay\n");
+		kfree(fdt);
+		kfree(tree);
 		ret = -EBUSY;
 		goto out;
 	}
 
 	ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
 	if (!ovcs) {
+		kfree(fdt);
+		kfree(tree);
 		ret = -ENOMEM;
 		goto out;
 	}
@@ -709,12 +738,17 @@ int of_overlay_apply(struct device_node *tree, int
 *ovcs_id)
 
 	ret = of_resolve_phandles(tree);
 	if (ret)
-		goto err_free_overlay_changeset;
+		goto err_free_tree;
 
-	ret = init_overlay_changeset(ovcs, tree);
+	ret = init_overlay_changeset(ovcs, fdt, tree);
 	if (ret)
-		goto err_free_overlay_changeset;
+		goto err_free_tree;
 
+	/*
+	 * after overlay_notify(), ovcs->overlay_tree related pointers may have
+	 * leaked to drivers, so can not kfree() tree, aka ovcs->overlay_tree;
+	 * and can not free fdt, aka ovcs->fdt
+	 */
 	ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
 	if (ret) {
drivers/of/overlay.c +3 -0
 		pr_err("overlay changeset pre-apply notify error %d\n", ret);
@@ -754,6 +788,9 @@ int of_overlay_apply(struct device_node *tree, int
 *ovcs_id)
 
 	goto out_unlock;
 
+err_free_tree:
+	kfree(tree);
+
 err_free_overlay_changeset:
 	free_overlay_changeset(ovcs);

drivers/of/overlay.c int of_overlay_apply(struct device_node *tree, int · line 808

Quoted 7 lines
drivers/of/overlay.c +2 -1
@@ -766,7 +803,59 @@ int of_overlay_apply(struct device_node *tree, int
 *ovcs_id)
 
 	return ret;
 }
-EXPORT_SYMBOL_GPL(of_overlay_apply);
+
+int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id)

Can you make the overlay_fdt pointer const ?

Apart from that the patch looks OK to me.

Quoted 54 lines
drivers/of/overlay.c +51 -0
+{
+	const void *new_fdt;
+	int ret;
+	u32 size;
+	struct device_node *overlay_root;
+
+	*ovcs_id = 0;
+	ret = 0;
+
+	if (fdt_check_header(overlay_fdt)) {
+		pr_err("Invalid overlay_fdt header\n");
+		return -EINVAL;
+	}
+
+	size = fdt_totalsize(overlay_fdt);
+
+	/*
+	 * Must create permanent copy of FDT because of_fdt_unflatten_tree()
+	 * will create pointers to the passed in FDT in the unflattened tree.
+	 */
+	new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
+	if (!new_fdt)
+		return -ENOMEM;
+
+	of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
+	if (!overlay_root) {
+		pr_err("unable to unflatten overlay_fdt\n");
+		ret = -EINVAL;
+		goto out_free_new_fdt;
+	}
+
+	ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
+	if (ret < 0) {
+		/*
+		 * new_fdt and overlay_root now belong to the overlay
+		 * changeset.
+		 * overlay changeset code is responsible for freeing them.
+		 */
+		goto out;
+	}
+
+	return 0;
+
+
+out_free_new_fdt:
+	kfree(new_fdt);
+
+out:
+	return ret;
+}
+EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
 
 /*

* Find @np in @tree.

Signature · 3 lines
Regards,

Laurent Pinchart
#5 lore raw

Re: [PATCH v4 1/4] of: change overlay apply input data from unflattened to FDT

In reply to [email protected]
Quoted 248 lines · On Thu, Mar 1, 2018 at 12:00 PM, <[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.

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.

Changes from v3:
- OF_OVERLAY: add select OF_FLATTREE

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

 drivers/of/Kconfig                          |   1 +
 drivers/of/of_private.h                     |   1 +
 drivers/of/overlay.c                        | 107 +++++++++-
 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                       | 294 ++++++++++++++--------------
 include/linux/of.h                          |   7 -
 23 files changed, 552 insertions(+), 389 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/of_private.h +1 -0
index 0c609e7d0334..6e39dce3a979 100644
@@ -77,6 +77,7 @@ static inline void __of_detach_node_sysfs(struct device_node *np) {}
 #endif
 
 #if defined(CONFIG_OF_OVERLAY)
+int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id);
 void of_overlay_mutex_lock(void);
 void of_overlay_mutex_unlock(void);
 #else
drivers/of/overlay.c +30 -5
index 3397d7642958..5f6c5569e97d 100644
@@ -12,10 +12,12 @@
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
+#include <linux/of_fdt.h>
 #include <linux/string.h>
 #include <linux/ctype.h>
 #include <linux/errno.h>
 #include <linux/slab.h>
+#include <linux/libfdt.h>
 #include <linux/err.h>
 #include <linux/idr.h>
 
@@ -33,7 +35,9 @@ struct fragment {
 
 /**
  * struct overlay_changeset
+ * @id:                        changeset identifier
  * @ovcs_list:         list on which we are located
+ * @fdt:               FDT that was unflattened to create @overlay_tree
  * @overlay_tree:      expanded device tree that contains the fragment nodes
  * @count:             count of fragment structures
  * @fragments:         fragment nodes in the overlay expanded device tree
@@ -43,6 +47,7 @@ struct fragment {
 struct overlay_changeset {
        int id;
        struct list_head ovcs_list;
+       const void *fdt;
        struct device_node *overlay_tree;
        int count;
        struct fragment *fragments;
@@ -503,7 +508,8 @@ static struct device_node *find_target_node(struct device_node *info_node)
 
 /**
  * init_overlay_changeset() - initialize overlay changeset from overlay tree
- * @ovcs       Overlay changeset to build
+ * @ovcs:      Overlay changeset to build
+ * @fdt:       the FDT that was unflattened to create @tree
  * @tree:      Contains all the overlay fragments and overlay fixup nodes
  *
  * Initialize @ovcs.  Populate @ovcs->fragments with node information from
@@ -514,7 +520,7 @@ static struct device_node *find_target_node(struct device_node *info_node)
  * detected in @tree, or -ENOSPC if idr_alloc() error.
  */
 static int init_overlay_changeset(struct overlay_changeset *ovcs,
-               struct device_node *tree)
+               const void *fdt, struct device_node *tree)
 {
        struct device_node *node, *overlay_node;
        struct fragment *fragment;
@@ -535,6 +541,7 @@ static int init_overlay_changeset(struct overlay_changeset *ovcs,
                pr_debug("%s() tree is not root\n", __func__);
 
        ovcs->overlay_tree = tree;
+       ovcs->fdt = fdt;
 
        INIT_LIST_HEAD(&ovcs->ovcs_list);
 
@@ -606,6 +613,7 @@ static int init_overlay_changeset(struct overlay_changeset *ovcs,
        }
 
        if (!cnt) {
+               pr_err("no fragments or symbols in overlay\n");
                ret = -EINVAL;
                goto err_free_fragments;
        }
@@ -642,11 +650,24 @@ static void free_overlay_changeset(struct overlay_changeset *ovcs)
        }
        kfree(ovcs->fragments);
 
+       /*
+        * 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);
 }
 
-/**
+/*
+ * internal documentation
+ *
  * of_overlay_apply() - Create and apply an overlay changeset
+ * @fdt:       the FDT that was unflattened to create @tree
  * @tree:      Expanded overlay device tree
  * @ovcs_id:   Pointer to overlay changeset id
  *
@@ -685,21 +706,29 @@ static void free_overlay_changeset(struct overlay_changeset *ovcs)
  * id is returned to *ovcs_id.
  */
 
-int of_overlay_apply(struct device_node *tree, int *ovcs_id)
+static int of_overlay_apply(const void *fdt, struct device_node *tree,
+               int *ovcs_id)
 {
        struct overlay_changeset *ovcs;
        int ret = 0, ret_revert, ret_tmp;
 
-       *ovcs_id = 0;
+       /*
+        * As of this point, fdt and tree belong to the overlay changeset.
+        * overlay changeset code is responsible for freeing them.
+        */
 
        if (devicetree_corrupt()) {

drivers/of/overlay.c static void free_overlay_changeset(struct overlay_changeset *ovcs) · line 730

Quoted 10 lines
drivers/of/overlay.c +3 -0
                pr_err("devicetree state suspect, refuse to apply overlay\n");
+               kfree(fdt);
+               kfree(tree);
                ret = -EBUSY;
                goto out;
        }
 
        ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
        if (!ovcs) {
+               kfree(fdt);

You free the fdt up here, but ...

Quoted 26 lines
drivers/of/overlay.c +9 -3
+               kfree(tree);
                ret = -ENOMEM;
                goto out;
        }
@@ -709,12 +738,17 @@ int of_overlay_apply(struct device_node *tree, int *ovcs_id)
 
        ret = of_resolve_phandles(tree);
        if (ret)
-               goto err_free_overlay_changeset;
+               goto err_free_tree;
 
-       ret = init_overlay_changeset(ovcs, tree);
+       ret = init_overlay_changeset(ovcs, fdt, tree);
        if (ret)
-               goto err_free_overlay_changeset;
+               goto err_free_tree;
 
+       /*
+        * after overlay_notify(), ovcs->overlay_tree related pointers may have
+        * leaked to drivers, so can not kfree() tree, aka ovcs->overlay_tree;
+        * and can not free fdt, aka ovcs->fdt
+        */
        ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
        if (ret) {
                pr_err("overlay changeset pre-apply notify error %d\n", ret);

drivers/of/overlay.c int of_overlay_apply(struct device_node *tree, int *ovcs_id) · line 793

Quoted 6 lines
drivers/of/overlay.c +3 -0
@@ -754,6 +788,9 @@ int of_overlay_apply(struct device_node *tree, int *ovcs_id)
 
        goto out_unlock;
 
+err_free_tree:
+       kfree(tree);
+

...not down here?

Seems like of_overlay_fdt_apply should do the freeing as that is what does the allocation of the fdt.

Quoted 61 lines
drivers/of/overlay.c +53 -1
 err_free_overlay_changeset:
        free_overlay_changeset(ovcs);
 
@@ -766,7 +803,59 @@ int of_overlay_apply(struct device_node *tree, int *ovcs_id)
 
        return ret;
 }
-EXPORT_SYMBOL_GPL(of_overlay_apply);
+
+int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id)
+{
+       const void *new_fdt;
+       int ret;
+       u32 size;
+       struct device_node *overlay_root;
+
+       *ovcs_id = 0;
+       ret = 0;
+
+       if (fdt_check_header(overlay_fdt)) {
+               pr_err("Invalid overlay_fdt header\n");
+               return -EINVAL;
+       }
+
+       size = fdt_totalsize(overlay_fdt);
+
+       /*
+        * Must create permanent copy of FDT because of_fdt_unflatten_tree()
+        * will create pointers to the passed in FDT in the unflattened tree.
+        */
+       new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
+       if (!new_fdt)
+               return -ENOMEM;
+
+       of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
+       if (!overlay_root) {
+               pr_err("unable to unflatten overlay_fdt\n");
+               ret = -EINVAL;
+               goto out_free_new_fdt;
+       }
+
+       ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
+       if (ret < 0) {
+               /*
+                * new_fdt and overlay_root now belong to the overlay
+                * changeset.
+                * overlay changeset code is responsible for freeing them.
+                */
+               goto out;
+       }
+
+       return 0;
+
+
+out_free_new_fdt:
+       kfree(new_fdt);
+
+out:
+       return ret;
+}
+EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);

[...]

Quoted 14 lines
drivers/of/unittest.c +2 -0
index 7a9abaae874d..2d706039ac96 100644
@@ -45,6 +45,8 @@
        failed; \
 })
 
+static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
+
 static void __init of_unittest_find_node_by_name(void)
 {
        struct device_node *np;

drivers/of/unittest.c static int __init unittest_data_add(void) · line 1002

Quoted 6 lines
drivers/of/unittest.c +1 -2
@@ -997,8 +999,7 @@ static int __init unittest_data_add(void)
        }
 
        /*
-        * This lock normally encloses of_overlay_apply() as well as
-        * of_resolve_phandles().
+        * This lock normally encloses of_resolve_phandles()

I thought this lock was going to be internal when we did this change.

Quoted 19 lines
drivers/of/unittest.c +2 -2
         */
        of_overlay_mutex_lock();
 
@@ -1191,12 +1192,12 @@ static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
        return 0;
 }
 
-static const char *overlay_path(int nr)
+static const char *overlay_name_from_nr(int nr)
 {
        static char buf[256];
 
        snprintf(buf, sizeof(buf) - 1,
-               "/testcase-data/overlay%d", nr);
+               "overlay_%d", nr);
        buf[sizeof(buf) - 1] = '\0';
 
        return buf;

drivers/of/unittest.c static void of_unittest_destroy_tracked_overlays(void) · line 1268

Quoted 6 lines
drivers/of/unittest.c +1 -1
@@ -1263,25 +1264,19 @@ static void of_unittest_destroy_tracked_overlays(void)
        } while (defers > 0);
 }
 
-static int of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
+static int __init of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
                int *overlay_id)

Why __init? Really, we want to move towards building the unittests as a module and we don't want __init for that. Though maybe __init is a nop for modules, I don't remember offhand.

In any case, seems like a separate change.

Rob

Tested-by on the v4 series
#11 lore raw
In reply to [email protected]

Hi Frank,

Thank you for the patches.

For the whole series,

Quoted 84 lines · On Thursday, 1 March 2018 20:00:52 EET [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.

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 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 (4):
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 +-
 drivers/of/Kconfig                               |   1 +
 drivers/of/of_private.h                          |   1 +
 drivers/of/overlay.c                             | 129 ++++++++--
 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                            | 294 ++++++++++----------
 include/linux/of.h                               |   7 -
 27 files changed, 575 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 · 3 lines
Regards,

Laurent Pinchart
#3 lore raw

Re: [PATCH v4 1/4] of: change overlay apply input data from unflattened to FDT

In reply to Laurent Pinchart

Hi Frank,

Another comment, please see below.

Quoted 110 lines · On Thursday, 1 March 2018 22:59:08 EET Laurent Pinchart wrote:
Quoted 109 lines · On Thursday, 1 March 2018 20:00:53 EET [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.

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.

Changes from v3:
- OF_OVERLAY: add select OF_FLATTREE

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

 drivers/of/Kconfig                          |   1 +
 drivers/of/of_private.h                     |   1 +
 drivers/of/overlay.c                        | 107 +++++++++-
 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                       | 294 +++++++++++------------
 include/linux/of.h                          |   7 -
 23 files changed, 552 insertions(+), 389 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

[snip]

Quoted 4 lines
Quoted 4 lines
drivers/of/overlay.c +0 -0
index 3397d7642958..5f6c5569e97d 100644

[snip]

Quoted 10 lines

drivers/of/overlay.c int of_overlay_apply(struct device_node *tree, int · line 810

Quoted 10 lines
drivers/of/overlay.c +2 -1
@@ -766,7 +803,59 @@ int of_overlay_apply(struct device_node *tree, int
 *ovcs_id)
 
 	return ret;
 
 }
 
-EXPORT_SYMBOL_GPL(of_overlay_apply);
+
+int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id)

I find it a bit dangerous that this function doesn't take the size of the FDT blob. I don't think we will always be able to trust the FDT blob. I'm fine fixing this in follow-up patches, I don't think it's a blocker as the only user we have today passes an FDT compiled in the kernel sources.

Quoted 54 lines
Quoted 54 lines
drivers/of/overlay.c +51 -0
+{
+	const void *new_fdt;
+	int ret;
+	u32 size;
+	struct device_node *overlay_root;
+
+	*ovcs_id = 0;
+	ret = 0;
+
+	if (fdt_check_header(overlay_fdt)) {
+		pr_err("Invalid overlay_fdt header\n");
+		return -EINVAL;
+	}
+
+	size = fdt_totalsize(overlay_fdt);
+
+	/*
+	 * Must create permanent copy of FDT because of_fdt_unflatten_tree()
+	 * will create pointers to the passed in FDT in the unflattened tree.
+	 */
+	new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
+	if (!new_fdt)
+		return -ENOMEM;
+
+	of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
+	if (!overlay_root) {
+		pr_err("unable to unflatten overlay_fdt\n");
+		ret = -EINVAL;
+		goto out_free_new_fdt;
+	}
+
+	ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
+	if (ret < 0) {
+		/*
+		 * new_fdt and overlay_root now belong to the overlay
+		 * changeset.
+		 * overlay changeset code is responsible for freeing them.
+		 */
+		goto out;
+	}
+
+	return 0;
+
+
+out_free_new_fdt:
+	kfree(new_fdt);
+
+out:
+	return ret;
+}
+EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);

/*
* Find @np in @tree.

Signature · 3 lines
Regards,

Laurent Pinchart
#4 lore raw

Re: [PATCH v4 1/4] of: change overlay apply input data from unflattened to FDT

In reply to Laurent Pinchart
Quoted 134 lines · Laurent Pinchart

Hi Frank,

Thank you for the patch.

Quoted 129 lines · On Thursday, 1 March 2018 20:00:53 EET [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.

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.

Changes from v3:
- OF_OVERLAY: add select OF_FLATTREE

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

 drivers/of/Kconfig                          |   1 +
 drivers/of/of_private.h                     |   1 +
 drivers/of/overlay.c                        | 107 +++++++++-
 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                       | 294 ++++++++++++------------
 include/linux/of.h                          |   7 -
 23 files changed, 552 insertions(+), 389 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/of_private.h +0 -0
index 0c609e7d0334..6e39dce3a979 100644
@@ -77,6 +77,7 @@ static inline void __of_detach_node_sysfs(struct

device_node *np) {} #endif

Quoted 4 lines
Quoted 2 lines

#if defined(CONFIG_OF_OVERLAY)
+int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id);

As discussed on IRC, could you move this to include/linux/of.h ?

Yes, got it.

Quoted 81 lines
Quoted 81 lines

void of_overlay_mutex_lock(void);
void of_overlay_mutex_unlock(void);
#else

drivers/of/overlay.c +5 -0
index 3397d7642958..5f6c5569e97d 100644
@@ -12,10 +12,12 @@
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
+#include <linux/of_fdt.h>
 #include <linux/string.h>
 #include <linux/ctype.h>
 #include <linux/errno.h>
 #include <linux/slab.h>
+#include <linux/libfdt.h>
 #include <linux/err.h>
 #include <linux/idr.h>
 
@@ -33,7 +35,9 @@ struct fragment {
 
 /**
  * struct overlay_changeset
+ * @id:			changeset identifier
  * @ovcs_list:		list on which we are located
+ * @fdt:		FDT that was unflattened to create @overlay_tree
  * @overlay_tree:	expanded device tree that contains the fragment nodes
  * @count:		count of fragment structures
  * @fragments:		fragment nodes in the overlay expanded device tree
@@ -43,6 +47,7 @@ struct fragment {
 struct overlay_changeset {
 	int id;
 	struct list_head ovcs_list;
+	const void *fdt;
 	struct device_node *overlay_tree;
 	int count;
 	struct fragment *fragments;
@@ -503,7 +508,8 @@ static struct device_node *find_target_node(struct
 device_node *info_node)
 
 /**
  * init_overlay_changeset() - initialize overlay changeset from overlay

tree
- * @ovcs Overlay changeset to build
+ * @ovcs: Overlay changeset to build
+ * @fdt: the FDT that was unflattened to create @tree
* @tree: Contains all the overlay fragments and overlay fixup nodes
*
* Initialize @ovcs. Populate @ovcs->fragments with node information from

drivers/of/overlay.c +0 -0
@@ -514,7 +520,7 @@ static struct device_node *find_target_node(struct
 device_node *info_node) * detected in @tree, or -ENOSPC if idr_alloc()

error.
*/
static int init_overlay_changeset(struct overlay_changeset *ovcs,

drivers/of/overlay.c +3 -1
-		struct device_node *tree)
+		const void *fdt, struct device_node *tree)
 {
 	struct device_node *node, *overlay_node;
 	struct fragment *fragment;
@@ -535,6 +541,7 @@ static int init_overlay_changeset(struct
 overlay_changeset *ovcs, pr_debug("%s() tree is not root\n", __func__);
 
 	ovcs->overlay_tree = tree;
+	ovcs->fdt = fdt;
 
 	INIT_LIST_HEAD(&ovcs->ovcs_list);
 
@@ -606,6 +613,7 @@ static int init_overlay_changeset(struct
 overlay_changeset *ovcs, }
 
 	if (!cnt) {
+		pr_err("no fragments or symbols in overlay\n");
 		ret = -EINVAL;
 		goto err_free_fragments;
 	}
@@ -642,11 +650,24 @@ static void free_overlay_changeset(struct
 overlay_changeset *ovcs) }
 	kfree(ovcs->fragments);
 
Quoted 12 lines

drivers/of/overlay.c static void free_overlay_changeset(struct overlay_changeset *ovcs) · line 662

Quoted 10 lines
drivers/of/overlay.c +10 -0
+	/*
+	 * 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
+	 */
+

I assume you'll fix it at some point ? :-)

Long term project. It's not easy. But that is my intent.

Quoted 80 lines
Quoted 80 lines
drivers/of/overlay.c +4 -1
 	kfree(ovcs);
 }
 
-/**
+/*
+ * internal documentation
+ *
  * of_overlay_apply() - Create and apply an overlay changeset
+ * @fdt:	the FDT that was unflattened to create @tree
  * @tree:	Expanded overlay device tree
  * @ovcs_id:	Pointer to overlay changeset id
  *
@@ -685,21 +706,29 @@ static void free_overlay_changeset(struct

overlay_changeset *ovcs) * id is returned to *ovcs_id.
*/

drivers/of/overlay.c +18 -5
-int of_overlay_apply(struct device_node *tree, int *ovcs_id)
+static int of_overlay_apply(const void *fdt, struct device_node *tree,
+		int *ovcs_id)
 {
 	struct overlay_changeset *ovcs;
 	int ret = 0, ret_revert, ret_tmp;
 
-	*ovcs_id = 0;
+	/*
+	 * As of this point, fdt and tree belong to the overlay changeset.
+	 * overlay changeset code is responsible for freeing them.
+	 */
 
 	if (devicetree_corrupt()) {
 		pr_err("devicetree state suspect, refuse to apply overlay\n");
+		kfree(fdt);
+		kfree(tree);
 		ret = -EBUSY;
 		goto out;
 	}
 
 	ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
 	if (!ovcs) {
+		kfree(fdt);
+		kfree(tree);
 		ret = -ENOMEM;
 		goto out;
 	}
@@ -709,12 +738,17 @@ int of_overlay_apply(struct device_node *tree, int
 *ovcs_id)
 
 	ret = of_resolve_phandles(tree);
 	if (ret)
-		goto err_free_overlay_changeset;
+		goto err_free_tree;
 
-	ret = init_overlay_changeset(ovcs, tree);
+	ret = init_overlay_changeset(ovcs, fdt, tree);
 	if (ret)
-		goto err_free_overlay_changeset;
+		goto err_free_tree;
 
+	/*
+	 * after overlay_notify(), ovcs->overlay_tree related pointers may have
+	 * leaked to drivers, so can not kfree() tree, aka ovcs->overlay_tree;
+	 * and can not free fdt, aka ovcs->fdt
+	 */
 	ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
 	if (ret) {
drivers/of/overlay.c +3 -0
 		pr_err("overlay changeset pre-apply notify error %d\n", ret);
@@ -754,6 +788,9 @@ int of_overlay_apply(struct device_node *tree, int
 *ovcs_id)
 
 	goto out_unlock;
 
+err_free_tree:
+	kfree(tree);
+
 err_free_overlay_changeset:
 	free_overlay_changeset(ovcs);
@@ -766,7 +803,59 @@ int of_overlay_apply(struct device_node *tree, int
 *ovcs_id)
 
Quoted 7 lines

drivers/of/overlay.c int of_overlay_apply(struct device_node *tree, int *ovcs_id) · line 807

Quoted 5 lines
drivers/of/overlay.c +2 -1
 	return ret;
 }
-EXPORT_SYMBOL_GPL(of_overlay_apply);
+
+int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id)

Can you make the overlay_fdt pointer const ?

It looks like I should be able to.

Quoted 57 lines

Apart from that the patch looks OK to me.

Quoted 54 lines
drivers/of/overlay.c +51 -0
+{
+	const void *new_fdt;
+	int ret;
+	u32 size;
+	struct device_node *overlay_root;
+
+	*ovcs_id = 0;
+	ret = 0;
+
+	if (fdt_check_header(overlay_fdt)) {
+		pr_err("Invalid overlay_fdt header\n");
+		return -EINVAL;
+	}
+
+	size = fdt_totalsize(overlay_fdt);
+
+	/*
+	 * Must create permanent copy of FDT because of_fdt_unflatten_tree()
+	 * will create pointers to the passed in FDT in the unflattened tree.
+	 */
+	new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
+	if (!new_fdt)
+		return -ENOMEM;
+
+	of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
+	if (!overlay_root) {
+		pr_err("unable to unflatten overlay_fdt\n");
+		ret = -EINVAL;
+		goto out_free_new_fdt;
+	}
+
+	ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
+	if (ret < 0) {
+		/*
+		 * new_fdt and overlay_root now belong to the overlay
+		 * changeset.
+		 * overlay changeset code is responsible for freeing them.
+		 */
+		goto out;
+	}
+
+	return 0;
+
+
+out_free_new_fdt:
+	kfree(new_fdt);
+
+out:
+	return ret;
+}
+EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
 
 /*

* Find @np in @tree.

#6 lore raw

Re: [PATCH v4 1/4] of: change overlay apply input data from unflattened to FDT

In reply to Rob Herring
Quoted 291 lines · Rob Herring
Quoted 248 lines · On Thu, Mar 1, 2018 at 12:00 PM, <[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.

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.

Changes from v3:
- OF_OVERLAY: add select OF_FLATTREE

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

 drivers/of/Kconfig                          |   1 +
 drivers/of/of_private.h                     |   1 +
 drivers/of/overlay.c                        | 107 +++++++++-
 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                       | 294 ++++++++++++++--------------
 include/linux/of.h                          |   7 -
 23 files changed, 552 insertions(+), 389 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/of_private.h +1 -0
index 0c609e7d0334..6e39dce3a979 100644
@@ -77,6 +77,7 @@ static inline void __of_detach_node_sysfs(struct device_node *np) {}
 #endif
 
 #if defined(CONFIG_OF_OVERLAY)
+int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id);
 void of_overlay_mutex_lock(void);
 void of_overlay_mutex_unlock(void);
 #else
drivers/of/overlay.c +30 -5
index 3397d7642958..5f6c5569e97d 100644
@@ -12,10 +12,12 @@
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
+#include <linux/of_fdt.h>
 #include <linux/string.h>
 #include <linux/ctype.h>
 #include <linux/errno.h>
 #include <linux/slab.h>
+#include <linux/libfdt.h>
 #include <linux/err.h>
 #include <linux/idr.h>
 
@@ -33,7 +35,9 @@ struct fragment {
 
 /**
  * struct overlay_changeset
+ * @id:                        changeset identifier
  * @ovcs_list:         list on which we are located
+ * @fdt:               FDT that was unflattened to create @overlay_tree
  * @overlay_tree:      expanded device tree that contains the fragment nodes
  * @count:             count of fragment structures
  * @fragments:         fragment nodes in the overlay expanded device tree
@@ -43,6 +47,7 @@ struct fragment {
 struct overlay_changeset {
        int id;
        struct list_head ovcs_list;
+       const void *fdt;
        struct device_node *overlay_tree;
        int count;
        struct fragment *fragments;
@@ -503,7 +508,8 @@ static struct device_node *find_target_node(struct device_node *info_node)
 
 /**
  * init_overlay_changeset() - initialize overlay changeset from overlay tree
- * @ovcs       Overlay changeset to build
+ * @ovcs:      Overlay changeset to build
+ * @fdt:       the FDT that was unflattened to create @tree
  * @tree:      Contains all the overlay fragments and overlay fixup nodes
  *
  * Initialize @ovcs.  Populate @ovcs->fragments with node information from
@@ -514,7 +520,7 @@ static struct device_node *find_target_node(struct device_node *info_node)
  * detected in @tree, or -ENOSPC if idr_alloc() error.
  */
 static int init_overlay_changeset(struct overlay_changeset *ovcs,
-               struct device_node *tree)
+               const void *fdt, struct device_node *tree)
 {
        struct device_node *node, *overlay_node;
        struct fragment *fragment;
@@ -535,6 +541,7 @@ static int init_overlay_changeset(struct overlay_changeset *ovcs,
                pr_debug("%s() tree is not root\n", __func__);
 
        ovcs->overlay_tree = tree;
+       ovcs->fdt = fdt;
 
        INIT_LIST_HEAD(&ovcs->ovcs_list);
 
@@ -606,6 +613,7 @@ static int init_overlay_changeset(struct overlay_changeset *ovcs,
        }
 
        if (!cnt) {
+               pr_err("no fragments or symbols in overlay\n");
                ret = -EINVAL;
                goto err_free_fragments;
        }
@@ -642,11 +650,24 @@ static void free_overlay_changeset(struct overlay_changeset *ovcs)
        }
        kfree(ovcs->fragments);
 
+       /*
+        * 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);
 }
 
-/**
+/*
+ * internal documentation
+ *
  * of_overlay_apply() - Create and apply an overlay changeset
+ * @fdt:       the FDT that was unflattened to create @tree
  * @tree:      Expanded overlay device tree
  * @ovcs_id:   Pointer to overlay changeset id
  *
@@ -685,21 +706,29 @@ static void free_overlay_changeset(struct overlay_changeset *ovcs)
  * id is returned to *ovcs_id.
  */
 
-int of_overlay_apply(struct device_node *tree, int *ovcs_id)
+static int of_overlay_apply(const void *fdt, struct device_node *tree,
+               int *ovcs_id)
 {
        struct overlay_changeset *ovcs;
        int ret = 0, ret_revert, ret_tmp;
 
-       *ovcs_id = 0;
+       /*
+        * As of this point, fdt and tree belong to the overlay changeset.
+        * overlay changeset code is responsible for freeing them.
+        */
 
        if (devicetree_corrupt()) {

drivers/of/overlay.c static void free_overlay_changeset(struct overlay_changeset *ovcs) · line 730

Quoted 10 lines
drivers/of/overlay.c +3 -0
                pr_err("devicetree state suspect, refuse to apply overlay\n");
+               kfree(fdt);
+               kfree(tree);
                ret = -EBUSY;
                goto out;
        }
 
        ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
        if (!ovcs) {
+               kfree(fdt);

You free the fdt up here, but ...

Quoted 29 lines
drivers/of/overlay.c +9 -3
+               kfree(tree);
                ret = -ENOMEM;
                goto out;
        }
@@ -709,12 +738,17 @@ int of_overlay_apply(struct device_node *tree, int *ovcs_id)
 
        ret = of_resolve_phandles(tree);
        if (ret)
-               goto err_free_overlay_changeset;
+               goto err_free_tree;
 
-       ret = init_overlay_changeset(ovcs, tree);
+       ret = init_overlay_changeset(ovcs, fdt, tree);
        if (ret)
-               goto err_free_overlay_changeset;
+               goto err_free_tree;
 
+       /*
+        * after overlay_notify(), ovcs->overlay_tree related pointers may have
+        * leaked to drivers, so can not kfree() tree, aka ovcs->overlay_tree;
+        * and can not free fdt, aka ovcs->fdt
+        */
        ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
        if (ret) {
                pr_err("overlay changeset pre-apply notify error %d\n", ret);
@@ -754,6 +788,9 @@ int of_overlay_apply(struct device_node *tree, int *ovcs_id)
 
        goto out_unlock;
 
Quoted 8 lines

drivers/of/overlay.c int of_overlay_apply(struct device_node *tree, int *ovcs_id) · line 793

Quoted 3 lines
drivers/of/overlay.c +3 -0
+err_free_tree:
+       kfree(tree);
+

...not down here?

Seems like of_overlay_fdt_apply should do the freeing as that is what does the allocation of the fdt.

Yes, fdt should be freed here also.

Quoted 80 lines
Quoted 61 lines
drivers/of/overlay.c +53 -1
 err_free_overlay_changeset:
        free_overlay_changeset(ovcs);
 
@@ -766,7 +803,59 @@ int of_overlay_apply(struct device_node *tree, int *ovcs_id)
 
        return ret;
 }
-EXPORT_SYMBOL_GPL(of_overlay_apply);
+
+int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id)
+{
+       const void *new_fdt;
+       int ret;
+       u32 size;
+       struct device_node *overlay_root;
+
+       *ovcs_id = 0;
+       ret = 0;
+
+       if (fdt_check_header(overlay_fdt)) {
+               pr_err("Invalid overlay_fdt header\n");
+               return -EINVAL;
+       }
+
+       size = fdt_totalsize(overlay_fdt);
+
+       /*
+        * Must create permanent copy of FDT because of_fdt_unflatten_tree()
+        * will create pointers to the passed in FDT in the unflattened tree.
+        */
+       new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
+       if (!new_fdt)
+               return -ENOMEM;
+
+       of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
+       if (!overlay_root) {
+               pr_err("unable to unflatten overlay_fdt\n");
+               ret = -EINVAL;
+               goto out_free_new_fdt;
+       }
+
+       ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
+       if (ret < 0) {
+               /*
+                * new_fdt and overlay_root now belong to the overlay
+                * changeset.
+                * overlay changeset code is responsible for freeing them.
+                */
+               goto out;
+       }
+
+       return 0;
+
+
+out_free_new_fdt:
+       kfree(new_fdt);
+
+out:
+       return ret;
+}
+EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);

[...]

Quoted 16 lines
drivers/of/unittest.c +2 -0
index 7a9abaae874d..2d706039ac96 100644
@@ -45,6 +45,8 @@
        failed; \
 })
 
+static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
+
 static void __init of_unittest_find_node_by_name(void)
 {
        struct device_node *np;
@@ -997,8 +999,7 @@ static int __init unittest_data_add(void)
        }
 
Quoted 6 lines

drivers/of/unittest.c static int __init unittest_data_add(void) · line 1002

Quoted 4 lines
drivers/of/unittest.c +1 -2
        /*
-        * This lock normally encloses of_overlay_apply() as well as
-        * of_resolve_phandles().
+        * This lock normally encloses of_resolve_phandles()

I thought this lock was going to be internal when we did this change.

Not quite there yet. I managed to pull all of the overlay fragments out of tests-overlay.dtsi, but the overlay fragments still have references back into testcases.dtb. unittest_data_add() is fixing up the phandle values in testcases.dtb (even though it is not an overlay) and then splicing testcases.dtb into the live tree.

Modifying the overlays to not refer back into testcases.dtb is yet another largish project.

Quoted 22 lines
Quoted 22 lines
drivers/of/unittest.c +2 -2
         */
        of_overlay_mutex_lock();
 
@@ -1191,12 +1192,12 @@ static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
        return 0;
 }
 
-static const char *overlay_path(int nr)
+static const char *overlay_name_from_nr(int nr)
 {
        static char buf[256];
 
        snprintf(buf, sizeof(buf) - 1,
-               "/testcase-data/overlay%d", nr);
+               "overlay_%d", nr);
        buf[sizeof(buf) - 1] = '\0';
 
        return buf;
@@ -1263,25 +1264,19 @@ static void of_unittest_destroy_tracked_overlays(void)
        } while (defers > 0);
 }
 
Quoted 7 lines

drivers/of/unittest.c static void of_unittest_destroy_tracked_overlays(void) · line 1268

Quoted 3 lines
drivers/of/unittest.c +1 -1
-static int of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
+static int __init of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
                int *overlay_id)

Why __init? Really, we want to move towards building the unittests as a module and we don't want __init for that. Though maybe __init is a nop for modules, I don't remember offhand.

It has to be a nop for modules.

__init makes sense when the unittests are an initcall.

Quoted 1 line

In any case, seems like a separate change.

Correct. It was a gratuitous clean up when I was making other changes to the function. I'll remove it from this series.

There is a mix of some functions being marked __init and some not, so that will be worth another patch someday.

Quoted 3 lines

Rob

#7 lore raw

Re: [PATCH v4 1/4] of: change overlay apply input data from unflattened to FDT

Quoted 61 lines · Frank Rowand
Quoted 60 lines · Rob Herring
Quoted 59 lines · On Thu, Mar 1, 2018 at 12:00 PM, <[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.

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.

---

< snip >

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/of/unittest.c +0 -0
index 7a9abaae874d..2d706039ac96 100644

< snip >

Quoted 14 lines
Quoted 11 lines

drivers/of/unittest.c static void of_unittest_destroy_tracked_overlays(void) · line 1268

Quoted 7 lines
drivers/of/unittest.c +1 -1
@@ -1263,25 +1264,19 @@ static void of_unittest_destroy_tracked_overlays(void)
        } while (defers > 0);
 }
 
-static int of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
+static int __init of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
                int *overlay_id)

Why __init? Really, we want to move towards building the unittests as a module and we don't want __init for that. Though maybe __init is a nop for modules, I don't remember offhand.

It has to be a nop for modules.

Quoted 7 lines

__init makes sense when the unittests are an initcall.

Quoted 1 line

In any case, seems like a separate change.

Correct. It was a gratuitous clean up when I was making other changes to the function. I'll remove it from this series.

Now I remember, after trying to remove the __init. I get a modpost warning when I build the kernel.

Before the patches, the old overlay tests did not apply the overlays from overlay FDTs. Instead the overlay fragment nodes were hand coded into the testcases dts, and the overlay apply code just grabbed the fragment nodes from the live testcases tree and called the overlay code to apply the fragment nodes.

With the new FDT based overlay API, I was able to make the test overlays actual overlay dts files and dtbs. This change involved changing of_unittest_apply_overlay() to no longer call of_overlay_apply() (which passed in a pointer to a node from the live testcases tree), but instead call overlay_data_apply() (which handles finding the requested fdt and applies it). overlay_data_apply() is already an __init() function.

There are a few more functions that I would expect modpost to warn about, but it does not.

Quoted 9 lines

There is a mix of some functions being marked __init and some not, so that will be worth another patch someday.

Quoted 3 lines

Rob