Skip to contentSkip to conversation

schedutil: Address the r/w ordering race in kthread

RFC PATCH In review Joel Fernandes (Google) 4 participants 4 messages May 22, 2018 – May 23, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 schedutil: Address the r/w ordering race in kthread

Currently there is a race in schedutil code for slow-switch single-CPU systems. Fix it by enforcing ordering the write to work_in_progress to happen before the read of next_freq.

Kthread Sched update

sugov_work() sugov_update_single()

      lock();
      // The CPU is free to rearrange below
      // two in any order, so it may clear
      // the flag first and then read next
      // freq. Lets assume it does.
      work_in_progress = false

                                               if (work_in_progress)
                                                     return;

                                               sg_policy->next_freq = 0;
      freq = sg_policy->next_freq;
                                               sg_policy->next_freq = real-freq;
      unlock();

---
I split this into separate patch, because this race can also happen in mainline.

 kernel/sched/cpufreq_schedutil.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/sched/cpufreq_schedutil.c +7 -0
index 5c482ec38610..ce7749da7a44 100644
@@ -401,6 +401,13 @@ static void sugov_work(struct kthread_work *work)
 	 */
 	raw_spin_lock_irqsave(&sg_policy->update_lock, flags);
 	freq = sg_policy->next_freq;
+
+	/*
+	 * sugov_update_single can access work_in_progress without update_lock,
+	 * make sure next_freq is read before work_in_progress is set.
+	 */
+	smp_mb();
+
 	sg_policy->work_in_progress = false;
 	raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags);
 
Signature · 1 line
2.17.0.441.gb46fe60e1d-goog
#1 lore raw
Quoted 3 lines · Joel Fernandes (Google)

Currently there is a race in schedutil code for slow-switch single-CPU systems. Fix it by enforcing ordering the write to work_in_progress to happen before the read of next_freq.

Aargh, s/before/after/.

Commit log has above issue but code is Ok. Should I resend this patch or are there any additional comments? thanks!

- Joel

[..]

maintainer
#2 lore raw
In reply to Joel Fernandes (Google)

Hi Joel,

Quoted 47 lines · Joel Fernandes (Google)

Currently there is a race in schedutil code for slow-switch single-CPU systems. Fix it by enforcing ordering the write to work_in_progress to happen before the read of next_freq.

Kthread Sched update

sugov_work() sugov_update_single()

      lock();
      // The CPU is free to rearrange below
      // two in any order, so it may clear
      // the flag first and then read next
      // freq. Lets assume it does.
      work_in_progress = false

                                               if (work_in_progress)
                                                     return;

                                               sg_policy->next_freq = 0;
      freq = sg_policy->next_freq;
                                               sg_policy->next_freq = real-freq;
      unlock();

---
I split this into separate patch, because this race can also happen in mainline.

 kernel/sched/cpufreq_schedutil.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/sched/cpufreq_schedutil.c +0 -0
index 5c482ec38610..ce7749da7a44 100644

kernel/sched/cpufreq_schedutil.c static void sugov_work(struct kthread_work *work) · line 407

Quoted 7 lines
kernel/sched/cpufreq_schedutil.c +4 -0
@@ -401,6 +401,13 @@ static void sugov_work(struct kthread_work *work)
 	 */
 	raw_spin_lock_irqsave(&sg_policy->update_lock, flags);
 	freq = sg_policy->next_freq;
+
+	/*
+	 * sugov_update_single can access work_in_progress without update_lock,
+	 * make sure next_freq is read before work_in_progress is set.

s/set/reset/

kernel/sched/cpufreq_schedutil.c static void sugov_work(struct kthread_work *work) · line 410

Quoted 3 lines
kernel/sched/cpufreq_schedutil.c +3 -0
+	 */
+	smp_mb();
+

Also, doesn't this need a corresponding barrier (I guess in sugov_should_update_freq)? That being a wmb and this a rmb?

Best,

- Juri

#3 lore raw
In reply to Joel Fernandes (Google)
Quoted 47 lines · Joel Fernandes (Google)

Currently there is a race in schedutil code for slow-switch single-CPU systems. Fix it by enforcing ordering the write to work_in_progress to happen before the read of next_freq.

Kthread Sched update

sugov_work() sugov_update_single()

      lock();
      // The CPU is free to rearrange below
      // two in any order, so it may clear
      // the flag first and then read next
      // freq. Lets assume it does.
      work_in_progress = false

                                               if (work_in_progress)
                                                     return;

                                               sg_policy->next_freq = 0;
      freq = sg_policy->next_freq;
                                               sg_policy->next_freq = real-freq;
      unlock();

---
I split this into separate patch, because this race can also happen in mainline.

 kernel/sched/cpufreq_schedutil.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/sched/cpufreq_schedutil.c +0 -0
index 5c482ec38610..ce7749da7a44 100644

kernel/sched/cpufreq_schedutil.c static void sugov_work(struct kthread_work *work) · line 410

Quoted 10 lines
kernel/sched/cpufreq_schedutil.c +7 -0
@@ -401,6 +401,13 @@ static void sugov_work(struct kthread_work *work)
         */
        raw_spin_lock_irqsave(&sg_policy->update_lock, flags);
        freq = sg_policy->next_freq;
+
+       /*
+        * sugov_update_single can access work_in_progress without update_lock,
+        * make sure next_freq is read before work_in_progress is set.
+        */
+       smp_mb();
+

This requires a corresponding barrier somewhere else.

kernel/sched/cpufreq_schedutil.c static void sugov_work(struct kthread_work *work)

Quoted 4 lines
kernel/sched/cpufreq_schedutil.c +0 -1
        sg_policy->work_in_progress = false;
        raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags);
 
--

Also, as I said I actually would prefer to use the spinlock in the one-CPU case when the kthread is used.

I'll have a patch for that shortly.