From: Dan Williams <dan.j.williams@intel.com>
To: Valentine Sinitsyn <valesini@yandex-team.ru>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Tejun Heo <tj@kernel.org>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>,
Bjorn Helgaas <bhelgaas@google.com>,
Dan Williams <dan.j.williams@intel.com>,
<linux-kernel@vger.kernel.org>
Subject: RE: [PATCH] kernfs: implement custom llseek method to fix userspace regression
Date: Mon, 14 Aug 2023 13:01:05 -0700 [thread overview]
Message-ID: <64da880166403_5ea6e29449@dwillia2-xfh.jf.intel.com.notmuch> (raw)
In-Reply-To: <20230814190850.948032-1-valesini@yandex-team.ru>
Valentine Sinitsyn wrote:
> Since commit 636b21b50152 ("PCI: Revoke mappings like devmem"),
> mmapable sysfs binary attributes have started receiving their
> f_mapping from the iomem pseudo filesystem, so that
> CONFIG_IO_STRICT_DEVMEM is honored in sysfs (and procfs) as well
> as in /dev/[k]mem.
>
> This resulted in a userspace-visible regression: lseek(fd, 0, SEEK_END)
> now returns zero regardless the real sysfs attribute size which stat()
> reports. The reason is that kernfs files use generic_file_llseek()
> implementation, which relies on f_mapping->host inode to get the file
> size. As f_mapping is now redefined, f_mapping->host points to an
> anonymous zero-sized iomem inode which has nothing to do with sysfs
> attribute or kernfs file representing it. This being said, f_inode
> remains valid, so stat() which uses it works correctly.
Can you say a bit more about what userspace scenario regressed so that
others doing backports can make a judgement call on the severity?
>
> Fixes the regression by implementing a custom llseek fop for kernfs,
> which uses an attribute's file inode to get the file size,
> just as stat() does.
>
> Fixes: 636b21b50152 ("PCI: Revoke mappings like devmem")
> Cc: stable@vger.kernel.org
> Signed-off-by: Valentine Sinitsyn <valesini@yandex-team.ru>
> ---
> fs/kernfs/file.c | 17 ++++++++++++++++-
> 1 file changed, 16 insertions(+), 1 deletion(-)
>
> diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
> index 180906c36f51..6d81e0c981f3 100644
> --- a/fs/kernfs/file.c
> +++ b/fs/kernfs/file.c
> @@ -903,6 +903,21 @@ static __poll_t kernfs_fop_poll(struct file *filp, poll_table *wait)
> return ret;
> }
>
> +static loff_t kernfs_fop_llseek(struct file *file, loff_t offset, int whence)
> +{
> + /*
> + * This is almost identical to generic_file_llseek() except it uses
> + * cached inode value instead of f_mapping->host.
> + * The reason is that, for PCI resources in sysfs the latter points to
> + * iomem_inode whose size has nothing to do with the attribute's size.
> + */
> + struct inode *inode = file_inode(file);
My only concern is whether there are any scenarios where this is not
appropriate. I.e. do a bit more work to define a kernfs_ops instance
specifically for overriding lseek() in this scenario.
> +
> + return generic_file_llseek_size(file, offset, whence,
> + inode->i_sb->s_maxbytes,
> + i_size_read(inode));
> +}
> +
> static void kernfs_notify_workfn(struct work_struct *work)
> {
> struct kernfs_node *kn;
> @@ -1005,7 +1020,7 @@ EXPORT_SYMBOL_GPL(kernfs_notify);
> const struct file_operations kernfs_file_fops = {
> .read_iter = kernfs_fop_read_iter,
> .write_iter = kernfs_fop_write_iter,
> - .llseek = generic_file_llseek,
> + .llseek = kernfs_fop_llseek,
> .mmap = kernfs_fop_mmap,
> .open = kernfs_fop_open,
> .release = kernfs_fop_release,
> --
> 2.34.1
>
next prev parent reply other threads:[~2023-08-14 20:02 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-14 19:08 Valentine Sinitsyn
2023-08-14 20:01 ` Dan Williams [this message]
2023-08-15 8:25 ` Valentin Sinitsyn
2023-08-15 15:48 ` Dan Williams
2023-08-17 18:53 ` Valentin Sinitsyn
2023-08-21 7:29 ` [RESEND PATCH v2 1/2] kernfs: sysfs: support custom llseek method for sysfs entries Valentine Sinitsyn
2023-08-21 7:29 ` [RESEND PATCH v2 2/2] PCI: implement custom llseek method for PCI resource entries in sysfs Valentine Sinitsyn
2023-08-21 19:55 ` Bjorn Helgaas
2023-08-22 7:51 ` [PATCH v3 1/2] kernfs: sysfs: support custom llseek method for sysfs entries Valentine Sinitsyn
2023-08-22 7:51 ` [PATCH v3 2/2] PCI: Implement custom llseek for sysfs resource entries Valentine Sinitsyn
2023-08-22 11:54 ` [PATCH v4 1/2] kernfs: sysfs: support custom llseek method for sysfs entries Valentine Sinitsyn
2023-08-22 11:54 ` [PATCH v4 2/2] PCI: Implement custom llseek for sysfs resource entries Valentine Sinitsyn
2023-08-22 17:43 ` kernel test robot
2023-08-23 12:58 ` [PATCH v5 1/2] kernfs: sysfs: support custom llseek method for sysfs entries Valentine Sinitsyn
2023-08-23 14:37 ` Greg Kroah-Hartman
2023-08-23 14:39 ` Greg Kroah-Hartman
2023-08-23 20:11 ` Valentin Sinitsyn
2023-08-23 12:58 ` [PATCH v5 2/2] PCI: Implement custom llseek for sysfs resource entries Valentine Sinitsyn
2023-08-25 14:10 ` kernel test robot
2023-08-28 7:22 ` Valentin Sinitsyn
2023-08-29 20:02 ` Bjorn Helgaas
2023-08-29 20:26 ` Bjorn Helgaas
2023-09-02 15:50 ` [PATCH v6 1/2] kernfs: sysfs: support custom llseek method for sysfs entries Valentine Sinitsyn
2023-09-05 7:02 ` kernel test robot
2023-09-08 13:49 ` [PATCH v7 " Valentine Sinitsyn
2023-09-08 13:49 ` [PATCH v7 2/2] PCI: Implement custom llseek for sysfs resource entries Valentine Sinitsyn
2023-09-08 13:56 ` [PATCH v8 1/2] kernfs: sysfs: support custom llseek method for sysfs entries Valentine Sinitsyn
2023-09-08 13:56 ` [PATCH v8 2/2] PCI: Implement custom llseek for sysfs resource entries Valentine Sinitsyn
2023-09-02 15:50 ` [PATCH v6 " Valentine Sinitsyn
2023-08-21 23:15 ` [RESEND PATCH v2 2/2] PCI: implement custom llseek method for PCI resource entries in sysfs kernel test robot
2023-08-22 8:09 ` Valentin Sinitsyn
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=64da880166403_5ea6e29449@dwillia2-xfh.jf.intel.com.notmuch \
--to=dan.j.williams@intel.com \
--cc=bhelgaas@google.com \
--cc=daniel.vetter@ffwll.ch \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=tj@kernel.org \
--cc=valesini@yandex-team.ru \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®