From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id Pf/6L0wyD2o69AwAWB0awg (envelope-from ) for ; Thu, 21 May 2026 12:26:52 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=nMa4gdMK; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id A10121E098; Thu, 21 May 2026 12:26:52 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 51C9C1E024 for ; Thu, 21 May 2026 12:26:51 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B95BB4BB24C7 for ; Thu, 21 May 2026 16:26:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B95BB4BB24C7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1779380810; bh=QEAfGpBgN2oAxRBUlYfffsCUgFwLcUi+MFkDxCrvkE8=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=nMa4gdMKic8hHqt1HSqWVo5PWsbQMvuU3FpvQEUWzTeic++mnnnvzk9d8GxtmsTHp zBgtSJjemTcHZeSX58YoP9+n1yTsQhMEM97jHIrR+WImfqj3eTGrZnthXsKe/jU+eE ZE3U7vkqPZqi1NrUePWcw9cjnS+AxaXLQY+CLku8= Received: from mail-yw1-x1129.google.com (mail-yw1-x1129.google.com [IPv6:2607:f8b0:4864:20::1129]) by sourceware.org (Postfix) with ESMTPS id D3FD64BAD16D for ; Thu, 21 May 2026 16:26:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D3FD64BAD16D ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D3FD64BAD16D ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779380774; cv=none; b=nzsswA4miI81zutV6mt2AXmKVbxAiuZTMp+T6sADQCkgJJQ5bMvxioDJWXsDRYRGCCEaztJp+2PJizi4SHKN2N61UguVv8RDBmdOyJinFrCLEqx9CCrH9fqgpi7J1eWQsPGpdH4NVCBrm/nsVTozQYy8c9rsYXMUf+FNktjtrJQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779380774; c=relaxed/simple; bh=7bY5klGbHslVJRO9cyrIgQaB3LxMWkl14jlH7faShB0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=YIlTxHe7zAN36kWqXXniz4nX80JTB/uEuoRqbLimpu54COHbCNxtFIWlTMY/u+vpFZG/ZV82b63OljWWjq54Tx8CW/2KT+cxNtDVVydXHXLv4u7kzIdfL6qv3jfftrm8zQsTXOlNqS7QEBbEhnBLH+I/mHQ82mKtzZ8JbGMzjw4= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=tenstorrent.com header.i=@tenstorrent.com header.a=rsa-sha256 header.s=google header.b=EAHBIp/a DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D3FD64BAD16D Received: by mail-yw1-x1129.google.com with SMTP id 00721157ae682-7bdaa68cf81so58397267b3.3 for ; Thu, 21 May 2026 09:26:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779380773; x=1779985573; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=QEAfGpBgN2oAxRBUlYfffsCUgFwLcUi+MFkDxCrvkE8=; b=YQ3JadcFAEjGu0BRjHVmXOgrchXa4v+8qupH3nVqFGUG6fRtZkpmNLB3cBcqvKDXGk fMkuIh7OD6thOyOGQichR0QmxpH0wTJBPArbjXdsJVW9RfouC2Hg1zcI3bwjo8UlMMEp gOg1qM8wAox22vPT3J7eYesvb+XWaFlAaI4lEvQ66cHOvBcGmY5iEB0eHeGGtzXNFqUM PptwUqeOQht77Y27h3tmH02aNKgScweBMw/ipmPnnF4o7Z7kX0l7UzIBlwg1R2Ihg/fG L9HcBFiySKZGQSI9ZngzBVIDcUPx6KYUwrU2+G1N3rMvD9yvOGPweYz2EV/nqVR9ESsX C96Q== X-Forwarded-Encrypted: i=1; AFNElJ/xYByj/MYUkGKLm6ctCjdhVAkYRZX31MlZPp7yMr5yD170dxhD025Po2YxbTKcP9tj3RE=@sourceware.org X-Gm-Message-State: AOJu0YzJD/bOChzKuJ8eYG/PK0+UTFbUoflnvd6pPaWpRLrBFiaeTImy vQkb4/kr9TGZxf8VTwAXEtzIav43Hj2I4hvbYmJ0us+KWXu0tHCqZHtnn7LkVYK9eaY= X-Gm-Gg: Acq92OEyvNIrz0jR7kNrpH3Ie3KI60A+Gj3af4UMCYQiqvUI4P2mGl3TYOmikbI8h/L WmyuXyLUQAFULGemZBWr30JiN2Ih+oYnRXeo7OiCbLRMCDRA5fK7GjeEoe7gyCaBIEiv6vsDYmA 4VPXgWPapO8TEfarVX2RWz7E6h1LBEdKtOfZuizKLcx1bq1zIbqBuObAwB+xqskaOLAIZuZ5oEh fbmSTxu8GayF/dHnefD7TyJ6rwj3EDmKczVEWMCcwCLBv4Z4wvR6e8fzSE6YC5vY0+RxXbKBtI+ WPzpOxuL2eKdxeILluaN3RyCQ+e/V1dRjxkzM4xZhJNRxey7+NWcPln10Jd4od9eVRoAIEHNTkX Pqo4bSL3+PzzirqSmyPsbW4ue3z2E3pwbBQds9s6I80Gk3ET8IbH3qOeZLNDtMKjAtdAvf9LPy/ ZB1BJ5vkmmyVimqBTMz0D6LKlWvDwr4nTZKyYcy8+zAj8dBGqZ7g/NKdoH X-Received: by 2002:a05:690c:9685:b0:79a:b9cc:2100 with SMTP id 00721157ae682-7d20c54fb28mr40035927b3.35.1779380773137; Thu, 21 May 2026 09:26:13 -0700 (PDT) Received: from lima-default.tail89d63.ts.net ([12.55.13.134]) by smtp.gmail.com with ESMTPSA id 00721157ae682-7d2ca76a8cbsm4822007b3.32.2026.05.21.09.26.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 May 2026 09:26:12 -0700 (PDT) To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Oleg Nesterov , linux-riscv@lists.infradead.org Cc: Andy Chiu , Sergey Matyukevich , gdb@sourceware.org, dfustini@oss.tenstorrent.com, bjorn@kernel.org, greentime.hu@sifive.com, vincent.chen@sifive.com Subject: [PATCH v3 3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state Date: Thu, 21 May 2026 11:25:19 -0500 Message-ID: <20260521162521.188629-4-tchiu@tenstorrent.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260521162521.188629-1-tchiu@tenstorrent.com> References: <20260521162521.188629-1-tchiu@tenstorrent.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Andy Chiu via Gdb Reply-To: Andy Chiu Errors-To: gdb-bounces~public-inbox=simark.ca@sourceware.org Sender: "Gdb" The last patch introduced the INITIAL vector state to avoid saving and restoring vector registers across syscall boundaries. However, this optimization did not fully account for the ptrace and signal handling interfaces. As a result, two issues emerged: 1. Ptrace reads at syscall stop could observe stale, non-nulled registers. 2. Modifications to the ucontext through signal interface during a syscall stop would be overwritten by the vector discaring macro. This patch introduces riscv_v_ucontext_save() to synchronize these paths with the INITIAL state: - Ptrace reads during a syscall stop now explicitly execute the hardware discard macro and return the discarded state to prevent data leaks. - Ptrace writes (PTRACE_SETREGSET) during a syscall stop are silently dropped (returning 0). Returning an error like EINVAL would break debbugers like GDB, which disables the optional regset on receiving such error. - Signal handling (rt_sigreturn) now honor user-space modifications to the vector context (for user-space thread schedulers). CC: Sergey Matyukevich CC: gdb@sourceware.org Signed-off-by: Andy Chiu --- Changelog v3: - new patch since v3 --- arch/riscv/include/asm/vector.h | 2 ++ arch/riscv/kernel/ptrace.c | 13 ++++++------ arch/riscv/kernel/signal.c | 11 ++++++---- arch/riscv/kernel/vector.c | 37 +++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index 769f30779867..8c1e64e0dd0b 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -52,6 +52,7 @@ void riscv_v_thread_free(struct task_struct *tsk); void __init riscv_v_setup_ctx_cache(void); void riscv_v_thread_alloc(struct task_struct *tsk); void __init update_regset_vector_info(unsigned long size); +void riscv_v_ucontext_save(struct task_struct *tsk); static inline u32 riscv_v_flags(void) { @@ -427,6 +428,7 @@ static inline bool riscv_v_vstate_ctrl_user_allowed(void) { return false; } #define riscv_v_thread_alloc(tsk) do {} while (0) #define get_cpu_vector_context() do {} while (0) #define put_cpu_vector_context() do {} while (0) +#define riscv_v_ucontext_save(tsk) do {} while (0) #define riscv_v_vstate_set_restore(task, regs) do {} while (0) #endif /* CONFIG_RISCV_ISA_V */ diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index 93de2e7a3074..554fd5a4747d 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -109,11 +109,7 @@ static int riscv_vr_get(struct task_struct *target, * Ensure the vector registers have been saved to the memory before * copying them to membuf. */ - if (target == current) { - get_cpu_vector_context(); - riscv_v_vstate_save(¤t->thread.vstate, task_pt_regs(current)); - put_cpu_vector_context(); - } + riscv_v_ucontext_save(target); ptrace_vstate.vstart = vstate->vstart; ptrace_vstate.vl = vstate->vl; @@ -222,13 +218,18 @@ static int riscv_vr_set(struct task_struct *target, int ret; struct __riscv_v_ext_state *vstate = &target->thread.vstate; struct __riscv_v_regset_state ptrace_vstate; + struct pt_regs *regs = task_pt_regs(target); if (!(has_vector() || has_xtheadvector())) return -EINVAL; - if (!riscv_v_vstate_query(task_pt_regs(target))) + if (!riscv_v_vstate_query(regs)) return -ENODATA; + /* Silently drop the modification to tracee as no vreg lives across a syscall */ + if (__riscv_v_vstate_check(regs->status, INITIAL)) + return 0; + /* Copy rest of the vstate except datap */ ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ptrace_vstate, 0, sizeof(struct __riscv_v_regset_state)); diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 59784dc117e4..0da352310b84 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -89,9 +89,7 @@ static long save_v_state(struct pt_regs *regs, void __user *sc_vec) /* datap is designed to be 16 byte aligned for better performance */ WARN_ON(!IS_ALIGNED((unsigned long)datap, 16)); - get_cpu_vector_context(); - riscv_v_vstate_save(¤t->thread.vstate, regs); - put_cpu_vector_context(); + riscv_v_ucontext_save(current); /* Copy everything of vstate but datap. */ err = __copy_to_user(&state->v_state, ¤t->thread.vstate, @@ -121,9 +119,14 @@ static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec) /* * Mark the vstate as clean prior performing the actual copy, * to avoid getting the vstate incorrectly clobbered by the - * discarded vector state. + * discarded vector state. + * + * This also allows user to modify vregs through the signal + * interface at a syscall stop. e.g. to support user space + * context switching. */ riscv_v_vstate_set_restore(current, regs); + __riscv_v_vstate_clean(regs); /* Copy everything of __sc_riscv_v_state except datap. */ err = __copy_from_user(¤t->thread.vstate, &state->v_state, diff --git a/arch/riscv/kernel/vector.c b/arch/riscv/kernel/vector.c index 4eef51f6d432..6fd541f5d5cb 100644 --- a/arch/riscv/kernel/vector.c +++ b/arch/riscv/kernel/vector.c @@ -29,6 +29,43 @@ static struct kmem_cache *riscv_v_kernel_cachep; unsigned long riscv_v_vsize __read_mostly; EXPORT_SYMBOL_GPL(riscv_v_vsize); +/* + * Context memory is not coherent to register when sstatus.vs is set to INITIAL. This function + * take the INITIAL state into consideration and reflect the nulled state into context memory. + * Assume the target task is not actively running when tsk != current + */ +void riscv_v_ucontext_save(struct task_struct *tsk) +{ + struct __riscv_v_ext_state *vstate = &tsk->thread.vstate; + struct pt_regs *regs = task_pt_regs(tsk); + + /* + * Do not set vstate as clean when it is INITIAL, otherwise we lose track of the nulled + * state in ptrace. + */ + if (tsk == current) { + get_cpu_vector_context(); + if (__riscv_v_vstate_check(regs->status, INITIAL)) { + riscv_v_enable(); + __riscv_v_vstate_discard(); + __riscv_v_vstate_save(vstate, vstate->datap); + riscv_v_disable(); + } else { + riscv_v_vstate_save(vstate, regs); + } + put_cpu_vector_context(); + } else if (__riscv_v_vstate_check(regs->status, INITIAL)) { + /* + * If we are not current and VS == INITIAL, null out the context memory for tsk + * using kernel mode vector. + */ + kernel_vector_begin(); + __riscv_v_vstate_discard(); + __riscv_v_vstate_save(vstate, vstate->datap); + kernel_vector_end(); + } +} + int riscv_v_setup_vsize(void) { unsigned long this_vsize; -- 2.43.0