Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp1473990imn; Sun, 31 Jul 2022 08:20:55 -0700 (PDT) X-Google-Smtp-Source: AGRyM1teodHtgc2cfULCu7iQCKc0iWEa1Gt9dJh39132cTm27V2KcO6AAuLcAfVnUiDz3CVh9Oky X-Received: by 2002:a63:504c:0:b0:419:d02e:f42a with SMTP id q12-20020a63504c000000b00419d02ef42amr9824741pgl.566.1659280855422; Sun, 31 Jul 2022 08:20:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659280855; cv=none; d=google.com; s=arc-20160816; b=0qNXBmuAZL9G+Yp0F/wuclAYnm3UjADgQsXm0Xu3GKHJSHt2Fl4T3Vg+ZxsWoPiMkT PzcnicJnq3Zr3dMn0yf2J6xwfDjUwjzWZwuR045XBlZCx5V6mdC8ZmSMKibIf3nMgw02 ND12otKgQ93XpU+2bxcD5ibLQ+qj6o7FrAEhMzjDttJIG3sFqoHmDy0J64LjdfmCPDvv PYXBKRn+LtawajsNefFjGZ0nZR5PP+H6Pvhvh2HUPWoQcA34i2+mPOCbUpFMRlFss9vB +xK5ElEYc13OTgY1q7UTame1qkEqc0rqrURxQwLWxvVJw8pRnuOT1Huy71CiTfIBaceH tD0g== 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=qwgOflw1XbrvVIGX1NtZYJq0Zdca6rMcXu+6e4uFK6A=; b=yJcMiHWRsGm3JDZdufXROy9MYc1Jcc0Ak+f8k0ajzleL3ycWAmZ3I622BDRtlC7ONq EWzFFx599dEw6ePQUJXnN03OYziCbFWnZ6CP1eFTAgt0itF5wmrIx9sT3SP+/U5y5dRG aWYs0SI6LqrynOceTiCbRhwBdRoHJHT//5dKhHzPa+jV/VdzVFovzCSihWcLcuGVhj28 vSO4bg2yyvkdJK6CwIjCYZLzNI9mmC/wDkrAuMzBuH61y2SKSZztrE+gyjaP11xX+TGb OcaJi40NdZolxPwfDMjwY5mOQpteuma9zznrI7M8wfMDQYVG04FeewDAFK3uOJt9jQSv U+Hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LkIxaWQ6; 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 n12-20020a63590c000000b004128ea1a97dsi10128425pgb.399.2022.07.31.08.20.39; Sun, 31 Jul 2022 08:20:55 -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=LkIxaWQ6; 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 S229530AbiGaPIP (ORCPT + 99 others); Sun, 31 Jul 2022 11:08:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229651AbiGaPIN (ORCPT ); Sun, 31 Jul 2022 11:08:13 -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 810ADDFE1 for ; Sun, 31 Jul 2022 08:08:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659280091; 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=qwgOflw1XbrvVIGX1NtZYJq0Zdca6rMcXu+6e4uFK6A=; b=LkIxaWQ61/h7LYplP996rMdKxWRXz4+LpUl3oBZY9jKS3uB7ZSbvu+eBH1FH51sJ3/y1xO 3Kqoj8TgtQ3y0cwRKsx75QNenzba++gvbRrqm9e0/Mz2rj6rX5aIepug3vpp0v9NGpURpa PQEo7/Cli22NAr3Iv1C9s6iOynlvUb8= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-661-cSECzgojN3ayQWbNcLgCaA-1; Sun, 31 Jul 2022 11:08:07 -0400 X-MC-Unique: cSECzgojN3ayQWbNcLgCaA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9D0DF3C02B7D; Sun, 31 Jul 2022 15:08:06 +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 5D8DE492C3B; Sun, 31 Jul 2022 15:08:06 +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 26VF86R8016557; Sun, 31 Jul 2022 11:08:06 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 26VF846u016553; Sun, 31 Jul 2022 11:08:04 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Sun, 31 Jul 2022 11:08:04 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Ard Biesheuvel cc: Linus Torvalds , Alexander Viro , Alan Stern , Andrea Parri , Will Deacon , Peter Zijlstra , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Daniel Lustig , Joel Fernandes , Linux Kernel Mailing List , linux-arch , linux-fsdevel@vger.kernel.org Subject: [PATCH v2] make buffer_locked provide an 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.10 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=unavailable 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 > The only problem is that test_bit doesn't provide any memory barriers. > Should we add the barrier to buffer_locked() instead of wait_on_buffer()? > Perhaps it would fix more bugs - in reiserfs, there's this piece of code: Her I'm sending the second version of the patch that changes buffer_locked to provide an acquire semantics. Mikulas From: Mikulas Patocka 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. Also, there is this pattern present several times: wait_on_buffer(bh); if (!buffer_uptodate(bh)) err = -EIO; It may be possible that buffer_uptodate is executed before wait_on_buffer and it may return spurious error. Fix these bugs by chaning the function buffer_locked to have the acquire semantics - so that code that follows buffer_locked cannot be moved before it. We must also add a read barrier after wait_on_bit_io because wait_on_bit_io doesn't provide any barrier. (perhaps, should this smp_rmb() be moved into wait_on_bit_io?) 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) +{ + unsigned long state = smp_load_acquire(&bh->b_state); + return test_bit(BH_Lock, &state); +} + #define bh_offset(bh) ((unsigned long)(bh)->b_data & ~PAGE_MASK) /* If we *know* page->private refers to buffer_heads */ Index: linux-2.6/fs/buffer.c =================================================================== --- linux-2.6.orig/fs/buffer.c +++ linux-2.6/fs/buffer.c @@ -120,6 +120,7 @@ EXPORT_SYMBOL(buffer_check_dirty_writeba void __wait_on_buffer(struct buffer_head * bh) { wait_on_bit_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE); + smp_rmb(); } EXPORT_SYMBOL(__wait_on_buffer);