mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nikolay Borisov <nik.borisov@suse.com>
To: Borislav Petkov <bp@alien8.de>, Kuan-Wei Chiu <visitorckw@gmail.com>
Cc: linux-edac@vger.kernel.org, linux-kernel@vger.kernel.org,
	Yazen.Ghannam@amd.com
Subject: Re: [PATCH v2] RAS/AMD/ATL: Remove bitwise_xor_bits
Date: Mon, 24 Nov 2025 15:24:40 +0200	[thread overview]
Message-ID: <3e191e87-5b7f-49e9-b794-eb244d478c56@suse.com> (raw)
In-Reply-To: <20251124125249.GDaSRVIapy2dmis28p@fat_crate.local>



On 11/24/25 14:52, Borislav Petkov wrote:
> On Mon, Nov 24, 2025 at 08:03:02PM +0800, Kuan-Wei Chiu wrote:
>> On Mon, Nov 24, 2025 at 12:05:26PM +0100, Borislav Petkov wrote:
>>> On Mon, Nov 24, 2025 at 04:57:51PM +0800, Kuan-Wei Chiu wrote:
>>>>> Both LLVM/GCC support a __builtin_parity function which is functionally
>>>>> equivalent to the custom bitwise_xor_bits() one. Let's simplify the code by
>>>>> relying on the built-in. No functional changes.
>>>>
>>>> IIRC in some cases,
>>>
>>> Which are those cases?
>>>
>>> Do you have a trigger scenario?
>>>
>> I did a quick search, and I believe it was this kernel test robot
>> report [1] that reminded me of this compiler behavior.
>>
>> [1]: https://lore.kernel.org/oe-kbuild-all/202501312159.l6jNRaYy-lkp@intel.com/
> 
> Interesting, thanks for the pointer.
> 
> @Nik, just use hweight16() but pls do check what asm the compiler generates
> before and after so that at least there's some palpable improvement or gcc is
> smart enough to replace the unrolled XORing with something slick.
> 
> Also put in the commit message why we're not using the builtin parity thing
> and quote the link above.
> 
> Thx.
> 


So the custom function one results in the following asm being inlined i.e 4 total copies:

# drivers/ras/amd/atl/umc.c:59:                 tmp ^= (val >> i) & 0x1;
         xorl    %ecx, %ecx      # ivtmp.157
# drivers/ras/amd/atl/umc.c:55:         u16 tmp = 0;
         xorl    %esi, %esi      # tmp
# drivers/ras/amd/atl/umc.c:253:                temp  = bitwise_xor_bits(col & addr_hash.bank[i].col_xor);
         andl    %r13d, %edx     # col, tmp249
# drivers/ras/amd/atl/umc.c:59:                 tmp ^= (val >> i) & 0x1;
         movzwl  %dx, %edx       # tmp249, _387
.L3:
         movl    %edx, %eax      # _387, tmp250
         sarl    %cl, %eax       # ivtmp.157, tmp250
# drivers/ras/amd/atl/umc.c:58:         for (i = 0; i < 16; i++)
         addl    $1, %ecx        #, ivtmp.157
# drivers/ras/amd/atl/umc.c:59:                 tmp ^= (val >> i) & 0x1;
         andl    $1, %eax        #, tmp251
# drivers/ras/amd/atl/umc.c:59:                 tmp ^= (val >> i) & 0x1;
         xorl    %eax, %esi      # tmp251, tmp
# drivers/ras/amd/atl/umc.c:58:         for (i = 0; i < 16; i++)
         cmpl    $16, %ecx       #, ivtmp.157
         jne     .L3     #,


Whilst with hweight, if the cpu has popcnt it will be 2 instructions - popcnt and an andl:

# drivers/ras/amd/atl/umc.c:243:                temp ^= hweight16(row & addr_hash.bank[i].row_xor) & 1;
         andl    %ebp, %edi      # _321, tmp221
# ./arch/x86/include/asm/arch_hweight.h:19:     asm_inline (ALTERNATIVE("call __sw_hweight32",
#APP
# 19 "./arch/x86/include/asm/arch_hweight.h" 1
         # ALT: oldinstr
771:
         call __sw_hweight32
772:
# ALT: padding
.skip -(((775f-774f)-(772b-771b)) > 0) * ((775f-774f)-(772b-771b)),0x90
773:
.pushsection .altinstructions,"a"
  .long 771b - .
  .long 774f - .
  .4byte ( 4*32+23)
  .byte 773b-771b
  .byte 775f-774f
.popsection
.pushsection .altinstr_replacement, "ax"
# ALT: replacement
774:
         popcntl %edi, %eax      # tmp221, res
775:
.popsection
                                                                                 
# 0 "" 2
# drivers/ras/amd/atl/umc.c:243:                temp ^= hweight16(row & addr_hash.bank[i].row_xor) & 1;
#NO_APP
         xorl    %eax, %edx      # res, tmp222
         andl    $1, %edx        #, temp


So yeah, much better IMHO, unless there is some hidden latency in the popcnt...

  reply	other threads:[~2025-11-24 13:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-24  8:40 Nikolay Borisov
2025-11-24  8:57 ` Kuan-Wei Chiu
2025-11-24 11:05   ` Borislav Petkov
2025-11-24 12:03     ` Kuan-Wei Chiu
2025-11-24 12:52       ` Borislav Petkov
2025-11-24 13:24         ` Nikolay Borisov [this message]
2025-11-24 13:34           ` Borislav Petkov
2025-11-25  9:35 ` david laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3e191e87-5b7f-49e9-b794-eb244d478c56@suse.com \
    --to=nik.borisov@suse.com \
    --cc=Yazen.Ghannam@amd.com \
    --cc=bp@alien8.de \
    --cc=linux-edac@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=visitorckw@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®