Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp1600867imn; Sun, 31 Jul 2022 13:48:15 -0700 (PDT) X-Google-Smtp-Source: AA6agR7FtyFrLe0sliXIOFvh9Fg1y9uncFY/uSFv/jlbiRh11x0iljFvMXlLU83rBAtFJmWPqsrH X-Received: by 2002:a05:6402:3201:b0:43d:47ee:4ae2 with SMTP id g1-20020a056402320100b0043d47ee4ae2mr8041150eda.235.1659300495542; Sun, 31 Jul 2022 13:48:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659300495; cv=none; d=google.com; s=arc-20160816; b=jkEZkPx3Jqd+OeDcGcdwknHENO92UwmOwe/mSTUcoS4t9V9kPXR0cVEHk8up9TsyJ8 RNf1XYt6u1QOeyPetJw2T8zkq3k4hjOOBHc/r4gM0FvmO+NEvyH3E2bykJgkqiZdyFbj X7OCuALTVLbZNS8HwlgDJnqdl0vB1PsEc9M8eP1hJTOHinMT+YCfqOqjMhZMqkAD6dWh YblXv00bY/PxGIBP2AXXtPhcf19x3ffqshU2orTFfxWHk77UdANNXnukkzTq0MEfgrwI HOCkj6g6NFXovZt5UaQMNDX66Q56prIqHxv8Ely7eoc48zDTZKC1Lp2X8F8Ba1YgdEli M+nw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date:dkim-signature; bh=VMHQWFo4ggpQkBpOs+DHYC6SwV0uJW3IJ2nZ3pcHIQ8=; b=duAEaNQm6bLuZ+i9+6N30fij7skA0yjLh/ueUomsOzWg8hRgtNGemq01CjBwkktGFe f2Dy04TW9+oKxPWQZJ6RNhqJ3hYZJFCcTQcI9dUnMfj5A36qD06cU1JEfU3Emv8K0BQv TWp0/DYwLfNCk5DYPa0uMdvsWIARNNSEjRpbfD4aSLr/lLy8GcrCpFKt08uQwWKg1F6T uPEsP8aqUz9LKjttYe6s+dxLkPMkqgWKzlSEpUeuNCmFEb3R9u/zPUFE6TbV1d1h5ZvE cI0KCovL+ATZWZEqTuj5OnwlhmiFb29upcFwrchBponapCWBiRxFz2y3CJ80/7JzSFOq YUXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HPdLKBlg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o3-20020aa7c503000000b0043dadb7f517si426118edq.121.2022.07.31.13.47.50; Sun, 31 Jul 2022 13:48:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HPdLKBlg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238380AbiGaUnV (ORCPT + 99 others); Sun, 31 Jul 2022 16:43:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238135AbiGaUnQ (ORCPT ); Sun, 31 Jul 2022 16:43:16 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 60DAC10550 for ; Sun, 31 Jul 2022 13:43:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659300194; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=VMHQWFo4ggpQkBpOs+DHYC6SwV0uJW3IJ2nZ3pcHIQ8=; b=HPdLKBlgN2HWBIpMoNd3VWyT71/YN6iOOUuAZIkf/HZ/0ZcwLgFBjWR8ol1xvu27LWaVTe 4R16ZMj3rYsJ8vZSiJe9ghz+41T6YbuVQyrpfpDpwHixDpZLo7DtvIc2QY6yaNbP74nV6i bBx5MyrJQoXSf4UyB17S0pugxr/ig44= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-112-P-xSew1EMCWV5cjoNQjQdg-1; Sun, 31 Jul 2022 16:43:09 -0400 X-MC-Unique: P-xSew1EMCWV5cjoNQjQdg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BB01C280406F; Sun, 31 Jul 2022 20:43:08 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B04082026D64; Sun, 31 Jul 2022 20:43:08 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id 26VKh8FV007001; Sun, 31 Jul 2022 16:43:08 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 26VKh8oN006997; Sun, 31 Jul 2022 16:43:08 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Sun, 31 Jul 2022 16:43:08 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Linus Torvalds cc: Will Deacon , "Paul E. McKenney" , Ard Biesheuvel , Alexander Viro , Alan Stern , Andrea Parri , Peter Zijlstra , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , Akira Yokosawa , Daniel Lustig , Joel Fernandes , Linux Kernel Mailing List , linux-arch , linux-fsdevel@vger.kernel.org Subject: [PATCH v3 2/2] make buffer_locked provide an acquire semantics In-Reply-To: Message-ID: References: User-Agent: Alpine 2.02 (LRH 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Let's have a look at this piece of code in __bread_slow: get_bh(bh); bh->b_end_io = end_buffer_read_sync; submit_bh(REQ_OP_READ, 0, bh); wait_on_buffer(bh); if (buffer_uptodate(bh)) return bh; Neither wait_on_buffer nor buffer_uptodate contain a memory barrier. Consequently, if someone calls sb_bread and then reads the buffer data, the read of buffer data may be executed before wait_on_buffer(bh) on architectures with weak memory ordering and it may return invalid data. Fix this bug by changing the function buffer_locked to have the acquire semantics - so that code that follows buffer_locked cannot be moved before it. Signed-off-by: Mikulas Patocka Cc: stable@vger.kernel.org Index: linux-2.6/include/linux/buffer_head.h =================================================================== --- linux-2.6.orig/include/linux/buffer_head.h +++ linux-2.6/include/linux/buffer_head.h @@ -120,7 +120,6 @@ static __always_inline int test_clear_bu BUFFER_FNS(Uptodate, uptodate) BUFFER_FNS(Dirty, dirty) TAS_BUFFER_FNS(Dirty, dirty) -BUFFER_FNS(Lock, locked) BUFFER_FNS(Req, req) TAS_BUFFER_FNS(Req, req) BUFFER_FNS(Mapped, mapped) @@ -135,6 +134,17 @@ BUFFER_FNS(Meta, meta) BUFFER_FNS(Prio, prio) BUFFER_FNS(Defer_Completion, defer_completion) +static __always_inline void set_buffer_locked(struct buffer_head *bh) +{ + set_bit(BH_Lock, &bh->b_state); +} + +static __always_inline int buffer_locked(const struct buffer_head *bh) +{ + unsigned long state = smp_load_acquire(&bh->b_state); + return test_bit(BH_Lock, &state); +} + #define bh_offset(bh) ((unsigned long)(bh)->b_data & ~PAGE_MASK) /* If we *know* page->private refers to buffer_heads */