From patchwork Thu Feb 8 11:55:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Luba X-Patchwork-Id: 198344 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp110972dyd; Thu, 8 Feb 2024 03:59:54 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVpCPrq07mrReIG9LTyW708UQcPjk1umeOq60ux1DoEjwMxVj3zaXR+lEp03vEVUa4TpGSm0kNK1eMnLQdt1V87eZHW/g== X-Google-Smtp-Source: AGHT+IF+Kd9zWQi3H0w+kD7puIYlccr41jQurPrD3azJnvwef10wr9b9RtuNPNJi8IvJZXphK6/B X-Received: by 2002:a05:620a:3890:b0:785:9fb8:a46b with SMTP id qp16-20020a05620a389000b007859fb8a46bmr3544902qkn.60.1707393594738; Thu, 08 Feb 2024 03:59:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707393594; cv=pass; d=google.com; s=arc-20160816; b=K9+7U16T4tnltvibONDDpydpVH0l5mXRj5WZRlWhNf1w320+CFwYfcrUqsFlxSis6T rcR8rJtg1+RZlJHrtp8jYw0w5wLezwvlGlDUM4iq0OJnN3ck0cSpW/Ek91XJS3pdkzAR rzTCZ6z4hVUdTAUt0t+JjJ/gB1FkIYjWOkfkWlYEexaPAStIjWY3zD1aa/KkTt5iGUzS zfs+dKf+XehmBa4OmvowqNCrLUhZKQXtgsP1O6bocQX/UwGz07SXStk08vlezUNuQ3CG K5WVGpniCvDVaS2j1j1uinl21RHpSxAY5sMv/ao5YYeH9orpZgpe4f65Jk0TKKbxhpPn QYsg== ARC-Message-Signature: i=2; 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=ehl74s8H6KBWPH1gNbRcisSNCymH4a0OFm6UiRHHYBE=; fh=tUw5tHTPu9WfuyPta6y3NF5E8yjz0hqlZcwMpYCDJrE=; b=bejfmkoK5/ISxQJuefIXawULLXJuyTBZi5xDzpO31NbwrLByJFeBsPhgUw01pooLWh Z86BRpRgApplRane3zCTtdkQC1f6uKViF9xqYZoaP6J0Kkot14UsoVUD7vEOWoL+JCH1 mGZQ2HbPII8/rptVrfv5TcmMd6btc6OxOIxwDsxcopEZXSR8ihOvPZBdkE8SWdDny9aX DSdMFGDq5zPFfY7y8YxCQK2Yy/4he04v4r27+prTE36S2fEVltdWVGPKI+/4wTtJtlzV 1vWKFn1V3+/t5XXaQbNiQzWRDVMMAL5fkcGdApXXxWZllAQ/e+/gywvIBU82B4XGF4Y0 y8Nw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-58005-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58005-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com X-Forwarded-Encrypted: i=2; AJvYcCXztm1wYnuB+5wL6ZozGx8OpX/ThWBtYC7ZKJjnNRunbKCr7ZXNBHwT8E+7oz2pkPI8owEiUZjC2fvXoqxyKgJMuSsddw== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id ay39-20020a05622a22a700b0042c4118fcfdsi3062020qtb.157.2024.02.08.03.59.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 03:59:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-58005-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-58005-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58005-ouuuleilei=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 8405B1C25D33 for ; Thu, 8 Feb 2024 11:59:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A60987AE79; Thu, 8 Feb 2024 11:56:42 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0F5FD79DA1; Thu, 8 Feb 2024 11:56:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707393400; cv=none; b=Dv1sxkBgJEmfImUZ0dDGCEsse6OlgCbyb1BhwiB6TFH2WrklUoZYLz+WHjFF7rF1FfXWHcqc9a9oDx/YfNxUd69wyHV+8ntrwo5mfoRZDbibP7ZBh5OdB92m+L+nftu1Umeu9qNi7FuB4BIQElXCBrq0R3t2dxZJPUkEkj291ck= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707393400; c=relaxed/simple; bh=OoFrzVwdfR8MuA4B3/NSqIrR20qh/M0U8am5P3jts3o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gZ89Zw/TrzrjqAYHntSSVxAUjpQzvOEVvsK0iTIznzBhVqdoNOwdKbFSi0lgoX8h/Wede2GwYSOlIowuZ2ra2tg5AH0T+PCYso5o3sxBrT/Yfvg1fvroVSF5sdbesD/K2PBDUP4hjcytCBjCqhMTgsTvXf9wZ8dptuVvAUjCwp8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 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 E565A1FB; Thu, 8 Feb 2024 03:57:20 -0800 (PST) Received: from e129166.arm.com (unknown [10.57.8.23]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 1013B3F5A1; Thu, 8 Feb 2024 03:56:35 -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, xuewen.yan94@gmail.com Subject: [PATCH v8 11/23] PM: EM: Introduce em_dev_update_perf_domain() for EM updates Date: Thu, 8 Feb 2024 11:55:45 +0000 Message-Id: <20240208115557.1273962-12-lukasz.luba@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240208115557.1273962-1-lukasz.luba@arm.com> References: <20240208115557.1273962-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790331945911961435 X-GMAIL-MSGID: 1790331945911961435 Add API function em_dev_update_perf_domain() which allows the EM to be changed safely. Concurrent updaters are serialized with a mutex and the removal of memory that will not be used any more is carried out with the help of RCU. Reviewed-by: Dietmar Eggemann Tested-by: Dietmar Eggemann Signed-off-by: Lukasz Luba --- include/linux/energy_model.h | 8 +++++++ kernel/power/energy_model.c | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h index 27911dc1887e..324a3a8e0a2d 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_table_alloc(struct em_perf_domain *pd) return NULL; } static inline void em_table_free(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 16795743f969..667619b70be7 100644 --- a/kernel/power/energy_model.c +++ b/kernel/power/energy_model.c @@ -209,6 +209,50 @@ 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 + * @new_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 a mutex to serialize writers, so it must not be called + * from a non-sleeping context. + * + * Return 0 on success or an error code on 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; + + if (!dev) + return -EINVAL; + + /* Serialize update/unregister or concurrent updates */ + mutex_lock(&em_pd_mutex); + + if (!dev->em_pd) { + mutex_unlock(&em_pd_mutex); + return -EINVAL; + } + pd = dev->em_pd; + + kref_get(&new_table->kref); + + old_table = pd->em_table; + rcu_assign_pointer(pd->em_table, new_table); + + em_cpufreq_update_efficiencies(dev, new_table->state); + + em_table_free(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;