From: Muralidhara M K <muralidhara.mk@amd.com>
To: <ilpo.jarvinen@linux.intel.com>, <gregkh@linuxfoundation.org>,
<rafael@kernel.org>
Cc: <platform-driver-x86@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, <driver-core@lists.linux.dev>,
Muralidhara M K <muralidhara.mk@amd.com>,
Nayak K Prateek <kprateek.nayak@amd.com>
Subject: [PATCH v2 4/7] sysfs: Add SYSFS_HUGE_BIN_FILE flag for binary attributes larger than PAGE_SIZE
Date: Mon, 27 Apr 2026 21:21:26 +0530 [thread overview]
Message-ID: <20260427155129.545327-5-muralidhara.mk@amd.com> (raw)
In-Reply-To: <20260427155129.545327-1-muralidhara.mk@amd.com>
Historically, sysfs read buffers were allocated with get_zeroed_page(),
limiting reads to PAGE_SIZE. Commit 13c589d5b0ac ("sysfs: use seq_file
when reading regular files") transitioned regular (text) attribute reads
to seq_file, which can dynamically grow buffers beyond PAGE_SIZE.
However, the PAGE_SIZE limit was intentionally preserved for
compatibility. When binary attribute handling was later unified into
the same codebase, the non-seq_file read path (kernfs_file_read_iter)
retained this PAGE_SIZE cap for binary files as well.
Drivers that expose binary attributes larger than PAGE_SIZE — such as
the AMD HSMP metric table (~13 KB) — cannot deliver the full content
in a single read() call through the existing path.
Introduce a new opt-in flag SYSFS_HUGE_BIN_FILE (040000) that drivers
can OR into their bin_attribute mode. When set, sysfs selects a new
kernfs_ops (sysfs_bin_kfops_huge_file_ro) whose .seq_show callback
pipes the bin_attribute ->read() result through seq_file, allowing
reads of arbitrary size in one shot. Existing binary attributes
without the flag continue using the legacy capped path.
Co-developed-by: Nayak K Prateek <kprateek.nayak@amd.com>
Signed-off-by: Nayak K Prateek <kprateek.nayak@amd.com>
Signed-off-by: Muralidhara M K <muralidhara.mk@amd.com>
---
Changes v1->v2: New patch
fs/sysfs/file.c | 45 +++++++++++++++++++++++++++++++++++++++++++
fs/sysfs/group.c | 8 ++++----
include/linux/sysfs.h | 1 +
3 files changed, 50 insertions(+), 4 deletions(-)
diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index 5709cede1d75..be42c3c1e056 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -38,6 +38,45 @@ static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
}
+/*
+ * Reads on huge sysfs bin files are handled through seq_file, which
+ * takes care of hairy details like buffering and seeking. The
+ * following function pipes the bin_attribute ->read() result through
+ * seq_file so that reads larger than PAGE_SIZE work in one shot.
+ */
+static int sysfs_kf_huge_file_seq_show(struct seq_file *sf, void *v)
+{
+ struct kernfs_open_file *of = sf->private;
+ const struct bin_attribute *battr = of->kn->priv;
+ struct kobject *kobj = sysfs_file_kobj(of->kn);
+ loff_t size = file_inode(of->file)->i_size;
+ ssize_t count;
+ char *buf;
+
+ if (!battr->read)
+ return -EIO;
+
+ if (!size)
+ return -EIO;
+
+ /* acquire buffer and ensure that it's >= size */
+ count = seq_get_buf(sf, &buf);
+ if (count < size) {
+ seq_commit(sf, -1);
+ return 0;
+ }
+
+ memset(buf, 0, size);
+
+ count = battr->read(of->file, kobj, battr, buf, 0, size);
+ if (count < 0)
+ return count;
+
+ WARN_ON(count > size);
+ seq_commit(sf, min_t(ssize_t, count, size));
+ return 0;
+}
+
/*
* Reads on sysfs are handled through seq_file, which takes care of hairy
* details like buffering and seeking. The following function pipes
@@ -249,6 +288,10 @@ static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
.prealloc = true,
};
+static const struct kernfs_ops sysfs_bin_kfops_huge_file_ro = {
+ .seq_show = sysfs_kf_huge_file_seq_show,
+};
+
static const struct kernfs_ops sysfs_bin_kfops_ro = {
.read = sysfs_kf_bin_read,
};
@@ -333,6 +376,8 @@ int sysfs_add_bin_file_mode_ns(struct kernfs_node *parent,
ops = &sysfs_bin_kfops_mmap;
else if (battr->read && battr->write)
ops = &sysfs_bin_kfops_rw;
+ else if (battr->read && (mode & SYSFS_HUGE_BIN_FILE))
+ ops = &sysfs_bin_kfops_huge_file_ro;
else if (battr->read)
ops = &sysfs_bin_kfops_ro;
else if (battr->write)
diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c
index b3edae0578c0..2d0b01c00a97 100644
--- a/fs/sysfs/group.c
+++ b/fs/sysfs/group.c
@@ -74,11 +74,11 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
continue;
}
- WARN(mode & ~(SYSFS_PREALLOC | 0664),
+ WARN(mode & ~(SYSFS_PREALLOC | SYSFS_HUGE_BIN_FILE | 0664),
"Attribute %s: Invalid permissions 0%o\n",
(*attr)->name, mode);
- mode &= SYSFS_PREALLOC | 0664;
+ mode &= SYSFS_PREALLOC | SYSFS_HUGE_BIN_FILE | 0664;
error = sysfs_add_file_mode_ns(parent, *attr, mode, uid,
gid, NULL);
if (unlikely(error))
@@ -107,11 +107,11 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
if (grp->bin_size)
size = grp->bin_size(kobj, *bin_attr, i);
- WARN(mode & ~(SYSFS_PREALLOC | 0664),
+ WARN(mode & ~(SYSFS_PREALLOC | SYSFS_HUGE_BIN_FILE | 0664),
"Attribute %s: Invalid permissions 0%o\n",
(*bin_attr)->attr.name, mode);
- mode &= SYSFS_PREALLOC | 0664;
+ mode &= SYSFS_PREALLOC | SYSFS_HUGE_BIN_FILE | 0664;
error = sysfs_add_bin_file_mode_ns(parent, *bin_attr,
mode, size, uid, gid,
NULL);
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index b1a3a1e6ad09..78f6c6252cf9 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -124,6 +124,7 @@ struct attribute_group {
#define SYSFS_PREALLOC 010000
#define SYSFS_GROUP_INVISIBLE 020000
+#define SYSFS_HUGE_BIN_FILE 040000
/*
* DEFINE_SYSFS_GROUP_VISIBLE(name):
--
2.34.1
next prev parent reply other threads:[~2026-04-27 15:52 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-27 15:51 [PATCH v2 0/7] AMD HSMP: metrics table improvements and Family 1Ah Model 50h-5Fh support Muralidhara M K
2026-04-27 15:51 ` [PATCH v2 1/7] platform/x86/amd/hsmp: Add new HSMP messages for Family 1Ah, Model 50h-5Fh Muralidhara M K
2026-04-27 15:51 ` [PATCH v2 2/7] platform/x86/amd/hsmp: Add metrics table support for Family 1Ah " Muralidhara M K
2026-04-27 15:51 ` [PATCH v2 3/7] platform/x86/amd/hsmp: Unify response_sz validation to an upper-bound check Muralidhara M K
2026-04-27 15:51 ` Muralidhara M K [this message]
2026-04-28 7:20 ` [PATCH v2 4/7] sysfs: Add SYSFS_HUGE_BIN_FILE flag for binary attributes larger than PAGE_SIZE K Prateek Nayak
2026-04-27 15:51 ` [PATCH v2 5/7] platform/x86/amd/hsmp: Add dynamic table size for metric table Muralidhara M K
2026-04-27 15:51 ` [PATCH v2 6/7] platform/x86/amd/hsmp: Make metric table read locking use guard(mutex) Muralidhara M K
2026-04-27 15:51 ` [PATCH v2 7/7] platform/x86/amd/hsmp: Support SYSFS_HUGE_BIN_FILE for metric table reads Muralidhara M K
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=20260427155129.545327-5-muralidhara.mk@amd.com \
--to=muralidhara.mk@amd.com \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=platform-driver-x86@vger.kernel.org \
--cc=rafael@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