From patchwork Mon Jul 27 12:28:13 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 51520 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f70.google.com (mail-la0-f70.google.com [209.85.215.70]) by patches.linaro.org (Postfix) with ESMTPS id 6FF9F22918 for ; Mon, 27 Jul 2015 12:29:29 +0000 (UTC) Received: by laah7 with SMTP id h7sf27237766laa.2 for ; Mon, 27 Jul 2015 05:29:28 -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=EOLADnWR5SUVbcafgdt9vwj7Ge8PtKQczxG/Rh4UX3I=; b=nEwwLCQFNMh6Yl4Jq8hGhynyFQ7C15AAo/IpBQPdpeE4vTFI/fykacxgmFQ1j+mpof uMFJizceJc8iCgsDtmZTCulA3rcChioRCBBB4nv+iDbY5LJoc2laCteTCGWLBwMeyP2P jI6feODJ6hu/IBDn90rqK8NmA+lRv1BWv6mHdLF/9tcFe69Im9sTLTqGbhXthbU/3jBv mjVU85b+uBW8L9/Z6iT70LsG5FhLCRi4dTF7evcG6uTyJCY0tFgI3cxh63OMWgCJPJRk /a3kG31LJcPNmwdlzjq2Rk6rWb+vmUKB1/eD5l7MBpUYrR8LrVcI5Aus3f5G3ZHHs8vK ZfGw== X-Gm-Message-State: ALoCoQkqZiL5nDLrfS8YXD5cmDeGbHTeHLSE7GnWuCgpxCCjxqFk7+wQwc8XkZRgy79/1aVg+6Td X-Received: by 10.112.171.41 with SMTP id ar9mr12082836lbc.24.1438000168468; Mon, 27 Jul 2015 05:29:28 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.153.8.171 with SMTP id dl11ls560140lad.28.gmail; Mon, 27 Jul 2015 05:29:28 -0700 (PDT) X-Received: by 10.152.10.97 with SMTP id h1mr26917839lab.45.1438000168221; Mon, 27 Jul 2015 05:29:28 -0700 (PDT) Received: from mail-lb0-f171.google.com (mail-lb0-f171.google.com. [209.85.217.171]) by mx.google.com with ESMTPS id i5si15127031lam.8.2015.07.27.05.29.28 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Jul 2015 05:29:28 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.171 as permitted sender) client-ip=209.85.217.171; Received: by lblf12 with SMTP id f12so52268204lbl.2 for ; Mon, 27 Jul 2015 05:29:28 -0700 (PDT) X-Received: by 10.112.133.72 with SMTP id pa8mr26575284lbb.117.1438000168111; Mon, 27 Jul 2015 05:29:28 -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.7.198 with SMTP id l6csp1502207lba; Mon, 27 Jul 2015 05:29:26 -0700 (PDT) X-Received: by 10.70.42.202 with SMTP id q10mr67056236pdl.41.1438000162206; Mon, 27 Jul 2015 05:29:22 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id rf7si28141008pdb.85.2015.07.27.05.29.21; Mon, 27 Jul 2015 05:29:22 -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; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753595AbbG0M3J (ORCPT + 28 others); Mon, 27 Jul 2015 08:29:09 -0400 Received: from mail-pd0-f175.google.com ([209.85.192.175]:36796 "EHLO mail-pd0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753567AbbG0M3F (ORCPT ); Mon, 27 Jul 2015 08:29:05 -0400 Received: by pdjr16 with SMTP id r16so52377678pdj.3 for ; Mon, 27 Jul 2015 05:29:05 -0700 (PDT) X-Received: by 10.70.8.131 with SMTP id r3mr69037429pda.62.1438000145400; Mon, 27 Jul 2015 05:29:05 -0700 (PDT) Received: from localhost ([122.171.186.190]) by smtp.gmail.com with ESMTPSA id ca13sm29470805pac.25.2015.07.27.05.29.04 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Mon, 27 Jul 2015 05:29:04 -0700 (PDT) From: Viresh Kumar To: Rafael Wysocki Cc: linaro-kernel@lists.linaro.org, linux-pm@vger.kernel.org, preeti.lkml@gmail.com, Viresh Kumar , linux-kernel@vger.kernel.org (open list) Subject: [PATCH V2 8/9] cpufreq: governor: Quit work-handlers early if governor is stopped Date: Mon, 27 Jul 2015 17:58:13 +0530 Message-Id: X-Mailer: git-send-email 2.4.0 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@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.171 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: , cpufreq_governor_lock is abused by using it outside of cpufreq core, i.e. in cpufreq-governors. But we didn't had a solution at that point of time, and so doing that was the only acceptable solution: 6f1e4efd882e ("cpufreq: Fix timer/workqueue corruption by protecting reading governor_enabled") The cpufreq governor core is fixed now against possible races and things are in much better shape. cpufreq core is checking for invalid state-transitions of governors in __cpufreq_governor() with help of governor_enabled flag. The governor core is already taking care of that now and so we can get rid of those extra checks in __cpufreq_governor(). To do that, we first need to get rid of the dependency on governor_enabled flag in governor core, in gov_queue_work. This patch is about getting rid of this dependency. When a CPU is hot removed we'll cancel all the delayed work items via gov_cancel_work(). Normally this will just cancels a delayed timer on each CPU that the policy is managing and the work won't run. But if the work is already running, the workqueue code will wait for the work to finish before continuing to prevent the work items from re-queuing themselves like they normally do. This scheme will work most of the time, except for the case where the work function determines that it should adjust the delay for all other CPUs that the policy is managing. If this scenario occurs, the canceling CPU will cancel its own work but queue up the other CPUs works to run. And we will enter a situation where gov_cancel_work() has returned with work being queued on few CPUs. To fix that in a different (non-hacky) way, set set shared->policy to false before trying to cancel the work. It should be updated within timer_mutex, which will prevent the work-handlers to start. Once the work-handlers finds that we are already trying to stop the governor, it will exit early. And that will prevent queuing of works again as well. Signed-off-by: Viresh Kumar --- drivers/cpufreq/cpufreq_governor.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/drivers/cpufreq/cpufreq_governor.c b/drivers/cpufreq/cpufreq_governor.c index 3ddc27764e10..bb12acff3ba6 100644 --- a/drivers/cpufreq/cpufreq_governor.c +++ b/drivers/cpufreq/cpufreq_governor.c @@ -164,17 +164,10 @@ void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy, struct cpu_dbs_info *cdbs; int cpu; - mutex_lock(&cpufreq_governor_lock); - if (!policy->governor_enabled) - goto out_unlock; - for_each_cpu(cpu, cpus) { cdbs = dbs_data->cdata->get_cpu_cdbs(cpu); mod_delayed_work_on(cpu, system_wq, &cdbs->dwork, delay); } - -out_unlock: - mutex_unlock(&cpufreq_governor_lock); } EXPORT_SYMBOL_GPL(gov_queue_work); @@ -213,14 +206,25 @@ static void dbs_timer(struct work_struct *work) struct cpu_dbs_info *cdbs = container_of(work, struct cpu_dbs_info, dwork.work); struct cpu_common_dbs_info *shared = cdbs->shared; - struct cpufreq_policy *policy = shared->policy; - struct dbs_data *dbs_data = policy->governor_data; + struct cpufreq_policy *policy; + struct dbs_data *dbs_data; unsigned int sampling_rate, delay; const struct cpumask *cpus; bool load_eval; mutex_lock(&shared->timer_mutex); + policy = shared->policy; + + /* + * Governor might already be disabled and there is no point continuing + * with the work-handler. + */ + if (!policy) + goto unlock; + + dbs_data = policy->governor_data; + if (dbs_data->cdata->governor == GOV_CONSERVATIVE) { struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; @@ -237,6 +241,7 @@ static void dbs_timer(struct work_struct *work) delay = dbs_data->cdata->gov_dbs_timer(cdbs, dbs_data, load_eval); gov_queue_work(dbs_data, policy, delay, cpus); +unlock: mutex_unlock(&shared->timer_mutex); } @@ -473,9 +478,17 @@ static int cpufreq_governor_stop(struct cpufreq_policy *policy, if (!shared || !shared->policy) return -EBUSY; + /* + * Work-handler must see this updated, as it should not proceed any + * further after governor is disabled. And so timer_mutex is taken while + * updating this value. + */ + mutex_lock(&shared->timer_mutex); + shared->policy = NULL; + mutex_unlock(&shared->timer_mutex); + gov_cancel_work(dbs_data, policy); - shared->policy = NULL; mutex_destroy(&shared->timer_mutex); return 0; }