Monday, October 31, 2022

[SOLVED] Max number of bytes available for sysfs read

Issue

To handle a sysfs read I need to create a show function which is added to a kobj_attribute structure. The prototype of href="https://elixir.bootlin.com/linux/v4.14.63/source/include/linux/kobject.h#L145" rel="noreferrer">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?


Solution

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);
        }


Answered By - user253751
Answer Checked By - Timothy Miller (WPSolving Admin)