xref: /openbmc/linux/kernel/profile.c (revision b012d346c0e914f505d92f730ad1758f73e2953b)
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/profile.h>
241da177e4SLinus Torvalds #include <linux/highmem.h>
2597d1f15bSArjan van de Ven #include <linux/mutex.h>
261da177e4SLinus Torvalds #include <asm/sections.h>
271da177e4SLinus Torvalds #include <asm/semaphore.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 */
40*b012d346SAdrian 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 
481da177e4SLinus Torvalds static cpumask_t prof_cpu_mask = CPU_MASK_ALL;
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 
551da177e4SLinus Torvalds static int __init profile_setup(char * str)
561da177e4SLinus Torvalds {
57dfaa9c94SWilliam Lee Irwin III 	static char __initdata schedstr[] = "schedule";
58ece8a684SIngo Molnar 	static char __initdata sleepstr[] = "sleep";
5907031e14SIngo Molnar 	static char __initdata kvmstr[] = "kvm";
601da177e4SLinus Torvalds 	int par;
611da177e4SLinus Torvalds 
62ece8a684SIngo Molnar 	if (!strncmp(str, sleepstr, strlen(sleepstr))) {
63ece8a684SIngo Molnar 		prof_on = SLEEP_PROFILING;
64ece8a684SIngo Molnar 		if (str[strlen(sleepstr)] == ',')
65ece8a684SIngo Molnar 			str += strlen(sleepstr) + 1;
66ece8a684SIngo Molnar 		if (get_option(&str, &par))
67ece8a684SIngo Molnar 			prof_shift = par;
68ece8a684SIngo Molnar 		printk(KERN_INFO
69ece8a684SIngo Molnar 			"kernel sleep profiling enabled (shift: %ld)\n",
70ece8a684SIngo Molnar 			prof_shift);
71a75acf85SIngo Molnar 	} else if (!strncmp(str, schedstr, strlen(schedstr))) {
721da177e4SLinus Torvalds 		prof_on = SCHED_PROFILING;
73dfaa9c94SWilliam Lee Irwin III 		if (str[strlen(schedstr)] == ',')
74dfaa9c94SWilliam Lee Irwin III 			str += strlen(schedstr) + 1;
75dfaa9c94SWilliam Lee Irwin III 		if (get_option(&str, &par))
76dfaa9c94SWilliam Lee Irwin III 			prof_shift = par;
77dfaa9c94SWilliam Lee Irwin III 		printk(KERN_INFO
78dfaa9c94SWilliam Lee Irwin III 			"kernel schedule profiling enabled (shift: %ld)\n",
79dfaa9c94SWilliam Lee Irwin III 			prof_shift);
8007031e14SIngo Molnar 	} else if (!strncmp(str, kvmstr, strlen(kvmstr))) {
8107031e14SIngo Molnar 		prof_on = KVM_PROFILING;
8207031e14SIngo Molnar 		if (str[strlen(kvmstr)] == ',')
8307031e14SIngo Molnar 			str += strlen(kvmstr) + 1;
8407031e14SIngo Molnar 		if (get_option(&str, &par))
8507031e14SIngo Molnar 			prof_shift = par;
8607031e14SIngo Molnar 		printk(KERN_INFO
8707031e14SIngo Molnar 			"kernel KVM profiling enabled (shift: %ld)\n",
8807031e14SIngo Molnar 			prof_shift);
89dfaa9c94SWilliam Lee Irwin III 	} else if (get_option(&str, &par)) {
901da177e4SLinus Torvalds 		prof_shift = par;
911da177e4SLinus Torvalds 		prof_on = CPU_PROFILING;
921da177e4SLinus Torvalds 		printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
931da177e4SLinus Torvalds 			prof_shift);
941da177e4SLinus Torvalds 	}
951da177e4SLinus Torvalds 	return 1;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds __setup("profile=", profile_setup);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds void __init profile_init(void)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	if (!prof_on)
1031da177e4SLinus Torvalds 		return;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	/* only text is profiled */
1061da177e4SLinus Torvalds 	prof_len = (_etext - _stext) >> prof_shift;
1071da177e4SLinus Torvalds 	prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* Profile event notifications */
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds #ifdef CONFIG_PROFILING
1131da177e4SLinus Torvalds 
114e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
115e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
116e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds void profile_task_exit(struct task_struct * task)
1191da177e4SLinus Torvalds {
120e041c683SAlan Stern 	blocking_notifier_call_chain(&task_exit_notifier, 0, task);
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds int profile_handoff_task(struct task_struct * task)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	int ret;
126e041c683SAlan Stern 	ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
1271da177e4SLinus Torvalds 	return (ret == NOTIFY_OK) ? 1 : 0;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds void profile_munmap(unsigned long addr)
1311da177e4SLinus Torvalds {
132e041c683SAlan Stern 	blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds int task_handoff_register(struct notifier_block * n)
1361da177e4SLinus Torvalds {
137e041c683SAlan Stern 	return atomic_notifier_chain_register(&task_free_notifier, n);
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds int task_handoff_unregister(struct notifier_block * n)
1411da177e4SLinus Torvalds {
142e041c683SAlan Stern 	return atomic_notifier_chain_unregister(&task_free_notifier, n);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds int profile_event_register(enum profile_type type, struct notifier_block * n)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	int err = -EINVAL;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	switch (type) {
1501da177e4SLinus Torvalds 		case PROFILE_TASK_EXIT:
151e041c683SAlan Stern 			err = blocking_notifier_chain_register(
152e041c683SAlan Stern 					&task_exit_notifier, n);
1531da177e4SLinus Torvalds 			break;
1541da177e4SLinus Torvalds 		case PROFILE_MUNMAP:
155e041c683SAlan Stern 			err = blocking_notifier_chain_register(
156e041c683SAlan Stern 					&munmap_notifier, n);
1571da177e4SLinus Torvalds 			break;
1581da177e4SLinus Torvalds 	}
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	return err;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds int profile_event_unregister(enum profile_type type, struct notifier_block * n)
1651da177e4SLinus Torvalds {
1661da177e4SLinus Torvalds 	int err = -EINVAL;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	switch (type) {
1691da177e4SLinus Torvalds 		case PROFILE_TASK_EXIT:
170e041c683SAlan Stern 			err = blocking_notifier_chain_unregister(
171e041c683SAlan Stern 					&task_exit_notifier, n);
1721da177e4SLinus Torvalds 			break;
1731da177e4SLinus Torvalds 		case PROFILE_MUNMAP:
174e041c683SAlan Stern 			err = blocking_notifier_chain_unregister(
175e041c683SAlan Stern 					&munmap_notifier, n);
1761da177e4SLinus Torvalds 			break;
1771da177e4SLinus Torvalds 	}
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	return err;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds int register_timer_hook(int (*hook)(struct pt_regs *))
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	if (timer_hook)
1851da177e4SLinus Torvalds 		return -EBUSY;
1861da177e4SLinus Torvalds 	timer_hook = hook;
1871da177e4SLinus Torvalds 	return 0;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds void unregister_timer_hook(int (*hook)(struct pt_regs *))
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	WARN_ON(hook != timer_hook);
1931da177e4SLinus Torvalds 	timer_hook = NULL;
1941da177e4SLinus Torvalds 	/* make sure all CPUs see the NULL hook */
195fbd568a3SPaul E. McKenney 	synchronize_sched();  /* Allow ongoing interrupts to complete. */
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(register_timer_hook);
1991da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(unregister_timer_hook);
2001da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(task_handoff_register);
2011da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(task_handoff_unregister);
202cd5bfea2SPeter Chubb EXPORT_SYMBOL_GPL(profile_event_register);
203cd5bfea2SPeter Chubb EXPORT_SYMBOL_GPL(profile_event_unregister);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds #endif /* CONFIG_PROFILING */
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds #ifdef CONFIG_SMP
2091da177e4SLinus Torvalds /*
2101da177e4SLinus Torvalds  * Each cpu has a pair of open-addressed hashtables for pending
2111da177e4SLinus Torvalds  * profile hits. read_profile() IPI's all cpus to request them
2121da177e4SLinus Torvalds  * to flip buffers and flushes their contents to prof_buffer itself.
2131da177e4SLinus Torvalds  * Flip requests are serialized by the profile_flip_mutex. The sole
2141da177e4SLinus Torvalds  * use of having a second hashtable is for avoiding cacheline
2151da177e4SLinus Torvalds  * contention that would otherwise happen during flushes of pending
2161da177e4SLinus Torvalds  * profile hits required for the accuracy of reported profile hits
2171da177e4SLinus Torvalds  * and so resurrect the interrupt livelock issue.
2181da177e4SLinus Torvalds  *
2191da177e4SLinus Torvalds  * The open-addressed hashtables are indexed by profile buffer slot
2201da177e4SLinus Torvalds  * and hold the number of pending hits to that profile buffer slot on
2211da177e4SLinus Torvalds  * a cpu in an entry. When the hashtable overflows, all pending hits
2221da177e4SLinus Torvalds  * are accounted to their corresponding profile buffer slots with
2231da177e4SLinus Torvalds  * atomic_add() and the hashtable emptied. As numerous pending hits
2241da177e4SLinus Torvalds  * may be accounted to a profile buffer slot in a hashtable entry,
2251da177e4SLinus Torvalds  * this amortizes a number of atomic profile buffer increments likely
2261da177e4SLinus Torvalds  * to be far larger than the number of entries in the hashtable,
2271da177e4SLinus Torvalds  * particularly given that the number of distinct profile buffer
2281da177e4SLinus Torvalds  * positions to which hits are accounted during short intervals (e.g.
2291da177e4SLinus Torvalds  * several seconds) is usually very small. Exclusion from buffer
2301da177e4SLinus Torvalds  * flipping is provided by interrupt disablement (note that for
231ece8a684SIngo Molnar  * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
232ece8a684SIngo Molnar  * process context).
2331da177e4SLinus Torvalds  * The hash function is meant to be lightweight as opposed to strong,
2341da177e4SLinus Torvalds  * and was vaguely inspired by ppc64 firmware-supported inverted
2351da177e4SLinus Torvalds  * pagetable hash functions, but uses a full hashtable full of finite
2361da177e4SLinus Torvalds  * collision chains, not just pairs of them.
2371da177e4SLinus Torvalds  *
2381da177e4SLinus Torvalds  * -- wli
2391da177e4SLinus Torvalds  */
2401da177e4SLinus Torvalds static void __profile_flip_buffers(void *unused)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	int cpu = smp_processor_id();
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds static void profile_flip_buffers(void)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	int i, j, cpu;
2501da177e4SLinus Torvalds 
25197d1f15bSArjan van de Ven 	mutex_lock(&profile_flip_mutex);
2521da177e4SLinus Torvalds 	j = per_cpu(cpu_profile_flip, get_cpu());
2531da177e4SLinus Torvalds 	put_cpu();
2541da177e4SLinus Torvalds 	on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
2551da177e4SLinus Torvalds 	for_each_online_cpu(cpu) {
2561da177e4SLinus Torvalds 		struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
2571da177e4SLinus Torvalds 		for (i = 0; i < NR_PROFILE_HIT; ++i) {
2581da177e4SLinus Torvalds 			if (!hits[i].hits) {
2591da177e4SLinus Torvalds 				if (hits[i].pc)
2601da177e4SLinus Torvalds 					hits[i].pc = 0;
2611da177e4SLinus Torvalds 				continue;
2621da177e4SLinus Torvalds 			}
2631da177e4SLinus Torvalds 			atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
2641da177e4SLinus Torvalds 			hits[i].hits = hits[i].pc = 0;
2651da177e4SLinus Torvalds 		}
2661da177e4SLinus Torvalds 	}
26797d1f15bSArjan van de Ven 	mutex_unlock(&profile_flip_mutex);
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds static void profile_discard_flip_buffers(void)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	int i, cpu;
2731da177e4SLinus Torvalds 
27497d1f15bSArjan van de Ven 	mutex_lock(&profile_flip_mutex);
2751da177e4SLinus Torvalds 	i = per_cpu(cpu_profile_flip, get_cpu());
2761da177e4SLinus Torvalds 	put_cpu();
2771da177e4SLinus Torvalds 	on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
2781da177e4SLinus Torvalds 	for_each_online_cpu(cpu) {
2791da177e4SLinus Torvalds 		struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
2801da177e4SLinus Torvalds 		memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
2811da177e4SLinus Torvalds 	}
28297d1f15bSArjan van de Ven 	mutex_unlock(&profile_flip_mutex);
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
285ece8a684SIngo Molnar void profile_hits(int type, void *__pc, unsigned int nr_hits)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds 	unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
2881da177e4SLinus Torvalds 	int i, j, cpu;
2891da177e4SLinus Torvalds 	struct profile_hit *hits;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	if (prof_on != type || !prof_buffer)
2921da177e4SLinus Torvalds 		return;
2931da177e4SLinus Torvalds 	pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
2941da177e4SLinus Torvalds 	i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
2951da177e4SLinus Torvalds 	secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
2961da177e4SLinus Torvalds 	cpu = get_cpu();
2971da177e4SLinus Torvalds 	hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
2981da177e4SLinus Torvalds 	if (!hits) {
2991da177e4SLinus Torvalds 		put_cpu();
3001da177e4SLinus Torvalds 		return;
3011da177e4SLinus Torvalds 	}
302ece8a684SIngo Molnar 	/*
303ece8a684SIngo Molnar 	 * We buffer the global profiler buffer into a per-CPU
304ece8a684SIngo Molnar 	 * queue and thus reduce the number of global (and possibly
305ece8a684SIngo Molnar 	 * NUMA-alien) accesses. The write-queue is self-coalescing:
306ece8a684SIngo Molnar 	 */
3071da177e4SLinus Torvalds 	local_irq_save(flags);
3081da177e4SLinus Torvalds 	do {
3091da177e4SLinus Torvalds 		for (j = 0; j < PROFILE_GRPSZ; ++j) {
3101da177e4SLinus Torvalds 			if (hits[i + j].pc == pc) {
311ece8a684SIngo Molnar 				hits[i + j].hits += nr_hits;
3121da177e4SLinus Torvalds 				goto out;
3131da177e4SLinus Torvalds 			} else if (!hits[i + j].hits) {
3141da177e4SLinus Torvalds 				hits[i + j].pc = pc;
315ece8a684SIngo Molnar 				hits[i + j].hits = nr_hits;
3161da177e4SLinus Torvalds 				goto out;
3171da177e4SLinus Torvalds 			}
3181da177e4SLinus Torvalds 		}
3191da177e4SLinus Torvalds 		i = (i + secondary) & (NR_PROFILE_HIT - 1);
3201da177e4SLinus Torvalds 	} while (i != primary);
321ece8a684SIngo Molnar 
322ece8a684SIngo Molnar 	/*
323ece8a684SIngo Molnar 	 * Add the current hit(s) and flush the write-queue out
324ece8a684SIngo Molnar 	 * to the global buffer:
325ece8a684SIngo Molnar 	 */
326ece8a684SIngo Molnar 	atomic_add(nr_hits, &prof_buffer[pc]);
3271da177e4SLinus Torvalds 	for (i = 0; i < NR_PROFILE_HIT; ++i) {
3281da177e4SLinus Torvalds 		atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
3291da177e4SLinus Torvalds 		hits[i].pc = hits[i].hits = 0;
3301da177e4SLinus Torvalds 	}
3311da177e4SLinus Torvalds out:
3321da177e4SLinus Torvalds 	local_irq_restore(flags);
3331da177e4SLinus Torvalds 	put_cpu();
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3369c7b216dSChandra Seetharaman static int __devinit profile_cpu_callback(struct notifier_block *info,
3371da177e4SLinus Torvalds 					unsigned long action, void *__cpu)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	int node, cpu = (unsigned long)__cpu;
3401da177e4SLinus Torvalds 	struct page *page;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	switch (action) {
3431da177e4SLinus Torvalds 	case CPU_UP_PREPARE:
3448bb78442SRafael J. Wysocki 	case CPU_UP_PREPARE_FROZEN:
3451da177e4SLinus Torvalds 		node = cpu_to_node(cpu);
3461da177e4SLinus Torvalds 		per_cpu(cpu_profile_flip, cpu) = 0;
3471da177e4SLinus Torvalds 		if (!per_cpu(cpu_profile_hits, cpu)[1]) {
348fbd98167SChristoph Lameter 			page = alloc_pages_node(node,
3494199cfa0SChristoph Lameter 					GFP_KERNEL | __GFP_ZERO,
350fbd98167SChristoph Lameter 					0);
3511da177e4SLinus Torvalds 			if (!page)
3521da177e4SLinus Torvalds 				return NOTIFY_BAD;
3531da177e4SLinus Torvalds 			per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
3541da177e4SLinus Torvalds 		}
3551da177e4SLinus Torvalds 		if (!per_cpu(cpu_profile_hits, cpu)[0]) {
356fbd98167SChristoph Lameter 			page = alloc_pages_node(node,
3574199cfa0SChristoph Lameter 					GFP_KERNEL | __GFP_ZERO,
358fbd98167SChristoph Lameter 					0);
3591da177e4SLinus Torvalds 			if (!page)
3601da177e4SLinus Torvalds 				goto out_free;
3611da177e4SLinus Torvalds 			per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
3621da177e4SLinus Torvalds 		}
3631da177e4SLinus Torvalds 		break;
3641da177e4SLinus Torvalds 	out_free:
3651da177e4SLinus Torvalds 		page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
3661da177e4SLinus Torvalds 		per_cpu(cpu_profile_hits, cpu)[1] = NULL;
3671da177e4SLinus Torvalds 		__free_page(page);
3681da177e4SLinus Torvalds 		return NOTIFY_BAD;
3691da177e4SLinus Torvalds 	case CPU_ONLINE:
3708bb78442SRafael J. Wysocki 	case CPU_ONLINE_FROZEN:
3711da177e4SLinus Torvalds 		cpu_set(cpu, prof_cpu_mask);
3721da177e4SLinus Torvalds 		break;
3731da177e4SLinus Torvalds 	case CPU_UP_CANCELED:
3748bb78442SRafael J. Wysocki 	case CPU_UP_CANCELED_FROZEN:
3751da177e4SLinus Torvalds 	case CPU_DEAD:
3768bb78442SRafael J. Wysocki 	case CPU_DEAD_FROZEN:
3771da177e4SLinus Torvalds 		cpu_clear(cpu, prof_cpu_mask);
3781da177e4SLinus Torvalds 		if (per_cpu(cpu_profile_hits, cpu)[0]) {
3791da177e4SLinus Torvalds 			page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
3801da177e4SLinus Torvalds 			per_cpu(cpu_profile_hits, cpu)[0] = NULL;
3811da177e4SLinus Torvalds 			__free_page(page);
3821da177e4SLinus Torvalds 		}
3831da177e4SLinus Torvalds 		if (per_cpu(cpu_profile_hits, cpu)[1]) {
3841da177e4SLinus Torvalds 			page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
3851da177e4SLinus Torvalds 			per_cpu(cpu_profile_hits, cpu)[1] = NULL;
3861da177e4SLinus Torvalds 			__free_page(page);
3871da177e4SLinus Torvalds 		}
3881da177e4SLinus Torvalds 		break;
3891da177e4SLinus Torvalds 	}
3901da177e4SLinus Torvalds 	return NOTIFY_OK;
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds #else /* !CONFIG_SMP */
3931da177e4SLinus Torvalds #define profile_flip_buffers()		do { } while (0)
3941da177e4SLinus Torvalds #define profile_discard_flip_buffers()	do { } while (0)
39502316067SIngo Molnar #define profile_cpu_callback		NULL
3961da177e4SLinus Torvalds 
397ece8a684SIngo Molnar void profile_hits(int type, void *__pc, unsigned int nr_hits)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	unsigned long pc;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	if (prof_on != type || !prof_buffer)
4021da177e4SLinus Torvalds 		return;
4031da177e4SLinus Torvalds 	pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
404ece8a684SIngo Molnar 	atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds #endif /* !CONFIG_SMP */
4071da177e4SLinus Torvalds 
408bbe1a59bSAndrew Morton EXPORT_SYMBOL_GPL(profile_hits);
409bbe1a59bSAndrew Morton 
4107d12e780SDavid Howells void profile_tick(int type)
4111da177e4SLinus Torvalds {
4127d12e780SDavid Howells 	struct pt_regs *regs = get_irq_regs();
4137d12e780SDavid Howells 
4141da177e4SLinus Torvalds 	if (type == CPU_PROFILING && timer_hook)
4151da177e4SLinus Torvalds 		timer_hook(regs);
4161da177e4SLinus Torvalds 	if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
4171da177e4SLinus Torvalds 		profile_hit(type, (void *)profile_pc(regs));
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
4211da177e4SLinus Torvalds #include <linux/proc_fs.h>
4221da177e4SLinus Torvalds #include <asm/uaccess.h>
4231da177e4SLinus Torvalds #include <asm/ptrace.h>
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
4261da177e4SLinus Torvalds 			int count, int *eof, void *data)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
4291da177e4SLinus Torvalds 	if (count - len < 2)
4301da177e4SLinus Torvalds 		return -EINVAL;
4311da177e4SLinus Torvalds 	len += sprintf(page + len, "\n");
4321da177e4SLinus Torvalds 	return len;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
4361da177e4SLinus Torvalds 					unsigned long count, void *data)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	cpumask_t *mask = (cpumask_t *)data;
4391da177e4SLinus Torvalds 	unsigned long full_count = count, err;
4401da177e4SLinus Torvalds 	cpumask_t new_value;
4411da177e4SLinus Torvalds 
44201a3ee2bSReinette Chatre 	err = cpumask_parse_user(buffer, count, new_value);
4431da177e4SLinus Torvalds 	if (err)
4441da177e4SLinus Torvalds 		return err;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	*mask = new_value;
4471da177e4SLinus Torvalds 	return full_count;
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
4511da177e4SLinus Torvalds {
4521da177e4SLinus Torvalds 	struct proc_dir_entry *entry;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	/* create /proc/irq/prof_cpu_mask */
4551da177e4SLinus Torvalds 	if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
4561da177e4SLinus Torvalds 		return;
4571da177e4SLinus Torvalds 	entry->data = (void *)&prof_cpu_mask;
4581da177e4SLinus Torvalds 	entry->read_proc = prof_cpu_mask_read_proc;
4591da177e4SLinus Torvalds 	entry->write_proc = prof_cpu_mask_write_proc;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds /*
4631da177e4SLinus Torvalds  * This function accesses profiling information. The returned data is
4641da177e4SLinus Torvalds  * binary: the sampling step and the actual contents of the profile
4651da177e4SLinus Torvalds  * buffer. Use of the program readprofile is recommended in order to
4661da177e4SLinus Torvalds  * get meaningful info out of these data.
4671da177e4SLinus Torvalds  */
4681da177e4SLinus Torvalds static ssize_t
4691da177e4SLinus Torvalds read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds 	unsigned long p = *ppos;
4721da177e4SLinus Torvalds 	ssize_t read;
4731da177e4SLinus Torvalds 	char * pnt;
4741da177e4SLinus Torvalds 	unsigned int sample_step = 1 << prof_shift;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	profile_flip_buffers();
4771da177e4SLinus Torvalds 	if (p >= (prof_len+1)*sizeof(unsigned int))
4781da177e4SLinus Torvalds 		return 0;
4791da177e4SLinus Torvalds 	if (count > (prof_len+1)*sizeof(unsigned int) - p)
4801da177e4SLinus Torvalds 		count = (prof_len+1)*sizeof(unsigned int) - p;
4811da177e4SLinus Torvalds 	read = 0;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	while (p < sizeof(unsigned int) && count > 0) {
484064b022cSHeiko Carstens 		if (put_user(*((char *)(&sample_step)+p),buf))
485064b022cSHeiko Carstens 			return -EFAULT;
4861da177e4SLinus Torvalds 		buf++; p++; count--; read++;
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 	pnt = (char *)prof_buffer + p - sizeof(atomic_t);
4891da177e4SLinus Torvalds 	if (copy_to_user(buf,(void *)pnt,count))
4901da177e4SLinus Torvalds 		return -EFAULT;
4911da177e4SLinus Torvalds 	read += count;
4921da177e4SLinus Torvalds 	*ppos += read;
4931da177e4SLinus Torvalds 	return read;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds /*
4971da177e4SLinus Torvalds  * Writing to /proc/profile resets the counters
4981da177e4SLinus Torvalds  *
4991da177e4SLinus Torvalds  * Writing a 'profiling multiplier' value into it also re-sets the profiling
5001da177e4SLinus Torvalds  * interrupt frequency, on architectures that support this.
5011da177e4SLinus Torvalds  */
5021da177e4SLinus Torvalds static ssize_t write_profile(struct file *file, const char __user *buf,
5031da177e4SLinus Torvalds 			     size_t count, loff_t *ppos)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds #ifdef CONFIG_SMP
5061da177e4SLinus Torvalds 	extern int setup_profiling_timer (unsigned int multiplier);
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	if (count == sizeof(int)) {
5091da177e4SLinus Torvalds 		unsigned int multiplier;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 		if (copy_from_user(&multiplier, buf, sizeof(int)))
5121da177e4SLinus Torvalds 			return -EFAULT;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 		if (setup_profiling_timer(multiplier))
5151da177e4SLinus Torvalds 			return -EINVAL;
5161da177e4SLinus Torvalds 	}
5171da177e4SLinus Torvalds #endif
5181da177e4SLinus Torvalds 	profile_discard_flip_buffers();
5191da177e4SLinus Torvalds 	memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
5201da177e4SLinus Torvalds 	return count;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
52315ad7cdcSHelge Deller static const struct file_operations proc_profile_operations = {
5241da177e4SLinus Torvalds 	.read		= read_profile,
5251da177e4SLinus Torvalds 	.write		= write_profile,
5261da177e4SLinus Torvalds };
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds #ifdef CONFIG_SMP
5291da177e4SLinus Torvalds static void __init profile_nop(void *unused)
5301da177e4SLinus Torvalds {
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds static int __init create_hash_tables(void)
5341da177e4SLinus Torvalds {
5351da177e4SLinus Torvalds 	int cpu;
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	for_each_online_cpu(cpu) {
5381da177e4SLinus Torvalds 		int node = cpu_to_node(cpu);
5391da177e4SLinus Torvalds 		struct page *page;
5401da177e4SLinus Torvalds 
541fbd98167SChristoph Lameter 		page = alloc_pages_node(node,
542fbd98167SChristoph Lameter 				GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
543fbd98167SChristoph Lameter 				0);
5441da177e4SLinus Torvalds 		if (!page)
5451da177e4SLinus Torvalds 			goto out_cleanup;
5461da177e4SLinus Torvalds 		per_cpu(cpu_profile_hits, cpu)[1]
5471da177e4SLinus Torvalds 				= (struct profile_hit *)page_address(page);
548fbd98167SChristoph Lameter 		page = alloc_pages_node(node,
549fbd98167SChristoph Lameter 				GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
550fbd98167SChristoph Lameter 				0);
5511da177e4SLinus Torvalds 		if (!page)
5521da177e4SLinus Torvalds 			goto out_cleanup;
5531da177e4SLinus Torvalds 		per_cpu(cpu_profile_hits, cpu)[0]
5541da177e4SLinus Torvalds 				= (struct profile_hit *)page_address(page);
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 	return 0;
5571da177e4SLinus Torvalds out_cleanup:
5581da177e4SLinus Torvalds 	prof_on = 0;
559d59dd462Sakpm@osdl.org 	smp_mb();
5601da177e4SLinus Torvalds 	on_each_cpu(profile_nop, NULL, 0, 1);
5611da177e4SLinus Torvalds 	for_each_online_cpu(cpu) {
5621da177e4SLinus Torvalds 		struct page *page;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 		if (per_cpu(cpu_profile_hits, cpu)[0]) {
5651da177e4SLinus Torvalds 			page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
5661da177e4SLinus Torvalds 			per_cpu(cpu_profile_hits, cpu)[0] = NULL;
5671da177e4SLinus Torvalds 			__free_page(page);
5681da177e4SLinus Torvalds 		}
5691da177e4SLinus Torvalds 		if (per_cpu(cpu_profile_hits, cpu)[1]) {
5701da177e4SLinus Torvalds 			page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
5711da177e4SLinus Torvalds 			per_cpu(cpu_profile_hits, cpu)[1] = NULL;
5721da177e4SLinus Torvalds 			__free_page(page);
5731da177e4SLinus Torvalds 		}
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 	return -1;
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds #else
5781da177e4SLinus Torvalds #define create_hash_tables()			({ 0; })
5791da177e4SLinus Torvalds #endif
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds static int __init create_proc_profile(void)
5821da177e4SLinus Torvalds {
5831da177e4SLinus Torvalds 	struct proc_dir_entry *entry;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	if (!prof_on)
5861da177e4SLinus Torvalds 		return 0;
5871da177e4SLinus Torvalds 	if (create_hash_tables())
5881da177e4SLinus Torvalds 		return -1;
5891da177e4SLinus Torvalds 	if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
5901da177e4SLinus Torvalds 		return 0;
5911da177e4SLinus Torvalds 	entry->proc_fops = &proc_profile_operations;
5921da177e4SLinus Torvalds 	entry->size = (1+prof_len) * sizeof(atomic_t);
5931da177e4SLinus Torvalds 	hotcpu_notifier(profile_cpu_callback, 0);
5941da177e4SLinus Torvalds 	return 0;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds module_init(create_proc_profile);
5971da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
598