Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp750425rdb; Fri, 8 Sep 2023 15:35:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFBJ8MvxurFiZ397tW9vg5KeitHf0hOSwA4nduCTTnBmX15Ryg26k2xCKORpUr0B1/E4Qn+ X-Received: by 2002:a17:902:c144:b0:1c0:cb31:d66 with SMTP id 4-20020a170902c14400b001c0cb310d66mr3711159plj.7.1694212542572; Fri, 08 Sep 2023 15:35:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694212542; cv=none; d=google.com; s=arc-20160816; b=hKpl1LaB5x4C6u/DNk7FVkre2e8/V9enUYEFj6kPYbVuucj8D94hxVwk2ACfIvkO/p kIN+rGHS+9avOYwf4C2e+VGOEM+GoK5Tlf6NIpjrnERaSGE4rBrFjRd+S+oE903CExE5 DozQKfq4fXuNyezJOdzmKZ402DAbcDF8oLp3fobM4DPeaQ4rR2jyAsGKuCHCrs627qQK ALRUOGhdscsgQV4eS3vnO5GBvIW7F7JNdtBzj7bqlfLf8H462cgl7IUNml9oge21k1zj jJkTyrhb+DDLqsaA6zu6+LBTX9hOiBRCnePZ/QtPLUWCZs8bP22yt/SKjj2WJeWd3dwI TpUw== 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=H0w/h30ida2PMg+LOls8odAvZahwrQ5WOiuuSuvbg3Q=; fh=FRJBrvPY375Ij4n8pHno+KUl/X/zm1YwHVxEBYHeOng=; b=uKtCfFCBCRUJ+EBOg1IpiJUm8YHWeDg2cJUFr6AcYsB7wdGR+xzCYctvXNa7qXJkJj kgjwT0mUufx275dsBkoWXUniHQte2oCM385GQgHUieLkNZ9KYZgQsKezFiDY4jbeAiSQ jBGU6D60NCWthd/7XreKQWPKWrLoqszacLQCY577YEHN8kAfjy7yYNzFM4O9HGUL73Dh x3RXxn/lRcIZWZ+DO8Ck5IFjRmEq+3T8XjnN4pdCKnGCbusXEx5tNbxLvFTetc1tjH+q UdjT889eDLszr6fF9glwIFxRMAt2ecKfWOfJeJw/F5FBIY+4CdHX7hvNqgOgEmou2GKw vpxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yandex-team.ru header.s=default header.b=fqdbDnhD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a11-20020a170902ee8b00b001bf193a70b3si2198677pld.298.2023.09.08.15.35.28; Fri, 08 Sep 2023 15:35:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@yandex-team.ru header.s=default header.b=fqdbDnhD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243632AbjIHNta (ORCPT + 99 others); Fri, 8 Sep 2023 09:49:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243541AbjIHNt2 (ORCPT ); Fri, 8 Sep 2023 09:49:28 -0400 Received: from forwardcorp1b.mail.yandex.net (forwardcorp1b.mail.yandex.net [178.154.239.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFFA11BF4 for ; Fri, 8 Sep 2023 06:49:19 -0700 (PDT) Received: from mail-nwsmtp-smtp-corp-canary-81.sas.yp-c.yandex.net (mail-nwsmtp-smtp-corp-canary-81.sas.yp-c.yandex.net [IPv6:2a02:6b8:c08:ac00:0:640:e1b9:0]) by forwardcorp1b.mail.yandex.net (Yandex) with ESMTP id 6795C60025; Fri, 8 Sep 2023 16:49:17 +0300 (MSK) Received: from valesini-ubuntu.yandex-team.ru (unknown [2a02:6b8:b081:b69c::1:20]) by mail-nwsmtp-smtp-corp-canary-81.sas.yp-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id 7na2h21OeKo0-T2qBrQie; Fri, 08 Sep 2023 16:49:16 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1694180956; bh=H0w/h30ida2PMg+LOls8odAvZahwrQ5WOiuuSuvbg3Q=; h=Message-Id:Date:In-Reply-To:Cc:Subject:References:To:From; b=fqdbDnhDjql0LgkHOi9BR3RjuTm9VcZHxOcMDEgCc4pp8X0DomP7p/LL0urXZfmrd DggHMd54lfvv4ghJwbaaYDN9ynm2w9srBgvvqBkMCyXqp2sP1qI6DOUIBZOZROhC2h npahztAfUQBWi9UblS0DGIJhtXDoPPcJAbHoCozo= Authentication-Results: mail-nwsmtp-smtp-corp-canary-81.sas.yp-c.yandex.net; dkim=pass header.i=@yandex-team.ru From: Valentine Sinitsyn To: Greg Kroah-Hartman , Tejun Heo Cc: Daniel Vetter , Bjorn Helgaas , Dan Williams , linux-kernel@vger.kernel.org Subject: [PATCH v7 1/2] kernfs: sysfs: support custom llseek method for sysfs entries Date: Fri, 8 Sep 2023 18:49:06 +0500 Message-Id: <20230908134907.121407-1-valesini@yandex-team.ru> X-Mailer: git-send-email 2.34.1 In-Reply-To: <202309051442.bd6f9879-oliver.sang@intel.com> References: <202309051442.bd6f9879-oliver.sang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org As of now, seeking in sysfs files is handled by generic_file_llseek(). There are situations where one may want to customize seeking logic: - Many sysfs entries are fixed files while generic_file_llseek() accepts past-the-end positions. Not only being useless by itself, this also means a bug in userspace code will trigger not at lseek(), but at some later point making debugging harder. - generic_file_llseek() relies on f_mapping->host to get the file size which might not be correct for all sysfs entries. See commit 636b21b50152 ("PCI: Revoke mappings like devmem") as an example. Implement llseek method to override this behavior at sysfs attribute level. The method is optional, and if it is absent, generic_file_llseek() is called to preserve backwards compatibility. Cc: stable@vger.kernel.org Signed-off-by: Valentine Sinitsyn --- v7: - Use proper locking in kernfs_fop_llseek() v6: - Mark pci_llseek_resource() as __maybe_unused - Fix a typo in pci_create_legacy_files() v5: - Fix builds without PCI mmap support (e.g. Alpha) v4: - Fix builds which #define HAVE_PCI_LEGACY (e.g. PowerPC) v3: - Grammar fixes - Add base-patch: and prerequisite-patch-id: to make kernel test robot happy v2: - Add infrastructure to customize llseek per sysfs entry type - Override llseek for PCI sysfs entries with fixed_file_llseek() fs/kernfs/file.c | 29 ++++++++++++++++++++++++++++- fs/sysfs/file.c | 13 +++++++++++++ include/linux/kernfs.h | 1 + include/linux/sysfs.h | 2 ++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 180906c36f51..1a76fb8128ce 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -903,6 +903,33 @@ 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) +{ + struct kernfs_open_file *of = kernfs_of(file); + const struct kernfs_ops *ops; + loff_t ret; + + /* + * @of->mutex nests outside active ref and is primarily to ensure that + * the ops aren't called concurrently for the same open file. + */ + mutex_lock(&of->mutex); + if (!kernfs_get_active(of->kn)) { + mutex_unlock(&of->mutex); + return -ENODEV; + } + + ops = kernfs_ops(of->kn); + if (ops->llseek) + ret = ops->llseek(of, offset, whence); + else + ret = generic_file_llseek(file, offset, whence); + + mutex_unlock(&of->mutex); + kernfs_put_active(of->kn); + return ret; +} + static void kernfs_notify_workfn(struct work_struct *work) { struct kernfs_node *kn; @@ -1005,7 +1032,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, diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c index a12ac0356c69..6b7652fb8050 100644 --- a/fs/sysfs/file.c +++ b/fs/sysfs/file.c @@ -167,6 +167,18 @@ static int sysfs_kf_bin_mmap(struct kernfs_open_file *of, return battr->mmap(of->file, kobj, battr, vma); } +static loff_t sysfs_kf_bin_llseek(struct kernfs_open_file *of, loff_t offset, + int whence) +{ + struct bin_attribute *battr = of->kn->priv; + struct kobject *kobj = of->kn->parent->priv; + + if (battr->llseek) + return battr->llseek(of->file, kobj, battr, offset, whence); + else + return generic_file_llseek(of->file, offset, whence); +} + static int sysfs_kf_bin_open(struct kernfs_open_file *of) { struct bin_attribute *battr = of->kn->priv; @@ -249,6 +261,7 @@ static const struct kernfs_ops sysfs_bin_kfops_mmap = { .write = sysfs_kf_bin_write, .mmap = sysfs_kf_bin_mmap, .open = sysfs_kf_bin_open, + .llseek = sysfs_kf_bin_llseek, }; int sysfs_add_file_mode_ns(struct kernfs_node *parent, diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 2a36f3218b51..99aaa050ccb7 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -316,6 +316,7 @@ struct kernfs_ops { struct poll_table_struct *pt); int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma); + loff_t (*llseek)(struct kernfs_open_file *of, loff_t offset, int whence); }; /* diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index fd3fe5c8c17f..b717a70219f6 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -181,6 +181,8 @@ struct bin_attribute { char *, loff_t, size_t); ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *, char *, loff_t, size_t); + loff_t (*llseek)(struct file *, struct kobject *, struct bin_attribute *, + loff_t, int); int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr, struct vm_area_struct *vma); }; base-commit: a48fa7efaf1161c1c898931fe4c7f0070964233a -- 2.34.1