blk-cgroup.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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 "blk-cgroup.h"
  21. #include <linux/genhd.h>
  22. #define MAX_KEY_LEN 100
  23. static DEFINE_SPINLOCK(blkio_list_lock);
  24. static LIST_HEAD(blkio_list);
  25. struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
  26. EXPORT_SYMBOL_GPL(blkio_root_cgroup);
  27. static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
  28. struct cgroup *);
  29. static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
  30. struct task_struct *, bool);
  31. static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
  32. struct cgroup *, struct task_struct *, bool);
  33. static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
  34. static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
  35. struct cgroup_subsys blkio_subsys = {
  36. .name = "blkio",
  37. .create = blkiocg_create,
  38. .can_attach = blkiocg_can_attach,
  39. .attach = blkiocg_attach,
  40. .destroy = blkiocg_destroy,
  41. .populate = blkiocg_populate,
  42. #ifdef CONFIG_BLK_CGROUP
  43. /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
  44. .subsys_id = blkio_subsys_id,
  45. #endif
  46. .use_id = 1,
  47. .module = THIS_MODULE,
  48. };
  49. EXPORT_SYMBOL_GPL(blkio_subsys);
  50. static inline void blkio_policy_insert_node(struct blkio_cgroup *blkcg,
  51. struct blkio_policy_node *pn)
  52. {
  53. list_add(&pn->node, &blkcg->policy_list);
  54. }
  55. /* Must be called with blkcg->lock held */
  56. static inline void blkio_policy_delete_node(struct blkio_policy_node *pn)
  57. {
  58. list_del(&pn->node);
  59. }
  60. /* Must be called with blkcg->lock held */
  61. static struct blkio_policy_node *
  62. blkio_policy_search_node(const struct blkio_cgroup *blkcg, dev_t dev)
  63. {
  64. struct blkio_policy_node *pn;
  65. list_for_each_entry(pn, &blkcg->policy_list, node) {
  66. if (pn->dev == dev)
  67. return pn;
  68. }
  69. return NULL;
  70. }
  71. struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
  72. {
  73. return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
  74. struct blkio_cgroup, css);
  75. }
  76. EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
  77. /*
  78. * Add to the appropriate stat variable depending on the request type.
  79. * This should be called with the blkg->stats_lock held.
  80. */
  81. static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
  82. bool sync)
  83. {
  84. if (direction)
  85. stat[BLKIO_STAT_WRITE] += add;
  86. else
  87. stat[BLKIO_STAT_READ] += add;
  88. if (sync)
  89. stat[BLKIO_STAT_SYNC] += add;
  90. else
  91. stat[BLKIO_STAT_ASYNC] += add;
  92. }
  93. /*
  94. * Decrements the appropriate stat variable if non-zero depending on the
  95. * request type. Panics on value being zero.
  96. * This should be called with the blkg->stats_lock held.
  97. */
  98. static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
  99. {
  100. if (direction) {
  101. BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
  102. stat[BLKIO_STAT_WRITE]--;
  103. } else {
  104. BUG_ON(stat[BLKIO_STAT_READ] == 0);
  105. stat[BLKIO_STAT_READ]--;
  106. }
  107. if (sync) {
  108. BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
  109. stat[BLKIO_STAT_SYNC]--;
  110. } else {
  111. BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
  112. stat[BLKIO_STAT_ASYNC]--;
  113. }
  114. }
  115. #ifdef CONFIG_DEBUG_BLK_CGROUP
  116. /* This should be called with the blkg->stats_lock held. */
  117. static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
  118. struct blkio_group *curr_blkg)
  119. {
  120. if (blkio_blkg_waiting(&blkg->stats))
  121. return;
  122. if (blkg == curr_blkg)
  123. return;
  124. blkg->stats.start_group_wait_time = sched_clock();
  125. blkio_mark_blkg_waiting(&blkg->stats);
  126. }
  127. /* This should be called with the blkg->stats_lock held. */
  128. static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
  129. {
  130. unsigned long long now;
  131. if (!blkio_blkg_waiting(stats))
  132. return;
  133. now = sched_clock();
  134. if (time_after64(now, stats->start_group_wait_time))
  135. stats->group_wait_time += now - stats->start_group_wait_time;
  136. blkio_clear_blkg_waiting(stats);
  137. }
  138. /* This should be called with the blkg->stats_lock held. */
  139. static void blkio_end_empty_time(struct blkio_group_stats *stats)
  140. {
  141. unsigned long long now;
  142. if (!blkio_blkg_empty(stats))
  143. return;
  144. now = sched_clock();
  145. if (time_after64(now, stats->start_empty_time))
  146. stats->empty_time += now - stats->start_empty_time;
  147. blkio_clear_blkg_empty(stats);
  148. }
  149. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
  150. {
  151. unsigned long flags;
  152. spin_lock_irqsave(&blkg->stats_lock, flags);
  153. BUG_ON(blkio_blkg_idling(&blkg->stats));
  154. blkg->stats.start_idle_time = sched_clock();
  155. blkio_mark_blkg_idling(&blkg->stats);
  156. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  157. }
  158. EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
  159. void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
  160. {
  161. unsigned long flags;
  162. unsigned long long now;
  163. struct blkio_group_stats *stats;
  164. spin_lock_irqsave(&blkg->stats_lock, flags);
  165. stats = &blkg->stats;
  166. if (blkio_blkg_idling(stats)) {
  167. now = sched_clock();
  168. if (time_after64(now, stats->start_idle_time))
  169. stats->idle_time += now - stats->start_idle_time;
  170. blkio_clear_blkg_idling(stats);
  171. }
  172. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  173. }
  174. EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
  175. void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
  176. {
  177. unsigned long flags;
  178. struct blkio_group_stats *stats;
  179. spin_lock_irqsave(&blkg->stats_lock, flags);
  180. stats = &blkg->stats;
  181. stats->avg_queue_size_sum +=
  182. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
  183. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
  184. stats->avg_queue_size_samples++;
  185. blkio_update_group_wait_time(stats);
  186. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  187. }
  188. EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
  189. void blkiocg_set_start_empty_time(struct blkio_group *blkg)
  190. {
  191. unsigned long flags;
  192. struct blkio_group_stats *stats;
  193. spin_lock_irqsave(&blkg->stats_lock, flags);
  194. stats = &blkg->stats;
  195. if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
  196. stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
  197. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  198. return;
  199. }
  200. /*
  201. * group is already marked empty. This can happen if cfqq got new
  202. * request in parent group and moved to this group while being added
  203. * to service tree. Just ignore the event and move on.
  204. */
  205. if(blkio_blkg_empty(stats)) {
  206. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  207. return;
  208. }
  209. stats->start_empty_time = sched_clock();
  210. blkio_mark_blkg_empty(stats);
  211. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  212. }
  213. EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
  214. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  215. unsigned long dequeue)
  216. {
  217. blkg->stats.dequeue += dequeue;
  218. }
  219. EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
  220. #else
  221. static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
  222. struct blkio_group *curr_blkg) {}
  223. static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
  224. #endif
  225. void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  226. struct blkio_group *curr_blkg, bool direction,
  227. bool sync)
  228. {
  229. unsigned long flags;
  230. spin_lock_irqsave(&blkg->stats_lock, flags);
  231. blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
  232. sync);
  233. blkio_end_empty_time(&blkg->stats);
  234. blkio_set_start_group_wait_time(blkg, curr_blkg);
  235. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  236. }
  237. EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
  238. void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  239. bool direction, bool sync)
  240. {
  241. unsigned long flags;
  242. spin_lock_irqsave(&blkg->stats_lock, flags);
  243. blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
  244. direction, sync);
  245. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  246. }
  247. EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
  248. void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
  249. {
  250. unsigned long flags;
  251. spin_lock_irqsave(&blkg->stats_lock, flags);
  252. blkg->stats.time += time;
  253. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  254. }
  255. EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
  256. void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  257. uint64_t bytes, bool direction, bool sync)
  258. {
  259. struct blkio_group_stats *stats;
  260. unsigned long flags;
  261. spin_lock_irqsave(&blkg->stats_lock, flags);
  262. stats = &blkg->stats;
  263. stats->sectors += bytes >> 9;
  264. blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction,
  265. sync);
  266. blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes,
  267. direction, sync);
  268. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  269. }
  270. EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
  271. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  272. uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
  273. {
  274. struct blkio_group_stats *stats;
  275. unsigned long flags;
  276. unsigned long long now = sched_clock();
  277. spin_lock_irqsave(&blkg->stats_lock, flags);
  278. stats = &blkg->stats;
  279. if (time_after64(now, io_start_time))
  280. blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
  281. now - io_start_time, direction, sync);
  282. if (time_after64(io_start_time, start_time))
  283. blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
  284. io_start_time - start_time, direction, sync);
  285. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  286. }
  287. EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
  288. void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
  289. bool sync)
  290. {
  291. unsigned long flags;
  292. spin_lock_irqsave(&blkg->stats_lock, flags);
  293. blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
  294. sync);
  295. spin_unlock_irqrestore(&blkg->stats_lock, flags);
  296. }
  297. EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
  298. void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
  299. struct blkio_group *blkg, void *key, dev_t dev)
  300. {
  301. unsigned long flags;
  302. spin_lock_irqsave(&blkcg->lock, flags);
  303. spin_lock_init(&blkg->stats_lock);
  304. rcu_assign_pointer(blkg->key, key);
  305. blkg->blkcg_id = css_id(&blkcg->css);
  306. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  307. spin_unlock_irqrestore(&blkcg->lock, flags);
  308. /* Need to take css reference ? */
  309. cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
  310. blkg->dev = dev;
  311. }
  312. EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
  313. static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
  314. {
  315. hlist_del_init_rcu(&blkg->blkcg_node);
  316. blkg->blkcg_id = 0;
  317. }
  318. /*
  319. * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
  320. * indicating that blk_group was unhashed by the time we got to it.
  321. */
  322. int blkiocg_del_blkio_group(struct blkio_group *blkg)
  323. {
  324. struct blkio_cgroup *blkcg;
  325. unsigned long flags;
  326. struct cgroup_subsys_state *css;
  327. int ret = 1;
  328. rcu_read_lock();
  329. css = css_lookup(&blkio_subsys, blkg->blkcg_id);
  330. if (css) {
  331. blkcg = container_of(css, struct blkio_cgroup, css);
  332. spin_lock_irqsave(&blkcg->lock, flags);
  333. if (!hlist_unhashed(&blkg->blkcg_node)) {
  334. __blkiocg_del_blkio_group(blkg);
  335. ret = 0;
  336. }
  337. spin_unlock_irqrestore(&blkcg->lock, flags);
  338. }
  339. rcu_read_unlock();
  340. return ret;
  341. }
  342. EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
  343. /* called under rcu_read_lock(). */
  344. struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
  345. {
  346. struct blkio_group *blkg;
  347. struct hlist_node *n;
  348. void *__key;
  349. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
  350. __key = blkg->key;
  351. if (__key == key)
  352. return blkg;
  353. }
  354. return NULL;
  355. }
  356. EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
  357. #define SHOW_FUNCTION(__VAR) \
  358. static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
  359. struct cftype *cftype) \
  360. { \
  361. struct blkio_cgroup *blkcg; \
  362. \
  363. blkcg = cgroup_to_blkio_cgroup(cgroup); \
  364. return (u64)blkcg->__VAR; \
  365. }
  366. SHOW_FUNCTION(weight);
  367. #undef SHOW_FUNCTION
  368. static int
  369. blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
  370. {
  371. struct blkio_cgroup *blkcg;
  372. struct blkio_group *blkg;
  373. struct hlist_node *n;
  374. struct blkio_policy_type *blkiop;
  375. struct blkio_policy_node *pn;
  376. if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
  377. return -EINVAL;
  378. blkcg = cgroup_to_blkio_cgroup(cgroup);
  379. spin_lock(&blkio_list_lock);
  380. spin_lock_irq(&blkcg->lock);
  381. blkcg->weight = (unsigned int)val;
  382. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  383. pn = blkio_policy_search_node(blkcg, blkg->dev);
  384. if (pn)
  385. continue;
  386. list_for_each_entry(blkiop, &blkio_list, list)
  387. blkiop->ops.blkio_update_group_weight_fn(blkg,
  388. blkcg->weight);
  389. }
  390. spin_unlock_irq(&blkcg->lock);
  391. spin_unlock(&blkio_list_lock);
  392. return 0;
  393. }
  394. static int
  395. blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
  396. {
  397. struct blkio_cgroup *blkcg;
  398. struct blkio_group *blkg;
  399. struct blkio_group_stats *stats;
  400. struct hlist_node *n;
  401. uint64_t queued[BLKIO_STAT_TOTAL];
  402. int i;
  403. #ifdef CONFIG_DEBUG_BLK_CGROUP
  404. bool idling, waiting, empty;
  405. unsigned long long now = sched_clock();
  406. #endif
  407. blkcg = cgroup_to_blkio_cgroup(cgroup);
  408. spin_lock_irq(&blkcg->lock);
  409. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  410. spin_lock(&blkg->stats_lock);
  411. stats = &blkg->stats;
  412. #ifdef CONFIG_DEBUG_BLK_CGROUP
  413. idling = blkio_blkg_idling(stats);
  414. waiting = blkio_blkg_waiting(stats);
  415. empty = blkio_blkg_empty(stats);
  416. #endif
  417. for (i = 0; i < BLKIO_STAT_TOTAL; i++)
  418. queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
  419. memset(stats, 0, sizeof(struct blkio_group_stats));
  420. for (i = 0; i < BLKIO_STAT_TOTAL; i++)
  421. stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
  422. #ifdef CONFIG_DEBUG_BLK_CGROUP
  423. if (idling) {
  424. blkio_mark_blkg_idling(stats);
  425. stats->start_idle_time = now;
  426. }
  427. if (waiting) {
  428. blkio_mark_blkg_waiting(stats);
  429. stats->start_group_wait_time = now;
  430. }
  431. if (empty) {
  432. blkio_mark_blkg_empty(stats);
  433. stats->start_empty_time = now;
  434. }
  435. #endif
  436. spin_unlock(&blkg->stats_lock);
  437. }
  438. spin_unlock_irq(&blkcg->lock);
  439. return 0;
  440. }
  441. static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
  442. int chars_left, bool diskname_only)
  443. {
  444. snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
  445. chars_left -= strlen(str);
  446. if (chars_left <= 0) {
  447. printk(KERN_WARNING
  448. "Possibly incorrect cgroup stat display format");
  449. return;
  450. }
  451. if (diskname_only)
  452. return;
  453. switch (type) {
  454. case BLKIO_STAT_READ:
  455. strlcat(str, " Read", chars_left);
  456. break;
  457. case BLKIO_STAT_WRITE:
  458. strlcat(str, " Write", chars_left);
  459. break;
  460. case BLKIO_STAT_SYNC:
  461. strlcat(str, " Sync", chars_left);
  462. break;
  463. case BLKIO_STAT_ASYNC:
  464. strlcat(str, " Async", chars_left);
  465. break;
  466. case BLKIO_STAT_TOTAL:
  467. strlcat(str, " Total", chars_left);
  468. break;
  469. default:
  470. strlcat(str, " Invalid", chars_left);
  471. }
  472. }
  473. static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
  474. struct cgroup_map_cb *cb, dev_t dev)
  475. {
  476. blkio_get_key_name(0, dev, str, chars_left, true);
  477. cb->fill(cb, str, val);
  478. return val;
  479. }
  480. /* This should be called with blkg->stats_lock held */
  481. static uint64_t blkio_get_stat(struct blkio_group *blkg,
  482. struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
  483. {
  484. uint64_t disk_total;
  485. char key_str[MAX_KEY_LEN];
  486. enum stat_sub_type sub_type;
  487. if (type == BLKIO_STAT_TIME)
  488. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  489. blkg->stats.time, cb, dev);
  490. if (type == BLKIO_STAT_SECTORS)
  491. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  492. blkg->stats.sectors, cb, dev);
  493. #ifdef CONFIG_DEBUG_BLK_CGROUP
  494. if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
  495. uint64_t sum = blkg->stats.avg_queue_size_sum;
  496. uint64_t samples = blkg->stats.avg_queue_size_samples;
  497. if (samples)
  498. do_div(sum, samples);
  499. else
  500. sum = 0;
  501. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
  502. }
  503. if (type == BLKIO_STAT_GROUP_WAIT_TIME)
  504. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  505. blkg->stats.group_wait_time, cb, dev);
  506. if (type == BLKIO_STAT_IDLE_TIME)
  507. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  508. blkg->stats.idle_time, cb, dev);
  509. if (type == BLKIO_STAT_EMPTY_TIME)
  510. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  511. blkg->stats.empty_time, cb, dev);
  512. if (type == BLKIO_STAT_DEQUEUE)
  513. return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
  514. blkg->stats.dequeue, cb, dev);
  515. #endif
  516. for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
  517. sub_type++) {
  518. blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
  519. cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
  520. }
  521. disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
  522. blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
  523. blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
  524. cb->fill(cb, key_str, disk_total);
  525. return disk_total;
  526. }
  527. #define SHOW_FUNCTION_PER_GROUP(__VAR, type, show_total) \
  528. static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
  529. struct cftype *cftype, struct cgroup_map_cb *cb) \
  530. { \
  531. struct blkio_cgroup *blkcg; \
  532. struct blkio_group *blkg; \
  533. struct hlist_node *n; \
  534. uint64_t cgroup_total = 0; \
  535. \
  536. if (!cgroup_lock_live_group(cgroup)) \
  537. return -ENODEV; \
  538. \
  539. blkcg = cgroup_to_blkio_cgroup(cgroup); \
  540. rcu_read_lock(); \
  541. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
  542. if (blkg->dev) { \
  543. spin_lock_irq(&blkg->stats_lock); \
  544. cgroup_total += blkio_get_stat(blkg, cb, \
  545. blkg->dev, type); \
  546. spin_unlock_irq(&blkg->stats_lock); \
  547. } \
  548. } \
  549. if (show_total) \
  550. cb->fill(cb, "Total", cgroup_total); \
  551. rcu_read_unlock(); \
  552. cgroup_unlock(); \
  553. return 0; \
  554. }
  555. SHOW_FUNCTION_PER_GROUP(time, BLKIO_STAT_TIME, 0);
  556. SHOW_FUNCTION_PER_GROUP(sectors, BLKIO_STAT_SECTORS, 0);
  557. SHOW_FUNCTION_PER_GROUP(io_service_bytes, BLKIO_STAT_SERVICE_BYTES, 1);
  558. SHOW_FUNCTION_PER_GROUP(io_serviced, BLKIO_STAT_SERVICED, 1);
  559. SHOW_FUNCTION_PER_GROUP(io_service_time, BLKIO_STAT_SERVICE_TIME, 1);
  560. SHOW_FUNCTION_PER_GROUP(io_wait_time, BLKIO_STAT_WAIT_TIME, 1);
  561. SHOW_FUNCTION_PER_GROUP(io_merged, BLKIO_STAT_MERGED, 1);
  562. SHOW_FUNCTION_PER_GROUP(io_queued, BLKIO_STAT_QUEUED, 1);
  563. #ifdef CONFIG_DEBUG_BLK_CGROUP
  564. SHOW_FUNCTION_PER_GROUP(dequeue, BLKIO_STAT_DEQUEUE, 0);
  565. SHOW_FUNCTION_PER_GROUP(avg_queue_size, BLKIO_STAT_AVG_QUEUE_SIZE, 0);
  566. SHOW_FUNCTION_PER_GROUP(group_wait_time, BLKIO_STAT_GROUP_WAIT_TIME, 0);
  567. SHOW_FUNCTION_PER_GROUP(idle_time, BLKIO_STAT_IDLE_TIME, 0);
  568. SHOW_FUNCTION_PER_GROUP(empty_time, BLKIO_STAT_EMPTY_TIME, 0);
  569. #endif
  570. #undef SHOW_FUNCTION_PER_GROUP
  571. static int blkio_check_dev_num(dev_t dev)
  572. {
  573. int part = 0;
  574. struct gendisk *disk;
  575. disk = get_gendisk(dev, &part);
  576. if (!disk || part)
  577. return -ENODEV;
  578. return 0;
  579. }
  580. static int blkio_policy_parse_and_set(char *buf,
  581. struct blkio_policy_node *newpn)
  582. {
  583. char *s[4], *p, *major_s = NULL, *minor_s = NULL;
  584. int ret;
  585. unsigned long major, minor, temp;
  586. int i = 0;
  587. dev_t dev;
  588. memset(s, 0, sizeof(s));
  589. while ((p = strsep(&buf, " ")) != NULL) {
  590. if (!*p)
  591. continue;
  592. s[i++] = p;
  593. /* Prevent from inputing too many things */
  594. if (i == 3)
  595. break;
  596. }
  597. if (i != 2)
  598. return -EINVAL;
  599. p = strsep(&s[0], ":");
  600. if (p != NULL)
  601. major_s = p;
  602. else
  603. return -EINVAL;
  604. minor_s = s[0];
  605. if (!minor_s)
  606. return -EINVAL;
  607. ret = strict_strtoul(major_s, 10, &major);
  608. if (ret)
  609. return -EINVAL;
  610. ret = strict_strtoul(minor_s, 10, &minor);
  611. if (ret)
  612. return -EINVAL;
  613. dev = MKDEV(major, minor);
  614. ret = blkio_check_dev_num(dev);
  615. if (ret)
  616. return ret;
  617. newpn->dev = dev;
  618. if (s[1] == NULL)
  619. return -EINVAL;
  620. ret = strict_strtoul(s[1], 10, &temp);
  621. if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
  622. temp > BLKIO_WEIGHT_MAX)
  623. return -EINVAL;
  624. newpn->weight = temp;
  625. return 0;
  626. }
  627. unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
  628. dev_t dev)
  629. {
  630. struct blkio_policy_node *pn;
  631. pn = blkio_policy_search_node(blkcg, dev);
  632. if (pn)
  633. return pn->weight;
  634. else
  635. return blkcg->weight;
  636. }
  637. EXPORT_SYMBOL_GPL(blkcg_get_weight);
  638. static int blkiocg_weight_device_write(struct cgroup *cgrp, struct cftype *cft,
  639. const char *buffer)
  640. {
  641. int ret = 0;
  642. char *buf;
  643. struct blkio_policy_node *newpn, *pn;
  644. struct blkio_cgroup *blkcg;
  645. struct blkio_group *blkg;
  646. int keep_newpn = 0;
  647. struct hlist_node *n;
  648. struct blkio_policy_type *blkiop;
  649. buf = kstrdup(buffer, GFP_KERNEL);
  650. if (!buf)
  651. return -ENOMEM;
  652. newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
  653. if (!newpn) {
  654. ret = -ENOMEM;
  655. goto free_buf;
  656. }
  657. ret = blkio_policy_parse_and_set(buf, newpn);
  658. if (ret)
  659. goto free_newpn;
  660. blkcg = cgroup_to_blkio_cgroup(cgrp);
  661. spin_lock_irq(&blkcg->lock);
  662. pn = blkio_policy_search_node(blkcg, newpn->dev);
  663. if (!pn) {
  664. if (newpn->weight != 0) {
  665. blkio_policy_insert_node(blkcg, newpn);
  666. keep_newpn = 1;
  667. }
  668. spin_unlock_irq(&blkcg->lock);
  669. goto update_io_group;
  670. }
  671. if (newpn->weight == 0) {
  672. /* weight == 0 means deleteing a specific weight */
  673. blkio_policy_delete_node(pn);
  674. spin_unlock_irq(&blkcg->lock);
  675. goto update_io_group;
  676. }
  677. spin_unlock_irq(&blkcg->lock);
  678. pn->weight = newpn->weight;
  679. update_io_group:
  680. /* update weight for each cfqg */
  681. spin_lock(&blkio_list_lock);
  682. spin_lock_irq(&blkcg->lock);
  683. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  684. if (newpn->dev == blkg->dev) {
  685. list_for_each_entry(blkiop, &blkio_list, list)
  686. blkiop->ops.blkio_update_group_weight_fn(blkg,
  687. newpn->weight ?
  688. newpn->weight :
  689. blkcg->weight);
  690. }
  691. }
  692. spin_unlock_irq(&blkcg->lock);
  693. spin_unlock(&blkio_list_lock);
  694. free_newpn:
  695. if (!keep_newpn)
  696. kfree(newpn);
  697. free_buf:
  698. kfree(buf);
  699. return ret;
  700. }
  701. static int blkiocg_weight_device_read(struct cgroup *cgrp, struct cftype *cft,
  702. struct seq_file *m)
  703. {
  704. struct blkio_cgroup *blkcg;
  705. struct blkio_policy_node *pn;
  706. blkcg = cgroup_to_blkio_cgroup(cgrp);
  707. if (!list_empty(&blkcg->policy_list)) {
  708. spin_lock_irq(&blkcg->lock);
  709. list_for_each_entry(pn, &blkcg->policy_list, node) {
  710. seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
  711. MINOR(pn->dev), pn->weight);
  712. }
  713. spin_unlock_irq(&blkcg->lock);
  714. }
  715. return 0;
  716. }
  717. struct cftype blkio_files[] = {
  718. {
  719. .name = "weight_device",
  720. .read_seq_string = blkiocg_weight_device_read,
  721. .write_string = blkiocg_weight_device_write,
  722. .max_write_len = 256,
  723. },
  724. {
  725. .name = "weight",
  726. .read_u64 = blkiocg_weight_read,
  727. .write_u64 = blkiocg_weight_write,
  728. },
  729. {
  730. .name = "time",
  731. .read_map = blkiocg_time_read,
  732. },
  733. {
  734. .name = "sectors",
  735. .read_map = blkiocg_sectors_read,
  736. },
  737. {
  738. .name = "io_service_bytes",
  739. .read_map = blkiocg_io_service_bytes_read,
  740. },
  741. {
  742. .name = "io_serviced",
  743. .read_map = blkiocg_io_serviced_read,
  744. },
  745. {
  746. .name = "io_service_time",
  747. .read_map = blkiocg_io_service_time_read,
  748. },
  749. {
  750. .name = "io_wait_time",
  751. .read_map = blkiocg_io_wait_time_read,
  752. },
  753. {
  754. .name = "io_merged",
  755. .read_map = blkiocg_io_merged_read,
  756. },
  757. {
  758. .name = "io_queued",
  759. .read_map = blkiocg_io_queued_read,
  760. },
  761. {
  762. .name = "reset_stats",
  763. .write_u64 = blkiocg_reset_stats,
  764. },
  765. #ifdef CONFIG_DEBUG_BLK_CGROUP
  766. {
  767. .name = "avg_queue_size",
  768. .read_map = blkiocg_avg_queue_size_read,
  769. },
  770. {
  771. .name = "group_wait_time",
  772. .read_map = blkiocg_group_wait_time_read,
  773. },
  774. {
  775. .name = "idle_time",
  776. .read_map = blkiocg_idle_time_read,
  777. },
  778. {
  779. .name = "empty_time",
  780. .read_map = blkiocg_empty_time_read,
  781. },
  782. {
  783. .name = "dequeue",
  784. .read_map = blkiocg_dequeue_read,
  785. },
  786. #endif
  787. };
  788. static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  789. {
  790. return cgroup_add_files(cgroup, subsys, blkio_files,
  791. ARRAY_SIZE(blkio_files));
  792. }
  793. static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  794. {
  795. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  796. unsigned long flags;
  797. struct blkio_group *blkg;
  798. void *key;
  799. struct blkio_policy_type *blkiop;
  800. struct blkio_policy_node *pn, *pntmp;
  801. rcu_read_lock();
  802. do {
  803. spin_lock_irqsave(&blkcg->lock, flags);
  804. if (hlist_empty(&blkcg->blkg_list)) {
  805. spin_unlock_irqrestore(&blkcg->lock, flags);
  806. break;
  807. }
  808. blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
  809. blkcg_node);
  810. key = rcu_dereference(blkg->key);
  811. __blkiocg_del_blkio_group(blkg);
  812. spin_unlock_irqrestore(&blkcg->lock, flags);
  813. /*
  814. * This blkio_group is being unlinked as associated cgroup is
  815. * going away. Let all the IO controlling policies know about
  816. * this event. Currently this is static call to one io
  817. * controlling policy. Once we have more policies in place, we
  818. * need some dynamic registration of callback function.
  819. */
  820. spin_lock(&blkio_list_lock);
  821. list_for_each_entry(blkiop, &blkio_list, list)
  822. blkiop->ops.blkio_unlink_group_fn(key, blkg);
  823. spin_unlock(&blkio_list_lock);
  824. } while (1);
  825. list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
  826. blkio_policy_delete_node(pn);
  827. kfree(pn);
  828. }
  829. free_css_id(&blkio_subsys, &blkcg->css);
  830. rcu_read_unlock();
  831. if (blkcg != &blkio_root_cgroup)
  832. kfree(blkcg);
  833. }
  834. static struct cgroup_subsys_state *
  835. blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  836. {
  837. struct blkio_cgroup *blkcg;
  838. struct cgroup *parent = cgroup->parent;
  839. if (!parent) {
  840. blkcg = &blkio_root_cgroup;
  841. goto done;
  842. }
  843. /* Currently we do not support hierarchy deeper than two level (0,1) */
  844. if (parent != cgroup->top_cgroup)
  845. return ERR_PTR(-EINVAL);
  846. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  847. if (!blkcg)
  848. return ERR_PTR(-ENOMEM);
  849. blkcg->weight = BLKIO_WEIGHT_DEFAULT;
  850. done:
  851. spin_lock_init(&blkcg->lock);
  852. INIT_HLIST_HEAD(&blkcg->blkg_list);
  853. INIT_LIST_HEAD(&blkcg->policy_list);
  854. return &blkcg->css;
  855. }
  856. /*
  857. * We cannot support shared io contexts, as we have no mean to support
  858. * two tasks with the same ioc in two different groups without major rework
  859. * of the main cic data structures. For now we allow a task to change
  860. * its cgroup only if it's the only owner of its ioc.
  861. */
  862. static int blkiocg_can_attach(struct cgroup_subsys *subsys,
  863. struct cgroup *cgroup, struct task_struct *tsk,
  864. bool threadgroup)
  865. {
  866. struct io_context *ioc;
  867. int ret = 0;
  868. /* task_lock() is needed to avoid races with exit_io_context() */
  869. task_lock(tsk);
  870. ioc = tsk->io_context;
  871. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  872. ret = -EINVAL;
  873. task_unlock(tsk);
  874. return ret;
  875. }
  876. static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
  877. struct cgroup *prev, struct task_struct *tsk,
  878. bool threadgroup)
  879. {
  880. struct io_context *ioc;
  881. task_lock(tsk);
  882. ioc = tsk->io_context;
  883. if (ioc)
  884. ioc->cgroup_changed = 1;
  885. task_unlock(tsk);
  886. }
  887. void blkio_policy_register(struct blkio_policy_type *blkiop)
  888. {
  889. spin_lock(&blkio_list_lock);
  890. list_add_tail(&blkiop->list, &blkio_list);
  891. spin_unlock(&blkio_list_lock);
  892. }
  893. EXPORT_SYMBOL_GPL(blkio_policy_register);
  894. void blkio_policy_unregister(struct blkio_policy_type *blkiop)
  895. {
  896. spin_lock(&blkio_list_lock);
  897. list_del_init(&blkiop->list);
  898. spin_unlock(&blkio_list_lock);
  899. }
  900. EXPORT_SYMBOL_GPL(blkio_policy_unregister);
  901. static int __init init_cgroup_blkio(void)
  902. {
  903. return cgroup_load_subsys(&blkio_subsys);
  904. }
  905. static void __exit exit_cgroup_blkio(void)
  906. {
  907. cgroup_unload_subsys(&blkio_subsys);
  908. }
  909. module_init(init_cgroup_blkio);
  910. module_exit(exit_cgroup_blkio);
  911. MODULE_LICENSE("GPL");