From: Pavel Begunkov <asml.silence@gmail.com>
To: Jens Axboe <axboe@kernel.dk>,
linux-block@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: asml.silence@gmail.com
Subject: [PATCH 1/6] block: cache bdev in struct file for raw bdev IO
Date: Sat, 9 Oct 2021 13:25:38 +0100 [thread overview]
Message-ID: <cfc66d9946422fa1778504f976621c91be2befb5.1633781740.git.asml.silence@gmail.com> (raw)
In-Reply-To: <cover.1633781740.git.asml.silence@gmail.com>
bdev = &BDEV_I(file->f_mapping->host)->bdev
Getting struct block_device from a file requires 2 memory dereferences
as illustrated above, that takes a toll on performance, so cache it in
yet unused file->private_data. That gives a noticeable peak performance
improvement.
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
block/fops.c | 36 +++++++++++++++++++-----------------
1 file changed, 19 insertions(+), 17 deletions(-)
diff --git a/block/fops.c b/block/fops.c
index 765086d51f8b..99e699427f31 100644
--- a/block/fops.c
+++ b/block/fops.c
@@ -17,11 +17,16 @@
#include <linux/fs.h>
#include "blk.h"
-static struct inode *bdev_file_inode(struct file *file)
+static inline struct inode *bdev_file_inode(struct file *file)
{
return file->f_mapping->host;
}
+static inline struct block_device *blkdev_get_bdev(struct file *file)
+{
+ return file->private_data;
+}
+
static int blkdev_get_block(struct inode *inode, sector_t iblock,
struct buffer_head *bh, int create)
{
@@ -54,8 +59,7 @@ static void blkdev_bio_end_io_simple(struct bio *bio)
static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb,
struct iov_iter *iter, unsigned int nr_pages)
{
- struct file *file = iocb->ki_filp;
- struct block_device *bdev = I_BDEV(bdev_file_inode(file));
+ struct block_device *bdev = blkdev_get_bdev(iocb->ki_filp);
struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
loff_t pos = iocb->ki_pos;
bool should_dirty = false;
@@ -143,7 +147,7 @@ static struct bio_set blkdev_dio_pool;
static int blkdev_iopoll(struct kiocb *kiocb, struct io_batch *ib, bool wait)
{
- struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host);
+ struct block_device *bdev = blkdev_get_bdev(kiocb->ki_filp);
struct request_queue *q = bdev_get_queue(bdev);
return blk_poll(q, READ_ONCE(kiocb->ki_cookie), ib, wait);
@@ -191,9 +195,7 @@ static void blkdev_bio_end_io(struct bio *bio)
static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
unsigned int nr_pages)
{
- struct file *file = iocb->ki_filp;
- struct inode *inode = bdev_file_inode(file);
- struct block_device *bdev = I_BDEV(inode);
+ struct block_device *bdev = blkdev_get_bdev(iocb->ki_filp);
struct blk_plug plug;
struct blkdev_dio *dio;
struct bio *bio;
@@ -405,8 +407,7 @@ static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence)
static int blkdev_fsync(struct file *filp, loff_t start, loff_t end,
int datasync)
{
- struct inode *bd_inode = bdev_file_inode(filp);
- struct block_device *bdev = I_BDEV(bd_inode);
+ struct block_device *bdev = blkdev_get_bdev(filp);
int error;
error = file_write_and_wait_range(filp, start, end);
@@ -448,6 +449,8 @@ static int blkdev_open(struct inode *inode, struct file *filp)
bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode, filp);
if (IS_ERR(bdev))
return PTR_ERR(bdev);
+
+ filp->private_data = bdev;
filp->f_mapping = bdev->bd_inode->i_mapping;
filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
return 0;
@@ -455,7 +458,7 @@ static int blkdev_open(struct inode *inode, struct file *filp)
static int blkdev_close(struct inode *inode, struct file *filp)
{
- struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
+ struct block_device *bdev = blkdev_get_bdev(filp);
blkdev_put(bdev, filp->f_mode);
return 0;
@@ -463,7 +466,7 @@ static int blkdev_close(struct inode *inode, struct file *filp)
static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
{
- struct block_device *bdev = I_BDEV(bdev_file_inode(file));
+ struct block_device *bdev = blkdev_get_bdev(file);
fmode_t mode = file->f_mode;
/*
@@ -487,14 +490,14 @@ static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
*/
static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
{
- struct file *file = iocb->ki_filp;
- struct inode *bd_inode = bdev_file_inode(file);
+ struct block_device *bdev = blkdev_get_bdev(iocb->ki_filp);
+ struct inode *bd_inode = bdev->bd_inode;
loff_t size = i_size_read(bd_inode);
struct blk_plug plug;
size_t shorted = 0;
ssize_t ret;
- if (bdev_read_only(I_BDEV(bd_inode)))
+ if (bdev_read_only(bdev))
return -EPERM;
if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
@@ -526,9 +529,8 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
{
- struct file *file = iocb->ki_filp;
- struct inode *bd_inode = bdev_file_inode(file);
- loff_t size = i_size_read(bd_inode);
+ struct block_device *bdev = blkdev_get_bdev(iocb->ki_filp);
+ loff_t size = (loff_t)bdev->bd_nr_sectors << SECTOR_SHIFT;
loff_t pos = iocb->ki_pos;
size_t shorted = 0;
ssize_t ret;
--
2.33.0
next prev parent reply other threads:[~2021-10-09 12:26 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-09 12:25 [PATCH 0/6] some block optimisations Pavel Begunkov
2021-10-09 12:25 ` Pavel Begunkov [this message]
2021-10-09 16:33 ` [PATCH 1/6] block: cache bdev in struct file for raw bdev IO Jens Axboe
2021-10-11 8:26 ` Christoph Hellwig
2021-10-13 8:45 ` Pavel Begunkov
2021-10-09 12:25 ` [PATCH 2/6] block: inline BDEV_I and friends Pavel Begunkov
2021-10-11 8:20 ` Christoph Hellwig
2021-10-09 12:25 ` [PATCH 3/6] blk-mq: optimise *end_request non-stat path Pavel Begunkov
2021-10-09 12:25 ` [PATCH 4/6] block: inline hot paths of blk_account_io_*() Pavel Begunkov
2021-10-09 12:25 ` [PATCH 5/6] blk-mq: inline hot part of __blk_mq_sched_restart Pavel Begunkov
2021-10-09 12:25 ` [PATCH 6/6] block: convert ->bd_inode to container_of() Pavel Begunkov
2021-10-11 8:32 ` Christoph Hellwig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cfc66d9946422fa1778504f976621c91be2befb5.1633781740.git.asml.silence@gmail.com \
--to=asml.silence@gmail.com \
--cc=axboe@kernel.dk \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®