From 13b29f8e16d117461f42a4d06aa20973b03e82cb Mon Sep 17 00:00:00 2001 From: Cyrill Gorcunov Date: Tue, 25 Jun 2019 15:16:26 +0300 Subject: [PATCH] tty: Move tty layer shared init into tty_init_restore Instead of using tty_mutex value in atomic context (which is wrong, since it is not atomic) better move tty_mutex allocation into cr_restore_tasks where our all initializers live. Otherwise weird race effect might be observed. Reported-by: Deng Guangxing Signed-off-by: Cyrill Gorcunov --- criu/cr-restore.c | 3 +++ criu/include/tty.h | 1 + criu/tty.c | 15 +-------------- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/criu/cr-restore.c b/criu/cr-restore.c index 23be81140..bf85ab04b 100644 --- a/criu/cr-restore.c +++ b/criu/cr-restore.c @@ -2353,6 +2353,9 @@ int cr_restore_tasks(void) if (vdso_init_restore()) goto err; + if (tty_init_restore()) + goto err; + if (opts.cpu_cap & CPU_CAP_IMAGE) { if (cpu_validate_cpuinfo()) goto err; diff --git a/criu/include/tty.h b/criu/include/tty.h index 95ced8396..8419593e5 100644 --- a/criu/include/tty.h +++ b/criu/include/tty.h @@ -32,6 +32,7 @@ struct mount_info; extern int devpts_restore(struct mount_info *pm); extern int tty_prep_fds(void); +extern int tty_init_restore(void); extern int devpts_check_bindmount(struct mount_info *m); diff --git a/criu/tty.c b/criu/tty.c index e9a28897c..dee8d46bf 100644 --- a/criu/tty.c +++ b/criu/tty.c @@ -349,11 +349,8 @@ static mutex_t *tty_mutex; static bool tty_is_master(struct tty_info *info); -static int init_tty_mutex(void) +int tty_init_restore(void) { - if (tty_mutex) - return 0; - tty_mutex = shmalloc(sizeof(*tty_mutex)); if (!tty_mutex) { pr_err("Can't create ptmx index mutex\n"); @@ -600,9 +597,6 @@ static int __pty_open_ptmx_index(int index, int flags, memset(fds, 0xff, sizeof(fds)); - if (init_tty_mutex()) - return -1; - mutex_lock(tty_mutex); for (i = 0; i < ARRAY_SIZE(fds); i++) { @@ -1792,13 +1786,6 @@ static int tty_info_setup(struct tty_info *info) add_post_prepare_cb_once(&prep_tty_restore); - /* - * Call it explicitly. Post-callbacks will be called after - * namespaces preparation, while the latter needs this mutex. - */ - if (init_tty_mutex()) - return -1; - info->fdstore_id = -1; return file_desc_add(&info->d, info->tfe->id, &tty_desc_ops); }