blk-cgroup.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  1. /*
  2. * Common Block IO controller cgroup interface
  3. *
  4. * Based on ideas and code from CFQ, CFS and BFQ:
  5. * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
  6. *
  7. * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
  8. * Paolo Valente <paolo.valente@unimore.it>
  9. *
  10. * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
  11. * Nauman Rafique <nauman@google.com>
  12. */
  13. #include <linux/ioprio.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/kdev_t.h>
  16. #include <linux/module.h>
  17. #include <linux/err.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/slab.h>
  20. #include <linux/genhd.h>
  21. #include <linux/delay.h>
  22. #include "blk-cgroup.h"
  23. #include "blk.h"
  24. #define MAX_KEY_LEN 100
  25. static DEFINE_SPINLOCK(blkio_list_lock);
  26. static LIST_HEAD(blkio_list);
  27. static DEFINE_MUTEX(all_q_mutex);
  28. static LIST_HEAD(all_q_list);
  29. /* List of groups pending per cpu stats allocation */
  30. static DEFINE_SPINLOCK(alloc_list_lock);
  31. static LIST_HEAD(alloc_list);
  32. static void blkio_stat_alloc_fn(struct work_struct *);
  33. static DECLARE_DELAYED_WORK(blkio_stat_alloc_work, blkio_stat_alloc_fn);
  34. struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
  35. EXPORT_SYMBOL_GPL(blkio_root_cgroup);
  36. static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
  37. static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
  38. struct cgroup *);
  39. static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
  40. struct cgroup_taskset *);
  41. static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
  42. struct cgroup_taskset *);
  43. static int blkiocg_pre_destroy(struct cgroup_subsys *, struct cgroup *);
  44. static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
  45. static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
  46. /* for encoding cft->private value on file */
  47. #define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
  48. /* What policy owns the file, proportional or throttle */
  49. #define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
  50. #define BLKIOFILE_ATTR(val) ((val) & 0xffff)
  51. struct cgroup_subsys blkio_subsys = {
  52. .name = "blkio",
  53. .create = blkiocg_create,
  54. .can_attach = blkiocg_can_attach,
  55. .attach = blkiocg_attach,
  56. .pre_destroy = blkiocg_pre_destroy,
  57. .destroy = blkiocg_destroy,
  58. .populate = blkiocg_populate,
  59. .subsys_id = blkio_subsys_id,
  60. .module = THIS_MODULE,
  61. };
  62. EXPORT_SYMBOL_GPL(blkio_subsys);
  63. struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
  64. {
  65. return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
  66. struct blkio_cgroup, css);
  67. }
  68. EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
  69. static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
  70. {
  71. return container_of(task_subsys_state(tsk, blkio_subsys_id),
  72. struct blkio_cgroup, css);
  73. }
  74. struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
  75. {
  76. if (bio && bio->bi_css)
  77. return container_of(bio->bi_css, struct blkio_cgroup, css);
  78. return task_blkio_cgroup(current);
  79. }
  80. EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
  81. static inline void blkio_update_group_weight(struct blkio_group *blkg,
  82. int plid, unsigned int weight)
  83. {
  84. struct blkio_policy_type *blkiop;
  85. list_for_each_entry(blkiop, &blkio_list, list) {
  86. /* If this policy does not own the blkg, do not send updates */
  87. if (blkiop->plid != plid)
  88. continue;
  89. if (blkiop->ops.blkio_update_group_weight_fn)
  90. blkiop->ops.blkio_update_group_weight_fn(blkg->q,
  91. blkg, weight);
  92. }
  93. }
  94. static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid,
  95. u64 bps, int fileid)
  96. {
  97. struct blkio_policy_type *blkiop;
  98. list_for_each_entry(blkiop, &blkio_list, list) {
  99. /* If this policy does not own the blkg, do not send updates */
  100. if (blkiop->plid != plid)
  101. continue;
  102. if (fileid == BLKIO_THROTL_read_bps_device
  103. && blkiop->ops.blkio_update_group_read_bps_fn)
  104. blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
  105. blkg, bps);
  106. if (fileid == BLKIO_THROTL_write_bps_device
  107. && blkiop->ops.blkio_update_group_write_bps_fn)
  108. blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
  109. blkg, bps);
  110. }
  111. }
  112. static inline void blkio_update_group_iops(struct blkio_group *blkg,
  113. int plid, unsigned int iops,
  114. int fileid)
  115. {
  116. struct blkio_policy_type *blkiop;
  117. list_for_each_entry(blkiop, &blkio_list, list) {
  118. /* If this policy does not own the blkg, do not send updates */
  119. if (blkiop->plid != plid)
  120. continue;
  121. if (fileid == BLKIO_THROTL_read_iops_device
  122. && blkiop->ops.blkio_update_group_read_iops_fn)
  123. blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
  124. blkg, iops);
  125. if (fileid == BLKIO_THROTL_write_iops_device
  126. && blkiop->ops.blkio_update_group_write_iops_fn)
  127. blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
  128. blkg,iops);
  129. }
  130. }
  131. /*
  132. * Add to the appropriate stat variable depending on the request type.
  133. * This should be called with the blkg->stats_lock held.
  134. */
  135. static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
  136. bool sync)
  137. {
  138. if (direction)
  139. stat[BLKIO_STAT_WRITE] += add;
  140. else
  141. stat[BLKIO_STAT_READ] += add;
  142. if (sync)
  143. stat[BLKIO_STAT_SYNC] += add;
  144. else
  145. stat[BLKIO_STAT_ASYNC] += add;
  146. }
  147. /*
  148. * Decrements the appropriate stat variable if non-zero depending on the
  149. * request type. Panics on value being zero.
  150. * This should be called with the blkg->stats_lock held.
  151. */
  152. static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
  153. {
  154. if (direction) {
  155. BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
  156. stat[BLKIO_STAT_WRITE]--;
  157. } else {
  158. BUG_ON(stat[BLKIO_STAT_READ] == 0);
  159. stat[BLKIO_STAT_READ]--;
  160. }
  161. if (sync) {
  162. BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
  163. stat[BLKIO_STAT_SYNC]--;
  164. } else {
  165. BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
  166. stat[BLKIO_STAT_ASYNC]--;
  167. }
  168. }
  169. #ifdef CONFIG_DEBUG_BLK_CGROUP
  170. /* This should be called with the blkg->stats_lock held. */
  171. static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
  172. struct blkio_policy_type *pol,
  173. struct blkio_group *curr_blkg)
  174. {
  175. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  176. if (blkio_blkg_waiting(&pd->stats))
  177. return;
  178. if (blkg == curr_blkg)
  179. return;
  180. pd->stats.start_group_wait_time = sched_clock();
  181. blkio_mark_blkg_waiting(&pd->stats);
  182. }
  183. /* This should be called with the blkg->stats_lock held. */
  184. static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
  185. {
  186. unsigned long long now;
  187. if (!blkio_blkg_waiting(stats))
  188. return;
  189. now = sched_clock();
  190. if (time_after64(now, stats->start_group_wait_time))
  191. stats->group_wait_time += now - stats->start_group_wait_time;
  192. blkio_clear_blkg_waiting(stats);
  193. }
  194. /* This should be called with the blkg->stats_lock held. */
  195. static void blkio_end_empty_time(struct blkio_group_stats *stats)
  196. {
  197. unsigned long long now;
  198. if (!blkio_blkg_empty(stats))
  199. return;
  200. now = sched_clock();
  201. if (time_after64(now, stats->start_empty_time))
  202. stats->empty_time += now - stats->start_empty_time;
  203. blkio_clear_blkg_empty(stats);
  204. }
  205. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
  206. struct blkio_policy_type *pol)
  207. {
  208. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  209. unsigned long flags;
  210. spin_lock_irqsave(&blkg->stats_lock, flags);
  211. BUG_ON(blkio_blkg_idling(&pd->stats));
  212. pd->stats.start_idle_time = sched_clock();
  213. blkio_mark_blkg_idling(&pd->stats);
  214. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  215. }
  216. EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
  217. void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
  218. struct blkio_policy_type *pol)
  219. {
  220. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  221. unsigned long flags;
  222. unsigned long long now;
  223. struct blkio_group_stats *stats;
  224. spin_lock_irqsave(&blkg->stats_lock, flags);
  225. stats = &pd->stats;
  226. if (blkio_blkg_idling(stats)) {
  227. now = sched_clock();
  228. if (time_after64(now, stats->start_idle_time))
  229. stats->idle_time += now - stats->start_idle_time;
  230. blkio_clear_blkg_idling(stats);
  231. }
  232. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  233. }
  234. EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
  235. void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
  236. struct blkio_policy_type *pol)
  237. {
  238. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  239. unsigned long flags;
  240. struct blkio_group_stats *stats;
  241. spin_lock_irqsave(&blkg->stats_lock, flags);
  242. stats = &pd->stats;
  243. stats->avg_queue_size_sum +=
  244. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
  245. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
  246. stats->avg_queue_size_samples++;
  247. blkio_update_group_wait_time(stats);
  248. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  249. }
  250. EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
  251. void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  252. struct blkio_policy_type *pol)
  253. {
  254. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  255. unsigned long flags;
  256. struct blkio_group_stats *stats;
  257. spin_lock_irqsave(&blkg->stats_lock, flags);
  258. stats = &pd->stats;
  259. if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
  260. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
  261. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  262. return;
  263. }
  264. /*
  265. * group is already marked empty. This can happen if cfqq got new
  266. * request in parent group and moved to this group while being added
  267. * to service tree. Just ignore the event and move on.
  268. */
  269. if(blkio_blkg_empty(stats)) {
  270. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  271. return;
  272. }
  273. stats->start_empty_time = sched_clock();
  274. blkio_mark_blkg_empty(stats);
  275. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  276. }
  277. EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
  278. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  279. struct blkio_policy_type *pol,
  280. unsigned long dequeue)
  281. {
  282. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  283. pd->stats.dequeue += dequeue;
  284. }
  285. EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
  286. #else
  287. static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
  288. struct blkio_policy_type *pol,
  289. struct blkio_group *curr_blkg) { }
  290. static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
  291. #endif
  292. void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  293. struct blkio_policy_type *pol,
  294. struct blkio_group *curr_blkg, bool direction,
  295. bool sync)
  296. {
  297. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  298. unsigned long flags;
  299. spin_lock_irqsave(&blkg->stats_lock, flags);
  300. blkio_add_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
  301. sync);
  302. blkio_end_empty_time(&pd->stats);
  303. blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
  304. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  305. }
  306. EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
  307. void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  308. struct blkio_policy_type *pol,
  309. bool direction, bool sync)
  310. {
  311. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  312. unsigned long flags;
  313. spin_lock_irqsave(&blkg->stats_lock, flags);
  314. blkio_check_and_dec_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED],
  315. direction, sync);
  316. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  317. }
  318. EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
  319. void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  320. struct blkio_policy_type *pol,
  321. unsigned long time,
  322. unsigned long unaccounted_time)
  323. {
  324. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  325. unsigned long flags;
  326. spin_lock_irqsave(&blkg->stats_lock, flags);
  327. pd->stats.time += time;
  328. #ifdef CONFIG_DEBUG_BLK_CGROUP
  329. pd->stats.unaccounted_time += unaccounted_time;
  330. #endif
  331. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  332. }
  333. EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
  334. /*
  335. * should be called under rcu read lock or queue lock to make sure blkg pointer
  336. * is valid.
  337. */
  338. void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  339. struct blkio_policy_type *pol,
  340. uint64_t bytes, bool direction, bool sync)
  341. {
  342. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  343. struct blkio_group_stats_cpu *stats_cpu;
  344. unsigned long flags;
  345. /* If per cpu stats are not allocated yet, don't do any accounting. */
  346. if (pd->stats_cpu == NULL)
  347. return;
  348. /*
  349. * Disabling interrupts to provide mutual exclusion between two
  350. * writes on same cpu. It probably is not needed for 64bit. Not
  351. * optimizing that case yet.
  352. */
  353. local_irq_save(flags);
  354. stats_cpu = this_cpu_ptr(pd->stats_cpu);
  355. u64_stats_update_begin(&stats_cpu->syncp);
  356. stats_cpu->sectors += bytes >> 9;
  357. blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
  358. 1, direction, sync);
  359. blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
  360. bytes, direction, sync);
  361. u64_stats_update_end(&stats_cpu->syncp);
  362. local_irq_restore(flags);
  363. }
  364. EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
  365. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  366. struct blkio_policy_type *pol,
  367. uint64_t start_time,
  368. uint64_t io_start_time, bool direction,
  369. bool sync)
  370. {
  371. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  372. struct blkio_group_stats *stats;
  373. unsigned long flags;
  374. unsigned long long now = sched_clock();
  375. spin_lock_irqsave(&blkg->stats_lock, flags);
  376. stats = &pd->stats;
  377. if (time_after64(now, io_start_time))
  378. blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
  379. now - io_start_time, direction, sync);
  380. if (time_after64(io_start_time, start_time))
  381. blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
  382. io_start_time - start_time, direction, sync);
  383. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  384. }
  385. EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
  386. /* Merged stats are per cpu. */
  387. void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  388. struct blkio_policy_type *pol,
  389. bool direction, bool sync)
  390. {
  391. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  392. struct blkio_group_stats *stats;
  393. unsigned long flags;
  394. spin_lock_irqsave(&blkg->stats_lock, flags);
  395. stats = &pd->stats;
  396. blkio_add_stat(stats->stat_arr[BLKIO_STAT_MERGED], 1, direction, sync);
  397. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  398. }
  399. EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
  400. /*
  401. * Worker for allocating per cpu stat for blk groups. This is scheduled on
  402. * the system_nrt_wq once there are some groups on the alloc_list waiting
  403. * for allocation.
  404. */
  405. static void blkio_stat_alloc_fn(struct work_struct *work)
  406. {
  407. static void *pcpu_stats[BLKIO_NR_POLICIES];
  408. struct delayed_work *dwork = to_delayed_work(work);
  409. struct blkio_group *blkg;
  410. int i;
  411. bool empty = false;
  412. alloc_stats:
  413. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  414. if (pcpu_stats[i] != NULL)
  415. continue;
  416. pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
  417. /* Allocation failed. Try again after some time. */
  418. if (pcpu_stats[i] == NULL) {
  419. queue_delayed_work(system_nrt_wq, dwork,
  420. msecs_to_jiffies(10));
  421. return;
  422. }
  423. }
  424. spin_lock_irq(&blkio_list_lock);
  425. spin_lock(&alloc_list_lock);
  426. /* cgroup got deleted or queue exited. */
  427. if (!list_empty(&alloc_list)) {
  428. blkg = list_first_entry(&alloc_list, struct blkio_group,
  429. alloc_node);
  430. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  431. struct blkg_policy_data *pd = blkg->pd[i];
  432. if (blkio_policy[i] && pd && !pd->stats_cpu)
  433. swap(pd->stats_cpu, pcpu_stats[i]);
  434. }
  435. list_del_init(&blkg->alloc_node);
  436. }
  437. empty = list_empty(&alloc_list);
  438. spin_unlock(&alloc_list_lock);
  439. spin_unlock_irq(&blkio_list_lock);
  440. if (!empty)
  441. goto alloc_stats;
  442. }
  443. /**
  444. * blkg_free - free a blkg
  445. * @blkg: blkg to free
  446. *
  447. * Free @blkg which may be partially allocated.
  448. */
  449. static void blkg_free(struct blkio_group *blkg)
  450. {
  451. int i;
  452. if (!blkg)
  453. return;
  454. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  455. struct blkg_policy_data *pd = blkg->pd[i];
  456. if (pd) {
  457. free_percpu(pd->stats_cpu);
  458. kfree(pd);
  459. }
  460. }
  461. kfree(blkg);
  462. }
  463. /**
  464. * blkg_alloc - allocate a blkg
  465. * @blkcg: block cgroup the new blkg is associated with
  466. * @q: request_queue the new blkg is associated with
  467. *
  468. * Allocate a new blkg assocating @blkcg and @q.
  469. */
  470. static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
  471. struct request_queue *q)
  472. {
  473. struct blkio_group *blkg;
  474. int i;
  475. /* alloc and init base part */
  476. blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
  477. if (!blkg)
  478. return NULL;
  479. spin_lock_init(&blkg->stats_lock);
  480. blkg->q = q;
  481. INIT_LIST_HEAD(&blkg->q_node);
  482. INIT_LIST_HEAD(&blkg->alloc_node);
  483. blkg->blkcg = blkcg;
  484. blkg->refcnt = 1;
  485. cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
  486. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  487. struct blkio_policy_type *pol = blkio_policy[i];
  488. struct blkg_policy_data *pd;
  489. if (!pol)
  490. continue;
  491. /* alloc per-policy data and attach it to blkg */
  492. pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
  493. q->node);
  494. if (!pd) {
  495. blkg_free(blkg);
  496. return NULL;
  497. }
  498. blkg->pd[i] = pd;
  499. pd->blkg = blkg;
  500. }
  501. /* invoke per-policy init */
  502. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  503. struct blkio_policy_type *pol = blkio_policy[i];
  504. if (pol)
  505. pol->ops.blkio_init_group_fn(blkg);
  506. }
  507. return blkg;
  508. }
  509. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  510. struct request_queue *q,
  511. enum blkio_policy_id plid,
  512. bool for_root)
  513. __releases(q->queue_lock) __acquires(q->queue_lock)
  514. {
  515. struct blkio_group *blkg;
  516. WARN_ON_ONCE(!rcu_read_lock_held());
  517. lockdep_assert_held(q->queue_lock);
  518. /*
  519. * This could be the first entry point of blkcg implementation and
  520. * we shouldn't allow anything to go through for a bypassing queue.
  521. * The following can be removed if blkg lookup is guaranteed to
  522. * fail on a bypassing queue.
  523. */
  524. if (unlikely(blk_queue_bypass(q)) && !for_root)
  525. return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
  526. blkg = blkg_lookup(blkcg, q);
  527. if (blkg)
  528. return blkg;
  529. /* blkg holds a reference to blkcg */
  530. if (!css_tryget(&blkcg->css))
  531. return ERR_PTR(-EINVAL);
  532. /*
  533. * Allocate and initialize.
  534. */
  535. blkg = blkg_alloc(blkcg, q);
  536. /* did alloc fail? */
  537. if (unlikely(!blkg)) {
  538. blkg = ERR_PTR(-ENOMEM);
  539. goto out;
  540. }
  541. /* insert */
  542. spin_lock(&blkcg->lock);
  543. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  544. list_add(&blkg->q_node, &q->blkg_list);
  545. spin_unlock(&blkcg->lock);
  546. spin_lock(&alloc_list_lock);
  547. list_add(&blkg->alloc_node, &alloc_list);
  548. /* Queue per cpu stat allocation from worker thread. */
  549. queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
  550. spin_unlock(&alloc_list_lock);
  551. out:
  552. return blkg;
  553. }
  554. EXPORT_SYMBOL_GPL(blkg_lookup_create);
  555. /* called under rcu_read_lock(). */
  556. struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  557. struct request_queue *q)
  558. {
  559. struct blkio_group *blkg;
  560. struct hlist_node *n;
  561. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
  562. if (blkg->q == q)
  563. return blkg;
  564. return NULL;
  565. }
  566. EXPORT_SYMBOL_GPL(blkg_lookup);
  567. static void blkg_destroy(struct blkio_group *blkg)
  568. {
  569. struct request_queue *q = blkg->q;
  570. struct blkio_cgroup *blkcg = blkg->blkcg;
  571. lockdep_assert_held(q->queue_lock);
  572. lockdep_assert_held(&blkcg->lock);
  573. /* Something wrong if we are trying to remove same group twice */
  574. WARN_ON_ONCE(list_empty(&blkg->q_node));
  575. WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
  576. list_del_init(&blkg->q_node);
  577. hlist_del_init_rcu(&blkg->blkcg_node);
  578. spin_lock(&alloc_list_lock);
  579. list_del_init(&blkg->alloc_node);
  580. spin_unlock(&alloc_list_lock);
  581. /*
  582. * Put the reference taken at the time of creation so that when all
  583. * queues are gone, group can be destroyed.
  584. */
  585. blkg_put(blkg);
  586. }
  587. /*
  588. * XXX: This updates blkg policy data in-place for root blkg, which is
  589. * necessary across elevator switch and policy registration as root blkgs
  590. * aren't shot down. This broken and racy implementation is temporary.
  591. * Eventually, blkg shoot down will be replaced by proper in-place update.
  592. */
  593. void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
  594. {
  595. struct blkio_policy_type *pol = blkio_policy[plid];
  596. struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
  597. struct blkg_policy_data *pd;
  598. if (!blkg)
  599. return;
  600. kfree(blkg->pd[plid]);
  601. blkg->pd[plid] = NULL;
  602. if (!pol)
  603. return;
  604. pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
  605. WARN_ON_ONCE(!pd);
  606. pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
  607. WARN_ON_ONCE(!pd->stats_cpu);
  608. blkg->pd[plid] = pd;
  609. pd->blkg = blkg;
  610. pol->ops.blkio_init_group_fn(blkg);
  611. }
  612. EXPORT_SYMBOL_GPL(update_root_blkg_pd);
  613. /**
  614. * blkg_destroy_all - destroy all blkgs associated with a request_queue
  615. * @q: request_queue of interest
  616. * @destroy_root: whether to destroy root blkg or not
  617. *
  618. * Destroy blkgs associated with @q. If @destroy_root is %true, all are
  619. * destroyed; otherwise, root blkg is left alone.
  620. */
  621. void blkg_destroy_all(struct request_queue *q, bool destroy_root)
  622. {
  623. struct blkio_group *blkg, *n;
  624. spin_lock_irq(q->queue_lock);
  625. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  626. struct blkio_cgroup *blkcg = blkg->blkcg;
  627. /* skip root? */
  628. if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
  629. continue;
  630. spin_lock(&blkcg->lock);
  631. blkg_destroy(blkg);
  632. spin_unlock(&blkcg->lock);
  633. }
  634. spin_unlock_irq(q->queue_lock);
  635. }
  636. EXPORT_SYMBOL_GPL(blkg_destroy_all);
  637. static void blkg_rcu_free(struct rcu_head *rcu_head)
  638. {
  639. blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
  640. }
  641. void __blkg_release(struct blkio_group *blkg)
  642. {
  643. /* release the extra blkcg reference this blkg has been holding */
  644. css_put(&blkg->blkcg->css);
  645. /*
  646. * A group is freed in rcu manner. But having an rcu lock does not
  647. * mean that one can access all the fields of blkg and assume these
  648. * are valid. For example, don't try to follow throtl_data and
  649. * request queue links.
  650. *
  651. * Having a reference to blkg under an rcu allows acess to only
  652. * values local to groups like group stats and group rate limits
  653. */
  654. call_rcu(&blkg->rcu_head, blkg_rcu_free);
  655. }
  656. EXPORT_SYMBOL_GPL(__blkg_release);
  657. static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
  658. {
  659. struct blkg_policy_data *pd = blkg->pd[plid];
  660. int cpu;
  661. if (pd->stats_cpu == NULL)
  662. return;
  663. for_each_possible_cpu(cpu) {
  664. struct blkio_group_stats_cpu *sc =
  665. per_cpu_ptr(pd->stats_cpu, cpu);
  666. sc->sectors = 0;
  667. memset(sc->stat_arr_cpu, 0, sizeof(sc->stat_arr_cpu));
  668. }
  669. }
  670. static int
  671. blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
  672. {
  673. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  674. struct blkio_group *blkg;
  675. struct hlist_node *n;
  676. int i;
  677. spin_lock(&blkio_list_lock);
  678. spin_lock_irq(&blkcg->lock);
  679. /*
  680. * Note that stat reset is racy - it doesn't synchronize against
  681. * stat updates. This is a debug feature which shouldn't exist
  682. * anyway. If you get hit by a race, retry.
  683. */
  684. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  685. struct blkio_policy_type *pol;
  686. list_for_each_entry(pol, &blkio_list, list) {
  687. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  688. struct blkio_group_stats *stats = &pd->stats;
  689. /* queued stats shouldn't be cleared */
  690. for (i = 0; i < ARRAY_SIZE(stats->stat_arr); i++)
  691. if (i != BLKIO_STAT_QUEUED)
  692. memset(stats->stat_arr[i], 0,
  693. sizeof(stats->stat_arr[i]));
  694. stats->time = 0;
  695. #ifdef CONFIG_DEBUG_BLK_CGROUP
  696. memset((void *)stats + BLKG_STATS_DEBUG_CLEAR_START, 0,
  697. BLKG_STATS_DEBUG_CLEAR_SIZE);
  698. #endif
  699. blkio_reset_stats_cpu(blkg, pol->plid);
  700. }
  701. }
  702. spin_unlock_irq(&blkcg->lock);
  703. spin_unlock(&blkio_list_lock);
  704. return 0;
  705. }
  706. static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
  707. char *str, int chars_left, bool diskname_only)
  708. {
  709. snprintf(str, chars_left, "%s", dname);
  710. chars_left -= strlen(str);
  711. if (chars_left <= 0) {
  712. printk(KERN_WARNING
  713. "Possibly incorrect cgroup stat display format");
  714. return;
  715. }
  716. if (diskname_only)
  717. return;
  718. switch (type) {
  719. case BLKIO_STAT_READ:
  720. strlcat(str, " Read", chars_left);
  721. break;
  722. case BLKIO_STAT_WRITE:
  723. strlcat(str, " Write", chars_left);
  724. break;
  725. case BLKIO_STAT_SYNC:
  726. strlcat(str, " Sync", chars_left);
  727. break;
  728. case BLKIO_STAT_ASYNC:
  729. strlcat(str, " Async", chars_left);
  730. break;
  731. case BLKIO_STAT_TOTAL:
  732. strlcat(str, " Total", chars_left);
  733. break;
  734. default:
  735. strlcat(str, " Invalid", chars_left);
  736. }
  737. }
  738. static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
  739. enum stat_type_cpu type, enum stat_sub_type sub_type)
  740. {
  741. struct blkg_policy_data *pd = blkg->pd[plid];
  742. int cpu;
  743. struct blkio_group_stats_cpu *stats_cpu;
  744. u64 val = 0, tval;
  745. if (pd->stats_cpu == NULL)
  746. return val;
  747. for_each_possible_cpu(cpu) {
  748. unsigned int start;
  749. stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
  750. do {
  751. start = u64_stats_fetch_begin(&stats_cpu->syncp);
  752. if (type == BLKIO_STAT_CPU_SECTORS)
  753. tval = stats_cpu->sectors;
  754. else
  755. tval = stats_cpu->stat_arr_cpu[type][sub_type];
  756. } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
  757. val += tval;
  758. }
  759. return val;
  760. }
  761. static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
  762. struct cgroup_map_cb *cb, const char *dname,
  763. enum stat_type_cpu type)
  764. {
  765. uint64_t disk_total, val;
  766. char key_str[MAX_KEY_LEN];
  767. enum stat_sub_type sub_type;
  768. if (type == BLKIO_STAT_CPU_SECTORS) {
  769. val = blkio_read_stat_cpu(blkg, plid, type, 0);
  770. blkio_get_key_name(0, dname, key_str, MAX_KEY_LEN, true);
  771. cb->fill(cb, key_str, val);
  772. return val;
  773. }
  774. for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
  775. sub_type++) {
  776. blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
  777. false);
  778. val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
  779. cb->fill(cb, key_str, val);
  780. }
  781. disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
  782. blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
  783. blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
  784. false);
  785. cb->fill(cb, key_str, disk_total);
  786. return disk_total;
  787. }
  788. /* This should be called with blkg->stats_lock held */
  789. static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
  790. struct cgroup_map_cb *cb, const char *dname,
  791. enum stat_type type)
  792. {
  793. struct blkio_group_stats *stats = &blkg->pd[plid]->stats;
  794. uint64_t v = 0, disk_total = 0;
  795. char key_str[MAX_KEY_LEN];
  796. int st;
  797. if (type >= BLKIO_STAT_ARR_NR) {
  798. switch (type) {
  799. case BLKIO_STAT_TIME:
  800. v = stats->time;
  801. break;
  802. #ifdef CONFIG_DEBUG_BLK_CGROUP
  803. case BLKIO_STAT_UNACCOUNTED_TIME:
  804. v = stats->unaccounted_time;
  805. break;
  806. case BLKIO_STAT_AVG_QUEUE_SIZE: {
  807. uint64_t samples = stats->avg_queue_size_samples;
  808. if (samples) {
  809. v = stats->avg_queue_size_sum;
  810. do_div(v, samples);
  811. }
  812. break;
  813. }
  814. case BLKIO_STAT_IDLE_TIME:
  815. v = stats->idle_time;
  816. break;
  817. case BLKIO_STAT_EMPTY_TIME:
  818. v = stats->empty_time;
  819. break;
  820. case BLKIO_STAT_DEQUEUE:
  821. v = stats->dequeue;
  822. break;
  823. case BLKIO_STAT_GROUP_WAIT_TIME:
  824. v = stats->group_wait_time;
  825. break;
  826. #endif
  827. default:
  828. WARN_ON_ONCE(1);
  829. }
  830. blkio_get_key_name(0, dname, key_str, MAX_KEY_LEN, true);
  831. cb->fill(cb, key_str, v);
  832. return v;
  833. }
  834. for (st = BLKIO_STAT_READ; st < BLKIO_STAT_TOTAL; st++) {
  835. v = stats->stat_arr[type][st];
  836. blkio_get_key_name(st, dname, key_str, MAX_KEY_LEN, false);
  837. cb->fill(cb, key_str, v);
  838. if (st == BLKIO_STAT_READ || st == BLKIO_STAT_WRITE)
  839. disk_total += v;
  840. }
  841. blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
  842. false);
  843. cb->fill(cb, key_str, disk_total);
  844. return disk_total;
  845. }
  846. static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
  847. int fileid, struct blkio_cgroup *blkcg)
  848. {
  849. struct gendisk *disk = NULL;
  850. struct blkio_group *blkg = NULL;
  851. struct blkg_policy_data *pd;
  852. char *s[4], *p, *major_s = NULL, *minor_s = NULL;
  853. unsigned long major, minor;
  854. int i = 0, ret = -EINVAL;
  855. int part;
  856. dev_t dev;
  857. u64 temp;
  858. memset(s, 0, sizeof(s));
  859. while ((p = strsep(&buf, " ")) != NULL) {
  860. if (!*p)
  861. continue;
  862. s[i++] = p;
  863. /* Prevent from inputing too many things */
  864. if (i == 3)
  865. break;
  866. }
  867. if (i != 2)
  868. goto out;
  869. p = strsep(&s[0], ":");
  870. if (p != NULL)
  871. major_s = p;
  872. else
  873. goto out;
  874. minor_s = s[0];
  875. if (!minor_s)
  876. goto out;
  877. if (strict_strtoul(major_s, 10, &major))
  878. goto out;
  879. if (strict_strtoul(minor_s, 10, &minor))
  880. goto out;
  881. dev = MKDEV(major, minor);
  882. if (strict_strtoull(s[1], 10, &temp))
  883. goto out;
  884. disk = get_gendisk(dev, &part);
  885. if (!disk || part)
  886. goto out;
  887. rcu_read_lock();
  888. spin_lock_irq(disk->queue->queue_lock);
  889. blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
  890. spin_unlock_irq(disk->queue->queue_lock);
  891. if (IS_ERR(blkg)) {
  892. ret = PTR_ERR(blkg);
  893. goto out_unlock;
  894. }
  895. pd = blkg->pd[plid];
  896. switch (plid) {
  897. case BLKIO_POLICY_PROP:
  898. if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
  899. temp > BLKIO_WEIGHT_MAX)
  900. goto out_unlock;
  901. pd->conf.weight = temp;
  902. blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
  903. break;
  904. case BLKIO_POLICY_THROTL:
  905. switch(fileid) {
  906. case BLKIO_THROTL_read_bps_device:
  907. pd->conf.bps[READ] = temp;
  908. blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
  909. break;
  910. case BLKIO_THROTL_write_bps_device:
  911. pd->conf.bps[WRITE] = temp;
  912. blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
  913. break;
  914. case BLKIO_THROTL_read_iops_device:
  915. if (temp > THROTL_IOPS_MAX)
  916. goto out_unlock;
  917. pd->conf.iops[READ] = temp;
  918. blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
  919. break;
  920. case BLKIO_THROTL_write_iops_device:
  921. if (temp > THROTL_IOPS_MAX)
  922. goto out_unlock;
  923. pd->conf.iops[WRITE] = temp;
  924. blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
  925. break;
  926. }
  927. break;
  928. default:
  929. BUG();
  930. }
  931. ret = 0;
  932. out_unlock:
  933. rcu_read_unlock();
  934. out:
  935. put_disk(disk);
  936. /*
  937. * If queue was bypassing, we should retry. Do so after a short
  938. * msleep(). It isn't strictly necessary but queue can be
  939. * bypassing for some time and it's always nice to avoid busy
  940. * looping.
  941. */
  942. if (ret == -EBUSY) {
  943. msleep(10);
  944. return restart_syscall();
  945. }
  946. return ret;
  947. }
  948. static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
  949. const char *buffer)
  950. {
  951. int ret = 0;
  952. char *buf;
  953. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  954. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  955. int fileid = BLKIOFILE_ATTR(cft->private);
  956. buf = kstrdup(buffer, GFP_KERNEL);
  957. if (!buf)
  958. return -ENOMEM;
  959. ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
  960. kfree(buf);
  961. return ret;
  962. }
  963. static const char *blkg_dev_name(struct blkio_group *blkg)
  964. {
  965. /* some drivers (floppy) instantiate a queue w/o disk registered */
  966. if (blkg->q->backing_dev_info.dev)
  967. return dev_name(blkg->q->backing_dev_info.dev);
  968. return NULL;
  969. }
  970. static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
  971. struct seq_file *m)
  972. {
  973. int plid = BLKIOFILE_POLICY(cft->private);
  974. int fileid = BLKIOFILE_ATTR(cft->private);
  975. struct blkg_policy_data *pd = blkg->pd[plid];
  976. const char *dname = blkg_dev_name(blkg);
  977. int rw = WRITE;
  978. if (!dname)
  979. return;
  980. switch (plid) {
  981. case BLKIO_POLICY_PROP:
  982. if (pd->conf.weight)
  983. seq_printf(m, "%s\t%u\n",
  984. dname, pd->conf.weight);
  985. break;
  986. case BLKIO_POLICY_THROTL:
  987. switch (fileid) {
  988. case BLKIO_THROTL_read_bps_device:
  989. rw = READ;
  990. case BLKIO_THROTL_write_bps_device:
  991. if (pd->conf.bps[rw])
  992. seq_printf(m, "%s\t%llu\n",
  993. dname, pd->conf.bps[rw]);
  994. break;
  995. case BLKIO_THROTL_read_iops_device:
  996. rw = READ;
  997. case BLKIO_THROTL_write_iops_device:
  998. if (pd->conf.iops[rw])
  999. seq_printf(m, "%s\t%u\n",
  1000. dname, pd->conf.iops[rw]);
  1001. break;
  1002. }
  1003. break;
  1004. default:
  1005. BUG();
  1006. }
  1007. }
  1008. /* cgroup files which read their data from policy nodes end up here */
  1009. static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
  1010. struct seq_file *m)
  1011. {
  1012. struct blkio_group *blkg;
  1013. struct hlist_node *n;
  1014. spin_lock_irq(&blkcg->lock);
  1015. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
  1016. blkio_print_group_conf(cft, blkg, m);
  1017. spin_unlock_irq(&blkcg->lock);
  1018. }
  1019. static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
  1020. struct seq_file *m)
  1021. {
  1022. struct blkio_cgroup *blkcg;
  1023. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1024. int name = BLKIOFILE_ATTR(cft->private);
  1025. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1026. switch(plid) {
  1027. case BLKIO_POLICY_PROP:
  1028. switch(name) {
  1029. case BLKIO_PROP_weight_device:
  1030. blkio_read_conf(cft, blkcg, m);
  1031. return 0;
  1032. default:
  1033. BUG();
  1034. }
  1035. break;
  1036. case BLKIO_POLICY_THROTL:
  1037. switch(name){
  1038. case BLKIO_THROTL_read_bps_device:
  1039. case BLKIO_THROTL_write_bps_device:
  1040. case BLKIO_THROTL_read_iops_device:
  1041. case BLKIO_THROTL_write_iops_device:
  1042. blkio_read_conf(cft, blkcg, m);
  1043. return 0;
  1044. default:
  1045. BUG();
  1046. }
  1047. break;
  1048. default:
  1049. BUG();
  1050. }
  1051. return 0;
  1052. }
  1053. static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
  1054. struct cftype *cft, struct cgroup_map_cb *cb,
  1055. enum stat_type type, bool show_total, bool pcpu)
  1056. {
  1057. struct blkio_group *blkg;
  1058. struct hlist_node *n;
  1059. uint64_t cgroup_total = 0;
  1060. spin_lock_irq(&blkcg->lock);
  1061. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  1062. const char *dname = blkg_dev_name(blkg);
  1063. int plid = BLKIOFILE_POLICY(cft->private);
  1064. if (!dname)
  1065. continue;
  1066. if (pcpu) {
  1067. cgroup_total += blkio_get_stat_cpu(blkg, plid,
  1068. cb, dname, type);
  1069. } else {
  1070. spin_lock(&blkg->stats_lock);
  1071. cgroup_total += blkio_get_stat(blkg, plid,
  1072. cb, dname, type);
  1073. spin_unlock(&blkg->stats_lock);
  1074. }
  1075. }
  1076. if (show_total)
  1077. cb->fill(cb, "Total", cgroup_total);
  1078. spin_unlock_irq(&blkcg->lock);
  1079. return 0;
  1080. }
  1081. /* All map kind of cgroup file get serviced by this function */
  1082. static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
  1083. struct cgroup_map_cb *cb)
  1084. {
  1085. struct blkio_cgroup *blkcg;
  1086. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1087. int name = BLKIOFILE_ATTR(cft->private);
  1088. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1089. switch(plid) {
  1090. case BLKIO_POLICY_PROP:
  1091. switch(name) {
  1092. case BLKIO_PROP_time:
  1093. return blkio_read_blkg_stats(blkcg, cft, cb,
  1094. BLKIO_STAT_TIME, 0, 0);
  1095. case BLKIO_PROP_sectors:
  1096. return blkio_read_blkg_stats(blkcg, cft, cb,
  1097. BLKIO_STAT_CPU_SECTORS, 0, 1);
  1098. case BLKIO_PROP_io_service_bytes:
  1099. return blkio_read_blkg_stats(blkcg, cft, cb,
  1100. BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
  1101. case BLKIO_PROP_io_serviced:
  1102. return blkio_read_blkg_stats(blkcg, cft, cb,
  1103. BLKIO_STAT_CPU_SERVICED, 1, 1);
  1104. case BLKIO_PROP_io_service_time:
  1105. return blkio_read_blkg_stats(blkcg, cft, cb,
  1106. BLKIO_STAT_SERVICE_TIME, 1, 0);
  1107. case BLKIO_PROP_io_wait_time:
  1108. return blkio_read_blkg_stats(blkcg, cft, cb,
  1109. BLKIO_STAT_WAIT_TIME, 1, 0);
  1110. case BLKIO_PROP_io_merged:
  1111. return blkio_read_blkg_stats(blkcg, cft, cb,
  1112. BLKIO_STAT_MERGED, 1, 0);
  1113. case BLKIO_PROP_io_queued:
  1114. return blkio_read_blkg_stats(blkcg, cft, cb,
  1115. BLKIO_STAT_QUEUED, 1, 0);
  1116. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1117. case BLKIO_PROP_unaccounted_time:
  1118. return blkio_read_blkg_stats(blkcg, cft, cb,
  1119. BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
  1120. case BLKIO_PROP_dequeue:
  1121. return blkio_read_blkg_stats(blkcg, cft, cb,
  1122. BLKIO_STAT_DEQUEUE, 0, 0);
  1123. case BLKIO_PROP_avg_queue_size:
  1124. return blkio_read_blkg_stats(blkcg, cft, cb,
  1125. BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
  1126. case BLKIO_PROP_group_wait_time:
  1127. return blkio_read_blkg_stats(blkcg, cft, cb,
  1128. BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
  1129. case BLKIO_PROP_idle_time:
  1130. return blkio_read_blkg_stats(blkcg, cft, cb,
  1131. BLKIO_STAT_IDLE_TIME, 0, 0);
  1132. case BLKIO_PROP_empty_time:
  1133. return blkio_read_blkg_stats(blkcg, cft, cb,
  1134. BLKIO_STAT_EMPTY_TIME, 0, 0);
  1135. #endif
  1136. default:
  1137. BUG();
  1138. }
  1139. break;
  1140. case BLKIO_POLICY_THROTL:
  1141. switch(name){
  1142. case BLKIO_THROTL_io_service_bytes:
  1143. return blkio_read_blkg_stats(blkcg, cft, cb,
  1144. BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
  1145. case BLKIO_THROTL_io_serviced:
  1146. return blkio_read_blkg_stats(blkcg, cft, cb,
  1147. BLKIO_STAT_CPU_SERVICED, 1, 1);
  1148. default:
  1149. BUG();
  1150. }
  1151. break;
  1152. default:
  1153. BUG();
  1154. }
  1155. return 0;
  1156. }
  1157. static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
  1158. {
  1159. struct blkio_group *blkg;
  1160. struct hlist_node *n;
  1161. if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
  1162. return -EINVAL;
  1163. spin_lock(&blkio_list_lock);
  1164. spin_lock_irq(&blkcg->lock);
  1165. blkcg->weight = (unsigned int)val;
  1166. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  1167. struct blkg_policy_data *pd = blkg->pd[plid];
  1168. if (!pd->conf.weight)
  1169. blkio_update_group_weight(blkg, plid, blkcg->weight);
  1170. }
  1171. spin_unlock_irq(&blkcg->lock);
  1172. spin_unlock(&blkio_list_lock);
  1173. return 0;
  1174. }
  1175. static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
  1176. struct blkio_cgroup *blkcg;
  1177. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1178. int name = BLKIOFILE_ATTR(cft->private);
  1179. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1180. switch(plid) {
  1181. case BLKIO_POLICY_PROP:
  1182. switch(name) {
  1183. case BLKIO_PROP_weight:
  1184. return (u64)blkcg->weight;
  1185. }
  1186. break;
  1187. default:
  1188. BUG();
  1189. }
  1190. return 0;
  1191. }
  1192. static int
  1193. blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
  1194. {
  1195. struct blkio_cgroup *blkcg;
  1196. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1197. int name = BLKIOFILE_ATTR(cft->private);
  1198. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1199. switch(plid) {
  1200. case BLKIO_POLICY_PROP:
  1201. switch(name) {
  1202. case BLKIO_PROP_weight:
  1203. return blkio_weight_write(blkcg, plid, val);
  1204. }
  1205. break;
  1206. default:
  1207. BUG();
  1208. }
  1209. return 0;
  1210. }
  1211. struct cftype blkio_files[] = {
  1212. {
  1213. .name = "weight_device",
  1214. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1215. BLKIO_PROP_weight_device),
  1216. .read_seq_string = blkiocg_file_read,
  1217. .write_string = blkiocg_file_write,
  1218. .max_write_len = 256,
  1219. },
  1220. {
  1221. .name = "weight",
  1222. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1223. BLKIO_PROP_weight),
  1224. .read_u64 = blkiocg_file_read_u64,
  1225. .write_u64 = blkiocg_file_write_u64,
  1226. },
  1227. {
  1228. .name = "time",
  1229. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1230. BLKIO_PROP_time),
  1231. .read_map = blkiocg_file_read_map,
  1232. },
  1233. {
  1234. .name = "sectors",
  1235. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1236. BLKIO_PROP_sectors),
  1237. .read_map = blkiocg_file_read_map,
  1238. },
  1239. {
  1240. .name = "io_service_bytes",
  1241. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1242. BLKIO_PROP_io_service_bytes),
  1243. .read_map = blkiocg_file_read_map,
  1244. },
  1245. {
  1246. .name = "io_serviced",
  1247. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1248. BLKIO_PROP_io_serviced),
  1249. .read_map = blkiocg_file_read_map,
  1250. },
  1251. {
  1252. .name = "io_service_time",
  1253. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1254. BLKIO_PROP_io_service_time),
  1255. .read_map = blkiocg_file_read_map,
  1256. },
  1257. {
  1258. .name = "io_wait_time",
  1259. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1260. BLKIO_PROP_io_wait_time),
  1261. .read_map = blkiocg_file_read_map,
  1262. },
  1263. {
  1264. .name = "io_merged",
  1265. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1266. BLKIO_PROP_io_merged),
  1267. .read_map = blkiocg_file_read_map,
  1268. },
  1269. {
  1270. .name = "io_queued",
  1271. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1272. BLKIO_PROP_io_queued),
  1273. .read_map = blkiocg_file_read_map,
  1274. },
  1275. {
  1276. .name = "reset_stats",
  1277. .write_u64 = blkiocg_reset_stats,
  1278. },
  1279. #ifdef CONFIG_BLK_DEV_THROTTLING
  1280. {
  1281. .name = "throttle.read_bps_device",
  1282. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1283. BLKIO_THROTL_read_bps_device),
  1284. .read_seq_string = blkiocg_file_read,
  1285. .write_string = blkiocg_file_write,
  1286. .max_write_len = 256,
  1287. },
  1288. {
  1289. .name = "throttle.write_bps_device",
  1290. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1291. BLKIO_THROTL_write_bps_device),
  1292. .read_seq_string = blkiocg_file_read,
  1293. .write_string = blkiocg_file_write,
  1294. .max_write_len = 256,
  1295. },
  1296. {
  1297. .name = "throttle.read_iops_device",
  1298. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1299. BLKIO_THROTL_read_iops_device),
  1300. .read_seq_string = blkiocg_file_read,
  1301. .write_string = blkiocg_file_write,
  1302. .max_write_len = 256,
  1303. },
  1304. {
  1305. .name = "throttle.write_iops_device",
  1306. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1307. BLKIO_THROTL_write_iops_device),
  1308. .read_seq_string = blkiocg_file_read,
  1309. .write_string = blkiocg_file_write,
  1310. .max_write_len = 256,
  1311. },
  1312. {
  1313. .name = "throttle.io_service_bytes",
  1314. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1315. BLKIO_THROTL_io_service_bytes),
  1316. .read_map = blkiocg_file_read_map,
  1317. },
  1318. {
  1319. .name = "throttle.io_serviced",
  1320. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1321. BLKIO_THROTL_io_serviced),
  1322. .read_map = blkiocg_file_read_map,
  1323. },
  1324. #endif /* CONFIG_BLK_DEV_THROTTLING */
  1325. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1326. {
  1327. .name = "avg_queue_size",
  1328. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1329. BLKIO_PROP_avg_queue_size),
  1330. .read_map = blkiocg_file_read_map,
  1331. },
  1332. {
  1333. .name = "group_wait_time",
  1334. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1335. BLKIO_PROP_group_wait_time),
  1336. .read_map = blkiocg_file_read_map,
  1337. },
  1338. {
  1339. .name = "idle_time",
  1340. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1341. BLKIO_PROP_idle_time),
  1342. .read_map = blkiocg_file_read_map,
  1343. },
  1344. {
  1345. .name = "empty_time",
  1346. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1347. BLKIO_PROP_empty_time),
  1348. .read_map = blkiocg_file_read_map,
  1349. },
  1350. {
  1351. .name = "dequeue",
  1352. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1353. BLKIO_PROP_dequeue),
  1354. .read_map = blkiocg_file_read_map,
  1355. },
  1356. {
  1357. .name = "unaccounted_time",
  1358. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1359. BLKIO_PROP_unaccounted_time),
  1360. .read_map = blkiocg_file_read_map,
  1361. },
  1362. #endif
  1363. };
  1364. static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1365. {
  1366. return cgroup_add_files(cgroup, subsys, blkio_files,
  1367. ARRAY_SIZE(blkio_files));
  1368. }
  1369. /**
  1370. * blkiocg_pre_destroy - cgroup pre_destroy callback
  1371. * @subsys: cgroup subsys
  1372. * @cgroup: cgroup of interest
  1373. *
  1374. * This function is called when @cgroup is about to go away and responsible
  1375. * for shooting down all blkgs associated with @cgroup. blkgs should be
  1376. * removed while holding both q and blkcg locks. As blkcg lock is nested
  1377. * inside q lock, this function performs reverse double lock dancing.
  1378. *
  1379. * This is the blkcg counterpart of ioc_release_fn().
  1380. */
  1381. static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
  1382. struct cgroup *cgroup)
  1383. {
  1384. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  1385. spin_lock_irq(&blkcg->lock);
  1386. while (!hlist_empty(&blkcg->blkg_list)) {
  1387. struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
  1388. struct blkio_group, blkcg_node);
  1389. struct request_queue *q = blkg->q;
  1390. if (spin_trylock(q->queue_lock)) {
  1391. blkg_destroy(blkg);
  1392. spin_unlock(q->queue_lock);
  1393. } else {
  1394. spin_unlock_irq(&blkcg->lock);
  1395. cpu_relax();
  1396. spin_lock(&blkcg->lock);
  1397. }
  1398. }
  1399. spin_unlock_irq(&blkcg->lock);
  1400. return 0;
  1401. }
  1402. static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1403. {
  1404. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  1405. if (blkcg != &blkio_root_cgroup)
  1406. kfree(blkcg);
  1407. }
  1408. static struct cgroup_subsys_state *
  1409. blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1410. {
  1411. struct blkio_cgroup *blkcg;
  1412. struct cgroup *parent = cgroup->parent;
  1413. if (!parent) {
  1414. blkcg = &blkio_root_cgroup;
  1415. goto done;
  1416. }
  1417. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  1418. if (!blkcg)
  1419. return ERR_PTR(-ENOMEM);
  1420. blkcg->weight = BLKIO_WEIGHT_DEFAULT;
  1421. done:
  1422. spin_lock_init(&blkcg->lock);
  1423. INIT_HLIST_HEAD(&blkcg->blkg_list);
  1424. return &blkcg->css;
  1425. }
  1426. /**
  1427. * blkcg_init_queue - initialize blkcg part of request queue
  1428. * @q: request_queue to initialize
  1429. *
  1430. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  1431. * part of new request_queue @q.
  1432. *
  1433. * RETURNS:
  1434. * 0 on success, -errno on failure.
  1435. */
  1436. int blkcg_init_queue(struct request_queue *q)
  1437. {
  1438. int ret;
  1439. might_sleep();
  1440. ret = blk_throtl_init(q);
  1441. if (ret)
  1442. return ret;
  1443. mutex_lock(&all_q_mutex);
  1444. INIT_LIST_HEAD(&q->all_q_node);
  1445. list_add_tail(&q->all_q_node, &all_q_list);
  1446. mutex_unlock(&all_q_mutex);
  1447. return 0;
  1448. }
  1449. /**
  1450. * blkcg_drain_queue - drain blkcg part of request_queue
  1451. * @q: request_queue to drain
  1452. *
  1453. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  1454. */
  1455. void blkcg_drain_queue(struct request_queue *q)
  1456. {
  1457. lockdep_assert_held(q->queue_lock);
  1458. blk_throtl_drain(q);
  1459. }
  1460. /**
  1461. * blkcg_exit_queue - exit and release blkcg part of request_queue
  1462. * @q: request_queue being released
  1463. *
  1464. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  1465. */
  1466. void blkcg_exit_queue(struct request_queue *q)
  1467. {
  1468. mutex_lock(&all_q_mutex);
  1469. list_del_init(&q->all_q_node);
  1470. mutex_unlock(&all_q_mutex);
  1471. blkg_destroy_all(q, true);
  1472. blk_throtl_exit(q);
  1473. }
  1474. /*
  1475. * We cannot support shared io contexts, as we have no mean to support
  1476. * two tasks with the same ioc in two different groups without major rework
  1477. * of the main cic data structures. For now we allow a task to change
  1478. * its cgroup only if it's the only owner of its ioc.
  1479. */
  1480. static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  1481. struct cgroup_taskset *tset)
  1482. {
  1483. struct task_struct *task;
  1484. struct io_context *ioc;
  1485. int ret = 0;
  1486. /* task_lock() is needed to avoid races with exit_io_context() */
  1487. cgroup_taskset_for_each(task, cgrp, tset) {
  1488. task_lock(task);
  1489. ioc = task->io_context;
  1490. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  1491. ret = -EINVAL;
  1492. task_unlock(task);
  1493. if (ret)
  1494. break;
  1495. }
  1496. return ret;
  1497. }
  1498. static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  1499. struct cgroup_taskset *tset)
  1500. {
  1501. struct task_struct *task;
  1502. struct io_context *ioc;
  1503. cgroup_taskset_for_each(task, cgrp, tset) {
  1504. /* we don't lose anything even if ioc allocation fails */
  1505. ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
  1506. if (ioc) {
  1507. ioc_cgroup_changed(ioc);
  1508. put_io_context(ioc);
  1509. }
  1510. }
  1511. }
  1512. static void blkcg_bypass_start(void)
  1513. __acquires(&all_q_mutex)
  1514. {
  1515. struct request_queue *q;
  1516. mutex_lock(&all_q_mutex);
  1517. list_for_each_entry(q, &all_q_list, all_q_node) {
  1518. blk_queue_bypass_start(q);
  1519. blkg_destroy_all(q, false);
  1520. }
  1521. }
  1522. static void blkcg_bypass_end(void)
  1523. __releases(&all_q_mutex)
  1524. {
  1525. struct request_queue *q;
  1526. list_for_each_entry(q, &all_q_list, all_q_node)
  1527. blk_queue_bypass_end(q);
  1528. mutex_unlock(&all_q_mutex);
  1529. }
  1530. void blkio_policy_register(struct blkio_policy_type *blkiop)
  1531. {
  1532. struct request_queue *q;
  1533. blkcg_bypass_start();
  1534. spin_lock(&blkio_list_lock);
  1535. BUG_ON(blkio_policy[blkiop->plid]);
  1536. blkio_policy[blkiop->plid] = blkiop;
  1537. list_add_tail(&blkiop->list, &blkio_list);
  1538. spin_unlock(&blkio_list_lock);
  1539. list_for_each_entry(q, &all_q_list, all_q_node)
  1540. update_root_blkg_pd(q, blkiop->plid);
  1541. blkcg_bypass_end();
  1542. }
  1543. EXPORT_SYMBOL_GPL(blkio_policy_register);
  1544. void blkio_policy_unregister(struct blkio_policy_type *blkiop)
  1545. {
  1546. struct request_queue *q;
  1547. blkcg_bypass_start();
  1548. spin_lock(&blkio_list_lock);
  1549. BUG_ON(blkio_policy[blkiop->plid] != blkiop);
  1550. blkio_policy[blkiop->plid] = NULL;
  1551. list_del_init(&blkiop->list);
  1552. spin_unlock(&blkio_list_lock);
  1553. list_for_each_entry(q, &all_q_list, all_q_node)
  1554. update_root_blkg_pd(q, blkiop->plid);
  1555. blkcg_bypass_end();
  1556. }
  1557. EXPORT_SYMBOL_GPL(blkio_policy_unregister);