blk-throttle.c 36 KB

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