Skip to content

Commit f18573a

Browse files
fujitaJens Axboe
authored andcommitted
block: move the padding adjustment to blk_rq_map_sg
blk_rq_map_user adjusts bi_size of the last bio. It breaks the rule that req->data_len (the true data length) is equal to sum(bio). It broke the scsi command completion code. commit e97a294 was introduced to fix the above issue. However, the partial completion code doesn't work with it. The commit is also a layer violation (scsi mid-layer should not know about the block layer's padding). This patch moves the padding adjustment to blk_rq_map_sg (suggested by James). The padding works like the drain buffer. This patch breaks the rule that req->data_len is equal to sum(sg), however, the drain buffer already broke it. So this patch just restores the rule that req->data_len is equal to sub(bio) without breaking anything new. Now when a low level driver needs padding, blk_rq_map_user and blk_rq_map_user_iov guarantee there's enough room for padding. blk_rq_map_sg can safely extend the last entry of a scatter list. blk_rq_map_sg must extend the last entry of a scatter list only for a request that got through bio_copy_user_iov. This patches introduces new REQ_COPY_USER flag. Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> Cc: Tejun Heo <htejun@gmail.com> Cc: Mike Christie <michaelc@cs.wisc.edu> Cc: James Bottomley <James.Bottomley@HansenPartnership.com> Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
1 parent afdc1a7 commit f18573a

4 files changed

Lines changed: 17 additions & 20 deletions

File tree

‎block/blk-map.c‎

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,8 @@ int blk_rq_map_user(struct request_queue *q, struct request *rq,
141141
ubuf += ret;
142142
}
143143

144-
/*
145-
* __blk_rq_map_user() copies the buffers if starting address
146-
* or length isn't aligned to dma_pad_mask. As the copied
147-
* buffer is always page aligned, we know that there's enough
148-
* room for padding. Extend the last bio and update
149-
* rq->data_len accordingly.
150-
*
151-
* On unmap, bio_uncopy_user() will use unmodified
152-
* bio_map_data pointed to by bio->bi_private.
153-
*/
154-
if (len & q->dma_pad_mask) {
155-
unsigned int pad_len = (q->dma_pad_mask & ~len) + 1;
156-
struct bio *tail = rq->biotail;
157-
158-
tail->bi_io_vec[tail->bi_vcnt - 1].bv_len += pad_len;
159-
tail->bi_size += pad_len;
160-
161-
rq->extra_len += pad_len;
162-
}
144+
if (!bio_flagged(bio, BIO_USER_MAPPED))
145+
rq->cmd_flags |= REQ_COPY_USER;
163146

164147
rq->buffer = rq->data = NULL;
165148
return 0;
@@ -224,6 +207,9 @@ int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
224207
return -EINVAL;
225208
}
226209

210+
if (!bio_flagged(bio, BIO_USER_MAPPED))
211+
rq->cmd_flags |= REQ_COPY_USER;
212+
227213
bio_get(bio);
228214
blk_rq_bio_prep(q, rq, bio);
229215
rq->buffer = rq->data = NULL;

‎block/blk-merge.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,15 @@ int blk_rq_map_sg(struct request_queue *q, struct request *rq,
220220
bvprv = bvec;
221221
} /* segments in rq */
222222

223+
224+
if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
225+
(rq->data_len & q->dma_pad_mask)) {
226+
unsigned int pad_len = (q->dma_pad_mask & ~rq->data_len) + 1;
227+
228+
sg->length += pad_len;
229+
rq->extra_len += pad_len;
230+
}
231+
223232
if (q->dma_drain_size && q->dma_drain_needed(rq)) {
224233
if (rq->cmd_flags & REQ_RW)
225234
memset(q->dma_drain_buffer, 0, q->dma_drain_size);

‎drivers/scsi/scsi.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ void scsi_finish_command(struct scsi_cmnd *cmd)
852852
"Notifying upper driver of completion "
853853
"(result %x)\n", cmd->result));
854854

855-
good_bytes = scsi_bufflen(cmd) + cmd->request->extra_len;
855+
good_bytes = scsi_bufflen(cmd);
856856
if (cmd->request->cmd_type != REQ_TYPE_BLOCK_PC) {
857857
drv = scsi_cmd_to_driver(cmd);
858858
if (drv->done)

‎include/linux/blkdev.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ enum rq_flag_bits {
112112
__REQ_RW_SYNC, /* request is sync (O_DIRECT) */
113113
__REQ_ALLOCED, /* request came from our alloc pool */
114114
__REQ_RW_META, /* metadata io request */
115+
__REQ_COPY_USER, /* contains copies of user pages */
115116
__REQ_NR_BITS, /* stops here */
116117
};
117118

@@ -133,6 +134,7 @@ enum rq_flag_bits {
133134
#define REQ_RW_SYNC (1 << __REQ_RW_SYNC)
134135
#define REQ_ALLOCED (1 << __REQ_ALLOCED)
135136
#define REQ_RW_META (1 << __REQ_RW_META)
137+
#define REQ_COPY_USER (1 << __REQ_COPY_USER)
136138

137139
#define BLK_MAX_CDB 16
138140

0 commit comments

Comments
 (0)