Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp493036lqh; Tue, 7 May 2024 05:52:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWygB0rG+3refGtD3Q061daO0/zKmdKRpGrS9XtKnAFZtrDrCSt/qpaR1KTMEnwRsiEE6j1fWmRQXKDzRX8wU+SeOR4tedqvjIpK8oJEA== X-Google-Smtp-Source: AGHT+IGYnpVDFwynO4geg/m6XGva3htyWvIWvOSDOoZNYGCVU7E0oqpoBPjDYbsHbGXZGd30hWtE X-Received: by 2002:ac2:518f:0:b0:519:611f:df49 with SMTP id u15-20020ac2518f000000b00519611fdf49mr8969357lfi.69.1715086356977; Tue, 07 May 2024 05:52:36 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715086356; cv=pass; d=google.com; s=arc-20160816; b=o0rLyp2xXDHtTBMZGiEcByQYELMKGQ32D7yAV0oNEy0WtMgs2rprYvjVQn/cZXj0qO OPMS84uQnBhJBJQxAqoZVIpOScIcjj54QwIc+yhUKBuQG4IQNALqj3GKDuOKF9mvFb9e QmMMBnBPUd6ciBVByr/4ufL7eIvpjBvfwwZ/xjWxYcqqQZcZ8jj8XR4LMfM+zi6vDOrK /KG6ld82+lQSOQYe28MIjcFwT6gGQ+RToKQnGdQRbDlGcDxARMRvcKH0xrsk54nbqRfV y360X/9y+4OJy0b1AEEU+DkdukOZks4DUHE7mvCu4IODdY9vG4NWItnqRik1st8kSjca enyg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=6mVZwEugILgcKbdxvJJup7xri+9zcLk/n6FQMUo3TaQ=; fh=0Icot6U8y/jzzh4C/0m1iuu/YoTW8eUo3TM7jO/4ktk=; b=xFQUfWU5YbNmBOs8uG+AxFD0ymFiS5JE/N+Ze5XtrJTs5UPZ62tetiXc85vW1T/4U6 rCfU8K0FPtVT9YydF9q1qsRkWKoDf6GngxDm5x8btFTpxtbW70ToYcmy/f3UDW0Dnj8y VLhun3UK83DvvIX29cs2/fWYDoBLQenQYtJmsgn5aC1JuWt4WQMbRiNZ+rUdFv2+2LdD DQgyoWyVH1F523kDJXVTWLcW3c92L026Qa+jTIZTIAgD4j+Q5OWFU6JpTlIgyZk0JjW7 wWqnLt5fPul+UBMwQJtE0SqIdC8Lk5vDPTWuZ6bzN3PCggJ1YfbCrWQhoNtWARbh93Wn UNTA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-171315-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171315-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id y6-20020a056402440600b005726dff91acsi5954385eda.56.2024.05.07.05.52.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 05:52:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-171315-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-171315-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171315-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 48A3E1F2341E for ; Tue, 7 May 2024 12:52:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6B1C915D5C8; Tue, 7 May 2024 12:51:03 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6826B15E5CC for ; Tue, 7 May 2024 12:51:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715086262; cv=none; b=jBAzLGJPjMmoS/sR51UDDn+O/u7iRv9dalT+w3QCeV4GUL/M9bmtNOVDgZY1f9WLnXtSnrXihXqd/dmfDoEhQuKeJBk1SX1o2QRqvOkeeOyn777dw5dVbx6lg6Lu71j52/YhdYNW/xFLpUqrlcrLVlyy01kzE1JOQdTjI2SsJFw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715086262; c=relaxed/simple; bh=kCOFTh2w1NJO+h53GHlkLLXq/I3SCktxk8/Z5w+DA04=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=i77hMn9ZR8OLmJ58b/sYVLzQLZ2lnngsJG69Lgt0e9gOcbZzYgaTFzOuCwtO3rpCEAoM8sSdaJ2tkfcd0ELfUNrhDPghX3TM/68Yui/2yyZhibTDSqOAiqsvqL7syLwZj/YhCexKDpftryfEC2nP0w8gXM4d7i2Xz1apFzcg1SE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D4AC31063; Tue, 7 May 2024 05:51:26 -0700 (PDT) Received: from e130256.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D1EF83F793; Tue, 7 May 2024 05:50:58 -0700 (PDT) From: Hongyan Xia To: Ingo Molnar , Peter Zijlstra , Vincent Guittot , Dietmar Eggemann , Juri Lelli , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider Cc: Qais Yousef , Morten Rasmussen , Lukasz Luba , Christian Loehle , pierre.gondois@arm.com, linux-kernel@vger.kernel.org, Hongyan Xia Subject: [RFC PATCH v3 6/6] Propagate negative bias Date: Tue, 7 May 2024 13:50:29 +0100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Negative bias is interesting, because dequeuing such a task will actually increase utilization. Solve by applying PELT decay to negative biases as well. This in fact can be implemented easily with some math tricks. Signed-off-by: Hongyan Xia --- kernel/sched/fair.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 0177d7e8f364..7259a61e9ae5 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -4863,6 +4863,45 @@ static inline unsigned long task_util_est_uclamp(struct task_struct *p) { return max(task_util_uclamp(p), _task_util_est_uclamp(p)); } + +/* + * Negative biases are tricky. If we remove them right away then dequeuing a + * uclamp_max task has the interesting effect that dequeuing results in a higher + * rq utilization. Solve this by applying PELT decay to the bias itself. + * + * Keeping track of a PELT-decayed negative bias is extra overhead. However, we + * observe this interesting math property, where y is the decay factor and p is + * the number of periods elapsed: + * + * util_new = util_old * y^p - neg_bias * y^p + * = (util_old - neg_bias) * y^p + * + * Therefore, we simply subtract the negative bias from util_avg the moment we + * dequeue, then the PELT signal itself is the total of util_avg and the decayed + * negative bias, and we no longer need to track the decayed bias separately. + */ +static void propagate_negative_bias(struct task_struct *p) +{ + if (task_util_bias(p) < 0 && !task_on_rq_migrating(p)) { + unsigned long neg_bias = -task_util_bias(p); + struct sched_entity *se = &p->se; + struct cfs_rq *cfs_rq; + + p->se.avg.util_avg_bias = 0; + + for_each_sched_entity(se) { + u32 divider, neg_sum; + + cfs_rq = cfs_rq_of(se); + divider = get_pelt_divider(&cfs_rq->avg); + neg_sum = neg_bias * divider; + sub_positive(&se->avg.util_avg, neg_bias); + sub_positive(&se->avg.util_sum, neg_sum); + sub_positive(&cfs_rq->avg.util_avg, neg_bias); + sub_positive(&cfs_rq->avg.util_sum, neg_sum); + } + } +} #else static inline long task_util_bias(struct task_struct *p) { @@ -4883,6 +4922,10 @@ static inline unsigned long task_util_est_uclamp(struct task_struct *p) { return task_util_est(p); } + +static void propagate_negative_bias(struct task_struct *p) +{ +} #endif static inline void util_est_enqueue(struct cfs_rq *cfs_rq, @@ -6844,6 +6887,7 @@ static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) /* At this point se is NULL and we are at root level*/ sub_nr_running(rq, 1); util_bias_dequeue(&rq->cfs.avg, p); + propagate_negative_bias(p); /* XXX: We should skip the update above and only do it once here. */ cpufreq_update_util(rq, 0); -- 2.34.1