Received: by 10.213.65.68 with SMTP id h4csp1727268imn; Mon, 19 Mar 2018 11:34:15 -0700 (PDT) X-Google-Smtp-Source: AG47ELvSxnBFRDUeku9pZbjh37jlJsMAfQH++Rb55J5L9OlJw2itq/D+Ske7I2HmKTNDDrDsgfIW X-Received: by 10.99.96.147 with SMTP id u141mr9955530pgb.227.1521484455402; Mon, 19 Mar 2018 11:34:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521484455; cv=none; d=google.com; s=arc-20160816; b=mkajLQ5m9ZsUTHTdssZ0Am3Wbc3iNLH4YdT2PBQWUCDAuJVS+6cPdE/eGYnTM9c2hs MdD+pN+1duwd0A0ypGolpXeJzajm7yN3Ttvvq5qWCWAQ6kITcN3T6av3yZ4A2b7UVHND e0FsEOe3bSAGMwg5iG4HRslwkZlETyYRLH+uopQRWSrNcys2hvqzO3gS8WgG+5ba2/M7 nSGC6JMh3+OYhUgGKf+hSIrKcL+X/Loze77UDb1IeMgwvhCmOC7aTQhM+qfUEUNXb4XN 4divlJ5r4WbmzicGdl9vfFvKxeJuh1Pv88qX2WkJLdk9mBKCsNp8YmaMnKlXXE71BHJB aFbw== 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=j+QAicckKDvk181J0J2UtcKZPJGIhFtH1agjKDyKDUk=; b=YBXUBlAJroFqF5V7gwd+Vovz5yzm1L7DVbBXrTWnTWYTpgwNVQsMHHXvKn6PqOkvTm 93sQAXpJzc0isMkSpGH4j76xF86F2+cl7yP0xsE901AR6lHleJPe/qOp3eCwxQkLO8kb I6YILe8doryPZOy/nzy2YpEQC9EcK3xUM+Wde2lnJxvJRjbkhXpjxLXiQLiV80FRVhxl l3UCp9nFiqhLYTYjJvY+jIUVbW/wNSU4qP8m7KaQW17y0zbYolv+NCDA8Kw1HErpal/C 5rutgnWGD7qb9lZ2qvZK4nLoMGM0W+IgKRpFgCefCHzqfXWqPaIPe5UkUPdvleWk6xf7 IYiw== 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 i35-v6si409447plg.48.2018.03.19.11.33.59; Mon, 19 Mar 2018 11:34:15 -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 S970307AbeCSScd (ORCPT + 99 others); Mon, 19 Mar 2018 14:32:33 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:53940 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934606AbeCSScY (ORCPT ); Mon, 19 Mar 2018 14:32:24 -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 2A95012AD; Mon, 19 Mar 2018 18:32:24 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nikolay Borisov , Mathieu Desnoyers , David Sterba Subject: [PATCH 4.15 37/52] btrfs: Fix memory barriers usage with device stats counters Date: Mon, 19 Mar 2018 19:08:35 +0100 Message-Id: <20180319180737.073560552@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180319180734.976730813@linuxfoundation.org> References: <20180319180734.976730813@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 4.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Nikolay Borisov commit 9deae9689231964972a94bb56a79b669f9d47ac1 upstream. Commit addc3fa74e5b ("Btrfs: Fix the problem that the dirty flag of dev stats is cleared") reworked the way device stats changes are tracked. A new atomic dev_stats_ccnt counter was introduced which is incremented every time any of the device stats counters are changed. This serves as a flag whether there are any pending stats changes. However, this patch only partially implemented the correct memory barriers necessary: - It only ordered the stores to the counters but not the reads e.g. btrfs_run_dev_stats - It completely omitted any comments documenting the intended design and how the memory barriers pair with each-other This patch provides the necessary comments as well as adds a missing smp_rmb in btrfs_run_dev_stats. Furthermore since dev_stats_cnt is only a snapshot at best there was no point in reading the counter twice - once in btrfs_dev_stats_dirty and then again when assigning stats_cnt. Just collapse both reads into 1. Fixes: addc3fa74e5b ("Btrfs: Fix the problem that the dirty flag of dev stats is cleared") Signed-off-by: Nikolay Borisov Reviewed-by: Mathieu Desnoyers Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/volumes.c | 18 ++++++++++++++++-- fs/btrfs/volumes.h | 12 ++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -7093,10 +7093,24 @@ int btrfs_run_dev_stats(struct btrfs_tra mutex_lock(&fs_devices->device_list_mutex); list_for_each_entry(device, &fs_devices->devices, dev_list) { - if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device)) + stats_cnt = atomic_read(&device->dev_stats_ccnt); + if (!device->dev_stats_valid || stats_cnt == 0) continue; - stats_cnt = atomic_read(&device->dev_stats_ccnt); + + /* + * There is a LOAD-LOAD control dependency between the value of + * dev_stats_ccnt and updating the on-disk values which requires + * reading the in-memory counters. Such control dependencies + * require explicit read memory barriers. + * + * This memory barriers pairs with smp_mb__before_atomic in + * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full + * barrier implied by atomic_xchg in + * btrfs_dev_stats_read_and_reset + */ + smp_rmb(); + ret = update_dev_stat_item(trans, fs_info, device); if (!ret) atomic_sub(stats_cnt, &device->dev_stats_ccnt); --- a/fs/btrfs/volumes.h +++ b/fs/btrfs/volumes.h @@ -498,6 +498,12 @@ static inline void btrfs_dev_stat_inc(st int index) { atomic_inc(dev->dev_stat_values + index); + /* + * This memory barrier orders stores updating statistics before stores + * updating dev_stats_ccnt. + * + * It pairs with smp_rmb() in btrfs_run_dev_stats(). + */ smp_mb__before_atomic(); atomic_inc(&dev->dev_stats_ccnt); } @@ -523,6 +529,12 @@ static inline void btrfs_dev_stat_set(st int index, unsigned long val) { atomic_set(dev->dev_stat_values + index, val); + /* + * This memory barrier orders stores updating statistics before stores + * updating dev_stats_ccnt. + * + * It pairs with smp_rmb() in btrfs_run_dev_stats(). + */ smp_mb__before_atomic(); atomic_inc(&dev->dev_stats_ccnt); }