Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755396AbcK3AgI (ORCPT ); Tue, 29 Nov 2016 19:36:08 -0500 Received: from mail-wj0-f195.google.com ([209.85.210.195]:35206 "EHLO mail-wj0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755208AbcK3AgA (ORCPT ); Tue, 29 Nov 2016 19:36:00 -0500 From: Chris Wilson To: linux-kernel@vger.kernel.org Cc: Chris Wilson , Peter Zijlstra , Maarten Lankhorst , =?UTF-8?q?Nicolai=20H=C3=A4hnle?= Subject: [PATCH 1/4] locking: Begin kselftests for ww_mutex Date: Wed, 30 Nov 2016 00:35:45 +0000 Message-Id: <20161130003548.22266-1-chris@chris-wilson.co.uk> X-Mailer: git-send-email 2.10.2 In-Reply-To: <1480335612-12069-1-git-send-email-nhaehnle@gmail.com> References: <1480335612-12069-1-git-send-email-nhaehnle@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4778 Lines: 186 Signed-off-by: Chris Wilson Cc: Peter Zijlstra Cc: Maarten Lankhorst Cc: Nicolai Hähnle --- kernel/locking/Makefile | 1 + kernel/locking/test-ww_mutex.c | 137 +++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 10 +++ 3 files changed, 148 insertions(+) create mode 100644 kernel/locking/test-ww_mutex.c diff --git a/kernel/locking/Makefile b/kernel/locking/Makefile index 6f88e352cd4f..760158d9d98d 100644 --- a/kernel/locking/Makefile +++ b/kernel/locking/Makefile @@ -28,3 +28,4 @@ obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem-xadd.o obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o +obj-$(CONFIG_WW_MUTEX_SELFTEST) += test-ww_mutex.o diff --git a/kernel/locking/test-ww_mutex.c b/kernel/locking/test-ww_mutex.c new file mode 100644 index 000000000000..e94b807e06c2 --- /dev/null +++ b/kernel/locking/test-ww_mutex.c @@ -0,0 +1,137 @@ +/* + * Module-based API test facility for ww_mutexes + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you can access it online at + * http://www.gnu.org/licenses/gpl-2.0.html. + */ + +#include +#include +#include +#include +#include + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Intel Corporation"); + +static DEFINE_WW_CLASS(ww_class); + +struct test_mutex { + struct work_struct work; + struct ww_mutex mutex; + struct completion ready, go, done; + unsigned flags; +#define TEST_AB_SPIN BIT(0) +#define TEST_AB_TRY BIT(1) +}; + +static void test_mutex_work(struct work_struct *work) +{ + struct test_mutex *mtx = container_of(work, typeof(*mtx), work); + + complete(&mtx->ready); + wait_for_completion(&mtx->go); + + if (mtx->flags & TEST_AB_TRY) { + while (!ww_mutex_trylock(&mtx->mutex)) + cpu_relax(); + } else { + ww_mutex_lock(&mtx->mutex, NULL); + } + complete(&mtx->done); + ww_mutex_unlock(&mtx->mutex); +} + +static int __test_mutex(unsigned flags) +{ + struct test_mutex mtx; + int ret; + + ww_mutex_init(&mtx.mutex, &ww_class); + INIT_WORK_ONSTACK(&mtx.work, test_mutex_work); + init_completion(&mtx.ready); + init_completion(&mtx.go); + init_completion(&mtx.done); + mtx.flags = flags; + + schedule_work(&mtx.work); + + wait_for_completion(&mtx.ready); + ww_mutex_lock(&mtx.mutex, NULL); + complete(&mtx.go); + if (flags & TEST_AB_SPIN) { + unsigned long timeout = jiffies + HZ; + + ret = 0; + do { + if (completion_done(&mtx.done)) { + ret = -EINVAL; + break; + } + cpu_relax(); + } while (time_before(jiffies, timeout)); + } else { + ret = wait_for_completion_timeout(&mtx.done, HZ); + } + ww_mutex_unlock(&mtx.mutex); + + if (ret) { + pr_err("%s(flags=%x): mutual exclusion failure\n", + __func__, flags); + ret = -EINVAL; + } + + flush_work(&mtx.work); + destroy_work_on_stack(&mtx.work); + return ret; +} + +static int test_mutex(void) +{ + unsigned int modes[] = { + 0, + TEST_AB_SPIN, + TEST_AB_TRY, + TEST_AB_SPIN | TEST_AB_TRY, + }; + int i; + + for (i = 0; i < ARRAY_SIZE(modes); i++) { + int ret; + + ret = __test_mutex(modes[i]); + if (ret) + return ret; + } + + return 0; +} + +static int __init test_ww_mutex_init(void) +{ + int ret; + + ret = test_mutex(); + if (ret) + return ret; + + return 0; +} + +static void __exit test_ww_mutex_exit(void) +{ +} + +module_init(test_ww_mutex_init); +module_exit(test_ww_mutex_exit); diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index a6c8db1d62f6..5ffe7fd34c50 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1161,6 +1161,16 @@ config LOCK_TORTURE_TEST Say M if you want these torture tests to build as a module. Say N if you are unsure. +config WW_MUTEX_SELFTEST + tristate "Wait/wound mutex selftests" + select DEBUG_WW_MUTEX_SLOWPATH + help + This option provides a kernel module that runs tests on the + on the struct ww_mutex locking API. + + Say M if you want these self tests to build as a module. + Say N if you are unsure. + endmenu # lock debugging config TRACE_IRQFLAGS -- 2.10.2