From patchwork Tue Jan 30 08:06:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xuewen Yan X-Patchwork-Id: 193875 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1066393dyb; Tue, 30 Jan 2024 00:07:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IEJpYTLyPHdwJs204RxqlCbCDDl00FrvZLxe5Jryr+FKcLfDH2lkUuSZQC5Vsf4y3vCO7eQ X-Received: by 2002:a17:906:48cb:b0:a36:33ef:cff6 with SMTP id d11-20020a17090648cb00b00a3633efcff6mr303771ejt.54.1706602068697; Tue, 30 Jan 2024 00:07:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706602068; cv=pass; d=google.com; s=arc-20160816; b=V0sewgyQG1eZierx2nzsNyX7tyfxqYu8FEoAlK7OxS2H123hGISIWFpWoxcUxcRWcZ i7IRIGqvZYlD5bW9cDBRHOHeC41aa+d5dh+J5rgzdMFIxuA8OXDjG3VFHQumiZ1YKvBQ iCqT3tHEDsR7jXwiSGURnD05gZyyYT+uJ+z1Nx0MLyr4wJK7+Qes49abUd+MMNiVWhNQ auGg5snmfCcXa7TzRMnGcwDKflCrj/IAz9A2NMuC0+8Vv8CU2lTs4oFKSBPin3n02ZFH iutP23Vd8G6JSYVS1kpC5RYuSsvopiXIIT/4eXM+by7WuMN1/n3Ewuo7rAtibqzw0GrG zPhg== 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:message-id:date:subject:cc:to :from; bh=j+OKFzQ+3gTcqo+xXsieYeSO/+JqJAiUlRcZjibBn9E=; fh=/8IPIRTyzGB8OjLmUc+yPSBIMOsvC3i9WUkjsYf748c=; b=vjWxJ5nXtXT1df1oW3F9dLgAmZxueE/iRpdwJgL72yaULk69km7J3LBEQDfKvVI2yS 9v6gvDWYPhzeh+BjzfRBJnbm4ytBER+rJLb9q+Ktz4qaO8o5MHY9kd5fF5nGQYtrzI3C l+NggJsD552SxcVkLwl875KdEEP5AAjchKThVWuJEAkBIXivtq4Zh7wMSSjGipDNdFcz RPPJsy+ZhPtZceXj+cEV5J9xI35KlzdHKDlqu1n9VJDB5P6xhPH5n7I0Ap4AbqdD3eyA Jub4gwRz+p0iaKRXClZJmuFv1iP3poWJeijW9HSY/1jGmZRnGCcRJnogugbm5h/+EKe6 RSsA== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=unisoc.com); spf=pass (google.com: domain of linux-kernel+bounces-44088-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44088-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id u19-20020a17090657d300b00a36134c3e03si568154ejr.770.2024.01.30.00.07.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 00:07:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-44088-ouuuleilei=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=unisoc.com); spf=pass (google.com: domain of linux-kernel+bounces-44088-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44088-ouuuleilei=gmail.com@vger.kernel.org" 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 50EFD1F2857E for ; Tue, 30 Jan 2024 08:07:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 901E956469; Tue, 30 Jan 2024 08:07:34 +0000 (UTC) Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7931F55C02 for ; Tue, 30 Jan 2024 08:07:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=222.66.158.135 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706602053; cv=none; b=tlzoL+r/YRt0GfD0PGJ0ueZPUlU0LmTZN35wfHYUnGyaFtraMyHEHX43CLA2kfZidRbaxqfAKXb73320B+guf213/6x7rQfJtqMYMuLhZ9ZTIE/UiHCPU7FK3LcKCZCHupdFKu9AF39H+YylAMH7+cflHI+8Aior9kSdIT/mE4A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706602053; c=relaxed/simple; bh=lixRsKqZbJZXIkGyOzAqZUfXdnrx6g0JD9mQ7KwNOJg=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=CV9kgqBu2J/yTtdFBFw/PzI4gR89FkN3CEKvJu51M4OQBN1W33Wbz1HhT9CjVH7gJsW5mb0Q7mneHMTp9orzXLXOzF3evlVJ+Hk87CI3k4F+qMbAzF6NGtEPq4ZRog1UcwaQFGaK1dKC1wIlCA3TUU/8Xr9IlupsR7PVHBzZywg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; arc=none smtp.client-ip=222.66.158.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 40U8796h038585; Tue, 30 Jan 2024 16:07:09 +0800 (+08) (envelope-from Xuewen.Yan@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4TPHd15m7Dz2S73y6; Tue, 30 Jan 2024 15:59:33 +0800 (CST) Received: from BJ10918NBW01.spreadtrum.com (10.0.73.73) by BJMBX01.spreadtrum.com (10.0.64.7) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Tue, 30 Jan 2024 16:07:06 +0800 From: Xuewen Yan To: , , , CC: , , , , , , , , , Subject: [PATCH] sched/eevdf: Prevent vlag from exceeding the limit value Date: Tue, 30 Jan 2024 16:06:43 +0800 Message-ID: <20240130080643.1828-1-xuewen.yan@unisoc.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SHCAS01.spreadtrum.com (10.0.1.201) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL: SHSQR01.spreadtrum.com 40U8796h038585 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789501970856992061 X-GMAIL-MSGID: 1789501970856992061 There are some scenarios here that will cause vlag to exceed eevdf's limit. 1. In set_next_entity, it will stash a copy of deadline at the point of pick in vlag, and if the task fork child, the child's vlag would inherit parent's vlag, as a result, the child's vlag is equal to the deadline. And then, when place_entity, because the se's vlag is wrong, it would cause vruntime and deadline update error. 2. When reweight_entity, the vlag would be recalculated, because the new weights are uncertain, and it may cause the vlag to exceed eevdf's limit. In order to ensure that vlag does not exceed the limit, separate the calculation of limit from the update_entity_lag and create a new func limit_entity_lag. When the vlag is updated, use this func to prevent vlag from exceeding the limit. And add check whether the se is forked in place_entity, and calc the curr's vlag to update the se's vlag. Signed-off-by: Xuewen Yan --- kernel/sched/fair.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 533547e3c90a..3fc99b4b8b41 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -696,15 +696,22 @@ u64 avg_vruntime(struct cfs_rq *cfs_rq) * * XXX could add max_slice to the augmented data to track this. */ +static s64 limit_entity_lag(struct sched_entity *se, s64 lag) +{ + s64 limit; + + limit = calc_delta_fair(max_t(u64, 2*se->slice, TICK_NSEC), se); + return clamp(lag, -limit, limit); +} + static void update_entity_lag(struct cfs_rq *cfs_rq, struct sched_entity *se) { - s64 lag, limit; + s64 lag; SCHED_WARN_ON(!se->on_rq); lag = avg_vruntime(cfs_rq) - se->vruntime; - limit = calc_delta_fair(max_t(u64, 2*se->slice, TICK_NSEC), se); - se->vlag = clamp(lag, -limit, limit); + se->vlag = limit_entity_lag(se, lag); } /* @@ -3757,6 +3764,7 @@ static void reweight_eevdf(struct cfs_rq *cfs_rq, struct sched_entity *se, if (avruntime != se->vruntime) { vlag = (s64)(avruntime - se->vruntime); vlag = div_s64(vlag * old_weight, weight); + vlag = limit_entity_lag(se, vlag); se->vruntime = avruntime - vlag; } @@ -3804,6 +3812,9 @@ static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, update_load_set(&se->load, weight); + if (!se->on_rq) + se-vlag = limit_entity_lag(se, se->vlag); + #ifdef CONFIG_SMP do { u32 divider = get_pelt_divider(&se->avg); @@ -5177,6 +5188,16 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) lag = se->vlag; + /* When fork, the child would inherit parent's vlag, + * but parent's vlag may be euqual to it's deadline. + */ + if (flag & ENQUEUE_INITIAL) { + if (curr) + lag = vruntime - curr->vruntime; + + lag = limit_entity_lag(se, lag); + se->vlag = lag; + } /* * If we want to place a task and preserve lag, we have to * consider the effect of the new entity on the weighted @@ -5237,6 +5258,7 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) if (WARN_ON_ONCE(!load)) load = 1; lag = div_s64(lag, load); + lag = limit_entity_lag(se, lag); } se->vruntime = vruntime - lag;