Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp177395pxu; Tue, 6 Oct 2020 23:23:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy9fn+L8AKPLRk2pksPN5+3M6p42FZ0FtAIoma/auhmoTsXvRZ9LjQR6zOmhh0tXb5bdPDJ X-Received: by 2002:a17:907:33d2:: with SMTP id zk18mr1838135ejb.145.1602051814434; Tue, 06 Oct 2020 23:23:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602051814; cv=none; d=google.com; s=arc-20160816; b=REKnikj40NzdQU5JQzA8mIiAYRi/0E43Ip/RRmT2vFmbrlAvmDWVGuZW98nRLG3rqK EVQEIHn5F3Z/eZw8exVPO6aAjZPKV+VRHng57lTTiT1qXAofqvzM4X8bvZ0gigo4bab5 WHuX4YZ3RJfT1e9LK3YC6wrD2NO0Kvm/H3VuJ0DDShKyPBotZdE6FdMAFZ+CD2kMeHnm ZduezwCV+k/h/8PcE6mwjli6TwXBkuOhjfu5Q8kyz+vj0yLQJGnK9uqsGX3lkY5VXN6r nBlOcFl+p57M7M/Ivcm/iL3Skza7hCUAPwzL4Iy0JScpp91x8jViE4qyOYwpdMn8tJTH yWwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=ayGMg8h43lG7PbBqTH8iqf1Q66kPpyBldgC5Ro0D6M8=; b=RrZzl3g7bkzHKKDZvFMTaStgMmy0kxYBJ0mvXlFHt7Z8q3xOQ+gzH7ysZQXA8lcCiS 5JMfSpoheBJCoMFZXCsy5IGdYOaA/1VYbgZ26rHvS/3eHOhFWFldw4wStaCDaQcNB4xH HQ8pn6M/CB6w6x7LORmgzTzsR/mEzM082D5ltWgEtnzUKV+r51+cw25gekKPXloYiREA uc6cEcosQHrsdWzZkaJCJEI1wIV62zAi/DoESkP01tdONWHqcuDZ0fWVQd3KdjexL7q4 qUO/kjqK5GwDwT1vLXykV64UqeoNkS//s9ldQl1wNtiSrHb+IcVGCdzP85V3gieMjq+0 2pkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=G5cbNoRl; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jr8si850480ejb.304.2020.10.06.23.23.11; Tue, 06 Oct 2020 23:23:34 -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; dkim=pass header.i=@linaro.org header.s=google header.b=G5cbNoRl; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726493AbgJGFTF (ORCPT + 99 others); Wed, 7 Oct 2020 01:19:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726404AbgJGFTE (ORCPT ); Wed, 7 Oct 2020 01:19:04 -0400 Received: from mail-pl1-x642.google.com (mail-pl1-x642.google.com [IPv6:2607:f8b0:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56CEEC0613D2 for ; Tue, 6 Oct 2020 22:19:03 -0700 (PDT) Received: by mail-pl1-x642.google.com with SMTP id o8so415458pll.4 for ; Tue, 06 Oct 2020 22:19:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=ayGMg8h43lG7PbBqTH8iqf1Q66kPpyBldgC5Ro0D6M8=; b=G5cbNoRl9Gz+77Gjj21vI6W5SSUbGy9flyeg6kxobLbVaiGLTcBr9ER6WL7bZnUEWA Xzmk19xbrDdwr3ax06nRdYaE30ysp5i16L6k36Y41wX6NAkozvbxzhL2WANIXJrBVHZF 60Nmd447wQiVnznC8cORn6OmJxoCy7v40Nk5IPRKu1DJ7YS4IZbOs17NoUAg+vwRBNBc 11jFakeqz1kFmummy3mgoANFhzI+DJjKY38f3NXkyNrW5PI3SOhiqFZ8WIpvuzIYlnBg YDhDtgfX0oDpMf6AijVTf+hPPOfLzEfcTCqvFaQpn5cROjYuOdM4pnD314bwajil0hmm QNGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=ayGMg8h43lG7PbBqTH8iqf1Q66kPpyBldgC5Ro0D6M8=; b=hqZbuzm+HBQ6THHGmIp4B+DVMkXjOtXccJtw2MeRi/sxBJgO6iainxCpoRd2TWiuHZ D9oDV9W17SHRXreTNgy7hK8uG+mgGQMA7mSn94zVJtTRfCAbJF+wnph7kRAKibLU9asO tJIMqlgk6Dtw5og5nWzkTEaqGPyoxlUK7ZYUugr5rhKYU7UeYw5rX/sZoFYyCfUlZgop tJNUdRe7gyV/M25k96hRoOagzcpjqTzrKumzFxSoUKHksXQOmD4kyU3EITtJ3YTXRAWw KKQbQbJZi053JBAgD09q8R/ZA+gwZ5jXkrTHeWitcVueqLqD07VRslDocz9EV4QRyq85 3qwQ== X-Gm-Message-State: AOAM533tDFRjujSTs2jXng2PyJw5Es+YnFPdUjGb/4tIBqtNOkEnWIrr lBqWcCWYHQ02v/mWuJLmA5Yv9g== X-Received: by 2002:a17:90a:a595:: with SMTP id b21mr1324973pjq.95.1602047942767; Tue, 06 Oct 2020 22:19:02 -0700 (PDT) Received: from localhost ([122.181.54.133]) by smtp.gmail.com with ESMTPSA id k7sm765222pjs.9.2020.10.06.22.19.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Oct 2020 22:19:02 -0700 (PDT) Date: Wed, 7 Oct 2020 10:49:00 +0530 From: Viresh Kumar To: "Rafael J. Wysocki" Cc: LKML , Linux PM Subject: Re: [PATCH v2] cpufreq: stats: Add memory barrier to store_reset() Message-ID: <20201007051900.3t22s2gzbaedw2jb@vireshk-i7> References: <4635763.B4JZuFUhXG@kreacher> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4635763.B4JZuFUhXG@kreacher> User-Agent: NeoMutt/20180716-391-311a52 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 06-10-20, 21:43, Rafael J. Wysocki wrote: > 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; Acked-by: Viresh Kumar -- viresh