Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp549106pxu; Tue, 6 Oct 2020 12:45:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyUk/bSO6NBo6gEsa8x73crwiDQxyvPQVdqMw1muSBjkBNXAkpaM6XL6cTCHqJ/HVgv+Tr8 X-Received: by 2002:a05:6402:1684:: with SMTP id a4mr5270724edv.79.1602013558090; Tue, 06 Oct 2020 12:45:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602013558; cv=none; d=google.com; s=arc-20160816; b=E4ahwr11yuN8d8+wFUO2/OtgdBOYicY0JeHR8SkAt9Bocar9RzRahI9lF+4pFf9DoV 8IKcdTCf0E5GZHkj7LjZlAoCHfNrxawP3D1Z6lnqph6ziuImsE/tWb3eYuk1BoDS5olG b48ljbKjNWhdmJf84duwqaGoaYjoFQw47lHDK2myvHP/eL3sMON/5lZ5JpYbR4x6kxJa nyVrlmSiSAVvr6GGVoqxO5WAEJewJTyVLFKAaxCEqsB/fTg91p3r74m9hHEyXMnALy+h OvZE3Ym5n5hd7UjaaB7hyVTh1hBd0WFDnnuGAp25J7n7b44NQta21VOStuE2OX4pcTP1 8kDw== 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=OtrdG19ojRvWEp1ajC2XcDNl9I1Q6xdYh44ZxvGfPT4=; b=b/2ikmeVjrpvgHwDqdlAXwGx7rFeWm0OovR825igpcHHz+R7Cl16fPg8zJWoKe9/21 15OjZFYNbnBtqTiWyy3LwtM1f7GtAxQhcE7PcFImw/vzGFY66ZHvOSJBjO3vcdz2pQ8k sAcTWiWE8q+dYXgKjTEhK3hHvjt9/5Q3OXUOty0MqsxZ2tABkkcM7KZWN3ZLq8MD6aLJ P8LdfJ5HBG6SoWPMe5sbfXY1TN0NceVXyWZpzUGF+KHGyTANMqnL8ioHGh8RQCdxYnG8 tVwplf2T97sJGS+GTdZS8rA9lFZt5Xy8TD1ykV96Stlat6UytbTx4qXylSCRa3SM+oaE bnnQ== 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 f21si3178535edy.137.2020.10.06.12.45.34; Tue, 06 Oct 2020 12:45:58 -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 S1726323AbgJFTnq (ORCPT + 99 others); Tue, 6 Oct 2020 15:43:46 -0400 Received: from cloudserver094114.home.pl ([79.96.170.134]:48368 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725943AbgJFTnq (ORCPT ); Tue, 6 Oct 2020 15:43:46 -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 3bf34580619004af; Tue, 6 Oct 2020 21:43:44 +0200 From: "Rafael J. Wysocki" To: Viresh Kumar Cc: LKML , Linux PM Subject: [PATCH v2] cpufreq: stats: Add memory barrier to store_reset() Date: Tue, 06 Oct 2020 21:43:43 +0200 Message-ID: <4635763.B4JZuFUhXG@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() and corresponding read memory barrier in the readers of stats->reset_time. Fixes: 40c3bd4cfa6f ("cpufreq: stats: Defer stats update to cpufreq_stats_record_transition()") Signed-off-by: Rafael J. Wysocki --- linux-next material. -> v2: Pair read and write memory barriers as appropriate. --- drivers/cpufreq/cpufreq_stats.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) Index: linux-pm/drivers/cpufreq/cpufreq_stats.c =================================================================== --- linux-pm.orig/drivers/cpufreq/cpufreq_stats.c +++ linux-pm/drivers/cpufreq/cpufreq_stats.c @@ -47,6 +47,11 @@ static void cpufreq_stats_reset_table(st /* Adjust for the time elapsed since reset was requested */ WRITE_ONCE(stats->reset_pending, 0); + /* + * Prevent the reset_time read from being reordered before the + * reset_pending accesses in cpufreq_stats_record_transition(). + */ + smp_rmb(); cpufreq_stats_update(stats, READ_ONCE(stats->reset_time)); } @@ -71,10 +76,16 @@ static ssize_t show_time_in_state(struct for (i = 0; i < stats->state_num; i++) { if (pending) { - if (i == stats->last_index) + if (i == stats->last_index) { + /* + * Prevent the reset_time read from occurring + * before the reset_pending read above. + */ + smp_rmb(); time = get_jiffies_64() - READ_ONCE(stats->reset_time); - else + } else { time = 0; + } } else { time = stats->time_in_state[i]; if (i == stats->last_index) @@ -99,6 +110,11 @@ 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. + */ + smp_wmb(); WRITE_ONCE(stats->reset_pending, 1); return count;