taskstats.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. static const struct nla_policy cgroupstats_cmd_get_policy[CGROUPSTATS_CMD_ATTR_MAX+1] = {
  52. [CGROUPSTATS_CMD_ATTR_FD] = { .type = NLA_U32 },
  53. };
  54. struct listener {
  55. struct list_head list;
  56. pid_t pid;
  57. char valid;
  58. };
  59. struct listener_list {
  60. struct rw_semaphore sem;
  61. struct list_head list;
  62. };
  63. static DEFINE_PER_CPU(struct listener_list, listener_array);
  64. enum actions {
  65. REGISTER,
  66. DEREGISTER,
  67. CPU_DONT_CARE
  68. };
  69. static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
  70. size_t size)
  71. {
  72. struct sk_buff *skb;
  73. void *reply;
  74. /*
  75. * If new attributes are added, please revisit this allocation
  76. */
  77. skb = genlmsg_new(size, GFP_KERNEL);
  78. if (!skb)
  79. return -ENOMEM;
  80. if (!info) {
  81. int seq = this_cpu_inc_return(taskstats_seqnum) - 1;
  82. reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
  83. } else
  84. reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
  85. if (reply == NULL) {
  86. nlmsg_free(skb);
  87. return -EINVAL;
  88. }
  89. *skbp = skb;
  90. return 0;
  91. }
  92. /*
  93. * Send taskstats data in @skb to listener with nl_pid @pid
  94. */
  95. static int send_reply(struct sk_buff *skb, struct genl_info *info)
  96. {
  97. struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
  98. void *reply = genlmsg_data(genlhdr);
  99. int rc;
  100. rc = genlmsg_end(skb, reply);
  101. if (rc < 0) {
  102. nlmsg_free(skb);
  103. return rc;
  104. }
  105. return genlmsg_reply(skb, info);
  106. }
  107. /*
  108. * Send taskstats data in @skb to listeners registered for @cpu's exit data
  109. */
  110. static void send_cpu_listeners(struct sk_buff *skb,
  111. struct listener_list *listeners)
  112. {
  113. struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
  114. struct listener *s, *tmp;
  115. struct sk_buff *skb_next, *skb_cur = skb;
  116. void *reply = genlmsg_data(genlhdr);
  117. int rc, delcount = 0;
  118. rc = genlmsg_end(skb, reply);
  119. if (rc < 0) {
  120. nlmsg_free(skb);
  121. return;
  122. }
  123. rc = 0;
  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(&init_net, 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 void fill_stats(struct user_namespace *user_ns,
  155. struct pid_namespace *pid_ns,
  156. struct task_struct *tsk, struct taskstats *stats)
  157. {
  158. memset(stats, 0, sizeof(*stats));
  159. /*
  160. * Each accounting subsystem adds calls to its functions to
  161. * fill in relevant parts of struct taskstsats as follows
  162. *
  163. * per-task-foo(stats, tsk);
  164. */
  165. delayacct_add_tsk(stats, tsk);
  166. /* fill in basic acct fields */
  167. stats->version = TASKSTATS_VERSION;
  168. stats->nvcsw = tsk->nvcsw;
  169. stats->nivcsw = tsk->nivcsw;
  170. bacct_add_tsk(user_ns, pid_ns, stats, tsk);
  171. /* fill in extended acct fields */
  172. xacct_add_tsk(stats, tsk);
  173. }
  174. static int fill_stats_for_pid(pid_t pid, struct taskstats *stats)
  175. {
  176. struct task_struct *tsk;
  177. rcu_read_lock();
  178. tsk = find_task_by_vpid(pid);
  179. if (tsk)
  180. get_task_struct(tsk);
  181. rcu_read_unlock();
  182. if (!tsk)
  183. return -ESRCH;
  184. fill_stats(current_user_ns(), task_active_pid_ns(current), tsk, stats);
  185. put_task_struct(tsk);
  186. return 0;
  187. }
  188. static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
  189. {
  190. struct task_struct *tsk, *first;
  191. unsigned long flags;
  192. int rc = -ESRCH;
  193. /*
  194. * Add additional stats from live tasks except zombie thread group
  195. * leaders who are already counted with the dead tasks
  196. */
  197. rcu_read_lock();
  198. first = find_task_by_vpid(tgid);
  199. if (!first || !lock_task_sighand(first, &flags))
  200. goto out;
  201. if (first->signal->stats)
  202. memcpy(stats, first->signal->stats, sizeof(*stats));
  203. else
  204. memset(stats, 0, sizeof(*stats));
  205. tsk = first;
  206. do {
  207. if (tsk->exit_state)
  208. continue;
  209. /*
  210. * Accounting subsystem can call its functions here to
  211. * fill in relevant parts of struct taskstsats as follows
  212. *
  213. * per-task-foo(stats, tsk);
  214. */
  215. delayacct_add_tsk(stats, tsk);
  216. stats->nvcsw += tsk->nvcsw;
  217. stats->nivcsw += tsk->nivcsw;
  218. } while_each_thread(first, tsk);
  219. unlock_task_sighand(first, &flags);
  220. rc = 0;
  221. out:
  222. rcu_read_unlock();
  223. stats->version = TASKSTATS_VERSION;
  224. /*
  225. * Accounting subsystems can also add calls here to modify
  226. * fields of taskstats.
  227. */
  228. return rc;
  229. }
  230. static void fill_tgid_exit(struct task_struct *tsk)
  231. {
  232. unsigned long flags;
  233. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  234. if (!tsk->signal->stats)
  235. goto ret;
  236. /*
  237. * Each accounting subsystem calls its functions here to
  238. * accumalate its per-task stats for tsk, into the per-tgid structure
  239. *
  240. * per-task-foo(tsk->signal->stats, tsk);
  241. */
  242. delayacct_add_tsk(tsk->signal->stats, tsk);
  243. ret:
  244. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  245. return;
  246. }
  247. static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd)
  248. {
  249. struct listener_list *listeners;
  250. struct listener *s, *tmp, *s2;
  251. unsigned int cpu;
  252. if (!cpumask_subset(mask, cpu_possible_mask))
  253. return -EINVAL;
  254. if (current_user_ns() != &init_user_ns)
  255. return -EINVAL;
  256. if (task_active_pid_ns(current) != &init_pid_ns)
  257. return -EINVAL;
  258. if (isadd == REGISTER) {
  259. for_each_cpu(cpu, mask) {
  260. s = kmalloc_node(sizeof(struct listener),
  261. GFP_KERNEL, cpu_to_node(cpu));
  262. if (!s)
  263. goto cleanup;
  264. s->pid = pid;
  265. s->valid = 1;
  266. listeners = &per_cpu(listener_array, cpu);
  267. down_write(&listeners->sem);
  268. list_for_each_entry(s2, &listeners->list, list) {
  269. if (s2->pid == pid && s2->valid)
  270. goto exists;
  271. }
  272. list_add(&s->list, &listeners->list);
  273. s = NULL;
  274. exists:
  275. up_write(&listeners->sem);
  276. kfree(s); /* nop if NULL */
  277. }
  278. return 0;
  279. }
  280. /* Deregister or cleanup */
  281. cleanup:
  282. for_each_cpu(cpu, mask) {
  283. listeners = &per_cpu(listener_array, cpu);
  284. down_write(&listeners->sem);
  285. list_for_each_entry_safe(s, tmp, &listeners->list, list) {
  286. if (s->pid == pid) {
  287. list_del(&s->list);
  288. kfree(s);
  289. break;
  290. }
  291. }
  292. up_write(&listeners->sem);
  293. }
  294. return 0;
  295. }
  296. static int parse(struct nlattr *na, struct cpumask *mask)
  297. {
  298. char *data;
  299. int len;
  300. int ret;
  301. if (na == NULL)
  302. return 1;
  303. len = nla_len(na);
  304. if (len > TASKSTATS_CPUMASK_MAXLEN)
  305. return -E2BIG;
  306. if (len < 1)
  307. return -EINVAL;
  308. data = kmalloc(len, GFP_KERNEL);
  309. if (!data)
  310. return -ENOMEM;
  311. nla_strlcpy(data, na, len);
  312. ret = cpulist_parse(data, mask);
  313. kfree(data);
  314. return ret;
  315. }
  316. #if defined(CONFIG_64BIT) && !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  317. #define TASKSTATS_NEEDS_PADDING 1
  318. #endif
  319. static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
  320. {
  321. struct nlattr *na, *ret;
  322. int aggr;
  323. aggr = (type == TASKSTATS_TYPE_PID)
  324. ? TASKSTATS_TYPE_AGGR_PID
  325. : TASKSTATS_TYPE_AGGR_TGID;
  326. /*
  327. * The taskstats structure is internally aligned on 8 byte
  328. * boundaries but the layout of the aggregrate reply, with
  329. * two NLA headers and the pid (each 4 bytes), actually
  330. * force the entire structure to be unaligned. This causes
  331. * the kernel to issue unaligned access warnings on some
  332. * architectures like ia64. Unfortunately, some software out there
  333. * doesn't properly unroll the NLA packet and assumes that the start
  334. * of the taskstats structure will always be 20 bytes from the start
  335. * of the netlink payload. Aligning the start of the taskstats
  336. * structure breaks this software, which we don't want. So, for now
  337. * the alignment only happens on architectures that require it
  338. * and those users will have to update to fixed versions of those
  339. * packages. Space is reserved in the packet only when needed.
  340. * This ifdef should be removed in several years e.g. 2012 once
  341. * we can be confident that fixed versions are installed on most
  342. * systems. We add the padding before the aggregate since the
  343. * aggregate is already a defined type.
  344. */
  345. #ifdef TASKSTATS_NEEDS_PADDING
  346. if (nla_put(skb, TASKSTATS_TYPE_NULL, 0, NULL) < 0)
  347. goto err;
  348. #endif
  349. na = nla_nest_start(skb, aggr);
  350. if (!na)
  351. goto err;
  352. if (nla_put(skb, type, sizeof(pid), &pid) < 0) {
  353. nla_nest_cancel(skb, na);
  354. goto err;
  355. }
  356. ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
  357. if (!ret) {
  358. nla_nest_cancel(skb, na);
  359. goto err;
  360. }
  361. nla_nest_end(skb, na);
  362. return nla_data(ret);
  363. err:
  364. return NULL;
  365. }
  366. static int cgroupstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
  367. {
  368. int rc = 0;
  369. struct sk_buff *rep_skb;
  370. struct cgroupstats *stats;
  371. struct nlattr *na;
  372. size_t size;
  373. u32 fd;
  374. struct fd f;
  375. na = info->attrs[CGROUPSTATS_CMD_ATTR_FD];
  376. if (!na)
  377. return -EINVAL;
  378. fd = nla_get_u32(info->attrs[CGROUPSTATS_CMD_ATTR_FD]);
  379. f = fdget(fd);
  380. if (!f.file)
  381. return 0;
  382. size = nla_total_size(sizeof(struct cgroupstats));
  383. rc = prepare_reply(info, CGROUPSTATS_CMD_NEW, &rep_skb,
  384. size);
  385. if (rc < 0)
  386. goto err;
  387. na = nla_reserve(rep_skb, CGROUPSTATS_TYPE_CGROUP_STATS,
  388. sizeof(struct cgroupstats));
  389. if (na == NULL) {
  390. nlmsg_free(rep_skb);
  391. rc = -EMSGSIZE;
  392. goto err;
  393. }
  394. stats = nla_data(na);
  395. memset(stats, 0, sizeof(*stats));
  396. rc = cgroupstats_build(stats, f.file->f_dentry);
  397. if (rc < 0) {
  398. nlmsg_free(rep_skb);
  399. goto err;
  400. }
  401. rc = send_reply(rep_skb, info);
  402. err:
  403. fdput(f);
  404. return rc;
  405. }
  406. static int cmd_attr_register_cpumask(struct genl_info *info)
  407. {
  408. cpumask_var_t mask;
  409. int rc;
  410. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  411. return -ENOMEM;
  412. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], mask);
  413. if (rc < 0)
  414. goto out;
  415. rc = add_del_listener(info->snd_portid, mask, REGISTER);
  416. out:
  417. free_cpumask_var(mask);
  418. return rc;
  419. }
  420. static int cmd_attr_deregister_cpumask(struct genl_info *info)
  421. {
  422. cpumask_var_t mask;
  423. int rc;
  424. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  425. return -ENOMEM;
  426. rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], mask);
  427. if (rc < 0)
  428. goto out;
  429. rc = add_del_listener(info->snd_portid, mask, DEREGISTER);
  430. out:
  431. free_cpumask_var(mask);
  432. return rc;
  433. }
  434. static size_t taskstats_packet_size(void)
  435. {
  436. size_t size;
  437. size = nla_total_size(sizeof(u32)) +
  438. nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
  439. #ifdef TASKSTATS_NEEDS_PADDING
  440. size += nla_total_size(0); /* Padding for alignment */
  441. #endif
  442. return size;
  443. }
  444. static int cmd_attr_pid(struct genl_info *info)
  445. {
  446. struct taskstats *stats;
  447. struct sk_buff *rep_skb;
  448. size_t size;
  449. u32 pid;
  450. int rc;
  451. size = taskstats_packet_size();
  452. rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
  453. if (rc < 0)
  454. return rc;
  455. rc = -EINVAL;
  456. pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
  457. stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
  458. if (!stats)
  459. goto err;
  460. rc = fill_stats_for_pid(pid, stats);
  461. if (rc < 0)
  462. goto err;
  463. return send_reply(rep_skb, info);
  464. err:
  465. nlmsg_free(rep_skb);
  466. return rc;
  467. }
  468. static int cmd_attr_tgid(struct genl_info *info)
  469. {
  470. struct taskstats *stats;
  471. struct sk_buff *rep_skb;
  472. size_t size;
  473. u32 tgid;
  474. int rc;
  475. size = taskstats_packet_size();
  476. rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
  477. if (rc < 0)
  478. return rc;
  479. rc = -EINVAL;
  480. tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
  481. stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
  482. if (!stats)
  483. goto err;
  484. rc = fill_stats_for_tgid(tgid, stats);
  485. if (rc < 0)
  486. goto err;
  487. return send_reply(rep_skb, info);
  488. err:
  489. nlmsg_free(rep_skb);
  490. return rc;
  491. }
  492. static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
  493. {
  494. if (info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK])
  495. return cmd_attr_register_cpumask(info);
  496. else if (info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK])
  497. return cmd_attr_deregister_cpumask(info);
  498. else if (info->attrs[TASKSTATS_CMD_ATTR_PID])
  499. return cmd_attr_pid(info);
  500. else if (info->attrs[TASKSTATS_CMD_ATTR_TGID])
  501. return cmd_attr_tgid(info);
  502. else
  503. return -EINVAL;
  504. }
  505. static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
  506. {
  507. struct signal_struct *sig = tsk->signal;
  508. struct taskstats *stats;
  509. if (sig->stats || thread_group_empty(tsk))
  510. goto ret;
  511. /* No problem if kmem_cache_zalloc() fails */
  512. stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
  513. spin_lock_irq(&tsk->sighand->siglock);
  514. if (!sig->stats) {
  515. sig->stats = stats;
  516. stats = NULL;
  517. }
  518. spin_unlock_irq(&tsk->sighand->siglock);
  519. if (stats)
  520. kmem_cache_free(taskstats_cache, stats);
  521. ret:
  522. return sig->stats;
  523. }
  524. /* Send pid data out on exit */
  525. void taskstats_exit(struct task_struct *tsk, int group_dead)
  526. {
  527. int rc;
  528. struct listener_list *listeners;
  529. struct taskstats *stats;
  530. struct sk_buff *rep_skb;
  531. size_t size;
  532. int is_thread_group;
  533. if (!family_registered)
  534. return;
  535. /*
  536. * Size includes space for nested attributes
  537. */
  538. size = taskstats_packet_size();
  539. is_thread_group = !!taskstats_tgid_alloc(tsk);
  540. if (is_thread_group) {
  541. /* PID + STATS + TGID + STATS */
  542. size = 2 * size;
  543. /* fill the tsk->signal->stats structure */
  544. fill_tgid_exit(tsk);
  545. }
  546. listeners = __this_cpu_ptr(&listener_array);
  547. if (list_empty(&listeners->list))
  548. return;
  549. rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
  550. if (rc < 0)
  551. return;
  552. stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID,
  553. task_pid_nr_ns(tsk, &init_pid_ns));
  554. if (!stats)
  555. goto err;
  556. fill_stats(&init_user_ns, &init_pid_ns, tsk, stats);
  557. /*
  558. * Doesn't matter if tsk is the leader or the last group member leaving
  559. */
  560. if (!is_thread_group || !group_dead)
  561. goto send;
  562. stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID,
  563. task_tgid_nr_ns(tsk, &init_pid_ns));
  564. if (!stats)
  565. goto err;
  566. memcpy(stats, tsk->signal->stats, sizeof(*stats));
  567. send:
  568. send_cpu_listeners(rep_skb, listeners);
  569. return;
  570. err:
  571. nlmsg_free(rep_skb);
  572. }
  573. static struct genl_ops taskstats_ops = {
  574. .cmd = TASKSTATS_CMD_GET,
  575. .doit = taskstats_user_cmd,
  576. .policy = taskstats_cmd_get_policy,
  577. .flags = GENL_ADMIN_PERM,
  578. };
  579. static struct genl_ops cgroupstats_ops = {
  580. .cmd = CGROUPSTATS_CMD_GET,
  581. .doit = cgroupstats_user_cmd,
  582. .policy = cgroupstats_cmd_get_policy,
  583. };
  584. /* Needed early in initialization */
  585. void __init taskstats_init_early(void)
  586. {
  587. unsigned int i;
  588. taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
  589. for_each_possible_cpu(i) {
  590. INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
  591. init_rwsem(&(per_cpu(listener_array, i).sem));
  592. }
  593. }
  594. static int __init taskstats_init(void)
  595. {
  596. int rc;
  597. rc = genl_register_family(&family);
  598. if (rc)
  599. return rc;
  600. rc = genl_register_ops(&family, &taskstats_ops);
  601. if (rc < 0)
  602. goto err;
  603. rc = genl_register_ops(&family, &cgroupstats_ops);
  604. if (rc < 0)
  605. goto err_cgroup_ops;
  606. family_registered = 1;
  607. pr_info("registered taskstats version %d\n", TASKSTATS_GENL_VERSION);
  608. return 0;
  609. err_cgroup_ops:
  610. genl_unregister_ops(&family, &taskstats_ops);
  611. err:
  612. genl_unregister_family(&family);
  613. return rc;
  614. }
  615. /*
  616. * late initcall ensures initialization of statistics collection
  617. * mechanisms precedes initialization of the taskstats interface
  618. */
  619. late_initcall(taskstats_init);