Skip to contentSkip to conversation

workqueue: watchdog: update wq_watchdog_touched for unbound lockup checking

PATCH In review Wang Qing 3 participants 9 messages Mar 19, 2021 – Mar 24, 2021 Workqueue

lore
Share & tools
lore

Wang Qing

author
#0 lore raw

PATCH v2 1/1 workqueue: watchdog: update wq_watchdog_touched for unbound lockup checking

When touch_softlockup_watchdog() is called, only wq_watchdog_touched_cpu updated, while the unbound worker_pool running on its core uses wq_watchdog_touched to determine whether locked up. This may be mischecked.

My suggestion is to update both when touch_softlockup_watchdog() is called, use wq_watchdog_touched_cpu to check bound, and use wq_watchdog_touched to check unbound worker_pool.

---
 kernel/watchdog.c  |  5 +++--
 kernel/workqueue.c | 17 ++++++-----------
 2 files changed, 9 insertions(+), 13 deletions(-)
kernel/watchdog.c +3 -2
index 7110906..107bc38
@@ -278,9 +278,10 @@ void touch_all_softlockup_watchdogs(void)
 	 * update as well, the only side effect might be a cycle delay for
 	 * the softlockup check.
 	 */
-	for_each_cpu(cpu, &watchdog_allowed_mask)
+	for_each_cpu(cpu, &watchdog_allowed_mask) {
 		per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
-	wq_watchdog_touch(-1);
+		wq_watchdog_touch(cpu);
+	}
 }
 
 void touch_softlockup_watchdog_sync(void)
kernel/workqueue.c +6 -11
index 0d150da..be08295
@@ -5787,22 +5787,17 @@ static void wq_watchdog_timer_fn(struct timer_list *unused)
 			continue;
 
 		/* get the latest of pool and touched timestamps */
+		if (pool->cpu >= 0)
+			touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu));
+		else
+			touched = READ_ONCE(wq_watchdog_touched);
 		pool_ts = READ_ONCE(pool->watchdog_ts);
-		touched = READ_ONCE(wq_watchdog_touched);
 
 		if (time_after(pool_ts, touched))
 			ts = pool_ts;
 		else
 			ts = touched;
 
-		if (pool->cpu >= 0) {
-			unsigned long cpu_touched =
-				READ_ONCE(per_cpu(wq_watchdog_touched_cpu,
-						  pool->cpu));
-			if (time_after(cpu_touched, ts))
-				ts = cpu_touched;
-		}
-
 		/* did we stall? */
 		if (time_after(jiffies, ts + thresh)) {
 			lockup_detected = true;
@@ -5826,8 +5821,8 @@ notrace void wq_watchdog_touch(int cpu)
 {
 	if (cpu >= 0)
 		per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies;
-	else
-		wq_watchdog_touched = jiffies;
+
+	wq_watchdog_touched = jiffies;
 }
 
 static void wq_watchdog_set_thresh(unsigned long thresh)
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 3 lines · Wang Qing

When touch_softlockup_watchdog() is called, only wq_watchdog_touched_cpu updated, while the unbound worker_pool running on its core uses wq_watchdog_touched to determine whether locked up. This may be mischecked.

Can you please elaborate here, preferably with a concrete scenario where the new code is better?

Thanks.

Signature · 1 line
tejun
author
#2 lore raw
Quoted 8 lines · Wang Qing
Quoted 3 lines

When touch_softlockup_watchdog() is called, only wq_watchdog_touched_cpu updated, while the unbound worker_pool running on its core uses wq_watchdog_touched to determine whether locked up. This may be mischecked.

Can you please elaborate here, preferably with a concrete scenario where the new code is better?

The previous code is problematic for judging whether the unbound pool is locked up:
When the expected single cpu stall occurs, only wq_watchdog_touched_cpu is updated, However, the unbound pool uses wq_watchdog_touched to determine whether it is locked up, so when the unbound pool is running in a scenario where the cpu stall is expected, a misjudgment will occur, because wq_watchdog_touched only be update when all the cpu stall as expect.

So we need to update wq_watchdog_touched in touch_softlockup_watchdog(), and it is only used for unbound pool, it will not affect the bound pool in any way.

Thanks,

Qing

Quoted 5 lines

Thanks.

Signature · 1 line
tejun
#3 lore raw
In reply to Wang Qing
Quoted 3 lines · Wang Qing

When touch_softlockup_watchdog() is called, only wq_watchdog_touched_cpu updated, while the unbound worker_pool running on its core uses wq_watchdog_touched to determine whether locked up. This may be mischecked.

By other words, unbound workqueues are not aware of the more common touch_softlockup_watchdog() because it updates only wq_watchdog_touched_cpu for the affected CPU. As a result, the workqueue watchdog might report lockup in unbound workqueue even though it is blocked by a known slow code.

Quoted 15 lines

My suggestion is to update both when touch_softlockup_watchdog() is called, use wq_watchdog_touched_cpu to check bound, and use wq_watchdog_touched to check unbound worker_pool.

---
 kernel/watchdog.c  |  5 +++--
 kernel/workqueue.c | 17 ++++++-----------
 2 files changed, 9 insertions(+), 13 deletions(-)
kernel/watchdog.c +0 -0
index 7110906..107bc38

kernel/watchdog.c void touch_all_softlockup_watchdogs(void) · line 283

Quoted 8 lines
kernel/watchdog.c +2 -2
@@ -278,9 +278,10 @@ void touch_all_softlockup_watchdogs(void)
 	 * update as well, the only side effect might be a cycle delay for
 	 * the softlockup check.
 	 */
-	for_each_cpu(cpu, &watchdog_allowed_mask)
+	for_each_cpu(cpu, &watchdog_allowed_mask) {
 		per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
-	wq_watchdog_touch(-1);
+		wq_watchdog_touch(cpu);

Note that wq_watchdog_touch(cpu) newly always updates wq_watchdog_touched. This cycle will set the same jiffies value cpu-times to the same variable.

Quoted 36 lines
kernel/watchdog.c +1 -0
+	}
 }
 
 void touch_softlockup_watchdog_sync(void)
kernel/workqueue.c +4 -9
index 0d150da..be08295
@@ -5787,22 +5787,17 @@ static void wq_watchdog_timer_fn(struct timer_list *unused)
 			continue;
 
 		/* get the latest of pool and touched timestamps */
+		if (pool->cpu >= 0)
+			touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu));
+		else
+			touched = READ_ONCE(wq_watchdog_touched);
 		pool_ts = READ_ONCE(pool->watchdog_ts);
-		touched = READ_ONCE(wq_watchdog_touched);
 
 		if (time_after(pool_ts, touched))
 			ts = pool_ts;
 		else
 			ts = touched;
 
-		if (pool->cpu >= 0) {
-			unsigned long cpu_touched =
-				READ_ONCE(per_cpu(wq_watchdog_touched_cpu,
-						  pool->cpu));
-			if (time_after(cpu_touched, ts))
-				ts = cpu_touched;
-		}
-
 		/* did we stall? */
 		if (time_after(jiffies, ts + thresh)) {
 			lockup_detected = true;

kernel/workqueue.c notrace void wq_watchdog_touch(int cpu) · line 5828

Quoted 10 lines
kernel/workqueue.c +2 -2
@@ -5826,8 +5821,8 @@ notrace void wq_watchdog_touch(int cpu)
 {
 	if (cpu >= 0)
 		per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies;
-	else
-		wq_watchdog_touched = jiffies;
+
+	wq_watchdog_touched = jiffies;
 }
 
 static void wq_watchdog_set_thresh(unsigned long thresh)

This last hunk is enough to fix the problem. wq_watchdog_touched will get updated also from cpu-specific touch_softlockup_watchdog().

The original patch simplified the logic of wq_watchdog_timer_fn(). But it added un-necessary assignments into touch_all_softlockup_watchdogs(void).

I do not have strong opinion what solution is better. I slightly prefer to keep only this last hunk.

Best Regards,
Petr

author
#4 lore raw
Quoted 11 lines · Wang Qing
Quoted 3 lines

When touch_softlockup_watchdog() is called, only wq_watchdog_touched_cpu updated, while the unbound worker_pool running on its core uses wq_watchdog_touched to determine whether locked up. This may be mischecked.

By other words, unbound workqueues are not aware of the more common touch_softlockup_watchdog() because it updates only wq_watchdog_touched_cpu for the affected CPU. As a result, the workqueue watchdog might report lockup in unbound workqueue even though it is blocked by a known slow code.

Yes, this is the problem I'm talking about.

Quoted 20 lines
Quoted 20 lines

My suggestion is to update both when touch_softlockup_watchdog() is called, use wq_watchdog_touched_cpu to check bound, and use wq_watchdog_touched to check unbound worker_pool.

---
 kernel/watchdog.c  |  5 +++--
 kernel/workqueue.c | 17 ++++++-----------
 2 files changed, 9 insertions(+), 13 deletions(-)
kernel/watchdog.c +0 -1
index 7110906..107bc38
@@ -278,9 +278,10 @@ void touch_all_softlockup_watchdogs(void)
 	 * update as well, the only side effect might be a cycle delay for
 	 * the softlockup check.
 	 */
-	for_each_cpu(cpu, &watchdog_allowed_mask)
Quoted 9 lines

kernel/watchdog.c void touch_all_softlockup_watchdogs(void) · line 283

Quoted 4 lines
kernel/watchdog.c +2 -1
+	for_each_cpu(cpu, &watchdog_allowed_mask) {
 		per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
-	wq_watchdog_touch(-1);
+		wq_watchdog_touch(cpu);

Note that wq_watchdog_touch(cpu) newly always updates wq_watchdog_touched. This cycle will set the same jiffies value cpu-times to the same variable.

Although there is a bit of redundancy here, but the most concise way of implementation, and it is certain that it will not affect performance.

Quoted 45 lines
Quoted 45 lines
kernel/watchdog.c +1 -0
+	}
 }
 
 void touch_softlockup_watchdog_sync(void)
kernel/workqueue.c +6 -11
index 0d150da..be08295
@@ -5787,22 +5787,17 @@ static void wq_watchdog_timer_fn(struct timer_list *unused)
 			continue;
 
 		/* get the latest of pool and touched timestamps */
+		if (pool->cpu >= 0)
+			touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu));
+		else
+			touched = READ_ONCE(wq_watchdog_touched);
 		pool_ts = READ_ONCE(pool->watchdog_ts);
-		touched = READ_ONCE(wq_watchdog_touched);
 
 		if (time_after(pool_ts, touched))
 			ts = pool_ts;
 		else
 			ts = touched;
 
-		if (pool->cpu >= 0) {
-			unsigned long cpu_touched =
-				READ_ONCE(per_cpu(wq_watchdog_touched_cpu,
-						  pool->cpu));
-			if (time_after(cpu_touched, ts))
-				ts = cpu_touched;
-		}
-
 		/* did we stall? */
 		if (time_after(jiffies, ts + thresh)) {
 			lockup_detected = true;
@@ -5826,8 +5821,8 @@ notrace void wq_watchdog_touch(int cpu)
 {
 	if (cpu >= 0)
 		per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies;
-	else
-		wq_watchdog_touched = jiffies;
+
+	wq_watchdog_touched = jiffies;
 }
 
Quoted 4 lines

kernel/workqueue.c notrace void wq_watchdog_touch(int cpu) · line 5828

Quoted 1 line
kernel/workqueue.c +0 -0
 static void wq_watchdog_set_thresh(unsigned long thresh)

This last hunk is enough to fix the problem. wq_watchdog_touched will get updated also from cpu-specific touch_softlockup_watchdog().

Not enough in fact, because wq_watchdog_touched was updated in wq_watchdog_touch(), so wq_watchdog_touched can no longer be used to judge the bound pool, we must update every wq_watchdog_touched_cpu in touch_all_softlockup_watchdogs() for bound judgment.

Thanks,
Qing Wang

Quoted 10 lines

The original patch simplified the logic of wq_watchdog_timer_fn(). But it added un-necessary assignments into touch_all_softlockup_watchdogs(void).

I do not have strong opinion what solution is better. I slightly prefer to keep only this last hunk.

Best Regards,
Petr

#5 lore raw
Quoted 6 lines · 王擎
Quoted 6 lines · Wang Qing
Quoted 3 lines

When touch_softlockup_watchdog() is called, only wq_watchdog_touched_cpu updated, while the unbound worker_pool running on its core uses wq_watchdog_touched to determine whether locked up. This may be mischecked.

Quoted 7 lines
Quoted 5 lines

By other words, unbound workqueues are not aware of the more common touch_softlockup_watchdog() because it updates only wq_watchdog_touched_cpu for the affected CPU. As a result, the workqueue watchdog might report lockup in unbound workqueue even though it is blocked by a known slow code.

Yes, this is the problem I'm talking about.

I thought more about it. This patch prevents a false positive. Could it bring an opposite problem and hide real problems?

I mean that an unbound workqueue might get blocked on CPU A because of a real softlockup. But we might not notice it because CPU B is touched. Well, there are other ways how to detect this situation, e.g. the softlockup watchdog.

Quoted 49 lines
Quoted 19 lines
Quoted 19 lines

My suggestion is to update both when touch_softlockup_watchdog() is called, use wq_watchdog_touched_cpu to check bound, and use wq_watchdog_touched to check unbound worker_pool.

---
 kernel/watchdog.c  |  5 +++--
 kernel/workqueue.c | 17 ++++++-----------
 2 files changed, 9 insertions(+), 13 deletions(-)
kernel/watchdog.c +0 -1
index 7110906..107bc38
@@ -278,9 +278,10 @@ void touch_all_softlockup_watchdogs(void)
 	 * update as well, the only side effect might be a cycle delay for
 	 * the softlockup check.
 	 */
-	for_each_cpu(cpu, &watchdog_allowed_mask)
Quoted 9 lines

kernel/watchdog.c void touch_all_softlockup_watchdogs(void) · line 283

Quoted 4 lines
kernel/watchdog.c +2 -1
+	for_each_cpu(cpu, &watchdog_allowed_mask) {
 		per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
-	wq_watchdog_touch(-1);
+		wq_watchdog_touch(cpu);

Note that wq_watchdog_touch(cpu) newly always updates wq_watchdog_touched. This cycle will set the same jiffies value cpu-times to the same variable.

Although there is a bit of redundancy here, but the most concise way of implementation, and it is certain that it will not affect performance.

Quoted 18 lines
Quoted 18 lines
kernel/watchdog.c +1 -0
+	}
 }
 
 void touch_softlockup_watchdog_sync(void)
kernel/workqueue.c +2 -2
index 0d150da..be08295
@@ -5826,8 +5821,8 @@ notrace void wq_watchdog_touch(int cpu)
 {
 	if (cpu >= 0)
 		per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies;
-	else
-		wq_watchdog_touched = jiffies;
+
+	wq_watchdog_touched = jiffies;
 }
 
Quoted 8 lines
Quoted 4 lines

kernel/workqueue.c notrace void wq_watchdog_touch(int cpu) · line 5828

Quoted 1 line
kernel/workqueue.c +0 -0
 static void wq_watchdog_set_thresh(unsigned long thresh)

This last hunk is enough to fix the problem. wq_watchdog_touched will get updated also from cpu-specific touch_softlockup_watchdog().

Not enough in fact, because wq_watchdog_touched was updated in wq_watchdog_touch(), so wq_watchdog_touched can no longer be used to judge the bound pool, we must update every wq_watchdog_touched_cpu in touch_all_softlockup_watchdogs() for bound judgment.

I see. Your patch makes sense.

I would just improve the commit message. It should better explain why all the twists are needed. I would write something like:

<proposal>
Subject: workqueue/watchdog: Make unbound workqueues aware of touch_softlockup_watchdog()

There are two workqueue-specific watchdog timestamps:

+ @wq_watchdog_touched_cpu (per-CPU) updated by touch_softlockup_watchdog()

+ @wq_watchdog_touched (global) updated by
touch_all_softlockup_watchdogs()

watchdog_timer_fn() checks only the global @wq_watchdog_touched for unbound workqueues. As a result, unbound workqueues are not aware of touch_softlockup_watchdog(). The watchdog might report a stall even when the unbound workqueues are blocked by a known slow code.

Solution:

touch_softlockup_watchdog() must touch also the global @wq_watchdog_touched timestamp.

The global timestamp can not longer be used for bound workqueues because it is updated on all CPUs. Instead, bound workqueues have to check only @wq_watchdog_touched_cpu and these timestamp has to be updated for all CPUs in touch_all_softlockup_watchdogs().

Beware:

The change might cause the opposite problem. An unbound workqueue might get blocked on CPU A because of a real softlockup. The workqueue watchdog would miss it when the timestamp got touched on CPU B.

It is acceptable because softlockups are detected by softlockup watchdog. The workqueue watchdog is there to detect stalls where a work never finishes, for example, because of dependencies of works queued into the same workqueue.
</proposal>

How does that sound?

Best Regards,
Petr

author
#6 lore raw
Quoted 16 lines · 王擎
Quoted 6 lines
Quoted 6 lines · Wang Qing
Quoted 3 lines

When touch_softlockup_watchdog() is called, only wq_watchdog_touched_cpu updated, while the unbound worker_pool running on its core uses wq_watchdog_touched to determine whether locked up. This may be mischecked.

Quoted 7 lines
Quoted 5 lines

By other words, unbound workqueues are not aware of the more common touch_softlockup_watchdog() because it updates only wq_watchdog_touched_cpu for the affected CPU. As a result, the workqueue watchdog might report lockup in unbound workqueue even though it is blocked by a known slow code.

Yes, this is the problem I'm talking about.

Quoted 40 lines

I thought more about it. This patch prevents a false positive. Could it bring an opposite problem and hide real problems?

I mean that an unbound workqueue might get blocked on CPU A because of a real softlockup. But we might not notice it because CPU B is touched. Well, there are other ways how to detect this situation, e.g. the softlockup watchdog.

Quoted 31 lines
Quoted 19 lines
Quoted 19 lines

My suggestion is to update both when touch_softlockup_watchdog() is called, use wq_watchdog_touched_cpu to check bound, and use wq_watchdog_touched to check unbound worker_pool.

---
 kernel/watchdog.c  |  5 +++--
 kernel/workqueue.c | 17 ++++++-----------
 2 files changed, 9 insertions(+), 13 deletions(-)
kernel/watchdog.c +0 -1
index 7110906..107bc38
@@ -278,9 +278,10 @@ void touch_all_softlockup_watchdogs(void)
 	 * update as well, the only side effect might be a cycle delay for
 	 * the softlockup check.
 	 */
-	for_each_cpu(cpu, &watchdog_allowed_mask)
Quoted 9 lines

kernel/watchdog.c void touch_all_softlockup_watchdogs(void) · line 283

Quoted 4 lines
kernel/watchdog.c +2 -1
+	for_each_cpu(cpu, &watchdog_allowed_mask) {
 		per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
-	wq_watchdog_touch(-1);
+		wq_watchdog_touch(cpu);

Note that wq_watchdog_touch(cpu) newly always updates wq_watchdog_touched. This cycle will set the same jiffies value cpu-times to the same variable.

Although there is a bit of redundancy here, but the most concise way of implementation, and it is certain that it will not affect performance.

Another way to implement is wq_watchdog_touch() remain unchanged, but need to modify touch_softlockup_watchdog() and touch_all_softlockup_watchdogs():
notrace void touch_softlockup_watchdog(void)

{
	touch_softlockup_watchdog_sched();
	wq_watchdog_touch(raw_smp_processor_id());

+ wq_watchdog_touch(-1);
}
void touch_all_softlockup_watchdogs(void)
* update as well, the only side effect might be a cycle delay for
* the softlockup check.
*/
- for_each_cpu(cpu, &watchdog_allowed_mask)
+ for_each_cpu(cpu, &watchdog_allowed_mask) {
per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
+ wq_watchdog_touch(cpu);
+ }
wq_watchdog_touch(-1);
}
So wq_watchdog_touched will not get updated many times, which do you think is better, Petr?

Quoted 63 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
kernel/watchdog.c +0 -0
 void touch_softlockup_watchdog_sync(void)
kernel/workqueue.c +2 -2
index 0d150da..be08295
@@ -5826,8 +5821,8 @@ notrace void wq_watchdog_touch(int cpu)
 {
 	if (cpu >= 0)
 		per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies;
-	else
-		wq_watchdog_touched = jiffies;
+
+	wq_watchdog_touched = jiffies;
 }
 
Quoted 8 lines
Quoted 4 lines

kernel/workqueue.c notrace void wq_watchdog_touch(int cpu) · line 5828

Quoted 1 line
kernel/workqueue.c +0 -0
 static void wq_watchdog_set_thresh(unsigned long thresh)

This last hunk is enough to fix the problem. wq_watchdog_touched will get updated also from cpu-specific touch_softlockup_watchdog().

Not enough in fact, because wq_watchdog_touched was updated in wq_watchdog_touch(), so wq_watchdog_touched can no longer be used to judge the bound pool, we must update every wq_watchdog_touched_cpu in touch_all_softlockup_watchdogs() for bound judgment.

I see. Your patch makes sense.

I would just improve the commit message. It should better explain why all the twists are needed. I would write something like:

<proposal>
Subject: workqueue/watchdog: Make unbound workqueues aware of touch_softlockup_watchdog()

There are two workqueue-specific watchdog timestamps:

+ @wq_watchdog_touched_cpu (per-CPU) updated by touch_softlockup_watchdog()

+ @wq_watchdog_touched (global) updated by
touch_all_softlockup_watchdogs()

watchdog_timer_fn() checks only the global @wq_watchdog_touched for unbound workqueues. As a result, unbound workqueues are not aware of touch_softlockup_watchdog(). The watchdog might report a stall even when the unbound workqueues are blocked by a known slow code.

Solution:

touch_softlockup_watchdog() must touch also the global @wq_watchdog_touched timestamp.

The global timestamp can not longer be used for bound workqueues because it is updated on all CPUs. Instead, bound workqueues have to check only @wq_watchdog_touched_cpu and these timestamp has to be updated for all CPUs in touch_all_softlockup_watchdogs().

Beware:

The change might cause the opposite problem. An unbound workqueue might get blocked on CPU A because of a real softlockup. The workqueue watchdog would miss it when the timestamp got touched on CPU B.

Quoted 7 lines

It is acceptable because softlockups are detected by softlockup watchdog. The workqueue watchdog is there to detect stalls where a work never finishes, for example, because of dependencies of works queued into the same workqueue.
</proposal>

How does that sound?

It's fine to me, what's your opinion, Tejun?

Thanks,
Qing

Quoted 3 lines

Best Regards,
Petr

#7 lore raw
Quoted 75 lines · 王擎
Quoted 16 lines · 王擎
Quoted 6 lines
Quoted 6 lines · Wang Qing
Quoted 3 lines

When touch_softlockup_watchdog() is called, only wq_watchdog_touched_cpu updated, while the unbound worker_pool running on its core uses wq_watchdog_touched to determine whether locked up. This may be mischecked.

Quoted 7 lines
Quoted 5 lines

By other words, unbound workqueues are not aware of the more common touch_softlockup_watchdog() because it updates only wq_watchdog_touched_cpu for the affected CPU. As a result, the workqueue watchdog might report lockup in unbound workqueue even though it is blocked by a known slow code.

Yes, this is the problem I'm talking about.

Quoted 40 lines

I thought more about it. This patch prevents a false positive. Could it bring an opposite problem and hide real problems?

I mean that an unbound workqueue might get blocked on CPU A because of a real softlockup. But we might not notice it because CPU B is touched. Well, there are other ways how to detect this situation, e.g. the softlockup watchdog.

Quoted 31 lines
Quoted 19 lines
Quoted 19 lines

My suggestion is to update both when touch_softlockup_watchdog() is called, use wq_watchdog_touched_cpu to check bound, and use wq_watchdog_touched to check unbound worker_pool.

---
 kernel/watchdog.c  |  5 +++--
 kernel/workqueue.c | 17 ++++++-----------
 2 files changed, 9 insertions(+), 13 deletions(-)
kernel/watchdog.c +0 -1
index 7110906..107bc38
@@ -278,9 +278,10 @@ void touch_all_softlockup_watchdogs(void)
 	 * update as well, the only side effect might be a cycle delay for
 	 * the softlockup check.
 	 */
-	for_each_cpu(cpu, &watchdog_allowed_mask)
Quoted 9 lines

kernel/watchdog.c void touch_all_softlockup_watchdogs(void) · line 283

Quoted 4 lines
kernel/watchdog.c +2 -1
+	for_each_cpu(cpu, &watchdog_allowed_mask) {
 		per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
-	wq_watchdog_touch(-1);
+		wq_watchdog_touch(cpu);

Note that wq_watchdog_touch(cpu) newly always updates wq_watchdog_touched. This cycle will set the same jiffies value cpu-times to the same variable.

Although there is a bit of redundancy here, but the most concise way of implementation, and it is certain that it will not affect performance.

Another way to implement is wq_watchdog_touch() remain unchanged, but need to modify touch_softlockup_watchdog() and touch_all_softlockup_watchdogs():
notrace void touch_softlockup_watchdog(void)

{
	touch_softlockup_watchdog_sched();
	wq_watchdog_touch(raw_smp_processor_id());

+ wq_watchdog_touch(-1);
}
void touch_all_softlockup_watchdogs(void)
* update as well, the only side effect might be a cycle delay for
* the softlockup check.
*/
- for_each_cpu(cpu, &watchdog_allowed_mask)

kernel/watchdog.c +1 -0
+	for_each_cpu(cpu, &watchdog_allowed_mask) {
  		per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
 +		wq_watchdog_touch(cpu);
 +	}
 	wq_watchdog_touch(-1);
  }
Quoted 2 lines

So wq_watchdog_touched will not get updated many times, which do you think is better, Petr?

I actually prefer the original patch. It makes wq_watchdog_touch() easy to use. The complexity is hidden in wq-specific code.

The alternative way updates each timestamp only once but the use is more complicated. IMHO, it is more error prone.

Best Regards,
Petr

author
#8 lore raw
Quoted 83 lines · 王擎
Quoted 75 lines
Quoted 16 lines · 王擎
Quoted 6 lines
Quoted 6 lines · Wang Qing
Quoted 3 lines

When touch_softlockup_watchdog() is called, only wq_watchdog_touched_cpu updated, while the unbound worker_pool running on its core uses wq_watchdog_touched to determine whether locked up. This may be mischecked.

Quoted 7 lines
Quoted 5 lines

By other words, unbound workqueues are not aware of the more common touch_softlockup_watchdog() because it updates only wq_watchdog_touched_cpu for the affected CPU. As a result, the workqueue watchdog might report lockup in unbound workqueue even though it is blocked by a known slow code.

Yes, this is the problem I'm talking about.

Quoted 40 lines

I thought more about it. This patch prevents a false positive. Could it bring an opposite problem and hide real problems?

I mean that an unbound workqueue might get blocked on CPU A because of a real softlockup. But we might not notice it because CPU B is touched. Well, there are other ways how to detect this situation, e.g. the softlockup watchdog.

Quoted 31 lines
Quoted 19 lines
Quoted 19 lines

My suggestion is to update both when touch_softlockup_watchdog() is called, use wq_watchdog_touched_cpu to check bound, and use wq_watchdog_touched to check unbound worker_pool.

---
 kernel/watchdog.c  |  5 +++--
 kernel/workqueue.c | 17 ++++++-----------
 2 files changed, 9 insertions(+), 13 deletions(-)
kernel/watchdog.c +0 -1
index 7110906..107bc38
@@ -278,9 +278,10 @@ void touch_all_softlockup_watchdogs(void)
 	 * update as well, the only side effect might be a cycle delay for
 	 * the softlockup check.
 	 */
-	for_each_cpu(cpu, &watchdog_allowed_mask)
Quoted 9 lines

kernel/watchdog.c void touch_all_softlockup_watchdogs(void) · line 283

Quoted 4 lines
kernel/watchdog.c +2 -1
+	for_each_cpu(cpu, &watchdog_allowed_mask) {
 		per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
-	wq_watchdog_touch(-1);
+		wq_watchdog_touch(cpu);

Note that wq_watchdog_touch(cpu) newly always updates wq_watchdog_touched. This cycle will set the same jiffies value cpu-times to the same variable.

Although there is a bit of redundancy here, but the most concise way of implementation, and it is certain that it will not affect performance.

Another way to implement is wq_watchdog_touch() remain unchanged, but need to modify touch_softlockup_watchdog() and touch_all_softlockup_watchdogs():
notrace void touch_softlockup_watchdog(void)

{
	touch_softlockup_watchdog_sched();
	wq_watchdog_touch(raw_smp_processor_id());

+ wq_watchdog_touch(-1);
}
void touch_all_softlockup_watchdogs(void)
* update as well, the only side effect might be a cycle delay for
* the softlockup check.
*/
- for_each_cpu(cpu, &watchdog_allowed_mask)

kernel/watchdog.c +1 -0
+	for_each_cpu(cpu, &watchdog_allowed_mask) {
  		per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
 +		wq_watchdog_touch(cpu);
 +	}
 	wq_watchdog_touch(-1);
  }
Quoted 2 lines

So wq_watchdog_touched will not get updated many times, which do you think is better, Petr?

I actually prefer the original patch. It makes wq_watchdog_touch() easy to use. The complexity is hidden in wq-specific code.

Quoted 2 lines

The alternative way updates each timestamp only once but the use is more complicated. IMHO, it is more error prone.

I agree, so I will just modify the commit log based on V2 and resend.

Thanks,
Qing

Quoted 3 lines

Best Regards,
Petr