From patchwork Mon Feb 12 23:36:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 200103 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp226416dyb; Mon, 12 Feb 2024 15:37:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVDkR9v5p5IH7kwa/l98d2xmBfNcdM2E5Ymw+mZA2RbjcphnBEWFqBwtdSRNqjnPNMUXxqaGMoSNPyDHnNo2DO9JqZJxA== X-Google-Smtp-Source: AGHT+IFm9k6l9yCAZXKzL2r6y1pJZNtQfuUgEsN//ju2oE9AYU+wKsz5ia1QXJcO66eiQfvY1ERK X-Received: by 2002:a25:f621:0:b0:dc7:776b:5e4a with SMTP id t33-20020a25f621000000b00dc7776b5e4amr5032822ybd.56.1707781057831; Mon, 12 Feb 2024 15:37:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707781057; cv=pass; d=google.com; s=arc-20160816; b=sJejzyO3hPq8ai1sm25+bokE5/TlTpFEsRWQdMDm9ndq93gOLDNlm6oH9C9QiarNgu 27YO6xXQKfisUrfdQGkC4hsNmWZ6X0d7LvuIv+Jec+5mBAdve/6oYAHXdRtTgSG7VZKH 9OtwVtQbXMLFZG4CRqyIiHkp80xUe4eVHHOd6K77QgGlw10cxMXuXCst9S0IEg7S5NQ6 mViPDiOvaXfFYUTIr9ldREONWPpzOoPY6+fRCBDa1CeWISj2tTtKt2gbIL7ne4VcKPrb eK4O42A26t+BIxQlWIDYabZ4A0QGfwk4T5LRat2fmgkeO211vQhbo/z5mAJ8/MBfHjhp 5dJA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=7cnan8PSAVXC3cUypQgKZqafgG+bekGNiKDSOb6OW5c=; fh=4pY8SxA4zQkAWlhK/iW/3z6NDHpuALqkMGyioM1+eTg=; b=fOkamYUz3smnVT3q9nezvSFhJXEUPPsyd/byj/y8KdRq85gg7KmaR2RrxD3EXh3xCU wRX2P2LbNjboySYKDy+NluAJkUXSLRr15Tfgo2XLuZqojbQpX+/Fi2wjX+kJmCiKqA6K 6aSMhxAGeIqOBDdaVeEWaV74Ad3HH8HhBK8no7nT3tjAsSH94iPE6xGftCjNbavMBU+f 9IQE8xeUQXrVWFWz1BJLPI71fIsiRCIu/ZaATHnQ3mEW/tq5K6r1R+1zNJ1Zj1bcd14t RLvjhtyt5g431Xq6deE3znVS6nuN5uJGN0JWflY6iaz+5SfIt6hwar4HpfMzHKxdlZNc eGFg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=okLmbxmk; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-62545-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62545-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=2; AJvYcCWQ1mMDlomIqzUn+Tt9FrwNtyZ1Ac1jvlyQZz3bzVkDTR49ijhjrRpdW2c6jyJRrk8V33zsESxQPWHaQq3gpqOP3j41dA== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id h1-20020a05620a13e100b00785d6d13172si3288838qkl.164.2024.02.12.15.37.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 15:37:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-62545-ouuuleilei=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=okLmbxmk; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-62545-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62545-ouuuleilei=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 8DABB1C2212D for ; Mon, 12 Feb 2024 23:37:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4BB3F52F75; Mon, 12 Feb 2024 23:36:41 +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="okLmbxmk" Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.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 02F3550A70 for ; Mon, 12 Feb 2024 23:36:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707780997; cv=none; b=Ue+CfQ3dmD83QB8rH8uTVchJL9XD5wQu6LKeUaOxajAmojJrNBeMdT0vAlJf1mbet5C7ZLaKpfVAeIe8yPmsmvbtpXlp1TYyxM9XJeAvGBxtUM+ezSOelORurvlIKBcFrDxklQ3+wdFVJS7Et0fo5ggiNZGHIZP7fYceCsmLkCY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707780997; c=relaxed/simple; bh=h5IjKPzf39ifTQj2Z0xAqATqlmjtpC61zariLMJKI0A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XcMMXdzYvzAD5PZ5mGIokMRRoLGWybej/d2EKvTqCJIiTnIYcifkAljtF1uvKygPnXjcB5NZWZdWqmB9NpN9QeHPAS+ydF8NjdIZcdjMo3BRdoLaiXzTqZERsjJJt/ynpg0+evQx4XjAhUDKL2VnWX9D82pyCs+a3vL1eeiEkLM= 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=okLmbxmk; arc=none smtp.client-ip=209.85.216.52 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-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2971562c3ceso1790322a91.1 for ; Mon, 12 Feb 2024 15:36:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1707780995; x=1708385795; 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=7cnan8PSAVXC3cUypQgKZqafgG+bekGNiKDSOb6OW5c=; b=okLmbxmkTuNXAYv90pOm7DssvdKt39xjXkYFgWttX0SxHB0Bl9wjPCM58z4kUpEbsZ soGnCo2gjNnbND7vR+axbFApO32czykT0/SYRp3YUE1rdTxmNlY81+3a4baL3JzrgO2S aEMIh73tGc55Q67V1FWqqK3ChT3ALPk243DdKiaaAzdBfWKrs4g3rHN41fQwA/ooLB00 K0YQTB0LS0u/IDkidU7tjV3RW3RakyIg8e6bFlog5/mcArGaXIzSI7lv2MNDies1H0kC UylvUoCtZhs99x8Hm9EFQbq4y85obQtOlM9hydfzbbuLH3f2EVDMooPdBbBXpiwCm734 RMSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707780995; x=1708385795; 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=7cnan8PSAVXC3cUypQgKZqafgG+bekGNiKDSOb6OW5c=; b=HQ5xEskvvvITkkK+i8a9iaq2v3LE1xLIyVlNE2zxrPCgOqt11JvcQoqnSP2UH4szVt +MtDHIYof067pbhUBhJRhkQQvhl+43JVWtTHKZqiydT7lkBCZu0NmjtY2v6zcMsIzwNH RdSkH45gKVPZOnCvmgp4YCR0JbW7DAZvCpyH5U5bLKf84E563PMSk3QgtJJZ4/Dijtk/ XnBDhYWCyjAaNQzNjQHJZaBGEzXR7UBGF3aMQFz5M8IiohUEvoMMSptaer25Bqh6KfDK 4ItJZyfIMYk37uS8uJh6IPONCPbq5I64KUcDlhPqUQIjeYRSjMcp1hCGu4w9D7XVSjxT H2xw== X-Forwarded-Encrypted: i=1; AJvYcCWeqENHV8jJ/BbruV6Jew7TI+Juq0YbuCyYPiz62WWxSlCJ+807vk5ChIsWz32Bv8kRy7pMGA398u4Oz6BXTx+HcVI/vVM7rx5TjTaj X-Gm-Message-State: AOJu0Ywd8JLZA5F++54WLArp9ORR6z/3cHtshwOTurRSccHaCF3GKmx8 n7xAklMRXfLCkM5voyTofnrtHB9YNc8mBFyiDm/13Hyk1amMpFBR4zv95/LI7LA= X-Received: by 2002:a17:90a:b395:b0:296:2f9b:8c2f with SMTP id e21-20020a17090ab39500b002962f9b8c2fmr5593744pjr.40.1707780995399; Mon, 12 Feb 2024 15:36:35 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXmdXilEjO7FPIdqZaOyYuCeviPFS0SqLHZnCp/3gpSUAcvcq1RseNEO4AQ/xLKhhErZIkjWUUVEw5LUaDtfKBINEqVwe12bA0TS73NEdLCj0rsj6xYtj9GqmqLqCqspVc9Geyq4JxEFeQttHJs/GpyH2RJ64YcR66d5DidBGbD+P1Sch0aN2vyW5MoX0cYMAefu6Sy8YSXfxlVpECJIheAf2i3t89jVImJooAddlrWE0aRH/4eW2IIYaSDoNl7ag3Yqhg+MW5+vYxv+CdWiPLnOWRcKcGDj+JacCAKTOZR6O+fI38OmdnlEuSYvVkPBmFFgRpVBZAxwDuUhRNoW3gFK7cs7YYZsJGBF8XfBhnrvJpJvFtSRRVl/3ScCqdZozSE+tCpO8hsi07wpOMfzWAyq46aGvtGkQ6rfgLZhsCCIK8g9wgsDGoqAIfPXygy4RuRdRw= Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b5-20020a170902650500b001da153682c5sm888693plk.261.2024.02.12.15.36.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 15:36:34 -0800 (PST) From: Charlie Jenkins Date: Mon, 12 Feb 2024 15:36:28 -0800 Subject: [PATCH v11 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 Message-Id: <20240212-fencei-v11-3-e1327f25fe10@rivosinc.com> References: <20240212-fencei-v11-0-e1327f25fe10@rivosinc.com> In-Reply-To: <20240212-fencei-v11-0-e1327f25fe10@rivosinc.com> To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Jonathan Corbet , Conor Dooley , =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , Atish Patra , Randy Dunlap , Alexandre Ghiti 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=1707780989; l=4601; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=h5IjKPzf39ifTQj2Z0xAqATqlmjtpC61zariLMJKI0A=; b=vJDezW43wOShNn6FckmWv22EMLkCD0nM7x+UHVRHI3N8+Z+FmM5YXkveip/sA8wovq7Ws6bKO BP+ZSS/Sc9LDs5LKYhUoWvOeHEH9nKsMNE7+MESOdiw9PK7Yb73MHqV X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790738230598018894 X-GMAIL-MSGID: 1790738230598018894 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 + +============================================================================== +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(); + // Call prctl after final fence.i is called in process + prctl(PR_RISCV_SET_ICACHE_FLUSH_CTX_OFF, PR_RISCV_CTX_SW_FENCEI, PR_RISCV_SCOPE_PER_PROCESS); + + 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