taskstats.c 16 KB

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