Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp53006rwb; Wed, 18 Jan 2023 14:02:23 -0800 (PST) X-Google-Smtp-Source: AMrXdXuQ/W4hBzuRFx4xpgRh5rl5j7Y7MkB6Ts5PUt9aG9BD10Du4yld4wX2rPhoDD3QuqIZuPUY X-Received: by 2002:a17:907:29c4:b0:86e:a761:c5bd with SMTP id ev4-20020a17090729c400b0086ea761c5bdmr7369553ejc.34.1674079343213; Wed, 18 Jan 2023 14:02:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674079343; cv=none; d=google.com; s=arc-20160816; b=ywdqKNdphAziMdp62lLTN7vvSkvmW+UwQk2I4B8Q0l6IZmP9TWAGyEK3/5AVU/O7L9 4hOi88tetXco3cAky9cTGyjXrLCPv3TjIXGzVCTVczJu1TLWGjstDtl6UnWRgQLvWoQw cxuhag5Qwk5BCMH+Kglgykig1tv2+21IPWEiJJ2TK1Zjpvw3ABCE1OD9Md9QqHqAneLP XdlZxxgVdfvOfrZJLgkNaKQbgkAzy2/ULL+y0RdpbNmMcOILK0SHas1gFo/Hw4eGQ5+z cRc7M2mDjP55YZwl29EPBybSKRCxdSXL1U/HLQ23xDkKYvD2PYKPMSKWFWBesfLtWjdN NG1w== 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=s+vb6zEcZT2dbdq498iVIMckBbJXuDk07zbcxhVQhG4=; b=i1WQLzo9eX+eMA+FGVd3Psij1ItBS75qXXjFkJkPP+Y68Aj8Lo4+jTGDVC/SBKv76r 9wB1NXGDmPCgRLPetNrJ3GoBk8PQrRAHLGTvVPtqb9naJWqSwhXOIzm1aQuvshOvX2QT AFsrIunBUz1cR/uPj08ZlSEpbLiYOd0orRQzZu1XVDlqKIm0Uw5p4vLyrSNdDRBL+fFi geDH25WqDMSdff+8Tl5aPqG64vyli74SpsSD2fzMA5dsSKU79Th8lJOGQZz95FzREzw+ 5M9zJvFHGrwL4pd87H2eLEqyUJOMOE/fNF4Wj5Pge5KxvBm74NkrqRfsPMs0/Zi9pyyc shlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="J7yQ/TBK"; 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 go19-20020a1709070d9300b0086d67b0615esi17112073ejc.242.2023.01.18.14.02.12; Wed, 18 Jan 2023 14:02:23 -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="J7yQ/TBK"; 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 S231265AbjARVrW (ORCPT + 45 others); Wed, 18 Jan 2023 16:47:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231243AbjARVrT (ORCPT ); Wed, 18 Jan 2023 16:47:19 -0500 Received: from mail-yb1-xb31.google.com (mail-yb1-xb31.google.com [IPv6:2607:f8b0:4864:20::b31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2411218A83 for ; Wed, 18 Jan 2023 13:47:19 -0800 (PST) Received: by mail-yb1-xb31.google.com with SMTP id 69so205044ybn.2 for ; Wed, 18 Jan 2023 13:47:19 -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=s+vb6zEcZT2dbdq498iVIMckBbJXuDk07zbcxhVQhG4=; b=J7yQ/TBKIUB403GWhpiGw3SDLkWVutrAoZ1tPjg5bkTHoPIjrTGh9S68w9IME5UeQo 1rENdf9HwQjTxNUPOJK/6RiNTH1SFKWcVZEVbIvu4gAO2DBd6IwalBHID5eMcTIJvKwv sHr/Y/zu+OfgwSiZwteGWlJK6/EVlc4pQ1gM4L8DkxighAHJi4B/8eAHNWFJ870Dnbrr W65l/Nn4SN/T/sD3WdJF+7CoWQJ8AowA5AdPtUTV82/TUaLVXSwBzIaeGlNrytF0d5iZ qPIlj6ZykbyGzgHQA/uv1pwNI3x5ULPBbAjhk5g8qK3s6gRU4Mtiyep1NYfjNjheL/2t N8mw== 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=s+vb6zEcZT2dbdq498iVIMckBbJXuDk07zbcxhVQhG4=; b=aBB/QBhK8IcxsnbuunKrwIahkJ1n9Q1FUhgefoPcRRXBbqaf7c00/B/eaaj7YAPzpr OehvJDUVy6VJwtDcLYOCaynFpFLH6VoI4CI2qLUWAzRMXVQOotmTT1WiZo2WiqOeZeaf 7/KyfR0bniBXTfL5OlF7qDwnxO+2Y5I5rhrB4yAKP0iSmot7PEYbnTBQkqTZp3S2MC5h n6uJ9C9ci0ammrlzAcdN6STUspqptMERQF7Wc+f47SWZV3J4M/uVUeC7Z8v545XxbY0P 1O0E5p9zPylTdJXakLAdWVjCd69/RjlqHCSpeJdvHEaMYgkkHhdo6rIuze/Bwe5+XCDi x/NA== X-Gm-Message-State: AFqh2koWw6ZIjZGAc3ojD0sK9nMMGfWBYtwpmXggw7J3BSjW74fMAOn+ 5EKAe98H5JHuB69EPaL+L7PHm0+m+Ghz6dl1sKUqJbT/Ro8uYQ== X-Received: by 2002:a25:740b:0:b0:7b6:9dcb:6588 with SMTP id p11-20020a25740b000000b007b69dcb6588mr973980ybc.251.1674078438272; Wed, 18 Jan 2023 13:47:18 -0800 (PST) MIME-Version: 1.0 References: <20230118150703.4024-1-ubizjak@gmail.com> <20230118131825.c6daea81ea1e2dc6aa014f38@linux-foundation.org> In-Reply-To: <20230118131825.c6daea81ea1e2dc6aa014f38@linux-foundation.org> From: Uros Bizjak Date: Wed, 18 Jan 2023 22:47:06 +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: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. Uros.