diff mbox

[2/2] Warn on node name unit-addresses with '0x' or leading 0s

Message ID 1455223619-16052-3-git-send-email-robh@kernel.org
State Superseded
Headers show

Commit Message

Rob Herring Feb. 11, 2016, 8:46 p.m. UTC
Node name unit-addresses should never begin with 0x or leading 0s
regardless of whether they have a bus specific address (i.e. one with
commas) or not. Add warnings to check for these cases.

Signed-off-by: Rob Herring <robh@kernel.org>

---
 checks.c                       | 10 ++++++++++
 tests/run_tests.sh             |  2 ++
 tests/unit-addr-leading-0s.dts | 10 ++++++++++
 tests/unit-addr-leading-0x.dts | 10 ++++++++++
 4 files changed, 32 insertions(+)
 create mode 100644 tests/unit-addr-leading-0s.dts
 create mode 100644 tests/unit-addr-leading-0x.dts

-- 
2.5.0

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Rob Herring Feb. 22, 2016, 4:51 p.m. UTC | #1
On Thu, Feb 18, 2016 at 11:07 PM, David Gibson
<david@gibson.dropbear.id.au> wrote:
> On Thu, Feb 11, 2016 at 02:46:59PM -0600, Rob Herring wrote:

>> Node name unit-addresses should never begin with 0x or leading 0s

>> regardless of whether they have a bus specific address (i.e. one with

>> commas) or not. Add warnings to check for these cases.

>

> Hmm.. I'm pretty sure that's true in practice, but it's not true in

> theory.  A bus could define it's unit address format just about

> however it wants, including with leading 0s.


Only if it is not reviewed... This whole check is about what best
practices are, not what is possible.

> I think a better approach would be to add a test specific to

> simple-bus devices (by looking at compatible on the parent) that fully

> checks the unit address.

>

> From there we can start adding tests for other bus types.


simple-bus is easy enough, but then next up would be I2C and SPI. We
can't generically tell if a node is on I2C or SPI bus. If we do have
some bus with wacky addresses, it should definitely have a bus
compatible and then we can simply exclude it from the check.

Another option would be skipping the test if there are any commas (or
periods, etc.) in the unit address. That's pretty rare to begin with
and a single number is pretty much not a bus specific unit-address.

Rob
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Rob Herring Feb. 23, 2016, 2:35 p.m. UTC | #2
On Mon, Feb 22, 2016 at 11:47 PM, David Gibson
<david@gibson.dropbear.id.au> wrote:
> On Mon, Feb 22, 2016 at 10:51:46AM -0600, Rob Herring wrote:

>> On Thu, Feb 18, 2016 at 11:07 PM, David Gibson

>> <david@gibson.dropbear.id.au> wrote:

>> > On Thu, Feb 11, 2016 at 02:46:59PM -0600, Rob Herring wrote:

>> >> Node name unit-addresses should never begin with 0x or leading 0s

>> >> regardless of whether they have a bus specific address (i.e. one with

>> >> commas) or not. Add warnings to check for these cases.

>> >

>> > Hmm.. I'm pretty sure that's true in practice, but it's not true in

>> > theory.  A bus could define it's unit address format just about

>> > however it wants, including with leading 0s.

>>

>> Only if it is not reviewed... This whole check is about what best

>> practices are, not what is possible.

>

> Hmm.  dtc checks are really about checking for best practice at the

> level of individual dts files, though, not bindings.


Checking simple-bus specifically would be checking a binding.

>> > I think a better approach would be to add a test specific to

>> > simple-bus devices (by looking at compatible on the parent) that fully

>> > checks the unit address.

>> >

>> > From there we can start adding tests for other bus types.

>>

>> simple-bus is easy enough,

>

> So, start with that, then tackle the next problem.

>

>> but then next up would be I2C and SPI. We

>> can't generically tell if a node is on I2C or SPI bus.

>

> Why not?  Or perhaps.. how generically do you need?  I think having a

> big list of i2c / spi controllers would be acceptable here, if not

> ideal.


So someone adds a new controller, puts crap in for unit addresses, and
then no warnings until that compatible string is added to dtc. And I'm
still left spending my time in reviews telling them to fix this
trivial crap.

That's roughly 60 I2C controllers (families, so multiple compatible
strings each) plus similar number of SPI controllers, OF-graph
binding, and random other things where reg gets used.

>

>> If we do have

>> some bus with wacky addresses, it should definitely have a bus

>> compatible and then we can simply exclude it from the check.

>>

>> Another option would be skipping the test if there are any commas (or

>> periods, etc.) in the unit address. That's pretty rare to begin with

>> and a single number is pretty much not a bus specific unit-address.

>

> Um.. no.. there are definitely bus types that don't typically use

> commas.  ISA, for one.


All the cases of ISA in the kernel tree at least would pass this test.
But we could either blacklist ISA or skip if any non-hex characters
are present.

BTW, my next patch is stricter node and property name checks on the
use of '#', '?', '.', '+', '*', and '_'. So if you don't think these
kinds to checks belong in dtc, then tell me and suggest how we should
check for this.

Rob
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Rob Herring Feb. 24, 2016, 3:01 p.m. UTC | #3
On Wed, Feb 24, 2016 at 11:44:56AM +1100, David Gibson wrote:
> On Tue, Feb 23, 2016 at 08:35:46AM -0600, Rob Herring wrote:

> > On Mon, Feb 22, 2016 at 11:47 PM, David Gibson

> > <david@gibson.dropbear.id.au> wrote:

> > > On Mon, Feb 22, 2016 at 10:51:46AM -0600, Rob Herring wrote:

> > >> On Thu, Feb 18, 2016 at 11:07 PM, David Gibson

> > >> <david@gibson.dropbear.id.au> wrote:

> > >> > On Thu, Feb 11, 2016 at 02:46:59PM -0600, Rob Herring wrote:

> > >> >> Node name unit-addresses should never begin with 0x or leading 0s

> > >> >> regardless of whether they have a bus specific address (i.e. one with

> > >> >> commas) or not. Add warnings to check for these cases.

> > >> >

> > >> > Hmm.. I'm pretty sure that's true in practice, but it's not true in

> > >> > theory.  A bus could define it's unit address format just about

> > >> > however it wants, including with leading 0s.

> > >>

> > >> Only if it is not reviewed... This whole check is about what best

> > >> practices are, not what is possible.

> > >

> > > Hmm.  dtc checks are really about checking for best practice at the

> > > level of individual dts files, though, not bindings.

> > 

> > Checking simple-bus specifically would be checking a binding.

> 

> Sorry, I wasn't clear.  dtc checking the dts against a binding is

> fine, but checking the sanity of the binding itself is beyond its

> scope.

> 

> > >> > I think a better approach would be to add a test specific to

> > >> > simple-bus devices (by looking at compatible on the parent) that fully

> > >> > checks the unit address.

> > >> >

> > >> > From there we can start adding tests for other bus types.

> > >>

> > >> simple-bus is easy enough,

> > >

> > > So, start with that, then tackle the next problem.

> > >

> > >> but then next up would be I2C and SPI. We

> > >> can't generically tell if a node is on I2C or SPI bus.

> > >

> > > Why not?  Or perhaps.. how generically do you need?  I think having a

> > > big list of i2c / spi controllers would be acceptable here, if not

> > > ideal.

> > 

> > So someone adds a new controller, puts crap in for unit addresses, and

> > then no warnings until that compatible string is added to dtc. And I'm

> > still left spending my time in reviews telling them to fix this

> > trivial crap.

> > 

> > That's roughly 60 I2C controllers (families, so multiple compatible

> > strings each) plus similar number of SPI controllers, OF-graph

> > binding, and random other things where reg gets used.

> 

> Ah, I see.

> 

> Ok, I guess we do need to have an option for a "fallback" scheme for

> unit addresses (i.e. hex) for bus types we don't specifically

> recognize.  But I'd still like the logic to be:

>       if (known bus type)

>            check against format for this bus type

>       else

>            check against fallback format

> 

> Rather than putting the second test in with a hacked up set of

> exclusions.


Okay, makes sense.

Do you think we still need simple-bus as an explicit type given the 
check is the same as the default? Might be useful to have if we want to 
add some checks that address translations work.

> To do this nicely, I think the best way will be to add a bus_type

> field to the node structure in dtc, and have it populated (with an

> option for "unknown") in an early check pass, that later unit address

> tests can references as a prereq.

> 

> Pointer to a struct with unit address formatting functions, with NULL

> for unknown is the obvious choice to me for bus_type.


So, something like this for the first stage:

static bool pci_bus_check_is_type(struct node *node)
{
	struct property *prop;
	
	if (!node || !node->parent)
		return false;

	prop = get_property(node->parent, "device_type");
	if (!prop)
		return false;
		
	if (strcmp(prop->val, "pci") == 0)
		return true;
		
	return false;
}

static void pci_bus_check_unit_address()
{

}

struct bus_type_fns {
	.check_is_type = pci_bus_check_is_type,
	.check_unit_address = pci_bus_check_unit_address,
} pci_bus_fns;

struct bus_type_fns * {
	&pci_bus_fns,
	NULL
} bus_types;

static void fixup_bus_type(struct check *c, struct node *root,
				  struct node *node)
{
	struct bus_type_fns **bus;
	
	for (bus = bus_types; *bus != NULL; bus++) {
		if (!(*bus)->check_is_type(node))
			continue;

		node->bus_type = *bus;
		break;
	}
}
ERROR(bus_type, NULL, NULL, fixup_bus_type, NULL, NULL);


Rob
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/checks.c b/checks.c
index 4001b8c..300ab43 100644
--- a/checks.c
+++ b/checks.c
@@ -310,6 +310,16 @@  static void check_unit_address_vs_reg(struct check *c, struct node *dt,
 		if (!unitname[0])
 			FAIL(c, "Node %s has a reg or ranges property, but no unit name",
 			    node->fullpath);
+
+		if (strstr(unitname, "0x") == unitname) {
+			FAIL(c, "Node %s unit name should not have leading \"0x\"",
+			    node->fullpath);
+			/* skip over 0x for next test */
+			unitname += 2;
+		}
+		if (unitname[0] == '0' && isxdigit(unitname[1]))
+			FAIL(c, "Node %s unit name should not have leading 0s",
+			    node->fullpath);
 	} else {
 		if (unitname[0])
 			FAIL(c, "Node %s has a unit name, but no reg property",
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 4b7a131..502caa6 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -445,6 +445,8 @@  dtc_tests () {
     check_tests obsolete-chosen-interrupt-controller.dts obsolete_chosen_interrupt_controller
     check_tests reg-without-unit-addr.dts unit_address_vs_reg
     check_tests unit-addr-without-reg.dts unit_address_vs_reg
+    check_tests unit-addr-leading-0x.dts unit_address_vs_reg
+    check_tests unit-addr-leading-0s.dts unit_address_vs_reg
     run_sh_test dtc-checkfails.sh node_name_chars -- -I dtb -O dtb bad_node_char.dtb
     run_sh_test dtc-checkfails.sh node_name_format -- -I dtb -O dtb bad_node_format.dtb
     run_sh_test dtc-checkfails.sh prop_name_chars -- -I dtb -O dtb bad_prop_char.dtb
diff --git a/tests/unit-addr-leading-0s.dts b/tests/unit-addr-leading-0s.dts
new file mode 100644
index 0000000..7c8e2ce
--- /dev/null
+++ b/tests/unit-addr-leading-0s.dts
@@ -0,0 +1,10 @@ 
+/dts-v1/;
+
+/ {
+	#address-cells = <1>;
+	#size-cells = <1>;
+
+	node@001 {
+		reg = <1 0>;
+	};
+};
diff --git a/tests/unit-addr-leading-0x.dts b/tests/unit-addr-leading-0x.dts
new file mode 100644
index 0000000..7ed7254
--- /dev/null
+++ b/tests/unit-addr-leading-0x.dts
@@ -0,0 +1,10 @@ 
+/dts-v1/;
+
+/ {
+	#address-cells = <1>;
+	#size-cells = <1>;
+
+	node@0x1 {
+		reg = <1 0>;
+	};
+};