Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp219755pxu; Tue, 6 Oct 2020 05:00:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzS9GbJAiWbVWF/ZfLb1lgEsDRvHJNkSz3qVA6Q08NUBwOTpavgtvzEF0BYjRpIb8tmmtgL X-Received: by 2002:adf:edd2:: with SMTP id v18mr4479440wro.242.1601985639878; Tue, 06 Oct 2020 05:00:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601985639; cv=none; d=google.com; s=arc-20160816; b=W4vE+D8ZSWxaJiXX361MVKKlh43b+MiWoZFGopfoLj/NVQ/Kb/68Z6fXNj1yZYpdI8 3+0ew2T7Kx4BVhhNpmSl5HpkUA5sfTifrQTq9pn/V+iaLMaKcEY0fIgBxik5ccdMVC4v JGXkCnuEcXrvYuLvUAOzZaBKWQZfmPksPRoL9SQjzQH6RvL8bs8RV5rUxrFWCHk2dbFh yAhC6HzpVLpzuR0KqUGMzeuQHqCQiqaSVTx22EgbZ/5A8hi9NcnOteUvTeGEHLbZ4iFC ZpWGaBCV6BUlfBDrOll/+qQ6u8dbeaXrUS+MUE62nYzTiR3RNt4EpiwLcA6p/yB9V7pO cg1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=p2t94a9Bs3U/7m5lM8YSuZKpX6YMMJfizDHN888L0r8=; b=u4WsmisbQ1dBxGZ2KpEu6/et0RvAY5Ee3qD3J6Zsmue3A5QmsT2M+gxvWPOJnqcycr aMIXcNI7b0b+6Kl3eVwrToi+2GxQXi4BS66Yj1y52PKkYfddsrJXEbhbVVeqLjeV1pnq 0s6feRNRSHptW7lw7p209DBx2xRcZyTvRu6fuWk2vPVBLde637NAfJF9GPdukL7rKjaw fGNrbVN8betCZRANi/sgnG8nRkMBx4j6bNbVjzIaVK/JKqBmDI11BXACTJ3+Dy20YGw1 93WnDZIlKTxKqvAL63FcOMz1/L9B6UDeyh8hs1svJb0MnvslUEWKcOiA7mhI6PuXvOay zWtg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h7si2200882edw.224.2020.10.06.05.00.15; Tue, 06 Oct 2020 05:00:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726391AbgJFL7N (ORCPT + 99 others); Tue, 6 Oct 2020 07:59:13 -0400 Received: from cloudserver094114.home.pl ([79.96.170.134]:45494 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726074AbgJFL7N (ORCPT ); Tue, 6 Oct 2020 07:59:13 -0400 Received: from 89-64-87-80.dynamic.chello.pl (89.64.87.80) (HELO kreacher.localnet) by serwer1319399.home.pl (79.96.170.134) with SMTP (IdeaSmtpServer 0.83.489) id 7489302f797d67c0; Tue, 6 Oct 2020 13:59:10 +0200 From: "Rafael J. Wysocki" To: Viresh Kumar Cc: LKML , Linux PM Subject: [PATCH] cpufreq: stats: Add memory barrier to store_reset() Date: Tue, 06 Oct 2020 13:59:05 +0200 Message-ID: <7155888.fM3j0pV3QS@kreacher> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki There is nothing to prevent the CPU or the compiler from reordering the writes to stats->reset_time and stats->reset_pending in store_reset(), in which case the readers of stats->reset_time may see a stale value. Moreover, on 32-bit arches the write to reset_time cannot be completed in one go, so the readers of it may see a partially updated value in that case. To prevent that from happening, add a write memory barrier between the writes to stats->reset_time and stats->reset_pending in store_reset(). Fixes: 40c3bd4cfa6f ("cpufreq: stats: Defer stats update to cpufreq_stats_record_transition()") Signed-off-by: Rafael J. Wysocki --- I couldn't convince myself that it was OK to leave the code as it was. linux-next material. --- drivers/cpufreq/cpufreq_stats.c | 7 +++++++ 1 file changed, 7 insertions(+) Index: linux-pm/drivers/cpufreq/cpufreq_stats.c =================================================================== --- linux-pm.orig/drivers/cpufreq/cpufreq_stats.c +++ linux-pm/drivers/cpufreq/cpufreq_stats.c @@ -99,6 +99,13 @@ static ssize_t store_reset(struct cpufre * avoid races. */ WRITE_ONCE(stats->reset_time, get_jiffies_64()); + /* + * The memory barrier below is to prevent the readers of reset_time from + * seeing a stale or partially updated value. Note that they both access + * reset_time only if reset_pending is 1, so corresponding read barriers + * are not needed. + */ + smp_wmb(); WRITE_ONCE(stats->reset_pending, 1); return count;