Skip to content

Commit b77c88c

Browse files
Christoph Hellwigaxboe
authored andcommitted
block: pass a block_device and opf to bio_alloc_kiocb
Pass the block_device and operation that we plan to use this bio for to bio_alloc_kiocb to optimize the assigment. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Chaitanya Kulkarni <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 609be10 commit b77c88c

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

block/bio.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,9 @@ EXPORT_SYMBOL(bioset_init_from_src);
17311731
/**
17321732
* bio_alloc_kiocb - Allocate a bio from bio_set based on kiocb
17331733
* @kiocb: kiocb describing the IO
1734+
* @bdev: block device to allocate the bio for (can be %NULL)
17341735
* @nr_vecs: number of iovecs to pre-allocate
1736+
* @opf: operation and flags for bio
17351737
* @bs: bio_set to allocate from
17361738
*
17371739
* Description:
@@ -1742,14 +1744,14 @@ EXPORT_SYMBOL(bioset_init_from_src);
17421744
* MUST be done from process context, not hard/soft IRQ.
17431745
*
17441746
*/
1745-
struct bio *bio_alloc_kiocb(struct kiocb *kiocb, unsigned short nr_vecs,
1746-
struct bio_set *bs)
1747+
struct bio *bio_alloc_kiocb(struct kiocb *kiocb, struct block_device *bdev,
1748+
unsigned short nr_vecs, unsigned int opf, struct bio_set *bs)
17471749
{
17481750
struct bio_alloc_cache *cache;
17491751
struct bio *bio;
17501752

17511753
if (!(kiocb->ki_flags & IOCB_ALLOC_CACHE) || nr_vecs > BIO_INLINE_VECS)
1752-
return bio_alloc_bioset(NULL, nr_vecs, 0, GFP_KERNEL, bs);
1754+
return bio_alloc_bioset(bdev, nr_vecs, opf, GFP_KERNEL, bs);
17531755

17541756
cache = per_cpu_ptr(bs->cache, get_cpu());
17551757
if (cache->free_list) {
@@ -1758,12 +1760,14 @@ struct bio *bio_alloc_kiocb(struct kiocb *kiocb, unsigned short nr_vecs,
17581760
cache->nr--;
17591761
put_cpu();
17601762
bio_init(bio, nr_vecs ? bio->bi_inline_vecs : NULL, nr_vecs);
1763+
bio_set_dev(bio, bdev);
1764+
bio->bi_opf = opf;
17611765
bio->bi_pool = bs;
17621766
bio_set_flag(bio, BIO_PERCPU_CACHE);
17631767
return bio;
17641768
}
17651769
put_cpu();
1766-
bio = bio_alloc_bioset(NULL, nr_vecs, 0, GFP_KERNEL, bs);
1770+
bio = bio_alloc_bioset(bdev, nr_vecs, opf, GFP_KERNEL, bs);
17671771
bio_set_flag(bio, BIO_PERCPU_CACHE);
17681772
return bio;
17691773
}

block/fops.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,15 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
190190
struct blkdev_dio *dio;
191191
struct bio *bio;
192192
bool is_read = (iov_iter_rw(iter) == READ), is_sync;
193+
unsigned int opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb);
193194
loff_t pos = iocb->ki_pos;
194195
int ret = 0;
195196

196197
if ((pos | iov_iter_alignment(iter)) &
197198
(bdev_logical_block_size(bdev) - 1))
198199
return -EINVAL;
199200

200-
bio = bio_alloc_kiocb(iocb, nr_pages, &blkdev_dio_pool);
201+
bio = bio_alloc_kiocb(iocb, bdev, nr_pages, opf, &blkdev_dio_pool);
201202

202203
dio = container_of(bio, struct blkdev_dio, bio);
203204
atomic_set(&dio->ref, 1);
@@ -223,7 +224,6 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
223224
blk_start_plug(&plug);
224225

225226
for (;;) {
226-
bio_set_dev(bio, bdev);
227227
bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT;
228228
bio->bi_write_hint = iocb->ki_hint;
229229
bio->bi_private = dio;
@@ -238,11 +238,9 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
238238
}
239239

240240
if (is_read) {
241-
bio->bi_opf = REQ_OP_READ;
242241
if (dio->flags & DIO_SHOULD_DIRTY)
243242
bio_set_pages_dirty(bio);
244243
} else {
245-
bio->bi_opf = dio_bio_write_op(iocb);
246244
task_io_account_write(bio->bi_iter.bi_size);
247245
}
248246
if (iocb->ki_flags & IOCB_NOWAIT)
@@ -259,6 +257,8 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
259257
atomic_inc(&dio->ref);
260258
submit_bio(bio);
261259
bio = bio_alloc(GFP_KERNEL, nr_pages);
260+
bio_set_dev(bio, bdev);
261+
bio->bi_opf = opf;
262262
}
263263

264264
blk_finish_plug(&plug);
@@ -311,6 +311,8 @@ static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
311311
unsigned int nr_pages)
312312
{
313313
struct block_device *bdev = iocb->ki_filp->private_data;
314+
bool is_read = iov_iter_rw(iter) == READ;
315+
unsigned int opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb);
314316
struct blkdev_dio *dio;
315317
struct bio *bio;
316318
loff_t pos = iocb->ki_pos;
@@ -320,11 +322,10 @@ static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
320322
(bdev_logical_block_size(bdev) - 1))
321323
return -EINVAL;
322324

323-
bio = bio_alloc_kiocb(iocb, nr_pages, &blkdev_dio_pool);
325+
bio = bio_alloc_kiocb(iocb, bdev, nr_pages, opf, &blkdev_dio_pool);
324326
dio = container_of(bio, struct blkdev_dio, bio);
325327
dio->flags = 0;
326328
dio->iocb = iocb;
327-
bio_set_dev(bio, bdev);
328329
bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT;
329330
bio->bi_write_hint = iocb->ki_hint;
330331
bio->bi_end_io = blkdev_bio_end_io_async;
@@ -347,14 +348,12 @@ static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
347348
}
348349
dio->size = bio->bi_iter.bi_size;
349350

350-
if (iov_iter_rw(iter) == READ) {
351-
bio->bi_opf = REQ_OP_READ;
351+
if (is_read) {
352352
if (iter_is_iovec(iter)) {
353353
dio->flags |= DIO_SHOULD_DIRTY;
354354
bio_set_pages_dirty(bio);
355355
}
356356
} else {
357-
bio->bi_opf = dio_bio_write_op(iocb);
358357
task_io_account_write(bio->bi_iter.bi_size);
359358
}
360359

include/linux/bio.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,8 @@ extern int bioset_init_from_src(struct bio_set *bs, struct bio_set *src);
408408
struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
409409
unsigned int opf, gfp_t gfp_mask,
410410
struct bio_set *bs);
411-
struct bio *bio_alloc_kiocb(struct kiocb *kiocb, unsigned short nr_vecs,
412-
struct bio_set *bs);
411+
struct bio *bio_alloc_kiocb(struct kiocb *kiocb, struct block_device *bdev,
412+
unsigned short nr_vecs, unsigned int opf, struct bio_set *bs);
413413
struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned short nr_iovecs);
414414
extern void bio_put(struct bio *);
415415

0 commit comments

Comments
 (0)