Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/block/bio-integrity.c
29264 views
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 "blk.h"
11
12
struct bio_integrity_alloc {
13
struct bio_integrity_payload bip;
14
struct bio_vec bvecs[];
15
};
16
17
/**
18
* bio_integrity_free - Free bio integrity payload
19
* @bio: bio containing bip to be freed
20
*
21
* Description: Free the integrity portion of a bio.
22
*/
23
void bio_integrity_free(struct bio *bio)
24
{
25
kfree(bio_integrity(bio));
26
bio->bi_integrity = NULL;
27
bio->bi_opf &= ~REQ_INTEGRITY;
28
}
29
30
void bio_integrity_init(struct bio *bio, struct bio_integrity_payload *bip,
31
struct bio_vec *bvecs, unsigned int nr_vecs)
32
{
33
memset(bip, 0, sizeof(*bip));
34
bip->bip_max_vcnt = nr_vecs;
35
if (nr_vecs)
36
bip->bip_vec = bvecs;
37
38
bio->bi_integrity = bip;
39
bio->bi_opf |= REQ_INTEGRITY;
40
}
41
42
/**
43
* bio_integrity_alloc - Allocate integrity payload and attach it to bio
44
* @bio: bio to attach integrity metadata to
45
* @gfp_mask: Memory allocation mask
46
* @nr_vecs: Number of integrity metadata scatter-gather elements
47
*
48
* Description: This function prepares a bio for attaching integrity
49
* metadata. nr_vecs specifies the maximum number of pages containing
50
* integrity metadata that can be attached.
51
*/
52
struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
53
gfp_t gfp_mask,
54
unsigned int nr_vecs)
55
{
56
struct bio_integrity_alloc *bia;
57
58
if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
59
return ERR_PTR(-EOPNOTSUPP);
60
61
bia = kmalloc(struct_size(bia, bvecs, nr_vecs), gfp_mask);
62
if (unlikely(!bia))
63
return ERR_PTR(-ENOMEM);
64
bio_integrity_init(bio, &bia->bip, bia->bvecs, nr_vecs);
65
return &bia->bip;
66
}
67
EXPORT_SYMBOL(bio_integrity_alloc);
68
69
static void bio_integrity_unpin_bvec(struct bio_vec *bv, int nr_vecs)
70
{
71
int i;
72
73
for (i = 0; i < nr_vecs; i++)
74
unpin_user_page(bv[i].bv_page);
75
}
76
77
static void bio_integrity_uncopy_user(struct bio_integrity_payload *bip)
78
{
79
unsigned short orig_nr_vecs = bip->bip_max_vcnt - 1;
80
struct bio_vec *orig_bvecs = &bip->bip_vec[1];
81
struct bio_vec *bounce_bvec = &bip->bip_vec[0];
82
size_t bytes = bounce_bvec->bv_len;
83
struct iov_iter orig_iter;
84
int ret;
85
86
iov_iter_bvec(&orig_iter, ITER_DEST, orig_bvecs, orig_nr_vecs, bytes);
87
ret = copy_to_iter(bvec_virt(bounce_bvec), bytes, &orig_iter);
88
WARN_ON_ONCE(ret != bytes);
89
90
bio_integrity_unpin_bvec(orig_bvecs, orig_nr_vecs);
91
}
92
93
/**
94
* bio_integrity_unmap_user - Unmap user integrity payload
95
* @bio: bio containing bip to be unmapped
96
*
97
* Unmap the user mapped integrity portion of a bio.
98
*/
99
void bio_integrity_unmap_user(struct bio *bio)
100
{
101
struct bio_integrity_payload *bip = bio_integrity(bio);
102
103
if (bip->bip_flags & BIP_COPY_USER) {
104
if (bio_data_dir(bio) == READ)
105
bio_integrity_uncopy_user(bip);
106
kfree(bvec_virt(bip->bip_vec));
107
return;
108
}
109
110
bio_integrity_unpin_bvec(bip->bip_vec, bip->bip_max_vcnt);
111
}
112
113
/**
114
* bio_integrity_add_page - Attach integrity metadata
115
* @bio: bio to update
116
* @page: page containing integrity metadata
117
* @len: number of bytes of integrity metadata in page
118
* @offset: start offset within page
119
*
120
* Description: Attach a page containing integrity metadata to bio.
121
*/
122
int bio_integrity_add_page(struct bio *bio, struct page *page,
123
unsigned int len, unsigned int offset)
124
{
125
struct request_queue *q = bdev_get_queue(bio->bi_bdev);
126
struct bio_integrity_payload *bip = bio_integrity(bio);
127
128
if (bip->bip_vcnt > 0) {
129
struct bio_vec *bv = &bip->bip_vec[bip->bip_vcnt - 1];
130
131
if (!zone_device_pages_have_same_pgmap(bv->bv_page, page))
132
return 0;
133
134
if (bvec_try_merge_hw_page(q, bv, page, len, offset)) {
135
bip->bip_iter.bi_size += len;
136
return len;
137
}
138
139
if (bip->bip_vcnt >=
140
min(bip->bip_max_vcnt, queue_max_integrity_segments(q)))
141
return 0;
142
143
/*
144
* If the queue doesn't support SG gaps and adding this segment
145
* would create a gap, disallow it.
146
*/
147
if (bvec_gap_to_prev(&q->limits, bv, offset))
148
return 0;
149
}
150
151
bvec_set_page(&bip->bip_vec[bip->bip_vcnt], page, len, offset);
152
bip->bip_vcnt++;
153
bip->bip_iter.bi_size += len;
154
155
return len;
156
}
157
EXPORT_SYMBOL(bio_integrity_add_page);
158
159
static int bio_integrity_copy_user(struct bio *bio, struct bio_vec *bvec,
160
int nr_vecs, unsigned int len)
161
{
162
bool write = op_is_write(bio_op(bio));
163
struct bio_integrity_payload *bip;
164
struct iov_iter iter;
165
void *buf;
166
int ret;
167
168
buf = kmalloc(len, GFP_KERNEL);
169
if (!buf)
170
return -ENOMEM;
171
172
if (write) {
173
iov_iter_bvec(&iter, ITER_SOURCE, bvec, nr_vecs, len);
174
if (!copy_from_iter_full(buf, len, &iter)) {
175
ret = -EFAULT;
176
goto free_buf;
177
}
178
179
bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
180
} else {
181
memset(buf, 0, len);
182
183
/*
184
* We need to preserve the original bvec and the number of vecs
185
* in it for completion handling
186
*/
187
bip = bio_integrity_alloc(bio, GFP_KERNEL, nr_vecs + 1);
188
}
189
190
if (IS_ERR(bip)) {
191
ret = PTR_ERR(bip);
192
goto free_buf;
193
}
194
195
if (write)
196
bio_integrity_unpin_bvec(bvec, nr_vecs);
197
else
198
memcpy(&bip->bip_vec[1], bvec, nr_vecs * sizeof(*bvec));
199
200
ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
201
offset_in_page(buf));
202
if (ret != len) {
203
ret = -ENOMEM;
204
goto free_bip;
205
}
206
207
bip->bip_flags |= BIP_COPY_USER;
208
bip->bip_vcnt = nr_vecs;
209
return 0;
210
free_bip:
211
bio_integrity_free(bio);
212
free_buf:
213
kfree(buf);
214
return ret;
215
}
216
217
static int bio_integrity_init_user(struct bio *bio, struct bio_vec *bvec,
218
int nr_vecs, unsigned int len)
219
{
220
struct bio_integrity_payload *bip;
221
222
bip = bio_integrity_alloc(bio, GFP_KERNEL, nr_vecs);
223
if (IS_ERR(bip))
224
return PTR_ERR(bip);
225
226
memcpy(bip->bip_vec, bvec, nr_vecs * sizeof(*bvec));
227
bip->bip_iter.bi_size = len;
228
bip->bip_vcnt = nr_vecs;
229
return 0;
230
}
231
232
static unsigned int bvec_from_pages(struct bio_vec *bvec, struct page **pages,
233
int nr_vecs, ssize_t bytes, ssize_t offset,
234
bool *is_p2p)
235
{
236
unsigned int nr_bvecs = 0;
237
int i, j;
238
239
for (i = 0; i < nr_vecs; i = j) {
240
size_t size = min_t(size_t, bytes, PAGE_SIZE - offset);
241
struct folio *folio = page_folio(pages[i]);
242
243
bytes -= size;
244
for (j = i + 1; j < nr_vecs; j++) {
245
size_t next = min_t(size_t, PAGE_SIZE, bytes);
246
247
if (page_folio(pages[j]) != folio ||
248
pages[j] != pages[j - 1] + 1)
249
break;
250
unpin_user_page(pages[j]);
251
size += next;
252
bytes -= next;
253
}
254
255
if (is_pci_p2pdma_page(pages[i]))
256
*is_p2p = true;
257
258
bvec_set_page(&bvec[nr_bvecs], pages[i], size, offset);
259
offset = 0;
260
nr_bvecs++;
261
}
262
263
return nr_bvecs;
264
}
265
266
int bio_integrity_map_user(struct bio *bio, struct iov_iter *iter)
267
{
268
struct request_queue *q = bdev_get_queue(bio->bi_bdev);
269
struct page *stack_pages[UIO_FASTIOV], **pages = stack_pages;
270
struct bio_vec stack_vec[UIO_FASTIOV], *bvec = stack_vec;
271
iov_iter_extraction_t extraction_flags = 0;
272
size_t offset, bytes = iter->count;
273
bool copy, is_p2p = false;
274
unsigned int nr_bvecs;
275
int ret, nr_vecs;
276
277
if (bio_integrity(bio))
278
return -EINVAL;
279
if (bytes >> SECTOR_SHIFT > queue_max_hw_sectors(q))
280
return -E2BIG;
281
282
nr_vecs = iov_iter_npages(iter, BIO_MAX_VECS + 1);
283
if (nr_vecs > BIO_MAX_VECS)
284
return -E2BIG;
285
if (nr_vecs > UIO_FASTIOV) {
286
bvec = kcalloc(nr_vecs, sizeof(*bvec), GFP_KERNEL);
287
if (!bvec)
288
return -ENOMEM;
289
pages = NULL;
290
}
291
292
copy = iov_iter_alignment(iter) &
293
blk_lim_dma_alignment_and_pad(&q->limits);
294
295
if (blk_queue_pci_p2pdma(q))
296
extraction_flags |= ITER_ALLOW_P2PDMA;
297
298
ret = iov_iter_extract_pages(iter, &pages, bytes, nr_vecs,
299
extraction_flags, &offset);
300
if (unlikely(ret < 0))
301
goto free_bvec;
302
303
nr_bvecs = bvec_from_pages(bvec, pages, nr_vecs, bytes, offset,
304
&is_p2p);
305
if (pages != stack_pages)
306
kvfree(pages);
307
if (nr_bvecs > queue_max_integrity_segments(q))
308
copy = true;
309
if (is_p2p)
310
bio->bi_opf |= REQ_NOMERGE;
311
312
if (copy)
313
ret = bio_integrity_copy_user(bio, bvec, nr_bvecs, bytes);
314
else
315
ret = bio_integrity_init_user(bio, bvec, nr_bvecs, bytes);
316
if (ret)
317
goto release_pages;
318
if (bvec != stack_vec)
319
kfree(bvec);
320
321
return 0;
322
323
release_pages:
324
bio_integrity_unpin_bvec(bvec, nr_bvecs);
325
free_bvec:
326
if (bvec != stack_vec)
327
kfree(bvec);
328
return ret;
329
}
330
331
static void bio_uio_meta_to_bip(struct bio *bio, struct uio_meta *meta)
332
{
333
struct bio_integrity_payload *bip = bio_integrity(bio);
334
335
if (meta->flags & IO_INTEGRITY_CHK_GUARD)
336
bip->bip_flags |= BIP_CHECK_GUARD;
337
if (meta->flags & IO_INTEGRITY_CHK_APPTAG)
338
bip->bip_flags |= BIP_CHECK_APPTAG;
339
if (meta->flags & IO_INTEGRITY_CHK_REFTAG)
340
bip->bip_flags |= BIP_CHECK_REFTAG;
341
342
bip->app_tag = meta->app_tag;
343
}
344
345
int bio_integrity_map_iter(struct bio *bio, struct uio_meta *meta)
346
{
347
struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
348
unsigned int integrity_bytes;
349
int ret;
350
struct iov_iter it;
351
352
if (!bi)
353
return -EINVAL;
354
/*
355
* original meta iterator can be bigger.
356
* process integrity info corresponding to current data buffer only.
357
*/
358
it = meta->iter;
359
integrity_bytes = bio_integrity_bytes(bi, bio_sectors(bio));
360
if (it.count < integrity_bytes)
361
return -EINVAL;
362
363
/* should fit into two bytes */
364
BUILD_BUG_ON(IO_INTEGRITY_VALID_FLAGS >= (1 << 16));
365
366
if (meta->flags && (meta->flags & ~IO_INTEGRITY_VALID_FLAGS))
367
return -EINVAL;
368
369
it.count = integrity_bytes;
370
ret = bio_integrity_map_user(bio, &it);
371
if (!ret) {
372
bio_uio_meta_to_bip(bio, meta);
373
bip_set_seed(bio_integrity(bio), meta->seed);
374
iov_iter_advance(&meta->iter, integrity_bytes);
375
meta->seed += bio_integrity_intervals(bi, bio_sectors(bio));
376
}
377
return ret;
378
}
379
380
/**
381
* bio_integrity_advance - Advance integrity vector
382
* @bio: bio whose integrity vector to update
383
* @bytes_done: number of data bytes that have been completed
384
*
385
* Description: This function calculates how many integrity bytes the
386
* number of completed data bytes correspond to and advances the
387
* integrity vector accordingly.
388
*/
389
void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
390
{
391
struct bio_integrity_payload *bip = bio_integrity(bio);
392
struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
393
unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
394
395
bip->bip_iter.bi_sector += bio_integrity_intervals(bi, bytes_done >> 9);
396
bvec_iter_advance(bip->bip_vec, &bip->bip_iter, bytes);
397
}
398
399
/**
400
* bio_integrity_trim - Trim integrity vector
401
* @bio: bio whose integrity vector to update
402
*
403
* Description: Used to trim the integrity vector in a cloned bio.
404
*/
405
void bio_integrity_trim(struct bio *bio)
406
{
407
struct bio_integrity_payload *bip = bio_integrity(bio);
408
struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
409
410
bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
411
}
412
EXPORT_SYMBOL(bio_integrity_trim);
413
414
/**
415
* bio_integrity_clone - Callback for cloning bios with integrity metadata
416
* @bio: New bio
417
* @bio_src: Original bio
418
* @gfp_mask: Memory allocation mask
419
*
420
* Description: Called to allocate a bip when cloning a bio
421
*/
422
int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
423
gfp_t gfp_mask)
424
{
425
struct bio_integrity_payload *bip_src = bio_integrity(bio_src);
426
struct bio_integrity_payload *bip;
427
428
BUG_ON(bip_src == NULL);
429
430
bip = bio_integrity_alloc(bio, gfp_mask, 0);
431
if (IS_ERR(bip))
432
return PTR_ERR(bip);
433
434
bip->bip_vec = bip_src->bip_vec;
435
bip->bip_iter = bip_src->bip_iter;
436
bip->bip_flags = bip_src->bip_flags & BIP_CLONE_FLAGS;
437
bip->app_tag = bip_src->app_tag;
438
439
return 0;
440
}
441
442