svn commit: r280279 - head/sys/sys

Konstantin Belousov kostikbel at gmail.com
Sat Mar 21 16:35:43 UTC 2015


On Sat, Mar 21, 2015 at 12:04:41PM -0400, John Baldwin wrote:
> On 3/20/15 9:02 AM, Konstantin Belousov wrote:
> > On Fri, Mar 20, 2015 at 10:27:06AM +0000, John Baldwin wrote:
> >> Author: jhb
> >> Date: Fri Mar 20 10:27:06 2015
> >> New Revision: 280279
> >> URL: https://svnweb.freebsd.org/changeset/base/280279
> >>
> >> Log:
> >>   Expand the bitcount* API to support 64-bit integers, plain ints and longs
> >>   and create a "hidden" API that can be used in other system headers without
> >>   adding namespace pollution.
> >>   - If the POPCNT instruction is enabled at compile time, use
> >>     __builtin_popcount*() to implement __bitcount*(), otherwise fall back
> >>     to software implementations.
> > Are you aware of the Haswell errata HSD146 ?  I see the described behaviour
> > on machines back to SandyBridge, but not on Nehalems.
> > HSD146.   POPCNT Instruction May Take Longer to Execute Than Expected
> > Problem: POPCNT instruction execution with a 32 or 64 bit operand may be
> > delayed until previous non-dependent instructions have executed.
> > 
> > Jilles noted that gcc head and 4.9.2 already provides a workaround by
> > xoring the dst register.  I have some patch for amd64 pmap, see the end
> > of the message.
> 
> No, I was not aware, but I think it's hard to fix this anywhere but the
> compiler.  I set CPUTYPE in src.conf on my Ivy Bridge desktop and clang
> uses POPCOUNT for this function from ACPI-CA:
> 
> static UINT8
> AcpiRsCountSetBits (
>     UINT16                  BitField)
> {
>     UINT8                   BitsSet;
> 
> 
>     ACPI_FUNCTION_ENTRY ();
> 
> 
>     for (BitsSet = 0; BitField; BitsSet++)
>     {
>         /* Zero the least significant bit that is set */
> 
>         BitField &= (UINT16) (BitField - 1);
>     }
> 
>     return (BitsSet);
> }
> 
> (I ran into this accidentally because a kernel built on my system failed
> to boot in older qemu because the kernel paniced with an illegal instruction
> fault in this function.)
> 
> There's no way we can preemptively locate every bit of C that clang might
> decide to replace with popcount and fix them to employ a workaround.  I think
> the only viable solution is to use "-mno-popcount" on all compilers that aren't
> known to be fixed.
Both you and Bruce and Jilles state this, but I am unable to understand the
argument about the compiler.  Can somebody explain this to me, please ?

Compiler cannot generate popcntq instructions unless it is directed to
generate code not for amd64, but for some specific microacrchitecture.
Any use of bitcount in generic kernel or userspace is going to be a
SWAR. In other words, #ifdef __POPCNT__ is for non-default settings of
the FreeBSD compilation environment.

And even then, compiler must issue cpuid to fetch the feature mask, which
arguably compiler cannot do due to the serialization instruction cost.
Compiler could generate the call to instruction in the init code, but
this is impossible for freestanding environment, since compiler does not
know where to put init code.

Only explicit uses of popcnt in the asm, with the CPU feature check around,
going to end actually use the instruction in the generic code.  And there,
we may apply the workaround like I did for pmap.

> 
> In regards to whether or not this should be a dynamic test instead, it seems a
> bit much to require a dynamic check for code that has to work in both userland
> and the kernel, and I'm not sure if something like CPU_COUNT() would actually
> benefit from that versus just always using the software solution instead.

I agree.



More information about the svn-src-head mailing list