summaryrefslogtreecommitdiff
path: root/SOURCE/module/kernel/load.c
blob: 92261175b01d2c3a5355f5ed34a893674095bca4 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * Linux内核诊断工具--内核态load-monitor功能
 *
 * 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 <linux/rbtree.h>
#include <linux/cpu.h>
#include <linux/syscalls.h>
#include <asm-generic/rwonce.h> // for READ_ONCE(x)

#include <asm/irq_regs.h>

#include "internal.h"
#include "mm_tree.h"
#include "pub/trace_file.h"
#include "pub/variant_buffer.h"
#include "pub/trace_point.h"

#include "uapi/load_monitor.h"

static atomic64_t diag_nr_running = ATOMIC64_INIT(0);

static struct diag_load_monitor_settings load_monitor_settings;

static unsigned int load_monitor_alloced;

static struct mm_tree mm_tree;

unsigned long *orig_avenrun_r;
unsigned long *orig_avenrun;

static struct load_monitor_cpu_run ld_mon_cpu_run[NR_CPUS];
static struct diag_variant_buffer load_monitor_variant_buffer;

static ktime_t last_dump;

static void __maybe_unused clean_data(void)
{
	cleanup_mm_tree(&mm_tree);
}

#ifndef FSHIFT
#define FSHIFT		11		/* nr of bits of precision */
#endif
#ifndef FIXED_1
#define FIXED_1		(1<<FSHIFT)	/* 1.0 as fixed-point */
#endif
#ifndef LOAD_INT
#define LOAD_INT(x) ((x) >> FSHIFT)
#endif
#ifndef LOAD_FRAC
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
#endif

#if defined(UPSTREAM_4_19_32)
void diag_load_timer(struct diag_percpu_context *context)
{
	return;
}
#else

// https://www.spinics.net/lists/kernel/msg3582022.html
// fun remove from 5.8.rc3, just for build to add it here
#define task_contributes_to_load2(task)	((READ_ONCE(task->__state)  & TASK_UNINTERRUPTIBLE) != 0 && \
					 (task->flags & PF_FROZEN) == 0 && \
					 (READ_ONCE(task->__state) & TASK_NOLOAD) == 0)

static void load_monitor_ipi(void *ignore)
{
	struct load_monitor_cpu_run *cpu_run;
	struct task_struct *tsk;
	unsigned long flags;
	int cpu;

	tsk = current;
	cpu = smp_processor_id();
	if (cpu >= NR_CPUS)
		return;

	cpu_run = &ld_mon_cpu_run[cpu];

	cpu_run->id = get_cycles();
	cpu_run->et_type = et_load_monitor_cpu_run;
	cpu_run->cpu = cpu;
	do_diag_gettimeofday(&cpu_run->tv);

	diag_task_brief(tsk, &cpu_run->task);
	diag_task_kern_stack(tsk, &cpu_run->kern_stack);
	diag_task_user_stack(tsk, &cpu_run->user_stack);

	diag_variant_buffer_spin_lock(&load_monitor_variant_buffer, flags);
	diag_variant_buffer_reserve(&load_monitor_variant_buffer, sizeof(struct load_monitor_cpu_run));
	diag_variant_buffer_write_nolock(&load_monitor_variant_buffer,
					 cpu_run, sizeof(struct load_monitor_cpu_run));
	diag_variant_buffer_seal(&load_monitor_variant_buffer);
	diag_variant_buffer_spin_unlock(&load_monitor_variant_buffer, flags);
}

void diag_load_timer(struct diag_percpu_context *context)
{
	u64 ms;
	bool scare = false;
	unsigned long load_d;
	struct task_struct *g, *p;

	if (!load_monitor_settings.activated)
		return;
	if (!load_monitor_settings.threshold_load && !load_monitor_settings.threshold_load_r && !load_monitor_settings.threshold_load_d
		&& !load_monitor_settings.threshold_task_d)
		return;
	if (smp_processor_id() != 0)
		return;

	if (load_monitor_settings.threshold_load && orig_avenrun
			&& LOAD_INT(orig_avenrun[0]) >= load_monitor_settings.threshold_load)
		scare = true;
	if (load_monitor_settings.threshold_load_r && orig_avenrun_r
			&& LOAD_INT(orig_avenrun_r[0]) >= load_monitor_settings.threshold_load_r)
		scare = true;
	if (load_monitor_settings.threshold_load_d && orig_avenrun && orig_avenrun_r) {
		load_d = LOAD_INT(orig_avenrun[0] - orig_avenrun_r[0]);

		if (load_d >= load_monitor_settings.threshold_load_d && load_d < 999999)
			scare = true;
	}
	if (load_monitor_settings.threshold_task_d) {
		int nr_uninterrupt = 0;
		struct task_struct *g, *p;

		rcu_read_lock();

		do_each_thread(g, p) {
			if (task_contributes_to_load2(p))
				nr_uninterrupt++;
		} while_each_thread(g, p);

		rcu_read_unlock();

		if (nr_uninterrupt >= load_monitor_settings.threshold_task_d)
			scare = true;
	}

	if (scare) {
		unsigned long flags;
		static struct load_monitor_detail detail;
		static struct load_monitor_task tsk_info;
		unsigned long event_id;

		ms = ktime_to_ms(ktime_sub(ktime_get(), last_dump));
		if (!load_monitor_settings.mass && ms < 10 * 1000)
			return;

		last_dump = ktime_get();
		
		if (orig_avenrun) {
			detail.load_1_1 = LOAD_INT(orig_avenrun[0]);
			detail.load_1_2 = LOAD_FRAC(orig_avenrun[0]);
			detail.load_5_1 = LOAD_INT(orig_avenrun[1]);
			detail.load_5_2 = LOAD_FRAC(orig_avenrun[1]);
			detail.load_15_1 = LOAD_INT(orig_avenrun[2]);
			detail.load_15_2 = LOAD_FRAC(orig_avenrun[2]);
		}
		if (orig_avenrun && orig_avenrun_r) {
			unsigned long l1, l2, l3;

			detail.load_r_1_1 = LOAD_INT(orig_avenrun_r[0]);
			detail.load_r_1_2 = LOAD_FRAC(orig_avenrun_r[0]);
			detail.load_r_5_1 = LOAD_INT(orig_avenrun_r[1]);
			detail.load_r_5_2 = LOAD_FRAC(orig_avenrun_r[1]);
			detail.load_r_15_1 = LOAD_INT(orig_avenrun_r[2]);
			detail.load_r_15_2 = LOAD_FRAC(orig_avenrun_r[2]);
			l1 = orig_avenrun[0] - orig_avenrun_r[0];
			l2 = orig_avenrun[1] - orig_avenrun_r[1];
			l3 = orig_avenrun[2] - orig_avenrun_r[2];
			detail.load_d_1_1 = LOAD_INT(l1);
			detail.load_d_1_2 = LOAD_FRAC(l1);
			detail.load_d_5_1 = LOAD_INT(l2);
			detail.load_d_5_2 = LOAD_FRAC(l2);
			detail.load_d_15_1 = LOAD_INT(l3);
			detail.load_d_15_2 = LOAD_FRAC(l3);
		}

		event_id = get_cycles();
		detail.id = event_id;
		detail.et_type = et_load_monitor_detail;
		do_diag_gettimeofday(&detail.tv);

		rcu_read_lock();
		diag_variant_buffer_spin_lock(&load_monitor_variant_buffer, flags);
		diag_variant_buffer_reserve(&load_monitor_variant_buffer, sizeof(struct load_monitor_detail));
		diag_variant_buffer_write_nolock(&load_monitor_variant_buffer, &detail, sizeof(struct load_monitor_detail));
		diag_variant_buffer_seal(&load_monitor_variant_buffer);
		diag_variant_buffer_spin_unlock(&load_monitor_variant_buffer, flags);
		do_each_thread(g, p) {
// [[PATCH v2 7/7] sched: Change task_struct::state](https://lore.kernel.org/all/[email protected]/#r)
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 13, 0)
			if ((p->state == TASK_RUNNING) || task_contributes_to_load2(p)) {
#else
			if ((p->__state == TASK_RUNNING) || task_contributes_to_load2(p)) {
#endif
				tsk_info.et_type = et_load_monitor_task;
				tsk_info.id = event_id;
				tsk_info.tv = detail.tv;
				diag_task_brief(p, &tsk_info.task);
				diag_task_kern_stack(p, &tsk_info.kern_stack);
				dump_proc_chains_argv(load_monitor_settings.style, &mm_tree, p, &tsk_info.proc_chains);
				diag_variant_buffer_spin_lock(&load_monitor_variant_buffer, flags);
				diag_variant_buffer_reserve(&load_monitor_variant_buffer, sizeof(struct load_monitor_task));
				diag_variant_buffer_write_nolock(&load_monitor_variant_buffer,
					&tsk_info, sizeof(struct load_monitor_task));
				diag_variant_buffer_seal(&load_monitor_variant_buffer);
				diag_variant_buffer_spin_unlock(&load_monitor_variant_buffer, flags);
			}
		} while_each_thread(g, p);
		rcu_read_unlock();

		if (!load_monitor_settings.mass && load_monitor_settings.cpu_run) {
			smp_call_function(load_monitor_ipi, NULL, 1);
			load_monitor_ipi(NULL);
		}
	}
}
#endif

#if KERNEL_VERSION(4, 9, 0) <= LINUX_VERSION_CODE
__maybe_unused static void trace_sched_process_exec_hit(void *__data,
	struct task_struct *tsk,
	pid_t old_pid,
	struct linux_binprm *bprm)
#elif KERNEL_VERSION(3, 10, 0) <= LINUX_VERSION_CODE
__maybe_unused static void trace_sched_process_exec_hit(void *__data,
	struct task_struct *tsk,
	pid_t old_pid,
	struct linux_binprm *bprm)
#endif
#if KERNEL_VERSION(3, 10, 0) <= LINUX_VERSION_CODE
{
	atomic64_inc_return(&diag_nr_running);
	diag_hook_exec(bprm, &mm_tree);
	atomic64_dec_return(&diag_nr_running);
}
#endif

#if KERNEL_VERSION(4, 9, 0) <= LINUX_VERSION_CODE
static void trace_sched_process_exit_hit(void *__data, struct task_struct *tsk)
#elif KERNEL_VERSION(3, 10, 0) <= LINUX_VERSION_CODE
static void trace_sched_process_exit_hit(void *__data, struct task_struct *tsk)
#else
static void trace_sched_process_exit_hit(struct task_struct *tsk)
#endif
{
	diag_hook_process_exit_exec(tsk, &mm_tree);
}

static int __activate_load_monitor(void)
{
	int ret = 0;

	clean_data();

	ret = alloc_diag_variant_buffer(&load_monitor_variant_buffer);
	if (ret)
		goto out_variant_buffer;
	load_monitor_alloced = 1;

	if (load_monitor_settings.style == 1) {
#if KERNEL_VERSION(3, 10, 0) <= LINUX_VERSION_CODE
		hook_tracepoint("sched_process_exec", trace_sched_process_exec_hit, NULL);
#endif
		hook_tracepoint("sched_process_exit", trace_sched_process_exit_hit, NULL);
	}
	//get_argv_processes(&mm_tree);

	return 1;
out_variant_buffer:
	return 0;
}

int activate_load_monitor(void)
{
	if (!load_monitor_settings.activated)
		load_monitor_settings.activated = __activate_load_monitor();

	return load_monitor_settings.activated;
}

static void __deactivate_load_monitor(void)
{
	if (load_monitor_settings.style == 1) {
#if KERNEL_VERSION(3, 10, 0) <= LINUX_VERSION_CODE
		unhook_tracepoint("sched_process_exec", trace_sched_process_exec_hit, NULL);
#endif
		unhook_tracepoint("sched_process_exit", trace_sched_process_exit_hit, NULL);
	}

	synchronize_sched();
	msleep(10);
	while (atomic64_read(&diag_nr_running) > 0) {
		msleep(10);
	}

	clean_data();

	load_monitor_settings.verbose = 0;
	load_monitor_settings.threshold_load = 0;
	load_monitor_settings.threshold_load_r = 0;
	load_monitor_settings.threshold_load_d = 0;
	load_monitor_settings.threshold_task_d = 0;
	last_dump = ktime_set(0, 0);
}

int deactivate_load_monitor(void)
{
	if (load_monitor_settings.activated)
		__deactivate_load_monitor();
	load_monitor_settings.activated = 0;

	return load_monitor_settings.activated;
}

int load_monitor_syscall(struct pt_regs *regs, long id)
{
	int __user *user_ptr_len;
	size_t __user user_buf_len;
	void __user *user_buf;
	int ret = 0;
	struct diag_load_monitor_settings settings;

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

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

		if (user_buf_len != sizeof(struct diag_load_monitor_settings)) {
			ret = -EINVAL;
		} else {
			settings = load_monitor_settings;
			ret = copy_to_user(user_buf, &settings, user_buf_len);
		}
		break;
	case DIAG_LOAD_MONITOR_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 (!load_monitor_alloced) {
			ret = -EINVAL;
		} else {
			ret = copy_to_user_variant_buffer(&load_monitor_variant_buffer,
					user_ptr_len, user_buf, user_buf_len);
			record_dump_cmd("load-monitor");
		}
		break;
	default:
		ret = -ENOSYS;
		break;
	}

	return ret;
}

long diag_ioctl_load_monitor(unsigned int cmd, unsigned long arg)
{
	int ret = 0;
	struct diag_load_monitor_settings settings;
	struct diag_ioctl_dump_param dump_param;

	switch (cmd) {
	case CMD_LOAD_MONITOR_SET:
		if (load_monitor_settings.activated) {
			ret = -EBUSY;
		} else {
			ret = copy_from_user(&settings, (void *)arg, sizeof(struct diag_load_monitor_settings));
			if (!ret) {
				load_monitor_settings = settings;
			}
		}
		break;
	case CMD_LOAD_MONITOR_SETTINGS:
		settings = load_monitor_settings;
		ret = copy_to_user((void *)arg, &settings, sizeof(struct diag_load_monitor_settings));
		break;
	case CMD_LOAD_MONITOR_DUMP:
		ret = copy_from_user(&dump_param, (void *)arg, sizeof(struct diag_ioctl_dump_param));
		if (!load_monitor_alloced) {
			ret = -EINVAL;
		} else if (!ret) {
			ret = copy_to_user_variant_buffer(&load_monitor_variant_buffer,
					dump_param.user_ptr_len, dump_param.user_buf, dump_param.user_buf_len);
			record_dump_cmd("load-monitor");
		}
		break;
	default:
		ret = -ENOSYS;
		break;
	}

	return ret;
}

int diag_load_init(void)
{
	LOOKUP_SYMS_NORET(avenrun_r);
	LOOKUP_SYMS_NORET(avenrun);

	init_mm_tree(&mm_tree);
	init_diag_variant_buffer(&load_monitor_variant_buffer, 50 * 1024 * 1024);
	if (load_monitor_settings.activated)
		load_monitor_settings.activated = __activate_load_monitor();

	return 0;
}

void diag_load_exit(void)
{
	if (load_monitor_settings.activated)
		deactivate_load_monitor();
	load_monitor_settings.activated = 0;
	destroy_diag_variant_buffer(&load_monitor_variant_buffer);
}