1f96e8577SJoel Fernandes (Google) // SPDX-License-Identifier: GPL-2.0
2f96e8577SJoel Fernandes (Google) /*
3f96e8577SJoel Fernandes (Google)  * Preempt / IRQ disable delay thread to test latency tracers
4f96e8577SJoel Fernandes (Google)  *
5f96e8577SJoel Fernandes (Google)  * Copyright (C) 2018 Joel Fernandes (Google) <joel@joelfernandes.org>
6f96e8577SJoel Fernandes (Google)  */
7f96e8577SJoel Fernandes (Google) 
812ad0cb2SSteven Rostedt (VMware) #include <linux/trace_clock.h>
9f96e8577SJoel Fernandes (Google) #include <linux/delay.h>
10f96e8577SJoel Fernandes (Google) #include <linux/interrupt.h>
11f96e8577SJoel Fernandes (Google) #include <linux/irq.h>
12f96e8577SJoel Fernandes (Google) #include <linux/kernel.h>
1379393723SViktor Rosendahl (BMW) #include <linux/kobject.h>
14f96e8577SJoel Fernandes (Google) #include <linux/kthread.h>
15f96e8577SJoel Fernandes (Google) #include <linux/module.h>
16f96e8577SJoel Fernandes (Google) #include <linux/printk.h>
17f96e8577SJoel Fernandes (Google) #include <linux/string.h>
1879393723SViktor Rosendahl (BMW) #include <linux/sysfs.h>
198b1fac2eSSteven Rostedt (VMware) #include <linux/completion.h>
20f96e8577SJoel Fernandes (Google) 
21f96e8577SJoel Fernandes (Google) static ulong delay = 100;
2279393723SViktor Rosendahl (BMW) static char test_mode[12] = "irq";
2379393723SViktor Rosendahl (BMW) static uint burst_size = 1;
24*4b9091e1SSong Chen static int  cpu_affinity = -1;
25f96e8577SJoel Fernandes (Google) 
2679393723SViktor Rosendahl (BMW) module_param_named(delay, delay, ulong, 0444);
2779393723SViktor Rosendahl (BMW) module_param_string(test_mode, test_mode, 12, 0444);
2879393723SViktor Rosendahl (BMW) module_param_named(burst_size, burst_size, uint, 0444);
29*4b9091e1SSong Chen module_param_named(cpu_affinity, cpu_affinity, int, 0444);
3079393723SViktor Rosendahl (BMW) MODULE_PARM_DESC(delay, "Period in microseconds (100 us default)");
3179393723SViktor Rosendahl (BMW) MODULE_PARM_DESC(test_mode, "Mode of the test such as preempt, irq, or alternate (default irq)");
3279393723SViktor Rosendahl (BMW) MODULE_PARM_DESC(burst_size, "The size of a burst (default 1)");
33*4b9091e1SSong Chen MODULE_PARM_DESC(cpu_affinity, "Cpu num test is running on");
3479393723SViktor Rosendahl (BMW) 
358b1fac2eSSteven Rostedt (VMware) static struct completion done;
368b1fac2eSSteven Rostedt (VMware) 
3779393723SViktor Rosendahl (BMW) #define MIN(x, y) ((x) < (y) ? (x) : (y))
38f96e8577SJoel Fernandes (Google) 
busy_wait(ulong time)39f96e8577SJoel Fernandes (Google) static void busy_wait(ulong time)
40f96e8577SJoel Fernandes (Google) {
4112ad0cb2SSteven Rostedt (VMware) 	u64 start, end;
42*4b9091e1SSong Chen 
4312ad0cb2SSteven Rostedt (VMware) 	start = trace_clock_local();
44*4b9091e1SSong Chen 
45f96e8577SJoel Fernandes (Google) 	do {
4612ad0cb2SSteven Rostedt (VMware) 		end = trace_clock_local();
47f96e8577SJoel Fernandes (Google) 		if (kthread_should_stop())
48f96e8577SJoel Fernandes (Google) 			break;
4912ad0cb2SSteven Rostedt (VMware) 	} while ((end - start) < (time * 1000));
50f96e8577SJoel Fernandes (Google) }
51f96e8577SJoel Fernandes (Google) 
irqoff_test(void)5279393723SViktor Rosendahl (BMW) static __always_inline void irqoff_test(void)
53f96e8577SJoel Fernandes (Google) {
54f96e8577SJoel Fernandes (Google) 	unsigned long flags;
55*4b9091e1SSong Chen 
56f96e8577SJoel Fernandes (Google) 	local_irq_save(flags);
57f96e8577SJoel Fernandes (Google) 	busy_wait(delay);
58f96e8577SJoel Fernandes (Google) 	local_irq_restore(flags);
5979393723SViktor Rosendahl (BMW) }
6079393723SViktor Rosendahl (BMW) 
preemptoff_test(void)6179393723SViktor Rosendahl (BMW) static __always_inline void preemptoff_test(void)
6279393723SViktor Rosendahl (BMW) {
63f96e8577SJoel Fernandes (Google) 	preempt_disable();
64f96e8577SJoel Fernandes (Google) 	busy_wait(delay);
65f96e8577SJoel Fernandes (Google) 	preempt_enable();
66f96e8577SJoel Fernandes (Google) }
67f96e8577SJoel Fernandes (Google) 
execute_preemptirqtest(int idx)6879393723SViktor Rosendahl (BMW) static void execute_preemptirqtest(int idx)
6979393723SViktor Rosendahl (BMW) {
7079393723SViktor Rosendahl (BMW) 	if (!strcmp(test_mode, "irq"))
7179393723SViktor Rosendahl (BMW) 		irqoff_test();
7279393723SViktor Rosendahl (BMW) 	else if (!strcmp(test_mode, "preempt"))
7379393723SViktor Rosendahl (BMW) 		preemptoff_test();
7479393723SViktor Rosendahl (BMW) 	else if (!strcmp(test_mode, "alternate")) {
7579393723SViktor Rosendahl (BMW) 		if (idx % 2 == 0)
7679393723SViktor Rosendahl (BMW) 			irqoff_test();
7779393723SViktor Rosendahl (BMW) 		else
7879393723SViktor Rosendahl (BMW) 			preemptoff_test();
7979393723SViktor Rosendahl (BMW) 	}
8079393723SViktor Rosendahl (BMW) }
8179393723SViktor Rosendahl (BMW) 
8279393723SViktor Rosendahl (BMW) #define DECLARE_TESTFN(POSTFIX)				\
8379393723SViktor Rosendahl (BMW) 	static void preemptirqtest_##POSTFIX(int idx)	\
8479393723SViktor Rosendahl (BMW) 	{						\
8579393723SViktor Rosendahl (BMW) 		execute_preemptirqtest(idx);		\
8679393723SViktor Rosendahl (BMW) 	}						\
8779393723SViktor Rosendahl (BMW) 
8879393723SViktor Rosendahl (BMW) /*
8979393723SViktor Rosendahl (BMW)  * We create 10 different functions, so that we can get 10 different
9079393723SViktor Rosendahl (BMW)  * backtraces.
9179393723SViktor Rosendahl (BMW)  */
9279393723SViktor Rosendahl (BMW) DECLARE_TESTFN(0)
9379393723SViktor Rosendahl (BMW) DECLARE_TESTFN(1)
9479393723SViktor Rosendahl (BMW) DECLARE_TESTFN(2)
9579393723SViktor Rosendahl (BMW) DECLARE_TESTFN(3)
9679393723SViktor Rosendahl (BMW) DECLARE_TESTFN(4)
9779393723SViktor Rosendahl (BMW) DECLARE_TESTFN(5)
9879393723SViktor Rosendahl (BMW) DECLARE_TESTFN(6)
9979393723SViktor Rosendahl (BMW) DECLARE_TESTFN(7)
10079393723SViktor Rosendahl (BMW) DECLARE_TESTFN(8)
10179393723SViktor Rosendahl (BMW) DECLARE_TESTFN(9)
10279393723SViktor Rosendahl (BMW) 
10379393723SViktor Rosendahl (BMW) static void (*testfuncs[])(int)  = {
10479393723SViktor Rosendahl (BMW) 	preemptirqtest_0,
10579393723SViktor Rosendahl (BMW) 	preemptirqtest_1,
10679393723SViktor Rosendahl (BMW) 	preemptirqtest_2,
10779393723SViktor Rosendahl (BMW) 	preemptirqtest_3,
10879393723SViktor Rosendahl (BMW) 	preemptirqtest_4,
10979393723SViktor Rosendahl (BMW) 	preemptirqtest_5,
11079393723SViktor Rosendahl (BMW) 	preemptirqtest_6,
11179393723SViktor Rosendahl (BMW) 	preemptirqtest_7,
11279393723SViktor Rosendahl (BMW) 	preemptirqtest_8,
11379393723SViktor Rosendahl (BMW) 	preemptirqtest_9,
11479393723SViktor Rosendahl (BMW) };
11579393723SViktor Rosendahl (BMW) 
11679393723SViktor Rosendahl (BMW) #define NR_TEST_FUNCS ARRAY_SIZE(testfuncs)
11779393723SViktor Rosendahl (BMW) 
preemptirq_delay_run(void * data)11879393723SViktor Rosendahl (BMW) static int preemptirq_delay_run(void *data)
11979393723SViktor Rosendahl (BMW) {
12079393723SViktor Rosendahl (BMW) 	int i;
12179393723SViktor Rosendahl (BMW) 	int s = MIN(burst_size, NR_TEST_FUNCS);
122*4b9091e1SSong Chen 	struct cpumask cpu_mask;
123*4b9091e1SSong Chen 
124*4b9091e1SSong Chen 	if (cpu_affinity > -1) {
125*4b9091e1SSong Chen 		cpumask_clear(&cpu_mask);
126*4b9091e1SSong Chen 		cpumask_set_cpu(cpu_affinity, &cpu_mask);
127*4b9091e1SSong Chen 		if (set_cpus_allowed_ptr(current, &cpu_mask))
128*4b9091e1SSong Chen 			pr_err("cpu_affinity:%d, failed\n", cpu_affinity);
129*4b9091e1SSong Chen 	}
13079393723SViktor Rosendahl (BMW) 
13179393723SViktor Rosendahl (BMW) 	for (i = 0; i < s; i++)
13279393723SViktor Rosendahl (BMW) 		(testfuncs[i])(i);
133d16a8c31SSteven Rostedt (VMware) 
1348b1fac2eSSteven Rostedt (VMware) 	complete(&done);
1358b1fac2eSSteven Rostedt (VMware) 
136d16a8c31SSteven Rostedt (VMware) 	set_current_state(TASK_INTERRUPTIBLE);
137d16a8c31SSteven Rostedt (VMware) 	while (!kthread_should_stop()) {
138d16a8c31SSteven Rostedt (VMware) 		schedule();
139d16a8c31SSteven Rostedt (VMware) 		set_current_state(TASK_INTERRUPTIBLE);
140d16a8c31SSteven Rostedt (VMware) 	}
141d16a8c31SSteven Rostedt (VMware) 
142d16a8c31SSteven Rostedt (VMware) 	__set_current_state(TASK_RUNNING);
143d16a8c31SSteven Rostedt (VMware) 
144f96e8577SJoel Fernandes (Google) 	return 0;
145f96e8577SJoel Fernandes (Google) }
146f96e8577SJoel Fernandes (Google) 
preemptirq_run_test(void)147d16a8c31SSteven Rostedt (VMware) static int preemptirq_run_test(void)
148f96e8577SJoel Fernandes (Google) {
149d16a8c31SSteven Rostedt (VMware) 	struct task_struct *task;
150f96e8577SJoel Fernandes (Google) 	char task_name[50];
151f96e8577SJoel Fernandes (Google) 
1528b1fac2eSSteven Rostedt (VMware) 	init_completion(&done);
1538b1fac2eSSteven Rostedt (VMware) 
154f96e8577SJoel Fernandes (Google) 	snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
155d16a8c31SSteven Rostedt (VMware) 	task =  kthread_run(preemptirq_delay_run, NULL, task_name);
156d16a8c31SSteven Rostedt (VMware) 	if (IS_ERR(task))
157d16a8c31SSteven Rostedt (VMware) 		return PTR_ERR(task);
1588b1fac2eSSteven Rostedt (VMware) 	if (task) {
1598b1fac2eSSteven Rostedt (VMware) 		wait_for_completion(&done);
160d16a8c31SSteven Rostedt (VMware) 		kthread_stop(task);
1618b1fac2eSSteven Rostedt (VMware) 	}
162d16a8c31SSteven Rostedt (VMware) 	return 0;
16379393723SViktor Rosendahl (BMW) }
164f96e8577SJoel Fernandes (Google) 
16579393723SViktor Rosendahl (BMW) 
trigger_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)16679393723SViktor Rosendahl (BMW) static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
16779393723SViktor Rosendahl (BMW) 			 const char *buf, size_t count)
16879393723SViktor Rosendahl (BMW) {
169d16a8c31SSteven Rostedt (VMware) 	ssize_t ret;
170d16a8c31SSteven Rostedt (VMware) 
171d16a8c31SSteven Rostedt (VMware) 	ret = preemptirq_run_test();
172d16a8c31SSteven Rostedt (VMware) 	if (ret)
173d16a8c31SSteven Rostedt (VMware) 		return ret;
17479393723SViktor Rosendahl (BMW) 	return count;
17579393723SViktor Rosendahl (BMW) }
17679393723SViktor Rosendahl (BMW) 
17779393723SViktor Rosendahl (BMW) static struct kobj_attribute trigger_attribute =
17879393723SViktor Rosendahl (BMW) 	__ATTR(trigger, 0200, NULL, trigger_store);
17979393723SViktor Rosendahl (BMW) 
18079393723SViktor Rosendahl (BMW) static struct attribute *attrs[] = {
18179393723SViktor Rosendahl (BMW) 	&trigger_attribute.attr,
18279393723SViktor Rosendahl (BMW) 	NULL,
18379393723SViktor Rosendahl (BMW) };
18479393723SViktor Rosendahl (BMW) 
18579393723SViktor Rosendahl (BMW) static struct attribute_group attr_group = {
18679393723SViktor Rosendahl (BMW) 	.attrs = attrs,
18779393723SViktor Rosendahl (BMW) };
18879393723SViktor Rosendahl (BMW) 
18979393723SViktor Rosendahl (BMW) static struct kobject *preemptirq_delay_kobj;
19079393723SViktor Rosendahl (BMW) 
preemptirq_delay_init(void)19179393723SViktor Rosendahl (BMW) static int __init preemptirq_delay_init(void)
19279393723SViktor Rosendahl (BMW) {
19379393723SViktor Rosendahl (BMW) 	int retval;
19479393723SViktor Rosendahl (BMW) 
195d16a8c31SSteven Rostedt (VMware) 	retval = preemptirq_run_test();
19679393723SViktor Rosendahl (BMW) 	if (retval != 0)
19779393723SViktor Rosendahl (BMW) 		return retval;
19879393723SViktor Rosendahl (BMW) 
19979393723SViktor Rosendahl (BMW) 	preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
20079393723SViktor Rosendahl (BMW) 						       kernel_kobj);
20179393723SViktor Rosendahl (BMW) 	if (!preemptirq_delay_kobj)
20279393723SViktor Rosendahl (BMW) 		return -ENOMEM;
20379393723SViktor Rosendahl (BMW) 
20479393723SViktor Rosendahl (BMW) 	retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
20579393723SViktor Rosendahl (BMW) 	if (retval)
20679393723SViktor Rosendahl (BMW) 		kobject_put(preemptirq_delay_kobj);
20779393723SViktor Rosendahl (BMW) 
20879393723SViktor Rosendahl (BMW) 	return retval;
209f96e8577SJoel Fernandes (Google) }
210f96e8577SJoel Fernandes (Google) 
preemptirq_delay_exit(void)211f96e8577SJoel Fernandes (Google) static void __exit preemptirq_delay_exit(void)
212f96e8577SJoel Fernandes (Google) {
21379393723SViktor Rosendahl (BMW) 	kobject_put(preemptirq_delay_kobj);
214f96e8577SJoel Fernandes (Google) }
215f96e8577SJoel Fernandes (Google) 
216f96e8577SJoel Fernandes (Google) module_init(preemptirq_delay_init)
217f96e8577SJoel Fernandes (Google) module_exit(preemptirq_delay_exit)
218f96e8577SJoel Fernandes (Google) MODULE_LICENSE("GPL v2");
219