Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp2012309lqb; Mon, 27 May 2024 05:16:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVahMuz3hIBfZsx2MH8ENPPE4JRIu+CprNtbIOtyCHH2uY/icVttHId0+pc1/YxhCMLyVgH3+b0VXJulnLLONmKu/XjZ6Gm9S12m5y1kA== X-Google-Smtp-Source: AGHT+IFDXbnPZAHbw0vt2mRHEEKRrgq6jsurccY4lEuA1J0w5O2Z1YRDjwwpflwuypCfwRgWrF7p X-Received: by 2002:a05:6a20:96d3:b0:1ad:3d93:b71e with SMTP id adf61e73a8af0-1b212f873b6mr8163725637.59.1716812207234; Mon, 27 May 2024 05:16:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716812207; cv=pass; d=google.com; s=arc-20160816; b=QB41O+RIHMtM17SWGWejJfCyAO9v8nh5w+pQgjYuwxZlHKSQVan1KMSihydrqAQIp9 ir5uZkv9AYIwMArym1zEJx0FBTpeZ48+acUA4VgE6fiFpFgP+SJcEZ/zgNIP9RqIko9i T6q+ilDA8bZrhU5IhKKbevv6746QrcRd/K1jfbCorA1Xd5YiyHceZi4KZ9dz47UbaU/K rZg+IV4BRntNgu6lPlR8daO477Hs9pujlm+g9cNZmtpA9FzIweQfsklwo2mOtKtslSd2 FJjq+nEFyURPyninUofhkCiusMRm57OupGnz8LEkEim7+OTwENo0ZZgGy6sKPqdgg6S0 dQEw== 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:dkim-signature; bh=xkIbIinkHwEQ58DQDG8xfVXjlxknSTVYp+pyo3BD3cI=; fh=HU7ivrnk+91Ajh81gryUDbZXf63BwQAlqSQDHkrjwDI=; b=zsJhbb5PM1oCQiAOZXjzdIVM+qxs78+eeJqbFSPOEmHFdUo28rGfGM4+9N3l4siYlQ hGUyHILM13EyTLEpW/a4HzYTn7l073ys5riVMLyCi8WUvlWNC8GdyPczN44sgXJEyMKb jpPZWjUSPeLntNyjcqFRk0dGhBA19P8RA968PpZ5LC5+A90bTfSlrTC1Bi8AoatDkSZC dcJ/qMjTfD4tBmbc2LiXEgOAOI9UGwkzKo8MPm89V1ibCdGaCcPpP6KTfxX3cZadEFYC FNQg51m/JpzC/kIeDV0pNdmclaTxof6/Rc6OBejXrtVy7vRLoe+eSjsdluFP1/nsO3tV olgQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Cmke/4R/"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-190535-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190535-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 41be03b00d2f7-6822789025bsi6357338a12.419.2024.05.27.05.16.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 05:16:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-190535-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Cmke/4R/"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-190535-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190535-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id E381BB20CBA for ; Mon, 27 May 2024 12:08:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1851915ECDE; Mon, 27 May 2024 12:07:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Cmke/4R/" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 1A8C015E5A1 for ; Mon, 27 May 2024 12:07:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716811668; cv=none; b=Uy76pPUg/6ZNXyU4HzXXBUdDRX6IeB94dqiHKrpQIfSwZOot4XmG1YCyhULvahhD8hMrUJFgqjBSdFs6jfkY0BlGWrfCbbcsyT6MxNjK7VPxPNX5Nwcz/xx/incRY+9lcujBEhi2ZHG90RaMvkJVKbx8Di9E80qTnyAJcWxU0Iw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716811668; c=relaxed/simple; bh=TxEOnzOIGujg32HlOqWh78fNUd3cZ9WBcF3eGv0gPuQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=etQIEkDvSPPIEBfUWcJp8iF8gl6Y1cNfdmM3u3EgvmB0SvmXdIzSQOkshz3qlhruIJCk32Dndahhsw0RGSJL0B0Z3mkSOgAYxjnUSzaNVMmiHJ2Jp9T5I5oFwIes/VH4rZNicwQeIVEynw5ILzZei2cTZ5mNu5iH32rwUewl+Rg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Cmke/4R/; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7341C32781; Mon, 27 May 2024 12:07:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716811667; bh=TxEOnzOIGujg32HlOqWh78fNUd3cZ9WBcF3eGv0gPuQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cmke/4R/wN8jjP+oMQSZTK1Q78xdhT484z6rsM/9wIYAqu//fb2XsPp+n+Et5sJ1x aMb+kX0Nlx6Wd6FPMZy6hD7NVjg93ToeFZa3kPhRCVwMRclpAUd365J6h3pX7ESwIt oIDSTt2ektVR4elbkDHd+tWvJwf0U8zHihP7AJOS4PK1YbRtgpuGXtRTopthf9G3zb SgMBbOY2PTrc+jP16Ga5v93UXL231vGkwlFteLqx/DhwiWvbw+XBAZDCMZkU7piVqp j1ezi2z8aU8836HYIY/4XftiJAhQeuykem4wkE3E374ixLBvUGcf9vKlSezZm/Syjm MnpeJ1dFkht6Q== From: Daniel Bristot de Oliveira To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot Cc: Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , linux-kernel@vger.kernel.org, Luca Abeni , Tommaso Cucinotta , Thomas Gleixner , Joel Fernandes , Vineeth Pillai , Shuah Khan , bristot@kernel.org, Phil Auld , Suleiman Souhlal , Youssef Esmat Subject: [PATCH V7 8/9] sched/core: Fix picking of tasks for core scheduling with DL server Date: Mon, 27 May 2024 14:06:54 +0200 Message-ID: X-Mailer: git-send-email 2.45.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 From: "Joel Fernandes (Google)" * Use simple CFS pick_task for DL pick_task DL server's pick_task calls CFS's pick_next_task_fair(), this is wrong because core scheduling's pick_task only calls CFS's pick_task() for evaluation / checking of the CFS task (comparing across CPUs), not for actually affirmatively picking the next task. This causes RB tree corruption issues in CFS that were found by syzbot. * Make pick_task_fair clear DL server A DL task pick might set ->dl_server, but it is possible the task will never run (say the other HT has a stop task). If the CFS task is picked in the future directly (say without DL server), ->dl_server will be set. So clear it in pick_task_fair(). This fixes the KASAN issue reported by syzbot in set_next_entity(). (DL refactoring suggestions by Vineeth Pillai). Reviewed-by: Vineeth Pillai Reported-by: Suleiman Souhlal Signed-off-by: Joel Fernandes (Google) Signed-off-by: Daniel Bristot de Oliveira --- include/linux/sched.h | 3 ++- kernel/sched/deadline.c | 27 ++++++++++++++++++++++----- kernel/sched/fair.c | 23 +++++++++++++++++++++-- kernel/sched/sched.h | 3 ++- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 611771fec4df..eb8f8b7929c8 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -684,7 +684,8 @@ struct sched_dl_entity { */ struct rq *rq; dl_server_has_tasks_f server_has_tasks; - dl_server_pick_f server_pick; + dl_server_pick_f server_pick_next; + dl_server_pick_f server_pick_task; #ifdef CONFIG_RT_MUTEXES /* diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index f8afe0a69c1e..0dbb42cf7fe6 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -1664,11 +1664,13 @@ void dl_server_stop(struct sched_dl_entity *dl_se) void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq, dl_server_has_tasks_f has_tasks, - dl_server_pick_f pick) + dl_server_pick_f pick_next, + dl_server_pick_f pick_task) { dl_se->rq = rq; dl_se->server_has_tasks = has_tasks; - dl_se->server_pick = pick; + dl_se->server_pick_next = pick_next; + dl_se->server_pick_task = pick_task; } void __dl_server_attach_root(struct sched_dl_entity *dl_se, struct rq *rq) @@ -2394,7 +2396,12 @@ static struct sched_dl_entity *pick_next_dl_entity(struct dl_rq *dl_rq) return __node_2_dle(left); } -static struct task_struct *pick_task_dl(struct rq *rq) +/* + * __pick_next_task_dl - Helper to pick the next -deadline task to run. + * @rq: The runqueue to pick the next task from. + * @peek: If true, just peek at the next task. Only relevant for dlserver. + */ +static struct task_struct *__pick_next_task_dl(struct rq *rq, bool peek) { struct sched_dl_entity *dl_se; struct dl_rq *dl_rq = &rq->dl; @@ -2408,7 +2415,10 @@ static struct task_struct *pick_task_dl(struct rq *rq) WARN_ON_ONCE(!dl_se); if (dl_server(dl_se)) { - p = dl_se->server_pick(dl_se); + if (IS_ENABLED(CONFIG_SMP) && peek) + p = dl_se->server_pick_task(dl_se); + else + p = dl_se->server_pick_next(dl_se); if (!p) { WARN_ON_ONCE(1); dl_se->dl_yielded = 1; @@ -2423,11 +2433,18 @@ static struct task_struct *pick_task_dl(struct rq *rq) return p; } +#ifdef CONFIG_SMP +static struct task_struct *pick_task_dl(struct rq *rq) +{ + return __pick_next_task_dl(rq, true); +} +#endif + static struct task_struct *pick_next_task_dl(struct rq *rq) { struct task_struct *p; - p = pick_task_dl(rq); + p = __pick_next_task_dl(rq, false); if (!p) return p; diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 20e8b02c5cb3..14ec002bb4f9 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -8484,6 +8484,14 @@ static struct task_struct *pick_task_fair(struct rq *rq) cfs_rq = group_cfs_rq(se); } while (cfs_rq); + /* + * This can be called from directly from CFS's ->pick_task() or indirectly + * from DL's ->pick_task when fair server is enabled. In the indirect case, + * DL will set ->dl_server just after this function is called, so its Ok to + * clear. In the direct case, we are picking directly so we must clear it. + */ + task_of(se)->dl_server = NULL; + return task_of(se); } #endif @@ -8643,7 +8651,16 @@ static bool fair_server_has_tasks(struct sched_dl_entity *dl_se) return !!dl_se->rq->cfs.nr_running; } -static struct task_struct *fair_server_pick(struct sched_dl_entity *dl_se) +static struct task_struct *fair_server_pick_task(struct sched_dl_entity *dl_se) +{ +#ifdef CONFIG_SMP + return pick_task_fair(dl_se->rq); +#else + return NULL; +#endif +} + +static struct task_struct *fair_server_pick_next(struct sched_dl_entity *dl_se) { return pick_next_task_fair(dl_se->rq, NULL, NULL); } @@ -8654,7 +8671,9 @@ void fair_server_init(struct rq *rq) init_dl_entity(dl_se); - dl_server_init(dl_se, rq, fair_server_has_tasks, fair_server_pick); + dl_server_init(dl_se, rq, fair_server_has_tasks, fair_server_pick_next, + fair_server_pick_task); + } /* diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index ed7be7b085af..3b8684b5ec8e 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -354,7 +354,8 @@ extern void dl_server_start(struct sched_dl_entity *dl_se); extern void dl_server_stop(struct sched_dl_entity *dl_se); extern void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq, dl_server_has_tasks_f has_tasks, - dl_server_pick_f pick); + dl_server_pick_f pick_next, + dl_server_pick_f pick_task); extern void dl_server_update_idle_time(struct rq *rq, struct task_struct *p); -- 2.45.1