]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
block: enable per-cpu bio cache by default
authorFengnan Chang <changfengnan@bytedance.com>
Fri, 14 Nov 2025 09:21:49 +0000 (17:21 +0800)
committerJens Axboe <axboe@kernel.dk>
Thu, 4 Dec 2025 14:19:24 +0000 (07:19 -0700)
Since after commit 12e4e8c7ab59 ("io_uring/rw: enable bio caches for
IRQ rw"), bio_put is safe for task and irq context, bio_alloc_bioset is
safe for task context and no one calls in irq context, so we can enable
per cpu bio cache by default.

Benchmarked with t/io_uring and ext4+nvme:
taskset -c 6 /root/fio/t/io_uring  -p0 -d128 -b4096 -s1 -c1 -F1 -B1 -R1
-X1 -n1 -P1  /mnt/testfile
base IOPS is 562K, patch IOPS is 574K. The CPU usage of bio_alloc_bioset
decrease from 1.42% to 1.22%.

The worst case is allocate bio in CPU A but free in CPU B, still use
t/io_uring and ext4+nvme:
base IOPS is 648K, patch IOPS is 647K.

Also use fio test ext4/xfs with libaio/sync/io_uring on null_blk and
nvme, no obvious performance regression.

Signed-off-by: Fengnan Chang <changfengnan@bytedance.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/bio.c
block/fops.c
io_uring/rw.c

index 7b13bdf72de09a625c4862d98ce79468af82d4f9..fa5ff36b443f9104142c1e8405f94cb4e109e8d9 100644 (file)
@@ -517,20 +517,18 @@ struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
        if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && nr_vecs > 0))
                return NULL;
 
-       if (opf & REQ_ALLOC_CACHE) {
-               if (bs->cache && nr_vecs <= BIO_INLINE_VECS) {
-                       bio = bio_alloc_percpu_cache(bdev, nr_vecs, opf,
-                                                    gfp_mask, bs);
-                       if (bio)
-                               return bio;
-                       /*
-                        * No cached bio available, bio returned below marked with
-                        * REQ_ALLOC_CACHE to particpate in per-cpu alloc cache.
-                        */
-               } else {
-                       opf &= ~REQ_ALLOC_CACHE;
-               }
-       }
+       if (bs->cache && nr_vecs <= BIO_INLINE_VECS) {
+               opf |= REQ_ALLOC_CACHE;
+               bio = bio_alloc_percpu_cache(bdev, nr_vecs, opf,
+                                            gfp_mask, bs);
+               if (bio)
+                       return bio;
+               /*
+                * No cached bio available, bio returned below marked with
+                * REQ_ALLOC_CACHE to participate in per-cpu alloc cache.
+                */
+       } else
+               opf &= ~REQ_ALLOC_CACHE;
 
        /*
         * submit_bio_noacct() converts recursion to iteration; this means if
index 4dad9c2d5796e6c4429317e78935ae431277d4ce..4d32785b31d90eff0746bb1d435107efd540da57 100644 (file)
@@ -184,8 +184,6 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
        loff_t pos = iocb->ki_pos;
        int ret = 0;
 
-       if (iocb->ki_flags & IOCB_ALLOC_CACHE)
-               opf |= REQ_ALLOC_CACHE;
        bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL,
                               &blkdev_dio_pool);
        dio = container_of(bio, struct blkdev_dio, bio);
@@ -333,8 +331,6 @@ static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
        loff_t pos = iocb->ki_pos;
        int ret = 0;
 
-       if (iocb->ki_flags & IOCB_ALLOC_CACHE)
-               opf |= REQ_ALLOC_CACHE;
        bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL,
                               &blkdev_dio_pool);
        dio = container_of(bio, struct blkdev_dio, bio);
index 331af6bf423483b176f4f880d86dba2cb3ceb256..70ca88cc1f547144e916c839849112cfde8137d5 100644 (file)
@@ -855,7 +855,6 @@ static int io_rw_init_file(struct io_kiocb *req, fmode_t mode, int rw_type)
        ret = kiocb_set_rw_flags(kiocb, rw->flags, rw_type);
        if (unlikely(ret))
                return ret;
-       kiocb->ki_flags |= IOCB_ALLOC_CACHE;
 
        /*
         * If the file is marked O_NONBLOCK, still allow retry for it if it