]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
cgroup: pids: use atomic64_t for pids->limit
authorAleksa Sarai <cyphar@cyphar.com>
Wed, 16 Oct 2019 15:50:01 +0000 (02:50 +1100)
committerMarcelo Henrique Cerri <marcelo.cerri@canonical.com>
Fri, 17 Jan 2020 17:22:41 +0000 (14:22 -0300)
BugLink: https://bugs.launchpad.net/bugs/1857158
commit a713af394cf382a30dd28a1015cbe572f1b9ca75 upstream.

Because pids->limit can be changed concurrently (but we don't want to
take a lock because it would be needlessly expensive), use atomic64_ts
instead.

Fixes: commit 49b786ea146f ("cgroup: implement the PIDs subsystem")
Cc: stable@vger.kernel.org # v4.3+
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
Signed-off-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
kernel/cgroup/pids.c

index c9960baaa14f23f7b85a47b8f6f067da083c21cc..940d2e8db776ed5341f195c34fef977e726d336e 100644 (file)
@@ -48,7 +48,7 @@ struct pids_cgroup {
         * %PIDS_MAX = (%PID_MAX_LIMIT + 1).
         */
        atomic64_t                      counter;
-       int64_t                         limit;
+       atomic64_t                      limit;
 
        /* Handle for "pids.events" */
        struct cgroup_file              events_file;
@@ -76,8 +76,8 @@ pids_css_alloc(struct cgroup_subsys_state *parent)
        if (!pids)
                return ERR_PTR(-ENOMEM);
 
-       pids->limit = PIDS_MAX;
        atomic64_set(&pids->counter, 0);
+       atomic64_set(&pids->limit, PIDS_MAX);
        atomic64_set(&pids->events_limit, 0);
        return &pids->css;
 }
@@ -149,13 +149,14 @@ static int pids_try_charge(struct pids_cgroup *pids, int num)
 
        for (p = pids; parent_pids(p); p = parent_pids(p)) {
                int64_t new = atomic64_add_return(num, &p->counter);
+               int64_t limit = atomic64_read(&p->limit);
 
                /*
                 * Since new is capped to the maximum number of pid_t, if
                 * p->limit is %PIDS_MAX then we know that this test will never
                 * fail.
                 */
-               if (new > p->limit)
+               if (new > limit)
                        goto revert;
        }
 
@@ -280,7 +281,7 @@ set_limit:
         * Limit updates don't need to be mutex'd, since it isn't
         * critical that any racing fork()s follow the new limit.
         */
-       pids->limit = limit;
+       atomic64_set(&pids->limit, limit);
        return nbytes;
 }
 
@@ -288,7 +289,7 @@ static int pids_max_show(struct seq_file *sf, void *v)
 {
        struct cgroup_subsys_state *css = seq_css(sf);
        struct pids_cgroup *pids = css_pids(css);
-       int64_t limit = pids->limit;
+       int64_t limit = atomic64_read(&pids->limit);
 
        if (limit >= PIDS_MAX)
                seq_printf(sf, "%s\n", PIDS_MAX_STR);