blk-throttle.c 31 KB

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