Received: by 10.213.65.68 with SMTP id h4csp235922imn; Fri, 23 Mar 2018 03:34:43 -0700 (PDT) X-Google-Smtp-Source: AG47ELsdR9xMrxvU46V45EkMe8U54+kto1YIHXnU46jCtW6+Y6+axrdfA5/Unzoc1rJTEd9waN8w X-Received: by 10.99.121.131 with SMTP id u125mr20710217pgc.263.1521801283712; Fri, 23 Mar 2018 03:34:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521801283; cv=none; d=google.com; s=arc-20160816; b=uU+L0uwGWsWbgkCPvPVw7q/Fzp5JEioW9VfyEkVySdPy3iFxPKsTS49CJaxWoCT3Kn Q5HdKE7DSTFc/qY5mwvcy4r7k5EqH5Fwy/wKomGdHq68hLdTOOx0L0eh9zxokcCXhjNG xBRR7ZHmbqyINDjZQMKnJVtgHWOJqHHj6Gu31onQm59OtRs9c9cZFTXu3TAzvbc/388j +798HV5OVnq8Lp001sb7qB9SxQpMA8NIlSPxmazzFCsCEhQNl8Uruvb5hwnod9GWVAqJ 6CvkklVxvLt9B9l4bsvwKlFmTpP5+vQc7mLWpucUWStSdK102kANUK5KZaIdTCmgfaXd EPxQ== 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=WFdn2ljVVIt+c8nl4sE+Fw6WRa7AtfBCZmFSCHPjMAU=; b=e3krQl1/2fvzZlugEguY9oNV54D/+ZQq1qRF4C9DHKlPKFxFk0NGyw6ixd3aiQhJOb jTQzhpKY+d6UrC714ccZjluJciozUNk7aGlEQaPFd8A7krXHhce3eNRUXtBLyYbBDxQA sq/Z/T8VNFr6VmgApEAls62KRu/PgKBROfFm4xVzn1sglTzxMfjEvwWxLiEg5cES4/h2 tI/diH1MqdfED6sS/5ZLD6fp9BXDqA3jLF+SckcQLOBQMkyo4+okiejJ45UIQsMwjDNa uz+dnnk2NjHXIRHiF6Qt9joXC8l09wJdZrMmKUeff1GsInrnNJFz9krN03iqUhmGLFiQ 9VbQ== 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 a62-v6si8471835pla.185.2018.03.23.03.34.29; Fri, 23 Mar 2018 03:34:43 -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 S1754430AbeCWKd1 (ORCPT + 99 others); Fri, 23 Mar 2018 06:33:27 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:47580 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756089AbeCWKST (ORCPT ); Fri, 23 Mar 2018 06:18:19 -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 11C6BF12; Fri, 23 Mar 2018 10:18:18 +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 3.18 05/47] cpufreq/sh: Replace racy task affinity logic Date: Fri, 23 Mar 2018 10:54:56 +0100 Message-Id: <20180323094248.347689835@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180323094248.117679641@linuxfoundation.org> References: <20180323094248.117679641@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 3.18-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);