From: "Michal Koutný" <mkoutny@suse.com>
To: cgroups@vger.kernel.org, linux-doc@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Cc: Tejun Heo <tj@kernel.org>, Zefan Li <lizefan.x@bytedance.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Jonathan Corbet <corbet@lwn.net>, Shuah Khan <shuah@kernel.org>
Subject: [RFC PATCH v3 7/9] cgroup/pids: Replace uncharge/charge pair with a single function
Date: Fri, 5 Apr 2024 19:05:46 +0200 [thread overview]
Message-ID: <20240405170548.15234-8-mkoutny@suse.com> (raw)
In-Reply-To: <20240405170548.15234-1-mkoutny@suse.com>
No functional change intended. This rework reduces modifications of pids
counters only to a minimal subtree of uncharged/charged cgroups.
Signed-off-by: Michal Koutný <mkoutny@suse.com>
---
kernel/cgroup/pids.c | 80 ++++++++++++++++++++++++++------------------
1 file changed, 47 insertions(+), 33 deletions(-)
diff --git a/kernel/cgroup/pids.c b/kernel/cgroup/pids.c
index f5f81274658e..9df8a209a6e2 100644
--- a/kernel/cgroup/pids.c
+++ b/kernel/cgroup/pids.c
@@ -133,41 +133,23 @@ static void pids_uncharge(struct pids_cgroup *pids, int num)
pids_cancel(p, num);
}
-/**
- * pids_charge - hierarchically charge the pid count
- * @pids: the pid cgroup state
- * @num: the number of pids to charge
- *
- * This function does *not* follow the pid limit set. It cannot fail and the new
- * pid count may exceed the limit. This is only used for reverting failed
- * attaches, where there is no other way out than violating the limit.
- */
-static void pids_charge(struct pids_cgroup *pids, int num)
-{
- struct pids_cgroup *p;
-
- for (p = pids; parent_pids(p); p = parent_pids(p)) {
- int64_t new = atomic64_add_return(num, &p->counter);
-
- pids_update_watermark(p, new);
- }
-}
-
/**
* pids_try_charge - hierarchically try to charge the pid count
* @pids: the pid cgroup state
* @num: the number of pids to charge
+ * @root: charge only under this root (NULL is global root)
* @fail: storage of pid cgroup causing the fail
*
* This function follows the set limit. It will fail if the charge would cause
- * the new value to exceed the hierarchical limit. Returns 0 if the charge
- * succeeded, otherwise -EAGAIN.
+ * the new value to exceed the hierarchical limit and fail is set. Returns 0 if
+ * no limit was hit, otherwise -EAGAIN.
*/
-static int pids_try_charge(struct pids_cgroup *pids, int num, struct pids_cgroup **fail)
+static int pids_try_charge(struct pids_cgroup *pids, int num, struct pids_cgroup *root, struct pids_cgroup **fail)
{
struct pids_cgroup *p, *q;
+ int ret = 0;
- for (p = pids; parent_pids(p); p = parent_pids(p)) {
+ for (p = pids; parent_pids(p) && p != root; p = parent_pids(p)) {
int64_t new = atomic64_add_return(num, &p->counter);
int64_t limit = atomic64_read(&p->limit);
@@ -177,8 +159,11 @@ static int pids_try_charge(struct pids_cgroup *pids, int num, struct pids_cgroup
* fail.
*/
if (new > limit) {
- *fail = p;
- goto revert;
+ ret = -EAGAIN;
+ if (fail) {
+ *fail = p;
+ goto revert;
+ }
}
/*
* Not technically accurate if we go over limit somewhere up
@@ -187,14 +172,45 @@ static int pids_try_charge(struct pids_cgroup *pids, int num, struct pids_cgroup
pids_update_watermark(p, new);
}
- return 0;
+ return ret;
revert:
for (q = pids; q != p; q = parent_pids(q))
pids_cancel(q, num);
pids_cancel(p, num);
- return -EAGAIN;
+ return ret;
+}
+
+/**
+ * pids_tranfer_charge - charge/uncharge in subtree betwee src and dst
+ * @src: pid cgroup state to uncharge
+ * @dst: pid cgroup state to charge
+ * @num: the number of pids to transfer
+ *
+ * The function updates charged pids in subtree whose root is the closest
+ * common ancestor of @src and @dst. This root and its ancestors are not
+ * modified (their limits are not enacted).
+ *
+ * Returns 0 if no limit was hit, -EAGAIN if a limit on path [@dst, @comm) was
+ * hit (charges are transferred despite the limit).
+ */
+static int pids_tranfer_charge(struct pids_cgroup *src, struct pids_cgroup *dst, int num)
+{
+ struct pids_cgroup *p, *comm = src;
+ int ret;
+
+ /* for stable cgroup tree */
+ lockdep_assert_held(&cgroup_mutex);
+
+ while (!cgroup_is_descendant(dst->css.cgroup, comm->css.cgroup))
+ comm = parent_pids(comm);
+
+ ret = pids_try_charge(dst, num, comm, NULL);
+
+ for (p = src; p != comm; p = parent_pids(p))
+ pids_cancel(p, num);
+ return ret;
}
static int pids_can_attach(struct cgroup_taskset *tset)
@@ -215,8 +231,7 @@ static int pids_can_attach(struct cgroup_taskset *tset)
old_css = task_css(task, pids_cgrp_id);
old_pids = css_pids(old_css);
- pids_charge(pids, 1);
- pids_uncharge(old_pids, 1);
+ (void) pids_tranfer_charge(old_pids, pids, 1);
}
return 0;
@@ -235,8 +250,7 @@ static void pids_cancel_attach(struct cgroup_taskset *tset)
old_css = task_css(task, pids_cgrp_id);
old_pids = css_pids(old_css);
- pids_charge(old_pids, 1);
- pids_uncharge(pids, 1);
+ (void) pids_tranfer_charge(pids, old_pids, 1);
}
}
@@ -287,7 +301,7 @@ static int pids_can_fork(struct task_struct *task, struct css_set *cset)
else
css = task_css_check(current, pids_cgrp_id, true);
pids = css_pids(css);
- err = pids_try_charge(pids, 1, &pids_over_limit);
+ err = pids_try_charge(pids, 1, NULL, &pids_over_limit);
if (err)
pids_event(pids, pids_over_limit);
--
2.44.0
next prev parent reply other threads:[~2024-04-05 17:05 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-05 17:05 [RFC PATCH v3 0/9] pids controller events rework and migration charging Michal Koutný
2024-04-05 17:05 ` [RFC PATCH v3 1/9] cgroup/pids: Remove superfluous zeroing Michal Koutný
2024-04-05 17:05 ` [RFC PATCH v3 2/9] cgroup/pids: Separate semantics of pids.events related to pids.max Michal Koutný
2024-04-08 17:55 ` Tejun Heo
2024-04-09 16:02 ` Johannes Weiner
2024-04-12 14:23 ` Michal Koutný
2024-04-12 17:04 ` Tejun Heo
2024-04-05 17:05 ` [RFC PATCH v3 3/9] cgroup/pids: Make event counters hierarchical Michal Koutný
2024-04-05 17:05 ` [RFC PATCH v3 4/9] cgroup/pids: Add pids.events.local Michal Koutný
2024-04-05 17:05 ` [RFC PATCH v3 5/9] selftests: cgroup: Lexicographic order in Makefile Michal Koutný
2024-04-05 17:05 ` [RFC PATCH v3 6/9] selftests: cgroup: Add basic tests for pids controller Michal Koutný
2024-04-06 21:37 ` Muhammad Usama Anjum
2024-04-08 11:29 ` Michal Koutný
2024-04-08 11:53 ` Muhammad Usama Anjum
2024-04-08 12:01 ` Michal Koutný
2024-04-08 12:04 ` Muhammad Usama Anjum
2024-04-09 0:12 ` Waiman Long
2024-04-09 13:00 ` Muhammad Usama Anjum
2024-04-05 17:05 ` Michal Koutný [this message]
2024-04-05 17:05 ` [RFC PATCH v3 8/9] cgroup/pids: Enforce pids.max on task migrations Michal Koutný
2024-04-05 17:05 ` [RFC PATCH v3 9/9] selftests: cgroup: Add tests pids controller Michal Koutný
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240405170548.15234-8-mkoutny@suse.com \
--to=mkoutny@suse.com \
--cc=cgroups@vger.kernel.org \
--cc=corbet@lwn.net \
--cc=hannes@cmpxchg.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=lizefan.x@bytedance.com \
--cc=shuah@kernel.org \
--cc=tj@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).