blk-cgroup.c 44 KB

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