bool use_debt, ioc_locked;
        unsigned long flags;
 
-       /* bypass IOs if disabled or for root cgroup */
-       if (!ioc->enabled || !iocg->level)
+       /* bypass IOs if disabled, still initializing, or for root cgroup */
+       if (!ioc->enabled || !iocg || !iocg->level)
                return;
 
        /* calculate the absolute vtime cost */
                           struct bio *bio)
 {
        struct ioc_gq *iocg = blkg_to_iocg(bio->bi_blkg);
-       struct ioc *ioc = iocg->ioc;
+       struct ioc *ioc = rqos_to_ioc(rqos);
        sector_t bio_end = bio_end_sector(bio);
        struct ioc_now now;
        u64 vtime, abs_cost, cost;
        unsigned long flags;
 
-       /* bypass if disabled or for root cgroup */
-       if (!ioc->enabled || !iocg->level)
+       /* bypass if disabled, still initializing, or for root cgroup */
+       if (!ioc->enabled || !iocg || !iocg->level)
                return;
 
        abs_cost = calc_vtime_cost(bio, iocg, true);
        ioc_refresh_params(ioc, true);
        spin_unlock_irq(&ioc->lock);
 
+       /*
+        * rqos must be added before activation to allow iocg_pd_init() to
+        * lookup the ioc from q. This means that the rqos methods may get
+        * called before policy activation completion, can't assume that the
+        * target bio has an iocg associated and need to test for NULL iocg.
+        */
        rq_qos_add(q, rqos);
        ret = blkcg_activate_policy(q, &blkcg_policy_iocost);
        if (ret) {