[PATCH v2 3/7] of: resolver: Add export_symbols in of_resolve_phandles() parameters

Herve Codina posted 7 patches 7 months, 3 weeks ago
[PATCH v2 3/7] of: resolver: Add export_symbols in of_resolve_phandles() parameters
Posted by Herve Codina 7 months, 3 weeks ago
In order to prepare the introduction of the export symbols node
handling, add a export_symbols parameter in of_resolve_phandles().

The export_symbols is the export symbols device tree node the resolver
will use for the overlay symbols resolution.

Signed-off-by: Herve Codina <herve.codina@bootlin.com>
Tested-by: Ayush Singh <ayush@beagleboard.org>
---
 drivers/of/of_private.h | 2 +-
 drivers/of/overlay.c    | 2 +-
 drivers/of/resolver.c   | 9 +++++++--
 drivers/of/unittest.c   | 4 ++--
 4 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/drivers/of/of_private.h b/drivers/of/of_private.h
index df0bb00349e0..cf68edb873ab 100644
--- a/drivers/of/of_private.h
+++ b/drivers/of/of_private.h
@@ -94,7 +94,7 @@ static inline void __of_detach_node_sysfs(struct device_node *np) {}
 #endif
 
 #if defined(CONFIG_OF_RESOLVE)
-int of_resolve_phandles(struct device_node *tree);
+int of_resolve_phandles(struct device_node *tree, const struct device_node *export_symbols);
 #endif
 
 void __of_phandle_cache_inv_entry(phandle handle);
diff --git a/drivers/of/overlay.c b/drivers/of/overlay.c
index 1af6f52d0708..aa1b97e634aa 100644
--- a/drivers/of/overlay.c
+++ b/drivers/of/overlay.c
@@ -919,7 +919,7 @@ static int of_overlay_apply(struct overlay_changeset *ovcs,
 {
 	int ret = 0, ret_revert, ret_tmp;
 
-	ret = of_resolve_phandles(ovcs->overlay_root);
+	ret = of_resolve_phandles(ovcs->overlay_root, NULL);
 	if (ret)
 		goto out;
 
diff --git a/drivers/of/resolver.c b/drivers/of/resolver.c
index 2c7a3b479b15..5c492711b21f 100644
--- a/drivers/of/resolver.c
+++ b/drivers/of/resolver.c
@@ -237,7 +237,8 @@ static int get_phandle_from_symbols_node(const struct device_node *tree_symbols,
 /**
  * of_resolve_phandles - Relocate and resolve overlay against live tree
  *
- * @overlay:	Pointer to devicetree overlay to relocate and resolve
+ * @overlay:		Pointer to devicetree overlay to relocate and resolve
+ * @export_symbols:	Pointer to devicetree export symbols node.
  *
  * Modify (relocate) values of local phandles in @overlay to a range that
  * does not conflict with the live expanded devicetree.  Update references
@@ -257,6 +258,10 @@ static int get_phandle_from_symbols_node(const struct device_node *tree_symbols,
  * corresponding to that symbol in the live tree.  Update the references in
  * the overlay with the phandle values in the live tree.
  *
+ * @export_symbols can be use in this references update. The resolver tries
+ * first to find a match in the @export_symbols. If not found, it uses the
+ * "__symbol__" node in the live tree.
+ *
  * @overlay must be detached.
  *
  * Resolving and applying @overlay to the live expanded devicetree must be
@@ -267,7 +272,7 @@ static int get_phandle_from_symbols_node(const struct device_node *tree_symbols,
  *
  * Return: %0 on success or a negative error value on error.
  */
-int of_resolve_phandles(struct device_node *overlay)
+int of_resolve_phandles(struct device_node *overlay, const struct device_node *export_symbols)
 {
 	struct device_node *child, *overlay_fixups;
 	struct device_node __free(device_node) *local_fixups = NULL;
diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
index 64d301893af7..620237365566 100644
--- a/drivers/of/unittest.c
+++ b/drivers/of/unittest.c
@@ -2026,7 +2026,7 @@ static int __init unittest_data_add(void)
 	 */
 	of_overlay_mutex_lock();
 
-	rc = of_resolve_phandles(unittest_data_node);
+	rc = of_resolve_phandles(unittest_data_node, NULL);
 	if (rc) {
 		pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
 		of_overlay_mutex_unlock();
@@ -3915,7 +3915,7 @@ static __init void of_unittest_overlay_high_level(void)
 	 * because kmalloc() was not yet available.
 	 */
 	of_overlay_mutex_lock();
-	of_resolve_phandles(overlay_base_root);
+	of_resolve_phandles(overlay_base_root, NULL);
 	of_overlay_mutex_unlock();
 
 
-- 
2.49.0
Re: [PATCH v2 3/7] of: resolver: Add export_symbols in of_resolve_phandles() parameters
Posted by Luca Ceresoli 7 months, 2 weeks ago
Hello Hervé,

On Wed, 30 Apr 2025 14:51:47 +0200
Herve Codina <herve.codina@bootlin.com> wrote:

> In order to prepare the introduction of the export symbols node
> handling, add a export_symbols parameter in of_resolve_phandles().
> 
> The export_symbols is the export symbols device tree node the resolver
> will use for the overlay symbols resolution.
> 
> Signed-off-by: Herve Codina <herve.codina@bootlin.com>
> Tested-by: Ayush Singh <ayush@beagleboard.org>

[...]

> --- a/drivers/of/resolver.c
> +++ b/drivers/of/resolver.c
> @@ -237,7 +237,8 @@ static int get_phandle_from_symbols_node(const struct device_node *tree_symbols,
>  /**
>   * of_resolve_phandles - Relocate and resolve overlay against live tree
>   *
> - * @overlay:	Pointer to devicetree overlay to relocate and resolve
> + * @overlay:		Pointer to devicetree overlay to relocate and resolve
> + * @export_symbols:	Pointer to devicetree export symbols node.
>   *
>   * Modify (relocate) values of local phandles in @overlay to a range that
>   * does not conflict with the live expanded devicetree.  Update references
> @@ -257,6 +258,10 @@ static int get_phandle_from_symbols_node(const struct device_node *tree_symbols,
>   * corresponding to that symbol in the live tree.  Update the references in
>   * the overlay with the phandle values in the live tree.
>   *
> + * @export_symbols can be use in this references update. The resolver tries
> + * first to find a match in the @export_symbols. If not found, it uses the
> + * "__symbol__" node in the live tree.

The rationale behind this logic is not clear to me. I'd have expected
instead this logic:

  if (export-symbols != NULL):
      match only in export-symbols
  else
      match only in __symbols__

following the idea that it's better to be strict when introducing
something, and possibly relax it later on.

As I see it, with the current logic if you use export-symbols but you
build dtbs with -@, you can still match a global label. export-symbols
should avoid that instead.

Let me know whether I'm missing something here (which is surely
possible).

Other than this, the series looks good to me. I'm not adding my R-by on
the implementation patches though, waiting for the above question to be
clarified.

Luca

-- 
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Re: [PATCH v2 3/7] of: resolver: Add export_symbols in of_resolve_phandles() parameters
Posted by Herve Codina 7 months, 2 weeks ago
Hi Luca,

On Fri, 2 May 2025 16:35:59 +0200
Luca Ceresoli <luca.ceresoli@bootlin.com> wrote:

> Hello Hervé,
> 
> On Wed, 30 Apr 2025 14:51:47 +0200
> Herve Codina <herve.codina@bootlin.com> wrote:
> 
> > In order to prepare the introduction of the export symbols node
> > handling, add a export_symbols parameter in of_resolve_phandles().
> > 
> > The export_symbols is the export symbols device tree node the resolver
> > will use for the overlay symbols resolution.
> > 
> > Signed-off-by: Herve Codina <herve.codina@bootlin.com>
> > Tested-by: Ayush Singh <ayush@beagleboard.org>  
> 
> [...]
> 
> > --- a/drivers/of/resolver.c
> > +++ b/drivers/of/resolver.c
> > @@ -237,7 +237,8 @@ static int get_phandle_from_symbols_node(const struct device_node *tree_symbols,
> >  /**
> >   * of_resolve_phandles - Relocate and resolve overlay against live tree
> >   *
> > - * @overlay:	Pointer to devicetree overlay to relocate and resolve
> > + * @overlay:		Pointer to devicetree overlay to relocate and resolve
> > + * @export_symbols:	Pointer to devicetree export symbols node.
> >   *
> >   * Modify (relocate) values of local phandles in @overlay to a range that
> >   * does not conflict with the live expanded devicetree.  Update references
> > @@ -257,6 +258,10 @@ static int get_phandle_from_symbols_node(const struct device_node *tree_symbols,
> >   * corresponding to that symbol in the live tree.  Update the references in
> >   * the overlay with the phandle values in the live tree.
> >   *
> > + * @export_symbols can be use in this references update. The resolver tries
> > + * first to find a match in the @export_symbols. If not found, it uses the
> > + * "__symbol__" node in the live tree.  
> 
> The rationale behind this logic is not clear to me. I'd have expected
> instead this logic:
> 
>   if (export-symbols != NULL):
>       match only in export-symbols
>   else
>       match only in __symbols__
> 
> following the idea that it's better to be strict when introducing
> something, and possibly relax it later on.
> 
> As I see it, with the current logic if you use export-symbols but you
> build dtbs with -@, you can still match a global label. export-symbols
> should avoid that instead.
> 
> Let me know whether I'm missing something here (which is surely
> possible).

No, you don't miss anything, it was just a choice and I have chosen to be
not exclusive between export-symbols and __symbols__.

export-symbols is taken in priority and if the symbol is not found in
export-symbols, we try with __symbols__.

Maybe I should be stricter. I don't have any strong opinion about that.

Ayush, on fdtoverlay what is the choice done ?

If an export-symbols is present and a symbol needs to be resolved but this
symbol is not found in export-symbols, do you try to fing it in __symbols__
or do you simply abort the resolution with an error ?

Best regards,
Hervé