Skip to contentSkip to conversation

of: replace be32_to_cpu to be32_to_cpup

PATCH In review Phong Tran 6 participants 10 messages Apr 30, 2019 – May 1, 2019 Device tree

lore
Share & tools
lore

Phong Tran

author
#0 lore raw

PATCH 1/1 of: replace be32_to_cpu to be32_to_cpup

The cell is a pointer to __be32.
with the be32_to_cpu a lot of clang warning show that:

./include/linux/of.h:238:37: warning: multiple unsequenced modifications to 'cell' [-Wunsequenced]

                r = (r << 32) | be32_to_cpu(*(cell++));
                                                  ^~
./include/linux/byteorder/generic.h:95:21: note: expanded from macro
'be32_to_cpu'
                    ^
./include/uapi/linux/byteorder/little_endian.h:40:59: note: expanded
from macro '__be32_to_cpu'
                                                          ^
./include/uapi/linux/swab.h:118:21: note: expanded from macro '__swab32'
        ___constant_swab32(x) :                 \
                           ^
./include/uapi/linux/swab.h:18:12: note: expanded from macro
'___constant_swab32'
        (((__u32)(x) & (__u32)0x000000ffUL) << 24) |            \
                  ^
---
 include/linux/of.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/of.h +1 -1
index e240992e5cb6..1c35fc8f19b0 100644
@@ -235,7 +235,7 @@ static inline u64 of_read_number(const __be32 *cell, int size)
 {
 	u64 r = 0;
 	while (size--)
-		r = (r << 32) | be32_to_cpu(*(cell++));
+		r = (r << 32) | be32_to_cpup(cell++);
 	return r;
 }
 
Signature · 1 line
2.21.0
#1 lore raw

+ Nick and the list

Quoted 42 lines · Phong Tran

The cell is a pointer to __be32.
with the be32_to_cpu a lot of clang warning show that:

./include/linux/of.h:238:37: warning: multiple unsequenced modifications to 'cell' [-Wunsequenced]

                r = (r << 32) | be32_to_cpu(*(cell++));
                                                  ^~
./include/linux/byteorder/generic.h:95:21: note: expanded from macro
'be32_to_cpu'
                    ^
./include/uapi/linux/byteorder/little_endian.h:40:59: note: expanded
from macro '__be32_to_cpu'
                                                          ^
./include/uapi/linux/swab.h:118:21: note: expanded from macro '__swab32'
        ___constant_swab32(x) :                 \
                           ^
./include/uapi/linux/swab.h:18:12: note: expanded from macro
'___constant_swab32'
        (((__u32)(x) & (__u32)0x000000ffUL) << 24) |            \
                  ^
---
 include/linux/of.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/of.h +1 -1
index e240992e5cb6..1c35fc8f19b0 100644
@@ -235,7 +235,7 @@ static inline u64 of_read_number(const __be32 *cell, int size)
 {
 	u64 r = 0;
 	while (size--)
-		r = (r << 32) | be32_to_cpu(*(cell++));
+		r = (r << 32) | be32_to_cpup(cell++);
 	return r;
 }
 
Signature · 1 line
2.21.0

While the patch does remove the warning, I am not convinced that this isn't a clang bug based on my brief analysis here:

https://github.com/ClangBuiltLinux/linux/issues/460#issuecomment-487808008

However, I'm waiting for people smarter than I am to comment on whether that sounds correct or not.

I am not familiar with the various different big/little endian functions enough to review this but thank you for the patch!

Nathan

#2 lore raw
In reply to Phong Tran

From: Phong Tran

Quoted 32 lines

Sent: 30 April 2019 10:01
The cell is a pointer to __be32.
with the be32_to_cpu a lot of clang warning show that:

./include/linux/of.h:238:37: warning: multiple unsequenced modifications to 'cell' [-Wunsequenced]

                r = (r << 32) | be32_to_cpu(*(cell++));
                                                  ^~
./include/linux/byteorder/generic.h:95:21: note: expanded from macro
'be32_to_cpu'
                    ^
./include/uapi/linux/byteorder/little_endian.h:40:59: note: expanded
from macro '__be32_to_cpu'
                                                          ^
./include/uapi/linux/swab.h:118:21: note: expanded from macro '__swab32'
        ___constant_swab32(x) :                 \
                           ^
./include/uapi/linux/swab.h:18:12: note: expanded from macro
'___constant_swab32'
        (((__u32)(x) & (__u32)0x000000ffUL) << 24) |            \
                  ^
---
 include/linux/of.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/of.h +0 -0
index e240992e5cb6..1c35fc8f19b0 100644

include/linux/of.h static inline u64 of_read_number(const __be32 *cell, int size) · line 239

Quoted 6 lines
include/linux/of.h +1 -1
@@ -235,7 +235,7 @@ static inline u64 of_read_number(const __be32 *cell, int size)
 {
 	u64 r = 0;
 	while (size--)
-		r = (r << 32) | be32_to_cpu(*(cell++));
+		r = (r << 32) | be32_to_cpup(cell++);
 	return r;

That is a very strange loop.
It is probably equivalent to:

	r = be32_to_cpu(*cell);
	if (size)
		r = r << 32 | be32_to_cpu(cell[1]);
	return r;

In any case replacing the while with (say):

	for (; size--; cell++)
		r = (r << 32) | be32_to_cpu(*cell);

would remove the ambiguity.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

author
#3 lore raw

PATCH v2 1/1 of: fix clang -Wunsequenced for be32_to_cpu()

Now, make the loop explicit to avoid clang warning.

./include/linux/of.h:238:37: warning: multiple unsequenced modifications to 'cell' [-Wunsequenced]

                r = (r << 32) | be32_to_cpu(*(cell++));
                                                  ^~
./include/linux/byteorder/generic.h:95:21: note: expanded from macro
'be32_to_cpu'
                    ^
./include/uapi/linux/byteorder/little_endian.h:40:59: note: expanded
from macro '__be32_to_cpu'
                                                          ^
./include/uapi/linux/swab.h:118:21: note: expanded from macro '__swab32'
        ___constant_swab32(x) :                 \
                           ^
./include/uapi/linux/swab.h:18:12: note: expanded from macro
'___constant_swab32'
        (((__u32)(x) & (__u32)0x000000ffUL) << 24) |            \
                  ^
---
 include/linux/of.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/of.h +2 -2
index e240992e5cb6..71ca25ac01f6 100644
@@ -234,8 +234,8 @@ extern struct device_node *of_find_all_nodes(struct device_node *prev);
 static inline u64 of_read_number(const __be32 *cell, int size)
 {
 	u64 r = 0;
-	while (size--)
-		r = (r << 32) | be32_to_cpu(*(cell++));
+	for(; size--; cell++)
+		r = (r << 32) | be32_to_cpu(*cell);
 	return r;
 }
 
Signature · 1 line
2.21.0
#4 lore raw

Re: [PATCH V2] of: fix clang -Wunsequenced for be32_to_cpu()

Quoted 21 lines · Phong Tran

Now, make the loop explicit to avoid clang warning.

./include/linux/of.h:238:37: warning: multiple unsequenced modifications to 'cell' [-Wunsequenced]

                r = (r << 32) | be32_to_cpu(*(cell++));
                                                  ^~
./include/linux/byteorder/generic.h:95:21: note: expanded from macro
'be32_to_cpu'
                    ^
./include/uapi/linux/byteorder/little_endian.h:40:59: note: expanded
from macro '__be32_to_cpu'
                                                          ^
./include/uapi/linux/swab.h:118:21: note: expanded from macro '__swab32'
        ___constant_swab32(x) :                 \
                           ^
./include/uapi/linux/swab.h:18:12: note: expanded from macro
'___constant_swab32'
        (((__u32)(x) & (__u32)0x000000ffUL) << 24) |            \
                  ^
Quoted 1 line

Thanks for the patch.

Quoted 20 lines
---
 include/linux/of.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/of.h +2 -3
index e240992e5cb6..71ca25ac01f6 100644
@@ -234,8 +234,8 @@ extern struct device_node *of_find_all_nodes(struct device_node *prev);
 static inline u64 of_read_number(const __be32 *cell, int size)
 {
        u64 r = 0;
-       while (size--)
-               r = (r << 32) | be32_to_cpu(*(cell++));
+       for(; size--; cell++)
+               r = (r << 32) | be32_to_cpu(*cell);
        return r;
 }
 
--
Quoted 2 lines

2.21.0

--
Thanks,
~Nick Desaulniers

Reviewed-by
#5 lore raw

Re: [PATCH V2] of: fix clang -Wunsequenced for be32_to_cpu()

Quoted 23 lines · Nick Desaulniers
Quoted 21 lines · Phong Tran

Now, make the loop explicit to avoid clang warning.

./include/linux/of.h:238:37: warning: multiple unsequenced modifications to 'cell' [-Wunsequenced]

                r = (r << 32) | be32_to_cpu(*(cell++));
                                                  ^~
./include/linux/byteorder/generic.h:95:21: note: expanded from macro
'be32_to_cpu'
                    ^
./include/uapi/linux/byteorder/little_endian.h:40:59: note: expanded
from macro '__be32_to_cpu'
                                                          ^
./include/uapi/linux/swab.h:118:21: note: expanded from macro '__swab32'
        ___constant_swab32(x) :                 \
                           ^
./include/uapi/linux/swab.h:18:12: note: expanded from macro
'___constant_swab32'
        (((__u32)(x) & (__u32)0x000000ffUL) << 24) |            \
                  ^
Quoted 6 lines
Quoted 1 line

Thanks for the patch.

sent too soon...

Signature · 2 lines
Thanks,
~Nick Desaulniers
#6 lore raw

Re: [PATCH V2] of: fix clang -Wunsequenced for be32_to_cpu()

Quoted 10 lines · Nick Desaulniers
Quoted 4 lines · Nick Desaulniers

Thanks for the patch.

sent too soon...

We'll also need this for stable, can the maintainers please add the following tag if it's not too late:

to unbreak ppc back through at least 4.14.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#7 lore raw

Re: [PATCH V2] of: fix clang -Wunsequenced for be32_to_cpu()

Quoted 11 lines · Nick Desaulniers
Quoted 8 lines · Nick Desaulniers
Quoted 4 lines · Nick Desaulniers

Thanks for the patch.

Quoted 7 lines
Quoted 2 lines

sent too soon...

We'll also need this for stable, can the maintainers please add the following tag if it's not too late:

Applied with a whitespace fixup and stable tag.

Rob

#8 lore raw
In reply to Phong Tran

include/linux/of.h static inline u64 of_read_number(const __be32 *cell, int size) · line 239

Quoted 12 lines · Phong Tran
include/linux/of.h +1 -1
index e240992e5cb6..1c35fc8f19b0 100644
@@ -235,7 +235,7 @@ static inline u64 of_read_number(const __be32 *cell, int size)
 {
 	u64 r = 0;
 	while (size--)
-		r = (r << 32) | be32_to_cpu(*(cell++));
+		r = (r << 32) | be32_to_cpup(cell++);
 	return r;

This whole function looks odd. It could simply be replaced with calls to get_unaligned_be64 / get_unaligned_be32. Given that we have a lot of callers we can't easily do that, but at least we could try something like

static inline u64 of_read_number(const __be32 *cell, int size)

{
	WARN_ON_ONCE(size < 1);
	WARN_ON_ONCE(size > 2);

	if (size == 1)
		return get_unaligned_be32(cell);
	return get_unaligned_be64(cell);
}
author
#9 lore raw
Quoted 20 lines · Christoph Hellwig

include/linux/of.h static inline u64 of_read_number(const __be32 *cell, int size) · line 239

Quoted 12 lines · Phong Tran
include/linux/of.h +1 -1
index e240992e5cb6..1c35fc8f19b0 100644
@@ -235,7 +235,7 @@ static inline u64 of_read_number(const __be32 *cell, int size)
 {
      u64 r = 0;
      while (size--)
-             r = (r << 32) | be32_to_cpu(*(cell++));
+             r = (r << 32) | be32_to_cpup(cell++);
      return r;

This whole function looks odd. It could simply be replaced with calls to get_unaligned_be64 / get_unaligned_be32. Given that we have a lot of callers we can't easily do that, but at least we could try something like

It's risky. there are many callers of of_read_number(). There is suggestion from David
(https://lore.kernel.org/lkml/[email protected]/)
only changing the loop.

Quoted 9 lines

static inline u64 of_read_number(const __be32 *cell, int size)

{
        WARN_ON_ONCE(size < 1);
        WARN_ON_ONCE(size > 2);

        if (size == 1)
                return get_unaligned_be32(cell);
        return get_unaligned_be64(cell);
}

Thank you for your support.

Phong.