blk-throttle.c 30 KB

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