From patchwork Thu Mar 9 17:07:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Domenico Cerasuolo X-Patchwork-Id: 67060 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp425873wrd; Thu, 9 Mar 2023 09:46:09 -0800 (PST) X-Google-Smtp-Source: AK7set/wZnvU1m4C1WCHPTTpkci7Oh7uX/lnOe8N6oH2TL+jovcJL2sVwdz/mffs8BvCGSp/tsgT X-Received: by 2002:a17:90a:12c4:b0:23a:f4e7:733d with SMTP id b4-20020a17090a12c400b0023af4e7733dmr7370209pjg.10.1678383969342; Thu, 09 Mar 2023 09:46:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678383969; cv=none; d=google.com; s=arc-20160816; b=cksLNXcbt/jAXr8blhK3SrDq5qKiSL6WsW/iCLLS61iDnNjRmFtWfgQq94M2TM1mKt hMaw7n3vLwOoiVd79ps0nAfujzNVRiX1h9QmvnUFynimBiRNlVVh6kOGJcqyHuq2UH0J b/187Ywve1sGvH/jnHDD/4BXzEIGMrh9+jhgNJab4UD0r8IQXoUK3Qmq/tK02QUu+ATa LBBCfwxKf1O7JvEKn0yinTW1/igEjQ0occkUILA+1V2+XzoUF23FffFrlo7MHTVPJV0E yt+q7qwev0jPvqzsH8cX0BwE9IDJJNSlgdgCtFPa/2o4Mob0JUCLRbFvGWs8mH0HE+vx ixMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=e39gTwxGluXQJRtHutgVo/eHgQ2HpZYFqbaudGJzbN8=; b=fh9s/3UF+Huyxn0e/Nvge33Zn1o4x9pqEz5wh+JTFmpn60IFiyNSihD8Z8MR2AhM6h UpGCQFgY4ETkCiBGCjOJs/p735FVkdFaLEl3vDQV7XFbFK8q6m9LnuyVT+JYbhM2kw16 XbVvaK2UX91mQyBTZ6enFTgXOSoZnvM8KoBUgHGcpfttv9Dilhbn+TDYCGqzXLWnlPcg vd3cYrznVIES8S3ijvdac3FU1n7/aaOEK/lwas5Dmyw4Gqn/rj3ySjjsjkBXlGhoHU4y 0hTFiT8wABVrEPb3yyqRkpy41q/huRwnb5Q2cd0MHRI/q/HQLMxlM9W1ZpwJAWhV1oDL BECw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=OYRmI8B9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n11-20020a17090ade8b00b0022c4ddf8413si386313pjv.8.2023.03.09.09.45.56; Thu, 09 Mar 2023 09:46:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=OYRmI8B9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229913AbjCIRLF (ORCPT + 99 others); Thu, 9 Mar 2023 12:11:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231325AbjCIRKB (ORCPT ); Thu, 9 Mar 2023 12:10:01 -0500 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBB344C6E5 for ; Thu, 9 Mar 2023 09:08:03 -0800 (PST) Received: by mail-ed1-x532.google.com with SMTP id g3so9850493eda.1 for ; Thu, 09 Mar 2023 09:08:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678381682; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=e39gTwxGluXQJRtHutgVo/eHgQ2HpZYFqbaudGJzbN8=; b=OYRmI8B9D0MjYsSKwLdDr+uzR7jo6MQ8AS4rod/3sOVErh9mOpD9j9SEyXXbt3CPjI M0yGJgQSlC36lTTBtX49rRQzEc18Bl142EGJdvxaUCVi6p+pSogY8VgAKiXhgC+zVBEl ncAvvlrLGtJ4A35pcbzkORaXp0x9fhzdLgviuX+f37FG/aAEEfTMpwHHJlJZyz25d6x5 LjTfHXB7TnPmNwy37MJScd9c9pFR1/KoYqr2vDXwzowyz8km4WWaSLqPwqsyNZGgpX9g H+wggHzgLjuitlK/4DVgkcTaF1G1aqtLzoVsftH+Bab+BCTGG2TPS1cu6Aw8zObdT0Ra OZlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678381682; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=e39gTwxGluXQJRtHutgVo/eHgQ2HpZYFqbaudGJzbN8=; b=5bxplLYOOWEcOefjJnsStZ9qT1pS37FskreoyWeo9d7d1MS2HaWc1E9etkjvq2T6Y8 bZuF0d0q52hWsW58mvCHQFXefD8a+EZNm2iJZgs98OcClP6p8QWoTq3ZcwIZye8OXTfq 5YR+jN8Gb4g8CTBNeCrRhZHaJkfUVnAWHTOaO0euOyUtp5Pw5Jq8Ze9rDdNm7+EVWHE1 R7+fBV6yACnDO5eyjaUQMwkppCr8mMgIH+3+AfdDJsMmaZARQ+umOy1yxkuta+ERdd2K VbWsnT0qclN72EXbvc12SrSWxqw5XuQB48PaYGDmbliO7Ku6ygMXXbIX6zWz2O8yFbRq 0y5A== X-Gm-Message-State: AO0yUKUK97M2BxQ9C6Pf/qBQ2PyhWuiDmpcN2i+Sz8qlqT/++BlVKjZa h+Pla39pMYfrjBZD7gOUoa24Tx8acGHBhw== X-Received: by 2002:aa7:d80f:0:b0:4ce:bb5c:158e with SMTP id v15-20020aa7d80f000000b004cebb5c158emr21630736edq.19.1678381681935; Thu, 09 Mar 2023 09:08:01 -0800 (PST) Received: from lelloman-5950.. (host-79-22-154-28.retail.telecomitalia.it. [79.22.154.28]) by smtp.gmail.com with ESMTPSA id bj8-20020a170906b04800b008d85435f914sm9154867ejb.98.2023.03.09.09.08.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Mar 2023 09:08:01 -0800 (PST) From: Domenico Cerasuolo To: linux-kernel@vger.kernel.org Cc: peterz@infradead.org, surenb@google.com, brauner@kernel.org, chris@chrisdown.name, hannes@cmpxchg.org, Domenico Cerasuolo Subject: [PATCH 1/4] sched/psi: rearrange polling code in preparation Date: Thu, 9 Mar 2023 18:07:53 +0100 Message-Id: <20230309170756.52927-2-cerasuolodomenico@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309170756.52927-1-cerasuolodomenico@gmail.com> References: <20230309170756.52927-1-cerasuolodomenico@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759913148996549652?= X-GMAIL-MSGID: =?utf-8?q?1759913148996549652?= Move a few functions up in the file to avoid forward declaration needed in the patch implementing unprivileged PSI triggers. Suggested-by: Johannes Weiner Signed-off-by: Domenico Cerasuolo --- kernel/sched/psi.c | 196 ++++++++++++++++++++++----------------------- 1 file changed, 98 insertions(+), 98 deletions(-) diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c index 02e011cabe91..fe9269f1d2a4 100644 --- a/kernel/sched/psi.c +++ b/kernel/sched/psi.c @@ -384,92 +384,6 @@ static void collect_percpu_times(struct psi_group *group, *pchanged_states = changed_states; } -static u64 update_averages(struct psi_group *group, u64 now) -{ - unsigned long missed_periods = 0; - u64 expires, period; - u64 avg_next_update; - int s; - - /* avgX= */ - expires = group->avg_next_update; - if (now - expires >= psi_period) - missed_periods = div_u64(now - expires, psi_period); - - /* - * The periodic clock tick can get delayed for various - * reasons, especially on loaded systems. To avoid clock - * drift, we schedule the clock in fixed psi_period intervals. - * But the deltas we sample out of the per-cpu buckets above - * are based on the actual time elapsing between clock ticks. - */ - avg_next_update = expires + ((1 + missed_periods) * psi_period); - period = now - (group->avg_last_update + (missed_periods * psi_period)); - group->avg_last_update = now; - - for (s = 0; s < NR_PSI_STATES - 1; s++) { - u32 sample; - - sample = group->total[PSI_AVGS][s] - group->avg_total[s]; - /* - * Due to the lockless sampling of the time buckets, - * recorded time deltas can slip into the next period, - * which under full pressure can result in samples in - * excess of the period length. - * - * We don't want to report non-sensical pressures in - * excess of 100%, nor do we want to drop such events - * on the floor. Instead we punt any overage into the - * future until pressure subsides. By doing this we - * don't underreport the occurring pressure curve, we - * just report it delayed by one period length. - * - * The error isn't cumulative. As soon as another - * delta slips from a period P to P+1, by definition - * it frees up its time T in P. - */ - if (sample > period) - sample = period; - group->avg_total[s] += sample; - calc_avgs(group->avg[s], missed_periods, sample, period); - } - - return avg_next_update; -} - -static void psi_avgs_work(struct work_struct *work) -{ - struct delayed_work *dwork; - struct psi_group *group; - u32 changed_states; - u64 now; - - dwork = to_delayed_work(work); - group = container_of(dwork, struct psi_group, avgs_work); - - mutex_lock(&group->avgs_lock); - - now = sched_clock(); - - collect_percpu_times(group, PSI_AVGS, &changed_states); - /* - * If there is task activity, periodically fold the per-cpu - * times and feed samples into the running averages. If things - * are idle and there is no data to process, stop the clock. - * Once restarted, we'll catch up the running averages in one - * go - see calc_avgs() and missed_periods. - */ - if (now >= group->avg_next_update) - group->avg_next_update = update_averages(group, now); - - if (changed_states & PSI_STATE_RESCHEDULE) { - schedule_delayed_work(dwork, nsecs_to_jiffies( - group->avg_next_update - now) + 1); - } - - mutex_unlock(&group->avgs_lock); -} - /* Trigger tracking window manipulations */ static void window_reset(struct psi_window *win, u64 now, u64 value, u64 prev_growth) @@ -516,18 +430,6 @@ static u64 window_update(struct psi_window *win, u64 now, u64 value) return growth; } -static void init_triggers(struct psi_group *group, u64 now) -{ - struct psi_trigger *t; - - list_for_each_entry(t, &group->triggers, node) - window_reset(&t->win, now, - group->total[PSI_POLL][t->state], 0); - memcpy(group->polling_total, group->total[PSI_POLL], - sizeof(group->polling_total)); - group->polling_next_update = now + group->poll_min_period; -} - static u64 update_triggers(struct psi_group *group, u64 now) { struct psi_trigger *t; @@ -590,6 +492,104 @@ static u64 update_triggers(struct psi_group *group, u64 now) return now + group->poll_min_period; } +static u64 update_averages(struct psi_group *group, u64 now) +{ + unsigned long missed_periods = 0; + u64 expires, period; + u64 avg_next_update; + int s; + + /* avgX= */ + expires = group->avg_next_update; + if (now - expires >= psi_period) + missed_periods = div_u64(now - expires, psi_period); + + /* + * The periodic clock tick can get delayed for various + * reasons, especially on loaded systems. To avoid clock + * drift, we schedule the clock in fixed psi_period intervals. + * But the deltas we sample out of the per-cpu buckets above + * are based on the actual time elapsing between clock ticks. + */ + avg_next_update = expires + ((1 + missed_periods) * psi_period); + period = now - (group->avg_last_update + (missed_periods * psi_period)); + group->avg_last_update = now; + + for (s = 0; s < NR_PSI_STATES - 1; s++) { + u32 sample; + + sample = group->total[PSI_AVGS][s] - group->avg_total[s]; + /* + * Due to the lockless sampling of the time buckets, + * recorded time deltas can slip into the next period, + * which under full pressure can result in samples in + * excess of the period length. + * + * We don't want to report non-sensical pressures in + * excess of 100%, nor do we want to drop such events + * on the floor. Instead we punt any overage into the + * future until pressure subsides. By doing this we + * don't underreport the occurring pressure curve, we + * just report it delayed by one period length. + * + * The error isn't cumulative. As soon as another + * delta slips from a period P to P+1, by definition + * it frees up its time T in P. + */ + if (sample > period) + sample = period; + group->avg_total[s] += sample; + calc_avgs(group->avg[s], missed_periods, sample, period); + } + + return avg_next_update; +} + +static void psi_avgs_work(struct work_struct *work) +{ + struct delayed_work *dwork; + struct psi_group *group; + u32 changed_states; + u64 now; + + dwork = to_delayed_work(work); + group = container_of(dwork, struct psi_group, avgs_work); + + mutex_lock(&group->avgs_lock); + + now = sched_clock(); + + collect_percpu_times(group, PSI_AVGS, &changed_states); + /* + * If there is task activity, periodically fold the per-cpu + * times and feed samples into the running averages. If things + * are idle and there is no data to process, stop the clock. + * Once restarted, we'll catch up the running averages in one + * go - see calc_avgs() and missed_periods. + */ + if (now >= group->avg_next_update) + group->avg_next_update = update_averages(group, now); + + if (changed_states & PSI_STATE_RESCHEDULE) { + schedule_delayed_work(dwork, nsecs_to_jiffies( + group->avg_next_update - now) + 1); + } + + mutex_unlock(&group->avgs_lock); +} + +static void init_triggers(struct psi_group *group, u64 now) +{ + struct psi_trigger *t; + + list_for_each_entry(t, &group->triggers, node) + window_reset(&t->win, now, + group->total[PSI_POLL][t->state], 0); + memcpy(group->polling_total, group->total[PSI_POLL], + sizeof(group->polling_total)); + group->polling_next_update = now + group->poll_min_period; +} + /* Schedule polling if it's not already scheduled or forced. */ static void psi_schedule_poll_work(struct psi_group *group, unsigned long delay, bool force)