blk-sysfs.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Functions related to sysfs handling
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/bio.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/backing-dev.h>
  11. #include <linux/blktrace_api.h>
  12. #include <linux/blk-mq.h>
  13. #include <linux/blk-cgroup.h>
  14. #include "blk.h"
  15. #include "blk-mq.h"
  16. #include "blk-mq-debugfs.h"
  17. #include "blk-wbt.h"
  18. struct queue_sysfs_entry {
  19. struct attribute attr;
  20. ssize_t (*show)(struct request_queue *, char *);
  21. ssize_t (*store)(struct request_queue *, const char *, size_t);
  22. };
  23. static ssize_t
  24. queue_var_show(unsigned long var, char *page)
  25. {
  26. return sprintf(page, "%lu\n", var);
  27. }
  28. static ssize_t
  29. queue_var_store(unsigned long *var, const char *page, size_t count)
  30. {
  31. int err;
  32. unsigned long v;
  33. err = kstrtoul(page, 10, &v);
  34. if (err || v > UINT_MAX)
  35. return -EINVAL;
  36. *var = v;
  37. return count;
  38. }
  39. static ssize_t queue_var_store64(s64 *var, const char *page)
  40. {
  41. int err;
  42. s64 v;
  43. err = kstrtos64(page, 10, &v);
  44. if (err < 0)
  45. return err;
  46. *var = v;
  47. return 0;
  48. }
  49. static ssize_t queue_requests_show(struct request_queue *q, char *page)
  50. {
  51. return queue_var_show(q->nr_requests, (page));
  52. }
  53. static ssize_t
  54. queue_requests_store(struct request_queue *q, const char *page, size_t count)
  55. {
  56. unsigned long nr;
  57. int ret, err;
  58. if (!q->request_fn && !q->mq_ops)
  59. return -EINVAL;
  60. ret = queue_var_store(&nr, page, count);
  61. if (ret < 0)
  62. return ret;
  63. if (nr < BLKDEV_MIN_RQ)
  64. nr = BLKDEV_MIN_RQ;
  65. if (q->request_fn)
  66. err = blk_update_nr_requests(q, nr);
  67. else
  68. err = blk_mq_update_nr_requests(q, nr);
  69. if (err)
  70. return err;
  71. return ret;
  72. }
  73. static ssize_t queue_ra_show(struct request_queue *q, char *page)
  74. {
  75. unsigned long ra_kb = q->backing_dev_info->ra_pages <<
  76. (PAGE_SHIFT - 10);
  77. return queue_var_show(ra_kb, (page));
  78. }
  79. static ssize_t
  80. queue_ra_store(struct request_queue *q, const char *page, size_t count)
  81. {
  82. unsigned long ra_kb;
  83. ssize_t ret = queue_var_store(&ra_kb, page, count);
  84. if (ret < 0)
  85. return ret;
  86. q->backing_dev_info->ra_pages = ra_kb >> (PAGE_SHIFT - 10);
  87. return ret;
  88. }
  89. static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
  90. {
  91. int max_sectors_kb = queue_max_sectors(q) >> 1;
  92. return queue_var_show(max_sectors_kb, (page));
  93. }
  94. static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
  95. {
  96. return queue_var_show(queue_max_segments(q), (page));
  97. }
  98. static ssize_t queue_max_discard_segments_show(struct request_queue *q,
  99. char *page)
  100. {
  101. return queue_var_show(queue_max_discard_segments(q), (page));
  102. }
  103. static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
  104. {
  105. return queue_var_show(q->limits.max_integrity_segments, (page));
  106. }
  107. static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
  108. {
  109. if (blk_queue_cluster(q))
  110. return queue_var_show(queue_max_segment_size(q), (page));
  111. return queue_var_show(PAGE_SIZE, (page));
  112. }
  113. static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
  114. {
  115. return queue_var_show(queue_logical_block_size(q), page);
  116. }
  117. static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
  118. {
  119. return queue_var_show(queue_physical_block_size(q), page);
  120. }
  121. static ssize_t queue_chunk_sectors_show(struct request_queue *q, char *page)
  122. {
  123. return queue_var_show(q->limits.chunk_sectors, page);
  124. }
  125. static ssize_t queue_io_min_show(struct request_queue *q, char *page)
  126. {
  127. return queue_var_show(queue_io_min(q), page);
  128. }
  129. static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
  130. {
  131. return queue_var_show(queue_io_opt(q), page);
  132. }
  133. static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
  134. {
  135. return queue_var_show(q->limits.discard_granularity, page);
  136. }
  137. static ssize_t queue_discard_max_hw_show(struct request_queue *q, char *page)
  138. {
  139. return sprintf(page, "%llu\n",
  140. (unsigned long long)q->limits.max_hw_discard_sectors << 9);
  141. }
  142. static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
  143. {
  144. return sprintf(page, "%llu\n",
  145. (unsigned long long)q->limits.max_discard_sectors << 9);
  146. }
  147. static ssize_t queue_discard_max_store(struct request_queue *q,
  148. const char *page, size_t count)
  149. {
  150. unsigned long max_discard;
  151. ssize_t ret = queue_var_store(&max_discard, page, count);
  152. if (ret < 0)
  153. return ret;
  154. if (max_discard & (q->limits.discard_granularity - 1))
  155. return -EINVAL;
  156. max_discard >>= 9;
  157. if (max_discard > UINT_MAX)
  158. return -EINVAL;
  159. if (max_discard > q->limits.max_hw_discard_sectors)
  160. max_discard = q->limits.max_hw_discard_sectors;
  161. q->limits.max_discard_sectors = max_discard;
  162. return ret;
  163. }
  164. static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
  165. {
  166. return queue_var_show(0, page);
  167. }
  168. static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
  169. {
  170. return sprintf(page, "%llu\n",
  171. (unsigned long long)q->limits.max_write_same_sectors << 9);
  172. }
  173. static ssize_t queue_write_zeroes_max_show(struct request_queue *q, char *page)
  174. {
  175. return sprintf(page, "%llu\n",
  176. (unsigned long long)q->limits.max_write_zeroes_sectors << 9);
  177. }
  178. static ssize_t
  179. queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
  180. {
  181. unsigned long max_sectors_kb,
  182. max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
  183. page_kb = 1 << (PAGE_SHIFT - 10);
  184. ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
  185. if (ret < 0)
  186. return ret;
  187. max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
  188. q->limits.max_dev_sectors >> 1);
  189. if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
  190. return -EINVAL;
  191. spin_lock_irq(q->queue_lock);
  192. q->limits.max_sectors = max_sectors_kb << 1;
  193. q->backing_dev_info->io_pages = max_sectors_kb >> (PAGE_SHIFT - 10);
  194. spin_unlock_irq(q->queue_lock);
  195. return ret;
  196. }
  197. static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
  198. {
  199. int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
  200. return queue_var_show(max_hw_sectors_kb, (page));
  201. }
  202. #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
  203. static ssize_t \
  204. queue_show_##name(struct request_queue *q, char *page) \
  205. { \
  206. int bit; \
  207. bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
  208. return queue_var_show(neg ? !bit : bit, page); \
  209. } \
  210. static ssize_t \
  211. queue_store_##name(struct request_queue *q, const char *page, size_t count) \
  212. { \
  213. unsigned long val; \
  214. ssize_t ret; \
  215. ret = queue_var_store(&val, page, count); \
  216. if (ret < 0) \
  217. return ret; \
  218. if (neg) \
  219. val = !val; \
  220. \
  221. if (val) \
  222. blk_queue_flag_set(QUEUE_FLAG_##flag, q); \
  223. else \
  224. blk_queue_flag_clear(QUEUE_FLAG_##flag, q); \
  225. return ret; \
  226. }
  227. QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
  228. QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
  229. QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
  230. #undef QUEUE_SYSFS_BIT_FNS
  231. static ssize_t queue_zoned_show(struct request_queue *q, char *page)
  232. {
  233. switch (blk_queue_zoned_model(q)) {
  234. case BLK_ZONED_HA:
  235. return sprintf(page, "host-aware\n");
  236. case BLK_ZONED_HM:
  237. return sprintf(page, "host-managed\n");
  238. default:
  239. return sprintf(page, "none\n");
  240. }
  241. }
  242. static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
  243. {
  244. return queue_var_show((blk_queue_nomerges(q) << 1) |
  245. blk_queue_noxmerges(q), page);
  246. }
  247. static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
  248. size_t count)
  249. {
  250. unsigned long nm;
  251. ssize_t ret = queue_var_store(&nm, page, count);
  252. if (ret < 0)
  253. return ret;
  254. spin_lock_irq(q->queue_lock);
  255. queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
  256. queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
  257. if (nm == 2)
  258. queue_flag_set(QUEUE_FLAG_NOMERGES, q);
  259. else if (nm)
  260. queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
  261. spin_unlock_irq(q->queue_lock);
  262. return ret;
  263. }
  264. static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
  265. {
  266. bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
  267. bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
  268. return queue_var_show(set << force, page);
  269. }
  270. static ssize_t
  271. queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
  272. {
  273. ssize_t ret = -EINVAL;
  274. #ifdef CONFIG_SMP
  275. unsigned long val;
  276. ret = queue_var_store(&val, page, count);
  277. if (ret < 0)
  278. return ret;
  279. spin_lock_irq(q->queue_lock);
  280. if (val == 2) {
  281. queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  282. queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
  283. } else if (val == 1) {
  284. queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
  285. queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  286. } else if (val == 0) {
  287. queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
  288. queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
  289. }
  290. spin_unlock_irq(q->queue_lock);
  291. #endif
  292. return ret;
  293. }
  294. static ssize_t queue_poll_delay_show(struct request_queue *q, char *page)
  295. {
  296. int val;
  297. if (q->poll_nsec == -1)
  298. val = -1;
  299. else
  300. val = q->poll_nsec / 1000;
  301. return sprintf(page, "%d\n", val);
  302. }
  303. static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page,
  304. size_t count)
  305. {
  306. int err, val;
  307. if (!q->mq_ops || !q->mq_ops->poll)
  308. return -EINVAL;
  309. err = kstrtoint(page, 10, &val);
  310. if (err < 0)
  311. return err;
  312. if (val == -1)
  313. q->poll_nsec = -1;
  314. else
  315. q->poll_nsec = val * 1000;
  316. return count;
  317. }
  318. static ssize_t queue_poll_show(struct request_queue *q, char *page)
  319. {
  320. return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page);
  321. }
  322. static ssize_t queue_poll_store(struct request_queue *q, const char *page,
  323. size_t count)
  324. {
  325. unsigned long poll_on;
  326. ssize_t ret;
  327. if (!q->mq_ops || !q->mq_ops->poll)
  328. return -EINVAL;
  329. ret = queue_var_store(&poll_on, page, count);
  330. if (ret < 0)
  331. return ret;
  332. if (poll_on)
  333. blk_queue_flag_set(QUEUE_FLAG_POLL, q);
  334. else
  335. blk_queue_flag_clear(QUEUE_FLAG_POLL, q);
  336. return ret;
  337. }
  338. static ssize_t queue_wb_lat_show(struct request_queue *q, char *page)
  339. {
  340. if (!wbt_rq_qos(q))
  341. return -EINVAL;
  342. return sprintf(page, "%llu\n", div_u64(wbt_get_min_lat(q), 1000));
  343. }
  344. static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
  345. size_t count)
  346. {
  347. struct rq_qos *rqos;
  348. ssize_t ret;
  349. s64 val;
  350. ret = queue_var_store64(&val, page);
  351. if (ret < 0)
  352. return ret;
  353. if (val < -1)
  354. return -EINVAL;
  355. rqos = wbt_rq_qos(q);
  356. if (!rqos) {
  357. ret = wbt_init(q);
  358. if (ret)
  359. return ret;
  360. }
  361. if (val == -1)
  362. val = wbt_default_latency_nsec(q);
  363. else if (val >= 0)
  364. val *= 1000ULL;
  365. /*
  366. * Ensure that the queue is idled, in case the latency update
  367. * ends up either enabling or disabling wbt completely. We can't
  368. * have IO inflight if that happens.
  369. */
  370. if (q->mq_ops) {
  371. blk_mq_freeze_queue(q);
  372. blk_mq_quiesce_queue(q);
  373. } else
  374. blk_queue_bypass_start(q);
  375. wbt_set_min_lat(q, val);
  376. wbt_update_limits(q);
  377. if (q->mq_ops) {
  378. blk_mq_unquiesce_queue(q);
  379. blk_mq_unfreeze_queue(q);
  380. } else
  381. blk_queue_bypass_end(q);
  382. return count;
  383. }
  384. static ssize_t queue_wc_show(struct request_queue *q, char *page)
  385. {
  386. if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
  387. return sprintf(page, "write back\n");
  388. return sprintf(page, "write through\n");
  389. }
  390. static ssize_t queue_wc_store(struct request_queue *q, const char *page,
  391. size_t count)
  392. {
  393. int set = -1;
  394. if (!strncmp(page, "write back", 10))
  395. set = 1;
  396. else if (!strncmp(page, "write through", 13) ||
  397. !strncmp(page, "none", 4))
  398. set = 0;
  399. if (set == -1)
  400. return -EINVAL;
  401. if (set)
  402. blk_queue_flag_set(QUEUE_FLAG_WC, q);
  403. else
  404. blk_queue_flag_clear(QUEUE_FLAG_WC, q);
  405. return count;
  406. }
  407. static ssize_t queue_fua_show(struct request_queue *q, char *page)
  408. {
  409. return sprintf(page, "%u\n", test_bit(QUEUE_FLAG_FUA, &q->queue_flags));
  410. }
  411. static ssize_t queue_dax_show(struct request_queue *q, char *page)
  412. {
  413. return queue_var_show(blk_queue_dax(q), page);
  414. }
  415. static struct queue_sysfs_entry queue_requests_entry = {
  416. .attr = {.name = "nr_requests", .mode = 0644 },
  417. .show = queue_requests_show,
  418. .store = queue_requests_store,
  419. };
  420. static struct queue_sysfs_entry queue_ra_entry = {
  421. .attr = {.name = "read_ahead_kb", .mode = 0644 },
  422. .show = queue_ra_show,
  423. .store = queue_ra_store,
  424. };
  425. static struct queue_sysfs_entry queue_max_sectors_entry = {
  426. .attr = {.name = "max_sectors_kb", .mode = 0644 },
  427. .show = queue_max_sectors_show,
  428. .store = queue_max_sectors_store,
  429. };
  430. static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
  431. .attr = {.name = "max_hw_sectors_kb", .mode = 0444 },
  432. .show = queue_max_hw_sectors_show,
  433. };
  434. static struct queue_sysfs_entry queue_max_segments_entry = {
  435. .attr = {.name = "max_segments", .mode = 0444 },
  436. .show = queue_max_segments_show,
  437. };
  438. static struct queue_sysfs_entry queue_max_discard_segments_entry = {
  439. .attr = {.name = "max_discard_segments", .mode = 0444 },
  440. .show = queue_max_discard_segments_show,
  441. };
  442. static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
  443. .attr = {.name = "max_integrity_segments", .mode = 0444 },
  444. .show = queue_max_integrity_segments_show,
  445. };
  446. static struct queue_sysfs_entry queue_max_segment_size_entry = {
  447. .attr = {.name = "max_segment_size", .mode = 0444 },
  448. .show = queue_max_segment_size_show,
  449. };
  450. static struct queue_sysfs_entry queue_iosched_entry = {
  451. .attr = {.name = "scheduler", .mode = 0644 },
  452. .show = elv_iosched_show,
  453. .store = elv_iosched_store,
  454. };
  455. static struct queue_sysfs_entry queue_hw_sector_size_entry = {
  456. .attr = {.name = "hw_sector_size", .mode = 0444 },
  457. .show = queue_logical_block_size_show,
  458. };
  459. static struct queue_sysfs_entry queue_logical_block_size_entry = {
  460. .attr = {.name = "logical_block_size", .mode = 0444 },
  461. .show = queue_logical_block_size_show,
  462. };
  463. static struct queue_sysfs_entry queue_physical_block_size_entry = {
  464. .attr = {.name = "physical_block_size", .mode = 0444 },
  465. .show = queue_physical_block_size_show,
  466. };
  467. static struct queue_sysfs_entry queue_chunk_sectors_entry = {
  468. .attr = {.name = "chunk_sectors", .mode = 0444 },
  469. .show = queue_chunk_sectors_show,
  470. };
  471. static struct queue_sysfs_entry queue_io_min_entry = {
  472. .attr = {.name = "minimum_io_size", .mode = 0444 },
  473. .show = queue_io_min_show,
  474. };
  475. static struct queue_sysfs_entry queue_io_opt_entry = {
  476. .attr = {.name = "optimal_io_size", .mode = 0444 },
  477. .show = queue_io_opt_show,
  478. };
  479. static struct queue_sysfs_entry queue_discard_granularity_entry = {
  480. .attr = {.name = "discard_granularity", .mode = 0444 },
  481. .show = queue_discard_granularity_show,
  482. };
  483. static struct queue_sysfs_entry queue_discard_max_hw_entry = {
  484. .attr = {.name = "discard_max_hw_bytes", .mode = 0444 },
  485. .show = queue_discard_max_hw_show,
  486. };
  487. static struct queue_sysfs_entry queue_discard_max_entry = {
  488. .attr = {.name = "discard_max_bytes", .mode = 0644 },
  489. .show = queue_discard_max_show,
  490. .store = queue_discard_max_store,
  491. };
  492. static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
  493. .attr = {.name = "discard_zeroes_data", .mode = 0444 },
  494. .show = queue_discard_zeroes_data_show,
  495. };
  496. static struct queue_sysfs_entry queue_write_same_max_entry = {
  497. .attr = {.name = "write_same_max_bytes", .mode = 0444 },
  498. .show = queue_write_same_max_show,
  499. };
  500. static struct queue_sysfs_entry queue_write_zeroes_max_entry = {
  501. .attr = {.name = "write_zeroes_max_bytes", .mode = 0444 },
  502. .show = queue_write_zeroes_max_show,
  503. };
  504. static struct queue_sysfs_entry queue_nonrot_entry = {
  505. .attr = {.name = "rotational", .mode = 0644 },
  506. .show = queue_show_nonrot,
  507. .store = queue_store_nonrot,
  508. };
  509. static struct queue_sysfs_entry queue_zoned_entry = {
  510. .attr = {.name = "zoned", .mode = 0444 },
  511. .show = queue_zoned_show,
  512. };
  513. static struct queue_sysfs_entry queue_nomerges_entry = {
  514. .attr = {.name = "nomerges", .mode = 0644 },
  515. .show = queue_nomerges_show,
  516. .store = queue_nomerges_store,
  517. };
  518. static struct queue_sysfs_entry queue_rq_affinity_entry = {
  519. .attr = {.name = "rq_affinity", .mode = 0644 },
  520. .show = queue_rq_affinity_show,
  521. .store = queue_rq_affinity_store,
  522. };
  523. static struct queue_sysfs_entry queue_iostats_entry = {
  524. .attr = {.name = "iostats", .mode = 0644 },
  525. .show = queue_show_iostats,
  526. .store = queue_store_iostats,
  527. };
  528. static struct queue_sysfs_entry queue_random_entry = {
  529. .attr = {.name = "add_random", .mode = 0644 },
  530. .show = queue_show_random,
  531. .store = queue_store_random,
  532. };
  533. static struct queue_sysfs_entry queue_poll_entry = {
  534. .attr = {.name = "io_poll", .mode = 0644 },
  535. .show = queue_poll_show,
  536. .store = queue_poll_store,
  537. };
  538. static struct queue_sysfs_entry queue_poll_delay_entry = {
  539. .attr = {.name = "io_poll_delay", .mode = 0644 },
  540. .show = queue_poll_delay_show,
  541. .store = queue_poll_delay_store,
  542. };
  543. static struct queue_sysfs_entry queue_wc_entry = {
  544. .attr = {.name = "write_cache", .mode = 0644 },
  545. .show = queue_wc_show,
  546. .store = queue_wc_store,
  547. };
  548. static struct queue_sysfs_entry queue_fua_entry = {
  549. .attr = {.name = "fua", .mode = 0444 },
  550. .show = queue_fua_show,
  551. };
  552. static struct queue_sysfs_entry queue_dax_entry = {
  553. .attr = {.name = "dax", .mode = 0444 },
  554. .show = queue_dax_show,
  555. };
  556. static struct queue_sysfs_entry queue_wb_lat_entry = {
  557. .attr = {.name = "wbt_lat_usec", .mode = 0644 },
  558. .show = queue_wb_lat_show,
  559. .store = queue_wb_lat_store,
  560. };
  561. #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  562. static struct queue_sysfs_entry throtl_sample_time_entry = {
  563. .attr = {.name = "throttle_sample_time", .mode = 0644 },
  564. .show = blk_throtl_sample_time_show,
  565. .store = blk_throtl_sample_time_store,
  566. };
  567. #endif
  568. static struct attribute *default_attrs[] = {
  569. &queue_requests_entry.attr,
  570. &queue_ra_entry.attr,
  571. &queue_max_hw_sectors_entry.attr,
  572. &queue_max_sectors_entry.attr,
  573. &queue_max_segments_entry.attr,
  574. &queue_max_discard_segments_entry.attr,
  575. &queue_max_integrity_segments_entry.attr,
  576. &queue_max_segment_size_entry.attr,
  577. &queue_iosched_entry.attr,
  578. &queue_hw_sector_size_entry.attr,
  579. &queue_logical_block_size_entry.attr,
  580. &queue_physical_block_size_entry.attr,
  581. &queue_chunk_sectors_entry.attr,
  582. &queue_io_min_entry.attr,
  583. &queue_io_opt_entry.attr,
  584. &queue_discard_granularity_entry.attr,
  585. &queue_discard_max_entry.attr,
  586. &queue_discard_max_hw_entry.attr,
  587. &queue_discard_zeroes_data_entry.attr,
  588. &queue_write_same_max_entry.attr,
  589. &queue_write_zeroes_max_entry.attr,
  590. &queue_nonrot_entry.attr,
  591. &queue_zoned_entry.attr,
  592. &queue_nomerges_entry.attr,
  593. &queue_rq_affinity_entry.attr,
  594. &queue_iostats_entry.attr,
  595. &queue_random_entry.attr,
  596. &queue_poll_entry.attr,
  597. &queue_wc_entry.attr,
  598. &queue_fua_entry.attr,
  599. &queue_dax_entry.attr,
  600. &queue_wb_lat_entry.attr,
  601. &queue_poll_delay_entry.attr,
  602. #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  603. &throtl_sample_time_entry.attr,
  604. #endif
  605. NULL,
  606. };
  607. #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
  608. static ssize_t
  609. queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  610. {
  611. struct queue_sysfs_entry *entry = to_queue(attr);
  612. struct request_queue *q =
  613. container_of(kobj, struct request_queue, kobj);
  614. ssize_t res;
  615. if (!entry->show)
  616. return -EIO;
  617. mutex_lock(&q->sysfs_lock);
  618. if (blk_queue_dying(q)) {
  619. mutex_unlock(&q->sysfs_lock);
  620. return -ENOENT;
  621. }
  622. res = entry->show(q, page);
  623. mutex_unlock(&q->sysfs_lock);
  624. return res;
  625. }
  626. static ssize_t
  627. queue_attr_store(struct kobject *kobj, struct attribute *attr,
  628. const char *page, size_t length)
  629. {
  630. struct queue_sysfs_entry *entry = to_queue(attr);
  631. struct request_queue *q;
  632. ssize_t res;
  633. if (!entry->store)
  634. return -EIO;
  635. q = container_of(kobj, struct request_queue, kobj);
  636. mutex_lock(&q->sysfs_lock);
  637. if (blk_queue_dying(q)) {
  638. mutex_unlock(&q->sysfs_lock);
  639. return -ENOENT;
  640. }
  641. res = entry->store(q, page, length);
  642. mutex_unlock(&q->sysfs_lock);
  643. return res;
  644. }
  645. static void blk_free_queue_rcu(struct rcu_head *rcu_head)
  646. {
  647. struct request_queue *q = container_of(rcu_head, struct request_queue,
  648. rcu_head);
  649. kmem_cache_free(blk_requestq_cachep, q);
  650. }
  651. /**
  652. * __blk_release_queue - release a request queue when it is no longer needed
  653. * @work: pointer to the release_work member of the request queue to be released
  654. *
  655. * Description:
  656. * blk_release_queue is the counterpart of blk_init_queue(). It should be
  657. * called when a request queue is being released; typically when a block
  658. * device is being de-registered. Its primary task it to free the queue
  659. * itself.
  660. *
  661. * Notes:
  662. * The low level driver must have finished any outstanding requests first
  663. * via blk_cleanup_queue().
  664. *
  665. * Although blk_release_queue() may be called with preemption disabled,
  666. * __blk_release_queue() may sleep.
  667. */
  668. static void __blk_release_queue(struct work_struct *work)
  669. {
  670. struct request_queue *q = container_of(work, typeof(*q), release_work);
  671. if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags))
  672. blk_stat_remove_callback(q, q->poll_cb);
  673. blk_stat_free_callback(q->poll_cb);
  674. if (!blk_queue_dead(q)) {
  675. /*
  676. * Last reference was dropped without having called
  677. * blk_cleanup_queue().
  678. */
  679. WARN_ONCE(blk_queue_init_done(q),
  680. "request queue %p has been registered but blk_cleanup_queue() has not been called for that queue\n",
  681. q);
  682. blk_exit_queue(q);
  683. }
  684. WARN(blk_queue_root_blkg(q),
  685. "request queue %p is being released but it has not yet been removed from the blkcg controller\n",
  686. q);
  687. blk_free_queue_stats(q->stats);
  688. if (q->mq_ops)
  689. cancel_delayed_work_sync(&q->requeue_work);
  690. blk_exit_rl(q, &q->root_rl);
  691. if (q->queue_tags)
  692. __blk_queue_free_tags(q);
  693. if (!q->mq_ops) {
  694. if (q->exit_rq_fn)
  695. q->exit_rq_fn(q, q->fq->flush_rq);
  696. blk_free_flush_queue(q->fq);
  697. } else {
  698. blk_mq_release(q);
  699. }
  700. blk_trace_shutdown(q);
  701. if (q->mq_ops)
  702. blk_mq_debugfs_unregister(q);
  703. bioset_exit(&q->bio_split);
  704. ida_simple_remove(&blk_queue_ida, q->id);
  705. call_rcu(&q->rcu_head, blk_free_queue_rcu);
  706. }
  707. static void blk_release_queue(struct kobject *kobj)
  708. {
  709. struct request_queue *q =
  710. container_of(kobj, struct request_queue, kobj);
  711. INIT_WORK(&q->release_work, __blk_release_queue);
  712. schedule_work(&q->release_work);
  713. }
  714. static const struct sysfs_ops queue_sysfs_ops = {
  715. .show = queue_attr_show,
  716. .store = queue_attr_store,
  717. };
  718. struct kobj_type blk_queue_ktype = {
  719. .sysfs_ops = &queue_sysfs_ops,
  720. .default_attrs = default_attrs,
  721. .release = blk_release_queue,
  722. };
  723. /**
  724. * blk_register_queue - register a block layer queue with sysfs
  725. * @disk: Disk of which the request queue should be registered with sysfs.
  726. */
  727. int blk_register_queue(struct gendisk *disk)
  728. {
  729. int ret;
  730. struct device *dev = disk_to_dev(disk);
  731. struct request_queue *q = disk->queue;
  732. if (WARN_ON(!q))
  733. return -ENXIO;
  734. WARN_ONCE(test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags),
  735. "%s is registering an already registered queue\n",
  736. kobject_name(&dev->kobj));
  737. queue_flag_set_unlocked(QUEUE_FLAG_REGISTERED, q);
  738. /*
  739. * SCSI probing may synchronously create and destroy a lot of
  740. * request_queues for non-existent devices. Shutting down a fully
  741. * functional queue takes measureable wallclock time as RCU grace
  742. * periods are involved. To avoid excessive latency in these
  743. * cases, a request_queue starts out in a degraded mode which is
  744. * faster to shut down and is made fully functional here as
  745. * request_queues for non-existent devices never get registered.
  746. */
  747. if (!blk_queue_init_done(q)) {
  748. queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE, q);
  749. percpu_ref_switch_to_percpu(&q->q_usage_counter);
  750. blk_queue_bypass_end(q);
  751. }
  752. ret = blk_trace_init_sysfs(dev);
  753. if (ret)
  754. return ret;
  755. /* Prevent changes through sysfs until registration is completed. */
  756. mutex_lock(&q->sysfs_lock);
  757. ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
  758. if (ret < 0) {
  759. blk_trace_remove_sysfs(dev);
  760. goto unlock;
  761. }
  762. if (q->mq_ops) {
  763. __blk_mq_register_dev(dev, q);
  764. blk_mq_debugfs_register(q);
  765. }
  766. kobject_uevent(&q->kobj, KOBJ_ADD);
  767. wbt_enable_default(q);
  768. blk_throtl_register_queue(q);
  769. if (q->request_fn || (q->mq_ops && q->elevator)) {
  770. ret = elv_register_queue(q);
  771. if (ret) {
  772. mutex_unlock(&q->sysfs_lock);
  773. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  774. kobject_del(&q->kobj);
  775. blk_trace_remove_sysfs(dev);
  776. kobject_put(&dev->kobj);
  777. return ret;
  778. }
  779. }
  780. ret = 0;
  781. unlock:
  782. mutex_unlock(&q->sysfs_lock);
  783. return ret;
  784. }
  785. EXPORT_SYMBOL_GPL(blk_register_queue);
  786. /**
  787. * blk_unregister_queue - counterpart of blk_register_queue()
  788. * @disk: Disk of which the request queue should be unregistered from sysfs.
  789. *
  790. * Note: the caller is responsible for guaranteeing that this function is called
  791. * after blk_register_queue() has finished.
  792. */
  793. void blk_unregister_queue(struct gendisk *disk)
  794. {
  795. struct request_queue *q = disk->queue;
  796. if (WARN_ON(!q))
  797. return;
  798. /* Return early if disk->queue was never registered. */
  799. if (!test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags))
  800. return;
  801. /*
  802. * Since sysfs_remove_dir() prevents adding new directory entries
  803. * before removal of existing entries starts, protect against
  804. * concurrent elv_iosched_store() calls.
  805. */
  806. mutex_lock(&q->sysfs_lock);
  807. blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q);
  808. /*
  809. * Remove the sysfs attributes before unregistering the queue data
  810. * structures that can be modified through sysfs.
  811. */
  812. if (q->mq_ops)
  813. blk_mq_unregister_dev(disk_to_dev(disk), q);
  814. mutex_unlock(&q->sysfs_lock);
  815. kobject_uevent(&q->kobj, KOBJ_REMOVE);
  816. kobject_del(&q->kobj);
  817. blk_trace_remove_sysfs(disk_to_dev(disk));
  818. mutex_lock(&q->sysfs_lock);
  819. if (q->request_fn || (q->mq_ops && q->elevator))
  820. elv_unregister_queue(q);
  821. mutex_unlock(&q->sysfs_lock);
  822. kobject_put(&disk_to_dev(disk)->kobj);
  823. }