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