perf_counter.c 57 KB

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