]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - block/blk-cgroup.c
Merge tag 'for-linus-20190715' of git://git.kernel.org/pub/scm/linux/kernel/git/braun...
[linux.git] / block / blk-cgroup.c
index 1f7127b03490db873a6a631a9ca17d349995ac81..24ed26957367e2cbba91590040c9454612edad98 100644 (file)
@@ -29,6 +29,7 @@
 #include <linux/ctype.h>
 #include <linux/blk-cgroup.h>
 #include <linux/tracehook.h>
+#include <linux/psi.h>
 #include "blk.h"
 
 #define MAX_KEY_LEN 100
@@ -47,12 +48,14 @@ struct blkcg blkcg_root;
 EXPORT_SYMBOL_GPL(blkcg_root);
 
 struct cgroup_subsys_state * const blkcg_root_css = &blkcg_root.css;
+EXPORT_SYMBOL_GPL(blkcg_root_css);
 
 static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
 
 static LIST_HEAD(all_blkcgs);          /* protected by blkcg_pol_mutex */
 
 static bool blkcg_debug_stats = false;
+static struct workqueue_struct *blkcg_punt_bio_wq;
 
 static bool blkcg_policy_enabled(struct request_queue *q,
                                 const struct blkcg_policy *pol)
@@ -79,6 +82,7 @@ static void blkg_free(struct blkcg_gq *blkg)
 
        blkg_rwstat_exit(&blkg->stat_ios);
        blkg_rwstat_exit(&blkg->stat_bytes);
+       percpu_ref_exit(&blkg->refcnt);
        kfree(blkg);
 }
 
@@ -86,7 +90,7 @@ static void __blkg_release(struct rcu_head *rcu)
 {
        struct blkcg_gq *blkg = container_of(rcu, struct blkcg_gq, rcu_head);
 
-       percpu_ref_exit(&blkg->refcnt);
+       WARN_ON(!bio_list_empty(&blkg->async_bios));
 
        /* release the blkcg and parent blkg refs this blkg has been holding */
        css_put(&blkg->blkcg->css);
@@ -113,6 +117,23 @@ static void blkg_release(struct percpu_ref *ref)
        call_rcu(&blkg->rcu_head, __blkg_release);
 }
 
+static void blkg_async_bio_workfn(struct work_struct *work)
+{
+       struct blkcg_gq *blkg = container_of(work, struct blkcg_gq,
+                                            async_bio_work);
+       struct bio_list bios = BIO_EMPTY_LIST;
+       struct bio *bio;
+
+       /* as long as there are pending bios, @blkg can't go away */
+       spin_lock_bh(&blkg->async_bio_lock);
+       bio_list_merge(&bios, &blkg->async_bios);
+       bio_list_init(&blkg->async_bios);
+       spin_unlock_bh(&blkg->async_bio_lock);
+
+       while ((bio = bio_list_pop(&bios)))
+               submit_bio(bio);
+}
+
 /**
  * blkg_alloc - allocate a blkg
  * @blkcg: block cgroup the new blkg is associated with
@@ -132,12 +153,18 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
        if (!blkg)
                return NULL;
 
+       if (percpu_ref_init(&blkg->refcnt, blkg_release, 0, gfp_mask))
+               goto err_free;
+
        if (blkg_rwstat_init(&blkg->stat_bytes, gfp_mask) ||
            blkg_rwstat_init(&blkg->stat_ios, gfp_mask))
                goto err_free;
 
        blkg->q = q;
        INIT_LIST_HEAD(&blkg->q_node);
+       spin_lock_init(&blkg->async_bio_lock);
+       bio_list_init(&blkg->async_bios);
+       INIT_WORK(&blkg->async_bio_work, blkg_async_bio_workfn);
        blkg->blkcg = blkcg;
 
        for (i = 0; i < BLKCG_MAX_POLS; i++) {
@@ -244,11 +271,6 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
                blkg_get(blkg->parent);
        }
 
-       ret = percpu_ref_init(&blkg->refcnt, blkg_release, 0,
-                             GFP_NOWAIT | __GFP_NOWARN);
-       if (ret)
-               goto err_cancel_ref;
-
        /* invoke per-policy init */
        for (i = 0; i < BLKCG_MAX_POLS; i++) {
                struct blkcg_policy *pol = blkcg_policy[i];
@@ -281,8 +303,6 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
        blkg_put(blkg);
        return ERR_PTR(ret);
 
-err_cancel_ref:
-       percpu_ref_exit(&blkg->refcnt);
 err_put_congested:
        wb_congested_put(wb_congested);
 err_put_css:
@@ -549,7 +569,7 @@ EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
  * Print @rwstat to @sf for the device assocaited with @pd.
  */
 u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
-                        const struct blkg_rwstat *rwstat)
+                        const struct blkg_rwstat_sample *rwstat)
 {
        static const char *rwstr[] = {
                [BLKG_RWSTAT_READ]      = "Read",
@@ -567,30 +587,16 @@ u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
 
        for (i = 0; i < BLKG_RWSTAT_NR; i++)
                seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
-                          (unsigned long long)atomic64_read(&rwstat->aux_cnt[i]));
+                          rwstat->cnt[i]);
 
-       v = atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_READ]) +
-               atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_WRITE]) +
-               atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_DISCARD]);
-       seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
+       v = rwstat->cnt[BLKG_RWSTAT_READ] +
+               rwstat->cnt[BLKG_RWSTAT_WRITE] +
+               rwstat->cnt[BLKG_RWSTAT_DISCARD];
+       seq_printf(sf, "%s Total %llu\n", dname, v);
        return v;
 }
 EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);
 
-/**
- * blkg_prfill_stat - prfill callback for blkg_stat
- * @sf: seq_file to print to
- * @pd: policy private data of interest
- * @off: offset to the blkg_stat in @pd
- *
- * prfill callback for printing a blkg_stat.
- */
-u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
-{
-       return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
-}
-EXPORT_SYMBOL_GPL(blkg_prfill_stat);
-
 /**
  * blkg_prfill_rwstat - prfill callback for blkg_rwstat
  * @sf: seq_file to print to
@@ -602,8 +608,9 @@ EXPORT_SYMBOL_GPL(blkg_prfill_stat);
 u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
                       int off)
 {
-       struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
+       struct blkg_rwstat_sample rwstat = { };
 
+       blkg_rwstat_read((void *)pd + off, &rwstat);
        return __blkg_prfill_rwstat(sf, pd, &rwstat);
 }
 EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
@@ -611,8 +618,9 @@ EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
 static u64 blkg_prfill_rwstat_field(struct seq_file *sf,
                                    struct blkg_policy_data *pd, int off)
 {
-       struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd->blkg + off);
+       struct blkg_rwstat_sample rwstat = { };
 
+       blkg_rwstat_read((void *)pd->blkg + off, &rwstat);
        return __blkg_prfill_rwstat(sf, pd, &rwstat);
 }
 
@@ -654,8 +662,9 @@ static u64 blkg_prfill_rwstat_field_recursive(struct seq_file *sf,
                                              struct blkg_policy_data *pd,
                                              int off)
 {
-       struct blkg_rwstat rwstat = blkg_rwstat_recursive_sum(pd->blkg,
-                                                             NULL, off);
+       struct blkg_rwstat_sample rwstat;
+
+       blkg_rwstat_recursive_sum(pd->blkg, NULL, off, &rwstat);
        return __blkg_prfill_rwstat(sf, pd, &rwstat);
 }
 
@@ -689,53 +698,12 @@ int blkg_print_stat_ios_recursive(struct seq_file *sf, void *v)
 }
 EXPORT_SYMBOL_GPL(blkg_print_stat_ios_recursive);
 
-/**
- * blkg_stat_recursive_sum - collect hierarchical blkg_stat
- * @blkg: blkg of interest
- * @pol: blkcg_policy which contains the blkg_stat
- * @off: offset to the blkg_stat in blkg_policy_data or @blkg
- *
- * Collect the blkg_stat specified by @blkg, @pol and @off and all its
- * online descendants and their aux counts.  The caller must be holding the
- * queue lock for online tests.
- *
- * If @pol is NULL, blkg_stat is at @off bytes into @blkg; otherwise, it is
- * at @off bytes into @blkg's blkg_policy_data of the policy.
- */
-u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg,
-                           struct blkcg_policy *pol, int off)
-{
-       struct blkcg_gq *pos_blkg;
-       struct cgroup_subsys_state *pos_css;
-       u64 sum = 0;
-
-       lockdep_assert_held(&blkg->q->queue_lock);
-
-       rcu_read_lock();
-       blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
-               struct blkg_stat *stat;
-
-               if (!pos_blkg->online)
-                       continue;
-
-               if (pol)
-                       stat = (void *)blkg_to_pd(pos_blkg, pol) + off;
-               else
-                       stat = (void *)blkg + off;
-
-               sum += blkg_stat_read(stat) + atomic64_read(&stat->aux_cnt);
-       }
-       rcu_read_unlock();
-
-       return sum;
-}
-EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum);
-
 /**
  * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
  * @blkg: blkg of interest
  * @pol: blkcg_policy which contains the blkg_rwstat
  * @off: offset to the blkg_rwstat in blkg_policy_data or @blkg
+ * @sum: blkg_rwstat_sample structure containing the results
  *
  * Collect the blkg_rwstat specified by @blkg, @pol and @off and all its
  * online descendants and their aux counts.  The caller must be holding the
@@ -744,13 +712,12 @@ EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum);
  * If @pol is NULL, blkg_rwstat is at @off bytes into @blkg; otherwise, it
  * is at @off bytes into @blkg's blkg_policy_data of the policy.
  */
-struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg,
-                                            struct blkcg_policy *pol, int off)
+void blkg_rwstat_recursive_sum(struct blkcg_gq *blkg, struct blkcg_policy *pol,
+               int off, struct blkg_rwstat_sample *sum)
 {
        struct blkcg_gq *pos_blkg;
        struct cgroup_subsys_state *pos_css;
-       struct blkg_rwstat sum = { };
-       int i;
+       unsigned int i;
 
        lockdep_assert_held(&blkg->q->queue_lock);
 
@@ -767,13 +734,9 @@ struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg,
                        rwstat = (void *)pos_blkg + off;
 
                for (i = 0; i < BLKG_RWSTAT_NR; i++)
-                       atomic64_add(atomic64_read(&rwstat->aux_cnt[i]) +
-                               percpu_counter_sum_positive(&rwstat->cpu_cnt[i]),
-                               &sum.aux_cnt[i]);
+                       sum->cnt[i] = blkg_rwstat_read_counter(rwstat, i);
        }
        rcu_read_unlock();
-
-       return sum;
 }
 EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);
 
@@ -939,7 +902,7 @@ static int blkcg_print_stat(struct seq_file *sf, void *v)
        hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
                const char *dname;
                char *buf;
-               struct blkg_rwstat rwstat;
+               struct blkg_rwstat_sample rwstat;
                u64 rbytes, wbytes, rios, wios, dbytes, dios;
                size_t size = seq_get_buf(sf, &buf), off = 0;
                int i;
@@ -959,17 +922,17 @@ static int blkcg_print_stat(struct seq_file *sf, void *v)
 
                spin_lock_irq(&blkg->q->queue_lock);
 
-               rwstat = blkg_rwstat_recursive_sum(blkg, NULL,
-                                       offsetof(struct blkcg_gq, stat_bytes));
-               rbytes = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_READ]);
-               wbytes = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_WRITE]);
-               dbytes = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_DISCARD]);
+               blkg_rwstat_recursive_sum(blkg, NULL,
+                               offsetof(struct blkcg_gq, stat_bytes), &rwstat);
+               rbytes = rwstat.cnt[BLKG_RWSTAT_READ];
+               wbytes = rwstat.cnt[BLKG_RWSTAT_WRITE];
+               dbytes = rwstat.cnt[BLKG_RWSTAT_DISCARD];
 
-               rwstat = blkg_rwstat_recursive_sum(blkg, NULL,
-                                       offsetof(struct blkcg_gq, stat_ios));
-               rios = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_READ]);
-               wios = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_WRITE]);
-               dios = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_DISCARD]);
+               blkg_rwstat_recursive_sum(blkg, NULL,
+                                       offsetof(struct blkcg_gq, stat_ios), &rwstat);
+               rios = rwstat.cnt[BLKG_RWSTAT_READ];
+               wios = rwstat.cnt[BLKG_RWSTAT_WRITE];
+               dios = rwstat.cnt[BLKG_RWSTAT_DISCARD];
 
                spin_unlock_irq(&blkg->q->queue_lock);
 
@@ -1006,8 +969,12 @@ static int blkcg_print_stat(struct seq_file *sf, void *v)
                }
 next:
                if (has_stats) {
-                       off += scnprintf(buf+off, size-off, "\n");
-                       seq_commit(sf, off);
+                       if (off < size - 1) {
+                               off += scnprintf(buf+off, size-off, "\n");
+                               seq_commit(sf, off);
+                       } else {
+                               seq_commit(sf, -1);
+                       }
                }
        }
 
@@ -1391,7 +1358,8 @@ int blkcg_activate_policy(struct request_queue *q,
 
        spin_lock_irq(&q->queue_lock);
 
-       list_for_each_entry(blkg, &q->blkg_list, q_node) {
+       /* blkg_list is pushed at the head, reverse walk to init parents first */
+       list_for_each_entry_reverse(blkg, &q->blkg_list, q_node) {
                struct blkg_policy_data *pd;
 
                if (blkg->pd[pol->plid])
@@ -1583,6 +1551,25 @@ void blkcg_policy_unregister(struct blkcg_policy *pol)
 }
 EXPORT_SYMBOL_GPL(blkcg_policy_unregister);
 
+bool __blkcg_punt_bio_submit(struct bio *bio)
+{
+       struct blkcg_gq *blkg = bio->bi_blkg;
+
+       /* consume the flag first */
+       bio->bi_opf &= ~REQ_CGROUP_PUNT;
+
+       /* never bounce for the root cgroup */
+       if (!blkg->parent)
+               return false;
+
+       spin_lock_bh(&blkg->async_bio_lock);
+       bio_list_add(&blkg->async_bios, bio);
+       spin_unlock_bh(&blkg->async_bio_lock);
+
+       queue_work(blkcg_punt_bio_wq, &blkg->async_bio_work);
+       return true;
+}
+
 /*
  * Scale the accumulated delay based on how long it has been since we updated
  * the delay.  We only call this when we are adding delay, in case it's been a
@@ -1644,6 +1631,7 @@ static void blkcg_scale_delay(struct blkcg_gq *blkg, u64 now)
  */
 static void blkcg_maybe_throttle_blkg(struct blkcg_gq *blkg, bool use_memdelay)
 {
+       unsigned long pflags;
        u64 now = ktime_to_ns(ktime_get());
        u64 exp;
        u64 delay_nsec = 0;
@@ -1670,11 +1658,8 @@ static void blkcg_maybe_throttle_blkg(struct blkcg_gq *blkg, bool use_memdelay)
         */
        delay_nsec = min_t(u64, delay_nsec, 250 * NSEC_PER_MSEC);
 
-       /*
-        * TODO: the use_memdelay flag is going to be for the upcoming psi stuff
-        * that hasn't landed upstream yet.  Once that stuff is in place we need
-        * to do a psi_memstall_enter/leave if memdelay is set.
-        */
+       if (use_memdelay)
+               psi_memstall_enter(&pflags);
 
        exp = ktime_add_ns(now, delay_nsec);
        tok = io_schedule_prepare();
@@ -1684,6 +1669,9 @@ static void blkcg_maybe_throttle_blkg(struct blkcg_gq *blkg, bool use_memdelay)
                        break;
        } while (!fatal_signal_pending(current));
        io_schedule_finish(tok);
+
+       if (use_memdelay)
+               psi_memstall_leave(&pflags);
 }
 
 /**
@@ -1783,5 +1771,16 @@ void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta)
        atomic64_add(delta, &blkg->delay_nsec);
 }
 
+static int __init blkcg_init(void)
+{
+       blkcg_punt_bio_wq = alloc_workqueue("blkcg_punt_bio",
+                                           WQ_MEM_RECLAIM | WQ_FREEZABLE |
+                                           WQ_UNBOUND | WQ_SYSFS, 0);
+       if (!blkcg_punt_bio_wq)
+               return -ENOMEM;
+       return 0;
+}
+subsys_initcall(blkcg_init);
+
 module_param(blkcg_debug_stats, bool, 0644);
 MODULE_PARM_DESC(blkcg_debug_stats, "True if you want debug stats, false if not");