From patchwork Wed Jan 31 14:49:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 13539530 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 08178C47DB3 for ; Wed, 31 Jan 2024 14:50:21 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=pVLhTWG4VeiCzl1uh3XYZXmDmmwUfko3kluPFHYxoX4=; b=37kpp9TS6JVJva /mRjoFqzrrIBmWm6gayREwjtjLNyNApyNanc7++6SpI/sgmQV5rRLd+5NERjo0Nq1d2MdpwtrOb2x 1lLN6XTCD20eLT6odnb9t+dJr+YgUPv6uayLU5fEyrtU1Cq4QESURarOrSomV/5JY8htcxaZ1cou+ gUDPvts8rzpeYrA0WZccDaCwzS09YsfM04T6Yw/zHNHkvu0TgS/wIZgmVp1cLDF6BC7HlPdq9kR/Z Ce3UjFRTTq/dLi3HyY78oFeLBwNMsUpm0QuNBWzydLr0Et7MOhvjOZb8DrHq4wy6powNd2tbKDBJ/ Qeq/3aHtBVS4V5kkcAnQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rVBus-000000040AL-1cPm; Wed, 31 Jan 2024 14:50:18 +0000 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rVBum-0000000405U-2Zf6 for linux-riscv@lists.infradead.org; Wed, 31 Jan 2024 14:50:14 +0000 Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-3394b892691so569133f8f.1 for ; Wed, 31 Jan 2024 06:50:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706712610; x=1707317410; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QVYIfgnNZXAnAGrFV+qVvJtPQ32MBOSOxj+SNVTy3v0=; b=dnR5hAptOxkB7f+PWnnYidHgVVTRtKNDsVzase4VT86Y+fG/WjfkqMQ6KFDyh9UvAH N7PlmeAp2rQbQNdPd9m/j9jj+UXFTu5VI7HniAWeG5ScC1nQSUTIVAcRyZK1VOhKuLp4 +M0rOiyhyuRqJVP1Hr7vfQErzGhFpddNWkG99EBtRf9VSxUGlUS6ZTVQ5596WcSWi1xb AEKMi/BqJghEBwfqmr+8s8bHXhU/MfH5FNntZdR+zeV0HI+Z9COX20wFvDD8ooxNncRK 8CLazuvcBzMytjVHStu2KLA+GYrPWb8SVMtDjLzL06NgtC6730KHcJdTRwMhY5Th0QOp OBMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706712610; x=1707317410; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QVYIfgnNZXAnAGrFV+qVvJtPQ32MBOSOxj+SNVTy3v0=; b=StS03//QiAHiKombCEBUDn6kGZCwRTsa/fM60Qw9mkqdYNHvED2PUDiWg9OooeVRwn oHqXmWTAlXX1+KEvD1NHGnBKc2z+ibGxUqUkfa2eQBO0BYOsmRecSPWmO1AGeMTEmjr+ wVt1D9fGJ2eVw0WazrRf4mdIpqulg5zB2X43xxGJx5FwkTPWqntGJLjGLcoIML4fiwlj 8zXT7K7/EdoUH70VigehsGHzO/3z+iKuD0/wMt5OOz6wJ/kBRbqOLKrzRMSSpm7iqbZs zGyYsp4zzooTdR0HnNgXzlmyqVE58KUDcgGLxyoL3GqdJS6nGBCeRGvjxqAVuBfVgDwy 1qdw== X-Gm-Message-State: AOJu0YxUpt2KUY9SI50scwwFio7ck8QYP5AkQZPTPzfw8TjS66APVySJ ZGJHTr4QywyVH3qxyIhptrJ9kDEjj0UjgtzwKZHf+jQpWpp8/Sidyz7yRhFgx0X2LA== X-Google-Smtp-Source: AGHT+IGtN/fFFgD5kWlZM7o9A5tbWzODTz9z9HcCpoaArlTRJ10128W2icRy33OzgUwIaucVecHQQQ== X-Received: by 2002:a5d:68cd:0:b0:33a:fe3b:a048 with SMTP id p13-20020a5d68cd000000b0033afe3ba048mr1822841wrw.34.1706712609631; Wed, 31 Jan 2024 06:50:09 -0800 (PST) Received: from andrea.wind3.hub ([31.189.8.91]) by smtp.gmail.com with ESMTPSA id w6-20020a5d4046000000b0033b0d049c71sm419265wrp.54.2024.01.31.06.50.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 06:50:09 -0800 (PST) From: Andrea Parri To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, mathieu.desnoyers@efficios.com, paulmck@kernel.org, corbet@lwn.net Cc: mmaas@google.com, hboehm@google.com, striker@us.ibm.com, charlie@rivosinc.com, rehn@rivosinc.com, rdunlap@infradead.org, sorear@fastmail.com, linux-riscv@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Andrea Parri Subject: [PATCH v4 1/4] membarrier: riscv: Add full memory barrier in switch_mm() Date: Wed, 31 Jan 2024 15:49:33 +0100 Message-Id: <20240131144936.29190-2-parri.andrea@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240131144936.29190-1-parri.andrea@gmail.com> References: <20240131144936.29190-1-parri.andrea@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240131_065012_735764_A9321F93 X-CRM114-Status: GOOD ( 23.35 ) 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 The membarrier system call requires a full memory barrier after storing to rq->curr, before going back to user-space. The barrier is only needed when switching between processes: the barrier is implied by mmdrop() when switching from kernel to userspace, and it's not needed when switching from userspace to kernel. Rely on the feature/mechanism ARCH_HAS_MEMBARRIER_CALLBACKS and on the primitive membarrier_arch_switch_mm(), already adopted by the PowerPC architecture, to insert the required barrier. Fixes: fab957c11efe2f ("RISC-V: Atomic and Locking Code") Signed-off-by: Andrea Parri Reviewed-by: Mathieu Desnoyers --- MAINTAINERS | 2 +- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/membarrier.h | 31 +++++++++++++++++++++++++++++ arch/riscv/mm/context.c | 2 ++ kernel/sched/core.c | 5 +++-- 5 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 arch/riscv/include/asm/membarrier.h diff --git a/MAINTAINERS b/MAINTAINERS index 8999497011a26..d7ed83b73cbe4 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14040,7 +14040,7 @@ M: Mathieu Desnoyers M: "Paul E. McKenney" L: linux-kernel@vger.kernel.org S: Supported -F: arch/powerpc/include/asm/membarrier.h +F: arch/*/include/asm/membarrier.h F: include/uapi/linux/membarrier.h F: kernel/sched/membarrier.c diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index bffbd869a0682..087abf9e51c69 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -27,6 +27,7 @@ config RISCV select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_GIGANTIC_PAGE select ARCH_HAS_KCOV + select ARCH_HAS_MEMBARRIER_CALLBACKS select ARCH_HAS_MMIOWB select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE select ARCH_HAS_PMEM_API diff --git a/arch/riscv/include/asm/membarrier.h b/arch/riscv/include/asm/membarrier.h new file mode 100644 index 0000000000000..6c016ebb5020a --- /dev/null +++ b/arch/riscv/include/asm/membarrier.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASM_RISCV_MEMBARRIER_H +#define _ASM_RISCV_MEMBARRIER_H + +static inline void membarrier_arch_switch_mm(struct mm_struct *prev, + struct mm_struct *next, + struct task_struct *tsk) +{ + /* + * Only need the full barrier when switching between processes. + * Barrier when switching from kernel to userspace is not + * required here, given that it is implied by mmdrop(). Barrier + * when switching from userspace to kernel is not needed after + * store to rq->curr. + */ + if (IS_ENABLED(CONFIG_SMP) && + likely(!(atomic_read(&next->membarrier_state) & + (MEMBARRIER_STATE_PRIVATE_EXPEDITED | + MEMBARRIER_STATE_GLOBAL_EXPEDITED)) || !prev)) + return; + + /* + * The membarrier system call requires a full memory barrier + * after storing to rq->curr, before going back to user-space. + * Matches a full barrier in the proximity of the membarrier + * system call entry. + */ + smp_mb(); +} + +#endif /* _ASM_RISCV_MEMBARRIER_H */ diff --git a/arch/riscv/mm/context.c b/arch/riscv/mm/context.c index 217fd4de61342..ba8eb3944687c 100644 --- a/arch/riscv/mm/context.c +++ b/arch/riscv/mm/context.c @@ -323,6 +323,8 @@ void switch_mm(struct mm_struct *prev, struct mm_struct *next, if (unlikely(prev == next)) return; + membarrier_arch_switch_mm(prev, next, task); + /* * Mark the current MM context as inactive, and the next as * active. This is at least used by the icache flushing diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 9116bcc903467..c4ca8085885a3 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6709,8 +6709,9 @@ static void __sched notrace __schedule(unsigned int sched_mode) * * Here are the schemes providing that barrier on the * various architectures: - * - mm ? switch_mm() : mmdrop() for x86, s390, sparc, PowerPC. - * switch_mm() rely on membarrier_arch_switch_mm() on PowerPC. + * - mm ? switch_mm() : mmdrop() for x86, s390, sparc, PowerPC, + * RISC-V. switch_mm() relies on membarrier_arch_switch_mm() + * on PowerPC and on RISC-V. * - finish_lock_switch() for weakly-ordered * architectures where spin_unlock is a full barrier, * - switch_to() for arm64 (weakly-ordered, spin_unlock From patchwork Wed Jan 31 14:49:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 13539531 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 9B4C2C47258 for ; Wed, 31 Jan 2024 14:50:22 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=KPh/khhOKzbqmR5eEfZtJNkB1Vxke2aR7dIGGU1dXDk=; b=XbDDcem3gPOzya cLK79sIexCniL1rUktUCXryKnnsrUCiVOcKBnzw3iq/qGLl6l5V1Tj63iwZjrufFPAJEIRwiKu6a8 sks2+nPqh9+qMewPCDiyvstj7VIAhwgWzePH+M2aOCeiY7/Y7QIS61tsH1NUKAv49bMIm1YmVJaGO x17zqadXcmqtLpLBa937XI4p2T+8xfsi6IUrl6tlggWzITgHdjTNphOfIrD+gRu7B/f3diSCGT3nP Ddfg2GAmKQS4iolM/98jKSUKwL75l/4gAvPIDTmy2RUKWyfkzdcSGorts55RKasNTL8csV4uiRi3Q Ee8vLqpjsnG06YEDJbrg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rVBut-000000040Be-3iQN; Wed, 31 Jan 2024 14:50:19 +0000 Received: from mail-lf1-x133.google.com ([2a00:1450:4864:20::133]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rVBup-0000000406v-1G7M for linux-riscv@lists.infradead.org; Wed, 31 Jan 2024 14:50:16 +0000 Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-5102a877d50so5982577e87.0 for ; Wed, 31 Jan 2024 06:50:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706712612; x=1707317412; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/IlmVFOYTS7aecT5TyzaoLng2Pb3mBf2n8I/n2NMhv8=; b=VxwgQ5ryS73hlpkodWlstIhkNcKDnHrP5JRt8kLMeXHr0nbTKHpjBrSsDTAy23Lg4U 6oXoi0u9fXwq1nbkX2zGxct2pqFec+UmAlRcJTAFUWN+apoAKWCch1S5fTexKzJd+XLp hFfxmFJBlj+Y7ya1vUdG1A91axAPvjbkCZu7l8s9J/T7zrR8zN5y+A9AVpX5f/hLlB2E 2fKb+jmuxNCtMt65Ew5z1iQQ052nnzQ3Wc7+TMsNsN+kmhCLn7A1JVajRueb5tJz8f3Y qmcVHWK7OK7hV4mjUNQIybYOlFP6l23pw83kKzPwvol2/KPwV+h/LipZM3298rESrdrq 7Ohw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706712612; x=1707317412; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/IlmVFOYTS7aecT5TyzaoLng2Pb3mBf2n8I/n2NMhv8=; b=sL7CKWczuwxuyyMCN/RWtwJ3+iiwtAxLimHQK2KXZRpZPdYO6i9lfR662DnjizYZGc iZeb/AQ+UFzCIvkrcjWuJBb1uRbYuGEYd0nkW8Y4bVWS7W7aZ0eZb3QnxAePJF8ZdBkL nl18JY5oIj/4Kx6tRyywFBYr8VPn2elRwgNSq/UIwXm0hfY8VX2yOxjSncbFq0jASKpF 28BgXHmbSpyLc+u3FmsBh6qm+AxTWEvSnfESt6X9rsOr97UxFvlyYkB4fFYAVlB7XQNh pyA+xVJg1VKbr8+Bwt/ghHccQgahJqsdIgcHmw3DA9j7aA8Xif4EMJGJXUQMR38YTlsH eSAw== X-Gm-Message-State: AOJu0YyaDsel7HPwHPURIrKZ1T+8FQXZoJ1HQMwRRmKkm6gXgs3DDvP8 DuR8mfryPsOd+BShwIm77/KoLrtesC7NhSk+6jLq0xq1kCg+S4nT X-Google-Smtp-Source: AGHT+IGrkog/qhZGfuQC5VscYem/7+Dnb/2LHjHr6AKiylercaEQE/FyzttVwRS81d3a/pjOtyBwJQ== X-Received: by 2002:a05:6512:6ce:b0:510:f52:648c with SMTP id u14-20020a05651206ce00b005100f52648cmr1623894lff.51.1706712612052; Wed, 31 Jan 2024 06:50:12 -0800 (PST) Received: from andrea.wind3.hub ([31.189.8.91]) by smtp.gmail.com with ESMTPSA id w6-20020a5d4046000000b0033b0d049c71sm419265wrp.54.2024.01.31.06.50.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 06:50:11 -0800 (PST) From: Andrea Parri To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, mathieu.desnoyers@efficios.com, paulmck@kernel.org, corbet@lwn.net Cc: mmaas@google.com, hboehm@google.com, striker@us.ibm.com, charlie@rivosinc.com, rehn@rivosinc.com, rdunlap@infradead.org, sorear@fastmail.com, linux-riscv@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Andrea Parri Subject: [PATCH v4 2/4] membarrier: Create Documentation/scheduler/membarrier.rst Date: Wed, 31 Jan 2024 15:49:34 +0100 Message-Id: <20240131144936.29190-3-parri.andrea@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240131144936.29190-1-parri.andrea@gmail.com> References: <20240131144936.29190-1-parri.andrea@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240131_065015_414378_D07EE550 X-CRM114-Status: GOOD ( 18.48 ) 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 To gather the architecture requirements of the "private/global expedited" membarrier commands. The file will be expanded to integrate further information about the membarrier syscall (as needed/desired in the future). While at it, amend some related inline comments in the membarrier codebase. Suggested-by: Mathieu Desnoyers Signed-off-by: Andrea Parri Reviewed-by: Mathieu Desnoyers --- Documentation/scheduler/index.rst | 1 + Documentation/scheduler/membarrier.rst | 39 ++++++++++++++++++++++++++ MAINTAINERS | 1 + kernel/sched/core.c | 7 ++++- kernel/sched/membarrier.c | 8 +++--- 5 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 Documentation/scheduler/membarrier.rst diff --git a/Documentation/scheduler/index.rst b/Documentation/scheduler/index.rst index 3170747226f6d..43bd8a145b7a9 100644 --- a/Documentation/scheduler/index.rst +++ b/Documentation/scheduler/index.rst @@ -7,6 +7,7 @@ Scheduler completion + membarrier sched-arch sched-bwc sched-deadline diff --git a/Documentation/scheduler/membarrier.rst b/Documentation/scheduler/membarrier.rst new file mode 100644 index 0000000000000..2387804b1c633 --- /dev/null +++ b/Documentation/scheduler/membarrier.rst @@ -0,0 +1,39 @@ +.. SPDX-License-Identifier: GPL-2.0 + +======================== +membarrier() System Call +======================== + +MEMBARRIER_CMD_{PRIVATE,GLOBAL}_EXPEDITED - Architecture requirements +===================================================================== + +Memory barriers before updating rq->curr +---------------------------------------- + +The commands MEMBARRIER_CMD_PRIVATE_EXPEDITED and MEMBARRIER_CMD_GLOBAL_EXPEDITED +require each architecture to have a full memory barrier after coming from +user-space, before updating rq->curr. This barrier is implied by the sequence +rq_lock(); smp_mb__after_spinlock() in __schedule(). The barrier matches a full +barrier in the proximity of the membarrier system call exit, cf. +membarrier_{private,global}_expedited(). + +Memory barriers after updating rq->curr +--------------------------------------- + +The commands MEMBARRIER_CMD_PRIVATE_EXPEDITED and MEMBARRIER_CMD_GLOBAL_EXPEDITED +require each architecture to have a full memory barrier after updating rq->curr, +before returning to user-space. The schemes providing this barrier on the various +architectures are as follows. + + - alpha, arc, arm, hexagon, mips rely on the full barrier implied by + spin_unlock() in finish_lock_switch(). + + - arm64 relies on the full barrier implied by switch_to(). + + - powerpc, riscv, s390, sparc, x86 rely on the full barrier implied by + switch_mm(), if mm is not NULL; they rely on the full barrier implied + by mmdrop(), otherwise. On powerpc and riscv, switch_mm() relies on + membarrier_arch_switch_mm(). + +The barrier matches a full barrier in the proximity of the membarrier system call +entry, cf. membarrier_{private,global}_expedited(). diff --git a/MAINTAINERS b/MAINTAINERS index d7ed83b73cbe4..628f3762d97cd 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14040,6 +14040,7 @@ M: Mathieu Desnoyers M: "Paul E. McKenney" L: linux-kernel@vger.kernel.org S: Supported +F: Documentation/scheduler/membarrier.rst F: arch/*/include/asm/membarrier.h F: include/uapi/linux/membarrier.h F: kernel/sched/membarrier.c diff --git a/kernel/sched/core.c b/kernel/sched/core.c index c4ca8085885a3..a972628e77567 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6638,7 +6638,9 @@ static void __sched notrace __schedule(unsigned int sched_mode) * if (signal_pending_state()) if (p->state & @state) * * Also, the membarrier system call requires a full memory barrier - * after coming from user-space, before storing to rq->curr. + * after coming from user-space, before storing to rq->curr; this + * barrier matches a full barrier in the proximity of the membarrier + * system call exit. */ rq_lock(rq, &rf); smp_mb__after_spinlock(); @@ -6716,6 +6718,9 @@ static void __sched notrace __schedule(unsigned int sched_mode) * architectures where spin_unlock is a full barrier, * - switch_to() for arm64 (weakly-ordered, spin_unlock * is a RELEASE barrier), + * + * The barrier matches a full barrier in the proximity of + * the membarrier system call entry. */ ++*switch_count; diff --git a/kernel/sched/membarrier.c b/kernel/sched/membarrier.c index 2ad881d07752c..f3d91628d6b8a 100644 --- a/kernel/sched/membarrier.c +++ b/kernel/sched/membarrier.c @@ -251,7 +251,7 @@ static int membarrier_global_expedited(void) return 0; /* - * Matches memory barriers around rq->curr modification in + * Matches memory barriers after rq->curr modification in * scheduler. */ smp_mb(); /* system call entry is not a mb. */ @@ -300,7 +300,7 @@ static int membarrier_global_expedited(void) /* * Memory barrier on the caller thread _after_ we finished - * waiting for the last IPI. Matches memory barriers around + * waiting for the last IPI. Matches memory barriers before * rq->curr modification in scheduler. */ smp_mb(); /* exit from system call is not a mb */ @@ -339,7 +339,7 @@ static int membarrier_private_expedited(int flags, int cpu_id) return 0; /* - * Matches memory barriers around rq->curr modification in + * Matches memory barriers after rq->curr modification in * scheduler. */ smp_mb(); /* system call entry is not a mb. */ @@ -415,7 +415,7 @@ static int membarrier_private_expedited(int flags, int cpu_id) /* * Memory barrier on the caller thread _after_ we finished - * waiting for the last IPI. Matches memory barriers around + * waiting for the last IPI. Matches memory barriers before * rq->curr modification in scheduler. */ smp_mb(); /* exit from system call is not a mb */ From patchwork Wed Jan 31 14:49:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 13539582 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 9F4A8C47258 for ; Wed, 31 Jan 2024 16:01:44 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=z93FrsjsuXG6joZZPpdZgL0iPAYwI/jsSAD0lVzTdiM=; b=sZOsPV5KqhF/CE exlSPVWhFUC9Cm1PFxjtqz4OVQAhyqXD9+dZwizJooCOPSRXu194eRMU3Z3uMCljczuH9J+GUobaV RlwWskK3zPui0Eu7G6x+/Eb44cNMFBC4VUs2/8RDyTgqsEiqZIsSFCTGfkKiYD1wqK3oAjYn2Fx7R eewMcmCA0RL+YIFnBKOPwtXqO0jTfyo+ugY7fBSaVHIrMHM3lShUzJcZgNC49cU9amxdysst2qPSP TNyhK0Wc5j6rkCc4xfzZb7HDvOZc/XyuZIPvenY/GHo0tEOqqxiSI6K2o0kUZTNFpWNzy7Wjy7f2n vrYChjzMXXRAXfvo739A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rVD1x-00000004Na5-1QYt; Wed, 31 Jan 2024 16:01:41 +0000 Received: from mail-wr1-x42a.google.com ([2a00:1450:4864:20::42a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rVBus-0000000407j-0Nki for linux-riscv@lists.infradead.org; Wed, 31 Jan 2024 14:50:19 +0000 Received: by mail-wr1-x42a.google.com with SMTP id ffacd0b85a97d-33ae4205ad8so3013702f8f.2 for ; Wed, 31 Jan 2024 06:50:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706712614; x=1707317414; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EgAdMMq2AoWWNilPNigg7Nbpcu7FSjABsk32vurOK/A=; b=lmKvyYSjvWjfdyRny3BQffnJZ/10tzQAEQdIpzbQiYN97reDKSGxTRPOYQQEkX8maf nYTK4oPE8Hb8oeE/yYwaZ09s5b0FuswzJN724B/1/ze4HYtkOIdK//a1n/wWX+R7r4Pg VXcwfRUlgV5RTR6I3ASSL6o+wiOqe8z8dJAmeOEkP551dIzaHlS3wPzrvJcxblOdS3Eg 6ilGmeu7d1yjcf+5rcbm5lzugXloSwghfcS6XPIKW6y4Ozmfvm8RY+8DtRpjC2owejfH Lqdh/fNjUWnCU8pmGAuajFp6RU009N/Lrs7F6Ycju4SXkTXmf4ErwbRbD0+ZA6DIifC0 Qv2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706712614; x=1707317414; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EgAdMMq2AoWWNilPNigg7Nbpcu7FSjABsk32vurOK/A=; b=e9wFz3QC8xOxY7+Kpb0jztHK5wukdossyLwDNneXxKz+QGrllQEEiAfg1cY3Kh/zZz jFk9eJFqjZShDKnL7mM4HuJy7KR21oU9+BsoTAyHxO+ZiC5yeWxiIVLvgxfDbMc3t4pG 0GupV4dAT79yPQkfXr5A+nJpDwNym4wIzOhrb5j2u0bRqjyqrCdzTG5GXaZUdtQePAvB A7rUoJRPdEQCCXmzXqxjtexK9opv4A18wD+aAqyQ8pLoEe8KbFs+3egJ892ADfwvTCJT 2ZiSUu695fyq8S3gXTJfMqkQPSZeT7/TuhCECOeVtwm+pKuJOmgRfNLWJre6hwPC2a3H KITg== X-Gm-Message-State: AOJu0YyeyxzpRe3duTnjaPQludZkJG/MMZI3N/xcc+67tCok3Gvnmrws Gs9i0OiZ1PtX6eXpdLr/npJm3sAGgOjTgS+ulLyQnwvo0q4gG+sJ X-Google-Smtp-Source: AGHT+IFRT6v8rC9ruh3UI6dztiV62kHw03zIYHJ0VpYeoWNXLg2hPPytoIqnwPVCg0HA7hvUfEmVsg== X-Received: by 2002:a5d:64ce:0:b0:33a:e834:5545 with SMTP id f14-20020a5d64ce000000b0033ae8345545mr1675049wri.16.1706712614271; Wed, 31 Jan 2024 06:50:14 -0800 (PST) Received: from andrea.wind3.hub ([31.189.8.91]) by smtp.gmail.com with ESMTPSA id w6-20020a5d4046000000b0033b0d049c71sm419265wrp.54.2024.01.31.06.50.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 06:50:13 -0800 (PST) From: Andrea Parri To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, mathieu.desnoyers@efficios.com, paulmck@kernel.org, corbet@lwn.net Cc: mmaas@google.com, hboehm@google.com, striker@us.ibm.com, charlie@rivosinc.com, rehn@rivosinc.com, rdunlap@infradead.org, sorear@fastmail.com, linux-riscv@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Andrea Parri Subject: [PATCH v4 3/4] locking: Introduce prepare_sync_core_cmd() Date: Wed, 31 Jan 2024 15:49:35 +0100 Message-Id: <20240131144936.29190-4-parri.andrea@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240131144936.29190-1-parri.andrea@gmail.com> References: <20240131144936.29190-1-parri.andrea@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240131_065018_250458_3E06B714 X-CRM114-Status: GOOD ( 12.74 ) 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 Introduce an architecture function that architectures can use to set up ("prepare") SYNC_CORE commands. The function will be used by RISC-V to update its "deferred icache- flush" data structures (icache_stale_mask). Architectures defining prepare_sync_core_cmd() static inline need to select ARCH_HAS_PREPARE_SYNC_CORE_CMD. Suggested-by: Mathieu Desnoyers Signed-off-by: Andrea Parri Reviewed-by: Mathieu Desnoyers --- include/linux/sync_core.h | 16 +++++++++++++++- init/Kconfig | 3 +++ kernel/sched/membarrier.c | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/linux/sync_core.h b/include/linux/sync_core.h index 013da4b8b3272..67bb9794b8758 100644 --- a/include/linux/sync_core.h +++ b/include/linux/sync_core.h @@ -17,5 +17,19 @@ static inline void sync_core_before_usermode(void) } #endif -#endif /* _LINUX_SYNC_CORE_H */ +#ifdef CONFIG_ARCH_HAS_PREPARE_SYNC_CORE_CMD +#include +#else +/* + * This is a dummy prepare_sync_core_cmd() implementation that can be used on + * all architectures which provide unconditional core serializing instructions + * in switch_mm(). + * If your architecture doesn't provide such core serializing instructions in + * switch_mm(), you may need to write your own functions. + */ +static inline void prepare_sync_core_cmd(struct mm_struct *mm) +{ +} +#endif +#endif /* _LINUX_SYNC_CORE_H */ diff --git a/init/Kconfig b/init/Kconfig index 8d4e836e1b6b1..e2bad47922642 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1982,6 +1982,9 @@ source "kernel/Kconfig.locks" config ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE bool +config ARCH_HAS_PREPARE_SYNC_CORE_CMD + bool + config ARCH_HAS_SYNC_CORE_BEFORE_USERMODE bool diff --git a/kernel/sched/membarrier.c b/kernel/sched/membarrier.c index f3d91628d6b8a..6d1f31b3a967b 100644 --- a/kernel/sched/membarrier.c +++ b/kernel/sched/membarrier.c @@ -320,6 +320,7 @@ static int membarrier_private_expedited(int flags, int cpu_id) MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY)) return -EPERM; ipi_func = ipi_sync_core; + prepare_sync_core_cmd(mm); } else if (flags == MEMBARRIER_FLAG_RSEQ) { if (!IS_ENABLED(CONFIG_RSEQ)) return -EINVAL; From patchwork Wed Jan 31 14:49:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Parri X-Patchwork-Id: 13539532 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 1E3C5C47DB3 for ; Wed, 31 Jan 2024 14:50:29 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=LHrtETVt0dQxyqXoym9C8G6LK/MtDKotp6WZvIv4QoY=; b=2Rtw/r1/x07Zcn 0cGvRjy4GoJJXXuN0xRN9CXRY3+16wEd1B1Ry3eiIdEILuiBzSJc4UbHiBbBNW4BLqk95yqAjFC1H lku4TcdlKVm8SF3GPMgR5I4ETDoXr+JUwtH8AB7DhnyWqFkyHky/VAKwq93WFPUigJn9+2BMYCyS4 KXC1mRwKWA/FhyWrRyCPPINnJt8YJowk8c5+TxFo1mhqnREGossb3Wuhhq/m6NNd74aWqhp4OMenW 8uRhW4ZdtC3MAaFebNznb++gznfN0i32Z9nVTpdOoNQmvxJ83IY6i/ySNpURuSxT+9cnfahqfBrsG WY8k+4KGqbD9Yk/4g37A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rVBv0-000000040HD-1WcJ; Wed, 31 Jan 2024 14:50:26 +0000 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rVBut-0000000409A-18JX for linux-riscv@lists.infradead.org; Wed, 31 Jan 2024 14:50:21 +0000 Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-33ae4205ad8so3013736f8f.2 for ; Wed, 31 Jan 2024 06:50:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706712616; x=1707317416; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sBtYOBqzah2COc6Cv21eJlyQjax7AL2k8qugBY856Dk=; b=fnr7gUg6FfOUas341TGlKbZsF9bdt6kCw0Ohao2Tk+xYjwkhp0cj9X2YmK3D+IS4ag OSBuCiJI29YoOIBN5dIp2YM6o2oaoPH2EEyZr7NgNd8XjEiYQhs3X+JPD2TZnECF2RUx GsxIRJrNl/DQmuFTgR51ltfi/puTuIojxQJwRUk9VRH1SckKCuC7tWMqZWUEVKRKlv82 edDm3RCK7pt8Phj6Hg8H0d93AuuTz+Yf8TyInKrcKxorNI7AojvLvg6XIrtk6Anbl9Ln LGyfreEuCXnMr1Z2zBNKuJj27IlIuiK3DP2XVVMjLVJVwGJol34R5SLYv5vZFlMPE1N0 CDkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706712616; x=1707317416; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sBtYOBqzah2COc6Cv21eJlyQjax7AL2k8qugBY856Dk=; b=uJYHWH5ZgWDHiRa/kmjJeqZrkNQv8Z5oHQvDB+Jx2GInMF+a02gKdM8ISymmemsYLw nPFdsgWYUWg2Nx/HsNoSyqQwaPm6JCzQIYFgNk6GgfN1or8nwZ57xRimCO33OWeVtz3g JdJXd3/AxxHJwwYNMlMsXtBZyEiNKezaYNNuCJ2+FmN2owHwWgfsbp7KiPcjaa6oyq/U 1YqVQvcan4sUmWc8dnT9jQtSL90uAo/Kwsb4CedJntgBupHX23vBLNSTbGTve9dQQySO lywTZWts1Y58tdXlozbsXiHRq5tldpiQzKignAu2Bo3NTSdNou3tAvt6ZNo3J/oFoZHv gCVA== X-Gm-Message-State: AOJu0Yw0OD2wM8LSGfGd5kqxZHj9BElugdgC1V9BWqhJOb3rkL3W9PVJ M/5eAd8kSsiK9Kc2j65u5ZpJYWWF6cwT9vILZajRWPCQSt4buZsJ X-Google-Smtp-Source: AGHT+IFzHyII0E803YFcr2LWchdZp3gu+YQKrnRRDpAoCqkSNmvsjur8XaGr+luL1UtHCiX8h+kWWA== X-Received: by 2002:a05:6000:12c2:b0:33a:ff90:68eb with SMTP id l2-20020a05600012c200b0033aff9068ebmr1309832wrx.26.1706712616480; Wed, 31 Jan 2024 06:50:16 -0800 (PST) Received: from andrea.wind3.hub ([31.189.8.91]) by smtp.gmail.com with ESMTPSA id w6-20020a5d4046000000b0033b0d049c71sm419265wrp.54.2024.01.31.06.50.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 06:50:16 -0800 (PST) From: Andrea Parri To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, mathieu.desnoyers@efficios.com, paulmck@kernel.org, corbet@lwn.net Cc: mmaas@google.com, hboehm@google.com, striker@us.ibm.com, charlie@rivosinc.com, rehn@rivosinc.com, rdunlap@infradead.org, sorear@fastmail.com, linux-riscv@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Andrea Parri Subject: [PATCH v4 4/4] membarrier: riscv: Provide core serializing command Date: Wed, 31 Jan 2024 15:49:36 +0100 Message-Id: <20240131144936.29190-5-parri.andrea@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240131144936.29190-1-parri.andrea@gmail.com> References: <20240131144936.29190-1-parri.andrea@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240131_065019_417587_14D125B5 X-CRM114-Status: GOOD ( 23.42 ) 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 RISC-V uses xRET instructions on return from interrupt and to go back to user-space; the xRET instruction is not core serializing. Use FENCE.I for providing core serialization as follows: - by calling sync_core_before_usermode() on return from interrupt (cf. ipi_sync_core()), - via switch_mm() and sync_core_before_usermode() (respectively, for uthread->uthread and kthread->uthread transitions) before returning to user-space. On RISC-V, the serialization in switch_mm() is activated by resetting the icache_stale_mask of the mm at prepare_sync_core_cmd(). Suggested-by: Palmer Dabbelt Signed-off-by: Andrea Parri Reviewed-by: Mathieu Desnoyers --- .../membarrier-sync-core/arch-support.txt | 18 +++++++++++- MAINTAINERS | 1 + arch/riscv/Kconfig | 3 ++ arch/riscv/include/asm/membarrier.h | 19 ++++++++++++ arch/riscv/include/asm/sync_core.h | 29 +++++++++++++++++++ kernel/sched/core.c | 4 +++ kernel/sched/membarrier.c | 4 +++ 7 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/sync_core.h diff --git a/Documentation/features/sched/membarrier-sync-core/arch-support.txt b/Documentation/features/sched/membarrier-sync-core/arch-support.txt index d96b778b87ed8..7425d2b994a39 100644 --- a/Documentation/features/sched/membarrier-sync-core/arch-support.txt +++ b/Documentation/features/sched/membarrier-sync-core/arch-support.txt @@ -10,6 +10,22 @@ # Rely on implicit context synchronization as a result of exception return # when returning from IPI handler, and when returning to user-space. # +# * riscv +# +# riscv uses xRET as return from interrupt and to return to user-space. +# +# Given that xRET is not core serializing, we rely on FENCE.I for providing +# core serialization: +# +# - by calling sync_core_before_usermode() on return from interrupt (cf. +# ipi_sync_core()), +# +# - via switch_mm() and sync_core_before_usermode() (respectively, for +# uthread->uthread and kthread->uthread transitions) before returning +# to user-space. +# +# The serialization in switch_mm() is activated by prepare_sync_core_cmd(). +# # * x86 # # x86-32 uses IRET as return from interrupt, which takes care of the IPI. @@ -43,7 +59,7 @@ | openrisc: | TODO | | parisc: | TODO | | powerpc: | ok | - | riscv: | TODO | + | riscv: | ok | | s390: | ok | | sh: | TODO | | sparc: | TODO | diff --git a/MAINTAINERS b/MAINTAINERS index 628f3762d97cd..8373ce3e4350a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14042,6 +14042,7 @@ L: linux-kernel@vger.kernel.org S: Supported F: Documentation/scheduler/membarrier.rst F: arch/*/include/asm/membarrier.h +F: arch/*/include/asm/sync_core.h F: include/uapi/linux/membarrier.h F: kernel/sched/membarrier.c diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 087abf9e51c69..70836381b9482 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -28,14 +28,17 @@ config RISCV select ARCH_HAS_GIGANTIC_PAGE select ARCH_HAS_KCOV select ARCH_HAS_MEMBARRIER_CALLBACKS + select ARCH_HAS_MEMBARRIER_SYNC_CORE select ARCH_HAS_MMIOWB select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE select ARCH_HAS_PMEM_API + select ARCH_HAS_PREPARE_SYNC_CORE_CMD select ARCH_HAS_PTE_SPECIAL select ARCH_HAS_SET_DIRECT_MAP if MMU select ARCH_HAS_SET_MEMORY if MMU select ARCH_HAS_STRICT_KERNEL_RWX if MMU && !XIP_KERNEL select ARCH_HAS_STRICT_MODULE_RWX if MMU && !XIP_KERNEL + select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE select ARCH_HAS_SYSCALL_WRAPPER select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN_SANITIZE_ALL diff --git a/arch/riscv/include/asm/membarrier.h b/arch/riscv/include/asm/membarrier.h index 6c016ebb5020a..47b240d0d596a 100644 --- a/arch/riscv/include/asm/membarrier.h +++ b/arch/riscv/include/asm/membarrier.h @@ -22,6 +22,25 @@ static inline void membarrier_arch_switch_mm(struct mm_struct *prev, /* * The membarrier system call requires a full memory barrier * after storing to rq->curr, before going back to user-space. + * + * This barrier is also needed for the SYNC_CORE command when + * switching between processes; in particular, on a transition + * from a thread belonging to another mm to a thread belonging + * to the mm for which a membarrier SYNC_CORE is done on CPU0: + * + * - [CPU0] sets all bits in the mm icache_stale_mask (in + * prepare_sync_core_cmd()); + * + * - [CPU1] stores to rq->curr (by the scheduler); + * + * - [CPU0] loads rq->curr within membarrier and observes + * cpu_rq(1)->curr->mm != mm, so the IPI is skipped on + * CPU1; this means membarrier relies on switch_mm() to + * issue the sync-core; + * + * - [CPU1] switch_mm() loads icache_stale_mask; if the bit + * is zero, switch_mm() may incorrectly skip the sync-core. + * * Matches a full barrier in the proximity of the membarrier * system call entry. */ diff --git a/arch/riscv/include/asm/sync_core.h b/arch/riscv/include/asm/sync_core.h new file mode 100644 index 0000000000000..9153016da8f14 --- /dev/null +++ b/arch/riscv/include/asm/sync_core.h @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_RISCV_SYNC_CORE_H +#define _ASM_RISCV_SYNC_CORE_H + +/* + * RISC-V implements return to user-space through an xRET instruction, + * which is not core serializing. + */ +static inline void sync_core_before_usermode(void) +{ + asm volatile ("fence.i" ::: "memory"); +} + +#ifdef CONFIG_SMP +/* + * Ensure the next switch_mm() on every CPU issues a core serializing + * instruction for the given @mm. + */ +static inline void prepare_sync_core_cmd(struct mm_struct *mm) +{ + cpumask_setall(&mm->context.icache_stale_mask); +} +#else +static inline void prepare_sync_core_cmd(struct mm_struct *mm) +{ +} +#endif /* CONFIG_SMP */ + +#endif /* _ASM_RISCV_SYNC_CORE_H */ diff --git a/kernel/sched/core.c b/kernel/sched/core.c index a972628e77567..e4a87bcf28d40 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6721,6 +6721,10 @@ static void __sched notrace __schedule(unsigned int sched_mode) * * The barrier matches a full barrier in the proximity of * the membarrier system call entry. + * + * On RISC-V, this barrier pairing is also needed for the + * SYNC_CORE command when switching between processes, cf. + * the inline comments in membarrier_arch_switch_mm(). */ ++*switch_count; diff --git a/kernel/sched/membarrier.c b/kernel/sched/membarrier.c index 6d1f31b3a967b..703e8d80a576d 100644 --- a/kernel/sched/membarrier.c +++ b/kernel/sched/membarrier.c @@ -342,6 +342,10 @@ static int membarrier_private_expedited(int flags, int cpu_id) /* * Matches memory barriers after rq->curr modification in * scheduler. + * + * On RISC-V, this barrier pairing is also needed for the + * SYNC_CORE command when switching between processes, cf. + * the inline comments in membarrier_arch_switch_mm(). */ smp_mb(); /* system call entry is not a mb. */