diff --git a/include/linux/sched.h b/include/linux/sched.h
index 3df4ab414f1ad..94ead66808a50 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2058,21 +2058,22 @@ static inline bool task_state_match_and(struct task_struct *tsk, long state)
 	return match;
 }
 
-static inline bool __task_state_match_eq(struct task_struct *tsk, long state)
+static inline int __task_state_match_eq(struct task_struct *tsk, long state)
 {
-	bool match = false;
+	int match = 0;
 
 	if (READ_ONCE(tsk->__state) == state)
-		match = true;
+		match = 1;
 	else if (tsk->saved_state == state)
-		match = true;
+		match = -1;
+
 	return match;
 }
 
-static inline bool task_state_match_eq(struct task_struct *tsk, long state)
+static inline int task_state_match_eq(struct task_struct *tsk, long state)
 {
 	unsigned long flags;
-	bool match;
+	int match;
 
 	raw_spin_lock_irqsave(&tsk->pi_lock, flags);
 	match = __task_state_match_eq(tsk, state);
@@ -2091,7 +2092,7 @@ static inline bool task_state_match_and_set(struct task_struct *tsk, long state,
 		WRITE_ONCE(tsk->__state, new_state);
 		match = true;
 	} else if (tsk->saved_state & state) {
-		tsk->__state = new_state;
+		tsk->saved_state = new_state;
 		match = true;
 	}
 	raw_spin_unlock_irqrestore(&tsk->pi_lock, flags);
@@ -2123,12 +2124,12 @@ static inline bool task_state_match_and(struct task_struct *tsk, long state)
 	return READ_ONCE(tsk->__state) & state;
 }
 
-static inline bool __task_state_match_eq(struct task_struct *tsk, long state)
+static inline int __task_state_match_eq(struct task_struct *tsk, long state)
 {
 	return READ_ONCE(tsk->__state) == state;
 }
 
-static inline bool task_state_match_eq(struct task_struct *tsk, long state)
+static inline int task_state_match_eq(struct task_struct *tsk, long state)
 {
 	return __task_state_match_eq(tsk, state);
 }
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 33ce5cd113d82..7cda920dc6939 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3261,6 +3261,8 @@ unsigned long wait_task_inactive(struct task_struct *p, unsigned int match_state
 	struct rq *rq;
 
 	for (;;) {
+		int match_type = 0;
+
 		/*
 		 * We do the initial early heuristics without holding
 		 * any task-queue locks at all. We'll only try to get
@@ -3297,7 +3299,9 @@ unsigned long wait_task_inactive(struct task_struct *p, unsigned int match_state
 		running = task_running(rq, p);
 		queued = task_on_rq_queued(p);
 		ncsw = 0;
-		if (!match_state || __task_state_match_eq(p, match_state))
+		if (match_state)
+			match_type = __task_state_match_eq(p, match_state);
+		if (!match_state || match_type)
 			ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
 		task_rq_unlock(rq, p, &rf);
 
@@ -3327,7 +3331,7 @@ unsigned long wait_task_inactive(struct task_struct *p, unsigned int match_state
 		 * running right now), it's preempted, and we should
 		 * yield - it could be a while.
 		 */
-		if (unlikely(queued)) {
+		if (unlikely(queued || match_type < 0)) {
 			ktime_t to = NSEC_PER_SEC / HZ;
 
 			set_current_state(TASK_UNINTERRUPTIBLE);
diff --git a/localversion-rt b/localversion-rt
index 1199ebade17b4..1e584b47c987e 100644
--- a/localversion-rt
+++ b/localversion-rt
@@ -1 +1 @@
--rt16
+-rt17
