taskstats.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * taskstats.c - Export per-task statistics to userland
  3. *
  4. * Copyright (C) Shailabh Nagar, IBM Corp. 2006
  5. * (C) Balbir Singh, IBM Corp. 2006
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/taskstats_kern.h>
  20. #include <linux/tsacct_kern.h>
  21. #include <linux/delayacct.h>
  22. #include <linux/tsacct_kern.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/percpu.h>
  25. #include <net/genetlink.h>
  26. #include <asm/atomic.h>
  27. /*
  28. * Maximum length of a cpumask that can be specified in
  29. * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
  30. */
  31. #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
  32. static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
  33. static int family_registered;
  34. kmem_cache_t *taskstats_cache;
  35. static struct genl_family family = {
  36. .id = GENL_ID_GENERATE,
  37. .name = TASKSTATS_GENL_NAME,
  38. .version = TASKSTATS_GENL_VERSION,
  39. .maxattr = TASKSTATS_CMD_ATTR_MAX,
  40. };
  41. static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
  42. __read_mostly = {
  43. [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 },
  44. [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
  45. [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
  46. [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
  47. struct listener {
  48. struct list_head list;
  49. pid_t pid;
  50. char valid;
  51. };
  52. struct listener_list {
  53. struct rw_semaphore sem;
  54. struct list_head list;
  55. };
  56. static DEFINE_PER_CPU(struct listener_list, listener_array);
  57. enum actions {
  58. REGISTER,
  59. DEREGISTER,
  60. CPU_DONT_CARE
  61. };
  62. static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
  63. void **replyp, size_t size)
  64. {
  65. struct sk_buff *skb;
  66. void *reply;
  67. /*
  68. * If new attributes are added, please revisit this allocation
  69. */
  70. skb = genlmsg_new(size, GFP_KERNEL);
  71. if (!skb)
  72. return -ENOMEM;
  73. if (!info) {
  74. int seq = get_cpu_var(taskstats_seqnum)++;
  75. put_cpu_var(taskstats_seqnum);
  76. reply = genlmsg_put(skb, 0, seq,
  77. family.id, 0, 0,
  78. cmd, family.version);
  79. } else
  80. reply = genlmsg_put(skb, info->snd_pid, info->snd_seq,
  81. family.id, 0, 0,
  82. cmd, family.version);
  83. if (reply == NULL) {
  84. nlmsg_free(skb);
  85. return -EINVAL;
  86. }
  87. *skbp = skb;
  88. *replyp = reply;
  89. return 0;
  90. }
  91. /*
  92. * Send taskstats data in @skb to listener with nl_pid @pid
  93. */
  94. static int send_reply(struct sk_buff *skb, pid_t pid)
  95. {
  96. struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
  97. void *reply = genlmsg_data(genlhdr);
  98. int rc;
  99. rc = genlmsg_end(skb, reply);
  100. if (rc < 0) {
  101. nlmsg_free(skb);
  102. return rc;
  103. }
  104. return genlmsg_unicast(skb, pid);
  105. }
  106. /*
  107. * Send taskstats data in @skb to listeners registered for @cpu's exit data
  108. */
  109. static void send_cpu_listeners(struct sk_buff *skb, unsigned int cpu)
  110. {
  111. struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
  112. struct listener_list *listeners;
  113. struct listener *s, *tmp;
  114. struct sk_buff *skb_next, *skb_cur = skb;
  115. void *reply = genlmsg_data(genlhdr);
  116. int rc, delcount = 0;
  117. rc = genlmsg_end(skb, reply);
  118. if (rc < 0) {
  119. nlmsg_free(skb);
  120. return;
  121. }
  122. rc = 0;
  123. listeners = &per_cpu(listener_array, cpu);
  124. down_read(&listeners->sem);
  125. list_for_each_entry(s, &listeners->list, list) {
  126. skb_next = NULL;
  127. if (!list_is_last(&s->list, &listeners->list)) {
  128. skb_next = skb_clone(skb_cur, GFP_KERNEL);
  129. if (!skb_next)
  130. break;
  131. }
  132. rc = genlmsg_unicast(skb_cur, s->pid);
  133. if (rc == -ECONNREFUSED) {
  134. s->valid = 0;
  135. delcount++;
  136. }
  137. skb_cur = skb_next;
  138. }
  139. up_read(&listeners->sem);
  140. if (skb_cur)
  141. nlmsg_free(skb_cur);
  142. if (!delcount)
  143. return;
  144. /* Delete invalidated entries */
  145. down_write(&listeners->sem);
  146. list_for_each_entry_safe(s, tmp, &listeners->list, list) {
  147. if (!s->valid) {
  148. list_del(&s->list);
  149. kfree(s);
  150. }
  151. }
  152. up_write(&listeners->sem);
  153. }
  154. static int fill_pid(pid_t pid, struct task_struct *tsk,
  155. struct taskstats *stats)
  156. {
  157. int rc = 0;
  158. if (!tsk) {
  159. rcu_read_lock();
  160. tsk = find_task_by_pid(pid);
  161. if (tsk)
  162. get_task_struct(tsk);
  163. rcu_read_unlock();
  164. if (!tsk)
  165. return -ESRCH;
  166. } else
  167. get_task_struct(tsk);
  168. /*
  169. * Each accounting subsystem adds calls to its functions to
  170. * fill in relevant parts of struct taskstsats as follows
  171. *
  172. * per-task-foo(stats, tsk);
  173. */
  174. delayacct_add_tsk(stats, tsk);
  175. /* fill in basic acct fields */
  176. stats->version = TASKSTATS_VERSION;
  177. bacct_add_tsk(stats, tsk);
  178. /* fill in extended acct fields */
  179. xacct_add_tsk(stats, tsk);
  180. /* Define err: label here if needed */
  181. put_task_struct(tsk);
  182. return rc;
  183. }
  184. static int fill_tgid(pid_t tgid, struct task_struct *first,
  185. struct taskstats *stats)
  186. {
  187. struct task_struct *tsk;
  188. unsigned long flags;
  189. int rc = -ESRCH;
  190. /*
  191. * Add additional stats from live tasks except zombie thread group
  192. * leaders who are already counted with the dead tasks
  193. */
  194. rcu_read_lock();
  195. if (!first)
  196. first = find_task_by_pid(tgid);
  197. if (!first || !lock_task_sighand(first, &flags))
  198. goto out;
  199. if (first->signal->stats)
  200. memcpy(stats, first->signal->stats, sizeof(*stats));
  201. tsk = first;
  202. do {
  203. if (tsk->exit_state)
  204. continue;
  205. /*
  206. * Accounting subsystem can call its functions here to
  207. * fill in relevant parts of struct taskstsats as follows
  208. *
  209. * per-task-foo(stats, tsk);
  210. */
  211. delayacct_add_tsk(stats, tsk);
  212. } while_each_thread(first, tsk);
  213. unlock_task_sighand(first, &flags);
  214. rc = 0;
  215. out:
  216. rcu_read_unlock();
  217. stats->version = TASKSTATS_VERSION;
  218. /*
  219. * Accounting subsytems can also add calls here to modify
  220. * fields of taskstats.
  221. */
  222. return rc;
  223. }
  224. static void fill_tgid_exit(struct task_struct *tsk)
  225. {
  226. unsigned long flags;
  227. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  228. if (!tsk->signal->stats)
  229. goto ret;
  230. /*
  231. * Each accounting subsystem calls its functions here to
  232. * accumalate its per-task stats for tsk, into the per-tgid structure
  233. *
  234. * per-task-foo(tsk->signal->stats, tsk);
  235. */
  236. delayacct_add_tsk(tsk->signal->stats, tsk);
  237. ret:
  238. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  239. return;
  240. }
  241. static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
  242. {
  243. struct listener_list *listeners;
  244. struct listener *s, *tmp;
  245. unsigned int cpu;
  246. cpumask_t mask = *maskp;
  247. if (!cpus_subset(mask, cpu_possible_map))
  248. return -EINVAL;
  249. if (isadd == REGISTER) {
  250. for_each_cpu_mask(cpu, mask) {
  251. s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
  252. cpu_to_node(cpu));
  253. if (!s)
  254. goto cleanup;
  255. s->pid = pid;
  256. INIT_LIST_HEAD(&s->list);
  257. s->valid = 1;
  258. listeners = &per_cpu(listener_array, cpu);
  259. down_write(&listeners->sem);
  260. list_add(&s->list, &listeners->list);
  261. up_write(&listeners->sem);
  262. }
  263. return 0;
  264. }
  265. /* Deregister or cleanup */
  266. cleanup:
  267. for_each_cpu_mask(cpu, mask) {
  268. listeners = &per_cpu(listener_array, cpu);
  269. down_write(&listeners->sem);
  270. list_for_each_entry_safe(s, tmp, &listeners->list, list) {
  271. if (s->pid == pid) {
  272. list_del(&s->list);
  273. kfree(s);
  274. break;
  275. }
  276. }
  277. up_write(&listeners->sem);
  278. }
  279. return 0;
  280. }
  281. static int parse(struct nlattr *na, cpumask_t *mask)
  282. {
  283. char *data;
  284. int len;
  285. int ret;
  286. if (na == NULL)
  287. return 1;
  288. len = nla_len(na);
  289. if (len > TASKSTATS_CPUMASK_MAXLEN)
  290. return -E2BIG;
  291. if (len < 1)
  292. return -EINVAL;
  293. data = kmalloc(len, GFP_KERNEL);
  294. if (!data)
  295. return -ENOMEM;
  296. nla_strlcpy(data, na, len);
  297. ret = cpulist_parse(data, *mask);
  298. kfree(data);
  299. return ret;
  300. }
  301. static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
  302. {
  303. int rc = 0;
  304. struct sk_buff *rep_skb;
  305. struct taskstats stats;
  306. void *reply;
  307. size_t size;
  308. struct nlattr *na;
  309. cpumask_t mask;
  310. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
  311. if (rc < 0)
  312. return rc;
  313. if (rc == 0)
  314. return add_del_listener(info->snd_pid, &mask, REGISTER);
  315. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
  316. if (rc < 0)
  317. return rc;
  318. if (rc == 0)
  319. return add_del_listener(info->snd_pid, &mask, DEREGISTER);
  320. /*
  321. * Size includes space for nested attributes
  322. */
  323. size = nla_total_size(sizeof(u32)) +
  324. nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
  325. memset(&stats, 0, sizeof(stats));
  326. rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
  327. if (rc < 0)
  328. return rc;
  329. if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
  330. u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
  331. rc = fill_pid(pid, NULL, &stats);
  332. if (rc < 0)
  333. goto err;
  334. na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
  335. NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
  336. NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
  337. stats);
  338. } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
  339. u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
  340. rc = fill_tgid(tgid, NULL, &stats);
  341. if (rc < 0)
  342. goto err;
  343. na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
  344. NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
  345. NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
  346. stats);
  347. } else {
  348. rc = -EINVAL;
  349. goto err;
  350. }
  351. nla_nest_end(rep_skb, na);
  352. return send_reply(rep_skb, info->snd_pid);
  353. nla_put_failure:
  354. rc = genlmsg_cancel(rep_skb, reply);
  355. err:
  356. nlmsg_free(rep_skb);
  357. return rc;
  358. }
  359. void taskstats_exit_alloc(struct taskstats **ptidstats, unsigned int *mycpu)
  360. {
  361. struct listener_list *listeners;
  362. struct taskstats *tmp;
  363. /*
  364. * This is the cpu on which the task is exiting currently and will
  365. * be the one for which the exit event is sent, even if the cpu
  366. * on which this function is running changes later.
  367. */
  368. *mycpu = raw_smp_processor_id();
  369. *ptidstats = NULL;
  370. tmp = kmem_cache_zalloc(taskstats_cache, SLAB_KERNEL);
  371. if (!tmp)
  372. return;
  373. listeners = &per_cpu(listener_array, *mycpu);
  374. down_read(&listeners->sem);
  375. if (!list_empty(&listeners->list)) {
  376. *ptidstats = tmp;
  377. tmp = NULL;
  378. }
  379. up_read(&listeners->sem);
  380. kfree(tmp);
  381. }
  382. /* Send pid data out on exit */
  383. void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats,
  384. int group_dead, unsigned int mycpu)
  385. {
  386. int rc;
  387. struct sk_buff *rep_skb;
  388. void *reply;
  389. size_t size;
  390. int is_thread_group;
  391. struct nlattr *na;
  392. if (!family_registered)
  393. return;
  394. /*
  395. * Size includes space for nested attributes
  396. */
  397. size = nla_total_size(sizeof(u32)) +
  398. nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
  399. is_thread_group = (tsk->signal->stats != NULL);
  400. if (is_thread_group) {
  401. /* PID + STATS + TGID + STATS */
  402. size = 2 * size;
  403. /* fill the tsk->signal->stats structure */
  404. fill_tgid_exit(tsk);
  405. }
  406. if (!tidstats)
  407. return;
  408. rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
  409. if (rc < 0)
  410. goto ret;
  411. rc = fill_pid(tsk->pid, tsk, tidstats);
  412. if (rc < 0)
  413. goto err_skb;
  414. na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
  415. NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
  416. NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
  417. *tidstats);
  418. nla_nest_end(rep_skb, na);
  419. if (!is_thread_group)
  420. goto send;
  421. /*
  422. * Doesn't matter if tsk is the leader or the last group member leaving
  423. */
  424. if (!group_dead)
  425. goto send;
  426. na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
  427. NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
  428. /* No locking needed for tsk->signal->stats since group is dead */
  429. NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
  430. *tsk->signal->stats);
  431. nla_nest_end(rep_skb, na);
  432. send:
  433. send_cpu_listeners(rep_skb, mycpu);
  434. return;
  435. nla_put_failure:
  436. genlmsg_cancel(rep_skb, reply);
  437. err_skb:
  438. nlmsg_free(rep_skb);
  439. ret:
  440. return;
  441. }
  442. static struct genl_ops taskstats_ops = {
  443. .cmd = TASKSTATS_CMD_GET,
  444. .doit = taskstats_user_cmd,
  445. .policy = taskstats_cmd_get_policy,
  446. };
  447. /* Needed early in initialization */
  448. void __init taskstats_init_early(void)
  449. {
  450. unsigned int i;
  451. taskstats_cache = kmem_cache_create("taskstats_cache",
  452. sizeof(struct taskstats),
  453. 0, SLAB_PANIC, NULL, NULL);
  454. for_each_possible_cpu(i) {
  455. INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
  456. init_rwsem(&(per_cpu(listener_array, i).sem));
  457. }
  458. }
  459. static int __init taskstats_init(void)
  460. {
  461. int rc;
  462. rc = genl_register_family(&family);
  463. if (rc)
  464. return rc;
  465. rc = genl_register_ops(&family, &taskstats_ops);
  466. if (rc < 0)
  467. goto err;
  468. family_registered = 1;
  469. return 0;
  470. err:
  471. genl_unregister_family(&family);
  472. return rc;
  473. }
  474. /*
  475. * late initcall ensures initialization of statistics collection
  476. * mechanisms precedes initialization of the taskstats interface
  477. */
  478. late_initcall(taskstats_init);