devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
Cc: Yves-Alexis Perez <corsac@debian.org>,
	devicetree-compiler@vger.kernel.org, entwicklung@pengutronix.de
Subject: Re: [PATCH v2] libfdt: overlay: ensure that existing phandles are not overwritten
Date: Thu, 22 Feb 2024 17:33:00 +1100	[thread overview]
Message-ID: <ZdbqnDY2k6sU0JRy@zatzit> (raw)
In-Reply-To: <fgyaporsv6l5jqtseabnhzykf4xlfwrxmnpokqanxoyfkzkkfi@kkdu7cfdkgnt>

[-- Attachment #1: Type: text/plain, Size: 4191 bytes --]

On Fri, Feb 16, 2024 at 10:06:37PM +0100, Uwe Kleine-König wrote:
> Hello,
> 
> On Fri, Feb 16, 2024 at 07:19:20PM +0100, Uwe Kleine-König wrote:
> > A phandle in an overlay is not supposed to overwrite a phandle that
> > already exists in the base dtb as this breaks references to the
> > respective node in the base.
> > 
> > So add another iteration over the fdto that checks for such overwrites
> > and fixes the fdto phandle's value to match the fdt's.
> > 
> > A test is added that checks that newly added phandles and existing
> > phandles work as expected.
> > 
> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> 
> After sending the patch out I had a nice idea for an optimisation here.
> It doesn't make the process faster, but improves the result.
> 
> If phandle with the (shifted) value 17 is changed to 5, there is no
> phandle with the value 17 in the end. So while iterating over the fdto
> to replace all phandles with value 17 by 5 all values > 17 can be
> reduced by one. This way the phandle allocation in the resulting dtb is
> continuous if both inputs have continuous numbers.

Honestly, I don't think the marginal advantage of improving the
chances of contiguous phandles is worth the extra complexity.

> 
> This can be implemented by the patch below on top of the patch I'm
> replying to.
> 
> Best regards
> Uwe
> 
> diff --git a/libfdt/fdt_overlay.c b/libfdt/fdt_overlay.c
> index 914acc5b14a6..bfdba50dee50 100644
> --- a/libfdt/fdt_overlay.c
> +++ b/libfdt/fdt_overlay.c
> @@ -529,15 +529,25 @@ static int overlay_adjust_node_conflicting_phandle(void *fdto, int node,
>  	int child;
>  
>  	php = fdt_getprop(fdto, node, "phandle", &len);
> -	if (php && len == sizeof(*php) && fdt32_to_cpu(*php) == fdto_phandle) {
> -		ret = fdt_setprop_inplace_u32(fdto, node, "phandle", fdt_phandle);
> +	if (php && len == sizeof(*php)) {
> +		if (fdt32_to_cpu(*php) == fdto_phandle)
> +			ret = fdt_setprop_inplace_u32(fdto, node, "phandle", fdt_phandle);
> +		else if (fdt32_to_cpu(*php) > fdto_phandle)
> +			ret = fdt_setprop_inplace_u32(fdto, node, "phandle", fdt32_to_cpu(*php) - 1);
> +		else
> +			ret = 0;
>  		if (ret)
>  			return ret;
>  	}
>  
>  	php = fdt_getprop(fdto, node, "linux,phandle", &len);
> -	if (php && len == sizeof(*php) && fdt32_to_cpu(*php) == fdto_phandle) {
> -		ret = fdt_setprop_inplace_u32(fdto, node, "linux,phandle", fdt_phandle);
> +	if (php && len == sizeof(*php)) {
> +		if (fdt32_to_cpu(*php) == fdto_phandle)
> +			ret = fdt_setprop_inplace_u32(fdto, node, "linux,phandle", fdt_phandle);
> +		else if (fdt32_to_cpu(*php) > fdto_phandle)
> +			ret = fdt_setprop_inplace_u32(fdto, node, "phandle", fdt32_to_cpu(*php) - 1);
> +		else
> +			ret = 0;
>  		if (ret)
>  			return ret;
>  	}
> @@ -609,23 +619,27 @@ static int overlay_update_node_conflicting_references(void *fdto, int tree_node,
>  			 */
>  			memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
>  
> -			if (fdt32_to_cpu(adj_val) == fdto_phandle) {
> +			if (fdt32_to_cpu(adj_val) < fdto_phandle)
> +				continue;
>  
> +			if (fdt32_to_cpu(adj_val) == fdto_phandle)
>  				adj_val = cpu_to_fdt32(fdt_phandle);
> +			else
> +				adj_val = cpu_to_fdt32(fdt32_to_cpu(adj_val) - 1);
>  
> -				ret = fdt_setprop_inplace_namelen_partial(fdto,
> -									  tree_node,
> -									  name,
> -									  strlen(name),
> -									  poffset,
> -									  &adj_val,
> -									  sizeof(adj_val));
> -				if (ret == -FDT_ERR_NOSPACE)
> -					return -FDT_ERR_BADOVERLAY;
>  
> -				if (ret)
> -					return ret;
> -			}
> +			ret = fdt_setprop_inplace_namelen_partial(fdto,
> +								  tree_node,
> +								  name,
> +								  strlen(name),
> +								  poffset,
> +								  &adj_val,
> +								  sizeof(adj_val));
> +			if (ret == -FDT_ERR_NOSPACE)
> +				return -FDT_ERR_BADOVERLAY;
> +
> +			if (ret)
> +				return ret;
>  		}
>  	}
>  



-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2024-02-22  6:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-16 18:19 [PATCH v2] libfdt: overlay: ensure that existing phandles are not overwritten Uwe Kleine-König
2024-02-16 21:06 ` Uwe Kleine-König
2024-02-22  6:33   ` David Gibson [this message]
2024-02-22  6:32 ` David Gibson
2024-02-22  7:22   ` Uwe Kleine-König
2024-02-22  9:02     ` David Gibson
2024-02-22  9:38   ` Uwe Kleine-König
2024-02-23  4:41     ` David Gibson
2024-02-23  7:53       ` Uwe Kleine-König
2024-02-23  8:15         ` David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZdbqnDY2k6sU0JRy@zatzit \
    --to=david@gibson.dropbear.id.au \
    --cc=corsac@debian.org \
    --cc=devicetree-compiler@vger.kernel.org \
    --cc=entwicklung@pengutronix.de \
    --cc=u.kleine-koenig@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).