Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1098571rdh; Mon, 25 Sep 2023 03:17:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGaAPzaCw4PamPTN+GRVdvPtWgXPpqvutx00ju2/PWIAucOa+Wtv6QjjFRcqkmCvfPr74QM X-Received: by 2002:a25:daca:0:b0:d86:5844:a897 with SMTP id n193-20020a25daca000000b00d865844a897mr5833663ybf.5.1695637077661; Mon, 25 Sep 2023 03:17:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695637077; cv=none; d=google.com; s=arc-20160816; b=Z2Y8BZ+OtoVvb2wiaTJVJ2VawEURVQYR8qe8ODn73N38TrkGe1Fh6he00jcz+8Ia6P inEZGyaz/lf5bugyMw+XIA7Vg3mwU10aK4DTnHP77rPNwkbegphrtmgnRBgiBlpGgF1s nZ9lZJsDfrt1YIYRj2GtvEgN+gH6BPYAAC38PRAR3P3gfljHmt3qlw3B/QurbLgjnndO A0KpWVIeXAt1DvVuOsEQiEdk6hdZEpIM1esE/zr0Kdkj2V2vaknD5Xy2mXZm2xH46Hq/ bc+0Y9yE+7P6EbspBuEpJCunEpMXRzWM5I/K3wrhpPtQ7fZhWR5UYk/5RuTO9l90U4w0 ypxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XpSiMgstCaB6mxfNqbC2zgKSk3FX6BnjwH2YglOmEPc=; fh=qy7XMuwYhBh0AfpP7C/h3uQiMjzWp9Etm3Bd/ArEuEw=; b=IgFNfydvRlgvVmufP4JL9Gex9ou1BwZGjnQmGZzYnbOUazifWkhMwMvIlULRG46BZm Ke4d363pIWOyoCaMaJk6BYAbdRBXhibP5RNi+kL0G5j9IhUIMAJa2ghkQrMmpjQ+6p6t UIlzhIEe86GVBHd+/QtTpivHWUARV8yQU1fy9vvwqX4rxHepgHDHo9WFmh58zPgrEwu+ Oihvcv+98A96h3eq5NaJbIhy0vsY7QOd8815RCQb/MK6nIxAGzYjHGSILxjLy5HEdW7c AysA7+TXQYSvoRsGqub8dalyRa9hZONJUdxetpz1qIKlYLZzO9/6xmOzvMU8ICoPmmJL FLew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yandex-team.ru header.s=default header.b=Svadz1E5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=yandex-team.ru Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id g123-20020a636b81000000b0056a55b14f2bsi9186294pgc.285.2023.09.25.03.17.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 03:17:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@yandex-team.ru header.s=default header.b=Svadz1E5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=yandex-team.ru Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 03AF780AF26E; Mon, 25 Sep 2023 01:41:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232960AbjIYIkl (ORCPT + 99 others); Mon, 25 Sep 2023 04:40:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232974AbjIYIki (ORCPT ); Mon, 25 Sep 2023 04:40:38 -0400 Received: from forwardcorp1a.mail.yandex.net (forwardcorp1a.mail.yandex.net [178.154.239.72]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 661ADFC for ; Mon, 25 Sep 2023 01:40:30 -0700 (PDT) Received: from mail-nwsmtp-smtp-corp-main-83.vla.yp-c.yandex.net (mail-nwsmtp-smtp-corp-main-83.vla.yp-c.yandex.net [IPv6:2a02:6b8:c18:d11:0:640:6943:0]) by forwardcorp1a.mail.yandex.net (Yandex) with ESMTP id 2E3725FC6F; Mon, 25 Sep 2023 11:40:23 +0300 (MSK) Received: from valesini-ubuntu.yandex.net (unknown [2a02:6b8:0:419:ef2d:88e2:d835:7089]) by mail-nwsmtp-smtp-corp-main-83.vla.yp-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id DeJIo00OrSw0-TSrfTwG7; Mon, 25 Sep 2023 11:40:22 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1695631222; bh=XpSiMgstCaB6mxfNqbC2zgKSk3FX6BnjwH2YglOmEPc=; h=Message-Id:Date:In-Reply-To:Cc:Subject:References:To:From; b=Svadz1E5rOcwMmK9x7PKuN3gkyOGs0PPhiEmDZ7n/liwKc5SzFr6whjnZxhW+zDPu hzNy0UrzRoVb2JmXlaw2T2AU6VVdb6j3dIJSgvppd3fT5mT3ZhZX1RNEpt7Dl8FVvo 3QVkjU0uT5DUkrqspIVNHkzFbuiNhHFASAtZR5bI= Authentication-Results: mail-nwsmtp-smtp-corp-main-83.vla.yp-c.yandex.net; dkim=pass header.i=@yandex-team.ru From: Valentine Sinitsyn To: Greg Kroah-Hartman , Tejun Heo , "Rafael J. Wysocki" , Bjorn Helgaas Cc: Daniel Vetter , Dan Williams , linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org Subject: [PATCH v9 2/2] PCI: Implement custom llseek for sysfs resource entries Date: Mon, 25 Sep 2023 11:40:13 +0300 Message-Id: <20230925084013.309399-2-valesini@yandex-team.ru> X-Mailer: git-send-email 2.34.1 In-Reply-To: <2023092241-obedient-squirt-966c@gregkh> References: <2023092241-obedient-squirt-966c@gregkh> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 25 Sep 2023 01:41:06 -0700 (PDT) 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 Acked-by: Bjorn Helgaas --- drivers/pci/pci-sysfs.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index d9eede2dbc0e..97c9c62d5e3e 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -835,6 +835,19 @@ static const struct attribute_group pci_dev_config_attr_group = { .is_bin_visible = pci_dev_config_attr_is_visible, }; +/* + * llseek operation for mmappable PCI resources. + * May be left unused if the arch doesn't provide them. + */ +static __maybe_unused 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); +} + #ifdef HAVE_PCI_LEGACY /** * pci_read_legacy_io - read byte(s) from legacy I/O port space @@ -967,6 +980,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 +996,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_mem->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); @@ -1199,8 +1216,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