Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754605AbcKUMra (ORCPT ); Mon, 21 Nov 2016 07:47:30 -0500 Received: from mx1.redhat.com ([209.132.183.28]:37928 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754518AbcKUMr2 (ORCPT ); Mon, 21 Nov 2016 07:47:28 -0500 Date: Mon, 21 Nov 2016 13:47:23 +0100 From: Oleg Nesterov To: Peter Zijlstra Cc: Davidlohr Bueso , mingo@kernel.org, john.stultz@linaro.org, dimitrysh@google.com, linux-kernel@vger.kernel.org, Davidlohr Bueso Subject: Re: [PATCH 3/3] locking/percpu-rwsem: Avoid unnecessary writer wakeups Message-ID: <20161121124722.GA1459@redhat.com> References: <1479495277-9075-1-git-send-email-dave@stgolabs.net> <1479495277-9075-4-git-send-email-dave@stgolabs.net> <20161121122343.GA635@redhat.com> <20161121122935.GD3092@twins.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20161121122935.GD3092@twins.programming.kicks-ass.net> User-Agent: Mutt/1.5.18 (2008-05-17) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 21 Nov 2016 12:47:28 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1415 Lines: 42 On 11/21, Peter Zijlstra wrote: > > On Mon, Nov 21, 2016 at 01:23:44PM +0100, Oleg Nesterov wrote: > > > int __percpu_init_rwsem(struct percpu_rw_semaphore *sem, > > > const char *name, struct lock_class_key *rwsem_key) > > > { > > > @@ -103,41 +141,11 @@ void __percpu_up_read(struct percpu_rw_semaphore *sem) > > > __this_cpu_dec(*sem->read_count); > > > > > > /* Prod writer to recheck readers_active */ > > > - swake_up(&sem->writer); > > > + if (__readers_active_check(sem)) > > > + swake_up(&sem->writer); > > > > Suppose we have 2 active readers which call __percpu_up_read() at the same > > time and the pending writer sleeps. > > > > What guarantees that one of these readers will observe per_cpu_sum() == 0 ? > > They both can read the old value of the remote per-cpu counter, no? > > In particular, you're thinking of what provides the guarantee that the > woken CPU observes the same state the wakee saw? No, no, I meant that afaics both readers can see per_cpu_sum() != 0 and thus the writer won't be woken up. Till the next down_read/up_read. Suppose that we have 2 CPU's, both counters == 1, both readers decrement. its counter at the same time. READER_ON_CPU_0 READER_ON_CPU_1 --ctr_0; --ctr_1; if (ctr_0 + ctr_1) if (ctr_0 + ctr_1) wakeup(); wakeup(); Why we can't miss a wakeup? This patch doesn't even add a barrier, but I think wmb() won't be enough anyway. Oleg.