From: Al Viro <viro@zeniv.linux.org.uk>
To: Christoph Hellwig <hch@lst.de>
Cc: Matthew Wilcox <willy@infradead.org>,
Jens Axboe <axboe@kernel.dk>, Xiubo Li <xiubli@redhat.com>,
Ilya Dryomov <idryomov@gmail.com>,
Christian Brauner <brauner@kernel.org>,
Theodore Ts'o <tytso@mit.edu>, Jaegeuk Kim <jaegeuk@kernel.org>,
Chao Yu <chao@kernel.org>, Miklos Szeredi <miklos@szeredi.hu>,
Andreas Gruenbacher <agruenba@redhat.com>,
"Darrick J. Wong" <djwong@kernel.org>,
Trond Myklebust <trond.myklebust@hammerspace.com>,
Anna Schumaker <anna@kernel.org>,
Damien Le Moal <dlemoal@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
linux-block@vger.kernel.org, ceph-devel@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org,
linux-f2fs-devel@lists.sourceforge.net, cluster-devel@redhat.com,
linux-xfs@vger.kernel.org, linux-nfs@vger.kernel.org,
linux-mm@kvack.org, Hannes Reinecke <hare@suse.de>
Subject: Re: [PATCH 03/12] filemap: update ki_pos in generic_perform_write
Date: Sun, 27 Aug 2023 20:41:22 +0100 [thread overview]
Message-ID: <20230827194122.GA325446@ZenIV> (raw)
In-Reply-To: <20230601145904.1385409-4-hch@lst.de>
On Thu, Jun 01, 2023 at 04:58:55PM +0200, Christoph Hellwig wrote:
> All callers of generic_perform_write need to updated ki_pos, move it into
> common code.
> @@ -4034,7 +4037,6 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
> endbyte = pos + status - 1;
> err = filemap_write_and_wait_range(mapping, pos, endbyte);
> if (err == 0) {
> - iocb->ki_pos = endbyte + 1;
> written += status;
> invalidate_mapping_pages(mapping,
> pos >> PAGE_SHIFT,
> @@ -4047,8 +4049,6 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
> }
> } else {
> written = generic_perform_write(iocb, from);
> - if (likely(written > 0))
> - iocb->ki_pos += written;
> }
> out:
> return written ? written : err;
[another late reply, sorry]
That part is somewhat fishy - there's a case where you return a positive value
and advance ->ki_pos by more than that amount. I really wonder if all callers
of ->write_iter() are OK with that. Consider e.g. this:
ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
{
struct fd f = fdget_pos(fd);
ssize_t ret = -EBADF;
if (f.file) {
loff_t pos, *ppos = file_ppos(f.file);
if (ppos) {
pos = *ppos;
ppos = &pos;
}
ret = vfs_write(f.file, buf, count, ppos);
if (ret >= 0 && ppos)
f.file->f_pos = pos;
fdput_pos(f);
}
return ret;
}
ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
{
ssize_t ret;
if (!(file->f_mode & FMODE_WRITE))
return -EBADF;
if (!(file->f_mode & FMODE_CAN_WRITE))
return -EINVAL;
if (unlikely(!access_ok(buf, count)))
return -EFAULT;
ret = rw_verify_area(WRITE, file, pos, count);
if (ret)
return ret;
if (count > MAX_RW_COUNT)
count = MAX_RW_COUNT;
file_start_write(file);
if (file->f_op->write)
ret = file->f_op->write(file, buf, count, pos);
else if (file->f_op->write_iter)
ret = new_sync_write(file, buf, count, pos);
else
ret = -EINVAL;
if (ret > 0) {
fsnotify_modify(file);
add_wchar(current, ret);
}
inc_syscw(current);
file_end_write(file);
return ret;
}
static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
{
struct kiocb kiocb;
struct iov_iter iter;
ssize_t ret;
init_sync_kiocb(&kiocb, filp);
kiocb.ki_pos = (ppos ? *ppos : 0);
iov_iter_ubuf(&iter, ITER_SOURCE, (void __user *)buf, len);
ret = call_write_iter(filp, &kiocb, &iter);
BUG_ON(ret == -EIOCBQUEUED);
if (ret > 0 && ppos)
*ppos = kiocb.ki_pos;
return ret;
}
Suppose ->write_iter() ends up doing returning a positive value smaller than
the increment of kiocb.ki_pos. What do we get? ret is positive, so
kiocb.ki_pos gets copied into *ppos, which is ksys_write's pos and there
we copy it into file->f_pos.
Is it really OK to have write() return 4096 and advance the file position
by 16K? AFAICS, userland wouldn't get any indication of something
odd going on - just a short write to a regular file, with followup write
of remaining 12K getting quietly written in the range 16K..28K.
I don't remember what POSIX says about that, but it would qualify as
nasty surprise for any userland program - sure, one can check fsync()
results before closing the sucker and see if everything looks fine,
but the way it's usually discussed could easily lead to assumption that
(synchronous) O_DIRECT writes would not be affected by anything of that
sort.
next prev parent reply other threads:[~2023-08-27 19:43 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-01 14:58 cleanup the filemap / direct I/O interaction v4 Christoph Hellwig
2023-06-01 14:58 ` [PATCH 01/12] backing_dev: remove current->backing_dev_info Christoph Hellwig
2023-07-06 0:18 ` [f2fs-dev] " patchwork-bot+f2fs
2023-06-01 14:58 ` [PATCH 02/12] iomap: update ki_pos a little later in iomap_dio_complete Christoph Hellwig
2023-06-01 14:58 ` [PATCH 03/12] filemap: update ki_pos in generic_perform_write Christoph Hellwig
2023-08-27 19:41 ` Al Viro [this message]
2023-08-27 21:45 ` Al Viro
2023-08-28 12:32 ` Christoph Hellwig
2023-08-28 13:56 ` Al Viro
2023-08-28 14:15 ` Christoph Hellwig
2023-09-13 11:00 ` Christoph Hellwig
2023-09-13 16:33 ` Christian Brauner
2023-08-28 1:04 ` Al Viro
2023-08-28 12:30 ` Christoph Hellwig
2023-08-28 14:02 ` Al Viro
2023-06-01 14:58 ` [PATCH 04/12] filemap: add a kiocb_write_and_wait helper Christoph Hellwig
2023-06-01 14:58 ` [PATCH 05/12] filemap: add a kiocb_invalidate_pages helper Christoph Hellwig
2023-06-01 14:58 ` [PATCH 06/12] filemap: add a kiocb_invalidate_post_direct_write helper Christoph Hellwig
2023-06-01 14:58 ` [PATCH 07/12] iomap: update ki_pos in iomap_file_buffered_write Christoph Hellwig
2023-06-01 14:59 ` [PATCH 08/12] iomap: use kiocb_write_and_wait and kiocb_invalidate_pages Christoph Hellwig
2023-06-01 14:59 ` [PATCH 09/12] fs: factor out a direct_write_fallback helper Christoph Hellwig
2023-06-06 0:04 ` Darrick J. Wong
2023-06-06 6:43 ` Christoph Hellwig
2023-06-01 14:59 ` [PATCH 10/12] fuse: update ki_pos in fuse_perform_write Christoph Hellwig
2023-06-01 14:59 ` [PATCH 11/12] fuse: drop redundant arguments to fuse_perform_write Christoph Hellwig
2023-06-01 14:59 ` [PATCH 12/12] fuse: use direct_write_fallback Christoph Hellwig
-- strict thread matches above, loose matches on Subject: below --
2023-05-31 7:50 cleanup the filemap / direct I/O interaction v3 (full series now) Christoph Hellwig
2023-05-31 7:50 ` [PATCH 03/12] filemap: update ki_pos in generic_perform_write Christoph Hellwig
2023-06-01 4:06 ` Theodore Ts'o
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230827194122.GA325446@ZenIV \
--to=viro@zeniv.linux.org.uk \
--cc=agruenba@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=anna@kernel.org \
--cc=axboe@kernel.dk \
--cc=brauner@kernel.org \
--cc=ceph-devel@vger.kernel.org \
--cc=chao@kernel.org \
--cc=cluster-devel@redhat.com \
--cc=djwong@kernel.org \
--cc=dlemoal@kernel.org \
--cc=hare@suse.de \
--cc=hch@lst.de \
--cc=idryomov@gmail.com \
--cc=jaegeuk@kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-nfs@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=miklos@szeredi.hu \
--cc=trond.myklebust@hammerspace.com \
--cc=tytso@mit.edu \
--cc=willy@infradead.org \
--cc=xiubli@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).