Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1379066imm; Sat, 4 Aug 2018 02:10:33 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeVmOrjwhYBJUzaDE2WsSIrm0Ac5FE+/MIVbD+1zWLIzA2JzsQl+/FXpk3BLaeJGsYRghSQ X-Received: by 2002:a17:902:7147:: with SMTP id u7-v6mr6575444plm.154.1533373833928; Sat, 04 Aug 2018 02:10:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533373833; cv=none; d=google.com; s=arc-20160816; b=TwGoBkpwVKpWoteZ4OJBtIAO8c6YDYBTX+MgmdPCxhdmk1UzFuEPXef80+7iX0MF6A TSdclNs8LfY3RKKCaH8ThzIUzLdMDHcEWajPat3U8w5HFmUlwGPREuiaXs9KGXGsAEuD dcUKOvCojaPvqrkuy+XmsIVhtw0hm2FZeDTUOnjcr6FwbXtmmwDdYYAC0OpOYQ5mCxm4 Fc5AtP95xAlxTbIFgcqJz3uOS/edqUOPazhC4MnGfZ6tfD0h/7vuGpYMWpA85EajBcpo iR0PypHBLPXKDNpMlYwr3Vf5MbvIOP6HKr23Y3PWb16CM1OKRgJaHZ23/x1FlgzQOdtK HXlw== 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=8rZdBgV3zo0cor0hen8og939iNR6ZuyL19chzrkUv1g=; b=h7eW/Kvlj/+hwhJHYWe47k4URyQSqIIt15hkzYvVMRO9MaRXVItUPsS7rsSIM4/8se 0fY78l5tPLzZChbWg9DqKRJuNWLDCj0JNx9DEerV1/IdC1oPTkUSGMzZutyba+Xjnrv2 5rJ8VYe4VDW2See0O4i+2VqIds7ZGbOr0G5IfN4azt1A0jY1gaJyVfZLTU9s8oLAerYk M9JB6sMcf8tbx+8RBRsUqUtBRGQ/PbxL+XYVTVXwTqBLrOJgJnF0C4EAPMT2zjzKeVVJ RlzPLbjupS/XIbH5tNO0vM0Qdhie9OXzBdVrw36NOlolnFHTgiMqiYR3oeL1tudClEvc s09Q== 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 h16-v6si6010502plr.445.2018.08.04.02.10.19; Sat, 04 Aug 2018 02:10:33 -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 S2387713AbeHDLH5 (ORCPT + 99 others); Sat, 4 Aug 2018 07:07:57 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:49482 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732384AbeHDLH5 (ORCPT ); Sat, 4 Aug 2018 07:07:57 -0400 Received: from localhost (D57E6652.static.ziggozakelijk.nl [213.126.102.82]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id F388DBB3; Sat, 4 Aug 2018 09:07:57 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nikolay Borisov , David Sterba , Sasha Levin Subject: [PATCH 4.4 034/124] btrfs: add barriers to btrfs_sync_log before log_commit_wait wakeups Date: Sat, 4 Aug 2018 11:00:23 +0200 Message-Id: <20180804082703.712833610@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180804082702.434482435@linuxfoundation.org> References: <20180804082702.434482435@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.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Sterba [ Upstream commit 3d3a2e610ea5e7c6d4f9481ecce5d8e2d8317843 ] Currently the code assumes that there's an implied barrier by the sequence of code preceding the wakeup, namely the mutex unlock. As Nikolay pointed out: I think this is wrong (not your code) but the original assumption that the RELEASE semantics provided by mutex_unlock is sufficient. According to memory-barriers.txt: Section 'LOCK ACQUISITION FUNCTIONS' states: (2) RELEASE operation implication: Memory operations issued before the RELEASE will be completed before the RELEASE operation has completed. Memory operations issued after the RELEASE *may* be completed before the RELEASE operation has completed. (I've bolded the may portion) The example given there: As an example, consider the following: *A = a; *B = b; ACQUIRE *C = c; *D = d; RELEASE *E = e; *F = f; The following sequence of events is acceptable: ACQUIRE, {*F,*A}, *E, {*C,*D}, *B, RELEASE So if we assume that *C is modifying the flag which the waitqueue is checking, and *E is the actual wakeup, then those accesses can be re-ordered... IMHO this code should be considered broken... Signed-off-by: Greg Kroah-Hartman --- To be on the safe side, add the barriers. The synchronization logic around log using the mutexes and several other threads does not make it easy to reason for/against the barrier. CC: Nikolay Borisov Link: https://lkml.kernel.org/r/6ee068d8-1a69-3728-00d1-d86293d43c9f@suse.com Reviewed-by: Nikolay Borisov Signed-off-by: David Sterba Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/tree-log.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -2961,8 +2961,11 @@ out_wake_log_root: mutex_unlock(&log_root_tree->log_mutex); /* - * The barrier before waitqueue_active is implied by mutex_unlock + * The barrier before waitqueue_active is needed so all the updates + * above are seen by the woken threads. It might not be necessary, but + * proving that seems to be hard. */ + smp_mb(); if (waitqueue_active(&log_root_tree->log_commit_wait[index2])) wake_up(&log_root_tree->log_commit_wait[index2]); out: @@ -2973,8 +2976,11 @@ out: mutex_unlock(&root->log_mutex); /* - * The barrier before waitqueue_active is implied by mutex_unlock + * The barrier before waitqueue_active is needed so all the updates + * above are seen by the woken threads. It might not be necessary, but + * proving that seems to be hard. */ + smp_mb(); if (waitqueue_active(&root->log_commit_wait[index1])) wake_up(&root->log_commit_wait[index1]); return ret;