[v3] powerpc: Fix static-linked version of __ppc_get_timebase_freq [BZ #24640]

Message ID 20190619201535.18815-1-raoni@linux.ibm.com
State Committed
Delegated to: Tulio Magno Quites Machado Filho
Headers

Commit Message

Raoni Fassina Firmino June 19, 2019, 8:15 p.m. UTC
  From: Stan Shebs <stanshebs@google.com>

Originally part of a larger pathset[1].

Changes since v2:

  - Change how added new testcase in Makefile

Changes since v1:

  - Merged fix & testcase in one patch.
    Originally patches #1 and #2[2,3].

[1] https://www.sourceware.org/ml/libc-alpha/2019-06/msg00283.html
[2] https://www.sourceware.org/ml/libc-alpha/2019-06/msg00284.html
[3] https://www.sourceware.org/ml/libc-alpha/2019-06/msg00285.html

---- 8< ----

__ppc_get_timebase_freq() always return 0 when using static linked
glibc.

This is a minimal example.c to reproduce:

    /******************************/
    #include <inttypes.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <sys/platform/ppc.h>

    int main() {
        uint64_t freq = __ppc_get_timebase_freq();
        printf("Time Base frequency = %"PRIu64" Hz\n", freq);
        if (freq == 0)
            return -1;
        return 0;
    }
    /******************************/

Compile command: gcc -static example.c

This bug has been reproduced, fixed and tested on all powerpc platforms
(ppc32, ppc64 and ppc64le).

The underlying code of __ppc_get_timebase_freq uses __get_timebase_freq
that has a different implementation for shared and static version of
glibc.  In the static version, there is an incorrect sense in the if
check for the fd returned when opening /proc/cpuinfo.

This solution is mostly a cherry-pick from:

  commit 4791e4f773d060c1a37b27aac5b03cdfa9327afc
  Author: Stan Shebs <stanshebs@google.com>
  Date:   Fri May 17 12:25:19 2019 -0700
  Subject: Fix sense of a test in the static-linking version of ppc get_clockfreq

That is in branch glibc/google/grte/v5-2.27/master and was mentioned for
inclusion on master here:

  https://www.sourceware.org/ml/libc-alpha/2019-05/msg00409.html

Adapted from original fix for get_clockfreq. That code was moved to
get_timebase_freq.

Also added a static-build testcase for __ppc_get_timebase_freq since the
underlying function has different implementations for shared and static
build.

ChangeLog:

2019-06-19  Stan Shebs  <stanshebs@google.com>
	    Raoni Fassina Firmino  <raoni@linux.ibm.com>

	[BZ #24640]
	* sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c
	[!SHARED] (__get_timebase_freq): Fix sense of a test in the
	static-linking version.

	* sysdeps/unix/sysv/linux/powerpc/Makefile
	(tests-static): Add test-gettimebasefreq-static.
	(tests): Likewise.
	* sysdeps/unix/sysv/linux/powerpc/test-gettimebasefreq-static.c:
	New file.
---
 sysdeps/unix/sysv/linux/powerpc/Makefile      |  2 ++
 .../sysv/linux/powerpc/get_timebase_freq.c    |  2 +-
 .../powerpc/test-gettimebasefreq-static.c     | 19 +++++++++++++++++++
 3 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 sysdeps/unix/sysv/linux/powerpc/test-gettimebasefreq-static.c
  

Comments

Adhemerval Zanella Netto June 19, 2019, 8:33 p.m. UTC | #1
On 19/06/2019 17:15, Raoni Fassina Firmino wrote:
> From: Stan Shebs <stanshebs@google.com>
> 
> Originally part of a larger pathset[1].
> 
> Changes since v2:
> 
>   - Change how added new testcase in Makefile
> 
> Changes since v1:
> 
>   - Merged fix & testcase in one patch.
>     Originally patches #1 and #2[2,3].
> 
> [1] https://www.sourceware.org/ml/libc-alpha/2019-06/msg00283.html
> [2] https://www.sourceware.org/ml/libc-alpha/2019-06/msg00284.html
> [3] https://www.sourceware.org/ml/libc-alpha/2019-06/msg00285.html
> 
> ---- 8< ----
> 
> __ppc_get_timebase_freq() always return 0 when using static linked
> glibc.
> 
> This is a minimal example.c to reproduce:
> 
>     /******************************/
>     #include <inttypes.h>
>     #include <stdint.h>
>     #include <stdio.h>
>     #include <sys/platform/ppc.h>
> 
>     int main() {
>         uint64_t freq = __ppc_get_timebase_freq();
>         printf("Time Base frequency = %"PRIu64" Hz\n", freq);
>         if (freq == 0)
>             return -1;
>         return 0;
>     }
>     /******************************/
> 
> Compile command: gcc -static example.c
> 
> This bug has been reproduced, fixed and tested on all powerpc platforms
> (ppc32, ppc64 and ppc64le).
> 
> The underlying code of __ppc_get_timebase_freq uses __get_timebase_freq
> that has a different implementation for shared and static version of
> glibc.  In the static version, there is an incorrect sense in the if
> check for the fd returned when opening /proc/cpuinfo.
> 
> This solution is mostly a cherry-pick from:
> 
>   commit 4791e4f773d060c1a37b27aac5b03cdfa9327afc
>   Author: Stan Shebs <stanshebs@google.com>
>   Date:   Fri May 17 12:25:19 2019 -0700
>   Subject: Fix sense of a test in the static-linking version of ppc get_clockfreq
> 
> That is in branch glibc/google/grte/v5-2.27/master and was mentioned for
> inclusion on master here:
> 
>   https://www.sourceware.org/ml/libc-alpha/2019-05/msg00409.html
> 
> Adapted from original fix for get_clockfreq. That code was moved to
> get_timebase_freq.
> 
> Also added a static-build testcase for __ppc_get_timebase_freq since the
> underlying function has different implementations for shared and static
> build.
> 
> ChangeLog:
> 
> 2019-06-19  Stan Shebs  <stanshebs@google.com>
> 	    Raoni Fassina Firmino  <raoni@linux.ibm.com>
> 
> 	[BZ #24640]
> 	* sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c
> 	[!SHARED] (__get_timebase_freq): Fix sense of a test in the
> 	static-linking version.
> 
> 	* sysdeps/unix/sysv/linux/powerpc/Makefile
> 	(tests-static): Add test-gettimebasefreq-static.
> 	(tests): Likewise.
> 	* sysdeps/unix/sysv/linux/powerpc/test-gettimebasefreq-static.c:
> 	New file.

LGTM, do you need someone to push it upstream for you?

> ---
>  sysdeps/unix/sysv/linux/powerpc/Makefile      |  2 ++
>  .../sysv/linux/powerpc/get_timebase_freq.c    |  2 +-
>  .../powerpc/test-gettimebasefreq-static.c     | 19 +++++++++++++++++++
>  3 files changed, 22 insertions(+), 1 deletion(-)
>  create mode 100644 sysdeps/unix/sysv/linux/powerpc/test-gettimebasefreq-static.c
> 
> diff --git a/sysdeps/unix/sysv/linux/powerpc/Makefile b/sysdeps/unix/sysv/linux/powerpc/Makefile
> index 19ac59d862..1596238afa 100644
> --- a/sysdeps/unix/sysv/linux/powerpc/Makefile
> +++ b/sysdeps/unix/sysv/linux/powerpc/Makefile
> @@ -25,6 +25,8 @@ endif
>  ifeq ($(subdir),misc)
>  sysdep_headers += bits/ppc.h
>  sysdep_routines += get_timebase_freq
> +tests-static += test-gettimebasefreq-static
> +tests += $(tests-static)
>  tests += test-gettimebasefreq
>  tests += test-powerpc-linux-sysconf
>  endif
> diff --git a/sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c b/sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c
> index 23e7694d87..c245e97526 100644
> --- a/sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c
> +++ b/sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c
> @@ -39,7 +39,7 @@ __get_timebase_freq (void)
>       timebase        : 33333333
>       We search for this line and convert the number into an integer.  */
>    int fd = __open_nocancel ("/proc/cpuinfo", O_RDONLY);
> -  if (__glibc_likely (fd != -1))
> +  if (__glibc_unlikely (fd == -1))
>      return result;
>  
>    /* The timebase will be in the 1st 1024 bytes for systems with up
> diff --git a/sysdeps/unix/sysv/linux/powerpc/test-gettimebasefreq-static.c b/sysdeps/unix/sysv/linux/powerpc/test-gettimebasefreq-static.c
> new file mode 100644
> index 0000000000..58b4eee98d
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/powerpc/test-gettimebasefreq-static.c
> @@ -0,0 +1,19 @@
> +/* Check __ppc_get_timebase_freq() for architecture changes
> +   Copyright (C) 2019 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <http://www.gnu.org/licenses/>.  */
> +
> +#include "test-gettimebasefreq.c"
>
  
Tulio Magno Quites Machado Filho June 19, 2019, 10:14 p.m. UTC | #2
Raoni Fassina Firmino <raoni@linux.ibm.com> writes:

> __ppc_get_timebase_freq() always return 0 when using static linked
> glibc.
>
> This is a minimal example.c to reproduce:
>
>     /******************************/
>     #include <inttypes.h>
>     #include <stdint.h>
>     #include <stdio.h>
>     #include <sys/platform/ppc.h>
>
>     int main() {
>         uint64_t freq = __ppc_get_timebase_freq();
>         printf("Time Base frequency = %"PRIu64" Hz\n", freq);
>         if (freq == 0)
>             return -1;
>         return 0;
>     }
>     /******************************/
>
> Compile command: gcc -static example.c
>
> This bug has been reproduced, fixed and tested on all powerpc platforms
> (ppc32, ppc64 and ppc64le).
>
> The underlying code of __ppc_get_timebase_freq uses __get_timebase_freq
> that has a different implementation for shared and static version of
> glibc.  In the static version, there is an incorrect sense in the if
> check for the fd returned when opening /proc/cpuinfo.
>
> This solution is mostly a cherry-pick from:
>
>   commit 4791e4f773d060c1a37b27aac5b03cdfa9327afc
>   Author: Stan Shebs <stanshebs@google.com>
>   Date:   Fri May 17 12:25:19 2019 -0700
>   Subject: Fix sense of a test in the static-linking version of ppc get_clockfreq
>
> That is in branch glibc/google/grte/v5-2.27/master and was mentioned for
> inclusion on master here:
>
>   https://www.sourceware.org/ml/libc-alpha/2019-05/msg00409.html
>
> Adapted from original fix for get_clockfreq. That code was moved to
> get_timebase_freq.
>
> Also added a static-build testcase for __ppc_get_timebase_freq since the
> underlying function has different implementations for shared and static
> build.

LGTM too.

Pushed as 335c1007bf18.

Thanks!!
  
Raoni Fassina Firmino June 21, 2019, 5:59 p.m. UTC | #3
On Wed, Jun 19, 2019 at 05:33:58PM -0300, Adhemerval Zanella wrote:
> LGTM, do you need someone to push it upstream for you?

Thanks for asking, Indeed I needed, but Tulio already pushed it.


o/
Raoni
  

Patch

diff --git a/sysdeps/unix/sysv/linux/powerpc/Makefile b/sysdeps/unix/sysv/linux/powerpc/Makefile
index 19ac59d862..1596238afa 100644
--- a/sysdeps/unix/sysv/linux/powerpc/Makefile
+++ b/sysdeps/unix/sysv/linux/powerpc/Makefile
@@ -25,6 +25,8 @@  endif
 ifeq ($(subdir),misc)
 sysdep_headers += bits/ppc.h
 sysdep_routines += get_timebase_freq
+tests-static += test-gettimebasefreq-static
+tests += $(tests-static)
 tests += test-gettimebasefreq
 tests += test-powerpc-linux-sysconf
 endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c b/sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c
index 23e7694d87..c245e97526 100644
--- a/sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c
+++ b/sysdeps/unix/sysv/linux/powerpc/get_timebase_freq.c
@@ -39,7 +39,7 @@  __get_timebase_freq (void)
      timebase        : 33333333
      We search for this line and convert the number into an integer.  */
   int fd = __open_nocancel ("/proc/cpuinfo", O_RDONLY);
-  if (__glibc_likely (fd != -1))
+  if (__glibc_unlikely (fd == -1))
     return result;
 
   /* The timebase will be in the 1st 1024 bytes for systems with up
diff --git a/sysdeps/unix/sysv/linux/powerpc/test-gettimebasefreq-static.c b/sysdeps/unix/sysv/linux/powerpc/test-gettimebasefreq-static.c
new file mode 100644
index 0000000000..58b4eee98d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/powerpc/test-gettimebasefreq-static.c
@@ -0,0 +1,19 @@ 
+/* Check __ppc_get_timebase_freq() for architecture changes
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include "test-gettimebasefreq.c"