After I fixed access to task->tgid in kernel/acct.c, Oleg pointed out some bad side effects with this accounting vs pid namespaces interaction. I.e. when some task in pid namespace sets this accounting up, this blocks all the others from doing the same. Restricting this to init namespace only could help, but didn't look as a grace solution.

So here is the approach to make this accounting work with pidnamespaces properly.

The idea is simple - when a task dies it accounts itself in each namespace it is visible from and which set the accounting up.

For example here are the commands run and the output of lastcomm from init and sub namespaces: