taskstats.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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/cpumask.h>
  23. #include <linux/percpu.h>
  24. #include <linux/slab.h>
  25. #include <linux/cgroupstats.h>
  26. #include <linux/cgroup.h>
  27. #include <linux/fs.h>
  28. #include <linux/file.h>
  29. #include <linux/pid_namespace.h>
  30. #include <net/genetlink.h>
  31. #include <linux/atomic.h>
  32. /*
  33. * Maximum length of a cpumask that can be specified in
  34. * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
  35. */
  36. #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
  37. static DEFINE_PER_CPU(__u32, taskstats_seqnum);
  38. static int family_registered;
  39. struct kmem_cache *taskstats_cache;
  40. static struct genl_family family = {
  41. .id = GENL_ID_GENERATE,
  42. .name = TASKSTATS_GENL_NAME,
  43. .version = TASKSTATS_GENL_VERSION,
  44. .maxattr = TASKSTATS_CMD_ATTR_MAX,
  45. };
  46. static const struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] = {
  47. [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 },
  48. [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
  49. [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
  50. [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
  51. /*
  52. * We have to use TASKSTATS_CMD_ATTR_MAX here, it is the maxattr in the family.
  53. * Make sure they are always aligned.
  54. */
  55. static const struct nla_policy cgroupstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1] = {
  56. [CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 },
  57. };
  58. struct listener {
  59. struct list_head list;
  60. pid_t pid;
  61. char valid;
  62. };
  63. struct listener_list {
  64. struct rw_semaphore sem;
  65. struct list_head list;
  66. };
  67. static DEFINE_PER_CPU(struct listener_list, listener_array);
  68. enum actions {
  69. REGISTER,
  70. DEREGISTER,
  71. CPU_DONT_CARE
  72. };
  73. static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
  74. size_t size)
  75. {
  76. struct sk_buff *skb;
  77. void *reply;
  78. /*
  79. * If new attributes are added, please revisit this allocation
  80. */
  81. skb = genlmsg_new(size, GFP_KERNEL);
  82. if (!skb)
  83. return -ENOMEM;
  84. if (!info) {
  85. int seq = this_cpu_inc_return(taskstats_seqnum) - 1;
  86. reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
  87. } else
  88. reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
  89. if (reply == NULL) {
  90. nlmsg_free(skb);
  91. return -EINVAL;
  92. }
  93. *skbp = skb;
  94. return 0;
  95. }
  96. /*
  97. * Send taskstats data in @skb to listener with nl_pid @pid
  98. */
  99. static int send_reply(struct sk_buff *skb, struct genl_info *info)
  100. {
  101. struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
  102. void *reply = genlmsg_data(genlhdr);
  103. genlmsg_end(skb, reply);
  104. return genlmsg_reply(skb, info);
  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,
  110. struct listener_list *listeners)
  111. {
  112. struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
  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. genlmsg_end(skb, reply);
  118. rc = 0;
  119. down_read(&listeners->sem);
  120. list_for_each_entry(s, &listeners->list, list) {
  121. skb_next = NULL;
  122. if (!list_is_last(&s->list, &listeners->list)) {
  123. skb_next = skb_clone(skb_cur, GFP_KERNEL);
  124. if (!skb_next)
  125. break;
  126. }
  127. rc = genlmsg_unicast(&init_net, skb_cur, s->pid);
  128. if (rc == -ECONNREFUSED) {
  129. s->valid = 0;
  130. delcount++;
  131. }
  132. skb_cur = skb_next;
  133. }
  134. up_read(&listeners->sem);
  135. if (skb_cur)
  136. nlmsg_free(skb_cur);
  137. if (!delcount)
  138. return;
  139. /* Delete invalidated entries */
  140. down_write(&listeners->sem);
  141. list_for_each_entry_safe(s, tmp, &listeners->list, list) {
  142. if (!s->valid) {
  143. list_del(&s->list);
  144. kfree(s);
  145. }
  146. }
  147. up_write(&listeners->sem);
  148. }
  149. static void fill_stats(struct user_namespace *user_ns,
  150. struct pid_namespace *pid_ns,
  151. struct task_struct *tsk, struct taskstats *stats)
  152. {
  153. memset(stats, 0, sizeof(*stats));
  154. /*
  155. * Each accounting subsystem adds calls to its functions to
  156. * fill in relevant parts of struct taskstsats as follows
  157. *
  158. * per-task-foo(stats, tsk);
  159. */
  160. delayacct_add_tsk(stats, tsk);
  161. /* fill in basic acct fields */
  162. stats->version = TASKSTATS_VERSION;
  163. stats->nvcsw = tsk->nvcsw;
  164. stats->nivcsw = tsk->nivcsw;
  165. bacct_add_tsk(user_ns, pid_ns, stats, tsk);
  166. /* fill in extended acct fields */
  167. xacct_add_tsk(stats, tsk);
  168. }
  169. static int fill_stats_for_pid(pid_t pid, struct taskstats *stats)
  170. {
  171. struct task_struct *tsk;
  172. rcu_read_lock();
  173. tsk = find_task_by_vpid(pid);
  174. if (tsk)
  175. get_task_struct(tsk);
  176. rcu_read_unlock();
  177. if (!tsk)
  178. return -ESRCH;
  179. fill_stats(current_user_ns(), task_active_pid_ns(current), tsk, stats);
  180. put_task_struct(tsk);
  181. return 0;
  182. }
  183. static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
  184. {
  185. struct task_struct *tsk, *first;
  186. unsigned long flags;
  187. int rc = -ESRCH;
  188. /*
  189. * Add additional stats from live tasks except zombie thread group
  190. * leaders who are already counted with the dead tasks
  191. */
  192. rcu_read_lock();
  193. first = find_task_by_vpid(tgid);
  194. if (!first || !lock_task_sighand(first, &flags))
  195. goto out;
  196. if (first->signal->stats)
  197. memcpy(stats, first->signal->stats, sizeof(*stats));
  198. else
  199. memset(stats, 0, sizeof(*stats));
  200. tsk = first;
  201. do {
  202. if (tsk->exit_state)
  203. continue;
  204. /*
  205. * Accounting subsystem can call its functions here to
  206. * fill in relevant parts of struct taskstsats as follows
  207. *
  208. * per-task-foo(stats, tsk);
  209. */
  210. delayacct_add_tsk(stats, tsk);
  211. stats->nvcsw += tsk->nvcsw;
  212. stats->nivcsw += tsk->nivcsw;
  213. } while_each_thread(first, tsk);
  214. unlock_task_sighand(first, &flags);
  215. rc = 0;
  216. out:
  217. rcu_read_unlock();
  218. stats->version = TASKSTATS_VERSION;
  219. /*
  220. * Accounting subsystems can also add calls here to modify
  221. * fields of taskstats.
  222. */
  223. return rc;
  224. }
  225. static void fill_tgid_exit(struct task_struct *tsk)
  226. {
  227. unsigned long flags;
  228. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  229. if (!tsk->signal->stats)
  230. goto ret;
  231. /*
  232. * Each accounting subsystem calls its functions here to
  233. * accumalate its per-task stats for tsk, into the per-tgid structure
  234. *
  235. * per-task-foo(tsk->signal->stats, tsk);
  236. */
  237. delayacct_add_tsk(tsk->signal->stats, tsk);
  238. ret:
  239. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  240. return;
  241. }
  242. static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd)
  243. {
  244. struct listener_list *listeners;
  245. struct listener *s, *tmp, *s2;
  246. unsigned int cpu;
  247. int ret = 0;
  248. if (!cpumask_subset(mask, cpu_possible_mask))
  249. return -EINVAL;
  250. if (current_user_ns() != &init_user_ns)
  251. return -EINVAL;
  252. if (task_active_pid_ns(current) != &init_pid_ns)
  253. return -EINVAL;
  254. if (isadd == REGISTER) {
  255. for_each_cpu(cpu, mask) {
  256. s = kmalloc_node(sizeof(struct listener),
  257. GFP_KERNEL, cpu_to_node(cpu));
  258. if (!s) {
  259. ret = -ENOMEM;
  260. goto cleanup;
  261. }
  262. s->pid = pid;
  263. s->valid = 1;
  264. listeners = &per_cpu(listener_array, cpu);
  265. down_write(&listeners->sem);
  266. list_for_each_entry(s2, &listeners->list, list) {
  267. if (s2->pid == pid && s2->valid)
  268. goto exists;
  269. }
  270. list_add(&s->list, &listeners->list);
  271. s = NULL;
  272. exists:
  273. up_write(&listeners->sem);
  274. kfree(s); /* nop if NULL */
  275. }
  276. return 0;
  277. }
  278. /* Deregister or cleanup */
  279. cleanup:
  280. for_each_cpu(cpu, mask) {
  281. listeners = &per_cpu(listener_array, cpu);
  282. down_write(&listeners->sem);
  283. list_for_each_entry_safe(s, tmp, &listeners->list, list) {
  284. if (s->pid == pid) {
  285. list_del(&s->list);
  286. kfree(s);
  287. break;
  288. }
  289. }
  290. up_write(&listeners->sem);
  291. }
  292. return ret;
  293. }
  294. static int parse(struct nlattr *na, struct cpumask *mask)
  295. {
  296. char *data;
  297. int len;
  298. int ret;
  299. if (na == NULL)
  300. return 1;
  301. len = nla_len(na);
  302. if (len > TASKSTATS_CPUMASK_MAXLEN)
  303. return -E2BIG;
  304. if (len < 1)
  305. return -EINVAL;
  306. data = kmalloc(len, GFP_KERNEL);
  307. if (!data)
  308. return -ENOMEM;
  309. nla_strlcpy(data, na, len);
  310. ret = cpulist_parse(data, mask);
  311. kfree(data);
  312. return ret;
  313. }
  314. static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
  315. {
  316. struct nlattr *na, *ret;
  317. int aggr;
  318. aggr = (type == TASKSTATS_TYPE_PID)
  319. ? TASKSTATS_TYPE_AGGR_PID
  320. : TASKSTATS_TYPE_AGGR_TGID;
  321. na = nla_nest_start(skb, aggr);
  322. if (!na)
  323. goto err;
  324. if (nla_put(skb, type, sizeof(pid), &pid) < 0) {
  325. nla_nest_cancel(skb, na);
  326. goto err;
  327. }
  328. ret = nla_reserve_64bit(skb, TASKSTATS_TYPE_STATS,
  329. sizeof(struct taskstats), TASKSTATS_TYPE_NULL);
  330. if (!ret) {
  331. nla_nest_cancel(skb, na);
  332. goto err;
  333. }
  334. nla_nest_end(skb, na);
  335. return nla_data(ret);
  336. err:
  337. return NULL;
  338. }
  339. static int cgroupstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
  340. {
  341. int rc = 0;
  342. struct sk_buff *rep_skb;
  343. struct cgroupstats *stats;
  344. struct nlattr *na;
  345. size_t size;
  346. u32 fd;
  347. struct fd f;
  348. na = info->attrs[CGROUPSTATS_CMD_ATTR_FD];
  349. if (!na)
  350. return -EINVAL;
  351. fd = nla_get_u32(info->attrs[CGROUPSTATS_CMD_ATTR_FD]);
  352. f = fdget(fd);
  353. if (!f.file)
  354. return 0;
  355. size = nla_total_size(sizeof(struct cgroupstats));
  356. rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb,
  357. size);
  358. if (rc < 0)
  359. goto err;
  360. na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS,
  361. sizeof(struct cgroupstats));
  362. if (na == NULL) {
  363. nlmsg_free(rep_skb);
  364. rc = -EMSGSIZE;
  365. goto err;
  366. }
  367. stats = nla_data(na);
  368. memset(stats, 0, sizeof(*stats));
  369. rc = cgroupstats_build(stats, f.file->f_path.dentry);
  370. if (rc < 0) {
  371. nlmsg_free(rep_skb);
  372. goto err;
  373. }
  374. rc = send_reply(rep_skb, info);
  375. err:
  376. fdput(f);
  377. return rc;
  378. }
  379. static int cmd_attr_register_cpumask(struct genl_info *info)
  380. {
  381. cpumask_var_t mask;
  382. int rc;
  383. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  384. return -ENOMEM;
  385. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], mask);
  386. if (rc < 0)
  387. goto out;
  388. rc = add_del_listener(info->snd_portid, mask, REGISTER);
  389. out:
  390. free_cpumask_var(mask);
  391. return rc;
  392. }
  393. static int cmd_attr_deregister_cpumask(struct genl_info *info)
  394. {
  395. cpumask_var_t mask;
  396. int rc;
  397. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  398. return -ENOMEM;
  399. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], mask);
  400. if (rc < 0)
  401. goto out;
  402. rc = add_del_listener(info->snd_portid, mask, DEREGISTER);
  403. out:
  404. free_cpumask_var(mask);
  405. return rc;
  406. }
  407. static size_t taskstats_packet_size(void)
  408. {
  409. size_t size;
  410. size = nla_total_size(sizeof(u32)) +
  411. nla_total_size_64bit(sizeof(struct taskstats)) +
  412. nla_total_size(0);
  413. return size;
  414. }
  415. static int cmd_attr_pid(struct genl_info *info)
  416. {
  417. struct taskstats *stats;
  418. struct sk_buff *rep_skb;
  419. size_t size;
  420. u32 pid;
  421. int rc;
  422. size = taskstats_packet_size();
  423. rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
  424. if (rc < 0)
  425. return rc;
  426. rc = -EINVAL;
  427. pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
  428. stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
  429. if (!stats)
  430. goto err;
  431. rc = fill_stats_for_pid(pid, stats);
  432. if (rc < 0)
  433. goto err;
  434. return send_reply(rep_skb, info);
  435. err:
  436. nlmsg_free(rep_skb);
  437. return rc;
  438. }
  439. static int cmd_attr_tgid(struct genl_info *info)
  440. {
  441. struct taskstats *stats;
  442. struct sk_buff *rep_skb;
  443. size_t size;
  444. u32 tgid;
  445. int rc;
  446. size = taskstats_packet_size();
  447. rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
  448. if (rc < 0)
  449. return rc;
  450. rc = -EINVAL;
  451. tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
  452. stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
  453. if (!stats)
  454. goto err;
  455. rc = fill_stats_for_tgid(tgid, stats);
  456. if (rc < 0)
  457. goto err;
  458. return send_reply(rep_skb, info);
  459. err:
  460. nlmsg_free(rep_skb);
  461. return rc;
  462. }
  463. static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
  464. {
  465. if (info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK])
  466. return cmd_attr_register_cpumask(info);
  467. else if (info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK])
  468. return cmd_attr_deregister_cpumask(info);
  469. else if (info->attrs[TASKSTATS_CMD_ATTR_PID])
  470. return cmd_attr_pid(info);
  471. else if (info->attrs[TASKSTATS_CMD_ATTR_TGID])
  472. return cmd_attr_tgid(info);
  473. else
  474. return -EINVAL;
  475. }
  476. static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
  477. {
  478. struct signal_struct *sig = tsk->signal;
  479. struct taskstats *stats;
  480. if (sig->stats || thread_group_empty(tsk))
  481. goto ret;
  482. /* No problem if kmem_cache_zalloc() fails */
  483. stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
  484. spin_lock_irq(&tsk->sighand->siglock);
  485. if (!sig->stats) {
  486. sig->stats = stats;
  487. stats = NULL;
  488. }
  489. spin_unlock_irq(&tsk->sighand->siglock);
  490. if (stats)
  491. kmem_cache_free(taskstats_cache, stats);
  492. ret:
  493. return sig->stats;
  494. }
  495. /* Send pid data out on exit */
  496. void taskstats_exit(struct task_struct *tsk, int group_dead)
  497. {
  498. int rc;
  499. struct listener_list *listeners;
  500. struct taskstats *stats;
  501. struct sk_buff *rep_skb;
  502. size_t size;
  503. int is_thread_group;
  504. if (!family_registered)
  505. return;
  506. /*
  507. * Size includes space for nested attributes
  508. */
  509. size = taskstats_packet_size();
  510. is_thread_group = !!taskstats_tgid_alloc(tsk);
  511. if (is_thread_group) {
  512. /* PID + STATS + TGID + STATS */
  513. size = 2 * size;
  514. /* fill the tsk->signal->stats structure */
  515. fill_tgid_exit(tsk);
  516. }
  517. listeners = raw_cpu_ptr(&listener_array);
  518. if (list_empty(&listeners->list))
  519. return;
  520. rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
  521. if (rc < 0)
  522. return;
  523. stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID,
  524. task_pid_nr_ns(tsk, &init_pid_ns));
  525. if (!stats)
  526. goto err;
  527. fill_stats(&init_user_ns, &init_pid_ns, tsk, stats);
  528. /*
  529. * Doesn't matter if tsk is the leader or the last group member leaving
  530. */
  531. if (!is_thread_group || !group_dead)
  532. goto send;
  533. stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID,
  534. task_tgid_nr_ns(tsk, &init_pid_ns));
  535. if (!stats)
  536. goto err;
  537. memcpy(stats, tsk->signal->stats, sizeof(*stats));
  538. send:
  539. send_cpu_listeners(rep_skb, listeners);
  540. return;
  541. err:
  542. nlmsg_free(rep_skb);
  543. }
  544. static const struct genl_ops taskstats_ops[] = {
  545. {
  546. .cmd = TASKSTATS_CMD_GET,
  547. .doit = taskstats_user_cmd,
  548. .policy = taskstats_cmd_get_policy,
  549. .flags = GENL_ADMIN_PERM,
  550. },
  551. {
  552. .cmd = CGROUPSTATS_CMD_GET,
  553. .doit = cgroupstats_user_cmd,
  554. .policy = cgroupstats_cmd_get_policy,
  555. },
  556. };
  557. /* Needed early in initialization */
  558. void __init taskstats_init_early(void)
  559. {
  560. unsigned int i;
  561. taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
  562. for_each_possible_cpu(i) {
  563. INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
  564. init_rwsem(&(per_cpu(listener_array, i).sem));
  565. }
  566. }
  567. static int __init taskstats_init(void)
  568. {
  569. int rc;
  570. rc = genl_register_family_with_ops(&family, taskstats_ops);
  571. if (rc)
  572. return rc;
  573. family_registered = 1;
  574. pr_info("registered taskstats version %d\n", TASKSTATS_GENL_VERSION);
  575. return 0;
  576. }
  577. /*
  578. * late initcall ensures initialization of statistics collection
  579. * mechanisms precedes initialization of the taskstats interface
  580. */
  581. late_initcall(taskstats_init);