From patchwork Thu Jul 4 07:36:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 168465 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp1921707ilk; Thu, 4 Jul 2019 00:36:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqy1Or6EjTCMhnnOPfqEpvBe8tuoJnRszziKA5EjqJyDLU2T/HQHSnv8j4oFsCnJEXTWgktt X-Received: by 2002:a17:90a:22ef:: with SMTP id s102mr18519825pjc.2.1562225808566; Thu, 04 Jul 2019 00:36:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562225808; cv=none; d=google.com; s=arc-20160816; b=yLhQgb9i9J5lXEz842iJBMupRLOdmgebNQL14oKsPSsPLtux8xLCHpTUCm6CyaJDfB /ZiKu8GkOPFRIZQ7/5buNe7qNIB0xR3c3hFdONwdNhM7KqPDAqCAt1TkRaJ6s48AsYWF AU35qgRhgUFoKz9Sy/gonvwg/3jqK1kFYf5C8/sy2GAckkbCMqTlat1l6c4P09Er9mL3 gO8M0StLKg9G5Da7m8pO8g95nueMUMPjVjYKEX04WWkq27Sybv2MZnpWOmvTnUdR2Qaq OKuTSnACEV1RLVNfq+U72rqO6bTV4Y1z4/nxIAFnT6VwHImkWGM7zBZ3SxVyend2UZQg Jspg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ucpldABbW8/3jH3u+faZEWtKvMNLILFWVMZ6DzJhvvI=; b=0rUT3k7CjGh0WTXgtJFq4wkB72sqIcwlL27JpKaSZUZXD0pCwFGuJudue6GaTUYanO 5shkE56ZpwilG6TJs84dy0e3rqJ2xx07hJ1TkH7ZKYU7EkxaKnDCxHagfXnKgbD2TbQp vmdAeBgexZYbGA/jCpe2Q1aST4IZyO+A0jJAhav0HK+OMxnqCktA/BZw0pS6cIPQsIt0 6aIodlfj2AbFkZbzhRcTcZQvQOyrUybi6oJLTVdb//ud/wiKlvCZV/6URjHPnAnfu2ZJ ZhQbVvNbSlngc+D5GVmLbtGhNlYHJcgCggAqgF5RgP7UuzBExSwXtgHnTDsqxD/i5AJz x5xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=wG8etO7Y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d12si4843008pla.121.2019.07.04.00.36.48; Thu, 04 Jul 2019 00:36:48 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=wG8etO7Y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727544AbfGDHgr (ORCPT + 30 others); Thu, 4 Jul 2019 03:36:47 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:40871 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727528AbfGDHgp (ORCPT ); Thu, 4 Jul 2019 03:36:45 -0400 Received: by mail-pf1-f194.google.com with SMTP id p184so2525259pfp.7 for ; Thu, 04 Jul 2019 00:36:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ucpldABbW8/3jH3u+faZEWtKvMNLILFWVMZ6DzJhvvI=; b=wG8etO7Yt1bBZuwRtL6oYyKvU5eSay0J+oDqZ+r4qC14aa7P3aNMTqV2JQiccaxzyA 75vIiSj0uWHz+NcO50TBcru2hp0wGBe0aJXRI2gRXYYcS4XQM+CUuTgPWgDP2zYZTnG3 VHxGMj4unTfXFsFHJLAHrXYWp/cEciVO3y4P6Ol1vVxZhKyETemVIciuggIgmT4fBOnY uCVb1q0Z1ld4NQhD7Cz38yWPsUjtZY8dcTISXE5GMt3AhLbkMSkHNzPvWOyAu5rJXUwj 2hmZMlBtt0RG4kEzaXQnvJQWvgtgO37N9qLKN/fdpSzC0fLaPSFeqreBZhRxdOcQcNl5 McmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ucpldABbW8/3jH3u+faZEWtKvMNLILFWVMZ6DzJhvvI=; b=AeXJrjTGY3WAMvbbY9cyYgMq6cFkwtQ5EBcjd1EhV/B1KyVkp5JHm+BjlXqm2dlwdx px6YHppfYklhM25kVj5LIh7K3L8nD8oLqIx9GzYLbhKtk7QmT3og+fxAE6UwH2zNhTR6 IrNnjR7Y2qeLbAIhEZrNwBTjS0mAVAobcDW8s9MXbpquBAnnVU9I553sG5OM/rwsRGdl umW5DjLBLAgCfHfIvF4hbY+IdJvjKA44csofLoRCViTQVX+nKnu7rH+QGDqvYD55+8FT krUXGkYd9QN6zRAkbyHSN7r2rLVLptAzBuny3Gn5q2xL2McfZo9fxM3DGvdlB3ZWJJlH lX8Q== X-Gm-Message-State: APjAAAXlSko1ND0LgJDIX0NfJz2Vx9NqaAFkRrVfRrbqRhDDIlJaUuE+ 1VHA/+wvR97/YeohuEoZrW8ILXGkSIo= X-Received: by 2002:a63:5964:: with SMTP id j36mr39448142pgm.428.1562225804520; Thu, 04 Jul 2019 00:36:44 -0700 (PDT) Received: from localhost ([122.172.21.205]) by smtp.gmail.com with ESMTPSA id x14sm5926822pfq.158.2019.07.04.00.36.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 Jul 2019 00:36:44 -0700 (PDT) From: Viresh Kumar To: Rafael Wysocki Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , Matthias Kaehlcke , Ulf Hansson , "Rafael J . Wysocki" , linux-kernel@vger.kernel.org Subject: [PATCH V6 5/7] cpufreq: Register notifiers with the PM QoS framework Date: Thu, 4 Jul 2019 13:06:21 +0530 Message-Id: <5ad2624194baa2f53acc1f1e627eb7684c577a19.1562210705.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.21.0.rc0.269.g1a574e7a288b In-Reply-To: References: MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This registers the notifiers for min/max frequency constraints with the PM QoS framework. The constraints are also taken into consideration in cpufreq_set_policy(). This also relocates cpufreq_policy_put_kobj() as it is required to be called from cpufreq_policy_alloc() now. refresh_frequency_limits() is updated to have proper locking in place and avoid calling cpufreq_set_policy() for inactive policies. No constraints are added until now though. Reviewed-by: Matthias Kaehlcke Reviewed-by: Ulf Hansson Signed-off-by: Viresh Kumar Signed-off-by: Rafael J. Wysocki --- drivers/cpufreq/cpufreq.c | 135 ++++++++++++++++++++++++++++++-------- include/linux/cpufreq.h | 3 + 2 files changed, 109 insertions(+), 29 deletions(-) -- 2.21.0.rc0.269.g1a574e7a288b diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index ceb57af15ca0..81117e4d43cc 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -999,7 +1000,7 @@ static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu) { struct device *dev = get_cpu_device(cpu); - if (!dev) + if (unlikely(!dev)) return; if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) @@ -1117,14 +1118,20 @@ static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cp static void refresh_frequency_limits(struct cpufreq_policy *policy) { - struct cpufreq_policy new_policy = *policy; + struct cpufreq_policy new_policy; - pr_debug("updating policy for CPU %u\n", policy->cpu); + down_write(&policy->rwsem); + + if (!policy_is_inactive(policy)) { + new_policy = *policy; + pr_debug("updating policy for CPU %u\n", policy->cpu); - new_policy.min = policy->user_policy.min; - new_policy.max = policy->user_policy.max; + new_policy.min = policy->user_policy.min; + new_policy.max = policy->user_policy.max; + cpufreq_set_policy(policy, &new_policy); + } - cpufreq_set_policy(policy, &new_policy); + up_write(&policy->rwsem); } static void handle_update(struct work_struct *work) @@ -1136,11 +1143,55 @@ static void handle_update(struct work_struct *work) refresh_frequency_limits(policy); } +static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq, + void *data) +{ + struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min); + + schedule_work(&policy->update); + return 0; +} + +static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq, + void *data) +{ + struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max); + + schedule_work(&policy->update); + return 0; +} + +static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) +{ + struct kobject *kobj; + struct completion *cmp; + + down_write(&policy->rwsem); + cpufreq_stats_free_table(policy); + kobj = &policy->kobj; + cmp = &policy->kobj_unregister; + up_write(&policy->rwsem); + kobject_put(kobj); + + /* + * We need to make sure that the underlying kobj is + * actually not referenced anymore by anybody before we + * proceed with unloading. + */ + pr_debug("waiting for dropping of refcount\n"); + wait_for_completion(cmp); + pr_debug("wait complete\n"); +} + static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) { struct cpufreq_policy *policy; + struct device *dev = get_cpu_device(cpu); int ret; + if (!dev) + return NULL; + policy = kzalloc(sizeof(*policy), GFP_KERNEL); if (!policy) return NULL; @@ -1157,7 +1208,7 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, cpufreq_global_kobject, "policy%u", cpu); if (ret) { - pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret); + dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret); /* * The entire policy object will be freed below, but the extra * memory allocated for the kobject name needs to be freed by @@ -1167,6 +1218,25 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) goto err_free_real_cpus; } + policy->nb_min.notifier_call = cpufreq_notifier_min; + policy->nb_max.notifier_call = cpufreq_notifier_max; + + ret = dev_pm_qos_add_notifier(dev, &policy->nb_min, + DEV_PM_QOS_MIN_FREQUENCY); + if (ret) { + dev_err(dev, "Failed to register MIN QoS notifier: %d (%*pbl)\n", + ret, cpumask_pr_args(policy->cpus)); + goto err_kobj_remove; + } + + ret = dev_pm_qos_add_notifier(dev, &policy->nb_max, + DEV_PM_QOS_MAX_FREQUENCY); + if (ret) { + dev_err(dev, "Failed to register MAX QoS notifier: %d (%*pbl)\n", + ret, cpumask_pr_args(policy->cpus)); + goto err_min_qos_notifier; + } + INIT_LIST_HEAD(&policy->policy_list); init_rwsem(&policy->rwsem); spin_lock_init(&policy->transition_lock); @@ -1177,6 +1247,11 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) policy->cpu = cpu; return policy; +err_min_qos_notifier: + dev_pm_qos_remove_notifier(dev, &policy->nb_min, + DEV_PM_QOS_MIN_FREQUENCY); +err_kobj_remove: + cpufreq_policy_put_kobj(policy); err_free_real_cpus: free_cpumask_var(policy->real_cpus); err_free_rcpumask: @@ -1189,30 +1264,9 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) return NULL; } -static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) -{ - struct kobject *kobj; - struct completion *cmp; - - down_write(&policy->rwsem); - cpufreq_stats_free_table(policy); - kobj = &policy->kobj; - cmp = &policy->kobj_unregister; - up_write(&policy->rwsem); - kobject_put(kobj); - - /* - * We need to make sure that the underlying kobj is - * actually not referenced anymore by anybody before we - * proceed with unloading. - */ - pr_debug("waiting for dropping of refcount\n"); - wait_for_completion(cmp); - pr_debug("wait complete\n"); -} - static void cpufreq_policy_free(struct cpufreq_policy *policy) { + struct device *dev = get_cpu_device(policy->cpu); unsigned long flags; int cpu; @@ -1224,6 +1278,11 @@ static void cpufreq_policy_free(struct cpufreq_policy *policy) per_cpu(cpufreq_cpu_data, cpu) = NULL; write_unlock_irqrestore(&cpufreq_driver_lock, flags); + dev_pm_qos_remove_notifier(dev, &policy->nb_max, + DEV_PM_QOS_MAX_FREQUENCY); + dev_pm_qos_remove_notifier(dev, &policy->nb_min, + DEV_PM_QOS_MIN_FREQUENCY); + cpufreq_policy_put_kobj(policy); free_cpumask_var(policy->real_cpus); free_cpumask_var(policy->related_cpus); @@ -2283,6 +2342,8 @@ int cpufreq_set_policy(struct cpufreq_policy *policy, struct cpufreq_policy *new_policy) { struct cpufreq_governor *old_gov; + struct device *cpu_dev = get_cpu_device(policy->cpu); + unsigned long min, max; int ret; pr_debug("setting new policy for CPU %u: %u - %u kHz\n", @@ -2297,11 +2358,27 @@ int cpufreq_set_policy(struct cpufreq_policy *policy, if (new_policy->min > new_policy->max) return -EINVAL; + /* + * PM QoS framework collects all the requests from users and provide us + * the final aggregated value here. + */ + min = dev_pm_qos_read_value(cpu_dev, DEV_PM_QOS_MIN_FREQUENCY); + max = dev_pm_qos_read_value(cpu_dev, DEV_PM_QOS_MAX_FREQUENCY); + + if (min > new_policy->min) + new_policy->min = min; + if (max < new_policy->max) + new_policy->max = max; + /* verify the cpu speed can be set within this limit */ ret = cpufreq_driver->verify(new_policy); if (ret) return ret; + /* + * The notifier-chain shall be removed once all the users of + * CPUFREQ_ADJUST are moved to use the QoS framework. + */ /* adjust if necessary - all reasons */ blocking_notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST, new_policy); diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index a1467aa7f58b..95425941f46d 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -147,6 +147,9 @@ struct cpufreq_policy { /* Pointer to the cooling device if used for thermal mitigation */ struct thermal_cooling_device *cdev; + + struct notifier_block nb_min; + struct notifier_block nb_max; }; struct cpufreq_freqs {