diff mbox

[v2] OF: base: match each node compatible against all given matches first

Message ID 1386078720-8756-1-git-send-email-sebastian.hesselbarth@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Sebastian Hesselbarth Dec. 3, 2013, 1:52 p.m. UTC
Currently, of_match_node compares each given match against all node's
compatible strings with of_device_is_compatible.

To achieve multiple compatible strings per node with ordering from
specific to generic, this requires given matches to be ordered from
specific to generic. For most of the drivers this is not true and also
an alphabetical ordering is more sane there.

Therefore, this patch modifies of_match_node to match each of the node's
compatible strings against all given matches first, before checking the
next compatible string. This implies that node's compatibles are ordered
from specific to generic while given matches can be in any order.

Signed-off-by: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
---
Changelog:
v1->v2:
- Allow checks against nodes with no compatible (Reported by Rob Herring)
- Add some comments

Cc: Grant Likely <grant.likely@linaro.org>
Cc: Rob Herring <rob.herring@calxeda.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Russell King <linux@arm.linux.org.uk>
Cc: Thierry Reding <thierry.reding@gmail.com>
Cc: Meelis Roos <mroos@linux.ee>
Cc: Marc Kleine-Budde <mkl@pengutronix.de>
Cc: Scott Wood <scottwood@freescale.com>
Cc: devicetree@vger.kernel.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
---
 drivers/of/base.c |   53 +++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 37 insertions(+), 16 deletions(-)

Comments

Meelis Roos Dec. 3, 2013, 8:14 p.m. UTC | #1
> Currently, of_match_node compares each given match against all node's
> compatible strings with of_device_is_compatible.
> 
> To achieve multiple compatible strings per node with ordering from
> specific to generic, this requires given matches to be ordered from
> specific to generic. For most of the drivers this is not true and also
> an alphabetical ordering is more sane there.
> 
> Therefore, this patch modifies of_match_node to match each of the node's
> compatible strings against all given matches first, before checking the
> next compatible string. This implies that node's compatibles are ordered
> from specific to generic while given matches can be in any order.

I think I am on the CC: list because of a CPU detection problem report 
on sparc64 (183912d352a242a276a7877852f107459a13aff9 (of: move 
of_get_cpu_node implementation to DT core library) caused trouble and 
was reverted). So while your V2 patch does not cause any visible harm on 
the same Sun E3500, my gut feeling is that an additional patch would be 
needed to actually test it (a patch like 
183912d352a242a276a7877852f107459a13aff9).

Is this correct or am I missing something?

> 
> Signed-off-by: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
> ---
> Changelog:
> v1->v2:
> - Allow checks against nodes with no compatible (Reported by Rob Herring)
> - Add some comments
> 
> Cc: Grant Likely <grant.likely@linaro.org>
> Cc: Rob Herring <rob.herring@calxeda.com>
> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
> Cc: Russell King <linux@arm.linux.org.uk>
> Cc: Thierry Reding <thierry.reding@gmail.com>
> Cc: Meelis Roos <mroos@linux.ee>
> Cc: Marc Kleine-Budde <mkl@pengutronix.de>
> Cc: Scott Wood <scottwood@freescale.com>
> Cc: devicetree@vger.kernel.org
> Cc: linux-arm-kernel@lists.infradead.org
> Cc: linux-kernel@vger.kernel.org
> ---
>  drivers/of/base.c |   53 +++++++++++++++++++++++++++++++++++++----------------
>  1 files changed, 37 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/of/base.c b/drivers/of/base.c
> index f807d0e..8d007d8 100644
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -731,24 +731,42 @@ static
>  const struct of_device_id *__of_match_node(const struct of_device_id *matches,
>  					   const struct device_node *node)
>  {
> +	const char *cp;
> +	int cplen, l;
> +
>  	if (!matches)
>  		return NULL;
>  
> -	while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
> -		int match = 1;
> -		if (matches->name[0])
> -			match &= node->name
> -				&& !strcmp(matches->name, node->name);
> -		if (matches->type[0])
> -			match &= node->type
> -				&& !strcmp(matches->type, node->type);
> -		if (matches->compatible[0])
> -			match &= __of_device_is_compatible(node,
> -							   matches->compatible);
> -		if (match)
> -			return matches;
> -		matches++;
> -	}
> +	cp = __of_get_property(node, "compatible", &cplen);
> +	do {
> +		const struct of_device_id *m = matches;
> +
> +		/* Check against matches with current compatible string */
> +		while (m->name[0] || m->type[0] || m->compatible[0]) {
> +			int match = 1;
> +			if (m->name[0])
> +				match &= node->name
> +					&& !strcmp(m->name, node->name);
> +			if (m->type[0])
> +				match &= node->type
> +					&& !strcmp(m->type, node->type);
> +			if (m->compatible[0])
> +				match &= cp
> +					&& !of_compat_cmp(m->compatible, cp,
> +							strlen(m->compatible));
> +			if (match)
> +				return m;
> +			m++;
> +		}
> +
> +		/* Get node's next compatible string */ 
> +		if (cp) {
> +			l = strlen(cp) + 1;
> +			cp += l;
> +			cplen -= l;
> +		}
> +	} while (cp && (cplen > 0));
> +
>  	return NULL;
>  }
>  
> @@ -757,7 +775,10 @@ const struct of_device_id *__of_match_node(const struct of_device_id *matches,
>   *	@matches:	array of of device match structures to search in
>   *	@node:		the of device structure to match against
>   *
> - *	Low level utility function used by device matching.
> + *	Low level utility function used by device matching. Matching order
> + *	is to compare each of the node's compatibles with all given matches
> + *	first. This implies node's compatible is sorted from specific to
> + *	generic while matches can be in any order.
>   */
>  const struct of_device_id *of_match_node(const struct of_device_id *matches,
>  					 const struct device_node *node)
>
Sebastian Hesselbarth Dec. 3, 2013, 10:55 p.m. UTC | #2
On 12/03/2013 09:14 PM, Meelis Roos wrote:
>> Currently, of_match_node compares each given match against all node's
>> compatible strings with of_device_is_compatible.
>>
>> To achieve multiple compatible strings per node with ordering from
>> specific to generic, this requires given matches to be ordered from
>> specific to generic. For most of the drivers this is not true and also
>> an alphabetical ordering is more sane there.
>>
>> Therefore, this patch modifies of_match_node to match each of the node's
>> compatible strings against all given matches first, before checking the
>> next compatible string. This implies that node's compatibles are ordered
>> from specific to generic while given matches can be in any order.
>
> I think I am on the CC: list because of a CPU detection problem report
> on sparc64 (183912d352a242a276a7877852f107459a13aff9 (of: move
> of_get_cpu_node implementation to DT core library) caused trouble and

The reason you are on Cc is that Thierry added you on last patch
version. I cannot see how above commit should be related with this
one, but maybe Thierry can comment on it.

> was reverted). So while your V2 patch does not cause any visible harm on
> the same Sun E3500, my gut feeling is that an additional patch would be
> needed to actually test it (a patch like
> 183912d352a242a276a7877852f107459a13aff9).

This patch deals with matching a node with more than one compatible
string on a (unordered) list of matches. Although not related to your
issue, it is good to hear that it causes no harm on DT-mature archs :)

I tested it with ARM and l2x0 cache controllers, where the specific
of_device_id (marvell,tauros3-cache) is sorted after the generic
one (arm,pl310-cache). The corresponding node's property is
compatible = "marvell,tauros3-cache", "arm,pl310-cache".

Without this patch, of_match_node always hits the first match that
equals _any_ of the above compatible strings. With this patch, it
hits the matches _in order_ of the compatible strings.

> Is this correct or am I missing something?

Thierry?

>> Signed-off-by: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
>> ---
>> Changelog:
>> v1->v2:
>> - Allow checks against nodes with no compatible (Reported by Rob Herring)
>> - Add some comments
>>
>> Cc: Grant Likely <grant.likely@linaro.org>
>> Cc: Rob Herring <rob.herring@calxeda.com>
>> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
>> Cc: Russell King <linux@arm.linux.org.uk>
>> Cc: Thierry Reding <thierry.reding@gmail.com>
>> Cc: Meelis Roos <mroos@linux.ee>
>> Cc: Marc Kleine-Budde <mkl@pengutronix.de>
>> Cc: Scott Wood <scottwood@freescale.com>
>> Cc: devicetree@vger.kernel.org
>> Cc: linux-arm-kernel@lists.infradead.org
>> Cc: linux-kernel@vger.kernel.org
>> ---
>>   drivers/of/base.c |   53 +++++++++++++++++++++++++++++++++++++----------------
>>   1 files changed, 37 insertions(+), 16 deletions(-)
>>
>> diff --git a/drivers/of/base.c b/drivers/of/base.c
>> index f807d0e..8d007d8 100644
>> --- a/drivers/of/base.c
>> +++ b/drivers/of/base.c
>> @@ -731,24 +731,42 @@ static
>>   const struct of_device_id *__of_match_node(const struct of_device_id *matches,
>>   					   const struct device_node *node)
>>   {
>> +	const char *cp;
>> +	int cplen, l;
>> +
>>   	if (!matches)
>>   		return NULL;
>>
>> -	while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
>> -		int match = 1;
>> -		if (matches->name[0])
>> -			match &= node->name
>> -				&& !strcmp(matches->name, node->name);
>> -		if (matches->type[0])
>> -			match &= node->type
>> -				&& !strcmp(matches->type, node->type);
>> -		if (matches->compatible[0])
>> -			match &= __of_device_is_compatible(node,
>> -							   matches->compatible);
>> -		if (match)
>> -			return matches;
>> -		matches++;
>> -	}
>> +	cp = __of_get_property(node, "compatible", &cplen);
>> +	do {
>> +		const struct of_device_id *m = matches;
>> +
>> +		/* Check against matches with current compatible string */
>> +		while (m->name[0] || m->type[0] || m->compatible[0]) {
>> +			int match = 1;
>> +			if (m->name[0])
>> +				match &= node->name
>> +					&& !strcmp(m->name, node->name);
>> +			if (m->type[0])
>> +				match &= node->type
>> +					&& !strcmp(m->type, node->type);
>> +			if (m->compatible[0])
>> +				match &= cp
>> +					&& !of_compat_cmp(m->compatible, cp,
>> +							strlen(m->compatible));
>> +			if (match)
>> +				return m;
>> +			m++;
>> +		}
>> +
>> +		/* Get node's next compatible string */
>> +		if (cp) {
>> +			l = strlen(cp) + 1;
>> +			cp += l;
>> +			cplen -= l;
>> +		}
>> +	} while (cp && (cplen > 0));
>> +
>>   	return NULL;
>>   }
>>
>> @@ -757,7 +775,10 @@ const struct of_device_id *__of_match_node(const struct of_device_id *matches,
>>    *	@matches:	array of of device match structures to search in
>>    *	@node:		the of device structure to match against
>>    *
>> - *	Low level utility function used by device matching.
>> + *	Low level utility function used by device matching. Matching order
>> + *	is to compare each of the node's compatibles with all given matches
>> + *	first. This implies node's compatible is sorted from specific to
>> + *	generic while matches can be in any order.
>>    */
>>   const struct of_device_id *of_match_node(const struct of_device_id *matches,
>>   					 const struct device_node *node)
>>
>
Thierry Reding Dec. 4, 2013, 9:40 a.m. UTC | #3
On Tue, Dec 03, 2013 at 11:55:07PM +0100, Sebastian Hesselbarth wrote:
> On 12/03/2013 09:14 PM, Meelis Roos wrote:
> >>Currently, of_match_node compares each given match against all node's
> >>compatible strings with of_device_is_compatible.
> >>
> >>To achieve multiple compatible strings per node with ordering from
> >>specific to generic, this requires given matches to be ordered from
> >>specific to generic. For most of the drivers this is not true and also
> >>an alphabetical ordering is more sane there.
> >>
> >>Therefore, this patch modifies of_match_node to match each of the node's
> >>compatible strings against all given matches first, before checking the
> >>next compatible string. This implies that node's compatibles are ordered
> >>from specific to generic while given matches can be in any order.
> >
> >I think I am on the CC: list because of a CPU detection problem report
> >on sparc64 (183912d352a242a276a7877852f107459a13aff9 (of: move
> >of_get_cpu_node implementation to DT core library) caused trouble and
> 
> The reason you are on Cc is that Thierry added you on last patch
> version. I cannot see how above commit should be related with this
> one, but maybe Thierry can comment on it.
> 
> >was reverted). So while your V2 patch does not cause any visible harm on
> >the same Sun E3500, my gut feeling is that an additional patch would be
> >needed to actually test it (a patch like
> >183912d352a242a276a7877852f107459a13aff9).
> 
> This patch deals with matching a node with more than one compatible
> string on a (unordered) list of matches. Although not related to your
> issue, it is good to hear that it causes no harm on DT-mature archs :)
> 
> I tested it with ARM and l2x0 cache controllers, where the specific
> of_device_id (marvell,tauros3-cache) is sorted after the generic
> one (arm,pl310-cache). The corresponding node's property is
> compatible = "marvell,tauros3-cache", "arm,pl310-cache".
> 
> Without this patch, of_match_node always hits the first match that
> equals _any_ of the above compatible strings. With this patch, it
> hits the matches _in order_ of the compatible strings.
> 
> >Is this correct or am I missing something?
> 
> Thierry?

I added Meelis on Cc because he found a regression with my original
proposal (107a84e61cdd "of: match by compatible property first"). That
got later reverted in commit bc51b0c22ceb (Revert "of: match by
compatible property first"). Here's the commit message for reference:

commit bc51b0c22ceb
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Tue Jul 10 12:49:32 2012 -0700

    Revert "of: match by compatible property first"

    This reverts commit 107a84e61cdd3406c842a0e4be7efffd3a05dba6.

    Meelis Roos reports a regression since 3.5-rc5 that stops Sun Fire V100
    and Sun Netra X1 sparc64 machines from booting, hanging after enabling
    serial console.  He bisected it to commit 107a84e61cdd.

    Rob Herring explains:
     "The problem is match combinations of compatible plus name and/or type
      fail to match correctly.  I have a fix for this, but given how late it
      is for 3.5 I think it is best to revert this for now.  There could be
      other cases that rely on the current although wrong behavior.  I will
      post an updated version for 3.6."

    Bisected-and-reported-by: Meelis Roos <mroos@linux.ee>
    Requested-by: Rob Herring <rob.herring@calxeda.com>
    Cc: Thierry Reding <thierry.reding@avionic-design.de>
    Cc: Grant Likely <grant.likely@secretlab.ca>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

So if Meelis still has access to the Sun Fire V100 and Sun Netra X1
machines that regressed last time around, it'd be great to get this
patch tested on them to verify that it indeed fixes the problem and
doesn't regress.

Thierry
Meelis Roos Dec. 4, 2013, 1:08 p.m. UTC | #4
> I added Meelis on Cc because he found a regression with my original
> proposal (107a84e61cdd "of: match by compatible property first"). That
> got later reverted in commit bc51b0c22ceb (Revert "of: match by
> compatible property first"). Here's the commit message for reference:
> 
> commit bc51b0c22ceb
> Author: Linus Torvalds <torvalds@linux-foundation.org>
> Date:   Tue Jul 10 12:49:32 2012 -0700
> 
>     Revert "of: match by compatible property first"
> 
>     This reverts commit 107a84e61cdd3406c842a0e4be7efffd3a05dba6.
> 
>     Meelis Roos reports a regression since 3.5-rc5 that stops Sun Fire V100
>     and Sun Netra X1 sparc64 machines from booting, hanging after enabling
>     serial console.  He bisected it to commit 107a84e61cdd.
> 
>     Rob Herring explains:
>      "The problem is match combinations of compatible plus name and/or type
>       fail to match correctly.  I have a fix for this, but given how late it
>       is for 3.5 I think it is best to revert this for now.  There could be
>       other cases that rely on the current although wrong behavior.  I will
>       post an updated version for 3.6."
> 
>     Bisected-and-reported-by: Meelis Roos <mroos@linux.ee>
>     Requested-by: Rob Herring <rob.herring@calxeda.com>
>     Cc: Thierry Reding <thierry.reding@avionic-design.de>
>     Cc: Grant Likely <grant.likely@secretlab.ca>
>     Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
> 
> So if Meelis still has access to the Sun Fire V100 and Sun Netra X1
> machines that regressed last time around, it'd be great to get this
> patch tested on them to verify that it indeed fixes the problem and
> doesn't regress.

OK, I had forgotten about that.

Tested successfully on the same Sun Fire V100 and Sun Netra X1, on top 
of 3.13-rc2. The sunsu console is detected fine and I see no problem for 
now.
Thierry Reding Dec. 4, 2013, 2:04 p.m. UTC | #5
On Wed, Dec 04, 2013 at 03:08:39PM +0200, Meelis Roos wrote:
> > I added Meelis on Cc because he found a regression with my original
> > proposal (107a84e61cdd "of: match by compatible property first"). That
> > got later reverted in commit bc51b0c22ceb (Revert "of: match by
> > compatible property first"). Here's the commit message for reference:
> > 
> > commit bc51b0c22ceb
> > Author: Linus Torvalds <torvalds@linux-foundation.org>
> > Date:   Tue Jul 10 12:49:32 2012 -0700
> > 
> >     Revert "of: match by compatible property first"
> > 
> >     This reverts commit 107a84e61cdd3406c842a0e4be7efffd3a05dba6.
> > 
> >     Meelis Roos reports a regression since 3.5-rc5 that stops Sun Fire V100
> >     and Sun Netra X1 sparc64 machines from booting, hanging after enabling
> >     serial console.  He bisected it to commit 107a84e61cdd.
> > 
> >     Rob Herring explains:
> >      "The problem is match combinations of compatible plus name and/or type
> >       fail to match correctly.  I have a fix for this, but given how late it
> >       is for 3.5 I think it is best to revert this for now.  There could be
> >       other cases that rely on the current although wrong behavior.  I will
> >       post an updated version for 3.6."
> > 
> >     Bisected-and-reported-by: Meelis Roos <mroos@linux.ee>
> >     Requested-by: Rob Herring <rob.herring@calxeda.com>
> >     Cc: Thierry Reding <thierry.reding@avionic-design.de>
> >     Cc: Grant Likely <grant.likely@secretlab.ca>
> >     Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
> > 
> > So if Meelis still has access to the Sun Fire V100 and Sun Netra X1
> > machines that regressed last time around, it'd be great to get this
> > patch tested on them to verify that it indeed fixes the problem and
> > doesn't regress.
> 
> OK, I had forgotten about that.

Yeah, I had always meant to come back to it, but that never quite
happened, so I'm all the more glad that Sebastian took over.

> Tested successfully on the same Sun Fire V100 and Sun Netra X1, on top 
> of 3.13-rc2. The sunsu console is detected fine and I see no problem for 
> now.

Excellent. Thanks for testing!

Rob, I guess this should be reasonable safe then to take for a spin in
linux-next?

Thierry
Rob Herring Dec. 4, 2013, 7:23 p.m. UTC | #6
On Tue, Dec 3, 2013 at 7:52 AM, Sebastian Hesselbarth
<sebastian.hesselbarth@gmail.com> wrote:
> Currently, of_match_node compares each given match against all node's
> compatible strings with of_device_is_compatible.
>
> To achieve multiple compatible strings per node with ordering from
> specific to generic, this requires given matches to be ordered from
> specific to generic. For most of the drivers this is not true and also
> an alphabetical ordering is more sane there.
>
> Therefore, this patch modifies of_match_node to match each of the node's
> compatible strings against all given matches first, before checking the
> next compatible string. This implies that node's compatibles are ordered
> from specific to generic while given matches can be in any order.
>
> Signed-off-by: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
> ---
> Changelog:
> v1->v2:
> - Allow checks against nodes with no compatible (Reported by Rob Herring)
> - Add some comments
>
> Cc: Grant Likely <grant.likely@linaro.org>
> Cc: Rob Herring <rob.herring@calxeda.com>
> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
> Cc: Russell King <linux@arm.linux.org.uk>
> Cc: Thierry Reding <thierry.reding@gmail.com>
> Cc: Meelis Roos <mroos@linux.ee>
> Cc: Marc Kleine-Budde <mkl@pengutronix.de>
> Cc: Scott Wood <scottwood@freescale.com>
> Cc: devicetree@vger.kernel.org
> Cc: linux-arm-kernel@lists.infradead.org
> Cc: linux-kernel@vger.kernel.org
> ---
>  drivers/of/base.c |   53 +++++++++++++++++++++++++++++++++++++----------------
>  1 files changed, 37 insertions(+), 16 deletions(-)

Applied.

Rob
diff mbox

Patch

diff --git a/drivers/of/base.c b/drivers/of/base.c
index f807d0e..8d007d8 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -731,24 +731,42 @@  static
 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
 					   const struct device_node *node)
 {
+	const char *cp;
+	int cplen, l;
+
 	if (!matches)
 		return NULL;
 
-	while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
-		int match = 1;
-		if (matches->name[0])
-			match &= node->name
-				&& !strcmp(matches->name, node->name);
-		if (matches->type[0])
-			match &= node->type
-				&& !strcmp(matches->type, node->type);
-		if (matches->compatible[0])
-			match &= __of_device_is_compatible(node,
-							   matches->compatible);
-		if (match)
-			return matches;
-		matches++;
-	}
+	cp = __of_get_property(node, "compatible", &cplen);
+	do {
+		const struct of_device_id *m = matches;
+
+		/* Check against matches with current compatible string */
+		while (m->name[0] || m->type[0] || m->compatible[0]) {
+			int match = 1;
+			if (m->name[0])
+				match &= node->name
+					&& !strcmp(m->name, node->name);
+			if (m->type[0])
+				match &= node->type
+					&& !strcmp(m->type, node->type);
+			if (m->compatible[0])
+				match &= cp
+					&& !of_compat_cmp(m->compatible, cp,
+							strlen(m->compatible));
+			if (match)
+				return m;
+			m++;
+		}
+
+		/* Get node's next compatible string */ 
+		if (cp) {
+			l = strlen(cp) + 1;
+			cp += l;
+			cplen -= l;
+		}
+	} while (cp && (cplen > 0));
+
 	return NULL;
 }
 
@@ -757,7 +775,10 @@  const struct of_device_id *__of_match_node(const struct of_device_id *matches,
  *	@matches:	array of of device match structures to search in
  *	@node:		the of device structure to match against
  *
- *	Low level utility function used by device matching.
+ *	Low level utility function used by device matching. Matching order
+ *	is to compare each of the node's compatibles with all given matches
+ *	first. This implies node's compatible is sorted from specific to
+ *	generic while matches can be in any order.
  */
 const struct of_device_id *of_match_node(const struct of_device_id *matches,
 					 const struct device_node *node)