[PATCH v2 3/8] locking: Begin kselftests for ww_mutex
v2: Add a couple more variations for degenerate ww_mutex, decrease timeouts Signed-off-by: Chris WilsonCc: Peter Zijlstra Cc: Ingo Molnar Cc: Maarten Lankhorst Cc: Nicolai Hähnle --- kernel/locking/Makefile| 1 + kernel/locking/test-ww_mutex.c | 140 + lib/Kconfig.debug | 12 3 files changed, 153 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 ..472d5b1f303f --- /dev/null +++ b/kernel/locking/test-ww_mutex.c @@ -0,0 +1,140 @@ +/* + * 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 + +static DEFINE_WW_CLASS(ww_class); + +struct test_mutex { + struct work_struct work; + struct ww_mutex mutex; + struct completion ready, go, done; + unsigned int flags; +}; + +#define TEST_MTX_SPIN BIT(0) +#define TEST_MTX_TRY BIT(1) +#define TEST_MTX_CTX BIT(2) +#define __TEST_MTX_LAST BIT(3) + +static void test_mutex_work(struct work_struct *work) +{ + struct test_mutex *mtx = container_of(work, typeof(*mtx), work); + + complete(>ready); + wait_for_completion(>go); + + if (mtx->flags & TEST_MTX_TRY) { + while (!ww_mutex_trylock(>mutex)) + cpu_relax(); + } else { + ww_mutex_lock(>mutex, NULL); + } + complete(>done); + ww_mutex_unlock(>mutex); +} + +static int __test_mutex(unsigned int flags) +{ +#define TIMEOUT (HZ / 16) + struct test_mutex mtx; + struct ww_acquire_ctx ctx; + int ret; + + ww_mutex_init(, _class); + ww_acquire_init(, _class); + + INIT_WORK_ONSTACK(, test_mutex_work); + init_completion(); + init_completion(); + init_completion(); + mtx.flags = flags; + + schedule_work(); + + wait_for_completion(); + ww_mutex_lock(, (flags & TEST_MTX_CTX) ? : NULL); + complete(); + if (flags & TEST_MTX_SPIN) { + unsigned long timeout = jiffies + TIMEOUT; + + ret = 0; + do { + if (completion_done()) { + ret = -EINVAL; + break; + } + cpu_relax(); + } while (time_before(jiffies, timeout)); + } else { + ret = wait_for_completion_timeout(, TIMEOUT); + } + ww_mutex_unlock(); + ww_acquire_fini(); + + if (ret) { + pr_err("%s(flags=%x): mutual exclusion failure\n", + __func__, flags); + ret = -EINVAL; + } + + flush_work(); + destroy_work_on_stack(); + return ret; +#undef TIMEOUT +} + +static int test_mutex(void) +{ + int ret; + int i; + + for (i = 0; i < __TEST_MTX_LAST; i++) { + ret = __test_mutex(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); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Intel Corporation"); diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index a6c8db1d62f6..cf8f3e942b46 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1161,6 +1161,18 @@ config LOCK_TORTURE_TEST Say M if you want these torture tests to build as a module. Say N if you are
[PATCH v2 3/8] locking: Begin kselftests for ww_mutex
v2: Add a couple more variations for degenerate ww_mutex, decrease timeouts Signed-off-by: Chris Wilson Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Maarten Lankhorst Cc: Nicolai Hähnle --- kernel/locking/Makefile| 1 + kernel/locking/test-ww_mutex.c | 140 + lib/Kconfig.debug | 12 3 files changed, 153 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 ..472d5b1f303f --- /dev/null +++ b/kernel/locking/test-ww_mutex.c @@ -0,0 +1,140 @@ +/* + * 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 + +static DEFINE_WW_CLASS(ww_class); + +struct test_mutex { + struct work_struct work; + struct ww_mutex mutex; + struct completion ready, go, done; + unsigned int flags; +}; + +#define TEST_MTX_SPIN BIT(0) +#define TEST_MTX_TRY BIT(1) +#define TEST_MTX_CTX BIT(2) +#define __TEST_MTX_LAST BIT(3) + +static void test_mutex_work(struct work_struct *work) +{ + struct test_mutex *mtx = container_of(work, typeof(*mtx), work); + + complete(>ready); + wait_for_completion(>go); + + if (mtx->flags & TEST_MTX_TRY) { + while (!ww_mutex_trylock(>mutex)) + cpu_relax(); + } else { + ww_mutex_lock(>mutex, NULL); + } + complete(>done); + ww_mutex_unlock(>mutex); +} + +static int __test_mutex(unsigned int flags) +{ +#define TIMEOUT (HZ / 16) + struct test_mutex mtx; + struct ww_acquire_ctx ctx; + int ret; + + ww_mutex_init(, _class); + ww_acquire_init(, _class); + + INIT_WORK_ONSTACK(, test_mutex_work); + init_completion(); + init_completion(); + init_completion(); + mtx.flags = flags; + + schedule_work(); + + wait_for_completion(); + ww_mutex_lock(, (flags & TEST_MTX_CTX) ? : NULL); + complete(); + if (flags & TEST_MTX_SPIN) { + unsigned long timeout = jiffies + TIMEOUT; + + ret = 0; + do { + if (completion_done()) { + ret = -EINVAL; + break; + } + cpu_relax(); + } while (time_before(jiffies, timeout)); + } else { + ret = wait_for_completion_timeout(, TIMEOUT); + } + ww_mutex_unlock(); + ww_acquire_fini(); + + if (ret) { + pr_err("%s(flags=%x): mutual exclusion failure\n", + __func__, flags); + ret = -EINVAL; + } + + flush_work(); + destroy_work_on_stack(); + return ret; +#undef TIMEOUT +} + +static int test_mutex(void) +{ + int ret; + int i; + + for (i = 0; i < __TEST_MTX_LAST; i++) { + ret = __test_mutex(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); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Intel Corporation"); diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index a6c8db1d62f6..cf8f3e942b46 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1161,6 +1161,18 @@ 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" + help +