Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3022756rwb; Mon, 15 Aug 2022 16:20:13 -0700 (PDT) X-Google-Smtp-Source: AA6agR7+B8gfrbDZQDUOM8WzKZSaVi25kfQ1+9nqw47gJNK78YJ0ARdWawWorSGtobPyc8d5ErNx X-Received: by 2002:a17:907:72d1:b0:730:a0c4:2aaa with SMTP id du17-20020a17090772d100b00730a0c42aaamr11403850ejc.560.1660605613177; Mon, 15 Aug 2022 16:20:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660605613; cv=none; d=google.com; s=arc-20160816; b=mH6fi9Vc9OKpgLYcl8sYKHstRIyjpWiB+/D+xws4ihoTqIKgs/5BTqwZXXCMFc452V bzH46E9G6ge5ZT0kBQgKwEKFprYed1CNMbfU+0yoBC5I4Y0wycSyYmJvqIPlHcqW+s2Y HwV5jabGZXNKWnYdamULv30oM1H/jmlGyTCkW0Pji715V80VAsrA3+EExUwPDjKQWM1q /8GdhRveDZD9DZI1Kc5Ip4H6w/t9b6OU0rPRdyvs/fcIpg5HdUQOU0Fw3pMoP4RdIylP T8OLQswGt55C+PAq2tcwM+50hSqdjxzTbbhoPzDWQzTcOZC94gGX2qKOShRFCqLMfRrH ktGw== 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=HcK5AtlJI3skfKpfHyLzxkNAOkDPVA8dWVXsZ6gnrnUr/DwZDtbKmfCBwEhdt/GhNn lDEPVJMDP1zLre0jGJ8IFQIYQadbGDvWctI9d+GFf0JeA2dusfe5jE7pW4FZcBzUnMNM PmNgesShb1ECaskiWMCSoXtG31AqSXjLPrmC5kOZI9gXkny8S7rKs2OdQ872Zf8Tt7Jy fOE+cYlEEpviyEp2MdimCA9ap3L1+vIOExCw74vYQcdWcNlLaeU1Ik2c+dHRZciGFqrV VYYLbAybSddK8zhjVbl+xHGHP+HIczj5zgfrUdo8dDg1/G01ROuEuJ9w6Bzsy+WlB6q1 5tcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fGYuF48P; 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 bd28-20020a056402207c00b0043f2c588d55si7774106edb.339.2022.08.15.16.19.47; Mon, 15 Aug 2022 16:20:13 -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=fGYuF48P; 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 S1349519AbiHOVty (ORCPT + 99 others); Mon, 15 Aug 2022 17:49:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350047AbiHOVrG (ORCPT ); Mon, 15 Aug 2022 17:47:06 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5589574364; Mon, 15 Aug 2022 12:30:52 -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 C331DB8107A; Mon, 15 Aug 2022 19:30:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2CD9CC433D6; Mon, 15 Aug 2022 19:30:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660591849; bh=qpyjHBlNmvh5qz8ZjsmPzt6EGeEiWMCvxwoRJDS94vo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fGYuF48P3MEqJ5w9iu6mAJJiEweInGgl73TDA6UhZpVk1nCAkPgoc4+61qBTNLO4B qJcNq/UewZjir7P9GKUSjUcGzYf7QDQu+DVWOzfwsSVIfx5Fk4A3kMVzodtFhn3Yle tQIhTAH+yw54JzXUVzE4lpAA4VFhMHcJwZONPCMQ= 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.19 0017/1157] add barriers to buffer_uptodate and set_buffer_uptodate Date: Mon, 15 Aug 2022 19:49:33 +0200 Message-Id: <20220815180440.140926483@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@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 */