From nobody Wed Dec 17 15:54:35 2025 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 8A6A6220F2A for ; Mon, 13 Oct 2025 09:13:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760346814; cv=none; b=lLz6JXVRU3Kxual8YRJK7Di2DFzxB0WeucZJICLO5bcye4Oz5An+GAKs/IPBtRmyMTuf1jjiRiXIUqz8wI86O4p0hQeZxEpmFekxM7ceho1I7T6jJ9wAoDS66RdLXYspVdLCMJP8TB4kE7NkBj/Z6lYbNDjIUCwKJcm/sml9ua4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760346814; c=relaxed/simple; bh=qSWrxNVvK5O9D9jD1MXkG9Dpx7QjubRRK8pdKRIn+3Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LyQgMMZEmyYC1gc+wLBSgNsNONlg161bZv/JWqYBUjBM8ShilmLxjVfx8XzpbhbNYSEW6yC95Hnv2iO/ETqHRBtd53hKGhKWjFD8ObphR0r+P5ZpPYAw4dKnn094hhg9tGHy58DAqBWhOlmPwTfBXAklPn9tbawW35BZCBhkuKw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=DL+q8Czd; arc=none smtp.client-ip=209.85.216.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="DL+q8Czd" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-3322e6360bbso3684459a91.0 for ; Mon, 13 Oct 2025 02:13:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1760346811; x=1760951611; 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=I8Y70FtM73eem1DqR0aER86RyTdB5RacsbefhqzztUc=; b=DL+q8Czd7lxOGQ5+uJtNLL6/3RwGUC+16ttjYF5vCztgfoRp/gKLiwyuLF+PWnjutC rz5Qeetig32kPD7iIwk/oac8G4A3G7PNXneLcGyeEtFpcWmmJTgLxMAdicJtr9q7Qkre kNeXltuERW+7O6IP9d48VTWhU2nQXZsFCifASsHvxsbPDq5RoGDgg7+1FN82jhDLSRFR lUPu3M/5zg4U5CyyeO5FyBasASqx9epEgBQWtL3ESvnHFXwBpmudZEZL1u1vW2pFAKmU k2LuGnZMDnox6sGvhksaledYdItCA2l/uTAb81zhm2N0hHyl5ruUVGb9vkifQvdxkKkQ nlNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760346811; x=1760951611; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=I8Y70FtM73eem1DqR0aER86RyTdB5RacsbefhqzztUc=; b=NPCFIcxo3Ab8cSXfBViZKFyDKh7wlcfExPApWNKWyejTKqDfxM+oyfxXW/gVCuqFGI kUToy6EqpCf2XVdQ34gPVwuNyZYikxpYBWwTDCB2uaKVq5492t5mjz2Y4F4pRqReToHr 5fXjO7WrLKJKV7N1XCrVOGLj8chKrpTErioAIZ2XC44c4HAONvRrIQE9nQglmA9FKLMi YBEci+elOYuPsjF8T3u/lqH2WyaIxvl5WOAyjboPPIBaARql9VgftDVXRzUmRYDs/U+s ujFLvVvWE+4sk5Bp8sbo2I+Gl1pBHolfa/BIJ6GkFiY0sEp+VTXsqFCXy0VRnZex5/AE klDw== X-Gm-Message-State: AOJu0Yxh1CI+5J9/d4sSGTBaX3oIZRNBSm9IP+13K2Yw7Eto2OhgTwgQ 8o4zNXGaUQ/Hua1aTKWzuZg1A+XVfH2g6RphbZ157EJ/6+RS99HsENNePWhwIFom9BpqA0lm3OI WhAxsJJbw6Q5wnZJ682h9ppPLUoI8kYbS1fjmF3rfn6t+jgLLzYKXq6wv/4BB8gcPGT72lfcDLZ DEVZPhZWVUzaWRECRcknlXbJGsaeqjuLDTFK+fMvSkTW/9HJcHU4cqYAo= X-Gm-Gg: ASbGncudVrvUHo4fP++tzBqfI05RSb4N4Nqn3K+7aysRBfnR2pFdIp8g6KvR1qaSdCZ ERyxBzQB6z7Kp9LVWxG8OB2bFozvWiSS4Ih90tc6naNesARKB4p6LaVLZZrtrsPLbMKR/BBMPwI qIkvPjHFf2LIBnb0rqBGp+0Aj7b8+LTwtFoItM2RHsTNpU05I5shrHnUoXF3Hlfc2+t75iKhZMD DoDDElRfp/s+N/d22XOS8Vobs3HgYAtB5Fz+ZpydiJz1vHqxUBZXVzITXP4G5IDbgb6NTaqmeYJ oQOcEPBgVwEEQCjO/XU3pEehAuCzaNCwbChyMPx7mHjXTKdAhUfPesUAFcYo8xZjMdjBSPfUo9l wvXzFMI9m2ZMyuwi4NLJ8Vm9yhudyjzJ+akIL91l4NdCQA8jDIu4Kq9BJxmU+eo7fCp2Bad8= X-Google-Smtp-Source: AGHT+IGE6p2GX8nv0LK6w4qN/cEZpc/XaGzoZf/flFr9L0xcubk7H6sKJW7BAPJLpx7bcAcNWpCDXw== X-Received: by 2002:a17:90b:4a84:b0:32e:a10b:ce48 with SMTP id 98e67ed59e1d1-33b51114f9emr30667288a91.12.1760346810709; Mon, 13 Oct 2025 02:13:30 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-33b61a3cc0dsm11727220a91.9.2025.10.13.02.13.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Oct 2025 02:13:30 -0700 (PDT) From: Yong-Xuan Wang To: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org Cc: greentime.hu@sifive.com, vincent.chen@sifive.com, andybnac@gmail.com, Yong-Xuan Wang , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Oleg Nesterov , Charlie Jenkins , Han Gao , Jesse Taube , Conor Dooley , Joel Granados , Anna Schumaker Subject: [PATCH v2 1/2] riscv: ptrace: Optimize the allocation of vector regset Date: Mon, 13 Oct 2025 17:12:31 +0800 Message-ID: <20251013091318.467864-2-yongxuan.wang@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251013091318.467864-1-yongxuan.wang@sifive.com> References: <20251013091318.467864-1-yongxuan.wang@sifive.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 vector regset uses the maximum possible vlen value to estimate the .n field. But not all the hardwares support the maximum vlen. Linux might wastes time to prepare a large memory buffer(about 2^6 pages) for the vector regset. The regset can only copy vector registers when the process are using vector. Add .active callback and determine the n field of vector regset in riscv_v_setup_ctx_cache() doesn't affect the ptrace syscall and coredump. It can avoid oversized allocations and better matches real hardware limits. Signed-off-by: Yong-Xuan Wang Reviewed-by: Greentime Hu Reviewed-by: Andy Chiu --- arch/riscv/include/asm/vector.h | 1 + arch/riscv/kernel/ptrace.c | 24 +++++++++++++++++++++--- arch/riscv/kernel/vector.c | 2 ++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vecto= r.h index b61786d43c20..e7aa449368ad 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -51,6 +51,7 @@ void put_cpu_vector_context(void); 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); =20 static inline u32 riscv_v_flags(void) { diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index 8e86305831ea..e6272d74572f 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -153,6 +153,17 @@ static int riscv_vr_set(struct task_struct *target, 0, riscv_v_vsize); return ret; } + +static int riscv_vr_active(struct task_struct *target, const struct user_r= egset *regset) +{ + if (!(has_vector() || has_xtheadvector())) + return -ENODEV; + + if (!riscv_v_vstate_query(task_pt_regs(target))) + return 0; + + return regset->n; +} #endif =20 #ifdef CONFIG_RISCV_ISA_SUPM @@ -184,7 +195,7 @@ static int tagged_addr_ctrl_set(struct task_struct *tar= get, } #endif =20 -static const struct user_regset riscv_user_regset[] =3D { +static struct user_regset riscv_user_regset[] __ro_after_init =3D { [REGSET_X] =3D { USER_REGSET_NOTE_TYPE(PRSTATUS), .n =3D ELF_NGREG, @@ -207,11 +218,10 @@ static const struct user_regset riscv_user_regset[] = =3D { [REGSET_V] =3D { USER_REGSET_NOTE_TYPE(RISCV_VECTOR), .align =3D 16, - .n =3D ((32 * RISCV_MAX_VLENB) + - sizeof(struct __riscv_v_regset_state)) / sizeof(__u32), .size =3D sizeof(__u32), .regset_get =3D riscv_vr_get, .set =3D riscv_vr_set, + .active =3D riscv_vr_active, }, #endif #ifdef CONFIG_RISCV_ISA_SUPM @@ -233,6 +243,14 @@ static const struct user_regset_view riscv_user_native= _view =3D { .n =3D ARRAY_SIZE(riscv_user_regset), }; =20 +#ifdef CONFIG_RISCV_ISA_V +void __init update_regset_vector_info(unsigned long size) +{ + riscv_user_regset[REGSET_V].n =3D (size + sizeof(struct __riscv_v_regset_= state)) / + sizeof(__u32); +} +#endif + struct pt_regs_offset { const char *name; int offset; diff --git a/arch/riscv/kernel/vector.c b/arch/riscv/kernel/vector.c index 901e67adf576..3ed071dab9d8 100644 --- a/arch/riscv/kernel/vector.c +++ b/arch/riscv/kernel/vector.c @@ -66,6 +66,8 @@ void __init riscv_v_setup_ctx_cache(void) if (!(has_vector() || has_xtheadvector())) return; =20 + update_regset_vector_info(riscv_v_vsize); + riscv_v_user_cachep =3D kmem_cache_create_usercopy("riscv_vector_ctx", riscv_v_vsize, 16, SLAB_PANIC, 0, riscv_v_vsize, NULL); --=20 2.43.0