Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp949514pxb; Thu, 15 Apr 2021 10:08:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzBfmUeDLm6n3Rlh6bNne0jkqRXEUU4FTJuMeaTv81hDmJ/tNPmJsgO4iyT1CbCukwzLlpi X-Received: by 2002:a17:902:b68a:b029:e9:7643:6335 with SMTP id c10-20020a170902b68ab02900e976436335mr4999895pls.2.1618506523994; Thu, 15 Apr 2021 10:08:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618506523; cv=none; d=google.com; s=arc-20160816; b=eCgqvBMozZUcu47NWhZeOCSXuqzw+7MMcVkLAnnru644pQAtE/iwECwPiFotLdI3rM 5DCv1PGssbUgWj0RBFIVolgwFbMDYIq5p4vQdrss3dtALcTjRCLgW6/EXNXjxhfQJYZr utt+71bR4n40g0HqzgcSkFTgvhOiujQWt+V5vLHYByx1njLWPRuF64yCV3LpfI2EA+Wt omfUAdLuPjXoPZ3PT3AIVQzdNGFleWS+v0lK7vDm3ScG3dSi6K5TrzwmoRblosSctNW7 xHvSWxYeERulN/t5q84SubrmSyPG8MhVRg+XAg9OQH3YUrjMbc2S8+UJ/LIcq8W7jtrf rw+w== 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=NGoNd1yvCcD92V3Jnn0GoNASbydDeQn8GbxRMU8shUU=; b=cgXj89orDStyPmPoeV0CQS5YGhdIidlI/EKQv+4WLyoscV5WoFn0KeisBVy5KUpm9a KR0FkYRuXwf4jj8P7vwLjFLHPwRhVBcOVfkCrkrQQnZu5ahu7YfqQ9keCueUdAIPRZM6 bzj6UG8RtVE7vBRA7D4VDJ/e6NjAukKC3ztBQ2GjVymiEXOnIjWgPT3ZWHZf3DrwbnCM mO5H9ZYIfqQBjDRSRL3999KtoMPVUqE4ZPS1l1xiDjEoEVh4xRBc9p9GOOmYrRapuvjc nMs/G+N4lZwxXriSfYBfagmJwJgnA1OD88vLG+iZoRmxaUKdvrEUt2a2gqyfOm61l3EC wUeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WqnAX6pR; 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 e2si3906711plo.6.2021.04.15.10.08.30; Thu, 15 Apr 2021 10:08:43 -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=WqnAX6pR; 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 S233992AbhDORIQ (ORCPT + 99 others); Thu, 15 Apr 2021 13:08:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:49768 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232759AbhDORIL (ORCPT ); Thu, 15 Apr 2021 13:08:11 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7E0C06109D; Thu, 15 Apr 2021 17:07:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1618506468; bh=OJNggdW0+1ja/X+OPtr4SVyl/OXXtG6ShVmzItcJE8s=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=WqnAX6pRvPq0Kuxl0FqebuxdehDVaAo7/loIylqDYOcOf35B+X6WPkMSTsQ2DshWA yQn/7c+i0ZdlVXY+XsZKMZy/7KxfCdDhRtdMWzc2fV9P3W1EitKSg+WN8WcSoBP0x0 59CojOZCupTnAubtPCL/kqJKPsu8xnv548BdE2089TK9CoFOYgujPK+n/y1GslXYx6 zXpyjr3fJPswF2vAr61kj9wqPYQGDDEF7sqdP3a/3GQySY5lw/4EAtCZJ9rJSjyRct MpVF3EehhiEq+kNBphN5bbro+4puTu0TZFxqlZUEp19Y/aMjRBPb8UrikFIko1EUIY s61WMTLuMHzeA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 1F2775C00F4; Thu, 15 Apr 2021 10:07:48 -0700 (PDT) Date: Thu, 15 Apr 2021 10:07:48 -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: <20210415170748.GA4212@paulmck-ThinkPad-P17-Gen-1> Reply-To: paulmck@kernel.org References: <20210415154326.GF4510@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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