irmap: Get root mntfd before releasing tasks on predump

We have a use-after-free in predump code:

1st the free_pstree() is called in pre_dump_tasks(), then we
go to irmap_predump_run() which may call the lookup_irmap()
which, in turn, dereferences the root_item to get the root
mount ns fd.

But the problem is bigger than that. After we've released the
tasks (done before freeing pstree on predump) we can no longer
access them by PIDs, so keeping the root-item after irmap
scan is not a fix.

Fix is to get the root fd before releasing the tasks and using
one in irmap scanner.

Caught recently on iterative inotify_irmap test.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
Acked-by: Andrew Vagin <avagin@parallels.com>
ns: Introduce collect_namespaces routine

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
ns: Introduce collect_net_namespaces

And move sockets collection there.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
net: Do walk net namespaces to collect

Right now we don't support multiple net namespaces,
but some day we will. Other than this we have a logic
to distinguish cases with no namespaces vs one namespace,
so this walking already makes sence.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
net: Pre-create nl diag sk

The setns() syscall (called by switch_ns()) can be extremely
slow. If we call it two or more times from the same task the
kernel will synchonously go on a very slow routine called
synchronize_rcu() trying to put a reference on old namespaces.

To avoid doing this more than once I propose to create all
per-ns sockets in one place with one setns call. In this
patch there's on nl diag socket used to collect other sockets
is created this way.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
rst: Rework the rst_info referencing

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
pstree: Introduce item's dump info

Empty for now, will be filled soon.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
ns: Reshuffle nsid generation code

To make it possible to get ns_id object together
with its ID.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
pstree: Store task's netns on pstree-item

Will be needed for parasite sockets.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
parasite: Reshuffle tsock creation

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
parasite: Precreate daemon control sockets

Now we have netns on pstree-item and have the place
where to pre-create daemon socket in needed namespace.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
predump: Collect mnt and net namespaces properly

On pre-dump we collect only two namespaces -- the mnt one
for criu and mnt one again for root task.

This is not correct. We need all mount namespaces to make
the irmap generation work properly and we need all net
namespaces to have parasite sockets created.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
cg: don't leak properties when cg dir exists

We're using NULL as a sentinel here to indicate that we shouldn't restore any
cgroup properties. We should make sure that we don't leak this information and
instead check the n_properties field, which we should also set correctly.

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
net: Move NETLINK_INET_DIAG from socket.c to net.c

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
bfd: Use ERR_PTR and such instead of BREADERR

No need to invent new error codes here, simply
use ERR_PTR/IS_ERR_OR_NULL and such.

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
restore: copy special cpuset props recursively

The symptom of this bug was that users restoring tasks to a nested cgroup where
the top level group was created by criu (and not previously configured) e.g.
cpuset:/lxc/u1 would get an ENOSPC. criu would try to copy the special
properties into /lxc/u1 directly and (silently) fail, and then tried to copy
the task into the cg and fail with ENOSPC:

ENOSPC Attempted  to  write(2)  an empty cpuset.cpus or cpuset.mems setting to
       a cpuset that has tasks attached.

Fixing the silent failure to a loud failure, it gave EACCES:

EACCES Attempted to add, using write(2), a CPU or memory node to a cpuset, when
       that CPU or memory node was not already in its parent.

So, we need to copy the the special props down the entire tree. Additionally,
we shouldn't copy props directly from the top, since some intermediate point in
the tree could add restrictions. We first walk back up the tree to find the
first point where the props are empty, and then copy that parent's props all
the way down.

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
test: expand the default test set

* check page server
* check snapshots
* check a few iterations of dump/restore

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
mount: proc-parse -- Show @mnt_id on debug print as well

This is convenient when need to lookup into debug prints
and check which mount point were used somewhere else
(in particular I will need @mnt_id in tty code so
 on error I can easily figure out which mountpoint has
 been used).

No func changes.

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
cpuinfo: x86 -- Drop redundant cpu_has helper

It's simply a wrapper over cpu_has_feature,
so use this it directly instead.

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
cpuinfo: Drop cpu_set_feature from exporting

It's redundant, should be cpu local.

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>