Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp6828301rwn; Tue, 13 Sep 2022 09:30:47 -0700 (PDT) X-Google-Smtp-Source: AA6agR5UmUJOQY6H5GoUqeq4Iz3eGP73tgSHjy5690YRWs8Pt1tCRrMCvhcecDO7JMDp0bIIpmg3 X-Received: by 2002:aa7:90d0:0:b0:544:fc12:5c55 with SMTP id k16-20020aa790d0000000b00544fc125c55mr7540593pfk.6.1663086647270; Tue, 13 Sep 2022 09:30:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663086647; cv=none; d=google.com; s=arc-20160816; b=xOfn6ChPOYh7H0vfe4uEOcPhkOYnY1QNJLww9WDybR66bD2nKj87JvQvnkjMr+pk1X mxU+qY8hFw//Dy4VdwFvybhl9DVCYXX34u9vR6PrBxF8Ui6S5MJb1k0uJpNdJExA1xMu 9pei5LWDTqaCj9IgUrlgmxVIVJ0mT090pJFPMcsrdQvmhLXgCqcdNTcJG9n33ByQeZUC CNmlVV3lM1i/O2wQQYxb6OBb1vGi+wmTueEHuIqL4K0rgU1/QDai1/VNsUSrt1DK+15m H/ijm/NJhosOWcVCL1qUnIhorlHK67Me8XwnP01N/tNO99iMQDV8rb+mn+9UCafuZUmv sYRw== 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=lzxTn5hsJOyCyiLmLXwDsAxlL2A5u+O/kwe6oiVKx30=; b=wDF2ks3Q0z/cm0IXL0Mf7wCI/GmwTTtl8qDhlW+q5ED/zCBsUAlPPwNXISQ/kXudKU /DiI9N9pEMStGpzUNhFmGW2FyI+6StAfltU0Q3IjB5NKeYKLnyMCEVeKqroqB8BSzApp SByYQv1BK3vl0A78IsY+RYqnVZHyEwU200h1udFOWnXMyLztm5NtZbxN06HTMHHMRnC9 ONd0YqyLYcDyKBdCXy+/wCB6KxCacb6gvYv4QXe5Ak/sNZ5hb/FtJGoZ7EvG8+rlAOEj V5kn4C82EG1K2o9mmOFAqjuBcu54WzY9Pr7WR+qPhByJm1zaT39gnTupyPFstbM+f8DI avLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=K0uWQ4mO; 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 u1-20020a17090341c100b00176d6e8c127si13107673ple.126.2022.09.13.09.30.34; Tue, 13 Sep 2022 09:30:47 -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=K0uWQ4mO; 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 S233959AbiIMOcN (ORCPT + 99 others); Tue, 13 Sep 2022 10:32:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233815AbiIMO3z (ORCPT ); Tue, 13 Sep 2022 10:29:55 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57DA3BFA; Tue, 13 Sep 2022 07:18:57 -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 D9AA1614AC; Tue, 13 Sep 2022 14:16:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F0E3EC433C1; Tue, 13 Sep 2022 14:16:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1663078608; bh=4q8krE1FsncwNTfSC0uky0pPAGgOhmkWebSZonlFBbA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K0uWQ4mOnmJUID5g8pnAx5aGCvs55T9JgyUsbAHFL/7Hr5GhCpd9MjojkbECLixEU qyfIrqiVVmOGvSmWfhmFAaXBZsz9vpx8OvH36bSLreKfd23yYS7lCys8jGrY1m+6ik 8OWU4K7fOmesfIoB8Qpk6CCtBh+xFTtLH1ZIFauQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, kernel test robot , Fengwei Yin , Mikulas Patocka , "Matthew Wilcox (Oracle)" , stable@kernel.org, Linus Torvalds Subject: [PATCH 5.15 006/121] fs: only do a memory barrier for the first set_buffer_uptodate() Date: Tue, 13 Sep 2022 16:03:17 +0200 Message-Id: <20220913140357.601665462@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220913140357.323297659@linuxfoundation.org> References: <20220913140357.323297659@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: Linus Torvalds commit 2f79cdfe58c13949bbbb65ba5926abfe9561d0ec upstream. Commit d4252071b97d ("add barriers to buffer_uptodate and set_buffer_uptodate") added proper memory barriers to the buffer head BH_Uptodate bit, so that anybody who tests a buffer for being up-to-date will be guaranteed to actually see initialized state. However, that commit didn't _just_ add the memory barrier, it also ended up dropping the "was it already set" logic that the BUFFER_FNS() macro had. That's conceptually the right thing for a generic "this is a memory barrier" operation, but in the case of the buffer contents, we really only care about the memory barrier for the _first_ time we set the bit, in that the only memory ordering protection we need is to avoid anybody seeing uninitialized memory contents. Any other access ordering wouldn't be about the BH_Uptodate bit anyway, and would require some other proper lock (typically BH_Lock or the folio lock). A reader that races with somebody invalidating the buffer head isn't an issue wrt the memory ordering, it's a serialization issue. Now, you'd think that the buffer head operations don't matter in this day and age (and I certainly thought so), but apparently some loads still end up being heavy users of buffer heads. In particular, the kernel test robot reported that not having this bit access optimization in place caused a noticeable direct IO performance regression on ext4: fxmark.ssd_ext4_no_jnl_DWTL_54_directio.works/sec -26.5% regression although you presumably need a fast disk and a lot of cores to actually notice. Link: https://lore.kernel.org/all/Yw8L7HTZ%2FdE2%2Fo9C@xsang-OptiPlex-9020/ Reported-by: kernel test robot Tested-by: Fengwei Yin Cc: Mikulas Patocka Cc: Matthew Wilcox (Oracle) Cc: stable@kernel.org Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- include/linux/buffer_head.h | 11 +++++++++++ 1 file changed, 11 insertions(+) --- a/include/linux/buffer_head.h +++ b/include/linux/buffer_head.h @@ -137,6 +137,17 @@ BUFFER_FNS(Defer_Completion, defer_compl static __always_inline void set_buffer_uptodate(struct buffer_head *bh) { /* + * If somebody else already set this uptodate, they will + * have done the memory barrier, and a reader will thus + * see *some* valid buffer state. + * + * Any other serialization (with IO errors or whatever that + * might clear the bit) has to come from other state (eg BH_Lock). + */ + if (test_bit(BH_Uptodate, &bh->b_state)) + return; + + /* * make it consistent with folio_mark_uptodate * pairs with smp_load_acquire in buffer_uptodate */