提交 37167485 编写于 作者: O Oleg Nesterov 提交者: Linus Torvalds

[PATCH] taskstats: cleanup reply assembling

Thomas Graf wrote:
>
> nla_nest_start() may return NULL, either rely on prepare_reply() to be
> correct and BUG() on failure or do proper error handling for all
> functions.

nla_put() in taskstat.c can fail only if the 'size' argument of alloc_skb()
was not right. This is a kernel bug, we should not hide it. So add 'BUG()'
on error path and check for 'na == NULL'.

> genlmsg_cancel() is only required in error paths for dumping
> procedures.

So we can remove 'genlmsg_cancel()' calls and 'void *reply' (saves 227 bytes).
Signed-off-by: NOleg Nesterov <oleg@tv-sign.ru>
Cc: Thomas Graf <tgraf@suug.ch>
Cc: Shailabh Nagar <nagar@watson.ibm.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Cc: Jay Lan <jlan@sgi.com>
Signed-off-by: NAndrew Morton <akpm@osdl.org>
Signed-off-by: NLinus Torvalds <torvalds@osdl.org>
上级 51de4d90
...@@ -69,7 +69,7 @@ enum actions { ...@@ -69,7 +69,7 @@ enum actions {
}; };
static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp, static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
void **replyp, size_t size) size_t size)
{ {
struct sk_buff *skb; struct sk_buff *skb;
void *reply; void *reply;
...@@ -94,7 +94,6 @@ static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp, ...@@ -94,7 +94,6 @@ static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
} }
*skbp = skb; *skbp = skb;
*replyp = reply;
return 0; return 0;
} }
...@@ -351,11 +350,13 @@ static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid) ...@@ -351,11 +350,13 @@ static struct taskstats *mk_reply(struct sk_buff *skb, int type, u32 pid)
struct nlattr *na, *ret; struct nlattr *na, *ret;
int aggr; int aggr;
aggr = TASKSTATS_TYPE_AGGR_TGID; aggr = (type == TASKSTATS_TYPE_PID)
if (type == TASKSTATS_TYPE_PID) ? TASKSTATS_TYPE_AGGR_PID
aggr = TASKSTATS_TYPE_AGGR_PID; : TASKSTATS_TYPE_AGGR_TGID;
na = nla_nest_start(skb, aggr); na = nla_nest_start(skb, aggr);
if (!na)
goto err;
if (nla_put(skb, type, sizeof(pid), &pid) < 0) if (nla_put(skb, type, sizeof(pid), &pid) < 0)
goto err; goto err;
ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats)); ret = nla_reserve(skb, TASKSTATS_TYPE_STATS, sizeof(struct taskstats));
...@@ -373,7 +374,6 @@ static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info) ...@@ -373,7 +374,6 @@ 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;
size_t size; size_t size;
cpumask_t mask; cpumask_t mask;
...@@ -395,7 +395,7 @@ static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info) ...@@ -395,7 +395,7 @@ 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);
rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, &reply, size); rc = prepare_reply(info, TASKSTATS_CMD_NEW, &rep_skb, size);
if (rc < 0) if (rc < 0)
return rc; return rc;
...@@ -404,27 +404,24 @@ static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info) ...@@ -404,27 +404,24 @@ static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]); u32 pid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_PID]);
stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid); stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid);
if (!stats) if (!stats)
goto nla_err; goto err;
rc = fill_pid(pid, NULL, stats); rc = fill_pid(pid, NULL, stats);
if (rc < 0) if (rc < 0)
goto nla_err; goto 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]);
stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid); stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid);
if (!stats) if (!stats)
goto nla_err; goto err;
rc = fill_tgid(tgid, NULL, stats); rc = fill_tgid(tgid, NULL, stats);
if (rc < 0) if (rc < 0)
goto nla_err; goto err;
} else } else
goto err; goto err;
return send_reply(rep_skb, info->snd_pid); return send_reply(rep_skb, info->snd_pid);
nla_err:
genlmsg_cancel(rep_skb, reply);
err: err:
nlmsg_free(rep_skb); nlmsg_free(rep_skb);
return rc; return rc;
...@@ -461,7 +458,6 @@ void taskstats_exit(struct task_struct *tsk, int group_dead) ...@@ -461,7 +458,6 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
struct listener_list *listeners; struct listener_list *listeners;
struct taskstats *stats; struct taskstats *stats;
struct sk_buff *rep_skb; struct sk_buff *rep_skb;
void *reply;
size_t size; size_t size;
int is_thread_group; int is_thread_group;
...@@ -486,17 +482,17 @@ void taskstats_exit(struct task_struct *tsk, int group_dead) ...@@ -486,17 +482,17 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
if (list_empty(&listeners->list)) if (list_empty(&listeners->list))
return; return;
rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, &reply, size); rc = prepare_reply(NULL, TASKSTATS_CMD_NEW, &rep_skb, size);
if (rc < 0) if (rc < 0)
return; return;
stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid); stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid);
if (!stats) if (!stats)
goto nla_err; goto err;
rc = fill_pid(tsk->pid, tsk, stats); rc = fill_pid(tsk->pid, tsk, stats);
if (rc < 0) if (rc < 0)
goto nla_err; goto 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
...@@ -506,16 +502,14 @@ void taskstats_exit(struct task_struct *tsk, int group_dead) ...@@ -506,16 +502,14 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid); stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid);
if (!stats) if (!stats)
goto nla_err; goto err;
memcpy(stats, tsk->signal->stats, sizeof(*stats)); memcpy(stats, tsk->signal->stats, sizeof(*stats));
send: send:
send_cpu_listeners(rep_skb, listeners); send_cpu_listeners(rep_skb, listeners);
return; return;
err:
nla_err:
genlmsg_cancel(rep_skb, reply);
nlmsg_free(rep_skb); nlmsg_free(rep_skb);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册