LinuxPPC-Dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Thorsten Blum <blum@kernel.org>
To: Madhavan Srinivasan <maddy@linux.ibm.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Nicholas Piggin <npiggin@gmail.com>,
	"Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
	Jeff Mahoney <jeffm@suse.com>
Cc: Thorsten Blum <blum@kernel.org>, linuxppc-dev@lists.ozlabs.org
Subject: [PATCH] macintosh/macio: Fix sysfs output in compatible_show()
Date: Thu, 26 Nov 2026 14:29:28 +0100	[thread overview]
Message-ID: <20261126132930.394539-2-blum@kernel.org> (raw)

compatible_show() incorrectly advances the buffer pointer by the
accumulated output length instead of the length of the last string.
If the property contains three or more strings, this leaves gaps in the
output and can truncate or omit later strings.

For example, a PowerMac11,2 OF dump [1] shows the compatible strings
"mac-io-i2c-control", "k2-i2c", and "keywest-i2c". The first two strings
take 19 and 7 bytes, but buf is advanced by 19 and then by 26, so the
third string is written at offset 45. However, only 38 bytes are
returned, so "keywest-i2c" is omitted from the sysfs output.

Use of_property_for_each_string() to iterate over the compatible strings
and sysfs_emit_at() to append each string at the correct offset.

Link: https://gist.github.com/rokuyama/079eebc318ad9417665f2c023527dac4 [1]
Fixes: b5bf5b6786cc ("[PATCH] openfirmware: add sysfs nodes for open firmware devices")
Cc: stable@vger.kernel.org
Signed-off-by: Thorsten Blum <blum@kernel.org>
---
Compile-tested only.

 drivers/macintosh/macio_sysfs.c | 20 ++++----------------
 1 file changed, 4 insertions(+), 16 deletions(-)

diff --git a/drivers/macintosh/macio_sysfs.c b/drivers/macintosh/macio_sysfs.c
index 2bbe359b26d9..59fb2a51de2d 100644
--- a/drivers/macintosh/macio_sysfs.c
+++ b/drivers/macintosh/macio_sysfs.c
@@ -3,30 +3,18 @@
 #include <linux/of.h>
 #include <linux/of_device.h>
 #include <linux/stat.h>
+#include <linux/sysfs.h>
 #include <asm/macio.h>
 
 static ssize_t
 compatible_show (struct device *dev, struct device_attribute *attr, char *buf)
 {
-	struct platform_device *of;
+	struct property *prop;
 	const char *compat;
-	int cplen;
 	int length = 0;
 
-	of = &to_macio_device (dev)->ofdev;
-	compat = of_get_property(of->dev.of_node, "compatible", &cplen);
-	if (!compat) {
-		*buf = '\0';
-		return 0;
-	}
-	while (cplen > 0) {
-		int l;
-		length += sprintf (buf, "%s\n", compat);
-		buf += length;
-		l = strlen (compat) + 1;
-		compat += l;
-		cplen -= l;
-	}
+	of_property_for_each_string(dev->of_node, "compatible", prop, compat)
+		length += sysfs_emit_at(buf, length, "%s\n", compat);
 
 	return length;
 }


                 reply	other threads:[~2026-11-26 13:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261126132930.394539-2-blum@kernel.org \
    --to=blum@kernel.org \
    --cc=chleroy@kernel.org \
    --cc=jeffm@suse.com \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mpe@ellerman.id.au \
    --cc=npiggin@gmail.com \
    /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