From patchwork Fri Jun 19 11:48:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 50083 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f200.google.com (mail-lb0-f200.google.com [209.85.217.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 23FCB218C9 for ; Fri, 19 Jun 2015 11:49:16 +0000 (UTC) Received: by lbbti3 with SMTP id ti3sf27760645lbb.1 for ; Fri, 19 Jun 2015 04:49:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:in-reply-to:references :sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=6o3XndvQ4m1Hdywhm4GZjpaUZUYbZWfVyPteaFG8xpw=; b=N54TI2vB/OzzUh3Ee/xJq0vTUag2bqC6WZ00fIjlJ0X5cIdoVn+XPD9MvHJj8+ts8B VQeg+fpJXClazgHnmte/rNXAEdp8zUc4IN1MWJ/Xfo7GAMz3pAnDIPt1D/YCcf7DFD1o X+JIujSxPnK2+0MnrHRIu4cZwCIdDzDD/Cwl1GaB5juGlavdvRYBJvzHiCK2pco4XE2b Izz4cAV9i6rBa937yMmVWs0qDrHuybiTErOCAw7JjaKTYYp9gijc0rl68BaqECUc1RWR G1v1ok9+qwTsBzc6mgK8E4KN7atmm4mLYdXpol4YQzCoPejg6qfM62xVPQ+/ic76Jqgc 4beg== X-Gm-Message-State: ALoCoQklw5oftZ1ozLmqZPDnGCHS41gOVuw9rETXmTZb/3jwoHYWTweKdl0G+WIbhzUN3zCe5a1+ X-Received: by 10.180.89.231 with SMTP id br7mr2983887wib.0.1434714555100; Fri, 19 Jun 2015 04:49:15 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.36.136 with SMTP id q8ls667151laj.91.gmail; Fri, 19 Jun 2015 04:49:14 -0700 (PDT) X-Received: by 10.152.37.136 with SMTP id y8mr13905188laj.21.1434714554934; Fri, 19 Jun 2015 04:49:14 -0700 (PDT) Received: from mail-lb0-f173.google.com (mail-lb0-f173.google.com. [209.85.217.173]) by mx.google.com with ESMTPS id mh3si8977325lbb.59.2015.06.19.04.49.14 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Jun 2015 04:49:14 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.173 as permitted sender) client-ip=209.85.217.173; Received: by lbbqq2 with SMTP id qq2so70452072lbb.3 for ; Fri, 19 Jun 2015 04:49:14 -0700 (PDT) X-Received: by 10.112.155.197 with SMTP id vy5mr17197033lbb.29.1434714554834; Fri, 19 Jun 2015 04:49:14 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.108.230 with SMTP id hn6csp529295lbb; Fri, 19 Jun 2015 04:49:13 -0700 (PDT) X-Received: by 10.70.130.35 with SMTP id ob3mr31124268pdb.64.1434714549198; Fri, 19 Jun 2015 04:49:09 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z2si16100144pde.8.2015.06.19.04.49.08; Fri, 19 Jun 2015 04:49:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751022AbbFSLs7 (ORCPT + 11 others); Fri, 19 Jun 2015 07:48:59 -0400 Received: from mail-pa0-f48.google.com ([209.85.220.48]:33652 "EHLO mail-pa0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751477AbbFSLs6 (ORCPT ); Fri, 19 Jun 2015 07:48:58 -0400 Received: by padev16 with SMTP id ev16so83994203pad.0 for ; Fri, 19 Jun 2015 04:48:58 -0700 (PDT) X-Received: by 10.68.200.69 with SMTP id jq5mr30962724pbc.111.1434714538363; Fri, 19 Jun 2015 04:48:58 -0700 (PDT) Received: from localhost ([122.167.70.98]) by mx.google.com with ESMTPSA id r4sm11078042pap.8.2015.06.19.04.48.57 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 19 Jun 2015 04:48:57 -0700 (PDT) From: Viresh Kumar To: Rafael Wysocki , Preeti U Murthy , ke.wang@spreadtrum.com Cc: linaro-kernel@lists.linaro.org, linux-pm@vger.kernel.org, ego@linux.vnet.ibm.com, paulus@samba.org, shilpa.bhat@linux.vnet.ibm.com, prarit@redhat.com, robert.schoene@tu-dresden.de, skannan@codeaurora.org, Viresh Kumar Subject: [PATCH V2 08/10] cpufreq: governor: Avoid invalid states with additional checks Date: Fri, 19 Jun 2015 17:18:08 +0530 Message-Id: <2c24de3653ffb3c48a5dfcfb9ea52a753456481a.1434713657.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.4.0 In-Reply-To: References: In-Reply-To: References: Sender: linux-pm-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: viresh.kumar@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.173 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , There can be races where the request has come to a wrong state. For example INIT followed by STOP (instead of START) or START followed by EXIT (instead of STOP). Tackle these races by comparing making sure the state-machine never gets into any invalid state. And return error if an invalid state-transition is requested. Signed-off-by: Viresh Kumar --- drivers/cpufreq/cpufreq_governor.c | 46 +++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/drivers/cpufreq/cpufreq_governor.c b/drivers/cpufreq/cpufreq_governor.c index 441150dea078..8d4c86fa43ec 100644 --- a/drivers/cpufreq/cpufreq_governor.c +++ b/drivers/cpufreq/cpufreq_governor.c @@ -305,6 +305,10 @@ static int cpufreq_governor_init(struct cpufreq_policy *policy, unsigned int latency; int ret; + /* State should be equivalent to EXIT */ + if (policy->governor_data) + return -EBUSY; + if (dbs_data) { if (WARN_ON(have_governor_per_policy())) return -EINVAL; @@ -375,10 +379,15 @@ static int cpufreq_governor_init(struct cpufreq_policy *policy, return ret; } -static void cpufreq_governor_exit(struct cpufreq_policy *policy, - struct dbs_data *dbs_data) +static int cpufreq_governor_exit(struct cpufreq_policy *policy, + struct dbs_data *dbs_data) { struct common_dbs_data *cdata = dbs_data->cdata; + struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs(policy->cpu); + + /* State should be equivalent to INIT */ + if (!cdbs->ccdbs || cdbs->ccdbs->policy) + return -EBUSY; policy->governor_data = NULL; if (!--dbs_data->usage_count) { @@ -395,6 +404,7 @@ static void cpufreq_governor_exit(struct cpufreq_policy *policy, } free_ccdbs(policy, cdata); + return 0; } static int cpufreq_governor_start(struct cpufreq_policy *policy, @@ -409,6 +419,10 @@ static int cpufreq_governor_start(struct cpufreq_policy *policy, if (!policy->cur) return -EINVAL; + /* State should be equivalent to INIT */ + if (!ccdbs || ccdbs->policy) + return -EBUSY; + if (cdata->governor == GOV_CONSERVATIVE) { struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; @@ -465,14 +479,18 @@ static int cpufreq_governor_start(struct cpufreq_policy *policy, return 0; } -static void cpufreq_governor_stop(struct cpufreq_policy *policy, - struct dbs_data *dbs_data) +static int cpufreq_governor_stop(struct cpufreq_policy *policy, + struct dbs_data *dbs_data) { struct common_dbs_data *cdata = dbs_data->cdata; unsigned int cpu = policy->cpu; struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs(cpu); struct cpu_common_dbs_info *ccdbs = cdbs->ccdbs; + /* State should be equivalent to START */ + if (!ccdbs || !ccdbs->policy) + return -EBUSY; + gov_cancel_work(dbs_data, policy); if (cdata->governor == GOV_CONSERVATIVE) { @@ -484,17 +502,19 @@ static void cpufreq_governor_stop(struct cpufreq_policy *policy, ccdbs->policy = NULL; mutex_destroy(&ccdbs->timer_mutex); + return 0; } -static void cpufreq_governor_limits(struct cpufreq_policy *policy, - struct dbs_data *dbs_data) +static int cpufreq_governor_limits(struct cpufreq_policy *policy, + struct dbs_data *dbs_data) { struct common_dbs_data *cdata = dbs_data->cdata; unsigned int cpu = policy->cpu; struct cpu_dbs_info *cdbs = cdata->get_cpu_cdbs(cpu); + /* State should be equivalent to START */ if (!cdbs->ccdbs || !cdbs->ccdbs->policy) - return; + return -EBUSY; mutex_lock(&cdbs->ccdbs->timer_mutex); if (policy->max < cdbs->ccdbs->policy->cur) @@ -505,13 +525,15 @@ static void cpufreq_governor_limits(struct cpufreq_policy *policy, CPUFREQ_RELATION_L); dbs_check_cpu(dbs_data, cpu); mutex_unlock(&cdbs->ccdbs->timer_mutex); + + return 0; } int cpufreq_governor_dbs(struct cpufreq_policy *policy, struct common_dbs_data *cdata, unsigned int event) { struct dbs_data *dbs_data; - int ret = 0; + int ret; /* Lock governor to block concurrent initialization of governor */ mutex_lock(&cdata->mutex); @@ -531,17 +553,19 @@ int cpufreq_governor_dbs(struct cpufreq_policy *policy, ret = cpufreq_governor_init(policy, dbs_data, cdata); break; case CPUFREQ_GOV_POLICY_EXIT: - cpufreq_governor_exit(policy, dbs_data); + ret = cpufreq_governor_exit(policy, dbs_data); break; case CPUFREQ_GOV_START: ret = cpufreq_governor_start(policy, dbs_data); break; case CPUFREQ_GOV_STOP: - cpufreq_governor_stop(policy, dbs_data); + ret = cpufreq_governor_stop(policy, dbs_data); break; case CPUFREQ_GOV_LIMITS: - cpufreq_governor_limits(policy, dbs_data); + ret = cpufreq_governor_limits(policy, dbs_data); break; + default: + ret = -EINVAL; } unlock: