Skip to content

Commit 9ff0125

Browse files
liubogithubaxboe
authored andcommitted
Blk-throttle: update to use rbtree with leftmost node cached
As rbtree has native support of caching leftmost node, i.e. rb_root_cached, no need to do the caching by ourselves. Signed-off-by: Liu Bo <bo.liu@linux.alibaba.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 576ed91 commit 9ff0125

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

block/blk-throttle.c

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ struct throtl_service_queue {
8484
* RB tree of active children throtl_grp's, which are sorted by
8585
* their ->disptime.
8686
*/
87-
struct rb_root pending_tree; /* RB tree of active tgs */
88-
struct rb_node *first_pending; /* first node in the tree */
87+
struct rb_root_cached pending_tree; /* RB tree of active tgs */
8988
unsigned int nr_pending; /* # queued in the tree */
9089
unsigned long first_pending_disptime; /* disptime of the first tg */
9190
struct timer_list pending_timer; /* fires on first_pending_disptime */
@@ -475,7 +474,7 @@ static void throtl_service_queue_init(struct throtl_service_queue *sq)
475474
{
476475
INIT_LIST_HEAD(&sq->queued[0]);
477476
INIT_LIST_HEAD(&sq->queued[1]);
478-
sq->pending_tree = RB_ROOT;
477+
sq->pending_tree = RB_ROOT_CACHED;
479478
timer_setup(&sq->pending_timer, throtl_pending_timer_fn, 0);
480479
}
481480

@@ -616,31 +615,23 @@ static void throtl_pd_free(struct blkg_policy_data *pd)
616615
static struct throtl_grp *
617616
throtl_rb_first(struct throtl_service_queue *parent_sq)
618617
{
618+
struct rb_node *n;
619619
/* Service tree is empty */
620620
if (!parent_sq->nr_pending)
621621
return NULL;
622622

623-
if (!parent_sq->first_pending)
624-
parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
625-
626-
if (parent_sq->first_pending)
627-
return rb_entry_tg(parent_sq->first_pending);
628-
629-
return NULL;
630-
}
631-
632-
static void rb_erase_init(struct rb_node *n, struct rb_root *root)
633-
{
634-
rb_erase(n, root);
635-
RB_CLEAR_NODE(n);
623+
n = rb_first_cached(&parent_sq->pending_tree);
624+
WARN_ON_ONCE(!n);
625+
if (!n)
626+
return NULL;
627+
return rb_entry_tg(n);
636628
}
637629

638630
static void throtl_rb_erase(struct rb_node *n,
639631
struct throtl_service_queue *parent_sq)
640632
{
641-
if (parent_sq->first_pending == n)
642-
parent_sq->first_pending = NULL;
643-
rb_erase_init(n, &parent_sq->pending_tree);
633+
rb_erase_cached(n, &parent_sq->pending_tree);
634+
RB_CLEAR_NODE(n);
644635
--parent_sq->nr_pending;
645636
}
646637

@@ -658,11 +649,11 @@ static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
658649
static void tg_service_queue_add(struct throtl_grp *tg)
659650
{
660651
struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
661-
struct rb_node **node = &parent_sq->pending_tree.rb_node;
652+
struct rb_node **node = &parent_sq->pending_tree.rb_root.rb_node;
662653
struct rb_node *parent = NULL;
663654
struct throtl_grp *__tg;
664655
unsigned long key = tg->disptime;
665-
int left = 1;
656+
bool leftmost = true;
666657

667658
while (*node != NULL) {
668659
parent = *node;
@@ -672,15 +663,13 @@ static void tg_service_queue_add(struct throtl_grp *tg)
672663
node = &parent->rb_left;
673664
else {
674665
node = &parent->rb_right;
675-
left = 0;
666+
leftmost = false;
676667
}
677668
}
678669

679-
if (left)
680-
parent_sq->first_pending = &tg->rb_node;
681-
682670
rb_link_node(&tg->rb_node, parent, node);
683-
rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
671+
rb_insert_color_cached(&tg->rb_node, &parent_sq->pending_tree,
672+
leftmost);
684673
}
685674

686675
static void __throtl_enqueue_tg(struct throtl_grp *tg)

0 commit comments

Comments
 (0)