Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2483162pxj; Sat, 19 Jun 2021 13:49:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRXra8/80kZlJe+Qt20eICQZ6z6SDbTVCqU2sHM2tqk0joIzoMmWZ5UXKQr9ve6+yM9siD X-Received: by 2002:a17:906:4b43:: with SMTP id j3mr15047538ejv.414.1624135742710; Sat, 19 Jun 2021 13:49:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624135742; cv=none; d=google.com; s=arc-20160816; b=04edBSbJLJpefVPyqY9Rw3Y1IID7xb4O6ALnyEqmZ1yIsGfCNLLT6AyHuzYuwdsfsQ PauITPYnY8LTSvj0pqu9SvIeFZrMe54omj6NqTRZJLBRC7F+xJD7/W47WBdNRZjVx7es 9YkO8t28QQGz+1IXkEJFHipAKk8Gq3WFdpb/bZllBZNARbt8+ThaO8jqLOJsOqGeLaCp rVUJ1H0FMR+uYSjkq3mGuG4GUV3zd4GScMo+7mKs3zAzN26avWmweEF4r2WDCKK2RI5N g6KPLN003jkHxnk6XkfPzKpg5sLAJCsAe6vPuCeaBzbVuakeccU9XTTFO6j56M8Au0Xq e2ow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=2fuqk2Uq9ZlnrXPo4v18ajx4HA0ZizExc9F16VvNLus=; b=XavyWIuWUDBrnVK2Gvaqp/Lj0CLyITxdj5Tku8/C2MBvUKfZ6J0aDDjdtd1S22giuk S1+1GzMbCMpmjl3gaa8M+wLpNS+xfn3kEXtpW6L03xNuqM+KDGp/voXFi3JDLpCKvSZC D7Kui6dzzm+rQ3bWU96mzKPwSEDLBvkAlfD5N0AIRdMCJkkGtnXRLiqO+YnvQD+jvFc6 vjoAC+kypDB04o/20n/yP+kYDppOjTxlm7xDfJHt6bwWyWbEm5vvZSHjY1fUfmTKbNtX uc5lpDELUQPQ2zJiYdwhlk39ZqCB7WUxicZCv1uS/agX+sihY8TTxLv9CNxm9aWLMJAW i/GA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=qxauMLGY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bc26si11064263edb.76.2021.06.19.13.48.41; Sat, 19 Jun 2021 13:49:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=qxauMLGY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S234917AbhFSRaU (ORCPT + 99 others); Sat, 19 Jun 2021 13:30:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234900AbhFSRaT (ORCPT ); Sat, 19 Jun 2021 13:30:19 -0400 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FE4AC061574; Sat, 19 Jun 2021 10:28:08 -0700 (PDT) Received: by mail-qv1-xf29.google.com with SMTP id im10so5309662qvb.3; Sat, 19 Jun 2021 10:28:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=2fuqk2Uq9ZlnrXPo4v18ajx4HA0ZizExc9F16VvNLus=; b=qxauMLGYdJHmLOE9NAzDcQ4rSL85U6WRsaTxi4imvMzlgh5aaGSsVHCMoZjKDWfc1Z Cubw6ucf2e5ThDITSXLpj8lXuA/QAGwzzQ5Nfy2UWCVIL8wE1U47Ul27IyhLywGouc2E SpcL+ZJD+4x3oQz1b8wjCLWq7L+xnBbhIyykl5NAD6zvpj0RngxFgatT/NhHugR1HVhl O1TWLIqVpFeDGMV+lzAODOWEFdTqjgTofcG41PaNfAMZyIJE2XoNGsfURSyrLyt/pIoC ruqZLiAf7f4icv3fFwdK1wv3xRo+6QEfXO1I+87XozfQ2wLyzoDdx1Gv6qfKHYTYIsxe ISNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=2fuqk2Uq9ZlnrXPo4v18ajx4HA0ZizExc9F16VvNLus=; b=lu7If7699HpDnFxYT7PY/+augD3i82anxHfRaKzYvcqNv56XK+RdU1eSKNxiTL3JIe PwkOiUXvl4NbS2kb5PCjhHHt5AO3Pn0rMit7RZLm2anCWtI1TXYCt0LHg86MQ7fNbcWc xCpPqIG/60SrJiEXhZ9ruhmAxt4mJVGpiDSLt8oIVPXMyxf5IZAR73ZgqbPdaCBFIVCM 9qtALHFpIDCCr4CuBXhbXsC6FZAeLDYeDViRyLbIfMC7thQVfVH0lto0PxZM0xqf8ER3 rclDPhMCoe8ywPvIuG20h1mUMy5IqKhUowMchJq/3iqJS6u1lC2vQvaDrCslmWT11xmV PlJw== X-Gm-Message-State: AOAM533l3RS/MoiX+ZqeHs1MDuvG2AjYrgYUDM33E11Cvwt8nC/5lyEy Q6E5AeqBqx5riK5rcwrqgcI= X-Received: by 2002:ad4:5f0e:: with SMTP id fo14mr11491351qvb.16.1624123687363; Sat, 19 Jun 2021 10:28:07 -0700 (PDT) Received: from localhost ([207.98.216.60]) by smtp.gmail.com with ESMTPSA id 186sm877814qkf.29.2021.06.19.10.28.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Jun 2021 10:28:07 -0700 (PDT) Date: Sat, 19 Jun 2021 10:28:06 -0700 From: Yury Norov To: Marc Zyngier Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Lucas Stach , Russell King , Christian Gmeiner , David Airlie , Daniel Vetter , Jean Delvare , Guenter Roeck , Andy Shevchenko , Rasmus Villemoes , David Woodhouse , Andrew Morton , Wei Yang , Geert Uytterhoeven , Alexey Klimov , x86@kernel.org, linux-kernel@vger.kernel.org, etnaviv@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-hwmon@vger.kernel.org Subject: Re: [PATCH 2/3] find: micro-optimize for_each_{set,clear}_bit() Message-ID: References: <20210618195735.55933-1-yury.norov@gmail.com> <20210618195735.55933-3-yury.norov@gmail.com> <87bl81ddqo.wl-maz@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87bl81ddqo.wl-maz@kernel.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jun 19, 2021 at 05:24:15PM +0100, Marc Zyngier wrote: > On Fri, 18 Jun 2021 20:57:34 +0100, > Yury Norov wrote: > > > > The macros iterate thru all set/clear bits in a bitmap. They search a > > first bit using find_first_bit(), and the rest bits using find_next_bit(). > > > > Since find_next_bit() is called shortly after find_first_bit(), we can > > save few lines of I-cache by not using find_first_bit(). > > Really? > > > > > Signed-off-by: Yury Norov > > --- > > include/linux/find.h | 4 ++-- > > 1 file changed, 2 insertions(+), 2 deletions(-) > > > > diff --git a/include/linux/find.h b/include/linux/find.h > > index 4500e8ab93e2..ae9ed52b52b8 100644 > > --- a/include/linux/find.h > > +++ b/include/linux/find.h > > @@ -280,7 +280,7 @@ unsigned long find_next_bit_le(const void *addr, unsigned > > #endif > > > > #define for_each_set_bit(bit, addr, size) \ > > - for ((bit) = find_first_bit((addr), (size)); \ > > + for ((bit) = find_next_bit((addr), (size), 0); \ > > On which architecture do you observe a gain? Only 32bit ARM and m68k > implement their own version of find_first_bit(), and everyone else > uses the canonical implementation: And those who enable GENERIC_FIND_FIRST_BIT - x86, arm64, arc, mips and s390. > #ifndef find_first_bit > #define find_first_bit(addr, size) find_next_bit((addr), (size), 0) > #endif > > These architectures explicitly have different implementations for > find_first_bit() and find_next_bit() because they can do better > (whether that is true or not is another debate). I don't think you > should remove this optimisation until it has been measured on these > two architectures. This patch is based on a series that enables separate implementation of find_first_bit() for all architectures; according to my tests, find_first* is ~ twice faster than find_next* on arm64 and x86. https://lore.kernel.org/lkml/20210612123639.329047-1-yury.norov@gmail.com/T/#t After applying the series, I noticed that my small kernel module that calls for_each_set_bit() is now using find_first_bit() to just find one bit, and find_next_bit() for all others. I think it's better to always use find_next_bit() in this case to minimize the chance of cache miss. But if it's not that obvious, I'll try to write some test.