Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp2912933rwb; Fri, 20 Jan 2023 08:50:23 -0800 (PST) X-Google-Smtp-Source: AMrXdXs1/2tTVnIyKbBBHzjgbRDdDODL417h9O7meFseI9Hnf0RBrmKlS7kDjpCOxUbjmXHm9Yxz X-Received: by 2002:aa7:dc10:0:b0:483:b15a:3206 with SMTP id b16-20020aa7dc10000000b00483b15a3206mr5329519edu.23.1674233423550; Fri, 20 Jan 2023 08:50:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674233423; cv=none; d=google.com; s=arc-20160816; b=yUkGwroouYENML3cnkJJNusix2xKzJxP7kH3GePfA9wb+IOwk1Hsagl2lw6UZwX1Lg SE28G8/gpxGFbTYa0xYHEO/ig/cOPqRcUf+nGc4T4+zbC9rGzYQMjSdCKX67kSjUjrlM auROuA08xXJfNhdoSNQkhbmxNyuI/8Vbz+uIxsIH6KyLdiZ0PQnCOXfWuNptWALEkspH ebmPaffNJyvR6wtxiz8tkRq2MgRhbnnWoAUiEqsBMjFTaVzcR8wWN3uFm6DExMj5LxzF HVfB4rHfJAH/+i60GvtBFjO8wsgJcPjJVRWMfu4sTL7hr6QV6ei9k/fmq5NHy0AQNHYT Awxg== 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:message-id:subject:cc:to:from:date; bh=q8/O6ow2bI9nmbzeM9A/ktVbaQLGUow+imjMZpibPTQ=; b=CqJi+iPEy9SHsg1UELZcgVswGFjWMKkXKA2HbFWbhQHuLmHzv044KcqV+pgPBVTiWq CRrL1y4nNF4rHKYOD6twu0TuwPKkZmcZh3XyfK+QLdsBP7OLPDqJdHzkQ+ABpbn0cYqD 7Ch9FJgqqX1ihryQbQX7jT14PREEvxHft6E2HLQP8mY/bV4RxMgiQxwZvipZl39It/dq kReJbOlV6IAyc7fo46giwBA/SiccgP92hlOWLSchbFQEEAJ7kWZcjL2ViSmrZo0G/kwg C+JWbvRM6tKspNVTMNHbIGupUsBYbZCkNDYQCvqtsIuj9lpwmqmWwEkcE05cFcXV/2jg 8QPQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s1-20020a056402164100b0049eed9a1a93si932500edx.576.2023.01.20.08.50.11; Fri, 20 Jan 2023 08:50:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229880AbjATQBJ (ORCPT + 49 others); Fri, 20 Jan 2023 11:01:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229461AbjATQBI (ORCPT ); Fri, 20 Jan 2023 11:01:08 -0500 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id ABF01707C4 for ; Fri, 20 Jan 2023 08:01:04 -0800 (PST) Received: (qmail 37304 invoked by uid 1000); 20 Jan 2023 11:01:03 -0500 Date: Fri, 20 Jan 2023 11:01:03 -0500 From: Alan Stern To: "Paul E. McKenney" Cc: Andrea Parri , Jonas Oberhauser , Peter Zijlstra , will , "boqun.feng" , npiggin , dhowells , "j.alglave" , "luc.maranget" , akiyks , dlustig , joel , urezki , quic_neeraju , frederic , Kernel development list Subject: Re: Internal vs. external barriers (was: Re: Interesting LKMM litmus test) Message-ID: References: <20230117151416.GI2948950@paulmck-ThinkPad-P17-Gen-1> <20230117174308.GK2948950@paulmck-ThinkPad-P17-Gen-1> <20230118035041.GQ2948950@paulmck-ThinkPad-P17-Gen-1> <20230118200601.GH2948950@paulmck-ThinkPad-P17-Gen-1> <20230119000214.GM2948950@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230119000214.GM2948950@paulmck-ThinkPad-P17-Gen-1> X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS autolearn=no 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 On Wed, Jan 18, 2023 at 04:02:14PM -0800, Paul E. McKenney wrote: > There are pairs of per-CPU counters. One pair (->srcu_lock_count[]) > counts the number of srcu_down_read() operations that took place on > that CPU and another pair (->srcu_unlock_count[]) counts the number > of srcu_down_read() operations that took place on that CPU. There is > an ->srcu_idx that selects which of the ->srcu_lock_count[] elements > should be incremented by srcu_down_read(). Of course, srcu_down_read() > returns the value of ->srcu_idx that it used so that the matching > srcu_up_read() will use that same index when incrementing its CPU's > ->srcu_unlock_count[]. > > Grace periods go something like this: > > 1. Sum up the ->srcu_unlock_count[!ssp->srcu_idx] counters. > > 2. smp_mb(). > > 3. Sum up the ->srcu_unlock_count[!ssp->srcu_idx] counters. Presumably you meant to write "lock" here, not "unlock". > > 4. If the sums are not equal, retry from #1. > > 5. smp_mb(). > > 6. WRITE_ONCE(ssp->srcu_idx, !ssp->srcu_idx); > > 7. smp_mb(). > > 8. Same loop as #1-4. > > So similar to r/w semaphores, but with two separate distributed counts. > This means that the number of readers need not go to zero at any given > point in time, consistent with the need to wait only on old readers. Reasoning from first principles, I deduce the following: You didn't describe exactly how srcu_down_read() and srcu_up_read() work. Evidently the unlock increment in srcu_up_read() should have release semantics, to prevent accesses from escaping out the end of the critical section. But the lock increment in srcu_down_read() has to be stronger than an acquire; to prevent accesses in the critical section escaping out the start, the increment has to be followed by smp_mb(). The smp_mb() fences in steps 5 and 7 appear to be completely unnecessary. Provided an smp_mb() is added at the very start and end of the grace period, the memory barrier in step 2 and its copy in step 8 can be demoted to smp_rmb(). These changes would be small optimizations at best, and you may consider them unimportant in view of the fact that grace periods often last quite a long time. Alan