taskstats.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. struct kmem_cache *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. 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, &family, 0, cmd);
  77. } else
  78. reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
  79. if (reply == NULL) {
  80. nlmsg_free(skb);
  81. return -EINVAL;
  82. }
  83. *skbp = skb;
  84. return 0;
  85. }
  86. /*
  87. * Send taskstats data in @skb to listener with nl_pid @pid
  88. */
  89. static int send_reply(struct sk_buff *skb, pid_t pid)
  90. {
  91. struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
  92. void *reply = genlmsg_data(genlhdr);
  93. int rc;
  94. rc = genlmsg_end(skb, reply);
  95. if (rc < 0) {
  96. nlmsg_free(skb);
  97. return rc;
  98. }
  99. return genlmsg_unicast(skb, pid);
  100. }
  101. /*
  102. * Send taskstats data in @skb to listeners registered for @cpu's exit data
  103. */
  104. static void send_cpu_listeners(struct sk_buff *skb,
  105. struct listener_list *listeners)
  106. {
  107. struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
  108. struct listener *s, *tmp;
  109. struct sk_buff *skb_next, *skb_cur = skb;
  110. void *reply = genlmsg_data(genlhdr);
  111. int rc, delcount = 0;
  112. rc = genlmsg_end(skb, reply);
  113. if (rc < 0) {
  114. nlmsg_free(skb);
  115. return;
  116. }
  117. rc = 0;
  118. down_read(&listeners->sem);
  119. list_for_each_entry(s, &listeners->list, list) {
  120. skb_next = NULL;
  121. if (!list_is_last(&s->list, &listeners->list)) {
  122. skb_next = skb_clone(skb_cur, GFP_KERNEL);
  123. if (!skb_next)
  124. break;
  125. }
  126. rc = genlmsg_unicast(skb_cur, s->pid);
  127. if (rc == -ECONNREFUSED) {
  128. s->valid = 0;
  129. delcount++;
  130. }
  131. skb_cur = skb_next;
  132. }
  133. up_read(&listeners->sem);
  134. if (skb_cur)
  135. nlmsg_free(skb_cur);
  136. if (!delcount)
  137. return;
  138. /* Delete invalidated entries */
  139. down_write(&listeners->sem);
  140. list_for_each_entry_safe(s, tmp, &listeners->list, list) {
  141. if (!s->valid) {
  142. list_del(&s->list);
  143. kfree(s);
  144. }
  145. }
  146. up_write(&listeners->sem);
  147. }
  148. static int fill_pid(pid_t pid, struct task_struct *tsk,
  149. struct taskstats *stats)
  150. {
  151. int rc = 0;
  152. if (!tsk) {
  153. rcu_read_lock();
  154. tsk = find_task_by_pid(pid);
  155. if (tsk)
  156. get_task_struct(tsk);
  157. rcu_read_unlock();
  158. if (!tsk)
  159. return -ESRCH;
  160. } else
  161. get_task_struct(tsk);
  162. memset(stats, 0, sizeof(*stats));
  163. /*
  164. * Each accounting subsystem adds calls to its functions to
  165. * fill in relevant parts of struct taskstsats as follows
  166. *
  167. * per-task-foo(stats, tsk);
  168. */
  169. delayacct_add_tsk(stats, tsk);
  170. /* fill in basic acct fields */
  171. stats->version = TASKSTATS_VERSION;
  172. bacct_add_tsk(stats, tsk);
  173. /* fill in extended acct fields */
  174. xacct_add_tsk(stats, tsk);
  175. /* Define err: label here if needed */
  176. put_task_struct(tsk);
  177. return rc;
  178. }
  179. static int fill_tgid(pid_t tgid, struct task_struct *first,
  180. struct taskstats *stats)
  181. {
  182. struct task_struct *tsk;
  183. unsigned long flags;
  184. int rc = -ESRCH;
  185. /*
  186. * Add additional stats from live tasks except zombie thread group
  187. * leaders who are already counted with the dead tasks
  188. */
  189. rcu_read_lock();
  190. if (!first)
  191. first = find_task_by_pid(tgid);
  192. if (!first || !lock_task_sighand(first, &flags))
  193. goto out;
  194. if (first->signal->stats)
  195. memcpy(stats, first->signal->stats, sizeof(*stats));
  196. else
  197. memset(stats, 0, sizeof(*stats));
  198. tsk = first;
  199. do {
  200. if (tsk->exit_state)
  201. continue;
  202. /*
  203. * Accounting subsystem can call its functions here to
  204. * fill in relevant parts of struct taskstsats as follows
  205. *
  206. * per-task-foo(stats, tsk);
  207. */
  208. delayacct_add_tsk(stats, tsk);
  209. } while_each_thread(first, tsk);
  210. unlock_task_sighand(first, &flags);
  211. rc = 0;
  212. out:
  213. rcu_read_unlock();
  214. stats->version = TASKSTATS_VERSION;
  215. /*
  216. * Accounting subsytems can also add calls here to modify
  217. * fields of taskstats.
  218. */
  219. return rc;
  220. }
  221. static void fill_tgid_exit(struct task_struct *tsk)
  222. {
  223. unsigned long flags;
  224. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  225. if (!tsk->signal->stats)
  226. goto ret;
  227. /*
  228. * Each accounting subsystem calls its functions here to
  229. * accumalate its per-task stats for tsk, into the per-tgid structure
  230. *
  231. * per-task-foo(tsk->signal->stats, tsk);
  232. */
  233. delayacct_add_tsk(tsk->signal->stats, tsk);
  234. ret:
  235. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  236. return;
  237. }
  238. static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
  239. {
  240. struct listener_list *listeners;
  241. struct listener *s, *tmp;
  242. unsigned int cpu;
  243. cpumask_t mask = *maskp;
  244. if (!cpus_subset(mask, cpu_possible_map))
  245. return -EINVAL;
  246. if (isadd == REGISTER) {
  247. for_each_cpu_mask(cpu, mask) {
  248. s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
  249. cpu_to_node(cpu));
  250. if (!s)
  251. goto cleanup;
  252. s->pid = pid;
  253. INIT_LIST_HEAD(&s->list);
  254. s->valid = 1;
  255. listeners = &per_cpu(listener_array, cpu);
  256. down_write(&listeners->sem);
  257. list_add(&s->list, &listeners->list);
  258. up_write(&listeners->sem);
  259. }
  260. return 0;
  261. }
  262. /* Deregister or cleanup */
  263. cleanup:
  264. for_each_cpu_mask(cpu, mask) {
  265. listeners = &per_cpu(listener_array, cpu);
  266. down_write(&listeners->sem);
  267. list_for_each_entry_safe(s, tmp, &listeners->list, list) {
  268. if (s->pid == pid) {
  269. list_del(&s->list);
  270. kfree(s);
  271. break;
  272. }
  273. }
  274. up_write(&listeners->sem);
  275. }
  276. return 0;
  277. }
  278. static int parse(struct nlattr *na, cpumask_t *mask)
  279. {
  280. char *data;
  281. int len;
  282. int ret;
  283. if (na == NULL)
  284. return 1;
  285. len = nla_len(na);
  286. if (len > TASKSTATS_CPUMASK_MAXLEN)
  287. return -E2BIG;
  288. if (len < 1)
  289. return -EINVAL;
  290. data = kmalloc(len, GFP_KERNEL);
  291. if (!data)
  292. return -ENOMEM;
  293. nla_strlcpy(data, na, len);
  294. ret = cpulist_parse(data, *mask);
  295. kfree(data);
  296. return ret;
  297. }
  298. static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
  299. {
  300. struct nlattr *na, *ret;
  301. int aggr;
  302. aggr = (type == TASKSTATS_TYPE_PID)
  303. ? TASKSTATS_TYPE_AGGR_PID
  304. : TASKSTATS_TYPE_AGGR_TGID;
  305. na = nla_nest_start(skb, aggr);
  306. if (!na)
  307. goto err;
  308. if (nla_put(skb, type, sizeof(pid), &pid) < 0)
  309. goto err;
  310. ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
  311. if (!ret)
  312. goto err;
  313. nla_nest_end(skb, na);
  314. return nla_data(ret);
  315. err:
  316. return NULL;
  317. }
  318. static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
  319. {
  320. int rc = 0;
  321. struct sk_buff *rep_skb;
  322. struct taskstats *stats;
  323. size_t size;
  324. cpumask_t mask;
  325. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
  326. if (rc < 0)
  327. return rc;
  328. if (rc == 0)
  329. return add_del_listener(info->snd_pid, &mask, REGISTER);
  330. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
  331. if (rc < 0)
  332. return rc;
  333. if (rc == 0)
  334. return add_del_listener(info->snd_pid, &mask, DEREGISTER);
  335. /*
  336. * Size includes space for nested attributes
  337. */
  338. size = nla_total_size(sizeof(u32)) +
  339. nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
  340. rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
  341. if (rc < 0)
  342. return rc;
  343. rc = -EINVAL;
  344. if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
  345. u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
  346. stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
  347. if (!stats)
  348. goto err;
  349. rc = fill_pid(pid, NULL, stats);
  350. if (rc < 0)
  351. goto err;
  352. } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
  353. u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
  354. stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
  355. if (!stats)
  356. goto err;
  357. rc = fill_tgid(tgid, NULL, stats);
  358. if (rc < 0)
  359. goto err;
  360. } else
  361. goto err;
  362. return send_reply(rep_skb, info->snd_pid);
  363. err:
  364. nlmsg_free(rep_skb);
  365. return rc;
  366. }
  367. static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
  368. {
  369. struct signal_struct *sig = tsk->signal;
  370. struct taskstats *stats;
  371. if (sig->stats || thread_group_empty(tsk))
  372. goto ret;
  373. /* No problem if kmem_cache_zalloc() fails */
  374. stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
  375. spin_lock_irq(&tsk->sighand->siglock);
  376. if (!sig->stats) {
  377. sig->stats = stats;
  378. stats = NULL;
  379. }
  380. spin_unlock_irq(&tsk->sighand->siglock);
  381. if (stats)
  382. kmem_cache_free(taskstats_cache, stats);
  383. ret:
  384. return sig->stats;
  385. }
  386. /* Send pid data out on exit */
  387. void taskstats_exit(struct task_struct *tsk, int group_dead)
  388. {
  389. int rc;
  390. struct listener_list *listeners;
  391. struct taskstats *stats;
  392. struct sk_buff *rep_skb;
  393. size_t size;
  394. int is_thread_group;
  395. if (!family_registered)
  396. return;
  397. /*
  398. * Size includes space for nested attributes
  399. */
  400. size = nla_total_size(sizeof(u32)) +
  401. nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
  402. is_thread_group = !!taskstats_tgid_alloc(tsk);
  403. if (is_thread_group) {
  404. /* PID + STATS + TGID + STATS */
  405. size = 2 * size;
  406. /* fill the tsk->signal->stats structure */
  407. fill_tgid_exit(tsk);
  408. }
  409. listeners = &__raw_get_cpu_var(listener_array);
  410. if (list_empty(&listeners->list))
  411. return;
  412. rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
  413. if (rc < 0)
  414. return;
  415. stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid);
  416. if (!stats)
  417. goto err;
  418. rc = fill_pid(tsk->pid, tsk, stats);
  419. if (rc < 0)
  420. goto err;
  421. /*
  422. * Doesn't matter if tsk is the leader or the last group member leaving
  423. */
  424. if (!is_thread_group || !group_dead)
  425. goto send;
  426. stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid);
  427. if (!stats)
  428. goto err;
  429. memcpy(stats, tsk->signal->stats, sizeof(*stats));
  430. send:
  431. send_cpu_listeners(rep_skb, listeners);
  432. return;
  433. err:
  434. nlmsg_free(rep_skb);
  435. }
  436. static struct genl_ops taskstats_ops = {
  437. .cmd = TASKSTATS_CMD_GET,
  438. .doit = taskstats_user_cmd,
  439. .policy = taskstats_cmd_get_policy,
  440. };
  441. /* Needed early in initialization */
  442. void __init taskstats_init_early(void)
  443. {
  444. unsigned int i;
  445. taskstats_cache = kmem_cache_create("taskstats_cache",
  446. sizeof(struct taskstats),
  447. 0, SLAB_PANIC, NULL, NULL);
  448. for_each_possible_cpu(i) {
  449. INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
  450. init_rwsem(&(per_cpu(listener_array, i).sem));
  451. }
  452. }
  453. static int __init taskstats_init(void)
  454. {
  455. int rc;
  456. rc = genl_register_family(&family);
  457. if (rc)
  458. return rc;
  459. rc = genl_register_ops(&family, &taskstats_ops);
  460. if (rc < 0)
  461. goto err;
  462. family_registered = 1;
  463. return 0;
  464. err:
  465. genl_unregister_family(&family);
  466. return rc;
  467. }
  468. /*
  469. * late initcall ensures initialization of statistics collection
  470. * mechanisms precedes initialization of the taskstats interface
  471. */
  472. late_initcall(taskstats_init);