diff options
Diffstat (limited to 'kernel/taskstats.c')
-rw-r--r-- | kernel/taskstats.c | 87 |
1 files changed, 39 insertions, 48 deletions
diff --git a/kernel/taskstats.c b/kernel/taskstats.c index 5d6a8c54ee85..f45c5e70773c 100644 --- a/kernel/taskstats.c +++ b/kernel/taskstats.c | |||
@@ -77,7 +77,8 @@ static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp, | |||
77 | /* | 77 | /* |
78 | * If new attributes are added, please revisit this allocation | 78 | * If new attributes are added, please revisit this allocation |
79 | */ | 79 | */ |
80 | skb = nlmsg_new(genlmsg_total_size(size), GFP_KERNEL); | 80 | size = nlmsg_total_size(genlmsg_total_size(size)); |
81 | skb = nlmsg_new(size, GFP_KERNEL); | ||
81 | if (!skb) | 82 | if (!skb) |
82 | return -ENOMEM; | 83 | return -ENOMEM; |
83 | 84 | ||
@@ -174,21 +175,19 @@ static void send_cpu_listeners(struct sk_buff *skb, unsigned int cpu) | |||
174 | up_write(&listeners->sem); | 175 | up_write(&listeners->sem); |
175 | } | 176 | } |
176 | 177 | ||
177 | static int fill_pid(pid_t pid, struct task_struct *pidtsk, | 178 | static int fill_pid(pid_t pid, struct task_struct *tsk, |
178 | struct taskstats *stats) | 179 | struct taskstats *stats) |
179 | { | 180 | { |
180 | int rc = 0; | 181 | int rc = 0; |
181 | struct task_struct *tsk = pidtsk; | ||
182 | 182 | ||
183 | if (!pidtsk) { | 183 | if (!tsk) { |
184 | read_lock(&tasklist_lock); | 184 | rcu_read_lock(); |
185 | tsk = find_task_by_pid(pid); | 185 | tsk = find_task_by_pid(pid); |
186 | if (!tsk) { | 186 | if (tsk) |
187 | read_unlock(&tasklist_lock); | 187 | get_task_struct(tsk); |
188 | rcu_read_unlock(); | ||
189 | if (!tsk) | ||
188 | return -ESRCH; | 190 | return -ESRCH; |
189 | } | ||
190 | get_task_struct(tsk); | ||
191 | read_unlock(&tasklist_lock); | ||
192 | } else | 191 | } else |
193 | get_task_struct(tsk); | 192 | get_task_struct(tsk); |
194 | 193 | ||
@@ -214,39 +213,30 @@ static int fill_pid(pid_t pid, struct task_struct *pidtsk, | |||
214 | 213 | ||
215 | } | 214 | } |
216 | 215 | ||
217 | static int fill_tgid(pid_t tgid, struct task_struct *tgidtsk, | 216 | static int fill_tgid(pid_t tgid, struct task_struct *first, |
218 | struct taskstats *stats) | 217 | struct taskstats *stats) |
219 | { | 218 | { |
220 | struct task_struct *tsk, *first; | 219 | struct task_struct *tsk; |
221 | unsigned long flags; | 220 | unsigned long flags; |
221 | int rc = -ESRCH; | ||
222 | 222 | ||
223 | /* | 223 | /* |
224 | * Add additional stats from live tasks except zombie thread group | 224 | * Add additional stats from live tasks except zombie thread group |
225 | * leaders who are already counted with the dead tasks | 225 | * leaders who are already counted with the dead tasks |
226 | */ | 226 | */ |
227 | first = tgidtsk; | 227 | rcu_read_lock(); |
228 | if (!first) { | 228 | if (!first) |
229 | read_lock(&tasklist_lock); | ||
230 | first = find_task_by_pid(tgid); | 229 | first = find_task_by_pid(tgid); |
231 | if (!first) { | ||
232 | read_unlock(&tasklist_lock); | ||
233 | return -ESRCH; | ||
234 | } | ||
235 | get_task_struct(first); | ||
236 | read_unlock(&tasklist_lock); | ||
237 | } else | ||
238 | get_task_struct(first); | ||
239 | 230 | ||
240 | /* Start with stats from dead tasks */ | 231 | if (!first || !lock_task_sighand(first, &flags)) |
241 | spin_lock_irqsave(&first->signal->stats_lock, flags); | 232 | goto out; |
233 | |||
242 | if (first->signal->stats) | 234 | if (first->signal->stats) |
243 | memcpy(stats, first->signal->stats, sizeof(*stats)); | 235 | memcpy(stats, first->signal->stats, sizeof(*stats)); |
244 | spin_unlock_irqrestore(&first->signal->stats_lock, flags); | ||
245 | 236 | ||
246 | tsk = first; | 237 | tsk = first; |
247 | read_lock(&tasklist_lock); | ||
248 | do { | 238 | do { |
249 | if (tsk->exit_state == EXIT_ZOMBIE && thread_group_leader(tsk)) | 239 | if (tsk->exit_state) |
250 | continue; | 240 | continue; |
251 | /* | 241 | /* |
252 | * Accounting subsystem can call its functions here to | 242 | * Accounting subsystem can call its functions here to |
@@ -257,15 +247,18 @@ static int fill_tgid(pid_t tgid, struct task_struct *tgidtsk, | |||
257 | delayacct_add_tsk(stats, tsk); | 247 | delayacct_add_tsk(stats, tsk); |
258 | 248 | ||
259 | } while_each_thread(first, tsk); | 249 | } while_each_thread(first, tsk); |
260 | read_unlock(&tasklist_lock); | ||
261 | stats->version = TASKSTATS_VERSION; | ||
262 | 250 | ||
251 | unlock_task_sighand(first, &flags); | ||
252 | rc = 0; | ||
253 | out: | ||
254 | rcu_read_unlock(); | ||
255 | |||
256 | stats->version = TASKSTATS_VERSION; | ||
263 | /* | 257 | /* |
264 | * Accounting subsytems can also add calls here to modify | 258 | * Accounting subsytems can also add calls here to modify |
265 | * fields of taskstats. | 259 | * fields of taskstats. |
266 | */ | 260 | */ |
267 | 261 | return rc; | |
268 | return 0; | ||
269 | } | 262 | } |
270 | 263 | ||
271 | 264 | ||
@@ -273,7 +266,7 @@ static void fill_tgid_exit(struct task_struct *tsk) | |||
273 | { | 266 | { |
274 | unsigned long flags; | 267 | unsigned long flags; |
275 | 268 | ||
276 | spin_lock_irqsave(&tsk->signal->stats_lock, flags); | 269 | spin_lock_irqsave(&tsk->sighand->siglock, flags); |
277 | if (!tsk->signal->stats) | 270 | if (!tsk->signal->stats) |
278 | goto ret; | 271 | goto ret; |
279 | 272 | ||
@@ -285,7 +278,7 @@ static void fill_tgid_exit(struct task_struct *tsk) | |||
285 | */ | 278 | */ |
286 | delayacct_add_tsk(tsk->signal->stats, tsk); | 279 | delayacct_add_tsk(tsk->signal->stats, tsk); |
287 | ret: | 280 | ret: |
288 | spin_unlock_irqrestore(&tsk->signal->stats_lock, flags); | 281 | spin_unlock_irqrestore(&tsk->sighand->siglock, flags); |
289 | return; | 282 | return; |
290 | } | 283 | } |
291 | 284 | ||
@@ -419,7 +412,7 @@ static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info) | |||
419 | return send_reply(rep_skb, info->snd_pid); | 412 | return send_reply(rep_skb, info->snd_pid); |
420 | 413 | ||
421 | nla_put_failure: | 414 | nla_put_failure: |
422 | return genlmsg_cancel(rep_skb, reply); | 415 | rc = genlmsg_cancel(rep_skb, reply); |
423 | err: | 416 | err: |
424 | nlmsg_free(rep_skb); | 417 | nlmsg_free(rep_skb); |
425 | return rc; | 418 | return rc; |
@@ -461,24 +454,26 @@ void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats, | |||
461 | size_t size; | 454 | size_t size; |
462 | int is_thread_group; | 455 | int is_thread_group; |
463 | struct nlattr *na; | 456 | struct nlattr *na; |
464 | unsigned long flags; | ||
465 | 457 | ||
466 | if (!family_registered || !tidstats) | 458 | if (!family_registered) |
467 | return; | 459 | return; |
468 | 460 | ||
469 | spin_lock_irqsave(&tsk->signal->stats_lock, flags); | ||
470 | is_thread_group = tsk->signal->stats ? 1 : 0; | ||
471 | spin_unlock_irqrestore(&tsk->signal->stats_lock, flags); | ||
472 | |||
473 | rc = 0; | ||
474 | /* | 461 | /* |
475 | * Size includes space for nested attributes | 462 | * Size includes space for nested attributes |
476 | */ | 463 | */ |
477 | size = nla_total_size(sizeof(u32)) + | 464 | size = nla_total_size(sizeof(u32)) + |
478 | nla_total_size(sizeof(struct taskstats)) + nla_total_size(0); | 465 | nla_total_size(sizeof(struct taskstats)) + nla_total_size(0); |
479 | 466 | ||
480 | if (is_thread_group) | 467 | is_thread_group = (tsk->signal->stats != NULL); |
481 | size = 2 * size; /* PID + STATS + TGID + STATS */ | 468 | if (is_thread_group) { |
469 | /* PID + STATS + TGID + STATS */ | ||
470 | size = 2 * size; | ||
471 | /* fill the tsk->signal->stats structure */ | ||
472 | fill_tgid_exit(tsk); | ||
473 | } | ||
474 | |||
475 | if (!tidstats) | ||
476 | return; | ||
482 | 477 | ||
483 | rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size); | 478 | rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size); |
484 | if (rc < 0) | 479 | if (rc < 0) |
@@ -498,11 +493,8 @@ void taskstats_exit_send(struct task_struct *tsk, struct taskstats *tidstats, | |||
498 | goto send; | 493 | goto send; |
499 | 494 | ||
500 | /* | 495 | /* |
501 | * tsk has/had a thread group so fill the tsk->signal->stats structure | ||
502 | * Doesn't matter if tsk is the leader or the last group member leaving | 496 | * Doesn't matter if tsk is the leader or the last group member leaving |
503 | */ | 497 | */ |
504 | |||
505 | fill_tgid_exit(tsk); | ||
506 | if (!group_dead) | 498 | if (!group_dead) |
507 | goto send; | 499 | goto send; |
508 | 500 | ||
@@ -519,7 +511,6 @@ send: | |||
519 | 511 | ||
520 | nla_put_failure: | 512 | nla_put_failure: |
521 | genlmsg_cancel(rep_skb, reply); | 513 | genlmsg_cancel(rep_skb, reply); |
522 | goto ret; | ||
523 | err_skb: | 514 | err_skb: |
524 | nlmsg_free(rep_skb); | 515 | nlmsg_free(rep_skb); |
525 | ret: | 516 | ret: |