Received: by 10.213.65.68 with SMTP id h4csp489904imn; Wed, 28 Mar 2018 07:23:42 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+NEtEnmY7FsSCAlxr4Goa6iw15AqhM97E6Amnvj/yGGaeF/+juMp/RgIC+iCu8W9qI7D6h X-Received: by 10.98.31.216 with SMTP id l85mr3071152pfj.80.1522247022304; Wed, 28 Mar 2018 07:23:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522247022; cv=none; d=google.com; s=arc-20160816; b=wkbekY9YB+k5o/vRQZwPfQOlCcOLZatHVKAfNNLjNalMwwoTbWM3pywqqwVbgzy91i tKNEz4o0wEUzqB7zmrFsPD064c2EWN8swudj41RZ43fU715DuH+SNU7TvVKXlG4XUx1t iNWzUawW5hfUqFd5CyY0Jt16dggz9Gze9Wub0PPIm04k/kGYbce8KXRk7dsMjZiaMAog ctA67i8F2wDJ8ccKjBd2QdNim+QRtDWFBhiWVGYbSoABMSUi3oqNkR9TuJfZ9Wvy2Ro3 kK1QC5Tolg+VkJQRCP8CKtwo72KQX/uiyL0C5aSVmU534yWnoXCHD4jk2Yek+c7EKcWO rXMQ== 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=IHVQR/qYooQfxQ6C4ySsd3AwjhXnMJVmknBb7yFah4M=; b=x1juctWQyCyWYzegSf/Z319nH6yRkd0QahwsbXrR/wm3zU6ZM3JbEXNTTp6A5Gfr1n bmJApz10xYG78v3P0NOEkSalchX2PmzdgetXWRqfBagoKcMr4taxlMc+ErOSEH4HhzKN 8du1ApvWgv8V0vapIrNGuJT/UeG8ywYVpx3WDAN2BeYsTdIQRpZ+ouZ29bUR1jbk8bm9 r+2lxj27rVGtJoO/K40EQdRgcMDTk3bCQ+M2M538YSB6HrC+EL4mSZYBTPEArgm1hJjo cjlaXRhpkL1L2lsKhmGeywPwi9mjUpl6T4H9ejqo7hx41jWBYgu4+iAWAvPTJovlhhhR qe4Q== 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 o33-v6si3678095plb.429.2018.03.28.07.23.27; Wed, 28 Mar 2018 07:23:42 -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 S1753759AbeC1OVx (ORCPT + 99 others); Wed, 28 Mar 2018 10:21:53 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:37290 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753531AbeC1OVe (ORCPT ); Wed, 28 Mar 2018 10:21:34 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9F159407020E; Wed, 28 Mar 2018 14:21:33 +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 5D0562024CA2; Wed, 28 Mar 2018 14:21:33 +0000 (UTC) From: Waiman Long To: Ingo Molnar , Peter Zijlstra , Thomas Gleixner Cc: linux-kernel@vger.kernel.org, Davidlohr Bueso , Waiman Long Subject: [PATCH v4 1/2] locking/rwsem: Add DEBUG_RWSEMS to look for lock/unlock mismatches Date: Wed, 28 Mar 2018 10:20:51 -0400 Message-Id: <1522246852-17501-2-git-send-email-longman@redhat.com> In-Reply-To: <1522246852-17501-1-git-send-email-longman@redhat.com> References: <1522246852-17501-1-git-send-email-longman@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Wed, 28 Mar 2018 14:21:33 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Wed, 28 Mar 2018 14:21:33 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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 | 8 ++++++++ 3 files changed, 19 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..6aad28c 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 @@ -1097,6 +1104,7 @@ config PROVE_LOCKING select DEBUG_SPINLOCK select DEBUG_MUTEXES select DEBUG_RT_MUTEXES if RT_MUTEXES + select DEBUG_RWSEMS if RWSEM_SPIN_ON_OWNER select DEBUG_LOCK_ALLOC select TRACE_IRQFLAGS default n -- 1.8.3.1