From patchwork Thu Feb 8 11:55:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Luba X-Patchwork-Id: 198351 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp115874dyd; Thu, 8 Feb 2024 04:06:37 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUHwVC19DWfp+jpiDu3q//HTNYuL41Q6kyYpr1cE/raqpU4UoXaqQsHMy1hHSkpmbA7qYQCB2udmrc9Z0ZVERxTbVF9tg== X-Google-Smtp-Source: AGHT+IE2dnKr6AJR9svuDMkU5LsBdv6k+6JnWA4DFpyOIoB+RIgaIpedUMy6aHT4jdG30PhwNYze X-Received: by 2002:a17:906:578b:b0:a39:34ff:d6ad with SMTP id k11-20020a170906578b00b00a3934ffd6admr1744218ejq.7.1707393997524; Thu, 08 Feb 2024 04:06:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707393997; cv=pass; d=google.com; s=arc-20160816; b=VTmOH7C1cqx2JufYuwwQbK5gGr9ri+0EXOvrujFBRjDp/uwc48IuD3PG4p5itr+kRR UNPb6EQwiez0zEb371JqaLYAGKx5sjsP9ZD/lOLG2MhV6PKzloPnvKJMpJo7djyahTRJ a/iqmvixHAeCwCxm/UtSrajxVo1fSV2Msd/5nvFqrq75taNY8cGZgYr/8QzZa6mumogD jM2Tt8rhIFaUtpm13glQ9q7RQYR6i/JKI5+oP9FSox+cO3Mlwm1Rcmu526xkevh8Jl0r APgx0jTYou2HP+N1ewSnIxx1dG+2sssW04AVNyuq4Cw2Srwp2dZ6l+u6RhvXWqqWQLO3 FOhw== 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=D/4y9ZrSa+JjcXRscAmxuSQ+HMX6BK/Z3rYUbyZUFf0=; b=ZaH0aM2OdL8d7RNZPQhaMR9+NdfY3XOI0p9nSoVGF0k7N1EerE8c9h7jccNdlONWRk pZcwMRO/OjivEXM4YxbZDYwVsDjlq7u0BvkW7MyCzQbdHQZR7HSDVq5u6z3E/AZj2FDN 2fbdNthaFF6xGacXmFYvF1zFclDdOxxo0icqEMOvf31rZ9tM/TmYyimJk0k1VTlKSf8t S0v/bA+ghO8O0HqV18rhm+ahSSjZzNyaTbbZrrHiLnfJdiGBdcHbQGCSbPoY9Sf4q1yN mWzTqnW7nsT11XOZy8cIZPfo73k4PmoRPeup0cSfX4crL2h/SkRgyWoVKnwY+t60S2Dm vLwA==; 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58004-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com X-Forwarded-Encrypted: i=2; AJvYcCUUMPQAkXq4XSPB2VSbIKjyL0ewSzQvCST36xqfEtgKvADuErXBFiXLbC4PS2YJ8mgXcXuNZgj3ubrIkY8wMvbekow+6g== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id gq8-20020a170906e24800b00a38460f89absi2174043ejb.476.2024.02.08.04.06.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 04:06:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-58004-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58004-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 9909B1F2BFED for ; Thu, 8 Feb 2024 11:59:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E43AF7A70E; Thu, 8 Feb 2024 11:56:39 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790332368358895460 X-GMAIL-MSGID: 1790332368358895460 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;