Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp936805rwl; Fri, 24 Mar 2023 04:12:53 -0700 (PDT) X-Google-Smtp-Source: AKy350ZdQkerQK04Zt97DrhCIgGRygPlYOaAK1h8qS2H8C+l531sPII86g+QFTINVWnFW/2u+CgI X-Received: by 2002:a17:906:9149:b0:932:4990:2563 with SMTP id y9-20020a170906914900b0093249902563mr2387508ejw.24.1679656372959; Fri, 24 Mar 2023 04:12:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679656372; cv=none; d=google.com; s=arc-20160816; b=bePxHMxBqbbkkmGUqUYJHgZ/DRhfC0T2316a40EzgVKdIyT1lD5E9FpgmVy44xIrhI y4Na8O94hbdH7LBjjPR4N8A/Vfv7wx8WKIiGqWhFOImRLkM72w7TgOyd0TW0ojsZiODy p/8f3TbwHlAKLyvFN2+mueLBX9cbYkOHjxaTg0q3EGWakRRAOOd50CK5tE0Lk8gJxivP 8iQbUrmO32j0CcplCN346PN3sM/RlrSYkfe/dHpLinSO9xg8rekipPa8zSHzN0FfMkfS lr8dU6275Ps1SXQmDk5C+2SI4Hlkw9rqVx3I5fRT1vaBwoQx14t/XQDhdfkbu3SU4x1W nYlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=4D4LgeFONhVD0OMpRmJOGu7gxn1ZSDYycD+7uVuvFCA=; b=pZ99DFuRO1GMkcmbzvTSJq9ux6EzS7gNLpSz7eFPRrJEBgZWWugxi/rLY8AD7vhPFA RVx1HTKuZ1o5xttZNdmzRqk8MjgbBe0gQ9F5GKfnC9hQFehwoFEkg7Y4y5i1oAidLxPh JnKlDS7N973eMZhFgaODkdAcfpS1uVxADzBULLLz+S7gVIRPDq0xwP3TYFqhW1Cig+xO 1zDY31NptBdnDOhnAdv2MbF0RDSvVQQOLIQ+Sq49ihmfABWZa4RV+S1E0jnFMV4ajWF5 qzPsO0Bf9VtmcQyn05aozC93dsqQ3O01AzAjBmDpvHSax4TiB+5bB4baQfZI3hvEnSKg BVzQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jz12-20020a17090775ec00b0092fd27697ecsi18831143ejc.908.2023.03.24.04.12.28; Fri, 24 Mar 2023 04:12:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231289AbjCXKq3 (ORCPT + 99 others); Fri, 24 Mar 2023 06:46:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230213AbjCXKq1 (ORCPT ); Fri, 24 Mar 2023 06:46:27 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6671A241F0; Fri, 24 Mar 2023 03:46:26 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3B9F411FB; Fri, 24 Mar 2023 03:47:10 -0700 (PDT) Received: from [10.57.18.195] (unknown [10.57.18.195]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A51003F6C4; Fri, 24 Mar 2023 03:46:23 -0700 (PDT) Message-ID: <09ea63b7-7294-a143-c797-95eba87c765e@arm.com> Date: Fri, 24 Mar 2023 10:46:21 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: [PATCH] cpufreq: schedutil: Combine two loops into one in sugov_start() Content-Language: en-US To: Yajun Deng Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, rostedt@goodmis.org, dietmar.eggemann@arm.com, vincent.guittot@linaro.org, mingo@redhat.com, vschneid@redhat.com, bristot@redhat.com, bsegall@google.com, juri.lelli@redhat.com, peterz@infradead.org, mgorman@suse.de, viresh.kumar@linaro.org, rafael@kernel.org References: <20230324100023.900616-1-yajun.deng@linux.dev> From: Lukasz Luba In-Reply-To: <20230324100023.900616-1-yajun.deng@linux.dev> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.3 required=5.0 tests=NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Yajun, On 3/24/23 10:00, Yajun Deng wrote: > The sugov_start() function currently contains two for loops that > traverse the CPU list and perform some initialization tasks. The first > loop initializes each sugov_cpu struct and assigns the CPU number and > sugov_policy pointer. The second loop sets up the update_util hook for > each CPU based on the policy type. > > Since both loops operate on the same CPU list, it is possible to combine > them into a single for loop. This simplifies the code and reduces the > number of times the CPU list needs to be traversed, which can improve > performance. > > Signed-off-by: Yajun Deng > --- > kernel/sched/cpufreq_schedutil.c | 12 ++++-------- > 1 file changed, 4 insertions(+), 8 deletions(-) > > diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c > index e3211455b203..9a28ebbb9c1e 100644 > --- a/kernel/sched/cpufreq_schedutil.c > +++ b/kernel/sched/cpufreq_schedutil.c > @@ -766,14 +766,6 @@ static int sugov_start(struct cpufreq_policy *policy) > > sg_policy->need_freq_update = cpufreq_driver_test_flags(CPUFREQ_NEED_UPDATE_LIMITS); > > - for_each_cpu(cpu, policy->cpus) { > - struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu); > - > - memset(sg_cpu, 0, sizeof(*sg_cpu)); > - sg_cpu->cpu = cpu; > - sg_cpu->sg_policy = sg_policy; > - } > - > if (policy_is_shared(policy)) > uu = sugov_update_shared; > else if (policy->fast_switch_enabled && cpufreq_driver_has_adjust_perf()) > @@ -784,6 +776,10 @@ static int sugov_start(struct cpufreq_policy *policy) > for_each_cpu(cpu, policy->cpus) { > struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu); > > + memset(sg_cpu, 0, sizeof(*sg_cpu)); > + sg_cpu->cpu = cpu; > + sg_cpu->sg_policy = sg_policy; > + > cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, uu); > } > return 0; IMO the change might cause a race. There is a call to set scheduler hook in the 2nd loop. If you combine two loops that hook might be used from other CPU in the meantime, while still the rest CPUs are not finished. The first loop makes sure all CPUs in the 'policy->cpus' get a clean context 'sg_cpu' and proper 'cpu' values first (and 'sg_policy' as well). When the two loops are combined, there might be fast usage from scheduler on other CPU the sugov code path. If the policy is shared for many CPUs and any of them is able to change the freq, then some CPU can enter this code flow, where remotely wants to check the other CPUs' utilization: sugov_next_freq_shared() for each cpu in policy->cpus: sugov_get_util() where the 'sg_cpu->cpu' is used Therefore, IMO this optimization in a start function (which is only called once and is not part of the 'hot path') is not worth the race risk. Regards Lukasz