From nobody Wed Apr 8 04:37:54 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 78EF8C00140 for ; Wed, 24 Aug 2022 08:20:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235681AbiHXIUS (ORCPT ); Wed, 24 Aug 2022 04:20:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235584AbiHXIUL (ORCPT ); Wed, 24 Aug 2022 04:20:11 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F01B37FF93 for ; Wed, 24 Aug 2022 01:20:09 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id x14-20020a17090a8a8e00b001fb61a71d99so826213pjn.2 for ; Wed, 24 Aug 2022 01:20:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=h1njInj6+0OhDb5dvpxX7bRUejwiRI98B8XQXql9WZQ=; b=6yf8wi5zNwzZEIkCgVGTFObmPAniOIiQplzYcLSqdfynQw4Vz6bS6N+/FmuzSny6ig IbyW0Ll7Ib5/jwQy0MXG8EzqmWCb3Z+6uOTpaat+70mhVmkKwAycfew2QvqpBorPYVpn j8UXv3nEOdb0xJXxSQbqLZu2SGvwDIRpmeOsvG6Meij2IOzxPdRcyTYoyp4tnOaBvrG0 ij0kKjV6+2yUcfuz5jgsZTb1l8Je8MXPyvvGRgtN8nbzcgBCzC+9q+XiNo6ZvIudhAV+ C3bE3iISnulIycKcpmRz9qXnmLn7bqfELsePToRk1Ibz7O2hAhd646bBzd+ws075cyUU k5Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=h1njInj6+0OhDb5dvpxX7bRUejwiRI98B8XQXql9WZQ=; b=7bVhpiNdrmTzJ/e1u1KBaScDbXMFBsLeucYCi71QRQTvvABoxQ9xYhKOuIcoP0CGoE NYuo5YAuA//JANTMhOHFlFlMqGVdTHgfqtNRSHZhUPQ9/W8n0tByjwuQIudzVXakfIGV d/AtwR/CdRMKBnL6sJf6autB6EMoD686E4YHRkmOsPzGlgG/wH4839mjCvPvd6QS8yTV kYoFRlRtoaF7oJwf3Ry1Dtxa/uG0xhY+IRoSDR1r66EPRSbtMfo0tVFUBu6yo3+zYOZF mbwz24N9mb1m4PYVC/YlMYBYDrXgVDrI/6US9NAZ86R4jA49IfL4/wYnJxqPrp4zEYVo PE3w== X-Gm-Message-State: ACgBeo0mDnXCJruAv5sHt+3JNIjZfndJkVbhkCaZy1gnMe1Y2aWmg+V/ RpX5/fwvIsRpV8rTg3dmHPb0yA== X-Google-Smtp-Source: AA6agR5rrAy3gZcaFBGnbostM2BwS4dc/C5F1+ZodDYHXjtFDsV/x7DOwZWdoQqDtcedLKMDeBp79A== X-Received: by 2002:a17:902:cec1:b0:16d:c4f2:66c5 with SMTP id d1-20020a170902cec100b0016dc4f266c5mr27326630plg.20.1661329209524; Wed, 24 Aug 2022 01:20:09 -0700 (PDT) Received: from C02CV1DAMD6P.bytedance.net ([139.177.225.244]) by smtp.gmail.com with ESMTPSA id q31-20020a635c1f000000b00421841943dfsm10486587pgb.12.2022.08.24.01.20.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Aug 2022 01:20:09 -0700 (PDT) From: Chengming Zhou To: tj@kernel.org, hannes@cmpxchg.org, mkoutny@suse.com, surenb@google.com Cc: gregkh@linuxfoundation.org, corbet@lwn.net, mingo@redhat.com, peterz@infradead.org, songmuchun@bytedance.com, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Chengming Zhou Subject: [PATCH v3 05/10] sched/psi: optimize task switch inside shared cgroups again Date: Wed, 24 Aug 2022 16:18:24 +0800 Message-Id: <20220824081829.33748-6-zhouchengming@bytedance.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220824081829.33748-1-zhouchengming@bytedance.com> References: <20220824081829.33748-1-zhouchengming@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" commit 4117cebf1a9f ("psi: Optimize task switch inside shared cgroups") defer prev task sleep handling to psi_task_switch(), so we don't need to clear and set TSK_ONCPU state for common cgroups. A | B / \ C D / \ prev next After that commit psi_task_switch() do: 1. psi_group_change(next, .set=3DTSK_ONCPU) for D 2. psi_group_change(prev, .clear=3DTSK_ONCPU | TSK_RUNNING) for C 3. psi_group_change(prev, .clear=3DTSK_RUNNING) for B, A But there is a limitation "prev->psi_flags =3D=3D next->psi_flags" that if not satisfied, will make this cgroups optimization unusable for both sleep switch or running switch cases. For example: prev->in_memstall !=3D next->in_memstall when sleep switch: 1. psi_group_change(next, .set=3DTSK_ONCPU) for D, B, A 2. psi_group_change(prev, .clear=3DTSK_ONCPU | TSK_RUNNING) for C, B, A prev->in_memstall !=3D next->in_memstall when running switch: 1. psi_group_change(next, .set=3DTSK_ONCPU) for D, B, A 2. psi_group_change(prev, .clear=3DTSK_ONCPU) for C, B, A The reason why this limitation exist is that we consider a group is PSI_MEM_FULL if the CPU is actively reclaiming and nothing productive could run even if it were runnable. So when CPU curr changed from prev to next and their in_memstall status is different, we have to change PSI_MEM_FULL status for their common cgroups. This patch remove this limitation by making psi_group_change() change PSI_MEM_FULL status depend on CPU curr->in_memstall status. Signed-off-by: Chengming Zhou --- kernel/sched/psi.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c index 77d53c03a76f..26c03bd56b9c 100644 --- a/kernel/sched/psi.c +++ b/kernel/sched/psi.c @@ -820,8 +820,6 @@ void psi_task_switch(struct task_struct *prev, struct t= ask_struct *next, u64 now =3D cpu_clock(cpu); =20 if (next->pid) { - bool identical_state; - psi_flags_change(next, 0, TSK_ONCPU); /* * When switching between tasks that have an identical @@ -829,11 +827,9 @@ void psi_task_switch(struct task_struct *prev, struct = task_struct *next, * we reach the first common ancestor. Iterate @next's * ancestors only until we encounter @prev's ONCPU. */ - identical_state =3D prev->psi_flags =3D=3D next->psi_flags; iter =3D NULL; while ((group =3D iterate_groups(next, &iter))) { - if (identical_state && - per_cpu_ptr(group->pcpu, cpu)->tasks[NR_ONCPU]) { + if (per_cpu_ptr(group->pcpu, cpu)->tasks[NR_ONCPU]) { common =3D group; break; } @@ -880,7 +876,7 @@ void psi_task_switch(struct task_struct *prev, struct t= ask_struct *next, * TSK_ONCPU is handled up to the common ancestor. If we're tasked * with dequeuing too, finish that for the rest of the hierarchy. */ - if (sleep) { + if (sleep || unlikely(prev->in_memstall !=3D next->in_memstall)) { clear &=3D ~TSK_ONCPU; for (; group; group =3D iterate_groups(prev, &iter)) psi_group_change(group, cpu, clear, set, now, wake_clock); --=20 2.37.2