Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp4834630rdh; Wed, 29 Nov 2023 12:01:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IGbAJiWN9P4tfyXJQvW0TWAEOmloXGqI33vbEHvc1Vcu1Eb4csihQvaL6EkbPEV+8j6HBIS X-Received: by 2002:a05:6830:618c:b0:6d8:5421:1e10 with SMTP id cb12-20020a056830618c00b006d854211e10mr2509470otb.33.1701288086668; Wed, 29 Nov 2023 12:01:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701288086; cv=none; d=google.com; s=arc-20160816; b=K5RukyPwn2ifZ8DzRTYRaqm4C7Sh21/UE9Z02jWqoCweiG4OwMcIGax1fzzUaKArBO wFgXIq8N8UST2CNgA7Hc3oYNLFYQ9yDW3XHcQbivqsDMQZDhABVF9/PM/A4gOIaOyD/3 FzvAu9efywdj5KBtzMfDYBaD9ik72It5uJD9+slqvXcunXzZ9L0mI9/rP9oagUq7YdTI snnzHsTAHsA3fUAQd2UYFgP1iDFWa0c53MAZE+STVzRwcke9BuiAR0ev8GPrcnN2O08Y fuNUYhC6wshA4CwY3nONb5Jl/74YtSx7wc0jRpRZRqrGpe8qPYpo8oNlagQbV/dPQAEn U2Ow== 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=MN8Z/I7gawLDvdrZqr1VW7PO8wgqj4mQdQK9IXXhaqc=; fh=QQOLJj1t3fuOs/xn8P5MMLWfEiRGcBT++YRk6zTi0qE=; b=s6GAk86blAgBCP8PxDOBfotSdM/xfKn/OFG8Lg50FB0uK3TTRjyU1y97auehc+M8+C bG2zeeC42fZP3KckBqVLQ3J3vl7iiggt3D9V7dENYnd7aMdxAhknhnv8+Ds4trObekOB E4/SqgeTyx82DH6skdekOuZbAdLhHkKak+cl5SWYJCWa2l4Ssq+qrNYAXn0oITh3WNfl rJw2jE1D8rdsAP2d2HNU/cPFf8nCwN9bpmA0vGTpQZ2YuQOmm+an+KYVbSSPCsSLyB0J LXXDtJeOPAebvxmlnbnF7VZO1Ms6YmaTJebX+toGkXn75euSyYLeN4v5+ORPcke3KY3X 5jFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=smtpout1 header.b=OH9niDeI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id q7-20020a9d4b07000000b006ce2c3187a0si5680491otf.178.2023.11.29.12.01.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 12:01:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@efficios.com header.s=smtpout1 header.b=OH9niDeI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id A396F80BB3C8; Wed, 29 Nov 2023 12:01:23 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233640AbjK2UAx (ORCPT + 99 others); Wed, 29 Nov 2023 15:00:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231171AbjK2UAv (ORCPT ); Wed, 29 Nov 2023 15:00:51 -0500 Received: from smtpout.efficios.com (smtpout.efficios.com [167.114.26.122]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57DD7122 for ; Wed, 29 Nov 2023 12:00:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1701288054; bh=gURHk+F8I/B5qgEFhrTj6bvW+61mibfmAZ8NGFKftRk=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=OH9niDeIBd0DjmAc5qNoWQTFhzi/2fIRejCFBUs8qs3LNdUZvNSey2gc7Sl9R4qpW poQgVCF5eSYDo3VrPFCgEiFsiKYfm4h10fVbkLQSGIwu7v1skE+2jxa70ppPsWEEqA yTzUNMqWSNaZXmDJLm8jxj2Ygk3bBioP462nfJ5jttf1BPIZR7fQWUDB6cLQhmk41a KG7i9EvcdtxzO2/gWw24AwgQHR4S1Ey3Qu/iGvckJch5ZkPZk9TiFxZY8gXXGXFTOL 2Cs9E+ogCtQViwmqZmX35fLCOewOSbUboKaav10J35CoEc0fd8yBZc1cqadGgh4sjk bvuQ2FH6CbF0Q== 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 4SgVYy1HMKz1dw0; Wed, 29 Nov 2023 15:00:54 -0500 (EST) Message-ID: <53aac2ac-46ae-46b8-9fdf-34527b79a63b@efficios.com> Date: Wed, 29 Nov 2023 15:00:53 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] membarrier: riscv: Provide core serializing command Content-Language: en-US To: Andrea Parri Cc: paulmck@kernel.org, palmer@dabbelt.com, paul.walmsley@sifive.com, aou@eecs.berkeley.edu, 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: <20231127103235.28442-1-parri.andrea@gmail.com> <20231127103235.28442-3-parri.andrea@gmail.com> <91ab0210-07f9-42c4-af7f-a98799250cf7@efficios.com> <0a84c0e0-2571-4c7f-82ae-a429f467a16b@efficios.com> From: Mathieu Desnoyers In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email 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 (agentk.vger.email [0.0.0.0]); Wed, 29 Nov 2023 12:01:23 -0800 (PST) On 2023-11-29 13:29, Andrea Parri wrote: >>> diff --git a/arch/riscv/mm/context.c b/arch/riscv/mm/context.c >>> index 217fd4de61342..f63222513076d 100644 >>> --- a/arch/riscv/mm/context.c >>> +++ b/arch/riscv/mm/context.c >>> @@ -323,6 +323,23 @@ void switch_mm(struct mm_struct *prev, struct mm_struct *next, >>> if (unlikely(prev == next)) >>> return; >>> +#if defined(CONFIG_MEMBARRIER) && defined(CONFIG_SMP) >>> + /* >>> + * The membarrier system call requires a full memory barrier >>> + * after storing to rq->curr, before going back to user-space. >>> + * >>> + * 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 (unlikely(atomic_read(&next->membarrier_state) & >>> + (MEMBARRIER_STATE_PRIVATE_EXPEDITED | >>> + MEMBARRIER_STATE_GLOBAL_EXPEDITED)) && prev) >>> + smp_mb(); >>> +#endif >> >> The approach looks good. Please implement it within a separate >> membarrier_arch_switch_mm() as done on powerpc. > > Will do. Thanks. > > As regards the Fixes: tag, I guess it boils down to what we want or we > need to take for commit "riscv: Support membarrier private cmd". :-) I'm not seeing this commit in the Linux master branch, am I missing something ? > FWIW, a quick git-log search confirmed that MEMBARRIER has been around > for quite some time in the RISC-V world (though I'm not familiar with > any of its mainstream uses): commit 1464d00b27b2 says (at least) since > 93917ad50972 ("RISC-V: Add support for restartable sequence"). I am > currently inclined to pick the latter commit (and check it w/ Palmer), > but other suggestions are welcome. Supporting membarrier private expedited is not optional since Linux 4.14: see kernel/sched/core.c: membarrier_switch_mm(rq, prev->active_mm, next->mm); /* * sys_membarrier() requires an smp_mb() between setting * rq->curr / membarrier_switch_mm() and returning to userspace. * * The below provides this either through switch_mm(), or in * case 'prev->active_mm == next->mm' through * finish_task_switch()'s mmdrop(). */ switch_mm_irqs_off(prev->active_mm, next->mm, next); Failure to provide the required barrier is a bug in the architecture's switch_mm implementation when CONFIG_MEMBARRIER=y. We should probably introduce a new Documentation/features/sched/membarrier/arch-support.txt to clarify this requirement. Userspace code such as liburcu [1] heavily relies on membarrier private expedited (when available) to speed up RCU read-side critical sections. Various DNS servers, including BIND 9, use liburcu. Thanks, Mathieu [1] https:/liburcu.org -- Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com