perf_counter.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. /*
  2. * Performance counter core code
  3. *
  4. * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
  6. *
  7. * For licencing details see kernel-base/COPYING
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/cpu.h>
  11. #include <linux/smp.h>
  12. #include <linux/file.h>
  13. #include <linux/poll.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/percpu.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/anon_inodes.h>
  20. #include <linux/perf_counter.h>
  21. /*
  22. * Each CPU has a list of per CPU counters:
  23. */
  24. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  25. int perf_max_counters __read_mostly = 1;
  26. static int perf_reserved_percpu __read_mostly;
  27. static int perf_overcommit __read_mostly = 1;
  28. /*
  29. * Mutex for (sysadmin-configurable) counter reservations:
  30. */
  31. static DEFINE_MUTEX(perf_resource_mutex);
  32. /*
  33. * Architecture provided APIs - weak aliases:
  34. */
  35. extern __weak const struct hw_perf_counter_ops *
  36. hw_perf_counter_init(struct perf_counter *counter)
  37. {
  38. return ERR_PTR(-EINVAL);
  39. }
  40. u64 __weak hw_perf_save_disable(void) { return 0; }
  41. void __weak hw_perf_restore(u64 ctrl) { }
  42. void __weak hw_perf_counter_setup(void) { }
  43. static void
  44. list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  45. {
  46. struct perf_counter *group_leader = counter->group_leader;
  47. /*
  48. * Depending on whether it is a standalone or sibling counter,
  49. * add it straight to the context's counter list, or to the group
  50. * leader's sibling list:
  51. */
  52. if (counter->group_leader == counter)
  53. list_add_tail(&counter->list_entry, &ctx->counter_list);
  54. else
  55. list_add_tail(&counter->list_entry, &group_leader->sibling_list);
  56. }
  57. static void
  58. list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  59. {
  60. struct perf_counter *sibling, *tmp;
  61. list_del_init(&counter->list_entry);
  62. /*
  63. * If this was a group counter with sibling counters then
  64. * upgrade the siblings to singleton counters by adding them
  65. * to the context list directly:
  66. */
  67. list_for_each_entry_safe(sibling, tmp,
  68. &counter->sibling_list, list_entry) {
  69. list_del_init(&sibling->list_entry);
  70. list_add_tail(&sibling->list_entry, &ctx->counter_list);
  71. sibling->group_leader = sibling;
  72. }
  73. }
  74. /*
  75. * Cross CPU call to remove a performance counter
  76. *
  77. * We disable the counter on the hardware level first. After that we
  78. * remove it from the context list.
  79. */
  80. static void __perf_counter_remove_from_context(void *info)
  81. {
  82. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  83. struct perf_counter *counter = info;
  84. struct perf_counter_context *ctx = counter->ctx;
  85. unsigned long flags;
  86. u64 perf_flags;
  87. /*
  88. * If this is a task context, we need to check whether it is
  89. * the current task context of this cpu. If not it has been
  90. * scheduled out before the smp call arrived.
  91. */
  92. if (ctx->task && cpuctx->task_ctx != ctx)
  93. return;
  94. spin_lock_irqsave(&ctx->lock, flags);
  95. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  96. counter->hw_ops->disable(counter);
  97. counter->state = PERF_COUNTER_STATE_INACTIVE;
  98. ctx->nr_active--;
  99. cpuctx->active_oncpu--;
  100. counter->task = NULL;
  101. }
  102. ctx->nr_counters--;
  103. /*
  104. * Protect the list operation against NMI by disabling the
  105. * counters on a global level. NOP for non NMI based counters.
  106. */
  107. perf_flags = hw_perf_save_disable();
  108. list_del_counter(counter, ctx);
  109. hw_perf_restore(perf_flags);
  110. if (!ctx->task) {
  111. /*
  112. * Allow more per task counters with respect to the
  113. * reservation:
  114. */
  115. cpuctx->max_pertask =
  116. min(perf_max_counters - ctx->nr_counters,
  117. perf_max_counters - perf_reserved_percpu);
  118. }
  119. spin_unlock_irqrestore(&ctx->lock, flags);
  120. }
  121. /*
  122. * Remove the counter from a task's (or a CPU's) list of counters.
  123. *
  124. * Must be called with counter->mutex held.
  125. *
  126. * CPU counters are removed with a smp call. For task counters we only
  127. * call when the task is on a CPU.
  128. */
  129. static void perf_counter_remove_from_context(struct perf_counter *counter)
  130. {
  131. struct perf_counter_context *ctx = counter->ctx;
  132. struct task_struct *task = ctx->task;
  133. if (!task) {
  134. /*
  135. * Per cpu counters are removed via an smp call and
  136. * the removal is always sucessful.
  137. */
  138. smp_call_function_single(counter->cpu,
  139. __perf_counter_remove_from_context,
  140. counter, 1);
  141. return;
  142. }
  143. retry:
  144. task_oncpu_function_call(task, __perf_counter_remove_from_context,
  145. counter);
  146. spin_lock_irq(&ctx->lock);
  147. /*
  148. * If the context is active we need to retry the smp call.
  149. */
  150. if (ctx->nr_active && !list_empty(&counter->list_entry)) {
  151. spin_unlock_irq(&ctx->lock);
  152. goto retry;
  153. }
  154. /*
  155. * The lock prevents that this context is scheduled in so we
  156. * can remove the counter safely, if the call above did not
  157. * succeed.
  158. */
  159. if (!list_empty(&counter->list_entry)) {
  160. ctx->nr_counters--;
  161. list_del_counter(counter, ctx);
  162. counter->task = NULL;
  163. }
  164. spin_unlock_irq(&ctx->lock);
  165. }
  166. /*
  167. * Cross CPU call to install and enable a preformance counter
  168. */
  169. static void __perf_install_in_context(void *info)
  170. {
  171. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  172. struct perf_counter *counter = info;
  173. struct perf_counter_context *ctx = counter->ctx;
  174. int cpu = smp_processor_id();
  175. unsigned long flags;
  176. u64 perf_flags;
  177. /*
  178. * If this is a task context, we need to check whether it is
  179. * the current task context of this cpu. If not it has been
  180. * scheduled out before the smp call arrived.
  181. */
  182. if (ctx->task && cpuctx->task_ctx != ctx)
  183. return;
  184. spin_lock_irqsave(&ctx->lock, flags);
  185. /*
  186. * Protect the list operation against NMI by disabling the
  187. * counters on a global level. NOP for non NMI based counters.
  188. */
  189. perf_flags = hw_perf_save_disable();
  190. list_add_counter(counter, ctx);
  191. hw_perf_restore(perf_flags);
  192. ctx->nr_counters++;
  193. if (cpuctx->active_oncpu < perf_max_counters) {
  194. counter->state = PERF_COUNTER_STATE_ACTIVE;
  195. counter->oncpu = cpu;
  196. ctx->nr_active++;
  197. cpuctx->active_oncpu++;
  198. counter->hw_ops->enable(counter);
  199. }
  200. if (!ctx->task && cpuctx->max_pertask)
  201. cpuctx->max_pertask--;
  202. spin_unlock_irqrestore(&ctx->lock, flags);
  203. }
  204. /*
  205. * Attach a performance counter to a context
  206. *
  207. * First we add the counter to the list with the hardware enable bit
  208. * in counter->hw_config cleared.
  209. *
  210. * If the counter is attached to a task which is on a CPU we use a smp
  211. * call to enable it in the task context. The task might have been
  212. * scheduled away, but we check this in the smp call again.
  213. */
  214. static void
  215. perf_install_in_context(struct perf_counter_context *ctx,
  216. struct perf_counter *counter,
  217. int cpu)
  218. {
  219. struct task_struct *task = ctx->task;
  220. counter->ctx = ctx;
  221. if (!task) {
  222. /*
  223. * Per cpu counters are installed via an smp call and
  224. * the install is always sucessful.
  225. */
  226. smp_call_function_single(cpu, __perf_install_in_context,
  227. counter, 1);
  228. return;
  229. }
  230. counter->task = task;
  231. retry:
  232. task_oncpu_function_call(task, __perf_install_in_context,
  233. counter);
  234. spin_lock_irq(&ctx->lock);
  235. /*
  236. * we need to retry the smp call.
  237. */
  238. if (ctx->nr_active && list_empty(&counter->list_entry)) {
  239. spin_unlock_irq(&ctx->lock);
  240. goto retry;
  241. }
  242. /*
  243. * The lock prevents that this context is scheduled in so we
  244. * can add the counter safely, if it the call above did not
  245. * succeed.
  246. */
  247. if (list_empty(&counter->list_entry)) {
  248. list_add_counter(counter, ctx);
  249. ctx->nr_counters++;
  250. }
  251. spin_unlock_irq(&ctx->lock);
  252. }
  253. static void
  254. counter_sched_out(struct perf_counter *counter,
  255. struct perf_cpu_context *cpuctx,
  256. struct perf_counter_context *ctx)
  257. {
  258. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  259. return;
  260. counter->hw_ops->disable(counter);
  261. counter->state = PERF_COUNTER_STATE_INACTIVE;
  262. counter->oncpu = -1;
  263. cpuctx->active_oncpu--;
  264. ctx->nr_active--;
  265. }
  266. static void
  267. group_sched_out(struct perf_counter *group_counter,
  268. struct perf_cpu_context *cpuctx,
  269. struct perf_counter_context *ctx)
  270. {
  271. struct perf_counter *counter;
  272. counter_sched_out(group_counter, cpuctx, ctx);
  273. /*
  274. * Schedule out siblings (if any):
  275. */
  276. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  277. counter_sched_out(counter, cpuctx, ctx);
  278. }
  279. /*
  280. * Called from scheduler to remove the counters of the current task,
  281. * with interrupts disabled.
  282. *
  283. * We stop each counter and update the counter value in counter->count.
  284. *
  285. * This does not protect us against NMI, but disable()
  286. * sets the disabled bit in the control field of counter _before_
  287. * accessing the counter control register. If a NMI hits, then it will
  288. * not restart the counter.
  289. */
  290. void perf_counter_task_sched_out(struct task_struct *task, int cpu)
  291. {
  292. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  293. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  294. struct perf_counter *counter;
  295. if (likely(!cpuctx->task_ctx))
  296. return;
  297. spin_lock(&ctx->lock);
  298. if (ctx->nr_active) {
  299. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  300. group_sched_out(counter, cpuctx, ctx);
  301. }
  302. spin_unlock(&ctx->lock);
  303. cpuctx->task_ctx = NULL;
  304. }
  305. static void
  306. counter_sched_in(struct perf_counter *counter,
  307. struct perf_cpu_context *cpuctx,
  308. struct perf_counter_context *ctx,
  309. int cpu)
  310. {
  311. if (counter->state == PERF_COUNTER_STATE_OFF)
  312. return;
  313. counter->hw_ops->enable(counter);
  314. counter->state = PERF_COUNTER_STATE_ACTIVE;
  315. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  316. cpuctx->active_oncpu++;
  317. ctx->nr_active++;
  318. }
  319. static int
  320. group_sched_in(struct perf_counter *group_counter,
  321. struct perf_cpu_context *cpuctx,
  322. struct perf_counter_context *ctx,
  323. int cpu)
  324. {
  325. struct perf_counter *counter;
  326. int was_group = 0;
  327. counter_sched_in(group_counter, cpuctx, ctx, cpu);
  328. /*
  329. * Schedule in siblings as one group (if any):
  330. */
  331. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  332. counter_sched_in(counter, cpuctx, ctx, cpu);
  333. was_group = 1;
  334. }
  335. return was_group;
  336. }
  337. /*
  338. * Called from scheduler to add the counters of the current task
  339. * with interrupts disabled.
  340. *
  341. * We restore the counter value and then enable it.
  342. *
  343. * This does not protect us against NMI, but enable()
  344. * sets the enabled bit in the control field of counter _before_
  345. * accessing the counter control register. If a NMI hits, then it will
  346. * keep the counter running.
  347. */
  348. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  349. {
  350. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  351. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  352. struct perf_counter *counter;
  353. if (likely(!ctx->nr_counters))
  354. return;
  355. spin_lock(&ctx->lock);
  356. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  357. if (ctx->nr_active == cpuctx->max_pertask)
  358. break;
  359. /*
  360. * Listen to the 'cpu' scheduling filter constraint
  361. * of counters:
  362. */
  363. if (counter->cpu != -1 && counter->cpu != cpu)
  364. continue;
  365. /*
  366. * If we scheduled in a group atomically and
  367. * exclusively, break out:
  368. */
  369. if (group_sched_in(counter, cpuctx, ctx, cpu))
  370. break;
  371. }
  372. spin_unlock(&ctx->lock);
  373. cpuctx->task_ctx = ctx;
  374. }
  375. int perf_counter_task_disable(void)
  376. {
  377. struct task_struct *curr = current;
  378. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  379. struct perf_counter *counter;
  380. u64 perf_flags;
  381. int cpu;
  382. if (likely(!ctx->nr_counters))
  383. return 0;
  384. local_irq_disable();
  385. cpu = smp_processor_id();
  386. perf_counter_task_sched_out(curr, cpu);
  387. spin_lock(&ctx->lock);
  388. /*
  389. * Disable all the counters:
  390. */
  391. perf_flags = hw_perf_save_disable();
  392. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  393. counter->state = PERF_COUNTER_STATE_OFF;
  394. hw_perf_restore(perf_flags);
  395. spin_unlock(&ctx->lock);
  396. local_irq_enable();
  397. return 0;
  398. }
  399. int perf_counter_task_enable(void)
  400. {
  401. struct task_struct *curr = current;
  402. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  403. struct perf_counter *counter;
  404. u64 perf_flags;
  405. int cpu;
  406. if (likely(!ctx->nr_counters))
  407. return 0;
  408. local_irq_disable();
  409. cpu = smp_processor_id();
  410. spin_lock(&ctx->lock);
  411. /*
  412. * Disable all the counters:
  413. */
  414. perf_flags = hw_perf_save_disable();
  415. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  416. if (counter->state != PERF_COUNTER_STATE_OFF)
  417. continue;
  418. counter->state = PERF_COUNTER_STATE_INACTIVE;
  419. }
  420. hw_perf_restore(perf_flags);
  421. spin_unlock(&ctx->lock);
  422. perf_counter_task_sched_in(curr, cpu);
  423. local_irq_enable();
  424. return 0;
  425. }
  426. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  427. {
  428. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  429. struct perf_counter *counter;
  430. u64 perf_flags;
  431. if (likely(!ctx->nr_counters))
  432. return;
  433. perf_counter_task_sched_out(curr, cpu);
  434. spin_lock(&ctx->lock);
  435. /*
  436. * Rotate the first entry last (works just fine for group counters too):
  437. */
  438. perf_flags = hw_perf_save_disable();
  439. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  440. list_del(&counter->list_entry);
  441. list_add_tail(&counter->list_entry, &ctx->counter_list);
  442. break;
  443. }
  444. hw_perf_restore(perf_flags);
  445. spin_unlock(&ctx->lock);
  446. perf_counter_task_sched_in(curr, cpu);
  447. }
  448. /*
  449. * Cross CPU call to read the hardware counter
  450. */
  451. static void __read(void *info)
  452. {
  453. struct perf_counter *counter = info;
  454. counter->hw_ops->read(counter);
  455. }
  456. static u64 perf_counter_read(struct perf_counter *counter)
  457. {
  458. /*
  459. * If counter is enabled and currently active on a CPU, update the
  460. * value in the counter structure:
  461. */
  462. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  463. smp_call_function_single(counter->oncpu,
  464. __read, counter, 1);
  465. }
  466. return atomic64_read(&counter->count);
  467. }
  468. /*
  469. * Cross CPU call to switch performance data pointers
  470. */
  471. static void __perf_switch_irq_data(void *info)
  472. {
  473. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  474. struct perf_counter *counter = info;
  475. struct perf_counter_context *ctx = counter->ctx;
  476. struct perf_data *oldirqdata = counter->irqdata;
  477. /*
  478. * If this is a task context, we need to check whether it is
  479. * the current task context of this cpu. If not it has been
  480. * scheduled out before the smp call arrived.
  481. */
  482. if (ctx->task) {
  483. if (cpuctx->task_ctx != ctx)
  484. return;
  485. spin_lock(&ctx->lock);
  486. }
  487. /* Change the pointer NMI safe */
  488. atomic_long_set((atomic_long_t *)&counter->irqdata,
  489. (unsigned long) counter->usrdata);
  490. counter->usrdata = oldirqdata;
  491. if (ctx->task)
  492. spin_unlock(&ctx->lock);
  493. }
  494. static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
  495. {
  496. struct perf_counter_context *ctx = counter->ctx;
  497. struct perf_data *oldirqdata = counter->irqdata;
  498. struct task_struct *task = ctx->task;
  499. if (!task) {
  500. smp_call_function_single(counter->cpu,
  501. __perf_switch_irq_data,
  502. counter, 1);
  503. return counter->usrdata;
  504. }
  505. retry:
  506. spin_lock_irq(&ctx->lock);
  507. if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
  508. counter->irqdata = counter->usrdata;
  509. counter->usrdata = oldirqdata;
  510. spin_unlock_irq(&ctx->lock);
  511. return oldirqdata;
  512. }
  513. spin_unlock_irq(&ctx->lock);
  514. task_oncpu_function_call(task, __perf_switch_irq_data, counter);
  515. /* Might have failed, because task was scheduled out */
  516. if (counter->irqdata == oldirqdata)
  517. goto retry;
  518. return counter->usrdata;
  519. }
  520. static void put_context(struct perf_counter_context *ctx)
  521. {
  522. if (ctx->task)
  523. put_task_struct(ctx->task);
  524. }
  525. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  526. {
  527. struct perf_cpu_context *cpuctx;
  528. struct perf_counter_context *ctx;
  529. struct task_struct *task;
  530. /*
  531. * If cpu is not a wildcard then this is a percpu counter:
  532. */
  533. if (cpu != -1) {
  534. /* Must be root to operate on a CPU counter: */
  535. if (!capable(CAP_SYS_ADMIN))
  536. return ERR_PTR(-EACCES);
  537. if (cpu < 0 || cpu > num_possible_cpus())
  538. return ERR_PTR(-EINVAL);
  539. /*
  540. * We could be clever and allow to attach a counter to an
  541. * offline CPU and activate it when the CPU comes up, but
  542. * that's for later.
  543. */
  544. if (!cpu_isset(cpu, cpu_online_map))
  545. return ERR_PTR(-ENODEV);
  546. cpuctx = &per_cpu(perf_cpu_context, cpu);
  547. ctx = &cpuctx->ctx;
  548. return ctx;
  549. }
  550. rcu_read_lock();
  551. if (!pid)
  552. task = current;
  553. else
  554. task = find_task_by_vpid(pid);
  555. if (task)
  556. get_task_struct(task);
  557. rcu_read_unlock();
  558. if (!task)
  559. return ERR_PTR(-ESRCH);
  560. ctx = &task->perf_counter_ctx;
  561. ctx->task = task;
  562. /* Reuse ptrace permission checks for now. */
  563. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  564. put_context(ctx);
  565. return ERR_PTR(-EACCES);
  566. }
  567. return ctx;
  568. }
  569. /*
  570. * Called when the last reference to the file is gone.
  571. */
  572. static int perf_release(struct inode *inode, struct file *file)
  573. {
  574. struct perf_counter *counter = file->private_data;
  575. struct perf_counter_context *ctx = counter->ctx;
  576. file->private_data = NULL;
  577. mutex_lock(&counter->mutex);
  578. perf_counter_remove_from_context(counter);
  579. put_context(ctx);
  580. mutex_unlock(&counter->mutex);
  581. kfree(counter);
  582. return 0;
  583. }
  584. /*
  585. * Read the performance counter - simple non blocking version for now
  586. */
  587. static ssize_t
  588. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  589. {
  590. u64 cntval;
  591. if (count != sizeof(cntval))
  592. return -EINVAL;
  593. mutex_lock(&counter->mutex);
  594. cntval = perf_counter_read(counter);
  595. mutex_unlock(&counter->mutex);
  596. return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
  597. }
  598. static ssize_t
  599. perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
  600. {
  601. if (!usrdata->len)
  602. return 0;
  603. count = min(count, (size_t)usrdata->len);
  604. if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
  605. return -EFAULT;
  606. /* Adjust the counters */
  607. usrdata->len -= count;
  608. if (!usrdata->len)
  609. usrdata->rd_idx = 0;
  610. else
  611. usrdata->rd_idx += count;
  612. return count;
  613. }
  614. static ssize_t
  615. perf_read_irq_data(struct perf_counter *counter,
  616. char __user *buf,
  617. size_t count,
  618. int nonblocking)
  619. {
  620. struct perf_data *irqdata, *usrdata;
  621. DECLARE_WAITQUEUE(wait, current);
  622. ssize_t res;
  623. irqdata = counter->irqdata;
  624. usrdata = counter->usrdata;
  625. if (usrdata->len + irqdata->len >= count)
  626. goto read_pending;
  627. if (nonblocking)
  628. return -EAGAIN;
  629. spin_lock_irq(&counter->waitq.lock);
  630. __add_wait_queue(&counter->waitq, &wait);
  631. for (;;) {
  632. set_current_state(TASK_INTERRUPTIBLE);
  633. if (usrdata->len + irqdata->len >= count)
  634. break;
  635. if (signal_pending(current))
  636. break;
  637. spin_unlock_irq(&counter->waitq.lock);
  638. schedule();
  639. spin_lock_irq(&counter->waitq.lock);
  640. }
  641. __remove_wait_queue(&counter->waitq, &wait);
  642. __set_current_state(TASK_RUNNING);
  643. spin_unlock_irq(&counter->waitq.lock);
  644. if (usrdata->len + irqdata->len < count)
  645. return -ERESTARTSYS;
  646. read_pending:
  647. mutex_lock(&counter->mutex);
  648. /* Drain pending data first: */
  649. res = perf_copy_usrdata(usrdata, buf, count);
  650. if (res < 0 || res == count)
  651. goto out;
  652. /* Switch irq buffer: */
  653. usrdata = perf_switch_irq_data(counter);
  654. if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
  655. if (!res)
  656. res = -EFAULT;
  657. } else {
  658. res = count;
  659. }
  660. out:
  661. mutex_unlock(&counter->mutex);
  662. return res;
  663. }
  664. static ssize_t
  665. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  666. {
  667. struct perf_counter *counter = file->private_data;
  668. switch (counter->hw_event.record_type) {
  669. case PERF_RECORD_SIMPLE:
  670. return perf_read_hw(counter, buf, count);
  671. case PERF_RECORD_IRQ:
  672. case PERF_RECORD_GROUP:
  673. return perf_read_irq_data(counter, buf, count,
  674. file->f_flags & O_NONBLOCK);
  675. }
  676. return -EINVAL;
  677. }
  678. static unsigned int perf_poll(struct file *file, poll_table *wait)
  679. {
  680. struct perf_counter *counter = file->private_data;
  681. unsigned int events = 0;
  682. unsigned long flags;
  683. poll_wait(file, &counter->waitq, wait);
  684. spin_lock_irqsave(&counter->waitq.lock, flags);
  685. if (counter->usrdata->len || counter->irqdata->len)
  686. events |= POLLIN;
  687. spin_unlock_irqrestore(&counter->waitq.lock, flags);
  688. return events;
  689. }
  690. static const struct file_operations perf_fops = {
  691. .release = perf_release,
  692. .read = perf_read,
  693. .poll = perf_poll,
  694. };
  695. static void cpu_clock_perf_counter_enable(struct perf_counter *counter)
  696. {
  697. }
  698. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  699. {
  700. }
  701. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  702. {
  703. int cpu = raw_smp_processor_id();
  704. atomic64_set(&counter->count, cpu_clock(cpu));
  705. }
  706. static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
  707. .enable = cpu_clock_perf_counter_enable,
  708. .disable = cpu_clock_perf_counter_disable,
  709. .read = cpu_clock_perf_counter_read,
  710. };
  711. static void task_clock_perf_counter_update(struct perf_counter *counter)
  712. {
  713. u64 prev, now;
  714. s64 delta;
  715. prev = atomic64_read(&counter->hw.prev_count);
  716. now = current->se.sum_exec_runtime;
  717. atomic64_set(&counter->hw.prev_count, now);
  718. delta = now - prev;
  719. atomic64_add(delta, &counter->count);
  720. }
  721. static void task_clock_perf_counter_read(struct perf_counter *counter)
  722. {
  723. task_clock_perf_counter_update(counter);
  724. }
  725. static void task_clock_perf_counter_enable(struct perf_counter *counter)
  726. {
  727. atomic64_set(&counter->hw.prev_count, current->se.sum_exec_runtime);
  728. }
  729. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  730. {
  731. task_clock_perf_counter_update(counter);
  732. }
  733. static const struct hw_perf_counter_ops perf_ops_task_clock = {
  734. .enable = task_clock_perf_counter_enable,
  735. .disable = task_clock_perf_counter_disable,
  736. .read = task_clock_perf_counter_read,
  737. };
  738. static u64 get_page_faults(void)
  739. {
  740. struct task_struct *curr = current;
  741. return curr->maj_flt + curr->min_flt;
  742. }
  743. static void page_faults_perf_counter_update(struct perf_counter *counter)
  744. {
  745. u64 prev, now;
  746. s64 delta;
  747. prev = atomic64_read(&counter->hw.prev_count);
  748. now = get_page_faults();
  749. atomic64_set(&counter->hw.prev_count, now);
  750. delta = now - prev;
  751. atomic64_add(delta, &counter->count);
  752. }
  753. static void page_faults_perf_counter_read(struct perf_counter *counter)
  754. {
  755. page_faults_perf_counter_update(counter);
  756. }
  757. static void page_faults_perf_counter_enable(struct perf_counter *counter)
  758. {
  759. /*
  760. * page-faults is a per-task value already,
  761. * so we dont have to clear it on switch-in.
  762. */
  763. }
  764. static void page_faults_perf_counter_disable(struct perf_counter *counter)
  765. {
  766. page_faults_perf_counter_update(counter);
  767. }
  768. static const struct hw_perf_counter_ops perf_ops_page_faults = {
  769. .enable = page_faults_perf_counter_enable,
  770. .disable = page_faults_perf_counter_disable,
  771. .read = page_faults_perf_counter_read,
  772. };
  773. static u64 get_context_switches(void)
  774. {
  775. struct task_struct *curr = current;
  776. return curr->nvcsw + curr->nivcsw;
  777. }
  778. static void context_switches_perf_counter_update(struct perf_counter *counter)
  779. {
  780. u64 prev, now;
  781. s64 delta;
  782. prev = atomic64_read(&counter->hw.prev_count);
  783. now = get_context_switches();
  784. atomic64_set(&counter->hw.prev_count, now);
  785. delta = now - prev;
  786. atomic64_add(delta, &counter->count);
  787. }
  788. static void context_switches_perf_counter_read(struct perf_counter *counter)
  789. {
  790. context_switches_perf_counter_update(counter);
  791. }
  792. static void context_switches_perf_counter_enable(struct perf_counter *counter)
  793. {
  794. /*
  795. * ->nvcsw + curr->nivcsw is a per-task value already,
  796. * so we dont have to clear it on switch-in.
  797. */
  798. }
  799. static void context_switches_perf_counter_disable(struct perf_counter *counter)
  800. {
  801. context_switches_perf_counter_update(counter);
  802. }
  803. static const struct hw_perf_counter_ops perf_ops_context_switches = {
  804. .enable = context_switches_perf_counter_enable,
  805. .disable = context_switches_perf_counter_disable,
  806. .read = context_switches_perf_counter_read,
  807. };
  808. static inline u64 get_cpu_migrations(void)
  809. {
  810. return current->se.nr_migrations;
  811. }
  812. static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
  813. {
  814. u64 prev, now;
  815. s64 delta;
  816. prev = atomic64_read(&counter->hw.prev_count);
  817. now = get_cpu_migrations();
  818. atomic64_set(&counter->hw.prev_count, now);
  819. delta = now - prev;
  820. atomic64_add(delta, &counter->count);
  821. }
  822. static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
  823. {
  824. cpu_migrations_perf_counter_update(counter);
  825. }
  826. static void cpu_migrations_perf_counter_enable(struct perf_counter *counter)
  827. {
  828. /*
  829. * se.nr_migrations is a per-task value already,
  830. * so we dont have to clear it on switch-in.
  831. */
  832. }
  833. static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
  834. {
  835. cpu_migrations_perf_counter_update(counter);
  836. }
  837. static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
  838. .enable = cpu_migrations_perf_counter_enable,
  839. .disable = cpu_migrations_perf_counter_disable,
  840. .read = cpu_migrations_perf_counter_read,
  841. };
  842. static const struct hw_perf_counter_ops *
  843. sw_perf_counter_init(struct perf_counter *counter)
  844. {
  845. const struct hw_perf_counter_ops *hw_ops = NULL;
  846. switch (counter->hw_event.type) {
  847. case PERF_COUNT_CPU_CLOCK:
  848. hw_ops = &perf_ops_cpu_clock;
  849. break;
  850. case PERF_COUNT_TASK_CLOCK:
  851. hw_ops = &perf_ops_task_clock;
  852. break;
  853. case PERF_COUNT_PAGE_FAULTS:
  854. hw_ops = &perf_ops_page_faults;
  855. break;
  856. case PERF_COUNT_CONTEXT_SWITCHES:
  857. hw_ops = &perf_ops_context_switches;
  858. break;
  859. case PERF_COUNT_CPU_MIGRATIONS:
  860. hw_ops = &perf_ops_cpu_migrations;
  861. break;
  862. default:
  863. break;
  864. }
  865. return hw_ops;
  866. }
  867. /*
  868. * Allocate and initialize a counter structure
  869. */
  870. static struct perf_counter *
  871. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  872. int cpu,
  873. struct perf_counter *group_leader,
  874. gfp_t gfpflags)
  875. {
  876. const struct hw_perf_counter_ops *hw_ops;
  877. struct perf_counter *counter;
  878. counter = kzalloc(sizeof(*counter), gfpflags);
  879. if (!counter)
  880. return NULL;
  881. /*
  882. * Single counters are their own group leaders, with an
  883. * empty sibling list:
  884. */
  885. if (!group_leader)
  886. group_leader = counter;
  887. mutex_init(&counter->mutex);
  888. INIT_LIST_HEAD(&counter->list_entry);
  889. INIT_LIST_HEAD(&counter->sibling_list);
  890. init_waitqueue_head(&counter->waitq);
  891. counter->irqdata = &counter->data[0];
  892. counter->usrdata = &counter->data[1];
  893. counter->cpu = cpu;
  894. counter->hw_event = *hw_event;
  895. counter->wakeup_pending = 0;
  896. counter->group_leader = group_leader;
  897. counter->hw_ops = NULL;
  898. if (hw_event->disabled)
  899. counter->state = PERF_COUNTER_STATE_OFF;
  900. hw_ops = NULL;
  901. if (!hw_event->raw && hw_event->type < 0)
  902. hw_ops = sw_perf_counter_init(counter);
  903. if (!hw_ops)
  904. hw_ops = hw_perf_counter_init(counter);
  905. if (!hw_ops) {
  906. kfree(counter);
  907. return NULL;
  908. }
  909. counter->hw_ops = hw_ops;
  910. return counter;
  911. }
  912. /**
  913. * sys_perf_task_open - open a performance counter, associate it to a task/cpu
  914. *
  915. * @hw_event_uptr: event type attributes for monitoring/sampling
  916. * @pid: target pid
  917. * @cpu: target cpu
  918. * @group_fd: group leader counter fd
  919. */
  920. asmlinkage int
  921. sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
  922. pid_t pid, int cpu, int group_fd)
  923. {
  924. struct perf_counter *counter, *group_leader;
  925. struct perf_counter_hw_event hw_event;
  926. struct perf_counter_context *ctx;
  927. struct file *counter_file = NULL;
  928. struct file *group_file = NULL;
  929. int fput_needed = 0;
  930. int fput_needed2 = 0;
  931. int ret;
  932. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  933. return -EFAULT;
  934. /*
  935. * Get the target context (task or percpu):
  936. */
  937. ctx = find_get_context(pid, cpu);
  938. if (IS_ERR(ctx))
  939. return PTR_ERR(ctx);
  940. /*
  941. * Look up the group leader (we will attach this counter to it):
  942. */
  943. group_leader = NULL;
  944. if (group_fd != -1) {
  945. ret = -EINVAL;
  946. group_file = fget_light(group_fd, &fput_needed);
  947. if (!group_file)
  948. goto err_put_context;
  949. if (group_file->f_op != &perf_fops)
  950. goto err_put_context;
  951. group_leader = group_file->private_data;
  952. /*
  953. * Do not allow a recursive hierarchy (this new sibling
  954. * becoming part of another group-sibling):
  955. */
  956. if (group_leader->group_leader != group_leader)
  957. goto err_put_context;
  958. /*
  959. * Do not allow to attach to a group in a different
  960. * task or CPU context:
  961. */
  962. if (group_leader->ctx != ctx)
  963. goto err_put_context;
  964. }
  965. ret = -EINVAL;
  966. counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL);
  967. if (!counter)
  968. goto err_put_context;
  969. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  970. if (ret < 0)
  971. goto err_free_put_context;
  972. counter_file = fget_light(ret, &fput_needed2);
  973. if (!counter_file)
  974. goto err_free_put_context;
  975. counter->filp = counter_file;
  976. perf_install_in_context(ctx, counter, cpu);
  977. fput_light(counter_file, fput_needed2);
  978. out_fput:
  979. fput_light(group_file, fput_needed);
  980. return ret;
  981. err_free_put_context:
  982. kfree(counter);
  983. err_put_context:
  984. put_context(ctx);
  985. goto out_fput;
  986. }
  987. /*
  988. * Initialize the perf_counter context in a task_struct:
  989. */
  990. static void
  991. __perf_counter_init_context(struct perf_counter_context *ctx,
  992. struct task_struct *task)
  993. {
  994. memset(ctx, 0, sizeof(*ctx));
  995. spin_lock_init(&ctx->lock);
  996. INIT_LIST_HEAD(&ctx->counter_list);
  997. ctx->task = task;
  998. }
  999. /*
  1000. * inherit a counter from parent task to child task:
  1001. */
  1002. static int
  1003. inherit_counter(struct perf_counter *parent_counter,
  1004. struct task_struct *parent,
  1005. struct perf_counter_context *parent_ctx,
  1006. struct task_struct *child,
  1007. struct perf_counter_context *child_ctx)
  1008. {
  1009. struct perf_counter *child_counter;
  1010. child_counter = perf_counter_alloc(&parent_counter->hw_event,
  1011. parent_counter->cpu, NULL,
  1012. GFP_ATOMIC);
  1013. if (!child_counter)
  1014. return -ENOMEM;
  1015. /*
  1016. * Link it up in the child's context:
  1017. */
  1018. child_counter->ctx = child_ctx;
  1019. child_counter->task = child;
  1020. list_add_counter(child_counter, child_ctx);
  1021. child_ctx->nr_counters++;
  1022. child_counter->parent = parent_counter;
  1023. parent_counter->nr_inherited++;
  1024. /*
  1025. * inherit into child's child as well:
  1026. */
  1027. child_counter->hw_event.inherit = 1;
  1028. /*
  1029. * Get a reference to the parent filp - we will fput it
  1030. * when the child counter exits. This is safe to do because
  1031. * we are in the parent and we know that the filp still
  1032. * exists and has a nonzero count:
  1033. */
  1034. atomic_long_inc(&parent_counter->filp->f_count);
  1035. return 0;
  1036. }
  1037. static void
  1038. __perf_counter_exit_task(struct task_struct *child,
  1039. struct perf_counter *child_counter,
  1040. struct perf_counter_context *child_ctx)
  1041. {
  1042. struct perf_counter *parent_counter;
  1043. u64 parent_val, child_val;
  1044. u64 perf_flags;
  1045. /*
  1046. * Disable and unlink this counter.
  1047. *
  1048. * Be careful about zapping the list - IRQ/NMI context
  1049. * could still be processing it:
  1050. */
  1051. local_irq_disable();
  1052. perf_flags = hw_perf_save_disable();
  1053. if (child_counter->state == PERF_COUNTER_STATE_ACTIVE) {
  1054. struct perf_cpu_context *cpuctx;
  1055. cpuctx = &__get_cpu_var(perf_cpu_context);
  1056. child_counter->hw_ops->disable(child_counter);
  1057. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  1058. child_counter->oncpu = -1;
  1059. cpuctx->active_oncpu--;
  1060. child_ctx->nr_active--;
  1061. }
  1062. list_del_init(&child_counter->list_entry);
  1063. hw_perf_restore(perf_flags);
  1064. local_irq_enable();
  1065. parent_counter = child_counter->parent;
  1066. /*
  1067. * It can happen that parent exits first, and has counters
  1068. * that are still around due to the child reference. These
  1069. * counters need to be zapped - but otherwise linger.
  1070. */
  1071. if (!parent_counter)
  1072. return;
  1073. parent_val = atomic64_read(&parent_counter->count);
  1074. child_val = atomic64_read(&child_counter->count);
  1075. /*
  1076. * Add back the child's count to the parent's count:
  1077. */
  1078. atomic64_add(child_val, &parent_counter->count);
  1079. fput(parent_counter->filp);
  1080. kfree(child_counter);
  1081. }
  1082. /*
  1083. * When a child task exist, feed back counter values to parent counters.
  1084. *
  1085. * Note: we are running in child context, but the PID is not hashed
  1086. * anymore so new counters will not be added.
  1087. */
  1088. void perf_counter_exit_task(struct task_struct *child)
  1089. {
  1090. struct perf_counter *child_counter, *tmp;
  1091. struct perf_counter_context *child_ctx;
  1092. child_ctx = &child->perf_counter_ctx;
  1093. if (likely(!child_ctx->nr_counters))
  1094. return;
  1095. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  1096. list_entry)
  1097. __perf_counter_exit_task(child, child_counter, child_ctx);
  1098. }
  1099. /*
  1100. * Initialize the perf_counter context in task_struct
  1101. */
  1102. void perf_counter_init_task(struct task_struct *child)
  1103. {
  1104. struct perf_counter_context *child_ctx, *parent_ctx;
  1105. struct perf_counter *counter, *parent_counter;
  1106. struct task_struct *parent = current;
  1107. unsigned long flags;
  1108. child_ctx = &child->perf_counter_ctx;
  1109. parent_ctx = &parent->perf_counter_ctx;
  1110. __perf_counter_init_context(child_ctx, child);
  1111. /*
  1112. * This is executed from the parent task context, so inherit
  1113. * counters that have been marked for cloning:
  1114. */
  1115. if (likely(!parent_ctx->nr_counters))
  1116. return;
  1117. /*
  1118. * Lock the parent list. No need to lock the child - not PID
  1119. * hashed yet and not running, so nobody can access it.
  1120. */
  1121. spin_lock_irqsave(&parent_ctx->lock, flags);
  1122. /*
  1123. * We dont have to disable NMIs - we are only looking at
  1124. * the list, not manipulating it:
  1125. */
  1126. list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
  1127. if (!counter->hw_event.inherit || counter->group_leader != counter)
  1128. continue;
  1129. /*
  1130. * Instead of creating recursive hierarchies of counters,
  1131. * we link inheritd counters back to the original parent,
  1132. * which has a filp for sure, which we use as the reference
  1133. * count:
  1134. */
  1135. parent_counter = counter;
  1136. if (counter->parent)
  1137. parent_counter = counter->parent;
  1138. if (inherit_counter(parent_counter, parent,
  1139. parent_ctx, child, child_ctx))
  1140. break;
  1141. }
  1142. spin_unlock_irqrestore(&parent_ctx->lock, flags);
  1143. }
  1144. static void __cpuinit perf_counter_init_cpu(int cpu)
  1145. {
  1146. struct perf_cpu_context *cpuctx;
  1147. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1148. __perf_counter_init_context(&cpuctx->ctx, NULL);
  1149. mutex_lock(&perf_resource_mutex);
  1150. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  1151. mutex_unlock(&perf_resource_mutex);
  1152. hw_perf_counter_setup();
  1153. }
  1154. #ifdef CONFIG_HOTPLUG_CPU
  1155. static void __perf_counter_exit_cpu(void *info)
  1156. {
  1157. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1158. struct perf_counter_context *ctx = &cpuctx->ctx;
  1159. struct perf_counter *counter, *tmp;
  1160. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  1161. __perf_counter_remove_from_context(counter);
  1162. }
  1163. static void perf_counter_exit_cpu(int cpu)
  1164. {
  1165. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  1166. }
  1167. #else
  1168. static inline void perf_counter_exit_cpu(int cpu) { }
  1169. #endif
  1170. static int __cpuinit
  1171. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  1172. {
  1173. unsigned int cpu = (long)hcpu;
  1174. switch (action) {
  1175. case CPU_UP_PREPARE:
  1176. case CPU_UP_PREPARE_FROZEN:
  1177. perf_counter_init_cpu(cpu);
  1178. break;
  1179. case CPU_DOWN_PREPARE:
  1180. case CPU_DOWN_PREPARE_FROZEN:
  1181. perf_counter_exit_cpu(cpu);
  1182. break;
  1183. default:
  1184. break;
  1185. }
  1186. return NOTIFY_OK;
  1187. }
  1188. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  1189. .notifier_call = perf_cpu_notify,
  1190. };
  1191. static int __init perf_counter_init(void)
  1192. {
  1193. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  1194. (void *)(long)smp_processor_id());
  1195. register_cpu_notifier(&perf_cpu_nb);
  1196. return 0;
  1197. }
  1198. early_initcall(perf_counter_init);
  1199. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  1200. {
  1201. return sprintf(buf, "%d\n", perf_reserved_percpu);
  1202. }
  1203. static ssize_t
  1204. perf_set_reserve_percpu(struct sysdev_class *class,
  1205. const char *buf,
  1206. size_t count)
  1207. {
  1208. struct perf_cpu_context *cpuctx;
  1209. unsigned long val;
  1210. int err, cpu, mpt;
  1211. err = strict_strtoul(buf, 10, &val);
  1212. if (err)
  1213. return err;
  1214. if (val > perf_max_counters)
  1215. return -EINVAL;
  1216. mutex_lock(&perf_resource_mutex);
  1217. perf_reserved_percpu = val;
  1218. for_each_online_cpu(cpu) {
  1219. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1220. spin_lock_irq(&cpuctx->ctx.lock);
  1221. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  1222. perf_max_counters - perf_reserved_percpu);
  1223. cpuctx->max_pertask = mpt;
  1224. spin_unlock_irq(&cpuctx->ctx.lock);
  1225. }
  1226. mutex_unlock(&perf_resource_mutex);
  1227. return count;
  1228. }
  1229. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  1230. {
  1231. return sprintf(buf, "%d\n", perf_overcommit);
  1232. }
  1233. static ssize_t
  1234. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  1235. {
  1236. unsigned long val;
  1237. int err;
  1238. err = strict_strtoul(buf, 10, &val);
  1239. if (err)
  1240. return err;
  1241. if (val > 1)
  1242. return -EINVAL;
  1243. mutex_lock(&perf_resource_mutex);
  1244. perf_overcommit = val;
  1245. mutex_unlock(&perf_resource_mutex);
  1246. return count;
  1247. }
  1248. static SYSDEV_CLASS_ATTR(
  1249. reserve_percpu,
  1250. 0644,
  1251. perf_show_reserve_percpu,
  1252. perf_set_reserve_percpu
  1253. );
  1254. static SYSDEV_CLASS_ATTR(
  1255. overcommit,
  1256. 0644,
  1257. perf_show_overcommit,
  1258. perf_set_overcommit
  1259. );
  1260. static struct attribute *perfclass_attrs[] = {
  1261. &attr_reserve_percpu.attr,
  1262. &attr_overcommit.attr,
  1263. NULL
  1264. };
  1265. static struct attribute_group perfclass_attr_group = {
  1266. .attrs = perfclass_attrs,
  1267. .name = "perf_counters",
  1268. };
  1269. static int __init perf_counter_sysfs_init(void)
  1270. {
  1271. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  1272. &perfclass_attr_group);
  1273. }
  1274. device_initcall(perf_counter_sysfs_init);