From: Daniel Jordan <daniel.m.jordan@oracle.com>
To: hannes@cmpxchg.org, jiangshanlai@gmail.com, lizefan@huawei.com,
tj@kernel.org
Cc: bsd@redhat.com, dan.j.williams@intel.com,
daniel.m.jordan@oracle.com, dave.hansen@intel.com,
juri.lelli@redhat.com, mhocko@kernel.org, peterz@infradead.org,
steven.sistare@oracle.com, tglx@linutronix.de,
tom.hromatka@oracle.com, vdavydov.dev@gmail.com,
cgroups@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org
Subject: [RFC v2 3/5] workqueue, memcontrol: make memcg throttle workqueue workers
Date: Wed, 5 Jun 2019 09:36:48 -0400 [thread overview]
Message-ID: <20190605133650.28545-4-daniel.m.jordan@oracle.com> (raw)
In-Reply-To: <20190605133650.28545-1-daniel.m.jordan@oracle.com>
Attaching a worker to a css_set isn't enough for all controllers to
throttle it. In particular, the memory controller currently bypasses
accounting for kernel threads.
Support memcg accounting for cgroup-aware workqueue workers so that
they're appropriately throttled.
Another, probably better way to do this is to have kernel threads, or
even specifically cgroup-aware workqueue workers, call
memalloc_use_memcg and memalloc_unuse_memcg during cgroup migration
(memcg attach callback maybe).
Signed-off-by: Daniel Jordan <daniel.m.jordan@oracle.com>
---
kernel/workqueue.c | 26 ++++++++++++++++++++++++++
kernel/workqueue_internal.h | 5 +++++
mm/memcontrol.c | 26 ++++++++++++++++++++++++--
3 files changed, 55 insertions(+), 2 deletions(-)
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 89b90899bc09..c8cc69e296c0 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -50,6 +50,8 @@
#include <linux/sched/isolation.h>
#include <linux/nmi.h>
#include <linux/cgroup.h>
+#include <linux/memcontrol.h>
+#include <linux/sched/mm.h>
#include "workqueue_internal.h"
@@ -1829,6 +1831,28 @@ static inline bool worker_in_child_cgroup(struct worker *worker)
return (worker->flags & WORKER_CGROUP) && cgroup_parent(worker->cgroup);
}
+/* XXX Put this in the memory controller's attach callback. */
+#ifdef CONFIG_MEMCG
+static void worker_unuse_memcg(struct worker *worker)
+{
+ if (worker->task->active_memcg) {
+ struct mem_cgroup *memcg = worker->task->active_memcg;
+
+ memalloc_unuse_memcg();
+ css_put(&memcg->css);
+ }
+}
+
+static void worker_use_memcg(struct worker *worker)
+{
+ struct mem_cgroup *memcg;
+
+ worker_unuse_memcg(worker);
+ memcg = mem_cgroup_from_css(task_get_css(worker->task, memory_cgrp_id));
+ memalloc_use_memcg(memcg);
+}
+#endif /* CONFIG_MEMCG */
+
static void attach_worker_to_dfl_root(struct worker *worker)
{
int ret;
@@ -1841,6 +1865,7 @@ static void attach_worker_to_dfl_root(struct worker *worker)
rcu_read_lock();
worker->cgroup = task_dfl_cgroup(worker->task);
rcu_read_unlock();
+ worker_unuse_memcg(worker);
} else {
/*
* TODO Modify the cgroup migration path to guarantee that a
@@ -1880,6 +1905,7 @@ static void attach_worker_to_cgroup(struct worker *worker,
if (cgroup_attach_kthread(cgroup) == 0) {
worker->cgroup = cgroup;
+ worker_use_memcg(worker);
} else {
/*
* Attach failed, so attach to the default root so the
diff --git a/kernel/workqueue_internal.h b/kernel/workqueue_internal.h
index 3ad5861258ca..f254b93edc2c 100644
--- a/kernel/workqueue_internal.h
+++ b/kernel/workqueue_internal.h
@@ -79,6 +79,11 @@ work_func_t wq_worker_last_func(struct task_struct *task);
#ifdef CONFIG_CGROUPS
+#ifndef CONFIG_MEMCG
+static inline void worker_use_memcg(struct worker *worker) {}
+static inline void worker_unuse_memcg(struct worker *worker) {}
+#endif /* CONFIG_MEMCG */
+
/*
* A barrier work running in a cgroup-aware worker pool needs to specify a
* cgroup. For simplicity, WQ_BARRIER_CGROUP makes the worker stay in its
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 81a0d3914ec9..1a80931b124a 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2513,9 +2513,31 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
static inline bool memcg_kmem_bypass(void)
{
- if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+ if (in_interrupt())
return true;
- return false;
+
+ if (unlikely(current->flags & PF_WQ_WORKER)) {
+ struct cgroup *parent;
+
+ /*
+ * memcg should throttle cgroup-aware workers. Infer the
+ * worker is cgroup-aware by its presence in a non-root cgroup.
+ *
+ * This test won't detect a cgroup-aware worker attached to the
+ * default root, but in that case memcg doesn't need to
+ * throttle it anyway.
+ *
+ * XXX One alternative to this awkward block is adding a
+ * cgroup-aware-worker bit to task_struct.
+ */
+ rcu_read_lock();
+ parent = cgroup_parent(task_dfl_cgroup(current));
+ rcu_read_unlock();
+
+ return !parent;
+ }
+
+ return !current->mm || (current->flags & PF_KTHREAD);
}
/**
--
2.21.0
next prev parent reply other threads:[~2019-06-05 13:38 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-05 13:36 [RFC v2 0/5] cgroup-aware unbound workqueues Daniel Jordan
2019-06-05 13:36 ` [RFC v2 1/5] cgroup: add cgroup v2 interfaces to migrate kernel threads Daniel Jordan
2019-06-05 13:36 ` [RFC v2 2/5] workqueue, cgroup: add cgroup-aware workqueues Daniel Jordan
2019-06-05 13:36 ` Daniel Jordan [this message]
2019-06-05 13:36 ` [RFC v2 4/5] workqueue, cgroup: add test module Daniel Jordan
2019-06-05 13:36 ` [RFC v2 5/5] ktask, cgroup: attach helper threads to the master thread's cgroup Daniel Jordan
2019-06-05 13:53 ` [RFC v2 0/5] cgroup-aware unbound workqueues Tejun Heo
2019-06-05 15:32 ` Daniel Jordan
2019-06-11 19:55 ` Tejun Heo
2019-06-12 22:29 ` Daniel Jordan
2019-06-06 6:15 ` Mike Rapoport
2019-06-11 19:52 ` Tejun Heo
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=20190605133650.28545-4-daniel.m.jordan@oracle.com \
--to=daniel.m.jordan@oracle.com \
--cc=bsd@redhat.com \
--cc=cgroups@vger.kernel.org \
--cc=dan.j.williams@intel.com \
--cc=dave.hansen@intel.com \
--cc=hannes@cmpxchg.org \
--cc=jiangshanlai@gmail.com \
--cc=juri.lelli@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lizefan@huawei.com \
--cc=mhocko@kernel.org \
--cc=peterz@infradead.org \
--cc=steven.sistare@oracle.com \
--cc=tglx@linutronix.de \
--cc=tj@kernel.org \
--cc=tom.hromatka@oracle.com \
--cc=vdavydov.dev@gmail.com \
/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).