blob: 9c08a2c7cb1d3567d2162128c51d52db00f9eaa5 [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,
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010011 * Nadia Yvette Chambers, Oracle, July 2004
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Amortized hit count accounting via per-cpu open-addressed hashtables
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * to resolve timer interrupt livelocks, Nadia Yvette Chambers,
14 * Oracle, 2004
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
Paul Gortmaker9984de12011-05-23 14:51:41 -040017#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/profile.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070019#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/notifier.h>
21#include <linux/mm.h>
22#include <linux/cpumask.h>
23#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/highmem.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080025#include <linux/mutex.h>
Dave Hansen22b8ce92008-10-15 22:01:46 -070026#include <linux/slab.h>
27#include <linux/vmalloc.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010028#include <linux/sched/stat.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/sections.h>
David Howells7d12e782006-10-05 14:55:46 +010031#include <asm/irq_regs.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040032#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34struct profile_hit {
35 u32 pc, hits;
36};
37#define PROFILE_GRPSHIFT 3
38#define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT)
39#define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit))
40#define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ)
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static atomic_t *prof_buffer;
43static unsigned long prof_len, prof_shift;
Ingo Molnar07031e12007-01-10 23:15:38 -080044
Ingo Molnarece8a682006-12-06 20:37:24 -080045int prof_on __read_mostly;
Ingo Molnar07031e12007-01-10 23:15:38 -080046EXPORT_SYMBOL_GPL(prof_on);
47
Rusty Russellc309b912009-01-01 10:12:27 +103048static cpumask_var_t prof_cpu_mask;
Arnd Bergmannade356b2016-03-22 14:27:26 -070049#if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
51static DEFINE_PER_CPU(int, cpu_profile_flip);
Arjan van de Ven97d1f152006-03-23 03:00:24 -080052static DEFINE_MUTEX(profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#endif /* CONFIG_SMP */
54
Dave Hansen22b8ce92008-10-15 22:01:46 -070055int profile_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Fabian Frederickf3da64d2014-06-06 14:37:30 -070057 static const char schedstr[] = "schedule";
58 static const char sleepstr[] = "sleep";
59 static const char kvmstr[] = "kvm";
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 int par;
61
Ingo Molnarece8a682006-12-06 20:37:24 -080062 if (!strncmp(str, sleepstr, strlen(sleepstr))) {
Mel Gormanb3da2a72007-10-24 18:23:50 +020063#ifdef CONFIG_SCHEDSTATS
Mel Gormancb251762016-02-05 09:08:36 +000064 force_schedstat_enabled();
Ingo Molnarece8a682006-12-06 20:37:24 -080065 prof_on = SLEEP_PROFILING;
66 if (str[strlen(sleepstr)] == ',')
67 str += strlen(sleepstr) + 1;
68 if (get_option(&str, &par))
69 prof_shift = par;
Fabian Frederickaba871f2014-06-06 14:37:29 -070070 pr_info("kernel sleep profiling enabled (shift: %ld)\n",
Ingo Molnarece8a682006-12-06 20:37:24 -080071 prof_shift);
Mel Gormanb3da2a72007-10-24 18:23:50 +020072#else
Fabian Frederickaba871f2014-06-06 14:37:29 -070073 pr_warn("kernel sleep profiling requires CONFIG_SCHEDSTATS\n");
Mel Gormanb3da2a72007-10-24 18:23:50 +020074#endif /* CONFIG_SCHEDSTATS */
Ingo Molnara75acf82007-01-05 16:36:29 -080075 } else if (!strncmp(str, schedstr, strlen(schedstr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 prof_on = SCHED_PROFILING;
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070077 if (str[strlen(schedstr)] == ',')
78 str += strlen(schedstr) + 1;
79 if (get_option(&str, &par))
80 prof_shift = par;
Fabian Frederickaba871f2014-06-06 14:37:29 -070081 pr_info("kernel schedule profiling enabled (shift: %ld)\n",
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070082 prof_shift);
Ingo Molnar07031e12007-01-10 23:15:38 -080083 } else if (!strncmp(str, kvmstr, strlen(kvmstr))) {
84 prof_on = KVM_PROFILING;
85 if (str[strlen(kvmstr)] == ',')
86 str += strlen(kvmstr) + 1;
87 if (get_option(&str, &par))
88 prof_shift = par;
Fabian Frederickaba871f2014-06-06 14:37:29 -070089 pr_info("kernel KVM profiling enabled (shift: %ld)\n",
Ingo Molnar07031e12007-01-10 23:15:38 -080090 prof_shift);
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070091 } else if (get_option(&str, &par)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 prof_shift = par;
93 prof_on = CPU_PROFILING;
Fabian Frederickaba871f2014-06-06 14:37:29 -070094 pr_info("kernel profiling enabled (shift: %ld)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 prof_shift);
96 }
97 return 1;
98}
99__setup("profile=", profile_setup);
100
101
Paul Mundtce05fcc2008-10-29 14:01:07 -0700102int __ref profile_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Dave Hansen22b8ce92008-10-15 22:01:46 -0700104 int buffer_bytes;
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100105 if (!prof_on)
Dave Hansen22b8ce92008-10-15 22:01:46 -0700106 return 0;
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* only text is profiled */
109 prof_len = (_etext - _stext) >> prof_shift;
Dave Hansen22b8ce92008-10-15 22:01:46 -0700110 buffer_bytes = prof_len*sizeof(atomic_t);
Dave Hansen22b8ce92008-10-15 22:01:46 -0700111
Rusty Russellc309b912009-01-01 10:12:27 +1030112 if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL))
113 return -ENOMEM;
114
Hugh Dickinsacd89572009-02-09 19:20:50 +0000115 cpumask_copy(prof_cpu_mask, cpu_possible_mask);
116
Mel Gormanb62f4952009-07-29 15:04:09 -0700117 prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN);
Dave Hansen22b8ce92008-10-15 22:01:46 -0700118 if (prof_buffer)
119 return 0;
120
Mel Gormanb62f4952009-07-29 15:04:09 -0700121 prof_buffer = alloc_pages_exact(buffer_bytes,
122 GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN);
Dave Hansen22b8ce92008-10-15 22:01:46 -0700123 if (prof_buffer)
124 return 0;
125
Jesper Juhl559fa6e2010-10-30 21:56:26 +0200126 prof_buffer = vzalloc(buffer_bytes);
127 if (prof_buffer)
Dave Hansen22b8ce92008-10-15 22:01:46 -0700128 return 0;
129
Rusty Russellc309b912009-01-01 10:12:27 +1030130 free_cpumask_var(prof_cpu_mask);
Dave Hansen22b8ce92008-10-15 22:01:46 -0700131 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134/* Profile event notifications */
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100135
Alan Sterne041c682006-03-27 01:16:30 -0800136static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
137static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
138static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100139
140void profile_task_exit(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Alan Sterne041c682006-03-27 01:16:30 -0800142 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100144
145int profile_handoff_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 int ret;
Alan Sterne041c682006-03-27 01:16:30 -0800148 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return (ret == NOTIFY_OK) ? 1 : 0;
150}
151
152void profile_munmap(unsigned long addr)
153{
Alan Sterne041c682006-03-27 01:16:30 -0800154 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100157int task_handoff_register(struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Alan Sterne041c682006-03-27 01:16:30 -0800159 return atomic_notifier_chain_register(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100161EXPORT_SYMBOL_GPL(task_handoff_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100163int task_handoff_unregister(struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Alan Sterne041c682006-03-27 01:16:30 -0800165 return atomic_notifier_chain_unregister(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100167EXPORT_SYMBOL_GPL(task_handoff_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100169int profile_event_register(enum profile_type type, struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 switch (type) {
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100174 case PROFILE_TASK_EXIT:
175 err = blocking_notifier_chain_register(
176 &task_exit_notifier, n);
177 break;
178 case PROFILE_MUNMAP:
179 err = blocking_notifier_chain_register(
180 &munmap_notifier, n);
181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return err;
185}
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100186EXPORT_SYMBOL_GPL(profile_event_register);
187
188int profile_event_unregister(enum profile_type type, struct notifier_block *n)
189{
190 int err = -EINVAL;
191
192 switch (type) {
193 case PROFILE_TASK_EXIT:
194 err = blocking_notifier_chain_unregister(
195 &task_exit_notifier, n);
196 break;
197 case PROFILE_MUNMAP:
198 err = blocking_notifier_chain_unregister(
199 &munmap_notifier, n);
200 break;
201 }
202
203 return err;
204}
205EXPORT_SYMBOL_GPL(profile_event_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Arnd Bergmannade356b2016-03-22 14:27:26 -0700207#if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/*
209 * Each cpu has a pair of open-addressed hashtables for pending
210 * profile hits. read_profile() IPI's all cpus to request them
211 * to flip buffers and flushes their contents to prof_buffer itself.
212 * Flip requests are serialized by the profile_flip_mutex. The sole
213 * use of having a second hashtable is for avoiding cacheline
214 * contention that would otherwise happen during flushes of pending
215 * profile hits required for the accuracy of reported profile hits
216 * and so resurrect the interrupt livelock issue.
217 *
218 * The open-addressed hashtables are indexed by profile buffer slot
219 * and hold the number of pending hits to that profile buffer slot on
220 * a cpu in an entry. When the hashtable overflows, all pending hits
221 * are accounted to their corresponding profile buffer slots with
222 * atomic_add() and the hashtable emptied. As numerous pending hits
223 * may be accounted to a profile buffer slot in a hashtable entry,
224 * this amortizes a number of atomic profile buffer increments likely
225 * to be far larger than the number of entries in the hashtable,
226 * particularly given that the number of distinct profile buffer
227 * positions to which hits are accounted during short intervals (e.g.
228 * several seconds) is usually very small. Exclusion from buffer
229 * flipping is provided by interrupt disablement (note that for
Ingo Molnarece8a682006-12-06 20:37:24 -0800230 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
231 * process context).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * The hash function is meant to be lightweight as opposed to strong,
233 * and was vaguely inspired by ppc64 firmware-supported inverted
234 * pagetable hash functions, but uses a full hashtable full of finite
235 * collision chains, not just pairs of them.
236 *
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +0100237 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
239static void __profile_flip_buffers(void *unused)
240{
241 int cpu = smp_processor_id();
242
243 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
244}
245
246static void profile_flip_buffers(void)
247{
248 int i, j, cpu;
249
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800250 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 j = per_cpu(cpu_profile_flip, get_cpu());
252 put_cpu();
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200253 on_each_cpu(__profile_flip_buffers, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 for_each_online_cpu(cpu) {
255 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
256 for (i = 0; i < NR_PROFILE_HIT; ++i) {
257 if (!hits[i].hits) {
258 if (hits[i].pc)
259 hits[i].pc = 0;
260 continue;
261 }
262 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
263 hits[i].hits = hits[i].pc = 0;
264 }
265 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800266 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269static void profile_discard_flip_buffers(void)
270{
271 int i, cpu;
272
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800273 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 i = per_cpu(cpu_profile_flip, get_cpu());
275 put_cpu();
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200276 on_each_cpu(__profile_flip_buffers, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 for_each_online_cpu(cpu) {
278 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
279 memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
280 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800281 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Rakib Mullick6f7bd762011-05-26 16:26:00 -0700284static void do_profile_hits(int type, void *__pc, unsigned int nr_hits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
287 int i, j, cpu;
288 struct profile_hit *hits;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
291 i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
292 secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
293 cpu = get_cpu();
294 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
295 if (!hits) {
296 put_cpu();
297 return;
298 }
Ingo Molnarece8a682006-12-06 20:37:24 -0800299 /*
300 * We buffer the global profiler buffer into a per-CPU
301 * queue and thus reduce the number of global (and possibly
302 * NUMA-alien) accesses. The write-queue is self-coalescing:
303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 local_irq_save(flags);
305 do {
306 for (j = 0; j < PROFILE_GRPSZ; ++j) {
307 if (hits[i + j].pc == pc) {
Ingo Molnarece8a682006-12-06 20:37:24 -0800308 hits[i + j].hits += nr_hits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto out;
310 } else if (!hits[i + j].hits) {
311 hits[i + j].pc = pc;
Ingo Molnarece8a682006-12-06 20:37:24 -0800312 hits[i + j].hits = nr_hits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 goto out;
314 }
315 }
316 i = (i + secondary) & (NR_PROFILE_HIT - 1);
317 } while (i != primary);
Ingo Molnarece8a682006-12-06 20:37:24 -0800318
319 /*
320 * Add the current hit(s) and flush the write-queue out
321 * to the global buffer:
322 */
323 atomic_add(nr_hits, &prof_buffer[pc]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 for (i = 0; i < NR_PROFILE_HIT; ++i) {
325 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
326 hits[i].pc = hits[i].hits = 0;
327 }
328out:
329 local_irq_restore(flags);
330 put_cpu();
331}
332
Sebastian Andrzej Siewiore722d8d2016-07-13 17:16:59 +0000333static int profile_dead_cpu(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Sebastian Andrzej Siewiore722d8d2016-07-13 17:16:59 +0000335 struct page *page;
336 int i;
337
338 if (prof_cpu_mask != NULL)
339 cpumask_clear_cpu(cpu, prof_cpu_mask);
340
341 for (i = 0; i < 2; i++) {
342 if (per_cpu(cpu_profile_hits, cpu)[i]) {
343 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[i]);
344 per_cpu(cpu_profile_hits, cpu)[i] = NULL;
345 __free_page(page);
346 }
347 }
348 return 0;
349}
350
351static int profile_prepare_cpu(unsigned int cpu)
352{
353 int i, node = cpu_to_mem(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct page *page;
355
Sebastian Andrzej Siewiore722d8d2016-07-13 17:16:59 +0000356 per_cpu(cpu_profile_flip, cpu) = 0;
357
358 for (i = 0; i < 2; i++) {
359 if (per_cpu(cpu_profile_hits, cpu)[i])
360 continue;
361
362 page = __alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
363 if (!page) {
364 profile_dead_cpu(cpu);
365 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Sebastian Andrzej Siewiore722d8d2016-07-13 17:16:59 +0000367 per_cpu(cpu_profile_hits, cpu)[i] = page_address(page);
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Sebastian Andrzej Siewiore722d8d2016-07-13 17:16:59 +0000370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
Sebastian Andrzej Siewiore722d8d2016-07-13 17:16:59 +0000372
373static int profile_online_cpu(unsigned int cpu)
374{
375 if (prof_cpu_mask != NULL)
376 cpumask_set_cpu(cpu, prof_cpu_mask);
377
378 return 0;
379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#else /* !CONFIG_SMP */
382#define profile_flip_buffers() do { } while (0)
383#define profile_discard_flip_buffers() do { } while (0)
384
Rakib Mullick6f7bd762011-05-26 16:26:00 -0700385static void do_profile_hits(int type, void *__pc, unsigned int nr_hits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 unsigned long pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
Ingo Molnarece8a682006-12-06 20:37:24 -0800389 atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391#endif /* !CONFIG_SMP */
Rakib Mullick6f7bd762011-05-26 16:26:00 -0700392
393void profile_hits(int type, void *__pc, unsigned int nr_hits)
394{
395 if (prof_on != type || !prof_buffer)
396 return;
397 do_profile_hits(type, __pc, nr_hits);
398}
Andrew Mortonbbe1a59b2007-01-22 20:40:33 -0800399EXPORT_SYMBOL_GPL(profile_hits);
400
David Howells7d12e782006-10-05 14:55:46 +0100401void profile_tick(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
David Howells7d12e782006-10-05 14:55:46 +0100403 struct pt_regs *regs = get_irq_regs();
404
Rusty Russellc309b912009-01-01 10:12:27 +1030405 if (!user_mode(regs) && prof_cpu_mask != NULL &&
406 cpumask_test_cpu(smp_processor_id(), prof_cpu_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 profile_hit(type, (void *)profile_pc(regs));
408}
409
410#ifdef CONFIG_PROC_FS
411#include <linux/proc_fs.h>
Alexey Dobriyan583a22e2009-09-18 12:57:09 -0700412#include <linux/seq_file.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -0800413#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Alexey Dobriyan583a22e2009-09-18 12:57:09 -0700415static int prof_cpu_mask_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Tejun Heoccbd59c2015-02-13 14:38:13 -0800417 seq_printf(m, "%*pb\n", cpumask_pr_args(prof_cpu_mask));
Alexey Dobriyan583a22e2009-09-18 12:57:09 -0700418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Alexey Dobriyan583a22e2009-09-18 12:57:09 -0700421static int prof_cpu_mask_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Alexey Dobriyan583a22e2009-09-18 12:57:09 -0700423 return single_open(file, prof_cpu_mask_proc_show, NULL);
424}
425
426static ssize_t prof_cpu_mask_proc_write(struct file *file,
427 const char __user *buffer, size_t count, loff_t *pos)
428{
Rusty Russellc309b912009-01-01 10:12:27 +1030429 cpumask_var_t new_value;
Alexey Dobriyan583a22e2009-09-18 12:57:09 -0700430 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Rusty Russellc309b912009-01-01 10:12:27 +1030432 if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
433 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Rusty Russellc309b912009-01-01 10:12:27 +1030435 err = cpumask_parse_user(buffer, count, new_value);
436 if (!err) {
Alexey Dobriyan583a22e2009-09-18 12:57:09 -0700437 cpumask_copy(prof_cpu_mask, new_value);
438 err = count;
Rusty Russellc309b912009-01-01 10:12:27 +1030439 }
440 free_cpumask_var(new_value);
441 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Alexey Dobriyan583a22e2009-09-18 12:57:09 -0700444static const struct file_operations prof_cpu_mask_proc_fops = {
445 .open = prof_cpu_mask_proc_open,
446 .read = seq_read,
447 .llseek = seq_lseek,
448 .release = single_release,
449 .write = prof_cpu_mask_proc_write,
450};
451
Al Virofbd387a2013-04-01 20:48:34 -0400452void create_prof_cpu_mask(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* create /proc/irq/prof_cpu_mask */
Al Virofbd387a2013-04-01 20:48:34 -0400455 proc_create("irq/prof_cpu_mask", 0600, NULL, &prof_cpu_mask_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458/*
459 * This function accesses profiling information. The returned data is
460 * binary: the sampling step and the actual contents of the profile
461 * buffer. Use of the program readprofile is recommended in order to
462 * get meaningful info out of these data.
463 */
464static ssize_t
465read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
466{
467 unsigned long p = *ppos;
468 ssize_t read;
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100469 char *pnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 unsigned int sample_step = 1 << prof_shift;
471
472 profile_flip_buffers();
473 if (p >= (prof_len+1)*sizeof(unsigned int))
474 return 0;
475 if (count > (prof_len+1)*sizeof(unsigned int) - p)
476 count = (prof_len+1)*sizeof(unsigned int) - p;
477 read = 0;
478
479 while (p < sizeof(unsigned int) && count > 0) {
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100480 if (put_user(*((char *)(&sample_step)+p), buf))
Heiko Carstens064b0222006-12-06 20:36:37 -0800481 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 buf++; p++; count--; read++;
483 }
484 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100485 if (copy_to_user(buf, (void *)pnt, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -EFAULT;
487 read += count;
488 *ppos += read;
489 return read;
490}
491
492/*
493 * Writing to /proc/profile resets the counters
494 *
495 * Writing a 'profiling multiplier' value into it also re-sets the profiling
496 * interrupt frequency, on architectures that support this.
497 */
498static ssize_t write_profile(struct file *file, const char __user *buf,
499 size_t count, loff_t *ppos)
500{
501#ifdef CONFIG_SMP
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100502 extern int setup_profiling_timer(unsigned int multiplier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (count == sizeof(int)) {
505 unsigned int multiplier;
506
507 if (copy_from_user(&multiplier, buf, sizeof(int)))
508 return -EFAULT;
509
510 if (setup_profiling_timer(multiplier))
511 return -EINVAL;
512 }
513#endif
514 profile_discard_flip_buffers();
515 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
516 return count;
517}
518
Helge Deller15ad7cd2006-12-06 20:40:36 -0800519static const struct file_operations proc_profile_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 .read = read_profile,
521 .write = write_profile,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200522 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523};
524
Sebastian Andrzej Siewiore722d8d2016-07-13 17:16:59 +0000525int __ref create_proc_profile(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 struct proc_dir_entry *entry;
Sebastian Andrzej Siewiore722d8d2016-07-13 17:16:59 +0000528#ifdef CONFIG_SMP
529 enum cpuhp_state online_state;
530#endif
531
Srivatsa S. Bhatc270a812014-03-11 02:12:08 +0530532 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 if (!prof_on)
535 return 0;
Sebastian Andrzej Siewiore722d8d2016-07-13 17:16:59 +0000536#ifdef CONFIG_SMP
537 err = cpuhp_setup_state(CPUHP_PROFILE_PREPARE, "PROFILE_PREPARE",
538 profile_prepare_cpu, profile_dead_cpu);
539 if (err)
540 return err;
Srivatsa S. Bhatc270a812014-03-11 02:12:08 +0530541
Sebastian Andrzej Siewiore722d8d2016-07-13 17:16:59 +0000542 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "AP_PROFILE_ONLINE",
543 profile_online_cpu, NULL);
544 if (err < 0)
545 goto err_state_prep;
546 online_state = err;
547 err = 0;
548#endif
Denis V. Lunevc33fff02008-04-29 01:02:31 -0700549 entry = proc_create("profile", S_IWUSR | S_IRUGO,
550 NULL, &proc_profile_operations);
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100551 if (!entry)
Sebastian Andrzej Siewiore722d8d2016-07-13 17:16:59 +0000552 goto err_state_onl;
David Howells271a15e2013-04-12 00:38:51 +0100553 proc_set_size(entry, (1 + prof_len) * sizeof(atomic_t));
Srivatsa S. Bhatc270a812014-03-11 02:12:08 +0530554
Sebastian Andrzej Siewiore722d8d2016-07-13 17:16:59 +0000555 return err;
556err_state_onl:
557#ifdef CONFIG_SMP
558 cpuhp_remove_state(online_state);
559err_state_prep:
560 cpuhp_remove_state(CPUHP_PROFILE_PREPARE);
561#endif
Srivatsa S. Bhatc270a812014-03-11 02:12:08 +0530562 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
Paul Gortmakerc96d6662014-04-03 14:48:35 -0700564subsys_initcall(create_proc_profile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565#endif /* CONFIG_PROC_FS */