Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp72057rdd; Mon, 8 Jan 2024 18:21:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IEvV4rxTur5ifxptTXDCk7T1QXgh6Ngy1iHSgNXv52lBnjLJSMuZf7iYP2SeNBF/aQlJR9o X-Received: by 2002:a05:620a:40c5:b0:783:1ba9:4d1e with SMTP id g5-20020a05620a40c500b007831ba94d1emr4450693qko.109.1704766869987; Mon, 08 Jan 2024 18:21:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704766869; cv=none; d=google.com; s=arc-20160816; b=lvYH3IRDOj0Nicm7Bryv4Cr10FvHMWn2uDd3FdbOVVbktB8oKeuOVkMx22fCjE4OKy sJOJ1Z4A0UwRun3jg43Qb5WdkJnEVOWV6dwNAIO8z2zxwBFz9HGScOTKEeLpQFJ7rLbi hC2wyEQmKnRvqX+Yzh/U5h+aeOue92h3awUgfhEh6pqhmnez1Sz6Nw5yICd9OtXeXHO2 zgfcy2R0scAqgj4Wz/KAcEyG+l82BW/NdcOuPZWblf/dmn4H8ELlCtCV44GUd1BCjaoh POwACFX9xXe++0zDaLHjI08C0Icm0Yj+tmDpk55gOG02iDBqQu+t+XIFnM5KsQ4LG91u KPAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=GVG8c40f68G3korcyEKi0z8ULvfpyOFV+s3GtOXVEHo=; fh=Hx/vqu6kD2zNFwVIsqGqjbinEVpsL6e8SOng7abaScs=; b=n5kz8ew5AhdQHJ+9KCV/bfpRw1Sqqaql3/EzJXoV+xTmxy6WEsGI+Wv22g0+7oAwI6 j8GdAL+k5IvDJfd4R801QKMXVJt7Hzcu4+nYd0SU+/nQWkjavV270ZhoK7bagKF/uORi iUV+/s7PSWsz9r4EPWWY9qa2HBnuNM64UJ/rAhRVCSKVdip9vGaCVDXwiAjfNryjilxp w1YmZNjvAgiD4mdOGrzn6TtCdLLEf92+FXCqqLZJhU02LqXDuMnPBBhsnw24FedVLjnf /Gn1ibazl4pB7WrWJFnUbsl4tl2LdxTsldJfGAvWAQyYeYJB9DcceW4TS0d1iiQ9j6xj Gkdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=1F2wpBk7; spf=pass (google.com: domain of linux-kernel+bounces-20283-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20283-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id m1-20020a05620a24c100b00781d107f9fcsi1164097qkn.641.2024.01.08.18.21.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 18:21:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-20283-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=1F2wpBk7; spf=pass (google.com: domain of linux-kernel+bounces-20283-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20283-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 AE7AF1C233F3 for ; Tue, 9 Jan 2024 02:21:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0BA0328E8; Tue, 9 Jan 2024 02:21:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="1F2wpBk7" Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 63B0523B1 for ; Tue, 9 Jan 2024 02:20:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1d3f8af8297so9419575ad.2 for ; Mon, 08 Jan 2024 18:20:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1704766858; x=1705371658; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=GVG8c40f68G3korcyEKi0z8ULvfpyOFV+s3GtOXVEHo=; b=1F2wpBk7F5WZV0KDV4EUPOhKPnarcGdL1A8WWZtSUqfjZJ/70wewamBM8hRj7c1ib3 V1HKJiagBjCylBltt5spy9lKJdy5ad3TYNBWwcC5Sftk0zBn00VXvxG14awwN16trbsr Hu8gOnGYgwt1OuPb+P7D25h96NL3awnoxOACJ1XLxNdlCB5pmGE6krw4bnyjkIfZR4a6 x1dx+FBp3BwSCbvKbSf1GLZDEsBEbAXsDKtKth99gjehjL1+I31QvRJfXQT0576kGXNP DTLxtb9gRr/IsjRWkot8v6OAJ7FnKHSdpF3DSjTtouMT+OLyG+rSD2LzsWJQynKduqUv Jbtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704766858; x=1705371658; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GVG8c40f68G3korcyEKi0z8ULvfpyOFV+s3GtOXVEHo=; b=hS0DtniBZ466jn7U75iBwLPgd2iGlH3Ya9+eC0Fg2UMMqiuwEXhOdvmjQS9qTb6059 zo8V8mpn1Bqq3Su4ZkWQJocyVcVwd5Nk+ZfOYQ8UKEghfyG5067TEewO6jUBBF83RkMG uwrHbQV2G9M/a0r57XSI+Y3ODlyX1yUJqoMzdhrhfLuIf6ekxkqgDT40OQtkNsXx2anW Boedh/x2iyjuLozhty/JXsTlfFJtlvjdZR/KHUclTvryGjxnmeVY7yGPRnm9g6TuDju8 lKtfFTL56iyUj9pojWRzQmSAUABBw8OnZ7vBhiObqKRNsoJHDmmn5+TlpykSs3mcqDt9 UrWQ== X-Gm-Message-State: AOJu0YyQQ0SKHqbOUTup9QQqfV/5MpoItPLddB8Ar8LbnaF7QXB8b6zG RfREewLg1iVfbb5MTzV6BtLTyZD2ylK2YuF3ZEjPptRBYe0= X-Received: by 2002:a17:902:740b:b0:1d3:ec6f:3c47 with SMTP id g11-20020a170902740b00b001d3ec6f3c47mr2406282pll.71.1704766858597; Mon, 08 Jan 2024 18:20:58 -0800 (PST) Received: from ghost ([12.44.203.122]) by smtp.gmail.com with ESMTPSA id d19-20020a170902c19300b001bc930d4517sm567240pld.42.2024.01.08.18.20.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 18:20:58 -0800 (PST) Date: Mon, 8 Jan 2024 18:20:55 -0800 From: Charlie Jenkins To: Atish Patra Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Jonathan Corbet , Conor Dooley , =?iso-8859-1?Q?Cl=E9ment_L=E9ger?= , Randy Dunlap , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Subject: Re: [PATCH v5 2/2] documentation: Document PR_RISCV_SET_ICACHE_FLUSH_CTX prctl Message-ID: References: <20240108-fencei-v5-0-aa1e51d7222f@rivosinc.com> <20240108-fencei-v5-2-aa1e51d7222f@rivosinc.com> 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-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Mon, Jan 08, 2024 at 05:24:47PM -0800, Atish Patra wrote: > On Mon, Jan 8, 2024 at 10:42 AM 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/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 > > + > > +============================================================================== > > +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 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. > > + > > +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 > > + > > 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. - 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 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, 0); > > + 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 > > > > > > -- > > 2.43.0 > > > > > -- > Regards, > Atish