1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * bio-integrity.c - bio data integrity extensions
4  *
5  * Copyright (C) 2007, 2008, 2009 Oracle Corporation
6  * Written by: Martin K. Petersen <[email protected]>
7  */
8 
9 #include <linux/blk-integrity.h>
10 #include <linux/mempool.h>
11 #include <linux/export.h>
12 #include <linux/bio.h>
13 #include <linux/workqueue.h>
14 #include <linux/slab.h>
15 #include "blk.h"
16 
17 static struct kmem_cache *bip_slab;
18 static struct workqueue_struct *kintegrityd_wq;
19 
blk_flush_integrity(void)20 void blk_flush_integrity(void)
21 {
22 	flush_workqueue(kintegrityd_wq);
23 }
24 
25 /**
26  * bio_integrity_free - Free bio integrity payload
27  * @bio:	bio containing bip to be freed
28  *
29  * Description: Free the integrity portion of a bio.
30  */
bio_integrity_free(struct bio * bio)31 void bio_integrity_free(struct bio *bio)
32 {
33 	struct bio_integrity_payload *bip = bio_integrity(bio);
34 	struct bio_set *bs = bio->bi_pool;
35 
36 	if (bs && mempool_initialized(&bs->bio_integrity_pool)) {
37 		if (bip->bip_vec)
38 			bvec_free(&bs->bvec_integrity_pool, bip->bip_vec,
39 				  bip->bip_max_vcnt);
40 		mempool_free(bip, &bs->bio_integrity_pool);
41 	} else {
42 		kfree(bip);
43 	}
44 	bio->bi_integrity = NULL;
45 	bio->bi_opf &= ~REQ_INTEGRITY;
46 }
47 
48 /**
49  * bio_integrity_alloc - Allocate integrity payload and attach it to bio
50  * @bio:	bio to attach integrity metadata to
51  * @gfp_mask:	Memory allocation mask
52  * @nr_vecs:	Number of integrity metadata scatter-gather elements
53  *
54  * Description: This function prepares a bio for attaching integrity
55  * metadata.  nr_vecs specifies the maximum number of pages containing
56  * integrity metadata that can be attached.
57  */
bio_integrity_alloc(struct bio * bio,gfp_t gfp_mask,unsigned int nr_vecs)58 struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
59 						  gfp_t gfp_mask,
60 						  unsigned int nr_vecs)
61 {
62 	struct bio_integrity_payload *bip;
63 	struct bio_set *bs = bio->bi_pool;
64 	unsigned inline_vecs;
65 
66 	if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
67 		return ERR_PTR(-EOPNOTSUPP);
68 
69 	if (!bs || !mempool_initialized(&bs->bio_integrity_pool)) {
70 		bip = kmalloc(struct_size(bip, bip_inline_vecs, nr_vecs), gfp_mask);
71 		inline_vecs = nr_vecs;
72 	} else {
73 		bip = mempool_alloc(&bs->bio_integrity_pool, gfp_mask);
74 		inline_vecs = BIO_INLINE_VECS;
75 	}
76 
77 	if (unlikely(!bip))
78 		return ERR_PTR(-ENOMEM);
79 
80 	memset(bip, 0, sizeof(*bip));
81 
82 	/* always report as many vecs as asked explicitly, not inline vecs */
83 	bip->bip_max_vcnt = nr_vecs;
84 	if (nr_vecs > inline_vecs) {
85 		bip->bip_vec = bvec_alloc(&bs->bvec_integrity_pool,
86 					  &bip->bip_max_vcnt, gfp_mask);
87 		if (!bip->bip_vec)
88 			goto err;
89 	} else if (nr_vecs) {
90 		bip->bip_vec = bip->bip_inline_vecs;
91 	}
92 
93 	bip->bip_bio = bio;
94 	bio->bi_integrity = bip;
95 	bio->bi_opf |= REQ_INTEGRITY;
96 
97 	return bip;
98 err:
99 	if (bs && mempool_initialized(&bs->bio_integrity_pool))
100 		mempool_free(bip, &bs->bio_integrity_pool);
101 	else
102 		kfree(bip);
103 	return ERR_PTR(-ENOMEM);
104 }
105 EXPORT_SYMBOL(bio_integrity_alloc);
106 
bio_integrity_unpin_bvec(struct bio_vec * bv,int nr_vecs)107 static void bio_integrity_unpin_bvec(struct bio_vec *bv, int nr_vecs)
108 {
109 	int i;
110 
111 	for (i = 0; i < nr_vecs; i++)
112 		unpin_user_page(bv[i].bv_page);
113 }
114 
bio_integrity_uncopy_user(struct bio_integrity_payload * bip)115 static void bio_integrity_uncopy_user(struct bio_integrity_payload *bip)
116 {
117 	unsigned short orig_nr_vecs = bip->bip_max_vcnt - 1;
118 	struct bio_vec *orig_bvecs = &bip->bip_vec[1];
119 	struct bio_vec *bounce_bvec = &bip->bip_vec[0];
120 	size_t bytes = bounce_bvec->bv_len;
121 	struct iov_iter orig_iter;
122 	int ret;
123 
124 	iov_iter_bvec(&orig_iter, ITER_DEST, orig_bvecs, orig_nr_vecs, bytes);
125 	ret = copy_to_iter(bvec_virt(bounce_bvec), bytes, &orig_iter);
126 	WARN_ON_ONCE(ret != bytes);
127 
128 	bio_integrity_unpin_bvec(orig_bvecs, orig_nr_vecs);
129 }
130 
131 /**
132  * bio_integrity_unmap_user - Unmap user integrity payload
133  * @bio:	bio containing bip to be unmapped
134  *
135  * Unmap the user mapped integrity portion of a bio.
136  */
bio_integrity_unmap_user(struct bio * bio)137 void bio_integrity_unmap_user(struct bio *bio)
138 {
139 	struct bio_integrity_payload *bip = bio_integrity(bio);
140 
141 	if (bip->bip_flags & BIP_COPY_USER) {
142 		if (bio_data_dir(bio) == READ)
143 			bio_integrity_uncopy_user(bip);
144 		kfree(bvec_virt(bip->bip_vec));
145 		return;
146 	}
147 
148 	bio_integrity_unpin_bvec(bip->bip_vec, bip->bip_max_vcnt);
149 }
150 
151 /**
152  * bio_integrity_add_page - Attach integrity metadata
153  * @bio:	bio to update
154  * @page:	page containing integrity metadata
155  * @len:	number of bytes of integrity metadata in page
156  * @offset:	start offset within page
157  *
158  * Description: Attach a page containing integrity metadata to bio.
159  */
bio_integrity_add_page(struct bio * bio,struct page * page,unsigned int len,unsigned int offset)160 int bio_integrity_add_page(struct bio *bio, struct page *page,
161 			   unsigned int len, unsigned int offset)
162 {
163 	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
164 	struct bio_integrity_payload *bip = bio_integrity(bio);
165 
166 	if (bip->bip_vcnt > 0) {
167 		struct bio_vec *bv = &bip->bip_vec[bip->bip_vcnt - 1];
168 		bool same_page = false;
169 
170 		if (bvec_try_merge_hw_page(q, bv, page, len, offset,
171 					   &same_page)) {
172 			bip->bip_iter.bi_size += len;
173 			return len;
174 		}
175 
176 		if (bip->bip_vcnt >=
177 		    min(bip->bip_max_vcnt, queue_max_integrity_segments(q)))
178 			return 0;
179 
180 		/*
181 		 * If the queue doesn't support SG gaps and adding this segment
182 		 * would create a gap, disallow it.
183 		 */
184 		if (bvec_gap_to_prev(&q->limits, bv, offset))
185 			return 0;
186 	}
187 
188 	bvec_set_page(&bip->bip_vec[bip->bip_vcnt], page, len, offset);
189 	bip->bip_vcnt++;
190 	bip->bip_iter.bi_size += len;
191 
192 	return len;
193 }
194 EXPORT_SYMBOL(bio_integrity_add_page);
195 
bio_integrity_copy_user(struct bio * bio,struct bio_vec * bvec,int nr_vecs,unsigned int len,unsigned int direction)196 static int bio_integrity_copy_user(struct bio *bio, struct bio_vec *bvec,
197 				   int nr_vecs, unsigned int len,
198 				   unsigned int direction)
199 {
200 	bool write = direction == ITER_SOURCE;
201 	struct bio_integrity_payload *bip;
202 	struct iov_iter iter;
203 	void *buf;
204 	int ret;
205 
206 	buf = kmalloc(len, GFP_KERNEL);
207 	if (!buf)
208 		return -ENOMEM;
209 
210 	if (write) {
211 		iov_iter_bvec(&iter, direction, bvec, nr_vecs, len);
212 		if (!copy_from_iter_full(buf, len, &iter)) {
213 			ret = -EFAULT;
214 			goto free_buf;
215 		}
216 
217 		bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
218 	} else {
219 		memset(buf, 0, len);
220 
221 		/*
222 		 * We need to preserve the original bvec and the number of vecs
223 		 * in it for completion handling
224 		 */
225 		bip = bio_integrity_alloc(bio, GFP_KERNEL, nr_vecs + 1);
226 	}
227 
228 	if (IS_ERR(bip)) {
229 		ret = PTR_ERR(bip);
230 		goto free_buf;
231 	}
232 
233 	if (write)
234 		bio_integrity_unpin_bvec(bvec, nr_vecs);
235 	else
236 		memcpy(&bip->bip_vec[1], bvec, nr_vecs * sizeof(*bvec));
237 
238 	ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
239 				     offset_in_page(buf));
240 	if (ret != len) {
241 		ret = -ENOMEM;
242 		goto free_bip;
243 	}
244 
245 	bip->bip_flags |= BIP_COPY_USER;
246 	bip->bip_vcnt = nr_vecs;
247 	return 0;
248 free_bip:
249 	bio_integrity_free(bio);
250 free_buf:
251 	kfree(buf);
252 	return ret;
253 }
254 
bio_integrity_init_user(struct bio * bio,struct bio_vec * bvec,int nr_vecs,unsigned int len)255 static int bio_integrity_init_user(struct bio *bio, struct bio_vec *bvec,
256 				   int nr_vecs, unsigned int len)
257 {
258 	struct bio_integrity_payload *bip;
259 
260 	bip = bio_integrity_alloc(bio, GFP_KERNEL, nr_vecs);
261 	if (IS_ERR(bip))
262 		return PTR_ERR(bip);
263 
264 	memcpy(bip->bip_vec, bvec, nr_vecs * sizeof(*bvec));
265 	bip->bip_iter.bi_size = len;
266 	bip->bip_vcnt = nr_vecs;
267 	return 0;
268 }
269 
bvec_from_pages(struct bio_vec * bvec,struct page ** pages,int nr_vecs,ssize_t bytes,ssize_t offset)270 static unsigned int bvec_from_pages(struct bio_vec *bvec, struct page **pages,
271 				    int nr_vecs, ssize_t bytes, ssize_t offset)
272 {
273 	unsigned int nr_bvecs = 0;
274 	int i, j;
275 
276 	for (i = 0; i < nr_vecs; i = j) {
277 		size_t size = min_t(size_t, bytes, PAGE_SIZE - offset);
278 		struct folio *folio = page_folio(pages[i]);
279 
280 		bytes -= size;
281 		for (j = i + 1; j < nr_vecs; j++) {
282 			size_t next = min_t(size_t, PAGE_SIZE, bytes);
283 
284 			if (page_folio(pages[j]) != folio ||
285 			    pages[j] != pages[j - 1] + 1)
286 				break;
287 			unpin_user_page(pages[j]);
288 			size += next;
289 			bytes -= next;
290 		}
291 
292 		bvec_set_page(&bvec[nr_bvecs], pages[i], size, offset);
293 		offset = 0;
294 		nr_bvecs++;
295 	}
296 
297 	return nr_bvecs;
298 }
299 
bio_integrity_map_user(struct bio * bio,struct iov_iter * iter)300 int bio_integrity_map_user(struct bio *bio, struct iov_iter *iter)
301 {
302 	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
303 	unsigned int align = blk_lim_dma_alignment_and_pad(&q->limits);
304 	struct page *stack_pages[UIO_FASTIOV], **pages = stack_pages;
305 	struct bio_vec stack_vec[UIO_FASTIOV], *bvec = stack_vec;
306 	size_t offset, bytes = iter->count;
307 	unsigned int direction, nr_bvecs;
308 	int ret, nr_vecs;
309 	bool copy;
310 
311 	if (bio_integrity(bio))
312 		return -EINVAL;
313 	if (bytes >> SECTOR_SHIFT > queue_max_hw_sectors(q))
314 		return -E2BIG;
315 
316 	if (bio_data_dir(bio) == READ)
317 		direction = ITER_DEST;
318 	else
319 		direction = ITER_SOURCE;
320 
321 	nr_vecs = iov_iter_npages(iter, BIO_MAX_VECS + 1);
322 	if (nr_vecs > BIO_MAX_VECS)
323 		return -E2BIG;
324 	if (nr_vecs > UIO_FASTIOV) {
325 		bvec = kcalloc(nr_vecs, sizeof(*bvec), GFP_KERNEL);
326 		if (!bvec)
327 			return -ENOMEM;
328 		pages = NULL;
329 	}
330 
331 	copy = !iov_iter_is_aligned(iter, align, align);
332 	ret = iov_iter_extract_pages(iter, &pages, bytes, nr_vecs, 0, &offset);
333 	if (unlikely(ret < 0))
334 		goto free_bvec;
335 
336 	nr_bvecs = bvec_from_pages(bvec, pages, nr_vecs, bytes, offset);
337 	if (pages != stack_pages)
338 		kvfree(pages);
339 	if (nr_bvecs > queue_max_integrity_segments(q))
340 		copy = true;
341 
342 	if (copy)
343 		ret = bio_integrity_copy_user(bio, bvec, nr_bvecs, bytes,
344 					      direction);
345 	else
346 		ret = bio_integrity_init_user(bio, bvec, nr_bvecs, bytes);
347 	if (ret)
348 		goto release_pages;
349 	if (bvec != stack_vec)
350 		kfree(bvec);
351 
352 	return 0;
353 
354 release_pages:
355 	bio_integrity_unpin_bvec(bvec, nr_bvecs);
356 free_bvec:
357 	if (bvec != stack_vec)
358 		kfree(bvec);
359 	return ret;
360 }
361 
bio_uio_meta_to_bip(struct bio * bio,struct uio_meta * meta)362 static void bio_uio_meta_to_bip(struct bio *bio, struct uio_meta *meta)
363 {
364 	struct bio_integrity_payload *bip = bio_integrity(bio);
365 
366 	if (meta->flags & IO_INTEGRITY_CHK_GUARD)
367 		bip->bip_flags |= BIP_CHECK_GUARD;
368 	if (meta->flags & IO_INTEGRITY_CHK_APPTAG)
369 		bip->bip_flags |= BIP_CHECK_APPTAG;
370 	if (meta->flags & IO_INTEGRITY_CHK_REFTAG)
371 		bip->bip_flags |= BIP_CHECK_REFTAG;
372 
373 	bip->app_tag = meta->app_tag;
374 }
375 
bio_integrity_map_iter(struct bio * bio,struct uio_meta * meta)376 int bio_integrity_map_iter(struct bio *bio, struct uio_meta *meta)
377 {
378 	struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
379 	unsigned int integrity_bytes;
380 	int ret;
381 	struct iov_iter it;
382 
383 	if (!bi)
384 		return -EINVAL;
385 	/*
386 	 * original meta iterator can be bigger.
387 	 * process integrity info corresponding to current data buffer only.
388 	 */
389 	it = meta->iter;
390 	integrity_bytes = bio_integrity_bytes(bi, bio_sectors(bio));
391 	if (it.count < integrity_bytes)
392 		return -EINVAL;
393 
394 	/* should fit into two bytes */
395 	BUILD_BUG_ON(IO_INTEGRITY_VALID_FLAGS >= (1 << 16));
396 
397 	if (meta->flags && (meta->flags & ~IO_INTEGRITY_VALID_FLAGS))
398 		return -EINVAL;
399 
400 	it.count = integrity_bytes;
401 	ret = bio_integrity_map_user(bio, &it);
402 	if (!ret) {
403 		bio_uio_meta_to_bip(bio, meta);
404 		bip_set_seed(bio_integrity(bio), meta->seed);
405 		iov_iter_advance(&meta->iter, integrity_bytes);
406 		meta->seed += bio_integrity_intervals(bi, bio_sectors(bio));
407 	}
408 	return ret;
409 }
410 
411 /**
412  * bio_integrity_prep - Prepare bio for integrity I/O
413  * @bio:	bio to prepare
414  *
415  * Description:  Checks if the bio already has an integrity payload attached.
416  * If it does, the payload has been generated by another kernel subsystem,
417  * and we just pass it through. Otherwise allocates integrity payload.
418  * The bio must have data direction, target device and start sector set priot
419  * to calling.  In the WRITE case, integrity metadata will be generated using
420  * the block device's integrity function.  In the READ case, the buffer
421  * will be prepared for DMA and a suitable end_io handler set up.
422  */
bio_integrity_prep(struct bio * bio)423 bool bio_integrity_prep(struct bio *bio)
424 {
425 	struct bio_integrity_payload *bip;
426 	struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
427 	unsigned int len;
428 	void *buf;
429 	gfp_t gfp = GFP_NOIO;
430 
431 	if (!bi)
432 		return true;
433 
434 	if (!bio_sectors(bio))
435 		return true;
436 
437 	/* Already protected? */
438 	if (bio_integrity(bio))
439 		return true;
440 
441 	switch (bio_op(bio)) {
442 	case REQ_OP_READ:
443 		if (bi->flags & BLK_INTEGRITY_NOVERIFY)
444 			return true;
445 		break;
446 	case REQ_OP_WRITE:
447 		if (bi->flags & BLK_INTEGRITY_NOGENERATE)
448 			return true;
449 
450 		/*
451 		 * Zero the memory allocated to not leak uninitialized kernel
452 		 * memory to disk for non-integrity metadata where nothing else
453 		 * initializes the memory.
454 		 */
455 		if (bi->csum_type == BLK_INTEGRITY_CSUM_NONE)
456 			gfp |= __GFP_ZERO;
457 		break;
458 	default:
459 		return true;
460 	}
461 
462 	/* Allocate kernel buffer for protection data */
463 	len = bio_integrity_bytes(bi, bio_sectors(bio));
464 	buf = kmalloc(len, gfp);
465 	if (unlikely(buf == NULL)) {
466 		goto err_end_io;
467 	}
468 
469 	bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
470 	if (IS_ERR(bip)) {
471 		kfree(buf);
472 		goto err_end_io;
473 	}
474 
475 	bip->bip_flags |= BIP_BLOCK_INTEGRITY;
476 	bip_set_seed(bip, bio->bi_iter.bi_sector);
477 
478 	if (bi->csum_type == BLK_INTEGRITY_CSUM_IP)
479 		bip->bip_flags |= BIP_IP_CHECKSUM;
480 
481 	/* describe what tags to check in payload */
482 	if (bi->csum_type)
483 		bip->bip_flags |= BIP_CHECK_GUARD;
484 	if (bi->flags & BLK_INTEGRITY_REF_TAG)
485 		bip->bip_flags |= BIP_CHECK_REFTAG;
486 	if (bio_integrity_add_page(bio, virt_to_page(buf), len,
487 			offset_in_page(buf)) < len) {
488 		printk(KERN_ERR "could not attach integrity payload\n");
489 		goto err_end_io;
490 	}
491 
492 	/* Auto-generate integrity metadata if this is a write */
493 	if (bio_data_dir(bio) == WRITE)
494 		blk_integrity_generate(bio);
495 	else
496 		bip->bio_iter = bio->bi_iter;
497 	return true;
498 
499 err_end_io:
500 	bio->bi_status = BLK_STS_RESOURCE;
501 	bio_endio(bio);
502 	return false;
503 }
504 EXPORT_SYMBOL(bio_integrity_prep);
505 
506 /**
507  * bio_integrity_verify_fn - Integrity I/O completion worker
508  * @work:	Work struct stored in bio to be verified
509  *
510  * Description: This workqueue function is called to complete a READ
511  * request.  The function verifies the transferred integrity metadata
512  * and then calls the original bio end_io function.
513  */
bio_integrity_verify_fn(struct work_struct * work)514 static void bio_integrity_verify_fn(struct work_struct *work)
515 {
516 	struct bio_integrity_payload *bip =
517 		container_of(work, struct bio_integrity_payload, bip_work);
518 	struct bio *bio = bip->bip_bio;
519 
520 	blk_integrity_verify(bio);
521 
522 	kfree(bvec_virt(bip->bip_vec));
523 	bio_integrity_free(bio);
524 	bio_endio(bio);
525 }
526 
527 /**
528  * __bio_integrity_endio - Integrity I/O completion function
529  * @bio:	Protected bio
530  *
531  * Description: Completion for integrity I/O
532  *
533  * Normally I/O completion is done in interrupt context.  However,
534  * verifying I/O integrity is a time-consuming task which must be run
535  * in process context.	This function postpones completion
536  * accordingly.
537  */
__bio_integrity_endio(struct bio * bio)538 bool __bio_integrity_endio(struct bio *bio)
539 {
540 	struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
541 	struct bio_integrity_payload *bip = bio_integrity(bio);
542 
543 	if (bio_op(bio) == REQ_OP_READ && !bio->bi_status && bi->csum_type) {
544 		INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
545 		queue_work(kintegrityd_wq, &bip->bip_work);
546 		return false;
547 	}
548 
549 	kfree(bvec_virt(bip->bip_vec));
550 	bio_integrity_free(bio);
551 	return true;
552 }
553 
554 /**
555  * bio_integrity_advance - Advance integrity vector
556  * @bio:	bio whose integrity vector to update
557  * @bytes_done:	number of data bytes that have been completed
558  *
559  * Description: This function calculates how many integrity bytes the
560  * number of completed data bytes correspond to and advances the
561  * integrity vector accordingly.
562  */
bio_integrity_advance(struct bio * bio,unsigned int bytes_done)563 void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
564 {
565 	struct bio_integrity_payload *bip = bio_integrity(bio);
566 	struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
567 	unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
568 
569 	bip->bip_iter.bi_sector += bio_integrity_intervals(bi, bytes_done >> 9);
570 	bvec_iter_advance(bip->bip_vec, &bip->bip_iter, bytes);
571 }
572 
573 /**
574  * bio_integrity_trim - Trim integrity vector
575  * @bio:	bio whose integrity vector to update
576  *
577  * Description: Used to trim the integrity vector in a cloned bio.
578  */
bio_integrity_trim(struct bio * bio)579 void bio_integrity_trim(struct bio *bio)
580 {
581 	struct bio_integrity_payload *bip = bio_integrity(bio);
582 	struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
583 
584 	bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
585 }
586 EXPORT_SYMBOL(bio_integrity_trim);
587 
588 /**
589  * bio_integrity_clone - Callback for cloning bios with integrity metadata
590  * @bio:	New bio
591  * @bio_src:	Original bio
592  * @gfp_mask:	Memory allocation mask
593  *
594  * Description:	Called to allocate a bip when cloning a bio
595  */
bio_integrity_clone(struct bio * bio,struct bio * bio_src,gfp_t gfp_mask)596 int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
597 			gfp_t gfp_mask)
598 {
599 	struct bio_integrity_payload *bip_src = bio_integrity(bio_src);
600 	struct bio_integrity_payload *bip;
601 
602 	BUG_ON(bip_src == NULL);
603 
604 	bip = bio_integrity_alloc(bio, gfp_mask, 0);
605 	if (IS_ERR(bip))
606 		return PTR_ERR(bip);
607 
608 	bip->bip_vec = bip_src->bip_vec;
609 	bip->bip_iter = bip_src->bip_iter;
610 	bip->bip_flags = bip_src->bip_flags & BIP_CLONE_FLAGS;
611 	bip->app_tag = bip_src->app_tag;
612 
613 	return 0;
614 }
615 
bioset_integrity_create(struct bio_set * bs,int pool_size)616 int bioset_integrity_create(struct bio_set *bs, int pool_size)
617 {
618 	if (mempool_initialized(&bs->bio_integrity_pool))
619 		return 0;
620 
621 	if (mempool_init_slab_pool(&bs->bio_integrity_pool,
622 				   pool_size, bip_slab))
623 		return -1;
624 
625 	if (biovec_init_pool(&bs->bvec_integrity_pool, pool_size)) {
626 		mempool_exit(&bs->bio_integrity_pool);
627 		return -1;
628 	}
629 
630 	return 0;
631 }
632 EXPORT_SYMBOL(bioset_integrity_create);
633 
bioset_integrity_free(struct bio_set * bs)634 void bioset_integrity_free(struct bio_set *bs)
635 {
636 	mempool_exit(&bs->bio_integrity_pool);
637 	mempool_exit(&bs->bvec_integrity_pool);
638 }
639 
bio_integrity_init(void)640 void __init bio_integrity_init(void)
641 {
642 	/*
643 	 * kintegrityd won't block much but may burn a lot of CPU cycles.
644 	 * Make it highpri CPU intensive wq with max concurrency of 1.
645 	 */
646 	kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
647 					 WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
648 	if (!kintegrityd_wq)
649 		panic("Failed to create kintegrityd\n");
650 
651 	bip_slab = kmem_cache_create("bio_integrity_payload",
652 				     sizeof(struct bio_integrity_payload) +
653 				     sizeof(struct bio_vec) * BIO_INLINE_VECS,
654 				     0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
655 }
656