Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Preempt / IRQ disable delay thread to test latency tracers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2018 Joel Fernandes (Google) <joel@joelfernandes.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/trace_clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static ulong delay = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static char test_mode[12] = "irq";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static uint burst_size = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) module_param_named(delay, delay, ulong, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) module_param_string(test_mode, test_mode, 12, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) module_param_named(burst_size, burst_size, uint, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) MODULE_PARM_DESC(delay, "Period in microseconds (100 us default)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) MODULE_PARM_DESC(test_mode, "Mode of the test such as preempt, irq, or alternate (default irq)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) MODULE_PARM_DESC(burst_size, "The size of a burst (default 1)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static struct completion done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define MIN(x, y) ((x) < (y) ? (x) : (y))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static void busy_wait(ulong time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	u64 start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	start = trace_clock_local();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		end = trace_clock_local();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		if (kthread_should_stop())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	} while ((end - start) < (time * 1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static __always_inline void irqoff_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	busy_wait(delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static __always_inline void preemptoff_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	busy_wait(delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static void execute_preemptirqtest(int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (!strcmp(test_mode, "irq"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		irqoff_test();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	else if (!strcmp(test_mode, "preempt"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		preemptoff_test();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	else if (!strcmp(test_mode, "alternate")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		if (idx % 2 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			irqoff_test();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			preemptoff_test();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #define DECLARE_TESTFN(POSTFIX)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	static void preemptirqtest_##POSTFIX(int idx)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	{						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		execute_preemptirqtest(idx);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * We create 10 different functions, so that we can get 10 different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * backtraces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) DECLARE_TESTFN(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) DECLARE_TESTFN(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) DECLARE_TESTFN(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) DECLARE_TESTFN(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) DECLARE_TESTFN(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) DECLARE_TESTFN(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) DECLARE_TESTFN(6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) DECLARE_TESTFN(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) DECLARE_TESTFN(8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) DECLARE_TESTFN(9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static void (*testfuncs[])(int)  = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	preemptirqtest_0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	preemptirqtest_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	preemptirqtest_2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	preemptirqtest_3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	preemptirqtest_4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	preemptirqtest_5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	preemptirqtest_6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	preemptirqtest_7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	preemptirqtest_8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	preemptirqtest_9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #define NR_TEST_FUNCS ARRAY_SIZE(testfuncs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int preemptirq_delay_run(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	int s = MIN(burst_size, NR_TEST_FUNCS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	for (i = 0; i < s; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		(testfuncs[i])(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	complete(&done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	while (!kthread_should_stop()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	__set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static int preemptirq_run_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	char task_name[50];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	init_completion(&done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	task =  kthread_run(preemptirq_delay_run, NULL, task_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (IS_ERR(task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return PTR_ERR(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (task) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		wait_for_completion(&done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		kthread_stop(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	ret = preemptirq_run_test();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static struct kobj_attribute trigger_attribute =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	__ATTR(trigger, 0200, NULL, trigger_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static struct attribute *attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	&trigger_attribute.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static struct attribute_group attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	.attrs = attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static struct kobject *preemptirq_delay_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static int __init preemptirq_delay_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	retval = preemptirq_run_test();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (retval != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 						       kernel_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (!preemptirq_delay_kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		kobject_put(preemptirq_delay_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static void __exit preemptirq_delay_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	kobject_put(preemptirq_delay_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) module_init(preemptirq_delay_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) module_exit(preemptirq_delay_exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) MODULE_LICENSE("GPL v2");