blk-cgroup.c 26 KB

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