From 1b6f7ab585ff1eea43b0dd37f4962f7c765cc8e4 Mon Sep 17 00:00:00 2001 From: zeroway Date: Fri, 16 Nov 2018 10:56:47 +0800 Subject: [PATCH] Lock : add spin and mutex lock test --- debug/platform_driver_test/Makefile | 2 + debug/platform_driver_test/lock_test.dtsi | 5 ++ debug/platform_driver_test/mutex_test.c | 84 ++++++++++++++++++++++ debug/platform_driver_test/spinlock_test.c | 79 ++++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 debug/platform_driver_test/lock_test.dtsi create mode 100644 debug/platform_driver_test/mutex_test.c create mode 100644 debug/platform_driver_test/spinlock_test.c diff --git a/debug/platform_driver_test/Makefile b/debug/platform_driver_test/Makefile index f4bc5f0..cd966c4 100644 --- a/debug/platform_driver_test/Makefile +++ b/debug/platform_driver_test/Makefile @@ -17,6 +17,8 @@ endif obj-m := test_pf.o obj-m += show_store_pf.o obj-m += skeleton_ss.o +obj-m += spinlock_test.o +obj-m += mutex_test.o KERNEL_DIR ?= /lib/modules/$(shell uname -r)/build KERNEL_BUID_OUTPUT ?=$(KERNEL_DIR) diff --git a/debug/platform_driver_test/lock_test.dtsi b/debug/platform_driver_test/lock_test.dtsi new file mode 100644 index 0000000..cf866ee --- /dev/null +++ b/debug/platform_driver_test/lock_test.dtsi @@ -0,0 +1,5 @@ +/ { + lock_node { + compatible = "lock-test"; + }; +}; diff --git a/debug/platform_driver_test/mutex_test.c b/debug/platform_driver_test/mutex_test.c new file mode 100644 index 0000000..358b980 --- /dev/null +++ b/debug/platform_driver_test/mutex_test.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct locktest_chip { + struct mutex lock; +}; + +static int mutexlock_test_probe(struct platform_device *pdev) +{ + struct locktest_chip *chip; + int ret = 0; + + printk("%s, %d\n", __FUNCTION__, __LINE__); + + /* alloc for chip point */ + chip = devm_kzalloc(&pdev->dev, sizeof(struct locktest_chip), GFP_KERNEL); + if (!chip) + { + printk("no memory\n"); + ret = -ENOMEM; + } + + /* init mutext lock */ + mutex_init(&chip->lock); + + /* set pdata */ + dev_set_drvdata(&pdev->dev, (void *)chip); + + /* visit the race conditon area */ + if (!mutex_trylock(&chip->lock)) + { + printk("can not get the lock\n"); + return -1; + } + printk("Accessing race condition area...\n"); + + /* + * mutext can sleep + * sleep will cause context switch + */ + msleep(1); + mutex_unlock(&chip->lock); + + return ret; +} + +static int mutexlock_test_remove(struct platform_device *pdev) +{ + printk("%s, %d\n", __FUNCTION__, __LINE__); + + return 0; +} + +static const struct of_device_id mutexlock_test_dt_ids[] = { + {.compatible = "lock-test",}, + {}, +}; + +static struct platform_driver mutexlock_test = { + .driver = { + .name = "locktest example", + .owner = THIS_MODULE, + .of_match_table = of_match_ptr(mutexlock_test_dt_ids), + }, + .probe = mutexlock_test_probe, + .remove = mutexlock_test_remove, +}; + +module_platform_driver(mutexlock_test); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("zeroway "); +MODULE_DESCRIPTION("locktest"); diff --git a/debug/platform_driver_test/spinlock_test.c b/debug/platform_driver_test/spinlock_test.c new file mode 100644 index 0000000..940425c --- /dev/null +++ b/debug/platform_driver_test/spinlock_test.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct locktest_chip { + spinlock_t lock; +}; + +static int spinlock_test_probe(struct platform_device *pdev) +{ + struct locktest_chip *chip; + int ret = 0; + + printk("%s, %d\n", __FUNCTION__, __LINE__); + + /* alloc for chip point */ + chip = devm_kzalloc(&pdev->dev, sizeof(struct locktest_chip), GFP_KERNEL); + if (!chip) + { + printk("no memory\n"); + ret = -ENOMEM; + } + + /* set pdata */ + dev_set_drvdata(&pdev->dev, (void *)chip); + + /* visit the race conditon area */ + spin_lock(&chip->lock); + printk("Accessing race condition area...\n"); +#ifdef TRY_TO_SLEEP + /* + * sleep is not allow in spinlock context + * using for (i = 0; i < loop; i++); instead + * or using mutex + */ + msleep(1); +#endif + spin_unlock(&chip->lock); + + return ret; +} + +static int spinlock_test_remove(struct platform_device *pdev) +{ + printk("%s, %d\n", __FUNCTION__, __LINE__); + + return 0; +} + +static const struct of_device_id spinlock_test_dt_ids[] = { + {.compatible = "lock-test",}, + {}, +}; + +static struct platform_driver spinlock_test = { + .driver = { + .name = "locktest example", + .owner = THIS_MODULE, + .of_match_table = of_match_ptr(spinlock_test_dt_ids), + }, + .probe = spinlock_test_probe, + .remove = spinlock_test_remove, +}; + +module_platform_driver(spinlock_test); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("zeroway "); +MODULE_DESCRIPTION("locktest");