From nobody Tue Dec 16 14:50:02 2025 Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 567D32DE70D for ; Sun, 14 Dec 2025 16:35:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765730152; cv=none; b=Y1yHVvXqCOlg0qyOIrz2SGi2KUM1CnvBJ+oo/G+UbixjmPlNJYmDJKx+mOXBkNsPSCEhKrrzBUPaRD910kQSHSCPLFQfSwEudoqj9p4pxIngr1yOtm0WsmzV47j96IfJ1a1yCDnjzL0KlUoQcBmHZrl+9u2QTNkznVG6oVH30m8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765730152; c=relaxed/simple; bh=u4GRx7isGS6qqcvrlTr4xHwjuxkIkW+2PvgAhpYYI+U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dPtnPkMJhw7GdKG0zA/THmmUMermttdj6VkAcX/hBWijNxhN2Hv7S1zeq8MCGPd9qCcttOUYL8hKtwYobPRUvkvx+facbxCf1IeuWPo0mH8CWWYroNABm/xTNLWQHfKEUBR5k0FKjQbixAWsNvDW+1Qq1Kng+tfdbmqJ5ukS7yQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Y3DSRZMi; arc=none smtp.client-ip=209.85.167.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Y3DSRZMi" Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-597de27b241so3377415e87.2 for ; Sun, 14 Dec 2025 08:35:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765730148; x=1766334948; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dLn4MNRkDT1IUgV60n0qo0HeUjZmH78SLEbhEVR7tyI=; b=Y3DSRZMi9kujvscyef9hLqAd1IORKsLVIw7VXQcYdtdOPJiRJjKy5T4s4ec6J6UUv7 iA2UdsEC2hBIkpWAESLSGn/0i7TkSP0vjvKhXiKJkGDzUF2raBiLVJt/NSu0dxxWbR9k ocH843SYesEHeyp8rXXrvgj1uSS3eBdva5Oz+n/McPn+mQR7Bd0ipjfURvA8saIgke8Y I9OhUjc32kKCT3gcZGEIUx1V0o5hiKtTCrofj/n6oGUZFvjBUZ+KITqYTBtC7zCStINQ 1NwFtQcG49S0/hLyOFPuwlAE8g+mkOw1tsOE3yfCOiIsP+qCQnHW9zp5UGnaHiDaxajG JwpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765730148; x=1766334948; 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=dLn4MNRkDT1IUgV60n0qo0HeUjZmH78SLEbhEVR7tyI=; b=hl0vgPtTp+9ecV9TJeS19Pzsxh5Z7FM7QIn14wb+ZwLU1u/pCP07yyAk922kRDL07f OLX+mwUv/WEGEtQr0hAFiQ79pdPpkTV085nsvBL5eaFX1RTeonk8xVfdpFNUTBSDvyiD J8pocQSJOXLrBi/POone1aqcEvZuBflBtpYRxnuyA0VX2qMy4ERcGBg640khp/zHVNOb V/9P8HKyQeZiaSHPeUI0fzVEZ2hbP+OoJIcpeyf9w8EGINnshRjBNFL9740AelD4bQyc I2+efLvFWIAKLm4FdJ4BpIHZlZviFCn3eY8Qit/RTUeUiYTi7wl6dXlNdanLgFJa8+6z SdZw== X-Forwarded-Encrypted: i=1; AJvYcCW1hIgGB6WLLCh1LL+9ncbYuy2KVFLc50pHeJwWtPhm4KHCCtvKtb3irMrlMeQWdTVlGeeHomVinndjjZY=@vger.kernel.org X-Gm-Message-State: AOJu0YytLS1fbPa4l2MpDENB+wSH+MxWIqFkh+vLKPWz9wKx7Vp1IzYn csIuYZ3y8YXk9K5hIyyXV5ne0y9WxkXnfMS40iBaxc/Pp2qy6h+T5TlB X-Gm-Gg: AY/fxX4qiDyo0cZeCaRejbLbi+3qgmDXB4hk6rMkkjXmBt6qKUsl3csrd7tKG+WUm6J mCkG2lDZ1MQrxpBAATFUXrRgKw//mcixafqpc8P7QrY4UmP58SNdt9WxINerZ4yd+et+mIeVm9S JgROBnGDfyvOwzSXcQQHLlZQfHSFdT5uCeH6lRe8WDSKlHB6TxcmnOx5qhvcpyPl40a2/SmDnNz ZjU8yjWBeCBvv0+bICmdkjNTxZyUFgQ2ADStnD3Q2475lxI/HTfSUkaXRYbgvH6JReUNCPIE24X L9eLXvUjYF/9G4607EG4zI1nsKJO2iaHaTIAgIcpcU3ui4GwUpH8+V0rZTl/yVzZpCMcNMuf0ye 6BG785c8KaFZn04iKzpSd8WQWtjcrBiW1dRXiehPKNNaKogrU735S2NPvztRPuC9BOmjfxtvsGU Wx4kjzSxsi2jr6 X-Google-Smtp-Source: AGHT+IFh1nTDiB6bfb+d32bUKdxI+dMGC8og7D8jELF0KQuXTZE0v1XUdZ2F7715E8gk3xNrCq+2AA== X-Received: by 2002:ac2:4c47:0:b0:595:7f1c:29bb with SMTP id 2adb3069b0e04-598faa36a70mr2545740e87.22.1765730148316; Sun, 14 Dec 2025 08:35:48 -0800 (PST) Received: from curiosity ([5.188.167.4]) by smtp.googlemail.com with ESMTPSA id 2adb3069b0e04-598f7717b79sm3789618e87.60.2025.12.14.08.35.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Dec 2025 08:35:47 -0800 (PST) From: Sergey Matyukevich To: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , Oleg Nesterov , Shuah Khan , Thomas Huth , Charlie Jenkins , Andy Chiu , Samuel Holland , Joel Granados , Conor Dooley , Yong-Xuan Wang , Heiko Stuebner , Guo Ren , Sergey Matyukevich Subject: [PATCH v5 2/9] riscv: vector: init vector context with proper vlenb Date: Sun, 14 Dec 2025 19:35:06 +0300 Message-ID: <20251214163537.1054292-3-geomatsi@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251214163537.1054292-1-geomatsi@gmail.com> References: <20251214163537.1054292-1-geomatsi@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- arch/riscv/kernel/vector.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/arch/riscv/kernel/vector.c b/arch/riscv/kernel/vector.c index 3ed071dab9d8..b112166d51e9 100644 --- a/arch/riscv/kernel/vector.c +++ b/arch/riscv/kernel/vector.c @@ -111,8 +111,8 @@ bool insn_is_vector(u32 insn_buf) return false; } =20 -static int riscv_v_thread_zalloc(struct kmem_cache *cache, - struct __riscv_v_ext_state *ctx) +static int riscv_v_thread_ctx_alloc(struct kmem_cache *cache, + struct __riscv_v_ext_state *ctx) { void *datap; =20 @@ -122,13 +122,15 @@ static int riscv_v_thread_zalloc(struct kmem_cache *c= ache, =20 ctx->datap =3D datap; memset(ctx, 0, offsetof(struct __riscv_v_ext_state, datap)); + ctx->vlenb =3D riscv_v_vsize / 32; + return 0; } =20 void riscv_v_thread_alloc(struct task_struct *tsk) { #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE - riscv_v_thread_zalloc(riscv_v_kernel_cachep, &tsk->thread.kernel_vstate); + riscv_v_thread_ctx_alloc(riscv_v_kernel_cachep, &tsk->thread.kernel_vstat= e); #endif } =20 @@ -214,12 +216,14 @@ bool riscv_v_first_use_handler(struct pt_regs *regs) * context where VS has been off. So, try to allocate the user's V * context and resume execution. */ - if (riscv_v_thread_zalloc(riscv_v_user_cachep, ¤t->thread.vstate)) { + if (riscv_v_thread_ctx_alloc(riscv_v_user_cachep, ¤t->thread.vstate= )) { force_sig(SIGBUS); return true; } + riscv_v_vstate_on(regs); riscv_v_vstate_set_restore(current, regs); + return true; } =20 --=20 2.52.0