Received: by 2002:ab2:6f44:0:b0:1fd:c486:4f03 with SMTP id l4csp220723lqq; Thu, 13 Jun 2024 00:35:01 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX/VrrC0fc4gi63d9JARWhKr0htx9+gWhLK5HJA7duip3z/h0cwe/nikKbQx6n9CGoGZsvAdQrCdbP6fFJaXvkQ/q5Or3aCj+kr0bom8Q== X-Google-Smtp-Source: AGHT+IFhM+BhneUj09fSm+6EZxkafaNVNMxPfGzSFDtldKJnWkBGM+EOFmcTxOLN/hEpFIcVi3F9 X-Received: by 2002:a05:6808:6544:b0:3d2:1cca:b7c3 with SMTP id 5614622812f47-3d23e032a12mr4760515b6e.27.1718264101045; Thu, 13 Jun 2024 00:35:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718264101; cv=pass; d=google.com; s=arc-20160816; b=wkw02DugxQJ+bSgKDTfjWH+aRlijP9j+zoI8/zmxPTPPNbzrGjGhDPA9QszjmPg7ZR bwB+fiChmz83E4vSQjlV1UeRjQZlLa1SrLEgEigkCxsMCv+37s+T1GpcSHLjGcK6Hg5U eep9kF81Zq0fc8ixL/VN8OlI1d5FFz8ohR2+gKn/3ekXfRAlCViGXaAfUUgO+jZXKWxB BBI8OWYvIAuSJQjszLILWdPq8Zs1rxW8HjcT09gOKNBnR7Yr0DpCnQ9L3xnJxkSeqLP1 H2uwtHBxfOnwINc+CMM/vr1r4TnoHlHo/Zv3CZPbM6OtCB9Qd5/Oa9TBC8fKk8Z+/73e y6wA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=TeZhGtu+DoZzH37TzHgHW9NZu5mW4WfgIzkfv6QNhDQ=; fh=PRkXJ+ytpiJClb4JHpgLpH0ObB3LDBGpdU+SVFEkQww=; b=xSgoRE9Ie+xKFkYnfqfHjlx1SlRONtU+auxddlAHrRsO2Mrh2cRKLmciQiPytAWKfN VePIZHyWwZcYzl/8Ba1E61kLjOu0Qqm1/UYdLG/c3JrzmjgaTjknMEJae3fEfu2Fd38B hcCF7DXTeHws4x9m6qJm8OrmCJ0PKfrM0kTENWInGl658+mohXHwu/qixvhyrttUS13n +eKEKDX+XZ6xsPAdYyPSPPW+wvfNqsH25aj8Fx/QlUGadRX3v/1/4T+x+pi3fHAalP3u QCjj9mPkZukNboTVmZRQnYGauKFLRME/v+RSE27RIT0Sf83bXDHqYiVwCBg8rJ1oS+Ft F0+Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=iRvRIYIO; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-212717-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212717-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d2e1a72fcca58-705cc8eafaesi917184b3a.16.2024.06.13.00.35.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 00:35:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-212717-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=iRvRIYIO; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-212717-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212717-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 16D9C289FC7 for ; Thu, 13 Jun 2024 07:34:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 64D3A13C3F0; Thu, 13 Jun 2024 07:33:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="iRvRIYIO" Received: from mail-oi1-f171.google.com (mail-oi1-f171.google.com [209.85.167.171]) (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 1047713D50A for ; Thu, 13 Jun 2024 07:33:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718263990; cv=none; b=U2Bdeb8Q7/vP6H+MyTiI5kRk7BQVfz4PPh2gXYSDASzk2USgaLP9ZPCXzuUngDpBb1nt8Ct0KNoO3m2yY8aTxmNs8WhTs2cswytgDqrO1LkoeESBwXxcqwYzHngNXGUmBbLoWS0WM/sLbe6Wn28uhqQh7Ntr1YqFSEEyNZHD3yM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718263990; c=relaxed/simple; bh=Qip5cL32AGGgHJ2xW9xkkh9vglc7nxfsBldmU/OW3wg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pdNewMoUEkoNGxBJRcqmNzPwdrXmlSIb1dZ+GgI5gGFB5PRTzH0oXs0S7IqUGVCjAHW0Oj21Ic+BVqCE9knWLrE8d7Lx5M0Vq21VNLR78a8dmdIMHjsHfmC/DkCTzomOV/9qiwFf3ybcfgnPTv8VOL8qd78r1NU8DPSmCKqYSkc= 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=iRvRIYIO; arc=none smtp.client-ip=209.85.167.171 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 Received: by mail-oi1-f171.google.com with SMTP id 5614622812f47-3d24724514eso228780b6e.0 for ; Thu, 13 Jun 2024 00:33:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1718263988; x=1718868788; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=TeZhGtu+DoZzH37TzHgHW9NZu5mW4WfgIzkfv6QNhDQ=; b=iRvRIYIOrrNcIUvUYdmLBf3vNeKbUpWLvESA1dCHUUXQt8nVtTaI7UemCGutCRFY8K G9xrTTsoptnQtxLK1WhcmlSMmGE37YVSrGVzz1cyl+5qX5kT7FmtZ8uCbKEgKS/gbxzc 6OE76uclx0Yv2wLQmV4MzDKVBgvq8zCDvuvoyF+vU6VMHM1VmgK5fXjzCU4lnyX5wzMu BhnhthKk1SwoYuk5IrS7XIa0t1wrV7cofVvXD11xhovd+bKyfKss2h5ICWt7dfkYjEfQ ss1GUWcA5KpvZqibYp9FjCxHFMOjtVxr4nUZh4hnqX4E5keSo81mv4VgypAjnuMKhhMi 191A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718263988; x=1718868788; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TeZhGtu+DoZzH37TzHgHW9NZu5mW4WfgIzkfv6QNhDQ=; b=ZwcVnuHknL8lOIDgSLRfF7xcvDjCd90zlCKTUG3iX8YofW3tkB5kFr9qzAIbD05lhZ rjwsgs5AMz/fEus9in96LYhGoeCGhyUemiVwEB3aTk0NyTfXrm3aCuunJK84a2Kj4HZB dN+W0LttgjXynPE41JBDM5jm75IDFulqxMD6zO+lTJX1BtJXmslKDet2yj47bt/5OhE/ LX6PRpbR1Q2phw2w/ckr2kPPMTGaCYybAbPxvRonsLUq+OazTwGoKTRPQ9OhnDKSNu0j GUz0/ST1FWFdD4LgXjgtdMRmN/eJsscXcjkH4YXPIIO/hN08q0VH90SR9AQXn0OlOAj2 2Wzg== X-Forwarded-Encrypted: i=1; AJvYcCUJHFMEDeEkBbAB6tAUqW3+Q37EnGaOiPPy1dMeUnFjfU+1xle4p1UMoawpcvkeYutxleYUhjCxXE1yUYA5R+ysXz+nyWyW423LNptx X-Gm-Message-State: AOJu0Yy2zwlSZIr74WMsj/52hGCbIfnNazM2gOR/eJsC0qwYhqy8rdJF vL3qm81M/OPZ4ddgeP7yczUbM4K3k7HFFQcxDFgTOY+bicSj3IBaqJ8xwhKMIFc= X-Received: by 2002:a05:6808:2199:b0:3d2:1c8f:be0c with SMTP id 5614622812f47-3d23e045f11mr5371608b6e.30.1718263988155; Thu, 13 Jun 2024 00:33:08 -0700 (PDT) Received: from [127.0.1.1] (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-705cc78a1a0sm730028b3a.0.2024.06.13.00.33.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 00:33:07 -0700 (PDT) From: Andy Chiu Date: Thu, 13 Jun 2024 15:11:12 +0800 Subject: [PATCH 7/8] riscv: vector: Support calling schedule() for preemptible Vector Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240613-dev-andyc-dyn-ftrace-v4-v1-7-1a538e12c01e@sifive.com> References: <20240613-dev-andyc-dyn-ftrace-v4-v1-0-1a538e12c01e@sifive.com> In-Reply-To: <20240613-dev-andyc-dyn-ftrace-v4-v1-0-1a538e12c01e@sifive.com> To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Zong Li , Steven Rostedt , Masami Hiramatsu , Mark Rutland , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Puranjay Mohan Cc: Palmer Dabbelt , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, llvm@lists.linux.dev, Andy Chiu X-Mailer: b4 0.12.4 Each function entry implies a call to ftrace infrastructure. And it may call into schedule in some cases. So, it is possible for preemptible kernel-mode Vector to implicitly call into schedule. Since all V-regs are caller-saved, it is possible to drop all V context when a thread voluntarily call schedule(). Besides, we currently don't pass argument through vector register, so we don't have to save/restore V-regs in ftrace trampoline. Signed-off-by: Andy Chiu --- arch/riscv/include/asm/processor.h | 5 +++++ arch/riscv/include/asm/vector.h | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 68c3432dc6ea..02598e168659 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -95,6 +95,10 @@ struct pt_regs; * Thus, the task does not own preempt_v. Any use of Vector will have to * save preempt_v, if dirty, and fallback to non-preemptible kernel-mode * Vector. + * - bit 29: The thread voluntarily calls schedule() while holding an active + * preempt_v. All preempt_v context should be dropped in such case because + * V-regs are caller-saved. Only sstatus.VS=ON is persisted across a + * schedule() call. * - bit 30: The in-kernel preempt_v context is saved, and requries to be * restored when returning to the context that owns the preempt_v. * - bit 31: The in-kernel preempt_v context is dirty, as signaled by the @@ -109,6 +113,7 @@ struct pt_regs; #define RISCV_PREEMPT_V 0x00000100 #define RISCV_PREEMPT_V_DIRTY 0x80000000 #define RISCV_PREEMPT_V_NEED_RESTORE 0x40000000 +#define RISCV_PREEMPT_V_IN_SCHEDULE 0x20000000 /* CPU-specific state of a task */ struct thread_struct { diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index 731dcd0ed4de..50693cffbe78 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -75,6 +75,11 @@ static __always_inline void riscv_v_disable(void) csr_clear(CSR_SSTATUS, SR_VS); } +static __always_inline bool riscv_v_is_on(void) +{ + return !!(csr_read(CSR_SSTATUS) & SR_VS); +} + static __always_inline void __vstate_csr_save(struct __riscv_v_ext_state *dest) { asm volatile ( @@ -243,6 +248,11 @@ static inline void __switch_to_vector(struct task_struct *prev, struct pt_regs *regs; if (riscv_preempt_v_started(prev)) { + if (riscv_v_is_on()) { + WARN_ON(prev->thread.riscv_v_flags & RISCV_V_CTX_DEPTH_MASK); + riscv_v_disable(); + prev->thread.riscv_v_flags |= RISCV_PREEMPT_V_IN_SCHEDULE; + } if (riscv_preempt_v_dirty(prev)) { __riscv_v_vstate_save(&prev->thread.kernel_vstate, prev->thread.kernel_vstate.datap); @@ -253,10 +263,16 @@ static inline void __switch_to_vector(struct task_struct *prev, riscv_v_vstate_save(&prev->thread.vstate, regs); } - if (riscv_preempt_v_started(next)) - riscv_preempt_v_set_restore(next); - else + if (riscv_preempt_v_started(next)) { + if (next->thread.riscv_v_flags & RISCV_PREEMPT_V_IN_SCHEDULE) { + next->thread.riscv_v_flags &= ~RISCV_PREEMPT_V_IN_SCHEDULE; + riscv_v_enable(); + } else { + riscv_preempt_v_set_restore(next); + } + } else { riscv_v_vstate_set_restore(next, task_pt_regs(next)); + } } void riscv_v_vstate_ctrl_init(struct task_struct *tsk); -- 2.43.0