Skip to content

Commit 08e18ea

Browse files
Josef Bacikaxboe
authored andcommitted
block: add bi_blkg to the bio for cgroups
Currently io.low uses a bi_cg_private to stash its private data for the blkg, however other blkcg policies may want to use this as well. Since we can get the private data out of the blkg, move this to bi_blkg in the bio and make it generic, then we can use bio_associate_blkg() to attach the blkg to the bio. Theoretically we could simply replace the bi_css with this since we can get to all the same information from the blkg, however you have to lookup the blkg, so for example wbc_init_bio() would have to lookup and possibly allocate the blkg for the css it was trying to attach to the bio. This could be problematic and result in us either not attaching the css at all to the bio, or falling back to the root blkcg if we are unable to allocate the corresponding blkg. So for now do this, and in the future if possible we could just replace the bi_css with bi_blkg and update the helpers to do the correct translation. Signed-off-by: Josef Bacik <jbacik@fb.com> Acked-by: Tejun Heo <tj@kernel.org> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 6e76871 commit 08e18ea

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

block/bio.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <linux/mempool.h>
2929
#include <linux/workqueue.h>
3030
#include <linux/cgroup.h>
31+
#include <linux/blk-cgroup.h>
3132

3233
#include <trace/events/block.h>
3334
#include "blk.h"
@@ -2036,6 +2037,24 @@ int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css)
20362037
}
20372038
EXPORT_SYMBOL_GPL(bio_associate_blkcg);
20382039

2040+
/**
2041+
* bio_associate_blkg - associate a bio with the specified blkg
2042+
* @bio: target bio
2043+
* @blkg: the blkg to associate
2044+
*
2045+
* Associate @bio with the blkg specified by @blkg. This is the queue specific
2046+
* blkcg information associated with the @bio, a reference will be taken on the
2047+
* @blkg and will be freed when the bio is freed.
2048+
*/
2049+
int bio_associate_blkg(struct bio *bio, struct blkcg_gq *blkg)
2050+
{
2051+
if (unlikely(bio->bi_blkg))
2052+
return -EBUSY;
2053+
blkg_get(blkg);
2054+
bio->bi_blkg = blkg;
2055+
return 0;
2056+
}
2057+
20392058
/**
20402059
* bio_disassociate_task - undo bio_associate_current()
20412060
* @bio: target bio
@@ -2050,6 +2069,10 @@ void bio_disassociate_task(struct bio *bio)
20502069
css_put(bio->bi_css);
20512070
bio->bi_css = NULL;
20522071
}
2072+
if (bio->bi_blkg) {
2073+
blkg_put(bio->bi_blkg);
2074+
bio->bi_blkg = NULL;
2075+
}
20532076
}
20542077

20552078
/**

block/blk-throttle.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,12 +2134,8 @@ static inline void throtl_update_latency_buckets(struct throtl_data *td)
21342134
static void blk_throtl_assoc_bio(struct throtl_grp *tg, struct bio *bio)
21352135
{
21362136
#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2137-
if (bio->bi_css) {
2138-
if (bio->bi_cg_private)
2139-
blkg_put(tg_to_blkg(bio->bi_cg_private));
2140-
bio->bi_cg_private = tg;
2141-
blkg_get(tg_to_blkg(tg));
2142-
}
2137+
if (bio->bi_css)
2138+
bio_associate_blkg(bio, tg_to_blkg(tg));
21432139
bio_issue_init(&bio->bi_issue, bio_sectors(bio));
21442140
#endif
21452141
}
@@ -2287,27 +2283,26 @@ void blk_throtl_stat_add(struct request *rq, u64 time_ns)
22872283

22882284
void blk_throtl_bio_endio(struct bio *bio)
22892285
{
2286+
struct blkcg_gq *blkg;
22902287
struct throtl_grp *tg;
22912288
u64 finish_time_ns;
22922289
unsigned long finish_time;
22932290
unsigned long start_time;
22942291
unsigned long lat;
22952292
int rw = bio_data_dir(bio);
22962293

2297-
tg = bio->bi_cg_private;
2298-
if (!tg)
2294+
blkg = bio->bi_blkg;
2295+
if (!blkg)
22992296
return;
2300-
bio->bi_cg_private = NULL;
2297+
tg = blkg_to_tg(blkg);
23012298

23022299
finish_time_ns = ktime_get_ns();
23032300
tg->last_finish_time = finish_time_ns >> 10;
23042301

23052302
start_time = bio_issue_time(&bio->bi_issue) >> 10;
23062303
finish_time = __bio_issue_time(finish_time_ns) >> 10;
2307-
if (!start_time || finish_time <= start_time) {
2308-
blkg_put(tg_to_blkg(tg));
2304+
if (!start_time || finish_time <= start_time)
23092305
return;
2310-
}
23112306

23122307
lat = finish_time - start_time;
23132308
/* this is only for bio based driver */
@@ -2336,8 +2331,6 @@ void blk_throtl_bio_endio(struct bio *bio)
23362331
tg->bio_cnt /= 2;
23372332
tg->bad_bio_cnt /= 2;
23382333
}
2339-
2340-
blkg_put(tg_to_blkg(tg));
23412334
}
23422335
#endif
23432336

include/linux/bio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ do { \
555555

556556
#ifdef CONFIG_BLK_CGROUP
557557
int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css);
558+
int bio_associate_blkg(struct bio *bio, struct blkcg_gq *blkg);
558559
void bio_disassociate_task(struct bio *bio);
559560
void bio_clone_blkcg_association(struct bio *dst, struct bio *src);
560561
#else /* CONFIG_BLK_CGROUP */

include/linux/blk_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ struct bio {
179179
*/
180180
struct io_context *bi_ioc;
181181
struct cgroup_subsys_state *bi_css;
182+
struct blkcg_gq *bi_blkg;
182183
#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
183-
void *bi_cg_private;
184184
struct bio_issue bi_issue;
185185
#endif
186186
#endif

0 commit comments

Comments
 (0)