migration: Fix iocs leaks during file and fd migration
authorFabiano Rosas <farosas@suse.de>
Wed, 13 Mar 2024 21:28:23 +0000 (18:28 -0300)
committerPeter Xu <peterx@redhat.com>
Thu, 14 Mar 2024 15:39:08 +0000 (11:39 -0400)
commit74228c598f139bd9ce7839794be9a3ccc180fb27
tree0f7fd35b9c0054f96ad865bdd5ae75d79d72445b
parent20e6b1565306c9f537225e633c9a9fb67394937a
migration: Fix iocs leaks during file and fd migration

The memory for the io channels is being leaked in three different ways
during file migration:

1) if the offset check fails we never drop the ioc reference;

2) we allocate an extra channel for no reason;

3) if multifd is enabled but channel creation fails when calling
   dup(), we leave the previous channels around along with the glib
   polling;

Fix all issues by restructuring the code to first allocate the
channels and only register the watches when all channels have been
created.

For multifd, the file and fd migrations can share code because both
are backed by a QIOChannelFile. For the non-multifd case, the fd needs
to be separate because it is backed by a QIOChannelSocket.

Fixes: 2dd7ee7a51 ("migration/multifd: Add incoming QIOChannelFile support")
Fixes: decdc76772 ("migration/multifd: Add mapped-ram support to fd: URI")
Reported-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
Link: https://lore.kernel.org/r/20240313212824.16974-2-farosas@suse.de
Signed-off-by: Peter Xu <peterx@redhat.com>
migration/fd.c
migration/file.c
migration/file.h