blk-cgroup.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  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
  71. blkio_update_group_weight(struct blkio_group *blkg, 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 != blkg->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, u64 bps,
  84. 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 != blkg->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. unsigned int iops, int fileid)
  103. {
  104. struct blkio_policy_type *blkiop;
  105. list_for_each_entry(blkiop, &blkio_list, list) {
  106. /* If this policy does not own the blkg, do not send updates */
  107. if (blkiop->plid != blkg->plid)
  108. continue;
  109. if (fileid == BLKIO_THROTL_read_iops_device
  110. && blkiop->ops.blkio_update_group_read_iops_fn)
  111. blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
  112. blkg, iops);
  113. if (fileid == BLKIO_THROTL_write_iops_device
  114. && blkiop->ops.blkio_update_group_write_iops_fn)
  115. blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
  116. blkg,iops);
  117. }
  118. }
  119. /*
  120. * Add to the appropriate stat variable depending on the request type.
  121. * This should be called with the blkg->stats_lock held.
  122. */
  123. static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
  124. bool sync)
  125. {
  126. if (direction)
  127. stat[BLKIO_STAT_WRITE] += add;
  128. else
  129. stat[BLKIO_STAT_READ] += add;
  130. if (sync)
  131. stat[BLKIO_STAT_SYNC] += add;
  132. else
  133. stat[BLKIO_STAT_ASYNC] += add;
  134. }
  135. /*
  136. * Decrements the appropriate stat variable if non-zero depending on the
  137. * request type. Panics on value being zero.
  138. * This should be called with the blkg->stats_lock held.
  139. */
  140. static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
  141. {
  142. if (direction) {
  143. BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
  144. stat[BLKIO_STAT_WRITE]--;
  145. } else {
  146. BUG_ON(stat[BLKIO_STAT_READ] == 0);
  147. stat[BLKIO_STAT_READ]--;
  148. }
  149. if (sync) {
  150. BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
  151. stat[BLKIO_STAT_SYNC]--;
  152. } else {
  153. BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
  154. stat[BLKIO_STAT_ASYNC]--;
  155. }
  156. }
  157. #ifdef CONFIG_DEBUG_BLK_CGROUP
  158. /* This should be called with the blkg->stats_lock held. */
  159. static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
  160. struct blkio_group *curr_blkg)
  161. {
  162. if (blkio_blkg_waiting(&blkg->stats))
  163. return;
  164. if (blkg == curr_blkg)
  165. return;
  166. blkg->stats.start_group_wait_time = sched_clock();
  167. blkio_mark_blkg_waiting(&blkg->stats);
  168. }
  169. /* This should be called with the blkg->stats_lock held. */
  170. static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
  171. {
  172. unsigned long long now;
  173. if (!blkio_blkg_waiting(stats))
  174. return;
  175. now = sched_clock();
  176. if (time_after64(now, stats->start_group_wait_time))
  177. stats->group_wait_time += now - stats->start_group_wait_time;
  178. blkio_clear_blkg_waiting(stats);
  179. }
  180. /* This should be called with the blkg->stats_lock held. */
  181. static void blkio_end_empty_time(struct blkio_group_stats *stats)
  182. {
  183. unsigned long long now;
  184. if (!blkio_blkg_empty(stats))
  185. return;
  186. now = sched_clock();
  187. if (time_after64(now, stats->start_empty_time))
  188. stats->empty_time += now - stats->start_empty_time;
  189. blkio_clear_blkg_empty(stats);
  190. }
  191. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
  192. {
  193. unsigned long flags;
  194. spin_lock_irqsave(&blkg->stats_lock, flags);
  195. BUG_ON(blkio_blkg_idling(&blkg->stats));
  196. blkg->stats.start_idle_time = sched_clock();
  197. blkio_mark_blkg_idling(&blkg->stats);
  198. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  199. }
  200. EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
  201. void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
  202. {
  203. unsigned long flags;
  204. unsigned long long now;
  205. struct blkio_group_stats *stats;
  206. spin_lock_irqsave(&blkg->stats_lock, flags);
  207. stats = &blkg->stats;
  208. if (blkio_blkg_idling(stats)) {
  209. now = sched_clock();
  210. if (time_after64(now, stats->start_idle_time))
  211. stats->idle_time += now - stats->start_idle_time;
  212. blkio_clear_blkg_idling(stats);
  213. }
  214. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  215. }
  216. EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
  217. void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
  218. {
  219. unsigned long flags;
  220. struct blkio_group_stats *stats;
  221. spin_lock_irqsave(&blkg->stats_lock, flags);
  222. stats = &blkg->stats;
  223. stats->avg_queue_size_sum +=
  224. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
  225. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
  226. stats->avg_queue_size_samples++;
  227. blkio_update_group_wait_time(stats);
  228. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  229. }
  230. EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
  231. void blkiocg_set_start_empty_time(struct blkio_group *blkg)
  232. {
  233. unsigned long flags;
  234. struct blkio_group_stats *stats;
  235. spin_lock_irqsave(&blkg->stats_lock, flags);
  236. stats = &blkg->stats;
  237. if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
  238. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
  239. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  240. return;
  241. }
  242. /*
  243. * group is already marked empty. This can happen if cfqq got new
  244. * request in parent group and moved to this group while being added
  245. * to service tree. Just ignore the event and move on.
  246. */
  247. if(blkio_blkg_empty(stats)) {
  248. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  249. return;
  250. }
  251. stats->start_empty_time = sched_clock();
  252. blkio_mark_blkg_empty(stats);
  253. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  254. }
  255. EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
  256. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  257. unsigned long dequeue)
  258. {
  259. blkg->stats.dequeue += dequeue;
  260. }
  261. EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
  262. #else
  263. static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
  264. struct blkio_group *curr_blkg) {}
  265. static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
  266. #endif
  267. void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  268. struct blkio_group *curr_blkg, bool direction,
  269. bool sync)
  270. {
  271. unsigned long flags;
  272. spin_lock_irqsave(&blkg->stats_lock, flags);
  273. blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
  274. sync);
  275. blkio_end_empty_time(&blkg->stats);
  276. blkio_set_start_group_wait_time(blkg, curr_blkg);
  277. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  278. }
  279. EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
  280. void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  281. bool direction, bool sync)
  282. {
  283. unsigned long flags;
  284. spin_lock_irqsave(&blkg->stats_lock, flags);
  285. blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
  286. direction, sync);
  287. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  288. }
  289. EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
  290. void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time,
  291. unsigned long unaccounted_time)
  292. {
  293. unsigned long flags;
  294. spin_lock_irqsave(&blkg->stats_lock, flags);
  295. blkg->stats.time += time;
  296. #ifdef CONFIG_DEBUG_BLK_CGROUP
  297. blkg->stats.unaccounted_time += unaccounted_time;
  298. #endif
  299. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  300. }
  301. EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
  302. /*
  303. * should be called under rcu read lock or queue lock to make sure blkg pointer
  304. * is valid.
  305. */
  306. void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  307. uint64_t bytes, bool direction, bool sync)
  308. {
  309. struct blkio_group_stats_cpu *stats_cpu;
  310. unsigned long flags;
  311. /*
  312. * Disabling interrupts to provide mutual exclusion between two
  313. * writes on same cpu. It probably is not needed for 64bit. Not
  314. * optimizing that case yet.
  315. */
  316. local_irq_save(flags);
  317. stats_cpu = this_cpu_ptr(blkg->stats_cpu);
  318. u64_stats_update_begin(&stats_cpu->syncp);
  319. stats_cpu->sectors += bytes >> 9;
  320. blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
  321. 1, direction, sync);
  322. blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
  323. bytes, direction, sync);
  324. u64_stats_update_end(&stats_cpu->syncp);
  325. local_irq_restore(flags);
  326. }
  327. EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
  328. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  329. uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
  330. {
  331. struct blkio_group_stats *stats;
  332. unsigned long flags;
  333. unsigned long long now = sched_clock();
  334. spin_lock_irqsave(&blkg->stats_lock, flags);
  335. stats = &blkg->stats;
  336. if (time_after64(now, io_start_time))
  337. blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
  338. now - io_start_time, direction, sync);
  339. if (time_after64(io_start_time, start_time))
  340. blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
  341. io_start_time - start_time, direction, sync);
  342. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  343. }
  344. EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
  345. /* Merged stats are per cpu. */
  346. void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
  347. bool sync)
  348. {
  349. struct blkio_group_stats_cpu *stats_cpu;
  350. unsigned long flags;
  351. /*
  352. * Disabling interrupts to provide mutual exclusion between two
  353. * writes on same cpu. It probably is not needed for 64bit. Not
  354. * optimizing that case yet.
  355. */
  356. local_irq_save(flags);
  357. stats_cpu = this_cpu_ptr(blkg->stats_cpu);
  358. u64_stats_update_begin(&stats_cpu->syncp);
  359. blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
  360. direction, sync);
  361. u64_stats_update_end(&stats_cpu->syncp);
  362. local_irq_restore(flags);
  363. }
  364. EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
  365. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  366. struct request_queue *q,
  367. enum blkio_policy_id plid,
  368. bool for_root)
  369. __releases(q->queue_lock) __acquires(q->queue_lock)
  370. {
  371. struct blkio_policy_type *pol = blkio_policy[plid];
  372. struct blkio_group *blkg, *new_blkg;
  373. WARN_ON_ONCE(!rcu_read_lock_held());
  374. lockdep_assert_held(q->queue_lock);
  375. /*
  376. * This could be the first entry point of blkcg implementation and
  377. * we shouldn't allow anything to go through for a bypassing queue.
  378. * The following can be removed if blkg lookup is guaranteed to
  379. * fail on a bypassing queue.
  380. */
  381. if (unlikely(blk_queue_bypass(q)) && !for_root)
  382. return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
  383. blkg = blkg_lookup(blkcg, q, plid);
  384. if (blkg)
  385. return blkg;
  386. /* blkg holds a reference to blkcg */
  387. if (!css_tryget(&blkcg->css))
  388. return ERR_PTR(-EINVAL);
  389. /*
  390. * Allocate and initialize.
  391. *
  392. * FIXME: The following is broken. Percpu memory allocation
  393. * requires %GFP_KERNEL context and can't be performed from IO
  394. * path. Allocation here should inherently be atomic and the
  395. * following lock dancing can be removed once the broken percpu
  396. * allocation is fixed.
  397. */
  398. spin_unlock_irq(q->queue_lock);
  399. rcu_read_unlock();
  400. new_blkg = pol->ops.blkio_alloc_group_fn(q, blkcg);
  401. if (new_blkg) {
  402. new_blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
  403. spin_lock_init(&new_blkg->stats_lock);
  404. rcu_assign_pointer(new_blkg->q, q);
  405. new_blkg->blkcg = blkcg;
  406. new_blkg->plid = plid;
  407. cgroup_path(blkcg->css.cgroup, new_blkg->path,
  408. sizeof(new_blkg->path));
  409. } else {
  410. css_put(&blkcg->css);
  411. }
  412. rcu_read_lock();
  413. spin_lock_irq(q->queue_lock);
  414. /* did bypass get turned on inbetween? */
  415. if (unlikely(blk_queue_bypass(q)) && !for_root) {
  416. blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
  417. goto out;
  418. }
  419. /* did someone beat us to it? */
  420. blkg = blkg_lookup(blkcg, q, plid);
  421. if (unlikely(blkg))
  422. goto out;
  423. /* did alloc fail? */
  424. if (unlikely(!new_blkg || !new_blkg->stats_cpu)) {
  425. blkg = ERR_PTR(-ENOMEM);
  426. goto out;
  427. }
  428. /* insert */
  429. spin_lock(&blkcg->lock);
  430. swap(blkg, new_blkg);
  431. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  432. pol->ops.blkio_link_group_fn(q, blkg);
  433. spin_unlock(&blkcg->lock);
  434. out:
  435. if (new_blkg) {
  436. free_percpu(new_blkg->stats_cpu);
  437. kfree(new_blkg);
  438. css_put(&blkcg->css);
  439. }
  440. return blkg;
  441. }
  442. EXPORT_SYMBOL_GPL(blkg_lookup_create);
  443. static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
  444. {
  445. hlist_del_init_rcu(&blkg->blkcg_node);
  446. }
  447. /*
  448. * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
  449. * indicating that blk_group was unhashed by the time we got to it.
  450. */
  451. int blkiocg_del_blkio_group(struct blkio_group *blkg)
  452. {
  453. struct blkio_cgroup *blkcg = blkg->blkcg;
  454. unsigned long flags;
  455. int ret = 1;
  456. spin_lock_irqsave(&blkcg->lock, flags);
  457. if (!hlist_unhashed(&blkg->blkcg_node)) {
  458. __blkiocg_del_blkio_group(blkg);
  459. ret = 0;
  460. }
  461. spin_unlock_irqrestore(&blkcg->lock, flags);
  462. return ret;
  463. }
  464. EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
  465. /* called under rcu_read_lock(). */
  466. struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  467. struct request_queue *q,
  468. enum blkio_policy_id plid)
  469. {
  470. struct blkio_group *blkg;
  471. struct hlist_node *n;
  472. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
  473. if (blkg->q == q && blkg->plid == plid)
  474. return blkg;
  475. return NULL;
  476. }
  477. EXPORT_SYMBOL_GPL(blkg_lookup);
  478. void blkg_destroy_all(struct request_queue *q)
  479. {
  480. struct blkio_policy_type *pol;
  481. while (true) {
  482. bool done = true;
  483. spin_lock(&blkio_list_lock);
  484. spin_lock_irq(q->queue_lock);
  485. /*
  486. * clear_queue_fn() might return with non-empty group list
  487. * if it raced cgroup removal and lost. cgroup removal is
  488. * guaranteed to make forward progress and retrying after a
  489. * while is enough. This ugliness is scheduled to be
  490. * removed after locking update.
  491. */
  492. list_for_each_entry(pol, &blkio_list, list)
  493. if (!pol->ops.blkio_clear_queue_fn(q))
  494. done = false;
  495. spin_unlock_irq(q->queue_lock);
  496. spin_unlock(&blkio_list_lock);
  497. if (done)
  498. break;
  499. msleep(10); /* just some random duration I like */
  500. }
  501. }
  502. static void blkio_reset_stats_cpu(struct blkio_group *blkg)
  503. {
  504. struct blkio_group_stats_cpu *stats_cpu;
  505. int i, j, k;
  506. /*
  507. * Note: On 64 bit arch this should not be an issue. This has the
  508. * possibility of returning some inconsistent value on 32bit arch
  509. * as 64bit update on 32bit is non atomic. Taking care of this
  510. * corner case makes code very complicated, like sending IPIs to
  511. * cpus, taking care of stats of offline cpus etc.
  512. *
  513. * reset stats is anyway more of a debug feature and this sounds a
  514. * corner case. So I am not complicating the code yet until and
  515. * unless this becomes a real issue.
  516. */
  517. for_each_possible_cpu(i) {
  518. stats_cpu = per_cpu_ptr(blkg->stats_cpu, i);
  519. stats_cpu->sectors = 0;
  520. for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
  521. for (k = 0; k < BLKIO_STAT_TOTAL; k++)
  522. stats_cpu->stat_arr_cpu[j][k] = 0;
  523. }
  524. }
  525. static int
  526. blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
  527. {
  528. struct blkio_cgroup *blkcg;
  529. struct blkio_group *blkg;
  530. struct blkio_group_stats *stats;
  531. struct hlist_node *n;
  532. uint64_t queued[BLKIO_STAT_TOTAL];
  533. int i;
  534. #ifdef CONFIG_DEBUG_BLK_CGROUP
  535. bool idling, waiting, empty;
  536. unsigned long long now = sched_clock();
  537. #endif
  538. blkcg = cgroup_to_blkio_cgroup(cgroup);
  539. spin_lock_irq(&blkcg->lock);
  540. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  541. spin_lock(&blkg->stats_lock);
  542. stats = &blkg->stats;
  543. #ifdef CONFIG_DEBUG_BLK_CGROUP
  544. idling = blkio_blkg_idling(stats);
  545. waiting = blkio_blkg_waiting(stats);
  546. empty = blkio_blkg_empty(stats);
  547. #endif
  548. for (i = 0; i < BLKIO_STAT_TOTAL; i++)
  549. queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
  550. memset(stats, 0, sizeof(struct blkio_group_stats));
  551. for (i = 0; i < BLKIO_STAT_TOTAL; i++)
  552. stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
  553. #ifdef CONFIG_DEBUG_BLK_CGROUP
  554. if (idling) {
  555. blkio_mark_blkg_idling(stats);
  556. stats->start_idle_time = now;
  557. }
  558. if (waiting) {
  559. blkio_mark_blkg_waiting(stats);
  560. stats->start_group_wait_time = now;
  561. }
  562. if (empty) {
  563. blkio_mark_blkg_empty(stats);
  564. stats->start_empty_time = now;
  565. }
  566. #endif
  567. spin_unlock(&blkg->stats_lock);
  568. /* Reset Per cpu stats which don't take blkg->stats_lock */
  569. blkio_reset_stats_cpu(blkg);
  570. }
  571. spin_unlock_irq(&blkcg->lock);
  572. return 0;
  573. }
  574. static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
  575. char *str, int chars_left, bool diskname_only)
  576. {
  577. snprintf(str, chars_left, "%s", dname);
  578. chars_left -= strlen(str);
  579. if (chars_left <= 0) {
  580. printk(KERN_WARNING
  581. "Possibly incorrect cgroup stat display format");
  582. return;
  583. }
  584. if (diskname_only)
  585. return;
  586. switch (type) {
  587. case BLKIO_STAT_READ:
  588. strlcat(str, " Read", chars_left);
  589. break;
  590. case BLKIO_STAT_WRITE:
  591. strlcat(str, " Write", chars_left);
  592. break;
  593. case BLKIO_STAT_SYNC:
  594. strlcat(str, " Sync", chars_left);
  595. break;
  596. case BLKIO_STAT_ASYNC:
  597. strlcat(str, " Async", chars_left);
  598. break;
  599. case BLKIO_STAT_TOTAL:
  600. strlcat(str, " Total", chars_left);
  601. break;
  602. default:
  603. strlcat(str, " Invalid", chars_left);
  604. }
  605. }
  606. static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
  607. struct cgroup_map_cb *cb, const char *dname)
  608. {
  609. blkio_get_key_name(0, dname, str, chars_left, true);
  610. cb->fill(cb, str, val);
  611. return val;
  612. }
  613. static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
  614. enum stat_type_cpu type, enum stat_sub_type sub_type)
  615. {
  616. int cpu;
  617. struct blkio_group_stats_cpu *stats_cpu;
  618. u64 val = 0, tval;
  619. for_each_possible_cpu(cpu) {
  620. unsigned int start;
  621. stats_cpu = per_cpu_ptr(blkg->stats_cpu, cpu);
  622. do {
  623. start = u64_stats_fetch_begin(&stats_cpu->syncp);
  624. if (type == BLKIO_STAT_CPU_SECTORS)
  625. tval = stats_cpu->sectors;
  626. else
  627. tval = stats_cpu->stat_arr_cpu[type][sub_type];
  628. } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
  629. val += tval;
  630. }
  631. return val;
  632. }
  633. static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
  634. struct cgroup_map_cb *cb, const char *dname,
  635. enum stat_type_cpu type)
  636. {
  637. uint64_t disk_total, val;
  638. char key_str[MAX_KEY_LEN];
  639. enum stat_sub_type sub_type;
  640. if (type == BLKIO_STAT_CPU_SECTORS) {
  641. val = blkio_read_stat_cpu(blkg, type, 0);
  642. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
  643. dname);
  644. }
  645. for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
  646. sub_type++) {
  647. blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
  648. false);
  649. val = blkio_read_stat_cpu(blkg, type, sub_type);
  650. cb->fill(cb, key_str, val);
  651. }
  652. disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
  653. blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
  654. blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
  655. false);
  656. cb->fill(cb, key_str, disk_total);
  657. return disk_total;
  658. }
  659. /* This should be called with blkg->stats_lock held */
  660. static uint64_t blkio_get_stat(struct blkio_group *blkg,
  661. struct cgroup_map_cb *cb, const char *dname,
  662. enum stat_type type)
  663. {
  664. uint64_t disk_total;
  665. char key_str[MAX_KEY_LEN];
  666. enum stat_sub_type sub_type;
  667. if (type == BLKIO_STAT_TIME)
  668. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  669. blkg->stats.time, cb, dname);
  670. #ifdef CONFIG_DEBUG_BLK_CGROUP
  671. if (type == BLKIO_STAT_UNACCOUNTED_TIME)
  672. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  673. blkg->stats.unaccounted_time, cb, dname);
  674. if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
  675. uint64_t sum = blkg->stats.avg_queue_size_sum;
  676. uint64_t samples = blkg->stats.avg_queue_size_samples;
  677. if (samples)
  678. do_div(sum, samples);
  679. else
  680. sum = 0;
  681. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  682. sum, cb, dname);
  683. }
  684. if (type == BLKIO_STAT_GROUP_WAIT_TIME)
  685. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  686. blkg->stats.group_wait_time, cb, dname);
  687. if (type == BLKIO_STAT_IDLE_TIME)
  688. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  689. blkg->stats.idle_time, cb, dname);
  690. if (type == BLKIO_STAT_EMPTY_TIME)
  691. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  692. blkg->stats.empty_time, cb, dname);
  693. if (type == BLKIO_STAT_DEQUEUE)
  694. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  695. blkg->stats.dequeue, cb, dname);
  696. #endif
  697. for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
  698. sub_type++) {
  699. blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
  700. false);
  701. cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
  702. }
  703. disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
  704. blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
  705. blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
  706. false);
  707. cb->fill(cb, key_str, disk_total);
  708. return disk_total;
  709. }
  710. static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
  711. int fileid, struct blkio_cgroup *blkcg)
  712. {
  713. struct gendisk *disk = NULL;
  714. struct blkio_group *blkg = NULL;
  715. char *s[4], *p, *major_s = NULL, *minor_s = NULL;
  716. unsigned long major, minor;
  717. int i = 0, ret = -EINVAL;
  718. int part;
  719. dev_t dev;
  720. u64 temp;
  721. memset(s, 0, sizeof(s));
  722. while ((p = strsep(&buf, " ")) != NULL) {
  723. if (!*p)
  724. continue;
  725. s[i++] = p;
  726. /* Prevent from inputing too many things */
  727. if (i == 3)
  728. break;
  729. }
  730. if (i != 2)
  731. goto out;
  732. p = strsep(&s[0], ":");
  733. if (p != NULL)
  734. major_s = p;
  735. else
  736. goto out;
  737. minor_s = s[0];
  738. if (!minor_s)
  739. goto out;
  740. if (strict_strtoul(major_s, 10, &major))
  741. goto out;
  742. if (strict_strtoul(minor_s, 10, &minor))
  743. goto out;
  744. dev = MKDEV(major, minor);
  745. if (strict_strtoull(s[1], 10, &temp))
  746. goto out;
  747. disk = get_gendisk(dev, &part);
  748. if (!disk || part)
  749. goto out;
  750. rcu_read_lock();
  751. spin_lock_irq(disk->queue->queue_lock);
  752. blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
  753. spin_unlock_irq(disk->queue->queue_lock);
  754. if (IS_ERR(blkg)) {
  755. ret = PTR_ERR(blkg);
  756. goto out_unlock;
  757. }
  758. switch (plid) {
  759. case BLKIO_POLICY_PROP:
  760. if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
  761. temp > BLKIO_WEIGHT_MAX)
  762. goto out_unlock;
  763. blkg->conf.weight = temp;
  764. blkio_update_group_weight(blkg, temp ?: blkcg->weight);
  765. break;
  766. case BLKIO_POLICY_THROTL:
  767. switch(fileid) {
  768. case BLKIO_THROTL_read_bps_device:
  769. blkg->conf.bps[READ] = temp;
  770. blkio_update_group_bps(blkg, temp ?: -1, fileid);
  771. break;
  772. case BLKIO_THROTL_write_bps_device:
  773. blkg->conf.bps[WRITE] = temp;
  774. blkio_update_group_bps(blkg, temp ?: -1, fileid);
  775. break;
  776. case BLKIO_THROTL_read_iops_device:
  777. if (temp > THROTL_IOPS_MAX)
  778. goto out_unlock;
  779. blkg->conf.iops[READ] = temp;
  780. blkio_update_group_iops(blkg, temp ?: -1, fileid);
  781. break;
  782. case BLKIO_THROTL_write_iops_device:
  783. if (temp > THROTL_IOPS_MAX)
  784. goto out_unlock;
  785. blkg->conf.iops[WRITE] = temp;
  786. blkio_update_group_iops(blkg, temp ?: -1, fileid);
  787. break;
  788. }
  789. break;
  790. default:
  791. BUG();
  792. }
  793. ret = 0;
  794. out_unlock:
  795. rcu_read_unlock();
  796. out:
  797. put_disk(disk);
  798. /*
  799. * If queue was bypassing, we should retry. Do so after a short
  800. * msleep(). It isn't strictly necessary but queue can be
  801. * bypassing for some time and it's always nice to avoid busy
  802. * looping.
  803. */
  804. if (ret == -EBUSY) {
  805. msleep(10);
  806. return restart_syscall();
  807. }
  808. return ret;
  809. }
  810. static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
  811. const char *buffer)
  812. {
  813. int ret = 0;
  814. char *buf;
  815. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  816. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  817. int fileid = BLKIOFILE_ATTR(cft->private);
  818. buf = kstrdup(buffer, GFP_KERNEL);
  819. if (!buf)
  820. return -ENOMEM;
  821. ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
  822. kfree(buf);
  823. return ret;
  824. }
  825. static const char *blkg_dev_name(struct blkio_group *blkg)
  826. {
  827. /* some drivers (floppy) instantiate a queue w/o disk registered */
  828. if (blkg->q->backing_dev_info.dev)
  829. return dev_name(blkg->q->backing_dev_info.dev);
  830. return NULL;
  831. }
  832. static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
  833. struct seq_file *m)
  834. {
  835. const char *dname = blkg_dev_name(blkg);
  836. int fileid = BLKIOFILE_ATTR(cft->private);
  837. int rw = WRITE;
  838. if (!dname)
  839. return;
  840. switch (blkg->plid) {
  841. case BLKIO_POLICY_PROP:
  842. if (blkg->conf.weight)
  843. seq_printf(m, "%s\t%u\n",
  844. dname, blkg->conf.weight);
  845. break;
  846. case BLKIO_POLICY_THROTL:
  847. switch (fileid) {
  848. case BLKIO_THROTL_read_bps_device:
  849. rw = READ;
  850. case BLKIO_THROTL_write_bps_device:
  851. if (blkg->conf.bps[rw])
  852. seq_printf(m, "%s\t%llu\n",
  853. dname, blkg->conf.bps[rw]);
  854. break;
  855. case BLKIO_THROTL_read_iops_device:
  856. rw = READ;
  857. case BLKIO_THROTL_write_iops_device:
  858. if (blkg->conf.iops[rw])
  859. seq_printf(m, "%s\t%u\n",
  860. dname, blkg->conf.iops[rw]);
  861. break;
  862. }
  863. break;
  864. default:
  865. BUG();
  866. }
  867. }
  868. /* cgroup files which read their data from policy nodes end up here */
  869. static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
  870. struct seq_file *m)
  871. {
  872. struct blkio_group *blkg;
  873. struct hlist_node *n;
  874. spin_lock_irq(&blkcg->lock);
  875. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
  876. if (BLKIOFILE_POLICY(cft->private) == blkg->plid)
  877. blkio_print_group_conf(cft, blkg, m);
  878. spin_unlock_irq(&blkcg->lock);
  879. }
  880. static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
  881. struct seq_file *m)
  882. {
  883. struct blkio_cgroup *blkcg;
  884. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  885. int name = BLKIOFILE_ATTR(cft->private);
  886. blkcg = cgroup_to_blkio_cgroup(cgrp);
  887. switch(plid) {
  888. case BLKIO_POLICY_PROP:
  889. switch(name) {
  890. case BLKIO_PROP_weight_device:
  891. blkio_read_conf(cft, blkcg, m);
  892. return 0;
  893. default:
  894. BUG();
  895. }
  896. break;
  897. case BLKIO_POLICY_THROTL:
  898. switch(name){
  899. case BLKIO_THROTL_read_bps_device:
  900. case BLKIO_THROTL_write_bps_device:
  901. case BLKIO_THROTL_read_iops_device:
  902. case BLKIO_THROTL_write_iops_device:
  903. blkio_read_conf(cft, blkcg, m);
  904. return 0;
  905. default:
  906. BUG();
  907. }
  908. break;
  909. default:
  910. BUG();
  911. }
  912. return 0;
  913. }
  914. static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
  915. struct cftype *cft, struct cgroup_map_cb *cb,
  916. enum stat_type type, bool show_total, bool pcpu)
  917. {
  918. struct blkio_group *blkg;
  919. struct hlist_node *n;
  920. uint64_t cgroup_total = 0;
  921. rcu_read_lock();
  922. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
  923. const char *dname = blkg_dev_name(blkg);
  924. if (!dname || BLKIOFILE_POLICY(cft->private) != blkg->plid)
  925. continue;
  926. if (pcpu)
  927. cgroup_total += blkio_get_stat_cpu(blkg, cb, dname,
  928. type);
  929. else {
  930. spin_lock_irq(&blkg->stats_lock);
  931. cgroup_total += blkio_get_stat(blkg, cb, dname, type);
  932. spin_unlock_irq(&blkg->stats_lock);
  933. }
  934. }
  935. if (show_total)
  936. cb->fill(cb, "Total", cgroup_total);
  937. rcu_read_unlock();
  938. return 0;
  939. }
  940. /* All map kind of cgroup file get serviced by this function */
  941. static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
  942. struct cgroup_map_cb *cb)
  943. {
  944. struct blkio_cgroup *blkcg;
  945. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  946. int name = BLKIOFILE_ATTR(cft->private);
  947. blkcg = cgroup_to_blkio_cgroup(cgrp);
  948. switch(plid) {
  949. case BLKIO_POLICY_PROP:
  950. switch(name) {
  951. case BLKIO_PROP_time:
  952. return blkio_read_blkg_stats(blkcg, cft, cb,
  953. BLKIO_STAT_TIME, 0, 0);
  954. case BLKIO_PROP_sectors:
  955. return blkio_read_blkg_stats(blkcg, cft, cb,
  956. BLKIO_STAT_CPU_SECTORS, 0, 1);
  957. case BLKIO_PROP_io_service_bytes:
  958. return blkio_read_blkg_stats(blkcg, cft, cb,
  959. BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
  960. case BLKIO_PROP_io_serviced:
  961. return blkio_read_blkg_stats(blkcg, cft, cb,
  962. BLKIO_STAT_CPU_SERVICED, 1, 1);
  963. case BLKIO_PROP_io_service_time:
  964. return blkio_read_blkg_stats(blkcg, cft, cb,
  965. BLKIO_STAT_SERVICE_TIME, 1, 0);
  966. case BLKIO_PROP_io_wait_time:
  967. return blkio_read_blkg_stats(blkcg, cft, cb,
  968. BLKIO_STAT_WAIT_TIME, 1, 0);
  969. case BLKIO_PROP_io_merged:
  970. return blkio_read_blkg_stats(blkcg, cft, cb,
  971. BLKIO_STAT_CPU_MERGED, 1, 1);
  972. case BLKIO_PROP_io_queued:
  973. return blkio_read_blkg_stats(blkcg, cft, cb,
  974. BLKIO_STAT_QUEUED, 1, 0);
  975. #ifdef CONFIG_DEBUG_BLK_CGROUP
  976. case BLKIO_PROP_unaccounted_time:
  977. return blkio_read_blkg_stats(blkcg, cft, cb,
  978. BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
  979. case BLKIO_PROP_dequeue:
  980. return blkio_read_blkg_stats(blkcg, cft, cb,
  981. BLKIO_STAT_DEQUEUE, 0, 0);
  982. case BLKIO_PROP_avg_queue_size:
  983. return blkio_read_blkg_stats(blkcg, cft, cb,
  984. BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
  985. case BLKIO_PROP_group_wait_time:
  986. return blkio_read_blkg_stats(blkcg, cft, cb,
  987. BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
  988. case BLKIO_PROP_idle_time:
  989. return blkio_read_blkg_stats(blkcg, cft, cb,
  990. BLKIO_STAT_IDLE_TIME, 0, 0);
  991. case BLKIO_PROP_empty_time:
  992. return blkio_read_blkg_stats(blkcg, cft, cb,
  993. BLKIO_STAT_EMPTY_TIME, 0, 0);
  994. #endif
  995. default:
  996. BUG();
  997. }
  998. break;
  999. case BLKIO_POLICY_THROTL:
  1000. switch(name){
  1001. case BLKIO_THROTL_io_service_bytes:
  1002. return blkio_read_blkg_stats(blkcg, cft, cb,
  1003. BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
  1004. case BLKIO_THROTL_io_serviced:
  1005. return blkio_read_blkg_stats(blkcg, cft, cb,
  1006. BLKIO_STAT_CPU_SERVICED, 1, 1);
  1007. default:
  1008. BUG();
  1009. }
  1010. break;
  1011. default:
  1012. BUG();
  1013. }
  1014. return 0;
  1015. }
  1016. static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
  1017. {
  1018. struct blkio_group *blkg;
  1019. struct hlist_node *n;
  1020. if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
  1021. return -EINVAL;
  1022. spin_lock(&blkio_list_lock);
  1023. spin_lock_irq(&blkcg->lock);
  1024. blkcg->weight = (unsigned int)val;
  1025. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
  1026. if (blkg->plid == plid && !blkg->conf.weight)
  1027. blkio_update_group_weight(blkg, blkcg->weight);
  1028. spin_unlock_irq(&blkcg->lock);
  1029. spin_unlock(&blkio_list_lock);
  1030. return 0;
  1031. }
  1032. static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
  1033. struct blkio_cgroup *blkcg;
  1034. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1035. int name = BLKIOFILE_ATTR(cft->private);
  1036. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1037. switch(plid) {
  1038. case BLKIO_POLICY_PROP:
  1039. switch(name) {
  1040. case BLKIO_PROP_weight:
  1041. return (u64)blkcg->weight;
  1042. }
  1043. break;
  1044. default:
  1045. BUG();
  1046. }
  1047. return 0;
  1048. }
  1049. static int
  1050. blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
  1051. {
  1052. struct blkio_cgroup *blkcg;
  1053. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1054. int name = BLKIOFILE_ATTR(cft->private);
  1055. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1056. switch(plid) {
  1057. case BLKIO_POLICY_PROP:
  1058. switch(name) {
  1059. case BLKIO_PROP_weight:
  1060. return blkio_weight_write(blkcg, plid, val);
  1061. }
  1062. break;
  1063. default:
  1064. BUG();
  1065. }
  1066. return 0;
  1067. }
  1068. struct cftype blkio_files[] = {
  1069. {
  1070. .name = "weight_device",
  1071. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1072. BLKIO_PROP_weight_device),
  1073. .read_seq_string = blkiocg_file_read,
  1074. .write_string = blkiocg_file_write,
  1075. .max_write_len = 256,
  1076. },
  1077. {
  1078. .name = "weight",
  1079. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1080. BLKIO_PROP_weight),
  1081. .read_u64 = blkiocg_file_read_u64,
  1082. .write_u64 = blkiocg_file_write_u64,
  1083. },
  1084. {
  1085. .name = "time",
  1086. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1087. BLKIO_PROP_time),
  1088. .read_map = blkiocg_file_read_map,
  1089. },
  1090. {
  1091. .name = "sectors",
  1092. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1093. BLKIO_PROP_sectors),
  1094. .read_map = blkiocg_file_read_map,
  1095. },
  1096. {
  1097. .name = "io_service_bytes",
  1098. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1099. BLKIO_PROP_io_service_bytes),
  1100. .read_map = blkiocg_file_read_map,
  1101. },
  1102. {
  1103. .name = "io_serviced",
  1104. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1105. BLKIO_PROP_io_serviced),
  1106. .read_map = blkiocg_file_read_map,
  1107. },
  1108. {
  1109. .name = "io_service_time",
  1110. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1111. BLKIO_PROP_io_service_time),
  1112. .read_map = blkiocg_file_read_map,
  1113. },
  1114. {
  1115. .name = "io_wait_time",
  1116. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1117. BLKIO_PROP_io_wait_time),
  1118. .read_map = blkiocg_file_read_map,
  1119. },
  1120. {
  1121. .name = "io_merged",
  1122. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1123. BLKIO_PROP_io_merged),
  1124. .read_map = blkiocg_file_read_map,
  1125. },
  1126. {
  1127. .name = "io_queued",
  1128. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1129. BLKIO_PROP_io_queued),
  1130. .read_map = blkiocg_file_read_map,
  1131. },
  1132. {
  1133. .name = "reset_stats",
  1134. .write_u64 = blkiocg_reset_stats,
  1135. },
  1136. #ifdef CONFIG_BLK_DEV_THROTTLING
  1137. {
  1138. .name = "throttle.read_bps_device",
  1139. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1140. BLKIO_THROTL_read_bps_device),
  1141. .read_seq_string = blkiocg_file_read,
  1142. .write_string = blkiocg_file_write,
  1143. .max_write_len = 256,
  1144. },
  1145. {
  1146. .name = "throttle.write_bps_device",
  1147. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1148. BLKIO_THROTL_write_bps_device),
  1149. .read_seq_string = blkiocg_file_read,
  1150. .write_string = blkiocg_file_write,
  1151. .max_write_len = 256,
  1152. },
  1153. {
  1154. .name = "throttle.read_iops_device",
  1155. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1156. BLKIO_THROTL_read_iops_device),
  1157. .read_seq_string = blkiocg_file_read,
  1158. .write_string = blkiocg_file_write,
  1159. .max_write_len = 256,
  1160. },
  1161. {
  1162. .name = "throttle.write_iops_device",
  1163. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1164. BLKIO_THROTL_write_iops_device),
  1165. .read_seq_string = blkiocg_file_read,
  1166. .write_string = blkiocg_file_write,
  1167. .max_write_len = 256,
  1168. },
  1169. {
  1170. .name = "throttle.io_service_bytes",
  1171. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1172. BLKIO_THROTL_io_service_bytes),
  1173. .read_map = blkiocg_file_read_map,
  1174. },
  1175. {
  1176. .name = "throttle.io_serviced",
  1177. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1178. BLKIO_THROTL_io_serviced),
  1179. .read_map = blkiocg_file_read_map,
  1180. },
  1181. #endif /* CONFIG_BLK_DEV_THROTTLING */
  1182. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1183. {
  1184. .name = "avg_queue_size",
  1185. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1186. BLKIO_PROP_avg_queue_size),
  1187. .read_map = blkiocg_file_read_map,
  1188. },
  1189. {
  1190. .name = "group_wait_time",
  1191. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1192. BLKIO_PROP_group_wait_time),
  1193. .read_map = blkiocg_file_read_map,
  1194. },
  1195. {
  1196. .name = "idle_time",
  1197. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1198. BLKIO_PROP_idle_time),
  1199. .read_map = blkiocg_file_read_map,
  1200. },
  1201. {
  1202. .name = "empty_time",
  1203. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1204. BLKIO_PROP_empty_time),
  1205. .read_map = blkiocg_file_read_map,
  1206. },
  1207. {
  1208. .name = "dequeue",
  1209. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1210. BLKIO_PROP_dequeue),
  1211. .read_map = blkiocg_file_read_map,
  1212. },
  1213. {
  1214. .name = "unaccounted_time",
  1215. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1216. BLKIO_PROP_unaccounted_time),
  1217. .read_map = blkiocg_file_read_map,
  1218. },
  1219. #endif
  1220. };
  1221. static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1222. {
  1223. return cgroup_add_files(cgroup, subsys, blkio_files,
  1224. ARRAY_SIZE(blkio_files));
  1225. }
  1226. static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
  1227. struct cgroup *cgroup)
  1228. {
  1229. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  1230. unsigned long flags;
  1231. struct blkio_group *blkg;
  1232. struct request_queue *q;
  1233. struct blkio_policy_type *blkiop;
  1234. rcu_read_lock();
  1235. do {
  1236. spin_lock_irqsave(&blkcg->lock, flags);
  1237. if (hlist_empty(&blkcg->blkg_list)) {
  1238. spin_unlock_irqrestore(&blkcg->lock, flags);
  1239. break;
  1240. }
  1241. blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
  1242. blkcg_node);
  1243. q = rcu_dereference(blkg->q);
  1244. __blkiocg_del_blkio_group(blkg);
  1245. spin_unlock_irqrestore(&blkcg->lock, flags);
  1246. /*
  1247. * This blkio_group is being unlinked as associated cgroup is
  1248. * going away. Let all the IO controlling policies know about
  1249. * this event.
  1250. */
  1251. spin_lock(&blkio_list_lock);
  1252. list_for_each_entry(blkiop, &blkio_list, list) {
  1253. if (blkiop->plid != blkg->plid)
  1254. continue;
  1255. blkiop->ops.blkio_unlink_group_fn(q, blkg);
  1256. }
  1257. spin_unlock(&blkio_list_lock);
  1258. } while (1);
  1259. rcu_read_unlock();
  1260. return 0;
  1261. }
  1262. static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1263. {
  1264. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  1265. if (blkcg != &blkio_root_cgroup)
  1266. kfree(blkcg);
  1267. }
  1268. static struct cgroup_subsys_state *
  1269. blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1270. {
  1271. struct blkio_cgroup *blkcg;
  1272. struct cgroup *parent = cgroup->parent;
  1273. if (!parent) {
  1274. blkcg = &blkio_root_cgroup;
  1275. goto done;
  1276. }
  1277. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  1278. if (!blkcg)
  1279. return ERR_PTR(-ENOMEM);
  1280. blkcg->weight = BLKIO_WEIGHT_DEFAULT;
  1281. done:
  1282. spin_lock_init(&blkcg->lock);
  1283. INIT_HLIST_HEAD(&blkcg->blkg_list);
  1284. return &blkcg->css;
  1285. }
  1286. /**
  1287. * blkcg_init_queue - initialize blkcg part of request queue
  1288. * @q: request_queue to initialize
  1289. *
  1290. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  1291. * part of new request_queue @q.
  1292. *
  1293. * RETURNS:
  1294. * 0 on success, -errno on failure.
  1295. */
  1296. int blkcg_init_queue(struct request_queue *q)
  1297. {
  1298. int ret;
  1299. might_sleep();
  1300. ret = blk_throtl_init(q);
  1301. if (ret)
  1302. return ret;
  1303. mutex_lock(&all_q_mutex);
  1304. INIT_LIST_HEAD(&q->all_q_node);
  1305. list_add_tail(&q->all_q_node, &all_q_list);
  1306. mutex_unlock(&all_q_mutex);
  1307. return 0;
  1308. }
  1309. /**
  1310. * blkcg_drain_queue - drain blkcg part of request_queue
  1311. * @q: request_queue to drain
  1312. *
  1313. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  1314. */
  1315. void blkcg_drain_queue(struct request_queue *q)
  1316. {
  1317. lockdep_assert_held(q->queue_lock);
  1318. blk_throtl_drain(q);
  1319. }
  1320. /**
  1321. * blkcg_exit_queue - exit and release blkcg part of request_queue
  1322. * @q: request_queue being released
  1323. *
  1324. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  1325. */
  1326. void blkcg_exit_queue(struct request_queue *q)
  1327. {
  1328. mutex_lock(&all_q_mutex);
  1329. list_del_init(&q->all_q_node);
  1330. mutex_unlock(&all_q_mutex);
  1331. blk_throtl_exit(q);
  1332. }
  1333. /*
  1334. * We cannot support shared io contexts, as we have no mean to support
  1335. * two tasks with the same ioc in two different groups without major rework
  1336. * of the main cic data structures. For now we allow a task to change
  1337. * its cgroup only if it's the only owner of its ioc.
  1338. */
  1339. static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  1340. struct cgroup_taskset *tset)
  1341. {
  1342. struct task_struct *task;
  1343. struct io_context *ioc;
  1344. int ret = 0;
  1345. /* task_lock() is needed to avoid races with exit_io_context() */
  1346. cgroup_taskset_for_each(task, cgrp, tset) {
  1347. task_lock(task);
  1348. ioc = task->io_context;
  1349. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  1350. ret = -EINVAL;
  1351. task_unlock(task);
  1352. if (ret)
  1353. break;
  1354. }
  1355. return ret;
  1356. }
  1357. static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  1358. struct cgroup_taskset *tset)
  1359. {
  1360. struct task_struct *task;
  1361. struct io_context *ioc;
  1362. cgroup_taskset_for_each(task, cgrp, tset) {
  1363. /* we don't lose anything even if ioc allocation fails */
  1364. ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
  1365. if (ioc) {
  1366. ioc_cgroup_changed(ioc);
  1367. put_io_context(ioc);
  1368. }
  1369. }
  1370. }
  1371. static void blkcg_bypass_start(void)
  1372. __acquires(&all_q_mutex)
  1373. {
  1374. struct request_queue *q;
  1375. mutex_lock(&all_q_mutex);
  1376. list_for_each_entry(q, &all_q_list, all_q_node) {
  1377. blk_queue_bypass_start(q);
  1378. blkg_destroy_all(q);
  1379. }
  1380. }
  1381. static void blkcg_bypass_end(void)
  1382. __releases(&all_q_mutex)
  1383. {
  1384. struct request_queue *q;
  1385. list_for_each_entry(q, &all_q_list, all_q_node)
  1386. blk_queue_bypass_end(q);
  1387. mutex_unlock(&all_q_mutex);
  1388. }
  1389. void blkio_policy_register(struct blkio_policy_type *blkiop)
  1390. {
  1391. blkcg_bypass_start();
  1392. spin_lock(&blkio_list_lock);
  1393. BUG_ON(blkio_policy[blkiop->plid]);
  1394. blkio_policy[blkiop->plid] = blkiop;
  1395. list_add_tail(&blkiop->list, &blkio_list);
  1396. spin_unlock(&blkio_list_lock);
  1397. blkcg_bypass_end();
  1398. }
  1399. EXPORT_SYMBOL_GPL(blkio_policy_register);
  1400. void blkio_policy_unregister(struct blkio_policy_type *blkiop)
  1401. {
  1402. blkcg_bypass_start();
  1403. spin_lock(&blkio_list_lock);
  1404. BUG_ON(blkio_policy[blkiop->plid] != blkiop);
  1405. blkio_policy[blkiop->plid] = NULL;
  1406. list_del_init(&blkiop->list);
  1407. spin_unlock(&blkio_list_lock);
  1408. blkcg_bypass_end();
  1409. }
  1410. EXPORT_SYMBOL_GPL(blkio_policy_unregister);