Max number of bytes available for sysfs read
Asked Answered
C

1

7

To handle a sysfs read I need to create a show function which is added to a kobj_attribute structure. The prototype of the function is defined as:

ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
            char *buf);

Obviously I need to write data to the buf parameter, but what is the upper limit of the number of bytes which can be written? Is it defined anywhere?

Clynes answered 15/8, 2018 at 22:22 Comment(0)
S
9

According to Documentation/filesystems/sysfs.txt (search for "Reading/Writing Attribute Data") the buffer size is one page, or PAGE_SIZE bytes.

To avoid the warning below, you can effectively only use PAGE_SIZE - 1 bytes though:

        if (ret >= (ssize_t)PAGE_SIZE) {
                printk("dev_attr_show: %pS returned bad count\n",
                                dev_attr->show);
        }
Saunderson answered 15/8, 2018 at 22:25 Comment(1)
Perfect answer. I don't plan on writing more than about 10 bytes, but it's always safe to have a sanity check in place :)Clynes

© 2022 - 2024 — McMap. All rights reserved.