taskstats.c 13 KB

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