blk-throttle.c 45 KB

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