Commit 51de4d90 authored by Oleg Nesterov's avatar Oleg Nesterov Committed by Linus Torvalds

[PATCH] taskstats: use nla_reserve() for reply assembling

Currently taskstats_user_cmd()/taskstats_exit() do:

	1) allocate stats
	2) fill stats
	3) make a temporary copy on stack (236 bytes)
	4) copy that copy to skb
	5) free stats

With the help of nla_reserve() we can operate on skb->data directly,
thus avoiding all these steps except 2).

So, before this patch:

	// copy *stats to skb->data
	int mk_reply(skb, ..., struct taskstats *stats);

	fill_pid(stats);
	mk_reply(skb, ..., stats);

After:
	// return a pointer to skb->data
	struct taskstats *mk_reply(skb, ...);

	stat = mk_reply(skb, ...);
	fill_pid(stats);

Shrinks taskatsks.o by 162 bytes.

A stupid benchmark (send one million TASKSTATS_CMD_ATTR_PID) shows the

		real user sys
	before:
		4.02 0.06 3.96
		4.02 0.04 3.98
		4.02 0.04 3.97
	after:
		3.86 0.08 3.78
		3.88 0.10 3.77
		3.89 0.09 3.80

but this looks suspiciously good.
Signed-off-by: default avatarOleg Nesterov <oleg@tv-sign.ru>
Acked-by: default avatarShailabh Nagar <nagar@watson.ibm.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Cc: Jay Lan <jlan@sgi.com>
Cc: Thomas Graf <tgraf@suug.ch>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 68062b86
...@@ -185,6 +185,7 @@ static int fill_pid(pid_t pid, struct task_struct *tsk, ...@@ -185,6 +185,7 @@ static int fill_pid(pid_t pid, struct task_struct *tsk,
} else } else
get_task_struct(tsk); get_task_struct(tsk);
memset(stats, 0, sizeof(*stats));
/* /*
* Each accounting subsystem adds calls to its functions to * Each accounting subsystem adds calls to its functions to
* fill in relevant parts of struct taskstsats as follows * fill in relevant parts of struct taskstsats as follows
...@@ -227,6 +228,8 @@ static int fill_tgid(pid_t tgid, struct task_struct *first, ...@@ -227,6 +228,8 @@ static int fill_tgid(pid_t tgid, struct task_struct *first,
if (first->signal->stats) if (first->signal->stats)
memcpy(stats, first->signal->stats, sizeof(*stats)); memcpy(stats, first->signal->stats, sizeof(*stats));
else
memset(stats, 0, sizeof(*stats));
tsk = first; tsk = first;
do { do {
...@@ -343,9 +346,9 @@ static int parse(struct nlattr *na, cpumask_t *mask) ...@@ -343,9 +346,9 @@ static int parse(struct nlattr *na, cpumask_t *mask)
return ret; return ret;
} }
static int mk_reply(struct sk_buff *skb, int type, u32 pid, struct taskstats *stats) static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
{ {
struct nlattr *na; struct nlattr *na, *ret;
int aggr; int aggr;
aggr = TASKSTATS_TYPE_AGGR_TGID; aggr = TASKSTATS_TYPE_AGGR_TGID;
...@@ -353,20 +356,23 @@ static int mk_reply(struct sk_buff *skb, int type, u32 pid, struct taskstats *st ...@@ -353,20 +356,23 @@ static int mk_reply(struct sk_buff *skb, int type, u32 pid, struct taskstats *st
aggr = TASKSTATS_TYPE_AGGR_PID; aggr = TASKSTATS_TYPE_AGGR_PID;
na = nla_nest_start(skb, aggr); na = nla_nest_start(skb, aggr);
NLA_PUT_U32(skb, type, pid); if (nla_put(skb, type, sizeof(pid), &pid) < 0)
NLA_PUT_TYPE(skb, struct taskstats, TASKSTATS_TYPE_STATS, *stats); goto err;
ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
if (!ret)
goto err;
nla_nest_end(skb, na); nla_nest_end(skb, na);
return 0; return nla_data(ret);
nla_put_failure: err:
return -1; return NULL;
} }
static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info) static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
{ {
int rc = 0; int rc = 0;
struct sk_buff *rep_skb; struct sk_buff *rep_skb;
struct taskstats stats; struct taskstats *stats;
void *reply; void *reply;
size_t size; size_t size;
cpumask_t mask; cpumask_t mask;
...@@ -389,36 +395,36 @@ static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info) ...@@ -389,36 +395,36 @@ static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
size = nla_total_size(sizeof(u32)) + size = nla_total_size(sizeof(u32)) +
nla_total_size(sizeof(struct taskstats)) + nla_total_size(0); nla_total_size(sizeof(struct taskstats)) + nla_total_size(0);
memset(&stats, 0, sizeof(stats));
rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size); rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
if (rc < 0) if (rc < 0)
return rc; return rc;
rc = -EINVAL;
if (info->attrs[TASKSTATS_CMD_ATTR_PID]) { if (info->attrs[TASKSTATS_CMD_ATTR_PID]) {
u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]); u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
rc = fill_pid(pid, NULL, &stats); stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
if (rc < 0) if (!stats)
goto err; goto nla_err;
if (mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid, &stats)) rc = fill_pid(pid, NULL, stats);
goto nla_put_failure; if (rc < 0)
goto nla_err;
} else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) { } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]); u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
rc = fill_tgid(tgid, NULL, &stats); stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
if (rc < 0) if (!stats)
goto err; goto nla_err;
if (mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid, &stats)) rc = fill_tgid(tgid, NULL, stats);
goto nla_put_failure; if (rc < 0)
} else { goto nla_err;
rc = -EINVAL; } else
goto err; goto err;
}
return send_reply(rep_skb, info->snd_pid); return send_reply(rep_skb, info->snd_pid);
nla_put_failure: nla_err:
rc = genlmsg_cancel(rep_skb, reply); genlmsg_cancel(rep_skb, reply);
err: err:
nlmsg_free(rep_skb); nlmsg_free(rep_skb);
return rc; return rc;
...@@ -453,7 +459,7 @@ void taskstats_exit(struct task_struct *tsk, int group_dead) ...@@ -453,7 +459,7 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
{ {
int rc; int rc;
struct listener_list *listeners; struct listener_list *listeners;
struct taskstats *tidstats; struct taskstats *stats;
struct sk_buff *rep_skb; struct sk_buff *rep_skb;
void *reply; void *reply;
size_t size; size_t size;
...@@ -480,20 +486,17 @@ void taskstats_exit(struct task_struct *tsk, int group_dead) ...@@ -480,20 +486,17 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
if (list_empty(&listeners->list)) if (list_empty(&listeners->list))
return; return;
tidstats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL);
if (!tidstats)
return;
rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size); rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size);
if (rc < 0) if (rc < 0)
goto free_stats; return;
rc = fill_pid(tsk->pid, tsk, tidstats); stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid);
if (rc < 0) if (!stats)
goto err_skb; goto nla_err;
if (mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid, tidstats)) rc = fill_pid(tsk->pid, tsk, stats);
goto nla_put_failure; if (rc < 0)
goto nla_err;
/* /*
* Doesn't matter if tsk is the leader or the last group member leaving * Doesn't matter if tsk is the leader or the last group member leaving
...@@ -501,20 +504,19 @@ void taskstats_exit(struct task_struct *tsk, int group_dead) ...@@ -501,20 +504,19 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
if (!is_thread_group || !group_dead) if (!is_thread_group || !group_dead)
goto send; goto send;
if (mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid, tsk->signal->stats)) stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid);
goto nla_put_failure; if (!stats)
goto nla_err;
memcpy(stats, tsk->signal->stats, sizeof(*stats));
send: send:
send_cpu_listeners(rep_skb, listeners); send_cpu_listeners(rep_skb, listeners);
free_stats:
kmem_cache_free(taskstats_cache, tidstats);
return; return;
nla_put_failure: nla_err:
genlmsg_cancel(rep_skb, reply); genlmsg_cancel(rep_skb, reply);
err_skb:
nlmsg_free(rep_skb); nlmsg_free(rep_skb);
goto free_stats;
} }
static struct genl_ops taskstats_ops = { static struct genl_ops taskstats_ops = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment