Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8fe7e94e authored by Robert Richter's avatar Robert Richter
Browse files

oprofile, x86: Fix race in nmi handler while starting counters



In some rare cases, nmis are generated immediately after the nmi
handler of the cpu was started. This causes the counter not to be
enabled. Before enabling the nmi handlers we need to set variable
ctr_running first and make sure its value is written to memory.

Also, the patch makes all existing barriers a memory barrier instead
of a compiler barrier only.

Reported-by: default avatarSuravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Cc: <stable@kernel.org> # .35+
Signed-off-by: default avatarRobert Richter <robert.richter@amd.com>
parent 2c53b436
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -112,8 +112,10 @@ static void nmi_cpu_start(void *dummy)
static int nmi_start(void)
{
	get_online_cpus();
	on_each_cpu(nmi_cpu_start, NULL, 1);
	ctr_running = 1;
	/* make ctr_running visible to the nmi handler: */
	smp_mb();
	on_each_cpu(nmi_cpu_start, NULL, 1);
	put_online_cpus();
	return 0;
}
@@ -504,15 +506,18 @@ static int nmi_setup(void)

	nmi_enabled = 0;
	ctr_running = 0;
	barrier();
	/* make variables visible to the nmi handler: */
	smp_mb();
	err = register_die_notifier(&profile_exceptions_nb);
	if (err)
		goto fail;

	get_online_cpus();
	register_cpu_notifier(&oprofile_cpu_nb);
	on_each_cpu(nmi_cpu_setup, NULL, 1);
	nmi_enabled = 1;
	/* make nmi_enabled visible to the nmi handler: */
	smp_mb();
	on_each_cpu(nmi_cpu_setup, NULL, 1);
	put_online_cpus();

	return 0;
@@ -531,7 +536,8 @@ static void nmi_shutdown(void)
	nmi_enabled = 0;
	ctr_running = 0;
	put_online_cpus();
	barrier();
	/* make variables visible to the nmi handler: */
	smp_mb();
	unregister_die_notifier(&profile_exceptions_nb);
	msrs = &get_cpu_var(cpu_msrs);
	model->shutdown(msrs);