Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp77849rwb; Wed, 18 Jan 2023 14:24:52 -0800 (PST) X-Google-Smtp-Source: AMrXdXvkLrUkWioCTbiE1FMe8lEl5E66IAJr7xq6gDpqeLjmgcZxD3cJ5kSvChOcBdwId5NoMeEs X-Received: by 2002:a17:907:d23:b0:877:6873:70b9 with SMTP id gn35-20020a1709070d2300b00877687370b9mr2187066ejc.29.1674080692322; Wed, 18 Jan 2023 14:24:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674080692; cv=none; d=google.com; s=arc-20160816; b=crenaDbcX91z/nty17DzezsEdStDv84vx4UoN7exnUHLMhN1i5jvVOm4Rb5V8emE4l BvF3J8Cd/vKprE0seVxCe1q1exBeM5vURufwGLgf7q39bChm8cHQgEfrTGTrV3I4npZP jdhOxp5worlekb/LUJwbeNlfrnxu3Mz1dky3+J1emjUlBnuBrn0q+qq838uXy92CmcY7 N3pVjIa3VCDu+WuOxRor2nVk2pkcOJZgAKmjnkkFq1tjZapDLB4wkYx51a/yRXYS05wy 76oNCZY43Vx+gpUi3COY0lKwaMtKBF0bAWbxmjwadtF9vOvfX58wuTlSVBDzhRCOQW55 EHnA== 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=eh8dAbgVRrFwnzWUEGIwJaAIH80Ew7E9/OKwj57uyg0=; b=uYGIhBPdymWKaWXPreEUNAVfIZ2phZEciREiKT1muSbAnApR4ZhdddpfeLckN6l/Xh rrsiQE5WmTitRGfdEufwi174wuduE+0AqiGPSk8rGtW6soTgiJ/SS5GqRf5LsgZ9O08E uQDPkt1R4TyYz9N1zyIMu1p6veeWZx+i5VEe8rWPyNbC/QhLtOpx1xgq8bVGb+v0XsKh Nhgq1gEkid+7xThIdSUP6elBCCC7Lju8bNl3F6vvR+6Boo7IjZLT36Kck0+8iXU+sM4c tFIfqmoQc0dPn+VUaI4weLHxc1p38ux9So1q/K1gO3ZpSE6Qym7RAiK+giRQN7Kck68p ZGkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=nJGNy+FY; 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 hr6-20020a1709073f8600b0077bd074d50bsi42218060ejc.105.2023.01.18.14.24.41; Wed, 18 Jan 2023 14:24:52 -0800 (PST) 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=nJGNy+FY; 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 S229898AbjARV4K (ORCPT + 44 others); Wed, 18 Jan 2023 16:56:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229900AbjARVz7 (ORCPT ); Wed, 18 Jan 2023 16:55:59 -0500 Received: from mail-yw1-x1135.google.com (mail-yw1-x1135.google.com [IPv6:2607:f8b0:4864:20::1135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75F33654D0 for ; Wed, 18 Jan 2023 13:55:58 -0800 (PST) Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-4d13cb4bbffso658247b3.3 for ; Wed, 18 Jan 2023 13:55:58 -0800 (PST) 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:subject:date:message-id:reply-to; bh=eh8dAbgVRrFwnzWUEGIwJaAIH80Ew7E9/OKwj57uyg0=; b=nJGNy+FYWaPE6HWuHcH3oVryDlTnQctCoWA59TQXSEdtT7u9LVPaE+3Bp6dcuR9eo8 ZuCt/cm+uMsaq4ePsq2JuQNp72mXm9CCQ8wZd5k1ixUun0zKMaVU0yGAhGPJ7VYH35k7 ZXaiN6GgNMMY83qMhnVgWVsUlUzDW6nzZyMBwP9v8ftHZEG1zdJ2EEGJkrIMcFAqqPBh jQxzRS+A4btlwvGxufPrEifN6QyKTtiFOyDYzKQw6roPc60PFhHbHqWCCVPN+bGlEVI7 K2/Nus0JAbDaikFmuLLiQdU2OO+gsg/XY6u/NHBMMiYIBmzYkDopNVKgW2+8wK02SRg8 h7UQ== 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:subject:date:message-id :reply-to; bh=eh8dAbgVRrFwnzWUEGIwJaAIH80Ew7E9/OKwj57uyg0=; b=yLIwihXmBpDCojreCJhchFMQ9qg+9pMobSmTGiN6mv7uInsnGvYCEsQVeEX1mSd5Vv /B/dZgDmEruyHBC/AZSCDuAzMxgg600OwSJu6jT2vFLK/6vjGhLXuMSEVLpxEzOqKDsA AnsSPyMqdHsoMehUYb6iTdk0L8ZH2O4nKIJxOC63Yxp2shdJHRzt7UFH2pA2M/jrPfCT 8dE7L436cgzV0i2ojUIh8p7TkL+dkOOcxOFzYtUgnn5hruHg9nJwcDhXE/3TO3tLwO37 biAAlDxyrMHiNCKgafn+fSLXP7l1fJgftHaTuwjrt/rlB5Sn6hCRG/dcC0q+d8GTFd1B YDyg== X-Gm-Message-State: AFqh2kp4SKsaWIXcKqjrsaSAb48GpuzEvYpr7VP2goZzuTrnqwuMjF+i LHZQGTCdlT7beJY5G5rJITgCNqRUDrs4bCRHm+U= X-Received: by 2002:a81:5c02:0:b0:4e2:db5a:2c2c with SMTP id q2-20020a815c02000000b004e2db5a2c2cmr1134272ywb.202.1674078957602; Wed, 18 Jan 2023 13:55:57 -0800 (PST) MIME-Version: 1.0 References: <20230118150703.4024-1-ubizjak@gmail.com> <20230118131825.c6daea81ea1e2dc6aa014f38@linux-foundation.org> In-Reply-To: From: Uros Bizjak Date: Wed, 18 Jan 2023 22:55:46 +0100 Message-ID: Subject: Re: [PATCH] lib/genalloc: use try_cmpxchg in {set,clear}_bits_ll To: Andrew Morton Cc: linux-kernel@vger.kernel.org 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 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, Jan 18, 2023 at 10:47 PM Uros Bizjak wrote: > > On Wed, Jan 18, 2023 at 10:18 PM Andrew Morton > wrote: > > > > On Wed, 18 Jan 2023 16:07:03 +0100 Uros Bizjak wrote: > > > > > Use try_cmpxchg instead of cmpxchg (*ptr, old, new) == old in > > > {set,clear}_bits_ll. x86 CMPXCHG instruction returns success in ZF > > > flag, so this change saves a compare after cmpxchg (and related move > > > instruction in front of cmpxchg). > > > > > > Also, try_cmpxchg implicitly assigns old *ptr value to "old" > > > when cmpxchg fails. > > > > > > Note that the value from *ptr should be read using READ_ONCE to prevent > > > the compiler from merging, refetching or reordering the read. > > > > > > The patch also declares these two functions inline, to ensure inlining. > > > > But why is that better? This adds a few hundred bytes more text, which > > has a cost. > > Originally, both functions are inlined and the size of an object file > is (gcc version 12.2.1, x86_64): > > text data bss dec hex filename > 4661 480 0 5141 1415 genalloc-orig.o > > When try_cmpxchg is used, gcc chooses to not inline set_bits_ll (its > estimate of code size is not very precise when multi-line assembly is > involved), resulting in: > > text data bss dec hex filename > 4705 488 0 5193 1449 genalloc-noinline.o > > And with an inline added to avoid gcc's quirks: > > text data bss dec hex filename > 4629 480 0 5109 13f5 genalloc.o > > Considering that these two changed functions are used only in > genalloc.o, adding inline qualifier is a win, also when comparing to > the original size. BTW: Recently, it was determined [1] that the usage of cpu_relax() inside the cmpxchg loop can be harmful for performance. We actually have the same situation here, so perhaps cpu_relax() should be removed in the same way it was removed from the lockref. [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=f5fe24ef17b5fbe6db49534163e77499fb10ae8c Uros.