<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p><br>
    </p>
    <div class="moz-cite-prefix">On 27.07.2020 13:18, Kirill Tkhai
      wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:229b8cbd-905d-3aaa-5220-ec4839546c0f@virtuozzo.com">
      <pre class="moz-quote-pre" wrap="">On 27.07.2020 12:10, Valeriy Vdovin wrote:
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
</pre>
        <blockquote type="cite">
          <pre class="moz-quote-pre" wrap="">From: Kirill Tkhai <a class="moz-txt-link-rfc2396E" href="mailto:ktkhai@virtuozzo.com">&lt;ktkhai@virtuozzo.com&gt;</a>
Sent: Tuesday, July 21, 2020 4:52 PM
To: Valeriy Vdovin <a class="moz-txt-link-rfc2396E" href="mailto:Valeriy.Vdovin@virtuozzo.com">&lt;Valeriy.Vdovin@virtuozzo.com&gt;</a>; <a class="moz-txt-link-abbreviated" href="mailto:devel@openvz.org">devel@openvz.org</a> &gt; <a class="moz-txt-link-rfc2396E" href="mailto:devel@openvz.org">&lt;devel@openvz.org&gt;</a>
Cc: Valeriy Vdovin <a class="moz-txt-link-rfc2396E" href="mailto:valeriy.vdovin@virtuozz.com">&lt;valeriy.vdovin@virtuozz.com&gt;</a>
Subject: Re: [PATCH RHEL7 v20 06/14] ve/cgroup: unmark ve-root cgroups at container stop

On 25.06.2020 17:29, Valeriy Vdovin wrote:
</pre>
          <blockquote type="cite">
            <pre class="moz-quote-pre" wrap="">Signed-off-by: Valeriy Vdovin <a class="moz-txt-link-rfc2396E" href="mailto:valeriy.vdovin@virtuozzo.com">&lt;valeriy.vdovin@virtuozzo.com&gt;</a>
Reviewed-by: Kirill Tkhai <a class="moz-txt-link-rfc2396E" href="mailto:ktkhai@virtuozzo.com">&lt;ktkhai@virtuozzo.com&gt;</a>
---
&nbsp;include/linux/cgroup.h | &nbsp;1 +
&nbsp;kernel/cgroup.c &nbsp; &nbsp; &nbsp; &nbsp;| 38 ++++++++++++++++++++++++++++++++++++++
&nbsp;kernel/ve/ve.c &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp;2 ++
&nbsp;3 files changed, 41 insertions(+)

diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index ac60aaed..6e2c206 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -671,6 +671,7 @@ int cgroup_task_count(const struct cgroup *cgrp);

&nbsp;#ifdef CONFIG_VE
&nbsp;void cgroup_mark_ve_roots(struct ve_struct *ve);
+void cgroup_unmark_ve_roots(struct ve_struct *ve);
&nbsp;#endif

&nbsp;/*
diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index ce576c5..6e3871a 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -637,6 +637,31 @@ static struct css_set *find_css_set(
&nbsp;}

&nbsp;/*
+ * Walk each cgroup link of a given css_set and find a cgroup that
+ * is the child of cgroupfs_root in argument.
+ */
+static struct cgroup *css_cgroup_from_root(struct css_set *css_set,
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; struct cgroupfs_root *root)
+{
+ &nbsp; &nbsp; struct cgroup *res = NULL;
+ &nbsp; &nbsp; struct cg_cgroup_link *link;
+
+ &nbsp; &nbsp; BUG_ON(!mutex_is_locked(&amp;cgroup_mutex));
+ &nbsp; &nbsp; read_lock(&amp;css_set_lock);
+
+ &nbsp; &nbsp; list_for_each_entry(link, &amp;css_set-&gt;cg_links, cg_link_list) {
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; struct cgroup *c = link-&gt;cgrp;
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (c-&gt;root == root) {
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res = c;
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
+ &nbsp; &nbsp; }
+ &nbsp; &nbsp; read_unlock(&amp;css_set_lock);
+ &nbsp; &nbsp; BUG_ON(!res);
+ &nbsp; &nbsp; return res;
+}
+
+/*
&nbsp; * Return the cgroup for &quot;task&quot; from the given hierarchy. Must be
&nbsp; * called with cgroup_mutex held.
&nbsp; */
@@ -4329,6 +4354,19 @@ void cgroup_mark_ve_roots(struct ve_struct *ve)
&nbsp; &nbsp; &nbsp; &nbsp;mutex_unlock(&amp;cgroup_mutex);
&nbsp;}

+void cgroup_unmark_ve_roots(struct ve_struct *ve)
+{
+ &nbsp; &nbsp; struct cgroup *cgrp;
+ &nbsp; &nbsp; struct cgroupfs_root *root;
+
+ &nbsp; &nbsp; mutex_lock(&amp;cgroup_mutex);
+ &nbsp; &nbsp; for_each_active_root(root) {
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cgrp = css_cgroup_from_root(ve-&gt;root_css_set, root);
+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clear_bit(CGRP_VE_ROOT, &amp;cgrp-&gt;flags);
+ &nbsp; &nbsp; } &nbsp;
+ &nbsp; &nbsp; mutex_unlock(&amp;cgroup_mutex);
+}
+
&nbsp;struct cgroup *cgroup_get_ve_root(struct cgroup *cgrp)
&nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp;struct cgroup *ve_root = NULL;
diff --git a/kernel/ve/ve.c b/kernel/ve/ve.c
index 73cfee6..711050c 100644
--- a/kernel/ve/ve.c
+++ b/kernel/ve/ve.c
@@ -623,6 +623,8 @@ void ve_exit_ns(struct pid_namespace *pid_ns)
&nbsp; &nbsp; &nbsp; &nbsp;if (!ve-&gt;ve_ns || ve-&gt;ve_ns-&gt;pid_ns != pid_ns)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;
&nbsp;
+ &nbsp; &nbsp; cgroup_unmark_ve_roots(ve);
</pre>
          </blockquote>
          <pre class="moz-quote-pre" wrap="">
Is there a problem that ve workqueue works will run after we unmark roots?
Maybe we should call this cgroup_unmark_ve_roots() after ve_workqueue_stop()?
</pre>
        </blockquote>
        <pre class="moz-quote-pre" wrap="">
When a cgroup gets empty it's decided to which workqueue it should be put to await for
release. Thus when we unmark ve root, we prevent any new empty cgroups from entering this
workqueue. After that we are safe to stop the workqueue by waiting for all the current jobs to
complete.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
But is it OK that workqueue is executing its works after root is cleared? Won't there some
security leak? If it's safe, why?
</pre>
    </blockquote>
    <p>There are more than one reasons. <br>
    </p>
    <p>In this release_agent logic still uses ve0 and
      call_usermodhelper, so exactly here it affects nothing. Later when
      it actually affets something in patch &quot;ve/cgroup: Implemented
      logic that uses 'cgroup-&gt;ve_owner' to run release_agent
      notifications.&quot; we are doing the check for this special case</p>
    <pre>if (rcu_access_pointer(root_cgrp-&gt;ve_owner) != ve) {
&nbsp; rcu_read_unlock();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp; goto continue_free;
}</pre>
    <p>per-ve release_agent workqueue always knows it's &quot;ve&quot; and is able
      to check that virtual root is sane in this way.<br>
    </p>
    There is one more reason, which is less direct. At the time when
    unmark_ve_roots is called ve-&gt;init_task already has the flag
    PF_EXITING. This means that it would not be possible to launch
    usermode task even if the above check was not present, because
    per-ve workqueue will try to run it from ve and ve-&gt;init_task
    &amp; PF_EXITING will result to true<br>
    <p><br>
    </p>
    <blockquote type="cite" cite="mid:229b8cbd-905d-3aaa-5220-ec4839546c0f@virtuozzo.com">
      <pre class="moz-quote-pre" wrap="">
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">
</pre>
        <blockquote type="cite">
          <blockquote type="cite">
            <pre class="moz-quote-pre" wrap="">+
&nbsp; &nbsp; &nbsp; &nbsp;ve_workqueue_stop(ve);
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;/* &nbsp;

</pre>
          </blockquote>
        </blockquote>
        <pre class="moz-quote-pre" wrap="">
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
</pre>
    </blockquote>
  </body>
</html>