blk-throttle.c 30 KB

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