blk-cgroup.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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/kdev_t.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/slab.h>
  19. #include <linux/genhd.h>
  20. #include <linux/delay.h>
  21. #include <linux/atomic.h>
  22. #include "blk-cgroup.h"
  23. #include "blk.h"
  24. #define MAX_KEY_LEN 100
  25. static DEFINE_MUTEX(blkcg_pol_mutex);
  26. struct blkcg blkcg_root = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT,
  27. .cfq_leaf_weight = 2 * CFQ_WEIGHT_DEFAULT, };
  28. EXPORT_SYMBOL_GPL(blkcg_root);
  29. static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
  30. static bool blkcg_policy_enabled(struct request_queue *q,
  31. const struct blkcg_policy *pol)
  32. {
  33. return pol && test_bit(pol->plid, q->blkcg_pols);
  34. }
  35. /**
  36. * blkg_free - free a blkg
  37. * @blkg: blkg to free
  38. *
  39. * Free @blkg which may be partially allocated.
  40. */
  41. static void blkg_free(struct blkcg_gq *blkg)
  42. {
  43. int i;
  44. if (!blkg)
  45. return;
  46. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  47. struct blkcg_policy *pol = blkcg_policy[i];
  48. struct blkg_policy_data *pd = blkg->pd[i];
  49. if (!pd)
  50. continue;
  51. if (pol && pol->pd_exit_fn)
  52. pol->pd_exit_fn(blkg);
  53. kfree(pd);
  54. }
  55. blk_exit_rl(&blkg->rl);
  56. kfree(blkg);
  57. }
  58. /**
  59. * blkg_alloc - allocate a blkg
  60. * @blkcg: block cgroup the new blkg is associated with
  61. * @q: request_queue the new blkg is associated with
  62. * @gfp_mask: allocation mask to use
  63. *
  64. * Allocate a new blkg assocating @blkcg and @q.
  65. */
  66. static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
  67. gfp_t gfp_mask)
  68. {
  69. struct blkcg_gq *blkg;
  70. int i;
  71. /* alloc and init base part */
  72. blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node);
  73. if (!blkg)
  74. return NULL;
  75. blkg->q = q;
  76. INIT_LIST_HEAD(&blkg->q_node);
  77. blkg->blkcg = blkcg;
  78. blkg->refcnt = 1;
  79. /* root blkg uses @q->root_rl, init rl only for !root blkgs */
  80. if (blkcg != &blkcg_root) {
  81. if (blk_init_rl(&blkg->rl, q, gfp_mask))
  82. goto err_free;
  83. blkg->rl.blkg = blkg;
  84. }
  85. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  86. struct blkcg_policy *pol = blkcg_policy[i];
  87. struct blkg_policy_data *pd;
  88. if (!blkcg_policy_enabled(q, pol))
  89. continue;
  90. /* alloc per-policy data and attach it to blkg */
  91. pd = kzalloc_node(pol->pd_size, gfp_mask, q->node);
  92. if (!pd)
  93. goto err_free;
  94. blkg->pd[i] = pd;
  95. pd->blkg = blkg;
  96. pd->plid = i;
  97. /* invoke per-policy init */
  98. if (pol->pd_init_fn)
  99. pol->pd_init_fn(blkg);
  100. }
  101. return blkg;
  102. err_free:
  103. blkg_free(blkg);
  104. return NULL;
  105. }
  106. /**
  107. * __blkg_lookup - internal version of blkg_lookup()
  108. * @blkcg: blkcg of interest
  109. * @q: request_queue of interest
  110. * @update_hint: whether to update lookup hint with the result or not
  111. *
  112. * This is internal version and shouldn't be used by policy
  113. * implementations. Looks up blkgs for the @blkcg - @q pair regardless of
  114. * @q's bypass state. If @update_hint is %true, the caller should be
  115. * holding @q->queue_lock and lookup hint is updated on success.
  116. */
  117. struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, struct request_queue *q,
  118. bool update_hint)
  119. {
  120. struct blkcg_gq *blkg;
  121. blkg = rcu_dereference(blkcg->blkg_hint);
  122. if (blkg && blkg->q == q)
  123. return blkg;
  124. /*
  125. * Hint didn't match. Look up from the radix tree. Note that the
  126. * hint can only be updated under queue_lock as otherwise @blkg
  127. * could have already been removed from blkg_tree. The caller is
  128. * responsible for grabbing queue_lock if @update_hint.
  129. */
  130. blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
  131. if (blkg && blkg->q == q) {
  132. if (update_hint) {
  133. lockdep_assert_held(q->queue_lock);
  134. rcu_assign_pointer(blkcg->blkg_hint, blkg);
  135. }
  136. return blkg;
  137. }
  138. return NULL;
  139. }
  140. /**
  141. * blkg_lookup - lookup blkg for the specified blkcg - q pair
  142. * @blkcg: blkcg of interest
  143. * @q: request_queue of interest
  144. *
  145. * Lookup blkg for the @blkcg - @q pair. This function should be called
  146. * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
  147. * - see blk_queue_bypass_start() for details.
  148. */
  149. struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q)
  150. {
  151. WARN_ON_ONCE(!rcu_read_lock_held());
  152. if (unlikely(blk_queue_bypass(q)))
  153. return NULL;
  154. return __blkg_lookup(blkcg, q, false);
  155. }
  156. EXPORT_SYMBOL_GPL(blkg_lookup);
  157. /*
  158. * If @new_blkg is %NULL, this function tries to allocate a new one as
  159. * necessary using %GFP_ATOMIC. @new_blkg is always consumed on return.
  160. */
  161. static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
  162. struct request_queue *q,
  163. struct blkcg_gq *new_blkg)
  164. {
  165. struct blkcg_gq *blkg;
  166. int i, ret;
  167. WARN_ON_ONCE(!rcu_read_lock_held());
  168. lockdep_assert_held(q->queue_lock);
  169. /* blkg holds a reference to blkcg */
  170. if (!css_tryget(&blkcg->css)) {
  171. ret = -EINVAL;
  172. goto err_free_blkg;
  173. }
  174. /* allocate */
  175. if (!new_blkg) {
  176. new_blkg = blkg_alloc(blkcg, q, GFP_ATOMIC);
  177. if (unlikely(!new_blkg)) {
  178. ret = -ENOMEM;
  179. goto err_put_css;
  180. }
  181. }
  182. blkg = new_blkg;
  183. /* link parent and insert */
  184. if (blkcg_parent(blkcg)) {
  185. blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false);
  186. if (WARN_ON_ONCE(!blkg->parent)) {
  187. ret = -EINVAL;
  188. goto err_put_css;
  189. }
  190. blkg_get(blkg->parent);
  191. }
  192. spin_lock(&blkcg->lock);
  193. ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg);
  194. if (likely(!ret)) {
  195. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  196. list_add(&blkg->q_node, &q->blkg_list);
  197. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  198. struct blkcg_policy *pol = blkcg_policy[i];
  199. if (blkg->pd[i] && pol->pd_online_fn)
  200. pol->pd_online_fn(blkg);
  201. }
  202. }
  203. blkg->online = true;
  204. spin_unlock(&blkcg->lock);
  205. if (!ret)
  206. return blkg;
  207. /* @blkg failed fully initialized, use the usual release path */
  208. blkg_put(blkg);
  209. return ERR_PTR(ret);
  210. err_put_css:
  211. css_put(&blkcg->css);
  212. err_free_blkg:
  213. blkg_free(new_blkg);
  214. return ERR_PTR(ret);
  215. }
  216. /**
  217. * blkg_lookup_create - lookup blkg, try to create one if not there
  218. * @blkcg: blkcg of interest
  219. * @q: request_queue of interest
  220. *
  221. * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to
  222. * create one. blkg creation is performed recursively from blkcg_root such
  223. * that all non-root blkg's have access to the parent blkg. This function
  224. * should be called under RCU read lock and @q->queue_lock.
  225. *
  226. * Returns pointer to the looked up or created blkg on success, ERR_PTR()
  227. * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
  228. * dead and bypassing, returns ERR_PTR(-EBUSY).
  229. */
  230. struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
  231. struct request_queue *q)
  232. {
  233. struct blkcg_gq *blkg;
  234. WARN_ON_ONCE(!rcu_read_lock_held());
  235. lockdep_assert_held(q->queue_lock);
  236. /*
  237. * This could be the first entry point of blkcg implementation and
  238. * we shouldn't allow anything to go through for a bypassing queue.
  239. */
  240. if (unlikely(blk_queue_bypass(q)))
  241. return ERR_PTR(blk_queue_dying(q) ? -EINVAL : -EBUSY);
  242. blkg = __blkg_lookup(blkcg, q, true);
  243. if (blkg)
  244. return blkg;
  245. /*
  246. * Create blkgs walking down from blkcg_root to @blkcg, so that all
  247. * non-root blkgs have access to their parents.
  248. */
  249. while (true) {
  250. struct blkcg *pos = blkcg;
  251. struct blkcg *parent = blkcg_parent(blkcg);
  252. while (parent && !__blkg_lookup(parent, q, false)) {
  253. pos = parent;
  254. parent = blkcg_parent(parent);
  255. }
  256. blkg = blkg_create(pos, q, NULL);
  257. if (pos == blkcg || IS_ERR(blkg))
  258. return blkg;
  259. }
  260. }
  261. EXPORT_SYMBOL_GPL(blkg_lookup_create);
  262. static void blkg_destroy(struct blkcg_gq *blkg)
  263. {
  264. struct blkcg *blkcg = blkg->blkcg;
  265. int i;
  266. lockdep_assert_held(blkg->q->queue_lock);
  267. lockdep_assert_held(&blkcg->lock);
  268. /* Something wrong if we are trying to remove same group twice */
  269. WARN_ON_ONCE(list_empty(&blkg->q_node));
  270. WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
  271. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  272. struct blkcg_policy *pol = blkcg_policy[i];
  273. if (blkg->pd[i] && pol->pd_offline_fn)
  274. pol->pd_offline_fn(blkg);
  275. }
  276. blkg->online = false;
  277. radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
  278. list_del_init(&blkg->q_node);
  279. hlist_del_init_rcu(&blkg->blkcg_node);
  280. /*
  281. * Both setting lookup hint to and clearing it from @blkg are done
  282. * under queue_lock. If it's not pointing to @blkg now, it never
  283. * will. Hint assignment itself can race safely.
  284. */
  285. if (rcu_dereference_raw(blkcg->blkg_hint) == blkg)
  286. rcu_assign_pointer(blkcg->blkg_hint, NULL);
  287. /*
  288. * Put the reference taken at the time of creation so that when all
  289. * queues are gone, group can be destroyed.
  290. */
  291. blkg_put(blkg);
  292. }
  293. /**
  294. * blkg_destroy_all - destroy all blkgs associated with a request_queue
  295. * @q: request_queue of interest
  296. *
  297. * Destroy all blkgs associated with @q.
  298. */
  299. static void blkg_destroy_all(struct request_queue *q)
  300. {
  301. struct blkcg_gq *blkg, *n;
  302. lockdep_assert_held(q->queue_lock);
  303. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  304. struct blkcg *blkcg = blkg->blkcg;
  305. spin_lock(&blkcg->lock);
  306. blkg_destroy(blkg);
  307. spin_unlock(&blkcg->lock);
  308. }
  309. /*
  310. * root blkg is destroyed. Just clear the pointer since
  311. * root_rl does not take reference on root blkg.
  312. */
  313. q->root_blkg = NULL;
  314. q->root_rl.blkg = NULL;
  315. }
  316. static void blkg_rcu_free(struct rcu_head *rcu_head)
  317. {
  318. blkg_free(container_of(rcu_head, struct blkcg_gq, rcu_head));
  319. }
  320. void __blkg_release(struct blkcg_gq *blkg)
  321. {
  322. /* release the blkcg and parent blkg refs this blkg has been holding */
  323. css_put(&blkg->blkcg->css);
  324. if (blkg->parent)
  325. blkg_put(blkg->parent);
  326. /*
  327. * A group is freed in rcu manner. But having an rcu lock does not
  328. * mean that one can access all the fields of blkg and assume these
  329. * are valid. For example, don't try to follow throtl_data and
  330. * request queue links.
  331. *
  332. * Having a reference to blkg under an rcu allows acess to only
  333. * values local to groups like group stats and group rate limits
  334. */
  335. call_rcu(&blkg->rcu_head, blkg_rcu_free);
  336. }
  337. EXPORT_SYMBOL_GPL(__blkg_release);
  338. /*
  339. * The next function used by blk_queue_for_each_rl(). It's a bit tricky
  340. * because the root blkg uses @q->root_rl instead of its own rl.
  341. */
  342. struct request_list *__blk_queue_next_rl(struct request_list *rl,
  343. struct request_queue *q)
  344. {
  345. struct list_head *ent;
  346. struct blkcg_gq *blkg;
  347. /*
  348. * Determine the current blkg list_head. The first entry is
  349. * root_rl which is off @q->blkg_list and mapped to the head.
  350. */
  351. if (rl == &q->root_rl) {
  352. ent = &q->blkg_list;
  353. /* There are no more block groups, hence no request lists */
  354. if (list_empty(ent))
  355. return NULL;
  356. } else {
  357. blkg = container_of(rl, struct blkcg_gq, rl);
  358. ent = &blkg->q_node;
  359. }
  360. /* walk to the next list_head, skip root blkcg */
  361. ent = ent->next;
  362. if (ent == &q->root_blkg->q_node)
  363. ent = ent->next;
  364. if (ent == &q->blkg_list)
  365. return NULL;
  366. blkg = container_of(ent, struct blkcg_gq, q_node);
  367. return &blkg->rl;
  368. }
  369. static int blkcg_reset_stats(struct cgroup *cgroup, struct cftype *cftype,
  370. u64 val)
  371. {
  372. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  373. struct blkcg_gq *blkg;
  374. int i;
  375. mutex_lock(&blkcg_pol_mutex);
  376. spin_lock_irq(&blkcg->lock);
  377. /*
  378. * Note that stat reset is racy - it doesn't synchronize against
  379. * stat updates. This is a debug feature which shouldn't exist
  380. * anyway. If you get hit by a race, retry.
  381. */
  382. hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
  383. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  384. struct blkcg_policy *pol = blkcg_policy[i];
  385. if (blkcg_policy_enabled(blkg->q, pol) &&
  386. pol->pd_reset_stats_fn)
  387. pol->pd_reset_stats_fn(blkg);
  388. }
  389. }
  390. spin_unlock_irq(&blkcg->lock);
  391. mutex_unlock(&blkcg_pol_mutex);
  392. return 0;
  393. }
  394. static const char *blkg_dev_name(struct blkcg_gq *blkg)
  395. {
  396. /* some drivers (floppy) instantiate a queue w/o disk registered */
  397. if (blkg->q->backing_dev_info.dev)
  398. return dev_name(blkg->q->backing_dev_info.dev);
  399. return NULL;
  400. }
  401. /**
  402. * blkcg_print_blkgs - helper for printing per-blkg data
  403. * @sf: seq_file to print to
  404. * @blkcg: blkcg of interest
  405. * @prfill: fill function to print out a blkg
  406. * @pol: policy in question
  407. * @data: data to be passed to @prfill
  408. * @show_total: to print out sum of prfill return values or not
  409. *
  410. * This function invokes @prfill on each blkg of @blkcg if pd for the
  411. * policy specified by @pol exists. @prfill is invoked with @sf, the
  412. * policy data and @data and the matching queue lock held. If @show_total
  413. * is %true, the sum of the return values from @prfill is printed with
  414. * "Total" label at the end.
  415. *
  416. * This is to be used to construct print functions for
  417. * cftype->read_seq_string method.
  418. */
  419. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  420. u64 (*prfill)(struct seq_file *,
  421. struct blkg_policy_data *, int),
  422. const struct blkcg_policy *pol, int data,
  423. bool show_total)
  424. {
  425. struct blkcg_gq *blkg;
  426. u64 total = 0;
  427. rcu_read_lock();
  428. hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
  429. spin_lock_irq(blkg->q->queue_lock);
  430. if (blkcg_policy_enabled(blkg->q, pol))
  431. total += prfill(sf, blkg->pd[pol->plid], data);
  432. spin_unlock_irq(blkg->q->queue_lock);
  433. }
  434. rcu_read_unlock();
  435. if (show_total)
  436. seq_printf(sf, "Total %llu\n", (unsigned long long)total);
  437. }
  438. EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
  439. /**
  440. * __blkg_prfill_u64 - prfill helper for a single u64 value
  441. * @sf: seq_file to print to
  442. * @pd: policy private data of interest
  443. * @v: value to print
  444. *
  445. * Print @v to @sf for the device assocaited with @pd.
  446. */
  447. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
  448. {
  449. const char *dname = blkg_dev_name(pd->blkg);
  450. if (!dname)
  451. return 0;
  452. seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
  453. return v;
  454. }
  455. EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
  456. /**
  457. * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
  458. * @sf: seq_file to print to
  459. * @pd: policy private data of interest
  460. * @rwstat: rwstat to print
  461. *
  462. * Print @rwstat to @sf for the device assocaited with @pd.
  463. */
  464. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  465. const struct blkg_rwstat *rwstat)
  466. {
  467. static const char *rwstr[] = {
  468. [BLKG_RWSTAT_READ] = "Read",
  469. [BLKG_RWSTAT_WRITE] = "Write",
  470. [BLKG_RWSTAT_SYNC] = "Sync",
  471. [BLKG_RWSTAT_ASYNC] = "Async",
  472. };
  473. const char *dname = blkg_dev_name(pd->blkg);
  474. u64 v;
  475. int i;
  476. if (!dname)
  477. return 0;
  478. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  479. seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
  480. (unsigned long long)rwstat->cnt[i]);
  481. v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
  482. seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
  483. return v;
  484. }
  485. EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);
  486. /**
  487. * blkg_prfill_stat - prfill callback for blkg_stat
  488. * @sf: seq_file to print to
  489. * @pd: policy private data of interest
  490. * @off: offset to the blkg_stat in @pd
  491. *
  492. * prfill callback for printing a blkg_stat.
  493. */
  494. u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
  495. {
  496. return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
  497. }
  498. EXPORT_SYMBOL_GPL(blkg_prfill_stat);
  499. /**
  500. * blkg_prfill_rwstat - prfill callback for blkg_rwstat
  501. * @sf: seq_file to print to
  502. * @pd: policy private data of interest
  503. * @off: offset to the blkg_rwstat in @pd
  504. *
  505. * prfill callback for printing a blkg_rwstat.
  506. */
  507. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  508. int off)
  509. {
  510. struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
  511. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  512. }
  513. EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
  514. /**
  515. * blkg_stat_recursive_sum - collect hierarchical blkg_stat
  516. * @pd: policy private data of interest
  517. * @off: offset to the blkg_stat in @pd
  518. *
  519. * Collect the blkg_stat specified by @off from @pd and all its online
  520. * descendants and return the sum. The caller must be holding the queue
  521. * lock for online tests.
  522. */
  523. u64 blkg_stat_recursive_sum(struct blkg_policy_data *pd, int off)
  524. {
  525. struct blkcg_policy *pol = blkcg_policy[pd->plid];
  526. struct blkcg_gq *pos_blkg;
  527. struct cgroup *pos_cgrp;
  528. u64 sum;
  529. lockdep_assert_held(pd->blkg->q->queue_lock);
  530. sum = blkg_stat_read((void *)pd + off);
  531. rcu_read_lock();
  532. blkg_for_each_descendant_pre(pos_blkg, pos_cgrp, pd_to_blkg(pd)) {
  533. struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol);
  534. struct blkg_stat *stat = (void *)pos_pd + off;
  535. if (pos_blkg->online)
  536. sum += blkg_stat_read(stat);
  537. }
  538. rcu_read_unlock();
  539. return sum;
  540. }
  541. EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum);
  542. /**
  543. * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
  544. * @pd: policy private data of interest
  545. * @off: offset to the blkg_stat in @pd
  546. *
  547. * Collect the blkg_rwstat specified by @off from @pd and all its online
  548. * descendants and return the sum. The caller must be holding the queue
  549. * lock for online tests.
  550. */
  551. struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkg_policy_data *pd,
  552. int off)
  553. {
  554. struct blkcg_policy *pol = blkcg_policy[pd->plid];
  555. struct blkcg_gq *pos_blkg;
  556. struct cgroup *pos_cgrp;
  557. struct blkg_rwstat sum;
  558. int i;
  559. lockdep_assert_held(pd->blkg->q->queue_lock);
  560. sum = blkg_rwstat_read((void *)pd + off);
  561. rcu_read_lock();
  562. blkg_for_each_descendant_pre(pos_blkg, pos_cgrp, pd_to_blkg(pd)) {
  563. struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol);
  564. struct blkg_rwstat *rwstat = (void *)pos_pd + off;
  565. struct blkg_rwstat tmp;
  566. if (!pos_blkg->online)
  567. continue;
  568. tmp = blkg_rwstat_read(rwstat);
  569. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  570. sum.cnt[i] += tmp.cnt[i];
  571. }
  572. rcu_read_unlock();
  573. return sum;
  574. }
  575. EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);
  576. /**
  577. * blkg_conf_prep - parse and prepare for per-blkg config update
  578. * @blkcg: target block cgroup
  579. * @pol: target policy
  580. * @input: input string
  581. * @ctx: blkg_conf_ctx to be filled
  582. *
  583. * Parse per-blkg config update from @input and initialize @ctx with the
  584. * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
  585. * value. This function returns with RCU read lock and queue lock held and
  586. * must be paired with blkg_conf_finish().
  587. */
  588. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  589. const char *input, struct blkg_conf_ctx *ctx)
  590. __acquires(rcu) __acquires(disk->queue->queue_lock)
  591. {
  592. struct gendisk *disk;
  593. struct blkcg_gq *blkg;
  594. unsigned int major, minor;
  595. unsigned long long v;
  596. int part, ret;
  597. if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
  598. return -EINVAL;
  599. disk = get_gendisk(MKDEV(major, minor), &part);
  600. if (!disk || part)
  601. return -EINVAL;
  602. rcu_read_lock();
  603. spin_lock_irq(disk->queue->queue_lock);
  604. if (blkcg_policy_enabled(disk->queue, pol))
  605. blkg = blkg_lookup_create(blkcg, disk->queue);
  606. else
  607. blkg = ERR_PTR(-EINVAL);
  608. if (IS_ERR(blkg)) {
  609. ret = PTR_ERR(blkg);
  610. rcu_read_unlock();
  611. spin_unlock_irq(disk->queue->queue_lock);
  612. put_disk(disk);
  613. /*
  614. * If queue was bypassing, we should retry. Do so after a
  615. * short msleep(). It isn't strictly necessary but queue
  616. * can be bypassing for some time and it's always nice to
  617. * avoid busy looping.
  618. */
  619. if (ret == -EBUSY) {
  620. msleep(10);
  621. ret = restart_syscall();
  622. }
  623. return ret;
  624. }
  625. ctx->disk = disk;
  626. ctx->blkg = blkg;
  627. ctx->v = v;
  628. return 0;
  629. }
  630. EXPORT_SYMBOL_GPL(blkg_conf_prep);
  631. /**
  632. * blkg_conf_finish - finish up per-blkg config update
  633. * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
  634. *
  635. * Finish up after per-blkg config update. This function must be paired
  636. * with blkg_conf_prep().
  637. */
  638. void blkg_conf_finish(struct blkg_conf_ctx *ctx)
  639. __releases(ctx->disk->queue->queue_lock) __releases(rcu)
  640. {
  641. spin_unlock_irq(ctx->disk->queue->queue_lock);
  642. rcu_read_unlock();
  643. put_disk(ctx->disk);
  644. }
  645. EXPORT_SYMBOL_GPL(blkg_conf_finish);
  646. struct cftype blkcg_files[] = {
  647. {
  648. .name = "reset_stats",
  649. .write_u64 = blkcg_reset_stats,
  650. },
  651. { } /* terminate */
  652. };
  653. /**
  654. * blkcg_css_offline - cgroup css_offline callback
  655. * @cgroup: cgroup of interest
  656. *
  657. * This function is called when @cgroup is about to go away and responsible
  658. * for shooting down all blkgs associated with @cgroup. blkgs should be
  659. * removed while holding both q and blkcg locks. As blkcg lock is nested
  660. * inside q lock, this function performs reverse double lock dancing.
  661. *
  662. * This is the blkcg counterpart of ioc_release_fn().
  663. */
  664. static void blkcg_css_offline(struct cgroup *cgroup)
  665. {
  666. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  667. spin_lock_irq(&blkcg->lock);
  668. while (!hlist_empty(&blkcg->blkg_list)) {
  669. struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
  670. struct blkcg_gq, blkcg_node);
  671. struct request_queue *q = blkg->q;
  672. if (spin_trylock(q->queue_lock)) {
  673. blkg_destroy(blkg);
  674. spin_unlock(q->queue_lock);
  675. } else {
  676. spin_unlock_irq(&blkcg->lock);
  677. cpu_relax();
  678. spin_lock_irq(&blkcg->lock);
  679. }
  680. }
  681. spin_unlock_irq(&blkcg->lock);
  682. }
  683. static void blkcg_css_free(struct cgroup *cgroup)
  684. {
  685. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  686. if (blkcg != &blkcg_root)
  687. kfree(blkcg);
  688. }
  689. static struct cgroup_subsys_state *blkcg_css_alloc(struct cgroup *cgroup)
  690. {
  691. static atomic64_t id_seq = ATOMIC64_INIT(0);
  692. struct blkcg *blkcg;
  693. struct cgroup *parent = cgroup->parent;
  694. if (!parent) {
  695. blkcg = &blkcg_root;
  696. goto done;
  697. }
  698. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  699. if (!blkcg)
  700. return ERR_PTR(-ENOMEM);
  701. blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
  702. blkcg->cfq_leaf_weight = CFQ_WEIGHT_DEFAULT;
  703. blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
  704. done:
  705. spin_lock_init(&blkcg->lock);
  706. INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_ATOMIC);
  707. INIT_HLIST_HEAD(&blkcg->blkg_list);
  708. return &blkcg->css;
  709. }
  710. /**
  711. * blkcg_init_queue - initialize blkcg part of request queue
  712. * @q: request_queue to initialize
  713. *
  714. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  715. * part of new request_queue @q.
  716. *
  717. * RETURNS:
  718. * 0 on success, -errno on failure.
  719. */
  720. int blkcg_init_queue(struct request_queue *q)
  721. {
  722. might_sleep();
  723. return blk_throtl_init(q);
  724. }
  725. /**
  726. * blkcg_drain_queue - drain blkcg part of request_queue
  727. * @q: request_queue to drain
  728. *
  729. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  730. */
  731. void blkcg_drain_queue(struct request_queue *q)
  732. {
  733. lockdep_assert_held(q->queue_lock);
  734. blk_throtl_drain(q);
  735. }
  736. /**
  737. * blkcg_exit_queue - exit and release blkcg part of request_queue
  738. * @q: request_queue being released
  739. *
  740. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  741. */
  742. void blkcg_exit_queue(struct request_queue *q)
  743. {
  744. spin_lock_irq(q->queue_lock);
  745. blkg_destroy_all(q);
  746. spin_unlock_irq(q->queue_lock);
  747. blk_throtl_exit(q);
  748. }
  749. /*
  750. * We cannot support shared io contexts, as we have no mean to support
  751. * two tasks with the same ioc in two different groups without major rework
  752. * of the main cic data structures. For now we allow a task to change
  753. * its cgroup only if it's the only owner of its ioc.
  754. */
  755. static int blkcg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
  756. {
  757. struct task_struct *task;
  758. struct io_context *ioc;
  759. int ret = 0;
  760. /* task_lock() is needed to avoid races with exit_io_context() */
  761. cgroup_taskset_for_each(task, cgrp, tset) {
  762. task_lock(task);
  763. ioc = task->io_context;
  764. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  765. ret = -EINVAL;
  766. task_unlock(task);
  767. if (ret)
  768. break;
  769. }
  770. return ret;
  771. }
  772. struct cgroup_subsys blkio_subsys = {
  773. .name = "blkio",
  774. .css_alloc = blkcg_css_alloc,
  775. .css_offline = blkcg_css_offline,
  776. .css_free = blkcg_css_free,
  777. .can_attach = blkcg_can_attach,
  778. .subsys_id = blkio_subsys_id,
  779. .base_cftypes = blkcg_files,
  780. .module = THIS_MODULE,
  781. /*
  782. * blkio subsystem is utterly broken in terms of hierarchy support.
  783. * It treats all cgroups equally regardless of where they're
  784. * located in the hierarchy - all cgroups are treated as if they're
  785. * right below the root. Fix it and remove the following.
  786. */
  787. .broken_hierarchy = true,
  788. };
  789. EXPORT_SYMBOL_GPL(blkio_subsys);
  790. /**
  791. * blkcg_activate_policy - activate a blkcg policy on a request_queue
  792. * @q: request_queue of interest
  793. * @pol: blkcg policy to activate
  794. *
  795. * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
  796. * bypass mode to populate its blkgs with policy_data for @pol.
  797. *
  798. * Activation happens with @q bypassed, so nobody would be accessing blkgs
  799. * from IO path. Update of each blkg is protected by both queue and blkcg
  800. * locks so that holding either lock and testing blkcg_policy_enabled() is
  801. * always enough for dereferencing policy data.
  802. *
  803. * The caller is responsible for synchronizing [de]activations and policy
  804. * [un]registerations. Returns 0 on success, -errno on failure.
  805. */
  806. int blkcg_activate_policy(struct request_queue *q,
  807. const struct blkcg_policy *pol)
  808. {
  809. LIST_HEAD(pds);
  810. struct blkcg_gq *blkg, *new_blkg;
  811. struct blkg_policy_data *pd, *n;
  812. int cnt = 0, ret;
  813. bool preloaded;
  814. if (blkcg_policy_enabled(q, pol))
  815. return 0;
  816. /* preallocations for root blkg */
  817. new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
  818. if (!new_blkg)
  819. return -ENOMEM;
  820. blk_queue_bypass_start(q);
  821. preloaded = !radix_tree_preload(GFP_KERNEL);
  822. /*
  823. * Make sure the root blkg exists and count the existing blkgs. As
  824. * @q is bypassing at this point, blkg_lookup_create() can't be
  825. * used. Open code it.
  826. */
  827. spin_lock_irq(q->queue_lock);
  828. rcu_read_lock();
  829. blkg = __blkg_lookup(&blkcg_root, q, false);
  830. if (blkg)
  831. blkg_free(new_blkg);
  832. else
  833. blkg = blkg_create(&blkcg_root, q, new_blkg);
  834. rcu_read_unlock();
  835. if (preloaded)
  836. radix_tree_preload_end();
  837. if (IS_ERR(blkg)) {
  838. ret = PTR_ERR(blkg);
  839. goto out_unlock;
  840. }
  841. q->root_blkg = blkg;
  842. q->root_rl.blkg = blkg;
  843. list_for_each_entry(blkg, &q->blkg_list, q_node)
  844. cnt++;
  845. spin_unlock_irq(q->queue_lock);
  846. /* allocate policy_data for all existing blkgs */
  847. while (cnt--) {
  848. pd = kzalloc_node(pol->pd_size, GFP_KERNEL, q->node);
  849. if (!pd) {
  850. ret = -ENOMEM;
  851. goto out_free;
  852. }
  853. list_add_tail(&pd->alloc_node, &pds);
  854. }
  855. /*
  856. * Install the allocated pds. With @q bypassing, no new blkg
  857. * should have been created while the queue lock was dropped.
  858. */
  859. spin_lock_irq(q->queue_lock);
  860. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  861. if (WARN_ON(list_empty(&pds))) {
  862. /* umm... this shouldn't happen, just abort */
  863. ret = -ENOMEM;
  864. goto out_unlock;
  865. }
  866. pd = list_first_entry(&pds, struct blkg_policy_data, alloc_node);
  867. list_del_init(&pd->alloc_node);
  868. /* grab blkcg lock too while installing @pd on @blkg */
  869. spin_lock(&blkg->blkcg->lock);
  870. blkg->pd[pol->plid] = pd;
  871. pd->blkg = blkg;
  872. pd->plid = pol->plid;
  873. pol->pd_init_fn(blkg);
  874. spin_unlock(&blkg->blkcg->lock);
  875. }
  876. __set_bit(pol->plid, q->blkcg_pols);
  877. ret = 0;
  878. out_unlock:
  879. spin_unlock_irq(q->queue_lock);
  880. out_free:
  881. blk_queue_bypass_end(q);
  882. list_for_each_entry_safe(pd, n, &pds, alloc_node)
  883. kfree(pd);
  884. return ret;
  885. }
  886. EXPORT_SYMBOL_GPL(blkcg_activate_policy);
  887. /**
  888. * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
  889. * @q: request_queue of interest
  890. * @pol: blkcg policy to deactivate
  891. *
  892. * Deactivate @pol on @q. Follows the same synchronization rules as
  893. * blkcg_activate_policy().
  894. */
  895. void blkcg_deactivate_policy(struct request_queue *q,
  896. const struct blkcg_policy *pol)
  897. {
  898. struct blkcg_gq *blkg;
  899. if (!blkcg_policy_enabled(q, pol))
  900. return;
  901. blk_queue_bypass_start(q);
  902. spin_lock_irq(q->queue_lock);
  903. __clear_bit(pol->plid, q->blkcg_pols);
  904. /* if no policy is left, no need for blkgs - shoot them down */
  905. if (bitmap_empty(q->blkcg_pols, BLKCG_MAX_POLS))
  906. blkg_destroy_all(q);
  907. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  908. /* grab blkcg lock too while removing @pd from @blkg */
  909. spin_lock(&blkg->blkcg->lock);
  910. if (pol->pd_offline_fn)
  911. pol->pd_offline_fn(blkg);
  912. if (pol->pd_exit_fn)
  913. pol->pd_exit_fn(blkg);
  914. kfree(blkg->pd[pol->plid]);
  915. blkg->pd[pol->plid] = NULL;
  916. spin_unlock(&blkg->blkcg->lock);
  917. }
  918. spin_unlock_irq(q->queue_lock);
  919. blk_queue_bypass_end(q);
  920. }
  921. EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
  922. /**
  923. * blkcg_policy_register - register a blkcg policy
  924. * @pol: blkcg policy to register
  925. *
  926. * Register @pol with blkcg core. Might sleep and @pol may be modified on
  927. * successful registration. Returns 0 on success and -errno on failure.
  928. */
  929. int blkcg_policy_register(struct blkcg_policy *pol)
  930. {
  931. int i, ret;
  932. if (WARN_ON(pol->pd_size < sizeof(struct blkg_policy_data)))
  933. return -EINVAL;
  934. mutex_lock(&blkcg_pol_mutex);
  935. /* find an empty slot */
  936. ret = -ENOSPC;
  937. for (i = 0; i < BLKCG_MAX_POLS; i++)
  938. if (!blkcg_policy[i])
  939. break;
  940. if (i >= BLKCG_MAX_POLS)
  941. goto out_unlock;
  942. /* register and update blkgs */
  943. pol->plid = i;
  944. blkcg_policy[i] = pol;
  945. /* everything is in place, add intf files for the new policy */
  946. if (pol->cftypes)
  947. WARN_ON(cgroup_add_cftypes(&blkio_subsys, pol->cftypes));
  948. ret = 0;
  949. out_unlock:
  950. mutex_unlock(&blkcg_pol_mutex);
  951. return ret;
  952. }
  953. EXPORT_SYMBOL_GPL(blkcg_policy_register);
  954. /**
  955. * blkcg_policy_unregister - unregister a blkcg policy
  956. * @pol: blkcg policy to unregister
  957. *
  958. * Undo blkcg_policy_register(@pol). Might sleep.
  959. */
  960. void blkcg_policy_unregister(struct blkcg_policy *pol)
  961. {
  962. mutex_lock(&blkcg_pol_mutex);
  963. if (WARN_ON(blkcg_policy[pol->plid] != pol))
  964. goto out_unlock;
  965. /* kill the intf files first */
  966. if (pol->cftypes)
  967. cgroup_rm_cftypes(&blkio_subsys, pol->cftypes);
  968. /* unregister and update blkgs */
  969. blkcg_policy[pol->plid] = NULL;
  970. out_unlock:
  971. mutex_unlock(&blkcg_pol_mutex);
  972. }
  973. EXPORT_SYMBOL_GPL(blkcg_policy_unregister);