From: Tejun Heo <tj@kernel•org>
To: torvalds@linux-foundation•org, mpatocka@redhat•com
Cc: linux-kernel@vger•kernel.org, dm-devel@lists•linux.dev,
msnitzer@redhat•com, ignat@cloudflare•com, damien.lemoal@wdc•com,
bob.liu@oracle•com, houtao1@huawei•com, peterz@infradead•org,
mingo@kernel•org, netdev@vger•kernel.org, allen.lkml@gmail•com,
kernel-team@meta•com, Tejun Heo <tj@kernel•org>
Subject: [PATCH 1/8] workqueue: Update lock debugging code
Date: Mon, 29 Jan 2024 23:11:48 -1000 [thread overview]
Message-ID: <20240130091300.2968534-2-tj@kernel.org> (raw)
In-Reply-To: <20240130091300.2968534-1-tj@kernel.org>
These changes are in preparation of BH workqueue which will execute work
items from BH context.
- Update lock and RCU depth checks in process_one_work() so that it
remembers and checks against the starting depths and prints out the depth
changes.
- Factor out lockdep annotations in the flush paths into
touch_{wq|work}_lockdep_map(). The work->lockdep_map touching is moved
from __flush_work() to its callee - start_flush_work(). This brings it
closer to the wq counterpart and will allow testing the associated wq's
flags which will be needed to support BH workqueues. This is not expected
to cause any functional changes.
Signed-off-by: Tejun Heo <tj@kernel•org>
---
kernel/workqueue.c | 51 ++++++++++++++++++++++++++++++----------------
1 file changed, 34 insertions(+), 17 deletions(-)
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 9221a4c57ae1..3f2081bd05a4 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -2954,6 +2954,7 @@ __acquires(&pool->lock)
struct pool_workqueue *pwq = get_work_pwq(work);
struct worker_pool *pool = worker->pool;
unsigned long work_data;
+ int lockdep_start_depth, rcu_start_depth;
#ifdef CONFIG_LOCKDEP
/*
* It is permissible to free the struct work_struct from
@@ -3016,6 +3017,8 @@ __acquires(&pool->lock)
pwq->stats[PWQ_STAT_STARTED]++;
raw_spin_unlock_irq(&pool->lock);
+ rcu_start_depth = rcu_preempt_depth();
+ lockdep_start_depth = lockdep_depth(current);
lock_map_acquire(&pwq->wq->lockdep_map);
lock_map_acquire(&lockdep_map);
/*
@@ -3051,12 +3054,15 @@ __acquires(&pool->lock)
lock_map_release(&lockdep_map);
lock_map_release(&pwq->wq->lockdep_map);
- if (unlikely(in_atomic() || lockdep_depth(current) > 0 ||
- rcu_preempt_depth() > 0)) {
- pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d/%d\n"
- " last function: %ps\n",
- current->comm, preempt_count(), rcu_preempt_depth(),
- task_pid_nr(current), worker->current_func);
+ if (unlikely((worker->task && in_atomic()) ||
+ lockdep_depth(current) != lockdep_start_depth ||
+ rcu_preempt_depth() != rcu_start_depth)) {
+ pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n"
+ " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n",
+ current->comm, task_pid_nr(current), preempt_count(),
+ lockdep_start_depth, lockdep_depth(current),
+ rcu_start_depth, rcu_preempt_depth(),
+ worker->current_func);
debug_show_held_locks(current);
dump_stack();
}
@@ -3538,6 +3544,19 @@ static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
return wait;
}
+static void touch_wq_lockdep_map(struct workqueue_struct *wq)
+{
+ lock_map_acquire(&wq->lockdep_map);
+ lock_map_release(&wq->lockdep_map);
+}
+
+static void touch_work_lockdep_map(struct work_struct *work,
+ struct workqueue_struct *wq)
+{
+ lock_map_acquire(&work->lockdep_map);
+ lock_map_release(&work->lockdep_map);
+}
+
/**
* __flush_workqueue - ensure that any scheduled work has run to completion.
* @wq: workqueue to flush
@@ -3557,8 +3576,7 @@ void __flush_workqueue(struct workqueue_struct *wq)
if (WARN_ON(!wq_online))
return;
- lock_map_acquire(&wq->lockdep_map);
- lock_map_release(&wq->lockdep_map);
+ touch_wq_lockdep_map(wq);
mutex_lock(&wq->mutex);
@@ -3757,6 +3775,7 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
struct worker *worker = NULL;
struct worker_pool *pool;
struct pool_workqueue *pwq;
+ struct workqueue_struct *wq;
might_sleep();
@@ -3780,11 +3799,14 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
pwq = worker->current_pwq;
}
- check_flush_dependency(pwq->wq, work);
+ wq = pwq->wq;
+ check_flush_dependency(wq, work);
insert_wq_barrier(pwq, barr, work, worker);
raw_spin_unlock_irq(&pool->lock);
+ touch_work_lockdep_map(work, wq);
+
/*
* Force a lock recursion deadlock when using flush_work() inside a
* single-threaded or rescuer equipped workqueue.
@@ -3794,11 +3816,9 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
* workqueues the deadlock happens when the rescuer stalls, blocking
* forward progress.
*/
- if (!from_cancel &&
- (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) {
- lock_map_acquire(&pwq->wq->lockdep_map);
- lock_map_release(&pwq->wq->lockdep_map);
- }
+ if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer))
+ touch_wq_lockdep_map(wq);
+
rcu_read_unlock();
return true;
already_gone:
@@ -3817,9 +3837,6 @@ static bool __flush_work(struct work_struct *work, bool from_cancel)
if (WARN_ON(!work->func))
return false;
- lock_map_acquire(&work->lockdep_map);
- lock_map_release(&work->lockdep_map);
-
if (start_flush_work(work, &barr, from_cancel)) {
wait_for_completion(&barr.done);
destroy_work_on_stack(&barr.work);
--
2.43.0
next prev parent reply other threads:[~2024-01-30 9:13 UTC|newest]
Thread overview: 59+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-30 9:11 [PATCHSET wq/for-6.9] workqueue: Implement BH workqueue and convert several tasklet users Tejun Heo
2024-01-30 9:11 ` Tejun Heo [this message]
2024-01-30 9:11 ` [PATCH 2/8] workqueue: Factor out init_cpu_worker_pool() Tejun Heo
2024-01-30 9:11 ` [PATCH 3/8] workqueue: Implement BH workqueues to eventually replace tasklets Tejun Heo
2024-01-30 17:25 ` Linus Torvalds
2024-02-01 11:02 ` Lai Jiangshan
2024-02-01 21:47 ` Tejun Heo
2024-02-02 1:15 ` [PATCH v2 " Tejun Heo
2024-02-04 2:20 ` Lai Jiangshan
2024-02-04 21:29 ` [PATCH v3 " Tejun Heo
2024-02-05 4:48 ` Hillf Danton
2024-02-05 17:47 ` Tejun Heo
2024-02-26 2:00 ` Boqun Feng
2024-02-26 18:47 ` Tejun Heo
2024-02-27 1:38 ` [PATCH for-6.9] workqueue: Drain BH work items on hot-unplugged CPUs Tejun Heo
2024-02-29 20:37 ` Tejun Heo
2024-02-29 21:07 ` Boqun Feng
2024-01-30 9:11 ` [PATCH 4/8] backtracetest: Convert from tasklet to BH workqueue Tejun Heo
2024-01-30 9:11 ` [PATCH 5/8] usb: core: hcd: " Tejun Heo
2024-01-30 16:38 ` Greg Kroah-Hartman
2024-02-20 17:25 ` Davidlohr Bueso
2024-02-20 17:55 ` Linus Torvalds
2024-02-20 18:19 ` Tejun Heo
2024-02-20 19:36 ` Davidlohr Bueso
2024-01-30 9:11 ` [PATCH 6/8] net: tcp: tsq: " Tejun Heo
2024-02-16 5:31 ` Tejun Heo
2024-02-16 8:23 ` Eric Dumazet
2024-02-16 15:52 ` David Wei
2024-02-16 16:24 ` Tejun Heo
2025-05-25 3:51 ` Jason Xing
2025-05-27 18:55 ` Tejun Heo
2025-05-27 23:43 ` Jason Xing
2024-01-30 9:11 ` [PATCH 7/8] dm-crypt: " Tejun Heo
2024-01-30 10:46 ` Sebastian Andrzej Siewior
2024-01-30 15:53 ` Tejun Heo
2024-01-31 21:23 ` Mikulas Patocka
2024-01-30 9:11 ` [PATCH 8/8] dm-verity: " Tejun Heo
2024-01-31 21:19 ` Mikulas Patocka
2024-01-31 21:32 ` Tejun Heo
2024-01-31 22:02 ` Mikulas Patocka
2024-01-31 23:19 ` Linus Torvalds
2024-02-01 0:04 ` Tejun Heo
2024-02-01 0:19 ` Mike Snitzer
2024-02-20 19:44 ` Mike Snitzer
2024-02-20 20:05 ` Tejun Heo
2024-02-22 21:24 ` Mike Snitzer
2024-02-23 17:22 ` Tejun Heo
2024-02-01 0:07 ` Mike Snitzer
2024-01-30 9:22 ` [PATCHSET wq/for-6.9] workqueue: Implement BH workqueue and convert several tasklet users Tejun Heo
2024-01-30 10:20 ` Sebastian Andrzej Siewior
2024-01-30 15:50 ` Tejun Heo
2024-01-30 23:37 ` Allen
2024-02-04 21:33 ` Tejun Heo
2024-02-05 20:50 ` Allen
2024-02-05 21:12 ` Tejun Heo
2024-02-05 21:31 ` Allen
2024-02-07 19:02 ` Allen
2024-02-08 16:56 ` Tejun Heo
2024-02-08 19:07 ` Allen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240130091300.2968534-2-tj@kernel.org \
--to=tj@kernel$(echo .)org \
--cc=allen.lkml@gmail$(echo .)com \
--cc=bob.liu@oracle$(echo .)com \
--cc=damien.lemoal@wdc$(echo .)com \
--cc=dm-devel@lists$(echo .)linux.dev \
--cc=houtao1@huawei$(echo .)com \
--cc=ignat@cloudflare$(echo .)com \
--cc=kernel-team@meta$(echo .)com \
--cc=linux-kernel@vger$(echo .)kernel.org \
--cc=mingo@kernel$(echo .)org \
--cc=mpatocka@redhat$(echo .)com \
--cc=msnitzer@redhat$(echo .)com \
--cc=netdev@vger$(echo .)kernel.org \
--cc=peterz@infradead$(echo .)org \
--cc=torvalds@linux-foundation$(echo .)org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox