Skip to content

Commit ef3ff40

Browse files
geomatsiPaul Walmsley
authored andcommitted
riscv: vector: init vector context with proper vlenb
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>
1 parent 8cdb04b commit ef3ff40

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

arch/riscv/kernel/vector.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ bool insn_is_vector(u32 insn_buf)
111111
return false;
112112
}
113113

114-
static int riscv_v_thread_zalloc(struct kmem_cache *cache,
115-
struct __riscv_v_ext_state *ctx)
114+
static int riscv_v_thread_ctx_alloc(struct kmem_cache *cache,
115+
struct __riscv_v_ext_state *ctx)
116116
{
117117
void *datap;
118118

@@ -122,13 +122,15 @@ static int riscv_v_thread_zalloc(struct kmem_cache *cache,
122122

123123
ctx->datap = datap;
124124
memset(ctx, 0, offsetof(struct __riscv_v_ext_state, datap));
125+
ctx->vlenb = riscv_v_vsize / 32;
126+
125127
return 0;
126128
}
127129

128130
void riscv_v_thread_alloc(struct task_struct *tsk)
129131
{
130132
#ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE
131-
riscv_v_thread_zalloc(riscv_v_kernel_cachep, &tsk->thread.kernel_vstate);
133+
riscv_v_thread_ctx_alloc(riscv_v_kernel_cachep, &tsk->thread.kernel_vstate);
132134
#endif
133135
}
134136

@@ -214,12 +216,14 @@ bool riscv_v_first_use_handler(struct pt_regs *regs)
214216
* context where VS has been off. So, try to allocate the user's V
215217
* context and resume execution.
216218
*/
217-
if (riscv_v_thread_zalloc(riscv_v_user_cachep, &current->thread.vstate)) {
219+
if (riscv_v_thread_ctx_alloc(riscv_v_user_cachep, &current->thread.vstate)) {
218220
force_sig(SIGBUS);
219221
return true;
220222
}
223+
221224
riscv_v_vstate_on(regs);
222225
riscv_v_vstate_set_restore(current, regs);
226+
223227
return true;
224228
}
225229

0 commit comments

Comments
 (0)