Re: [PATCH] kernel/sched: Fix sched_fork() access an invalid sched_task_group

From: Zhang Qiao
Date: Tue Aug 31 2021 - 03:59:08 EST



hi, thanks for your reviews.

On 2021/8/31 1:32, Tejun Heo wrote:
> Hello,
>
> On Mon, Aug 30, 2021 at 04:39:54PM +0200, Peter Zijlstra wrote:
>>> When a new process is forked, cgroup_post_fork() associates it
>>> with the cgroup of its parent. Therefore this commit move the
>>> __set_task_cpu() and task_fork() that access some cgroup-related
>>> fields(sched_task_group and cfs_rq) to sched_post_fork() and
>>> call sched_post_fork() after cgroup_post_fork().
>
> I think this would allow cgroup migrations to take place before
> sched_post_fork() is run, which likely will break stuff. The right
> thing to do likely is taking sched_task_group (and whatever other
> fields) after cgroup_can_fork(), which fixates the cgroup memberships,
But it still seems possible that it accessed an invalid sched_task_group?
because the child process does not update its sched_task_group util
cgroup_post_fork().
> is run. For other controllers, operations like this would be performed
> from cgroup_subsys->fork() callback but it's tricky for sched due to
> autogroup.
>
>>> Fixes: 8323f26ce342 ("sched: Fix race in task_group")
>>> Signed-off-by: Zhang Qiao <zhangqiao22@xxxxxxxxxx>
>>
>> Hmm, I think you're right. Did something recently chagne in cgroup land
>> to make this more visible? This code hasn't changed in like 9 years.
I think this problem has always existed. I've reproduced it in multiple versions,
including 3.10 and 5.14-rc3.

>
> I can't think of any remotely recent change either. I guess ppl just
> don't try to migrate the parent while fork is in progress.
>
> Thanks.
>

thandks.