Skip to contentSkip to conversation

tools/power turbostat: call pread64 in kernel directly

PATCH Applied Liwei Song 3 participants 7 messages Jul 17, 2020 – Sep 3, 2020 Turbostat utility

lore
Share & tools
lore

Liwei Song

author
#0 lore raw

PATCH 1/1 tools/power turbostat: call pread64 in kernel directly

with 32-bit rootfs, the offset may out of range when set it to 0xc0010299, define it as "unsigned long long" type and call pread64 directly in kernel.

---
 tools/power/x86/turbostat/turbostat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/power/x86/turbostat/turbostat.c +3 -2
index 33b370865d16..4c5cdfcb5721 100644
@@ -33,6 +33,7 @@
 #include <sys/capability.h>
 #include <errno.h>
 #include <math.h>
+#include <sys/syscall.h>
 
 char *proc_stat = "/proc/stat";
 FILE *outf;
@@ -381,11 +382,11 @@ int get_msr_fd(int cpu)
 	return fd;
 }
 
-int get_msr(int cpu, off_t offset, unsigned long long *msr)
+int get_msr(int cpu, unsigned long long offset, unsigned long long *msr)
 {
 	ssize_t retval;
 
-	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
+	retval = syscall(SYS_pread64, get_msr_fd(cpu), msr, sizeof(*msr), offset);
 
 	if (retval != sizeof *msr)
 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
Signature · 1 line
2.17.1
maintainer
#1 lore raw

Huh?

Quoted 39 lines · Liwei Song

with 32-bit rootfs, the offset may out of range when set it to 0xc0010299, define it as "unsigned long long" type and call pread64 directly in kernel.

---
 tools/power/x86/turbostat/turbostat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/power/x86/turbostat/turbostat.c +3 -3
index 33b370865d16..4c5cdfcb5721 100644
@@ -33,6 +33,7 @@
 #include <sys/capability.h>
 #include <errno.h>
 #include <math.h>
+#include <sys/syscall.h>
 
 char *proc_stat = "/proc/stat";
 FILE *outf;
@@ -381,11 +382,11 @@ int get_msr_fd(int cpu)
        return fd;
 }
 
-int get_msr(int cpu, off_t offset, unsigned long long *msr)
+int get_msr(int cpu, unsigned long long offset, unsigned long long *msr)
 {
        ssize_t retval;
 
-       retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
+       retval = syscall(SYS_pread64, get_msr_fd(cpu), msr, sizeof(*msr), offset);
 
        if (retval != sizeof *msr)
                err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
--

2.17.1

Signature · 1 line
Len Brown, Intel Open Source Technology Center
author
#2 lore raw

with multilib lib32 support, the rootfs will be 32-bit, the kernel is still 64-bit, in this case run turbostat will failed with "out of range" error.

Thanks,
Liwei.

Quoted 44 lines · Len Brown

Huh?

Quoted 39 lines · Liwei Song

with 32-bit rootfs, the offset may out of range when set it to 0xc0010299, define it as "unsigned long long" type and call pread64 directly in kernel.

---
 tools/power/x86/turbostat/turbostat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/power/x86/turbostat/turbostat.c +3 -3
index 33b370865d16..4c5cdfcb5721 100644
@@ -33,6 +33,7 @@
 #include <sys/capability.h>
 #include <errno.h>
 #include <math.h>
+#include <sys/syscall.h>
 
 char *proc_stat = "/proc/stat";
 FILE *outf;
@@ -381,11 +382,11 @@ int get_msr_fd(int cpu)
        return fd;
 }
 
-int get_msr(int cpu, off_t offset, unsigned long long *msr)
+int get_msr(int cpu, unsigned long long offset, unsigned long long *msr)
 {
        ssize_t retval;
 
-       retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
+       retval = syscall(SYS_pread64, get_msr_fd(cpu), msr, sizeof(*msr), offset);
 
        if (retval != sizeof *msr)
                err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
--

2.17.1

maintainer
#3 lore raw

Re: offset size

The offsets on this file are the MSR offsets. What MSR are you trying to access at offset 0xc0010299?

Re: pread vs pread64

If I take on faith that you have some kind of 32-bit execution environment that makes pread into pread32 instead of pread64, and that truncates an off_t to 32-bits from 64-bits, and it actually makes sense to request a read at this large offset...

would we really have to invoke syscall() directly -- couldn't we invoke pread64() directly? (eg. below)

thanks,
-Len

@@ -490,11 +491,12 @@ int get_msr_fd(int cpu)

        return fd;

}

-int get_msr(int cpu, off_t offset, unsigned long long *msr) +int get_msr(int cpu, unsigned long long offset, unsigned long long *msr)

 {
        ssize_t retval;

- retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
+ retval = pread64(get_msr_fd(cpu), msr, sizeof(*msr), offset);

        if (retval != sizeof *msr)
                err(-1, "cpu%d: msr offset 0x%llx read failed", cpu,

(unsigned long long)offset);

Quoted 53 lines · Liwei Song

with multilib lib32 support, the rootfs will be 32-bit, the kernel is still 64-bit, in this case run turbostat will failed with "out of range" error.

Thanks,
Liwei.

Quoted 44 lines · Len Brown

Huh?

Quoted 39 lines · Liwei Song

with 32-bit rootfs, the offset may out of range when set it to 0xc0010299, define it as "unsigned long long" type and call pread64 directly in kernel.

---
 tools/power/x86/turbostat/turbostat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/power/x86/turbostat/turbostat.c +3 -3
index 33b370865d16..4c5cdfcb5721 100644
@@ -33,6 +33,7 @@
 #include <sys/capability.h>
 #include <errno.h>
 #include <math.h>
+#include <sys/syscall.h>
 
 char *proc_stat = "/proc/stat";
 FILE *outf;
@@ -381,11 +382,11 @@ int get_msr_fd(int cpu)
        return fd;
 }
 
-int get_msr(int cpu, off_t offset, unsigned long long *msr)
+int get_msr(int cpu, unsigned long long offset, unsigned long long *msr)
 {
        ssize_t retval;
 
-       retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
+       retval = syscall(SYS_pread64, get_msr_fd(cpu), msr, sizeof(*msr), offset);
 
        if (retval != sizeof *msr)
                err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
--

2.17.1

Signature · 1 line
Len Brown, Intel Open Source Technology Center
#4 lore raw

Hi,

I am not the original submitter, but I have answers and a proper patch :)

Quoted 4 lines · Len Brown

Re: offset size

The offsets on this file are the MSR offsets. What MSR are you trying to access at offset 0xc0010299?

This MSR is particular is part of AMD RAPL (energy measurements) interface.

Quoted 6 lines

Re: pread vs pread64

If I take on faith that you have some kind of 32-bit execution environment that makes pread into pread32 instead of pread64, and that truncates an off_t to 32-bits from 64-bits, and it actually makes sense to request a read at this large offset...

The problem here stems from the backward compatibility in Glibc: off_t is 32-bit on 32-bit x86, unless compiled with -D_FILE_OFFSET_BITS=64. This macro should be used for all new code. Distros should enable it for all builds, but when one builds turbostat 'by hand', they hit the issue.

Quoted 2 lines

would we really have to invoke syscall() directly -- couldn't we invoke pread64() directly? (eg. below)

No, the proper fix is to pass -D_FILE_OFFSET_BITS=64 to the compiler.

Here's the patch:

---8<---

From: Alexander Monakov <[email protected]>
Date: Sun, 23 Aug 2020 23:27:02 +0300
Subject: [PATCH] turbostat: build with _FILE_OFFSET_BITS=64

For compatibility reasons, Glibc off_t is a 32-bit type on 32-bit x86 unless _FILE_OFFSET_BITS=64 is defined. Add this define, as otherwise reading MSRs with index 0x80000000 and above attempts a pread with a negative offset, which fails.

---
 tools/power/x86/turbostat/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/power/x86/turbostat/Makefile +1 -0
index 2b6551269e43..40ae44402eec 100644
@@ -12,6 +12,7 @@ turbostat : turbostat.c
 override CFLAGS +=	-O2 -Wall -I../../../include
 override CFLAGS +=	-DMSRHEADER='"../../../../arch/x86/include/asm/msr-index.h"'
 override CFLAGS +=	-DINTEL_FAMILY_HEADER='"../../../../arch/x86/include/asm/intel-family.h"'
+override CFLAGS +=	-D_FILE_OFFSET_BITS=64
 override CFLAGS +=	-D_FORTIFY_SOURCE=2
 
 %: %.c
Signature · 1 line
2.26.2
author
#5 lore raw
Quoted 25 lines · Alexander Monakov

Hi,

I am not the original submitter, but I have answers and a proper patch :)

Quoted 4 lines · Len Brown

Re: offset size

The offsets on this file are the MSR offsets. What MSR are you trying to access at offset 0xc0010299?

This MSR is particular is part of AMD RAPL (energy measurements) interface.

Quoted 6 lines

Re: pread vs pread64

If I take on faith that you have some kind of 32-bit execution environment that makes pread into pread32 instead of pread64, and that truncates an off_t to 32-bits from 64-bits, and it actually makes sense to request a read at this large offset...

The problem here stems from the backward compatibility in Glibc: off_t is 32-bit on 32-bit x86, unless compiled with -D_FILE_OFFSET_BITS=64. This macro should be used for all new code. Distros should enable it for all builds, but when one builds turbostat 'by hand', they hit the issue.

Quoted 6 lines
Quoted 2 lines

would we really have to invoke syscall() directly -- couldn't we invoke pread64() directly? (eg. below)

No, the proper fix is to pass -D_FILE_OFFSET_BITS=64 to the compiler.

Here's the patch:

This path works with my case.

Thanks,
Liwei.

Quoted 30 lines

---8<---

From: Alexander Monakov <[email protected]>
Date: Sun, 23 Aug 2020 23:27:02 +0300
Subject: [PATCH] turbostat: build with _FILE_OFFSET_BITS=64

For compatibility reasons, Glibc off_t is a 32-bit type on 32-bit x86 unless _FILE_OFFSET_BITS=64 is defined. Add this define, as otherwise reading MSRs with index 0x80000000 and above attempts a pread with a negative offset, which fails.

---
 tools/power/x86/turbostat/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/power/x86/turbostat/Makefile +1 -0
index 2b6551269e43..40ae44402eec 100644
@@ -12,6 +12,7 @@ turbostat : turbostat.c
 override CFLAGS +=	-O2 -Wall -I../../../include
 override CFLAGS +=	-DMSRHEADER='"../../../../arch/x86/include/asm/msr-index.h"'
 override CFLAGS +=	-DINTEL_FAMILY_HEADER='"../../../../arch/x86/include/asm/intel-family.h"'
+override CFLAGS +=	-D_FILE_OFFSET_BITS=64
 override CFLAGS +=	-D_FORTIFY_SOURCE=2
 
 %: %.c
maintainer Applied
#6 lore raw
Quoted 1 line

-D_FILE_OFFSET_BITS=64

Applied.

thanks!
-Len

Quoted 72 lines · Liwei Song
Quoted 25 lines · Alexander Monakov

Hi,

I am not the original submitter, but I have answers and a proper patch :)

Quoted 4 lines · Len Brown

Re: offset size

The offsets on this file are the MSR offsets. What MSR are you trying to access at offset 0xc0010299?

This MSR is particular is part of AMD RAPL (energy measurements) interface.

Quoted 6 lines

Re: pread vs pread64

If I take on faith that you have some kind of 32-bit execution environment that makes pread into pread32 instead of pread64, and that truncates an off_t to 32-bits from 64-bits, and it actually makes sense to request a read at this large offset...

The problem here stems from the backward compatibility in Glibc: off_t is 32-bit on 32-bit x86, unless compiled with -D_FILE_OFFSET_BITS=64. This macro should be used for all new code. Distros should enable it for all builds, but when one builds turbostat 'by hand', they hit the issue.

Quoted 6 lines
Quoted 2 lines

would we really have to invoke syscall() directly -- couldn't we invoke pread64() directly? (eg. below)

No, the proper fix is to pass -D_FILE_OFFSET_BITS=64 to the compiler.

Here's the patch:

This path works with my case.

Thanks,
Liwei.

Quoted 30 lines

---8<---

From: Alexander Monakov <[email protected]>
Date: Sun, 23 Aug 2020 23:27:02 +0300
Subject: [PATCH] turbostat: build with _FILE_OFFSET_BITS=64

For compatibility reasons, Glibc off_t is a 32-bit type on 32-bit x86 unless _FILE_OFFSET_BITS=64 is defined. Add this define, as otherwise reading MSRs with index 0x80000000 and above attempts a pread with a negative offset, which fails.

---
 tools/power/x86/turbostat/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/power/x86/turbostat/Makefile +1 -0
index 2b6551269e43..40ae44402eec 100644
@@ -12,6 +12,7 @@ turbostat : turbostat.c
 override CFLAGS +=   -O2 -Wall -I../../../include
 override CFLAGS +=   -DMSRHEADER='"../../../../arch/x86/include/asm/msr-index.h"'
 override CFLAGS +=   -DINTEL_FAMILY_HEADER='"../../../../arch/x86/include/asm/intel-family.h"'
+override CFLAGS +=   -D_FILE_OFFSET_BITS=64
 override CFLAGS +=   -D_FORTIFY_SOURCE=2
 
 %: %.c
Signature · 1 line
Len Brown, Intel Open Source Technology Center