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