Re: [PATCH v10 1/2] fork: extend clone3() to support setting a PID

From: Eric W. Biederman
Date: Sat Nov 16 2019 - 17:57:01 EST


Christian Brauner <christian.brauner@xxxxxxxxxx> writes:

> On Fri, Nov 15, 2019 at 11:49:10AM +0100, Oleg Nesterov wrote:
>> On 11/15, Christian Brauner wrote:
>> >
>> > +static int set_tid_next(pid_t *set_tid, size_t *size, int idx)
>> > +{
>> > + int tid = 0;
>> > +
>> > + if (*size) {
>> > + tid = set_tid[idx];
>> > + if (tid < 1 || tid >= pid_max)
>> > + return -EINVAL;
>> > +
>> > + /*
>> > + * Also fail if a PID != 1 is requested and
>> > + * no PID 1 exists.
>> > + */
>> > + if (tid != 1 && !tmp->child_reaper)
>> > + return -EINVAL;
>> > +
>> > + if (!ns_capable(tmp->user_ns, CAP_SYS_ADMIN))
>> > + return -EPERM;
>> > +
>> > + (*size)--;
>> > + }
>>
>> this needs more args, struct pid_namespace *tmp + pid_t pid_max
>> if (set_tid_size) {
>> tid = set_tid[ns->level - i];
>>
>> retval = -EINVAL;
>> if (tid < 1 || tid >= pid_max)
>> goto out_free;
>
> I'm not a fan of this pattern of _not_ setting error codes in the actual
> error path t but I won't object.

If you can show a compiler that actually manages to reliably perform
that code motion it is worth discussing making it go away. Last I
checked gcc will emit an extra basic block just to handle setting
the error code before jumping to the out_free. That extra basic block
because of the extra jump tends to be costly.

Not a huge cost but in these days when branches are getting increasingly
expensive and Moore's law wrapping up I prefer code patterns that
generate cood code.

Eric