Bug 13753

Summary: [2.15 backport] Really fix AVX tests
Product: glibc Reporter: Allan McRae <allan>
Component: libcAssignee: Carlos O'Donell <carlos>
Status: RESOLVED FIXED    
Severity: normal CC: aj, carlos_odonell, drepper.fsp, law
Priority: P2 Keywords: glibc_2.15
Version: 2.15Flags: fweimer: security-
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description Allan McRae 2012-02-25 01:33:41 UTC
The following commit(s) should be backported to glibc 2.15:

commit 08cf777f9e7f6d826658a99c7d77a359f73a45bf
Author: Ulrich Drepper <drepper@gmail.com>
Date:   Thu Jan 26 09:45:54 2012 -0500

    Really fix AVX tests

which also reverts the previous commit:

commit afc5ed09cbce5d6fd48b3a8c5ec427b31f996880
Author: Ulrich Drepper <drepper@gmail.com>
Date:   Thu Jan 26 07:45:14 2012 -0500

    Reset bit_AVX in __cpu_features is OS support is missing
Comment 1 Carlos O'Donell 2012-05-02 02:27:59 UTC
In the process of backporting.
Comment 2 Andreas Jaeger 2012-05-08 13:45:11 UTC
See bug #14059, you should add the following part from my patch as well (full patch for review at http://sourceware.org/ml/libc-alpha/2012-05/msg00321.html):

2012-05-08  Jim Westfall  <jwestfall@surrealistic.net>

        [BZ #14059]
        * sysdeps/x86_64/multiarch/init-arch.c (__init_cpu_features):
        Disable FMA4 if AVX is disabled.

+  /* FMA4 depends on AVX support.  */
+  if (__cpu_features.cpuid[COMMON_CPUID_INDEX_80000001].ecx & bit_FMA4
+      && (__cpu_features.cpuid[COMMON_CPUID_INDEX_1].ecx & bit_AVX) == 0)
+    __cpu_features.cpuid[COMMON_CPUID_INDEX_80000001].ecx &= ~bit_FMA4;
Comment 3 law 2012-05-10 19:06:41 UTC
I've also got reports that Uli's patch is wrong because it can leave AVX enabled even though OSXSAVE is off. 

My understanding is:

1. OSXSAVE must be enabled
2. AVX must be enabled
3. XGETBV must have XMM & XMM state enabled

Otherwise AVX can not be used.

This (for example) causes Xen to fail miserably.
Comment 4 Carlos O'Donell 2012-05-10 20:25:46 UTC
Jeff,

Do the commits and patch in question in this issue fix AVX usage for you? In other words will we be backporting the right stuff for 2.15.1?
Comment 5 law 2012-05-10 20:49:18 UTC
No, the code on the trunk plus the patch in this BZ are not sufficient to fix the problem AFAIK. Unfortunately I don't have the right hardware to do testing, so I'm relying on other folks.

I've got a change which I hope fixes this problem, but I'm going to have to wait for feedback.  Based on my understanding we really need to avoid using any AVX if the OS doesn't support XSAVE, even for things which don't use the YMM regs.
Comment 6 Allan McRae 2012-05-10 22:02:58 UTC
@Jeff: I have access to a Xen system to test these issues, so if you post a link to the patch I can test.

Note that with the two original patches I still get issues in strcasecmp() with illegal instructions with AVX:

#0  0x00007ffff76cd0ff in __strcasecmp_l_avx () from ./lib/libc.so.6

   0x00007ffff76cd0f7 <+23>:	and    $0x3f,%rcx
   0x00007ffff76cd0fb <+27>:	and    $0x3f,%rax
=> 0x00007ffff76cd0ff <+31>:	vmovdqa 0x46979(%rip),%xmm4        # 0x7ffff7713a80
   0x00007ffff76cd107 <+39>:	vmovdqa 0x469a1(%rip),%xmm5        # 0x7ffff7713ab0
   0x00007ffff76cd10f <+47>:	vmovdqa 0x46989(%rip),%xmm6        # 0x7ffff7713aa0

I will test with the additional patches linked in this bug report.


BTW, just to confirm how this should be done, the intel manual [1] states:
1) Detect CPUID.1:ECX.OSXSAVE[bit 27] = 1 (XGETBV enabled for application use1)
2) Issue XGETBV and verify that XCR0[2:1] = ‘11b’ (XMM state and YMM state are enabled by OS).
3) detect CPUID.1:ECX.AVX[bit 28] = 1 (AVX instructions supported).
(Step 3 can be done in any order relative to 1 and 2)

http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-manual-325462-rmver.html - section 13.5
Comment 7 Carlos O'Donell 2012-05-10 22:20:58 UTC
(In reply to comment #6)
> @Jeff: I have access to a Xen system to test these issues, so if you post a
> link to the patch I can test.
> 
> Note that with the two original patches I still get issues in strcasecmp() with
> illegal instructions with AVX:
> 
> #0  0x00007ffff76cd0ff in __strcasecmp_l_avx () from ./lib/libc.so.6
> 
>    0x00007ffff76cd0f7 <+23>:    and    $0x3f,%rcx
>    0x00007ffff76cd0fb <+27>:    and    $0x3f,%rax
> => 0x00007ffff76cd0ff <+31>:    vmovdqa 0x46979(%rip),%xmm4        #
> 0x7ffff7713a80
>    0x00007ffff76cd107 <+39>:    vmovdqa 0x469a1(%rip),%xmm5        #
> 0x7ffff7713ab0
>    0x00007ffff76cd10f <+47>:    vmovdqa 0x46989(%rip),%xmm6        #
> 0x7ffff7713aa0
> 
> I will test with the additional patches linked in this bug report.
> 
> 
> BTW, just to confirm how this should be done, the intel manual [1] states:
> 1) Detect CPUID.1:ECX.OSXSAVE[bit 27] = 1 (XGETBV enabled for application use1)
> 2) Issue XGETBV and verify that XCR0[2:1] = ‘11b’ (XMM state and YMM state are
> enabled by OS).
> 3) detect CPUID.1:ECX.AVX[bit 28] = 1 (AVX instructions supported).
> (Step 3 can be done in any order relative to 1 and 2)
> 
> http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-manual-325462-rmver.html
> - section 13.5

Can you test this?
http://sourceware.org/ml/libc-alpha/2012-05/msg00607.html
Comment 8 law 2012-05-11 03:17:19 UTC
Allan, if you could test the patch referenced by Carlos in c#7, that'd be better than testing my hack.  

My hack (well actually Konrad's hack) is similar to what AJ posted on libc-alpha and resets cpu_features, which Carlos would like to avoid (and which I tend to agree is hackish).  We'll probably use my hack for f17 simply due to time constraints.  But for f18 and beyond Carlos's change or a variant thereof would be better.
Comment 9 Allan McRae 2012-05-11 05:36:24 UTC
I still get illegal instructions (vmovdqa) in __strcasecmp_l_avx () with Carlos' patch [1].  The patch from Andreas [2] works.

[1] http://sourceware.org/ml/libc-alpha/2012-05/msg00607.html
[2] http://sourceware.org/ml/libc-alpha/2012-05/msg00321.html

(Xen DomU, i7-2600, x86_64-unknown-linux-gnu)
Comment 10 Andreas Jaeger 2012-05-11 06:51:12 UTC
Allan, please test:

http://sourceware.org/ml/libc-alpha/2012-05/msg00619.html

And let's continue this discussion on libc-alpha where I posted the patch.
Comment 11 Carlos O'Donell 2012-05-17 14:10:17 UTC
Final fix is now on trunk.

http://sources.redhat.com/git/gitweb.cgi?p=glibc.git;a=commitdiff;h=1a0994f5356214e8af8a1c1cc33fbf74a7ac8993

I will backport this to 2.15.
Comment 12 Carlos O'Donell 2012-05-18 21:07:24 UTC
Backported final fix. Testing.
Comment 13 Carlos O'Donell 2012-05-18 21:29:34 UTC
Checked into 2.15.