linux-mips.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4] MIPS: add missing MSACSR and upper MSA initialization
@ 2020-09-01  6:53 Huang Pei
  2020-09-01  7:43 ` Huacai Chen
  2020-09-03  8:16 ` Thomas Bogendoerfer
  0 siblings, 2 replies; 4+ messages in thread
From: Huang Pei @ 2020-09-01  6:53 UTC (permalink / raw)
  To: Thomas Bogendoerfer, ambrosehua
  Cc: Li Xuefeng, Yang Tiezhu, Gao Juxin, Fuxin Zhang, Huacai Chen, linux-mips

In cc97ab235f ("MIPS: Simplify FP context initialization), init_fp_ctx
just initialize the fp/msa context, and own_fp_inatomic just restore 
FCSR and 64bit FP regs from it, but miss MSACSR and upper MSA regs for
MSA, so MSACSR and MSA upper regs's value from previous task on current
cpu can leak into current task and cause unpredictable behavior when MSA
context not initialized.

Fixes: cc97ab235f ("MIPS: Simplify FP context initialization")
Signed-off-by: Huang Pei <huangpei@loongson.cn>
---
 arch/mips/kernel/traps.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/arch/mips/kernel/traps.c b/arch/mips/kernel/traps.c
index 38aa07ccdbcc..cf788591f091 100644
--- a/arch/mips/kernel/traps.c
+++ b/arch/mips/kernel/traps.c
@@ -1287,6 +1287,18 @@ static int enable_restore_fp_context(int msa)
 		err = own_fpu_inatomic(1);
 		if (msa && !err) {
 			enable_msa();
+			/*
+			 * with MSA enabled, userspace can see MSACSR
+			 * and MSA regs, but the values in them are from
+			 * other task before current task, restore them
+			 * from saved fp/msa context
+			 */
+			write_msa_csr(current->thread.fpu.msacsr);
+			/*
+			 * own_fpu_inatomic(1) just restore low 64bit,
+			 * fix the high 64bit
+			 */
+			init_msa_upper();
 			set_thread_flag(TIF_USEDMSA);
 			set_thread_flag(TIF_MSA_CTX_LIVE);
 		}
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] MIPS: add missing MSACSR and upper MSA initialization
  2020-09-01  6:53 [PATCH v4] MIPS: add missing MSACSR and upper MSA initialization Huang Pei
@ 2020-09-01  7:43 ` Huacai Chen
  2020-09-03  8:20   ` Thomas Bogendoerfer
  2020-09-03  8:16 ` Thomas Bogendoerfer
  1 sibling, 1 reply; 4+ messages in thread
From: Huacai Chen @ 2020-09-01  7:43 UTC (permalink / raw)
  To: Huang Pei, Paul Burton
  Cc: Thomas Bogendoerfer, Paul Ambrose, Li Xuefeng, Yang Tiezhu,
	Gao Juxin, Fuxin Zhang, open list:MIPS

Hi, all,

On Tue, Sep 1, 2020 at 2:53 PM Huang Pei <huangpei@loongson.cn> wrote:
>
> In cc97ab235f ("MIPS: Simplify FP context initialization), init_fp_ctx
> just initialize the fp/msa context, and own_fp_inatomic just restore
> FCSR and 64bit FP regs from it, but miss MSACSR and upper MSA regs for
> MSA, so MSACSR and MSA upper regs's value from previous task on current
> cpu can leak into current task and cause unpredictable behavior when MSA
> context not initialized.
>
I still think this needs an ACK from Paul Burton.

Huacai

> Fixes: cc97ab235f ("MIPS: Simplify FP context initialization")
> Signed-off-by: Huang Pei <huangpei@loongson.cn>
> ---
>  arch/mips/kernel/traps.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/arch/mips/kernel/traps.c b/arch/mips/kernel/traps.c
> index 38aa07ccdbcc..cf788591f091 100644
> --- a/arch/mips/kernel/traps.c
> +++ b/arch/mips/kernel/traps.c
> @@ -1287,6 +1287,18 @@ static int enable_restore_fp_context(int msa)
>                 err = own_fpu_inatomic(1);
>                 if (msa && !err) {
>                         enable_msa();
> +                       /*
> +                        * with MSA enabled, userspace can see MSACSR
> +                        * and MSA regs, but the values in them are from
> +                        * other task before current task, restore them
> +                        * from saved fp/msa context
> +                        */
> +                       write_msa_csr(current->thread.fpu.msacsr);
> +                       /*
> +                        * own_fpu_inatomic(1) just restore low 64bit,
> +                        * fix the high 64bit
> +                        */
> +                       init_msa_upper();
>                         set_thread_flag(TIF_USEDMSA);
>                         set_thread_flag(TIF_MSA_CTX_LIVE);
>                 }
> --
> 2.17.1
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] MIPS: add missing MSACSR and upper MSA initialization
  2020-09-01  6:53 [PATCH v4] MIPS: add missing MSACSR and upper MSA initialization Huang Pei
  2020-09-01  7:43 ` Huacai Chen
@ 2020-09-03  8:16 ` Thomas Bogendoerfer
  1 sibling, 0 replies; 4+ messages in thread
From: Thomas Bogendoerfer @ 2020-09-03  8:16 UTC (permalink / raw)
  To: Huang Pei
  Cc: ambrosehua, Li Xuefeng, Yang Tiezhu, Gao Juxin, Fuxin Zhang,
	Huacai Chen, linux-mips

On Tue, Sep 01, 2020 at 02:53:09PM +0800, Huang Pei wrote:
> In cc97ab235f ("MIPS: Simplify FP context initialization), init_fp_ctx
> just initialize the fp/msa context, and own_fp_inatomic just restore 
> FCSR and 64bit FP regs from it, but miss MSACSR and upper MSA regs for
> MSA, so MSACSR and MSA upper regs's value from previous task on current
> cpu can leak into current task and cause unpredictable behavior when MSA
> context not initialized.
> 
> Fixes: cc97ab235f ("MIPS: Simplify FP context initialization")
> Signed-off-by: Huang Pei <huangpei@loongson.cn>
> ---
>  arch/mips/kernel/traps.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)

applied to mips-fixes.

Thomas.

-- 
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] MIPS: add missing MSACSR and upper MSA initialization
  2020-09-01  7:43 ` Huacai Chen
@ 2020-09-03  8:20   ` Thomas Bogendoerfer
  0 siblings, 0 replies; 4+ messages in thread
From: Thomas Bogendoerfer @ 2020-09-03  8:20 UTC (permalink / raw)
  To: Huacai Chen
  Cc: Huang Pei, Paul Burton, Paul Ambrose, Li Xuefeng, Yang Tiezhu,
	Gao Juxin, Fuxin Zhang, open list:MIPS

On Tue, Sep 01, 2020 at 03:43:15PM +0800, Huacai Chen wrote:
> Hi, all,
> 
> On Tue, Sep 1, 2020 at 2:53 PM Huang Pei <huangpei@loongson.cn> wrote:
> >
> > In cc97ab235f ("MIPS: Simplify FP context initialization), init_fp_ctx
> > just initialize the fp/msa context, and own_fp_inatomic just restore
> > FCSR and 64bit FP regs from it, but miss MSACSR and upper MSA regs for
> > MSA, so MSACSR and MSA upper regs's value from previous task on current
> > cpu can leak into current task and cause unpredictable behavior when MSA
> > context not initialized.
> >
> I still think this needs an ACK from Paul Burton.

I'm also curious why Paul removed the init_msa_upper() call in that patch,
but I don't need an explicit ACK from him. I've checked how FPU/MSA
context is setup and to me this patch does the correct thing. IMHO it doesn't
do any harm. So if we find out they are for whatever reason redudant it's
quite easy to revert this patch.

Thomas.

-- 
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-09-03  8:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-01  6:53 [PATCH v4] MIPS: add missing MSACSR and upper MSA initialization Huang Pei
2020-09-01  7:43 ` Huacai Chen
2020-09-03  8:20   ` Thomas Bogendoerfer
2020-09-03  8:16 ` Thomas Bogendoerfer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).