Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2919253rwb; Mon, 15 Aug 2022 14:03:46 -0700 (PDT) X-Google-Smtp-Source: AA6agR4mO9kIJka7YNFgMwuN7U+pHmMZH0ytmEKARqAs1sXGdUP/XXIUW4xAu6k8C29DN8pc05vQ X-Received: by 2002:a17:907:9619:b0:730:8ebb:9e45 with SMTP id gb25-20020a170907961900b007308ebb9e45mr11790072ejc.628.1660597426344; Mon, 15 Aug 2022 14:03:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660597426; cv=none; d=google.com; s=arc-20160816; b=ckN1iYVEdYa9d79ZKqruBecyIc7YOh7C+5/qi/jBw3Ywbq/azpE/GJ74i3kFHM6Bmc 9VzAzmcVgNbNY0M3PhOh+VCwMZmSAwk/63WERQ2xTejuTokasGWuHmj09lWLI8j92tLD vuWEVjdhMnG+mFKoTXRKwfcz5M93ipewamWAa6eIp6d5HJZfGxYilS5I68ti09AgrdxO d1XZfvXRr5QyAxCvk99s1u4PLcjo9eT9y5jr/rTfjl6FOx4aasl3NGyVQIxyyF71gd4r miYVx+3n1ornqWW0OlzWNroWtC68CiJQ4Z+pcCFsFKPgeSi9roktp2pwtz9IDP3ucZy2 8R2w== 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=BaPcAp1boiCr8Q367feubvYj3vWTKlzx4aiEY+syp7Eodn511uz4fHdlu1CXwCccXe pWnD/f50SoTtIPo+/TZDN4a/YA22EbH+SZZ2vItSzYMyyH0PoEdDVWpD+FxyLE65SPaD qs2eICqKJD5+TW+VkosXLD4hU006TSwtfpZD8OXujPH7yE46MkAzOTiJnsqI/Vr7lnZz wshMNKMwyk4yVRH6V82Peb/Bi7saOEYJplJJNfeTc4hFAOiP9jzDeNf0uCHPq1UQrsl6 yD1EFQWFvduUd5tEnXVnO+xyMIDu03vnCZ8ZgYlLlUNEdHqvJh6cMhHoQTZADchEqd01 8n1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=juQNrNZS; 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 gt34-20020a1709072da200b0073314be475fsi9059406ejc.763.2022.08.15.14.03.19; Mon, 15 Aug 2022 14:03: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=juQNrNZS; 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 S242972AbiHOUHO (ORCPT + 99 others); Mon, 15 Aug 2022 16:07:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244080AbiHOUFn (ORCPT ); Mon, 15 Aug 2022 16:05:43 -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 B23968050D; Mon, 15 Aug 2022 11:54:48 -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 CEE5861029; Mon, 15 Aug 2022 18:54:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B133CC433D6; Mon, 15 Aug 2022 18:54:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660589686; bh=qpyjHBlNmvh5qz8ZjsmPzt6EGeEiWMCvxwoRJDS94vo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=juQNrNZSTQ9pcxyiFMcpimCcn3AuqdoWRaB9IfsuJ+oxfYS5SM5HmN04KVxHjvuU/ vyYWEHlBpcs3HPYIlZ/kJq+2h4MkoN+uWsWd1DXZFZJ2NOtthjA361w6O8s+rcId0I DBN2e/d1w5EEq0R4wfg18D3NzG2cLwZBI1l3JyWg= 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.18 0017/1095] add barriers to buffer_uptodate and set_buffer_uptodate Date: Mon, 15 Aug 2022 19:50:16 +0200 Message-Id: <20220815180430.048003332@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@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 */