-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoload.c
More file actions
79 lines (61 loc) · 1.85 KB
/
noload.c
File metadata and controls
79 lines (61 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#define pr_fmt(fmt) "%s:%s():%d: " fmt, KBUILD_MODNAME, __func__, __LINE__
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/vmalloc.h>
#include <linux/sched/sysctl.h>
#define DEFAULT_TIMEOUT_SECS 60
static unsigned int load = 10000;
module_param(load, uint, 0444);
MODULE_PARM_DESC(load, "Target load (default: 10000)");
struct task_struct **kthreads;
static unsigned long timeout_secs(void) {
#if IS_ENABLED(CONFIG_DETECT_HUNG_TASK)
extern unsigned long sysctl_hung_task_timeout_secs __attribute__((weak));
if (&sysctl_hung_task_timeout_secs)
return sysctl_hung_task_timeout_secs
? sysctl_hung_task_timeout_secs / 2 + 1
: CONFIG_DEFAULT_HUNG_TASK_TIMEOUT;
#endif
return DEFAULT_TIMEOUT_SECS;
}
static int kthread_fn(void *unused) {
while (!kthread_should_stop())
schedule_timeout_uninterruptible(timeout_secs() * HZ);
return 0;
}
static int __init noload_init(void) {
int i, ret;
if (load == 0) {
pr_err("load(%d) must be greater than 0.\n", load);
return -EINVAL;
}
kthreads = vmalloc_array(load, sizeof(struct task_struct *));
if (!kthreads)
return -ENOMEM;
for (i = 0; i < load; i++) {
struct task_struct *k = kthread_run(kthread_fn, NULL, "noload/%07d", i);
if (IS_ERR(k)) {
ret = PTR_ERR(k);
goto err;
}
kthreads[i] = k;
}
return 0;
err:
for (int j = 0; j < i; j++)
if (kthreads[j] != NULL)
kthread_stop(kthreads[j]);
vfree(kthreads);
return ret;
}
static void __exit noload_exit(void) {
for (int i = 0; i < load; i++)
if (kthreads[i] != NULL)
kthread_stop(kthreads[i]);
vfree(kthreads);
return;
}
module_init(noload_init);
module_exit(noload_exit);
MODULE_LICENSE("GPL");