summaryrefslogtreecommitdiff
path: root/SOURCE/module/kernel/irq_delay.c
blob: b59e9db16e8b679fdcd82ac12a85b0999b50fc19 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Linux内核诊断工具--内核态irq-delay功能
 *
 * Copyright (C) 2020 Alibaba Ltd.
 *
 * 作者: Baoyou Xie <[email protected]>
 *
 * License terms: GNU General Public License (GPL) version 3
 *
 */

#include <linux/hrtimer.h>
#include <linux/kernel.h>
#include <linux/kallsyms.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/timex.h>
#include <linux/tracepoint.h>
#include <trace/events/irq.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/sysctl.h>
#include <trace/events/napi.h>
#include <linux/rtc.h>
#include <linux/time.h>

#include "internal.h"
#include "pub/trace_file.h"

#include "uapi/irq_delay.h"

struct diag_irq_delay_settings irq_delay_settings = {
	.threshold = 20,
};

static int irq_delay_alloced = 0;
static struct diag_variant_buffer irq_delay_variant_buffer;

static void clean_data(void)
{
	//
}

void irq_delay_timer(struct diag_percpu_context *context)
{
	u64 expected;
	static struct irq_delay_detail *detail;

	if (!irq_delay_settings.activated)
		return;

	expected = context->timer_info.timer_expected_time;
	if (expected <= 0)
		return;

	if (need_dump(irq_delay_settings.threshold,
				&context->irq_delay.max_irq_delay_ms, expected))
	{
		u64 delay_ns = sched_clock() - expected;
		unsigned long flags;

		diag_variant_buffer_spin_lock(&irq_delay_variant_buffer, flags);
		detail = &diag_percpu_context[smp_processor_id()]->irq_delay_detail;
		detail->et_type = et_irq_delay_detail;
		detail->cpu = smp_processor_id();
		detail->delay_ns = delay_ns;
		do_diag_gettimeofday(&detail->tv);
		diag_task_brief(current, &detail->task);
		diag_task_kern_stack(current, &detail->kern_stack);
		diag_task_user_stack(current, &detail->user_stack);

		diag_variant_buffer_reserve(&irq_delay_variant_buffer, sizeof(struct irq_delay_detail));
		diag_variant_buffer_write_nolock(&irq_delay_variant_buffer, detail, sizeof(struct irq_delay_detail));
		diag_variant_buffer_seal(&irq_delay_variant_buffer);
		diag_variant_buffer_spin_unlock(&irq_delay_variant_buffer, flags);
	}
}

static int __activate_irq_delay(void)
{
	int ret = 0;
	ret = alloc_diag_variant_buffer(&irq_delay_variant_buffer);
	if (ret)
		goto out_variant_buffer;
	irq_delay_alloced = 1;

	clean_data();

	return 1;
out_variant_buffer:
	return 0;
}

static void __deactivate_irq_delay(void)
{
	synchronize_sched();
	clean_data();
}

int activate_irq_delay(void)
{
	if (!irq_delay_settings.activated)
		irq_delay_settings.activated = __activate_irq_delay();

	return irq_delay_settings.activated;
}

int deactivate_irq_delay(void)
{
	if (irq_delay_settings.activated)
		__deactivate_irq_delay();
	irq_delay_settings.activated = 0;

	return 0;
}

int irq_delay_syscall(struct pt_regs *regs, long id)
{
	int __user *user_ptr_len;
	size_t __user user_buf_len;
	void __user *user_buf;
	unsigned long flags;
	int i, ms;
	int ret = 0;
	struct diag_irq_delay_settings settings;

	switch (id) {
	case DIAG_IRQ_DELAY_SET:
		user_buf = (void __user *)SYSCALL_PARAM1(regs);
		user_buf_len = (size_t)SYSCALL_PARAM2(regs);

		if (user_buf_len != sizeof(struct diag_irq_delay_settings)) {
			ret = -EINVAL;
		} else if (irq_delay_settings.activated) {
			ret = -EBUSY;
		} else {
			ret = copy_from_user(&settings, user_buf, user_buf_len);
			if (!ret) {
				irq_delay_settings = settings;
			}
		}
		break;
	case DIAG_IRQ_DELAY_SETTINGS:
		user_buf = (void __user *)SYSCALL_PARAM1(regs);
		user_buf_len = (size_t)SYSCALL_PARAM2(regs);

		if (user_buf_len != sizeof(struct diag_irq_delay_settings)) {
			ret = -EINVAL;
		} else {
			settings.activated = irq_delay_settings.activated;
			settings.verbose = irq_delay_settings.verbose;
			settings.threshold = irq_delay_settings.threshold;
			ret = copy_to_user(user_buf, &settings, user_buf_len);
		}
		break;
	case DIAG_IRQ_DELAY_DUMP:
		user_ptr_len = (void __user *)SYSCALL_PARAM1(regs);
		user_buf = (void __user *)SYSCALL_PARAM2(regs);
		user_buf_len = (size_t)SYSCALL_PARAM3(regs);

		if (!irq_delay_alloced) {
			ret = -EINVAL;
		} else {
			ret = copy_to_user_variant_buffer(&irq_delay_variant_buffer,
					user_ptr_len, user_buf, user_buf_len);
			record_dump_cmd("irq-delay");
		}
		break;
	case DIAG_IRQ_DELAY_TEST:
		ms = SYSCALL_PARAM1(regs);
		
		if (ms <= 0 || ms > 1000) {
			ret = -EINVAL;
		} else {
			local_irq_save(flags);
			for (i = 0; i < ms; i++)
				mdelay(1);
			local_irq_restore(flags);
		}
		break;
	default:
		ret = -ENOSYS;
		break;
	}

	return ret;
}

long diag_ioctl_irq_delay(unsigned int cmd, unsigned long arg)
{
	unsigned long flags;
	int i, ms;
	int ret = 0;
	struct diag_irq_delay_settings settings;
	struct diag_ioctl_dump_param dump_param;

	switch (cmd) {
	case CMD_IRQ_DELAY_SET:
		if (irq_delay_settings.activated) {
			ret = -EBUSY;
		} else {
			ret = copy_from_user(&settings, (void *)arg, sizeof(struct diag_irq_delay_settings));
			if (!ret) {
				irq_delay_settings = settings;
			}
		}
		break;
	case CMD_IRQ_DELAY_SETTINGS:
		settings = irq_delay_settings;
		ret = copy_to_user((void *)arg, &settings, sizeof(struct diag_irq_delay_settings));
		break;
	case CMD_IRQ_DELAY_DUMP:
		ret = copy_from_user(&dump_param, (void *)arg, sizeof(struct diag_ioctl_dump_param));
		if (!irq_delay_alloced) {
			ret = -EINVAL;
		} else if (!ret) {
			ret = copy_to_user_variant_buffer(&irq_delay_variant_buffer,
					dump_param.user_ptr_len, dump_param.user_buf, dump_param.user_buf_len);
			record_dump_cmd("irq-delay");
		}
		break;
	case CMD_IRQ_DELAY_TEST:
		ret = copy_from_user(&ms, (void *)arg, sizeof(int));
		if (ret)
			ms = 0;

		if (ms <= 0 || ms > 1000) {
			ret = -EINVAL;
		} else {
			local_irq_save(flags);
			for (i = 0; i < ms; i++)
				mdelay(1);
			local_irq_restore(flags);
		}

		break;
	default:
		ret = -ENOSYS;
		break;
	}

	return ret;
}

static int lookup_syms(void)
{
	return 0;
}

int diag_irq_delay_init(void)
{
	if (lookup_syms())
		return -EINVAL;

	init_diag_variant_buffer(&irq_delay_variant_buffer, 1 * 1024 * 1024);
	clean_data();

	if (irq_delay_settings.activated)
		__activate_irq_delay();

	return 0;
}

void diag_irq_delay_exit(void)
{
	if (irq_delay_settings.activated)
		deactivate_irq_delay();
	irq_delay_settings.activated = 0;
	destroy_diag_variant_buffer(&irq_delay_variant_buffer);
}