blk-settings.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Functions related to setting various queue properties from drivers
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/bio.h>
  9. #include <linux/blk-integrity.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/backing-dev-defs.h>
  12. #include <linux/gcd.h>
  13. #include <linux/lcm.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/gfp.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/t10-pi.h>
  18. #include <linux/crc64.h>
  19. #include "blk.h"
  20. #include "blk-rq-qos.h"
  21. #include "blk-wbt.h"
  22. void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
  23. {
  24. WRITE_ONCE(q->rq_timeout, timeout);
  25. }
  26. EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
  27. /**
  28. * blk_set_stacking_limits - set default limits for stacking devices
  29. * @lim: the queue_limits structure to reset
  30. *
  31. * Prepare queue limits for applying limits from underlying devices using
  32. * blk_stack_limits().
  33. */
  34. void blk_set_stacking_limits(struct queue_limits *lim)
  35. {
  36. memset(lim, 0, sizeof(*lim));
  37. lim->logical_block_size = SECTOR_SIZE;
  38. lim->physical_block_size = SECTOR_SIZE;
  39. lim->io_min = SECTOR_SIZE;
  40. lim->discard_granularity = SECTOR_SIZE;
  41. lim->dma_alignment = SECTOR_SIZE - 1;
  42. lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
  43. /* Inherit limits from component devices */
  44. lim->max_segments = USHRT_MAX;
  45. lim->max_discard_segments = USHRT_MAX;
  46. lim->max_hw_sectors = UINT_MAX;
  47. lim->max_segment_size = UINT_MAX;
  48. lim->max_sectors = UINT_MAX;
  49. lim->max_dev_sectors = UINT_MAX;
  50. lim->max_write_zeroes_sectors = UINT_MAX;
  51. lim->max_hw_wzeroes_unmap_sectors = UINT_MAX;
  52. lim->max_user_wzeroes_unmap_sectors = UINT_MAX;
  53. lim->max_hw_zone_append_sectors = UINT_MAX;
  54. lim->max_user_discard_sectors = UINT_MAX;
  55. lim->atomic_write_hw_max = UINT_MAX;
  56. }
  57. EXPORT_SYMBOL(blk_set_stacking_limits);
  58. void blk_apply_bdi_limits(struct backing_dev_info *bdi,
  59. struct queue_limits *lim)
  60. {
  61. u64 io_opt = lim->io_opt;
  62. /*
  63. * For read-ahead of large files to be effective, we need to read ahead
  64. * at least twice the optimal I/O size. For rotational devices that do
  65. * not report an optimal I/O size (e.g. ATA HDDs), use the maximum I/O
  66. * size to avoid falling back to the (rather inefficient) small default
  67. * read-ahead size.
  68. *
  69. * There is no hardware limitation for the read-ahead size and the user
  70. * might have increased the read-ahead size through sysfs, so don't ever
  71. * decrease it.
  72. */
  73. if (!io_opt && (lim->features & BLK_FEAT_ROTATIONAL))
  74. io_opt = (u64)lim->max_sectors << SECTOR_SHIFT;
  75. bdi->ra_pages = max3(bdi->ra_pages,
  76. io_opt * 2 >> PAGE_SHIFT,
  77. VM_READAHEAD_PAGES);
  78. bdi->io_pages = lim->max_sectors >> PAGE_SECTORS_SHIFT;
  79. }
  80. static int blk_validate_zoned_limits(struct queue_limits *lim)
  81. {
  82. if (!(lim->features & BLK_FEAT_ZONED)) {
  83. if (WARN_ON_ONCE(lim->max_open_zones) ||
  84. WARN_ON_ONCE(lim->max_active_zones) ||
  85. WARN_ON_ONCE(lim->zone_write_granularity) ||
  86. WARN_ON_ONCE(lim->max_zone_append_sectors))
  87. return -EINVAL;
  88. return 0;
  89. }
  90. if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_BLK_DEV_ZONED)))
  91. return -EINVAL;
  92. /*
  93. * Given that active zones include open zones, the maximum number of
  94. * open zones cannot be larger than the maximum number of active zones.
  95. */
  96. if (lim->max_active_zones &&
  97. lim->max_open_zones > lim->max_active_zones)
  98. return -EINVAL;
  99. if (lim->zone_write_granularity < lim->logical_block_size)
  100. lim->zone_write_granularity = lim->logical_block_size;
  101. /*
  102. * The Zone Append size is limited by the maximum I/O size and the zone
  103. * size given that it can't span zones.
  104. *
  105. * If no max_hw_zone_append_sectors limit is provided, the block layer
  106. * will emulated it, else we're also bound by the hardware limit.
  107. */
  108. lim->max_zone_append_sectors =
  109. min_not_zero(lim->max_hw_zone_append_sectors,
  110. min(lim->chunk_sectors, lim->max_hw_sectors));
  111. return 0;
  112. }
  113. /*
  114. * Maximum size of I/O that needs a block layer integrity buffer. Limited
  115. * by the number of intervals for which we can fit the integrity buffer into
  116. * the buffer size. Because the buffer is a single segment it is also limited
  117. * by the maximum segment size.
  118. */
  119. static inline unsigned int max_integrity_io_size(struct queue_limits *lim)
  120. {
  121. return min_t(unsigned int, lim->max_segment_size,
  122. (BLK_INTEGRITY_MAX_SIZE / lim->integrity.metadata_size) <<
  123. lim->integrity.interval_exp);
  124. }
  125. static int blk_validate_integrity_limits(struct queue_limits *lim)
  126. {
  127. struct blk_integrity *bi = &lim->integrity;
  128. if (!bi->metadata_size) {
  129. if (bi->csum_type != BLK_INTEGRITY_CSUM_NONE ||
  130. bi->tag_size || ((bi->flags & BLK_INTEGRITY_REF_TAG))) {
  131. pr_warn("invalid PI settings.\n");
  132. return -EINVAL;
  133. }
  134. bi->flags |= BLK_INTEGRITY_NOGENERATE | BLK_INTEGRITY_NOVERIFY;
  135. return 0;
  136. }
  137. if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) {
  138. pr_warn("integrity support disabled.\n");
  139. return -EINVAL;
  140. }
  141. if (bi->csum_type == BLK_INTEGRITY_CSUM_NONE &&
  142. (bi->flags & BLK_INTEGRITY_REF_TAG)) {
  143. pr_warn("ref tag not support without checksum.\n");
  144. return -EINVAL;
  145. }
  146. if (bi->pi_offset + bi->pi_tuple_size > bi->metadata_size) {
  147. pr_warn("pi_offset (%u) + pi_tuple_size (%u) exceeds metadata_size (%u)\n",
  148. bi->pi_offset, bi->pi_tuple_size, bi->metadata_size);
  149. return -EINVAL;
  150. }
  151. switch (bi->csum_type) {
  152. case BLK_INTEGRITY_CSUM_NONE:
  153. if (bi->pi_tuple_size) {
  154. pr_warn("pi_tuple_size must be 0 when checksum type is none\n");
  155. return -EINVAL;
  156. }
  157. break;
  158. case BLK_INTEGRITY_CSUM_CRC:
  159. case BLK_INTEGRITY_CSUM_IP:
  160. if (bi->pi_tuple_size != sizeof(struct t10_pi_tuple)) {
  161. pr_warn("pi_tuple_size mismatch for T10 PI: expected %zu, got %u\n",
  162. sizeof(struct t10_pi_tuple),
  163. bi->pi_tuple_size);
  164. return -EINVAL;
  165. }
  166. break;
  167. case BLK_INTEGRITY_CSUM_CRC64:
  168. if (bi->pi_tuple_size != sizeof(struct crc64_pi_tuple)) {
  169. pr_warn("pi_tuple_size mismatch for CRC64 PI: expected %zu, got %u\n",
  170. sizeof(struct crc64_pi_tuple),
  171. bi->pi_tuple_size);
  172. return -EINVAL;
  173. }
  174. break;
  175. }
  176. if (!bi->interval_exp) {
  177. bi->interval_exp = ilog2(lim->logical_block_size);
  178. } else if (bi->interval_exp < SECTOR_SHIFT ||
  179. bi->interval_exp > ilog2(lim->logical_block_size)) {
  180. pr_warn("invalid interval_exp %u\n", bi->interval_exp);
  181. return -EINVAL;
  182. }
  183. /*
  184. * The PI generation / validation helpers do not expect intervals to
  185. * straddle multiple bio_vecs. Enforce alignment so that those are
  186. * never generated, and that each buffer is aligned as expected.
  187. */
  188. if (bi->csum_type) {
  189. lim->dma_alignment = max(lim->dma_alignment,
  190. (1U << bi->interval_exp) - 1);
  191. }
  192. /*
  193. * The block layer automatically adds integrity data for bios that don't
  194. * already have it. Limit the I/O size so that a single maximum size
  195. * metadata segment can cover the integrity data for the entire I/O.
  196. */
  197. lim->max_sectors = min(lim->max_sectors,
  198. max_integrity_io_size(lim) >> SECTOR_SHIFT);
  199. return 0;
  200. }
  201. /*
  202. * Returns max guaranteed bytes which we can fit in a bio.
  203. *
  204. * We request that an atomic_write is ITER_UBUF iov_iter (so a single vector),
  205. * so we assume that we can fit in at least PAGE_SIZE in a segment, apart from
  206. * the first and last segments.
  207. */
  208. static unsigned int blk_queue_max_guaranteed_bio(struct queue_limits *lim)
  209. {
  210. unsigned int max_segments = min(BIO_MAX_VECS, lim->max_segments);
  211. unsigned int length;
  212. length = min(max_segments, 2) * lim->logical_block_size;
  213. if (max_segments > 2)
  214. length += (max_segments - 2) * PAGE_SIZE;
  215. return length;
  216. }
  217. static void blk_atomic_writes_update_limits(struct queue_limits *lim)
  218. {
  219. unsigned int unit_limit = min(lim->max_hw_sectors << SECTOR_SHIFT,
  220. blk_queue_max_guaranteed_bio(lim));
  221. unit_limit = rounddown_pow_of_two(unit_limit);
  222. lim->atomic_write_max_sectors =
  223. min(lim->atomic_write_hw_max >> SECTOR_SHIFT,
  224. lim->max_hw_sectors);
  225. lim->atomic_write_unit_min =
  226. min(lim->atomic_write_hw_unit_min, unit_limit);
  227. lim->atomic_write_unit_max =
  228. min(lim->atomic_write_hw_unit_max, unit_limit);
  229. lim->atomic_write_boundary_sectors =
  230. lim->atomic_write_hw_boundary >> SECTOR_SHIFT;
  231. }
  232. /*
  233. * Test whether any boundary is aligned with any chunk size. Stacked
  234. * devices store any stripe size in t->chunk_sectors.
  235. */
  236. static bool blk_valid_atomic_writes_boundary(unsigned int chunk_sectors,
  237. unsigned int boundary_sectors)
  238. {
  239. if (!chunk_sectors || !boundary_sectors)
  240. return true;
  241. if (boundary_sectors > chunk_sectors &&
  242. boundary_sectors % chunk_sectors)
  243. return false;
  244. if (chunk_sectors > boundary_sectors &&
  245. chunk_sectors % boundary_sectors)
  246. return false;
  247. return true;
  248. }
  249. static void blk_validate_atomic_write_limits(struct queue_limits *lim)
  250. {
  251. unsigned int boundary_sectors;
  252. unsigned int atomic_write_hw_max_sectors =
  253. lim->atomic_write_hw_max >> SECTOR_SHIFT;
  254. if (!(lim->features & BLK_FEAT_ATOMIC_WRITES))
  255. goto unsupported;
  256. /* UINT_MAX indicates stacked limits in initial state */
  257. if (lim->atomic_write_hw_max == UINT_MAX)
  258. goto unsupported;
  259. if (!lim->atomic_write_hw_max)
  260. goto unsupported;
  261. if (WARN_ON_ONCE(!is_power_of_2(lim->atomic_write_hw_unit_min)))
  262. goto unsupported;
  263. if (WARN_ON_ONCE(!is_power_of_2(lim->atomic_write_hw_unit_max)))
  264. goto unsupported;
  265. if (WARN_ON_ONCE(lim->atomic_write_hw_unit_min >
  266. lim->atomic_write_hw_unit_max))
  267. goto unsupported;
  268. if (WARN_ON_ONCE(lim->atomic_write_hw_unit_max >
  269. lim->atomic_write_hw_max))
  270. goto unsupported;
  271. if (WARN_ON_ONCE(lim->chunk_sectors &&
  272. atomic_write_hw_max_sectors > lim->chunk_sectors))
  273. goto unsupported;
  274. boundary_sectors = lim->atomic_write_hw_boundary >> SECTOR_SHIFT;
  275. if (boundary_sectors) {
  276. if (WARN_ON_ONCE(lim->atomic_write_hw_max >
  277. lim->atomic_write_hw_boundary))
  278. goto unsupported;
  279. if (WARN_ON_ONCE(!blk_valid_atomic_writes_boundary(
  280. lim->chunk_sectors, boundary_sectors)))
  281. goto unsupported;
  282. /*
  283. * The boundary size just needs to be a multiple of unit_max
  284. * (and not necessarily a power-of-2), so this following check
  285. * could be relaxed in future.
  286. * Furthermore, if needed, unit_max could even be reduced so
  287. * that it is compliant with a !power-of-2 boundary.
  288. */
  289. if (!is_power_of_2(boundary_sectors))
  290. goto unsupported;
  291. }
  292. blk_atomic_writes_update_limits(lim);
  293. return;
  294. unsupported:
  295. lim->atomic_write_max_sectors = 0;
  296. lim->atomic_write_boundary_sectors = 0;
  297. lim->atomic_write_unit_min = 0;
  298. lim->atomic_write_unit_max = 0;
  299. }
  300. /*
  301. * Check that the limits in lim are valid, initialize defaults for unset
  302. * values, and cap values based on others where needed.
  303. */
  304. int blk_validate_limits(struct queue_limits *lim)
  305. {
  306. unsigned int max_hw_sectors;
  307. unsigned int logical_block_sectors;
  308. unsigned long seg_size;
  309. int err;
  310. /*
  311. * Unless otherwise specified, default to 512 byte logical blocks and a
  312. * physical block size equal to the logical block size.
  313. */
  314. if (!lim->logical_block_size)
  315. lim->logical_block_size = SECTOR_SIZE;
  316. else if (blk_validate_block_size(lim->logical_block_size)) {
  317. pr_warn("Invalid logical block size (%d)\n", lim->logical_block_size);
  318. return -EINVAL;
  319. }
  320. if (lim->physical_block_size < lim->logical_block_size) {
  321. lim->physical_block_size = lim->logical_block_size;
  322. } else if (!is_power_of_2(lim->physical_block_size)) {
  323. pr_warn("Invalid physical block size (%d)\n", lim->physical_block_size);
  324. return -EINVAL;
  325. }
  326. /*
  327. * The minimum I/O size defaults to the physical block size unless
  328. * explicitly overridden.
  329. */
  330. if (lim->io_min < lim->physical_block_size)
  331. lim->io_min = lim->physical_block_size;
  332. /*
  333. * The optimal I/O size may not be aligned to physical block size
  334. * (because it may be limited by dma engines which have no clue about
  335. * block size of the disks attached to them), so we round it down here.
  336. */
  337. lim->io_opt = round_down(lim->io_opt, lim->physical_block_size);
  338. /*
  339. * max_hw_sectors has a somewhat weird default for historical reason,
  340. * but driver really should set their own instead of relying on this
  341. * value.
  342. *
  343. * The block layer relies on the fact that every driver can
  344. * handle at lest a page worth of data per I/O, and needs the value
  345. * aligned to the logical block size.
  346. */
  347. if (!lim->max_hw_sectors)
  348. lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS;
  349. if (WARN_ON_ONCE(lim->max_hw_sectors < PAGE_SECTORS))
  350. return -EINVAL;
  351. logical_block_sectors = lim->logical_block_size >> SECTOR_SHIFT;
  352. if (WARN_ON_ONCE(logical_block_sectors > lim->max_hw_sectors))
  353. return -EINVAL;
  354. lim->max_hw_sectors = round_down(lim->max_hw_sectors,
  355. logical_block_sectors);
  356. /*
  357. * The actual max_sectors value is a complex beast and also takes the
  358. * max_dev_sectors value (set by SCSI ULPs) and a user configurable
  359. * value into account. The ->max_sectors value is always calculated
  360. * from these, so directly setting it won't have any effect.
  361. */
  362. max_hw_sectors = min_not_zero(lim->max_hw_sectors,
  363. lim->max_dev_sectors);
  364. if (lim->max_user_sectors) {
  365. if (lim->max_user_sectors < BLK_MIN_SEGMENT_SIZE / SECTOR_SIZE)
  366. return -EINVAL;
  367. lim->max_sectors = min(max_hw_sectors, lim->max_user_sectors);
  368. } else if (lim->io_opt > (BLK_DEF_MAX_SECTORS_CAP << SECTOR_SHIFT)) {
  369. lim->max_sectors =
  370. min(max_hw_sectors, lim->io_opt >> SECTOR_SHIFT);
  371. } else if (lim->io_min > (BLK_DEF_MAX_SECTORS_CAP << SECTOR_SHIFT)) {
  372. lim->max_sectors =
  373. min(max_hw_sectors, lim->io_min >> SECTOR_SHIFT);
  374. } else {
  375. lim->max_sectors = min(max_hw_sectors, BLK_DEF_MAX_SECTORS_CAP);
  376. }
  377. lim->max_sectors = round_down(lim->max_sectors,
  378. logical_block_sectors);
  379. /*
  380. * Random default for the maximum number of segments. Driver should not
  381. * rely on this and set their own.
  382. */
  383. if (!lim->max_segments)
  384. lim->max_segments = BLK_MAX_SEGMENTS;
  385. if (lim->max_hw_wzeroes_unmap_sectors &&
  386. lim->max_hw_wzeroes_unmap_sectors != lim->max_write_zeroes_sectors)
  387. return -EINVAL;
  388. lim->max_wzeroes_unmap_sectors = min(lim->max_hw_wzeroes_unmap_sectors,
  389. lim->max_user_wzeroes_unmap_sectors);
  390. lim->max_discard_sectors =
  391. min(lim->max_hw_discard_sectors, lim->max_user_discard_sectors);
  392. /*
  393. * When discard is not supported, discard_granularity should be reported
  394. * as 0 to userspace.
  395. */
  396. if (lim->max_discard_sectors)
  397. lim->discard_granularity =
  398. max(lim->discard_granularity, lim->physical_block_size);
  399. else
  400. lim->discard_granularity = 0;
  401. if (!lim->max_discard_segments)
  402. lim->max_discard_segments = 1;
  403. /*
  404. * By default there is no limit on the segment boundary alignment,
  405. * but if there is one it can't be smaller than the page size as
  406. * that would break all the normal I/O patterns.
  407. */
  408. if (!lim->seg_boundary_mask)
  409. lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
  410. if (WARN_ON_ONCE(lim->seg_boundary_mask < BLK_MIN_SEGMENT_SIZE - 1))
  411. return -EINVAL;
  412. /*
  413. * Stacking device may have both virtual boundary and max segment
  414. * size limit, so allow this setting now, and long-term the two
  415. * might need to move out of stacking limits since we have immutable
  416. * bvec and lower layer bio splitting is supposed to handle the two
  417. * correctly.
  418. */
  419. if (lim->virt_boundary_mask) {
  420. if (!lim->max_segment_size)
  421. lim->max_segment_size = UINT_MAX;
  422. } else {
  423. /*
  424. * The maximum segment size has an odd historic 64k default that
  425. * drivers probably should override. Just like the I/O size we
  426. * require drivers to at least handle a full page per segment.
  427. */
  428. if (!lim->max_segment_size)
  429. lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
  430. if (WARN_ON_ONCE(lim->max_segment_size < BLK_MIN_SEGMENT_SIZE))
  431. return -EINVAL;
  432. }
  433. /* setup max segment size for building new segment in fast path */
  434. if (lim->seg_boundary_mask > lim->max_segment_size - 1)
  435. seg_size = lim->max_segment_size;
  436. else
  437. seg_size = lim->seg_boundary_mask + 1;
  438. lim->max_fast_segment_size = min_t(unsigned int, seg_size, PAGE_SIZE);
  439. /*
  440. * We require drivers to at least do logical block aligned I/O, but
  441. * historically could not check for that due to the separate calls
  442. * to set the limits. Once the transition is finished the check
  443. * below should be narrowed down to check the logical block size.
  444. */
  445. if (!lim->dma_alignment)
  446. lim->dma_alignment = SECTOR_SIZE - 1;
  447. if (WARN_ON_ONCE(lim->dma_alignment > PAGE_SIZE))
  448. return -EINVAL;
  449. if (lim->alignment_offset) {
  450. lim->alignment_offset &= (lim->physical_block_size - 1);
  451. lim->flags &= ~BLK_FLAG_MISALIGNED;
  452. }
  453. if (!(lim->features & BLK_FEAT_WRITE_CACHE))
  454. lim->features &= ~BLK_FEAT_FUA;
  455. blk_validate_atomic_write_limits(lim);
  456. err = blk_validate_integrity_limits(lim);
  457. if (err)
  458. return err;
  459. return blk_validate_zoned_limits(lim);
  460. }
  461. EXPORT_SYMBOL_GPL(blk_validate_limits);
  462. /*
  463. * Set the default limits for a newly allocated queue. @lim contains the
  464. * initial limits set by the driver, which could be no limit in which case
  465. * all fields are cleared to zero.
  466. */
  467. int blk_set_default_limits(struct queue_limits *lim)
  468. {
  469. /*
  470. * Most defaults are set by capping the bounds in blk_validate_limits,
  471. * but these limits are special and need an explicit initialization to
  472. * the max value here.
  473. */
  474. lim->max_user_discard_sectors = UINT_MAX;
  475. lim->max_user_wzeroes_unmap_sectors = UINT_MAX;
  476. return blk_validate_limits(lim);
  477. }
  478. /**
  479. * queue_limits_commit_update - commit an atomic update of queue limits
  480. * @q: queue to update
  481. * @lim: limits to apply
  482. *
  483. * Apply the limits in @lim that were obtained from queue_limits_start_update()
  484. * and updated by the caller to @q. The caller must have frozen the queue or
  485. * ensure that there are no outstanding I/Os by other means.
  486. *
  487. * Returns 0 if successful, else a negative error code.
  488. */
  489. int queue_limits_commit_update(struct request_queue *q,
  490. struct queue_limits *lim)
  491. {
  492. int error;
  493. lockdep_assert_held(&q->limits_lock);
  494. error = blk_validate_limits(lim);
  495. if (error)
  496. goto out_unlock;
  497. #ifdef CONFIG_BLK_INLINE_ENCRYPTION
  498. if (q->crypto_profile && lim->integrity.tag_size) {
  499. pr_warn("blk-integrity: Integrity and hardware inline encryption are not supported together.\n");
  500. error = -EINVAL;
  501. goto out_unlock;
  502. }
  503. #endif
  504. q->limits = *lim;
  505. if (q->disk)
  506. blk_apply_bdi_limits(q->disk->bdi, lim);
  507. out_unlock:
  508. mutex_unlock(&q->limits_lock);
  509. return error;
  510. }
  511. EXPORT_SYMBOL_GPL(queue_limits_commit_update);
  512. /**
  513. * queue_limits_commit_update_frozen - commit an atomic update of queue limits
  514. * @q: queue to update
  515. * @lim: limits to apply
  516. *
  517. * Apply the limits in @lim that were obtained from queue_limits_start_update()
  518. * and updated with the new values by the caller to @q. Freezes the queue
  519. * before the update and unfreezes it after.
  520. *
  521. * Returns 0 if successful, else a negative error code.
  522. */
  523. int queue_limits_commit_update_frozen(struct request_queue *q,
  524. struct queue_limits *lim)
  525. {
  526. unsigned int memflags;
  527. int ret;
  528. memflags = blk_mq_freeze_queue(q);
  529. ret = queue_limits_commit_update(q, lim);
  530. blk_mq_unfreeze_queue(q, memflags);
  531. return ret;
  532. }
  533. EXPORT_SYMBOL_GPL(queue_limits_commit_update_frozen);
  534. /**
  535. * queue_limits_set - apply queue limits to queue
  536. * @q: queue to update
  537. * @lim: limits to apply
  538. *
  539. * Apply the limits in @lim that were freshly initialized to @q.
  540. * To update existing limits use queue_limits_start_update() and
  541. * queue_limits_commit_update() instead.
  542. *
  543. * Returns 0 if successful, else a negative error code.
  544. */
  545. int queue_limits_set(struct request_queue *q, struct queue_limits *lim)
  546. {
  547. mutex_lock(&q->limits_lock);
  548. return queue_limits_commit_update(q, lim);
  549. }
  550. EXPORT_SYMBOL_GPL(queue_limits_set);
  551. static int queue_limit_alignment_offset(const struct queue_limits *lim,
  552. sector_t sector)
  553. {
  554. unsigned int granularity = max(lim->physical_block_size, lim->io_min);
  555. unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
  556. << SECTOR_SHIFT;
  557. return (granularity + lim->alignment_offset - alignment) % granularity;
  558. }
  559. static unsigned int queue_limit_discard_alignment(
  560. const struct queue_limits *lim, sector_t sector)
  561. {
  562. unsigned int alignment, granularity, offset;
  563. if (!lim->max_discard_sectors)
  564. return 0;
  565. /* Why are these in bytes, not sectors? */
  566. alignment = lim->discard_alignment >> SECTOR_SHIFT;
  567. granularity = lim->discard_granularity >> SECTOR_SHIFT;
  568. /* Offset of the partition start in 'granularity' sectors */
  569. offset = sector_div(sector, granularity);
  570. /* And why do we do this modulus *again* in blkdev_issue_discard()? */
  571. offset = (granularity + alignment - offset) % granularity;
  572. /* Turn it back into bytes, gaah */
  573. return offset << SECTOR_SHIFT;
  574. }
  575. static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs)
  576. {
  577. sectors = round_down(sectors, lbs >> SECTOR_SHIFT);
  578. if (sectors < PAGE_SIZE >> SECTOR_SHIFT)
  579. sectors = PAGE_SIZE >> SECTOR_SHIFT;
  580. return sectors;
  581. }
  582. /* Check if second and later bottom devices are compliant */
  583. static bool blk_stack_atomic_writes_tail(struct queue_limits *t,
  584. struct queue_limits *b)
  585. {
  586. /* We're not going to support different boundary sizes.. yet */
  587. if (t->atomic_write_hw_boundary != b->atomic_write_hw_boundary)
  588. return false;
  589. /* Can't support this */
  590. if (t->atomic_write_hw_unit_min > b->atomic_write_hw_unit_max)
  591. return false;
  592. /* Or this */
  593. if (t->atomic_write_hw_unit_max < b->atomic_write_hw_unit_min)
  594. return false;
  595. t->atomic_write_hw_max = min(t->atomic_write_hw_max,
  596. b->atomic_write_hw_max);
  597. t->atomic_write_hw_unit_min = max(t->atomic_write_hw_unit_min,
  598. b->atomic_write_hw_unit_min);
  599. t->atomic_write_hw_unit_max = min(t->atomic_write_hw_unit_max,
  600. b->atomic_write_hw_unit_max);
  601. return true;
  602. }
  603. static void blk_stack_atomic_writes_chunk_sectors(struct queue_limits *t)
  604. {
  605. unsigned int chunk_bytes;
  606. if (!t->chunk_sectors)
  607. return;
  608. /*
  609. * If chunk sectors is so large that its value in bytes overflows
  610. * UINT_MAX, then just shift it down so it definitely will fit.
  611. * We don't support atomic writes of such a large size anyway.
  612. */
  613. if (check_shl_overflow(t->chunk_sectors, SECTOR_SHIFT, &chunk_bytes))
  614. chunk_bytes = t->chunk_sectors;
  615. /*
  616. * Find values for limits which work for chunk size.
  617. * b->atomic_write_hw_unit_{min, max} may not be aligned with chunk
  618. * size, as the chunk size is not restricted to a power-of-2.
  619. * So we need to find highest power-of-2 which works for the chunk
  620. * size.
  621. * As an example scenario, we could have t->unit_max = 16K and
  622. * t->chunk_sectors = 24KB. For this case, reduce t->unit_max to a
  623. * value aligned with both limits, i.e. 8K in this example.
  624. */
  625. t->atomic_write_hw_unit_max = min(t->atomic_write_hw_unit_max,
  626. max_pow_of_two_factor(chunk_bytes));
  627. t->atomic_write_hw_unit_min = min(t->atomic_write_hw_unit_min,
  628. t->atomic_write_hw_unit_max);
  629. t->atomic_write_hw_max = min(t->atomic_write_hw_max, chunk_bytes);
  630. }
  631. /* Check stacking of first bottom device */
  632. static bool blk_stack_atomic_writes_head(struct queue_limits *t,
  633. struct queue_limits *b)
  634. {
  635. if (!blk_valid_atomic_writes_boundary(t->chunk_sectors,
  636. b->atomic_write_hw_boundary >> SECTOR_SHIFT))
  637. return false;
  638. t->atomic_write_hw_unit_max = b->atomic_write_hw_unit_max;
  639. t->atomic_write_hw_unit_min = b->atomic_write_hw_unit_min;
  640. t->atomic_write_hw_max = b->atomic_write_hw_max;
  641. t->atomic_write_hw_boundary = b->atomic_write_hw_boundary;
  642. return true;
  643. }
  644. static void blk_stack_atomic_writes_limits(struct queue_limits *t,
  645. struct queue_limits *b, sector_t start)
  646. {
  647. if (!(b->features & BLK_FEAT_ATOMIC_WRITES))
  648. goto unsupported;
  649. if (!b->atomic_write_hw_unit_min)
  650. goto unsupported;
  651. if (!blk_atomic_write_start_sect_aligned(start, b))
  652. goto unsupported;
  653. /* UINT_MAX indicates no stacking of bottom devices yet */
  654. if (t->atomic_write_hw_max == UINT_MAX) {
  655. if (!blk_stack_atomic_writes_head(t, b))
  656. goto unsupported;
  657. } else {
  658. if (!blk_stack_atomic_writes_tail(t, b))
  659. goto unsupported;
  660. }
  661. blk_stack_atomic_writes_chunk_sectors(t);
  662. return;
  663. unsupported:
  664. t->atomic_write_hw_max = 0;
  665. t->atomic_write_hw_unit_max = 0;
  666. t->atomic_write_hw_unit_min = 0;
  667. t->atomic_write_hw_boundary = 0;
  668. }
  669. /**
  670. * blk_stack_limits - adjust queue_limits for stacked devices
  671. * @t: the stacking driver limits (top device)
  672. * @b: the underlying queue limits (bottom, component device)
  673. * @start: first data sector within component device
  674. *
  675. * Description:
  676. * This function is used by stacking drivers like MD and DM to ensure
  677. * that all component devices have compatible block sizes and
  678. * alignments. The stacking driver must provide a queue_limits
  679. * struct (top) and then iteratively call the stacking function for
  680. * all component (bottom) devices. The stacking function will
  681. * attempt to combine the values and ensure proper alignment.
  682. *
  683. * Returns 0 if the top and bottom queue_limits are compatible. The
  684. * top device's block sizes and alignment offsets may be adjusted to
  685. * ensure alignment with the bottom device. If no compatible sizes
  686. * and alignments exist, -1 is returned and the resulting top
  687. * queue_limits will have the misaligned flag set to indicate that
  688. * the alignment_offset is undefined.
  689. */
  690. int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
  691. sector_t start)
  692. {
  693. unsigned int top, bottom, alignment;
  694. int ret = 0;
  695. t->features |= (b->features & BLK_FEAT_INHERIT_MASK);
  696. /*
  697. * Some feaures need to be supported both by the stacking driver and all
  698. * underlying devices. The stacking driver sets these flags before
  699. * stacking the limits, and this will clear the flags if any of the
  700. * underlying devices does not support it.
  701. */
  702. if (!(b->features & BLK_FEAT_NOWAIT))
  703. t->features &= ~BLK_FEAT_NOWAIT;
  704. if (!(b->features & BLK_FEAT_POLL))
  705. t->features &= ~BLK_FEAT_POLL;
  706. t->flags |= (b->flags & BLK_FLAG_MISALIGNED);
  707. t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
  708. t->max_user_sectors = min_not_zero(t->max_user_sectors,
  709. b->max_user_sectors);
  710. t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
  711. t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
  712. t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
  713. b->max_write_zeroes_sectors);
  714. t->max_user_wzeroes_unmap_sectors =
  715. min(t->max_user_wzeroes_unmap_sectors,
  716. b->max_user_wzeroes_unmap_sectors);
  717. t->max_hw_wzeroes_unmap_sectors =
  718. min(t->max_hw_wzeroes_unmap_sectors,
  719. b->max_hw_wzeroes_unmap_sectors);
  720. t->max_hw_zone_append_sectors = min(t->max_hw_zone_append_sectors,
  721. b->max_hw_zone_append_sectors);
  722. t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
  723. b->seg_boundary_mask);
  724. t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
  725. b->virt_boundary_mask);
  726. t->max_segments = min_not_zero(t->max_segments, b->max_segments);
  727. t->max_discard_segments = min_not_zero(t->max_discard_segments,
  728. b->max_discard_segments);
  729. t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
  730. b->max_integrity_segments);
  731. t->max_segment_size = min_not_zero(t->max_segment_size,
  732. b->max_segment_size);
  733. alignment = queue_limit_alignment_offset(b, start);
  734. /* Bottom device has different alignment. Check that it is
  735. * compatible with the current top alignment.
  736. */
  737. if (t->alignment_offset != alignment) {
  738. top = max(t->physical_block_size, t->io_min)
  739. + t->alignment_offset;
  740. bottom = max(b->physical_block_size, b->io_min) + alignment;
  741. /* Verify that top and bottom intervals line up */
  742. if (max(top, bottom) % min(top, bottom)) {
  743. t->flags |= BLK_FLAG_MISALIGNED;
  744. ret = -1;
  745. }
  746. }
  747. t->logical_block_size = max(t->logical_block_size,
  748. b->logical_block_size);
  749. t->physical_block_size = max(t->physical_block_size,
  750. b->physical_block_size);
  751. t->io_min = max(t->io_min, b->io_min);
  752. t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
  753. t->dma_alignment = max(t->dma_alignment, b->dma_alignment);
  754. /* Set non-power-of-2 compatible chunk_sectors boundary */
  755. if (b->chunk_sectors)
  756. t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors);
  757. /* Physical block size a multiple of the logical block size? */
  758. if (t->physical_block_size & (t->logical_block_size - 1)) {
  759. t->physical_block_size = t->logical_block_size;
  760. t->flags |= BLK_FLAG_MISALIGNED;
  761. ret = -1;
  762. }
  763. /* Minimum I/O a multiple of the physical block size? */
  764. if (t->io_min & (t->physical_block_size - 1)) {
  765. t->io_min = t->physical_block_size;
  766. t->flags |= BLK_FLAG_MISALIGNED;
  767. ret = -1;
  768. }
  769. /* Optimal I/O a multiple of the physical block size? */
  770. if (t->io_opt & (t->physical_block_size - 1)) {
  771. t->io_opt = 0;
  772. t->flags |= BLK_FLAG_MISALIGNED;
  773. ret = -1;
  774. }
  775. /* chunk_sectors a multiple of the physical block size? */
  776. if (t->chunk_sectors % (t->physical_block_size >> SECTOR_SHIFT)) {
  777. t->chunk_sectors = 0;
  778. t->flags |= BLK_FLAG_MISALIGNED;
  779. ret = -1;
  780. }
  781. /* Find lowest common alignment_offset */
  782. t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
  783. % max(t->physical_block_size, t->io_min);
  784. /* Verify that new alignment_offset is on a logical block boundary */
  785. if (t->alignment_offset & (t->logical_block_size - 1)) {
  786. t->flags |= BLK_FLAG_MISALIGNED;
  787. ret = -1;
  788. }
  789. t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size);
  790. t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size);
  791. t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size);
  792. /* Discard alignment and granularity */
  793. if (b->discard_granularity) {
  794. alignment = queue_limit_discard_alignment(b, start);
  795. t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
  796. b->max_discard_sectors);
  797. t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
  798. b->max_hw_discard_sectors);
  799. t->discard_granularity = max(t->discard_granularity,
  800. b->discard_granularity);
  801. t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
  802. t->discard_granularity;
  803. }
  804. t->max_secure_erase_sectors = min_not_zero(t->max_secure_erase_sectors,
  805. b->max_secure_erase_sectors);
  806. t->zone_write_granularity = max(t->zone_write_granularity,
  807. b->zone_write_granularity);
  808. if (!(t->features & BLK_FEAT_ZONED)) {
  809. t->zone_write_granularity = 0;
  810. t->max_zone_append_sectors = 0;
  811. }
  812. blk_stack_atomic_writes_limits(t, b, start);
  813. return ret;
  814. }
  815. EXPORT_SYMBOL(blk_stack_limits);
  816. /**
  817. * queue_limits_stack_bdev - adjust queue_limits for stacked devices
  818. * @t: the stacking driver limits (top device)
  819. * @bdev: the underlying block device (bottom)
  820. * @offset: offset to beginning of data within component device
  821. * @pfx: prefix to use for warnings logged
  822. *
  823. * Description:
  824. * This function is used by stacking drivers like MD and DM to ensure
  825. * that all component devices have compatible block sizes and
  826. * alignments. The stacking driver must provide a queue_limits
  827. * struct (top) and then iteratively call the stacking function for
  828. * all component (bottom) devices. The stacking function will
  829. * attempt to combine the values and ensure proper alignment.
  830. */
  831. void queue_limits_stack_bdev(struct queue_limits *t, struct block_device *bdev,
  832. sector_t offset, const char *pfx)
  833. {
  834. if (blk_stack_limits(t, bdev_limits(bdev),
  835. get_start_sect(bdev) + offset))
  836. pr_notice("%s: Warning: Device %pg is misaligned\n",
  837. pfx, bdev);
  838. }
  839. EXPORT_SYMBOL_GPL(queue_limits_stack_bdev);
  840. /**
  841. * queue_limits_stack_integrity - stack integrity profile
  842. * @t: target queue limits
  843. * @b: base queue limits
  844. *
  845. * Check if the integrity profile in the @b can be stacked into the
  846. * target @t. Stacking is possible if either:
  847. *
  848. * a) does not have any integrity information stacked into it yet
  849. * b) the integrity profile in @b is identical to the one in @t
  850. *
  851. * If @b can be stacked into @t, return %true. Else return %false and clear the
  852. * integrity information in @t.
  853. */
  854. bool queue_limits_stack_integrity(struct queue_limits *t,
  855. struct queue_limits *b)
  856. {
  857. struct blk_integrity *ti = &t->integrity;
  858. struct blk_integrity *bi = &b->integrity;
  859. if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
  860. return true;
  861. if (ti->flags & BLK_INTEGRITY_STACKED) {
  862. if (ti->metadata_size != bi->metadata_size)
  863. goto incompatible;
  864. if (ti->interval_exp != bi->interval_exp)
  865. goto incompatible;
  866. if (ti->tag_size != bi->tag_size)
  867. goto incompatible;
  868. if (ti->csum_type != bi->csum_type)
  869. goto incompatible;
  870. if (ti->pi_tuple_size != bi->pi_tuple_size)
  871. goto incompatible;
  872. if ((ti->flags & BLK_INTEGRITY_REF_TAG) !=
  873. (bi->flags & BLK_INTEGRITY_REF_TAG))
  874. goto incompatible;
  875. } else {
  876. ti->flags = BLK_INTEGRITY_STACKED;
  877. ti->flags |= (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) |
  878. (bi->flags & BLK_INTEGRITY_REF_TAG);
  879. ti->csum_type = bi->csum_type;
  880. ti->pi_tuple_size = bi->pi_tuple_size;
  881. ti->metadata_size = bi->metadata_size;
  882. ti->pi_offset = bi->pi_offset;
  883. ti->interval_exp = bi->interval_exp;
  884. ti->tag_size = bi->tag_size;
  885. }
  886. return true;
  887. incompatible:
  888. memset(ti, 0, sizeof(*ti));
  889. return false;
  890. }
  891. EXPORT_SYMBOL_GPL(queue_limits_stack_integrity);
  892. /**
  893. * blk_set_queue_depth - tell the block layer about the device queue depth
  894. * @q: the request queue for the device
  895. * @depth: queue depth
  896. *
  897. */
  898. void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
  899. {
  900. q->queue_depth = depth;
  901. rq_qos_queue_depth_changed(q);
  902. }
  903. EXPORT_SYMBOL(blk_set_queue_depth);
  904. int bdev_alignment_offset(struct block_device *bdev)
  905. {
  906. struct request_queue *q = bdev_get_queue(bdev);
  907. if (q->limits.flags & BLK_FLAG_MISALIGNED)
  908. return -1;
  909. if (bdev_is_partition(bdev))
  910. return queue_limit_alignment_offset(&q->limits,
  911. bdev->bd_start_sect);
  912. return q->limits.alignment_offset;
  913. }
  914. EXPORT_SYMBOL_GPL(bdev_alignment_offset);
  915. unsigned int bdev_discard_alignment(struct block_device *bdev)
  916. {
  917. struct request_queue *q = bdev_get_queue(bdev);
  918. if (bdev_is_partition(bdev))
  919. return queue_limit_discard_alignment(&q->limits,
  920. bdev->bd_start_sect);
  921. return q->limits.discard_alignment;
  922. }
  923. EXPORT_SYMBOL_GPL(bdev_discard_alignment);