linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Valentine Sinitsyn <valesini@yandex-team.ru>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Tejun Heo <tj@kernel.org>, Bjorn Helgaas <bhelgaas@google.com>,
	Dan Williams <dan.j.williams@intel.com>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>, linux-kernel@vger.kernel.org
Subject: [PATCH v4 2/2] PCI: Implement custom llseek for sysfs resource entries
Date: Tue, 22 Aug 2023 16:54:55 +0500	[thread overview]
Message-ID: <20230822115455.310222-2-valesini@yandex-team.ru> (raw)
In-Reply-To: <20230822075107.224512-2-valesini@yandex-team.ru>

Since commit 636b21b50152 ("PCI: Revoke mappings like devmem"), mmappable
sysfs entries have started to receive 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:

1. Open a sysfs PCI resource file (eg. /sys/bus/pci/devices/*/resource0)
2. Use lseek(fd, 0, SEEK_END) to determine its size

Expected result: a PCI region size is returned.
Actual result: 0 is returned.

The reason is that PCI resource files residing in sysfs use
generic_file_llseek(), 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 file
in question.

Implement a custom llseek method for sysfs PCI resources, which is
almost the same as proc_bus_pci_lseek() used for procfs entries.

This makes sysfs and procfs entries consistent with regards to seeking,
but also introduces userspace-visible changes to seeking PCI resources
in sysfs:

- SEEK_DATA and SEEK_HOLE are no longer supported;
- Seeking past the end of the file is prohibited while previously
  offsets up to MAX_NON_LFS were accepted (reading from these offsets
  was always invalid).

Fixes: 636b21b50152 ("PCI: Revoke mappings like devmem")
Cc: stable@vger.kernel.org
Signed-off-by: Valentine Sinitsyn <valesini@yandex-team.ru>
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
---
 drivers/pci/pci-sysfs.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index ab32a91f287b..2b9d93f11dd1 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -836,6 +836,12 @@ static const struct attribute_group pci_dev_config_attr_group = {
 };
 
 #ifdef HAVE_PCI_LEGACY
+
+static loff_t pci_llseek_resource(struct file *filep,
+				  struct kobject *kobj,
+				  struct bin_attribute *attr,
+				  loff_t offset, int whence);
+
 /**
  * pci_read_legacy_io - read byte(s) from legacy I/O port space
  * @filp: open sysfs file
@@ -967,6 +973,8 @@ void pci_create_legacy_files(struct pci_bus *b)
 	b->legacy_io->attr.mode = 0600;
 	b->legacy_io->read = pci_read_legacy_io;
 	b->legacy_io->write = pci_write_legacy_io;
+	/* See pci_create_attr() for motivation */
+	b->legacy_io->llseek = pci_llseek_resource;
 	b->legacy_io->mmap = pci_mmap_legacy_io;
 	b->legacy_io->f_mapping = iomem_get_mapping;
 	pci_adjust_legacy_attr(b, pci_mmap_io);
@@ -981,6 +989,8 @@ void pci_create_legacy_files(struct pci_bus *b)
 	b->legacy_mem->size = 1024*1024;
 	b->legacy_mem->attr.mode = 0600;
 	b->legacy_mem->mmap = pci_mmap_legacy_mem;
+	/* See pci_create_attr() for motivation */
+	b->legacy_io->llseek = pci_llseek_resource;
 	b->legacy_mem->f_mapping = iomem_get_mapping;
 	pci_adjust_legacy_attr(b, pci_mmap_mem);
 	error = device_create_bin_file(&b->dev, b->legacy_mem);
@@ -1138,6 +1148,14 @@ static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
 	return pci_resource_io(filp, kobj, attr, buf, off, count, true);
 }
 
+static loff_t pci_llseek_resource(struct file *filep,
+				  struct kobject *kobj __always_unused,
+				  struct bin_attribute *attr,
+				  loff_t offset, int whence)
+{
+	return fixed_size_llseek(filep, offset, whence, attr->size);
+}
+
 /**
  * pci_remove_resource_files - cleanup resource files
  * @pdev: dev to cleanup
@@ -1195,8 +1213,15 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
 			res_attr->mmap = pci_mmap_resource_uc;
 		}
 	}
-	if (res_attr->mmap)
+	if (res_attr->mmap) {
 		res_attr->f_mapping = iomem_get_mapping;
+		/*
+		 * generic_file_llseek() consults f_mapping->host to determine
+		 * the file size. As iomem_inode knows nothing about the
+		 * attribute, it's not going to work, so override it as well.
+		 */
+		res_attr->llseek = pci_llseek_resource;
+	}
 	res_attr->attr.name = res_attr_name;
 	res_attr->attr.mode = 0600;
 	res_attr->size = pci_resource_len(pdev, num);
-- 
2.34.1


  parent reply	other threads:[~2023-08-22 11:55 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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               ` Valentine Sinitsyn [this message]
2023-08-22 17:43                 ` [PATCH v4 2/2] PCI: Implement custom llseek for sysfs resource entries 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=20230822115455.310222-2-valesini@yandex-team.ru \
    --to=valesini@yandex-team.ru \
    --cc=bhelgaas@google.com \
    --cc=dan.j.williams@intel.com \
    --cc=daniel.vetter@ffwll.ch \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tj@kernel.org \
    /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).