Skip to contentSkip to conversation

allow preemption in check_task_state

PATCH Nicholas Mc Guire 3 participants 9 messages Feb 10, 2014 Scheduler

lore
Share & tools
lore
#0 lore raw

A lockfree approach to check_task_state

This treates the state as an indicator variable and use it to probe saved_state lock free. There is actually no consistency demand on state/saved_state but rather a consistency demand on the transitions of the two variables but those transition, based on path inspection, are not independent.

Its probably not faster than the lock/unlock case if uncontended - atleast it does not show up in benchmark results, but it would never be hit by a full pi-boost cycle as there is no contention.

This also was tested against the test-case from Sebastian as well as rnning a few scripted gdb breakpoint debugging/single-stepping loops to trigger this.

---
 kernel/sched/core.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
kernel/sched/core.c +8 -2
index bf93f63..5690ba3 100644
@@ -1074,11 +1074,17 @@ static int migration_cpu_stop(void *data);
 static bool check_task_state(struct task_struct *p, long match_state)
 {
 	bool match = false;
+	long state, saved_state;
+
+	/* catch restored state */
+	do {
+		state = p->state;
+		saved_state = p->saved_state;
+		rmb();  /* make sure we actually catch updates */
+	} while (state != p->state);
 
-	raw_spin_lock_irq(&p->pi_lock);
 	if (p->state == match_state || p->saved_state == match_state)
 		match = true;
-	raw_spin_unlock_irq(&p->pi_lock);
 
 	return match;
 }
Signature · 1 line
1.7.2.5
#1 lore raw

Subject is missing patch number.

Quoted 9 lines · On Mon, 10 Feb 2014 16:38:56 +0100 Nicholas Mc Guire <[email protected]> wrote:

A lockfree approach to check_task_state

This treates the state as an indicator variable and use it to probe saved_state lock free. There is actually no consistency demand on state/saved_state but rather a consistency demand on the transitions of the two variables but those transition, based on path inspection, are not independent.

Quoted 7 lines

Its probably not faster than the lock/unlock case if uncontended - atleast it does not show up in benchmark results, but it would never be hit by a full pi-boost cycle as there is no contention.

This also was tested against the test-case from Sebastian as well as rnning a few scripted gdb breakpoint debugging/single-stepping loops to trigger this.

To trigger what?

Quoted 13 lines
---
 kernel/sched/core.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
kernel/sched/core.c +0 -0
index bf93f63..5690ba3 100644

kernel/sched/core.c static int migration_cpu_stop(void *data); · line 1083

Quoted 10 lines
kernel/sched/core.c +7 -0
@@ -1074,11 +1074,17 @@ static int migration_cpu_stop(void *data);
 static bool check_task_state(struct task_struct *p, long match_state)
 {
 	bool match = false;
+	long state, saved_state;
+
+	/* catch restored state */
+	do {
+		state = p->state;
+		saved_state = p->saved_state;
+		rmb();  /* make sure we actually catch updates */

The problem I have with this is that there's no matching wmb(). Also, shouldn't that be a smp_rmb(), I don't think we can race with devices here.

kernel/sched/core.c static int migration_cpu_stop(void *data); · line 1090

Quoted 9 lines
kernel/sched/core.c +1 -2
+	} while (state != p->state);
 
-	raw_spin_lock_irq(&p->pi_lock);
 	if (p->state == match_state || p->saved_state == match_state)
 		match = true;
-	raw_spin_unlock_irq(&p->pi_lock);
 
 	return match;
 }

In rtmutex.c we have:

	pi_lock(&self->pi_lock);
	__set_current_state(self->saved_state);
	self->saved_state = TASK_RUNNING;
	pi_unlock(&self->pi_lock);

As there is no wmb() here, it can be very possible that another CPU will see saved_state as TASK_RUNNING, and current state as TASK_RUNNING, and miss the update completely.

I would not want to add a wmb() unless there is a real bug with the check state, as the above is in a very fast path and the check state is in a slower path.

-- Steve

#2 lore raw
Quoted 19 lines · Steven Rostedt

Subject is missing patch number.

Quoted 13 lines · On Mon, 10 Feb 2014 16:38:56 +0100 Nicholas Mc Guire <[email protected]> wrote:

A lockfree approach to check_task_state

This treates the state as an indicator variable and use it to probe saved_state lock free. There is actually no consistency demand on state/saved_state but rather a consistency demand on the transitions of the two variables but those transition, based on path inspection, are not independent.

Its probably not faster than the lock/unlock case if uncontended - atleast it does not show up in benchmark results, but it would never be hit by a full pi-boost cycle as there is no contention.

Quoted 5 lines
Quoted 3 lines

This also was tested against the test-case from Sebastian as well as rnning a few scripted gdb breakpoint debugging/single-stepping loops to trigger this.

To trigger what?

sorry should have included that in the patch header the testcase that Sebastian Andrzej Siewior had - available at:
http://breakpoint.cc/ptrace-test.c
the test-case triggers missing the state update.

Quoted 20 lines
Quoted 20 lines
---
 kernel/sched/core.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
kernel/sched/core.c +3 -0
index bf93f63..5690ba3 100644
@@ -1074,11 +1074,17 @@ static int migration_cpu_stop(void *data);
 static bool check_task_state(struct task_struct *p, long match_state)
 {
 	bool match = false;
+	long state, saved_state;
+
+	/* catch restored state */
Quoted 8 lines

kernel/sched/core.c static int migration_cpu_stop(void *data); · line 1083

Quoted 4 lines
kernel/sched/core.c +4 -0
+	do {
+		state = p->state;
+		saved_state = p->saved_state;
+		rmb();  /* make sure we actually catch updates */

The problem I have with this is that there's no matching wmb(). Also, shouldn't that be a smp_rmb(), I don't think we can race with devices here.

Sebastian also mentioned that - I simply was not sure on this - still not into this deep enough I guess .

Quoted 19 lines

kernel/sched/core.c static int migration_cpu_stop(void *data); · line 1090

Quoted 10 lines
kernel/sched/core.c +1 -2
+	} while (state != p->state);
 
-	raw_spin_lock_irq(&p->pi_lock);
 	if (p->state == match_state || p->saved_state == match_state)
 		match = true;
-	raw_spin_unlock_irq(&p->pi_lock);
 
 	return match;
 }

In rtmutex.c we have:

	pi_lock(&self->pi_lock);
	__set_current_state(self->saved_state);
	self->saved_state = TASK_RUNNING;
	pi_unlock(&self->pi_lock);
Quoted 8 lines

As there is no wmb() here, it can be very possible that another CPU will see saved_state as TASK_RUNNING, and current state as TASK_RUNNING, and miss the update completely.

I would not want to add a wmb() unless there is a real bug with the check state, as the above is in a very fast path and the check state is in a slower path.

maybe I'm missing/missunderstanding something here but pi_unlock -> arch_spin_unlock is a full mb()
so once any task did an update of the state the loop should be catching this update ? if the loop exits before the updat takes effect (pi_unlock) would that be ncorrect ?

thx!
hofrat

maintainer
#3 lore raw
Quoted 2 lines · Nicholas Mc Guire

maybe I'm missing/missunderstanding something here but pi_unlock -> arch_spin_unlock is a full mb()

Nope, arch_spin_unlock() on x86 is a single add[wb] without LOCK prefix.

The lock and unlock primitives are in general specified to have ACQUIRE resp. RELEASE semantics.

See Documentation/memory-barriers.txt for far too much head-hurting details.

#4 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 2 lines · Nicholas Mc Guire

maybe I'm missing/missunderstanding something here but pi_unlock -> arch_spin_unlock is a full mb()

Nope, arch_spin_unlock() on x86 is a single add[wb] without LOCK prefix.

The lock and unlock primitives are in general specified to have ACQUIRE resp. RELEASE semantics.

See Documentation/memory-barriers.txt for far too much head-hurting details.

I did check that - but from the code check it seems to me to be using a lock prefix in the fast __add() path and an explicit smp_add() in the slow path (arch/x86/include/asm/spinlock.h:arch_spin_unlock) the __add from arch/x86/include/asm/cmpxchg.h does lock or am I missinterpreting this ? the other archs I believe were all doing explicit mb()/smp_mb() in the arch_spin_unlock - will go check this again.

thx!
hofrat

maintainer
#5 lore raw
Quoted 14 lines · Nicholas Mc Guire
Quoted 11 lines · Peter Zijlstra
Quoted 2 lines · Nicholas Mc Guire

maybe I'm missing/missunderstanding something here but pi_unlock -> arch_spin_unlock is a full mb()

Nope, arch_spin_unlock() on x86 is a single add[wb] without LOCK prefix.

The lock and unlock primitives are in general specified to have ACQUIRE resp. RELEASE semantics.

See Documentation/memory-barriers.txt for far too much head-hurting details.

Quoted 6 lines

I did check that - but from the code check it seems to me to be using a lock prefix in the fast __add() path and an explicit smp_add() in the slow path (arch/x86/include/asm/spinlock.h:arch_spin_unlock) the __add from arch/x86/include/asm/cmpxchg.h does lock or am I missinterpreting this ? the other archs I believe were all doing explicit mb()/smp_mb() in the arch_spin_unlock - will go check this again.

It uses UNLOCK_LOCK_PREFIX, which if you look carefully, is normally always "". Only some 'broken' i386 chips require a LOCK there.

#6 lore raw
Quoted 22 lines · Peter Zijlstra
Quoted 14 lines · Nicholas Mc Guire
Quoted 11 lines · Peter Zijlstra
Quoted 2 lines · Nicholas Mc Guire

maybe I'm missing/missunderstanding something here but pi_unlock -> arch_spin_unlock is a full mb()

Nope, arch_spin_unlock() on x86 is a single add[wb] without LOCK prefix.

The lock and unlock primitives are in general specified to have ACQUIRE resp. RELEASE semantics.

See Documentation/memory-barriers.txt for far too much head-hurting details.

Quoted 6 lines

I did check that - but from the code check it seems to me to be using a lock prefix in the fast __add() path and an explicit smp_add() in the slow path (arch/x86/include/asm/spinlock.h:arch_spin_unlock) the __add from arch/x86/include/asm/cmpxchg.h does lock or am I missinterpreting this ? the other archs I believe were all doing explicit mb()/smp_mb() in the arch_spin_unlock - will go check this again.

Quoted 2 lines

It uses UNLOCK_LOCK_PREFIX, which if you look carefully, is normally always "". Only some 'broken' i386 chips require a LOCK there.

thanks for the details - will go dig through this again - still missing seme bits.

the first patch proposal is taken care of I guess :)

thx!
hofrat

#7 lore raw
In reply to Nicholas Mc Guire
Quoted 16 lines · On Mon, 10 Feb 2014 18:17:12 +0100 Nicholas Mc Guire <[email protected]> wrote:
Quoted 8 lines

In rtmutex.c we have:

	pi_lock(&self->pi_lock);
	__set_current_state(self->saved_state);
	self->saved_state = TASK_RUNNING;
	pi_unlock(&self->pi_lock);
Quoted 8 lines

As there is no wmb() here, it can be very possible that another CPU will see saved_state as TASK_RUNNING, and current state as TASK_RUNNING, and miss the update completely.

I would not want to add a wmb() unless there is a real bug with the check state, as the above is in a very fast path and the check state is in a slower path.

Quoted 5 lines

maybe I'm missing/missunderstanding something here but pi_unlock -> arch_spin_unlock is a full mb()
so once any task did an update of the state the loop should be catching this update ? if the loop exits before the updat takes effect (pi_unlock) would that be ncorrect ?

Even if the spin locks were full memory barriers, it is still buggy. The fact that we set current_state to saved_state, and then saved_state to TASK_RUNNING without any memory barriers in between those two statements, means that the reader (even with a rmb()) can still see both as TASK_RUNNING.

-- Steve

#8 lore raw
Quoted 26 lines · Steven Rostedt
Quoted 16 lines · On Mon, 10 Feb 2014 18:17:12 +0100 Nicholas Mc Guire <[email protected]> wrote:
Quoted 8 lines

In rtmutex.c we have:

	pi_lock(&self->pi_lock);
	__set_current_state(self->saved_state);
	self->saved_state = TASK_RUNNING;
	pi_unlock(&self->pi_lock);
Quoted 8 lines

As there is no wmb() here, it can be very possible that another CPU will see saved_state as TASK_RUNNING, and current state as TASK_RUNNING, and miss the update completely.

I would not want to add a wmb() unless there is a real bug with the check state, as the above is in a very fast path and the check state is in a slower path.

Quoted 5 lines

maybe I'm missing/missunderstanding something here but pi_unlock -> arch_spin_unlock is a full mb()
so once any task did an update of the state the loop should be catching this update ? if the loop exits before the updat takes effect (pi_unlock) would that be ncorrect ?

Quoted 6 lines

Even if the spin locks were full memory barriers, it is still buggy. The fact that we set current_state to saved_state, and then saved_state to TASK_RUNNING without any memory barriers in between those two statements, means that the reader (even with a rmb()) can still see both as TASK_RUNNING.

ok - thanks - I think now I got it.

thx!
hofrat