Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2108600lqt; Mon, 22 Apr 2024 01:23:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU8/l2h+ebUe3dFJ/t0Qc3IRkeXiwQbF213UP9nYJXmiFgUOflrXU+U4OprQHpJF803lXcw5kd+AZasDbs4BuFEIxiWj+IDiaAT7BS51A== X-Google-Smtp-Source: AGHT+IHea2+y9axctcibgS6q+QtTvIsTJQJo5g143oWEzQ4tDUZSsu0HGUE7ACKVJ+FlcaqiFQup X-Received: by 2002:a17:906:78e:b0:a51:b1b1:b772 with SMTP id l14-20020a170906078e00b00a51b1b1b772mr5764182ejc.65.1713774196335; Mon, 22 Apr 2024 01:23:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713774196; cv=pass; d=google.com; s=arc-20160816; b=I1tiCTpVsmTeR+BLd69ye2Oc7MojN0fjIWcnW8WQgmTOQxrsGGQhSKvo2shuFltNBE zGdR9U3mzSiWklZ3wcEnb9raRYpq+5SR0E16a4Hm0N/H7wHQ3Jz+AehE2NoEWhW5/y71 XOeXB7Dh9YFfLUs+m6PmMl+IZ9nAjQKUc67D8m1ke91k0vZdiNZCI/f8ziV7vFekWfVa 61wu2UfVWF//op63T1z7IwZ0h6qdHcByfLX0kpo8i3k88cAn+UyYpGRDiVQI95AtRZWv C2jS9pHZWSk8dAr4R5sPVO6TbFrKUicfl42vs9HES79yZf2n8gir8sNb29Pd/2tNpIS2 Q6aw== 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=9DD1JOBzbGvWwJB06PqDxhxQSUz83SXpQvJkyWRAlH8=; fh=yrQbixctd40Cu2mJkMCvTwbUcTqDnLtTx4BMsLLp9E8=; b=0LaFOhFbqJr0CSZpckzb9kJEoJxyMsA/v3jd21+O5MWhj3GPAoXDSGYlvzK0TuRaAn ipcc23wSXo8/ux7fONJCpbR14g2QvPb7toS/8+F3gYpjqhdpBYcRohD1vCW7i185KpSW qEhldboKkRMDi0a7Wc6SrQFhEYT3KxKvIfhM48ZIcFKnM7lHmfBKPGN4vTDzucGTQCbU 1239QuFkCs04Pao/F0yEpPCS7VWAb/0q1npOBcUtwMSV0sVE/5/iMTxnJCHjrzdBnVw7 oZKrmqVIb9+XOEMWaV+mPmlx4+kxlzOH1Ja4GNUi97uiXDH+YrQCkpYyttNCVlHR+UFW GQag==; dara=google.com 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-152967-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152967-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id wn1-20020a170907068100b00a5555084d5bsi5135221ejb.623.2024.04.22.01.23.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 01:23:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-152967-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=unisoc.com); spf=pass (google.com: domain of linux-kernel+bounces-152967-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152967-linux.lists.archive=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 13BFB1F2128A for ; Mon, 22 Apr 2024 08:23:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7369650276; Mon, 22 Apr 2024 08:23:10 +0000 (UTC) Received: from SHSQR01.spreadtrum.com (unknown [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 6F92F4CB2E for ; Mon, 22 Apr 2024 08:23:04 +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=1713774190; cv=none; b=OQVsNp5p4YRqUt1C7MHMcOGwyRHy/BIzDf1LZ7HLm69IR+jWCBfi1C5eRsDdjsr8Yr9jUksr5r2F556ww6oC6yO+6sQ9u48ynfxBDNXBFj0W6gNohchNS/z1jSTsajc6HP32yJzQu0DkOIiqaG7mElv2zkYmiGTI8d6CMvGm34Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713774190; c=relaxed/simple; bh=fx9YuYE5tfNvbEhu+Kifp+zfFy5kCKUb/5dSgbVi/Bw=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=n4I1BxsjzY/Fw7nIJkpX+uDsPAGmPtcROJtjfWcSGTDvqzhXHHZQD/fGZDppJumzlAExPBoqbkqRJ7eaca2qPyuSgSdEDzv+0NNPi8YzrYt9OJds5e21gDfF++DF1Wk76FpkBCWM9zfOFQT7/1MmwrdwJ3b+zlsz1hv+g2NmwF4= 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 43M8MmmN085755; Mon, 22 Apr 2024 16:22:48 +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 4VNJ8b0qwzz2MxHx5; Mon, 22 Apr 2024 16:20:15 +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; Mon, 22 Apr 2024 16:22:42 +0800 From: Xuewen Yan To: , , , CC: , , , , , , , , , , Subject: [PATCH v2] sched/eevdf: Prevent vlag from going out of bounds when reweight_eevdf Date: Mon, 22 Apr 2024 16:22:38 +0800 Message-ID: <20240422082238.5784-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 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL:SHSQR01.spreadtrum.com 43M8MmmN085755 kernel encounters the following error when running workload: BUG: kernel NULL pointer dereference, address: 0000002c EIP: set_next_entity (fair.c:?) which was caused by NULL pointer returned by pick_eevdf(). Further investigation has shown that, the entity_eligible() has an false-negative issue when the entity's vruntime is far behind the cfs_rq.min_vruntime that, the (vruntime - cfs_rq->min_vruntime) * load caused a s64 overflow, thus every entity on the rb-tree is not eligible, which results in a NULL candidate. The reason why entity's vruntime is far behind the cfs_rq.min_vruntime is because during a on_rq task group's update_cfs_group()->reweight_eevdf(), there is no limit on the new entity's vlag. If the new weight is much smaller than the old one, vlag = div_s64(vlag * old_weight, weight) generates a huge vlag, and results in very small(negative) vruntime. Thus limit the range of vlag accordingly. Reported-by: Sergei Trofimovich Closes: https://lore.kernel.org/all/ZhuYyrh3mweP_Kd8@nz.home/ Reported-by: Igor Raits Closes: https://lore.kernel.org/all/CA+9S74ih+45M_2TPUY_mPPVDhNvyYfy1J1ftSix+KjiTVxg8nw@mail.gmail.com/ Reported-by: Breno Leitao Reported-by: kernel test robot Closes: https://lore.kernel.org/lkml/202401301012.2ed95df0-oliver.sang@intel.com/ Reported-by: Yujie Liu Signed-off-by: Xuewen Yan --- changes of v2: -add reported-by (suggested by ) -remork the changelog () -remove the judgement of fork (Peter) -remove the !on_rq case. (Peter) --- Previous discussion link: https://lore.kernel.org/lkml/20240226082349.302363-1-yu.c.chen@intel.com/ https://lore.kernel.org/all/20240130080643.1828-1-xuewen.yan@unisoc.com/ --- --- kernel/sched/fair.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 03be0d1330a6..64826f406d6d 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -696,15 +696,21 @@ u64 avg_vruntime(struct cfs_rq *cfs_rq) * * XXX could add max_slice to the augmented data to track this. */ -static void update_entity_lag(struct cfs_rq *cfs_rq, struct sched_entity *se) +static s64 entity_lag(u64 avruntime, struct sched_entity *se) { - s64 lag, limit; + s64 vlag, limit; + + vlag = avruntime - se->vruntime; + limit = calc_delta_fair(max_t(u64, 2*se->slice, TICK_NSEC), se); + + return clamp(vlag, -limit, limit); +} +static void update_entity_lag(struct cfs_rq *cfs_rq, struct sched_entity *se) +{ 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 = entity_lag(avg_vruntime(cfs_rq), se); } /* @@ -3761,7 +3767,7 @@ static void reweight_eevdf(struct cfs_rq *cfs_rq, struct sched_entity *se, * = V - vl' */ if (avruntime != se->vruntime) { - vlag = (s64)(avruntime - se->vruntime); + vlag = entity_lag(avruntime, se); vlag = div_s64(vlag * old_weight, weight); se->vruntime = avruntime - vlag; } -- 2.25.1