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