Skip to content

Commit 76c313f

Browse files
keithbuschaxboe
authored andcommitted
blk-integrity: improved sg segment mapping
Make the integrity mapping more like data mapping, blk_rq_map_sg. Use the request to validate the segment count, and update the callers so they don't have to. Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com> Signed-off-by: Keith Busch <kbusch@kernel.org> Link: https://lore.kernel.org/r/20240913191746.2628196-1-kbusch@meta.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent db5197b commit 76c313f

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

block/blk-integrity.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,20 @@ int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
6262
*
6363
* Description: Map the integrity vectors in request into a
6464
* scatterlist. The scatterlist must be big enough to hold all
65-
* elements. I.e. sized using blk_rq_count_integrity_sg().
65+
* elements. I.e. sized using blk_rq_count_integrity_sg() or
66+
* rq->nr_integrity_segments.
6667
*/
67-
int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
68-
struct scatterlist *sglist)
68+
int blk_rq_map_integrity_sg(struct request *rq, struct scatterlist *sglist)
6969
{
7070
struct bio_vec iv, ivprv = { NULL };
71+
struct request_queue *q = rq->q;
7172
struct scatterlist *sg = NULL;
73+
struct bio *bio = rq->bio;
7274
unsigned int segments = 0;
7375
struct bvec_iter iter;
7476
int prev = 0;
7577

7678
bio_for_each_integrity_vec(iv, bio, iter) {
77-
7879
if (prev) {
7980
if (!biovec_phys_mergeable(q, &ivprv, &iv))
8081
goto new_segment;
@@ -102,6 +103,12 @@ int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
102103
if (sg)
103104
sg_mark_end(sg);
104105

106+
/*
107+
* Something must have been wrong if the figured number of segment
108+
* is bigger than number of req's physical integrity segments
109+
*/
110+
BUG_ON(segments > rq->nr_integrity_segments);
111+
BUG_ON(segments > queue_max_integrity_segments(q));
105112
return segments;
106113
}
107114
EXPORT_SYMBOL(blk_rq_map_integrity_sg);

drivers/nvme/host/rdma.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,8 +1504,8 @@ static int nvme_rdma_dma_map_req(struct ib_device *ibdev, struct request *rq,
15041504
goto out_unmap_sg;
15051505
}
15061506

1507-
req->metadata_sgl->nents = blk_rq_map_integrity_sg(rq->q,
1508-
rq->bio, req->metadata_sgl->sg_table.sgl);
1507+
req->metadata_sgl->nents = blk_rq_map_integrity_sg(rq,
1508+
req->metadata_sgl->sg_table.sgl);
15091509
*pi_count = ib_dma_map_sg(ibdev,
15101510
req->metadata_sgl->sg_table.sgl,
15111511
req->metadata_sgl->nents,

drivers/scsi/scsi_lib.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,6 @@ blk_status_t scsi_alloc_sgtables(struct scsi_cmnd *cmd)
11631163

11641164
if (blk_integrity_rq(rq)) {
11651165
struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
1166-
int ivecs;
11671166

11681167
if (WARN_ON_ONCE(!prot_sdb)) {
11691168
/*
@@ -1175,19 +1174,15 @@ blk_status_t scsi_alloc_sgtables(struct scsi_cmnd *cmd)
11751174
goto out_free_sgtables;
11761175
}
11771176

1178-
ivecs = rq->nr_integrity_segments;
1179-
if (sg_alloc_table_chained(&prot_sdb->table, ivecs,
1177+
if (sg_alloc_table_chained(&prot_sdb->table,
1178+
rq->nr_integrity_segments,
11801179
prot_sdb->table.sgl,
11811180
SCSI_INLINE_PROT_SG_CNT)) {
11821181
ret = BLK_STS_RESOURCE;
11831182
goto out_free_sgtables;
11841183
}
11851184

1186-
count = blk_rq_map_integrity_sg(rq->q, rq->bio,
1187-
prot_sdb->table.sgl);
1188-
BUG_ON(count > ivecs);
1189-
BUG_ON(count > queue_max_integrity_segments(rq->q));
1190-
1185+
count = blk_rq_map_integrity_sg(rq, prot_sdb->table.sgl);
11911186
cmd->prot_sdb = prot_sdb;
11921187
cmd->prot_sdb->table.nents = count;
11931188
}

include/linux/blk-integrity.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ static inline bool queue_limits_stack_integrity_bdev(struct queue_limits *t,
2525
}
2626

2727
#ifdef CONFIG_BLK_DEV_INTEGRITY
28-
int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
29-
struct scatterlist *);
28+
int blk_rq_map_integrity_sg(struct request *, struct scatterlist *);
3029
int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
3130
int blk_rq_integrity_map_user(struct request *rq, void __user *ubuf,
3231
ssize_t bytes, u32 seed);
@@ -98,8 +97,7 @@ static inline int blk_rq_count_integrity_sg(struct request_queue *q,
9897
{
9998
return 0;
10099
}
101-
static inline int blk_rq_map_integrity_sg(struct request_queue *q,
102-
struct bio *b,
100+
static inline int blk_rq_map_integrity_sg(struct request *q,
103101
struct scatterlist *s)
104102
{
105103
return 0;

0 commit comments

Comments
 (0)