Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp4494482rdh; Wed, 29 Nov 2023 03:09:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IGuwkN5FrxfnbWPKQs9rzWMhgiw59mdfnSOa//LtnXrxcV0U9aKef157hCT8ZKVnAyV8seh X-Received: by 2002:a05:6808:2f18:b0:3b8:93b4:44e0 with SMTP id gu24-20020a0568082f1800b003b893b444e0mr3170734oib.36.1701256187022; Wed, 29 Nov 2023 03:09:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701256186; cv=none; d=google.com; s=arc-20160816; b=Hjnu/zVDJYLg0DdmZG8YfMXEdLk3TfHcS64Bbzqsx+Vdin/EdHN6zK//rgI53F04kS /SUQG0DGtMcoKIAdc/PQX1V33dZrQPZCZjlbZhuHBPeJYk2fQRZbWG8HNNwvzoFrw5C1 S1fmrs1RvG8edFRdO8AKnEI+QJt7W103qkqpnV80nin2Ny1du4qjk+3+EoKabVk5zIgd ouAQy1tyN0wgy0mhns622HZ6NtYhzC24NsU4XGyEdjSmx4uAQarES66PCsyJtRMPwCD1 Z86Pt8qeYE1p4RNnYmaW+oeUFM4Z7USMWn7o2Ch7PbNddk0v1jw07zWAfddn/O2MSHSR epLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=CU5Br8vYtFEwg9if3s/Nj1dALUScHqeMgKvP4liuMgk=; fh=jF+j1UEDAwnYbShW3HmO1TshMsWh36Jt7pSLTP62NeE=; b=FP5OuMHHRZdNk6SxIHeu1pbKmejf6gZkmdK3DFUjK/iCcL8zYHbetC1J7Jvz48FuN+ uWnnp2NCvl9fngs+MRQgjGm8n9rZehxmRqbNxhjavAQh6aU7vTUWWF1X9qthcyxGrdPd +dsijUzHdfrhTskLlBxe82pdML5HshwoWo7MxAtCV1hP75fMk0hfQfBpm/KJqaFLk6iY nWYnUhaV7KmRJXDGyy199cgquzZqOuMkdcsBZBzHgSFueGsnu7AaXDA7jo4KjqaZ4zjT 67jfEcXJzKjampo/0dIH6uxqDQ/KqyFI6cNrguxMmbKN6Hcu/r0mHDQhysrvvFaGfSHN 4J+g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id f12-20020a631f0c000000b005b21d089aabsi14389175pgf.519.2023.11.29.03.09.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 03:09:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 1D22B80423AB; Wed, 29 Nov 2023 03:09:43 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232906AbjK2LJL (ORCPT + 99 others); Wed, 29 Nov 2023 06:09:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232697AbjK2LIk (ORCPT ); Wed, 29 Nov 2023 06:08:40 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A89A4210D; Wed, 29 Nov 2023 03:08:31 -0800 (PST) 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 9616C2F4; Wed, 29 Nov 2023 03:09:18 -0800 (PST) Received: from e129166.arm.com (unknown [10.57.4.241]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 088573F5A1; Wed, 29 Nov 2023 03:08:28 -0800 (PST) From: Lukasz Luba To: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, rafael@kernel.org Cc: lukasz.luba@arm.com, 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, qyousef@layalina.io, wvw@google.com Subject: [PATCH v5 11/23] PM: EM: Add API for updating the runtime modifiable EM Date: Wed, 29 Nov 2023 11:08:41 +0000 Message-Id: <20231129110853.94344-12-lukasz.luba@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231129110853.94344-1-lukasz.luba@arm.com> References: <20231129110853.94344-1-lukasz.luba@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 29 Nov 2023 03:09:43 -0800 (PST) Add API function em_dev_update_perf_domain() which allows to safely change the EM. The concurrent modifiers are protected by the mutex to serialize them. Removal of the old memory is asynchronous and handled by the RCU mechanisms. Signed-off-by: Lukasz Luba --- include/linux/energy_model.h | 8 +++++++ kernel/power/energy_model.c | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h index e785211828fe..520a8c8ad849 100644 --- a/include/linux/energy_model.h +++ b/include/linux/energy_model.h @@ -183,6 +183,8 @@ struct em_data_callback { struct em_perf_domain *em_cpu_get(int cpu); struct em_perf_domain *em_pd_get(struct device *dev); +int em_dev_update_perf_domain(struct device *dev, + struct em_perf_table __rcu *new_table); int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states, struct em_data_callback *cb, cpumask_t *span, bool microwatts); @@ -379,6 +381,12 @@ struct em_perf_table __rcu *em_allocate_table(struct em_perf_domain *pd) return NULL; } static inline void em_free_table(struct em_perf_table __rcu *table) {} +static inline +int em_dev_update_perf_domain(struct device *dev, + struct em_perf_table __rcu *new_table) +{ + return -EINVAL; +} #endif #endif diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c index 489a358b9a00..614891fde8df 100644 --- a/kernel/power/energy_model.c +++ b/kernel/power/energy_model.c @@ -221,6 +221,52 @@ static int em_allocate_perf_table(struct em_perf_domain *pd, return 0; } +/** + * em_dev_update_perf_domain() - Update runtime EM table for a device + * @dev : Device for which the EM is to be updated + * @table : The new EM table that is going to used from now + * + * Update EM runtime modifiable table for the @dev using the privided @table. + * + * This function uses mutex to serialize writers, so it must not be called + * from non-sleeping context. + * + * Return 0 on success or a proper error in case of failure. + */ +int em_dev_update_perf_domain(struct device *dev, + struct em_perf_table __rcu *new_table) +{ + struct em_perf_table __rcu *old_table; + struct em_perf_domain *pd; + + /* + * The lock serializes update and unregister code paths. When the + * EM has been unregistered in the meantime, we should capture that + * when entering this critical section. It also makes sure that + * two concurrent updates will be serialized. + */ + mutex_lock(&em_pd_mutex); + + if (!dev || !dev->em_pd) { + mutex_unlock(&em_pd_mutex); + return -EINVAL; + } + pd = dev->em_pd; + + em_inc_usage(new_table); + + old_table = pd->runtime_table; + rcu_assign_pointer(pd->runtime_table, new_table); + + em_cpufreq_update_efficiencies(dev, new_table->state); + + em_dec_usage(old_table); + + mutex_unlock(&em_pd_mutex); + return 0; +} +EXPORT_SYMBOL_GPL(em_dev_update_perf_domain); + static int em_create_runtime_table(struct em_perf_domain *pd) { struct em_perf_table __rcu *runtime_table; -- 2.25.1