what to do with IOCB_DSYNC?
Subsystems (13)
Al Viro
maintainer dde0c2e79848 "fs: add IOCB_SYNC and IOCB_DSYNC" had
done this
+ if ((file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host))
+ res |= IOCB_DSYNC;
in iocb_flags(). The first half is not a problem. However, if you look at the second one, you'll see fetches from file->f_mapping->host->i_flags and, worse yet, file->f_mapping->host->i_sb->s_flags. Now, ->f_mapping might be within the cacheline we'd already fetched from. However, ->f_mapping->host and ->f_mapping->host->i_flags are in different cachelines (i_data.host and i_flags in some struct inode instance). ->host->i_sb is in the same cacheline as ->host->i_flags, but ->host->i_sb->s_flags is certain to bring yet another cacheline into the picture.
IOW, it's not going to be cheap, no matter what we do. Moreover, that thing used to live in generic_write_sync() and used only by the ->write_iter() instances that knew they would care about it. Now it's used by all ->write_iter() and ->read_iter() callers.
How about the following:
* new flag: IOCB_DSYNC_KNOWN
* in iocb_flags(), O_DSYNC => IOCB_SYNC_KNOWN | IOCB_DSYNC
* ditto for places explicitly setting IOCB_DSYNC
* places checking IOCB_DSYNC use
static inline bool iocb_is_dsync(struct kiocb *iocb)
{
if (likely(iocb->ki_flags & IOCB_DSYNC_KNOWN))
return iocb->ki_flags & IOCB_DSYNC;
if (unlikely(IS_SYNC(iocb->ki_filp->f_mapping->host))
iocb->ki_flags |= IOCB_DSYNC_KNOWN | IOCB_DSYNC;
return true;
}
iocb->ki_flags |= IOCB_DSYNC_KNOWN;
return false;
}instead
That way init_sync_kiocb() becomes much lighter, especially
if we cache its value in struct file - there are very few places where it can change. Comments?
Christoph Hellwig
maintainerWhat about just setting O_DSYNC in f_flags at open time if a superblock has SB_SYNCHRONOUS set? That means the flag won't be picked up for open files when SB_SYNCHRONOUS is set at remount time, but I'm not even sure we need to support it for remount.
Quoted 4 lines · Christoph Hellwig
What about just setting
O_DSYNCinf_flagsat open time if a superblock hasSB_SYNCHRONOUSset? That means the flag won't be picked up for open files whenSB_SYNCHRONOUSis set at remount time, but I'm not even sure we need to support it for remount.
i suggested that to viro last night, and he pointed out that ioctl(S_SYNC) would also not be reflected in open files. that's more of a concern, i think. if only we had the revoke() infrastructure (ie could walk currently-open files on an inode).
Christoph Hellwig
maintainerQuoted 1 line · Matthew Wilcox
i suggested that to viro last night, and he pointed out that
ioctl(S_SYNC)
Where would that S_SYNC ioctl be implemented?
Quoted 4 lines · Christoph Hellwig
Quoted 1 line · Matthew Wilcox
i suggested that to viro last night, and he pointed out that
ioctl(S_SYNC)Where would that
S_SYNCioctl be implemented?
xfs_diflags_to_iflags(
if (xflags & FS_XFLAG_SYNC)
flags |= S_SYNC;
(mutatis mutandi per filesystem)
Christoph Hellwig
maintainerQuoted 11 lines · Matthew Wilcox
Quoted 4 lines · Christoph Hellwig
Quoted 1 line · Matthew Wilcox
i suggested that to viro last night, and he pointed out that
ioctl(S_SYNC)Where would that
S_SYNCioctl be implemented?xfs_diflags_to_iflags(
if (xflags &FS_XFLAG_SYNC)
flags |=S_SYNC;(mutatis mutandi per filesystem)
Ok, your description above wasn't very exact.
Anyway, that at least doesn't go out to the superblock. But if Al dislikes it we can also make generic_sync_file and friends check IS_SYNC() again. Having a single flag is kinda nice as it avoids stupid errors, but if we actually have a performance problem here (do we have any data on that?) just going back to the old way would seem like the simplest fix.
Al Viro
maintainerQuoted 7 lines · Christoph Hellwig
Quoted 6 lines · Matthew Wilcox
Quoted 4 lines · Christoph Hellwig
Quoted 1 line · Matthew Wilcox
i suggested that to viro last night, and he pointed out that
ioctl(S_SYNC)Where would that
S_SYNCioctl be implemented?
Quoted 8 lines
Quoted 5 lines
xfs_diflags_to_iflags(
if (xflags &FS_XFLAG_SYNC)
flags |=S_SYNC;(mutatis mutandi per filesystem)
Ok, your description above wasn't very exact.
Sorry - the relevant part of conversation went
< viro> willy: that's the part that can't be done at open() time < willy> because we might mount -o remount,sync after open()? < viro> ... as well as setting S_SYNC via ioctl
Should've been phrased better...
Quoted 6 lines
Anyway, that at least doesn't go out to the superblock. But if Al dislikes it we can also make
generic_sync_fileand friends checkIS_SYNC()again. Having a single flag is kinda nice as it avoids stupid errors, but if we actually have a performance problem here (do we have any data on that?) just going back to the old way would seem like the simplest fix.
IIRC, there had been profiling data posted with init_sync_kiocb()
responsible for large part of new_sync_write()/new_sync_read() overhead. Remember the threads about the use of ->read_iter()/->write_iter() being slower than having ->read()/->write()? Back in December or so, I think; one surprising part had been that large chunk of overhead sat not in suboptimal iov_iter primitives, but right in new_sync_read()/new_sync_write() with init_sync_kiocb() being the source of it,
I'd rather have a single helper for those checks, rather than
open-coding IS_SYNC() + IOCB_DSYNC in each, for obvious reasons...
Christoph Hellwig
maintainerQuoted 2 lines · Al Viro
I'd rather have a single helper for those checks, rather than
open-codingIS_SYNC()+IOCB_DSYNCin each, for obvious reasons...
Yes, I think something like:
static inline bool iocb_is_sync(struct kiocb *iocb)
{
return (iocb->ki_flags & IOCB_DSYNC) ||
S_SYNC(iocb->ki_filp->f_mapping->host);
}should do the job.
Jens Axboe
maintainerQuoted 6 lines · Christoph Hellwig
Quoted 2 lines · Al Viro
I'd rather have a single helper for those checks, rather than
open-codingIS_SYNC()+IOCB_DSYNCin each, for obvious reasons...Yes, I think something like:
Quoted 7 lines
static inline bool
iocb_is_sync(struct kiocb *iocb){ return (iocb->ki_flags & IOCB_DSYNC) || S_SYNC(iocb->ki_filp->f_mapping->host); }should do the job.
I think that's the right approach.
FWIW, my more recent testing does show the init of the iocb being the big cycler eater causing slowdowns for sync IO in conversions to the iter handlers instead of ->read/write. Would really welcome the improvements suggested here, various conversions are stalled because of it.
Signature · 1 line
Jens Axboe
Al Viro
maintainer[resurrecting an old thread]
Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Al Viro
I'd rather have a single helper for those checks, rather than
open-codingIS_SYNC()+IOCB_DSYNCin each, for obvious reasons...Yes, I think something like:
Quoted 7 lines
static inline bool
iocb_is_sync(struct kiocb *iocb){ return (iocb->ki_flags & IOCB_DSYNC) || S_SYNC(iocb->ki_filp->f_mapping->host); }should do the job.
There's a problem with that variant. Take a look at btrfs_direct_write():
const bool is_sync_write = (iocb->ki_flags & IOCB_DSYNC);
...
/*
* We remove IOCB_DSYNC so that we don't deadlock when iomap_dio_rw()
* calls generic_write_sync() (through iomap_dio_complete()), because
* that results in calling fsync (btrfs_sync_file()) which will try to
* lock the inode in exclusive/write mode.
*/
if (is_sync_write)
iocb->ki_flags &= ~IOCB_DSYNC;
...
/*
* Add back IOCB_DSYNC. Our caller, btrfs_file_write_iter(), will do
* the fsync (call generic_write_sync()).
*/
if (is_sync_write)
iocb->ki_flags |= IOCB_DSYNC;will run into trouble. How about this (completely untested):
Precalculate iocb_flags()
Store the value in file->f_i_flags; calculate at open time (do_dentry_open() for opens, alloc_file() for pipe(2)/socket(2)/etc.), update at FCNTL_SETFL time.
IOCB_DSYNC is... special in that respect; replace checks for it with an inlined helper (iocb_is_dsync()), leave the checks of underlying fs mounted with -o sync and/or inode being marked sync until then. To avoid breaking btrfs deadlock avoidance, add an explicit "no dsync" flag that would suppress IOCB_DSYNC; that way btrfs_direct_write() can set it for the duration of work where it wants to avoid generic_write_sync() triggering.
That ought to reduce the overhead of new_sync_{read,write}() quite a bit.
NEEDS TESTING; NOT FOR MERGE IN THAT FORM.
---
block/fops.c +1 -1
@@ -37,7 +37,7 @@ static unsigned int dio_bio_write_op(struct kiocb *iocb)unsigned int op =REQ_OP_WRITE |REQ_SYNC |REQ_IDLE ;/* avoid the need for a I/O completion work item */ -if (iocb->ki_flags &IOCB_DSYNC ) +if (iocb_is_dsync (iocb)) op |=REQ_FUA ;return op; }
drivers/nvme/target/io-cmd-file.c +1 -1
@@ -112,7 +112,7 @@ static ssize_t nvmet_file_submit_bvec(struct nvmet_req *req, loff_t pos, iocb->ki_pos = pos; iocb->ki_filp = req->ns->file; - iocb->ki_flags = ki_flags |iocb_flags (req->ns->file); + iocb->ki_flags = ki_flags | iocb->ki_filp->f_i_flags;return call_iter (iocb, &iter); }
fs/aio.c +1 -1
@@ -1475,7 +1475,7 @@ static int aio_prep_rw(struct kiocb *req, const struct iocb *iocb) req->ki_complete = aio_complete_rw; req->private =NULL ; req->ki_pos = iocb->aio_offset; - req->ki_flags =iocb_flags (req->ki_filp); + req->ki_flags = req->ki_filp->f_i_flags;if (iocb->aio_flags &IOCB_FLAG_RESFD ) req->ki_flags |=IOCB_EVENTFD ;if (iocb->aio_flags &IOCB_FLAG_IOPRIO ) {
fs/btrfs/file.c +4 -7
@@ -1883,7 +1883,6 @@ static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,static ssize_t btrfs_direct_write (struct kiocb *iocb,struct iov_iter *from) { -const bool is_sync_write = (iocb->ki_flags &IOCB_DSYNC );struct file *file = iocb->ki_filp;struct inode *inode =file_inode (file);struct btrfs_fs_info *fs_info =btrfs_sb (inode->i_sb);
@@ -1937,13 +1936,12 @@ static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from) }/* -* We remove IOCB_DSYNC so that we don't deadlock when iomap_dio_rw() +* We suppress IOCB_DSYNC so that we don't deadlock when iomap_dio_rw() * calls generic_write_sync() (through iomap_dio_complete()), because * that results in calling fsync (btrfs_sync_file()) which will try to * lock the inode in exclusive/write mode. */ -if (is_sync_write) - iocb->ki_flags &= ~IOCB_DSYNC ; + iocb->ki_flags |=IOCB_NO_DSYNC ;/* * The iov_iter can be mapped to the same file range we are writing to.
@@ -2004,8 +2002,7 @@ static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)* Add back IOCB_DSYNC. Our caller, btrfs_file_write_iter(), will do * the fsync (call generic_write_sync()). */ -if (is_sync_write) - iocb->ki_flags |=IOCB_DSYNC ; + iocb->ki_flags &= ~IOCB_NO_DSYNC ;/* If 'err' is -ENOTBLK then it means we must fallback to buffered IO. */ if ((err <0 && err != -ENOTBLK ) || !iov_iter_count (from))
@@ -2074,7 +2071,7 @@ ssize_t btrfs_do_write_iter(struct kiocb *iocb, struct iov_iter *from,struct file *file = iocb->ki_filp;struct btrfs_inode *inode =BTRFS_I (file_inode (file));ssize_t num_written, num_sync; -const bool sync = iocb->ki_flags &IOCB_DSYNC ; +const bool sync =iocb_is_dsync (iocb);/* * If the fs flips readonly due to some impossible error, although we
fs/direct-io.c +1 -1
@@ -1211,7 +1211,7 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,*/ if (dio->is_async &&iov_iter_rw (iter) ==WRITE ) { retval =0 ; -if (iocb->ki_flags &IOCB_DSYNC ) +if (iocb_is_dsync (iocb)) retval =dio_set_defer_completion (dio);else if (!dio->inode->i_sb->s_dio_done_wq) {/*
fs/fcntl.c +1 -0
@@ -79,6 +79,7 @@ static int setfl(int fd, struct file * filp, unsigned long arg) }spin_lock (&filp->f_lock); filp->f_flags = (arg &SETFL_MASK ) | (filp->f_flags & ~SETFL_MASK ); + filp->f_i_flags =iocb_flags (filp);spin_unlock (&filp->f_lock); out:
fs/file_table.c +1 -0
@@ -241,6 +241,7 @@ static struct file *alloc_file(const struct path *path, int flags,if ((file->f_mode &FMODE_WRITE ) &&likely (fop->write || fop->write_iter)) file->f_mode |=FMODE_CAN_WRITE ; + file->f_i_flags =iocb_flags (file); file->f_mode |=FMODE_OPENED ; file->f_op = fop;if ((file->f_mode & (FMODE_READ |FMODE_WRITE )) ==FMODE_READ )
fs/fuse/file.c +1 -1
@@ -1041,7 +1041,7 @@ static unsigned int fuse_write_flags(struct kiocb *iocb) {unsigned int flags = iocb->ki_filp->f_flags; -if (iocb->ki_flags &IOCB_DSYNC ) +if (iocb_is_dsync (iocb)) flags |=O_DSYNC ;if (iocb->ki_flags &IOCB_SYNC ) flags |=O_SYNC ;
fs/io_uring.c +1 -1
@@ -3765,7 +3765,7 @@ static int io_rw_init_file(struct io_kiocb *req, fmode_t mode)if (!io_req_ffs_set (req)) req->flags |=io_file_get_flags (file) <<REQ_F_SUPPORT_NOWAIT_BIT ; - kiocb->ki_flags =iocb_flags (file); + kiocb->ki_flags = file->f_i_flags; ret =kiocb_set_rw_flags (kiocb, req->rw.flags);if (unlikely (ret))return ret;
fs/iomap/direct-io.c +12 -9
@@ -538,17 +538,20 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, }/* for data sync or sync, we need sync completion processing */ -if (iocb->ki_flags &IOCB_DSYNC ) +if (iocb_is_dsync (iocb)) { dio->flags |=IOMAP_DIO_NEED_SYNC ; -/* -* For datasync only writes, we optimistically try using FUA for -* this IO. Any non-FUA write that occurs will clear this flag, -* hence we know before completion whether a cache flush is -* necessary. -*/ -if ((iocb->ki_flags & (IOCB_DSYNC |IOCB_SYNC )) ==IOCB_DSYNC ) - dio->flags |=IOMAP_DIO_WRITE_FUA ; +/* +* For datasync only writes, we optimistically +* try using FUA for this IO. Any non-FUA write +* that occurs will clear this flag, hence we +* know before completion whether a cache flush +* is necessary. +*/ + +if (!(iocb->ki_flags &IOCB_SYNC )) + dio->flags |=IOMAP_DIO_WRITE_FUA ; + } }if (dio_flags &IOMAP_DIO_OVERWRITE_ONLY ) {
fs/zonefs/super.c +1 -1
@@ -707,7 +707,7 @@ static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from) REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE, GFP_NOFS); bio->bi_iter.bi_sector = zi->i_zsector; bio->bi_ioprio = iocb->ki_ioprio; -if (iocb->ki_flags &IOCB_DSYNC ) +if (iocb_is_dsync (iocb)) bio->bi_opf |=REQ_FUA ; ret =bio_iov_iter_get_pages (bio, from);
include/linux/fs.h +15 -5
@@ -317,6 +317,9 @@ enum rw_hint {/* can use bio alloc cache */ #define IOCB_ALLOC_CACHE (1 << 21) +/* suppress dsync */ +#define IOCB_NO_DSYNC (1 << 31) +struct kiocb {struct file *ki_filp;
@@ -945,6 +948,7 @@ struct file {spinlock_t f_lock;atomic_long_t f_count;unsigned int f_flags; +unsigned int f_i_flags;fmode_t f_mode;struct mutex f_pos_lock;loff_t f_pos;
@@ -2191,13 +2195,11 @@ static inline bool HAS_UNMAPPED_ID(struct user_namespace *mnt_userns, !gid_valid (i_gid_into_mnt (mnt_userns, inode)); } -static inline int iocb_flags (struct file *file); -static inline void init_sync_kiocb (struct kiocb *kiocb,struct file *filp) { *kiocb = (struct kiocb ) { .ki_filp = filp, - .ki_flags =iocb_flags (filp), + .ki_flags = filp->f_i_flags, .ki_ioprio =get_current_ioprio (), }; }
@@ -2721,6 +2723,14 @@ extern int vfs_fsync(struct file *file, int datasync);extern int sync_file_range (struct file *file,loff_t offset,loff_t nbytes,unsigned int flags); +static inline bool iocb_is_dsync (const struct kiocb *iocb) +{ +if (unlikely (iocb->ki_flags &IOCB_NO_DSYNC )) +return false; +return (iocb->ki_flags &IOCB_DSYNC ) || +IS_SYNC (iocb->ki_filp->f_mapping->host); +} +/* * Sync the bytes written if this was a synchronous write. Expect ki_pos * to already be updated for the write, and will return either the amount
@@ -2728,7 +2738,7 @@ extern int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,*/ static inline ssize_t generic_write_sync (struct kiocb *iocb,ssize_t count) { -if (iocb->ki_flags &IOCB_DSYNC ) { +if (iocb_is_dsync (iocb)) {int ret =vfs_fsync_range (iocb->ki_filp, iocb->ki_pos - count, iocb->ki_pos -1 , (iocb->ki_flags &IOCB_SYNC ) ?0 :1 );
@@ -3265,7 +3275,7 @@ static inline int iocb_flags(struct file *file) res |=IOCB_APPEND ;if (file->f_flags &O_DIRECT ) res |=IOCB_DIRECT ; -if ((file->f_flags &O_DSYNC ) ||IS_SYNC (file->f_mapping->host)) +if (file->f_flags &O_DSYNC ) res |=IOCB_DSYNC ;if (file->f_flags & __O_SYNC) res |=IOCB_SYNC ;
Jens Axboe
maintainerQuoted 53 lines · Al Viro
[resurrecting an old thread]
Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Al Viro
I'd rather have a single helper for those checks, rather than
open-codingIS_SYNC()+IOCB_DSYNCin each, for obvious reasons...Yes, I think something like:
Quoted 7 lines
static inline bool
iocb_is_sync(struct kiocb *iocb){ return (iocb->ki_flags & IOCB_DSYNC) || S_SYNC(iocb->ki_filp->f_mapping->host); }should do the job.
There's a problem with that variant. Take a look at
btrfs_direct_write():const bool is_sync_write = (iocb->ki_flags & IOCB_DSYNC); ... /* * We remove IOCB_DSYNC so that we don't deadlock when iomap_dio_rw() * calls generic_write_sync() (through iomap_dio_complete()), because * that results in calling fsync (btrfs_sync_file()) which will try to * lock the inode in exclusive/write mode. */ if (is_sync_write) iocb->ki_flags &= ~IOCB_DSYNC; ... /* * Add back IOCB_DSYNC. Our caller, btrfs_file_write_iter(), will do * the fsync (call generic_write_sync()). */ if (is_sync_write) iocb->ki_flags |= IOCB_DSYNC;will run into trouble. How about this (completely untested):
Precalculate
iocb_flags()Store the value in
file->f_i_flags; calculate at open time (do_dentry_open()for opens,alloc_file()for pipe(2)/socket(2)/etc.), update atFCNTL_SETFLtime.
IOCB_DSYNCis... special in that respect; replace checks for it with an inlined helper (iocb_is_dsync()), leave the checks of underlying fs mounted with -o sync and/or inode being marked sync until then. To avoid breaking btrfs deadlock avoidance, add an explicit "no dsync" flag that would suppressIOCB_DSYNC; that waybtrfs_direct_write()can set it for the duration of work where it wants to avoidgeneric_write_sync()triggering.
Quoted 3 lines
That ought to reduce the overhead of new_sync_{read,write}() quite a bit.
NEEDS TESTING; NOT FOR MERGE IN THAT FORM.
Definitely generates better code here, unsurprisingly. Unfortunately it doesn't seem to help a whole lot for a direct comparison, though it does nudge us in the right direction.
The main issue here, using urandom and 4k reads (iter reads done, non-iter writes), the main difference seems to be that with the non-iter reads, here's our copy overhead:
+ 1.80% dd [kernel.kallsyms] [k] __arch_copy_to_user + 0.74% dd [kernel.kallsyms] [k] _copy_to_user
and with the iter variant, for the same workload, it looks like this:
+ 4.13% dd [kernel.kallsyms] [k] _copy_to_iter + 0.88% dd [kernel.kallsyms] [k] __arch_copy_to_user + 0.72% dd [kernel.kallsyms] [k] copyout
and about 1% just doing the iov_iter advance. Since this test case is a single iovec read, ran a quick test where we simply use this helper:
static size_t random_copy(void *src, int size, struct iov_iter *to)
{
const struct iovec *iov = to->iov;
size_t to_copy = min_t(size_t, size, iov->iov_len);
if (copy_to_user(iov->iov_base, src, to_copy))
return 0;
to->count -= to_copy;
return to_copy;
}rather than copy_to_iter(). That brings us within 0.8% of the non-iter read performance:
non-iter: 408MB/sec
iter: 395MB/sec -3.2%
iter+al+hack 406MB/sec -0.8%
for 32-byte reads:
non-iter 73.1MB/sec
iter: 72.1MB/sec -1.4%
iter+al+hack 72.5MB/sec -0.8%
which as mentioned in my initial email is closer than the 4k, but still sees a nice reduction in overhead.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 97 lines · Jens Axboe
Quoted 53 lines · Al Viro
[resurrecting an old thread]
Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Al Viro
I'd rather have a single helper for those checks, rather than
open-codingIS_SYNC()+IOCB_DSYNCin each, for obvious reasons...Yes, I think something like:
Quoted 7 lines
static inline bool
iocb_is_sync(struct kiocb *iocb){ return (iocb->ki_flags & IOCB_DSYNC) || S_SYNC(iocb->ki_filp->f_mapping->host); }should do the job.
There's a problem with that variant. Take a look at
btrfs_direct_write():const bool is_sync_write = (iocb->ki_flags & IOCB_DSYNC); ... /* * We remove IOCB_DSYNC so that we don't deadlock when iomap_dio_rw() * calls generic_write_sync() (through iomap_dio_complete()), because * that results in calling fsync (btrfs_sync_file()) which will try to * lock the inode in exclusive/write mode. */ if (is_sync_write) iocb->ki_flags &= ~IOCB_DSYNC; ... /* * Add back IOCB_DSYNC. Our caller, btrfs_file_write_iter(), will do * the fsync (call generic_write_sync()). */ if (is_sync_write) iocb->ki_flags |= IOCB_DSYNC;will run into trouble. How about this (completely untested):
Precalculate
iocb_flags()Store the value in
file->f_i_flags; calculate at open time (do_dentry_open()for opens,alloc_file()for pipe(2)/socket(2)/etc.), update atFCNTL_SETFLtime.
IOCB_DSYNCis... special in that respect; replace checks for it with an inlined helper (iocb_is_dsync()), leave the checks of underlying fs mounted with -o sync and/or inode being marked sync until then. To avoid breaking btrfs deadlock avoidance, add an explicit "no dsync" flag that would suppressIOCB_DSYNC; that waybtrfs_direct_write()can set it for the duration of work where it wants to avoidgeneric_write_sync()triggering.Quoted 3 lines
That ought to reduce the overhead of new_sync_{read,write}() quite a bit.
NEEDS TESTING; NOT FOR MERGE IN THAT FORM.
Definitely generates better code here, unsurprisingly. Unfortunately it doesn't seem to help a whole lot for a direct comparison, though it does nudge us in the right direction.
The main issue here, using urandom and 4k reads (iter reads done, non-iter writes), the main difference seems to be that with the non-iter reads, here's our copy overhead:
(quoted diff) +2
-0+ 1.80% dd [kernel.kallsyms] [k] __arch_copy_to_user + 0.74% dd [kernel.kallsyms] [k] _copy_to_userand with the iter variant, for the same workload, it looks like this:
(quoted diff) +3
-0+ 4.13% dd [kernel.kallsyms] [k] _copy_to_iter + 0.88% dd [kernel.kallsyms] [k] __arch_copy_to_user + 0.72% dd [kernel.kallsyms] [k] copyoutand about 1% just doing the
iov_iteradvance. Since this test case is a single iovec read, ran a quick test where we simply use this helper:static
size_trandom_copy(void *src, int size, struct iov_iter *to){ const struct iovec *iov = to->iov; size_t to_copy = min_t(size_t, size, iov->iov_len); if (copy_to_user(iov->iov_base, src, to_copy)) return 0; to->count -= to_copy; return to_copy; }rather than
copy_to_iter(). That brings us within 0.8% of the non-iter read performance:non-iter: 408MB/sec
iter: 395MB/sec -3.2%
iter+al+hack 406MB/sec -0.8%
Quoted 8 lines
for 32-byte reads:
non-iter 73.1MB/sec
iter: 72.1MB/sec -1.4%
iter+al+hack 72.5MB/sec -0.8%which as mentioned in my initial email is closer than the 4k, but still sees a nice reduction in overhead.
Experimented a bit more - if we special case nr_segs == 1 && is_iovec in _copy_to_iter():
_copy_to_iter()
{
...
if (iter_is_iovec(i)) {
might_fault();
if (i->nr_segs == 1) {
/* copy */
iov_iter_advance(i, copied);
return copied;
}
}
iterate_and_advance(i, bytes, base, len, off, ...
return bytes;
}Then we're almost on par, and it looks like we just need to special case iov_iter_advance() for the nr_segs == 1 as well to be on par. This is on top of your patch as well, fwiw.
It might make sense to special case the single segment cases, for both setup, iteration, and advancing. With that, I think we'll be where we want to be, and there will be no discernable difference between the iter paths and the old style paths.
Signature · 1 line
Jens Axboe
Christoph Hellwig
maintainerQuoted 8 lines · Jens Axboe
Then we're almost on par, and it looks like we just need to special case
iov_iter_advance()for thenr_segs== 1 as well to be on par. This is on top of your patch as well, fwiw.It might make sense to special case the single segment cases, for both setup, iteration, and advancing. With that, I think we'll be where we want to be, and there will be no discernable difference between the iter paths and the old style paths.
A while ago willy posted patches to support a new ITER type for direct userspace pointer without iov. It might be worth looking through the archives and test that.
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Jens Axboe
Then we're almost on par, and it looks like we just need to special case
iov_iter_advance()for thenr_segs== 1 as well to be on par. This is on top of your patch as well, fwiw.
Quoted 8 lines
Quoted 4 lines
It might make sense to special case the single segment cases, for both setup, iteration, and advancing. With that, I think we'll be where we want to be, and there will be no discernable difference between the iter paths and the old style paths.
A while ago willy posted patches to support a new ITER type for direct userspace pointer without iov. It might be worth looking through the archives and test that.
https://lore.kernel.org/linux-fsdevel/Yba+YSF6mkM%[email protected]/
Al Viro
maintainerQuoted 11 lines · Matthew Wilcox
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Jens Axboe
Then we're almost on par, and it looks like we just need to special case
iov_iter_advance()for thenr_segs== 1 as well to be on par. This is on top of your patch as well, fwiw.It might make sense to special case the single segment cases, for both setup, iteration, and advancing. With that, I think we'll be where we want to be, and there will be no discernable difference between the iter paths and the old style paths.
Quoted 5 lines
Quoted 3 lines
A while ago willy posted patches to support a new ITER type for direct userspace pointer without iov. It might be worth looking through the archives and test that.
https://lore.kernel.org/linux-fsdevel/Yba+YSF6mkM%[email protected]/
Direct kernel pointer, surely? And from a quick look,iov_iter_is_kaddr() checks for the wrong value...
Quoted 12 lines · Al Viro
Quoted 11 lines · Matthew Wilcox
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Jens Axboe
Then we're almost on par, and it looks like we just need to special case
iov_iter_advance()for thenr_segs== 1 as well to be on par. This is on top of your patch as well, fwiw.It might make sense to special case the single segment cases, for both setup, iteration, and advancing. With that, I think we'll be where we want to be, and there will be no discernable difference between the iter paths and the old style paths.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
A while ago willy posted patches to support a new ITER type for direct userspace pointer without iov. It might be worth looking through the archives and test that.
https://lore.kernel.org/linux-fsdevel/Yba+YSF6mkM%[email protected]/
Direct kernel pointer, surely? And from a quick look,
iov_iter_is_kaddr()checks for the wrong value...
Indeed. I didn't test it; it was a quick patch to see if the idea was worth pursuing. Neither you nor Christoph thought so at the time, so I dropped it. if there are performance improvements to be had from doing something like that, it's a more compelling idea than just "Hey, this removes a few lines of code and a bit of stack space from every caller".
Christoph Hellwig
maintainerQuoted 9 lines · Matthew Wilcox
Quoted 2 lines
Direct kernel pointer, surely? And from a quick look,
iov_iter_is_kaddr()checks for the wrong value...Indeed. I didn't test it; it was a quick patch to see if the idea was worth pursuing. Neither you nor Christoph thought so at the time, so I dropped it. if there are performance improvements to be had from doing something like that, it's a more compelling idea than just "Hey, this removes a few lines of code and a bit of stack space from every caller".
Oh, right I actually misremembered what the series did. But something similar except for user pointers might help with the performance issues that Jens sees, and if it does it might be worth it to avoid having both the legacy read/write path and the iter path in various drivers.
Jens Axboe
maintainerQuoted 11 lines · Christoph Hellwig
Quoted 9 lines · Matthew Wilcox
Quoted 2 lines
Direct kernel pointer, surely? And from a quick look,
iov_iter_is_kaddr()checks for the wrong value...Indeed. I didn't test it; it was a quick patch to see if the idea was worth pursuing. Neither you nor Christoph thought so at the time, so I dropped it. if there are performance improvements to be had from doing something like that, it's a more compelling idea than just "Hey, this removes a few lines of code and a bit of stack space from every caller".
Quoted 4 lines
Oh, right I actually misremembered what the series did. But something similar except for user pointers might help with the performance issues that Jens sees, and if it does it might be worth it to avoid having both the legacy read/write path and the iter path in various drivers.
Right, ITER_UADDR or something would useful. I'll try and test that, should be easy to wire up.
Agree with Al that it's orthogonal to the miserable kiocb init code, would be nice to get that sorted out too. Al's patch cut 1/3 of the code out of those paths for me.
Signature · 1 line
Jens Axboe
Al Viro
maintainerQuoted 12 lines · Jens Axboe
Quoted 11 lines · Christoph Hellwig
Quoted 9 lines · Matthew Wilcox
Quoted 2 lines
Direct kernel pointer, surely? And from a quick look,
iov_iter_is_kaddr()checks for the wrong value...Indeed. I didn't test it; it was a quick patch to see if the idea was worth pursuing. Neither you nor Christoph thought so at the time, so I dropped it. if there are performance improvements to be had from doing something like that, it's a more compelling idea than just "Hey, this removes a few lines of code and a bit of stack space from every caller".
Quoted 7 lines
Quoted 4 lines
Oh, right I actually misremembered what the series did. But something similar except for user pointers might help with the performance issues that Jens sees, and if it does it might be worth it to avoid having both the legacy read/write path and the iter path in various drivers.
Right,
ITER_UADDRor something would useful. I'll try and test that, should be easy to wire up.
Careful - it's not just iov_iter_advance() and __iterate_and_advance() (that one should use the same "callback" argument as iovec case). /dev/random is not the only thing we use read(2) and write(2) on...
I can cook a patch doing that, just let me get some caffeine into the bloodstream first...
Jens Axboe
maintainerQuoted 21 lines · Al Viro
Quoted 12 lines · Jens Axboe
Quoted 11 lines · Christoph Hellwig
Quoted 9 lines · Matthew Wilcox
Quoted 2 lines
Direct kernel pointer, surely? And from a quick look,
iov_iter_is_kaddr()checks for the wrong value...Indeed. I didn't test it; it was a quick patch to see if the idea was worth pursuing. Neither you nor Christoph thought so at the time, so I dropped it. if there are performance improvements to be had from doing something like that, it's a more compelling idea than just "Hey, this removes a few lines of code and a bit of stack space from every caller".
Quoted 7 lines
Quoted 4 lines
Oh, right I actually misremembered what the series did. But something similar except for user pointers might help with the performance issues that Jens sees, and if it does it might be worth it to avoid having both the legacy read/write path and the iter path in various drivers.
Right,
ITER_UADDRor something would useful. I'll try and test that, should be easy to wire up.
Quoted 6 lines
Careful - it's not just
iov_iter_advance()and__iterate_and_advance()(that one should use the same "callback" argument as iovec case)./dev/randomis not the only thing we use read(2) and write(2) on...I can cook a patch doing that, just let me get some caffeine into the bloodstream first...
Sure, if you want to cook it up. Here's what I did so far, no caffeine and not even compiled yet :-). It also doesn't do the page copy, separate advancing, etc. Was going to check all the parts in iov_iter.c that checks the type and has separate handlers.
The read vs write const and not is not great...
fs/read_write.c +2 -4
@@ -389,14 +389,13 @@ EXPORT_SYMBOL(rw_verify_area);static ssize_t new_sync_read (struct file *filp,char __user *buf,size_t len,loff_t *ppos) { -struct iovec iov = { .iov_base = buf, .iov_len = len };struct kiocb kiocb;struct iov_iter iter;ssize_t ret;init_sync_kiocb (&kiocb, filp); kiocb.ki_pos = (ppos ? *ppos :0 ); -iov_iter_init (&iter,READ , &iov,1 , len); +uaddr_iter_init (&iter,READ , buf, len); ret =call_read_iter (filp, &kiocb, &iter);BUG_ON (ret == -EIOCBQUEUED );
@@ -492,14 +491,13 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)static ssize_t new_sync_write (struct file *filp,const char __user *buf,size_t len,loff_t *ppos) { -struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };struct kiocb kiocb;struct iov_iter iter;ssize_t ret;init_sync_kiocb (&kiocb, filp); kiocb.ki_pos = (ppos ? *ppos :0 ); -iov_iter_init (&iter,WRITE , &iov,1 , len); +uaddr_iter_init (&iter,WRITE , (void __user *) buf, len); ret =call_write_iter (filp, &kiocb, &iter);BUG_ON (ret == -EIOCBQUEUED );
include/linux/uio.h +19 -0
@@ -21,6 +21,7 @@ struct kvec {enum iter_type {/* iter types */ ITER_IOVEC , +ITER_UADDR ,ITER_KVEC ,ITER_BVEC ,ITER_PIPE ,
@@ -42,6 +43,7 @@ struct iov_iter {size_t count;union {const struct iovec *iov; +void __user *uaddr;const struct kvec *kvec;const struct bio_vec *bvec;struct xarray *xarray;
@@ -75,6 +77,11 @@ static inline bool iter_is_iovec(const struct iov_iter *i)return iov_iter_type (i) ==ITER_IOVEC ; } +static inline bool iter_is_uaddr (const struct iov_iter *i) +{ +return iov_iter_type (i) ==ITER_UADDR ; +} +static inline bool iov_iter_is_kvec (const struct iov_iter *i) {return iov_iter_type (i) ==ITER_KVEC ;
@@ -241,6 +248,18 @@ void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state);const void *dup_iter (struct iov_iter *new,struct iov_iter *old,gfp_t flags); +static inline void uaddr_iter_init (struct iov_iter *iter,int rw, +void __user *uaddr,size_t len) +{ + iter->iter_type =ITER_UADDR ; + iter->nofault = false; + iter->data_source = rw; + iter->iov_offset =0 ; + iter->count = len; + iter->uaddr = uaddr; + iter->nr_segs =0 ; +} +static inline size_t iov_iter_count (const struct iov_iter *i) {return i->count;
lib/iov_iter.c +26 -2
@@ -38,6 +38,22 @@ n = off; \ } +#define iterate_uaddr (i, n, base, len, off,STEP ) { \ + void __user *base; \ + size_t len; \ + size_t off = 0; \ + size_t skip = i->iov_offset; \ + len = min(n, i->count - skip); \ + if (likely(len)) { \ + base = i->uaddr + skip; \ + len -= (STEP); \ + off += len; \ + skip += len; \ + } \ + i->iov_offset = skip; \ + n = off; \ +} +#define iterate_bvec (i, n, base, len, off, p,STEP ) { \ size_t off = 0; \ unsigned skip = i->iov_offset; \
@@ -118,6 +134,8 @@ __out: \ iov, (I)) \ i->nr_segs -= iov - i->iov; \ i->iov = iov; \ + }else if (iter_is_uaddr (i)) { \ + iterate_uaddr(i, n, base, len, off, (I)) \ }else if (iov_iter_is_bvec (i)) { \const struct bio_vec *bvec = i->bvec; \void *base; \
@@ -662,7 +680,7 @@ size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) {if (unlikely (iov_iter_is_pipe (i)))return copy_pipe_to_iter (addr, bytes, i); -if (iter_is_iovec (i)) +if (iter_is_iovec (i) ||iter_is_uaddr (i))might_fault (); iterate_and_advance(i, bytes, base, len, off, copyout(base, addr + off, len),
@@ -744,7 +762,7 @@ size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i) {if (unlikely (iov_iter_is_pipe (i)))return copy_mc_pipe_to_iter (addr, bytes, i); -if (iter_is_iovec (i)) +if (iter_is_iovec (i) ||iter_is_uaddr (i))might_fault (); __iterate_and_advance(i, bytes, base, len, off, copyout_mc(base, addr + off, len),
@@ -1061,6 +1079,10 @@ static void iov_iter_iovec_advance(struct iov_iter *i, size_t size) i->iov = iov; } +static void iter_uaddr_advance (struct iov_iter *i,size_t size) +{ +} +void iov_iter_advance (struct iov_iter *i,size_t size) {if (unlikely (i->count < size))
@@ -1068,6 +1090,8 @@ void iov_iter_advance(struct iov_iter *i, size_t size)if (likely (iter_is_iovec (i) ||iov_iter_is_kvec (i))) {/* iovec and kvec have identical layouts */ iov_iter_iovec_advance (i, size); + }else if (iter_is_uaddr (i)) { +iter_uaddr_advance (i, size); }else if (iov_iter_is_bvec (i)) {iov_iter_bvec_advance (i, size); }else if (iov_iter_is_pipe (i)) {
Signature · 1 line
Jens Axboe
Al Viro
maintainerlib/iov_iter.c static void iov_iter_iovec_advance(struct iov_iter *i, size_t size) · line 1084
Quoted 3 lines · Jens Axboe
lib/iov_iter.c +3
-0+static void iter_uaddr_advance (struct iov_iter *i,size_t size) +{ +}
How could that possibly work? At the very least you want to do what xarray does - you *must* decrement ->count and shift ->iov_offset. Matter of fact, I'd simply go with a bit of reorder and had it go for if (iter_is_uaddr(i) || iter_is_xarray(i)) in there...
Jens Axboe
maintainerQuoted 9 lines · Al Viro
lib/iov_iter.c static void iov_iter_iovec_advance(struct iov_iter *i, size_t size) · line 1084
Quoted 3 lines · Jens Axboe
lib/iov_iter.c +3
-0+static void iter_uaddr_advance (struct iov_iter *i,size_t size) +{ +}How could that possibly work? At the very least you want to do what xarray does - you *must* decrement ->count and shift ->iov_offset. Matter of fact, I'd simply go with a bit of reorder and had it go for if (
iter_is_uaddr(i)||iter_is_xarray(i)) in there...
It's just a stub, you said you'd want to do it, so I just dropped what I had in the email. As I said, it's not even compiled yet, let alone complete or tested.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 11 lines · Jens Axboe
Quoted 9 lines · Al Viro
Review
Quoted 3 lines · Jens Axboe
(quoted diff) +3
-0+static void iter_uaddr_advance(struct iov_iter *i, size_t size) +{ +}How could that possibly work? At the very least you want to do what xarray does - you *must* decrement ->count and shift ->iov_offset. Matter of fact, I'd simply go with a bit of reorder and had it go for if (
iter_is_uaddr(i)||iter_is_xarray(i)) in there...
Quoted 3 lines
It's just a stub, you said you'd want to do it, so I just dropped what I had in the email. As I said, it's not even compiled yet, let alone complete or tested.
Had a bit more time, this one actually boots and uses ITER_UADDR for new_sync_read() and new_sync_write().
A few notes:
- Why aren't we using iter->nofault for the might_fault() check? Didn't matter too much before, but it's nice to consolidate with multiple iov_iter types that can fault.
- Since checks use iter_is_iovec() to mean "this is user data", we should probably add another bool for that since there's a hole.
- There are some non-core spots that check iter type, eg shmem and others. Somewhat annoying we don't have core helpers for all things.
No real testing done on this, outside of just checking that it actually boots in my vm. No guarantees it doesn't have weird corner cases and breakages...
fs/read_write.c +2 -4
@@ -389,14 +389,13 @@ EXPORT_SYMBOL(rw_verify_area);static ssize_t new_sync_read (struct file *filp,char __user *buf,size_t len,loff_t *ppos) { -struct iovec iov = { .iov_base = buf, .iov_len = len };struct kiocb kiocb;struct iov_iter iter;ssize_t ret;init_sync_kiocb (&kiocb, filp); kiocb.ki_pos = (ppos ? *ppos :0 ); -iov_iter_init (&iter,READ , &iov,1 , len); +uaddr_iter_init (&iter,READ , buf, len); ret =call_read_iter (filp, &kiocb, &iter);BUG_ON (ret == -EIOCBQUEUED );
@@ -492,14 +491,13 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)static ssize_t new_sync_write (struct file *filp,const char __user *buf,size_t len,loff_t *ppos) { -struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };struct kiocb kiocb;struct iov_iter iter;ssize_t ret;init_sync_kiocb (&kiocb, filp); kiocb.ki_pos = (ppos ? *ppos :0 ); -iov_iter_init (&iter,WRITE , &iov,1 , len); +uaddr_iter_init (&iter,WRITE , (void __user *) buf, len); ret =call_write_iter (filp, &kiocb, &iter);BUG_ON (ret == -EIOCBQUEUED );
include/linux/uio.h +10 -0
@@ -21,6 +21,7 @@ struct kvec {enum iter_type {/* iter types */ ITER_IOVEC , +ITER_UADDR ,ITER_KVEC ,ITER_BVEC ,ITER_PIPE ,
@@ -42,12 +43,14 @@ struct iov_iter {size_t count;union {const struct iovec *iov; +void __user *uaddr;const struct kvec *kvec;const struct bio_vec *bvec;struct xarray *xarray;struct pipe_inode_info *pipe; };union { +size_t uaddr_len;unsigned long nr_segs;struct {unsigned int head;
@@ -75,6 +78,11 @@ static inline bool iter_is_iovec(const struct iov_iter *i)return iov_iter_type (i) ==ITER_IOVEC ; } +static inline bool iter_is_uaddr (const struct iov_iter *i) +{ +return iov_iter_type (i) ==ITER_UADDR ; +} +static inline bool iov_iter_is_kvec (const struct iov_iter *i) {return iov_iter_type (i) ==ITER_KVEC ;
@@ -221,6 +229,8 @@ size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i);size_t iov_iter_zero (size_t bytes,struct iov_iter *);unsigned long iov_iter_alignment (const struct iov_iter *i);unsigned long iov_iter_gap_alignment (const struct iov_iter *i); +void uaddr_iter_init (struct iov_iter *iter,int rw,void __user *uaddr, +size_t len);void iov_iter_init (struct iov_iter *i,unsigned int direction,const struct iovec *iov,unsigned long nr_segs,size_t count); void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec,
lib/iov_iter.c +82 -8
193 lines not on this page · raw message
Signature · 1 line
Jens Axboe
Al Viro
maintainerinclude/linux/uio.h struct iov_iter { · line 53
Quoted 2 lines · Jens Axboe
include/linux/uio.h +1
-0union { +size_t uaddr_len;
WTF for?
lib/iov_iter.c line 44
Quoted 4 lines
lib/iov_iter.c +4
-0+#define iterate_uaddr (i, n, base, len, off,STEP ) { \ + size_t off = 0; \ + size_t skip = i->iov_offset; \ + len = min(n, i->uaddr_len - skip); \
How would you ever get offset past the size? Note that we do keep track of amount of space left...
lib/iov_iter.c size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) · line 699
Quoted 3 lines
lib/iov_iter.c +1
-1-if (iter_is_iovec (i)) +if (!i->nofault)might_fault ();
Nope. Sorry, but by default nofault will be false for *all* types. It's not "I won't fault", it's "pass FOLL_NOFAULT to g_u_p".
lib/iov_iter.c static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes · line 890
Quoted 6 lines
lib/iov_iter.c +2
-1-if (iov_iter_is_bvec (i) ||iov_iter_is_kvec (i) ||iov_iter_is_xarray (i)) { +if (iov_iter_is_bvec (i) ||iov_iter_is_kvec (i) || +iter_is_uaddr (i) ||iov_iter_is_xarray (i)) {void *kaddr =kmap_local_page (page);size_t wanted =_copy_to_iter (kaddr + offset, bytes, i);kunmap_local (kaddr);
Nope. This is bogus - _copy_to_iter() *can* legitimately fault for those, so the same considerations as for iovec apply.
lib/iov_iter.c size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, · line 938
Quoted 7 lines
lib/iov_iter.c +2
-1@@ -900,7 +934,8 @@ size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,return 0 ;if (likely (iter_is_iovec (i)))return copy_page_from_iter_iovec (page, offset, bytes, i); -if (iov_iter_is_bvec (i) ||iov_iter_is_kvec (i) ||iov_iter_is_xarray (i)) { +if (iov_iter_is_bvec (i) ||iov_iter_is_kvec (i) || +iter_is_uaddr (i) ||iov_iter_is_xarray (i)) {
Ditto.
lib/iov_iter.c unsigned long iov_iter_alignment(const struct iov_iter *i) · line 1375
Quoted 9 lines
lib/iov_iter.c +5
-0@@ -1319,6 +1368,14 @@ unsigned long iov_iter_alignment(const struct iov_iter *i)if (iov_iter_is_bvec (i))return iov_iter_alignment_bvec (i); +if (iter_is_uaddr (i)) { +size_t len = i->count - i->iov_offset; + +if (len) +return (unsigned long ) i->uaddr + i->iov_offset;
Huh? iov_iter_alignment() wants the worse of beginning and end, if there's any data at all.
lib/iov_iter.c ssize_t iov_iter_get_pages(struct iov_iter *i, · line 1588
Quoted 6 lines
lib/iov_iter.c +2
-0@@ -1527,6 +1584,9 @@ ssize_t iov_iter_get_pages(struct iov_iter *i,if (!maxsize)return 0 ; +if (WARN_ON_ONCE (iter_is_uaddr (i))) +return 0 ;
So no DIO read(2) or write(2) anymore? Harsh...
lib/iov_iter.c ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, · line 1716
Quoted 6 lines
lib/iov_iter.c +2
-0@@ -1652,6 +1712,8 @@ ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, maxsize = i->count;if (!maxsize)return 0 ; +if (WARN_ON_ONCE (iter_is_uaddr (i))) +return 0 ;
Ditto.
lib/iov_iter.c int iov_iter_npages(const struct iov_iter *i, int maxpages) · line 1897
Quoted 8 lines
lib/iov_iter.c +8
-0+if (iter_is_uaddr (i)) { +unsigned long uaddr = (unsigned long ) i->uaddr; +unsigned long start, end; + + end = (uaddr + i->count - i->iov_offset +PAGE_SIZE -1 ) + >>PAGE_SHIFT ; + start = uaddr >>PAGE_SHIFT ; +return min_t (int, end - start, maxpages);
Nope. The value is wrong (sanity check - decrement the base and increment iov_offset by the same amount; the range of addresses is the same, and the same should go for the result; yours fails that test).
FWIW, here it's
if (likely(iter_is_ubuf(i))) {
unsigned offs = offset_in_page(i->ubuf + i->iov_offset);
int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE);
return min(npages, maxpages);
}BTW, you've missed iov_iter_gap_alignment()...
Jens Axboe
maintainerIt was sent out as a discussion point, it's not a submission and it's by no means complete (as mentioned!). If you're working on this, I'd be happy to drop it, it's not like I really enjoy the iov_iter code... And it sounds like you are?
Signature · 1 line
Jens Axboe
Al Viro
maintainerQuoted 4 lines · Jens Axboe
It was sent out as a discussion point, it's not a submission and it's by no means complete (as mentioned!). If you're working on this, I'd be happy to drop it, it's not like I really enjoy the
iov_itercode... And it sounds like you are?
*snort*
Yes, I am working on it. As for enjoying that thing... I'm not fond of forests of macros, to put it mildly. Even in the trimmed form it still stinks, and places like copy_page_to_iter for iovec are still fucking awful wrt misguided microoptimization attempts - mine, at that, so I've nobody else to curse ;-/
Jens Axboe
maintainerQuoted 9 lines · Al Viro
Quoted 4 lines · Jens Axboe
It was sent out as a discussion point, it's not a submission and it's by no means complete (as mentioned!). If you're working on this, I'd be happy to drop it, it's not like I really enjoy the
iov_itercode... And it sounds like you are?*snort*
Yes, I am working on it. As for enjoying that thing... I'm not fond of
OK great, I'll abandon this sandbox. Let me know when you have something to test, and I can compare some numbers between non-iter, iter, iter-with-ubuf.
Quoted 4 lines
forests of macros, to put it mildly. Even in the trimmed form it still stinks, and places like
copy_page_to_iterfor iovec are still fucking awful wrt misguided microoptimization attempts - mine, at that, so I've nobody else to curse ;-/
And it's not even clear which ones won't work with the generic variants, and which ones are just an optimization. I suspect a lot of the icache bloat from this just makes things worse...
Signature · 1 line
Jens Axboe
Al Viro
maintainerQuoted 15 lines · Jens Axboe
Quoted 9 lines · Al Viro
Quoted 4 lines · Jens Axboe
It was sent out as a discussion point, it's not a submission and it's by no means complete (as mentioned!). If you're working on this, I'd be happy to drop it, it's not like I really enjoy the
iov_itercode... And it sounds like you are?*snort*
Yes, I am working on it. As for enjoying that thing... I'm not fond of
OK great, I'll abandon this sandbox. Let me know when you have something to test, and I can compare some numbers between non-iter, iter, iter-with-ubuf.
Quoted 8 lines
Quoted 4 lines
forests of macros, to put it mildly. Even in the trimmed form it still stinks, and places like
copy_page_to_iterfor iovec are still fucking awful wrt misguided microoptimization attempts - mine, at that, so I've nobody else to curse ;-/And it's not even clear which ones won't work with the generic variants, and which ones are just an optimization. I suspect a lot of the icache bloat from this just makes things worse...
???
copy_page_{from,to}_iter() iovec side is needed not for the sake of optimizations - if you look at the generic variant, you'll see that it's basically "kmap_local_page + copy_{to,from}_iter() for the contents + kunmap_local_page", which obviously relies upon the copy_{to,from}_iter() being non-blocking. So iovec part of it genuinely needs to differ from the generic variant; it's just that on top of that it had been (badly) microoptimized. So were iterators, but that got at least somewhat cleaned up a while ago. And no, turning that into indirect calls ended up with arseloads of overhead, more's the pity...
Anyway, at the moment I have something that builds; hadn't tried to boot it yet.
Jens Axboe
maintainerQuoted 27 lines · Al Viro
Quoted 15 lines · Jens Axboe
Quoted 9 lines · Al Viro
Quoted 4 lines · Jens Axboe
It was sent out as a discussion point, it's not a submission and it's by no means complete (as mentioned!). If you're working on this, I'd be happy to drop it, it's not like I really enjoy the
iov_itercode... And it sounds like you are?*snort*
Yes, I am working on it. As for enjoying that thing... I'm not fond of
OK great, I'll abandon this sandbox. Let me know when you have something to test, and I can compare some numbers between non-iter, iter, iter-with-ubuf.
Quoted 8 lines
Quoted 4 lines
forests of macros, to put it mildly. Even in the trimmed form it still stinks, and places like
copy_page_to_iterfor iovec are still fucking awful wrt misguided microoptimization attempts - mine, at that, so I've nobody else to curse ;-/And it's not even clear which ones won't work with the generic variants, and which ones are just an optimization. I suspect a lot of the icache bloat from this just makes things worse...
???
Quoted 7 lines
copy_page_{from,to}_iter() iovec side is needed not for the sake of optimizations - if you look at the generic variant, you'll see that it's basically "
kmap_local_page+ copy_{to,from}_iter() for the contents +kunmap_local_page", which obviously relies upon the copy_{to,from}_iter() being non-blocking. So iovec part of it genuinely needs to differ from the generic variant; it's just that on top of that it had been (badly) microoptimized. So were iterators, but that got at least somewhat cleaned
Right, I'm saying it's not _immediately_ clear which cases are what when reading the code.
Quoted 2 lines
up a while ago. And no, turning that into indirect calls ended up with arseloads of overhead, more's the pity...
It's a shame, since indirect calls make for nicer code, but it's always been slower and these days even more so.
Quoted 2 lines
Anyway, at the moment I have something that builds; hadn't tried to boot it yet.
Nice!
Signature · 1 line
Jens Axboe
Al Viro
maintainerQuoted 6 lines · Jens Axboe
Right, I'm saying it's not _immediately_ clear which cases are what when reading the code.
Quoted 2 lines
up a while ago. And no, turning that into indirect calls ended up with arseloads of overhead, more's the pity...
Quoted 7 lines
It's a shame, since indirect calls make for nicer code, but it's always been slower and these days even more so.
Quoted 2 lines
Anyway, at the moment I have something that builds; hadn't tried to boot it yet.
Nice!
Boots and survives LTP and xfstests... Current variant is in vfs.git#work.iov_iter (head should be at 27fa77a9829c). I have *not* looked into the code generation in primitives; the likely/unlikely on those cascades of ifs need rethinking.
I hadn't added ITER_KBUF (or KADDR, whatever); should be an easy incremental, though.
At the moment it's carved up into 6 commits:
btrfs_direct_write(): cleaner way to handle generic_write_sync() suppression struct file: use anonymous union member for rcuhead and llist
iocb: delay evaluation of IS_SYNC(...) until we want to check IOCB_DSYNC keep iocb_flags() result cached in struct file
new iov_iter flavour - ITER_UBUF
switch new_sync_{read,write}() to ITER_UBUF
Review and testing would be welcome, but it's obviously not this window fodder.
Jens Axboe
maintainerQuoted 14 lines · Al Viro
Quoted 12 lines · Jens Axboe
Right, I'm saying it's not _immediately_ clear which cases are what when reading the code.
Quoted 2 lines
up a while ago. And no, turning that into indirect calls ended up with arseloads of overhead, more's the pity...
It's a shame, since indirect calls make for nicer code, but it's always been slower and these days even more so.
Quoted 2 lines
Anyway, at the moment I have something that builds; hadn't tried to boot it yet.
Quoted 6 lines
Quoted 1 line
Nice!
Boots and survives LTP and xfstests... Current variant is in vfs.git#work.iov_iter (head should be at
27fa77a9829c). I have *not* looked into the code generation in primitives; the likely/unlikely on those cascades of ifs need rethinking.
I noticed too. Haven't fiddled much in iov_iter.c, but for uio.h I had the below. iov_iter.c is a worse "offender" though, with 53 unlikely and 22 likely annotations...
Quoted 11 lines
I hadn't added
ITER_KBUF(or KADDR, whatever); should be an easy incremental, though.At the moment it's carved up into 6 commits:
btrfs_direct_write(): cleaner way to handlegeneric_write_sync()suppression struct file: use anonymous union member for rcuhead and llist
iocb: delay evaluation ofIS_SYNC(...)until we want to checkIOCB_DSYNCkeepiocb_flags()result cached in struct file
newiov_iterflavour -ITER_UBUF
switch new_sync_{read,write}() toITER_UBUF
Quoted 2 lines
Review and testing would be welcome, but it's obviously not this window fodder.
I'll take a look at it tomorrow, but did run a quick test on my vm and it looks good. It's < 1% now for me, which is a big improvement.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 17 lines · Jens Axboe
Quoted 16 lines · Al Viro
Quoted 13 lines · Jens Axboe
Right, I'm saying it's not _immediately_ clear which cases are what when reading the code.
Quoted 2 lines
up a while ago. And no, turning that into indirect calls ended up with arseloads of overhead, more's the pity...
It's a shame, since indirect calls make for nicer code, but it's always been slower and these days even more so.
Quoted 2 lines
Anyway, at the moment I have something that builds; hadn't tried to boot it yet.
Nice!
Quoted 8 lines
Quoted 4 lines
Boots and survives LTP and xfstests... Current variant is in vfs.git#work.iov_iter (head should be at
27fa77a9829c). I have *not* looked into the code generation in primitives; the likely/unlikely on those cascades of ifs need rethinking.I noticed too. Haven't fiddled much in
iov_iter.c, but foruio.hI had the below.iov_iter.cis a worse "offender" though, with 53 unlikely and 22 likely annotations...
Here it is...
include/linux/uio.h +6 -9
@@ -163,19 +163,17 @@ static inline size_t copy_folio_to_iter(struct folio *folio, size_t offset,static __always_inline __must_check size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) { -if (unlikely (!check_copy_size (addr, bytes, true))) -return 0 ; -else +if (check_copy_size (addr, bytes, true))return _copy_to_iter (addr, bytes, i); +return 0 ; }static __always_inline __must_check size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) { -if (unlikely (!check_copy_size (addr, bytes, false))) -return 0 ; -else +if (check_copy_size (addr, bytes, false))return _copy_from_iter (addr, bytes, i); +return 0 ; } static __always_inline __must_check
@@ -191,10 +189,9 @@ bool copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)static __always_inline __must_check size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) { -if (unlikely (!check_copy_size (addr, bytes, false))) -return 0 ; -else +if (check_copy_size (addr, bytes, false))return _copy_from_iter_nocache (addr, bytes, i); +return 0 ; } static __always_inline __must_check
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 23 lines · Jens Axboe
Quoted 22 lines · Jens Axboe
Quoted 20 lines · Al Viro
Quoted 6 lines · Jens Axboe
Right, I'm saying it's not _immediately_ clear which cases are what when reading the code.
Quoted 2 lines
up a while ago. And no, turning that into indirect calls ended up with arseloads of overhead, more's the pity...
Quoted 7 lines
It's a shame, since indirect calls make for nicer code, but it's always been slower and these days even more so.
Quoted 2 lines
Anyway, at the moment I have something that builds; hadn't tried to boot it yet.
Nice!
Boots and survives LTP and xfstests... Current variant is in vfs.git#work.iov_iter (head should be at
27fa77a9829c). I have *not* looked into the code generation in primitives; the likely/unlikely on those cascades of ifs need rethinking.
Quoted 5 lines
Quoted 3 lines
I noticed too. Haven't fiddled much in
iov_iter.c, but foruio.hI had the below.iov_iter.cis a worse "offender" though, with 53 unlikely and 22 likely annotations...Here it is...
Few more, most notably making sure that dio dirties reads even if they are not of the iovec type.
Last two just add a helper for import_ubuf() and then adopts it for io_uring send/recv which is also a hot path. The single range read/write can be converted too, but that needs a bit more work...
Signature · 1 line
Jens Axboe
From d05b59dc10e6c7c74da439561831f744196eb412 Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Sun, 22 May 2022 19:49:36 -0600
Subject: [PATCH 4/4] io_uring: use import_ubuf() for send/recv
We just have the single buffer, no point in using ITER_IOVEC when we can be using the more efficient ITER_UBUF instead.
--- fs/io_uring.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
fs/io_uring.c +2 -4
@@ -5272,7 +5272,6 @@ static int io_send(struct io_kiocb *req, unsigned int issue_flags) {struct io_sr_msg *sr = &req->sr_msg;struct msghdr msg; -struct iovec iov;struct socket *sock;unsigned flags;int min_ret =0 ;
@@ -5282,7 +5281,7 @@ static int io_send(struct io_kiocb *req, unsigned int issue_flags)if (unlikely (!sock))return -ENOTSOCK ; - ret =import_single_range (WRITE , sr->buf, sr->len, &iov, &msg.msg_iter); + ret =import_ubuf (WRITE , sr->buf, sr->len, &msg.msg_iter);if (unlikely (ret))return ret;
@@ -5521,7 +5520,6 @@ static int io_recv(struct io_kiocb *req, unsigned int issue_flags)struct msghdr msg;void __user *buf = sr->buf;struct socket *sock; -struct iovec iov;unsigned flags;int ret, min_ret =0 ;bool force_nonblock = issue_flags &IO_URING_F_NONBLOCK ;
@@ -5537,7 +5535,7 @@ static int io_recv(struct io_kiocb *req, unsigned int issue_flags) buf =u64_to_user_ptr (kbuf->addr); } - ret =import_single_range (READ , buf, sr->len, &iov, &msg.msg_iter); + ret =import_ubuf (READ , buf, sr->len, &msg.msg_iter);if (unlikely (ret)) goto out_free;
Signature · 1 line
2.35.1
From 5786b1db0bf43313092e61797f639290079b2709 Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Sun, 22 May 2022 19:47:29 -0600
Subject: [PATCH 3/4] iov: add import_ubuf()
Like import_single_range(), but for ITER_UBUF.
--- include/linux/uio.h | 1 + lib/iov_iter.c | 11 +++++++++++ 2 files changed, 12 insertions(+)
include/linux/uio.h +1 -0
@@ -331,6 +331,7 @@ ssize_t __import_iovec(int type, const struct iovec __user *uvec, struct iov_iter *i, bool compat);int import_single_range (int type,void __user *buf,size_t len,struct iovec *iov,struct iov_iter *i); +int import_ubuf (int type,void __user *buf,size_t len,struct iov_iter *i);static inline void iov_iter_ubuf (struct iov_iter *i,unsigned int direction,void __user *buf,size_t count)
lib/iov_iter.c +11 -0
@@ -2167,6 +2167,17 @@ int import_single_range(int rw, void __user *buf, size_t len, }EXPORT_SYMBOL (import_single_range); +int import_ubuf (int rw,void __user *buf,size_t len,struct iov_iter *i) +{ +if (len >MAX_RW_COUNT ) + len =MAX_RW_COUNT ; +if (unlikely (!access_ok (buf, len))) +return -EFAULT ; + +iov_iter_ubuf (i, rw, buf, len); +return 0 ; +} +/** * iov_iter_restore() - Restore a &struct iov_iter to the same state as when * iov_iter_save_state() was called.
Signature · 1 line
2.35.1
From cc8bfd64c1a8d754230809a56bf23bcadeabd63f Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Sun, 22 May 2022 19:37:33 -0600
Subject: [PATCH 2/4] Switch iter_is_iovec() checks for user memory with
iter_is_user()
--- block/fops.c | 7 +++---- fs/direct-io.c | 2 +- fs/iomap/direct-io.c | 2 +- fs/nfs/direct.c | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-)
block/fops.c +3 -4
@@ -77,8 +77,7 @@ static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb,if (iov_iter_rw (iter) ==READ ) {bio_init (&bio, bdev, vecs, nr_pages,REQ_OP_READ ); -if (iter_is_iovec (iter)) - should_dirty = true; + should_dirty =iter_is_user (iter); }else {bio_init (&bio, bdev, vecs, nr_pages,dio_bio_write_op (iocb)); }
@@ -217,7 +216,7 @@ static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, } dio->size =0 ; -if (is_read &&iter_is_iovec (iter)) +if (is_read &&iter_is_user (iter)) dio->flags |=DIO_SHOULD_DIRTY ;blk_start_plug (&plug);
@@ -346,7 +345,7 @@ static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb, dio->size = bio->bi_iter.bi_size;if (is_read) { -if (iter_is_iovec (iter)) { +if (iter_is_user (iter)) { dio->flags |=DIO_SHOULD_DIRTY ;bio_set_pages_dirty (bio); }
fs/direct-io.c +1 -1
@@ -1246,7 +1246,7 @@ do_blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,spin_lock_init (&dio->bio_lock); dio->refcount =1 ; - dio->should_dirty =iter_is_iovec (iter) &&iov_iter_rw (iter) ==READ ; + dio->should_dirty =iter_is_user (iter) &&iov_iter_rw (iter) ==READ ; sdio.iter = iter; sdio.final_block_in_request = end >> blkbits;
fs/iomap/direct-io.c +1 -1
@@ -523,7 +523,7 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, iomi.flags |=IOMAP_NOWAIT ; } -if (iter_is_iovec (iter)) +if (iter_is_user (iter)) dio->flags |=IOMAP_DIO_DIRTY ; }else { iomi.flags |=IOMAP_WRITE ;
fs/nfs/direct.c +1 -1
@@ -481,7 +481,7 @@ ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter,if (!is_sync_kiocb (iocb)) dreq->iocb = iocb; -if (iter_is_iovec (iter)) +if (iter_is_user (iter)) dreq->flags =NFS_ODIRECT_SHOULD_DIRTY ;if (!swap)
Signature · 1 line
2.35.1
From 992e03e0bd87801c35f5c6d8855396fc481387b8 Mon Sep 17 00:00:00 2001
From: Jens Axboe <[email protected]>
Date: Sun, 22 May 2022 19:34:45 -0600
Subject: [PATCH 1/4] iov: add iter_is_user()
Returns true if the iter is holding user memory. Use that for the might_fault() checks.
--- include/linux/uio.h | 5 +++++ lib/iov_iter.c | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-)
include/linux/uio.h +5 -0
@@ -112,6 +112,11 @@ static inline unsigned char iov_iter_rw(const struct iov_iter *i)return i->data_source ?WRITE :READ ; } +static inline bool iter_is_user (const struct iov_iter *i) +{ +return iter_is_iovec (i) ||iter_is_ubuf (i); +} +/* * Total number of bytes covered by an iovec. *
lib/iov_iter.c +3 -3
@@ -767,7 +767,7 @@ size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) {if (unlikely (iov_iter_is_pipe (i)))return copy_pipe_to_iter (addr, bytes, i); -if (iter_is_iovec (i) ||iter_is_ubuf (i)) +if (iter_is_user (i))might_fault (); iterate_and_advance(i, bytes, base, len, off, copyout(base, addr + off, len),
@@ -849,7 +849,7 @@ size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i) {if (unlikely (iov_iter_is_pipe (i)))return copy_mc_pipe_to_iter (addr, bytes, i); -if (iter_is_iovec (i) ||iter_is_ubuf (i)) +if (iter_is_user (i))might_fault (); __iterate_and_advance(i, bytes, base, len, off, copyout_mc(base, addr + off, len),
@@ -867,7 +867,7 @@ size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)WARN_ON (1 );return 0 ; } -if (iter_is_iovec (i) ||iter_is_ubuf (i)) +if (iter_is_user (i))might_fault (); iterate_and_advance(i, bytes, base, len, off, copyin(addr + off, base, len),
Signature · 1 line
2.35.1
Jens Axboe
maintainerQuoted 28 lines · Jens Axboe
Quoted 27 lines · Jens Axboe
Quoted 25 lines · Jens Axboe
Quoted 14 lines · Al Viro
Quoted 12 lines · Jens Axboe
Right, I'm saying it's not _immediately_ clear which cases are what when reading the code.
Quoted 2 lines
up a while ago. And no, turning that into indirect calls ended up with arseloads of overhead, more's the pity...
It's a shame, since indirect calls make for nicer code, but it's always been slower and these days even more so.
Quoted 2 lines
Anyway, at the moment I have something that builds; hadn't tried to boot it yet.
Quoted 6 lines
Quoted 1 line
Nice!
Boots and survives LTP and xfstests... Current variant is in vfs.git#work.iov_iter (head should be at
27fa77a9829c). I have *not* looked into the code generation in primitives; the likely/unlikely on those cascades of ifs need rethinking.I noticed too. Haven't fiddled much in
iov_iter.c, but foruio.hI had the below.iov_iter.cis a worse "offender" though, with 53 unlikely and 22 likely annotations...
Quoted 8 lines
Quoted 1 line
Here it is...
Few more, most notably making sure that dio dirties reads even if they are not of the iovec type.
Last two just add a helper for
import_ubuf()and then adopts it forio_uringsend/recv which is also a hot path. The single range read/write can be converted too, but that needs a bit more work...
Branch here:
https://git.kernel.dk/cgit/linux-block/log/?h=iov-iter
First 5 are generic ones, and some of them should just be folded with your changes.
Last 2 are just converting io_uring to use it where appropriate.
We can also use it for vectored readv/writev and recvmsg/sendmsg with one segment. The latter is mostly single segment in the real world anyway, former probably too. Though not sure it's worth it when we're copying a single iovec first anyway? Something to test...
Signature · 1 line
Jens Axboe
Al Viro
maintainerQuoted 7 lines · Jens Axboe
Branch here:
https://git.kernel.dk/cgit/linux-block/log/?h=iov-iter
First 5 are generic ones, and some of them should just be folded with your changes.
Quoted 6 lines
Last 2 are just converting
io_uringto use it where appropriate.We can also use it for vectored readv/writev and recvmsg/sendmsg with one segment. The latter is mostly single segment in the real world anyway, former probably too. Though not sure it's worth it when we're copying a single iovec first anyway? Something to test...
Not a good idea. Don't assume that all users of iov_iter are well-behaving; not everything is flavour-agnostic. If nothing else, you'll break the hell out of infinibarf - both qib and hfi check that ->write_iter() gets IOV_ITER target and fail otherwise.
BTW, #work.iov_iter is going to be rebased and reordered; if nothing else, a bunch of places like
dio->should_dirty = iter_is_iovec(iter) && iov_iter_rw(iter) == READ;
need to be dealt with before we switch new_sync_read() and new_sync_write() to ITER_UBUF.
I'm trying to sort out the /dev/fuse breakage at the moment - that thing expects ITER_IOVEC in ->read_iter() and ->write_iter(). Needs to be dealt with before new_sync_{read,write}() part, or we'll get bisection hazard...
Jens Axboe
maintainerQuoted 16 lines · Al Viro
Quoted 7 lines · Jens Axboe
Branch here:
https://git.kernel.dk/cgit/linux-block/log/?h=iov-iter
First 5 are generic ones, and some of them should just be folded with your changes.
Quoted 6 lines
Last 2 are just converting
io_uringto use it where appropriate.We can also use it for vectored readv/writev and recvmsg/sendmsg with one segment. The latter is mostly single segment in the real world anyway, former probably too. Though not sure it's worth it when we're copying a single iovec first anyway? Something to test...
Quoted 4 lines
Not a good idea. Don't assume that all users of
iov_iterare well-behaving; not everything is flavour-agnostic. If nothing else, you'll break the hell out of infinibarf - both qib and hfi check that ->write_iter() getsIOV_ITERtarget and fail otherwise.
OK, I'll check up on that.
Quoted 5 lines
BTW, #work.iov_iter is going to be rebased and reordered; if nothing else, a bunch of places like
dio->should_dirty = iter_is_iovec(iter) && iov_iter_rw(iter) == READ;need to be dealt with before we switch
new_sync_read()andnew_sync_write()toITER_UBUF.
I already made an attempt at that, see the git branch I sent in the last email.
Signature · 1 line
Jens Axboe
Al Viro
maintainerQuoted 24 lines · Jens Axboe
Quoted 16 lines · Al Viro
Quoted 7 lines · Jens Axboe
Branch here:
https://git.kernel.dk/cgit/linux-block/log/?h=iov-iter
First 5 are generic ones, and some of them should just be folded with your changes.
Quoted 6 lines
Last 2 are just converting
io_uringto use it where appropriate.We can also use it for vectored readv/writev and recvmsg/sendmsg with one segment. The latter is mostly single segment in the real world anyway, former probably too. Though not sure it's worth it when we're copying a single iovec first anyway? Something to test...
Quoted 4 lines
Not a good idea. Don't assume that all users of
iov_iterare well-behaving; not everything is flavour-agnostic. If nothing else, you'll break the hell out of infinibarf - both qib and hfi check that ->write_iter() getsIOV_ITERtarget and fail otherwise.OK, I'll check up on that.
Quoted 7 lines
Quoted 5 lines
BTW, #work.iov_iter is going to be rebased and reordered; if nothing else, a bunch of places like
dio->should_dirty = iter_is_iovec(iter) && iov_iter_rw(iter) == READ;need to be dealt with before we switch
new_sync_read()andnew_sync_write()toITER_UBUF.I already made an attempt at that, see the git branch I sent in the last email.
There's several more, AFAICS (cifs, ceph, fuse, gfs2)... The check in /dev/fuse turned out to be fine - it's only using primitives, so we can pass ITER_UBUF ones there. mm/shmem.c check... similar, but I really wonder if x86 clear_user() really sucks worse than copy_to_user() from zero page...
FWIW, I'd added bool ->user_backed next to ->data_source, with user_backed_iter() as an inline predicate checking it. Seems to get slightly better iov_iter.c code generation that way...
Current branch pushed to #new.iov_iter (at the moment; will rename back to work.iov_iter once it gets more or less stable).
Jens Axboe
maintainerQuoted 31 lines · Al Viro
Quoted 30 lines · Jens Axboe
Quoted 16 lines · Al Viro
Quoted 7 lines · Jens Axboe
Branch here:
https://git.kernel.dk/cgit/linux-block/log/?h=iov-iter
First 5 are generic ones, and some of them should just be folded with your changes.
Quoted 6 lines
Last 2 are just converting
io_uringto use it where appropriate.We can also use it for vectored readv/writev and recvmsg/sendmsg with one segment. The latter is mostly single segment in the real world anyway, former probably too. Though not sure it's worth it when we're copying a single iovec first anyway? Something to test...
Quoted 4 lines
Not a good idea. Don't assume that all users of
iov_iterare well-behaving; not everything is flavour-agnostic. If nothing else, you'll break the hell out of infinibarf - both qib and hfi check that ->write_iter() getsIOV_ITERtarget and fail otherwise.OK, I'll check up on that.
Quoted 5 lines
BTW, #work.iov_iter is going to be rebased and reordered; if nothing else, a bunch of places like
dio->should_dirty = iter_is_iovec(iter) && iov_iter_rw(iter) == READ;need to be dealt with before we switch
new_sync_read()andnew_sync_write()toITER_UBUF.
Quoted 7 lines
Quoted 1 line
I already made an attempt at that, see the git branch I sent in the last email.
There's several more, AFAICS (cifs, ceph, fuse, gfs2)... The check in
/dev/fuseturned out to be fine - it's only using primitives, so we can passITER_UBUFones there.mm/shmem.ccheck... similar, but I really wonder if x86clear_user()really sucks worse thancopy_to_user()from zero page...
Yep, not surprised if it isn't complete, I just tackled the ones I found. I do like the idea of having a generic check for that rather than implicit knowledge about which iter types may contain user memory.
I haven't looked at clear_user() vs copy_to_user() from the zero page. But should be trivial to benchmark and profile. I'll try and do that when I find some time.
Quoted 3 lines
FWIW, I'd added bool ->user_backed next to ->data_source, with
user_backed_iter()as an inline predicate checking it. Seems to get slightly betteriov_iter.ccode generation that way...
OK that sounds fine too, I pondered doing something similar rather than have the helper since there is an existing hole in there anyway.
Quoted 2 lines
Current branch pushed to #new.iov_iter (at the moment; will rename back to work.iov_iter once it gets more or less stable).
Sounds good, I'll see what I need to rebase.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 4 lines · Jens Axboe
Quoted 2 lines
Current branch pushed to #new.iov_iter (at the moment; will rename back to work.iov_iter once it gets more or less stable).
Sounds good, I'll see what I need to rebase.
On the previous branch, ran a few quick numbers. dd from /dev/zero to /dev/null, with /dev/zero using ->read() as it does by default:
32 260MB/sec 1k 6.6GB/sec 4k 17.9GB/sec 16k 28.8GB/sec
now comment out ->read() so it uses ->read_iter() instead:
32 259MB/sec 1k 6.6GB/sec 4k 18.0GB/sec
16k 28.6GB/sec
which are roughly identical, all things considered. Just a sanity check, but looks good from a performance POV in this basic test.
Now let's do ->read_iter() but make iov_iter_zero() copy from the zero page instead:
32 250MB/sec 1k 7.7GB/sec 4k 28.8GB/sec
16k 71.2GB/sec
Looks like it's a tad slower for 32-bytes, considerably better for 1k, and massively better at page size and above. This is on an Intel 12900K, so recent CPU. Let's try cacheline and above:
Size Method BW
64 copy_from_zero() 508MB/sec
128 copy_from_zero() 1.0GB/sec
64 clear_user() 513MB/sec
128 clear_user() 1.0GB/sec
Something like the below may make sense to do, the wins at bigger sizes is substantial and that gets me the best of both worlds. If we really care, we could move the check earlier and not have it per-segment. I doubt it matters in practice, though.
lib/iov_iter.c +8 -1
@@ -1049,12 +1049,19 @@ static size_t pipe_zero(size_t bytes, struct iov_iter *i)return bytes; } +static unsigned long copy_from_zero (void __user *buf,size_t len) +{ +if (len >=128 ) +return copy_to_user (buf,page_address (ZERO_PAGE (0 )), len); +return clear_user (buf, len); +} +size_t iov_iter_zero (size_t bytes,struct iov_iter *i) {if (unlikely (iov_iter_is_pipe (i)))return pipe_zero (bytes, i);iterate_and_advance (i, bytes, base, len, count, -clear_user (base, len), +copy_from_zero (base, len),memset (base,0 , len) )
Signature · 1 line
Jens Axboe
Quoted 36 lines · Jens Axboe
Quoted 4 lines · Jens Axboe
Quoted 2 lines
Current branch pushed to #new.iov_iter (at the moment; will rename back to work.iov_iter once it gets more or less stable).
Sounds good, I'll see what I need to rebase.
On the previous branch, ran a few quick numbers. dd from
/dev/zeroto/dev/null, with/dev/zerousing ->read() as it does by default:32 260MB/sec 1k 6.6GB/sec 4k 17.9GB/sec 16k 28.8GB/secnow comment out ->read() so it uses ->read_iter() instead:
32 259MB/sec 1k 6.6GB/sec 4k 18.0GB/sec16k 28.6GB/sec
which are roughly identical, all things considered. Just a sanity check, but looks good from a performance POV in this basic test.
Now let's do ->read_iter() but make
iov_iter_zero()copy from the zero page instead:32 250MB/sec 1k 7.7GB/sec 4k 28.8GB/sec16k 71.2GB/sec
Looks like it's a tad slower for 32-bytes, considerably better for 1k, and massively better at page size and above. This is on an Intel 12900K, so recent CPU. Let's try cacheline and above:
Quoted 5 lines
Size Method BW
64copy_from_zero()508MB/sec
128copy_from_zero()1.0GB/sec
64clear_user()513MB/sec
128clear_user()1.0GB/sec
See this thread-of-doom:
Jens Axboe
maintainerQuoted 43 lines · Matthew Wilcox
Quoted 36 lines · Jens Axboe
Quoted 4 lines · Jens Axboe
Quoted 2 lines
Current branch pushed to #new.iov_iter (at the moment; will rename back to work.iov_iter once it gets more or less stable).
Sounds good, I'll see what I need to rebase.
On the previous branch, ran a few quick numbers. dd from
/dev/zeroto/dev/null, with/dev/zerousing ->read() as it does by default:32 260MB/sec 1k 6.6GB/sec 4k 17.9GB/sec 16k 28.8GB/secnow comment out ->read() so it uses ->read_iter() instead:
32 259MB/sec 1k 6.6GB/sec 4k 18.0GB/sec16k 28.6GB/sec
which are roughly identical, all things considered. Just a sanity check, but looks good from a performance POV in this basic test.
Now let's do ->read_iter() but make
iov_iter_zero()copy from the zero page instead:32 250MB/sec 1k 7.7GB/sec 4k 28.8GB/sec16k 71.2GB/sec
Looks like it's a tad slower for 32-bytes, considerably better for 1k, and massively better at page size and above. This is on an Intel 12900K, so recent CPU. Let's try cacheline and above:
Quoted 5 lines
Size Method BW
64copy_from_zero()508MB/sec
128copy_from_zero()1.0GB/sec
64clear_user()513MB/sec
128clear_user()1.0GB/sec
Quoted 3 lines
See this thread-of-doom:
Yikes, will check. Also realized that my > PAGE_SIZE 16k result is invalid as it should iterate with min(left, PAGE_SIZE). But that just changes it from 71.2GB/sec to 68.9MB/sec, still a substantial win. Updated diff below.
Was going to test on aarch64 next...
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 1 line · Jens Axboe
Was going to test on aarch64 next...
Just for completeness, before diving into that thread. On aarch64, 16k native page size, but running in a vm with 4k page size:
clear_user()
32 ~96MB/sec
64 195MB/sec
128 386MB/sec
1k 2.7GB/sec
4k 7.8GB/sec
16k 14.8GB/sec
copy_from_zero_page()
32 ~96MB/sec
64 193MB/sec
128 383MB/sec
1k 2.9GB/sec
4k 9.8GB/sec
16k 21.8GB/sec
Tad slower on <= 128 bytes, 1k a bit faster, and substantially faster above that. Eg same logic would be sane on aarch64 as it is on x86-64, use copy_from_zero_page at > 128 bytes.
Signature · 1 line
Jens Axboe
Quoted 8 lines · Jens Axboe
clear_user()
32 ~96MB/sec
64 195MB/sec
128 386MB/sec
1k 2.7GB/sec
4k 7.8GB/sec
16k 14.8GB/sec
Quoted 7 lines
copy_from_zero_page()
32 ~96MB/sec
64 193MB/sec
128 383MB/sec
1k 2.9GB/sec
4k 9.8GB/sec
16k 21.8GB/sec
Just FYI, on x86, Samuel Neves proposed some nice clear_user() performance improvements that were forgotten about:
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
Hoping somebody picks this up at some point...
Jason
Quoted 17 lines · Jason A. Donenfeld
Quoted 8 lines · Jens Axboe
clear_user()
32 ~96MB/sec
64 195MB/sec
128 386MB/sec
1k 2.7GB/sec
4k 7.8GB/sec
16k 14.8GB/secQuoted 7 lines
copy_from_zero_page()
32 ~96MB/sec
64 193MB/sec
128 383MB/sec
1k 2.9GB/sec
4k 9.8GB/sec
16k 21.8GB/sec
Quoted 7 lines
Just FYI, on x86, Samuel Neves proposed some nice
clear_user()performance improvements that were forgotten about:https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/Hoping somebody picks this up at some point...
Those ~2x speedup numbers are indeed looking very nice:
| After this patch, on a Skylake CPU, these are the | before/after figures: | | $ dd if=/dev/zero of=/dev/null bs=1024k status=progress | 94402248704 bytes (94 GB, 88 GiB) copied, 6 s, 15.7 GB/s | | $ dd if=/dev/zero of=/dev/null bs=1024k status=progress | 446476320768 bytes (446 GB, 416 GiB) copied, 15 s, 29.8 GB/s
Patch fell through the cracks & it doesn't apply anymore:
checking file arch/x86/lib/usercopy_64.c
Hunk #2 FAILED at 17.
1 out of 2 hunks FAILED
Would be nice to re-send it.
Thanks,
Ingo
Hi Ingo,
Quoted 39 lines · Ingo Molnar
Quoted 17 lines · Jason A. Donenfeld
Quoted 8 lines · Jens Axboe
clear_user()
32 ~96MB/sec
64 195MB/sec
128 386MB/sec
1k 2.7GB/sec
4k 7.8GB/sec
16k 14.8GB/secQuoted 7 lines
copy_from_zero_page()
32 ~96MB/sec
64 193MB/sec
128 383MB/sec
1k 2.9GB/sec
4k 9.8GB/sec
16k 21.8GB/secQuoted 7 lines
Just FYI, on x86, Samuel Neves proposed some nice
clear_user()performance improvements that were forgotten about:https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/Hoping somebody picks this up at some point...
Those ~2x speedup numbers are indeed looking very nice:
| After this patch, on a Skylake CPU, these are the | before/after figures: | | $ dd if=/dev/zero of=/dev/null bs=1024k status=progress | 94402248704 bytes (94 GB, 88 GiB) copied, 6 s, 15.7 GB/s | | $ dd if=/dev/zero of=/dev/null bs=1024k status=progress | 446476320768 bytes (446 GB, 416 GiB) copied, 15 s, 29.8 GB/s
Quoted 7 lines
Patch fell through the cracks & it doesn't apply anymore:
checking file
arch/x86/lib/usercopy_64.c
Hunk #2 FAILED at 17.
1 out of 2 hunks FAILEDWould be nice to re-send it.
I don't think Samuel is going to do that at this point, so I think it's probably best if you do it.
Jason
Quoted 45 lines · Jason A. Donenfeld
Hi Ingo,
Quoted 41 lines · Ingo Molnar
Quoted 17 lines · Jason A. Donenfeld
Quoted 8 lines · Jens Axboe
clear_user()
32 ~96MB/sec
64 195MB/sec
128 386MB/sec
1k 2.7GB/sec
4k 7.8GB/sec
16k 14.8GB/secQuoted 7 lines
copy_from_zero_page()
32 ~96MB/sec
64 193MB/sec
128 383MB/sec
1k 2.9GB/sec
4k 9.8GB/sec
16k 21.8GB/secQuoted 7 lines
Just FYI, on x86, Samuel Neves proposed some nice
clear_user()performance improvements that were forgotten about:https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/Hoping somebody picks this up at some point...
Those ~2x speedup numbers are indeed looking very nice:
| After this patch, on a Skylake CPU, these are the | before/after figures: | | $ dd if=/dev/zero of=/dev/null bs=1024k status=progress | 94402248704 bytes (94 GB, 88 GiB) copied, 6 s, 15.7 GB/s | | $ dd if=/dev/zero of=/dev/null bs=1024k status=progress | 446476320768 bytes (446 GB, 416 GiB) copied, 15 s, 29.8 GB/sPatch fell through the cracks & it doesn't apply anymore:
Quoted 8 lines
Quoted 5 lines
checking file
arch/x86/lib/usercopy_64.c
Hunk #2 FAILED at 17.
1 out of 2 hunks FAILEDWould be nice to re-send it.
I don't think Samuel is going to do that at this point, so I think it's probably best if you do it.
For what it's worth, these are the benchmarks I did at the time comparing the various code paths (generic loop, rep stosd + stosb, rep stosd + final loop, rep stosb) for sizes from 0 to 4096 (and then to 32k in larger increments) in the following chips:
- Atom C2550 - Avoton
- Xeon E3-1240 v3 - Haswell
- EPYC 7401P - Zen 1
- EPYC 7402P - Zen 2
- Xeon D-1537 - Broadwell
- Core i7-6700HQ - Skylake
- Core i7-3770 - Ivy Bridge
- Xeon Gold 5120 - Skylake-SP
The fields are: bytes, cycles for generic loop, cycles for rep stosd+stosb, cycles for rep stosd + final loop, cycles for rep stosb. There are aligned (to cacheline) and unaligned (cacheline+1) destination buffer numbers, as that seems to be relevant for rep stos performance.
Make of that what you will; as Jason said, I'm not particularly interested in reviving this.
Samuel.
Quoted 1 line
Jason
Quoted 39 lines · Ingo Molnar
Quoted 17 lines · Jason A. Donenfeld
Quoted 8 lines · Jens Axboe
clear_user()
32 ~96MB/sec
64 195MB/sec
128 386MB/sec
1k 2.7GB/sec
4k 7.8GB/sec
16k 14.8GB/secQuoted 7 lines
copy_from_zero_page()
32 ~96MB/sec
64 193MB/sec
128 383MB/sec
1k 2.9GB/sec
4k 9.8GB/sec
16k 21.8GB/secQuoted 7 lines
Just FYI, on x86, Samuel Neves proposed some nice
clear_user()performance improvements that were forgotten about:https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/Hoping somebody picks this up at some point...
Those ~2x speedup numbers are indeed looking very nice:
| After this patch, on a Skylake CPU, these are the | before/after figures: | | $ dd if=/dev/zero of=/dev/null bs=1024k status=progress | 94402248704 bytes (94 GB, 88 GiB) copied, 6 s, 15.7 GB/s | | $ dd if=/dev/zero of=/dev/null bs=1024k status=progress | 446476320768 bytes (446 GB, 416 GiB) copied, 15 s, 29.8 GB/s
Quoted 7 lines
Patch fell through the cracks & it doesn't apply anymore:
checking file
arch/x86/lib/usercopy_64.c
Hunk #2 FAILED at 17.
1 out of 2 hunks FAILEDWould be nice to re-send it.
Turns out Boris just sent a competing optimization to clear_user() 3 days ago:
https://lore.kernel.org/r/[email protected]
Thanks,
Ingo
Borislav Petkov
maintainerQuoted 3 lines · Ingo Molnar
Turns out Boris just sent a competing optimization to
clear_user()3 days ago:
Yes, and that one needs to be properly measured and verified it doesn't impact any workload. I have been working on this for a while now so let's all relax ourselves - it'll get fixed properly eventually.
What you could do in the meantime is, run it and see if your microbenchmarks are happy with it.
Because clear_user() doesn't matter one whit in real workloads, as we realized upthread.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 10 lines · Borislav Petkov
Quoted 3 lines · Ingo Molnar
Turns out Boris just sent a competing optimization to
clear_user()3 days ago:Yes, and that one needs to be properly measured and verified it doesn't impact any workload. I have been working on this for a while now so let's all relax ourselves - it'll get fixed properly eventually.
Quoted 6 lines
What you could do in the meantime is, run it and see if your microbenchmarks are happy with it.
Because
clear_user()doesn't matter one whit in real workloads, as we realized upthread.
Can you CC me on further evolution of this patch?
What was the (Git) base for it?
Can you describe your or a test-case on how to test it?
Unfortunately, I cannot get the patch from the above link with the b4 tool.
link="https://lore.kernel.org/all/[email protected]/" b4 -d am $link
Gives a strange collection of akpm mm patchset.
Thanks.
-Sedat-
Quoted 7 lines
Thx.
--
Regards/Gruss,
Boris.
Quoted 49 lines · Ingo Molnar
Quoted 45 lines · Ingo Molnar
Quoted 17 lines · Jason A. Donenfeld
Quoted 8 lines · Jens Axboe
clear_user()32 ~96MB/sec 64 195MB/sec 128 386MB/sec 1k 2.7GB/sec 4k 7.8GB/sec 16k 14.8GB/secQuoted 7 lines
copy_from_zero_page()32 ~96MB/sec 64 193MB/sec 128 383MB/sec 1k 2.9GB/sec 4k 9.8GB/sec 16k 21.8GB/secQuoted 7 lines
Just FYI, on x86, Samuel Neves proposed some nice
clear_user()performance improvements that were forgotten about:https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/Hoping somebody picks this up at some point...
Those ~2x speedup numbers are indeed looking very nice:
| After this patch, on a Skylake CPU, these are the | before/after figures: | | $ dd if=/dev/zero of=/dev/null bs=1024k status=progress | 94402248704 bytes (94 GB, 88 GiB) copied, 6 s, 15.7 GB/s | | $ dd if=/dev/zero of=/dev/null bs=1024k status=progress | 446476320768 bytes (446 GB, 416 GiB) copied, 15 s, 29.8 GB/sPatch fell through the cracks & it doesn't apply anymore:
checking file
arch/x86/lib/usercopy_64.c
Hunk #2 FAILED at 17.
1 out of 2 hunks FAILED
Quoted 8 lines
Quoted 1 line
Would be nice to re-send it.
Turns out Boris just sent a competing optimization to
clear_user()3 days ago:https://lore.kernel.org/r/[email protected]
Thanks,
[ CC Hugh ]
I hope I adapted both patches from Hugh and Samuel against Linux v5.18 correctly.
As I have no "modern CPU" meaning Intel Sandy-Bridge, the patch of Hugh was not predestined for me (see numbers).
Samuel's patch gave me 15% of speedup with running Hugh's dd test-case (cannot say if this is a real benchmark for testing).
Patches and latest linux-config attached.
*** Without patch
root# cat /proc/version
Linux version 5.18.0-3-amd64-clang14-lto ([email protected]@iniza) (dileks clang version 14.0.4 (https://github.com/llvm/llvm-project.git29f1039a7285a5c3a9c353d054140bf2556d4c4d), LLD 14.0.4) #3~bookworm+dileks1 SMP PREEMPT_DYNAMIC 2022-05-27
root# dd if=/dev/zero of=/dev/null bs=1M count=1M 1048576+0 Datensätze ein
1048576+0 Datensätze aus
1099511627776 Bytes (1,1 TB, 1,0 TiB) kopiert, 97,18 s, 11,3 GB/s
*** With hughd patch
Patch: 0001-x86-usercopy-Use-alternatives-for-clear_user.patch
root# cat /proc/version
Linux version 5.18.0-4-amd64-clang14-lto ([email protected]@iniza) (dileks clang version 14.0.4 (https://github.com/llvm/llvm-project.git29f1039a7285a5c3a9c35>
root# dd if=/dev/zero of=/dev/null bs=1M count=1M 1048576+0 Datensätze ein
1048576+0 Datensätze aus
1099511627776 Bytes (1,1 TB, 1,0 TiB) kopiert, 588,053 s, 1,9 GB/s
root# cat /proc/version
Linux version 5.18.0-4-amd64-clang14-lto ([email protected]@iniza) (dileks clang version 14.0.4 (https://github.com/llvm/llvm-project.git29f1039a7285a5c3a9c353d054140bf2556d4c4d), LLD 14.0.4) #4~bookworm+dileks1 SMP PREEMPT_DYNAMIC 2022-05-28
*** With sneves patch
Patch: 0001-x86-usercopy-speed-up-64-bit-__clear_user-with-stos-.patch
root# cat /proc/version
Linux version 5.18.0-5-amd64-clang14-lto ([email protected]@iniza) (dileks clang version 14.0.4 (https://github.com/llvm/llvm-project.git29f1039a7285a5c3a9c353d054140bf2556d4c4d), LLD 14.0.4) #5~bookworm+dileks1 SMP PREEMPT_DYNAMIC 2022-05-28
root# dd if=/dev/zero of=/dev/null bs=1M count=1M 1048576+0 Datensätze ein
1048576+0 Datensätze aus
1099511627776 Bytes (1,1 TB, 1,0 TiB) kopiert, 82,697 s, 13,3 GB/s
-dileks // 28-May-2022
Quoted 118 lines · Sedat Dilek
Quoted 49 lines · Ingo Molnar
Quoted 45 lines · Ingo Molnar
Quoted 17 lines · Jason A. Donenfeld
Quoted 8 lines · Jens Axboe
clear_user()32 ~96MB/sec 64 195MB/sec 128 386MB/sec 1k 2.7GB/sec 4k 7.8GB/sec 16k 14.8GB/secQuoted 7 lines
copy_from_zero_page()32 ~96MB/sec 64 193MB/sec 128 383MB/sec 1k 2.9GB/sec 4k 9.8GB/sec 16k 21.8GB/secQuoted 7 lines
Just FYI, on x86, Samuel Neves proposed some nice
clear_user()performance improvements that were forgotten about:https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/Hoping somebody picks this up at some point...
Those ~2x speedup numbers are indeed looking very nice:
| After this patch, on a Skylake CPU, these are the | before/after figures: | | $ dd if=/dev/zero of=/dev/null bs=1024k status=progress | 94402248704 bytes (94 GB, 88 GiB) copied, 6 s, 15.7 GB/s | | $ dd if=/dev/zero of=/dev/null bs=1024k status=progress | 446476320768 bytes (446 GB, 416 GiB) copied, 15 s, 29.8 GB/sPatch fell through the cracks & it doesn't apply anymore:
checking file
arch/x86/lib/usercopy_64.c
Hunk #2 FAILED at 17.
1 out of 2 hunks FAILEDQuoted 8 lines
Quoted 1 line
Would be nice to re-send it.
Turns out Boris just sent a competing optimization to
clear_user()3 days ago:https://lore.kernel.org/r/[email protected]
Thanks,
[ CC Hugh ]
I hope I adapted both patches from Hugh and Samuel against Linux v5.18 correctly.
As I have no "modern CPU" meaning Intel Sandy-Bridge, the patch of Hugh was not predestined for me (see numbers).
Samuel's patch gave me 15% of speedup with running Hugh's dd test-case (cannot say if this is a real benchmark for testing).
Patches and latest linux-config attached.
*** Without patch
root# cat
/proc/version
Linux version 5.18.0-3-amd64-clang14-lto ([email protected]@iniza) (dileks clang version 14.0.4 (https://github.com/llvm/llvm-project.git29f1039a7285a5c3a9c353d054140bf2556d4c4d), LLD 14.0.4) #3~bookworm+dileks1 SMPPREEMPT_DYNAMIC2022-05-27root# dd if=/dev/zero of=/dev/null bs=1M count=1M 1048576+0 Datensätze ein
1048576+0 Datensätze aus
1099511627776 Bytes (1,1 TB, 1,0 TiB) kopiert, 97,18 s, 11,3 GB/s*** With hughd patch
Patch: 0001-x86-usercopy-Use-alternatives-for-clear_user.patch
root# cat
/proc/version
Linux version 5.18.0-4-amd64-clang14-lto ([email protected]@iniza) (dileks clang version 14.0.4 (https://github.com/llvm/llvm-project.git29f1039a7285a5c3a9c35>root# dd if=/dev/zero of=/dev/null bs=1M count=1M 1048576+0 Datensätze ein
1048576+0 Datensätze aus
1099511627776 Bytes (1,1 TB, 1,0 TiB) kopiert, 588,053 s, 1,9 GB/sroot# cat
/proc/version
Linux version 5.18.0-4-amd64-clang14-lto ([email protected]@iniza) (dileks clang version 14.0.4 (https://github.com/llvm/llvm-project.git29f1039a7285a5c3a9c353d054140bf2556d4c4d), LLD 14.0.4) #4~bookworm+dileks1 SMPPREEMPT_DYNAMIC2022-05-28*** With sneves patch
Patch: 0001-x86-usercopy-speed-up-64-bit-__clear_user-with-stos-.patch
root# cat
/proc/version
Linux version 5.18.0-5-amd64-clang14-lto ([email protected]@iniza) (dileks clang version 14.0.4 (https://github.com/llvm/llvm-project.git29f1039a7285a5c3a9c353d054140bf2556d4c4d), LLD 14.0.4) #5~bookworm+dileks1 SMPPREEMPT_DYNAMIC2022-05-28
Quoted 7 lines
root# dd if=/dev/zero of=/dev/null bs=1M count=1M 1048576+0 Datensätze ein
1048576+0 Datensätze aus
1099511627776 Bytes (1,1 TB, 1,0 TiB) kopiert, 82,697 s, 13,3 GB/s-dileks // 28-May-2022
Now with attachments.
-sed@-
From 98169b8804af661b485bbc5699825af8be22a125 Mon Sep 17 00:00:00 2001
From: Sedat Dilek <[email protected]>
Date: Sat, 28 May 2022 12:42:00 +0200
Subject: [PATCH] x86/usercopy: Use alternatives for clear_user()
From: Hugh Dickins <[email protected]>
--- arch/x86/lib/copy_user_64.S | 26 ++++++++++++++++++++++++++ arch/x86/lib/usercopy_64.c | 34 +--------------------------------- 2 files changed, 27 insertions(+), 33 deletions(-)
arch/x86/lib/copy_user_64.S +26 -0
@@ -408,3 +408,29 @@ SYM_FUNC_START(__copy_user_nocache) _ASM_EXTABLE_CPY(41b, .L_fixup_1b_copy) SYM_FUNC_END(__copy_user_nocache) EXPORT_SYMBOL(__copy_user_nocache) + +/* + * Recent CPUs have added enhanced REP MOVSB/STOSB instructions. + * It's recommended to use enhanced REP MOVSB/STOSB if it's enabled. + * Assume that's best for __clear_user(), until alternatives are provided + * (though would be better to avoid REP STOSB for short clears, if no FSRM). + * + * Input: + * rdi destination + * rsi count + * + * Output: + * rax uncopied bytes or 0 if successful. + */ +SYM_FUNC_START(__clear_user) + ASM_STAC + movl %esi,%ecx + xorq %rax,%rax +1: rep stosb +2: movl %ecx,%eax + ASM_CLAC + ret + + _ASM_EXTABLE_UA(1b, 2b) +SYM_FUNC_END(__clear_user) +EXPORT_SYMBOL(__clear_user)
arch/x86/lib/usercopy_64.c +1 -33
@@ -13,41 +13,9 @@/* * Zero Userspace */ - -unsigned long __clear_user (void __user *addr,unsigned long size) -{ -long __d0; -might_fault (); -/* no memory constraint because it doesn't change any memory gcc knows -about */ -stac (); - asmvolatile ( -" testq %[size8],%[size8]\n" -" jz 4f\n" -" .align 16\n" -"0: movq $0,(%[dst])\n" -" addq $8,%[dst]\n" -" decl %%ecx ; jnz 0b\n" -"4: movq %[size1],%%rcx\n" -" testl %%ecx,%%ecx\n" -" jz 2f\n" -"1: movb $0,(%[dst])\n" -" incq %[dst]\n" -" decl %%ecx ; jnz 1b\n" -"2:\n" - - _ASM_EXTABLE_TYPE_REG(0b, 2b, EX_TYPE_UCOPY_LEN8, %[size1]) -_ASM_EXTABLE_UA (1b ,2b ) - - : [size8] "=&c"(size), [dst] "=&D" (__d0) - : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr)); -clac (); -return size; -} -EXPORT_SYMBOL (__clear_user); -unsigned long clear_user (void __user *to,unsigned long n) { +might_fault ();if (access_ok (to, n))return __clear_user (to, n);return n;
Signature · 1 line
2.36.1
From f9ce989826f283efc429be41caded7c885b8950c Mon Sep 17 00:00:00 2001
From: Sedat Dilek <[email protected]>
Date: Sat, 28 May 2022 17:54:40 +0200
Subject: [PATCH] x86/usercopy: speed up 64-bit __clear_user() with stos{b,q}
From: Samuel Neves <[email protected]>
--- arch/x86/lib/usercopy_64.c | 61 +++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 20 deletions(-)
arch/x86/lib/usercopy_64.c +41 -20
78 lines not on this page · raw message
Signature · 1 line
2.36.1
Al Viro
maintainerQuoted 6 lines · Jens Axboe
Quoted 5 lines
There's several more, AFAICS (cifs, ceph, fuse, gfs2)... The check in
/dev/fuseturned out to be fine - it's only using primitives, so we can passITER_UBUFones there.mm/shmem.ccheck... similar, but I really wonder if x86clear_user()really sucks worse thancopy_to_user()from zero page...
Quoted 7 lines
Yep, not surprised if it isn't complete, I just tackled the ones I found. I do like the idea of having a generic check for that rather than implicit knowledge about which iter types may contain user memory.
I haven't looked at
clear_user()vscopy_to_user()from the zero page. But should be trivial to benchmark and profile. I'll try and do that when I find some time.
FWIW, having looked at __clear_user() in arch/x86/lib/usercopy_64.c... I'm not at all surprised. It should be parallel to memset_64.S; as it is, we have
loop with one 64bit store per iteration + loop with 8bit stores
for the tail, with no attempts to align anything
vs.
rep stosb if CPU has optimized rep stosb; otherwise
rep stosq + rep stosb on CPUs that don't suck at rep sto* in general; otherwise align, then do loop with 8*64bit stores, then loop with 64bit stores, then loop with 8bit stores
Shouldn't be hard to do uaccess parallel for that. Reads from /dev/zero, if nothing else, would benefit...
Might be worth doing what sparc does, with shared asm for both - it's an out-of-line code anyway, with most of the payload trivially shared...
Quoted 2 lines · Al Viro
At the moment it's carved up into 6 commits:
btrfs_direct_write(): cleaner way to handlegeneric_write_sync()suppression
I happpen to need something similar for JFS, so I tried it out. There's a mistake where dio->flags should have been dio_flags. I also added a comment on the definition of DIO_NOSYNC.
(the btrfs patch didn't apply cleanly, so I just dropped it since I wasn't going to be touching btrfs anyway)
fs/iomap/direct-io.c +2 -1
@@ -548,7 +548,8 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, }/* for data sync or sync, we need sync completion processing */ -if (iocb->ki_flags &IOCB_DSYNC ) +if (iocb->ki_flags &IOCB_DSYNC && + !(dio_flags &IOMAP_DIO_NOSYNC )) dio->flags |=IOMAP_DIO_NEED_SYNC ;/*
include/linux/iomap.h +6 -0
@@ -353,6 +353,12 @@ struct iomap_dio_ops {*/ #define IOMAP_DIO_PARTIAL (1 << 2) +/* +* The caller will sync the write if needed; do not sync it within +* iomap_dio_rw. Overrides IOMAP_DIO_FORCE_WAIT. +*/ +#define IOMAP_DIO_NOSYNC (1 << 3) +ssize_t iomap_dio_rw (struct kiocb *iocb,struct iov_iter *iter,const struct iomap_ops *ops,const struct iomap_dio_ops *dops,unsigned int dio_flags,void *private,size_t done_before);
Al Viro
maintainerQuoted 9 lines · Al Viro
copy_page_{from,to}_iter() iovec side is needed not for the sake of optimizations - if you look at the generic variant, you'll see that it's basically "
kmap_local_page+ copy_{to,from}_iter() for the contents +kunmap_local_page", which obviously relies upon the copy_{to,from}_iter() being non-blocking. So iovec part of it genuinely needs to differ from the generic variant; it's just that on top of that it had been (badly) microoptimized. So were iterators, but that got at least somewhat cleaned up a while ago. And no, turning that into indirect calls ended up with arseloads of overhead, more's the pity...
Actually, I take that back - the depth of kmap stack won't be increased by more than one compared to mainline, so we can switch iovec and ubuf variants to generic.
See #new.iov_iter; I do like the diffstat - -110 lines of code, with new flavour added...
Completely untested, though.
Jens Axboe
maintainerQuoted 12 lines · Al Viro
Quoted 9 lines · Al Viro
copy_page_{from,to}_iter() iovec side is needed not for the sake of optimizations - if you look at the generic variant, you'll see that it's basically "
kmap_local_page+ copy_{to,from}_iter() for the contents +kunmap_local_page", which obviously relies upon the copy_{to,from}_iter() being non-blocking. So iovec part of it genuinely needs to differ from the generic variant; it's just that on top of that it had been (badly) microoptimized. So were iterators, but that got at least somewhat cleaned up a while ago. And no, turning that into indirect calls ended up with arseloads of overhead, more's the pity...
Quoted 8 lines
Actually, I take that back - the depth of kmap stack won't be increased by more than one compared to mainline, so we can switch iovec and ubuf variants to generic.
See #new.iov_iter; I do like the diffstat - -110 lines of code, with new flavour added...
Completely untested, though.
That's nice looking indeed. Not just dropping the code, but reducing the unwieldly number of branches in there. A bit pressed for time these days, but I'll try and resurrect some testing mid next week.
Signature · 1 line
Jens Axboe
Al Viro
maintainerQuoted 22 lines · Matthew Wilcox
Quoted 12 lines · Al Viro
Quoted 11 lines · Matthew Wilcox
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Jens Axboe
Then we're almost on par, and it looks like we just need to special case
iov_iter_advance()for thenr_segs== 1 as well to be on par. This is on top of your patch as well, fwiw.It might make sense to special case the single segment cases, for both setup, iteration, and advancing. With that, I think we'll be where we want to be, and there will be no discernable difference between the iter paths and the old style paths.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
A while ago willy posted patches to support a new ITER type for direct userspace pointer without iov. It might be worth looking through the archives and test that.
https://lore.kernel.org/linux-fsdevel/Yba+YSF6mkM%[email protected]/
Direct kernel pointer, surely? And from a quick look,
iov_iter_is_kaddr()checks for the wrong value...
Quoted 6 lines
Indeed. I didn't test it; it was a quick patch to see if the idea was worth pursuing. Neither you nor Christoph thought so at the time, so I dropped it. if there are performance improvements to be had from doing something like that, it's a more compelling idea than just "Hey, this removes a few lines of code and a bit of stack space from every caller".
Depends; I rather doubt that it would be useful for kvec case, but iovec one like that... The only thing that worries me here is the code generation for the cascades of ifs in lib/iov_iter.c - that'd need profiling.
And it's orthogonal to the overhead in new_sync_{read,write}(), obviously.
Christoph Hellwig
maintainerQuoted 20 lines · Al Viro
There's a problem with that variant. Take a look at
btrfs_direct_write():const bool is_sync_write = (iocb->ki_flags & IOCB_DSYNC); ... /* * We remove IOCB_DSYNC so that we don't deadlock when iomap_dio_rw() * calls generic_write_sync() (through iomap_dio_complete()), because * that results in calling fsync (btrfs_sync_file()) which will try to * lock the inode in exclusive/write mode. */ if (is_sync_write) iocb->ki_flags &= ~IOCB_DSYNC; ... /* * Add back IOCB_DSYNC. Our caller, btrfs_file_write_iter(), will do * the fsync (call generic_write_sync()). */ if (is_sync_write) iocb->ki_flags |= IOCB_DSYNC;
Quoted 1 line
will run into trouble. How about this (completely untested):
Which is pretty gross. We can just add a IOMAP_DIO_NOSYNC flag to do what btrfs wants is a much less gross way.
Quoted 5 lines
Precalculate
iocb_flags()Store the value in
file->f_i_flags; calculate at open time (do_dentry_open()for opens,alloc_file()for pipe(2)/socket(2)/etc.), update atFCNTL_SETFLtime.
Eww. I don't think we should grow struct file for tht.
Al Viro
maintainerQuoted 21 lines · Christoph Hellwig
Quoted 20 lines · Al Viro
There's a problem with that variant. Take a look at
btrfs_direct_write():const bool is_sync_write = (iocb->ki_flags & IOCB_DSYNC); ... /* * We remove IOCB_DSYNC so that we don't deadlock when iomap_dio_rw() * calls generic_write_sync() (through iomap_dio_complete()), because * that results in calling fsync (btrfs_sync_file()) which will try to * lock the inode in exclusive/write mode. */ if (is_sync_write) iocb->ki_flags &= ~IOCB_DSYNC; ... /* * Add back IOCB_DSYNC. Our caller, btrfs_file_write_iter(), will do * the fsync (call generic_write_sync()). */ if (is_sync_write) iocb->ki_flags |= IOCB_DSYNC;
Quoted 4 lines
Quoted 1 line
will run into trouble. How about this (completely untested):
Which is pretty gross. We can just add a
IOMAP_DIO_NOSYNCflag to do what btrfs wants is a much less gross way.
Add
#define IOMAP_DIO_NOSYNC (1 << 3)
in iomap.h, pass IOMAP_DIO_PARTIAL | IOMAP_DIO_NOSYNC in btrfs and do
if (iocb_is_dsync(iocb) && !(dio->flags & IOMAP_DIO_NOSYNC)) {in __iomap_dio_rw(), you mean?
I wonder if we want something of that sort in another user of IOMAP_DIO_PARTIAL (gfs2, that is)...
Quoted 1 line
Eww. I don't think we should grow struct file for tht.
Not a problem - this one is only used before the refcount hits zero and neither fu_llist nor fu_rcuhead are used until that happens. So it can go into the same union (and it's past time to make it that union anonymous, IMO)
Christoph Hellwig
maintainerQuoted 5 lines · Al Viro
Add
#defineIOMAP_DIO_NOSYNC(1 << 3)
iniomap.h, passIOMAP_DIO_PARTIAL|IOMAP_DIO_NOSYNCin btrfs and doif (iocb_is_dsync(iocb) && !(dio->flags & IOMAP_DIO_NOSYNC)) {in
__iomap_dio_rw(), you mean?
Roughly, yes.
Quoted 2 lines
I wonder if we want something of that sort in another user of
IOMAP_DIO_PARTIAL(gfs2, that is)...
I don't think gfs2 needs it, but it might be worth to take another look at how it handles sync writes.
Al Viro
maintainerQuoted 4 lines · Christoph Hellwig
Quoted 1 line · Matthew Wilcox
i suggested that to viro last night, and he pointed out that
ioctl(S_SYNC)Where would that
S_SYNCioctl be implemented?
S_SYNC is not an ioctl, it's an inode flag settable by such.FS_IOC_SETFLAGS with FS_SYNC_FL in flags, or FS_IOC_FSSETXATTR with FS_XFLAG_SYNC.