From patchwork Mon Feb 12 23:36:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 13554166 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F1F80C4829D for ; Mon, 12 Feb 2024 23:36:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=YHs98TkCUJhCGkuMrWJCzGACI1XLcJYPAZO1i0ZDOL0=; b=XmbEU9jz9xlwvS vjKW0JIL1oTEmNcpNDA+IPeiQObsbdn8D/xTt2VbguHrO3gliVs8/w/F0JGe9TyV3uoOaevNGnUm2 CTewwMUZh9TtyGvBa4fOQIJkNHdZNR7wFh6gbuDXNd/9931MUYJB6sxpfaIr2EN1789UhoIVuGiDa NqIOmsE80+LvBPkT+VTmNtwYYXGUtjAuMS/O6FQkJqh1w+ejl7tnAxL3NiJ0Q0U3vqZLgrQI3zaWf 0LWRkfnZAbh+ZWz2mjECD9e+/HmucK73Ez20R83bsHoqwDv3WUgpUjZ1GjtFCltRaB493BMzADkHk OcqeI2m5M8sh33uEXteQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZfqm-00000007IYZ-3csP; Mon, 12 Feb 2024 23:36:36 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZfqj-00000007IVk-0ssD for linux-riscv@lists.infradead.org; Mon, 12 Feb 2024 23:36:35 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1d932f6ccfaso25855875ad.1 for ; Mon, 12 Feb 2024 15:36:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1707780992; x=1708385792; darn=lists.infradead.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=Olje9xypbAZ3WrcqD8VBFVJOFseKRGh8fXym/RnF5g8=; b=JGZfXw55kE2giJ0UjkzUVNcGuYyvjzoDDklsy4tHFDGn1jLsbtW4sl8CTPcbg8gaCl onFhKpMnYS9TtnzrM6shduymAmb0crVUPVngXXWVtIEK6BAS7UgLVdlR/Zm9oQYRRd/1 +04fyCjZMf+t4SAJMaCVNZ/tI4Ow0F2J4l77rXK+diyViFYuCH0DUa0iA/YilAJmhV/j Trp6fvXf85IyHPvrFRSnCSkP4ykCYyStQta7g3wRKfrBuPpWEQUH5tuI+IdxfYNOF4qO 2n0hmK080KKOvISMP3Xx6FYihPRgvKJxGogJBG8ZGyoBkc3WHlLT1bivqHtrTgIOXPMV hQZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707780992; x=1708385792; 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=Olje9xypbAZ3WrcqD8VBFVJOFseKRGh8fXym/RnF5g8=; b=vW+NnjljgAkEYK4eE+mjwuGQFGWrZsYAoxVv6OYkQ+Z4TBsoMb0grAxCdnZ/pWllak AOfeqKM2oez9Ylb/smaGC0h/wEuE9fhWaN+4HOo0C8LhkIQjHuimqi5Fyw/Db6dWaKOL mzL0JRMThtmh7uSaVhdd02ypcIt3JxLlEJuRHIFMuhwApbA/jSuED+sFXoxF4uGbLEMy dKYb2btDzi7arCkZ/Kv7GcRH134uIQBdZK8kmE9eZET6BcggpeUXd11/DKcI/s9yPH4J uzRBwFB1TVNDA9fbjcDAv8QvVht7ROhrb2hu2TNqQuWjoXJq660P83ses7zSWeO6bCAu VDAA== X-Gm-Message-State: AOJu0YxFi1xLicXKxcfoSAlVhap0LqpSLJEDrwlN2JZBLrki3b6+Kie9 UjisWKe0TjvfRvqOPI2DdO7JT7G0zaYJr9rSI3jHvGMdlhI1v56uJl2Cku7RvN0= X-Google-Smtp-Source: AGHT+IGOdL3DrCigpJSkV5Hyr5gHXGMCKTbmU/aGTha2J+6odxlz/IBG86EKAkZRn2/3Gp0s/00GPw== X-Received: by 2002:a17:903:110f:b0:1d8:a93f:a5b2 with SMTP id n15-20020a170903110f00b001d8a93fa5b2mr8982004plh.12.1707780992600; Mon, 12 Feb 2024 15:36:32 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCX9IHTBBsAU+LBcf9fWTCl49GRnlyxiyibnmfNmj/O7/h+tbt6+9IJmYjNakft2CXqPJE2jvAw7F08375+jC2mD8UEKwPVTypk5fDmJjKI4eySgbBuL41RNQiPmVh/G39Mt/x9gU7kceMUF8weph87jKzxgRSZYh2uoc0/jU3QHfpFOt++Vig/JOI89YyFKpz9KzEiw5gqb52lYt9ZyFN1i9QwIao8p3HsGo7mlb0dGMvYrmwUGIh7WwIBRywBXQ81Dsu7c6/iZGoPu/ACXbxByF8/LQ5BzCqKpUcLiixH9vZw4FNFoWJ0s74FhJty9ej228GQJZAzOn8FA1/UJvVNtlr65vPY33AkHuEDcnKBD5Ygcf073mQheXvc3jmkAooEpSecmNLA= 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.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 15:36:32 -0800 (PST) From: Charlie Jenkins Date: Mon, 12 Feb 2024 15:36:26 -0800 Subject: [PATCH v11 1/4] riscv: Remove unnecessary irqflags processor.h include MIME-Version: 1.0 Message-Id: <20240212-fencei-v11-1-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 X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707780989; l=639; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=23qauOezLdGd/qvi+eoGfAUmo21GdLnF/O1XuhUSBhA=; b=3fk1hPn8uk0LHRB8yl3Y0RYndhVQhDbBlNi5Ce1BSrXXKhsMTW4JUlwl8LFpuZbLYzru9RLEH y8tsCF/wTQcAF+CZe6nMI0QqK6iZZlmz8RM55ve7LYSJeVzXo12jwfv X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240212_153633_379441_90A4015E X-CRM114-Status: UNSURE ( 8.80 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This include is not used. Remove it to avoid a circular dependency in the next patch in the series. Signed-off-by: Charlie Jenkins Reviewed-by: Samuel Holland --- arch/riscv/include/asm/irqflags.h | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/riscv/include/asm/irqflags.h b/arch/riscv/include/asm/irqflags.h index 08d4d6a5b7e9..6fd8cbfcfcc7 100644 --- a/arch/riscv/include/asm/irqflags.h +++ b/arch/riscv/include/asm/irqflags.h @@ -7,7 +7,6 @@ #ifndef _ASM_RISCV_IRQFLAGS_H #define _ASM_RISCV_IRQFLAGS_H -#include #include /* read interrupt enabled status */ From patchwork Mon Feb 12 23:36:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 13554165 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 94A61C48BC4 for ; Mon, 12 Feb 2024 23:36:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=kFOaufIgd6uqQnQTNKVdAkvyNc9254UBqlsZp+hj/PA=; b=G9F7sxDLsDZtA8 i55tU814l/AoQ3BPEqNLT+/n9o6r1lcGVmKmqTHD+s13AjUHCXVrJF4CaGG4rCNvYF4alea6IhNNO qLwboHrjZFh/pJ+LkrDQemMkM/Hhp5Qmotx44KbTHZ6OA9tSff6Zxsqpkc/BlODf9e+oQ+K0rVBDx Kix8zQmmh0P/8HrwoE7oIFAlPRemZ3Nitpu8Ym3/js/V0JF+hb7HwlShb5DgpJDFrRei8AL0r7YMS Stsl9vEDqh7yHIRf/BK1oFrvSSvo6vNDTWhkKd8XRJPZzoyQu51P/Mj+picexpJNlYjan7u2XDdWJ orycnP4W+s1uJFWsqkOw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZfqq-00000007Ib2-1Nk7; Mon, 12 Feb 2024 23:36:40 +0000 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZfql-00000007IX4-0VCD for linux-riscv@lists.infradead.org; Mon, 12 Feb 2024 23:36:36 +0000 Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-5c6bd3100fcso2292883a12.3 for ; Mon, 12 Feb 2024 15:36:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1707780994; x=1708385794; darn=lists.infradead.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=dASIWM+uTiyyOMlmBksRTVw3Jl25BD2O8RxyTvNPqk4=; b=r9CHeTvAcXin5TudaCCQxu9vlDrkjLdD79art6QJHJDiVT696bWV2HImZS/WtliXun 72FDiwkwYv2t7C1KEl9SxQLjBTqLNNbtjOTHvKA61zXFx3c4U1BhlPFaSZlRblhe+9Gz 0MkiW6DjO++c0F7KHOtrInViE+OcHmgqZRT+OPpxnud6BghSJ4rV/ehWBpbLKDaHesFy pQLr+mWdM0BDeEh5VWN2VOzOKG6Dm7nkl9s4F2/syKv2crpP7szw6Rn65YdIgoTIWFoK xz1TtxRdq2nAWtlNcHiZ83b1Vnxg25vbBLlvExt71wSkyoYhWJyz9mzQCKLU4nahE4AY jlkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707780994; x=1708385794; 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=dASIWM+uTiyyOMlmBksRTVw3Jl25BD2O8RxyTvNPqk4=; b=rXaXKzcv0VlVds6i6RciRH14Y9L+C0xTx9Aytae49pJRFdlTgYEtNcK4vNzN/0uiNl 7mQvmSnhKVwgvtOhxnZB7Qb03/ZCctHHF2bj6d525V6ILJYzpLYhmD2DtyOZNmpX8w6p kjwNK8rs/rrYcyAvzscYFhga4NdlI2WfvKwUXwTdeEoan2FfKQm7yDooTtA75BISomc7 fOUeQJgr7q6vquDtY7lQ5BUqlKUSQmpHFC1NKlzxVSzz92ZBcvzO/asVuhUEOJDZeWXk od929oWpZIMAwCBoPVevD+w+zEzD3Zvx+7+AP9ZMfLUxkVDhS2pOikPcsEmmctQxPVwv mFSQ== X-Gm-Message-State: AOJu0YzSZHZBajW3VHHxS2zxyARu+2AXJn3xTq1sOk/UyZlSx/Moxcs8 auZuuXQ0x9zMNGotGFHkWO8rTCc7SE8rVPNO8by/RLNLdj/IyL6qLIPYPvi3cYQ= X-Google-Smtp-Source: AGHT+IGWoz642JfrMWAzBx9RQr9NFENL/+5QyCyIvCKo/k8pNFxdTik3rGrNsdh2pg+67CNIq6Gmeg== X-Received: by 2002:a05:6a20:c90d:b0:1a0:566a:5974 with SMTP id gx13-20020a056a20c90d00b001a0566a5974mr3532721pzb.61.1707780994003; Mon, 12 Feb 2024 15:36:34 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWJE5WRyf6IA0AR7of4b+3XD0bB7/QSUiLZkw0KbtMo1jNqX0pxK0HqTzs5wgC3fGGxC7Ktft53eGVFxbTtwfAT3RRhtNxCaRVMi3BTWsSNNBb4vNsalHcOJwgbWoGz3UPdeZrlQjcxewKCU0eHsxDJ5Sc5k5UA5OdGOONC61YQGdbTnZgg7i0sWizzSVrtLRvYRek/VwnlTVYpyZ1HHDTtcuuOdgo8jMBwDMLByAShZSQM+qzcGF7BjwhzOezlIVfEktA7dTyTzTZdynBUysDf5heHqfKFRjEhXN2aezMKX5i7avm5s3jW/Xzv20p5bT944Z6o2O7GgH7Nf+xmJ1xwzoo/Wh/hFFoMPfBbbSBQe7FbfyxrTLeH7L9fajeNbSFwLRPzt7dxwEm0QXU9HET9QUACTnR9bhVppo2GWQqjhNl7ARHMy1RFH5cVp8S8HyJXBtc= 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.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 15:36:33 -0800 (PST) From: Charlie Jenkins Date: Mon, 12 Feb 2024 15:36:27 -0800 Subject: [PATCH v11 2/4] riscv: Include riscv_set_icache_flush_ctx prctl MIME-Version: 1.0 Message-Id: <20240212-fencei-v11-2-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=11568; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=TKCt+8IZ9ruFBnTgfJxRkDydxZvQ/nlPrN0EVMID0+A=; b=mXok/mb98DSDzfQSJsMFWzjeOw67T47xCMxZXKsQOL+eBtqj6VQDjbSmupvqXHeKGrQt9DCXz RTyTXGZNotGBvzga7ax/Xk2AcIKACM5dP0xS8ZpYk4cSQsMp40CZquV X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240212_153635_418184_86C2E44B X-CRM114-Status: GOOD ( 26.11 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Support new prctl with key PR_RISCV_SET_ICACHE_FLUSH_CTX to enable optimization of cross modifying code. This prctl enables userspace code to use icache flushing instructions such as fence.i with the guarantee that the icache will continue to be clean after thread migration. Signed-off-by: Charlie Jenkins Reviewed-by: Atish Patra Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/mmu.h | 2 + arch/riscv/include/asm/processor.h | 12 +++++ arch/riscv/include/asm/switch_to.h | 23 ++++++++ arch/riscv/mm/cacheflush.c | 105 +++++++++++++++++++++++++++++++++++++ arch/riscv/mm/context.c | 18 +++++-- include/uapi/linux/prctl.h | 6 +++ kernel/sys.c | 6 +++ 7 files changed, 167 insertions(+), 5 deletions(-) diff --git a/arch/riscv/include/asm/mmu.h b/arch/riscv/include/asm/mmu.h index 355504b37f8e..60be458e94da 100644 --- a/arch/riscv/include/asm/mmu.h +++ b/arch/riscv/include/asm/mmu.h @@ -19,6 +19,8 @@ typedef struct { #ifdef CONFIG_SMP /* A local icache flush is needed before user execution can resume. */ cpumask_t icache_stale_mask; + /* Force local icache flush on all migrations. */ + bool force_icache_flush; #endif #ifdef CONFIG_BINFMT_ELF_FDPIC unsigned long exec_fdpic_loadmap; diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index a8509cc31ab2..46c5c3b91165 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -69,6 +69,7 @@ #endif #ifndef __ASSEMBLY__ +#include struct task_struct; struct pt_regs; @@ -123,6 +124,14 @@ struct thread_struct { struct __riscv_v_ext_state vstate; unsigned long align_ctl; struct __riscv_v_ext_state kernel_vstate; +#ifdef CONFIG_SMP + /* A local icache flush is needed before user execution can resume on one of these cpus. */ + cpumask_t icache_stale_mask; + /* Regardless of the icache_stale_mask, flush the icache on migration */ + bool force_icache_flush; + /* A forced icache flush is not needed if migrating to the previous cpu. */ + unsigned int prev_cpu; +#endif }; /* Whitelist the fstate from the task_struct for hardened usercopy */ @@ -184,6 +193,9 @@ extern int set_unalign_ctl(struct task_struct *tsk, unsigned int val); #define GET_UNALIGN_CTL(tsk, addr) get_unalign_ctl((tsk), (addr)) #define SET_UNALIGN_CTL(tsk, val) set_unalign_ctl((tsk), (val)) +#define RISCV_SET_ICACHE_FLUSH_CTX(arg1, arg2) riscv_set_icache_flush_ctx(arg1, arg2) +extern int riscv_set_icache_flush_ctx(unsigned long ctx, unsigned long per_thread); + #endif /* __ASSEMBLY__ */ #endif /* _ASM_RISCV_PROCESSOR_H */ diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 7efdb0584d47..7594df37cc9f 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -72,14 +73,36 @@ static __always_inline bool has_fpu(void) { return false; } extern struct task_struct *__switch_to(struct task_struct *, struct task_struct *); +static inline bool switch_to_should_flush_icache(struct task_struct *task) +{ +#ifdef CONFIG_SMP + bool stale_mm = task->mm && task->mm->context.force_icache_flush; + bool stale_thread = task->thread.force_icache_flush; + bool thread_migrated = smp_processor_id() != task->thread.prev_cpu; + + return thread_migrated && (stale_mm || stale_thread); +#else + return false; +#endif +} + +#ifdef CONFIG_SMP +#define __set_prev_cpu(thread) ((thread).prev_cpu = smp_processor_id()) +#else +#define __set_prev_cpu(thread) +#endif + #define switch_to(prev, next, last) \ do { \ struct task_struct *__prev = (prev); \ struct task_struct *__next = (next); \ + __set_prev_cpu(__prev->thread); \ if (has_fpu()) \ __switch_to_fpu(__prev, __next); \ if (has_vector()) \ __switch_to_vector(__prev, __next); \ + if (switch_to_should_flush_icache(__next)) \ + local_flush_icache_all(); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c index 55a34f2020a8..6513a0ab8655 100644 --- a/arch/riscv/mm/cacheflush.c +++ b/arch/riscv/mm/cacheflush.c @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -152,3 +153,107 @@ void __init riscv_init_cbo_blocksizes(void) if (cboz_block_size) riscv_cboz_block_size = cboz_block_size; } + +/** + * riscv_set_icache_flush_ctx() - Enable/disable icache flushing instructions in + * userspace. + * @ctx: Set the type of icache flushing instructions permitted/prohibited in + * userspace. Supported values described below. + * + * Supported values for ctx: + * + * * %PR_RISCV_CTX_SW_FENCEI_ON: Allow fence.i in user space. + * + * * %PR_RISCV_CTX_SW_FENCEI_OFF: Disallow fence.i in user space. All threads in + * a process will be affected when ``scope == PR_RISCV_SCOPE_PER_PROCESS``. + * Therefore, caution must be taken; use this flag only when you can guarantee + * that no thread in the process will emit fence.i from this point onward. + * + * @scope: Set scope of where icache flushing instructions are allowed to be + * emitted. Supported values described below. + * + * Supported values for scope: + * + * * %PR_RISCV_SCOPE_PER_PROCESS: Ensure the icache of any thread in this process + * is coherent with instruction storage upon + * migration. + * + * * %PR_RISCV_SCOPE_PER_THREAD: Ensure the icache of the current thread is + * coherent with instruction storage upon + * migration. + * + * When ``scope == PR_RISCV_SCOPE_PER_PROCESS``, all threads in the process are + * permitted to emit icache flushing instructions. Whenever any thread in the + * process is migrated, the corresponding hart's icache will be guaranteed to be + * consistent with instruction storage. This does not enforce any guarantees + * outside of migration. If a thread modifies an instruction that another thread + * may attempt to execute, the other thread must still emit an icache flushing + * instruction before attempting to execute the potentially modified + * instruction. This must be performed by the user-space program. + * + * In per-thread context (eg. ``scope == PR_RISCV_SCOPE_PER_THREAD``) only the + * thread calling this function is permitted to emit icache flushing + * instructions. When the thread is migrated, the corresponding hart's icache + * will be guaranteed to be consistent with instruction storage. + * + * On kernels configured without SMP, this function is a nop as migrations + * across harts will not occur. + */ +int riscv_set_icache_flush_ctx(unsigned long ctx, unsigned long scope) +{ +#ifdef CONFIG_SMP + switch (ctx) { + case PR_RISCV_CTX_SW_FENCEI_ON: + switch (scope) { + case PR_RISCV_SCOPE_PER_PROCESS: + current->mm->context.force_icache_flush = true; + break; + case PR_RISCV_SCOPE_PER_THREAD: + current->thread.force_icache_flush = true; + break; + default: + return -EINVAL; + } + break; + case PR_RISCV_CTX_SW_FENCEI_OFF: + cpumask_t *mask; + + switch (scope) { + case PR_RISCV_SCOPE_PER_PROCESS: + bool stale_cpu; + + current->mm->context.force_icache_flush = false; + + /* + * Mark every other hart's icache as needing a flush for + * this MM. Maintain the previous value of the current + * cpu to handle the case when this function is called + * concurrently on different harts. + */ + mask = ¤t->mm->context.icache_stale_mask; + stale_cpu = cpumask_test_cpu(smp_processor_id(), mask); + + cpumask_setall(mask); + assign_bit(cpumask_check(smp_processor_id()), cpumask_bits(mask), stale_cpu); + break; + case PR_RISCV_SCOPE_PER_THREAD: + current->thread.force_icache_flush = false; + + /* + * Mark every other hart's icache as needing a flush for + * this thread. + */ + mask = ¤t->thread.icache_stale_mask; + cpumask_setall(mask); + cpumask_clear_cpu(smp_processor_id(), mask); + break; + default: + return -EINVAL; + } + break; + default: + return -EINVAL; + } +#endif + return 0; +} diff --git a/arch/riscv/mm/context.c b/arch/riscv/mm/context.c index 217fd4de6134..2eb13b89cced 100644 --- a/arch/riscv/mm/context.c +++ b/arch/riscv/mm/context.c @@ -15,6 +15,7 @@ #include #include #include +#include #ifdef CONFIG_MMU @@ -297,21 +298,28 @@ static inline void set_mm(struct mm_struct *prev, * * The "cpu" argument must be the current local CPU number. */ -static inline void flush_icache_deferred(struct mm_struct *mm, unsigned int cpu) +static inline void flush_icache_deferred(struct mm_struct *mm, unsigned int cpu, + struct task_struct *task) { #ifdef CONFIG_SMP cpumask_t *mask = &mm->context.icache_stale_mask; - if (cpumask_test_cpu(cpu, mask)) { + if (cpumask_test_and_clear_cpu(cpu, mask) || + (task && cpumask_test_and_clear_cpu(cpu, &task->thread.icache_stale_mask))) { cpumask_clear_cpu(cpu, mask); + /* * Ensure the remote hart's writes are visible to this hart. * This pairs with a barrier in flush_icache_mm. */ smp_mb(); - local_flush_icache_all(); - } + /* + * If cache will be flushed in switch_to, no need to flush here. + */ + if (!(task && switch_to_should_flush_icache(task))) + local_flush_icache_all(); + } #endif } @@ -332,5 +340,5 @@ void switch_mm(struct mm_struct *prev, struct mm_struct *next, set_mm(prev, next, cpu); - flush_icache_deferred(next, cpu); + flush_icache_deferred(next, cpu, task); } diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 370ed14b1ae0..524d546d697b 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -306,4 +306,10 @@ struct prctl_mm_map { # define PR_RISCV_V_VSTATE_CTRL_NEXT_MASK 0xc # define PR_RISCV_V_VSTATE_CTRL_MASK 0x1f +#define PR_RISCV_SET_ICACHE_FLUSH_CTX 71 +# define PR_RISCV_CTX_SW_FENCEI_ON 0 +# define PR_RISCV_CTX_SW_FENCEI_OFF 1 +# define PR_RISCV_SCOPE_PER_PROCESS 0 +# define PR_RISCV_SCOPE_PER_THREAD 1 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index e219fcfa112d..69afdd8b430f 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -146,6 +146,9 @@ #ifndef RISCV_V_GET_CONTROL # define RISCV_V_GET_CONTROL() (-EINVAL) #endif +#ifndef RISCV_SET_ICACHE_FLUSH_CTX +# define RISCV_SET_ICACHE_FLUSH_CTX(a, b) (-EINVAL) +#endif /* * this is where the system-wide overflow UID and GID are defined, for @@ -2743,6 +2746,9 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_RISCV_V_GET_CONTROL: error = RISCV_V_GET_CONTROL(); break; + case PR_RISCV_SET_ICACHE_FLUSH_CTX: + error = RISCV_SET_ICACHE_FLUSH_CTX(arg2, arg3); + break; default: error = -EINVAL; break; 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: 13554162 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 33520C4829E for ; Mon, 12 Feb 2024 23:36:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ftWtoCtHrTVxf6+EBlI9XbZUYlWiAtGkBtBflkTa4Xs=; b=lb1JR5BCTCmuDF c+PQwhpKqV1uiM4Y99JiZqz0BmepJJk/sN65ht281N8lotPesrOcw4M0YxGkFRc9a0ePTXNCTfDrs UwDoa/WdDaDpGgRAQOMziidPW35WGn0sCTmTaItS4RSK1+gpGRHlLfWHYfguCJ3uhQVmaj3El1zJj 6w+znNiyAFgD5yrFzaMhHIk2b391vJQOPyrK9vQDu3OjnprDRAmWyVOy5O/cYcPlNCJrbYq7+lWM2 QFnKjsiLNRmGQzfvmhtIkC+hnkD7SokJGhWADfvhPyGZ52uihwojVtLPXsSbG0f0xDhak0kc84pkw lb4rAP6FKoS8Evt2jE3A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZfqr-00000007IbY-0Nyj; Mon, 12 Feb 2024 23:36:41 +0000 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZfqm-00000007IYK-1qpV for linux-riscv@lists.infradead.org; Mon, 12 Feb 2024 23:36:38 +0000 Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-296a79e6295so2670432a91.3 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=lists.infradead.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=ojb2KjTlmZHU90hXSMp3P2LIkiQfgcZOSl1ON/4WDdz2WGRLlNnWQjqcrdilukbuyg bRvbDFK4POMy4kgpKg9RQmHtZa4ENhxAhozdAgWk5czCMQtO76n3DBHnKRQWRAWmzYWg h+CKJti3ocbsa6+fqDswa7ELEM5zpkqZ7nbdQ/aBCLbdAk6EJUnJ9nEm92s3YPWNfRaD n+/S/B/6RPKoxKF46F+z+epoe0f/5lhMqvGmZxj5bobAcTGpqz23dHEMoOiU6KE2LAlT JZtJUsa+MxVfg5JMR0amwPpo+obzzzxbki/NQESOO3jbBHVmMTgIj2e0RrE+ubK39EXB UBFw== 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=XLFtn3tqFhyTgbnUrWI8iBdL9ogiLVOZrhbKKertbIK1gvR7RMU/JTODFg1TYD/Cs/ TwyflvSNrtKchSQ1CF6CHKJQwJkfJIBB8HI2wIb1IKPflRy54K6SiKK6H5X5O92xSDOW w2zoU678GgVfVswUWUKx6agfOswRftZfvmoymanJZZVz4T9Zne035yrBu2JsWknuHagq ew20IP3qfoqTQPCY8nv6fJppRtgz5QcNexFflh/6RcmqtTYNTngsI54I0UXQGP6IBjGY paTJ970bvFa/ZpWzt1WQTcQoV//0Seby8fmPqn2BPzAs1vOyLM3EEMcJugVcamrd9BRP lEMw== X-Gm-Message-State: AOJu0YzMZwd+WbuMpAoLa5SsUnUikLrk9tOxsW+C2Wnt+2qUyoKnlUnB 8wXVLfKr/u0+5BTBtAMWGrZe5pQ5o6j9rV5RS5L/KUww8O9dyeiaM3T7n3WvezE= X-Google-Smtp-Source: AGHT+IEy/UYemr39prYxAVOYTN6/oNrnyC8e1djNHt7JTfEtK270DmcLzYDcyfo+8s8sJcI5kCZL/w== 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 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240212_153636_574925_7326788E X-CRM114-Status: GOOD ( 20.05 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 From patchwork Mon Feb 12 23:36:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 13554164 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 723C7C4829F for ; Mon, 12 Feb 2024 23:36:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=eLaGKEnMS428tArkQ7Es2mdtkF09MI9Ttqy7QPx7Q/0=; b=b0Su/qHU1V0qex FNdm9kK36Xy/onNqU+01LiVLj5Ryc0o1VITsCZhJtTTobFwbWNLwQv8fpPLMx571YXripaME9HiqR h5NN9ovmMDxK8HdgxypJ7RZoAJxmgJ6VSX15j8YwPAes82dvBoAMyeN1zRvuuu5V4jrHKdlqaHgcS RB+z2kIdEK3EZ3PK3BZNssi/zqK1WFx0XP0stzRhaNOUW7Kku9d51UbynbpgLdk2imGK3vyjBIyLb wAo2CBbgbmYsMILGYHQGg3DYWqI09W1EuGPDWEw6PCnwDGjy4oenrWmXq8G8KVARYBCGPUsMv+vpq EMVXCMoYG0DpjsjpzhDg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZfqs-00000007IcC-0NOb; Mon, 12 Feb 2024 23:36:42 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZfqn-00000007IZ1-1zem for linux-riscv@lists.infradead.org; Mon, 12 Feb 2024 23:36:39 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1d7881b1843so34242885ad.3 for ; Mon, 12 Feb 2024 15:36:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1707780996; x=1708385796; darn=lists.infradead.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=XqFsnYbayaROauH/yeBk3F9rNPdoEfeRLt6dO0Xr65g=; b=BUe+b04E9xhC6Zu/iFsBW6l+PTF858M9dF1/L6UMwf44jTnBVfxc26YeaitI5JP4C3 yIcd7mefXSo3b8+MuVCHArFog4Zm0kKXN8k5G2Zn2yFO9l9LdDXkqMNarPLi0rsdfVmN MRoZSliwc5hiUDuOIpaVcz3jGEbSDCoW0DAaRubmRBwAOs3b9Zzhyug8XioRdXbBBd8O /YvYxIuhwqQd6p1cod9Z/4iVLh1owKxFRPwGhGxdfHYR8wlVtisHOTBuyq4BgDlxvAUl z3EX0GsfBuhz+HEUXbJ/3k1OLqy7NIaupb749AHz92deAkCj5YIDowb7s7FZUimDcuii p9OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707780996; x=1708385796; 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=XqFsnYbayaROauH/yeBk3F9rNPdoEfeRLt6dO0Xr65g=; b=e06g3s92PwIqxX7jU/Sv1r1QSMpzGDnIoOLKHTONwBkwrLdxdYN5aF3DBZjUW9zX+j bBGj4sCRkkbVCpuMLm5LKwCX8W1EGbtsmR8AGEu7qWWHNXqnP/BR4ZsF2XYjFtfg1zoK vDv6a+7Qv80z2FTjTdoF8JaZgrYVj7K4RNhVe+F12QpteNSrRHN+ktl0uVz6R0AKw0ke 8jDFNNtSpsk3VzeynaX1jmlzLq4ivwo/ORmnNO1/flyVz+hvynuw/QOLdB6ac28vIdRH NcI+Maaxrqr2jrNOUT5cI7FxfgXR41zs8Ui8IPBkiicmleWCZViImHgTlHa1hvOo7VXb IZ8w== X-Gm-Message-State: AOJu0YzUtd/cxr5g0/0TaNL9sI81rdpKHGdshQSCQsVnzrdS9nlsLQpP SXAAU1HJLDFE14exu0g4XhTac73m7+RA9nQ7xxyh3XzM8eY1YiXo7ZhtFXSj+BI= X-Google-Smtp-Source: AGHT+IEtzelG1aBj8MhHUkWpgbenml+rvNSXi4YOjQkaIXkajpKLpyf9eRWHPskzRMV/SZLXbDR4ug== X-Received: by 2002:a17:902:ecc1:b0:1d7:b1f:be9e with SMTP id a1-20020a170902ecc100b001d70b1fbe9emr9547643plh.66.1707780996763; Mon, 12 Feb 2024 15:36:36 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXmhahLkBycaRs+zzoTPL0pP1/Wbhljlk09mGvZl2U3BYlRz5afgzfA4K6oqUcu0CEflM4zj5+TqIePAEv1u9fURwIXrlf9AzC8VlXIv3fVP2DyI2ElWKkAeanWCs3jfOGB55CzBhMOKNK7t91AFuCyfpr4hG54nAJds3f4sSV/prcfAWmcrnbBMVlm49hPtTlMiLw3bu4G8rxu4OuobxGyLSqhOg50X9uwZDi/69PwQrtG4GAJTMhS1bmhY8NjeEdYt7zKMUFhPBN5NmfRWbF3Ci7F2uELrXj92QbZKRN/KTacsTyfNLYjc/k9bL7lSTdk+FXPauBA/9p9ZxViYmvh+WCSf4bb33CHZnad4402Ud3kDU2e9RlKDluUzWsPPLzK4MwXbl4= 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.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 15:36:36 -0800 (PST) From: Charlie Jenkins Date: Mon, 12 Feb 2024 15:36:29 -0800 Subject: [PATCH v11 4/4] cpumask: Add assign cpu MIME-Version: 1.0 Message-Id: <20240212-fencei-v11-4-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 X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1707780989; l=1797; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=teNltxEe5ATnvQPKeLgPp6b3k0t1rOkLmnDUUB6OJdA=; b=EiKlcL1ulk4DvUiCSUgvbFtR389U08UGs9J+Mx1NjicMNyA7ytjbg4bgirWaWZZtdE5HGgwHU EVUxZnpx4C/A+JHiRo45OTnP3oYSGtoMtT2ZFchA7WKDMysLkz7sWPx X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240212_153637_652394_615919FB X-CRM114-Status: UNSURE ( 9.74 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Standardize an assign_cpu function for cpumasks. Signed-off-by: Charlie Jenkins --- arch/riscv/mm/cacheflush.c | 2 +- include/linux/cpumask.h | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c index 6513a0ab8655..d10c2cba8aff 100644 --- a/arch/riscv/mm/cacheflush.c +++ b/arch/riscv/mm/cacheflush.c @@ -234,7 +234,7 @@ int riscv_set_icache_flush_ctx(unsigned long ctx, unsigned long scope) stale_cpu = cpumask_test_cpu(smp_processor_id(), mask); cpumask_setall(mask); - assign_bit(cpumask_check(smp_processor_id()), cpumask_bits(mask), stale_cpu); + cpumask_assign_cpu(smp_processor_id(), mask, stale_cpu); break; case PR_RISCV_SCOPE_PER_THREAD: current->thread.force_icache_flush = false; diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index cfb545841a2c..1b85e09c4ba5 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -492,6 +492,22 @@ static __always_inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp) __clear_bit(cpumask_check(cpu), cpumask_bits(dstp)); } +/** + * cpumask_assign_cpu - assign a cpu in a cpumask + * @cpu: cpu number (< nr_cpu_ids) + * @dstp: the cpumask pointer + * @bool: the value to assign + */ +static __always_inline void cpumask_assign_cpu(int cpu, struct cpumask *dstp, bool value) +{ + assign_bit(cpumask_check(cpu), cpumask_bits(dstp), value); +} + +static __always_inline void __cpumask_assign_cpu(int cpu, struct cpumask *dstp, bool value) +{ + __assign_bit(cpumask_check(cpu), cpumask_bits(dstp), value); +} + /** * cpumask_test_cpu - test for a cpu in a cpumask * @cpu: cpu number (< nr_cpu_ids)