From patchwork Mon Oct 31 10:53:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 13201 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2238257wru; Mon, 31 Oct 2022 03:54:55 -0700 (PDT) X-Google-Smtp-Source: AMsMyM549F+RcFyYjZqZw/rjaU2FD8NigNVQstjYWaw8rK8dJxfrFD9tY+Ydov3aRpV/61HkIILf X-Received: by 2002:a17:90a:8048:b0:212:c605:522c with SMTP id e8-20020a17090a804800b00212c605522cmr14399746pjw.230.1667213694787; Mon, 31 Oct 2022 03:54:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667213694; cv=none; d=google.com; s=arc-20160816; b=hyo5Qy0fUFkzms9YGANtb2/SnCdjenKcxLjBUcY7LZBJXYIMvpY/enT3ySX54RxC+P VM2f1sCHi2bR5GpiNDOGNH9IdWZ6nowTMoQpZpGE1E4SgAx0peTkcdov0K38AQFphV3W DlPQDYji7nrUsuvKG7Lyf7nW2fdkrIbzwfx1dOAupXndkUH/+LAyrsytpD17NzA465G0 bp7tpvkdMeqj7CjjCIxM5c3ZLbnT6m4Y9LL6BBv2TJU/RtIWRHNcX+BWA3b7jaHIa7Cg M+e4oZczx6JhryEpgEwWjdw7r1ZkYgthNjn0rQpZCqJK5CkvMjvg+AC9NMkG+3cmVHZ4 ipIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=3RSPSY5wJRYnB3ugtXqJ84HMVoLdph8PuV40tRRxlbo=; b=SurVR/ti8FlUF8sGye89XU7IZmSAowGKV3SrhaukBGcRMujGvjfArp+15TWTLyKYlB y8pSTaIQvohLKnCW7Tj/BSNDwwyEnZ0+z/yE3pCzBieSfuzMgDC/+PCdmu+g4xkhj5bL n1hx5jfm07e8BrAGWuhEy7kQm0dABenGKKbJ3VeQfZ8tNpWneO9UepkzHObwbtHXDLRE +fwIcvcGdoLsgl27U0XU2pHK0oX5pGAkW2Jt2sOyQFrUygMpv0S9sF7Wo+qhMKOfp6t+ JVQE4o58frSU6U3PcyU0UR1S3q6TzBzt5n349m5jqdp9gBOWIr2xDHqEkUev7HaICjlN iUZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=fMvQllP9; dkim=neutral (no key) header.i=@linutronix.de; 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=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id rm12-20020a17090b3ecc00b0020ae29877besi9238460pjb.20.2022.10.31.03.54.40; Mon, 31 Oct 2022 03:54:54 -0700 (PDT) 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=@linutronix.de header.s=2020 header.b=fMvQllP9; dkim=neutral (no key) header.i=@linutronix.de; 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=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230225AbiJaKyM (ORCPT + 99 others); Mon, 31 Oct 2022 06:54:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230087AbiJaKyC (ORCPT ); Mon, 31 Oct 2022 06:54:02 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 900643B0; Mon, 31 Oct 2022 03:54:01 -0700 (PDT) Date: Mon, 31 Oct 2022 10:53:58 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1667213640; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3RSPSY5wJRYnB3ugtXqJ84HMVoLdph8PuV40tRRxlbo=; b=fMvQllP91q/TwdfIVhimqRHoZSEmp9RDZ7QNp88cIjQbtCwQK1Gl4b7klRNh5I08GEQcZ9 6uJB0sw4Gm5qHba7xjuQ+aKKrOiODTNbwf8Tvgbjn/v3AdrOQpYihG/ZZr4AuqHn34noj0 Q117cRM30qEbz9OI10W/s1HHYVBqWb3SjlHIWWr+HYg6lQj08IX9yKCseDQG4RNx3uYo7f g7nfLsh9LS272Z5QW4gbIELxXgS5aXB7BjUU8nkegCgq01ehOqQBNEnafP5IEuq2Nqn6Sp Jy6szq3h8FlF56yOvsltqZw6sNwG3Bn3lE1iCE7PirdhF4q2QMm0JgJMpL6frg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1667213640; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3RSPSY5wJRYnB3ugtXqJ84HMVoLdph8PuV40tRRxlbo=; b=J7X/aGi+zP81FK86oE0C17+uTxsYaekQTauECABdLd54f2lR4/hesXHoRhYoD+rUdvAZyg edU4ro+D5qhXEcDQ== From: "tip-bot2 for Chengming Zhou" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/core] sched/psi: Fix avgs_work re-arm in psi_avgs_work() Cc: Pavan Kondeti , Chengming Zhou , "Peter Zijlstra (Intel)" , Johannes Weiner , Suren Baghdasaryan , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20221014110551.22695-1-zhouchengming@bytedance.com> References: <20221014110551.22695-1-zhouchengming@bytedance.com> MIME-Version: 1.0 Message-ID: <166721363899.7716.17032008194707353302.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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?1746570437327161087?= X-GMAIL-MSGID: =?utf-8?q?1748200266937411109?= The following commit has been merged into the sched/core branch of tip: Commit-ID: 2fcd7bbae90a6d844da8660a9d27079281dfbba2 Gitweb: https://git.kernel.org/tip/2fcd7bbae90a6d844da8660a9d27079281dfbba2 Author: Chengming Zhou AuthorDate: Fri, 14 Oct 2022 19:05:51 +08:00 Committer: Peter Zijlstra CommitterDate: Sun, 30 Oct 2022 10:12:14 +01:00 sched/psi: Fix avgs_work re-arm in psi_avgs_work() Pavan reported a problem that PSI avgs_work idle shutoff is not working at all. Because PSI_NONIDLE condition would be observed in psi_avgs_work()->collect_percpu_times()->get_recent_times() even if only the kworker running avgs_work on the CPU. Although commit 1b69ac6b40eb ("psi: fix aggregation idle shut-off") avoided the ping-pong wake problem when the worker sleep, psi_avgs_work() still will always re-arm the avgs_work, so shutoff is not working. This patch changes to use PSI_STATE_RESCHEDULE to flag whether to re-arm avgs_work in get_recent_times(). For the current CPU, we re-arm avgs_work only when (NR_RUNNING > 1 || NR_IOWAIT > 0 || NR_MEMSTALL > 0), for other CPUs we can just check PSI_NONIDLE delta. The new flag is only used in psi_avgs_work(), so we check in get_recent_times() that current_work() is avgs_work. One potential problem is that the brief period of non-idle time incurred between the aggregation run and the kworker's dequeue will be stranded in the per-cpu buckets until avgs_work run next time. The buckets can hold 4s worth of time, and future activity will wake the avgs_work with a 2s delay, giving us 2s worth of data we can leave behind when shut off the avgs_work. If the kworker run other works after avgs_work shut off and doesn't have any scheduler activities for 2s, this maybe a problem. Reported-by: Pavan Kondeti Signed-off-by: Chengming Zhou Signed-off-by: Peter Zijlstra (Intel) Acked-by: Johannes Weiner Acked-by: Suren Baghdasaryan Tested-by: Chengming Zhou Link: https://lore.kernel.org/r/20221014110551.22695-1-zhouchengming@bytedance.com --- include/linux/psi_types.h | 3 +++ kernel/sched/psi.c | 30 +++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/include/linux/psi_types.h b/include/linux/psi_types.h index 6e43727..3259818 100644 --- a/include/linux/psi_types.h +++ b/include/linux/psi_types.h @@ -72,6 +72,9 @@ enum psi_states { /* Use one bit in the state mask to track TSK_ONCPU */ #define PSI_ONCPU (1 << NR_PSI_STATES) +/* Flag whether to re-arm avgs_work, see details in get_recent_times() */ +#define PSI_STATE_RESCHEDULE (1 << (NR_PSI_STATES + 1)) + enum psi_aggregators { PSI_AVGS = 0, PSI_POLL, diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c index 7f40d87..a4348af 100644 --- a/kernel/sched/psi.c +++ b/kernel/sched/psi.c @@ -242,6 +242,8 @@ static void get_recent_times(struct psi_group *group, int cpu, u32 *pchanged_states) { struct psi_group_cpu *groupc = per_cpu_ptr(group->pcpu, cpu); + int current_cpu = raw_smp_processor_id(); + unsigned int tasks[NR_PSI_TASK_COUNTS]; u64 now, state_start; enum psi_states s; unsigned int seq; @@ -256,6 +258,8 @@ static void get_recent_times(struct psi_group *group, int cpu, memcpy(times, groupc->times, sizeof(groupc->times)); state_mask = groupc->state_mask; state_start = groupc->state_start; + if (cpu == current_cpu) + memcpy(tasks, groupc->tasks, sizeof(groupc->tasks)); } while (read_seqcount_retry(&groupc->seq, seq)); /* Calculate state time deltas against the previous snapshot */ @@ -280,6 +284,28 @@ static void get_recent_times(struct psi_group *group, int cpu, if (delta) *pchanged_states |= (1 << s); } + + /* + * When collect_percpu_times() from the avgs_work, we don't want to + * re-arm avgs_work when all CPUs are IDLE. But the current CPU running + * this avgs_work is never IDLE, cause avgs_work can't be shut off. + * So for the current CPU, we need to re-arm avgs_work only when + * (NR_RUNNING > 1 || NR_IOWAIT > 0 || NR_MEMSTALL > 0), for other CPUs + * we can just check PSI_NONIDLE delta. + */ + if (current_work() == &group->avgs_work.work) { + bool reschedule; + + if (cpu == current_cpu) + reschedule = tasks[NR_RUNNING] + + tasks[NR_IOWAIT] + + tasks[NR_MEMSTALL] > 1; + else + reschedule = *pchanged_states & (1 << PSI_NONIDLE); + + if (reschedule) + *pchanged_states |= PSI_STATE_RESCHEDULE; + } } static void calc_avgs(unsigned long avg[3], int missed_periods, @@ -415,7 +441,6 @@ static void psi_avgs_work(struct work_struct *work) struct delayed_work *dwork; struct psi_group *group; u32 changed_states; - bool nonidle; u64 now; dwork = to_delayed_work(work); @@ -426,7 +451,6 @@ static void psi_avgs_work(struct work_struct *work) now = sched_clock(); collect_percpu_times(group, PSI_AVGS, &changed_states); - nonidle = changed_states & (1 << PSI_NONIDLE); /* * If there is task activity, periodically fold the per-cpu * times and feed samples into the running averages. If things @@ -437,7 +461,7 @@ static void psi_avgs_work(struct work_struct *work) if (now >= group->avg_next_update) group->avg_next_update = update_averages(group, now); - if (nonidle) { + if (changed_states & PSI_STATE_RESCHEDULE) { schedule_delayed_work(dwork, nsecs_to_jiffies( group->avg_next_update - now) + 1); }