Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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, |
Nadia Yvette Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 11 | * Nadia Yvette Chambers, Oracle, July 2004 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Amortized hit count accounting via per-cpu open-addressed hashtables |
Nadia Yvette Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 13 | * to resolve timer interrupt livelocks, Nadia Yvette Chambers, |
| 14 | * Oracle, 2004 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 17 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/profile.h> |
| 19 | #include <linux/bootmem.h> |
| 20 | #include <linux/notifier.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/cpumask.h> |
| 23 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/highmem.h> |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 25 | #include <linux/mutex.h> |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/sections.h> |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 29 | #include <asm/irq_regs.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 30 | #include <asm/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | struct profile_hit { |
| 33 | u32 pc, hits; |
| 34 | }; |
| 35 | #define PROFILE_GRPSHIFT 3 |
| 36 | #define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT) |
| 37 | #define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit)) |
| 38 | #define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ) |
| 39 | |
| 40 | /* Oprofile timer tick hook */ |
Adrian Bunk | b012d34 | 2007-10-16 23:29:26 -0700 | [diff] [blame] | 41 | static int (*timer_hook)(struct pt_regs *) __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | static atomic_t *prof_buffer; |
| 44 | static unsigned long prof_len, prof_shift; |
Ingo Molnar | 07031e1 | 2007-01-10 23:15:38 -0800 | [diff] [blame] | 45 | |
Ingo Molnar | ece8a68 | 2006-12-06 20:37:24 -0800 | [diff] [blame] | 46 | int prof_on __read_mostly; |
Ingo Molnar | 07031e1 | 2007-01-10 23:15:38 -0800 | [diff] [blame] | 47 | EXPORT_SYMBOL_GPL(prof_on); |
| 48 | |
Rusty Russell | c309b91 | 2009-01-01 10:12:27 +1030 | [diff] [blame] | 49 | static cpumask_var_t prof_cpu_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #ifdef CONFIG_SMP |
| 51 | static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits); |
| 52 | static DEFINE_PER_CPU(int, cpu_profile_flip); |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 53 | static DEFINE_MUTEX(profile_flip_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #endif /* CONFIG_SMP */ |
| 55 | |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 56 | int profile_setup(char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 58 | static char schedstr[] = "schedule"; |
| 59 | static char sleepstr[] = "sleep"; |
| 60 | static char kvmstr[] = "kvm"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | int par; |
| 62 | |
Ingo Molnar | ece8a68 | 2006-12-06 20:37:24 -0800 | [diff] [blame] | 63 | if (!strncmp(str, sleepstr, strlen(sleepstr))) { |
Mel Gorman | b3da2a7 | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 64 | #ifdef CONFIG_SCHEDSTATS |
Ingo Molnar | ece8a68 | 2006-12-06 20:37:24 -0800 | [diff] [blame] | 65 | prof_on = SLEEP_PROFILING; |
| 66 | if (str[strlen(sleepstr)] == ',') |
| 67 | str += strlen(sleepstr) + 1; |
| 68 | if (get_option(&str, &par)) |
| 69 | prof_shift = par; |
| 70 | printk(KERN_INFO |
| 71 | "kernel sleep profiling enabled (shift: %ld)\n", |
| 72 | prof_shift); |
Mel Gorman | b3da2a7 | 2007-10-24 18:23:50 +0200 | [diff] [blame] | 73 | #else |
| 74 | printk(KERN_WARNING |
| 75 | "kernel sleep profiling requires CONFIG_SCHEDSTATS\n"); |
| 76 | #endif /* CONFIG_SCHEDSTATS */ |
Ingo Molnar | a75acf8 | 2007-01-05 16:36:29 -0800 | [diff] [blame] | 77 | } else if (!strncmp(str, schedstr, strlen(schedstr))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | prof_on = SCHED_PROFILING; |
William Lee Irwin III | dfaa9c9 | 2005-05-16 21:53:58 -0700 | [diff] [blame] | 79 | if (str[strlen(schedstr)] == ',') |
| 80 | str += strlen(schedstr) + 1; |
| 81 | if (get_option(&str, &par)) |
| 82 | prof_shift = par; |
| 83 | printk(KERN_INFO |
| 84 | "kernel schedule profiling enabled (shift: %ld)\n", |
| 85 | prof_shift); |
Ingo Molnar | 07031e1 | 2007-01-10 23:15:38 -0800 | [diff] [blame] | 86 | } else if (!strncmp(str, kvmstr, strlen(kvmstr))) { |
| 87 | prof_on = KVM_PROFILING; |
| 88 | if (str[strlen(kvmstr)] == ',') |
| 89 | str += strlen(kvmstr) + 1; |
| 90 | if (get_option(&str, &par)) |
| 91 | prof_shift = par; |
| 92 | printk(KERN_INFO |
| 93 | "kernel KVM profiling enabled (shift: %ld)\n", |
| 94 | prof_shift); |
William Lee Irwin III | dfaa9c9 | 2005-05-16 21:53:58 -0700 | [diff] [blame] | 95 | } else if (get_option(&str, &par)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | prof_shift = par; |
| 97 | prof_on = CPU_PROFILING; |
| 98 | printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n", |
| 99 | prof_shift); |
| 100 | } |
| 101 | return 1; |
| 102 | } |
| 103 | __setup("profile=", profile_setup); |
| 104 | |
| 105 | |
Paul Mundt | ce05fcc | 2008-10-29 14:01:07 -0700 | [diff] [blame] | 106 | int __ref profile_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 108 | int buffer_bytes; |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 109 | if (!prof_on) |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 110 | return 0; |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /* only text is profiled */ |
| 113 | prof_len = (_etext - _stext) >> prof_shift; |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 114 | buffer_bytes = prof_len*sizeof(atomic_t); |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 115 | |
Rusty Russell | c309b91 | 2009-01-01 10:12:27 +1030 | [diff] [blame] | 116 | if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL)) |
| 117 | return -ENOMEM; |
| 118 | |
Hugh Dickins | acd8957 | 2009-02-09 19:20:50 +0000 | [diff] [blame] | 119 | cpumask_copy(prof_cpu_mask, cpu_possible_mask); |
| 120 | |
Mel Gorman | b62f495 | 2009-07-29 15:04:09 -0700 | [diff] [blame] | 121 | prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN); |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 122 | if (prof_buffer) |
| 123 | return 0; |
| 124 | |
Mel Gorman | b62f495 | 2009-07-29 15:04:09 -0700 | [diff] [blame] | 125 | prof_buffer = alloc_pages_exact(buffer_bytes, |
| 126 | GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN); |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 127 | if (prof_buffer) |
| 128 | return 0; |
| 129 | |
Jesper Juhl | 559fa6e | 2010-10-30 21:56:26 +0200 | [diff] [blame] | 130 | prof_buffer = vzalloc(buffer_bytes); |
| 131 | if (prof_buffer) |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 132 | return 0; |
| 133 | |
Rusty Russell | c309b91 | 2009-01-01 10:12:27 +1030 | [diff] [blame] | 134 | free_cpumask_var(prof_cpu_mask); |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 135 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* Profile event notifications */ |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 139 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 140 | static BLOCKING_NOTIFIER_HEAD(task_exit_notifier); |
| 141 | static ATOMIC_NOTIFIER_HEAD(task_free_notifier); |
| 142 | static BLOCKING_NOTIFIER_HEAD(munmap_notifier); |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 143 | |
| 144 | void profile_task_exit(struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 146 | blocking_notifier_call_chain(&task_exit_notifier, 0, task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 148 | |
| 149 | int profile_handoff_task(struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | int ret; |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 152 | ret = atomic_notifier_call_chain(&task_free_notifier, 0, task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return (ret == NOTIFY_OK) ? 1 : 0; |
| 154 | } |
| 155 | |
| 156 | void profile_munmap(unsigned long addr) |
| 157 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 158 | blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 161 | int task_handoff_register(struct notifier_block *n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 163 | return atomic_notifier_chain_register(&task_free_notifier, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 165 | EXPORT_SYMBOL_GPL(task_handoff_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 167 | int task_handoff_unregister(struct notifier_block *n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 169 | return atomic_notifier_chain_unregister(&task_free_notifier, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 171 | EXPORT_SYMBOL_GPL(task_handoff_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 173 | int profile_event_register(enum profile_type type, struct notifier_block *n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
| 175 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | switch (type) { |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 178 | case PROFILE_TASK_EXIT: |
| 179 | err = blocking_notifier_chain_register( |
| 180 | &task_exit_notifier, n); |
| 181 | break; |
| 182 | case PROFILE_MUNMAP: |
| 183 | err = blocking_notifier_chain_register( |
| 184 | &munmap_notifier, n); |
| 185 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | return err; |
| 189 | } |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 190 | EXPORT_SYMBOL_GPL(profile_event_register); |
| 191 | |
| 192 | int profile_event_unregister(enum profile_type type, struct notifier_block *n) |
| 193 | { |
| 194 | int err = -EINVAL; |
| 195 | |
| 196 | switch (type) { |
| 197 | case PROFILE_TASK_EXIT: |
| 198 | err = blocking_notifier_chain_unregister( |
| 199 | &task_exit_notifier, n); |
| 200 | break; |
| 201 | case PROFILE_MUNMAP: |
| 202 | err = blocking_notifier_chain_unregister( |
| 203 | &munmap_notifier, n); |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | return err; |
| 208 | } |
| 209 | EXPORT_SYMBOL_GPL(profile_event_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | int register_timer_hook(int (*hook)(struct pt_regs *)) |
| 212 | { |
| 213 | if (timer_hook) |
| 214 | return -EBUSY; |
| 215 | timer_hook = hook; |
| 216 | return 0; |
| 217 | } |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 218 | EXPORT_SYMBOL_GPL(register_timer_hook); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | void unregister_timer_hook(int (*hook)(struct pt_regs *)) |
| 221 | { |
| 222 | WARN_ON(hook != timer_hook); |
| 223 | timer_hook = NULL; |
| 224 | /* make sure all CPUs see the NULL hook */ |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 225 | synchronize_sched(); /* Allow ongoing interrupts to complete. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | EXPORT_SYMBOL_GPL(unregister_timer_hook); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | #ifdef CONFIG_SMP |
| 231 | /* |
| 232 | * Each cpu has a pair of open-addressed hashtables for pending |
| 233 | * profile hits. read_profile() IPI's all cpus to request them |
| 234 | * to flip buffers and flushes their contents to prof_buffer itself. |
| 235 | * Flip requests are serialized by the profile_flip_mutex. The sole |
| 236 | * use of having a second hashtable is for avoiding cacheline |
| 237 | * contention that would otherwise happen during flushes of pending |
| 238 | * profile hits required for the accuracy of reported profile hits |
| 239 | * and so resurrect the interrupt livelock issue. |
| 240 | * |
| 241 | * The open-addressed hashtables are indexed by profile buffer slot |
| 242 | * and hold the number of pending hits to that profile buffer slot on |
| 243 | * a cpu in an entry. When the hashtable overflows, all pending hits |
| 244 | * are accounted to their corresponding profile buffer slots with |
| 245 | * atomic_add() and the hashtable emptied. As numerous pending hits |
| 246 | * may be accounted to a profile buffer slot in a hashtable entry, |
| 247 | * this amortizes a number of atomic profile buffer increments likely |
| 248 | * to be far larger than the number of entries in the hashtable, |
| 249 | * particularly given that the number of distinct profile buffer |
| 250 | * positions to which hits are accounted during short intervals (e.g. |
| 251 | * several seconds) is usually very small. Exclusion from buffer |
| 252 | * flipping is provided by interrupt disablement (note that for |
Ingo Molnar | ece8a68 | 2006-12-06 20:37:24 -0800 | [diff] [blame] | 253 | * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from |
| 254 | * process context). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | * The hash function is meant to be lightweight as opposed to strong, |
| 256 | * and was vaguely inspired by ppc64 firmware-supported inverted |
| 257 | * pagetable hash functions, but uses a full hashtable full of finite |
| 258 | * collision chains, not just pairs of them. |
| 259 | * |
Nadia Yvette Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 260 | * -- nyc |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | */ |
| 262 | static void __profile_flip_buffers(void *unused) |
| 263 | { |
| 264 | int cpu = smp_processor_id(); |
| 265 | |
| 266 | per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu); |
| 267 | } |
| 268 | |
| 269 | static void profile_flip_buffers(void) |
| 270 | { |
| 271 | int i, j, cpu; |
| 272 | |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 273 | mutex_lock(&profile_flip_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | j = per_cpu(cpu_profile_flip, get_cpu()); |
| 275 | put_cpu(); |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 276 | on_each_cpu(__profile_flip_buffers, NULL, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | for_each_online_cpu(cpu) { |
| 278 | struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j]; |
| 279 | for (i = 0; i < NR_PROFILE_HIT; ++i) { |
| 280 | if (!hits[i].hits) { |
| 281 | if (hits[i].pc) |
| 282 | hits[i].pc = 0; |
| 283 | continue; |
| 284 | } |
| 285 | atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]); |
| 286 | hits[i].hits = hits[i].pc = 0; |
| 287 | } |
| 288 | } |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 289 | mutex_unlock(&profile_flip_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static void profile_discard_flip_buffers(void) |
| 293 | { |
| 294 | int i, cpu; |
| 295 | |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 296 | mutex_lock(&profile_flip_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | i = per_cpu(cpu_profile_flip, get_cpu()); |
| 298 | put_cpu(); |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 299 | on_each_cpu(__profile_flip_buffers, NULL, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | for_each_online_cpu(cpu) { |
| 301 | struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i]; |
| 302 | memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit)); |
| 303 | } |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 304 | mutex_unlock(&profile_flip_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Rakib Mullick | 6f7bd76 | 2011-05-26 16:26:00 -0700 | [diff] [blame] | 307 | static void do_profile_hits(int type, void *__pc, unsigned int nr_hits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
| 309 | unsigned long primary, secondary, flags, pc = (unsigned long)__pc; |
| 310 | int i, j, cpu; |
| 311 | struct profile_hit *hits; |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1); |
| 314 | i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT; |
| 315 | secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT; |
| 316 | cpu = get_cpu(); |
| 317 | hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)]; |
| 318 | if (!hits) { |
| 319 | put_cpu(); |
| 320 | return; |
| 321 | } |
Ingo Molnar | ece8a68 | 2006-12-06 20:37:24 -0800 | [diff] [blame] | 322 | /* |
| 323 | * We buffer the global profiler buffer into a per-CPU |
| 324 | * queue and thus reduce the number of global (and possibly |
| 325 | * NUMA-alien) accesses. The write-queue is self-coalescing: |
| 326 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | local_irq_save(flags); |
| 328 | do { |
| 329 | for (j = 0; j < PROFILE_GRPSZ; ++j) { |
| 330 | if (hits[i + j].pc == pc) { |
Ingo Molnar | ece8a68 | 2006-12-06 20:37:24 -0800 | [diff] [blame] | 331 | hits[i + j].hits += nr_hits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | goto out; |
| 333 | } else if (!hits[i + j].hits) { |
| 334 | hits[i + j].pc = pc; |
Ingo Molnar | ece8a68 | 2006-12-06 20:37:24 -0800 | [diff] [blame] | 335 | hits[i + j].hits = nr_hits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | goto out; |
| 337 | } |
| 338 | } |
| 339 | i = (i + secondary) & (NR_PROFILE_HIT - 1); |
| 340 | } while (i != primary); |
Ingo Molnar | ece8a68 | 2006-12-06 20:37:24 -0800 | [diff] [blame] | 341 | |
| 342 | /* |
| 343 | * Add the current hit(s) and flush the write-queue out |
| 344 | * to the global buffer: |
| 345 | */ |
| 346 | atomic_add(nr_hits, &prof_buffer[pc]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | for (i = 0; i < NR_PROFILE_HIT; ++i) { |
| 348 | atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]); |
| 349 | hits[i].pc = hits[i].hits = 0; |
| 350 | } |
| 351 | out: |
| 352 | local_irq_restore(flags); |
| 353 | put_cpu(); |
| 354 | } |
| 355 | |
Al Viro | 8419641 | 2008-11-22 17:36:44 +0000 | [diff] [blame] | 356 | static int __cpuinit profile_cpu_callback(struct notifier_block *info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | unsigned long action, void *__cpu) |
| 358 | { |
| 359 | int node, cpu = (unsigned long)__cpu; |
| 360 | struct page *page; |
| 361 | |
| 362 | switch (action) { |
| 363 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 364 | case CPU_UP_PREPARE_FROZEN: |
Lee Schermerhorn | 3dd6b5f | 2010-05-26 14:45:04 -0700 | [diff] [blame] | 365 | node = cpu_to_mem(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | per_cpu(cpu_profile_flip, cpu) = 0; |
| 367 | if (!per_cpu(cpu_profile_hits, cpu)[1]) { |
Mel Gorman | 6484eb3 | 2009-06-16 15:31:54 -0700 | [diff] [blame] | 368 | page = alloc_pages_exact_node(node, |
Christoph Lameter | 4199cfa | 2007-10-16 01:25:34 -0700 | [diff] [blame] | 369 | GFP_KERNEL | __GFP_ZERO, |
Christoph Lameter | fbd9816 | 2006-09-25 23:31:45 -0700 | [diff] [blame] | 370 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | if (!page) |
Akinobu Mita | 80b5184 | 2010-05-26 14:43:32 -0700 | [diff] [blame] | 372 | return notifier_from_errno(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | per_cpu(cpu_profile_hits, cpu)[1] = page_address(page); |
| 374 | } |
| 375 | if (!per_cpu(cpu_profile_hits, cpu)[0]) { |
Mel Gorman | 6484eb3 | 2009-06-16 15:31:54 -0700 | [diff] [blame] | 376 | page = alloc_pages_exact_node(node, |
Christoph Lameter | 4199cfa | 2007-10-16 01:25:34 -0700 | [diff] [blame] | 377 | GFP_KERNEL | __GFP_ZERO, |
Christoph Lameter | fbd9816 | 2006-09-25 23:31:45 -0700 | [diff] [blame] | 378 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | if (!page) |
| 380 | goto out_free; |
| 381 | per_cpu(cpu_profile_hits, cpu)[0] = page_address(page); |
| 382 | } |
| 383 | break; |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 384 | out_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]); |
| 386 | per_cpu(cpu_profile_hits, cpu)[1] = NULL; |
| 387 | __free_page(page); |
Akinobu Mita | 80b5184 | 2010-05-26 14:43:32 -0700 | [diff] [blame] | 388 | return notifier_from_errno(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 390 | case CPU_ONLINE_FROZEN: |
Rusty Russell | c309b91 | 2009-01-01 10:12:27 +1030 | [diff] [blame] | 391 | if (prof_cpu_mask != NULL) |
| 392 | cpumask_set_cpu(cpu, prof_cpu_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | break; |
| 394 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 395 | case CPU_UP_CANCELED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 397 | case CPU_DEAD_FROZEN: |
Rusty Russell | c309b91 | 2009-01-01 10:12:27 +1030 | [diff] [blame] | 398 | if (prof_cpu_mask != NULL) |
| 399 | cpumask_clear_cpu(cpu, prof_cpu_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (per_cpu(cpu_profile_hits, cpu)[0]) { |
| 401 | page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]); |
| 402 | per_cpu(cpu_profile_hits, cpu)[0] = NULL; |
| 403 | __free_page(page); |
| 404 | } |
| 405 | if (per_cpu(cpu_profile_hits, cpu)[1]) { |
| 406 | page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]); |
| 407 | per_cpu(cpu_profile_hits, cpu)[1] = NULL; |
| 408 | __free_page(page); |
| 409 | } |
| 410 | break; |
| 411 | } |
| 412 | return NOTIFY_OK; |
| 413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | #else /* !CONFIG_SMP */ |
| 415 | #define profile_flip_buffers() do { } while (0) |
| 416 | #define profile_discard_flip_buffers() do { } while (0) |
Ingo Molnar | 02316067 | 2006-12-06 20:38:17 -0800 | [diff] [blame] | 417 | #define profile_cpu_callback NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Rakib Mullick | 6f7bd76 | 2011-05-26 16:26:00 -0700 | [diff] [blame] | 419 | static void do_profile_hits(int type, void *__pc, unsigned int nr_hits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
| 421 | unsigned long pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift; |
Ingo Molnar | ece8a68 | 2006-12-06 20:37:24 -0800 | [diff] [blame] | 423 | atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | #endif /* !CONFIG_SMP */ |
Rakib Mullick | 6f7bd76 | 2011-05-26 16:26:00 -0700 | [diff] [blame] | 426 | |
| 427 | void profile_hits(int type, void *__pc, unsigned int nr_hits) |
| 428 | { |
| 429 | if (prof_on != type || !prof_buffer) |
| 430 | return; |
| 431 | do_profile_hits(type, __pc, nr_hits); |
| 432 | } |
Andrew Morton | bbe1a59b | 2007-01-22 20:40:33 -0800 | [diff] [blame] | 433 | EXPORT_SYMBOL_GPL(profile_hits); |
| 434 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 435 | void profile_tick(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 437 | struct pt_regs *regs = get_irq_regs(); |
| 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | if (type == CPU_PROFILING && timer_hook) |
| 440 | timer_hook(regs); |
Rusty Russell | c309b91 | 2009-01-01 10:12:27 +1030 | [diff] [blame] | 441 | if (!user_mode(regs) && prof_cpu_mask != NULL && |
| 442 | cpumask_test_cpu(smp_processor_id(), prof_cpu_mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | profile_hit(type, (void *)profile_pc(regs)); |
| 444 | } |
| 445 | |
| 446 | #ifdef CONFIG_PROC_FS |
| 447 | #include <linux/proc_fs.h> |
Alexey Dobriyan | 583a22e | 2009-09-18 12:57:09 -0700 | [diff] [blame] | 448 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Alexey Dobriyan | 583a22e | 2009-09-18 12:57:09 -0700 | [diff] [blame] | 451 | static int prof_cpu_mask_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
Alexey Dobriyan | 583a22e | 2009-09-18 12:57:09 -0700 | [diff] [blame] | 453 | seq_cpumask(m, prof_cpu_mask); |
| 454 | seq_putc(m, '\n'); |
| 455 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Alexey Dobriyan | 583a22e | 2009-09-18 12:57:09 -0700 | [diff] [blame] | 458 | static int prof_cpu_mask_proc_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | { |
Alexey Dobriyan | 583a22e | 2009-09-18 12:57:09 -0700 | [diff] [blame] | 460 | return single_open(file, prof_cpu_mask_proc_show, NULL); |
| 461 | } |
| 462 | |
| 463 | static ssize_t prof_cpu_mask_proc_write(struct file *file, |
| 464 | const char __user *buffer, size_t count, loff_t *pos) |
| 465 | { |
Rusty Russell | c309b91 | 2009-01-01 10:12:27 +1030 | [diff] [blame] | 466 | cpumask_var_t new_value; |
Alexey Dobriyan | 583a22e | 2009-09-18 12:57:09 -0700 | [diff] [blame] | 467 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
Rusty Russell | c309b91 | 2009-01-01 10:12:27 +1030 | [diff] [blame] | 469 | if (!alloc_cpumask_var(&new_value, GFP_KERNEL)) |
| 470 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Rusty Russell | c309b91 | 2009-01-01 10:12:27 +1030 | [diff] [blame] | 472 | err = cpumask_parse_user(buffer, count, new_value); |
| 473 | if (!err) { |
Alexey Dobriyan | 583a22e | 2009-09-18 12:57:09 -0700 | [diff] [blame] | 474 | cpumask_copy(prof_cpu_mask, new_value); |
| 475 | err = count; |
Rusty Russell | c309b91 | 2009-01-01 10:12:27 +1030 | [diff] [blame] | 476 | } |
| 477 | free_cpumask_var(new_value); |
| 478 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Alexey Dobriyan | 583a22e | 2009-09-18 12:57:09 -0700 | [diff] [blame] | 481 | static const struct file_operations prof_cpu_mask_proc_fops = { |
| 482 | .open = prof_cpu_mask_proc_open, |
| 483 | .read = seq_read, |
| 484 | .llseek = seq_lseek, |
| 485 | .release = single_release, |
| 486 | .write = prof_cpu_mask_proc_write, |
| 487 | }; |
| 488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir) |
| 490 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | /* create /proc/irq/prof_cpu_mask */ |
Alexey Dobriyan | 583a22e | 2009-09-18 12:57:09 -0700 | [diff] [blame] | 492 | proc_create("prof_cpu_mask", 0600, root_irq_dir, &prof_cpu_mask_proc_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* |
| 496 | * This function accesses profiling information. The returned data is |
| 497 | * binary: the sampling step and the actual contents of the profile |
| 498 | * buffer. Use of the program readprofile is recommended in order to |
| 499 | * get meaningful info out of these data. |
| 500 | */ |
| 501 | static ssize_t |
| 502 | read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 503 | { |
| 504 | unsigned long p = *ppos; |
| 505 | ssize_t read; |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 506 | char *pnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | unsigned int sample_step = 1 << prof_shift; |
| 508 | |
| 509 | profile_flip_buffers(); |
| 510 | if (p >= (prof_len+1)*sizeof(unsigned int)) |
| 511 | return 0; |
| 512 | if (count > (prof_len+1)*sizeof(unsigned int) - p) |
| 513 | count = (prof_len+1)*sizeof(unsigned int) - p; |
| 514 | read = 0; |
| 515 | |
| 516 | while (p < sizeof(unsigned int) && count > 0) { |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 517 | if (put_user(*((char *)(&sample_step)+p), buf)) |
Heiko Carstens | 064b022 | 2006-12-06 20:36:37 -0800 | [diff] [blame] | 518 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | buf++; p++; count--; read++; |
| 520 | } |
| 521 | pnt = (char *)prof_buffer + p - sizeof(atomic_t); |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 522 | if (copy_to_user(buf, (void *)pnt, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | return -EFAULT; |
| 524 | read += count; |
| 525 | *ppos += read; |
| 526 | return read; |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * Writing to /proc/profile resets the counters |
| 531 | * |
| 532 | * Writing a 'profiling multiplier' value into it also re-sets the profiling |
| 533 | * interrupt frequency, on architectures that support this. |
| 534 | */ |
| 535 | static ssize_t write_profile(struct file *file, const char __user *buf, |
| 536 | size_t count, loff_t *ppos) |
| 537 | { |
| 538 | #ifdef CONFIG_SMP |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 539 | extern int setup_profiling_timer(unsigned int multiplier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
| 541 | if (count == sizeof(int)) { |
| 542 | unsigned int multiplier; |
| 543 | |
| 544 | if (copy_from_user(&multiplier, buf, sizeof(int))) |
| 545 | return -EFAULT; |
| 546 | |
| 547 | if (setup_profiling_timer(multiplier)) |
| 548 | return -EINVAL; |
| 549 | } |
| 550 | #endif |
| 551 | profile_discard_flip_buffers(); |
| 552 | memset(prof_buffer, 0, prof_len * sizeof(atomic_t)); |
| 553 | return count; |
| 554 | } |
| 555 | |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 556 | static const struct file_operations proc_profile_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | .read = read_profile, |
| 558 | .write = write_profile, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 559 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | }; |
| 561 | |
| 562 | #ifdef CONFIG_SMP |
Andrew Morton | 60a5151 | 2008-11-18 22:20:10 -0800 | [diff] [blame] | 563 | static void profile_nop(void *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
| 565 | } |
| 566 | |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 567 | static int create_hash_tables(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | { |
| 569 | int cpu; |
| 570 | |
| 571 | for_each_online_cpu(cpu) { |
Lee Schermerhorn | 3dd6b5f | 2010-05-26 14:45:04 -0700 | [diff] [blame] | 572 | int node = cpu_to_mem(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | struct page *page; |
| 574 | |
Mel Gorman | 6484eb3 | 2009-06-16 15:31:54 -0700 | [diff] [blame] | 575 | page = alloc_pages_exact_node(node, |
Christoph Lameter | fbd9816 | 2006-09-25 23:31:45 -0700 | [diff] [blame] | 576 | GFP_KERNEL | __GFP_ZERO | GFP_THISNODE, |
| 577 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | if (!page) |
| 579 | goto out_cleanup; |
| 580 | per_cpu(cpu_profile_hits, cpu)[1] |
| 581 | = (struct profile_hit *)page_address(page); |
Mel Gorman | 6484eb3 | 2009-06-16 15:31:54 -0700 | [diff] [blame] | 582 | page = alloc_pages_exact_node(node, |
Christoph Lameter | fbd9816 | 2006-09-25 23:31:45 -0700 | [diff] [blame] | 583 | GFP_KERNEL | __GFP_ZERO | GFP_THISNODE, |
| 584 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (!page) |
| 586 | goto out_cleanup; |
| 587 | per_cpu(cpu_profile_hits, cpu)[0] |
| 588 | = (struct profile_hit *)page_address(page); |
| 589 | } |
| 590 | return 0; |
| 591 | out_cleanup: |
| 592 | prof_on = 0; |
akpm@osdl.org | d59dd46 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 593 | smp_mb(); |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 594 | on_each_cpu(profile_nop, NULL, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | for_each_online_cpu(cpu) { |
| 596 | struct page *page; |
| 597 | |
| 598 | if (per_cpu(cpu_profile_hits, cpu)[0]) { |
| 599 | page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]); |
| 600 | per_cpu(cpu_profile_hits, cpu)[0] = NULL; |
| 601 | __free_page(page); |
| 602 | } |
| 603 | if (per_cpu(cpu_profile_hits, cpu)[1]) { |
| 604 | page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]); |
| 605 | per_cpu(cpu_profile_hits, cpu)[1] = NULL; |
| 606 | __free_page(page); |
| 607 | } |
| 608 | } |
| 609 | return -1; |
| 610 | } |
| 611 | #else |
| 612 | #define create_hash_tables() ({ 0; }) |
| 613 | #endif |
| 614 | |
Al Viro | 8419641 | 2008-11-22 17:36:44 +0000 | [diff] [blame] | 615 | int __ref create_proc_profile(void) /* false positive from hotcpu_notifier */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | { |
| 617 | struct proc_dir_entry *entry; |
| 618 | |
| 619 | if (!prof_on) |
| 620 | return 0; |
| 621 | if (create_hash_tables()) |
Dave Hansen | 22b8ce9 | 2008-10-15 22:01:46 -0700 | [diff] [blame] | 622 | return -ENOMEM; |
Denis V. Lunev | c33fff0 | 2008-04-29 01:02:31 -0700 | [diff] [blame] | 623 | entry = proc_create("profile", S_IWUSR | S_IRUGO, |
| 624 | NULL, &proc_profile_operations); |
Paolo Ciarrocchi | 1ad82fd | 2008-01-25 21:08:33 +0100 | [diff] [blame] | 625 | if (!entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | entry->size = (1+prof_len) * sizeof(atomic_t); |
| 628 | hotcpu_notifier(profile_cpu_callback, 0); |
| 629 | return 0; |
| 630 | } |
| 631 | module_init(create_proc_profile); |
| 632 | #endif /* CONFIG_PROC_FS */ |