Skip to content
/ linux Public

Commit f7fc253

Browse files
geomatsiSasha Levin
authored andcommitted
riscv: vector: init vector context with proper vlenb
[ Upstream commit ef3ff40 ] The vstate in thread_struct is zeroed when the vector context is initialized. That includes read-only register vlenb, which holds the vector register length in bytes. Zeroed state persists until mstatus.VS becomes 'dirty' and a context switch saves the actual hardware values. This can expose the zero vlenb value to the user-space in early debug scenarios, e.g. when ptrace attaches to a traced process early, before any vector instruction except the first one was executed. Fix this by specifying proper vlenb on vector context init. Signed-off-by: Sergey Matyukevich <geomatsi@gmail.com> Reviewed-by: Andy Chiu <andybnac@gmail.com> Tested-by: Andy Chiu <andybnac@gmail.com> Link: https://patch.msgid.link/20251214163537.1054292-3-geomatsi@gmail.com Signed-off-by: Paul Walmsley <pjw@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent d42ec56 commit f7fc253

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

arch/riscv/kernel/vector.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ static bool insn_is_vector(u32 insn_buf)
9999
return false;
100100
}
101101

102-
static int riscv_v_thread_zalloc(struct kmem_cache *cache,
103-
struct __riscv_v_ext_state *ctx)
102+
static int riscv_v_thread_ctx_alloc(struct kmem_cache *cache,
103+
struct __riscv_v_ext_state *ctx)
104104
{
105105
void *datap;
106106

@@ -110,13 +110,15 @@ static int riscv_v_thread_zalloc(struct kmem_cache *cache,
110110

111111
ctx->datap = datap;
112112
memset(ctx, 0, offsetof(struct __riscv_v_ext_state, datap));
113+
ctx->vlenb = riscv_v_vsize / 32;
114+
113115
return 0;
114116
}
115117

116118
void riscv_v_thread_alloc(struct task_struct *tsk)
117119
{
118120
#ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE
119-
riscv_v_thread_zalloc(riscv_v_kernel_cachep, &tsk->thread.kernel_vstate);
121+
riscv_v_thread_ctx_alloc(riscv_v_kernel_cachep, &tsk->thread.kernel_vstate);
120122
#endif
121123
}
122124

@@ -202,12 +204,14 @@ bool riscv_v_first_use_handler(struct pt_regs *regs)
202204
* context where VS has been off. So, try to allocate the user's V
203205
* context and resume execution.
204206
*/
205-
if (riscv_v_thread_zalloc(riscv_v_user_cachep, &current->thread.vstate)) {
207+
if (riscv_v_thread_ctx_alloc(riscv_v_user_cachep, &current->thread.vstate)) {
206208
force_sig(SIGBUS);
207209
return true;
208210
}
211+
209212
riscv_v_vstate_on(regs);
210213
riscv_v_vstate_set_restore(current, regs);
214+
211215
return true;
212216
}
213217

0 commit comments

Comments
 (0)