perf_counter.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199
  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/kernel_stat.h>
  21. #include <linux/perf_counter.h>
  22. #include <linux/mm.h>
  23. #include <linux/vmstat.h>
  24. /*
  25. * Each CPU has a list of per CPU counters:
  26. */
  27. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  28. int perf_max_counters __read_mostly = 1;
  29. static int perf_reserved_percpu __read_mostly;
  30. static int perf_overcommit __read_mostly = 1;
  31. /*
  32. * Mutex for (sysadmin-configurable) counter reservations:
  33. */
  34. static DEFINE_MUTEX(perf_resource_mutex);
  35. /*
  36. * Architecture provided APIs - weak aliases:
  37. */
  38. extern __weak const struct hw_perf_counter_ops *
  39. hw_perf_counter_init(struct perf_counter *counter)
  40. {
  41. return NULL;
  42. }
  43. u64 __weak hw_perf_save_disable(void) { return 0; }
  44. void __weak hw_perf_restore(u64 ctrl) { barrier(); }
  45. void __weak hw_perf_counter_setup(int cpu) { barrier(); }
  46. int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
  47. struct perf_cpu_context *cpuctx,
  48. struct perf_counter_context *ctx, int cpu)
  49. {
  50. return 0;
  51. }
  52. void __weak perf_counter_print_debug(void) { }
  53. static void
  54. list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  55. {
  56. struct perf_counter *group_leader = counter->group_leader;
  57. /*
  58. * Depending on whether it is a standalone or sibling counter,
  59. * add it straight to the context's counter list, or to the group
  60. * leader's sibling list:
  61. */
  62. if (counter->group_leader == counter)
  63. list_add_tail(&counter->list_entry, &ctx->counter_list);
  64. else
  65. list_add_tail(&counter->list_entry, &group_leader->sibling_list);
  66. }
  67. static void
  68. list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
  69. {
  70. struct perf_counter *sibling, *tmp;
  71. list_del_init(&counter->list_entry);
  72. /*
  73. * If this was a group counter with sibling counters then
  74. * upgrade the siblings to singleton counters by adding them
  75. * to the context list directly:
  76. */
  77. list_for_each_entry_safe(sibling, tmp,
  78. &counter->sibling_list, list_entry) {
  79. list_del_init(&sibling->list_entry);
  80. list_add_tail(&sibling->list_entry, &ctx->counter_list);
  81. sibling->group_leader = sibling;
  82. }
  83. }
  84. static void
  85. counter_sched_out(struct perf_counter *counter,
  86. struct perf_cpu_context *cpuctx,
  87. struct perf_counter_context *ctx)
  88. {
  89. if (counter->state != PERF_COUNTER_STATE_ACTIVE)
  90. return;
  91. counter->state = PERF_COUNTER_STATE_INACTIVE;
  92. counter->hw_ops->disable(counter);
  93. counter->oncpu = -1;
  94. if (!is_software_counter(counter))
  95. cpuctx->active_oncpu--;
  96. ctx->nr_active--;
  97. if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
  98. cpuctx->exclusive = 0;
  99. }
  100. static void
  101. group_sched_out(struct perf_counter *group_counter,
  102. struct perf_cpu_context *cpuctx,
  103. struct perf_counter_context *ctx)
  104. {
  105. struct perf_counter *counter;
  106. if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
  107. return;
  108. counter_sched_out(group_counter, cpuctx, ctx);
  109. /*
  110. * Schedule out siblings (if any):
  111. */
  112. list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
  113. counter_sched_out(counter, cpuctx, ctx);
  114. if (group_counter->hw_event.exclusive)
  115. cpuctx->exclusive = 0;
  116. }
  117. /*
  118. * Cross CPU call to remove a performance counter
  119. *
  120. * We disable the counter on the hardware level first. After that we
  121. * remove it from the context list.
  122. */
  123. static void __perf_counter_remove_from_context(void *info)
  124. {
  125. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  126. struct perf_counter *counter = info;
  127. struct perf_counter_context *ctx = counter->ctx;
  128. unsigned long flags;
  129. u64 perf_flags;
  130. /*
  131. * If this is a task context, we need to check whether it is
  132. * the current task context of this cpu. If not it has been
  133. * scheduled out before the smp call arrived.
  134. */
  135. if (ctx->task && cpuctx->task_ctx != ctx)
  136. return;
  137. curr_rq_lock_irq_save(&flags);
  138. spin_lock(&ctx->lock);
  139. counter_sched_out(counter, cpuctx, ctx);
  140. counter->task = NULL;
  141. ctx->nr_counters--;
  142. /*
  143. * Protect the list operation against NMI by disabling the
  144. * counters on a global level. NOP for non NMI based counters.
  145. */
  146. perf_flags = hw_perf_save_disable();
  147. list_del_counter(counter, ctx);
  148. hw_perf_restore(perf_flags);
  149. if (!ctx->task) {
  150. /*
  151. * Allow more per task counters with respect to the
  152. * reservation:
  153. */
  154. cpuctx->max_pertask =
  155. min(perf_max_counters - ctx->nr_counters,
  156. perf_max_counters - perf_reserved_percpu);
  157. }
  158. spin_unlock(&ctx->lock);
  159. curr_rq_unlock_irq_restore(&flags);
  160. }
  161. /*
  162. * Remove the counter from a task's (or a CPU's) list of counters.
  163. *
  164. * Must be called with counter->mutex and ctx->mutex held.
  165. *
  166. * CPU counters are removed with a smp call. For task counters we only
  167. * call when the task is on a CPU.
  168. */
  169. static void perf_counter_remove_from_context(struct perf_counter *counter)
  170. {
  171. struct perf_counter_context *ctx = counter->ctx;
  172. struct task_struct *task = ctx->task;
  173. if (!task) {
  174. /*
  175. * Per cpu counters are removed via an smp call and
  176. * the removal is always sucessful.
  177. */
  178. smp_call_function_single(counter->cpu,
  179. __perf_counter_remove_from_context,
  180. counter, 1);
  181. return;
  182. }
  183. retry:
  184. task_oncpu_function_call(task, __perf_counter_remove_from_context,
  185. counter);
  186. spin_lock_irq(&ctx->lock);
  187. /*
  188. * If the context is active we need to retry the smp call.
  189. */
  190. if (ctx->nr_active && !list_empty(&counter->list_entry)) {
  191. spin_unlock_irq(&ctx->lock);
  192. goto retry;
  193. }
  194. /*
  195. * The lock prevents that this context is scheduled in so we
  196. * can remove the counter safely, if the call above did not
  197. * succeed.
  198. */
  199. if (!list_empty(&counter->list_entry)) {
  200. ctx->nr_counters--;
  201. list_del_counter(counter, ctx);
  202. counter->task = NULL;
  203. }
  204. spin_unlock_irq(&ctx->lock);
  205. }
  206. /*
  207. * Cross CPU call to disable a performance counter
  208. */
  209. static void __perf_counter_disable(void *info)
  210. {
  211. struct perf_counter *counter = info;
  212. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  213. struct perf_counter_context *ctx = counter->ctx;
  214. unsigned long flags;
  215. /*
  216. * If this is a per-task counter, need to check whether this
  217. * counter's task is the current task on this cpu.
  218. */
  219. if (ctx->task && cpuctx->task_ctx != ctx)
  220. return;
  221. curr_rq_lock_irq_save(&flags);
  222. spin_lock(&ctx->lock);
  223. /*
  224. * If the counter is on, turn it off.
  225. * If it is in error state, leave it in error state.
  226. */
  227. if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
  228. if (counter == counter->group_leader)
  229. group_sched_out(counter, cpuctx, ctx);
  230. else
  231. counter_sched_out(counter, cpuctx, ctx);
  232. counter->state = PERF_COUNTER_STATE_OFF;
  233. }
  234. spin_unlock(&ctx->lock);
  235. curr_rq_unlock_irq_restore(&flags);
  236. }
  237. /*
  238. * Disable a counter.
  239. */
  240. static void perf_counter_disable(struct perf_counter *counter)
  241. {
  242. struct perf_counter_context *ctx = counter->ctx;
  243. struct task_struct *task = ctx->task;
  244. if (!task) {
  245. /*
  246. * Disable the counter on the cpu that it's on
  247. */
  248. smp_call_function_single(counter->cpu, __perf_counter_disable,
  249. counter, 1);
  250. return;
  251. }
  252. retry:
  253. task_oncpu_function_call(task, __perf_counter_disable, counter);
  254. spin_lock_irq(&ctx->lock);
  255. /*
  256. * If the counter is still active, we need to retry the cross-call.
  257. */
  258. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  259. spin_unlock_irq(&ctx->lock);
  260. goto retry;
  261. }
  262. /*
  263. * Since we have the lock this context can't be scheduled
  264. * in, so we can change the state safely.
  265. */
  266. if (counter->state == PERF_COUNTER_STATE_INACTIVE)
  267. counter->state = PERF_COUNTER_STATE_OFF;
  268. spin_unlock_irq(&ctx->lock);
  269. }
  270. /*
  271. * Disable a counter and all its children.
  272. */
  273. static void perf_counter_disable_family(struct perf_counter *counter)
  274. {
  275. struct perf_counter *child;
  276. perf_counter_disable(counter);
  277. /*
  278. * Lock the mutex to protect the list of children
  279. */
  280. mutex_lock(&counter->mutex);
  281. list_for_each_entry(child, &counter->child_list, child_list)
  282. perf_counter_disable(child);
  283. mutex_unlock(&counter->mutex);
  284. }
  285. static int
  286. counter_sched_in(struct perf_counter *counter,
  287. struct perf_cpu_context *cpuctx,
  288. struct perf_counter_context *ctx,
  289. int cpu)
  290. {
  291. if (counter->state <= PERF_COUNTER_STATE_OFF)
  292. return 0;
  293. counter->state = PERF_COUNTER_STATE_ACTIVE;
  294. counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
  295. /*
  296. * The new state must be visible before we turn it on in the hardware:
  297. */
  298. smp_wmb();
  299. if (counter->hw_ops->enable(counter)) {
  300. counter->state = PERF_COUNTER_STATE_INACTIVE;
  301. counter->oncpu = -1;
  302. return -EAGAIN;
  303. }
  304. if (!is_software_counter(counter))
  305. cpuctx->active_oncpu++;
  306. ctx->nr_active++;
  307. if (counter->hw_event.exclusive)
  308. cpuctx->exclusive = 1;
  309. return 0;
  310. }
  311. /*
  312. * Return 1 for a group consisting entirely of software counters,
  313. * 0 if the group contains any hardware counters.
  314. */
  315. static int is_software_only_group(struct perf_counter *leader)
  316. {
  317. struct perf_counter *counter;
  318. if (!is_software_counter(leader))
  319. return 0;
  320. list_for_each_entry(counter, &leader->sibling_list, list_entry)
  321. if (!is_software_counter(counter))
  322. return 0;
  323. return 1;
  324. }
  325. /*
  326. * Work out whether we can put this counter group on the CPU now.
  327. */
  328. static int group_can_go_on(struct perf_counter *counter,
  329. struct perf_cpu_context *cpuctx,
  330. int can_add_hw)
  331. {
  332. /*
  333. * Groups consisting entirely of software counters can always go on.
  334. */
  335. if (is_software_only_group(counter))
  336. return 1;
  337. /*
  338. * If an exclusive group is already on, no other hardware
  339. * counters can go on.
  340. */
  341. if (cpuctx->exclusive)
  342. return 0;
  343. /*
  344. * If this group is exclusive and there are already
  345. * counters on the CPU, it can't go on.
  346. */
  347. if (counter->hw_event.exclusive && cpuctx->active_oncpu)
  348. return 0;
  349. /*
  350. * Otherwise, try to add it if all previous groups were able
  351. * to go on.
  352. */
  353. return can_add_hw;
  354. }
  355. /*
  356. * Cross CPU call to install and enable a performance counter
  357. */
  358. static void __perf_install_in_context(void *info)
  359. {
  360. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  361. struct perf_counter *counter = info;
  362. struct perf_counter_context *ctx = counter->ctx;
  363. struct perf_counter *leader = counter->group_leader;
  364. int cpu = smp_processor_id();
  365. unsigned long flags;
  366. u64 perf_flags;
  367. int err;
  368. /*
  369. * If this is a task context, we need to check whether it is
  370. * the current task context of this cpu. If not it has been
  371. * scheduled out before the smp call arrived.
  372. */
  373. if (ctx->task && cpuctx->task_ctx != ctx)
  374. return;
  375. curr_rq_lock_irq_save(&flags);
  376. spin_lock(&ctx->lock);
  377. /*
  378. * Protect the list operation against NMI by disabling the
  379. * counters on a global level. NOP for non NMI based counters.
  380. */
  381. perf_flags = hw_perf_save_disable();
  382. list_add_counter(counter, ctx);
  383. ctx->nr_counters++;
  384. /*
  385. * Don't put the counter on if it is disabled or if
  386. * it is in a group and the group isn't on.
  387. */
  388. if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
  389. (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
  390. goto unlock;
  391. /*
  392. * An exclusive counter can't go on if there are already active
  393. * hardware counters, and no hardware counter can go on if there
  394. * is already an exclusive counter on.
  395. */
  396. if (!group_can_go_on(counter, cpuctx, 1))
  397. err = -EEXIST;
  398. else
  399. err = counter_sched_in(counter, cpuctx, ctx, cpu);
  400. if (err) {
  401. /*
  402. * This counter couldn't go on. If it is in a group
  403. * then we have to pull the whole group off.
  404. * If the counter group is pinned then put it in error state.
  405. */
  406. if (leader != counter)
  407. group_sched_out(leader, cpuctx, ctx);
  408. if (leader->hw_event.pinned)
  409. leader->state = PERF_COUNTER_STATE_ERROR;
  410. }
  411. if (!err && !ctx->task && cpuctx->max_pertask)
  412. cpuctx->max_pertask--;
  413. unlock:
  414. hw_perf_restore(perf_flags);
  415. spin_unlock(&ctx->lock);
  416. curr_rq_unlock_irq_restore(&flags);
  417. }
  418. /*
  419. * Attach a performance counter to a context
  420. *
  421. * First we add the counter to the list with the hardware enable bit
  422. * in counter->hw_config cleared.
  423. *
  424. * If the counter is attached to a task which is on a CPU we use a smp
  425. * call to enable it in the task context. The task might have been
  426. * scheduled away, but we check this in the smp call again.
  427. *
  428. * Must be called with ctx->mutex held.
  429. */
  430. static void
  431. perf_install_in_context(struct perf_counter_context *ctx,
  432. struct perf_counter *counter,
  433. int cpu)
  434. {
  435. struct task_struct *task = ctx->task;
  436. if (!task) {
  437. /*
  438. * Per cpu counters are installed via an smp call and
  439. * the install is always sucessful.
  440. */
  441. smp_call_function_single(cpu, __perf_install_in_context,
  442. counter, 1);
  443. return;
  444. }
  445. counter->task = task;
  446. retry:
  447. task_oncpu_function_call(task, __perf_install_in_context,
  448. counter);
  449. spin_lock_irq(&ctx->lock);
  450. /*
  451. * we need to retry the smp call.
  452. */
  453. if (ctx->is_active && list_empty(&counter->list_entry)) {
  454. spin_unlock_irq(&ctx->lock);
  455. goto retry;
  456. }
  457. /*
  458. * The lock prevents that this context is scheduled in so we
  459. * can add the counter safely, if it the call above did not
  460. * succeed.
  461. */
  462. if (list_empty(&counter->list_entry)) {
  463. list_add_counter(counter, ctx);
  464. ctx->nr_counters++;
  465. }
  466. spin_unlock_irq(&ctx->lock);
  467. }
  468. /*
  469. * Cross CPU call to enable a performance counter
  470. */
  471. static void __perf_counter_enable(void *info)
  472. {
  473. struct perf_counter *counter = info;
  474. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  475. struct perf_counter_context *ctx = counter->ctx;
  476. struct perf_counter *leader = counter->group_leader;
  477. unsigned long flags;
  478. int err;
  479. /*
  480. * If this is a per-task counter, need to check whether this
  481. * counter's task is the current task on this cpu.
  482. */
  483. if (ctx->task && cpuctx->task_ctx != ctx)
  484. return;
  485. curr_rq_lock_irq_save(&flags);
  486. spin_lock(&ctx->lock);
  487. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  488. goto unlock;
  489. counter->state = PERF_COUNTER_STATE_INACTIVE;
  490. /*
  491. * If the counter is in a group and isn't the group leader,
  492. * then don't put it on unless the group is on.
  493. */
  494. if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
  495. goto unlock;
  496. if (!group_can_go_on(counter, cpuctx, 1))
  497. err = -EEXIST;
  498. else
  499. err = counter_sched_in(counter, cpuctx, ctx,
  500. smp_processor_id());
  501. if (err) {
  502. /*
  503. * If this counter can't go on and it's part of a
  504. * group, then the whole group has to come off.
  505. */
  506. if (leader != counter)
  507. group_sched_out(leader, cpuctx, ctx);
  508. if (leader->hw_event.pinned)
  509. leader->state = PERF_COUNTER_STATE_ERROR;
  510. }
  511. unlock:
  512. spin_unlock(&ctx->lock);
  513. curr_rq_unlock_irq_restore(&flags);
  514. }
  515. /*
  516. * Enable a counter.
  517. */
  518. static void perf_counter_enable(struct perf_counter *counter)
  519. {
  520. struct perf_counter_context *ctx = counter->ctx;
  521. struct task_struct *task = ctx->task;
  522. if (!task) {
  523. /*
  524. * Enable the counter on the cpu that it's on
  525. */
  526. smp_call_function_single(counter->cpu, __perf_counter_enable,
  527. counter, 1);
  528. return;
  529. }
  530. spin_lock_irq(&ctx->lock);
  531. if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
  532. goto out;
  533. /*
  534. * If the counter is in error state, clear that first.
  535. * That way, if we see the counter in error state below, we
  536. * know that it has gone back into error state, as distinct
  537. * from the task having been scheduled away before the
  538. * cross-call arrived.
  539. */
  540. if (counter->state == PERF_COUNTER_STATE_ERROR)
  541. counter->state = PERF_COUNTER_STATE_OFF;
  542. retry:
  543. spin_unlock_irq(&ctx->lock);
  544. task_oncpu_function_call(task, __perf_counter_enable, counter);
  545. spin_lock_irq(&ctx->lock);
  546. /*
  547. * If the context is active and the counter is still off,
  548. * we need to retry the cross-call.
  549. */
  550. if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
  551. goto retry;
  552. /*
  553. * Since we have the lock this context can't be scheduled
  554. * in, so we can change the state safely.
  555. */
  556. if (counter->state == PERF_COUNTER_STATE_OFF)
  557. counter->state = PERF_COUNTER_STATE_INACTIVE;
  558. out:
  559. spin_unlock_irq(&ctx->lock);
  560. }
  561. /*
  562. * Enable a counter and all its children.
  563. */
  564. static void perf_counter_enable_family(struct perf_counter *counter)
  565. {
  566. struct perf_counter *child;
  567. perf_counter_enable(counter);
  568. /*
  569. * Lock the mutex to protect the list of children
  570. */
  571. mutex_lock(&counter->mutex);
  572. list_for_each_entry(child, &counter->child_list, child_list)
  573. perf_counter_enable(child);
  574. mutex_unlock(&counter->mutex);
  575. }
  576. void __perf_counter_sched_out(struct perf_counter_context *ctx,
  577. struct perf_cpu_context *cpuctx)
  578. {
  579. struct perf_counter *counter;
  580. u64 flags;
  581. spin_lock(&ctx->lock);
  582. ctx->is_active = 0;
  583. if (likely(!ctx->nr_counters))
  584. goto out;
  585. flags = hw_perf_save_disable();
  586. if (ctx->nr_active) {
  587. list_for_each_entry(counter, &ctx->counter_list, list_entry)
  588. group_sched_out(counter, cpuctx, ctx);
  589. }
  590. hw_perf_restore(flags);
  591. out:
  592. spin_unlock(&ctx->lock);
  593. }
  594. /*
  595. * Called from scheduler to remove the counters of the current task,
  596. * with interrupts disabled.
  597. *
  598. * We stop each counter and update the counter value in counter->count.
  599. *
  600. * This does not protect us against NMI, but disable()
  601. * sets the disabled bit in the control field of counter _before_
  602. * accessing the counter control register. If a NMI hits, then it will
  603. * not restart the counter.
  604. */
  605. void perf_counter_task_sched_out(struct task_struct *task, int cpu)
  606. {
  607. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  608. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  609. if (likely(!cpuctx->task_ctx))
  610. return;
  611. __perf_counter_sched_out(ctx, cpuctx);
  612. cpuctx->task_ctx = NULL;
  613. }
  614. static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
  615. {
  616. __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
  617. }
  618. static int
  619. group_sched_in(struct perf_counter *group_counter,
  620. struct perf_cpu_context *cpuctx,
  621. struct perf_counter_context *ctx,
  622. int cpu)
  623. {
  624. struct perf_counter *counter, *partial_group;
  625. int ret;
  626. if (group_counter->state == PERF_COUNTER_STATE_OFF)
  627. return 0;
  628. ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
  629. if (ret)
  630. return ret < 0 ? ret : 0;
  631. if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
  632. return -EAGAIN;
  633. /*
  634. * Schedule in siblings as one group (if any):
  635. */
  636. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  637. if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
  638. partial_group = counter;
  639. goto group_error;
  640. }
  641. }
  642. return 0;
  643. group_error:
  644. /*
  645. * Groups can be scheduled in as one unit only, so undo any
  646. * partial group before returning:
  647. */
  648. list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
  649. if (counter == partial_group)
  650. break;
  651. counter_sched_out(counter, cpuctx, ctx);
  652. }
  653. counter_sched_out(group_counter, cpuctx, ctx);
  654. return -EAGAIN;
  655. }
  656. static void
  657. __perf_counter_sched_in(struct perf_counter_context *ctx,
  658. struct perf_cpu_context *cpuctx, int cpu)
  659. {
  660. struct perf_counter *counter;
  661. u64 flags;
  662. int can_add_hw = 1;
  663. spin_lock(&ctx->lock);
  664. ctx->is_active = 1;
  665. if (likely(!ctx->nr_counters))
  666. goto out;
  667. flags = hw_perf_save_disable();
  668. /*
  669. * First go through the list and put on any pinned groups
  670. * in order to give them the best chance of going on.
  671. */
  672. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  673. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  674. !counter->hw_event.pinned)
  675. continue;
  676. if (counter->cpu != -1 && counter->cpu != cpu)
  677. continue;
  678. if (group_can_go_on(counter, cpuctx, 1))
  679. group_sched_in(counter, cpuctx, ctx, cpu);
  680. /*
  681. * If this pinned group hasn't been scheduled,
  682. * put it in error state.
  683. */
  684. if (counter->state == PERF_COUNTER_STATE_INACTIVE)
  685. counter->state = PERF_COUNTER_STATE_ERROR;
  686. }
  687. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  688. /*
  689. * Ignore counters in OFF or ERROR state, and
  690. * ignore pinned counters since we did them already.
  691. */
  692. if (counter->state <= PERF_COUNTER_STATE_OFF ||
  693. counter->hw_event.pinned)
  694. continue;
  695. /*
  696. * Listen to the 'cpu' scheduling filter constraint
  697. * of counters:
  698. */
  699. if (counter->cpu != -1 && counter->cpu != cpu)
  700. continue;
  701. if (group_can_go_on(counter, cpuctx, can_add_hw)) {
  702. if (group_sched_in(counter, cpuctx, ctx, cpu))
  703. can_add_hw = 0;
  704. }
  705. }
  706. hw_perf_restore(flags);
  707. out:
  708. spin_unlock(&ctx->lock);
  709. }
  710. /*
  711. * Called from scheduler to add the counters of the current task
  712. * with interrupts disabled.
  713. *
  714. * We restore the counter value and then enable it.
  715. *
  716. * This does not protect us against NMI, but enable()
  717. * sets the enabled bit in the control field of counter _before_
  718. * accessing the counter control register. If a NMI hits, then it will
  719. * keep the counter running.
  720. */
  721. void perf_counter_task_sched_in(struct task_struct *task, int cpu)
  722. {
  723. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  724. struct perf_counter_context *ctx = &task->perf_counter_ctx;
  725. __perf_counter_sched_in(ctx, cpuctx, cpu);
  726. cpuctx->task_ctx = ctx;
  727. }
  728. static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
  729. {
  730. struct perf_counter_context *ctx = &cpuctx->ctx;
  731. __perf_counter_sched_in(ctx, cpuctx, cpu);
  732. }
  733. int perf_counter_task_disable(void)
  734. {
  735. struct task_struct *curr = current;
  736. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  737. struct perf_counter *counter;
  738. unsigned long flags;
  739. u64 perf_flags;
  740. int cpu;
  741. if (likely(!ctx->nr_counters))
  742. return 0;
  743. curr_rq_lock_irq_save(&flags);
  744. cpu = smp_processor_id();
  745. /* force the update of the task clock: */
  746. __task_delta_exec(curr, 1);
  747. perf_counter_task_sched_out(curr, cpu);
  748. spin_lock(&ctx->lock);
  749. /*
  750. * Disable all the counters:
  751. */
  752. perf_flags = hw_perf_save_disable();
  753. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  754. if (counter->state != PERF_COUNTER_STATE_ERROR)
  755. counter->state = PERF_COUNTER_STATE_OFF;
  756. }
  757. hw_perf_restore(perf_flags);
  758. spin_unlock(&ctx->lock);
  759. curr_rq_unlock_irq_restore(&flags);
  760. return 0;
  761. }
  762. int perf_counter_task_enable(void)
  763. {
  764. struct task_struct *curr = current;
  765. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  766. struct perf_counter *counter;
  767. unsigned long flags;
  768. u64 perf_flags;
  769. int cpu;
  770. if (likely(!ctx->nr_counters))
  771. return 0;
  772. curr_rq_lock_irq_save(&flags);
  773. cpu = smp_processor_id();
  774. /* force the update of the task clock: */
  775. __task_delta_exec(curr, 1);
  776. perf_counter_task_sched_out(curr, cpu);
  777. spin_lock(&ctx->lock);
  778. /*
  779. * Disable all the counters:
  780. */
  781. perf_flags = hw_perf_save_disable();
  782. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  783. if (counter->state > PERF_COUNTER_STATE_OFF)
  784. continue;
  785. counter->state = PERF_COUNTER_STATE_INACTIVE;
  786. counter->hw_event.disabled = 0;
  787. }
  788. hw_perf_restore(perf_flags);
  789. spin_unlock(&ctx->lock);
  790. perf_counter_task_sched_in(curr, cpu);
  791. curr_rq_unlock_irq_restore(&flags);
  792. return 0;
  793. }
  794. /*
  795. * Round-robin a context's counters:
  796. */
  797. static void rotate_ctx(struct perf_counter_context *ctx)
  798. {
  799. struct perf_counter *counter;
  800. u64 perf_flags;
  801. if (!ctx->nr_counters)
  802. return;
  803. spin_lock(&ctx->lock);
  804. /*
  805. * Rotate the first entry last (works just fine for group counters too):
  806. */
  807. perf_flags = hw_perf_save_disable();
  808. list_for_each_entry(counter, &ctx->counter_list, list_entry) {
  809. list_del(&counter->list_entry);
  810. list_add_tail(&counter->list_entry, &ctx->counter_list);
  811. break;
  812. }
  813. hw_perf_restore(perf_flags);
  814. spin_unlock(&ctx->lock);
  815. }
  816. void perf_counter_task_tick(struct task_struct *curr, int cpu)
  817. {
  818. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  819. struct perf_counter_context *ctx = &curr->perf_counter_ctx;
  820. const int rotate_percpu = 0;
  821. if (rotate_percpu)
  822. perf_counter_cpu_sched_out(cpuctx);
  823. perf_counter_task_sched_out(curr, cpu);
  824. if (rotate_percpu)
  825. rotate_ctx(&cpuctx->ctx);
  826. rotate_ctx(ctx);
  827. if (rotate_percpu)
  828. perf_counter_cpu_sched_in(cpuctx, cpu);
  829. perf_counter_task_sched_in(curr, cpu);
  830. }
  831. /*
  832. * Cross CPU call to read the hardware counter
  833. */
  834. static void __read(void *info)
  835. {
  836. struct perf_counter *counter = info;
  837. unsigned long flags;
  838. curr_rq_lock_irq_save(&flags);
  839. counter->hw_ops->read(counter);
  840. curr_rq_unlock_irq_restore(&flags);
  841. }
  842. static u64 perf_counter_read(struct perf_counter *counter)
  843. {
  844. /*
  845. * If counter is enabled and currently active on a CPU, update the
  846. * value in the counter structure:
  847. */
  848. if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
  849. smp_call_function_single(counter->oncpu,
  850. __read, counter, 1);
  851. }
  852. return atomic64_read(&counter->count);
  853. }
  854. /*
  855. * Cross CPU call to switch performance data pointers
  856. */
  857. static void __perf_switch_irq_data(void *info)
  858. {
  859. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  860. struct perf_counter *counter = info;
  861. struct perf_counter_context *ctx = counter->ctx;
  862. struct perf_data *oldirqdata = counter->irqdata;
  863. /*
  864. * If this is a task context, we need to check whether it is
  865. * the current task context of this cpu. If not it has been
  866. * scheduled out before the smp call arrived.
  867. */
  868. if (ctx->task) {
  869. if (cpuctx->task_ctx != ctx)
  870. return;
  871. spin_lock(&ctx->lock);
  872. }
  873. /* Change the pointer NMI safe */
  874. atomic_long_set((atomic_long_t *)&counter->irqdata,
  875. (unsigned long) counter->usrdata);
  876. counter->usrdata = oldirqdata;
  877. if (ctx->task)
  878. spin_unlock(&ctx->lock);
  879. }
  880. static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
  881. {
  882. struct perf_counter_context *ctx = counter->ctx;
  883. struct perf_data *oldirqdata = counter->irqdata;
  884. struct task_struct *task = ctx->task;
  885. if (!task) {
  886. smp_call_function_single(counter->cpu,
  887. __perf_switch_irq_data,
  888. counter, 1);
  889. return counter->usrdata;
  890. }
  891. retry:
  892. spin_lock_irq(&ctx->lock);
  893. if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
  894. counter->irqdata = counter->usrdata;
  895. counter->usrdata = oldirqdata;
  896. spin_unlock_irq(&ctx->lock);
  897. return oldirqdata;
  898. }
  899. spin_unlock_irq(&ctx->lock);
  900. task_oncpu_function_call(task, __perf_switch_irq_data, counter);
  901. /* Might have failed, because task was scheduled out */
  902. if (counter->irqdata == oldirqdata)
  903. goto retry;
  904. return counter->usrdata;
  905. }
  906. static void put_context(struct perf_counter_context *ctx)
  907. {
  908. if (ctx->task)
  909. put_task_struct(ctx->task);
  910. }
  911. static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  912. {
  913. struct perf_cpu_context *cpuctx;
  914. struct perf_counter_context *ctx;
  915. struct task_struct *task;
  916. /*
  917. * If cpu is not a wildcard then this is a percpu counter:
  918. */
  919. if (cpu != -1) {
  920. /* Must be root to operate on a CPU counter: */
  921. if (!capable(CAP_SYS_ADMIN))
  922. return ERR_PTR(-EACCES);
  923. if (cpu < 0 || cpu > num_possible_cpus())
  924. return ERR_PTR(-EINVAL);
  925. /*
  926. * We could be clever and allow to attach a counter to an
  927. * offline CPU and activate it when the CPU comes up, but
  928. * that's for later.
  929. */
  930. if (!cpu_isset(cpu, cpu_online_map))
  931. return ERR_PTR(-ENODEV);
  932. cpuctx = &per_cpu(perf_cpu_context, cpu);
  933. ctx = &cpuctx->ctx;
  934. return ctx;
  935. }
  936. rcu_read_lock();
  937. if (!pid)
  938. task = current;
  939. else
  940. task = find_task_by_vpid(pid);
  941. if (task)
  942. get_task_struct(task);
  943. rcu_read_unlock();
  944. if (!task)
  945. return ERR_PTR(-ESRCH);
  946. ctx = &task->perf_counter_ctx;
  947. ctx->task = task;
  948. /* Reuse ptrace permission checks for now. */
  949. if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
  950. put_context(ctx);
  951. return ERR_PTR(-EACCES);
  952. }
  953. return ctx;
  954. }
  955. /*
  956. * Called when the last reference to the file is gone.
  957. */
  958. static int perf_release(struct inode *inode, struct file *file)
  959. {
  960. struct perf_counter *counter = file->private_data;
  961. struct perf_counter_context *ctx = counter->ctx;
  962. file->private_data = NULL;
  963. mutex_lock(&ctx->mutex);
  964. mutex_lock(&counter->mutex);
  965. perf_counter_remove_from_context(counter);
  966. mutex_unlock(&counter->mutex);
  967. mutex_unlock(&ctx->mutex);
  968. kfree(counter);
  969. put_context(ctx);
  970. return 0;
  971. }
  972. /*
  973. * Read the performance counter - simple non blocking version for now
  974. */
  975. static ssize_t
  976. perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
  977. {
  978. u64 cntval;
  979. if (count != sizeof(cntval))
  980. return -EINVAL;
  981. /*
  982. * Return end-of-file for a read on a counter that is in
  983. * error state (i.e. because it was pinned but it couldn't be
  984. * scheduled on to the CPU at some point).
  985. */
  986. if (counter->state == PERF_COUNTER_STATE_ERROR)
  987. return 0;
  988. mutex_lock(&counter->mutex);
  989. cntval = perf_counter_read(counter);
  990. mutex_unlock(&counter->mutex);
  991. return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
  992. }
  993. static ssize_t
  994. perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
  995. {
  996. if (!usrdata->len)
  997. return 0;
  998. count = min(count, (size_t)usrdata->len);
  999. if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
  1000. return -EFAULT;
  1001. /* Adjust the counters */
  1002. usrdata->len -= count;
  1003. if (!usrdata->len)
  1004. usrdata->rd_idx = 0;
  1005. else
  1006. usrdata->rd_idx += count;
  1007. return count;
  1008. }
  1009. static ssize_t
  1010. perf_read_irq_data(struct perf_counter *counter,
  1011. char __user *buf,
  1012. size_t count,
  1013. int nonblocking)
  1014. {
  1015. struct perf_data *irqdata, *usrdata;
  1016. DECLARE_WAITQUEUE(wait, current);
  1017. ssize_t res, res2;
  1018. irqdata = counter->irqdata;
  1019. usrdata = counter->usrdata;
  1020. if (usrdata->len + irqdata->len >= count)
  1021. goto read_pending;
  1022. if (nonblocking)
  1023. return -EAGAIN;
  1024. spin_lock_irq(&counter->waitq.lock);
  1025. __add_wait_queue(&counter->waitq, &wait);
  1026. for (;;) {
  1027. set_current_state(TASK_INTERRUPTIBLE);
  1028. if (usrdata->len + irqdata->len >= count)
  1029. break;
  1030. if (signal_pending(current))
  1031. break;
  1032. if (counter->state == PERF_COUNTER_STATE_ERROR)
  1033. break;
  1034. spin_unlock_irq(&counter->waitq.lock);
  1035. schedule();
  1036. spin_lock_irq(&counter->waitq.lock);
  1037. }
  1038. __remove_wait_queue(&counter->waitq, &wait);
  1039. __set_current_state(TASK_RUNNING);
  1040. spin_unlock_irq(&counter->waitq.lock);
  1041. if (usrdata->len + irqdata->len < count &&
  1042. counter->state != PERF_COUNTER_STATE_ERROR)
  1043. return -ERESTARTSYS;
  1044. read_pending:
  1045. mutex_lock(&counter->mutex);
  1046. /* Drain pending data first: */
  1047. res = perf_copy_usrdata(usrdata, buf, count);
  1048. if (res < 0 || res == count)
  1049. goto out;
  1050. /* Switch irq buffer: */
  1051. usrdata = perf_switch_irq_data(counter);
  1052. res2 = perf_copy_usrdata(usrdata, buf + res, count - res);
  1053. if (res2 < 0) {
  1054. if (!res)
  1055. res = -EFAULT;
  1056. } else {
  1057. res += res2;
  1058. }
  1059. out:
  1060. mutex_unlock(&counter->mutex);
  1061. return res;
  1062. }
  1063. static ssize_t
  1064. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1065. {
  1066. struct perf_counter *counter = file->private_data;
  1067. switch (counter->hw_event.record_type) {
  1068. case PERF_RECORD_SIMPLE:
  1069. return perf_read_hw(counter, buf, count);
  1070. case PERF_RECORD_IRQ:
  1071. case PERF_RECORD_GROUP:
  1072. return perf_read_irq_data(counter, buf, count,
  1073. file->f_flags & O_NONBLOCK);
  1074. }
  1075. return -EINVAL;
  1076. }
  1077. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1078. {
  1079. struct perf_counter *counter = file->private_data;
  1080. unsigned int events = 0;
  1081. unsigned long flags;
  1082. poll_wait(file, &counter->waitq, wait);
  1083. spin_lock_irqsave(&counter->waitq.lock, flags);
  1084. if (counter->usrdata->len || counter->irqdata->len)
  1085. events |= POLLIN;
  1086. spin_unlock_irqrestore(&counter->waitq.lock, flags);
  1087. return events;
  1088. }
  1089. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1090. {
  1091. struct perf_counter *counter = file->private_data;
  1092. int err = 0;
  1093. switch (cmd) {
  1094. case PERF_COUNTER_IOC_ENABLE:
  1095. perf_counter_enable_family(counter);
  1096. break;
  1097. case PERF_COUNTER_IOC_DISABLE:
  1098. perf_counter_disable_family(counter);
  1099. break;
  1100. default:
  1101. err = -ENOTTY;
  1102. }
  1103. return err;
  1104. }
  1105. static const struct file_operations perf_fops = {
  1106. .release = perf_release,
  1107. .read = perf_read,
  1108. .poll = perf_poll,
  1109. .unlocked_ioctl = perf_ioctl,
  1110. .compat_ioctl = perf_ioctl,
  1111. };
  1112. static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
  1113. {
  1114. int cpu = raw_smp_processor_id();
  1115. atomic64_set(&counter->hw.prev_count, cpu_clock(cpu));
  1116. return 0;
  1117. }
  1118. static void cpu_clock_perf_counter_update(struct perf_counter *counter)
  1119. {
  1120. int cpu = raw_smp_processor_id();
  1121. s64 prev;
  1122. u64 now;
  1123. now = cpu_clock(cpu);
  1124. prev = atomic64_read(&counter->hw.prev_count);
  1125. atomic64_set(&counter->hw.prev_count, now);
  1126. atomic64_add(now - prev, &counter->count);
  1127. }
  1128. static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
  1129. {
  1130. cpu_clock_perf_counter_update(counter);
  1131. }
  1132. static void cpu_clock_perf_counter_read(struct perf_counter *counter)
  1133. {
  1134. cpu_clock_perf_counter_update(counter);
  1135. }
  1136. static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
  1137. .enable = cpu_clock_perf_counter_enable,
  1138. .disable = cpu_clock_perf_counter_disable,
  1139. .read = cpu_clock_perf_counter_read,
  1140. };
  1141. /*
  1142. * Called from within the scheduler:
  1143. */
  1144. static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
  1145. {
  1146. struct task_struct *curr = counter->task;
  1147. u64 delta;
  1148. delta = __task_delta_exec(curr, update);
  1149. return curr->se.sum_exec_runtime + delta;
  1150. }
  1151. static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
  1152. {
  1153. u64 prev;
  1154. s64 delta;
  1155. prev = atomic64_read(&counter->hw.prev_count);
  1156. atomic64_set(&counter->hw.prev_count, now);
  1157. delta = now - prev;
  1158. atomic64_add(delta, &counter->count);
  1159. }
  1160. static void task_clock_perf_counter_read(struct perf_counter *counter)
  1161. {
  1162. u64 now = task_clock_perf_counter_val(counter, 1);
  1163. task_clock_perf_counter_update(counter, now);
  1164. }
  1165. static int task_clock_perf_counter_enable(struct perf_counter *counter)
  1166. {
  1167. u64 now = task_clock_perf_counter_val(counter, 0);
  1168. atomic64_set(&counter->hw.prev_count, now);
  1169. return 0;
  1170. }
  1171. static void task_clock_perf_counter_disable(struct perf_counter *counter)
  1172. {
  1173. u64 now = task_clock_perf_counter_val(counter, 0);
  1174. task_clock_perf_counter_update(counter, now);
  1175. }
  1176. static const struct hw_perf_counter_ops perf_ops_task_clock = {
  1177. .enable = task_clock_perf_counter_enable,
  1178. .disable = task_clock_perf_counter_disable,
  1179. .read = task_clock_perf_counter_read,
  1180. };
  1181. #ifdef CONFIG_VM_EVENT_COUNTERS
  1182. #define cpu_page_faults() __get_cpu_var(vm_event_states).event[PGFAULT]
  1183. #else
  1184. #define cpu_page_faults() 0
  1185. #endif
  1186. static u64 get_page_faults(struct perf_counter *counter)
  1187. {
  1188. struct task_struct *curr = counter->ctx->task;
  1189. if (curr)
  1190. return curr->maj_flt + curr->min_flt;
  1191. return cpu_page_faults();
  1192. }
  1193. static void page_faults_perf_counter_update(struct perf_counter *counter)
  1194. {
  1195. u64 prev, now;
  1196. s64 delta;
  1197. prev = atomic64_read(&counter->hw.prev_count);
  1198. now = get_page_faults(counter);
  1199. atomic64_set(&counter->hw.prev_count, now);
  1200. delta = now - prev;
  1201. atomic64_add(delta, &counter->count);
  1202. }
  1203. static void page_faults_perf_counter_read(struct perf_counter *counter)
  1204. {
  1205. page_faults_perf_counter_update(counter);
  1206. }
  1207. static int page_faults_perf_counter_enable(struct perf_counter *counter)
  1208. {
  1209. atomic64_set(&counter->hw.prev_count, get_page_faults(counter));
  1210. return 0;
  1211. }
  1212. static void page_faults_perf_counter_disable(struct perf_counter *counter)
  1213. {
  1214. page_faults_perf_counter_update(counter);
  1215. }
  1216. static const struct hw_perf_counter_ops perf_ops_page_faults = {
  1217. .enable = page_faults_perf_counter_enable,
  1218. .disable = page_faults_perf_counter_disable,
  1219. .read = page_faults_perf_counter_read,
  1220. };
  1221. static u64 get_context_switches(struct perf_counter *counter)
  1222. {
  1223. struct task_struct *curr = counter->ctx->task;
  1224. if (curr)
  1225. return curr->nvcsw + curr->nivcsw;
  1226. return cpu_nr_switches(smp_processor_id());
  1227. }
  1228. static void context_switches_perf_counter_update(struct perf_counter *counter)
  1229. {
  1230. u64 prev, now;
  1231. s64 delta;
  1232. prev = atomic64_read(&counter->hw.prev_count);
  1233. now = get_context_switches(counter);
  1234. atomic64_set(&counter->hw.prev_count, now);
  1235. delta = now - prev;
  1236. atomic64_add(delta, &counter->count);
  1237. }
  1238. static void context_switches_perf_counter_read(struct perf_counter *counter)
  1239. {
  1240. context_switches_perf_counter_update(counter);
  1241. }
  1242. static int context_switches_perf_counter_enable(struct perf_counter *counter)
  1243. {
  1244. atomic64_set(&counter->hw.prev_count, get_context_switches(counter));
  1245. return 0;
  1246. }
  1247. static void context_switches_perf_counter_disable(struct perf_counter *counter)
  1248. {
  1249. context_switches_perf_counter_update(counter);
  1250. }
  1251. static const struct hw_perf_counter_ops perf_ops_context_switches = {
  1252. .enable = context_switches_perf_counter_enable,
  1253. .disable = context_switches_perf_counter_disable,
  1254. .read = context_switches_perf_counter_read,
  1255. };
  1256. static inline u64 get_cpu_migrations(struct perf_counter *counter)
  1257. {
  1258. struct task_struct *curr = counter->ctx->task;
  1259. if (curr)
  1260. return curr->se.nr_migrations;
  1261. return cpu_nr_migrations(smp_processor_id());
  1262. }
  1263. static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
  1264. {
  1265. u64 prev, now;
  1266. s64 delta;
  1267. prev = atomic64_read(&counter->hw.prev_count);
  1268. now = get_cpu_migrations(counter);
  1269. atomic64_set(&counter->hw.prev_count, now);
  1270. delta = now - prev;
  1271. atomic64_add(delta, &counter->count);
  1272. }
  1273. static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
  1274. {
  1275. cpu_migrations_perf_counter_update(counter);
  1276. }
  1277. static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
  1278. {
  1279. atomic64_set(&counter->hw.prev_count, get_cpu_migrations(counter));
  1280. return 0;
  1281. }
  1282. static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
  1283. {
  1284. cpu_migrations_perf_counter_update(counter);
  1285. }
  1286. static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
  1287. .enable = cpu_migrations_perf_counter_enable,
  1288. .disable = cpu_migrations_perf_counter_disable,
  1289. .read = cpu_migrations_perf_counter_read,
  1290. };
  1291. static const struct hw_perf_counter_ops *
  1292. sw_perf_counter_init(struct perf_counter *counter)
  1293. {
  1294. const struct hw_perf_counter_ops *hw_ops = NULL;
  1295. /*
  1296. * Software counters (currently) can't in general distinguish
  1297. * between user, kernel and hypervisor events.
  1298. * However, context switches and cpu migrations are considered
  1299. * to be kernel events, and page faults are never hypervisor
  1300. * events.
  1301. */
  1302. switch (counter->hw_event.type) {
  1303. case PERF_COUNT_CPU_CLOCK:
  1304. if (!(counter->hw_event.exclude_user ||
  1305. counter->hw_event.exclude_kernel ||
  1306. counter->hw_event.exclude_hv))
  1307. hw_ops = &perf_ops_cpu_clock;
  1308. break;
  1309. case PERF_COUNT_TASK_CLOCK:
  1310. if (counter->hw_event.exclude_user ||
  1311. counter->hw_event.exclude_kernel ||
  1312. counter->hw_event.exclude_hv)
  1313. break;
  1314. /*
  1315. * If the user instantiates this as a per-cpu counter,
  1316. * use the cpu_clock counter instead.
  1317. */
  1318. if (counter->ctx->task)
  1319. hw_ops = &perf_ops_task_clock;
  1320. else
  1321. hw_ops = &perf_ops_cpu_clock;
  1322. break;
  1323. case PERF_COUNT_PAGE_FAULTS:
  1324. if (!(counter->hw_event.exclude_user ||
  1325. counter->hw_event.exclude_kernel))
  1326. hw_ops = &perf_ops_page_faults;
  1327. break;
  1328. case PERF_COUNT_CONTEXT_SWITCHES:
  1329. if (!counter->hw_event.exclude_kernel)
  1330. hw_ops = &perf_ops_context_switches;
  1331. break;
  1332. case PERF_COUNT_CPU_MIGRATIONS:
  1333. if (!counter->hw_event.exclude_kernel)
  1334. hw_ops = &perf_ops_cpu_migrations;
  1335. break;
  1336. default:
  1337. break;
  1338. }
  1339. return hw_ops;
  1340. }
  1341. /*
  1342. * Allocate and initialize a counter structure
  1343. */
  1344. static struct perf_counter *
  1345. perf_counter_alloc(struct perf_counter_hw_event *hw_event,
  1346. int cpu,
  1347. struct perf_counter_context *ctx,
  1348. struct perf_counter *group_leader,
  1349. gfp_t gfpflags)
  1350. {
  1351. const struct hw_perf_counter_ops *hw_ops;
  1352. struct perf_counter *counter;
  1353. counter = kzalloc(sizeof(*counter), gfpflags);
  1354. if (!counter)
  1355. return NULL;
  1356. /*
  1357. * Single counters are their own group leaders, with an
  1358. * empty sibling list:
  1359. */
  1360. if (!group_leader)
  1361. group_leader = counter;
  1362. mutex_init(&counter->mutex);
  1363. INIT_LIST_HEAD(&counter->list_entry);
  1364. INIT_LIST_HEAD(&counter->sibling_list);
  1365. init_waitqueue_head(&counter->waitq);
  1366. INIT_LIST_HEAD(&counter->child_list);
  1367. counter->irqdata = &counter->data[0];
  1368. counter->usrdata = &counter->data[1];
  1369. counter->cpu = cpu;
  1370. counter->hw_event = *hw_event;
  1371. counter->wakeup_pending = 0;
  1372. counter->group_leader = group_leader;
  1373. counter->hw_ops = NULL;
  1374. counter->ctx = ctx;
  1375. counter->state = PERF_COUNTER_STATE_INACTIVE;
  1376. if (hw_event->disabled)
  1377. counter->state = PERF_COUNTER_STATE_OFF;
  1378. hw_ops = NULL;
  1379. if (!hw_event->raw && hw_event->type < 0)
  1380. hw_ops = sw_perf_counter_init(counter);
  1381. else
  1382. hw_ops = hw_perf_counter_init(counter);
  1383. if (!hw_ops) {
  1384. kfree(counter);
  1385. return NULL;
  1386. }
  1387. counter->hw_ops = hw_ops;
  1388. return counter;
  1389. }
  1390. /**
  1391. * sys_perf_task_open - open a performance counter, associate it to a task/cpu
  1392. *
  1393. * @hw_event_uptr: event type attributes for monitoring/sampling
  1394. * @pid: target pid
  1395. * @cpu: target cpu
  1396. * @group_fd: group leader counter fd
  1397. */
  1398. asmlinkage int
  1399. sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
  1400. pid_t pid, int cpu, int group_fd)
  1401. {
  1402. struct perf_counter *counter, *group_leader;
  1403. struct perf_counter_hw_event hw_event;
  1404. struct perf_counter_context *ctx;
  1405. struct file *counter_file = NULL;
  1406. struct file *group_file = NULL;
  1407. int fput_needed = 0;
  1408. int fput_needed2 = 0;
  1409. int ret;
  1410. if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
  1411. return -EFAULT;
  1412. /*
  1413. * Get the target context (task or percpu):
  1414. */
  1415. ctx = find_get_context(pid, cpu);
  1416. if (IS_ERR(ctx))
  1417. return PTR_ERR(ctx);
  1418. /*
  1419. * Look up the group leader (we will attach this counter to it):
  1420. */
  1421. group_leader = NULL;
  1422. if (group_fd != -1) {
  1423. ret = -EINVAL;
  1424. group_file = fget_light(group_fd, &fput_needed);
  1425. if (!group_file)
  1426. goto err_put_context;
  1427. if (group_file->f_op != &perf_fops)
  1428. goto err_put_context;
  1429. group_leader = group_file->private_data;
  1430. /*
  1431. * Do not allow a recursive hierarchy (this new sibling
  1432. * becoming part of another group-sibling):
  1433. */
  1434. if (group_leader->group_leader != group_leader)
  1435. goto err_put_context;
  1436. /*
  1437. * Do not allow to attach to a group in a different
  1438. * task or CPU context:
  1439. */
  1440. if (group_leader->ctx != ctx)
  1441. goto err_put_context;
  1442. /*
  1443. * Only a group leader can be exclusive or pinned
  1444. */
  1445. if (hw_event.exclusive || hw_event.pinned)
  1446. goto err_put_context;
  1447. }
  1448. ret = -EINVAL;
  1449. counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
  1450. GFP_KERNEL);
  1451. if (!counter)
  1452. goto err_put_context;
  1453. ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
  1454. if (ret < 0)
  1455. goto err_free_put_context;
  1456. counter_file = fget_light(ret, &fput_needed2);
  1457. if (!counter_file)
  1458. goto err_free_put_context;
  1459. counter->filp = counter_file;
  1460. mutex_lock(&ctx->mutex);
  1461. perf_install_in_context(ctx, counter, cpu);
  1462. mutex_unlock(&ctx->mutex);
  1463. fput_light(counter_file, fput_needed2);
  1464. out_fput:
  1465. fput_light(group_file, fput_needed);
  1466. return ret;
  1467. err_free_put_context:
  1468. kfree(counter);
  1469. err_put_context:
  1470. put_context(ctx);
  1471. goto out_fput;
  1472. }
  1473. /*
  1474. * Initialize the perf_counter context in a task_struct:
  1475. */
  1476. static void
  1477. __perf_counter_init_context(struct perf_counter_context *ctx,
  1478. struct task_struct *task)
  1479. {
  1480. memset(ctx, 0, sizeof(*ctx));
  1481. spin_lock_init(&ctx->lock);
  1482. mutex_init(&ctx->mutex);
  1483. INIT_LIST_HEAD(&ctx->counter_list);
  1484. ctx->task = task;
  1485. }
  1486. /*
  1487. * inherit a counter from parent task to child task:
  1488. */
  1489. static struct perf_counter *
  1490. inherit_counter(struct perf_counter *parent_counter,
  1491. struct task_struct *parent,
  1492. struct perf_counter_context *parent_ctx,
  1493. struct task_struct *child,
  1494. struct perf_counter *group_leader,
  1495. struct perf_counter_context *child_ctx)
  1496. {
  1497. struct perf_counter *child_counter;
  1498. /*
  1499. * Instead of creating recursive hierarchies of counters,
  1500. * we link inherited counters back to the original parent,
  1501. * which has a filp for sure, which we use as the reference
  1502. * count:
  1503. */
  1504. if (parent_counter->parent)
  1505. parent_counter = parent_counter->parent;
  1506. child_counter = perf_counter_alloc(&parent_counter->hw_event,
  1507. parent_counter->cpu, child_ctx,
  1508. group_leader, GFP_KERNEL);
  1509. if (!child_counter)
  1510. return NULL;
  1511. /*
  1512. * Link it up in the child's context:
  1513. */
  1514. child_counter->task = child;
  1515. list_add_counter(child_counter, child_ctx);
  1516. child_ctx->nr_counters++;
  1517. child_counter->parent = parent_counter;
  1518. /*
  1519. * inherit into child's child as well:
  1520. */
  1521. child_counter->hw_event.inherit = 1;
  1522. /*
  1523. * Get a reference to the parent filp - we will fput it
  1524. * when the child counter exits. This is safe to do because
  1525. * we are in the parent and we know that the filp still
  1526. * exists and has a nonzero count:
  1527. */
  1528. atomic_long_inc(&parent_counter->filp->f_count);
  1529. /*
  1530. * Link this into the parent counter's child list
  1531. */
  1532. mutex_lock(&parent_counter->mutex);
  1533. list_add_tail(&child_counter->child_list, &parent_counter->child_list);
  1534. /*
  1535. * Make the child state follow the state of the parent counter,
  1536. * not its hw_event.disabled bit. We hold the parent's mutex,
  1537. * so we won't race with perf_counter_{en,dis}able_family.
  1538. */
  1539. if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
  1540. child_counter->state = PERF_COUNTER_STATE_INACTIVE;
  1541. else
  1542. child_counter->state = PERF_COUNTER_STATE_OFF;
  1543. mutex_unlock(&parent_counter->mutex);
  1544. return child_counter;
  1545. }
  1546. static int inherit_group(struct perf_counter *parent_counter,
  1547. struct task_struct *parent,
  1548. struct perf_counter_context *parent_ctx,
  1549. struct task_struct *child,
  1550. struct perf_counter_context *child_ctx)
  1551. {
  1552. struct perf_counter *leader;
  1553. struct perf_counter *sub;
  1554. leader = inherit_counter(parent_counter, parent, parent_ctx,
  1555. child, NULL, child_ctx);
  1556. if (!leader)
  1557. return -ENOMEM;
  1558. list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
  1559. if (!inherit_counter(sub, parent, parent_ctx,
  1560. child, leader, child_ctx))
  1561. return -ENOMEM;
  1562. }
  1563. return 0;
  1564. }
  1565. static void sync_child_counter(struct perf_counter *child_counter,
  1566. struct perf_counter *parent_counter)
  1567. {
  1568. u64 parent_val, child_val;
  1569. parent_val = atomic64_read(&parent_counter->count);
  1570. child_val = atomic64_read(&child_counter->count);
  1571. /*
  1572. * Add back the child's count to the parent's count:
  1573. */
  1574. atomic64_add(child_val, &parent_counter->count);
  1575. /*
  1576. * Remove this counter from the parent's list
  1577. */
  1578. mutex_lock(&parent_counter->mutex);
  1579. list_del_init(&child_counter->child_list);
  1580. mutex_unlock(&parent_counter->mutex);
  1581. /*
  1582. * Release the parent counter, if this was the last
  1583. * reference to it.
  1584. */
  1585. fput(parent_counter->filp);
  1586. }
  1587. static void
  1588. __perf_counter_exit_task(struct task_struct *child,
  1589. struct perf_counter *child_counter,
  1590. struct perf_counter_context *child_ctx)
  1591. {
  1592. struct perf_counter *parent_counter;
  1593. struct perf_counter *sub, *tmp;
  1594. /*
  1595. * If we do not self-reap then we have to wait for the
  1596. * child task to unschedule (it will happen for sure),
  1597. * so that its counter is at its final count. (This
  1598. * condition triggers rarely - child tasks usually get
  1599. * off their CPU before the parent has a chance to
  1600. * get this far into the reaping action)
  1601. */
  1602. if (child != current) {
  1603. wait_task_inactive(child, 0);
  1604. list_del_init(&child_counter->list_entry);
  1605. } else {
  1606. struct perf_cpu_context *cpuctx;
  1607. unsigned long flags;
  1608. u64 perf_flags;
  1609. /*
  1610. * Disable and unlink this counter.
  1611. *
  1612. * Be careful about zapping the list - IRQ/NMI context
  1613. * could still be processing it:
  1614. */
  1615. curr_rq_lock_irq_save(&flags);
  1616. perf_flags = hw_perf_save_disable();
  1617. cpuctx = &__get_cpu_var(perf_cpu_context);
  1618. group_sched_out(child_counter, cpuctx, child_ctx);
  1619. list_del_init(&child_counter->list_entry);
  1620. child_ctx->nr_counters--;
  1621. hw_perf_restore(perf_flags);
  1622. curr_rq_unlock_irq_restore(&flags);
  1623. }
  1624. parent_counter = child_counter->parent;
  1625. /*
  1626. * It can happen that parent exits first, and has counters
  1627. * that are still around due to the child reference. These
  1628. * counters need to be zapped - but otherwise linger.
  1629. */
  1630. if (parent_counter) {
  1631. sync_child_counter(child_counter, parent_counter);
  1632. list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
  1633. list_entry) {
  1634. if (sub->parent) {
  1635. sync_child_counter(sub, sub->parent);
  1636. kfree(sub);
  1637. }
  1638. }
  1639. kfree(child_counter);
  1640. }
  1641. }
  1642. /*
  1643. * When a child task exits, feed back counter values to parent counters.
  1644. *
  1645. * Note: we may be running in child context, but the PID is not hashed
  1646. * anymore so new counters will not be added.
  1647. */
  1648. void perf_counter_exit_task(struct task_struct *child)
  1649. {
  1650. struct perf_counter *child_counter, *tmp;
  1651. struct perf_counter_context *child_ctx;
  1652. child_ctx = &child->perf_counter_ctx;
  1653. if (likely(!child_ctx->nr_counters))
  1654. return;
  1655. list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
  1656. list_entry)
  1657. __perf_counter_exit_task(child, child_counter, child_ctx);
  1658. }
  1659. /*
  1660. * Initialize the perf_counter context in task_struct
  1661. */
  1662. void perf_counter_init_task(struct task_struct *child)
  1663. {
  1664. struct perf_counter_context *child_ctx, *parent_ctx;
  1665. struct perf_counter *counter;
  1666. struct task_struct *parent = current;
  1667. child_ctx = &child->perf_counter_ctx;
  1668. parent_ctx = &parent->perf_counter_ctx;
  1669. __perf_counter_init_context(child_ctx, child);
  1670. /*
  1671. * This is executed from the parent task context, so inherit
  1672. * counters that have been marked for cloning:
  1673. */
  1674. if (likely(!parent_ctx->nr_counters))
  1675. return;
  1676. /*
  1677. * Lock the parent list. No need to lock the child - not PID
  1678. * hashed yet and not running, so nobody can access it.
  1679. */
  1680. mutex_lock(&parent_ctx->mutex);
  1681. /*
  1682. * We dont have to disable NMIs - we are only looking at
  1683. * the list, not manipulating it:
  1684. */
  1685. list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
  1686. if (!counter->hw_event.inherit)
  1687. continue;
  1688. if (inherit_group(counter, parent,
  1689. parent_ctx, child, child_ctx))
  1690. break;
  1691. }
  1692. mutex_unlock(&parent_ctx->mutex);
  1693. }
  1694. static void __cpuinit perf_counter_init_cpu(int cpu)
  1695. {
  1696. struct perf_cpu_context *cpuctx;
  1697. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1698. __perf_counter_init_context(&cpuctx->ctx, NULL);
  1699. mutex_lock(&perf_resource_mutex);
  1700. cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
  1701. mutex_unlock(&perf_resource_mutex);
  1702. hw_perf_counter_setup(cpu);
  1703. }
  1704. #ifdef CONFIG_HOTPLUG_CPU
  1705. static void __perf_counter_exit_cpu(void *info)
  1706. {
  1707. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1708. struct perf_counter_context *ctx = &cpuctx->ctx;
  1709. struct perf_counter *counter, *tmp;
  1710. list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
  1711. __perf_counter_remove_from_context(counter);
  1712. }
  1713. static void perf_counter_exit_cpu(int cpu)
  1714. {
  1715. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  1716. struct perf_counter_context *ctx = &cpuctx->ctx;
  1717. mutex_lock(&ctx->mutex);
  1718. smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
  1719. mutex_unlock(&ctx->mutex);
  1720. }
  1721. #else
  1722. static inline void perf_counter_exit_cpu(int cpu) { }
  1723. #endif
  1724. static int __cpuinit
  1725. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  1726. {
  1727. unsigned int cpu = (long)hcpu;
  1728. switch (action) {
  1729. case CPU_UP_PREPARE:
  1730. case CPU_UP_PREPARE_FROZEN:
  1731. perf_counter_init_cpu(cpu);
  1732. break;
  1733. case CPU_DOWN_PREPARE:
  1734. case CPU_DOWN_PREPARE_FROZEN:
  1735. perf_counter_exit_cpu(cpu);
  1736. break;
  1737. default:
  1738. break;
  1739. }
  1740. return NOTIFY_OK;
  1741. }
  1742. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  1743. .notifier_call = perf_cpu_notify,
  1744. };
  1745. static int __init perf_counter_init(void)
  1746. {
  1747. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  1748. (void *)(long)smp_processor_id());
  1749. register_cpu_notifier(&perf_cpu_nb);
  1750. return 0;
  1751. }
  1752. early_initcall(perf_counter_init);
  1753. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  1754. {
  1755. return sprintf(buf, "%d\n", perf_reserved_percpu);
  1756. }
  1757. static ssize_t
  1758. perf_set_reserve_percpu(struct sysdev_class *class,
  1759. const char *buf,
  1760. size_t count)
  1761. {
  1762. struct perf_cpu_context *cpuctx;
  1763. unsigned long val;
  1764. int err, cpu, mpt;
  1765. err = strict_strtoul(buf, 10, &val);
  1766. if (err)
  1767. return err;
  1768. if (val > perf_max_counters)
  1769. return -EINVAL;
  1770. mutex_lock(&perf_resource_mutex);
  1771. perf_reserved_percpu = val;
  1772. for_each_online_cpu(cpu) {
  1773. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1774. spin_lock_irq(&cpuctx->ctx.lock);
  1775. mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
  1776. perf_max_counters - perf_reserved_percpu);
  1777. cpuctx->max_pertask = mpt;
  1778. spin_unlock_irq(&cpuctx->ctx.lock);
  1779. }
  1780. mutex_unlock(&perf_resource_mutex);
  1781. return count;
  1782. }
  1783. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  1784. {
  1785. return sprintf(buf, "%d\n", perf_overcommit);
  1786. }
  1787. static ssize_t
  1788. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  1789. {
  1790. unsigned long val;
  1791. int err;
  1792. err = strict_strtoul(buf, 10, &val);
  1793. if (err)
  1794. return err;
  1795. if (val > 1)
  1796. return -EINVAL;
  1797. mutex_lock(&perf_resource_mutex);
  1798. perf_overcommit = val;
  1799. mutex_unlock(&perf_resource_mutex);
  1800. return count;
  1801. }
  1802. static SYSDEV_CLASS_ATTR(
  1803. reserve_percpu,
  1804. 0644,
  1805. perf_show_reserve_percpu,
  1806. perf_set_reserve_percpu
  1807. );
  1808. static SYSDEV_CLASS_ATTR(
  1809. overcommit,
  1810. 0644,
  1811. perf_show_overcommit,
  1812. perf_set_overcommit
  1813. );
  1814. static struct attribute *perfclass_attrs[] = {
  1815. &attr_reserve_percpu.attr,
  1816. &attr_overcommit.attr,
  1817. NULL
  1818. };
  1819. static struct attribute_group perfclass_attr_group = {
  1820. .attrs = perfclass_attrs,
  1821. .name = "perf_counters",
  1822. };
  1823. static int __init perf_counter_sysfs_init(void)
  1824. {
  1825. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  1826. &perfclass_attr_group);
  1827. }
  1828. device_initcall(perf_counter_sysfs_init);