From patchwork Wed Mar 1 15:04:24 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 9598595 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 61774600CB for ; Wed, 1 Mar 2017 15:22:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4F7A72855C for ; Wed, 1 Mar 2017 15:22:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 42A5828579; Wed, 1 Mar 2017 15:22:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B3C482855C for ; Wed, 1 Mar 2017 15:22:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752012AbdCAPWt (ORCPT ); Wed, 1 Mar 2017 10:22:49 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48276 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751740AbdCAPWr (ORCPT ); Wed, 1 Mar 2017 10:22:47 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A9822C04BD2B; Wed, 1 Mar 2017 15:10:53 +0000 (UTC) Received: from amt.cnet (vpn1-4-106.gru2.redhat.com [10.97.4.106]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v21FAqAl028661; Wed, 1 Mar 2017 10:10:53 -0500 Received: from amt.cnet (localhost [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id 6C8F2100A41; Wed, 1 Mar 2017 12:10:23 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.7/8.14.7/Submit) id v21FAJRE019429; Wed, 1 Mar 2017 12:10:19 -0300 Message-Id: <20170301150927.970832783@redhat.com> User-Agent: quilt/0.60-1 Date: Wed, 01 Mar 2017 12:04:24 -0300 From: Marcelo Tosatti To: kvm@vger.kernel.org, linux-pm@lists.linux-foundation.org Cc: Paolo Bonzini , Radim Krcmar , "Rafael J. Wysocki" , Viresh Kumar , Marcelo Tosatti Subject: [patch 1/3] cpufreq: implement min/max/up/down functions References: <20170301150423.915563751@redhat.com> Content-Disposition: inline; filename=1 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 01 Mar 2017 15:10:53 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Implement functions in cpufreq userspace code to: * Change current frequency to {max,min,up,down} frequencies. up/down being relative to current one. These will be used to implement KVM hypercalls for the guest to issue frequency changes. Current situation with DPDK and frequency changes is as follows: An algorithm in the guest decides when to increase/decrease frequency based on the queue length of the device. On the host, a power manager daemon is used to listen for frequency change requests (on another core) and issue these requests. However frequency changes are performance sensitive events because: On a change from low load condition to max load condition, the frequency should be raised as soon as possible. Sending a virtio-serial notification to another pCPU, waiting for that pCPU to initiate an IPI to the requestor pCPU to change frequency, is slower and more cache costly than a direct hypercall to host to switch the frequency. Moreover, if the pCPU where the power manager daemon is running is not busy spinning on requests from the isolated DPDK vcpus, there is also the cost of HLT wakeup for that pCPU. Instructions to setup: Disable the intel_pstate driver (intel_pstate=disable host kernel command line option), and set cpufreq userspace governor for the isolated pCPU. Signed-off-by: Marcelo Tosatti --- drivers/cpufreq/cpufreq_userspace.c | 172 ++++++++++++++++++++++++++++++++++++ include/linux/cpufreq.h | 7 + 2 files changed, 179 insertions(+) Index: kvm-pvfreq/drivers/cpufreq/cpufreq_userspace.c =================================================================== --- kvm-pvfreq.orig/drivers/cpufreq/cpufreq_userspace.c 2017-01-31 10:41:54.102575877 -0200 +++ kvm-pvfreq/drivers/cpufreq/cpufreq_userspace.c 2017-02-02 15:32:53.456262640 -0200 @@ -118,6 +118,178 @@ mutex_unlock(&userspace_mutex); } +static int cpufreq_is_userspace_governor(int cpu) +{ + int ret; + + mutex_lock(&userspace_mutex); + ret = per_cpu(cpu_is_managed, cpu); + mutex_unlock(&userspace_mutex); + + return ret; +} + +int cpufreq_userspace_freq_up(int cpu) +{ + unsigned int curfreq, nextminfreq; + unsigned int ret = 0; + struct cpufreq_frequency_table *pos, *table; + struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); + + if (!policy) + return -EINVAL; + + if (!cpufreq_is_userspace_governor(cpu)) { + cpufreq_cpu_put(policy); + return -EINVAL; + } + + cpufreq_cpu_put(policy); + + mutex_lock(&userspace_mutex); + table = policy->freq_table; + if (!table) { + mutex_unlock(&userspace_mutex); + return -ENODEV; + } + nextminfreq = cpufreq_quick_get_max(cpu); + curfreq = policy->cur; + + cpufreq_for_each_valid_entry(pos, table) { + if (pos->frequency > curfreq && + pos->frequency < nextminfreq) + nextminfreq = pos->frequency; + } + + if (nextminfreq != curfreq) { + unsigned int *setspeed = policy->governor_data; + + *setspeed = nextminfreq; + ret = __cpufreq_driver_target(policy, nextminfreq, + CPUFREQ_RELATION_L); + } else + ret = 1; + mutex_unlock(&userspace_mutex); + + return ret; +} +EXPORT_SYMBOL_GPL(cpufreq_userspace_freq_up); + +int cpufreq_userspace_freq_down(int cpu) +{ + unsigned int curfreq, prevmaxfreq; + unsigned int ret = 0; + struct cpufreq_frequency_table *pos, *table; + struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); + + if (!policy) + return -EINVAL; + + if (!cpufreq_is_userspace_governor(cpu)) { + cpufreq_cpu_put(policy); + return -EINVAL; + } + + cpufreq_cpu_put(policy); + + mutex_lock(&userspace_mutex); + table = policy->freq_table; + if (!table) { + mutex_unlock(&userspace_mutex); + return -ENODEV; + } + prevmaxfreq = policy->min; + curfreq = policy->cur; + + cpufreq_for_each_valid_entry(pos, table) { + if (pos->frequency < curfreq && + pos->frequency > prevmaxfreq) + prevmaxfreq = pos->frequency; + } + + if (prevmaxfreq != curfreq) { + unsigned int *setspeed = policy->governor_data; + + *setspeed = prevmaxfreq; + ret = __cpufreq_driver_target(policy, prevmaxfreq, + CPUFREQ_RELATION_L); + } else + ret = 1; + mutex_unlock(&userspace_mutex); + + return ret; +} +EXPORT_SYMBOL_GPL(cpufreq_userspace_freq_down); + +int cpufreq_userspace_freq_max(int cpu) +{ + unsigned int maxfreq; + unsigned int ret = 0; + struct cpufreq_frequency_table *table; + struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); + unsigned int *setspeed = policy->governor_data; + + + if (!policy) + return -EINVAL; + + if (!cpufreq_is_userspace_governor(cpu)) { + cpufreq_cpu_put(policy); + return -EINVAL; + } + + cpufreq_cpu_put(policy); + + mutex_lock(&userspace_mutex); + table = policy->freq_table; + if (!table) { + mutex_unlock(&userspace_mutex); + return -ENODEV; + } + maxfreq = cpufreq_quick_get_max(cpu); + + *setspeed = maxfreq; + ret = __cpufreq_driver_target(policy, maxfreq, CPUFREQ_RELATION_L); + mutex_unlock(&userspace_mutex); + + return ret; +} +EXPORT_SYMBOL_GPL(cpufreq_userspace_freq_max); + +int cpufreq_userspace_freq_min(int cpu) +{ + unsigned int minfreq; + unsigned int ret = 0; + struct cpufreq_frequency_table *table; + struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); + unsigned int *setspeed = policy->governor_data; + + if (!policy) + return -EINVAL; + + if (!cpufreq_is_userspace_governor(cpu)) { + cpufreq_cpu_put(policy); + return -EINVAL; + } + minfreq = policy->min; + + cpufreq_cpu_put(policy); + + mutex_lock(&userspace_mutex); + table = policy->freq_table; + if (!table) { + mutex_unlock(&userspace_mutex); + return -ENODEV; + } + + *setspeed = minfreq; + ret = __cpufreq_driver_target(policy, minfreq, CPUFREQ_RELATION_L); + mutex_unlock(&userspace_mutex); + + return ret; +} +EXPORT_SYMBOL_GPL(cpufreq_userspace_freq_min); + static struct cpufreq_governor cpufreq_gov_userspace = { .name = "userspace", .init = cpufreq_userspace_policy_init, Index: kvm-pvfreq/include/linux/cpufreq.h =================================================================== --- kvm-pvfreq.orig/include/linux/cpufreq.h 2017-01-31 10:41:54.102575877 -0200 +++ kvm-pvfreq/include/linux/cpufreq.h 2017-01-31 14:20:00.508613672 -0200 @@ -890,4 +890,11 @@ int cpufreq_generic_init(struct cpufreq_policy *policy, struct cpufreq_frequency_table *table, unsigned int transition_latency); +#ifdef CONFIG_CPU_FREQ +int cpufreq_userspace_freq_down(int cpu); +int cpufreq_userspace_freq_up(int cpu); +int cpufreq_userspace_freq_max(int cpu); +int cpufreq_userspace_freq_min(int cpu); +#else +#endif #endif /* _LINUX_CPUFREQ_H */