Skip to contentSkip to conversation

Pinning down a blocked task to extract diagnostics

DISCUSSION Paul E. McKenney 3 participants 12 messages Mar 5, 2020 – Mar 6, 2020 Scheduler

lore
Share & tools
lore
#0 lore raw

Hello!

Suppose that I need to extract diagnostics information from a blocked task, but that I absolutely cannot tolerate this task awakening in the midst of this extraction process. Is the following code the right way to make this work given a task "t"?

	raw_spin_lock_irq(&t->pi_lock);
	if (t->on_rq) {
		/* Task no longer blocked, so ignore it. */
	} else {
		/* Extract consistent diagnostic information. */
	}
	raw_spin_unlock_irq(&t->pi_lock);

It looks like all the wakeup paths acquire ->pi_lock, but I figured I should actually ask...

Thanx, Paul

#1 lore raw
Quoted 15 lines · On Wed, 4 Mar 2020 16:50:49 -0800 "Paul E. McKenney" <[email protected]> wrote:

Hello!

Suppose that I need to extract diagnostics information from a blocked task, but that I absolutely cannot tolerate this task awakening in the midst of this extraction process. Is the following code the right way to make this work given a task "t"?

	raw_spin_lock_irq(&t->pi_lock);
	if (t->on_rq) {
		/* Task no longer blocked, so ignore it. */
	} else {
		/* Extract consistent diagnostic information. */
	}
	raw_spin_unlock_irq(&t->pi_lock);
Quoted 2 lines

It looks like all the wakeup paths acquire ->pi_lock, but I figured I should actually ask...

IIUC, the rtmutex code uses pi_lock to tinker with the task while it is blocked (not woken). But the on_rq test may not be correct. It appears that can change without holding the task's pi_lock. I'm looking at the ttwu_do_activate() code which modifies the t->on_rq without holding the pi_lock. Seems you may need to check the p->state as well. See the comment in try_to_wake_up() about testing on_rq vs the state.

-- Steve

#2 lore raw
Quoted 21 lines · Steven Rostedt
Quoted 15 lines · On Wed, 4 Mar 2020 16:50:49 -0800 "Paul E. McKenney" <[email protected]> wrote:

Hello!

Suppose that I need to extract diagnostics information from a blocked task, but that I absolutely cannot tolerate this task awakening in the midst of this extraction process. Is the following code the right way to make this work given a task "t"?

	raw_spin_lock_irq(&t->pi_lock);
	if (t->on_rq) {
		/* Task no longer blocked, so ignore it. */
	} else {
		/* Extract consistent diagnostic information. */
	}
	raw_spin_unlock_irq(&t->pi_lock);
Quoted 2 lines

It looks like all the wakeup paths acquire ->pi_lock, but I figured I should actually ask...

Quoted 6 lines

IIUC, the rtmutex code uses pi_lock to tinker with the task while it is blocked (not woken). But the on_rq test may not be correct. It appears that can change without holding the task's pi_lock. I'm looking at the ttwu_do_activate() code which modifies the t->on_rq without holding the pi_lock. Seems you may need to check the p->state as well. See the comment in try_to_wake_up() about testing on_rq vs the state.

Thank you, Steve!

Thanx, Paul

maintainer
#3 lore raw
In reply to Paul E. McKenney
Quoted 15 lines · Paul E. McKenney

Hello!

Suppose that I need to extract diagnostics information from a blocked task, but that I absolutely cannot tolerate this task awakening in the midst of this extraction process. Is the following code the right way to make this work given a task "t"?

	raw_spin_lock_irq(&t->pi_lock);
	if (t->on_rq) {
		/* Task no longer blocked, so ignore it. */
	} else {
		/* Extract consistent diagnostic information. */
	}
	raw_spin_unlock_irq(&t->pi_lock);
Quoted 2 lines

It looks like all the wakeup paths acquire ->pi_lock, but I figured I should actually ask...

Close, the thing pi_lock actually guards is the t->state transition *to* TASK_WAKING/TASK_RUNNING, so something like this:

	raw_spin_lock_irq(&t->pi_lock);
	switch (t->state) {
	case TASK_RUNNING:
	case TASK_WAKING:
		/* ignore */
		break;

	default:
		/* Extract consistent diagnostic information. */
		break;
	}
	raw_spin_unlock_irq(&t->pi_lock);

ought to work. But if you're going to do this, please add a reference to that code in a comment on top of try_to_wake_up(), such that we can later find all the code that relies on this.

maintainer
#4 lore raw
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Paul E. McKenney

Hello!

Suppose that I need to extract diagnostics information from a blocked task, but that I absolutely cannot tolerate this task awakening in the midst of this extraction process. Is the following code the right way to make this work given a task "t"?

	raw_spin_lock_irq(&t->pi_lock);
	if (t->on_rq) {
		/* Task no longer blocked, so ignore it. */
	} else {
		/* Extract consistent diagnostic information. */
	}
	raw_spin_unlock_irq(&t->pi_lock);
Quoted 5 lines
Quoted 2 lines

It looks like all the wakeup paths acquire ->pi_lock, but I figured I should actually ask...

Close, the thing pi_lock actually guards is the t->state transition *to* TASK_WAKING/TASK_RUNNING, so something like this:

Almost, we must indeed also check ->on_rq, otherwise it might change the state back itself.

Quoted 9 lines
	raw_spin_lock_irq(&t->pi_lock);
	switch (t->state) {
	case TASK_RUNNING:
	case TASK_WAKING:
		/* ignore */
		break;

	default:
		if (t->on_rq)
			break;
Quoted 8 lines
		/* Extract consistent diagnostic information. */
		break;
	}
	raw_spin_unlock_irq(&t->pi_lock);

ought to work. But if you're going to do this, please add a reference to that code in a comment on top of try_to_wake_up(), such that we can later find all the code that relies on this.

#5 lore raw
Quoted 15 lines · On Thu, 5 Mar 2020 09:13:37 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 2 lines

Close, the thing pi_lock actually guards is the t->state transition *to* TASK_WAKING/TASK_RUNNING, so something like this:

Almost, we must indeed also check ->on_rq, otherwise it might change the state back itself.

Quoted 9 lines
	raw_spin_lock_irq(&t->pi_lock);
	switch (t->state) {
	case TASK_RUNNING:
	case TASK_WAKING:
		/* ignore */
		break;

	default:  

Don't we need a smp_rmb() here, otherwise we could have the same issue as described in the comment in try_to_wake_up()?

-- Steve

Quoted 11 lines
		if (t->on_rq)
			break;
Quoted 8 lines
		/* Extract consistent diagnostic information. */
		break;
	}
	raw_spin_unlock_irq(&t->pi_lock);

ought to work. But if you're going to do this, please add a reference to that code in a comment on top of try_to_wake_up(), such that we can later find all the code that relies on this.

#6 lore raw
In reply to Peter Zijlstra
Quoted 34 lines · Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Paul E. McKenney

Hello!

Suppose that I need to extract diagnostics information from a blocked task, but that I absolutely cannot tolerate this task awakening in the midst of this extraction process. Is the following code the right way to make this work given a task "t"?

	raw_spin_lock_irq(&t->pi_lock);
	if (t->on_rq) {
		/* Task no longer blocked, so ignore it. */
	} else {
		/* Extract consistent diagnostic information. */
	}
	raw_spin_unlock_irq(&t->pi_lock);
Quoted 5 lines
Quoted 2 lines

It looks like all the wakeup paths acquire ->pi_lock, but I figured I should actually ask...

Close, the thing pi_lock actually guards is the t->state transition *to* TASK_WAKING/TASK_RUNNING, so something like this:

Almost, we must indeed also check ->on_rq, otherwise it might change the state back itself.

Quoted 8 lines
	raw_spin_lock_irq(&t->pi_lock);
	switch (t->state) {
	case TASK_RUNNING:
	case TASK_WAKING:
		/* ignore */
		break;
Quoted 12 lines
Quoted 1 line

default:

		if (t->on_rq)
			break;
Quoted 8 lines
		/* Extract consistent diagnostic information. */
		break;
	}
	raw_spin_unlock_irq(&t->pi_lock);

ought to work. But if you're going to do this, please add a reference to that code in a comment on top of try_to_wake_up(), such that we can later find all the code that relies on this.

How about if I add something like this, located right by try_to_wake_up()?

	bool try_to_keep_sleeping(struct task_struct *t)
	{
		raw_spin_lock_irq(&t->pi_lock);
		switch (t->state) {
		case TASK_RUNNING:
		case TASK_WAKING:
			raw_spin_unlock_irq(&t->pi_lock);
			return false;

		default:
			if (t->on_rq) {
				raw_spin_unlock_irq(&t->pi_lock);
				return false;
			}

			/* OK to extract consistent diagnostic information. */
			return true;
		}
		/* NOTREACHED */
	}

Then a use might look like this:

	if (try_to_keep_sleeping(t))
		/* Extract consistent diagnostic information. */
		raw_spin_unlock_irq(&t->pi_lock);
	} else {
		/* Woo-hoo!  It started running again!!! */
	}

Is there a better way to approach this?

Thanx, Paul

#7 lore raw
Quoted 50 lines · On Thu, 5 Mar 2020 06:22:45 -0800 "Paul E. McKenney" <[email protected]> wrote:
Quoted 34 lines · Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Paul E. McKenney

Hello!

Suppose that I need to extract diagnostics information from a blocked task, but that I absolutely cannot tolerate this task awakening in the midst of this extraction process. Is the following code the right way to make this work given a task "t"?

	raw_spin_lock_irq(&t->pi_lock);
	if (t->on_rq) {
		/* Task no longer blocked, so ignore it. */
	} else {
		/* Extract consistent diagnostic information. */
	}
	raw_spin_unlock_irq(&t->pi_lock);
Quoted 5 lines
Quoted 2 lines

It looks like all the wakeup paths acquire ->pi_lock, but I figured I should actually ask...

Close, the thing pi_lock actually guards is the t->state transition *to* TASK_WAKING/TASK_RUNNING, so something like this:

Almost, we must indeed also check ->on_rq, otherwise it might change the state back itself.

Quoted 8 lines
	raw_spin_lock_irq(&t->pi_lock);
	switch (t->state) {
	case TASK_RUNNING:
	case TASK_WAKING:
		/* ignore */
		break;
Quoted 12 lines
Quoted 1 line

default:

		if (t->on_rq)
			break;
Quoted 8 lines
		/* Extract consistent diagnostic information. */
		break;
	}
	raw_spin_unlock_irq(&t->pi_lock);

ought to work. But if you're going to do this, please add a reference to that code in a comment on top of try_to_wake_up(), such that we can later find all the code that relies on this.

How about if I add something like this, located right by try_to_wake_up()?

Quoted 11 lines
	bool try_to_keep_sleeping(struct task_struct *t)
	{
		raw_spin_lock_irq(&t->pi_lock);
		switch (t->state) {
		case TASK_RUNNING:
		case TASK_WAKING:
			raw_spin_unlock_irq(&t->pi_lock);
			return false;

		default:
			if (t->on_rq) {

Somehow I think there still needs to be a read barrier before the test to on_rq.

Quoted 10 lines
				raw_spin_unlock_irq(&t->pi_lock);
				return false;
			}

			/* OK to extract consistent diagnostic information. */
			return true;
		}
		/* NOTREACHED */
	}
Quoted 5 lines

Then a use might look like this:

	if (try_to_keep_sleeping(t))
		/* Extract consistent diagnostic information. */
		raw_spin_unlock_irq(&t->pi_lock);

Perhaps we should have a allow_awake(t) to match it?

		allow_awake(t);

Where we have:

static inline allow_awake(struct task_struct *t)

{
	raw_spin_unlock_irq(&t->pi_lock);
}

-- Steve?

Quoted 7 lines
	} else {
		/* Woo-hoo!  It started running again!!! */
	}

Is there a better way to approach this?

Thanx, Paul

#8 lore raw
Quoted 62 lines · Steven Rostedt
Quoted 59 lines · On Thu, 5 Mar 2020 06:22:45 -0800 "Paul E. McKenney" <[email protected]> wrote:
Quoted 34 lines · Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Paul E. McKenney

Hello!

Suppose that I need to extract diagnostics information from a blocked task, but that I absolutely cannot tolerate this task awakening in the midst of this extraction process. Is the following code the right way to make this work given a task "t"?

	raw_spin_lock_irq(&t->pi_lock);
	if (t->on_rq) {
		/* Task no longer blocked, so ignore it. */
	} else {
		/* Extract consistent diagnostic information. */
	}
	raw_spin_unlock_irq(&t->pi_lock);
Quoted 5 lines
Quoted 2 lines

It looks like all the wakeup paths acquire ->pi_lock, but I figured I should actually ask...

Close, the thing pi_lock actually guards is the t->state transition *to* TASK_WAKING/TASK_RUNNING, so something like this:

Almost, we must indeed also check ->on_rq, otherwise it might change the state back itself.

Quoted 8 lines
	raw_spin_lock_irq(&t->pi_lock);
	switch (t->state) {
	case TASK_RUNNING:
	case TASK_WAKING:
		/* ignore */
		break;
Quoted 12 lines
Quoted 1 line

default:

		if (t->on_rq)
			break;
Quoted 8 lines
		/* Extract consistent diagnostic information. */
		break;
	}
	raw_spin_unlock_irq(&t->pi_lock);

ought to work. But if you're going to do this, please add a reference to that code in a comment on top of try_to_wake_up(), such that we can later find all the code that relies on this.

How about if I add something like this, located right by try_to_wake_up()?

	bool try_to_keep_sleeping(struct task_struct *t)
	{
		raw_spin_lock_irq(&t->pi_lock);
		switch (t->state) {
		case TASK_RUNNING:
		case TASK_WAKING:
			raw_spin_unlock_irq(&t->pi_lock);
			return false;
Quoted 5 lines
Quoted 2 lines
		default:
			if (t->on_rq) {

Somehow I think there still needs to be a read barrier before the test to on_rq.

This is nowhere near a fastpath, so if there is uncertainty it gets the smp_rmb(). Or an smp_load_acquire() on t->state.

Quoted 18 lines
Quoted 10 lines
				raw_spin_unlock_irq(&t->pi_lock);
				return false;
			}

			/* OK to extract consistent diagnostic information. */
			return true;
		}
		/* NOTREACHED */
	}
Quoted 5 lines

Then a use might look like this:

	if (try_to_keep_sleeping(t))
		/* Extract consistent diagnostic information. */
		raw_spin_unlock_irq(&t->pi_lock);

Perhaps we should have a allow_awake(t) to match it?

Quoted 8 lines
		allow_awake(t);

Where we have:

static inline allow_awake(struct task_struct *t)

{
	raw_spin_unlock_irq(&t->pi_lock);
}

Makes sense to me!

Thanx, Paul

Quoted 10 lines

-- Steve?

Quoted 7 lines
	} else {
		/* Woo-hoo!  It started running again!!! */
	}

Is there a better way to approach this?

Thanx, Paul

#9 lore raw
Quoted 5 lines · Paul E. McKenney
Quoted 4 lines · Steven Rostedt
Quoted 1 line · On Thu, 5 Mar 2020 06:22:45 -0800 "Paul E. McKenney" <[email protected]> wrote:

On Thu, Mar 05, 2020 at 09:13:37AM +0100, Peter Zijlstra wrote:

[ . . . ]

Quoted 40 lines
Quoted 11 lines
Quoted 11 lines

How about if I add something like this, located right by try_to_wake_up()?

	bool try_to_keep_sleeping(struct task_struct *t)
	{
		raw_spin_lock_irq(&t->pi_lock);
		switch (t->state) {
		case TASK_RUNNING:
		case TASK_WAKING:
			raw_spin_unlock_irq(&t->pi_lock);
			return false;
Quoted 5 lines
Quoted 2 lines
		default:
			if (t->on_rq) {

Somehow I think there still needs to be a read barrier before the test to on_rq.

This is nowhere near a fastpath, so if there is uncertainty it gets the smp_rmb(). Or an smp_load_acquire() on t->state.

Quoted 20 lines
Quoted 10 lines
				raw_spin_unlock_irq(&t->pi_lock);
				return false;
			}

			/* OK to extract consistent diagnostic information. */
			return true;
		}
		/* NOTREACHED */
	}
Quoted 5 lines

Then a use might look like this:

	if (try_to_keep_sleeping(t))
		/* Extract consistent diagnostic information. */
		raw_spin_unlock_irq(&t->pi_lock);

Perhaps we should have a allow_awake(t) to match it?

		allow_awake(t);
Quoted 8 lines
Quoted 6 lines

Where we have:

static inline allow_awake(struct task_struct *t)

{
	raw_spin_unlock_irq(&t->pi_lock);
}

Makes sense to me!

So how about like this?

Thanx, Paul

------------------------------------------------------------------------

commit e2821ae6c6a6adaabc89ccd9babf4375a78e0626

Author: Paul E. McKenney <[email protected]>
Date:   Thu Mar 5 16:53:58 2020 -0800

sched/core: Add functions to prevent sleepers from awakening

In some cases, it is necessary to examine a consistent version of a sleeping process's state, in other words, it is necessary to keep that process in sleeping state. This commit therefore provides a try_to_keep_sleeping() function that acquires ->pi_lock to prevent wakeups from proceeding, returning true if the function is still asleep, and otherwise releasing ->pi_lock and returning false.

This commit also provides an allow_awake() function (as suggested by by Steven Rostedt) that reverses the effect of a successful call to try_to_keep_sleeping(), allowing the process to once again be awakened.

[ paulmck: Apply feedback from Peter Zijlstra and Steven Rostedt. ]
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Ben Segall <[email protected]>
Cc Mel Gorman <[email protected]>
include/linux/wait.h +3 -0
index 3283c8d..aefea4a 100644
@@ -1148,4 +1148,7 @@ int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, i
 		(wait)->flags = 0;						\
 	} while (0)
 
+bool try_to_keep_sleeping(struct task_struct *p);
+void allow_awake(struct task_struct *p);
+
 #endif /* _LINUX_WAIT_H */
kernel/sched/core.c +42 -0

48 lines not on this page · raw message

#10 lore raw
Quoted 45 lines · On Thu, 5 Mar 2020 17:40:27 -0800 "Paul E. McKenney" <[email protected]> wrote:

commit e2821ae6c6a6adaabc89ccd9babf4375a78e0626

Author: Paul E. McKenney <[email protected]>
Date:   Thu Mar 5 16:53:58 2020 -0800

sched/core: Add functions to prevent sleepers from awakening

In some cases, it is necessary to examine a consistent version of a sleeping process's state, in other words, it is necessary to keep that process in sleeping state. This commit therefore provides a try_to_keep_sleeping() function that acquires ->pi_lock to prevent wakeups from proceeding, returning true if the function is still asleep, and otherwise releasing ->pi_lock and returning false.

This commit also provides an allow_awake() function (as suggested by by Steven Rostedt) that reverses the effect of a successful call to try_to_keep_sleeping(), allowing the process to once again be awakened.

[ paulmck: Apply feedback from Peter Zijlstra and Steven Rostedt. ]
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Ben Segall <[email protected]>
Cc Mel Gorman <[email protected]>
include/linux/wait.h +3 -0
index 3283c8d..aefea4a 100644
@@ -1148,4 +1148,7 @@ int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, i
 		(wait)->flags = 0;						\
 	} while (0)
 
+bool try_to_keep_sleeping(struct task_struct *p);
+void allow_awake(struct task_struct *p);
+
 #endif /* _LINUX_WAIT_H */
kernel/sched/core.c +0 -0
index fc1dfc0..b665ff7 100644

kernel/sched/core.c try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) · line 2662

Quoted 9 lines
kernel/sched/core.c +6 -0
@@ -2654,6 +2654,48 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
 }
 
 /**
+ * try_to_keep_sleeping - Attempt to force task to remain off runqueues
+ * @p: The process to remain asleep.
+ *
+ * Acquires the process's ->pi_lock and checks state.  If the process
+ * is still blocked, returns @true and leave ->pi_lock held, otherwise
+ * releases ->pi_locked and returns @false.

I would add a comment here that this is paired with allow_awake(). As well as a "Returns" statement.

* Returns:
* false if the task is awake, then no lock is taken.
* true if the task is sleeping, and then task's pi_lock will be held.
* allow_awake() must be used to release the pi_lock and let
* task @p awake again.

kernel/sched/core.c try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) · line 2675

Quoted 13 lines
kernel/sched/core.c +13 -0
+ */
+bool try_to_keep_sleeping(struct task_struct *p)
+{
+	lockdep_assert_irqs_enabled();
+	raw_spin_lock_irq(&p->pi_lock);
+	switch (p->state) {
+	case TASK_RUNNING:
+	case TASK_WAKING:
+		raw_spin_unlock_irq(&p->pi_lock);
+		return false;
+
+	default:
+		smp_rmb(); /* See comments in try_to_wake_up(). */

I remember Peter asking to add a comment in try_to_wake_up() stating that this is used, so that if that code is changed, this code may also need to be updated.

-- Steve

Quoted 26 lines
kernel/sched/core.c +23 -0
+		if (p->on_rq) {
+			raw_spin_unlock_irq(&p->pi_lock);
+			return false;
+		}
+		return true;  /* Process is now stuck in blocked state. */
+	}
+	/* NOTREACHED */
+}
+
+/**
+ * allow_awake - Allow a kept-sleeping process to awaken
+ * @p: Process to be allowed to awaken.
+ *
+ * Given that @p was passed to an earlier call to try_to_keep_sleeping
+ * that returned @true, hence preventing @p from waking up, allow @p
+ * to once again be awakened.
+ */
+void allow_awake(struct task_struct *p)
+{
+	raw_spin_unlock_irq(&p->pi_lock);
+}
+
+/**
  * wake_up_process - Wake up a specific process
  * @p: The process to be woken up.
  *
#11 lore raw
Quoted 61 lines · Steven Rostedt
Quoted 45 lines · On Thu, 5 Mar 2020 17:40:27 -0800 "Paul E. McKenney" <[email protected]> wrote:

commit e2821ae6c6a6adaabc89ccd9babf4375a78e0626

Author: Paul E. McKenney <[email protected]>
Date:   Thu Mar 5 16:53:58 2020 -0800

sched/core: Add functions to prevent sleepers from awakening

In some cases, it is necessary to examine a consistent version of a sleeping process's state, in other words, it is necessary to keep that process in sleeping state. This commit therefore provides a try_to_keep_sleeping() function that acquires ->pi_lock to prevent wakeups from proceeding, returning true if the function is still asleep, and otherwise releasing ->pi_lock and returning false.

This commit also provides an allow_awake() function (as suggested by by Steven Rostedt) that reverses the effect of a successful call to try_to_keep_sleeping(), allowing the process to once again be awakened.

[ paulmck: Apply feedback from Peter Zijlstra and Steven Rostedt. ]
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Ben Segall <[email protected]>
Cc Mel Gorman <[email protected]>
include/linux/wait.h +3 -0
index 3283c8d..aefea4a 100644
@@ -1148,4 +1148,7 @@ int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, i
 		(wait)->flags = 0;						\
 	} while (0)
 
+bool try_to_keep_sleeping(struct task_struct *p);
+void allow_awake(struct task_struct *p);
+
 #endif /* _LINUX_WAIT_H */
kernel/sched/core.c +0 -0
index fc1dfc0..b665ff7 100644

kernel/sched/core.c try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) · line 2662

Quoted 9 lines
kernel/sched/core.c +6 -0
@@ -2654,6 +2654,48 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
 }
 
 /**
+ * try_to_keep_sleeping - Attempt to force task to remain off runqueues
+ * @p: The process to remain asleep.
+ *
+ * Acquires the process's ->pi_lock and checks state.  If the process
+ * is still blocked, returns @true and leave ->pi_lock held, otherwise
+ * releases ->pi_locked and returns @false.

I would add a comment here that this is paired with allow_awake(). As well as a "Returns" statement.

Quoted 5 lines

* Returns:
* false if the task is awake, then no lock is taken.
* true if the task is sleeping, and then task's pi_lock will be held.
* allow_awake() must be used to release the pi_lock and let
* task @p awake again.

Good point, added.

Quoted 9 lines
Quoted 9 lines
kernel/sched/core.c +9 -0
+ */
+bool try_to_keep_sleeping(struct task_struct *p)
+{
+	lockdep_assert_irqs_enabled();
+	raw_spin_lock_irq(&p->pi_lock);
+	switch (p->state) {
+	case TASK_RUNNING:
+	case TASK_WAKING:
+		raw_spin_unlock_irq(&p->pi_lock);
Quoted 8 lines

kernel/sched/core.c try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) · line 2675

Quoted 4 lines
kernel/sched/core.c +4 -0
+		return false;
+
+	default:
+		smp_rmb(); /* See comments in try_to_wake_up(). */

I remember Peter asking to add a comment in try_to_wake_up() stating that this is used, so that if that code is changed, this code may also need to be updated.

Adding a reference to try_to_keep_sleeping() in try_to_wake_up()'s smp_rmb() comment, as shown below?

Thanx, Paul

------------------------------------------------------------------------

commit 57008d8adaa1e93acedea23e4858b9831e0dd075

Author: Paul E. McKenney <[email protected]>
Date:   Thu Mar 5 16:53:58 2020 -0800

sched/core: Add functions to prevent sleepers from awakening

In some cases, it is necessary to examine a consistent version of a sleeping process's state, in other words, it is necessary to keep that process in sleeping state. This commit therefore provides a try_to_keep_sleeping() function that acquires ->pi_lock to prevent wakeups from proceeding, returning true if the function is still asleep, and otherwise releasing ->pi_lock and returning false.

This commit also provides an allow_awake() function (as suggested by by Steven Rostedt) that reverses the effect of a successful call to try_to_keep_sleeping(), allowing the process to once again be awakened.

[ paulmck: Apply feedback from Peter Zijlstra and Steven Rostedt. ]
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Dietmar Eggemann <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Ben Segall <[email protected]>
Cc Mel Gorman <[email protected]>
include/linux/wait.h +3 -0
index 3283c8d..aefea4a 100644
@@ -1148,4 +1148,7 @@ int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, i
 		(wait)->flags = 0;						\
 	} while (0)
 
+bool try_to_keep_sleeping(struct task_struct *p);
+void allow_awake(struct task_struct *p);
+
 #endif /* _LINUX_WAIT_H */
kernel/sched/core.c +50 -0

62 lines not on this page · raw message