blk-throttle.c 31 KB

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