Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp482809rdd; Tue, 9 Jan 2024 09:54:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IFDlz/3kiNWnkoWePFGOXb3S9CW4xt58smoY3J8Acgk+nUmTsiMWfLBFpCh8s1R8Wgk+gfy X-Received: by 2002:ad4:574c:0:b0:680:fdfb:5837 with SMTP id q12-20020ad4574c000000b00680fdfb5837mr6279518qvx.8.1704822893641; Tue, 09 Jan 2024 09:54:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704822893; cv=none; d=google.com; s=arc-20160816; b=osooF3+hlXQZAPYd5NalvxSTvYCM8sPH0SRQ24mWXSyylpeBXHUUe8vBZY8FJX3PBB q+lvzaVMUM56ttnH8hDmqP/beyxUGAGO0Cg0L8wiV9+8XYKcNVZX1sVYer92lvttW2jV 1dittcLtme00fNfa+hdUcsRxsVlRcV5iHsyA8+nywDjmHRLI2Mnb3cArGUZdrPS2MAMF mNY+GxgkY7AW/KToEkCF/P7arSGU+jd7cfGHo9qzZZuN2miLimaO11KIpYPByEQMrhlo +PZnJc+IUyKBUrNQoYzByW9DwX46ij48yyOi/Rbqtm4wAYD1OiRsEnGvlGf9nx1LhHje s47A== 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=CeM+hho/qwbP9Z55ZVvdC0wwc1f1sWXXK1EVcgFxsIo=; fh=kDSyqvgr9ljAYs3JA7oODf7wxtmlh5QmxJ8l8kRyN3s=; b=kXfMTEJLpfqI8KEgm9xftQJIo6CCZAkP5wHfGNrqiAhwXjvD6jfJ7v8yDL+ZLG/Quw lB0YEZMjzBnrHPN5ESFV4BEK7hgKhoEiHWPWOOGmsVv+2fPkr0ezEof+FIa3bLPEd1AU Q6xZR0WX4FJ3iEs1jD2zL1u2dQq84k8cfjmE88FaNe1iGBGBkdbC0VjksJiVf3Z2SVuQ ul7CPqHsV2p70VN4jleNYe6g5gFI5XVuMB0y+g4KDTd3+8kgoQjpiWfH4V+MNnF/uC1z FdWkbxC/pC6QwMx2BNaCHa14+IPrTMJISfjgnB2l1zpGIy6T/+O7klR300sjngaPuULA bvZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b="Qvus/sT/"; spf=pass (google.com: domain of linux-kernel+bounces-21193-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21193-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id g6-20020a0ce4c6000000b0067f00b3f0c8si2653854qvm.536.2024.01.09.09.54.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 09:54:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21193-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b="Qvus/sT/"; spf=pass (google.com: domain of linux-kernel+bounces-21193-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21193-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 57CA11C24AF5 for ; Tue, 9 Jan 2024 17:54:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 07E973BB28; Tue, 9 Jan 2024 17:54:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=atishpatra.org header.i=@atishpatra.org header.b="Qvus/sT/" Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com [209.85.167.52]) (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 DADFA3C466 for ; Tue, 9 Jan 2024 17:54:14 +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-lf1-f52.google.com with SMTP id 2adb3069b0e04-50eaa8b447bso3375008e87.1 for ; Tue, 09 Jan 2024 09:54:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=atishpatra.org; s=google; t=1704822853; x=1705427653; 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=CeM+hho/qwbP9Z55ZVvdC0wwc1f1sWXXK1EVcgFxsIo=; b=Qvus/sT/+nA2q0woVUrPGBWCFUKZL1B6aNU9v5fLikXcsQwJo1KYrMQbMRW5+x8FTh FJSweKrqHATBfo/S2KhJQadbhKb4MTTPxmVrXfQqLDDA3gKaJvrToCnpZtlgYz+eKrxn Wi7GytCXvj1sgHIjJiKHmUS9njp7n85HNz6Sg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704822853; x=1705427653; 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=CeM+hho/qwbP9Z55ZVvdC0wwc1f1sWXXK1EVcgFxsIo=; b=MizVO2dQJIHNlzEp86/XdUKKG3AuvA2rmiKbHP1RPuRhgxCpUenHe24Eu3xUG5kVF3 ixC/6jC0ecxKKqBgk03MX3jYwdolWBOF1DaMTCHrD2thdfoZgRFrriyNKvjp3fBEYK6C gEuq3OT4OB00jlw3ezOsFOyM6obfesT9OprUig5c1EyDjiwFfY5W7MwdyquruCO0rWiX ZKvZ7uvOYw9ORmYILPXx5zIO8Ob5kaOO84d7NG3uOusVZiEfQgHAROOsd1+3zFTCdu2Q 8O67dn1KKYezytHEN6HpT7o+Tq977mc9ZoYSw/P1LpLyggWdyZd/GaLYJ1mq90JCeu6s O2cQ== X-Gm-Message-State: AOJu0YxAoMXqEHLnXzBr/KcNRSKSlrq9UD2jD+f0DoCu4QbC4Y2RCfRI EmH9jUkZrYP6CpE3wwv3p58O8YKSQ5Jppok8+fSEuaurU/Dk X-Received: by 2002:a05:651c:2208:b0:2cd:52fd:9bb with SMTP id y8-20020a05651c220800b002cd52fd09bbmr1112283ljq.30.1704822852166; Tue, 09 Jan 2024 09:54:12 -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: From: Atish Patra Date: Tue, 9 Jan 2024 09:54:00 -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 11:51=E2=80=AFPM Atish Patra = wrote: > > On Mon, Jan 8, 2024 at 6:20=E2=80=AFPM Charlie Jenkins wrote: > > > > On Mon, Jan 08, 2024 at 05:24:47PM -0800, Atish Patra wrote: > > > On Mon, Jan 8, 2024 at 10:42=E2=80=AFAM Charlie Jenkins wrote: > > > > > > > > Provide documentation that explains how to properly do CMODX in ris= cv. > > > > > > > > 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/arc= h/riscv/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 instruct= ions that were > > > > +modified by the program itself. Instruction storage and the instru= ction 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 use= rspace > > > > +applications. At any point the scheduler may migrate a task onto a= new hart. If > > > > +migration occurs after the userspace synchronized the icache and i= nstruction > > > > +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 instr= uction storage > > > > +and icache. > > > > + > > > > +There are two ways to solve this problem: use the riscv_flush_icac= he() syscall, > > > > +or use the ``PR_RISCV_SET_ICACHE_FLUSH_CTX`` prctl() and emit fenc= e.i in > > > > +userspace. The syscall performs a one-off icache flushing operatio= n. The prctl > > > > +changes the Linux ABI to allow userspace to emit icache flushing o= perations. > > > > + > > > > +prctl() Interface > > > > +--------------------- > > > > + > > > > +Call prctl() with ``PR_RISCV_SET_ICACHE_FLUSH_CTX`` as the first a= rgument. The > > > > +remaining arguments will be delegated to the riscv_set_icache_flus= h_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 ? > > > > Do you mean to include the ``PR_RISCV_SET_ICACHE_FLUSH_CTX`` key in the > > comment of riscv_set_icache_flush_ctx? The args to > > riscv_set_icache_flush_ctx are the args to the prctl except for the key= . > > > > No, I mean describe the argument2(ctx) and argument3(per_thread) as well. > Since this is a documentation of the new prctl, we should document all > args so that an user > can use it without grepping through the kernel sources. > Never mind. I missed the identifiers tag for kernel doc. Thanks for the reminder. The patch looks good to me. Reviewed-by: Atish Patra > > - Charlie > > > > > > > > > +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 w= ith one, > > > > +causing the instruction sequence in get_value() to change from ret= urning 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= modify_instruction > > > > + prctl(PR_RISCV_SET_ICACHE_FLUSH_CTX_ON, PR_RISCV_CT= X_SW_FENCEI, 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/arc= h/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 > > > > > > > > > > > > -- > > > > 2.43.0 > > > > > > > > > > > > > -- > > > Regards, > > > Atish > > > > -- > Regards, > Atish --=20 Regards, Atish