xref: /linux/block/blk-settings.c (revision fdb9aed869f34d776298b3a8197909eb820e4d0d)
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 	error = blk_validate_limits(lim);
550 	if (error)
551 		goto out_unlock;
552 
553 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
554 	if (q->crypto_profile && lim->integrity.tag_size) {
555 		pr_warn("blk-integrity: Integrity and hardware inline encryption are not supported together.\n");
556 		error = -EINVAL;
557 		goto out_unlock;
558 	}
559 #endif
560 
561 	q->limits = *lim;
562 	if (q->disk)
563 		blk_apply_bdi_limits(q->disk->bdi, lim);
564 out_unlock:
565 	mutex_unlock(&q->limits_lock);
566 	return error;
567 }
568 EXPORT_SYMBOL_GPL(queue_limits_commit_update);
569 
570 /**
571  * queue_limits_commit_update_frozen - commit an atomic update of queue limits
572  * @q:		queue to update
573  * @lim:	limits to apply
574  *
575  * Apply the limits in @lim that were obtained from queue_limits_start_update()
576  * and updated with the new values by the caller to @q.  Freezes the queue
577  * before the update and unfreezes it after.
578  *
579  * Returns 0 if successful, else a negative error code.
580  */
581 int queue_limits_commit_update_frozen(struct request_queue *q,
582 		struct queue_limits *lim)
583 {
584 	unsigned int memflags;
585 	int ret;
586 
587 	memflags = blk_mq_freeze_queue(q);
588 	ret = queue_limits_commit_update(q, lim);
589 	blk_mq_unfreeze_queue(q, memflags);
590 
591 	return ret;
592 }
593 EXPORT_SYMBOL_GPL(queue_limits_commit_update_frozen);
594 
595 /**
596  * queue_limits_set - apply queue limits to queue
597  * @q:		queue to update
598  * @lim:	limits to apply
599  *
600  * Apply the limits in @lim that were freshly initialized to @q.
601  * To update existing limits use queue_limits_start_update() and
602  * queue_limits_commit_update() instead.
603  *
604  * Returns 0 if successful, else a negative error code.
605  */
606 int queue_limits_set(struct request_queue *q, struct queue_limits *lim)
607 {
608 	mutex_lock(&q->limits_lock);
609 	return queue_limits_commit_update(q, lim);
610 }
611 EXPORT_SYMBOL_GPL(queue_limits_set);
612 
613 static int queue_limit_alignment_offset(const struct queue_limits *lim,
614 		sector_t sector)
615 {
616 	unsigned int granularity = max(lim->physical_block_size, lim->io_min);
617 	unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
618 		<< SECTOR_SHIFT;
619 
620 	return (granularity + lim->alignment_offset - alignment) % granularity;
621 }
622 
623 static unsigned int queue_limit_discard_alignment(
624 		const struct queue_limits *lim, sector_t sector)
625 {
626 	unsigned int alignment, granularity, offset;
627 
628 	if (!lim->max_discard_sectors)
629 		return 0;
630 
631 	/* Why are these in bytes, not sectors? */
632 	alignment = lim->discard_alignment >> SECTOR_SHIFT;
633 	granularity = lim->discard_granularity >> SECTOR_SHIFT;
634 
635 	/* Offset of the partition start in 'granularity' sectors */
636 	offset = sector_div(sector, granularity);
637 
638 	/* And why do we do this modulus *again* in blkdev_issue_discard()? */
639 	offset = (granularity + alignment - offset) % granularity;
640 
641 	/* Turn it back into bytes, gaah */
642 	return offset << SECTOR_SHIFT;
643 }
644 
645 static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs)
646 {
647 	sectors = round_down(sectors, lbs >> SECTOR_SHIFT);
648 	if (sectors < PAGE_SIZE >> SECTOR_SHIFT)
649 		sectors = PAGE_SIZE >> SECTOR_SHIFT;
650 	return sectors;
651 }
652 
653 /* Check if second and later bottom devices are compliant */
654 static bool blk_stack_atomic_writes_tail(struct queue_limits *t,
655 				struct queue_limits *b)
656 {
657 	/* We're not going to support different boundary sizes.. yet */
658 	if (t->atomic_write_hw_boundary != b->atomic_write_hw_boundary)
659 		return false;
660 
661 	/* Can't support this */
662 	if (t->atomic_write_hw_unit_min > b->atomic_write_hw_unit_max)
663 		return false;
664 
665 	/* Or this */
666 	if (t->atomic_write_hw_unit_max < b->atomic_write_hw_unit_min)
667 		return false;
668 
669 	t->atomic_write_hw_max = min(t->atomic_write_hw_max,
670 				b->atomic_write_hw_max);
671 	t->atomic_write_hw_unit_min = max(t->atomic_write_hw_unit_min,
672 				b->atomic_write_hw_unit_min);
673 	t->atomic_write_hw_unit_max = min(t->atomic_write_hw_unit_max,
674 				b->atomic_write_hw_unit_max);
675 	return true;
676 }
677 
678 static void blk_stack_atomic_writes_chunk_sectors(struct queue_limits *t)
679 {
680 	unsigned int chunk_bytes;
681 
682 	if (!t->chunk_sectors)
683 		return;
684 
685 	/*
686 	 * If chunk sectors is so large that its value in bytes overflows
687 	 * UINT_MAX, then just shift it down so it definitely will fit.
688 	 * We don't support atomic writes of such a large size anyway.
689 	 */
690 	if (check_shl_overflow(t->chunk_sectors, SECTOR_SHIFT, &chunk_bytes))
691 		chunk_bytes = t->chunk_sectors;
692 
693 	/*
694 	 * Find values for limits which work for chunk size.
695 	 * b->atomic_write_hw_unit_{min, max} may not be aligned with chunk
696 	 * size, as the chunk size is not restricted to a power-of-2.
697 	 * So we need to find highest power-of-2 which works for the chunk
698 	 * size.
699 	 * As an example scenario, we could have t->unit_max = 16K and
700 	 * t->chunk_sectors = 24KB. For this case, reduce t->unit_max to a
701 	 * value aligned with both limits, i.e. 8K in this example.
702 	 */
703 	t->atomic_write_hw_unit_max = min(t->atomic_write_hw_unit_max,
704 					max_pow_of_two_factor(chunk_bytes));
705 
706 	t->atomic_write_hw_unit_min = min(t->atomic_write_hw_unit_min,
707 					  t->atomic_write_hw_unit_max);
708 	t->atomic_write_hw_max = min(t->atomic_write_hw_max, chunk_bytes);
709 }
710 
711 /* Check stacking of first bottom device */
712 static bool blk_stack_atomic_writes_head(struct queue_limits *t,
713 				struct queue_limits *b)
714 {
715 	if (!blk_valid_atomic_writes_boundary(t->chunk_sectors,
716 			b->atomic_write_hw_boundary >> SECTOR_SHIFT))
717 		return false;
718 
719 	t->atomic_write_hw_unit_max = b->atomic_write_hw_unit_max;
720 	t->atomic_write_hw_unit_min = b->atomic_write_hw_unit_min;
721 	t->atomic_write_hw_max = b->atomic_write_hw_max;
722 	t->atomic_write_hw_boundary = b->atomic_write_hw_boundary;
723 	return true;
724 }
725 
726 static void blk_stack_atomic_writes_limits(struct queue_limits *t,
727 				struct queue_limits *b, sector_t start)
728 {
729 	if (!(b->features & BLK_FEAT_ATOMIC_WRITES))
730 		goto unsupported;
731 
732 	if (!b->atomic_write_hw_unit_min)
733 		goto unsupported;
734 
735 	if (!blk_atomic_write_start_sect_aligned(start, b))
736 		goto unsupported;
737 
738 	/* UINT_MAX indicates no stacking of bottom devices yet */
739 	if (t->atomic_write_hw_max == UINT_MAX) {
740 		if (!blk_stack_atomic_writes_head(t, b))
741 			goto unsupported;
742 	} else {
743 		if (!blk_stack_atomic_writes_tail(t, b))
744 			goto unsupported;
745 	}
746 	blk_stack_atomic_writes_chunk_sectors(t);
747 	return;
748 
749 unsupported:
750 	t->atomic_write_hw_max = 0;
751 	t->atomic_write_hw_unit_max = 0;
752 	t->atomic_write_hw_unit_min = 0;
753 	t->atomic_write_hw_boundary = 0;
754 }
755 
756 /**
757  * blk_stack_limits - adjust queue_limits for stacked devices
758  * @t:	the stacking driver limits (top device)
759  * @b:  the underlying queue limits (bottom, component device)
760  * @start:  first data sector within component device
761  *
762  * Description:
763  *    This function is used by stacking drivers like MD and DM to ensure
764  *    that all component devices have compatible block sizes and
765  *    alignments.  The stacking driver must provide a queue_limits
766  *    struct (top) and then iteratively call the stacking function for
767  *    all component (bottom) devices.  The stacking function will
768  *    attempt to combine the values and ensure proper alignment.
769  *
770  *    Returns 0 if the top and bottom queue_limits are compatible.  The
771  *    top device's block sizes and alignment offsets may be adjusted to
772  *    ensure alignment with the bottom device. If no compatible sizes
773  *    and alignments exist, -1 is returned and the resulting top
774  *    queue_limits will have the misaligned flag set to indicate that
775  *    the alignment_offset is undefined.
776  */
777 int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
778 		     sector_t start)
779 {
780 	unsigned int top, bottom, alignment;
781 	int ret = 0;
782 
783 	t->features |= (b->features & BLK_FEAT_INHERIT_MASK);
784 
785 	/*
786 	 * Some feaures need to be supported both by the stacking driver and all
787 	 * underlying devices.  The stacking driver sets these flags before
788 	 * stacking the limits, and this will clear the flags if any of the
789 	 * underlying devices does not support it.
790 	 */
791 	if (!(b->features & BLK_FEAT_NOWAIT))
792 		t->features &= ~BLK_FEAT_NOWAIT;
793 	if (!(b->features & BLK_FEAT_POLL))
794 		t->features &= ~BLK_FEAT_POLL;
795 
796 	t->flags |= (b->flags & BLK_FLAG_MISALIGNED);
797 
798 	t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
799 	t->max_user_sectors = min_not_zero(t->max_user_sectors,
800 			b->max_user_sectors);
801 	t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
802 	t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
803 	t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
804 					b->max_write_zeroes_sectors);
805 	t->max_user_wzeroes_unmap_sectors =
806 			min(t->max_user_wzeroes_unmap_sectors,
807 			    b->max_user_wzeroes_unmap_sectors);
808 	t->max_hw_wzeroes_unmap_sectors =
809 			min(t->max_hw_wzeroes_unmap_sectors,
810 			    b->max_hw_wzeroes_unmap_sectors);
811 
812 	t->max_hw_zone_append_sectors = min(t->max_hw_zone_append_sectors,
813 					b->max_hw_zone_append_sectors);
814 
815 	t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
816 					    b->seg_boundary_mask);
817 	t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
818 					    b->virt_boundary_mask);
819 
820 	t->max_segments = min_not_zero(t->max_segments, b->max_segments);
821 	t->max_discard_segments = min_not_zero(t->max_discard_segments,
822 					       b->max_discard_segments);
823 	t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
824 						 b->max_integrity_segments);
825 
826 	t->max_segment_size = min_not_zero(t->max_segment_size,
827 					   b->max_segment_size);
828 
829 	alignment = queue_limit_alignment_offset(b, start);
830 
831 	/* Bottom device has different alignment.  Check that it is
832 	 * compatible with the current top alignment.
833 	 */
834 	if (t->alignment_offset != alignment) {
835 
836 		top = max(t->physical_block_size, t->io_min)
837 			+ t->alignment_offset;
838 		bottom = max(b->physical_block_size, b->io_min) + alignment;
839 
840 		/* Verify that top and bottom intervals line up */
841 		if (max(top, bottom) % min(top, bottom)) {
842 			t->flags |= BLK_FLAG_MISALIGNED;
843 			ret = -1;
844 		}
845 	}
846 
847 	t->logical_block_size = max(t->logical_block_size,
848 				    b->logical_block_size);
849 
850 	t->physical_block_size = max(t->physical_block_size,
851 				     b->physical_block_size);
852 
853 	t->io_min = max(t->io_min, b->io_min);
854 	t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
855 	t->dma_alignment = max(t->dma_alignment, b->dma_alignment);
856 
857 	/* Set non-power-of-2 compatible chunk_sectors boundary */
858 	if (b->chunk_sectors)
859 		t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors);
860 
861 	/* Physical block size a multiple of the logical block size? */
862 	if (t->physical_block_size & (t->logical_block_size - 1)) {
863 		t->physical_block_size = t->logical_block_size;
864 		t->flags |= BLK_FLAG_MISALIGNED;
865 		ret = -1;
866 	}
867 
868 	/* Minimum I/O a multiple of the physical block size? */
869 	if (t->io_min & (t->physical_block_size - 1)) {
870 		t->io_min = t->physical_block_size;
871 		t->flags |= BLK_FLAG_MISALIGNED;
872 		ret = -1;
873 	}
874 
875 	/* Optimal I/O a multiple of the physical block size? */
876 	if (t->io_opt & (t->physical_block_size - 1)) {
877 		t->io_opt = 0;
878 		t->flags |= BLK_FLAG_MISALIGNED;
879 		ret = -1;
880 	}
881 
882 	/* chunk_sectors a multiple of the physical block size? */
883 	if (t->chunk_sectors % (t->physical_block_size >> SECTOR_SHIFT)) {
884 		t->chunk_sectors = 0;
885 		t->flags |= BLK_FLAG_MISALIGNED;
886 		ret = -1;
887 	}
888 
889 	/* Find lowest common alignment_offset */
890 	t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
891 		% max(t->physical_block_size, t->io_min);
892 
893 	/* Verify that new alignment_offset is on a logical block boundary */
894 	if (t->alignment_offset & (t->logical_block_size - 1)) {
895 		t->flags |= BLK_FLAG_MISALIGNED;
896 		ret = -1;
897 	}
898 
899 	t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size);
900 	t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size);
901 	t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size);
902 
903 	/* Discard alignment and granularity */
904 	if (b->discard_granularity) {
905 		alignment = queue_limit_discard_alignment(b, start);
906 
907 		t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
908 						      b->max_discard_sectors);
909 		t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
910 							 b->max_hw_discard_sectors);
911 		t->discard_granularity = max(t->discard_granularity,
912 					     b->discard_granularity);
913 		t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
914 			t->discard_granularity;
915 	}
916 	t->max_secure_erase_sectors = min_not_zero(t->max_secure_erase_sectors,
917 						   b->max_secure_erase_sectors);
918 	t->zone_write_granularity = max(t->zone_write_granularity,
919 					b->zone_write_granularity);
920 	if (!(t->features & BLK_FEAT_ZONED)) {
921 		t->zone_write_granularity = 0;
922 		t->max_zone_append_sectors = 0;
923 	}
924 	blk_stack_atomic_writes_limits(t, b, start);
925 
926 	return ret;
927 }
928 EXPORT_SYMBOL(blk_stack_limits);
929 
930 /**
931  * queue_limits_stack_bdev - adjust queue_limits for stacked devices
932  * @t:	the stacking driver limits (top device)
933  * @bdev:  the underlying block device (bottom)
934  * @offset:  offset to beginning of data within component device
935  * @pfx: prefix to use for warnings logged
936  *
937  * Description:
938  *    This function is used by stacking drivers like MD and DM to ensure
939  *    that all component devices have compatible block sizes and
940  *    alignments.  The stacking driver must provide a queue_limits
941  *    struct (top) and then iteratively call the stacking function for
942  *    all component (bottom) devices.  The stacking function will
943  *    attempt to combine the values and ensure proper alignment.
944  */
945 void queue_limits_stack_bdev(struct queue_limits *t, struct block_device *bdev,
946 		sector_t offset, const char *pfx)
947 {
948 	if (blk_stack_limits(t, bdev_limits(bdev),
949 			get_start_sect(bdev) + offset))
950 		pr_notice("%s: Warning: Device %pg is misaligned\n",
951 			pfx, bdev);
952 }
953 EXPORT_SYMBOL_GPL(queue_limits_stack_bdev);
954 
955 /**
956  * queue_limits_stack_integrity - stack integrity profile
957  * @t: target queue limits
958  * @b: base queue limits
959  *
960  * Check if the integrity profile in the @b can be stacked into the
961  * target @t.  Stacking is possible if either:
962  *
963  *   a) does not have any integrity information stacked into it yet
964  *   b) the integrity profile in @b is identical to the one in @t
965  *
966  * If @b can be stacked into @t, return %true.  Else return %false and clear the
967  * integrity information in @t.
968  */
969 bool queue_limits_stack_integrity(struct queue_limits *t,
970 		struct queue_limits *b)
971 {
972 	struct blk_integrity *ti = &t->integrity;
973 	struct blk_integrity *bi = &b->integrity;
974 
975 	if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
976 		return true;
977 
978 	if (ti->flags & BLK_INTEGRITY_STACKED) {
979 		if (ti->metadata_size != bi->metadata_size)
980 			goto incompatible;
981 		if (ti->interval_exp != bi->interval_exp)
982 			goto incompatible;
983 		if (ti->tag_size != bi->tag_size)
984 			goto incompatible;
985 		if (ti->csum_type != bi->csum_type)
986 			goto incompatible;
987 		if (ti->pi_tuple_size != bi->pi_tuple_size)
988 			goto incompatible;
989 		if ((ti->flags & BLK_INTEGRITY_REF_TAG) !=
990 		    (bi->flags & BLK_INTEGRITY_REF_TAG))
991 			goto incompatible;
992 	} else {
993 		ti->flags = BLK_INTEGRITY_STACKED;
994 		ti->flags |= (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) |
995 			     (bi->flags & BLK_INTEGRITY_REF_TAG);
996 		ti->csum_type = bi->csum_type;
997 		ti->pi_tuple_size = bi->pi_tuple_size;
998 		ti->metadata_size = bi->metadata_size;
999 		ti->pi_offset = bi->pi_offset;
1000 		ti->interval_exp = bi->interval_exp;
1001 		ti->tag_size = bi->tag_size;
1002 	}
1003 	return true;
1004 
1005 incompatible:
1006 	memset(ti, 0, sizeof(*ti));
1007 	return false;
1008 }
1009 EXPORT_SYMBOL_GPL(queue_limits_stack_integrity);
1010 
1011 /**
1012  * blk_set_queue_depth - tell the block layer about the device queue depth
1013  * @q:		the request queue for the device
1014  * @depth:		queue depth
1015  *
1016  */
1017 void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
1018 {
1019 	q->queue_depth = depth;
1020 	rq_qos_queue_depth_changed(q);
1021 }
1022 EXPORT_SYMBOL(blk_set_queue_depth);
1023 
1024 int bdev_alignment_offset(struct block_device *bdev)
1025 {
1026 	struct request_queue *q = bdev_get_queue(bdev);
1027 
1028 	if (q->limits.flags & BLK_FLAG_MISALIGNED)
1029 		return -1;
1030 	if (bdev_is_partition(bdev))
1031 		return queue_limit_alignment_offset(&q->limits,
1032 				bdev->bd_start_sect);
1033 	return q->limits.alignment_offset;
1034 }
1035 EXPORT_SYMBOL_GPL(bdev_alignment_offset);
1036 
1037 unsigned int bdev_discard_alignment(struct block_device *bdev)
1038 {
1039 	struct request_queue *q = bdev_get_queue(bdev);
1040 
1041 	if (bdev_is_partition(bdev))
1042 		return queue_limit_discard_alignment(&q->limits,
1043 				bdev->bd_start_sect);
1044 	return q->limits.discard_alignment;
1045 }
1046 EXPORT_SYMBOL_GPL(bdev_discard_alignment);
1047