Improve FMODE_NOWAIT coverage
Subsystems (5)
Jens Axboe
maintainercover Improve FMODE_NOWAIT coverage
Hi,
io_uring jumps through some hoops to check block devices and sockets for sanity wrt nonblocking read/write attempts. We can get rid of that if we just flag sockets and block devices as being sane in that regard. Patches 1-2 do that for sockets and block devices, and then patch 3 can remove some cruft on the io_uring side that is pretty ugly.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 1/3 net: set FMODE_NOWAIT for sockets
The socket read/write functions deal with O_NONBLOCK and IOCB_NOWAIT just fine, so we can flag them as being FMODE_NOWAIT compliant. With this, we can remove socket special casing in io_uring when checking if a file type is sane for nonblocking IO, and it's also the defined way to flag file types as such in the kernel.
--- net/socket.c | 1 + 1 file changed, 1 insertion(+)
net/socket.c +1 -0
@@ -471,6 +471,7 @@ struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)return file; } + file->f_mode |=FMODE_NOWAIT ; sock->file = file; file->private_data = sock;stream_open (SOCK_INODE (sock), file);
Signature · 1 line
2.39.2
Re: [PATCH 1/3] net: set FMODE_NOWAIT for sockets
Quoted 20 lines · Jens Axboe
The socket read/write functions deal with
O_NONBLOCKandIOCB_NOWAITjust fine, so we can flag them as beingFMODE_NOWAITcompliant. With this, we can remove socket special casing inio_uringwhen checking if a file type is sane for nonblocking IO, and it's also the defined way to flag file types as such in the kernel.Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>Signed-off-by Jens Axboe <[email protected]>--- net/socket.c | 1 + 1 file changed, 1 insertion(+)net/socket.c +0
-0
net/socket.c struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname) · line 477
Quoted 7 lines
net/socket.c +1
-0@@ -471,6 +471,7 @@ struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)return file; } + file->f_mode |=FMODE_NOWAIT ; sock->file = file; file->private_data = sock;stream_open (SOCK_INODE (sock), file);
The patch looks sane to me:
I understand the intention is merging patch via the io_uring tree? If so, no objections on my side: hopefully it should not cause any conflicts with the netdev tree.
Thanks,
Paolo
Jens Axboe
maintainerRe: [PATCH 1/3] net: set FMODE_NOWAIT for sockets
Quoted 31 lines · Paolo Abeni
Quoted 20 lines · Jens Axboe
The socket read/write functions deal with
O_NONBLOCKandIOCB_NOWAITjust fine, so we can flag them as beingFMODE_NOWAITcompliant. With this, we can remove socket special casing inio_uringwhen checking if a file type is sane for nonblocking IO, and it's also the defined way to flag file types as such in the kernel.Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>Signed-off-by Jens Axboe <[email protected]>--- net/socket.c | 1 + 1 file changed, 1 insertion(+)net/socket.c +0
-0net/socket.c struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname) · line 477
Quoted 7 lines
net/socket.c +1
-0@@ -471,6 +471,7 @@ struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)return file; } + file->f_mode |=FMODE_NOWAIT ; sock->file = file; file->private_data = sock;stream_open (SOCK_INODE (sock), file);The patch looks sane to me:
Quoted 5 lines
Reviewed-by Paolo Abeni <[email protected]>I understand the intention is merging patch via the
io_uringtree? If so, no objections on my side: hopefully it should not cause any conflicts with the netdev tree.
If it's fine with you guys, then yeah that would make my life easier. Risk of conflicts should be very low, and trivial if it does occur. Thanks for the review!
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 2/3 block: mark bdev files as FMODE_NOWAIT if underlying device supports it
In reply to Jens AxboeWe set this unconditionally, but it really should be dependent on if the underlying device is nowait compliant.
--- block/fops.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
block/fops.c +4 -1
@@ -481,7 +481,7 @@ static int blkdev_open(struct inode *inode, struct file *filp)* during an unstable branch. */ filp->f_flags |=O_LARGEFILE ; - filp->f_mode |=FMODE_NOWAIT |FMODE_BUF_RASYNC ; + filp->f_mode |=FMODE_BUF_RASYNC ;if (filp->f_flags &O_NDELAY ) filp->f_mode |=FMODE_NDELAY ;
@@ -494,6 +494,9 @@ static int blkdev_open(struct inode *inode, struct file *filp)if (IS_ERR (bdev))return PTR_ERR (bdev); +if (bdev_nowait (bdev)) + filp->f_mode |=FMODE_NOWAIT ; + filp->private_data = bdev; filp->f_mapping = bdev->bd_inode->i_mapping; filp->f_wb_err =filemap_sample_wb_err (filp->f_mapping);
Signature · 1 line
2.39.2
Re: [PATCH 2/3] block: mark bdev files as FMODE_NOWAIT if underlying device supports it
Quoted 2 lines · Jens Axboe
We set this unconditionally, but it really should be dependent on if the underlying device is nowait compliant.
Somehow I only see patch 2 of 3 of whatever series this is supposed to be in my linux-block mbox, something is broken with your patch sending script.
The change itself looks fine even standalone, though:
Jens Axboe
maintainerRe: [PATCH 2/3] block: mark bdev files as FMODE_NOWAIT if underlying device supports it
Quoted 11 lines · Christoph Hellwig
Quoted 2 lines · Jens Axboe
We set this unconditionally, but it really should be dependent on if the underlying device is nowait compliant.
Somehow I only see patch 2 of 3 of whatever series this is supposed to be in my linux-block mbox, something is broken with your patch sending script.
The change itself looks fine even standalone, though:
Reviewed-by Christoph Hellwig <[email protected]>
Thanks - the 2/3 only is on purpose, 1/3 is a networking ditto and 3/3 is just io_uring now being able to delete some code. So this one was supposed to be able to stand on its own, should've had the cover letter for everyone obviously though.
Signature · 1 line
Jens Axboe
Re: [PATCH 2/3] block: mark bdev files as FMODE_NOWAIT if underlying device supports it
So it turns out this gets into the way of my planned cleanup to move all the tatic FMODE_ flags out of this basically full field into a new static one in file_operations. Do you think it is ok to go back to always claiming FMODE_NOWAIT for block devices and then just punt for the drivers that don't support it like the patch below?
---
From 05a591ac066d9d2d57c4967bbd49c8bc63b04abf Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Tue, 20 Jun 2023 07:53:13 +0200
Subject: block: always set FMODE_NOWAIT
Block devices are the only file_operation that do not set FMODE_NOWAIT unconditionall in ->open and thus get in the way of a planned cleanup to
move this flags into a static field in file_operations. Switch to
always set FMODE_NOWAIT and just return -EAGAIN if it isn't actually supported. This just affects minor ->submit_bio based drivers as all blk-mq drivers and the important remappers (dm, md, nvme-multipath) support nowait I/O.
--- block/fops.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
block/fops.c +7 -4
@@ -505,7 +505,7 @@ static int blkdev_open(struct inode *inode, struct file *filp)* during an unstable branch. */ filp->f_flags |=O_LARGEFILE ; - filp->f_mode |=FMODE_BUF_RASYNC ; + filp->f_mode |=FMODE_BUF_RASYNC |FMODE_NOWAIT ;/* * Use the file private data to store the holder for exclusive openes.
@@ -519,9 +519,6 @@ static int blkdev_open(struct inode *inode, struct file *filp)if (IS_ERR (bdev))return PTR_ERR (bdev); -if (bdev_nowait (bdev)) - filp->f_mode |=FMODE_NOWAIT ; - filp->f_mapping = bdev->bd_inode->i_mapping; filp->f_wb_err =filemap_sample_wb_err (filp->f_mapping);return 0 ;
@@ -563,6 +560,9 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)if ((iocb->ki_flags & (IOCB_NOWAIT |IOCB_DIRECT )) ==IOCB_NOWAIT )return -EOPNOTSUPP ; +if (!bdev_nowait (bdev)) +return -EAGAIN ; + size -= iocb->ki_pos;if (iov_iter_count (from) > size) { shorted =iov_iter_count (from) - size;
@@ -585,6 +585,9 @@ static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)ssize_t ret =0 ;size_t count; +if (!bdev_nowait (bdev)) +return -EAGAIN ; +if (unlikely (pos +iov_iter_count (to) > size)) {if (pos >= size)return 0 ;
Signature · 1 line
2.39.2
Re: [PATCH 2/3] block: mark bdev files as FMODE_NOWAIT if underlying device supports it
Quoted 5 lines · Christoph Hellwig
So it turns out this gets into the way of my planned cleanup to move all the tatic
FMODE_flags out of this basically full field into a new static one infile_operations. Do you think it is ok to go back to always claimingFMODE_NOWAITfor block devices and then just punt for the drivers that don't support it like the patch below?
Except that the version I posted if of course completly broken as my testing rig told me. This is the version that actually works:
---
From 7916df7434e1570978b9c81c65aa1ec1f3396b13 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Tue, 20 Jun 2023 07:53:13 +0200
Subject: block: always set FMODE_NOWAIT
Block devices are the only file_operations that do not set FMODE_NOWAIT unconditionall in ->open and thus get in the way of a planned cleanup to
move this flags into a static field in file_operations. Switch to
always set FMODE_NOWAIT and just return -EAGAIN if it isn't actually supported. This just affects minor ->submit_bio based drivers as all blk-mq drivers and the important remappers (dm, md, nvme-multipath) support nowait I/O.
--- block/fops.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
block/fops.c +7 -4
@@ -505,7 +505,7 @@ static int blkdev_open(struct inode *inode, struct file *filp)* during an unstable branch. */ filp->f_flags |=O_LARGEFILE ; - filp->f_mode |=FMODE_BUF_RASYNC ; + filp->f_mode |=FMODE_BUF_RASYNC |FMODE_NOWAIT ;/* * Use the file private data to store the holder for exclusive openes.
@@ -519,9 +519,6 @@ static int blkdev_open(struct inode *inode, struct file *filp)if (IS_ERR (bdev))return PTR_ERR (bdev); -if (bdev_nowait (bdev)) - filp->f_mode |=FMODE_NOWAIT ; - filp->f_mapping = bdev->bd_inode->i_mapping; filp->f_wb_err =filemap_sample_wb_err (filp->f_mapping);return 0 ;
@@ -563,6 +560,9 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)if ((iocb->ki_flags & (IOCB_NOWAIT |IOCB_DIRECT )) ==IOCB_NOWAIT )return -EOPNOTSUPP ; +if ((iocb->ki_flags &IOCB_NOWAIT ) && !bdev_nowait (bdev)) +return -EAGAIN ; + size -= iocb->ki_pos;if (iov_iter_count (from) > size) { shorted =iov_iter_count (from) - size;
@@ -585,6 +585,9 @@ static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)ssize_t ret =0 ;size_t count; +if ((iocb->ki_flags &IOCB_NOWAIT ) && !bdev_nowait (bdev)) +return -EAGAIN ; +if (unlikely (pos +iov_iter_count (to) > size)) {if (pos >= size)return 0 ;
Signature · 1 line
2.39.2
Jens Axboe
maintainerRe: [PATCH 2/3] block: mark bdev files as FMODE_NOWAIT if underlying device supports it
In reply to Christoph HellwigQuoted 5 lines · Christoph Hellwig
So it turns out this gets into the way of my planned cleanup to move all the tatic
FMODE_flags out of this basically full field into a new static one infile_operations. Do you think it is ok to go back to always claimingFMODE_NOWAITfor block devices and then just punt for the drivers that don't support it like the patch below?
I think we need stronger justification than that, it's much nicer to have it in the open path than doing the same check over and over for each IO.
With your new proposed scheme, why can't the check and FMODE_NOWAIT set still be in open?
Signature · 1 line
Jens Axboe
Re: [PATCH 2/3] block: mark bdev files as FMODE_NOWAIT if underlying device supports it
Quoted 6 lines · Jens Axboe
I think we need stronger justification than that, it's much nicer to have it in the open path than doing the same check over and over for each IO.
With your new proposed scheme, why can't the check and
FMODE_NOWAITset still be in open?
Because I want to move the by now huge number of static flags out of file->f_mode and into file->f_op.flags. It's just that with this patch this one flag isn't static anymore for block devices. We could also do two sets of file operations assuming we never allow run-time changes for QUEUE_FLAG_NOWAIT. If we care about optimizing fo async I/O on the few drivers not supporting QUEUE_FLAG_NOWAIT that's probably the next best thing.
Jens Axboe
maintainerRe: [PATCH 2/3] block: mark bdev files as FMODE_NOWAIT if underlying device supports it
Quoted 8 lines · Christoph Hellwig
Quoted 6 lines · Jens Axboe
I think we need stronger justification than that, it's much nicer to have it in the open path than doing the same check over and over for each IO.
With your new proposed scheme, why can't the check and
FMODE_NOWAITset still be in open?
Quoted 7 lines
Because I want to move the by now huge number of static flags out of
file->f_modeand intofile->f_op.flags. It's just that with this patch this one flag isn't static anymore for block devices. We could also do two sets of file operations assuming we never allow run-time changes forQUEUE_FLAG_NOWAIT. If we care about optimizing fo async I/O on the few drivers not supportingQUEUE_FLAG_NOWAITthat's probably the next best thing.
Doing two sets of fops makes sense to me, and then hopefully down the line we can shave it down to 1 again once everything sets FMODE_NOWAIT.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 3/3 io_uring: rely solely on FMODE_NOWAIT
In reply to Jens AxboeNow that we have both sockets and block devices setting FMODE_NOWAIT appropriately, we can get rid of all the odd special casing in __io_file_supports_nowait() and rely soley on FMODE_NOWAIT and O_NONBLOCK rather than special case sockets and (in particular) bdevs.
--- io_uring/io_uring.c | 21 --------------------- 1 file changed, 21 deletions(-)
io_uring/io_uring.c +0 -21
@@ -1758,11 +1758,6 @@ static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) } } -static bool io_bdev_nowait (struct block_device *bdev) -{ -return !bdev ||bdev_nowait (bdev); -} -/* * If we tracked the file through the SCM inflight mechanism, we could support * any file. For now, just ensure that anything potentially problematic is done
@@ -1770,22 +1765,6 @@ static bool io_bdev_nowait(struct block_device *bdev)*/ static bool __io_file_supports_nowait (struct file *file,umode_t mode) { -if (S_ISBLK (mode)) { -if (IS_ENABLED (CONFIG_BLOCK ) && -io_bdev_nowait (I_BDEV (file->f_mapping->host))) -return true; -return false; - } -if (S_ISSOCK (mode)) -return true; -if (S_ISREG (mode)) { -if (IS_ENABLED (CONFIG_BLOCK ) && -io_bdev_nowait (file->f_inode->i_sb->s_bdev) && - !io_is_uring_fops (file)) -return true; -return false; - } -/* any ->read/write should understand O_NONBLOCK */ if (file->f_flags &O_NONBLOCK )return true;
Signature · 1 line
2.39.2
Re: [PATCH 3/3] io_uring: rely solely on FMODE_NOWAIT
Quoted 5 lines · Jens Axboe
Now that we have both sockets and block devices setting
FMODE_NOWAITappropriately, we can get rid of all the odd special casing in__io_file_supports_nowait()and rely soley onFMODE_NOWAITandO_NONBLOCKrather than special case sockets and (in particular) bdevs.
Yup, looks good to me. Thanks.
Linus