From patchwork Tue Nov 27 13:24:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 152127 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp1262113ljp; Tue, 27 Nov 2018 05:24:55 -0800 (PST) X-Google-Smtp-Source: AFSGD/VSP/XfN00aaSJ2RTrzoGpbhGwglB1HGSILgIdFxH72vF2rhqdWim2+O44PEdlJJpxm0XN4 X-Received: by 2002:a17:902:680f:: with SMTP id h15mr31768457plk.40.1543325095820; Tue, 27 Nov 2018 05:24:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543325095; cv=none; d=google.com; s=arc-20160816; b=zruM6t65ZXX9E0EBQo+RdIltUJYfNstQtIsu842Zy6NGsT4f5Vs0PcEhrLJsnH4lZs JLxGtc4R9Xnehy/BfvoXkdfTR5x41CSrADK646ZDbt8ayvMQL2QZ7IA0BIqqW/M7bACF F4BgAbXUyZXjKV08V6IqEX9fXRlmppJQC3bHzVFbWrOSftQW/ayjB1xRUaM3FthDF+pf iIvzuGt+QWSn4IB5cDfNIqEBu/KMVIFFPFQYCr6nxxqlN+Fh+QEQ57v/gjlzF1+6nXbE naN/wae/zZPp3I3cj4xmMN2qEQ1usj3OKJwYhx44YYA2yvwE5EGNDIgwqprlRWxI9PR1 AlKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=He3pqGo+A5MVuYP3nfDqw4JkRlqG2IIP4aWgZQSIRFM=; b=uUuY627XzTWAeRIw1rl8M+710DX/ybYueAOEQPAshWyoOo6Am8NZ9H7Ydyg82QupqT FMdQMTFbAN4xmTtK90Fdm4LD21m/fK2LnzTKuEnUHiJm29dRQuR8vcQvWMbKKgygdp3K BHRy0CvTqBej9qA7rzyHQNE0dMK18tBra/0f2RYYnfH5nzWj/CUsu2lxz7dAsERmOq5o R8TO3D4rnYA29rXQChPB29/XZaqS98ZC8gwHN031JLodwAoI6TggB8/SfbmOrb/9kWol sqBVH5OUEgsIm1AzH+cFDI55sz7KWlMuH06bsdhqi6VxM3Omn6JlmOOnQuK3eiEPAINH ELYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="EDVRI/At"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a23si3979408plm.334.2018.11.27.05.24.45; Tue, 27 Nov 2018 05:24:55 -0800 (PST) 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; dkim=pass header.i=@linaro.org header.s=google header.b="EDVRI/At"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727048AbeK1AW3 (ORCPT + 32 others); Tue, 27 Nov 2018 19:22:29 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:35907 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726893AbeK1AW2 (ORCPT ); Tue, 27 Nov 2018 19:22:28 -0500 Received: by mail-wm1-f65.google.com with SMTP id s11so22252578wmh.1 for ; Tue, 27 Nov 2018 05:24:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=He3pqGo+A5MVuYP3nfDqw4JkRlqG2IIP4aWgZQSIRFM=; b=EDVRI/AthinCP7ldegu7GhnBcZcqO7yh5Mn3mh/GYspg5L/sZFTu6IbwAhFvZnmYBG KOaIa9aN3JZzI/XcQe6pw+szL9ncITY0Q754FOQkd433jxv1mN73ZHHQpdc6Mk68yYYL uXFYbdRjtRFtl+XK3RTHegc24Uk1+R/rnZl24= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=He3pqGo+A5MVuYP3nfDqw4JkRlqG2IIP4aWgZQSIRFM=; b=KjpwkqyRa0+4IbwVU8hAllXgsvP0y8iExSoR0IPPlhWrcEu0w8T4HaWolPKo6lJDq7 SaXwXY8zHOKKjQHHLzVqND4WWhcghf1Imz2++qlwc6tlyAqz6/eiz5ddmN+iEmErgC7O 02WHyskOrv5pen2doYUYKiToP29aA2vkJObvNn5+G+bJQwVNsuLYmLg62uQnrD+wVvVz LeuSEosmzpAKKkjjytKcF5tdOAVOVkoFbGu/iEIZ47xX33nxRcdrnYQ0aaBztdBUQ1M0 PXPF+OCHvcYu8ZxUj8+2C3xR95CO7K+Hpx+9oV7D8Vxmy5v5VPJck8lfEZbtjPgUoY/w MUmw== X-Gm-Message-State: AA+aEWakBEzOBR4FUV98EE77b9+KYVhYrNiZkIQQrNrj6VpLQvZxPw/o Uhf663u4GckVGeAR5Fp9ydTQXQ== X-Received: by 2002:a1c:d912:: with SMTP id q18mr27440497wmg.122.1543325070781; Tue, 27 Nov 2018 05:24:30 -0800 (PST) Received: from localhost.localdomain (179.156.136.77.rev.sfr.net. [77.136.156.179]) by smtp.gmail.com with ESMTPSA id 80sm6137635wmv.6.2018.11.27.05.24.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 27 Nov 2018 05:24:30 -0800 (PST) From: Daniel Lezcano To: rjw@rjwysocki.net Cc: linux-kernel@vger.kernel.org, viresh.kumar@linaro.org, Sudeep Holla , Greg Kroah-Hartman , "Rafael J. Wysocki" , Ingo Molnar , "Peter Zijlstra (Intel)" , Morten Rasmussen , Juri Lelli Subject: [PATCH V5 1/2] base/drivers/arch_topology: Replace mutex with READ_ONCE / WRITE_ONCE Date: Tue, 27 Nov 2018 14:24:14 +0100 Message-Id: <1543325060-1599-1-git-send-email-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The mutex protects a per_cpu variable access. The potential race can happen only when the cpufreq governor module is loaded and at the same time the cpu capacity is changed in the sysfs. There is no real interest of using a mutex to protect a variable assignation when there is no situation where a task can take the lock and block. Replace the mutex by READ_ONCE / WRITE_ONCE. Signed-off-by: Daniel Lezcano Cc: Sudeep Holla Reviewed-by: Viresh Kumar --- drivers/base/arch_topology.c | 7 +------ include/linux/arch_topology.h | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) -- 2.7.4 Reviewed-by: Juri Lelli diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c index edfcf8d..fd5325b 100644 --- a/drivers/base/arch_topology.c +++ b/drivers/base/arch_topology.c @@ -31,12 +31,11 @@ void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq, per_cpu(freq_scale, i) = scale; } -static DEFINE_MUTEX(cpu_scale_mutex); DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE; void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity) { - per_cpu(cpu_scale, cpu) = capacity; + WRITE_ONCE(per_cpu(cpu_scale, cpu), capacity); } static ssize_t cpu_capacity_show(struct device *dev, @@ -71,10 +70,8 @@ static ssize_t cpu_capacity_store(struct device *dev, if (new_capacity > SCHED_CAPACITY_SCALE) return -EINVAL; - mutex_lock(&cpu_scale_mutex); for_each_cpu(i, &cpu_topology[this_cpu].core_sibling) topology_set_cpu_scale(i, new_capacity); - mutex_unlock(&cpu_scale_mutex); schedule_work(&update_topology_flags_work); @@ -141,7 +138,6 @@ void topology_normalize_cpu_scale(void) return; pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale); - mutex_lock(&cpu_scale_mutex); for_each_possible_cpu(cpu) { pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n", cpu, raw_capacity[cpu]); @@ -151,7 +147,6 @@ void topology_normalize_cpu_scale(void) pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n", cpu, topology_get_cpu_scale(NULL, cpu)); } - mutex_unlock(&cpu_scale_mutex); } bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu) diff --git a/include/linux/arch_topology.h b/include/linux/arch_topology.h index d9bdc1a..12c439f 100644 --- a/include/linux/arch_topology.h +++ b/include/linux/arch_topology.h @@ -20,7 +20,7 @@ struct sched_domain; static inline unsigned long topology_get_cpu_scale(struct sched_domain *sd, int cpu) { - return per_cpu(cpu_scale, cpu); + return READ_ONCE(per_cpu(cpu_scale, cpu)); } void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity);