perf_counter.c 57 KB

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