profile.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/kernel/profile.c
  4. * Simple profiling. Manages a direct-mapped profile hit count buffer,
  5. * with configurable resolution, support for restricting the cpus on
  6. * which profiling is done, and switching between cpu time and
  7. * schedule() calls via kernel command line parameters passed at boot.
  8. *
  9. * Scheduler profiling support, Arjan van de Ven and Ingo Molnar,
  10. * Red Hat, July 2004
  11. * Consolidation of architecture support code for profiling,
  12. * Nadia Yvette Chambers, Oracle, July 2004
  13. * Amortized hit count accounting via per-cpu open-addressed hashtables
  14. * to resolve timer interrupt livelocks, Nadia Yvette Chambers,
  15. * Oracle, 2004
  16. */
  17. #include <linux/export.h>
  18. #include <linux/profile.h>
  19. #include <linux/memblock.h>
  20. #include <linux/notifier.h>
  21. #include <linux/mm.h>
  22. #include <linux/cpumask.h>
  23. #include <linux/cpu.h>
  24. #include <linux/highmem.h>
  25. #include <linux/mutex.h>
  26. #include <linux/slab.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/sched/stat.h>
  29. #include <asm/sections.h>
  30. #include <asm/irq_regs.h>
  31. #include <asm/ptrace.h>
  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. static atomic_t *prof_buffer;
  40. static unsigned long prof_len;
  41. static unsigned short int prof_shift;
  42. int prof_on __read_mostly;
  43. EXPORT_SYMBOL_GPL(prof_on);
  44. static cpumask_var_t prof_cpu_mask;
  45. #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
  46. static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
  47. static DEFINE_PER_CPU(int, cpu_profile_flip);
  48. static DEFINE_MUTEX(profile_flip_mutex);
  49. #endif /* CONFIG_SMP */
  50. int profile_setup(char *str)
  51. {
  52. static const char schedstr[] = "schedule";
  53. static const char sleepstr[] = "sleep";
  54. static const char kvmstr[] = "kvm";
  55. int par;
  56. if (!strncmp(str, sleepstr, strlen(sleepstr))) {
  57. #ifdef CONFIG_SCHEDSTATS
  58. force_schedstat_enabled();
  59. prof_on = SLEEP_PROFILING;
  60. if (str[strlen(sleepstr)] == ',')
  61. str += strlen(sleepstr) + 1;
  62. if (get_option(&str, &par))
  63. prof_shift = clamp(par, 0, BITS_PER_LONG - 1);
  64. pr_info("kernel sleep profiling enabled (shift: %u)\n",
  65. prof_shift);
  66. #else
  67. pr_warn("kernel sleep profiling requires CONFIG_SCHEDSTATS\n");
  68. #endif /* CONFIG_SCHEDSTATS */
  69. } else if (!strncmp(str, schedstr, strlen(schedstr))) {
  70. prof_on = SCHED_PROFILING;
  71. if (str[strlen(schedstr)] == ',')
  72. str += strlen(schedstr) + 1;
  73. if (get_option(&str, &par))
  74. prof_shift = clamp(par, 0, BITS_PER_LONG - 1);
  75. pr_info("kernel schedule profiling enabled (shift: %u)\n",
  76. prof_shift);
  77. } else if (!strncmp(str, kvmstr, strlen(kvmstr))) {
  78. prof_on = KVM_PROFILING;
  79. if (str[strlen(kvmstr)] == ',')
  80. str += strlen(kvmstr) + 1;
  81. if (get_option(&str, &par))
  82. prof_shift = clamp(par, 0, BITS_PER_LONG - 1);
  83. pr_info("kernel KVM profiling enabled (shift: %u)\n",
  84. prof_shift);
  85. } else if (get_option(&str, &par)) {
  86. prof_shift = clamp(par, 0, BITS_PER_LONG - 1);
  87. prof_on = CPU_PROFILING;
  88. pr_info("kernel profiling enabled (shift: %u)\n",
  89. prof_shift);
  90. }
  91. return 1;
  92. }
  93. __setup("profile=", profile_setup);
  94. int __ref profile_init(void)
  95. {
  96. int buffer_bytes;
  97. if (!prof_on)
  98. return 0;
  99. /* only text is profiled */
  100. prof_len = (_etext - _stext) >> prof_shift;
  101. if (!prof_len) {
  102. pr_warn("profiling shift: %u too large\n", prof_shift);
  103. prof_on = 0;
  104. return -EINVAL;
  105. }
  106. buffer_bytes = prof_len*sizeof(atomic_t);
  107. if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL))
  108. return -ENOMEM;
  109. cpumask_copy(prof_cpu_mask, cpu_possible_mask);
  110. prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN);
  111. if (prof_buffer)
  112. return 0;
  113. prof_buffer = alloc_pages_exact(buffer_bytes,
  114. GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN);
  115. if (prof_buffer)
  116. return 0;
  117. prof_buffer = vzalloc(buffer_bytes);
  118. if (prof_buffer)
  119. return 0;
  120. free_cpumask_var(prof_cpu_mask);
  121. return -ENOMEM;
  122. }
  123. /* Profile event notifications */
  124. static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
  125. static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
  126. static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
  127. void profile_task_exit(struct task_struct *task)
  128. {
  129. blocking_notifier_call_chain(&task_exit_notifier, 0, task);
  130. }
  131. int profile_handoff_task(struct task_struct *task)
  132. {
  133. int ret;
  134. ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
  135. return (ret == NOTIFY_OK) ? 1 : 0;
  136. }
  137. void profile_munmap(unsigned long addr)
  138. {
  139. blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
  140. }
  141. int task_handoff_register(struct notifier_block *n)
  142. {
  143. return atomic_notifier_chain_register(&task_free_notifier, n);
  144. }
  145. EXPORT_SYMBOL_GPL(task_handoff_register);
  146. int task_handoff_unregister(struct notifier_block *n)
  147. {
  148. return atomic_notifier_chain_unregister(&task_free_notifier, n);
  149. }
  150. EXPORT_SYMBOL_GPL(task_handoff_unregister);
  151. int profile_event_register(enum profile_type type, struct notifier_block *n)
  152. {
  153. int err = -EINVAL;
  154. switch (type) {
  155. case PROFILE_TASK_EXIT:
  156. err = blocking_notifier_chain_register(
  157. &task_exit_notifier, n);
  158. break;
  159. case PROFILE_MUNMAP:
  160. err = blocking_notifier_chain_register(
  161. &munmap_notifier, n);
  162. break;
  163. }
  164. return err;
  165. }
  166. EXPORT_SYMBOL_GPL(profile_event_register);
  167. int profile_event_unregister(enum profile_type type, struct notifier_block *n)
  168. {
  169. int err = -EINVAL;
  170. switch (type) {
  171. case PROFILE_TASK_EXIT:
  172. err = blocking_notifier_chain_unregister(
  173. &task_exit_notifier, n);
  174. break;
  175. case PROFILE_MUNMAP:
  176. err = blocking_notifier_chain_unregister(
  177. &munmap_notifier, n);
  178. break;
  179. }
  180. return err;
  181. }
  182. EXPORT_SYMBOL_GPL(profile_event_unregister);
  183. #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
  184. /*
  185. * Each cpu has a pair of open-addressed hashtables for pending
  186. * profile hits. read_profile() IPI's all cpus to request them
  187. * to flip buffers and flushes their contents to prof_buffer itself.
  188. * Flip requests are serialized by the profile_flip_mutex. The sole
  189. * use of having a second hashtable is for avoiding cacheline
  190. * contention that would otherwise happen during flushes of pending
  191. * profile hits required for the accuracy of reported profile hits
  192. * and so resurrect the interrupt livelock issue.
  193. *
  194. * The open-addressed hashtables are indexed by profile buffer slot
  195. * and hold the number of pending hits to that profile buffer slot on
  196. * a cpu in an entry. When the hashtable overflows, all pending hits
  197. * are accounted to their corresponding profile buffer slots with
  198. * atomic_add() and the hashtable emptied. As numerous pending hits
  199. * may be accounted to a profile buffer slot in a hashtable entry,
  200. * this amortizes a number of atomic profile buffer increments likely
  201. * to be far larger than the number of entries in the hashtable,
  202. * particularly given that the number of distinct profile buffer
  203. * positions to which hits are accounted during short intervals (e.g.
  204. * several seconds) is usually very small. Exclusion from buffer
  205. * flipping is provided by interrupt disablement (note that for
  206. * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
  207. * process context).
  208. * The hash function is meant to be lightweight as opposed to strong,
  209. * and was vaguely inspired by ppc64 firmware-supported inverted
  210. * pagetable hash functions, but uses a full hashtable full of finite
  211. * collision chains, not just pairs of them.
  212. *
  213. * -- nyc
  214. */
  215. static void __profile_flip_buffers(void *unused)
  216. {
  217. int cpu = smp_processor_id();
  218. per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
  219. }
  220. static void profile_flip_buffers(void)
  221. {
  222. int i, j, cpu;
  223. mutex_lock(&profile_flip_mutex);
  224. j = per_cpu(cpu_profile_flip, get_cpu());
  225. put_cpu();
  226. on_each_cpu(__profile_flip_buffers, NULL, 1);
  227. for_each_online_cpu(cpu) {
  228. struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
  229. for (i = 0; i < NR_PROFILE_HIT; ++i) {
  230. if (!hits[i].hits) {
  231. if (hits[i].pc)
  232. hits[i].pc = 0;
  233. continue;
  234. }
  235. atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
  236. hits[i].hits = hits[i].pc = 0;
  237. }
  238. }
  239. mutex_unlock(&profile_flip_mutex);
  240. }
  241. static void profile_discard_flip_buffers(void)
  242. {
  243. int i, cpu;
  244. mutex_lock(&profile_flip_mutex);
  245. i = per_cpu(cpu_profile_flip, get_cpu());
  246. put_cpu();
  247. on_each_cpu(__profile_flip_buffers, NULL, 1);
  248. for_each_online_cpu(cpu) {
  249. struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
  250. memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
  251. }
  252. mutex_unlock(&profile_flip_mutex);
  253. }
  254. static void do_profile_hits(int type, void *__pc, unsigned int nr_hits)
  255. {
  256. unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
  257. int i, j, cpu;
  258. struct profile_hit *hits;
  259. pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
  260. i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
  261. secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
  262. cpu = get_cpu();
  263. hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
  264. if (!hits) {
  265. put_cpu();
  266. return;
  267. }
  268. /*
  269. * We buffer the global profiler buffer into a per-CPU
  270. * queue and thus reduce the number of global (and possibly
  271. * NUMA-alien) accesses. The write-queue is self-coalescing:
  272. */
  273. local_irq_save(flags);
  274. do {
  275. for (j = 0; j < PROFILE_GRPSZ; ++j) {
  276. if (hits[i + j].pc == pc) {
  277. hits[i + j].hits += nr_hits;
  278. goto out;
  279. } else if (!hits[i + j].hits) {
  280. hits[i + j].pc = pc;
  281. hits[i + j].hits = nr_hits;
  282. goto out;
  283. }
  284. }
  285. i = (i + secondary) & (NR_PROFILE_HIT - 1);
  286. } while (i != primary);
  287. /*
  288. * Add the current hit(s) and flush the write-queue out
  289. * to the global buffer:
  290. */
  291. atomic_add(nr_hits, &prof_buffer[pc]);
  292. for (i = 0; i < NR_PROFILE_HIT; ++i) {
  293. atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
  294. hits[i].pc = hits[i].hits = 0;
  295. }
  296. out:
  297. local_irq_restore(flags);
  298. put_cpu();
  299. }
  300. static int profile_dead_cpu(unsigned int cpu)
  301. {
  302. struct page *page;
  303. int i;
  304. if (cpumask_available(prof_cpu_mask))
  305. cpumask_clear_cpu(cpu, prof_cpu_mask);
  306. for (i = 0; i < 2; i++) {
  307. if (per_cpu(cpu_profile_hits, cpu)[i]) {
  308. page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[i]);
  309. per_cpu(cpu_profile_hits, cpu)[i] = NULL;
  310. __free_page(page);
  311. }
  312. }
  313. return 0;
  314. }
  315. static int profile_prepare_cpu(unsigned int cpu)
  316. {
  317. int i, node = cpu_to_mem(cpu);
  318. struct page *page;
  319. per_cpu(cpu_profile_flip, cpu) = 0;
  320. for (i = 0; i < 2; i++) {
  321. if (per_cpu(cpu_profile_hits, cpu)[i])
  322. continue;
  323. page = __alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  324. if (!page) {
  325. profile_dead_cpu(cpu);
  326. return -ENOMEM;
  327. }
  328. per_cpu(cpu_profile_hits, cpu)[i] = page_address(page);
  329. }
  330. return 0;
  331. }
  332. static int profile_online_cpu(unsigned int cpu)
  333. {
  334. if (cpumask_available(prof_cpu_mask))
  335. cpumask_set_cpu(cpu, prof_cpu_mask);
  336. return 0;
  337. }
  338. #else /* !CONFIG_SMP */
  339. #define profile_flip_buffers() do { } while (0)
  340. #define profile_discard_flip_buffers() do { } while (0)
  341. static void do_profile_hits(int type, void *__pc, unsigned int nr_hits)
  342. {
  343. unsigned long pc;
  344. pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
  345. atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
  346. }
  347. #endif /* !CONFIG_SMP */
  348. void profile_hits(int type, void *__pc, unsigned int nr_hits)
  349. {
  350. if (prof_on != type || !prof_buffer)
  351. return;
  352. do_profile_hits(type, __pc, nr_hits);
  353. }
  354. EXPORT_SYMBOL_GPL(profile_hits);
  355. void profile_tick(int type)
  356. {
  357. struct pt_regs *regs = get_irq_regs();
  358. if (!user_mode(regs) && cpumask_available(prof_cpu_mask) &&
  359. cpumask_test_cpu(smp_processor_id(), prof_cpu_mask))
  360. profile_hit(type, (void *)profile_pc(regs));
  361. }
  362. #ifdef CONFIG_PROC_FS
  363. #include <linux/proc_fs.h>
  364. #include <linux/seq_file.h>
  365. #include <linux/uaccess.h>
  366. static int prof_cpu_mask_proc_show(struct seq_file *m, void *v)
  367. {
  368. seq_printf(m, "%*pb\n", cpumask_pr_args(prof_cpu_mask));
  369. return 0;
  370. }
  371. static int prof_cpu_mask_proc_open(struct inode *inode, struct file *file)
  372. {
  373. return single_open(file, prof_cpu_mask_proc_show, NULL);
  374. }
  375. static ssize_t prof_cpu_mask_proc_write(struct file *file,
  376. const char __user *buffer, size_t count, loff_t *pos)
  377. {
  378. cpumask_var_t new_value;
  379. int err;
  380. if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
  381. return -ENOMEM;
  382. err = cpumask_parse_user(buffer, count, new_value);
  383. if (!err) {
  384. cpumask_copy(prof_cpu_mask, new_value);
  385. err = count;
  386. }
  387. free_cpumask_var(new_value);
  388. return err;
  389. }
  390. static const struct proc_ops prof_cpu_mask_proc_ops = {
  391. .proc_open = prof_cpu_mask_proc_open,
  392. .proc_read = seq_read,
  393. .proc_lseek = seq_lseek,
  394. .proc_release = single_release,
  395. .proc_write = prof_cpu_mask_proc_write,
  396. };
  397. void create_prof_cpu_mask(void)
  398. {
  399. /* create /proc/irq/prof_cpu_mask */
  400. proc_create("irq/prof_cpu_mask", 0600, NULL, &prof_cpu_mask_proc_ops);
  401. }
  402. /*
  403. * This function accesses profiling information. The returned data is
  404. * binary: the sampling step and the actual contents of the profile
  405. * buffer. Use of the program readprofile is recommended in order to
  406. * get meaningful info out of these data.
  407. */
  408. static ssize_t
  409. read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  410. {
  411. unsigned long p = *ppos;
  412. ssize_t read;
  413. char *pnt;
  414. unsigned long sample_step = 1UL << prof_shift;
  415. profile_flip_buffers();
  416. if (p >= (prof_len+1)*sizeof(unsigned int))
  417. return 0;
  418. if (count > (prof_len+1)*sizeof(unsigned int) - p)
  419. count = (prof_len+1)*sizeof(unsigned int) - p;
  420. read = 0;
  421. while (p < sizeof(unsigned int) && count > 0) {
  422. if (put_user(*((char *)(&sample_step)+p), buf))
  423. return -EFAULT;
  424. buf++; p++; count--; read++;
  425. }
  426. pnt = (char *)prof_buffer + p - sizeof(atomic_t);
  427. if (copy_to_user(buf, (void *)pnt, count))
  428. return -EFAULT;
  429. read += count;
  430. *ppos += read;
  431. return read;
  432. }
  433. /*
  434. * Writing to /proc/profile resets the counters
  435. *
  436. * Writing a 'profiling multiplier' value into it also re-sets the profiling
  437. * interrupt frequency, on architectures that support this.
  438. */
  439. static ssize_t write_profile(struct file *file, const char __user *buf,
  440. size_t count, loff_t *ppos)
  441. {
  442. #ifdef CONFIG_SMP
  443. extern int setup_profiling_timer(unsigned int multiplier);
  444. if (count == sizeof(int)) {
  445. unsigned int multiplier;
  446. if (copy_from_user(&multiplier, buf, sizeof(int)))
  447. return -EFAULT;
  448. if (setup_profiling_timer(multiplier))
  449. return -EINVAL;
  450. }
  451. #endif
  452. profile_discard_flip_buffers();
  453. memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
  454. return count;
  455. }
  456. static const struct proc_ops profile_proc_ops = {
  457. .proc_read = read_profile,
  458. .proc_write = write_profile,
  459. .proc_lseek = default_llseek,
  460. };
  461. int __ref create_proc_profile(void)
  462. {
  463. struct proc_dir_entry *entry;
  464. #ifdef CONFIG_SMP
  465. enum cpuhp_state online_state;
  466. #endif
  467. int err = 0;
  468. if (!prof_on)
  469. return 0;
  470. #ifdef CONFIG_SMP
  471. err = cpuhp_setup_state(CPUHP_PROFILE_PREPARE, "PROFILE_PREPARE",
  472. profile_prepare_cpu, profile_dead_cpu);
  473. if (err)
  474. return err;
  475. err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "AP_PROFILE_ONLINE",
  476. profile_online_cpu, NULL);
  477. if (err < 0)
  478. goto err_state_prep;
  479. online_state = err;
  480. err = 0;
  481. #endif
  482. entry = proc_create("profile", S_IWUSR | S_IRUGO,
  483. NULL, &profile_proc_ops);
  484. if (!entry)
  485. goto err_state_onl;
  486. proc_set_size(entry, (1 + prof_len) * sizeof(atomic_t));
  487. return err;
  488. err_state_onl:
  489. #ifdef CONFIG_SMP
  490. cpuhp_remove_state(online_state);
  491. err_state_prep:
  492. cpuhp_remove_state(CPUHP_PROFILE_PREPARE);
  493. #endif
  494. return err;
  495. }
  496. subsys_initcall(create_proc_profile);
  497. #endif /* CONFIG_PROC_FS */