Skip to contentSkip to conversation

byteorder: force in-place endian conversion to always evaluate args

PATCH In review Harvey Harrison 4 participants 8 messages Jul 25, 2008 – Jul 26, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 byteorder: force in-place endian conversion to always evaluate args

David Miller reported breakage in ide when the in-place byteorder helpers were used as the macros do not always evaluate their args which led to an infinite loop.

Just make them functions to ensure they always do so.

---
 include/linux/byteorder/big_endian.h    |   60 ++++++++++++++++++++++++------
 include/linux/byteorder/little_endian.h |   60 ++++++++++++++++++++++++------
 2 files changed, 96 insertions(+), 24 deletions(-)
include/linux/byteorder/big_endian.h +48 -12
index 961ed4b..b53ccd0 100644
@@ -88,18 +88,54 @@ static inline __u16 __be16_to_cpup(const __be16 *p)
 {
 	return (__force __u16)*p;
 }
-#define __cpu_to_le64s(x) __swab64s((x))
-#define __le64_to_cpus(x) __swab64s((x))
-#define __cpu_to_le32s(x) __swab32s((x))
-#define __le32_to_cpus(x) __swab32s((x))
-#define __cpu_to_le16s(x) __swab16s((x))
-#define __le16_to_cpus(x) __swab16s((x))
-#define __cpu_to_be64s(x) do {} while (0)
-#define __be64_to_cpus(x) do {} while (0)
-#define __cpu_to_be32s(x) do {} while (0)
-#define __be32_to_cpus(x) do {} while (0)
-#define __cpu_to_be16s(x) do {} while (0)
-#define __be16_to_cpus(x) do {} while (0)
+
+static inline void __cpu_to_le64s(__u64 *p)
+{
+	__swab64s(x);
+}
+
+static inline void __le64_to_cpus(__u64 *p)
+{
+	__swab64s(x);
+}
+
+static inline void __cpu_to_le32s(__u32 *p)
+{
+	__swab32s(x);
+}
+
+static inline void __le32_to_cpus(__u32 *p)
+{
+	__swab32s(x);
+}
+
+static inline void __cpu_to_le16s(__u16 *p)
+{
+	__swab16s(x);
+}
+
+static inline void __le16_to_cpus(__u16 *p)
+{
+	__swab16s(x);
+}
+
+static inline void __cpu_to_be64s(__u64 *p)
+{}
+
+static inline void __be64_to_cpus(__u64 *p)
+{}
+
+static inline void __cpu_to_be32s(__u32 *p)
+{}
+
+static inline void __be32_to_cpus(__u32 *p)
+{}
+
+static inline void __cpu_to_be16s(__u16 *p)
+{}
+
+static inline void __be16_to_cpus(__u16 *p)
+{}
 
 #ifdef __KERNEL__
 #include <linux/byteorder/generic.h>
include/linux/byteorder/little_endian.h +48 -12

66 lines not on this page · raw message

Signature · 1 line
1.5.6.4.570.g052e
#1 lore raw
Quoted 7 lines · Harvey Harrison

David Miller reported breakage in ide when the in-place byteorder helpers were used as the macros do not always evaluate their args which led to an infinite loop.

Just make them functions to ensure they always do so.

-#define __cpu_to_be64s(x) do {} while (0)

For what it's worth, the way to write a macro like this:

#define __cpu_to_be64s(x) ((void)(x))

-hpa

#2 lore raw
Quoted 5 lines · H. Peter Anvin
Quoted 4 lines · Harvey Harrison

David Miller reported breakage in ide when the in-place byteorder helpers were used as the macros do not always evaluate their args which led to an infinite loop.

Quoted 7 lines
Quoted 3 lines

Just make them functions to ensure they always do so.

-#define __cpu_to_be64s(x) do {} while (0)

For what it's worth, the way to write a macro like this:

#define __cpu_to_be64s(x) ((void)(x))

If you've looked at the byteorder rework I've done in -mm, these get unified in a single include/linux/byteorder.h and look like:

static inline void __le16_to_cpus(__u16 *p)
{

#ifdef __BIG_ENDIAN
	__swab16s(p);

#endif
}

static inline void __be16_to_cpus(__u16 *p)
{

#ifdef __LITTLE_ENDIAN
	__swab16s(p);

#endif
}

...etc.

As you can now rely on __BIG/__LITTLE_ENDIAN being set reliably.

Harvey

#3 lore raw
Quoted 6 lines · Harvey Harrison
Quoted 3 lines

For what it's worth, the way to write a macro like this:

#define __cpu_to_be64s(x) ((void)(x))

If you've looked at the byteorder rework I've done in -mm, these get unified in a single include/linux/byteorder.h and look like:

[inline functions]

What I meant with the above, was that it seems poorly understood how to write an empty "function-like" macro.

Obviously, inline functions don't have this problem, and is generally better anyway (type-safe even in the empty condition, for example.)

-hpa

#4 lore raw
In reply to Harvey Harrison
Quoted 29 lines · Harvey Harrison

David Miller reported breakage in ide when the in-place byteorder helpers
were used as the macros do not always evaluate their args which led to an infinite loop.

Just make them functions to ensure they always do so.

---

  include/linux/byteorder/big_endian.h    |   60 ++++++++++++++++++++++++------
  include/linux/byteorder/little_endian.h |   60 ++++++++++++++++++++++++------
  2 files changed, 96 insertions(+), 24 deletions(-)

diff --git a/include/linux/byteorder/big_endian.h b/include/linux/byteorder/big_endian.h index 961ed4b..b53ccd0 100644
--- a/include/linux/byteorder/big_endian.h
+++ b/include/linux/byteorder/big_endian.h
@@ -88,18 +88,54 @@ static inline __u16 __be16_to_cpup(const __be16 *p)

  {
        return (__force __u16)*p;

}
-#define __cpu_to_le64s(x) __swab64s((x))
-#define __le64_to_cpus(x) __swab64s((x))
-#define __cpu_to_le32s(x) __swab32s((x))
-#define __le32_to_cpus(x) __swab32s((x))
-#define __cpu_to_le16s(x) __swab16s((x))
-#define __le16_to_cpus(x) __swab16s((x))
-#define __cpu_to_be64s(x) do {} while (0)
-#define __be64_to_cpus(x) do {} while (0)
-#define __cpu_to_be32s(x) do {} while (0)

Quoted 8 lines

-#define __be32_to_cpus(x) do {} while (0)
-#define __cpu_to_be16s(x) do {} while (0)
-#define __be16_to_cpus(x) do {} while (0)
+
+static inline void __cpu_to_le64s(__u64 *p)
+{
+ __swab64s(x);
+}

Shouldn't all the x's in the function bodies be p's? And I thought David already posted a macro version of this change along the lines of hpa's reply?

Thanks,
Nish

#5 lore raw
Quoted 39 lines · Nish Aravamudan
Quoted 29 lines · Harvey Harrison

David Miller reported breakage in ide when the in-place byteorder helpers
were used as the macros do not always evaluate their args which led to an infinite loop.

Just make them functions to ensure they always do so.

---

  include/linux/byteorder/big_endian.h    |   60 ++++++++++++++++++++++++------
  include/linux/byteorder/little_endian.h |   60 ++++++++++++++++++++++++------
  2 files changed, 96 insertions(+), 24 deletions(-)

diff --git a/include/linux/byteorder/big_endian.h b/include/linux/byteorder/big_endian.h index 961ed4b..b53ccd0 100644
--- a/include/linux/byteorder/big_endian.h
+++ b/include/linux/byteorder/big_endian.h
@@ -88,18 +88,54 @@ static inline __u16 __be16_to_cpup(const __be16 *p)

  {
        return (__force __u16)*p;

}
-#define __cpu_to_le64s(x) __swab64s((x))
-#define __le64_to_cpus(x) __swab64s((x))
-#define __cpu_to_le32s(x) __swab32s((x))
-#define __le32_to_cpus(x) __swab32s((x))
-#define __cpu_to_le16s(x) __swab16s((x))
-#define __le16_to_cpus(x) __swab16s((x))
-#define __cpu_to_be64s(x) do {} while (0)
-#define __be64_to_cpus(x) do {} while (0)
-#define __cpu_to_be32s(x) do {} while (0)

Quoted 8 lines

-#define __be32_to_cpus(x) do {} while (0)
-#define __cpu_to_be16s(x) do {} while (0)
-#define __be16_to_cpus(x) do {} while (0)
+
+static inline void __cpu_to_le64s(__u64 *p)
+{
+ __swab64s(x);
+}

Quoted 3 lines

Shouldn't all the x's in the function bodies be p's? And I thought David already posted a macro version of this change along the lines of hpa's reply?

*sigh*

Yes they should, I sent the wrong mbox after this failed to compile and fixed it.

Cheers,

Harvey

#6 lore raw
In reply to Harvey Harrison
Quoted 5 lines · Harvey Harrison

David Miller reported breakage in ide when the in-place byteorder helpers were used as the macros do not always evaluate their args which led to an infinite loop.

Just make them functions to ensure they always do so.

Of course the best thing would be to kill these macros entirely. In-place endianess conversions are bad idea.

#7 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Harvey Harrison

David Miller reported breakage in ide when the in-place byteorder helpers were used as the macros do not always evaluate their args which led to an infinite loop.

Just make them functions to ensure they always do so.

Of course the best thing would be to kill these macros entirely. In-place endianess conversions are bad idea.

It's better than seeing this:

	some_u32 = cpu_to_le32(some_u32);

But agreed, the 's' versions of the byteswapping api are a pretty good sign something could be done better.

Harvey