psi: fix possible trigger missing in the window
authorZhaoyang Huang <zhaoyang.huang@unisoc.com>
Tue, 25 Jan 2022 06:56:58 +0000 (14:56 +0800)
committerPeter Zijlstra <peterz@infradead.org>
Wed, 16 Feb 2022 14:57:54 +0000 (15:57 +0100)
When a new threshold breaching stall happens after a psi event was
generated and within the window duration, the new event is not
generated because the events are rate-limited to one per window. If
after that no new stall is recorded then the event will not be
generated even after rate-limiting duration has passed. This is
happening because with no new stall, window_update will not be called
even though threshold was previously breached. To fix this, record
threshold breaching occurrence and generate the event once window
duration is passed.

Suggested-by: Suren Baghdasaryan <surenb@google.com>
Signed-off-by: Zhaoyang Huang <zhaoyang.huang@unisoc.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Suren Baghdasaryan <surenb@google.com>
Link: https://lore.kernel.org/r/1643093818-19835-1-git-send-email-huangzhaoyang@gmail.com
include/linux/psi_types.h
kernel/sched/psi.c

index 516c0fe836fd5be6b17f29c73ffcac892bf9addc..dc3ec5e4b9eea3dec1369798688653eb08f53a11 100644 (file)
@@ -144,6 +144,9 @@ struct psi_trigger {
 
        /* Refcounting to prevent premature destruction */
        struct kref refcount;
+
+       /* Deferred event(s) from previous ratelimit window */
+       bool pending_event;
 };
 
 struct psi_group {
index cfe76f704d8a0682a7996bf5724eafce9a11d9ec..e9d623cb8d1bd49f5e71247ba9eb878f79cac2aa 100644 (file)
@@ -523,7 +523,7 @@ static void init_triggers(struct psi_group *group, u64 now)
 static u64 update_triggers(struct psi_group *group, u64 now)
 {
        struct psi_trigger *t;
-       bool new_stall = false;
+       bool update_total = false;
        u64 *total = group->total[PSI_POLL];
 
        /*
@@ -532,24 +532,35 @@ static u64 update_triggers(struct psi_group *group, u64 now)
         */
        list_for_each_entry(t, &group->triggers, node) {
                u64 growth;
+               bool new_stall;
 
-               /* Check for stall activity */
-               if (group->polling_total[t->state] == total[t->state])
-                       continue;
+               new_stall = group->polling_total[t->state] != total[t->state];
 
+               /* Check for stall activity or a previous threshold breach */
+               if (!new_stall && !t->pending_event)
+                       continue;
                /*
-                * Multiple triggers might be looking at the same state,
-                * remember to update group->polling_total[] once we've
-                * been through all of them. Also remember to extend the
-                * polling time if we see new stall activity.
+                * Check for new stall activity, as well as deferred
+                * events that occurred in the last window after the
+                * trigger had already fired (we want to ratelimit
+                * events without dropping any).
                 */
-               new_stall = true;
-
-               /* Calculate growth since last update */
-               growth = window_update(&t->win, now, total[t->state]);
-               if (growth < t->threshold)
-                       continue;
-
+               if (new_stall) {
+                       /*
+                        * Multiple triggers might be looking at the same state,
+                        * remember to update group->polling_total[] once we've
+                        * been through all of them. Also remember to extend the
+                        * polling time if we see new stall activity.
+                        */
+                       update_total = true;
+
+                       /* Calculate growth since last update */
+                       growth = window_update(&t->win, now, total[t->state]);
+                       if (growth < t->threshold)
+                               continue;
+
+                       t->pending_event = true;
+               }
                /* Limit event signaling to once per window */
                if (now < t->last_event_time + t->win.size)
                        continue;
@@ -558,9 +569,11 @@ static u64 update_triggers(struct psi_group *group, u64 now)
                if (cmpxchg(&t->event, 0, 1) == 0)
                        wake_up_interruptible(&t->event_wait);
                t->last_event_time = now;
+               /* Reset threshold breach flag once event got generated */
+               t->pending_event = false;
        }
 
-       if (new_stall)
+       if (update_total)
                memcpy(group->polling_total, total,
                                sizeof(group->polling_total));
 
@@ -1125,6 +1138,7 @@ struct psi_trigger *psi_trigger_create(struct psi_group *group,
        t->last_event_time = 0;
        init_waitqueue_head(&t->event_wait);
        kref_init(&t->refcount);
+       t->pending_event = false;
 
        mutex_lock(&group->trigger_lock);