fs: create kiocb_{start,end}_write() helpers
Subsystems (5)
Amir Goldstein
maintainerPATCH 1/1 fs: create kiocb_{start,end}_write() helpers
aio, io_uring, cachefiles and overlayfs, all open code an ugly variant of file_{start,end}_write() to silence lockdep warnings.
Create helpers for this lockdep dance and use the helpers in all the callers.
Use a new iocb flag IOCB_WRITE_STARTED to indicate if sb_start_write() was called.
---
Christian,
This is an attempt to consolidate the open coded lockdep fooling in all those async io submitters into a single helper.
The idea to do that consolidation was suggested by Jan. The (questionable) idea to use an IOCB_ flag was mine.
This re-factoring is part of a larger vfs cleanup I am doing for fanotify permission events. The complete series is not ready for prime time yet, but this one patch is independent and I would love to get it reviewed/merged a head of the rest.
Thanks,
Amir.
fs/aio.c | 26 ++---------------- fs/cachefiles/io.c | 16 ++--------- fs/overlayfs/file.c | 15 ++++------ include/linux/fs.h | 67 +++++++++++++++++++++++++++++++++++++++++++-- io_uring/rw.c | 36 ++++-------------------- 5 files changed, 82 insertions(+), 78 deletions(-)
fs/aio.c +3 -23
@@ -1444,17 +1444,8 @@ static void aio_complete_rw(struct kiocb *kiocb, long res)if (!list_empty_careful (&iocb->ki_list))aio_remove_iocb (iocb); -if (kiocb->ki_flags &IOCB_WRITE ) { -struct inode *inode =file_inode (kiocb->ki_filp); - -/* -* Tell lockdep we inherited freeze protection from submission -* thread. -*/ -if (S_ISREG (inode->i_mode)) -__sb_writers_acquired (inode->i_sb,SB_FREEZE_WRITE ); -file_end_write (kiocb->ki_filp); - } +if (kiocb->ki_flags &IOCB_WRITE ) +kiocb_end_write (kiocb); iocb->ki_res.res = res; iocb->ki_res.res2 =0 ;
@@ -1581,18 +1572,7 @@ static int aio_write(struct kiocb *req, const struct iocb *iocb,return ret; ret =rw_verify_area (WRITE , file, &req->ki_pos,iov_iter_count (&iter));if (!ret) { -/* -* Open-code file_start_write here to grab freeze protection, -* which will be released by another thread in -* aio_complete_rw(). Fool lockdep by telling it the lock got -* released so that it doesn't complain about the held lock when -* we return to userspace. -*/ -if (S_ISREG (file_inode (file)->i_mode)) { -sb_start_write (file_inode (file)->i_sb); -__sb_writers_release (file_inode (file)->i_sb,SB_FREEZE_WRITE ); - } - req->ki_flags |=IOCB_WRITE ; +kiocb_start_write (req);aio_rw_done (req,call_write_iter (file, req, &iter)); }kfree (iovec);
fs/cachefiles/io.c +3 -13
@@ -259,9 +259,7 @@ static void cachefiles_write_complete(struct kiocb *iocb, long ret)_enter ("%ld" , ret); -/* Tell lockdep we inherited freeze protection from submission thread */ -__sb_writers_acquired (inode->i_sb,SB_FREEZE_WRITE ); -__sb_end_write (inode->i_sb,SB_FREEZE_WRITE ); +kiocb_end_write (iocb);if (ret <0 ) trace_cachefiles_io_error(object, inode, ret,
@@ -286,7 +284,6 @@ int __cachefiles_write(struct cachefiles_object *object, {struct cachefiles_cache *cache;struct cachefiles_kiocb *ki; -struct inode *inode;unsigned int old_nofs;ssize_t ret;size_t len =iov_iter_count (iter);
@@ -322,19 +319,12 @@ int __cachefiles_write(struct cachefiles_object *object, ki->iocb.ki_complete = cachefiles_write_complete;atomic_long_add (ki->b_writing, &cache->b_writing); -/* Open-code file_start_write here to grab freeze protection, which -* will be released by another thread in aio_complete_rw(). Fool -* lockdep by telling it the lock got released so that it doesn't -* complain about the held lock when we return to userspace. -*/ - inode =file_inode (file); -__sb_start_write (inode->i_sb,SB_FREEZE_WRITE ); -__sb_writers_release (inode->i_sb,SB_FREEZE_WRITE ); +kiocb_start_write (ki);get_file (ki->iocb.ki_filp);cachefiles_grab_object (object, cachefiles_obj_get_ioreq); -trace_cachefiles_write (object, inode, ki->iocb.ki_pos, len); +trace_cachefiles_write (object,file_inode (file), ki->iocb.ki_pos, len); old_nofs =memalloc_nofs_save (); ret =cachefiles_inject_write_error ();if (ret ==0 )
fs/overlayfs/file.c +6 -9
@@ -293,10 +293,7 @@ static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)if (iocb->ki_flags &IOCB_WRITE ) {struct inode *inode =file_inode (orig_iocb->ki_filp); -/* Actually acquired in ovl_write_iter() */ -__sb_writers_acquired (file_inode (iocb->ki_filp)->i_sb, -SB_FREEZE_WRITE ); -file_end_write (iocb->ki_filp); +kiocb_end_write (iocb);ovl_copyattr (inode); }
@@ -365,6 +362,9 @@ static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)return ret; } +/* IOCB flags that may be propagated to real file io */ +#define OVL_IOCB_MASK ~(IOCB_WRITE_STARTED) +static ssize_t ovl_write_iter (struct kiocb *iocb,struct iov_iter *iter) {struct file *file = iocb->ki_filp;
@@ -372,7 +372,7 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)struct fd real;const struct cred *old_cred;ssize_t ret; -int ifl = iocb->ki_flags; +int ifl = iocb->ki_flags &OVL_IOCB_MASK ;if (!iov_iter_count (iter))return 0 ;
@@ -412,10 +412,6 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)if (!aio_req) goto out; -file_start_write (real.file); -/* Pacify lockdep, same trick as done in aio_write() */ -__sb_writers_release (file_inode (real.file)->i_sb, -SB_FREEZE_WRITE ); aio_req->fd = real; real.flags =0 ; aio_req->orig_iocb = iocb;
@@ -423,6 +419,7 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) aio_req->iocb.ki_flags = ifl; aio_req->iocb.ki_complete = ovl_aio_rw_complete;refcount_set (&aio_req->ref,2 ); +kiocb_start_write (&aio_req->iocb); ret =vfs_iocb_iter_write (real.file, &aio_req->iocb, iter);ovl_aio_put (aio_req);if (ret != -EIOCBQUEUED )
include/linux/fs.h +65 -2
95 lines not on this page · raw message
io_uring/rw.c +5 -31
@@ -220,20 +220,6 @@ static bool io_rw_should_reissue(struct io_kiocb *req) }#endif -static void kiocb_end_write (struct io_kiocb *req) -{ -/* -* Tell lockdep we inherited freeze protection from submission -* thread. -*/ -if (req->flags &REQ_F_ISREG ) { -struct super_block *sb =file_inode (req->file)->i_sb; - -__sb_writers_acquired (sb,SB_FREEZE_WRITE ); -sb_end_write (sb); - } -} -/* * Trigger the notifications after having done some IO, and finish the write * accounting, if any.
@@ -243,7 +229,7 @@ static void io_req_io_end(struct io_kiocb *req) struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);if (rw->kiocb.ki_flags &IOCB_WRITE ) { -kiocb_end_write (req); +kiocb_end_write (&rw->kiocb);fsnotify_modify (req->file); }else {fsnotify_access (req->file);
@@ -313,7 +299,7 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)struct io_kiocb *req =cmd_to_io_kiocb (rw);if (kiocb->ki_flags &IOCB_WRITE ) -kiocb_end_write (req); +kiocb_end_write (kiocb);if (unlikely (res != req->cqe.res)) {if (res == -EAGAIN &&io_rw_should_reissue (req)) { req->flags |=REQ_F_REISSUE |REQ_F_PARTIAL_IO ;
@@ -902,19 +888,7 @@ int io_write(struct io_kiocb *req, unsigned int issue_flags)return ret; } -/* -* Open-code file_start_write here to grab freeze protection, -* which will be released by another thread in -* io_complete_rw(). Fool lockdep by telling it the lock got -* released so that it doesn't complain about the held lock when -* we return to userspace. -*/ -if (req->flags &REQ_F_ISREG ) { -sb_start_write (file_inode (req->file)->i_sb); -__sb_writers_release (file_inode (req->file)->i_sb, -SB_FREEZE_WRITE ); - } - kiocb->ki_flags |=IOCB_WRITE ; +kiocb_start_write (kiocb);if (likely (req->file->f_op->write_iter)) ret2 =call_write_iter (req->file, kiocb, &s->iter);
@@ -961,7 +935,7 @@ int io_write(struct io_kiocb *req, unsigned int issue_flags) io->bytes_done += ret2;if (kiocb->ki_flags &IOCB_WRITE ) -kiocb_end_write (req); +kiocb_end_write (kiocb);return ret ? ret : -EAGAIN ; } done:
@@ -972,7 +946,7 @@ int io_write(struct io_kiocb *req, unsigned int issue_flags) ret =io_setup_async_rw (req, iovec, s, false);if (!ret) {if (kiocb->ki_flags &IOCB_WRITE ) -kiocb_end_write (req); +kiocb_end_write (kiocb);return -EAGAIN ; }return ret;
Signature · 1 line
2.34.1
Jens Axboe
maintainerQuoted 36 lines · Amir Goldstein
include/linux/fs.h +36
-0+/** +* kiocb_start_write - get write access to a superblock for async file io +* @iocb: the io context we want to submit the write with +* +* This is a variant of file_start_write() for async io submission. +* Should be matched with a call to kiocb_end_write(). +*/ +static inline void kiocb_start_write (struct kiocb *iocb) +{ +struct inode *inode =file_inode (iocb->ki_filp); + + iocb->ki_flags |=IOCB_WRITE ; +if (WARN_ON_ONCE (iocb->ki_flags &IOCB_WRITE_STARTED )) +return ; +if (!S_ISREG (inode->i_mode)) +return ; +sb_start_write (inode->i_sb); +/* +* Fool lockdep by telling it the lock got released so that it +* doesn't complain about the held lock when we return to userspace. +*/ +__sb_writers_release (inode->i_sb,SB_FREEZE_WRITE ); + iocb->ki_flags |=IOCB_WRITE_STARTED ; +} + +/** +* kiocb_end_write - drop write access to a superblock after async file io +* @iocb: the io context we sumbitted the write with +* +* Should be matched with a call to kiocb_start_write(). +*/ +static inline void kiocb_end_write (struct kiocb *iocb) +{ +struct inode *inode =file_inode (iocb->ki_filp); + +if (!(iocb->ki_flags &IOCB_WRITE_STARTED ))
include/linux/fs.h static inline bool file_start_write_trylock(struct file *file) · line 2717
Quoted 10 lines
include/linux/fs.h +9
-0+return ; +if (!S_ISREG (inode->i_mode)) +return ; +/* +* Tell lockdep we inherited freeze protection from submission thread. +*/ +__sb_writers_acquired (inode->i_sb,SB_FREEZE_WRITE ); +sb_end_write (inode->i_sb); + iocb->ki_flags &= ~IOCB_WRITE_STARTED ; }
Please don't add code that dips into the inode when it's not needed for all callers.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 32 lines · Jens Axboe
Quoted 31 lines · Amir Goldstein
include/linux/fs.h +31
-0+/** +* kiocb_start_write - get write access to a superblock for async file io +* @iocb: the io context we want to submit the write with +* +* This is a variant of file_start_write() for async io submission. +* Should be matched with a call to kiocb_end_write(). +*/ +static inline void kiocb_start_write (struct kiocb *iocb) +{ +struct inode *inode =file_inode (iocb->ki_filp); + + iocb->ki_flags |=IOCB_WRITE ; +if (WARN_ON_ONCE (iocb->ki_flags &IOCB_WRITE_STARTED )) +return ; +if (!S_ISREG (inode->i_mode)) +return ; +sb_start_write (inode->i_sb); +/* +* Fool lockdep by telling it the lock got released so that it +* doesn't complain about the held lock when we return to userspace. +*/ +__sb_writers_release (inode->i_sb,SB_FREEZE_WRITE ); + iocb->ki_flags |=IOCB_WRITE_STARTED ; +} + +/** +* kiocb_end_write - drop write access to a superblock after async file io +* @iocb: the io context we sumbitted the write with +* +* Should be matched with a call to kiocb_start_write(). +*/
Quoted 8 lines
include/linux/fs.h static inline bool file_start_write_trylock(struct file *file) · line 2710
Quoted 8 lines
include/linux/fs.h +8
-0+static inline void kiocb_end_write (struct kiocb *iocb) +{ +struct inode *inode =file_inode (iocb->ki_filp); + +if (!(iocb->ki_flags &IOCB_WRITE_STARTED )) +return ; +if (!S_ISREG (inode->i_mode)) +return ;
And how would IOCB_WRITE_STARTED ever be set, if S_ISREG() isn't true?
Signature · 1 line
Jens Axboe
Amir Goldstein
maintainerQuoted 36 lines · Jens Axboe
Quoted 34 lines · Jens Axboe
Quoted 33 lines · Amir Goldstein
include/linux/fs.h +33
-0+/** +* kiocb_start_write - get write access to a superblock for async file io +* @iocb: the io context we want to submit the write with +* +* This is a variant of file_start_write() for async io submission. +* Should be matched with a call to kiocb_end_write(). +*/ +static inline void kiocb_start_write (struct kiocb *iocb) +{ +struct inode *inode =file_inode (iocb->ki_filp); + + iocb->ki_flags |=IOCB_WRITE ; +if (WARN_ON_ONCE (iocb->ki_flags &IOCB_WRITE_STARTED )) +return ; +if (!S_ISREG (inode->i_mode)) +return ; +sb_start_write (inode->i_sb); +/* +* Fool lockdep by telling it the lock got released so that it +* doesn't complain about the held lock when we return to userspace. +*/ +__sb_writers_release (inode->i_sb,SB_FREEZE_WRITE ); + iocb->ki_flags |=IOCB_WRITE_STARTED ; +} + +/** +* kiocb_end_write - drop write access to a superblock after async file io +* @iocb: the io context we sumbitted the write with +* +* Should be matched with a call to kiocb_start_write(). +*/ +static inline void kiocb_end_write (struct kiocb *iocb) +{
Quoted 8 lines
Quoted 6 lines
include/linux/fs.h static inline bool file_start_write_trylock(struct file *file) · line 2710
Quoted 6 lines
include/linux/fs.h +6
-0+struct inode *inode =file_inode (iocb->ki_filp); + +if (!(iocb->ki_flags &IOCB_WRITE_STARTED )) +return ; +if (!S_ISREG (inode->i_mode)) +return ;And how would
IOCB_WRITE_STARTEDever be set, ifS_ISREG()isn't true?
Good point.
I will pass is_reg argument from callers of kiocb_start_write() and will only check IOCB_WRITE_STARTED in kiocb_end_write().
Thanks,
Amir.
Jens Axboe
maintainerQuoted 46 lines · Amir Goldstein
Quoted 36 lines · On Tue, Aug 15, 2023 at 8:06?PM Jens Axboe <[email protected]> wrote:
Quoted 34 lines · Jens Axboe
Quoted 33 lines · Amir Goldstein
include/linux/fs.h +33
-0+/** +* kiocb_start_write - get write access to a superblock for async file io +* @iocb: the io context we want to submit the write with +* +* This is a variant of file_start_write() for async io submission. +* Should be matched with a call to kiocb_end_write(). +*/ +static inline void kiocb_start_write (struct kiocb *iocb) +{ +struct inode *inode =file_inode (iocb->ki_filp); + + iocb->ki_flags |=IOCB_WRITE ; +if (WARN_ON_ONCE (iocb->ki_flags &IOCB_WRITE_STARTED )) +return ; +if (!S_ISREG (inode->i_mode)) +return ; +sb_start_write (inode->i_sb); +/* +* Fool lockdep by telling it the lock got released so that it +* doesn't complain about the held lock when we return to userspace. +*/ +__sb_writers_release (inode->i_sb,SB_FREEZE_WRITE ); + iocb->ki_flags |=IOCB_WRITE_STARTED ; +} + +/** +* kiocb_end_write - drop write access to a superblock after async file io +* @iocb: the io context we sumbitted the write with +* +* Should be matched with a call to kiocb_start_write(). +*/ +static inline void kiocb_end_write (struct kiocb *iocb) +{Quoted 8 lines
Quoted 6 lines
include/linux/fs.h static inline bool file_start_write_trylock(struct file *file) · line 2710
Quoted 6 lines
include/linux/fs.h +6
-0+struct inode *inode =file_inode (iocb->ki_filp); + +if (!(iocb->ki_flags &IOCB_WRITE_STARTED )) +return ; +if (!S_ISREG (inode->i_mode)) +return ;And how would
IOCB_WRITE_STARTEDever be set, ifS_ISREG()isn't true?
Quoted 3 lines
Good point.
I will passis_regargument from callers ofkiocb_start_write()and will only checkIOCB_WRITE_STARTEDinkiocb_end_write().
Please don't pass in an argument that just makes the function do nothing. Just gate calling the function on it instead.
Signature · 1 line
Jens Axboe
Christian Brauner
maintainerQuoted 47 lines · Jens Axboe
Quoted 46 lines · Amir Goldstein
Quoted 44 lines · On Tue, Aug 15, 2023 at 8:06?PM Jens Axboe <[email protected]> wrote:
Quoted 32 lines · Jens Axboe
Quoted 31 lines · Amir Goldstein
include/linux/fs.h +31
-0+/** +* kiocb_start_write - get write access to a superblock for async file io +* @iocb: the io context we want to submit the write with +* +* This is a variant of file_start_write() for async io submission. +* Should be matched with a call to kiocb_end_write(). +*/ +static inline void kiocb_start_write (struct kiocb *iocb) +{ +struct inode *inode =file_inode (iocb->ki_filp); + + iocb->ki_flags |=IOCB_WRITE ; +if (WARN_ON_ONCE (iocb->ki_flags &IOCB_WRITE_STARTED )) +return ; +if (!S_ISREG (inode->i_mode)) +return ; +sb_start_write (inode->i_sb); +/* +* Fool lockdep by telling it the lock got released so that it +* doesn't complain about the held lock when we return to userspace. +*/ +__sb_writers_release (inode->i_sb,SB_FREEZE_WRITE ); + iocb->ki_flags |=IOCB_WRITE_STARTED ; +} + +/** +* kiocb_end_write - drop write access to a superblock after async file io +* @iocb: the io context we sumbitted the write with +* +* Should be matched with a call to kiocb_start_write(). +*/ Quoted 8 lines
include/linux/fs.h static inline bool file_start_write_trylock(struct file *file) · line 2710
Quoted 8 lines
include/linux/fs.h +8
-0+static inline void kiocb_end_write (struct kiocb *iocb) +{ +struct inode *inode =file_inode (iocb->ki_filp); + +if (!(iocb->ki_flags &IOCB_WRITE_STARTED )) +return ; +if (!S_ISREG (inode->i_mode)) +return ;And how would
IOCB_WRITE_STARTEDever be set, ifS_ISREG()isn't true?
Quoted 6 lines
Quoted 3 lines
Good point.
I will passis_regargument from callers ofkiocb_start_write()and will only checkIOCB_WRITE_STARTEDinkiocb_end_write().Please don't pass in an argument that just makes the function do nothing. Just gate calling the function on it instead.
Your commit about avoiding dipping into inodes unnecessarily when not all callers need it is for perf reasons or what's the worry?
Fwiw, I don't mind if we force the callers to check for prerequisites instead of the helpers. I'm just curious what the thinking behind it is.
Otherwise I think a cleanup like this might be useful.
Jens Axboe
maintainerQuoted 52 lines · Christian Brauner
Quoted 51 lines · Jens Axboe
Quoted 49 lines · Amir Goldstein
Quoted 36 lines · On Tue, Aug 15, 2023 at 8:06?PM Jens Axboe <[email protected]> wrote:
Quoted 34 lines · Jens Axboe
Quoted 33 lines · Amir Goldstein
include/linux/fs.h +33
-0+/** +* kiocb_start_write - get write access to a superblock for async file io +* @iocb: the io context we want to submit the write with +* +* This is a variant of file_start_write() for async io submission. +* Should be matched with a call to kiocb_end_write(). +*/ +static inline void kiocb_start_write (struct kiocb *iocb) +{ +struct inode *inode =file_inode (iocb->ki_filp); + + iocb->ki_flags |=IOCB_WRITE ; +if (WARN_ON_ONCE (iocb->ki_flags &IOCB_WRITE_STARTED )) +return ; +if (!S_ISREG (inode->i_mode)) +return ; +sb_start_write (inode->i_sb); +/* +* Fool lockdep by telling it the lock got released so that it +* doesn't complain about the held lock when we return to userspace. +*/ +__sb_writers_release (inode->i_sb,SB_FREEZE_WRITE ); + iocb->ki_flags |=IOCB_WRITE_STARTED ; +} + +/** +* kiocb_end_write - drop write access to a superblock after async file io +* @iocb: the io context we sumbitted the write with +* +* Should be matched with a call to kiocb_start_write(). +*/ +static inline void kiocb_end_write (struct kiocb *iocb) +{Quoted 8 lines
Quoted 6 lines
include/linux/fs.h static inline bool file_start_write_trylock(struct file *file) · line 2710
Quoted 6 lines
include/linux/fs.h +6
-0+struct inode *inode =file_inode (iocb->ki_filp); + +if (!(iocb->ki_flags &IOCB_WRITE_STARTED )) +return ; +if (!S_ISREG (inode->i_mode)) +return ;And how would
IOCB_WRITE_STARTEDever be set, ifS_ISREG()isn't true?Good point.
I will passis_regargument from callers ofkiocb_start_write()and will only checkIOCB_WRITE_STARTEDinkiocb_end_write().
Quoted 5 lines
Quoted 2 lines
Please don't pass in an argument that just makes the function do nothing. Just gate calling the function on it instead.
Your commit about avoiding dipping into inodes unnecessarily when not all callers need it is for perf reasons or what's the worry?
Right, it's to avoid pulling in dependent loads for the cases where you don't need to access it. Granted for this it's not super important as most workloads using read/write would be a regular file to begin with, and then we'd dip into the inode anyway for the lock dance. But it's good practice in general, and if you were eg using pipes then it becomes just wasted dependent loads.
Quoted 4 lines
Fwiw, I don't mind if we force the callers to check for prerequisites instead of the helpers. I'm just curious what the thinking behind it is.
Otherwise I think a cleanup like this might be useful.
Agree, it's a useful cleanup.
Signature · 1 line
Jens Axboe