Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp53328rdd; Mon, 8 Jan 2024 17:25:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IGuicT2+GChPIcsx64yng7roZHmawr/yMa4LlnLB13aFsEm93Jd7jJTsT79eFOe+HrY++zu X-Received: by 2002:a05:6a20:1453:b0:199:eb43:1542 with SMTP id a19-20020a056a20145300b00199eb431542mr507848pzi.1.1704763514639; Mon, 08 Jan 2024 17:25:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704763514; cv=none; d=google.com; s=arc-20160816; b=CpoY2HcTWzR4agnCTLCs7pVwG9vl35/Fi8JSogFfDeN6kbynS9W/CLTCna73roQpYW Inun4eHjhG+aipTvxy24t0HtiWSvz2Ar0HgRtCH+QXYWxd2eC6ulHJ3F7YKFCVDhIVLT 1ipllBDg1P9jrw8gPK4SGOufKmDEtw4uiQXlHpQfbcFT7xZvayd5GBAFaqh2XJPCg6IG nrX2RWova9kUjeeOi57BoTJVtvlGna9BvAH6JWzUZlVT8O2djq6RIE+/xse3oc6Cd2LW 19Kl5njU7oX5KSxaSrTT+csb5rwLQg23PP/0QOJcTIaWe+VFIf9GeXcpIPYXQCz05ozr 0TWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=UIbDSUCc838y1oV7jmlYgxyTqimJrrucnoF1PISCp8k=; fh=kDSyqvgr9ljAYs3JA7oODf7wxtmlh5QmxJ8l8kRyN3s=; b=CfYzvcMXf7FxKGFO3qrCjh+XGnwsNOG8uwdfqJ2GkSpLbvBLYq4c0Kz899/SjvGcsx P36m/wpbVnn1qXNZfK4yfoKFEmVwXTC8K5tXstup+6rEcOmU0H58rKN+UFqJkm24TuXE C/m/ncz8iOCQ8BegxpRM1XknWb/D9RlLgERjj7GoNX8JeZdLB3SyqGrnnT6K5HyFvqB7 UIrS9hTZPmOixkkImmbz3qDstgeEqyRGlo1yuJvh0C4PoWPc60GWo8hZjFDOmmQMVDgj ShMtM8eVwtBYw47bJLpMLSvyEIn+fRx8WQU3GPCJsdoP6vkXe/uOcqCzsRwQhORG/H9A c/9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@atishpatra.org header.s=google header.b=DqRomOwC; spf=pass (google.com: domain of linux-kernel+bounces-20260-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20260-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id z93-20020a17090a6d6600b0028cf198066esi6458444pjj.31.2024.01.08.17.25.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 17:25:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-20260-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=neutral (body hash did not verify) header.i=@atishpatra.org header.s=google header.b=DqRomOwC; spf=pass (google.com: domain of linux-kernel+bounces-20260-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20260-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id F21A6284134 for ; Tue, 9 Jan 2024 01:25:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1DC6E29A2; Tue, 9 Jan 2024 01:25:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=atishpatra.org header.i=@atishpatra.org header.b="DqRomOwC" Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.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 0458028EA for ; Tue, 9 Jan 2024 01:25:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=atishpatra.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=atishpatra.org Received: by mail-lj1-f171.google.com with SMTP id 38308e7fff4ca-2cd5c55d6b8so12876421fa.3 for ; Mon, 08 Jan 2024 17:25:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=atishpatra.org; s=google; t=1704763500; x=1705368300; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=2LI7RsQHT+MQ0HPpr0l5UjU5EPgbfYzbdDPhebxTuko=; b=DqRomOwChNh+9wD2BAnuEHziK8E+tJWZmbF4BEllrhwufIaYJjM/1E0YICBCil6hAB ltk/+uwhEhc5W+6yVhV2CN4YTWOupPYgyOiixwUw9QPncIw8GjpPGHEpIOOvV/f+RcHj CS2kSauOfEogy+pjFccJD+VeTHmhxDOIZNBXo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704763500; x=1705368300; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2LI7RsQHT+MQ0HPpr0l5UjU5EPgbfYzbdDPhebxTuko=; b=O6EQ8gKHwND9/Ps0q0d4hOs/sTNpy5+hr7VGwcqMK0DbkejglyXmNTpL6ZkHTLNNOv MQ3IvIQedTGldWIifLxcLkYDojgv9k6nfSCeMTz7CZNCDJA5FVKoSyJPu8Mg88LnIzXo NuawmL3J9L/AcpLGimvGv61GCLn75L4ZrVONsYSX0tHhfpqZYgIoAW/HSIs5rCWuyGNQ ZS3Gpl7IoJbHTVtdJYNU3GEpKoQl/N29lS3j6ZA4arvocWjYY3mFjhLr5aIIz8PBewRv VFOIGL5MiJSo+6F6+0LRn7aPYw/a4WqpRydjg8VESEK+jPLKDMc/OJxa76CP2R0+N2Cn LwYg== X-Gm-Message-State: AOJu0Yynv9eencYJqA0PO4jq70tpxVjZSPHHkYFNvT3Rjf6Flr2ct2ak gL/OATCFn1LEO8zLPa/8pSBmM34wP59jlwvTNGigazXN7tqy X-Received: by 2002:a2e:9212:0:b0:2cc:da2a:d266 with SMTP id k18-20020a2e9212000000b002ccda2ad266mr1270020ljg.71.1704763500076; Mon, 08 Jan 2024 17:25:00 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240108-fencei-v5-0-aa1e51d7222f@rivosinc.com> <20240108-fencei-v5-2-aa1e51d7222f@rivosinc.com> In-Reply-To: <20240108-fencei-v5-2-aa1e51d7222f@rivosinc.com> From: Atish Patra Date: Mon, 8 Jan 2024 17:24:47 -0800 Message-ID: Subject: Re: [PATCH v5 2/2] documentation: Document PR_RISCV_SET_ICACHE_FLUSH_CTX prctl To: Charlie Jenkins Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Jonathan Corbet , Conor Dooley , =?UTF-8?B?Q2zDqW1lbnQgTMOpZ2Vy?= , Randy Dunlap , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jan 8, 2024 at 10:42=E2=80=AFAM Charlie Jenkins wrote: > > Provide documentation that explains how to properly do CMODX in riscv. > > Signed-off-by: Charlie Jenkins > --- > Documentation/arch/riscv/cmodx.rst | 88 ++++++++++++++++++++++++++++++++= ++++++ > Documentation/arch/riscv/index.rst | 1 + > 2 files changed, 89 insertions(+) > > diff --git a/Documentation/arch/riscv/cmodx.rst b/Documentation/arch/risc= v/cmodx.rst > new file mode 100644 > index 000000000000..afd7086c222c > --- /dev/null > +++ b/Documentation/arch/riscv/cmodx.rst > @@ -0,0 +1,88 @@ > +.. SPDX-License-Identifier: GPL-2.0 > + > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D > +Concurrent Modification and Execution of Instructions (CMODX) for RISC-V= Linux > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D > + > +CMODX is a programming technique where a program executes instructions t= hat were > +modified by the program itself. Instruction storage and the instruction = cache > +(icache) are not guaranteed to be synchronized on RISC-V hardware. There= fore, 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 h= art. If > +migration occurs after the userspace synchronized the icache and instruc= tion > +storage with fence.i, the icache 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() s= yscall, > +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 operati= ons. > + > +prctl() Interface > +--------------------- > + > +Call prctl() with ``PR_RISCV_SET_ICACHE_FLUSH_CTX`` as the first argumen= t. 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 > + Document the arguments of the prctl as well ? > +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 on= e, > +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 =3D get_value(); > + printf("Value before cmodx: %d\n", value); > + > + // Call prctl before first fence.i is called inside modif= y_instruction > + prctl(PR_RISCV_SET_ICACHE_FLUSH_CTX_ON, PR_RISCV_CTX_SW_F= ENCEI, 0); > + modify_instruction(); > + > + value =3D 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/risc= v/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 > > > -- > 2.43.0 > -- Regards, Atish