Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp1883815imn; Mon, 1 Aug 2022 03:54:28 -0700 (PDT) X-Google-Smtp-Source: AA6agR5B1T1ITS0v6A93GvJNrqqi9KAjeOlOMEgpH9aesEhWWsnK9lQpM2TcxC9VbYYKAXdJ3AvO X-Received: by 2002:a17:90a:6fe1:b0:1f3:19f7:2e2d with SMTP id e88-20020a17090a6fe100b001f319f72e2dmr19015694pjk.28.1659351268036; Mon, 01 Aug 2022 03:54:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659351268; cv=none; d=google.com; s=arc-20160816; b=Q8bHw+uOaogvfNq+/o+/d+O44cuIhWfScrfV3sUWMvR8IJFgEMfdT/2Xqi6Co7MwHR V0aii2Pwn1oIhaJTyoyh9DW03jkOCyUvb2k0zE0EANm1adcx3ZzD9L4LxjI+CMu7RaiI SrGfl6D59ExJKPSimEArPxwNBOO0Xzgy0jBkdHAqO9xqPVmFWzCKyU1yPWCTb5u6TWsD yJolr4C+N/YvUjI78wwQoTC+lZpLySjHDnpMfStInmqa1lZHqSTK+LVIcfu3Qqz4BXu6 EeynsKL3sEZhgDXLf4B1agxd6OpB0h4Q5nrbmyhQ0MHchfkC7Jd107sOesW+56kf0wBH b6Hw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date:dkim-signature; bh=AlQxFUH0VbGf7ysRiP07rHbskpU3q3i2vOryL8lB/2Y=; b=GWLz2jEgPJQSY0NQql76892Co7qITif1atzh8VFoIifqfUtaXE0G+Kt4hwPhSY1esY nCwXwQySnMoM2frDczP2R86zmBp7OxCRRRY6r4tAMkHej1oTugeCylDoFSNVxokfUBP3 W6wA3ULxIYmndPZiOsshNi+GF+k/hge2SI77BNoligfm4g+OQsGGiXLITj6O1Sko3pF8 RvD5uBpj+LGjnRMY2vy2sjt1Z0wugnAvkJqQ9LjHOAh87hfCdO5SUoUlhBCit3LJ3ctt xBLFgpkOR1O+DxiDYydSt/ommV7YdXY9wkpQp7vioVFMXnQSxmVo5V4SQrjlre6Oag01 O3Bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="gqraY9b/"; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p37-20020a056a000a2500b0051f2b9f9b3bsi12527856pfh.243.2022.08.01.03.54.13; Mon, 01 Aug 2022 03:54: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=@redhat.com header.s=mimecast20190719 header.b="gqraY9b/"; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229939AbiHAKoD (ORCPT + 99 others); Mon, 1 Aug 2022 06:44:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229921AbiHAKoB (ORCPT ); Mon, 1 Aug 2022 06:44:01 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D2A683B2 for ; Mon, 1 Aug 2022 03:44:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659350640; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=AlQxFUH0VbGf7ysRiP07rHbskpU3q3i2vOryL8lB/2Y=; b=gqraY9b/R5YieidW+Q0pv6UDfUxW21D2nPf9Ii6T/HSGIjSeS2swlngwqVB3bG/CrRrPy+ 1pf6O6L2sxcTIciqjo0o2Xv+CZhMTwYW5M7sQC9YDWMnVmsUaYVdvWREagp9ODoeH9Dtp4 /G0zOPFsB2W9foowaXcWEAeW8vWaZ1w= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-329-_iSCFO_uOoWLL4Vb4FS1ug-1; Mon, 01 Aug 2022 06:43:57 -0400 X-MC-Unique: _iSCFO_uOoWLL4Vb4FS1ug-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0105C85A581; Mon, 1 Aug 2022 10:43:56 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E9F48492C3B; Mon, 1 Aug 2022 10:43:55 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id 271AhtU7024246; Mon, 1 Aug 2022 06:43:55 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 271Ahtdk024242; Mon, 1 Aug 2022 06:43:55 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Mon, 1 Aug 2022 06:43:55 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Linus Torvalds cc: Will Deacon , "Paul E. McKenney" , Ard Biesheuvel , Alexander Viro , Alan Stern , Andrea Parri , Peter Zijlstra , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , Akira Yokosawa , Daniel Lustig , Joel Fernandes , Linux Kernel Mailing List , linux-arch , linux-fsdevel@vger.kernel.org Subject: [PATCH v4 2/2] change buffer_locked, so that it has acquire semantics In-Reply-To: Message-ID: References: User-Agent: Alpine 2.02 (LRH 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE 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 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 changing the function buffer_locked to have the acquire semantics - so that code that follows buffer_locked cannot be moved before it. Signed-off-by: Mikulas Patocka Cc: stable@vger.kernel.org Index: linux-2.6/include/linux/buffer_head.h =================================================================== --- linux-2.6.orig/include/linux/buffer_head.h +++ linux-2.6/include/linux/buffer_head.h @@ -120,7 +120,6 @@ static __always_inline int test_clear_bu BUFFER_FNS(Uptodate, uptodate) BUFFER_FNS(Dirty, dirty) TAS_BUFFER_FNS(Dirty, dirty) -BUFFER_FNS(Lock, locked) BUFFER_FNS(Req, req) TAS_BUFFER_FNS(Req, req) BUFFER_FNS(Mapped, mapped) @@ -135,6 +134,17 @@ BUFFER_FNS(Meta, meta) BUFFER_FNS(Prio, prio) BUFFER_FNS(Defer_Completion, defer_completion) +static __always_inline void set_buffer_locked(struct buffer_head *bh) +{ + set_bit(BH_Lock, &bh->b_state); +} + +static __always_inline int buffer_locked(const struct buffer_head *bh) +{ + /* pairs with smp_mb__after_atomic() in unlock_buffer() */ + return test_bit_acquire(BH_Lock, &bh->b_state); +} + #define bh_offset(bh) ((unsigned long)(bh)->b_data & ~PAGE_MASK) /* If we *know* page->private refers to buffer_heads */