Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp1375813imn; Sun, 31 Jul 2022 04:46:53 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s9QUtT2lUJ++2mroXVhOG+ebA5GT3hk+Z58dPXuqL3+SYuaONkWcmLsJj5X1TZOZzgoGlG X-Received: by 2002:a65:46cc:0:b0:419:cb1b:d9c2 with SMTP id n12-20020a6546cc000000b00419cb1bd9c2mr9587397pgr.311.1659268013175; Sun, 31 Jul 2022 04:46:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659268013; cv=none; d=google.com; s=arc-20160816; b=Qdpav2nGYXoNsM30tv6peLeCLKen8YXujTqZo8F5L2jFEtoXgQv1gK9GVgilDwbl9C xyh/SjFR9L+xtjDLzLPKInd3o4BvdPPGGeakB+6wtgcQfj04/aNFCG6eframrbfoKEN8 I6gUuO3hbAnMXiWkWjeuShsI5Oz9yWsWrwv1Uo73CktlED04cvrWASoopW/cZhu549YH VJmNkJWbfk87ogKQCCQnDkRzKcfqmcO/EHGaaqGnq4K2ZJ0ew273p/F5lN3oKb3PyVRd mbNWLHXWCR7xTS6KFaVV490kiuvkOjpEd7Drl9apNfsS3ULBt+Gt+uRmU5bFlvsjLO+m wRKw== 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:message-id:subject:cc:to :from:date:dkim-signature; bh=vFxSZYoOFI8pbsXA26XWb5kd3pqpsgOyUjMmefUVP4c=; b=FsnMKPOxXsvOkqNrVEAJz7YKGXGzanWKrJrmK5v1i5+Cf1XXTNtip9ap3uJoPGHxlP ET/oboeWpjup3Xxx+zEjReHLlQDB8EyjmDLhTYqG6XxHKWpjBIIGcTb1BfKn3GZ3rTts mqlQBlZzRTws30b2CuB2T0H3zBzqsG5PYTuook2r6SFjoo4zS+YfaY7YNPJcAnQjFO8B P8IMmef/MZfxTKIemfAQtJUVw0eDm6QqyjbtKgifC9Fb0LzmxdOkCWMvYICBgqcAIMDS K7T9NDJ+h0WS15dhLS/hHSAKwwgjFXpJXcvndBTJR0TeayuphEd/jw2gpULzCZJ4kGSE uzbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JPPtSfwM; 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 i4-20020a631304000000b004119d19ff20si9818333pgl.478.2022.07.31.04.46.38; Sun, 31 Jul 2022 04:46:53 -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=JPPtSfwM; 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 S229745AbiGaLnr (ORCPT + 99 others); Sun, 31 Jul 2022 07:43:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232220AbiGaLnp (ORCPT ); Sun, 31 Jul 2022 07:43:45 -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 355EF659B for ; Sun, 31 Jul 2022 04:43:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659267820; 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; bh=vFxSZYoOFI8pbsXA26XWb5kd3pqpsgOyUjMmefUVP4c=; b=JPPtSfwM7PSSZJHC/MPmkydlsuhJBbP4aaTVWwvLiZa6QK9tTyPonNGet+BTIfI8TOJM0p +5r+PqU7TPUNdR4lg9eSJnSCGpJu/w74jwu9030E1+3F1EPGJtXcFm/rdJK3WjvDjyuqJ3 HS2derUf1E3qel/T0pr1CPG+d4ZNk5Y= 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-463-p0cWVs2kM9uRBAGXlESLLQ-1; Sun, 31 Jul 2022 07:43:36 -0400 X-MC-Unique: p0cWVs2kM9uRBAGXlESLLQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A478D1C01B21; Sun, 31 Jul 2022 11:43:35 +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 53DABC15D4F; Sun, 31 Jul 2022 11:43:35 +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 26VBhZLu031396; Sun, 31 Jul 2022 07:43:35 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 26VBhYdf031392; Sun, 31 Jul 2022 07:43:34 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Sun, 31 Jul 2022 07:43:34 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Linus Torvalds , Alexander Viro cc: 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@vger.kernel.org, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH] Add a read memory barrier to wait_on_buffer Message-ID: 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.8 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 a memory barrier. Consequently, if someone calls sb_bread and then reads the buffer data, the read of buffer data may be speculatively executed before wait_on_buffer(bh) 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 adding a read memory barrier to wait_on_buffer(). 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 @@ -353,6 +353,11 @@ static inline void wait_on_buffer(struct might_sleep(); if (buffer_locked(bh)) __wait_on_buffer(bh); + /* + * Make sure that the following accesses to buffer state or buffer data + * are not reordered with buffer_locked(bh). + */ + smp_rmb(); } static inline int trylock_buffer(struct buffer_head *bh)