blk-cgroup.c 39 KB

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