Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp210489rdb; Thu, 8 Feb 2024 03:59:23 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVcU999+3gJDjVH91yIgkggfOWvYs807e1/adqlW+LptNIWQjZcSPV7zCsPc1F8gsYocAI2FBuz2doLkYlTQzbmTZzu8rxArtc+3biHHw== X-Google-Smtp-Source: AGHT+IElM4RsnXIDLp8wridK926GDbKieQSms2pdMTcIxI9ziGqnufJ2U/dCAAz34laVoUXwuEA6 X-Received: by 2002:a05:620a:2091:b0:785:5ec5:a2b6 with SMTP id e17-20020a05620a209100b007855ec5a2b6mr2857446qka.31.1707393562971; Thu, 08 Feb 2024 03:59:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707393562; cv=pass; d=google.com; s=arc-20160816; b=C1UqogqCn4ooXnyL6sAgnhGGtwk1uCA5GEKvA4NDNGmvfF/Z/ZJFkguEJfkrsBsk3Z Lfe/EVIytRX2BfzuQuDMgt7UXQWBIkhNhKndQXBWyYOufY+64X9GPoHIefCYZHES4CnV RV1IwfJaEuANqlrlsoTtP6HkX+nPTGskacON6k/TCGglh7ArWgpm+kOFljvX4wkS/d1V M1mcu0FZg5GisMQsFABvJmXcYDFmb8ZVV22TXCYue4oeyOm0SGKl/9ExXnF1xp7L/8SZ q/gs1m49eu4e60Mk/YngQC2AwDPdYkbIeBCm2Jyvtrk8PfRbQjgJ08UJM/GzuGcPZbb+ IltA== 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=y0FTRhgGoeghg3P4DAY4Fd0aymLCTWCadF5o//dluwA=; fh=SOraM+JXfG0Ny0rZgnaXOKDuO9KKt4Cpo3RyRkg7q8s=; b=K1lfOE7Jpno1IrO6SkMWyRSzcK4OtS+PoNgF87tySuZcaWTnmTHuiVjYIbiBxraViz q1B0fJD4UhmPVovsKyp7zpPWrrj+WakGEO1W5E+Z82FiPSl7z5kA3fLGB1y0EzURcFna zReiRJXPU8QQLVv08hQh7CYfTh7yD878sjODEs+NhFjqmXCyuZU18Rz3o8iG8zTrWKp5 Fz860kafRV/WgcKhBHJKmlvZW+W0c6U8NIMESel+X8NQfFwQ3t8OXM9C2ptWK1CIq6Qe BYFKBq7yyxKr4Twsx+LEMssrUvkrOGc8MoYS71ZYg0JB+AKEDGZOXknST+bopc+i/s3Y DGsw==; 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-58004-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58004-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com X-Forwarded-Encrypted: i=2; AJvYcCUCRJbBYtGxQPFimjky77Wg/uNue9wkdkZFopgb/eXsPwFDFQD0shSWLzOAvk9kPG/A8eCOdznyYdPs5GQZtXi8ESQgSGM289NM0tAx5g== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id qj19-20020a05620a881300b00785ab590374si452102qkn.332.2024.02.08.03.59.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 03:59:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-58004-linux.lists.archive=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-58004-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58004-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id B5E3F1C25FD3 for ; Thu, 8 Feb 2024 11:59:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B14697994D; Thu, 8 Feb 2024 11:56:38 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2480D76C79; Thu, 8 Feb 2024 11:56:36 +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=1707393398; cv=none; b=SGduYDeHBDz8APkeAMP5xmFnLU5x2R+FF9uaW6cO0hE8V6nwDLLUKwLxNplxPlwvWIVC2U7jrjctp9R7CdX06qCHHqGSJdKTQ6HtC3MhEUFZr7SmUtNnzD2DIVejmXIinUjpC4Gk7eRpmYsMh6ijNP59fLaOX1Q0CIdhoxXPQ9M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707393398; c=relaxed/simple; bh=DiNv7mQncPSnB+eywfhrvz0BEnMEKg6b9IKEAkOD/Bg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kJpiAt10YG5b78qJHHT5ewi0/IajsXgRaId9+MLyEu3Cm7mK9v6YeTsxBPWwGhWbgbZEj2OoPIHiIbf+oHkjvVIp4BTIi73rLXeFcjGiBFwQnFSrRB6k8n65DrB3CRcstmxHNWKXvkaaDTn/vwycJLBPRLTi3hOwyKkLQsv7uh0= 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 F051B1FB; Thu, 8 Feb 2024 03:57:17 -0800 (PST) Received: from e129166.arm.com (unknown [10.57.8.23]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 1B12A3F5A1; Thu, 8 Feb 2024 03:56:32 -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 10/23] PM: EM: Add functions for memory allocations for new EM tables Date: Thu, 8 Feb 2024 11:55:44 +0000 Message-Id: <20240208115557.1273962-11-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 Content-Transfer-Encoding: 8bit The runtime modified EM table can be provided from drivers. Create mechanism which allows safely allocate and free the table for device drivers. The same table can be used by the EAS in task scheduler code paths, so make sure the memory is not freed when the device driver module is unloaded. Reviewed-by: Dietmar Eggemann Tested-by: Dietmar Eggemann Signed-off-by: Lukasz Luba --- include/linux/energy_model.h | 11 +++++++++++ kernel/power/energy_model.c | 38 +++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h index 5f842da3bb0c..27911dc1887e 100644 --- a/include/linux/energy_model.h +++ b/include/linux/energy_model.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -39,10 +40,12 @@ struct em_perf_state { /** * struct em_perf_table - Performance states table * @rcu: RCU used for safe access and destruction + * @kref: Reference counter to track the users * @state: List of performance states, in ascending order */ struct em_perf_table { struct rcu_head rcu; + struct kref kref; struct em_perf_state state[]; }; @@ -184,6 +187,8 @@ int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states, struct em_data_callback *cb, cpumask_t *span, bool microwatts); void em_dev_unregister_perf_domain(struct device *dev); +struct em_perf_table __rcu *em_table_alloc(struct em_perf_domain *pd); +void em_table_free(struct em_perf_table __rcu *table); /** * em_pd_get_efficient_state() - Get an efficient performance state from the EM @@ -365,6 +370,12 @@ static inline int em_pd_nr_perf_states(struct em_perf_domain *pd) { return 0; } +static inline +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) {} #endif #endif diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c index 131ff1d0dc5b..16795743f969 100644 --- a/kernel/power/energy_model.c +++ b/kernel/power/energy_model.c @@ -114,13 +114,36 @@ static void em_destroy_table_rcu(struct rcu_head *rp) kfree(table); } -static void em_free_table(struct em_perf_table __rcu *table) +static void em_release_table_kref(struct kref *kref) { + struct em_perf_table __rcu *table; + + /* It was the last owner of this table so we can free */ + table = container_of(kref, struct em_perf_table, kref); + call_rcu(&table->rcu, em_destroy_table_rcu); } -static struct em_perf_table __rcu * -em_allocate_table(struct em_perf_domain *pd) +/** + * em_table_free() - Handles safe free of the EM table when needed + * @table : EM table which is going to be freed + * + * No return values. + */ +void em_table_free(struct em_perf_table __rcu *table) +{ + kref_put(&table->kref, em_release_table_kref); +} + +/** + * em_table_alloc() - Allocate a new EM table + * @pd : EM performance domain for which this must be done + * + * Allocate a new EM table and initialize its kref to indicate that it + * has a user. + * Returns allocated table or NULL. + */ +struct em_perf_table __rcu *em_table_alloc(struct em_perf_domain *pd) { struct em_perf_table __rcu *table; int table_size; @@ -128,6 +151,11 @@ em_allocate_table(struct em_perf_domain *pd) table_size = sizeof(struct em_perf_state) * pd->nr_perf_states; table = kzalloc(sizeof(*table) + table_size, GFP_KERNEL); + if (!table) + return NULL; + + kref_init(&table->kref); + return table; } @@ -186,7 +214,7 @@ static int em_create_runtime_table(struct em_perf_domain *pd) struct em_perf_table __rcu *table; int table_size; - table = em_allocate_table(pd); + table = em_table_alloc(pd); if (!table) return -ENOMEM; @@ -512,7 +540,7 @@ void em_dev_unregister_perf_domain(struct device *dev) kfree(dev->em_pd->table); - em_free_table(dev->em_pd->em_table); + em_table_free(dev->em_pd->em_table); kfree(dev->em_pd); dev->em_pd = NULL; -- 2.25.1