2006-11-01 18:26:55

by Oleg Nesterov

[permalink] [raw]
Subject: [PATCH 1/2] taskstats: factor out reply assembling

Introduce mk_reply() helper which does all nla_put()s on reply.

Saves 453 bytes and a preparation for the next patch.

Signed-off-by: Oleg Nesterov <[email protected]>

taskstats.c | 55 ++++++++++++++++++++++++++++---------------------------
1 files changed, 28 insertions(+), 27 deletions(-)

--- STATS/kernel/taskstats.c~5_factor 2006-10-31 16:33:56.000000000 +0300
+++ STATS/kernel/taskstats.c 2006-11-01 14:00:03.000000000 +0300
@@ -348,6 +348,25 @@ static int parse(struct nlattr *na, cpum
return ret;
}

+static int mk_reply(struct sk_buff *skb, int type, u32 pid, struct taskstats *stats)
+{
+ struct nlattr *na;
+ int aggr;
+
+ aggr = TASKSTATS_TYPE_AGGR_TGID;
+ if (type == TASKSTATS_TYPE_PID)
+ aggr = TASKSTATS_TYPE_AGGR_PID;
+
+ na = nla_nest_start(skb, aggr);
+ NLA_PUT_U32(skb, type, pid);
+ NLA_PUT_TYPE(skb, struct taskstats, TASKSTATS_TYPE_STATS, *stats);
+ nla_nest_end(skb, na);
+
+ return 0;
+nla_put_failure:
+ return -1;
+}
+
static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
{
int rc = 0;
@@ -355,7 +374,6 @@ static int taskstats_user_cmd(struct sk_
struct taskstats stats;
void *reply;
size_t size;
- struct nlattr *na;
cpumask_t mask;

rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
@@ -387,27 +405,21 @@ static int taskstats_user_cmd(struct sk_
if (rc < 0)
goto err;

- na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
- NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
- NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
- stats);
+ if (mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid, &stats))
+ goto nla_put_failure;
} else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
rc = fill_tgid(tgid, NULL, &stats);
if (rc < 0)
goto err;

- na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
- NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
- NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
- stats);
+ if (mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid, &stats))
+ goto nla_put_failure;
} else {
rc = -EINVAL;
goto err;
}

- nla_nest_end(rep_skb, na);
-
return send_reply(rep_skb, info->snd_pid);

nla_put_failure:
@@ -451,7 +463,6 @@ void taskstats_exit(struct task_struct *
void *reply;
size_t size;
int is_thread_group;
- struct nlattr *na;

if (!family_registered)
return;
@@ -486,27 +497,17 @@ void taskstats_exit(struct task_struct *
if (rc < 0)
goto err_skb;

- na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
- NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
- NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
- *tidstats);
- nla_nest_end(rep_skb, na);
-
- if (!is_thread_group)
- goto send;
+ if (mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid, tidstats))
+ goto nla_put_failure;

/*
* Doesn't matter if tsk is the leader or the last group member leaving
*/
- if (!group_dead)
+ if (!is_thread_group || !group_dead)
goto send;

- na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
- NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
- /* No locking needed for tsk->signal->stats since group is dead */
- NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
- *tsk->signal->stats);
- nla_nest_end(rep_skb, na);
+ if (mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid, tsk->signal->stats))
+ goto nla_put_failure;

send:
send_cpu_listeners(rep_skb, listeners);


2006-11-01 20:31:32

by Shailabh Nagar

[permalink] [raw]
Subject: Re: [PATCH 1/2] taskstats: factor out reply assembling

Oleg Nesterov wrote:
> Introduce mk_reply() helper which does all nla_put()s on reply.
>
> Saves 453 bytes and a preparation for the next patch.
>
> Signed-off-by: Oleg Nesterov <[email protected]>

Acked-by: Shailabh Nagar <[email protected]>

>
> taskstats.c | 55 ++++++++++++++++++++++++++++---------------------------
> 1 files changed, 28 insertions(+), 27 deletions(-)
>
> --- STATS/kernel/taskstats.c~5_factor 2006-10-31 16:33:56.000000000 +0300
> +++ STATS/kernel/taskstats.c 2006-11-01 14:00:03.000000000 +0300
> @@ -348,6 +348,25 @@ static int parse(struct nlattr *na, cpum
> return ret;
> }
>
> +static int mk_reply(struct sk_buff *skb, int type, u32 pid, struct taskstats *stats)
> +{
> + struct nlattr *na;
> + int aggr;
> +
> + aggr = TASKSTATS_TYPE_AGGR_TGID;
> + if (type == TASKSTATS_TYPE_PID)
> + aggr = TASKSTATS_TYPE_AGGR_PID;
> +
> + na = nla_nest_start(skb, aggr);
> + NLA_PUT_U32(skb, type, pid);
> + NLA_PUT_TYPE(skb, struct taskstats, TASKSTATS_TYPE_STATS, *stats);
> + nla_nest_end(skb, na);
> +
> + return 0;
> +nla_put_failure:
> + return -1;
> +}
> +
> static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
> {
> int rc = 0;
> @@ -355,7 +374,6 @@ static int taskstats_user_cmd(struct sk_
> struct taskstats stats;
> void *reply;
> size_t size;
> - struct nlattr *na;
> cpumask_t mask;
>
> rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
> @@ -387,27 +405,21 @@ static int taskstats_user_cmd(struct sk_
> if (rc < 0)
> goto err;
>
> - na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
> - NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
> - NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
> - stats);
> + if (mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid, &stats))
> + goto nla_put_failure;
> } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
> u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
> rc = fill_tgid(tgid, NULL, &stats);
> if (rc < 0)
> goto err;
>
> - na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
> - NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
> - NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
> - stats);
> + if (mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid, &stats))
> + goto nla_put_failure;
> } else {
> rc = -EINVAL;
> goto err;
> }
>
> - nla_nest_end(rep_skb, na);
> -
> return send_reply(rep_skb, info->snd_pid);
>
> nla_put_failure:
> @@ -451,7 +463,6 @@ void taskstats_exit(struct task_struct *
> void *reply;
> size_t size;
> int is_thread_group;
> - struct nlattr *na;
>
> if (!family_registered)
> return;
> @@ -486,27 +497,17 @@ void taskstats_exit(struct task_struct *
> if (rc < 0)
> goto err_skb;
>
> - na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
> - NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
> - NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
> - *tidstats);
> - nla_nest_end(rep_skb, na);
> -
> - if (!is_thread_group)
> - goto send;
> + if (mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid, tidstats))
> + goto nla_put_failure;
>
> /*
> * Doesn't matter if tsk is the leader or the last group member leaving
> */
> - if (!group_dead)
> + if (!is_thread_group || !group_dead)
> goto send;
>
> - na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
> - NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
> - /* No locking needed for tsk->signal->stats since group is dead */
> - NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
> - *tsk->signal->stats);
> - nla_nest_end(rep_skb, na);
> + if (mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid, tsk->signal->stats))
> + goto nla_put_failure;
>
> send:
> send_cpu_listeners(rep_skb, listeners);
>

2006-11-02 05:14:08

by Balbir Singh

[permalink] [raw]
Subject: Re: [PATCH 1/2] taskstats: factor out reply assembling

Oleg Nesterov wrote:
> Introduce mk_reply() helper which does all nla_put()s on reply.
>
> Saves 453 bytes and a preparation for the next patch.
>
> Signed-off-by: Oleg Nesterov <[email protected]>
>
> taskstats.c | 55 ++++++++++++++++++++++++++++---------------------------
> 1 files changed, 28 insertions(+), 27 deletions(-)
>
> --- STATS/kernel/taskstats.c~5_factor 2006-10-31 16:33:56.000000000 +0300
> +++ STATS/kernel/taskstats.c 2006-11-01 14:00:03.000000000 +0300
> @@ -348,6 +348,25 @@ static int parse(struct nlattr *na, cpum
> return ret;
> }
>
> +static int mk_reply(struct sk_buff *skb, int type, u32 pid, struct taskstats *stats)
> +{
> + struct nlattr *na;
> + int aggr;
> +
> + aggr = TASKSTATS_TYPE_AGGR_TGID;
> + if (type == TASKSTATS_TYPE_PID)
> + aggr = TASKSTATS_TYPE_AGGR_PID;
> +
> + na = nla_nest_start(skb, aggr);
> + NLA_PUT_U32(skb, type, pid);
> + NLA_PUT_TYPE(skb, struct taskstats, TASKSTATS_TYPE_STATS, *stats);
> + nla_nest_end(skb, na);
> +
> + return 0;
> +nla_put_failure:
> + return -1;
> +}
> +

How about using

aggr = (type == TASKSTATS_TYPE_PID) ? TASKSTATS_TYPE_AGGR_PID :
TASKSTATS_TYPE_AGGR_TGID;



> static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info)
> {
> int rc = 0;
> @@ -355,7 +374,6 @@ static int taskstats_user_cmd(struct sk_
> struct taskstats stats;
> void *reply;
> size_t size;
> - struct nlattr *na;
> cpumask_t mask;
>
> rc = parse(info->attrs[TASKSTATS_CMD_ATTR_REGISTER_CPUMASK], &mask);
> @@ -387,27 +405,21 @@ static int taskstats_user_cmd(struct sk_
> if (rc < 0)
> goto err;
>
> - na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
> - NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, pid);
> - NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
> - stats);
> + if (mk_reply(rep_skb, TASKSTATS_TYPE_PID, pid, &stats))
> + goto nla_put_failure;
> } else if (info->attrs[TASKSTATS_CMD_ATTR_TGID]) {
> u32 tgid = nla_get_u32(info->attrs[TASKSTATS_CMD_ATTR_TGID]);
> rc = fill_tgid(tgid, NULL, &stats);
> if (rc < 0)
> goto err;
>
> - na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
> - NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, tgid);
> - NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
> - stats);
> + if (mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tgid, &stats))
> + goto nla_put_failure;
> } else {
> rc = -EINVAL;
> goto err;
> }
>
> - nla_nest_end(rep_skb, na);
> -
> return send_reply(rep_skb, info->snd_pid);
>
> nla_put_failure:
> @@ -451,7 +463,6 @@ void taskstats_exit(struct task_struct *
> void *reply;
> size_t size;
> int is_thread_group;
> - struct nlattr *na;
>
> if (!family_registered)
> return;
> @@ -486,27 +497,17 @@ void taskstats_exit(struct task_struct *
> if (rc < 0)
> goto err_skb;
>
> - na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_PID);
> - NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_PID, (u32)tsk->pid);
> - NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
> - *tidstats);
> - nla_nest_end(rep_skb, na);
> -
> - if (!is_thread_group)
> - goto send;
> + if (mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid, tidstats))
> + goto nla_put_failure;
>
> /*
> * Doesn't matter if tsk is the leader or the last group member leaving
> */
> - if (!group_dead)
> + if (!is_thread_group || !group_dead)
> goto send;
>
> - na = nla_nest_start(rep_skb, TASKSTATS_TYPE_AGGR_TGID);
> - NLA_PUT_U32(rep_skb, TASKSTATS_TYPE_TGID, (u32)tsk->tgid);
> - /* No locking needed for tsk->signal->stats since group is dead */
> - NLA_PUT_TYPE(rep_skb, struct taskstats, TASKSTATS_TYPE_STATS,
> - *tsk->signal->stats);
> - nla_nest_end(rep_skb, na);
> + if (mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid, tsk->signal->stats))
> + goto nla_put_failure;
>
> send:
> send_cpu_listeners(rep_skb, listeners);
>

Looks good!

Acked-by: Balbir Singh <[email protected]>

--

Balbir Singh,
Linux Technology Center,
IBM Software Labs

2006-11-02 10:33:43

by Thomas Graf

[permalink] [raw]
Subject: Re: [PATCH 1/2] taskstats: factor out reply assembling

* Oleg Nesterov <[email protected]> 2006-11-01 21:26
> Introduce mk_reply() helper which does all nla_put()s on reply.
>
> Saves 453 bytes and a preparation for the next patch.
>
> Signed-off-by: Oleg Nesterov <[email protected]>
>
> taskstats.c | 55 ++++++++++++++++++++++++++++---------------------------
> 1 files changed, 28 insertions(+), 27 deletions(-)
>
> --- STATS/kernel/taskstats.c~5_factor 2006-10-31 16:33:56.000000000 +0300
> +++ STATS/kernel/taskstats.c 2006-11-01 14:00:03.000000000 +0300
> @@ -348,6 +348,25 @@ static int parse(struct nlattr *na, cpum
> return ret;
> }
>
> +static int mk_reply(struct sk_buff *skb, int type, u32 pid, struct taskstats *stats)
> +{
> + struct nlattr *na;
> + int aggr;
> +
> + aggr = TASKSTATS_TYPE_AGGR_TGID;
> + if (type == TASKSTATS_TYPE_PID)
> + aggr = TASKSTATS_TYPE_AGGR_PID;
> +
> + na = nla_nest_start(skb, aggr);
> + NLA_PUT_U32(skb, type, pid);
> + NLA_PUT_TYPE(skb, struct taskstats, TASKSTATS_TYPE_STATS, *stats);
> + nla_nest_end(skb, na);
> +
> + return 0;
> +nla_put_failure:
> + return -1;
> +}

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.

2006-11-02 12:37:36

by Shailabh Nagar

[permalink] [raw]
Subject: Re: [PATCH 1/2] taskstats: factor out reply assembling

Thomas Graf wrote:
> * Oleg Nesterov <[email protected]> 2006-11-01 21:26
>> Introduce mk_reply() helper which does all nla_put()s on reply.
>>
>> Saves 453 bytes and a preparation for the next patch.
>>
>> Signed-off-by: Oleg Nesterov <[email protected]>
>>
>> taskstats.c | 55 ++++++++++++++++++++++++++++---------------------------
>> 1 files changed, 28 insertions(+), 27 deletions(-)
>>
>> --- STATS/kernel/taskstats.c~5_factor 2006-10-31 16:33:56.000000000 +0300
>> +++ STATS/kernel/taskstats.c 2006-11-01 14:00:03.000000000 +0300
>> @@ -348,6 +348,25 @@ static int parse(struct nlattr *na, cpum
>> return ret;
>> }
>>
>> +static int mk_reply(struct sk_buff *skb, int type, u32 pid, struct taskstats *stats)
>> +{
>> + struct nlattr *na;
>> + int aggr;
>> +
>> + aggr = TASKSTATS_TYPE_AGGR_TGID;
>> + if (type == TASKSTATS_TYPE_PID)
>> + aggr = TASKSTATS_TYPE_AGGR_PID;
>> +
>> + na = nla_nest_start(skb, aggr);

if (!na)
goto nla_put_failure;

>> + NLA_PUT_U32(skb, type, pid);
>> + NLA_PUT_TYPE(skb, struct taskstats, TASKSTATS_TYPE_STATS, *stats);
>> + nla_nest_end(skb, na);
>> +
>> + return 0;
>> +nla_put_failure:
>> + return -1;
>> +}
>
> 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.

Thanks for catching that bug which was present in the unrefactored code.
The error path from prepare_reply() which generates the skb is being
handled already.

But as you point out, nla_nest_start failure, unlike those of the NLA_PUT_*
macros, isn't handled by having a nla_put_failure label alone. So we'll
need to add something like the above since we certainly don't want a BUG()
on failure !

Thanks,
Shailabh


2006-11-02 13:23:17

by Oleg Nesterov

[permalink] [raw]
Subject: Re: [PATCH 1/2] taskstats: factor out reply assembling

On 11/02, Balbir Singh wrote:
>
> Oleg Nesterov wrote:
> > +
> > + aggr = TASKSTATS_TYPE_AGGR_TGID;
> > + if (type == TASKSTATS_TYPE_PID)
> > + aggr = TASKSTATS_TYPE_AGGR_PID;
>
> How about using
>
> aggr = (type == TASKSTATS_TYPE_PID) ? TASKSTATS_TYPE_AGGR_PID :
> TASKSTATS_TYPE_AGGR_TGID;

I personally think this is much better. In fact, i did exactly same, but
then changed it because (I think) CodingStyle police doesn't like '?:'.

Or it does?

Oleg.

2006-11-02 13:45:04

by Balbir Singh

[permalink] [raw]
Subject: Re: [PATCH 1/2] taskstats: factor out reply assembling

Oleg Nesterov wrote:
> On 11/02, Balbir Singh wrote:
>> Oleg Nesterov wrote:
>>> +
>>> + aggr = TASKSTATS_TYPE_AGGR_TGID;
>>> + if (type == TASKSTATS_TYPE_PID)
>>> + aggr = TASKSTATS_TYPE_AGGR_PID;
>> How about using
>>
>> aggr = (type == TASKSTATS_TYPE_PID) ? TASKSTATS_TYPE_AGGR_PID :
>> TASKSTATS_TYPE_AGGR_TGID;
>
> I personally think this is much better. In fact, i did exactly same, but
> then changed it because (I think) CodingStyle police doesn't like '?:'.
>
> Or it does?
>
> Oleg.
>

There is an issue with double assignment if the type is TASKSTATS_TYPE_PID most
of the time. I see the '?' operator in several places in the code.

--

Balbir Singh,
Linux Technology Center,
IBM Software Labs