blk-throttle.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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. throtl_enqueue_tg(tg);
  678. }
  679. static void tg_update_disptime(struct throtl_grp *tg)
  680. {
  681. struct throtl_service_queue *sq = &tg->service_queue;
  682. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  683. struct bio *bio;
  684. if ((bio = bio_list_peek(&sq->bio_lists[READ])))
  685. tg_may_dispatch(tg, bio, &read_wait);
  686. if ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
  687. tg_may_dispatch(tg, bio, &write_wait);
  688. min_wait = min(read_wait, write_wait);
  689. disptime = jiffies + min_wait;
  690. /* Update dispatch time */
  691. throtl_dequeue_tg(tg);
  692. tg->disptime = disptime;
  693. throtl_enqueue_tg(tg);
  694. /* see throtl_add_bio_tg() */
  695. tg->flags &= ~THROTL_TG_WAS_EMPTY;
  696. }
  697. static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
  698. {
  699. struct throtl_service_queue *sq = &tg->service_queue;
  700. struct throtl_service_queue *parent_sq = sq->parent_sq;
  701. struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
  702. struct bio *bio;
  703. bio = bio_list_pop(&sq->bio_lists[rw]);
  704. sq->nr_queued[rw]--;
  705. throtl_charge_bio(tg, bio);
  706. /*
  707. * If our parent is another tg, we just need to transfer @bio to
  708. * the parent using throtl_add_bio_tg(). If our parent is
  709. * @td->service_queue, @bio is ready to be issued. Put it on its
  710. * bio_lists[] and decrease total number queued. The caller is
  711. * responsible for issuing these bios.
  712. */
  713. if (parent_tg) {
  714. throtl_add_bio_tg(bio, parent_tg);
  715. } else {
  716. bio_list_add(&parent_sq->bio_lists[rw], bio);
  717. BUG_ON(tg->td->nr_queued[rw] <= 0);
  718. tg->td->nr_queued[rw]--;
  719. }
  720. throtl_trim_slice(tg, rw);
  721. /* @bio is transferred to parent, drop its blkg reference */
  722. blkg_put(tg_to_blkg(tg));
  723. }
  724. static int throtl_dispatch_tg(struct throtl_grp *tg)
  725. {
  726. struct throtl_service_queue *sq = &tg->service_queue;
  727. unsigned int nr_reads = 0, nr_writes = 0;
  728. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  729. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  730. struct bio *bio;
  731. /* Try to dispatch 75% READS and 25% WRITES */
  732. while ((bio = bio_list_peek(&sq->bio_lists[READ])) &&
  733. tg_may_dispatch(tg, bio, NULL)) {
  734. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  735. nr_reads++;
  736. if (nr_reads >= max_nr_reads)
  737. break;
  738. }
  739. while ((bio = bio_list_peek(&sq->bio_lists[WRITE])) &&
  740. tg_may_dispatch(tg, bio, NULL)) {
  741. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  742. nr_writes++;
  743. if (nr_writes >= max_nr_writes)
  744. break;
  745. }
  746. return nr_reads + nr_writes;
  747. }
  748. static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
  749. {
  750. unsigned int nr_disp = 0;
  751. while (1) {
  752. struct throtl_grp *tg = throtl_rb_first(parent_sq);
  753. struct throtl_service_queue *sq = &tg->service_queue;
  754. if (!tg)
  755. break;
  756. if (time_before(jiffies, tg->disptime))
  757. break;
  758. throtl_dequeue_tg(tg);
  759. nr_disp += throtl_dispatch_tg(tg);
  760. if (sq->nr_queued[0] || sq->nr_queued[1])
  761. tg_update_disptime(tg);
  762. if (nr_disp >= throtl_quantum)
  763. break;
  764. }
  765. return nr_disp;
  766. }
  767. /**
  768. * throtl_pending_timer_fn - timer function for service_queue->pending_timer
  769. * @arg: the throtl_service_queue being serviced
  770. *
  771. * This timer is armed when a child throtl_grp with active bio's become
  772. * pending and queued on the service_queue's pending_tree and expires when
  773. * the first child throtl_grp should be dispatched. This function
  774. * dispatches bio's from the children throtl_grps to the parent
  775. * service_queue.
  776. *
  777. * If the parent's parent is another throtl_grp, dispatching is propagated
  778. * by either arming its pending_timer or repeating dispatch directly. If
  779. * the top-level service_tree is reached, throtl_data->dispatch_work is
  780. * kicked so that the ready bio's are issued.
  781. */
  782. static void throtl_pending_timer_fn(unsigned long arg)
  783. {
  784. struct throtl_service_queue *sq = (void *)arg;
  785. struct throtl_grp *tg = sq_to_tg(sq);
  786. struct throtl_data *td = sq_to_td(sq);
  787. struct request_queue *q = td->queue;
  788. struct throtl_service_queue *parent_sq;
  789. bool dispatched;
  790. int ret;
  791. spin_lock_irq(q->queue_lock);
  792. again:
  793. parent_sq = sq->parent_sq;
  794. dispatched = false;
  795. while (true) {
  796. throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
  797. sq->nr_queued[READ] + sq->nr_queued[WRITE],
  798. sq->nr_queued[READ], sq->nr_queued[WRITE]);
  799. ret = throtl_select_dispatch(sq);
  800. if (ret) {
  801. throtl_log(sq, "bios disp=%u", ret);
  802. dispatched = true;
  803. }
  804. if (throtl_schedule_next_dispatch(sq, false))
  805. break;
  806. /* this dispatch windows is still open, relax and repeat */
  807. spin_unlock_irq(q->queue_lock);
  808. cpu_relax();
  809. spin_lock_irq(q->queue_lock);
  810. }
  811. if (!dispatched)
  812. goto out_unlock;
  813. if (parent_sq) {
  814. /* @parent_sq is another throl_grp, propagate dispatch */
  815. if (tg->flags & THROTL_TG_WAS_EMPTY) {
  816. tg_update_disptime(tg);
  817. if (!throtl_schedule_next_dispatch(parent_sq, false)) {
  818. /* window is already open, repeat dispatching */
  819. sq = parent_sq;
  820. tg = sq_to_tg(sq);
  821. goto again;
  822. }
  823. }
  824. } else {
  825. /* reached the top-level, queue issueing */
  826. queue_work(kthrotld_workqueue, &td->dispatch_work);
  827. }
  828. out_unlock:
  829. spin_unlock_irq(q->queue_lock);
  830. }
  831. /**
  832. * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
  833. * @work: work item being executed
  834. *
  835. * This function is queued for execution when bio's reach the bio_lists[]
  836. * of throtl_data->service_queue. Those bio's are ready and issued by this
  837. * function.
  838. */
  839. void blk_throtl_dispatch_work_fn(struct work_struct *work)
  840. {
  841. struct throtl_data *td = container_of(work, struct throtl_data,
  842. dispatch_work);
  843. struct throtl_service_queue *td_sq = &td->service_queue;
  844. struct request_queue *q = td->queue;
  845. struct bio_list bio_list_on_stack;
  846. struct bio *bio;
  847. struct blk_plug plug;
  848. int rw;
  849. bio_list_init(&bio_list_on_stack);
  850. spin_lock_irq(q->queue_lock);
  851. for (rw = READ; rw <= WRITE; rw++) {
  852. bio_list_merge(&bio_list_on_stack, &td_sq->bio_lists[rw]);
  853. bio_list_init(&td_sq->bio_lists[rw]);
  854. }
  855. spin_unlock_irq(q->queue_lock);
  856. if (!bio_list_empty(&bio_list_on_stack)) {
  857. blk_start_plug(&plug);
  858. while((bio = bio_list_pop(&bio_list_on_stack)))
  859. generic_make_request(bio);
  860. blk_finish_plug(&plug);
  861. }
  862. }
  863. static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
  864. struct blkg_policy_data *pd, int off)
  865. {
  866. struct throtl_grp *tg = pd_to_tg(pd);
  867. struct blkg_rwstat rwstat = { }, tmp;
  868. int i, cpu;
  869. for_each_possible_cpu(cpu) {
  870. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  871. tmp = blkg_rwstat_read((void *)sc + off);
  872. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  873. rwstat.cnt[i] += tmp.cnt[i];
  874. }
  875. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  876. }
  877. static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
  878. struct seq_file *sf)
  879. {
  880. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  881. blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
  882. cft->private, true);
  883. return 0;
  884. }
  885. static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
  886. int off)
  887. {
  888. struct throtl_grp *tg = pd_to_tg(pd);
  889. u64 v = *(u64 *)((void *)tg + off);
  890. if (v == -1)
  891. return 0;
  892. return __blkg_prfill_u64(sf, pd, v);
  893. }
  894. static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
  895. int off)
  896. {
  897. struct throtl_grp *tg = pd_to_tg(pd);
  898. unsigned int v = *(unsigned int *)((void *)tg + off);
  899. if (v == -1)
  900. return 0;
  901. return __blkg_prfill_u64(sf, pd, v);
  902. }
  903. static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  904. struct seq_file *sf)
  905. {
  906. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
  907. &blkcg_policy_throtl, cft->private, false);
  908. return 0;
  909. }
  910. static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  911. struct seq_file *sf)
  912. {
  913. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
  914. &blkcg_policy_throtl, cft->private, false);
  915. return 0;
  916. }
  917. static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
  918. bool is_u64)
  919. {
  920. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  921. struct blkg_conf_ctx ctx;
  922. struct throtl_grp *tg;
  923. struct throtl_service_queue *sq;
  924. int ret;
  925. ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
  926. if (ret)
  927. return ret;
  928. tg = blkg_to_tg(ctx.blkg);
  929. sq = &tg->service_queue;
  930. if (!ctx.v)
  931. ctx.v = -1;
  932. if (is_u64)
  933. *(u64 *)((void *)tg + cft->private) = ctx.v;
  934. else
  935. *(unsigned int *)((void *)tg + cft->private) = ctx.v;
  936. throtl_log(&tg->service_queue,
  937. "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
  938. tg->bps[READ], tg->bps[WRITE],
  939. tg->iops[READ], tg->iops[WRITE]);
  940. /*
  941. * We're already holding queue_lock and know @tg is valid. Let's
  942. * apply the new config directly.
  943. *
  944. * Restart the slices for both READ and WRITES. It might happen
  945. * that a group's limit are dropped suddenly and we don't want to
  946. * account recently dispatched IO with new low rate.
  947. */
  948. throtl_start_new_slice(tg, 0);
  949. throtl_start_new_slice(tg, 1);
  950. if (tg->flags & THROTL_TG_PENDING) {
  951. tg_update_disptime(tg);
  952. throtl_schedule_next_dispatch(sq->parent_sq, true);
  953. }
  954. blkg_conf_finish(&ctx);
  955. return 0;
  956. }
  957. static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  958. const char *buf)
  959. {
  960. return tg_set_conf(cgrp, cft, buf, true);
  961. }
  962. static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  963. const char *buf)
  964. {
  965. return tg_set_conf(cgrp, cft, buf, false);
  966. }
  967. static struct cftype throtl_files[] = {
  968. {
  969. .name = "throttle.read_bps_device",
  970. .private = offsetof(struct throtl_grp, bps[READ]),
  971. .read_seq_string = tg_print_conf_u64,
  972. .write_string = tg_set_conf_u64,
  973. .max_write_len = 256,
  974. },
  975. {
  976. .name = "throttle.write_bps_device",
  977. .private = offsetof(struct throtl_grp, bps[WRITE]),
  978. .read_seq_string = tg_print_conf_u64,
  979. .write_string = tg_set_conf_u64,
  980. .max_write_len = 256,
  981. },
  982. {
  983. .name = "throttle.read_iops_device",
  984. .private = offsetof(struct throtl_grp, iops[READ]),
  985. .read_seq_string = tg_print_conf_uint,
  986. .write_string = tg_set_conf_uint,
  987. .max_write_len = 256,
  988. },
  989. {
  990. .name = "throttle.write_iops_device",
  991. .private = offsetof(struct throtl_grp, iops[WRITE]),
  992. .read_seq_string = tg_print_conf_uint,
  993. .write_string = tg_set_conf_uint,
  994. .max_write_len = 256,
  995. },
  996. {
  997. .name = "throttle.io_service_bytes",
  998. .private = offsetof(struct tg_stats_cpu, service_bytes),
  999. .read_seq_string = tg_print_cpu_rwstat,
  1000. },
  1001. {
  1002. .name = "throttle.io_serviced",
  1003. .private = offsetof(struct tg_stats_cpu, serviced),
  1004. .read_seq_string = tg_print_cpu_rwstat,
  1005. },
  1006. { } /* terminate */
  1007. };
  1008. static void throtl_shutdown_wq(struct request_queue *q)
  1009. {
  1010. struct throtl_data *td = q->td;
  1011. cancel_work_sync(&td->dispatch_work);
  1012. }
  1013. static struct blkcg_policy blkcg_policy_throtl = {
  1014. .pd_size = sizeof(struct throtl_grp),
  1015. .cftypes = throtl_files,
  1016. .pd_init_fn = throtl_pd_init,
  1017. .pd_exit_fn = throtl_pd_exit,
  1018. .pd_reset_stats_fn = throtl_pd_reset_stats,
  1019. };
  1020. bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
  1021. {
  1022. struct throtl_data *td = q->td;
  1023. struct throtl_grp *tg;
  1024. struct throtl_service_queue *sq;
  1025. bool rw = bio_data_dir(bio);
  1026. struct blkcg *blkcg;
  1027. bool throttled = false;
  1028. /* see throtl_charge_bio() */
  1029. if (bio->bi_rw & REQ_THROTTLED)
  1030. goto out;
  1031. /*
  1032. * A throtl_grp pointer retrieved under rcu can be used to access
  1033. * basic fields like stats and io rates. If a group has no rules,
  1034. * just update the dispatch stats in lockless manner and return.
  1035. */
  1036. rcu_read_lock();
  1037. blkcg = bio_blkcg(bio);
  1038. tg = throtl_lookup_tg(td, blkcg);
  1039. if (tg) {
  1040. if (tg_no_rule_group(tg, rw)) {
  1041. throtl_update_dispatch_stats(tg_to_blkg(tg),
  1042. bio->bi_size, bio->bi_rw);
  1043. goto out_unlock_rcu;
  1044. }
  1045. }
  1046. /*
  1047. * Either group has not been allocated yet or it is not an unlimited
  1048. * IO group
  1049. */
  1050. spin_lock_irq(q->queue_lock);
  1051. tg = throtl_lookup_create_tg(td, blkcg);
  1052. if (unlikely(!tg))
  1053. goto out_unlock;
  1054. sq = &tg->service_queue;
  1055. while (true) {
  1056. /* throtl is FIFO - if bios are already queued, should queue */
  1057. if (sq->nr_queued[rw])
  1058. break;
  1059. /* if above limits, break to queue */
  1060. if (!tg_may_dispatch(tg, bio, NULL))
  1061. break;
  1062. /* within limits, let's charge and dispatch directly */
  1063. throtl_charge_bio(tg, bio);
  1064. /*
  1065. * We need to trim slice even when bios are not being queued
  1066. * otherwise it might happen that a bio is not queued for
  1067. * a long time and slice keeps on extending and trim is not
  1068. * called for a long time. Now if limits are reduced suddenly
  1069. * we take into account all the IO dispatched so far at new
  1070. * low rate and * newly queued IO gets a really long dispatch
  1071. * time.
  1072. *
  1073. * So keep on trimming slice even if bio is not queued.
  1074. */
  1075. throtl_trim_slice(tg, rw);
  1076. /*
  1077. * @bio passed through this layer without being throttled.
  1078. * Climb up the ladder. If we''re already at the top, it
  1079. * can be executed directly.
  1080. */
  1081. sq = sq->parent_sq;
  1082. tg = sq_to_tg(sq);
  1083. if (!tg)
  1084. goto out_unlock;
  1085. }
  1086. /* out-of-limit, queue to @tg */
  1087. throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
  1088. rw == READ ? 'R' : 'W',
  1089. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  1090. tg->io_disp[rw], tg->iops[rw],
  1091. sq->nr_queued[READ], sq->nr_queued[WRITE]);
  1092. bio_associate_current(bio);
  1093. tg->td->nr_queued[rw]++;
  1094. throtl_add_bio_tg(bio, tg);
  1095. throttled = true;
  1096. /*
  1097. * Update @tg's dispatch time and force schedule dispatch if @tg
  1098. * was empty before @bio. The forced scheduling isn't likely to
  1099. * cause undue delay as @bio is likely to be dispatched directly if
  1100. * its @tg's disptime is not in the future.
  1101. */
  1102. if (tg->flags & THROTL_TG_WAS_EMPTY) {
  1103. tg_update_disptime(tg);
  1104. throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
  1105. }
  1106. out_unlock:
  1107. spin_unlock_irq(q->queue_lock);
  1108. out_unlock_rcu:
  1109. rcu_read_unlock();
  1110. out:
  1111. /*
  1112. * As multiple blk-throtls may stack in the same issue path, we
  1113. * don't want bios to leave with the flag set. Clear the flag if
  1114. * being issued.
  1115. */
  1116. if (!throttled)
  1117. bio->bi_rw &= ~REQ_THROTTLED;
  1118. return throttled;
  1119. }
  1120. /*
  1121. * Dispatch all bios from all children tg's queued on @parent_sq. On
  1122. * return, @parent_sq is guaranteed to not have any active children tg's
  1123. * and all bios from previously active tg's are on @parent_sq->bio_lists[].
  1124. */
  1125. static void tg_drain_bios(struct throtl_service_queue *parent_sq)
  1126. {
  1127. struct throtl_grp *tg;
  1128. while ((tg = throtl_rb_first(parent_sq))) {
  1129. struct throtl_service_queue *sq = &tg->service_queue;
  1130. struct bio *bio;
  1131. throtl_dequeue_tg(tg);
  1132. while ((bio = bio_list_peek(&sq->bio_lists[READ])))
  1133. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  1134. while ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
  1135. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  1136. }
  1137. }
  1138. /**
  1139. * blk_throtl_drain - drain throttled bios
  1140. * @q: request_queue to drain throttled bios for
  1141. *
  1142. * Dispatch all currently throttled bios on @q through ->make_request_fn().
  1143. */
  1144. void blk_throtl_drain(struct request_queue *q)
  1145. __releases(q->queue_lock) __acquires(q->queue_lock)
  1146. {
  1147. struct throtl_data *td = q->td;
  1148. struct blkcg_gq *blkg;
  1149. struct cgroup *pos_cgrp;
  1150. struct bio *bio;
  1151. int rw;
  1152. queue_lockdep_assert_held(q);
  1153. rcu_read_lock();
  1154. /*
  1155. * Drain each tg while doing post-order walk on the blkg tree, so
  1156. * that all bios are propagated to td->service_queue. It'd be
  1157. * better to walk service_queue tree directly but blkg walk is
  1158. * easier.
  1159. */
  1160. blkg_for_each_descendant_post(blkg, pos_cgrp, td->queue->root_blkg)
  1161. tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
  1162. tg_drain_bios(&td_root_tg(td)->service_queue);
  1163. /* finally, transfer bios from top-level tg's into the td */
  1164. tg_drain_bios(&td->service_queue);
  1165. rcu_read_unlock();
  1166. spin_unlock_irq(q->queue_lock);
  1167. /* all bios now should be in td->service_queue, issue them */
  1168. for (rw = READ; rw <= WRITE; rw++)
  1169. while ((bio = bio_list_pop(&td->service_queue.bio_lists[rw])))
  1170. generic_make_request(bio);
  1171. spin_lock_irq(q->queue_lock);
  1172. }
  1173. int blk_throtl_init(struct request_queue *q)
  1174. {
  1175. struct throtl_data *td;
  1176. int ret;
  1177. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  1178. if (!td)
  1179. return -ENOMEM;
  1180. INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
  1181. throtl_service_queue_init(&td->service_queue, NULL);
  1182. q->td = td;
  1183. td->queue = q;
  1184. /* activate policy */
  1185. ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
  1186. if (ret)
  1187. kfree(td);
  1188. return ret;
  1189. }
  1190. void blk_throtl_exit(struct request_queue *q)
  1191. {
  1192. BUG_ON(!q->td);
  1193. throtl_shutdown_wq(q);
  1194. blkcg_deactivate_policy(q, &blkcg_policy_throtl);
  1195. kfree(q->td);
  1196. }
  1197. static int __init throtl_init(void)
  1198. {
  1199. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  1200. if (!kthrotld_workqueue)
  1201. panic("Failed to create kthrotld\n");
  1202. return blkcg_policy_register(&blkcg_policy_throtl);
  1203. }
  1204. module_init(throtl_init);