From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id ck1ZBX+TGGq1CiUAWB0awg (envelope-from ) for ; Thu, 28 May 2026 15:11:59 -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=AthBEfzp; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 04DC41E024; Thu, 28 May 2026 15:11:59 -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 32A4C1E024 for ; Thu, 28 May 2026 15:11:58 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D540D4BA2E1A for ; Thu, 28 May 2026 19:11:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D540D4BA2E1A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1779995510; bh=FWWqTENoczsjNFCIUqYKduqqqXe2joGR8WfyuD2JJzY=; 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=AthBEfzp0QK1fW4QILqAXhdPtgJK4DRxZjUOqff//q9RumoIs71RGIkG31EqjAHvY xeL8GKBNm+RA6SYXv7unZDNX6jdHxk64mKoPDM33gxV9RQHOyENh1uTIS3VOmEgO5r FYiGR1SCew+SvdhpJRqKVzknLrISpqVenSk6wMhU= Received: from mail-ot1-x333.google.com (mail-ot1-x333.google.com [IPv6:2607:f8b0:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id 40B654BA2E13 for ; Thu, 28 May 2026 19:11:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 40B654BA2E13 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 40B654BA2E13 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779995476; cv=none; b=P41Cxdj9OwRL8vUbgOYQP6t7ROYlTDtXaLtIJLGUHCZ59lMsW8kBXhyyhXPjNaWBpUeSzZqY8K1VIM84D/sPA8QPCdGwvIm+yQ+MWnyijlBr/MzVXPBOH1XVQ41oGF8Kh03cvaINemwKvV1Ci+4Qy8uqqsD9ntjBJVrYJt2xcqw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779995476; c=relaxed/simple; bh=km1a4e+bzbC3Vjc4aCQYZB719NJoXr5HrFGkcxTMmoY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lV1EiNjKRFFdvdSrEZWX1l6bfFmlWdRwgAlovPGSf7ibccSR2O3tI5i2nb/YspMW+CbOktE5qe897G/FctqgQMlXQjYDFTw/WFb1vrCA7VdzCE6BWQSbDmX1k6AAWeenqRMw9TTgAmDgJqKFwumLRI+21uetSZ4HVLF95dw7qpo= 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=J1pg9w9R DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 40B654BA2E13 Received: by mail-ot1-x333.google.com with SMTP id 46e09a7af769-7e615efd7d7so5047219a34.2 for ; Thu, 28 May 2026 12:11:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779995475; x=1780600275; 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=FWWqTENoczsjNFCIUqYKduqqqXe2joGR8WfyuD2JJzY=; b=GSLYdpPPTdlEbFsDCfj2McC2ECWAtpT3bfd5cHT+gFpCJ7+5xwhOIC9BJt/4eide5N fYz7OfkN8jYvOYHtnVTV+BrqCc7SKnla4p+PCHxBX2XpaXhhF8mZSpVmAM79nG8WAffL ewKLzgqK/XukRX3m0r1I+hcpbD1IWXX1+nrWpAMTP7QYv9eoVbiYbFMArppQLIgM4M2T sMXlJB9xLkij7Y4f9fGlvUkumtjDYV/fR+NjY/+pEy6bVT57HYBFSYnkMIUWwLFHBDkd VncyXsiN0qhqTgy1XisdrJf2KjwGhpif+LG1ENPH4wkjUQrnPWnBeW4j9K9Hk96fTVcd ZuHQ== X-Forwarded-Encrypted: i=1; AFNElJ8iG328x0ziHlHvCS2YwIhP59tjuufigESfhSi5L8gBNgtBgeNWD78HyB+9uSchpHvzimw=@sourceware.org X-Gm-Message-State: AOJu0Yzev21jCUVqb4VFPYAxhO5c2mANERSA+EtXkTbRzs4SvntmGtkZ BEBzEvv+IBD8WwPEeyGrrJxOtjsVqedGRl3JGuCuOU+iXNJ0OGMqDgr5b+eIkilUCZOxBWJBMUv NK2jhoAQ= X-Gm-Gg: Acq92OH/WZmPaKOtfIhFq42rblcqZjm/0hL8bCI/zDTURg86Aqe+FNmnzq7506QT5fJ t5CVMGZNahLL7L0zD4r43bg+NrisK0SbzLfI/5yPjA5KW/Jig1Ha57iR37zEIChUqHXUT0rSfyF A33WLcXj9/7+hd8PvWZbCGkWxYsgW8QBXsGihh+KA6oA8g0qfs5Q1xN9XV2jEnNDpnSg6DJ52Y0 f/0QuYjXqmMZTc8doQO6XJeWqqF5RkrGJgmR0xzjeG6AK8LHdv55WWdRay8C0z/Pfdl6ePXgB17 wlr8FOeU11ek9JG5k+6h9JXkWyUCJS69vTzOuX/16wc6FbqcEpcC0l4THu0L/b8YCPxGye1+Eoc PN25oRKQLe+LV0Ft5rHcV1u3H5HcozS0QDSf1p9o01FFA0HZQV6NW2lxWHFev+BbS8T4EKMu7ME xnjjrP4ItgqJs49R4UlUCNOKoCMlAdXgqsonP0XG87ed8YeBeb9oFTg88I0acQD40hU3g= X-Received: by 2002:a05:6830:82cc:b0:7e3:bc8c:5ea with SMTP id 46e09a7af769-7e5fefd43dbmr19175489a34.26.1779995475556; Thu, 28 May 2026 12:11:15 -0700 (PDT) Received: from lima-default.tail89d63.ts.net ([12.55.13.134]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7e606482b65sm14849267a34.10.2026.05.28.12.11.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 May 2026 12:11:15 -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, greentime.hu@sifive.com, Yong-Xuan Wang , daichengrong , Han Gao , Deepak Gupta , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [[PATCH] v4 3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state Date: Thu, 28 May 2026 14:09:14 -0500 Message-ID: <20260528190927.886558-4-tchiu@tenstorrent.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260528190927.886558-1-tchiu@tenstorrent.com> References: <20260528190927.886558-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 c699cec27c79..c469ff2bc06c 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -61,6 +61,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) { @@ -433,6 +434,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 f336a183667e..9276485b6cac 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