sched/clock: Prevent tracing recursion in sched_clock_cpu()

Prevent tracing of preempt_disable/enable() in sched_clock_cpu().
When CONFIG_DEBUG_PREEMPT is enabled, preempt_disable/enable() are
traced and this causes trace_clock() users (and probably others) to
go into an infinite recursion. Systems with a stable sched_clock()
are not affected.

This problem is similar to that fixed by upstream commit 95ef1e52922
("KVM guest: prevent tracing recursion with kvmclock").

Signed-off-by: Fernando Luis Vazquez Cao <>
Signed-off-by: Peter Zijlstra <>
Acked-by: Steven Rostedt <>
Cc: Andrew Morton <>
Cc: Linus Torvalds <>
Signed-off-by: Ingo Molnar <>
diff --git a/kernel/sched/clock.c b/kernel/sched/clock.c
index 43c2bcc..b30a292 100644
--- a/kernel/sched/clock.c
+++ b/kernel/sched/clock.c
@@ -301,14 +301,14 @@
 	if (unlikely(!sched_clock_running))
 		return 0ull;
-	preempt_disable();
+	preempt_disable_notrace();
 	scd = cpu_sdc(cpu);
 	if (cpu != smp_processor_id())
 		clock = sched_clock_remote(scd);
 		clock = sched_clock_local(scd);
-	preempt_enable();
+	preempt_enable_notrace();
 	return clock;