Skip to content

Commit 54d4e6a

Browse files
Max Gurtovoyaxboe
authored andcommitted
block: centralize PI remapping logic to the block layer
Currently t10_pi_prepare/t10_pi_complete functions are called during the NVMe and SCSi layers command preparetion/completion, but their actual place should be the block layer since T10-PI is a general data integrity feature that is used by block storage protocols. Introduce .prepare_fn and .complete_fn callbacks within the integrity profile that each type can implement according to its needs. Suggested-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Christoph Hellwig <hch@lst.de> Suggested-by: Martin K. Petersen <martin.petersen@oracle.com> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com> Signed-off-by: Max Gurtovoy <maxg@mellanox.com> Fixed to not call queue integrity functions if BLK_DEV_INTEGRITY isn't defined in the config. Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 5eaed68 commit 54d4e6a

9 files changed

Lines changed: 114 additions & 99 deletions

File tree

‎block/blk-core.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <linux/ratelimit.h>
3535
#include <linux/pm_runtime.h>
3636
#include <linux/blk-cgroup.h>
37+
#include <linux/t10-pi.h>
3738
#include <linux/debugfs.h>
3839
#include <linux/bpf.h>
3940
#include <linux/psi.h>
@@ -1436,6 +1437,12 @@ bool blk_update_request(struct request *req, blk_status_t error,
14361437
if (!req->bio)
14371438
return false;
14381439

1440+
#ifdef CONFIG_BLK_DEV_INTEGRITY
1441+
if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ &&
1442+
error == BLK_STS_OK)
1443+
req->q->integrity.profile->complete_fn(req, nr_bytes);
1444+
#endif
1445+
14391446
if (unlikely(error && !blk_rq_is_passthrough(req) &&
14401447
!(req->rq_flags & RQF_QUIET)))
14411448
print_req_error(req, error, __func__);

‎block/blk-integrity.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,21 @@ static blk_status_t blk_integrity_nop_fn(struct blk_integrity_iter *iter)
368368
return BLK_STS_OK;
369369
}
370370

371+
static void blk_integrity_nop_prepare(struct request *rq)
372+
{
373+
}
374+
375+
static void blk_integrity_nop_complete(struct request *rq,
376+
unsigned int nr_bytes)
377+
{
378+
}
379+
371380
static const struct blk_integrity_profile nop_profile = {
372381
.name = "nop",
373382
.generate_fn = blk_integrity_nop_fn,
374383
.verify_fn = blk_integrity_nop_fn,
384+
.prepare_fn = blk_integrity_nop_prepare,
385+
.complete_fn = blk_integrity_nop_complete,
375386
};
376387

377388
/**

‎block/blk-mq.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <trace/events/block.h>
3131

3232
#include <linux/blk-mq.h>
33+
#include <linux/t10-pi.h>
3334
#include "blk.h"
3435
#include "blk-mq.h"
3536
#include "blk-mq-debugfs.h"
@@ -700,6 +701,11 @@ void blk_mq_start_request(struct request *rq)
700701
*/
701702
rq->nr_phys_segments++;
702703
}
704+
705+
#ifdef CONFIG_BLK_DEV_INTEGRITY
706+
if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
707+
q->integrity.profile->prepare_fn(rq);
708+
#endif
703709
}
704710
EXPORT_SYMBOL(blk_mq_start_request);
705711

‎block/t10-pi.c‎

Lines changed: 76 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -120,76 +120,22 @@ static blk_status_t t10_pi_type1_verify_ip(struct blk_integrity_iter *iter)
120120
return t10_pi_verify(iter, t10_pi_ip_fn, T10_PI_TYPE1_PROTECTION);
121121
}
122122

123-
static blk_status_t t10_pi_type3_generate_crc(struct blk_integrity_iter *iter)
124-
{
125-
return t10_pi_generate(iter, t10_pi_crc_fn, T10_PI_TYPE3_PROTECTION);
126-
}
127-
128-
static blk_status_t t10_pi_type3_generate_ip(struct blk_integrity_iter *iter)
129-
{
130-
return t10_pi_generate(iter, t10_pi_ip_fn, T10_PI_TYPE3_PROTECTION);
131-
}
132-
133-
static blk_status_t t10_pi_type3_verify_crc(struct blk_integrity_iter *iter)
134-
{
135-
return t10_pi_verify(iter, t10_pi_crc_fn, T10_PI_TYPE3_PROTECTION);
136-
}
137-
138-
static blk_status_t t10_pi_type3_verify_ip(struct blk_integrity_iter *iter)
139-
{
140-
return t10_pi_verify(iter, t10_pi_ip_fn, T10_PI_TYPE3_PROTECTION);
141-
}
142-
143-
const struct blk_integrity_profile t10_pi_type1_crc = {
144-
.name = "T10-DIF-TYPE1-CRC",
145-
.generate_fn = t10_pi_type1_generate_crc,
146-
.verify_fn = t10_pi_type1_verify_crc,
147-
};
148-
EXPORT_SYMBOL(t10_pi_type1_crc);
149-
150-
const struct blk_integrity_profile t10_pi_type1_ip = {
151-
.name = "T10-DIF-TYPE1-IP",
152-
.generate_fn = t10_pi_type1_generate_ip,
153-
.verify_fn = t10_pi_type1_verify_ip,
154-
};
155-
EXPORT_SYMBOL(t10_pi_type1_ip);
156-
157-
const struct blk_integrity_profile t10_pi_type3_crc = {
158-
.name = "T10-DIF-TYPE3-CRC",
159-
.generate_fn = t10_pi_type3_generate_crc,
160-
.verify_fn = t10_pi_type3_verify_crc,
161-
};
162-
EXPORT_SYMBOL(t10_pi_type3_crc);
163-
164-
const struct blk_integrity_profile t10_pi_type3_ip = {
165-
.name = "T10-DIF-TYPE3-IP",
166-
.generate_fn = t10_pi_type3_generate_ip,
167-
.verify_fn = t10_pi_type3_verify_ip,
168-
};
169-
EXPORT_SYMBOL(t10_pi_type3_ip);
170-
171123
/**
172-
* t10_pi_prepare - prepare PI prior submitting request to device
124+
* t10_pi_type1_prepare - prepare PI prior submitting request to device
173125
* @rq: request with PI that should be prepared
174-
* @protection_type: PI type (Type 1/Type 2/Type 3)
175126
*
176127
* For Type 1/Type 2, the virtual start sector is the one that was
177128
* originally submitted by the block layer for the ref_tag usage. Due to
178129
* partitioning, MD/DM cloning, etc. the actual physical start sector is
179130
* likely to be different. Remap protection information to match the
180131
* physical LBA.
181-
*
182-
* Type 3 does not have a reference tag so no remapping is required.
183132
*/
184-
void t10_pi_prepare(struct request *rq, u8 protection_type)
133+
static void t10_pi_type1_prepare(struct request *rq)
185134
{
186135
const int tuple_sz = rq->q->integrity.tuple_size;
187136
u32 ref_tag = t10_pi_ref_tag(rq);
188137
struct bio *bio;
189138

190-
if (protection_type == T10_PI_TYPE3_PROTECTION)
191-
return;
192-
193139
__rq_for_each_bio(bio, rq) {
194140
struct bio_integrity_payload *bip = bio_integrity(bio);
195141
u32 virt = bip_get_seed(bip) & 0xffffffff;
@@ -222,33 +168,26 @@ void t10_pi_prepare(struct request *rq, u8 protection_type)
222168
bip->bip_flags |= BIP_MAPPED_INTEGRITY;
223169
}
224170
}
225-
EXPORT_SYMBOL(t10_pi_prepare);
226171

227172
/**
228-
* t10_pi_complete - prepare PI prior returning request to the block layer
173+
* t10_pi_type1_complete - prepare PI prior returning request to the blk layer
229174
* @rq: request with PI that should be prepared
230-
* @protection_type: PI type (Type 1/Type 2/Type 3)
231-
* @intervals: total elements to prepare
175+
* @nr_bytes: total bytes to prepare
232176
*
233177
* For Type 1/Type 2, the virtual start sector is the one that was
234178
* originally submitted by the block layer for the ref_tag usage. Due to
235179
* partitioning, MD/DM cloning, etc. the actual physical start sector is
236180
* likely to be different. Since the physical start sector was submitted
237181
* to the device, we should remap it back to virtual values expected by the
238182
* block layer.
239-
*
240-
* Type 3 does not have a reference tag so no remapping is required.
241183
*/
242-
void t10_pi_complete(struct request *rq, u8 protection_type,
243-
unsigned int intervals)
184+
static void t10_pi_type1_complete(struct request *rq, unsigned int nr_bytes)
244185
{
186+
unsigned intervals = nr_bytes >> rq->q->integrity.interval_exp;
245187
const int tuple_sz = rq->q->integrity.tuple_size;
246188
u32 ref_tag = t10_pi_ref_tag(rq);
247189
struct bio *bio;
248190

249-
if (protection_type == T10_PI_TYPE3_PROTECTION)
250-
return;
251-
252191
__rq_for_each_bio(bio, rq) {
253192
struct bio_integrity_payload *bip = bio_integrity(bio);
254193
u32 virt = bip_get_seed(bip) & 0xffffffff;
@@ -276,4 +215,73 @@ void t10_pi_complete(struct request *rq, u8 protection_type,
276215
}
277216
}
278217
}
279-
EXPORT_SYMBOL(t10_pi_complete);
218+
219+
static blk_status_t t10_pi_type3_generate_crc(struct blk_integrity_iter *iter)
220+
{
221+
return t10_pi_generate(iter, t10_pi_crc_fn, T10_PI_TYPE3_PROTECTION);
222+
}
223+
224+
static blk_status_t t10_pi_type3_generate_ip(struct blk_integrity_iter *iter)
225+
{
226+
return t10_pi_generate(iter, t10_pi_ip_fn, T10_PI_TYPE3_PROTECTION);
227+
}
228+
229+
static blk_status_t t10_pi_type3_verify_crc(struct blk_integrity_iter *iter)
230+
{
231+
return t10_pi_verify(iter, t10_pi_crc_fn, T10_PI_TYPE3_PROTECTION);
232+
}
233+
234+
static blk_status_t t10_pi_type3_verify_ip(struct blk_integrity_iter *iter)
235+
{
236+
return t10_pi_verify(iter, t10_pi_ip_fn, T10_PI_TYPE3_PROTECTION);
237+
}
238+
239+
/**
240+
* Type 3 does not have a reference tag so no remapping is required.
241+
*/
242+
static void t10_pi_type3_prepare(struct request *rq)
243+
{
244+
}
245+
246+
/**
247+
* Type 3 does not have a reference tag so no remapping is required.
248+
*/
249+
static void t10_pi_type3_complete(struct request *rq, unsigned int nr_bytes)
250+
{
251+
}
252+
253+
const struct blk_integrity_profile t10_pi_type1_crc = {
254+
.name = "T10-DIF-TYPE1-CRC",
255+
.generate_fn = t10_pi_type1_generate_crc,
256+
.verify_fn = t10_pi_type1_verify_crc,
257+
.prepare_fn = t10_pi_type1_prepare,
258+
.complete_fn = t10_pi_type1_complete,
259+
};
260+
EXPORT_SYMBOL(t10_pi_type1_crc);
261+
262+
const struct blk_integrity_profile t10_pi_type1_ip = {
263+
.name = "T10-DIF-TYPE1-IP",
264+
.generate_fn = t10_pi_type1_generate_ip,
265+
.verify_fn = t10_pi_type1_verify_ip,
266+
.prepare_fn = t10_pi_type1_prepare,
267+
.complete_fn = t10_pi_type1_complete,
268+
};
269+
EXPORT_SYMBOL(t10_pi_type1_ip);
270+
271+
const struct blk_integrity_profile t10_pi_type3_crc = {
272+
.name = "T10-DIF-TYPE3-CRC",
273+
.generate_fn = t10_pi_type3_generate_crc,
274+
.verify_fn = t10_pi_type3_verify_crc,
275+
.prepare_fn = t10_pi_type3_prepare,
276+
.complete_fn = t10_pi_type3_complete,
277+
};
278+
EXPORT_SYMBOL(t10_pi_type3_crc);
279+
280+
const struct blk_integrity_profile t10_pi_type3_ip = {
281+
.name = "T10-DIF-TYPE3-IP",
282+
.generate_fn = t10_pi_type3_generate_ip,
283+
.verify_fn = t10_pi_type3_verify_ip,
284+
.prepare_fn = t10_pi_type3_prepare,
285+
.complete_fn = t10_pi_type3_complete,
286+
};
287+
EXPORT_SYMBOL(t10_pi_type3_ip);

‎drivers/md/dm-integrity.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,23 @@ static void __DEBUG_bytes(__u8 *bytes, size_t len, const char *msg, ...)
345345
#define DEBUG_bytes(bytes, len, msg, ...) do { } while (0)
346346
#endif
347347

348+
static void dm_integrity_prepare(struct request *rq)
349+
{
350+
}
351+
352+
static void dm_integrity_complete(struct request *rq, unsigned int nr_bytes)
353+
{
354+
}
355+
348356
/*
349357
* DM Integrity profile, protection is performed layer above (dm-crypt)
350358
*/
351359
static const struct blk_integrity_profile dm_integrity_profile = {
352360
.name = "DM-DIF-EXT-TAG",
353361
.generate_fn = NULL,
354362
.verify_fn = NULL,
363+
.prepare_fn = dm_integrity_prepare,
364+
.complete_fn = dm_integrity_complete,
355365
};
356366

357367
static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map);

‎drivers/nvme/host/core.c‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,6 @@ static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
666666
if (WARN_ON_ONCE(!nvme_ns_has_pi(ns)))
667667
return BLK_STS_NOTSUPP;
668668
control |= NVME_RW_PRINFO_PRACT;
669-
} else if (req_op(req) == REQ_OP_WRITE) {
670-
t10_pi_prepare(req, ns->pi_type);
671669
}
672670

673671
switch (ns->pi_type) {
@@ -690,13 +688,6 @@ static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
690688

691689
void nvme_cleanup_cmd(struct request *req)
692690
{
693-
if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ &&
694-
nvme_req(req)->status == 0) {
695-
struct nvme_ns *ns = req->rq_disk->private_data;
696-
697-
t10_pi_complete(req, ns->pi_type,
698-
blk_rq_bytes(req) >> ns->lba_shift);
699-
}
700691
if (req->rq_flags & RQF_SPECIAL_PAYLOAD) {
701692
struct nvme_ns *ns = req->rq_disk->private_data;
702693
struct page *page = req->special_vec.bv_page;

‎drivers/scsi/sd.c‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,9 +1211,6 @@ static blk_status_t sd_setup_read_write_cmnd(struct scsi_cmnd *cmd)
12111211
dix = scsi_prot_sg_count(cmd);
12121212
dif = scsi_host_dif_capable(cmd->device->host, sdkp->protection_type);
12131213

1214-
if (write && dix)
1215-
t10_pi_prepare(cmd->request, sdkp->protection_type);
1216-
12171214
if (dif || dix)
12181215
protect = sd_setup_protect_cmnd(cmd, dix, dif);
12191216
else
@@ -2054,11 +2051,6 @@ static int sd_done(struct scsi_cmnd *SCpnt)
20542051
"sd_done: completed %d of %d bytes\n",
20552052
good_bytes, scsi_bufflen(SCpnt)));
20562053

2057-
if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt) &&
2058-
good_bytes)
2059-
t10_pi_complete(SCpnt->request, sdkp->protection_type,
2060-
good_bytes / scsi_prot_interval(SCpnt));
2061-
20622054
return good_bytes;
20632055
}
20642056

‎include/linux/blkdev.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,10 +1522,14 @@ struct blk_integrity_iter {
15221522
};
15231523

15241524
typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
1525+
typedef void (integrity_prepare_fn) (struct request *);
1526+
typedef void (integrity_complete_fn) (struct request *, unsigned int);
15251527

15261528
struct blk_integrity_profile {
15271529
integrity_processing_fn *generate_fn;
15281530
integrity_processing_fn *verify_fn;
1531+
integrity_prepare_fn *prepare_fn;
1532+
integrity_complete_fn *complete_fn;
15291533
const char *name;
15301534
};
15311535

‎include/linux/t10-pi.h‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,4 @@ extern const struct blk_integrity_profile t10_pi_type1_ip;
5353
extern const struct blk_integrity_profile t10_pi_type3_crc;
5454
extern const struct blk_integrity_profile t10_pi_type3_ip;
5555

56-
#ifdef CONFIG_BLK_DEV_INTEGRITY
57-
extern void t10_pi_prepare(struct request *rq, u8 protection_type);
58-
extern void t10_pi_complete(struct request *rq, u8 protection_type,
59-
unsigned int intervals);
60-
#else
61-
static inline void t10_pi_complete(struct request *rq, u8 protection_type,
62-
unsigned int intervals)
63-
{
64-
}
65-
static inline void t10_pi_prepare(struct request *rq, u8 protection_type)
66-
{
67-
}
68-
#endif
69-
7056
#endif

0 commit comments

Comments
 (0)