Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp1887009rwb; Fri, 19 Aug 2022 11:04:46 -0700 (PDT) X-Google-Smtp-Source: AA6agR4P+Z9s42yR1UuOlow60E8eRjj5IFwsxSkqYvZF8GTpr4QoGVG59oKjFckkSyxBN1vQLb1D X-Received: by 2002:a05:6402:3892:b0:43b:d872:a66 with SMTP id fd18-20020a056402389200b0043bd8720a66mr7026633edb.139.1660932286425; Fri, 19 Aug 2022 11:04:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660932286; cv=none; d=google.com; s=arc-20160816; b=UNANhBxLBFAMA9r9ZAsrAdErCZ3dXbzDcfDUC7WzUcXODijxaSi1nuWKxVw6KhZAF7 lf2EhoQeVtXGhRUJZnBTnmIepOPGT9P4CqYYJj0x4ijqkchcMQbO4Nj+3CPa0T4Li6w1 +XO7uBFq/nyQv6j9P4C/x61BBH6Y0Pjsu5zc542v4HnXZCEGHOLP9/yJq/mOLI5OoIRM 6wd7LY9THC/hncEmOH6AHMtgeUtGmM4dPHC/7cUP66SEC8r6OmKi3klz4dZSWQhCbUrL zGMw3bsXJXOf3colMseRQeOQrwjTacR/lGMPFxaPDsQdTcWCe+s8gb+cokt0Rgk0dWTe uEnA== 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=A+rslSfmP1arxbGTmYoD2YEwc9VMl75Dadq6fHj7ZYeJlgJovzKaGaMz7Ys8bGsjyX yqrGNrfM+VfP1o960l8uz2nW+tpBCS9V9L5D9UTNsUakrIvUt2Mk8FzRxh8bBLou+Y+h q1itbgBCNGCakNRQ4LLX3kP3fYJ2aN8A4ZY3PR7SlBXFCAt4oLI4Npec0VaebqYTKHx5 /vhUE6jN1eQNjSZoI8BYE3zaYvQlnk1n8paW6h0uEGvCD4qKVdbnwDXht0j6vqiVQycp +K5wJt/b8lFZdZDbUicVJF9pGiDbQ1UIqMVTtdVJ/cPhr1UouFbOF7crMsgfYBF5LcqS d3Vg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=TdbmYuzm; 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 g15-20020a50d0cf000000b0043d9cfe0793si3445202edf.478.2022.08.19.11.04.19; Fri, 19 Aug 2022 11:04:46 -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=TdbmYuzm; 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 S1350061AbiHSPqV (ORCPT + 99 others); Fri, 19 Aug 2022 11:46:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349957AbiHSPqG (ORCPT ); Fri, 19 Aug 2022 11:46:06 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CF1F63BA; Fri, 19 Aug 2022 08:46:05 -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 dfw.source.kernel.org (Postfix) with ESMTPS id C69D0616B5; Fri, 19 Aug 2022 15:46:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD672C433B5; Fri, 19 Aug 2022 15:46:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660923964; bh=qpyjHBlNmvh5qz8ZjsmPzt6EGeEiWMCvxwoRJDS94vo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TdbmYuzm6ydCZvYwYuiHbhTUrt2yB+Q8zlxcb5dFf/3yutg1LNAZM3ET7ftfVuZgj 8MXC22SdSVQBmZ5f5U5JGG7EsWuWBrJGRHZLQS4UANAXRhvtDpR15ihn3ix1Q3VAwu Sbpk3RLjHN0CvWzJ4F+JdVwziCB4YmCRP7oejL38= 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 5.10 011/545] add barriers to buffer_uptodate and set_buffer_uptodate Date: Fri, 19 Aug 2022 17:36:21 +0200 Message-Id: <20220819153829.681391309@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220819153829.135562864@linuxfoundation.org> References: <20220819153829.135562864@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 */