Skip to contentSkip to conversation

sysfs: correctly handle read offset on PREALLOC attrs

PATCH In review Konstantin Khlebnikov 4 participants 8 messages Jun 22, 2016 – Sep 5, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sysfs: correctly handle read offset on PREALLOC attrs

Attributes declared with __ATTR_PREALLOC use sysfs_kf_read() which returns zero bytes for non-zero offset. This breaks script checkarray in mdadm tool in debian where /bin/sh is 'dash' because its builtin 'read' reads only one byte at a time. Script gets 'i' instead of 'idle' when reads current action from /sys/block/$dev/md/sync_action and as a result does nothing.

This patch adds trivial implementation of partial read: generate whole string and move required part into buffer head.

Fixes 4ef67a8c95f3 ("sysfs/kernfs: make read requests on pre-alloc files use the buffer.")
Cc Stable <[email protected]> # v3.19+
---
 fs/sysfs/file.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/sysfs/file.c +7 -1
index f35523d4fa3a..b803213d1307 100644
@@ -114,9 +114,15 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
 	 * If buf != of->prealloc_buf, we don't know how
 	 * large it is, so cannot safely pass it to ->show
 	 */
-	if (pos || WARN_ON_ONCE(buf != of->prealloc_buf))
+	if (WARN_ON_ONCE(buf != of->prealloc_buf))
 		return 0;
 	len = ops->show(kobj, of->kn->priv, buf);
+	if (pos) {
+		if (len <= pos)
+			return 0;
+		len -= pos;
+		memmove(buf, buf + pos, len);
+	}
 	return min(count, len);
 }
 
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Konstantin Khlebnikov

Attributes declared with __ATTR_PREALLOC use sysfs_kf_read() which returns zero bytes for non-zero offset. This breaks script checkarray in mdadm tool in debian where /bin/sh is 'dash' because its builtin 'read' reads only one byte at a time. Script gets 'i' instead of 'idle' when reads current action from /sys/block/$dev/md/sync_action and as a result does nothing.

Quoted 7 lines

This patch adds trivial implementation of partial read: generate whole string and move required part into buffer head.

Fixes 4ef67a8c95f3 ("sysfs/kernfs: make read requests on pre-alloc files use the buffer.")
Cc Stable <[email protected]> # v3.19+

Thanks.

Signature · 1 line
tejun
#2 lore raw
In reply to Konstantin Khlebnikov

Bump

Quoted 39 lines · Konstantin Khlebnikov

Attributes declared with __ATTR_PREALLOC use sysfs_kf_read() which returns zero bytes for non-zero offset. This breaks script checkarray in mdadm tool in debian where /bin/sh is 'dash' because its builtin 'read' reads only one byte at a time. Script gets 'i' instead of 'idle' when reads current action from /sys/block/$dev/md/sync_action and as a result does nothing.

This patch adds trivial implementation of partial read: generate whole string and move required part into buffer head.

Fixes 4ef67a8c95f3 ("sysfs/kernfs: make read requests on pre-alloc files use the buffer.")
Cc Stable <[email protected]> # v3.19+
---
 fs/sysfs/file.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/sysfs/file.c +7 -1
index f35523d4fa3a..b803213d1307 100644
@@ -114,9 +114,15 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
         * If buf != of->prealloc_buf, we don't know how
         * large it is, so cannot safely pass it to ->show
         */
-       if (pos || WARN_ON_ONCE(buf != of->prealloc_buf))
+       if (WARN_ON_ONCE(buf != of->prealloc_buf))
                return 0;
        len = ops->show(kobj, of->kn->priv, buf);
+       if (pos) {
+               if (len <= pos)
+                       return 0;
+               len -= pos;
+               memmove(buf, buf + pos, len);
+       }
        return min(count, len);
 }
 
#3 lore raw
Quoted 1 line · Konstantin Khlebnikov

Bump

Huh?

Quoted 31 lines
Quoted 29 lines · Konstantin Khlebnikov

Attributes declared with __ATTR_PREALLOC use sysfs_kf_read() which returns zero bytes for non-zero offset. This breaks script checkarray in mdadm tool in debian where /bin/sh is 'dash' because its builtin 'read' reads only one byte at a time. Script gets 'i' instead of 'idle' when reads current action from /sys/block/$dev/md/sync_action and as a result does nothing.

This patch adds trivial implementation of partial read: generate whole string and move required part into buffer head.

Fixes 4ef67a8c95f3 ("sysfs/kernfs: make read requests on pre-alloc files use the buffer.")
Cc Stable <[email protected]> # v3.19+
---
 fs/sysfs/file.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/sysfs/file.c +1 -1
index f35523d4fa3a..b803213d1307 100644
@@ -114,9 +114,15 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
         * If buf != of->prealloc_buf, we don't know how
         * large it is, so cannot safely pass it to ->show
         */
-       if (pos || WARN_ON_ONCE(buf != of->prealloc_buf))
+       if (WARN_ON_ONCE(buf != of->prealloc_buf))
                return 0;
        len = ops->show(kobj, of->kn->priv, buf);
Quoted 8 lines

fs/sysfs/file.c static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf, · line 127

Quoted 8 lines
fs/sysfs/file.c +6 -0
+       if (pos) {
+               if (len <= pos)
+                       return 0;
+               len -= pos;
+               memmove(buf, buf + pos, len);
+       }
        return min(count, len);
 }

I don't have this in any queue of mine, so I don't understand what you are asking about.

totally confused,

greg k-h

author Acked-by Tejun Heo
#4 lore raw
Quoted 48 lines · Greg Kroah-Hartman
Quoted 1 line · Konstantin Khlebnikov

Bump

Huh?

Quoted 31 lines
Quoted 29 lines · Konstantin Khlebnikov

Attributes declared with __ATTR_PREALLOC use sysfs_kf_read() which returns zero bytes for non-zero offset. This breaks script checkarray in mdadm tool in debian where /bin/sh is 'dash' because its builtin 'read' reads only one byte at a time. Script gets 'i' instead of 'idle' when reads current action from /sys/block/$dev/md/sync_action and as a result does nothing.

This patch adds trivial implementation of partial read: generate whole string and move required part into buffer head.

Fixes 4ef67a8c95f3 ("sysfs/kernfs: make read requests on pre-alloc files use the buffer.")
Cc Stable <[email protected]> # v3.19+
---
 fs/sysfs/file.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/sysfs/file.c +1 -1
index f35523d4fa3a..b803213d1307 100644
@@ -114,9 +114,15 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
         * If buf != of->prealloc_buf, we don't know how
         * large it is, so cannot safely pass it to ->show
         */
-       if (pos || WARN_ON_ONCE(buf != of->prealloc_buf))
+       if (WARN_ON_ONCE(buf != of->prealloc_buf))
                return 0;
        len = ops->show(kobj, of->kn->priv, buf);
Quoted 8 lines

fs/sysfs/file.c static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf, · line 127

Quoted 8 lines
fs/sysfs/file.c +6 -0
+       if (pos) {
+               if (len <= pos)
+                       return 0;
+               len -= pos;
+               memmove(buf, buf + pos, len);
+       }
        return min(count, len);
 }

I don't have this in any queue of mine, so I don't understand what you are asking about.

Quoted 2 lines

totally confused,

Well.. One user poked me about this bug.

Patch was

get_maintainer.pl points to you but probably this more VFS problem. so... this time I've added Al into CC Should I resend patch once again, or this very trivial but pretty dangerous problem could be solved without this?

IIRR this breaks automatic recheck of MD RAIDs for all debian-based systems

Signature · 1 line
Konstantin
#6 lore raw
In reply to Greg Kroah-Hartman
Quoted 48 lines · Greg Kroah-Hartman
Quoted 1 line · Konstantin Khlebnikov

Bump

Huh?

Quoted 31 lines
Quoted 29 lines · Konstantin Khlebnikov

Attributes declared with __ATTR_PREALLOC use sysfs_kf_read() which returns zero bytes for non-zero offset. This breaks script checkarray in mdadm tool in debian where /bin/sh is 'dash' because its builtin 'read' reads only one byte at a time. Script gets 'i' instead of 'idle' when reads current action from /sys/block/$dev/md/sync_action and as a result does nothing.

This patch adds trivial implementation of partial read: generate whole string and move required part into buffer head.

Fixes 4ef67a8c95f3 ("sysfs/kernfs: make read requests on pre-alloc files use the buffer.")
Cc Stable <[email protected]> # v3.19+
---
 fs/sysfs/file.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/sysfs/file.c +1 -1
index f35523d4fa3a..b803213d1307 100644
@@ -114,9 +114,15 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
         * If buf != of->prealloc_buf, we don't know how
         * large it is, so cannot safely pass it to ->show
         */
-       if (pos || WARN_ON_ONCE(buf != of->prealloc_buf))
+       if (WARN_ON_ONCE(buf != of->prealloc_buf))
                return 0;
        len = ops->show(kobj, of->kn->priv, buf);
Quoted 8 lines

fs/sysfs/file.c static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf, · line 127

Quoted 8 lines
fs/sysfs/file.c +6 -0
+       if (pos) {
+               if (len <= pos)
+                       return 0;
+               len -= pos;
+               memmove(buf, buf + pos, len);
+       }
        return min(count, len);
 }

I don't have this in any queue of mine, so I don't understand what you are asking about.

Quoted 1 line

totally confused,

Wait, this is already in Linus's tree, what are you asking for here?

still confused,

greg k-h

#5 lore raw
In reply to Konstantin Khlebnikov
Quoted 49 lines · Konstantin Khlebnikov
Quoted 48 lines · Greg Kroah-Hartman
Quoted 1 line · Konstantin Khlebnikov

Bump

Huh?

Quoted 31 lines
Quoted 29 lines · Konstantin Khlebnikov

Attributes declared with __ATTR_PREALLOC use sysfs_kf_read() which returns zero bytes for non-zero offset. This breaks script checkarray in mdadm tool in debian where /bin/sh is 'dash' because its builtin 'read' reads only one byte at a time. Script gets 'i' instead of 'idle' when reads current action from /sys/block/$dev/md/sync_action and as a result does nothing.

This patch adds trivial implementation of partial read: generate whole string and move required part into buffer head.

Fixes 4ef67a8c95f3 ("sysfs/kernfs: make read requests on pre-alloc files use the buffer.")
Cc Stable <[email protected]> # v3.19+
---
 fs/sysfs/file.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/sysfs/file.c +1 -1
index f35523d4fa3a..b803213d1307 100644
@@ -114,9 +114,15 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
         * If buf != of->prealloc_buf, we don't know how
         * large it is, so cannot safely pass it to ->show
         */
-       if (pos || WARN_ON_ONCE(buf != of->prealloc_buf))
+       if (WARN_ON_ONCE(buf != of->prealloc_buf))
                return 0;
        len = ops->show(kobj, of->kn->priv, buf);
Quoted 8 lines

fs/sysfs/file.c static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf, · line 127

Quoted 8 lines
fs/sysfs/file.c +6 -0
+       if (pos) {
+               if (len <= pos)
+                       return 0;
+               len -= pos;
+               memmove(buf, buf + pos, len);
+       }
        return min(count, len);
 }

I don't have this in any queue of mine, so I don't understand what you are asking about.

Quoted 4 lines
Quoted 2 lines

totally confused,

Well.. One user poked me about this bug.

As I just mentioned, this is in 4.8-rc5, and will show up in a stable release "soon". So I don't understand what else we can do here :)

thanks,

greg k-h

#7 lore raw
In reply to Greg Kroah-Hartman
Quoted 49 lines · Greg Kroah-Hartman
Quoted 48 lines · Greg Kroah-Hartman
Quoted 1 line · Konstantin Khlebnikov

Bump

Huh?

Quoted 31 lines
Quoted 29 lines · Konstantin Khlebnikov

Attributes declared with __ATTR_PREALLOC use sysfs_kf_read() which returns zero bytes for non-zero offset. This breaks script checkarray in mdadm tool in debian where /bin/sh is 'dash' because its builtin 'read' reads only one byte at a time. Script gets 'i' instead of 'idle' when reads current action from /sys/block/$dev/md/sync_action and as a result does nothing.

This patch adds trivial implementation of partial read: generate whole string and move required part into buffer head.

Fixes 4ef67a8c95f3 ("sysfs/kernfs: make read requests on pre-alloc files use the buffer.")
Cc Stable <[email protected]> # v3.19+
---
 fs/sysfs/file.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/sysfs/file.c +1 -1
index f35523d4fa3a..b803213d1307 100644
@@ -114,9 +114,15 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
         * If buf != of->prealloc_buf, we don't know how
         * large it is, so cannot safely pass it to ->show
         */
-       if (pos || WARN_ON_ONCE(buf != of->prealloc_buf))
+       if (WARN_ON_ONCE(buf != of->prealloc_buf))
                return 0;
        len = ops->show(kobj, of->kn->priv, buf);
Quoted 8 lines

fs/sysfs/file.c static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf, · line 127

Quoted 8 lines
fs/sysfs/file.c +6 -0
+       if (pos) {
+               if (len <= pos)
+                       return 0;
+               len -= pos;
+               memmove(buf, buf + pos, len);
+       }
        return min(count, len);
 }

I don't have this in any queue of mine, so I don't understand what you are asking about.

Quoted 6 lines
Quoted 1 line

totally confused,

Wait, this is already in Linus's tree, what are you asking for here?

still confused,

Huh. Sorry. Haven't done git pull before checking - it only in lastest v4.8-rc5