Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp6893390rwn; Tue, 13 Sep 2022 10:25:03 -0700 (PDT) X-Google-Smtp-Source: AA6agR6eqt5GnKde0wc5v2Sz8DZfc+PFY5/vIeHBtTawQQn1Qr9w3JG1l6REq9ZueA9IEHJDnwVX X-Received: by 2002:a17:907:31ce:b0:742:1206:529e with SMTP id xf14-20020a17090731ce00b007421206529emr22608323ejb.643.1663089903538; Tue, 13 Sep 2022 10:25:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663089903; cv=none; d=google.com; s=arc-20160816; b=O2OfhE9EHrJuUfylByn0qXS4i55dlQIcX9gXrj2tgbK5q4eDPLfAEqipWVBLUo1jBQ fK6suKvOwEEfu/lQv4Jh78nDUWo50j8pKinTqcR+jKLs46VLj0qodi7vABoIMGV6gqiw b19Q8dmSHQsWOZiyT96CZsV8PhkJKn7ZMM/XdLuj8yroiCe9pfl5UXW149bvwdVasg2u CHefs8YODkjtVfq+BE2rTQYrDL5DLt6YnNUx/J6k/mHyrxwgeyBFym0ydr3XKFz3c33K RSvhAIl6XOKRvk0aXTo2nh7M8WV3KdlfinO1gUyeds6ht4Y5wPr+23Z6lrF3gSS8lUeZ 3/3g== 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=4bvXGsn7ItZXd8Di/eTJ9tfp9CD56xhaH0XNR8UhyLQ=; b=T1/PxHaqjqh0PpioDyiZAMAmja/9b6+gCAicBOxEdYwHWQGGg6O5QHYzRqDrISg6Yd CAX3kxPxNYpABxtvk7fZATwdM/+0Cwidz3umMw0r1u1PLDqBelTc2G3Jxsp2XUcD6BDp L34uCEthSIqsBBWInW+83p+hUDkeW+MoomUh70yCuCE2u8/x6wYuTP2aUUa1BnYF67st ZfuF3iTEHuVlK6ci87TwMASsvhg9/zTMKnwVSMun3kcuG2rZpJlfHd+6CtC3vwGjdQoT /VdiSlxrC3rsLsum+Q/k8t68nbAljjm6ERd9cYC4cXvxMWE5A4dQxnA2dvIQo3VeZ0sq yFNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Z7lCGrGA; 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 kw2-20020a170907770200b00741a0720a2bsi9519282ejc.814.2022.09.13.10.24.37; Tue, 13 Sep 2022 10:25:03 -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=Z7lCGrGA; 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 S230057AbiIMRA2 (ORCPT + 99 others); Tue, 13 Sep 2022 13:00:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232138AbiIMQ7l (ORCPT ); Tue, 13 Sep 2022 12:59:41 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A3BFC12F0; Tue, 13 Sep 2022 08:51:23 -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 ams.source.kernel.org (Postfix) with ESMTPS id 2C4E8B80FEA; Tue, 13 Sep 2022 14:34:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9EE58C433C1; Tue, 13 Sep 2022 14:34:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1663079686; bh=6oKrQHMbUouvKFesVC5x9+vlDyMIL0qPZFyN2v0YdeI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z7lCGrGAtkGEtkFhu25YQvzlpRoH32xPHtcrBZDEDd47vn6ReJ1UnO2JSgO/d2jVD h8MCUBxY1S1GU2idWUFLqxzXmHX9gjCG3XE8wVlEbeFnbuDB52hS1dqta3uCfsKo3q poCxBbAHZZ6/tTkZTpdFEzZKJOdVJiSViVR2FRdk= 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 4.14 36/61] fs: only do a memory barrier for the first set_buffer_uptodate() Date: Tue, 13 Sep 2022 16:07:38 +0200 Message-Id: <20220913140348.298766987@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220913140346.422813036@linuxfoundation.org> References: <20220913140346.422813036@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 @@ -134,6 +134,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 */