Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1576921lql; Wed, 13 Mar 2024 01:58:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUjHBqzP23k1FktCqcOERofPB7HIkQdaMR7IaxDaOrAm9SpqisULm0wxkAjDM5cWTBTX4ga1fdJATA0TuN0MIs2bMtcjqPLqd4rmqPgKQ== X-Google-Smtp-Source: AGHT+IGc79M7LWzdMpWs4pgr+cwa/W8qPaK3w7Dt2nG6/9mbcMCQpGdOrcOR6qw/eH8gZVvFjbtC X-Received: by 2002:a17:902:d2c5:b0:1dc:1ca9:daf4 with SMTP id n5-20020a170902d2c500b001dc1ca9daf4mr2956422plc.12.1710320317063; Wed, 13 Mar 2024 01:58:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710320317; cv=pass; d=google.com; s=arc-20160816; b=xYZKY4jOXV5EtiG1SwzDUTSICZa/42PaUfpTVu+zx2IN7jjRW1MKmUjGCdPiKsRW18 j3sKiN/zDURBx7fh7Cx4ruqL5VVoBica+S5m0lVDuh/cT5p+ix9gRDdpKf0wEBY06+kW LrIbPxVOErV6Gpw95NUfaE3LM3ZyOUcMKe6AgcH08e5KrLjXG20c5lRf/OvTVvM6TxS4 IGAd8VZ2Afr8GHKNuMj2Pq1AwySaXONlJ/ErFmKcSrHkvFyAbKKk5zaK6n8o4nrgOCTR TqS7/+gUkEQYsekGKB7K5ov2j0Pg07yKElST47LSz8tKIGDffaFE2mn0J3PSdEUZNIy0 Ddlw== 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:dkim-signature; bh=zIrDh8lB94CwYXoBew7XaDRCsSW7KAjXffZ933XhSFA=; fh=yATCGbH5P+1NkAwnSzOjYUjKTuCwq64kMLxw68juzkA=; b=n2LkPSQy+H1mWhYsIw3qjwsWtkE6rKyZmtHv3/dLiXVN+rivPl+z5uf8bzq5gtHWy0 0jGrCDvy8UBJBgEtZlBQMJsO0STTuP2ek/hBvIo1Gdsi8PmcrA04ePIRegRue8lv0pUz VX26e/zputfQVFb3/dMn1Dquk8XzXqkeXusZN+RVRaKmQWkNqr/6SiCmUpLnfJaAJFD0 rgWYVv1JcKwYiaMP4++7QQoBbvsB5JD0zFXcaMlyr+d95YTZ4C+RNFAgx9/sqr7yemaX TebHdfi19m8N6UO5UzjzuVPq5HwtdsgPdLax/3tHKI275NBd9agy8aiFNlldEgcuB7KC YwTQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=hnhOtDzG; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-101235-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101235-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id b6-20020a170902d50600b001dcdf20226bsi8914833plg.465.2024.03.13.01.58.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 01:58:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-101235-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=hnhOtDzG; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-101235-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101235-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 65D58283492 for ; Wed, 13 Mar 2024 08:58:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EBB6E1B7FE; Wed, 13 Mar 2024 08:58:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hnhOtDzG" Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 7689C1AAD4 for ; Wed, 13 Mar 2024 08:58:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710320309; cv=none; b=p0zaTQU48zFUG9O+io0hvzMU+eU2jHeqhNyYfVyGT1MB8Usu3S00j7ucrRRRzuKZ2RJGE+s0IZivP699KjXIJmKrCkKnxDUTmTsmzx4d7TW5F0ebVfo61VJfr3pggRkC0AYVjqYkgN84JdL64fC0kl4prdn8z4RCJnnanTRF53k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710320309; c=relaxed/simple; bh=kCp9HVXPhRp7NMjJ4e2BGHnQLPGtkMmG62GCznhyQG8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=SJaN/Ksj9zcKwKSNx3vqx0nbSECxRXUKnUEmRhPDwR4hXgg+2OxOp2tB5IuhvCNbKYtUk5+ofshRgINqp3GgO755Zhdbfh5bNzkQkIlK0V/AOfb64FJSE9rgPqwlgkfHTMUlC4EImzbMOIqVGtFXN6v+0c+3WpaZ3mAE7gVIOIE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hnhOtDzG; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1dd5df90170so6008715ad.0 for ; Wed, 13 Mar 2024 01:58:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710320307; x=1710925107; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=zIrDh8lB94CwYXoBew7XaDRCsSW7KAjXffZ933XhSFA=; b=hnhOtDzGkN4yPPsY8feZ0ZBP0yWLYvcGHN93Q96aIG4cE5+lJFmRoOYyk9K2hlK8uI IhUsmrpE4p/IEmv6OfNqZ+Trm2YCig/srN+qyZrOBOVHO4LK1ZAg8xJLowvhGl2ylI/q 0xwlJ3TDrjlqm/sYpSZzxT1qlQ/V1xm97ld1aXKUNdG6iLlJT6jUanundj/68zhqAVJG a3cT70I2SYYOlhKE+jSlZzbF8i3mbfxxljUtZOvWsZXupnp7sYYhp8wuMbDtBFmQuWLR kABFMC+1g8ewBgLSLpnj5gGQwnU6vM+baW2g8eVAzCYjBeIt2h+DWaj9MqRd2ldom14A jYpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710320307; x=1710925107; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zIrDh8lB94CwYXoBew7XaDRCsSW7KAjXffZ933XhSFA=; b=EOgygpLBHnwtU/JT97hmEW+/sp6mGw0jzTVCvWMohISiGhd8DyJOsQMfiEUZ4WrcwN T6Ge5AKOj02i6Ymuu/Wxdyg2lorG1p4JFWZFCrp3L09wJEc6pM4uEfTwDC0Jy+rhqRIB 1ncyBJQZksJx4Ur0ciWSq+AMyLQ8eVlJsyGM14azR86z6ikmXzgfnAAuKhpS+70pvQiJ q+pY2GSpWH1mtYqtJBC/YkqEdBF+sBiKz+00/T+8osgDlDNQIvdJNHBystoTXwJS6w+s MXCkYwizz/pyVAGDeNKM3r4JYNS1lN/8c+DQbVw42yOfGpyhvG1Ef3lQdi9nZgUrG1bf +6AA== X-Gm-Message-State: AOJu0YwtSfkdu9m7nE9v/XDcU7dkP0f9iemqRCnQEgjVHj77/vNJ1mvn xNYqWyn0YlvTD9ZfaeWfvndu6fXBQODdi4IhSJfPAn1esEYVvs6j X-Received: by 2002:a17:902:da91:b0:1d9:4106:b8b5 with SMTP id j17-20020a170902da9100b001d94106b8b5mr3127445plx.11.1710320306721; Wed, 13 Mar 2024 01:58:26 -0700 (PDT) Received: from localhost.localdomain ([43.132.141.14]) by smtp.googlemail.com with ESMTPSA id w13-20020a170902d3cd00b001dd707d5fe6sm8114791plb.158.2024.03.13.01.58.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 01:58:26 -0700 (PDT) From: Ze Gao X-Google-Original-From: Ze Gao To: Ben Segall , Daniel Bristot de Oliveira , Dietmar Eggemann , Ingo Molnar , Juri Lelli , Mel Gorman , Peter Zijlstra , Steven Rostedt , Valentin Schneider , Vincent Guittot Cc: linux-kernel@vger.kernel.org, Ze Gao Subject: [PATCH] sched: Improve rq selection for a blocked task when its affinity changes Date: Wed, 13 Mar 2024 04:58:17 -0400 Message-ID: <20240313085817.48892-1-zegao@tencent.com> X-Mailer: git-send-email 2.41.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit We observered select_idle_sibling() is likely to return the *target* cpu early which is likely to be the previous cpu this task is running on even when it's actually not within the affinity list newly set, from where after we can only rely on select_fallback_rq() to choose one for us at its will (the first valid mostly for now). However, the one chosen by select_fallback_rq() is highly likely not a good enough candidate, sometimes it has to rely on load balancer to kick in to place itself to a better cpu, which adds one or more unnecessary migrations in no doubt. For example, this is what I get when I move task 3964 to cpu 23-24 where cpu 23 has a cpu bound work pinned already: swapper 0 [013] 959.791829: sched:sched_migrate_task: comm=stress-ng-cpu pid=3964 prio=120 orig_cpu=13 dest_cpu=23 kworker/24:2-mm 1014 [024] 959.806148: sched:sched_migrate_task: comm=stress-ng-cpu pid=3964 prio=120 orig_cpu=23 dest_cpu=24 The thing is we can actually do better if we do checks early and take more advantages of the *target* in select_idle_sibling(). That is, we continue the idle cpu selection if *target* fails the test of cpumask_test_cpu( *target*, p->cpus_ptr). By doing so, we are likely to pick a good candidate, especially when the newly allowed cpu set shares some cpu resources with *target*. And with this change, we clearly see the improvement when I move task 3964 to cpu 25-26 where cpu 25 has a cpu bound work pinned already. swapper 0 [027] 4249.204658: sched:sched_migrate_task: comm=stress-ng-cpu pid=3964 prio=120 orig_cpu=27 dest_cpu=26 Note we do the same check for *prev* in select_idle_sibling() as well. Signed-off-by: Ze Gao --- kernel/sched/fair.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 533547e3c90a..9ef6e74c6b2a 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -7511,16 +7511,19 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target) */ lockdep_assert_irqs_disabled(); - if ((available_idle_cpu(target) || sched_idle_cpu(target)) && - asym_fits_cpu(task_util, util_min, util_max, target)) + if (cpumask_test_cpu(target, p->cpus_ptr) && + (available_idle_cpu(target) || sched_idle_cpu(target)) && + asym_fits_cpu(task_util, util_min, util_max, target)) return target; /* * If the previous CPU is cache affine and idle, don't be stupid: */ - if (prev != target && cpus_share_cache(prev, target) && - (available_idle_cpu(prev) || sched_idle_cpu(prev)) && - asym_fits_cpu(task_util, util_min, util_max, prev)) { + if (cpumask_test_cpu(prev, p->cpus_ptr) && + prev != target && + cpus_share_cache(prev, target) && + (available_idle_cpu(prev) || sched_idle_cpu(prev)) && + asym_fits_cpu(task_util, util_min, util_max, prev)) { if (!static_branch_unlikely(&sched_cluster_active) || cpus_share_resources(prev, target)) -- 2.41.0