Skip to contentSkip to conversation

PATCH] psi: fix race between psi_trigger_create and psimon

RFC Superseded Huangzhaoyang 3 participants 7 messages May 17, 2021 – May 18, 2021

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

RFC 1/1 PATCH] psi: fix race between psi_trigger_create and psimon

From: Zhaoyang Huang <[email protected]>

Race detected between psimon_new and psimon_old as shown below, which cause panic by accessing invalid psi_system->poll_wait->wait_queue_entry and psi_system->poll_timer->entry->next. It is not necessary to reinit resource of psi_system when psi_trigger_create.

psi_trigger_create psimon_new psimon_old
init_waitqueue_head finish_wait
spin_lock(lock_old)
spin_lock_init(lock_new)
wake_up_process(psimon_new)

finish_wait
spin_lock(lock_new)
list_del list_del

---
 kernel/sched/psi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sched/psi.c +4 -2
index cc25a3c..d00e585 100644
@@ -182,6 +182,8 @@ struct psi_group psi_system = {
 
 static void psi_avgs_work(struct work_struct *work);
 
+static void poll_timer_fn(struct timer_list *t);
+
 static void group_init(struct psi_group *group)
 {
 	int cpu;
@@ -201,6 +203,8 @@ static void group_init(struct psi_group *group)
 	memset(group->polling_total, 0, sizeof(group->polling_total));
 	group->polling_next_update = ULLONG_MAX;
 	group->polling_until = 0;
+	init_waitqueue_head(&group->poll_wait);
+	timer_setup(&group->poll_timer, poll_timer_fn, 0);
 	rcu_assign_pointer(group->poll_task, NULL);
 }
 
@@ -1157,7 +1161,6 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
 			return ERR_CAST(task);
 		}
 		atomic_set(&group->poll_wakeup, 0);
-		init_waitqueue_head(&group->poll_wait);
 		wake_up_process(task);
 		timer_setup(&group->poll_timer, poll_timer_fn, 0);
 		rcu_assign_pointer(group->poll_task, task);
@@ -1233,7 +1236,6 @@ static void psi_trigger_destroy(struct kref *ref)
 		 * But it might have been already scheduled before
 		 * that - deschedule it cleanly before destroying it.
 		 */
-		del_timer_sync(&group->poll_timer);
 		kthread_stop(task_to_destroy);
 	}
 	kfree(t);
Signature · 1 line
1.9.1
maintainer
#1 lore raw

CC Suren

Quoted 38 lines · Huangzhaoyang

From: Zhaoyang Huang <[email protected]>

Race detected between psimon_new and psimon_old as shown below, which cause panic by accessing invalid psi_system->poll_wait->wait_queue_entry and psi_system->poll_timer->entry->next. It is not necessary to reinit resource of psi_system when psi_trigger_create.

psi_trigger_create psimon_new psimon_old
init_waitqueue_head finish_wait
spin_lock(lock_old)
spin_lock_init(lock_new)
wake_up_process(psimon_new)

finish_wait
spin_lock(lock_new)
list_del list_del

---
 kernel/sched/psi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sched/psi.c +2 -0
index cc25a3c..d00e585 100644
@@ -182,6 +182,8 @@ struct psi_group psi_system = {
 
 static void psi_avgs_work(struct work_struct *work);
 
+static void poll_timer_fn(struct timer_list *t);
+
 static void group_init(struct psi_group *group)
 {
 	int cpu;

kernel/sched/psi.c static void group_init(struct psi_group *group) · line 207

Quoted 5 lines
kernel/sched/psi.c +2 -0
@@ -201,6 +203,8 @@ static void group_init(struct psi_group *group)
 	memset(group->polling_total, 0, sizeof(group->polling_total));
 	group->polling_next_update = ULLONG_MAX;
 	group->polling_until = 0;
+	init_waitqueue_head(&group->poll_wait);
+	timer_setup(&group->poll_timer, poll_timer_fn, 0);

This makes sense.

kernel/sched/psi.c struct psi_trigger *psi_trigger_create(struct psi_group *group, · line 1165

Quoted 10 lines
kernel/sched/psi.c +0 -1
 	rcu_assign_pointer(group->poll_task, NULL);
 }
 
@@ -1157,7 +1161,6 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
 			return ERR_CAST(task);
 		}
 		atomic_set(&group->poll_wakeup, 0);
-		init_waitqueue_head(&group->poll_wait);
 		wake_up_process(task);
 		timer_setup(&group->poll_timer, poll_timer_fn, 0);

This looks now unncessary?

kernel/sched/psi.c static void psi_trigger_destroy(struct kref *ref) · line 1236

Quoted 6 lines
kernel/sched/psi.c +0 -1
 		rcu_assign_pointer(group->poll_task, task);
@@ -1233,7 +1236,6 @@ static void psi_trigger_destroy(struct kref *ref)
 		 * But it might have been already scheduled before
 		 * that - deschedule it cleanly before destroying it.
 		 */
-		del_timer_sync(&group->poll_timer);

And this looks wrong. Did you mean to delete the timer_setup() line instead?

#2 lore raw
Quoted 2 lines · Johannes Weiner

CC Suren

Thanks!

Quoted 8 lines · Huangzhaoyang
Quoted 6 lines

From: Zhaoyang Huang <[email protected]>

Race detected between psimon_new and psimon_old as shown below, which cause panic by accessing invalid psi_system->poll_wait->wait_queue_entry and psi_system->poll_timer->entry->next. It is not necessary to reinit resource of psi_system when psi_trigger_create.

resource of psi_system will not be reinitialized because init_waitqueue_head(&group->poll_wait) and friends are initialized only during the creation of the first trigger for that group (see this
condition: https://elixir.bootlin.com/linux/latest/source/kernel/sched/psi.c#L1119).

Quoted 10 lines
Quoted 10 lines

psi_trigger_create psimon_new psimon_old
init_waitqueue_head finish_wait
spin_lock(lock_old)
spin_lock_init(lock_new)
wake_up_process(psimon_new)

finish_wait
spin_lock(lock_new)
list_del list_del

Could you please clarify this race a bit? I'm having trouble deciphering this diagram. I'm guessing psimon_new/psimon_old refer to a new trigger being created while an old one is being deleted, so it seems like a race between psi_trigger_create/psi_trigger_destroy. The combination of trigger_lock and RCU should be protecting us from that but maybe I missed something?
I'm excluding a possibility of a race between psi_trigger_create with another existing trigger on the same group because the codepath calling init_waitqueue_head(&group->poll_wait) happens only when the first trigger for that group is created. Therefore if there is an existing trigger in that group that codepath will not be taken.

Quoted 16 lines
Quoted 16 lines
---
 kernel/sched/psi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sched/psi.c +0 -0
index cc25a3c..d00e585 100644
@@ -182,6 +182,8 @@ struct psi_group psi_system = {
 
 static void psi_avgs_work(struct work_struct *work);
 
Quoted 13 lines

kernel/sched/psi.c static void group_init(struct psi_group *group) · line 207

Quoted 11 lines
kernel/sched/psi.c +4 -0
+static void poll_timer_fn(struct timer_list *t);
+
 static void group_init(struct psi_group *group)
 {
      int cpu;
@@ -201,6 +203,8 @@ static void group_init(struct psi_group *group)
      memset(group->polling_total, 0, sizeof(group->polling_total));
      group->polling_next_update = ULLONG_MAX;
      group->polling_until = 0;
+     init_waitqueue_head(&group->poll_wait);
+     timer_setup(&group->poll_timer, poll_timer_fn, 0);

This makes sense.

Well, this means we initialize resources for triggers in each psi group even if the user never creates any triggers. Current logic initializes them when the first trigger in the group gets created.

Quoted 14 lines

kernel/sched/psi.c struct psi_trigger *psi_trigger_create(struct psi_group *group, · line 1165

Quoted 11 lines
kernel/sched/psi.c +0 -1
      rcu_assign_pointer(group->poll_task, NULL);
 }
 
@@ -1157,7 +1161,6 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
                      return ERR_CAST(task);
              }
              atomic_set(&group->poll_wakeup, 0);
-             init_waitqueue_head(&group->poll_wait);
              wake_up_process(task);
              timer_setup(&group->poll_timer, poll_timer_fn, 0);

This looks now unncessary?

Quoted 9 lines

kernel/sched/psi.c static void psi_trigger_destroy(struct kref *ref) · line 1236

Quoted 6 lines
kernel/sched/psi.c +0 -1
              rcu_assign_pointer(group->poll_task, task);
@@ -1233,7 +1236,6 @@ static void psi_trigger_destroy(struct kref *ref)
               * But it might have been already scheduled before
               * that - deschedule it cleanly before destroying it.
               */
-             del_timer_sync(&group->poll_timer);

And this looks wrong. Did you mean to delete the timer_setup() line instead?

I would like to get more details about this race before trying to fix it. Please clarify.
Thanks!

#3 lore raw
Quoted 32 lines · Suren Baghdasaryan
Quoted 2 lines · Johannes Weiner

CC Suren

Thanks!

Quoted 8 lines · Huangzhaoyang
Quoted 6 lines

From: Zhaoyang Huang <[email protected]>

Race detected between psimon_new and psimon_old as shown below, which cause panic by accessing invalid psi_system->poll_wait->wait_queue_entry and psi_system->poll_timer->entry->next. It is not necessary to reinit resource of psi_system when psi_trigger_create.

resource of psi_system will not be reinitialized because init_waitqueue_head(&group->poll_wait) and friends are initialized only during the creation of the first trigger for that group (see this
condition: https://elixir.bootlin.com/linux/latest/source/kernel/sched/psi.c#L1119).

Quoted 10 lines
Quoted 10 lines

psi_trigger_create psimon_new psimon_old
init_waitqueue_head finish_wait
spin_lock(lock_old)
spin_lock_init(lock_new)
wake_up_process(psimon_new)

finish_wait
spin_lock(lock_new)
list_del list_del

Quoted 11 lines

Could you please clarify this race a bit? I'm having trouble deciphering this diagram. I'm guessing psimon_new/psimon_old refer to a new trigger being created while an old one is being deleted, so it seems like a race between psi_trigger_create/psi_trigger_destroy. The combination of trigger_lock and RCU should be protecting us from that but maybe I missed something?
I'm excluding a possibility of a race between psi_trigger_create with another existing trigger on the same group because the codepath calling init_waitqueue_head(&group->poll_wait) happens only when the first trigger for that group is created. Therefore if there is an existing trigger in that group that codepath will not be taken.

Ok, looking at the current code I think you can hit the following race when psi_trigger_destroy is destroying the last trigger in a psi group while racing with psi_trigger_create:

psi_trigger_destroy psi_trigger_create
mutex_lock(trigger_lock);
rcu_assign_pointer(poll_task, NULL);

mutex_unlock(trigger_lock);
                                                    mutex_lock(trigger_lock);
                                                    if

(!rcu_access_pointer(group->poll_task)) {

timer_setup(poll_timer, poll_timer_fn, 0);

rcu_assign_pointer(poll_task, task);
                                                    }
                                                    mutex_unlock(trigger_lock);

synchronize_rcu();
del_timer_sync(poll_timer); <-- poll_timer has been reinitialized by psi_trigger_create

So, trigger_lock/RCU correctly protects destruction of group->poll_task but misses this race affecting poll_timer and poll_wait.
Let me think if we can handle this without moving initialization into group_init().

Quoted 62 lines
Quoted 17 lines
Quoted 17 lines
---
 kernel/sched/psi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sched/psi.c +0 -0
index cc25a3c..d00e585 100644
@@ -182,6 +182,8 @@ struct psi_group psi_system = {
 
 static void psi_avgs_work(struct work_struct *work);
 
Quoted 13 lines

kernel/sched/psi.c static void group_init(struct psi_group *group) · line 207

Quoted 11 lines
kernel/sched/psi.c +4 -0
+static void poll_timer_fn(struct timer_list *t);
+
 static void group_init(struct psi_group *group)
 {
      int cpu;
@@ -201,6 +203,8 @@ static void group_init(struct psi_group *group)
      memset(group->polling_total, 0, sizeof(group->polling_total));
      group->polling_next_update = ULLONG_MAX;
      group->polling_until = 0;
+     init_waitqueue_head(&group->poll_wait);
+     timer_setup(&group->poll_timer, poll_timer_fn, 0);

This makes sense.

Well, this means we initialize resources for triggers in each psi group even if the user never creates any triggers. Current logic initializes them when the first trigger in the group gets created.

Quoted 14 lines

kernel/sched/psi.c struct psi_trigger *psi_trigger_create(struct psi_group *group, · line 1165

Quoted 11 lines
kernel/sched/psi.c +0 -1
      rcu_assign_pointer(group->poll_task, NULL);
 }
 
@@ -1157,7 +1161,6 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
                      return ERR_CAST(task);
              }
              atomic_set(&group->poll_wakeup, 0);
-             init_waitqueue_head(&group->poll_wait);
              wake_up_process(task);
              timer_setup(&group->poll_timer, poll_timer_fn, 0);

This looks now unncessary?

Quoted 9 lines

kernel/sched/psi.c static void psi_trigger_destroy(struct kref *ref) · line 1236

Quoted 6 lines
kernel/sched/psi.c +0 -1
              rcu_assign_pointer(group->poll_task, task);
@@ -1233,7 +1236,6 @@ static void psi_trigger_destroy(struct kref *ref)
               * But it might have been already scheduled before
               * that - deschedule it cleanly before destroying it.
               */
-             del_timer_sync(&group->poll_timer);

And this looks wrong. Did you mean to delete the timer_setup() line instead?

I would like to get more details about this race before trying to fix it. Please clarify.
Thanks!

#4 lore raw
Quoted 68 lines · Suren Baghdasaryan
Quoted 32 lines · Suren Baghdasaryan
Quoted 2 lines · Johannes Weiner

CC Suren

Thanks!

Quoted 8 lines · Huangzhaoyang
Quoted 6 lines

From: Zhaoyang Huang <[email protected]>

Race detected between psimon_new and psimon_old as shown below, which cause panic by accessing invalid psi_system->poll_wait->wait_queue_entry and psi_system->poll_timer->entry->next. It is not necessary to reinit resource of psi_system when psi_trigger_create.

resource of psi_system will not be reinitialized because init_waitqueue_head(&group->poll_wait) and friends are initialized only during the creation of the first trigger for that group (see this
condition: https://elixir.bootlin.com/linux/latest/source/kernel/sched/psi.c#L1119).

Quoted 10 lines
Quoted 10 lines

psi_trigger_create psimon_new psimon_old
init_waitqueue_head finish_wait
spin_lock(lock_old)
spin_lock_init(lock_new)
wake_up_process(psimon_new)

finish_wait
spin_lock(lock_new)
list_del list_del

Quoted 11 lines

Could you please clarify this race a bit? I'm having trouble deciphering this diagram. I'm guessing psimon_new/psimon_old refer to a new trigger being created while an old one is being deleted, so it seems like a race between psi_trigger_create/psi_trigger_destroy. The combination of trigger_lock and RCU should be protecting us from that but maybe I missed something?
I'm excluding a possibility of a race between psi_trigger_create with another existing trigger on the same group because the codepath calling init_waitqueue_head(&group->poll_wait) happens only when the first trigger for that group is created. Therefore if there is an existing trigger in that group that codepath will not be taken.

Ok, looking at the current code I think you can hit the following race when psi_trigger_destroy is destroying the last trigger in a psi group while racing with psi_trigger_create:

psi_trigger_destroy psi_trigger_create
mutex_lock(trigger_lock);
rcu_assign_pointer(poll_task, NULL);

mutex_unlock(trigger_lock);
                                                    mutex_lock(trigger_lock);
                                                    if

(!rcu_access_pointer(group->poll_task)) {

timer_setup(poll_timer, poll_timer_fn, 0);

rcu_assign_pointer(poll_task, task);
                                                    }
                                                    mutex_unlock(trigger_lock);

synchronize_rcu();
del_timer_sync(poll_timer); <-- poll_timer has been reinitialized by psi_trigger_create

Quoted 5 lines

So, trigger_lock/RCU correctly protects destruction of group->poll_task but misses this race affecting poll_timer and poll_wait.
Let me think if we can handle this without moving initialization into group_init().

Right, this is exactly what we met during a monkey test on an android system, where the psimon will be destroyed/recreated by unref/recreate the psi_trigger. IMHO, poll_timer and poll_wait should exist during whole period

Quoted 63 lines
Quoted 63 lines
Quoted 18 lines
Quoted 18 lines
---
 kernel/sched/psi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sched/psi.c +0 -0
index cc25a3c..d00e585 100644
@@ -182,6 +182,8 @@ struct psi_group psi_system = {
 
 static void psi_avgs_work(struct work_struct *work);
 
Quoted 13 lines

kernel/sched/psi.c static void group_init(struct psi_group *group) · line 207

Quoted 11 lines
kernel/sched/psi.c +4 -0
+static void poll_timer_fn(struct timer_list *t);
+
 static void group_init(struct psi_group *group)
 {
      int cpu;
@@ -201,6 +203,8 @@ static void group_init(struct psi_group *group)
      memset(group->polling_total, 0, sizeof(group->polling_total));
      group->polling_next_update = ULLONG_MAX;
      group->polling_until = 0;
+     init_waitqueue_head(&group->poll_wait);
+     timer_setup(&group->poll_timer, poll_timer_fn, 0);

This makes sense.

Well, this means we initialize resources for triggers in each psi group even if the user never creates any triggers. Current logic initializes them when the first trigger in the group gets created.

Quoted 14 lines

kernel/sched/psi.c struct psi_trigger *psi_trigger_create(struct psi_group *group, · line 1165

Quoted 11 lines
kernel/sched/psi.c +0 -1
      rcu_assign_pointer(group->poll_task, NULL);
 }
 
@@ -1157,7 +1161,6 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
                      return ERR_CAST(task);
              }
              atomic_set(&group->poll_wakeup, 0);
-             init_waitqueue_head(&group->poll_wait);
              wake_up_process(task);
              timer_setup(&group->poll_timer, poll_timer_fn, 0);

This looks now unncessary?

Quoted 9 lines

kernel/sched/psi.c static void psi_trigger_destroy(struct kref *ref) · line 1236

Quoted 6 lines
kernel/sched/psi.c +0 -1
              rcu_assign_pointer(group->poll_task, task);
@@ -1233,7 +1236,6 @@ static void psi_trigger_destroy(struct kref *ref)
               * But it might have been already scheduled before
               * that - deschedule it cleanly before destroying it.
               */
-             del_timer_sync(&group->poll_timer);

And this looks wrong. Did you mean to delete the timer_setup() line instead?

I would like to get more details about this race before trying to fix it. Please clarify.
Thanks!

#5 lore raw
Quoted 75 lines · Zhaoyang Huang
Quoted 68 lines · Suren Baghdasaryan
Quoted 32 lines · Suren Baghdasaryan
Quoted 2 lines · Johannes Weiner

CC Suren

Thanks!

Quoted 8 lines · Huangzhaoyang
Quoted 6 lines

From: Zhaoyang Huang <[email protected]>

Race detected between psimon_new and psimon_old as shown below, which cause panic by accessing invalid psi_system->poll_wait->wait_queue_entry and psi_system->poll_timer->entry->next. It is not necessary to reinit resource of psi_system when psi_trigger_create.

resource of psi_system will not be reinitialized because init_waitqueue_head(&group->poll_wait) and friends are initialized only during the creation of the first trigger for that group (see this
condition: https://elixir.bootlin.com/linux/latest/source/kernel/sched/psi.c#L1119).

Quoted 10 lines
Quoted 10 lines

psi_trigger_create psimon_new psimon_old
init_waitqueue_head finish_wait
spin_lock(lock_old)
spin_lock_init(lock_new)
wake_up_process(psimon_new)

finish_wait
spin_lock(lock_new)
list_del list_del

Quoted 11 lines

Could you please clarify this race a bit? I'm having trouble deciphering this diagram. I'm guessing psimon_new/psimon_old refer to a new trigger being created while an old one is being deleted, so it seems like a race between psi_trigger_create/psi_trigger_destroy. The combination of trigger_lock and RCU should be protecting us from that but maybe I missed something?
I'm excluding a possibility of a race between psi_trigger_create with another existing trigger on the same group because the codepath calling init_waitqueue_head(&group->poll_wait) happens only when the first trigger for that group is created. Therefore if there is an existing trigger in that group that codepath will not be taken.

Ok, looking at the current code I think you can hit the following race when psi_trigger_destroy is destroying the last trigger in a psi group while racing with psi_trigger_create:

psi_trigger_destroy psi_trigger_create
mutex_lock(trigger_lock);
rcu_assign_pointer(poll_task, NULL);

mutex_unlock(trigger_lock);
                                                    mutex_lock(trigger_lock);
                                                    if

(!rcu_access_pointer(group->poll_task)) {

timer_setup(poll_timer, poll_timer_fn, 0);

rcu_assign_pointer(poll_task, task);
                                                    }
                                                    mutex_unlock(trigger_lock);

synchronize_rcu();
del_timer_sync(poll_timer); <-- poll_timer has been reinitialized by psi_trigger_create

Quoted 5 lines

So, trigger_lock/RCU correctly protects destruction of group->poll_task but misses this race affecting poll_timer and poll_wait.
Let me think if we can handle this without moving initialization into group_init().

Quoted 4 lines

Right, this is exactly what we met during a monkey test on an android system, where the psimon will be destroyed/recreated by unref/recreate the psi_trigger. IMHO, poll_timer and poll_wait should exist during whole period

Ok, understood. I think it should be ok to initialize poll_wait and poll_timer at the group creation time. Looks like init_waitqueue_head() and timer_setup() initialize the fields but I don't think they allocate some additional resources. Johannes pointed to some issues in your original patch, so I've made some small modifications (see below). del_timer_sync() was important back when we used kthread_worker, now even if timer fires unnecessarily it should be harmless after we reset group->poll_task. So I think a del_timer() in psi_trigger_destroy() should be enough:

@@ -181,6 +181,7 @@ struct psi_group psi_system = {
};

static void psi_avgs_work(struct work_struct *work);
+static void poll_timer_fn(struct timer_list *t);

static void group_init(struct psi_group *group)
{
@@ -202,6 +203,8 @@ static void group_init(struct psi_group *group)

         group->polling_next_update = ULLONG_MAX;
         group->polling_until = 0;
         rcu_assign_pointer(group->poll_task, NULL);

+ init_waitqueue_head(&group->poll_wait);
+ timer_setup(&group->poll_timer, poll_timer_fn, 0);
}

void __init psi_init(void)
@@ -1157,9 +1160,7 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,

                         return ERR_CAST(task);
                 }
                 atomic_set(&group->poll_wakeup, 0);
-                init_waitqueue_head(&group->poll_wait);
                 wake_up_process(task);
-                timer_setup(&group->poll_timer, poll_timer_fn, 0);
                 rcu_assign_pointer(group->poll_task, task);
         }

@@ -1211,6 +1212,7 @@ static void psi_trigger_destroy(struct kref *ref)

                                         group->poll_task,
                                         lockdep_is_held(&group->trigger_lock));
                         rcu_assign_pointer(group->poll_task, NULL);
+                        del_timer(&group->poll_timer);
                 }
         }

@@ -1230,10 +1232,7 @@ static void psi_trigger_destroy(struct kref *ref)
/*
* After the RCU grace period has expired, the worker
* can no longer be found through group->poll_task.
- * But it might have been already scheduled before
- * that - deschedule it cleanly before destroying it.

                  */
-                del_timer_sync(&group->poll_timer);
                 kthread_stop(task_to_destroy);
         }
         kfree(t);
Quoted 63 lines
Quoted 63 lines
Quoted 63 lines
Quoted 18 lines
Quoted 18 lines
---
 kernel/sched/psi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sched/psi.c +0 -0
index cc25a3c..d00e585 100644
@@ -182,6 +182,8 @@ struct psi_group psi_system = {
 
 static void psi_avgs_work(struct work_struct *work);
 
Quoted 13 lines

kernel/sched/psi.c static void group_init(struct psi_group *group) · line 207

Quoted 11 lines
kernel/sched/psi.c +4 -0
+static void poll_timer_fn(struct timer_list *t);
+
 static void group_init(struct psi_group *group)
 {
      int cpu;
@@ -201,6 +203,8 @@ static void group_init(struct psi_group *group)
      memset(group->polling_total, 0, sizeof(group->polling_total));
      group->polling_next_update = ULLONG_MAX;
      group->polling_until = 0;
+     init_waitqueue_head(&group->poll_wait);
+     timer_setup(&group->poll_timer, poll_timer_fn, 0);

This makes sense.

Well, this means we initialize resources for triggers in each psi group even if the user never creates any triggers. Current logic initializes them when the first trigger in the group gets created.

Quoted 14 lines

kernel/sched/psi.c struct psi_trigger *psi_trigger_create(struct psi_group *group, · line 1165

Quoted 11 lines
kernel/sched/psi.c +0 -1
      rcu_assign_pointer(group->poll_task, NULL);
 }
 
@@ -1157,7 +1161,6 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
                      return ERR_CAST(task);
              }
              atomic_set(&group->poll_wakeup, 0);
-             init_waitqueue_head(&group->poll_wait);
              wake_up_process(task);
              timer_setup(&group->poll_timer, poll_timer_fn, 0);

This looks now unncessary?

Quoted 9 lines

kernel/sched/psi.c static void psi_trigger_destroy(struct kref *ref) · line 1236

Quoted 6 lines
kernel/sched/psi.c +0 -1
              rcu_assign_pointer(group->poll_task, task);
@@ -1233,7 +1236,6 @@ static void psi_trigger_destroy(struct kref *ref)
               * But it might have been already scheduled before
               * that - deschedule it cleanly before destroying it.
               */
-             del_timer_sync(&group->poll_timer);

And this looks wrong. Did you mean to delete the timer_setup() line instead?

I would like to get more details about this race before trying to fix it. Please clarify.
Thanks!

#6 lore raw
Quoted 5 lines · Suren Baghdasaryan
Quoted 3 lines · Zhaoyang Huang
Quoted 1 line · Suren Baghdasaryan
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Suren Baghdasaryan
Quoted 6 lines · Johannes Weiner
Quoted 2 lines

CC Suren

Thanks!

When resending the patch please run scripts/get_maintainer.pl against your patch and CC reported recipients.
Thanks!

Quoted 191 lines
Quoted 65 lines
Quoted 60 lines
Quoted 26 lines
Quoted 9 lines · Huangzhaoyang
Quoted 6 lines

From: Zhaoyang Huang <[email protected]>

Race detected between psimon_new and psimon_old as shown below, which cause panic by accessing invalid psi_system->poll_wait->wait_queue_entry and psi_system->poll_timer->entry->next. It is not necessary to reinit resource of psi_system when psi_trigger_create.

resource of psi_system will not be reinitialized because init_waitqueue_head(&group->poll_wait) and friends are initialized only during the creation of the first trigger for that group (see this
condition: https://elixir.bootlin.com/linux/latest/source/kernel/sched/psi.c#L1119).

Quoted 10 lines
Quoted 10 lines

psi_trigger_create psimon_new psimon_old
init_waitqueue_head finish_wait
spin_lock(lock_old)
spin_lock_init(lock_new)
wake_up_process(psimon_new)

finish_wait
spin_lock(lock_new)
list_del list_del

Quoted 11 lines

Could you please clarify this race a bit? I'm having trouble deciphering this diagram. I'm guessing psimon_new/psimon_old refer to a new trigger being created while an old one is being deleted, so it seems like a race between psi_trigger_create/psi_trigger_destroy. The combination of trigger_lock and RCU should be protecting us from that but maybe I missed something?
I'm excluding a possibility of a race between psi_trigger_create with another existing trigger on the same group because the codepath calling init_waitqueue_head(&group->poll_wait) happens only when the first trigger for that group is created. Therefore if there is an existing trigger in that group that codepath will not be taken.

Ok, looking at the current code I think you can hit the following race when psi_trigger_destroy is destroying the last trigger in a psi group while racing with psi_trigger_create:

psi_trigger_destroy psi_trigger_create
mutex_lock(trigger_lock);
rcu_assign_pointer(poll_task, NULL);

mutex_unlock(trigger_lock);
                                                    mutex_lock(trigger_lock);
                                                    if

(!rcu_access_pointer(group->poll_task)) {

timer_setup(poll_timer, poll_timer_fn, 0);

rcu_assign_pointer(poll_task, task);
                                                    }
                                                    mutex_unlock(trigger_lock);

synchronize_rcu();
del_timer_sync(poll_timer); <-- poll_timer has been reinitialized by psi_trigger_create

Quoted 5 lines

So, trigger_lock/RCU correctly protects destruction of group->poll_task but misses this race affecting poll_timer and poll_wait.
Let me think if we can handle this without moving initialization into group_init().

Quoted 4 lines

Right, this is exactly what we met during a monkey test on an android system, where the psimon will be destroyed/recreated by unref/recreate the psi_trigger. IMHO, poll_timer and poll_wait should exist during whole period

Ok, understood. I think it should be ok to initialize poll_wait and poll_timer at the group creation time. Looks like init_waitqueue_head() and timer_setup() initialize the fields but I don't think they allocate some additional resources. Johannes pointed to some issues in your original patch, so I've made some small modifications (see below). del_timer_sync() was important back when we used kthread_worker, now even if timer fires unnecessarily it should be harmless after we reset group->poll_task. So I think a del_timer() in psi_trigger_destroy() should be enough:

kernel/sched/psi.c +4 -5
@@ -181,6 +181,7 @@ struct psi_group psi_system = {
 };
 
 static void psi_avgs_work(struct work_struct *work);
+static void poll_timer_fn(struct timer_list *t);
 
 static void group_init(struct psi_group *group)
 {
@@ -202,6 +203,8 @@ static void group_init(struct psi_group *group)
         group->polling_next_update = ULLONG_MAX;
         group->polling_until = 0;
         rcu_assign_pointer(group->poll_task, NULL);
+        init_waitqueue_head(&group->poll_wait);
+        timer_setup(&group->poll_timer, poll_timer_fn, 0);
 }
 
 void __init psi_init(void)
@@ -1157,9 +1160,7 @@ struct psi_trigger *psi_trigger_create(struct
 psi_group *group,
                         return ERR_CAST(task);
                 }
                 atomic_set(&group->poll_wakeup, 0);
-                init_waitqueue_head(&group->poll_wait);
                 wake_up_process(task);
-                timer_setup(&group->poll_timer, poll_timer_fn, 0);
                 rcu_assign_pointer(group->poll_task, task);
         }
@@ -1211,6 +1212,7 @@ static void psi_trigger_destroy(struct kref *ref)
                                         group->poll_task,
                                         lockdep_is_held(&group->trigger_lock));
                         rcu_assign_pointer(group->poll_task, NULL);
+                        del_timer(&group->poll_timer);
                 }
         }
 
@@ -1230,10 +1232,7 @@ static void psi_trigger_destroy(struct kref *ref)
                 /*
                  * After the RCU grace period has expired, the worker
                  * can no longer be found through group->poll_task.
-                 * But it might have been already scheduled before
-                 * that - deschedule it cleanly before destroying it.
                  */
-                del_timer_sync(&group->poll_timer);
                 kthread_stop(task_to_destroy);
         }
         kfree(t);
Quoted 64 lines
Quoted 64 lines
Quoted 64 lines
Quoted 19 lines
Quoted 19 lines
---
 kernel/sched/psi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sched/psi.c +0 -0
index cc25a3c..d00e585 100644
@@ -182,6 +182,8 @@ struct psi_group psi_system = {
 
 static void psi_avgs_work(struct work_struct *work);
 
Quoted 13 lines

kernel/sched/psi.c static void group_init(struct psi_group *group) · line 207

Quoted 11 lines
kernel/sched/psi.c +4 -0
+static void poll_timer_fn(struct timer_list *t);
+
 static void group_init(struct psi_group *group)
 {
      int cpu;
@@ -201,6 +203,8 @@ static void group_init(struct psi_group *group)
      memset(group->polling_total, 0, sizeof(group->polling_total));
      group->polling_next_update = ULLONG_MAX;
      group->polling_until = 0;
+     init_waitqueue_head(&group->poll_wait);
+     timer_setup(&group->poll_timer, poll_timer_fn, 0);

This makes sense.

Well, this means we initialize resources for triggers in each psi group even if the user never creates any triggers. Current logic initializes them when the first trigger in the group gets created.

Quoted 14 lines

kernel/sched/psi.c struct psi_trigger *psi_trigger_create(struct psi_group *group, · line 1165

Quoted 11 lines
kernel/sched/psi.c +0 -1
      rcu_assign_pointer(group->poll_task, NULL);
 }
 
@@ -1157,7 +1161,6 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
                      return ERR_CAST(task);
              }
              atomic_set(&group->poll_wakeup, 0);
-             init_waitqueue_head(&group->poll_wait);
              wake_up_process(task);
              timer_setup(&group->poll_timer, poll_timer_fn, 0);

This looks now unncessary?

Quoted 9 lines

kernel/sched/psi.c static void psi_trigger_destroy(struct kref *ref) · line 1236

Quoted 6 lines
kernel/sched/psi.c +0 -1
              rcu_assign_pointer(group->poll_task, task);
@@ -1233,7 +1236,6 @@ static void psi_trigger_destroy(struct kref *ref)
               * But it might have been already scheduled before
               * that - deschedule it cleanly before destroying it.
               */
-             del_timer_sync(&group->poll_timer);

And this looks wrong. Did you mean to delete the timer_setup() line instead?

I would like to get more details about this race before trying to fix it. Please clarify.
Thanks!