Skip to content

Commit 8188276

Browse files
fujitaJens Axboe
authored andcommitted
block: make blk_rq_map_user take a NULL user-space buffer
This patch changes blk_rq_map_user to accept a NULL user-space buffer with a READ command if rq_map_data is not NULL. Thus a caller can pass page frames to lk_rq_map_user to just set up a request and bios with page frames propely. bio_uncopy_user (called via blk_rq_unmap_user) doesn't copy data to user space with such request. Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
1 parent 839e96a commit 8188276

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

‎block/blk-map.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static int __blk_rq_unmap_user(struct bio *bio)
4242

4343
static int __blk_rq_map_user(struct request_queue *q, struct request *rq,
4444
struct rq_map_data *map_data, void __user *ubuf,
45-
unsigned int len, gfp_t gfp_mask)
45+
unsigned int len, int null_mapped, gfp_t gfp_mask)
4646
{
4747
unsigned long uaddr;
4848
struct bio *bio, *orig_bio;
@@ -63,6 +63,9 @@ static int __blk_rq_map_user(struct request_queue *q, struct request *rq,
6363
if (IS_ERR(bio))
6464
return PTR_ERR(bio);
6565

66+
if (null_mapped)
67+
bio->bi_flags |= (1 << BIO_NULL_MAPPED);
68+
6669
orig_bio = bio;
6770
blk_queue_bounce(q, &bio);
6871

@@ -111,12 +114,17 @@ int blk_rq_map_user(struct request_queue *q, struct request *rq,
111114
{
112115
unsigned long bytes_read = 0;
113116
struct bio *bio = NULL;
114-
int ret;
117+
int ret, null_mapped = 0;
115118

116119
if (len > (q->max_hw_sectors << 9))
117120
return -EINVAL;
118-
if (!len || !ubuf)
121+
if (!len)
119122
return -EINVAL;
123+
if (!ubuf) {
124+
if (!map_data || rq_data_dir(rq) != READ)
125+
return -EINVAL;
126+
null_mapped = 1;
127+
}
120128

121129
while (bytes_read != len) {
122130
unsigned long map_len, end, start;
@@ -135,7 +143,7 @@ int blk_rq_map_user(struct request_queue *q, struct request *rq,
135143
map_len -= PAGE_SIZE;
136144

137145
ret = __blk_rq_map_user(q, rq, map_data, ubuf, map_len,
138-
gfp_mask);
146+
null_mapped, gfp_mask);
139147
if (ret < 0)
140148
goto unmap_rq;
141149
if (!bio)

‎fs/bio.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,11 @@ static int __bio_copy_iov(struct bio *bio, struct bio_vec *iovecs,
547547
int bio_uncopy_user(struct bio *bio)
548548
{
549549
struct bio_map_data *bmd = bio->bi_private;
550-
int ret;
551-
552-
ret = __bio_copy_iov(bio, bmd->iovecs, bmd->sgvecs, bmd->nr_sgvecs, 1,
553-
bmd->is_our_pages);
550+
int ret = 0;
554551

552+
if (!bio_flagged(bio, BIO_NULL_MAPPED))
553+
ret = __bio_copy_iov(bio, bmd->iovecs, bmd->sgvecs,
554+
bmd->nr_sgvecs, 1, bmd->is_our_pages);
555555
bio_free_map_data(bmd);
556556
bio_put(bio);
557557
return ret;

‎include/linux/bio.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ struct bio {
108108
#define BIO_USER_MAPPED 6 /* contains user pages */
109109
#define BIO_EOPNOTSUPP 7 /* not supported */
110110
#define BIO_CPU_AFFINE 8 /* complete bio on same CPU as submitted */
111+
#define BIO_NULL_MAPPED 9 /* contains invalid user pages */
111112
#define bio_flagged(bio, flag) ((bio)->bi_flags & (1 << (flag)))
112113

113114
/*

0 commit comments

Comments
 (0)