blk-settings.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 "blk.h"
  18. #include "blk-rq-qos.h"
  19. #include "blk-wbt.h"
  20. void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
  21. {
  22. q->rq_timeout = timeout;
  23. }
  24. EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
  25. /**
  26. * blk_set_stacking_limits - set default limits for stacking devices
  27. * @lim: the queue_limits structure to reset
  28. *
  29. * Prepare queue limits for applying limits from underlying devices using
  30. * blk_stack_limits().
  31. */
  32. void blk_set_stacking_limits(struct queue_limits *lim)
  33. {
  34. memset(lim, 0, sizeof(*lim));
  35. lim->logical_block_size = SECTOR_SIZE;
  36. lim->physical_block_size = SECTOR_SIZE;
  37. lim->io_min = SECTOR_SIZE;
  38. lim->discard_granularity = SECTOR_SIZE;
  39. lim->dma_alignment = SECTOR_SIZE - 1;
  40. lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
  41. /* Inherit limits from component devices */
  42. lim->max_segments = USHRT_MAX;
  43. lim->max_discard_segments = USHRT_MAX;
  44. lim->max_hw_sectors = UINT_MAX;
  45. lim->max_segment_size = UINT_MAX;
  46. lim->max_sectors = UINT_MAX;
  47. lim->max_dev_sectors = UINT_MAX;
  48. lim->max_write_zeroes_sectors = UINT_MAX;
  49. lim->max_zone_append_sectors = UINT_MAX;
  50. lim->max_user_discard_sectors = UINT_MAX;
  51. }
  52. EXPORT_SYMBOL(blk_set_stacking_limits);
  53. void blk_apply_bdi_limits(struct backing_dev_info *bdi,
  54. struct queue_limits *lim)
  55. {
  56. /*
  57. * For read-ahead of large files to be effective, we need to read ahead
  58. * at least twice the optimal I/O size.
  59. *
  60. * There is no hardware limitation for the read-ahead size and the user
  61. * might have increased the read-ahead size through sysfs, so don't ever
  62. * decrease it.
  63. */
  64. bdi->ra_pages = max3(bdi->ra_pages,
  65. lim->io_opt * 2 / PAGE_SIZE,
  66. VM_READAHEAD_PAGES);
  67. bdi->io_pages = lim->max_sectors >> PAGE_SECTORS_SHIFT;
  68. }
  69. static int blk_validate_zoned_limits(struct queue_limits *lim)
  70. {
  71. if (!(lim->features & BLK_FEAT_ZONED)) {
  72. if (WARN_ON_ONCE(lim->max_open_zones) ||
  73. WARN_ON_ONCE(lim->max_active_zones) ||
  74. WARN_ON_ONCE(lim->zone_write_granularity) ||
  75. WARN_ON_ONCE(lim->max_zone_append_sectors))
  76. return -EINVAL;
  77. return 0;
  78. }
  79. if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_BLK_DEV_ZONED)))
  80. return -EINVAL;
  81. /*
  82. * Given that active zones include open zones, the maximum number of
  83. * open zones cannot be larger than the maximum number of active zones.
  84. */
  85. if (lim->max_active_zones &&
  86. lim->max_open_zones > lim->max_active_zones)
  87. return -EINVAL;
  88. if (lim->zone_write_granularity < lim->logical_block_size)
  89. lim->zone_write_granularity = lim->logical_block_size;
  90. if (lim->max_zone_append_sectors) {
  91. /*
  92. * The Zone Append size is limited by the maximum I/O size
  93. * and the zone size given that it can't span zones.
  94. */
  95. lim->max_zone_append_sectors =
  96. min3(lim->max_hw_sectors,
  97. lim->max_zone_append_sectors,
  98. lim->chunk_sectors);
  99. }
  100. return 0;
  101. }
  102. static int blk_validate_integrity_limits(struct queue_limits *lim)
  103. {
  104. struct blk_integrity *bi = &lim->integrity;
  105. if (!bi->tuple_size) {
  106. if (bi->csum_type != BLK_INTEGRITY_CSUM_NONE ||
  107. bi->tag_size || ((bi->flags & BLK_INTEGRITY_REF_TAG))) {
  108. pr_warn("invalid PI settings.\n");
  109. return -EINVAL;
  110. }
  111. return 0;
  112. }
  113. if (lim->features & BLK_FEAT_BOUNCE_HIGH) {
  114. pr_warn("no bounce buffer support for integrity metadata\n");
  115. return -EINVAL;
  116. }
  117. if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) {
  118. pr_warn("integrity support disabled.\n");
  119. return -EINVAL;
  120. }
  121. if (bi->csum_type == BLK_INTEGRITY_CSUM_NONE &&
  122. (bi->flags & BLK_INTEGRITY_REF_TAG)) {
  123. pr_warn("ref tag not support without checksum.\n");
  124. return -EINVAL;
  125. }
  126. if (!bi->interval_exp)
  127. bi->interval_exp = ilog2(lim->logical_block_size);
  128. return 0;
  129. }
  130. /*
  131. * Returns max guaranteed bytes which we can fit in a bio.
  132. *
  133. * We request that an atomic_write is ITER_UBUF iov_iter (so a single vector),
  134. * so we assume that we can fit in at least PAGE_SIZE in a segment, apart from
  135. * the first and last segments.
  136. */
  137. static unsigned int blk_queue_max_guaranteed_bio(struct queue_limits *lim)
  138. {
  139. unsigned int max_segments = min(BIO_MAX_VECS, lim->max_segments);
  140. unsigned int length;
  141. length = min(max_segments, 2) * lim->logical_block_size;
  142. if (max_segments > 2)
  143. length += (max_segments - 2) * PAGE_SIZE;
  144. return length;
  145. }
  146. static void blk_atomic_writes_update_limits(struct queue_limits *lim)
  147. {
  148. unsigned int unit_limit = min(lim->max_hw_sectors << SECTOR_SHIFT,
  149. blk_queue_max_guaranteed_bio(lim));
  150. unit_limit = rounddown_pow_of_two(unit_limit);
  151. lim->atomic_write_max_sectors =
  152. min(lim->atomic_write_hw_max >> SECTOR_SHIFT,
  153. lim->max_hw_sectors);
  154. lim->atomic_write_unit_min =
  155. min(lim->atomic_write_hw_unit_min, unit_limit);
  156. lim->atomic_write_unit_max =
  157. min(lim->atomic_write_hw_unit_max, unit_limit);
  158. lim->atomic_write_boundary_sectors =
  159. lim->atomic_write_hw_boundary >> SECTOR_SHIFT;
  160. }
  161. static void blk_validate_atomic_write_limits(struct queue_limits *lim)
  162. {
  163. unsigned int boundary_sectors;
  164. if (!lim->atomic_write_hw_max)
  165. goto unsupported;
  166. boundary_sectors = lim->atomic_write_hw_boundary >> SECTOR_SHIFT;
  167. if (boundary_sectors) {
  168. /*
  169. * A feature of boundary support is that it disallows bios to
  170. * be merged which would result in a merged request which
  171. * crosses either a chunk sector or atomic write HW boundary,
  172. * even though chunk sectors may be just set for performance.
  173. * For simplicity, disallow atomic writes for a chunk sector
  174. * which is non-zero and smaller than atomic write HW boundary.
  175. * Furthermore, chunk sectors must be a multiple of atomic
  176. * write HW boundary. Otherwise boundary support becomes
  177. * complicated.
  178. * Devices which do not conform to these rules can be dealt
  179. * with if and when they show up.
  180. */
  181. if (WARN_ON_ONCE(lim->chunk_sectors % boundary_sectors))
  182. goto unsupported;
  183. /*
  184. * The boundary size just needs to be a multiple of unit_max
  185. * (and not necessarily a power-of-2), so this following check
  186. * could be relaxed in future.
  187. * Furthermore, if needed, unit_max could even be reduced so
  188. * that it is compliant with a !power-of-2 boundary.
  189. */
  190. if (!is_power_of_2(boundary_sectors))
  191. goto unsupported;
  192. }
  193. blk_atomic_writes_update_limits(lim);
  194. return;
  195. unsupported:
  196. lim->atomic_write_max_sectors = 0;
  197. lim->atomic_write_boundary_sectors = 0;
  198. lim->atomic_write_unit_min = 0;
  199. lim->atomic_write_unit_max = 0;
  200. }
  201. /*
  202. * Check that the limits in lim are valid, initialize defaults for unset
  203. * values, and cap values based on others where needed.
  204. */
  205. static int blk_validate_limits(struct queue_limits *lim)
  206. {
  207. unsigned int max_hw_sectors;
  208. unsigned int logical_block_sectors;
  209. int err;
  210. /*
  211. * Unless otherwise specified, default to 512 byte logical blocks and a
  212. * physical block size equal to the logical block size.
  213. */
  214. if (!lim->logical_block_size)
  215. lim->logical_block_size = SECTOR_SIZE;
  216. else if (blk_validate_block_size(lim->logical_block_size)) {
  217. pr_warn("Invalid logical block size (%d)\n", lim->logical_block_size);
  218. return -EINVAL;
  219. }
  220. if (lim->physical_block_size < lim->logical_block_size)
  221. lim->physical_block_size = lim->logical_block_size;
  222. /*
  223. * The minimum I/O size defaults to the physical block size unless
  224. * explicitly overridden.
  225. */
  226. if (lim->io_min < lim->physical_block_size)
  227. lim->io_min = lim->physical_block_size;
  228. /*
  229. * The optimal I/O size may not be aligned to physical block size
  230. * (because it may be limited by dma engines which have no clue about
  231. * block size of the disks attached to them), so we round it down here.
  232. */
  233. lim->io_opt = round_down(lim->io_opt, lim->physical_block_size);
  234. /*
  235. * max_hw_sectors has a somewhat weird default for historical reason,
  236. * but driver really should set their own instead of relying on this
  237. * value.
  238. *
  239. * The block layer relies on the fact that every driver can
  240. * handle at lest a page worth of data per I/O, and needs the value
  241. * aligned to the logical block size.
  242. */
  243. if (!lim->max_hw_sectors)
  244. lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS;
  245. if (WARN_ON_ONCE(lim->max_hw_sectors < PAGE_SECTORS))
  246. return -EINVAL;
  247. logical_block_sectors = lim->logical_block_size >> SECTOR_SHIFT;
  248. if (WARN_ON_ONCE(logical_block_sectors > lim->max_hw_sectors))
  249. return -EINVAL;
  250. lim->max_hw_sectors = round_down(lim->max_hw_sectors,
  251. logical_block_sectors);
  252. /*
  253. * The actual max_sectors value is a complex beast and also takes the
  254. * max_dev_sectors value (set by SCSI ULPs) and a user configurable
  255. * value into account. The ->max_sectors value is always calculated
  256. * from these, so directly setting it won't have any effect.
  257. */
  258. max_hw_sectors = min_not_zero(lim->max_hw_sectors,
  259. lim->max_dev_sectors);
  260. if (lim->max_user_sectors) {
  261. if (lim->max_user_sectors < PAGE_SIZE / SECTOR_SIZE)
  262. return -EINVAL;
  263. lim->max_sectors = min(max_hw_sectors, lim->max_user_sectors);
  264. } else if (lim->io_opt > (BLK_DEF_MAX_SECTORS_CAP << SECTOR_SHIFT)) {
  265. lim->max_sectors =
  266. min(max_hw_sectors, lim->io_opt >> SECTOR_SHIFT);
  267. } else if (lim->io_min > (BLK_DEF_MAX_SECTORS_CAP << SECTOR_SHIFT)) {
  268. lim->max_sectors =
  269. min(max_hw_sectors, lim->io_min >> SECTOR_SHIFT);
  270. } else {
  271. lim->max_sectors = min(max_hw_sectors, BLK_DEF_MAX_SECTORS_CAP);
  272. }
  273. lim->max_sectors = round_down(lim->max_sectors,
  274. logical_block_sectors);
  275. /*
  276. * Random default for the maximum number of segments. Driver should not
  277. * rely on this and set their own.
  278. */
  279. if (!lim->max_segments)
  280. lim->max_segments = BLK_MAX_SEGMENTS;
  281. lim->max_discard_sectors =
  282. min(lim->max_hw_discard_sectors, lim->max_user_discard_sectors);
  283. /*
  284. * When discard is not supported, discard_granularity should be reported
  285. * as 0 to userspace.
  286. */
  287. if (lim->max_discard_sectors)
  288. lim->discard_granularity =
  289. max(lim->discard_granularity, lim->physical_block_size);
  290. else
  291. lim->discard_granularity = 0;
  292. if (!lim->max_discard_segments)
  293. lim->max_discard_segments = 1;
  294. /*
  295. * By default there is no limit on the segment boundary alignment,
  296. * but if there is one it can't be smaller than the page size as
  297. * that would break all the normal I/O patterns.
  298. */
  299. if (!lim->seg_boundary_mask)
  300. lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
  301. if (WARN_ON_ONCE(lim->seg_boundary_mask < PAGE_SIZE - 1))
  302. return -EINVAL;
  303. /*
  304. * Stacking device may have both virtual boundary and max segment
  305. * size limit, so allow this setting now, and long-term the two
  306. * might need to move out of stacking limits since we have immutable
  307. * bvec and lower layer bio splitting is supposed to handle the two
  308. * correctly.
  309. */
  310. if (lim->virt_boundary_mask) {
  311. if (!lim->max_segment_size)
  312. lim->max_segment_size = UINT_MAX;
  313. } else {
  314. /*
  315. * The maximum segment size has an odd historic 64k default that
  316. * drivers probably should override. Just like the I/O size we
  317. * require drivers to at least handle a full page per segment.
  318. */
  319. if (!lim->max_segment_size)
  320. lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
  321. if (WARN_ON_ONCE(lim->max_segment_size < PAGE_SIZE))
  322. return -EINVAL;
  323. }
  324. /*
  325. * We require drivers to at least do logical block aligned I/O, but
  326. * historically could not check for that due to the separate calls
  327. * to set the limits. Once the transition is finished the check
  328. * below should be narrowed down to check the logical block size.
  329. */
  330. if (!lim->dma_alignment)
  331. lim->dma_alignment = SECTOR_SIZE - 1;
  332. if (WARN_ON_ONCE(lim->dma_alignment > PAGE_SIZE))
  333. return -EINVAL;
  334. if (lim->alignment_offset) {
  335. lim->alignment_offset &= (lim->physical_block_size - 1);
  336. lim->flags &= ~BLK_FLAG_MISALIGNED;
  337. }
  338. if (!(lim->features & BLK_FEAT_WRITE_CACHE))
  339. lim->features &= ~BLK_FEAT_FUA;
  340. blk_validate_atomic_write_limits(lim);
  341. err = blk_validate_integrity_limits(lim);
  342. if (err)
  343. return err;
  344. return blk_validate_zoned_limits(lim);
  345. }
  346. /*
  347. * Set the default limits for a newly allocated queue. @lim contains the
  348. * initial limits set by the driver, which could be no limit in which case
  349. * all fields are cleared to zero.
  350. */
  351. int blk_set_default_limits(struct queue_limits *lim)
  352. {
  353. /*
  354. * Most defaults are set by capping the bounds in blk_validate_limits,
  355. * but max_user_discard_sectors is special and needs an explicit
  356. * initialization to the max value here.
  357. */
  358. lim->max_user_discard_sectors = UINT_MAX;
  359. return blk_validate_limits(lim);
  360. }
  361. /**
  362. * queue_limits_commit_update - commit an atomic update of queue limits
  363. * @q: queue to update
  364. * @lim: limits to apply
  365. *
  366. * Apply the limits in @lim that were obtained from queue_limits_start_update()
  367. * and updated by the caller to @q.
  368. *
  369. * Returns 0 if successful, else a negative error code.
  370. */
  371. int queue_limits_commit_update(struct request_queue *q,
  372. struct queue_limits *lim)
  373. {
  374. int error;
  375. error = blk_validate_limits(lim);
  376. if (error)
  377. goto out_unlock;
  378. #ifdef CONFIG_BLK_INLINE_ENCRYPTION
  379. if (q->crypto_profile && lim->integrity.tag_size) {
  380. pr_warn("blk-integrity: Integrity and hardware inline encryption are not supported together.\n");
  381. error = -EINVAL;
  382. goto out_unlock;
  383. }
  384. #endif
  385. q->limits = *lim;
  386. if (q->disk)
  387. blk_apply_bdi_limits(q->disk->bdi, lim);
  388. out_unlock:
  389. mutex_unlock(&q->limits_lock);
  390. return error;
  391. }
  392. EXPORT_SYMBOL_GPL(queue_limits_commit_update);
  393. /**
  394. * queue_limits_commit_update_frozen - commit an atomic update of queue limits
  395. * @q: queue to update
  396. * @lim: limits to apply
  397. *
  398. * Apply the limits in @lim that were obtained from queue_limits_start_update()
  399. * and updated with the new values by the caller to @q. Freezes the queue
  400. * before the update and unfreezes it after.
  401. *
  402. * Returns 0 if successful, else a negative error code.
  403. */
  404. int queue_limits_commit_update_frozen(struct request_queue *q,
  405. struct queue_limits *lim)
  406. {
  407. int ret;
  408. blk_mq_freeze_queue(q);
  409. ret = queue_limits_commit_update(q, lim);
  410. blk_mq_unfreeze_queue(q);
  411. return ret;
  412. }
  413. EXPORT_SYMBOL_GPL(queue_limits_commit_update_frozen);
  414. /**
  415. * queue_limits_set - apply queue limits to queue
  416. * @q: queue to update
  417. * @lim: limits to apply
  418. *
  419. * Apply the limits in @lim that were freshly initialized to @q.
  420. * To update existing limits use queue_limits_start_update() and
  421. * queue_limits_commit_update() instead.
  422. *
  423. * Returns 0 if successful, else a negative error code.
  424. */
  425. int queue_limits_set(struct request_queue *q, struct queue_limits *lim)
  426. {
  427. mutex_lock(&q->limits_lock);
  428. return queue_limits_commit_update(q, lim);
  429. }
  430. EXPORT_SYMBOL_GPL(queue_limits_set);
  431. static int queue_limit_alignment_offset(const struct queue_limits *lim,
  432. sector_t sector)
  433. {
  434. unsigned int granularity = max(lim->physical_block_size, lim->io_min);
  435. unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
  436. << SECTOR_SHIFT;
  437. return (granularity + lim->alignment_offset - alignment) % granularity;
  438. }
  439. static unsigned int queue_limit_discard_alignment(
  440. const struct queue_limits *lim, sector_t sector)
  441. {
  442. unsigned int alignment, granularity, offset;
  443. if (!lim->max_discard_sectors)
  444. return 0;
  445. /* Why are these in bytes, not sectors? */
  446. alignment = lim->discard_alignment >> SECTOR_SHIFT;
  447. granularity = lim->discard_granularity >> SECTOR_SHIFT;
  448. if (!granularity)
  449. return 0;
  450. /* Offset of the partition start in 'granularity' sectors */
  451. offset = sector_div(sector, granularity);
  452. /* And why do we do this modulus *again* in blkdev_issue_discard()? */
  453. offset = (granularity + alignment - offset) % granularity;
  454. /* Turn it back into bytes, gaah */
  455. return offset << SECTOR_SHIFT;
  456. }
  457. static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs)
  458. {
  459. sectors = round_down(sectors, lbs >> SECTOR_SHIFT);
  460. if (sectors < PAGE_SIZE >> SECTOR_SHIFT)
  461. sectors = PAGE_SIZE >> SECTOR_SHIFT;
  462. return sectors;
  463. }
  464. /**
  465. * blk_stack_limits - adjust queue_limits for stacked devices
  466. * @t: the stacking driver limits (top device)
  467. * @b: the underlying queue limits (bottom, component device)
  468. * @start: first data sector within component device
  469. *
  470. * Description:
  471. * This function is used by stacking drivers like MD and DM to ensure
  472. * that all component devices have compatible block sizes and
  473. * alignments. The stacking driver must provide a queue_limits
  474. * struct (top) and then iteratively call the stacking function for
  475. * all component (bottom) devices. The stacking function will
  476. * attempt to combine the values and ensure proper alignment.
  477. *
  478. * Returns 0 if the top and bottom queue_limits are compatible. The
  479. * top device's block sizes and alignment offsets may be adjusted to
  480. * ensure alignment with the bottom device. If no compatible sizes
  481. * and alignments exist, -1 is returned and the resulting top
  482. * queue_limits will have the misaligned flag set to indicate that
  483. * the alignment_offset is undefined.
  484. */
  485. int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
  486. sector_t start)
  487. {
  488. unsigned int top, bottom, alignment;
  489. int ret = 0;
  490. t->features |= (b->features & BLK_FEAT_INHERIT_MASK);
  491. /*
  492. * BLK_FEAT_NOWAIT and BLK_FEAT_POLL need to be supported both by the
  493. * stacking driver and all underlying devices. The stacking driver sets
  494. * the flags before stacking the limits, and this will clear the flags
  495. * if any of the underlying devices does not support it.
  496. */
  497. if (!(b->features & BLK_FEAT_NOWAIT))
  498. t->features &= ~BLK_FEAT_NOWAIT;
  499. if (!(b->features & BLK_FEAT_POLL))
  500. t->features &= ~BLK_FEAT_POLL;
  501. t->flags |= (b->flags & BLK_FLAG_MISALIGNED);
  502. t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
  503. t->max_user_sectors = min_not_zero(t->max_user_sectors,
  504. b->max_user_sectors);
  505. t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
  506. t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
  507. t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
  508. b->max_write_zeroes_sectors);
  509. t->max_zone_append_sectors = min(queue_limits_max_zone_append_sectors(t),
  510. queue_limits_max_zone_append_sectors(b));
  511. t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
  512. b->seg_boundary_mask);
  513. t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
  514. b->virt_boundary_mask);
  515. t->max_segments = min_not_zero(t->max_segments, b->max_segments);
  516. t->max_discard_segments = min_not_zero(t->max_discard_segments,
  517. b->max_discard_segments);
  518. t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
  519. b->max_integrity_segments);
  520. t->max_segment_size = min_not_zero(t->max_segment_size,
  521. b->max_segment_size);
  522. alignment = queue_limit_alignment_offset(b, start);
  523. /* Bottom device has different alignment. Check that it is
  524. * compatible with the current top alignment.
  525. */
  526. if (t->alignment_offset != alignment) {
  527. top = max(t->physical_block_size, t->io_min)
  528. + t->alignment_offset;
  529. bottom = max(b->physical_block_size, b->io_min) + alignment;
  530. /* Verify that top and bottom intervals line up */
  531. if (max(top, bottom) % min(top, bottom)) {
  532. t->flags |= BLK_FLAG_MISALIGNED;
  533. ret = -1;
  534. }
  535. }
  536. t->logical_block_size = max(t->logical_block_size,
  537. b->logical_block_size);
  538. t->physical_block_size = max(t->physical_block_size,
  539. b->physical_block_size);
  540. t->io_min = max(t->io_min, b->io_min);
  541. t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
  542. t->dma_alignment = max(t->dma_alignment, b->dma_alignment);
  543. /* Set non-power-of-2 compatible chunk_sectors boundary */
  544. if (b->chunk_sectors)
  545. t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors);
  546. /* Physical block size a multiple of the logical block size? */
  547. if (t->physical_block_size & (t->logical_block_size - 1)) {
  548. t->physical_block_size = t->logical_block_size;
  549. t->flags |= BLK_FLAG_MISALIGNED;
  550. ret = -1;
  551. }
  552. /* Minimum I/O a multiple of the physical block size? */
  553. if (t->io_min & (t->physical_block_size - 1)) {
  554. t->io_min = t->physical_block_size;
  555. t->flags |= BLK_FLAG_MISALIGNED;
  556. ret = -1;
  557. }
  558. /* Optimal I/O a multiple of the physical block size? */
  559. if (t->io_opt & (t->physical_block_size - 1)) {
  560. t->io_opt = 0;
  561. t->flags |= BLK_FLAG_MISALIGNED;
  562. ret = -1;
  563. }
  564. /* chunk_sectors a multiple of the physical block size? */
  565. if (t->chunk_sectors % (t->physical_block_size >> SECTOR_SHIFT)) {
  566. t->chunk_sectors = 0;
  567. t->flags |= BLK_FLAG_MISALIGNED;
  568. ret = -1;
  569. }
  570. /* Find lowest common alignment_offset */
  571. t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
  572. % max(t->physical_block_size, t->io_min);
  573. /* Verify that new alignment_offset is on a logical block boundary */
  574. if (t->alignment_offset & (t->logical_block_size - 1)) {
  575. t->flags |= BLK_FLAG_MISALIGNED;
  576. ret = -1;
  577. }
  578. t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size);
  579. t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size);
  580. t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size);
  581. /* Discard alignment and granularity */
  582. if (b->discard_granularity) {
  583. alignment = queue_limit_discard_alignment(b, start);
  584. t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
  585. b->max_discard_sectors);
  586. t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
  587. b->max_hw_discard_sectors);
  588. t->discard_granularity = max(t->discard_granularity,
  589. b->discard_granularity);
  590. t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
  591. t->discard_granularity;
  592. }
  593. t->max_secure_erase_sectors = min_not_zero(t->max_secure_erase_sectors,
  594. b->max_secure_erase_sectors);
  595. t->zone_write_granularity = max(t->zone_write_granularity,
  596. b->zone_write_granularity);
  597. if (!(t->features & BLK_FEAT_ZONED)) {
  598. t->zone_write_granularity = 0;
  599. t->max_zone_append_sectors = 0;
  600. }
  601. return ret;
  602. }
  603. EXPORT_SYMBOL(blk_stack_limits);
  604. /**
  605. * queue_limits_stack_bdev - adjust queue_limits for stacked devices
  606. * @t: the stacking driver limits (top device)
  607. * @bdev: the underlying block device (bottom)
  608. * @offset: offset to beginning of data within component device
  609. * @pfx: prefix to use for warnings logged
  610. *
  611. * Description:
  612. * This function is used by stacking drivers like MD and DM to ensure
  613. * that all component devices have compatible block sizes and
  614. * alignments. The stacking driver must provide a queue_limits
  615. * struct (top) and then iteratively call the stacking function for
  616. * all component (bottom) devices. The stacking function will
  617. * attempt to combine the values and ensure proper alignment.
  618. */
  619. void queue_limits_stack_bdev(struct queue_limits *t, struct block_device *bdev,
  620. sector_t offset, const char *pfx)
  621. {
  622. if (blk_stack_limits(t, &bdev_get_queue(bdev)->limits,
  623. get_start_sect(bdev) + offset))
  624. pr_notice("%s: Warning: Device %pg is misaligned\n",
  625. pfx, bdev);
  626. }
  627. EXPORT_SYMBOL_GPL(queue_limits_stack_bdev);
  628. /**
  629. * queue_limits_stack_integrity - stack integrity profile
  630. * @t: target queue limits
  631. * @b: base queue limits
  632. *
  633. * Check if the integrity profile in the @b can be stacked into the
  634. * target @t. Stacking is possible if either:
  635. *
  636. * a) does not have any integrity information stacked into it yet
  637. * b) the integrity profile in @b is identical to the one in @t
  638. *
  639. * If @b can be stacked into @t, return %true. Else return %false and clear the
  640. * integrity information in @t.
  641. */
  642. bool queue_limits_stack_integrity(struct queue_limits *t,
  643. struct queue_limits *b)
  644. {
  645. struct blk_integrity *ti = &t->integrity;
  646. struct blk_integrity *bi = &b->integrity;
  647. if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
  648. return true;
  649. if (!ti->tuple_size) {
  650. /* inherit the settings from the first underlying device */
  651. if (!(ti->flags & BLK_INTEGRITY_STACKED)) {
  652. ti->flags = BLK_INTEGRITY_DEVICE_CAPABLE |
  653. (bi->flags & BLK_INTEGRITY_REF_TAG);
  654. ti->csum_type = bi->csum_type;
  655. ti->tuple_size = bi->tuple_size;
  656. ti->pi_offset = bi->pi_offset;
  657. ti->interval_exp = bi->interval_exp;
  658. ti->tag_size = bi->tag_size;
  659. goto done;
  660. }
  661. if (!bi->tuple_size)
  662. goto done;
  663. }
  664. if (ti->tuple_size != bi->tuple_size)
  665. goto incompatible;
  666. if (ti->interval_exp != bi->interval_exp)
  667. goto incompatible;
  668. if (ti->tag_size != bi->tag_size)
  669. goto incompatible;
  670. if (ti->csum_type != bi->csum_type)
  671. goto incompatible;
  672. if ((ti->flags & BLK_INTEGRITY_REF_TAG) !=
  673. (bi->flags & BLK_INTEGRITY_REF_TAG))
  674. goto incompatible;
  675. done:
  676. ti->flags |= BLK_INTEGRITY_STACKED;
  677. return true;
  678. incompatible:
  679. memset(ti, 0, sizeof(*ti));
  680. return false;
  681. }
  682. EXPORT_SYMBOL_GPL(queue_limits_stack_integrity);
  683. /**
  684. * blk_set_queue_depth - tell the block layer about the device queue depth
  685. * @q: the request queue for the device
  686. * @depth: queue depth
  687. *
  688. */
  689. void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
  690. {
  691. q->queue_depth = depth;
  692. rq_qos_queue_depth_changed(q);
  693. }
  694. EXPORT_SYMBOL(blk_set_queue_depth);
  695. int bdev_alignment_offset(struct block_device *bdev)
  696. {
  697. struct request_queue *q = bdev_get_queue(bdev);
  698. if (q->limits.flags & BLK_FLAG_MISALIGNED)
  699. return -1;
  700. if (bdev_is_partition(bdev))
  701. return queue_limit_alignment_offset(&q->limits,
  702. bdev->bd_start_sect);
  703. return q->limits.alignment_offset;
  704. }
  705. EXPORT_SYMBOL_GPL(bdev_alignment_offset);
  706. unsigned int bdev_discard_alignment(struct block_device *bdev)
  707. {
  708. struct request_queue *q = bdev_get_queue(bdev);
  709. if (bdev_is_partition(bdev))
  710. return queue_limit_discard_alignment(&q->limits,
  711. bdev->bd_start_sect);
  712. return q->limits.discard_alignment;
  713. }
  714. EXPORT_SYMBOL_GPL(bdev_discard_alignment);