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