From patchwork Thu Oct 19 15:45:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13429471 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D0D96CDB465 for ; Thu, 19 Oct 2023 15:46:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=5oTxZxDbUD6nSToakSkwqf8o5SdirD2G0fU74EKFOeY=; b=loFgvI/FAlHfd5 X8B7KwpzJZOB+Zw9s87iHih9oXzd8/4WIfPnpXvid/PsdinH5uOBLRobkWyK4J+qXZVs86XQ4/ea0 EZJROqxiBmbaFKrmTdBsyopNpZ00HW2dNYC660QHhCDumCX4qDVdXJgM9MqY9Y5wBBFYPG1hjzIMr WInx8m42kLKX8RFcl7vXv8DlzbbDpgBmbaZKI8Zf0W9cjCCJDBeh+0egoiXWVU4aqdTWpFw3gwQSZ 4Q2XZ6BIcDjc8w0Vt1O2DJEnU1U1lOLPyEk3SxJQHvZhFTBjezHXDo9+HGu/F0OEfneKqGQkv76AY oOzDRmbXZ7RAYxVrbBxg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qtVDw-000ErH-0k; Thu, 19 Oct 2023 15:46:12 +0000 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qtVDs-000EpT-2k for linux-riscv@lists.infradead.org; Thu, 19 Oct 2023 15:46:10 +0000 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1c9b70b9656so54395575ad.1 for ; Thu, 19 Oct 2023 08:46:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1697730367; x=1698335167; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=o/1Sbnu2zG2XHrvwhNz0MNl6g+38tRSerwv//oFJB9A=; b=Fm8vzGVI8KxKLltZTUZ+C0y1JHDZCVfYDG8GFaf1PjQZOXaGOKu+QFTmXsWMWBU+et /xtJm8fY9esDO0ne+F/OAcm+fhdO96EtLA0+Aj1KfKmkfUBA41wGx7XLKumfGq5o+wtg k2EKEokQiP08Abip/z/Gl4Kdxd+40VzxxOTxDunxFjdZeJUGO8kVxIZotX7jwCSpaakl OO4pZarsBfD6F42A8agWgOlivCxOWugtpr62q5gCXS/a2py/oBfG2K9pmooW7tP2HJ1o GGR0MAKlM31WK5dALWFI14gBZKV0lkaRw5/PdStKjv0MKOZ8mL2mHwjpNf8+kBCG6t3a ikiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697730367; x=1698335167; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=o/1Sbnu2zG2XHrvwhNz0MNl6g+38tRSerwv//oFJB9A=; b=lSWHTLlpzBdrjbLcVq1POCP/etwyvPPzTh76Gj6WMXQLjw/XOUFTmw9581rYXGHRXU 0q+Sqn/kG5Vc8zWm22N+2hjJoLBksQ6eoTzrYGeFow9OuSBmndV68ws6d6uXdDv9g6rS Lk/NyeLG/n8PU0NilqXcbtSYv4j2rZbX6bKkP3K8IxbKmP4JbOBCJ/c+nGQ6FnyJGfNL zelzBOPCPDKHXMy1eYy7REukLIgBBn5YcVdq3Fa5dn7sSIPAjlipAqhi1WVZj71QfRQ+ aDxYsL9kmkfQ0aCYNtMHlij6nK2c4mVFP/k6xSm/lkoxmbdxnTtKastUQ9yEzHjltnY5 4KIQ== X-Gm-Message-State: AOJu0YzJxwspIZwE4+HuWZMrmdL+4r7NwKH+nPcyYEbgOFA8iLZRUPku bM/GTAMgUjoKdPT33I6ANQap3bUeToYxsdNnUt/Aysi4E8Vc0w4CZ+Z2wmmvDYnHdRVu2NkSfJ7 noX4W8sjqdrq+eCGVZPfPOrKC+aMVFWaepA3Cw1rlvNkPoy8WPIvO8gZLImbiPjM5hK1BVhiSwi 4wSfu/r/nl66+5T5w= X-Google-Smtp-Source: AGHT+IE27JQz2UPJW0efo195prSmXNVZmNPh8ibKWlGZSPbeJqARe8WFpbn3Kkee3+FQKSYhEKPZ7w== X-Received: by 2002:a17:902:fa83:b0:1c9:ccbd:6867 with SMTP id lc3-20020a170902fa8300b001c9ccbd6867mr2597923plb.38.1697730366638; Thu, 19 Oct 2023 08:46:06 -0700 (PDT) Received: from hsinchu26.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id u8-20020a17090282c800b001c75d7f2597sm2084710plz.141.2023.10.19.08.46.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 08:46:05 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com Cc: paul.walmsley@sifive.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, bjorn@kernel.org, peterz@infradead.org, tglx@linutronix.de, Andy Chiu , Albert Ou Subject: [v3, 0/5] riscv: support kernel-mode Vector Date: Thu, 19 Oct 2023 15:45:47 +0000 Message-Id: <20231019154552.23351-1-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231019_084608_962020_C4BA003C X-CRM114-Status: GOOD ( 16.73 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This series provides support for running Vector code in kernel mode. The implementation is based on the v12 series of the Vector series, but with some additions. First, patch 1, 2 add the kernel-mode Vector patch from v12 with minor modifications. Then, we introduce a mechanism to defer restoring Vector context for userspace programs (patch 3). This is similar to arm64 and x86's approaches when dealing with extra userspace register context. And it is benefitial to both Vector in user and kernel-mode. At the end of the series, patch 4, 5 add supports for making kernel-mode Vector code preemptible. We do this by adding kernel-mode Vector context, and keeping track of the frame where V context is last valid. We believe that enabling preemption of running V is a critical path for getting V more generally available in the kernel-mode. Besides, with status.VS, we can tell if saving/restoring V is required. So we don't have to save/restore extra context at each context switch if registers are not dirty. The series is tested by loading a kernel module on a PREEMPT kernel. The module launches multiple kworkers which run Vector operations and verifies with scalar code. Also, the module provides userspace intefaces via fops to verify if we can run Vector code on syscall path. Note: we still have not come to a conclusion on whether a preempitble kernel-mode simd is a good approach. Also, whether to use the same API to enable it. I am posting this again to fix known issues and comments from v2 and provide a to-date reference for who wants to develop kernel-mode Vector on riscv. Updated patches (on current order): 1, 3, 4, 5 New patch: - Unchanged patch: 2 Deleted patch: - Changelog v3: - Rebase on top of riscv for-next (6.6-rc1) - Fix a build issue (Conor) - Guard vstate_save, vstate_restore with {get,put}_cpu_vector_context. - Save V context after disabling preemption. (Guo) - Remove irqs_disabled() check from may_use_simd(). (Björn) - Comment about nesting V context. Changelog v2: - fix build issues - Follow arm's way of starting kernel-mode simd code: - add include/asm/simd.h and rename may_use_vector() -> may_use_simd() - return void in kernel_vector_begin(), and BUG_ON if may_use_simd() fails - Change naming scheme for functions/macros (Conor): - remove KMV - 's/rvv/vector/' - 's/RISCV_ISA_V_PREEMPTIVE_KMV/RISCV_ISA_V_PREEMPTIVE/' - 's/TIF_RISCV_V_KMV/TIF_RISCV_V_KERNEL_MODE/' Andy Chiu (3): riscv: sched: defer restoring Vector context for user riscv: vector: do not pass task_struct into riscv_v_vstate_{save,restore}() riscv: vector: allow kernel-mode Vector with preemption Greentime Hu (2): riscv: Add support for kernel mode vector riscv: Add vector extension XOR implementation arch/riscv/Kconfig | 10 ++ arch/riscv/include/asm/entry-common.h | 17 +++ arch/riscv/include/asm/processor.h | 2 + arch/riscv/include/asm/simd.h | 56 ++++++++++ arch/riscv/include/asm/thread_info.h | 6 + arch/riscv/include/asm/vector.h | 50 +++++++-- arch/riscv/include/asm/xor.h | 82 ++++++++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/asm-offsets.c | 2 + arch/riscv/kernel/entry.S | 49 ++++++++ arch/riscv/kernel/kernel_mode_vector.c | 148 +++++++++++++++++++++++++ arch/riscv/kernel/process.c | 10 +- arch/riscv/kernel/ptrace.c | 7 +- arch/riscv/kernel/signal.c | 7 +- arch/riscv/kernel/vector.c | 5 +- arch/riscv/lib/Makefile | 1 + arch/riscv/lib/xor.S | 81 ++++++++++++++ 17 files changed, 516 insertions(+), 18 deletions(-) create mode 100644 arch/riscv/include/asm/simd.h create mode 100644 arch/riscv/include/asm/xor.h create mode 100644 arch/riscv/kernel/kernel_mode_vector.c create mode 100644 arch/riscv/lib/xor.S