Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6473647rdb; Tue, 2 Jan 2024 03:16:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IHBNdc2P/UJ5pk8SYBVa6JHX6ZlIimOC4SVgTbiUW5mrPkVSf0l9QmBb8DOgtsc3wfkGWcQ X-Received: by 2002:a05:6808:201e:b0:3bc:1b42:836e with SMTP id q30-20020a056808201e00b003bc1b42836emr550309oiw.46.1704194189302; Tue, 02 Jan 2024 03:16:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704194189; cv=none; d=google.com; s=arc-20160816; b=s61hJmu11R0TSiv62K6FK3mHINATbh1IJ8pasmTE6H7GsRrbHVAaeu5FyteD3arVFJ F1Aq4WDNITulgZUhmhGMq6qRQB30HvOIJ7V0Hi4bckpvtMeKHnCJqRuMdRlFj4aSaF6q enWT/M9vSnTUNs5lSs5jEwuKy+lobD4tu9kHjCpVCKbfFKvjzWBhtCPExGH50dQOuXfu 1qkg+Q47u/+2PcNSqblFhm9s1S/zKrb+APYANa0YN6usLjLWM1EtNUL3zjWqF13sov1b 9ZtalTNTigJZmZlBhSaGUgEvl1xGSE27YVYP1ip/fkHOKmPYwDKr2GmevFu/TGV0v5/Y QXYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=MkXAwaiYExOrQanJFQyAUBBU2dLbGi3nnK94xSHI7tg=; fh=KVX3ysCEHaRuIbAydryWG/WyZqn6wdbvFTFD/q/ZByg=; b=P84KCSjgorjqIbtvS5c2MuC4pWniU2OzeqHS011eEmNEPoTHFc6gRoIdiCaRvEPYMF 5QHLTXk8FyE8Z0gRX2s/Zvm1p3EUVSsJr3zWkl4nLgblYYgxQpUgKWzBlWi/znefzTMO IAx6ui98gjPp170BhScsunLAR6B7JeMc/Jxz4d5kclHdB5h3Rw/By++8bSZTUQWsGRVu tXQ9LWW/NfzB8oQqoaPBbW86/HEGbUVr3LYFu+y1f5Yf+FxDhzJuKp+YURTuctZqWahw wLLxP76dqDTVW4YWy7ErOlfWXFakB5jUPfB4hsVVyGj82mKWf/1YPLlMKzi5LGUDzTZX MdiQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-14276-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14276-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id i26-20020aa78b5a000000b00690dbd360basi19593666pfd.152.2024.01.02.03.16.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 03:16:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14276-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-14276-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14276-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.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 E541528316B for ; Tue, 2 Jan 2024 11:16:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AFC5DDF63; Tue, 2 Jan 2024 11:16:19 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C5B07DF51; Tue, 2 Jan 2024 11:16:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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 21B5CC15; Tue, 2 Jan 2024 03:17:02 -0800 (PST) Received: from [10.57.86.61] (unknown [10.57.86.61]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 813A63F7A6; Tue, 2 Jan 2024 03:16:13 -0800 (PST) Message-ID: <781005d2-2ec9-4e6b-8472-33da85e77575@arm.com> Date: Tue, 2 Jan 2024 11:17:31 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 09/23] PM: EM: Use runtime modified EM for CPUs energy estimation in EAS Content-Language: en-US To: Qais Yousef Cc: Xuewen Yan , linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, rafael@kernel.org, dietmar.eggemann@arm.com, rui.zhang@intel.com, amit.kucheria@verdurent.com, amit.kachhap@gmail.com, daniel.lezcano@linaro.org, viresh.kumar@linaro.org, len.brown@intel.com, pavel@ucw.cz, mhiramat@kernel.org, wvw@google.com References: <20231129110853.94344-1-lukasz.luba@arm.com> <20231129110853.94344-10-lukasz.luba@arm.com> <20231217175923.wxmfocgckpaytptb@airbuntu> <1ccd7a20-0479-46f7-a968-57a18f0c0152@arm.com> <20231228173256.kepuwwimb4b2osew@airbuntu> From: Lukasz Luba In-Reply-To: <20231228173256.kepuwwimb4b2osew@airbuntu> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 12/28/23 17:32, Qais Yousef wrote: > On 12/19/23 08:32, Lukasz Luba wrote: >> Hi Qais and Xuewen, >> >> On 12/19/23 04:03, Xuewen Yan wrote: >>> On Mon, Dec 18, 2023 at 1:59 AM Qais Yousef wrote: >>>> >>>> On 11/29/23 11:08, Lukasz Luba wrote: >>>>> The new Energy Model (EM) supports runtime modification of the performance >>>>> state table to better model the power used by the SoC. Use this new >>>>> feature to improve energy estimation and therefore task placement in >>>>> Energy Aware Scheduler (EAS). >>>> >>>> nit: you moved the code to use the new runtime em table instead of the one >>>> parsed at boot. >>>> >>>>> >>>>> Signed-off-by: Lukasz Luba >>>>> --- >>>>> include/linux/energy_model.h | 16 ++++++++++++---- >>>>> 1 file changed, 12 insertions(+), 4 deletions(-) >>>>> >>>>> diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h >>>>> index 1e618e431cac..94a77a813724 100644 >>>>> --- a/include/linux/energy_model.h >>>>> +++ b/include/linux/energy_model.h >>>>> @@ -238,6 +238,7 @@ static inline unsigned long em_cpu_energy(struct em_perf_domain *pd, >>>>> unsigned long max_util, unsigned long sum_util, >>>>> unsigned long allowed_cpu_cap) >>>>> { >>>>> + struct em_perf_table *runtime_table; >>>>> unsigned long freq, scale_cpu; >>>>> struct em_perf_state *ps; >>>>> int cpu, i; >>>>> @@ -255,7 +256,14 @@ static inline unsigned long em_cpu_energy(struct em_perf_domain *pd, >>>>> */ >>>>> cpu = cpumask_first(to_cpumask(pd->cpus)); >>>>> scale_cpu = arch_scale_cpu_capacity(cpu); >>>>> - ps = &pd->table[pd->nr_perf_states - 1]; >>>>> + >>>>> + /* >>>>> + * No rcu_read_lock() since it's already called by task scheduler. >>>>> + * The runtime_table is always there for CPUs, so we don't check. >>>>> + */ >>>> >>>> WARN_ON(rcu_read_lock_held()) instead? >>> >>> I agree, or SCHED_WARN_ON(!rcu_read_lock_held()) ? >> >> I disagree here. This is a sched function in hot path and as comment > > WARN_ON() is not a sched function. I was referring to em_cpu_energy() being sched function. No one else should call it. That's the old contract also put into the doc of that function. > >> says: >> >> ----------------------- >> * This function must be used only for CPU devices. There is no validation, >> * i.e. if the EM is a CPU type and has cpumask allocated. It is called from >> * the scheduler code quite frequently and that is why there is not checks. >> ----------------------- >> >> We don't have to put the checks or warnings everywhere in the kernel >> functions. Especially hot one like this one. > > When checks are necessary, there are ways even for hot paths. We have that function called from feec() where the RCU must be hold, otherwise the whole EAS would be unstable. > >> >> As you might not notice, we don't even check if the pd->cpus is not NULL > > rcu_read_lock_held() is only enabled for lockdebug build and it's the standard > way to document and add verification to ensure locking rules are honoured. On > non lockdebug build this will be compiled out. > > You had to put a long comment to ensure locking rules are correct, why not > use existing infrastructure instead to provide better checks and inherent > documentation? I didn't want to add any more overhead in this hot path. > > We had a bug recently where the rcu_read_lock() was moved and this broke some > function buried down in the call stack. So subtle code shuffles elsewhere can > cause unwanted side effects; and it's hard to catch these bugs. > > https://lore.kernel.org/stable/20231009130130.210024505@linuxfoundation.org/ OK, let me check that w/ and w/o lockdebug build and the SCHED_WARN_ON(!rcu_read_lock_held()) Although, it would be only a safety net for accidental use of em_cpu_energy() from code path other than feec()... Which actually might bring some value.