Joe Lawrence | a2818ee | 2019-01-09 13:43:29 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com> |
| 3 | |
| 4 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 5 | |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/kernel.h> |
Joe Lawrence | 547840b | 2020-06-03 14:20:55 -0400 | [diff] [blame] | 8 | #include <linux/sched.h> |
Joe Lawrence | a2818ee | 2019-01-09 13:43:29 +0100 | [diff] [blame] | 9 | #include <linux/workqueue.h> |
| 10 | #include <linux/delay.h> |
| 11 | |
Joe Lawrence | 547840b | 2020-06-03 14:20:55 -0400 | [diff] [blame] | 12 | /* load/run-time control from sysfs writer */ |
| 13 | static bool block_transition; |
| 14 | module_param(block_transition, bool, 0644); |
| 15 | MODULE_PARM_DESC(block_transition, "block_transition (default=false)"); |
Joe Lawrence | a2818ee | 2019-01-09 13:43:29 +0100 | [diff] [blame] | 16 | |
| 17 | static void busymod_work_func(struct work_struct *work); |
Joe Lawrence | 547840b | 2020-06-03 14:20:55 -0400 | [diff] [blame] | 18 | static DECLARE_WORK(work, busymod_work_func); |
Joe Lawrence | a2818ee | 2019-01-09 13:43:29 +0100 | [diff] [blame] | 19 | |
| 20 | static void busymod_work_func(struct work_struct *work) |
| 21 | { |
Joe Lawrence | 547840b | 2020-06-03 14:20:55 -0400 | [diff] [blame] | 22 | pr_info("%s enter\n", __func__); |
| 23 | |
| 24 | while (READ_ONCE(block_transition)) { |
| 25 | /* |
| 26 | * Busy-wait until the sysfs writer has acknowledged a |
| 27 | * blocked transition and clears the flag. |
| 28 | */ |
| 29 | msleep(20); |
| 30 | } |
| 31 | |
Joe Lawrence | a2818ee | 2019-01-09 13:43:29 +0100 | [diff] [blame] | 32 | pr_info("%s exit\n", __func__); |
| 33 | } |
| 34 | |
| 35 | static int test_klp_callbacks_busy_init(void) |
| 36 | { |
| 37 | pr_info("%s\n", __func__); |
Joe Lawrence | 547840b | 2020-06-03 14:20:55 -0400 | [diff] [blame] | 38 | schedule_work(&work); |
| 39 | |
| 40 | if (!block_transition) { |
| 41 | /* |
| 42 | * Serialize output: print all messages from the work |
| 43 | * function before returning from init(). |
| 44 | */ |
| 45 | flush_work(&work); |
| 46 | } |
| 47 | |
Joe Lawrence | a2818ee | 2019-01-09 13:43:29 +0100 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static void test_klp_callbacks_busy_exit(void) |
| 52 | { |
Joe Lawrence | 547840b | 2020-06-03 14:20:55 -0400 | [diff] [blame] | 53 | WRITE_ONCE(block_transition, false); |
| 54 | flush_work(&work); |
Joe Lawrence | a2818ee | 2019-01-09 13:43:29 +0100 | [diff] [blame] | 55 | pr_info("%s\n", __func__); |
| 56 | } |
| 57 | |
| 58 | module_init(test_klp_callbacks_busy_init); |
| 59 | module_exit(test_klp_callbacks_busy_exit); |
| 60 | MODULE_LICENSE("GPL"); |
| 61 | MODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>"); |
| 62 | MODULE_DESCRIPTION("Livepatch test: busy target module"); |