blk-throttle.c 30 KB

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