From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5E9B136E for ; Sat, 18 Jan 2025 07:32:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737185556; cv=none; b=WSWTvQqrGXzn4iIXPAikVo8kjAqDDuJU+UmyjddvfcsOdsYw1XOe3GDLfEU8kbEzNI0eP1BgIb+uYhIT/XApMeW8PX7NT7VaF7BfOUslyE0neZLQEFgmMLFWqwjHuUFirYDJp4w/TEaxekYiJbTzcPH6MIsNKfnfYNdJiEpwaSc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737185556; c=relaxed/simple; bh=h5i1lHPCVUDi71IemBehb7zaMELJVERxNNzJ0/4lyJU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=s2Ln11gwhZVlTKfnuRyYUllKfi3L8Mku+bSxkWVrRREZVWHVIA92e4LMiPcPsEsoJ2ivx+yRdQu7gB1QrBpzk58blmiEpEr1dFzeHAXGAAP+dafvkhqwvzGt4ynZleTAlHEWR6QAG9CCY37osTb0EMWapjo7tRcVldYSXFXUHVs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ApdmT04E; arc=none smtp.client-ip=209.85.214.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ApdmT04E" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-216426b0865so51528555ad.0 for ; Fri, 17 Jan 2025 23:32:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737185554; x=1737790354; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=XQWS0ZH+WKSl1bSS/FbS0i0KfU4s2kpmSXFrB+3udoA=; b=ApdmT04EL6eaBwYV5uq4khO4xLIRYHGtgyIqKmZ+mRU05u4Vwux/0OAynxL3KNEj6H uNFMSCiOecIAd/qwuM6gfz3V1BKfxHzNdW8ijoEuwu4Dd0fV82Yi0EQamexCmIQlA7iA rBKIQFVQg3OptDKK8mqoVwQGej3MQ1b3vW1qFjr2vbOak2VgrVL98bXo6fdx15nwts3y ba3zcb0EKCJERoTjkyJju8had6lhWXTQDkZlzymH5PYhqA/U4VB2C2LDzhGZcHx71e4M w/RE/E2L8ZbaLw863mxMra49yNmXpFSzXIWB+7JpSA1vGvtHQsq1c534ByD224fIcIvI 30Vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737185554; x=1737790354; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=XQWS0ZH+WKSl1bSS/FbS0i0KfU4s2kpmSXFrB+3udoA=; b=peLi/4IAiqPxJiUeeSCPRaDx/dDYQQG/gK/kPPuTEyAlLIxLFMoGAeRtqU3KKrYzre oNP9I8iUNza7Zt8q/gn1+yUOh6XSP97AjeG3LIys0WR18WwwJmIqLPiXzWzzfVM2R3zi f5xXLlYAAtPcVAfnGC9g89XEdNVbINN5bC0WnldDmGp86MlY+4BIgghVCfH3Hw3RD/rX ZCt+dC2fcNVJkghfI+CtipjbWI/sz0aXCMWzYYBA4KFS5+e6KtDgwZrEDNc2J1MJH+u1 yM+yY567uO/g7llLOf3pIX28eJDK5TW//7Y0aUvFrwaU/o7b2CLE91FZJkj/KQOoQHTf 2wFg== X-Forwarded-Encrypted: i=1; AJvYcCXS7fTCDrZ3GeSu3TjCjyiUXtefcLq8lg24CZtyO5iIbqm69JJc4yPVhP24g3FxMyqBrMwB0HHicoShvEQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyMTfn0ODimN2KQzmJO13z57VGWhjDtTtZmTH8nxNuenE7dsWEF UuSeDSYcEMzMjfN75X8gOo76QsS4HzI9BqM75odPHCX/f2I27Gq5wwCTxA== X-Gm-Gg: ASbGncucgn2Bw98cRABZd+c8Sc9PX96JS0fI6G03yBHaemDgDC+bEtA+3KxmIhPbk8c 14UKZj6W9x1BZy5akM9jdicCsjd62hUD680CoFHjmIjfik17q2kOOC30pMvct5xaYimEFzBaItv gsEtgAfYv9NeQDnAp1FX3+xeAZUOVQewz983BMnN46jd77c/O/pyCZVyZkob+824GO+wn7ZKZl7 4SEHxc5rygpOQtjti7b3aAGDLDmCaFh1eSDCAfLQ5QjiIXByNAiEvmFj/uw3rxUoeNJOKrb7Uks CPP1YGUdTw6faW4WDOC6AA== X-Google-Smtp-Source: AGHT+IGlB/cLs5Y6yGclPUiejX1NQfWW8qecGso0q2EY4dd6KXgtEFhYI/kI6QTkw25mVxmg8xpuZw== X-Received: by 2002:a05:6a00:418d:b0:71e:21:d2d8 with SMTP id d2e1a72fcca58-72daf946789mr9438225b3a.7.1737185553776; Fri, 17 Jan 2025 23:32:33 -0800 (PST) Received: from visitorckw-System-Product-Name ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72daba4956esm3078737b3a.128.2025.01.17.23.32.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jan 2025 23:32:33 -0800 (PST) Date: Sat, 18 Jan 2025 15:32:29 +0800 From: Kuan-Wei Chiu To: Yury Norov Cc: I Hsin Cheng , linux@rasmusvillemoes.dk, jserv@ccns.ncku.edu.tw, mark.rutland@arm.com, linux-kernel@vger.kernel.org, eleanor15x@gmail.com Subject: Re: [PATCH] cpumask: Optimize cpumask_any_but() Message-ID: References: <20250117142658.297325-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Hi Yury, On Fri, Jan 17, 2025 at 11:32:54AM -0500, Yury Norov wrote: > On Fri, Jan 17, 2025 at 10:59:31PM +0800, I Hsin Cheng wrote: > > On Fri, Jan 17, 2025 at 10:26:58PM +0800, Kuan-Wei Chiu wrote: > > > The cpumask_any_but() function can avoid using a loop to determine the > > > CPU index to return. If the first set bit in the cpumask is not equal > > > to the specified CPU, we can directly return the index of the first set > > > bit. Otherwise, we return the next set bit's index. > > > > > > This optimization replaces the loop with a single if statement, > > > allowing the compiler to generate more concise and efficient code. > > I thought compilers are smart enough to unroll loop in this case. Can > you show disassembled code before and after? > Since cpumask_any_but() is an inline function, I added the following to lib/cpumask.c for convenience: unsigned int non_inline_cpumask_any_but(const struct cpumask *mask, unsigned int cpu); unsigned int non_inline_cpumask_any_but(const struct cpumask *mask, unsigned int cpu) { return cpumask_any_but(mask, cpu); } I used objdump -d ./lib/cpumask.o to compare the differences. * Before the patch: 00000000000001f0 : 1f0: f3 0f 1e fa endbr64 1f4: 48 8b 3f mov (%rdi),%rdi 1f7: b8 40 00 00 00 mov $0x40,%eax 1fc: 48 85 ff test %rdi,%rdi 1ff: 74 4b je 24c 201: f3 48 0f bc d7 tzcnt %rdi,%rdx 206: 89 d0 mov %edx,%eax 208: 39 d6 cmp %edx,%esi 20a: 75 40 jne 24c 20c: 83 fa 3f cmp $0x3f,%edx 20f: 77 3b ja 24c 211: 41 b8 01 00 00 00 mov $0x1,%r8d 217: 83 c0 01 add $0x1,%eax 21a: 83 f8 40 cmp $0x40,%eax 21d: 74 2d je 24c 21f: 89 c1 mov %eax,%ecx 221: 4c 89 c2 mov %r8,%rdx 224: 48 d3 e2 shl %cl,%rdx 227: 48 89 d0 mov %rdx,%rax 22a: 48 f7 d8 neg %rax 22d: 48 21 f8 and %rdi,%rax 230: 74 15 je 247 232: f3 48 0f bc d0 tzcnt %rax,%rdx 237: 89 d0 mov %edx,%eax 239: 39 d6 cmp %edx,%esi 23b: 75 0f jne 24c 23d: 83 fa 3f cmp $0x3f,%edx 240: 76 d5 jbe 217 242: e9 00 00 00 00 jmp 247 247: b8 40 00 00 00 mov $0x40,%eax 24c: e9 00 00 00 00 jmp 251 * After the patch: 00000000000001f0 : 1f0: f3 0f 1e fa endbr64 1f4: 48 8b 17 mov (%rdi),%rdx 1f7: 48 85 d2 test %rdx,%rdx 1fa: 74 34 je 230 1fc: f3 48 0f bc ca tzcnt %rdx,%rcx 201: 89 c8 mov %ecx,%eax 203: 39 ce cmp %ecx,%esi 205: 75 2e jne 235 207: 83 c1 01 add $0x1,%ecx 20a: 83 f9 3f cmp $0x3f,%ecx 20d: 77 21 ja 230 20f: 48 c7 c0 ff ff ff ff mov $0xffffffffffffffff,%rax 216: 48 d3 e0 shl %cl,%rax 219: 48 89 c1 mov %rax,%rcx 21c: b8 40 00 00 00 mov $0x40,%eax 221: 48 21 d1 and %rdx,%rcx 224: 74 0f je 235 226: f3 48 0f bc c1 tzcnt %rcx,%rax 22b: e9 00 00 00 00 jmp 230 230: b8 40 00 00 00 mov $0x40,%eax 235: e9 00 00 00 00 jmp 23a > > > > > > As a result, the size of the bzImage built with x86 defconfig is > > > reduced by 4096 bytes: > > > > > > * Before: > > > $ size arch/x86/boot/bzImage > > > text data bss dec hex filename > > > 13537280 1024 0 13538304 ce9400 arch/x86/boot/bzImage > > > > > > * After: > > > $ size arch/x86/boot/bzImage > > > text data bss dec hex filename > > > 13533184 1024 0 13534208 ce8400 arch/x86/boot/bzImage > > Comparing zipped images tells little about code generation. Please use > scripts/bloat-o-meter. > $ ./scripts/bloat-o-meter ./old_cpumask.o ./new_cpumask.o add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-23 (-23) Function old new delta non_inline_cpumask_any_but 97 74 -23 Total: Before=522, After=499, chg -4.41% > > > > > > Co-developed-by: Yu-Chun Lin > > > Signed-off-by: Yu-Chun Lin > > > Signed-off-by: Kuan-Wei Chiu > > > --- > > > Not sure how to measure the efficiency difference, but I guess this > > > patch might be slightly more efficient or nearly the same as before. If > > > you have any good ideas for measuring efficiency, please let me know! > > Check lib/find_bit_benchmark.c > > > > > > > include/linux/cpumask.h | 8 ++++---- > > > 1 file changed, 4 insertions(+), 4 deletions(-) > > > > > > diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h > > > index 9278a50d514f..b769fcdbaa10 100644 > > > --- a/include/linux/cpumask.h > > > +++ b/include/linux/cpumask.h > > > @@ -404,10 +404,10 @@ unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu) > > > unsigned int i; > > > > > > cpumask_check(cpu); > > > - for_each_cpu(i, mask) > > > - if (i != cpu) > > > - break; > > > - return i; > > > + i = find_first_bit(cpumask_bits(mask), small_cpumask_bits); > > > > Hi Kuan-Wei, > > > > How about using cpumask_first(mask) here to keep better consistency? > > I would do it the other way: introduce find_first_but_bit(), and then > make cpumask_any_but() a wrapper around it. Doing this you'll be able > to leverage find_bit_benchmark infrastructure to measure performance > difference, if any. > I'll try to conduct this experiment. Regards, Kuan-Wei > > > + if (i != cpu) > > > + return i; > > Wouldn't it benefit abit to check "i >= nr_cpu_ids" prior to > > find_next_bit() ? > > Yes it would. > > Thanks, > Yury > > > if "i >= nr_cpu_ids" holds it would be a waste to > > perform find_next_bit(). > > > > > + return find_next_bit(cpumask_bits(mask), small_cpumask_bits, i + 1); > > > } > > > > > > > Regards, > > I Hsin > > > > > /** > > > -- > > > 2.34.1 > > >