Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp2369291rdb; Mon, 25 Dec 2023 07:46:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IHivQ3SMEYOOsSE0qXO4aH4xjsqSS/SNCPrDY1rd/BSWTnfRFJLpj/eygDPJgiVoHp4Fe4r X-Received: by 2002:a05:6a20:4f93:b0:195:18e6:d82c with SMTP id gh19-20020a056a204f9300b0019518e6d82cmr2356673pzb.96.1703519173972; Mon, 25 Dec 2023 07:46:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703519173; cv=none; d=google.com; s=arc-20160816; b=tZE52qYGuHbpEpTj2kIfMSO3NblcQrspeq3r7NeaYbsNqWm4PW+6d7FxDL0VJpHcwz R9Uic4yD3CyX4mN57u8A3ti1OYk38avs9tcgcSFTjFyxLNoilHoNaDEVYWa3kT6hmug0 w7OMC6q/9Llsf65DetN91mKPwF9Js0d/dAw3QPxr319QcZu5agRdr3gqcVL6CYoS/Q5E +XfxyFLUVLM/52jG9eH4v3Zfx6JKvU2mfPQDyx6bWnkExCRuqAW6pWhk/LbL+3MtHrpZ bjY/pmDu2/FxmKd+v8GQw8OdWpdgq4xKwIOKdDmi5xyXvVYXjG01mmOtreN+Gz0SMLjk ygSg== ARC-Message-Signature: i=1; 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=HGaBq9ixKr+elVaP+1SDYfug/E6zNtqo5465zLHvM88=; fh=t1C/dPyzqmSzNMfQ0CsfmyVkHzq6R51jobBs/oNDCvY=; b=NSB8t7VkS/4+LCc0/0WqHYT/iukI75ZfbBrvNwd7DV1N6ZDeSauTpmi1qZy5GD5PGI oWj5M0s3JFxWmTYEaVApCCDDaIG1uMaAEJSeFR2m2NIxDvX7Cs7OIXt3/+fR/lN/4DAx 15RKvHPo7SinGGPX1SRgYfVOQA0/sUU0ciEmz8gHvGhz5yBrWpuIL6+5HAC+pvnYtbfV neSs67su1eei0mdmZJ6MbNckawv3MgPPe+0oPqtpg5q2e7GdLHCMEwTK9aaFVW3+24cR EDRJvxgqiuwkTPqKt8vjQOjimJxsFb6GgoQCtg4lr0qzAJsqUzgO8iEHNCiq+52JglzC wHgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b="C/4Z9vgI"; spf=pass (google.com: domain of linux-kernel+bounces-11114-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-11114-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id rm7-20020a17090b3ec700b0028bbdb77e2esi8029237pjb.65.2023.12.25.07.46.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Dec 2023 07:46:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-11114-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=@virtuozzo.com header.s=relay header.b="C/4Z9vgI"; spf=pass (google.com: domain of linux-kernel+bounces-11114-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-11114-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id A579CB212D0 for ; Mon, 25 Dec 2023 15:46:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A189E52F65; Mon, 25 Dec 2023 15:46:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="C/4Z9vgI" X-Original-To: linux-kernel@vger.kernel.org Received: from relay.virtuozzo.com (relay.virtuozzo.com [130.117.225.111]) (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 4A896524CC for ; Mon, 25 Dec 2023 15:45:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=virtuozzo.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=virtuozzo.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=HGaBq9ixKr+elVaP+1SDYfug/E6zNtqo5465zLHvM88=; b=C/4Z9vgINiPL DalMqX02e8xs08pOVG6Sa/vSxHpn6ni7MsipYkLlHEogcpwiBv3SPax6vaAYnVM2Snfoif0uq+xMh rEU1qvongQP+iEgj/8VRl05sT6o3Tg49++pPk7vigEcZNeech5ySEyGrXYMkXAkyP668ZOdI2YSOw rUTKK9oEXTQEyecgMgTJuf+1gCVDG1WMtaNiQ3y8zlx6YfmQ6IXsASc37jrW7C2we6Y3rMn1hgopp MLVkgKvkZnxsJubMKJaZrlh29SzEuVCKq02HGgTSyrR1SaJyGRkB5d0Y3QvBSMpwJ+kbnqi3gjLuK I6gxXjn6Gk3ak6059C9Hug==; Received: from [130.117.225.1] (helo=finist-alma9.sw.ru) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1rHmrV-003pq0-1k; Mon, 25 Dec 2023 16:29:36 +0100 From: Konstantin Khorenko To: Vincent Guittot Cc: Ingo Molnar , Peter Zijlstra , Juri Lelli , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Alexander Atanasov , linux-kernel@vger.kernel.org Subject: [PATCH v2] sched/fair: Do not scan non-movable tasks several times Date: Mon, 25 Dec 2023 18:29:43 +0300 Message-Id: <20231225152943.2657849-1-khorenko@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <2cf94373-4f25-4a33-a0b4-cab04031bae7@virtuozzo.com> References: <2cf94373-4f25-4a33-a0b4-cab04031bae7@virtuozzo.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit If busiest rq is small, nr_running < SCHED_NR_MIGRATE_BREAK and all tasks are not movable, detach_tasks() should not iterate more than tasks available in the busiest rq. Before commit: b0defa7ae03e ("sched/fair: Make sure to try to detach at least one movable task"), the (env->loop > env->loop_max) condition prevented us from scanning non-movable tasks more than rq size times, but after we start checking the LBF_ALL_PINNED flag, the "all tasks are not movable" case is under threat. Note: in case all tasks in the rq could not be moved in detach_tasks() we always increase loop_break by SCHED_NR_MIGRATE_BREAK, so we can step over loop_max, but i think it's a rare case and does not worth adding here extra check for rq->nr_running overlimit. Fixes: b0defa7ae03e ("sched/fair: Make sure to try to detach at least one movable task") Signed-off-by: Konstantin Khorenko --- Changes: v1->v2: * added the exact commit id caused the unefficiency + Fixes: tag * dropped a couple of extra redundunt env.loop_break assignments in load_balance() kernel/sched/fair.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index d7a3c63a2171..bd69c33fe9b4 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -11219,7 +11219,6 @@ static int load_balance(int this_cpu, struct rq *this_rq, .dst_rq = this_rq, .dst_grpmask = group_balance_mask(sd->groups), .idle = idle, - .loop_break = SCHED_NR_MIGRATE_BREAK, .cpus = cpus, .fbq_type = all, .tasks = LIST_HEAD_INIT(env.tasks), @@ -11266,6 +11265,14 @@ static int load_balance(int this_cpu, struct rq *this_rq, */ env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); +more_balance_reset_break: + /* + * If busiest rq is small, nr_running < SCHED_NR_MIGRATE_BREAK + * and all tasks are not movable, detach_tasks() should not + * iterate more than tasks available in rq. + */ + env.loop_break = min(SCHED_NR_MIGRATE_BREAK, busiest->nr_running); + more_balance: rq_lock_irqsave(busiest, &rf); update_rq_clock(busiest); @@ -11328,13 +11335,12 @@ static int load_balance(int this_cpu, struct rq *this_rq, env.dst_cpu = env.new_dst_cpu; env.flags &= ~LBF_DST_PINNED; env.loop = 0; - env.loop_break = SCHED_NR_MIGRATE_BREAK; /* * Go back to "more_balance" rather than "redo" since we * need to continue with same src_cpu. */ - goto more_balance; + goto more_balance_reset_break; } /* @@ -11360,7 +11366,6 @@ static int load_balance(int this_cpu, struct rq *this_rq, */ if (!cpumask_subset(cpus, env.dst_grpmask)) { env.loop = 0; - env.loop_break = SCHED_NR_MIGRATE_BREAK; goto redo; } goto out_all_pinned; -- 2.39.3