Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1732579 > unrolled thread
| Started by | Shaohua Li <shli@kernel.org> |
|---|---|
| First post | 2017-09-14 23:10 +0200 |
| Last post | 2017-09-25 21:10 +0200 |
| Articles | 7 — 3 participants |
Back to article view | Back to linux.kernel
[PATCH V3 0/4] block: make loop block device cgroup aware Shaohua Li <shli@kernel.org> - 2017-09-14 23:10 +0200
[PATCH V3 2/4] blkcg: delete unused APIs Shaohua Li <shli@kernel.org> - 2017-09-14 23:10 +0200
[PATCH V3 1/4] kthread: add a mechanism to store cgroup info Shaohua Li <shli@kernel.org> - 2017-09-14 23:10 +0200
Re: [PATCH V3 1/4] kthread: add a mechanism to store cgroup info Tejun Heo <tj@kernel.org> - 2017-09-15 18:00 +0200
[PATCH V3 3/4] block: make blkcg aware of kthread stored original cgroup info Shaohua Li <shli@kernel.org> - 2017-09-14 23:10 +0200
Re: [PATCH V3 0/4] block: make loop block device cgroup aware Shaohua Li <shli@kernel.org> - 2017-09-25 18:50 +0200
Re: [PATCH V3 0/4] block: make loop block device cgroup aware Jens Axboe <axboe@kernel.dk> - 2017-09-25 21:10 +0200
| From | Shaohua Li <shli@kernel.org> |
|---|---|
| Date | 2017-09-14 23:10 +0200 |
| Subject | [PATCH V3 0/4] block: make loop block device cgroup aware |
| Message-ID | <upJyy-1Kz-17@gated-at.bofh.it> |
From: Shaohua Li <shli@fb.com> Hi, The IO dispatched to under layer disk by loop block device isn't cloned from original bio, so the IO loses cgroup information of original bio. These IO escapes from cgroup control. The patches try to address this issue. The idea is quite generic, but we currently only make it work for blkcg. Thanks, Shaohua V2->V3: - Make the API more robust pointed out by Tejun V1->V2: - Address a couple of issues pointed out by Tejun Shaohua Li (4): kthread: add a mechanism to store cgroup info blkcg: delete unused APIs block: make blkcg aware of kthread stored original cgroup info block/loop: make loop cgroup aware block/bio.c | 31 ---------------------- drivers/block/loop.c | 13 +++++++++ drivers/block/loop.h | 1 + include/linux/bio.h | 2 -- include/linux/blk-cgroup.h | 25 +++++------------- include/linux/kthread.h | 11 ++++++++ kernel/kthread.c | 66 ++++++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 96 insertions(+), 53 deletions(-) -- 2.9.5
[toc] | [next] | [standalone]
| From | Shaohua Li <shli@kernel.org> |
|---|---|
| Date | 2017-09-14 23:10 +0200 |
| Subject | [PATCH V3 2/4] blkcg: delete unused APIs |
| Message-ID | <upJyz-1Kz-35@gated-at.bofh.it> |
| In reply to | #1732579 |
From: Shaohua Li <shli@fb.com>
Nobody uses the APIs right now.
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Shaohua Li <shli@fb.com>
---
block/bio.c | 31 -------------------------------
include/linux/bio.h | 2 --
include/linux/blk-cgroup.h | 12 ------------
3 files changed, 45 deletions(-)
diff --git a/block/bio.c b/block/bio.c
index 6745759..9271fa3 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -2033,37 +2033,6 @@ int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css)
EXPORT_SYMBOL_GPL(bio_associate_blkcg);
/**
- * bio_associate_current - associate a bio with %current
- * @bio: target bio
- *
- * Associate @bio with %current if it hasn't been associated yet. Block
- * layer will treat @bio as if it were issued by %current no matter which
- * task actually issues it.
- *
- * This function takes an extra reference of @task's io_context and blkcg
- * which will be put when @bio is released. The caller must own @bio,
- * ensure %current->io_context exists, and is responsible for synchronizing
- * calls to this function.
- */
-int bio_associate_current(struct bio *bio)
-{
- struct io_context *ioc;
-
- if (bio->bi_css)
- return -EBUSY;
-
- ioc = current->io_context;
- if (!ioc)
- return -ENOENT;
-
- get_io_context_active(ioc);
- bio->bi_ioc = ioc;
- bio->bi_css = task_get_css(current, io_cgrp_id);
- return 0;
-}
-EXPORT_SYMBOL_GPL(bio_associate_current);
-
-/**
* bio_disassociate_task - undo bio_associate_current()
* @bio: target bio
*/
diff --git a/include/linux/bio.h b/include/linux/bio.h
index a8fe793..d795cdd 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -514,13 +514,11 @@ do { \
#ifdef CONFIG_BLK_CGROUP
int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css);
-int bio_associate_current(struct bio *bio);
void bio_disassociate_task(struct bio *bio);
void bio_clone_blkcg_association(struct bio *dst, struct bio *src);
#else /* CONFIG_BLK_CGROUP */
static inline int bio_associate_blkcg(struct bio *bio,
struct cgroup_subsys_state *blkcg_css) { return 0; }
-static inline int bio_associate_current(struct bio *bio) { return -ENOENT; }
static inline void bio_disassociate_task(struct bio *bio) { }
static inline void bio_clone_blkcg_association(struct bio *dst,
struct bio *src) { }
diff --git a/include/linux/blk-cgroup.h b/include/linux/blk-cgroup.h
index 9d92153..0cfa8d2 100644
--- a/include/linux/blk-cgroup.h
+++ b/include/linux/blk-cgroup.h
@@ -235,12 +235,6 @@ static inline struct blkcg *bio_blkcg(struct bio *bio)
return task_blkcg(current);
}
-static inline struct cgroup_subsys_state *
-task_get_blkcg_css(struct task_struct *task)
-{
- return task_get_css(task, io_cgrp_id);
-}
-
/**
* blkcg_parent - get the parent of a blkcg
* @blkcg: blkcg of interest
@@ -735,12 +729,6 @@ struct blkcg_policy {
#define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL))
-static inline struct cgroup_subsys_state *
-task_get_blkcg_css(struct task_struct *task)
-{
- return NULL;
-}
-
#ifdef CONFIG_BLOCK
static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
--
2.9.5
[toc] | [prev] | [next] | [standalone]
| From | Shaohua Li <shli@kernel.org> |
|---|---|
| Date | 2017-09-14 23:10 +0200 |
| Subject | [PATCH V3 1/4] kthread: add a mechanism to store cgroup info |
| Message-ID | <upJyz-1Kz-37@gated-at.bofh.it> |
| In reply to | #1732579 |
From: Shaohua Li <shli@fb.com>
kthread usually runs jobs on behalf of other threads. The jobs should be
charged to cgroup of original threads. But the jobs run in a kthread,
where we lose the cgroup context of original threads. The patch adds a
machanism to record cgroup info of original threads in kthread context.
Later we can retrieve the cgroup info and attach the cgroup info to jobs.
Since this mechanism is only required by kthread, we store the cgroup
info in kthread data instead of generic task_struct.
Signed-off-by: Shaohua Li <shli@fb.com>
---
include/linux/kthread.h | 11 +++++++++
kernel/kthread.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 75 insertions(+), 2 deletions(-)
diff --git a/include/linux/kthread.h b/include/linux/kthread.h
index 82e197e..bd4369c 100644
--- a/include/linux/kthread.h
+++ b/include/linux/kthread.h
@@ -3,6 +3,7 @@
/* Simple interface for creating and stopping kernel threads without mess. */
#include <linux/err.h>
#include <linux/sched.h>
+#include <linux/cgroup.h>
__printf(4, 5)
struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
@@ -198,4 +199,14 @@ bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
void kthread_destroy_worker(struct kthread_worker *worker);
+#ifdef CONFIG_CGROUPS
+void kthread_associate_blkcg(struct cgroup_subsys_state *css);
+struct cgroup_subsys_state *kthread_blkcg(void);
+#else
+static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
+static inline struct cgroup_subsys_state *kthread_blkcg(void)
+{
+ return NULL;
+}
+#endif
#endif /* _LINUX_KTHREAD_H */
diff --git a/kernel/kthread.c b/kernel/kthread.c
index 26db528..a8b4e83 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -20,7 +20,6 @@
#include <linux/freezer.h>
#include <linux/ptrace.h>
#include <linux/uaccess.h>
-#include <linux/cgroup.h>
#include <trace/events/sched.h>
static DEFINE_SPINLOCK(kthread_create_lock);
@@ -47,6 +46,9 @@ struct kthread {
void *data;
struct completion parked;
struct completion exited;
+#ifdef CONFIG_CGROUPS
+ struct cgroup_subsys_state *blkcg_css;
+#endif
};
enum KTHREAD_BITS {
@@ -74,11 +76,17 @@ static inline struct kthread *to_kthread(struct task_struct *k)
void free_kthread_struct(struct task_struct *k)
{
+ struct kthread *kthread;
+
/*
* Can be NULL if this kthread was created by kernel_thread()
* or if kmalloc() in kthread() failed.
*/
- kfree(to_kthread(k));
+ kthread = to_kthread(k);
+#ifdef CONFIG_CGROUPS
+ WARN_ON_ONCE(kthread && kthread->blkcg_css);
+#endif
+ kfree(kthread);
}
/**
@@ -216,6 +224,9 @@ static int kthread(void *_create)
self->data = data;
init_completion(&self->exited);
init_completion(&self->parked);
+#ifdef CONFIG_CGROUPS
+ self->blkcg_css = NULL;
+#endif
current->vfork_done = &self->exited;
/* OK, tell user we're spawned, wait for stop or wakeup */
@@ -1153,3 +1164,54 @@ void kthread_destroy_worker(struct kthread_worker *worker)
kfree(worker);
}
EXPORT_SYMBOL(kthread_destroy_worker);
+
+#ifdef CONFIG_CGROUPS
+/**
+ * kthread_associate_blkcg - associate blkcg to current kthread
+ * @css: the cgroup info
+ *
+ * Current thread must be a kthread. The thread is running jobs on behalf of
+ * other threads. In some cases, we expect the jobs attach cgroup info of
+ * original threads instead of that of current thread. This function stores
+ * original thread's cgroup info in current kthread context for later
+ * retrieval.
+ */
+void kthread_associate_blkcg(struct cgroup_subsys_state *css)
+{
+ struct kthread *kthread;
+
+ if (!(current->flags & PF_KTHREAD))
+ return;
+ kthread = to_kthread(current);
+ if (!kthread)
+ return;
+
+ if (kthread->blkcg_css) {
+ css_put(kthread->blkcg_css);
+ kthread->blkcg_css = NULL;
+ }
+ if (css) {
+ css_get(css);
+ kthread->blkcg_css = css;
+ }
+}
+EXPORT_SYMBOL(kthread_associate_blkcg);
+
+/**
+ * kthread_blkcg - get associated blkcg css of current kthread
+ *
+ * Current thread must be a kthread.
+ */
+struct cgroup_subsys_state *kthread_blkcg(void)
+{
+ struct kthread *kthread;
+
+ if (current->flags & PF_KTHREAD) {
+ kthread = to_kthread(current);
+ if (kthread)
+ return kthread->blkcg_css;
+ }
+ return NULL;
+}
+EXPORT_SYMBOL(kthread_blkcg);
+#endif
--
2.9.5
[toc] | [prev] | [next] | [standalone]
| From | Tejun Heo <tj@kernel.org> |
|---|---|
| Date | 2017-09-15 18:00 +0200 |
| Subject | Re: [PATCH V3 1/4] kthread: add a mechanism to store cgroup info |
| Message-ID | <uq1c5-4Zr-9@gated-at.bofh.it> |
| In reply to | #1732582 |
On Thu, Sep 14, 2017 at 02:02:04PM -0700, Shaohua Li wrote: > From: Shaohua Li <shli@fb.com> > > kthread usually runs jobs on behalf of other threads. The jobs should be > charged to cgroup of original threads. But the jobs run in a kthread, > where we lose the cgroup context of original threads. The patch adds a > machanism to record cgroup info of original threads in kthread context. > Later we can retrieve the cgroup info and attach the cgroup info to jobs. > > Since this mechanism is only required by kthread, we store the cgroup > info in kthread data instead of generic task_struct. > > Signed-off-by: Shaohua Li <shli@fb.com> Acked-by: Tejun Heo <tj@kernel.org> Thanks. -- tejun
[toc] | [prev] | [next] | [standalone]
| From | Shaohua Li <shli@kernel.org> |
|---|---|
| Date | 2017-09-14 23:10 +0200 |
| Subject | [PATCH V3 3/4] block: make blkcg aware of kthread stored original cgroup info |
| Message-ID | <upJyz-1Kz-43@gated-at.bofh.it> |
| In reply to | #1732579 |
From: Shaohua Li <shli@fb.com>
bio_blkcg is the only API to get cgroup info for a bio right now. If
bio_blkcg finds current task is a kthread and has original blkcg
associated, it will use the css instead of associating the bio to
current task. This makes it possible that kthread dispatches bios on
behalf of other threads.
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Shaohua Li <shli@fb.com>
---
include/linux/blk-cgroup.h | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/include/linux/blk-cgroup.h b/include/linux/blk-cgroup.h
index 0cfa8d2..f57e54d 100644
--- a/include/linux/blk-cgroup.h
+++ b/include/linux/blk-cgroup.h
@@ -19,6 +19,7 @@
#include <linux/radix-tree.h>
#include <linux/blkdev.h>
#include <linux/atomic.h>
+#include <linux/kthread.h>
/* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */
#define BLKG_STAT_CPU_BATCH (INT_MAX / 2)
@@ -223,16 +224,16 @@ static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
return css ? container_of(css, struct blkcg, css) : NULL;
}
-static inline struct blkcg *task_blkcg(struct task_struct *tsk)
-{
- return css_to_blkcg(task_css(tsk, io_cgrp_id));
-}
-
static inline struct blkcg *bio_blkcg(struct bio *bio)
{
+ struct cgroup_subsys_state *css;
+
if (bio && bio->bi_css)
return css_to_blkcg(bio->bi_css);
- return task_blkcg(current);
+ css = kthread_blkcg();
+ if (css)
+ return css_to_blkcg(css);
+ return css_to_blkcg(task_css(current, io_cgrp_id));
}
/**
--
2.9.5
[toc] | [prev] | [next] | [standalone]
| From | Shaohua Li <shli@kernel.org> |
|---|---|
| Date | 2017-09-25 18:50 +0200 |
| Message-ID | <utEJY-ul-3@gated-at.bofh.it> |
| In reply to | #1732579 |
On Thu, Sep 14, 2017 at 02:02:03PM -0700, Shaohua Li wrote: > From: Shaohua Li <shli@fb.com> > > Hi, > > The IO dispatched to under layer disk by loop block device isn't cloned from > original bio, so the IO loses cgroup information of original bio. These IO > escapes from cgroup control. The patches try to address this issue. The idea is > quite generic, but we currently only make it work for blkcg. Ping! how do we proceed with this patch set? Thanks, Shaohua > > Thanks, > Shaohua > > V2->V3: > - Make the API more robust pointed out by Tejun > > V1->V2: > - Address a couple of issues pointed out by Tejun > > > Shaohua Li (4): > kthread: add a mechanism to store cgroup info > blkcg: delete unused APIs > block: make blkcg aware of kthread stored original cgroup info > block/loop: make loop cgroup aware > > block/bio.c | 31 ---------------------- > drivers/block/loop.c | 13 +++++++++ > drivers/block/loop.h | 1 + > include/linux/bio.h | 2 -- > include/linux/blk-cgroup.h | 25 +++++------------- > include/linux/kthread.h | 11 ++++++++ > kernel/kthread.c | 66 ++++++++++++++++++++++++++++++++++++++++++++-- > 7 files changed, 96 insertions(+), 53 deletions(-) > > -- > 2.9.5 >
[toc] | [prev] | [next] | [standalone]
| From | Jens Axboe <axboe@kernel.dk> |
|---|---|
| Date | 2017-09-25 21:10 +0200 |
| Message-ID | <utGVs-27d-7@gated-at.bofh.it> |
| In reply to | #1739169 |
On 09/25/2017 10:44 AM, Shaohua Li wrote: > On Thu, Sep 14, 2017 at 02:02:03PM -0700, Shaohua Li wrote: >> From: Shaohua Li <shli@fb.com> >> >> Hi, >> >> The IO dispatched to under layer disk by loop block device isn't cloned from >> original bio, so the IO loses cgroup information of original bio. These IO >> escapes from cgroup control. The patches try to address this issue. The idea is >> quite generic, but we currently only make it work for blkcg. > > Ping! how do we proceed with this patch set? I'll queue it up for 4.15. -- Jens Axboe
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web