perf_counter.c 53 KB

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