MIPS: Loongson, add sync before target of branch between llsc
PATCH 1/2 MIPS: Loongson, add sync before target of branch between llsc
From: YunQiang Su <[email protected]>
Loongson 2G/2H/3A/3B is quite weak sync'ed. If there is a branch, and the target is not in the scope of ll/sc or lld/scd, a sync is needed at the postion of target.
Loongson doesn't plan to fix this problem in future, so we add the sync here for any condition.
This is based on the patch from Chen Huacai.
--- arch/mips/mm/tlbex.c | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/mips/mm/tlbex.c +9 -0
@@ -932,6 +932,8 @@ build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,* to mimic that here by taking a load/istream page * fault. */ +if (current_cpu_type () ==CPU_LOONGSON3 ) +uasm_i_sync (p,0 );UASM_i_LA (p, ptr, (unsigned long )tlb_do_page_fault_0);uasm_i_jr (p, ptr);
@@ -1556,6 +1558,7 @@ static void build_loongson3_tlb_refill_handler(void)if (check_for_high_segbits) {uasm_l_large_segbits_fault (&l, p); +uasm_i_sync (&p,0 );UASM_i_LA (&p,K1 , (unsigned long )tlb_do_page_fault_0);uasm_i_jr (&p,K1 );uasm_i_nop (&p);
@@ -2259,6 +2262,8 @@ static void build_r4000_tlb_load_handler(void)#endif uasm_l_nopage_tlbl (&l, p); +if (current_cpu_type () ==CPU_LOONGSON3 ) +uasm_i_sync (&p,0 );build_restore_work_registers (&p);#ifdef CONFIG_CPU_MICROMIPS if ((unsigned long )tlb_do_page_fault_0 &1 ) {
@@ -2313,6 +2318,8 @@ static void build_r4000_tlb_store_handler(void)#endif uasm_l_nopage_tlbs (&l, p); +if (current_cpu_type () ==CPU_LOONGSON3 ) +uasm_i_sync (&p,0 );build_restore_work_registers (&p);#ifdef CONFIG_CPU_MICROMIPS if ((unsigned long )tlb_do_page_fault_1 &1 ) {
@@ -2368,6 +2375,8 @@ static void build_r4000_tlb_modify_handler(void)#endif uasm_l_nopage_tlbm (&l, p); +if (current_cpu_type () ==CPU_LOONGSON3 ) +uasm_i_sync (&p,0 );build_restore_work_registers (&p);#ifdef CONFIG_CPU_MICROMIPS if ((unsigned long )tlb_do_page_fault_1 &1 ) {
Signature · 1 line
2.20.1
PATCH 2/2 MIPS: Loongson, workaround ll/sc weak ordering
From: YunQiang Su <[email protected]>
On the Loongson-2G/2H/3A/3B there is a hardware flaw that ll/sc and lld/scd is very weak ordering. We should add sync instructions before each ll/lld and after the last sc/scd to workaround. Otherwise, this flaw will cause deadlock occationally (e.g. when doing heavy load test with LTP).
We introduced an gcc/as option "-mfix-loongson3-llsc", this option inserts sync before ll, and so some addresses in __ex_table will need to be shift.
Not all Loongson CPU have this problem, aka Loongson starts to solve it in their new models, such as the last series Loongson 3A 3000. So for kerenel we introduce a config option CPU_LOONGSON3_WORKAROUND_LLSC, with this option enabled, we will add "-mfix-loongson3-llsc" to cc-option.
This is based on the patch from Huacai Chen.
--- arch/mips/Kconfig | 19 +++++++++++++++++++ arch/mips/Makefile | 5 +++++ arch/mips/include/asm/futex.h | 20 ++++++++++++-------- arch/mips/mm/tlbex.c | 3 +++ 4 files changed, 39 insertions(+), 8 deletions(-)
arch/mips/Kconfig +19 -0
@@ -1385,6 +1385,25 @@ config CPU_LOONGSON3 The Loongson 3 processor implements the MIPS64R2 instruction set with many extensions. +config CPU_LOONGSON3_WORKAROUND_LLSC + bool "Workaround the LL/SC weak ordering" + default n + depends on CPU_LOONGSON3 + help + On the Loongson-2G/2H/3A/3B there is a hardware flaw that ll/sc and + lld/scd is very weak ordering. We should add sync instructions before + each ll/lld and after the last sc/scd to workaround. Otherwise, this + flaw will cause deadlock occationally (e.g. when doing heavy load test + with LTP). + + We introduced a gcc/as option "-mfix-loongson3-llsc", this option + inserts sync before ll, and so some addresses in __ex_table will need + to be shift. + + Newer model has solve this problem, such as the last series of 3A 3000 + but not all 3A 3000. If you want enable this workaround for older + Loongson's CPU, please say 'Y' here. + config LOONGSON3_ENHANCEMENT bool "New Loongson 3 CPU Enhancements" default n
arch/mips/Makefile +5 -0
@@ -194,6 +194,11 @@ cflags-$(CONFIG_CPU_CAVIUM_OCTEON) += -Wa,-march=octeon endif cflags-$(CONFIG_CAVIUM_CN63XXP1) += -Wa,-mfix-cn63xxp1 cflags-$(CONFIG_CPU_BMIPS) += -march=mips32 -Wa,-mips32 -Wa,--trap +ifeq ($(CONFIG_CPU_LOONGSON3_WORKAROUND_LLSC),y) +cflags-y += -mfix-loongson3-llsc +else +cflags-y += $(call cc-option,-mno-fix-loongson3-llsc,) +endif cflags-$(CONFIG_CPU_R4000_WORKAROUNDS) += $(call cc-option,-mfix-r4000,) cflags-$(CONFIG_CPU_R4400_WORKAROUNDS) += $(call cc-option,-mfix-r4400,)
arch/mips/include/asm/futex.h +12 -8
@@ -18,6 +18,14 @@#include <asm/errno.h> #include <asm/war.h> +#if defined(__mips_fix_loongson3_llsc) && defined(CONFIG_CPU_LOONGSON3_WORKAROUND_LLSC ) +# define LL_SHIFT_UA __UA_ADDR "\t(1b+0), 4b \n" \ + __UA_ADDR "\t(1b+4), 4b \n" \ + __UA_ADDR "\t(2b+0), 4b \n" +#else +# define LL_SHIFT_UA __UA_ADDR "\t1b, 4b \n" \ + __UA_ADDR "\t2b, 4b \n" +#endif #define __futex_atomic_op (insn, ret, oldval, uaddr, oparg) \ { \ if (cpu_has_llsc && R10000_LLSC_WAR) { \
@@ -41,8 +49,7 @@ " j 3b \n" \ " .previous \n" \ " .section __ex_table,\"a\" \n" \ -" " __UA_ADDR"\t1b, 4b \n" \ -" " __UA_ADDR"\t2b, 4b \n" \ + LL_SHIFT_UA \ " .previous \n" \ : "=r" (ret), "=&r" (oldval), \"=" GCC_OFF_SMALL_ASM () (*uaddr) \
@@ -70,8 +77,7 @@ " j 3b \n" \ " .previous \n" \ " .section __ex_table,\"a\" \n" \ -" " __UA_ADDR"\t1b, 4b \n" \ -" " __UA_ADDR"\t2b, 4b \n" \ + LL_SHIFT_UA \ " .previous \n" \ : "=r" (ret), "=&r" (oldval), \"=" GCC_OFF_SMALL_ASM () (*uaddr) \
@@ -155,8 +161,7 @@ futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr," j 3b \n" " .previous \n" " .section __ex_table,\"a\" \n" -" " __UA_ADDR"\t1b, 4b \n" -" " __UA_ADDR"\t2b, 4b \n" +LL_SHIFT_UA " .previous \n" : "+r" (ret), "=&r" (val), "=" GCC_OFF_SMALL_ASM() (*uaddr) : GCC_OFF_SMALL_ASM() (*uaddr), "Jr" (oldval), "Jr" (newval),
@@ -185,8 +190,7 @@ futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr," j 3b \n" " .previous \n" " .section __ex_table,\"a\" \n" -" " __UA_ADDR"\t1b, 4b \n" -" " __UA_ADDR"\t2b, 4b \n" +LL_SHIFT_UA " .previous \n" : "+r" (ret), "=&r" (val), "=" GCC_OFF_SMALL_ASM() (*uaddr) : GCC_OFF_SMALL_ASM() (*uaddr), "Jr" (oldval), "Jr" (newval),
arch/mips/mm/tlbex.c +3 -0
@@ -1649,6 +1649,9 @@ static void iPTE_LW(u32 **p, unsigned int pte, unsigned int ptr) {#ifdef CONFIG_SMP +# ifdef CONFIG_CPU_LOONGSON3_WORKAROUND_LLSC +uasm_i_sync (p,0 ); +# endif # ifdef CONFIG_PHYS_ADDR_T_64BIT if (cpu_has_64bits)uasm_i_lld (p, pte,0 , ptr);
Signature · 1 line
2.20.1
Hi YunQiang,
Quoted 3 lines · YunQiang Su
Loongson 2G/2H/3A/3B is quite weak sync'ed. If there is a branch, and the target is not in the scope of ll/sc or lld/scd, a sync is needed at the postion of target.
OK, so is this the same issue that the second patch in the series is working around or a different one?
I'm pretty confused at this point about what the actual bugs are in these various Loongson CPUs. Could someone provide an actual errata writeup describing the bugs in detail?
What does "in the scope of ll/sc" mean?
What happens if a branch target is not "in the scope of ll/sc"?
How does the sync help?
Are jumps affected, or just branches?
Does this affect userland as well as the kernel?
...and probably more questions depending upon the answers to these ones.
Quoted 2 lines
Loongson doesn't plan to fix this problem in future, so we add the sync here for any condition.
So are you saying that future Loongson CPUs will all be buggy too, and someone there has said that they consider this to be OK..? I really really hope that is not true.
If hardware people say they're not going to fix their bugs then working around them is definitely not going to be a priority. It's one thing if a CPU designer says "oops, my bad, work around this & I'll fix it next time". It's quite another for them to say they're not interested in fixing their bugs at all.
Thanks,
Paul
Quoted 11 lines · 在 2019年1月10日,上午6:08,Paul Burton <[email protected]> 写道:
Hi YunQiang,
Quoted 3 lines · YunQiang Su
Loongson 2G/2H/3A/3B is quite weak sync'ed. If there is a branch, and the target is not in the scope of ll/sc or lld/scd, a sync is needed at the postion of target.
Quoted 9 lines
OK, so is this the same issue that the second patch in the series is working around or a different one?
I'm pretty confused at this point about what the actual bugs are in these various Loongson CPUs. Could someone provide an actual errata writeup describing the bugs in detail?
What does "in the scope of ll/sc" mean?
Loongson 3 series has some version, called, 1000, 2000, and 3000.
There are 2 bugs all about LL/SC. Let’s call them bug-1 and bug-2.
BUG-1: a `sync’ is needed before LL or LLD instruction.
This bug appears on 1000 only, and I am sure that it has been fixed in 3000.
BUG-2: if there is an branch instruction inside LL/SC, and the branch target is outside
of the scope of LL/SC, a sync’ is needed at the branch target. Aka, the first insn of the target branch should be sync’. Loongson said that, we don’t plan fix this problem in short time before they Designe a totally new core.
Quoted 1 line
What happens if a branch target is not "in the scope of ll/sc”?
At least they said that there won’t be a problem
Quoted 3 lines
How does the sync help?
Are jumps affected, or just branches?
I am not sure, so CC a Loongson people.
@Paul Hua
Quoted 3 lines
Does this affect userland as well as the kernel?
There is few place can trigger these 2 bugs in kernel. In user land we have to workaround in binutils:
https://www.sourceware.org/ml/binutils/2019-01/msg00025.html
In fact the kernel is the easiest since we can have a flavor build for Loongson.
Quoted 9 lines
...and probably more questions depending upon the answers to these ones.
Quoted 2 lines
Loongson doesn't plan to fix this problem in future, so we add the sync here for any condition.
So are you saying that future Loongson CPUs will all be buggy too, and someone there has said that they consider this to be OK..? I really really hope that is not true.
Bug is bug. It is not OK.
I blame these Loongson guys here.
Some Loongson guys is not so normal people.
Anyway they are a little more normal now, and anyway again, still abnormal.
Quoted 5 lines
If hardware people say they're not going to fix their bugs then working around them is definitely not going to be a priority. It's one thing if a CPU designer says "oops, my bad, work around this & I'll fix it next time". It's quite another for them to say they're not interested in fixing their bugs at all.
They have interests, while I guess the true reason is that they have no enough people and money to desgin a core, while this bug is quilt hard to fix.
Quoted 3 lines
Thanks,
Paul
Hi Yunqiang,
Quoted 29 lines · Yunqiang Su
Quoted 6 lines · 在 2019年1月10日,上午6:08,Paul Burton <[email protected]> 写道:
Quoted 3 lines · YunQiang Su
Loongson 2G/2H/3A/3B is quite weak sync'ed. If there is a branch, and the target is not in the scope of ll/sc or lld/scd, a sync is needed at the postion of target.
Quoted 8 lines
OK, so is this the same issue that the second patch in the series is working around or a different one?
I'm pretty confused at this point about what the actual bugs are in these various Loongson CPUs. Could someone provide an actual errata writeup describing the bugs in detail?
What does "in the scope of ll/sc" mean?
Loongson 3 series has some version, called, 1000, 2000, and 3000.
There are 2 bugs all about LL/SC. Let’s call them bug-1 and bug-2.
BUG-1: a `sync’ is needed before LL or LLD instruction.
This bug appears on 1000 only, and I am sure that it has been fixed in 3000.BUG-2: if there is an branch instruction inside LL/SC, and the branch target is outside
of the scope of LL/SC, async’ is needed at the branch target. Aka, the first insn of the target branch should besync’. Loongson said that, we don’t plan fix this problem in short time before they Designe a totally new core.
Quoted 3 lines
Quoted 1 line
What happens if a branch target is not "in the scope of ll/sc”?
At least they said that there won’t be a problem
You still didn't define what "in the scope of ll/sc" means - I'm guessing that you're referring to a branch target as "in scope" if it is in between the ll & sc instructions (inclusive?). But this is just a guess & clarity from people who actually know would be helpful.
And there must be a problem. The whole point of this is that there's a bug, right? If there's no problem then we don't need to do anything :)
From a look at the GCC patch it talks about placing a sync at a branch target if it *is* in between an ll & sc [1], which I just can't reconcile with the phrase "outside of the scope of LL/SC". Is the problem when a branch target *is* in between an ll & sc, or when it *is not* between an ll & sc?
Reading this kernel patch doesn't make it any clearer - for example the sync it emits in build_loongson3_tlb_refill_handler() is nowhere near an ll or sc instruction. Something doesn't add up here.
Quoted 6 lines
Quoted 3 lines
How does the sync help?
Are jumps affected, or just branches?
I am not sure, so CC a Loongson people.
@Paul Hua
Hi Paul - any help obtaining a detailed description of these bugs would be much appreciated. Even if you only have something in Chinese I can probably get someone to help translate.
Quoted 7 lines
Quoted 1 line
Does this affect userland as well as the kernel?
There is few place can trigger these 2 bugs in kernel. In user land we have to workaround in binutils:
https://www.sourceware.org/ml/binutils/2019-01/msg00025.htmlIn fact the kernel is the easiest since we can have a flavor build for Loongson.
My concern with regards to userland is that there's talk of a "deadlock"
- if userland can hit this & the CPU actually stalls then the system is hopelessly vulnerable to denial of service from a malicious or buggy userland program, or simply an innocent program unaware of the errata.
Quoted 14 lines
Quoted 8 lines
...and probably more questions depending upon the answers to these ones.
Quoted 2 lines
Loongson doesn't plan to fix this problem in future, so we add the sync here for any condition.
So are you saying that future Loongson CPUs will all be buggy too, and someone there has said that they consider this to be OK..? I really really hope that is not true.
Bug is bug. It is not OK.
I blame these Loongson guys here.
Some Loongson guys is not so normal people.
Anyway they are a little more normal now, and anyway again, still abnormal.
Quoted 8 lines
Quoted 5 lines
If hardware people say they're not going to fix their bugs then working around them is definitely not going to be a priority. It's one thing if a CPU designer says "oops, my bad, work around this & I'll fix it next time". It's quite another for them to say they're not interested in fixing their bugs at all.
They have interests, while I guess the true reason is that they have no enough people and money to desgin a core, while this bug is quilt hard to fix.
I'm not sure I fully understand what you're saying above, but essentially I want to know that Loongson care about fixing their CPU bugs. If they don't, and the bugs are as bad as they sound, then in my view working around them will only reinforce that producing CPUs with such serious bugs is a good idea.
So if anyone from Loongson is reading, I'd really like to hear that the above is a miscommunication & that you're not intending to knowingly design any further CPUs with these bugs.
Thanks,
Paul
[1] https://gcc.gnu.org/ml/gcc-patches/2018-12/msg01064.html
("Loongson3 need a sync before branch target that between ll and sc.")
Quoted 33 lines · Paul Burton <[email protected]> 于2019年1月11日周五 上午1:35写道:
Hi Yunqiang,
Quoted 29 lines · Yunqiang Su
Quoted 6 lines · 在 2019年1月10日,上午6:08,Paul Burton <[email protected]> 写道:
Quoted 3 lines · YunQiang Su
Loongson 2G/2H/3A/3B is quite weak sync'ed. If there is a branch, and the target is not in the scope of ll/sc or lld/scd, a sync is needed at the postion of target.
Quoted 8 lines
OK, so is this the same issue that the second patch in the series is working around or a different one?
I'm pretty confused at this point about what the actual bugs are in these various Loongson CPUs. Could someone provide an actual errata writeup describing the bugs in detail?
What does "in the scope of ll/sc" mean?
Loongson 3 series has some version, called, 1000, 2000, and 3000.
There are 2 bugs all about LL/SC. Let’s call them bug-1 and bug-2.
BUG-1: a `sync’ is needed before LL or LLD instruction.
This bug appears on 1000 only, and I am sure that it has been fixed in 3000.BUG-2: if there is an branch instruction inside LL/SC, and the branch target is outside
of the scope of LL/SC, async’ is needed at the branch target. Aka, the first insn of the target branch should besync’. Loongson said that, we don’t plan fix this problem in short time before they Designe a totally new core.
Quoted 9 lines
Quoted 3 lines
Quoted 1 line
What happens if a branch target is not "in the scope of ll/sc”?
At least they said that there won’t be a problem
You still didn't define what "in the scope of ll/sc" means - I'm guessing that you're referring to a branch target as "in scope" if it is in between the ll & sc instructions (inclusive?). But this is just a guess & clarity from people who actually know would be helpful.
Yes. your guess is correct. It is between.
Quoted 3 lines
And there must be a problem. The whole point of this is that there's a bug, right? If there's no problem then we don't need to do anything :)
Sure. It is a problem.
Some Loongson guys seem no dare to say out their CPU is buggy.
Quoted 5 lines
From a look at the GCC patch it talks about placing a sync at a branch target if it *is* in between an ll & sc [1], which I just can't reconcile with the phrase "outside of the scope of LL/SC". Is the problem when a branch target *is* in between an ll & sc, or when it *is not* between an ll & sc?
This problem happens when:
the branch insn like `beq' is between ll and sc
AND
the target of the branch insn is not between ll/sc
Quoted 5 lines
Reading this kernel patch doesn't make it any clearer - for example the sync it emits in
build_loongson3_tlb_refill_handler()is nowhere near an ll or sc instruction. Something doesn't add up here.
Loongson guys told me that, there is a branch insn between ll and sc may jump here.
In fact I don't know where is the insn.
Quoted 17 lines
Quoted 6 lines
Quoted 3 lines
How does the sync help?
Are jumps affected, or just branches?
I am not sure, so CC a Loongson people.
@Paul HuaHi Paul - any help obtaining a detailed description of these bugs would be much appreciated. Even if you only have something in Chinese I can probably get someone to help translate.
Quoted 6 lines
Quoted 1 line
Does this affect userland as well as the kernel?
There is few place can trigger these 2 bugs in kernel. In user land we have to workaround in binutils:
https://www.sourceware.org/ml/binutils/2019-01/msg00025.html
Quoted 7 lines
Quoted 1 line
In fact the kernel is the easiest since we can have a flavor build for Loongson.
My concern with regards to userland is that there's talk of a "deadlock"
- if userland can hit this & the CPU actually stalls then the system is hopelessly vulnerable to denial of service from a malicious or buggy userland program, or simply an innocent program unaware of the errata.
I have an Loongson 3A 3000 laptop.
If without any workaround, the whole system hangs very frequently. With this patch, the whole system hangs rarely.
Since the bug effects the userland, applications still hangs frequently, for example `tmux'.
In Debian, we have a list packages that can build on Cavium while cannot on Loongson 1000.
bcftools botch casacore ceres-solver chemps2 clippoly cpl-plugin-giraf cpl-plugin-xshoo dolfin freeipa git golang-1.11 graphicsmagick
igraph libminc knot-resolver nodejs
octave-ltfat prodigal pypy redis ruby2.3 ghc yade
Most of them fail due to hangs.
I tested them on Loongson 3K, some of them can build successfully now, and some of them cannot
build still.
I guess the reason is that we also need some workaround in userland, like libc etc.
Quoted 20 lines
Quoted 20 lines
Quoted 8 lines
...and probably more questions depending upon the answers to these ones.
Quoted 2 lines
Loongson doesn't plan to fix this problem in future, so we add the sync here for any condition.
So are you saying that future Loongson CPUs will all be buggy too, and someone there has said that they consider this to be OK..? I really really hope that is not true.
Bug is bug. It is not OK.
I blame these Loongson guys here.
Some Loongson guys is not so normal people.
Anyway they are a little more normal now, and anyway again, still abnormal.Quoted 5 lines
If hardware people say they're not going to fix their bugs then working around them is definitely not going to be a priority. It's one thing if a CPU designer says "oops, my bad, work around this & I'll fix it next time". It's quite another for them to say they're not interested in fixing their bugs at all.
Quoted 9 lines
Quoted 2 lines
They have interests, while I guess the true reason is that they have no enough people and money to desgin a core, while this bug is quilt hard to fix.
I'm not sure I fully understand what you're saying above, but essentially I want to know that Loongson care about fixing their CPU bugs. If they don't, and the bugs are as bad as they sound, then in my view working around them will only reinforce that producing CPUs with such serious bugs is a good idea.
Yes, you are correct. (some bad words here.
Quoted 4 lines
So if anyone from Loongson is reading, I'd really like to hear that the above is a miscommunication & that you're not intending to knowingly design any further CPUs with these bugs.
In fact I told with them lots of times face to face. The only improve of them is that finally they can say out this is a bug not features.
Quoted 5 lines
Thanks,
Paul[1] https://gcc.gnu.org/ml/gcc-patches/2018-12/msg01064.html
("Loongson3 need a sync before branch target that between ll and sc.")