| 
									
										
										
										
											2013-06-28 16:22:19 +03:00
										 |  |  | #include <stdbool.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <linux/perf_event.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "../../perf.h"
 | 
					
						
							|  |  |  | #include "../../util/types.h"
 | 
					
						
							|  |  |  | #include "../../util/debug.h"
 | 
					
						
							|  |  |  | #include "tsc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-26 08:27:23 -06:00
										 |  |  | 	u64 t, quot, rem; | 
					
						
							| 
									
										
										
										
											2013-06-28 16:22:19 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-26 08:27:23 -06:00
										 |  |  | 	t = ns - tc->time_zero; | 
					
						
							|  |  |  | 	quot = t / tc->time_mult; | 
					
						
							|  |  |  | 	rem  = t % tc->time_mult; | 
					
						
							| 
									
										
										
										
											2013-06-28 16:22:19 +03:00
										 |  |  | 	return (quot << tc->time_shift) + | 
					
						
							|  |  |  | 	       (rem << tc->time_shift) / tc->time_mult; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	u64 quot, rem; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	quot = cyc >> tc->time_shift; | 
					
						
							|  |  |  | 	rem  = cyc & ((1 << tc->time_shift) - 1); | 
					
						
							|  |  |  | 	return tc->time_zero + quot * tc->time_mult + | 
					
						
							|  |  |  | 	       ((rem * tc->time_mult) >> tc->time_shift); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc, | 
					
						
							|  |  |  | 			     struct perf_tsc_conversion *tc) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
											  
											
												perf: Fix capabilities bitfield compatibility in 'struct perf_event_mmap_page'
Solve the problems around the broken definition of perf_event_mmap_page::
cap_usr_time and cap_usr_rdpmc fields which used to overlap, partially
fixed by:
  860f085b74e9 ("perf: Fix broken union in 'struct perf_event_mmap_page'")
The problem with the fix (merged in v3.12-rc1 and not yet released
officially), noticed by Vince Weaver is that the new behavior is
not detectable by new user-space, and that due to the reuse of the
field names it's easy to mis-compile a binary if old headers are used
on a new kernel or new headers are used on an old kernel.
To solve all that make this change explicit, detectable and self-contained,
by iterating the ABI the following way:
 - Always clear bit 0, and rename it to usrpage->cap_bit0, to at least not
   confuse old user-space binaries. RDPMC will be marked as unavailable
   to old binaries but that's within the ABI, this is a capability bit.
 - Rename bit 1 to ->cap_bit0_is_deprecated and always set it to 1, so new
   libraries can reliably detect that bit 0 is deprecated and perma-zero
   without having to check the kernel version.
 - Use bits 2, 3, 4 for the newly defined, correct functionality:
	cap_user_rdpmc		: 1, /* The RDPMC instruction can be used to read counts */
	cap_user_time		: 1, /* The time_* fields are used */
	cap_user_time_zero	: 1, /* The time_zero field is used */
 - Rename all the bitfield names in perf_event.h to be different from the
   old names, to make sure it's not possible to mis-compile it
   accidentally with old assumptions.
The 'size' field can then be used in the future to add new fields and it
will act as a natural ABI version indicator as well.
Also adjust tools/perf/ userspace for the new definitions, noticed by
Adrian Hunter.
Reported-by: Vince Weaver <vincent.weaver@maine.edu>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Also-Fixed-by: Adrian Hunter <adrian.hunter@intel.com>
Link: http://lkml.kernel.org/n/tip-zr03yxjrpXesOzzupszqglbv@git.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
											
										 
											2013-09-19 10:16:42 +02:00
										 |  |  | 	bool cap_user_time_zero; | 
					
						
							| 
									
										
										
										
											2013-06-28 16:22:19 +03:00
										 |  |  | 	u32 seq; | 
					
						
							|  |  |  | 	int i = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (1) { | 
					
						
							|  |  |  | 		seq = pc->lock; | 
					
						
							|  |  |  | 		rmb(); | 
					
						
							|  |  |  | 		tc->time_mult = pc->time_mult; | 
					
						
							|  |  |  | 		tc->time_shift = pc->time_shift; | 
					
						
							|  |  |  | 		tc->time_zero = pc->time_zero; | 
					
						
							| 
									
										
											  
											
												perf: Fix capabilities bitfield compatibility in 'struct perf_event_mmap_page'
Solve the problems around the broken definition of perf_event_mmap_page::
cap_usr_time and cap_usr_rdpmc fields which used to overlap, partially
fixed by:
  860f085b74e9 ("perf: Fix broken union in 'struct perf_event_mmap_page'")
The problem with the fix (merged in v3.12-rc1 and not yet released
officially), noticed by Vince Weaver is that the new behavior is
not detectable by new user-space, and that due to the reuse of the
field names it's easy to mis-compile a binary if old headers are used
on a new kernel or new headers are used on an old kernel.
To solve all that make this change explicit, detectable and self-contained,
by iterating the ABI the following way:
 - Always clear bit 0, and rename it to usrpage->cap_bit0, to at least not
   confuse old user-space binaries. RDPMC will be marked as unavailable
   to old binaries but that's within the ABI, this is a capability bit.
 - Rename bit 1 to ->cap_bit0_is_deprecated and always set it to 1, so new
   libraries can reliably detect that bit 0 is deprecated and perma-zero
   without having to check the kernel version.
 - Use bits 2, 3, 4 for the newly defined, correct functionality:
	cap_user_rdpmc		: 1, /* The RDPMC instruction can be used to read counts */
	cap_user_time		: 1, /* The time_* fields are used */
	cap_user_time_zero	: 1, /* The time_zero field is used */
 - Rename all the bitfield names in perf_event.h to be different from the
   old names, to make sure it's not possible to mis-compile it
   accidentally with old assumptions.
The 'size' field can then be used in the future to add new fields and it
will act as a natural ABI version indicator as well.
Also adjust tools/perf/ userspace for the new definitions, noticed by
Adrian Hunter.
Reported-by: Vince Weaver <vincent.weaver@maine.edu>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Also-Fixed-by: Adrian Hunter <adrian.hunter@intel.com>
Link: http://lkml.kernel.org/n/tip-zr03yxjrpXesOzzupszqglbv@git.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
											
										 
											2013-09-19 10:16:42 +02:00
										 |  |  | 		cap_user_time_zero = pc->cap_user_time_zero; | 
					
						
							| 
									
										
										
										
											2013-06-28 16:22:19 +03:00
										 |  |  | 		rmb(); | 
					
						
							|  |  |  | 		if (pc->lock == seq && !(seq & 1)) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		if (++i > 10000) { | 
					
						
							|  |  |  | 			pr_debug("failed to get perf_event_mmap_page lock\n"); | 
					
						
							|  |  |  | 			return -EINVAL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												perf: Fix capabilities bitfield compatibility in 'struct perf_event_mmap_page'
Solve the problems around the broken definition of perf_event_mmap_page::
cap_usr_time and cap_usr_rdpmc fields which used to overlap, partially
fixed by:
  860f085b74e9 ("perf: Fix broken union in 'struct perf_event_mmap_page'")
The problem with the fix (merged in v3.12-rc1 and not yet released
officially), noticed by Vince Weaver is that the new behavior is
not detectable by new user-space, and that due to the reuse of the
field names it's easy to mis-compile a binary if old headers are used
on a new kernel or new headers are used on an old kernel.
To solve all that make this change explicit, detectable and self-contained,
by iterating the ABI the following way:
 - Always clear bit 0, and rename it to usrpage->cap_bit0, to at least not
   confuse old user-space binaries. RDPMC will be marked as unavailable
   to old binaries but that's within the ABI, this is a capability bit.
 - Rename bit 1 to ->cap_bit0_is_deprecated and always set it to 1, so new
   libraries can reliably detect that bit 0 is deprecated and perma-zero
   without having to check the kernel version.
 - Use bits 2, 3, 4 for the newly defined, correct functionality:
	cap_user_rdpmc		: 1, /* The RDPMC instruction can be used to read counts */
	cap_user_time		: 1, /* The time_* fields are used */
	cap_user_time_zero	: 1, /* The time_zero field is used */
 - Rename all the bitfield names in perf_event.h to be different from the
   old names, to make sure it's not possible to mis-compile it
   accidentally with old assumptions.
The 'size' field can then be used in the future to add new fields and it
will act as a natural ABI version indicator as well.
Also adjust tools/perf/ userspace for the new definitions, noticed by
Adrian Hunter.
Reported-by: Vince Weaver <vincent.weaver@maine.edu>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Also-Fixed-by: Adrian Hunter <adrian.hunter@intel.com>
Link: http://lkml.kernel.org/n/tip-zr03yxjrpXesOzzupszqglbv@git.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
											
										 
											2013-09-19 10:16:42 +02:00
										 |  |  | 	if (!cap_user_time_zero) | 
					
						
							| 
									
										
										
										
											2013-06-28 16:22:19 +03:00
										 |  |  | 		return -EOPNOTSUPP; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } |