Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp6769387rwb; Wed, 10 Aug 2022 00:18:36 -0700 (PDT) X-Google-Smtp-Source: AA6agR4yQmZ+Y4K0YJrG7Ad1XxTWWZQuQS+bZ7ZEYKFJSexOQsCPfOzyqck+HIIIjclAhJLE9ncl X-Received: by 2002:a17:90a:c683:b0:1f4:8565:772f with SMTP id n3-20020a17090ac68300b001f48565772fmr2322558pjt.0.1660115916745; Wed, 10 Aug 2022 00:18:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660115916; cv=none; d=google.com; s=arc-20160816; b=pHHUAOtwCJvQwuWY4LafTK+MDBa+TG9RGtrqE3Nhpg4hFUrpdKUkDvQrDjPqajanBe 8ncVsREc8Wi+QCPam//Py0iE1GKfGDENydVn7DmA3Z4IPeOypr2OiNLFqZMSd9vfP1KF o9Fp7LMIlvoOw1jDZm3WOTuZiq34OktdMiTUpuDr3d93rWtbBZNfQqJlmy57Zohr6Li4 xJWxyOGjlyEk0FGML0xDgzIMpuf6MZFbcw2z4RvzxkCBBc5dbqvJBEq44+V3VDphE4lm XcSh9bloSC2HCQjeIgtsrIaMXV/qNIUDwrU+C9JB3f81TL1q/mRhmzykdfPY1zRAw+y4 vXLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=rSmYRwhSKwMnBfTi/jvVtIpAV+E+QFfv+LYuFK98Rr8=; b=Jxj/aySTm46n+n8StBk7V8PmgJrQcw/kdWd/GL55GM6hOsX5Yq9+gnMUWE/XsXsVf+ WO2eFw+B1e0UDKhgPM9HGIArpr3P/RcKfsfOYaGdFUDHZJmAwFGQENnQZ3zef5dOGHMS xB+FOIk3ICXJeC6cSMhYiQurlTIe+7nYrb9wQXEp0V+THTGCUjSZKEqweCVgfMyvIqNG BAt1HwqEE93v98947JVznSd8/aXKCqJ4rXFOMAIr0dPM/gE66MO4HDMRQ5oquMSxeXJh g8ng2+eD3pkjp0iOq3UZnUYabfJ6oc4krPVSDajCEV9RyTVh798uHSXnvf2ccP/A7mgI gwzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Ccc7NPfO; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m10-20020a170902f64a00b0016c0474d5cesi11056565plg.532.2022.08.10.00.18.23; Wed, 10 Aug 2022 00:18:36 -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=@gmail.com header.s=20210112 header.b=Ccc7NPfO; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231437AbiHJHCb (ORCPT + 99 others); Wed, 10 Aug 2022 03:02:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230119AbiHJHCa (ORCPT ); Wed, 10 Aug 2022 03:02:30 -0400 Received: from mail-qk1-x72a.google.com (mail-qk1-x72a.google.com [IPv6:2607:f8b0:4864:20::72a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29B3674DD4; Wed, 10 Aug 2022 00:02:29 -0700 (PDT) Received: by mail-qk1-x72a.google.com with SMTP id f14so10520651qkm.0; Wed, 10 Aug 2022 00:02:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=rSmYRwhSKwMnBfTi/jvVtIpAV+E+QFfv+LYuFK98Rr8=; b=Ccc7NPfOHdvS0O0vrc/nj68VpzBfetEjYqQjxZrtwhLsHaaJDWn1ig8iR2K6A0bLJe OfANXDbQ1cPkgQ25cTK91SNrNkoDu0YA7k1uTvkcDlrcBT5emBgWrlTmE8ndvyrNxQDB pyxrOQb4eCgQpiictOmdEdPMpZKANBX37bgaqJvsRVMuQL7tEE2+V3HKIFefiAPDs6qg neYUuO2lw54h6JEtHOAtNTsq0/K+50wio1mC1M63o6tG3WMyI2OeXHNn3GhPRzO3Wb6Y B01tgXEIrvbCmCfFWjlfIZOPv8uDRASc1DIzVx6oX2H0pLBLx2l20CwPnfmIxRHiJplM 2Xvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=rSmYRwhSKwMnBfTi/jvVtIpAV+E+QFfv+LYuFK98Rr8=; b=SHK0e3lugutYJKhhT/+F5x+WaCSQprVoL8m+79KoUrJ7ORzzw61x49ljW7Z6klUW96 iWVYByfH033nCipEhbNNscRsBtoCymCE3N2l8fTHci4cVVFQBVum2RA+Ymsc8xxX3pu8 VsCz/gBRmCVgn6M7FhdTXtvfi+H6WhWN+0vJQ9olN/gKyph9D79WRKt/qx+JmqAv5cm8 SBcmnzn7CkA5GXQunFQR37JNAc6EQnmEkTyBNG3XPbOZo/Yd7BV3/wkqMh6PyikKQE2k 2BIT48xyvOCsI/DRsgSCyXxxqhs5QDs25XnFKX/qubiJ3sYFMt/60d/Bi1WdXi5M5rjv iQBw== X-Gm-Message-State: ACgBeo0GNFbSHnNGn8sJBGi/DQXew/egTgx5x+ic8tSJW/tcivhuU23n DvcS4HBZaxS+Jf1f140Fg0UPaSudxQcaIUJP8bOVV9uJjjKy9w== X-Received: by 2002:a05:620a:4007:b0:6b5:d88b:6d42 with SMTP id h7-20020a05620a400700b006b5d88b6d42mr20185709qko.180.1660114948091; Wed, 10 Aug 2022 00:02:28 -0700 (PDT) MIME-Version: 1.0 References: <20220809165615.9694-1-ubizjak@gmail.com> <20220809220511.GI3600936@dread.disaster.area> <20220809230244.GJ3600936@dread.disaster.area> In-Reply-To: <20220809230244.GJ3600936@dread.disaster.area> From: Uros Bizjak Date: Wed, 10 Aug 2022 09:02:16 +0200 Message-ID: Subject: Re: [PATCH] fs/xfs: Use atomic64_try_cmpxchg in xlog_grant_{add,sub}_space To: Dave Chinner Cc: linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org, "Darrick J. Wong" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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 On Wed, Aug 10, 2022 at 1:02 AM Dave Chinner wrote: > > On Wed, Aug 10, 2022 at 08:05:11AM +1000, Dave Chinner wrote: > > On Tue, Aug 09, 2022 at 06:56:15PM +0200, Uros Bizjak wrote: > > > Use `!atomic64_try_cmpxchg(ptr, &old, new)` instead of > > > `atomic64_cmpxchg(ptr, old, new) != old` in xlog_grant_{add,sub}_space. > > > This has two benefits: > > > > > > - The x86 cmpxchg instruction returns success in the ZF flag, so this > > > change saves a compare after cmpxchg, as well as a related move > > > instruction in the front of cmpxchg. > > > > > > - atomic64_try_cmpxchg implicitly assigns the *ptr value to &old when > > > cmpxchg fails, enabling further code simplifications. > > > > Do the two cmpxchg operations have the same memory ordering > > semantics on failure? Yes. The API also provides _acquire, _release and _relaxed variants of both, atomic64_cmpxchg and atomic64_try_cmpxchg. On x86, these two functions actually compile to the same CMPXCHG instruction, the difference is only in how the comparison is handled: 15: 48 09 c2 or %rax,%rdx 18: 48 89 c8 mov %rcx,%rax 1b: f0 48 0f b1 16 lock cmpxchg %rdx,(%rsi) 20: 48 39 c1 cmp %rax,%rcx 23: 74 2a je 4f becomes: 29c: 48 09 ca or %rcx,%rdx 29f: f0 48 0f b1 16 lock cmpxchg %rdx,(%rsi) 2a4: 75 d2 jne 278 And as demonstrated in [1], even the fallback code compiles to a better assembly. [1] https://lore.kernel.org/lkml/CAFULd4bc54+_FmJ=f++zzz99mR8r5c11-Y49pz86Yb8G3dyJpA@mail.gmail.com/ > > > This patch has no functional change. > > > > The patch looks ok, but .... > > > > ... I'm about 2 hours away from posting a patchset that completely > > removes the cmpxchg and the new grant head accounting has > > significantly lower fast path overhead. It also opens the door for > > tracking more than 2GB of log space in the grant heads. > > FYI, the original RFC for this was posted a bit over a month ago: > > https://lore.kernel.org/linux-xfs/20220708015558.1134330-1-david@fromorbit.com/ -static void +void xlog_grant_sub_space( [...] - old = head_val; - new = xlog_assign_grant_head_val(cycle, space); - head_val = atomic64_cmpxchg(&head->grant, old, new); - } while (head_val != old); + atomic64_sub(bytes, &head->grant); } I actually wondered why these two functions were not implemented as atomic64_{add,sub}. Of course, this is a much better solution that renders the proposed patch obsolete. BR, Uros.