taskstats.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 <net/genetlink.h>
  25. #include <asm/atomic.h>
  26. /*
  27. * Maximum length of a cpumask that can be specified in
  28. * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
  29. */
  30. #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS)
  31. static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
  32. static int family_registered;
  33. struct kmem_cache *taskstats_cache;
  34. static struct genl_family family = {
  35. .id = GENL_ID_GENERATE,
  36. .name = TASKSTATS_GENL_NAME,
  37. .version = TASKSTATS_GENL_VERSION,
  38. .maxattr = TASKSTATS_CMD_ATTR_MAX,
  39. };
  40. static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
  41. __read_mostly = {
  42. [TASKSTATS_CMD_ATTR_PID] = { .type = NLA_U32 },
  43. [TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
  44. [TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
  45. [TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};
  46. struct listener {
  47. struct list_head list;
  48. pid_t pid;
  49. char valid;
  50. };
  51. struct listener_list {
  52. struct rw_semaphore sem;
  53. struct list_head list;
  54. };
  55. static DEFINE_PER_CPU(struct listener_list, listener_array);
  56. enum actions {
  57. REGISTER,
  58. DEREGISTER,
  59. CPU_DONT_CARE
  60. };
  61. static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
  62. size_t size)
  63. {
  64. struct sk_buff *skb;
  65. void *reply;
  66. /*
  67. * If new attributes are added, please revisit this allocation
  68. */
  69. skb = genlmsg_new(size, GFP_KERNEL);
  70. if (!skb)
  71. return -ENOMEM;
  72. if (!info) {
  73. int seq = get_cpu_var(taskstats_seqnum)++;
  74. put_cpu_var(taskstats_seqnum);
  75. reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
  76. } else
  77. reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
  78. if (reply == NULL) {
  79. nlmsg_free(skb);
  80. return -EINVAL;
  81. }
  82. *skbp = skb;
  83. return 0;
  84. }
  85. /*
  86. * Send taskstats data in @skb to listener with nl_pid @pid
  87. */
  88. static int send_reply(struct sk_buff *skb, pid_t pid)
  89. {
  90. struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
  91. void *reply = genlmsg_data(genlhdr);
  92. int rc;
  93. rc = genlmsg_end(skb, reply);
  94. if (rc < 0) {
  95. nlmsg_free(skb);
  96. return rc;
  97. }
  98. return genlmsg_unicast(skb, pid);
  99. }
  100. /*
  101. * Send taskstats data in @skb to listeners registered for @cpu's exit data
  102. */
  103. static void send_cpu_listeners(struct sk_buff *skb,
  104. struct listener_list *listeners)
  105. {
  106. struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
  107. struct listener *s, *tmp;
  108. struct sk_buff *skb_next, *skb_cur = skb;
  109. void *reply = genlmsg_data(genlhdr);
  110. int rc, delcount = 0;
  111. rc = genlmsg_end(skb, reply);
  112. if (rc < 0) {
  113. nlmsg_free(skb);
  114. return;
  115. }
  116. rc = 0;
  117. down_read(&listeners->sem);
  118. list_for_each_entry(s, &listeners->list, list) {
  119. skb_next = NULL;
  120. if (!list_is_last(&s->list, &listeners->list)) {
  121. skb_next = skb_clone(skb_cur, GFP_KERNEL);
  122. if (!skb_next)
  123. break;
  124. }
  125. rc = genlmsg_unicast(skb_cur, s->pid);
  126. if (rc == -ECONNREFUSED) {
  127. s->valid = 0;
  128. delcount++;
  129. }
  130. skb_cur = skb_next;
  131. }
  132. up_read(&listeners->sem);
  133. if (skb_cur)
  134. nlmsg_free(skb_cur);
  135. if (!delcount)
  136. return;
  137. /* Delete invalidated entries */
  138. down_write(&listeners->sem);
  139. list_for_each_entry_safe(s, tmp, &listeners->list, list) {
  140. if (!s->valid) {
  141. list_del(&s->list);
  142. kfree(s);
  143. }
  144. }
  145. up_write(&listeners->sem);
  146. }
  147. static int fill_pid(pid_t pid, struct task_struct *tsk,
  148. struct taskstats *stats)
  149. {
  150. int rc = 0;
  151. if (!tsk) {
  152. rcu_read_lock();
  153. tsk = find_task_by_pid(pid);
  154. if (tsk)
  155. get_task_struct(tsk);
  156. rcu_read_unlock();
  157. if (!tsk)
  158. return -ESRCH;
  159. } else
  160. get_task_struct(tsk);
  161. memset(stats, 0, sizeof(*stats));
  162. /*
  163. * Each accounting subsystem adds calls to its functions to
  164. * fill in relevant parts of struct taskstsats as follows
  165. *
  166. * per-task-foo(stats, tsk);
  167. */
  168. delayacct_add_tsk(stats, tsk);
  169. /* fill in basic acct fields */
  170. stats->version = TASKSTATS_VERSION;
  171. stats->nvcsw = tsk->nvcsw;
  172. stats->nivcsw = tsk->nivcsw;
  173. bacct_add_tsk(stats, tsk);
  174. /* fill in extended acct fields */
  175. xacct_add_tsk(stats, tsk);
  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 *first,
  181. struct taskstats *stats)
  182. {
  183. struct task_struct *tsk;
  184. unsigned long flags;
  185. int rc = -ESRCH;
  186. /*
  187. * Add additional stats from live tasks except zombie thread group
  188. * leaders who are already counted with the dead tasks
  189. */
  190. rcu_read_lock();
  191. if (!first)
  192. first = find_task_by_pid(tgid);
  193. if (!first || !lock_task_sighand(first, &flags))
  194. goto out;
  195. if (first->signal->stats)
  196. memcpy(stats, first->signal->stats, sizeof(*stats));
  197. else
  198. memset(stats, 0, sizeof(*stats));
  199. tsk = first;
  200. do {
  201. if (tsk->exit_state)
  202. continue;
  203. /*
  204. * Accounting subsystem can call its functions here to
  205. * fill in relevant parts of struct taskstsats as follows
  206. *
  207. * per-task-foo(stats, tsk);
  208. */
  209. delayacct_add_tsk(stats, tsk);
  210. stats->nvcsw += tsk->nvcsw;
  211. stats->nivcsw += tsk->nivcsw;
  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 struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
  302. {
  303. struct nlattr *na, *ret;
  304. int aggr;
  305. aggr = (type == TASKSTATS_TYPE_PID)
  306. ? TASKSTATS_TYPE_AGGR_PID
  307. : TASKSTATS_TYPE_AGGR_TGID;
  308. na = nla_nest_start(skb, aggr);
  309. if (!na)
  310. goto err;
  311. if (nla_put(skb, type, sizeof(pid), &pid) < 0)
  312. goto err;
  313. ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
  314. if (!ret)
  315. goto err;
  316. nla_nest_end(skb, na);
  317. return nla_data(ret);
  318. err:
  319. return NULL;
  320. }
  321. static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
  322. {
  323. int rc = 0;
  324. struct sk_buff *rep_skb;
  325. struct taskstats *stats;
  326. size_t size;
  327. cpumask_t mask;
  328. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
  329. if (rc < 0)
  330. return rc;
  331. if (rc == 0)
  332. return add_del_listener(info->snd_pid, &mask, REGISTER);
  333. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
  334. if (rc < 0)
  335. return rc;
  336. if (rc == 0)
  337. return add_del_listener(info->snd_pid, &mask, DEREGISTER);
  338. /*
  339. * Size includes space for nested attributes
  340. */
  341. size = nla_total_size(sizeof(u32)) +
  342. nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
  343. rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
  344. if (rc < 0)
  345. return rc;
  346. rc = -EINVAL;
  347. if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
  348. u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
  349. stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
  350. if (!stats)
  351. goto err;
  352. rc = fill_pid(pid, NULL, stats);
  353. if (rc < 0)
  354. goto err;
  355. } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
  356. u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
  357. stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
  358. if (!stats)
  359. goto err;
  360. rc = fill_tgid(tgid, NULL, stats);
  361. if (rc < 0)
  362. goto err;
  363. } else
  364. goto err;
  365. return send_reply(rep_skb, info->snd_pid);
  366. err:
  367. nlmsg_free(rep_skb);
  368. return rc;
  369. }
  370. static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
  371. {
  372. struct signal_struct *sig = tsk->signal;
  373. struct taskstats *stats;
  374. if (sig->stats || thread_group_empty(tsk))
  375. goto ret;
  376. /* No problem if kmem_cache_zalloc() fails */
  377. stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
  378. spin_lock_irq(&tsk->sighand->siglock);
  379. if (!sig->stats) {
  380. sig->stats = stats;
  381. stats = NULL;
  382. }
  383. spin_unlock_irq(&tsk->sighand->siglock);
  384. if (stats)
  385. kmem_cache_free(taskstats_cache, stats);
  386. ret:
  387. return sig->stats;
  388. }
  389. /* Send pid data out on exit */
  390. void taskstats_exit(struct task_struct *tsk, int group_dead)
  391. {
  392. int rc;
  393. struct listener_list *listeners;
  394. struct taskstats *stats;
  395. struct sk_buff *rep_skb;
  396. size_t size;
  397. int is_thread_group;
  398. if (!family_registered)
  399. return;
  400. /*
  401. * Size includes space for nested attributes
  402. */
  403. size = nla_total_size(sizeof(u32)) +
  404. nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
  405. is_thread_group = !!taskstats_tgid_alloc(tsk);
  406. if (is_thread_group) {
  407. /* PID + STATS + TGID + STATS */
  408. size = 2 * size;
  409. /* fill the tsk->signal->stats structure */
  410. fill_tgid_exit(tsk);
  411. }
  412. listeners = &__raw_get_cpu_var(listener_array);
  413. if (list_empty(&listeners->list))
  414. return;
  415. rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
  416. if (rc < 0)
  417. return;
  418. stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid);
  419. if (!stats)
  420. goto err;
  421. rc = fill_pid(tsk->pid, tsk, stats);
  422. if (rc < 0)
  423. goto err;
  424. /*
  425. * Doesn't matter if tsk is the leader or the last group member leaving
  426. */
  427. if (!is_thread_group || !group_dead)
  428. goto send;
  429. stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid);
  430. if (!stats)
  431. goto err;
  432. memcpy(stats, tsk->signal->stats, sizeof(*stats));
  433. send:
  434. send_cpu_listeners(rep_skb, listeners);
  435. return;
  436. err:
  437. nlmsg_free(rep_skb);
  438. }
  439. static struct genl_ops taskstats_ops = {
  440. .cmd = TASKSTATS_CMD_GET,
  441. .doit = taskstats_user_cmd,
  442. .policy = taskstats_cmd_get_policy,
  443. };
  444. /* Needed early in initialization */
  445. void __init taskstats_init_early(void)
  446. {
  447. unsigned int i;
  448. taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
  449. for_each_possible_cpu(i) {
  450. INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
  451. init_rwsem(&(per_cpu(listener_array, i).sem));
  452. }
  453. }
  454. static int __init taskstats_init(void)
  455. {
  456. int rc;
  457. rc = genl_register_family(&family);
  458. if (rc)
  459. return rc;
  460. rc = genl_register_ops(&family, &taskstats_ops);
  461. if (rc < 0)
  462. goto err;
  463. family_registered = 1;
  464. return 0;
  465. err:
  466. genl_unregister_family(&family);
  467. return rc;
  468. }
  469. /*
  470. * late initcall ensures initialization of statistics collection
  471. * mechanisms precedes initialization of the taskstats interface
  472. */
  473. late_initcall(taskstats_init);