0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/trace_clock.h>
0009 #include <linux/delay.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/irq.h>
0012 #include <linux/kernel.h>
0013 #include <linux/kobject.h>
0014 #include <linux/kthread.h>
0015 #include <linux/module.h>
0016 #include <linux/printk.h>
0017 #include <linux/string.h>
0018 #include <linux/sysfs.h>
0019 #include <linux/completion.h>
0020
0021 static ulong delay = 100;
0022 static char test_mode[12] = "irq";
0023 static uint burst_size = 1;
0024 static int cpu_affinity = -1;
0025
0026 module_param_named(delay, delay, ulong, 0444);
0027 module_param_string(test_mode, test_mode, 12, 0444);
0028 module_param_named(burst_size, burst_size, uint, 0444);
0029 module_param_named(cpu_affinity, cpu_affinity, int, 0444);
0030 MODULE_PARM_DESC(delay, "Period in microseconds (100 us default)");
0031 MODULE_PARM_DESC(test_mode, "Mode of the test such as preempt, irq, or alternate (default irq)");
0032 MODULE_PARM_DESC(burst_size, "The size of a burst (default 1)");
0033 MODULE_PARM_DESC(cpu_affinity, "Cpu num test is running on");
0034
0035 static struct completion done;
0036
0037 #define MIN(x, y) ((x) < (y) ? (x) : (y))
0038
0039 static void busy_wait(ulong time)
0040 {
0041 u64 start, end;
0042
0043 start = trace_clock_local();
0044
0045 do {
0046 end = trace_clock_local();
0047 if (kthread_should_stop())
0048 break;
0049 } while ((end - start) < (time * 1000));
0050 }
0051
0052 static __always_inline void irqoff_test(void)
0053 {
0054 unsigned long flags;
0055
0056 local_irq_save(flags);
0057 busy_wait(delay);
0058 local_irq_restore(flags);
0059 }
0060
0061 static __always_inline void preemptoff_test(void)
0062 {
0063 preempt_disable();
0064 busy_wait(delay);
0065 preempt_enable();
0066 }
0067
0068 static void execute_preemptirqtest(int idx)
0069 {
0070 if (!strcmp(test_mode, "irq"))
0071 irqoff_test();
0072 else if (!strcmp(test_mode, "preempt"))
0073 preemptoff_test();
0074 else if (!strcmp(test_mode, "alternate")) {
0075 if (idx % 2 == 0)
0076 irqoff_test();
0077 else
0078 preemptoff_test();
0079 }
0080 }
0081
0082 #define DECLARE_TESTFN(POSTFIX) \
0083 static void preemptirqtest_##POSTFIX(int idx) \
0084 { \
0085 execute_preemptirqtest(idx); \
0086 } \
0087
0088
0089
0090
0091
0092 DECLARE_TESTFN(0)
0093 DECLARE_TESTFN(1)
0094 DECLARE_TESTFN(2)
0095 DECLARE_TESTFN(3)
0096 DECLARE_TESTFN(4)
0097 DECLARE_TESTFN(5)
0098 DECLARE_TESTFN(6)
0099 DECLARE_TESTFN(7)
0100 DECLARE_TESTFN(8)
0101 DECLARE_TESTFN(9)
0102
0103 static void (*testfuncs[])(int) = {
0104 preemptirqtest_0,
0105 preemptirqtest_1,
0106 preemptirqtest_2,
0107 preemptirqtest_3,
0108 preemptirqtest_4,
0109 preemptirqtest_5,
0110 preemptirqtest_6,
0111 preemptirqtest_7,
0112 preemptirqtest_8,
0113 preemptirqtest_9,
0114 };
0115
0116 #define NR_TEST_FUNCS ARRAY_SIZE(testfuncs)
0117
0118 static int preemptirq_delay_run(void *data)
0119 {
0120 int i;
0121 int s = MIN(burst_size, NR_TEST_FUNCS);
0122 struct cpumask cpu_mask;
0123
0124 if (cpu_affinity > -1) {
0125 cpumask_clear(&cpu_mask);
0126 cpumask_set_cpu(cpu_affinity, &cpu_mask);
0127 if (set_cpus_allowed_ptr(current, &cpu_mask))
0128 pr_err("cpu_affinity:%d, failed\n", cpu_affinity);
0129 }
0130
0131 for (i = 0; i < s; i++)
0132 (testfuncs[i])(i);
0133
0134 complete(&done);
0135
0136 set_current_state(TASK_INTERRUPTIBLE);
0137 while (!kthread_should_stop()) {
0138 schedule();
0139 set_current_state(TASK_INTERRUPTIBLE);
0140 }
0141
0142 __set_current_state(TASK_RUNNING);
0143
0144 return 0;
0145 }
0146
0147 static int preemptirq_run_test(void)
0148 {
0149 struct task_struct *task;
0150 char task_name[50];
0151
0152 init_completion(&done);
0153
0154 snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
0155 task = kthread_run(preemptirq_delay_run, NULL, task_name);
0156 if (IS_ERR(task))
0157 return PTR_ERR(task);
0158 if (task) {
0159 wait_for_completion(&done);
0160 kthread_stop(task);
0161 }
0162 return 0;
0163 }
0164
0165
0166 static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
0167 const char *buf, size_t count)
0168 {
0169 ssize_t ret;
0170
0171 ret = preemptirq_run_test();
0172 if (ret)
0173 return ret;
0174 return count;
0175 }
0176
0177 static struct kobj_attribute trigger_attribute =
0178 __ATTR(trigger, 0200, NULL, trigger_store);
0179
0180 static struct attribute *attrs[] = {
0181 &trigger_attribute.attr,
0182 NULL,
0183 };
0184
0185 static struct attribute_group attr_group = {
0186 .attrs = attrs,
0187 };
0188
0189 static struct kobject *preemptirq_delay_kobj;
0190
0191 static int __init preemptirq_delay_init(void)
0192 {
0193 int retval;
0194
0195 retval = preemptirq_run_test();
0196 if (retval != 0)
0197 return retval;
0198
0199 preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
0200 kernel_kobj);
0201 if (!preemptirq_delay_kobj)
0202 return -ENOMEM;
0203
0204 retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
0205 if (retval)
0206 kobject_put(preemptirq_delay_kobj);
0207
0208 return retval;
0209 }
0210
0211 static void __exit preemptirq_delay_exit(void)
0212 {
0213 kobject_put(preemptirq_delay_kobj);
0214 }
0215
0216 module_init(preemptirq_delay_init)
0217 module_exit(preemptirq_delay_exit)
0218 MODULE_LICENSE("GPL v2");