blk-throttle.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*
  2. * Interface for controlling IO bandwidth on a request queue
  3. *
  4. * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/bio.h>
  10. #include <linux/blktrace_api.h>
  11. #include "blk-cgroup.h"
  12. #include "blk.h"
  13. /* Max dispatch from a group in 1 round */
  14. static int throtl_grp_quantum = 8;
  15. /* Total max dispatch from all groups in one round */
  16. static int throtl_quantum = 32;
  17. /* Throttling is performed over 100ms slice and after that slice is renewed */
  18. static unsigned long throtl_slice = HZ/10; /* 100 ms */
  19. static struct blkio_policy_type blkio_policy_throtl;
  20. /* A workqueue to queue throttle related work */
  21. static struct workqueue_struct *kthrotld_workqueue;
  22. static void throtl_schedule_delayed_work(struct throtl_data *td,
  23. unsigned long delay);
  24. struct throtl_rb_root {
  25. struct rb_root rb;
  26. struct rb_node *left;
  27. unsigned int count;
  28. unsigned long min_disptime;
  29. };
  30. #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
  31. .count = 0, .min_disptime = 0}
  32. #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
  33. /* Per-cpu group stats */
  34. struct tg_stats_cpu {
  35. /* total bytes transferred */
  36. struct blkg_rwstat service_bytes;
  37. /* total IOs serviced, post merge */
  38. struct blkg_rwstat serviced;
  39. };
  40. struct throtl_grp {
  41. /* active throtl group service_tree member */
  42. struct rb_node rb_node;
  43. /*
  44. * Dispatch time in jiffies. This is the estimated time when group
  45. * will unthrottle and is ready to dispatch more bio. It is used as
  46. * key to sort active groups in service tree.
  47. */
  48. unsigned long disptime;
  49. unsigned int flags;
  50. /* Two lists for READ and WRITE */
  51. struct bio_list bio_lists[2];
  52. /* Number of queued bios on READ and WRITE lists */
  53. unsigned int nr_queued[2];
  54. /* bytes per second rate limits */
  55. uint64_t bps[2];
  56. /* IOPS limits */
  57. unsigned int iops[2];
  58. /* Number of bytes disptached in current slice */
  59. uint64_t bytes_disp[2];
  60. /* Number of bio's dispatched in current slice */
  61. unsigned int io_disp[2];
  62. /* When did we start a new slice */
  63. unsigned long slice_start[2];
  64. unsigned long slice_end[2];
  65. /* Some throttle limits got updated for the group */
  66. int limits_changed;
  67. /* Per cpu stats pointer */
  68. struct tg_stats_cpu __percpu *stats_cpu;
  69. /* List of tgs waiting for per cpu stats memory to be allocated */
  70. struct list_head stats_alloc_node;
  71. };
  72. struct throtl_data
  73. {
  74. /* service tree for active throtl groups */
  75. struct throtl_rb_root tg_service_tree;
  76. struct throtl_grp *root_tg;
  77. struct request_queue *queue;
  78. /* Total Number of queued bios on READ and WRITE lists */
  79. unsigned int nr_queued[2];
  80. /*
  81. * number of total undestroyed groups
  82. */
  83. unsigned int nr_undestroyed_grps;
  84. /* Work for dispatching throttled bios */
  85. struct delayed_work throtl_work;
  86. int limits_changed;
  87. };
  88. /* list and work item to allocate percpu group stats */
  89. static DEFINE_SPINLOCK(tg_stats_alloc_lock);
  90. static LIST_HEAD(tg_stats_alloc_list);
  91. static void tg_stats_alloc_fn(struct work_struct *);
  92. static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
  93. static inline struct throtl_grp *blkg_to_tg(struct blkio_group *blkg)
  94. {
  95. return blkg_to_pdata(blkg, &blkio_policy_throtl);
  96. }
  97. static inline struct blkio_group *tg_to_blkg(struct throtl_grp *tg)
  98. {
  99. return pdata_to_blkg(tg);
  100. }
  101. enum tg_state_flags {
  102. THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
  103. };
  104. #define THROTL_TG_FNS(name) \
  105. static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
  106. { \
  107. (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
  108. } \
  109. static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
  110. { \
  111. (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
  112. } \
  113. static inline int throtl_tg_##name(const struct throtl_grp *tg) \
  114. { \
  115. return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
  116. }
  117. THROTL_TG_FNS(on_rr);
  118. #define throtl_log_tg(td, tg, fmt, args...) \
  119. blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
  120. blkg_path(tg_to_blkg(tg)), ##args); \
  121. #define throtl_log(td, fmt, args...) \
  122. blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
  123. static inline unsigned int total_nr_queued(struct throtl_data *td)
  124. {
  125. return td->nr_queued[0] + td->nr_queued[1];
  126. }
  127. /*
  128. * Worker for allocating per cpu stat for tgs. This is scheduled on the
  129. * system_nrt_wq once there are some groups on the alloc_list waiting for
  130. * allocation.
  131. */
  132. static void tg_stats_alloc_fn(struct work_struct *work)
  133. {
  134. static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
  135. struct delayed_work *dwork = to_delayed_work(work);
  136. bool empty = false;
  137. alloc_stats:
  138. if (!stats_cpu) {
  139. stats_cpu = alloc_percpu(struct tg_stats_cpu);
  140. if (!stats_cpu) {
  141. /* allocation failed, try again after some time */
  142. queue_delayed_work(system_nrt_wq, dwork,
  143. msecs_to_jiffies(10));
  144. return;
  145. }
  146. }
  147. spin_lock_irq(&tg_stats_alloc_lock);
  148. if (!list_empty(&tg_stats_alloc_list)) {
  149. struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
  150. struct throtl_grp,
  151. stats_alloc_node);
  152. swap(tg->stats_cpu, stats_cpu);
  153. list_del_init(&tg->stats_alloc_node);
  154. }
  155. empty = list_empty(&tg_stats_alloc_list);
  156. spin_unlock_irq(&tg_stats_alloc_lock);
  157. if (!empty)
  158. goto alloc_stats;
  159. }
  160. static void throtl_init_blkio_group(struct blkio_group *blkg)
  161. {
  162. struct throtl_grp *tg = blkg_to_tg(blkg);
  163. RB_CLEAR_NODE(&tg->rb_node);
  164. bio_list_init(&tg->bio_lists[0]);
  165. bio_list_init(&tg->bio_lists[1]);
  166. tg->limits_changed = false;
  167. tg->bps[READ] = -1;
  168. tg->bps[WRITE] = -1;
  169. tg->iops[READ] = -1;
  170. tg->iops[WRITE] = -1;
  171. /*
  172. * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
  173. * but percpu allocator can't be called from IO path. Queue tg on
  174. * tg_stats_alloc_list and allocate from work item.
  175. */
  176. spin_lock(&tg_stats_alloc_lock);
  177. list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
  178. queue_delayed_work(system_nrt_wq, &tg_stats_alloc_work, 0);
  179. spin_unlock(&tg_stats_alloc_lock);
  180. }
  181. static void throtl_exit_blkio_group(struct blkio_group *blkg)
  182. {
  183. struct throtl_grp *tg = blkg_to_tg(blkg);
  184. spin_lock(&tg_stats_alloc_lock);
  185. list_del_init(&tg->stats_alloc_node);
  186. spin_unlock(&tg_stats_alloc_lock);
  187. free_percpu(tg->stats_cpu);
  188. }
  189. static void throtl_reset_group_stats(struct blkio_group *blkg)
  190. {
  191. struct throtl_grp *tg = blkg_to_tg(blkg);
  192. int cpu;
  193. if (tg->stats_cpu == NULL)
  194. return;
  195. for_each_possible_cpu(cpu) {
  196. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  197. blkg_rwstat_reset(&sc->service_bytes);
  198. blkg_rwstat_reset(&sc->serviced);
  199. }
  200. }
  201. static struct
  202. throtl_grp *throtl_lookup_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
  203. {
  204. /*
  205. * This is the common case when there are no blkio cgroups.
  206. * Avoid lookup in this case
  207. */
  208. if (blkcg == &blkio_root_cgroup)
  209. return td->root_tg;
  210. return blkg_to_tg(blkg_lookup(blkcg, td->queue));
  211. }
  212. static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
  213. struct blkio_cgroup *blkcg)
  214. {
  215. struct request_queue *q = td->queue;
  216. struct throtl_grp *tg = NULL;
  217. /*
  218. * This is the common case when there are no blkio cgroups.
  219. * Avoid lookup in this case
  220. */
  221. if (blkcg == &blkio_root_cgroup) {
  222. tg = td->root_tg;
  223. } else {
  224. struct blkio_group *blkg;
  225. blkg = blkg_lookup_create(blkcg, q, false);
  226. /* if %NULL and @q is alive, fall back to root_tg */
  227. if (!IS_ERR(blkg))
  228. tg = blkg_to_tg(blkg);
  229. else if (!blk_queue_dead(q))
  230. tg = td->root_tg;
  231. }
  232. return tg;
  233. }
  234. static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
  235. {
  236. /* Service tree is empty */
  237. if (!root->count)
  238. return NULL;
  239. if (!root->left)
  240. root->left = rb_first(&root->rb);
  241. if (root->left)
  242. return rb_entry_tg(root->left);
  243. return NULL;
  244. }
  245. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  246. {
  247. rb_erase(n, root);
  248. RB_CLEAR_NODE(n);
  249. }
  250. static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
  251. {
  252. if (root->left == n)
  253. root->left = NULL;
  254. rb_erase_init(n, &root->rb);
  255. --root->count;
  256. }
  257. static void update_min_dispatch_time(struct throtl_rb_root *st)
  258. {
  259. struct throtl_grp *tg;
  260. tg = throtl_rb_first(st);
  261. if (!tg)
  262. return;
  263. st->min_disptime = tg->disptime;
  264. }
  265. static void
  266. tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
  267. {
  268. struct rb_node **node = &st->rb.rb_node;
  269. struct rb_node *parent = NULL;
  270. struct throtl_grp *__tg;
  271. unsigned long key = tg->disptime;
  272. int left = 1;
  273. while (*node != NULL) {
  274. parent = *node;
  275. __tg = rb_entry_tg(parent);
  276. if (time_before(key, __tg->disptime))
  277. node = &parent->rb_left;
  278. else {
  279. node = &parent->rb_right;
  280. left = 0;
  281. }
  282. }
  283. if (left)
  284. st->left = &tg->rb_node;
  285. rb_link_node(&tg->rb_node, parent, node);
  286. rb_insert_color(&tg->rb_node, &st->rb);
  287. }
  288. static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  289. {
  290. struct throtl_rb_root *st = &td->tg_service_tree;
  291. tg_service_tree_add(st, tg);
  292. throtl_mark_tg_on_rr(tg);
  293. st->count++;
  294. }
  295. static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  296. {
  297. if (!throtl_tg_on_rr(tg))
  298. __throtl_enqueue_tg(td, tg);
  299. }
  300. static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  301. {
  302. throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
  303. throtl_clear_tg_on_rr(tg);
  304. }
  305. static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  306. {
  307. if (throtl_tg_on_rr(tg))
  308. __throtl_dequeue_tg(td, tg);
  309. }
  310. static void throtl_schedule_next_dispatch(struct throtl_data *td)
  311. {
  312. struct throtl_rb_root *st = &td->tg_service_tree;
  313. /*
  314. * If there are more bios pending, schedule more work.
  315. */
  316. if (!total_nr_queued(td))
  317. return;
  318. BUG_ON(!st->count);
  319. update_min_dispatch_time(st);
  320. if (time_before_eq(st->min_disptime, jiffies))
  321. throtl_schedule_delayed_work(td, 0);
  322. else
  323. throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
  324. }
  325. static inline void
  326. throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  327. {
  328. tg->bytes_disp[rw] = 0;
  329. tg->io_disp[rw] = 0;
  330. tg->slice_start[rw] = jiffies;
  331. tg->slice_end[rw] = jiffies + throtl_slice;
  332. throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
  333. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  334. tg->slice_end[rw], jiffies);
  335. }
  336. static inline void throtl_set_slice_end(struct throtl_data *td,
  337. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  338. {
  339. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  340. }
  341. static inline void throtl_extend_slice(struct throtl_data *td,
  342. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  343. {
  344. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  345. throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  346. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  347. tg->slice_end[rw], jiffies);
  348. }
  349. /* Determine if previously allocated or extended slice is complete or not */
  350. static bool
  351. throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  352. {
  353. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  354. return 0;
  355. return 1;
  356. }
  357. /* Trim the used slices and adjust slice start accordingly */
  358. static inline void
  359. throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  360. {
  361. unsigned long nr_slices, time_elapsed, io_trim;
  362. u64 bytes_trim, tmp;
  363. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  364. /*
  365. * If bps are unlimited (-1), then time slice don't get
  366. * renewed. Don't try to trim the slice if slice is used. A new
  367. * slice will start when appropriate.
  368. */
  369. if (throtl_slice_used(td, tg, rw))
  370. return;
  371. /*
  372. * A bio has been dispatched. Also adjust slice_end. It might happen
  373. * that initially cgroup limit was very low resulting in high
  374. * slice_end, but later limit was bumped up and bio was dispached
  375. * sooner, then we need to reduce slice_end. A high bogus slice_end
  376. * is bad because it does not allow new slice to start.
  377. */
  378. throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
  379. time_elapsed = jiffies - tg->slice_start[rw];
  380. nr_slices = time_elapsed / throtl_slice;
  381. if (!nr_slices)
  382. return;
  383. tmp = tg->bps[rw] * throtl_slice * nr_slices;
  384. do_div(tmp, HZ);
  385. bytes_trim = tmp;
  386. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  387. if (!bytes_trim && !io_trim)
  388. return;
  389. if (tg->bytes_disp[rw] >= bytes_trim)
  390. tg->bytes_disp[rw] -= bytes_trim;
  391. else
  392. tg->bytes_disp[rw] = 0;
  393. if (tg->io_disp[rw] >= io_trim)
  394. tg->io_disp[rw] -= io_trim;
  395. else
  396. tg->io_disp[rw] = 0;
  397. tg->slice_start[rw] += nr_slices * throtl_slice;
  398. throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
  399. " start=%lu end=%lu jiffies=%lu",
  400. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  401. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  402. }
  403. static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
  404. struct bio *bio, unsigned long *wait)
  405. {
  406. bool rw = bio_data_dir(bio);
  407. unsigned int io_allowed;
  408. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  409. u64 tmp;
  410. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  411. /* Slice has just started. Consider one slice interval */
  412. if (!jiffy_elapsed)
  413. jiffy_elapsed_rnd = throtl_slice;
  414. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  415. /*
  416. * jiffy_elapsed_rnd should not be a big value as minimum iops can be
  417. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  418. * will allow dispatch after 1 second and after that slice should
  419. * have been trimmed.
  420. */
  421. tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
  422. do_div(tmp, HZ);
  423. if (tmp > UINT_MAX)
  424. io_allowed = UINT_MAX;
  425. else
  426. io_allowed = tmp;
  427. if (tg->io_disp[rw] + 1 <= io_allowed) {
  428. if (wait)
  429. *wait = 0;
  430. return 1;
  431. }
  432. /* Calc approx time to dispatch */
  433. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  434. if (jiffy_wait > jiffy_elapsed)
  435. jiffy_wait = jiffy_wait - jiffy_elapsed;
  436. else
  437. jiffy_wait = 1;
  438. if (wait)
  439. *wait = jiffy_wait;
  440. return 0;
  441. }
  442. static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
  443. struct bio *bio, unsigned long *wait)
  444. {
  445. bool rw = bio_data_dir(bio);
  446. u64 bytes_allowed, extra_bytes, tmp;
  447. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  448. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  449. /* Slice has just started. Consider one slice interval */
  450. if (!jiffy_elapsed)
  451. jiffy_elapsed_rnd = throtl_slice;
  452. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  453. tmp = tg->bps[rw] * jiffy_elapsed_rnd;
  454. do_div(tmp, HZ);
  455. bytes_allowed = tmp;
  456. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  457. if (wait)
  458. *wait = 0;
  459. return 1;
  460. }
  461. /* Calc approx time to dispatch */
  462. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  463. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  464. if (!jiffy_wait)
  465. jiffy_wait = 1;
  466. /*
  467. * This wait time is without taking into consideration the rounding
  468. * up we did. Add that time also.
  469. */
  470. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  471. if (wait)
  472. *wait = jiffy_wait;
  473. return 0;
  474. }
  475. static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
  476. if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
  477. return 1;
  478. return 0;
  479. }
  480. /*
  481. * Returns whether one can dispatch a bio or not. Also returns approx number
  482. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  483. */
  484. static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
  485. struct bio *bio, unsigned long *wait)
  486. {
  487. bool rw = bio_data_dir(bio);
  488. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  489. /*
  490. * Currently whole state machine of group depends on first bio
  491. * queued in the group bio list. So one should not be calling
  492. * this function with a different bio if there are other bios
  493. * queued.
  494. */
  495. BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
  496. /* If tg->bps = -1, then BW is unlimited */
  497. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  498. if (wait)
  499. *wait = 0;
  500. return 1;
  501. }
  502. /*
  503. * If previous slice expired, start a new one otherwise renew/extend
  504. * existing slice to make sure it is at least throtl_slice interval
  505. * long since now.
  506. */
  507. if (throtl_slice_used(td, tg, rw))
  508. throtl_start_new_slice(td, tg, rw);
  509. else {
  510. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  511. throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
  512. }
  513. if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
  514. && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
  515. if (wait)
  516. *wait = 0;
  517. return 1;
  518. }
  519. max_wait = max(bps_wait, iops_wait);
  520. if (wait)
  521. *wait = max_wait;
  522. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  523. throtl_extend_slice(td, tg, rw, jiffies + max_wait);
  524. return 0;
  525. }
  526. static void throtl_update_dispatch_stats(struct blkio_group *blkg, u64 bytes,
  527. int rw)
  528. {
  529. struct throtl_grp *tg = blkg_to_tg(blkg);
  530. struct tg_stats_cpu *stats_cpu;
  531. unsigned long flags;
  532. /* If per cpu stats are not allocated yet, don't do any accounting. */
  533. if (tg->stats_cpu == NULL)
  534. return;
  535. /*
  536. * Disabling interrupts to provide mutual exclusion between two
  537. * writes on same cpu. It probably is not needed for 64bit. Not
  538. * optimizing that case yet.
  539. */
  540. local_irq_save(flags);
  541. stats_cpu = this_cpu_ptr(tg->stats_cpu);
  542. blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
  543. blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
  544. local_irq_restore(flags);
  545. }
  546. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  547. {
  548. bool rw = bio_data_dir(bio);
  549. /* Charge the bio to the group */
  550. tg->bytes_disp[rw] += bio->bi_size;
  551. tg->io_disp[rw]++;
  552. throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
  553. }
  554. static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
  555. struct bio *bio)
  556. {
  557. bool rw = bio_data_dir(bio);
  558. bio_list_add(&tg->bio_lists[rw], bio);
  559. /* Take a bio reference on tg */
  560. blkg_get(tg_to_blkg(tg));
  561. tg->nr_queued[rw]++;
  562. td->nr_queued[rw]++;
  563. throtl_enqueue_tg(td, tg);
  564. }
  565. static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
  566. {
  567. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  568. struct bio *bio;
  569. if ((bio = bio_list_peek(&tg->bio_lists[READ])))
  570. tg_may_dispatch(td, tg, bio, &read_wait);
  571. if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  572. tg_may_dispatch(td, tg, bio, &write_wait);
  573. min_wait = min(read_wait, write_wait);
  574. disptime = jiffies + min_wait;
  575. /* Update dispatch time */
  576. throtl_dequeue_tg(td, tg);
  577. tg->disptime = disptime;
  578. throtl_enqueue_tg(td, tg);
  579. }
  580. static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
  581. bool rw, struct bio_list *bl)
  582. {
  583. struct bio *bio;
  584. bio = bio_list_pop(&tg->bio_lists[rw]);
  585. tg->nr_queued[rw]--;
  586. /* Drop bio reference on blkg */
  587. blkg_put(tg_to_blkg(tg));
  588. BUG_ON(td->nr_queued[rw] <= 0);
  589. td->nr_queued[rw]--;
  590. throtl_charge_bio(tg, bio);
  591. bio_list_add(bl, bio);
  592. bio->bi_rw |= REQ_THROTTLED;
  593. throtl_trim_slice(td, tg, rw);
  594. }
  595. static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
  596. struct bio_list *bl)
  597. {
  598. unsigned int nr_reads = 0, nr_writes = 0;
  599. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  600. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  601. struct bio *bio;
  602. /* Try to dispatch 75% READS and 25% WRITES */
  603. while ((bio = bio_list_peek(&tg->bio_lists[READ]))
  604. && tg_may_dispatch(td, tg, bio, NULL)) {
  605. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  606. nr_reads++;
  607. if (nr_reads >= max_nr_reads)
  608. break;
  609. }
  610. while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
  611. && tg_may_dispatch(td, tg, bio, NULL)) {
  612. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  613. nr_writes++;
  614. if (nr_writes >= max_nr_writes)
  615. break;
  616. }
  617. return nr_reads + nr_writes;
  618. }
  619. static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
  620. {
  621. unsigned int nr_disp = 0;
  622. struct throtl_grp *tg;
  623. struct throtl_rb_root *st = &td->tg_service_tree;
  624. while (1) {
  625. tg = throtl_rb_first(st);
  626. if (!tg)
  627. break;
  628. if (time_before(jiffies, tg->disptime))
  629. break;
  630. throtl_dequeue_tg(td, tg);
  631. nr_disp += throtl_dispatch_tg(td, tg, bl);
  632. if (tg->nr_queued[0] || tg->nr_queued[1]) {
  633. tg_update_disptime(td, tg);
  634. throtl_enqueue_tg(td, tg);
  635. }
  636. if (nr_disp >= throtl_quantum)
  637. break;
  638. }
  639. return nr_disp;
  640. }
  641. static void throtl_process_limit_change(struct throtl_data *td)
  642. {
  643. struct request_queue *q = td->queue;
  644. struct blkio_group *blkg, *n;
  645. if (!td->limits_changed)
  646. return;
  647. xchg(&td->limits_changed, false);
  648. throtl_log(td, "limits changed");
  649. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  650. struct throtl_grp *tg = blkg_to_tg(blkg);
  651. if (!tg->limits_changed)
  652. continue;
  653. if (!xchg(&tg->limits_changed, false))
  654. continue;
  655. throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
  656. " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
  657. tg->iops[READ], tg->iops[WRITE]);
  658. /*
  659. * Restart the slices for both READ and WRITES. It
  660. * might happen that a group's limit are dropped
  661. * suddenly and we don't want to account recently
  662. * dispatched IO with new low rate
  663. */
  664. throtl_start_new_slice(td, tg, 0);
  665. throtl_start_new_slice(td, tg, 1);
  666. if (throtl_tg_on_rr(tg))
  667. tg_update_disptime(td, tg);
  668. }
  669. }
  670. /* Dispatch throttled bios. Should be called without queue lock held. */
  671. static int throtl_dispatch(struct request_queue *q)
  672. {
  673. struct throtl_data *td = q->td;
  674. unsigned int nr_disp = 0;
  675. struct bio_list bio_list_on_stack;
  676. struct bio *bio;
  677. struct blk_plug plug;
  678. spin_lock_irq(q->queue_lock);
  679. throtl_process_limit_change(td);
  680. if (!total_nr_queued(td))
  681. goto out;
  682. bio_list_init(&bio_list_on_stack);
  683. throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
  684. total_nr_queued(td), td->nr_queued[READ],
  685. td->nr_queued[WRITE]);
  686. nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
  687. if (nr_disp)
  688. throtl_log(td, "bios disp=%u", nr_disp);
  689. throtl_schedule_next_dispatch(td);
  690. out:
  691. spin_unlock_irq(q->queue_lock);
  692. /*
  693. * If we dispatched some requests, unplug the queue to make sure
  694. * immediate dispatch
  695. */
  696. if (nr_disp) {
  697. blk_start_plug(&plug);
  698. while((bio = bio_list_pop(&bio_list_on_stack)))
  699. generic_make_request(bio);
  700. blk_finish_plug(&plug);
  701. }
  702. return nr_disp;
  703. }
  704. void blk_throtl_work(struct work_struct *work)
  705. {
  706. struct throtl_data *td = container_of(work, struct throtl_data,
  707. throtl_work.work);
  708. struct request_queue *q = td->queue;
  709. throtl_dispatch(q);
  710. }
  711. /* Call with queue lock held */
  712. static void
  713. throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
  714. {
  715. struct delayed_work *dwork = &td->throtl_work;
  716. /* schedule work if limits changed even if no bio is queued */
  717. if (total_nr_queued(td) || td->limits_changed) {
  718. /*
  719. * We might have a work scheduled to be executed in future.
  720. * Cancel that and schedule a new one.
  721. */
  722. __cancel_delayed_work(dwork);
  723. queue_delayed_work(kthrotld_workqueue, dwork, delay);
  724. throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
  725. delay, jiffies);
  726. }
  727. }
  728. static u64 tg_prfill_cpu_rwstat(struct seq_file *sf, void *pdata, int off)
  729. {
  730. struct throtl_grp *tg = pdata;
  731. struct blkg_rwstat rwstat = { }, tmp;
  732. int i, cpu;
  733. for_each_possible_cpu(cpu) {
  734. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  735. tmp = blkg_rwstat_read((void *)sc + off);
  736. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  737. rwstat.cnt[i] += tmp.cnt[i];
  738. }
  739. return __blkg_prfill_rwstat(sf, pdata, &rwstat);
  740. }
  741. static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
  742. struct seq_file *sf)
  743. {
  744. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  745. blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkio_policy_throtl,
  746. cft->private, true);
  747. return 0;
  748. }
  749. static u64 tg_prfill_conf_u64(struct seq_file *sf, void *pdata, int off)
  750. {
  751. u64 v = *(u64 *)(pdata + off);
  752. if (v == -1)
  753. return 0;
  754. return __blkg_prfill_u64(sf, pdata, v);
  755. }
  756. static u64 tg_prfill_conf_uint(struct seq_file *sf, void *pdata, int off)
  757. {
  758. unsigned int v = *(unsigned int *)(pdata + off);
  759. if (v == -1)
  760. return 0;
  761. return __blkg_prfill_u64(sf, pdata, v);
  762. }
  763. static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  764. struct seq_file *sf)
  765. {
  766. blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp), tg_prfill_conf_u64,
  767. &blkio_policy_throtl, cft->private, false);
  768. return 0;
  769. }
  770. static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  771. struct seq_file *sf)
  772. {
  773. blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp), tg_prfill_conf_uint,
  774. &blkio_policy_throtl, cft->private, false);
  775. return 0;
  776. }
  777. static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
  778. bool is_u64)
  779. {
  780. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  781. struct blkg_conf_ctx ctx;
  782. struct throtl_grp *tg;
  783. int ret;
  784. ret = blkg_conf_prep(blkcg, buf, &ctx);
  785. if (ret)
  786. return ret;
  787. ret = -EINVAL;
  788. tg = blkg_to_tg(ctx.blkg);
  789. if (tg) {
  790. struct throtl_data *td = ctx.blkg->q->td;
  791. if (!ctx.v)
  792. ctx.v = -1;
  793. if (is_u64)
  794. *(u64 *)((void *)tg + cft->private) = ctx.v;
  795. else
  796. *(unsigned int *)((void *)tg + cft->private) = ctx.v;
  797. /* XXX: we don't need the following deferred processing */
  798. xchg(&tg->limits_changed, true);
  799. xchg(&td->limits_changed, true);
  800. throtl_schedule_delayed_work(td, 0);
  801. ret = 0;
  802. }
  803. blkg_conf_finish(&ctx);
  804. return ret;
  805. }
  806. static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  807. const char *buf)
  808. {
  809. return tg_set_conf(cgrp, cft, buf, true);
  810. }
  811. static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  812. const char *buf)
  813. {
  814. return tg_set_conf(cgrp, cft, buf, false);
  815. }
  816. static struct cftype throtl_files[] = {
  817. {
  818. .name = "throttle.read_bps_device",
  819. .private = offsetof(struct throtl_grp, bps[READ]),
  820. .read_seq_string = tg_print_conf_u64,
  821. .write_string = tg_set_conf_u64,
  822. .max_write_len = 256,
  823. },
  824. {
  825. .name = "throttle.write_bps_device",
  826. .private = offsetof(struct throtl_grp, bps[WRITE]),
  827. .read_seq_string = tg_print_conf_u64,
  828. .write_string = tg_set_conf_u64,
  829. .max_write_len = 256,
  830. },
  831. {
  832. .name = "throttle.read_iops_device",
  833. .private = offsetof(struct throtl_grp, iops[READ]),
  834. .read_seq_string = tg_print_conf_uint,
  835. .write_string = tg_set_conf_uint,
  836. .max_write_len = 256,
  837. },
  838. {
  839. .name = "throttle.write_iops_device",
  840. .private = offsetof(struct throtl_grp, iops[WRITE]),
  841. .read_seq_string = tg_print_conf_uint,
  842. .write_string = tg_set_conf_uint,
  843. .max_write_len = 256,
  844. },
  845. {
  846. .name = "throttle.io_service_bytes",
  847. .private = offsetof(struct tg_stats_cpu, service_bytes),
  848. .read_seq_string = tg_print_cpu_rwstat,
  849. },
  850. {
  851. .name = "throttle.io_serviced",
  852. .private = offsetof(struct tg_stats_cpu, serviced),
  853. .read_seq_string = tg_print_cpu_rwstat,
  854. },
  855. { } /* terminate */
  856. };
  857. static void throtl_shutdown_wq(struct request_queue *q)
  858. {
  859. struct throtl_data *td = q->td;
  860. cancel_delayed_work_sync(&td->throtl_work);
  861. }
  862. static struct blkio_policy_type blkio_policy_throtl = {
  863. .ops = {
  864. .blkio_init_group_fn = throtl_init_blkio_group,
  865. .blkio_exit_group_fn = throtl_exit_blkio_group,
  866. .blkio_reset_group_stats_fn = throtl_reset_group_stats,
  867. },
  868. .pdata_size = sizeof(struct throtl_grp),
  869. .cftypes = throtl_files,
  870. };
  871. bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
  872. {
  873. struct throtl_data *td = q->td;
  874. struct throtl_grp *tg;
  875. bool rw = bio_data_dir(bio), update_disptime = true;
  876. struct blkio_cgroup *blkcg;
  877. bool throttled = false;
  878. if (bio->bi_rw & REQ_THROTTLED) {
  879. bio->bi_rw &= ~REQ_THROTTLED;
  880. goto out;
  881. }
  882. /* bio_associate_current() needs ioc, try creating */
  883. create_io_context(GFP_ATOMIC, q->node);
  884. /*
  885. * A throtl_grp pointer retrieved under rcu can be used to access
  886. * basic fields like stats and io rates. If a group has no rules,
  887. * just update the dispatch stats in lockless manner and return.
  888. */
  889. rcu_read_lock();
  890. blkcg = bio_blkio_cgroup(bio);
  891. tg = throtl_lookup_tg(td, blkcg);
  892. if (tg) {
  893. if (tg_no_rule_group(tg, rw)) {
  894. throtl_update_dispatch_stats(tg_to_blkg(tg),
  895. bio->bi_size, bio->bi_rw);
  896. goto out_unlock_rcu;
  897. }
  898. }
  899. /*
  900. * Either group has not been allocated yet or it is not an unlimited
  901. * IO group
  902. */
  903. spin_lock_irq(q->queue_lock);
  904. tg = throtl_lookup_create_tg(td, blkcg);
  905. if (unlikely(!tg))
  906. goto out_unlock;
  907. if (tg->nr_queued[rw]) {
  908. /*
  909. * There is already another bio queued in same dir. No
  910. * need to update dispatch time.
  911. */
  912. update_disptime = false;
  913. goto queue_bio;
  914. }
  915. /* Bio is with-in rate limit of group */
  916. if (tg_may_dispatch(td, tg, bio, NULL)) {
  917. throtl_charge_bio(tg, bio);
  918. /*
  919. * We need to trim slice even when bios are not being queued
  920. * otherwise it might happen that a bio is not queued for
  921. * a long time and slice keeps on extending and trim is not
  922. * called for a long time. Now if limits are reduced suddenly
  923. * we take into account all the IO dispatched so far at new
  924. * low rate and * newly queued IO gets a really long dispatch
  925. * time.
  926. *
  927. * So keep on trimming slice even if bio is not queued.
  928. */
  929. throtl_trim_slice(td, tg, rw);
  930. goto out_unlock;
  931. }
  932. queue_bio:
  933. throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
  934. " iodisp=%u iops=%u queued=%d/%d",
  935. rw == READ ? 'R' : 'W',
  936. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  937. tg->io_disp[rw], tg->iops[rw],
  938. tg->nr_queued[READ], tg->nr_queued[WRITE]);
  939. bio_associate_current(bio);
  940. throtl_add_bio_tg(q->td, tg, bio);
  941. throttled = true;
  942. if (update_disptime) {
  943. tg_update_disptime(td, tg);
  944. throtl_schedule_next_dispatch(td);
  945. }
  946. out_unlock:
  947. spin_unlock_irq(q->queue_lock);
  948. out_unlock_rcu:
  949. rcu_read_unlock();
  950. out:
  951. return throttled;
  952. }
  953. /**
  954. * blk_throtl_drain - drain throttled bios
  955. * @q: request_queue to drain throttled bios for
  956. *
  957. * Dispatch all currently throttled bios on @q through ->make_request_fn().
  958. */
  959. void blk_throtl_drain(struct request_queue *q)
  960. __releases(q->queue_lock) __acquires(q->queue_lock)
  961. {
  962. struct throtl_data *td = q->td;
  963. struct throtl_rb_root *st = &td->tg_service_tree;
  964. struct throtl_grp *tg;
  965. struct bio_list bl;
  966. struct bio *bio;
  967. WARN_ON_ONCE(!queue_is_locked(q));
  968. bio_list_init(&bl);
  969. while ((tg = throtl_rb_first(st))) {
  970. throtl_dequeue_tg(td, tg);
  971. while ((bio = bio_list_peek(&tg->bio_lists[READ])))
  972. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
  973. while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  974. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
  975. }
  976. spin_unlock_irq(q->queue_lock);
  977. while ((bio = bio_list_pop(&bl)))
  978. generic_make_request(bio);
  979. spin_lock_irq(q->queue_lock);
  980. }
  981. int blk_throtl_init(struct request_queue *q)
  982. {
  983. struct throtl_data *td;
  984. struct blkio_group *blkg;
  985. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  986. if (!td)
  987. return -ENOMEM;
  988. td->tg_service_tree = THROTL_RB_ROOT;
  989. td->limits_changed = false;
  990. INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
  991. q->td = td;
  992. td->queue = q;
  993. /* alloc and init root group. */
  994. rcu_read_lock();
  995. spin_lock_irq(q->queue_lock);
  996. blkg = blkg_lookup_create(&blkio_root_cgroup, q, true);
  997. if (!IS_ERR(blkg))
  998. td->root_tg = blkg_to_tg(blkg);
  999. spin_unlock_irq(q->queue_lock);
  1000. rcu_read_unlock();
  1001. if (!td->root_tg) {
  1002. kfree(td);
  1003. return -ENOMEM;
  1004. }
  1005. return 0;
  1006. }
  1007. void blk_throtl_exit(struct request_queue *q)
  1008. {
  1009. BUG_ON(!q->td);
  1010. throtl_shutdown_wq(q);
  1011. kfree(q->td);
  1012. }
  1013. static int __init throtl_init(void)
  1014. {
  1015. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  1016. if (!kthrotld_workqueue)
  1017. panic("Failed to create kthrotld\n");
  1018. return blkio_policy_register(&blkio_policy_throtl);
  1019. }
  1020. module_init(throtl_init);