Skip to contentSkip to conversation

psi: eliminate kthread_worker from psi trigger scheduling mechanism

PATCH Applied Suren Baghdasaryan 3 participants 6 messages May 28, 2020 – Jun 16, 2020

Subsystems (3)
lore
Share & tools
lore

Suren Baghdasaryan

author maintainer
#0 lore raw

PATCH 1/1 psi: eliminate kthread_worker from psi trigger scheduling mechanism

Each psi group requires a dedicated kthread_delayed_work and kthread_worker. Since no other work can be performed using psi_group's kthread_worker, the same result can be obtained using a task_struct and a timer directly. This makes psi triggering simpler by removing lists and locks involved with kthread_worker usage and eliminates the need for poll_scheduled atomic use in the hot path.

---
This patch is meant to address Peter's request in [1] to pull kthread_queue_delayed_work() out from under rq->lock. This should also address the lockdep warning about possibility of a circular dependency described in [2]

[1]: https://lore.kernel.org/lkml/[email protected]/
[2]: https://lore.kernel.org/lkml/CAJuCfpG4NkhpQvZjgXZ_3gm6Hf1QgN_eUOQ8iX9Cv1k9whLwSQ@mail.gmail.com

---
 include/linux/psi_types.h |   7 ++-
 kernel/sched/psi.c        | 113 +++++++++++++++++++++-----------------
 2 files changed, 68 insertions(+), 52 deletions(-)
include/linux/psi_types.h +4 -3
index 4b7258495a04..b95f3211566a 100644
@@ -153,9 +153,10 @@ struct psi_group {
 	unsigned long avg[NR_PSI_STATES - 1][3];
 
 	/* Monitor work control */
-	atomic_t poll_scheduled;
-	struct kthread_worker __rcu *poll_kworker;
-	struct kthread_delayed_work poll_work;
+	struct task_struct __rcu *poll_task;
+	struct timer_list poll_timer;
+	wait_queue_head_t poll_wait;
+	atomic_t poll_wakeup;
 
 	/* Protects data used by the monitor */
 	struct mutex trigger_lock;
kernel/sched/psi.c +64 -49

198 lines not on this page · raw message

Signature · 1 line
2.27.0.rc0.183.gde8f92d652-goog
maintainer
#1 lore raw
Quoted 7 lines · Suren Baghdasaryan

Each psi group requires a dedicated kthread_delayed_work and kthread_worker. Since no other work can be performed using psi_group's kthread_worker, the same result can be obtained using a task_struct and a timer directly. This makes psi triggering simpler by removing lists and locks involved with kthread_worker usage and eliminates the need for poll_scheduled atomic use in the hot path.

Quoted 5 lines

---
This patch is meant to address Peter's request in [1] to pull kthread_queue_delayed_work() out from under rq->lock. This should also address the lockdep warning about possibility of a circular dependency described in [2]

I think you could've just fixed kthread_queue_delayed_work(), that code is sub-optimal.

But I suppose this works too.

author maintainer
#2 lore raw
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Suren Baghdasaryan

Each psi group requires a dedicated kthread_delayed_work and kthread_worker. Since no other work can be performed using psi_group's kthread_worker, the same result can be obtained using a task_struct and a timer directly. This makes psi triggering simpler by removing lists and locks involved with kthread_worker usage and eliminates the need for poll_scheduled atomic use in the hot path.

Quoted 8 lines
Quoted 5 lines

---
This patch is meant to address Peter's request in [1] to pull kthread_queue_delayed_work() out from under rq->lock. This should also address the lockdep warning about possibility of a circular dependency described in [2]

I think you could've just fixed kthread_queue_delayed_work(), that code is sub-optimal.

Ok, let me look into it some more. My understanding was that the worker->lock in kthread_queue_delayed_work() was needed to synchronize worker->delayed_work_list access. But maybe I'm missing something... I assume you are talking about optimizing this beyond what
https://lkml.org/lkml/2020/5/4/1148 was doing?

BTW, any objections against taking https://lkml.org/lkml/2020/5/4/1148
? It's not the ultimate fix but it is an improvement since it gets some of the operations that were unnecessarily under worker->lock out of it.

Quoted 2 lines

But I suppose this works too.

In PSI's case there is always one work for each worker, so the delayed_work_list and work_list are not needed and therefore I can replace kthread_worker machinery with a task and a timer. I think I can simplify this a bit further. For example group->poll_wakeup doesn't have to be an atomic. Originally I wanted to avoid a possibility of a race when poll_timer_fn sets it and psi_poll_worker resets it and as a result misses a wakeup, however if psi_poll_worker resets it before calling psi_poll_work then there is no harm in missing a wakeup because we called psi_poll_work and did the required work anyway.

One question about this patch I'm not sure about and wanted to ask you Peter is whether it's ok to call mod_timer from within a hotpath (while holding rq->lock). As I described in the additional comment, there is a possibility of a race between when I check timer_pending and the call to mod_timer, so it's possible that mod_timer might be called both from psi_poll_work (psi poll work handler) and from psi_task_change (hotpath under rq->lock). I see that mod_timer takes base->lock spinlock, and IIUC such a race might block the hotpath and therefore is unacceptable. If this is true I'll need to revive the poll_scheduled atomic to close this race and then I can change mod_timer into add_timer.
WDYT? And sorry for my ignorance if this is a trivial question. I'm not sure about the rules when it comes to rq->locks.

Thanks,
Suren.

Quoted 4 lines

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

author maintainer
#3 lore raw
Quoted 11 lines · Suren Baghdasaryan
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Suren Baghdasaryan

Each psi group requires a dedicated kthread_delayed_work and kthread_worker. Since no other work can be performed using psi_group's kthread_worker, the same result can be obtained using a task_struct and a timer directly. This makes psi triggering simpler by removing lists and locks involved with kthread_worker usage and eliminates the need for poll_scheduled atomic use in the hot path.

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

---
This patch is meant to address Peter's request in [1] to pull kthread_queue_delayed_work() out from under rq->lock. This should also address the lockdep warning about possibility of a circular dependency described in [2]

I think you could've just fixed kthread_queue_delayed_work(), that code is sub-optimal.

After some more staring into kthread code I think I understand what Peter's comment meant about delayed_work_list. worker->delayed_work_list seems to be unnecessary because each kthread_delayed_work has its own timer which will add the work into worker->work_list when the time comes. So there is no need to store the delayed work in an intermediate worker->delayed_work_list. However I think kthread_destroy_worker() has an issue if it's called while worker->delayed_work_list is non-empty. The issue is that kthread_destroy_worker() does not stop all the kthread_delayed_work->timers scheduled on the worker->delayed_work_list. So if such a timer fires after a call to kthread_destroy_worker(), timer's handler will dereference the already destroyed worker.

If I'm right and this is indeed an issue then I think we do need worker->delayed_work_list to cancel all the scheduled timers. The issue can be avoided if we assume that the caller will alway call kthread_cancel_delayed_work_sync() for each delayed_work scheduled on worker->delayed_work_list before calling kthread_destroy_worker(). If that's what we expect I think this expectation should be reflected in the comments and a WARN_ON(!list_empty(&worker->delayed_work_list)) be added in kthread_destroy_worker(). WDYT?

Quoted 47 lines

Ok, let me look into it some more. My understanding was that the worker->lock in kthread_queue_delayed_work() was needed to synchronize worker->delayed_work_list access. But maybe I'm missing something... I assume you are talking about optimizing this beyond what
https://lkml.org/lkml/2020/5/4/1148 was doing?

BTW, any objections against taking https://lkml.org/lkml/2020/5/4/1148
? It's not the ultimate fix but it is an improvement since it gets some of the operations that were unnecessarily under worker->lock out of it.

Quoted 2 lines

But I suppose this works too.

In PSI's case there is always one work for each worker, so the delayed_work_list and work_list are not needed and therefore I can replace kthread_worker machinery with a task and a timer. I think I can simplify this a bit further. For example group->poll_wakeup doesn't have to be an atomic. Originally I wanted to avoid a possibility of a race when poll_timer_fn sets it and psi_poll_worker resets it and as a result misses a wakeup, however if psi_poll_worker resets it before calling psi_poll_work then there is no harm in missing a wakeup because we called psi_poll_work and did the required work anyway.

One question about this patch I'm not sure about and wanted to ask you Peter is whether it's ok to call mod_timer from within a hotpath (while holding rq->lock). As I described in the additional comment, there is a possibility of a race between when I check timer_pending and the call to mod_timer, so it's possible that mod_timer might be called both from psi_poll_work (psi poll work handler) and from psi_task_change (hotpath under rq->lock). I see that mod_timer takes base->lock spinlock, and IIUC such a race might block the hotpath and therefore is unacceptable. If this is true I'll need to revive the poll_scheduled atomic to close this race and then I can change mod_timer into add_timer.
WDYT? And sorry for my ignorance if this is a trivial question. I'm not sure about the rules when it comes to rq->locks.

Thanks,
Suren.

Quoted 4 lines

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

author maintainer
#4 lore raw
Quoted 36 lines · Suren Baghdasaryan
Quoted 11 lines · Suren Baghdasaryan
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Suren Baghdasaryan

Each psi group requires a dedicated kthread_delayed_work and kthread_worker. Since no other work can be performed using psi_group's kthread_worker, the same result can be obtained using a task_struct and a timer directly. This makes psi triggering simpler by removing lists and locks involved with kthread_worker usage and eliminates the need for poll_scheduled atomic use in the hot path.

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

---
This patch is meant to address Peter's request in [1] to pull kthread_queue_delayed_work() out from under rq->lock. This should also address the lockdep warning about possibility of a circular dependency described in [2]

I think you could've just fixed kthread_queue_delayed_work(), that code is sub-optimal.

After some more staring into kthread code I think I understand what Peter's comment meant about delayed_work_list. worker->delayed_work_list seems to be unnecessary because each kthread_delayed_work has its own timer which will add the work into worker->work_list when the time comes. So there is no need to store the delayed work in an intermediate worker->delayed_work_list. However I think kthread_destroy_worker() has an issue if it's called while worker->delayed_work_list is non-empty. The issue is that kthread_destroy_worker() does not stop all the kthread_delayed_work->timers scheduled on the worker->delayed_work_list. So if such a timer fires after a call to kthread_destroy_worker(), timer's handler will dereference the already destroyed worker.

Quoted 48 lines

If I'm right and this is indeed an issue then I think we do need worker->delayed_work_list to cancel all the scheduled timers. The issue can be avoided if we assume that the caller will alway call kthread_cancel_delayed_work_sync() for each delayed_work scheduled on worker->delayed_work_list before calling kthread_destroy_worker(). If that's what we expect I think this expectation should be reflected in the comments and a WARN_ON(!list_empty(&worker->delayed_work_list)) be added in kthread_destroy_worker(). WDYT?

Quoted 39 lines

Ok, let me look into it some more. My understanding was that the worker->lock in kthread_queue_delayed_work() was needed to synchronize worker->delayed_work_list access. But maybe I'm missing something... I assume you are talking about optimizing this beyond what
https://lkml.org/lkml/2020/5/4/1148 was doing?

BTW, any objections against taking https://lkml.org/lkml/2020/5/4/1148
? It's not the ultimate fix but it is an improvement since it gets some of the operations that were unnecessarily under worker->lock out of it.

Quoted 2 lines

But I suppose this works too.

In PSI's case there is always one work for each worker, so the delayed_work_list and work_list are not needed and therefore I can replace kthread_worker machinery with a task and a timer. I think I can simplify this a bit further. For example group->poll_wakeup doesn't have to be an atomic. Originally I wanted to avoid a possibility of a race when poll_timer_fn sets it and psi_poll_worker resets it and as a result misses a wakeup, however if psi_poll_worker resets it before calling psi_poll_work then there is no harm in missing a wakeup because we called psi_poll_work and did the required work anyway.

One question about this patch I'm not sure about and wanted to ask you Peter is whether it's ok to call mod_timer from within a hotpath (while holding rq->lock). As I described in the additional comment, there is a possibility of a race between when I check timer_pending and the call to mod_timer, so it's possible that mod_timer might be called both from psi_poll_work (psi poll work handler) and from psi_task_change (hotpath under rq->lock). I see that mod_timer takes base->lock spinlock, and IIUC such a race might block the hotpath and therefore is unacceptable. If this is true I'll need to revive the poll_scheduled atomic to close this race and then I can change mod_timer into add_timer.
WDYT? And sorry for my ignorance if this is a trivial question. I'm not sure about the rules when it comes to rq->locks.

Thanks for taking this patch, Peter. I just wanted to double-check if you considered the race I mentioned in the above paragraph and decided it's a non-issue. If it is an issue I can send a small follow-up patch to close the race or I can send a new version of the whole patch with the fix if that's preferable. Please LMK.

Quoted 8 lines
Quoted 8 lines

Thanks,
Suren.

Quoted 4 lines

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

#5 lore raw
In reply to Suren Baghdasaryan
The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     461daba06bdcb9c7a3f92b9bbd110e1f7d093ffc
Gitweb:        https://git.kernel.org/tip/461daba06bdcb9c7a3f92b9bbd110e1f7d093ffc
Author:        Suren Baghdasaryan <[email protected]>
AuthorDate:    Thu, 28 May 2020 12:54:42 -07:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Mon, 15 Jun 2020 14:10:03 +02:00

psi: eliminate kthread_worker from psi trigger scheduling mechanism

Each psi group requires a dedicated kthread_delayed_work and kthread_worker. Since no other work can be performed using psi_group's kthread_worker, the same result can be obtained using a task_struct and a timer directly. This makes psi triggering simpler by removing lists and locks involved with kthread_worker usage and eliminates the need for poll_scheduled atomic use in the hot path.

---
 include/linux/psi_types.h |   7 +-
 kernel/sched/psi.c        | 113 ++++++++++++++++++++-----------------
 2 files changed, 68 insertions(+), 52 deletions(-)
include/linux/psi_types.h +4 -3
index 4b72584..b95f321 100644
@@ -153,9 +153,10 @@ struct psi_group {
 	unsigned long avg[NR_PSI_STATES - 1][3];
 
 	/* Monitor work control */
-	atomic_t poll_scheduled;
-	struct kthread_worker __rcu *poll_kworker;
-	struct kthread_delayed_work poll_work;
+	struct task_struct __rcu *poll_task;
+	struct timer_list poll_timer;
+	wait_queue_head_t poll_wait;
+	atomic_t poll_wakeup;
 
 	/* Protects data used by the monitor */
 	struct mutex trigger_lock;
kernel/sched/psi.c +64 -49

198 lines not on this page · raw message