Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp3993078rdb; Mon, 11 Dec 2023 06:08:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IHqKqYUr7RxCtUjtnTDpgBiCgUiX0wrgxxVoeScOrScunSDnYs8Zd2I0UFkPdmYfaeqrh+3 X-Received: by 2002:a17:902:f690:b0:1d0:c502:e0b3 with SMTP id l16-20020a170902f69000b001d0c502e0b3mr5044473plg.36.1702303739397; Mon, 11 Dec 2023 06:08:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702303739; cv=none; d=google.com; s=arc-20160816; b=OdadAlX679wJ+0B5ETm0VBWKQXxxo3gmnEFADav7KkQvWv2/pP639Y4e0htQoqksaC eU4rheVuj/G/2ryNfQeofI1V29vXThWURQvKxWW+kRVfAbshzuUrSoFOUrNaMDkS334J y+pY83Tj3qhsGZRwGUU67Lv9ePxw8Mf8/OcR0YsblDo3McAVPY/pbLCJF4uo5inq3cEY kEsWOsaPjvB1rj6MHOQdZQz+VNwElOdUO8N08W8z4IoQL9yNZ3UqPGHdjsj+/I+2GBfD oql0dS+QUk7szT8Jw0R3HOlsjYBqeenD3EvSkIzJowr7R2URLhtJEgapL59XWr43HsRA 9WsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=wIvFGhb7exAg6gzxd6iuuCQ9VQ+PBhgQAnGp3vk3QE8=; fh=Bz9NwsytsT3vSVvQidftPATzo61v2khV4IZfIKXUczU=; b=HMwE0bTtl2Kr1lycxIAEdJPZxq9zSeF9rX6Xk9DcHbUYIdNfgKsHQrwr12dQJgYEEZ m5PmRDQx+GA/nGZeykbGI/VhDq5MoO/L4xS7T9fVMAX7hvDzErWs2krgWTF5fC8giqze HcZkRvRh7gCZdwPATQc/3IByTGT4xKcI+ErsrQCCNhuSIKhIoLT/M0ItWJz0b264+9sC /mVRDqazxp+dAKWpwKROP+2cCWpn7izZjrcAvA+K8ByoQp0eApfMb3cFdMa+ZbwNtFfK ttkBSKRDwdXwBgfmwbUzKGuxS7tmxlkjWB6M6qwPIg8bTEP1CLqAMke05NjrIs4gyDik v9Sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=smtpout1 header.b=uPyaDTl+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id p19-20020a170902b09300b001d07b6a0aeasi5996147plr.325.2023.12.11.06.08.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 06:08:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@efficios.com header.s=smtpout1 header.b=uPyaDTl+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 33EAC8075918; Mon, 11 Dec 2023 06:08:57 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344143AbjLKOId (ORCPT + 99 others); Mon, 11 Dec 2023 09:08:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344132AbjLKOIO (ORCPT ); Mon, 11 Dec 2023 09:08:14 -0500 Received: from smtpout.efficios.com (smtpout.efficios.com [167.114.26.122]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 850E74224 for ; Mon, 11 Dec 2023 05:55:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1702302920; bh=JZS3M0CY1us1Zr6CsScLovyExIwx8qNZhpLweKknXaE=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=uPyaDTl+S8XIhiVgq1qBTupBCuXyUUOkvGxko1XPRUnNefzAB/+idE2DeRK8L2DAi jz7A+pm98Xmnp6OmKW5rIdlPuSVfQAr0ED4PUDIbdVpsOxf1JsvD3D0D4387UL4cCe a4r00aaVb9DaAAxWREkmxENRYgNbr9rKlsXFcPZmrRAOQuh8N3VEYb6ZaZZRVm4NpL cE94hTIC5IeTONV7hvv0ycqpr+s7+6paY+t7DSh0lfPepXNFAINkXAunUDEbftwSGf B71Te4cYN4BagXcPAh8D141lkDFF21vw5kiq1s9yz1M8YJdwGQtODUomUgpyejyNag GQiarQjP67N+A== Received: from [172.16.0.134] (192-222-143-198.qc.cable.ebox.net [192.222.143.198]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4Spjtb6Fjbz1hsB; Mon, 11 Dec 2023 08:55:19 -0500 (EST) Message-ID: Date: Mon, 11 Dec 2023 08:55:19 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 3/4] membarrier: riscv: Provide core serializing command Content-Language: en-US To: Andrea Parri , paulmck@kernel.org, palmer@dabbelt.com, paul.walmsley@sifive.com, aou@eecs.berkeley.edu Cc: mmaas@google.com, hboehm@google.com, striker@us.ibm.com, charlie@rivosinc.com, rehn@rivosinc.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org References: <20231211094414.8078-1-parri.andrea@gmail.com> <20231211094414.8078-4-parri.andrea@gmail.com> From: Mathieu Desnoyers In-Reply-To: <20231211094414.8078-4-parri.andrea@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 11 Dec 2023 06:08:57 -0800 (PST) On 2023-12-11 04:44, Andrea Parri wrote: > 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) to go back 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 > --- > .../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 +++++++++++++++++++ > 5 files changed, 69 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..a163170fc0f48 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) to go back 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 a9166d82ffced..f6f1fdc76cf46 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -13808,6 +13808,7 @@ M: "Paul E. McKenney" > L: linux-kernel@vger.kernel.org > S: Supported > 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 f7db95097caf1..db7b1acd943e4 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 4be218fa03b14..a1071039c20ed 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. > + * > + * The 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. > + * > + * - [CPU1] store 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 the full barrier in membarrier_private_expedited(). There are two full barriers in membarrier_private_expedited(). We should clearly state which one it matches, and update the associated barrier to state that it matches this barrier as well. The barrier it matches is near the membarrier system call entry: /* * Matches memory barriers around rq->curr modification in * scheduler. */ smp_mb(); /* system call entry is not a mb. */ Note that membarrier _also_ requires a full barrier between entering the kernel and storing to rq->curr. This is provided by __schedule(): * Also, the membarrier system call requires a full memory barrier * after coming from user-space, before storing to rq->curr. */ rq_lock(rq, &rf); smp_mb__after_spinlock(); This other barrier is matched by the barrier near the membarrier system call exit: /* * Memory barrier on the caller thread _after_ we finished * waiting for the last IPI. Matches memory barriers around * rq->curr modification in scheduler. */ smp_mb(); /* exit from system call is not a mb */ I don't think this second barrier pairing matters for the cpumask update though. Thanks, Mathieu > */ > smp_mb(); > } > 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 */ -- Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com