[Devel] [PATCH v3 1/3] ve: add get_curr_ve helper

Pavel Tikhomirov ptikhomirov at virtuozzo.com
Wed Aug 18 14:00:03 MSK 2021


This helper is a safe alternative to get_exec_env(), this helper
actually gets reference on current ve so if from other thread current
would be moved from this ve, at least this ve would not be freed under
us.

https://jira.sw.ru/browse/PSBM-123766

Signed-off-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>

---
v2: fix missprint in EXPORT_SYMBOL
v3: move helper to cgroup.c

---
 include/linux/cgroup.h |  1 +
 kernel/cgroup.c        | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index ac255e4c6d79..6ed84d9dfc17 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -671,6 +671,7 @@ int cgroup_task_count(const struct cgroup *cgrp);
 void cgroup_release_agent(struct work_struct *work);
 
 #ifdef CONFIG_VE
+struct ve_struct *get_curr_ve(void);
 int cgroup_mark_ve_roots(struct ve_struct *ve);
 void cgroup_unmark_ve_roots(struct ve_struct *ve);
 struct cgroup *cgroup_get_local_root(struct cgroup *cgrp);
diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index 9d360a080990..8f01cb96bed9 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -4645,6 +4645,27 @@ static struct cftype *get_cftype_by_name(const char *name)
 }
 
 #ifdef CONFIG_VE
+/*
+ * This helper is a safe alternative to get_exec_env(), this helper actually
+ * gets reference on current ve so if in other thread we would be moved from
+ * this ve, at least this ve would not be freed under us.
+ */
+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.
+	 */
+	mutex_lock(&cgroup_mutex);
+	ve = get_ve(current->task_ve);
+	mutex_unlock(&cgroup_mutex);
+
+	return ve;
+}
+
 int cgroup_mark_ve_roots(struct ve_struct *ve)
 {
 	struct cgroup *cgrp, *tmp;
-- 
2.31.1



More information about the Devel mailing list