Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp101736rdb; Thu, 25 Jan 2024 09:19:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IGzP3gaLGwCIWykoHoXPekiX8LFZTUAEs151oNNWMroQYitOZf7z6j9yOOKe+rwpcnLxED2 X-Received: by 2002:a05:6512:3989:b0:50e:9356:1cf0 with SMTP id j9-20020a056512398900b0050e93561cf0mr85277lfu.126.1706203191833; Thu, 25 Jan 2024 09:19:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706203191; cv=pass; d=google.com; s=arc-20160816; b=fNeC7rpagmSE3JtnM5NLWe/j+5HZtjgXZ+PK5dFkKIDiVwXxJ3qf75ZPGydWbvtJhd Qi39Fdb3RYVgA23o6M8d7QNWrd1zG5eTOFaawtT2MMoWGa63SHTrjo6hzjex/x91/fVl +dUPLLfwwjcBo5M+lPa07E69h9AVL/nuQjbAjTVweJsCipo2pqSCnbfotJQLzQO/cvKE kfehINPAgWOjP3BvZJg6qUtzclnzmYJ3qKOURHl/xdNjGVOJf2BSISTZJAlZ7sLFM9/z is1RGED8anJzR1CGPDEN6QHKVSsRSP43/6b1gXKvrhOLW/wp4A3g4almllBWjpvR1LVT dZEA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=Q7MX2nHfwisXmFT5ToDLTT9/T2xUMNl+gIC/7LTn/Yc=; fh=plsn4hyjSctsgWtKXUSe6pL0dvqJ3yBc4Uk6X2Cl6iE=; b=CLmjdQx+Vo5D/6xclL2WlEYmZtbmYbKjDV+hoHLECmqBFXvtE0EShruZWlkzZ/oVzO ID8PUr78ENYgoDngXUE4tYhHNvb/H0L1HVPhQJIM1jIxvzZR6HI1CxiMEwEOb1tW8fUb PXfpKAD/cHk5n4tB6oKD4Lyc9eCV8j6jUX2VmB2zy5zfh/uA+P3XUgC0gXY2N5LEdV5T Me0z5gKywZGBqB1sXJpFPky73P2oYRrPpDAPx4vHoroRm0197lHoBiaKRLqGF8p8rJ+N bL+Tjy657V/UxJbJO93dYhd6NTPQEM0LCldjTEc+V/E82rXAT07hWEEcfGQXTX6UUUuN jCjA== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-39026-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39026-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id dg4-20020a0564021d0400b0055c27a816desi4897452edb.25.2024.01.25.09.19.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 09:19:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-39026-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-39026-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39026-linux.lists.archive=gmail.com@vger.kernel.org" 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 90F321F2A48A for ; Thu, 25 Jan 2024 17:19:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 87704133433; Thu, 25 Jan 2024 17:19:02 +0000 (UTC) Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [217.70.183.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F198B131E2C; Thu, 25 Jan 2024 17:18:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.199 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706203142; cv=none; b=oSGzi2MM56Dx2rb4FD5kJUw1u5AF43+BH4CXWeGH9rg+iXsVtaXfv3VxbrUFpkD5vJLbNQh6BtOvzBbppzz1leENohkS0Mud/U9dXx4QOOhWoSp6/P7QvmGFxqAn+q4WMbPmI57RyohYdp01fJgPROfWUAgtIrCOmlwLI/PVB+U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706203142; c=relaxed/simple; bh=sNJgdC50zN0C38SJTk6MtteKpj5pj6U+fUW/cNdoTJE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=u7Shtb65s+NhFaY+X+LooMXqExVvUgiZ/GiTFNFEtTW5LRpHVXLABvDJkMSJdc6JM8KS3/xm+U4Hk2G0W2oOPilbDuymQshz1Te0IODuKy5xh2Fm6+sMEvtq6Jav+cL49cnC/Dx0hq1jIM6YJO8VTElwv/r3DkACYZbvfQofvn0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr; spf=pass smtp.mailfrom=ghiti.fr; arc=none smtp.client-ip=217.70.183.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ghiti.fr Received: by mail.gandi.net (Postfix) with ESMTPSA id 8A3F7FF806; Thu, 25 Jan 2024 17:18:55 +0000 (UTC) Message-ID: <9d6cafcc-2bb8-4c7d-8d53-8c10a29a56c9@ghiti.fr> Date: Thu, 25 Jan 2024 18:18:55 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v10 2/2] documentation: Document PR_RISCV_SET_ICACHE_FLUSH_CTX prctl Content-Language: en-US To: Charlie Jenkins , Paul Walmsley , Palmer Dabbelt , Albert Ou , Jonathan Corbet , Conor Dooley , =?UTF-8?B?Q2zDqW1lbnQgTMOpZ2Vy?= , Atish Patra , Randy Dunlap Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Atish Patra References: <20240124-fencei-v10-0-a25971f4301d@rivosinc.com> <20240124-fencei-v10-2-a25971f4301d@rivosinc.com> From: Alexandre Ghiti In-Reply-To: <20240124-fencei-v10-2-a25971f4301d@rivosinc.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-GND-Sasl: alex@ghiti.fr On 25/01/2024 05:23, Charlie Jenkins wrote: > Provide documentation that explains how to properly do CMODX in riscv. > > Signed-off-by: Charlie Jenkins > Reviewed-by: Atish Patra > --- > Documentation/arch/riscv/cmodx.rst | 96 ++++++++++++++++++++++++++++++++++++++ > Documentation/arch/riscv/index.rst | 1 + > 2 files changed, 97 insertions(+) > > diff --git a/Documentation/arch/riscv/cmodx.rst b/Documentation/arch/riscv/cmodx.rst > new file mode 100644 > index 000000000000..24aafa23a72b > --- /dev/null > +++ b/Documentation/arch/riscv/cmodx.rst > @@ -0,0 +1,96 @@ > +.. SPDX-License-Identifier: GPL-2.0 > + > +============================================================================== > +Concurrent Modification and Execution of Instructions (CMODX) for RISC-V Linux > +============================================================================== > + > +CMODX is a programming technique where a program executes instructions that were > +modified by the program itself. Instruction storage and the instruction cache > +(icache) are not guaranteed to be synchronized on RISC-V hardware. Therefore, the > +program must enforce its own synchronization with the unprivileged fence.i > +instruction. > + > +However, the default Linux ABI prohibits the use of fence.i in userspace > +applications. At any point the scheduler may migrate a task onto a new hart. If > +migration occurs after the userspace synchronized the icache and instruction > +storage with fence.i, the icache on the new hart will no longer be clean. This > +is due to the behavior of fence.i only affecting the hart that it is called on. > +Thus, the hart that the task has been migrated to may not have synchronized > +instruction storage and icache. > + > +There are two ways to solve this problem: use the riscv_flush_icache() syscall, > +or use the ``PR_RISCV_SET_ICACHE_FLUSH_CTX`` prctl() and emit fence.i in > +userspace. The syscall performs a one-off icache flushing operation. The prctl > +changes the Linux ABI to allow userspace to emit icache flushing operations. > + > +As an aside, "deferred" icache flushes can sometimes be triggered in the kernel. > +At the time of writing, this only occurs during the riscv_flush_icache() syscall > +and when the kernel uses copy_to_user_page(). These deferred flushes happen only > +when the memory map being used by a hart changes. If the prctl() context caused > +an icache flush, this deferred icache flush will be skipped as it is redundant. > +Therefore, there will be no additional flush when using the riscv_flush_icache() > +syscall inside of the prctl() context. > + > +prctl() Interface > +--------------------- > + > +Call prctl() with ``PR_RISCV_SET_ICACHE_FLUSH_CTX`` as the first argument. The > +remaining arguments will be delegated to the riscv_set_icache_flush_ctx > +function detailed below. > + > +.. kernel-doc:: arch/riscv/mm/cacheflush.c > + :identifiers: riscv_set_icache_flush_ctx > + > +Example usage: > + > +The following files are meant to be compiled and linked with each other. The > +modify_instruction() function replaces an add with 0 with an add with one, > +causing the instruction sequence in get_value() to change from returning a zero > +to returning a one. > + > +cmodx.c:: > + > + #include > + #include > + > + extern int get_value(); > + extern void modify_instruction(); > + > + int main() > + { > + int value = get_value(); > + printf("Value before cmodx: %d\n", value); > + > + // Call prctl before first fence.i is called inside modify_instruction > + prctl(PR_RISCV_SET_ICACHE_FLUSH_CTX_ON, PR_RISCV_CTX_SW_FENCEI, PR_RISCV_SCOPE_PER_PROCESS); > + modify_instruction(); > + > + value = get_value(); > + printf("Value after cmodx: %d\n", value); > + return 0; > + } > + > +cmodx.S:: > + > + .option norvc > + > + .text > + .global modify_instruction > + modify_instruction: > + lw a0, new_insn > + lui a5,%hi(old_insn) > + sw a0,%lo(old_insn)(a5) > + fence.i > + ret > + > + .section modifiable, "awx" > + .global get_value > + get_value: > + li a0, 0 > + old_insn: > + addi a0, a0, 0 > + ret > + > + .data > + new_insn: > + addi a0, a0, 1 > diff --git a/Documentation/arch/riscv/index.rst b/Documentation/arch/riscv/index.rst > index 4dab0cb4b900..eecf347ce849 100644 > --- a/Documentation/arch/riscv/index.rst > +++ b/Documentation/arch/riscv/index.rst > @@ -13,6 +13,7 @@ RISC-V architecture > patch-acceptance > uabi > vector > + cmodx > > features > > You can add: Reviewed-by: Alexandre Ghiti Thanks, Alex