Received: by 10.213.65.68 with SMTP id h4csp279254imn; Fri, 23 Mar 2018 04:36:20 -0700 (PDT) X-Google-Smtp-Source: AG47ELt+e5m8e6QvWs7Xs13Q9gXUBTJm6TM8UOpCnyyvfKJJSVF9ElkcOfgysPi4XpSz3Sg3e1l7 X-Received: by 2002:a17:902:7e09:: with SMTP id b9-v6mr28893623plm.223.1521804980741; Fri, 23 Mar 2018 04:36:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521804980; cv=none; d=google.com; s=arc-20160816; b=zeHIw68EHwAoHZoowpqkFvcbgmeeQNwBTuofJK8gadsgYmTVkBkeUkZV0S6Jmta0lp CvZ0Hv5TrwALnJPZ/NwTqZnYBsqJj0gCe9+TTRzyBVjPp916rdN4DqZCWqeaGWqE0IDA GhIm6FOCl9TW9BZKUKh+ihrDXXnS12aHRzmHJIukMAvoeOGHhStkB7UxTU1L3uhXliAW Lv+mORo7ftV0v5YnfxdFZblbasWMEjCgOkerXAnopmNd+RGlGdZK9rCpD+lrFvNvX/Xc eKEopaM/qGT2aRnzrwBBDHdATs+9FAv+3PAS18fECCo2w6F437Yw0HVJZ/YZvtaVT51o wkSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=kD61rcR3Vy+orWNZkna8M48b+eb5uEPtmp63whVzwsY=; b=hp4nNBzO3tX9zKNyzMyqIrbmiWypkpICWJcWdRq5ayvvE+9yHqOWGDtLuU2MdvkYHf iCHlS+f89ehu7QIYL/8K/dbNEgf3kks910kdanmCFXy4jhTsRMH+EOvObkryYvRI8sXa RJru693CcKHDnUelwfSad1IFOkMKNHw5C/y9CjF0FwKi/p87daJba2104Jrv37DcQ5ct x7uPQg+glhiI4mfQK+YDRZfElx0zFN013Szf962UF260RkvDv8knvGVkpgIz43X/if1g tsVL85ewnJhbdDnT9gApaRbH1e0qi0Jv5GmzSjAI7+9zyRch6OuSbrH0WMY8F36JEHXo Tnmw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z189si5892617pgb.220.2018.03.23.04.36.05; Fri, 23 Mar 2018 04:36:20 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755026AbeCWLfQ (ORCPT + 99 others); Fri, 23 Mar 2018 07:35:16 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:39546 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754903AbeCWKFM (ORCPT ); Fri, 23 Mar 2018 06:05:12 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 4CAD513DA; Fri, 23 Mar 2018 10:05:11 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Thomas Gleixner , Viresh Kumar , Fenghua Yu , Tony Luck , Herbert Xu , "Rafael J. Wysocki" , Peter Zijlstra , Benjamin Herrenschmidt , Sebastian Siewior , linux-pm@vger.kernel.org, Lai Jiangshan , Michael Ellerman , Tejun Heo , "David S. Miller" , Len Brown , Sasha Levin Subject: [PATCH 4.9 018/177] cpufreq/sh: Replace racy task affinity logic Date: Fri, 23 Mar 2018 10:52:26 +0100 Message-Id: <20180323094205.998254321@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180323094205.090519271@linuxfoundation.org> References: <20180323094205.090519271@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Thomas Gleixner [ Upstream commit 205dcc1ecbc566cbc20acf246e68de3b080b3ecf ] The target() callback must run on the affected cpu. This is achieved by temporarily setting the affinity of the calling thread to the requested CPU and reset it to the original affinity afterwards. That's racy vs. concurrent affinity settings for that thread resulting in code executing on the wrong CPU. Replace it by work_on_cpu(). All call pathes which invoke the callbacks are already protected against CPU hotplug. Signed-off-by: Thomas Gleixner Acked-by: Viresh Kumar Cc: Fenghua Yu Cc: Tony Luck Cc: Herbert Xu Cc: "Rafael J. Wysocki" Cc: Peter Zijlstra Cc: Benjamin Herrenschmidt Cc: Sebastian Siewior Cc: linux-pm@vger.kernel.org Cc: Lai Jiangshan Cc: Michael Ellerman Cc: Tejun Heo Cc: "David S. Miller" Cc: Len Brown Link: http://lkml.kernel.org/r/20170412201042.958216363@linutronix.de Signed-off-by: Thomas Gleixner Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/cpufreq/sh-cpufreq.c | 45 +++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) --- a/drivers/cpufreq/sh-cpufreq.c +++ b/drivers/cpufreq/sh-cpufreq.c @@ -30,54 +30,63 @@ static DEFINE_PER_CPU(struct clk, sh_cpuclk); +struct cpufreq_target { + struct cpufreq_policy *policy; + unsigned int freq; +}; + static unsigned int sh_cpufreq_get(unsigned int cpu) { return (clk_get_rate(&per_cpu(sh_cpuclk, cpu)) + 500) / 1000; } -/* - * Here we notify other drivers of the proposed change and the final change. - */ -static int sh_cpufreq_target(struct cpufreq_policy *policy, - unsigned int target_freq, - unsigned int relation) +static long __sh_cpufreq_target(void *arg) { - unsigned int cpu = policy->cpu; + struct cpufreq_target *target = arg; + struct cpufreq_policy *policy = target->policy; + int cpu = policy->cpu; struct clk *cpuclk = &per_cpu(sh_cpuclk, cpu); - cpumask_t cpus_allowed; struct cpufreq_freqs freqs; struct device *dev; long freq; - cpus_allowed = current->cpus_allowed; - set_cpus_allowed_ptr(current, cpumask_of(cpu)); - - BUG_ON(smp_processor_id() != cpu); + if (smp_processor_id() != cpu) + return -ENODEV; dev = get_cpu_device(cpu); /* Convert target_freq from kHz to Hz */ - freq = clk_round_rate(cpuclk, target_freq * 1000); + freq = clk_round_rate(cpuclk, target->freq * 1000); if (freq < (policy->min * 1000) || freq > (policy->max * 1000)) return -EINVAL; - dev_dbg(dev, "requested frequency %u Hz\n", target_freq * 1000); + dev_dbg(dev, "requested frequency %u Hz\n", target->freq * 1000); freqs.old = sh_cpufreq_get(cpu); freqs.new = (freq + 500) / 1000; freqs.flags = 0; - cpufreq_freq_transition_begin(policy, &freqs); - set_cpus_allowed_ptr(current, &cpus_allowed); + cpufreq_freq_transition_begin(target->policy, &freqs); clk_set_rate(cpuclk, freq); - cpufreq_freq_transition_end(policy, &freqs, 0); + cpufreq_freq_transition_end(target->policy, &freqs, 0); dev_dbg(dev, "set frequency %lu Hz\n", freq); - return 0; } +/* + * Here we notify other drivers of the proposed change and the final change. + */ +static int sh_cpufreq_target(struct cpufreq_policy *policy, + unsigned int target_freq, + unsigned int relation) +{ + struct cpufreq_target data = { .policy = policy, .freq = target_freq }; + + return work_on_cpu(policy->cpu, __sh_cpufreq_target, &data); +} + static int sh_cpufreq_verify(struct cpufreq_policy *policy) { struct clk *cpuclk = &per_cpu(sh_cpuclk, policy->cpu);