From patchwork Fri Oct 28 09:33:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Guittot X-Patchwork-Id: 12182 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp725633wru; Fri, 28 Oct 2022 02:45:42 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4HXDBN8L529LbGnyLYYbOxJe6NSEpW6zWQY4xL2YlTJ5q6QOFxy40fq/e3rnyOisXPt/v+ X-Received: by 2002:a05:6402:3596:b0:462:6b69:873a with SMTP id y22-20020a056402359600b004626b69873amr11387475edc.374.1666950342162; Fri, 28 Oct 2022 02:45:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666950342; cv=none; d=google.com; s=arc-20160816; b=aQoY3H5teGBRC+1otRVxxF7PpNMbLhlSZYZuCM1FCRjAtLM73MYCNfCzC/Curz3QMn TiRhIBySN920x7jjbgJ5MurzmUpAj8nuhjbw8KcnByGbDDv4n+Hw83StOvw7gJ8VRf20 UR6hZAUWLKxhYK7PwEE2iAtHevaeyEVV7ZceHM9TUNp3X7LXsxsLDesiHNWGaEiX5CLr 4NAXp4zL0mHN5xeSb8t3dwugYJ4TvKmAzCv1af+uLAYmA9RHFqx3/hpV+8IN8tGKtZYP h1Sm07Q+uIfqYGukjCVmwGia7jqbEJnRwn7fD8FWWDYELO+svuwSGFbNeEWBYhyw9Tiq Ozzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=FulDXK+MjqUjf+jM0x0PxczJh4rRajiCQKKkLNt7L9g=; b=Sx47RyIrzuUlKgOxWkPoC4Z3iX9utfKUjQXfD/ikHyMecmrtwUZLR+PpXwFFAFyW+s +f0XwdrEux3M89bvjlaI3FSF2PInYiOHYEJuDEU+egtZASYGE6c5X/qWLcwosjOLvxN7 wmyejTgyGI8Jiz3jGIFOn8Q+Hm9JF2xt1VtjdOYZlQBjua8QBjb2aHKZ+Vz5/Y+3u4jz O/lz/SAi+5i4UtInknJ6PW6BLvOl6zilMQLrJQWBZqZNU2hEDHoyznZrpZ0EpS4zKVGZ lodG8dm6ATJ/CU13VI6K0AtofWkn9ng924w+Cvk2DsLr9OE84VO8ktXcVc4fHqfppiSz r9OQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SdbnTsaQ; 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=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id tb21-20020a1709078b9500b0078decbc3f73si3943464ejc.460.2022.10.28.02.45.18; Fri, 28 Oct 2022 02:45:42 -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=@linaro.org header.s=google header.b=SdbnTsaQ; 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=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230122AbiJ1JfI (ORCPT + 99 others); Fri, 28 Oct 2022 05:35:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229987AbiJ1JfB (ORCPT ); Fri, 28 Oct 2022 05:35:01 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE3ED4A118 for ; Fri, 28 Oct 2022 02:34:57 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id bg9-20020a05600c3c8900b003bf249616b0so3297960wmb.3 for ; Fri, 28 Oct 2022 02:34:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=FulDXK+MjqUjf+jM0x0PxczJh4rRajiCQKKkLNt7L9g=; b=SdbnTsaQh6LgyAxdqEqLmRwb7PI1bfyTiDndVDE72wlnq6+58oWdsYr/hY3s8l5lYX EF1dMaXFZkIb8+NbZYI6x1M/BDg3OS82tQDy20B3C7rV7IPrDBDiXr3saKP4YhK1zBDS gG8MuNuwR61tpEVH9PkjFnYQpRmLJkT6tHusBxPqn03r3/lgFyxWSmY3O8V0Hsij/6Iv fIsEmmeY/cTWIywK80i2LlMo0ya/P4flmrHe3BLgc4nM/N3z3m64uYomKU6Df9D6OQbX NE8uYeJVTrXoCb5x7pc5So4f5g5eT7zkrLXBO0y/2/cB2g98SdxvHLl7Z8E0L/MXr7El snSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=FulDXK+MjqUjf+jM0x0PxczJh4rRajiCQKKkLNt7L9g=; b=GjzdNS88WeooUq2gTeLoZ+WQXVrPTiz94m6xh0Q6hv0RFDBj2WKj3oP9HGJRDwgywC E/U1rNKq67u1KTAlmyUUoY/UK9FyfIohvHTl2FJEOyREiOmoRsP0GVNgcmOr1zS2qF2F 1a/YsW3JJUhbJoc+FWwPfZuf6sIySGkqK5apHbeWLzd74b/Dka2r9PI70NOZ+v+iR7ct gRPnu+FhadpCw/SDp4nHWgYJjLwFaYoLZcBsd1T6WdC9kMmnGwnhxbo7yQyZ4uZMYKsd p1E5+llP2+A1tZLGp3PNMKpWP5VSAOToCa5XE0d/qiWCp0OhfLPCGa0eXUBqRFM3D6gE VTbQ== X-Gm-Message-State: ACrzQf2PGeYIUwfDq3nxK/PJnwt+psmO+qyH1GTwFQlBTlztFQRMcxaf M6pVTP2RzGR9x5p/xG7y/OnwgQ== X-Received: by 2002:a1c:7405:0:b0:3cf:5d41:be8b with SMTP id p5-20020a1c7405000000b003cf5d41be8bmr1730022wmc.1.1666949696259; Fri, 28 Oct 2022 02:34:56 -0700 (PDT) Received: from localhost.localdomain ([2a01:e0a:f:6020:c12b:b448:f0a9:83ef]) by smtp.gmail.com with ESMTPSA id k3-20020a05600c1c8300b003c6b7f5567csm10909426wms.0.2022.10.28.02.34.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Oct 2022 02:34:55 -0700 (PDT) From: Vincent Guittot To: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, linux-kernel@vger.kernel.org, parth@linux.ibm.com Cc: qais.yousef@arm.com, chris.hyser@oracle.com, patrick.bellasi@matbug.net, David.Laight@aculab.com, pjt@google.com, pavel@ucw.cz, tj@kernel.org, qperret@google.com, tim.c.chen@linux.intel.com, joshdon@google.com, timj@gnu.org, kprateek.nayak@amd.com, yu.c.chen@intel.com, youssefesmat@chromium.org, joel@joelfernandes.org, Vincent Guittot Subject: [PATCH v7 1/9] sched/fair: fix unfairness at wakeup Date: Fri, 28 Oct 2022 11:33:55 +0200 Message-Id: <20221028093403.6673-2-vincent.guittot@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221028093403.6673-1-vincent.guittot@linaro.org> References: <20221028093403.6673-1-vincent.guittot@linaro.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DIET_1,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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?1747924122153555318?= X-GMAIL-MSGID: =?utf-8?q?1747924122153555318?= At wake up, the vruntime of a task is updated to not be more older than a sched_latency period behind the min_vruntime. This prevents long sleeping task to get unlimited credit at wakeup. Such waking task should preempt current one to use its CPU bandwidth but wakeup_gran() can be larger than sched_latency, filter out the wakeup preemption and as a results steals some CPU bandwidth to the waking task. Make sure that a task, which vruntime has been capped, will preempt current task and use its CPU bandwidth even if wakeup_gran() is in the same range as sched_latency. If the waking task failed to preempt current it could to wait up to sysctl_sched_min_granularity before preempting it during next tick. Strictly speaking, we should use cfs->min_vruntime instead of curr->vruntime but it doesn't worth the additional overhead and complexity as the vruntime of current should be close to min_vruntime if not equal. Signed-off-by: Vincent Guittot --- kernel/sched/fair.c | 46 ++++++++++++++++++++------------------------ kernel/sched/sched.h | 30 ++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 5ffec4370602..eb04c83112a0 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -4345,33 +4345,17 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) { u64 vruntime = cfs_rq->min_vruntime; - /* - * The 'current' period is already promised to the current tasks, - * however the extra weight of the new task will slow them down a - * little, place the new task so that it fits in the slot that - * stays open at the end. - */ - if (initial && sched_feat(START_DEBIT)) - vruntime += sched_vslice(cfs_rq, se); - - /* sleeps up to a single latency don't count. */ - if (!initial) { - unsigned long thresh; - - if (se_is_idle(se)) - thresh = sysctl_sched_min_granularity; - else - thresh = sysctl_sched_latency; - + if (!initial) + /* sleeps up to a single latency don't count. */ + vruntime -= get_sched_latency(se_is_idle(se)); + else if (sched_feat(START_DEBIT)) /* - * Halve their sleep time's effect, to allow - * for a gentler effect of sleepers: + * The 'current' period is already promised to the current tasks, + * however the extra weight of the new task will slow them down a + * little, place the new task so that it fits in the slot that + * stays open at the end. */ - if (sched_feat(GENTLE_FAIR_SLEEPERS)) - thresh >>= 1; - - vruntime -= thresh; - } + vruntime += sched_vslice(cfs_rq, se); /* ensure we never gain time by being placed backwards. */ se->vruntime = max_vruntime(se->vruntime, vruntime); @@ -7187,6 +7171,18 @@ wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) return -1; gran = wakeup_gran(se); + + /* + * At wake up, the vruntime of a task is capped to not be older than + * a sched_latency period compared to min_vruntime. This prevents long + * sleeping task to get unlimited credit at wakeup. Such waking up task + * has to preempt current in order to not lose its share of CPU + * bandwidth but wakeup_gran() can become higher than scheduling period + * for low priority task. Make sure that long sleeping task will get a + * chance to preempt current. + */ + gran = min_t(s64, gran, get_latency_max()); + if (vdiff > gran) return 1; diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 1fc198be1ffd..14879d429919 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -2432,9 +2432,9 @@ extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags); extern const_debug unsigned int sysctl_sched_nr_migrate; extern const_debug unsigned int sysctl_sched_migration_cost; -#ifdef CONFIG_SCHED_DEBUG extern unsigned int sysctl_sched_latency; extern unsigned int sysctl_sched_min_granularity; +#ifdef CONFIG_SCHED_DEBUG extern unsigned int sysctl_sched_idle_min_granularity; extern unsigned int sysctl_sched_wakeup_granularity; extern int sysctl_resched_latency_warn_ms; @@ -2448,6 +2448,34 @@ extern unsigned int sysctl_numa_balancing_scan_period_max; extern unsigned int sysctl_numa_balancing_scan_size; #endif +static inline unsigned long get_sched_latency(bool idle) +{ + unsigned long thresh; + + if (idle) + thresh = sysctl_sched_min_granularity; + else + thresh = sysctl_sched_latency; + + /* + * Halve their sleep time's effect, to allow + * for a gentler effect of sleepers: + */ + if (sched_feat(GENTLE_FAIR_SLEEPERS)) + thresh >>= 1; + + return thresh; +} + +static inline unsigned long get_latency_max(void) +{ + unsigned long thresh = get_sched_latency(false); + + thresh -= sysctl_sched_min_granularity; + + return thresh; +} + #ifdef CONFIG_SCHED_HRTICK /*