Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp549809pxb; Fri, 16 Apr 2021 11:56:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxUXW6Pti9litHpqo1N97q/24PjmnhoJA0gLVuAmt9tO9HN17MaVsTcZWhNgmsap2vkMERQ X-Received: by 2002:a17:906:ae84:: with SMTP id md4mr9946480ejb.432.1618599390686; Fri, 16 Apr 2021 11:56:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618599390; cv=none; d=google.com; s=arc-20160816; b=qreY1sZnWMqGuko9sVVwnFR8xdpe2nrylu9gNwjfbmdLACXMS1+XvVlv3wsZMC6Joj mG1b7uKFFmBiW86SBc3vuGCaakA7yStYglfsh8S+UysikVmBFM6MAgfR2f3P4oMbdd0g fOa9s+0jURNops8PiMDV1OFZbZGtM1rNIxtg1QF96Gu2uFzema9JWcWRfaJ5Sg6K5mzx 9Cq5eF91Uv2akkOeWrPCN1IU3t2QpoLMQEiYfcNwd971liuYlqeZqauJM+T0X8YYrgVD B+TOY4aZsqaoXFfwmuNuddJrIS30FhDZmrJ+UN82gxUqz02poLveGVZhrL/CHjN6YAcY ygkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=fQ9b9NOltWa3+wxn12KmKR1j/A3Ot5SVv6/hP+NhQjo=; b=dARtf2k35U+K4KZzwjd5FJpPXTkviDr62np1b1VUmGAX1r5dKyAENelhK+9tCDvhWK GnGRjcRdvOwScKhRknP3MSt0aOnkZ32WehBuwna90slLfArbzdaDv1e2D4oZB8cfyTOW bVTxrCNUL/J4WOFmEJk7PUgO1TU4mvZCvfHpz4ulyAKVnzJ/D9vQ3LtYpy4f+r7u2zSj 57piTOGbbjEDxM5Jjolnn2Cz2HD0EFjkg250bEg4MEZy9Bxdg+bJlWhNtnzjaq6SgYKd cdpCBhieUoRIg/CVYzyub0M2HTNkIXMYlqDv3IkQaNrGHwtACkqqSMSSIMo/ENcSU0UB Ewmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JaBGkBjp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e18si5538126edj.485.2021.04.16.11.56.07; Fri, 16 Apr 2021 11:56:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JaBGkBjp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241629AbhDPR0z (ORCPT + 99 others); Fri, 16 Apr 2021 13:26:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:55934 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235935AbhDPR0y (ORCPT ); Fri, 16 Apr 2021 13:26:54 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id BE88260FED; Fri, 16 Apr 2021 17:26:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1618593989; bh=5P4/qPwdpux1jE4JlcCASPqUxEgy+/WluDGUYEnfiG0=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=JaBGkBjpLtY194K6WS2uxdqR/r1qu3qiBMiRynIkyqd2MmgY/YbGP73TNzgq+vojy EhMl5l1JPqMGYvtWarXEsG9OHijpShN9oRDF2faL+YOs8P6WlHNmhY7/BODeymVuSa mJjhT1EpBpwvizfWakOM9MhTrycojEkEJFLUejqph2HpSnTABrEZdPSeY+9KmsGj5x K4fWGYiW8H4nhjHOzLxgl/XsAsFU+RJVNHf0sWv5ALl7uiN6uXaCyheHAsefSlNoZI bEFg02mV31s5GirLmmqtExkuuRNTCSbzOdHPCVFcv0Mw3lcvKLXz8ohHCtbCOIQb+E Nb/ZplQMRgHpw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 84E085C0253; Fri, 16 Apr 2021 10:26:29 -0700 (PDT) Date: Fri, 16 Apr 2021 10:26:29 -0700 From: "Paul E. McKenney" To: "Xu, Yanfei" Cc: rcu@vger.kernel.org, LKML Subject: Re: [Qestion] Is preempt_disable/enable needed in non-preemption code path Message-ID: <20210416172629.GH4212@paulmck-ThinkPad-P17-Gen-1> Reply-To: paulmck@kernel.org References: <20210415154326.GF4510@paulmck-ThinkPad-P17-Gen-1> <20210415170748.GA4212@paulmck-ThinkPad-P17-Gen-1> <3ef49985-68c9-277d-648c-53447ff602f4@windriver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3ef49985-68c9-277d-648c-53447ff602f4@windriver.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 16, 2021 at 06:51:10PM +0800, Xu, Yanfei wrote: > > > On 4/16/21 1:07 AM, Paul E. McKenney wrote: > > [Please note: This e-mail is from an EXTERNAL e-mail address] > > > > On Fri, Apr 16, 2021 at 12:18:42AM +0800, Xu, Yanfei wrote: > > > > > > > > > On 4/15/21 11:43 PM, Paul E. McKenney wrote: > > > > [Please note: This e-mail is from an EXTERNAL e-mail address] > > > > > > > > On Thu, Apr 15, 2021 at 11:04:05PM +0800, Xu, Yanfei wrote: > > > > > Hi experts, > > > > > > > > > > I am learning rcu mechanism and its codes. When looking at the > > > > > rcu_blocking_is_gp(), I found there is a pair preemption disable/enable > > > > > operation in non-preemption code path. And it has been a long time. I can't > > > > > understand why we need it? Is there some thing I missed? If not, can we > > > > > remove the unnecessary operation like blow? > > > > > > > > Good point, you are right that preemption is disabled anyway in that block > > > > of code. However, preempt_disable() and preempt_enable() also prevent the > > > > compiler from moving that READ_ONCE() around. So my question to you is > > > > whether it is safe to remove those statements entirely or whether they > > > > should instead be replaced by barrier() or similar. > > > > > > Thanks for your reply! :) > > > > > > Yes, preempt_disable() and preempt_enable() defined in !preemption are > > > barrier(). barrier can prevent from reordering that READ_ONCE(), but base on > > > my current understanding, volatile in READ_ONCE can also tell the compiler > > > not to reorder it. So, I think it's safe? > > > > Maybe. > > > > Please keep in mind that although the compiler is prohibited from > > reordering volatile accesses with each other, there is nothing stopping > > it from reordering volatile accesses with non-volatile accesses. > > Thanks for your patient explanation! > > I am trying to absorb what you said. Blow are my understanding: > 1. "the compiler is prohibited from reordering volatile accesses with each > other" means these situations: > int a; > foo() > { > for(;;) > READ_ONCE(a); > } > > or > > int a,b; > foo() > { > int c,d; > c = READ_ONCE(a); > d = READ_ONCE(b); > } Yes, in both cases the load instructions emitted for the READ_ONCE() macros must be emitted in order. The underlying hardware is free to reorder. > 2. "volatile accesses with non-volatile accesses" means d=b may happen > before c=READ_ONCE(a) : > int a; > foo() > { > int b = 2 > int c,d; > c = READ_ONCE(a); > d = b; > } > if we want to keep the ordering of volatile access "c=READ_ONCE(a)" and > non-volatile access "d=b", we should use stronger barrier like barrier(). Or an additional READ_ONCE() for b or a WRITE_ONCE() for d. But again, this would constrain only the compiler, not the hardware. But this wouldn't matter in most cases, because both b and d are local variables whose addresses were never taken. So someone would need to be using something crazy to poke into others' stacks for this to matter. > Hope I didn't misunderstand. It looks like you have most of it. > Back to rcu_blocking_is_gp(), I find this link today > https://www.spinics.net/lists/rcu/msg03985.html > With the content in this link, I still haven't got the meaning of these two > barrier(). I think I should learn knowledge about cpu-hotplug and things > which talked in the link first to make sure if I am missing something, and > then consult you. :) That sounds like a very good approach! Keep in mind that I am worried not just about the current state of the code and compilers, but also their possible future states. Thanx, Paul > Best regards, > Yanfei > > > > > Thanx, Paul > > > > > Best regards, > > > Yanfei > > > > > > > > > > > Thanx, Paul > > > > > > > > > diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c > > > > > index da6f5213fb74..c6d95a00715e 100644 > > > > > --- a/kernel/rcu/tree.c > > > > > +++ b/kernel/rcu/tree.c > > > > > @@ -3703,7 +3703,6 @@ static int rcu_blocking_is_gp(void) > > > > > if (IS_ENABLED(CONFIG_PREEMPTION)) > > > > > return rcu_scheduler_active == RCU_SCHEDULER_INACTIVE; > > > > > might_sleep(); /* Check for RCU read-side critical section. */ > > > > > - preempt_disable(); > > > > > /* > > > > > * If the rcu_state.n_online_cpus counter is equal to one, > > > > > * there is only one CPU, and that CPU sees all prior accesses > > > > > @@ -3718,7 +3717,6 @@ static int rcu_blocking_is_gp(void) > > > > > * Those memory barriers are provided by CPU-hotplug code. > > > > > */ > > > > > ret = READ_ONCE(rcu_state.n_online_cpus) <= 1; > > > > > - preempt_enable(); > > > > > return ret; > > > > > } > > > > > > > > > > > > > > > > > > > > Best regards, > > > > > Yanfei