From patchwork Mon Feb 18 12:39:14 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: 2157891 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 ABAFFDF25A for ; Mon, 18 Feb 2013 12:47:26 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1U7Q5P-0004Oq-NG; Mon, 18 Feb 2013 12:44:47 +0000 Received: from e28smtp07.in.ibm.com ([122.248.162.7]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1U7Q2B-0002Vi-19 for linux-arm-kernel@lists.infradead.org; Mon, 18 Feb 2013 12:41:29 +0000 Received: from /spool/local by e28smtp07.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 18 Feb 2013 18:08:39 +0530 Received: from d28dlp03.in.ibm.com (9.184.220.128) by e28smtp07.in.ibm.com (192.168.1.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 18 Feb 2013 18:08:36 +0530 Received: from d28relay04.in.ibm.com (d28relay04.in.ibm.com [9.184.220.61]) by d28dlp03.in.ibm.com (Postfix) with ESMTP id EAE83125804F for ; Mon, 18 Feb 2013 18:12:05 +0530 (IST) Received: from d28av03.in.ibm.com (d28av03.in.ibm.com [9.184.220.65]) by d28relay04.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r1ICfGf629950066 for ; Mon, 18 Feb 2013 18:11:16 +0530 Received: from d28av03.in.ibm.com (loopback [127.0.0.1]) by d28av03.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r1ICfBqk010674 for ; Mon, 18 Feb 2013 23:41:18 +1100 Received: from srivatsabhat.in.ibm.com (srivatsabhat.in.ibm.com [9.124.35.204] (may be forged)) by d28av03.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r1ICfAfj010604; Mon, 18 Feb 2013 23:41:10 +1100 From: "Srivatsa S. Bhat" Subject: [PATCH v6 07/46] percpu_rwlock: Allow writers to be readers, and add lockdep annotations To: tglx@linutronix.de, peterz@infradead.org, tj@kernel.org, oleg@redhat.com, paulmck@linux.vnet.ibm.com, rusty@rustcorp.com.au, mingo@kernel.org, akpm@linux-foundation.org, namhyung@kernel.org Date: Mon, 18 Feb 2013 18:09:14 +0530 Message-ID: <20130218123913.26245.7713.stgit@srivatsabhat.in.ibm.com> In-Reply-To: <20130218123714.26245.61816.stgit@srivatsabhat.in.ibm.com> References: <20130218123714.26245.61816.stgit@srivatsabhat.in.ibm.com> User-Agent: StGIT/0.14.3 MIME-Version: 1.0 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13021812-8878-0000-0000-000005EFB97F X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130218_074127_925726_31A0D866 X-CRM114-Status: GOOD ( 19.74 ) 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.7 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-arch@vger.kernel.org, linux@arm.linux.org.uk, nikunj@linux.vnet.ibm.com, linux-pm@vger.kernel.org, fweisbec@gmail.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, rostedt@goodmis.org, xiaoguangrong@linux.vnet.ibm.com, rjw@sisk.pl, sbw@mit.edu, wangyun@linux.vnet.ibm.com, srivatsa.bhat@linux.vnet.ibm.com, netdev@vger.kernel.org, vincent.guittot@linaro.org, walken@google.com, linuxppc-dev@lists.ozlabs.org, linux-arm-kernel@lists.infradead.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 CPU hotplug (which will be the first user of per-CPU rwlocks) has a special requirement with respect to locking: the writer, after acquiring the per-CPU rwlock for write, must be allowed to take the same lock for read, without deadlocking and without getting complaints from lockdep. In comparison, this is similar to what get_online_cpus()/put_online_cpus() does today: it allows a hotplug writer (who holds the cpu_hotplug.lock mutex) to invoke it without locking issues, because it silently returns if the caller is the hotplug writer itself. This can be easily achieved with per-CPU rwlocks as well (even without a "is this a writer?" check) by incrementing the per-CPU refcount of the writer immediately after taking the global rwlock for write, and then decrementing the per-CPU refcount before releasing the global rwlock. This ensures that any reader that comes along on that CPU while the writer is active (on that same CPU), notices the non-zero value of the nested counter and assumes that it is a nested read-side critical section and proceeds by just incrementing the refcount. Thus we prevent the reader from taking the global rwlock for read, which prevents the writer from deadlocking itself. Add that support and teach lockdep about this special locking scheme so that it knows that this sort of usage is valid. Also add the required lockdep annotations to enable it to detect common locking problems with per-CPU rwlocks. Cc: David Howells Signed-off-by: Srivatsa S. Bhat --- lib/percpu-rwlock.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/percpu-rwlock.c b/lib/percpu-rwlock.c index ed36531..bf95e40 100644 --- a/lib/percpu-rwlock.c +++ b/lib/percpu-rwlock.c @@ -102,6 +102,10 @@ void percpu_read_lock_irqsafe(struct percpu_rwlock *pcpu_rwlock) if (likely(!writer_active(pcpu_rwlock))) { this_cpu_inc(pcpu_rwlock->rw_state->reader_refcnt); + + /* Pretend that we take global_rwlock for lockdep */ + rwlock_acquire_read(&pcpu_rwlock->global_rwlock.dep_map, + 0, 0, _RET_IP_); } else { /* Writer is active, so switch to global rwlock. */ @@ -126,6 +130,12 @@ void percpu_read_lock_irqsafe(struct percpu_rwlock *pcpu_rwlock) if (!writer_active(pcpu_rwlock)) { this_cpu_inc(pcpu_rwlock->rw_state->reader_refcnt); read_unlock(&pcpu_rwlock->global_rwlock); + + /* + * Pretend that we take global_rwlock for lockdep + */ + rwlock_acquire_read(&pcpu_rwlock->global_rwlock.dep_map, + 0, 0, _RET_IP_); } } @@ -162,6 +172,13 @@ void percpu_read_unlock_irqsafe(struct percpu_rwlock *pcpu_rwlock) */ smp_mb(); this_cpu_dec(pcpu_rwlock->rw_state->reader_refcnt); + + /* + * Since this is the last decrement, it is time to pretend + * to lockdep that we are releasing the read lock. + */ + rwlock_release(&pcpu_rwlock->global_rwlock.dep_map, + 1, _RET_IP_); } else { read_unlock(&pcpu_rwlock->global_rwlock); } @@ -200,6 +217,16 @@ void percpu_write_lock_irqsave(struct percpu_rwlock *pcpu_rwlock, smp_mb(); /* Complete the wait-for-readers, before taking the lock */ write_lock_irqsave(&pcpu_rwlock->global_rwlock, *flags); + + /* + * It is desirable to allow the writer to acquire the percpu-rwlock + * for read (if necessary), without deadlocking or getting complaints + * from lockdep. To achieve that, just increment the reader_refcnt of + * this CPU - that way, any attempt by the writer to acquire the + * percpu-rwlock for read, will get treated as a case of nested percpu + * reader, which is safe, from a locking perspective. + */ + this_cpu_inc(pcpu_rwlock->rw_state->reader_refcnt); } void percpu_write_unlock_irqrestore(struct percpu_rwlock *pcpu_rwlock, @@ -207,6 +234,12 @@ void percpu_write_unlock_irqrestore(struct percpu_rwlock *pcpu_rwlock, { unsigned int cpu; + /* + * Undo the special increment that we had done in the write-lock path + * in order to allow writers to be readers. + */ + this_cpu_dec(pcpu_rwlock->rw_state->reader_refcnt); + /* Complete the critical section before clearing ->writer_signal */ smp_mb();