perf_counter.c 51 KB

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