Received: by 10.213.65.68 with SMTP id h4csp1678018imn; Mon, 26 Mar 2018 12:20:05 -0700 (PDT) X-Google-Smtp-Source: AG47ELtt1zo8ygvDoIuMBcNMq6KTCwOfrgNyjxcFGsMEKUjB3gFSrSW14ULSytbxajrnRX+D+DHl X-Received: by 2002:a17:902:3381:: with SMTP id b1-v6mr23762895plc.214.1522092005402; Mon, 26 Mar 2018 12:20:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522092005; cv=none; d=google.com; s=arc-20160816; b=ah/o0+NGxefE9mcGg0IkYItbzg4G7yNbrFeMS4QykV5A8yYERR9ZaLfapHwhUtAZzo 3pjVNm5J5cuaNRlnVoKiQODLlfO5CyUT5S/Nsdt3rahAtDe8sexJPgyfmReoWBWlBxb4 NhkkDp1ui0rd5x3lVW9nMRXqN9FgGVMvIHEa0Q5u/jHDaz3S/yD+CWhb707x7ktIVC0x KxzyRygxUIUmnQzxFDP8u3nWB7/xhqBAmkhbANFWmNEAzeUyHLTF+zaTFpx0mLbyWjQP xXArdhMMysMAluYYiW/vf0KBnk1zzvz9oD0LHYySTtBCfGPB6fMdYFzk3NIHnKNx56Jr YmHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=F+EUxjvT4Bbt6L/0EjTowt7TH++vNmXzXMgI4LEnkW0=; b=WY/aqoL1eVPw+dq27a3A063mFTqp02SVGW7BoM3e0jWroHZWecFRFe5PQE1yh6HpIG tmvxhcw9Po9yYa9pu6v7XIweba03ITohCITazhlwgoVjZz8HW48fgGXibMV52NPaTJRF gD/Hx2CfHUI2xZcGJYl7ILguC37vXuwb5kbLhq8l/O0OBo5LpK3l3LU0q1BzyXv9v+2+ 8ruQ3xJElqwXyxrPEMNSRqXfQHIYDl0UMByy8/zJ98BPwJ7TG2kow6t4sGkfXuqGtvmp gACj6wsLXMgMd+Vj964iAE3apGEXU3EwY0C/MIIeBiGoDD9wEL94Qxot3Ly4/Lkj9u8w vMRg== 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 f19-v6si2446419plr.411.2018.03.26.12.19.50; Mon, 26 Mar 2018 12:20:05 -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 S1752121AbeCZTRi (ORCPT + 99 others); Mon, 26 Mar 2018 15:17:38 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:33556 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751157AbeCZTRh (ORCPT ); Mon, 26 Mar 2018 15:17:37 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5CD0C7705F; Mon, 26 Mar 2018 19:17:37 +0000 (UTC) Received: from llong.com (dhcp-17-75.bos.redhat.com [10.18.17.75]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2ACB72166BC7; Mon, 26 Mar 2018 19:17:37 +0000 (UTC) From: Waiman Long To: Ingo Molnar , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, Waiman Long Subject: [PATCH v2] locking/rwsem: Add DEBUG_RWSEMS to look for lock/unlock mismatches Date: Mon, 26 Mar 2018 15:17:28 -0400 Message-Id: <1522091848-18426-1-git-send-email-longman@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Mon, 26 Mar 2018 19:17:37 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Mon, 26 Mar 2018 19:17:37 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.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 v2: - Fix typo --- 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