Received: by 10.213.65.68 with SMTP id h4csp1088183imn; Tue, 27 Mar 2018 14:40:34 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+7m1r/0TiI5IEL/Dy87n4Odgk8ODGhtZcVPt11xgM2RVRuOCAphmLlm7HXiswl/6VzUR9+ X-Received: by 10.98.236.4 with SMTP id k4mr747876pfh.240.1522186834291; Tue, 27 Mar 2018 14:40:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522186834; cv=none; d=google.com; s=arc-20160816; b=vYB5p7iEwiRVGL5ySHqViVAqzPMWmIRO5fUtieO7EwdJRoOk/yZC1c+woD/Qe4MUcj qA4aOptOP1XkVGLsewrmSkF0phsAL4LnBtxGmqJcNY76zhRvCnCY7VL1H9TY/s5OqKcC pyFPX65f5G3AZsi/QXXENyHDpiEEzhh+z8ifRaOhk4qTjOpYMhj/XmHEvofoXhaRj3iO vUqtgzXI41TPPvsG/lLyNyG4ELOSLlc6GxDH2JGb1M7QbyaEItqKbxiypxlaHY1XX1Ei 3lzn0GGs49+qIN89ePf6H2pRgOM4YVs1OqBsDFk8CU2x9P+DLEeqFHogc0s9wv9Jo7dZ GA5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=SZa9i5JRRj8aBpuybc+uGx4qvAKBas8eRyQSGQWusOQ=; b=nPdnUqelk2hOeo7PWmwxlwGdhkHBCkRuV2M5jvPT34Dh6hV2YffT5iXmjw5y/tuDg8 qBjVNR6rE78A5omW82/hu00qNOjtd6GOi9Dc5/k8DUlcyMzcwoSeCAdqh5Cm7YKU3Ry/ ZqaMM6eVlwvFIRnPz61N2KcJ24EweT6je6ad6zA//eX2ZNh/pxJUUQt8kLGpz/FRTgli aHIw9XPZ+FFsBo/BAOaUex5R8oTqA/CJj/ibugAvadkYSZ7XHLYGvzUjM3ONF4W8/No1 7r9heS+JejJ92lTXciIOQ4GpRtiWtoq9dFWwyXJfgQrXcmhNqGdjWZS+tAlo/5+hs7pH AFrQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v23-v6si1973655plo.276.2018.03.27.14.40.18; Tue, 27 Mar 2018 14:40:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752096AbeC0Viq (ORCPT + 99 others); Tue, 27 Mar 2018 17:38:46 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:41424 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751167AbeC0Vio (ORCPT ); Tue, 27 Mar 2018 17:38:44 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9E54F40201A3; Tue, 27 Mar 2018 21:38:43 +0000 (UTC) Received: from llong.com (dhcp-17-164.bos.redhat.com [10.18.17.164]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5FC546B41B; Tue, 27 Mar 2018 21:38:43 +0000 (UTC) From: Waiman Long To: Ingo Molnar , Peter Zijlstra , Thomas Gleixner Cc: linux-kernel@vger.kernel.org, Davidlohr Bueso , Waiman Long Subject: [PATCH v3 1/2] locking/rwsem: Add DEBUG_RWSEMS to look for lock/unlock mismatches Date: Tue, 27 Mar 2018 17:38:09 -0400 Message-Id: <1522186690-10526-2-git-send-email-longman@redhat.com> In-Reply-To: <1522186690-10526-1-git-send-email-longman@redhat.com> References: <1522186690-10526-1-git-send-email-longman@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 27 Mar 2018 21:38:43 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 27 Mar 2018 21:38:43 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'longman@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For a rwsem, locking can either be exclusive or shared. The corresponding exclusive or shared unlock must be used. Otherwise, the protected data structures may get corrupted or the lock may be in an inconsistent state. In order to detect such anomaly, a new configuration option DEBUG_RWSEMS is added which can be enabled to look for such mismatches and print warnings that that happens. Signed-off-by: Waiman Long --- kernel/locking/rwsem.c | 4 ++++ kernel/locking/rwsem.h | 8 +++++++- lib/Kconfig.debug | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index f549c55..30465a2 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -117,6 +117,7 @@ int down_write_trylock(struct rw_semaphore *sem) void up_read(struct rw_semaphore *sem) { rwsem_release(&sem->dep_map, 1, _RET_IP_); + DEBUG_RWSEMS_WARN_ON(sem->owner != RWSEM_READER_OWNED); __up_read(sem); } @@ -129,6 +130,7 @@ void up_read(struct rw_semaphore *sem) void up_write(struct rw_semaphore *sem) { rwsem_release(&sem->dep_map, 1, _RET_IP_); + DEBUG_RWSEMS_WARN_ON(sem->owner != current); rwsem_clear_owner(sem); __up_write(sem); @@ -142,6 +144,7 @@ void up_write(struct rw_semaphore *sem) void downgrade_write(struct rw_semaphore *sem) { lock_downgrade(&sem->dep_map, _RET_IP_); + DEBUG_RWSEMS_WARN_ON(sem->owner != current); rwsem_set_reader_owned(sem); __downgrade_write(sem); @@ -211,6 +214,7 @@ int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass) void up_read_non_owner(struct rw_semaphore *sem) { + DEBUG_RWSEMS_WARN_ON(sem->owner != RWSEM_READER_OWNED); __up_read(sem); } diff --git a/kernel/locking/rwsem.h b/kernel/locking/rwsem.h index a883b8f..563a7bc 100644 --- a/kernel/locking/rwsem.h +++ b/kernel/locking/rwsem.h @@ -16,6 +16,12 @@ */ #define RWSEM_READER_OWNED ((struct task_struct *)1UL) +#ifdef CONFIG_DEBUG_RWSEMS +#define DEBUG_RWSEMS_WARN_ON(c) DEBUG_LOCKS_WARN_ON(c) +#else +#define DEBUG_RWSEMS_WARN_ON(c) +#endif + #ifdef CONFIG_RWSEM_SPIN_ON_OWNER /* * All writes to owner are protected by WRITE_ONCE() to make sure that @@ -41,7 +47,7 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem) * do a write to the rwsem cacheline when it is really necessary * to minimize cacheline contention. */ - if (sem->owner != RWSEM_READER_OWNED) + if (READ_ONCE(sem->owner) != RWSEM_READER_OWNED) WRITE_ONCE(sem->owner, RWSEM_READER_OWNED); } diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 64155e3..0958192 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1075,6 +1075,13 @@ config DEBUG_WW_MUTEX_SLOWPATH even a debug kernel. If you are a driver writer, enable it. If you are a distro, do not. +config DEBUG_RWSEMS + bool "RW Semaphore debugging: basic checks" + depends on DEBUG_KERNEL && RWSEM_SPIN_ON_OWNER + help + This feature allows mismatched rw semaphore locks and unlocks + to be detected and reported. + config DEBUG_LOCK_ALLOC bool "Lock debugging: detect incorrect freeing of live locks" depends on DEBUG_KERNEL && TRACE_IRQFLAGS_SUPPORT && STACKTRACE_SUPPORT && LOCKDEP_SUPPORT -- 1.8.3.1