Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp7844476rdb; Thu, 4 Jan 2024 09:19:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IGeTivhGlaExnIa0CGmGhpVXC17NM5jarLVG7qd12S13Ei2ReWk9sXZ08ftvPWvS1k7/x0C X-Received: by 2002:a05:6a20:4c20:b0:199:2517:c563 with SMTP id fm32-20020a056a204c2000b001992517c563mr419807pzb.12.1704388752451; Thu, 04 Jan 2024 09:19:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704388752; cv=none; d=google.com; s=arc-20160816; b=jdqfntgSHkTCIt2ldeMuJZURcKuDNBVNhCSyYb+MiqBM1FNWbTdDN8BUyFMP83+yBo l74DMTnds2gwLKaN5o8KWL7SDF/tPeIgrvtAKh7wsAnXFh/29I4UDWpGIUSLzy8KDabx F39ockg7ylgwk81R5KxvMDwFxfdJmJYwJXuaPlYij/ku1oBXoL5N3W8vOJcdAy7NHSCA XqVHDPiqNpvaTZS8MIqDB6xC5a7mJh6v2BWqLoOpRx/G01ZjP6czunMwM9PxijD98nSE e1DL3k/PenwrtaSkHocI5amrRVYlh7QnMaBVpVE84od5tM2P3aA2anJT5cQDQpb+TzsC ZWZA== ARC-Message-Signature: i=1; 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:references:in-reply-to:message-id :date:subject:cc:to:from; bh=BoupHVeM3nKgsy3vFVQ2BSPfosTM/tc8Ni8tOmSPOr4=; fh=jF+j1UEDAwnYbShW3HmO1TshMsWh36Jt7pSLTP62NeE=; b=iYxdvx7Px1TZir7DDE3kwI42tBVMOkjiBjQxGVP1VozMCGxQfwH0QW1hpJ7EM3a6UG CtmrdtCTt5NAPY1S6uw9nAnrsoEkIbFyMf+dg8oyPhSF/o0EEyaNgmfUx8VohsPzjtFQ SY+619XycxAG/VM4x1IHqr9GpQufEO8oFBR9/rPEZPXtrZ8I0Jt+xh9FH3tajK2sFOwi ZRhZQHrIk6F6bspiiJYf4zaURWgLuARF6Fw4ZhLzKSbidw19lBYSLffngtGT8cfJVONn oZtd8geDscAhwVvMYQQoghVgePRkiCmKAaWc97Dg/pk9/PD587DQ2Q0F1EPqbgWW6NQx dLIA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-17014-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17014-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 j16-20020aa79290000000b006d8a3849027si23223458pfa.325.2024.01.04.09.19.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 09:19:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-17014-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-17014-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17014-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 1F4BE2884AA for ; Thu, 4 Jan 2024 17:19:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 94C752E40E; Thu, 4 Jan 2024 17:15:22 +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 C0A9E2E3FF; Thu, 4 Jan 2024 17:15:20 +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 626DB153B; Thu, 4 Jan 2024 09:16:06 -0800 (PST) Received: from e129166.arm.com (unknown [10.57.88.128]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id EF1B13F64C; Thu, 4 Jan 2024 09:15:17 -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 v6 11/23] PM: EM: Add API for updating the runtime modifiable EM Date: Thu, 4 Jan 2024 17:15:41 +0000 Message-Id: <20240104171553.2080674-12-lukasz.luba@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240104171553.2080674-1-lukasz.luba@arm.com> References: <20240104171553.2080674-1-lukasz.luba@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h index 753d70d0ce7e..f33257ed83fd 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); @@ -376,6 +378,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 bbc406db0be1..496dc00835c6 100644 --- a/kernel/power/energy_model.c +++ b/kernel/power/energy_model.c @@ -220,6 +220,47 @@ 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 be used from now + * + * Update EM runtime modifiable table for the @dev using the provided @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; + + /* Serialize update/unregister or concurrent updates */ + mutex_lock(&em_pd_mutex); + + if (!dev || !dev->em_pd) { + mutex_unlock(&em_pd_mutex); + return -EINVAL; + } + pd = dev->em_pd; + + em_table_inc(new_table); + + old_table = pd->em_table; + rcu_assign_pointer(pd->em_table, new_table); + + em_cpufreq_update_efficiencies(dev, new_table->state); + + em_table_dec(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 *table; -- 2.25.1