Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2869545rwb; Mon, 15 Aug 2022 13:00:29 -0700 (PDT) X-Google-Smtp-Source: AA6agR4pyDysumoRsbXu7AdwEwXwfl23P64+wt6qnyvgBCQURmSLfiqy03v6Z63SL3lZ8ZRj0YXn X-Received: by 2002:aa7:d292:0:b0:43d:7923:66cd with SMTP id w18-20020aa7d292000000b0043d792366cdmr15126454edq.403.1660593628888; Mon, 15 Aug 2022 13:00:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660593628; cv=none; d=google.com; s=arc-20160816; b=FmTA/h3j197QCJPSLxeAkhe+HgTXu47KMY/eHz8cnxz3zuc1ZCNg+iMoz4+63LVKa+ okHJL00npKrx1mpQ89qawc8pmcPMkhYdet2FP+uSSzVmm/xfsYo4c0RVaF64oY8piaiy GzS0xn6zDzlBjqcPqNpKcik9KaYw34XWiU3Ky9DASWYJJwz93zXzi3CpjbDd/gqEYbMr IUF04v04aq7Rkk1JZQJeTZ2eH3mwmPVXXyVR7rvIjv2Bu3LuP4EwKO4/fI7u1eyaOnQp 8IjYJ0xzeNWLuYim1P6lviRN0ezXwnshlQs6HNhjZfXaFEJbGvRwxhWYTTp1vqKuvXDs ORrA== 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=EMmIFHInZ7RKnym9/gewcAiO5RRJZUSY952cvW6CtHuTvRKE+KjDZ6XbVwK6ZT25Gd WXtQmL4tbBgB0QxTr0iTxYjmX68oVQS3pFEiFL4MscgD5RJFCZ9rvxZl7318tRwPRSnL PllJmC9iIYIh/w4VDzLq8htE1FCEBhQaHL2PutjaHbYOOMnHwQ14PxHUQ1tE7T04sElz fCcjQGRRvBGjA4U9FkwP5GiOj5hEj7iv878Ru+/AHwicqCwyw2h8v0ye9Vpr6eoMw/5y yeSyrbXc9D3/3qT2w+u5F08/cOSVfotP9ak5qNRgz8l22BbZv1jfy6L1DqBubO1vjKB+ g7DA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tASzAc58; 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 cw7-20020a170906c78700b00730abba3122si6990606ejb.467.2022.08.15.13.00.02; Mon, 15 Aug 2022 13:00:28 -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=tASzAc58; 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 S233286AbiHOSNx (ORCPT + 99 others); Mon, 15 Aug 2022 14:13:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232991AbiHOSNi (ORCPT ); Mon, 15 Aug 2022 14:13:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 304BE2A268; Mon, 15 Aug 2022 11:13:38 -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 B459B6126D; Mon, 15 Aug 2022 18:13:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BA3C6C433C1; Mon, 15 Aug 2022 18:13:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660587217; bh=qpyjHBlNmvh5qz8ZjsmPzt6EGeEiWMCvxwoRJDS94vo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tASzAc58/2rfWQMC7gX3klfikvuUygZwc8pYPI5jfu4Q4CA/Q6sbdETNGfGPNFFZp VLpDEkjFsq6YDmO1VWIDvHtE2ylB6OR+pzLKKqm/4ZDCop4+NWEa7heURxhDYYRXgN Up09P9v/Cu84uTwAzcLNWHThYuqJKxyd5T2FJy1o= 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.15 015/779] add barriers to buffer_uptodate and set_buffer_uptodate Date: Mon, 15 Aug 2022 19:54:19 +0200 Message-Id: <20220815180337.838203273@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@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 */