mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	perf/x86: Add /sys/devices/cpu/rdpmc=2 to allow rdpmc for all tasks
While perfmon2 is a sufficiently evil library (it pokes MSRs directly) that breaking it is fair game, it's still useful, so we might as well try to support it. This allows users to write 2 to /sys/devices/cpu/rdpmc to disable all rdpmc protection so that hack like perfmon2 can continue to work. At some point, if perf_event becomes fast enough to replace perfmon2, then this can go. Signed-off-by: Andy Lutomirski <luto@amacapital.net> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Arnaldo Carvalho de Melo <acme@kernel.org> Cc: Kees Cook <keescook@chromium.org> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Vince Weaver <vince@deater.net> Cc: "hillf.zj" <hillf.zj@alibaba-inc.com> Cc: Valdis Kletnieks <Valdis.Kletnieks@vt.edu> Cc: Linus Torvalds <torvalds@linux-foundation.org> Link: http://lkml.kernel.org/r/caac3c1c707dcca48ecbc35f4def21495856f479.1414190806.git.luto@amacapital.net Signed-off-by: Ingo Molnar <mingo@kernel.org>
This commit is contained in:
		
							parent
							
								
									7911d3f7af
								
							
						
					
					
						commit
						a66734297f
					
				
					 2 changed files with 24 additions and 2 deletions
				
			
		| 
						 | 
					@ -19,9 +19,12 @@ static inline void paravirt_activate_mm(struct mm_struct *prev,
 | 
				
			||||||
#endif	/* !CONFIG_PARAVIRT */
 | 
					#endif	/* !CONFIG_PARAVIRT */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef CONFIG_PERF_EVENTS
 | 
					#ifdef CONFIG_PERF_EVENTS
 | 
				
			||||||
 | 
					extern struct static_key rdpmc_always_available;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static inline void load_mm_cr4(struct mm_struct *mm)
 | 
					static inline void load_mm_cr4(struct mm_struct *mm)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (atomic_read(&mm->context.perf_rdpmc_allowed))
 | 
						if (static_key_true(&rdpmc_always_available) ||
 | 
				
			||||||
 | 
						    atomic_read(&mm->context.perf_rdpmc_allowed))
 | 
				
			||||||
		cr4_set_bits(X86_CR4_PCE);
 | 
							cr4_set_bits(X86_CR4_PCE);
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
		cr4_clear_bits(X86_CR4_PCE);
 | 
							cr4_clear_bits(X86_CR4_PCE);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -45,6 +45,8 @@ DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
 | 
				
			||||||
	.enabled = 1,
 | 
						.enabled = 1,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct static_key rdpmc_always_available = STATIC_KEY_INIT_FALSE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
u64 __read_mostly hw_cache_event_ids
 | 
					u64 __read_mostly hw_cache_event_ids
 | 
				
			||||||
				[PERF_COUNT_HW_CACHE_MAX]
 | 
									[PERF_COUNT_HW_CACHE_MAX]
 | 
				
			||||||
				[PERF_COUNT_HW_CACHE_OP_MAX]
 | 
									[PERF_COUNT_HW_CACHE_OP_MAX]
 | 
				
			||||||
| 
						 | 
					@ -1870,10 +1872,27 @@ static ssize_t set_attr_rdpmc(struct device *cdev,
 | 
				
			||||||
	if (ret)
 | 
						if (ret)
 | 
				
			||||||
		return ret;
 | 
							return ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (val > 2)
 | 
				
			||||||
 | 
							return -EINVAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (x86_pmu.attr_rdpmc_broken)
 | 
						if (x86_pmu.attr_rdpmc_broken)
 | 
				
			||||||
		return -ENOTSUPP;
 | 
							return -ENOTSUPP;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	x86_pmu.attr_rdpmc = !!val;
 | 
						if ((val == 2) != (x86_pmu.attr_rdpmc == 2)) {
 | 
				
			||||||
 | 
							/*
 | 
				
			||||||
 | 
							 * Changing into or out of always available, aka
 | 
				
			||||||
 | 
							 * perf-event-bypassing mode.  This path is extremely slow,
 | 
				
			||||||
 | 
							 * but only root can trigger it, so it's okay.
 | 
				
			||||||
 | 
							 */
 | 
				
			||||||
 | 
							if (val == 2)
 | 
				
			||||||
 | 
								static_key_slow_inc(&rdpmc_always_available);
 | 
				
			||||||
 | 
							else
 | 
				
			||||||
 | 
								static_key_slow_dec(&rdpmc_always_available);
 | 
				
			||||||
 | 
							on_each_cpu(refresh_pce, NULL, 1);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						x86_pmu.attr_rdpmc = val;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return count;
 | 
						return count;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue