profile.c 14 KB

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