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 20:02:45 +1100 [thread overview]
Message-ID: <ZdcNtSCopy-9BXb0@zatzit> (raw)
In-Reply-To: <eirdh4rp5zcees3b6p7jeazmg323vf74i5cgz64jzeph2bm5mp@qiqpdtz6id4a>
[-- Attachment #1: Type: text/plain, Size: 5039 bytes --]
On Thu, Feb 22, 2024 at 08:22:48AM +0100, Uwe Kleine-König wrote:
> Hello David,
>
> On Thu, Feb 22, 2024 at 05:32:10PM +1100, David Gibson wrote:
> > On Fri, Feb 16, 2024 at 07:19:20PM +0100, Uwe Kleine-König wrote:
> > > [...]
> > > diff --git a/libfdt/fdt_overlay.c b/libfdt/fdt_overlay.c
> > > index 5c0c3981b89d..914acc5b14a6 100644
> > > --- a/libfdt/fdt_overlay.c
> > > +++ b/libfdt/fdt_overlay.c
> > > @@ -520,6 +520,228 @@ static int overlay_fixup_phandles(void *fdt, void *fdto)
> > > return 0;
> > > }
> > >
> >
> > All of these new functions could do with descriptive comments saying
> > what they do (see the existing code for examples).
>
> Yeah, I was lazy. I didn't expect this patch to go in without comments
> and avoided investing time documenting functions that maybe will change
> in the course of the review-resend loop. :-)
>
> > > [...]
> > > +static int overlay_update_node_conflicting_references(void *fdto, int tree_node,
> > > + int fixup_node,
> > > + uint32_t fdt_phandle,
> > > + uint32_t fdto_phandle)
> > > +{
> > > + int fixup_prop;
> > > + int fixup_child;
> > > + int ret;
> > > +
> > > + fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
> > > +
> >
> > Extraneous blank line.
> >
> > > + const fdt32_t *fixup_val;
> > > + const char *tree_val;
> >
> > libfdt style is to use tab indents, looks like most of this block has
> > space indents instead.
>
> Huh, I'm surprised about myself that I didn't notice this alone.
>
> > > +
> > > + /*
> > > + * phandles to fixup can be unaligned.
> > > + *
> > > + * Use a memcpy for the architectures that do
> > > + * not support unaligned accesses.
> > > + */
> > > + memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
> >
> > And here you can use fdt32_ld() which has unaligned access handling
> > built in.
>
> I copied this from somewhere, so I guess there is some potential for
> improvement in existing code. Will take a look.
>
> > > [...]
> > > +
> > > +static int overlay_prevent_phandle_overwrite_node(void *fdt, int fdtnode,
> > > + void *fdto, int fdtonode)
> >
> > Maybe "overlay_resolve_phandle_conflict()" for a slightly shorter
> > function name.
>
> Sounds good, wasn't too lucky with
> overlay_prevent_phandle_overwrite_node() either.
>
> > > +{
> > > + uint32_t fdt_phandle, fdto_phandle;
> > > + int fdtochild;
> > > +
> > > + fdt_phandle = fdt_get_phandle(fdt, fdtnode);
> > > + fdto_phandle = fdt_get_phandle(fdto, fdtonode);
> > > +
> > > + if (fdt_phandle && fdto_phandle) {
> > > + int ret;
> > > +
> > > + ret = overlay_adjust_local_conflicting_phandle(fdto,
> > > + fdt_phandle,
> > > + fdto_phandle);
> > > + if (ret)
> > > + return ret;
> >
> > So while, as you've seen, there can be conflicting phandles between
> > the fdt and the fdto, within the fdto a particular phandle should only
> > appear on a single node. If a phandle is duplicated across nodes
> > witin a single fdto, you have bigger problems. Therefore, having
> > found a conflict, you're already found the only place you need to
> > change the phandle property itself - you don't need to scan the full
> > fdto again.
> >
> > So I believe you can replace the above call with just setting the
> > phandle property on fdtonode.
>
> Oh right!
>
> > > [...]
> > > /**
> > > * overlay_apply_node - Merges a node into the base device tree
> > > * @fdt: Base Device Tree blob
> > > @@ -824,18 +1046,26 @@ int fdt_overlay_apply(void *fdt, void *fdto)
> > > if (ret)
> > > goto err;
> > >
> > > + /* Increase all phandles in the fdto by delta */
> > > ret = overlay_adjust_local_phandles(fdto, delta);
> > > if (ret)
> > > goto err;
> > >
> > > + /* Adapt the phandle values in fdto to the above increase */
> > > ret = overlay_update_local_references(fdto, delta);
> > > if (ret)
> > > goto err;
> > >
> > > + /* Update fdto's phandles using symbols from fdt */
> > > ret = overlay_fixup_phandles(fdt, fdto);
> > > if (ret)
> > > goto err;
> > >
> > > + /* Don't overwrite phandles in fdt */
> > > + ret = overlay_prevent_phandle_overwrite(fdt, fdto);
> > > + if (ret)
> > > + goto err;
> >
> > I think this should go above overlay_fixup_phandles(), so that we're
> > completing all the __local_fixups__ based adjustments before going to
> > the __fixups__ based adjustments.
>
> This is only to please the reader, right? Or is there a corner case I
> failed to see?
Correct. At least as far as I've seen, too.
> The feedback I removed in this mail I agree with. Expect a v3 soon.
>
> Thanks for your time to review my patch,
> Uwe
>
--
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 --]
next prev parent reply other threads:[~2024-02-22 9:02 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
2024-02-22 6:32 ` David Gibson
2024-02-22 7:22 ` Uwe Kleine-König
2024-02-22 9:02 ` David Gibson [this message]
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=ZdcNtSCopy-9BXb0@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.