blk-cgroup.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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_fill_stat(char *str, int chars_left, uint64_t val,
  739. struct cgroup_map_cb *cb, const char *dname)
  740. {
  741. blkio_get_key_name(0, dname, str, chars_left, true);
  742. cb->fill(cb, str, val);
  743. return val;
  744. }
  745. static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
  746. enum stat_type_cpu type, enum stat_sub_type sub_type)
  747. {
  748. struct blkg_policy_data *pd = blkg->pd[plid];
  749. int cpu;
  750. struct blkio_group_stats_cpu *stats_cpu;
  751. u64 val = 0, tval;
  752. if (pd->stats_cpu == NULL)
  753. return val;
  754. for_each_possible_cpu(cpu) {
  755. unsigned int start;
  756. stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
  757. do {
  758. start = u64_stats_fetch_begin(&stats_cpu->syncp);
  759. if (type == BLKIO_STAT_CPU_SECTORS)
  760. tval = stats_cpu->sectors;
  761. else
  762. tval = stats_cpu->stat_arr_cpu[type][sub_type];
  763. } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
  764. val += tval;
  765. }
  766. return val;
  767. }
  768. static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
  769. struct cgroup_map_cb *cb, const char *dname,
  770. enum stat_type_cpu type)
  771. {
  772. uint64_t disk_total, val;
  773. char key_str[MAX_KEY_LEN];
  774. enum stat_sub_type sub_type;
  775. if (type == BLKIO_STAT_CPU_SECTORS) {
  776. val = blkio_read_stat_cpu(blkg, plid, type, 0);
  777. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
  778. dname);
  779. }
  780. for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
  781. sub_type++) {
  782. blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
  783. false);
  784. val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
  785. cb->fill(cb, key_str, val);
  786. }
  787. disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
  788. blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
  789. blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
  790. false);
  791. cb->fill(cb, key_str, disk_total);
  792. return disk_total;
  793. }
  794. /* This should be called with blkg->stats_lock held */
  795. static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
  796. struct cgroup_map_cb *cb, const char *dname,
  797. enum stat_type type)
  798. {
  799. struct blkg_policy_data *pd = blkg->pd[plid];
  800. uint64_t disk_total;
  801. char key_str[MAX_KEY_LEN];
  802. enum stat_sub_type sub_type;
  803. if (type == BLKIO_STAT_TIME)
  804. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  805. pd->stats.time, cb, dname);
  806. #ifdef CONFIG_DEBUG_BLK_CGROUP
  807. if (type == BLKIO_STAT_UNACCOUNTED_TIME)
  808. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  809. pd->stats.unaccounted_time, cb, dname);
  810. if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
  811. uint64_t sum = pd->stats.avg_queue_size_sum;
  812. uint64_t samples = pd->stats.avg_queue_size_samples;
  813. if (samples)
  814. do_div(sum, samples);
  815. else
  816. sum = 0;
  817. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  818. sum, cb, dname);
  819. }
  820. if (type == BLKIO_STAT_GROUP_WAIT_TIME)
  821. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  822. pd->stats.group_wait_time, cb, dname);
  823. if (type == BLKIO_STAT_IDLE_TIME)
  824. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  825. pd->stats.idle_time, cb, dname);
  826. if (type == BLKIO_STAT_EMPTY_TIME)
  827. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  828. pd->stats.empty_time, cb, dname);
  829. if (type == BLKIO_STAT_DEQUEUE)
  830. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  831. pd->stats.dequeue, cb, dname);
  832. #endif
  833. for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
  834. sub_type++) {
  835. blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
  836. false);
  837. cb->fill(cb, key_str, pd->stats.stat_arr[type][sub_type]);
  838. }
  839. disk_total = pd->stats.stat_arr[type][BLKIO_STAT_READ] +
  840. pd->stats.stat_arr[type][BLKIO_STAT_WRITE];
  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);