From nobody Thu Sep 19 01:06:22 2024 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 A0B0A4E1C4 for ; Tue, 12 Mar 2024 23:53:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710287629; cv=none; b=M0GAKLHdNnkX+RyAXAeV83YrHpAktcemcOnKOEkUau1PF10uawmcDbg0ptNRfX2cBqfVGxAiJxHrBpeF/NFmbjz5BNNpeQW54MiisiHGzUm+7LneNYnL1vShSJ7uJWA8vcCZvRVZNF3x23C6eF8JPyggzUwcJleWalPw+StOd2s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710287629; c=relaxed/simple; bh=VhbTJ9D/d65WnO+JrY1SIvIOPQdT9MHfEzWOw+OIGSo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dJGkBm/GtQjQgPAQVmQHZ9oJPWqAeR8hYMBwJ6bGLqqvsmxpTHhxwcrLKz8WHGvlxhvVaaPRyNam5xeszQc+Utr/jgwAsqMo2b/yih7EdIAXFGei3RboGHeIf+t4l2sjPRDYKsFF/Hmcru2pm4zO7rkAz60XTZtPqybIGjSEUh8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=sVJNUsyJ; arc=none smtp.client-ip=209.85.214.177 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 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="sVJNUsyJ" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1ddcec424a6so71895ad.2 for ; Tue, 12 Mar 2024 16:53:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1710287627; x=1710892427; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Xy5TOqSKX3DblYxEOD5MVVtHMk7JBMIm4GkKJlZsbS8=; b=sVJNUsyJ5goUle0ZXUpqeoFbNEn9YwoaklklZNk53B3oHOiNFnMp2gLAnKHEKQxzHd kONetzMkvVSzvdlJMDEhUkjkA8xeTMZ6T+yQDKcHYhYGaRAkLR/chWtbulxn0tFA7cp7 ORGG90zqBcP5L8qrglXqZcaO1rYHcRsnvkOoxk/HBqyY9BN/iuR9USrv4Wjqx2f9isAa AGVYG+B1RLEvNVXHJh3QWlJgT/UCu9E/Ch2c7QQedpCMMjsHI+/HuxPlaXq4yoyr0M2z MHjC84DWLRF0ZgqxXGBq6i6NBjeUw8A1fvl2rRQ1C26Q2x9jYQ9AahkWbUefpccqxASw Duzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710287627; x=1710892427; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xy5TOqSKX3DblYxEOD5MVVtHMk7JBMIm4GkKJlZsbS8=; b=g1MTea2NhPJ5FCTdee8jtCIFcVxClXchtoisaGISL08ccRNrAO3YyJEgojVsAZhNRO To0alDlwWrW312Oc7QGuGKPdb4szDS1RDsi5nQJ7No4a4yLnyohkgjI+xCt9xE0F+LHh wbzW4SVWMnv8yEnDUb58FY7a/HeRV9S/6H1p2/STRVdFd3KhlgbCRsDRSv83AQoRKaQe caqMMwpkAHt2gMVO5sDVmrLGxJHPCSDGQupUXoXo5O7cZo4nSbKHQBCa34sOn2uDMNhM 3/7106Ibc9hgrf0EpD8tbbJjZHo46ctQQYsYLv8EkuUAOS2TeJjtl3UdXSXvU2hOBt+H 63lQ== X-Forwarded-Encrypted: i=1; AJvYcCWqBVIDiF963wlWFyjXM62rd0Hh4FFZXkZA2eJKoIMcq3Ko1Rt9vOXOxLWnufvE17lMAHQAU4/oFFOp6v+02pqSdoNI+W1piA3oKqOp X-Gm-Message-State: AOJu0YxSmH6ZUY7nufR+v8RUmDlVx3OGMqGN7TFoYGP2Jgig14dTwzud OAevjD5+7/HrTa0LuDK9u41rwrIxVrHw+ofBis9PkKliCmOWJS1m0RDcZ/BqRQY= X-Google-Smtp-Source: AGHT+IGOuq44oY+SA4/K6Jk2+JXY0tCiJaFcX1Kd9JQD8qXjj735tLLLMG623VHrrnU0hXZIrtQRgA== X-Received: by 2002:a17:902:ecc4:b0:1dc:df18:c5c0 with SMTP id a4-20020a170902ecc400b001dcdf18c5c0mr11755148plh.0.1710287626915; Tue, 12 Mar 2024 16:53:46 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id h9-20020a170902f7c900b001dcad9cbf8bsm7282844plw.239.2024.03.12.16.53.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 16:53:46 -0700 (PDT) From: Charlie Jenkins Date: Tue, 12 Mar 2024 16:53:42 -0700 Subject: [PATCH v13 3/4] documentation: Document PR_RISCV_SET_ICACHE_FLUSH_CTX prctl 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-Transfer-Encoding: quoted-printable Message-Id: <20240312-fencei-v13-3-4b6bdc2bbf32@rivosinc.com> References: <20240312-fencei-v13-0-4b6bdc2bbf32@rivosinc.com> In-Reply-To: <20240312-fencei-v13-0-4b6bdc2bbf32@rivosinc.com> To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Jonathan Corbet , Conor Dooley , =?utf-8?q?Cl=C3=A9ment_L=C3=A9ger?= , Atish Patra , Randy Dunlap , Alexandre Ghiti , Samuel Holland Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Charlie Jenkins , Atish Patra , Alexandre Ghiti X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1710287621; l=4601; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=VhbTJ9D/d65WnO+JrY1SIvIOPQdT9MHfEzWOw+OIGSo=; b=cZs4wpLQVtK7aTyxMXY6Q9lLyWJ4tLsVLAH4e9c1IqQElmGi36HzjYpFtCMt0IsozfaaDWWOb QEJZI4fNKu1AxAOBDJtLa+UZHCwjpFVve7bojE9mGLUn6bjOh1cblBo X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= Provide documentation that explains how to properly do CMODX in riscv. Signed-off-by: Charlie Jenkins Reviewed-by: Atish Patra Reviewed-by: Alexandre Ghiti --- Documentation/arch/riscv/cmodx.rst | 98 ++++++++++++++++++++++++++++++++++= ++++ Documentation/arch/riscv/index.rst | 1 + 2 files changed, 99 insertions(+) diff --git a/Documentation/arch/riscv/cmodx.rst b/Documentation/arch/riscv/= cmodx.rst new file mode 100644 index 000000000000..1c0ca06b6c97 --- /dev/null +++ b/Documentation/arch/riscv/cmodx.rst @@ -0,0 +1,98 @@ +.. 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 L= inux +=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 tha= t were +modified by the program itself. Instruction storage and the instruction ca= che +(icache) are not guaranteed to be synchronized on RISC-V hardware. Therefo= re, 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 har= t. If +migration occurs after the userspace synchronized the icache and instructi= on +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 calle= d 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() sys= call, +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 p= rctl +changes the Linux ABI to allow userspace to emit icache flushing operation= s. + +As an aside, "deferred" icache flushes can sometimes be triggered in the k= ernel. +At the time of writing, this only occurs during the riscv_flush_icache() s= yscall +and when the kernel uses copy_to_user_page(). These deferred flushes happe= n only +when the memory map being used by a hart changes. If the prctl() context c= aused +an icache flush, this deferred icache flush will be skipped as it is redun= dant. +Therefore, there will be no additional flush when using the riscv_flush_ic= ache() +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. T= he +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 =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_CTX_SW_FENCEI, PR_RISCV= _SCOPE_PER_PROCESS); + modify_instruction(); + // Call prctl after final fence.i is called in process + prctl(PR_RISCV_SET_ICACHE_FLUSH_CTX_OFF, PR_RISCV_CTX_SW_FENCEI, PR_RISC= V_SCOPE_PER_PROCESS); + + 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/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 =20 features =20 --=20 2.43.2