Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp1288731rdb; Fri, 16 Feb 2024 10:43:28 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU9E5CTc4Gx7h0C5jKapMiF4GUw1sbx28m7n8cvMmgoMoOokZKuEmb9gPMS5QtsJMP2y3aGihkBY9i04J94W1NFEaYJzhw5xMOZF/9YvA== X-Google-Smtp-Source: AGHT+IETRWX8mU5dG1odEJfmwIkb/xIWn/NGKZlXOyjoF6HunTyIVrV1mwfrdWihIfhrCLuUQtPu X-Received: by 2002:a05:6a00:93a7:b0:6e0:4e0f:62a8 with SMTP id ka39-20020a056a0093a700b006e04e0f62a8mr12442081pfb.2.1708109008255; Fri, 16 Feb 2024 10:43:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708109008; cv=pass; d=google.com; s=arc-20160816; b=X5UuiIjS6OY50UG+QarQHQs3rT5dDYBcgjn+kGVxuoVqNYGjADHfx1nYMA/6KJ006c YZHtxTMGYfM1yuuSNzFBcXL1tLniqYSRJqEGPfEOOte4vwrGQj+kKaQH24TNOJf44/Tu ZmJa8SkjM9j8jVCYOWJF0KRQlBZ4rNNeF6aTdGSIde3DXAe7yO56iq2/MIGUz070fIMJ rZ2hm5tSS9l4OSZPBq8COMPnLvlo0T0coWfb+DJSmdlSX1qu1Gj/m3qL7c05jlT01dJk 2260hXjznNSTXgfSawydoaUF74+Gyd/Eywh5JvNx/GKYhg48JuYpTQU70j3Pqfjqedx4 oZtg== 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=SWiejWekyqz80XlkFsn0hVk2ugnWY0u297YrWVho63A=; fh=M2d0eEwkotGwrCl/QH/u/fnmeLJn9ZwWPiMveRgwy+8=; b=o8LWr4VGg/O6O6wLyPAmW/yZ+nh3pErFZcUWE4Sw9Br6/CMX3F/bBecIe5Qq3E8aZY hRpI/LIxQALliQc6S3cmeEuzw88avSUxf16UJXlBEjNRXwz322PSy1wxLLjwEuji0CwF hl4SysFhujj/uzQi4vwyL19kEwNu/W3xT3fA17IPbj227m318CQGy0gE7FpRn8wLn0jM X3/NBOxZkkrGYETeJUQcbU5qWjU7htkLCZ8KC/npf3EwwThDCW1bKDXBdjZCvQNPzB1+ qkTMH+xdtvNQFwRT1Gf8Jytw+OCykGA1U3T9G2vzpDKQnsQlgXS73e1iDC+9u2dBc5hk Wk5w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=CeyE2qJQ; arc=pass (i=1 spf=pass spfdomain=joelfernandes.org dkim=pass dkdomain=joelfernandes.org); spf=pass (google.com: domain of linux-kernel+bounces-69165-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69165-linux.lists.archive=gmail.com@vger.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 a63-20020a639042000000b005dc816b234esi258851pge.629.2024.02.16.10.43.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 10:43:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69165-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=@joelfernandes.org header.s=google header.b=CeyE2qJQ; arc=pass (i=1 spf=pass spfdomain=joelfernandes.org dkim=pass dkdomain=joelfernandes.org); spf=pass (google.com: domain of linux-kernel+bounces-69165-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69165-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 78C4BB20978 for ; Fri, 16 Feb 2024 18:33:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BE31E132472; Fri, 16 Feb 2024 18:31:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b="CeyE2qJQ" Received: from mail-qt1-f181.google.com (mail-qt1-f181.google.com [209.85.160.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 83F8A135A55 for ; Fri, 16 Feb 2024 18:31:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708108310; cv=none; b=b3nVVwMX2+GYVPkF5FFqXLJAlwRrLK3YUvgBTmTL9TdNe+zEIVXduqLRj8rs1iLxOw556KpXSiOldkfXNJOtOgq4U3+BPEw0aepbs9xGaRXkMzcQBBvJSeMb1x4wqTJU336EHwL8UYXCH4IdbTiVnIaW+2h+bdjMETRyQJHkgF8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708108310; c=relaxed/simple; bh=hNtzJxrNKpS3WmLYndaWMBO3so/VD/gQO0uqcJZ9FDc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Yteq4Rl/ecr1SEt0ocOq1Gmo3iNK79YkkxxjIpHnlPSj/RmxMNkHRBrFME/p8evDCGNg1TsIwpUMvLw+TPQCS8+NLzLOIMiVTUfT3th/hn59BLH1qVHkE2WrYVJFFC6leRS1h5zmpLeiARwNVrr57EjKlSRCl9qtY+meqPWsR3E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org; spf=pass smtp.mailfrom=joelfernandes.org; dkim=pass (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b=CeyE2qJQ; arc=none smtp.client-ip=209.85.160.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=joelfernandes.org Received: by mail-qt1-f181.google.com with SMTP id d75a77b69052e-42c758f075dso28644121cf.0 for ; Fri, 16 Feb 2024 10:31:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1708108305; x=1708713105; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SWiejWekyqz80XlkFsn0hVk2ugnWY0u297YrWVho63A=; b=CeyE2qJQQJZ35mXekLyB86cXhkXFSbDxRT9lZ9DQpr45yQ9v082goouHNxfg1foozC 6PB0fJ680jVN+s+lLFaJCZ/bRWFow2Ny9QFEHFdwyt61xfpDQ3DCoIvnqdRLe2EUQvr5 xVb1yt0CWobqdlWkhE/97mu1Dwj9co2TipWQA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708108305; x=1708713105; h=content-transfer-encoding:mime-version: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=SWiejWekyqz80XlkFsn0hVk2ugnWY0u297YrWVho63A=; b=b6l1gQsoQCB01lIoD4RwT41s611YxKpidN0rpk3BVIbKkjztVo1vB9VK7DQbt+CFvS pQF2yNpmD41AAa4c/K6mFs8h4aoHw3XJJm80jCmZsjDGcjExOstqbYIfgfyHYbBM4hWb qgMOzGmlmNT0ovV4w6VWW0CHKXDqVBSXe1GZY+JkTRrlKIXG8Xgv4p1DZHTojh2L4E9T nLQeF1p6Aq53YD6Fu05qZlkd6lS10DMzBDk6lrNxYtHJm17IjY+HRGCTrexIR3LZBW7e V0UphFHDlZ54aKTb9av74TSO5QORpDLhx+3OUmWUNV8EB59arLKiOPUOhek7IBJfs5V8 ffGQ== X-Gm-Message-State: AOJu0YxcI5fe0yGOWfrzpb40QEo/TU0iQhUyDWBRTKFzlrsvpulNE9Y9 dhemF/OoXVN1uaVVTIFgTESJsbyugTU3IZhzAxHg1JI0YYL1p4gCtTDuZLOo4EIO3KOLptLEv3t 6 X-Received: by 2002:a05:6214:3a8a:b0:68f:1102:680c with SMTP id nz10-20020a0562143a8a00b0068f1102680cmr11071161qvb.5.1708108305165; Fri, 16 Feb 2024 10:31:45 -0800 (PST) Received: from joelbox2.. (c-98-249-43-138.hsd1.va.comcast.net. [98.249.43.138]) by smtp.gmail.com with ESMTPSA id nd13-20020a056214420d00b0068cdadb5e7esm159722qvb.31.2024.02.16.10.31.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 10:31:44 -0800 (PST) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org, Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider Cc: Suleiman Souhlal , Youssef Esmat , David Vernet , Thomas Gleixner , "Paul E . McKenney" , joseph.salisbury@canonical.com, Luca Abeni , Tommaso Cucinotta , Vineeth Pillai , Shuah Khan , Phil Auld , "Joel Fernandes (Google)" Subject: [PATCH 04/10] sched/core: Fix picking of tasks for core scheduling with DL server Date: Fri, 16 Feb 2024 13:31:02 -0500 Message-Id: <20240216183108.1564958-5-joel@joelfernandes.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240216183108.1564958-1-joel@joelfernandes.org> References: <20240216183108.1564958-1-joel@joelfernandes.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit * 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) --- include/linux/sched.h | 3 ++- kernel/sched/deadline.c | 27 ++++++++++++++++++++++----- kernel/sched/fair.c | 22 ++++++++++++++++++++-- kernel/sched/sched.h | 3 ++- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index a1f918674383..e5ad1f232b35 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -672,7 +672,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 f5aaa3adac94..8fafe3f8b59c 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -1567,11 +1567,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; } int dl_server_apply_params(struct sched_dl_entity *dl_se, u64 runtime, u64 period, bool init) @@ -2271,7 +2273,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; @@ -2285,7 +2292,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; @@ -2300,11 +2310,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 b48287629610..9cc528a14001 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -8392,6 +8392,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 @@ -8551,7 +8559,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); } @@ -8561,7 +8578,8 @@ void fair_server_init(struct rq *rq) struct sched_dl_entity *dl_se = &rq->fair_server; 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 4d2c216049cb..bfb15037489c 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -338,7 +338,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 fair_server_init(struct rq *); extern int dl_server_apply_params(struct sched_dl_entity *dl_se, -- 2.34.1