taskstats.c 12.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * taskstats.c - Export per-task statistics to userland
 *
 * Copyright (C) Shailabh Nagar, IBM Corp. 2006
 *           (C) Balbir Singh,   IBM Corp. 2006
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/kernel.h>
#include <linux/taskstats_kern.h>
21
#include <linux/tsacct_kern.h>
22
#include <linux/delayacct.h>
23
#include <linux/tsacct_kern.h>
24 25
#include <linux/cpumask.h>
#include <linux/percpu.h>
26 27 28
#include <net/genetlink.h>
#include <asm/atomic.h>

29 30 31 32 33 34
/*
 * Maximum length of a cpumask that can be specified in
 * the TASKSTATS_CMD_ATTR_REGISTER/DEREGISTER_CPUMASK attribute
 */
#define TASKSTATS_CPUMASK_MAXLEN	(100+6*NR_CPUS)

35 36
static DEFINE_PER_CPU(__u32, taskstats_seqnum) = { 0 };
static int family_registered;
37
struct kmem_cache *taskstats_cache;
38 39 40 41 42 43 44 45 46 47 48 49

static struct genl_family family = {
	.id		= GENL_ID_GENERATE,
	.name		= TASKSTATS_GENL_NAME,
	.version	= TASKSTATS_GENL_VERSION,
	.maxattr	= TASKSTATS_CMD_ATTR_MAX,
};

static struct nla_policy taskstats_cmd_get_policy[TASKSTATS_CMD_ATTR_MAX+1]
__read_mostly = {
	[TASKSTATS_CMD_ATTR_PID]  = { .type = NLA_U32 },
	[TASKSTATS_CMD_ATTR_TGID] = { .type = NLA_U32 },
50 51 52 53 54 55
	[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK] = { .type = NLA_STRING },
	[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK] = { .type = NLA_STRING },};

struct listener {
	struct list_head list;
	pid_t pid;
56
	char valid;
57 58
};

59 60 61 62 63 64 65 66 67 68 69
struct listener_list {
	struct rw_semaphore sem;
	struct list_head list;
};
static DEFINE_PER_CPU(struct listener_list, listener_array);

enum actions {
	REGISTER,
	DEREGISTER,
	CPU_DONT_CARE
};
70 71

static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
72
				size_t size)
73 74 75 76 77 78 79
{
	struct sk_buff *skb;
	void *reply;

	/*
	 * If new attributes are added, please revisit this allocation
	 */
80
	skb = genlmsg_new(size, GFP_KERNEL);
81 82 83 84 85 86 87
	if (!skb)
		return -ENOMEM;

	if (!info) {
		int seq = get_cpu_var(taskstats_seqnum)++;
		put_cpu_var(taskstats_seqnum);

88
		reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
89
	} else
90
		reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
91 92 93 94 95 96 97 98 99
	if (reply == NULL) {
		nlmsg_free(skb);
		return -EINVAL;
	}

	*skbp = skb;
	return 0;
}

100 101 102 103
/*
 * Send taskstats data in @skb to listener with nl_pid @pid
 */
static int send_reply(struct sk_buff *skb, pid_t pid)
104
{
105
	struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
106
	void *reply = genlmsg_data(genlhdr);
107 108 109 110 111 112 113 114 115 116 117
	int rc;

	rc = genlmsg_end(skb, reply);
	if (rc < 0) {
		nlmsg_free(skb);
		return rc;
	}

	return genlmsg_unicast(skb, pid);
}

118 119 120
/*
 * Send taskstats data in @skb to listeners registered for @cpu's exit data
 */
121 122
static void send_cpu_listeners(struct sk_buff *skb,
					struct listener_list *listeners)
123
{
124
	struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
125 126 127
	struct listener *s, *tmp;
	struct sk_buff *skb_next, *skb_cur = skb;
	void *reply = genlmsg_data(genlhdr);
128
	int rc, delcount = 0;
129 130 131 132

	rc = genlmsg_end(skb, reply);
	if (rc < 0) {
		nlmsg_free(skb);
133
		return;
134 135 136
	}

	rc = 0;
137
	down_read(&listeners->sem);
138
	list_for_each_entry(s, &listeners->list, list) {
139 140 141
		skb_next = NULL;
		if (!list_is_last(&s->list, &listeners->list)) {
			skb_next = skb_clone(skb_cur, GFP_KERNEL);
142
			if (!skb_next)
143 144
				break;
		}
145 146
		rc = genlmsg_unicast(skb_cur, s->pid);
		if (rc == -ECONNREFUSED) {
147 148
			s->valid = 0;
			delcount++;
149 150 151
		}
		skb_cur = skb_next;
	}
152
	up_read(&listeners->sem);
153

154 155 156
	if (skb_cur)
		nlmsg_free(skb_cur);

157
	if (!delcount)
158
		return;
159 160 161 162 163 164 165 166 167 168

	/* Delete invalidated entries */
	down_write(&listeners->sem);
	list_for_each_entry_safe(s, tmp, &listeners->list, list) {
		if (!s->valid) {
			list_del(&s->list);
			kfree(s);
		}
	}
	up_write(&listeners->sem);
169 170
}

171
static int fill_pid(pid_t pid, struct task_struct *tsk,
172 173
		struct taskstats *stats)
{
174
	int rc = 0;
175

176 177
	if (!tsk) {
		rcu_read_lock();
178
		tsk = find_task_by_pid(pid);
179 180 181 182
		if (tsk)
			get_task_struct(tsk);
		rcu_read_unlock();
		if (!tsk)
183 184 185 186
			return -ESRCH;
	} else
		get_task_struct(tsk);

187
	memset(stats, 0, sizeof(*stats));
188 189 190 191
	/*
	 * Each accounting subsystem adds calls to its functions to
	 * fill in relevant parts of struct taskstsats as follows
	 *
192
	 *	per-task-foo(stats, tsk);
193 194
	 */

195
	delayacct_add_tsk(stats, tsk);
196 197

	/* fill in basic acct fields */
198
	stats->version = TASKSTATS_VERSION;
199
	bacct_add_tsk(stats, tsk);
200

201 202 203
	/* fill in extended acct fields */
	xacct_add_tsk(stats, tsk);

204
	/* Define err: label here if needed */
205 206 207 208 209
	put_task_struct(tsk);
	return rc;

}

210
static int fill_tgid(pid_t tgid, struct task_struct *first,
211 212
		struct taskstats *stats)
{
213
	struct task_struct *tsk;
214
	unsigned long flags;
215
	int rc = -ESRCH;
216

217 218 219 220
	/*
	 * Add additional stats from live tasks except zombie thread group
	 * leaders who are already counted with the dead tasks
	 */
221 222
	rcu_read_lock();
	if (!first)
223
		first = find_task_by_pid(tgid);
224

225 226
	if (!first || !lock_task_sighand(first, &flags))
		goto out;
227

228 229
	if (first->signal->stats)
		memcpy(stats, first->signal->stats, sizeof(*stats));
230 231
	else
		memset(stats, 0, sizeof(*stats));
232

233
	tsk = first;
234
	do {
235
		if (tsk->exit_state)
236
			continue;
237
		/*
238
		 * Accounting subsystem can call its functions here to
239 240
		 * fill in relevant parts of struct taskstsats as follows
		 *
241
		 *	per-task-foo(stats, tsk);
242
		 */
243
		delayacct_add_tsk(stats, tsk);
244

245
	} while_each_thread(first, tsk);
246

247 248 249 250 251 252
	unlock_task_sighand(first, &flags);
	rc = 0;
out:
	rcu_read_unlock();

	stats->version = TASKSTATS_VERSION;
253
	/*
254 255
	 * Accounting subsytems can also add calls here to modify
	 * fields of taskstats.
256
	 */
257
	return rc;
258 259 260 261 262 263 264
}


static void fill_tgid_exit(struct task_struct *tsk)
{
	unsigned long flags;

265
	spin_lock_irqsave(&tsk->sighand->siglock, flags);
266 267 268 269 270 271 272 273 274 275 276
	if (!tsk->signal->stats)
		goto ret;

	/*
	 * Each accounting subsystem calls its functions here to
	 * accumalate its per-task stats for tsk, into the per-tgid structure
	 *
	 *	per-task-foo(tsk->signal->stats, tsk);
	 */
	delayacct_add_tsk(tsk->signal->stats, tsk);
ret:
277
	spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
278
	return;
279 280
}

281 282 283 284 285 286
static int add_del_listener(pid_t pid, cpumask_t *maskp, int isadd)
{
	struct listener_list *listeners;
	struct listener *s, *tmp;
	unsigned int cpu;
	cpumask_t mask = *maskp;
287

288 289 290 291 292 293 294 295 296 297 298
	if (!cpus_subset(mask, cpu_possible_map))
		return -EINVAL;

	if (isadd == REGISTER) {
		for_each_cpu_mask(cpu, mask) {
			s = kmalloc_node(sizeof(struct listener), GFP_KERNEL,
					 cpu_to_node(cpu));
			if (!s)
				goto cleanup;
			s->pid = pid;
			INIT_LIST_HEAD(&s->list);
299
			s->valid = 1;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

			listeners = &per_cpu(listener_array, cpu);
			down_write(&listeners->sem);
			list_add(&s->list, &listeners->list);
			up_write(&listeners->sem);
		}
		return 0;
	}

	/* Deregister or cleanup */
cleanup:
	for_each_cpu_mask(cpu, mask) {
		listeners = &per_cpu(listener_array, cpu);
		down_write(&listeners->sem);
		list_for_each_entry_safe(s, tmp, &listeners->list, list) {
			if (s->pid == pid) {
				list_del(&s->list);
				kfree(s);
				break;
			}
		}
		up_write(&listeners->sem);
	}
	return 0;
}

static int parse(struct nlattr *na, cpumask_t *mask)
{
	char *data;
	int len;
	int ret;

	if (na == NULL)
		return 1;
	len = nla_len(na);
	if (len > TASKSTATS_CPUMASK_MAXLEN)
		return -E2BIG;
	if (len < 1)
		return -EINVAL;
	data = kmalloc(len, GFP_KERNEL);
	if (!data)
		return -ENOMEM;
	nla_strlcpy(data, na, len);
	ret = cpulist_parse(data, *mask);
	kfree(data);
	return ret;
}

348
static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
349
{
350
	struct nlattr *na, *ret;
351 352
	int aggr;

353 354 355
	aggr = (type == TASKSTATS_TYPE_PID)
			? TASKSTATS_TYPE_AGGR_PID
			: TASKSTATS_TYPE_AGGR_TGID;
356 357

	na = nla_nest_start(skb, aggr);
358 359
	if (!na)
		goto err;
360 361 362 363 364
	if (nla_put(skb, type, sizeof(pid), &pid) < 0)
		goto err;
	ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
	if (!ret)
		goto err;
365 366
	nla_nest_end(skb, na);

367 368 369
	return nla_data(ret);
err:
	return NULL;
370 371
}

372
static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
373 374 375
{
	int rc = 0;
	struct sk_buff *rep_skb;
376
	struct taskstats *stats;
377
	size_t size;
378 379 380 381 382 383 384 385 386 387 388 389 390
	cpumask_t mask;

	rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
	if (rc < 0)
		return rc;
	if (rc == 0)
		return add_del_listener(info->snd_pid, &mask, REGISTER);

	rc = parse(info->attrs[TASKSTATS_CMD_ATTR_DEREGISTER_CPUMASK], &mask);
	if (rc < 0)
		return rc;
	if (rc == 0)
		return add_del_listener(info->snd_pid, &mask, DEREGISTER);
391 392 393 394 395 396 397

	/*
	 * Size includes space for nested attributes
	 */
	size = nla_total_size(sizeof(u32)) +
		nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);

398
	rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
399 400 401
	if (rc < 0)
		return rc;

402
	rc = -EINVAL;
403 404
	if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
		u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
405 406
		stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
		if (!stats)
407
			goto err;
408

409 410
		rc = fill_pid(pid, NULL, stats);
		if (rc < 0)
411
			goto err;
412 413
	} else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
		u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
414 415
		stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
		if (!stats)
416
			goto err;
417

418 419
		rc = fill_tgid(tgid, NULL, stats);
		if (rc < 0)
420
			goto err;
421
	} else
422 423
		goto err;

424
	return send_reply(rep_skb, info->snd_pid);
425 426 427 428 429
err:
	nlmsg_free(rep_skb);
	return rc;
}

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
{
	struct signal_struct *sig = tsk->signal;
	struct taskstats *stats;

	if (sig->stats || thread_group_empty(tsk))
		goto ret;

	/* No problem if kmem_cache_zalloc() fails */
	stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);

	spin_lock_irq(&tsk->sighand->siglock);
	if (!sig->stats) {
		sig->stats = stats;
		stats = NULL;
	}
	spin_unlock_irq(&tsk->sighand->siglock);

	if (stats)
		kmem_cache_free(taskstats_cache, stats);
ret:
	return sig->stats;
}

454
/* Send pid data out on exit */
455
void taskstats_exit(struct task_struct *tsk, int group_dead)
456 457
{
	int rc;
458
	struct listener_list *listeners;
459
	struct taskstats *stats;
460 461 462 463
	struct sk_buff *rep_skb;
	size_t size;
	int is_thread_group;

464
	if (!family_registered)
465 466 467 468 469 470 471 472
		return;

	/*
	 * Size includes space for nested attributes
	 */
	size = nla_total_size(sizeof(u32)) +
		nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);

473
	is_thread_group = !!taskstats_tgid_alloc(tsk);
474 475 476 477 478 479 480
	if (is_thread_group) {
		/* PID + STATS + TGID + STATS */
		size = 2 * size;
		/* fill the tsk->signal->stats structure */
		fill_tgid_exit(tsk);
	}

481 482 483 484
	listeners = &__raw_get_cpu_var(listener_array);
	if (list_empty(&listeners->list))
		return;

485
	rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
486
	if (rc < 0)
487
		return;
488

489 490
	stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid);
	if (!stats)
491
		goto err;
492

493 494
	rc = fill_pid(tsk->pid, tsk, stats);
	if (rc < 0)
495
		goto err;
496 497

	/*
498
	 * Doesn't matter if tsk is the leader or the last group member leaving
499
	 */
500
	if (!is_thread_group || !group_dead)
501
		goto send;
502

503 504
	stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid);
	if (!stats)
505
		goto err;
506 507

	memcpy(stats, tsk->signal->stats, sizeof(*stats));
508

509
send:
510
	send_cpu_listeners(rep_skb, listeners);
511
	return;
512
err:
513 514 515 516 517
	nlmsg_free(rep_skb);
}

static struct genl_ops taskstats_ops = {
	.cmd		= TASKSTATS_CMD_GET,
518
	.doit		= taskstats_user_cmd,
519 520 521 522 523 524
	.policy		= taskstats_cmd_get_policy,
};

/* Needed early in initialization */
void __init taskstats_init_early(void)
{
525 526
	unsigned int i;

527
	taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
528 529 530 531
	for_each_possible_cpu(i) {
		INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
		init_rwsem(&(per_cpu(listener_array, i).sem));
	}
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
}

static int __init taskstats_init(void)
{
	int rc;

	rc = genl_register_family(&family);
	if (rc)
		return rc;

	rc = genl_register_ops(&family, &taskstats_ops);
	if (rc < 0)
		goto err;

	family_registered = 1;
	return 0;
err:
	genl_unregister_family(&family);
	return rc;
}

/*
 * late initcall ensures initialization of statistics collection
 * mechanisms precedes initialization of the taskstats interface
 */
late_initcall(taskstats_init);