[CRIU] [PATCH] pstree: Implement vpgid(), vsid() and vtid()
Andrei Vagin
avagin at virtuozzo.com
Thu May 4 13:36:56 PDT 2017
Applied, thanks!
On Wed, Apr 26, 2017 at 01:07:44PM +0300, Kirill Tkhai wrote:
> We already vpid() function. Make it #define instead of that,
> and this will allow to use it in assignments:
>
> vpid(item) = pid;
>
> Also, introduce vsid(), vpgid() and vtid() like it's shown below:
>
> #define vpid(item) (item->pid->ns[0].virt)
> #define vsid(item) (item->sid->ns[0].virt)
> #define vpgid(item) (item->pgid->ns[0].virt)
> #define vtid(item, i) (item->threads[i]->ns[0].virt)
>
> https://travis-ci.org/tkhai/criu/builds/225938195
>
> Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
> ---
> criu/cr-dump.c | 24 ++++++++----------
> criu/cr-restore.c | 10 ++++---
> criu/files-reg.c | 8 +++---
> criu/include/pstree.h | 8 +++---
> criu/pstree.c | 66 +++++++++++++++++++++++++------------------------
> criu/tty.c | 2 +
> 6 files changed, 58 insertions(+), 60 deletions(-)
>
> diff --git a/criu/cr-dump.c b/criu/cr-dump.c
> index 4562eeeaa..365f360f4 100644
> --- a/criu/cr-dump.c
> +++ b/criu/cr-dump.c
> @@ -1005,7 +1005,7 @@ static int dump_task_threads(struct parasite_ctl *parasite_ctl,
> for (i = 0; i < item->nr_threads; i++) {
> /* Leader is already dumped */
> if (item->pid->real == item->threads[i]->real) {
> - item->threads[i]->ns[0].virt = vpid(item);
> + vtid(item, i) = vpid(item);
> continue;
> }
> if (dump_task_thread(parasite_ctl, item, i))
> @@ -1065,7 +1065,7 @@ static int fill_zombies_pids(struct pstree_item *item)
> for (; i < nr; i++) {
> if (ch[i] < 0)
> continue;
> - child->pid->ns[0].virt = ch[i];
> + vpid(child) = ch[i];
> ch[i] = -1;
> break;
> }
> @@ -1098,7 +1098,7 @@ static int dump_zombies(void)
>
> if (vpid(item) < 0) {
> if (!pidns)
> - item->pid->ns[0].virt = item->pid->real;
> + vpid(item) = item->pid->real;
> else if (root_item == item) {
> pr_err("A root task is dead\n");
> goto err;
> @@ -1110,8 +1110,8 @@ static int dump_zombies(void)
> if (parse_pid_stat(vpid(item), &pps_buf) < 0)
> goto err;
>
> - item->sid->ns[0].virt = pps_buf.sid;
> - item->pgid->ns[0].virt = pps_buf.pgid;
> + vsid(item) = pps_buf.sid;
> + vpgid(item) = pps_buf.pgid;
>
> BUG_ON(!list_empty(&item->children));
> if (dump_one_zombie(item, &pps_buf) < 0)
> @@ -1181,7 +1181,7 @@ static int pre_dump_one_task(struct pstree_item *item)
> goto err_cure;
> }
>
> - item->pid->ns[0].virt = misc.pid;
> + vpid(item) = misc.pid;
>
> mdc.pre_dump = true;
> mdc.lazy = false;
> @@ -1311,16 +1311,14 @@ static int dump_one_task(struct pstree_item *item)
> goto err_cure_imgset;
> }
>
> - item->pid->ns[0].virt = misc.pid;
> - item->sid->ns[0].virt = misc.sid;
> - item->pgid->ns[0].virt = misc.pgid;
> + vpid(item) = misc.pid;
> + vsid(item) = misc.sid;
> + vpgid(item) = misc.pgid;
> pstree_insert_pid(item->pid, item->ids->pid_ns_id);
>
> - pr_info("sid=%d pgid=%d pid=%d\n",
> - item->sid->ns[0].virt, item->pgid->ns[0].virt, vpid(item));
> + pr_info("sid=%d pgid=%d pid=%d\n", vsid(item), vpgid(item), vpid(item));
>
> -
> - if (item->sid->ns[0].virt == 0) {
> + if (vsid(item) == 0) {
> pr_err("A session leader of %d(%d) is outside of its pid namespace\n",
> item->pid->real, vpid(item));
> goto err_cure;
> diff --git a/criu/cr-restore.c b/criu/cr-restore.c
> index 0c9f42bd8..23871a6d6 100644
> --- a/criu/cr-restore.c
> +++ b/criu/cr-restore.c
> @@ -625,7 +625,7 @@ static int open_cores(int pid, CoreEntry *leader_core)
> goto err;
>
> for (i = 0; i < current->nr_threads; i++) {
> - tpid = current->threads[i]->ns[0].virt;
> + tpid = vtid(current, i);
>
> if (tpid == pid)
> cores[i] = leader_core;
> @@ -1240,7 +1240,7 @@ static void restore_sid(void)
> */
>
> if (equal_pid(current->pid, current->sid)) {
> - pr_info("Restoring %d to %d sid\n", vpid(current), current->sid->ns[0].virt);
> + pr_info("Restoring %d to %d sid\n", vpid(current), vsid(current));
> sid = setsid();
> if (sid != last_level_pid(current->sid)) {
> pr_perror("Can't restore sid (%d)", sid);
> @@ -1297,7 +1297,7 @@ static void restore_pgid(void)
>
> pr_info("\twill call setpgid, mine pgid is %d\n", pgid);
> if (setpgid(0, my_pgid) != 0) {
> - pr_perror("Can't restore pgid (%d/%d->%d)", vpid(current), pgid, current->pgid->ns[0].virt);
> + pr_perror("Can't restore pgid (%d/%d->%d)", vpid(current), pgid, vpgid(current));
> exit(1);
> }
>
> @@ -2787,7 +2787,7 @@ static int prepare_signals(int pid, struct task_restore_args *ta, CoreEntry *lea
> for (i = 0; i < current->nr_threads; i++) {
> if (!current->core[i]->thread_core->signals_p)/*backward compatibility*/
> ret = open_signal_image(CR_FD_PSIGNAL,
> - current->threads[i]->ns[0].virt, &siginfo_priv_nr[i]);
> + vtid(current, i), &siginfo_priv_nr[i]);
> else
> ret = prepare_one_signal_queue(current->core[i]->thread_core->signals_p,
> &siginfo_priv_nr[i]);
> @@ -3248,7 +3248,7 @@ static int sigreturn_restore(pid_t pid, struct task_restore_args *task_args, uns
> struct rt_sigframe *sigframe;
> k_rtsigset_t *blkset = NULL;
>
> - thread_args[i].pid = current->threads[i]->ns[0].virt;
> + thread_args[i].pid = vtid(current, i);
> thread_args[i].siginfo_n = siginfo_priv_nr[i];
> thread_args[i].siginfo = task_args->siginfo;
> thread_args[i].siginfo += siginfo_n;
> diff --git a/criu/files-reg.c b/criu/files-reg.c
> index 7c81d182a..cd30bd0d8 100644
> --- a/criu/files-reg.c
> +++ b/criu/files-reg.c
> @@ -390,9 +390,9 @@ static int open_remap_dead_process(struct reg_file_info *rfi,
>
> init_pstree_helper(helper);
>
> - helper->sid->ns[0].virt = root_item->sid->ns[0].virt;
> - helper->pgid->ns[0].virt = root_item->pgid->ns[0].virt;
> - helper->pid->ns[0].virt = rfe->remap_id;
> + vsid(helper) = vsid(root_item);
> + vpgid(helper) = vpgid(root_item);
> + vpid(helper) = rfe->remap_id;
> helper->parent = root_item;
> helper->ids = root_item->ids;
> list_add_tail(&helper->sibling, &root_item->children);
> @@ -825,7 +825,7 @@ int dead_pid_conflict(void)
> */
> item = node->item;
> if (item->pid->real == item->threads[i]->real ||
> - item->threads[i]->ns[0].virt != pid)
> + vtid(item, i) != pid)
> continue;
> }
>
> diff --git a/criu/include/pstree.h b/criu/include/pstree.h
> index 438ef939a..4035c6bbb 100644
> --- a/criu/include/pstree.h
> +++ b/criu/include/pstree.h
> @@ -32,10 +32,10 @@ struct pstree_item {
> struct ns_id *user_ns;
> };
>
> -static inline pid_t vpid(const struct pstree_item *i)
> -{
> - return i->pid->ns[0].virt;
> -}
> +#define vpid(item) (item->pid->ns[0].virt)
> +#define vsid(item) (item->sid->ns[0].virt)
> +#define vpgid(item) (item->pgid->ns[0].virt)
> +#define vtid(item, i) (item->threads[i]->ns[0].virt)
>
> enum {
> FDS_EVENT_BIT = 0,
> diff --git a/criu/pstree.c b/criu/pstree.c
> index 39772e4be..b512e43b2 100644
> --- a/criu/pstree.c
> +++ b/criu/pstree.c
> @@ -240,7 +240,7 @@ struct pstree_item *__alloc_pstree_item(bool rst, int level)
> INIT_LIST_HEAD(&item->children);
> INIT_LIST_HEAD(&item->sibling);
>
> - item->pid->ns[0].virt = -1;
> + vpid(item) = -1;
> item->pid->real = -1;
> item->pid->state = TASK_UNDEF;
> item->born_sid = -1;
> @@ -326,8 +326,8 @@ int dump_pstree(struct pstree_item *root_item)
>
> e.pid = vpid(item);
> e.ppid = item->parent ? vpid(item->parent) : 0;
> - e.pgid = item->pgid->ns[0].virt;
> - e.sid = item->sid->ns[0].virt;
> + e.pgid = vpgid(item);
> + e.sid = vsid(item);
> e.n_threads = item->nr_threads;
>
> e.threads = xmalloc(sizeof(e.threads[0]) * e.n_threads);
> @@ -335,7 +335,7 @@ int dump_pstree(struct pstree_item *root_item)
> goto err;
>
> for (i = 0; i < item->nr_threads; i++)
> - e.threads[i] = item->threads[i]->ns[0].virt;
> + e.threads[i] = vtid(item, i);
>
> ret = pb_write_one(img, &e, PB_PSTREE);
> xfree(e.threads);
> @@ -384,17 +384,17 @@ static int prepare_pstree_for_shell_job(void)
> * Not that clever solution but at least it works.
> */
>
> - old_sid = root_item->sid->ns[0].virt;
> - old_gid = root_item->pgid->ns[0].virt;
> + old_sid = vsid(root_item);
> + old_gid = vpgid(root_item);
>
> pr_info("Migrating process tree (GID %d->%d SID %d->%d)\n",
> old_gid, current_gid, old_sid, current_sid);
>
> for_each_pstree_item(pi) {
> - if (pi->pgid->ns[0].virt == old_gid)
> - pi->pgid->ns[0].virt = current_gid;
> - if (pi->sid->ns[0].virt == old_sid)
> - pi->sid->ns[0].virt = current_sid;
> + if (vpgid(pi) == old_gid)
> + vpgid(pi) = current_gid;
> + if (vsid(pi) == old_sid)
> + vsid(pi) = current_sid;
> }
>
> if (lookup_create_item(¤t_sid, 1, root_item->ids->pid_ns_id) == NULL)
> @@ -657,13 +657,13 @@ static int read_pstree_image(pid_t *pid_max)
> if (lookup_create_item((pid_t *)&e->sid, 1, ids->pid_ns_id) == NULL)
> break;
>
> - pi->pid->ns[0].virt = e->pid;
> + vpid(pi) = e->pid;
> if (e->pid > *pid_max)
> *pid_max = e->pid;
> - pi->pgid->ns[0].virt = e->pgid;
> + vpgid(pi) = e->pgid;
> if (e->pgid > *pid_max)
> *pid_max = e->pgid;
> - pi->sid->ns[0].virt = e->sid;
> + vsid(pi) = e->sid;
> if (e->sid > *pid_max)
> *pid_max = e->sid;
> pi->pid->state = TASK_ALIVE;
> @@ -693,7 +693,7 @@ static int read_pstree_image(pid_t *pid_max)
> goto err;
> pi->threads[i]->real = -1;
> pi->threads[i]->level = pi->pid->level;
> - pi->threads[i]->ns[0].virt = e->threads[i];
> + vtid(pi, i) = e->threads[i];
> pi->threads[i]->state = TASK_THREAD;
> pi->threads[i]->item = NULL;
> if (i == 0)
> @@ -771,7 +771,7 @@ static int prepare_pstree_ids(void)
> if (equal_pid(item->sid, root_item->sid) || equal_pid(item->sid, item->pid))
> continue;
>
> - leader = pstree_item_by_virt(item->sid->ns[0].virt);
> + leader = pstree_item_by_virt(vsid(item));
> BUG_ON(leader == NULL);
> if (leader->pid->state != TASK_UNDEF) {
> pid_t pid;
> @@ -783,10 +783,10 @@ static int prepare_pstree_ids(void)
> if (helper == NULL)
> return -1;
>
> - pr_info("Session leader %d\n", item->sid->ns[0].virt);
> + pr_info("Session leader %d\n", vsid(item));
>
> - helper->sid->ns[0].virt = item->sid->ns[0].virt;
> - helper->pgid->ns[0].virt = leader->pgid->ns[0].virt;
> + vsid(helper) = vsid(item);
> + vpgid(helper) = vpgid(leader);
> helper->ids = leader->ids;
> helper->parent = leader;
> list_add(&helper->sibling, &leader->children);
> @@ -795,8 +795,8 @@ static int prepare_pstree_ids(void)
> vpid(helper), vpid(leader));
> } else {
> helper = leader;
> - helper->sid->ns[0].virt = item->sid->ns[0].virt;
> - helper->pgid->ns[0].virt = item->sid->ns[0].virt;
> + vsid(helper) = vsid(item);
> + vpgid(helper) = vsid(item);
> helper->parent = root_item;
> helper->ids = root_item->ids;
> list_add_tail(&helper->sibling, &helpers);
> @@ -804,7 +804,7 @@ static int prepare_pstree_ids(void)
> init_pstree_helper(helper);
>
> pr_info("Add a helper %d for restoring SID %d\n",
> - vpid(helper), helper->sid->ns[0].virt);
> + vpid(helper), vsid(helper));
>
> child = list_entry(item->sibling.prev, struct pstree_item, sibling);
> item = child;
> @@ -843,19 +843,19 @@ static int prepare_pstree_ids(void)
> /* the task could fork a child before and after setsid() */
> parent = item->parent;
> while (parent && !equal_pid(parent->pid, item->sid)) {
> - if (parent->born_sid != -1 && parent->born_sid != item->sid->ns[0].virt) {
> + if (parent->born_sid != -1 && parent->born_sid != vsid(item)) {
> pr_err("Can't figure out which sid (%d or %d)"
> "the process %d was born with\n",
> - parent->born_sid, item->sid->ns[0].virt, vpid(parent));
> + parent->born_sid, vsid(item), vpid(parent));
> return -1;
> }
> - parent->born_sid = item->sid->ns[0].virt;
> - pr_info("%d was born with sid %d\n", vpid(parent), item->sid->ns[0].virt);
> + parent->born_sid = vsid(item);
> + pr_info("%d was born with sid %d\n", vpid(parent), vsid(item));
> parent = parent->parent;
> }
>
> if (parent == NULL) {
> - pr_err("Can't find a session leader for %d\n", item->sid->ns[0].virt);
> + pr_err("Can't find a session leader for %d\n", vsid(item));
> return -1;
> }
>
> @@ -873,7 +873,7 @@ static int prepare_pstree_ids(void)
> if (!item->pgid || equal_pid(item->pid, item->pgid))
> continue;
>
> - pid = pstree_pid_by_virt(item->pgid->ns[0].virt);
> + pid = pstree_pid_by_virt(vpgid(item));
> if (pid->state != TASK_UNDEF) {
> BUG_ON(pid->state == TASK_THREAD);
> rsti(item)->pgrp_leader = pid->item;
> @@ -885,22 +885,22 @@ static int prepare_pstree_ids(void)
> * means we're inheriting group from the current
> * task so we need to escape creating a helper here.
> */
> - if (current_pgid == item->pgid->ns[0].virt)
> + if (current_pgid == vpgid(item))
> continue;
>
> helper = pid->item;
> init_pstree_helper(helper);
>
> - helper->sid->ns[0].virt = item->sid->ns[0].virt;
> - helper->pgid->ns[0].virt = item->pgid->ns[0].virt;
> - helper->pid->ns[0].virt = item->pgid->ns[0].virt;
> + vsid(helper) = vsid(item);
> + vpgid(helper) = vpgid(item);
> + vpid(helper) = vpgid(item);
> helper->parent = item;
> helper->ids = item->ids;
> list_add(&helper->sibling, &item->children);
> rsti(item)->pgrp_leader = helper;
>
> pr_info("Add a helper %d for restoring PGID %d\n",
> - vpid(helper), helper->pgid->ns[0].virt);
> + vpid(helper), vpgid(helper));
> }
>
> return 0;
> @@ -1107,7 +1107,7 @@ int prepare_dummy_pstree(void)
>
> bool restore_before_setsid(struct pstree_item *child)
> {
> - int csid = child->born_sid == -1 ? child->sid->ns[0].virt : child->born_sid;
> + int csid = child->born_sid == -1 ? vsid(child) : child->born_sid;
>
> if (child->parent->born_sid == csid)
> return true;
> diff --git a/criu/tty.c b/criu/tty.c
> index ade559ce0..d425c2e95 100644
> --- a/criu/tty.c
> +++ b/criu/tty.c
> @@ -1208,7 +1208,7 @@ static struct pstree_item *find_first_sid(int sid)
> struct pstree_item *item;
>
> for_each_pstree_item(item) {
> - if (item->sid->ns[0].virt == sid)
> + if (vsid(item) == sid)
> return item;
> }
>
>
More information about the CRIU
mailing list