Skip to contentSkip to conversation

Bug or feature? /somenode overwrites /somenode@17 in fdt-overlay

DISCUSSION Uwe Kleine-König 3 participants 7 messages Apr 25, 2023 – Apr 29, 2023 3 messages not in the archive

lore
Share & tools
lore
#0 lore raw

Hello,

TL;DR: The node &{/somenode} in my overlay overwrites &{/somenode@27}. Is this expected?

I have the following base and overlay dts files:

	uwe@taurus:~/tmp/fdtoverlay$ cat base.dts
	/dts-v1/;
	/ {
		somenode@12 {
			property = <0x2a>;
		};

		somenode@27 {
			property = <0x20>;
		};
	};
	uwe@taurus:~/tmp/fdtoverlay$ cat overlay.dts
	/dts-v1/;
	/plugin/;

	&{/somenode} {
		property = <0x17>;
	};

When compiling these and using them with fdtoverlay I see that /somenode@12 is overwritten(. And if I swap the order of /somenode@12 and /somenode@27 in base.dts, the latter is overwritten instead):

uwe@taurus:~/tmp/fdtoverlay$ dtc -I dts -O dtb -o base.dtb base.dts && dtc -I dts -O dtb -o overlay.dtb overlay.dts && fdtoverlay -i base.dtb -o base+overlay.dtb overlay.dtb && fdtdump base+overlay.dtb
base.dts:4.14-6.4: Warning (unit_address_vs_reg): /somenode@12: node has a unit name, but no reg or ranges property
base.dts:8.14-10.4: Warning (unit_address_vs_reg): /somenode@27: node has a unit name, but no reg or ranges property

**** fdtdump is a low-level debugging tool, not meant for general use. **** If you want to decompile a dtb, you probably want
**** dtc -I dtb -O dts <filename>

/dts-v1/;
// magic: 0xd00dfeed
// totalsize: 0x99 (153)
// off_dt_struct: 0x38
// off_dt_strings: 0x90
// off_mem_rsvmap: 0x28
// version: 17
// last_comp_version: 16
// boot_cpuid_phys: 0x0
// size_dt_strings: 0x9
// size_dt_struct: 0x58

/ {
    somenode@12 {
        property = <0x00000017>;
    };
    somenode@27 {
        property = <0x00000020>;
    };
};

I was surprised and wonder if this is as designed or if this is a bug that should be fixed. Any insights?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

A message that is not in the archive (sent off-list or not yet received): 20230425145504.tswuitufzikuhmnz-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org

#2 lore raw
Quoted 5 lines · Uwe Kleine-König

Hello,

TL;DR: The node &{/somenode} in my overlay overwrites &{/somenode@27}. Is this expected?

Sounds like a bug to me. Probably due to our mixed of handling node names with and without the unit-address included.

Rob

#3 lore raw
In reply to a message not in the archive
Quoted 4 lines · Uwe Kleine-König

Hello,

TL;DR: The node &{/somenode} in my overlay overwrites &{/somenode@27}. Is this expected?

Huh, this is a really interesting case. So I think the overall answer is "no" that's not expected. However, what exactly we should do instead is not entirely obvious

This happens as a consequence of the fact that that using a path reference here is interpreted just like a path reference anywhere - and I don't think we want to change that.

DT paths - for convenience, and to match long standing OF conventions
- allow you to omit the unit addresses, and will resolve to something matching the base name.

The case of overlays is particularly surprising, but it occurs to me that this could also be confusing for other uses of path references:

/ {
    somenode@abcd1234fedc9876 {
        . . .
    };
    othernode@100 {
        something = <&{/somenode}>;
    };
}

Could be desirable for brevity. However it becomes ambiguous and potentially confusing if another somenode@<different address> is added later.

So, I think when resolving path references, we should stop just accepting the first match we find, but instead search the whole tree and trip an error if it's ambiguous. That should avoid the confusing situation you encountered

Quoted 22 lines

I have the following base and overlay dts files:

	uwe@taurus:~/tmp/fdtoverlay$ cat base.dts
	/dts-v1/;
	/ {
		somenode@12 {
			property = <0x2a>;
		};

		somenode@27 {
			property = <0x20>;
		};
	};
	uwe@taurus:~/tmp/fdtoverlay$ cat overlay.dts
	/dts-v1/;
	/plugin/;

	&{/somenode} {
		property = <0x17>;
	};
Quoted 3 lines

When compiling these and using them with fdtoverlay I see that /somenode@12 is overwritten(. And if I swap the order of /somenode@12 and /somenode@27 in base.dts, the latter is overwritten instead):

Of course in the case of runtime overlays we can't perform this check at compile time. We can, and probably should, check for ambiguity in path reference during overlay application. However, that doesn't entirely address the problem - because we don't know everything about the base tree, doing this is kind of bad practice in an overlay.

Unfortunately, we can't outright ban (or deprecate) fragment targets with no unit address, because we might need to update a node which has no unit address.

We could change the semantics of runtime overlays so that paths are interpreted strictly - no missing unit addresses allowed. I'm pretty disinclined to do that though: it means paths there would be interpreted differently from how they are everywhere else. It could, at least in theory, break existing overlays that work in situations where the target is not ambiguous. This might even make some sensible seeming overlays impossible: imagine an overlay designed to patch a property on the ethernet node for a closely related family of SoCs. We only expect there to be one ethernet on the SoC, but maybe different minor revisions changed the NIC's address. With the current behaviour a single overlay can handle both revisions, with the changed behaviour it couldn't.

Another thing to bear in mind is that if you're using path-target overlays you're already kind of in fragile hack territory. A base tree that's designed for overlay patching should be exporting symbols with a clear convention on what labels a compatible overlay can expect.

Quoted 29 lines
uwe@taurus:~/tmp/fdtoverlay$ dtc -I dts -O dtb -o base.dtb base.dts && dtc -I dts -O dtb -o overlay.dtb overlay.dts && fdtoverlay -i base.dtb -o base+overlay.dtb overlay.dtb && fdtdump base+overlay.dtb
base.dts:4.14-6.4: Warning (unit_address_vs_reg): /somenode@12: node has a unit name, but no reg or ranges property
base.dts:8.14-10.4: Warning (unit_address_vs_reg): /somenode@27: node has a unit name, but no reg or ranges property

**** fdtdump is a low-level debugging tool, not meant for general use. **** If you want to decompile a dtb, you probably want
**** dtc -I dtb -O dts <filename>

/dts-v1/;
// magic: 0xd00dfeed
// totalsize: 0x99 (153)
// off_dt_struct: 0x38
// off_dt_strings: 0x90
// off_mem_rsvmap: 0x28
// version: 17
// last_comp_version: 16
// boot_cpuid_phys: 0x0
// size_dt_strings: 0x9
// size_dt_struct: 0x58

/ {
    somenode@12 {
        property = <0x00000017>;
    };
    somenode@27 {
        property = <0x00000020>;
    };
};
Quoted 2 lines

I was surprised and wonder if this is as designed or if this is a bug that should be fixed. Any insights?

As above, it's not really either. This is a consequence of applying established rules in a new situation where the results are a bit counterintuitive.

I think the best we can do is to report error on ambiguous path resolution, both within dtc itself and in the overlay application code of libfdt.

Signature · 4 lines
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
#4 lore raw

Helle David,

Quoted 7 lines · David Gibson
Quoted 2 lines · Uwe Kleine-König

TL;DR: The node &{/somenode} in my overlay overwrites &{/somenode@27}. Is this expected?

Huh, this is a really interesting case. So I think the overall answer is "no" that's not expected. However, what exactly we should do instead is not entirely obvious

IMHO there is a need for a set of different lookup functions.

Something like fdt_subnode_offset_strict() that does a lookup without unit address interpolation. And maybe rename fdt_path_offset to fdt_path_offset_lax() (but keep the name without suffix for API/ABI compatibility). And the same for all functions that have a similar issue.

Then all internal usages could be reviewed and switched to the right variant.

Changing the lax variants to error out if there is no unambiguous match also sounds like a good idea.

Quoted 55 lines

This happens as a consequence of the fact that that using a path reference here is interpreted just like a path reference anywhere - and I don't think we want to change that.

DT paths - for convenience, and to match long standing OF conventions
- allow you to omit the unit addresses, and will resolve to something matching the base name.

The case of overlays is particularly surprising, but it occurs to me that this could also be confusing for other uses of path references:

/ {
    somenode@abcd1234fedc9876 {
        . . .
    };
    othernode@100 {
        something = <&{/somenode}>;
    };
}

Could be desirable for brevity. However it becomes ambiguous and potentially confusing if another somenode@<different address> is added later.

So, I think when resolving path references, we should stop just accepting the first match we find, but instead search the whole tree and trip an error if it's ambiguous. That should avoid the confusing situation you encountered

Quoted 22 lines

I have the following base and overlay dts files:

	uwe@taurus:~/tmp/fdtoverlay$ cat base.dts
	/dts-v1/;
	/ {
		somenode@12 {
			property = <0x2a>;
		};

		somenode@27 {
			property = <0x20>;
		};
	};
	uwe@taurus:~/tmp/fdtoverlay$ cat overlay.dts
	/dts-v1/;
	/plugin/;

	&{/somenode} {
		property = <0x17>;
	};
Quoted 3 lines

When compiling these and using them with fdtoverlay I see that /somenode@12 is overwritten(. And if I swap the order of /somenode@12 and /somenode@27 in base.dts, the latter is overwritten instead):

Quoted 5 lines

Of course in the case of runtime overlays we can't perform this check at compile time. We can, and probably should, check for ambiguity in path reference during overlay application. However, that doesn't entirely address the problem - because we don't know everything about the base tree, doing this is kind of bad practice in an overlay.

Agreed. However it's unclear to me what information is missing. Of course you don't know the base tree at the time the overlay is compiled, but at application time the available information should be complete, shouldn't it?

Quoted 55 lines

Unfortunately, we can't outright ban (or deprecate) fragment targets with no unit address, because we might need to update a node which has no unit address.

We could change the semantics of runtime overlays so that paths are interpreted strictly - no missing unit addresses allowed. I'm pretty disinclined to do that though: it means paths there would be interpreted differently from how they are everywhere else. It could, at least in theory, break existing overlays that work in situations where the target is not ambiguous. This might even make some sensible seeming overlays impossible: imagine an overlay designed to patch a property on the ethernet node for a closely related family of SoCs. We only expect there to be one ethernet on the SoC, but maybe different minor revisions changed the NIC's address. With the current behaviour a single overlay can handle both revisions, with the changed behaviour it couldn't.

Another thing to bear in mind is that if you're using path-target overlays you're already kind of in fragile hack territory. A base tree that's designed for overlay patching should be exporting symbols with a clear convention on what labels a compatible overlay can expect.

Quoted 29 lines
uwe@taurus:~/tmp/fdtoverlay$ dtc -I dts -O dtb -o base.dtb base.dts && dtc -I dts -O dtb -o overlay.dtb overlay.dts && fdtoverlay -i base.dtb -o base+overlay.dtb overlay.dtb && fdtdump base+overlay.dtb
base.dts:4.14-6.4: Warning (unit_address_vs_reg): /somenode@12: node has a unit name, but no reg or ranges property
base.dts:8.14-10.4: Warning (unit_address_vs_reg): /somenode@27: node has a unit name, but no reg or ranges property

**** fdtdump is a low-level debugging tool, not meant for general use. **** If you want to decompile a dtb, you probably want
**** dtc -I dtb -O dts <filename>

/dts-v1/;
// magic: 0xd00dfeed
// totalsize: 0x99 (153)
// off_dt_struct: 0x38
// off_dt_strings: 0x90
// off_mem_rsvmap: 0x28
// version: 17
// last_comp_version: 16
// boot_cpuid_phys: 0x0
// size_dt_strings: 0x9
// size_dt_struct: 0x58

/ {
    somenode@12 {
        property = <0x00000017>;
    };
    somenode@27 {
        property = <0x00000020>;
    };
};
Quoted 2 lines

I was surprised and wonder if this is as designed or if this is a bug that should be fixed. Any insights?

Quoted 7 lines

As above, it's not really either. This is a consequence of applying established rules in a new situation where the results are a bit counterintuitive.

I think the best we can do is to report error on ambiguous path resolution, both within dtc itself and in the overlay application code of libfdt.

I agree that changing existing code paths might be fragile.

Still adding the strict flavour variants of the affected functions is a good idea. Then future usages can consciously choose which semantic is intended.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

A message that is not in the archive (sent off-list or not yet received): 20230428180441.hn34wlwevuj75zml-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org

#6 lore raw

Hello David,

Quoted 3 lines · Uwe Kleine-König

Still adding the strict flavour variants of the affected functions is a good idea. Then future usages can consciously choose which semantic is intended.

Another surprising instance is fdt_get_alias() which might behave in strange ways if there is a node at /aliases@12345.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

A message that is not in the archive (sent off-list or not yet received): 20230428184007.56cwbzk5fe5trb4z-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org

#8 lore raw
Quoted 9 lines · Uwe Kleine-König

Hello David,

Quoted 3 lines · Uwe Kleine-König

Still adding the strict flavour variants of the affected functions is a good idea. Then future usages can consciously choose which semantic is intended.

Another surprising instance is fdt_get_alias() which might behave in strange ways if there is a node at /aliases@12345.

Technically true, but having /aliases@anything is something that should never happen anyway. We should probably add a check for that, if we don't have one yet.

Signature · 4 lines
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
#9 lore raw
In reply to a message not in the archive
Quoted 4 lines · Uwe Kleine-König

Helle David,

Quoted 1 line · David Gibson

On Tue, Apr 25, 2023 at 04:55:04PM +0200, Uwe Kleine-König wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

TL;DR: The node &{/somenode} in my overlay overwrites &{/somenode@27}. Is this expected?

Huh, this is a really interesting case. So I think the overall answer is "no" that's not expected. However, what exactly we should do instead is not entirely obvious

IMHO there is a need for a set of different lookup functions.

Hmm.. I'm not yet convinced.

Quoted 8 lines

Something like fdt_subnode_offset_strict() that does a lookup without unit address interpolation. And maybe rename fdt_path_offset to fdt_path_offset_lax() (but keep the name without suffix for API/ABI compatibility). And the same for all functions that have a similar issue.

Then all internal usages could be reviewed and switched to the right variant.

Hrm. I think having two ways of looking up paths adds potential confusion, so we need a very strong case if we're going to do that.

Quoted 2 lines

Changing the lax variants to error out if there is no unambiguous match also sounds like a good idea.

This however, we should do.

Quoted 61 lines
Quoted 55 lines

This happens as a consequence of the fact that that using a path reference here is interpreted just like a path reference anywhere - and I don't think we want to change that.

DT paths - for convenience, and to match long standing OF conventions
- allow you to omit the unit addresses, and will resolve to something matching the base name.

The case of overlays is particularly surprising, but it occurs to me that this could also be confusing for other uses of path references:

/ {
    somenode@abcd1234fedc9876 {
        . . .
    };
    othernode@100 {
        something = <&{/somenode}>;
    };
}

Could be desirable for brevity. However it becomes ambiguous and potentially confusing if another somenode@<different address> is added later.

So, I think when resolving path references, we should stop just accepting the first match we find, but instead search the whole tree and trip an error if it's ambiguous. That should avoid the confusing situation you encountered

Quoted 22 lines

I have the following base and overlay dts files:

	uwe@taurus:~/tmp/fdtoverlay$ cat base.dts
	/dts-v1/;
	/ {
		somenode@12 {
			property = <0x2a>;
		};

		somenode@27 {
			property = <0x20>;
		};
	};
	uwe@taurus:~/tmp/fdtoverlay$ cat overlay.dts
	/dts-v1/;
	/plugin/;

	&{/somenode} {
		property = <0x17>;
	};
Quoted 3 lines

When compiling these and using them with fdtoverlay I see that /somenode@12 is overwritten(. And if I swap the order of /somenode@12 and /somenode@27 in base.dts, the latter is overwritten instead):

Quoted 5 lines

Of course in the case of runtime overlays we can't perform this check at compile time. We can, and probably should, check for ambiguity in path reference during overlay application. However, that doesn't entirely address the problem - because we don't know everything about the base tree, doing this is kind of bad practice in an overlay.

Quoted 4 lines

Agreed. However it's unclear to me what information is missing. Of course you don't know the base tree at the time the overlay is compiled, but at application time the available information should be complete, shouldn't it?

Right, at overlay application time we have all the information. However at overlay application time our options for reporting and handling errors are typically rather more constrained than at compile time.

Quoted 63 lines
Quoted 55 lines

Unfortunately, we can't outright ban (or deprecate) fragment targets with no unit address, because we might need to update a node which has no unit address.

We could change the semantics of runtime overlays so that paths are interpreted strictly - no missing unit addresses allowed. I'm pretty disinclined to do that though: it means paths there would be interpreted differently from how they are everywhere else. It could, at least in theory, break existing overlays that work in situations where the target is not ambiguous. This might even make some sensible seeming overlays impossible: imagine an overlay designed to patch a property on the ethernet node for a closely related family of SoCs. We only expect there to be one ethernet on the SoC, but maybe different minor revisions changed the NIC's address. With the current behaviour a single overlay can handle both revisions, with the changed behaviour it couldn't.

Another thing to bear in mind is that if you're using path-target overlays you're already kind of in fragile hack territory. A base tree that's designed for overlay patching should be exporting symbols with a clear convention on what labels a compatible overlay can expect.

Quoted 29 lines
uwe@taurus:~/tmp/fdtoverlay$ dtc -I dts -O dtb -o base.dtb base.dts && dtc -I dts -O dtb -o overlay.dtb overlay.dts && fdtoverlay -i base.dtb -o base+overlay.dtb overlay.dtb && fdtdump base+overlay.dtb
base.dts:4.14-6.4: Warning (unit_address_vs_reg): /somenode@12: node has a unit name, but no reg or ranges property
base.dts:8.14-10.4: Warning (unit_address_vs_reg): /somenode@27: node has a unit name, but no reg or ranges property

**** fdtdump is a low-level debugging tool, not meant for general use. **** If you want to decompile a dtb, you probably want
**** dtc -I dtb -O dts <filename>

/dts-v1/;
// magic: 0xd00dfeed
// totalsize: 0x99 (153)
// off_dt_struct: 0x38
// off_dt_strings: 0x90
// off_mem_rsvmap: 0x28
// version: 17
// last_comp_version: 16
// boot_cpuid_phys: 0x0
// size_dt_strings: 0x9
// size_dt_struct: 0x58

/ {
    somenode@12 {
        property = <0x00000017>;
    };
    somenode@27 {
        property = <0x00000020>;
    };
};
Quoted 2 lines

I was surprised and wonder if this is as designed or if this is a bug that should be fixed. Any insights?

Quoted 7 lines

As above, it's not really either. This is a consequence of applying established rules in a new situation where the results are a bit counterintuitive.

I think the best we can do is to report error on ambiguous path resolution, both within dtc itself and in the overlay application code of libfdt.

Quoted 5 lines

I agree that changing existing code paths might be fragile.

Still adding the strict flavour variants of the affected functions is a good idea. Then future usages can consciously choose which semantic is intended.

Yeah... see that's a decision I'd prefer avoiding making people make unless we really can't avoid it.

Signature · 4 lines
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