Skip to contentSkip to conversation

sysfs: correctly handle short reads on PREALLOC attrs.

PATCH In review NeilBrown 2 participants 2 messages Aug 5, 2015 – Aug 6, 2015 Driver core

lore
Share & tools
lore

NeilBrown

author
#0 lore raw

PATCH 1/1 sysfs: correctly handle short reads on PREALLOC attrs.

attributes declared with __ATTR_PREALLOC use sysfs_kf_read() which ignores the 'count' arg.
So a 1-byte read request can return more bytes than that.

This is seen with the 'dash' shell when 'read' is used on some 'md' sysfs attributes.

So only return the 'min' of count and the attribute length.

---
Possibly kernfs_file_direct_read should be changed instead (or as well) to use the min of 'count' and the return value from ops->read()

Thanks,
NeilBrown

fs/sysfs/file.c +3 -1
index 6c95628ea377..f35523d4fa3a 100644
@@ -108,6 +108,7 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
 {
 	const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
 	struct kobject *kobj = of->kn->parent->priv;
+	size_t len;
 
 	/*
 	 * If buf != of->prealloc_buf, we don't know how
@@ -115,7 +116,8 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
 	 */
 	if (pos || WARN_ON_ONCE(buf != of->prealloc_buf))
 		return 0;
-	return ops->show(kobj, of->kn->priv, buf);
+	len = ops->show(kobj, of->kn->priv, buf);
+	return min(count, len);
 }
 
 /* kernfs write callback for regular sysfs files */
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · NeilBrown

attributes declared with __ATTR_PREALLOC use sysfs_kf_read() which ignores the 'count' arg.
So a 1-byte read request can return more bytes than that.

Quoted 6 lines

This is seen with the 'dash' shell when 'read' is used on some 'md' sysfs attributes.

So only return the 'min' of count and the attribute length.

Thanks.

Signature · 1 line
tejun