blk-cgroup.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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. /**
  366. * blkg_free - free a blkg
  367. * @blkg: blkg to free
  368. *
  369. * Free @blkg which may be partially allocated.
  370. */
  371. static void blkg_free(struct blkio_group *blkg)
  372. {
  373. if (blkg) {
  374. free_percpu(blkg->stats_cpu);
  375. kfree(blkg->pd);
  376. kfree(blkg);
  377. }
  378. }
  379. /**
  380. * blkg_alloc - allocate a blkg
  381. * @blkcg: block cgroup the new blkg is associated with
  382. * @q: request_queue the new blkg is associated with
  383. * @pol: policy the new blkg is associated with
  384. *
  385. * Allocate a new blkg assocating @blkcg and @q for @pol.
  386. *
  387. * FIXME: Should be called with queue locked but currently isn't due to
  388. * percpu stat breakage.
  389. */
  390. static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
  391. struct request_queue *q,
  392. struct blkio_policy_type *pol)
  393. {
  394. struct blkio_group *blkg;
  395. /* alloc and init base part */
  396. blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
  397. if (!blkg)
  398. return NULL;
  399. spin_lock_init(&blkg->stats_lock);
  400. rcu_assign_pointer(blkg->q, q);
  401. blkg->blkcg = blkcg;
  402. blkg->plid = pol->plid;
  403. blkg->refcnt = 1;
  404. cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
  405. /* alloc per-policy data */
  406. blkg->pd = kzalloc_node(sizeof(*blkg->pd) + pol->pdata_size, GFP_ATOMIC,
  407. q->node);
  408. if (!blkg->pd) {
  409. blkg_free(blkg);
  410. return NULL;
  411. }
  412. /* broken, read comment in the callsite */
  413. blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
  414. if (!blkg->stats_cpu) {
  415. blkg_free(blkg);
  416. return NULL;
  417. }
  418. /* attach pd to blkg and invoke per-policy init */
  419. blkg->pd->blkg = blkg;
  420. pol->ops.blkio_init_group_fn(blkg);
  421. return blkg;
  422. }
  423. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  424. struct request_queue *q,
  425. enum blkio_policy_id plid,
  426. bool for_root)
  427. __releases(q->queue_lock) __acquires(q->queue_lock)
  428. {
  429. struct blkio_policy_type *pol = blkio_policy[plid];
  430. struct blkio_group *blkg, *new_blkg;
  431. WARN_ON_ONCE(!rcu_read_lock_held());
  432. lockdep_assert_held(q->queue_lock);
  433. /*
  434. * This could be the first entry point of blkcg implementation and
  435. * we shouldn't allow anything to go through for a bypassing queue.
  436. * The following can be removed if blkg lookup is guaranteed to
  437. * fail on a bypassing queue.
  438. */
  439. if (unlikely(blk_queue_bypass(q)) && !for_root)
  440. return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
  441. blkg = blkg_lookup(blkcg, q, plid);
  442. if (blkg)
  443. return blkg;
  444. /* blkg holds a reference to blkcg */
  445. if (!css_tryget(&blkcg->css))
  446. return ERR_PTR(-EINVAL);
  447. /*
  448. * Allocate and initialize.
  449. *
  450. * FIXME: The following is broken. Percpu memory allocation
  451. * requires %GFP_KERNEL context and can't be performed from IO
  452. * path. Allocation here should inherently be atomic and the
  453. * following lock dancing can be removed once the broken percpu
  454. * allocation is fixed.
  455. */
  456. spin_unlock_irq(q->queue_lock);
  457. rcu_read_unlock();
  458. new_blkg = blkg_alloc(blkcg, q, pol);
  459. rcu_read_lock();
  460. spin_lock_irq(q->queue_lock);
  461. /* did bypass get turned on inbetween? */
  462. if (unlikely(blk_queue_bypass(q)) && !for_root) {
  463. blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
  464. goto out;
  465. }
  466. /* did someone beat us to it? */
  467. blkg = blkg_lookup(blkcg, q, plid);
  468. if (unlikely(blkg))
  469. goto out;
  470. /* did alloc fail? */
  471. if (unlikely(!new_blkg)) {
  472. blkg = ERR_PTR(-ENOMEM);
  473. goto out;
  474. }
  475. /* insert */
  476. spin_lock(&blkcg->lock);
  477. swap(blkg, new_blkg);
  478. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  479. pol->ops.blkio_link_group_fn(q, blkg);
  480. spin_unlock(&blkcg->lock);
  481. out:
  482. blkg_free(new_blkg);
  483. return blkg;
  484. }
  485. EXPORT_SYMBOL_GPL(blkg_lookup_create);
  486. static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
  487. {
  488. hlist_del_init_rcu(&blkg->blkcg_node);
  489. }
  490. /*
  491. * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
  492. * indicating that blk_group was unhashed by the time we got to it.
  493. */
  494. int blkiocg_del_blkio_group(struct blkio_group *blkg)
  495. {
  496. struct blkio_cgroup *blkcg = blkg->blkcg;
  497. unsigned long flags;
  498. int ret = 1;
  499. spin_lock_irqsave(&blkcg->lock, flags);
  500. if (!hlist_unhashed(&blkg->blkcg_node)) {
  501. __blkiocg_del_blkio_group(blkg);
  502. ret = 0;
  503. }
  504. spin_unlock_irqrestore(&blkcg->lock, flags);
  505. return ret;
  506. }
  507. EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
  508. /* called under rcu_read_lock(). */
  509. struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  510. struct request_queue *q,
  511. enum blkio_policy_id plid)
  512. {
  513. struct blkio_group *blkg;
  514. struct hlist_node *n;
  515. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
  516. if (blkg->q == q && blkg->plid == plid)
  517. return blkg;
  518. return NULL;
  519. }
  520. EXPORT_SYMBOL_GPL(blkg_lookup);
  521. void blkg_destroy_all(struct request_queue *q)
  522. {
  523. struct blkio_policy_type *pol;
  524. while (true) {
  525. bool done = true;
  526. spin_lock(&blkio_list_lock);
  527. spin_lock_irq(q->queue_lock);
  528. /*
  529. * clear_queue_fn() might return with non-empty group list
  530. * if it raced cgroup removal and lost. cgroup removal is
  531. * guaranteed to make forward progress and retrying after a
  532. * while is enough. This ugliness is scheduled to be
  533. * removed after locking update.
  534. */
  535. list_for_each_entry(pol, &blkio_list, list)
  536. if (!pol->ops.blkio_clear_queue_fn(q))
  537. done = false;
  538. spin_unlock_irq(q->queue_lock);
  539. spin_unlock(&blkio_list_lock);
  540. if (done)
  541. break;
  542. msleep(10); /* just some random duration I like */
  543. }
  544. }
  545. static void blkg_rcu_free(struct rcu_head *rcu_head)
  546. {
  547. blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
  548. }
  549. void __blkg_release(struct blkio_group *blkg)
  550. {
  551. /* release the extra blkcg reference this blkg has been holding */
  552. css_put(&blkg->blkcg->css);
  553. /*
  554. * A group is freed in rcu manner. But having an rcu lock does not
  555. * mean that one can access all the fields of blkg and assume these
  556. * are valid. For example, don't try to follow throtl_data and
  557. * request queue links.
  558. *
  559. * Having a reference to blkg under an rcu allows acess to only
  560. * values local to groups like group stats and group rate limits
  561. */
  562. call_rcu(&blkg->rcu_head, blkg_rcu_free);
  563. }
  564. EXPORT_SYMBOL_GPL(__blkg_release);
  565. static void blkio_reset_stats_cpu(struct blkio_group *blkg)
  566. {
  567. struct blkio_group_stats_cpu *stats_cpu;
  568. int i, j, k;
  569. /*
  570. * Note: On 64 bit arch this should not be an issue. This has the
  571. * possibility of returning some inconsistent value on 32bit arch
  572. * as 64bit update on 32bit is non atomic. Taking care of this
  573. * corner case makes code very complicated, like sending IPIs to
  574. * cpus, taking care of stats of offline cpus etc.
  575. *
  576. * reset stats is anyway more of a debug feature and this sounds a
  577. * corner case. So I am not complicating the code yet until and
  578. * unless this becomes a real issue.
  579. */
  580. for_each_possible_cpu(i) {
  581. stats_cpu = per_cpu_ptr(blkg->stats_cpu, i);
  582. stats_cpu->sectors = 0;
  583. for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
  584. for (k = 0; k < BLKIO_STAT_TOTAL; k++)
  585. stats_cpu->stat_arr_cpu[j][k] = 0;
  586. }
  587. }
  588. static int
  589. blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
  590. {
  591. struct blkio_cgroup *blkcg;
  592. struct blkio_group *blkg;
  593. struct blkio_group_stats *stats;
  594. struct hlist_node *n;
  595. uint64_t queued[BLKIO_STAT_TOTAL];
  596. int i;
  597. #ifdef CONFIG_DEBUG_BLK_CGROUP
  598. bool idling, waiting, empty;
  599. unsigned long long now = sched_clock();
  600. #endif
  601. blkcg = cgroup_to_blkio_cgroup(cgroup);
  602. spin_lock_irq(&blkcg->lock);
  603. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  604. spin_lock(&blkg->stats_lock);
  605. stats = &blkg->stats;
  606. #ifdef CONFIG_DEBUG_BLK_CGROUP
  607. idling = blkio_blkg_idling(stats);
  608. waiting = blkio_blkg_waiting(stats);
  609. empty = blkio_blkg_empty(stats);
  610. #endif
  611. for (i = 0; i < BLKIO_STAT_TOTAL; i++)
  612. queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
  613. memset(stats, 0, sizeof(struct blkio_group_stats));
  614. for (i = 0; i < BLKIO_STAT_TOTAL; i++)
  615. stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
  616. #ifdef CONFIG_DEBUG_BLK_CGROUP
  617. if (idling) {
  618. blkio_mark_blkg_idling(stats);
  619. stats->start_idle_time = now;
  620. }
  621. if (waiting) {
  622. blkio_mark_blkg_waiting(stats);
  623. stats->start_group_wait_time = now;
  624. }
  625. if (empty) {
  626. blkio_mark_blkg_empty(stats);
  627. stats->start_empty_time = now;
  628. }
  629. #endif
  630. spin_unlock(&blkg->stats_lock);
  631. /* Reset Per cpu stats which don't take blkg->stats_lock */
  632. blkio_reset_stats_cpu(blkg);
  633. }
  634. spin_unlock_irq(&blkcg->lock);
  635. return 0;
  636. }
  637. static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
  638. char *str, int chars_left, bool diskname_only)
  639. {
  640. snprintf(str, chars_left, "%s", dname);
  641. chars_left -= strlen(str);
  642. if (chars_left <= 0) {
  643. printk(KERN_WARNING
  644. "Possibly incorrect cgroup stat display format");
  645. return;
  646. }
  647. if (diskname_only)
  648. return;
  649. switch (type) {
  650. case BLKIO_STAT_READ:
  651. strlcat(str, " Read", chars_left);
  652. break;
  653. case BLKIO_STAT_WRITE:
  654. strlcat(str, " Write", chars_left);
  655. break;
  656. case BLKIO_STAT_SYNC:
  657. strlcat(str, " Sync", chars_left);
  658. break;
  659. case BLKIO_STAT_ASYNC:
  660. strlcat(str, " Async", chars_left);
  661. break;
  662. case BLKIO_STAT_TOTAL:
  663. strlcat(str, " Total", chars_left);
  664. break;
  665. default:
  666. strlcat(str, " Invalid", chars_left);
  667. }
  668. }
  669. static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
  670. struct cgroup_map_cb *cb, const char *dname)
  671. {
  672. blkio_get_key_name(0, dname, str, chars_left, true);
  673. cb->fill(cb, str, val);
  674. return val;
  675. }
  676. static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
  677. enum stat_type_cpu type, enum stat_sub_type sub_type)
  678. {
  679. int cpu;
  680. struct blkio_group_stats_cpu *stats_cpu;
  681. u64 val = 0, tval;
  682. for_each_possible_cpu(cpu) {
  683. unsigned int start;
  684. stats_cpu = per_cpu_ptr(blkg->stats_cpu, cpu);
  685. do {
  686. start = u64_stats_fetch_begin(&stats_cpu->syncp);
  687. if (type == BLKIO_STAT_CPU_SECTORS)
  688. tval = stats_cpu->sectors;
  689. else
  690. tval = stats_cpu->stat_arr_cpu[type][sub_type];
  691. } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
  692. val += tval;
  693. }
  694. return val;
  695. }
  696. static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
  697. struct cgroup_map_cb *cb, const char *dname,
  698. enum stat_type_cpu type)
  699. {
  700. uint64_t disk_total, val;
  701. char key_str[MAX_KEY_LEN];
  702. enum stat_sub_type sub_type;
  703. if (type == BLKIO_STAT_CPU_SECTORS) {
  704. val = blkio_read_stat_cpu(blkg, type, 0);
  705. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb,
  706. dname);
  707. }
  708. for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
  709. sub_type++) {
  710. blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
  711. false);
  712. val = blkio_read_stat_cpu(blkg, type, sub_type);
  713. cb->fill(cb, key_str, val);
  714. }
  715. disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
  716. blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
  717. blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
  718. false);
  719. cb->fill(cb, key_str, disk_total);
  720. return disk_total;
  721. }
  722. /* This should be called with blkg->stats_lock held */
  723. static uint64_t blkio_get_stat(struct blkio_group *blkg,
  724. struct cgroup_map_cb *cb, const char *dname,
  725. enum stat_type type)
  726. {
  727. uint64_t disk_total;
  728. char key_str[MAX_KEY_LEN];
  729. enum stat_sub_type sub_type;
  730. if (type == BLKIO_STAT_TIME)
  731. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  732. blkg->stats.time, cb, dname);
  733. #ifdef CONFIG_DEBUG_BLK_CGROUP
  734. if (type == BLKIO_STAT_UNACCOUNTED_TIME)
  735. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  736. blkg->stats.unaccounted_time, cb, dname);
  737. if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
  738. uint64_t sum = blkg->stats.avg_queue_size_sum;
  739. uint64_t samples = blkg->stats.avg_queue_size_samples;
  740. if (samples)
  741. do_div(sum, samples);
  742. else
  743. sum = 0;
  744. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  745. sum, cb, dname);
  746. }
  747. if (type == BLKIO_STAT_GROUP_WAIT_TIME)
  748. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  749. blkg->stats.group_wait_time, cb, dname);
  750. if (type == BLKIO_STAT_IDLE_TIME)
  751. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  752. blkg->stats.idle_time, cb, dname);
  753. if (type == BLKIO_STAT_EMPTY_TIME)
  754. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  755. blkg->stats.empty_time, cb, dname);
  756. if (type == BLKIO_STAT_DEQUEUE)
  757. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  758. blkg->stats.dequeue, cb, dname);
  759. #endif
  760. for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
  761. sub_type++) {
  762. blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
  763. false);
  764. cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
  765. }
  766. disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
  767. blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
  768. blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
  769. false);
  770. cb->fill(cb, key_str, disk_total);
  771. return disk_total;
  772. }
  773. static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
  774. int fileid, struct blkio_cgroup *blkcg)
  775. {
  776. struct gendisk *disk = NULL;
  777. struct blkio_group *blkg = NULL;
  778. char *s[4], *p, *major_s = NULL, *minor_s = NULL;
  779. unsigned long major, minor;
  780. int i = 0, ret = -EINVAL;
  781. int part;
  782. dev_t dev;
  783. u64 temp;
  784. memset(s, 0, sizeof(s));
  785. while ((p = strsep(&buf, " ")) != NULL) {
  786. if (!*p)
  787. continue;
  788. s[i++] = p;
  789. /* Prevent from inputing too many things */
  790. if (i == 3)
  791. break;
  792. }
  793. if (i != 2)
  794. goto out;
  795. p = strsep(&s[0], ":");
  796. if (p != NULL)
  797. major_s = p;
  798. else
  799. goto out;
  800. minor_s = s[0];
  801. if (!minor_s)
  802. goto out;
  803. if (strict_strtoul(major_s, 10, &major))
  804. goto out;
  805. if (strict_strtoul(minor_s, 10, &minor))
  806. goto out;
  807. dev = MKDEV(major, minor);
  808. if (strict_strtoull(s[1], 10, &temp))
  809. goto out;
  810. disk = get_gendisk(dev, &part);
  811. if (!disk || part)
  812. goto out;
  813. rcu_read_lock();
  814. spin_lock_irq(disk->queue->queue_lock);
  815. blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
  816. spin_unlock_irq(disk->queue->queue_lock);
  817. if (IS_ERR(blkg)) {
  818. ret = PTR_ERR(blkg);
  819. goto out_unlock;
  820. }
  821. switch (plid) {
  822. case BLKIO_POLICY_PROP:
  823. if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
  824. temp > BLKIO_WEIGHT_MAX)
  825. goto out_unlock;
  826. blkg->conf.weight = temp;
  827. blkio_update_group_weight(blkg, temp ?: blkcg->weight);
  828. break;
  829. case BLKIO_POLICY_THROTL:
  830. switch(fileid) {
  831. case BLKIO_THROTL_read_bps_device:
  832. blkg->conf.bps[READ] = temp;
  833. blkio_update_group_bps(blkg, temp ?: -1, fileid);
  834. break;
  835. case BLKIO_THROTL_write_bps_device:
  836. blkg->conf.bps[WRITE] = temp;
  837. blkio_update_group_bps(blkg, temp ?: -1, fileid);
  838. break;
  839. case BLKIO_THROTL_read_iops_device:
  840. if (temp > THROTL_IOPS_MAX)
  841. goto out_unlock;
  842. blkg->conf.iops[READ] = temp;
  843. blkio_update_group_iops(blkg, temp ?: -1, fileid);
  844. break;
  845. case BLKIO_THROTL_write_iops_device:
  846. if (temp > THROTL_IOPS_MAX)
  847. goto out_unlock;
  848. blkg->conf.iops[WRITE] = temp;
  849. blkio_update_group_iops(blkg, temp ?: -1, fileid);
  850. break;
  851. }
  852. break;
  853. default:
  854. BUG();
  855. }
  856. ret = 0;
  857. out_unlock:
  858. rcu_read_unlock();
  859. out:
  860. put_disk(disk);
  861. /*
  862. * If queue was bypassing, we should retry. Do so after a short
  863. * msleep(). It isn't strictly necessary but queue can be
  864. * bypassing for some time and it's always nice to avoid busy
  865. * looping.
  866. */
  867. if (ret == -EBUSY) {
  868. msleep(10);
  869. return restart_syscall();
  870. }
  871. return ret;
  872. }
  873. static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
  874. const char *buffer)
  875. {
  876. int ret = 0;
  877. char *buf;
  878. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
  879. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  880. int fileid = BLKIOFILE_ATTR(cft->private);
  881. buf = kstrdup(buffer, GFP_KERNEL);
  882. if (!buf)
  883. return -ENOMEM;
  884. ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
  885. kfree(buf);
  886. return ret;
  887. }
  888. static const char *blkg_dev_name(struct blkio_group *blkg)
  889. {
  890. /* some drivers (floppy) instantiate a queue w/o disk registered */
  891. if (blkg->q->backing_dev_info.dev)
  892. return dev_name(blkg->q->backing_dev_info.dev);
  893. return NULL;
  894. }
  895. static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
  896. struct seq_file *m)
  897. {
  898. const char *dname = blkg_dev_name(blkg);
  899. int fileid = BLKIOFILE_ATTR(cft->private);
  900. int rw = WRITE;
  901. if (!dname)
  902. return;
  903. switch (blkg->plid) {
  904. case BLKIO_POLICY_PROP:
  905. if (blkg->conf.weight)
  906. seq_printf(m, "%s\t%u\n",
  907. dname, blkg->conf.weight);
  908. break;
  909. case BLKIO_POLICY_THROTL:
  910. switch (fileid) {
  911. case BLKIO_THROTL_read_bps_device:
  912. rw = READ;
  913. case BLKIO_THROTL_write_bps_device:
  914. if (blkg->conf.bps[rw])
  915. seq_printf(m, "%s\t%llu\n",
  916. dname, blkg->conf.bps[rw]);
  917. break;
  918. case BLKIO_THROTL_read_iops_device:
  919. rw = READ;
  920. case BLKIO_THROTL_write_iops_device:
  921. if (blkg->conf.iops[rw])
  922. seq_printf(m, "%s\t%u\n",
  923. dname, blkg->conf.iops[rw]);
  924. break;
  925. }
  926. break;
  927. default:
  928. BUG();
  929. }
  930. }
  931. /* cgroup files which read their data from policy nodes end up here */
  932. static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
  933. struct seq_file *m)
  934. {
  935. struct blkio_group *blkg;
  936. struct hlist_node *n;
  937. spin_lock_irq(&blkcg->lock);
  938. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
  939. if (BLKIOFILE_POLICY(cft->private) == blkg->plid)
  940. blkio_print_group_conf(cft, blkg, m);
  941. spin_unlock_irq(&blkcg->lock);
  942. }
  943. static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
  944. struct seq_file *m)
  945. {
  946. struct blkio_cgroup *blkcg;
  947. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  948. int name = BLKIOFILE_ATTR(cft->private);
  949. blkcg = cgroup_to_blkio_cgroup(cgrp);
  950. switch(plid) {
  951. case BLKIO_POLICY_PROP:
  952. switch(name) {
  953. case BLKIO_PROP_weight_device:
  954. blkio_read_conf(cft, blkcg, m);
  955. return 0;
  956. default:
  957. BUG();
  958. }
  959. break;
  960. case BLKIO_POLICY_THROTL:
  961. switch(name){
  962. case BLKIO_THROTL_read_bps_device:
  963. case BLKIO_THROTL_write_bps_device:
  964. case BLKIO_THROTL_read_iops_device:
  965. case BLKIO_THROTL_write_iops_device:
  966. blkio_read_conf(cft, blkcg, m);
  967. return 0;
  968. default:
  969. BUG();
  970. }
  971. break;
  972. default:
  973. BUG();
  974. }
  975. return 0;
  976. }
  977. static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
  978. struct cftype *cft, struct cgroup_map_cb *cb,
  979. enum stat_type type, bool show_total, bool pcpu)
  980. {
  981. struct blkio_group *blkg;
  982. struct hlist_node *n;
  983. uint64_t cgroup_total = 0;
  984. rcu_read_lock();
  985. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
  986. const char *dname = blkg_dev_name(blkg);
  987. if (!dname || BLKIOFILE_POLICY(cft->private) != blkg->plid)
  988. continue;
  989. if (pcpu)
  990. cgroup_total += blkio_get_stat_cpu(blkg, cb, dname,
  991. type);
  992. else {
  993. spin_lock_irq(&blkg->stats_lock);
  994. cgroup_total += blkio_get_stat(blkg, cb, dname, type);
  995. spin_unlock_irq(&blkg->stats_lock);
  996. }
  997. }
  998. if (show_total)
  999. cb->fill(cb, "Total", cgroup_total);
  1000. rcu_read_unlock();
  1001. return 0;
  1002. }
  1003. /* All map kind of cgroup file get serviced by this function */
  1004. static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
  1005. struct cgroup_map_cb *cb)
  1006. {
  1007. struct blkio_cgroup *blkcg;
  1008. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1009. int name = BLKIOFILE_ATTR(cft->private);
  1010. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1011. switch(plid) {
  1012. case BLKIO_POLICY_PROP:
  1013. switch(name) {
  1014. case BLKIO_PROP_time:
  1015. return blkio_read_blkg_stats(blkcg, cft, cb,
  1016. BLKIO_STAT_TIME, 0, 0);
  1017. case BLKIO_PROP_sectors:
  1018. return blkio_read_blkg_stats(blkcg, cft, cb,
  1019. BLKIO_STAT_CPU_SECTORS, 0, 1);
  1020. case BLKIO_PROP_io_service_bytes:
  1021. return blkio_read_blkg_stats(blkcg, cft, cb,
  1022. BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
  1023. case BLKIO_PROP_io_serviced:
  1024. return blkio_read_blkg_stats(blkcg, cft, cb,
  1025. BLKIO_STAT_CPU_SERVICED, 1, 1);
  1026. case BLKIO_PROP_io_service_time:
  1027. return blkio_read_blkg_stats(blkcg, cft, cb,
  1028. BLKIO_STAT_SERVICE_TIME, 1, 0);
  1029. case BLKIO_PROP_io_wait_time:
  1030. return blkio_read_blkg_stats(blkcg, cft, cb,
  1031. BLKIO_STAT_WAIT_TIME, 1, 0);
  1032. case BLKIO_PROP_io_merged:
  1033. return blkio_read_blkg_stats(blkcg, cft, cb,
  1034. BLKIO_STAT_CPU_MERGED, 1, 1);
  1035. case BLKIO_PROP_io_queued:
  1036. return blkio_read_blkg_stats(blkcg, cft, cb,
  1037. BLKIO_STAT_QUEUED, 1, 0);
  1038. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1039. case BLKIO_PROP_unaccounted_time:
  1040. return blkio_read_blkg_stats(blkcg, cft, cb,
  1041. BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
  1042. case BLKIO_PROP_dequeue:
  1043. return blkio_read_blkg_stats(blkcg, cft, cb,
  1044. BLKIO_STAT_DEQUEUE, 0, 0);
  1045. case BLKIO_PROP_avg_queue_size:
  1046. return blkio_read_blkg_stats(blkcg, cft, cb,
  1047. BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
  1048. case BLKIO_PROP_group_wait_time:
  1049. return blkio_read_blkg_stats(blkcg, cft, cb,
  1050. BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
  1051. case BLKIO_PROP_idle_time:
  1052. return blkio_read_blkg_stats(blkcg, cft, cb,
  1053. BLKIO_STAT_IDLE_TIME, 0, 0);
  1054. case BLKIO_PROP_empty_time:
  1055. return blkio_read_blkg_stats(blkcg, cft, cb,
  1056. BLKIO_STAT_EMPTY_TIME, 0, 0);
  1057. #endif
  1058. default:
  1059. BUG();
  1060. }
  1061. break;
  1062. case BLKIO_POLICY_THROTL:
  1063. switch(name){
  1064. case BLKIO_THROTL_io_service_bytes:
  1065. return blkio_read_blkg_stats(blkcg, cft, cb,
  1066. BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
  1067. case BLKIO_THROTL_io_serviced:
  1068. return blkio_read_blkg_stats(blkcg, cft, cb,
  1069. BLKIO_STAT_CPU_SERVICED, 1, 1);
  1070. default:
  1071. BUG();
  1072. }
  1073. break;
  1074. default:
  1075. BUG();
  1076. }
  1077. return 0;
  1078. }
  1079. static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
  1080. {
  1081. struct blkio_group *blkg;
  1082. struct hlist_node *n;
  1083. if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
  1084. return -EINVAL;
  1085. spin_lock(&blkio_list_lock);
  1086. spin_lock_irq(&blkcg->lock);
  1087. blkcg->weight = (unsigned int)val;
  1088. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
  1089. if (blkg->plid == plid && !blkg->conf.weight)
  1090. blkio_update_group_weight(blkg, blkcg->weight);
  1091. spin_unlock_irq(&blkcg->lock);
  1092. spin_unlock(&blkio_list_lock);
  1093. return 0;
  1094. }
  1095. static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
  1096. struct blkio_cgroup *blkcg;
  1097. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1098. int name = BLKIOFILE_ATTR(cft->private);
  1099. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1100. switch(plid) {
  1101. case BLKIO_POLICY_PROP:
  1102. switch(name) {
  1103. case BLKIO_PROP_weight:
  1104. return (u64)blkcg->weight;
  1105. }
  1106. break;
  1107. default:
  1108. BUG();
  1109. }
  1110. return 0;
  1111. }
  1112. static int
  1113. blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
  1114. {
  1115. struct blkio_cgroup *blkcg;
  1116. enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
  1117. int name = BLKIOFILE_ATTR(cft->private);
  1118. blkcg = cgroup_to_blkio_cgroup(cgrp);
  1119. switch(plid) {
  1120. case BLKIO_POLICY_PROP:
  1121. switch(name) {
  1122. case BLKIO_PROP_weight:
  1123. return blkio_weight_write(blkcg, plid, val);
  1124. }
  1125. break;
  1126. default:
  1127. BUG();
  1128. }
  1129. return 0;
  1130. }
  1131. struct cftype blkio_files[] = {
  1132. {
  1133. .name = "weight_device",
  1134. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1135. BLKIO_PROP_weight_device),
  1136. .read_seq_string = blkiocg_file_read,
  1137. .write_string = blkiocg_file_write,
  1138. .max_write_len = 256,
  1139. },
  1140. {
  1141. .name = "weight",
  1142. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1143. BLKIO_PROP_weight),
  1144. .read_u64 = blkiocg_file_read_u64,
  1145. .write_u64 = blkiocg_file_write_u64,
  1146. },
  1147. {
  1148. .name = "time",
  1149. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1150. BLKIO_PROP_time),
  1151. .read_map = blkiocg_file_read_map,
  1152. },
  1153. {
  1154. .name = "sectors",
  1155. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1156. BLKIO_PROP_sectors),
  1157. .read_map = blkiocg_file_read_map,
  1158. },
  1159. {
  1160. .name = "io_service_bytes",
  1161. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1162. BLKIO_PROP_io_service_bytes),
  1163. .read_map = blkiocg_file_read_map,
  1164. },
  1165. {
  1166. .name = "io_serviced",
  1167. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1168. BLKIO_PROP_io_serviced),
  1169. .read_map = blkiocg_file_read_map,
  1170. },
  1171. {
  1172. .name = "io_service_time",
  1173. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1174. BLKIO_PROP_io_service_time),
  1175. .read_map = blkiocg_file_read_map,
  1176. },
  1177. {
  1178. .name = "io_wait_time",
  1179. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1180. BLKIO_PROP_io_wait_time),
  1181. .read_map = blkiocg_file_read_map,
  1182. },
  1183. {
  1184. .name = "io_merged",
  1185. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1186. BLKIO_PROP_io_merged),
  1187. .read_map = blkiocg_file_read_map,
  1188. },
  1189. {
  1190. .name = "io_queued",
  1191. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1192. BLKIO_PROP_io_queued),
  1193. .read_map = blkiocg_file_read_map,
  1194. },
  1195. {
  1196. .name = "reset_stats",
  1197. .write_u64 = blkiocg_reset_stats,
  1198. },
  1199. #ifdef CONFIG_BLK_DEV_THROTTLING
  1200. {
  1201. .name = "throttle.read_bps_device",
  1202. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1203. BLKIO_THROTL_read_bps_device),
  1204. .read_seq_string = blkiocg_file_read,
  1205. .write_string = blkiocg_file_write,
  1206. .max_write_len = 256,
  1207. },
  1208. {
  1209. .name = "throttle.write_bps_device",
  1210. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1211. BLKIO_THROTL_write_bps_device),
  1212. .read_seq_string = blkiocg_file_read,
  1213. .write_string = blkiocg_file_write,
  1214. .max_write_len = 256,
  1215. },
  1216. {
  1217. .name = "throttle.read_iops_device",
  1218. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1219. BLKIO_THROTL_read_iops_device),
  1220. .read_seq_string = blkiocg_file_read,
  1221. .write_string = blkiocg_file_write,
  1222. .max_write_len = 256,
  1223. },
  1224. {
  1225. .name = "throttle.write_iops_device",
  1226. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1227. BLKIO_THROTL_write_iops_device),
  1228. .read_seq_string = blkiocg_file_read,
  1229. .write_string = blkiocg_file_write,
  1230. .max_write_len = 256,
  1231. },
  1232. {
  1233. .name = "throttle.io_service_bytes",
  1234. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1235. BLKIO_THROTL_io_service_bytes),
  1236. .read_map = blkiocg_file_read_map,
  1237. },
  1238. {
  1239. .name = "throttle.io_serviced",
  1240. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
  1241. BLKIO_THROTL_io_serviced),
  1242. .read_map = blkiocg_file_read_map,
  1243. },
  1244. #endif /* CONFIG_BLK_DEV_THROTTLING */
  1245. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1246. {
  1247. .name = "avg_queue_size",
  1248. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1249. BLKIO_PROP_avg_queue_size),
  1250. .read_map = blkiocg_file_read_map,
  1251. },
  1252. {
  1253. .name = "group_wait_time",
  1254. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1255. BLKIO_PROP_group_wait_time),
  1256. .read_map = blkiocg_file_read_map,
  1257. },
  1258. {
  1259. .name = "idle_time",
  1260. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1261. BLKIO_PROP_idle_time),
  1262. .read_map = blkiocg_file_read_map,
  1263. },
  1264. {
  1265. .name = "empty_time",
  1266. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1267. BLKIO_PROP_empty_time),
  1268. .read_map = blkiocg_file_read_map,
  1269. },
  1270. {
  1271. .name = "dequeue",
  1272. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1273. BLKIO_PROP_dequeue),
  1274. .read_map = blkiocg_file_read_map,
  1275. },
  1276. {
  1277. .name = "unaccounted_time",
  1278. .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
  1279. BLKIO_PROP_unaccounted_time),
  1280. .read_map = blkiocg_file_read_map,
  1281. },
  1282. #endif
  1283. };
  1284. static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1285. {
  1286. return cgroup_add_files(cgroup, subsys, blkio_files,
  1287. ARRAY_SIZE(blkio_files));
  1288. }
  1289. static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
  1290. struct cgroup *cgroup)
  1291. {
  1292. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  1293. unsigned long flags;
  1294. struct blkio_group *blkg;
  1295. struct request_queue *q;
  1296. struct blkio_policy_type *blkiop;
  1297. rcu_read_lock();
  1298. do {
  1299. spin_lock_irqsave(&blkcg->lock, flags);
  1300. if (hlist_empty(&blkcg->blkg_list)) {
  1301. spin_unlock_irqrestore(&blkcg->lock, flags);
  1302. break;
  1303. }
  1304. blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
  1305. blkcg_node);
  1306. q = rcu_dereference(blkg->q);
  1307. __blkiocg_del_blkio_group(blkg);
  1308. spin_unlock_irqrestore(&blkcg->lock, flags);
  1309. /*
  1310. * This blkio_group is being unlinked as associated cgroup is
  1311. * going away. Let all the IO controlling policies know about
  1312. * this event.
  1313. */
  1314. spin_lock(&blkio_list_lock);
  1315. list_for_each_entry(blkiop, &blkio_list, list) {
  1316. if (blkiop->plid != blkg->plid)
  1317. continue;
  1318. blkiop->ops.blkio_unlink_group_fn(q, blkg);
  1319. }
  1320. spin_unlock(&blkio_list_lock);
  1321. } while (1);
  1322. rcu_read_unlock();
  1323. return 0;
  1324. }
  1325. static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1326. {
  1327. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  1328. if (blkcg != &blkio_root_cgroup)
  1329. kfree(blkcg);
  1330. }
  1331. static struct cgroup_subsys_state *
  1332. blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  1333. {
  1334. struct blkio_cgroup *blkcg;
  1335. struct cgroup *parent = cgroup->parent;
  1336. if (!parent) {
  1337. blkcg = &blkio_root_cgroup;
  1338. goto done;
  1339. }
  1340. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  1341. if (!blkcg)
  1342. return ERR_PTR(-ENOMEM);
  1343. blkcg->weight = BLKIO_WEIGHT_DEFAULT;
  1344. done:
  1345. spin_lock_init(&blkcg->lock);
  1346. INIT_HLIST_HEAD(&blkcg->blkg_list);
  1347. return &blkcg->css;
  1348. }
  1349. /**
  1350. * blkcg_init_queue - initialize blkcg part of request queue
  1351. * @q: request_queue to initialize
  1352. *
  1353. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  1354. * part of new request_queue @q.
  1355. *
  1356. * RETURNS:
  1357. * 0 on success, -errno on failure.
  1358. */
  1359. int blkcg_init_queue(struct request_queue *q)
  1360. {
  1361. int ret;
  1362. might_sleep();
  1363. ret = blk_throtl_init(q);
  1364. if (ret)
  1365. return ret;
  1366. mutex_lock(&all_q_mutex);
  1367. INIT_LIST_HEAD(&q->all_q_node);
  1368. list_add_tail(&q->all_q_node, &all_q_list);
  1369. mutex_unlock(&all_q_mutex);
  1370. return 0;
  1371. }
  1372. /**
  1373. * blkcg_drain_queue - drain blkcg part of request_queue
  1374. * @q: request_queue to drain
  1375. *
  1376. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  1377. */
  1378. void blkcg_drain_queue(struct request_queue *q)
  1379. {
  1380. lockdep_assert_held(q->queue_lock);
  1381. blk_throtl_drain(q);
  1382. }
  1383. /**
  1384. * blkcg_exit_queue - exit and release blkcg part of request_queue
  1385. * @q: request_queue being released
  1386. *
  1387. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  1388. */
  1389. void blkcg_exit_queue(struct request_queue *q)
  1390. {
  1391. mutex_lock(&all_q_mutex);
  1392. list_del_init(&q->all_q_node);
  1393. mutex_unlock(&all_q_mutex);
  1394. blk_throtl_exit(q);
  1395. }
  1396. /*
  1397. * We cannot support shared io contexts, as we have no mean to support
  1398. * two tasks with the same ioc in two different groups without major rework
  1399. * of the main cic data structures. For now we allow a task to change
  1400. * its cgroup only if it's the only owner of its ioc.
  1401. */
  1402. static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  1403. struct cgroup_taskset *tset)
  1404. {
  1405. struct task_struct *task;
  1406. struct io_context *ioc;
  1407. int ret = 0;
  1408. /* task_lock() is needed to avoid races with exit_io_context() */
  1409. cgroup_taskset_for_each(task, cgrp, tset) {
  1410. task_lock(task);
  1411. ioc = task->io_context;
  1412. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  1413. ret = -EINVAL;
  1414. task_unlock(task);
  1415. if (ret)
  1416. break;
  1417. }
  1418. return ret;
  1419. }
  1420. static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  1421. struct cgroup_taskset *tset)
  1422. {
  1423. struct task_struct *task;
  1424. struct io_context *ioc;
  1425. cgroup_taskset_for_each(task, cgrp, tset) {
  1426. /* we don't lose anything even if ioc allocation fails */
  1427. ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
  1428. if (ioc) {
  1429. ioc_cgroup_changed(ioc);
  1430. put_io_context(ioc);
  1431. }
  1432. }
  1433. }
  1434. static void blkcg_bypass_start(void)
  1435. __acquires(&all_q_mutex)
  1436. {
  1437. struct request_queue *q;
  1438. mutex_lock(&all_q_mutex);
  1439. list_for_each_entry(q, &all_q_list, all_q_node) {
  1440. blk_queue_bypass_start(q);
  1441. blkg_destroy_all(q);
  1442. }
  1443. }
  1444. static void blkcg_bypass_end(void)
  1445. __releases(&all_q_mutex)
  1446. {
  1447. struct request_queue *q;
  1448. list_for_each_entry(q, &all_q_list, all_q_node)
  1449. blk_queue_bypass_end(q);
  1450. mutex_unlock(&all_q_mutex);
  1451. }
  1452. void blkio_policy_register(struct blkio_policy_type *blkiop)
  1453. {
  1454. blkcg_bypass_start();
  1455. spin_lock(&blkio_list_lock);
  1456. BUG_ON(blkio_policy[blkiop->plid]);
  1457. blkio_policy[blkiop->plid] = blkiop;
  1458. list_add_tail(&blkiop->list, &blkio_list);
  1459. spin_unlock(&blkio_list_lock);
  1460. blkcg_bypass_end();
  1461. }
  1462. EXPORT_SYMBOL_GPL(blkio_policy_register);
  1463. void blkio_policy_unregister(struct blkio_policy_type *blkiop)
  1464. {
  1465. blkcg_bypass_start();
  1466. spin_lock(&blkio_list_lock);
  1467. BUG_ON(blkio_policy[blkiop->plid] != blkiop);
  1468. blkio_policy[blkiop->plid] = NULL;
  1469. list_del_init(&blkiop->list);
  1470. spin_unlock(&blkio_list_lock);
  1471. blkcg_bypass_end();
  1472. }
  1473. EXPORT_SYMBOL_GPL(blkio_policy_unregister);