Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3407823rwa; Tue, 23 Aug 2022 04:23:35 -0700 (PDT) X-Google-Smtp-Source: AA6agR4zu6o0stSRC33w//A8IFtqvuCtExVqyU/dI3hc4ZzTbF5Ct1+N8VwLVsrRdgHVmzr9rKK4 X-Received: by 2002:a63:8548:0:b0:42a:206b:b6fd with SMTP id u69-20020a638548000000b0042a206bb6fdmr18813745pgd.556.1661253815060; Tue, 23 Aug 2022 04:23:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661253815; cv=none; d=google.com; s=arc-20160816; b=zDeUOKmezFJYp1YKElferhsw89FGiRAoJsudtivz5r4Tjdd3pDPLD4cPBd95QXHWwr TMq6PgvE7slZwf4U89+/JGM7QFqyqJjCOx+4ff7SLOGfYUIpTV8OmUEfJN2bcPVh5kSn QOnS1IQ37LikMS5cpJ3LBHlVpKMlzQhtjNX8xa/UY+WPrxlS2qSkObI0BeDTEV9eEFO3 V8RD4FtCCrf5hSTaLzzLe1kzHvusQZI6VaeM4c3ALnKHklFSzjx/+CPScMbaxFL+Vkip 0lWG9afqklAxTJnn0odpRrcEyXA4DJ8x9aHtSAKQRR8e5ZGz83yubdp5bcMbD4diOgW+ nKEQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=N5IbRPaqEwPL4xcOGzwxaNlsXvhPD8eKWBvV+qU68Sc=; b=Q9UvsrllZp6U4gAuzVg8Mk60FQlB7E0fz56FN9jPrL0QRx+6MUCVJ7DLsStt2njdGJ Gp1gbv3a5PhNTMubc9ApMcM1P71ZmmSp0impaKdqZOk/wuv5pcq0FXyA4fNb0h9eaFDD SIHus4V7b6WiH+Mk8EMbJjuGKk7Ao7aoTPopk/3k311fW7Kbzp7UuvJLrALk+F9iKtm4 8uCJFBOERzlaJUipsYj9SP2omznSVOVoBbNjLHiHG+LE7PIvT3YpWV9NOYiBpWB7PEFB 7bxQqMdnAbPmj/0gkB1jxcX5jNJfyOHwYE7OOJduX7goPh7z1yMlcYz3DH6VfBjb1k/o OQmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="QR1Wg/S1"; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mw13-20020a17090b4d0d00b001f1e9e2933asi16937879pjb.177.2022.08.23.04.23.21; Tue, 23 Aug 2022 04:23:35 -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=@linuxfoundation.org header.s=korg header.b="QR1Wg/S1"; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355093AbiHWKeZ (ORCPT + 99 others); Tue, 23 Aug 2022 06:34:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352593AbiHWKS6 (ORCPT ); Tue, 23 Aug 2022 06:18:58 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13C8480F45; Tue, 23 Aug 2022 02:01:50 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 102C6B81C4A; Tue, 23 Aug 2022 09:01:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5C9C9C433D6; Tue, 23 Aug 2022 09:01:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661245307; bh=qpyjHBlNmvh5qz8ZjsmPzt6EGeEiWMCvxwoRJDS94vo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QR1Wg/S1C8ewpzrTq9ghiA43Q8YQoDQKyOI3UzeH6Dr5fkrxJ1SMMm3DM2KTVnz5r u00ynisfPLh//vSoYvJYXrF0l5wYnWhHiBDNJstD45zY6fAZhxWC3/o8L68xZkidPI 2CLs3KqRwp1ysOxqQOB9NpNpSZl6pqE/Ml3W+GYM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mikulas Patocka , "Matthew Wilcox (Oracle)" , Linus Torvalds Subject: [PATCH 4.19 007/287] add barriers to buffer_uptodate and set_buffer_uptodate Date: Tue, 23 Aug 2022 10:22:56 +0200 Message-Id: <20220823080100.506500466@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080100.268827165@linuxfoundation.org> References: <20220823080100.268827165@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 From: Mikulas Patocka commit d4252071b97d2027d246f6a82cbee4d52f618b47 upstream. 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 any 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 adding a memory barrier to set_buffer_uptodate and an acquire barrier to buffer_uptodate (in a similar way as folio_test_uptodate and folio_mark_uptodate). Signed-off-by: Mikulas Patocka Reviewed-by: Matthew Wilcox (Oracle) Cc: stable@vger.kernel.org Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- include/linux/buffer_head.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) --- a/include/linux/buffer_head.h +++ b/include/linux/buffer_head.h @@ -117,7 +117,6 @@ static __always_inline int test_clear_bu * of the form "mark_buffer_foo()". These are higher-level functions which * do something in addition to setting a b_state bit. */ -BUFFER_FNS(Uptodate, uptodate) BUFFER_FNS(Dirty, dirty) TAS_BUFFER_FNS(Dirty, dirty) BUFFER_FNS(Lock, locked) @@ -135,6 +134,30 @@ BUFFER_FNS(Meta, meta) BUFFER_FNS(Prio, prio) BUFFER_FNS(Defer_Completion, defer_completion) +static __always_inline void set_buffer_uptodate(struct buffer_head *bh) +{ + /* + * make it consistent with folio_mark_uptodate + * pairs with smp_load_acquire in buffer_uptodate + */ + smp_mb__before_atomic(); + set_bit(BH_Uptodate, &bh->b_state); +} + +static __always_inline void clear_buffer_uptodate(struct buffer_head *bh) +{ + clear_bit(BH_Uptodate, &bh->b_state); +} + +static __always_inline int buffer_uptodate(const struct buffer_head *bh) +{ + /* + * make it consistent with folio_test_uptodate + * pairs with smp_mb__before_atomic in set_buffer_uptodate + */ + return (smp_load_acquire(&bh->b_state) & (1UL << BH_Uptodate)) != 0; +} + #define bh_offset(bh) ((unsigned long)(bh)->b_data & ~PAGE_MASK) /* If we *know* page->private refers to buffer_heads */