aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/oprofile/nmi_int.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86/oprofile/nmi_int.c')
-rw-r--r--arch/x86/oprofile/nmi_int.c187
1 files changed, 113 insertions, 74 deletions
diff --git a/arch/x86/oprofile/nmi_int.c b/arch/x86/oprofile/nmi_int.c
index 9f001d904599..b28d2f1253bb 100644
--- a/arch/x86/oprofile/nmi_int.c
+++ b/arch/x86/oprofile/nmi_int.c
@@ -31,8 +31,9 @@ static struct op_x86_model_spec *model;
31static DEFINE_PER_CPU(struct op_msrs, cpu_msrs); 31static DEFINE_PER_CPU(struct op_msrs, cpu_msrs);
32static DEFINE_PER_CPU(unsigned long, saved_lvtpc); 32static DEFINE_PER_CPU(unsigned long, saved_lvtpc);
33 33
34/* 0 == registered but off, 1 == registered and on */ 34/* must be protected with get_online_cpus()/put_online_cpus(): */
35static int nmi_enabled = 0; 35static int nmi_enabled;
36static int ctr_running;
36 37
37struct op_counter_config counter_config[OP_MAX_COUNTER]; 38struct op_counter_config counter_config[OP_MAX_COUNTER];
38 39
@@ -61,12 +62,16 @@ static int profile_exceptions_notify(struct notifier_block *self,
61{ 62{
62 struct die_args *args = (struct die_args *)data; 63 struct die_args *args = (struct die_args *)data;
63 int ret = NOTIFY_DONE; 64 int ret = NOTIFY_DONE;
64 int cpu = smp_processor_id();
65 65
66 switch (val) { 66 switch (val) {
67 case DIE_NMI: 67 case DIE_NMI:
68 case DIE_NMI_IPI: 68 case DIE_NMI_IPI:
69 model->check_ctrs(args->regs, &per_cpu(cpu_msrs, cpu)); 69 if (ctr_running)
70 model->check_ctrs(args->regs, &__get_cpu_var(cpu_msrs));
71 else if (!nmi_enabled)
72 break;
73 else
74 model->stop(&__get_cpu_var(cpu_msrs));
70 ret = NOTIFY_STOP; 75 ret = NOTIFY_STOP;
71 break; 76 break;
72 default: 77 default:
@@ -95,24 +100,36 @@ static void nmi_cpu_save_registers(struct op_msrs *msrs)
95static void nmi_cpu_start(void *dummy) 100static void nmi_cpu_start(void *dummy)
96{ 101{
97 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs); 102 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
98 model->start(msrs); 103 if (!msrs->controls)
104 WARN_ON_ONCE(1);
105 else
106 model->start(msrs);
99} 107}
100 108
101static int nmi_start(void) 109static int nmi_start(void)
102{ 110{
111 get_online_cpus();
103 on_each_cpu(nmi_cpu_start, NULL, 1); 112 on_each_cpu(nmi_cpu_start, NULL, 1);
113 ctr_running = 1;
114 put_online_cpus();
104 return 0; 115 return 0;
105} 116}
106 117
107static void nmi_cpu_stop(void *dummy) 118static void nmi_cpu_stop(void *dummy)
108{ 119{
109 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs); 120 struct op_msrs const *msrs = &__get_cpu_var(cpu_msrs);
110 model->stop(msrs); 121 if (!msrs->controls)
122 WARN_ON_ONCE(1);
123 else
124 model->stop(msrs);
111} 125}
112 126
113static void nmi_stop(void) 127static void nmi_stop(void)
114{ 128{
129 get_online_cpus();
115 on_each_cpu(nmi_cpu_stop, NULL, 1); 130 on_each_cpu(nmi_cpu_stop, NULL, 1);
131 ctr_running = 0;
132 put_online_cpus();
116} 133}
117 134
118#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX 135#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
@@ -252,7 +269,10 @@ static int nmi_switch_event(void)
252 if (nmi_multiplex_on() < 0) 269 if (nmi_multiplex_on() < 0)
253 return -EINVAL; /* not necessary */ 270 return -EINVAL; /* not necessary */
254 271
255 on_each_cpu(nmi_cpu_switch, NULL, 1); 272 get_online_cpus();
273 if (ctr_running)
274 on_each_cpu(nmi_cpu_switch, NULL, 1);
275 put_online_cpus();
256 276
257 return 0; 277 return 0;
258} 278}
@@ -344,50 +364,6 @@ static struct notifier_block profile_exceptions_nb = {
344 .priority = 2 364 .priority = 2
345}; 365};
346 366
347static int nmi_setup(void)
348{
349 int err = 0;
350 int cpu;
351
352 if (!allocate_msrs())
353 return -ENOMEM;
354
355 /* We need to serialize save and setup for HT because the subset
356 * of msrs are distinct for save and setup operations
357 */
358
359 /* Assume saved/restored counters are the same on all CPUs */
360 err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
361 if (err)
362 goto fail;
363
364 for_each_possible_cpu(cpu) {
365 if (!cpu)
366 continue;
367
368 memcpy(per_cpu(cpu_msrs, cpu).counters,
369 per_cpu(cpu_msrs, 0).counters,
370 sizeof(struct op_msr) * model->num_counters);
371
372 memcpy(per_cpu(cpu_msrs, cpu).controls,
373 per_cpu(cpu_msrs, 0).controls,
374 sizeof(struct op_msr) * model->num_controls);
375
376 mux_clone(cpu);
377 }
378
379 err = register_die_notifier(&profile_exceptions_nb);
380 if (err)
381 goto fail;
382
383 on_each_cpu(nmi_cpu_setup, NULL, 1);
384 nmi_enabled = 1;
385 return 0;
386fail:
387 free_msrs();
388 return err;
389}
390
391static void nmi_cpu_restore_registers(struct op_msrs *msrs) 367static void nmi_cpu_restore_registers(struct op_msrs *msrs)
392{ 368{
393 struct op_msr *counters = msrs->counters; 369 struct op_msr *counters = msrs->counters;
@@ -421,19 +397,24 @@ static void nmi_cpu_shutdown(void *dummy)
421 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu)); 397 apic_write(APIC_LVTPC, per_cpu(saved_lvtpc, cpu));
422 apic_write(APIC_LVTERR, v); 398 apic_write(APIC_LVTERR, v);
423 nmi_cpu_restore_registers(msrs); 399 nmi_cpu_restore_registers(msrs);
400 if (model->cpu_down)
401 model->cpu_down();
424} 402}
425 403
426static void nmi_shutdown(void) 404static void nmi_cpu_up(void *dummy)
427{ 405{
428 struct op_msrs *msrs; 406 if (nmi_enabled)
407 nmi_cpu_setup(dummy);
408 if (ctr_running)
409 nmi_cpu_start(dummy);
410}
429 411
430 nmi_enabled = 0; 412static void nmi_cpu_down(void *dummy)
431 on_each_cpu(nmi_cpu_shutdown, NULL, 1); 413{
432 unregister_die_notifier(&profile_exceptions_nb); 414 if (ctr_running)
433 msrs = &get_cpu_var(cpu_msrs); 415 nmi_cpu_stop(dummy);
434 model->shutdown(msrs); 416 if (nmi_enabled)
435 free_msrs(); 417 nmi_cpu_shutdown(dummy);
436 put_cpu_var(cpu_msrs);
437} 418}
438 419
439static int nmi_create_files(struct super_block *sb, struct dentry *root) 420static int nmi_create_files(struct super_block *sb, struct dentry *root)
@@ -465,7 +446,6 @@ static int nmi_create_files(struct super_block *sb, struct dentry *root)
465 return 0; 446 return 0;
466} 447}
467 448
468#ifdef CONFIG_SMP
469static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action, 449static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
470 void *data) 450 void *data)
471{ 451{
@@ -473,10 +453,10 @@ static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
473 switch (action) { 453 switch (action) {
474 case CPU_DOWN_FAILED: 454 case CPU_DOWN_FAILED:
475 case CPU_ONLINE: 455 case CPU_ONLINE:
476 smp_call_function_single(cpu, nmi_cpu_start, NULL, 0); 456 smp_call_function_single(cpu, nmi_cpu_up, NULL, 0);
477 break; 457 break;
478 case CPU_DOWN_PREPARE: 458 case CPU_DOWN_PREPARE:
479 smp_call_function_single(cpu, nmi_cpu_stop, NULL, 1); 459 smp_call_function_single(cpu, nmi_cpu_down, NULL, 1);
480 break; 460 break;
481 } 461 }
482 return NOTIFY_DONE; 462 return NOTIFY_DONE;
@@ -485,7 +465,75 @@ static int oprofile_cpu_notifier(struct notifier_block *b, unsigned long action,
485static struct notifier_block oprofile_cpu_nb = { 465static struct notifier_block oprofile_cpu_nb = {
486 .notifier_call = oprofile_cpu_notifier 466 .notifier_call = oprofile_cpu_notifier
487}; 467};
488#endif 468
469static int nmi_setup(void)
470{
471 int err = 0;
472 int cpu;
473
474 if (!allocate_msrs())
475 return -ENOMEM;
476
477 /* We need to serialize save and setup for HT because the subset
478 * of msrs are distinct for save and setup operations
479 */
480
481 /* Assume saved/restored counters are the same on all CPUs */
482 err = model->fill_in_addresses(&per_cpu(cpu_msrs, 0));
483 if (err)
484 goto fail;
485
486 for_each_possible_cpu(cpu) {
487 if (!cpu)
488 continue;
489
490 memcpy(per_cpu(cpu_msrs, cpu).counters,
491 per_cpu(cpu_msrs, 0).counters,
492 sizeof(struct op_msr) * model->num_counters);
493
494 memcpy(per_cpu(cpu_msrs, cpu).controls,
495 per_cpu(cpu_msrs, 0).controls,
496 sizeof(struct op_msr) * model->num_controls);
497
498 mux_clone(cpu);
499 }
500
501 nmi_enabled = 0;
502 ctr_running = 0;
503 barrier();
504 err = register_die_notifier(&profile_exceptions_nb);
505 if (err)
506 goto fail;
507
508 get_online_cpus();
509 register_cpu_notifier(&oprofile_cpu_nb);
510 on_each_cpu(nmi_cpu_setup, NULL, 1);
511 nmi_enabled = 1;
512 put_online_cpus();
513
514 return 0;
515fail:
516 free_msrs();
517 return err;
518}
519
520static void nmi_shutdown(void)
521{
522 struct op_msrs *msrs;
523
524 get_online_cpus();
525 unregister_cpu_notifier(&oprofile_cpu_nb);
526 on_each_cpu(nmi_cpu_shutdown, NULL, 1);
527 nmi_enabled = 0;
528 ctr_running = 0;
529 put_online_cpus();
530 barrier();
531 unregister_die_notifier(&profile_exceptions_nb);
532 msrs = &get_cpu_var(cpu_msrs);
533 model->shutdown(msrs);
534 free_msrs();
535 put_cpu_var(cpu_msrs);
536}
489 537
490#ifdef CONFIG_PM 538#ifdef CONFIG_PM
491 539
@@ -695,9 +743,6 @@ int __init op_nmi_init(struct oprofile_operations *ops)
695 return -ENODEV; 743 return -ENODEV;
696 } 744 }
697 745
698#ifdef CONFIG_SMP
699 register_cpu_notifier(&oprofile_cpu_nb);
700#endif
701 /* default values, can be overwritten by model */ 746 /* default values, can be overwritten by model */
702 ops->create_files = nmi_create_files; 747 ops->create_files = nmi_create_files;
703 ops->setup = nmi_setup; 748 ops->setup = nmi_setup;
@@ -724,12 +769,6 @@ int __init op_nmi_init(struct oprofile_operations *ops)
724 769
725void op_nmi_exit(void) 770void op_nmi_exit(void)
726{ 771{
727 if (using_nmi) { 772 if (using_nmi)
728 exit_sysfs(); 773 exit_sysfs();
729#ifdef CONFIG_SMP
730 unregister_cpu_notifier(&oprofile_cpu_nb);
731#endif
732 }
733 if (model->exit)
734 model->exit();
735} 774}