Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

powerpc/perf: Define big-endian version of perf_mem_data_src

perf_mem_data_src is a union that is initialized in the kernel via the ->val
field and accessed by userspace via the mem_xxx bitfields. For this to work
correctly on big endian platforms, we need a big-endian definition for the
bitfields.

Currently on a big endian system, if a user requests PERF_SAMPLE_DATA_SRC (perf
report -d), they will get the default value from perf_sample_data_init(), which
is PERF_MEM_NA. The value for PERF_MEM_NA is constructed using shifts:

/* TLB access */
#define PERF_MEM_TLB_NA 0x01 /* not available */
...
#define PERF_MEM_TLB_SHIFT 26

#define PERF_MEM_S(a, s) \
(((__u64)PERF_MEM_##a##_##s) << PERF_MEM_##a##_SHIFT)

#define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
PERF_MEM_S(LVL, NA) |\
PERF_MEM_S(SNOOP, NA) |\
PERF_MEM_S(LOCK, NA) |\
PERF_MEM_S(TLB, NA))

Which works out as:

((0x01 << 0) | (0x01 << 5) | (0x01 << 19) | (0x01 << 24) | (0x01 << 26))

Which means the PERF_MEM_NA value comes out of the kernel as 0x5080021
in CPU endian.

But then in the perf tool, the code uses the bitfields to inspect the value, and
currently the bitfields are defined using little endian ordering.

So eg. in perf_mem__tlb_scnprintf() we see:
data_src->val = 0x5080021
op = 0x0
lvl = 0x0
snoop = 0x0
lock = 0x0
dtlb = 0x0
rsvd = 0x5080021

Because of the way the perf tool code is written this is still displayed to the
user as "N/A", so there is no bug visible at the UI level.

Currently there are no big endian architectures which export a meaningful
value (ie. other than PERF_MEM_NA), so the extent of the bug on big endian
platforms is that the PERF_MEM_NA value is exported incorrectly as described
above. Subsequent patches will add support on big endian powerpc for populating
the data source value.

This patch does a minimal fix of adding big endian definition of the bitfields
to match the values that are already exported by the kernel on big endian. And
it makes no change on little endian.

Signed-off-by: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Signed-off-by: Madhavan Srinivasan <maddy@linux.vnet.ibm.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>

authored by

Sukadev Bhattiprolu and committed by
Michael Ellerman
8c5073db e889e96e

+32
+16
include/uapi/linux/perf_event.h
··· 891 891 #define PERF_FLAG_PID_CGROUP (1UL << 2) /* pid=cgroup id, per-cpu mode only */ 892 892 #define PERF_FLAG_FD_CLOEXEC (1UL << 3) /* O_CLOEXEC */ 893 893 894 + #if defined(__LITTLE_ENDIAN_BITFIELD) 894 895 union perf_mem_data_src { 895 896 __u64 val; 896 897 struct { ··· 903 902 mem_rsvd:31; 904 903 }; 905 904 }; 905 + #elif defined(__BIG_ENDIAN_BITFIELD) 906 + union perf_mem_data_src { 907 + __u64 val; 908 + struct { 909 + __u64 mem_rsvd:31, 910 + mem_dtlb:7, /* tlb access */ 911 + mem_lock:2, /* lock instr */ 912 + mem_snoop:5, /* snoop mode */ 913 + mem_lvl:14, /* memory hierarchy level */ 914 + mem_op:5; /* type of opcode */ 915 + }; 916 + }; 917 + #else 918 + #error "Unknown endianness" 919 + #endif 906 920 907 921 /* type of opcode (load/store/prefetch,code) */ 908 922 #define PERF_MEM_OP_NA 0x01 /* not available */
+16
tools/include/uapi/linux/perf_event.h
··· 891 891 #define PERF_FLAG_PID_CGROUP (1UL << 2) /* pid=cgroup id, per-cpu mode only */ 892 892 #define PERF_FLAG_FD_CLOEXEC (1UL << 3) /* O_CLOEXEC */ 893 893 894 + #if defined(__LITTLE_ENDIAN_BITFIELD) 894 895 union perf_mem_data_src { 895 896 __u64 val; 896 897 struct { ··· 903 902 mem_rsvd:31; 904 903 }; 905 904 }; 905 + #elif defined(__BIG_ENDIAN_BITFIELD) 906 + union perf_mem_data_src { 907 + __u64 val; 908 + struct { 909 + __u64 mem_rsvd:31, 910 + mem_dtlb:7, /* tlb access */ 911 + mem_lock:2, /* lock instr */ 912 + mem_snoop:5, /* snoop mode */ 913 + mem_lvl:14, /* memory hierarchy level */ 914 + mem_op:5; /* type of opcode */ 915 + }; 916 + }; 917 + #else 918 + #error "Unknown endianness" 919 + #endif 906 920 907 921 /* type of opcode (load/store/prefetch,code) */ 908 922 #define PERF_MEM_OP_NA 0x01 /* not available */