xen/lib: muldiv64() cleanup
cover xen/lib: muldiv64() cleanup
Split out of lib.h, remove x86-isms from the common library.
https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/2237126270
Andrew Cooper (5):
xen: Fix endian handling in muldiv64.c
arm/time: Sort headers
x86/time: Sort headers
xen: Split muldiv64() out of lib.h
xen: Move x86-ism out of muldiv64.c
xen/arch/arm/time.c | 21 ++++++++-------- xen/arch/riscv/include/asm/time.h | 4 +-- xen/arch/x86/emul-i8254.c | 12 ++++----- xen/arch/x86/include/asm/muldiv.h | 20 +++++++++++++++ xen/arch/x86/time.c | 42 ++++++++++++++++--------------- xen/common/bitops.c | 12 +++++++++ xen/include/xen/lib.h | 2 -- xen/include/xen/muldiv.h | 26 +++++++++++++++++++ xen/lib/muldiv64.c | 19 +++++--------- 9 files changed, 105 insertions(+), 53 deletions(-) create mode 100644 xen/arch/x86/include/asm/muldiv.h create mode 100644 xen/include/xen/muldiv.h
Signature · 1 line
2.39.5
PATCH 1/5 xen: Fix endian handling in muldiv64.c
WORDS_BIGENDIAN is a QEMU-ism. In the very dim and distant past, we had a fork of QEMU living in xen.git which wired WORDS_BIGENDIAN in the userspace part of the build, but nothing ever wired it up in the hypervisor build.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Oleksii Kurochko <[email protected]>
I'm not sure that this wants backporting, seeing as we have no big-endian architectures at all. Otherwse, 4.20 and older can't rely on the existence of __LITTLE_ENDIAN on older toolchains.
--- xen/lib/muldiv64.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
xen/lib/muldiv64.c +4 -2
@@ -13,10 +13,12 @@ uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)union {uint64_t ll;struct { -#ifdef WORDS_BIGENDIAN +#if defined(__BIG_ENDIAN)uint32_t high, low; -#else +#elif defined(__LITTLE_ENDIAN)uint32_t low, high; +#else +# error Unknown Endianness#endif } l; } u, res;
Signature · 1 line
2.39.5
Re: [PATCH 1/5] xen: Fix endian handling in muldiv64.c
Quoted 3 lines · Andrew Cooper
WORDS_BIGENDIANis a QEMU-ism. In the very dim and distant past, we had a fork of QEMU living in xen.git which wiredWORDS_BIGENDIANin the userspace part of the build, but nothing ever wired it up in the hypervisor build.
Perhaps the assumption was that big-endian targets would #define WORDS_BIGENDIAN in their asm/config.h?
Quoted 2 lines
Fixes 86d5da49a6f7 ("[HVM] Support multiple HVM time device models coming soon.")Signed-off-by Andrew Cooper <[email protected]>
Quoted 3 lines
I'm not sure that this wants backporting, seeing as we have no big-endian architectures at all. Otherwse, 4.20 and older can't rely on the existence of
__LITTLE_ENDIANon older toolchains.
This not being active breakage, I think we can omit the backporting here.
Jan
PATCH 2/5 arm/time: Sort headers
In reply to Andrew Cooper... in preparation for a logical change.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Oleksii Kurochko <[email protected]>
--- xen/arch/arm/time.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
xen/arch/arm/time.c +10 -9
@@ -8,26 +8,27 @@* Copyright (c) 2011 Citrix Systems. */ +#include <xen/acpi.h> #include <xen/console.h> +#include <xen/cpu.h> +#include <xen/delay.h> #include <xen/device_tree.h> +#include <xen/event.h> #include <xen/init.h> #include <xen/irq.h> #include <xen/lib.h> #include <xen/mm.h> -#include <xen/softirq.h> +#include <xen/notifier.h> #include <xen/sched.h> -#include <xen/time.h> -#include <xen/delay.h> #include <xen/sched.h> -#include <xen/event.h> -#include <xen/acpi.h> -#include <xen/cpu.h> -#include <xen/notifier.h> +#include <xen/softirq.h> +#include <xen/time.h> + +#include <asm/cpufeature.h> +#include <asm/platform.h> #include <asm/system.h> #include <asm/time.h> #include <asm/vgic.h> -#include <asm/cpufeature.h> -#include <asm/platform.h> uint64_t __read_mostly boot_count;
Signature · 1 line
2.39.5
Re: [PATCH 2/5] arm/time: Sort headers
Quoted 5 lines · Andrew Cooper
... in preparation for a logical change.
No functional change.
Signed-off-by Andrew Cooper <[email protected]>
~Michal
PATCH 3/5 x86/time: Sort headers
In reply to Andrew Cooper... in preparation for a logical change.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Oleksii Kurochko <[email protected]>
--- xen/arch/x86/emul-i8254.c | 12 +++++------ xen/arch/x86/time.c | 42 ++++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 26 deletions(-)
xen/arch/x86/emul-i8254.c +6 -6
@@ -24,19 +24,19 @@* IN THE SOFTWARE. */ -#include <xen/types.h> -#include <xen/mm.h> -#include <xen/xmalloc.h> -#include <xen/lib.h> #include <xen/errno.h> +#include <xen/lib.h> +#include <xen/mm.h> #include <xen/sched.h> #include <xen/trace.h> -#include <asm/time.h> +#include <xen/xmalloc.h> + +#include <asm/current.h> #include <asm/hvm/hvm.h> #include <asm/hvm/io.h> #include <asm/hvm/save.h> #include <asm/hvm/vpt.h> -#include <asm/current.h> +#include <asm/time.h> #define domain_vpit (x) (&(x)->arch.vpit)#define vcpu_vpit (x) (domain_vpit((x)->domain))
xen/arch/x86/time.c +22 -20
60 lines not on this page · raw message
Signature · 1 line
2.39.5
Re: [PATCH 3/5] x86/time: Sort headers
Quoted 5 lines · Andrew Cooper
... in preparation for a logical change.
No functional change.
Signed-off-by Andrew Cooper <[email protected]>
PATCH 4/5 xen: Split muldiv64() out of lib.h
In reply to Andrew Coopermuldiv64() has 7 users across all architectures, and is limited to time handling functions. Therefore, move it's declaration out of lib.h into a dedicated header.
Rename the library function to generic_muldiv64(), as x86 is going to provide an arch_muldiv64() in a subsequent change.
Explain what the function does, including the limitations; x86's version will suffer a divide error rather than truncate the result to 64 bits.
Annotate it with attr_const, not that this allows the optimiser to improve any of Xen's current users. Add one selftest to check the internal precision, putting it in bitops.c for want of any better place to locate it.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Oleksii Kurochko <[email protected]>
The reason the selftest can't go in lib/muldiv64.c is because doing so would exclude the test when the arch variant is used.
--- xen/arch/arm/time.c | 2 +- xen/arch/riscv/include/asm/time.h | 4 ++-- xen/arch/x86/emul-i8254.c | 2 +- xen/arch/x86/time.c | 2 +- xen/common/bitops.c | 12 ++++++++++++ xen/include/xen/lib.h | 2 -- xen/include/xen/muldiv.h | 18 ++++++++++++++++++ xen/lib/muldiv64.c | 5 ++--- 8 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 xen/include/xen/muldiv.h
xen/arch/arm/time.c +1 -1
@@ -16,8 +16,8 @@#include <xen/event.h> #include <xen/init.h> #include <xen/irq.h> -#include <xen/lib.h> #include <xen/mm.h> +#include <xen/muldiv.h> #include <xen/notifier.h> #include <xen/sched.h> #include <xen/sched.h>
xen/arch/riscv/include/asm/time.h +2 -2
@@ -3,8 +3,8 @@#define ASM__RISCV__TIME_H #include <xen/bug.h> -#include <xen/lib.h> -#include <xen/types.h> +#include <xen/muldiv.h> +#include <asm/csr.h> /* Clock cycles count at Xen startup */
xen/arch/x86/emul-i8254.c +1 -1
@@ -25,8 +25,8 @@*/ #include <xen/errno.h> -#include <xen/lib.h> #include <xen/mm.h> +#include <xen/muldiv.h> #include <xen/sched.h> #include <xen/trace.h> #include <xen/xmalloc.h>
xen/arch/x86/time.c +1 -1
@@ -17,7 +17,7 @@#include <xen/init.h> #include <xen/irq.h> #include <xen/keyhandler.h> -#include <xen/lib.h> +#include <xen/muldiv.h> #include <xen/param.h> #include <xen/pci_ids.h> #include <xen/sched.h>
xen/common/bitops.c +12 -0
@@ -247,3 +247,15 @@ static void __init __constructor test_bitops(void)test_multiple_bits_set ();test_hweight (); } + +#include <xen/muldiv.h> + +/* Not a bitop, but here in lieu of any any better location */ +static void __init __constructor test_muldiv64(void) +{ +uint64_t res =muldiv64 (0xffffffffffffffffULL , +HIDE (0xffffffffU ), +HIDE (0xffffffffU )); +if ( res !=0xffffffffffffffffULL ) +panic ("muldiv64(), expecting -1ULL, got 0x%" PRIx64"\n" , res); +}
xen/include/xen/lib.h +0 -2
@@ -136,8 +136,6 @@ unsigned long long simple_strtoull(unsigned long long parse_size_and_unit (const char *s,const char **ps); -uint64_t muldiv64 (uint64_t a,uint32_t b,uint32_t c); -/* * A slightly more typesafe variant of cmpxchg() when the entities dealt with * are pointers.
xen/include/xen/muldiv.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef XEN_MULDIV_H +#define XEN_MULDIV_H + +#include <xen/stdint.h> + +uint64_t attr_const generic_muldiv64(uint64_t a, uint32_t b, uint32_t c); + +/* +* Calculate a * b / c using at least 96-bit internal precision. The +* behaviour is undefined if the end result does not fit in a uint64_t. +*/ +static inline uint64_t attr_const muldiv64(uint64_t a, uint32_t b, uint32_t c) +{ +return generic_muldiv64 (a, b, c); +} + +#endif /* XEN_MULDIV_H */
xen/lib/muldiv64.c +2 -3
@@ -1,7 +1,6 @@ -#include <xen/lib.h> +#include <xen/muldiv.h> -/* Compute with 96 bit intermediate result: (a*b)/c */ -uint64_t muldiv64 (uint64_t a,uint32_t b,uint32_t c) +uint64_t generic_muldiv64 (uint64_t a,uint32_t b,uint32_t c) {#ifdef CONFIG_X86 asm ( "mulq %1; divq %2" : "+a" (a)
Signature · 1 line
2.39.5
Re: [PATCH 4/5] xen: Split muldiv64() out of lib.h
Quoted 7 lines · Andrew Cooper
xen/common/bitops.c +1
-0@@ -247,3 +247,15 @@ static void __init __constructor test_bitops(void)test_multiple_bits_set ();test_hweight (); } +
xen/common/bitops.c static void __init __constructor test_bitops(void) · line 260
Quoted 10 lines
xen/common/bitops.c +10
-0+#include <xen/muldiv.h> + +/* Not a bitop, but here in lieu of any any better location */ +static void __init __constructor test_muldiv64(void) +{ +uint64_t res =muldiv64 (0xffffffffffffffffULL , +HIDE (0xffffffffU ), +HIDE (0xffffffffU )); +if ( res !=0xffffffffffffffffULL ) +panic ("muldiv64(), expecting -1ULL, got 0x%" PRIx64"\n" , res);
Nit: Better %#"PRIX64".
Quoted 9 lines
xen/include/xen/muldiv.h new file +6
-0@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef XEN_MULDIV_H +#define XEN_MULDIV_H + +#include <xen/stdint.h> +
xen/include/xen/muldiv.h line 16
Quoted 10 lines
xen/include/xen/muldiv.h new file +10
-0+uint64_t attr_const generic_muldiv64(uint64_t a, uint32_t b, uint32_t c); + +/* +* Calculate a * b / c using at least 96-bit internal precision. The +* behaviour is undefined if the end result does not fit in a uint64_t. +*/ +static inline uint64_t attr_const muldiv64(uint64_t a, uint32_t b, uint32_t c) +{ +return generic_muldiv64 (a, b, c); +}
As to the file name: Are you expecting any other forms to appear here? Else why not make it muldiv64.h, matching the .c file's base name? I'm not going to insist though, so even with just the earlier nit addressed:
Jan
Re: [PATCH 4/5] xen: Split muldiv64() out of lib.h
In reply to Andrew CooperQuoted 7 lines · Andrew Cooper
muldiv64()has 7 users across all architectures, and is limited to time handling functions. Therefore, move it's declaration out oflib.hinto a dedicated header.Rename the library function to
generic_muldiv64(), as x86 is going to provide anarch_muldiv64()in a subsequent change.
Quoted 8 lines
Explain what the function does, including the limitations; x86's version will suffer a divide error rather than truncate the result to 64 bits.
Annotate it with
attr_const, not that this allows the optimiser to improve any of Xen's current users. Add one selftest to check the internal precision, putting it inbitops.cfor want of any better place to locate it.Signed-off-by Andrew Cooper <[email protected]>
For Arm:
~Michal
PATCH 5/5 xen: Move x86-ism out of muldiv64.c
In reply to Andrew CooperHaving an #ifdef CONFIG_X86 section in a common library is rude.
Furthermore, for x86 the main logic is 6 bytes, meaning it's ripe for inlining. Create an x86-specific asm/muldiv.h implementing arch_muldiv64().
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Oleksii Kurochko <[email protected]>
bloat-o-meter results:
add/remove: 0/1 grow/shrink: 1/2 up/down: 10/-39 (-29)
Function old new delta get_count 240 250 +10 calibrate_apic_timer 211 203 -8 calibrate_tsc 190 177 -13
muldiv64 18 - -18
The increase in get_count is from different register scheduling.
--- xen/arch/x86/include/asm/muldiv.h | 20 ++++++++++++++++++++ xen/include/xen/muldiv.h | 8 ++++++++ xen/lib/muldiv64.c | 8 -------- 3 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 xen/arch/x86/include/asm/muldiv.h
xen/arch/x86/include/asm/muldiv.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef X86_MULDIV_H +#define X86_MULDIV_H + +#include <xen/stdint.h> + +static inline uint64_t attr_const arch_muldiv64(uint64_t a, uint32_t b, uint32_t c) +{ +asm_inline ( +"mulq %[b]\n\t" +"divq %[c]" + : "+a" (a) + : [b] "rm" ((uint64_t)b), [c] "rm" ((uint64_t)c) + : "rdx" ); + + return a; +} +#define arch_muldiv64 arch_muldiv64 + +#endif /* X86_MULDIV_H */
xen/include/xen/muldiv.h +8 -0
@@ -4,6 +4,10 @@#include <xen/stdint.h> +#if __has_include(<asm/muldiv.h>) +# include <asm/muldiv.h> +#endif + uint64_t attr_const generic_muldiv64(uint64_t a, uint32_t b, uint32_t c);/*
@@ -12,7 +16,11 @@ uint64_t attr_const generic_muldiv64(uint64_t a, uint32_t b, uint32_t c);*/ static inline uint64_t attr_const muldiv64(uint64_t a, uint32_t b, uint32_t c) { +#ifdef arch_muldiv64 +return arch_muldiv64 (a, b, c); +#else return generic_muldiv64 (a, b, c); +#endif }#endif /* XEN_MULDIV_H */
xen/lib/muldiv64.c +0 -8
@@ -2,13 +2,6 @@ uint64_t generic_muldiv64(uint64_t a, uint32_t b, uint32_t c) { -#ifdef CONFIG_X86 - asm ( "mulq %1; divq %2" : "+a" (a) - : "rm" ((uint64_t)b), "rm" ((uint64_t)c) - : "rdx" ); - - return a; -#else union { uint64_t ll; struct {
@@ -31,7 +24,6 @@ uint64_t generic_muldiv64(uint64_t a, uint32_t b, uint32_t c) res.l.low = (((rh % c) <<32 ) + (uint32_t )rl) / c;return res.ll; -#endif }/*
Signature · 1 line
2.39.5
Re: [PATCH 5/5] xen: Move x86-ism out of muldiv64.c
Quoted 3 lines · Andrew Cooper
Having an #ifdef
CONFIG_X86section in a common library is rude.Furthermore, for x86 the main logic is 6 bytes,
If both insns' operands are registers.
Quoted 4 lines
meaning it's ripe for
inlining. Create an x86-specificasm/muldiv.himplementingarch_muldiv64().Signed-off-by Andrew Cooper <[email protected]>
Jan