public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kernfs: implement custom llseek method to fix userspace regression
@ 2023-08-14 19:08 Valentine Sinitsyn
  2023-08-14 20:01 ` Dan Williams
  0 siblings, 1 reply; 31+ messages in thread
From: Valentine Sinitsyn @ 2023-08-14 19:08 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Tejun Heo
  Cc: Daniel Vetter, Bjorn Helgaas, Dan Williams, linux-kernel

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.

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);
+
+	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


^ permalink raw reply related	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2023-09-08 13:56 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-14 19:08 [PATCH] kernfs: implement custom llseek method to fix userspace regression Valentine Sinitsyn
2023-08-14 20:01 ` Dan Williams
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox