Skip to content

Commit b978962

Browse files
dennisszhouaxboe
authored andcommitted
blkcg: update blkg_lookup_create() to do locking
To know when to create a blkg, the general pattern is to do a blkg_lookup() and if that fails, lock and do the lookup again, and if that fails finally create. It doesn't make much sense for everyone who wants to do creation to write this themselves. This changes blkg_lookup_create() to do locking and implement this pattern. The old blkg_lookup_create() is renamed to __blkg_lookup_create(). If a call site wants to do its own error handling or already owns the queue lock, they can use __blkg_lookup_create(). This will be used in upcoming patches. Signed-off-by: Dennis Zhou <dennis@kernel.org> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Acked-by: Tejun Heo <tj@kernel.org> Reviewed-by: Liu Bo <bo.liu@linux.alibaba.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 0fe061b commit b978962

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

block/blk-cgroup.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
249249
}
250250

251251
/**
252-
* blkg_lookup_create - lookup blkg, try to create one if not there
252+
* __blkg_lookup_create - lookup blkg, try to create one if not there
253253
* @blkcg: blkcg of interest
254254
* @q: request_queue of interest
255255
*
@@ -262,8 +262,8 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
262262
* value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
263263
* dead and bypassing, returns ERR_PTR(-EBUSY).
264264
*/
265-
struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
266-
struct request_queue *q)
265+
struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
266+
struct request_queue *q)
267267
{
268268
struct blkcg_gq *blkg;
269269

@@ -293,6 +293,28 @@ struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
293293
}
294294
}
295295

296+
/**
297+
* blkg_lookup_create - find or create a blkg
298+
* @blkcg: target block cgroup
299+
* @q: target request_queue
300+
*
301+
* This looks up or creates the blkg representing the unique pair
302+
* of the blkcg and the request_queue.
303+
*/
304+
struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
305+
struct request_queue *q)
306+
{
307+
struct blkcg_gq *blkg = blkg_lookup(blkcg, q);
308+
309+
if (unlikely(!blkg)) {
310+
spin_lock_irq(&q->queue_lock);
311+
blkg = __blkg_lookup_create(blkcg, q);
312+
spin_unlock_irq(&q->queue_lock);
313+
}
314+
315+
return blkg;
316+
}
317+
296318
static void blkg_destroy(struct blkcg_gq *blkg)
297319
{
298320
struct blkcg *blkcg = blkg->blkcg;

block/blk-iolatency.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ static void blkcg_iolatency_throttle(struct rq_qos *rqos, struct bio *bio)
486486
blkg = blkg_lookup(blkcg, q);
487487
if (unlikely(!blkg)) {
488488
spin_lock_irq(&q->queue_lock);
489-
blkg = blkg_lookup_create(blkcg, q);
489+
blkg = __blkg_lookup_create(blkcg, q);
490490
if (IS_ERR(blkg))
491491
blkg = NULL;
492492
spin_unlock_irq(&q->queue_lock);

include/linux/blk-cgroup.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ extern struct cgroup_subsys_state * const blkcg_root_css;
181181

182182
struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
183183
struct request_queue *q, bool update_hint);
184+
struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
185+
struct request_queue *q);
184186
struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
185187
struct request_queue *q);
186188
int blkcg_init_queue(struct request_queue *q);
@@ -799,7 +801,7 @@ static inline bool blkcg_bio_issue_check(struct request_queue *q,
799801
blkg = blkg_lookup(blkcg, q);
800802
if (unlikely(!blkg)) {
801803
spin_lock_irq(&q->queue_lock);
802-
blkg = blkg_lookup_create(blkcg, q);
804+
blkg = __blkg_lookup_create(blkcg, q);
803805
if (IS_ERR(blkg))
804806
blkg = NULL;
805807
spin_unlock_irq(&q->queue_lock);

0 commit comments

Comments
 (0)