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?

Twifty
  • 3,267
  • 1
  • 29
  • 54

1 Answers1

8

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);
        }
Julian Stecklina
  • 1,271
  • 1
  • 10
  • 24
user253751
  • 57,427
  • 7
  • 48
  • 90
  • 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 :) – Twifty Aug 15 '18 at 22:27