Skip to content

Commit 49f4c2d

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 then do a 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 <dennisszhou@gmail.com> 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 27e6fa9 commit 49f4c2d

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

block/blk-cgroup.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
259259
}
260260

261261
/**
262-
* blkg_lookup_create - lookup blkg, try to create one if not there
262+
* __blkg_lookup_create - lookup blkg, try to create one if not there
263263
* @blkcg: blkcg of interest
264264
* @q: request_queue of interest
265265
*
@@ -272,8 +272,8 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
272272
* value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
273273
* dead and bypassing, returns ERR_PTR(-EBUSY).
274274
*/
275-
struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
276-
struct request_queue *q)
275+
struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
276+
struct request_queue *q)
277277
{
278278
struct blkcg_gq *blkg;
279279

@@ -310,6 +310,31 @@ struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
310310
}
311311
}
312312

313+
/**
314+
* blkg_lookup_create - find or create a blkg
315+
* @blkcg: target block cgroup
316+
* @q: target request_queue
317+
*
318+
* This looks up or creates the blkg representing the unique pair
319+
* of the blkcg and the request_queue.
320+
*/
321+
struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
322+
struct request_queue *q)
323+
{
324+
struct blkcg_gq *blkg = blkg_lookup(blkcg, q);
325+
unsigned long flags;
326+
327+
if (unlikely(!blkg)) {
328+
spin_lock_irqsave(q->queue_lock, flags);
329+
330+
blkg = __blkg_lookup_create(blkcg, q);
331+
332+
spin_unlock_irqrestore(q->queue_lock, flags);
333+
}
334+
335+
return blkg;
336+
}
337+
313338
static void blkg_destroy(struct blkcg_gq *blkg)
314339
{
315340
struct blkcg *blkcg = blkg->blkcg;

block/blk-iolatency.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ static void blkcg_iolatency_throttle(struct rq_qos *rqos, struct bio *bio,
407407
if (unlikely(!blkg)) {
408408
if (!lock)
409409
spin_lock_irq(q->queue_lock);
410-
blkg = blkg_lookup_create(blkcg, q);
410+
blkg = __blkg_lookup_create(blkcg, q);
411411
if (IS_ERR(blkg))
412412
blkg = NULL;
413413
if (!lock)

include/linux/blk-cgroup.h

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

185185
struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
186186
struct request_queue *q, bool update_hint);
187+
struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
188+
struct request_queue *q);
187189
struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
188190
struct request_queue *q);
189191
int blkcg_init_queue(struct request_queue *q);
@@ -897,7 +899,7 @@ static inline bool blkcg_bio_issue_check(struct request_queue *q,
897899
blkg = blkg_lookup(blkcg, q);
898900
if (unlikely(!blkg)) {
899901
spin_lock_irq(q->queue_lock);
900-
blkg = blkg_lookup_create(blkcg, q);
902+
blkg = __blkg_lookup_create(blkcg, q);
901903
if (IS_ERR(blkg))
902904
blkg = NULL;
903905
spin_unlock_irq(q->queue_lock);

0 commit comments

Comments
 (0)