blk-throttle.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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 request_queue *queue;
  77. /* Total Number of queued bios on READ and WRITE lists */
  78. unsigned int nr_queued[2];
  79. /*
  80. * number of total undestroyed groups
  81. */
  82. unsigned int nr_undestroyed_grps;
  83. /* Work for dispatching throttled bios */
  84. struct delayed_work throtl_work;
  85. int limits_changed;
  86. };
  87. /* list and work item to allocate percpu group stats */
  88. static DEFINE_SPINLOCK(tg_stats_alloc_lock);
  89. static LIST_HEAD(tg_stats_alloc_list);
  90. static void tg_stats_alloc_fn(struct work_struct *);
  91. static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
  92. static inline struct throtl_grp *blkg_to_tg(struct blkio_group *blkg)
  93. {
  94. return blkg_to_pdata(blkg, &blkio_policy_throtl);
  95. }
  96. static inline struct blkio_group *tg_to_blkg(struct throtl_grp *tg)
  97. {
  98. return pdata_to_blkg(tg);
  99. }
  100. static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
  101. {
  102. return blkg_to_tg(td->queue->root_blkg);
  103. }
  104. enum tg_state_flags {
  105. THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
  106. };
  107. #define THROTL_TG_FNS(name) \
  108. static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
  109. { \
  110. (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
  111. } \
  112. static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
  113. { \
  114. (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
  115. } \
  116. static inline int throtl_tg_##name(const struct throtl_grp *tg) \
  117. { \
  118. return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
  119. }
  120. THROTL_TG_FNS(on_rr);
  121. #define throtl_log_tg(td, tg, fmt, args...) \
  122. blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
  123. blkg_path(tg_to_blkg(tg)), ##args); \
  124. #define throtl_log(td, fmt, args...) \
  125. blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
  126. static inline unsigned int total_nr_queued(struct throtl_data *td)
  127. {
  128. return td->nr_queued[0] + td->nr_queued[1];
  129. }
  130. /*
  131. * Worker for allocating per cpu stat for tgs. This is scheduled on the
  132. * system_nrt_wq once there are some groups on the alloc_list waiting for
  133. * allocation.
  134. */
  135. static void tg_stats_alloc_fn(struct work_struct *work)
  136. {
  137. static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
  138. struct delayed_work *dwork = to_delayed_work(work);
  139. bool empty = false;
  140. alloc_stats:
  141. if (!stats_cpu) {
  142. stats_cpu = alloc_percpu(struct tg_stats_cpu);
  143. if (!stats_cpu) {
  144. /* allocation failed, try again after some time */
  145. queue_delayed_work(system_nrt_wq, dwork,
  146. msecs_to_jiffies(10));
  147. return;
  148. }
  149. }
  150. spin_lock_irq(&tg_stats_alloc_lock);
  151. if (!list_empty(&tg_stats_alloc_list)) {
  152. struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
  153. struct throtl_grp,
  154. stats_alloc_node);
  155. swap(tg->stats_cpu, stats_cpu);
  156. list_del_init(&tg->stats_alloc_node);
  157. }
  158. empty = list_empty(&tg_stats_alloc_list);
  159. spin_unlock_irq(&tg_stats_alloc_lock);
  160. if (!empty)
  161. goto alloc_stats;
  162. }
  163. static void throtl_init_blkio_group(struct blkio_group *blkg)
  164. {
  165. struct throtl_grp *tg = blkg_to_tg(blkg);
  166. RB_CLEAR_NODE(&tg->rb_node);
  167. bio_list_init(&tg->bio_lists[0]);
  168. bio_list_init(&tg->bio_lists[1]);
  169. tg->limits_changed = false;
  170. tg->bps[READ] = -1;
  171. tg->bps[WRITE] = -1;
  172. tg->iops[READ] = -1;
  173. tg->iops[WRITE] = -1;
  174. /*
  175. * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
  176. * but percpu allocator can't be called from IO path. Queue tg on
  177. * tg_stats_alloc_list and allocate from work item.
  178. */
  179. spin_lock(&tg_stats_alloc_lock);
  180. list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
  181. queue_delayed_work(system_nrt_wq, &tg_stats_alloc_work, 0);
  182. spin_unlock(&tg_stats_alloc_lock);
  183. }
  184. static void throtl_exit_blkio_group(struct blkio_group *blkg)
  185. {
  186. struct throtl_grp *tg = blkg_to_tg(blkg);
  187. spin_lock(&tg_stats_alloc_lock);
  188. list_del_init(&tg->stats_alloc_node);
  189. spin_unlock(&tg_stats_alloc_lock);
  190. free_percpu(tg->stats_cpu);
  191. }
  192. static void throtl_reset_group_stats(struct blkio_group *blkg)
  193. {
  194. struct throtl_grp *tg = blkg_to_tg(blkg);
  195. int cpu;
  196. if (tg->stats_cpu == NULL)
  197. return;
  198. for_each_possible_cpu(cpu) {
  199. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  200. blkg_rwstat_reset(&sc->service_bytes);
  201. blkg_rwstat_reset(&sc->serviced);
  202. }
  203. }
  204. static struct
  205. throtl_grp *throtl_lookup_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
  206. {
  207. /*
  208. * This is the common case when there are no blkio cgroups.
  209. * Avoid lookup in this case
  210. */
  211. if (blkcg == &blkio_root_cgroup)
  212. return td_root_tg(td);
  213. return blkg_to_tg(blkg_lookup(blkcg, td->queue));
  214. }
  215. static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
  216. struct blkio_cgroup *blkcg)
  217. {
  218. struct request_queue *q = td->queue;
  219. struct throtl_grp *tg = NULL;
  220. /*
  221. * This is the common case when there are no blkio cgroups.
  222. * Avoid lookup in this case
  223. */
  224. if (blkcg == &blkio_root_cgroup) {
  225. tg = td_root_tg(td);
  226. } else {
  227. struct blkio_group *blkg;
  228. blkg = blkg_lookup_create(blkcg, q, false);
  229. /* if %NULL and @q is alive, fall back to root_tg */
  230. if (!IS_ERR(blkg))
  231. tg = blkg_to_tg(blkg);
  232. else if (!blk_queue_dead(q))
  233. tg = td_root_tg(td);
  234. }
  235. return tg;
  236. }
  237. static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
  238. {
  239. /* Service tree is empty */
  240. if (!root->count)
  241. return NULL;
  242. if (!root->left)
  243. root->left = rb_first(&root->rb);
  244. if (root->left)
  245. return rb_entry_tg(root->left);
  246. return NULL;
  247. }
  248. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  249. {
  250. rb_erase(n, root);
  251. RB_CLEAR_NODE(n);
  252. }
  253. static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
  254. {
  255. if (root->left == n)
  256. root->left = NULL;
  257. rb_erase_init(n, &root->rb);
  258. --root->count;
  259. }
  260. static void update_min_dispatch_time(struct throtl_rb_root *st)
  261. {
  262. struct throtl_grp *tg;
  263. tg = throtl_rb_first(st);
  264. if (!tg)
  265. return;
  266. st->min_disptime = tg->disptime;
  267. }
  268. static void
  269. tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
  270. {
  271. struct rb_node **node = &st->rb.rb_node;
  272. struct rb_node *parent = NULL;
  273. struct throtl_grp *__tg;
  274. unsigned long key = tg->disptime;
  275. int left = 1;
  276. while (*node != NULL) {
  277. parent = *node;
  278. __tg = rb_entry_tg(parent);
  279. if (time_before(key, __tg->disptime))
  280. node = &parent->rb_left;
  281. else {
  282. node = &parent->rb_right;
  283. left = 0;
  284. }
  285. }
  286. if (left)
  287. st->left = &tg->rb_node;
  288. rb_link_node(&tg->rb_node, parent, node);
  289. rb_insert_color(&tg->rb_node, &st->rb);
  290. }
  291. static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  292. {
  293. struct throtl_rb_root *st = &td->tg_service_tree;
  294. tg_service_tree_add(st, tg);
  295. throtl_mark_tg_on_rr(tg);
  296. st->count++;
  297. }
  298. static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  299. {
  300. if (!throtl_tg_on_rr(tg))
  301. __throtl_enqueue_tg(td, tg);
  302. }
  303. static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  304. {
  305. throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
  306. throtl_clear_tg_on_rr(tg);
  307. }
  308. static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  309. {
  310. if (throtl_tg_on_rr(tg))
  311. __throtl_dequeue_tg(td, tg);
  312. }
  313. static void throtl_schedule_next_dispatch(struct throtl_data *td)
  314. {
  315. struct throtl_rb_root *st = &td->tg_service_tree;
  316. /*
  317. * If there are more bios pending, schedule more work.
  318. */
  319. if (!total_nr_queued(td))
  320. return;
  321. BUG_ON(!st->count);
  322. update_min_dispatch_time(st);
  323. if (time_before_eq(st->min_disptime, jiffies))
  324. throtl_schedule_delayed_work(td, 0);
  325. else
  326. throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
  327. }
  328. static inline void
  329. throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  330. {
  331. tg->bytes_disp[rw] = 0;
  332. tg->io_disp[rw] = 0;
  333. tg->slice_start[rw] = jiffies;
  334. tg->slice_end[rw] = jiffies + throtl_slice;
  335. throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
  336. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  337. tg->slice_end[rw], jiffies);
  338. }
  339. static inline void throtl_set_slice_end(struct throtl_data *td,
  340. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  341. {
  342. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  343. }
  344. static inline void throtl_extend_slice(struct throtl_data *td,
  345. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  346. {
  347. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  348. throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  349. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  350. tg->slice_end[rw], jiffies);
  351. }
  352. /* Determine if previously allocated or extended slice is complete or not */
  353. static bool
  354. throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  355. {
  356. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  357. return 0;
  358. return 1;
  359. }
  360. /* Trim the used slices and adjust slice start accordingly */
  361. static inline void
  362. throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  363. {
  364. unsigned long nr_slices, time_elapsed, io_trim;
  365. u64 bytes_trim, tmp;
  366. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  367. /*
  368. * If bps are unlimited (-1), then time slice don't get
  369. * renewed. Don't try to trim the slice if slice is used. A new
  370. * slice will start when appropriate.
  371. */
  372. if (throtl_slice_used(td, tg, rw))
  373. return;
  374. /*
  375. * A bio has been dispatched. Also adjust slice_end. It might happen
  376. * that initially cgroup limit was very low resulting in high
  377. * slice_end, but later limit was bumped up and bio was dispached
  378. * sooner, then we need to reduce slice_end. A high bogus slice_end
  379. * is bad because it does not allow new slice to start.
  380. */
  381. throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
  382. time_elapsed = jiffies - tg->slice_start[rw];
  383. nr_slices = time_elapsed / throtl_slice;
  384. if (!nr_slices)
  385. return;
  386. tmp = tg->bps[rw] * throtl_slice * nr_slices;
  387. do_div(tmp, HZ);
  388. bytes_trim = tmp;
  389. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  390. if (!bytes_trim && !io_trim)
  391. return;
  392. if (tg->bytes_disp[rw] >= bytes_trim)
  393. tg->bytes_disp[rw] -= bytes_trim;
  394. else
  395. tg->bytes_disp[rw] = 0;
  396. if (tg->io_disp[rw] >= io_trim)
  397. tg->io_disp[rw] -= io_trim;
  398. else
  399. tg->io_disp[rw] = 0;
  400. tg->slice_start[rw] += nr_slices * throtl_slice;
  401. throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
  402. " start=%lu end=%lu jiffies=%lu",
  403. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  404. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  405. }
  406. static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
  407. struct bio *bio, unsigned long *wait)
  408. {
  409. bool rw = bio_data_dir(bio);
  410. unsigned int io_allowed;
  411. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  412. u64 tmp;
  413. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  414. /* Slice has just started. Consider one slice interval */
  415. if (!jiffy_elapsed)
  416. jiffy_elapsed_rnd = throtl_slice;
  417. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  418. /*
  419. * jiffy_elapsed_rnd should not be a big value as minimum iops can be
  420. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  421. * will allow dispatch after 1 second and after that slice should
  422. * have been trimmed.
  423. */
  424. tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
  425. do_div(tmp, HZ);
  426. if (tmp > UINT_MAX)
  427. io_allowed = UINT_MAX;
  428. else
  429. io_allowed = tmp;
  430. if (tg->io_disp[rw] + 1 <= io_allowed) {
  431. if (wait)
  432. *wait = 0;
  433. return 1;
  434. }
  435. /* Calc approx time to dispatch */
  436. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  437. if (jiffy_wait > jiffy_elapsed)
  438. jiffy_wait = jiffy_wait - jiffy_elapsed;
  439. else
  440. jiffy_wait = 1;
  441. if (wait)
  442. *wait = jiffy_wait;
  443. return 0;
  444. }
  445. static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
  446. struct bio *bio, unsigned long *wait)
  447. {
  448. bool rw = bio_data_dir(bio);
  449. u64 bytes_allowed, extra_bytes, tmp;
  450. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  451. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  452. /* Slice has just started. Consider one slice interval */
  453. if (!jiffy_elapsed)
  454. jiffy_elapsed_rnd = throtl_slice;
  455. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  456. tmp = tg->bps[rw] * jiffy_elapsed_rnd;
  457. do_div(tmp, HZ);
  458. bytes_allowed = tmp;
  459. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  460. if (wait)
  461. *wait = 0;
  462. return 1;
  463. }
  464. /* Calc approx time to dispatch */
  465. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  466. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  467. if (!jiffy_wait)
  468. jiffy_wait = 1;
  469. /*
  470. * This wait time is without taking into consideration the rounding
  471. * up we did. Add that time also.
  472. */
  473. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  474. if (wait)
  475. *wait = jiffy_wait;
  476. return 0;
  477. }
  478. static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
  479. if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
  480. return 1;
  481. return 0;
  482. }
  483. /*
  484. * Returns whether one can dispatch a bio or not. Also returns approx number
  485. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  486. */
  487. static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
  488. struct bio *bio, unsigned long *wait)
  489. {
  490. bool rw = bio_data_dir(bio);
  491. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  492. /*
  493. * Currently whole state machine of group depends on first bio
  494. * queued in the group bio list. So one should not be calling
  495. * this function with a different bio if there are other bios
  496. * queued.
  497. */
  498. BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
  499. /* If tg->bps = -1, then BW is unlimited */
  500. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  501. if (wait)
  502. *wait = 0;
  503. return 1;
  504. }
  505. /*
  506. * If previous slice expired, start a new one otherwise renew/extend
  507. * existing slice to make sure it is at least throtl_slice interval
  508. * long since now.
  509. */
  510. if (throtl_slice_used(td, tg, rw))
  511. throtl_start_new_slice(td, tg, rw);
  512. else {
  513. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  514. throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
  515. }
  516. if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
  517. && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
  518. if (wait)
  519. *wait = 0;
  520. return 1;
  521. }
  522. max_wait = max(bps_wait, iops_wait);
  523. if (wait)
  524. *wait = max_wait;
  525. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  526. throtl_extend_slice(td, tg, rw, jiffies + max_wait);
  527. return 0;
  528. }
  529. static void throtl_update_dispatch_stats(struct blkio_group *blkg, u64 bytes,
  530. int rw)
  531. {
  532. struct throtl_grp *tg = blkg_to_tg(blkg);
  533. struct tg_stats_cpu *stats_cpu;
  534. unsigned long flags;
  535. /* If per cpu stats are not allocated yet, don't do any accounting. */
  536. if (tg->stats_cpu == NULL)
  537. return;
  538. /*
  539. * Disabling interrupts to provide mutual exclusion between two
  540. * writes on same cpu. It probably is not needed for 64bit. Not
  541. * optimizing that case yet.
  542. */
  543. local_irq_save(flags);
  544. stats_cpu = this_cpu_ptr(tg->stats_cpu);
  545. blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
  546. blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
  547. local_irq_restore(flags);
  548. }
  549. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  550. {
  551. bool rw = bio_data_dir(bio);
  552. /* Charge the bio to the group */
  553. tg->bytes_disp[rw] += bio->bi_size;
  554. tg->io_disp[rw]++;
  555. throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
  556. }
  557. static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
  558. struct bio *bio)
  559. {
  560. bool rw = bio_data_dir(bio);
  561. bio_list_add(&tg->bio_lists[rw], bio);
  562. /* Take a bio reference on tg */
  563. blkg_get(tg_to_blkg(tg));
  564. tg->nr_queued[rw]++;
  565. td->nr_queued[rw]++;
  566. throtl_enqueue_tg(td, tg);
  567. }
  568. static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
  569. {
  570. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  571. struct bio *bio;
  572. if ((bio = bio_list_peek(&tg->bio_lists[READ])))
  573. tg_may_dispatch(td, tg, bio, &read_wait);
  574. if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  575. tg_may_dispatch(td, tg, bio, &write_wait);
  576. min_wait = min(read_wait, write_wait);
  577. disptime = jiffies + min_wait;
  578. /* Update dispatch time */
  579. throtl_dequeue_tg(td, tg);
  580. tg->disptime = disptime;
  581. throtl_enqueue_tg(td, tg);
  582. }
  583. static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
  584. bool rw, struct bio_list *bl)
  585. {
  586. struct bio *bio;
  587. bio = bio_list_pop(&tg->bio_lists[rw]);
  588. tg->nr_queued[rw]--;
  589. /* Drop bio reference on blkg */
  590. blkg_put(tg_to_blkg(tg));
  591. BUG_ON(td->nr_queued[rw] <= 0);
  592. td->nr_queued[rw]--;
  593. throtl_charge_bio(tg, bio);
  594. bio_list_add(bl, bio);
  595. bio->bi_rw |= REQ_THROTTLED;
  596. throtl_trim_slice(td, tg, rw);
  597. }
  598. static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
  599. struct bio_list *bl)
  600. {
  601. unsigned int nr_reads = 0, nr_writes = 0;
  602. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  603. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  604. struct bio *bio;
  605. /* Try to dispatch 75% READS and 25% WRITES */
  606. while ((bio = bio_list_peek(&tg->bio_lists[READ]))
  607. && tg_may_dispatch(td, tg, bio, NULL)) {
  608. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  609. nr_reads++;
  610. if (nr_reads >= max_nr_reads)
  611. break;
  612. }
  613. while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
  614. && tg_may_dispatch(td, tg, bio, NULL)) {
  615. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  616. nr_writes++;
  617. if (nr_writes >= max_nr_writes)
  618. break;
  619. }
  620. return nr_reads + nr_writes;
  621. }
  622. static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
  623. {
  624. unsigned int nr_disp = 0;
  625. struct throtl_grp *tg;
  626. struct throtl_rb_root *st = &td->tg_service_tree;
  627. while (1) {
  628. tg = throtl_rb_first(st);
  629. if (!tg)
  630. break;
  631. if (time_before(jiffies, tg->disptime))
  632. break;
  633. throtl_dequeue_tg(td, tg);
  634. nr_disp += throtl_dispatch_tg(td, tg, bl);
  635. if (tg->nr_queued[0] || tg->nr_queued[1]) {
  636. tg_update_disptime(td, tg);
  637. throtl_enqueue_tg(td, tg);
  638. }
  639. if (nr_disp >= throtl_quantum)
  640. break;
  641. }
  642. return nr_disp;
  643. }
  644. static void throtl_process_limit_change(struct throtl_data *td)
  645. {
  646. struct request_queue *q = td->queue;
  647. struct blkio_group *blkg, *n;
  648. if (!td->limits_changed)
  649. return;
  650. xchg(&td->limits_changed, false);
  651. throtl_log(td, "limits changed");
  652. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  653. struct throtl_grp *tg = blkg_to_tg(blkg);
  654. if (!tg->limits_changed)
  655. continue;
  656. if (!xchg(&tg->limits_changed, false))
  657. continue;
  658. throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
  659. " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
  660. tg->iops[READ], tg->iops[WRITE]);
  661. /*
  662. * Restart the slices for both READ and WRITES. It
  663. * might happen that a group's limit are dropped
  664. * suddenly and we don't want to account recently
  665. * dispatched IO with new low rate
  666. */
  667. throtl_start_new_slice(td, tg, 0);
  668. throtl_start_new_slice(td, tg, 1);
  669. if (throtl_tg_on_rr(tg))
  670. tg_update_disptime(td, tg);
  671. }
  672. }
  673. /* Dispatch throttled bios. Should be called without queue lock held. */
  674. static int throtl_dispatch(struct request_queue *q)
  675. {
  676. struct throtl_data *td = q->td;
  677. unsigned int nr_disp = 0;
  678. struct bio_list bio_list_on_stack;
  679. struct bio *bio;
  680. struct blk_plug plug;
  681. spin_lock_irq(q->queue_lock);
  682. throtl_process_limit_change(td);
  683. if (!total_nr_queued(td))
  684. goto out;
  685. bio_list_init(&bio_list_on_stack);
  686. throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
  687. total_nr_queued(td), td->nr_queued[READ],
  688. td->nr_queued[WRITE]);
  689. nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
  690. if (nr_disp)
  691. throtl_log(td, "bios disp=%u", nr_disp);
  692. throtl_schedule_next_dispatch(td);
  693. out:
  694. spin_unlock_irq(q->queue_lock);
  695. /*
  696. * If we dispatched some requests, unplug the queue to make sure
  697. * immediate dispatch
  698. */
  699. if (nr_disp) {
  700. blk_start_plug(&plug);
  701. while((bio = bio_list_pop(&bio_list_on_stack)))
  702. generic_make_request(bio);
  703. blk_finish_plug(&plug);
  704. }
  705. return nr_disp;
  706. }
  707. void blk_throtl_work(struct work_struct *work)
  708. {
  709. struct throtl_data *td = container_of(work, struct throtl_data,
  710. throtl_work.work);
  711. struct request_queue *q = td->queue;
  712. throtl_dispatch(q);
  713. }
  714. /* Call with queue lock held */
  715. static void
  716. throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
  717. {
  718. struct delayed_work *dwork = &td->throtl_work;
  719. /* schedule work if limits changed even if no bio is queued */
  720. if (total_nr_queued(td) || td->limits_changed) {
  721. /*
  722. * We might have a work scheduled to be executed in future.
  723. * Cancel that and schedule a new one.
  724. */
  725. __cancel_delayed_work(dwork);
  726. queue_delayed_work(kthrotld_workqueue, dwork, delay);
  727. throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
  728. delay, jiffies);
  729. }
  730. }
  731. static u64 tg_prfill_cpu_rwstat(struct seq_file *sf, void *pdata, int off)
  732. {
  733. struct throtl_grp *tg = pdata;
  734. struct blkg_rwstat rwstat = { }, tmp;
  735. int i, cpu;
  736. for_each_possible_cpu(cpu) {
  737. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  738. tmp = blkg_rwstat_read((void *)sc + off);
  739. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  740. rwstat.cnt[i] += tmp.cnt[i];
  741. }
  742. return __blkg_prfill_rwstat(sf, pdata, &rwstat);
  743. }
  744. static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
  745. struct seq_file *sf)
  746. {
  747. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  748. blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkio_policy_throtl,
  749. cft->private, true);
  750. return 0;
  751. }
  752. static u64 tg_prfill_conf_u64(struct seq_file *sf, void *pdata, int off)
  753. {
  754. u64 v = *(u64 *)(pdata + off);
  755. if (v == -1)
  756. return 0;
  757. return __blkg_prfill_u64(sf, pdata, v);
  758. }
  759. static u64 tg_prfill_conf_uint(struct seq_file *sf, void *pdata, int off)
  760. {
  761. unsigned int v = *(unsigned int *)(pdata + off);
  762. if (v == -1)
  763. return 0;
  764. return __blkg_prfill_u64(sf, pdata, v);
  765. }
  766. static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  767. struct seq_file *sf)
  768. {
  769. blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp), tg_prfill_conf_u64,
  770. &blkio_policy_throtl, cft->private, false);
  771. return 0;
  772. }
  773. static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  774. struct seq_file *sf)
  775. {
  776. blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp), tg_prfill_conf_uint,
  777. &blkio_policy_throtl, cft->private, false);
  778. return 0;
  779. }
  780. static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
  781. bool is_u64)
  782. {
  783. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  784. struct blkg_conf_ctx ctx;
  785. struct throtl_grp *tg;
  786. struct throtl_data *td;
  787. int ret;
  788. ret = blkg_conf_prep(blkcg, &blkio_policy_throtl, buf, &ctx);
  789. if (ret)
  790. return ret;
  791. tg = blkg_to_tg(ctx.blkg);
  792. td = ctx.blkg->q->td;
  793. if (!ctx.v)
  794. ctx.v = -1;
  795. if (is_u64)
  796. *(u64 *)((void *)tg + cft->private) = ctx.v;
  797. else
  798. *(unsigned int *)((void *)tg + cft->private) = ctx.v;
  799. /* XXX: we don't need the following deferred processing */
  800. xchg(&tg->limits_changed, true);
  801. xchg(&td->limits_changed, true);
  802. throtl_schedule_delayed_work(td, 0);
  803. blkg_conf_finish(&ctx);
  804. return 0;
  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. int ret;
  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. /* activate policy */
  994. ret = blkcg_activate_policy(q, &blkio_policy_throtl);
  995. if (ret)
  996. kfree(td);
  997. return ret;
  998. }
  999. void blk_throtl_exit(struct request_queue *q)
  1000. {
  1001. BUG_ON(!q->td);
  1002. throtl_shutdown_wq(q);
  1003. blkcg_deactivate_policy(q, &blkio_policy_throtl);
  1004. kfree(q->td);
  1005. }
  1006. static int __init throtl_init(void)
  1007. {
  1008. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  1009. if (!kthrotld_workqueue)
  1010. panic("Failed to create kthrotld\n");
  1011. return blkio_policy_register(&blkio_policy_throtl);
  1012. }
  1013. module_init(throtl_init);