blk-cgroup.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  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. struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
  30. EXPORT_SYMBOL_GPL(blkio_root_cgroup);
  31. static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
  32. static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
  33. struct cgroup *);
  34. static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
  35. struct cgroup_taskset *);
  36. static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
  37. struct cgroup_taskset *);
  38. static int blkiocg_pre_destroy(struct cgroup_subsys *, struct cgroup *);
  39. static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
  40. static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
  41. /* for encoding cft->private value on file */
  42. #define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
  43. /* What policy owns the file, proportional or throttle */
  44. #define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
  45. #define BLKIOFILE_ATTR(val) ((val) & 0xffff)
  46. struct cgroup_subsys blkio_subsys = {
  47. .name = "blkio",
  48. .create = blkiocg_create,
  49. .can_attach = blkiocg_can_attach,
  50. .attach = blkiocg_attach,
  51. .pre_destroy = blkiocg_pre_destroy,
  52. .destroy = blkiocg_destroy,
  53. .populate = blkiocg_populate,
  54. .subsys_id = blkio_subsys_id,
  55. .module = THIS_MODULE,
  56. };
  57. EXPORT_SYMBOL_GPL(blkio_subsys);
  58. struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
  59. {
  60. return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
  61. struct blkio_cgroup, css);
  62. }
  63. EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
  64. struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
  65. {
  66. return container_of(task_subsys_state(tsk, blkio_subsys_id),
  67. struct blkio_cgroup, css);
  68. }
  69. EXPORT_SYMBOL_GPL(task_blkio_cgroup);
  70. static inline void blkio_update_group_weight(struct blkio_group *blkg,
  71. int plid, unsigned int weight)
  72. {
  73. struct blkio_policy_type *blkiop;
  74. list_for_each_entry(blkiop, &blkio_list, list) {
  75. /* If this policy does not own the blkg, do not send updates */
  76. if (blkiop->plid != plid)
  77. continue;
  78. if (blkiop->ops.blkio_update_group_weight_fn)
  79. blkiop->ops.blkio_update_group_weight_fn(blkg->q,
  80. blkg, weight);
  81. }
  82. }
  83. static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid,
  84. u64 bps, int fileid)
  85. {
  86. struct blkio_policy_type *blkiop;
  87. list_for_each_entry(blkiop, &blkio_list, list) {
  88. /* If this policy does not own the blkg, do not send updates */
  89. if (blkiop->plid != plid)
  90. continue;
  91. if (fileid == BLKIO_THROTL_read_bps_device
  92. && blkiop->ops.blkio_update_group_read_bps_fn)
  93. blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
  94. blkg, bps);
  95. if (fileid == BLKIO_THROTL_write_bps_device
  96. && blkiop->ops.blkio_update_group_write_bps_fn)
  97. blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
  98. blkg, bps);
  99. }
  100. }
  101. static inline void blkio_update_group_iops(struct blkio_group *blkg,
  102. int plid, unsigned int iops,
  103. int fileid)
  104. {
  105. struct blkio_policy_type *blkiop;
  106. list_for_each_entry(blkiop, &blkio_list, list) {
  107. /* If this policy does not own the blkg, do not send updates */
  108. if (blkiop->plid != plid)
  109. continue;
  110. if (fileid == BLKIO_THROTL_read_iops_device
  111. && blkiop->ops.blkio_update_group_read_iops_fn)
  112. blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
  113. blkg, iops);
  114. if (fileid == BLKIO_THROTL_write_iops_device
  115. && blkiop->ops.blkio_update_group_write_iops_fn)
  116. blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
  117. blkg,iops);
  118. }
  119. }
  120. /*
  121. * Add to the appropriate stat variable depending on the request type.
  122. * This should be called with the blkg->stats_lock held.
  123. */
  124. static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
  125. bool sync)
  126. {
  127. if (direction)
  128. stat[BLKIO_STAT_WRITE] += add;
  129. else
  130. stat[BLKIO_STAT_READ] += add;
  131. if (sync)
  132. stat[BLKIO_STAT_SYNC] += add;
  133. else
  134. stat[BLKIO_STAT_ASYNC] += add;
  135. }
  136. /*
  137. * Decrements the appropriate stat variable if non-zero depending on the
  138. * request type. Panics on value being zero.
  139. * This should be called with the blkg->stats_lock held.
  140. */
  141. static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
  142. {
  143. if (direction) {
  144. BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
  145. stat[BLKIO_STAT_WRITE]--;
  146. } else {
  147. BUG_ON(stat[BLKIO_STAT_READ] == 0);
  148. stat[BLKIO_STAT_READ]--;
  149. }
  150. if (sync) {
  151. BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
  152. stat[BLKIO_STAT_SYNC]--;
  153. } else {
  154. BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
  155. stat[BLKIO_STAT_ASYNC]--;
  156. }
  157. }
  158. #ifdef CONFIG_DEBUG_BLK_CGROUP
  159. /* This should be called with the blkg->stats_lock held. */
  160. static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
  161. struct blkio_policy_type *pol,
  162. struct blkio_group *curr_blkg)
  163. {
  164. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  165. if (blkio_blkg_waiting(&pd->stats))
  166. return;
  167. if (blkg == curr_blkg)
  168. return;
  169. pd->stats.start_group_wait_time = sched_clock();
  170. blkio_mark_blkg_waiting(&pd->stats);
  171. }
  172. /* This should be called with the blkg->stats_lock held. */
  173. static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
  174. {
  175. unsigned long long now;
  176. if (!blkio_blkg_waiting(stats))
  177. return;
  178. now = sched_clock();
  179. if (time_after64(now, stats->start_group_wait_time))
  180. stats->group_wait_time += now - stats->start_group_wait_time;
  181. blkio_clear_blkg_waiting(stats);
  182. }
  183. /* This should be called with the blkg->stats_lock held. */
  184. static void blkio_end_empty_time(struct blkio_group_stats *stats)
  185. {
  186. unsigned long long now;
  187. if (!blkio_blkg_empty(stats))
  188. return;
  189. now = sched_clock();
  190. if (time_after64(now, stats->start_empty_time))
  191. stats->empty_time += now - stats->start_empty_time;
  192. blkio_clear_blkg_empty(stats);
  193. }
  194. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
  195. struct blkio_policy_type *pol)
  196. {
  197. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  198. unsigned long flags;
  199. spin_lock_irqsave(&blkg->stats_lock, flags);
  200. BUG_ON(blkio_blkg_idling(&pd->stats));
  201. pd->stats.start_idle_time = sched_clock();
  202. blkio_mark_blkg_idling(&pd->stats);
  203. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  204. }
  205. EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
  206. void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
  207. struct blkio_policy_type *pol)
  208. {
  209. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  210. unsigned long flags;
  211. unsigned long long now;
  212. struct blkio_group_stats *stats;
  213. spin_lock_irqsave(&blkg->stats_lock, flags);
  214. stats = &pd->stats;
  215. if (blkio_blkg_idling(stats)) {
  216. now = sched_clock();
  217. if (time_after64(now, stats->start_idle_time))
  218. stats->idle_time += now - stats->start_idle_time;
  219. blkio_clear_blkg_idling(stats);
  220. }
  221. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  222. }
  223. EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
  224. void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
  225. struct blkio_policy_type *pol)
  226. {
  227. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  228. unsigned long flags;
  229. struct blkio_group_stats *stats;
  230. spin_lock_irqsave(&blkg->stats_lock, flags);
  231. stats = &pd->stats;
  232. stats->avg_queue_size_sum +=
  233. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
  234. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
  235. stats->avg_queue_size_samples++;
  236. blkio_update_group_wait_time(stats);
  237. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  238. }
  239. EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
  240. void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  241. struct blkio_policy_type *pol)
  242. {
  243. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  244. unsigned long flags;
  245. struct blkio_group_stats *stats;
  246. spin_lock_irqsave(&blkg->stats_lock, flags);
  247. stats = &pd->stats;
  248. if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
  249. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
  250. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  251. return;
  252. }
  253. /*
  254. * group is already marked empty. This can happen if cfqq got new
  255. * request in parent group and moved to this group while being added
  256. * to service tree. Just ignore the event and move on.
  257. */
  258. if(blkio_blkg_empty(stats)) {
  259. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  260. return;
  261. }
  262. stats->start_empty_time = sched_clock();
  263. blkio_mark_blkg_empty(stats);
  264. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  265. }
  266. EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
  267. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  268. struct blkio_policy_type *pol,
  269. unsigned long dequeue)
  270. {
  271. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  272. pd->stats.dequeue += dequeue;
  273. }
  274. EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
  275. #else
  276. static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
  277. struct blkio_policy_type *pol,
  278. struct blkio_group *curr_blkg) { }
  279. static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
  280. #endif
  281. void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  282. struct blkio_policy_type *pol,
  283. struct blkio_group *curr_blkg, bool direction,
  284. bool sync)
  285. {
  286. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  287. unsigned long flags;
  288. spin_lock_irqsave(&blkg->stats_lock, flags);
  289. blkio_add_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
  290. sync);
  291. blkio_end_empty_time(&pd->stats);
  292. blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
  293. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  294. }
  295. EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
  296. void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  297. struct blkio_policy_type *pol,
  298. bool direction, bool sync)
  299. {
  300. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  301. unsigned long flags;
  302. spin_lock_irqsave(&blkg->stats_lock, flags);
  303. blkio_check_and_dec_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED],
  304. direction, sync);
  305. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  306. }
  307. EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
  308. void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  309. struct blkio_policy_type *pol,
  310. unsigned long time,
  311. unsigned long unaccounted_time)
  312. {
  313. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  314. unsigned long flags;
  315. spin_lock_irqsave(&blkg->stats_lock, flags);
  316. pd->stats.time += time;
  317. #ifdef CONFIG_DEBUG_BLK_CGROUP
  318. pd->stats.unaccounted_time += unaccounted_time;
  319. #endif
  320. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  321. }
  322. EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
  323. /*
  324. * should be called under rcu read lock or queue lock to make sure blkg pointer
  325. * is valid.
  326. */
  327. void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  328. struct blkio_policy_type *pol,
  329. uint64_t bytes, bool direction, bool sync)
  330. {
  331. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  332. struct blkio_group_stats_cpu *stats_cpu;
  333. unsigned long flags;
  334. /*
  335. * Disabling interrupts to provide mutual exclusion between two
  336. * writes on same cpu. It probably is not needed for 64bit. Not
  337. * optimizing that case yet.
  338. */
  339. local_irq_save(flags);
  340. stats_cpu = this_cpu_ptr(pd->stats_cpu);
  341. u64_stats_update_begin(&stats_cpu->syncp);
  342. stats_cpu->sectors += bytes >> 9;
  343. blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
  344. 1, direction, sync);
  345. blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
  346. bytes, direction, sync);
  347. u64_stats_update_end(&stats_cpu->syncp);
  348. local_irq_restore(flags);
  349. }
  350. EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
  351. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  352. struct blkio_policy_type *pol,
  353. uint64_t start_time,
  354. uint64_t io_start_time, bool direction,
  355. bool sync)
  356. {
  357. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  358. struct blkio_group_stats *stats;
  359. unsigned long flags;
  360. unsigned long long now = sched_clock();
  361. spin_lock_irqsave(&blkg->stats_lock, flags);
  362. stats = &pd->stats;
  363. if (time_after64(now, io_start_time))
  364. blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
  365. now - io_start_time, direction, sync);
  366. if (time_after64(io_start_time, start_time))
  367. blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
  368. io_start_time - start_time, direction, sync);
  369. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  370. }
  371. EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
  372. /* Merged stats are per cpu. */
  373. void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  374. struct blkio_policy_type *pol,
  375. bool direction, bool sync)
  376. {
  377. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  378. struct blkio_group_stats_cpu *stats_cpu;
  379. unsigned long flags;
  380. /*
  381. * Disabling interrupts to provide mutual exclusion between two
  382. * writes on same cpu. It probably is not needed for 64bit. Not
  383. * optimizing that case yet.
  384. */
  385. local_irq_save(flags);
  386. stats_cpu = this_cpu_ptr(pd->stats_cpu);
  387. u64_stats_update_begin(&stats_cpu->syncp);
  388. blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
  389. direction, sync);
  390. u64_stats_update_end(&stats_cpu->syncp);
  391. local_irq_restore(flags);
  392. }
  393. EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
  394. /**
  395. * blkg_free - free a blkg
  396. * @blkg: blkg to free
  397. *
  398. * Free @blkg which may be partially allocated.
  399. */
  400. static void blkg_free(struct blkio_group *blkg)
  401. {
  402. int i;
  403. if (!blkg)
  404. return;
  405. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  406. struct blkg_policy_data *pd = blkg->pd[i];
  407. if (pd) {
  408. free_percpu(pd->stats_cpu);
  409. kfree(pd);
  410. }
  411. }
  412. kfree(blkg);
  413. }
  414. /**
  415. * blkg_alloc - allocate a blkg
  416. * @blkcg: block cgroup the new blkg is associated with
  417. * @q: request_queue the new blkg is associated with
  418. *
  419. * Allocate a new blkg assocating @blkcg and @q.
  420. *
  421. * FIXME: Should be called with queue locked but currently isn't due to
  422. * percpu stat breakage.
  423. */
  424. static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
  425. struct request_queue *q)
  426. {
  427. struct blkio_group *blkg;
  428. int i;
  429. /* alloc and init base part */
  430. blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
  431. if (!blkg)
  432. return NULL;
  433. spin_lock_init(&blkg->stats_lock);
  434. blkg->q = q;
  435. INIT_LIST_HEAD(&blkg->q_node);
  436. blkg->blkcg = blkcg;
  437. blkg->refcnt = 1;
  438. cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
  439. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  440. struct blkio_policy_type *pol = blkio_policy[i];
  441. struct blkg_policy_data *pd;
  442. if (!pol)
  443. continue;
  444. /* alloc per-policy data and attach it to blkg */
  445. pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
  446. q->node);
  447. if (!pd) {
  448. blkg_free(blkg);
  449. return NULL;
  450. }
  451. blkg->pd[i] = pd;
  452. pd->blkg = blkg;
  453. /* broken, read comment in the callsite */
  454. pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
  455. if (!pd->stats_cpu) {
  456. blkg_free(blkg);
  457. return NULL;
  458. }
  459. }
  460. /* invoke per-policy init */
  461. for (i = 0; i < BLKIO_NR_POLICIES; i++) {
  462. struct blkio_policy_type *pol = blkio_policy[i];
  463. if (pol)
  464. pol->ops.blkio_init_group_fn(blkg);
  465. }
  466. return blkg;
  467. }
  468. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  469. struct request_queue *q,
  470. enum blkio_policy_id plid,
  471. bool for_root)
  472. __releases(q->queue_lock) __acquires(q->queue_lock)
  473. {
  474. struct blkio_group *blkg, *new_blkg;
  475. WARN_ON_ONCE(!rcu_read_lock_held());
  476. lockdep_assert_held(q->queue_lock);
  477. /*
  478. * This could be the first entry point of blkcg implementation and
  479. * we shouldn't allow anything to go through for a bypassing queue.
  480. * The following can be removed if blkg lookup is guaranteed to
  481. * fail on a bypassing queue.
  482. */
  483. if (unlikely(blk_queue_bypass(q)) && !for_root)
  484. return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
  485. blkg = blkg_lookup(blkcg, q);
  486. if (blkg)
  487. return blkg;
  488. /* blkg holds a reference to blkcg */
  489. if (!css_tryget(&blkcg->css))
  490. return ERR_PTR(-EINVAL);
  491. /*
  492. * Allocate and initialize.
  493. *
  494. * FIXME: The following is broken. Percpu memory allocation
  495. * requires %GFP_KERNEL context and can't be performed from IO
  496. * path. Allocation here should inherently be atomic and the
  497. * following lock dancing can be removed once the broken percpu
  498. * allocation is fixed.
  499. */
  500. spin_unlock_irq(q->queue_lock);
  501. rcu_read_unlock();
  502. new_blkg = blkg_alloc(blkcg, q);
  503. rcu_read_lock();
  504. spin_lock_irq(q->queue_lock);
  505. /* did bypass get turned on inbetween? */
  506. if (unlikely(blk_queue_bypass(q)) && !for_root) {
  507. blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
  508. goto out;
  509. }
  510. /* did someone beat us to it? */
  511. blkg = blkg_lookup(blkcg, q);
  512. if (unlikely(blkg))
  513. goto out;
  514. /* did alloc fail? */
  515. if (unlikely(!new_blkg)) {
  516. blkg = ERR_PTR(-ENOMEM);
  517. goto out;
  518. }
  519. /* insert */
  520. spin_lock(&blkcg->lock);
  521. swap(blkg, new_blkg);
  522. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  523. list_add(&blkg->q_node, &q->blkg_list);
  524. spin_unlock(&blkcg->lock);
  525. out:
  526. blkg_free(new_blkg);
  527. return blkg;
  528. }
  529. EXPORT_SYMBOL_GPL(blkg_lookup_create);
  530. /* called under rcu_read_lock(). */
  531. struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  532. struct request_queue *q)
  533. {
  534. struct blkio_group *blkg;
  535. struct hlist_node *n;
  536. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
  537. if (blkg->q == q)
  538. return blkg;
  539. return NULL;
  540. }
  541. EXPORT_SYMBOL_GPL(blkg_lookup);
  542. static void blkg_destroy(struct blkio_group *blkg)
  543. {
  544. struct request_queue *q = blkg->q;
  545. struct blkio_cgroup *blkcg = blkg->blkcg;
  546. lockdep_assert_held(q->queue_lock);
  547. lockdep_assert_held(&blkcg->lock);
  548. /* Something wrong if we are trying to remove same group twice */
  549. WARN_ON_ONCE(list_empty(&blkg->q_node));
  550. WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
  551. list_del_init(&blkg->q_node);
  552. hlist_del_init_rcu(&blkg->blkcg_node);
  553. /*
  554. * Put the reference taken at the time of creation so that when all
  555. * queues are gone, group can be destroyed.
  556. */
  557. blkg_put(blkg);
  558. }
  559. /*
  560. * XXX: This updates blkg policy data in-place for root blkg, which is
  561. * necessary across elevator switch and policy registration as root blkgs
  562. * aren't shot down. This broken and racy implementation is temporary.
  563. * Eventually, blkg shoot down will be replaced by proper in-place update.
  564. */
  565. void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
  566. {
  567. struct blkio_policy_type *pol = blkio_policy[plid];
  568. struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
  569. struct blkg_policy_data *pd;
  570. if (!blkg)
  571. return;
  572. kfree(blkg->pd[plid]);
  573. blkg->pd[plid] = NULL;
  574. if (!pol)
  575. return;
  576. pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
  577. WARN_ON_ONCE(!pd);
  578. pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
  579. WARN_ON_ONCE(!pd->stats_cpu);
  580. blkg->pd[plid] = pd;
  581. pd->blkg = blkg;
  582. pol->ops.blkio_init_group_fn(blkg);
  583. }
  584. EXPORT_SYMBOL_GPL(update_root_blkg_pd);
  585. /**
  586. * blkg_destroy_all - destroy all blkgs associated with a request_queue
  587. * @q: request_queue of interest
  588. * @destroy_root: whether to destroy root blkg or not
  589. *
  590. * Destroy blkgs associated with @q. If @destroy_root is %true, all are
  591. * destroyed; otherwise, root blkg is left alone.
  592. */
  593. void blkg_destroy_all(struct request_queue *q, bool destroy_root)
  594. {
  595. struct blkio_group *blkg, *n;
  596. spin_lock_irq(q->queue_lock);
  597. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  598. struct blkio_cgroup *blkcg = blkg->blkcg;
  599. /* skip root? */
  600. if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
  601. continue;
  602. spin_lock(&blkcg->lock);
  603. blkg_destroy(blkg);
  604. spin_unlock(&blkcg->lock);
  605. }
  606. spin_unlock_irq(q->queue_lock);
  607. }
  608. EXPORT_SYMBOL_GPL(blkg_destroy_all);
  609. static void blkg_rcu_free(struct rcu_head *rcu_head)
  610. {
  611. blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
  612. }
  613. void __blkg_release(struct blkio_group *blkg)
  614. {
  615. /* release the extra blkcg reference this blkg has been holding */
  616. css_put(&blkg->blkcg->css);
  617. /*
  618. * A group is freed in rcu manner. But having an rcu lock does not
  619. * mean that one can access all the fields of blkg and assume these
  620. * are valid. For example, don't try to follow throtl_data and
  621. * request queue links.
  622. *
  623. * Having a reference to blkg under an rcu allows acess to only
  624. * values local to groups like group stats and group rate limits
  625. */
  626. call_rcu(&blkg->rcu_head, blkg_rcu_free);
  627. }
  628. EXPORT_SYMBOL_GPL(__blkg_release);
  629. static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
  630. {
  631. struct blkg_policy_data *pd = blkg->pd[plid];
  632. struct blkio_group_stats_cpu *stats_cpu;
  633. int i, j, k;
  634. /*
  635. * Note: On 64 bit arch this should not be an issue. This has the
  636. * possibility of returning some inconsistent value on 32bit arch
  637. * as 64bit update on 32bit is non atomic. Taking care of this
  638. * corner case makes code very complicated, like sending IPIs to
  639. * cpus, taking care of stats of offline cpus etc.
  640. *
  641. * reset stats is anyway more of a debug feature and this sounds a
  642. * corner case. So I am not complicating the code yet until and
  643. * unless this becomes a real issue.
  644. */
  645. for_each_possible_cpu(i) {
  646. stats_cpu = per_cpu_ptr(pd->stats_cpu, i);
  647. stats_cpu->sectors = 0;
  648. for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
  649. for (k = 0; k < BLKIO_STAT_TOTAL; k++)
  650. stats_cpu->stat_arr_cpu[j][k] = 0;
  651. }
  652. }
  653. static int
  654. blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
  655. {
  656. struct blkio_cgroup *blkcg;
  657. struct blkio_group *blkg;
  658. struct blkio_group_stats *stats;
  659. struct hlist_node *n;
  660. uint64_t queued[BLKIO_STAT_TOTAL];
  661. int i;
  662. #ifdef CONFIG_DEBUG_BLK_CGROUP
  663. bool idling, waiting, empty;
  664. unsigned long long now = sched_clock();
  665. #endif
  666. blkcg = cgroup_to_blkio_cgroup(cgroup);
  667. spin_lock(&blkio_list_lock);
  668. spin_lock_irq(&blkcg->lock);
  669. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  670. struct blkio_policy_type *pol;
  671. list_for_each_entry(pol, &blkio_list, list) {
  672. struct blkg_policy_data *pd = blkg->pd[pol->plid];
  673. spin_lock(&blkg->stats_lock);
  674. stats = &pd->stats;
  675. #ifdef CONFIG_DEBUG_BLK_CGROUP
  676. idling = blkio_blkg_idling(stats);
  677. waiting = blkio_blkg_waiting(stats);
  678. empty = blkio_blkg_empty(stats);
  679. #endif
  680. for (i = 0; i < BLKIO_STAT_TOTAL; i++)
  681. queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
  682. memset(stats, 0, sizeof(struct blkio_group_stats));
  683. for (i = 0; i < BLKIO_STAT_TOTAL; i++)
  684. stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
  685. #ifdef CONFIG_DEBUG_BLK_CGROUP
  686. if (idling) {
  687. blkio_mark_blkg_idling(stats);
  688. stats->start_idle_time = now;
  689. }
  690. if (waiting) {
  691. blkio_mark_blkg_waiting(stats);
  692. stats->start_group_wait_time = now;
  693. }
  694. if (empty) {
  695. blkio_mark_blkg_empty(stats);
  696. stats->start_empty_time = now;
  697. }
  698. #endif
  699. spin_unlock(&blkg->stats_lock);
  700. /* Reset Per cpu stats which don't take blkg->stats_lock */
  701. blkio_reset_stats_cpu(blkg, pol->plid);
  702. }
  703. }
  704. spin_unlock_irq(&blkcg->lock);
  705. spin_unlock(&blkio_list_lock);
  706. return 0;
  707. }
  708. static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
  709. char *str, int chars_left, bool diskname_only)
  710. {
  711. snprintf(str, chars_left, "%s", dname);
  712. chars_left -= strlen(str);
  713. if (chars_left <= 0) {
  714. printk(KERN_WARNING
  715. "Possibly incorrect cgroup stat display format");
  716. return;
  717. }
  718. if (diskname_only)
  719. return;
  720. switch (type) {
  721. case BLKIO_STAT_READ:
  722. strlcat(str, " Read", chars_left);
  723. break;
  724. case BLKIO_STAT_WRITE:
  725. strlcat(str, " Write", chars_left);
  726. break;
  727. case BLKIO_STAT_SYNC:
  728. strlcat(str, " Sync", chars_left);
  729. break;
  730. case BLKIO_STAT_ASYNC:
  731. strlcat(str, " Async", chars_left);
  732. break;
  733. case BLKIO_STAT_TOTAL:
  734. strlcat(str, " Total", chars_left);
  735. break;
  736. default:
  737. strlcat(str, " Invalid", chars_left);
  738. }
  739. }
  740. static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
  741. struct cgroup_map_cb *cb, const char *dname)
  742. {
  743. blkio_get_key_name(0, dname, str, chars_left, true);
  744. cb->fill(cb, str, val);
  745. return val;
  746. }
  747. static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
  748. enum stat_type_cpu type, enum stat_sub_type sub_type)
  749. {
  750. struct blkg_policy_data *pd = blkg->pd[plid];
  751. int cpu;
  752. struct blkio_group_stats_cpu *stats_cpu;
  753. u64 val = 0, tval;
  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_CPU_MERGED, 1, 1);
  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);