Implement byteswap and update references
PATCH v3 6/6 tools: Remove unnecessary header
In reply to Lin Liuxen/byteorder/little_endian.h is included but not used, Remove it.
No functional change.
---
Cc: Wei Liu <[email protected]>
Cc: Anthony PERARD <[email protected]>
Cc: Juergen Gross <[email protected]>
--- tools/libs/guest/xg_dom_decompress_unsafe_zstd.c | 1 - 1 file changed, 1 deletion(-)
tools/libs/guest/xg_dom_decompress_unsafe_zstd.c +0 -1
@@ -31,7 +31,6 @@ typedef uint64_t __be64;#define __BYTEORDER_HAS_U64__#define __TYPES_H__/* xen/types.h guard */ -#include "../../xen/include/xen/byteorder/little_endian.h" #define __ASM_UNALIGNED_H__/* asm/unaligned.h guard */ #include "../../xen/include/xen/unaligned.h" #include "../../xen/include/xen/xxhash.h"
Signature · 1 line
2.27.0
v3 cover Implement byteswap and update references
The swab() is massively over complicated
Simplify it with compiler builtins and fallback to plain C function if undefined.
Update components to switch to this new swap bytes.
Lin Liu (6):
xen: implement byteswap
crypto/vmac: Simplify code with byteswap
arm64/find_next_bit: Remove ext2_swab()
xen: Switch to byteswap
byteorder: Remove byteorder
tools: Remove unnecessary header
.../guest/xg_dom_decompress_unsafe_zstd.c | 1 - xen/arch/arm/arm64/lib/find_next_bit.c | 36 +--- xen/arch/arm/include/asm/byteorder.h | 14 +- xen/arch/x86/include/asm/byteorder.h | 34 +--- xen/common/device_tree.c | 44 ++--- xen/common/libelf/libelf-private.h | 6 +- xen/common/xz/private.h | 2 +- xen/crypto/vmac.c | 76 +------- xen/include/xen/byteorder.h | 56 ++++++ xen/include/xen/byteorder/big_endian.h | 102 ---------- xen/include/xen/byteorder/generic.h | 68 ------- xen/include/xen/byteorder/little_endian.h | 102 ---------- xen/include/xen/byteorder/swab.h | 183 ------------------ xen/include/xen/byteswap.h | 44 +++++ xen/include/xen/compiler.h | 12 ++ xen/include/xen/unaligned.h | 24 +-- 16 files changed, 167 insertions(+), 637 deletions(-) create mode 100644 xen/include/xen/byteorder.h delete mode 100644 xen/include/xen/byteorder/big_endian.h delete mode 100644 xen/include/xen/byteorder/generic.h delete mode 100644 xen/include/xen/byteorder/little_endian.h delete mode 100644 xen/include/xen/byteorder/swab.h create mode 100644 xen/include/xen/byteswap.h
Signature · 1 line
2.27.0
PATCH v3 3/6 arm64/find_next_bit: Remove ext2_swab()
ext2 has nothing to do with this logic. Clean up the code with xen/byteswap.h which now has an unsigned long helper.
No functional change.
---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Bertrand Marquis <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>
This patche is not well tested without running environment for arm
--- xen/arch/arm/arm64/lib/find_next_bit.c | 36 +++++--------------------- 1 file changed, 6 insertions(+), 30 deletions(-)
xen/arch/arm/arm64/lib/find_next_bit.c +6 -30
@@ -161,30 +161,6 @@ EXPORT_SYMBOL(find_first_zero_bit);#ifdef __BIG_ENDIAN -/* include/linux/byteorder does not support "unsigned long" type */ -static inline unsigned long ext2_swabp (const unsigned long * x) -{ -#if BITS_PER_LONG ==64 -return (unsigned long )__swab64p ((u64 *) x); -#elif BITS_PER_LONG ==32 -return (unsigned long )__swab32p ((u32 *) x); -#else -#error BITS_PER_LONG not defined -#endif -} - -/* include/linux/byteorder doesn't support "unsigned long" type */ -static inline unsigned long ext2_swab (const unsigned long y) -{ -#if BITS_PER_LONG ==64 -return (unsigned long )__swab64 ((u64 ) y); -#elif BITS_PER_LONG ==32 -return (unsigned long )__swab32 ((u32 ) y); -#else -#error BITS_PER_LONG not defined -#endif -} -#ifndef find_next_zero_bit_leunsigned long find_next_zero_bit_le (const void *addr,unsigned long size,unsigned long offset)
@@ -199,7 +175,7 @@ unsigned long find_next_zero_bit_le(const void *addr, unsigned size -= result; offset &= (BITS_PER_LONG -1UL );if (offset) { - tmp =ext2_swabp (p++); + tmp =bswap_ul (*p++); tmp |= (~0UL >> (BITS_PER_LONG - offset));if (size <BITS_PER_LONG ) goto found_first;
@@ -217,7 +193,7 @@ unsigned long find_next_zero_bit_le(const void *addr, unsigned }if (!size)return result; - tmp =ext2_swabp (p); + tmp =bswap_ul (*p); found_first: tmp |= ~0UL << size;if (tmp == ~0UL )/* Are any bits zero? */
@@ -226,7 +202,7 @@ found_middle:return result +ffz (tmp); found_middle_swap: - return result + ffz(ext2_swab(tmp)); + return result + ffz(bswap_ul(tmp)); }EXPORT_SYMBOL (find_next_zero_bit_le);#endif
@@ -245,7 +221,7 @@ unsigned long find_next_bit_le(const void *addr, unsigned size -= result; offset &= (BITS_PER_LONG -1UL );if (offset) { - tmp =ext2_swabp (p++); + tmp =bswap_ul (*p++); tmp &= (~0UL << offset);if (size <BITS_PER_LONG ) goto found_first;
@@ -264,7 +240,7 @@ unsigned long find_next_bit_le(const void *addr, unsigned }if (!size)return result; - tmp =ext2_swabp (p); + tmp =bswap_ul (*p); found_first: tmp &= (~0UL >> (BITS_PER_LONG - size));if (tmp ==0UL )/* Are any bits set? */
@@ -273,7 +249,7 @@ found_middle:return result +__ffs (tmp); found_middle_swap: - return result + __ffs(ext2_swab(tmp)); + return result + __ffs(bswap_ul(tmp)); }EXPORT_SYMBOL (find_next_bit_le);#endif
Signature · 1 line
2.27.0
PATCH v3 2/6 crypto/vmac: Simplify code with byteswap
In reply to Lin LiuThis file has its own implementation of swap bytes. Clean up the code with xen/byteswap.h.
No functional change.
---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>
--- xen/crypto/vmac.c | 76 ++--------------------------------------------- 1 file changed, 3 insertions(+), 73 deletions(-)
xen/crypto/vmac.c +3 -73
@@ -8,6 +8,7 @@/* start for Xen */ #include <xen/init.h> +#include <xen/byteswap.h> #include <xen/types.h> #include <xen/lib.h> #include <crypto/vmac.h>
@@ -50,7 +51,6 @@ const uint64_t mpoly = UINT64_C(0x1fffffff1fffffff); /* Poly key mask */* MUL64: 64x64->128-bit multiplication * PMUL64: assumes top bits cleared on inputs * ADD128: 128x128->128-bit addition -* GET_REVERSED_64: load and byte-reverse 64-bit word * ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
@@ -68,22 +68,6 @@ const uint64_t mpoly = UINT64_C(0x1fffffff1fffffff); /* Poly key mask */#define PMUL64 MUL64 -#define GET_REVERSED_64 (p) \ - ({uint64_t x; \ - asm ("bswapq %0" : "=r" (x) : "0"(*(uint64_t *)(p))); x;}) - -/* ----------------------------------------------------------------------- */ -#elif (__GNUC__ && __i386__) -/* ----------------------------------------------------------------------- */ - -#define GET_REVERSED_64 (p) \ - ({ uint64_t x; \ - uint32_t *tp = (uint32_t *)(p); \ - asm ("bswap %%edx\n\t" \ - "bswap %%eax" \ - : "=A"(x) \ - : "a"(tp[1]), "d"(tp[0])); \ - x; })/* ----------------------------------------------------------------------- */ #elif (__GNUC__ && __ppc64__)
@@ -103,37 +87,6 @@ const uint64_t mpoly = UINT64_C(0x1fffffff1fffffff); /* Poly key mask */#define PMUL64 MUL64 -#define GET_REVERSED_64 (p) \ - ({ uint32_t hi, lo, *_p = (uint32_t *)(p); \ - asm volatile ("lwbrx %0, %1, %2" : "=r"(lo) : "b%"(0), "r"(_p) ); \ - asm volatile ("lwbrx %0, %1, %2" : "=r"(hi) : "b%"(4), "r"(_p) ); \ - ((uint64_t)hi << 32) | (uint64_t)lo; } ) - -/* ----------------------------------------------------------------------- */ -#elif (__GNUC__ && (__ppc__ || __PPC__)) -/* ----------------------------------------------------------------------- */ - -#define GET_REVERSED_64 (p) \ - ({ uint32_t hi, lo, *_p = (uint32_t *)(p); \ - asm volatile ("lwbrx %0, %1, %2" : "=r"(lo) : "b%"(0), "r"(_p) ); \ - asm volatile ("lwbrx %0, %1, %2" : "=r"(hi) : "b%"(4), "r"(_p) ); \ - ((uint64_t)hi << 32) | (uint64_t)lo; } ) - -/* ----------------------------------------------------------------------- */ -#elif (__GNUC__ && (__ARMEL__ || __ARM__)) -/* ----------------------------------------------------------------------- */ - -#define bswap32 (v) \ -({ uint32_t tmp,out; \ - asm volatile( \ - "eor %1, %2, %2, ror #16\n" \ - "bic %1, %1, #0x00ff0000\n" \ - "mov %0, %2, ror #8\n" \ - "eor %0, %0, %1, lsr #8" \ - : "=r" (out), "=&r" (tmp) \ - : "r" (v)); \ - out;}) -/* ----------------------------------------------------------------------- */ #elif _MSC_VER/* ----------------------------------------------------------------------- */
@@ -154,11 +107,6 @@ const uint64_t mpoly = UINT64_C(0x1fffffff1fffffff); /* Poly key mask */ (rh) += (ih) + ((rl) < (_il)); \ } -#if _MSC_VER >=1300 -#define GET_REVERSED_64 (p) _byteswap_uint64(*(uint64_t *)(p)) -#pragma intrinsic(_byteswap_uint64) -#endif -#if _MSC_VER >=1400 && \ (!defined(__INTEL_COMPILER) || __INTEL_COMPILER >=1000 )#define MUL32 (i1,i2) (__emulu((uint32_t)(i1),(uint32_t)(i2)))
@@ -219,24 +167,6 @@ const uint64_t mpoly = UINT64_C(0x1fffffff1fffffff); /* Poly key mask */ }#endif -#ifndef GET_REVERSED_64 -#ifndef bswap64 -#ifndef bswap32 -#define bswap32 (x) \ - ({ uint32_t bsx = (x); \ - ((((bsx) & 0xff000000u) >> 24) | (((bsx) & 0x00ff0000u) >> 8) | \ - (((bsx) & 0x0000ff00u) << 8) | (((bsx) & 0x000000ffu) << 24)); }) -#endif -#define bswap64 (x) \ - ({ union { uint64_t ll; uint32_t l[2]; } w, r; \ - w.ll = (x); \ - r.l[0] = bswap32 (w.l[1]); \ - r.l[1] = bswap32 (w.l[0]); \ - r.ll; }) -#endif -#define GET_REVERSED_64 (p) bswap64(*(uint64_t *)(p)) -#endif -/* ----------------------------------------------------------------------- */ #if (VMAC_PREFER_BIG_ENDIAN )
@@ -247,9 +177,9 @@ const uint64_t mpoly = UINT64_C(0x1fffffff1fffffff); /* Poly key mask */#if (VMAC_ARCH_BIG_ENDIAN )# define get64BE (ptr) (*(uint64_t *)(ptr)) -# define get64LE (ptr) GET_REVERSED_64(ptr) +# define get64LE (ptr) bswap64(*(uint64_t *)(ptr))#else /* assume little-endian */ -# define get64BE (ptr) GET_REVERSED_64(ptr) +# define get64BE (ptr) bswap64(*(uint64_t *)(ptr))# define get64LE (ptr) (*(uint64_t *)(ptr))#endif
Signature · 1 line
2.27.0
PATCH v3 4/6 xen: Switch to byteswap
In reply to Lin LiuUpdate to use byteswap to swap bytes.
No functional change.
---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
Changes in v3:
- Update xen/common/device_tree.c to use be32_to_cpu
- Keep const in type cast in unaligned.h
--- xen/common/device_tree.c | 44 +++++++++++++++--------------- xen/common/libelf/libelf-private.h | 6 ++-- xen/common/xz/private.h | 2 +- xen/include/xen/unaligned.h | 24 ++++++++-------- 4 files changed, 38 insertions(+), 38 deletions(-)
xen/common/device_tree.c +22 -22
@@ -171,7 +171,7 @@ bool_t dt_property_read_u32(const struct dt_device_node *np,if ( !val || len <sizeof (*out_value) )return 0 ; - *out_value =be32_to_cpup (val); + *out_value =be32_to_cpu (*val);return 1 ; }
@@ -264,7 +264,7 @@ int dt_property_read_variable_u32_array(const struct dt_device_node *np, count = sz;while ( count-- ) - *out_values++ =be32_to_cpup (val++); + *out_values++ =be32_to_cpu (*val++);return sz; }
@@ -490,7 +490,7 @@ static int __dt_n_addr_cells(const struct dt_device_node *np, bool_t parent) ip =dt_get_property (np,"#address-cells" ,NULL );if ( ip ) -return be32_to_cpup (ip); +return be32_to_cpu (*ip); }while ( np->parent );/* No #address-cells property for the root node */ return DT_ROOT_NODE_ADDR_CELLS_DEFAULT ;
@@ -507,7 +507,7 @@ int __dt_n_size_cells(const struct dt_device_node *np, bool_t parent) ip =dt_get_property (np,"#size-cells" ,NULL );if ( ip ) -return be32_to_cpup (ip); +return be32_to_cpu (*ip); }while ( np->parent );/* No #address-cells property for the root node */ return DT_ROOT_NODE_SIZE_CELLS_DEFAULT ;
@@ -660,7 +660,7 @@ static void dt_bus_pci_count_cells(const struct dt_device_node *np,static unsigned int dt_bus_pci_get_flags (const __be32 *addr) {unsigned int flags =0 ; -u32 w =be32_to_cpup (addr); +u32 w =be32_to_cpu (*addr);switch ((w >>24 ) &0x03 ) {case 0x01 :
@@ -1077,7 +1077,7 @@ dt_irq_find_parent(const struct dt_device_node *child)if ( parp ==NULL ) p =dt_get_parent (child);else - p =dt_find_node_by_phandle (be32_to_cpup (parp)); + p =dt_find_node_by_phandle (be32_to_cpu (*parp)); child = p; }while ( p &&dt_get_property (p,"#interrupt-cells" ,NULL ) ==NULL );
@@ -1110,7 +1110,7 @@ unsigned int dt_number_of_irq(const struct dt_device_node *device) intlen /=sizeof (*intspec);dt_dprintk (" using 'interrupts' property\n" ); -dt_dprintk (" intspec=%d intlen=%d\n" ,be32_to_cpup (intspec), intlen); +dt_dprintk (" intspec=%d intlen=%d\n" ,be32_to_cpu (*intspec), intlen);/* Look for the interrupt parent. */ p =dt_irq_find_parent (device);
@@ -1241,7 +1241,7 @@ int dt_for_each_irq_map(const struct dt_device_node *dev, imaplen -= addrsize + intsize;/* Get the interrupt parent */ - ipar =dt_find_node_by_phandle (be32_to_cpup (imap)); + ipar =dt_find_node_by_phandle (be32_to_cpu (*imap)); imap++; --imaplen;
@@ -1358,8 +1358,8 @@ static int dt_irq_map_raw(const struct dt_device_node *parent,int match, i;dt_dprintk ("dt_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n" , - parent->full_name,be32_to_cpup (intspec), -be32_to_cpup (intspec +1 ), ointsize); + parent->full_name,be32_to_cpu (*intspec), +be32_to_cpu (*(intspec+1 )), ointsize); ipar = parent;
@@ -1471,7 +1471,7 @@ static int dt_irq_map_raw(const struct dt_device_node *parent,dt_dprintk (" -> match=%d (imaplen=%d)\n" , match, imaplen);/* Get the interrupt parent */ - newpar =dt_find_node_by_phandle (be32_to_cpup (imap)); + newpar =dt_find_node_by_phandle (be32_to_cpu (*imap)); imap++; --imaplen;
@@ -1565,7 +1565,7 @@ int dt_device_get_raw_irq(const struct dt_device_node *device, intlen /=sizeof (*intspec);dt_dprintk (" using 'interrupts' property\n" ); -dt_dprintk (" intspec=%d intlen=%d\n" ,be32_to_cpup (intspec), intlen); +dt_dprintk (" intspec=%d intlen=%d\n" ,be32_to_cpu (*intspec), intlen);/* Look for the interrupt parent. */ p =dt_irq_find_parent (device);
@@ -1676,7 +1676,7 @@ static int __dt_parse_phandle_with_args(const struct dt_device_node *np,* If phandle is 0, then it is an empty entry with no * arguments. Skip forward to the next entry. * */ - phandle =be32_to_cpup (list++); + phandle =be32_to_cpu (*list++);if ( phandle ) {/*
@@ -1745,7 +1745,7 @@ static int __dt_parse_phandle_with_args(const struct dt_device_node *np, out_args->np = node; out_args->args_count = count;for ( i =0 ; i < count; i++ ) - out_args->args[i] =be32_to_cpup (list++); + out_args->args[i] =be32_to_cpu (*list++); }/* Found it! return success */
@@ -1826,7 +1826,7 @@ static unsigned long __init unflatten_dt_node(const void *fdt,int has_name =0 ;int new_format =0 ; - tag =be32_to_cpup ((__be32 *)(*p)); + tag =be32_to_cpu (*(__be32 *)(*p));if ( tag !=FDT_BEGIN_NODE ) {printk (XENLOG_WARNING "Weird tag at start of node: %x\n" , tag);
@@ -1919,7 +1919,7 @@ static unsigned long __init unflatten_dt_node(const void *fdt,u32 sz, noff;const char *pname; - tag =be32_to_cpup ((__be32 *)(*p)); + tag =be32_to_cpu (*(__be32 *)(*p));if ( tag ==FDT_NOP ) { *p +=4 ;
@@ -1928,8 +1928,8 @@ static unsigned long __init unflatten_dt_node(const void *fdt,if ( tag !=FDT_PROP )break ; *p +=4 ; - sz =be32_to_cpup ((__be32 *)(*p)); - noff =be32_to_cpup ((__be32 *)((*p) +4 )); + sz =be32_to_cpu (*(__be32 *)(*p)); + noff =be32_to_cpu (*(__be32 *)((*p) +4 )); *p +=8 ;if (fdt_version (fdt) <0x10 ) *p =ROUNDUP (*p, sz >=8 ?8 :4 );
@@ -1956,13 +1956,13 @@ static unsigned long __init unflatten_dt_node(const void *fdt, (strcmp(pname, "linux,phandle") == 0) ) {if ( np->phandle ==0 ) - np->phandle =be32_to_cpup ((__be32 *)*p); + np->phandle =be32_to_cpu (*(__be32 *)*p); }/* And we process the "ibm,phandle" property * used in pSeries dynamic device tree * stuff */ if (strcmp (pname,"ibm,phandle" ) ==0 ) - np->phandle =be32_to_cpup ((__be32 *)*p); + np->phandle =be32_to_cpu (*(__be32 *)*p); pp->name = pname; pp->length = sz; pp->value = (void *)*p;
@@ -2034,7 +2034,7 @@ static unsigned long __init unflatten_dt_node(const void *fdt, *p +=4 ; else mem =unflatten_dt_node (fdt, mem, p, np, allnextpp, fpsize); - tag =be32_to_cpup ((__be32 *)(*p)); + tag =be32_to_cpu (*(__be32 *)(*p)); }if ( tag !=FDT_END_NODE ) {
@@ -2086,7 +2086,7 @@ static void __init __unflatten_device_tree(const void *fdt,/* Second pass, do actual unflattening */ start = ((unsigned long )fdt) +fdt_off_dt_struct (fdt);unflatten_dt_node (fdt, mem, &start,NULL , &allnextp,0 ); -if (be32_to_cpup ((__be32 *)start) !=FDT_END ) +if (be32_to_cpu (*(__be32 *)start) !=FDT_END )printk (XENLOG_WARNING "Weird tag at end of tree: %08x\n" , *((u32 *)start));if (be32_to_cpu (((__be32 *)mem)[size /4 ]) !=0xdeadbeef )
xen/common/libelf/libelf-private.h +3 -3
@@ -31,9 +31,9 @@ printk(fmt, ## args )#define strtoull (str, end, base) simple_strtoull(str, end, base) -#define bswap_16 (x) swab16(x) -#define bswap_32 (x) swab32(x) -#define bswap_64 (x) swab64(x) +#define bswap_16 (x) bswap16(x) +#define bswap_32 (x) bswap32(x) +#define bswap_64 (x) bswap64(x)#else /* !__XEN__ */
xen/common/xz/private.h +1 -1
@@ -28,7 +28,7 @@ static inline void put_unaligned_le32(u32 val, void *p)#endif -#define get_le32 (p) le32_to_cpup((const uint32_t *)(p)) +#define get_le32 (p) le32_to_cpu(*(const uint32_t *)(p))#define false 0#define true 1
xen/include/xen/unaligned.h +12 -12
@@ -20,62 +20,62 @@static inline uint16_t get_unaligned_be16 (const void *p) { -return be16_to_cpup (p); +return be16_to_cpu (*(const uint16_t *)p); }static inline void put_unaligned_be16 (uint16_t val,void *p) { - *(__force __be16 *)p =cpu_to_be16 (val); + *(__be16 *)p =cpu_to_be16 (val); }static inline uint32_t get_unaligned_be32 (const void *p) { -return be32_to_cpup (p); +return be32_to_cpu (*(const uint32_t *)p); }static inline void put_unaligned_be32 (uint32_t val,void *p) { - *(__force __be32 *)p =cpu_to_be32 (val); + *(__be32 *)p =cpu_to_be32 (val); }static inline uint64_t get_unaligned_be64 (const void *p) { -return be64_to_cpup (p); +return be64_to_cpu (*(const uint64_t *)p); }static inline void put_unaligned_be64 (uint64_t val,void *p) { - *(__force __be64 *)p =cpu_to_be64 (val); + *(__be64 *)p =cpu_to_be64 (val); }static inline uint16_t get_unaligned_le16 (const void *p) { -return le16_to_cpup (p); +return le16_to_cpu (*(const uint16_t *)p); }static inline void put_unaligned_le16 (uint16_t val,void *p) { - *(__force __le16 *)p =cpu_to_le16 (val); + *(__le16 *)p =cpu_to_le16 (val); }static inline uint32_t get_unaligned_le32 (const void *p) { -return le32_to_cpup (p); +return le32_to_cpu (*(const uint32_t *)p); }static inline void put_unaligned_le32 (uint32_t val,void *p) { - *(__force __le32 *)p =cpu_to_le32 (val); + *(__le32 *)p =cpu_to_le32 (val); }static inline uint64_t get_unaligned_le64 (const void *p) { -return le64_to_cpup (p); +return le64_to_cpu (*(const uint64_t *)p); }static inline void put_unaligned_le64 (uint64_t val,void *p) { - *(__force __le64 *)p =cpu_to_le64 (val); + *(__le64 *)p =cpu_to_le64 (val); }#endif /* __XEN_UNALIGNED_H__ */
Signature · 1 line
2.27.0
PATCH v3 1/6 xen: implement byteswap
In reply to Lin Liuswab() is massively over complicated and can be simplified by builtins. The compilers provide builtin function to swap bytes.
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
* clang: https://clang.llvm.org/docs/LanguageExtensions.html
This patch simplify swab() with builtins and fallback for old compilers.
---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Bertrand Marquis <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]> Changes in v3:
- Check __has_builtin instead of GNUC version
Changes in v2:
- Add fallback for compilers without __builtin_bswap
- Implement with plain C instead of macros
--- xen/arch/arm/include/asm/byteorder.h | 14 ++----- xen/arch/x86/include/asm/byteorder.h | 34 ++--------------- xen/include/xen/byteorder.h | 56 ++++++++++++++++++++++++++++ xen/include/xen/byteswap.h | 44 ++++++++++++++++++++++ xen/include/xen/compiler.h | 12 ++++++ 5 files changed, 120 insertions(+), 40 deletions(-) create mode 100644 xen/include/xen/byteorder.h create mode 100644 xen/include/xen/byteswap.h
xen/arch/arm/include/asm/byteorder.h +4 -10
@@ -1,16 +1,10 @@#ifndef __ASM_ARM_BYTEORDER_H__#define __ASM_ARM_BYTEORDER_H__ -#define __BYTEORDER_HAS_U64__ +#ifndef __BYTE_ORDER__ +#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ +#endif -#include <xen/byteorder/little_endian.h> +#include <xen/byteorder.h> #endif /* __ASM_ARM_BYTEORDER_H__ */ -/* -* Local variables: -* mode: C -* c-file-style: "BSD" -* c-basic-offset: 4 -* indent-tabs-mode: nil -* End: -*/
xen/arch/x86/include/asm/byteorder.h +4 -30
@@ -1,36 +1,10 @@#ifndef __ASM_X86_BYTEORDER_H__#define __ASM_X86_BYTEORDER_H__ -#include <asm/types.h> -#include <xen/compiler.h> +#ifndef __BYTE_ORDER__ +#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ +#endif -static inline __attribute_const__ __u32 ___arch__swab32(__u32 x) -{ - asm("bswap %0" :"=r" (x) :"0" (x)); -return x; -} - -static inline __attribute_const__ __u64 ___arch__swab64(__u64 val) -{ -union { -struct {__u32 a,b; } s; -__u64 u; - } v; - v.u = val; - asm("bswapl %0 ; bswapl %1 ; xchgl %0,%1" - :"=r" (v.s.a),"=r" (v.s.b) - :"0" (v.s.a),"1" (v.s.b)); -return v.u; -} - -/* Do not define swab16. Gcc is smart enough to recognize "C" version and -convert it into rotation or exhange. */ - -#define __arch__swab64 (x) ___arch__swab64(x) -#define __arch__swab32 (x) ___arch__swab32(x) - -#define __BYTEORDER_HAS_U64__ - -#include <xen/byteorder/little_endian.h> +#include <xen/byteorder.h> #endif /* __ASM_X86_BYTEORDER_H__ */
xen/include/xen/byteorder.h new file +56 -0
56 lines not on this page · raw message
xen/include/xen/byteswap.h new file +44 -0
44 lines not on this page · raw message
xen/include/xen/compiler.h +12 -0
@@ -185,4 +185,16 @@# define CLANG_DISABLE_WARN_GCC_COMPAT_END #endif +#ifndef __has_builtin +/* +* Backwards compatibility for GCC < 10. +* All supported versions of Clang support __has_builtin +* */ +#define __has_builtin (x) GCC_has ## x + +#define GCC_has__builtin_bswap16 (CONFIG_GCC_VERSION >= 40800) +#define GCC_has__builtin_bswap32 (CONFIG_GCC_VERSION >= 40400) +#define GCC_has__builtin_bswap64 (CONFIG_GCC_VERSION >= 40400) +#endif +#endif /* __LINUX_COMPILER_H */
Signature · 1 line
2.27.0
PATCH v3 5/6 byteorder: Remove byteorder
In reply to Lin Liuinclude/xen/byteswap.h has simplify the interface, just clean the old interface
No functional change
---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>
--- xen/include/xen/byteorder/big_endian.h | 102 ------------ xen/include/xen/byteorder/generic.h | 68 -------- xen/include/xen/byteorder/little_endian.h | 102 ------------ xen/include/xen/byteorder/swab.h | 183 ---------------------- 4 files changed, 455 deletions(-) delete mode 100644 xen/include/xen/byteorder/big_endian.h delete mode 100644 xen/include/xen/byteorder/generic.h delete mode 100644 xen/include/xen/byteorder/little_endian.h delete mode 100644 xen/include/xen/byteorder/swab.h
xen/include/xen/byteorder/big_endian.h deleted +0 -102
@@ -1,102 +0,0 @@ -#ifndef __XEN_BYTEORDER_BIG_ENDIAN_H__ -#define __XEN_BYTEORDER_BIG_ENDIAN_H__ - -#ifndef __BIG_ENDIAN -#define __BIG_ENDIAN 4321 -#endif -#ifndef __BIG_ENDIAN_BITFIELD -#define __BIG_ENDIAN_BITFIELD -#endif - -#include <xen/types.h> -#include <xen/byteorder/swab.h> - -#define __constant_cpu_to_le64 (x) ((__force __le64)___constant_swab64((x))) -#define __constant_le64_to_cpu (x) ___constant_swab64((__force __u64)(__le64)(x)) -#define __constant_cpu_to_le32 (x) ((__force __le32)___constant_swab32((x))) -#define __constant_le32_to_cpu (x) ___constant_swab32((__force __u32)(__le32)(x)) -#define __constant_cpu_to_le16 (x) ((__force __le16)___constant_swab16((x))) -#define __constant_le16_to_cpu (x) ___constant_swab16((__force __u16)(__le16)(x)) -#define __constant_cpu_to_be64 (x) ((__force __be64)(__u64)(x)) -#define __constant_be64_to_cpu (x) ((__force __u64)(__be64)(x)) -#define __constant_cpu_to_be32 (x) ((__force __be32)(__u32)(x)) -#define __constant_be32_to_cpu (x) ((__force __u32)(__be32)(x)) -#define __constant_cpu_to_be16 (x) ((__force __be16)(__u16)(x)) -#define __constant_be16_to_cpu (x) ((__force __u16)(__be16)(x)) -#define __cpu_to_le64 (x) ((__force __le64)__swab64((x))) -#define __le64_to_cpu (x) __swab64((__force __u64)(__le64)(x)) -#define __cpu_to_le32 (x) ((__force __le32)__swab32((x))) -#define __le32_to_cpu (x) __swab32((__force __u32)(__le32)(x)) -#define __cpu_to_le16 (x) ((__force __le16)__swab16((x))) -#define __le16_to_cpu (x) __swab16((__force __u16)(__le16)(x)) -#define __cpu_to_be64 (x) ((__force __be64)(__u64)(x)) -#define __be64_to_cpu (x) ((__force __u64)(__be64)(x)) -#define __cpu_to_be32 (x) ((__force __be32)(__u32)(x)) -#define __be32_to_cpu (x) ((__force __u32)(__be32)(x)) -#define __cpu_to_be16 (x) ((__force __be16)(__u16)(x)) -#define __be16_to_cpu (x) ((__force __u16)(__be16)(x)) - -static inline __le64 __cpu_to_le64p (const __u64 *p) -{ -return (__force __le64 )__swab64p (p); -} -static inline __u64 __le64_to_cpup (const __le64 *p) -{ -return __swab64p ((__u64 *)p); -} -static inline __le32 __cpu_to_le32p (const __u32 *p) -{ -return (__force __le32 )__swab32p (p); -} -static inline __u32 __le32_to_cpup (const __le32 *p) -{ -return __swab32p ((__u32 *)p); -} -static inline __le16 __cpu_to_le16p (const __u16 *p) -{ -return (__force __le16 )__swab16p (p); -} -static inline __u16 __le16_to_cpup (const __le16 *p) -{ -return __swab16p ((__u16 *)p); -} -static inline __be64 __cpu_to_be64p (const __u64 *p) -{ -return (__force __be64)*p; -} -static inline __u64 __be64_to_cpup (const __be64 *p) -{ -return (__force __u64)*p; -} -static inline __be32 __cpu_to_be32p (const __u32 *p) -{ -return (__force __be32)*p; -} -static inline __u32 __be32_to_cpup (const __be32 *p) -{ -return (__force __u32)*p; -} -static inline __be16 __cpu_to_be16p (const __u16 *p) -{ -return (__force __be16)*p; -} -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) - -#include <xen/byteorder/generic.h> - -#endif /* __XEN_BYTEORDER_BIG_ENDIAN_H__ */
xen/include/xen/byteorder/generic.h deleted +0 -68
68 lines not on this page · raw message
xen/include/xen/byteorder/little_endian.h deleted +0 -102
102 lines not on this page · raw message
xen/include/xen/byteorder/swab.h deleted +0 -183
183 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH v3 1/6] xen: implement byteswap
In reply to Lin LiuQuoted 5 lines · Lin Liu
swab()is massively over complicated and can be simplified by builtins. The compilers provide builtin function to swap bytes.
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
* clang: https://clang.llvm.org/docs/LanguageExtensions.html
This patch simplifyswab()with builtins and fallback for old compilers.
Arguably, this patch introduces a new byteswapping infrastructure in terms of compiler builtins and bswapXX(), so the swab() infrastructure can be retired.
xen/arch/arm/include/asm/byteorder.h line 6
Quoted 12 lines
xen/arch/arm/include/asm/byteorder.h +3
-1@@ -1,16 +1,10 @@#ifndef __ASM_ARM_BYTEORDER_H__#define __ASM_ARM_BYTEORDER_H__ -#define __BYTEORDER_HAS_U64__ +#ifndef __BYTE_ORDER__ +#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ +#endif
This won't actually do what you want on GCC 4.5 or older. You also want
#ifndef __ORDER_LITTLE_ENDIAN__
# define __ORDER_LITTLE_ENDIAN__ 1234 #endif
#ifndef __ORDER_BIG_ENDIAN__
# define __ORDER_BIG_ENDIAN__ 4321 #endif
in compiler.h to cope with older GCC.
Otherwise, LGTM. Reviewed-by: Andrew Cooper <[email protected]>
I can fix this on commit if its the only issue issue. Otherwise, please correct it when posting v4.
~Andrew
Re: [PATCH v3 4/6] xen: Switch to byteswap
In reply to Lin LiuHi,
Quoted 27 lines · Lin Liu
Update to use byteswap to swap bytes.
No functional change.
Signed-off-by Lin Liu <[email protected]>---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
Changes in v3:
- Updatexen/common/device_tree.cto usebe32_to_cpu
- Keep const in type cast inunaligned.h--- xen/common/device_tree.c | 44 +++++++++++++++--------------- xen/common/libelf/libelf-private.h | 6 ++-- xen/common/xz/private.h | 2 +- xen/include/xen/unaligned.h | 24 ++++++++-------- 4 files changed, 38 insertions(+), 38 deletions(-)xen/common/device_tree.c +0
-0
xen/common/device_tree.c bool_t dt_property_read_u32(const struct dt_device_node *np, · line 174
Quoted 5 lines
xen/common/device_tree.c +1
-1@@ -171,7 +171,7 @@ bool_t dt_property_read_u32(const struct dt_device_node *np,if ( !val || len <sizeof (*out_value) )return 0 ; - *out_value =be32_to_cpup (val); + *out_value =be32_to_cpu (*val);
This code has been taken from Linux and I would rather prefer to keep the *cpup* helpers to avoid any changes when backporting.
xen/include/xen/unaligned.h line 23
Quoted 10 lines
xen/include/xen/unaligned.h +1
-1@@ -20,62 +20,62 @@static inline uint16_t get_unaligned_be16 (const void *p) { -return be16_to_cpup (p); +return be16_to_cpu (*(const uint16_t *)p)
I haven't checked the existing implementation of be16_to_cpup(). However, this new approach would allow the compiler to use a single load instruction to read the 16-bit value from memory. So this change may break on platform where unaligned access is forbidden (such as arm32).
xen/include/xen/unaligned.h line 28
Quoted 6 lines
xen/include/xen/unaligned.h +1
-1}static inline void put_unaligned_be16 (uint16_t val,void *p) { - *(__force __be16 *)p =cpu_to_be16 (val); + *(__be16 *)p =cpu_to_be16 (val);
Why did you drop the __force?
xen/include/xen/unaligned.h line 30
Quoted 2 lines
xen/include/xen/unaligned.h +0
-0}
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 2/6] crypto/vmac: Simplify code with byteswap
In reply to Lin LiuQuoted 7 lines · Lin Liu
This file has its own implementation of swap bytes. Clean up the code with
xen/byteswap.h.No functional change.
Signed-off-by Lin Liu <[email protected]>Acked-by Jan Beulich <[email protected]>
Re: [PATCH v3 3/6] arm64/find_next_bit: Remove ext2_swab()
In reply to Lin LiuQuoted 6 lines · Lin Liu
ext2 has nothing to do with this logic. Clean up the code with
xen/byteswap.hwhich now has an unsigned long helper.No functional change.
Signed-off-by Lin Liu <[email protected]>
Re: [PATCH v3 3/6] arm64/find_next_bit: Remove ext2_swab()
In reply to Lin LiuHi,
Quoted 1 line · Lin Liu
ext2 has nothing to do with this logic.
This code was a verbatim copy from Linux. Looking at the history, I am not sure there was even a connection with the ext2 filesystem (I guess this is what you mean?).
So I would drop this and simply say that we could use the new helpers.
Quoted 6 lines
Clean up the code with
xen/byteswap.hwhich now has an unsigned long helper.No functional change.
Signed-off-by Lin Liu <[email protected]>
Other that what I wrote above:
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 4/6] xen: Switch to byteswap
In reply to Julien GrallQuoted 14 lines · Julien Grall
xen/common/device_tree.c bool_t dt_property_read_u32(const struct · line 175
Quoted 10 lines · Lin Liu
xen/common/device_tree.c +1
-0@@ -171,7 +171,7 @@ bool_t dt_property_read_u32(const struct dt_device_node *np, if ( !val || len < sizeof(*out_value) ) return 0; - *out_value = be32_to_cpup(val); + *out_value = be32_to_cpu(*val);This code has been taken from Linux and I would rather prefer to keep the *cpup* helpers to avoid any changes when backporting.
I specifically requested that this be de-obfuscated. Hiding indirection is a fantastic way to introduce bugs, and we've had XSAs in the past because of it (admittedly in libxl, but still...).
This file is already Xen style, not Linux, so won't be taking backports directly, and the resulting compiler diagnostic will make it obvious what is going on. be32_to_cpu(*val) works fine on older versions of Xen too.
In this case, the cost of changing is well worth the improvements and simplifications gained. See the 0/6 diffstat and see that the compiler can make better optimisations when it can see the builtin.
Quoted 12 lines
xen/include/xen/unaligned.h line 22
Quoted 10 lines
xen/include/xen/unaligned.h +1
-1@@ -20,62 +20,62 @@ static inline uint16_t get_unaligned_be16(const void *p) { - return be16_to_cpup(p); + return be16_to_cpu(*(const uint16_t *)p)I haven't checked the existing implementation of
be16_to_cpup().
It's a plain dereference, just like this. AFAICT, it wasn't unaligned safe before, either.
It should be reasonably easy to fix in a followup patch. Just memcpy() to/from the void pointer to a stack variable of the appropriate type.
~Andrew
Re: [PATCH v3 5/6] byteorder: Remove byteorder
In reply to Lin LiuQuoted 13 lines · Lin Liu
include/xen/byteswap.hhas simplify the interface, just clean the old interfaceNo functional change
Signed-off-by Lin Liu <[email protected]>---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>
Quoted 6 lines
--- xen/include/xen/byteorder/big_endian.h | 102 ------------ xen/include/xen/byteorder/generic.h | 68 -------- xen/include/xen/byteorder/little_endian.h | 102 ------------ xen/include/xen/byteorder/swab.h | 183 ---------------------- 4 files changed, 455 deletions(-)
Good riddance.
Re: [PATCH v3 1/6] xen: implement byteswap
In reply to Lin LiuHi,
Quoted 1 line · Lin Liu
swab()is massively over complicated and can be simplified by builtins.
NIT: "by builtins" -> "by re-implementing using compiler builtins".
Quoted 47 lines
The compilers provide builtin function to swap bytes.
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
* clang: https://clang.llvm.org/docs/LanguageExtensions.html
This patch simplifyswab()with builtins and fallback for old compilers.Signed-off-by Lin Liu <[email protected]>---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Bertrand Marquis <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]> Changes in v3:
- Check__has_builtininstead of GNUC versionChanges in v2:
- Add fallback for compilers without__builtin_bswap
- Implement with plain C instead of macros--- xen/arch/arm/include/asm/byteorder.h | 14 ++----- xen/arch/x86/include/asm/byteorder.h | 34 ++--------------- xen/include/xen/byteorder.h | 56 ++++++++++++++++++++++++++++ xen/include/xen/byteswap.h | 44 ++++++++++++++++++++++ xen/include/xen/compiler.h | 12 ++++++ 5 files changed, 120 insertions(+), 40 deletions(-)create mode 100644
xen/include/xen/byteorder.hcreate mode 100644xen/include/xen/byteswap.hxen/arch/arm/include/asm/byteorder.h +4
-2@@ -1,16 +1,10 @@#ifndef __ASM_ARM_BYTEORDER_H__#define __ASM_ARM_BYTEORDER_H__ -#define __BYTEORDER_HAS_U64__ +#ifndef __BYTE_ORDER__ +#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ +#endif -#include <xen/byteorder/little_endian.h> +#include <xen/byteorder.h>
xen/arch/arm/include/asm/byteorder.h line 16
Quoted 10 lines
xen/arch/arm/include/asm/byteorder.h +0
-8#endif /* __ASM_ARM_BYTEORDER_H__ */ -/* -* Local variables: -* mode: C -* c-file-style: "BSD" -* c-basic-offset: 4 -* indent-tabs-mode: nil -* End: -*/
This change looks unrelated to this patch. Can you explain it?
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 4/6] xen: Switch to byteswap
In reply to Andrew CooperHi,
Quoted 16 lines · Andrew Cooper
Quoted 14 lines · Julien Grall
xen/common/device_tree.c bool_t dt_property_read_u32(const struct · line 175
Quoted 10 lines · Lin Liu
xen/common/device_tree.c +1
-0@@ -171,7 +171,7 @@ bool_t dt_property_read_u32(const struct dt_device_node *np, if ( !val || len < sizeof(*out_value) ) return 0; - *out_value = be32_to_cpup(val); + *out_value = be32_to_cpu(*val);This code has been taken from Linux and I would rather prefer to keep the *cpup* helpers to avoid any changes when backporting.
Quoted 3 lines
I specifically requested that this be de-obfuscated. Hiding indirection is a fantastic way to introduce bugs, and we've had XSAs in the past because of it (admittedly in libxl, but still...).
Care providing a link to those XSAs? But I don't really see what's the problem here, this is no better no worth than passing pointer to other functions...
Quoted 8 lines
This file is already Xen style, not Linux, so won't be taking backports directly, and the resulting compiler diagnostic will make it obvious what is going on.
be32_to_cpu(*val)works fine on older versions of Xen too.In this case, the cost of changing is well worth the improvements and simplifications gained. See the 0/6 diffstat and see that the compiler can make better optimisations when it can see the builtin.
I take your point... However, the commit message provides virtually zero justification into why we should switch to be32_to_cpup(). So to me, the changes so far looks unwanted.
Quoted 14 lines
Quoted 13 lines
xen/include/xen/unaligned.h line 22
Quoted 11 lines
xen/include/xen/unaligned.h +1
-1@@ -20,62 +20,62 @@ static inline uint16_t get_unaligned_be16(const void *p) { - return be16_to_cpup(p); + return be16_to_cpu(*(const uint16_t *)p)I haven't checked the existing implementation of
be16_to_cpup().
Quoted 2 lines
It's a plain dereference, just like this. AFAICT, it wasn't unaligned safe before, either.
Well, technically an architecture could provide an override for the copy. I agree that arm32 is already bogus but...
Quoted 3 lines
It should be reasonably easy to fix in a followup patch. Just
memcpy()to/from the void pointer to a stack variable of the appropriate type.
... I disagree that it should be fixed in a follow-up patch. It should be fixed now as this is where the badness is spread to any architecture.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 4/6] xen: Switch to byteswap
Quoted 17 lines · Julien Grall
Quoted 14 lines
Quoted 13 lines
xen/include/xen/unaligned.h line 22
Quoted 11 lines
xen/include/xen/unaligned.h +1
-1@@ -20,62 +20,62 @@ static inline uint16_t get_unaligned_be16(const void *p) { - return be16_to_cpup(p); + return be16_to_cpu(*(const uint16_t *)p)I haven't checked the existing implementation of
be16_to_cpup().Quoted 2 lines
It's a plain dereference, just like this. AFAICT, it wasn't unaligned safe before, either.
Quoted 8 lines
Well, technically an architecture could provide an override for the copy. I agree that arm32 is already bogus but...
Quoted 3 lines
It should be reasonably easy to fix in a followup patch. Just
memcpy()to/from the void pointer to a stack variable of the appropriate type.... I disagree that it should be fixed in a follow-up patch. It should be fixed now as this is where the badness is spread to any architecture.
No. That is an inappropriate request to make.
Lin's patch does not alter the broken-ness of unaligned on arm32, and does improve the aspect of the hypervisor that it pertains to. It therefore stands on its own merit.
Your choices are to either fix it yourself (after all, you are the maintainer who cares about this unrelated bug), or you ask Lin kindly if he has time to look into fixing the unrelated bug after this series is complete.
It is not reasonable to say "this unrelated thing is broken, and you need to fix it first to get your series in". Requests like that are, I'm sure, part of what Bertrand raised in the community call as unnecessary fiction getting work submitted.
~Andrew
Re: [PATCH v3 4/6] xen: Switch to byteswap
Hi,
Quoted 24 lines · Andrew Cooper
Quoted 23 lines · Julien Grall
Quoted 14 lines
Quoted 13 lines
xen/include/xen/unaligned.h line 22
Quoted 11 lines
xen/include/xen/unaligned.h +1
-1@@ -20,62 +20,62 @@ static inline uint16_t get_unaligned_be16(const void *p) { - return be16_to_cpup(p); + return be16_to_cpu(*(const uint16_t *)p)I haven't checked the existing implementation of
be16_to_cpup().Quoted 2 lines
It's a plain dereference, just like this. AFAICT, it wasn't unaligned safe before, either.
Well, technically an architecture could provide an override for the copy. I agree that arm32 is already bogus but...
Quoted 3 lines
It should be reasonably easy to fix in a followup patch. Just
memcpy()to/from the void pointer to a stack variable of the appropriate type.
Quoted 8 lines
Quoted 2 lines
... I disagree that it should be fixed in a follow-up patch. It should be fixed now as this is where the badness is spread to any architecture.
No. That is an inappropriate request to make.
Lin's patch does not alter the broken-ness of unaligned on arm32, and does improve the aspect of the hypervisor that it pertains to. It therefore stands on its own merit.
I am not sure sure why switching from *cpup* improves things... and as usual you haven't answered to the clarification questions.
Quoted 5 lines
Your choices are to either fix it yourself (after all, you are the maintainer who cares about this unrelated bug), or you ask Lin kindly if he has time to look into fixing the unrelated bug after this series is complete.
Or 3) keep *cpup* so there is only one place to fix it.
Quoted 5 lines
It is not reasonable to say "this unrelated thing is broken, and you need to fix it first to get your series in". Requests like that are, I'm sure, part of what Bertrand raised in the community call as unnecessary fiction getting work submitted.
To be honest, you put the contributor in this situation. I would have been perfectly happy if we keep *cpup* around as there would be only a place to fix.
With this approach, you are effectively going to increase the work later one because now we would have to chase all the open-coded version of *cpup* and check which one is not safe.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 1/6] xen: implement byteswap
In reply to Julien GrallHi,
Quoted 1 line · Lin Liu
swab()is massively over complicated and can be simplified by builtins.
NIT: "by builtins" -> "by re-implementing using compiler builtins".
Quoted 58 lines
The compilers provide builtin function to swap bytes.
* gcc: https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgcc.gnu.org%2Fonlinedocs%2Fgcc%2FOther-Builtins.html&data=05%7C01%7Clin.liu%40citrix.com%7Ce0b3d98d7f8d47b8fe8708da3275afcd%7C335836de42ef43a2b145348c2ee9ca5b%7C0%7C0%7C637877778294067911%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=HDTF1LDJcD2PLSCuM%2FjIz%2FWGf1CrYk0e%2FLox22%2FXnvQ%3D&reserved=0
* clang: https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fclang.llvm.org%2Fdocs%2FLanguageExtensions.html&data=05%7C01%7Clin.liu%40citrix.com%7Ce0b3d98d7f8d47b8fe8708da3275afcd%7C335836de42ef43a2b145348c2ee9ca5b%7C0%7C0%7C637877778294067911%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=EvWcLMi2vtT9haQVo%2F9uXmjBh2zVLUzZAgU57i%2FFMNo%3D&reserved=0
This patch simplifyswab()with builtins and fallback for old compilers.Signed-off-by Lin Liu <[email protected]>---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Bertrand Marquis <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]> Changes in v3:
- Check__has_builtininstead of GNUC versionChanges in v2:
- Add fallback for compilers without__builtin_bswap
- Implement with plain C instead of macros--- xen/arch/arm/include/asm/byteorder.h | 14 ++----- xen/arch/x86/include/asm/byteorder.h | 34 ++--------------- xen/include/xen/byteorder.h | 56 ++++++++++++++++++++++++++++ xen/include/xen/byteswap.h | 44 ++++++++++++++++++++++ xen/include/xen/compiler.h | 12 ++++++ 5 files changed, 120 insertions(+), 40 deletions(-)create mode 100644
xen/include/xen/byteorder.hcreate mode 100644xen/include/xen/byteswap.hxen/arch/arm/include/asm/byteorder.h +4
-10@@ -1,16 +1,10 @@#ifndef __ASM_ARM_BYTEORDER_H__#define __ASM_ARM_BYTEORDER_H__ -#define __BYTEORDER_HAS_U64__ +#ifndef __BYTE_ORDER__ +#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ +#endif -#include <xen/byteorder/little_endian.h> +#include <xen/byteorder.h> #endif /* __ASM_ARM_BYTEORDER_H__ */ -/* -* Local variables: -* mode: C -* c-file-style: "BSD" -* c-basic-offset: 4 -* indent-tabs-mode: nil -* End: -*/
Quoted 1 line
Quoted 1 line
This change looks unrelated to this patch. Can you explain it?
Do you mean following code block? Yes, it is unrelated, I removed it as I found some files does not include such field. Will revert such field in V4.
Quoted 8 lines
-/*
- * Local variables:
- * mode: C
- * c-file-style: "BSD"
- * c-basic-offset: 4
- * indent-tabs-mode: nil
- * End:
- */
Re: [PATCH v3 4/6] xen: Switch to byteswap
In reply to Lin LiuQuoted 5 lines · Lin Liu
Update to use byteswap to swap bytes.
No functional change.
Signed-off-by Lin Liu <[email protected]>
FYI, this patch breaks build of stubdomain:
In file included from /var/tmp/git.xen.lU52/stubdom/include/../../xen/common/unxz.c:124,
from xg_dom_decompress_unsafe_xz.c:40:/var/tmp/git.xen.lU52/stubdom/include/../../xen/common/xz/dec_stream.c: In function ‘dec_stream_header’:/var/tmp/git.xen.lU52/stubdom/include/../../xen/common/xz/private.h:31:21: error: implicit declaration of function ‘le32_to_cpu’; did you mean ‘le32_to_cpup’? [-Werror=implicit-function-declaration]
31 | #define get_le32(p) le32_to_cpu(*(const uint32_t *)(p))
| ^~~~~~~~~~~
/var/tmp/git.xen.lU52/stubdom/include/../../xen/common/xz/dec_stream.c:393:7: note: in expansion of macro ‘get_le32’
393 | != get_le32(s->temp.buf + HEADER_MAGIC_SIZE + 2))
| ^~~~~~~~cc1: all warnings being treated as errors
make[2]: *** [/var/tmp/git.xen.lU52/stubdom/libs-x86_64/guest/../../../tools/Rules.mk:150: xg_dom_decompress_unsafe_xz.o] Error 1 make[1]: *** [Makefile:367: libs-x86_64/guest/libxenguest.a] Error 2
make: *** [Makefile:73: build-stubdom] Error 2
Cheers,
Signature · 1 line
Anthony PERARD
Re: [PATCH v3 1/6] xen: implement byteswap
In reply to Lin Liu (刘林)Quoted 63 lines · Lin Liu (刘林)
Quoted 1 line · Lin Liu
swab()is massively over complicated and can be simplified by builtins.NIT: "by builtins" -> "by re-implementing using compiler builtins".
Quoted 57 lines
The compilers provide builtin function to swap bytes.
* gcc: https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgcc.gnu.org%2Fonlinedocs%2Fgcc%2FOther-Builtins.html&data=05%7C01%7Clin.liu%40citrix.com%7Ce0b3d98d7f8d47b8fe8708da3275afcd%7C335836de42ef43a2b145348c2ee9ca5b%7C0%7C0%7C637877778294067911%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=HDTF1LDJcD2PLSCuM%2FjIz%2FWGf1CrYk0e%2FLox22%2FXnvQ%3D&reserved=0
* clang: https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fclang.llvm.org%2Fdocs%2FLanguageExtensions.html&data=05%7C01%7Clin.liu%40citrix.com%7Ce0b3d98d7f8d47b8fe8708da3275afcd%7C335836de42ef43a2b145348c2ee9ca5b%7C0%7C0%7C637877778294067911%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=EvWcLMi2vtT9haQVo%2F9uXmjBh2zVLUzZAgU57i%2FFMNo%3D&reserved=0
This patch simplifyswab()with builtins and fallback for old compilers.Signed-off-by Lin Liu <[email protected]>---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Bertrand Marquis <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]> Changes in v3:
- Check__has_builtininstead of GNUC versionChanges in v2:
- Add fallback for compilers without__builtin_bswap
- Implement with plain C instead of macros--- xen/arch/arm/include/asm/byteorder.h | 14 ++----- xen/arch/x86/include/asm/byteorder.h | 34 ++--------------- xen/include/xen/byteorder.h | 56 ++++++++++++++++++++++++++++ xen/include/xen/byteswap.h | 44 ++++++++++++++++++++++ xen/include/xen/compiler.h | 12 ++++++ 5 files changed, 120 insertions(+), 40 deletions(-)create mode 100644
xen/include/xen/byteorder.hcreate mode 100644xen/include/xen/byteswap.hxen/arch/arm/include/asm/byteorder.h +4
-10@@ -1,16 +1,10 @@#ifndef __ASM_ARM_BYTEORDER_H__#define __ASM_ARM_BYTEORDER_H__ -#define __BYTEORDER_HAS_U64__ +#ifndef __BYTE_ORDER__ +#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ +#endif -#include <xen/byteorder/little_endian.h> +#include <xen/byteorder.h> #endif /* __ASM_ARM_BYTEORDER_H__ */ -/* -* Local variables: -* mode: C -* c-file-style: "BSD" -* c-basic-offset: 4 -* indent-tabs-mode: nil -* End: -*/
Quoted 3 lines
Quoted 1 line
Quoted 1 line
This change looks unrelated to this patch. Can you explain it?
Do you mean following code block? Yes, it is unrelated, I removed it as I found some files does not include such field.
So in general we try to avoid unrelated change within a same patch. In this case, the emacs magic should be present in the files rather than the other way around.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 4/6] xen: Switch to byteswap
In reply to Julien GrallQuoted 5 lines · Julien Grall
Quoted 4 lines
It is not reasonable to say "this unrelated thing is broken, and you need to fix it first to get your series in". Requests like that are, I'm sure, part of what Bertrand raised in the community call as unnecessary fiction getting work submitted.
Quoted 7 lines
To be honest, you put the contributor in this situation. I would have been perfectly happy if we keep *cpup* around as there would be only a place to fix.
With this approach, you are effectively going to increase the work later one because now we would have to chase all the open-coded version of *cpup* and check which one is not safe.
Without disagreeing with Julien or Andrew, I am actually happy to see an effort to make the review process faster. We have lot of room for improvement and spotting opportunities to do so is the first step toward improving the process. I have actually been thinking about how to make things faster in cases like this and I have a suggesion below.
In this case all of the options are OK. Whether we fix the alignment problem as part of this patch or soon after it doesn't make much of a difference. It is more important that we don't get bogged down in a long discussion. Coding is faster and more fun.
It would take less time for Julien (or Andrew) to write the code than to explain to the contributor how to do it. English is a good language for an architectural discussion, but simply replying with the example code in C would be much faster in cases like this one.
So my suggestion is that it would be best if the reviewer (Julien in this case) replied directly with the code snipper he wants added. Just an example without looking too closely:
---
Please do this instead so that alignment also gets fixed:
return be16_to_cpu(*(const __packed uint16_t *)p);
---
Alternatively, I also think that taking this patch as is without alignment fix (either using be16_to_cpu or be16_to_cpup) is fine. The alignment could be fixed afterwards. The key is that I think it should be one of the maintainers to write the follow-up fix. Both of you are very fast coders and would certainly finish the patch before finishing the explanation on what needs to be done, which then would need to be understood and implemented by the contributor (opportunity for misunderstandings), and verified again by the reviewer on v2. That would take an order of magnitude more of collective time and effort.
Of course this doesn't apply to all cases, but it should apply to quite a few.
In short, less English, more C ;-)
Re: [PATCH v3 4/6] xen: Switch to byteswap
In reply to Julien GrallSubject: Re: [PATCH v3 4/6] xen: Switch to byteswap Hi,
Quoted 59 lines · Lin Liu
Update to use byteswap to swap bytes.
No functional change.
Signed-off-by Lin Liu <[email protected]>---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
Changes in v3:
- Updatexen/common/device_tree.cto usebe32_to_cpu
- Keep const in type cast inunaligned.h--- xen/common/device_tree.c | 44 +++++++++++++++--------------- xen/common/libelf/libelf-private.h | 6 ++-- xen/common/xz/private.h | 2 +- xen/include/xen/unaligned.h | 24 ++++++++-------- 4 files changed, 38 insertions(+), 38 deletions(-)xen/common/device_tree.c +1
-1@@ -171,7 +171,7 @@ bool_t dt_property_read_u32(const struct dt_device_node *np,if ( !val || len <sizeof (*out_value) )return 0 ; - *out_value =be32_to_cpup (val); + *out_value =be32_to_cpu (*val);This code has been taken from Linux and I would rather prefer to keep the *cpup* helpers to avoid any changes when backporting.
xen/include/xen/unaligned.h +1
-1@@ -20,62 +20,62 @@static inline uint16_t get_unaligned_be16 (const void *p) { -return be16_to_cpup (p); +return be16_to_cpu (*(const uint16_t *)p)I haven't checked the existing implementation of
be16_to_cpup(). However, this new approach would allow the compiler to use a single load instruction to read the 16-bit value from memory. So this change may break on platform where unaligned access is forbidden (such as arm32).}static inline void
put_unaligned_be16(uint16_t val, void *p){xen/include/xen/unaligned.h +1
-1- *(__force __be16 *)p =cpu_to_be16 (val); + *(__be16 *)p =cpu_to_be16 (val);
Quoted 1 line
Quoted 1 line
Why did you drop the
__force?
Google told me __force is used in linux kernel to suppress warning in sparse,
https://stackoverflow.com/questions/53120610/what-does-the-attribute-force-do
Is sparse also used in xen?
Quoted 2 lines
}
Re: [PATCH v3 4/6] xen: Switch to byteswap
In reply to Stefano StabelliniHi,
Quoted 9 lines · Stefano Stabellini
Alternatively, I also think that taking this patch as is without alignment fix (either using
be16_to_cpuorbe16_to_cpup) is fine. The alignment could be fixed afterwards. The key is that I think it should be one of the maintainers to write the follow-up fix. Both of you are very fast coders and would certainly finish the patch before finishing the explanation on what needs to be done, which then would need to be understood and implemented by the contributor (opportunity for misunderstandings), and verified again by the reviewer on v2. That would take an order of magnitude more of collective time and effort.
I am happy to write a follow-up patch so long be16_to_cpup() (& co) are kept (because this is where I want to fix this issue so it is done for everyone).
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 4/6] xen: Switch to byteswap
In reply to Lin Liu (刘林)Hi,
Please configure your e-mail client to send in plain text.
Quoted 61 lines · Lin Liu (刘林)
Subject: Re: [PATCH v3 4/6] xen: Switch to byteswap
Quoted 58 lines · Lin Liu
Update to use byteswap to swap bytes.
No functional change.
Signed-off-by Lin Liu <[email protected]>---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
Changes in v3:
- Updatexen/common/device_tree.cto usebe32_to_cpu
- Keep const in type cast inunaligned.h--- xen/common/device_tree.c | 44 +++++++++++++++--------------- xen/common/libelf/libelf-private.h | 6 ++-- xen/common/xz/private.h | 2 +- xen/include/xen/unaligned.h | 24 ++++++++-------- 4 files changed, 38 insertions(+), 38 deletions(-)xen/common/device_tree.c +1
-1@@ -171,7 +171,7 @@ bool_t dt_property_read_u32(const struct dt_device_node *np,if ( !val || len <sizeof (*out_value) )return 0 ; - *out_value =be32_to_cpup (val); + *out_value =be32_to_cpu (*val);This code has been taken from Linux and I would rather prefer to keep the *cpup* helpers to avoid any changes when backporting.
xen/include/xen/unaligned.h +1
-1@@ -20,62 +20,62 @@static inline uint16_t get_unaligned_be16 (const void *p) { -return be16_to_cpup (p); +return be16_to_cpu (*(const uint16_t *)p)I haven't checked the existing implementation of
be16_to_cpup(). However, this new approach would allow the compiler to use a single load instruction to read the 16-bit value from memory. So this change may break on platform where unaligned access is forbidden (such as arm32).}static inline void
put_unaligned_be16(uint16_t val, void *p){xen/include/xen/unaligned.h +1
-1- *(__force __be16 *)p =cpu_to_be16 (val); + *(__be16 *)p =cpu_to_be16 (val);
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Why did you drop the
__force?Google told me
__forceis used in linux kernel to suppress warning in sparse,
https://stackoverflow.com/questions/53120610/what-does-the-attribute-force-do
Is sparse also used in xen?
I am not aware of any use of Sparse in Xen, but it would technically be possible.
However, my point here is more that this change seems to be unrelated to what the patch is meant to do (i.e. switching to byteswap). So if it is unnecessary, then it should be dropped from this patch.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 4/6] xen: Switch to byteswap
In reply to Julien GrallQuoted 33 lines · Julien Grall
Hi,
Quoted 30 lines · Andrew Cooper
Quoted 28 lines · Julien Grall
Quoted 15 lines
Quoted 14 lines
xen/include/xen/unaligned.h line 22
Quoted 12 lines
xen/include/xen/unaligned.h +1
-1@@ -20,62 +20,62 @@ static inline uint16_t get_unaligned_be16(const void *p) { - return be16_to_cpup(p); + return be16_to_cpu(*(const uint16_t *)p)I haven't checked the existing implementation of
be16_to_cpup().Quoted 2 lines
It's a plain dereference, just like this. AFAICT, it wasn't unaligned safe before, either.
Well, technically an architecture could provide an override for the copy. I agree that arm32 is already bogus but...
Quoted 4 lines
It should be reasonably easy to fix in a followup patch. Just
memcpy()
to/from the void pointer to a stack variable of the appropriate type.... I disagree that it should be fixed in a follow-up patch. It should be fixed now as this is where the badness is spread to any architecture.
Quoted 7 lines
Quoted 5 lines
No. That is an inappropriate request to make.
Lin's patch does not alter the broken-ness of unaligned on arm32, and does improve the aspect of the hypervisor that it pertains to. It therefore stands on its own merit.
I am not sure sure why switching from *cpup* improves things... and as usual you haven't answered to the clarification questions.
Adjust your tone back to something appropriate to the conversation.
The p helpers hide a unsafe typecast&dereference which will erroneously compile both of these:
void foo(void *ptr)
{
blah_p(ptr);
}
void bar(baz *ptr)
{
blah_p(ptr);
}
and potentially malfunction as a consequence, not to mention that it's sufficient obfuscation to trick a maintainer into believe an unrelated area of code was safe when it wasn't.
Deleting the p helpers is a specific objective of the work, because it forces the author to resolve to an integral type, and have the deference chain visible in a single location which improves code clarity.
On a hunch, I checked the MISRA spec, and it turns out there is a rule against the p helpers (specifically the type safety aspect), so this series is removing a whole load of DIR 4.9 violations from the codebase.
~Andrew
Re: [PATCH v3 4/6] xen: Switch to byteswap
Quoted 36 lines · Andrew Cooper
Quoted 35 lines · Julien Grall
Hi,
Quoted 32 lines · Andrew Cooper
Quoted 22 lines · Julien Grall
Quoted 15 lines
Quoted 14 lines
xen/include/xen/unaligned.h line 22
Quoted 12 lines
xen/include/xen/unaligned.h +1
-1@@ -20,62 +20,62 @@ static inline uint16_t get_unaligned_be16(const void *p) { - return be16_to_cpup(p); + return be16_to_cpu(*(const uint16_t *)p)I haven't checked the existing implementation of
be16_to_cpup().Quoted 2 lines
It's a plain dereference, just like this. AFAICT, it wasn't unaligned safe before, either.
Well, technically an architecture could provide an override for the copy. I agree that arm32 is already bogus but...
Quoted 1 line
Quoted 6 lines
Quoted 3 lines
It should be reasonably easy to fix in a followup patch. Just
memcpy()
to/from the void pointer to a stack variable of the appropriate type.... I disagree that it should be fixed in a follow-up patch. It should be fixed now as this is where the badness is spread to any architecture.
No. That is an inappropriate request to make.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Lin's patch does not alter the broken-ness of unaligned on arm32, and does improve the aspect of the hypervisor that it pertains to. It therefore stands on its own merit.
I am not sure sure why switching from *cpup* improves things... and as usual you haven't answered to the clarification questions.
Adjust your tone back to something appropriate to the conversation.
It was indeed harsh. Sorry for that.
Quoted 9 lines
The p helpers hide a unsafe typecast&dereference which will erroneously compile both of these:
void
foo(void *ptr)
{
blah_p(ptr);
}
Quoted 4 lines
void
bar(baz *ptr)
{
blah_p(ptr);
}
I am assuming that blah would expect a (blah *).
Quoted 4 lines
and potentially malfunction as a consequence, not to mention that it's sufficient obfuscation to trick a maintainer into believe an unrelated area of code was safe when it wasn't.
I looked at the helpers, they are static inline and use a proper type. Therefore, I am not sure why bar would compile in this situation.
In fact, to me it seems this is an inherent issue to C: any void pointer can be casted to anything. You are removing one here, but there are hundreds of other potential "unsafe" place in Xen.
Quoted 4 lines
Deleting the p helpers is a specific objective of the work, because it forces the author to resolve to an integral type, and have the deference chain visible in a single location which improves code clarity.
See above, I think dropping p helpers is not solving the underlying problem (we are not going to be able to remove pointers in Xen).
What would solve the problem is forbidding cast from void pointer to any pointer. At which point, keeping *cpup* is not going to be problem.
Quoted 4 lines
On a hunch, I checked the MISRA spec, and it turns out there is a rule against the p helpers (specifically the type safety aspect), so this series is removing a whole load of DIR 4.9 violations from the codebase.
I read through DIR 4.9., AFAIU it is about using function rather than macro. The current implementation of *cpup* are using function so I don't understand how removing *cpup* would help.
So I am afraid, I still see no justifications to drop *cpup* here and I actually prefer them the open-code version. I will not Nack it, but I will not support.
In any case, the commit message should contain some information why they are dropped.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 4/6] xen: Switch to byteswap
In reply to Julien GrallQuoted 65 lines · Julien Grall
Hi,
Please configure your e-mail client to send in plain text.
Quoted 56 lines · Lin Liu (刘林)
Subject: Re: [PATCH v3 4/6] xen: Switch to byteswap
Quoted 54 lines · Lin Liu
Update to use byteswap to swap bytes.
No functional change.
Signed-off-by Lin Liu <[email protected]>---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
Changes in v3:
- Updatexen/common/device_tree.cto usebe32_to_cpu
- Keep const in type cast inunaligned.h--- xen/common/device_tree.c | 44 +++++++++++++++--------------- xen/common/libelf/libelf-private.h | 6 ++-- xen/common/xz/private.h | 2 +- xen/include/xen/unaligned.h | 24 ++++++++-------- 4 files changed, 38 insertions(+), 38 deletions(-)xen/common/device_tree.c +1
-1@@ -171,7 +171,7 @@ bool_t dt_property_read_u32(const struct dt_device_node *np,if ( !val || len <sizeof (*out_value) )return 0 ; - *out_value =be32_to_cpup (val); + *out_value =be32_to_cpu (*val);This code has been taken from Linux and I would rather prefer to keep the *cpup* helpers to avoid any changes when backporting.
xen/include/xen/unaligned.h +1
-1@@ -20,62 +20,62 @@static inline uint16_t get_unaligned_be16 (const void *p) { -return be16_to_cpup (p); +return be16_to_cpu (*(const uint16_t *)p)I haven't checked the existing implementation of
be16_to_cpup(). However, this new approach would allow the compiler to use a single load instruction to read the 16-bit value from memory. So this change may break on platform where unaligned access is forbidden (such as arm32).
}static inline void
put_unaligned_be16(uint16_t val, void *p){xen/include/xen/unaligned.h +1
-1- *(__force __be16 *)p =cpu_to_be16 (val); + *(__be16 *)p =cpu_to_be16 (val);
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Why did you drop the
__force?Google told me
__forceis used in linux kernel to suppress warning in sparse,
https://stackoverflow.com/questions/53120610/what-does-the-attribute-force-do
Is sparse also used in xen?I am not aware of any use of Sparse in Xen, but it would technically be possible.
However, my point here is more that this change seems to be unrelated to what the patch is meant to do (i.e. switching to byteswap). So if it is unnecessary, then it should be dropped from this patch.
I think making people pull little changes like this out into separate patches is asking too much. It’s a lot of extra effort on the part of the submitter for basically no value. We commonly do little clean-ups like this in patches, and just require a comment at the bottom, like this:
8<—
While here:
- Drop ‘_force’ keyword, which is only needed when running the Sparse analysis tool
—>8
I do agree that minor changes like this need to be described, so that people 5 years from now have some hope of figuring out what’s going on.
-George
Re: [PATCH v3 4/6] xen: Switch to byteswap
Hi George,
Quoted 9 lines · George Dunlap
Quoted 7 lines
Quoted 3 lines
Google told me
__forceis used in linux kernel to suppress warning in sparse,
https://stackoverflow.com/questions/53120610/what-does-the-attribute-force-do
Is sparse also used in xen?I am not aware of any use of Sparse in Xen, but it would technically be possible.
However, my point here is more that this change seems to be unrelated to what the patch is meant to do (i.e. switching to byteswap). So if it is unnecessary, then it should be dropped from this patch.
I think making people pull little changes like this out into separate patches is asking too much. It’s a lot of extra effort on the part of the submitter for basically no value. We commonly do little clean-ups like this in patches, and just require a comment at the bottom, like this:
I suggested to drop from the patch because I don't think we should remove the __force. In fact, I have contemplated a few times in the past to use sparse in Xen.
Sorry I should have been clearer.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v3 4/6] xen: Switch to byteswap
In reply to Stefano StabelliniHi,
Quoted 17 lines · Stefano Stabellini
Quoted 5 lines · Julien Grall
Quoted 4 lines
It is not reasonable to say "this unrelated thing is broken, and you need to fix it first to get your series in". Requests like that are, I'm sure, part of what Bertrand raised in the community call as unnecessary fiction getting work submitted.
Quoted 7 lines
To be honest, you put the contributor in this situation. I would have been perfectly happy if we keep *cpup* around as there would be only a place to fix.
With this approach, you are effectively going to increase the work later one because now we would have to chase all the open-coded version of *cpup* and check which one is not safe.
Quoted 5 lines
Without disagreeing with Julien or Andrew, I am actually happy to see an effort to make the review process faster. We have lot of room for improvement and spotting opportunities to do so is the first step toward improving the process. I have actually been thinking about how to make things faster in cases like this and I have a suggesion below.
Definitely with you here, it is good to see that my message on review process and effort is actually in people’s minds :-)
Quoted 5 lines
In this case all of the options are OK. Whether we fix the alignment problem as part of this patch or soon after it doesn't make much of a difference. It is more important that we don't get bogged down in a long discussion. Coding is faster and more fun.
I would just make a small exception here (which corresponds to something Julien kind of suggested during the discussion): unless we introduce a temporary bug between the patches.
But this could actually be solved here by making a patch upfront and merging it before the one in discussion (which might require some rebasing).
Quoted 17 lines
It would take less time for Julien (or Andrew) to write the code than to explain to the contributor how to do it. English is a good language for an architectural discussion, but simply replying with the example code in C would be much faster in cases like this one.
So my suggestion is that it would be best if the reviewer (Julien in this case) replied directly with the code snipper he wants added. Just an example without looking too closely:
---
Please do this instead so that alignment also gets fixed:return
be16_to_cpu(*(const __packed uint16_t *)p);
---
Quoted 9 lines
Alternatively, I also think that taking this patch as is without alignment fix (either using
be16_to_cpuorbe16_to_cpup) is fine. The alignment could be fixed afterwards. The key is that I think it should be one of the maintainers to write the follow-up fix. Both of you are very fast coders and would certainly finish the patch before finishing the explanation on what needs to be done, which then would need to be understood and implemented by the contributor (opportunity for misunderstandings), and verified again by the reviewer on v2. That would take an order of magnitude more of collective time and effort.
Agree with the exception I mentioned.
Quoted 5 lines
Of course this doesn't apply to all cases, but it should apply to quite a few.
In short, less English, more C ;-)
Same goes for things like “please add a comment” or “please say something in the commit message”, it would be most of the time easier for everyone to do: Could you add the comment “xxx” on top of this or the sentence “yyy” in your commit (or even better ask the contributor if he is ok with it and do it on commit when it not modifying the code).
Cheers
Bertrand
Re: [PATCH v3 4/6] xen: Switch to byteswap
In reply to George DunlapHi George,
Quoted 78 lines · George Dunlap
Quoted 66 lines · Julien Grall
Hi,
Please configure your e-mail client to send in plain text.
Quoted 56 lines · Lin Liu (刘林)
Subject: Re: [PATCH v3 4/6] xen: Switch to byteswap
Quoted 54 lines · Lin Liu
Update to use byteswap to swap bytes.
No functional change.
Signed-off-by Lin Liu <[email protected]>---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
Changes in v3:
- Updatexen/common/device_tree.cto usebe32_to_cpu
- Keep const in type cast inunaligned.h
---xen/common/device_tree.c| 44 +++++++++++++++---------------xen/common/libelf/libelf-private.h| 6 ++--xen/common/xz/private.h| 2 +-xen/include/xen/unaligned.h| 24 ++++++++--------4 files changed, 38 insertions(+), 38 deletions(-)xen/common/device_tree.c +1
-1@@ -171,7 +171,7 @@ bool_t dt_property_read_u32(const struct dt_device_node *np,if ( !val || len <sizeof (*out_value) )return 0 ; - *out_value =be32_to_cpup (val); + *out_value =be32_to_cpu (*val);This code has been taken from Linux and I would rather prefer to keep the *cpup* helpers to avoid any changes when backporting.
xen/include/xen/unaligned.h +1
-1@@ -20,62 +20,62 @@static inline uint16_t get_unaligned_be16 (const void *p) { -return be16_to_cpup (p); +return be16_to_cpu (*(const uint16_t *)p)I haven't checked the existing implementation of
be16_to_cpup(). However, this new approach would allow the compiler to use a single load instruction to read the 16-bit value from memory. So this change may break on platform where unaligned access is forbidden (such as arm32). }static inline void
put_unaligned_be16(uint16_t val, void *p){
- *(__force __be16*)p =cpu_to_be16(val);
+ *(__be16 *)p =cpu_to_be16(val);Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Why did you drop the
__force?Google told me
__forceis used in linux kernel to suppress warning in sparse,
https://stackoverflow.com/questions/53120610/what-does-the-attribute-force-do
Is sparse also used in xen?I am not aware of any use of Sparse in Xen, but it would technically be possible.
However, my point here is more that this change seems to be unrelated to what the patch is meant to do (i.e. switching to byteswap). So if it is unnecessary, then it should be dropped from this patch.
I think making people pull little changes like this out into separate patches is asking too much. It’s a lot of extra effort on the part of the submitter for basically no value. We commonly do little clean-ups like this in patches, and just require a comment at the bottom, like this:
Quoted 8 lines
8<—
While here:
- Drop ‘_force’ keyword, which is only needed when running the Sparse analysis tool—>8
I do agree that minor changes like this need to be described, so that people 5 years from now have some hope of figuring out what’s going on.
I fully agree here. The effort involved by splitting a patch in several ones (both for the contributor and the maintainers) means it should be prevented unless the original patch could not be reviewed as is (patch to long or to complex to test for example but there might be other valid cases).
Modifying the commit message to describe all changes is definitely mandatory
though (but could be done without a v+1).
Bertrand
Quoted 2 lines
-George
Re: [PATCH v3 4/6] xen: Switch to byteswap
Quoted 24 lines · Bertrand Marquis
Quoted 24 lines · George Dunlap
Quoted 7 lines · Julien Grall
Quoted 5 lines · Lin Liu (刘林)
Quoted 4 lines · Lin Liu
static inline void
put_unaligned_be16(uint16_t val, void *p){
- *(__force __be16*)p =cpu_to_be16(val);
+ *(__be16 *)p =cpu_to_be16(val);Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Why did you drop the
__force?Google told me
__forceis used in linux kernel to suppress warning in sparse,
https://stackoverflow.com/questions/53120610/what-does-the-attribute-force-do
Is sparse also used in xen?I am not aware of any use of Sparse in Xen, but it would technically be possible.
However, my point here is more that this change seems to be unrelated to what the patch is meant to do (i.e. switching to byteswap). So if it is unnecessary, then it should be dropped from this patch.
I think making people pull little changes like this out into separate patches is asking too much. It’s a lot of extra effort on the part of the submitter for basically no value. We commonly do little clean-ups like this in patches, and just require a comment at the bottom, like this:
8<—
While here:
- Drop ‘_force’ keyword, which is only needed when running the Sparse analysis tool
Quoted 8 lines
Quoted 3 lines
—>8
I do agree that minor changes like this need to be described, so that people 5 years from now have some hope of figuring out what’s going on.
I fully agree here. The effort involved by splitting a patch in several ones (both for the contributor and the maintainers) means it should be prevented unless the original patch could not be reviewed as is (patch to long or to complex to test for example but there might be other valid cases).
This is fine for uncontroversial changes, but not in cases like this one. Like Julien, I think we should rather strive towards completing the Linux-inherited annotations like __force. Even without the actual use of sparse they serve a documentation purpose.
Jan
Re: [PATCH v3 6/6] tools: Remove unnecessary header
In reply to Lin Liutools/libs/guest/xg_dom_decompress_unsafe_zstd.c typedef uint64_t __be64; · line 36
Quoted 10 lines · Lin Liu
tools/libs/guest/xg_dom_decompress_unsafe_zstd.c +0
-1@@ -31,7 +31,6 @@ typedef uint64_t __be64;#define __BYTEORDER_HAS_U64__#define __TYPES_H__/* xen/types.h guard */ -#include "../../xen/include/xen/byteorder/little_endian.h" #define __ASM_UNALIGNED_H__/* asm/unaligned.h guard */ #include "../../xen/include/xen/unaligned.h" #include "../../xen/include/xen/xxhash.h"
Doesn't this need to come ahead of what is now patch 5?
Jan
回复: [PATCH v3 6/6] tools: Remove unnecessary header
Quoted 13 lines · Lin Liu
Quoted 10 lines
tools/libs/guest/xg_dom_decompress_unsafe_zstd.c +0
-0@@ -31,7 +31,6 @@ typedef uint64_t __be64;#define __BYTEORDER_HAS_U64__#define __TYPES_H__/* xen/types.h guard */ -#include"
../../xen/include/xen/byteorder/little_endian.h"
#define__ASM_UNALIGNED_H__/*asm/unaligned.hguard */ #include
"../../xen/include/xen/unaligned.h"
#include "../../xen/include/xen/xxhash.h"Doesn't this need to come ahead of what is now patch 5?
Quoted 1 line
Jan
Thanks for the feedback,
I am preparing a new patch series for this.