From 99721cca0df248b06afc90c99e200620b40b63d0 Mon Sep 17 00:00:00 2001 From: Nicolas Benech Date: Wed, 6 Mar 2019 15:20:32 -0500 Subject: [PATCH] nvgpu: unit: unit test for nvgpu.interface.lock This unit test covers all APIs of the nvgpu.interface.lock and verify their behavior. JIRA NVGPU-2245 Change-Id: I317663ab45a2cc93ddd46edccc9c3fdc37148e72 Signed-off-by: Nicolas Benech Reviewed-on: https://git-master.nvidia.com/r/2034205 Reviewed-by: mobile promotions Tested-by: mobile promotions --- Makefile.umbrella.tmk | 1 + drivers/gpu/nvgpu/libnvgpu-drv.export | 9 + userspace/Makefile.sources | 3 +- userspace/required_tests.json | 20 ++ userspace/units/interface/lock/Makefile | 26 ++ .../interface/lock/Makefile.interface.tmk | 23 ++ userspace/units/interface/lock/Makefile.tmk | 27 ++ userspace/units/interface/lock/lock.c | 284 ++++++++++++++++++ 8 files changed, 392 insertions(+), 1 deletion(-) create mode 100644 userspace/units/interface/lock/Makefile create mode 100644 userspace/units/interface/lock/Makefile.interface.tmk create mode 100644 userspace/units/interface/lock/Makefile.tmk create mode 100644 userspace/units/interface/lock/lock.c diff --git a/Makefile.umbrella.tmk b/Makefile.umbrella.tmk index d3e475d8a..ba7078a1f 100644 --- a/Makefile.umbrella.tmk +++ b/Makefile.umbrella.tmk @@ -36,6 +36,7 @@ NV_REPOSITORY_COMPONENTS += userspace/units/fifo/runlist NV_REPOSITORY_COMPONENTS += userspace/units/fuse NV_REPOSITORY_COMPONENTS += userspace/units/list NV_REPOSITORY_COMPONENTS += userspace/units/enabled +NV_REPOSITORY_COMPONENTS += userspace/units/interface/lock endif # Local Variables: diff --git a/drivers/gpu/nvgpu/libnvgpu-drv.export b/drivers/gpu/nvgpu/libnvgpu-drv.export index 0ec0f55fe..66717de98 100644 --- a/drivers/gpu/nvgpu/libnvgpu-drv.export +++ b/drivers/gpu/nvgpu/libnvgpu-drv.export @@ -102,7 +102,10 @@ nvgpu_mem_sgt_iommuable nvgpu_mem_sgt_posix_create_from_list nvgpu_mem_wr32 nvgpu_mutex_acquire +nvgpu_mutex_destroy +nvgpu_mutex_init nvgpu_mutex_release +nvgpu_mutex_tryacquire nvgpu_pd_alloc nvgpu_pd_cache_fini nvgpu_pd_cache_init @@ -127,6 +130,9 @@ nvgpu_posix_register_io nvgpu_pramin_memset nvgpu_pramin_rd_n nvgpu_pramin_wr_n +nvgpu_raw_spinlock_acquire +nvgpu_raw_spinlock_init +nvgpu_raw_spinlock_release nvgpu_rbtree_enum_next nvgpu_rbtree_enum_start nvgpu_readl @@ -143,6 +149,9 @@ nvgpu_sgt_get_next nvgpu_sgt_get_phys nvgpu_sgt_iommuable nvgpu_sgt_ipa_to_pa +nvgpu_spinlock_acquire +nvgpu_spinlock_init +nvgpu_spinlock_release nvgpu_usermode_writel nvgpu_vm_find_mapped_buf nvgpu_vm_init diff --git a/userspace/Makefile.sources b/userspace/Makefile.sources index 7639e197a..ca0e2e8a0 100644 --- a/userspace/Makefile.sources +++ b/userspace/Makefile.sources @@ -60,7 +60,8 @@ UNITS := \ $(UNIT_SRC)/mm/page_table_faults \ $(UNIT_SRC)/fifo/runlist \ $(UNIT_SRC)/list \ - $(UNIT_SRC)/enabled + $(UNIT_SRC)/enabled \ + $(UNIT_SRC)/interface/lock # A test unit. Not really needed any more... # $(UNIT_SRC)/test \ No newline at end of file diff --git a/userspace/required_tests.json b/userspace/required_tests.json index 1706f3788..78feed252 100644 --- a/userspace/required_tests.json +++ b/userspace/required_tests.json @@ -155,6 +155,26 @@ "test": "fuse_gv100_vin_cal_slope_intercept", "unit": "fuse" }, + { + "test": "mutex_acquire_release", + "unit": "interface_lock" + }, + { + "test": "mutex_init", + "unit": "interface_lock" + }, + { + "test": "mutex_tryacquire", + "unit": "interface_lock" + }, + { + "test": "raw_spinlock_acquire_release", + "unit": "interface_lock" + }, + { + "test": "spinlock_acquire_release", + "unit": "interface_lock" + }, { "test": "list_all_head", "unit": "list" diff --git a/userspace/units/interface/lock/Makefile b/userspace/units/interface/lock/Makefile new file mode 100644 index 000000000..6a0a6428a --- /dev/null +++ b/userspace/units/interface/lock/Makefile @@ -0,0 +1,26 @@ +# Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved. +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. + +.SUFFIXES: + +OBJS = lock.o +MODULE = lock + +include ../../Makefile.units diff --git a/userspace/units/interface/lock/Makefile.interface.tmk b/userspace/units/interface/lock/Makefile.interface.tmk new file mode 100644 index 000000000..19ca66521 --- /dev/null +++ b/userspace/units/interface/lock/Makefile.interface.tmk @@ -0,0 +1,23 @@ +################################### tell Emacs this is a -*- makefile-gmake -*- +# +# Copyright (c) 2018-2019, NVIDIA CORPORATION. All Rights Reserved. +# +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. +# +# tmake for SW Mobile component makefile +# +############################################################################### + +NVGPU_UNIT_NAME=lock + +include $(NV_COMPONENT_DIR)/../../Makefile.units.common.interface.tmk + +# Local Variables: +# indent-tabs-mode: t +# tab-width: 8 +# End: +# vi: set tabstop=8 noexpandtab: diff --git a/userspace/units/interface/lock/Makefile.tmk b/userspace/units/interface/lock/Makefile.tmk new file mode 100644 index 000000000..f760393db --- /dev/null +++ b/userspace/units/interface/lock/Makefile.tmk @@ -0,0 +1,27 @@ +################################### tell Emacs this is a -*- makefile-gmake -*- +# +# Copyright (c) 2018-2019 NVIDIA CORPORATION. All Rights Reserved. +# +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. +# +# tmake for SW Mobile component makefile +# +############################################################################### + +NVGPU_UNIT_NAME=lock + +ifneq ($(NV_BUILD_CONFIGURATION_OS_IS_QNX),1) +NVGPU_UNIT_SHARED_LIBRARIES += pthread +endif + +include $(NV_COMPONENT_DIR)/../../Makefile.units.common.tmk + +# Local Variables: +# indent-tabs-mode: t +# tab-width: 8 +# End: +# vi: set tabstop=8 noexpandtab: diff --git a/userspace/units/interface/lock/lock.c b/userspace/units/interface/lock/lock.c new file mode 100644 index 000000000..aaf6a4ae5 --- /dev/null +++ b/userspace/units/interface/lock/lock.c @@ -0,0 +1,284 @@ +/* + * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#define TYPE_MUTEX 0 +#define TYPE_SPINLOCK 1 +#define TYPE_RAW_SPINLOCK 2 + +/* Parameter structure to pass to worker threads */ +struct worker_parameters { + u64 type; + struct nvgpu_mutex *mutex; + struct nvgpu_spinlock *lock; + struct nvgpu_raw_spinlock *raw_lock; +}; + +sem_t worker_sem; +bool test_shared_flag; + +/* + * Simple test to check mutex init routine + */ +static int test_mutex_init(struct unit_module *m, struct gk20a *g, + void *args) +{ + struct nvgpu_mutex mutex; + int err = nvgpu_mutex_init(&mutex); + + if (err != 0) { + unit_return_fail(m, "mutex_init failure: %d\n", err); + } + + nvgpu_mutex_destroy(&mutex); + + return UNIT_SUCCESS; +} + +/* + * Test to verify the behavior of mutex tryacquire function. + */ +static int test_mutex_tryacquire(struct unit_module *m, struct gk20a *g, + void *args) +{ + struct nvgpu_mutex mutex; + int status; + + if (nvgpu_mutex_init(&mutex) != 0) { + unit_return_fail(m, "mutex_init failure\n"); + } + + nvgpu_mutex_acquire(&mutex); + + status = nvgpu_mutex_tryacquire(&mutex); + + if (status == 1) { + unit_return_fail(m, "tryacquire did not fail as expected: %d\n", + status); + } + + nvgpu_mutex_release(&mutex); + + status = nvgpu_mutex_tryacquire(&mutex); + + if (status != 1) { + unit_return_fail(m, + "tryacquire did not succeed as expected: %d\n", status); + } + + nvgpu_mutex_release(&mutex); + nvgpu_mutex_destroy(&mutex); + + return UNIT_SUCCESS; +} + +/* + * Worker function to be used in a separate thread to test acquire of one of + * the supported lock types, provided as an argument. + */ +static void *lock_worker(void *args) +{ + struct worker_parameters *params = (struct worker_parameters *) args; + + /* Signal main testing function that the worker thread has started. */ + sem_post(&worker_sem); + + /* + * Lock should already be held by the main test function, so execution + * should block here. + */ + switch (params->type) { + case TYPE_MUTEX: + nvgpu_mutex_acquire(params->mutex); + break; + case TYPE_SPINLOCK: + nvgpu_spinlock_acquire(params->lock); + break; + case TYPE_RAW_SPINLOCK: + nvgpu_raw_spinlock_acquire(params->raw_lock); + break; + default: + break; + } + + /* + * Update the shared_flag to indicate that the spinlock_acquire + * succeeded and signal the main thread. + */ + test_shared_flag = true; + sem_post(&worker_sem); + + /* Cleanup */ + switch (params->type) { + case TYPE_MUTEX: + nvgpu_mutex_release(params->mutex); + break; + case TYPE_SPINLOCK: + nvgpu_spinlock_release(params->lock); + break; + case TYPE_RAW_SPINLOCK: + nvgpu_raw_spinlock_release(params->raw_lock); + break; + default: + break; + } + return NULL; +} + +/* + * Test to verify the behavior of mutex, regular and raw spinlocks acquire and + * release functions. + * The "args" argument is an int indicating the lock type. + */ +static int test_lock_acquire_release(struct unit_module *m, struct gk20a *g, + void *args) +{ + struct nvgpu_mutex mutex; + struct nvgpu_spinlock lock; + struct nvgpu_raw_spinlock raw_lock; + pthread_t worker_thread; + struct worker_parameters worker_params; + u64 type = (u64) args; + int result = UNIT_FAIL; + + worker_params.type = type; + + switch (type) { + case TYPE_MUTEX: + if (nvgpu_mutex_init(&mutex) != 0) { + unit_return_fail(m, "mutex_init failure\n"); + } + worker_params.mutex = &mutex; + break; + case TYPE_SPINLOCK: + nvgpu_spinlock_init(&lock); + worker_params.lock = &lock; + break; + case TYPE_RAW_SPINLOCK: + nvgpu_raw_spinlock_init(&raw_lock); + worker_params.raw_lock = &raw_lock; + break; + default: + unit_return_fail(m, "Unexpected lock type.\n"); + break; + } + + /* + * The semaphore is used to synchronize things when needed between the + * current thread and the worker thread + * (*_acquire_release_worker) + */ + sem_init(&worker_sem, 0, 0); + test_shared_flag = false; + + /* + * Acquire the lock so that the worker thread will block when it tries + * to acquire it too. + */ + switch (type) { + case TYPE_MUTEX: + nvgpu_mutex_acquire(&mutex); + break; + case TYPE_SPINLOCK: + nvgpu_spinlock_acquire(&lock); + break; + case TYPE_RAW_SPINLOCK: + nvgpu_raw_spinlock_acquire(&raw_lock); + break; + default: + break; + } + + /* Start the thread and wait for its signal */ + pthread_create(&worker_thread, NULL, lock_worker, + (void *) &worker_params); + + sem_wait(&worker_sem); + + /* + * Worker thread is initialized and running. It should be waiting on the + * lock, if not (i.e. the flag was updated) then it's a failure + */ + if (test_shared_flag) { + unit_err(m, "Worker thread did not block on lock\n"); + goto cleanup; + } + + /* + * If the flag was not updated, release the lock and check that the + * flag was updated this time + */ + switch (type) { + case TYPE_MUTEX: + nvgpu_mutex_release(&mutex); + break; + case TYPE_SPINLOCK: + nvgpu_spinlock_release(&lock); + break; + case TYPE_RAW_SPINLOCK: + nvgpu_raw_spinlock_release(&raw_lock); + break; + default: + break; + } + + sem_wait(&worker_sem); + + if (!test_shared_flag) { + unit_err(m, "Lock did not get released in worker thread\n"); + goto cleanup; + } + + result = UNIT_SUCCESS; + +cleanup: + pthread_join(worker_thread, NULL); + if (type == TYPE_MUTEX) { + nvgpu_mutex_destroy(&mutex); + } + sem_destroy(&worker_sem); + return result; +} + +struct unit_module_test interface_lock_tests[] = { + UNIT_TEST(mutex_init, test_mutex_init, NULL), + UNIT_TEST(mutex_acquire_release, test_lock_acquire_release, + (u64 *) 0), + UNIT_TEST(spinlock_acquire_release, test_lock_acquire_release, + (u64 *) 1), + UNIT_TEST(raw_spinlock_acquire_release, test_lock_acquire_release, + (u64 *) 2), + UNIT_TEST(mutex_tryacquire, test_mutex_tryacquire, NULL), +}; + +UNIT_MODULE(interface_lock, interface_lock_tests, UNIT_PRIO_NVGPU_TEST);