]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - block/blk-throttle.c
blkcg: move root blkg lookup optimization from throtl_lookup_tg() to __blkg_lookup()
[karo-tx-linux.git] / block / blk-throttle.c
index b23193518ac7a964a9f0d75b26c6fa844f6eea40..1f63fc834dc3e6dab428ead1a0daee3b56574059 100644 (file)
@@ -144,9 +144,6 @@ struct throtl_grp {
 
        /* Per cpu stats pointer */
        struct tg_stats_cpu __percpu *stats_cpu;
-
-       /* List of tgs waiting for per cpu stats memory to be allocated */
-       struct list_head stats_alloc_node;
 };
 
 struct throtl_data
@@ -168,13 +165,6 @@ struct throtl_data
        struct work_struct dispatch_work;
 };
 
-/* list and work item to allocate percpu group stats */
-static DEFINE_SPINLOCK(tg_stats_alloc_lock);
-static LIST_HEAD(tg_stats_alloc_list);
-
-static void tg_stats_alloc_fn(struct work_struct *);
-static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
-
 static void throtl_pending_timer_fn(unsigned long arg);
 
 static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
@@ -256,53 +246,6 @@ static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
        }                                                               \
 } while (0)
 
-static void tg_stats_init(struct tg_stats_cpu *tg_stats)
-{
-       blkg_rwstat_init(&tg_stats->service_bytes);
-       blkg_rwstat_init(&tg_stats->serviced);
-}
-
-/*
- * Worker for allocating per cpu stat for tgs. This is scheduled on the
- * system_wq once there are some groups on the alloc_list waiting for
- * allocation.
- */
-static void tg_stats_alloc_fn(struct work_struct *work)
-{
-       static struct tg_stats_cpu *stats_cpu;  /* this fn is non-reentrant */
-       struct delayed_work *dwork = to_delayed_work(work);
-       bool empty = false;
-
-alloc_stats:
-       if (!stats_cpu) {
-               int cpu;
-
-               stats_cpu = alloc_percpu(struct tg_stats_cpu);
-               if (!stats_cpu) {
-                       /* allocation failed, try again after some time */
-                       schedule_delayed_work(dwork, msecs_to_jiffies(10));
-                       return;
-               }
-               for_each_possible_cpu(cpu)
-                       tg_stats_init(per_cpu_ptr(stats_cpu, cpu));
-       }
-
-       spin_lock_irq(&tg_stats_alloc_lock);
-
-       if (!list_empty(&tg_stats_alloc_list)) {
-               struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
-                                                        struct throtl_grp,
-                                                        stats_alloc_node);
-               swap(tg->stats_cpu, stats_cpu);
-               list_del_init(&tg->stats_alloc_node);
-       }
-
-       empty = list_empty(&tg_stats_alloc_list);
-       spin_unlock_irq(&tg_stats_alloc_lock);
-       if (!empty)
-               goto alloc_stats;
-}
-
 static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
 {
        INIT_LIST_HEAD(&qn->node);
@@ -387,29 +330,59 @@ static struct bio *throtl_pop_queued(struct list_head *queued,
 }
 
 /* init a service_queue, assumes the caller zeroed it */
-static void throtl_service_queue_init(struct throtl_service_queue *sq,
-                                     struct throtl_service_queue *parent_sq)
+static void throtl_service_queue_init(struct throtl_service_queue *sq)
 {
        INIT_LIST_HEAD(&sq->queued[0]);
        INIT_LIST_HEAD(&sq->queued[1]);
        sq->pending_tree = RB_ROOT;
-       sq->parent_sq = parent_sq;
        setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
                    (unsigned long)sq);
 }
 
-static void throtl_service_queue_exit(struct throtl_service_queue *sq)
+static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
 {
-       del_timer_sync(&sq->pending_timer);
+       struct throtl_grp *tg;
+       int rw, cpu;
+
+       tg = kzalloc_node(sizeof(*tg), gfp, node);
+       if (!tg)
+               return NULL;
+
+       tg->stats_cpu = alloc_percpu_gfp(struct tg_stats_cpu, gfp);
+       if (!tg->stats_cpu) {
+               kfree(tg);
+               return NULL;
+       }
+
+       throtl_service_queue_init(&tg->service_queue);
+
+       for (rw = READ; rw <= WRITE; rw++) {
+               throtl_qnode_init(&tg->qnode_on_self[rw], tg);
+               throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
+       }
+
+       RB_CLEAR_NODE(&tg->rb_node);
+       tg->bps[READ] = -1;
+       tg->bps[WRITE] = -1;
+       tg->iops[READ] = -1;
+       tg->iops[WRITE] = -1;
+
+       for_each_possible_cpu(cpu) {
+               struct tg_stats_cpu *stats_cpu = per_cpu_ptr(tg->stats_cpu, cpu);
+
+               blkg_rwstat_init(&stats_cpu->service_bytes);
+               blkg_rwstat_init(&stats_cpu->serviced);
+       }
+
+       return &tg->pd;
 }
 
-static void throtl_pd_init(struct blkcg_gq *blkg)
+static void throtl_pd_init(struct blkg_policy_data *pd)
 {
-       struct throtl_grp *tg = blkg_to_tg(blkg);
+       struct throtl_grp *tg = pd_to_tg(pd);
+       struct blkcg_gq *blkg = tg_to_blkg(tg);
        struct throtl_data *td = blkg->q->td;
-       struct throtl_service_queue *parent_sq;
-       unsigned long flags;
-       int rw;
+       struct throtl_service_queue *sq = &tg->service_queue;
 
        /*
         * If on the default hierarchy, we switch to properly hierarchical
@@ -424,35 +397,10 @@ static void throtl_pd_init(struct blkcg_gq *blkg)
         * Limits of a group don't interact with limits of other groups
         * regardless of the position of the group in the hierarchy.
         */
-       parent_sq = &td->service_queue;
-
+       sq->parent_sq = &td->service_queue;
        if (cgroup_on_dfl(blkg->blkcg->css.cgroup) && blkg->parent)
-               parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
-
-       throtl_service_queue_init(&tg->service_queue, parent_sq);
-
-       for (rw = READ; rw <= WRITE; rw++) {
-               throtl_qnode_init(&tg->qnode_on_self[rw], tg);
-               throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
-       }
-
-       RB_CLEAR_NODE(&tg->rb_node);
+               sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
        tg->td = td;
-
-       tg->bps[READ] = -1;
-       tg->bps[WRITE] = -1;
-       tg->iops[READ] = -1;
-       tg->iops[WRITE] = -1;
-
-       /*
-        * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
-        * but percpu allocator can't be called from IO path.  Queue tg on
-        * tg_stats_alloc_list and allocate from work item.
-        */
-       spin_lock_irqsave(&tg_stats_alloc_lock, flags);
-       list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
-       schedule_delayed_work(&tg_stats_alloc_work, 0);
-       spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
 }
 
 /*
@@ -470,37 +418,29 @@ static void tg_update_has_rules(struct throtl_grp *tg)
                                    (tg->bps[rw] != -1 || tg->iops[rw] != -1);
 }
 
-static void throtl_pd_online(struct blkcg_gq *blkg)
+static void throtl_pd_online(struct blkg_policy_data *pd)
 {
        /*
         * We don't want new groups to escape the limits of its ancestors.
         * Update has_rules[] after a new group is brought online.
         */
-       tg_update_has_rules(blkg_to_tg(blkg));
+       tg_update_has_rules(pd_to_tg(pd));
 }
 
-static void throtl_pd_exit(struct blkcg_gq *blkg)
+static void throtl_pd_free(struct blkg_policy_data *pd)
 {
-       struct throtl_grp *tg = blkg_to_tg(blkg);
-       unsigned long flags;
-
-       spin_lock_irqsave(&tg_stats_alloc_lock, flags);
-       list_del_init(&tg->stats_alloc_node);
-       spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
+       struct throtl_grp *tg = pd_to_tg(pd);
 
+       del_timer_sync(&tg->service_queue.pending_timer);
        free_percpu(tg->stats_cpu);
-
-       throtl_service_queue_exit(&tg->service_queue);
+       kfree(tg);
 }
 
-static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
+static void throtl_pd_reset_stats(struct blkg_policy_data *pd)
 {
-       struct throtl_grp *tg = blkg_to_tg(blkg);
+       struct throtl_grp *tg = pd_to_tg(pd);
        int cpu;
 
-       if (tg->stats_cpu == NULL)
-               return;
-
        for_each_possible_cpu(cpu) {
                struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
 
@@ -512,13 +452,6 @@ static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
 static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
                                           struct blkcg *blkcg)
 {
-       /*
-        * This is the common case when there are no blkcgs.  Avoid lookup
-        * in this case
-        */
-       if (blkcg == &blkcg_root)
-               return td_root_tg(td);
-
        return blkg_to_tg(blkg_lookup(blkcg, td->queue));
 }
 
@@ -963,10 +896,6 @@ static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
        struct tg_stats_cpu *stats_cpu;
        unsigned long flags;
 
-       /* If per cpu stats are not allocated yet, don't do any accounting. */
-       if (tg->stats_cpu == NULL)
-               return;
-
        /*
         * Disabling interrupts to provide mutual exclusion between two
         * writes on same cpu. It probably is not needed for 64bit. Not
@@ -1292,9 +1221,6 @@ static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
        struct blkg_rwstat rwstat = { }, tmp;
        int i, cpu;
 
-       if (tg->stats_cpu == NULL)
-               return 0;
-
        for_each_possible_cpu(cpu) {
                struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
 
@@ -1468,12 +1394,12 @@ static void throtl_shutdown_wq(struct request_queue *q)
 }
 
 static struct blkcg_policy blkcg_policy_throtl = {
-       .pd_size                = sizeof(struct throtl_grp),
        .cftypes                = throtl_files,
 
+       .pd_alloc_fn            = throtl_pd_alloc,
        .pd_init_fn             = throtl_pd_init,
        .pd_online_fn           = throtl_pd_online,
-       .pd_exit_fn             = throtl_pd_exit,
+       .pd_free_fn             = throtl_pd_free,
        .pd_reset_stats_fn      = throtl_pd_reset_stats,
 };
 
@@ -1667,7 +1593,7 @@ int blk_throtl_init(struct request_queue *q)
                return -ENOMEM;
 
        INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
-       throtl_service_queue_init(&td->service_queue, NULL);
+       throtl_service_queue_init(&td->service_queue);
 
        q->td = td;
        td->queue = q;