tizen: packaging: Add baselibs.conf to provide 64-bit kernel & modules for 32-bit...
[platform/kernel/linux-rpi.git] / block / blk-throttle.c
index 7397ff1..16f5766 100644 (file)
@@ -697,11 +697,47 @@ static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
        return true;
 }
 
+static unsigned int calculate_io_allowed(u32 iops_limit,
+                                        unsigned long jiffy_elapsed)
+{
+       unsigned int io_allowed;
+       u64 tmp;
+
+       /*
+        * jiffy_elapsed should not be a big value as minimum iops can be
+        * 1 then at max jiffy elapsed should be equivalent of 1 second as we
+        * will allow dispatch after 1 second and after that slice should
+        * have been trimmed.
+        */
+
+       tmp = (u64)iops_limit * jiffy_elapsed;
+       do_div(tmp, HZ);
+
+       if (tmp > UINT_MAX)
+               io_allowed = UINT_MAX;
+       else
+               io_allowed = tmp;
+
+       return io_allowed;
+}
+
+static u64 calculate_bytes_allowed(u64 bps_limit, unsigned long jiffy_elapsed)
+{
+       /*
+        * Can result be wider than 64 bits?
+        * We check against 62, not 64, due to ilog2 truncation.
+        */
+       if (ilog2(bps_limit) + ilog2(jiffy_elapsed) - ilog2(HZ) > 62)
+               return U64_MAX;
+       return mul_u64_u64_div_u64(bps_limit, (u64)jiffy_elapsed, (u64)HZ);
+}
+
 /* Trim the used slices and adjust slice start accordingly */
 static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
 {
-       unsigned long nr_slices, time_elapsed, io_trim;
-       u64 bytes_trim, tmp;
+       unsigned long time_elapsed;
+       long long bytes_trim;
+       int io_trim;
 
        BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
 
@@ -723,67 +759,38 @@ static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
 
        throtl_set_slice_end(tg, rw, jiffies + tg->td->throtl_slice);
 
-       time_elapsed = jiffies - tg->slice_start[rw];
-
-       nr_slices = time_elapsed / tg->td->throtl_slice;
-
-       if (!nr_slices)
+       time_elapsed = rounddown(jiffies - tg->slice_start[rw],
+                                tg->td->throtl_slice);
+       if (!time_elapsed)
                return;
-       tmp = tg_bps_limit(tg, rw) * tg->td->throtl_slice * nr_slices;
-       do_div(tmp, HZ);
-       bytes_trim = tmp;
-
-       io_trim = (tg_iops_limit(tg, rw) * tg->td->throtl_slice * nr_slices) /
-               HZ;
 
-       if (!bytes_trim && !io_trim)
+       bytes_trim = calculate_bytes_allowed(tg_bps_limit(tg, rw),
+                                            time_elapsed) +
+                    tg->carryover_bytes[rw];
+       io_trim = calculate_io_allowed(tg_iops_limit(tg, rw), time_elapsed) +
+                 tg->carryover_ios[rw];
+       if (bytes_trim <= 0 && io_trim <= 0)
                return;
 
-       if (tg->bytes_disp[rw] >= bytes_trim)
+       tg->carryover_bytes[rw] = 0;
+       if ((long long)tg->bytes_disp[rw] >= bytes_trim)
                tg->bytes_disp[rw] -= bytes_trim;
        else
                tg->bytes_disp[rw] = 0;
 
-       if (tg->io_disp[rw] >= io_trim)
+       tg->carryover_ios[rw] = 0;
+       if ((int)tg->io_disp[rw] >= io_trim)
                tg->io_disp[rw] -= io_trim;
        else
                tg->io_disp[rw] = 0;
 
-       tg->slice_start[rw] += nr_slices * tg->td->throtl_slice;
+       tg->slice_start[rw] += time_elapsed;
 
        throtl_log(&tg->service_queue,
-                  "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
-                  rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
-                  tg->slice_start[rw], tg->slice_end[rw], jiffies);
-}
-
-static unsigned int calculate_io_allowed(u32 iops_limit,
-                                        unsigned long jiffy_elapsed)
-{
-       unsigned int io_allowed;
-       u64 tmp;
-
-       /*
-        * jiffy_elapsed should not be a big value as minimum iops can be
-        * 1 then at max jiffy elapsed should be equivalent of 1 second as we
-        * will allow dispatch after 1 second and after that slice should
-        * have been trimmed.
-        */
-
-       tmp = (u64)iops_limit * jiffy_elapsed;
-       do_div(tmp, HZ);
-
-       if (tmp > UINT_MAX)
-               io_allowed = UINT_MAX;
-       else
-               io_allowed = tmp;
-
-       return io_allowed;
-}
-
-static u64 calculate_bytes_allowed(u64 bps_limit, unsigned long jiffy_elapsed)
-{
-       return mul_u64_u64_div_u64(bps_limit, (u64)jiffy_elapsed, (u64)HZ);
+                  "[%c] trim slice nr=%lu bytes=%lld io=%d start=%lu end=%lu jiffies=%lu",
+                  rw == READ ? 'R' : 'W', time_elapsed / tg->td->throtl_slice,
+                  bytes_trim, io_trim, tg->slice_start[rw], tg->slice_end[rw],
+                  jiffies);
 }
 
 static void __tg_update_carryover(struct throtl_grp *tg, bool rw)
@@ -816,7 +823,7 @@ static void tg_update_carryover(struct throtl_grp *tg)
                __tg_update_carryover(tg, WRITE);
 
        /* see comments in struct throtl_grp for meaning of these fields. */
-       throtl_log(&tg->service_queue, "%s: %llu %llu %u %u\n", __func__,
+       throtl_log(&tg->service_queue, "%s: %lld %lld %d %d\n", __func__,
                   tg->carryover_bytes[READ], tg->carryover_bytes[WRITE],
                   tg->carryover_ios[READ], tg->carryover_ios[WRITE]);
 }
@@ -825,7 +832,7 @@ static unsigned long tg_within_iops_limit(struct throtl_grp *tg, struct bio *bio
                                 u32 iops_limit)
 {
        bool rw = bio_data_dir(bio);
-       unsigned int io_allowed;
+       int io_allowed;
        unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
 
        if (iops_limit == UINT_MAX) {
@@ -838,9 +845,8 @@ static unsigned long tg_within_iops_limit(struct throtl_grp *tg, struct bio *bio
        jiffy_elapsed_rnd = roundup(jiffy_elapsed + 1, tg->td->throtl_slice);
        io_allowed = calculate_io_allowed(iops_limit, jiffy_elapsed_rnd) +
                     tg->carryover_ios[rw];
-       if (tg->io_disp[rw] + 1 <= io_allowed) {
+       if (io_allowed > 0 && tg->io_disp[rw] + 1 <= io_allowed)
                return 0;
-       }
 
        /* Calc approx time to dispatch */
        jiffy_wait = jiffy_elapsed_rnd - jiffy_elapsed;
@@ -851,7 +857,8 @@ static unsigned long tg_within_bps_limit(struct throtl_grp *tg, struct bio *bio,
                                u64 bps_limit)
 {
        bool rw = bio_data_dir(bio);
-       u64 bytes_allowed, extra_bytes;
+       long long bytes_allowed;
+       u64 extra_bytes;
        unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
        unsigned int bio_size = throtl_bio_data_size(bio);
 
@@ -869,9 +876,8 @@ static unsigned long tg_within_bps_limit(struct throtl_grp *tg, struct bio *bio,
        jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice);
        bytes_allowed = calculate_bytes_allowed(bps_limit, jiffy_elapsed_rnd) +
                        tg->carryover_bytes[rw];
-       if (tg->bytes_disp[rw] + bio_size <= bytes_allowed) {
+       if (bytes_allowed > 0 && tg->bytes_disp[rw] + bio_size <= bytes_allowed)
                return 0;
-       }
 
        /* Calc approx time to dispatch */
        extra_bytes = tg->bytes_disp[rw] + bio_size - bytes_allowed;
@@ -1314,6 +1320,7 @@ static void tg_conf_updated(struct throtl_grp *tg, bool global)
                   tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
                   tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
 
+       rcu_read_lock();
        /*
         * Update has_rules[] flags for the updated tg's subtree.  A tg is
         * considered to have rules if either the tg itself or any of its
@@ -1341,6 +1348,7 @@ static void tg_conf_updated(struct throtl_grp *tg, bool global)
                this_tg->latency_target = max(this_tg->latency_target,
                                parent_tg->latency_target);
        }
+       rcu_read_unlock();
 
        /*
         * We're already holding queue_lock and know @tg is valid.  Let's