blob: 04fd84e8cdbea76269b1b153e97244370ff50e78 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/profile.c
3 * Simple profiling. Manages a direct-mapped profile hit count buffer,
4 * with configurable resolution, support for restricting the cpus on
5 * which profiling is done, and switching between cpu time and
6 * schedule() calls via kernel command line parameters passed at boot.
7 *
8 * Scheduler profiling support, Arjan van de Ven and Ingo Molnar,
9 * Red Hat, July 2004
10 * Consolidation of architecture support code for profiling,
11 * William Irwin, Oracle, July 2004
12 * Amortized hit count accounting via per-cpu open-addressed hashtables
13 * to resolve timer interrupt livelocks, William Irwin, Oracle, 2004
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/profile.h>
18#include <linux/bootmem.h>
19#include <linux/notifier.h>
20#include <linux/mm.h>
21#include <linux/cpumask.h>
22#include <linux/cpu.h>
23#include <linux/profile.h>
24#include <linux/highmem.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080025#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/sections.h>
27#include <asm/semaphore.h>
David Howells7d12e782006-10-05 14:55:46 +010028#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30struct profile_hit {
31 u32 pc, hits;
32};
33#define PROFILE_GRPSHIFT 3
34#define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT)
35#define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit))
36#define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ)
37
38/* Oprofile timer tick hook */
Christoph Lameter6c036522005-07-07 17:56:59 -070039int (*timer_hook)(struct pt_regs *) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static atomic_t *prof_buffer;
42static unsigned long prof_len, prof_shift;
Ingo Molnarece8a682006-12-06 20:37:24 -080043int prof_on __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static cpumask_t prof_cpu_mask = CPU_MASK_ALL;
45#ifdef CONFIG_SMP
46static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
47static DEFINE_PER_CPU(int, cpu_profile_flip);
Arjan van de Ven97d1f152006-03-23 03:00:24 -080048static DEFINE_MUTEX(profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#endif /* CONFIG_SMP */
50
51static int __init profile_setup(char * str)
52{
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070053 static char __initdata schedstr[] = "schedule";
Ingo Molnarece8a682006-12-06 20:37:24 -080054 static char __initdata sleepstr[] = "sleep";
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int par;
56
Ingo Molnarece8a682006-12-06 20:37:24 -080057 if (!strncmp(str, sleepstr, strlen(sleepstr))) {
58 prof_on = SLEEP_PROFILING;
59 if (str[strlen(sleepstr)] == ',')
60 str += strlen(sleepstr) + 1;
61 if (get_option(&str, &par))
62 prof_shift = par;
63 printk(KERN_INFO
64 "kernel sleep profiling enabled (shift: %ld)\n",
65 prof_shift);
66 } else if (!strncmp(str, sleepstr, strlen(sleepstr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 prof_on = SCHED_PROFILING;
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070068 if (str[strlen(schedstr)] == ',')
69 str += strlen(schedstr) + 1;
70 if (get_option(&str, &par))
71 prof_shift = par;
72 printk(KERN_INFO
73 "kernel schedule profiling enabled (shift: %ld)\n",
74 prof_shift);
75 } else if (get_option(&str, &par)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 prof_shift = par;
77 prof_on = CPU_PROFILING;
78 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
79 prof_shift);
80 }
81 return 1;
82}
83__setup("profile=", profile_setup);
84
85
86void __init profile_init(void)
87{
88 if (!prof_on)
89 return;
90
91 /* only text is profiled */
92 prof_len = (_etext - _stext) >> prof_shift;
93 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
94}
95
96/* Profile event notifications */
97
98#ifdef CONFIG_PROFILING
99
Alan Sterne041c682006-03-27 01:16:30 -0800100static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
101static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
102static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104void profile_task_exit(struct task_struct * task)
105{
Alan Sterne041c682006-03-27 01:16:30 -0800106 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109int profile_handoff_task(struct task_struct * task)
110{
111 int ret;
Alan Sterne041c682006-03-27 01:16:30 -0800112 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return (ret == NOTIFY_OK) ? 1 : 0;
114}
115
116void profile_munmap(unsigned long addr)
117{
Alan Sterne041c682006-03-27 01:16:30 -0800118 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121int task_handoff_register(struct notifier_block * n)
122{
Alan Sterne041c682006-03-27 01:16:30 -0800123 return atomic_notifier_chain_register(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126int task_handoff_unregister(struct notifier_block * n)
127{
Alan Sterne041c682006-03-27 01:16:30 -0800128 return atomic_notifier_chain_unregister(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131int profile_event_register(enum profile_type type, struct notifier_block * n)
132{
133 int err = -EINVAL;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 switch (type) {
136 case PROFILE_TASK_EXIT:
Alan Sterne041c682006-03-27 01:16:30 -0800137 err = blocking_notifier_chain_register(
138 &task_exit_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 break;
140 case PROFILE_MUNMAP:
Alan Sterne041c682006-03-27 01:16:30 -0800141 err = blocking_notifier_chain_register(
142 &munmap_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 break;
144 }
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return err;
147}
148
149
150int profile_event_unregister(enum profile_type type, struct notifier_block * n)
151{
152 int err = -EINVAL;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 switch (type) {
155 case PROFILE_TASK_EXIT:
Alan Sterne041c682006-03-27 01:16:30 -0800156 err = blocking_notifier_chain_unregister(
157 &task_exit_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
159 case PROFILE_MUNMAP:
Alan Sterne041c682006-03-27 01:16:30 -0800160 err = blocking_notifier_chain_unregister(
161 &munmap_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break;
163 }
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return err;
166}
167
168int register_timer_hook(int (*hook)(struct pt_regs *))
169{
170 if (timer_hook)
171 return -EBUSY;
172 timer_hook = hook;
173 return 0;
174}
175
176void unregister_timer_hook(int (*hook)(struct pt_regs *))
177{
178 WARN_ON(hook != timer_hook);
179 timer_hook = NULL;
180 /* make sure all CPUs see the NULL hook */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -0700181 synchronize_sched(); /* Allow ongoing interrupts to complete. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184EXPORT_SYMBOL_GPL(register_timer_hook);
185EXPORT_SYMBOL_GPL(unregister_timer_hook);
186EXPORT_SYMBOL_GPL(task_handoff_register);
187EXPORT_SYMBOL_GPL(task_handoff_unregister);
188
189#endif /* CONFIG_PROFILING */
190
191EXPORT_SYMBOL_GPL(profile_event_register);
192EXPORT_SYMBOL_GPL(profile_event_unregister);
193
194#ifdef CONFIG_SMP
195/*
196 * Each cpu has a pair of open-addressed hashtables for pending
197 * profile hits. read_profile() IPI's all cpus to request them
198 * to flip buffers and flushes their contents to prof_buffer itself.
199 * Flip requests are serialized by the profile_flip_mutex. The sole
200 * use of having a second hashtable is for avoiding cacheline
201 * contention that would otherwise happen during flushes of pending
202 * profile hits required for the accuracy of reported profile hits
203 * and so resurrect the interrupt livelock issue.
204 *
205 * The open-addressed hashtables are indexed by profile buffer slot
206 * and hold the number of pending hits to that profile buffer slot on
207 * a cpu in an entry. When the hashtable overflows, all pending hits
208 * are accounted to their corresponding profile buffer slots with
209 * atomic_add() and the hashtable emptied. As numerous pending hits
210 * may be accounted to a profile buffer slot in a hashtable entry,
211 * this amortizes a number of atomic profile buffer increments likely
212 * to be far larger than the number of entries in the hashtable,
213 * particularly given that the number of distinct profile buffer
214 * positions to which hits are accounted during short intervals (e.g.
215 * several seconds) is usually very small. Exclusion from buffer
216 * flipping is provided by interrupt disablement (note that for
Ingo Molnarece8a682006-12-06 20:37:24 -0800217 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
218 * process context).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * The hash function is meant to be lightweight as opposed to strong,
220 * and was vaguely inspired by ppc64 firmware-supported inverted
221 * pagetable hash functions, but uses a full hashtable full of finite
222 * collision chains, not just pairs of them.
223 *
224 * -- wli
225 */
226static void __profile_flip_buffers(void *unused)
227{
228 int cpu = smp_processor_id();
229
230 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
231}
232
233static void profile_flip_buffers(void)
234{
235 int i, j, cpu;
236
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800237 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 j = per_cpu(cpu_profile_flip, get_cpu());
239 put_cpu();
240 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
241 for_each_online_cpu(cpu) {
242 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
243 for (i = 0; i < NR_PROFILE_HIT; ++i) {
244 if (!hits[i].hits) {
245 if (hits[i].pc)
246 hits[i].pc = 0;
247 continue;
248 }
249 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
250 hits[i].hits = hits[i].pc = 0;
251 }
252 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800253 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256static void profile_discard_flip_buffers(void)
257{
258 int i, cpu;
259
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800260 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 i = per_cpu(cpu_profile_flip, get_cpu());
262 put_cpu();
263 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
264 for_each_online_cpu(cpu) {
265 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
266 memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
267 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800268 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Ingo Molnarece8a682006-12-06 20:37:24 -0800271void profile_hits(int type, void *__pc, unsigned int nr_hits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
274 int i, j, cpu;
275 struct profile_hit *hits;
276
277 if (prof_on != type || !prof_buffer)
278 return;
279 pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
280 i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
281 secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
282 cpu = get_cpu();
283 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
284 if (!hits) {
285 put_cpu();
286 return;
287 }
Ingo Molnarece8a682006-12-06 20:37:24 -0800288 /*
289 * We buffer the global profiler buffer into a per-CPU
290 * queue and thus reduce the number of global (and possibly
291 * NUMA-alien) accesses. The write-queue is self-coalescing:
292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 local_irq_save(flags);
294 do {
295 for (j = 0; j < PROFILE_GRPSZ; ++j) {
296 if (hits[i + j].pc == pc) {
Ingo Molnarece8a682006-12-06 20:37:24 -0800297 hits[i + j].hits += nr_hits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 goto out;
299 } else if (!hits[i + j].hits) {
300 hits[i + j].pc = pc;
Ingo Molnarece8a682006-12-06 20:37:24 -0800301 hits[i + j].hits = nr_hits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 goto out;
303 }
304 }
305 i = (i + secondary) & (NR_PROFILE_HIT - 1);
306 } while (i != primary);
Ingo Molnarece8a682006-12-06 20:37:24 -0800307
308 /*
309 * Add the current hit(s) and flush the write-queue out
310 * to the global buffer:
311 */
312 atomic_add(nr_hits, &prof_buffer[pc]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 for (i = 0; i < NR_PROFILE_HIT; ++i) {
314 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
315 hits[i].pc = hits[i].hits = 0;
316 }
317out:
318 local_irq_restore(flags);
319 put_cpu();
320}
321
322#ifdef CONFIG_HOTPLUG_CPU
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -0700323static int __devinit profile_cpu_callback(struct notifier_block *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned long action, void *__cpu)
325{
326 int node, cpu = (unsigned long)__cpu;
327 struct page *page;
328
329 switch (action) {
330 case CPU_UP_PREPARE:
331 node = cpu_to_node(cpu);
332 per_cpu(cpu_profile_flip, cpu) = 0;
333 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
Christoph Lameterfbd98162006-09-25 23:31:45 -0700334 page = alloc_pages_node(node,
335 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
336 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (!page)
338 return NOTIFY_BAD;
339 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
340 }
341 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
Christoph Lameterfbd98162006-09-25 23:31:45 -0700342 page = alloc_pages_node(node,
343 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
344 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (!page)
346 goto out_free;
347 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
348 }
349 break;
350 out_free:
351 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
352 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
353 __free_page(page);
354 return NOTIFY_BAD;
355 case CPU_ONLINE:
356 cpu_set(cpu, prof_cpu_mask);
357 break;
358 case CPU_UP_CANCELED:
359 case CPU_DEAD:
360 cpu_clear(cpu, prof_cpu_mask);
361 if (per_cpu(cpu_profile_hits, cpu)[0]) {
362 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
363 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
364 __free_page(page);
365 }
366 if (per_cpu(cpu_profile_hits, cpu)[1]) {
367 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
368 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
369 __free_page(page);
370 }
371 break;
372 }
373 return NOTIFY_OK;
374}
375#endif /* CONFIG_HOTPLUG_CPU */
376#else /* !CONFIG_SMP */
377#define profile_flip_buffers() do { } while (0)
378#define profile_discard_flip_buffers() do { } while (0)
379
Ingo Molnarece8a682006-12-06 20:37:24 -0800380void profile_hits(int type, void *__pc, unsigned int nr_hits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 unsigned long pc;
383
384 if (prof_on != type || !prof_buffer)
385 return;
386 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
Ingo Molnarece8a682006-12-06 20:37:24 -0800387 atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389#endif /* !CONFIG_SMP */
390
David Howells7d12e782006-10-05 14:55:46 +0100391void profile_tick(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
David Howells7d12e782006-10-05 14:55:46 +0100393 struct pt_regs *regs = get_irq_regs();
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (type == CPU_PROFILING && timer_hook)
396 timer_hook(regs);
397 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
398 profile_hit(type, (void *)profile_pc(regs));
399}
400
401#ifdef CONFIG_PROC_FS
402#include <linux/proc_fs.h>
403#include <asm/uaccess.h>
404#include <asm/ptrace.h>
405
406static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
407 int count, int *eof, void *data)
408{
409 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
410 if (count - len < 2)
411 return -EINVAL;
412 len += sprintf(page + len, "\n");
413 return len;
414}
415
416static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
417 unsigned long count, void *data)
418{
419 cpumask_t *mask = (cpumask_t *)data;
420 unsigned long full_count = count, err;
421 cpumask_t new_value;
422
Reinette Chatre01a3ee22006-10-11 01:21:55 -0700423 err = cpumask_parse_user(buffer, count, new_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (err)
425 return err;
426
427 *mask = new_value;
428 return full_count;
429}
430
431void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
432{
433 struct proc_dir_entry *entry;
434
435 /* create /proc/irq/prof_cpu_mask */
436 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
437 return;
438 entry->nlink = 1;
439 entry->data = (void *)&prof_cpu_mask;
440 entry->read_proc = prof_cpu_mask_read_proc;
441 entry->write_proc = prof_cpu_mask_write_proc;
442}
443
444/*
445 * This function accesses profiling information. The returned data is
446 * binary: the sampling step and the actual contents of the profile
447 * buffer. Use of the program readprofile is recommended in order to
448 * get meaningful info out of these data.
449 */
450static ssize_t
451read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
452{
453 unsigned long p = *ppos;
454 ssize_t read;
455 char * pnt;
456 unsigned int sample_step = 1 << prof_shift;
457
458 profile_flip_buffers();
459 if (p >= (prof_len+1)*sizeof(unsigned int))
460 return 0;
461 if (count > (prof_len+1)*sizeof(unsigned int) - p)
462 count = (prof_len+1)*sizeof(unsigned int) - p;
463 read = 0;
464
465 while (p < sizeof(unsigned int) && count > 0) {
Heiko Carstens064b0222006-12-06 20:36:37 -0800466 if (put_user(*((char *)(&sample_step)+p),buf))
467 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 buf++; p++; count--; read++;
469 }
470 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
471 if (copy_to_user(buf,(void *)pnt,count))
472 return -EFAULT;
473 read += count;
474 *ppos += read;
475 return read;
476}
477
478/*
479 * Writing to /proc/profile resets the counters
480 *
481 * Writing a 'profiling multiplier' value into it also re-sets the profiling
482 * interrupt frequency, on architectures that support this.
483 */
484static ssize_t write_profile(struct file *file, const char __user *buf,
485 size_t count, loff_t *ppos)
486{
487#ifdef CONFIG_SMP
488 extern int setup_profiling_timer (unsigned int multiplier);
489
490 if (count == sizeof(int)) {
491 unsigned int multiplier;
492
493 if (copy_from_user(&multiplier, buf, sizeof(int)))
494 return -EFAULT;
495
496 if (setup_profiling_timer(multiplier))
497 return -EINVAL;
498 }
499#endif
500 profile_discard_flip_buffers();
501 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
502 return count;
503}
504
505static struct file_operations proc_profile_operations = {
506 .read = read_profile,
507 .write = write_profile,
508};
509
510#ifdef CONFIG_SMP
511static void __init profile_nop(void *unused)
512{
513}
514
515static int __init create_hash_tables(void)
516{
517 int cpu;
518
519 for_each_online_cpu(cpu) {
520 int node = cpu_to_node(cpu);
521 struct page *page;
522
Christoph Lameterfbd98162006-09-25 23:31:45 -0700523 page = alloc_pages_node(node,
524 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
525 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (!page)
527 goto out_cleanup;
528 per_cpu(cpu_profile_hits, cpu)[1]
529 = (struct profile_hit *)page_address(page);
Christoph Lameterfbd98162006-09-25 23:31:45 -0700530 page = alloc_pages_node(node,
531 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
532 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (!page)
534 goto out_cleanup;
535 per_cpu(cpu_profile_hits, cpu)[0]
536 = (struct profile_hit *)page_address(page);
537 }
538 return 0;
539out_cleanup:
540 prof_on = 0;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700541 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 on_each_cpu(profile_nop, NULL, 0, 1);
543 for_each_online_cpu(cpu) {
544 struct page *page;
545
546 if (per_cpu(cpu_profile_hits, cpu)[0]) {
547 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
548 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
549 __free_page(page);
550 }
551 if (per_cpu(cpu_profile_hits, cpu)[1]) {
552 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
553 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
554 __free_page(page);
555 }
556 }
557 return -1;
558}
559#else
560#define create_hash_tables() ({ 0; })
561#endif
562
563static int __init create_proc_profile(void)
564{
565 struct proc_dir_entry *entry;
566
567 if (!prof_on)
568 return 0;
569 if (create_hash_tables())
570 return -1;
571 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
572 return 0;
573 entry->proc_fops = &proc_profile_operations;
574 entry->size = (1+prof_len) * sizeof(atomic_t);
575 hotcpu_notifier(profile_cpu_callback, 0);
576 return 0;
577}
578module_init(create_proc_profile);
579#endif /* CONFIG_PROC_FS */