We can hold rcu lock so that ve pointer is still valid and using css_tryget we can get reference on ve cgroup if it is not yet destroying. In case cgroup is destroying retry with cgroup_mutex.
https://jira.sw.ru/browse/PSBM-123766 Signed-off-by: Pavel Tikhomirov <[email protected]> --- kernel/ve/ve.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/kernel/ve/ve.c b/kernel/ve/ve.c index 8b7e2ad67530..f22e0d6b3b34 100644 --- a/kernel/ve/ve.c +++ b/kernel/ve/ve.c @@ -143,10 +143,31 @@ struct ve_struct *get_curr_ve(void) struct ve_struct *ve; /* - * Under cgroup_mutex both current tasks ve cgroup and ->task_ve - * pointer can't change. Corresponding cgroup_mutex around - * cgroup_attach_task() protects us from it. + * If first thread loads current->task_ve pointer, and if just after + * that current is moved by other thread from this ve cgroup to some + * other and this ve cgroup gets destroyed, ve pointer gets freed, so + * first thread can't use such ve pointer safely. + */ + + /* + * Fast path: Let's make it safe with rcu lock, though current can be + * moved to other ve cgroup and our ve cgroup can start destroying, ve + * pointer would be still valid. As it is freed in ve_destroy. And + * ve_destroy is called from rcu callback after task_ve had changed. */ + rcu_read_lock(); + ve = rcu_dereference(current->task_ve); + if (css_tryget(&ve->css)) { + rcu_read_unlock(); + return ve; + } + rcu_read_unlock(); + + /* + * Slow path: Under cgroup_mutex both current tasks ve cgroup and + * task_ve pointer can't change. Corresponding cgroup_mutex around + * cgroup_attach_task() protects us from it. + */ mutex_lock(&cgroup_mutex); ve = get_ve(current->task_ve); mutex_unlock(&cgroup_mutex); @@ -1121,7 +1142,7 @@ static void ve_attach(struct cgroup *cg, struct cgroup_taskset *tset) ve_try_set_task_start_time(ve, task); - task->task_ve = ve; + rcu_assign_pointer(task->task_ve, ve); } /* Adjust cpuid faulting */ -- 2.31.1 _______________________________________________ Devel mailing list [email protected] https://lists.openvz.org/mailman/listinfo/devel
