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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/*
* Linux内核诊断工具--内核态pmu功能
*
* Copyright (C) 2020 Alibaba Ltd.
*
* 作者: Wen Yang <[email protected]>
*
* License terms: GNU General Public License (GPL) version 3
*
*/
#include <linux/version.h>
#include <linux/delay.h>
#include <linux/kallsyms.h>
#include <linux/module.h>
#include "internal.h"
#include "pub/cgroup.h"
#include "pub/trace_point.h"
#include "pub/variant_buffer.h"
#include "uapi/pmu.h"
#include "pmu/pmu.h"
#include "pmu/debug.h"
#include "pub/mem_pool.h"
#include "pub/kprobe.h"
#if !defined(ALIOS_7U) && !defined(UBUNTU)
long diag_ioctl_pmu(unsigned int cmd, unsigned long arg)
{
return -ENOSYS;
}
int activate_pmu(void)
{
return 0;
}
int deactivate_pmu(void)
{
return 0;
}
int diag_pmu_init(void)
{
return 0;
}
void diag_pmu_exit(void)
{
return 0;
}
#else
extern struct diag_variant_buffer pmu_variant_buffer;
extern struct diag_pmu_settings pmu_settings;
extern struct ali_mem_pool mem_pool;
#define CGROUP_BUFFER_MAX_COUNT 5000
#define CGROUP_BUFFER_INIT_COUNT 1000
static unsigned long diag_pmu_buffer_curr = 0;
static unsigned long diag_pmu_buffer_grow = 0;
enum {
DIAG_PMU_NOT_LOAD = 0,
DIAG_PMU_LOADING,
DIAG_PMU_LOADED,
DIAG_PMU_EXITING,
DIAG_PMU_EXITED,
};
static int diag_pmu_module_state = DIAG_PMU_NOT_LOAD;
static DEFINE_SEMAPHORE(diag_pmu_sem);
__maybe_unused static struct kprobe kprobe_cgroup_destroy_locked;
__maybe_unused static struct kprobe kprobe_cgroup_populate_dir;
#if KERNEL_VERSION(4, 9, 0) <= LINUX_VERSION_CODE
static void trace_sched_switch_hit(void *__data, bool preempt,
struct task_struct *prev, struct task_struct *next)
#elif KERNEL_VERSION(3, 10, 0) <= LINUX_VERSION_CODE
static void trace_sched_switch_hit(void *__data,
struct task_struct *prev, struct task_struct *next)
#else
static void trace_sched_switch_hit(struct rq *rq, struct task_struct *prev,
struct task_struct *next)
#endif
{
struct pmu_percpu *record;
struct pmu_registers data = {0};
__maybe_unused cycles_t cycles_begin = 0;
__maybe_unused cycles_t cycles_mm_task_prev = 0;
__maybe_unused cycles_t cycles_mm_task_next = 0;
__maybe_unused cycles_t cycles_update_pmu_prev= 0;
__maybe_unused cycles_t cycles_update_pmu_next= 0;
__maybe_unused cycles_t cycles_end;
if (!pmu_settings.activated || !pmu_settings.sample)
return;
pmu_debug_get_cycles(cycles_begin);
pmu_read_core_registers(&data);
record = pmu_find_record(prev);
if (record) {
pmu_debug_get_cycles(cycles_mm_task_prev);
if (record->flags == PMU_FLAG_SCHED_IN) {
pmu_acc_delta(record, &data);
}
record->flags = PMU_FLAG_SCHED_OUT;
pmu_debug_get_cycles(cycles_update_pmu_prev);
}
record = pmu_find_record(next);
if (record) {
pmu_debug_get_cycles(cycles_mm_task_next);
pmu_refresh_counters(record, &data);
record->flags = PMU_FLAG_SCHED_IN;
pmu_debug_get_cycles(cycles_update_pmu_next);
}
pmu_debug_get_cycles(cycles_end);
pmu_debug_context_switch(cycles_begin,
cycles_mm_task_prev,
cycles_mm_task_next,
cycles_update_pmu_prev,
cycles_update_pmu_next,
cycles_end);
return;
}
static void pmu_cgroup_rmdir(struct cgroup *cgrp)
{
struct diag_pmu_detail *detail;
struct pmu_cgroup *pmu_cgrp;
struct pmu_registers data;
struct pmu_percpu *record;
unsigned long flags;
int cpu;
__maybe_unused cycles_t cycles_begin = 0;
__maybe_unused cycles_t cycles_dump = 0;
__maybe_unused cycles_t cycles_detach = 0;
if (!cgrp)
return;
pmu_cgrp = pmu_find_cgroup(cgrp);
if (unlikely(!pmu_cgrp))
return;
get_online_cpus();
preempt_disable();
pmu_debug_get_cycles(cycles_begin);
pmu_cgrp = pmu_find_cgroup(cgrp);
if (!pmu_cgrp) {
put_online_cpus();
return;
}
for_each_online_cpu(cpu) {
record = (struct pmu_percpu*)&(pmu_cgrp->percpu_data[cpu]);
if (record) {
memset(&data, 0, sizeof(data));
pmu_read_and_clear_record(&data, record);
if (!data.instructions && !data.cycles && !data.ref_cycles)
continue;
detail = &get_percpu_context()->pmu.detail;
pmu_fill_core_detail(detail, &data, cpu);
diag_variant_buffer_spin_lock(&pmu_variant_buffer, flags);
diag_variant_buffer_reserve(&pmu_variant_buffer, sizeof(*detail));
diag_variant_buffer_write_nolock(&pmu_variant_buffer, detail,
sizeof(*detail));
diag_variant_buffer_seal(&pmu_variant_buffer);
diag_variant_buffer_spin_unlock(&pmu_variant_buffer, flags);
}
}
put_online_cpus();
pmu_debug_get_cycles(cycles_dump);
pmu_detach_cgroup(cgrp);
pmu_debug_get_cycles(cycles_detach);
pmu_debug_cgroup_rmdir(cycles_begin, cycles_dump, cycles_detach);
preempt_enable();
}
#if KERNEL_VERSION(4, 19, 0) <= LINUX_VERSION_CODE
__maybe_unused static void trace_cgroup_rmdir_hit(void *__data,
struct cgroup *cgrp, const char *path)
{
pmu_cgroup_rmdir(cgrp);
}
#elif KERNEL_VERSION(4, 9, 0) <= LINUX_VERSION_CODE
__maybe_unused static void trace_cgroup_rmdir_hit(void *__data,
struct cgroup *cgrp)
{
pmu_cgroup_rmdir(cgrp);
}
#endif
static void pmu_cgroup_mkdir(struct cgroup *cgrp)
{
__maybe_unused cycles_t cycles_begin = 0;
__maybe_unused cycles_t cycles_end;
if (unlikely(!cgrp))
return;
#if KERNEL_VERSION(4, 8, 0) < LINUX_VERSION_CODE
if (!(cgrp->subtree_ss_mask & (1 << cpuacct_cgrp_id)))
return;
#else
if (!cgrp->subsys[cpuacct_subsys_id])
return;
#endif
preempt_disable();
pmu_debug_get_cycles(cycles_begin);
pmu_attach_cgroup(cgrp);
pmu_debug_get_cycles(cycles_end);
preempt_enable();
pmu_debug_cgroup_mkdir(cycles_begin, cycles_end);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0)
__maybe_unused static void trace_cgroup_mkdir_hit(void *__data,
struct cgroup *cgrp, const char *path)
{
pmu_cgroup_mkdir(cgrp);
}
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
__maybe_unused static void trace_cgroup_mkdir_hit(void *__data,
struct cgroup *cgrp)
{
pmu_cgroup_mkdir(cgrp);
}
#else
__maybe_unused static int kprobe_cgroup_populate_dir_pre(struct kprobe *p,
struct pt_regs *regs)
{
struct cgroup * cgrp = (void *)ORIG_PARAM1(regs);
pmu_cgroup_mkdir(cgrp);
return 0;
}
__maybe_unused static int kprobe_cgroup_destroy_locked_pre(struct kprobe *p,
struct pt_regs *regs)
{
struct cgroup * cgrp = (void *)ORIG_PARAM1(regs);
pmu_cgroup_rmdir(cgrp);
return 0;
}
#endif
static void pmu_unhook_cgroup_create(void)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
unhook_kprobe(&kprobe_cgroup_populate_dir);
#else
unhook_tracepoint("cgroup_mkdir", trace_cgroup_mkdir_hit, NULL);
#endif
}
static void pmu_unhook_cgroup_destroy(void)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
unhook_kprobe(&kprobe_cgroup_destroy_locked);
#else
unhook_tracepoint("cgroup_rmdir", trace_cgroup_rmdir_hit, NULL);
#endif
}
static int pmu_hook_cgroup_create(void)
{
int ret;
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 10, 0)
ret = hook_tracepoint("cgroup_mkdir", trace_cgroup_mkdir_hit, NULL);
#else
ret = hook_kprobe(&kprobe_cgroup_populate_dir, "cgroup_populate_dir",
kprobe_cgroup_populate_dir_pre, NULL);
#endif
if (ret)
pr_err("pmu: failed to hook cgroup_mkdir, ret=%d\n", ret);
return ret;
}
static int pmu_hook_cgroup_destroy(void)
{
int ret;
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 10, 0)
ret = hook_tracepoint("cgroup_rmdir", trace_cgroup_rmdir_hit, NULL);
#else
ret = hook_kprobe(&kprobe_cgroup_destroy_locked, "cgroup_destroy_locked",
kprobe_cgroup_destroy_locked_pre, NULL);
#endif
if (ret)
pr_err("pmu: failed to hook cgroup_rmdir, ret=%d\n", ret);
return ret;
}
static int __activate_pmu(void)
{
int ret = 0;
pmu_clean_data();
pmu_attach_all_cgroups();
ret = pmu_create_all_events();
if (ret) {
pr_err("pmu: failed to activate pmu, ret=%d\n", ret);
goto err_out;
}
ret = pmu_hook_cgroup_create();
if (ret)
goto err_detach;
ret = pmu_hook_cgroup_destroy();
if (ret) {
goto err_unhook_cgroup_mkdir;
}
ret = hook_tracepoint("sched_switch", trace_sched_switch_hit, NULL);
if (ret) {
pr_err("pmu: failed to hook sched_switch, ret=%d\n", ret);
goto err_unhook_cgroup_rmdir;
}
pmu_settings.activated = 1;
return 1;
err_unhook_cgroup_rmdir:
pmu_unhook_cgroup_destroy();
err_unhook_cgroup_mkdir:
pmu_unhook_cgroup_create();
err_detach:
synchronize_sched();
pmu_detach_all_cgroups();
pmu_destroy_all_events();
err_out:
return 0;
}
int activate_pmu(void)
{
int ret = 0;
down(&diag_pmu_sem);
if (!pmu_settings.activated)
ret = __activate_pmu();
up(&diag_pmu_sem);
return ret;
}
static int __deactivate_pmu(void)
{
int ret = 0;
unhook_tracepoint("sched_switch", trace_sched_switch_hit, NULL);
pmu_unhook_cgroup_create();
pmu_unhook_cgroup_destroy();
synchronize_sched();
msleep(10);
pmu_destroy_all_events();
pmu_detach_all_cgroups();
return ret;
}
int deactivate_pmu(void)
{
int ret = 0;
down(&diag_pmu_sem);
if (pmu_settings.activated) {
__deactivate_pmu();
} else {
ret = -EAGAIN;
}
pmu_settings.activated = 0;
up(&diag_pmu_sem);
return ret;
}
long diag_ioctl_pmu(unsigned int cmd, unsigned long arg)
{
int ret = -EINVAL;
int sample;
static struct diag_pmu_settings settings;
struct diag_ioctl_dump_param dump_param;
switch (cmd) {
case CMD_PMU_SET:
down(&diag_pmu_sem);
if (pmu_settings.activated) {
ret = -EBUSY;
} else {
ret = copy_from_user(&settings, (void *)arg, sizeof(struct diag_pmu_settings));
if (!ret) {
pmu_settings = settings;
pmu_settings.activated = 0;
}
}
up(&diag_pmu_sem);
break;
case CMD_PMU_SETTINGS:
settings = pmu_settings;
ret = copy_to_user((void *)arg, &settings, sizeof(struct diag_pmu_settings));
break;
case CMD_PMU_DUMP:
ret = copy_from_user(&dump_param, (void *)arg, sizeof(struct diag_ioctl_dump_param));
if (!ret) {
pmu_do_dump();
ret = copy_to_user_variant_buffer(&pmu_variant_buffer,
dump_param.user_ptr_len, dump_param.user_buf, dump_param.user_buf_len);
}
break;
case CMD_PMU_SAMPLE:
ret = copy_from_user(&sample, (void *)arg, sizeof(int));
if (!ret) {
pmu_settings.sample = sample;
}
break;
default:
break;
}
return ret;
}
static int diag_pmu_mem_pool_grow(unsigned int num)
{
int ret;
if (diag_pmu_buffer_curr + num > CGROUP_BUFFER_MAX_COUNT)
return -EINVAL;
ret = ali_mem_pool_putin(&mem_pool, num);
if (ret) {
pr_err("pmu: grow mem_pool failed, ret=%d, num=%u\n",
ret, num);
return ret;
}
diag_pmu_buffer_grow = num;
diag_pmu_buffer_curr += num;
return 0;
}
static int pmu_lookup_syms(void)
{
#if defined(DIAG_ARM64)
LOOKUP_SYMS(armpmu_read);
#else
LOOKUP_SYMS(x86_perf_event_update);
#endif
return 0;
}
int diag_pmu_init(void)
{
int ret;
WRITE_ONCE(diag_pmu_module_state, DIAG_PMU_LOADING);
diag_pmu_pool_init();
diag_pmu_radix_init();
ret = init_diag_variant_buffer(&pmu_variant_buffer, DIAG_PMU_VARIANT_BUF_LEN);
if (ret) {
pr_err("pmu: init variant_buffer failed, ret=%d\n", ret);
return ret;
}
ret = alloc_diag_variant_buffer(&pmu_variant_buffer);
if (ret) {
pr_err("pmu: alloc variant_buffer failed, ret=%d\n", ret);
goto out_destroy_variant_buffer;
}
ret = diag_pmu_mem_pool_grow(CGROUP_BUFFER_INIT_COUNT);
if (ret) {
goto out_destroy_variant_buffer;
}
if (pmu_lookup_syms()) {
ret = -EINVAL;
goto out_destroy_mem_pool;
}
diag_pmu_init_wq();
ret = pmu_debug_proc_create();
if (ret) {
goto out_destroy_mem_pool;
}
WRITE_ONCE(diag_pmu_module_state, DIAG_PMU_LOADED);
return 0;
out_destroy_mem_pool:
ali_mem_pool_destroy(&mem_pool);
out_destroy_variant_buffer:
destroy_diag_variant_buffer(&pmu_variant_buffer);
return ret;
}
void diag_pmu_exit(void)
{
down(&diag_pmu_sem);
WRITE_ONCE(diag_pmu_module_state, DIAG_PMU_EXITING);
pmu_debug_proc_destroy();
if (pmu_settings.activated) {
__deactivate_pmu();
}
destroy_diag_variant_buffer(&pmu_variant_buffer);
ali_mem_pool_destroy(&mem_pool);
WRITE_ONCE(diag_pmu_module_state, DIAG_PMU_EXITED);
up(&diag_pmu_sem);
}
#endif
|