xref: /openbmc/linux/kernel/profile.c (revision 583a22e7c154dc0a3938db522696b4bc7f098f59)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/kernel/profile.c
31da177e4SLinus Torvalds  *  Simple profiling. Manages a direct-mapped profile hit count buffer,
41da177e4SLinus Torvalds  *  with configurable resolution, support for restricting the cpus on
51da177e4SLinus Torvalds  *  which profiling is done, and switching between cpu time and
61da177e4SLinus Torvalds  *  schedule() calls via kernel command line parameters passed at boot.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  Scheduler profiling support, Arjan van de Ven and Ingo Molnar,
91da177e4SLinus Torvalds  *	Red Hat, July 2004
101da177e4SLinus Torvalds  *  Consolidation of architecture support code for profiling,
111da177e4SLinus Torvalds  *	William Irwin, Oracle, July 2004
121da177e4SLinus Torvalds  *  Amortized hit count accounting via per-cpu open-addressed hashtables
131da177e4SLinus Torvalds  *	to resolve timer interrupt livelocks, William Irwin, Oracle, 2004
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/profile.h>
181da177e4SLinus Torvalds #include <linux/bootmem.h>
191da177e4SLinus Torvalds #include <linux/notifier.h>
201da177e4SLinus Torvalds #include <linux/mm.h>
211da177e4SLinus Torvalds #include <linux/cpumask.h>
221da177e4SLinus Torvalds #include <linux/cpu.h>
231da177e4SLinus Torvalds #include <linux/highmem.h>
2497d1f15bSArjan van de Ven #include <linux/mutex.h>
2522b8ce94SDave Hansen #include <linux/slab.h>
2622b8ce94SDave Hansen #include <linux/vmalloc.h>
271da177e4SLinus Torvalds #include <asm/sections.h>
287d12e780SDavid Howells #include <asm/irq_regs.h>
29e8edc6e0SAlexey Dobriyan #include <asm/ptrace.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds struct profile_hit {
321da177e4SLinus Torvalds 	u32 pc, hits;
331da177e4SLinus Torvalds };
341da177e4SLinus Torvalds #define PROFILE_GRPSHIFT	3
351da177e4SLinus Torvalds #define PROFILE_GRPSZ		(1 << PROFILE_GRPSHIFT)
361da177e4SLinus Torvalds #define NR_PROFILE_HIT		(PAGE_SIZE/sizeof(struct profile_hit))
371da177e4SLinus Torvalds #define NR_PROFILE_GRP		(NR_PROFILE_HIT/PROFILE_GRPSZ)
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds /* Oprofile timer tick hook */
40b012d346SAdrian Bunk static int (*timer_hook)(struct pt_regs *) __read_mostly;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds static atomic_t *prof_buffer;
431da177e4SLinus Torvalds static unsigned long prof_len, prof_shift;
4407031e14SIngo Molnar 
45ece8a684SIngo Molnar int prof_on __read_mostly;
4607031e14SIngo Molnar EXPORT_SYMBOL_GPL(prof_on);
4707031e14SIngo Molnar 
48c309b917SRusty Russell static cpumask_var_t prof_cpu_mask;
491da177e4SLinus Torvalds #ifdef CONFIG_SMP
501da177e4SLinus Torvalds static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
511da177e4SLinus Torvalds static DEFINE_PER_CPU(int, cpu_profile_flip);
5297d1f15bSArjan van de Ven static DEFINE_MUTEX(profile_flip_mutex);
531da177e4SLinus Torvalds #endif /* CONFIG_SMP */
541da177e4SLinus Torvalds 
5522b8ce94SDave Hansen int profile_setup(char *str)
561da177e4SLinus Torvalds {
5722b8ce94SDave Hansen 	static char schedstr[] = "schedule";
5822b8ce94SDave Hansen 	static char sleepstr[] = "sleep";
5922b8ce94SDave Hansen 	static char kvmstr[] = "kvm";
601da177e4SLinus Torvalds 	int par;
611da177e4SLinus Torvalds 
62ece8a684SIngo Molnar 	if (!strncmp(str, sleepstr, strlen(sleepstr))) {
63b3da2a73SMel Gorman #ifdef CONFIG_SCHEDSTATS
64ece8a684SIngo Molnar 		prof_on = SLEEP_PROFILING;
65ece8a684SIngo Molnar 		if (str[strlen(sleepstr)] == ',')
66ece8a684SIngo Molnar 			str += strlen(sleepstr) + 1;
67ece8a684SIngo Molnar 		if (get_option(&str, &par))
68ece8a684SIngo Molnar 			prof_shift = par;
69ece8a684SIngo Molnar 		printk(KERN_INFO
70ece8a684SIngo Molnar 			"kernel sleep profiling enabled (shift: %ld)\n",
71ece8a684SIngo Molnar 			prof_shift);
72b3da2a73SMel Gorman #else
73b3da2a73SMel Gorman 		printk(KERN_WARNING
74b3da2a73SMel Gorman 			"kernel sleep profiling requires CONFIG_SCHEDSTATS\n");
75b3da2a73SMel Gorman #endif /* CONFIG_SCHEDSTATS */
76a75acf85SIngo Molnar 	} else if (!strncmp(str, schedstr, strlen(schedstr))) {
771da177e4SLinus Torvalds 		prof_on = SCHED_PROFILING;
78dfaa9c94SWilliam Lee Irwin III 		if (str[strlen(schedstr)] == ',')
79dfaa9c94SWilliam Lee Irwin III 			str += strlen(schedstr) + 1;
80dfaa9c94SWilliam Lee Irwin III 		if (get_option(&str, &par))
81dfaa9c94SWilliam Lee Irwin III 			prof_shift = par;
82dfaa9c94SWilliam Lee Irwin III 		printk(KERN_INFO
83dfaa9c94SWilliam Lee Irwin III 			"kernel schedule profiling enabled (shift: %ld)\n",
84dfaa9c94SWilliam Lee Irwin III 			prof_shift);
8507031e14SIngo Molnar 	} else if (!strncmp(str, kvmstr, strlen(kvmstr))) {
8607031e14SIngo Molnar 		prof_on = KVM_PROFILING;
8707031e14SIngo Molnar 		if (str[strlen(kvmstr)] == ',')
8807031e14SIngo Molnar 			str += strlen(kvmstr) + 1;
8907031e14SIngo Molnar 		if (get_option(&str, &par))
9007031e14SIngo Molnar 			prof_shift = par;
9107031e14SIngo Molnar 		printk(KERN_INFO
9207031e14SIngo Molnar 			"kernel KVM profiling enabled (shift: %ld)\n",
9307031e14SIngo Molnar 			prof_shift);
94dfaa9c94SWilliam Lee Irwin III 	} else if (get_option(&str, &par)) {
951da177e4SLinus Torvalds 		prof_shift = par;
961da177e4SLinus Torvalds 		prof_on = CPU_PROFILING;
971da177e4SLinus Torvalds 		printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
981da177e4SLinus Torvalds 			prof_shift);
991da177e4SLinus Torvalds 	}
1001da177e4SLinus Torvalds 	return 1;
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds __setup("profile=", profile_setup);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 
105ce05fcc3SPaul Mundt int __ref profile_init(void)
1061da177e4SLinus Torvalds {
10722b8ce94SDave Hansen 	int buffer_bytes;
1081da177e4SLinus Torvalds 	if (!prof_on)
10922b8ce94SDave Hansen 		return 0;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	/* only text is profiled */
1121da177e4SLinus Torvalds 	prof_len = (_etext - _stext) >> prof_shift;
11322b8ce94SDave Hansen 	buffer_bytes = prof_len*sizeof(atomic_t);
11422b8ce94SDave Hansen 
115c309b917SRusty Russell 	if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL))
116c309b917SRusty Russell 		return -ENOMEM;
117c309b917SRusty Russell 
118acd89579SHugh Dickins 	cpumask_copy(prof_cpu_mask, cpu_possible_mask);
119acd89579SHugh Dickins 
120b62f495dSMel Gorman 	prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN);
12122b8ce94SDave Hansen 	if (prof_buffer)
12222b8ce94SDave Hansen 		return 0;
12322b8ce94SDave Hansen 
124b62f495dSMel Gorman 	prof_buffer = alloc_pages_exact(buffer_bytes,
125b62f495dSMel Gorman 					GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN);
12622b8ce94SDave Hansen 	if (prof_buffer)
12722b8ce94SDave Hansen 		return 0;
12822b8ce94SDave Hansen 
12922b8ce94SDave Hansen 	prof_buffer = vmalloc(buffer_bytes);
13022b8ce94SDave Hansen 	if (prof_buffer)
13122b8ce94SDave Hansen 		return 0;
13222b8ce94SDave Hansen 
133c309b917SRusty Russell 	free_cpumask_var(prof_cpu_mask);
13422b8ce94SDave Hansen 	return -ENOMEM;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /* Profile event notifications */
1381da177e4SLinus Torvalds 
139e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
140e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
141e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds void profile_task_exit(struct task_struct *task)
1441da177e4SLinus Torvalds {
145e041c683SAlan Stern 	blocking_notifier_call_chain(&task_exit_notifier, 0, task);
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds int profile_handoff_task(struct task_struct *task)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	int ret;
151e041c683SAlan Stern 	ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
1521da177e4SLinus Torvalds 	return (ret == NOTIFY_OK) ? 1 : 0;
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds void profile_munmap(unsigned long addr)
1561da177e4SLinus Torvalds {
157e041c683SAlan Stern 	blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds int task_handoff_register(struct notifier_block *n)
1611da177e4SLinus Torvalds {
162e041c683SAlan Stern 	return atomic_notifier_chain_register(&task_free_notifier, n);
1631da177e4SLinus Torvalds }
1641ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(task_handoff_register);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds int task_handoff_unregister(struct notifier_block *n)
1671da177e4SLinus Torvalds {
168e041c683SAlan Stern 	return atomic_notifier_chain_unregister(&task_free_notifier, n);
1691da177e4SLinus Torvalds }
1701ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(task_handoff_unregister);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds int profile_event_register(enum profile_type type, struct notifier_block *n)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	int err = -EINVAL;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	switch (type) {
1771da177e4SLinus Torvalds 	case PROFILE_TASK_EXIT:
178e041c683SAlan Stern 		err = blocking_notifier_chain_register(
179e041c683SAlan Stern 				&task_exit_notifier, n);
1801da177e4SLinus Torvalds 		break;
1811da177e4SLinus Torvalds 	case PROFILE_MUNMAP:
182e041c683SAlan Stern 		err = blocking_notifier_chain_register(
183e041c683SAlan Stern 				&munmap_notifier, n);
1841da177e4SLinus Torvalds 		break;
1851da177e4SLinus Torvalds 	}
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	return err;
1881da177e4SLinus Torvalds }
1891ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(profile_event_register);
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds int profile_event_unregister(enum profile_type type, struct notifier_block *n)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	int err = -EINVAL;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	switch (type) {
1961da177e4SLinus Torvalds 	case PROFILE_TASK_EXIT:
197e041c683SAlan Stern 		err = blocking_notifier_chain_unregister(
198e041c683SAlan Stern 				&task_exit_notifier, n);
1991da177e4SLinus Torvalds 		break;
2001da177e4SLinus Torvalds 	case PROFILE_MUNMAP:
201e041c683SAlan Stern 		err = blocking_notifier_chain_unregister(
202e041c683SAlan Stern 				&munmap_notifier, n);
2031da177e4SLinus Torvalds 		break;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	return err;
2071da177e4SLinus Torvalds }
2081ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(profile_event_unregister);
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds int register_timer_hook(int (*hook)(struct pt_regs *))
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	if (timer_hook)
2131da177e4SLinus Torvalds 		return -EBUSY;
2141da177e4SLinus Torvalds 	timer_hook = hook;
2151da177e4SLinus Torvalds 	return 0;
2161da177e4SLinus Torvalds }
2171ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(register_timer_hook);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds void unregister_timer_hook(int (*hook)(struct pt_regs *))
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	WARN_ON(hook != timer_hook);
2221da177e4SLinus Torvalds 	timer_hook = NULL;
2231da177e4SLinus Torvalds 	/* make sure all CPUs see the NULL hook */
224fbd568a3SPaul E. McKenney 	synchronize_sched();  /* Allow ongoing interrupts to complete. */
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(unregister_timer_hook);
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds #ifdef CONFIG_SMP
2301da177e4SLinus Torvalds /*
2311da177e4SLinus Torvalds  * Each cpu has a pair of open-addressed hashtables for pending
2321da177e4SLinus Torvalds  * profile hits. read_profile() IPI's all cpus to request them
2331da177e4SLinus Torvalds  * to flip buffers and flushes their contents to prof_buffer itself.
2341da177e4SLinus Torvalds  * Flip requests are serialized by the profile_flip_mutex. The sole
2351da177e4SLinus Torvalds  * use of having a second hashtable is for avoiding cacheline
2361da177e4SLinus Torvalds  * contention that would otherwise happen during flushes of pending
2371da177e4SLinus Torvalds  * profile hits required for the accuracy of reported profile hits
2381da177e4SLinus Torvalds  * and so resurrect the interrupt livelock issue.
2391da177e4SLinus Torvalds  *
2401da177e4SLinus Torvalds  * The open-addressed hashtables are indexed by profile buffer slot
2411da177e4SLinus Torvalds  * and hold the number of pending hits to that profile buffer slot on
2421da177e4SLinus Torvalds  * a cpu in an entry. When the hashtable overflows, all pending hits
2431da177e4SLinus Torvalds  * are accounted to their corresponding profile buffer slots with
2441da177e4SLinus Torvalds  * atomic_add() and the hashtable emptied. As numerous pending hits
2451da177e4SLinus Torvalds  * may be accounted to a profile buffer slot in a hashtable entry,
2461da177e4SLinus Torvalds  * this amortizes a number of atomic profile buffer increments likely
2471da177e4SLinus Torvalds  * to be far larger than the number of entries in the hashtable,
2481da177e4SLinus Torvalds  * particularly given that the number of distinct profile buffer
2491da177e4SLinus Torvalds  * positions to which hits are accounted during short intervals (e.g.
2501da177e4SLinus Torvalds  * several seconds) is usually very small. Exclusion from buffer
2511da177e4SLinus Torvalds  * flipping is provided by interrupt disablement (note that for
252ece8a684SIngo Molnar  * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
253ece8a684SIngo Molnar  * process context).
2541da177e4SLinus Torvalds  * The hash function is meant to be lightweight as opposed to strong,
2551da177e4SLinus Torvalds  * and was vaguely inspired by ppc64 firmware-supported inverted
2561da177e4SLinus Torvalds  * pagetable hash functions, but uses a full hashtable full of finite
2571da177e4SLinus Torvalds  * collision chains, not just pairs of them.
2581da177e4SLinus Torvalds  *
2591da177e4SLinus Torvalds  * -- wli
2601da177e4SLinus Torvalds  */
2611da177e4SLinus Torvalds static void __profile_flip_buffers(void *unused)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds 	int cpu = smp_processor_id();
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static void profile_flip_buffers(void)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	int i, j, cpu;
2711da177e4SLinus Torvalds 
27297d1f15bSArjan van de Ven 	mutex_lock(&profile_flip_mutex);
2731da177e4SLinus Torvalds 	j = per_cpu(cpu_profile_flip, get_cpu());
2741da177e4SLinus Torvalds 	put_cpu();
27515c8b6c1SJens Axboe 	on_each_cpu(__profile_flip_buffers, NULL, 1);
2761da177e4SLinus Torvalds 	for_each_online_cpu(cpu) {
2771da177e4SLinus Torvalds 		struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
2781da177e4SLinus Torvalds 		for (i = 0; i < NR_PROFILE_HIT; ++i) {
2791da177e4SLinus Torvalds 			if (!hits[i].hits) {
2801da177e4SLinus Torvalds 				if (hits[i].pc)
2811da177e4SLinus Torvalds 					hits[i].pc = 0;
2821da177e4SLinus Torvalds 				continue;
2831da177e4SLinus Torvalds 			}
2841da177e4SLinus Torvalds 			atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
2851da177e4SLinus Torvalds 			hits[i].hits = hits[i].pc = 0;
2861da177e4SLinus Torvalds 		}
2871da177e4SLinus Torvalds 	}
28897d1f15bSArjan van de Ven 	mutex_unlock(&profile_flip_mutex);
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static void profile_discard_flip_buffers(void)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	int i, cpu;
2941da177e4SLinus Torvalds 
29597d1f15bSArjan van de Ven 	mutex_lock(&profile_flip_mutex);
2961da177e4SLinus Torvalds 	i = per_cpu(cpu_profile_flip, get_cpu());
2971da177e4SLinus Torvalds 	put_cpu();
29815c8b6c1SJens Axboe 	on_each_cpu(__profile_flip_buffers, NULL, 1);
2991da177e4SLinus Torvalds 	for_each_online_cpu(cpu) {
3001da177e4SLinus Torvalds 		struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
3011da177e4SLinus Torvalds 		memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
3021da177e4SLinus Torvalds 	}
30397d1f15bSArjan van de Ven 	mutex_unlock(&profile_flip_mutex);
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
306ece8a684SIngo Molnar void profile_hits(int type, void *__pc, unsigned int nr_hits)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
3091da177e4SLinus Torvalds 	int i, j, cpu;
3101da177e4SLinus Torvalds 	struct profile_hit *hits;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	if (prof_on != type || !prof_buffer)
3131da177e4SLinus Torvalds 		return;
3141da177e4SLinus Torvalds 	pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
3151da177e4SLinus Torvalds 	i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
3161da177e4SLinus Torvalds 	secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
3171da177e4SLinus Torvalds 	cpu = get_cpu();
3181da177e4SLinus Torvalds 	hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
3191da177e4SLinus Torvalds 	if (!hits) {
3201da177e4SLinus Torvalds 		put_cpu();
3211da177e4SLinus Torvalds 		return;
3221da177e4SLinus Torvalds 	}
323ece8a684SIngo Molnar 	/*
324ece8a684SIngo Molnar 	 * We buffer the global profiler buffer into a per-CPU
325ece8a684SIngo Molnar 	 * queue and thus reduce the number of global (and possibly
326ece8a684SIngo Molnar 	 * NUMA-alien) accesses. The write-queue is self-coalescing:
327ece8a684SIngo Molnar 	 */
3281da177e4SLinus Torvalds 	local_irq_save(flags);
3291da177e4SLinus Torvalds 	do {
3301da177e4SLinus Torvalds 		for (j = 0; j < PROFILE_GRPSZ; ++j) {
3311da177e4SLinus Torvalds 			if (hits[i + j].pc == pc) {
332ece8a684SIngo Molnar 				hits[i + j].hits += nr_hits;
3331da177e4SLinus Torvalds 				goto out;
3341da177e4SLinus Torvalds 			} else if (!hits[i + j].hits) {
3351da177e4SLinus Torvalds 				hits[i + j].pc = pc;
336ece8a684SIngo Molnar 				hits[i + j].hits = nr_hits;
3371da177e4SLinus Torvalds 				goto out;
3381da177e4SLinus Torvalds 			}
3391da177e4SLinus Torvalds 		}
3401da177e4SLinus Torvalds 		i = (i + secondary) & (NR_PROFILE_HIT - 1);
3411da177e4SLinus Torvalds 	} while (i != primary);
342ece8a684SIngo Molnar 
343ece8a684SIngo Molnar 	/*
344ece8a684SIngo Molnar 	 * Add the current hit(s) and flush the write-queue out
345ece8a684SIngo Molnar 	 * to the global buffer:
346ece8a684SIngo Molnar 	 */
347ece8a684SIngo Molnar 	atomic_add(nr_hits, &prof_buffer[pc]);
3481da177e4SLinus Torvalds 	for (i = 0; i < NR_PROFILE_HIT; ++i) {
3491da177e4SLinus Torvalds 		atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
3501da177e4SLinus Torvalds 		hits[i].pc = hits[i].hits = 0;
3511da177e4SLinus Torvalds 	}
3521da177e4SLinus Torvalds out:
3531da177e4SLinus Torvalds 	local_irq_restore(flags);
3541da177e4SLinus Torvalds 	put_cpu();
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
35784196414SAl Viro static int __cpuinit profile_cpu_callback(struct notifier_block *info,
3581da177e4SLinus Torvalds 					unsigned long action, void *__cpu)
3591da177e4SLinus Torvalds {
3601da177e4SLinus Torvalds 	int node, cpu = (unsigned long)__cpu;
3611da177e4SLinus Torvalds 	struct page *page;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	switch (action) {
3641da177e4SLinus Torvalds 	case CPU_UP_PREPARE:
3658bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
3661da177e4SLinus Torvalds 		node = cpu_to_node(cpu);
3671da177e4SLinus Torvalds 		per_cpu(cpu_profile_flip, cpu) = 0;
3681da177e4SLinus Torvalds 		if (!per_cpu(cpu_profile_hits, cpu)[1]) {
3696484eb3eSMel Gorman 			page = alloc_pages_exact_node(node,
3704199cfa0SChristoph Lameter 					GFP_KERNEL | __GFP_ZERO,
371fbd98167SChristoph Lameter 					0);
3721da177e4SLinus Torvalds 			if (!page)
3731da177e4SLinus Torvalds 				return NOTIFY_BAD;
3741da177e4SLinus Torvalds 			per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
3751da177e4SLinus Torvalds 		}
3761da177e4SLinus Torvalds 		if (!per_cpu(cpu_profile_hits, cpu)[0]) {
3776484eb3eSMel Gorman 			page = alloc_pages_exact_node(node,
3784199cfa0SChristoph Lameter 					GFP_KERNEL | __GFP_ZERO,
379fbd98167SChristoph Lameter 					0);
3801da177e4SLinus Torvalds 			if (!page)
3811da177e4SLinus Torvalds 				goto out_free;
3821da177e4SLinus Torvalds 			per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
3831da177e4SLinus Torvalds 		}
3841da177e4SLinus Torvalds 		break;
3851da177e4SLinus Torvalds out_free:
3861da177e4SLinus Torvalds 		page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
3871da177e4SLinus Torvalds 		per_cpu(cpu_profile_hits, cpu)[1] = NULL;
3881da177e4SLinus Torvalds 		__free_page(page);
3891da177e4SLinus Torvalds 		return NOTIFY_BAD;
3901da177e4SLinus Torvalds 	case CPU_ONLINE:
3918bb78442SRafael J. Wysocki 	case CPU_ONLINE_FROZEN:
392c309b917SRusty Russell 		if (prof_cpu_mask != NULL)
393c309b917SRusty Russell 			cpumask_set_cpu(cpu, prof_cpu_mask);
3941da177e4SLinus Torvalds 		break;
3951da177e4SLinus Torvalds 	case CPU_UP_CANCELED:
3968bb78442SRafael J. Wysocki 	case CPU_UP_CANCELED_FROZEN:
3971da177e4SLinus Torvalds 	case CPU_DEAD:
3988bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
399c309b917SRusty Russell 		if (prof_cpu_mask != NULL)
400c309b917SRusty Russell 			cpumask_clear_cpu(cpu, prof_cpu_mask);
4011da177e4SLinus Torvalds 		if (per_cpu(cpu_profile_hits, cpu)[0]) {
4021da177e4SLinus Torvalds 			page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
4031da177e4SLinus Torvalds 			per_cpu(cpu_profile_hits, cpu)[0] = NULL;
4041da177e4SLinus Torvalds 			__free_page(page);
4051da177e4SLinus Torvalds 		}
4061da177e4SLinus Torvalds 		if (per_cpu(cpu_profile_hits, cpu)[1]) {
4071da177e4SLinus Torvalds 			page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
4081da177e4SLinus Torvalds 			per_cpu(cpu_profile_hits, cpu)[1] = NULL;
4091da177e4SLinus Torvalds 			__free_page(page);
4101da177e4SLinus Torvalds 		}
4111da177e4SLinus Torvalds 		break;
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds 	return NOTIFY_OK;
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds #else /* !CONFIG_SMP */
4161da177e4SLinus Torvalds #define profile_flip_buffers()		do { } while (0)
4171da177e4SLinus Torvalds #define profile_discard_flip_buffers()	do { } while (0)
41802316067SIngo Molnar #define profile_cpu_callback		NULL
4191da177e4SLinus Torvalds 
420ece8a684SIngo Molnar void profile_hits(int type, void *__pc, unsigned int nr_hits)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	unsigned long pc;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	if (prof_on != type || !prof_buffer)
4251da177e4SLinus Torvalds 		return;
4261da177e4SLinus Torvalds 	pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
427ece8a684SIngo Molnar 	atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds #endif /* !CONFIG_SMP */
430bbe1a59bSAndrew Morton EXPORT_SYMBOL_GPL(profile_hits);
431bbe1a59bSAndrew Morton 
4327d12e780SDavid Howells void profile_tick(int type)
4331da177e4SLinus Torvalds {
4347d12e780SDavid Howells 	struct pt_regs *regs = get_irq_regs();
4357d12e780SDavid Howells 
4361da177e4SLinus Torvalds 	if (type == CPU_PROFILING && timer_hook)
4371da177e4SLinus Torvalds 		timer_hook(regs);
438c309b917SRusty Russell 	if (!user_mode(regs) && prof_cpu_mask != NULL &&
439c309b917SRusty Russell 	    cpumask_test_cpu(smp_processor_id(), prof_cpu_mask))
4401da177e4SLinus Torvalds 		profile_hit(type, (void *)profile_pc(regs));
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
4441da177e4SLinus Torvalds #include <linux/proc_fs.h>
445*583a22e7SAlexey Dobriyan #include <linux/seq_file.h>
4461da177e4SLinus Torvalds #include <asm/uaccess.h>
4471da177e4SLinus Torvalds 
448*583a22e7SAlexey Dobriyan static int prof_cpu_mask_proc_show(struct seq_file *m, void *v)
4491da177e4SLinus Torvalds {
450*583a22e7SAlexey Dobriyan 	seq_cpumask(m, prof_cpu_mask);
451*583a22e7SAlexey Dobriyan 	seq_putc(m, '\n');
452*583a22e7SAlexey Dobriyan 	return 0;
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
455*583a22e7SAlexey Dobriyan static int prof_cpu_mask_proc_open(struct inode *inode, struct file *file)
4561da177e4SLinus Torvalds {
457*583a22e7SAlexey Dobriyan 	return single_open(file, prof_cpu_mask_proc_show, NULL);
458*583a22e7SAlexey Dobriyan }
459*583a22e7SAlexey Dobriyan 
460*583a22e7SAlexey Dobriyan static ssize_t prof_cpu_mask_proc_write(struct file *file,
461*583a22e7SAlexey Dobriyan 	const char __user *buffer, size_t count, loff_t *pos)
462*583a22e7SAlexey Dobriyan {
463c309b917SRusty Russell 	cpumask_var_t new_value;
464*583a22e7SAlexey Dobriyan 	int err;
4651da177e4SLinus Torvalds 
466c309b917SRusty Russell 	if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
467c309b917SRusty Russell 		return -ENOMEM;
468c309b917SRusty Russell 
469c309b917SRusty Russell 	err = cpumask_parse_user(buffer, count, new_value);
470c309b917SRusty Russell 	if (!err) {
471*583a22e7SAlexey Dobriyan 		cpumask_copy(prof_cpu_mask, new_value);
472*583a22e7SAlexey Dobriyan 		err = count;
473c309b917SRusty Russell 	}
474c309b917SRusty Russell 	free_cpumask_var(new_value);
4751da177e4SLinus Torvalds 	return err;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
478*583a22e7SAlexey Dobriyan static const struct file_operations prof_cpu_mask_proc_fops = {
479*583a22e7SAlexey Dobriyan 	.open		= prof_cpu_mask_proc_open,
480*583a22e7SAlexey Dobriyan 	.read		= seq_read,
481*583a22e7SAlexey Dobriyan 	.llseek		= seq_lseek,
482*583a22e7SAlexey Dobriyan 	.release	= single_release,
483*583a22e7SAlexey Dobriyan 	.write		= prof_cpu_mask_proc_write,
484*583a22e7SAlexey Dobriyan };
485*583a22e7SAlexey Dobriyan 
4861da177e4SLinus Torvalds void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
4871da177e4SLinus Torvalds {
4881da177e4SLinus Torvalds 	/* create /proc/irq/prof_cpu_mask */
489*583a22e7SAlexey Dobriyan 	proc_create("prof_cpu_mask", 0600, root_irq_dir, &prof_cpu_mask_proc_fops);
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds /*
4931da177e4SLinus Torvalds  * This function accesses profiling information. The returned data is
4941da177e4SLinus Torvalds  * binary: the sampling step and the actual contents of the profile
4951da177e4SLinus Torvalds  * buffer. Use of the program readprofile is recommended in order to
4961da177e4SLinus Torvalds  * get meaningful info out of these data.
4971da177e4SLinus Torvalds  */
4981da177e4SLinus Torvalds static ssize_t
4991da177e4SLinus Torvalds read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	unsigned long p = *ppos;
5021da177e4SLinus Torvalds 	ssize_t read;
5031da177e4SLinus Torvalds 	char *pnt;
5041da177e4SLinus Torvalds 	unsigned int sample_step = 1 << prof_shift;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	profile_flip_buffers();
5071da177e4SLinus Torvalds 	if (p >= (prof_len+1)*sizeof(unsigned int))
5081da177e4SLinus Torvalds 		return 0;
5091da177e4SLinus Torvalds 	if (count > (prof_len+1)*sizeof(unsigned int) - p)
5101da177e4SLinus Torvalds 		count = (prof_len+1)*sizeof(unsigned int) - p;
5111da177e4SLinus Torvalds 	read = 0;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	while (p < sizeof(unsigned int) && count > 0) {
514064b022cSHeiko Carstens 		if (put_user(*((char *)(&sample_step)+p), buf))
515064b022cSHeiko Carstens 			return -EFAULT;
5161da177e4SLinus Torvalds 		buf++; p++; count--; read++;
5171da177e4SLinus Torvalds 	}
5181da177e4SLinus Torvalds 	pnt = (char *)prof_buffer + p - sizeof(atomic_t);
5191da177e4SLinus Torvalds 	if (copy_to_user(buf, (void *)pnt, count))
5201da177e4SLinus Torvalds 		return -EFAULT;
5211da177e4SLinus Torvalds 	read += count;
5221da177e4SLinus Torvalds 	*ppos += read;
5231da177e4SLinus Torvalds 	return read;
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds /*
5271da177e4SLinus Torvalds  * Writing to /proc/profile resets the counters
5281da177e4SLinus Torvalds  *
5291da177e4SLinus Torvalds  * Writing a 'profiling multiplier' value into it also re-sets the profiling
5301da177e4SLinus Torvalds  * interrupt frequency, on architectures that support this.
5311da177e4SLinus Torvalds  */
5321da177e4SLinus Torvalds static ssize_t write_profile(struct file *file, const char __user *buf,
5331da177e4SLinus Torvalds 			     size_t count, loff_t *ppos)
5341da177e4SLinus Torvalds {
5351da177e4SLinus Torvalds #ifdef CONFIG_SMP
5361da177e4SLinus Torvalds 	extern int setup_profiling_timer(unsigned int multiplier);
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	if (count == sizeof(int)) {
5391da177e4SLinus Torvalds 		unsigned int multiplier;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 		if (copy_from_user(&multiplier, buf, sizeof(int)))
5421da177e4SLinus Torvalds 			return -EFAULT;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 		if (setup_profiling_timer(multiplier))
5451da177e4SLinus Torvalds 			return -EINVAL;
5461da177e4SLinus Torvalds 	}
5471da177e4SLinus Torvalds #endif
5481da177e4SLinus Torvalds 	profile_discard_flip_buffers();
5491da177e4SLinus Torvalds 	memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
5501da177e4SLinus Torvalds 	return count;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
55315ad7cdcSHelge Deller static const struct file_operations proc_profile_operations = {
5541da177e4SLinus Torvalds 	.read		= read_profile,
5551da177e4SLinus Torvalds 	.write		= write_profile,
5561da177e4SLinus Torvalds };
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds #ifdef CONFIG_SMP
55960a51513SAndrew Morton static void profile_nop(void *unused)
5601da177e4SLinus Torvalds {
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds 
56322b8ce94SDave Hansen static int create_hash_tables(void)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds 	int cpu;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	for_each_online_cpu(cpu) {
5681da177e4SLinus Torvalds 		int node = cpu_to_node(cpu);
5691da177e4SLinus Torvalds 		struct page *page;
5701da177e4SLinus Torvalds 
5716484eb3eSMel Gorman 		page = alloc_pages_exact_node(node,
572fbd98167SChristoph Lameter 				GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
573fbd98167SChristoph Lameter 				0);
5741da177e4SLinus Torvalds 		if (!page)
5751da177e4SLinus Torvalds 			goto out_cleanup;
5761da177e4SLinus Torvalds 		per_cpu(cpu_profile_hits, cpu)[1]
5771da177e4SLinus Torvalds 				= (struct profile_hit *)page_address(page);
5786484eb3eSMel Gorman 		page = alloc_pages_exact_node(node,
579fbd98167SChristoph Lameter 				GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
580fbd98167SChristoph Lameter 				0);
5811da177e4SLinus Torvalds 		if (!page)
5821da177e4SLinus Torvalds 			goto out_cleanup;
5831da177e4SLinus Torvalds 		per_cpu(cpu_profile_hits, cpu)[0]
5841da177e4SLinus Torvalds 				= (struct profile_hit *)page_address(page);
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 	return 0;
5871da177e4SLinus Torvalds out_cleanup:
5881da177e4SLinus Torvalds 	prof_on = 0;
589d59dd462Sakpm@osdl.org 	smp_mb();
59015c8b6c1SJens Axboe 	on_each_cpu(profile_nop, NULL, 1);
5911da177e4SLinus Torvalds 	for_each_online_cpu(cpu) {
5921da177e4SLinus Torvalds 		struct page *page;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 		if (per_cpu(cpu_profile_hits, cpu)[0]) {
5951da177e4SLinus Torvalds 			page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
5961da177e4SLinus Torvalds 			per_cpu(cpu_profile_hits, cpu)[0] = NULL;
5971da177e4SLinus Torvalds 			__free_page(page);
5981da177e4SLinus Torvalds 		}
5991da177e4SLinus Torvalds 		if (per_cpu(cpu_profile_hits, cpu)[1]) {
6001da177e4SLinus Torvalds 			page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
6011da177e4SLinus Torvalds 			per_cpu(cpu_profile_hits, cpu)[1] = NULL;
6021da177e4SLinus Torvalds 			__free_page(page);
6031da177e4SLinus Torvalds 		}
6041da177e4SLinus Torvalds 	}
6051da177e4SLinus Torvalds 	return -1;
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds #else
6081da177e4SLinus Torvalds #define create_hash_tables()			({ 0; })
6091da177e4SLinus Torvalds #endif
6101da177e4SLinus Torvalds 
61184196414SAl Viro int __ref create_proc_profile(void) /* false positive from hotcpu_notifier */
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	struct proc_dir_entry *entry;
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	if (!prof_on)
6161da177e4SLinus Torvalds 		return 0;
6171da177e4SLinus Torvalds 	if (create_hash_tables())
61822b8ce94SDave Hansen 		return -ENOMEM;
619c33fff0aSDenis V. Lunev 	entry = proc_create("profile", S_IWUSR | S_IRUGO,
620c33fff0aSDenis V. Lunev 			    NULL, &proc_profile_operations);
6211ad82fd5SPaolo Ciarrocchi 	if (!entry)
6221da177e4SLinus Torvalds 		return 0;
6231da177e4SLinus Torvalds 	entry->size = (1+prof_len) * sizeof(atomic_t);
6241da177e4SLinus Torvalds 	hotcpu_notifier(profile_cpu_callback, 0);
6251da177e4SLinus Torvalds 	return 0;
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds module_init(create_proc_profile);
6281da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
629