From patchwork Mon Feb 18 17:56:22 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Srivatsa S. Bhat" X-Patchwork-Id: 2159771 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork2.kernel.org (Postfix) with ESMTP id 71B81DF25A for ; Mon, 18 Feb 2013 18:03:00 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1U7Uz3-0008PW-1t; Mon, 18 Feb 2013 17:58:33 +0000 Received: from e28smtp05.in.ibm.com ([122.248.162.5]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1U7Uyz-0008Ns-23 for linux-arm-kernel@lists.infradead.org; Mon, 18 Feb 2013 17:58:30 +0000 Received: from /spool/local by e28smtp05.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 18 Feb 2013 23:26:26 +0530 Received: from d28dlp03.in.ibm.com (9.184.220.128) by e28smtp05.in.ibm.com (192.168.1.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 18 Feb 2013 23:26:24 +0530 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by d28dlp03.in.ibm.com (Postfix) with ESMTP id 734881258054 for ; Mon, 18 Feb 2013 23:29:07 +0530 (IST) Received: from d28av05.in.ibm.com (d28av05.in.ibm.com [9.184.220.67]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r1IHwIYm32964714 for ; Mon, 18 Feb 2013 23:28:18 +0530 Received: from d28av05.in.ibm.com (loopback [127.0.0.1]) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r1IHwKa3029208 for ; Tue, 19 Feb 2013 04:58:21 +1100 Received: from srivatsabhat.in.ibm.com ([9.79.186.149]) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r1IHwJje029187; Tue, 19 Feb 2013 04:58:19 +1100 Message-ID: <51226B46.9080707@linux.vnet.ibm.com> Date: Mon, 18 Feb 2013 23:26:22 +0530 From: "Srivatsa S. Bhat" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120828 Thunderbird/15.0 MIME-Version: 1.0 To: Michel Lespinasse Subject: Re: [PATCH v6 04/46] percpu_rwlock: Implement the core design of Per-CPU Reader-Writer Locks References: <20130218123714.26245.61816.stgit@srivatsabhat.in.ibm.com> <20130218123856.26245.46705.stgit@srivatsabhat.in.ibm.com> <5122551E.1080703@linux.vnet.ibm.com> In-Reply-To: <5122551E.1080703@linux.vnet.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13021817-8256-0000-0000-000006433844 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130218_125829_668205_A9A0274D X-CRM114-Status: GOOD ( 21.90 ) X-Spam-Score: -1.9 (-) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-1.9 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/, medium trust [122.248.162.5 listed in list.dnswl.org] 3.0 KHOP_BIG_TO_CC Sent to 10+ recipients instaed of Bcc or a list -0.7 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: linux-doc@vger.kernel.org, peterz@infradead.org, fweisbec@gmail.com, linux-kernel@vger.kernel.org, namhyung@kernel.org, mingo@kernel.org, linux-arch@vger.kernel.org, linux@arm.linux.org.uk, xiaoguangrong@linux.vnet.ibm.com, wangyun@linux.vnet.ibm.com, paulmck@linux.vnet.ibm.com, nikunj@linux.vnet.ibm.com, linux-pm@vger.kernel.org, rusty@rustcorp.com.au, rostedt@goodmis.org, rjw@sisk.pl, vincent.guittot@linaro.org, tglx@linutronix.de, linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, oleg@redhat.com, sbw@mit.edu, tj@kernel.org, akpm@linux-foundation.org, linuxppc-dev@lists.ozlabs.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-arm-kernel-bounces@lists.infradead.org Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org On 02/18/2013 09:51 PM, Srivatsa S. Bhat wrote: > Hi Michel, > > On 02/18/2013 09:15 PM, Michel Lespinasse wrote: >> Hi Srivasta, >> >> I admit not having followed in detail the threads about the previous >> iteration, so some of my comments may have been discussed already >> before - apologies if that is the case. >> >> On Mon, Feb 18, 2013 at 8:38 PM, Srivatsa S. Bhat >> wrote: >>> Reader-writer locks and per-cpu counters are recursive, so they can be >>> used in a nested fashion in the reader-path, which makes per-CPU rwlocks also >>> recursive. Also, this design of switching the synchronization scheme ensures >>> that you can safely nest and use these locks in a very flexible manner. [...] >>> void percpu_write_lock(struct percpu_rwlock *pcpu_rwlock) >>> { >>> + unsigned int cpu; >>> + >>> + /* >>> + * Tell all readers that a writer is becoming active, so that they >>> + * start switching over to the global rwlock. >>> + */ >>> + for_each_possible_cpu(cpu) >>> + per_cpu_ptr(pcpu_rwlock->rw_state, cpu)->writer_signal = true; >> >> I don't see anything preventing a race with the corresponding code in >> percpu_write_unlock() that sets writer_signal back to false. Did I >> miss something here ? It seems to me we don't have any guarantee that >> all writer signals will be set to true at the end of the loop... >> > > Ah, thanks for pointing that out! IIRC Oleg had pointed this issue in the last > version, but back then, I hadn't fully understood what he meant. Your > explanation made it clear. I'll work on fixing this. > We can fix this by using the simple patch (untested) shown below. The alternative would be to acquire the rwlock for write, update the ->writer_signal values, release the lock, wait for readers to switch, again acquire the rwlock for write with interrupts disabled etc... which makes it kinda messy, IMHO. So I prefer the simple version shown below. diff --git a/lib/percpu-rwlock.c b/lib/percpu-rwlock.c index bf95e40..64ccd3f 100644 --- a/lib/percpu-rwlock.c +++ b/lib/percpu-rwlock.c @@ -50,6 +50,12 @@ (__this_cpu_read((pcpu_rwlock)->rw_state->writer_signal)) +/* + * Spinlock to synchronize access to the writer's data-structures + * (->writer_signal) from multiple writers. + */ +static DEFINE_SPINLOCK(writer_side_lock); + int __percpu_init_rwlock(struct percpu_rwlock *pcpu_rwlock, const char *name, struct lock_class_key *rwlock_key) { @@ -191,6 +197,8 @@ void percpu_write_lock_irqsave(struct percpu_rwlock *pcpu_rwlock, { unsigned int cpu; + spin_lock(&writer_side_lock); + /* * Tell all readers that a writer is becoming active, so that they * start switching over to the global rwlock. @@ -252,5 +260,6 @@ void percpu_write_unlock_irqrestore(struct percpu_rwlock *pcpu_rwlock, per_cpu_ptr(pcpu_rwlock->rw_state, cpu)->writer_signal = false; write_unlock_irqrestore(&pcpu_rwlock->global_rwlock, *flags); + spin_unlock(&writer_side_lock); }